ng_source.c revision 129823
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
38125077Sharti#include <sys/cdefs.h>
39125077Sharti__FBSDID("$FreeBSD: head/sys/netgraph/ng_source.c 129823 2004-05-29 00:51:19Z julian $");
40125077Sharti
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;
87106266Sjulian	struct ng_source_stats		stats;
88106319Sjulian	struct ifqueue			snd_queue;	/* packets to send */
89106266Sjulian	struct ifnet			*output_ifp;
90106266Sjulian	struct callout_handle		intr_ch;
91106319Sjulian	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 */
108125243Shartistatic void		ng_source_intr(node_p, hook_p, void *, int);
109125029Shartistatic 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 *);
114106435Sjulianstatic int		ng_source_store_output_ifp(sc_p sc,
115106435Sjulian			    struct ng_mesg *msg);
116106266Sjulian
117106266Sjulian
118106266Sjulian/* Parse type for timeval */
119125077Shartistatic 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};
124106266Sjulianconst struct ng_parse_type ng_source_timeval_type = {
125106266Sjulian	&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	},
181125033Sharti	{
182125033Sharti	  NGM_SOURCE_COOKIE,
183125033Sharti	  NGM_SOURCE_START_NOW,
184125033Sharti	  "start_now",
185125033Sharti	  &ng_parse_uint64_type,
186125033Sharti	  NULL
187125033Sharti	},
188106266Sjulian	{ 0 }
189106266Sjulian};
190106266Sjulian
191106266Sjulian/* Netgraph type descriptor */
192106266Sjulianstatic struct ng_type ng_source_typestruct = {
193129823Sjulian	.version =	NG_ABI_VERSION,
194129823Sjulian	.name =		NG_SOURCE_NODE_TYPE,
195129823Sjulian	.constructor =	ng_source_constructor,
196129823Sjulian	.rcvmsg =	ng_source_rcvmsg,
197129823Sjulian	.shutdown =	ng_source_rmnode,
198129823Sjulian	.newhook =	ng_source_newhook,
199129823Sjulian	.rcvdata =	ng_source_rcvdata,
200129823Sjulian	.disconnect =	ng_source_disconnect,
201129823Sjulian	.cmdlist =	ng_source_cmds,
202106266Sjulian};
203106266SjulianNETGRAPH_INIT(source, &ng_source_typestruct);
204106266Sjulian
205125032Shartistatic int ng_source_set_autosrc(sc_p, u_int32_t);
206125032Sharti
207106266Sjulian/*
208106266Sjulian * Node constructor
209106266Sjulian */
210106266Sjulianstatic int
211106321Sjulianng_source_constructor(node_p node)
212106266Sjulian{
213106266Sjulian	sc_p sc;
214106266Sjulian
215125030Sharti	sc = malloc(sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO);
216106266Sjulian	if (sc == NULL)
217106266Sjulian		return (ENOMEM);
218106266Sjulian
219106321Sjulian	NG_NODE_SET_PRIVATE(node, sc);
220106321Sjulian	sc->node = node;
221106266Sjulian	sc->snd_queue.ifq_maxlen = 2048;	/* XXX not checked */
222106321Sjulian	callout_handle_init(&sc->intr_ch);   /* XXX fix.. will
223106321Sjulian						cause problems. */
224106266Sjulian	return (0);
225106266Sjulian}
226106266Sjulian
227106266Sjulian/*
228106266Sjulian * Add a hook
229106266Sjulian */
230106266Sjulianstatic int
231106266Sjulianng_source_newhook(node_p node, hook_p hook, const char *name)
232106266Sjulian{
233106321Sjulian	sc_p sc;
234106266Sjulian
235106321Sjulian	sc = NG_NODE_PRIVATE(node);
236125077Sharti	KASSERT(sc != NULL, ("%s: null node private", __func__));
237106266Sjulian	if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) {
238106266Sjulian		sc->input.hook = hook;
239106321Sjulian		NG_HOOK_SET_PRIVATE(hook, &sc->input);
240106266Sjulian	} else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) {
241106266Sjulian		sc->output.hook = hook;
242106321Sjulian		NG_HOOK_SET_PRIVATE(hook, &sc->output);
243106266Sjulian		sc->output_ifp = 0;
244106266Sjulian		bzero(&sc->stats, sizeof(sc->stats));
245106266Sjulian	} else
246106266Sjulian		return (EINVAL);
247106266Sjulian	return (0);
248106266Sjulian}
249106266Sjulian
250106266Sjulian/*
251106266Sjulian * Receive a control message
252106266Sjulian */
253106266Sjulianstatic int
254106321Sjulianng_source_rcvmsg(node_p node, item_p item, hook_p lasthook)
255106266Sjulian{
256106321Sjulian	sc_p sc;
257106266Sjulian	struct ng_mesg *resp = NULL;
258106266Sjulian	int error = 0;
259106321Sjulian	struct ng_mesg *msg;
260106266Sjulian
261106321Sjulian	sc = NG_NODE_PRIVATE(node);
262106321Sjulian	NGI_GET_MSG(item, msg);
263125077Sharti	KASSERT(sc != NULL, ("%s: null node private", __func__));
264106266Sjulian	switch (msg->header.typecookie) {
265106266Sjulian	case NGM_SOURCE_COOKIE:
266106435Sjulian		if (msg->header.flags & NGF_RESP) {
267106435Sjulian			error = EINVAL;
268106435Sjulian			break;
269106435Sjulian		}
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"
304125077Sharti				    , __func__);
305106266Sjulian				error = EINVAL;
306106266Sjulian				break;
307106266Sjulian			}
308106266Sjulian			/* TODO validation of packets */
309106266Sjulian			sc->packets = packets;
310106266Sjulian			ng_source_start(sc);
311106266Sjulian		    }
312106266Sjulian		    break;
313125033Sharti		case NGM_SOURCE_START_NOW:
314125033Sharti		    {
315125033Sharti			u_int64_t packets = *(u_int64_t *)msg->data;
316125033Sharti			if (sc->output.hook == NULL) {
317125033Sharti				printf("%s: start on node with no output hook\n"
318125077Sharti				    , __func__);
319125033Sharti				error = EINVAL;
320125033Sharti				break;
321125033Sharti			}
322125033Sharti			if (sc->node->nd_flags & NG_SOURCE_ACTIVE) {
323125033Sharti				error = EBUSY;
324125033Sharti				break;
325125033Sharti			}
326125033Sharti			/* TODO validation of packets */
327125033Sharti			sc->packets = packets;
328125033Sharti		        sc->output_ifp = NULL;
329125033Sharti
330125033Sharti			sc->node->nd_flags |= NG_SOURCE_ACTIVE;
331125033Sharti			timevalclear(&sc->stats.elapsedTime);
332125033Sharti			timevalclear(&sc->stats.endTime);
333125033Sharti			getmicrotime(&sc->stats.startTime);
334125243Sharti			sc->intr_ch = ng_timeout(node, NULL, 0,
335125243Sharti			    ng_source_intr, sc, 0);
336125033Sharti		    }
337125033Sharti		    break;
338106266Sjulian		case NGM_SOURCE_STOP:
339106266Sjulian			ng_source_stop(sc);
340106266Sjulian			break;
341106266Sjulian		case NGM_SOURCE_CLR_DATA:
342106266Sjulian			ng_source_clr_data(sc);
343106266Sjulian			break;
344106266Sjulian		default:
345106266Sjulian			error = EINVAL;
346106266Sjulian			break;
347106266Sjulian		}
348106266Sjulian		break;
349106435Sjulian	case NGM_ETHER_COOKIE:
350106435Sjulian		if (!(msg->header.flags & NGF_RESP)) {
351106435Sjulian			error = EINVAL;
352106435Sjulian			break;
353106435Sjulian		}
354106435Sjulian		switch (msg->header.cmd) {
355106435Sjulian		case NGM_ETHER_GET_IFINDEX:
356106435Sjulian			if (ng_source_store_output_ifp(sc, msg) == 0) {
357106435Sjulian				ng_source_set_autosrc(sc, 0);
358106435Sjulian				sc->node->nd_flags |= NG_SOURCE_ACTIVE;
359106435Sjulian				timevalclear(&sc->stats.elapsedTime);
360106435Sjulian				timevalclear(&sc->stats.endTime);
361106435Sjulian				getmicrotime(&sc->stats.startTime);
362125243Sharti				sc->intr_ch = ng_timeout(node, NULL, 0,
363125243Sharti				    ng_source_intr, sc, 0);
364106435Sjulian			}
365106435Sjulian			break;
366106435Sjulian		default:
367106435Sjulian			error = EINVAL;
368106435Sjulian		}
369106435Sjulian		break;
370106266Sjulian	default:
371106266Sjulian		error = EINVAL;
372106266Sjulian		break;
373106266Sjulian	}
374106266Sjulian
375106266Sjuliandone:
376106321Sjulian	/* Take care of synchronous response, if any */
377106321Sjulian	NG_RESPOND_MSG(error, node, item, resp);
378106321Sjulian	/* Free the message and return */
379106321Sjulian	NG_FREE_MSG(msg);
380106266Sjulian	return (error);
381106266Sjulian}
382106266Sjulian
383106266Sjulian/*
384106266Sjulian * Receive data on a hook
385106266Sjulian *
386106266Sjulian * If data comes in the input hook, enqueue it on the send queue.
387106266Sjulian * If data comes in the output hook, discard it.
388106266Sjulian */
389106266Sjulianstatic int
390106321Sjulianng_source_rcvdata(hook_p hook, item_p item)
391106266Sjulian{
392106321Sjulian	sc_p sc;
393106321Sjulian	struct source_hookinfo *hinfo;
394106266Sjulian	int error = 0;
395106321Sjulian	struct mbuf *m;
396106266Sjulian
397106321Sjulian	sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
398106321Sjulian	NGI_GET_M(item, m);
399106321Sjulian	NG_FREE_ITEM(item);
400106321Sjulian	hinfo = NG_HOOK_PRIVATE(hook);
401125077Sharti	KASSERT(sc != NULL, ("%s: null node private", __func__));
402125077Sharti	KASSERT(hinfo != NULL, ("%s: null hook info", __func__));
403106266Sjulian
404106266Sjulian	/* Which hook? */
405106266Sjulian	if (hinfo == &sc->output) {
406106266Sjulian		/* discard */
407106321Sjulian		NG_FREE_M(m);
408106266Sjulian		return (error);
409106266Sjulian	}
410125077Sharti	KASSERT(hinfo == &sc->input, ("%s: no hook!", __func__));
411106266Sjulian
412106266Sjulian	if ((m->m_flags & M_PKTHDR) == 0) {
413125077Sharti		printf("%s: mbuf without PKTHDR\n", __func__);
414106321Sjulian		NG_FREE_M(m);
415106266Sjulian		return (EINVAL);
416106266Sjulian	}
417106266Sjulian
418106266Sjulian	/* enque packet */
419106266Sjulian	/* XXX should we check IF_QFULL() ? */
420125031Sharti	_IF_ENQUEUE(&sc->snd_queue, m);
421106266Sjulian	sc->queueOctets += m->m_pkthdr.len;
422106266Sjulian
423106266Sjulian	return (0);
424106266Sjulian}
425106266Sjulian
426106266Sjulian/*
427106266Sjulian * Shutdown processing
428106266Sjulian */
429106266Sjulianstatic int
430106266Sjulianng_source_rmnode(node_p node)
431106266Sjulian{
432106321Sjulian	sc_p sc;
433106266Sjulian
434106321Sjulian	sc = NG_NODE_PRIVATE(node);
435125077Sharti	KASSERT(sc != NULL, ("%s: null node private", __func__));
436106321Sjulian	node->nd_flags |= NG_INVALID;
437106266Sjulian	ng_source_stop(sc);
438106266Sjulian	ng_source_clr_data(sc);
439106321Sjulian	NG_NODE_SET_PRIVATE(node, NULL);
440106321Sjulian	NG_NODE_UNREF(node);
441125077Sharti	free(sc, M_NETGRAPH);
442106266Sjulian	return (0);
443106266Sjulian}
444106266Sjulian
445106266Sjulian/*
446106266Sjulian * Hook disconnection
447106266Sjulian */
448106266Sjulianstatic int
449106266Sjulianng_source_disconnect(hook_p hook)
450106266Sjulian{
451106321Sjulian	struct source_hookinfo *hinfo;
452106319Sjulian	sc_p sc;
453106266Sjulian
454106321Sjulian	hinfo = NG_HOOK_PRIVATE(hook);
455106321Sjulian	sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
456125077Sharti	KASSERT(sc != NULL, ("%s: null node private", __func__));
457106266Sjulian	hinfo->hook = NULL;
458106321Sjulian	if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hinfo == &sc->output)
459106321Sjulian		ng_rmnode_self(NG_HOOK_NODE(hook));
460106266Sjulian	return (0);
461106266Sjulian}
462106266Sjulian
463106266Sjulian/*
464106435Sjulian *
465106435Sjulian * Ask out neighbour on the output hook side to send us it's interface
466106435Sjulian * information.
467106266Sjulian */
468125029Shartistatic int
469106435Sjulianng_source_request_output_ifp(sc_p sc)
470106266Sjulian{
471106435Sjulian	struct ng_mesg *msg;
472106266Sjulian	int error = 0;
473106266Sjulian
474106266Sjulian	sc->output_ifp = NULL;
475106266Sjulian
476106266Sjulian	/* Ask the attached node for the connected interface's index */
477106319Sjulian	NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFINDEX, 0, M_NOWAIT);
478106266Sjulian	if (msg == NULL)
479106266Sjulian		return (ENOBUFS);
480106266Sjulian
481125078Sharti	NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, 0);
482106435Sjulian	return (error);
483106435Sjulian}
484106266Sjulian
485106435Sjulian/*
486106435Sjulian * Set sc->output_ifp to point to the the struct ifnet of the interface
487106435Sjulian * reached via our output hook.
488106435Sjulian */
489106435Sjulianstatic int
490106435Sjulianng_source_store_output_ifp(sc_p sc, struct ng_mesg *msg)
491106435Sjulian{
492106435Sjulian	struct ifnet *ifp;
493106435Sjulian	u_int32_t if_index;
494106435Sjulian	int s;
495106266Sjulian
496106435Sjulian	if (msg->header.arglen < sizeof(u_int32_t))
497106266Sjulian		return (EINVAL);
498106266Sjulian
499106435Sjulian	if_index = *(u_int32_t *)msg->data;
500106266Sjulian	/* Could use ifindex2ifnet[if_index] except that we have no
501106266Sjulian	 * way of verifying if_index is valid since if_indexlim is
502106266Sjulian	 * local to if_attach()
503106266Sjulian	 */
504108172Shsu	IFNET_RLOCK();
505106266Sjulian	TAILQ_FOREACH(ifp, &ifnet, if_link) {
506106266Sjulian		if (ifp->if_index == if_index)
507106266Sjulian			break;
508106266Sjulian	}
509108172Shsu	IFNET_RUNLOCK();
510106266Sjulian
511106266Sjulian	if (ifp == NULL) {
512125077Sharti		printf("%s: can't find interface %d\n", __func__, if_index);
513106266Sjulian		return (EINVAL);
514106266Sjulian	}
515106266Sjulian	sc->output_ifp = ifp;
516106266Sjulian
517106266Sjulian#if 1
518106266Sjulian	/* XXX mucking with a drivers ifqueue size is ugly but we need it
519106266Sjulian	 * to queue a lot of packets to get close to line rate on a gigabit
520106266Sjulian	 * interface with small packets.
521106266Sjulian	 * XXX we should restore the original value at stop or disconnect
522106266Sjulian	 */
523106266Sjulian	s = splimp();		/* XXX is this required? */
524125077Sharti	if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN) {
525106266Sjulian		printf("ng_source: changing ifq_maxlen from %d to %d\n",
526106319Sjulian		    ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN);
527106266Sjulian		ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN;
528106266Sjulian	}
529106266Sjulian	splx(s);
530106266Sjulian#endif
531106266Sjulian	return (0);
532106266Sjulian}
533106266Sjulian
534106266Sjulian/*
535106266Sjulian * Set the attached ethernet node's ethernet source address override flag.
536106266Sjulian */
537106266Sjulianstatic int
538106266Sjulianng_source_set_autosrc(sc_p sc, u_int32_t flag)
539106266Sjulian{
540106266Sjulian	struct ng_mesg *msg;
541106266Sjulian	int error = 0;
542106266Sjulian
543106266Sjulian	NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC,
544125077Sharti	    sizeof (u_int32_t), M_NOWAIT);
545106266Sjulian	if (msg == NULL)
546106266Sjulian		return(ENOBUFS);
547106266Sjulian
548106266Sjulian	*(u_int32_t *)msg->data = flag;
549125078Sharti	NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, 0);
550106266Sjulian	return (error);
551106266Sjulian}
552106266Sjulian
553106266Sjulian/*
554106266Sjulian * Clear out the data we've queued
555106266Sjulian */
556106266Sjulianstatic void
557106266Sjulianng_source_clr_data (sc_p sc)
558106266Sjulian{
559106266Sjulian	struct mbuf *m;
560106266Sjulian
561106266Sjulian	for (;;) {
562125031Sharti		_IF_DEQUEUE(&sc->snd_queue, m);
563106266Sjulian		if (m == NULL)
564106266Sjulian			break;
565106266Sjulian		NG_FREE_M(m);
566106266Sjulian	}
567106266Sjulian	sc->queueOctets = 0;
568106266Sjulian}
569106266Sjulian
570106266Sjulian/*
571106266Sjulian * Start sending queued data out the output hook
572106266Sjulian */
573106266Sjulianstatic void
574106266Sjulianng_source_start (sc_p sc)
575106266Sjulian{
576106266Sjulian	KASSERT(sc->output.hook != NULL,
577125077Sharti			("%s: output hook unconnected", __func__));
578106435Sjulian	if (((sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) &&
579106435Sjulian	    (sc->output_ifp == NULL))
580106435Sjulian		ng_source_request_output_ifp(sc);
581106266Sjulian}
582106266Sjulian
583106266Sjulian/*
584106266Sjulian * Stop sending queued data out the output hook
585106266Sjulian */
586106266Sjulianstatic void
587106266Sjulianng_source_stop (sc_p sc)
588106266Sjulian{
589106321Sjulian	if (sc->node->nd_flags & NG_SOURCE_ACTIVE) {
590125243Sharti		ng_untimeout(sc->intr_ch, sc->node);
591106321Sjulian		sc->node->nd_flags &= ~NG_SOURCE_ACTIVE;
592106266Sjulian		getmicrotime(&sc->stats.endTime);
593106266Sjulian		sc->stats.elapsedTime = sc->stats.endTime;
594106266Sjulian		timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime);
595106266Sjulian		/* XXX should set this to the initial value instead */
596106266Sjulian		ng_source_set_autosrc(sc, 1);
597106266Sjulian	}
598106266Sjulian}
599106266Sjulian
600106266Sjulian/*
601106266Sjulian * While active called every NG_SOURCE_INTR_TICKS ticks.
602106266Sjulian * Sends as many packets as the interface connected to our
603106266Sjulian * output hook is able to enqueue.
604106266Sjulian */
605106266Sjulianstatic void
606125243Sharting_source_intr(node_p node, hook_p hook, void *arg1, int arg2)
607106266Sjulian{
608125243Sharti	sc_p sc = (sc_p)arg1;
609106266Sjulian	struct ifqueue *ifq;
610106266Sjulian	int packets;
611106266Sjulian
612125077Sharti	KASSERT(sc != NULL, ("%s: null node private", __func__));
613106266Sjulian
614106266Sjulian	callout_handle_init(&sc->intr_ch);
615106266Sjulian	if (sc->packets == 0 || sc->output.hook == NULL
616106321Sjulian	    || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) {
617106266Sjulian		ng_source_stop(sc);
618106266Sjulian		return;
619106266Sjulian	}
620106266Sjulian
621125033Sharti	if (sc->output_ifp != NULL) {
622125033Sharti		ifq = &sc->output_ifp->if_snd;
623125033Sharti		packets = ifq->ifq_maxlen - ifq->ifq_len;
624125033Sharti	} else
625125033Sharti		packets = sc->snd_queue.ifq_len;
626125033Sharti
627106266Sjulian	ng_source_send(sc, packets, NULL);
628125243Sharti	if (sc->packets == 0)
629106266Sjulian		ng_source_stop(sc);
630125243Sharti	else
631125243Sharti		sc->intr_ch = ng_timeout(node, NULL, 0,
632125243Sharti		    ng_source_intr, sc, NG_SOURCE_INTR_TICKS);
633106266Sjulian}
634106266Sjulian
635106266Sjulian/*
636106266Sjulian * Send packets out our output hook
637106266Sjulian */
638106266Sjulianstatic int
639106266Sjulianng_source_send (sc_p sc, int tosend, int *sent_p)
640106266Sjulian{
641106266Sjulian	struct ifqueue tmp_queue;
642106266Sjulian	struct mbuf *m, *m2;
643106266Sjulian	int sent = 0;
644106266Sjulian	int error = 0;
645106266Sjulian
646125077Sharti	KASSERT(sc != NULL, ("%s: null node private", __func__));
647125077Sharti	KASSERT(tosend >= 0, ("%s: negative tosend param", __func__));
648106321Sjulian	KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE,
649125077Sharti	    ("%s: inactive node", __func__));
650106266Sjulian
651106266Sjulian	if ((u_int64_t)tosend > sc->packets)
652106266Sjulian		tosend = sc->packets;
653106266Sjulian
654106266Sjulian	/* Copy the required number of packets to a temporary queue */
655106266Sjulian	bzero (&tmp_queue, sizeof (tmp_queue));
656106266Sjulian	for (sent = 0; error == 0 && sent < tosend; ++sent) {
657125031Sharti		_IF_DEQUEUE(&sc->snd_queue, m);
658106266Sjulian		if (m == NULL)
659106266Sjulian			break;
660106266Sjulian
661106266Sjulian		/* duplicate the packet */
662111119Simp		m2 = m_copypacket(m, M_DONTWAIT);
663106266Sjulian		if (m2 == NULL) {
664125031Sharti			_IF_PREPEND(&sc->snd_queue, m);
665106266Sjulian			error = ENOBUFS;
666106266Sjulian			break;
667106266Sjulian		}
668106266Sjulian
669106266Sjulian		/* re-enqueue the original packet for us */
670125031Sharti		_IF_ENQUEUE(&sc->snd_queue, m);
671106266Sjulian
672106266Sjulian		/* queue the copy for sending at smplimp */
673125031Sharti		_IF_ENQUEUE(&tmp_queue, m2);
674106266Sjulian	}
675106266Sjulian
676106266Sjulian	sent = 0;
677106266Sjulian	for (;;) {
678125031Sharti		_IF_DEQUEUE(&tmp_queue, m2);
679106266Sjulian		if (m2 == NULL)
680106266Sjulian			break;
681106266Sjulian		if (error == 0) {
682106266Sjulian			++sent;
683106266Sjulian			sc->stats.outFrames++;
684106266Sjulian			sc->stats.outOctets += m2->m_pkthdr.len;
685106266Sjulian			NG_SEND_DATA_ONLY(error, sc->output.hook, m2);
686125243Sharti			if (error)
687125243Sharti				printf("%s: error=%d\n", __func__, error);
688106266Sjulian		} else {
689106266Sjulian			NG_FREE_M(m2);
690106266Sjulian		}
691106266Sjulian	}
692106266Sjulian
693106266Sjulian	sc->packets -= sent;
694106266Sjulian	if (sent_p != NULL)
695106266Sjulian		*sent_p = sent;
696106266Sjulian	return (error);
697106266Sjulian}
698