Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys mbuf(9): New m_get_n, m_gethdr_n.



details:   https://anonhg.NetBSD.org/src/rev/14320a4e1675
branches:  trunk
changeset: 374240:14320a4e1675
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Wed Apr 12 06:48:08 2023 +0000

description:
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

diffstat:

 sys/kern/uipc_mbuf.c |  48 ++++++++++++++++++++++++++++++++++++++++++++++--
 sys/sys/mbuf.h       |   4 +++-
 2 files changed, 49 insertions(+), 3 deletions(-)

diffs (87 lines):

diff -r b8f986321675 -r 14320a4e1675 sys/kern/uipc_mbuf.c
--- a/sys/kern/uipc_mbuf.c      Wed Apr 12 06:39:15 2023 +0000
+++ b/sys/kern/uipc_mbuf.c      Wed Apr 12 06:48:08 2023 +0000
@@ -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)
 {
diff -r b8f986321675 -r 14320a4e1675 sys/sys/mbuf.h
--- a/sys/sys/mbuf.h    Wed Apr 12 06:39:15 2023 +0000
+++ b/sys/sys/mbuf.h    Wed Apr 12 06:48:08 2023 +0000
@@ -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);



Home | Main Index | Thread Index | Old Index