ng_source.c revision 125033
1106266Sjulian/*
2106266Sjulian * ng_source.c
3106266Sjulian *
4106266Sjulian * Copyright 2002 Sandvine Inc.
5106266Sjulian * All rights reserved.
6106266Sjulian *
7106266Sjulian * Subject to the following obligations and disclaimer of warranty, use and
8106266Sjulian * redistribution of this software, in source or object code forms, with or
9106319Sjulian * without modifications are expressly permitted by Sandvine Inc.; provided,
10106266Sjulian * however, that:
11106319Sjulian * 1. Any and all reproductions of the source or object code must include the
12106319Sjulian *    copyright notice above and the following disclaimer of warranties; and
13106266Sjulian * 2. No rights are granted, in any manner or form, to use Sandvine Inc.
14106319Sjulian *    trademarks, including the mark "SANDVINE" on advertising, endorsements,
15106319Sjulian *    or otherwise except as such appears in the above copyright notice or in
16106266Sjulian *    the software.
17106266Sjulian *
18106266Sjulian * THIS SOFTWARE IS BEING PROVIDED BY SANDVINE "AS IS", AND TO THE MAXIMUM
19106319Sjulian * EXTENT PERMITTED BY LAW, SANDVINE MAKES NO REPRESENTATIONS OR WARRANTIES,
20106319Sjulian * EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, INCLUDING WITHOUT LIMITATION,
21106319Sjulian * ANY AND ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22106266Sjulian * PURPOSE, OR NON-INFRINGEMENT.  SANDVINE DOES NOT WARRANT, GUARANTEE, OR
23106266Sjulian * MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE
24106266Sjulian * USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY
25106266Sjulian * OR OTHERWISE.  IN NO EVENT SHALL SANDVINE BE LIABLE FOR ANY DAMAGES
26106266Sjulian * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
27106319Sjulian * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28106266Sjulian * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
29106266Sjulian * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
30106266Sjulian * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31106266Sjulian * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32106266Sjulian * THIS SOFTWARE, EVEN IF SANDVINE IS ADVISED OF THE POSSIBILITY OF SUCH
33106266Sjulian * DAMAGE.
34106266Sjulian *
35106266Sjulian * Author: Dave Chapeskie <dchapeskie@sandvine.com>
36106266Sjulian *
37106266Sjulian * $FreeBSD: head/sys/netgraph/ng_source.c 125033 2004-01-26 14:53:16Z harti $
38106266Sjulian */
39106266Sjulian
40106266Sjulian/*
41106266Sjulian * This node is used for high speed packet geneneration.  It queues
42106266Sjulian * all data recieved on it's 'input' hook and when told to start via
43106266Sjulian * a control message it sends the packets out it's 'output' hook.  In
44106266Sjulian * this way this node can be preloaded with a packet stream which is
45106266Sjulian * continuously sent.
46106266Sjulian *
47106266Sjulian * Currently it just copies the mbufs as required.  It could do various
48106266Sjulian * tricks to try and avoid this.  Probably the best performance would
49106266Sjulian * be achieved by modifying the appropriate drivers to be told to
50106266Sjulian * self-re-enqueue packets (e.g. the if_bge driver could reuse the same
51106266Sjulian * transmit descriptors) under control of this node; perhaps via some
52106266Sjulian * flag in the mbuf or some such.  The node would peak at an appropriate
53106266Sjulian * ifnet flag to see if such support is available for the connected
54106266Sjulian * interface.
55106266Sjulian */
56106266Sjulian
57106266Sjulian#include <sys/param.h>
58106266Sjulian#include <sys/systm.h>
59106266Sjulian#include <sys/errno.h>
60106266Sjulian#include <sys/kernel.h>
61106266Sjulian#include <sys/malloc.h>
62106266Sjulian#include <sys/mbuf.h>
63106266Sjulian#include <sys/socket.h>
64106266Sjulian#include <net/if.h>
65106266Sjulian#include <net/if_var.h>
66106266Sjulian#include <netgraph/ng_message.h>
67106266Sjulian#include <netgraph/netgraph.h>
68106266Sjulian#include <netgraph/ng_parse.h>
69106266Sjulian#include <netgraph/ng_ether.h>
70106266Sjulian#include <netgraph/ng_source.h>
71106266Sjulian
72106266Sjulian#define NG_SOURCE_INTR_TICKS		1
73106266Sjulian#define NG_SOURCE_DRIVER_IFQ_MAXLEN	(4*1024)
74106266Sjulian
75106266Sjulian
76106266Sjulian/* Per hook info */
77106266Sjulianstruct source_hookinfo {
78106266Sjulian	hook_p				hook;
79106266Sjulian};
80106266Sjulian
81106266Sjulian/* Per node info */
82106266Sjulianstruct privdata {
83106266Sjulian	node_p				node;
84106266Sjulian	struct source_hookinfo		input;
85106266Sjulian	struct source_hookinfo		output;
86106266Sjulian	struct ng_source_stats		stats;
87106319Sjulian	struct ifqueue			snd_queue;	/* packets to send */
88106266Sjulian	struct ifnet			*output_ifp;
89106266Sjulian	struct callout_handle		intr_ch;
90106319Sjulian	u_int64_t			packets;	/* packets to send */
91106266Sjulian	u_int32_t			queueOctets;
92106266Sjulian};
93106266Sjuliantypedef struct privdata *sc_p;
94106266Sjulian
95106266Sjulian/* Node flags */
96106266Sjulian#define NG_SOURCE_ACTIVE	(NGF_TYPE1)
97106266Sjulian
98106266Sjulian/* XXX */
99106266Sjulian#if 1
100106266Sjulian#undef KASSERT
101106266Sjulian#define KASSERT(expr,msg) do {			\
102106266Sjulian		if (!(expr)) {			\
103106266Sjulian			printf msg ;		\
104106266Sjulian			panic("Assertion");	\
105106266Sjulian		}				\
106106266Sjulian	} while(0)
107106266Sjulian#endif
108106266Sjulian
109106266Sjulian/* Netgraph methods */
110106266Sjulianstatic ng_constructor_t	ng_source_constructor;
111106266Sjulianstatic ng_rcvmsg_t	ng_source_rcvmsg;
112106266Sjulianstatic ng_shutdown_t	ng_source_rmnode;
113106266Sjulianstatic ng_newhook_t	ng_source_newhook;
114106266Sjulianstatic ng_rcvdata_t	ng_source_rcvdata;
115106266Sjulianstatic ng_disconnect_t	ng_source_disconnect;
116106266Sjulian
117106266Sjulian/* Other functions */
118106266Sjulianstatic timeout_t	ng_source_intr;
119125029Shartistatic int		ng_source_request_output_ifp (sc_p);
120106266Sjulianstatic void		ng_source_clr_data (sc_p);
121106266Sjulianstatic void		ng_source_start (sc_p);
122106266Sjulianstatic void		ng_source_stop (sc_p);
123106266Sjulianstatic int		ng_source_send (sc_p, int, int *);
124106435Sjulianstatic int		ng_source_store_output_ifp(sc_p sc,
125106435Sjulian			    struct ng_mesg *msg);
126106266Sjulian
127106266Sjulian
128106266Sjulian/* Parse type for timeval */
129106266Sjulianstatic const struct ng_parse_struct_field ng_source_timeval_type_fields[] =
130106266Sjulian{
131106266Sjulian	{ "tv_sec",		&ng_parse_int32_type	},
132106266Sjulian	{ "tv_usec",		&ng_parse_int32_type	},
133106266Sjulian	{ NULL }
134106266Sjulian};
135106266Sjulianconst struct ng_parse_type ng_source_timeval_type = {
136106266Sjulian	&ng_parse_struct_type,
137106266Sjulian	&ng_source_timeval_type_fields
138106266Sjulian};
139106266Sjulian
140106266Sjulian/* Parse type for struct ng_source_stats */
141106266Sjulianstatic const struct ng_parse_struct_field ng_source_stats_type_fields[]
142106266Sjulian	= NG_SOURCE_STATS_TYPE_INFO;
143106266Sjulianstatic const struct ng_parse_type ng_source_stats_type = {
144106266Sjulian	&ng_parse_struct_type,
145106266Sjulian	&ng_source_stats_type_fields
146106266Sjulian};
147106266Sjulian
148106266Sjulian/* List of commands and how to convert arguments to/from ASCII */
149106266Sjulianstatic const struct ng_cmdlist ng_source_cmds[] = {
150106266Sjulian	{
151106266Sjulian	  NGM_SOURCE_COOKIE,
152106266Sjulian	  NGM_SOURCE_GET_STATS,
153106266Sjulian	  "getstats",
154106266Sjulian	  NULL,
155106266Sjulian	  &ng_source_stats_type
156106266Sjulian	},
157106266Sjulian	{
158106266Sjulian	  NGM_SOURCE_COOKIE,
159106266Sjulian	  NGM_SOURCE_CLR_STATS,
160106266Sjulian	  "clrstats",
161106266Sjulian	  NULL,
162106266Sjulian	  NULL
163106266Sjulian	},
164106266Sjulian	{
165106266Sjulian	  NGM_SOURCE_COOKIE,
166106266Sjulian	  NGM_SOURCE_GETCLR_STATS,
167106266Sjulian	  "getclrstats",
168106266Sjulian	  NULL,
169106266Sjulian	  &ng_source_stats_type
170106266Sjulian	},
171106266Sjulian	{
172106266Sjulian	  NGM_SOURCE_COOKIE,
173106266Sjulian	  NGM_SOURCE_START,
174106266Sjulian	  "start",
175106266Sjulian	  &ng_parse_uint64_type,
176106266Sjulian	  NULL
177106266Sjulian	},
178106266Sjulian	{
179106266Sjulian	  NGM_SOURCE_COOKIE,
180106266Sjulian	  NGM_SOURCE_STOP,
181106266Sjulian	  "stop",
182106266Sjulian	  NULL,
183106266Sjulian	  NULL
184106266Sjulian	},
185106266Sjulian	{
186106266Sjulian	  NGM_SOURCE_COOKIE,
187106266Sjulian	  NGM_SOURCE_CLR_DATA,
188106266Sjulian	  "clrdata",
189106266Sjulian	  NULL,
190106266Sjulian	  NULL
191106266Sjulian	},
192125033Sharti	{
193125033Sharti	  NGM_SOURCE_COOKIE,
194125033Sharti	  NGM_SOURCE_START_NOW,
195125033Sharti	  "start_now",
196125033Sharti	  &ng_parse_uint64_type,
197125033Sharti	  NULL
198125033Sharti	},
199106266Sjulian	{ 0 }
200106266Sjulian};
201106266Sjulian
202106266Sjulian/* Netgraph type descriptor */
203106266Sjulianstatic struct ng_type ng_source_typestruct = {
204125029Sharti	NG_ABI_VERSION,
205106266Sjulian	NG_SOURCE_NODE_TYPE,
206106266Sjulian	NULL,					/* module event handler */
207106266Sjulian	ng_source_constructor,
208106266Sjulian	ng_source_rcvmsg,
209106266Sjulian	ng_source_rmnode,
210106266Sjulian	ng_source_newhook,
211106266Sjulian	NULL,					/* findhook */
212106266Sjulian	NULL,
213106266Sjulian	ng_source_rcvdata,			/* rcvdata */
214106266Sjulian	ng_source_disconnect,
215106266Sjulian	ng_source_cmds
216106266Sjulian};
217106266SjulianNETGRAPH_INIT(source, &ng_source_typestruct);
218106266Sjulian
219125032Shartistatic int ng_source_set_autosrc(sc_p, u_int32_t);
220125032Sharti
221106266Sjulian/*
222106266Sjulian * Node constructor
223106266Sjulian */
224106266Sjulianstatic int
225106321Sjulianng_source_constructor(node_p node)
226106266Sjulian{
227106266Sjulian	sc_p sc;
228106266Sjulian
229125030Sharti	sc = malloc(sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO);
230106266Sjulian	if (sc == NULL)
231106266Sjulian		return (ENOMEM);
232106266Sjulian
233106321Sjulian	NG_NODE_SET_PRIVATE(node, sc);
234106321Sjulian	sc->node = node;
235106266Sjulian	sc->snd_queue.ifq_maxlen = 2048;	/* XXX not checked */
236106321Sjulian	callout_handle_init(&sc->intr_ch);   /* XXX fix.. will
237106321Sjulian						cause problems. */
238106266Sjulian	return (0);
239106266Sjulian}
240106266Sjulian
241106266Sjulian/*
242106266Sjulian * Add a hook
243106266Sjulian */
244106266Sjulianstatic int
245106266Sjulianng_source_newhook(node_p node, hook_p hook, const char *name)
246106266Sjulian{
247106321Sjulian	sc_p sc;
248106266Sjulian
249106321Sjulian	sc = NG_NODE_PRIVATE(node);
250106266Sjulian	KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__));
251106266Sjulian	if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) {
252106266Sjulian		sc->input.hook = hook;
253106321Sjulian		NG_HOOK_SET_PRIVATE(hook, &sc->input);
254106266Sjulian	} else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) {
255106266Sjulian		sc->output.hook = hook;
256106321Sjulian		NG_HOOK_SET_PRIVATE(hook, &sc->output);
257106266Sjulian		sc->output_ifp = 0;
258106266Sjulian		bzero(&sc->stats, sizeof(sc->stats));
259106266Sjulian	} else
260106266Sjulian		return (EINVAL);
261106266Sjulian	return (0);
262106266Sjulian}
263106266Sjulian
264106266Sjulian/*
265106266Sjulian * Receive a control message
266106266Sjulian */
267106266Sjulianstatic int
268106321Sjulianng_source_rcvmsg(node_p node, item_p item, hook_p lasthook)
269106266Sjulian{
270106321Sjulian	sc_p sc;
271106266Sjulian	struct ng_mesg *resp = NULL;
272106266Sjulian	int error = 0;
273106321Sjulian	struct ng_mesg *msg;
274106266Sjulian
275106321Sjulian	sc = NG_NODE_PRIVATE(node);
276106321Sjulian	NGI_GET_MSG(item, msg);
277106266Sjulian	KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__));
278106266Sjulian	switch (msg->header.typecookie) {
279106266Sjulian	case NGM_SOURCE_COOKIE:
280106435Sjulian		if (msg->header.flags & NGF_RESP) {
281106435Sjulian			error = EINVAL;
282106435Sjulian			break;
283106435Sjulian		}
284106266Sjulian		switch (msg->header.cmd) {
285106266Sjulian		case NGM_SOURCE_GET_STATS:
286106266Sjulian		case NGM_SOURCE_CLR_STATS:
287106266Sjulian		case NGM_SOURCE_GETCLR_STATS:
288106266Sjulian                    {
289106266Sjulian			struct ng_source_stats *stats;
290106266Sjulian
291106266Sjulian                        if (msg->header.cmd != NGM_SOURCE_CLR_STATS) {
292106266Sjulian                                NG_MKRESPONSE(resp, msg,
293106266Sjulian                                    sizeof(*stats), M_NOWAIT);
294106266Sjulian				if (resp == NULL) {
295106266Sjulian					error = ENOMEM;
296106266Sjulian					goto done;
297106266Sjulian				}
298106266Sjulian				sc->stats.queueOctets = sc->queueOctets;
299106319Sjulian				sc->stats.queueFrames = sc->snd_queue.ifq_len;
300106321Sjulian				if ((sc->node->nd_flags & NG_SOURCE_ACTIVE)
301106266Sjulian				    && !timevalisset(&sc->stats.endTime)) {
302106319Sjulian					getmicrotime(&sc->stats.elapsedTime);
303106266Sjulian					timevalsub(&sc->stats.elapsedTime,
304106319Sjulian					    &sc->stats.startTime);
305106266Sjulian				}
306106319Sjulian				stats = (struct ng_source_stats *)resp->data;
307106266Sjulian				bcopy(&sc->stats, stats, sizeof(* stats));
308106266Sjulian                        }
309106266Sjulian                        if (msg->header.cmd != NGM_SOURCE_GET_STATS)
310106266Sjulian				bzero(&sc->stats, sizeof(sc->stats));
311106266Sjulian		    }
312106266Sjulian		    break;
313106266Sjulian		case NGM_SOURCE_START:
314106266Sjulian		    {
315106266Sjulian			u_int64_t packets = *(u_int64_t *)msg->data;
316106266Sjulian			if (sc->output.hook == NULL) {
317106319Sjulian				printf("%s: start on node with no output hook\n"
318106319Sjulian				    , __FUNCTION__);
319106266Sjulian				error = EINVAL;
320106266Sjulian				break;
321106266Sjulian			}
322106266Sjulian			/* TODO validation of packets */
323106266Sjulian			sc->packets = packets;
324106266Sjulian			ng_source_start(sc);
325106266Sjulian		    }
326106266Sjulian		    break;
327125033Sharti		case NGM_SOURCE_START_NOW:
328125033Sharti		    {
329125033Sharti			u_int64_t packets = *(u_int64_t *)msg->data;
330125033Sharti			if (sc->output.hook == NULL) {
331125033Sharti				printf("%s: start on node with no output hook\n"
332125033Sharti				    , __FUNCTION__);
333125033Sharti				error = EINVAL;
334125033Sharti				break;
335125033Sharti			}
336125033Sharti			if (sc->node->nd_flags & NG_SOURCE_ACTIVE) {
337125033Sharti				error = EBUSY;
338125033Sharti				break;
339125033Sharti			}
340125033Sharti			/* TODO validation of packets */
341125033Sharti			sc->packets = packets;
342125033Sharti		        sc->output_ifp = NULL;
343125033Sharti
344125033Sharti			sc->node->nd_flags |= NG_SOURCE_ACTIVE;
345125033Sharti			timevalclear(&sc->stats.elapsedTime);
346125033Sharti			timevalclear(&sc->stats.endTime);
347125033Sharti			getmicrotime(&sc->stats.startTime);
348125033Sharti			sc->intr_ch = timeout(ng_source_intr, sc, 0);
349125033Sharti		    }
350125033Sharti		    break;
351106266Sjulian		case NGM_SOURCE_STOP:
352106266Sjulian			ng_source_stop(sc);
353106266Sjulian			break;
354106266Sjulian		case NGM_SOURCE_CLR_DATA:
355106266Sjulian			ng_source_clr_data(sc);
356106266Sjulian			break;
357106266Sjulian		default:
358106266Sjulian			error = EINVAL;
359106266Sjulian			break;
360106266Sjulian		}
361106266Sjulian		break;
362106435Sjulian	case NGM_ETHER_COOKIE:
363106435Sjulian		if (!(msg->header.flags & NGF_RESP)) {
364106435Sjulian			error = EINVAL;
365106435Sjulian			break;
366106435Sjulian		}
367106435Sjulian		switch (msg->header.cmd) {
368106435Sjulian		case NGM_ETHER_GET_IFINDEX:
369106435Sjulian			if (ng_source_store_output_ifp(sc, msg) == 0) {
370106435Sjulian				ng_source_set_autosrc(sc, 0);
371106435Sjulian				sc->node->nd_flags |= NG_SOURCE_ACTIVE;
372106435Sjulian				timevalclear(&sc->stats.elapsedTime);
373106435Sjulian				timevalclear(&sc->stats.endTime);
374106435Sjulian				getmicrotime(&sc->stats.startTime);
375106435Sjulian				sc->intr_ch = timeout(ng_source_intr, sc, 0);
376106435Sjulian			}
377106435Sjulian			break;
378106435Sjulian		default:
379106435Sjulian			error = EINVAL;
380106435Sjulian		}
381106435Sjulian		break;
382106266Sjulian	default:
383106266Sjulian		error = EINVAL;
384106266Sjulian		break;
385106266Sjulian	}
386106266Sjulian
387106266Sjuliandone:
388106321Sjulian	/* Take care of synchronous response, if any */
389106321Sjulian	NG_RESPOND_MSG(error, node, item, resp);
390106321Sjulian	/* Free the message and return */
391106321Sjulian	NG_FREE_MSG(msg);
392106266Sjulian	return (error);
393106266Sjulian}
394106266Sjulian
395106266Sjulian/*
396106266Sjulian * Receive data on a hook
397106266Sjulian *
398106266Sjulian * If data comes in the input hook, enqueue it on the send queue.
399106266Sjulian * If data comes in the output hook, discard it.
400106266Sjulian */
401106266Sjulianstatic int
402106321Sjulianng_source_rcvdata(hook_p hook, item_p item)
403106266Sjulian{
404106321Sjulian	sc_p sc;
405106321Sjulian	struct source_hookinfo *hinfo;
406106266Sjulian	int error = 0;
407106321Sjulian	struct mbuf *m;
408106266Sjulian
409106321Sjulian	sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
410106321Sjulian	NGI_GET_M(item, m);
411106321Sjulian	NG_FREE_ITEM(item);
412106321Sjulian	hinfo = NG_HOOK_PRIVATE(hook);
413106266Sjulian	KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__));
414106266Sjulian	KASSERT(hinfo != NULL, ("%s: null hook info", __FUNCTION__));
415106266Sjulian
416106266Sjulian	/* Which hook? */
417106266Sjulian	if (hinfo == &sc->output) {
418106266Sjulian		/* discard */
419106321Sjulian		NG_FREE_M(m);
420106266Sjulian		return (error);
421106266Sjulian	}
422106266Sjulian	KASSERT(hinfo == &sc->input, ("%s: no hook!", __FUNCTION__));
423106266Sjulian
424106266Sjulian	if ((m->m_flags & M_PKTHDR) == 0) {
425106266Sjulian		printf("%s: mbuf without PKTHDR\n", __FUNCTION__);
426106321Sjulian		NG_FREE_M(m);
427106266Sjulian		return (EINVAL);
428106266Sjulian	}
429106266Sjulian
430106266Sjulian	/* enque packet */
431106266Sjulian	/* XXX should we check IF_QFULL() ? */
432125031Sharti	_IF_ENQUEUE(&sc->snd_queue, m);
433106266Sjulian	sc->queueOctets += m->m_pkthdr.len;
434106266Sjulian
435106266Sjulian	return (0);
436106266Sjulian}
437106266Sjulian
438106266Sjulian/*
439106266Sjulian * Shutdown processing
440106266Sjulian */
441106266Sjulianstatic int
442106266Sjulianng_source_rmnode(node_p node)
443106266Sjulian{
444106321Sjulian	sc_p sc;
445106266Sjulian
446106321Sjulian	sc = NG_NODE_PRIVATE(node);
447106266Sjulian	KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__));
448106321Sjulian	node->nd_flags |= NG_INVALID;
449106266Sjulian	ng_source_stop(sc);
450106266Sjulian	ng_source_clr_data(sc);
451106321Sjulian	NG_NODE_SET_PRIVATE(node, NULL);
452106321Sjulian	NG_NODE_UNREF(node);
453106266Sjulian	FREE(sc, M_NETGRAPH);
454106266Sjulian	return (0);
455106266Sjulian}
456106266Sjulian
457106266Sjulian/*
458106266Sjulian * Hook disconnection
459106266Sjulian */
460106266Sjulianstatic int
461106266Sjulianng_source_disconnect(hook_p hook)
462106266Sjulian{
463106321Sjulian	struct source_hookinfo *hinfo;
464106319Sjulian	sc_p sc;
465106266Sjulian
466106321Sjulian	hinfo = NG_HOOK_PRIVATE(hook);
467106321Sjulian	sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
468106266Sjulian	KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__));
469106266Sjulian	hinfo->hook = NULL;
470106321Sjulian	if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hinfo == &sc->output)
471106321Sjulian		ng_rmnode_self(NG_HOOK_NODE(hook));
472106266Sjulian	return (0);
473106266Sjulian}
474106266Sjulian
475106266Sjulian/*
476106435Sjulian *
477106435Sjulian * Ask out neighbour on the output hook side to send us it's interface
478106435Sjulian * information.
479106266Sjulian */
480125029Shartistatic int
481106435Sjulianng_source_request_output_ifp(sc_p sc)
482106266Sjulian{
483106435Sjulian	struct ng_mesg *msg;
484106266Sjulian	int error = 0;
485106266Sjulian
486106266Sjulian	sc->output_ifp = NULL;
487106266Sjulian
488106266Sjulian	/* Ask the attached node for the connected interface's index */
489106319Sjulian	NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFINDEX, 0, M_NOWAIT);
490106266Sjulian	if (msg == NULL)
491106266Sjulian		return (ENOBUFS);
492106266Sjulian
493106321Sjulian	NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, NULL);
494106435Sjulian	return (error);
495106435Sjulian}
496106266Sjulian
497106435Sjulian/*
498106435Sjulian * Set sc->output_ifp to point to the the struct ifnet of the interface
499106435Sjulian * reached via our output hook.
500106435Sjulian */
501106435Sjulianstatic int
502106435Sjulianng_source_store_output_ifp(sc_p sc, struct ng_mesg *msg)
503106435Sjulian{
504106435Sjulian	struct ifnet *ifp;
505106435Sjulian	u_int32_t if_index;
506106435Sjulian	int s;
507106266Sjulian
508106435Sjulian	if (msg->header.arglen < sizeof(u_int32_t))
509106266Sjulian		return (EINVAL);
510106266Sjulian
511106435Sjulian	if_index = *(u_int32_t *)msg->data;
512106266Sjulian	/* Could use ifindex2ifnet[if_index] except that we have no
513106266Sjulian	 * way of verifying if_index is valid since if_indexlim is
514106266Sjulian	 * local to if_attach()
515106266Sjulian	 */
516108172Shsu	IFNET_RLOCK();
517106266Sjulian	TAILQ_FOREACH(ifp, &ifnet, if_link) {
518106266Sjulian		if (ifp->if_index == if_index)
519106266Sjulian			break;
520106266Sjulian	}
521108172Shsu	IFNET_RUNLOCK();
522106266Sjulian
523106266Sjulian	if (ifp == NULL) {
524106319Sjulian		printf("%s: can't find interface %d\n", __FUNCTION__, if_index);
525106266Sjulian		return (EINVAL);
526106266Sjulian	}
527106266Sjulian	sc->output_ifp = ifp;
528106266Sjulian
529106266Sjulian#if 1
530106266Sjulian	/* XXX mucking with a drivers ifqueue size is ugly but we need it
531106266Sjulian	 * to queue a lot of packets to get close to line rate on a gigabit
532106266Sjulian	 * interface with small packets.
533106266Sjulian	 * XXX we should restore the original value at stop or disconnect
534106266Sjulian	 */
535106266Sjulian	s = splimp();		/* XXX is this required? */
536106266Sjulian	if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN)
537106266Sjulian	{
538106266Sjulian		printf("ng_source: changing ifq_maxlen from %d to %d\n",
539106319Sjulian		    ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN);
540106266Sjulian		ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN;
541106266Sjulian	}
542106266Sjulian	splx(s);
543106266Sjulian#endif
544106266Sjulian	return (0);
545106266Sjulian}
546106266Sjulian
547106266Sjulian/*
548106266Sjulian * Set the attached ethernet node's ethernet source address override flag.
549106266Sjulian */
550106266Sjulianstatic int
551106266Sjulianng_source_set_autosrc(sc_p sc, u_int32_t flag)
552106266Sjulian{
553106266Sjulian	struct ng_mesg *msg;
554106266Sjulian	int error = 0;
555106266Sjulian
556106266Sjulian	NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC,
557106266Sjulian			sizeof (u_int32_t), M_NOWAIT);
558106266Sjulian	if (msg == NULL)
559106266Sjulian		return(ENOBUFS);
560106266Sjulian
561106266Sjulian	*(u_int32_t *)msg->data = flag;
562106321Sjulian	NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, NULL);
563106266Sjulian	return (error);
564106266Sjulian}
565106266Sjulian
566106266Sjulian/*
567106266Sjulian * Clear out the data we've queued
568106266Sjulian */
569106266Sjulianstatic void
570106266Sjulianng_source_clr_data (sc_p sc)
571106266Sjulian{
572106266Sjulian	struct mbuf *m;
573106266Sjulian
574106266Sjulian	for (;;) {
575125031Sharti		_IF_DEQUEUE(&sc->snd_queue, m);
576106266Sjulian		if (m == NULL)
577106266Sjulian			break;
578106266Sjulian		NG_FREE_M(m);
579106266Sjulian	}
580106266Sjulian	sc->queueOctets = 0;
581106266Sjulian}
582106266Sjulian
583106266Sjulian/*
584106266Sjulian * Start sending queued data out the output hook
585106266Sjulian */
586106266Sjulianstatic void
587106266Sjulianng_source_start (sc_p sc)
588106266Sjulian{
589106266Sjulian	KASSERT(sc->output.hook != NULL,
590106266Sjulian			("%s: output hook unconnected", __FUNCTION__));
591106435Sjulian	if (((sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) &&
592106435Sjulian	    (sc->output_ifp == NULL))
593106435Sjulian		ng_source_request_output_ifp(sc);
594106266Sjulian}
595106266Sjulian
596106266Sjulian/*
597106266Sjulian * Stop sending queued data out the output hook
598106266Sjulian */
599106266Sjulianstatic void
600106266Sjulianng_source_stop (sc_p sc)
601106266Sjulian{
602106321Sjulian	if (sc->node->nd_flags & NG_SOURCE_ACTIVE) {
603106266Sjulian		untimeout(ng_source_intr, sc, sc->intr_ch);
604106321Sjulian		sc->node->nd_flags &= ~NG_SOURCE_ACTIVE;
605106266Sjulian		getmicrotime(&sc->stats.endTime);
606106266Sjulian		sc->stats.elapsedTime = sc->stats.endTime;
607106266Sjulian		timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime);
608106266Sjulian		/* XXX should set this to the initial value instead */
609106266Sjulian		ng_source_set_autosrc(sc, 1);
610106266Sjulian	}
611106266Sjulian}
612106266Sjulian
613106266Sjulian/*
614106266Sjulian * While active called every NG_SOURCE_INTR_TICKS ticks.
615106266Sjulian * Sends as many packets as the interface connected to our
616106266Sjulian * output hook is able to enqueue.
617106266Sjulian */
618106266Sjulianstatic void
619106266Sjulianng_source_intr (void *arg)
620106266Sjulian{
621106321Sjulian	sc_p sc = (sc_p) arg;
622106266Sjulian	struct ifqueue *ifq;
623106266Sjulian	int packets;
624106266Sjulian
625106266Sjulian	KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__));
626106266Sjulian
627106266Sjulian	callout_handle_init(&sc->intr_ch);
628106266Sjulian	if (sc->packets == 0 || sc->output.hook == NULL
629106321Sjulian	    || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) {
630106266Sjulian		ng_source_stop(sc);
631106266Sjulian		return;
632106266Sjulian	}
633106266Sjulian
634125033Sharti	if (sc->output_ifp != NULL) {
635125033Sharti		ifq = &sc->output_ifp->if_snd;
636125033Sharti		packets = ifq->ifq_maxlen - ifq->ifq_len;
637125033Sharti	} else
638125033Sharti		packets = sc->snd_queue.ifq_len;
639125033Sharti
640106266Sjulian	ng_source_send(sc, packets, NULL);
641106266Sjulian	if (sc->packets == 0) {
642106266Sjulian		int s = splnet();
643106266Sjulian		ng_source_stop(sc);
644106266Sjulian		splx(s);
645106266Sjulian	} else
646106319Sjulian		sc->intr_ch = timeout(ng_source_intr, sc, NG_SOURCE_INTR_TICKS);
647106266Sjulian}
648106266Sjulian
649106266Sjulian/*
650106266Sjulian * Send packets out our output hook
651106266Sjulian */
652106266Sjulianstatic int
653106266Sjulianng_source_send (sc_p sc, int tosend, int *sent_p)
654106266Sjulian{
655106266Sjulian	struct ifqueue tmp_queue;
656106266Sjulian	struct mbuf *m, *m2;
657106266Sjulian	int sent = 0;
658106266Sjulian	int error = 0;
659106266Sjulian	int s, s2;
660106266Sjulian
661106266Sjulian	KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__));
662106266Sjulian	KASSERT(tosend >= 0, ("%s: negative tosend param", __FUNCTION__));
663106321Sjulian	KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE,
664106266Sjulian			("%s: inactive node", __FUNCTION__));
665106266Sjulian
666106266Sjulian	if ((u_int64_t)tosend > sc->packets)
667106266Sjulian		tosend = sc->packets;
668106266Sjulian
669106266Sjulian	/* Copy the required number of packets to a temporary queue */
670106266Sjulian	bzero (&tmp_queue, sizeof (tmp_queue));
671106266Sjulian	for (sent = 0; error == 0 && sent < tosend; ++sent) {
672106266Sjulian		s = splnet();
673125031Sharti		_IF_DEQUEUE(&sc->snd_queue, m);
674106266Sjulian		splx(s);
675106266Sjulian		if (m == NULL)
676106266Sjulian			break;
677106266Sjulian
678106266Sjulian		/* duplicate the packet */
679111119Simp		m2 = m_copypacket(m, M_DONTWAIT);
680106266Sjulian		if (m2 == NULL) {
681106266Sjulian			s = splnet();
682125031Sharti			_IF_PREPEND(&sc->snd_queue, m);
683106266Sjulian			splx(s);
684106266Sjulian			error = ENOBUFS;
685106266Sjulian			break;
686106266Sjulian		}
687106266Sjulian
688106266Sjulian		/* re-enqueue the original packet for us */
689106266Sjulian		s = splnet();
690125031Sharti		_IF_ENQUEUE(&sc->snd_queue, m);
691106266Sjulian		splx(s);
692106266Sjulian
693106266Sjulian		/* queue the copy for sending at smplimp */
694125031Sharti		_IF_ENQUEUE(&tmp_queue, m2);
695106266Sjulian	}
696106266Sjulian
697106266Sjulian	sent = 0;
698106266Sjulian	s = splimp();
699106266Sjulian	for (;;) {
700125031Sharti		_IF_DEQUEUE(&tmp_queue, m2);
701106266Sjulian		if (m2 == NULL)
702106266Sjulian			break;
703106266Sjulian		if (error == 0) {
704106266Sjulian			++sent;
705106266Sjulian			sc->stats.outFrames++;
706106266Sjulian			sc->stats.outOctets += m2->m_pkthdr.len;
707106266Sjulian			s2 = splnet();
708106266Sjulian			NG_SEND_DATA_ONLY(error, sc->output.hook, m2);
709106266Sjulian			splx(s2);
710106266Sjulian		} else {
711106266Sjulian			NG_FREE_M(m2);
712106266Sjulian		}
713106266Sjulian	}
714106266Sjulian	splx(s);
715106266Sjulian
716106266Sjulian	sc->packets -= sent;
717106266Sjulian	if (sent_p != NULL)
718106266Sjulian		*sent_p = sent;
719106266Sjulian	return (error);
720106266Sjulian}
721