ng_source.c revision 106321
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 106321 2002-11-02 02:29:43Z julian $
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;
119106266Sjulianstatic int		ng_source_get_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 *);
124106266Sjulian
125106266Sjulian
126106266Sjulian/* Parse type for timeval */
127106266Sjulianstatic const struct ng_parse_struct_field ng_source_timeval_type_fields[] =
128106266Sjulian{
129106266Sjulian	{ "tv_sec",		&ng_parse_int32_type	},
130106266Sjulian	{ "tv_usec",		&ng_parse_int32_type	},
131106266Sjulian	{ NULL }
132106266Sjulian};
133106266Sjulianconst struct ng_parse_type ng_source_timeval_type = {
134106266Sjulian	&ng_parse_struct_type,
135106266Sjulian	&ng_source_timeval_type_fields
136106266Sjulian};
137106266Sjulian
138106266Sjulian/* Parse type for struct ng_source_stats */
139106266Sjulianstatic const struct ng_parse_struct_field ng_source_stats_type_fields[]
140106266Sjulian	= NG_SOURCE_STATS_TYPE_INFO;
141106266Sjulianstatic const struct ng_parse_type ng_source_stats_type = {
142106266Sjulian	&ng_parse_struct_type,
143106266Sjulian	&ng_source_stats_type_fields
144106266Sjulian};
145106266Sjulian
146106266Sjulian/* List of commands and how to convert arguments to/from ASCII */
147106266Sjulianstatic const struct ng_cmdlist ng_source_cmds[] = {
148106266Sjulian	{
149106266Sjulian	  NGM_SOURCE_COOKIE,
150106266Sjulian	  NGM_SOURCE_GET_STATS,
151106266Sjulian	  "getstats",
152106266Sjulian	  NULL,
153106266Sjulian	  &ng_source_stats_type
154106266Sjulian	},
155106266Sjulian	{
156106266Sjulian	  NGM_SOURCE_COOKIE,
157106266Sjulian	  NGM_SOURCE_CLR_STATS,
158106266Sjulian	  "clrstats",
159106266Sjulian	  NULL,
160106266Sjulian	  NULL
161106266Sjulian	},
162106266Sjulian	{
163106266Sjulian	  NGM_SOURCE_COOKIE,
164106266Sjulian	  NGM_SOURCE_GETCLR_STATS,
165106266Sjulian	  "getclrstats",
166106266Sjulian	  NULL,
167106266Sjulian	  &ng_source_stats_type
168106266Sjulian	},
169106266Sjulian	{
170106266Sjulian	  NGM_SOURCE_COOKIE,
171106266Sjulian	  NGM_SOURCE_START,
172106266Sjulian	  "start",
173106266Sjulian	  &ng_parse_uint64_type,
174106266Sjulian	  NULL
175106266Sjulian	},
176106266Sjulian	{
177106266Sjulian	  NGM_SOURCE_COOKIE,
178106266Sjulian	  NGM_SOURCE_STOP,
179106266Sjulian	  "stop",
180106266Sjulian	  NULL,
181106266Sjulian	  NULL
182106266Sjulian	},
183106266Sjulian	{
184106266Sjulian	  NGM_SOURCE_COOKIE,
185106266Sjulian	  NGM_SOURCE_CLR_DATA,
186106266Sjulian	  "clrdata",
187106266Sjulian	  NULL,
188106266Sjulian	  NULL
189106266Sjulian	},
190106266Sjulian	{ 0 }
191106266Sjulian};
192106266Sjulian
193106266Sjulian/* Netgraph type descriptor */
194106266Sjulianstatic struct ng_type ng_source_typestruct = {
195106266Sjulian	NG_VERSION,
196106266Sjulian	NG_SOURCE_NODE_TYPE,
197106266Sjulian	NULL,					/* module event handler */
198106266Sjulian	ng_source_constructor,
199106266Sjulian	ng_source_rcvmsg,
200106266Sjulian	ng_source_rmnode,
201106266Sjulian	ng_source_newhook,
202106266Sjulian	NULL,					/* findhook */
203106266Sjulian	NULL,
204106266Sjulian	ng_source_rcvdata,			/* rcvdata */
205106266Sjulian	ng_source_disconnect,
206106266Sjulian	ng_source_cmds
207106266Sjulian};
208106266SjulianNETGRAPH_INIT(source, &ng_source_typestruct);
209106266Sjulian
210106266Sjulian/*
211106266Sjulian * Node constructor
212106266Sjulian */
213106266Sjulianstatic int
214106321Sjulianng_source_constructor(node_p node)
215106266Sjulian{
216106266Sjulian	sc_p sc;
217106266Sjulian
218106266Sjulian	MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_NOWAIT);
219106266Sjulian	if (sc == NULL)
220106266Sjulian		return (ENOMEM);
221106266Sjulian	bzero(sc, sizeof(*sc));
222106266Sjulian
223106321Sjulian	NG_NODE_SET_PRIVATE(node, sc);
224106321Sjulian	sc->node = node;
225106266Sjulian	sc->snd_queue.ifq_maxlen = 2048;	/* XXX not checked */
226106321Sjulian	callout_handle_init(&sc->intr_ch);   /* XXX fix.. will
227106321Sjulian						cause problems. */
228106266Sjulian	return (0);
229106266Sjulian}
230106266Sjulian
231106266Sjulian/*
232106266Sjulian * Add a hook
233106266Sjulian */
234106266Sjulianstatic int
235106266Sjulianng_source_newhook(node_p node, hook_p hook, const char *name)
236106266Sjulian{
237106321Sjulian	sc_p sc;
238106266Sjulian
239106321Sjulian	sc = NG_NODE_PRIVATE(node);
240106266Sjulian	KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__));
241106266Sjulian	if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) {
242106266Sjulian		sc->input.hook = hook;
243106321Sjulian		NG_HOOK_SET_PRIVATE(hook, &sc->input);
244106266Sjulian	} else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) {
245106266Sjulian		sc->output.hook = hook;
246106321Sjulian		NG_HOOK_SET_PRIVATE(hook, &sc->output);
247106266Sjulian		sc->output_ifp = 0;
248106266Sjulian		bzero(&sc->stats, sizeof(sc->stats));
249106266Sjulian	} else
250106266Sjulian		return (EINVAL);
251106266Sjulian	return (0);
252106266Sjulian}
253106266Sjulian
254106266Sjulian/*
255106266Sjulian * Receive a control message
256106266Sjulian */
257106266Sjulianstatic int
258106321Sjulianng_source_rcvmsg(node_p node, item_p item, hook_p lasthook)
259106266Sjulian{
260106321Sjulian	sc_p sc;
261106266Sjulian	struct ng_mesg *resp = NULL;
262106266Sjulian	int error = 0;
263106321Sjulian	struct ng_mesg *msg;
264106266Sjulian
265106321Sjulian	sc = NG_NODE_PRIVATE(node);
266106321Sjulian	NGI_GET_MSG(item, msg);
267106266Sjulian	KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__));
268106266Sjulian	switch (msg->header.typecookie) {
269106266Sjulian	case NGM_SOURCE_COOKIE:
270106266Sjulian		switch (msg->header.cmd) {
271106266Sjulian		case NGM_SOURCE_GET_STATS:
272106266Sjulian		case NGM_SOURCE_CLR_STATS:
273106266Sjulian		case NGM_SOURCE_GETCLR_STATS:
274106266Sjulian                    {
275106266Sjulian			struct ng_source_stats *stats;
276106266Sjulian
277106266Sjulian                        if (msg->header.cmd != NGM_SOURCE_CLR_STATS) {
278106266Sjulian                                NG_MKRESPONSE(resp, msg,
279106266Sjulian                                    sizeof(*stats), M_NOWAIT);
280106266Sjulian				if (resp == NULL) {
281106266Sjulian					error = ENOMEM;
282106266Sjulian					goto done;
283106266Sjulian				}
284106266Sjulian				sc->stats.queueOctets = sc->queueOctets;
285106319Sjulian				sc->stats.queueFrames = sc->snd_queue.ifq_len;
286106321Sjulian				if ((sc->node->nd_flags & NG_SOURCE_ACTIVE)
287106266Sjulian				    && !timevalisset(&sc->stats.endTime)) {
288106319Sjulian					getmicrotime(&sc->stats.elapsedTime);
289106266Sjulian					timevalsub(&sc->stats.elapsedTime,
290106319Sjulian					    &sc->stats.startTime);
291106266Sjulian				}
292106319Sjulian				stats = (struct ng_source_stats *)resp->data;
293106266Sjulian				bcopy(&sc->stats, stats, sizeof(* stats));
294106266Sjulian                        }
295106266Sjulian                        if (msg->header.cmd != NGM_SOURCE_GET_STATS)
296106266Sjulian				bzero(&sc->stats, sizeof(sc->stats));
297106266Sjulian		    }
298106266Sjulian		    break;
299106266Sjulian		case NGM_SOURCE_START:
300106266Sjulian		    {
301106266Sjulian			u_int64_t packets = *(u_int64_t *)msg->data;
302106266Sjulian			if (sc->output.hook == NULL) {
303106319Sjulian				printf("%s: start on node with no output hook\n"
304106319Sjulian				    , __FUNCTION__);
305106266Sjulian				error = EINVAL;
306106266Sjulian				break;
307106266Sjulian			}
308106266Sjulian			/* TODO validation of packets */
309106266Sjulian			sc->packets = packets;
310106266Sjulian			ng_source_start(sc);
311106266Sjulian		    }
312106266Sjulian		    break;
313106266Sjulian		case NGM_SOURCE_STOP:
314106266Sjulian			ng_source_stop(sc);
315106266Sjulian			break;
316106266Sjulian		case NGM_SOURCE_CLR_DATA:
317106266Sjulian			ng_source_clr_data(sc);
318106266Sjulian			break;
319106266Sjulian		default:
320106266Sjulian			error = EINVAL;
321106266Sjulian			break;
322106266Sjulian		}
323106266Sjulian		break;
324106266Sjulian	default:
325106266Sjulian		error = EINVAL;
326106266Sjulian		break;
327106266Sjulian	}
328106266Sjulian
329106266Sjuliandone:
330106321Sjulian	/* Take care of synchronous response, if any */
331106321Sjulian	NG_RESPOND_MSG(error, node, item, resp);
332106321Sjulian	/* Free the message and return */
333106321Sjulian	NG_FREE_MSG(msg);
334106266Sjulian	return (error);
335106266Sjulian}
336106266Sjulian
337106266Sjulian/*
338106266Sjulian * Receive data on a hook
339106266Sjulian *
340106266Sjulian * If data comes in the input hook, enqueue it on the send queue.
341106266Sjulian * If data comes in the output hook, discard it.
342106266Sjulian */
343106266Sjulianstatic int
344106321Sjulianng_source_rcvdata(hook_p hook, item_p item)
345106266Sjulian{
346106321Sjulian	sc_p sc;
347106321Sjulian	struct source_hookinfo *hinfo;
348106266Sjulian	int error = 0;
349106321Sjulian	struct mbuf *m;
350106266Sjulian
351106321Sjulian	sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
352106321Sjulian	NGI_GET_M(item, m);
353106321Sjulian	NG_FREE_ITEM(item);
354106321Sjulian	hinfo = NG_HOOK_PRIVATE(hook);
355106266Sjulian	KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__));
356106266Sjulian	KASSERT(hinfo != NULL, ("%s: null hook info", __FUNCTION__));
357106266Sjulian
358106266Sjulian	/* Which hook? */
359106266Sjulian	if (hinfo == &sc->output) {
360106266Sjulian		/* discard */
361106321Sjulian		NG_FREE_M(m);
362106266Sjulian		return (error);
363106266Sjulian	}
364106266Sjulian	KASSERT(hinfo == &sc->input, ("%s: no hook!", __FUNCTION__));
365106266Sjulian
366106266Sjulian	if ((m->m_flags & M_PKTHDR) == 0) {
367106266Sjulian		printf("%s: mbuf without PKTHDR\n", __FUNCTION__);
368106321Sjulian		NG_FREE_M(m);
369106266Sjulian		return (EINVAL);
370106266Sjulian	}
371106266Sjulian
372106266Sjulian	/* enque packet */
373106266Sjulian	/* XXX should we check IF_QFULL() ? */
374106266Sjulian	IF_ENQUEUE(&sc->snd_queue, m);
375106266Sjulian	sc->queueOctets += m->m_pkthdr.len;
376106266Sjulian
377106266Sjulian	return (0);
378106266Sjulian}
379106266Sjulian
380106266Sjulian/*
381106266Sjulian * Shutdown processing
382106266Sjulian */
383106266Sjulianstatic int
384106266Sjulianng_source_rmnode(node_p node)
385106266Sjulian{
386106321Sjulian	sc_p sc;
387106266Sjulian
388106321Sjulian	sc = NG_NODE_PRIVATE(node);
389106266Sjulian	KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__));
390106321Sjulian	node->nd_flags |= NG_INVALID;
391106266Sjulian	ng_source_stop(sc);
392106266Sjulian	ng_source_clr_data(sc);
393106321Sjulian	NG_NODE_SET_PRIVATE(node, NULL);
394106321Sjulian	NG_NODE_UNREF(node);
395106266Sjulian	FREE(sc, M_NETGRAPH);
396106266Sjulian	return (0);
397106266Sjulian}
398106266Sjulian
399106266Sjulian/*
400106266Sjulian * Hook disconnection
401106266Sjulian */
402106266Sjulianstatic int
403106266Sjulianng_source_disconnect(hook_p hook)
404106266Sjulian{
405106321Sjulian	struct source_hookinfo *hinfo;
406106319Sjulian	sc_p sc;
407106266Sjulian
408106321Sjulian	hinfo = NG_HOOK_PRIVATE(hook);
409106321Sjulian	sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
410106266Sjulian	KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__));
411106266Sjulian	hinfo->hook = NULL;
412106321Sjulian	if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hinfo == &sc->output)
413106321Sjulian		ng_rmnode_self(NG_HOOK_NODE(hook));
414106266Sjulian	return (0);
415106266Sjulian}
416106266Sjulian
417106266Sjulian/*
418106266Sjulian * Set sc->output_ifp to point to the the struct ifnet of the interface
419106266Sjulian * reached via our output hook.
420106266Sjulian */
421106266Sjulianstatic int
422106266Sjulianng_source_get_output_ifp(sc_p sc)
423106266Sjulian{
424106266Sjulian	struct ng_mesg *msg, *rsp;
425106266Sjulian	struct ifnet *ifp;
426106266Sjulian	u_int32_t if_index;
427106266Sjulian	int error = 0;
428106266Sjulian	int s;
429106266Sjulian
430106266Sjulian	sc->output_ifp = NULL;
431106266Sjulian
432106266Sjulian	/* Ask the attached node for the connected interface's index */
433106319Sjulian	NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFINDEX, 0, M_NOWAIT);
434106266Sjulian	if (msg == NULL)
435106266Sjulian		return (ENOBUFS);
436106266Sjulian
437106321Sjulian	NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, NULL);
438106321Sjulian	/* error = ng_send_msg(sc->node, msg, NG_SOURCE_HOOK_OUTPUT, &rsp); */
439106321Sjulian#warn "oiks"
440106266Sjulian	if (error != 0)
441106266Sjulian		return (error);
442106266Sjulian
443106266Sjulian	if (rsp == NULL)
444106266Sjulian		return (EINVAL);
445106266Sjulian
446106266Sjulian	if (rsp->header.arglen < sizeof(u_int32_t))
447106266Sjulian		return (EINVAL);
448106266Sjulian
449106266Sjulian	if_index = *(u_int32_t *)rsp->data;
450106266Sjulian	/* Could use ifindex2ifnet[if_index] except that we have no
451106266Sjulian	 * way of verifying if_index is valid since if_indexlim is
452106266Sjulian	 * local to if_attach()
453106266Sjulian	 */
454106266Sjulian	TAILQ_FOREACH(ifp, &ifnet, if_link) {
455106266Sjulian		if (ifp->if_index == if_index)
456106266Sjulian			break;
457106266Sjulian	}
458106266Sjulian
459106266Sjulian	if (ifp == NULL) {
460106319Sjulian		printf("%s: can't find interface %d\n", __FUNCTION__, if_index);
461106266Sjulian		return (EINVAL);
462106266Sjulian	}
463106266Sjulian	sc->output_ifp = ifp;
464106266Sjulian
465106266Sjulian#if 1
466106266Sjulian	/* XXX mucking with a drivers ifqueue size is ugly but we need it
467106266Sjulian	 * to queue a lot of packets to get close to line rate on a gigabit
468106266Sjulian	 * interface with small packets.
469106266Sjulian	 * XXX we should restore the original value at stop or disconnect
470106266Sjulian	 */
471106266Sjulian	s = splimp();		/* XXX is this required? */
472106266Sjulian	if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN)
473106266Sjulian	{
474106266Sjulian		printf("ng_source: changing ifq_maxlen from %d to %d\n",
475106319Sjulian		    ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN);
476106266Sjulian		ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN;
477106266Sjulian	}
478106266Sjulian	splx(s);
479106266Sjulian#endif
480106266Sjulian	return (0);
481106266Sjulian}
482106266Sjulian
483106266Sjulian/*
484106266Sjulian * Set the attached ethernet node's ethernet source address override flag.
485106266Sjulian */
486106266Sjulianstatic int
487106266Sjulianng_source_set_autosrc(sc_p sc, u_int32_t flag)
488106266Sjulian{
489106266Sjulian	struct ng_mesg *msg;
490106266Sjulian	int error = 0;
491106266Sjulian
492106266Sjulian	NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC,
493106266Sjulian			sizeof (u_int32_t), M_NOWAIT);
494106266Sjulian	if (msg == NULL)
495106266Sjulian		return(ENOBUFS);
496106266Sjulian
497106266Sjulian	*(u_int32_t *)msg->data = flag;
498106321Sjulian	NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, NULL);
499106266Sjulian	return (error);
500106266Sjulian}
501106266Sjulian
502106266Sjulian/*
503106266Sjulian * Clear out the data we've queued
504106266Sjulian */
505106266Sjulianstatic void
506106266Sjulianng_source_clr_data (sc_p sc)
507106266Sjulian{
508106266Sjulian	struct mbuf *m;
509106266Sjulian
510106266Sjulian	for (;;) {
511106266Sjulian		IF_DEQUEUE(&sc->snd_queue, m);
512106266Sjulian		if (m == NULL)
513106266Sjulian			break;
514106266Sjulian		NG_FREE_M(m);
515106266Sjulian	}
516106266Sjulian	sc->queueOctets = 0;
517106266Sjulian}
518106266Sjulian
519106266Sjulian/*
520106266Sjulian * Start sending queued data out the output hook
521106266Sjulian */
522106266Sjulianstatic void
523106266Sjulianng_source_start (sc_p sc)
524106266Sjulian{
525106266Sjulian	KASSERT(sc->output.hook != NULL,
526106266Sjulian			("%s: output hook unconnected", __FUNCTION__));
527106321Sjulian	if ((sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) {
528106319Sjulian		if (sc->output_ifp == NULL && ng_source_get_output_ifp(sc) != 0)
529106266Sjulian			return;
530106266Sjulian		ng_source_set_autosrc(sc, 0);
531106321Sjulian		sc->node->nd_flags |= NG_SOURCE_ACTIVE;
532106266Sjulian		timevalclear(&sc->stats.elapsedTime);
533106266Sjulian		timevalclear(&sc->stats.endTime);
534106266Sjulian		getmicrotime(&sc->stats.startTime);
535106266Sjulian		sc->intr_ch = timeout(ng_source_intr, sc, 0);
536106266Sjulian	}
537106266Sjulian}
538106266Sjulian
539106266Sjulian/*
540106266Sjulian * Stop sending queued data out the output hook
541106266Sjulian */
542106266Sjulianstatic void
543106266Sjulianng_source_stop (sc_p sc)
544106266Sjulian{
545106321Sjulian	if (sc->node->nd_flags & NG_SOURCE_ACTIVE) {
546106266Sjulian		untimeout(ng_source_intr, sc, sc->intr_ch);
547106321Sjulian		sc->node->nd_flags &= ~NG_SOURCE_ACTIVE;
548106266Sjulian		getmicrotime(&sc->stats.endTime);
549106266Sjulian		sc->stats.elapsedTime = sc->stats.endTime;
550106266Sjulian		timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime);
551106266Sjulian		/* XXX should set this to the initial value instead */
552106266Sjulian		ng_source_set_autosrc(sc, 1);
553106266Sjulian	}
554106266Sjulian}
555106266Sjulian
556106266Sjulian/*
557106266Sjulian * While active called every NG_SOURCE_INTR_TICKS ticks.
558106266Sjulian * Sends as many packets as the interface connected to our
559106266Sjulian * output hook is able to enqueue.
560106266Sjulian */
561106266Sjulianstatic void
562106266Sjulianng_source_intr (void *arg)
563106266Sjulian{
564106321Sjulian	sc_p sc = (sc_p) arg;
565106266Sjulian	struct ifqueue *ifq;
566106266Sjulian	int packets;
567106266Sjulian
568106266Sjulian	KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__));
569106266Sjulian
570106266Sjulian	callout_handle_init(&sc->intr_ch);
571106266Sjulian	if (sc->packets == 0 || sc->output.hook == NULL
572106321Sjulian	    || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) {
573106266Sjulian		ng_source_stop(sc);
574106266Sjulian		return;
575106266Sjulian	}
576106266Sjulian
577106266Sjulian	ifq = &sc->output_ifp->if_snd;
578106266Sjulian	packets = ifq->ifq_maxlen - ifq->ifq_len;
579106266Sjulian	ng_source_send(sc, packets, NULL);
580106266Sjulian	if (sc->packets == 0) {
581106266Sjulian		int s = splnet();
582106266Sjulian		ng_source_stop(sc);
583106266Sjulian		splx(s);
584106266Sjulian	} else
585106319Sjulian		sc->intr_ch = timeout(ng_source_intr, sc, NG_SOURCE_INTR_TICKS);
586106266Sjulian}
587106266Sjulian
588106266Sjulian/*
589106266Sjulian * Send packets out our output hook
590106266Sjulian */
591106266Sjulianstatic int
592106266Sjulianng_source_send (sc_p sc, int tosend, int *sent_p)
593106266Sjulian{
594106266Sjulian	struct ifqueue tmp_queue;
595106266Sjulian	struct mbuf *m, *m2;
596106266Sjulian	int sent = 0;
597106266Sjulian	int error = 0;
598106266Sjulian	int s, s2;
599106266Sjulian
600106266Sjulian	KASSERT(sc != NULL, ("%s: null node private", __FUNCTION__));
601106266Sjulian	KASSERT(tosend >= 0, ("%s: negative tosend param", __FUNCTION__));
602106321Sjulian	KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE,
603106266Sjulian			("%s: inactive node", __FUNCTION__));
604106266Sjulian
605106266Sjulian	if ((u_int64_t)tosend > sc->packets)
606106266Sjulian		tosend = sc->packets;
607106266Sjulian
608106266Sjulian	/* Copy the required number of packets to a temporary queue */
609106266Sjulian	bzero (&tmp_queue, sizeof (tmp_queue));
610106266Sjulian	for (sent = 0; error == 0 && sent < tosend; ++sent) {
611106266Sjulian		s = splnet();
612106266Sjulian		IF_DEQUEUE(&sc->snd_queue, m);
613106266Sjulian		splx(s);
614106266Sjulian		if (m == NULL)
615106266Sjulian			break;
616106266Sjulian
617106266Sjulian		/* duplicate the packet */
618106266Sjulian		m2 = m_copypacket(m, M_NOWAIT);
619106266Sjulian		if (m2 == NULL) {
620106266Sjulian			s = splnet();
621106266Sjulian			IF_PREPEND(&sc->snd_queue, m);
622106266Sjulian			splx(s);
623106266Sjulian			error = ENOBUFS;
624106266Sjulian			break;
625106266Sjulian		}
626106266Sjulian
627106266Sjulian		/* re-enqueue the original packet for us */
628106266Sjulian		s = splnet();
629106266Sjulian		IF_ENQUEUE(&sc->snd_queue, m);
630106266Sjulian		splx(s);
631106266Sjulian
632106266Sjulian		/* queue the copy for sending at smplimp */
633106266Sjulian		IF_ENQUEUE(&tmp_queue, m2);
634106266Sjulian	}
635106266Sjulian
636106266Sjulian	sent = 0;
637106266Sjulian	s = splimp();
638106266Sjulian	for (;;) {
639106266Sjulian		IF_DEQUEUE(&tmp_queue, m2);
640106266Sjulian		if (m2 == NULL)
641106266Sjulian			break;
642106266Sjulian		if (error == 0) {
643106266Sjulian			++sent;
644106266Sjulian			sc->stats.outFrames++;
645106266Sjulian			sc->stats.outOctets += m2->m_pkthdr.len;
646106266Sjulian			s2 = splnet();
647106266Sjulian			NG_SEND_DATA_ONLY(error, sc->output.hook, m2);
648106266Sjulian			splx(s2);
649106266Sjulian		} else {
650106266Sjulian			NG_FREE_M(m2);
651106266Sjulian		}
652106266Sjulian	}
653106266Sjulian	splx(s);
654106266Sjulian
655106266Sjulian	sc->packets -= sent;
656106266Sjulian	if (sent_p != NULL)
657106266Sjulian		*sent_p = sent;
658106266Sjulian	return (error);
659106266Sjulian}
660