unix/dgram: inline sbappendaddr_locked() into uipc_sosend_dgram()

This allows to remove one M_NOWAIT allocation and also makes it
more clear what's going on.

Reviewed by:		markj
Differential revision:	https://reviews.freebsd.org/D35297
This commit is contained in:
Gleb Smirnoff 2022-06-24 09:09:10 -07:00
parent e3fbbf965e
commit 5dc8dd5f3a

View File

@ -1118,6 +1118,9 @@ uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
/*
* PF_UNIX/SOCK_DGRAM send
*
* Allocate a record consisting of 3 mbufs in the sequence of
* from -> control -> data and append it to the socket buffer.
*/
static int
uipc_sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
@ -1126,11 +1129,14 @@ uipc_sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
struct unpcb *unp, *unp2;
const struct sockaddr *from;
struct socket *so2;
int error;
struct sockbuf *sb;
struct mbuf *f, *clast;
int cc, error;
MPASS((uio != NULL && m == NULL) || (m != NULL && uio == NULL));
error = 0;
f = NULL;
if (__predict_false(flags & MSG_OOB)) {
error = EOPNOTSUPP;
@ -1146,6 +1152,7 @@ uipc_sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
error = EFAULT;
goto out;
}
f = m_get(M_WAITOK, MT_SONAME);
if (c != NULL && (error = unp_internalize(&c, td)))
goto out;
} else {
@ -1159,6 +1166,10 @@ uipc_sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
error = EMSGSIZE;
goto out;
}
if ((f = m_get(M_NOWAIT, MT_SONAME)) == NULL) {
error = ENOBUFS;
goto out;
}
/* Condition the foreign mbuf to our standards. */
m_clrprotoflags(m);
m_tag_delete_chain(m, NULL);
@ -1225,11 +1236,38 @@ uipc_sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
from = (struct sockaddr *)unp->unp_addr;
else
from = &sun_noname;
f->m_len = from->sa_len;
MPASS(from->sa_len <= MLEN);
bcopy(from, mtod(f, void *), from->sa_len);
cc = f->m_len + m->m_pkthdr.len;
if (c != NULL)
cc += m_length(c, &clast);
so2 = unp2->unp_socket;
SOCKBUF_LOCK(&so2->so_rcv);
if (sbappendaddr_locked(&so2->so_rcv, from, m, c)) {
sb = &so2->so_rcv;
SOCKBUF_LOCK(sb);
if (cc <= sbspace(sb)) {
/* Concatenate: from -> control -> data. */
if (c != NULL) {
f->m_next = c;
clast->m_next = m;
} else
f->m_next = m;
m = f;
/* Reusing f as iterator. */
for (f = m; f->m_next != NULL; f = f->m_next)
sballoc(sb, f);
sballoc(sb, f);
sb->sb_mbtail = f;
/* SBLINKRECORD */
if (sb->sb_lastrecord != NULL)
sb->sb_lastrecord->m_nextpkt = m;
else
sb->sb_mb = m;
sb->sb_lastrecord = m;
SBLASTMBUFCHK(sb);
SBLASTRECORDCHK(sb);
sorwakeup_locked(so2);
m = c = NULL;
f = m = c = NULL;
} else {
soroverflow_locked(so2);
error = (so->so_state & SS_NBIO) ? EAGAIN : ENOBUFS;
@ -1248,6 +1286,8 @@ uipc_sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
if (c)
unp_scan(c, unp_freerights);
out:
if (f)
m_free(f);
if (c)
m_freem(c);
if (m)