netlink: avoid putting empty mbufs on the socket queue

When processing incoming Netlink messages in nl_process_nbuf() kernel
always allocates a writer with a buffer to put generated reply to.
However, certain messages aren't replied.  That makes nlmsg_flush()
to put an empty buffer to the socket.  Avoid doing that because avoiding
is much easier than dealing with empty buffers on the receiver side.
This commit is contained in:
Gleb Smirnoff 2024-01-10 20:51:53 -08:00
parent e6f4c31460
commit f75d7fac10
3 changed files with 12 additions and 8 deletions

View File

@ -740,12 +740,7 @@ nl_soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio,
TAILQ_FOREACH(nb, &sb->nl_queue, tailq) {
u_int offset;
/*
* XXXGL: zero length buffer may be at the tail of a queue
* when a writer overflows socket buffer. When this is
* improved, use MPASS(nb->offset < nb->datalen).
*/
MPASS(nb->offset <= nb->datalen);
MPASS(nb->offset < nb->datalen);
offset = nb->offset;
while (offset < nb->datalen) {
hdr = (struct nlmsghdr *)&nb->data[offset];

View File

@ -201,6 +201,8 @@ nl_send(struct nl_writer *nw, struct nlpcb *nlp)
struct nl_buf *nb;
MPASS(nw->hdr == NULL);
MPASS(nw->buf != NULL);
MPASS(nw->buf->datalen > 0);
IF_DEBUG_LEVEL(LOG_DEBUG2) {
struct nlmsghdr *hdr = (struct nlmsghdr *)nw->buf->data;

View File

@ -100,6 +100,7 @@ _nlmsg_ignore_limit(struct nl_writer *nw)
bool
_nlmsg_flush(struct nl_writer *nw)
{
bool result;
if (__predict_false(nw->hdr != NULL)) {
/* Last message has not been completed, skip it. */
@ -109,8 +110,14 @@ _nlmsg_flush(struct nl_writer *nw)
nw->hdr = NULL;
}
NL_LOG(LOG_DEBUG2, "OUT");
bool result = nw->cb(nw);
if (nw->buf->datalen == 0) {
MPASS(nw->num_messages == 0);
nl_buf_free(nw->buf);
nw->buf = NULL;
return (true);
}
result = nw->cb(nw);
nw->num_messages = 0;
if (!result) {