xref: /freebsd/sys/netgraph/ng_source.c (revision 5ae50c80859f7da940bfb972337845a59158e81d)
1 /*
2  * ng_source.c
3  *
4  * Copyright 2002 Sandvine Inc.
5  * All rights reserved.
6  *
7  * Subject to the following obligations and disclaimer of warranty, use and
8  * redistribution of this software, in source or object code forms, with or
9  * without modifications are expressly permitted by Sandvine Inc.; provided,
10  * however, that:
11  * 1. Any and all reproductions of the source or object code must include the
12  *    copyright notice above and the following disclaimer of warranties; and
13  * 2. No rights are granted, in any manner or form, to use Sandvine Inc.
14  *    trademarks, including the mark "SANDVINE" on advertising, endorsements,
15  *    or otherwise except as such appears in the above copyright notice or in
16  *    the software.
17  *
18  * THIS SOFTWARE IS BEING PROVIDED BY SANDVINE "AS IS", AND TO THE MAXIMUM
19  * EXTENT PERMITTED BY LAW, SANDVINE MAKES NO REPRESENTATIONS OR WARRANTIES,
20  * EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, INCLUDING WITHOUT LIMITATION,
21  * ANY AND ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22  * PURPOSE, OR NON-INFRINGEMENT.  SANDVINE DOES NOT WARRANT, GUARANTEE, OR
23  * MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE
24  * USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY
25  * OR OTHERWISE.  IN NO EVENT SHALL SANDVINE BE LIABLE FOR ANY DAMAGES
26  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
27  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF SANDVINE IS ADVISED OF THE POSSIBILITY OF SUCH
33  * DAMAGE.
34  *
35  * Author: Dave Chapeskie <dchapeskie@sandvine.com>
36  */
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 /*
42  * This node is used for high speed packet geneneration.  It queues
43  * all data recieved on it's 'input' hook and when told to start via
44  * a control message it sends the packets out it's 'output' hook.  In
45  * this way this node can be preloaded with a packet stream which is
46  * continuously sent.
47  *
48  * Currently it just copies the mbufs as required.  It could do various
49  * tricks to try and avoid this.  Probably the best performance would
50  * be achieved by modifying the appropriate drivers to be told to
51  * self-re-enqueue packets (e.g. the if_bge driver could reuse the same
52  * transmit descriptors) under control of this node; perhaps via some
53  * flag in the mbuf or some such.  The node would peak at an appropriate
54  * ifnet flag to see if such support is available for the connected
55  * interface.
56  */
57 
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/errno.h>
61 #include <sys/kernel.h>
62 #include <sys/malloc.h>
63 #include <sys/mbuf.h>
64 #include <sys/socket.h>
65 #include <net/if.h>
66 #include <net/if_var.h>
67 #include <netgraph/ng_message.h>
68 #include <netgraph/netgraph.h>
69 #include <netgraph/ng_parse.h>
70 #include <netgraph/ng_ether.h>
71 #include <netgraph/ng_source.h>
72 
73 #define NG_SOURCE_INTR_TICKS		1
74 #define NG_SOURCE_DRIVER_IFQ_MAXLEN	(4*1024)
75 
76 
77 /* Per hook info */
78 struct source_hookinfo {
79 	hook_p				hook;
80 };
81 
82 /* Per node info */
83 struct privdata {
84 	node_p				node;
85 	struct source_hookinfo		input;
86 	struct source_hookinfo		output;
87 	struct ng_source_stats		stats;
88 	struct ifqueue			snd_queue;	/* packets to send */
89 	struct ifnet			*output_ifp;
90 	struct callout_handle		intr_ch;
91 	u_int64_t			packets;	/* packets to send */
92 	u_int32_t			queueOctets;
93 };
94 typedef struct privdata *sc_p;
95 
96 /* Node flags */
97 #define NG_SOURCE_ACTIVE	(NGF_TYPE1)
98 
99 /* Netgraph methods */
100 static ng_constructor_t	ng_source_constructor;
101 static ng_rcvmsg_t	ng_source_rcvmsg;
102 static ng_shutdown_t	ng_source_rmnode;
103 static ng_newhook_t	ng_source_newhook;
104 static ng_rcvdata_t	ng_source_rcvdata;
105 static ng_disconnect_t	ng_source_disconnect;
106 
107 /* Other functions */
108 static timeout_t	ng_source_intr;
109 static int		ng_source_request_output_ifp (sc_p);
110 static void		ng_source_clr_data (sc_p);
111 static void		ng_source_start (sc_p);
112 static void		ng_source_stop (sc_p);
113 static int		ng_source_send (sc_p, int, int *);
114 static int		ng_source_store_output_ifp(sc_p sc,
115 			    struct ng_mesg *msg);
116 
117 
118 /* Parse type for timeval */
119 static const struct ng_parse_struct_field ng_source_timeval_type_fields[] = {
120 	{ "tv_sec",		&ng_parse_int32_type	},
121 	{ "tv_usec",		&ng_parse_int32_type	},
122 	{ NULL }
123 };
124 const struct ng_parse_type ng_source_timeval_type = {
125 	&ng_parse_struct_type,
126 	&ng_source_timeval_type_fields
127 };
128 
129 /* Parse type for struct ng_source_stats */
130 static const struct ng_parse_struct_field ng_source_stats_type_fields[]
131 	= NG_SOURCE_STATS_TYPE_INFO;
132 static const struct ng_parse_type ng_source_stats_type = {
133 	&ng_parse_struct_type,
134 	&ng_source_stats_type_fields
135 };
136 
137 /* List of commands and how to convert arguments to/from ASCII */
138 static const struct ng_cmdlist ng_source_cmds[] = {
139 	{
140 	  NGM_SOURCE_COOKIE,
141 	  NGM_SOURCE_GET_STATS,
142 	  "getstats",
143 	  NULL,
144 	  &ng_source_stats_type
145 	},
146 	{
147 	  NGM_SOURCE_COOKIE,
148 	  NGM_SOURCE_CLR_STATS,
149 	  "clrstats",
150 	  NULL,
151 	  NULL
152 	},
153 	{
154 	  NGM_SOURCE_COOKIE,
155 	  NGM_SOURCE_GETCLR_STATS,
156 	  "getclrstats",
157 	  NULL,
158 	  &ng_source_stats_type
159 	},
160 	{
161 	  NGM_SOURCE_COOKIE,
162 	  NGM_SOURCE_START,
163 	  "start",
164 	  &ng_parse_uint64_type,
165 	  NULL
166 	},
167 	{
168 	  NGM_SOURCE_COOKIE,
169 	  NGM_SOURCE_STOP,
170 	  "stop",
171 	  NULL,
172 	  NULL
173 	},
174 	{
175 	  NGM_SOURCE_COOKIE,
176 	  NGM_SOURCE_CLR_DATA,
177 	  "clrdata",
178 	  NULL,
179 	  NULL
180 	},
181 	{
182 	  NGM_SOURCE_COOKIE,
183 	  NGM_SOURCE_START_NOW,
184 	  "start_now",
185 	  &ng_parse_uint64_type,
186 	  NULL
187 	},
188 	{ 0 }
189 };
190 
191 /* Netgraph type descriptor */
192 static struct ng_type ng_source_typestruct = {
193 	NG_ABI_VERSION,
194 	NG_SOURCE_NODE_TYPE,
195 	NULL,					/* module event handler */
196 	ng_source_constructor,
197 	ng_source_rcvmsg,
198 	ng_source_rmnode,
199 	ng_source_newhook,
200 	NULL,					/* findhook */
201 	NULL,					/* connect */
202 	ng_source_rcvdata,			/* rcvdata */
203 	ng_source_disconnect,
204 	ng_source_cmds
205 };
206 NETGRAPH_INIT(source, &ng_source_typestruct);
207 
208 static int ng_source_set_autosrc(sc_p, u_int32_t);
209 
210 /*
211  * Node constructor
212  */
213 static int
214 ng_source_constructor(node_p node)
215 {
216 	sc_p sc;
217 
218 	sc = malloc(sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO);
219 	if (sc == NULL)
220 		return (ENOMEM);
221 
222 	NG_NODE_SET_PRIVATE(node, sc);
223 	sc->node = node;
224 	sc->snd_queue.ifq_maxlen = 2048;	/* XXX not checked */
225 	callout_handle_init(&sc->intr_ch);   /* XXX fix.. will
226 						cause problems. */
227 	return (0);
228 }
229 
230 /*
231  * Add a hook
232  */
233 static int
234 ng_source_newhook(node_p node, hook_p hook, const char *name)
235 {
236 	sc_p sc;
237 
238 	sc = NG_NODE_PRIVATE(node);
239 	KASSERT(sc != NULL, ("%s: null node private", __func__));
240 	if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) {
241 		sc->input.hook = hook;
242 		NG_HOOK_SET_PRIVATE(hook, &sc->input);
243 	} else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) {
244 		sc->output.hook = hook;
245 		NG_HOOK_SET_PRIVATE(hook, &sc->output);
246 		sc->output_ifp = 0;
247 		bzero(&sc->stats, sizeof(sc->stats));
248 	} else
249 		return (EINVAL);
250 	return (0);
251 }
252 
253 /*
254  * Receive a control message
255  */
256 static int
257 ng_source_rcvmsg(node_p node, item_p item, hook_p lasthook)
258 {
259 	sc_p sc;
260 	struct ng_mesg *resp = NULL;
261 	int error = 0;
262 	struct ng_mesg *msg;
263 
264 	sc = NG_NODE_PRIVATE(node);
265 	NGI_GET_MSG(item, msg);
266 	KASSERT(sc != NULL, ("%s: null node private", __func__));
267 	switch (msg->header.typecookie) {
268 	case NGM_SOURCE_COOKIE:
269 		if (msg->header.flags & NGF_RESP) {
270 			error = EINVAL;
271 			break;
272 		}
273 		switch (msg->header.cmd) {
274 		case NGM_SOURCE_GET_STATS:
275 		case NGM_SOURCE_CLR_STATS:
276 		case NGM_SOURCE_GETCLR_STATS:
277                     {
278 			struct ng_source_stats *stats;
279 
280                         if (msg->header.cmd != NGM_SOURCE_CLR_STATS) {
281                                 NG_MKRESPONSE(resp, msg,
282                                     sizeof(*stats), M_NOWAIT);
283 				if (resp == NULL) {
284 					error = ENOMEM;
285 					goto done;
286 				}
287 				sc->stats.queueOctets = sc->queueOctets;
288 				sc->stats.queueFrames = sc->snd_queue.ifq_len;
289 				if ((sc->node->nd_flags & NG_SOURCE_ACTIVE)
290 				    && !timevalisset(&sc->stats.endTime)) {
291 					getmicrotime(&sc->stats.elapsedTime);
292 					timevalsub(&sc->stats.elapsedTime,
293 					    &sc->stats.startTime);
294 				}
295 				stats = (struct ng_source_stats *)resp->data;
296 				bcopy(&sc->stats, stats, sizeof(* stats));
297                         }
298                         if (msg->header.cmd != NGM_SOURCE_GET_STATS)
299 				bzero(&sc->stats, sizeof(sc->stats));
300 		    }
301 		    break;
302 		case NGM_SOURCE_START:
303 		    {
304 			u_int64_t packets = *(u_int64_t *)msg->data;
305 			if (sc->output.hook == NULL) {
306 				printf("%s: start on node with no output hook\n"
307 				    , __func__);
308 				error = EINVAL;
309 				break;
310 			}
311 			/* TODO validation of packets */
312 			sc->packets = packets;
313 			ng_source_start(sc);
314 		    }
315 		    break;
316 		case NGM_SOURCE_START_NOW:
317 		    {
318 			u_int64_t packets = *(u_int64_t *)msg->data;
319 			if (sc->output.hook == NULL) {
320 				printf("%s: start on node with no output hook\n"
321 				    , __func__);
322 				error = EINVAL;
323 				break;
324 			}
325 			if (sc->node->nd_flags & NG_SOURCE_ACTIVE) {
326 				error = EBUSY;
327 				break;
328 			}
329 			/* TODO validation of packets */
330 			sc->packets = packets;
331 		        sc->output_ifp = NULL;
332 
333 			sc->node->nd_flags |= NG_SOURCE_ACTIVE;
334 			timevalclear(&sc->stats.elapsedTime);
335 			timevalclear(&sc->stats.endTime);
336 			getmicrotime(&sc->stats.startTime);
337 			sc->intr_ch = timeout(ng_source_intr, sc, 0);
338 		    }
339 		    break;
340 		case NGM_SOURCE_STOP:
341 			ng_source_stop(sc);
342 			break;
343 		case NGM_SOURCE_CLR_DATA:
344 			ng_source_clr_data(sc);
345 			break;
346 		default:
347 			error = EINVAL;
348 			break;
349 		}
350 		break;
351 	case NGM_ETHER_COOKIE:
352 		if (!(msg->header.flags & NGF_RESP)) {
353 			error = EINVAL;
354 			break;
355 		}
356 		switch (msg->header.cmd) {
357 		case NGM_ETHER_GET_IFINDEX:
358 			if (ng_source_store_output_ifp(sc, msg) == 0) {
359 				ng_source_set_autosrc(sc, 0);
360 				sc->node->nd_flags |= NG_SOURCE_ACTIVE;
361 				timevalclear(&sc->stats.elapsedTime);
362 				timevalclear(&sc->stats.endTime);
363 				getmicrotime(&sc->stats.startTime);
364 				sc->intr_ch = timeout(ng_source_intr, sc, 0);
365 			}
366 			break;
367 		default:
368 			error = EINVAL;
369 		}
370 		break;
371 	default:
372 		error = EINVAL;
373 		break;
374 	}
375 
376 done:
377 	/* Take care of synchronous response, if any */
378 	NG_RESPOND_MSG(error, node, item, resp);
379 	/* Free the message and return */
380 	NG_FREE_MSG(msg);
381 	return (error);
382 }
383 
384 /*
385  * Receive data on a hook
386  *
387  * If data comes in the input hook, enqueue it on the send queue.
388  * If data comes in the output hook, discard it.
389  */
390 static int
391 ng_source_rcvdata(hook_p hook, item_p item)
392 {
393 	sc_p sc;
394 	struct source_hookinfo *hinfo;
395 	int error = 0;
396 	struct mbuf *m;
397 
398 	sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
399 	NGI_GET_M(item, m);
400 	NG_FREE_ITEM(item);
401 	hinfo = NG_HOOK_PRIVATE(hook);
402 	KASSERT(sc != NULL, ("%s: null node private", __func__));
403 	KASSERT(hinfo != NULL, ("%s: null hook info", __func__));
404 
405 	/* Which hook? */
406 	if (hinfo == &sc->output) {
407 		/* discard */
408 		NG_FREE_M(m);
409 		return (error);
410 	}
411 	KASSERT(hinfo == &sc->input, ("%s: no hook!", __func__));
412 
413 	if ((m->m_flags & M_PKTHDR) == 0) {
414 		printf("%s: mbuf without PKTHDR\n", __func__);
415 		NG_FREE_M(m);
416 		return (EINVAL);
417 	}
418 
419 	/* enque packet */
420 	/* XXX should we check IF_QFULL() ? */
421 	_IF_ENQUEUE(&sc->snd_queue, m);
422 	sc->queueOctets += m->m_pkthdr.len;
423 
424 	return (0);
425 }
426 
427 /*
428  * Shutdown processing
429  */
430 static int
431 ng_source_rmnode(node_p node)
432 {
433 	sc_p sc;
434 
435 	sc = NG_NODE_PRIVATE(node);
436 	KASSERT(sc != NULL, ("%s: null node private", __func__));
437 	node->nd_flags |= NG_INVALID;
438 	ng_source_stop(sc);
439 	ng_source_clr_data(sc);
440 	NG_NODE_SET_PRIVATE(node, NULL);
441 	NG_NODE_UNREF(node);
442 	free(sc, M_NETGRAPH);
443 	return (0);
444 }
445 
446 /*
447  * Hook disconnection
448  */
449 static int
450 ng_source_disconnect(hook_p hook)
451 {
452 	struct source_hookinfo *hinfo;
453 	sc_p sc;
454 
455 	hinfo = NG_HOOK_PRIVATE(hook);
456 	sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
457 	KASSERT(sc != NULL, ("%s: null node private", __func__));
458 	hinfo->hook = NULL;
459 	if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hinfo == &sc->output)
460 		ng_rmnode_self(NG_HOOK_NODE(hook));
461 	return (0);
462 }
463 
464 /*
465  *
466  * Ask out neighbour on the output hook side to send us it's interface
467  * information.
468  */
469 static int
470 ng_source_request_output_ifp(sc_p sc)
471 {
472 	struct ng_mesg *msg;
473 	int error = 0;
474 
475 	sc->output_ifp = NULL;
476 
477 	/* Ask the attached node for the connected interface's index */
478 	NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFINDEX, 0, M_NOWAIT);
479 	if (msg == NULL)
480 		return (ENOBUFS);
481 
482 	NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, 0);
483 	return (error);
484 }
485 
486 /*
487  * Set sc->output_ifp to point to the the struct ifnet of the interface
488  * reached via our output hook.
489  */
490 static int
491 ng_source_store_output_ifp(sc_p sc, struct ng_mesg *msg)
492 {
493 	struct ifnet *ifp;
494 	u_int32_t if_index;
495 	int s;
496 
497 	if (msg->header.arglen < sizeof(u_int32_t))
498 		return (EINVAL);
499 
500 	if_index = *(u_int32_t *)msg->data;
501 	/* Could use ifindex2ifnet[if_index] except that we have no
502 	 * way of verifying if_index is valid since if_indexlim is
503 	 * local to if_attach()
504 	 */
505 	IFNET_RLOCK();
506 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
507 		if (ifp->if_index == if_index)
508 			break;
509 	}
510 	IFNET_RUNLOCK();
511 
512 	if (ifp == NULL) {
513 		printf("%s: can't find interface %d\n", __func__, if_index);
514 		return (EINVAL);
515 	}
516 	sc->output_ifp = ifp;
517 
518 #if 1
519 	/* XXX mucking with a drivers ifqueue size is ugly but we need it
520 	 * to queue a lot of packets to get close to line rate on a gigabit
521 	 * interface with small packets.
522 	 * XXX we should restore the original value at stop or disconnect
523 	 */
524 	s = splimp();		/* XXX is this required? */
525 	if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN) {
526 		printf("ng_source: changing ifq_maxlen from %d to %d\n",
527 		    ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN);
528 		ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN;
529 	}
530 	splx(s);
531 #endif
532 	return (0);
533 }
534 
535 /*
536  * Set the attached ethernet node's ethernet source address override flag.
537  */
538 static int
539 ng_source_set_autosrc(sc_p sc, u_int32_t flag)
540 {
541 	struct ng_mesg *msg;
542 	int error = 0;
543 
544 	NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC,
545 	    sizeof (u_int32_t), M_NOWAIT);
546 	if (msg == NULL)
547 		return(ENOBUFS);
548 
549 	*(u_int32_t *)msg->data = flag;
550 	NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output.hook, 0);
551 	return (error);
552 }
553 
554 /*
555  * Clear out the data we've queued
556  */
557 static void
558 ng_source_clr_data (sc_p sc)
559 {
560 	struct mbuf *m;
561 
562 	for (;;) {
563 		_IF_DEQUEUE(&sc->snd_queue, m);
564 		if (m == NULL)
565 			break;
566 		NG_FREE_M(m);
567 	}
568 	sc->queueOctets = 0;
569 }
570 
571 /*
572  * Start sending queued data out the output hook
573  */
574 static void
575 ng_source_start (sc_p sc)
576 {
577 	KASSERT(sc->output.hook != NULL,
578 			("%s: output hook unconnected", __func__));
579 	if (((sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) &&
580 	    (sc->output_ifp == NULL))
581 		ng_source_request_output_ifp(sc);
582 }
583 
584 /*
585  * Stop sending queued data out the output hook
586  */
587 static void
588 ng_source_stop (sc_p sc)
589 {
590 	if (sc->node->nd_flags & NG_SOURCE_ACTIVE) {
591 		untimeout(ng_source_intr, sc, sc->intr_ch);
592 		sc->node->nd_flags &= ~NG_SOURCE_ACTIVE;
593 		getmicrotime(&sc->stats.endTime);
594 		sc->stats.elapsedTime = sc->stats.endTime;
595 		timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime);
596 		/* XXX should set this to the initial value instead */
597 		ng_source_set_autosrc(sc, 1);
598 	}
599 }
600 
601 /*
602  * While active called every NG_SOURCE_INTR_TICKS ticks.
603  * Sends as many packets as the interface connected to our
604  * output hook is able to enqueue.
605  */
606 static void
607 ng_source_intr (void *arg)
608 {
609 	sc_p sc = (sc_p) arg;
610 	struct ifqueue *ifq;
611 	int packets;
612 
613 	KASSERT(sc != NULL, ("%s: null node private", __func__));
614 
615 	callout_handle_init(&sc->intr_ch);
616 	if (sc->packets == 0 || sc->output.hook == NULL
617 	    || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) {
618 		ng_source_stop(sc);
619 		return;
620 	}
621 
622 	if (sc->output_ifp != NULL) {
623 		ifq = &sc->output_ifp->if_snd;
624 		packets = ifq->ifq_maxlen - ifq->ifq_len;
625 	} else
626 		packets = sc->snd_queue.ifq_len;
627 
628 	ng_source_send(sc, packets, NULL);
629 	if (sc->packets == 0) {
630 		int s = splnet();
631 		ng_source_stop(sc);
632 		splx(s);
633 	} else
634 		sc->intr_ch = timeout(ng_source_intr, sc, NG_SOURCE_INTR_TICKS);
635 }
636 
637 /*
638  * Send packets out our output hook
639  */
640 static int
641 ng_source_send (sc_p sc, int tosend, int *sent_p)
642 {
643 	struct ifqueue tmp_queue;
644 	struct mbuf *m, *m2;
645 	int sent = 0;
646 	int error = 0;
647 	int s, s2;
648 
649 	KASSERT(sc != NULL, ("%s: null node private", __func__));
650 	KASSERT(tosend >= 0, ("%s: negative tosend param", __func__));
651 	KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE,
652 	    ("%s: inactive node", __func__));
653 
654 	if ((u_int64_t)tosend > sc->packets)
655 		tosend = sc->packets;
656 
657 	/* Copy the required number of packets to a temporary queue */
658 	bzero (&tmp_queue, sizeof (tmp_queue));
659 	for (sent = 0; error == 0 && sent < tosend; ++sent) {
660 		s = splnet();
661 		_IF_DEQUEUE(&sc->snd_queue, m);
662 		splx(s);
663 		if (m == NULL)
664 			break;
665 
666 		/* duplicate the packet */
667 		m2 = m_copypacket(m, M_DONTWAIT);
668 		if (m2 == NULL) {
669 			s = splnet();
670 			_IF_PREPEND(&sc->snd_queue, m);
671 			splx(s);
672 			error = ENOBUFS;
673 			break;
674 		}
675 
676 		/* re-enqueue the original packet for us */
677 		s = splnet();
678 		_IF_ENQUEUE(&sc->snd_queue, m);
679 		splx(s);
680 
681 		/* queue the copy for sending at smplimp */
682 		_IF_ENQUEUE(&tmp_queue, m2);
683 	}
684 
685 	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