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>
42 __FBSDID("$FreeBSD$");
45 * This node is used for high speed packet geneneration. It queues
46 * all data recieved on its 'input' hook and when told to start via
47 * a control message it sends the packets out its 'output' hook. In
48 * this way this node can be preloaded with a packet stream which it
49 * can then send continuously as fast as possible.
51 * Currently it just copies the mbufs as required. It could do various
52 * tricks to try and avoid this. Probably the best performance would
53 * be achieved by modifying the appropriate drivers to be told to
54 * self-re-enqueue packets (e.g. the if_bge driver could reuse the same
55 * transmit descriptors) under control of this node; perhaps via some
56 * flag in the mbuf or some such. The node could peek at an appropriate
57 * ifnet flag to see if such support is available for the connected
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/errno.h>
64 #include <sys/kernel.h>
65 #include <sys/malloc.h>
67 #include <sys/socket.h>
68 #include <sys/syslog.h>
70 #include <net/if_var.h>
71 #include <netgraph/ng_message.h>
72 #include <netgraph/netgraph.h>
73 #include <netgraph/ng_parse.h>
74 #include <netgraph/ng_ether.h>
75 #include <netgraph/ng_source.h>
77 #define NG_SOURCE_INTR_TICKS 1
78 #define NG_SOURCE_DRIVER_IFQ_MAXLEN (4*1024)
80 #define mtod_off(m,off,t) ((t)(mtod((m),caddr_t)+(off)))
87 struct ng_source_stats stats;
88 struct ifqueue snd_queue; /* packets to send */
89 struct mbuf *last_packet; /* last pkt in queue */
90 struct ifnet *output_ifp;
91 struct callout intr_ch;
92 uint64_t packets; /* packets to send */
94 struct ng_source_embed_info embed_timestamp;
95 struct ng_source_embed_cnt_info embed_counter[NG_SOURCE_COUNTERS];
97 typedef struct privdata *sc_p;
100 #define NG_SOURCE_ACTIVE (NGF_TYPE1)
102 /* Netgraph methods */
103 static ng_constructor_t ng_source_constructor;
104 static ng_rcvmsg_t ng_source_rcvmsg;
105 static ng_shutdown_t ng_source_rmnode;
106 static ng_newhook_t ng_source_newhook;
107 static ng_connect_t ng_source_connect;
108 static ng_rcvdata_t ng_source_rcvdata;
109 static ng_disconnect_t ng_source_disconnect;
111 /* Other functions */
112 static void ng_source_intr(node_p, hook_p, void *, int);
113 static void ng_source_clr_data (sc_p);
114 static int ng_source_start (sc_p, uint64_t);
115 static void ng_source_stop (sc_p);
116 static int ng_source_send (sc_p, int, int *);
117 static int ng_source_store_output_ifp(sc_p, char *);
118 static void ng_source_packet_mod(sc_p, struct mbuf *,
119 int, int, caddr_t, int);
120 static void ng_source_mod_counter(sc_p sc,
121 struct ng_source_embed_cnt_info *cnt,
122 struct mbuf *m, int increment);
123 static int ng_source_dup_mod(sc_p, struct mbuf *,
126 /* Parse type for timeval */
127 static const struct ng_parse_struct_field ng_source_timeval_type_fields[] = {
128 { "tv_sec", &ng_parse_int32_type },
129 { "tv_usec", &ng_parse_int32_type },
132 const struct ng_parse_type ng_source_timeval_type = {
133 &ng_parse_struct_type,
134 &ng_source_timeval_type_fields
137 /* Parse type for struct ng_source_stats */
138 static const struct ng_parse_struct_field ng_source_stats_type_fields[]
139 = NG_SOURCE_STATS_TYPE_INFO;
140 static const struct ng_parse_type ng_source_stats_type = {
141 &ng_parse_struct_type,
142 &ng_source_stats_type_fields
145 /* Parse type for struct ng_source_embed_info */
146 static const struct ng_parse_struct_field ng_source_embed_type_fields[] =
147 NG_SOURCE_EMBED_TYPE_INFO;
148 static const struct ng_parse_type ng_source_embed_type = {
149 &ng_parse_struct_type,
150 &ng_source_embed_type_fields
153 /* Parse type for struct ng_source_embed_cnt_info */
154 static const struct ng_parse_struct_field ng_source_embed_cnt_type_fields[] =
155 NG_SOURCE_EMBED_CNT_TYPE_INFO;
156 static const struct ng_parse_type ng_source_embed_cnt_type = {
157 &ng_parse_struct_type,
158 &ng_source_embed_cnt_type_fields
161 /* List of commands and how to convert arguments to/from ASCII */
162 static const struct ng_cmdlist ng_source_cmds[] = {
165 NGM_SOURCE_GET_STATS,
168 &ng_source_stats_type
172 NGM_SOURCE_CLR_STATS,
179 NGM_SOURCE_GETCLR_STATS,
182 &ng_source_stats_type
188 &ng_parse_uint64_type,
209 &ng_parse_string_type,
216 &ng_parse_uint32_type,
221 NGM_SOURCE_SET_TIMESTAMP,
223 &ng_source_embed_type,
228 NGM_SOURCE_GET_TIMESTAMP,
231 &ng_source_embed_type
235 NGM_SOURCE_SET_COUNTER,
237 &ng_source_embed_cnt_type,
242 NGM_SOURCE_GET_COUNTER,
244 &ng_parse_uint8_type,
245 &ng_source_embed_cnt_type
250 /* Netgraph type descriptor */
251 static struct ng_type ng_source_typestruct = {
252 .version = NG_ABI_VERSION,
253 .name = NG_SOURCE_NODE_TYPE,
254 .constructor = ng_source_constructor,
255 .rcvmsg = ng_source_rcvmsg,
256 .shutdown = ng_source_rmnode,
257 .newhook = ng_source_newhook,
258 .connect = ng_source_connect,
259 .rcvdata = ng_source_rcvdata,
260 .disconnect = ng_source_disconnect,
261 .cmdlist = ng_source_cmds,
263 NETGRAPH_INIT(source, &ng_source_typestruct);
265 static int ng_source_set_autosrc(sc_p, uint32_t);
271 ng_source_constructor(node_p node)
275 sc = malloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
277 NG_NODE_SET_PRIVATE(node, sc);
279 sc->snd_queue.ifq_maxlen = 2048; /* XXX not checked */
280 ng_callout_init(&sc->intr_ch);
289 ng_source_newhook(node_p node, hook_p hook, const char *name)
291 sc_p sc = NG_NODE_PRIVATE(node);
293 if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) {
295 } else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) {
298 bzero(&sc->stats, sizeof(sc->stats));
306 * Hook has been added
309 ng_source_connect(hook_p hook)
311 sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
316 * If this is "output" hook, then request information
317 * from our downstream.
319 if (hook == sc->output) {
320 NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFNAME,
326 * Our hook and peer hook have HK_INVALID flag set,
327 * so we can't use NG_SEND_MSG_HOOK() macro here.
329 NG_SEND_MSG_ID(dummy_error, sc->node, msg,
330 NG_NODE_ID(NG_PEER_NODE(sc->output)), NG_NODE_ID(sc->node));
337 * Receive a control message
340 ng_source_rcvmsg(node_p node, item_p item, hook_p lasthook)
342 sc_p sc = NG_NODE_PRIVATE(node);
343 struct ng_mesg *msg, *resp = NULL;
346 NGI_GET_MSG(item, msg);
348 switch (msg->header.typecookie) {
349 case NGM_SOURCE_COOKIE:
350 if (msg->header.flags & NGF_RESP) {
354 switch (msg->header.cmd) {
355 case NGM_SOURCE_GET_STATS:
356 case NGM_SOURCE_CLR_STATS:
357 case NGM_SOURCE_GETCLR_STATS:
359 struct ng_source_stats *stats;
361 if (msg->header.cmd != NGM_SOURCE_CLR_STATS) {
362 NG_MKRESPONSE(resp, msg,
363 sizeof(*stats), M_NOWAIT);
368 sc->stats.queueOctets = sc->queueOctets;
369 sc->stats.queueFrames = sc->snd_queue.ifq_len;
370 if ((sc->node->nd_flags & NG_SOURCE_ACTIVE)
371 && !timevalisset(&sc->stats.endTime)) {
372 getmicrotime(&sc->stats.elapsedTime);
373 timevalsub(&sc->stats.elapsedTime,
374 &sc->stats.startTime);
376 stats = (struct ng_source_stats *)resp->data;
377 bcopy(&sc->stats, stats, sizeof(* stats));
379 if (msg->header.cmd != NGM_SOURCE_GET_STATS)
380 bzero(&sc->stats, sizeof(sc->stats));
383 case NGM_SOURCE_START:
387 if (msg->header.arglen != sizeof(uint64_t)) {
392 packets = *(uint64_t *)msg->data;
394 error = ng_source_start(sc, packets);
398 case NGM_SOURCE_STOP:
401 case NGM_SOURCE_CLR_DATA:
402 ng_source_clr_data(sc);
404 case NGM_SOURCE_SETIFACE:
406 char *ifname = (char *)msg->data;
408 if (msg->header.arglen < 2) {
413 ng_source_store_output_ifp(sc, ifname);
416 case NGM_SOURCE_SETPPS:
420 if (msg->header.arglen != sizeof(uint32_t)) {
425 pps = *(uint32_t *)msg->data;
427 sc->stats.maxPps = pps;
431 case NGM_SOURCE_SET_TIMESTAMP:
433 struct ng_source_embed_info *embed;
435 if (msg->header.arglen != sizeof(*embed)) {
439 embed = (struct ng_source_embed_info *)msg->data;
440 bcopy(embed, &sc->embed_timestamp, sizeof(*embed));
444 case NGM_SOURCE_GET_TIMESTAMP:
446 struct ng_source_embed_info *embed;
448 NG_MKRESPONSE(resp, msg, sizeof(*embed), M_DONTWAIT);
453 embed = (struct ng_source_embed_info *)resp->data;
454 bcopy(&sc->embed_timestamp, embed, sizeof(*embed));
458 case NGM_SOURCE_SET_COUNTER:
460 struct ng_source_embed_cnt_info *embed;
462 if (msg->header.arglen != sizeof(*embed)) {
466 embed = (struct ng_source_embed_cnt_info *)msg->data;
467 if (embed->index >= NG_SOURCE_COUNTERS ||
468 !(embed->width == 1 || embed->width == 2 ||
469 embed->width == 4)) {
473 bcopy(embed, &sc->embed_counter[embed->index],
478 case NGM_SOURCE_GET_COUNTER:
480 uint8_t index = *(uint8_t *)msg->data;
481 struct ng_source_embed_cnt_info *embed;
483 if (index >= NG_SOURCE_COUNTERS) {
487 NG_MKRESPONSE(resp, msg, sizeof(*embed), M_DONTWAIT);
492 embed = (struct ng_source_embed_cnt_info *)resp->data;
493 bcopy(&sc->embed_counter[index], embed, sizeof(*embed));
502 case NGM_ETHER_COOKIE:
503 if (!(msg->header.flags & NGF_RESP)) {
507 switch (msg->header.cmd) {
508 case NGM_ETHER_GET_IFNAME:
510 char *ifname = (char *)msg->data;
512 if (msg->header.arglen < 2) {
517 if (ng_source_store_output_ifp(sc, ifname) == 0)
518 ng_source_set_autosrc(sc, 0);
531 /* Take care of synchronous response, if any. */
532 NG_RESPOND_MSG(error, node, item, resp);
533 /* Free the message and return. */
539 * Receive data on a hook
541 * If data comes in the input hook, enqueue it on the send queue.
542 * If data comes in the output hook, discard it.
545 ng_source_rcvdata(hook_p hook, item_p item)
547 sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
555 if (hook == sc->output) {
560 KASSERT(hook == sc->input, ("%s: no hook!", __func__));
562 /* Enqueue packet. */
563 /* XXX should we check IF_QFULL() ? */
564 _IF_ENQUEUE(&sc->snd_queue, m);
565 sc->queueOctets += m->m_pkthdr.len;
572 * Shutdown processing
575 ng_source_rmnode(node_p node)
577 sc_p sc = NG_NODE_PRIVATE(node);
580 ng_source_clr_data(sc);
581 NG_NODE_SET_PRIVATE(node, NULL);
583 free(sc, M_NETGRAPH);
592 ng_source_disconnect(hook_p hook)
596 sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
597 KASSERT(sc != NULL, ("%s: null node private", __func__));
598 if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hook == sc->output)
599 ng_rmnode_self(NG_HOOK_NODE(hook));
604 * Set sc->output_ifp to point to the struct ifnet of the interface
605 * reached via our output hook.
608 ng_source_store_output_ifp(sc_p sc, char *ifname)
612 ifp = ifunit(ifname);
615 printf("%s: can't find interface %s\n", __func__, ifname);
618 sc->output_ifp = ifp;
621 /* XXX mucking with a drivers ifqueue size is ugly but we need it
622 * to queue a lot of packets to get close to line rate on a gigabit
623 * interface with small packets.
624 * XXX we should restore the original value at stop or disconnect
626 if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN) {
627 printf("ng_source: changing ifq_maxlen from %d to %d\n",
628 ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN);
629 ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN;
636 * Set the attached ethernet node's ethernet source address override flag.
639 ng_source_set_autosrc(sc_p sc, uint32_t flag)
644 NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC,
645 sizeof (uint32_t), M_NOWAIT);
649 *(uint32_t *)msg->data = flag;
650 NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output, 0);
655 * Clear out the data we've queued
658 ng_source_clr_data (sc_p sc)
663 _IF_DEQUEUE(&sc->snd_queue, m);
669 sc->last_packet = NULL;
673 * Start sending queued data out the output hook
676 ng_source_start(sc_p sc, uint64_t packets)
678 if (sc->output_ifp == NULL) {
679 printf("ng_source: start without iface configured\n");
683 if (sc->node->nd_flags & NG_SOURCE_ACTIVE)
686 sc->node->nd_flags |= NG_SOURCE_ACTIVE;
688 sc->packets = packets;
689 timevalclear(&sc->stats.elapsedTime);
690 timevalclear(&sc->stats.endTime);
691 getmicrotime(&sc->stats.startTime);
692 getmicrotime(&sc->stats.lastTime);
693 ng_callout(&sc->intr_ch, sc->node, NULL, 0,
694 ng_source_intr, sc, 0);
700 * Stop sending queued data out the output hook
703 ng_source_stop(sc_p sc)
705 ng_uncallout(&sc->intr_ch, sc->node);
706 sc->node->nd_flags &= ~NG_SOURCE_ACTIVE;
707 getmicrotime(&sc->stats.endTime);
708 sc->stats.elapsedTime = sc->stats.endTime;
709 timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime);
713 * While active called every NG_SOURCE_INTR_TICKS ticks.
714 * Sends as many packets as the interface connected to our
715 * output hook is able to enqueue.
718 ng_source_intr(node_p node, hook_p hook, void *arg1, int arg2)
720 sc_p sc = (sc_p)arg1;
724 KASSERT(sc != NULL, ("%s: null node private", __func__));
726 if (sc->packets == 0 || sc->output == NULL
727 || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) {
732 if (sc->output_ifp != NULL) {
733 ifq = (struct ifqueue *)&sc->output_ifp->if_snd;
734 packets = ifq->ifq_maxlen - ifq->ifq_len;
736 packets = sc->snd_queue.ifq_len;
738 if (sc->stats.maxPps != 0) {
739 struct timeval now, elapsed;
745 timevalsub(&elapsed, &sc->stats.lastTime);
746 usec = elapsed.tv_sec * 1000000 + elapsed.tv_usec;
747 maxpkt = (uint64_t)sc->stats.maxPps * usec / 1000000;
748 sc->stats.lastTime = now;
749 if (packets > maxpkt)
753 ng_source_send(sc, packets, NULL);
754 if (sc->packets == 0)
757 ng_callout(&sc->intr_ch, node, NULL, NG_SOURCE_INTR_TICKS,
758 ng_source_intr, sc, 0);
762 * Send packets out our output hook.
765 ng_source_send(sc_p sc, int tosend, int *sent_p)
771 KASSERT(tosend >= 0, ("%s: negative tosend param", __func__));
772 KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE,
773 ("%s: inactive node", __func__));
775 if ((uint64_t)tosend > sc->packets)
776 tosend = sc->packets;
778 /* Go through the queue sending packets one by one. */
779 for (sent = 0; error == 0 && sent < tosend; ++sent) {
780 _IF_DEQUEUE(&sc->snd_queue, m);
784 /* Duplicate and modify the packet. */
785 error = ng_source_dup_mod(sc, m, &m2);
787 if (error == ENOBUFS)
788 _IF_PREPEND(&sc->snd_queue, m);
790 _IF_ENQUEUE(&sc->snd_queue, m);
794 /* Re-enqueue the original packet for us. */
795 _IF_ENQUEUE(&sc->snd_queue, m);
797 sc->stats.outFrames++;
798 sc->stats.outOctets += m2->m_pkthdr.len;
799 NG_SEND_DATA_ONLY(error, sc->output, m2);
811 * Modify packet in 'm' by changing 'len' bytes starting at 'offset'
814 * The packet data in 'm' must be in a contiguous buffer in a single mbuf.
817 ng_source_packet_mod(sc_p sc, struct mbuf *m, int offset, int len, caddr_t cp,
823 /* Can't modify beyond end of packet. */
824 /* TODO: Pad packet for this case. */
825 if (offset + len > m->m_len)
828 bcopy(cp, mtod_off(m, offset, caddr_t), len);
832 ng_source_mod_counter(sc_p sc, struct ng_source_embed_cnt_info *cnt,
833 struct mbuf *m, int increment)
838 val = htonl(cnt->next_val);
839 cp = (caddr_t)&val + sizeof(val) - cnt->width;
840 ng_source_packet_mod(sc, m, cnt->offset, cnt->width, cp, cnt->flags);
843 cnt->next_val += increment;
845 if (increment > 0 && cnt->next_val > cnt->max_val) {
846 cnt->next_val = cnt->min_val - 1 +
847 (cnt->next_val - cnt->max_val);
848 if (cnt->next_val > cnt->max_val)
849 cnt->next_val = cnt->max_val;
850 } else if (increment < 0 && cnt->next_val < cnt->min_val) {
851 cnt->next_val = cnt->max_val + 1 +
852 (cnt->next_val - cnt->min_val);
853 if (cnt->next_val < cnt->min_val)
854 cnt->next_val = cnt->max_val;
860 ng_source_dup_mod(sc_p sc, struct mbuf *m0, struct mbuf **m_ptr)
863 struct ng_source_embed_cnt_info *cnt;
864 struct ng_source_embed_info *ts;
869 /* Are we going to modify packets? */
870 modify = sc->embed_timestamp.flags & NGM_SOURCE_EMBED_ENABLE;
871 for (i = 0; !modify && i < NG_SOURCE_COUNTERS; ++i)
872 modify = sc->embed_counter[i].flags & NGM_SOURCE_EMBED_ENABLE;
874 /* Duplicate the packet. */
876 m = m_dup(m0, M_DONTWAIT);
878 m = m_copypacket(m0, M_DONTWAIT);
888 /* Modify the copied packet for sending. */
889 KASSERT(M_WRITABLE(m), ("%s: packet not writable", __func__));
891 for (i = 0; i < NG_SOURCE_COUNTERS; ++i) {
892 cnt = &sc->embed_counter[i];
893 if (cnt->flags & NGM_SOURCE_EMBED_ENABLE) {
894 if ((cnt->flags & NGM_SOURCE_INC_CNT_PER_LIST) == 0 ||
895 sc->last_packet == m0)
896 increment = cnt->increment;
899 ng_source_mod_counter(sc, cnt, m, increment);
903 ts = &sc->embed_timestamp;
904 if (ts->flags & NGM_SOURCE_EMBED_ENABLE) {
907 now.tv_sec = htonl(now.tv_sec);
908 now.tv_usec = htonl(now.tv_usec);
909 ng_source_packet_mod(sc, m, ts->offset, sizeof (now),
910 (caddr_t)&now, ts->flags);