platform/netlink: simplify socket flags and use boolean fields

- replace "s_flags" field by explicit boolean fields.

- "s_msg_peek" now is simplified. Previously, we would default
  to peek, unless the user caller nl_socket_disable_msg_peek()
  or set nl_socket_set_msg_buf_size(). Simplify that. We now
  default to peek, unless NL_SOCKET_FLAGS_DISABLE_MSG_PEEK is set.

  We have no callers that call nl_socket_set_msg_buf_size(),
  so we can simplify that logic and just enable peeking by default.

- keep "s_auto_ack" field, although it is always TRUE and there
  is no API to toggle that. However, it is kept as a self-documenting
  thing, so we would know the relevant places where auto-ack matters.

- drop nl_socket_disable_msg_peek(). We have no caller of this function
  and we can set peeking in nl_socket_new(). We also don't need to
  change it after creation of the socket.
This commit is contained in:
Thomas Haller
2022-06-23 18:29:39 +02:00
parent c09b37f3c7
commit 9cd986ba2e
3 changed files with 9 additions and 21 deletions

View File

@@ -28,10 +28,6 @@
} \ } \
G_STMT_END G_STMT_END
#define NL_MSG_PEEK (1 << 3)
#define NL_MSG_PEEK_EXPLICIT (1 << 4)
#define NL_NO_AUTO_ACK (1 << 5)
#ifndef NETLINK_EXT_ACK #ifndef NETLINK_EXT_ACK
#define NETLINK_EXT_ACK 11 #define NETLINK_EXT_ACK 11
#endif #endif
@@ -49,12 +45,13 @@ struct nl_msg {
struct nl_sock { struct nl_sock {
struct sockaddr_nl s_local; struct sockaddr_nl s_local;
struct sockaddr_nl s_peer; struct sockaddr_nl s_peer;
size_t s_bufsize;
int s_fd; int s_fd;
int s_proto; int s_proto;
unsigned int s_seq_next; unsigned int s_seq_next;
unsigned int s_seq_expect; unsigned int s_seq_expect;
int s_flags; bool s_msg_peek : 1;
size_t s_bufsize; bool s_auto_ack : 1;
}; };
/*****************************************************************************/ /*****************************************************************************/
@@ -1051,13 +1048,6 @@ nl_socket_set_ext_ack(struct nl_sock *sk, gboolean enable)
return 0; return 0;
} }
void
nl_socket_disable_msg_peek(struct nl_sock *sk)
{
sk->s_flags |= NL_MSG_PEEK_EXPLICIT;
sk->s_flags &= ~NL_MSG_PEEK;
}
/*****************************************************************************/ /*****************************************************************************/
int int
@@ -1103,7 +1093,9 @@ nl_socket_new(struct nl_sock **out_sk,
}, },
.s_seq_expect = t, .s_seq_expect = t,
.s_seq_next = t, .s_seq_next = t,
.s_flags = NM_FLAGS_HAS(flags, NL_SOCKET_FLAGS_DISABLE_MSG_PEEK) ? 0 : NL_MSG_PEEK, .s_bufsize = 0,
.s_msg_peek = !NM_FLAGS_HAS(flags, NL_SOCKET_FLAGS_DISABLE_MSG_PEEK),
.s_auto_ack = TRUE,
}; };
nmerr = nl_socket_set_buffer_size(sk, bufsize_rx, bufsize_tx); nmerr = nl_socket_set_buffer_size(sk, bufsize_rx, bufsize_tx);
@@ -1229,7 +1221,7 @@ continue_reading:
nrecv++; nrecv++;
/* Only do sequence checking if auto-ack mode is enabled */ /* Only do sequence checking if auto-ack mode is enabled */
if (!(sk->s_flags & NL_NO_AUTO_ACK)) { if (sk->s_auto_ack) {
if (hdr->nlmsg_seq != sk->s_seq_expect) { if (hdr->nlmsg_seq != sk->s_seq_expect) {
nmerr = -NME_NL_SEQ_MISMATCH; nmerr = -NME_NL_SEQ_MISMATCH;
goto out; goto out;
@@ -1414,7 +1406,7 @@ nl_complete_msg(struct nl_sock *sk, struct nl_msg *msg)
nlh->nlmsg_flags |= NLM_F_REQUEST; nlh->nlmsg_flags |= NLM_F_REQUEST;
if (!(sk->s_flags & NL_NO_AUTO_ACK)) if (sk->s_auto_ack)
nlh->nlmsg_flags |= NLM_F_ACK; nlh->nlmsg_flags |= NLM_F_ACK;
} }
@@ -1510,8 +1502,7 @@ nl_recv(struct nl_sock *sk,
nm_assert(!out_creds_has || out_creds); nm_assert(!out_creds_has || out_creds);
nm_assert(!out_pktinfo_has || out_pktinfo_group); nm_assert(!out_pktinfo_has || out_pktinfo_group);
if ((sk->s_flags & NL_MSG_PEEK) if (sk->s_msg_peek)
|| (!(sk->s_flags & NL_MSG_PEEK_EXPLICIT) && sk->s_bufsize == 0))
flags |= MSG_PEEK | MSG_TRUNC; flags |= MSG_PEEK | MSG_TRUNC;
if (buf0_len > 0) { if (buf0_len > 0) {

View File

@@ -548,8 +548,6 @@ int nl_socket_set_pktinfo(struct nl_sock *sk, int state);
int nl_socket_set_nonblocking(const struct nl_sock *sk); int nl_socket_set_nonblocking(const struct nl_sock *sk);
void nl_socket_disable_msg_peek(struct nl_sock *sk);
uint32_t nl_socket_get_local_port(const struct nl_sock *sk); uint32_t nl_socket_get_local_port(const struct nl_sock *sk);
int nl_socket_add_memberships(struct nl_sock *sk, int group, ...); int nl_socket_add_memberships(struct nl_sock *sk, int group, ...);

View File

@@ -71,7 +71,6 @@ test_use_symbols(void)
(void (*)(void)) nl_socket_set_buffer_size, (void (*)(void)) nl_socket_set_buffer_size,
(void (*)(void)) nl_socket_add_memberships, (void (*)(void)) nl_socket_add_memberships,
(void (*)(void)) nl_socket_set_ext_ack, (void (*)(void)) nl_socket_set_ext_ack,
(void (*)(void)) nl_socket_disable_msg_peek,
(void (*)(void)) nl_wait_for_ack, (void (*)(void)) nl_wait_for_ack,
(void (*)(void)) nl_recvmsgs, (void (*)(void)) nl_recvmsgs,
(void (*)(void)) nl_sendmsg, (void (*)(void)) nl_sendmsg,