xref: /freebsd/share/man/man4/netgraph.4 (revision b2c76c41be32f904179efed29c0ca04d53f3996c)
14cf49a43SJulian Elischer.\" Copyright (c) 1996-1999 Whistle Communications, Inc.
24cf49a43SJulian Elischer.\" All rights reserved.
34cf49a43SJulian Elischer.\"
44cf49a43SJulian Elischer.\" Subject to the following obligations and disclaimer of warranty, use and
54cf49a43SJulian Elischer.\" redistribution of this software, in source or object code forms, with or
64cf49a43SJulian Elischer.\" without modifications are expressly permitted by Whistle Communications;
74cf49a43SJulian Elischer.\" provided, however, that:
84cf49a43SJulian Elischer.\" 1. Any and all reproductions of the source or object code must include the
94cf49a43SJulian Elischer.\"    copyright notice above and the following disclaimer of warranties; and
104cf49a43SJulian Elischer.\" 2. No rights are granted, in any manner or form, to use Whistle
114cf49a43SJulian Elischer.\"    Communications, Inc. trademarks, including the mark "WHISTLE
124cf49a43SJulian Elischer.\"    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
134cf49a43SJulian Elischer.\"    such appears in the above copyright notice or in the software.
144cf49a43SJulian Elischer.\"
154cf49a43SJulian Elischer.\" THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
164cf49a43SJulian Elischer.\" TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
174cf49a43SJulian Elischer.\" REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
184cf49a43SJulian Elischer.\" INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
194cf49a43SJulian Elischer.\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
204cf49a43SJulian Elischer.\" WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
214cf49a43SJulian Elischer.\" REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
224cf49a43SJulian Elischer.\" SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
234cf49a43SJulian Elischer.\" IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
244cf49a43SJulian Elischer.\" RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
254cf49a43SJulian Elischer.\" WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
264cf49a43SJulian Elischer.\" PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
274cf49a43SJulian Elischer.\" SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
284cf49a43SJulian Elischer.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
294cf49a43SJulian Elischer.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
304cf49a43SJulian Elischer.\" THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
314cf49a43SJulian Elischer.\" OF SUCH DAMAGE.
324cf49a43SJulian Elischer.\"
33eddc45e7SJeroen Ruigrok van der Werven.\" Authors: Julian Elischer <julian@FreeBSD.org>
34eddc45e7SJeroen Ruigrok van der Werven.\"          Archie Cobbs <archie@FreeBSD.org>
354cf49a43SJulian Elischer.\"
364cf49a43SJulian Elischer.\" $Whistle: netgraph.4,v 1.7 1999/01/28 23:54:52 julian Exp $
374cf49a43SJulian Elischer.\"
38*79a100e2SWarner Losh.Dd September 29, 2021
394cf49a43SJulian Elischer.Dt NETGRAPH 4
403d45e180SRuslan Ermilov.Os
414cf49a43SJulian Elischer.Sh NAME
424cf49a43SJulian Elischer.Nm netgraph
436e341a6dSRuslan Ermilov.Nd "graph based kernel networking subsystem"
444cf49a43SJulian Elischer.Sh DESCRIPTION
454cf49a43SJulian ElischerThe
464cf49a43SJulian Elischer.Nm
474cf49a43SJulian Elischersystem provides a uniform and modular system for the implementation
486e341a6dSRuslan Ermilovof kernel objects which perform various networking functions.
496e341a6dSRuslan ErmilovThe objects, known as
504cf49a43SJulian Elischer.Em nodes ,
516e341a6dSRuslan Ermilovcan be arranged into arbitrarily complicated graphs.
526e341a6dSRuslan ErmilovNodes have
534cf49a43SJulian Elischer.Em hooks
544cf49a43SJulian Elischerwhich are used to connect two nodes together, forming the edges in the graph.
554cf49a43SJulian ElischerNodes communicate along the edges to process data, implement protocols, etc.
564cf49a43SJulian Elischer.Pp
574cf49a43SJulian ElischerThe aim of
584cf49a43SJulian Elischer.Nm
594cf49a43SJulian Elischeris to supplement rather than replace the existing kernel networking
60b5e7e999SRuslan Ermilovinfrastructure.
61b5e7e999SRuslan ErmilovIt provides:
624cf49a43SJulian Elischer.Pp
636e341a6dSRuslan Ermilov.Bl -bullet -compact
644cf49a43SJulian Elischer.It
656e341a6dSRuslan ErmilovA flexible way of combining protocol and link level drivers.
664cf49a43SJulian Elischer.It
676e341a6dSRuslan ErmilovA modular way to implement new protocols.
684cf49a43SJulian Elischer.It
696e341a6dSRuslan ErmilovA common framework for kernel entities to inter-communicate.
704cf49a43SJulian Elischer.It
716e341a6dSRuslan ErmilovA reasonably fast, kernel-based implementation.
724cf49a43SJulian Elischer.El
73b5e7e999SRuslan Ermilov.Ss Nodes and Types
744cf49a43SJulian ElischerThe most fundamental concept in
754cf49a43SJulian Elischer.Nm
764cf49a43SJulian Elischeris that of a
774cf49a43SJulian Elischer.Em node .
784cf49a43SJulian ElischerAll nodes implement a number of predefined methods which allow them
794cf49a43SJulian Elischerto interact with other nodes in a well defined manner.
804cf49a43SJulian Elischer.Pp
814cf49a43SJulian ElischerEach node has a
824cf49a43SJulian Elischer.Em type ,
834cf49a43SJulian Elischerwhich is a static property of the node determined at node creation time.
8451291597SJulian ElischerA node's type is described by a unique
8551291597SJulian Elischer.Tn ASCII
8651291597SJulian Elischertype name.
874cf49a43SJulian ElischerThe type implies what the node does and how it may be connected
884cf49a43SJulian Elischerto other nodes.
894cf49a43SJulian Elischer.Pp
906e341a6dSRuslan ErmilovIn object-oriented language, types are classes, and nodes are instances
916e341a6dSRuslan Ermilovof their respective class.
926e341a6dSRuslan ErmilovAll node types are subclasses of the generic node
934cf49a43SJulian Elischertype, and hence inherit certain common functionality and capabilities
9451291597SJulian Elischer(e.g., the ability to have an
9551291597SJulian Elischer.Tn ASCII
9651291597SJulian Elischername).
974cf49a43SJulian Elischer.Pp
9851291597SJulian ElischerNodes may be assigned a globally unique
9951291597SJulian Elischer.Tn ASCII
10051291597SJulian Elischername which can be
1014cf49a43SJulian Elischerused to refer to the node.
10251291597SJulian ElischerThe name must not contain the characters
1036e341a6dSRuslan Ermilov.Ql .\&
10451291597SJulian Elischeror
1056e341a6dSRuslan Ermilov.Ql \&: ,
10651291597SJulian Elischerand is limited to
1076e341a6dSRuslan Ermilov.Dv NG_NODESIZ
1086e341a6dSRuslan Ermilovcharacters (including the terminating
1096e341a6dSRuslan Ermilov.Dv NUL
1106e341a6dSRuslan Ermilovcharacter).
1114cf49a43SJulian Elischer.Pp
1123563464eSGiorgos KeramidasEach node instance has a unique
1133563464eSGiorgos Keramidas.Em ID number
1143563464eSGiorgos Keramidaswhich is expressed as a 32-bit hexadecimal value.
1153563464eSGiorgos KeramidasThis value may be used to refer to a node when there is no
11651291597SJulian Elischer.Tn ASCII
11751291597SJulian Elischername assigned to it.
118b5e7e999SRuslan Ermilov.Ss Hooks
1194cf49a43SJulian ElischerNodes are connected to other nodes by connecting a pair of
1204cf49a43SJulian Elischer.Em hooks ,
1216e341a6dSRuslan Ermilovone from each node.
1226e341a6dSRuslan ErmilovData flows bidirectionally between nodes along
123b5e7e999SRuslan Ermilovconnected pairs of hooks.
124b5e7e999SRuslan ErmilovA node may have as many hooks as it
1254cf49a43SJulian Elischerneeds, and may assign whatever meaning it wants to a hook.
1264cf49a43SJulian Elischer.Pp
1274cf49a43SJulian ElischerHooks have these properties:
1286e341a6dSRuslan Ermilov.Bl -bullet
1294cf49a43SJulian Elischer.It
13051291597SJulian ElischerA hook has an
13151291597SJulian Elischer.Tn ASCII
13251291597SJulian Elischername which is unique among all hooks
1334cf49a43SJulian Elischeron that node (other hooks on other nodes may have the same name).
1346e341a6dSRuslan ErmilovThe name must not contain the characters
1356e341a6dSRuslan Ermilov.Ql .\&
1366e341a6dSRuslan Ermilovor
1376e341a6dSRuslan Ermilov.Ql \&: ,
13851291597SJulian Elischerand is
1394cf49a43SJulian Elischerlimited to
1406e341a6dSRuslan Ermilov.Dv NG_HOOKSIZ
1416e341a6dSRuslan Ermilovcharacters (including the terminating
1426e341a6dSRuslan Ermilov.Dv NUL
1436e341a6dSRuslan Ermilovcharacter).
1444cf49a43SJulian Elischer.It
145b5e7e999SRuslan ErmilovA hook is always connected to another hook.
146b5e7e999SRuslan ErmilovThat is, hooks are
1474cf49a43SJulian Elischercreated at the time they are connected, and breaking an edge by
1484cf49a43SJulian Elischerremoving either hook destroys both hooks.
149859a4d16SJulian Elischer.It
150859a4d16SJulian ElischerA hook can be set into a state where incoming packets are always queued
151b5e7e999SRuslan Ermilovby the input queueing system, rather than being delivered directly.
15287757985SRuslan ErmilovThis can be used when the data is sent from an interrupt handler,
15387757985SRuslan Ermilovand processing must be quick so as not to block other interrupts.
154c4b5eea4SJulian Elischer.It
15536f7af57SGiorgos KeramidasA hook may supply overriding receive data and receive message functions,
156c4b5eea4SJulian Elischerwhich should be used for data and messages received through that hook
157c4b5eea4SJulian Elischerin preference to the general node-wide methods.
1584cf49a43SJulian Elischer.El
1594cf49a43SJulian Elischer.Pp
1604cf49a43SJulian ElischerA node may decide to assign special meaning to some hooks.
16151291597SJulian ElischerFor example, connecting to the hook named
1626e341a6dSRuslan Ermilov.Va debug
16351291597SJulian Elischermight trigger
1644cf49a43SJulian Elischerthe node to start sending debugging information to that hook.
165b5e7e999SRuslan Ermilov.Ss Data Flow
1664cf49a43SJulian ElischerTwo types of information flow between nodes: data messages and
167b5e7e999SRuslan Ermilovcontrol messages.
1686e341a6dSRuslan ErmilovData messages are passed in
1696e341a6dSRuslan Ermilov.Vt mbuf chains
1706e341a6dSRuslan Ermilovalong the edges
171b5e7e999SRuslan Ermilovin the graph, one edge at a time.
1726e341a6dSRuslan ErmilovThe first
1736e341a6dSRuslan Ermilov.Vt mbuf
1746e341a6dSRuslan Ermilovin a chain must have the
1754cf49a43SJulian Elischer.Dv M_PKTHDR
1766e341a6dSRuslan Ermilovflag set.
17736f7af57SGiorgos KeramidasEach node decides how to handle data received through one of its hooks.
1784cf49a43SJulian Elischer.Pp
17987757985SRuslan ErmilovAlong with data, nodes can also receive control messages.
18087757985SRuslan ErmilovThere are generic and type-specific control messages.
181b5e7e999SRuslan ErmilovControl messages have a common
18236f7af57SGiorgos Keramidasheader format, followed by type-specific data, and are binary structures
183b5e7e999SRuslan Ermilovfor efficiency.
1846e341a6dSRuslan ErmilovHowever, node types may also support conversion of the
18536f7af57SGiorgos Keramidastype-specific data between binary and
18651291597SJulian Elischer.Tn ASCII
1876e341a6dSRuslan Ermilovformats,
18851291597SJulian Elischerfor debugging and human interface purposes (see the
189f8307e12SArchie Cobbs.Dv NGM_ASCII2BINARY
190f8307e12SArchie Cobbsand
191f8307e12SArchie Cobbs.Dv NGM_BINARY2ASCII
192b5e7e999SRuslan Ermilovgeneric control messages below).
193b5e7e999SRuslan ErmilovNodes are not required to support these conversions.
194f8307e12SArchie Cobbs.Pp
195b5e7e999SRuslan ErmilovThere are three ways to address a control message.
196b5e7e999SRuslan ErmilovIf there is a sequence of edges connecting the two nodes, the message
19751291597SJulian Elischermay be
19851291597SJulian Elischer.Dq source routed
19951291597SJulian Elischerby specifying the corresponding sequence
200859a4d16SJulian Elischerof
201859a4d16SJulian Elischer.Tn ASCII
202859a4d16SJulian Elischerhook names as the destination address for the message (relative
203b5e7e999SRuslan Ermilovaddressing).
204b5e7e999SRuslan ErmilovIf the destination is adjacent to the source, then the source
205859a4d16SJulian Elischernode may simply specify (as a pointer in the code) the hook across which the
206b5e7e999SRuslan Ermilovmessage should be sent.
20736f7af57SGiorgos KeramidasOtherwise, the recipient node's global
20851291597SJulian Elischer.Tn ASCII
20951291597SJulian Elischername
2103563464eSGiorgos Keramidas(or equivalent ID-based name) is used as the destination address
211b5e7e999SRuslan Ermilovfor the message (absolute addressing).
212b5e7e999SRuslan ErmilovThe two types of
213859a4d16SJulian Elischer.Tn ASCII
214859a4d16SJulian Elischeraddressing
2154cf49a43SJulian Elischermay be combined, by specifying an absolute start node and a sequence
2166e341a6dSRuslan Ermilovof hooks.
2176e341a6dSRuslan ErmilovOnly the
218859a4d16SJulian Elischer.Tn ASCII
21936f7af57SGiorgos Keramidasaddressing modes are available to control programs outside the kernel;
22036f7af57SGiorgos Keramidasuse of direct pointers is limited to kernel modules.
2214cf49a43SJulian Elischer.Pp
2223563464eSGiorgos KeramidasMessages often represent commands that are followed by a reply message
223b5e7e999SRuslan Ermilovin the reverse direction.
224b5e7e999SRuslan ErmilovTo facilitate this, the recipient of a
22551291597SJulian Elischercontrol message is supplied with a
22651291597SJulian Elischer.Dq return address
227859a4d16SJulian Elischerthat is suitable for addressing a reply.
2284cf49a43SJulian Elischer.Pp
22936f7af57SGiorgos KeramidasEach control message contains a 32-bit value, called a
23036f7af57SGiorgos Keramidas.Dq typecookie ,
2313563464eSGiorgos Keramidasindicating the type of the message, i.e.\& how to interpret it.
2324cf49a43SJulian ElischerTypically each type defines a unique typecookie for the messages
233b5e7e999SRuslan Ermilovthat it understands.
234b5e7e999SRuslan ErmilovHowever, a node may choose to recognize and
2356e341a6dSRuslan Ermilovimplement more than one type of messages.
236ac444505SJulian Elischer.Pp
237859a4d16SJulian ElischerIf a message is delivered to an address that implies that it arrived
2386e341a6dSRuslan Ermilovat that node through a particular hook (as opposed to having been directly
2396e341a6dSRuslan Ermilovaddressed using its ID or global name) then that hook is identified to the
240b5e7e999SRuslan Ermilovreceiving node.
2416e341a6dSRuslan ErmilovThis allows a message to be re-routed or passed on, should
242859a4d16SJulian Elischera node decide that this is required, in much the same way that data packets
2436e341a6dSRuslan Ermilovare passed around between nodes.
2446e341a6dSRuslan ErmilovA set of standard
245859a4d16SJulian Elischermessages for flow control and link management purposes are
246859a4d16SJulian Elischerdefined by the base system that are usually
247b5e7e999SRuslan Ermilovpassed around in this manner.
248b5e7e999SRuslan ErmilovFlow control message would usually travel
249859a4d16SJulian Elischerin the opposite direction to the data to which they pertain.
2506e341a6dSRuslan Ermilov.Ss Netgraph is (Usually) Functional
2514cf49a43SJulian ElischerIn order to minimize latency, most
25251291597SJulian Elischer.Nm
2534cf49a43SJulian Elischeroperations are functional.
2544cf49a43SJulian ElischerThat is, data and control messages are delivered by making function
255b5e7e999SRuslan Ermilovcalls rather than by using queues and mailboxes.
256b5e7e999SRuslan ErmilovFor example, if node
2576e341a6dSRuslan ErmilovA wishes to send a data
2586e341a6dSRuslan Ermilov.Vt mbuf
2596e341a6dSRuslan Ermilovto neighboring node B, it calls the
2604cf49a43SJulian Elischergeneric
2614cf49a43SJulian Elischer.Nm
262b5e7e999SRuslan Ermilovdata delivery function.
263b5e7e999SRuslan ErmilovThis function in turn locates
26451291597SJulian Elischernode B and calls B's
26551291597SJulian Elischer.Dq receive data
266b5e7e999SRuslan Ermilovmethod.
267b5e7e999SRuslan ErmilovThere are exceptions to this.
268ac444505SJulian Elischer.Pp
269069154d5SJulian ElischerEach node has an input queue, and some operations can be considered to
2706e341a6dSRuslan Ermilovbe
2716e341a6dSRuslan Ermilov.Em writers
2726e341a6dSRuslan Ermilovin that they alter the state of the node.
2736e341a6dSRuslan ErmilovObviously, in an SMP
274069154d5SJulian Elischerworld it would be bad if the state of a node were changed while another
275b5e7e999SRuslan Ermilovdata packet were transiting the node.
276b5e7e999SRuslan ErmilovFor this purpose, the input queue implements a
277069154d5SJulian Elischer.Em reader/writer
278069154d5SJulian Elischersemantic so that when there is a writer in the node, all other requests
279069154d5SJulian Elischerare queued, and while there are readers, a writer, and any following
280b5e7e999SRuslan Ermilovpackets are queued.
281b5e7e999SRuslan ErmilovIn the case where there is no reason to queue the
282f87717faSMike Pritcharddata, the input method is called directly, as mentioned above.
283069154d5SJulian Elischer.Pp
284069154d5SJulian ElischerA node may declare that all requests should be considered as writers,
285069154d5SJulian Elischeror that requests coming in over a particular hook should be considered to
286069154d5SJulian Elischerbe a writer, or even that packets leaving or entering across a particular
287069154d5SJulian Elischerhook should always be queued, rather than delivered directly (often useful
288069154d5SJulian Elischerfor interrupt routines who want to get back to the hardware quickly).
289f87717faSMike PritchardBy default, all control message packets are considered to be writers
2906e341a6dSRuslan Ermilovunless specifically declared to be a reader in their definition.
2916e341a6dSRuslan Ermilov(See
2926e341a6dSRuslan Ermilov.Dv NGM_READONLY
2936e341a6dSRuslan Ermilovin
2944131c351SGleb Smirnoff.In netgraph/ng_message.h . )
295ac444505SJulian Elischer.Pp
296ac444505SJulian ElischerWhile this mode of operation
2974cf49a43SJulian Elischerresults in good performance, it has a few implications for node
2984cf49a43SJulian Elischerdevelopers:
2996e341a6dSRuslan Ermilov.Bl -bullet
3004cf49a43SJulian Elischer.It
3014cf49a43SJulian ElischerWhenever a node delivers a data or control message, the node
302ac444505SJulian Elischermay need to allow for the possibility of receiving a returning
303ac444505SJulian Elischermessage before the original delivery function call returns.
3044cf49a43SJulian Elischer.It
3056e341a6dSRuslan Ermilov.Nm Netgraph
30687757985SRuslan Ermilovprovides internal synchronization between nodes.
30787757985SRuslan ErmilovData always enters a
30887757985SRuslan Ermilov.Dq graph
30987757985SRuslan Ermilovat an
31087757985SRuslan Ermilov.Em edge node .
31187757985SRuslan ErmilovAn
31287757985SRuslan Ermilov.Em edge node
31387757985SRuslan Ermilovis a node that interfaces between
31487757985SRuslan Ermilov.Nm
31587757985SRuslan Ermilovand some other part of the system.
31687757985SRuslan ErmilovExamples of
31787757985SRuslan Ermilov.Dq edge nodes
31887757985SRuslan Ermilovinclude device drivers, the
31987757985SRuslan Ermilov.Vt socket , ether , tty ,
32087757985SRuslan Ermilovand
32187757985SRuslan Ermilov.Vt ksocket
32287757985SRuslan Ermilovnode type.
32387757985SRuslan ErmilovIn these
32487757985SRuslan Ermilov.Em edge nodes ,
3256e341a6dSRuslan Ermilovthe calling thread directly executes code in the node, and from that code
32687757985SRuslan Ermilovcalls upon the
32787757985SRuslan Ermilov.Nm
32887757985SRuslan Ermilovframework to deliver data across some edge
32987757985SRuslan Ermilovin the graph.
33087757985SRuslan ErmilovFrom an execution point of view, the calling thread will execute the
33187757985SRuslan Ermilov.Nm
33287757985SRuslan Ermilovframework methods, and if it can acquire a lock to do so,
33387757985SRuslan Ermilovthe input methods of the next node.
33487757985SRuslan ErmilovThis continues until either the data is discarded or queued for some
33587757985SRuslan Ermilovdevice or system entity, or the thread is unable to acquire a lock on
33687757985SRuslan Ermilovthe next node.
33787757985SRuslan ErmilovIn that case, the data is queued for the node, and execution rewinds
33887757985SRuslan Ermilovback to the original calling entity.
33987757985SRuslan ErmilovThe queued data will be picked up and processed by either the current
34087757985SRuslan Ermilovholder of the lock when they have completed their operations, or by
34187757985SRuslan Ermilova special
34287757985SRuslan Ermilov.Nm
34387757985SRuslan Ermilovthread that is activated when there are such items
34487757985SRuslan Ermilovqueued.
3454cf49a43SJulian Elischer.It
3466e341a6dSRuslan ErmilovIt is possible for an infinite loop to occur if the graph contains cycles.
3474cf49a43SJulian Elischer.El
3484cf49a43SJulian Elischer.Pp
3494cf49a43SJulian ElischerSo far, these issues have not proven problematical in practice.
3506e341a6dSRuslan Ermilov.Ss Interaction with Other Parts of the Kernel
3514cf49a43SJulian ElischerA node may have a hidden interaction with other components of the
3524cf49a43SJulian Elischerkernel outside of the
3534cf49a43SJulian Elischer.Nm
3544cf49a43SJulian Elischersubsystem, such as device hardware,
3556e341a6dSRuslan Ermilovkernel protocol stacks, etc.
3566e341a6dSRuslan ErmilovIn fact, one of the benefits of
3574cf49a43SJulian Elischer.Nm
3584cf49a43SJulian Elischeris the ability to join disparate kernel networking entities together in a
3594cf49a43SJulian Elischerconsistent communication framework.
3604cf49a43SJulian Elischer.Pp
3616e341a6dSRuslan ErmilovAn example is the
3626e341a6dSRuslan Ermilov.Vt socket
3636e341a6dSRuslan Ermilovnode type which is both a
3646e341a6dSRuslan Ermilov.Nm
3656e341a6dSRuslan Ermilovnode and a
3664cf49a43SJulian Elischer.Xr socket 2
3676e341a6dSRuslan Ermilovin the protocol family
3684cf49a43SJulian Elischer.Dv PF_NETGRAPH .
3694cf49a43SJulian ElischerSocket nodes allow user processes to participate in
3704b66483fSRuslan Ermilov.Nm .
3714cf49a43SJulian ElischerOther nodes communicate with socket nodes using the usual methods, and the
3724cf49a43SJulian Elischernode hides the fact that it is also passing information to and from a
3734cf49a43SJulian Elischercooperating user process.
3744cf49a43SJulian Elischer.Pp
3754cf49a43SJulian ElischerAnother example is a device driver that presents
3764cf49a43SJulian Elischera node interface to the hardware.
377b5e7e999SRuslan Ermilov.Ss Node Methods
3784cf49a43SJulian ElischerNodes are notified of the following actions via function calls
3796e341a6dSRuslan Ermilovto the following node methods,
3804cf49a43SJulian Elischerand may accept or reject that action (by returning the appropriate
3814cf49a43SJulian Elischererror code):
3826e341a6dSRuslan Ermilov.Bl -tag -width 2n
3834cf49a43SJulian Elischer.It Creation of a new node
38487757985SRuslan ErmilovThe constructor for the type is called.
38587757985SRuslan ErmilovIf creation of a new node is allowed, constructor method may allocate any
38687757985SRuslan Ermilovspecial resources it needs.
38787757985SRuslan ErmilovFor nodes that correspond to hardware, this is typically done during the
38887757985SRuslan Ermilovdevice attach routine.
38987757985SRuslan ErmilovOften a global
39051291597SJulian Elischer.Tn ASCII
39151291597SJulian Elischername corresponding to the
3924cf49a43SJulian Elischerdevice name is assigned here as well.
3934cf49a43SJulian Elischer.It Creation of a new hook
3944cf49a43SJulian ElischerThe hook is created and tentatively
3954cf49a43SJulian Elischerlinked to the node, and the node is told about the name that will be
3966e341a6dSRuslan Ermilovused to describe this hook.
3976e341a6dSRuslan ErmilovThe node sets up any special data structures
3984cf49a43SJulian Elischerit needs, or may reject the connection, based on the name of the hook.
3994cf49a43SJulian Elischer.It Successful connection of two hooks
4004cf49a43SJulian ElischerAfter both ends have accepted their
4014cf49a43SJulian Elischerhooks, and the links have been made, the nodes get a chance to
4026e341a6dSRuslan Ermilovfind out who their peer is across the link, and can then decide to reject
4036e341a6dSRuslan Ermilovthe connection.
4046e341a6dSRuslan ErmilovTear-down is automatic.
4056e341a6dSRuslan ErmilovThis is also the time at which
406859a4d16SJulian Elischera node may decide whether to set a particular hook (or its peer) into
4076e341a6dSRuslan Ermilovthe
408b196d1ceSDima Dorfman.Em queueing
409859a4d16SJulian Elischermode.
4104cf49a43SJulian Elischer.It Destruction of a hook
4116e341a6dSRuslan ErmilovThe node is notified of a broken connection.
4126e341a6dSRuslan ErmilovThe node may consider some hooks
4134cf49a43SJulian Elischerto be critical to operation and others to be expendable: the disconnection
4144cf49a43SJulian Elischerof one hook may be an acceptable event while for another it
4156e341a6dSRuslan Ermilovmay effect a total shutdown for the node.
41687757985SRuslan Ermilov.It Preshutdown of a node
41787757985SRuslan ErmilovThis method is called before real shutdown, which is discussed below.
41887757985SRuslan ErmilovWhile in this method, the node is fully operational and can send a
41987757985SRuslan Ermilov.Dq goodbye
42087757985SRuslan Ermilovmessage to its peers, or it can exclude itself from the chain and reconnect
42187757985SRuslan Ermilovits peers together, like the
42287757985SRuslan Ermilov.Xr ng_tee 4
42387757985SRuslan Ermilovnode type does.
4244cf49a43SJulian Elischer.It Shutdown of a node
4254cf49a43SJulian ElischerThis method allows a node to clean up
4264cf49a43SJulian Elischerand to ensure that any actions that need to be performed
4276e341a6dSRuslan Ermilovat this time are taken.
4286e341a6dSRuslan ErmilovThe method is called by the generic (i.e., superclass)
429c4b5eea4SJulian Elischernode destructor which will get rid of the generic components of the node.
4304cf49a43SJulian ElischerSome nodes (usually associated with a piece of hardware) may be
4314cf49a43SJulian Elischer.Em persistent
4324cf49a43SJulian Elischerin that a shutdown breaks all edges and resets the node,
4336e341a6dSRuslan Ermilovbut does not remove it.
4346e341a6dSRuslan ErmilovIn this case, the shutdown method should not
435be4252b3SJulian Elischerfree its resources, but rather, clean up and then call the
436da2fa159SRuslan Ermilov.Fn NG_NODE_REVIVE
437be4252b3SJulian Elischermacro to signal the generic code that the shutdown is aborted.
4386e341a6dSRuslan ErmilovIn the case where the shutdown is started by the node itself due to hardware
4396e341a6dSRuslan Ermilovremoval or unloading (via
4406e341a6dSRuslan Ermilov.Fn ng_rmnode_self ) ,
4416e341a6dSRuslan Ermilovit should set the
442be4252b3SJulian Elischer.Dv NGF_REALLY_DIE
4435e75e35cSDima Dorfmanflag to signal to its own shutdown method that it is not to persist.
4444cf49a43SJulian Elischer.El
445b5e7e999SRuslan Ermilov.Ss Sending and Receiving Data
446859a4d16SJulian ElischerTwo other methods are also supported by all nodes:
4476e341a6dSRuslan Ermilov.Bl -tag -width 2n
4484cf49a43SJulian Elischer.It Receive data message
449069154d5SJulian ElischerA
4506e341a6dSRuslan Ermilov.Nm
4516e341a6dSRuslan Ermilov.Em queueable request item ,
452f87717faSMike Pritchardusually referred to as an
453c4b5eea4SJulian Elischer.Em item ,
45487757985SRuslan Ermilovis received by this function.
4556e341a6dSRuslan ErmilovThe item contains a pointer to an
4566e341a6dSRuslan Ermilov.Vt mbuf .
457069154d5SJulian Elischer.Pp
4586e341a6dSRuslan ErmilovThe node is notified on which hook the item has arrived,
4594cf49a43SJulian Elischerand can use this information in its processing decision.
460ac444505SJulian ElischerThe receiving node must always
461069154d5SJulian Elischer.Fn NG_FREE_M
4626e341a6dSRuslan Ermilovthe
4636e341a6dSRuslan Ermilov.Vt mbuf chain
4646e341a6dSRuslan Ermilovon completion or error, or pass it on to another node
4654cf49a43SJulian Elischer(or kernel module) which will then be responsible for freeing it.
4666e341a6dSRuslan ErmilovSimilarly, the
467069154d5SJulian Elischer.Em item
468069154d5SJulian Elischermust be freed if it is not to be passed on to another node, by using the
469069154d5SJulian Elischer.Fn NG_FREE_ITEM
47087757985SRuslan Ermilovmacro.
4716e341a6dSRuslan ErmilovIf the item still holds references to
4726e341a6dSRuslan Ermilov.Vt mbufs
4736e341a6dSRuslan Ermilovat the time of
474f87717faSMike Pritchardfreeing then they will also be appropriately freed.
4756e341a6dSRuslan ErmilovTherefore, if there is any chance that the
4766e341a6dSRuslan Ermilov.Vt mbuf
4776e341a6dSRuslan Ermilovwill be
478f87717faSMike Pritchardchanged or freed separately from the item, it is very important
47987757985SRuslan Ermilovthat it be retrieved using the
480069154d5SJulian Elischer.Fn NGI_GET_M
48187757985SRuslan Ermilovmacro that also removes the reference within the item.
48287757985SRuslan Ermilov(Or multiple frees of the same object will occur.)
4834cf49a43SJulian Elischer.Pp
4846e341a6dSRuslan ErmilovIf it is only required to examine the contents of the
4856e341a6dSRuslan Ermilov.Vt mbufs ,
48687757985SRuslan Ermilovthen it is possible to use the
487069154d5SJulian Elischer.Fn NGI_M
4886e341a6dSRuslan Ermilovmacro to both read and rewrite
4896e341a6dSRuslan Ermilov.Vt mbuf
4906e341a6dSRuslan Ermilovpointer inside the item.
491069154d5SJulian Elischer.Pp
4926e341a6dSRuslan ErmilovIf developer needs to pass any meta information along with the
4936e341a6dSRuslan Ermilov.Vt mbuf chain ,
49487757985SRuslan Ermilovhe should use
49587757985SRuslan Ermilov.Xr mbuf_tags 9
49687757985SRuslan Ermilovframework.
49787757985SRuslan Ermilov.Bf -symbolic
4986e341a6dSRuslan ErmilovNote that old
49987757985SRuslan Ermilov.Nm
5006e341a6dSRuslan Ermilovspecific meta-data format is obsoleted now.
50187757985SRuslan Ermilov.Ef
5024cf49a43SJulian Elischer.Pp
503a845211bSDima DorfmanThe receiving node may decide to defer the data by queueing it in the
5044cf49a43SJulian Elischer.Nm
5056e341a6dSRuslan ErmilovNETISR system (see below).
5066e341a6dSRuslan ErmilovIt achieves this by setting the
507859a4d16SJulian Elischer.Dv HK_QUEUE
508859a4d16SJulian Elischerflag in the flags word of the hook on which that data will arrive.
509859a4d16SJulian ElischerThe infrastructure will respect that bit and queue the data for delivery at
5106e341a6dSRuslan Ermilova later time, rather than deliver it directly.
5116e341a6dSRuslan ErmilovA node may decide to set
512859a4d16SJulian Elischerthe bit on the
513859a4d16SJulian Elischer.Em peer
51487757985SRuslan Ermilovnode, so that its own output packets are queued.
515c4b5eea4SJulian Elischer.Pp
516c4b5eea4SJulian ElischerThe node may elect to nominate a different receive data function
5176e341a6dSRuslan Ermilovfor data received on a particular hook, to simplify coding.
5186e341a6dSRuslan ErmilovIt uses the
519d0353b83SRuslan Ermilov.Fn NG_HOOK_SET_RCVDATA hook fn
5206e341a6dSRuslan Ermilovmacro to do this.
5216e341a6dSRuslan ErmilovThe function receives the same arguments in every way
522c4b5eea4SJulian Elischerother than it will receive all (and only) packets from that hook.
5234cf49a43SJulian Elischer.It Receive control message
5244cf49a43SJulian ElischerThis method is called when a control message is addressed to the node.
525069154d5SJulian ElischerAs with the received data, an
526069154d5SJulian Elischer.Em item
527f87717faSMike Pritchardis received, with a pointer to the control message.
528069154d5SJulian ElischerThe message can be examined using the
529069154d5SJulian Elischer.Fn NGI_MSG
530069154d5SJulian Elischermacro, or completely extracted from the item using the
531069154d5SJulian Elischer.Fn NGI_GET_MSG
532069154d5SJulian Elischerwhich also removes the reference within the item.
533c2025a76SJoel DahlIf the item still holds a reference to the message when it is freed
534069154d5SJulian Elischer(using the
535069154d5SJulian Elischer.Fn NG_FREE_ITEM
5366e341a6dSRuslan Ermilovmacro), then the message will also be freed appropriately.
5376e341a6dSRuslan ErmilovIf the
5386e341a6dSRuslan Ermilovreference has been removed, the node must free the message itself using the
539069154d5SJulian Elischer.Fn NG_FREE_MSG
540069154d5SJulian Elischermacro.
5414cf49a43SJulian ElischerA return address is always supplied, giving the address of the node
5424cf49a43SJulian Elischerthat originated the message so a reply message can be sent anytime later.
543069154d5SJulian ElischerThe return address is retrieved from the
544069154d5SJulian Elischer.Em item
545069154d5SJulian Elischerusing the
546069154d5SJulian Elischer.Fn NGI_RETADDR
547069154d5SJulian Elischermacro and is of type
5486e341a6dSRuslan Ermilov.Vt ng_ID_t .
549069154d5SJulian ElischerAll control messages and replies are
5506e341a6dSRuslan Ermilovallocated with the
5516e341a6dSRuslan Ermilov.Xr malloc 9
5524cf49a43SJulian Elischertype
553069154d5SJulian Elischer.Dv M_NETGRAPH_MSG ,
5546e341a6dSRuslan Ermilovhowever it is more convenient to use the
555069154d5SJulian Elischer.Fn NG_MKMESSAGE
556069154d5SJulian Elischerand
557069154d5SJulian Elischer.Fn NG_MKRESPONSE
558069154d5SJulian Elischermacros to allocate and fill out a message.
559069154d5SJulian ElischerMessages must be freed using the
560069154d5SJulian Elischer.Fn NG_FREE_MSG
561069154d5SJulian Elischermacro.
562ac444505SJulian Elischer.Pp
563ac444505SJulian ElischerIf the message was delivered via a specific hook, that hook will
564ac444505SJulian Elischeralso be made known, which allows the use of such things as flow-control
565ac444505SJulian Elischermessages, and status change messages, where the node may want to forward
566ac444505SJulian Elischerthe message out another hook to that on which it arrived.
567c4b5eea4SJulian Elischer.Pp
568c4b5eea4SJulian ElischerThe node may elect to nominate a different receive message function
5696e341a6dSRuslan Ermilovfor messages received on a particular hook, to simplify coding.
5706e341a6dSRuslan ErmilovIt uses the
571d0353b83SRuslan Ermilov.Fn NG_HOOK_SET_RCVMSG hook fn
5726e341a6dSRuslan Ermilovmacro to do this.
5736e341a6dSRuslan ErmilovThe function receives the same arguments in every way
574c4b5eea4SJulian Elischerother than it will receive all (and only) messages from that hook.
5754cf49a43SJulian Elischer.El
5764cf49a43SJulian Elischer.Pp
5774cf49a43SJulian ElischerMuch use has been made of reference counts, so that nodes being
5786e341a6dSRuslan Ermilovfreed of all references are automatically freed, and this behaviour
5794cf49a43SJulian Elischerhas been tested and debugged to present a consistent and trustworthy
58051291597SJulian Elischerframework for the
58151291597SJulian Elischer.Dq type module
58251291597SJulian Elischerwriter to use.
583b5e7e999SRuslan Ermilov.Ss Addressing
5844cf49a43SJulian ElischerThe
5854cf49a43SJulian Elischer.Nm
5864cf49a43SJulian Elischerframework provides an unambiguous and simple to use method of specifically
5876e341a6dSRuslan Ermilovaddressing any single node in the graph.
5886e341a6dSRuslan ErmilovThe naming of a node is
5894cf49a43SJulian Elischerindependent of its type, in that another node, or external component
5904cf49a43SJulian Elischerneed not know anything about the node's type in order to address it so as
5916e341a6dSRuslan Ermilovto send it a generic message type.
5926e341a6dSRuslan ErmilovNode and hook names should be
5934cf49a43SJulian Elischerchosen so as to make addresses meaningful.
5944cf49a43SJulian Elischer.Pp
5956e341a6dSRuslan ErmilovAddresses are either absolute or relative.
5966e341a6dSRuslan ErmilovAn absolute address begins
5976e341a6dSRuslan Ermilovwith a node name or ID, followed by a colon, followed by a sequence of hook
5986e341a6dSRuslan Ermilovnames separated by periods.
5996e341a6dSRuslan ErmilovThis addresses the node reached by starting
6004cf49a43SJulian Elischerat the named node and following the specified sequence of hooks.
6014cf49a43SJulian ElischerA relative address includes only the sequence of hook names, implicitly
6024cf49a43SJulian Elischerstarting hook traversal at the local node.
6034cf49a43SJulian Elischer.Pp
6044cf49a43SJulian ElischerThere are a couple of special possibilities for the node name.
60551291597SJulian ElischerThe name
6066e341a6dSRuslan Ermilov.Ql .\&
60751291597SJulian Elischer(referred to as
6086e341a6dSRuslan Ermilov.Ql .: )
60951291597SJulian Elischeralways refers to the local node.
6104cf49a43SJulian ElischerAlso, nodes that have no global name may be addressed by their ID numbers,
6116e341a6dSRuslan Ermilovby enclosing the hexadecimal representation of the ID number within
6126e341a6dSRuslan Ermilovthe square brackets.
6136e341a6dSRuslan ErmilovHere are some examples of valid
6146e341a6dSRuslan Ermilov.Nm
6156e341a6dSRuslan Ermilovaddresses:
6166e341a6dSRuslan Ermilov.Bd -literal -offset indent
6176e341a6dSRuslan Ermilov\&.:
618069154d5SJulian Elischer[3f]:
6194cf49a43SJulian Elischerfoo:
6206e341a6dSRuslan Ermilov\&.:hook1
6214cf49a43SJulian Elischerfoo:hook1.hook2
622859a4d16SJulian Elischer[d80]:hook1
6234cf49a43SJulian Elischer.Ed
6244cf49a43SJulian Elischer.Pp
6256e341a6dSRuslan ErmilovThe following set of nodes might be created for a site with
6264cf49a43SJulian Elischera single physical frame relay line having two active logical DLCI channels,
6276e341a6dSRuslan Ermilovwith RFC 1490 frames on DLCI 16 and PPP frames over DLCI 20:
6284cf49a43SJulian Elischer.Bd -literal
6294cf49a43SJulian Elischer[type SYNC ]                  [type FRAME]                 [type RFC1490]
6304cf49a43SJulian Elischer[ "Frame1" ](uplink)<-->(data)[<un-named>](dlci16)<-->(mux)[<un-named>  ]
6314cf49a43SJulian Elischer[    A     ]                  [    B     ](dlci20)<---+    [     C      ]
6324cf49a43SJulian Elischer                                                      |
6334cf49a43SJulian Elischer                                                      |      [ type PPP ]
6344cf49a43SJulian Elischer                                                      +>(mux)[<un-named>]
6354cf49a43SJulian Elischer                                                             [    D     ]
6364cf49a43SJulian Elischer.Ed
6374cf49a43SJulian Elischer.Pp
6384cf49a43SJulian ElischerOne could always send a control message to node C from anywhere
6394cf49a43SJulian Elischerby using the name
6406e341a6dSRuslan Ermilov.Dq Li Frame1:uplink.dlci16 .
641ac444505SJulian ElischerIn this case, node C would also be notified that the message
642ac444505SJulian Elischerreached it via its hook
6436e341a6dSRuslan Ermilov.Va mux .
6444cf49a43SJulian ElischerSimilarly,
6456e341a6dSRuslan Ermilov.Dq Li Frame1:uplink.dlci20
6464cf49a43SJulian Elischercould reliably be used to reach node D, and node A could refer
6474cf49a43SJulian Elischerto node B as
6486e341a6dSRuslan Ermilov.Dq Li .:uplink ,
6494cf49a43SJulian Elischeror simply
6506e341a6dSRuslan Ermilov.Dq Li uplink .
6514cf49a43SJulian ElischerConversely, B can refer to A as
6526e341a6dSRuslan Ermilov.Dq Li data .
6534cf49a43SJulian ElischerThe address
6546e341a6dSRuslan Ermilov.Dq Li mux.data
6554cf49a43SJulian Elischercould be used by both nodes C and D to address a message to node A.
6564cf49a43SJulian Elischer.Pp
6574cf49a43SJulian ElischerNote that this is only for
6584cf49a43SJulian Elischer.Em control messages .
659ac444505SJulian ElischerIn each of these cases, where a relative addressing mode is
660ac444505SJulian Elischerused, the recipient is notified of the hook on which the
661ac444505SJulian Elischermessage arrived, as well as
662ac444505SJulian Elischerthe originating node.
663f87717faSMike PritchardThis allows the option of hop-by-hop distribution of messages and
664ac444505SJulian Elischerstate information.
665ac444505SJulian ElischerData messages are
666ac444505SJulian Elischer.Em only
667ac444505SJulian Elischerrouted one hop at a time, by specifying the departing
668ac444505SJulian Elischerhook, with each node making
6696e341a6dSRuslan Ermilovthe next routing decision.
6706e341a6dSRuslan ErmilovSo when B receives a frame on hook
6716e341a6dSRuslan Ermilov.Va data ,
6724cf49a43SJulian Elischerit decodes the frame relay header to determine the DLCI,
6734cf49a43SJulian Elischerand then forwards the unwrapped frame to either C or D.
6744cf49a43SJulian Elischer.Pp
675859a4d16SJulian ElischerIn a similar way, flow control messages may be routed in the reverse
6766e341a6dSRuslan Ermilovdirection to outgoing data.
6776e341a6dSRuslan ErmilovFor example a
6786e341a6dSRuslan Ermilov.Dq "buffer nearly full"
6796e341a6dSRuslan Ermilovmessage from
6806e341a6dSRuslan Ermilov.Dq Li Frame1:
6816e341a6dSRuslan Ermilovwould be passed to node B
682859a4d16SJulian Elischerwhich might decide to send similar messages to both nodes
6836e341a6dSRuslan ErmilovC and D.
684859a4d16SJulian ElischerThe nodes would use
6856e341a6dSRuslan Ermilov.Em "direct hook pointer"
6866e341a6dSRuslan Ermilovaddressing to route the messages.
6876e341a6dSRuslan ErmilovThe message may have travelled from
6886e341a6dSRuslan Ermilov.Dq Li Frame1:
6896e341a6dSRuslan Ermilovto B
690859a4d16SJulian Elischeras a synchronous reply, saving time and cycles.
691b5e7e999SRuslan Ermilov.Ss Netgraph Structures
692069154d5SJulian ElischerStructures are defined in
6936e341a6dSRuslan Ermilov.In netgraph/netgraph.h
694f87717faSMike Pritchard(for kernel structures only of interest to nodes)
695069154d5SJulian Elischerand
6966e341a6dSRuslan Ermilov.In netgraph/ng_message.h
697069154d5SJulian Elischer(for message definitions also of interest to user programs).
698adefd3f6SRuslan Ermilov.Pp
69930400f03SJulian ElischerThe two basic object types that are of interest to node authors are
70030400f03SJulian Elischer.Em nodes
70130400f03SJulian Elischerand
70230400f03SJulian Elischer.Em hooks .
70330400f03SJulian ElischerThese two objects have the following
70430400f03SJulian Elischerproperties that are also of interest to the node writers.
7056e341a6dSRuslan Ermilov.Bl -tag -width 2n
7066e341a6dSRuslan Ermilov.It Vt "struct ng_node"
7076e341a6dSRuslan ErmilovNode authors should always use the following
7086e341a6dSRuslan Ermilov.Ic typedef
7096e341a6dSRuslan Ermilovto declare
71030400f03SJulian Elischertheir pointers, and should never actually declare the structure.
71130400f03SJulian Elischer.Pp
7126e341a6dSRuslan Ermilov.Fd "typedef struct ng_node *node_p;"
71330400f03SJulian Elischer.Pp
71430400f03SJulian ElischerThe following properties are associated with a node, and can be
71530400f03SJulian Elischeraccessed in the following manner:
7166e341a6dSRuslan Ermilov.Bl -tag -width 2n
7176e341a6dSRuslan Ermilov.It Validity
71830400f03SJulian ElischerA driver or interrupt routine may want to check whether
7196e341a6dSRuslan Ermilovthe node is still valid.
7206e341a6dSRuslan ErmilovIt is assumed that the caller holds a reference
72130400f03SJulian Elischeron the node so it will not have been freed, however it may have been
7226e341a6dSRuslan Ermilovdisabled or otherwise shut down.
7236e341a6dSRuslan ErmilovUsing the
7246e341a6dSRuslan Ermilov.Fn NG_NODE_IS_VALID node
7256e341a6dSRuslan Ermilovmacro will return this state.
7266e341a6dSRuslan ErmilovEventually it should be almost impossible
72730400f03SJulian Elischerfor code to run in an invalid node but at this time that work has not been
72830400f03SJulian Elischercompleted.
7296e341a6dSRuslan Ermilov.It Node ID Pq Vt ng_ID_t
73030400f03SJulian ElischerThis property can be retrieved using the macro
7316e341a6dSRuslan Ermilov.Fn NG_NODE_ID node .
7326e341a6dSRuslan Ermilov.It Node name
7336e341a6dSRuslan ErmilovOptional globally unique name,
7346e341a6dSRuslan Ermilov.Dv NUL
7356e341a6dSRuslan Ermilovterminated string.
7366e341a6dSRuslan ErmilovIf there
737069154d5SJulian Elischeris a value in here, it is the name of the node.
7386e341a6dSRuslan Ermilov.Bd -literal -offset indent
7396e341a6dSRuslan Ermilovif (NG_NODE_NAME(node)[0] != '\e0') ...
7406e341a6dSRuslan Ermilov
7416e341a6dSRuslan Ermilovif (strcmp(NG_NODE_NAME(node), "fred") == 0) ...
7426e341a6dSRuslan Ermilov.Ed
7436e341a6dSRuslan Ermilov.It A node dependent opaque cookie
7446e341a6dSRuslan ErmilovAnything of the pointer type can be placed here.
7456e341a6dSRuslan ErmilovThe macros
746d0353b83SRuslan Ermilov.Fn NG_NODE_SET_PRIVATE node value
74730400f03SJulian Elischerand
7486e341a6dSRuslan Ermilov.Fn NG_NODE_PRIVATE node
7496e341a6dSRuslan Ermilovset and retrieve this property, respectively.
7506e341a6dSRuslan Ermilov.It Number of hooks
7516e341a6dSRuslan ErmilovThe
7526e341a6dSRuslan Ermilov.Fn NG_NODE_NUMHOOKS node
7536e341a6dSRuslan Ermilovmacro is used
75430400f03SJulian Elischerto retrieve this value.
7556e341a6dSRuslan Ermilov.It Hooks
75630400f03SJulian ElischerThe node may have a number of hooks.
75730400f03SJulian ElischerA traversal method is provided to allow all the hooks to be
75830400f03SJulian Elischertested for some condition.
759d0353b83SRuslan Ermilov.Fn NG_NODE_FOREACH_HOOK node fn arg rethook
7606e341a6dSRuslan Ermilovwhere
7616e341a6dSRuslan Ermilov.Fa fn
7626e341a6dSRuslan Ermilovis a function that will be called for each hook
76330400f03SJulian Elischerwith the form
764d0353b83SRuslan Ermilov.Fn fn hook arg
7656e341a6dSRuslan Ermilovand returning 0 to terminate the search.
7666e341a6dSRuslan ErmilovIf the search is terminated, then
7676e341a6dSRuslan Ermilov.Fa rethook
76830400f03SJulian Elischerwill be set to the hook at which the search was terminated.
769069154d5SJulian Elischer.El
7706e341a6dSRuslan Ermilov.It Vt "struct ng_hook"
7716e341a6dSRuslan ErmilovNode authors should always use the following
7726e341a6dSRuslan Ermilov.Ic typedef
7736e341a6dSRuslan Ermilovto declare
77430400f03SJulian Elischertheir hook pointers.
77530400f03SJulian Elischer.Pp
7766e341a6dSRuslan Ermilov.Fd "typedef struct ng_hook *hook_p;"
77730400f03SJulian Elischer.Pp
77830400f03SJulian ElischerThe following properties are associated with a hook, and can be
77930400f03SJulian Elischeraccessed in the following manner:
7806e341a6dSRuslan Ermilov.Bl -tag -width 2n
7816e341a6dSRuslan Ermilov.It A hook dependent opaque cookie
7826e341a6dSRuslan ErmilovAnything of the pointer type can be placed here.
7836e341a6dSRuslan ErmilovThe macros
784d0353b83SRuslan Ermilov.Fn NG_HOOK_SET_PRIVATE hook value
78530400f03SJulian Elischerand
7866e341a6dSRuslan Ermilov.Fn NG_HOOK_PRIVATE hook
7876e341a6dSRuslan Ermilovset and retrieve this property, respectively.
7886e341a6dSRuslan Ermilov.It \&An associate node
7896e341a6dSRuslan ErmilovThe macro
7906e341a6dSRuslan Ermilov.Fn NG_HOOK_NODE hook
7916e341a6dSRuslan Ermilovfinds the associated node.
7926e341a6dSRuslan Ermilov.It A peer hook Pq Vt hook_p
7936e341a6dSRuslan ErmilovThe other hook in this connected pair.
7946e341a6dSRuslan ErmilovThe
7956e341a6dSRuslan Ermilov.Fn NG_HOOK_PEER hook
7966e341a6dSRuslan Ermilovmacro finds the peer.
7976e341a6dSRuslan Ermilov.It References
7986e341a6dSRuslan ErmilovThe
7996e341a6dSRuslan Ermilov.Fn NG_HOOK_REF hook
80030400f03SJulian Elischerand
8016e341a6dSRuslan Ermilov.Fn NG_HOOK_UNREF hook
8026e341a6dSRuslan Ermilovmacros
80330400f03SJulian Elischerincrement and decrement the hook reference count accordingly.
8041acb27c6SJulian ElischerAfter decrement you should always assume the hook has been freed
8051acb27c6SJulian Elischerunless you have another reference still valid.
8066e341a6dSRuslan Ermilov.It Override receive functions
807c4b5eea4SJulian ElischerThe
808d0353b83SRuslan Ermilov.Fn NG_HOOK_SET_RCVDATA hook fn
809c4b5eea4SJulian Elischerand
810d0353b83SRuslan Ermilov.Fn NG_HOOK_SET_RCVMSG hook fn
8116e341a6dSRuslan Ermilovmacros can be used to set override methods that will be used in preference
8126e341a6dSRuslan Ermilovto the generic receive data and receive message functions.
8136e341a6dSRuslan ErmilovTo unset these, use the macros to set them to
8146e341a6dSRuslan Ermilov.Dv NULL .
8156e341a6dSRuslan ErmilovThey will only be used for data and
816c4b5eea4SJulian Elischermessages received on the hook on which they are set.
817069154d5SJulian Elischer.El
818069154d5SJulian Elischer.Pp
819069154d5SJulian ElischerThe maintenance of the names, reference counts, and linked list
8204cf49a43SJulian Elischerof hooks for each node is handled automatically by the
8214cf49a43SJulian Elischer.Nm
8224cf49a43SJulian Elischersubsystem.
8234cf49a43SJulian ElischerTypically a node's private info contains a back-pointer to the node or hook
824069154d5SJulian Elischerstructure, which counts as a new reference that must be included
8256e341a6dSRuslan Ermilovin the reference count for the node.
8266e341a6dSRuslan ErmilovWhen the node constructor is called,
827069154d5SJulian Elischerthere is already a reference for this calculated in, so that
828069154d5SJulian Elischerwhen the node is destroyed, it should remember to do a
82930400f03SJulian Elischer.Fn NG_NODE_UNREF
830069154d5SJulian Elischeron the node.
8314cf49a43SJulian Elischer.Pp
8324cf49a43SJulian ElischerFrom a hook you can obtain the corresponding node, and from
83330400f03SJulian Elischera node, it is possible to traverse all the active hooks.
8344cf49a43SJulian Elischer.Pp
835069154d5SJulian ElischerA current example of how to define a node can always be seen in
8366e341a6dSRuslan Ermilov.Pa src/sys/netgraph/ng_sample.c
837069154d5SJulian Elischerand should be used as a starting point for new node writers.
838adefd3f6SRuslan Ermilov.El
839b5e7e999SRuslan Ermilov.Ss Netgraph Message Structure
8404cf49a43SJulian ElischerControl messages have the following structure:
8414cf49a43SJulian Elischer.Bd -literal
8424131c351SGleb Smirnoff#define NG_CMDSTRSIZ    32      /* Max command string (including null) */
8434cf49a43SJulian Elischer
8444cf49a43SJulian Elischerstruct ng_mesg {
8454cf49a43SJulian Elischer  struct ng_msghdr {
8464cf49a43SJulian Elischer    u_char      version;        /* Must equal NG_VERSION */
8474131c351SGleb Smirnoff    u_char      spare;          /* Pad to 4 bytes */
8484131c351SGleb Smirnoff    uint16_t    spare2;
8494131c351SGleb Smirnoff    uint32_t    arglen;         /* Length of cmd/resp data */
8504131c351SGleb Smirnoff    uint32_t    cmd;            /* Command identifier */
8514131c351SGleb Smirnoff    uint32_t    flags;          /* Message status flags */
8524131c351SGleb Smirnoff    uint32_t    token;          /* Reply should have the same token */
8534131c351SGleb Smirnoff    uint32_t    typecookie;     /* Node type understanding this message */
8544131c351SGleb Smirnoff    u_char      cmdstr[NG_CMDSTRSIZ];  /* cmd string + \0 */
8554cf49a43SJulian Elischer  } header;
8564131c351SGleb Smirnoff  char  data[];                 /* placeholder for actual data */
8574cf49a43SJulian Elischer};
8584cf49a43SJulian Elischer
8594131c351SGleb Smirnoff#define NG_ABI_VERSION  12              /* Netgraph kernel ABI version */
8604131c351SGleb Smirnoff#define NG_VERSION      8               /* Netgraph message version */
8614131c351SGleb Smirnoff#define NGF_ORIG        0x00000000      /* The msg is the original request */
8624131c351SGleb Smirnoff#define NGF_RESP        0x00000001      /* The message is a response */
8634cf49a43SJulian Elischer.Ed
8644cf49a43SJulian Elischer.Pp
8654cf49a43SJulian ElischerControl messages have the fixed header shown above, followed by a
8664cf49a43SJulian Elischervariable length data section which depends on the type cookie
8676e341a6dSRuslan Ermilovand the command.
8686e341a6dSRuslan ErmilovEach field is explained below:
8696e341a6dSRuslan Ermilov.Bl -tag -width indent
8706e341a6dSRuslan Ermilov.It Va version
8716e341a6dSRuslan ErmilovIndicates the version of the
8726e341a6dSRuslan Ermilov.Nm
8736e341a6dSRuslan Ermilovmessage protocol itself.
8746e341a6dSRuslan ErmilovThe current version is
8754cf49a43SJulian Elischer.Dv NG_VERSION .
8766e341a6dSRuslan Ermilov.It Va arglen
8774cf49a43SJulian ElischerThis is the length of any extra arguments, which begin at
8786e341a6dSRuslan Ermilov.Va data .
8796e341a6dSRuslan Ermilov.It Va flags
8804cf49a43SJulian ElischerIndicates whether this is a command or a response control message.
8816e341a6dSRuslan Ermilov.It Va token
8824cf49a43SJulian ElischerThe
8836e341a6dSRuslan Ermilov.Va token
8844cf49a43SJulian Elischeris a means by which a sender can match a reply message to the
8854cf49a43SJulian Elischercorresponding command message; the reply always has the same token.
8866e341a6dSRuslan Ermilov.It Va typecookie
8874cf49a43SJulian ElischerThe corresponding node type's unique 32-bit value.
8886e341a6dSRuslan ErmilovIf a node does not recognize the type cookie it must reject the message
8894cf49a43SJulian Elischerby returning
8904cf49a43SJulian Elischer.Er EINVAL .
8914cf49a43SJulian Elischer.Pp
8924cf49a43SJulian ElischerEach type should have an include file that defines the commands,
8934cf49a43SJulian Elischerargument format, and cookie for its own messages.
8944cf49a43SJulian ElischerThe typecookie
895e7f8dd75SRebecca Cranensures that the same header file was included by both sender and
8964cf49a43SJulian Elischerreceiver; when an incompatible change in the header file is made,
8974cf49a43SJulian Elischerthe typecookie
8984cf49a43SJulian Elischer.Em must
8994cf49a43SJulian Elischerbe changed.
9006e341a6dSRuslan ErmilovThe de-facto method for generating unique type cookies is to take the
9016e341a6dSRuslan Ermilovseconds from the Epoch at the time the header file is written
9024cf49a43SJulian Elischer(i.e., the output of
9036e341a6dSRuslan Ermilov.Dq Nm date Fl u Li +%s ) .
9044cf49a43SJulian Elischer.Pp
9054cf49a43SJulian ElischerThere is a predefined typecookie
9064cf49a43SJulian Elischer.Dv NGM_GENERIC_COOKIE
90751291597SJulian Elischerfor the
9086e341a6dSRuslan Ermilov.Vt generic
90951291597SJulian Elischernode type, and
9104cf49a43SJulian Elischera corresponding set of generic messages which all nodes understand.
9114cf49a43SJulian ElischerThe handling of these messages is automatic.
9121c325cb9SGiorgos Keramidas.It Va cmd
9136e341a6dSRuslan ErmilovThe identifier for the message command.
9146e341a6dSRuslan ErmilovThis is type specific,
9154cf49a43SJulian Elischerand is defined in the same header file as the typecookie.
9166e341a6dSRuslan Ermilov.It Va cmdstr
91751291597SJulian ElischerRoom for a short human readable version of
9186e341a6dSRuslan Ermilov.Va command
91951291597SJulian Elischer(for debugging purposes only).
9204cf49a43SJulian Elischer.El
9214cf49a43SJulian Elischer.Pp
9224cf49a43SJulian ElischerSome modules may choose to implement messages from more than one
9234cf49a43SJulian Elischerof the header files and thus recognize more than one type cookie.
924b5e7e999SRuslan Ermilov.Ss Control Message ASCII Form
9256e341a6dSRuslan ErmilovControl messages are in binary format for efficiency.
9266e341a6dSRuslan ErmilovHowever, for
927f8307e12SArchie Cobbsdebugging and human interface purposes, and if the node type supports
92851291597SJulian Elischerit, control messages may be converted to and from an equivalent
92951291597SJulian Elischer.Tn ASCII
9306e341a6dSRuslan Ermilovform.
9316e341a6dSRuslan ErmilovThe
93251291597SJulian Elischer.Tn ASCII
93351291597SJulian Elischerform is similar to the binary form, with two exceptions:
9346e341a6dSRuslan Ermilov.Bl -enum
9356e341a6dSRuslan Ermilov.It
936f8307e12SArchie CobbsThe
9376e341a6dSRuslan Ermilov.Va cmdstr
93851291597SJulian Elischerheader field must contain the
93951291597SJulian Elischer.Tn ASCII
94051291597SJulian Elischername of the command, corresponding to the
9416e341a6dSRuslan Ermilov.Va cmd
942f8307e12SArchie Cobbsheader field.
9436e341a6dSRuslan Ermilov.It
9446e341a6dSRuslan ErmilovThe arguments field contains a
9456e341a6dSRuslan Ermilov.Dv NUL Ns
9466e341a6dSRuslan Ermilov-terminated
94751291597SJulian Elischer.Tn ASCII
94851291597SJulian Elischerstring version of the message arguments.
949f8307e12SArchie Cobbs.El
950f8307e12SArchie Cobbs.Pp
951f87717faSMike PritchardIn general, the arguments field of a control message can be any
9526e341a6dSRuslan Ermilovarbitrary C data type.
9536e341a6dSRuslan Ermilov.Nm Netgraph
9546e341a6dSRuslan Ermilovincludes parsing routines to support
95551291597SJulian Elischersome pre-defined datatypes in
95651291597SJulian Elischer.Tn ASCII
95751291597SJulian Elischerwith this simple syntax:
9586e341a6dSRuslan Ermilov.Bl -bullet
9596e341a6dSRuslan Ermilov.It
960f8307e12SArchie CobbsInteger types are represented by base 8, 10, or 16 numbers.
9616e341a6dSRuslan Ermilov.It
962f8307e12SArchie CobbsStrings are enclosed in double quotes and respect the normal
963f8307e12SArchie CobbsC language backslash escapes.
9646e341a6dSRuslan Ermilov.It
965f8307e12SArchie CobbsIP addresses have the obvious form.
9666e341a6dSRuslan Ermilov.It
967f8307e12SArchie CobbsArrays are enclosed in square brackets, with the elements listed
9686e341a6dSRuslan Ermilovconsecutively starting at index zero.
9696e341a6dSRuslan ErmilovAn element may have an optional index and equals sign
9706e341a6dSRuslan Ermilov.Pq Ql =
9716e341a6dSRuslan Ermilovpreceding it.
9726e341a6dSRuslan ErmilovWhenever an element
973f8307e12SArchie Cobbsdoes not have an explicit index, the index is implicitly the previous
974f8307e12SArchie Cobbselement's index plus one.
9756e341a6dSRuslan Ermilov.It
976f8307e12SArchie CobbsStructures are enclosed in curly braces, and each field is specified
97751291597SJulian Elischerin the form
9786e341a6dSRuslan Ermilov.Ar fieldname Ns = Ns Ar value .
9796e341a6dSRuslan Ermilov.It
980f8307e12SArchie CobbsAny array element or structure field whose value is equal to its
98151291597SJulian Elischer.Dq default value
9826e341a6dSRuslan Ermilovmay be omitted.
9836e341a6dSRuslan ErmilovFor integer types, the default value
984f8307e12SArchie Cobbsis usually zero; for string types, the empty string.
9856e341a6dSRuslan Ermilov.It
986f8307e12SArchie CobbsArray elements and structure fields may be specified in any order.
987f8307e12SArchie Cobbs.El
988f8307e12SArchie Cobbs.Pp
989f8307e12SArchie CobbsEach node type may define its own arbitrary types by providing
99051291597SJulian Elischerthe necessary routines to parse and unparse.
99151291597SJulian Elischer.Tn ASCII
99251291597SJulian Elischerforms defined
9936e341a6dSRuslan Ermilovfor a specific node type are documented in the corresponding man page.
994b5e7e999SRuslan Ermilov.Ss Generic Control Messages
9954cf49a43SJulian ElischerThere are a number of standard predefined messages that will work
9964cf49a43SJulian Elischerfor any node, as they are supported directly by the framework itself.
9974cf49a43SJulian ElischerThese are defined in
9986e341a6dSRuslan Ermilov.In netgraph/ng_message.h
9994cf49a43SJulian Elischeralong with the basic layout of messages and other similar information.
10006e341a6dSRuslan Ermilov.Bl -tag -width indent
10014cf49a43SJulian Elischer.It Dv NGM_CONNECT
10024cf49a43SJulian ElischerConnect to another node, using the supplied hook names on either end.
10034cf49a43SJulian Elischer.It Dv NGM_MKPEER
10044cf49a43SJulian ElischerConstruct a node of the given type and then connect to it using the
10054cf49a43SJulian Elischersupplied hook names.
10064cf49a43SJulian Elischer.It Dv NGM_SHUTDOWN
10074cf49a43SJulian ElischerThe target node should disconnect from all its neighbours and shut down.
10084cf49a43SJulian ElischerPersistent nodes such as those representing physical hardware
10097f9d26bdSMike Pritchardmight not disappear from the node namespace, but only reset themselves.
10104cf49a43SJulian ElischerThe node must disconnect all of its hooks.
10114cf49a43SJulian ElischerThis may result in neighbors shutting themselves down, and possibly a
10124cf49a43SJulian Elischercascading shutdown of the entire connected graph.
10134cf49a43SJulian Elischer.It Dv NGM_NAME
10146e341a6dSRuslan ErmilovAssign a name to a node.
10156e341a6dSRuslan ErmilovNodes can exist without having a name, and this
10164cf49a43SJulian Elischeris the default for nodes created using the
10174cf49a43SJulian Elischer.Dv NGM_MKPEER
10186e341a6dSRuslan Ermilovmethod.
10196e341a6dSRuslan ErmilovSuch nodes can only be addressed relatively or by their ID number.
10204cf49a43SJulian Elischer.It Dv NGM_RMHOOK
10214cf49a43SJulian ElischerAsk the node to break a hook connection to one of its neighbours.
102251291597SJulian ElischerBoth nodes will have their
102351291597SJulian Elischer.Dq disconnect
102451291597SJulian Elischermethod invoked.
10254cf49a43SJulian ElischerEither node may elect to totally shut down as a result.
10264cf49a43SJulian Elischer.It Dv NGM_NODEINFO
10276e341a6dSRuslan ErmilovAsks the target node to describe itself.
10286e341a6dSRuslan ErmilovThe four returned fields
10294cf49a43SJulian Elischerare the node name (if named), the node type, the node ID and the
10306e341a6dSRuslan Ermilovnumber of hooks attached.
10316e341a6dSRuslan ErmilovThe ID is an internal number unique to that node.
10324cf49a43SJulian Elischer.It Dv NGM_LISTHOOKS
10334cf49a43SJulian ElischerThis returns the information given by
10344cf49a43SJulian Elischer.Dv NGM_NODEINFO ,
10354cf49a43SJulian Elischerbut in addition
10367f9d26bdSMike Pritchardincludes an array of fields describing each link, and the description for
10374cf49a43SJulian Elischerthe node at the far end of that link.
10384cf49a43SJulian Elischer.It Dv NGM_LISTNAMES
10394cf49a43SJulian ElischerThis returns an array of node descriptions (as for
10406e341a6dSRuslan Ermilov.Dv NGM_NODEINFO )
10414cf49a43SJulian Elischerwhere each entry of the array describes a named node.
10424cf49a43SJulian ElischerAll named nodes will be described.
10434cf49a43SJulian Elischer.It Dv NGM_LISTNODES
10444cf49a43SJulian ElischerThis is the same as
10454cf49a43SJulian Elischer.Dv NGM_LISTNAMES
10464cf49a43SJulian Elischerexcept that all nodes are listed regardless of whether they have a name or not.
10474cf49a43SJulian Elischer.It Dv NGM_LISTTYPES
10486e341a6dSRuslan ErmilovThis returns a list of all currently installed
10496e341a6dSRuslan Ermilov.Nm
10506e341a6dSRuslan Ermilovtypes.
10514cf49a43SJulian Elischer.It Dv NGM_TEXT_STATUS
10524cf49a43SJulian ElischerThe node may return a text formatted status message.
10534cf49a43SJulian ElischerThe status information is determined entirely by the node type.
10546e341a6dSRuslan ErmilovIt is the only
10556e341a6dSRuslan Ermilov.Dq generic
10566e341a6dSRuslan Ermilovmessage
10574cf49a43SJulian Elischerthat requires any support within the node itself and as such the node may
10586e341a6dSRuslan Ermilovelect to not support this message.
10596e341a6dSRuslan ErmilovThe text response must be less than
10604cf49a43SJulian Elischer.Dv NG_TEXTRESPONSE
10616e341a6dSRuslan Ermilovbytes in length (presently 1024).
10626e341a6dSRuslan ErmilovThis can be used to return general
10634cf49a43SJulian Elischerstatus information in human readable form.
1064f8307e12SArchie Cobbs.It Dv NGM_BINARY2ASCII
106551291597SJulian ElischerThis message converts a binary control message to its
106651291597SJulian Elischer.Tn ASCII
106751291597SJulian Elischerform.
1068f8307e12SArchie CobbsThe entire control message to be converted is contained within the
1069f8307e12SArchie Cobbsarguments field of the
1070c2d03ea8SRuslan Ermilov.Dv NGM_BINARY2ASCII
10716e341a6dSRuslan Ermilovmessage itself.
10726e341a6dSRuslan ErmilovIf successful, the reply will contain the same control
107351291597SJulian Elischermessage in
107451291597SJulian Elischer.Tn ASCII
107551291597SJulian Elischerform.
1076f8307e12SArchie CobbsA node will typically only know how to translate messages that it
1077ac83689eSArchie Cobbsitself understands, so the target node of the
1078f8307e12SArchie Cobbs.Dv NGM_BINARY2ASCII
1079f8307e12SArchie Cobbsis often the same node that would actually receive that message.
1080f8307e12SArchie Cobbs.It Dv NGM_ASCII2BINARY
1081f8307e12SArchie CobbsThe opposite of
1082f8307e12SArchie Cobbs.Dv NGM_BINARY2ASCII .
108351291597SJulian ElischerThe entire control message to be converted, in
108451291597SJulian Elischer.Tn ASCII
108551291597SJulian Elischerform, is contained
1086f8307e12SArchie Cobbsin the arguments section of the
1087f8307e12SArchie Cobbs.Dv NGM_ASCII2BINARY
1088f8307e12SArchie Cobbsand need only have the
10896e341a6dSRuslan Ermilov.Va flags , cmdstr ,
1090f8307e12SArchie Cobbsand
10916e341a6dSRuslan Ermilov.Va arglen
10926e341a6dSRuslan Ermilovheader fields filled in, plus the
10936e341a6dSRuslan Ermilov.Dv NUL Ns
10946e341a6dSRuslan Ermilov-terminated string version of
10956e341a6dSRuslan Ermilovthe arguments in the arguments field.
10966e341a6dSRuslan ErmilovIf successful, the reply
1097f8307e12SArchie Cobbscontains the binary version of the control message.
10984cf49a43SJulian Elischer.El
1099b5e7e999SRuslan Ermilov.Ss Flow Control Messages
1100859a4d16SJulian ElischerIn addition to the control messages that affect nodes with respect to the
1101859a4d16SJulian Elischergraph, there are also a number of
11026e341a6dSRuslan Ermilov.Em flow control
11036e341a6dSRuslan Ermilovmessages defined.
11046e341a6dSRuslan ErmilovAt present these are
11056e341a6dSRuslan Ermilov.Em not
1106859a4d16SJulian Elischerhandled automatically by the system, so
1107859a4d16SJulian Elischernodes need to handle them if they are going to be used in a graph utilising
1108b5e7e999SRuslan Ermilovflow control, and will be in the likely path of these messages.
11096e341a6dSRuslan ErmilovThe default action of a node that does not understand these messages should
1110b5e7e999SRuslan Ermilovbe to pass them onto the next node.
1111b5e7e999SRuslan ErmilovHopefully some helper functions will assist in this eventually.
1112b5e7e999SRuslan ErmilovThese messages are also defined in
11136e341a6dSRuslan Ermilov.In netgraph/ng_message.h
1114859a4d16SJulian Elischerand have a separate cookie
11156e341a6dSRuslan Ermilov.Dv NG_FLOW_COOKIE
1116b5e7e999SRuslan Ermilovto help identify them.
1117b5e7e999SRuslan ErmilovThey will not be covered in depth here.
11184cf49a43SJulian Elischer.Sh INITIALIZATION
11194cf49a43SJulian ElischerThe base
11204cf49a43SJulian Elischer.Nm
11214cf49a43SJulian Elischercode may either be statically compiled
11224cf49a43SJulian Elischerinto the kernel or else loaded dynamically as a KLD via
11234cf49a43SJulian Elischer.Xr kldload 8 .
11244cf49a43SJulian ElischerIn the former case, include
11253136363fSRuslan Ermilov.Pp
11266e341a6dSRuslan Ermilov.D1 Cd "options NETGRAPH"
11273136363fSRuslan Ermilov.Pp
1128b5e7e999SRuslan Ermilovin your kernel configuration file.
1129b5e7e999SRuslan ErmilovYou may also include selected
11304cf49a43SJulian Elischernode types in the kernel compilation, for example:
11316e341a6dSRuslan Ermilov.Pp
11326e341a6dSRuslan Ermilov.D1 Cd "options NETGRAPH"
11336e341a6dSRuslan Ermilov.D1 Cd "options NETGRAPH_SOCKET"
11346e341a6dSRuslan Ermilov.D1 Cd "options NETGRAPH_ECHO"
11354cf49a43SJulian Elischer.Pp
11364cf49a43SJulian ElischerOnce the
11374cf49a43SJulian Elischer.Nm
11384cf49a43SJulian Elischersubsystem is loaded, individual node types may be loaded at any time
11394cf49a43SJulian Elischeras KLD modules via
11404cf49a43SJulian Elischer.Xr kldload 8 .
11414cf49a43SJulian ElischerMoreover,
11424cf49a43SJulian Elischer.Nm
11434cf49a43SJulian Elischerknows how to automatically do this; when a request to create a new
11444cf49a43SJulian Elischernode of unknown type
11456e341a6dSRuslan Ermilov.Ar type
11464cf49a43SJulian Elischeris made,
11474cf49a43SJulian Elischer.Nm
11484cf49a43SJulian Elischerwill attempt to load the KLD module
11496e341a6dSRuslan Ermilov.Pa ng_ Ns Ao Ar type Ac Ns Pa .ko .
11504cf49a43SJulian Elischer.Pp
11514cf49a43SJulian ElischerTypes can also be installed at boot time, as certain device drivers
11526e341a6dSRuslan Ermilovmay want to export each instance of the device as a
11536e341a6dSRuslan Ermilov.Nm
11546e341a6dSRuslan Ermilovnode.
11554cf49a43SJulian Elischer.Pp
11564cf49a43SJulian ElischerIn general, new types can be installed at any time from within the
11574cf49a43SJulian Elischerkernel by calling
115851291597SJulian Elischer.Fn ng_newtype ,
11594cf49a43SJulian Elischersupplying a pointer to the type's
11606e341a6dSRuslan Ermilov.Vt "struct ng_type"
11614cf49a43SJulian Elischerstructure.
11624cf49a43SJulian Elischer.Pp
11634cf49a43SJulian ElischerThe
116451291597SJulian Elischer.Fn NETGRAPH_INIT
11654cf49a43SJulian Elischermacro automates this process by using a linker set.
11664cf49a43SJulian Elischer.Sh EXISTING NODE TYPES
1167b5e7e999SRuslan ErmilovSeveral node types currently exist.
1168b5e7e999SRuslan ErmilovEach is fully documented in its own man page:
11696e341a6dSRuslan Ermilov.Bl -tag -width indent
11704cf49a43SJulian Elischer.It SOCKET
11714cf49a43SJulian ElischerThe socket type implements two new sockets in the new protocol domain
11724cf49a43SJulian Elischer.Dv PF_NETGRAPH .
11734cf49a43SJulian ElischerThe new sockets protocols are
11744cf49a43SJulian Elischer.Dv NG_DATA
11754cf49a43SJulian Elischerand
11764cf49a43SJulian Elischer.Dv NG_CONTROL ,
11774cf49a43SJulian Elischerboth of type
11784cf49a43SJulian Elischer.Dv SOCK_DGRAM .
11794cf49a43SJulian ElischerTypically one of each is associated with a socket node.
1180b5e7e999SRuslan ErmilovWhen both sockets have closed, the node will shut down.
1181b5e7e999SRuslan ErmilovThe
11824cf49a43SJulian Elischer.Dv NG_DATA
11834cf49a43SJulian Elischersocket is used for sending and receiving data, while the
11844cf49a43SJulian Elischer.Dv NG_CONTROL
11854cf49a43SJulian Elischersocket is used for sending and receiving control messages.
11864cf49a43SJulian ElischerData and control messages are passed using the
11874cf49a43SJulian Elischer.Xr sendto 2
11884cf49a43SJulian Elischerand
11894cf49a43SJulian Elischer.Xr recvfrom 2
11906e341a6dSRuslan Ermilovsystem calls, using a
11916e341a6dSRuslan Ermilov.Vt "struct sockaddr_ng"
11924cf49a43SJulian Elischersocket address.
11934cf49a43SJulian Elischer.It HOLE
119451291597SJulian ElischerResponds only to generic messages and is a
119551291597SJulian Elischer.Dq black hole
11966e341a6dSRuslan Ermilovfor data.
11976e341a6dSRuslan ErmilovUseful for testing.
11986e341a6dSRuslan ErmilovAlways accepts new hooks.
11994cf49a43SJulian Elischer.It ECHO
12004cf49a43SJulian ElischerResponds only to generic messages and always echoes data back through the
12016e341a6dSRuslan Ermilovhook from which it arrived.
12026e341a6dSRuslan ErmilovReturns any non-generic messages as their own response.
12036e341a6dSRuslan ErmilovUseful for testing.
12046e341a6dSRuslan ErmilovAlways accepts new hooks.
12054cf49a43SJulian Elischer.It TEE
120651291597SJulian ElischerThis node is useful for
120751291597SJulian Elischer.Dq snooping .
120851291597SJulian ElischerIt has 4 hooks:
12096e341a6dSRuslan Ermilov.Va left , right , left2right ,
12104cf49a43SJulian Elischerand
12116e341a6dSRuslan Ermilov.Va right2left .
12126e341a6dSRuslan ErmilovData entering from the
12136e341a6dSRuslan Ermilov.Va right
12146e341a6dSRuslan Ermilovis passed to the
12156e341a6dSRuslan Ermilov.Va left
12166e341a6dSRuslan Ermilovand duplicated on
12176e341a6dSRuslan Ermilov.Va right2left ,
12186e341a6dSRuslan Ermilovand data entering from the
12196e341a6dSRuslan Ermilov.Va left
12206e341a6dSRuslan Ermilovis passed to the
12216e341a6dSRuslan Ermilov.Va right
12226e341a6dSRuslan Ermilovand duplicated on
12236e341a6dSRuslan Ermilov.Va left2right .
12244cf49a43SJulian ElischerData entering from
12256e341a6dSRuslan Ermilov.Va left2right
12266e341a6dSRuslan Ermilovis sent to the
12276e341a6dSRuslan Ermilov.Va right
12286e341a6dSRuslan Ermilovand data from
12296e341a6dSRuslan Ermilov.Va right2left
12306e341a6dSRuslan Ermilovto
12316e341a6dSRuslan Ermilov.Va left .
12324cf49a43SJulian Elischer.It RFC1490 MUX
12334cf49a43SJulian ElischerEncapsulates/de-encapsulates frames encoded according to RFC 1490.
123451291597SJulian ElischerHas a hook for the encapsulated packets
12356e341a6dSRuslan Ermilov.Pq Va downstream
123651291597SJulian Elischerand one hook
12374cf49a43SJulian Elischerfor each protocol (i.e., IP, PPP, etc.).
12384cf49a43SJulian Elischer.It FRAME RELAY MUX
12394cf49a43SJulian ElischerEncapsulates/de-encapsulates Frame Relay frames.
124051291597SJulian ElischerHas a hook for the encapsulated packets
12416e341a6dSRuslan Ermilov.Pq Va downstream
124251291597SJulian Elischerand one hook
12434cf49a43SJulian Elischerfor each DLCI.
12444cf49a43SJulian Elischer.It FRAME RELAY LMI
12454cf49a43SJulian ElischerAutomatically handles frame relay
124651291597SJulian Elischer.Dq LMI
124751291597SJulian Elischer(link management interface) operations and packets.
12487f9d26bdSMike PritchardAutomatically probes and detects which of several LMI standards
12494cf49a43SJulian Elischeris in use at the exchange.
12504cf49a43SJulian Elischer.It TTY
12516e341a6dSRuslan ErmilovThis node is also a line discipline.
12526e341a6dSRuslan ErmilovIt simply converts between
12536e341a6dSRuslan Ermilov.Vt mbuf
12546e341a6dSRuslan Ermilovframes and sequential serial data, allowing a TTY to appear as a
12556e341a6dSRuslan Ermilov.Nm
12566e341a6dSRuslan Ermilovnode.
12576e341a6dSRuslan ErmilovIt has a programmable
125851291597SJulian Elischer.Dq hotkey
125951291597SJulian Elischercharacter.
12604cf49a43SJulian Elischer.It ASYNC
12614cf49a43SJulian ElischerThis node encapsulates and de-encapsulates asynchronous frames
12626e341a6dSRuslan Ermilovaccording to RFC 1662.
12636e341a6dSRuslan ErmilovThis is used in conjunction with the TTY node
12644cf49a43SJulian Elischertype for supporting PPP links over asynchronous serial lines.
126587757985SRuslan Ermilov.It ETHERNET
126687757985SRuslan ErmilovThis node is attached to every Ethernet interface in the system.
126787757985SRuslan ErmilovIt allows capturing raw Ethernet frames from the network, as well as
126887757985SRuslan Ermilovsending frames out of the interface.
12694cf49a43SJulian Elischer.It INTERFACE
12706e341a6dSRuslan ErmilovThis node is also a system networking interface.
127145c203fcSGleb SmirnoffIt has hooks representing each protocol family (IP, IPv6)
127245c203fcSGleb Smirnoffand appears in the output of
12734cf49a43SJulian Elischer.Xr ifconfig 8 .
12744cf49a43SJulian ElischerThe interfaces are named
12756e341a6dSRuslan Ermilov.Dq Li ng0 ,
12766e341a6dSRuslan Ermilov.Dq Li ng1 ,
12774cf49a43SJulian Elischeretc.
1278859a4d16SJulian Elischer.It ONE2MANY
12796e341a6dSRuslan ErmilovThis node implements a simple round-robin multiplexer.
12806e341a6dSRuslan ErmilovIt can be used
1281859a4d16SJulian Elischerfor example to make several LAN ports act together to get a higher speed
1282859a4d16SJulian Elischerlink between two machines.
12836e341a6dSRuslan Ermilov.It Various PPP related nodes
12846e341a6dSRuslan ErmilovThere is a full multilink PPP implementation that runs in
12856e341a6dSRuslan Ermilov.Nm .
1286859a4d16SJulian ElischerThe
12873d94c26eSAlexander Motin.Pa net/mpd5
1288859a4d16SJulian Elischerport can use these modules to make a very low latency high
12896e341a6dSRuslan Ermilovcapacity PPP system.
12906e341a6dSRuslan ErmilovIt also supports
12916e341a6dSRuslan Ermilov.Tn PPTP
12926e341a6dSRuslan ErmilovVPNs using the PPTP node.
1293859a4d16SJulian Elischer.It PPPOE
12946e341a6dSRuslan ErmilovA server and client side implementation of PPPoE.
12956e341a6dSRuslan ErmilovUsed in conjunction with
1296859a4d16SJulian Elischereither
1297859a4d16SJulian Elischer.Xr ppp 8
1298859a4d16SJulian Elischeror the
12994131c351SGleb Smirnoff.Pa net/mpd5
13006e341a6dSRuslan Ermilovport.
1301859a4d16SJulian Elischer.It BRIDGE
13026e341a6dSRuslan ErmilovThis node, together with the Ethernet nodes, allows a very flexible
1303859a4d16SJulian Elischerbridging system to be implemented.
1304859a4d16SJulian Elischer.It KSOCKET
1305859a4d16SJulian ElischerThis intriguing node looks like a socket to the system but diverts
13066e341a6dSRuslan Ermilovall data to and from the
13076e341a6dSRuslan Ermilov.Nm
13086e341a6dSRuslan Ermilovsystem for further processing.
13096e341a6dSRuslan ErmilovThis allows
1310859a4d16SJulian Elischersuch things as UDP tunnels to be almost trivially implemented from the
1311859a4d16SJulian Elischercommand line.
13124cf49a43SJulian Elischer.El
1313859a4d16SJulian Elischer.Pp
1314859a4d16SJulian ElischerRefer to the section at the end of this man page for more nodes types.
13154cf49a43SJulian Elischer.Sh NOTES
13167f9d26bdSMike PritchardWhether a named node exists can be checked by trying to send a control message
13174cf49a43SJulian Elischerto it (e.g.,
131894ba280cSRuslan Ermilov.Dv NGM_NODEINFO ) .
13194cf49a43SJulian ElischerIf it does not exist,
13204cf49a43SJulian Elischer.Er ENOENT
13214cf49a43SJulian Elischerwill be returned.
13224cf49a43SJulian Elischer.Pp
13236e341a6dSRuslan ErmilovAll data messages are
13246e341a6dSRuslan Ermilov.Vt mbuf chains
13256e341a6dSRuslan Ermilovwith the
13266e341a6dSRuslan Ermilov.Dv M_PKTHDR
13276e341a6dSRuslan Ermilovflag set.
13284cf49a43SJulian Elischer.Pp
13294cf49a43SJulian ElischerNodes are responsible for freeing what they allocate.
13304cf49a43SJulian ElischerThere are three exceptions:
13316e341a6dSRuslan Ermilov.Bl -enum
13326e341a6dSRuslan Ermilov.It
13336e341a6dSRuslan Ermilov.Vt Mbufs
13346e341a6dSRuslan Ermilovsent across a data link are never to be freed by the sender.
13356e341a6dSRuslan ErmilovIn the
1336069154d5SJulian Elischercase of error, they should be considered freed.
13376e341a6dSRuslan Ermilov.It
133887757985SRuslan ErmilovMessages sent using one of
133987757985SRuslan Ermilov.Fn NG_SEND_MSG_*
134087757985SRuslan Ermilovfamily macros are freed by the recipient.
134187757985SRuslan ErmilovAs in the case above, the addresses
13424cf49a43SJulian Elischerassociated with the message are freed by whatever allocated them so the
13434cf49a43SJulian Elischerrecipient should copy them if it wants to keep that information.
13446e341a6dSRuslan Ermilov.It
13456e341a6dSRuslan ErmilovBoth control messages and data are delivered and queued with a
13466e341a6dSRuslan Ermilov.Nm
1347069154d5SJulian Elischer.Em item .
1348069154d5SJulian ElischerThe item must be freed using
13496e341a6dSRuslan Ermilov.Fn NG_FREE_ITEM item
1350069154d5SJulian Elischeror passed on to another node.
13514cf49a43SJulian Elischer.El
13524cf49a43SJulian Elischer.Sh FILES
13536e341a6dSRuslan Ermilov.Bl -tag -width indent
13546e341a6dSRuslan Ermilov.It In netgraph/netgraph.h
13557f9d26bdSMike PritchardDefinitions for use solely within the kernel by
13564cf49a43SJulian Elischer.Nm
13574cf49a43SJulian Elischernodes.
13586e341a6dSRuslan Ermilov.It In netgraph/ng_message.h
13594cf49a43SJulian ElischerDefinitions needed by any file that needs to deal with
13604cf49a43SJulian Elischer.Nm
13614cf49a43SJulian Elischermessages.
13626e341a6dSRuslan Ermilov.It In netgraph/ng_socket.h
13634cf49a43SJulian ElischerDefinitions needed to use
13644cf49a43SJulian Elischer.Nm
13656e341a6dSRuslan Ermilov.Vt socket
13666e341a6dSRuslan Ermilovtype nodes.
13676e341a6dSRuslan Ermilov.It In netgraph/ng_ Ns Ao Ar type Ac Ns Pa .h
13684cf49a43SJulian ElischerDefinitions needed to use
13694cf49a43SJulian Elischer.Nm
13706e341a6dSRuslan Ermilov.Ar type
13714cf49a43SJulian Elischernodes, including the type cookie definition.
1372d59dfc2eSRuslan Ermilov.It Pa /boot/kernel/netgraph.ko
13736e341a6dSRuslan ErmilovThe
13746e341a6dSRuslan Ermilov.Nm
13756e341a6dSRuslan Ermilovsubsystem loadable KLD module.
13766e341a6dSRuslan Ermilov.It Pa /boot/kernel/ng_ Ns Ao Ar type Ac Ns Pa .ko
13776e341a6dSRuslan ErmilovLoadable KLD module for node type
13786e341a6dSRuslan Ermilov.Ar type .
13796e341a6dSRuslan Ermilov.It Pa src/sys/netgraph/ng_sample.c
13806e341a6dSRuslan ErmilovSkeleton
13816e341a6dSRuslan Ermilov.Nm
13826e341a6dSRuslan Ermilovnode.
1383c5083414SRuslan ErmilovUse this as a starting point for new node types.
13844cf49a43SJulian Elischer.El
13854cf49a43SJulian Elischer.Sh USER MODE SUPPORT
13864cf49a43SJulian ElischerThere is a library for supporting user-mode programs that wish
13876e341a6dSRuslan Ermilovto interact with the
13886e341a6dSRuslan Ermilov.Nm
13896e341a6dSRuslan Ermilovsystem.
13906e341a6dSRuslan ErmilovSee
13914cf49a43SJulian Elischer.Xr netgraph 3
13924cf49a43SJulian Elischerfor details.
13934cf49a43SJulian Elischer.Pp
13944cf49a43SJulian ElischerTwo user-mode support programs,
13954cf49a43SJulian Elischer.Xr ngctl 8
13964cf49a43SJulian Elischerand
13974cf49a43SJulian Elischer.Xr nghook 8 ,
13984cf49a43SJulian Elischerare available to assist manual configuration and debugging.
13994cf49a43SJulian Elischer.Pp
14004cf49a43SJulian ElischerThere are a few useful techniques for debugging new node types.
14014cf49a43SJulian ElischerFirst, implementing new node types in user-mode first
14024cf49a43SJulian Elischermakes debugging easier.
14034cf49a43SJulian ElischerThe
14046e341a6dSRuslan Ermilov.Vt tee
14054cf49a43SJulian Elischernode type is also useful for debugging, especially in conjunction with
14064cf49a43SJulian Elischer.Xr ngctl 8
14074cf49a43SJulian Elischerand
14084cf49a43SJulian Elischer.Xr nghook 8 .
1409859a4d16SJulian Elischer.Pp
14106e341a6dSRuslan ErmilovAlso look in
14116e341a6dSRuslan Ermilov.Pa /usr/share/examples/netgraph
14126e341a6dSRuslan Ermilovfor solutions to several
1413859a4d16SJulian Elischercommon networking problems, solved using
1414859a4d16SJulian Elischer.Nm .
14154cf49a43SJulian Elischer.Sh SEE ALSO
14164cf49a43SJulian Elischer.Xr socket 2 ,
14174cf49a43SJulian Elischer.Xr netgraph 3 ,
1418bec35b9aSArchie Cobbs.Xr ng_async 4 ,
1419299738e7SRalf S. Engelschall.Xr ng_bluetooth 4 ,
1420bec35b9aSArchie Cobbs.Xr ng_bpf 4 ,
14215521ff5aSRuslan Ermilov.Xr ng_bridge 4 ,
1422299738e7SRalf S. Engelschall.Xr ng_btsocket 4 ,
14233d94c26eSAlexander Motin.Xr ng_car 4 ,
1424bec35b9aSArchie Cobbs.Xr ng_cisco 4 ,
1425299738e7SRalf S. Engelschall.Xr ng_device 4 ,
1426bec35b9aSArchie Cobbs.Xr ng_echo 4 ,
1427299738e7SRalf S. Engelschall.Xr ng_eiface 4 ,
1428299738e7SRalf S. Engelschall.Xr ng_etf 4 ,
1429859a4d16SJulian Elischer.Xr ng_ether 4 ,
1430bec35b9aSArchie Cobbs.Xr ng_frame_relay 4 ,
1431299738e7SRalf S. Engelschall.Xr ng_gif 4 ,
1432299738e7SRalf S. Engelschall.Xr ng_gif_demux 4 ,
1433299738e7SRalf S. Engelschall.Xr ng_hci 4 ,
1434bec35b9aSArchie Cobbs.Xr ng_hole 4 ,
1435299738e7SRalf S. Engelschall.Xr ng_hub 4 ,
1436bec35b9aSArchie Cobbs.Xr ng_iface 4 ,
1437299738e7SRalf S. Engelschall.Xr ng_ip_input 4 ,
14383d94c26eSAlexander Motin.Xr ng_ipfw 4 ,
1439bec35b9aSArchie Cobbs.Xr ng_ksocket 4 ,
1440299738e7SRalf S. Engelschall.Xr ng_l2cap 4 ,
1441299738e7SRalf S. Engelschall.Xr ng_l2tp 4 ,
1442bec35b9aSArchie Cobbs.Xr ng_lmi 4 ,
1443bec35b9aSArchie Cobbs.Xr ng_mppc 4 ,
14443d94c26eSAlexander Motin.Xr ng_nat 4 ,
1445299738e7SRalf S. Engelschall.Xr ng_netflow 4 ,
1446299738e7SRalf S. Engelschall.Xr ng_one2many 4 ,
14473d94c26eSAlexander Motin.Xr ng_patch 4 ,
1448bec35b9aSArchie Cobbs.Xr ng_ppp 4 ,
1449bec35b9aSArchie Cobbs.Xr ng_pppoe 4 ,
1450859a4d16SJulian Elischer.Xr ng_pptpgre 4 ,
1451bec35b9aSArchie Cobbs.Xr ng_rfc1490 4 ,
1452bec35b9aSArchie Cobbs.Xr ng_socket 4 ,
1453299738e7SRalf S. Engelschall.Xr ng_split 4 ,
1454bec35b9aSArchie Cobbs.Xr ng_tee 4 ,
1455bec35b9aSArchie Cobbs.Xr ng_tty 4 ,
1456299738e7SRalf S. Engelschall.Xr ng_ubt 4 ,
1457bec35b9aSArchie Cobbs.Xr ng_UI 4 ,
1458bec35b9aSArchie Cobbs.Xr ng_vjc 4 ,
1459299738e7SRalf S. Engelschall.Xr ng_vlan 4 ,
14604cf49a43SJulian Elischer.Xr ngctl 8 ,
1461bec35b9aSArchie Cobbs.Xr nghook 8
14624cf49a43SJulian Elischer.Sh HISTORY
14634cf49a43SJulian ElischerThe
14644cf49a43SJulian Elischer.Nm
146557e4378bSRuslan Ermilovsystem was designed and first implemented at Whistle Communications, Inc.\&
1466958b92d2SBill Fumerolain a version of
146751291597SJulian Elischer.Fx 2.2
146851291597SJulian Elischercustomized for the Whistle InterJet.
1469958b92d2SBill FumerolaIt first made its debut in the main tree in
1470958b92d2SBill Fumerola.Fx 3.4 .
14714cf49a43SJulian Elischer.Sh AUTHORS
1472f4d874a1SRuslan Ermilov.An -nosplit
14736c899950SBaptiste Daroussin.An Julian Elischer Aq Mt julian@FreeBSD.org ,
147451291597SJulian Elischerwith contributions by
14756c899950SBaptiste Daroussin.An Archie Cobbs Aq Mt archie@FreeBSD.org .
1476