NG_SOURCE(4)
π NG_SOURCE(4) π
okay bestie, welcome to the Kernel Interfaces Manual πΈ β today's main character is ng_source, and she is literally the netgraph node for traffic generation. packet factory energy. no cap. π
β¨ NAME β¨
ng_source β netgraph node for traffic generation
β¨ SYNOPSIS β¨
#include <sys/types.h>
#include <netgraph/ng_source.h>
β¨ DESCRIPTION β¨
so the source node is out here acting as a source of packets π
β it does that based on whatever parameters you set up with control messages, plus whatever packets you feed it on the input. the ng_source node type is used primarily for testing and benchmarking. it's the friend you call when you need traffic and you need it now. π¦
π HOOKS π
the source node has two hooks, and they are:
inputoutput
the output hook must remain connected. if it gets disconnected, that's it, the node shuts down. it's giving "you can't sit with us" energy. π
π OPERATION π
okay so here's how the whole thing goes down. packets received on the input hook get queued internally. π«Ά
when the output hook is connected, the ng_source node assumes its neighbour node is of ng_ether(4) node type. she queries that neighbour for its interface name. then the ng_source node uses the queue of that interface for its evil purposes. π she also disables the autosrc option on the neighbour ng_ether(4) node.
but if the interface name can't be obtained automatically, you have to configure it explicitly with the NGM_SOURCE_SETIFACE control message β and you also need to turn off autosrc on the ng_ether(4) node manually. do it yourself, bestie. π
once the interface is configured, upon receipt of an NGM_SOURCE_START control message the node starts sending the previously queued packets out the output hook on every clock tick, as fast as the connected interface will take them. π₯
while she's active, on every clock tick the node checks the available space in the interface queue and sends that many packets out its output hook. once the number of packets indicated in the start message has been sent β or upon receipt of an NGM_SOURCE_STOP message β the node stops sending data. periodt. π
β CONTROL MESSAGES β
this node type supports the generic control messages, plus all the following ones β which must be sent with the NGM_SOURCE_COOKIE attached. don't forget the cookie, it's the whole vibe. π
NGM_SOURCE_GET_STATS(getstats) β returns a structure containing the following fields:outOctetsβ the number of octets/bytes sent out theoutputhook.outFramesβ the number of frames/packets sent out theoutputhook.queueOctetsβ the number of octets queued from theinputhook.queueFramesβ the number of frames queued from theinputhook.startTimeβ the time the last start message was received.endTimeβ the time the last end message was received or the output packet count was reached.elapsedTimeβ eitherendTime - startTimeor current time -startTime.
NGM_SOURCE_CLR_STATS(clrstats) β clears and resets the statistics returned bygetstats(exceptqueueOctetsandqueueFrames). those two get to stay. β¨NGM_SOURCE_GETCLR_STATS(getclrstats) β asgetstatsbut clears the statistics at the same time. two birds, one iconic message. ποΈNGM_SOURCE_START(start) β this message requires a singleuint64_tparameter, which is the number of packets to send before stopping. node starts sending the queued packets out theoutputhook. theoutputhook must be connected and the node must have an interface configured. no shortcuts. πNGM_SOURCE_STOP(stop) β stops the node if it is active.NGM_SOURCE_CLR_DATA(clrdata) β clears the packets queued from theinputhook.NGM_SOURCE_SETIFACE(setiface) β this message requires the name of the interface to be configured as an argument.NGM_SOURCE_SETPPS(setpps) β this message requires a singleuint32_tparameter, which puts an upper limit on the amount of packets sent per second. pacing, but make it cute. π§NGM_SOURCE_SET_TIMESTAMP(settimestamp) β this message specifies that a timestamp (in the format of astruct timeval) should be inserted in the transmitted packets. this message requires a structure containing the following fields:offsetβ the offset from the beginning of the packet at which the timestamp is to be inserted.flagsβ set to1to enable the timestamp.
NGM_SOURCE_GET_TIMESTAMP(gettimestamp) β returns the current timestamp settings in the form of the structure described above.NGM_SOURCE_SET_COUNTER(setcounter) β this message specifies that a counter should be embedded in transmitted packets. up to four counters may be independently configured. this message requires a structure containing the following fields:offsetβ the offset from the beginning of the packet at which the counter is to be inserted.flagsβ set to1to enable the counter.widthβ the byte width of the counter. it may be1,2, or4.next_valβ the value for the next insertion of the counter.min_valβ the minimum value to be used by the counter.max_valβ the maximum value to be used by the counter.incrementβ the value to be added to the counter after each insertion. it may be negative. πindexβ the counter to be configured, from0to3.
NGM_SOURCE_GET_COUNTER(getcounter) β this message requires a singleuint8_tparameter, which specifies the counter to query. returns the current counter settings in the form of the structure described above.
π SHUTDOWN π
this node shuts down upon receipt of an NGM_SHUTDOWN control message, when all hooks have been disconnected, or when the output hook has been disconnected. three ways to say goodbye. π
πΈ EXAMPLES πΈ
first up: attach the node to an ng_ether(4) node for an interface. if ng_ether isn't already loaded you will need to do so. for example, these commands load the ng_ether module and attach the output hook of a new source node to the orphans hook of the bge0: ng_ether node:
kldload ng_ether
ngctl mkpeer bge0: source orphans output
at this point the new node can be referred to as "bge0:orphans". she can be given her own name like this:
ngctl name bge0:orphans src0
after which it can be referred to as "src0:". glow-up complete. β¨
once created, packets can be sent to the node as raw binary data. each packet must be delivered in a separate netgraph message. one packet per message, no bundling, we don't do that here. π
the following example uses a short Perl script to convert the hex representation of an ICMP packet to binary and deliver it to the source node's input hook via nghook(8):
perl -pe 's/(..)[ \t\n]*/chr(hex($1))/ge' <<EOF | nghook src0: input
ff ff ff ff ff ff 00 00 00 00 00 00 08 00 45 00
00 54 cb 13 00 00 40 01 b9 87 c0 a8 2b 65 0a 00
00 01 08 00 f8 d0 c9 76 00 00 45 37 01 73 00 01
04 0a 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15
16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25
26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35
36 37
EOF
to check that the node has queued these packets you can get the node statistics:
ngctl msg bge0:orphans getstats
Args: { queueOctets=64 queueFrames=1 }
send as many packets as required out the output hook:
ngctl msg bge0:orphans start 16
either wait for them to be sent (periodically fetching stats if desired, live your truth) or send the stop message:
ngctl msg bge0:orphans stop
check the statistics β here we use getclrstats to also clear the statistics:
ngctl msg bge0:orphans getclrstats
Args: { outOctets=1024 outFrames=16 queueOctets=64 queueFrames=1
startTime={ tv_sec=1035305880 tv_usec=758036 } endTime={ tv_sec=1035305880
tv_usec=759041 } elapsedTime={ tv_usec=1005 } }
the times are from struct timevals, the tv_sec field is seconds since the Epoch and can be converted into a date string via TCL's [clock format] or via the date(1) command:
date -r 1035305880
Tue Oct 22 12:58:00 EDT 2002
π¦ SEE ALSO π¦
netgraph(4), ng_echo(4), ng_hole(4), ng_tee(4), ngctl(8), nghook(8)
π HISTORY π
the ng_source node type was implemented in FreeBSD 4.8. vintage, but make it serve. π
π AUTHORS π
Dave Chapeskie
and that's the whole page, bestie π«Ά go forth and generate some traffic, responsibly. Debian, March 1, 2007 β mwah π slay π π
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright 2002-2007 Sandvine Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms