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 <dchapeskie@sandvine.com>
40 * $FreeBSD: src/sys/netgraph/ng_source.c,v 1.30 2007/03/02 14:36:19 emaste Exp $
44 * This node is used for high speed packet geneneration. It queues
45 * all data recieved on its 'input' hook and when told to start via
46 * a control message it sends the packets out its 'output' hook. In
47 * this way this node can be preloaded with a packet stream which it
48 * can then send continuously as fast as possible.
50 * Currently it just copies the mbufs as required. It could do various
51 * tricks to try and avoid this. Probably the best performance would
52 * be achieved by modifying the appropriate drivers to be told to
53 * self-re-enqueue packets (e.g. the if_bge driver could reuse the same
54 * transmit descriptors) under control of this node; perhaps via some
55 * flag in the mbuf or some such. The node could peek at an appropriate
56 * ifnet flag to see if such support is available for the connected
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/errno.h>
63 #include <sys/kernel.h>
64 #include <sys/malloc.h>
66 #include <sys/socket.h>
67 #include <sys/syslog.h>
69 #include <net/if_var.h>
70 #include "ng_message.h"
74 #include "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 ifqueue 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[] = {
127 { "tv_sec", &ng_parse_int32_type },
128 { "tv_usec", &ng_parse_int32_type },
131 const struct ng_parse_type ng_source_timeval_type = {
132 &ng_parse_struct_type,
133 &ng_source_timeval_type_fields
136 /* Parse type for struct ng_source_stats */
137 static const struct ng_parse_struct_field ng_source_stats_type_fields[]
138 = NG_SOURCE_STATS_TYPE_INFO;
139 static const struct ng_parse_type ng_source_stats_type = {
140 &ng_parse_struct_type,
141 &ng_source_stats_type_fields
144 /* Parse type for struct ng_source_embed_info */
145 static const struct ng_parse_struct_field ng_source_embed_type_fields[] =
146 NG_SOURCE_EMBED_TYPE_INFO;
147 static const struct ng_parse_type ng_source_embed_type = {
148 &ng_parse_struct_type,
149 &ng_source_embed_type_fields
152 /* Parse type for struct ng_source_embed_cnt_info */
153 static const struct ng_parse_struct_field ng_source_embed_cnt_type_fields[] =
154 NG_SOURCE_EMBED_CNT_TYPE_INFO;
155 static const struct ng_parse_type ng_source_embed_cnt_type = {
156 &ng_parse_struct_type,
157 &ng_source_embed_cnt_type_fields
160 /* List of commands and how to convert arguments to/from ASCII */
161 static const struct ng_cmdlist ng_source_cmds[] = {
164 NGM_SOURCE_GET_STATS,
167 &ng_source_stats_type
171 NGM_SOURCE_CLR_STATS,
178 NGM_SOURCE_GETCLR_STATS,
181 &ng_source_stats_type
187 &ng_parse_uint64_type,
208 &ng_parse_string_type,
215 &ng_parse_uint32_type,
220 NGM_SOURCE_SET_TIMESTAMP,
222 &ng_source_embed_type,
227 NGM_SOURCE_GET_TIMESTAMP,
230 &ng_source_embed_type
234 NGM_SOURCE_SET_COUNTER,
236 &ng_source_embed_cnt_type,
241 NGM_SOURCE_GET_COUNTER,
243 &ng_parse_uint8_type,
244 &ng_source_embed_cnt_type
249 /* Netgraph type descriptor */
250 static struct ng_type ng_source_typestruct = {
251 .version = NG_ABI_VERSION,
252 .name = NG_SOURCE_NODE_TYPE,
253 .constructor = ng_source_constructor,
254 .rcvmsg = ng_source_rcvmsg,
255 .shutdown = ng_source_rmnode,
256 .newhook = ng_source_newhook,
257 .connect = ng_source_connect,
258 .rcvdata = ng_source_rcvdata,
259 .disconnect = ng_source_disconnect,
260 .cmdlist = ng_source_cmds,
262 NETGRAPH_INIT(source, &ng_source_typestruct);
264 static int ng_source_set_autosrc(sc_p, uint32_t);
270 ng_source_constructor(node_p node)
274 sc = kmalloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_NULLOK | M_ZERO);
278 NG_NODE_SET_PRIVATE(node, sc);
280 sc->snd_queue.ifq_maxlen = 2048; /* XXX not checked */
281 ng_callout_init(&sc->intr_ch);
290 ng_source_newhook(node_p node, hook_p hook, const char *name)
292 sc_p sc = NG_NODE_PRIVATE(node);
294 if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) {
296 } else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) {
298 sc->output_ifp = NULL;
299 bzero(&sc->stats, sizeof(sc->stats));
307 * Hook has been added
310 ng_source_connect(hook_p hook)
312 sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
317 * If this is "output" hook, then request information
318 * from our downstream.
320 if (hook == sc->output) {
321 NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFNAME,
322 0, M_WAITOK | M_NULLOK);
327 * Our hook and peer hook have HK_INVALID flag set,
328 * so we can't use NG_SEND_MSG_HOOK() macro here.
330 NG_SEND_MSG_ID(dummy_error, sc->node, msg,
331 NG_NODE_ID(NG_PEER_NODE(sc->output)), NG_NODE_ID(sc->node));
338 * Receive a control message
341 ng_source_rcvmsg(node_p node, item_p item, hook_p lasthook)
343 sc_p sc = NG_NODE_PRIVATE(node);
344 struct ng_mesg *msg, *resp = NULL;
347 NGI_GET_MSG(item, msg);
349 switch (msg->header.typecookie) {
350 case NGM_SOURCE_COOKIE:
351 if (msg->header.flags & NGF_RESP) {
355 switch (msg->header.cmd) {
356 case NGM_SOURCE_GET_STATS:
357 case NGM_SOURCE_CLR_STATS:
358 case NGM_SOURCE_GETCLR_STATS:
360 struct ng_source_stats *stats;
362 if (msg->header.cmd != NGM_SOURCE_CLR_STATS) {
363 NG_MKRESPONSE(resp, msg,
364 sizeof(*stats), M_WAITOK | M_NULLOK);
369 sc->stats.queueOctets = sc->queueOctets;
370 sc->stats.queueFrames = sc->snd_queue.ifq_len;
371 if ((sc->node->nd_flags & NG_SOURCE_ACTIVE)
372 && !timevalisset(&sc->stats.endTime)) {
373 getmicrotime(&sc->stats.elapsedTime);
374 timevalsub(&sc->stats.elapsedTime,
375 &sc->stats.startTime);
377 stats = (struct ng_source_stats *)resp->data;
378 bcopy(&sc->stats, stats, sizeof(* stats));
380 if (msg->header.cmd != NGM_SOURCE_GET_STATS)
381 bzero(&sc->stats, sizeof(sc->stats));
384 case NGM_SOURCE_START:
388 if (msg->header.arglen != sizeof(uint64_t)) {
393 packets = *(uint64_t *)msg->data;
395 error = ng_source_start(sc, packets);
399 case NGM_SOURCE_STOP:
402 case NGM_SOURCE_CLR_DATA:
403 ng_source_clr_data(sc);
405 case NGM_SOURCE_SETIFACE:
407 char *ifname = (char *)msg->data;
409 if (msg->header.arglen < 2) {
414 ng_source_store_output_ifp(sc, ifname);
417 case NGM_SOURCE_SETPPS:
421 if (msg->header.arglen != sizeof(uint32_t)) {
426 pps = *(uint32_t *)msg->data;
428 sc->stats.maxPps = pps;
432 case NGM_SOURCE_SET_TIMESTAMP:
434 struct ng_source_embed_info *embed;
436 if (msg->header.arglen != sizeof(*embed)) {
440 embed = (struct ng_source_embed_info *)msg->data;
441 bcopy(embed, &sc->embed_timestamp, sizeof(*embed));
445 case NGM_SOURCE_GET_TIMESTAMP:
447 struct ng_source_embed_info *embed;
449 NG_MKRESPONSE(resp, msg, sizeof(*embed), M_WAITOK);
454 embed = (struct ng_source_embed_info *)resp->data;
455 bcopy(&sc->embed_timestamp, embed, sizeof(*embed));
459 case NGM_SOURCE_SET_COUNTER:
461 struct ng_source_embed_cnt_info *embed;
463 if (msg->header.arglen != sizeof(*embed)) {
467 embed = (struct ng_source_embed_cnt_info *)msg->data;
468 if (embed->index >= NG_SOURCE_COUNTERS ||
469 !(embed->width == 1 || embed->width == 2 ||
470 embed->width == 4)) {
474 bcopy(embed, &sc->embed_counter[embed->index],
479 case NGM_SOURCE_GET_COUNTER:
481 uint8_t index = *(uint8_t *)msg->data;
482 struct ng_source_embed_cnt_info *embed;
484 if (index >= NG_SOURCE_COUNTERS) {
488 NG_MKRESPONSE(resp, msg, sizeof(*embed), M_WAITOK);
493 embed = (struct ng_source_embed_cnt_info *)resp->data;
494 bcopy(&sc->embed_counter[index], embed, sizeof(*embed));
503 case NGM_ETHER_COOKIE:
504 if (!(msg->header.flags & NGF_RESP)) {
508 switch (msg->header.cmd) {
509 case NGM_ETHER_GET_IFNAME:
511 char *ifname = (char *)msg->data;
513 if (msg->header.arglen < 2) {
518 if (ng_source_store_output_ifp(sc, ifname) == 0)
519 ng_source_set_autosrc(sc, 0);
532 /* Take care of synchronous response, if any. */
533 NG_RESPOND_MSG(error, node, item, resp);
534 /* Free the message and return. */
540 * Receive data on a hook
542 * If data comes in the input hook, enqueue it on the send queue.
543 * If data comes in the output hook, discard it.
546 ng_source_rcvdata(hook_p hook, item_p item)
548 sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
556 if (hook == sc->output) {
561 KASSERT(hook == sc->input, ("%s: no hook!", __func__));
563 /* Enqueue packet. */
564 /* XXX should we check IF_QFULL() ? */
565 _IF_ENQUEUE(&sc->snd_queue, m);
566 sc->queueOctets += m->m_pkthdr.len;
573 * Shutdown processing
576 ng_source_rmnode(node_p node)
578 sc_p sc = NG_NODE_PRIVATE(node);
581 ng_source_clr_data(sc);
582 NG_NODE_SET_PRIVATE(node, NULL);
584 kfree(sc, M_NETGRAPH);
593 ng_source_disconnect(hook_p hook)
597 sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
598 KASSERT(sc != NULL, ("%s: null node private", __func__));
599 if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hook == sc->output)
600 ng_rmnode_self(NG_HOOK_NODE(hook));
605 * Set sc->output_ifp to point to the the struct ifnet of the interface
606 * reached via our output hook.
609 ng_source_store_output_ifp(sc_p sc, char *ifname)
614 ifp = ifunit(ifname);
617 printf("%s: can't find interface %d\n", __func__, if_index);
620 sc->output_ifp = ifp;
623 /* XXX mucking with a drivers ifqueue size is ugly but we need it
624 * to queue a lot of packets to get close to line rate on a gigabit
625 * interface with small packets.
626 * XXX we should restore the original value at stop or disconnect
628 s = splimp(); /* XXX is this required? */
629 if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN) {
630 printf("ng_source: changing ifq_maxlen from %d to %d\n",
631 ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN);
632 ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN;
640 * Set the attached ethernet node's ethernet source address override flag.
643 ng_source_set_autosrc(sc_p sc, uint32_t flag)
648 NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC,
649 sizeof (uint32_t), M_WAITOK | M_NULLOK);
653 *(uint32_t *)msg->data = flag;
654 NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output, 0);
659 * Clear out the data we've queued
662 ng_source_clr_data (sc_p sc)
667 _IF_DEQUEUE(&sc->snd_queue, m);
673 sc->last_packet = NULL;
677 * Start sending queued data out the output hook
680 ng_source_start(sc_p sc, uint64_t packets)
682 if (sc->output_ifp == NULL) {
683 printf("ng_source: start without iface configured\n");
687 if (sc->node->nd_flags & NG_SOURCE_ACTIVE)
690 sc->node->nd_flags |= NG_SOURCE_ACTIVE;
692 sc->packets = packets;
693 timevalclear(&sc->stats.elapsedTime);
694 timevalclear(&sc->stats.endTime);
695 getmicrotime(&sc->stats.startTime);
696 getmicrotime(&sc->stats.lastTime);
697 ng_callout(&sc->intr_ch, sc->node, NULL, 0,
698 ng_source_intr, sc, 0);
704 * Stop sending queued data out the output hook
707 ng_source_stop(sc_p sc)
709 ng_uncallout(&sc->intr_ch, sc->node);
710 sc->node->nd_flags &= ~NG_SOURCE_ACTIVE;
711 getmicrotime(&sc->stats.endTime);
712 sc->stats.elapsedTime = sc->stats.endTime;
713 timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime);
717 * While active called every NG_SOURCE_INTR_TICKS ticks.
718 * Sends as many packets as the interface connected to our
719 * output hook is able to enqueue.
722 ng_source_intr(node_p node, hook_p hook, void *arg1, int arg2)
724 sc_p sc = (sc_p)arg1;
728 KASSERT(sc != NULL, ("%s: null node private", __func__));
730 if (sc->packets == 0 || sc->output == NULL
731 || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) {
736 if (sc->output_ifp != NULL) {
737 ifq = (struct ifqueue *)&sc->output_ifp->if_snd;
738 packets = ifq->ifq_maxlen - ifq->ifq_len;
740 packets = sc->snd_queue.ifq_len;
742 if (sc->stats.maxPps != 0) {
743 struct timeval now, elapsed;
749 timevalsub(&elapsed, &sc->stats.lastTime);
750 usec = elapsed.tv_sec * 1000000 + elapsed.tv_usec;
751 maxpkt = (uint64_t)sc->stats.maxPps * usec / 1000000;
752 sc->stats.lastTime = now;
753 if (packets > maxpkt)
757 ng_source_send(sc, packets, NULL);
758 if (sc->packets == 0)
761 ng_callout(&sc->intr_ch, node, NULL, NG_SOURCE_INTR_TICKS,
762 ng_source_intr, sc, 0);
766 * Send packets out our output hook.
769 ng_source_send(sc_p sc, int tosend, int *sent_p)
775 KASSERT(tosend >= 0, ("%s: negative tosend param", __func__));
776 KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE,
777 ("%s: inactive node", __func__));
779 if ((uint64_t)tosend > sc->packets)
780 tosend = sc->packets;
782 /* Go through the queue sending packets one by one. */
783 for (sent = 0; error == 0 && sent < tosend; ++sent) {
784 _IF_DEQUEUE(&sc->snd_queue, m);
788 /* Duplicate and modify the packet. */
789 error = ng_source_dup_mod(sc, m, &m2);
791 if (error == ENOBUFS)
792 _IF_PREPEND(&sc->snd_queue, m);
794 _IF_ENQUEUE(&sc->snd_queue, m);
798 /* Re-enqueue the original packet for us. */
799 _IF_ENQUEUE(&sc->snd_queue, m);
801 sc->stats.outFrames++;
802 sc->stats.outOctets += m2->m_pkthdr.len;
803 NG_SEND_DATA_ONLY(error, sc->output, m2);
815 * Modify packet in 'm' by changing 'len' bytes starting at 'offset'
818 * The packet data in 'm' must be in a contiguous buffer in a single mbuf.
821 ng_source_packet_mod(sc_p sc, struct mbuf *m, int offset, int len, caddr_t cp,
827 /* Can't modify beyond end of packet. */
828 /* TODO: Pad packet for this case. */
829 if (offset + len > m->m_len)
832 bcopy(cp, mtod_off(m, offset, caddr_t), len);
836 ng_source_mod_counter(sc_p sc, struct ng_source_embed_cnt_info *cnt,
837 struct mbuf *m, int increment)
842 val = htonl(cnt->next_val);
843 cp = (caddr_t)&val + sizeof(val) - cnt->width;
844 ng_source_packet_mod(sc, m, cnt->offset, cnt->width, cp, cnt->flags);
847 cnt->next_val += increment;
849 if (increment > 0 && cnt->next_val > cnt->max_val) {
850 cnt->next_val = cnt->min_val - 1 +
851 (cnt->next_val - cnt->max_val);
852 if (cnt->next_val > cnt->max_val)
853 cnt->next_val = cnt->max_val;
854 } else if (increment < 0 && cnt->next_val < cnt->min_val) {
855 cnt->next_val = cnt->max_val + 1 +
856 (cnt->next_val - cnt->min_val);
857 if (cnt->next_val < cnt->min_val)
858 cnt->next_val = cnt->max_val;
864 ng_source_dup_mod(sc_p sc, struct mbuf *m0, struct mbuf **m_ptr)
867 struct ng_source_embed_cnt_info *cnt;
868 struct ng_source_embed_info *ts;
873 /* Are we going to modify packets? */
874 modify = sc->embed_timestamp.flags & NGM_SOURCE_EMBED_ENABLE;
875 for (i = 0; !modify && i < NG_SOURCE_COUNTERS; ++i)
876 modify = sc->embed_counter[i].flags & NGM_SOURCE_EMBED_ENABLE;
878 /* Duplicate the packet. */
880 m = m_dup(m0, MB_DONTWAIT);
882 m = m_copypacket(m0, MB_DONTWAIT);
892 /* Modify the copied packet for sending. */
893 KASSERT(M_WRITABLE(m), ("%s: packet not writable", __func__));
895 for (i = 0; i < NG_SOURCE_COUNTERS; ++i) {
896 cnt = &sc->embed_counter[i];
897 if (cnt->flags & NGM_SOURCE_EMBED_ENABLE) {
898 if ((cnt->flags & NGM_SOURCE_INC_CNT_PER_LIST) == 0 ||
899 sc->last_packet == m0)
900 increment = cnt->increment;
903 ng_source_mod_counter(sc, cnt, m, increment);
907 ts = &sc->embed_timestamp;
908 if (ts->flags & NGM_SOURCE_EMBED_ENABLE) {
911 now.tv_sec = htonl(now.tv_sec);
912 now.tv_usec = htonl(now.tv_usec);
913 ng_source_packet_mod(sc, m, ts->offset, sizeof (now),
914 (caddr_t)&now, ts->flags);