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