ng_source.c revision 125077
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
38106266Sjulian#include <sys/cdefs.h>
39106266Sjulian__FBSDID("$FreeBSD: head/sys/netgraph/ng_source.c 125077 2004-01-27 10:45:37Z harti $");
40106266Sjulian
41106266Sjulian/*
42106266Sjulian * This node is used for high speed packet geneneration.  It queues
43106266Sjulian * all data recieved on it's 'input' hook and when told to start via
44106266Sjulian * a control message it sends the packets out it's 'output' hook.  In
45106266Sjulian * this way this node can be preloaded with a packet stream which is
46106266Sjulian * continuously sent.
47106266Sjulian *
48106266Sjulian * Currently it just copies the mbufs as required.  It could do various
49106266Sjulian * tricks to try and avoid this.  Probably the best performance would
50106266Sjulian * be achieved by modifying the appropriate drivers to be told to
51106266Sjulian * self-re-enqueue packets (e.g. the if_bge driver could reuse the same
52106266Sjulian * transmit descriptors) under control of this node; perhaps via some
53106266Sjulian * flag in the mbuf or some such.  The node would peak at an appropriate
54106266Sjulian * ifnet flag to see if such support is available for the connected
55106266Sjulian * interface.
56106266Sjulian */
57106266Sjulian
58106266Sjulian#include <sys/param.h>
59106266Sjulian#include <sys/systm.h>
60106266Sjulian#include <sys/errno.h>
61106266Sjulian#include <sys/kernel.h>
62106266Sjulian#include <sys/malloc.h>
63106266Sjulian#include <sys/mbuf.h>
64106266Sjulian#include <sys/socket.h>
65106266Sjulian#include <net/if.h>
66106266Sjulian#include <net/if_var.h>
67106266Sjulian#include <netgraph/ng_message.h>
68106266Sjulian#include <netgraph/netgraph.h>
69106266Sjulian#include <netgraph/ng_parse.h>
70106266Sjulian#include <netgraph/ng_ether.h>
71106266Sjulian#include <netgraph/ng_source.h>
72106266Sjulian
73106266Sjulian#define NG_SOURCE_INTR_TICKS		1
74106266Sjulian#define NG_SOURCE_DRIVER_IFQ_MAXLEN	(4*1024)
75106266Sjulian
76106266Sjulian
77106266Sjulian/* Per hook info */
78106266Sjulianstruct source_hookinfo {
79106266Sjulian	hook_p				hook;
80106266Sjulian};
81106266Sjulian
82106266Sjulian/* Per node info */
83106266Sjulianstruct privdata {
84106266Sjulian	node_p				node;
85106266Sjulian	struct source_hookinfo		input;
86106266Sjulian	struct source_hookinfo		output;
87106319Sjulian	struct ng_source_stats		stats;
88106266Sjulian	struct ifqueue			snd_queue;	/* packets to send */
89106266Sjulian	struct ifnet			*output_ifp;
90106319Sjulian	struct callout_handle		intr_ch;
91106266Sjulian	u_int64_t			packets;	/* packets to send */
92106266Sjulian	u_int32_t			queueOctets;
93106266Sjulian};
94106266Sjuliantypedef struct privdata *sc_p;
95106266Sjulian
96106266Sjulian/* Node flags */
97106266Sjulian#define NG_SOURCE_ACTIVE	(NGF_TYPE1)
98106266Sjulian
99106266Sjulian/* Netgraph methods */
100106266Sjulianstatic ng_constructor_t	ng_source_constructor;
101106266Sjulianstatic ng_rcvmsg_t	ng_source_rcvmsg;
102106266Sjulianstatic ng_shutdown_t	ng_source_rmnode;
103106266Sjulianstatic ng_newhook_t	ng_source_newhook;
104106266Sjulianstatic ng_rcvdata_t	ng_source_rcvdata;
105106266Sjulianstatic ng_disconnect_t	ng_source_disconnect;
106106266Sjulian
107106266Sjulian/* Other functions */
108106266Sjulianstatic timeout_t	ng_source_intr;
109106266Sjulianstatic int		ng_source_request_output_ifp (sc_p);
110106266Sjulianstatic void		ng_source_clr_data (sc_p);
111106266Sjulianstatic void		ng_source_start (sc_p);
112106266Sjulianstatic void		ng_source_stop (sc_p);
113106266Sjulianstatic int		ng_source_send (sc_p, int, int *);
114106266Sjulianstatic int		ng_source_store_output_ifp(sc_p sc,
115106266Sjulian			    struct ng_mesg *msg);
116106266Sjulian
117106266Sjulian
118106266Sjulian/* Parse type for timeval */
119125029Shartistatic const struct ng_parse_struct_field ng_source_timeval_type_fields[] = {
120106266Sjulian	{ "tv_sec",		&ng_parse_int32_type	},
121106266Sjulian	{ "tv_usec",		&ng_parse_int32_type	},
122106266Sjulian	{ NULL }
123106266Sjulian};
124106435Sjulianconst struct ng_parse_type ng_source_timeval_type = {
125106435Sjulian	&ng_parse_struct_type,
126106266Sjulian	&ng_source_timeval_type_fields
127106266Sjulian};
128106266Sjulian
129106266Sjulian/* Parse type for struct ng_source_stats */
130106266Sjulianstatic const struct ng_parse_struct_field ng_source_stats_type_fields[]
131106266Sjulian	= NG_SOURCE_STATS_TYPE_INFO;
132106266Sjulianstatic const struct ng_parse_type ng_source_stats_type = {
133106266Sjulian	&ng_parse_struct_type,
134106266Sjulian	&ng_source_stats_type_fields
135106266Sjulian};
136106266Sjulian
137106266Sjulian/* List of commands and how to convert arguments to/from ASCII */
138106266Sjulianstatic const struct ng_cmdlist ng_source_cmds[] = {
139106266Sjulian	{
140106266Sjulian	  NGM_SOURCE_COOKIE,
141106266Sjulian	  NGM_SOURCE_GET_STATS,
142106266Sjulian	  "getstats",
143106266Sjulian	  NULL,
144106266Sjulian	  &ng_source_stats_type
145106266Sjulian	},
146106266Sjulian	{
147106266Sjulian	  NGM_SOURCE_COOKIE,
148106266Sjulian	  NGM_SOURCE_CLR_STATS,
149106266Sjulian	  "clrstats",
150106266Sjulian	  NULL,
151106266Sjulian	  NULL
152106266Sjulian	},
153106266Sjulian	{
154106266Sjulian	  NGM_SOURCE_COOKIE,
155106266Sjulian	  NGM_SOURCE_GETCLR_STATS,
156106266Sjulian	  "getclrstats",
157106266Sjulian	  NULL,
158106266Sjulian	  &ng_source_stats_type
159106266Sjulian	},
160106266Sjulian	{
161106266Sjulian	  NGM_SOURCE_COOKIE,
162106266Sjulian	  NGM_SOURCE_START,
163106266Sjulian	  "start",
164106266Sjulian	  &ng_parse_uint64_type,
165106266Sjulian	  NULL
166106266Sjulian	},
167106266Sjulian	{
168106266Sjulian	  NGM_SOURCE_COOKIE,
169106266Sjulian	  NGM_SOURCE_STOP,
170106266Sjulian	  "stop",
171106266Sjulian	  NULL,
172106266Sjulian	  NULL
173106266Sjulian	},
174106266Sjulian	{
175106266Sjulian	  NGM_SOURCE_COOKIE,
176106266Sjulian	  NGM_SOURCE_CLR_DATA,
177106266Sjulian	  "clrdata",
178106266Sjulian	  NULL,
179106266Sjulian	  NULL
180106266Sjulian	},
181106266Sjulian	{
182106266Sjulian	  NGM_SOURCE_COOKIE,
183106266Sjulian	  NGM_SOURCE_START_NOW,
184106266Sjulian	  "start_now",
185106266Sjulian	  &ng_parse_uint64_type,
186106266Sjulian	  NULL
187106266Sjulian	},
188106266Sjulian	{ 0 }
189106266Sjulian};
190106266Sjulian
191106266Sjulian/* Netgraph type descriptor */
192106266Sjulianstatic struct ng_type ng_source_typestruct = {
193106266Sjulian	NG_ABI_VERSION,
194106266Sjulian	NG_SOURCE_NODE_TYPE,
195106266Sjulian	NULL,					/* module event handler */
196106266Sjulian	ng_source_constructor,
197125029Sharti	ng_source_rcvmsg,
198106266Sjulian	ng_source_rmnode,
199106266Sjulian	ng_source_newhook,
200106266Sjulian	NULL,					/* findhook */
201106266Sjulian	NULL,					/* connect */
202106266Sjulian	ng_source_rcvdata,			/* rcvdata */
203106266Sjulian	ng_source_disconnect,
204106266Sjulian	ng_source_cmds
205106266Sjulian};
206106266SjulianNETGRAPH_INIT(source, &ng_source_typestruct);
207106266Sjulian
208106266Sjulianstatic int ng_source_set_autosrc(sc_p, u_int32_t);
209106266Sjulian
210106266Sjulian/*
211106266Sjulian * Node constructor
212125032Sharti */
213125032Shartistatic int
214106266Sjulianng_source_constructor(node_p node)
215106266Sjulian{
216106266Sjulian	sc_p sc;
217106266Sjulian
218106321Sjulian	sc = malloc(sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO);
219106266Sjulian	if (sc == NULL)
220106266Sjulian		return (ENOMEM);
221106266Sjulian
222125030Sharti	NG_NODE_SET_PRIVATE(node, sc);
223106266Sjulian	sc->node = node;
224106266Sjulian	sc->snd_queue.ifq_maxlen = 2048;	/* XXX not checked */
225106266Sjulian	callout_handle_init(&sc->intr_ch);   /* XXX fix.. will
226106321Sjulian						cause problems. */
227106321Sjulian	return (0);
228106266Sjulian}
229106321Sjulian
230106321Sjulian/*
231106266Sjulian * Add a hook
232106266Sjulian */
233106266Sjulianstatic int
234106266Sjulianng_source_newhook(node_p node, hook_p hook, const char *name)
235106266Sjulian{
236106266Sjulian	sc_p sc;
237106266Sjulian
238106266Sjulian	sc = NG_NODE_PRIVATE(node);
239106266Sjulian	KASSERT(sc != NULL, ("%s: null node private", __func__));
240106321Sjulian	if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) {
241106266Sjulian		sc->input.hook = hook;
242106321Sjulian		NG_HOOK_SET_PRIVATE(hook, &sc->input);
243106266Sjulian	} else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) {
244106266Sjulian		sc->output.hook = hook;
245106266Sjulian		NG_HOOK_SET_PRIVATE(hook, &sc->output);
246106321Sjulian		sc->output_ifp = 0;
247106266Sjulian		bzero(&sc->stats, sizeof(sc->stats));
248106266Sjulian	} else
249106321Sjulian		return (EINVAL);
250106266Sjulian	return (0);
251106266Sjulian}
252106266Sjulian
253106266Sjulian/*
254106266Sjulian * Receive a control message
255106266Sjulian */
256106266Sjulianstatic int
257106266Sjulianng_source_rcvmsg(node_p node, item_p item, hook_p lasthook)
258106266Sjulian{
259106266Sjulian	sc_p sc;
260106266Sjulian	struct ng_mesg *resp = NULL;
261106321Sjulian	int error = 0;
262106266Sjulian	struct ng_mesg *msg;
263106321Sjulian
264106266Sjulian	sc = NG_NODE_PRIVATE(node);
265106266Sjulian	NGI_GET_MSG(item, msg);
266106321Sjulian	KASSERT(sc != NULL, ("%s: null node private", __func__));
267106266Sjulian	switch (msg->header.typecookie) {
268106321Sjulian	case NGM_SOURCE_COOKIE:
269106321Sjulian		if (msg->header.flags & NGF_RESP) {
270106266Sjulian			error = EINVAL;
271106266Sjulian			break;
272106266Sjulian		}
273106435Sjulian		switch (msg->header.cmd) {
274106435Sjulian		case NGM_SOURCE_GET_STATS:
275106435Sjulian		case NGM_SOURCE_CLR_STATS:
276106435Sjulian		case NGM_SOURCE_GETCLR_STATS:
277106266Sjulian                    {
278106266Sjulian			struct ng_source_stats *stats;
279106266Sjulian
280106266Sjulian                        if (msg->header.cmd != NGM_SOURCE_CLR_STATS) {
281106266Sjulian                                NG_MKRESPONSE(resp, msg,
282106266Sjulian                                    sizeof(*stats), M_NOWAIT);
283106266Sjulian				if (resp == NULL) {
284106266Sjulian					error = ENOMEM;
285106266Sjulian					goto done;
286106266Sjulian				}
287106266Sjulian				sc->stats.queueOctets = sc->queueOctets;
288106266Sjulian				sc->stats.queueFrames = sc->snd_queue.ifq_len;
289106266Sjulian				if ((sc->node->nd_flags & NG_SOURCE_ACTIVE)
290106266Sjulian				    && !timevalisset(&sc->stats.endTime)) {
291106266Sjulian					getmicrotime(&sc->stats.elapsedTime);
292106319Sjulian					timevalsub(&sc->stats.elapsedTime,
293106321Sjulian					    &sc->stats.startTime);
294106266Sjulian				}
295106319Sjulian				stats = (struct ng_source_stats *)resp->data;
296106266Sjulian				bcopy(&sc->stats, stats, sizeof(* stats));
297106319Sjulian                        }
298106266Sjulian                        if (msg->header.cmd != NGM_SOURCE_GET_STATS)
299106319Sjulian				bzero(&sc->stats, sizeof(sc->stats));
300106266Sjulian		    }
301106266Sjulian		    break;
302106266Sjulian		case NGM_SOURCE_START:
303106266Sjulian		    {
304106266Sjulian			u_int64_t packets = *(u_int64_t *)msg->data;
305106266Sjulian			if (sc->output.hook == NULL) {
306106266Sjulian				printf("%s: start on node with no output hook\n"
307106266Sjulian				    , __func__);
308106266Sjulian				error = EINVAL;
309106266Sjulian				break;
310106319Sjulian			}
311106319Sjulian			/* TODO validation of packets */
312106266Sjulian			sc->packets = packets;
313106266Sjulian			ng_source_start(sc);
314106266Sjulian		    }
315106266Sjulian		    break;
316106266Sjulian		case NGM_SOURCE_START_NOW:
317106266Sjulian		    {
318106266Sjulian			u_int64_t packets = *(u_int64_t *)msg->data;
319106266Sjulian			if (sc->output.hook == NULL) {
320106266Sjulian				printf("%s: start on node with no output hook\n"
321106266Sjulian				    , __func__);
322106266Sjulian				error = EINVAL;
323106266Sjulian				break;
324106266Sjulian			}
325106266Sjulian			if (sc->node->nd_flags & NG_SOURCE_ACTIVE) {
326106266Sjulian				error = EBUSY;
327106266Sjulian				break;
328106266Sjulian			}
329106266Sjulian			/* TODO validation of packets */
330106266Sjulian			sc->packets = packets;
331106435Sjulian		        sc->output_ifp = NULL;
332106435Sjulian
333106435Sjulian			sc->node->nd_flags |= NG_SOURCE_ACTIVE;
334106435Sjulian			timevalclear(&sc->stats.elapsedTime);
335106435Sjulian			timevalclear(&sc->stats.endTime);
336106435Sjulian			getmicrotime(&sc->stats.startTime);
337106435Sjulian			sc->intr_ch = timeout(ng_source_intr, sc, 0);
338106435Sjulian		    }
339106435Sjulian		    break;
340106435Sjulian		case NGM_SOURCE_STOP:
341106435Sjulian			ng_source_stop(sc);
342106435Sjulian			break;
343106435Sjulian		case NGM_SOURCE_CLR_DATA:
344106435Sjulian			ng_source_clr_data(sc);
345106435Sjulian			break;
346106435Sjulian		default:
347106435Sjulian			error = EINVAL;
348106435Sjulian			break;
349106435Sjulian		}
350106435Sjulian		break;
351106266Sjulian	case NGM_ETHER_COOKIE:
352106266Sjulian		if (!(msg->header.flags & NGF_RESP)) {
353106266Sjulian			error = EINVAL;
354106266Sjulian			break;
355106266Sjulian		}
356106266Sjulian		switch (msg->header.cmd) {
357106321Sjulian		case NGM_ETHER_GET_IFINDEX:
358106321Sjulian			if (ng_source_store_output_ifp(sc, msg) == 0) {
359106321Sjulian				ng_source_set_autosrc(sc, 0);
360106321Sjulian				sc->node->nd_flags |= NG_SOURCE_ACTIVE;
361106266Sjulian				timevalclear(&sc->stats.elapsedTime);
362106266Sjulian				timevalclear(&sc->stats.endTime);
363106266Sjulian				getmicrotime(&sc->stats.startTime);
364106266Sjulian				sc->intr_ch = timeout(ng_source_intr, sc, 0);
365106266Sjulian			}
366106266Sjulian			break;
367106266Sjulian		default:
368106266Sjulian			error = EINVAL;
369106266Sjulian		}
370106266Sjulian		break;
371106321Sjulian	default:
372106266Sjulian		error = EINVAL;
373106321Sjulian		break;
374106321Sjulian	}
375106266Sjulian
376106321Sjuliandone:
377106266Sjulian	/* Take care of synchronous response, if any */
378106321Sjulian	NG_RESPOND_MSG(error, node, item, resp);
379106321Sjulian	/* Free the message and return */
380106321Sjulian	NG_FREE_MSG(msg);
381106321Sjulian	return (error);
382106266Sjulian}
383106266Sjulian
384106266Sjulian/*
385106266Sjulian * Receive data on a hook
386106266Sjulian *
387106266Sjulian * If data comes in the input hook, enqueue it on the send queue.
388106321Sjulian * If data comes in the output hook, discard it.
389106266Sjulian */
390106266Sjulianstatic int
391106266Sjulianng_source_rcvdata(hook_p hook, item_p item)
392106266Sjulian{
393106266Sjulian	sc_p sc;
394106266Sjulian	struct source_hookinfo *hinfo;
395106321Sjulian	int error = 0;
396106266Sjulian	struct mbuf *m;
397106266Sjulian
398106266Sjulian	sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
399106266Sjulian	NGI_GET_M(item, m);
400106266Sjulian	NG_FREE_ITEM(item);
401125031Sharti	hinfo = NG_HOOK_PRIVATE(hook);
402106266Sjulian	KASSERT(sc != NULL, ("%s: null node private", __func__));
403106266Sjulian	KASSERT(hinfo != NULL, ("%s: null hook info", __func__));
404106266Sjulian
405106266Sjulian	/* Which hook? */
406106266Sjulian	if (hinfo == &sc->output) {
407106266Sjulian		/* discard */
408106266Sjulian		NG_FREE_M(m);
409106266Sjulian		return (error);
410106266Sjulian	}
411106266Sjulian	KASSERT(hinfo == &sc->input, ("%s: no hook!", __func__));
412106266Sjulian
413106321Sjulian	if ((m->m_flags & M_PKTHDR) == 0) {
414106266Sjulian		printf("%s: mbuf without PKTHDR\n", __func__);
415106321Sjulian		NG_FREE_M(m);
416106266Sjulian		return (EINVAL);
417106321Sjulian	}
418106266Sjulian
419106266Sjulian	/* enque packet */
420106321Sjulian	/* XXX should we check IF_QFULL() ? */
421106321Sjulian	_IF_ENQUEUE(&sc->snd_queue, m);
422106266Sjulian	sc->queueOctets += m->m_pkthdr.len;
423106266Sjulian
424106266Sjulian	return (0);
425106266Sjulian}
426106266Sjulian
427106266Sjulian/*
428106266Sjulian * Shutdown processing
429106266Sjulian */
430106266Sjulianstatic int
431106266Sjulianng_source_rmnode(node_p node)
432106321Sjulian{
433106319Sjulian	sc_p sc;
434106266Sjulian
435106321Sjulian	sc = NG_NODE_PRIVATE(node);
436106321Sjulian	KASSERT(sc != NULL, ("%s: null node private", __func__));
437106266Sjulian	node->nd_flags |= NG_INVALID;
438106266Sjulian	ng_source_stop(sc);
439106321Sjulian	ng_source_clr_data(sc);
440106321Sjulian	NG_NODE_SET_PRIVATE(node, NULL);
441106266Sjulian	NG_NODE_UNREF(node);
442106266Sjulian	free(sc, M_NETGRAPH);
443106266Sjulian	return (0);
444106266Sjulian}
445106435Sjulian
446106435Sjulian/*
447106435Sjulian * Hook disconnection
448106266Sjulian */
449125029Shartistatic int
450106435Sjulianng_source_disconnect(hook_p hook)
451106266Sjulian{
452106435Sjulian	struct source_hookinfo *hinfo;
453106266Sjulian	sc_p sc;
454106266Sjulian
455106266Sjulian	hinfo = NG_HOOK_PRIVATE(hook);
456106266Sjulian	sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
457106266Sjulian	KASSERT(sc != NULL, ("%s: null node private", __func__));
458106319Sjulian	hinfo->hook = NULL;
459106266Sjulian	if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hinfo == &sc->output)
460106266Sjulian		ng_rmnode_self(NG_HOOK_NODE(hook));
461106266Sjulian	return (0);
462106321Sjulian}
463106435Sjulian
464106435Sjulian/*
465106266Sjulian *
466106435Sjulian * Ask out neighbour on the output hook side to send us it's interface
467106435Sjulian * information.
468106435Sjulian */
469106435Sjulianstatic int
470106435Sjulianng_source_request_output_ifp(sc_p sc)
471106435Sjulian{
472106435Sjulian	struct ng_mesg *msg;
473106435Sjulian	int error = 0;
474106435Sjulian
475106435Sjulian	sc->output_ifp = NULL;
476106266Sjulian
477106435Sjulian	/* Ask the attached node for the connected interface's index */
478106266Sjulian	NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFINDEX, 0, M_NOWAIT);
479106266Sjulian	if (msg == NULL)
480106435Sjulian		return (ENOBUFS);
481106266Sjulian
482106266Sjulian	NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, NULL);
483106266Sjulian	return (error);
484106266Sjulian}
485108172Shsu
486106266Sjulian/*
487106266Sjulian * Set sc->output_ifp to point to the the struct ifnet of the interface
488106266Sjulian * reached via our output hook.
489106266Sjulian */
490108172Shsustatic int
491106266Sjulianng_source_store_output_ifp(sc_p sc, struct ng_mesg *msg)
492106266Sjulian{
493106319Sjulian	struct ifnet *ifp;
494106266Sjulian	u_int32_t if_index;
495106266Sjulian	int s;
496106266Sjulian
497106266Sjulian	if (msg->header.arglen < sizeof(u_int32_t))
498106266Sjulian		return (EINVAL);
499106266Sjulian
500106266Sjulian	if_index = *(u_int32_t *)msg->data;
501106266Sjulian	/* Could use ifindex2ifnet[if_index] except that we have no
502106266Sjulian	 * way of verifying if_index is valid since if_indexlim is
503106266Sjulian	 * local to if_attach()
504106266Sjulian	 */
505106266Sjulian	IFNET_RLOCK();
506106266Sjulian	TAILQ_FOREACH(ifp, &ifnet, if_link) {
507106266Sjulian		if (ifp->if_index == if_index)
508106319Sjulian			break;
509106266Sjulian	}
510106266Sjulian	IFNET_RUNLOCK();
511106266Sjulian
512106266Sjulian	if (ifp == NULL) {
513106266Sjulian		printf("%s: can't find interface %d\n", __func__, if_index);
514106266Sjulian		return (EINVAL);
515106266Sjulian	}
516106266Sjulian	sc->output_ifp = ifp;
517106266Sjulian
518106266Sjulian#if 1
519106266Sjulian	/* XXX mucking with a drivers ifqueue size is ugly but we need it
520106266Sjulian	 * to queue a lot of packets to get close to line rate on a gigabit
521106266Sjulian	 * interface with small packets.
522106266Sjulian	 * XXX we should restore the original value at stop or disconnect
523106266Sjulian	 */
524106266Sjulian	s = splimp();		/* XXX is this required? */
525106266Sjulian	if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN) {
526106266Sjulian		printf("ng_source: changing ifq_maxlen from %d to %d\n",
527106266Sjulian		    ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN);
528106266Sjulian		ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN;
529106266Sjulian	}
530106266Sjulian	splx(s);
531106321Sjulian#endif
532106266Sjulian	return (0);
533106266Sjulian}
534106266Sjulian
535106266Sjulian/*
536106266Sjulian * Set the attached ethernet node's ethernet source address override flag.
537106266Sjulian */
538106266Sjulianstatic int
539106266Sjulianng_source_set_autosrc(sc_p sc, u_int32_t flag)
540106266Sjulian{
541106266Sjulian	struct ng_mesg *msg;
542106266Sjulian	int error = 0;
543106266Sjulian
544125031Sharti	NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC,
545106266Sjulian	    sizeof (u_int32_t), M_NOWAIT);
546106266Sjulian	if (msg == NULL)
547106266Sjulian		return(ENOBUFS);
548106266Sjulian
549106266Sjulian	*(u_int32_t *)msg->data = flag;
550106266Sjulian	NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, NULL);
551106266Sjulian	return (error);
552106266Sjulian}
553106266Sjulian
554106266Sjulian/*
555106266Sjulian * Clear out the data we've queued
556106266Sjulian */
557106266Sjulianstatic void
558106266Sjulianng_source_clr_data (sc_p sc)
559106266Sjulian{
560106435Sjulian	struct mbuf *m;
561106435Sjulian
562106435Sjulian	for (;;) {
563106266Sjulian		_IF_DEQUEUE(&sc->snd_queue, m);
564106266Sjulian		if (m == NULL)
565106266Sjulian			break;
566106266Sjulian		NG_FREE_M(m);
567106266Sjulian	}
568106266Sjulian	sc->queueOctets = 0;
569106266Sjulian}
570106266Sjulian
571106321Sjulian/*
572106266Sjulian * Start sending queued data out the output hook
573106321Sjulian */
574106266Sjulianstatic void
575106266Sjulianng_source_start (sc_p sc)
576106266Sjulian{
577106266Sjulian	KASSERT(sc->output.hook != NULL,
578106266Sjulian			("%s: output hook unconnected", __func__));
579106266Sjulian	if (((sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) &&
580106266Sjulian	    (sc->output_ifp == NULL))
581106266Sjulian		ng_source_request_output_ifp(sc);
582106266Sjulian}
583106266Sjulian
584106266Sjulian/*
585106266Sjulian * Stop sending queued data out the output hook
586106266Sjulian */
587106266Sjulianstatic void
588106266Sjulianng_source_stop (sc_p sc)
589106266Sjulian{
590106321Sjulian	if (sc->node->nd_flags & NG_SOURCE_ACTIVE) {
591106266Sjulian		untimeout(ng_source_intr, sc, sc->intr_ch);
592106266Sjulian		sc->node->nd_flags &= ~NG_SOURCE_ACTIVE;
593106266Sjulian		getmicrotime(&sc->stats.endTime);
594106266Sjulian		sc->stats.elapsedTime = sc->stats.endTime;
595106266Sjulian		timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime);
596106266Sjulian		/* XXX should set this to the initial value instead */
597106266Sjulian		ng_source_set_autosrc(sc, 1);
598106321Sjulian	}
599106266Sjulian}
600106266Sjulian
601106266Sjulian/*
602106266Sjulian * While active called every NG_SOURCE_INTR_TICKS ticks.
603106266Sjulian * Sends as many packets as the interface connected to our
604106266Sjulian * output hook is able to enqueue.
605106266Sjulian */
606106266Sjulianstatic void
607106266Sjulianng_source_intr (void *arg)
608106266Sjulian{
609106266Sjulian	sc_p sc = (sc_p) arg;
610106266Sjulian	struct ifqueue *ifq;
611106319Sjulian	int packets;
612106266Sjulian
613106266Sjulian	KASSERT(sc != NULL, ("%s: null node private", __func__));
614106266Sjulian
615106266Sjulian	callout_handle_init(&sc->intr_ch);
616106266Sjulian	if (sc->packets == 0 || sc->output.hook == NULL
617106266Sjulian	    || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) {
618106266Sjulian		ng_source_stop(sc);
619106266Sjulian		return;
620106266Sjulian	}
621106266Sjulian
622106266Sjulian	if (sc->output_ifp != NULL) {
623106266Sjulian		ifq = &sc->output_ifp->if_snd;
624106266Sjulian		packets = ifq->ifq_maxlen - ifq->ifq_len;
625106266Sjulian	} else
626106266Sjulian		packets = sc->snd_queue.ifq_len;
627106266Sjulian
628106321Sjulian	ng_source_send(sc, packets, NULL);
629106266Sjulian	if (sc->packets == 0) {
630106266Sjulian		int s = splnet();
631106266Sjulian		ng_source_stop(sc);
632106266Sjulian		splx(s);
633106266Sjulian	} else
634106266Sjulian		sc->intr_ch = timeout(ng_source_intr, sc, NG_SOURCE_INTR_TICKS);
635106266Sjulian}
636106266Sjulian
637106266Sjulian/*
638125031Sharti * Send packets out our output hook
639106266Sjulian */
640106266Sjulianstatic int
641106266Sjulianng_source_send (sc_p sc, int tosend, int *sent_p)
642106266Sjulian{
643106266Sjulian	struct ifqueue tmp_queue;
644111119Simp	struct mbuf *m, *m2;
645106266Sjulian	int sent = 0;
646106266Sjulian	int error = 0;
647125031Sharti	int s, s2;
648106266Sjulian
649106266Sjulian	KASSERT(sc != NULL, ("%s: null node private", __func__));
650106266Sjulian	KASSERT(tosend >= 0, ("%s: negative tosend param", __func__));
651106266Sjulian	KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE,
652106266Sjulian	    ("%s: inactive node", __func__));
653106266Sjulian
654106266Sjulian	if ((u_int64_t)tosend > sc->packets)
655125031Sharti		tosend = sc->packets;
656106266Sjulian
657106266Sjulian	/* Copy the required number of packets to a temporary queue */
658106266Sjulian	bzero (&tmp_queue, sizeof (tmp_queue));
659125031Sharti	for (sent = 0; error == 0 && sent < tosend; ++sent) {
660106266Sjulian		s = splnet();
661106266Sjulian		_IF_DEQUEUE(&sc->snd_queue, m);
662106266Sjulian		splx(s);
663106266Sjulian		if (m == NULL)
664106266Sjulian			break;
665125031Sharti
666106266Sjulian		/* duplicate the packet */
667106266Sjulian		m2 = m_copypacket(m, M_DONTWAIT);
668106266Sjulian		if (m2 == NULL) {
669106266Sjulian			s = splnet();
670106266Sjulian			_IF_PREPEND(&sc->snd_queue, m);
671106266Sjulian			splx(s);
672106266Sjulian			error = ENOBUFS;
673106266Sjulian			break;
674106266Sjulian		}
675106266Sjulian
676106266Sjulian		/* re-enqueue the original packet for us */
677106266Sjulian		s = splnet();
678106266Sjulian		_IF_ENQUEUE(&sc->snd_queue, m);
679106266Sjulian		splx(s);
680106266Sjulian
681106266Sjulian		/* queue the copy for sending at smplimp */
682106266Sjulian		_IF_ENQUEUE(&tmp_queue, m2);
683106266Sjulian	}
684106266Sjulian
685106266Sjulian	sent = 0;
686	s = splimp();
687	for (;;) {
688		_IF_DEQUEUE(&tmp_queue, m2);
689		if (m2 == NULL)
690			break;
691		if (error == 0) {
692			++sent;
693			sc->stats.outFrames++;
694			sc->stats.outOctets += m2->m_pkthdr.len;
695			s2 = splnet();
696			NG_SEND_DATA_ONLY(error, sc->output.hook, m2);
697			splx(s2);
698		} else {
699			NG_FREE_M(m2);
700		}
701	}
702	splx(s);
703
704	sc->packets -= sent;
705	if (sent_p != NULL)
706		*sent_p = sent;
707	return (error);
708}
709