xref: /dragonfly/share/man/man4/netgraph.4 (revision 548a3528)
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: src/share/man/man4/netgraph.4,v 1.39.2.1 2001/12/21 09:00:50 ru Exp $
37.\" $Whistle: netgraph.4,v 1.7 1999/01/28 23:54:52 julian Exp $
38.\"
39.Dd September 2, 2008
40.Dt NETGRAPH 4
41.Os
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.
50The objects, known as
51.Em nodes ,
52can be arranged into arbitrarily complicated graphs.
53Nodes have
54.Em hooks
55which are used to connect two nodes together, forming the edges in the graph.
56Nodes communicate along the edges to process data, implement protocols, etc.
57.Pp
58The aim of
59.Nm
60is to supplement rather than replace the existing kernel networking
61infrastructure.
62It provides:
63.Pp
64.Bl -bullet -compact -offset 2n
65.It
66A flexible way of combining protocol and link level drivers
67.It
68A modular way to implement new protocols
69.It
70A common framework for kernel entities to inter-communicate
71.It
72A reasonably fast, kernel-based implementation
73.El
74.Sh Nodes and Types
75The most fundamental concept in
76.Nm
77is that of a
78.Em node .
79All nodes implement a number of predefined methods which allow them
80to interact with other nodes in a well defined manner.
81.Pp
82Each node has a
83.Em type ,
84which is a static property of the node determined at node creation time.
85A node's type is described by a unique
86.Tn ASCII
87type name.
88The type implies what the node does and how it may be connected
89to other nodes.
90.Pp
91In object-oriented language, types are classes and nodes are instances
92of their respective class.
93All node types are subclasses of the generic node
94type, and hence inherit certain common functionality and capabilities
95(e.g., the ability to have an
96.Tn ASCII
97name).
98.Pp
99Nodes may be assigned a globally unique
100.Tn ASCII
101name which can be
102used to refer to the node.
103The name must not contain the characters
104.Dq .\&
105or
106.Dq \&:
107and is limited to
108.Dv "NG_NODESIZ"
109characters (including NUL byte).
110.Pp
111Each node instance has a unique
112.Em ID number
113which is expressed as a 32-bit hex value.
114This value may be used to refer to a node when there is no
115.Tn ASCII
116name assigned to it.
117.Sh Hooks
118Nodes are connected to other nodes by connecting a pair of
119.Em hooks ,
120one from each node.
121Data flows bidirectionally between nodes along connected pairs of hooks.
122A node may have as many hooks as it needs,
123and may assign whatever meaning it wants to a hook.
124.Pp
125Hooks have these properties:
126.Pp
127.Bl -bullet -compact -offset 2n
128.It
129A hook has an
130.Tn ASCII
131name which is unique among all hooks
132on that node (other hooks on other nodes may have the same name).
133The name must not contain a
134.Dq .\&
135or a
136.Dq \&:
137and is
138limited to
139.Dv "NG_HOOKSIZ"
140characters (including NUL byte).
141.It
142A hook is always connected to another hook.
143That is, hooks are created at the time they are connected,
144and breaking an edge by removing either hook destroys both hooks.
145.El
146.Pp
147A node may decide to assign special meaning to some hooks.
148For example, connecting to the hook named
149.Dq debug
150might trigger
151the node to start sending debugging information to that hook.
152.Sh Data Flow
153Two types of information flow between nodes: data messages and
154control messages.
155Data messages are passed in mbuf chains along the edges
156in the graph, one edge at a time.
157The first mbuf in a chain must have the
158.Dv M_PKTHDR
159flag set.
160Each node decides how to handle data coming in on its hooks.
161.Pp
162Control messages are type-specific C structures sent from one node
163directly to some arbitrary other node.
164Control messages have a common header format,
165followed by type-specific data, and are binary structures
166for efficiency.
167However, node types also may support conversion of the
168type specific data between binary and
169.Tn ASCII
170for debugging and human interface purposes (see the
171.Dv NGM_ASCII2BINARY
172and
173.Dv NGM_BINARY2ASCII
174generic control messages below).
175Nodes are not required to support these conversions.
176.Pp
177There are two ways to address a control message.
178If there is a sequence of edges connecting the two nodes,
179the message may be
180.Dq source routed
181by specifying the corresponding sequence
182of hooks as the destination address for the message (relative
183addressing).
184Otherwise, the recipient node global
185.Tn ASCII
186name
187(or equivalent ID based name) is used as the destination address
188for the message (absolute addressing).
189The two types of addressing may be combined,
190by specifying an absolute start node and a sequence of hooks.
191.Pp
192Messages often represent commands that are followed by a reply message
193in the reverse direction.
194To facilitate this, the recipient of a
195control message is supplied with a
196.Dq return address
197that is suitable for addressing a reply.
198.Pp
199Each control message contains a 32 bit value called a
200.Em typecookie
201indicating the type of the message, i.e. how to interpret it.
202Typically each type defines a unique typecookie for the messages
203that it understands.
204However, a node may choose to recognize and
205implement more than one type of message.
206.Sh Netgraph is Functional
207In order to minimize latency, most
208.Nm
209operations are functional.
210That is, data and control messages are delivered by making function
211calls rather than by using queues and mailboxes.
212For example, if node A wishes to send a data mbuf to neighboring node B,
213it calls the generic
214.Nm
215data delivery function.
216This function in turn locates node B and calls B's
217.Dq receive data
218method.
219While this mode of operation results in good performance,
220it has a few implications for node developers:
221.Pp
222.Bl -bullet -compact -offset 2n
223.It
224Whenever a node delivers a data or control message, the node
225may need to allow for the possibility of receiving a returning
226message before the original delivery function call returns.
227.It
228Netgraph nodes and support routines generally run inside critical
229sections.
230However, some nodes may want to send data and control messages
231from a different priority level.
232Netgraph supplies queueing routines which utilize the NETISR system to
233move message delivery inside a critical section.
234Note that messages are always received from inside a critical section.
235.It
236It's possible for an infinite loop to occur if the graph contains cycles.
237.El
238.Pp
239So far, these issues have not proven problematical in practice.
240.Sh Interaction With Other Parts of the Kernel
241A node may have a hidden interaction with other components of the
242kernel outside of the
243.Nm
244subsystem, such as device hardware, kernel protocol stacks, etc.
245In fact, one of the benefits of
246.Nm
247is the ability to join disparate kernel networking entities together in a
248consistent communication framework.
249.Pp
250An example is the node type
251.Em socket
252which is both a netgraph node and a
253.Xr socket 2
254.Bx
255socket in the protocol family
256.Dv PF_NETGRAPH .
257Socket nodes allow user processes to participate in
258.Nm .
259Other nodes communicate with socket nodes using the usual methods, and the
260node hides the fact that it is also passing information to and from a
261cooperating user process.
262.Pp
263Another example is a device driver that presents
264a node interface to the hardware.
265.Sh Node Methods
266Nodes are notified of the following actions via function calls
267to the following node methods (all from inside critical sections)
268and may accept or reject that action (by returning the appropriate
269error code):
270.Bl -tag -width xxx
271.It Creation of a new node
272The constructor for the type is called.
273If creation of a new node is allowed,
274the constructor must call the generic node creation
275function (in object-oriented terms, the superclass constructor)
276and then allocate any special resources it needs.
277For nodes that correspond to hardware, this is typically done during
278the device attach routine.
279Often a global
280.Tn ASCII
281name corresponding to the
282device name is assigned here as well.
283.It Creation of a new hook
284The hook is created and tentatively
285linked to the node, and the node is told about the name that will be
286used to describe this hook.
287The node sets up any special data structures it needs,
288or may reject the connection, based on the name of the hook.
289.It Successful connection of two hooks
290After both ends have accepted their
291hooks, and the links have been made, the nodes get a chance to
292find out who their peer is across the link and can then decide to reject
293the connection.
294Tear-down is automatic.
295.It Destruction of a hook
296The node is notified of a broken connection.
297The node may consider some hooks to be critical to operation and others
298to be expendable: the disconnection of one hook may be an acceptable
299event while for another it may affect a total shutdown for the node.
300.It Shutdown of a node
301This method allows a node to clean up
302and to ensure that any actions that need to be performed
303at this time are taken.
304The method must call the generic (i.e. superclass)
305node destructor to get rid of the generic components of the node.
306Some nodes (usually associated with a piece of hardware) may be
307.Em persistent
308in that a shutdown breaks all edges and resets the node,
309but doesn't remove it, in which case the generic destructor is not called.
310.El
311.Sh Sending and Receiving Data
312Three other methods are also supported by all nodes:
313.Bl -tag -width xxx
314.It Receive data message
315An mbuf chain is passed to the node.
316The node is notified on which hook the data arrived,
317and can use this information in its processing decision.
318The node must always
319.Fn m_freem
320the mbuf chain on completion or error, or pass it on to another node
321(or kernel module) which will then be responsible for freeing it.
322.Pp
323In addition to the mbuf chain itself there is also a pointer to a
324structure describing meta-data about the message
325(e.g. priority information).
326This pointer may be
327.Dv NULL
328if there is no additional information.
329The format for this information is described in
330.In netgraph/netgraph.h .
331The memory for meta-data must allocated via
332.Fn malloc
333with type
334.Dv M_NETGRAPH .
335As with the data itself, it is the receiver's responsibility to
336.Fn free
337the meta-data.
338If the mbuf chain is freed the meta-data must be freed at the same time.
339If the meta-data is freed but the real data on is passed on, then a
340.Dv NULL
341pointer must be substituted.
342.Pp
343The receiving node may decide to defer the data by queueing it in the
344.Nm
345NETISR system (see below).
346.Pp
347The structure and use of meta-data is still experimental, but is
348presently used in frame-relay to indicate that management packets
349should be queued for transmission
350at a higher priority than data packets.
351This is required for conformance with Frame Relay standards.
352.It Receive queued data message
353Usually this will be the same function as
354.Em Receive data message.
355This is the entry point called when a data message is being handed to
356the node after having been queued in the NETISR system.
357This allows a node to decide in the
358.Em Receive data message
359method that a message should be deferred and queued,
360and be sure that when it is processed from the queue,
361it will not be queued again.
362.It Receive control message
363This method is called when a control message is addressed to the node.
364A return address is always supplied, giving the address of the node
365that originated the message so a reply message can be sent anytime later.
366.Pp
367It is possible for a synchronous reply to be made, and in fact this
368is more common in practice.
369This is done by setting a pointer (supplied as an extra function parameter)
370to point to the reply.
371Then when the control message delivery function returns,
372the caller can check if this pointer has been made non-NULL,
373and if so then it points to the reply message allocated via
374.Fn malloc
375and containing the synchronous response.
376In both directions, (request and response) it is up to the
377receiver of that message to
378.Fn free
379the control message buffer.
380All control messages and replies are allocated with
381.Fn malloc
382type
383.Dv M_NETGRAPH .
384.El
385.Pp
386Much use has been made of reference counts, so that nodes being
387free'd of all references are automatically freed, and this behaviour
388has been tested and debugged to present a consistent and trustworthy
389framework for the
390.Dq type module
391writer to use.
392.Sh Addressing
393The
394.Nm
395framework provides an unambiguous and simple to use method of specifically
396addressing any single node in the graph.
397The naming of a node is independent of its type, in that another node,
398or external component need not know anything about the node's type in
399order to address it so as to send it a generic message type.
400Node and hook names should be chosen so as to make addresses meaningful.
401.Pp
402Addresses are either absolute or relative.
403An absolute address begins with a node name (or ID), followed by a colon,
404followed by a sequence of hook names separated by periods.
405This addresses the node reached by starting
406at the named node and following the specified sequence of hooks.
407A relative address includes only the sequence of hook names, implicitly
408starting hook traversal at the local node.
409.Pp
410There are a couple of special possibilities for the node name.
411The name
412.Dq .\&
413(referred to as
414.Dq \&.: )
415always refers to the local node.
416Also, nodes that have no global name may be addressed by their ID numbers,
417by enclosing the hex representation of the ID number within square brackets.
418Here are some examples of valid netgraph addresses:
419.Bd -literal -offset 4n -compact
420
421  .:
422  foo:
423  .:hook1
424  foo:hook1.hook2
425  [f057cd80]:hook1
426.Ed
427.Pp
428Consider the following set of nodes might be created for a site with
429a single physical frame relay line having two active logical DLCI channels,
430with RFC 1490 frames on DLCI 16 and PPP frames over DLCI 20:
431.Bd -literal
432[type SYNC ]                  [type FRAME]                 [type RFC1490]
433[ "Frame1" ](uplink)<-->(data)[<un-named>](dlci16)<-->(mux)[<un-named>  ]
434[    A     ]                  [    B     ](dlci20)<---+    [     C      ]
435                                                      |
436                                                      |      [ type PPP ]
437                                                      +>(mux)[<un-named>]
438                                                             [    D     ]
439.Ed
440.Pp
441One could always send a control message to node C from anywhere
442by using the name
443.Em "Frame1:uplink.dlci16" .
444Similarly,
445.Em "Frame1:uplink.dlci20"
446could reliably be used to reach node D, and node A could refer
447to node B as
448.Em ".:uplink" ,
449or simply
450.Em "uplink" .
451Conversely, B can refer to A as
452.Em "data" .
453The address
454.Em "mux.data"
455could be used by both nodes C and D to address a message to node A.
456.Pp
457Note that this is only for
458.Em control messages .
459Data messages are routed one hop at a time, by specifying the departing
460hook, with each node making the next routing decision.
461So when B receives a frame on hook
462.Em data
463it decodes the frame relay header to determine the DLCI,
464and then forwards the unwrapped frame to either C or D.
465.Sh Netgraph Structures
466Interesting members of the node and hook structures are shown below:
467.Bd -literal
468struct  ng_node {
469  char    *name;                /* Optional globally unique name */
470  void    *private;             /* Node implementation private info */
471  struct  ng_type *type;        /* The type of this node */
472  int     refs;                 /* Number of references to this struct */
473  int     numhooks;             /* Number of connected hooks */
474  hook_p  hooks;                /* Linked list of (connected) hooks */
475};
476typedef struct ng_node *node_p;
477
478struct  ng_hook {
479  char           *name;         /* This node's name for this hook */
480  void           *private;      /* Node implementation private info */
481  int            refs;          /* Number of references to this struct */
482  struct ng_node *node;         /* The node this hook is attached to */
483  struct ng_hook *peer;         /* The other hook in this connected pair */
484  struct ng_hook *next;         /* Next in list of hooks for this node */
485};
486typedef struct ng_hook *hook_p;
487.Ed
488.Pp
489The maintenance of the name pointers, reference counts, and linked list
490of hooks for each node is handled automatically by the
491.Nm
492subsystem.
493Typically a node's private info contains a back-pointer to the node or hook
494structure, which counts as a new reference that must be registered by
495incrementing
496.Dv "node->refs" .
497.Pp
498From a hook you can obtain the corresponding node, and from
499a node the list of all active hooks.
500.Pp
501Node types are described by these structures:
502.Bd -literal
503/** How to convert a control message from binary <-> ASCII */
504struct ng_cmdlist {
505  u_int32_t                  cookie;     /* typecookie */
506  int                        cmd;        /* command number */
507  const char                 *name;      /* command name */
508  const struct ng_parse_type *mesgType;  /* args if !NGF_RESP */
509  const struct ng_parse_type *respType;  /* args if NGF_RESP */
510};
511
512struct ng_type {
513  u_int32_t version;                    /* Must equal NG_VERSION */
514  const  char *name;                    /* Unique type name */
515
516  /* Module event handler */
517  modeventhand_t  mod_event;            /* Handle load/unload (optional) */
518
519  /* Constructor */
520  int    (*constructor)(node_p *node);  /* Create a new node */
521
522  /** Methods using the node **/
523  int    (*rcvmsg)(node_p node,         /* Receive control message */
524            struct ng_mesg *msg,                /* The message */
525            const char *retaddr,                /* Return address */
526            struct ng_mesg **resp);             /* Synchronous response */
527  int    (*shutdown)(node_p node);      /* Shutdown this node */
528  int    (*newhook)(node_p node,        /* create a new hook */
529            hook_p hook,                        /* Pre-allocated struct */
530            const char *name);                  /* Name for new hook */
531
532  /** Methods using the hook **/
533  int    (*connect)(hook_p hook);       /* Confirm new hook attachment */
534  int    (*rcvdata)(hook_p hook,        /* Receive data on a hook */
535            struct mbuf *m,                     /* The data in an mbuf */
536            meta_p meta);                       /* Meta-data, if any */
537  int    (*disconnect)(hook_p hook);    /* Notify disconnection of hook */
538
539  /** How to convert control messages binary <-> ASCII */
540  const struct ng_cmdlist *cmdlist; 	/* Optional; may be NULL */
541};
542.Ed
543.Pp
544Control messages have the following structure:
545.Bd -literal
546#define NG_CMDSTRSIZ    16      /* Max command string (including null) */
547
548struct ng_mesg {
549  struct ng_msghdr {
550    u_char      version;        /* Must equal NG_VERSION */
551    u_char      spare;          /* Pad to 2 bytes */
552    u_short     arglen;         /* Length of cmd/resp data */
553    u_long      flags;          /* Message status flags */
554    u_long      token;          /* Reply should have the same token */
555    u_long      typecookie;     /* Node type understanding this message */
556    u_long      cmd;            /* Command identifier */
557    u_char      cmdstr[NG_CMDSTRSIZ]; /* Cmd string (for debug) */
558  } header;
559  char  data[0];                /* Start of cmd/resp data */
560};
561
562#define NG_VERSION      1               /* Netgraph version */
563#define NGF_ORIG        0x0000          /* Command */
564#define NGF_RESP        0x0001          /* Response */
565.Ed
566.Pp
567Control messages have the fixed header shown above, followed by a
568variable length data section which depends on the type cookie
569and the command.
570Each field is explained below:
571.Bl -tag -width xxx
572.It Dv version
573Indicates the version of netgraph itself.
574The current version is
575.Dv NG_VERSION .
576.It Dv arglen
577This is the length of any extra arguments, which begin at
578.Dv data .
579.It Dv flags
580Indicates whether this is a command or a response control message.
581.It Dv token
582The
583.Dv token
584is a means by which a sender can match a reply message to the
585corresponding command message; the reply always has the same token.
586.It Dv typecookie
587The corresponding node type's unique 32-bit value.
588If a node doesn't recognize the type cookie it must reject the message
589by returning
590.Er EINVAL .
591.Pp
592Each type should have an include file that defines the commands,
593argument format, and cookie for its own messages.
594The typecookie
595insures that the same header file was included by both sender and
596receiver; when an incompatible change in the header file is made,
597the typecookie
598.Em must
599be changed.
600The de facto method for generating unique type cookies is to take the
601seconds from the epoch at the time the header file is written
602(i.e. the output of
603.Dv "date -u +'%s'" ) .
604.Pp
605There is a predefined typecookie
606.Dv NGM_GENERIC_COOKIE
607for the
608.Dq generic
609node type, and
610a corresponding set of generic messages which all nodes understand.
611The handling of these messages is automatic.
612.It Dv command
613The identifier for the message command.
614This is type specific,
615and is defined in the same header file as the typecookie.
616.It Dv cmdstr
617Room for a short human readable version of
618.Dq command
619(for debugging purposes only).
620.El
621.Pp
622Some modules may choose to implement messages from more than one
623of the header files and thus recognize more than one type cookie.
624.Sh Control Message ASCII Form
625Control messages are in binary format for efficiency.
626However, for debugging and human interface purposes,
627and if the node type supports it,
628control messages may be converted to and from an equivalent
629.Tn ASCII
630form.
631The
632.Tn ASCII
633form is similar to the binary form, with two exceptions:
634.Pp
635.Bl -tag -compact -width xxx
636.It o
637The
638.Dv cmdstr
639header field must contain the
640.Tn ASCII
641name of the command, corresponding to the
642.Dv cmd
643header field.
644.It o
645The
646.Dv args
647field contains a NUL-terminated
648.Tn ASCII
649string version of the message arguments.
650.El
651.Pp
652In general, the arguments field of a control message can be any
653arbitrary C data type.
654Netgraph includes parsing routines to support
655some pre-defined datatypes in
656.Tn ASCII
657with this simple syntax:
658.Pp
659.Bl -tag -compact -width xxx
660.It o
661Integer types are represented by base 8, 10, or 16 numbers.
662.It o
663Strings are enclosed in double quotes and respect the normal
664C language backslash escapes.
665.It o
666IP addresses have the obvious form.
667.It o
668Arrays are enclosed in square brackets, with the elements listed
669consecutively starting at index zero.
670An element may have an optional index and equals sign preceding it.
671Whenever an element does not have an explicit index, the index is
672implicitly the previous element's index plus one.
673.It o
674Structures are enclosed in curly braces, and each field is specified
675in the form
676.Dq fieldname=value .
677.It o
678Any array element or structure field whose value is equal to its
679.Dq default value
680may be omitted.
681For integer types, the default value is usually zero;
682for string types, the empty string.
683.It o
684Array elements and structure fields may be specified in any order.
685.El
686.Pp
687Each node type may define its own arbitrary types by providing
688the necessary routines to parse and unparse.
689.Tn ASCII
690forms defined
691for a specific node type are documented in the documentation for
692that node type.
693.Sh Generic Control Messages
694There are a number of standard predefined messages that will work
695for any node, as they are supported directly by the framework itself.
696These are defined in
697.In netgraph/ng_message.h
698along with the basic layout of messages and other similar information.
699.Bl -tag -width xxx
700.It Dv NGM_CONNECT
701Connect to another node, using the supplied hook names on either end.
702.It Dv NGM_MKPEER
703Construct a node of the given type and then connect to it using the
704supplied hook names.
705.It Dv NGM_SHUTDOWN
706The target node should disconnect from all its neighbours and shut down.
707Persistent nodes such as those representing physical hardware
708might not disappear from the node namespace, but only reset themselves.
709The node must disconnect all of its hooks.
710This may result in neighbors shutting themselves down, and possibly a
711cascading shutdown of the entire connected graph.
712.It Dv NGM_NAME
713Assign a name to a node.
714Nodes can exist without having a name, and this
715is the default for nodes created using the
716.Dv NGM_MKPEER
717method.
718Such nodes can only be addressed relatively or by their ID number.
719.It Dv NGM_RMHOOK
720Ask the node to break a hook connection to one of its neighbours.
721Both nodes will have their
722.Dq disconnect
723method invoked.
724Either node may elect to totally shut down as a result.
725.It Dv NGM_NODEINFO
726Asks the target node to describe itself.
727The four returned fields are the node name (if named), the node type,
728the node ID and the number of hooks attached.
729The ID is an internal number unique to that node.
730.It Dv NGM_LISTHOOKS
731This returns the information given by
732.Dv NGM_NODEINFO ,
733but in addition
734includes an array of fields describing each link, and the description for
735the node at the far end of that link.
736.It Dv NGM_LISTNAMES
737This returns an array of node descriptions (as for
738.Dv NGM_NODEINFO ")"
739where each entry of the array describes a named node.
740All named nodes will be described.
741.It Dv NGM_LISTNODES
742This is the same as
743.Dv NGM_LISTNAMES
744except that all nodes are listed regardless of whether they have a name or not.
745.It Dv NGM_LISTTYPES
746This returns a list of all currently installed netgraph types.
747.It Dv NGM_TEXT_STATUS
748The node may return a text formatted status message.
749The status information is determined entirely by the node type.
750It is the only "generic" message
751that requires any support within the node itself and as such the node may
752elect to not support this message.
753The text response must be less than
754.Dv NG_TEXTRESPONSE
755bytes in length (presently 1024).
756This can be used to return general
757status information in human readable form.
758.It Dv NGM_BINARY2ASCII
759This message converts a binary control message to its
760.Tn ASCII
761form.
762The entire control message to be converted is contained within the
763arguments field of the
764.Dv NGM_BINARY2ASCII
765message itself.
766If successful, the reply will contain the same control message in
767.Tn ASCII
768form.
769A node will typically only know how to translate messages that it
770itself understands, so the target node of the
771.Dv NGM_BINARY2ASCII
772is often the same node that would actually receive that message.
773.It Dv NGM_ASCII2BINARY
774The opposite of
775.Dv NGM_BINARY2ASCII .
776The entire control message to be converted, in
777.Tn ASCII
778form, is contained
779in the arguments section of the
780.Dv NGM_ASCII2BINARY
781and need only have the
782.Dv flags ,
783.Dv cmdstr ,
784and
785.Dv arglen
786header fields filled in, plus the NUL-terminated string version of
787the arguments in the arguments field.
788If successful, the reply
789contains the binary version of the control message.
790.El
791.Sh Metadata
792Data moving through the
793.Nm
794system can be accompanied by meta-data that describes some
795aspect of that data.
796The form of the meta-data is a fixed header,
797which contains enough information for most uses, and can optionally
798be supplemented by trailing
799.Em option
800structures, which contain a
801.Em cookie
802(see the section on control messages), an identifier, a length and optional
803data.
804If a node does not recognize the cookie associated with an option,
805it should ignore that option.
806.Pp
807Meta data might include such things as priority, discard eligibility,
808or special processing requirements.
809It might also mark a packet for debug status, etc.
810The use of meta-data is still experimental.
811.Sh INITIALIZATION
812The base
813.Nm
814code may either be statically compiled
815into the kernel or else loaded dynamically as a KLD via
816.Xr kldload 8 .
817In the former case, include
818.Pp
819.D1 Cd options NETGRAPH
820.Pp
821in your kernel configuration file.
822You may also include selected
823node types in the kernel compilation, for example:
824.Bd -unfilled -offset indent
825.Cd options NETGRAPH
826.Cd options NETGRAPH_SOCKET
827.Cd options NETGRAPH_ECHO
828.Ed
829.Pp
830Once the
831.Nm
832subsystem is loaded, individual node types may be loaded at any time
833as KLD modules via
834.Xr kldload 8 .
835Moreover,
836.Nm
837knows how to automatically do this; when a request to create a new
838node of unknown type
839.Em type
840is made,
841.Nm
842will attempt to load the KLD module
843.Pa ng_type.ko .
844.Pp
845Types can also be installed at boot time, as certain device drivers
846may want to export each instance of the device as a netgraph node.
847.Pp
848In general, new types can be installed at any time from within the
849kernel by calling
850.Fn ng_newtype ,
851supplying a pointer to the type's
852.Dv struct ng_type
853structure.
854.Pp
855The
856.Fn NETGRAPH_INIT
857macro automates this process by using a linker set.
858.Sh EXISTING NODE TYPES
859Several node types currently exist.
860Each is fully documented in its own man page:
861.Bl -tag -width xxx
862.It SOCKET
863The socket type implements two new sockets in the new protocol domain
864.Dv PF_NETGRAPH .
865The new sockets protocols are
866.Dv NG_DATA
867and
868.Dv NG_CONTROL ,
869both of type
870.Dv SOCK_DGRAM .
871Typically one of each is associated with a socket node.
872When both sockets have closed, the node will shut down.
873The
874.Dv NG_DATA
875socket is used for sending and receiving data, while the
876.Dv NG_CONTROL
877socket is used for sending and receiving control messages.
878Data and control messages are passed using the
879.Xr sendto 2
880and
881.Xr recvfrom 2
882calls, using a
883.Dv struct sockaddr_ng
884socket address.
885.It HOLE
886Responds only to generic messages and is a
887.Dq black hole
888for data, Useful for testing.
889Always accepts new hooks.
890.It ECHO
891Responds only to generic messages and always echoes data back through the
892hook from which it arrived.
893Returns any non generic messages as their own response.
894Useful for testing.
895Always accepts new hooks.
896.It TEE
897This node is useful for
898.Dq snooping .
899It has 4 hooks:
900.Dv left ,
901.Dv right ,
902.Dv left2right ,
903and
904.Dv right2left .
905Data entering from the right is passed to the left and duplicated on
906.Dv right2left ,
907and data entering from the left is passed to the right and
908duplicated on
909.Dv left2right .
910Data entering from
911.Dv left2right
912is sent to the right and data from
913.Dv right2left
914to left.
915.It RFC1490 MUX
916Encapsulates/de-encapsulates frames encoded according to RFC 1490.
917Has a hook for the encapsulated packets
918.Pq Dq downstream
919and one hook
920for each protocol (i.e. IP, PPP, etc.).
921.It FRAME RELAY MUX
922Encapsulates/de-encapsulates Frame Relay frames.
923Has a hook for the encapsulated packets
924.Pq Dq downstream
925and one hook
926for each DLCI.
927.It FRAME RELAY LMI
928Automatically handles frame relay
929.Dq LMI
930(link management interface) operations and packets.
931Automatically probes and detects which of several LMI standards
932is in use at the exchange.
933.It TTY
934This node is also a line discipline.
935It simply converts between mbuf frames and sequential serial data,
936allowing a tty to appear as a netgraph node.
937It has a programmable
938.Dq hotkey
939character.
940.It ASYNC
941This node encapsulates and de-encapsulates asynchronous frames
942according to RFC 1662.
943This is used in conjunction with the TTY node
944type for supporting PPP links over asynchronous serial lines.
945.It INTERFACE
946This node is also a system networking interface.
947It has hooks representing each protocol family (IP, AppleTalk, etc.)
948and appears in the output of
949.Xr ifconfig 8 .
950The interfaces are named
951.Em ng0 ,
952.Em ng1 ,
953etc.
954.El
955.Sh NOTES
956Whether a named node exists can be checked by trying to send a control message
957to it (e.g.,
958.Dv NGM_NODEINFO ) .
959If it does not exist,
960.Er ENOENT
961will be returned.
962.Pp
963All data messages are mbuf chains with the M_PKTHDR flag set.
964.Pp
965Nodes are responsible for freeing what they allocate.
966There are three exceptions:
967.Bl -tag -width xxxx
968.It 1
969Mbufs sent across a data link are never to be freed by the sender.
970.It 2
971Any meta-data information traveling with the data has the same restriction.
972It might be freed by any node the data passes through, and a
973.Dv NULL
974passed onwards, but the caller will never free it.
975Two macros
976.Fn NG_FREE_META "meta"
977and
978.Fn NG_FREE_DATA "m" "meta"
979should be used if possible to free data and meta data (see
980.In netgraph/netgraph.h ) .
981.It 3
982Messages sent using
983.Fn ng_send_msg
984are freed by the callee.
985As in the case above, the addresses
986associated with the message are freed by whatever allocated them so the
987recipient should copy them if it wants to keep that information.
988.El
989.Sh FILES
990.Bl -tag -width xxxxx -compact
991.It In netgraph/netgraph.h
992Definitions for use solely within the kernel by
993.Nm
994nodes.
995.It In netgraph/ng_message.h
996Definitions needed by any file that needs to deal with
997.Nm
998messages.
999.It In netgraph/socket/ng_socket.h
1000Definitions needed to use
1001.Nm
1002socket type nodes.
1003.It In netgraph/{type}/ng_{type}.h
1004Definitions needed to use
1005.Nm
1006{type}
1007nodes, including the type cookie definition.
1008.It Pa /boot/modules/netgraph.ko
1009Netgraph subsystem loadable KLD module.
1010.It Pa /boot/modules/ng_{type}.ko
1011Loadable KLD module for node type {type}.
1012.El
1013.Sh USER MODE SUPPORT
1014There is a library for supporting user-mode programs that wish
1015to interact with the netgraph system.
1016See
1017.Xr netgraph 3
1018for details.
1019.Pp
1020Two user-mode support programs,
1021.Xr ngctl 8
1022and
1023.Xr nghook 8 ,
1024are available to assist manual configuration and debugging.
1025.Pp
1026There are a few useful techniques for debugging new node types.
1027First, implementing new node types in user-mode first
1028makes debugging easier.
1029The
1030.Em tee
1031node type is also useful for debugging, especially in conjunction with
1032.Xr ngctl 8
1033and
1034.Xr nghook 8 .
1035.Sh SEE ALSO
1036.Xr socket 2 ,
1037.Xr netgraph 3 ,
1038.Xr ng_async 4 ,
1039.Xr ng_bpf 4 ,
1040.Xr ng_bridge 4 ,
1041.Xr ng_cisco 4 ,
1042.Xr ng_echo 4 ,
1043.Xr ng_eiface 4 ,
1044.Xr ng_etf 4 ,
1045.Xr ng_ether 4 ,
1046.Xr ng_frame_relay 4 ,
1047.Xr ng_hole 4 ,
1048.Xr ng_iface 4 ,
1049.Xr ng_ksocket 4 ,
1050.Xr ng_l2tp 4 ,
1051.Xr ng_lmi 4 ,
1052.Xr ng_mppc 4 ,
1053.Xr ng_one2many 4 ,
1054.Xr ng_ppp 4 ,
1055.Xr ng_pppoe 4 ,
1056.Xr ng_rfc1490 4 ,
1057.Xr ng_socket 4 ,
1058.Xr ng_tee 4 ,
1059.Xr ng_tty 4 ,
1060.Xr ng_UI 4 ,
1061.Xr ng_vjc 4 ,
1062.Xr ngctl 8 ,
1063.Xr nghook 8
1064.Sh HISTORY
1065The
1066.Nm
1067system was designed and first implemented at Whistle Communications, Inc.\&
1068in a version of
1069.Fx 2.2
1070customized for the Whistle InterJet.
1071It first made its debut in the main tree in
1072.Fx 3.4 .
1073.Sh AUTHORS
1074.An -nosplit
1075.An Julian Elischer Aq Mt julian@FreeBSD.org ,
1076with contributions by
1077.An Archie Cobbs Aq Mt archie@FreeBSD.org .
1078