ng_source.c revision 144674
1106266Sjulian/*
2106266Sjulian * ng_source.c
3139823Simp */
4139823Simp
5139823Simp/*-
6144674Sglebius * Copyright (c) 2005 Gleb Smirnoff <glebius@FreeBSD.org>
7106266Sjulian * Copyright 2002 Sandvine Inc.
8106266Sjulian * All rights reserved.
9106266Sjulian *
10106266Sjulian * Subject to the following obligations and disclaimer of warranty, use and
11106266Sjulian * redistribution of this software, in source or object code forms, with or
12106319Sjulian * without modifications are expressly permitted by Sandvine Inc.; provided,
13106266Sjulian * however, that:
14106319Sjulian * 1. Any and all reproductions of the source or object code must include the
15106319Sjulian *    copyright notice above and the following disclaimer of warranties; and
16106266Sjulian * 2. No rights are granted, in any manner or form, to use Sandvine Inc.
17106319Sjulian *    trademarks, including the mark "SANDVINE" on advertising, endorsements,
18106319Sjulian *    or otherwise except as such appears in the above copyright notice or in
19106266Sjulian *    the software.
20106266Sjulian *
21106266Sjulian * THIS SOFTWARE IS BEING PROVIDED BY SANDVINE "AS IS", AND TO THE MAXIMUM
22106319Sjulian * EXTENT PERMITTED BY LAW, SANDVINE MAKES NO REPRESENTATIONS OR WARRANTIES,
23106319Sjulian * EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, INCLUDING WITHOUT LIMITATION,
24106319Sjulian * ANY AND ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25106266Sjulian * PURPOSE, OR NON-INFRINGEMENT.  SANDVINE DOES NOT WARRANT, GUARANTEE, OR
26106266Sjulian * MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE
27106266Sjulian * USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY
28106266Sjulian * OR OTHERWISE.  IN NO EVENT SHALL SANDVINE BE LIABLE FOR ANY DAMAGES
29106266Sjulian * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30106319Sjulian * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31106266Sjulian * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32106266Sjulian * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33106266Sjulian * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34106266Sjulian * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35106266Sjulian * THIS SOFTWARE, EVEN IF SANDVINE IS ADVISED OF THE POSSIBILITY OF SUCH
36106266Sjulian * DAMAGE.
37106266Sjulian *
38106266Sjulian * Author: Dave Chapeskie <dchapeskie@sandvine.com>
39106266Sjulian */
40106266Sjulian
41125077Sharti#include <sys/cdefs.h>
42125077Sharti__FBSDID("$FreeBSD: head/sys/netgraph/ng_source.c 144674 2005-04-05 17:22:05Z glebius $");
43125077Sharti
44106266Sjulian/*
45106266Sjulian * This node is used for high speed packet geneneration.  It queues
46143387Sbmilekic * all data recieved on its 'input' hook and when told to start via
47143387Sbmilekic * a control message it sends the packets out its 'output' hook.  In
48143387Sbmilekic * this way this node can be preloaded with a packet stream which it
49143387Sbmilekic * can then send continuously as fast as possible.
50106266Sjulian *
51106266Sjulian * Currently it just copies the mbufs as required.  It could do various
52106266Sjulian * tricks to try and avoid this.  Probably the best performance would
53106266Sjulian * be achieved by modifying the appropriate drivers to be told to
54106266Sjulian * self-re-enqueue packets (e.g. the if_bge driver could reuse the same
55106266Sjulian * transmit descriptors) under control of this node; perhaps via some
56143387Sbmilekic * flag in the mbuf or some such.  The node could peek at an appropriate
57106266Sjulian * ifnet flag to see if such support is available for the connected
58106266Sjulian * interface.
59106266Sjulian */
60106266Sjulian
61106266Sjulian#include <sys/param.h>
62106266Sjulian#include <sys/systm.h>
63106266Sjulian#include <sys/errno.h>
64106266Sjulian#include <sys/kernel.h>
65106266Sjulian#include <sys/malloc.h>
66106266Sjulian#include <sys/mbuf.h>
67106266Sjulian#include <sys/socket.h>
68144674Sglebius#include <sys/syslog.h>
69106266Sjulian#include <net/if.h>
70106266Sjulian#include <net/if_var.h>
71106266Sjulian#include <netgraph/ng_message.h>
72106266Sjulian#include <netgraph/netgraph.h>
73106266Sjulian#include <netgraph/ng_parse.h>
74106266Sjulian#include <netgraph/ng_ether.h>
75106266Sjulian#include <netgraph/ng_source.h>
76106266Sjulian
77106266Sjulian#define NG_SOURCE_INTR_TICKS		1
78106266Sjulian#define NG_SOURCE_DRIVER_IFQ_MAXLEN	(4*1024)
79106266Sjulian
80106266Sjulian/* Per node info */
81106266Sjulianstruct privdata {
82106266Sjulian	node_p				node;
83144674Sglebius	hook_p				input;
84144674Sglebius	hook_p				output;
85106266Sjulian	struct ng_source_stats		stats;
86106319Sjulian	struct ifqueue			snd_queue;	/* packets to send */
87106266Sjulian	struct ifnet			*output_ifp;
88137138Sglebius	struct callout			intr_ch;
89144674Sglebius	uint64_t			packets;	/* packets to send */
90144674Sglebius	uint32_t			queueOctets;
91106266Sjulian};
92106266Sjuliantypedef struct privdata *sc_p;
93106266Sjulian
94106266Sjulian/* Node flags */
95106266Sjulian#define NG_SOURCE_ACTIVE	(NGF_TYPE1)
96106266Sjulian
97106266Sjulian/* Netgraph methods */
98106266Sjulianstatic ng_constructor_t	ng_source_constructor;
99106266Sjulianstatic ng_rcvmsg_t	ng_source_rcvmsg;
100106266Sjulianstatic ng_shutdown_t	ng_source_rmnode;
101106266Sjulianstatic ng_newhook_t	ng_source_newhook;
102144674Sglebiusstatic ng_connect_t	ng_source_connect;
103106266Sjulianstatic ng_rcvdata_t	ng_source_rcvdata;
104106266Sjulianstatic ng_disconnect_t	ng_source_disconnect;
105106266Sjulian
106106266Sjulian/* Other functions */
107125243Shartistatic void		ng_source_intr(node_p, hook_p, void *, int);
108106266Sjulianstatic void		ng_source_clr_data (sc_p);
109144674Sglebiusstatic int		ng_source_start (sc_p, uint64_t);
110106266Sjulianstatic void		ng_source_stop (sc_p);
111106266Sjulianstatic int		ng_source_send (sc_p, int, int *);
112144674Sglebiusstatic int		ng_source_store_output_ifp(sc_p, char *);
113106266Sjulian
114106266Sjulian/* Parse type for timeval */
115125077Shartistatic const struct ng_parse_struct_field ng_source_timeval_type_fields[] = {
116106266Sjulian	{ "tv_sec",		&ng_parse_int32_type	},
117106266Sjulian	{ "tv_usec",		&ng_parse_int32_type	},
118106266Sjulian	{ NULL }
119106266Sjulian};
120106266Sjulianconst struct ng_parse_type ng_source_timeval_type = {
121106266Sjulian	&ng_parse_struct_type,
122106266Sjulian	&ng_source_timeval_type_fields
123106266Sjulian};
124106266Sjulian
125106266Sjulian/* Parse type for struct ng_source_stats */
126106266Sjulianstatic const struct ng_parse_struct_field ng_source_stats_type_fields[]
127106266Sjulian	= NG_SOURCE_STATS_TYPE_INFO;
128106266Sjulianstatic const struct ng_parse_type ng_source_stats_type = {
129106266Sjulian	&ng_parse_struct_type,
130106266Sjulian	&ng_source_stats_type_fields
131106266Sjulian};
132106266Sjulian
133106266Sjulian/* List of commands and how to convert arguments to/from ASCII */
134106266Sjulianstatic const struct ng_cmdlist ng_source_cmds[] = {
135106266Sjulian	{
136106266Sjulian	  NGM_SOURCE_COOKIE,
137106266Sjulian	  NGM_SOURCE_GET_STATS,
138106266Sjulian	  "getstats",
139106266Sjulian	  NULL,
140106266Sjulian	  &ng_source_stats_type
141106266Sjulian	},
142106266Sjulian	{
143106266Sjulian	  NGM_SOURCE_COOKIE,
144106266Sjulian	  NGM_SOURCE_CLR_STATS,
145106266Sjulian	  "clrstats",
146106266Sjulian	  NULL,
147106266Sjulian	  NULL
148106266Sjulian	},
149106266Sjulian	{
150106266Sjulian	  NGM_SOURCE_COOKIE,
151106266Sjulian	  NGM_SOURCE_GETCLR_STATS,
152106266Sjulian	  "getclrstats",
153106266Sjulian	  NULL,
154106266Sjulian	  &ng_source_stats_type
155106266Sjulian	},
156106266Sjulian	{
157106266Sjulian	  NGM_SOURCE_COOKIE,
158106266Sjulian	  NGM_SOURCE_START,
159106266Sjulian	  "start",
160106266Sjulian	  &ng_parse_uint64_type,
161106266Sjulian	  NULL
162106266Sjulian	},
163106266Sjulian	{
164106266Sjulian	  NGM_SOURCE_COOKIE,
165106266Sjulian	  NGM_SOURCE_STOP,
166106266Sjulian	  "stop",
167106266Sjulian	  NULL,
168106266Sjulian	  NULL
169106266Sjulian	},
170106266Sjulian	{
171106266Sjulian	  NGM_SOURCE_COOKIE,
172106266Sjulian	  NGM_SOURCE_CLR_DATA,
173106266Sjulian	  "clrdata",
174106266Sjulian	  NULL,
175106266Sjulian	  NULL
176106266Sjulian	},
177125033Sharti	{
178125033Sharti	  NGM_SOURCE_COOKIE,
179144674Sglebius	  NGM_SOURCE_SETIFACE,
180144674Sglebius	  "setiface",
181144674Sglebius	  &ng_parse_string_type,
182125033Sharti	  NULL
183125033Sharti	},
184106266Sjulian	{ 0 }
185106266Sjulian};
186106266Sjulian
187106266Sjulian/* Netgraph type descriptor */
188106266Sjulianstatic struct ng_type ng_source_typestruct = {
189129823Sjulian	.version =	NG_ABI_VERSION,
190129823Sjulian	.name =		NG_SOURCE_NODE_TYPE,
191129823Sjulian	.constructor =	ng_source_constructor,
192129823Sjulian	.rcvmsg =	ng_source_rcvmsg,
193129823Sjulian	.shutdown =	ng_source_rmnode,
194129823Sjulian	.newhook =	ng_source_newhook,
195144674Sglebius	.connect =	ng_source_connect,
196129823Sjulian	.rcvdata =	ng_source_rcvdata,
197129823Sjulian	.disconnect =	ng_source_disconnect,
198129823Sjulian	.cmdlist =	ng_source_cmds,
199106266Sjulian};
200106266SjulianNETGRAPH_INIT(source, &ng_source_typestruct);
201106266Sjulian
202144674Sglebiusstatic int ng_source_set_autosrc(sc_p, uint32_t);
203125032Sharti
204106266Sjulian/*
205106266Sjulian * Node constructor
206106266Sjulian */
207106266Sjulianstatic int
208106321Sjulianng_source_constructor(node_p node)
209106266Sjulian{
210106266Sjulian	sc_p sc;
211106266Sjulian
212125030Sharti	sc = malloc(sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO);
213106266Sjulian	if (sc == NULL)
214106266Sjulian		return (ENOMEM);
215106266Sjulian
216106321Sjulian	NG_NODE_SET_PRIVATE(node, sc);
217106321Sjulian	sc->node = node;
218106266Sjulian	sc->snd_queue.ifq_maxlen = 2048;	/* XXX not checked */
219137138Sglebius	ng_callout_init(&sc->intr_ch);
220137138Sglebius
221106266Sjulian	return (0);
222106266Sjulian}
223106266Sjulian
224106266Sjulian/*
225106266Sjulian * Add a hook
226106266Sjulian */
227106266Sjulianstatic int
228106266Sjulianng_source_newhook(node_p node, hook_p hook, const char *name)
229106266Sjulian{
230144674Sglebius	sc_p sc = NG_NODE_PRIVATE(node);
231106266Sjulian
232106266Sjulian	if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) {
233144674Sglebius		sc->input = hook;
234106266Sjulian	} else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) {
235144674Sglebius		sc->output = hook;
236106266Sjulian		sc->output_ifp = 0;
237106266Sjulian		bzero(&sc->stats, sizeof(sc->stats));
238106266Sjulian	} else
239106266Sjulian		return (EINVAL);
240144674Sglebius
241106266Sjulian	return (0);
242106266Sjulian}
243106266Sjulian
244106266Sjulian/*
245144674Sglebius * Hook has been added
246144674Sglebius */
247144674Sglebiusstatic int
248144674Sglebiusng_source_connect(hook_p hook)
249144674Sglebius{
250144674Sglebius	sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
251144674Sglebius	struct ng_mesg *msg;
252144674Sglebius	int dummy_error = 0;
253144674Sglebius
254144674Sglebius	/*
255144674Sglebius	 * If this is "output" hook, then request information
256144674Sglebius	 * from our downstream.
257144674Sglebius	 */
258144674Sglebius	if (hook == sc->output) {
259144674Sglebius		NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFNAME,
260144674Sglebius		    0, M_NOWAIT);
261144674Sglebius		if (msg == NULL)
262144674Sglebius			return (ENOBUFS);
263144674Sglebius
264144674Sglebius		/*
265144674Sglebius		 * Our hook and peer hook have HK_INVALID flag set,
266144674Sglebius		 * so we can't use NG_SEND_MSG_HOOK() macro here.
267144674Sglebius		 */
268144674Sglebius		NG_SEND_MSG_ID(dummy_error, sc->node, msg,
269144674Sglebius		    NG_NODE_ID(NG_PEER_NODE(sc->output)), NG_NODE_ID(sc->node));
270144674Sglebius	}
271144674Sglebius
272144674Sglebius	return (0);
273144674Sglebius}
274144674Sglebius
275144674Sglebius/*
276106266Sjulian * Receive a control message
277106266Sjulian */
278106266Sjulianstatic int
279106321Sjulianng_source_rcvmsg(node_p node, item_p item, hook_p lasthook)
280106266Sjulian{
281144674Sglebius	sc_p sc = NG_NODE_PRIVATE(node);
282144674Sglebius	struct ng_mesg *msg, *resp = NULL;
283106266Sjulian	int error = 0;
284106266Sjulian
285106321Sjulian	NGI_GET_MSG(item, msg);
286144674Sglebius
287106266Sjulian	switch (msg->header.typecookie) {
288106266Sjulian	case NGM_SOURCE_COOKIE:
289106435Sjulian		if (msg->header.flags & NGF_RESP) {
290106435Sjulian			error = EINVAL;
291106435Sjulian			break;
292106435Sjulian		}
293106266Sjulian		switch (msg->header.cmd) {
294106266Sjulian		case NGM_SOURCE_GET_STATS:
295106266Sjulian		case NGM_SOURCE_CLR_STATS:
296106266Sjulian		case NGM_SOURCE_GETCLR_STATS:
297106266Sjulian                    {
298106266Sjulian			struct ng_source_stats *stats;
299106266Sjulian
300106266Sjulian                        if (msg->header.cmd != NGM_SOURCE_CLR_STATS) {
301106266Sjulian                                NG_MKRESPONSE(resp, msg,
302106266Sjulian                                    sizeof(*stats), M_NOWAIT);
303106266Sjulian				if (resp == NULL) {
304106266Sjulian					error = ENOMEM;
305106266Sjulian					goto done;
306106266Sjulian				}
307106266Sjulian				sc->stats.queueOctets = sc->queueOctets;
308106319Sjulian				sc->stats.queueFrames = sc->snd_queue.ifq_len;
309106321Sjulian				if ((sc->node->nd_flags & NG_SOURCE_ACTIVE)
310106266Sjulian				    && !timevalisset(&sc->stats.endTime)) {
311106319Sjulian					getmicrotime(&sc->stats.elapsedTime);
312106266Sjulian					timevalsub(&sc->stats.elapsedTime,
313106319Sjulian					    &sc->stats.startTime);
314106266Sjulian				}
315106319Sjulian				stats = (struct ng_source_stats *)resp->data;
316106266Sjulian				bcopy(&sc->stats, stats, sizeof(* stats));
317106266Sjulian                        }
318106266Sjulian                        if (msg->header.cmd != NGM_SOURCE_GET_STATS)
319106266Sjulian				bzero(&sc->stats, sizeof(sc->stats));
320106266Sjulian		    }
321106266Sjulian		    break;
322106266Sjulian		case NGM_SOURCE_START:
323106266Sjulian		    {
324144674Sglebius			uint64_t packets;
325144674Sglebius
326144674Sglebius			if (msg->header.arglen != sizeof(uint64_t)) {
327106266Sjulian				error = EINVAL;
328106266Sjulian				break;
329106266Sjulian			}
330125033Sharti
331144674Sglebius			packets = *(uint64_t *)msg->data;
332144674Sglebius
333144674Sglebius			error = ng_source_start(sc, packets);
334144674Sglebius
335144674Sglebius		    	break;
336125033Sharti		    }
337106266Sjulian		case NGM_SOURCE_STOP:
338106266Sjulian			ng_source_stop(sc);
339106266Sjulian			break;
340106266Sjulian		case NGM_SOURCE_CLR_DATA:
341106266Sjulian			ng_source_clr_data(sc);
342106266Sjulian			break;
343144674Sglebius		case NGM_SOURCE_SETIFACE:
344144674Sglebius		    {
345144674Sglebius			char *ifname = (char *)msg->data;
346144674Sglebius
347144674Sglebius			if (msg->header.arglen < 2) {
348144674Sglebius				error = EINVAL;
349144674Sglebius				break;
350144674Sglebius			}
351144674Sglebius
352144674Sglebius			ng_source_store_output_ifp(sc, ifname);
353144674Sglebius			break;
354144674Sglebius		    }
355106266Sjulian		default:
356106266Sjulian			error = EINVAL;
357106266Sjulian			break;
358106266Sjulian		}
359106266Sjulian		break;
360106435Sjulian	case NGM_ETHER_COOKIE:
361106435Sjulian		if (!(msg->header.flags & NGF_RESP)) {
362106435Sjulian			error = EINVAL;
363106435Sjulian			break;
364106435Sjulian		}
365106435Sjulian		switch (msg->header.cmd) {
366144674Sglebius		case NGM_ETHER_GET_IFNAME:
367144674Sglebius		    {
368144674Sglebius			char *ifname = (char *)msg->data;
369144674Sglebius
370144674Sglebius			if (msg->header.arglen < 2) {
371144674Sglebius				error = EINVAL;
372144674Sglebius				break;
373144674Sglebius			}
374144674Sglebius
375144674Sglebius			if (ng_source_store_output_ifp(sc, ifname) == 0)
376106435Sjulian				ng_source_set_autosrc(sc, 0);
377106435Sjulian			break;
378144674Sglebius		    }
379106435Sjulian		default:
380106435Sjulian			error = EINVAL;
381106435Sjulian		}
382106435Sjulian		break;
383106266Sjulian	default:
384106266Sjulian		error = EINVAL;
385106266Sjulian		break;
386106266Sjulian	}
387106266Sjulian
388106266Sjuliandone:
389144674Sglebius	/* Take care of synchronous response, if any. */
390106321Sjulian	NG_RESPOND_MSG(error, node, item, resp);
391144674Sglebius	/* Free the message and return. */
392106321Sjulian	NG_FREE_MSG(msg);
393106266Sjulian	return (error);
394106266Sjulian}
395106266Sjulian
396106266Sjulian/*
397106266Sjulian * Receive data on a hook
398106266Sjulian *
399106266Sjulian * If data comes in the input hook, enqueue it on the send queue.
400106266Sjulian * If data comes in the output hook, discard it.
401106266Sjulian */
402106266Sjulianstatic int
403106321Sjulianng_source_rcvdata(hook_p hook, item_p item)
404106266Sjulian{
405144674Sglebius	sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
406144674Sglebius	struct mbuf *m;
407106266Sjulian	int error = 0;
408106266Sjulian
409106321Sjulian	NGI_GET_M(item, m);
410106321Sjulian	NG_FREE_ITEM(item);
411106266Sjulian
412106266Sjulian	/* Which hook? */
413144674Sglebius	if (hook == sc->output) {
414106266Sjulian		/* discard */
415106321Sjulian		NG_FREE_M(m);
416106266Sjulian		return (error);
417106266Sjulian	}
418144674Sglebius	KASSERT(hook == sc->input, ("%s: no hook!", __func__));
419106266Sjulian
420144674Sglebius	/* Enqueue packet. */
421106266Sjulian	/* XXX should we check IF_QFULL() ? */
422125031Sharti	_IF_ENQUEUE(&sc->snd_queue, m);
423106266Sjulian	sc->queueOctets += m->m_pkthdr.len;
424106266Sjulian
425106266Sjulian	return (0);
426106266Sjulian}
427106266Sjulian
428106266Sjulian/*
429106266Sjulian * Shutdown processing
430106266Sjulian */
431106266Sjulianstatic int
432106266Sjulianng_source_rmnode(node_p node)
433106266Sjulian{
434144674Sglebius	sc_p sc = NG_NODE_PRIVATE(node);
435106266Sjulian
436106266Sjulian	ng_source_stop(sc);
437106266Sjulian	ng_source_clr_data(sc);
438106321Sjulian	NG_NODE_SET_PRIVATE(node, NULL);
439106321Sjulian	NG_NODE_UNREF(node);
440125077Sharti	free(sc, M_NETGRAPH);
441144674Sglebius
442106266Sjulian	return (0);
443106266Sjulian}
444106266Sjulian
445106266Sjulian/*
446106266Sjulian * Hook disconnection
447106266Sjulian */
448106266Sjulianstatic int
449106266Sjulianng_source_disconnect(hook_p hook)
450106266Sjulian{
451106319Sjulian	sc_p sc;
452106266Sjulian
453106321Sjulian	sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
454125077Sharti	KASSERT(sc != NULL, ("%s: null node private", __func__));
455144674Sglebius	if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hook == sc->output)
456106321Sjulian		ng_rmnode_self(NG_HOOK_NODE(hook));
457106266Sjulian	return (0);
458106266Sjulian}
459106266Sjulian
460106266Sjulian/*
461106435Sjulian * Set sc->output_ifp to point to the the struct ifnet of the interface
462106435Sjulian * reached via our output hook.
463106435Sjulian */
464106435Sjulianstatic int
465144674Sglebiusng_source_store_output_ifp(sc_p sc, char *ifname)
466106435Sjulian{
467106435Sjulian	struct ifnet *ifp;
468106435Sjulian	int s;
469106266Sjulian
470144674Sglebius	ifp = ifunit(ifname);
471106266Sjulian
472106266Sjulian	if (ifp == NULL) {
473125077Sharti		printf("%s: can't find interface %d\n", __func__, if_index);
474106266Sjulian		return (EINVAL);
475106266Sjulian	}
476106266Sjulian	sc->output_ifp = ifp;
477106266Sjulian
478106266Sjulian#if 1
479106266Sjulian	/* XXX mucking with a drivers ifqueue size is ugly but we need it
480106266Sjulian	 * to queue a lot of packets to get close to line rate on a gigabit
481106266Sjulian	 * interface with small packets.
482106266Sjulian	 * XXX we should restore the original value at stop or disconnect
483106266Sjulian	 */
484106266Sjulian	s = splimp();		/* XXX is this required? */
485125077Sharti	if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN) {
486106266Sjulian		printf("ng_source: changing ifq_maxlen from %d to %d\n",
487106319Sjulian		    ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN);
488106266Sjulian		ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN;
489106266Sjulian	}
490106266Sjulian	splx(s);
491106266Sjulian#endif
492106266Sjulian	return (0);
493106266Sjulian}
494106266Sjulian
495106266Sjulian/*
496106266Sjulian * Set the attached ethernet node's ethernet source address override flag.
497106266Sjulian */
498106266Sjulianstatic int
499144674Sglebiusng_source_set_autosrc(sc_p sc, uint32_t flag)
500106266Sjulian{
501106266Sjulian	struct ng_mesg *msg;
502106266Sjulian	int error = 0;
503106266Sjulian
504106266Sjulian	NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC,
505144674Sglebius	    sizeof (uint32_t), M_NOWAIT);
506106266Sjulian	if (msg == NULL)
507106266Sjulian		return(ENOBUFS);
508106266Sjulian
509144674Sglebius	*(uint32_t *)msg->data = flag;
510144674Sglebius	NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output, 0);
511106266Sjulian	return (error);
512106266Sjulian}
513106266Sjulian
514106266Sjulian/*
515106266Sjulian * Clear out the data we've queued
516106266Sjulian */
517106266Sjulianstatic void
518106266Sjulianng_source_clr_data (sc_p sc)
519106266Sjulian{
520106266Sjulian	struct mbuf *m;
521106266Sjulian
522106266Sjulian	for (;;) {
523125031Sharti		_IF_DEQUEUE(&sc->snd_queue, m);
524106266Sjulian		if (m == NULL)
525106266Sjulian			break;
526106266Sjulian		NG_FREE_M(m);
527106266Sjulian	}
528106266Sjulian	sc->queueOctets = 0;
529106266Sjulian}
530106266Sjulian
531106266Sjulian/*
532106266Sjulian * Start sending queued data out the output hook
533106266Sjulian */
534144674Sglebiusstatic int
535144674Sglebiusng_source_start(sc_p sc, uint64_t packets)
536106266Sjulian{
537144674Sglebius	if (sc->output_ifp == NULL) {
538144674Sglebius		printf("ng_source: start without iface configured\n");
539144674Sglebius		return (ENXIO);
540144674Sglebius	}
541144674Sglebius
542144674Sglebius	if (sc->node->nd_flags & NG_SOURCE_ACTIVE)
543144674Sglebius		return (EBUSY);
544144674Sglebius
545144674Sglebius	sc->node->nd_flags |= NG_SOURCE_ACTIVE;
546144674Sglebius
547144674Sglebius	sc->packets = packets;
548144674Sglebius	timevalclear(&sc->stats.elapsedTime);
549144674Sglebius	timevalclear(&sc->stats.endTime);
550144674Sglebius	getmicrotime(&sc->stats.startTime);
551144674Sglebius	ng_callout(&sc->intr_ch, sc->node, NULL, 0,
552144674Sglebius	    ng_source_intr, sc, 0);
553144674Sglebius
554144674Sglebius	return (0);
555106266Sjulian}
556106266Sjulian
557106266Sjulian/*
558106266Sjulian * Stop sending queued data out the output hook
559106266Sjulian */
560106266Sjulianstatic void
561144674Sglebiusng_source_stop(sc_p sc)
562106266Sjulian{
563144674Sglebius	ng_uncallout(&sc->intr_ch, sc->node);
564144674Sglebius	sc->node->nd_flags &= ~NG_SOURCE_ACTIVE;
565144674Sglebius	getmicrotime(&sc->stats.endTime);
566144674Sglebius	sc->stats.elapsedTime = sc->stats.endTime;
567144674Sglebius	timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime);
568106266Sjulian}
569106266Sjulian
570106266Sjulian/*
571106266Sjulian * While active called every NG_SOURCE_INTR_TICKS ticks.
572106266Sjulian * Sends as many packets as the interface connected to our
573106266Sjulian * output hook is able to enqueue.
574106266Sjulian */
575106266Sjulianstatic void
576125243Sharting_source_intr(node_p node, hook_p hook, void *arg1, int arg2)
577106266Sjulian{
578125243Sharti	sc_p sc = (sc_p)arg1;
579106266Sjulian	struct ifqueue *ifq;
580106266Sjulian	int packets;
581106266Sjulian
582125077Sharti	KASSERT(sc != NULL, ("%s: null node private", __func__));
583106266Sjulian
584144674Sglebius	if (sc->packets == 0 || sc->output == NULL
585106321Sjulian	    || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) {
586106266Sjulian		ng_source_stop(sc);
587106266Sjulian		return;
588106266Sjulian	}
589106266Sjulian
590125033Sharti	if (sc->output_ifp != NULL) {
591141745Sru		ifq = (struct ifqueue *)&sc->output_ifp->if_snd;
592125033Sharti		packets = ifq->ifq_maxlen - ifq->ifq_len;
593125033Sharti	} else
594125033Sharti		packets = sc->snd_queue.ifq_len;
595125033Sharti
596106266Sjulian	ng_source_send(sc, packets, NULL);
597125243Sharti	if (sc->packets == 0)
598106266Sjulian		ng_source_stop(sc);
599125243Sharti	else
600138268Sglebius		ng_callout(&sc->intr_ch, node, NULL, NG_SOURCE_INTR_TICKS,
601137136Sglebius		    ng_source_intr, sc, 0);
602106266Sjulian}
603106266Sjulian
604106266Sjulian/*
605106266Sjulian * Send packets out our output hook
606106266Sjulian */
607106266Sjulianstatic int
608106266Sjulianng_source_send (sc_p sc, int tosend, int *sent_p)
609106266Sjulian{
610106266Sjulian	struct ifqueue tmp_queue;
611106266Sjulian	struct mbuf *m, *m2;
612106266Sjulian	int sent = 0;
613106266Sjulian	int error = 0;
614106266Sjulian
615125077Sharti	KASSERT(tosend >= 0, ("%s: negative tosend param", __func__));
616106321Sjulian	KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE,
617125077Sharti	    ("%s: inactive node", __func__));
618106266Sjulian
619144674Sglebius	if ((uint64_t)tosend > sc->packets)
620106266Sjulian		tosend = sc->packets;
621106266Sjulian
622106266Sjulian	/* Copy the required number of packets to a temporary queue */
623106266Sjulian	bzero (&tmp_queue, sizeof (tmp_queue));
624106266Sjulian	for (sent = 0; error == 0 && sent < tosend; ++sent) {
625125031Sharti		_IF_DEQUEUE(&sc->snd_queue, m);
626106266Sjulian		if (m == NULL)
627106266Sjulian			break;
628106266Sjulian
629106266Sjulian		/* duplicate the packet */
630111119Simp		m2 = m_copypacket(m, M_DONTWAIT);
631106266Sjulian		if (m2 == NULL) {
632125031Sharti			_IF_PREPEND(&sc->snd_queue, m);
633106266Sjulian			error = ENOBUFS;
634106266Sjulian			break;
635106266Sjulian		}
636106266Sjulian
637144674Sglebius		/* Re-enqueue the original packet for us. */
638125031Sharti		_IF_ENQUEUE(&sc->snd_queue, m);
639106266Sjulian
640144674Sglebius		/* Queue the copy for sending at splimp. */
641125031Sharti		_IF_ENQUEUE(&tmp_queue, m2);
642106266Sjulian	}
643106266Sjulian
644106266Sjulian	sent = 0;
645106266Sjulian	for (;;) {
646125031Sharti		_IF_DEQUEUE(&tmp_queue, m2);
647106266Sjulian		if (m2 == NULL)
648106266Sjulian			break;
649106266Sjulian		if (error == 0) {
650106266Sjulian			++sent;
651106266Sjulian			sc->stats.outFrames++;
652106266Sjulian			sc->stats.outOctets += m2->m_pkthdr.len;
653144674Sglebius			NG_SEND_DATA_ONLY(error, sc->output, m2);
654125243Sharti			if (error)
655144674Sglebius				log(LOG_DEBUG, "%s: error=%d", __func__, error);
656106266Sjulian		} else {
657106266Sjulian			NG_FREE_M(m2);
658106266Sjulian		}
659106266Sjulian	}
660106266Sjulian
661106266Sjulian	sc->packets -= sent;
662106266Sjulian	if (sent_p != NULL)
663106266Sjulian		*sent_p = sent;
664106266Sjulian	return (error);
665106266Sjulian}
666