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