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 received 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 <net/if_private.h>
72 #include <netgraph/ng_message.h>
73 #include <netgraph/netgraph.h>
74 #include <netgraph/ng_parse.h>
75 #include <netgraph/ng_ether.h>
76 #include <netgraph/ng_source.h>
78 #define NG_SOURCE_INTR_TICKS 1
79 #define NG_SOURCE_DRIVER_IFQ_MAXLEN (4*1024)
81 #define mtod_off(m,off,t) ((t)(mtod((m),caddr_t)+(off)))
88 struct ng_source_stats stats;
89 struct mbufq snd_queue; /* packets to send */
90 struct mbuf *last_packet; /* last pkt in queue */
91 struct ifnet *output_ifp;
92 struct callout intr_ch;
93 uint64_t packets; /* packets to send */
95 struct ng_source_embed_info embed_timestamp;
96 struct ng_source_embed_cnt_info embed_counter[NG_SOURCE_COUNTERS];
98 typedef struct privdata *sc_p;
101 #define NG_SOURCE_ACTIVE (NGF_TYPE1)
103 /* Netgraph methods */
104 static ng_constructor_t ng_source_constructor;
105 static ng_rcvmsg_t ng_source_rcvmsg;
106 static ng_shutdown_t ng_source_rmnode;
107 static ng_newhook_t ng_source_newhook;
108 static ng_connect_t ng_source_connect;
109 static ng_rcvdata_t ng_source_rcvdata;
110 static ng_disconnect_t ng_source_disconnect;
112 /* Other functions */
113 static void ng_source_intr(node_p, hook_p, void *, int);
114 static void ng_source_clr_data (sc_p);
115 static int ng_source_start (sc_p, uint64_t);
116 static void ng_source_stop (sc_p);
117 static int ng_source_send (sc_p, int, int *);
118 static int ng_source_store_output_ifp(sc_p, char *);
119 static void ng_source_packet_mod(sc_p, struct mbuf *,
120 int, int, caddr_t, int);
121 static void ng_source_mod_counter(sc_p sc,
122 struct ng_source_embed_cnt_info *cnt,
123 struct mbuf *m, int increment);
124 static int ng_source_dup_mod(sc_p, struct mbuf *,
127 /* Parse type for timeval */
128 static const struct ng_parse_struct_field ng_source_timeval_type_fields[] = {
130 { "tv_sec", &ng_parse_int32_type },
132 { "tv_sec", &ng_parse_int64_type },
135 { "tv_usec", &ng_parse_int64_type },
137 { "tv_usec", &ng_parse_int32_type },
141 const struct ng_parse_type ng_source_timeval_type = {
142 &ng_parse_struct_type,
143 &ng_source_timeval_type_fields
146 /* Parse type for struct ng_source_stats */
147 static const struct ng_parse_struct_field ng_source_stats_type_fields[]
148 = NG_SOURCE_STATS_TYPE_INFO;
149 static const struct ng_parse_type ng_source_stats_type = {
150 &ng_parse_struct_type,
151 &ng_source_stats_type_fields
154 /* Parse type for struct ng_source_embed_info */
155 static const struct ng_parse_struct_field ng_source_embed_type_fields[] =
156 NG_SOURCE_EMBED_TYPE_INFO;
157 static const struct ng_parse_type ng_source_embed_type = {
158 &ng_parse_struct_type,
159 &ng_source_embed_type_fields
162 /* Parse type for struct ng_source_embed_cnt_info */
163 static const struct ng_parse_struct_field ng_source_embed_cnt_type_fields[] =
164 NG_SOURCE_EMBED_CNT_TYPE_INFO;
165 static const struct ng_parse_type ng_source_embed_cnt_type = {
166 &ng_parse_struct_type,
167 &ng_source_embed_cnt_type_fields
170 /* List of commands and how to convert arguments to/from ASCII */
171 static const struct ng_cmdlist ng_source_cmds[] = {
174 NGM_SOURCE_GET_STATS,
177 &ng_source_stats_type
181 NGM_SOURCE_CLR_STATS,
188 NGM_SOURCE_GETCLR_STATS,
191 &ng_source_stats_type
197 &ng_parse_uint64_type,
218 &ng_parse_string_type,
225 &ng_parse_uint32_type,
230 NGM_SOURCE_SET_TIMESTAMP,
232 &ng_source_embed_type,
237 NGM_SOURCE_GET_TIMESTAMP,
240 &ng_source_embed_type
244 NGM_SOURCE_SET_COUNTER,
246 &ng_source_embed_cnt_type,
251 NGM_SOURCE_GET_COUNTER,
253 &ng_parse_uint8_type,
254 &ng_source_embed_cnt_type
259 /* Netgraph type descriptor */
260 static struct ng_type ng_source_typestruct = {
261 .version = NG_ABI_VERSION,
262 .name = NG_SOURCE_NODE_TYPE,
263 .constructor = ng_source_constructor,
264 .rcvmsg = ng_source_rcvmsg,
265 .shutdown = ng_source_rmnode,
266 .newhook = ng_source_newhook,
267 .connect = ng_source_connect,
268 .rcvdata = ng_source_rcvdata,
269 .disconnect = ng_source_disconnect,
270 .cmdlist = ng_source_cmds,
272 NETGRAPH_INIT(source, &ng_source_typestruct);
274 static int ng_source_set_autosrc(sc_p, uint32_t);
280 ng_source_constructor(node_p node)
284 sc = malloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
286 NG_NODE_SET_PRIVATE(node, sc);
288 mbufq_init(&sc->snd_queue, 2048);
289 ng_callout_init(&sc->intr_ch);
298 ng_source_newhook(node_p node, hook_p hook, const char *name)
300 sc_p sc = NG_NODE_PRIVATE(node);
302 if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) {
304 } else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) {
306 sc->output_ifp = NULL;
307 bzero(&sc->stats, sizeof(sc->stats));
315 * Hook has been added
318 ng_source_connect(hook_p hook)
320 sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
325 * If this is "output" hook, then request information
326 * from our downstream.
328 if (hook == sc->output) {
329 NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFNAME,
335 * Our hook and peer hook have HK_INVALID flag set,
336 * so we can't use NG_SEND_MSG_HOOK() macro here.
338 NG_SEND_MSG_ID(dummy_error, sc->node, msg,
339 NG_NODE_ID(NG_PEER_NODE(sc->output)), NG_NODE_ID(sc->node));
346 * Receive a control message
349 ng_source_rcvmsg(node_p node, item_p item, hook_p lasthook)
351 sc_p sc = NG_NODE_PRIVATE(node);
352 struct ng_mesg *msg, *resp = NULL;
355 NGI_GET_MSG(item, msg);
357 switch (msg->header.typecookie) {
358 case NGM_SOURCE_COOKIE:
359 if (msg->header.flags & NGF_RESP) {
363 switch (msg->header.cmd) {
364 case NGM_SOURCE_GET_STATS:
365 case NGM_SOURCE_CLR_STATS:
366 case NGM_SOURCE_GETCLR_STATS:
368 struct ng_source_stats *stats;
370 if (msg->header.cmd != NGM_SOURCE_CLR_STATS) {
371 NG_MKRESPONSE(resp, msg,
372 sizeof(*stats), M_NOWAIT);
377 sc->stats.queueOctets = sc->queueOctets;
378 sc->stats.queueFrames = mbufq_len(&sc->snd_queue);
379 if ((sc->node->nd_flags & NG_SOURCE_ACTIVE)
380 && !timevalisset(&sc->stats.endTime)) {
381 getmicrotime(&sc->stats.elapsedTime);
382 timevalsub(&sc->stats.elapsedTime,
383 &sc->stats.startTime);
385 stats = (struct ng_source_stats *)resp->data;
386 bcopy(&sc->stats, stats, sizeof(* stats));
388 if (msg->header.cmd != NGM_SOURCE_GET_STATS)
389 bzero(&sc->stats, sizeof(sc->stats));
392 case NGM_SOURCE_START:
396 if (msg->header.arglen != sizeof(uint64_t)) {
401 packets = *(uint64_t *)msg->data;
403 error = ng_source_start(sc, packets);
407 case NGM_SOURCE_STOP:
410 case NGM_SOURCE_CLR_DATA:
411 ng_source_clr_data(sc);
413 case NGM_SOURCE_SETIFACE:
415 char *ifname = (char *)msg->data;
417 if (msg->header.arglen < 2) {
422 ng_source_store_output_ifp(sc, ifname);
425 case NGM_SOURCE_SETPPS:
429 if (msg->header.arglen != sizeof(uint32_t)) {
434 pps = *(uint32_t *)msg->data;
436 sc->stats.maxPps = pps;
440 case NGM_SOURCE_SET_TIMESTAMP:
442 struct ng_source_embed_info *embed;
444 if (msg->header.arglen != sizeof(*embed)) {
448 embed = (struct ng_source_embed_info *)msg->data;
449 bcopy(embed, &sc->embed_timestamp, sizeof(*embed));
453 case NGM_SOURCE_GET_TIMESTAMP:
455 struct ng_source_embed_info *embed;
457 NG_MKRESPONSE(resp, msg, sizeof(*embed), M_NOWAIT);
462 embed = (struct ng_source_embed_info *)resp->data;
463 bcopy(&sc->embed_timestamp, embed, sizeof(*embed));
467 case NGM_SOURCE_SET_COUNTER:
469 struct ng_source_embed_cnt_info *embed;
471 if (msg->header.arglen != sizeof(*embed)) {
475 embed = (struct ng_source_embed_cnt_info *)msg->data;
476 if (embed->index >= NG_SOURCE_COUNTERS ||
477 !(embed->width == 1 || embed->width == 2 ||
478 embed->width == 4)) {
482 bcopy(embed, &sc->embed_counter[embed->index],
487 case NGM_SOURCE_GET_COUNTER:
489 uint8_t index = *(uint8_t *)msg->data;
490 struct ng_source_embed_cnt_info *embed;
492 if (index >= NG_SOURCE_COUNTERS) {
496 NG_MKRESPONSE(resp, msg, sizeof(*embed), M_NOWAIT);
501 embed = (struct ng_source_embed_cnt_info *)resp->data;
502 bcopy(&sc->embed_counter[index], embed, sizeof(*embed));
511 case NGM_ETHER_COOKIE:
512 if (!(msg->header.flags & NGF_RESP)) {
516 switch (msg->header.cmd) {
517 case NGM_ETHER_GET_IFNAME:
519 char *ifname = (char *)msg->data;
521 if (msg->header.arglen < 2) {
526 if (ng_source_store_output_ifp(sc, ifname) == 0)
527 ng_source_set_autosrc(sc, 0);
540 /* Take care of synchronous response, if any. */
541 NG_RESPOND_MSG(error, node, item, resp);
542 /* Free the message and return. */
548 * Receive data on a hook
550 * If data comes in the input hook, enqueue it on the send queue.
551 * If data comes in the output hook, discard it.
554 ng_source_rcvdata(hook_p hook, item_p item)
556 sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
564 if (hook == sc->output) {
569 KASSERT(hook == sc->input, ("%s: no hook!", __func__));
571 /* Enqueue packet if the queue isn't full. */
572 error = mbufq_enqueue(&sc->snd_queue, m);
577 sc->queueOctets += m->m_pkthdr.len;
584 * Shutdown processing
587 ng_source_rmnode(node_p node)
589 sc_p sc = NG_NODE_PRIVATE(node);
592 ng_source_clr_data(sc);
593 NG_NODE_SET_PRIVATE(node, NULL);
595 free(sc, M_NETGRAPH);
604 ng_source_disconnect(hook_p hook)
608 sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
609 KASSERT(sc != NULL, ("%s: null node private", __func__));
610 if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hook == sc->output)
611 ng_rmnode_self(NG_HOOK_NODE(hook));
616 * Set sc->output_ifp to point to the struct ifnet of the interface
617 * reached via our output hook.
620 ng_source_store_output_ifp(sc_p sc, char *ifname)
624 ifp = ifunit(ifname);
627 printf("%s: can't find interface %s\n", __func__, ifname);
630 sc->output_ifp = ifp;
633 /* XXX mucking with a drivers ifqueue size is ugly but we need it
634 * to queue a lot of packets to get close to line rate on a gigabit
635 * interface with small packets.
636 * XXX we should restore the original value at stop or disconnect
638 if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN) {
639 printf("ng_source: changing ifq_maxlen from %d to %d\n",
640 ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN);
641 ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN;
648 * Set the attached ethernet node's ethernet source address override flag.
651 ng_source_set_autosrc(sc_p sc, uint32_t flag)
656 NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC,
657 sizeof (uint32_t), M_NOWAIT);
661 *(uint32_t *)msg->data = flag;
662 NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output, 0);
667 * Clear out the data we've queued
670 ng_source_clr_data (sc_p sc)
675 m = mbufq_dequeue(&sc->snd_queue);
681 sc->last_packet = NULL;
685 * Start sending queued data out the output hook
688 ng_source_start(sc_p sc, uint64_t packets)
690 if (sc->output_ifp == NULL && sc->stats.maxPps == 0) {
691 printf("ng_source: start without iface or pps configured\n");
695 if (sc->node->nd_flags & NG_SOURCE_ACTIVE)
698 sc->node->nd_flags |= NG_SOURCE_ACTIVE;
700 sc->packets = packets;
701 timevalclear(&sc->stats.elapsedTime);
702 timevalclear(&sc->stats.endTime);
703 getmicrotime(&sc->stats.startTime);
704 getmicrotime(&sc->stats.lastTime);
705 ng_callout(&sc->intr_ch, sc->node, NULL, 0,
706 ng_source_intr, sc, 0);
712 * Stop sending queued data out the output hook
715 ng_source_stop(sc_p sc)
717 ng_uncallout(&sc->intr_ch, sc->node);
718 sc->node->nd_flags &= ~NG_SOURCE_ACTIVE;
719 getmicrotime(&sc->stats.endTime);
720 sc->stats.elapsedTime = sc->stats.endTime;
721 timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime);
725 * While active called every NG_SOURCE_INTR_TICKS ticks.
726 * Sends as many packets as the interface connected to our
727 * output hook is able to enqueue.
730 ng_source_intr(node_p node, hook_p hook, void *arg1, int arg2)
732 sc_p sc = (sc_p)arg1;
736 KASSERT(sc != NULL, ("%s: null node private", __func__));
738 if (sc->packets == 0 || sc->output == NULL
739 || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) {
744 if (sc->output_ifp != NULL) {
745 ifq = (struct ifqueue *)&sc->output_ifp->if_snd;
746 packets = ifq->ifq_maxlen - ifq->ifq_len;
748 packets = mbufq_len(&sc->snd_queue);
750 if (sc->stats.maxPps != 0) {
751 struct timeval now, elapsed;
757 timevalsub(&elapsed, &sc->stats.lastTime);
758 usec = elapsed.tv_sec * 1000000 + elapsed.tv_usec;
759 maxpkt = (uint64_t)sc->stats.maxPps * usec / 1000000;
760 sc->stats.lastTime = now;
761 if (packets > maxpkt)
765 ng_source_send(sc, packets, NULL);
766 if (sc->packets == 0)
769 ng_callout(&sc->intr_ch, node, NULL, NG_SOURCE_INTR_TICKS,
770 ng_source_intr, sc, 0);
774 * Send packets out our output hook.
777 ng_source_send(sc_p sc, int tosend, int *sent_p)
783 KASSERT(tosend >= 0, ("%s: negative tosend param", __func__));
784 KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE,
785 ("%s: inactive node", __func__));
787 if ((uint64_t)tosend > sc->packets)
788 tosend = sc->packets;
790 /* Go through the queue sending packets one by one. */
791 for (sent = 0; error == 0 && sent < tosend; ++sent) {
792 m = mbufq_dequeue(&sc->snd_queue);
796 /* Duplicate and modify the packet. */
797 error = ng_source_dup_mod(sc, m, &m2);
799 if (error == ENOBUFS)
800 mbufq_prepend(&sc->snd_queue, m);
802 (void)mbufq_enqueue(&sc->snd_queue, m);
807 * Re-enqueue the original packet for us. The queue
808 * has a free slot, because we dequeued the packet
809 * above and this callout function runs under WRITER
812 error = mbufq_enqueue(&sc->snd_queue, m);
813 KASSERT(error == 0, ("%s: re-enqueue packet failed", __func__));
815 sc->stats.outFrames++;
816 sc->stats.outOctets += m2->m_pkthdr.len;
817 NG_SEND_DATA_ONLY(error, sc->output, m2);
829 * Modify packet in 'm' by changing 'len' bytes starting at 'offset'
832 * The packet data in 'm' must be in a contiguous buffer in a single mbuf.
835 ng_source_packet_mod(sc_p sc, struct mbuf *m, int offset, int len, caddr_t cp,
841 /* Can't modify beyond end of packet. */
842 /* TODO: Pad packet for this case. */
843 if (offset + len > m->m_len)
846 bcopy(cp, mtod_off(m, offset, caddr_t), len);
850 ng_source_mod_counter(sc_p sc, struct ng_source_embed_cnt_info *cnt,
851 struct mbuf *m, int increment)
856 val = htonl(cnt->next_val);
857 cp = (caddr_t)&val + sizeof(val) - cnt->width;
858 ng_source_packet_mod(sc, m, cnt->offset, cnt->width, cp, cnt->flags);
861 cnt->next_val += increment;
863 if (increment > 0 && cnt->next_val > cnt->max_val) {
864 cnt->next_val = cnt->min_val - 1 +
865 (cnt->next_val - cnt->max_val);
866 if (cnt->next_val > cnt->max_val)
867 cnt->next_val = cnt->max_val;
868 } else if (increment < 0 && cnt->next_val < cnt->min_val) {
869 cnt->next_val = cnt->max_val + 1 +
870 (cnt->next_val - cnt->min_val);
871 if (cnt->next_val < cnt->min_val)
872 cnt->next_val = cnt->max_val;
878 ng_source_dup_mod(sc_p sc, struct mbuf *m0, struct mbuf **m_ptr)
881 struct ng_source_embed_cnt_info *cnt;
882 struct ng_source_embed_info *ts;
887 /* Are we going to modify packets? */
888 modify = sc->embed_timestamp.flags & NGM_SOURCE_EMBED_ENABLE;
889 for (i = 0; !modify && i < NG_SOURCE_COUNTERS; ++i)
890 modify = sc->embed_counter[i].flags & NGM_SOURCE_EMBED_ENABLE;
892 /* Duplicate the packet. */
894 m = m_dup(m0, M_NOWAIT);
896 m = m_copypacket(m0, M_NOWAIT);
906 /* Modify the copied packet for sending. */
907 KASSERT(M_WRITABLE(m), ("%s: packet not writable", __func__));
909 for (i = 0; i < NG_SOURCE_COUNTERS; ++i) {
910 cnt = &sc->embed_counter[i];
911 if (cnt->flags & NGM_SOURCE_EMBED_ENABLE) {
912 if ((cnt->flags & NGM_SOURCE_INC_CNT_PER_LIST) == 0 ||
913 sc->last_packet == m0)
914 increment = cnt->increment;
917 ng_source_mod_counter(sc, cnt, m, increment);
921 ts = &sc->embed_timestamp;
922 if (ts->flags & NGM_SOURCE_EMBED_ENABLE) {
925 now.tv_sec = htonl(now.tv_sec);
926 now.tv_usec = htonl(now.tv_usec);
927 ng_source_packet_mod(sc, m, ts->offset, sizeof (now),
928 (caddr_t)&now, ts->flags);