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