From: Alexander Bluhm Subject: Re: ddb mbuf chains To: tech@openbsd.org Date: Thu, 29 Aug 2024 21:15:54 +0200 On Wed, Aug 28, 2024 at 10:43:16PM +0200, Alexander Bluhm wrote: > I want to print the layout of mbuf chains in ddb. For debugging > hardware offloading, DMA requirements and bounce buffers, I need > to know the layout of the mbuf chain by m_next and m_nextpkt pointer. > So I implemented /c and /p modifiers for ddb show mbuf. > > ddb> show mbuf /pc 0xfffffd8006fd5100 Additionally we can print mbuf type and offset. Then a UDP receive socket buffer looks like this: ++- mbuf 0xfffffd806dcddd00, nam, off 0, len 16 |+- mbuf 0xfffffd806dcddf00, dat, off 52, len 1472, pktlen 2049, clsize 2048 |+- mbuf 0xfffffd806dcdd400, dat, off 44, len 577, clsize 2048 |\- total chain 3, len 2065, size 4320 ++- mbuf 0xfffffd806dcdde00, nam, off 0, len 16 |+- mbuf 0xfffffd806dcdd800, dat, off 52, len 4, pktlen 4, clsize 2048 |\- total chain 2, len 20, size 2272 \-- total packets 2 ok? bluhm Index: sys/ddb/db_command.c =================================================================== RCS file: /data/mirror/openbsd/cvs/src/sys/ddb/db_command.c,v diff -u -p -r1.101 db_command.c --- sys/ddb/db_command.c 13 May 2024 01:15:50 -0000 1.101 +++ sys/ddb/db_command.c 29 Aug 2024 17:57:18 -0000 @@ -340,7 +340,15 @@ db_malloc_print_cmd(db_expr_t addr, int void db_mbuf_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif) { - m_print((void *)addr, db_printf); + if ((modif[0] == 'c' && modif[1] == 'p') || + (modif[0] == 'p' && modif[1] == 'c')) + m_print_packet((void *)addr, 1, db_printf); + else if (modif[0] == 'c') + m_print_chain((void *)addr, 0, db_printf); + else if (modif[0] == 'p') + m_print_packet((void *)addr, 0, db_printf); + else + m_print((void *)addr, db_printf); } void Index: sys/ddb/db_interface.h =================================================================== RCS file: /data/mirror/openbsd/cvs/src/sys/ddb/db_interface.h,v diff -u -p -r1.27 db_interface.h --- sys/ddb/db_interface.h 3 Feb 2024 18:51:58 -0000 1.27 +++ sys/ddb/db_interface.h 29 Aug 2024 17:57:18 -0000 @@ -61,6 +61,8 @@ void db_show_all_pools(db_expr_t, int, d /* kern/uipc_mbuf.c */ void m_print(void *, int (*)(const char *, ...)); +void m_print_chain(void *, int, int (*)(const char *, ...)); +void m_print_packet(void *, int, int (*)(const char *, ...)); /* kern/uipc_socket.c */ void so_print(void *, int (*)(const char *, ...)); Index: sys/kern/uipc_mbuf.c =================================================================== RCS file: /data/mirror/openbsd/cvs/src/sys/kern/uipc_mbuf.c,v diff -u -p -r1.291 uipc_mbuf.c --- sys/kern/uipc_mbuf.c 29 Aug 2024 10:44:40 -0000 1.291 +++ sys/kern/uipc_mbuf.c 29 Aug 2024 18:48:13 -0000 @@ -1533,6 +1533,79 @@ m_print(void *v, } } + +const char *m_types[MT_NTYPES] = { + "fre", + "dat", + "hdr", + "nam", + "opt", + "ftb", + "ctl", + "oob", +}; + +void +m_print_chain(void *v, int deep, + int (*pr)(const char *, ...) __attribute__((__format__(__kprintf__,1,2)))) +{ + struct mbuf *m; + const char *indent = deep ? "++-" : "-+-"; + size_t chain = 0, len = 0, size = 0; + + for (m = v; m != NULL; m = m->m_next) { + const char *type; + + chain++; + len += m->m_len; + size += M_SIZE(m); + type = m->m_type < MT_NTYPES ? m_types[m->m_type] : "???"; + (*pr)("%s mbuf %p, %s, off %zd, len %u", indent, m, type, + m->m_data - M_DATABUF(m), m->m_len); + if (m->m_flags & M_PKTHDR) + (*pr)(", pktlen %d", m->m_pkthdr.len); + if (m->m_flags & M_EXT) + (*pr)(", clsize %u", m->m_ext.ext_size); + (*pr)("\n"); + indent = deep ? "|+-" : " +-"; + } + indent = deep ? "|\\-" : " \\-"; + if (v != NULL) { + (*pr)("%s total chain %zu, len %zu, size %zu\n", + indent, chain, len, size); + } +} + +void +m_print_packet(void *v, int deep, + int (*pr)(const char *, ...) __attribute__((__format__(__kprintf__,1,2)))) +{ + struct mbuf *m, *n; + const char *indent = "+--"; + size_t pkts = 0; + + for (m = v; m != NULL; m = m->m_nextpkt) { + size_t chain = 0, len = 0, size = 0; + + pkts++; + if (deep) { + m_print_chain(m, deep, pr); + continue; + } + for (n = m; n != NULL; n = n->m_next) { + chain++; + len += n->m_len; + size += M_SIZE(n); + } + (*pr)("%s mbuf %p, chain %zu", indent, m, chain); + if (m->m_flags & M_PKTHDR) + (*pr)(", pktlen %d", m->m_pkthdr.len); + (*pr)(", len %zu, size %zu\n", len, size); + } + indent = "\\--"; + if (v != NULL) + (*pr)("%s total packets %zu\n", indent, pkts); +} #endif /* Index: share/man/man4/ddb.4 =================================================================== RCS file: /data/mirror/openbsd/cvs/src/share/man/man4/ddb.4,v diff -u -p -r1.107 ddb.4 --- share/man/man4/ddb.4 5 Feb 2024 21:33:00 -0000 1.107 +++ share/man/man4/ddb.4 28 Aug 2024 19:51:26 -0000 @@ -658,7 +658,11 @@ If the .Cm /f modifier is specified, the complete map is printed. .\" -------------------- -.It Ic show mbuf Ar addr +.It Xo +.Ic show mbuf +.Op Cm /cp +.Ar addr +.Xc Prints the .Vt struct mbuf header at @@ -668,6 +672,13 @@ Depending on the mbuf flags and .Vt struct m_ext are printed as well. +If the +.Cm /c +modifier is specified, print the mbuf chain linked with the m_next +pointer. +.Cm /p +does the same using m_nextpkt. +Both can be combined. .\" -------------------- .It Xo .Ic show mount