1585ff168SJulian Elischer /*
2585ff168SJulian Elischer * ng_source.c
3c398230bSWarner Losh */
4c398230bSWarner Losh
5c398230bSWarner Losh /*-
6d8f5d037SGleb Smirnoff * Copyright (c) 2005 Gleb Smirnoff <glebius@FreeBSD.org>
7585ff168SJulian Elischer * Copyright 2002 Sandvine Inc.
8585ff168SJulian Elischer * All rights reserved.
9585ff168SJulian Elischer *
10585ff168SJulian Elischer * Subject to the following obligations and disclaimer of warranty, use and
11585ff168SJulian Elischer * redistribution of this software, in source or object code forms, with or
124b52f283SJulian Elischer * without modifications are expressly permitted by Sandvine Inc.; provided,
13585ff168SJulian Elischer * however, that:
144b52f283SJulian Elischer * 1. Any and all reproductions of the source or object code must include the
154b52f283SJulian Elischer * copyright notice above and the following disclaimer of warranties; and
16585ff168SJulian Elischer * 2. No rights are granted, in any manner or form, to use Sandvine Inc.
174b52f283SJulian Elischer * trademarks, including the mark "SANDVINE" on advertising, endorsements,
184b52f283SJulian Elischer * or otherwise except as such appears in the above copyright notice or in
19585ff168SJulian Elischer * the software.
20585ff168SJulian Elischer *
21585ff168SJulian Elischer * THIS SOFTWARE IS BEING PROVIDED BY SANDVINE "AS IS", AND TO THE MAXIMUM
224b52f283SJulian Elischer * EXTENT PERMITTED BY LAW, SANDVINE MAKES NO REPRESENTATIONS OR WARRANTIES,
234b52f283SJulian Elischer * EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, INCLUDING WITHOUT LIMITATION,
244b52f283SJulian Elischer * ANY AND ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25585ff168SJulian Elischer * PURPOSE, OR NON-INFRINGEMENT. SANDVINE DOES NOT WARRANT, GUARANTEE, OR
26585ff168SJulian Elischer * MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE
27585ff168SJulian Elischer * USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY
28585ff168SJulian Elischer * OR OTHERWISE. IN NO EVENT SHALL SANDVINE BE LIABLE FOR ANY DAMAGES
29585ff168SJulian Elischer * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
304b52f283SJulian Elischer * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31585ff168SJulian Elischer * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32585ff168SJulian Elischer * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33585ff168SJulian Elischer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34585ff168SJulian Elischer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35585ff168SJulian Elischer * THIS SOFTWARE, EVEN IF SANDVINE IS ADVISED OF THE POSSIBILITY OF SUCH
36585ff168SJulian Elischer * DAMAGE.
37585ff168SJulian Elischer *
38d11f4f5dSEd Maste * Author: Dave Chapeskie
39585ff168SJulian Elischer */
40585ff168SJulian Elischer
4176bd5857SHartmut Brandt #include <sys/cdefs.h>
42585ff168SJulian Elischer /*
43585ff168SJulian Elischer * This node is used for high speed packet geneneration. It queues
44053359b7SPedro F. Giffuni * all data received on its 'input' hook and when told to start via
452b0ffc02SBosko Milekic * a control message it sends the packets out its 'output' hook. In
462b0ffc02SBosko Milekic * this way this node can be preloaded with a packet stream which it
472b0ffc02SBosko Milekic * can then send continuously as fast as possible.
48585ff168SJulian Elischer *
49585ff168SJulian Elischer * Currently it just copies the mbufs as required. It could do various
50585ff168SJulian Elischer * tricks to try and avoid this. Probably the best performance would
51585ff168SJulian Elischer * be achieved by modifying the appropriate drivers to be told to
52585ff168SJulian Elischer * self-re-enqueue packets (e.g. the if_bge driver could reuse the same
53585ff168SJulian Elischer * transmit descriptors) under control of this node; perhaps via some
542b0ffc02SBosko Milekic * flag in the mbuf or some such. The node could peek at an appropriate
55585ff168SJulian Elischer * ifnet flag to see if such support is available for the connected
56585ff168SJulian Elischer * interface.
57585ff168SJulian Elischer */
58585ff168SJulian Elischer
59585ff168SJulian Elischer #include <sys/param.h>
60585ff168SJulian Elischer #include <sys/systm.h>
61585ff168SJulian Elischer #include <sys/errno.h>
62585ff168SJulian Elischer #include <sys/kernel.h>
63585ff168SJulian Elischer #include <sys/malloc.h>
64585ff168SJulian Elischer #include <sys/mbuf.h>
65585ff168SJulian Elischer #include <sys/socket.h>
66d8f5d037SGleb Smirnoff #include <sys/syslog.h>
67585ff168SJulian Elischer #include <net/if.h>
68585ff168SJulian Elischer #include <net/if_var.h>
69*3d0d5b21SJustin Hibbits #include <net/if_private.h>
70585ff168SJulian Elischer #include <netgraph/ng_message.h>
71585ff168SJulian Elischer #include <netgraph/netgraph.h>
72585ff168SJulian Elischer #include <netgraph/ng_parse.h>
73585ff168SJulian Elischer #include <netgraph/ng_ether.h>
74585ff168SJulian Elischer #include <netgraph/ng_source.h>
75585ff168SJulian Elischer
76585ff168SJulian Elischer #define NG_SOURCE_INTR_TICKS 1
77585ff168SJulian Elischer #define NG_SOURCE_DRIVER_IFQ_MAXLEN (4*1024)
78585ff168SJulian Elischer
795f87dd69SEd Maste #define mtod_off(m,off,t) ((t)(mtod((m),caddr_t)+(off)))
805f87dd69SEd Maste
81585ff168SJulian Elischer /* Per node info */
82585ff168SJulian Elischer struct privdata {
83585ff168SJulian Elischer node_p node;
84d8f5d037SGleb Smirnoff hook_p input;
85d8f5d037SGleb Smirnoff hook_p output;
86585ff168SJulian Elischer struct ng_source_stats stats;
87ebc61c86SLutz Donnerhacke struct mbufq snd_queue; /* packets to send */
88577421ebSEd Maste struct mbuf *last_packet; /* last pkt in queue */
89585ff168SJulian Elischer struct ifnet *output_ifp;
9030bef41bSGleb Smirnoff struct callout intr_ch;
91d8f5d037SGleb Smirnoff uint64_t packets; /* packets to send */
92d8f5d037SGleb Smirnoff uint32_t queueOctets;
935f87dd69SEd Maste struct ng_source_embed_info embed_timestamp;
94577421ebSEd Maste struct ng_source_embed_cnt_info embed_counter[NG_SOURCE_COUNTERS];
95585ff168SJulian Elischer };
96585ff168SJulian Elischer typedef struct privdata *sc_p;
97585ff168SJulian Elischer
98585ff168SJulian Elischer /* Node flags */
99585ff168SJulian Elischer #define NG_SOURCE_ACTIVE (NGF_TYPE1)
100585ff168SJulian Elischer
101585ff168SJulian Elischer /* Netgraph methods */
102585ff168SJulian Elischer static ng_constructor_t ng_source_constructor;
103585ff168SJulian Elischer static ng_rcvmsg_t ng_source_rcvmsg;
104585ff168SJulian Elischer static ng_shutdown_t ng_source_rmnode;
105585ff168SJulian Elischer static ng_newhook_t ng_source_newhook;
106d8f5d037SGleb Smirnoff static ng_connect_t ng_source_connect;
107585ff168SJulian Elischer static ng_rcvdata_t ng_source_rcvdata;
108585ff168SJulian Elischer static ng_disconnect_t ng_source_disconnect;
109585ff168SJulian Elischer
110585ff168SJulian Elischer /* Other functions */
111a1adb510SHartmut Brandt static void ng_source_intr(node_p, hook_p, void *, int);
112585ff168SJulian Elischer static void ng_source_clr_data (sc_p);
113d8f5d037SGleb Smirnoff static int ng_source_start (sc_p, uint64_t);
114585ff168SJulian Elischer static void ng_source_stop (sc_p);
115585ff168SJulian Elischer static int ng_source_send (sc_p, int, int *);
116d8f5d037SGleb Smirnoff static int ng_source_store_output_ifp(sc_p, char *);
1175f87dd69SEd Maste static void ng_source_packet_mod(sc_p, struct mbuf *,
1185f87dd69SEd Maste int, int, caddr_t, int);
119577421ebSEd Maste static void ng_source_mod_counter(sc_p sc,
120577421ebSEd Maste struct ng_source_embed_cnt_info *cnt,
121577421ebSEd Maste struct mbuf *m, int increment);
1225f87dd69SEd Maste static int ng_source_dup_mod(sc_p, struct mbuf *,
1235f87dd69SEd Maste struct mbuf **);
124585ff168SJulian Elischer
125585ff168SJulian Elischer /* Parse type for timeval */
12676bd5857SHartmut Brandt static const struct ng_parse_struct_field ng_source_timeval_type_fields[] = {
127f8edc373SEugene Grosbein #ifdef __i386__
128f8edc373SEugene Grosbein { "tv_sec", &ng_parse_int32_type },
129f8edc373SEugene Grosbein #else
130d642b942SEugene Grosbein { "tv_sec", &ng_parse_int64_type },
131f8edc373SEugene Grosbein #endif
132f8edc373SEugene Grosbein #ifdef __LP64__
133d642b942SEugene Grosbein { "tv_usec", &ng_parse_int64_type },
134d642b942SEugene Grosbein #else
135585ff168SJulian Elischer { "tv_usec", &ng_parse_int32_type },
136d642b942SEugene Grosbein #endif
137585ff168SJulian Elischer { NULL }
138585ff168SJulian Elischer };
139585ff168SJulian Elischer const struct ng_parse_type ng_source_timeval_type = {
140585ff168SJulian Elischer &ng_parse_struct_type,
141585ff168SJulian Elischer &ng_source_timeval_type_fields
142585ff168SJulian Elischer };
143585ff168SJulian Elischer
144585ff168SJulian Elischer /* Parse type for struct ng_source_stats */
145585ff168SJulian Elischer static const struct ng_parse_struct_field ng_source_stats_type_fields[]
146585ff168SJulian Elischer = NG_SOURCE_STATS_TYPE_INFO;
147585ff168SJulian Elischer static const struct ng_parse_type ng_source_stats_type = {
148585ff168SJulian Elischer &ng_parse_struct_type,
149585ff168SJulian Elischer &ng_source_stats_type_fields
150585ff168SJulian Elischer };
151585ff168SJulian Elischer
1525f87dd69SEd Maste /* Parse type for struct ng_source_embed_info */
1535f87dd69SEd Maste static const struct ng_parse_struct_field ng_source_embed_type_fields[] =
1545f87dd69SEd Maste NG_SOURCE_EMBED_TYPE_INFO;
1555f87dd69SEd Maste static const struct ng_parse_type ng_source_embed_type = {
1565f87dd69SEd Maste &ng_parse_struct_type,
1575f87dd69SEd Maste &ng_source_embed_type_fields
1585f87dd69SEd Maste };
1595f87dd69SEd Maste
160577421ebSEd Maste /* Parse type for struct ng_source_embed_cnt_info */
161577421ebSEd Maste static const struct ng_parse_struct_field ng_source_embed_cnt_type_fields[] =
162577421ebSEd Maste NG_SOURCE_EMBED_CNT_TYPE_INFO;
163577421ebSEd Maste static const struct ng_parse_type ng_source_embed_cnt_type = {
164577421ebSEd Maste &ng_parse_struct_type,
165577421ebSEd Maste &ng_source_embed_cnt_type_fields
166577421ebSEd Maste };
167577421ebSEd Maste
168585ff168SJulian Elischer /* List of commands and how to convert arguments to/from ASCII */
169585ff168SJulian Elischer static const struct ng_cmdlist ng_source_cmds[] = {
170585ff168SJulian Elischer {
171585ff168SJulian Elischer NGM_SOURCE_COOKIE,
172585ff168SJulian Elischer NGM_SOURCE_GET_STATS,
173585ff168SJulian Elischer "getstats",
174585ff168SJulian Elischer NULL,
175585ff168SJulian Elischer &ng_source_stats_type
176585ff168SJulian Elischer },
177585ff168SJulian Elischer {
178585ff168SJulian Elischer NGM_SOURCE_COOKIE,
179585ff168SJulian Elischer NGM_SOURCE_CLR_STATS,
180585ff168SJulian Elischer "clrstats",
181585ff168SJulian Elischer NULL,
182585ff168SJulian Elischer NULL
183585ff168SJulian Elischer },
184585ff168SJulian Elischer {
185585ff168SJulian Elischer NGM_SOURCE_COOKIE,
186585ff168SJulian Elischer NGM_SOURCE_GETCLR_STATS,
187585ff168SJulian Elischer "getclrstats",
188585ff168SJulian Elischer NULL,
189585ff168SJulian Elischer &ng_source_stats_type
190585ff168SJulian Elischer },
191585ff168SJulian Elischer {
192585ff168SJulian Elischer NGM_SOURCE_COOKIE,
193585ff168SJulian Elischer NGM_SOURCE_START,
194585ff168SJulian Elischer "start",
195585ff168SJulian Elischer &ng_parse_uint64_type,
196585ff168SJulian Elischer NULL
197585ff168SJulian Elischer },
198585ff168SJulian Elischer {
199585ff168SJulian Elischer NGM_SOURCE_COOKIE,
200585ff168SJulian Elischer NGM_SOURCE_STOP,
201585ff168SJulian Elischer "stop",
202585ff168SJulian Elischer NULL,
203585ff168SJulian Elischer NULL
204585ff168SJulian Elischer },
205585ff168SJulian Elischer {
206585ff168SJulian Elischer NGM_SOURCE_COOKIE,
207585ff168SJulian Elischer NGM_SOURCE_CLR_DATA,
208585ff168SJulian Elischer "clrdata",
209585ff168SJulian Elischer NULL,
210585ff168SJulian Elischer NULL
211585ff168SJulian Elischer },
212f5d15522SHartmut Brandt {
213f5d15522SHartmut Brandt NGM_SOURCE_COOKIE,
214d8f5d037SGleb Smirnoff NGM_SOURCE_SETIFACE,
215d8f5d037SGleb Smirnoff "setiface",
216d8f5d037SGleb Smirnoff &ng_parse_string_type,
217f5d15522SHartmut Brandt NULL
218f5d15522SHartmut Brandt },
21972235857SGleb Smirnoff {
22072235857SGleb Smirnoff NGM_SOURCE_COOKIE,
22172235857SGleb Smirnoff NGM_SOURCE_SETPPS,
22272235857SGleb Smirnoff "setpps",
22372235857SGleb Smirnoff &ng_parse_uint32_type,
22472235857SGleb Smirnoff NULL
22572235857SGleb Smirnoff },
2265f87dd69SEd Maste {
2275f87dd69SEd Maste NGM_SOURCE_COOKIE,
2285f87dd69SEd Maste NGM_SOURCE_SET_TIMESTAMP,
2295f87dd69SEd Maste "settimestamp",
2305f87dd69SEd Maste &ng_source_embed_type,
2315f87dd69SEd Maste NULL
2325f87dd69SEd Maste },
2335f87dd69SEd Maste {
2345f87dd69SEd Maste NGM_SOURCE_COOKIE,
2355f87dd69SEd Maste NGM_SOURCE_GET_TIMESTAMP,
2365f87dd69SEd Maste "gettimestamp",
2375f87dd69SEd Maste NULL,
2385f87dd69SEd Maste &ng_source_embed_type
2395f87dd69SEd Maste },
240577421ebSEd Maste {
241577421ebSEd Maste NGM_SOURCE_COOKIE,
242577421ebSEd Maste NGM_SOURCE_SET_COUNTER,
243577421ebSEd Maste "setcounter",
244577421ebSEd Maste &ng_source_embed_cnt_type,
245577421ebSEd Maste NULL
246577421ebSEd Maste },
247577421ebSEd Maste {
248577421ebSEd Maste NGM_SOURCE_COOKIE,
249577421ebSEd Maste NGM_SOURCE_GET_COUNTER,
250577421ebSEd Maste "getcounter",
251577421ebSEd Maste &ng_parse_uint8_type,
252577421ebSEd Maste &ng_source_embed_cnt_type
253577421ebSEd Maste },
254585ff168SJulian Elischer { 0 }
255585ff168SJulian Elischer };
256585ff168SJulian Elischer
257585ff168SJulian Elischer /* Netgraph type descriptor */
258585ff168SJulian Elischer static struct ng_type ng_source_typestruct = {
259f8aae777SJulian Elischer .version = NG_ABI_VERSION,
260f8aae777SJulian Elischer .name = NG_SOURCE_NODE_TYPE,
261f8aae777SJulian Elischer .constructor = ng_source_constructor,
262f8aae777SJulian Elischer .rcvmsg = ng_source_rcvmsg,
263f8aae777SJulian Elischer .shutdown = ng_source_rmnode,
264f8aae777SJulian Elischer .newhook = ng_source_newhook,
265d8f5d037SGleb Smirnoff .connect = ng_source_connect,
266f8aae777SJulian Elischer .rcvdata = ng_source_rcvdata,
267f8aae777SJulian Elischer .disconnect = ng_source_disconnect,
268f8aae777SJulian Elischer .cmdlist = ng_source_cmds,
269585ff168SJulian Elischer };
270585ff168SJulian Elischer NETGRAPH_INIT(source, &ng_source_typestruct);
271585ff168SJulian Elischer
272d8f5d037SGleb Smirnoff static int ng_source_set_autosrc(sc_p, uint32_t);
2732cafef3eSHartmut Brandt
274585ff168SJulian Elischer /*
275585ff168SJulian Elischer * Node constructor
276585ff168SJulian Elischer */
277585ff168SJulian Elischer static int
ng_source_constructor(node_p node)2785968e29eSJulian Elischer ng_source_constructor(node_p node)
279585ff168SJulian Elischer {
280585ff168SJulian Elischer sc_p sc;
281585ff168SJulian Elischer
282674d86bfSGleb Smirnoff sc = malloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
283585ff168SJulian Elischer
2845968e29eSJulian Elischer NG_NODE_SET_PRIVATE(node, sc);
2855968e29eSJulian Elischer sc->node = node;
286ebc61c86SLutz Donnerhacke mbufq_init(&sc->snd_queue, 2048);
28730bef41bSGleb Smirnoff ng_callout_init(&sc->intr_ch);
28830bef41bSGleb Smirnoff
289585ff168SJulian Elischer return (0);
290585ff168SJulian Elischer }
291585ff168SJulian Elischer
292585ff168SJulian Elischer /*
293585ff168SJulian Elischer * Add a hook
294585ff168SJulian Elischer */
295585ff168SJulian Elischer static int
ng_source_newhook(node_p node,hook_p hook,const char * name)296585ff168SJulian Elischer ng_source_newhook(node_p node, hook_p hook, const char *name)
297585ff168SJulian Elischer {
298d8f5d037SGleb Smirnoff sc_p sc = NG_NODE_PRIVATE(node);
299585ff168SJulian Elischer
300585ff168SJulian Elischer if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) {
301d8f5d037SGleb Smirnoff sc->input = hook;
302585ff168SJulian Elischer } else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) {
303d8f5d037SGleb Smirnoff sc->output = hook;
304155d72c4SPedro F. Giffuni sc->output_ifp = NULL;
305585ff168SJulian Elischer bzero(&sc->stats, sizeof(sc->stats));
306585ff168SJulian Elischer } else
307585ff168SJulian Elischer return (EINVAL);
308d8f5d037SGleb Smirnoff
309d8f5d037SGleb Smirnoff return (0);
310d8f5d037SGleb Smirnoff }
311d8f5d037SGleb Smirnoff
312d8f5d037SGleb Smirnoff /*
313d8f5d037SGleb Smirnoff * Hook has been added
314d8f5d037SGleb Smirnoff */
315d8f5d037SGleb Smirnoff static int
ng_source_connect(hook_p hook)316d8f5d037SGleb Smirnoff ng_source_connect(hook_p hook)
317d8f5d037SGleb Smirnoff {
318d8f5d037SGleb Smirnoff sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
319d8f5d037SGleb Smirnoff struct ng_mesg *msg;
320d8f5d037SGleb Smirnoff int dummy_error = 0;
321d8f5d037SGleb Smirnoff
322d8f5d037SGleb Smirnoff /*
323d8f5d037SGleb Smirnoff * If this is "output" hook, then request information
324d8f5d037SGleb Smirnoff * from our downstream.
325d8f5d037SGleb Smirnoff */
326d8f5d037SGleb Smirnoff if (hook == sc->output) {
327d8f5d037SGleb Smirnoff NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFNAME,
328d8f5d037SGleb Smirnoff 0, M_NOWAIT);
329d8f5d037SGleb Smirnoff if (msg == NULL)
330d8f5d037SGleb Smirnoff return (ENOBUFS);
331d8f5d037SGleb Smirnoff
332d8f5d037SGleb Smirnoff /*
333d8f5d037SGleb Smirnoff * Our hook and peer hook have HK_INVALID flag set,
334d8f5d037SGleb Smirnoff * so we can't use NG_SEND_MSG_HOOK() macro here.
335d8f5d037SGleb Smirnoff */
336d8f5d037SGleb Smirnoff NG_SEND_MSG_ID(dummy_error, sc->node, msg,
337d8f5d037SGleb Smirnoff NG_NODE_ID(NG_PEER_NODE(sc->output)), NG_NODE_ID(sc->node));
338d8f5d037SGleb Smirnoff }
339d8f5d037SGleb Smirnoff
340585ff168SJulian Elischer return (0);
341585ff168SJulian Elischer }
342585ff168SJulian Elischer
343585ff168SJulian Elischer /*
344585ff168SJulian Elischer * Receive a control message
345585ff168SJulian Elischer */
346585ff168SJulian Elischer static int
ng_source_rcvmsg(node_p node,item_p item,hook_p lasthook)3475968e29eSJulian Elischer ng_source_rcvmsg(node_p node, item_p item, hook_p lasthook)
348585ff168SJulian Elischer {
349d8f5d037SGleb Smirnoff sc_p sc = NG_NODE_PRIVATE(node);
350d8f5d037SGleb Smirnoff struct ng_mesg *msg, *resp = NULL;
351585ff168SJulian Elischer int error = 0;
352585ff168SJulian Elischer
3535968e29eSJulian Elischer NGI_GET_MSG(item, msg);
354d8f5d037SGleb Smirnoff
355585ff168SJulian Elischer switch (msg->header.typecookie) {
356585ff168SJulian Elischer case NGM_SOURCE_COOKIE:
357b655e33dSJulian Elischer if (msg->header.flags & NGF_RESP) {
358b655e33dSJulian Elischer error = EINVAL;
359b655e33dSJulian Elischer break;
360b655e33dSJulian Elischer }
361585ff168SJulian Elischer switch (msg->header.cmd) {
362585ff168SJulian Elischer case NGM_SOURCE_GET_STATS:
363585ff168SJulian Elischer case NGM_SOURCE_CLR_STATS:
364585ff168SJulian Elischer case NGM_SOURCE_GETCLR_STATS:
365585ff168SJulian Elischer {
366585ff168SJulian Elischer struct ng_source_stats *stats;
367585ff168SJulian Elischer
368585ff168SJulian Elischer if (msg->header.cmd != NGM_SOURCE_CLR_STATS) {
369585ff168SJulian Elischer NG_MKRESPONSE(resp, msg,
370585ff168SJulian Elischer sizeof(*stats), M_NOWAIT);
371585ff168SJulian Elischer if (resp == NULL) {
372585ff168SJulian Elischer error = ENOMEM;
373585ff168SJulian Elischer goto done;
374585ff168SJulian Elischer }
375585ff168SJulian Elischer sc->stats.queueOctets = sc->queueOctets;
376ebc61c86SLutz Donnerhacke sc->stats.queueFrames = mbufq_len(&sc->snd_queue);
3775968e29eSJulian Elischer if ((sc->node->nd_flags & NG_SOURCE_ACTIVE)
378585ff168SJulian Elischer && !timevalisset(&sc->stats.endTime)) {
379585ff168SJulian Elischer getmicrotime(&sc->stats.elapsedTime);
380585ff168SJulian Elischer timevalsub(&sc->stats.elapsedTime,
381585ff168SJulian Elischer &sc->stats.startTime);
382585ff168SJulian Elischer }
3834b52f283SJulian Elischer stats = (struct ng_source_stats *)resp->data;
384585ff168SJulian Elischer bcopy(&sc->stats, stats, sizeof(* stats));
385585ff168SJulian Elischer }
386585ff168SJulian Elischer if (msg->header.cmd != NGM_SOURCE_GET_STATS)
387585ff168SJulian Elischer bzero(&sc->stats, sizeof(sc->stats));
388585ff168SJulian Elischer }
389585ff168SJulian Elischer break;
390585ff168SJulian Elischer case NGM_SOURCE_START:
391585ff168SJulian Elischer {
392d8f5d037SGleb Smirnoff uint64_t packets;
393f5d15522SHartmut Brandt
394d8f5d037SGleb Smirnoff if (msg->header.arglen != sizeof(uint64_t)) {
395d8f5d037SGleb Smirnoff error = EINVAL;
396f5d15522SHartmut Brandt break;
397d8f5d037SGleb Smirnoff }
398d8f5d037SGleb Smirnoff
399d8f5d037SGleb Smirnoff packets = *(uint64_t *)msg->data;
400d8f5d037SGleb Smirnoff
401d8f5d037SGleb Smirnoff error = ng_source_start(sc, packets);
402d8f5d037SGleb Smirnoff
403d8f5d037SGleb Smirnoff break;
404d8f5d037SGleb Smirnoff }
405585ff168SJulian Elischer case NGM_SOURCE_STOP:
406585ff168SJulian Elischer ng_source_stop(sc);
407585ff168SJulian Elischer break;
408585ff168SJulian Elischer case NGM_SOURCE_CLR_DATA:
409585ff168SJulian Elischer ng_source_clr_data(sc);
410585ff168SJulian Elischer break;
411d8f5d037SGleb Smirnoff case NGM_SOURCE_SETIFACE:
412d8f5d037SGleb Smirnoff {
413d8f5d037SGleb Smirnoff char *ifname = (char *)msg->data;
414d8f5d037SGleb Smirnoff
415d8f5d037SGleb Smirnoff if (msg->header.arglen < 2) {
416d8f5d037SGleb Smirnoff error = EINVAL;
417d8f5d037SGleb Smirnoff break;
418d8f5d037SGleb Smirnoff }
419d8f5d037SGleb Smirnoff
420d8f5d037SGleb Smirnoff ng_source_store_output_ifp(sc, ifname);
421d8f5d037SGleb Smirnoff break;
422d8f5d037SGleb Smirnoff }
42372235857SGleb Smirnoff case NGM_SOURCE_SETPPS:
42472235857SGleb Smirnoff {
42572235857SGleb Smirnoff uint32_t pps;
42672235857SGleb Smirnoff
42772235857SGleb Smirnoff if (msg->header.arglen != sizeof(uint32_t)) {
42872235857SGleb Smirnoff error = EINVAL;
42972235857SGleb Smirnoff break;
43072235857SGleb Smirnoff }
43172235857SGleb Smirnoff
43272235857SGleb Smirnoff pps = *(uint32_t *)msg->data;
43372235857SGleb Smirnoff
43472235857SGleb Smirnoff sc->stats.maxPps = pps;
43572235857SGleb Smirnoff
43672235857SGleb Smirnoff break;
43772235857SGleb Smirnoff }
4385f87dd69SEd Maste case NGM_SOURCE_SET_TIMESTAMP:
4395f87dd69SEd Maste {
4405f87dd69SEd Maste struct ng_source_embed_info *embed;
4415f87dd69SEd Maste
442ab2e868cSEd Maste if (msg->header.arglen != sizeof(*embed)) {
443ab2e868cSEd Maste error = EINVAL;
444ab2e868cSEd Maste goto done;
445ab2e868cSEd Maste }
4465f87dd69SEd Maste embed = (struct ng_source_embed_info *)msg->data;
4475f87dd69SEd Maste bcopy(embed, &sc->embed_timestamp, sizeof(*embed));
4485f87dd69SEd Maste
4495f87dd69SEd Maste break;
4505f87dd69SEd Maste }
4515f87dd69SEd Maste case NGM_SOURCE_GET_TIMESTAMP:
4525f87dd69SEd Maste {
4535f87dd69SEd Maste struct ng_source_embed_info *embed;
4545f87dd69SEd Maste
455eb1b1807SGleb Smirnoff NG_MKRESPONSE(resp, msg, sizeof(*embed), M_NOWAIT);
4565f87dd69SEd Maste if (resp == NULL) {
4575f87dd69SEd Maste error = ENOMEM;
4585f87dd69SEd Maste goto done;
4595f87dd69SEd Maste }
4605f87dd69SEd Maste embed = (struct ng_source_embed_info *)resp->data;
4615f87dd69SEd Maste bcopy(&sc->embed_timestamp, embed, sizeof(*embed));
4625f87dd69SEd Maste
4635f87dd69SEd Maste break;
4645f87dd69SEd Maste }
465577421ebSEd Maste case NGM_SOURCE_SET_COUNTER:
466577421ebSEd Maste {
467577421ebSEd Maste struct ng_source_embed_cnt_info *embed;
468577421ebSEd Maste
469ab2e868cSEd Maste if (msg->header.arglen != sizeof(*embed)) {
470ab2e868cSEd Maste error = EINVAL;
471ab2e868cSEd Maste goto done;
472ab2e868cSEd Maste }
473577421ebSEd Maste embed = (struct ng_source_embed_cnt_info *)msg->data;
474577421ebSEd Maste if (embed->index >= NG_SOURCE_COUNTERS ||
475577421ebSEd Maste !(embed->width == 1 || embed->width == 2 ||
476577421ebSEd Maste embed->width == 4)) {
477577421ebSEd Maste error = EINVAL;
478577421ebSEd Maste goto done;
479577421ebSEd Maste }
480577421ebSEd Maste bcopy(embed, &sc->embed_counter[embed->index],
481577421ebSEd Maste sizeof(*embed));
482577421ebSEd Maste
483577421ebSEd Maste break;
484577421ebSEd Maste }
485577421ebSEd Maste case NGM_SOURCE_GET_COUNTER:
486577421ebSEd Maste {
487577421ebSEd Maste uint8_t index = *(uint8_t *)msg->data;
488577421ebSEd Maste struct ng_source_embed_cnt_info *embed;
489577421ebSEd Maste
490577421ebSEd Maste if (index >= NG_SOURCE_COUNTERS) {
491577421ebSEd Maste error = EINVAL;
492577421ebSEd Maste goto done;
493577421ebSEd Maste }
494eb1b1807SGleb Smirnoff NG_MKRESPONSE(resp, msg, sizeof(*embed), M_NOWAIT);
495577421ebSEd Maste if (resp == NULL) {
496577421ebSEd Maste error = ENOMEM;
497577421ebSEd Maste goto done;
498577421ebSEd Maste }
499577421ebSEd Maste embed = (struct ng_source_embed_cnt_info *)resp->data;
500577421ebSEd Maste bcopy(&sc->embed_counter[index], embed, sizeof(*embed));
501577421ebSEd Maste
502577421ebSEd Maste break;
503577421ebSEd Maste }
504585ff168SJulian Elischer default:
505585ff168SJulian Elischer error = EINVAL;
506585ff168SJulian Elischer break;
507585ff168SJulian Elischer }
508585ff168SJulian Elischer break;
509b655e33dSJulian Elischer case NGM_ETHER_COOKIE:
510b655e33dSJulian Elischer if (!(msg->header.flags & NGF_RESP)) {
511b655e33dSJulian Elischer error = EINVAL;
512b655e33dSJulian Elischer break;
513b655e33dSJulian Elischer }
514b655e33dSJulian Elischer switch (msg->header.cmd) {
515d8f5d037SGleb Smirnoff case NGM_ETHER_GET_IFNAME:
516d8f5d037SGleb Smirnoff {
517d8f5d037SGleb Smirnoff char *ifname = (char *)msg->data;
518d8f5d037SGleb Smirnoff
519d8f5d037SGleb Smirnoff if (msg->header.arglen < 2) {
520d8f5d037SGleb Smirnoff error = EINVAL;
521b655e33dSJulian Elischer break;
522d8f5d037SGleb Smirnoff }
523d8f5d037SGleb Smirnoff
524d8f5d037SGleb Smirnoff if (ng_source_store_output_ifp(sc, ifname) == 0)
525d8f5d037SGleb Smirnoff ng_source_set_autosrc(sc, 0);
526d8f5d037SGleb Smirnoff break;
527d8f5d037SGleb Smirnoff }
528b655e33dSJulian Elischer default:
529b655e33dSJulian Elischer error = EINVAL;
530b655e33dSJulian Elischer }
531b655e33dSJulian Elischer break;
532585ff168SJulian Elischer default:
533585ff168SJulian Elischer error = EINVAL;
534585ff168SJulian Elischer break;
535585ff168SJulian Elischer }
536585ff168SJulian Elischer
537585ff168SJulian Elischer done:
538d8f5d037SGleb Smirnoff /* Take care of synchronous response, if any. */
5395968e29eSJulian Elischer NG_RESPOND_MSG(error, node, item, resp);
540d8f5d037SGleb Smirnoff /* Free the message and return. */
5415968e29eSJulian Elischer NG_FREE_MSG(msg);
542585ff168SJulian Elischer return (error);
543585ff168SJulian Elischer }
544585ff168SJulian Elischer
545585ff168SJulian Elischer /*
546585ff168SJulian Elischer * Receive data on a hook
547585ff168SJulian Elischer *
548585ff168SJulian Elischer * If data comes in the input hook, enqueue it on the send queue.
549585ff168SJulian Elischer * If data comes in the output hook, discard it.
550585ff168SJulian Elischer */
551585ff168SJulian Elischer static int
ng_source_rcvdata(hook_p hook,item_p item)5525968e29eSJulian Elischer ng_source_rcvdata(hook_p hook, item_p item)
553585ff168SJulian Elischer {
554d8f5d037SGleb Smirnoff sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
5555968e29eSJulian Elischer struct mbuf *m;
556d8f5d037SGleb Smirnoff int error = 0;
557585ff168SJulian Elischer
5585968e29eSJulian Elischer NGI_GET_M(item, m);
5595968e29eSJulian Elischer NG_FREE_ITEM(item);
560585ff168SJulian Elischer
561585ff168SJulian Elischer /* Which hook? */
562d8f5d037SGleb Smirnoff if (hook == sc->output) {
563585ff168SJulian Elischer /* discard */
5645968e29eSJulian Elischer NG_FREE_M(m);
565585ff168SJulian Elischer return (error);
566585ff168SJulian Elischer }
567d8f5d037SGleb Smirnoff KASSERT(hook == sc->input, ("%s: no hook!", __func__));
568585ff168SJulian Elischer
569d7dd28bbSLutz Donnerhacke /* Enqueue packet if the queue isn't full. */
570ebc61c86SLutz Donnerhacke error = mbufq_enqueue(&sc->snd_queue, m);
571ebc61c86SLutz Donnerhacke if (error) {
572d7dd28bbSLutz Donnerhacke NG_FREE_M(m);
573ebc61c86SLutz Donnerhacke return (error);
574d7dd28bbSLutz Donnerhacke }
575585ff168SJulian Elischer sc->queueOctets += m->m_pkthdr.len;
576577421ebSEd Maste sc->last_packet = m;
577585ff168SJulian Elischer
578585ff168SJulian Elischer return (0);
579585ff168SJulian Elischer }
580585ff168SJulian Elischer
581585ff168SJulian Elischer /*
582585ff168SJulian Elischer * Shutdown processing
583585ff168SJulian Elischer */
584585ff168SJulian Elischer static int
ng_source_rmnode(node_p node)585585ff168SJulian Elischer ng_source_rmnode(node_p node)
586585ff168SJulian Elischer {
587d8f5d037SGleb Smirnoff sc_p sc = NG_NODE_PRIVATE(node);
588585ff168SJulian Elischer
589585ff168SJulian Elischer ng_source_stop(sc);
590585ff168SJulian Elischer ng_source_clr_data(sc);
5915968e29eSJulian Elischer NG_NODE_SET_PRIVATE(node, NULL);
5925968e29eSJulian Elischer NG_NODE_UNREF(node);
59376bd5857SHartmut Brandt free(sc, M_NETGRAPH);
594d8f5d037SGleb Smirnoff
595585ff168SJulian Elischer return (0);
596585ff168SJulian Elischer }
597585ff168SJulian Elischer
598585ff168SJulian Elischer /*
599585ff168SJulian Elischer * Hook disconnection
600585ff168SJulian Elischer */
601585ff168SJulian Elischer static int
ng_source_disconnect(hook_p hook)602585ff168SJulian Elischer ng_source_disconnect(hook_p hook)
603585ff168SJulian Elischer {
6044b52f283SJulian Elischer sc_p sc;
605585ff168SJulian Elischer
6065968e29eSJulian Elischer sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
60776bd5857SHartmut Brandt KASSERT(sc != NULL, ("%s: null node private", __func__));
608d8f5d037SGleb Smirnoff if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hook == sc->output)
6095968e29eSJulian Elischer ng_rmnode_self(NG_HOOK_NODE(hook));
610585ff168SJulian Elischer return (0);
611585ff168SJulian Elischer }
612585ff168SJulian Elischer
613585ff168SJulian Elischer /*
6146bccea7cSRebecca Cran * Set sc->output_ifp to point to the struct ifnet of the interface
615b655e33dSJulian Elischer * reached via our output hook.
616b655e33dSJulian Elischer */
617b655e33dSJulian Elischer static int
ng_source_store_output_ifp(sc_p sc,char * ifname)618d8f5d037SGleb Smirnoff ng_source_store_output_ifp(sc_p sc, char *ifname)
619b655e33dSJulian Elischer {
620b655e33dSJulian Elischer struct ifnet *ifp;
621b655e33dSJulian Elischer
622d8f5d037SGleb Smirnoff ifp = ifunit(ifname);
623585ff168SJulian Elischer
624585ff168SJulian Elischer if (ifp == NULL) {
6253ac12c59SMarko Zec printf("%s: can't find interface %s\n", __func__, ifname);
626585ff168SJulian Elischer return (EINVAL);
627585ff168SJulian Elischer }
628585ff168SJulian Elischer sc->output_ifp = ifp;
629585ff168SJulian Elischer
630585ff168SJulian Elischer #if 1
631585ff168SJulian Elischer /* XXX mucking with a drivers ifqueue size is ugly but we need it
632585ff168SJulian Elischer * to queue a lot of packets to get close to line rate on a gigabit
633585ff168SJulian Elischer * interface with small packets.
634585ff168SJulian Elischer * XXX we should restore the original value at stop or disconnect
635585ff168SJulian Elischer */
63676bd5857SHartmut Brandt if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN) {
637585ff168SJulian Elischer printf("ng_source: changing ifq_maxlen from %d to %d\n",
6384b52f283SJulian Elischer ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN);
639585ff168SJulian Elischer ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN;
640585ff168SJulian Elischer }
641585ff168SJulian Elischer #endif
642585ff168SJulian Elischer return (0);
643585ff168SJulian Elischer }
644585ff168SJulian Elischer
645585ff168SJulian Elischer /*
646585ff168SJulian Elischer * Set the attached ethernet node's ethernet source address override flag.
647585ff168SJulian Elischer */
648585ff168SJulian Elischer static int
ng_source_set_autosrc(sc_p sc,uint32_t flag)649d8f5d037SGleb Smirnoff ng_source_set_autosrc(sc_p sc, uint32_t flag)
650585ff168SJulian Elischer {
651585ff168SJulian Elischer struct ng_mesg *msg;
652585ff168SJulian Elischer int error = 0;
653585ff168SJulian Elischer
654585ff168SJulian Elischer NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC,
655d8f5d037SGleb Smirnoff sizeof (uint32_t), M_NOWAIT);
656585ff168SJulian Elischer if (msg == NULL)
657585ff168SJulian Elischer return(ENOBUFS);
658585ff168SJulian Elischer
659d8f5d037SGleb Smirnoff *(uint32_t *)msg->data = flag;
660d8f5d037SGleb Smirnoff NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output, 0);
661585ff168SJulian Elischer return (error);
662585ff168SJulian Elischer }
663585ff168SJulian Elischer
664585ff168SJulian Elischer /*
665585ff168SJulian Elischer * Clear out the data we've queued
666585ff168SJulian Elischer */
667585ff168SJulian Elischer static void
ng_source_clr_data(sc_p sc)668585ff168SJulian Elischer ng_source_clr_data (sc_p sc)
669585ff168SJulian Elischer {
670585ff168SJulian Elischer struct mbuf *m;
671585ff168SJulian Elischer
672585ff168SJulian Elischer for (;;) {
673ebc61c86SLutz Donnerhacke m = mbufq_dequeue(&sc->snd_queue);
674585ff168SJulian Elischer if (m == NULL)
675585ff168SJulian Elischer break;
676585ff168SJulian Elischer NG_FREE_M(m);
677585ff168SJulian Elischer }
678585ff168SJulian Elischer sc->queueOctets = 0;
679ab2e868cSEd Maste sc->last_packet = NULL;
680585ff168SJulian Elischer }
681585ff168SJulian Elischer
682585ff168SJulian Elischer /*
683585ff168SJulian Elischer * Start sending queued data out the output hook
684585ff168SJulian Elischer */
685d8f5d037SGleb Smirnoff static int
ng_source_start(sc_p sc,uint64_t packets)686d8f5d037SGleb Smirnoff ng_source_start(sc_p sc, uint64_t packets)
687585ff168SJulian Elischer {
68875e7ef74SLutz Donnerhacke if (sc->output_ifp == NULL && sc->stats.maxPps == 0) {
68975e7ef74SLutz Donnerhacke printf("ng_source: start without iface or pps configured\n");
690d8f5d037SGleb Smirnoff return (ENXIO);
691d8f5d037SGleb Smirnoff }
692d8f5d037SGleb Smirnoff
693d8f5d037SGleb Smirnoff if (sc->node->nd_flags & NG_SOURCE_ACTIVE)
694d8f5d037SGleb Smirnoff return (EBUSY);
695d8f5d037SGleb Smirnoff
696d8f5d037SGleb Smirnoff sc->node->nd_flags |= NG_SOURCE_ACTIVE;
697d8f5d037SGleb Smirnoff
698d8f5d037SGleb Smirnoff sc->packets = packets;
699d8f5d037SGleb Smirnoff timevalclear(&sc->stats.elapsedTime);
700d8f5d037SGleb Smirnoff timevalclear(&sc->stats.endTime);
701d8f5d037SGleb Smirnoff getmicrotime(&sc->stats.startTime);
70272235857SGleb Smirnoff getmicrotime(&sc->stats.lastTime);
703d8f5d037SGleb Smirnoff ng_callout(&sc->intr_ch, sc->node, NULL, 0,
704d8f5d037SGleb Smirnoff ng_source_intr, sc, 0);
705d8f5d037SGleb Smirnoff
706d8f5d037SGleb Smirnoff return (0);
707585ff168SJulian Elischer }
708585ff168SJulian Elischer
709585ff168SJulian Elischer /*
710585ff168SJulian Elischer * Stop sending queued data out the output hook
711585ff168SJulian Elischer */
712585ff168SJulian Elischer static void
ng_source_stop(sc_p sc)713585ff168SJulian Elischer ng_source_stop(sc_p sc)
714585ff168SJulian Elischer {
715f9d9e1b4SGleb Smirnoff ng_uncallout(&sc->intr_ch, sc->node);
7165968e29eSJulian Elischer sc->node->nd_flags &= ~NG_SOURCE_ACTIVE;
717585ff168SJulian Elischer getmicrotime(&sc->stats.endTime);
718585ff168SJulian Elischer sc->stats.elapsedTime = sc->stats.endTime;
719585ff168SJulian Elischer timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime);
720585ff168SJulian Elischer }
721585ff168SJulian Elischer
722585ff168SJulian Elischer /*
723585ff168SJulian Elischer * While active called every NG_SOURCE_INTR_TICKS ticks.
724585ff168SJulian Elischer * Sends as many packets as the interface connected to our
725585ff168SJulian Elischer * output hook is able to enqueue.
726585ff168SJulian Elischer */
727585ff168SJulian Elischer static void
ng_source_intr(node_p node,hook_p hook,void * arg1,int arg2)728a1adb510SHartmut Brandt ng_source_intr(node_p node, hook_p hook, void *arg1, int arg2)
729585ff168SJulian Elischer {
730a1adb510SHartmut Brandt sc_p sc = (sc_p)arg1;
731585ff168SJulian Elischer struct ifqueue *ifq;
732585ff168SJulian Elischer int packets;
733585ff168SJulian Elischer
73476bd5857SHartmut Brandt KASSERT(sc != NULL, ("%s: null node private", __func__));
735585ff168SJulian Elischer
736d8f5d037SGleb Smirnoff if (sc->packets == 0 || sc->output == NULL
7375968e29eSJulian Elischer || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) {
738585ff168SJulian Elischer ng_source_stop(sc);
739585ff168SJulian Elischer return;
740585ff168SJulian Elischer }
741585ff168SJulian Elischer
742f5d15522SHartmut Brandt if (sc->output_ifp != NULL) {
7430572dfacSRuslan Ermilov ifq = (struct ifqueue *)&sc->output_ifp->if_snd;
744585ff168SJulian Elischer packets = ifq->ifq_maxlen - ifq->ifq_len;
745f5d15522SHartmut Brandt } else
746ebc61c86SLutz Donnerhacke packets = mbufq_len(&sc->snd_queue);
747f5d15522SHartmut Brandt
74872235857SGleb Smirnoff if (sc->stats.maxPps != 0) {
74972235857SGleb Smirnoff struct timeval now, elapsed;
75072235857SGleb Smirnoff uint64_t usec;
75172235857SGleb Smirnoff int maxpkt;
75272235857SGleb Smirnoff
75372235857SGleb Smirnoff getmicrotime(&now);
75472235857SGleb Smirnoff elapsed = now;
75572235857SGleb Smirnoff timevalsub(&elapsed, &sc->stats.lastTime);
75672235857SGleb Smirnoff usec = elapsed.tv_sec * 1000000 + elapsed.tv_usec;
75772235857SGleb Smirnoff maxpkt = (uint64_t)sc->stats.maxPps * usec / 1000000;
75872235857SGleb Smirnoff sc->stats.lastTime = now;
75972235857SGleb Smirnoff if (packets > maxpkt)
76072235857SGleb Smirnoff packets = maxpkt;
76172235857SGleb Smirnoff }
76272235857SGleb Smirnoff
763585ff168SJulian Elischer ng_source_send(sc, packets, NULL);
764a1adb510SHartmut Brandt if (sc->packets == 0)
765585ff168SJulian Elischer ng_source_stop(sc);
766a1adb510SHartmut Brandt else
767f9d9e1b4SGleb Smirnoff ng_callout(&sc->intr_ch, node, NULL, NG_SOURCE_INTR_TICKS,
768d312eaf5SGleb Smirnoff ng_source_intr, sc, 0);
769585ff168SJulian Elischer }
770585ff168SJulian Elischer
771585ff168SJulian Elischer /*
772205aefa3SGleb Smirnoff * Send packets out our output hook.
773585ff168SJulian Elischer */
774585ff168SJulian Elischer static int
ng_source_send(sc_p sc,int tosend,int * sent_p)775585ff168SJulian Elischer ng_source_send(sc_p sc, int tosend, int *sent_p)
776585ff168SJulian Elischer {
777585ff168SJulian Elischer struct mbuf *m, *m2;
778205aefa3SGleb Smirnoff int sent;
779585ff168SJulian Elischer int error = 0;
780585ff168SJulian Elischer
78176bd5857SHartmut Brandt KASSERT(tosend >= 0, ("%s: negative tosend param", __func__));
7825968e29eSJulian Elischer KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE,
78376bd5857SHartmut Brandt ("%s: inactive node", __func__));
784585ff168SJulian Elischer
785d8f5d037SGleb Smirnoff if ((uint64_t)tosend > sc->packets)
786585ff168SJulian Elischer tosend = sc->packets;
787585ff168SJulian Elischer
788205aefa3SGleb Smirnoff /* Go through the queue sending packets one by one. */
789585ff168SJulian Elischer for (sent = 0; error == 0 && sent < tosend; ++sent) {
790ebc61c86SLutz Donnerhacke m = mbufq_dequeue(&sc->snd_queue);
791585ff168SJulian Elischer if (m == NULL)
792585ff168SJulian Elischer break;
793585ff168SJulian Elischer
7945f87dd69SEd Maste /* Duplicate and modify the packet. */
7955f87dd69SEd Maste error = ng_source_dup_mod(sc, m, &m2);
7965f87dd69SEd Maste if (error) {
7975f87dd69SEd Maste if (error == ENOBUFS)
798ebc61c86SLutz Donnerhacke mbufq_prepend(&sc->snd_queue, m);
7995f87dd69SEd Maste else
800ebc61c86SLutz Donnerhacke (void)mbufq_enqueue(&sc->snd_queue, m);
801585ff168SJulian Elischer break;
802585ff168SJulian Elischer }
803585ff168SJulian Elischer
804ebc61c86SLutz Donnerhacke /*
805ebc61c86SLutz Donnerhacke * Re-enqueue the original packet for us. The queue
806ebc61c86SLutz Donnerhacke * has a free slot, because we dequeued the packet
807ebc61c86SLutz Donnerhacke * above and this callout function runs under WRITER
808ebc61c86SLutz Donnerhacke * lock.
809ebc61c86SLutz Donnerhacke */
810ebc61c86SLutz Donnerhacke error = mbufq_enqueue(&sc->snd_queue, m);
811ebc61c86SLutz Donnerhacke KASSERT(error == 0, ("%s: re-enqueue packet failed", __func__));
812585ff168SJulian Elischer
813585ff168SJulian Elischer sc->stats.outFrames++;
814585ff168SJulian Elischer sc->stats.outOctets += m2->m_pkthdr.len;
815d8f5d037SGleb Smirnoff NG_SEND_DATA_ONLY(error, sc->output, m2);
816a1adb510SHartmut Brandt if (error)
817205aefa3SGleb Smirnoff break;
818585ff168SJulian Elischer }
819585ff168SJulian Elischer
820585ff168SJulian Elischer sc->packets -= sent;
821585ff168SJulian Elischer if (sent_p != NULL)
822585ff168SJulian Elischer *sent_p = sent;
823585ff168SJulian Elischer return (error);
824585ff168SJulian Elischer }
8255f87dd69SEd Maste
8265f87dd69SEd Maste /*
8275f87dd69SEd Maste * Modify packet in 'm' by changing 'len' bytes starting at 'offset'
8285f87dd69SEd Maste * to data in 'cp'.
8295f87dd69SEd Maste *
8305f87dd69SEd Maste * The packet data in 'm' must be in a contiguous buffer in a single mbuf.
8315f87dd69SEd Maste */
8325f87dd69SEd Maste static void
ng_source_packet_mod(sc_p sc,struct mbuf * m,int offset,int len,caddr_t cp,int flags)8335f87dd69SEd Maste ng_source_packet_mod(sc_p sc, struct mbuf *m, int offset, int len, caddr_t cp,
8345f87dd69SEd Maste int flags)
8355f87dd69SEd Maste {
8365f87dd69SEd Maste if (len == 0)
8375f87dd69SEd Maste return;
8385f87dd69SEd Maste
8395f87dd69SEd Maste /* Can't modify beyond end of packet. */
8405f87dd69SEd Maste /* TODO: Pad packet for this case. */
8415f87dd69SEd Maste if (offset + len > m->m_len)
8425f87dd69SEd Maste return;
8435f87dd69SEd Maste
8445f87dd69SEd Maste bcopy(cp, mtod_off(m, offset, caddr_t), len);
8455f87dd69SEd Maste }
8465f87dd69SEd Maste
847577421ebSEd Maste static void
ng_source_mod_counter(sc_p sc,struct ng_source_embed_cnt_info * cnt,struct mbuf * m,int increment)848577421ebSEd Maste ng_source_mod_counter(sc_p sc, struct ng_source_embed_cnt_info *cnt,
849577421ebSEd Maste struct mbuf *m, int increment)
850577421ebSEd Maste {
851577421ebSEd Maste caddr_t cp;
852577421ebSEd Maste uint32_t val;
853577421ebSEd Maste
854577421ebSEd Maste val = htonl(cnt->next_val);
855577421ebSEd Maste cp = (caddr_t)&val + sizeof(val) - cnt->width;
856577421ebSEd Maste ng_source_packet_mod(sc, m, cnt->offset, cnt->width, cp, cnt->flags);
857577421ebSEd Maste
858577421ebSEd Maste if (increment) {
859577421ebSEd Maste cnt->next_val += increment;
860577421ebSEd Maste
861577421ebSEd Maste if (increment > 0 && cnt->next_val > cnt->max_val) {
862577421ebSEd Maste cnt->next_val = cnt->min_val - 1 +
863577421ebSEd Maste (cnt->next_val - cnt->max_val);
864577421ebSEd Maste if (cnt->next_val > cnt->max_val)
865577421ebSEd Maste cnt->next_val = cnt->max_val;
866577421ebSEd Maste } else if (increment < 0 && cnt->next_val < cnt->min_val) {
867577421ebSEd Maste cnt->next_val = cnt->max_val + 1 +
868577421ebSEd Maste (cnt->next_val - cnt->min_val);
869577421ebSEd Maste if (cnt->next_val < cnt->min_val)
870577421ebSEd Maste cnt->next_val = cnt->max_val;
871577421ebSEd Maste }
872577421ebSEd Maste }
873577421ebSEd Maste }
874577421ebSEd Maste
8755f87dd69SEd Maste static int
ng_source_dup_mod(sc_p sc,struct mbuf * m0,struct mbuf ** m_ptr)8765f87dd69SEd Maste ng_source_dup_mod(sc_p sc, struct mbuf *m0, struct mbuf **m_ptr)
8775f87dd69SEd Maste {
8785f87dd69SEd Maste struct mbuf *m;
879577421ebSEd Maste struct ng_source_embed_cnt_info *cnt;
8805f87dd69SEd Maste struct ng_source_embed_info *ts;
8815f87dd69SEd Maste int modify;
8825f87dd69SEd Maste int error = 0;
883577421ebSEd Maste int i, increment;
8845f87dd69SEd Maste
8855f87dd69SEd Maste /* Are we going to modify packets? */
8865f87dd69SEd Maste modify = sc->embed_timestamp.flags & NGM_SOURCE_EMBED_ENABLE;
887577421ebSEd Maste for (i = 0; !modify && i < NG_SOURCE_COUNTERS; ++i)
888577421ebSEd Maste modify = sc->embed_counter[i].flags & NGM_SOURCE_EMBED_ENABLE;
8895f87dd69SEd Maste
8905f87dd69SEd Maste /* Duplicate the packet. */
8915f87dd69SEd Maste if (modify)
892eb1b1807SGleb Smirnoff m = m_dup(m0, M_NOWAIT);
8935f87dd69SEd Maste else
894eb1b1807SGleb Smirnoff m = m_copypacket(m0, M_NOWAIT);
8955f87dd69SEd Maste if (m == NULL) {
8965f87dd69SEd Maste error = ENOBUFS;
8975f87dd69SEd Maste goto done;
8985f87dd69SEd Maste }
8995f87dd69SEd Maste *m_ptr = m;
9005f87dd69SEd Maste
9015f87dd69SEd Maste if (!modify)
9025f87dd69SEd Maste goto done;
9035f87dd69SEd Maste
9045f87dd69SEd Maste /* Modify the copied packet for sending. */
9055f87dd69SEd Maste KASSERT(M_WRITABLE(m), ("%s: packet not writable", __func__));
9065f87dd69SEd Maste
907577421ebSEd Maste for (i = 0; i < NG_SOURCE_COUNTERS; ++i) {
908577421ebSEd Maste cnt = &sc->embed_counter[i];
909577421ebSEd Maste if (cnt->flags & NGM_SOURCE_EMBED_ENABLE) {
910577421ebSEd Maste if ((cnt->flags & NGM_SOURCE_INC_CNT_PER_LIST) == 0 ||
911577421ebSEd Maste sc->last_packet == m0)
912577421ebSEd Maste increment = cnt->increment;
913577421ebSEd Maste else
914577421ebSEd Maste increment = 0;
915577421ebSEd Maste ng_source_mod_counter(sc, cnt, m, increment);
916577421ebSEd Maste }
917577421ebSEd Maste }
918577421ebSEd Maste
9195f87dd69SEd Maste ts = &sc->embed_timestamp;
9205f87dd69SEd Maste if (ts->flags & NGM_SOURCE_EMBED_ENABLE) {
9215f87dd69SEd Maste struct timeval now;
9225f87dd69SEd Maste getmicrotime(&now);
9235f87dd69SEd Maste now.tv_sec = htonl(now.tv_sec);
9245f87dd69SEd Maste now.tv_usec = htonl(now.tv_usec);
9255f87dd69SEd Maste ng_source_packet_mod(sc, m, ts->offset, sizeof (now),
9265f87dd69SEd Maste (caddr_t)&now, ts->flags);
9275f87dd69SEd Maste }
9285f87dd69SEd Maste
9295f87dd69SEd Maste done:
9305f87dd69SEd Maste return(error);
9315f87dd69SEd Maste }
932