ng_source.c revision 343022
1106266Sjulian/*
2106266Sjulian * ng_source.c
3139823Simp */
4139823Simp
5139823Simp/*-
6144674Sglebius * Copyright (c) 2005 Gleb Smirnoff <glebius@FreeBSD.org>
7106266Sjulian * Copyright 2002 Sandvine Inc.
8106266Sjulian * All rights reserved.
9106266Sjulian *
10106266Sjulian * Subject to the following obligations and disclaimer of warranty, use and
11106266Sjulian * redistribution of this software, in source or object code forms, with or
12106319Sjulian * without modifications are expressly permitted by Sandvine Inc.; provided,
13106266Sjulian * however, that:
14106319Sjulian * 1. Any and all reproductions of the source or object code must include the
15106319Sjulian *    copyright notice above and the following disclaimer of warranties; and
16106266Sjulian * 2. No rights are granted, in any manner or form, to use Sandvine Inc.
17106319Sjulian *    trademarks, including the mark "SANDVINE" on advertising, endorsements,
18106319Sjulian *    or otherwise except as such appears in the above copyright notice or in
19106266Sjulian *    the software.
20106266Sjulian *
21106266Sjulian * THIS SOFTWARE IS BEING PROVIDED BY SANDVINE "AS IS", AND TO THE MAXIMUM
22106319Sjulian * EXTENT PERMITTED BY LAW, SANDVINE MAKES NO REPRESENTATIONS OR WARRANTIES,
23106319Sjulian * EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, INCLUDING WITHOUT LIMITATION,
24106319Sjulian * ANY AND ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25106266Sjulian * PURPOSE, OR NON-INFRINGEMENT.  SANDVINE DOES NOT WARRANT, GUARANTEE, OR
26106266Sjulian * MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE
27106266Sjulian * USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY
28106266Sjulian * OR OTHERWISE.  IN NO EVENT SHALL SANDVINE BE LIABLE FOR ANY DAMAGES
29106266Sjulian * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30106319Sjulian * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31106266Sjulian * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32106266Sjulian * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33106266Sjulian * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34106266Sjulian * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35106266Sjulian * THIS SOFTWARE, EVEN IF SANDVINE IS ADVISED OF THE POSSIBILITY OF SUCH
36106266Sjulian * DAMAGE.
37106266Sjulian *
38209728Semaste * Author: Dave Chapeskie
39106266Sjulian */
40106266Sjulian
41125077Sharti#include <sys/cdefs.h>
42125077Sharti__FBSDID("$FreeBSD: stable/11/sys/netgraph/ng_source.c 343022 2019-01-14 09:57:36Z eugen $");
43125077Sharti
44106266Sjulian/*
45106266Sjulian * This node is used for high speed packet geneneration.  It queues
46298813Spfg * all data received on its 'input' hook and when told to start via
47143387Sbmilekic * a control message it sends the packets out its 'output' hook.  In
48143387Sbmilekic * this way this node can be preloaded with a packet stream which it
49143387Sbmilekic * can then send continuously as fast as possible.
50106266Sjulian *
51106266Sjulian * Currently it just copies the mbufs as required.  It could do various
52106266Sjulian * tricks to try and avoid this.  Probably the best performance would
53106266Sjulian * be achieved by modifying the appropriate drivers to be told to
54106266Sjulian * self-re-enqueue packets (e.g. the if_bge driver could reuse the same
55106266Sjulian * transmit descriptors) under control of this node; perhaps via some
56143387Sbmilekic * flag in the mbuf or some such.  The node could peek at an appropriate
57106266Sjulian * ifnet flag to see if such support is available for the connected
58106266Sjulian * interface.
59106266Sjulian */
60106266Sjulian
61106266Sjulian#include <sys/param.h>
62106266Sjulian#include <sys/systm.h>
63106266Sjulian#include <sys/errno.h>
64106266Sjulian#include <sys/kernel.h>
65106266Sjulian#include <sys/malloc.h>
66106266Sjulian#include <sys/mbuf.h>
67106266Sjulian#include <sys/socket.h>
68144674Sglebius#include <sys/syslog.h>
69106266Sjulian#include <net/if.h>
70106266Sjulian#include <net/if_var.h>
71106266Sjulian#include <netgraph/ng_message.h>
72106266Sjulian#include <netgraph/netgraph.h>
73106266Sjulian#include <netgraph/ng_parse.h>
74106266Sjulian#include <netgraph/ng_ether.h>
75106266Sjulian#include <netgraph/ng_source.h>
76106266Sjulian
77106266Sjulian#define NG_SOURCE_INTR_TICKS		1
78106266Sjulian#define NG_SOURCE_DRIVER_IFQ_MAXLEN	(4*1024)
79106266Sjulian
80167156Semaste#define	mtod_off(m,off,t)	((t)(mtod((m),caddr_t)+(off)))
81167156Semaste
82106266Sjulian/* Per node info */
83106266Sjulianstruct privdata {
84106266Sjulian	node_p				node;
85144674Sglebius	hook_p				input;
86144674Sglebius	hook_p				output;
87106266Sjulian	struct ng_source_stats		stats;
88106319Sjulian	struct ifqueue			snd_queue;	/* packets to send */
89167160Semaste	struct mbuf			*last_packet;	/* last pkt in queue */
90106266Sjulian	struct ifnet			*output_ifp;
91137138Sglebius	struct callout			intr_ch;
92144674Sglebius	uint64_t			packets;	/* packets to send */
93144674Sglebius	uint32_t			queueOctets;
94167156Semaste	struct ng_source_embed_info	embed_timestamp;
95167160Semaste	struct ng_source_embed_cnt_info	embed_counter[NG_SOURCE_COUNTERS];
96106266Sjulian};
97106266Sjuliantypedef struct privdata *sc_p;
98106266Sjulian
99106266Sjulian/* Node flags */
100106266Sjulian#define NG_SOURCE_ACTIVE	(NGF_TYPE1)
101106266Sjulian
102106266Sjulian/* Netgraph methods */
103106266Sjulianstatic ng_constructor_t	ng_source_constructor;
104106266Sjulianstatic ng_rcvmsg_t	ng_source_rcvmsg;
105106266Sjulianstatic ng_shutdown_t	ng_source_rmnode;
106106266Sjulianstatic ng_newhook_t	ng_source_newhook;
107144674Sglebiusstatic ng_connect_t	ng_source_connect;
108106266Sjulianstatic ng_rcvdata_t	ng_source_rcvdata;
109106266Sjulianstatic ng_disconnect_t	ng_source_disconnect;
110106266Sjulian
111106266Sjulian/* Other functions */
112125243Shartistatic void		ng_source_intr(node_p, hook_p, void *, int);
113106266Sjulianstatic void		ng_source_clr_data (sc_p);
114144674Sglebiusstatic int		ng_source_start (sc_p, uint64_t);
115106266Sjulianstatic void		ng_source_stop (sc_p);
116106266Sjulianstatic int		ng_source_send (sc_p, int, int *);
117144674Sglebiusstatic int		ng_source_store_output_ifp(sc_p, char *);
118167156Semastestatic void		ng_source_packet_mod(sc_p, struct mbuf *,
119167156Semaste			    int, int, caddr_t, int);
120167160Semastestatic void		ng_source_mod_counter(sc_p sc,
121167160Semaste			    struct ng_source_embed_cnt_info *cnt,
122167160Semaste			    struct mbuf *m, int increment);
123167156Semastestatic int		ng_source_dup_mod(sc_p, struct mbuf *,
124167156Semaste			    struct mbuf **);
125106266Sjulian
126106266Sjulian/* Parse type for timeval */
127125077Shartistatic const struct ng_parse_struct_field ng_source_timeval_type_fields[] = {
128343022Seugen#ifdef __i386__
129106266Sjulian	{ "tv_sec",		&ng_parse_int32_type	},
130343022Seugen#else
131343022Seugen	{ "tv_sec",		&ng_parse_int64_type	},
132343022Seugen#endif
133343022Seugen#ifdef __LP64__
134343022Seugen	{ "tv_usec",		&ng_parse_int64_type	},
135343022Seugen#else
136106266Sjulian	{ "tv_usec",		&ng_parse_int32_type	},
137343022Seugen#endif
138106266Sjulian	{ NULL }
139106266Sjulian};
140106266Sjulianconst struct ng_parse_type ng_source_timeval_type = {
141106266Sjulian	&ng_parse_struct_type,
142106266Sjulian	&ng_source_timeval_type_fields
143106266Sjulian};
144106266Sjulian
145106266Sjulian/* Parse type for struct ng_source_stats */
146106266Sjulianstatic const struct ng_parse_struct_field ng_source_stats_type_fields[]
147106266Sjulian	= NG_SOURCE_STATS_TYPE_INFO;
148106266Sjulianstatic const struct ng_parse_type ng_source_stats_type = {
149106266Sjulian	&ng_parse_struct_type,
150106266Sjulian	&ng_source_stats_type_fields
151106266Sjulian};
152106266Sjulian
153167156Semaste/* Parse type for struct ng_source_embed_info */
154167156Semastestatic const struct ng_parse_struct_field ng_source_embed_type_fields[] =
155167156Semaste	NG_SOURCE_EMBED_TYPE_INFO;
156167156Semastestatic const struct ng_parse_type ng_source_embed_type = {
157167156Semaste	&ng_parse_struct_type,
158167156Semaste	&ng_source_embed_type_fields
159167156Semaste};
160167156Semaste
161167160Semaste/* Parse type for struct ng_source_embed_cnt_info */
162167160Semastestatic const struct ng_parse_struct_field ng_source_embed_cnt_type_fields[] =
163167160Semaste	NG_SOURCE_EMBED_CNT_TYPE_INFO;
164167160Semastestatic const struct ng_parse_type ng_source_embed_cnt_type = {
165167160Semaste	&ng_parse_struct_type,
166167160Semaste	&ng_source_embed_cnt_type_fields
167167160Semaste};
168167160Semaste
169106266Sjulian/* List of commands and how to convert arguments to/from ASCII */
170106266Sjulianstatic const struct ng_cmdlist ng_source_cmds[] = {
171106266Sjulian	{
172106266Sjulian	  NGM_SOURCE_COOKIE,
173106266Sjulian	  NGM_SOURCE_GET_STATS,
174106266Sjulian	  "getstats",
175106266Sjulian	  NULL,
176106266Sjulian	  &ng_source_stats_type
177106266Sjulian	},
178106266Sjulian	{
179106266Sjulian	  NGM_SOURCE_COOKIE,
180106266Sjulian	  NGM_SOURCE_CLR_STATS,
181106266Sjulian	  "clrstats",
182106266Sjulian	  NULL,
183106266Sjulian	  NULL
184106266Sjulian	},
185106266Sjulian	{
186106266Sjulian	  NGM_SOURCE_COOKIE,
187106266Sjulian	  NGM_SOURCE_GETCLR_STATS,
188106266Sjulian	  "getclrstats",
189106266Sjulian	  NULL,
190106266Sjulian	  &ng_source_stats_type
191106266Sjulian	},
192106266Sjulian	{
193106266Sjulian	  NGM_SOURCE_COOKIE,
194106266Sjulian	  NGM_SOURCE_START,
195106266Sjulian	  "start",
196106266Sjulian	  &ng_parse_uint64_type,
197106266Sjulian	  NULL
198106266Sjulian	},
199106266Sjulian	{
200106266Sjulian	  NGM_SOURCE_COOKIE,
201106266Sjulian	  NGM_SOURCE_STOP,
202106266Sjulian	  "stop",
203106266Sjulian	  NULL,
204106266Sjulian	  NULL
205106266Sjulian	},
206106266Sjulian	{
207106266Sjulian	  NGM_SOURCE_COOKIE,
208106266Sjulian	  NGM_SOURCE_CLR_DATA,
209106266Sjulian	  "clrdata",
210106266Sjulian	  NULL,
211106266Sjulian	  NULL
212106266Sjulian	},
213125033Sharti	{
214125033Sharti	  NGM_SOURCE_COOKIE,
215144674Sglebius	  NGM_SOURCE_SETIFACE,
216144674Sglebius	  "setiface",
217144674Sglebius	  &ng_parse_string_type,
218125033Sharti	  NULL
219125033Sharti	},
220153690Sglebius	{
221153690Sglebius	  NGM_SOURCE_COOKIE,
222153690Sglebius	  NGM_SOURCE_SETPPS,
223153690Sglebius	  "setpps",
224153690Sglebius	  &ng_parse_uint32_type,
225153690Sglebius	  NULL
226153690Sglebius	},
227167156Semaste	{
228167156Semaste	  NGM_SOURCE_COOKIE,
229167156Semaste	  NGM_SOURCE_SET_TIMESTAMP,
230167156Semaste	  "settimestamp",
231167156Semaste	  &ng_source_embed_type,
232167156Semaste	  NULL
233167156Semaste	},
234167156Semaste	{
235167156Semaste	  NGM_SOURCE_COOKIE,
236167156Semaste	  NGM_SOURCE_GET_TIMESTAMP,
237167156Semaste	  "gettimestamp",
238167156Semaste	  NULL,
239167156Semaste	  &ng_source_embed_type
240167156Semaste	},
241167160Semaste	{
242167160Semaste	  NGM_SOURCE_COOKIE,
243167160Semaste	  NGM_SOURCE_SET_COUNTER,
244167160Semaste	  "setcounter",
245167160Semaste	  &ng_source_embed_cnt_type,
246167160Semaste	  NULL
247167160Semaste	},
248167160Semaste	{
249167160Semaste	  NGM_SOURCE_COOKIE,
250167160Semaste	  NGM_SOURCE_GET_COUNTER,
251167160Semaste	  "getcounter",
252167160Semaste	  &ng_parse_uint8_type,
253167160Semaste	  &ng_source_embed_cnt_type
254167160Semaste	},
255106266Sjulian	{ 0 }
256106266Sjulian};
257106266Sjulian
258106266Sjulian/* Netgraph type descriptor */
259106266Sjulianstatic struct ng_type ng_source_typestruct = {
260129823Sjulian	.version =	NG_ABI_VERSION,
261129823Sjulian	.name =		NG_SOURCE_NODE_TYPE,
262129823Sjulian	.constructor =	ng_source_constructor,
263129823Sjulian	.rcvmsg =	ng_source_rcvmsg,
264129823Sjulian	.shutdown =	ng_source_rmnode,
265129823Sjulian	.newhook =	ng_source_newhook,
266144674Sglebius	.connect =	ng_source_connect,
267129823Sjulian	.rcvdata =	ng_source_rcvdata,
268129823Sjulian	.disconnect =	ng_source_disconnect,
269129823Sjulian	.cmdlist =	ng_source_cmds,
270106266Sjulian};
271106266SjulianNETGRAPH_INIT(source, &ng_source_typestruct);
272106266Sjulian
273144674Sglebiusstatic int ng_source_set_autosrc(sc_p, uint32_t);
274125032Sharti
275106266Sjulian/*
276106266Sjulian * Node constructor
277106266Sjulian */
278106266Sjulianstatic int
279106321Sjulianng_source_constructor(node_p node)
280106266Sjulian{
281106266Sjulian	sc_p sc;
282106266Sjulian
283220768Sglebius	sc = malloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
284106266Sjulian
285106321Sjulian	NG_NODE_SET_PRIVATE(node, sc);
286106321Sjulian	sc->node = node;
287106266Sjulian	sc->snd_queue.ifq_maxlen = 2048;	/* XXX not checked */
288137138Sglebius	ng_callout_init(&sc->intr_ch);
289137138Sglebius
290106266Sjulian	return (0);
291106266Sjulian}
292106266Sjulian
293106266Sjulian/*
294106266Sjulian * Add a hook
295106266Sjulian */
296106266Sjulianstatic int
297106266Sjulianng_source_newhook(node_p node, hook_p hook, const char *name)
298106266Sjulian{
299144674Sglebius	sc_p sc = NG_NODE_PRIVATE(node);
300106266Sjulian
301106266Sjulian	if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) {
302144674Sglebius		sc->input = hook;
303106266Sjulian	} else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) {
304144674Sglebius		sc->output = hook;
305298075Spfg		sc->output_ifp = NULL;
306106266Sjulian		bzero(&sc->stats, sizeof(sc->stats));
307106266Sjulian	} else
308106266Sjulian		return (EINVAL);
309144674Sglebius
310106266Sjulian	return (0);
311106266Sjulian}
312106266Sjulian
313106266Sjulian/*
314144674Sglebius * Hook has been added
315144674Sglebius */
316144674Sglebiusstatic int
317144674Sglebiusng_source_connect(hook_p hook)
318144674Sglebius{
319144674Sglebius	sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
320144674Sglebius	struct ng_mesg *msg;
321144674Sglebius	int dummy_error = 0;
322144674Sglebius
323144674Sglebius	/*
324144674Sglebius	 * If this is "output" hook, then request information
325144674Sglebius	 * from our downstream.
326144674Sglebius	 */
327144674Sglebius	if (hook == sc->output) {
328144674Sglebius		NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFNAME,
329144674Sglebius		    0, M_NOWAIT);
330144674Sglebius		if (msg == NULL)
331144674Sglebius			return (ENOBUFS);
332144674Sglebius
333144674Sglebius		/*
334144674Sglebius		 * Our hook and peer hook have HK_INVALID flag set,
335144674Sglebius		 * so we can't use NG_SEND_MSG_HOOK() macro here.
336144674Sglebius		 */
337144674Sglebius		NG_SEND_MSG_ID(dummy_error, sc->node, msg,
338144674Sglebius		    NG_NODE_ID(NG_PEER_NODE(sc->output)), NG_NODE_ID(sc->node));
339144674Sglebius	}
340144674Sglebius
341144674Sglebius	return (0);
342144674Sglebius}
343144674Sglebius
344144674Sglebius/*
345106266Sjulian * Receive a control message
346106266Sjulian */
347106266Sjulianstatic int
348106321Sjulianng_source_rcvmsg(node_p node, item_p item, hook_p lasthook)
349106266Sjulian{
350144674Sglebius	sc_p sc = NG_NODE_PRIVATE(node);
351144674Sglebius	struct ng_mesg *msg, *resp = NULL;
352106266Sjulian	int error = 0;
353106266Sjulian
354106321Sjulian	NGI_GET_MSG(item, msg);
355144674Sglebius
356106266Sjulian	switch (msg->header.typecookie) {
357106266Sjulian	case NGM_SOURCE_COOKIE:
358106435Sjulian		if (msg->header.flags & NGF_RESP) {
359106435Sjulian			error = EINVAL;
360106435Sjulian			break;
361106435Sjulian		}
362106266Sjulian		switch (msg->header.cmd) {
363106266Sjulian		case NGM_SOURCE_GET_STATS:
364106266Sjulian		case NGM_SOURCE_CLR_STATS:
365106266Sjulian		case NGM_SOURCE_GETCLR_STATS:
366106266Sjulian                    {
367106266Sjulian			struct ng_source_stats *stats;
368106266Sjulian
369106266Sjulian                        if (msg->header.cmd != NGM_SOURCE_CLR_STATS) {
370106266Sjulian                                NG_MKRESPONSE(resp, msg,
371106266Sjulian                                    sizeof(*stats), M_NOWAIT);
372106266Sjulian				if (resp == NULL) {
373106266Sjulian					error = ENOMEM;
374106266Sjulian					goto done;
375106266Sjulian				}
376106266Sjulian				sc->stats.queueOctets = sc->queueOctets;
377106319Sjulian				sc->stats.queueFrames = sc->snd_queue.ifq_len;
378106321Sjulian				if ((sc->node->nd_flags & NG_SOURCE_ACTIVE)
379106266Sjulian				    && !timevalisset(&sc->stats.endTime)) {
380106319Sjulian					getmicrotime(&sc->stats.elapsedTime);
381106266Sjulian					timevalsub(&sc->stats.elapsedTime,
382106319Sjulian					    &sc->stats.startTime);
383106266Sjulian				}
384106319Sjulian				stats = (struct ng_source_stats *)resp->data;
385106266Sjulian				bcopy(&sc->stats, stats, sizeof(* stats));
386106266Sjulian                        }
387106266Sjulian                        if (msg->header.cmd != NGM_SOURCE_GET_STATS)
388106266Sjulian				bzero(&sc->stats, sizeof(sc->stats));
389106266Sjulian		    }
390106266Sjulian		    break;
391106266Sjulian		case NGM_SOURCE_START:
392106266Sjulian		    {
393144674Sglebius			uint64_t packets;
394144674Sglebius
395144674Sglebius			if (msg->header.arglen != sizeof(uint64_t)) {
396106266Sjulian				error = EINVAL;
397106266Sjulian				break;
398106266Sjulian			}
399125033Sharti
400144674Sglebius			packets = *(uint64_t *)msg->data;
401144674Sglebius
402144674Sglebius			error = ng_source_start(sc, packets);
403144674Sglebius
404144674Sglebius		    	break;
405125033Sharti		    }
406106266Sjulian		case NGM_SOURCE_STOP:
407106266Sjulian			ng_source_stop(sc);
408106266Sjulian			break;
409106266Sjulian		case NGM_SOURCE_CLR_DATA:
410106266Sjulian			ng_source_clr_data(sc);
411106266Sjulian			break;
412144674Sglebius		case NGM_SOURCE_SETIFACE:
413144674Sglebius		    {
414144674Sglebius			char *ifname = (char *)msg->data;
415144674Sglebius
416144674Sglebius			if (msg->header.arglen < 2) {
417144674Sglebius				error = EINVAL;
418144674Sglebius				break;
419144674Sglebius			}
420144674Sglebius
421144674Sglebius			ng_source_store_output_ifp(sc, ifname);
422144674Sglebius			break;
423144674Sglebius		    }
424153690Sglebius		case NGM_SOURCE_SETPPS:
425153690Sglebius		    {
426153690Sglebius			uint32_t pps;
427153690Sglebius
428153690Sglebius			if (msg->header.arglen != sizeof(uint32_t)) {
429153690Sglebius				error = EINVAL;
430153690Sglebius				break;
431153690Sglebius			}
432153690Sglebius
433153690Sglebius			pps = *(uint32_t *)msg->data;
434153690Sglebius
435153690Sglebius			sc->stats.maxPps = pps;
436153690Sglebius
437153690Sglebius			break;
438153690Sglebius		    }
439167156Semaste		case NGM_SOURCE_SET_TIMESTAMP:
440167156Semaste		    {
441167156Semaste			struct ng_source_embed_info *embed;
442167156Semaste
443167168Semaste			if (msg->header.arglen != sizeof(*embed)) {
444167168Semaste				error = EINVAL;
445167168Semaste				goto done;
446167168Semaste			}
447167156Semaste			embed = (struct ng_source_embed_info *)msg->data;
448167156Semaste			bcopy(embed, &sc->embed_timestamp, sizeof(*embed));
449167156Semaste
450167156Semaste			break;
451167156Semaste		    }
452167156Semaste		case NGM_SOURCE_GET_TIMESTAMP:
453167156Semaste		    {
454167156Semaste			struct ng_source_embed_info *embed;
455167156Semaste
456243882Sglebius			NG_MKRESPONSE(resp, msg, sizeof(*embed), M_NOWAIT);
457167156Semaste			if (resp == NULL) {
458167156Semaste				error = ENOMEM;
459167156Semaste				goto done;
460167156Semaste			}
461167156Semaste			embed = (struct ng_source_embed_info *)resp->data;
462167156Semaste			bcopy(&sc->embed_timestamp, embed, sizeof(*embed));
463167156Semaste
464167156Semaste			break;
465167156Semaste		    }
466167160Semaste		case NGM_SOURCE_SET_COUNTER:
467167160Semaste		    {
468167160Semaste			struct ng_source_embed_cnt_info *embed;
469167160Semaste
470167168Semaste			if (msg->header.arglen != sizeof(*embed)) {
471167168Semaste				error = EINVAL;
472167168Semaste				goto done;
473167168Semaste			}
474167160Semaste			embed = (struct ng_source_embed_cnt_info *)msg->data;
475167160Semaste			if (embed->index >= NG_SOURCE_COUNTERS ||
476167160Semaste			    !(embed->width == 1 || embed->width == 2 ||
477167160Semaste			    embed->width == 4)) {
478167160Semaste				error = EINVAL;
479167160Semaste				goto done;
480167160Semaste			}
481167160Semaste			bcopy(embed, &sc->embed_counter[embed->index],
482167160Semaste			    sizeof(*embed));
483167160Semaste
484167160Semaste			break;
485167160Semaste		    }
486167160Semaste		case NGM_SOURCE_GET_COUNTER:
487167160Semaste		    {
488167160Semaste			uint8_t index = *(uint8_t *)msg->data;
489167160Semaste			struct ng_source_embed_cnt_info *embed;
490167160Semaste
491167160Semaste			if (index >= NG_SOURCE_COUNTERS) {
492167160Semaste				error = EINVAL;
493167160Semaste				goto done;
494167160Semaste			}
495243882Sglebius			NG_MKRESPONSE(resp, msg, sizeof(*embed), M_NOWAIT);
496167160Semaste			if (resp == NULL) {
497167160Semaste				error = ENOMEM;
498167160Semaste				goto done;
499167160Semaste			}
500167160Semaste			embed = (struct ng_source_embed_cnt_info *)resp->data;
501167160Semaste			bcopy(&sc->embed_counter[index], embed, sizeof(*embed));
502167160Semaste
503167160Semaste			break;
504167160Semaste		    }
505106266Sjulian		default:
506106266Sjulian			error = EINVAL;
507106266Sjulian			break;
508106266Sjulian		}
509106266Sjulian		break;
510106435Sjulian	case NGM_ETHER_COOKIE:
511106435Sjulian		if (!(msg->header.flags & NGF_RESP)) {
512106435Sjulian			error = EINVAL;
513106435Sjulian			break;
514106435Sjulian		}
515106435Sjulian		switch (msg->header.cmd) {
516144674Sglebius		case NGM_ETHER_GET_IFNAME:
517144674Sglebius		    {
518144674Sglebius			char *ifname = (char *)msg->data;
519144674Sglebius
520144674Sglebius			if (msg->header.arglen < 2) {
521144674Sglebius				error = EINVAL;
522144674Sglebius				break;
523144674Sglebius			}
524144674Sglebius
525144674Sglebius			if (ng_source_store_output_ifp(sc, ifname) == 0)
526106435Sjulian				ng_source_set_autosrc(sc, 0);
527106435Sjulian			break;
528144674Sglebius		    }
529106435Sjulian		default:
530106435Sjulian			error = EINVAL;
531106435Sjulian		}
532106435Sjulian		break;
533106266Sjulian	default:
534106266Sjulian		error = EINVAL;
535106266Sjulian		break;
536106266Sjulian	}
537106266Sjulian
538106266Sjuliandone:
539144674Sglebius	/* Take care of synchronous response, if any. */
540106321Sjulian	NG_RESPOND_MSG(error, node, item, resp);
541144674Sglebius	/* Free the message and return. */
542106321Sjulian	NG_FREE_MSG(msg);
543106266Sjulian	return (error);
544106266Sjulian}
545106266Sjulian
546106266Sjulian/*
547106266Sjulian * Receive data on a hook
548106266Sjulian *
549106266Sjulian * If data comes in the input hook, enqueue it on the send queue.
550106266Sjulian * If data comes in the output hook, discard it.
551106266Sjulian */
552106266Sjulianstatic int
553106321Sjulianng_source_rcvdata(hook_p hook, item_p item)
554106266Sjulian{
555144674Sglebius	sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
556144674Sglebius	struct mbuf *m;
557106266Sjulian	int error = 0;
558106266Sjulian
559106321Sjulian	NGI_GET_M(item, m);
560106321Sjulian	NG_FREE_ITEM(item);
561106266Sjulian
562106266Sjulian	/* Which hook? */
563144674Sglebius	if (hook == sc->output) {
564106266Sjulian		/* discard */
565106321Sjulian		NG_FREE_M(m);
566106266Sjulian		return (error);
567106266Sjulian	}
568144674Sglebius	KASSERT(hook == sc->input, ("%s: no hook!", __func__));
569106266Sjulian
570144674Sglebius	/* Enqueue packet. */
571106266Sjulian	/* XXX should we check IF_QFULL() ? */
572125031Sharti	_IF_ENQUEUE(&sc->snd_queue, m);
573106266Sjulian	sc->queueOctets += m->m_pkthdr.len;
574167160Semaste	sc->last_packet = m;
575106266Sjulian
576106266Sjulian	return (0);
577106266Sjulian}
578106266Sjulian
579106266Sjulian/*
580106266Sjulian * Shutdown processing
581106266Sjulian */
582106266Sjulianstatic int
583106266Sjulianng_source_rmnode(node_p node)
584106266Sjulian{
585144674Sglebius	sc_p sc = NG_NODE_PRIVATE(node);
586106266Sjulian
587106266Sjulian	ng_source_stop(sc);
588106266Sjulian	ng_source_clr_data(sc);
589106321Sjulian	NG_NODE_SET_PRIVATE(node, NULL);
590106321Sjulian	NG_NODE_UNREF(node);
591125077Sharti	free(sc, M_NETGRAPH);
592144674Sglebius
593106266Sjulian	return (0);
594106266Sjulian}
595106266Sjulian
596106266Sjulian/*
597106266Sjulian * Hook disconnection
598106266Sjulian */
599106266Sjulianstatic int
600106266Sjulianng_source_disconnect(hook_p hook)
601106266Sjulian{
602106319Sjulian	sc_p sc;
603106266Sjulian
604106321Sjulian	sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
605125077Sharti	KASSERT(sc != NULL, ("%s: null node private", __func__));
606144674Sglebius	if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hook == sc->output)
607106321Sjulian		ng_rmnode_self(NG_HOOK_NODE(hook));
608106266Sjulian	return (0);
609106266Sjulian}
610106266Sjulian
611106266Sjulian/*
612218909Sbrucec * Set sc->output_ifp to point to the struct ifnet of the interface
613106435Sjulian * reached via our output hook.
614106435Sjulian */
615106435Sjulianstatic int
616144674Sglebiusng_source_store_output_ifp(sc_p sc, char *ifname)
617106435Sjulian{
618106435Sjulian	struct ifnet *ifp;
619106266Sjulian
620144674Sglebius	ifp = ifunit(ifname);
621106266Sjulian
622106266Sjulian	if (ifp == NULL) {
623183225Szec		printf("%s: can't find interface %s\n", __func__, ifname);
624106266Sjulian		return (EINVAL);
625106266Sjulian	}
626106266Sjulian	sc->output_ifp = ifp;
627106266Sjulian
628106266Sjulian#if 1
629106266Sjulian	/* XXX mucking with a drivers ifqueue size is ugly but we need it
630106266Sjulian	 * to queue a lot of packets to get close to line rate on a gigabit
631106266Sjulian	 * interface with small packets.
632106266Sjulian	 * XXX we should restore the original value at stop or disconnect
633106266Sjulian	 */
634125077Sharti	if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN) {
635106266Sjulian		printf("ng_source: changing ifq_maxlen from %d to %d\n",
636106319Sjulian		    ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN);
637106266Sjulian		ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN;
638106266Sjulian	}
639106266Sjulian#endif
640106266Sjulian	return (0);
641106266Sjulian}
642106266Sjulian
643106266Sjulian/*
644106266Sjulian * Set the attached ethernet node's ethernet source address override flag.
645106266Sjulian */
646106266Sjulianstatic int
647144674Sglebiusng_source_set_autosrc(sc_p sc, uint32_t flag)
648106266Sjulian{
649106266Sjulian	struct ng_mesg *msg;
650106266Sjulian	int error = 0;
651106266Sjulian
652106266Sjulian	NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC,
653144674Sglebius	    sizeof (uint32_t), M_NOWAIT);
654106266Sjulian	if (msg == NULL)
655106266Sjulian		return(ENOBUFS);
656106266Sjulian
657144674Sglebius	*(uint32_t *)msg->data = flag;
658144674Sglebius	NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output, 0);
659106266Sjulian	return (error);
660106266Sjulian}
661106266Sjulian
662106266Sjulian/*
663106266Sjulian * Clear out the data we've queued
664106266Sjulian */
665106266Sjulianstatic void
666106266Sjulianng_source_clr_data (sc_p sc)
667106266Sjulian{
668106266Sjulian	struct mbuf *m;
669106266Sjulian
670106266Sjulian	for (;;) {
671125031Sharti		_IF_DEQUEUE(&sc->snd_queue, m);
672106266Sjulian		if (m == NULL)
673106266Sjulian			break;
674106266Sjulian		NG_FREE_M(m);
675106266Sjulian	}
676106266Sjulian	sc->queueOctets = 0;
677167168Semaste	sc->last_packet = NULL;
678106266Sjulian}
679106266Sjulian
680106266Sjulian/*
681106266Sjulian * Start sending queued data out the output hook
682106266Sjulian */
683144674Sglebiusstatic int
684144674Sglebiusng_source_start(sc_p sc, uint64_t packets)
685106266Sjulian{
686144674Sglebius	if (sc->output_ifp == NULL) {
687144674Sglebius		printf("ng_source: start without iface configured\n");
688144674Sglebius		return (ENXIO);
689144674Sglebius	}
690144674Sglebius
691144674Sglebius	if (sc->node->nd_flags & NG_SOURCE_ACTIVE)
692144674Sglebius		return (EBUSY);
693144674Sglebius
694144674Sglebius	sc->node->nd_flags |= NG_SOURCE_ACTIVE;
695144674Sglebius
696144674Sglebius	sc->packets = packets;
697144674Sglebius	timevalclear(&sc->stats.elapsedTime);
698144674Sglebius	timevalclear(&sc->stats.endTime);
699144674Sglebius	getmicrotime(&sc->stats.startTime);
700153690Sglebius	getmicrotime(&sc->stats.lastTime);
701144674Sglebius	ng_callout(&sc->intr_ch, sc->node, NULL, 0,
702144674Sglebius	    ng_source_intr, sc, 0);
703144674Sglebius
704144674Sglebius	return (0);
705106266Sjulian}
706106266Sjulian
707106266Sjulian/*
708106266Sjulian * Stop sending queued data out the output hook
709106266Sjulian */
710106266Sjulianstatic void
711144674Sglebiusng_source_stop(sc_p sc)
712106266Sjulian{
713144674Sglebius	ng_uncallout(&sc->intr_ch, sc->node);
714144674Sglebius	sc->node->nd_flags &= ~NG_SOURCE_ACTIVE;
715144674Sglebius	getmicrotime(&sc->stats.endTime);
716144674Sglebius	sc->stats.elapsedTime = sc->stats.endTime;
717144674Sglebius	timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime);
718106266Sjulian}
719106266Sjulian
720106266Sjulian/*
721106266Sjulian * While active called every NG_SOURCE_INTR_TICKS ticks.
722106266Sjulian * Sends as many packets as the interface connected to our
723106266Sjulian * output hook is able to enqueue.
724106266Sjulian */
725106266Sjulianstatic void
726125243Sharting_source_intr(node_p node, hook_p hook, void *arg1, int arg2)
727106266Sjulian{
728125243Sharti	sc_p sc = (sc_p)arg1;
729106266Sjulian	struct ifqueue *ifq;
730106266Sjulian	int packets;
731106266Sjulian
732125077Sharti	KASSERT(sc != NULL, ("%s: null node private", __func__));
733106266Sjulian
734144674Sglebius	if (sc->packets == 0 || sc->output == NULL
735106321Sjulian	    || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) {
736106266Sjulian		ng_source_stop(sc);
737106266Sjulian		return;
738106266Sjulian	}
739106266Sjulian
740125033Sharti	if (sc->output_ifp != NULL) {
741141745Sru		ifq = (struct ifqueue *)&sc->output_ifp->if_snd;
742125033Sharti		packets = ifq->ifq_maxlen - ifq->ifq_len;
743125033Sharti	} else
744125033Sharti		packets = sc->snd_queue.ifq_len;
745125033Sharti
746153690Sglebius	if (sc->stats.maxPps != 0) {
747153690Sglebius		struct timeval	now, elapsed;
748153690Sglebius		uint64_t	usec;
749153690Sglebius		int		maxpkt;
750153690Sglebius
751153690Sglebius		getmicrotime(&now);
752153690Sglebius		elapsed = now;
753153690Sglebius		timevalsub(&elapsed, &sc->stats.lastTime);
754153690Sglebius		usec = elapsed.tv_sec * 1000000 + elapsed.tv_usec;
755153690Sglebius		maxpkt = (uint64_t)sc->stats.maxPps * usec / 1000000;
756153690Sglebius		sc->stats.lastTime = now;
757153690Sglebius		if (packets > maxpkt)
758153690Sglebius			packets = maxpkt;
759153690Sglebius	}
760153690Sglebius
761106266Sjulian	ng_source_send(sc, packets, NULL);
762125243Sharti	if (sc->packets == 0)
763106266Sjulian		ng_source_stop(sc);
764125243Sharti	else
765138268Sglebius		ng_callout(&sc->intr_ch, node, NULL, NG_SOURCE_INTR_TICKS,
766137136Sglebius		    ng_source_intr, sc, 0);
767106266Sjulian}
768106266Sjulian
769106266Sjulian/*
770154707Sglebius * Send packets out our output hook.
771106266Sjulian */
772106266Sjulianstatic int
773154707Sglebiusng_source_send(sc_p sc, int tosend, int *sent_p)
774106266Sjulian{
775106266Sjulian	struct mbuf *m, *m2;
776154707Sglebius	int sent;
777106266Sjulian	int error = 0;
778106266Sjulian
779125077Sharti	KASSERT(tosend >= 0, ("%s: negative tosend param", __func__));
780106321Sjulian	KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE,
781125077Sharti	    ("%s: inactive node", __func__));
782106266Sjulian
783144674Sglebius	if ((uint64_t)tosend > sc->packets)
784106266Sjulian		tosend = sc->packets;
785106266Sjulian
786154707Sglebius	/* Go through the queue sending packets one by one. */
787106266Sjulian	for (sent = 0; error == 0 && sent < tosend; ++sent) {
788125031Sharti		_IF_DEQUEUE(&sc->snd_queue, m);
789106266Sjulian		if (m == NULL)
790106266Sjulian			break;
791106266Sjulian
792167156Semaste		/* Duplicate and modify the packet. */
793167156Semaste		error = ng_source_dup_mod(sc, m, &m2);
794167156Semaste		if (error) {
795167156Semaste			if (error == ENOBUFS)
796167156Semaste				_IF_PREPEND(&sc->snd_queue, m);
797167156Semaste			else
798167156Semaste				_IF_ENQUEUE(&sc->snd_queue, m);
799106266Sjulian			break;
800106266Sjulian		}
801106266Sjulian
802144674Sglebius		/* Re-enqueue the original packet for us. */
803125031Sharti		_IF_ENQUEUE(&sc->snd_queue, m);
804106266Sjulian
805154707Sglebius		sc->stats.outFrames++;
806154707Sglebius		sc->stats.outOctets += m2->m_pkthdr.len;
807154707Sglebius		NG_SEND_DATA_ONLY(error, sc->output, m2);
808154707Sglebius		if (error)
809106266Sjulian			break;
810106266Sjulian	}
811106266Sjulian
812106266Sjulian	sc->packets -= sent;
813106266Sjulian	if (sent_p != NULL)
814106266Sjulian		*sent_p = sent;
815106266Sjulian	return (error);
816106266Sjulian}
817167156Semaste
818167156Semaste/*
819167156Semaste * Modify packet in 'm' by changing 'len' bytes starting at 'offset'
820167156Semaste * to data in 'cp'.
821167156Semaste *
822167156Semaste * The packet data in 'm' must be in a contiguous buffer in a single mbuf.
823167156Semaste */
824167156Semastestatic void
825167156Semasteng_source_packet_mod(sc_p sc, struct mbuf *m, int offset, int len, caddr_t cp,
826167156Semaste    int flags)
827167156Semaste{
828167156Semaste	if (len == 0)
829167156Semaste		return;
830167156Semaste
831167156Semaste	/* Can't modify beyond end of packet. */
832167156Semaste	/* TODO: Pad packet for this case. */
833167156Semaste	if (offset + len > m->m_len)
834167156Semaste		return;
835167156Semaste
836167156Semaste	bcopy(cp, mtod_off(m, offset, caddr_t), len);
837167156Semaste}
838167156Semaste
839167160Semastestatic void
840167160Semasteng_source_mod_counter(sc_p sc, struct ng_source_embed_cnt_info *cnt,
841167160Semaste    struct mbuf *m, int increment)
842167160Semaste{
843167160Semaste	caddr_t cp;
844167160Semaste	uint32_t val;
845167160Semaste
846167160Semaste	val = htonl(cnt->next_val);
847167160Semaste	cp = (caddr_t)&val + sizeof(val) - cnt->width;
848167160Semaste	ng_source_packet_mod(sc, m, cnt->offset, cnt->width, cp, cnt->flags);
849167160Semaste
850167160Semaste	if (increment) {
851167160Semaste		cnt->next_val += increment;
852167160Semaste
853167160Semaste		if (increment > 0 && cnt->next_val > cnt->max_val) {
854167160Semaste			cnt->next_val = cnt->min_val - 1 +
855167160Semaste			    (cnt->next_val - cnt->max_val);
856167160Semaste			if (cnt->next_val > cnt->max_val)
857167160Semaste				cnt->next_val = cnt->max_val;
858167160Semaste		} else if (increment < 0 && cnt->next_val < cnt->min_val) {
859167160Semaste			cnt->next_val = cnt->max_val + 1 +
860167160Semaste			    (cnt->next_val - cnt->min_val);
861167160Semaste			if (cnt->next_val < cnt->min_val)
862167160Semaste				cnt->next_val = cnt->max_val;
863167160Semaste		}
864167160Semaste	}
865167160Semaste}
866167160Semaste
867167156Semastestatic int
868167156Semasteng_source_dup_mod(sc_p sc, struct mbuf *m0, struct mbuf **m_ptr)
869167156Semaste{
870167156Semaste	struct mbuf *m;
871167160Semaste	struct ng_source_embed_cnt_info *cnt;
872167156Semaste	struct ng_source_embed_info *ts;
873167156Semaste	int modify;
874167156Semaste	int error = 0;
875167160Semaste	int i, increment;
876167156Semaste
877167156Semaste	/* Are we going to modify packets? */
878167156Semaste	modify = sc->embed_timestamp.flags & NGM_SOURCE_EMBED_ENABLE;
879167160Semaste	for (i = 0; !modify && i < NG_SOURCE_COUNTERS; ++i)
880167160Semaste		modify = sc->embed_counter[i].flags & NGM_SOURCE_EMBED_ENABLE;
881167156Semaste
882167156Semaste	/* Duplicate the packet. */
883167156Semaste	if (modify)
884243882Sglebius		m = m_dup(m0, M_NOWAIT);
885167156Semaste	else
886243882Sglebius		m = m_copypacket(m0, M_NOWAIT);
887167156Semaste	if (m == NULL) {
888167156Semaste		error = ENOBUFS;
889167156Semaste		goto done;
890167156Semaste	}
891167156Semaste	*m_ptr = m;
892167156Semaste
893167156Semaste	if (!modify)
894167156Semaste		goto done;
895167156Semaste
896167156Semaste	/* Modify the copied packet for sending. */
897167156Semaste	KASSERT(M_WRITABLE(m), ("%s: packet not writable", __func__));
898167156Semaste
899167160Semaste	for (i = 0; i < NG_SOURCE_COUNTERS; ++i) {
900167160Semaste		cnt = &sc->embed_counter[i];
901167160Semaste		if (cnt->flags & NGM_SOURCE_EMBED_ENABLE) {
902167160Semaste			if ((cnt->flags & NGM_SOURCE_INC_CNT_PER_LIST) == 0 ||
903167160Semaste			    sc->last_packet == m0)
904167160Semaste				increment = cnt->increment;
905167160Semaste			else
906167160Semaste				increment = 0;
907167160Semaste			ng_source_mod_counter(sc, cnt, m, increment);
908167160Semaste		}
909167160Semaste	}
910167160Semaste
911167156Semaste	ts = &sc->embed_timestamp;
912167156Semaste	if (ts->flags & NGM_SOURCE_EMBED_ENABLE) {
913167156Semaste		struct timeval now;
914167156Semaste		getmicrotime(&now);
915167156Semaste		now.tv_sec = htonl(now.tv_sec);
916167156Semaste		now.tv_usec = htonl(now.tv_usec);
917167156Semaste		ng_source_packet_mod(sc, m, ts->offset, sizeof (now),
918167156Semaste		    (caddr_t)&now, ts->flags);
919167156Semaste	}
920167156Semaste
921167156Semastedone:
922167156Semaste	return(error);
923167156Semaste}
924