Module Name: src Committed By: riastradh Date: Wed Apr 12 06:48:08 UTC 2023
Modified Files: src/sys/kern: uipc_mbuf.c src/sys/sys: mbuf.h Log Message: mbuf(9): New m_get_n, m_gethdr_n. m_get_n(how, type, alignbytes, nbytes) returns an mbuf with no packet header having space for nbytes, with an internal buffer pointer aligned by alignbytes (typically ETHER_ALIGN or similar, if not zero). m_gethdr_n(how, type, alignbytes, nbytes) does the same but for an mbuf with a packet header. These return NULL on failure, which can happen either: (a) because how is M_DONTWAIT and allocating memory would sleep, or (b) because alignbytes + nbytes > MCLBYTES. On exit, m_len is set to nbytes, as is m_pkthdr.len for m_gethdr_n. These should be used to systematically replace all calls to m_get, m_gethdr, MGET, MGETHDR, and m_getcl. Most calls to m_clget and MCLGET will probably evaporate as a consequence. Proposed on tech-net last year: https://mail-index.netbsd.org/tech-net/2022/07/16/msg008285.html To generate a diff of this commit: cvs rdiff -u -r1.250 -r1.251 src/sys/kern/uipc_mbuf.c cvs rdiff -u -r1.237 -r1.238 src/sys/sys/mbuf.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/kern/uipc_mbuf.c diff -u src/sys/kern/uipc_mbuf.c:1.250 src/sys/kern/uipc_mbuf.c:1.251 --- src/sys/kern/uipc_mbuf.c:1.250 Sat Apr 1 06:30:19 2023 +++ src/sys/kern/uipc_mbuf.c Wed Apr 12 06:48:08 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: uipc_mbuf.c,v 1.250 2023/04/01 06:30:19 skrll Exp $ */ +/* $NetBSD: uipc_mbuf.c,v 1.251 2023/04/12 06:48:08 riastradh Exp $ */ /* * Copyright (c) 1999, 2001, 2018 The NetBSD Foundation, Inc. @@ -62,7 +62,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: uipc_mbuf.c,v 1.250 2023/04/01 06:30:19 skrll Exp $"); +__KERNEL_RCSID(0, "$NetBSD: uipc_mbuf.c,v 1.251 2023/04/12 06:48:08 riastradh Exp $"); #ifdef _KERNEL_OPT #include "opt_mbuftrace.h" @@ -587,6 +587,50 @@ m_gethdr(int how, int type) return m; } +struct mbuf * +m_get_n(int how, int type, size_t alignbytes, size_t nbytes) +{ + struct mbuf *m; + + if (alignbytes > MCLBYTES || nbytes > MCLBYTES - alignbytes) + return NULL; + if ((m = m_get(how, type)) == NULL) + return NULL; + if (nbytes + alignbytes > MLEN) { + m_clget(m, how); + if ((m->m_flags & M_EXT) == 0) { + m_free(m); + return NULL; + } + } + m->m_len = alignbytes + nbytes; + m_adj(m, alignbytes); + + return m; +} + +struct mbuf * +m_gethdr_n(int how, int type, size_t alignbytes, size_t nbytes) +{ + struct mbuf *m; + + if (nbytes > MCLBYTES || nbytes > MCLBYTES - alignbytes) + return NULL; + if ((m = m_gethdr(how, type)) == NULL) + return NULL; + if (alignbytes + nbytes > MHLEN) { + m_clget(m, how); + if ((m->m_flags & M_EXT) == 0) { + m_free(m); + return NULL; + } + } + m->m_len = m->m_pkthdr.len = alignbytes + nbytes; + m_adj(m, alignbytes); + + return m; +} + void m_clget(struct mbuf *m, int how) { Index: src/sys/sys/mbuf.h diff -u src/sys/sys/mbuf.h:1.237 src/sys/sys/mbuf.h:1.238 --- src/sys/sys/mbuf.h:1.237 Fri Dec 16 08:42:55 2022 +++ src/sys/sys/mbuf.h Wed Apr 12 06:48:08 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: mbuf.h,v 1.237 2022/12/16 08:42:55 msaitoh Exp $ */ +/* $NetBSD: mbuf.h,v 1.238 2023/04/12 06:48:08 riastradh Exp $ */ /* * Copyright (c) 1996, 1997, 1999, 2001, 2007 The NetBSD Foundation, Inc. @@ -740,6 +740,8 @@ struct mbuf *m_devget(char *, int, int, struct mbuf *m_dup(struct mbuf *, int, int, int); struct mbuf *m_get(int, int); struct mbuf *m_gethdr(int, int); +struct mbuf *m_get_n(int, int, size_t, size_t); +struct mbuf *m_gethdr_n(int, int, size_t, size_t); struct mbuf *m_prepend(struct mbuf *,int, int); struct mbuf *m_pulldown(struct mbuf *, int, int, int *); struct mbuf *m_pullup(struct mbuf *, int);