6 * Copyright (c) 2005 Gleb Smirnoff <glebius@FreeBSD.org>
7 * Copyright 2002 Sandvine Inc.
10 * Subject to the following obligations and disclaimer of warranty, use and
11 * redistribution of this software, in source or object code forms, with or
12 * without modifications are expressly permitted by Sandvine Inc.; provided,
14 * 1. Any and all reproductions of the source or object code must include the
15 * copyright notice above and the following disclaimer of warranties; and
16 * 2. No rights are granted, in any manner or form, to use Sandvine Inc.
17 * trademarks, including the mark "SANDVINE" on advertising, endorsements,
18 * or otherwise except as such appears in the above copyright notice or in
21 * THIS SOFTWARE IS BEING PROVIDED BY SANDVINE "AS IS", AND TO THE MAXIMUM
22 * EXTENT PERMITTED BY LAW, SANDVINE MAKES NO REPRESENTATIONS OR WARRANTIES,
23 * EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, INCLUDING WITHOUT LIMITATION,
24 * ANY AND ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 * PURPOSE, OR NON-INFRINGEMENT. SANDVINE DOES NOT WARRANT, GUARANTEE, OR
26 * MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE
27 * USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY
28 * OR OTHERWISE. IN NO EVENT SHALL SANDVINE BE LIABLE FOR ANY DAMAGES
29 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF SANDVINE IS ADVISED OF THE POSSIBILITY OF SUCH
38 * Author: Dave Chapeskie
41 #include <sys/cdefs.h>
43 * This node is used for high speed packet geneneration. It queues
44 * all data received on its 'input' hook and when told to start via
45 * a control message it sends the packets out its 'output' hook. In
46 * this way this node can be preloaded with a packet stream which it
47 * can then send continuously as fast as possible.
49 * Currently it just copies the mbufs as required. It could do various
50 * tricks to try and avoid this. Probably the best performance would
51 * be achieved by modifying the appropriate drivers to be told to
52 * self-re-enqueue packets (e.g. the if_bge driver could reuse the same
53 * transmit descriptors) under control of this node; perhaps via some
54 * flag in the mbuf or some such. The node could peek at an appropriate
55 * ifnet flag to see if such support is available for the connected
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/errno.h>
62 #include <sys/kernel.h>
63 #include <sys/malloc.h>
65 #include <sys/socket.h>
66 #include <sys/syslog.h>
68 #include <net/if_var.h>
69 #include <net/if_private.h>
70 #include <netgraph/ng_message.h>
71 #include <netgraph/netgraph.h>
72 #include <netgraph/ng_parse.h>
73 #include <netgraph/ng_ether.h>
74 #include <netgraph/ng_source.h>
76 #define NG_SOURCE_INTR_TICKS 1
77 #define NG_SOURCE_DRIVER_IFQ_MAXLEN (4*1024)
79 #define mtod_off(m,off,t) ((t)(mtod((m),caddr_t)+(off)))
86 struct ng_source_stats stats;
87 struct mbufq snd_queue; /* packets to send */
88 struct mbuf *last_packet; /* last pkt in queue */
89 struct ifnet *output_ifp;
90 struct callout intr_ch;
91 uint64_t packets; /* packets to send */
93 struct ng_source_embed_info embed_timestamp;
94 struct ng_source_embed_cnt_info embed_counter[NG_SOURCE_COUNTERS];
96 typedef struct privdata *sc_p;
99 #define NG_SOURCE_ACTIVE (NGF_TYPE1)
101 /* Netgraph methods */
102 static ng_constructor_t ng_source_constructor;
103 static ng_rcvmsg_t ng_source_rcvmsg;
104 static ng_shutdown_t ng_source_rmnode;
105 static ng_newhook_t ng_source_newhook;
106 static ng_connect_t ng_source_connect;
107 static ng_rcvdata_t ng_source_rcvdata;
108 static ng_disconnect_t ng_source_disconnect;
110 /* Other functions */
111 static void ng_source_intr(node_p, hook_p, void *, int);
112 static void ng_source_clr_data (sc_p);
113 static int ng_source_start (sc_p, uint64_t);
114 static void ng_source_stop (sc_p);
115 static int ng_source_send (sc_p, int, int *);
116 static int ng_source_store_output_ifp(sc_p, char *);
117 static void ng_source_packet_mod(sc_p, struct mbuf *,
118 int, int, caddr_t, int);
119 static void ng_source_mod_counter(sc_p sc,
120 struct ng_source_embed_cnt_info *cnt,
121 struct mbuf *m, int increment);
122 static int ng_source_dup_mod(sc_p, struct mbuf *,
125 /* Parse type for timeval */
126 static const struct ng_parse_struct_field ng_source_timeval_type_fields[] = {
128 { "tv_sec", &ng_parse_int32_type },
130 { "tv_sec", &ng_parse_int64_type },
133 { "tv_usec", &ng_parse_int64_type },
135 { "tv_usec", &ng_parse_int32_type },
139 const struct ng_parse_type ng_source_timeval_type = {
140 &ng_parse_struct_type,
141 &ng_source_timeval_type_fields
144 /* Parse type for struct ng_source_stats */
145 static const struct ng_parse_struct_field ng_source_stats_type_fields[]
146 = NG_SOURCE_STATS_TYPE_INFO;
147 static const struct ng_parse_type ng_source_stats_type = {
148 &ng_parse_struct_type,
149 &ng_source_stats_type_fields
152 /* Parse type for struct ng_source_embed_info */
153 static const struct ng_parse_struct_field ng_source_embed_type_fields[] =
154 NG_SOURCE_EMBED_TYPE_INFO;
155 static const struct ng_parse_type ng_source_embed_type = {
156 &ng_parse_struct_type,
157 &ng_source_embed_type_fields
160 /* Parse type for struct ng_source_embed_cnt_info */
161 static const struct ng_parse_struct_field ng_source_embed_cnt_type_fields[] =
162 NG_SOURCE_EMBED_CNT_TYPE_INFO;
163 static const struct ng_parse_type ng_source_embed_cnt_type = {
164 &ng_parse_struct_type,
165 &ng_source_embed_cnt_type_fields
168 /* List of commands and how to convert arguments to/from ASCII */
169 static const struct ng_cmdlist ng_source_cmds[] = {
172 NGM_SOURCE_GET_STATS,
175 &ng_source_stats_type
179 NGM_SOURCE_CLR_STATS,
186 NGM_SOURCE_GETCLR_STATS,
189 &ng_source_stats_type
195 &ng_parse_uint64_type,
216 &ng_parse_string_type,
223 &ng_parse_uint32_type,
228 NGM_SOURCE_SET_TIMESTAMP,
230 &ng_source_embed_type,
235 NGM_SOURCE_GET_TIMESTAMP,
238 &ng_source_embed_type
242 NGM_SOURCE_SET_COUNTER,
244 &ng_source_embed_cnt_type,
249 NGM_SOURCE_GET_COUNTER,
251 &ng_parse_uint8_type,
252 &ng_source_embed_cnt_type
257 /* Netgraph type descriptor */
258 static struct ng_type ng_source_typestruct = {
259 .version = NG_ABI_VERSION,
260 .name = NG_SOURCE_NODE_TYPE,
261 .constructor = ng_source_constructor,
262 .rcvmsg = ng_source_rcvmsg,
263 .shutdown = ng_source_rmnode,
264 .newhook = ng_source_newhook,
265 .connect = ng_source_connect,
266 .rcvdata = ng_source_rcvdata,
267 .disconnect = ng_source_disconnect,
268 .cmdlist = ng_source_cmds,
270 NETGRAPH_INIT(source, &ng_source_typestruct);
272 static int ng_source_set_autosrc(sc_p, uint32_t);
278 ng_source_constructor(node_p node)
282 sc = malloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
284 NG_NODE_SET_PRIVATE(node, sc);
286 mbufq_init(&sc->snd_queue, 2048);
287 ng_callout_init(&sc->intr_ch);
296 ng_source_newhook(node_p node, hook_p hook, const char *name)
298 sc_p sc = NG_NODE_PRIVATE(node);
300 if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) {
302 } else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) {
304 sc->output_ifp = NULL;
305 bzero(&sc->stats, sizeof(sc->stats));
313 * Hook has been added
316 ng_source_connect(hook_p hook)
318 sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
323 * If this is "output" hook, then request information
324 * from our downstream.
326 if (hook == sc->output) {
327 NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFNAME,
333 * Our hook and peer hook have HK_INVALID flag set,
334 * so we can't use NG_SEND_MSG_HOOK() macro here.
336 NG_SEND_MSG_ID(dummy_error, sc->node, msg,
337 NG_NODE_ID(NG_PEER_NODE(sc->output)), NG_NODE_ID(sc->node));
344 * Receive a control message
347 ng_source_rcvmsg(node_p node, item_p item, hook_p lasthook)
349 sc_p sc = NG_NODE_PRIVATE(node);
350 struct ng_mesg *msg, *resp = NULL;
353 NGI_GET_MSG(item, msg);
355 switch (msg->header.typecookie) {
356 case NGM_SOURCE_COOKIE:
357 if (msg->header.flags & NGF_RESP) {
361 switch (msg->header.cmd) {
362 case NGM_SOURCE_GET_STATS:
363 case NGM_SOURCE_CLR_STATS:
364 case NGM_SOURCE_GETCLR_STATS:
366 struct ng_source_stats *stats;
368 if (msg->header.cmd != NGM_SOURCE_CLR_STATS) {
369 NG_MKRESPONSE(resp, msg,
370 sizeof(*stats), M_NOWAIT);
375 sc->stats.queueOctets = sc->queueOctets;
376 sc->stats.queueFrames = mbufq_len(&sc->snd_queue);
377 if ((sc->node->nd_flags & NG_SOURCE_ACTIVE)
378 && !timevalisset(&sc->stats.endTime)) {
379 getmicrotime(&sc->stats.elapsedTime);
380 timevalsub(&sc->stats.elapsedTime,
381 &sc->stats.startTime);
383 stats = (struct ng_source_stats *)resp->data;
384 bcopy(&sc->stats, stats, sizeof(* stats));
386 if (msg->header.cmd != NGM_SOURCE_GET_STATS)
387 bzero(&sc->stats, sizeof(sc->stats));
390 case NGM_SOURCE_START:
394 if (msg->header.arglen != sizeof(uint64_t)) {
399 packets = *(uint64_t *)msg->data;
401 error = ng_source_start(sc, packets);
405 case NGM_SOURCE_STOP:
408 case NGM_SOURCE_CLR_DATA:
409 ng_source_clr_data(sc);
411 case NGM_SOURCE_SETIFACE:
413 char *ifname = (char *)msg->data;
415 if (msg->header.arglen < 2) {
420 ng_source_store_output_ifp(sc, ifname);
423 case NGM_SOURCE_SETPPS:
427 if (msg->header.arglen != sizeof(uint32_t)) {
432 pps = *(uint32_t *)msg->data;
434 sc->stats.maxPps = pps;
438 case NGM_SOURCE_SET_TIMESTAMP:
440 struct ng_source_embed_info *embed;
442 if (msg->header.arglen != sizeof(*embed)) {
446 embed = (struct ng_source_embed_info *)msg->data;
447 bcopy(embed, &sc->embed_timestamp, sizeof(*embed));
451 case NGM_SOURCE_GET_TIMESTAMP:
453 struct ng_source_embed_info *embed;
455 NG_MKRESPONSE(resp, msg, sizeof(*embed), M_NOWAIT);
460 embed = (struct ng_source_embed_info *)resp->data;
461 bcopy(&sc->embed_timestamp, embed, sizeof(*embed));
465 case NGM_SOURCE_SET_COUNTER:
467 struct ng_source_embed_cnt_info *embed;
469 if (msg->header.arglen != sizeof(*embed)) {
473 embed = (struct ng_source_embed_cnt_info *)msg->data;
474 if (embed->index >= NG_SOURCE_COUNTERS ||
475 !(embed->width == 1 || embed->width == 2 ||
476 embed->width == 4)) {
480 bcopy(embed, &sc->embed_counter[embed->index],
485 case NGM_SOURCE_GET_COUNTER:
487 uint8_t index = *(uint8_t *)msg->data;
488 struct ng_source_embed_cnt_info *embed;
490 if (index >= NG_SOURCE_COUNTERS) {
494 NG_MKRESPONSE(resp, msg, sizeof(*embed), M_NOWAIT);
499 embed = (struct ng_source_embed_cnt_info *)resp->data;
500 bcopy(&sc->embed_counter[index], embed, sizeof(*embed));
509 case NGM_ETHER_COOKIE:
510 if (!(msg->header.flags & NGF_RESP)) {
514 switch (msg->header.cmd) {
515 case NGM_ETHER_GET_IFNAME:
517 char *ifname = (char *)msg->data;
519 if (msg->header.arglen < 2) {
524 if (ng_source_store_output_ifp(sc, ifname) == 0)
525 ng_source_set_autosrc(sc, 0);
538 /* Take care of synchronous response, if any. */
539 NG_RESPOND_MSG(error, node, item, resp);
540 /* Free the message and return. */
546 * Receive data on a hook
548 * If data comes in the input hook, enqueue it on the send queue.
549 * If data comes in the output hook, discard it.
552 ng_source_rcvdata(hook_p hook, item_p item)
554 sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
562 if (hook == sc->output) {
567 KASSERT(hook == sc->input, ("%s: no hook!", __func__));
569 /* Enqueue packet if the queue isn't full. */
570 error = mbufq_enqueue(&sc->snd_queue, m);
575 sc->queueOctets += m->m_pkthdr.len;
582 * Shutdown processing
585 ng_source_rmnode(node_p node)
587 sc_p sc = NG_NODE_PRIVATE(node);
590 ng_source_clr_data(sc);
591 NG_NODE_SET_PRIVATE(node, NULL);
593 free(sc, M_NETGRAPH);
602 ng_source_disconnect(hook_p hook)
606 sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
607 KASSERT(sc != NULL, ("%s: null node private", __func__));
608 if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hook == sc->output)
609 ng_rmnode_self(NG_HOOK_NODE(hook));
614 * Set sc->output_ifp to point to the struct ifnet of the interface
615 * reached via our output hook.
618 ng_source_store_output_ifp(sc_p sc, char *ifname)
622 ifp = ifunit(ifname);
625 printf("%s: can't find interface %s\n", __func__, ifname);
628 sc->output_ifp = ifp;
631 /* XXX mucking with a drivers ifqueue size is ugly but we need it
632 * to queue a lot of packets to get close to line rate on a gigabit
633 * interface with small packets.
634 * XXX we should restore the original value at stop or disconnect
636 if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN) {
637 printf("ng_source: changing ifq_maxlen from %d to %d\n",
638 ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN);
639 ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN;
646 * Set the attached ethernet node's ethernet source address override flag.
649 ng_source_set_autosrc(sc_p sc, uint32_t flag)
654 NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC,
655 sizeof (uint32_t), M_NOWAIT);
659 *(uint32_t *)msg->data = flag;
660 NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output, 0);
665 * Clear out the data we've queued
668 ng_source_clr_data (sc_p sc)
673 m = mbufq_dequeue(&sc->snd_queue);
679 sc->last_packet = NULL;
683 * Start sending queued data out the output hook
686 ng_source_start(sc_p sc, uint64_t packets)
688 if (sc->output_ifp == NULL && sc->stats.maxPps == 0) {
689 printf("ng_source: start without iface or pps configured\n");
693 if (sc->node->nd_flags & NG_SOURCE_ACTIVE)
696 sc->node->nd_flags |= NG_SOURCE_ACTIVE;
698 sc->packets = packets;
699 timevalclear(&sc->stats.elapsedTime);
700 timevalclear(&sc->stats.endTime);
701 getmicrotime(&sc->stats.startTime);
702 getmicrotime(&sc->stats.lastTime);
703 ng_callout(&sc->intr_ch, sc->node, NULL, 0,
704 ng_source_intr, sc, 0);
710 * Stop sending queued data out the output hook
713 ng_source_stop(sc_p sc)
715 ng_uncallout(&sc->intr_ch, sc->node);
716 sc->node->nd_flags &= ~NG_SOURCE_ACTIVE;
717 getmicrotime(&sc->stats.endTime);
718 sc->stats.elapsedTime = sc->stats.endTime;
719 timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime);
723 * While active called every NG_SOURCE_INTR_TICKS ticks.
724 * Sends as many packets as the interface connected to our
725 * output hook is able to enqueue.
728 ng_source_intr(node_p node, hook_p hook, void *arg1, int arg2)
730 sc_p sc = (sc_p)arg1;
734 KASSERT(sc != NULL, ("%s: null node private", __func__));
736 if (sc->packets == 0 || sc->output == NULL
737 || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) {
742 if (sc->output_ifp != NULL) {
743 ifq = (struct ifqueue *)&sc->output_ifp->if_snd;
744 packets = ifq->ifq_maxlen - ifq->ifq_len;
746 packets = mbufq_len(&sc->snd_queue);
748 if (sc->stats.maxPps != 0) {
749 struct timeval now, elapsed;
755 timevalsub(&elapsed, &sc->stats.lastTime);
756 usec = elapsed.tv_sec * 1000000 + elapsed.tv_usec;
757 maxpkt = (uint64_t)sc->stats.maxPps * usec / 1000000;
758 sc->stats.lastTime = now;
759 if (packets > maxpkt)
763 ng_source_send(sc, packets, NULL);
764 if (sc->packets == 0)
767 ng_callout(&sc->intr_ch, node, NULL, NG_SOURCE_INTR_TICKS,
768 ng_source_intr, sc, 0);
772 * Send packets out our output hook.
775 ng_source_send(sc_p sc, int tosend, int *sent_p)
781 KASSERT(tosend >= 0, ("%s: negative tosend param", __func__));
782 KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE,
783 ("%s: inactive node", __func__));
785 if ((uint64_t)tosend > sc->packets)
786 tosend = sc->packets;
788 /* Go through the queue sending packets one by one. */
789 for (sent = 0; error == 0 && sent < tosend; ++sent) {
790 m = mbufq_dequeue(&sc->snd_queue);
794 /* Duplicate and modify the packet. */
795 error = ng_source_dup_mod(sc, m, &m2);
797 if (error == ENOBUFS)
798 mbufq_prepend(&sc->snd_queue, m);
800 (void)mbufq_enqueue(&sc->snd_queue, m);
805 * Re-enqueue the original packet for us. The queue
806 * has a free slot, because we dequeued the packet
807 * above and this callout function runs under WRITER
810 error = mbufq_enqueue(&sc->snd_queue, m);
811 KASSERT(error == 0, ("%s: re-enqueue packet failed", __func__));
813 sc->stats.outFrames++;
814 sc->stats.outOctets += m2->m_pkthdr.len;
815 NG_SEND_DATA_ONLY(error, sc->output, m2);
827 * Modify packet in 'm' by changing 'len' bytes starting at 'offset'
830 * The packet data in 'm' must be in a contiguous buffer in a single mbuf.
833 ng_source_packet_mod(sc_p sc, struct mbuf *m, int offset, int len, caddr_t cp,
839 /* Can't modify beyond end of packet. */
840 /* TODO: Pad packet for this case. */
841 if (offset + len > m->m_len)
844 bcopy(cp, mtod_off(m, offset, caddr_t), len);
848 ng_source_mod_counter(sc_p sc, struct ng_source_embed_cnt_info *cnt,
849 struct mbuf *m, int increment)
854 val = htonl(cnt->next_val);
855 cp = (caddr_t)&val + sizeof(val) - cnt->width;
856 ng_source_packet_mod(sc, m, cnt->offset, cnt->width, cp, cnt->flags);
859 cnt->next_val += increment;
861 if (increment > 0 && cnt->next_val > cnt->max_val) {
862 cnt->next_val = cnt->min_val - 1 +
863 (cnt->next_val - cnt->max_val);
864 if (cnt->next_val > cnt->max_val)
865 cnt->next_val = cnt->max_val;
866 } else if (increment < 0 && cnt->next_val < cnt->min_val) {
867 cnt->next_val = cnt->max_val + 1 +
868 (cnt->next_val - cnt->min_val);
869 if (cnt->next_val < cnt->min_val)
870 cnt->next_val = cnt->max_val;
876 ng_source_dup_mod(sc_p sc, struct mbuf *m0, struct mbuf **m_ptr)
879 struct ng_source_embed_cnt_info *cnt;
880 struct ng_source_embed_info *ts;
885 /* Are we going to modify packets? */
886 modify = sc->embed_timestamp.flags & NGM_SOURCE_EMBED_ENABLE;
887 for (i = 0; !modify && i < NG_SOURCE_COUNTERS; ++i)
888 modify = sc->embed_counter[i].flags & NGM_SOURCE_EMBED_ENABLE;
890 /* Duplicate the packet. */
892 m = m_dup(m0, M_NOWAIT);
894 m = m_copypacket(m0, M_NOWAIT);
904 /* Modify the copied packet for sending. */
905 KASSERT(M_WRITABLE(m), ("%s: packet not writable", __func__));
907 for (i = 0; i < NG_SOURCE_COUNTERS; ++i) {
908 cnt = &sc->embed_counter[i];
909 if (cnt->flags & NGM_SOURCE_EMBED_ENABLE) {
910 if ((cnt->flags & NGM_SOURCE_INC_CNT_PER_LIST) == 0 ||
911 sc->last_packet == m0)
912 increment = cnt->increment;
915 ng_source_mod_counter(sc, cnt, m, increment);
919 ts = &sc->embed_timestamp;
920 if (ts->flags & NGM_SOURCE_EMBED_ENABLE) {
923 now.tv_sec = htonl(now.tv_sec);
924 now.tv_usec = htonl(now.tv_usec);
925 ng_source_packet_mod(sc, m, ts->offset, sizeof (now),
926 (caddr_t)&now, ts->flags);