Playing with mbuf in userland

Bosko Milekic bmilekic at FreeBSD.org
Sun Aug 22 07:03:18 PDT 2004


Paolo Pisati wrote:
>Hi,
>
>i'm developing a little app that manipulates mbuf.
>Right now i'm still working on it as userland app but i
>would like to test it with some real mbufs straight
>from the stack.
>Do you know how i can get some of these structs in
>an easy way?
>I mean, is it possible to copy some of these struct from
>stack to userland?
>Or should i fake it in userland?

  One way to do this would be to instrument a for-superuser-only
  socket option that would copy out all of the data, including the
  metadata and mbuf headers, out to userland, while taking care to
  modify references within the mbufs to userland locations {*}.  To do
  this, in turn, you would need to obtain the userland target addresses
  of all mbufs and clusters you're copying out beforehand, and overwrite
  all mbufs' m_next, m_nextpkt, m_data, and in some cases, m_ext.ext_buf
  references before doing the copyout in-kernel.  This can be a pretty
  involved copy and would require careful implementation.

{*} The data is the socket buffer is kept as an mbuf chain so this is
    possible.

  Another option to look into would be to implement a sysctl(8)-exported
  handler that iterates over the mbuf chain and prints out the mbuf chains
  in something like XML, which your userland application can then more or less
  easily parse, and reproduce the chain ("fake it up") in userland.  This
  solution is rather attractive because you can do all sorts of things
  with the intermediate-parsing-language right from the kernel, as well
  as from userland (at the parsing stages).  To see an example of a sysctl(8)
  handler, refer to src/sys/vm/uma_core.c (the bottom) in FreeBSD 5.x.

  In any case, I would be very interested in seeing what you come up with,
  as this could be a very useful diagnostic tool.

-Bosko
--
Bosko Milekic <bmilekic at technokratis.com> <bmilekic at FreeBSD.org>
"For the wicked / Carry us away / Captivity require from us a song /
How can we sing king alpha's song in a strange land?" --Bob Marley



More information about the freebsd-hackers mailing list