Deleted Added
full compact
netgraph.4 (71263) netgraph.4 (71849)
1.\" Copyright (c) 1996-1999 Whistle Communications, Inc.
2.\" All rights reserved.
3.\"
4.\" Subject to the following obligations and disclaimer of warranty, use and
5.\" redistribution of this software, in source or object code forms, with or
6.\" without modifications are expressly permitted by Whistle Communications;
7.\" provided, however, that:
8.\" 1. Any and all reproductions of the source or object code must include the
9.\" copyright notice above and the following disclaimer of warranties; and
10.\" 2. No rights are granted, in any manner or form, to use Whistle
11.\" Communications, Inc. trademarks, including the mark "WHISTLE
12.\" COMMUNICATIONS" on advertising, endorsements, or otherwise except as
13.\" such appears in the above copyright notice or in the software.
14.\"
15.\" THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
16.\" TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
17.\" REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
18.\" INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
19.\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
20.\" WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
21.\" REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
22.\" SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
23.\" IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
24.\" RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
25.\" WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26.\" PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
27.\" SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
28.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30.\" THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
31.\" OF SUCH DAMAGE.
32.\"
33.\" Authors: Julian Elischer <julian@FreeBSD.org>
34.\" Archie Cobbs <archie@FreeBSD.org>
35.\"
1.\" Copyright (c) 1996-1999 Whistle Communications, Inc.
2.\" All rights reserved.
3.\"
4.\" Subject to the following obligations and disclaimer of warranty, use and
5.\" redistribution of this software, in source or object code forms, with or
6.\" without modifications are expressly permitted by Whistle Communications;
7.\" provided, however, that:
8.\" 1. Any and all reproductions of the source or object code must include the
9.\" copyright notice above and the following disclaimer of warranties; and
10.\" 2. No rights are granted, in any manner or form, to use Whistle
11.\" Communications, Inc. trademarks, including the mark "WHISTLE
12.\" COMMUNICATIONS" on advertising, endorsements, or otherwise except as
13.\" such appears in the above copyright notice or in the software.
14.\"
15.\" THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
16.\" TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
17.\" REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
18.\" INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
19.\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
20.\" WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
21.\" REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
22.\" SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
23.\" IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
24.\" RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
25.\" WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26.\" PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
27.\" SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
28.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30.\" THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
31.\" OF SUCH DAMAGE.
32.\"
33.\" Authors: Julian Elischer <julian@FreeBSD.org>
34.\" Archie Cobbs <archie@FreeBSD.org>
35.\"
36.\" $FreeBSD: head/share/man/man4/netgraph.4 71263 2001-01-19 14:15:40Z ru $
36.\" $FreeBSD: head/share/man/man4/netgraph.4 71849 2001-01-30 20:51:52Z julian $
37.\" $Whistle: netgraph.4,v 1.7 1999/01/28 23:54:52 julian Exp $
38.\"
39.Dd January 19, 1999
40.Dt NETGRAPH 4
41.Os FreeBSD
42.Sh NAME
43.Nm netgraph
44.Nd graph based kernel networking subsystem
45.Sh DESCRIPTION
46The
47.Nm
48system provides a uniform and modular system for the implementation
49of kernel objects which perform various networking functions. The objects,
50known as
51.Em nodes ,
52can be arranged into arbitrarily complicated graphs. Nodes have
53.Em hooks
54which are used to connect two nodes together, forming the edges in the graph.
55Nodes communicate along the edges to process data, implement protocols, etc.
56.Pp
57The aim of
58.Nm
59is to supplement rather than replace the existing kernel networking
60infrastructure. It provides:
61.Pp
62.Bl -bullet -compact -offset 2n
63.It
64A flexible way of combining protocol and link level drivers
65.It
66A modular way to implement new protocols
67.It
68A common framework for kernel entities to inter-communicate
69.It
70A reasonably fast, kernel-based implementation
71.El
72.Sh Nodes and Types
73The most fundamental concept in
74.Nm
75is that of a
76.Em node .
77All nodes implement a number of predefined methods which allow them
78to interact with other nodes in a well defined manner.
79.Pp
80Each node has a
81.Em type ,
82which is a static property of the node determined at node creation time.
83A node's type is described by a unique
84.Tn ASCII
85type name.
86The type implies what the node does and how it may be connected
87to other nodes.
88.Pp
89In object-oriented language, types are classes and nodes are instances
90of their respective class. All node types are subclasses of the generic node
91type, and hence inherit certain common functionality and capabilities
92(e.g., the ability to have an
93.Tn ASCII
94name).
95.Pp
96Nodes may be assigned a globally unique
97.Tn ASCII
98name which can be
99used to refer to the node.
100The name must not contain the characters
101.Dq .\&
102or
103.Dq \&:
104and is limited to
105.Dv "NG_NODELEN + 1"
106characters (including NUL byte).
107.Pp
108Each node instance has a unique
109.Em ID number
110which is expressed as a 32-bit hex value. This value may be used to
111refer to a node when there is no
112.Tn ASCII
113name assigned to it.
114.Sh Hooks
115Nodes are connected to other nodes by connecting a pair of
116.Em hooks ,
117one from each node. Data flows bidirectionally between nodes along
118connected pairs of hooks. A node may have as many hooks as it
119needs, and may assign whatever meaning it wants to a hook.
120.Pp
121Hooks have these properties:
122.Pp
123.Bl -bullet -compact -offset 2n
124.It
125A hook has an
126.Tn ASCII
127name which is unique among all hooks
128on that node (other hooks on other nodes may have the same name).
129The name must not contain a
130.Dq .\&
131or a
132.Dq \&:
133and is
134limited to
135.Dv "NG_HOOKLEN + 1"
136characters (including NUL byte).
137.It
138A hook is always connected to another hook. That is, hooks are
139created at the time they are connected, and breaking an edge by
140removing either hook destroys both hooks.
141.It
142A hook can be set into a state where incoming packets are always queued
143by the input queuing system, rather than being delivered directly. This
144is used when the two joined nodes need to be decoupled, e.g. if they are
145running at different processor priority levels. (spl)
146.El
147.Pp
148A node may decide to assign special meaning to some hooks.
149For example, connecting to the hook named
150.Dq debug
151might trigger
152the node to start sending debugging information to that hook.
153.Sh Data Flow
154Two types of information flow between nodes: data messages and
155control messages. Data messages are passed in mbuf chains along the edges
156in the graph, one edge at a time. The first mbuf in a chain must have the
157.Dv M_PKTHDR
158flag set. Each node decides how to handle data coming in on its hooks.
159.Pp
160Control messages are type-specific C structures sent from one node
161directly to some arbitrary other node. Control messages have a common
162header format, followed by type-specific data, and are binary structures
163for efficiency. However, node types also may support conversion of the
164type specific data between binary and
165.Tn ASCII
166for debugging and human interface purposes (see the
167.Dv NGM_ASCII2BINARY
168and
169.Dv NGM_BINARY2ASCII
170generic control messages below). Nodes are not required to support
171these conversions.
172.Pp
173There are three ways to address a control message. If
174there is a sequence of edges connecting the two nodes, the message
175may be
176.Dq source routed
177by specifying the corresponding sequence
178of
179.Tn ASCII
180hook names as the destination address for the message (relative
181addressing). If the destination is adjacent to the source, then the source
182node may simply specify (as a pointer in the code) the hook across which the
183message should be sent. Otherwise, the recipient node global
184.Tn ASCII
185name
186(or equivalent ID based name) is used as the destination address
187for the message (absolute addressing). The two types of
188.Tn ASCII
189addressing
190may be combined, by specifying an absolute start node and a sequence
191of hooks. Only the
192.Tn ASCII
193addressing modes are available to control programs outside the kernel,
194as use of direct pointers is limited of course to kernel modules.
195.Pp
196Messages often represent commands that are followed by a reply message
197in the reverse direction. To facilitate this, the recipient of a
198control message is supplied with a
199.Dq return address
200that is suitable for addressing a reply.
201.Pp
202Each control message contains a 32 bit value called a
203.Em typecookie
204indicating the type of the message, i.e., how to interpret it.
205Typically each type defines a unique typecookie for the messages
206that it understands. However, a node may choose to recognize and
207implement more than one type of message.
208.Pp
209If a message is delivered to an address that implies that it arrived
210at that node through a particular hook, (as opposed to having been directly
211addressed using its ID or global name), then that hook is identified to the
212receiving node. This allows a message to be rerouted or passed on, should
213a node decide that this is required, in much the same way that data packets
214are passed around between nodes. A set of standard
215messages for flow control and link management purposes are
216defined by the base system that are usually
217passed around in this manner. Flow control message would usually travel
218in the opposite direction to the data to which they pertain.
219.Sh Netgraph is (usually) Functional
220In order to minimize latency, most
221.Nm
222operations are functional.
223That is, data and control messages are delivered by making function
224calls rather than by using queues and mailboxes. For example, if node
225A wishes to send a data mbuf to neighboring node B, it calls the
226generic
227.Nm
228data delivery function. This function in turn locates
229node B and calls B's
230.Dq receive data
231method. There are exceptions to this.
232.Pp
233Each node has an input queue, and some operations can be considered to
234be 'writers' in that they alter the state of the node. Obviously in an SMP
235world it would be bad if the state of a node were changed while another
236data packet were transiting the node. For this purpose, the input queue
237implements a
238.Em reader/writer
239semantic so that when there is a writer in the node, all other requests
240are queued, and while there are readers, a writer, and any following
241packets are queued. In the case where there is no reason to queue the
242data, the input method is called directly, as mentionned above.
243.Pp
244A node may declare that all requests should be considered as writers,
245or that requests coming in over a particular hook should be considered to
246be a writer, or even that packets leaving or entering across a particular
247hook should always be queued, rather than delivered directly (often useful
248for interrupt routines who want to get back to the hardware quickly).
249By default, all controll message packets are considered to be writers
250unless specifically declared to be a reader in their definition. (see
251NGM_READONLY in ng_message.h)
252.Pp
253While this mode of operation
254results in good performance, it has a few implications for node
255developers:
256.Pp
257.Bl -bullet -compact -offset 2n
258.It
259Whenever a node delivers a data or control message, the node
260may need to allow for the possibility of receiving a returning
261message before the original delivery function call returns.
262.It
263Netgraph nodes and support routines generally run at
264.Fn splnet .
265However, some nodes may want to send data and control messages
266from a different priority level. Netgraph supplies a mechanism which
267utilizes the NETISR system to move message and data delivery to
268.Fn splnet .
269Nodes that run at other priorities (e.g. interfaces) can be directly
270linked to other nodes so that the combination runs at the other priority,
271however any interaction with nodes running at splnet MUST be achieved via the
272queueing functions, (which use the
273.Fn netisr
274feature of the kernel).
275Note that messages are always received at
276.Fn splnet .
277.It
278It's possible for an infinite loop to occur if the graph contains cycles.
279.El
280.Pp
281So far, these issues have not proven problematical in practice.
282.Sh Interaction With Other Parts of the Kernel
283A node may have a hidden interaction with other components of the
284kernel outside of the
285.Nm
286subsystem, such as device hardware,
287kernel protocol stacks, etc. In fact, one of the benefits of
288.Nm
289is the ability to join disparate kernel networking entities together in a
290consistent communication framework.
291.Pp
292An example is the node type
293.Em socket
294which is both a netgraph node and a
295.Xr socket 2
296BSD socket in the protocol family
297.Dv PF_NETGRAPH .
298Socket nodes allow user processes to participate in
299.Nm .
300Other nodes communicate with socket nodes using the usual methods, and the
301node hides the fact that it is also passing information to and from a
302cooperating user process.
303.Pp
304Another example is a device driver that presents
305a node interface to the hardware.
306.Sh Node Methods
307Nodes are notified of the following actions via function calls
308to the following node methods (all at
309.Fn splnet )
310and may accept or reject that action (by returning the appropriate
311error code):
312.Bl -tag -width xxx
313.It Creation of a new node
314The constructor for the type is called. If creation of a new node is
315allowed, the constructor must call the generic node creation
316function (in object-oriented terms, the superclass constructor)
317and then allocate any special resources it needs. For nodes that
318correspond to hardware, this is typically done during the device
319attach routine. Often a global
320.Tn ASCII
321name corresponding to the
322device name is assigned here as well.
323.It Creation of a new hook
324The hook is created and tentatively
325linked to the node, and the node is told about the name that will be
326used to describe this hook. The node sets up any special data structures
327it needs, or may reject the connection, based on the name of the hook.
328.It Successful connection of two hooks
329After both ends have accepted their
330hooks, and the links have been made, the nodes get a chance to
331find out who their peer is across the link and can then decide to reject
332the connection. Tear-down is automatic. This is also the time at which
333a node may decide whether to set a particular hook (or its peer) into
334.Em queuing
335mode.
336.It Destruction of a hook
337The node is notified of a broken connection. The node may consider some hooks
338to be critical to operation and others to be expendable: the disconnection
339of one hook may be an acceptable event while for another it
340may effect a total shutdown for the node.
341.It Shutdown of a node
342This method allows a node to clean up
343and to ensure that any actions that need to be performed
344at this time are taken. The method must call the generic (i.e., superclass)
345node destructor to get rid of the generic components of the node.
346Some nodes (usually associated with a piece of hardware) may be
347.Em persistent
348in that a shutdown breaks all edges and resets the node,
349but doesn't remove it, in which case the generic destructor is not called.
350.El
351.Sh Sending and Receiving Data
352Two other methods are also supported by all nodes:
353.Bl -tag -width xxx
354.It Receive data message
355A
356.Em Netgraph queueable reqest item
357(usually refered to as an
358.Em item
359is recieved by the function.
360The item contains a pointer to an mbuf and metadata about the packet.
361.Pp
362The node is notified on which hook the item arrived,
363and can use this information in its processing decision.
364The receiving node must always
365.Fn NG_FREE_M
366the mbuf chain on completion or error, or pass it on to another node
367(or kernel module) which will then be responsible for freeing it.
368Similarly the
369.Em item
370must be freed if it is not to be passed on to another node, by using the
371.Fn NG_FREE_ITEM
372macro. If the item still holds references to mbufs or metadata at the time of
373freeing then they will also be appropriatly freed.
374Therefore, if there is any chance that the mbuf or metadata will be
375changed or freed separatly from the item, it is very important
376that these fields be retrieved using the
377.Fn NGI_GET_M
378and
379.Fn NGI_GET_META
380macros that also remove the reference within the item. (or multiple frees
381of the same object will occur).
382.Pp
383If it is only required to examine the contents of the mbufs or the
384metadata, then it is possible to use the
385.Fn NGI_M
386and
387.Fn NGI_META
388macros to both read and rewrite these fields.
389.Pp
390In addition to the mbuf chain itself there may also be a pointer to a
391structure describing meta-data about the message
392(e.g. priority information). This pointer may be
393.Dv NULL
394if there is no additional information. The format for this information is
395described in
396.Pa sys/netgraph/netgraph.h .
397The memory for meta-data must allocated via
398.Fn malloc
399with type
400.Dv M_NETGRAPH_META .
401As with the data itself, it is the receiver's responsibility to
402.Fn free
403the meta-data. If the mbuf chain is freed the meta-data must
404be freed at the same time. If the meta-data is freed but the
405real data on is passed on, then a
406.Dv NULL
407pointer must be substituted. It is also the duty of the receiver to free
408the request item itself, or to use it to pass the message on further.
409.Pp
410The receiving node may decide to defer the data by queueing it in the
411.Nm
412NETISR system (see below). It achieves this by setting the
413.Dv HK_QUEUE
414flag in the flags word of the hook on which that data will arrive.
415The infrastructure will respect that bit and queue the data for delivery at
416a later time, rather than deliver it directly. A node may decide to set
417the bit on the
418.Em peer
419node, so that it's own output packets are queued. This is used
420by device drivers running at different processor priorities to transfer
421packet delivery to the splnet() level at which the bulk of
422.Nm
423runs.
424.Pp
425The structure and use of meta-data is still experimental, but is
426presently used in frame-relay to indicate that management packets
427should be queued for transmission
428at a higher priority than data packets. This is required for
429conformance with Frame Relay standards.
430.It Receive control message
431This method is called when a control message is addressed to the node.
432As with the received data, an
433.Em item
434is reveived, with a pointer to the control message.
435The message can be examined using the
436.Fn NGI_MSG
437macro, or completely extracted from the item using the
438.Fn NGI_GET_MSG
439which also removes the reference within the item.
440If the Item still holds a reference to the message when it is freed
441(using the
442.Fn NG_FREE_ITEM
443macro), then the message will also be freed appropriatly. If the
444reference has been removed the node must free the message itself using the
445.Fn NG_FREE_MSG
446macro.
447A return address is always supplied, giving the address of the node
448that originated the message so a reply message can be sent anytime later.
449The return address is retrieved from the
450.Em item
451using the
452.Fn NGI_RETADDR
453macro and is of type
454.Em ng_ID_t.
455All control messages and replies are
456allocated with
457.Fn malloc
458type
459.Dv M_NETGRAPH_MSG ,
460however it is more usual to use the
461.Fn NG_MKMESSAGE
462and
463.Fn NG_MKRESPONSE
464macros to allocate and fill out a message.
465Messages must be freed using the
466.Fn NG_FREE_MSG
467macro.
468.Pp
469If the message was delivered via a specific hook, that hook will
470also be made known, which allows the use of such things as flow-control
471messages, and status change messages, where the node may want to forward
472the message out another hook to that on which it arrived.
473.El
474.Pp
475Much use has been made of reference counts, so that nodes being
476free'd of all references are automatically freed, and this behaviour
477has been tested and debugged to present a consistent and trustworthy
478framework for the
479.Dq type module
480writer to use.
481.Sh Addressing
482The
483.Nm
484framework provides an unambiguous and simple to use method of specifically
485addressing any single node in the graph. The naming of a node is
486independent of its type, in that another node, or external component
487need not know anything about the node's type in order to address it so as
488to send it a generic message type. Node and hook names should be
489chosen so as to make addresses meaningful.
490.Pp
491Addresses are either absolute or relative. An absolute address begins
492with a node name, (or ID), followed by a colon, followed by a sequence of hook
493names separated by periods. This addresses the node reached by starting
494at the named node and following the specified sequence of hooks.
495A relative address includes only the sequence of hook names, implicitly
496starting hook traversal at the local node.
497.Pp
498There are a couple of special possibilities for the node name.
499The name
500.Dq .\&
501(referred to as
502.Dq \&.: )
503always refers to the local node.
504Also, nodes that have no global name may be addressed by their ID numbers,
505by enclosing the hex representation of the ID number within square brackets.
506Here are some examples of valid netgraph addresses:
507.Bd -literal -offset 4n -compact
508
509 .:
510 [3f]:
511 foo:
512 .:hook1
513 foo:hook1.hook2
514 [d80]:hook1
515.Ed
516.Pp
517Consider the following set of nodes might be created for a site with
518a single physical frame relay line having two active logical DLCI channels,
519with RFC-1490 frames on DLCI 16 and PPP frames over DLCI 20:
520.Pp
521.Bd -literal
522[type SYNC ] [type FRAME] [type RFC1490]
523[ "Frame1" ](uplink)<-->(data)[<un-named>](dlci16)<-->(mux)[<un-named> ]
524[ A ] [ B ](dlci20)<---+ [ C ]
525 |
526 | [ type PPP ]
527 +>(mux)[<un-named>]
528 [ D ]
529.Ed
530.Pp
531One could always send a control message to node C from anywhere
532by using the name
533.Em "Frame1:uplink.dlci16" .
534In this case, node C would also be notified that the message
535reached it via its hook
536.Dq mux .
537Similarly,
538.Em "Frame1:uplink.dlci20"
539could reliably be used to reach node D, and node A could refer
540to node B as
541.Em ".:uplink" ,
542or simply
543.Em "uplink" .
544Conversely, B can refer to A as
545.Em "data" .
546The address
547.Em "mux.data"
548could be used by both nodes C and D to address a message to node A.
549.Pp
550Note that this is only for
551.Em control messages .
552In each of these cases, where a relative addressing mode is
553used, the recipient is notified of the hook on which the
554message arrived, as well as
555the originating node.
556This allows the option of hop-by-hop distibution of messages and
557state information.
558Data messages are
559.Em only
560routed one hop at a time, by specifying the departing
561hook, with each node making
562the next routing decision. So when B receives a frame on hook
563.Dq data
564it decodes the frame relay header to determine the DLCI,
565and then forwards the unwrapped frame to either C or D.
566.Pp
567In a similar way, flow control messages may be routed in the reverse
568direction to outgoing data. For example a "buffer nearly full" message from
569.Em "Frame1:
570would be passed to node
571.Em B
572which might decide to send similar messages to both nodes
573.Em C
574and
575.Em D .
576The nodes would use
577.Em "Direct hook pointer"
578addressing to route the messages. The message may have travelled from
579.Em "Frame1:
580to
581.Em B
582as a synchronous reply, saving time and cycles.
583.Pp
584A similar graph might be used to represent multi-link PPP running
585over an ISDN line:
586.Pp
587.Bd -literal
588[ type BRI ](B1)<--->(link1)[ type MPP ]
589[ "ISDN1" ](B2)<--->(link2)[ (no name) ]
590[ ](D) <-+
591 |
592 +----------------+
593 |
594 +->(switch)[ type Q.921 ](term1)<---->(datalink)[ type Q.931 ]
595 [ (no name) ] [ (no name) ]
596.Ed
597.Sh Netgraph Structures
598Structures are defined in
599.Pa sys/netgraph/netgraph.h
600(for kernel sructures only of interest to nodes)
601and
602.Pa sys/netgraph/ng_message.h
603(for message definitions also of interest to user programs).
604.Pp
605The two basic object types that are of interest to node authors are
606.Em nodes
607and
608.Em hooks .
609These two objects have the following
610properties that are also of interest to the node writers.
611.Bl -tag -width xxx
612.It struct ng_node
613Node authors should always use the following typedef to declare
614their pointers, and should never actually declare the structure.
615.Pp
616typedef struct ng_node *node_p;
617.Pp
618The following properties are associated with a node, and can be
619accessed in the following manner:
620.Bl -bullet -compact -offset 2n
621.Pp
622.It
623Validity
624.Pp
625A driver or interrupt routine may want to check whether
626the node is still valid. It is assumed that the caller holds a reference
627on the node so it will not have been freed, however it may have been
628disabled or otherwise shut down. Using the
629.Fn NG_NODE_IS_VALID "node"
630macro will return this state. Eventually it should be almost impossible
631for code to run in an invalid node but at this time that work has not been
632completed.
633.Pp
634.It
635node ID
636.Pp
637Of type
638.Em ng_ID_t ,
639This property can be retrieved using the macro
640.Fn NG_NODE_ID "node".
641.Pp
642.It
643node name
644.Pp
645Optional globally unique name, null terminated string. If there
646is a value in here, it is the name of the node.
647.Pp
648if (
649.Fn NG_NODE_NAME "node"
650[0]) ....
651.Pp
652if (strncmp(
653.Fn NG_NODE_NAME "node"
654, "fred", NG_NODELEN)) ...
655.Pp
656.It
657A node dependent opaque cookie
658.Pp
659You may place anything of type
660.Em pointer
661here.
662Use the macros
663.Fn NG_NODE_SET_PRIVATE "node, value"
664and
665.Fn NG_NODE_PRIVATE "node"
666to set and retrieve this property.
667.Pp
668.It
669number of hooks
670.Pp
671Use
672.Fn NG_NODE_NUMHOOKS "node"
673to retrieve this value.
674.Pp
675.It
676hooks
677.Pp
678The node may have a number of hooks.
679A traversal method is provided to allow all the hooks to be
680tested for some condition.
681.Fn NG_NODE_FOREACH_HOOK "node, fn, arg, rethook"
682where fn is a function that will be called for each hook
683with the form
684.Fn fn "hook, arg"
685and returning 0 to terminate the search. If the search is terminated, then
686.Em rethook
687will be set to the hook at which the search was terminated.
688.El
689.It struct ng_hook
690Node authors should always use the following typedef to declare
691their hook pointers.
692.Pp
693typedef struct ng_hook *hook_p;
694.Pp
695The following properties are associated with a hook, and can be
696accessed in the following manner:
697.Bl -bullet -compact -offset 2n
698.Pp
699.It
700A node dependent opaque cookie.
701.Pp
702You may place anything of type
703.Em pointer
704here.
705Use the macros
706.Fn NG_HOOK_SET_PRIVATE "hook, value"
707and
708.Fn NG_HOOK_PRIVATE "hook"
709to set and retrieve this property.
710.Pp
711.It
712An associate node.
713.Pp
714You may use the macro
715.Fn NG_HOOK_NODE "hook"
716to find the associated node.
717.Pp
718.It
719A peer hook
720.Pp
721The other hook in this connected pair. Of type hook_p. You can
722use
723.Fn NG_HOOK_PEER "hook"
724to find the peer.
725.Pp
726.It
727references
728.Pp
729.Fn NG_HOOK_REF "hook"
730and
731.Fn NG_HOOK_UNREF "hook"
732increment and decrement the hook reference count accordingly.
37.\" $Whistle: netgraph.4,v 1.7 1999/01/28 23:54:52 julian Exp $
38.\"
39.Dd January 19, 1999
40.Dt NETGRAPH 4
41.Os FreeBSD
42.Sh NAME
43.Nm netgraph
44.Nd graph based kernel networking subsystem
45.Sh DESCRIPTION
46The
47.Nm
48system provides a uniform and modular system for the implementation
49of kernel objects which perform various networking functions. The objects,
50known as
51.Em nodes ,
52can be arranged into arbitrarily complicated graphs. Nodes have
53.Em hooks
54which are used to connect two nodes together, forming the edges in the graph.
55Nodes communicate along the edges to process data, implement protocols, etc.
56.Pp
57The aim of
58.Nm
59is to supplement rather than replace the existing kernel networking
60infrastructure. It provides:
61.Pp
62.Bl -bullet -compact -offset 2n
63.It
64A flexible way of combining protocol and link level drivers
65.It
66A modular way to implement new protocols
67.It
68A common framework for kernel entities to inter-communicate
69.It
70A reasonably fast, kernel-based implementation
71.El
72.Sh Nodes and Types
73The most fundamental concept in
74.Nm
75is that of a
76.Em node .
77All nodes implement a number of predefined methods which allow them
78to interact with other nodes in a well defined manner.
79.Pp
80Each node has a
81.Em type ,
82which is a static property of the node determined at node creation time.
83A node's type is described by a unique
84.Tn ASCII
85type name.
86The type implies what the node does and how it may be connected
87to other nodes.
88.Pp
89In object-oriented language, types are classes and nodes are instances
90of their respective class. All node types are subclasses of the generic node
91type, and hence inherit certain common functionality and capabilities
92(e.g., the ability to have an
93.Tn ASCII
94name).
95.Pp
96Nodes may be assigned a globally unique
97.Tn ASCII
98name which can be
99used to refer to the node.
100The name must not contain the characters
101.Dq .\&
102or
103.Dq \&:
104and is limited to
105.Dv "NG_NODELEN + 1"
106characters (including NUL byte).
107.Pp
108Each node instance has a unique
109.Em ID number
110which is expressed as a 32-bit hex value. This value may be used to
111refer to a node when there is no
112.Tn ASCII
113name assigned to it.
114.Sh Hooks
115Nodes are connected to other nodes by connecting a pair of
116.Em hooks ,
117one from each node. Data flows bidirectionally between nodes along
118connected pairs of hooks. A node may have as many hooks as it
119needs, and may assign whatever meaning it wants to a hook.
120.Pp
121Hooks have these properties:
122.Pp
123.Bl -bullet -compact -offset 2n
124.It
125A hook has an
126.Tn ASCII
127name which is unique among all hooks
128on that node (other hooks on other nodes may have the same name).
129The name must not contain a
130.Dq .\&
131or a
132.Dq \&:
133and is
134limited to
135.Dv "NG_HOOKLEN + 1"
136characters (including NUL byte).
137.It
138A hook is always connected to another hook. That is, hooks are
139created at the time they are connected, and breaking an edge by
140removing either hook destroys both hooks.
141.It
142A hook can be set into a state where incoming packets are always queued
143by the input queuing system, rather than being delivered directly. This
144is used when the two joined nodes need to be decoupled, e.g. if they are
145running at different processor priority levels. (spl)
146.El
147.Pp
148A node may decide to assign special meaning to some hooks.
149For example, connecting to the hook named
150.Dq debug
151might trigger
152the node to start sending debugging information to that hook.
153.Sh Data Flow
154Two types of information flow between nodes: data messages and
155control messages. Data messages are passed in mbuf chains along the edges
156in the graph, one edge at a time. The first mbuf in a chain must have the
157.Dv M_PKTHDR
158flag set. Each node decides how to handle data coming in on its hooks.
159.Pp
160Control messages are type-specific C structures sent from one node
161directly to some arbitrary other node. Control messages have a common
162header format, followed by type-specific data, and are binary structures
163for efficiency. However, node types also may support conversion of the
164type specific data between binary and
165.Tn ASCII
166for debugging and human interface purposes (see the
167.Dv NGM_ASCII2BINARY
168and
169.Dv NGM_BINARY2ASCII
170generic control messages below). Nodes are not required to support
171these conversions.
172.Pp
173There are three ways to address a control message. If
174there is a sequence of edges connecting the two nodes, the message
175may be
176.Dq source routed
177by specifying the corresponding sequence
178of
179.Tn ASCII
180hook names as the destination address for the message (relative
181addressing). If the destination is adjacent to the source, then the source
182node may simply specify (as a pointer in the code) the hook across which the
183message should be sent. Otherwise, the recipient node global
184.Tn ASCII
185name
186(or equivalent ID based name) is used as the destination address
187for the message (absolute addressing). The two types of
188.Tn ASCII
189addressing
190may be combined, by specifying an absolute start node and a sequence
191of hooks. Only the
192.Tn ASCII
193addressing modes are available to control programs outside the kernel,
194as use of direct pointers is limited of course to kernel modules.
195.Pp
196Messages often represent commands that are followed by a reply message
197in the reverse direction. To facilitate this, the recipient of a
198control message is supplied with a
199.Dq return address
200that is suitable for addressing a reply.
201.Pp
202Each control message contains a 32 bit value called a
203.Em typecookie
204indicating the type of the message, i.e., how to interpret it.
205Typically each type defines a unique typecookie for the messages
206that it understands. However, a node may choose to recognize and
207implement more than one type of message.
208.Pp
209If a message is delivered to an address that implies that it arrived
210at that node through a particular hook, (as opposed to having been directly
211addressed using its ID or global name), then that hook is identified to the
212receiving node. This allows a message to be rerouted or passed on, should
213a node decide that this is required, in much the same way that data packets
214are passed around between nodes. A set of standard
215messages for flow control and link management purposes are
216defined by the base system that are usually
217passed around in this manner. Flow control message would usually travel
218in the opposite direction to the data to which they pertain.
219.Sh Netgraph is (usually) Functional
220In order to minimize latency, most
221.Nm
222operations are functional.
223That is, data and control messages are delivered by making function
224calls rather than by using queues and mailboxes. For example, if node
225A wishes to send a data mbuf to neighboring node B, it calls the
226generic
227.Nm
228data delivery function. This function in turn locates
229node B and calls B's
230.Dq receive data
231method. There are exceptions to this.
232.Pp
233Each node has an input queue, and some operations can be considered to
234be 'writers' in that they alter the state of the node. Obviously in an SMP
235world it would be bad if the state of a node were changed while another
236data packet were transiting the node. For this purpose, the input queue
237implements a
238.Em reader/writer
239semantic so that when there is a writer in the node, all other requests
240are queued, and while there are readers, a writer, and any following
241packets are queued. In the case where there is no reason to queue the
242data, the input method is called directly, as mentionned above.
243.Pp
244A node may declare that all requests should be considered as writers,
245or that requests coming in over a particular hook should be considered to
246be a writer, or even that packets leaving or entering across a particular
247hook should always be queued, rather than delivered directly (often useful
248for interrupt routines who want to get back to the hardware quickly).
249By default, all controll message packets are considered to be writers
250unless specifically declared to be a reader in their definition. (see
251NGM_READONLY in ng_message.h)
252.Pp
253While this mode of operation
254results in good performance, it has a few implications for node
255developers:
256.Pp
257.Bl -bullet -compact -offset 2n
258.It
259Whenever a node delivers a data or control message, the node
260may need to allow for the possibility of receiving a returning
261message before the original delivery function call returns.
262.It
263Netgraph nodes and support routines generally run at
264.Fn splnet .
265However, some nodes may want to send data and control messages
266from a different priority level. Netgraph supplies a mechanism which
267utilizes the NETISR system to move message and data delivery to
268.Fn splnet .
269Nodes that run at other priorities (e.g. interfaces) can be directly
270linked to other nodes so that the combination runs at the other priority,
271however any interaction with nodes running at splnet MUST be achieved via the
272queueing functions, (which use the
273.Fn netisr
274feature of the kernel).
275Note that messages are always received at
276.Fn splnet .
277.It
278It's possible for an infinite loop to occur if the graph contains cycles.
279.El
280.Pp
281So far, these issues have not proven problematical in practice.
282.Sh Interaction With Other Parts of the Kernel
283A node may have a hidden interaction with other components of the
284kernel outside of the
285.Nm
286subsystem, such as device hardware,
287kernel protocol stacks, etc. In fact, one of the benefits of
288.Nm
289is the ability to join disparate kernel networking entities together in a
290consistent communication framework.
291.Pp
292An example is the node type
293.Em socket
294which is both a netgraph node and a
295.Xr socket 2
296BSD socket in the protocol family
297.Dv PF_NETGRAPH .
298Socket nodes allow user processes to participate in
299.Nm .
300Other nodes communicate with socket nodes using the usual methods, and the
301node hides the fact that it is also passing information to and from a
302cooperating user process.
303.Pp
304Another example is a device driver that presents
305a node interface to the hardware.
306.Sh Node Methods
307Nodes are notified of the following actions via function calls
308to the following node methods (all at
309.Fn splnet )
310and may accept or reject that action (by returning the appropriate
311error code):
312.Bl -tag -width xxx
313.It Creation of a new node
314The constructor for the type is called. If creation of a new node is
315allowed, the constructor must call the generic node creation
316function (in object-oriented terms, the superclass constructor)
317and then allocate any special resources it needs. For nodes that
318correspond to hardware, this is typically done during the device
319attach routine. Often a global
320.Tn ASCII
321name corresponding to the
322device name is assigned here as well.
323.It Creation of a new hook
324The hook is created and tentatively
325linked to the node, and the node is told about the name that will be
326used to describe this hook. The node sets up any special data structures
327it needs, or may reject the connection, based on the name of the hook.
328.It Successful connection of two hooks
329After both ends have accepted their
330hooks, and the links have been made, the nodes get a chance to
331find out who their peer is across the link and can then decide to reject
332the connection. Tear-down is automatic. This is also the time at which
333a node may decide whether to set a particular hook (or its peer) into
334.Em queuing
335mode.
336.It Destruction of a hook
337The node is notified of a broken connection. The node may consider some hooks
338to be critical to operation and others to be expendable: the disconnection
339of one hook may be an acceptable event while for another it
340may effect a total shutdown for the node.
341.It Shutdown of a node
342This method allows a node to clean up
343and to ensure that any actions that need to be performed
344at this time are taken. The method must call the generic (i.e., superclass)
345node destructor to get rid of the generic components of the node.
346Some nodes (usually associated with a piece of hardware) may be
347.Em persistent
348in that a shutdown breaks all edges and resets the node,
349but doesn't remove it, in which case the generic destructor is not called.
350.El
351.Sh Sending and Receiving Data
352Two other methods are also supported by all nodes:
353.Bl -tag -width xxx
354.It Receive data message
355A
356.Em Netgraph queueable reqest item
357(usually refered to as an
358.Em item
359is recieved by the function.
360The item contains a pointer to an mbuf and metadata about the packet.
361.Pp
362The node is notified on which hook the item arrived,
363and can use this information in its processing decision.
364The receiving node must always
365.Fn NG_FREE_M
366the mbuf chain on completion or error, or pass it on to another node
367(or kernel module) which will then be responsible for freeing it.
368Similarly the
369.Em item
370must be freed if it is not to be passed on to another node, by using the
371.Fn NG_FREE_ITEM
372macro. If the item still holds references to mbufs or metadata at the time of
373freeing then they will also be appropriatly freed.
374Therefore, if there is any chance that the mbuf or metadata will be
375changed or freed separatly from the item, it is very important
376that these fields be retrieved using the
377.Fn NGI_GET_M
378and
379.Fn NGI_GET_META
380macros that also remove the reference within the item. (or multiple frees
381of the same object will occur).
382.Pp
383If it is only required to examine the contents of the mbufs or the
384metadata, then it is possible to use the
385.Fn NGI_M
386and
387.Fn NGI_META
388macros to both read and rewrite these fields.
389.Pp
390In addition to the mbuf chain itself there may also be a pointer to a
391structure describing meta-data about the message
392(e.g. priority information). This pointer may be
393.Dv NULL
394if there is no additional information. The format for this information is
395described in
396.Pa sys/netgraph/netgraph.h .
397The memory for meta-data must allocated via
398.Fn malloc
399with type
400.Dv M_NETGRAPH_META .
401As with the data itself, it is the receiver's responsibility to
402.Fn free
403the meta-data. If the mbuf chain is freed the meta-data must
404be freed at the same time. If the meta-data is freed but the
405real data on is passed on, then a
406.Dv NULL
407pointer must be substituted. It is also the duty of the receiver to free
408the request item itself, or to use it to pass the message on further.
409.Pp
410The receiving node may decide to defer the data by queueing it in the
411.Nm
412NETISR system (see below). It achieves this by setting the
413.Dv HK_QUEUE
414flag in the flags word of the hook on which that data will arrive.
415The infrastructure will respect that bit and queue the data for delivery at
416a later time, rather than deliver it directly. A node may decide to set
417the bit on the
418.Em peer
419node, so that it's own output packets are queued. This is used
420by device drivers running at different processor priorities to transfer
421packet delivery to the splnet() level at which the bulk of
422.Nm
423runs.
424.Pp
425The structure and use of meta-data is still experimental, but is
426presently used in frame-relay to indicate that management packets
427should be queued for transmission
428at a higher priority than data packets. This is required for
429conformance with Frame Relay standards.
430.It Receive control message
431This method is called when a control message is addressed to the node.
432As with the received data, an
433.Em item
434is reveived, with a pointer to the control message.
435The message can be examined using the
436.Fn NGI_MSG
437macro, or completely extracted from the item using the
438.Fn NGI_GET_MSG
439which also removes the reference within the item.
440If the Item still holds a reference to the message when it is freed
441(using the
442.Fn NG_FREE_ITEM
443macro), then the message will also be freed appropriatly. If the
444reference has been removed the node must free the message itself using the
445.Fn NG_FREE_MSG
446macro.
447A return address is always supplied, giving the address of the node
448that originated the message so a reply message can be sent anytime later.
449The return address is retrieved from the
450.Em item
451using the
452.Fn NGI_RETADDR
453macro and is of type
454.Em ng_ID_t.
455All control messages and replies are
456allocated with
457.Fn malloc
458type
459.Dv M_NETGRAPH_MSG ,
460however it is more usual to use the
461.Fn NG_MKMESSAGE
462and
463.Fn NG_MKRESPONSE
464macros to allocate and fill out a message.
465Messages must be freed using the
466.Fn NG_FREE_MSG
467macro.
468.Pp
469If the message was delivered via a specific hook, that hook will
470also be made known, which allows the use of such things as flow-control
471messages, and status change messages, where the node may want to forward
472the message out another hook to that on which it arrived.
473.El
474.Pp
475Much use has been made of reference counts, so that nodes being
476free'd of all references are automatically freed, and this behaviour
477has been tested and debugged to present a consistent and trustworthy
478framework for the
479.Dq type module
480writer to use.
481.Sh Addressing
482The
483.Nm
484framework provides an unambiguous and simple to use method of specifically
485addressing any single node in the graph. The naming of a node is
486independent of its type, in that another node, or external component
487need not know anything about the node's type in order to address it so as
488to send it a generic message type. Node and hook names should be
489chosen so as to make addresses meaningful.
490.Pp
491Addresses are either absolute or relative. An absolute address begins
492with a node name, (or ID), followed by a colon, followed by a sequence of hook
493names separated by periods. This addresses the node reached by starting
494at the named node and following the specified sequence of hooks.
495A relative address includes only the sequence of hook names, implicitly
496starting hook traversal at the local node.
497.Pp
498There are a couple of special possibilities for the node name.
499The name
500.Dq .\&
501(referred to as
502.Dq \&.: )
503always refers to the local node.
504Also, nodes that have no global name may be addressed by their ID numbers,
505by enclosing the hex representation of the ID number within square brackets.
506Here are some examples of valid netgraph addresses:
507.Bd -literal -offset 4n -compact
508
509 .:
510 [3f]:
511 foo:
512 .:hook1
513 foo:hook1.hook2
514 [d80]:hook1
515.Ed
516.Pp
517Consider the following set of nodes might be created for a site with
518a single physical frame relay line having two active logical DLCI channels,
519with RFC-1490 frames on DLCI 16 and PPP frames over DLCI 20:
520.Pp
521.Bd -literal
522[type SYNC ] [type FRAME] [type RFC1490]
523[ "Frame1" ](uplink)<-->(data)[<un-named>](dlci16)<-->(mux)[<un-named> ]
524[ A ] [ B ](dlci20)<---+ [ C ]
525 |
526 | [ type PPP ]
527 +>(mux)[<un-named>]
528 [ D ]
529.Ed
530.Pp
531One could always send a control message to node C from anywhere
532by using the name
533.Em "Frame1:uplink.dlci16" .
534In this case, node C would also be notified that the message
535reached it via its hook
536.Dq mux .
537Similarly,
538.Em "Frame1:uplink.dlci20"
539could reliably be used to reach node D, and node A could refer
540to node B as
541.Em ".:uplink" ,
542or simply
543.Em "uplink" .
544Conversely, B can refer to A as
545.Em "data" .
546The address
547.Em "mux.data"
548could be used by both nodes C and D to address a message to node A.
549.Pp
550Note that this is only for
551.Em control messages .
552In each of these cases, where a relative addressing mode is
553used, the recipient is notified of the hook on which the
554message arrived, as well as
555the originating node.
556This allows the option of hop-by-hop distibution of messages and
557state information.
558Data messages are
559.Em only
560routed one hop at a time, by specifying the departing
561hook, with each node making
562the next routing decision. So when B receives a frame on hook
563.Dq data
564it decodes the frame relay header to determine the DLCI,
565and then forwards the unwrapped frame to either C or D.
566.Pp
567In a similar way, flow control messages may be routed in the reverse
568direction to outgoing data. For example a "buffer nearly full" message from
569.Em "Frame1:
570would be passed to node
571.Em B
572which might decide to send similar messages to both nodes
573.Em C
574and
575.Em D .
576The nodes would use
577.Em "Direct hook pointer"
578addressing to route the messages. The message may have travelled from
579.Em "Frame1:
580to
581.Em B
582as a synchronous reply, saving time and cycles.
583.Pp
584A similar graph might be used to represent multi-link PPP running
585over an ISDN line:
586.Pp
587.Bd -literal
588[ type BRI ](B1)<--->(link1)[ type MPP ]
589[ "ISDN1" ](B2)<--->(link2)[ (no name) ]
590[ ](D) <-+
591 |
592 +----------------+
593 |
594 +->(switch)[ type Q.921 ](term1)<---->(datalink)[ type Q.931 ]
595 [ (no name) ] [ (no name) ]
596.Ed
597.Sh Netgraph Structures
598Structures are defined in
599.Pa sys/netgraph/netgraph.h
600(for kernel sructures only of interest to nodes)
601and
602.Pa sys/netgraph/ng_message.h
603(for message definitions also of interest to user programs).
604.Pp
605The two basic object types that are of interest to node authors are
606.Em nodes
607and
608.Em hooks .
609These two objects have the following
610properties that are also of interest to the node writers.
611.Bl -tag -width xxx
612.It struct ng_node
613Node authors should always use the following typedef to declare
614their pointers, and should never actually declare the structure.
615.Pp
616typedef struct ng_node *node_p;
617.Pp
618The following properties are associated with a node, and can be
619accessed in the following manner:
620.Bl -bullet -compact -offset 2n
621.Pp
622.It
623Validity
624.Pp
625A driver or interrupt routine may want to check whether
626the node is still valid. It is assumed that the caller holds a reference
627on the node so it will not have been freed, however it may have been
628disabled or otherwise shut down. Using the
629.Fn NG_NODE_IS_VALID "node"
630macro will return this state. Eventually it should be almost impossible
631for code to run in an invalid node but at this time that work has not been
632completed.
633.Pp
634.It
635node ID
636.Pp
637Of type
638.Em ng_ID_t ,
639This property can be retrieved using the macro
640.Fn NG_NODE_ID "node".
641.Pp
642.It
643node name
644.Pp
645Optional globally unique name, null terminated string. If there
646is a value in here, it is the name of the node.
647.Pp
648if (
649.Fn NG_NODE_NAME "node"
650[0]) ....
651.Pp
652if (strncmp(
653.Fn NG_NODE_NAME "node"
654, "fred", NG_NODELEN)) ...
655.Pp
656.It
657A node dependent opaque cookie
658.Pp
659You may place anything of type
660.Em pointer
661here.
662Use the macros
663.Fn NG_NODE_SET_PRIVATE "node, value"
664and
665.Fn NG_NODE_PRIVATE "node"
666to set and retrieve this property.
667.Pp
668.It
669number of hooks
670.Pp
671Use
672.Fn NG_NODE_NUMHOOKS "node"
673to retrieve this value.
674.Pp
675.It
676hooks
677.Pp
678The node may have a number of hooks.
679A traversal method is provided to allow all the hooks to be
680tested for some condition.
681.Fn NG_NODE_FOREACH_HOOK "node, fn, arg, rethook"
682where fn is a function that will be called for each hook
683with the form
684.Fn fn "hook, arg"
685and returning 0 to terminate the search. If the search is terminated, then
686.Em rethook
687will be set to the hook at which the search was terminated.
688.El
689.It struct ng_hook
690Node authors should always use the following typedef to declare
691their hook pointers.
692.Pp
693typedef struct ng_hook *hook_p;
694.Pp
695The following properties are associated with a hook, and can be
696accessed in the following manner:
697.Bl -bullet -compact -offset 2n
698.Pp
699.It
700A node dependent opaque cookie.
701.Pp
702You may place anything of type
703.Em pointer
704here.
705Use the macros
706.Fn NG_HOOK_SET_PRIVATE "hook, value"
707and
708.Fn NG_HOOK_PRIVATE "hook"
709to set and retrieve this property.
710.Pp
711.It
712An associate node.
713.Pp
714You may use the macro
715.Fn NG_HOOK_NODE "hook"
716to find the associated node.
717.Pp
718.It
719A peer hook
720.Pp
721The other hook in this connected pair. Of type hook_p. You can
722use
723.Fn NG_HOOK_PEER "hook"
724to find the peer.
725.Pp
726.It
727references
728.Pp
729.Fn NG_HOOK_REF "hook"
730and
731.Fn NG_HOOK_UNREF "hook"
732increment and decrement the hook reference count accordingly.
733After decrement you should always sume the hook has been freed.
734In fact the macro may set it to NULL.
733After decrement you should always assume the hook has been freed
734unless you have another reference still valid.
735.El
736.Pp
737The maintenance of the names, reference counts, and linked list
738of hooks for each node is handled automatically by the
739.Nm
740subsystem.
741Typically a node's private info contains a back-pointer to the node or hook
742structure, which counts as a new reference that must be included
743in the reference count for the node. When the node constructor is called
744there is already a reference for this calculated in, so that
745when the node is destroyed, it should remember to do a
746.Fn NG_NODE_UNREF
747on the node.
748.Pp
749From a hook you can obtain the corresponding node, and from
750a node, it is possible to traverse all the active hooks.
751.Pp
752A current example of how to define a node can always be seen in
753.Em sys/netgraph/ng_sample.c
754and should be used as a starting point for new node writers.
755.El
756.Sh Netgraph Message Structure
757Control messages have the following structure:
758.Bd -literal
759#define NG_CMDSTRLEN 15 /* Max command string (16 with null) */
760
761struct ng_mesg {
762 struct ng_msghdr {
763 u_char version; /* Must equal NG_VERSION */
764 u_char spare; /* Pad to 2 bytes */
765 u_short arglen; /* Length of cmd/resp data */
766 u_long flags; /* Message status flags */
767 u_long token; /* Reply should have the same token */
768 u_long typecookie; /* Node type understanding this message */
769 u_long cmd; /* Command identifier */
770 u_char cmdstr[NG_CMDSTRLEN+1]; /* Cmd string (for debug) */
771 } header;
772 char data[0]; /* Start of cmd/resp data */
773};
774
775#define NG_ABI_VERSION 5 /* Netgraph kernel ABI version */
776#define NG_VERSION 4 /* Netgraph message version */
777#define NGF_ORIG 0x0000 /* Command */
778#define NGF_RESP 0x0001 /* Response */
779.Ed
780.Pp
781Control messages have the fixed header shown above, followed by a
782variable length data section which depends on the type cookie
783and the command. Each field is explained below:
784.Bl -tag -width xxx
785.It Dv version
786Indicates the version of the netgraph message protocol itself. The current version is
787.Dv NG_VERSION .
788.It Dv arglen
789This is the length of any extra arguments, which begin at
790.Dv data .
791.It Dv flags
792Indicates whether this is a command or a response control message.
793.It Dv token
794The
795.Dv token
796is a means by which a sender can match a reply message to the
797corresponding command message; the reply always has the same token.
798.Pp
799.It Dv typecookie
800The corresponding node type's unique 32-bit value.
801If a node doesn't recognize the type cookie it must reject the message
802by returning
803.Er EINVAL .
804.Pp
805Each type should have an include file that defines the commands,
806argument format, and cookie for its own messages.
807The typecookie
808insures that the same header file was included by both sender and
809receiver; when an incompatible change in the header file is made,
810the typecookie
811.Em must
812be changed.
813The de facto method for generating unique type cookies is to take the
814seconds from the epoch at the time the header file is written
815(i.e., the output of
816.Dv "date -u +'%s'" ) .
817.Pp
818There is a predefined typecookie
819.Dv NGM_GENERIC_COOKIE
820for the
821.Dq generic
822node type, and
823a corresponding set of generic messages which all nodes understand.
824The handling of these messages is automatic.
825.It Dv command
826The identifier for the message command. This is type specific,
827and is defined in the same header file as the typecookie.
828.It Dv cmdstr
829Room for a short human readable version of
830.Dq command
831(for debugging purposes only).
832.El
833.Pp
834Some modules may choose to implement messages from more than one
835of the header files and thus recognize more than one type cookie.
836.Sh Control Message ASCII Form
837Control messages are in binary format for efficiency. However, for
838debugging and human interface purposes, and if the node type supports
839it, control messages may be converted to and from an equivalent
840.Tn ASCII
841form. The
842.Tn ASCII
843form is similar to the binary form, with two exceptions:
844.Pp
845.Bl -tag -compact -width xxx
846.It o
847The
848.Dv cmdstr
849header field must contain the
850.Tn ASCII
851name of the command, corresponding to the
852.Dv cmd
853header field.
854.It o
855The
856.Dv args
857field contains a NUL-terminated
858.Tn ASCII
859string version of the message arguments.
860.El
861.Pp
862In general, the arguments field of a control messgage can be any
863arbitrary C data type. Netgraph includes parsing routines to support
864some pre-defined datatypes in
865.Tn ASCII
866with this simple syntax:
867.Pp
868.Bl -tag -compact -width xxx
869.It o
870Integer types are represented by base 8, 10, or 16 numbers.
871.It o
872Strings are enclosed in double quotes and respect the normal
873C language backslash escapes.
874.It o
875IP addresses have the obvious form.
876.It o
877Arrays are enclosed in square brackets, with the elements listed
878consecutively starting at index zero. An element may have an optional
879index and equals sign preceeding it. Whenever an element
880does not have an explicit index, the index is implicitly the previous
881element's index plus one.
882.It o
883Structures are enclosed in curly braces, and each field is specified
884in the form
885.Dq fieldname=value .
886.It o
887Any array element or structure field whose value is equal to its
888.Dq default value
889may be omitted. For integer types, the default value
890is usually zero; for string types, the empty string.
891.It o
892Array elements and structure fields may be specified in any order.
893.El
894.Pp
895Each node type may define its own arbitrary types by providing
896the necessary routines to parse and unparse.
897.Tn ASCII
898forms defined
899for a specific node type are documented in the documentation for
900that node type.
901.Sh Generic Control Messages
902There are a number of standard predefined messages that will work
903for any node, as they are supported directly by the framework itself.
904These are defined in
905.Pa ng_message.h
906along with the basic layout of messages and other similar information.
907.Bl -tag -width xxx
908.It Dv NGM_CONNECT
909Connect to another node, using the supplied hook names on either end.
910.It Dv NGM_MKPEER
911Construct a node of the given type and then connect to it using the
912supplied hook names.
913.It Dv NGM_SHUTDOWN
914The target node should disconnect from all its neighbours and shut down.
915Persistent nodes such as those representing physical hardware
916might not disappear from the node namespace, but only reset themselves.
917The node must disconnect all of its hooks.
918This may result in neighbors shutting themselves down, and possibly a
919cascading shutdown of the entire connected graph.
920.It Dv NGM_NAME
921Assign a name to a node. Nodes can exist without having a name, and this
922is the default for nodes created using the
923.Dv NGM_MKPEER
924method. Such nodes can only be addressed relatively or by their ID number.
925.It Dv NGM_RMHOOK
926Ask the node to break a hook connection to one of its neighbours.
927Both nodes will have their
928.Dq disconnect
929method invoked.
930Either node may elect to totally shut down as a result.
931.It Dv NGM_NODEINFO
932Asks the target node to describe itself. The four returned fields
933are the node name (if named), the node type, the node ID and the
934number of hooks attached. The ID is an internal number unique to that node.
935.It Dv NGM_LISTHOOKS
936This returns the information given by
937.Dv NGM_NODEINFO ,
938but in addition
939includes an array of fields describing each link, and the description for
940the node at the far end of that link.
941.It Dv NGM_LISTNAMES
942This returns an array of node descriptions (as for
943.Dv NGM_NODEINFO ")"
944where each entry of the array describes a named node.
945All named nodes will be described.
946.It Dv NGM_LISTNODES
947This is the same as
948.Dv NGM_LISTNAMES
949except that all nodes are listed regardless of whether they have a name or not.
950.It Dv NGM_LISTTYPES
951This returns a list of all currently installed netgraph types.
952.It Dv NGM_TEXT_STATUS
953The node may return a text formatted status message.
954The status information is determined entirely by the node type.
955It is the only "generic" message
956that requires any support within the node itself and as such the node may
957elect to not support this message. The text response must be less than
958.Dv NG_TEXTRESPONSE
959bytes in length (presently 1024). This can be used to return general
960status information in human readable form.
961.It Dv NGM_BINARY2ASCII
962This message converts a binary control message to its
963.Tn ASCII
964form.
965The entire control message to be converted is contained within the
966arguments field of the
967.Dv Dv NGM_BINARY2ASCII
968message itself. If successful, the reply will contain the same control
969message in
970.Tn ASCII
971form.
972A node will typically only know how to translate messages that it
973itself understands, so the target node of the
974.Dv NGM_BINARY2ASCII
975is often the same node that would actually receive that message.
976.It Dv NGM_ASCII2BINARY
977The opposite of
978.Dv NGM_BINARY2ASCII .
979The entire control message to be converted, in
980.Tn ASCII
981form, is contained
982in the arguments section of the
983.Dv NGM_ASCII2BINARY
984and need only have the
985.Dv flags ,
986.Dv cmdstr ,
987and
988.Dv arglen
989header fields filled in, plus the NUL-terminated string version of
990the arguments in the arguments field. If successful, the reply
991contains the binary version of the control message.
992.El
993.Sh Flow Control Messages
994In addition to the control messages that affect nodes with respect to the
995graph, there are also a number of
996.Em Flow-control
997messages defined. At present these are
998.Em NOT
999handled automatically by the system, so
1000nodes need to handle them if they are going to be used in a graph utilising
1001flow control, and will be in the likely path of these messages. The
1002default action of a node that doesn't understand these messages should
1003be to pass them onto the next node. Hopefully some helper functions
1004will assist in this eventually. These messages are also defined in
1005.Pa sys/netgraph/ng_message.h
1006and have a separate cookie
1007.Em NG_FLOW_COOKIE
1008to help identify them. They will not be covered in depth here.
1009.Sh Metadata
1010Data moving through the
1011.Nm
1012system can be accompanied by meta-data that describes some
1013aspect of that data. The form of the meta-data is a fixed header,
1014which contains enough information for most uses, and can optionally
1015be supplemented by trailing
1016.Em option
1017structures, which contain a
1018.Em cookie
1019(see the section on control messages), an identifier, a length and optional
1020data. If a node does not recognize the cookie associated with an option,
1021it should ignore that option.
1022.Pp
1023Meta data might include such things as priority, discard eligibility,
1024or special processing requirements. It might also mark a packet for
1025debug status, etc. The use of meta-data is still experimental.
1026.Sh INITIALIZATION
1027The base
1028.Nm
1029code may either be statically compiled
1030into the kernel or else loaded dynamically as a KLD via
1031.Xr kldload 8 .
1032In the former case, include
1033.Pp
1034.Dl options NETGRAPH
1035.Pp
1036in your kernel configuration file. You may also include selected
1037node types in the kernel compilation, for example:
1038.Bd -literal -offset indent
1039options NETGRAPH
1040options NETGRAPH_SOCKET
1041options NETGRAPH_ECHO
1042.Ed
1043.Pp
1044Once the
1045.Nm
1046subsystem is loaded, individual node types may be loaded at any time
1047as KLD modules via
1048.Xr kldload 8 .
1049Moreover,
1050.Nm
1051knows how to automatically do this; when a request to create a new
1052node of unknown type
1053.Em type
1054is made,
1055.Nm
1056will attempt to load the KLD module
1057.Pa ng_type.ko .
1058.Pp
1059Types can also be installed at boot time, as certain device drivers
1060may want to export each instance of the device as a netgraph node.
1061.Pp
1062In general, new types can be installed at any time from within the
1063kernel by calling
1064.Fn ng_newtype ,
1065supplying a pointer to the type's
1066.Dv struct ng_type
1067structure.
1068.Pp
1069The
1070.Fn NETGRAPH_INIT
1071macro automates this process by using a linker set.
1072.Sh EXISTING NODE TYPES
1073Several node types currently exist. Each is fully documented
1074in its own man page:
1075.Bl -tag -width xxx
1076.It SOCKET
1077The socket type implements two new sockets in the new protocol domain
1078.Dv PF_NETGRAPH .
1079The new sockets protocols are
1080.Dv NG_DATA
1081and
1082.Dv NG_CONTROL ,
1083both of type
1084.Dv SOCK_DGRAM .
1085Typically one of each is associated with a socket node.
1086When both sockets have closed, the node will shut down. The
1087.Dv NG_DATA
1088socket is used for sending and receiving data, while the
1089.Dv NG_CONTROL
1090socket is used for sending and receiving control messages.
1091Data and control messages are passed using the
1092.Xr sendto 2
1093and
1094.Xr recvfrom 2
1095calls, using a
1096.Dv struct sockaddr_ng
1097socket address.
1098.Pp
1099.It HOLE
1100Responds only to generic messages and is a
1101.Dq black hole
1102for data, Useful for testing. Always accepts new hooks.
1103.Pp
1104.It ECHO
1105Responds only to generic messages and always echoes data back through the
1106hook from which it arrived. Returns any non generic messages as their
1107own response. Useful for testing. Always accepts new hooks.
1108.Pp
1109.It TEE
1110This node is useful for
1111.Dq snooping .
1112It has 4 hooks:
1113.Dv left ,
1114.Dv right ,
1115.Dv left2right ,
1116and
1117.Dv right2left .
1118Data entering from the right is passed to the left and duplicated on
1119.Dv right2left,
1120and data entering from the left is passed to the right and
1121duplicated on
1122.Dv left2right .
1123Data entering from
1124.Dv left2right
1125is sent to the right and data from
1126.Dv right2left
1127to left.
1128.Pp
1129.It RFC1490 MUX
1130Encapsulates/de-encapsulates frames encoded according to RFC 1490.
1131Has a hook for the encapsulated packets
1132.Pq Dq downstream
1133and one hook
1134for each protocol (i.e., IP, PPP, etc.).
1135.Pp
1136.It FRAME RELAY MUX
1137Encapsulates/de-encapsulates Frame Relay frames.
1138Has a hook for the encapsulated packets
1139.Pq Dq downstream
1140and one hook
1141for each DLCI.
1142.Pp
1143.It FRAME RELAY LMI
1144Automatically handles frame relay
1145.Dq LMI
1146(link management interface) operations and packets.
1147Automatically probes and detects which of several LMI standards
1148is in use at the exchange.
1149.Pp
1150.It TTY
1151This node is also a line discipline. It simply converts between mbuf
1152frames and sequential serial data, allowing a tty to appear as a netgraph
1153node. It has a programmable
1154.Dq hotkey
1155character.
1156.Pp
1157.It ASYNC
1158This node encapsulates and de-encapsulates asynchronous frames
1159according to RFC 1662. This is used in conjunction with the TTY node
1160type for supporting PPP links over asynchronous serial lines.
1161.Pp
1162.It INTERFACE
1163This node is also a system networking interface. It has hooks representing
1164each protocol family (IP, AppleTalk, IPX, etc.) and appears in the output of
1165.Xr ifconfig 8 .
1166The interfaces are named
1167.Em ng0 ,
1168.Em ng1 ,
1169etc.
1170.It ONE2MANY
1171This node implements a simple round-robin multiplexer. It can be used
1172for example to make several LAN ports act together to get a higher speed
1173link between two machines.
1174.It Various PPP related nodes.
1175There is a full multilink PPP implementation that runs in Netgraph.
1176The
1177.Em Mpd
1178port can use these modules to make a very low latency high
1179capacity ppp system. It also supports
1180.Em PPTP
1181vpns using the
1182.Em PPTP
1183node.
1184.It PPPOE
1185A server and client side implememtation of PPPoE. Used in conjunction with
1186either
1187.Xr ppp 8
1188or the
1189.Em mpd port.
1190.It BRIDGE
1191This node, togther with the ethernet nodes allows a very flexible
1192bridging system to be implemented.
1193.It KSOCKET
1194This intriguing node looks like a socket to the system but diverts
1195all data to and from the netgraph system for further processing. This allows
1196such things as UDP tunnels to be almost trivially implemented from the
1197command line.
1198.El
1199.Pp
1200Refer to the section at the end of this man page for more nodes types.
1201.Sh NOTES
1202Whether a named node exists can be checked by trying to send a control message
1203to it (e.g.,
1204.Dv NGM_NODEINFO
1205).
1206If it does not exist,
1207.Er ENOENT
1208will be returned.
1209.Pp
1210All data messages are mbuf chains with the M_PKTHDR flag set.
1211.Pp
1212Nodes are responsible for freeing what they allocate.
1213There are three exceptions:
1214.Bl -tag -width xxxx
1215.It 1
1216Mbufs sent across a data link are never to be freed by the sender. In the
1217case of error, they should be considered freed.
1218.It 2
1219Any meta-data information traveling with the data has the same restriction.
1220It might be freed by any node the data passes through, and a
1221.Dv NULL
1222passed onwards, but the caller will never free it.
1223Two macros
1224.Fn NG_FREE_META "meta"
1225and
1226.Fn NG_FREE_M "m"
1227should be used if possible to free data and meta data (see
1228.Pa netgraph.h ) .
1229.It 3
1230Messages sent using
1231.Fn ng_send_message
1232are freed by the recipient. As in the case above, the addresses
1233associated with the message are freed by whatever allocated them so the
1234recipient should copy them if it wants to keep that information.
1235.It 4
1236Both control mesages and data are delivered and queued with
1237a netgraph
1238.Em item .
1239The item must be freed using
1240.Fn NG_FREE_ITEM "item"
1241or passed on to another node.
1242.El
1243.Sh FILES
1244.Bl -tag -width xxxxx -compact
1245.It Pa /sys/netgraph/netgraph.h
1246Definitions for use solely within the kernel by
1247.Nm
1248nodes.
1249.It Pa /sys/netgraph/ng_message.h
1250Definitions needed by any file that needs to deal with
1251.Nm
1252messages.
1253.It Pa /sys/netgraph/ng_socket.h
1254Definitions needed to use
1255.Nm
1256socket type nodes.
1257.It Pa /sys/netgraph/ng_{type}.h
1258Definitions needed to use
1259.Nm
1260{type}
1261nodes, including the type cookie definition.
1262.It Pa /modules/netgraph.ko
1263Netgraph subsystem loadable KLD module.
1264.It Pa /modules/ng_{type}.ko
1265Loadable KLD module for node type {type}.
1266.It Pa /sys/netgraph/ng_sample.c
1267Skeleton netgraph node.
1268Use this as a starting point for new node types.
1269.El
1270.Sh USER MODE SUPPORT
1271There is a library for supporting user-mode programs that wish
1272to interact with the netgraph system. See
1273.Xr netgraph 3
1274for details.
1275.Pp
1276Two user-mode support programs,
1277.Xr ngctl 8
1278and
1279.Xr nghook 8 ,
1280are available to assist manual configuration and debugging.
1281.Pp
1282There are a few useful techniques for debugging new node types.
1283First, implementing new node types in user-mode first
1284makes debugging easier.
1285The
1286.Em tee
1287node type is also useful for debugging, especially in conjunction with
1288.Xr ngctl 8
1289and
1290.Xr nghook 8 .
1291.Pp
1292Also look in /usr/share/examples/netgraph for solutions to several
1293common networking problems, solved using
1294.Nm .
1295.Sh SEE ALSO
1296.Xr socket 2 ,
1297.Xr netgraph 3 ,
1298.Xr ng_async 4 ,
1299.Xr ng_bridge 4 ,
1300.Xr ng_bpf 4 ,
1301.Xr ng_cisco 4 ,
1302.Xr ng_ether 4 ,
1303.Xr ng_echo 4 ,
1304.Xr ng_ether 4 ,
1305.Xr ng_frame_relay 4 ,
1306.Xr ng_hole 4 ,
1307.Xr ng_iface 4 ,
1308.Xr ng_ksocket 4 ,
1309.Xr ng_lmi 4 ,
1310.Xr ng_mppc 4 ,
1311.Xr ng_ppp 4 ,
1312.Xr ng_pppoe 4 ,
1313.Xr ng_pptpgre 4 ,
1314.Xr ng_rfc1490 4 ,
1315.Xr ng_socket 4 ,
1316.Xr ng_tee 4 ,
1317.Xr ng_tty 4 ,
1318.Xr ng_UI 4 ,
1319.Xr ng_vjc 4 ,
1320.Xr ng_{type} 4 ,
1321.Xr ngctl 8 ,
1322.Xr nghook 8
1323.Sh HISTORY
1324The
1325.Nm
1326system was designed and first implemented at Whistle Communications, Inc.
1327in a version of
1328.Fx 2.2
1329customized for the Whistle InterJet.
1330It first made its debut in the main tree in
1331.Fx 3.4 .
1332.Sh AUTHORS
1333.An -nosplit
1334.An Julian Elischer Aq julian@FreeBSD.org ,
1335with contributions by
1336.An Archie Cobbs Aq archie@FreeBSD.org .
735.El
736.Pp
737The maintenance of the names, reference counts, and linked list
738of hooks for each node is handled automatically by the
739.Nm
740subsystem.
741Typically a node's private info contains a back-pointer to the node or hook
742structure, which counts as a new reference that must be included
743in the reference count for the node. When the node constructor is called
744there is already a reference for this calculated in, so that
745when the node is destroyed, it should remember to do a
746.Fn NG_NODE_UNREF
747on the node.
748.Pp
749From a hook you can obtain the corresponding node, and from
750a node, it is possible to traverse all the active hooks.
751.Pp
752A current example of how to define a node can always be seen in
753.Em sys/netgraph/ng_sample.c
754and should be used as a starting point for new node writers.
755.El
756.Sh Netgraph Message Structure
757Control messages have the following structure:
758.Bd -literal
759#define NG_CMDSTRLEN 15 /* Max command string (16 with null) */
760
761struct ng_mesg {
762 struct ng_msghdr {
763 u_char version; /* Must equal NG_VERSION */
764 u_char spare; /* Pad to 2 bytes */
765 u_short arglen; /* Length of cmd/resp data */
766 u_long flags; /* Message status flags */
767 u_long token; /* Reply should have the same token */
768 u_long typecookie; /* Node type understanding this message */
769 u_long cmd; /* Command identifier */
770 u_char cmdstr[NG_CMDSTRLEN+1]; /* Cmd string (for debug) */
771 } header;
772 char data[0]; /* Start of cmd/resp data */
773};
774
775#define NG_ABI_VERSION 5 /* Netgraph kernel ABI version */
776#define NG_VERSION 4 /* Netgraph message version */
777#define NGF_ORIG 0x0000 /* Command */
778#define NGF_RESP 0x0001 /* Response */
779.Ed
780.Pp
781Control messages have the fixed header shown above, followed by a
782variable length data section which depends on the type cookie
783and the command. Each field is explained below:
784.Bl -tag -width xxx
785.It Dv version
786Indicates the version of the netgraph message protocol itself. The current version is
787.Dv NG_VERSION .
788.It Dv arglen
789This is the length of any extra arguments, which begin at
790.Dv data .
791.It Dv flags
792Indicates whether this is a command or a response control message.
793.It Dv token
794The
795.Dv token
796is a means by which a sender can match a reply message to the
797corresponding command message; the reply always has the same token.
798.Pp
799.It Dv typecookie
800The corresponding node type's unique 32-bit value.
801If a node doesn't recognize the type cookie it must reject the message
802by returning
803.Er EINVAL .
804.Pp
805Each type should have an include file that defines the commands,
806argument format, and cookie for its own messages.
807The typecookie
808insures that the same header file was included by both sender and
809receiver; when an incompatible change in the header file is made,
810the typecookie
811.Em must
812be changed.
813The de facto method for generating unique type cookies is to take the
814seconds from the epoch at the time the header file is written
815(i.e., the output of
816.Dv "date -u +'%s'" ) .
817.Pp
818There is a predefined typecookie
819.Dv NGM_GENERIC_COOKIE
820for the
821.Dq generic
822node type, and
823a corresponding set of generic messages which all nodes understand.
824The handling of these messages is automatic.
825.It Dv command
826The identifier for the message command. This is type specific,
827and is defined in the same header file as the typecookie.
828.It Dv cmdstr
829Room for a short human readable version of
830.Dq command
831(for debugging purposes only).
832.El
833.Pp
834Some modules may choose to implement messages from more than one
835of the header files and thus recognize more than one type cookie.
836.Sh Control Message ASCII Form
837Control messages are in binary format for efficiency. However, for
838debugging and human interface purposes, and if the node type supports
839it, control messages may be converted to and from an equivalent
840.Tn ASCII
841form. The
842.Tn ASCII
843form is similar to the binary form, with two exceptions:
844.Pp
845.Bl -tag -compact -width xxx
846.It o
847The
848.Dv cmdstr
849header field must contain the
850.Tn ASCII
851name of the command, corresponding to the
852.Dv cmd
853header field.
854.It o
855The
856.Dv args
857field contains a NUL-terminated
858.Tn ASCII
859string version of the message arguments.
860.El
861.Pp
862In general, the arguments field of a control messgage can be any
863arbitrary C data type. Netgraph includes parsing routines to support
864some pre-defined datatypes in
865.Tn ASCII
866with this simple syntax:
867.Pp
868.Bl -tag -compact -width xxx
869.It o
870Integer types are represented by base 8, 10, or 16 numbers.
871.It o
872Strings are enclosed in double quotes and respect the normal
873C language backslash escapes.
874.It o
875IP addresses have the obvious form.
876.It o
877Arrays are enclosed in square brackets, with the elements listed
878consecutively starting at index zero. An element may have an optional
879index and equals sign preceeding it. Whenever an element
880does not have an explicit index, the index is implicitly the previous
881element's index plus one.
882.It o
883Structures are enclosed in curly braces, and each field is specified
884in the form
885.Dq fieldname=value .
886.It o
887Any array element or structure field whose value is equal to its
888.Dq default value
889may be omitted. For integer types, the default value
890is usually zero; for string types, the empty string.
891.It o
892Array elements and structure fields may be specified in any order.
893.El
894.Pp
895Each node type may define its own arbitrary types by providing
896the necessary routines to parse and unparse.
897.Tn ASCII
898forms defined
899for a specific node type are documented in the documentation for
900that node type.
901.Sh Generic Control Messages
902There are a number of standard predefined messages that will work
903for any node, as they are supported directly by the framework itself.
904These are defined in
905.Pa ng_message.h
906along with the basic layout of messages and other similar information.
907.Bl -tag -width xxx
908.It Dv NGM_CONNECT
909Connect to another node, using the supplied hook names on either end.
910.It Dv NGM_MKPEER
911Construct a node of the given type and then connect to it using the
912supplied hook names.
913.It Dv NGM_SHUTDOWN
914The target node should disconnect from all its neighbours and shut down.
915Persistent nodes such as those representing physical hardware
916might not disappear from the node namespace, but only reset themselves.
917The node must disconnect all of its hooks.
918This may result in neighbors shutting themselves down, and possibly a
919cascading shutdown of the entire connected graph.
920.It Dv NGM_NAME
921Assign a name to a node. Nodes can exist without having a name, and this
922is the default for nodes created using the
923.Dv NGM_MKPEER
924method. Such nodes can only be addressed relatively or by their ID number.
925.It Dv NGM_RMHOOK
926Ask the node to break a hook connection to one of its neighbours.
927Both nodes will have their
928.Dq disconnect
929method invoked.
930Either node may elect to totally shut down as a result.
931.It Dv NGM_NODEINFO
932Asks the target node to describe itself. The four returned fields
933are the node name (if named), the node type, the node ID and the
934number of hooks attached. The ID is an internal number unique to that node.
935.It Dv NGM_LISTHOOKS
936This returns the information given by
937.Dv NGM_NODEINFO ,
938but in addition
939includes an array of fields describing each link, and the description for
940the node at the far end of that link.
941.It Dv NGM_LISTNAMES
942This returns an array of node descriptions (as for
943.Dv NGM_NODEINFO ")"
944where each entry of the array describes a named node.
945All named nodes will be described.
946.It Dv NGM_LISTNODES
947This is the same as
948.Dv NGM_LISTNAMES
949except that all nodes are listed regardless of whether they have a name or not.
950.It Dv NGM_LISTTYPES
951This returns a list of all currently installed netgraph types.
952.It Dv NGM_TEXT_STATUS
953The node may return a text formatted status message.
954The status information is determined entirely by the node type.
955It is the only "generic" message
956that requires any support within the node itself and as such the node may
957elect to not support this message. The text response must be less than
958.Dv NG_TEXTRESPONSE
959bytes in length (presently 1024). This can be used to return general
960status information in human readable form.
961.It Dv NGM_BINARY2ASCII
962This message converts a binary control message to its
963.Tn ASCII
964form.
965The entire control message to be converted is contained within the
966arguments field of the
967.Dv Dv NGM_BINARY2ASCII
968message itself. If successful, the reply will contain the same control
969message in
970.Tn ASCII
971form.
972A node will typically only know how to translate messages that it
973itself understands, so the target node of the
974.Dv NGM_BINARY2ASCII
975is often the same node that would actually receive that message.
976.It Dv NGM_ASCII2BINARY
977The opposite of
978.Dv NGM_BINARY2ASCII .
979The entire control message to be converted, in
980.Tn ASCII
981form, is contained
982in the arguments section of the
983.Dv NGM_ASCII2BINARY
984and need only have the
985.Dv flags ,
986.Dv cmdstr ,
987and
988.Dv arglen
989header fields filled in, plus the NUL-terminated string version of
990the arguments in the arguments field. If successful, the reply
991contains the binary version of the control message.
992.El
993.Sh Flow Control Messages
994In addition to the control messages that affect nodes with respect to the
995graph, there are also a number of
996.Em Flow-control
997messages defined. At present these are
998.Em NOT
999handled automatically by the system, so
1000nodes need to handle them if they are going to be used in a graph utilising
1001flow control, and will be in the likely path of these messages. The
1002default action of a node that doesn't understand these messages should
1003be to pass them onto the next node. Hopefully some helper functions
1004will assist in this eventually. These messages are also defined in
1005.Pa sys/netgraph/ng_message.h
1006and have a separate cookie
1007.Em NG_FLOW_COOKIE
1008to help identify them. They will not be covered in depth here.
1009.Sh Metadata
1010Data moving through the
1011.Nm
1012system can be accompanied by meta-data that describes some
1013aspect of that data. The form of the meta-data is a fixed header,
1014which contains enough information for most uses, and can optionally
1015be supplemented by trailing
1016.Em option
1017structures, which contain a
1018.Em cookie
1019(see the section on control messages), an identifier, a length and optional
1020data. If a node does not recognize the cookie associated with an option,
1021it should ignore that option.
1022.Pp
1023Meta data might include such things as priority, discard eligibility,
1024or special processing requirements. It might also mark a packet for
1025debug status, etc. The use of meta-data is still experimental.
1026.Sh INITIALIZATION
1027The base
1028.Nm
1029code may either be statically compiled
1030into the kernel or else loaded dynamically as a KLD via
1031.Xr kldload 8 .
1032In the former case, include
1033.Pp
1034.Dl options NETGRAPH
1035.Pp
1036in your kernel configuration file. You may also include selected
1037node types in the kernel compilation, for example:
1038.Bd -literal -offset indent
1039options NETGRAPH
1040options NETGRAPH_SOCKET
1041options NETGRAPH_ECHO
1042.Ed
1043.Pp
1044Once the
1045.Nm
1046subsystem is loaded, individual node types may be loaded at any time
1047as KLD modules via
1048.Xr kldload 8 .
1049Moreover,
1050.Nm
1051knows how to automatically do this; when a request to create a new
1052node of unknown type
1053.Em type
1054is made,
1055.Nm
1056will attempt to load the KLD module
1057.Pa ng_type.ko .
1058.Pp
1059Types can also be installed at boot time, as certain device drivers
1060may want to export each instance of the device as a netgraph node.
1061.Pp
1062In general, new types can be installed at any time from within the
1063kernel by calling
1064.Fn ng_newtype ,
1065supplying a pointer to the type's
1066.Dv struct ng_type
1067structure.
1068.Pp
1069The
1070.Fn NETGRAPH_INIT
1071macro automates this process by using a linker set.
1072.Sh EXISTING NODE TYPES
1073Several node types currently exist. Each is fully documented
1074in its own man page:
1075.Bl -tag -width xxx
1076.It SOCKET
1077The socket type implements two new sockets in the new protocol domain
1078.Dv PF_NETGRAPH .
1079The new sockets protocols are
1080.Dv NG_DATA
1081and
1082.Dv NG_CONTROL ,
1083both of type
1084.Dv SOCK_DGRAM .
1085Typically one of each is associated with a socket node.
1086When both sockets have closed, the node will shut down. The
1087.Dv NG_DATA
1088socket is used for sending and receiving data, while the
1089.Dv NG_CONTROL
1090socket is used for sending and receiving control messages.
1091Data and control messages are passed using the
1092.Xr sendto 2
1093and
1094.Xr recvfrom 2
1095calls, using a
1096.Dv struct sockaddr_ng
1097socket address.
1098.Pp
1099.It HOLE
1100Responds only to generic messages and is a
1101.Dq black hole
1102for data, Useful for testing. Always accepts new hooks.
1103.Pp
1104.It ECHO
1105Responds only to generic messages and always echoes data back through the
1106hook from which it arrived. Returns any non generic messages as their
1107own response. Useful for testing. Always accepts new hooks.
1108.Pp
1109.It TEE
1110This node is useful for
1111.Dq snooping .
1112It has 4 hooks:
1113.Dv left ,
1114.Dv right ,
1115.Dv left2right ,
1116and
1117.Dv right2left .
1118Data entering from the right is passed to the left and duplicated on
1119.Dv right2left,
1120and data entering from the left is passed to the right and
1121duplicated on
1122.Dv left2right .
1123Data entering from
1124.Dv left2right
1125is sent to the right and data from
1126.Dv right2left
1127to left.
1128.Pp
1129.It RFC1490 MUX
1130Encapsulates/de-encapsulates frames encoded according to RFC 1490.
1131Has a hook for the encapsulated packets
1132.Pq Dq downstream
1133and one hook
1134for each protocol (i.e., IP, PPP, etc.).
1135.Pp
1136.It FRAME RELAY MUX
1137Encapsulates/de-encapsulates Frame Relay frames.
1138Has a hook for the encapsulated packets
1139.Pq Dq downstream
1140and one hook
1141for each DLCI.
1142.Pp
1143.It FRAME RELAY LMI
1144Automatically handles frame relay
1145.Dq LMI
1146(link management interface) operations and packets.
1147Automatically probes and detects which of several LMI standards
1148is in use at the exchange.
1149.Pp
1150.It TTY
1151This node is also a line discipline. It simply converts between mbuf
1152frames and sequential serial data, allowing a tty to appear as a netgraph
1153node. It has a programmable
1154.Dq hotkey
1155character.
1156.Pp
1157.It ASYNC
1158This node encapsulates and de-encapsulates asynchronous frames
1159according to RFC 1662. This is used in conjunction with the TTY node
1160type for supporting PPP links over asynchronous serial lines.
1161.Pp
1162.It INTERFACE
1163This node is also a system networking interface. It has hooks representing
1164each protocol family (IP, AppleTalk, IPX, etc.) and appears in the output of
1165.Xr ifconfig 8 .
1166The interfaces are named
1167.Em ng0 ,
1168.Em ng1 ,
1169etc.
1170.It ONE2MANY
1171This node implements a simple round-robin multiplexer. It can be used
1172for example to make several LAN ports act together to get a higher speed
1173link between two machines.
1174.It Various PPP related nodes.
1175There is a full multilink PPP implementation that runs in Netgraph.
1176The
1177.Em Mpd
1178port can use these modules to make a very low latency high
1179capacity ppp system. It also supports
1180.Em PPTP
1181vpns using the
1182.Em PPTP
1183node.
1184.It PPPOE
1185A server and client side implememtation of PPPoE. Used in conjunction with
1186either
1187.Xr ppp 8
1188or the
1189.Em mpd port.
1190.It BRIDGE
1191This node, togther with the ethernet nodes allows a very flexible
1192bridging system to be implemented.
1193.It KSOCKET
1194This intriguing node looks like a socket to the system but diverts
1195all data to and from the netgraph system for further processing. This allows
1196such things as UDP tunnels to be almost trivially implemented from the
1197command line.
1198.El
1199.Pp
1200Refer to the section at the end of this man page for more nodes types.
1201.Sh NOTES
1202Whether a named node exists can be checked by trying to send a control message
1203to it (e.g.,
1204.Dv NGM_NODEINFO
1205).
1206If it does not exist,
1207.Er ENOENT
1208will be returned.
1209.Pp
1210All data messages are mbuf chains with the M_PKTHDR flag set.
1211.Pp
1212Nodes are responsible for freeing what they allocate.
1213There are three exceptions:
1214.Bl -tag -width xxxx
1215.It 1
1216Mbufs sent across a data link are never to be freed by the sender. In the
1217case of error, they should be considered freed.
1218.It 2
1219Any meta-data information traveling with the data has the same restriction.
1220It might be freed by any node the data passes through, and a
1221.Dv NULL
1222passed onwards, but the caller will never free it.
1223Two macros
1224.Fn NG_FREE_META "meta"
1225and
1226.Fn NG_FREE_M "m"
1227should be used if possible to free data and meta data (see
1228.Pa netgraph.h ) .
1229.It 3
1230Messages sent using
1231.Fn ng_send_message
1232are freed by the recipient. As in the case above, the addresses
1233associated with the message are freed by whatever allocated them so the
1234recipient should copy them if it wants to keep that information.
1235.It 4
1236Both control mesages and data are delivered and queued with
1237a netgraph
1238.Em item .
1239The item must be freed using
1240.Fn NG_FREE_ITEM "item"
1241or passed on to another node.
1242.El
1243.Sh FILES
1244.Bl -tag -width xxxxx -compact
1245.It Pa /sys/netgraph/netgraph.h
1246Definitions for use solely within the kernel by
1247.Nm
1248nodes.
1249.It Pa /sys/netgraph/ng_message.h
1250Definitions needed by any file that needs to deal with
1251.Nm
1252messages.
1253.It Pa /sys/netgraph/ng_socket.h
1254Definitions needed to use
1255.Nm
1256socket type nodes.
1257.It Pa /sys/netgraph/ng_{type}.h
1258Definitions needed to use
1259.Nm
1260{type}
1261nodes, including the type cookie definition.
1262.It Pa /modules/netgraph.ko
1263Netgraph subsystem loadable KLD module.
1264.It Pa /modules/ng_{type}.ko
1265Loadable KLD module for node type {type}.
1266.It Pa /sys/netgraph/ng_sample.c
1267Skeleton netgraph node.
1268Use this as a starting point for new node types.
1269.El
1270.Sh USER MODE SUPPORT
1271There is a library for supporting user-mode programs that wish
1272to interact with the netgraph system. See
1273.Xr netgraph 3
1274for details.
1275.Pp
1276Two user-mode support programs,
1277.Xr ngctl 8
1278and
1279.Xr nghook 8 ,
1280are available to assist manual configuration and debugging.
1281.Pp
1282There are a few useful techniques for debugging new node types.
1283First, implementing new node types in user-mode first
1284makes debugging easier.
1285The
1286.Em tee
1287node type is also useful for debugging, especially in conjunction with
1288.Xr ngctl 8
1289and
1290.Xr nghook 8 .
1291.Pp
1292Also look in /usr/share/examples/netgraph for solutions to several
1293common networking problems, solved using
1294.Nm .
1295.Sh SEE ALSO
1296.Xr socket 2 ,
1297.Xr netgraph 3 ,
1298.Xr ng_async 4 ,
1299.Xr ng_bridge 4 ,
1300.Xr ng_bpf 4 ,
1301.Xr ng_cisco 4 ,
1302.Xr ng_ether 4 ,
1303.Xr ng_echo 4 ,
1304.Xr ng_ether 4 ,
1305.Xr ng_frame_relay 4 ,
1306.Xr ng_hole 4 ,
1307.Xr ng_iface 4 ,
1308.Xr ng_ksocket 4 ,
1309.Xr ng_lmi 4 ,
1310.Xr ng_mppc 4 ,
1311.Xr ng_ppp 4 ,
1312.Xr ng_pppoe 4 ,
1313.Xr ng_pptpgre 4 ,
1314.Xr ng_rfc1490 4 ,
1315.Xr ng_socket 4 ,
1316.Xr ng_tee 4 ,
1317.Xr ng_tty 4 ,
1318.Xr ng_UI 4 ,
1319.Xr ng_vjc 4 ,
1320.Xr ng_{type} 4 ,
1321.Xr ngctl 8 ,
1322.Xr nghook 8
1323.Sh HISTORY
1324The
1325.Nm
1326system was designed and first implemented at Whistle Communications, Inc.
1327in a version of
1328.Fx 2.2
1329customized for the Whistle InterJet.
1330It first made its debut in the main tree in
1331.Fx 3.4 .
1332.Sh AUTHORS
1333.An -nosplit
1334.An Julian Elischer Aq julian@FreeBSD.org ,
1335with contributions by
1336.An Archie Cobbs Aq archie@FreeBSD.org .