ng_source.c revision 220768
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: head/sys/netgraph/ng_source.c 220768 2011-04-18 09:12:27Z glebius $");
43125077Sharti
44106266Sjulian/*
45106266Sjulian * This node is used for high speed packet geneneration.  It queues
46143387Sbmilekic * all data recieved 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>
71196019Srwatson#include <net/vnet.h>
72106266Sjulian#include <netgraph/ng_message.h>
73106266Sjulian#include <netgraph/netgraph.h>
74106266Sjulian#include <netgraph/ng_parse.h>
75106266Sjulian#include <netgraph/ng_ether.h>
76106266Sjulian#include <netgraph/ng_source.h>
77106266Sjulian
78106266Sjulian#define NG_SOURCE_INTR_TICKS		1
79106266Sjulian#define NG_SOURCE_DRIVER_IFQ_MAXLEN	(4*1024)
80106266Sjulian
81167156Semaste#define	mtod_off(m,off,t)	((t)(mtod((m),caddr_t)+(off)))
82167156Semaste
83106266Sjulian/* Per node info */
84106266Sjulianstruct privdata {
85106266Sjulian	node_p				node;
86144674Sglebius	hook_p				input;
87144674Sglebius	hook_p				output;
88106266Sjulian	struct ng_source_stats		stats;
89106319Sjulian	struct ifqueue			snd_queue;	/* packets to send */
90167160Semaste	struct mbuf			*last_packet;	/* last pkt in queue */
91106266Sjulian	struct ifnet			*output_ifp;
92137138Sglebius	struct callout			intr_ch;
93144674Sglebius	uint64_t			packets;	/* packets to send */
94144674Sglebius	uint32_t			queueOctets;
95167156Semaste	struct ng_source_embed_info	embed_timestamp;
96167160Semaste	struct ng_source_embed_cnt_info	embed_counter[NG_SOURCE_COUNTERS];
97106266Sjulian};
98106266Sjuliantypedef struct privdata *sc_p;
99106266Sjulian
100106266Sjulian/* Node flags */
101106266Sjulian#define NG_SOURCE_ACTIVE	(NGF_TYPE1)
102106266Sjulian
103106266Sjulian/* Netgraph methods */
104106266Sjulianstatic ng_constructor_t	ng_source_constructor;
105106266Sjulianstatic ng_rcvmsg_t	ng_source_rcvmsg;
106106266Sjulianstatic ng_shutdown_t	ng_source_rmnode;
107106266Sjulianstatic ng_newhook_t	ng_source_newhook;
108144674Sglebiusstatic ng_connect_t	ng_source_connect;
109106266Sjulianstatic ng_rcvdata_t	ng_source_rcvdata;
110106266Sjulianstatic ng_disconnect_t	ng_source_disconnect;
111106266Sjulian
112106266Sjulian/* Other functions */
113125243Shartistatic void		ng_source_intr(node_p, hook_p, void *, int);
114106266Sjulianstatic void		ng_source_clr_data (sc_p);
115144674Sglebiusstatic int		ng_source_start (sc_p, uint64_t);
116106266Sjulianstatic void		ng_source_stop (sc_p);
117106266Sjulianstatic int		ng_source_send (sc_p, int, int *);
118144674Sglebiusstatic int		ng_source_store_output_ifp(sc_p, char *);
119167156Semastestatic void		ng_source_packet_mod(sc_p, struct mbuf *,
120167156Semaste			    int, int, caddr_t, int);
121167160Semastestatic void		ng_source_mod_counter(sc_p sc,
122167160Semaste			    struct ng_source_embed_cnt_info *cnt,
123167160Semaste			    struct mbuf *m, int increment);
124167156Semastestatic int		ng_source_dup_mod(sc_p, struct mbuf *,
125167156Semaste			    struct mbuf **);
126106266Sjulian
127106266Sjulian/* Parse type for timeval */
128125077Shartistatic const struct ng_parse_struct_field ng_source_timeval_type_fields[] = {
129106266Sjulian	{ "tv_sec",		&ng_parse_int32_type	},
130106266Sjulian	{ "tv_usec",		&ng_parse_int32_type	},
131106266Sjulian	{ NULL }
132106266Sjulian};
133106266Sjulianconst struct ng_parse_type ng_source_timeval_type = {
134106266Sjulian	&ng_parse_struct_type,
135106266Sjulian	&ng_source_timeval_type_fields
136106266Sjulian};
137106266Sjulian
138106266Sjulian/* Parse type for struct ng_source_stats */
139106266Sjulianstatic const struct ng_parse_struct_field ng_source_stats_type_fields[]
140106266Sjulian	= NG_SOURCE_STATS_TYPE_INFO;
141106266Sjulianstatic const struct ng_parse_type ng_source_stats_type = {
142106266Sjulian	&ng_parse_struct_type,
143106266Sjulian	&ng_source_stats_type_fields
144106266Sjulian};
145106266Sjulian
146167156Semaste/* Parse type for struct ng_source_embed_info */
147167156Semastestatic const struct ng_parse_struct_field ng_source_embed_type_fields[] =
148167156Semaste	NG_SOURCE_EMBED_TYPE_INFO;
149167156Semastestatic const struct ng_parse_type ng_source_embed_type = {
150167156Semaste	&ng_parse_struct_type,
151167156Semaste	&ng_source_embed_type_fields
152167156Semaste};
153167156Semaste
154167160Semaste/* Parse type for struct ng_source_embed_cnt_info */
155167160Semastestatic const struct ng_parse_struct_field ng_source_embed_cnt_type_fields[] =
156167160Semaste	NG_SOURCE_EMBED_CNT_TYPE_INFO;
157167160Semastestatic const struct ng_parse_type ng_source_embed_cnt_type = {
158167160Semaste	&ng_parse_struct_type,
159167160Semaste	&ng_source_embed_cnt_type_fields
160167160Semaste};
161167160Semaste
162106266Sjulian/* List of commands and how to convert arguments to/from ASCII */
163106266Sjulianstatic const struct ng_cmdlist ng_source_cmds[] = {
164106266Sjulian	{
165106266Sjulian	  NGM_SOURCE_COOKIE,
166106266Sjulian	  NGM_SOURCE_GET_STATS,
167106266Sjulian	  "getstats",
168106266Sjulian	  NULL,
169106266Sjulian	  &ng_source_stats_type
170106266Sjulian	},
171106266Sjulian	{
172106266Sjulian	  NGM_SOURCE_COOKIE,
173106266Sjulian	  NGM_SOURCE_CLR_STATS,
174106266Sjulian	  "clrstats",
175106266Sjulian	  NULL,
176106266Sjulian	  NULL
177106266Sjulian	},
178106266Sjulian	{
179106266Sjulian	  NGM_SOURCE_COOKIE,
180106266Sjulian	  NGM_SOURCE_GETCLR_STATS,
181106266Sjulian	  "getclrstats",
182106266Sjulian	  NULL,
183106266Sjulian	  &ng_source_stats_type
184106266Sjulian	},
185106266Sjulian	{
186106266Sjulian	  NGM_SOURCE_COOKIE,
187106266Sjulian	  NGM_SOURCE_START,
188106266Sjulian	  "start",
189106266Sjulian	  &ng_parse_uint64_type,
190106266Sjulian	  NULL
191106266Sjulian	},
192106266Sjulian	{
193106266Sjulian	  NGM_SOURCE_COOKIE,
194106266Sjulian	  NGM_SOURCE_STOP,
195106266Sjulian	  "stop",
196106266Sjulian	  NULL,
197106266Sjulian	  NULL
198106266Sjulian	},
199106266Sjulian	{
200106266Sjulian	  NGM_SOURCE_COOKIE,
201106266Sjulian	  NGM_SOURCE_CLR_DATA,
202106266Sjulian	  "clrdata",
203106266Sjulian	  NULL,
204106266Sjulian	  NULL
205106266Sjulian	},
206125033Sharti	{
207125033Sharti	  NGM_SOURCE_COOKIE,
208144674Sglebius	  NGM_SOURCE_SETIFACE,
209144674Sglebius	  "setiface",
210144674Sglebius	  &ng_parse_string_type,
211125033Sharti	  NULL
212125033Sharti	},
213153690Sglebius	{
214153690Sglebius	  NGM_SOURCE_COOKIE,
215153690Sglebius	  NGM_SOURCE_SETPPS,
216153690Sglebius	  "setpps",
217153690Sglebius	  &ng_parse_uint32_type,
218153690Sglebius	  NULL
219153690Sglebius	},
220167156Semaste	{
221167156Semaste	  NGM_SOURCE_COOKIE,
222167156Semaste	  NGM_SOURCE_SET_TIMESTAMP,
223167156Semaste	  "settimestamp",
224167156Semaste	  &ng_source_embed_type,
225167156Semaste	  NULL
226167156Semaste	},
227167156Semaste	{
228167156Semaste	  NGM_SOURCE_COOKIE,
229167156Semaste	  NGM_SOURCE_GET_TIMESTAMP,
230167156Semaste	  "gettimestamp",
231167156Semaste	  NULL,
232167156Semaste	  &ng_source_embed_type
233167156Semaste	},
234167160Semaste	{
235167160Semaste	  NGM_SOURCE_COOKIE,
236167160Semaste	  NGM_SOURCE_SET_COUNTER,
237167160Semaste	  "setcounter",
238167160Semaste	  &ng_source_embed_cnt_type,
239167160Semaste	  NULL
240167160Semaste	},
241167160Semaste	{
242167160Semaste	  NGM_SOURCE_COOKIE,
243167160Semaste	  NGM_SOURCE_GET_COUNTER,
244167160Semaste	  "getcounter",
245167160Semaste	  &ng_parse_uint8_type,
246167160Semaste	  &ng_source_embed_cnt_type
247167160Semaste	},
248106266Sjulian	{ 0 }
249106266Sjulian};
250106266Sjulian
251106266Sjulian/* Netgraph type descriptor */
252106266Sjulianstatic struct ng_type ng_source_typestruct = {
253129823Sjulian	.version =	NG_ABI_VERSION,
254129823Sjulian	.name =		NG_SOURCE_NODE_TYPE,
255129823Sjulian	.constructor =	ng_source_constructor,
256129823Sjulian	.rcvmsg =	ng_source_rcvmsg,
257129823Sjulian	.shutdown =	ng_source_rmnode,
258129823Sjulian	.newhook =	ng_source_newhook,
259144674Sglebius	.connect =	ng_source_connect,
260129823Sjulian	.rcvdata =	ng_source_rcvdata,
261129823Sjulian	.disconnect =	ng_source_disconnect,
262129823Sjulian	.cmdlist =	ng_source_cmds,
263106266Sjulian};
264106266SjulianNETGRAPH_INIT(source, &ng_source_typestruct);
265106266Sjulian
266144674Sglebiusstatic int ng_source_set_autosrc(sc_p, uint32_t);
267125032Sharti
268106266Sjulian/*
269106266Sjulian * Node constructor
270106266Sjulian */
271106266Sjulianstatic int
272106321Sjulianng_source_constructor(node_p node)
273106266Sjulian{
274106266Sjulian	sc_p sc;
275106266Sjulian
276220768Sglebius	sc = malloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
277106266Sjulian
278106321Sjulian	NG_NODE_SET_PRIVATE(node, sc);
279106321Sjulian	sc->node = node;
280106266Sjulian	sc->snd_queue.ifq_maxlen = 2048;	/* XXX not checked */
281137138Sglebius	ng_callout_init(&sc->intr_ch);
282137138Sglebius
283106266Sjulian	return (0);
284106266Sjulian}
285106266Sjulian
286106266Sjulian/*
287106266Sjulian * Add a hook
288106266Sjulian */
289106266Sjulianstatic int
290106266Sjulianng_source_newhook(node_p node, hook_p hook, const char *name)
291106266Sjulian{
292144674Sglebius	sc_p sc = NG_NODE_PRIVATE(node);
293106266Sjulian
294106266Sjulian	if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) {
295144674Sglebius		sc->input = hook;
296106266Sjulian	} else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) {
297144674Sglebius		sc->output = hook;
298106266Sjulian		sc->output_ifp = 0;
299106266Sjulian		bzero(&sc->stats, sizeof(sc->stats));
300106266Sjulian	} else
301106266Sjulian		return (EINVAL);
302144674Sglebius
303106266Sjulian	return (0);
304106266Sjulian}
305106266Sjulian
306106266Sjulian/*
307144674Sglebius * Hook has been added
308144674Sglebius */
309144674Sglebiusstatic int
310144674Sglebiusng_source_connect(hook_p hook)
311144674Sglebius{
312144674Sglebius	sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
313144674Sglebius	struct ng_mesg *msg;
314144674Sglebius	int dummy_error = 0;
315144674Sglebius
316144674Sglebius	/*
317144674Sglebius	 * If this is "output" hook, then request information
318144674Sglebius	 * from our downstream.
319144674Sglebius	 */
320144674Sglebius	if (hook == sc->output) {
321144674Sglebius		NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFNAME,
322144674Sglebius		    0, M_NOWAIT);
323144674Sglebius		if (msg == NULL)
324144674Sglebius			return (ENOBUFS);
325144674Sglebius
326144674Sglebius		/*
327144674Sglebius		 * Our hook and peer hook have HK_INVALID flag set,
328144674Sglebius		 * so we can't use NG_SEND_MSG_HOOK() macro here.
329144674Sglebius		 */
330144674Sglebius		NG_SEND_MSG_ID(dummy_error, sc->node, msg,
331144674Sglebius		    NG_NODE_ID(NG_PEER_NODE(sc->output)), NG_NODE_ID(sc->node));
332144674Sglebius	}
333144674Sglebius
334144674Sglebius	return (0);
335144674Sglebius}
336144674Sglebius
337144674Sglebius/*
338106266Sjulian * Receive a control message
339106266Sjulian */
340106266Sjulianstatic int
341106321Sjulianng_source_rcvmsg(node_p node, item_p item, hook_p lasthook)
342106266Sjulian{
343144674Sglebius	sc_p sc = NG_NODE_PRIVATE(node);
344144674Sglebius	struct ng_mesg *msg, *resp = NULL;
345106266Sjulian	int error = 0;
346106266Sjulian
347106321Sjulian	NGI_GET_MSG(item, msg);
348144674Sglebius
349106266Sjulian	switch (msg->header.typecookie) {
350106266Sjulian	case NGM_SOURCE_COOKIE:
351106435Sjulian		if (msg->header.flags & NGF_RESP) {
352106435Sjulian			error = EINVAL;
353106435Sjulian			break;
354106435Sjulian		}
355106266Sjulian		switch (msg->header.cmd) {
356106266Sjulian		case NGM_SOURCE_GET_STATS:
357106266Sjulian		case NGM_SOURCE_CLR_STATS:
358106266Sjulian		case NGM_SOURCE_GETCLR_STATS:
359106266Sjulian                    {
360106266Sjulian			struct ng_source_stats *stats;
361106266Sjulian
362106266Sjulian                        if (msg->header.cmd != NGM_SOURCE_CLR_STATS) {
363106266Sjulian                                NG_MKRESPONSE(resp, msg,
364106266Sjulian                                    sizeof(*stats), M_NOWAIT);
365106266Sjulian				if (resp == NULL) {
366106266Sjulian					error = ENOMEM;
367106266Sjulian					goto done;
368106266Sjulian				}
369106266Sjulian				sc->stats.queueOctets = sc->queueOctets;
370106319Sjulian				sc->stats.queueFrames = sc->snd_queue.ifq_len;
371106321Sjulian				if ((sc->node->nd_flags & NG_SOURCE_ACTIVE)
372106266Sjulian				    && !timevalisset(&sc->stats.endTime)) {
373106319Sjulian					getmicrotime(&sc->stats.elapsedTime);
374106266Sjulian					timevalsub(&sc->stats.elapsedTime,
375106319Sjulian					    &sc->stats.startTime);
376106266Sjulian				}
377106319Sjulian				stats = (struct ng_source_stats *)resp->data;
378106266Sjulian				bcopy(&sc->stats, stats, sizeof(* stats));
379106266Sjulian                        }
380106266Sjulian                        if (msg->header.cmd != NGM_SOURCE_GET_STATS)
381106266Sjulian				bzero(&sc->stats, sizeof(sc->stats));
382106266Sjulian		    }
383106266Sjulian		    break;
384106266Sjulian		case NGM_SOURCE_START:
385106266Sjulian		    {
386144674Sglebius			uint64_t packets;
387144674Sglebius
388144674Sglebius			if (msg->header.arglen != sizeof(uint64_t)) {
389106266Sjulian				error = EINVAL;
390106266Sjulian				break;
391106266Sjulian			}
392125033Sharti
393144674Sglebius			packets = *(uint64_t *)msg->data;
394144674Sglebius
395144674Sglebius			error = ng_source_start(sc, packets);
396144674Sglebius
397144674Sglebius		    	break;
398125033Sharti		    }
399106266Sjulian		case NGM_SOURCE_STOP:
400106266Sjulian			ng_source_stop(sc);
401106266Sjulian			break;
402106266Sjulian		case NGM_SOURCE_CLR_DATA:
403106266Sjulian			ng_source_clr_data(sc);
404106266Sjulian			break;
405144674Sglebius		case NGM_SOURCE_SETIFACE:
406144674Sglebius		    {
407144674Sglebius			char *ifname = (char *)msg->data;
408144674Sglebius
409144674Sglebius			if (msg->header.arglen < 2) {
410144674Sglebius				error = EINVAL;
411144674Sglebius				break;
412144674Sglebius			}
413144674Sglebius
414144674Sglebius			ng_source_store_output_ifp(sc, ifname);
415144674Sglebius			break;
416144674Sglebius		    }
417153690Sglebius		case NGM_SOURCE_SETPPS:
418153690Sglebius		    {
419153690Sglebius			uint32_t pps;
420153690Sglebius
421153690Sglebius			if (msg->header.arglen != sizeof(uint32_t)) {
422153690Sglebius				error = EINVAL;
423153690Sglebius				break;
424153690Sglebius			}
425153690Sglebius
426153690Sglebius			pps = *(uint32_t *)msg->data;
427153690Sglebius
428153690Sglebius			sc->stats.maxPps = pps;
429153690Sglebius
430153690Sglebius			break;
431153690Sglebius		    }
432167156Semaste		case NGM_SOURCE_SET_TIMESTAMP:
433167156Semaste		    {
434167156Semaste			struct ng_source_embed_info *embed;
435167156Semaste
436167168Semaste			if (msg->header.arglen != sizeof(*embed)) {
437167168Semaste				error = EINVAL;
438167168Semaste				goto done;
439167168Semaste			}
440167156Semaste			embed = (struct ng_source_embed_info *)msg->data;
441167156Semaste			bcopy(embed, &sc->embed_timestamp, sizeof(*embed));
442167156Semaste
443167156Semaste			break;
444167156Semaste		    }
445167156Semaste		case NGM_SOURCE_GET_TIMESTAMP:
446167156Semaste		    {
447167156Semaste			struct ng_source_embed_info *embed;
448167156Semaste
449167156Semaste			NG_MKRESPONSE(resp, msg, sizeof(*embed), M_DONTWAIT);
450167156Semaste			if (resp == NULL) {
451167156Semaste				error = ENOMEM;
452167156Semaste				goto done;
453167156Semaste			}
454167156Semaste			embed = (struct ng_source_embed_info *)resp->data;
455167156Semaste			bcopy(&sc->embed_timestamp, embed, sizeof(*embed));
456167156Semaste
457167156Semaste			break;
458167156Semaste		    }
459167160Semaste		case NGM_SOURCE_SET_COUNTER:
460167160Semaste		    {
461167160Semaste			struct ng_source_embed_cnt_info *embed;
462167160Semaste
463167168Semaste			if (msg->header.arglen != sizeof(*embed)) {
464167168Semaste				error = EINVAL;
465167168Semaste				goto done;
466167168Semaste			}
467167160Semaste			embed = (struct ng_source_embed_cnt_info *)msg->data;
468167160Semaste			if (embed->index >= NG_SOURCE_COUNTERS ||
469167160Semaste			    !(embed->width == 1 || embed->width == 2 ||
470167160Semaste			    embed->width == 4)) {
471167160Semaste				error = EINVAL;
472167160Semaste				goto done;
473167160Semaste			}
474167160Semaste			bcopy(embed, &sc->embed_counter[embed->index],
475167160Semaste			    sizeof(*embed));
476167160Semaste
477167160Semaste			break;
478167160Semaste		    }
479167160Semaste		case NGM_SOURCE_GET_COUNTER:
480167160Semaste		    {
481167160Semaste			uint8_t index = *(uint8_t *)msg->data;
482167160Semaste			struct ng_source_embed_cnt_info *embed;
483167160Semaste
484167160Semaste			if (index >= NG_SOURCE_COUNTERS) {
485167160Semaste				error = EINVAL;
486167160Semaste				goto done;
487167160Semaste			}
488167160Semaste			NG_MKRESPONSE(resp, msg, sizeof(*embed), M_DONTWAIT);
489167160Semaste			if (resp == NULL) {
490167160Semaste				error = ENOMEM;
491167160Semaste				goto done;
492167160Semaste			}
493167160Semaste			embed = (struct ng_source_embed_cnt_info *)resp->data;
494167160Semaste			bcopy(&sc->embed_counter[index], embed, sizeof(*embed));
495167160Semaste
496167160Semaste			break;
497167160Semaste		    }
498106266Sjulian		default:
499106266Sjulian			error = EINVAL;
500106266Sjulian			break;
501106266Sjulian		}
502106266Sjulian		break;
503106435Sjulian	case NGM_ETHER_COOKIE:
504106435Sjulian		if (!(msg->header.flags & NGF_RESP)) {
505106435Sjulian			error = EINVAL;
506106435Sjulian			break;
507106435Sjulian		}
508106435Sjulian		switch (msg->header.cmd) {
509144674Sglebius		case NGM_ETHER_GET_IFNAME:
510144674Sglebius		    {
511144674Sglebius			char *ifname = (char *)msg->data;
512144674Sglebius
513144674Sglebius			if (msg->header.arglen < 2) {
514144674Sglebius				error = EINVAL;
515144674Sglebius				break;
516144674Sglebius			}
517144674Sglebius
518144674Sglebius			if (ng_source_store_output_ifp(sc, ifname) == 0)
519106435Sjulian				ng_source_set_autosrc(sc, 0);
520106435Sjulian			break;
521144674Sglebius		    }
522106435Sjulian		default:
523106435Sjulian			error = EINVAL;
524106435Sjulian		}
525106435Sjulian		break;
526106266Sjulian	default:
527106266Sjulian		error = EINVAL;
528106266Sjulian		break;
529106266Sjulian	}
530106266Sjulian
531106266Sjuliandone:
532144674Sglebius	/* Take care of synchronous response, if any. */
533106321Sjulian	NG_RESPOND_MSG(error, node, item, resp);
534144674Sglebius	/* Free the message and return. */
535106321Sjulian	NG_FREE_MSG(msg);
536106266Sjulian	return (error);
537106266Sjulian}
538106266Sjulian
539106266Sjulian/*
540106266Sjulian * Receive data on a hook
541106266Sjulian *
542106266Sjulian * If data comes in the input hook, enqueue it on the send queue.
543106266Sjulian * If data comes in the output hook, discard it.
544106266Sjulian */
545106266Sjulianstatic int
546106321Sjulianng_source_rcvdata(hook_p hook, item_p item)
547106266Sjulian{
548144674Sglebius	sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
549144674Sglebius	struct mbuf *m;
550106266Sjulian	int error = 0;
551106266Sjulian
552106321Sjulian	NGI_GET_M(item, m);
553106321Sjulian	NG_FREE_ITEM(item);
554106266Sjulian
555106266Sjulian	/* Which hook? */
556144674Sglebius	if (hook == sc->output) {
557106266Sjulian		/* discard */
558106321Sjulian		NG_FREE_M(m);
559106266Sjulian		return (error);
560106266Sjulian	}
561144674Sglebius	KASSERT(hook == sc->input, ("%s: no hook!", __func__));
562106266Sjulian
563144674Sglebius	/* Enqueue packet. */
564106266Sjulian	/* XXX should we check IF_QFULL() ? */
565125031Sharti	_IF_ENQUEUE(&sc->snd_queue, m);
566106266Sjulian	sc->queueOctets += m->m_pkthdr.len;
567167160Semaste	sc->last_packet = m;
568106266Sjulian
569106266Sjulian	return (0);
570106266Sjulian}
571106266Sjulian
572106266Sjulian/*
573106266Sjulian * Shutdown processing
574106266Sjulian */
575106266Sjulianstatic int
576106266Sjulianng_source_rmnode(node_p node)
577106266Sjulian{
578144674Sglebius	sc_p sc = NG_NODE_PRIVATE(node);
579106266Sjulian
580106266Sjulian	ng_source_stop(sc);
581106266Sjulian	ng_source_clr_data(sc);
582106321Sjulian	NG_NODE_SET_PRIVATE(node, NULL);
583106321Sjulian	NG_NODE_UNREF(node);
584125077Sharti	free(sc, M_NETGRAPH);
585144674Sglebius
586106266Sjulian	return (0);
587106266Sjulian}
588106266Sjulian
589106266Sjulian/*
590106266Sjulian * Hook disconnection
591106266Sjulian */
592106266Sjulianstatic int
593106266Sjulianng_source_disconnect(hook_p hook)
594106266Sjulian{
595106319Sjulian	sc_p sc;
596106266Sjulian
597106321Sjulian	sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
598125077Sharti	KASSERT(sc != NULL, ("%s: null node private", __func__));
599144674Sglebius	if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hook == sc->output)
600106321Sjulian		ng_rmnode_self(NG_HOOK_NODE(hook));
601106266Sjulian	return (0);
602106266Sjulian}
603106266Sjulian
604106266Sjulian/*
605218909Sbrucec * Set sc->output_ifp to point to the struct ifnet of the interface
606106435Sjulian * reached via our output hook.
607106435Sjulian */
608106435Sjulianstatic int
609144674Sglebiusng_source_store_output_ifp(sc_p sc, char *ifname)
610106435Sjulian{
611106435Sjulian	struct ifnet *ifp;
612106435Sjulian	int s;
613106266Sjulian
614144674Sglebius	ifp = ifunit(ifname);
615106266Sjulian
616106266Sjulian	if (ifp == NULL) {
617183225Szec		printf("%s: can't find interface %s\n", __func__, ifname);
618106266Sjulian		return (EINVAL);
619106266Sjulian	}
620106266Sjulian	sc->output_ifp = ifp;
621106266Sjulian
622106266Sjulian#if 1
623106266Sjulian	/* XXX mucking with a drivers ifqueue size is ugly but we need it
624106266Sjulian	 * to queue a lot of packets to get close to line rate on a gigabit
625106266Sjulian	 * interface with small packets.
626106266Sjulian	 * XXX we should restore the original value at stop or disconnect
627106266Sjulian	 */
628106266Sjulian	s = splimp();		/* XXX is this required? */
629125077Sharti	if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN) {
630106266Sjulian		printf("ng_source: changing ifq_maxlen from %d to %d\n",
631106319Sjulian		    ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN);
632106266Sjulian		ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN;
633106266Sjulian	}
634106266Sjulian	splx(s);
635106266Sjulian#endif
636106266Sjulian	return (0);
637106266Sjulian}
638106266Sjulian
639106266Sjulian/*
640106266Sjulian * Set the attached ethernet node's ethernet source address override flag.
641106266Sjulian */
642106266Sjulianstatic int
643144674Sglebiusng_source_set_autosrc(sc_p sc, uint32_t flag)
644106266Sjulian{
645106266Sjulian	struct ng_mesg *msg;
646106266Sjulian	int error = 0;
647106266Sjulian
648106266Sjulian	NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC,
649144674Sglebius	    sizeof (uint32_t), M_NOWAIT);
650106266Sjulian	if (msg == NULL)
651106266Sjulian		return(ENOBUFS);
652106266Sjulian
653144674Sglebius	*(uint32_t *)msg->data = flag;
654144674Sglebius	NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output, 0);
655106266Sjulian	return (error);
656106266Sjulian}
657106266Sjulian
658106266Sjulian/*
659106266Sjulian * Clear out the data we've queued
660106266Sjulian */
661106266Sjulianstatic void
662106266Sjulianng_source_clr_data (sc_p sc)
663106266Sjulian{
664106266Sjulian	struct mbuf *m;
665106266Sjulian
666106266Sjulian	for (;;) {
667125031Sharti		_IF_DEQUEUE(&sc->snd_queue, m);
668106266Sjulian		if (m == NULL)
669106266Sjulian			break;
670106266Sjulian		NG_FREE_M(m);
671106266Sjulian	}
672106266Sjulian	sc->queueOctets = 0;
673167168Semaste	sc->last_packet = NULL;
674106266Sjulian}
675106266Sjulian
676106266Sjulian/*
677106266Sjulian * Start sending queued data out the output hook
678106266Sjulian */
679144674Sglebiusstatic int
680144674Sglebiusng_source_start(sc_p sc, uint64_t packets)
681106266Sjulian{
682144674Sglebius	if (sc->output_ifp == NULL) {
683144674Sglebius		printf("ng_source: start without iface configured\n");
684144674Sglebius		return (ENXIO);
685144674Sglebius	}
686144674Sglebius
687144674Sglebius	if (sc->node->nd_flags & NG_SOURCE_ACTIVE)
688144674Sglebius		return (EBUSY);
689144674Sglebius
690144674Sglebius	sc->node->nd_flags |= NG_SOURCE_ACTIVE;
691144674Sglebius
692144674Sglebius	sc->packets = packets;
693144674Sglebius	timevalclear(&sc->stats.elapsedTime);
694144674Sglebius	timevalclear(&sc->stats.endTime);
695144674Sglebius	getmicrotime(&sc->stats.startTime);
696153690Sglebius	getmicrotime(&sc->stats.lastTime);
697144674Sglebius	ng_callout(&sc->intr_ch, sc->node, NULL, 0,
698144674Sglebius	    ng_source_intr, sc, 0);
699144674Sglebius
700144674Sglebius	return (0);
701106266Sjulian}
702106266Sjulian
703106266Sjulian/*
704106266Sjulian * Stop sending queued data out the output hook
705106266Sjulian */
706106266Sjulianstatic void
707144674Sglebiusng_source_stop(sc_p sc)
708106266Sjulian{
709144674Sglebius	ng_uncallout(&sc->intr_ch, sc->node);
710144674Sglebius	sc->node->nd_flags &= ~NG_SOURCE_ACTIVE;
711144674Sglebius	getmicrotime(&sc->stats.endTime);
712144674Sglebius	sc->stats.elapsedTime = sc->stats.endTime;
713144674Sglebius	timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime);
714106266Sjulian}
715106266Sjulian
716106266Sjulian/*
717106266Sjulian * While active called every NG_SOURCE_INTR_TICKS ticks.
718106266Sjulian * Sends as many packets as the interface connected to our
719106266Sjulian * output hook is able to enqueue.
720106266Sjulian */
721106266Sjulianstatic void
722125243Sharting_source_intr(node_p node, hook_p hook, void *arg1, int arg2)
723106266Sjulian{
724125243Sharti	sc_p sc = (sc_p)arg1;
725106266Sjulian	struct ifqueue *ifq;
726106266Sjulian	int packets;
727106266Sjulian
728125077Sharti	KASSERT(sc != NULL, ("%s: null node private", __func__));
729106266Sjulian
730144674Sglebius	if (sc->packets == 0 || sc->output == NULL
731106321Sjulian	    || (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) {
732106266Sjulian		ng_source_stop(sc);
733106266Sjulian		return;
734106266Sjulian	}
735106266Sjulian
736125033Sharti	if (sc->output_ifp != NULL) {
737141745Sru		ifq = (struct ifqueue *)&sc->output_ifp->if_snd;
738125033Sharti		packets = ifq->ifq_maxlen - ifq->ifq_len;
739125033Sharti	} else
740125033Sharti		packets = sc->snd_queue.ifq_len;
741125033Sharti
742153690Sglebius	if (sc->stats.maxPps != 0) {
743153690Sglebius		struct timeval	now, elapsed;
744153690Sglebius		uint64_t	usec;
745153690Sglebius		int		maxpkt;
746153690Sglebius
747153690Sglebius		getmicrotime(&now);
748153690Sglebius		elapsed = now;
749153690Sglebius		timevalsub(&elapsed, &sc->stats.lastTime);
750153690Sglebius		usec = elapsed.tv_sec * 1000000 + elapsed.tv_usec;
751153690Sglebius		maxpkt = (uint64_t)sc->stats.maxPps * usec / 1000000;
752153690Sglebius		sc->stats.lastTime = now;
753153690Sglebius		if (packets > maxpkt)
754153690Sglebius			packets = maxpkt;
755153690Sglebius	}
756153690Sglebius
757106266Sjulian	ng_source_send(sc, packets, NULL);
758125243Sharti	if (sc->packets == 0)
759106266Sjulian		ng_source_stop(sc);
760125243Sharti	else
761138268Sglebius		ng_callout(&sc->intr_ch, node, NULL, NG_SOURCE_INTR_TICKS,
762137136Sglebius		    ng_source_intr, sc, 0);
763106266Sjulian}
764106266Sjulian
765106266Sjulian/*
766154707Sglebius * Send packets out our output hook.
767106266Sjulian */
768106266Sjulianstatic int
769154707Sglebiusng_source_send(sc_p sc, int tosend, int *sent_p)
770106266Sjulian{
771106266Sjulian	struct mbuf *m, *m2;
772154707Sglebius	int sent;
773106266Sjulian	int error = 0;
774106266Sjulian
775125077Sharti	KASSERT(tosend >= 0, ("%s: negative tosend param", __func__));
776106321Sjulian	KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE,
777125077Sharti	    ("%s: inactive node", __func__));
778106266Sjulian
779144674Sglebius	if ((uint64_t)tosend > sc->packets)
780106266Sjulian		tosend = sc->packets;
781106266Sjulian
782154707Sglebius	/* Go through the queue sending packets one by one. */
783106266Sjulian	for (sent = 0; error == 0 && sent < tosend; ++sent) {
784125031Sharti		_IF_DEQUEUE(&sc->snd_queue, m);
785106266Sjulian		if (m == NULL)
786106266Sjulian			break;
787106266Sjulian
788167156Semaste		/* Duplicate and modify the packet. */
789167156Semaste		error = ng_source_dup_mod(sc, m, &m2);
790167156Semaste		if (error) {
791167156Semaste			if (error == ENOBUFS)
792167156Semaste				_IF_PREPEND(&sc->snd_queue, m);
793167156Semaste			else
794167156Semaste				_IF_ENQUEUE(&sc->snd_queue, m);
795106266Sjulian			break;
796106266Sjulian		}
797106266Sjulian
798144674Sglebius		/* Re-enqueue the original packet for us. */
799125031Sharti		_IF_ENQUEUE(&sc->snd_queue, m);
800106266Sjulian
801154707Sglebius		sc->stats.outFrames++;
802154707Sglebius		sc->stats.outOctets += m2->m_pkthdr.len;
803154707Sglebius		NG_SEND_DATA_ONLY(error, sc->output, m2);
804154707Sglebius		if (error)
805106266Sjulian			break;
806106266Sjulian	}
807106266Sjulian
808106266Sjulian	sc->packets -= sent;
809106266Sjulian	if (sent_p != NULL)
810106266Sjulian		*sent_p = sent;
811106266Sjulian	return (error);
812106266Sjulian}
813167156Semaste
814167156Semaste/*
815167156Semaste * Modify packet in 'm' by changing 'len' bytes starting at 'offset'
816167156Semaste * to data in 'cp'.
817167156Semaste *
818167156Semaste * The packet data in 'm' must be in a contiguous buffer in a single mbuf.
819167156Semaste */
820167156Semastestatic void
821167156Semasteng_source_packet_mod(sc_p sc, struct mbuf *m, int offset, int len, caddr_t cp,
822167156Semaste    int flags)
823167156Semaste{
824167156Semaste	if (len == 0)
825167156Semaste		return;
826167156Semaste
827167156Semaste	/* Can't modify beyond end of packet. */
828167156Semaste	/* TODO: Pad packet for this case. */
829167156Semaste	if (offset + len > m->m_len)
830167156Semaste		return;
831167156Semaste
832167156Semaste	bcopy(cp, mtod_off(m, offset, caddr_t), len);
833167156Semaste}
834167156Semaste
835167160Semastestatic void
836167160Semasteng_source_mod_counter(sc_p sc, struct ng_source_embed_cnt_info *cnt,
837167160Semaste    struct mbuf *m, int increment)
838167160Semaste{
839167160Semaste	caddr_t cp;
840167160Semaste	uint32_t val;
841167160Semaste
842167160Semaste	val = htonl(cnt->next_val);
843167160Semaste	cp = (caddr_t)&val + sizeof(val) - cnt->width;
844167160Semaste	ng_source_packet_mod(sc, m, cnt->offset, cnt->width, cp, cnt->flags);
845167160Semaste
846167160Semaste	if (increment) {
847167160Semaste		cnt->next_val += increment;
848167160Semaste
849167160Semaste		if (increment > 0 && cnt->next_val > cnt->max_val) {
850167160Semaste			cnt->next_val = cnt->min_val - 1 +
851167160Semaste			    (cnt->next_val - cnt->max_val);
852167160Semaste			if (cnt->next_val > cnt->max_val)
853167160Semaste				cnt->next_val = cnt->max_val;
854167160Semaste		} else if (increment < 0 && cnt->next_val < cnt->min_val) {
855167160Semaste			cnt->next_val = cnt->max_val + 1 +
856167160Semaste			    (cnt->next_val - cnt->min_val);
857167160Semaste			if (cnt->next_val < cnt->min_val)
858167160Semaste				cnt->next_val = cnt->max_val;
859167160Semaste		}
860167160Semaste	}
861167160Semaste}
862167160Semaste
863167156Semastestatic int
864167156Semasteng_source_dup_mod(sc_p sc, struct mbuf *m0, struct mbuf **m_ptr)
865167156Semaste{
866167156Semaste	struct mbuf *m;
867167160Semaste	struct ng_source_embed_cnt_info *cnt;
868167156Semaste	struct ng_source_embed_info *ts;
869167156Semaste	int modify;
870167156Semaste	int error = 0;
871167160Semaste	int i, increment;
872167156Semaste
873167156Semaste	/* Are we going to modify packets? */
874167156Semaste	modify = sc->embed_timestamp.flags & NGM_SOURCE_EMBED_ENABLE;
875167160Semaste	for (i = 0; !modify && i < NG_SOURCE_COUNTERS; ++i)
876167160Semaste		modify = sc->embed_counter[i].flags & NGM_SOURCE_EMBED_ENABLE;
877167156Semaste
878167156Semaste	/* Duplicate the packet. */
879167156Semaste	if (modify)
880167156Semaste		m = m_dup(m0, M_DONTWAIT);
881167156Semaste	else
882167156Semaste		m = m_copypacket(m0, M_DONTWAIT);
883167156Semaste	if (m == NULL) {
884167156Semaste		error = ENOBUFS;
885167156Semaste		goto done;
886167156Semaste	}
887167156Semaste	*m_ptr = m;
888167156Semaste
889167156Semaste	if (!modify)
890167156Semaste		goto done;
891167156Semaste
892167156Semaste	/* Modify the copied packet for sending. */
893167156Semaste	KASSERT(M_WRITABLE(m), ("%s: packet not writable", __func__));
894167156Semaste
895167160Semaste	for (i = 0; i < NG_SOURCE_COUNTERS; ++i) {
896167160Semaste		cnt = &sc->embed_counter[i];
897167160Semaste		if (cnt->flags & NGM_SOURCE_EMBED_ENABLE) {
898167160Semaste			if ((cnt->flags & NGM_SOURCE_INC_CNT_PER_LIST) == 0 ||
899167160Semaste			    sc->last_packet == m0)
900167160Semaste				increment = cnt->increment;
901167160Semaste			else
902167160Semaste				increment = 0;
903167160Semaste			ng_source_mod_counter(sc, cnt, m, increment);
904167160Semaste		}
905167160Semaste	}
906167160Semaste
907167156Semaste	ts = &sc->embed_timestamp;
908167156Semaste	if (ts->flags & NGM_SOURCE_EMBED_ENABLE) {
909167156Semaste		struct timeval now;
910167156Semaste		getmicrotime(&now);
911167156Semaste		now.tv_sec = htonl(now.tv_sec);
912167156Semaste		now.tv_usec = htonl(now.tv_usec);
913167156Semaste		ng_source_packet_mod(sc, m, ts->offset, sizeof (now),
914167156Semaste		    (caddr_t)&now, ts->flags);
915167156Semaste	}
916167156Semaste
917167156Semastedone:
918167156Semaste	return(error);
919167156Semaste}
920