ng_pipe.c revision 182734
1182734Sjulian/*
2182734Sjulian * Copyright (c) 2004-2008 University of Zagreb
3182734Sjulian * Copyright (c) 2007-2008 FreeBSD Foundation
4182734Sjulian *
5182734Sjulian * This software was developed by the University of Zagreb and the
6182734Sjulian * FreeBSD Foundation under sponsorship by the Stichting NLnet and the
7182734Sjulian * FreeBSD Foundation.
8182734Sjulian *
9182734Sjulian * Redistribution and use in source and binary forms, with or without
10182734Sjulian * modification, are permitted provided that the following conditions
11182734Sjulian * are met:
12182734Sjulian * 1. Redistributions of source code must retain the above copyright
13182734Sjulian *    notice, this list of conditions and the following disclaimer.
14182734Sjulian * 2. Redistributions in binary form must reproduce the above copyright
15182734Sjulian *    notice, this list of conditions and the following disclaimer in the
16182734Sjulian *    documentation and/or other materials provided with the distribution.
17182734Sjulian *
18182734Sjulian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19182734Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20182734Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21182734Sjulian * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22182734Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23182734Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24182734Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25182734Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26182734Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27182734Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28182734Sjulian * SUCH DAMAGE.
29182734Sjulian *
30182734Sjulian * $FreeBSD: head/sys/netgraph/ng_pipe.c 182734 2008-09-03 18:17:45Z julian $
31182734Sjulian */
32182734Sjulian
33182734Sjulian/*
34182734Sjulian * This node permits simple traffic shaping by emulating bandwidth
35182734Sjulian * and delay, as well as random packet losses.
36182734Sjulian * The node has two hooks, upper and lower. Traffic flowing from upper to
37182734Sjulian * lower hook is referenced as downstream, and vice versa. Parameters for
38182734Sjulian * both directions can be set separately, except for delay.
39182734Sjulian */
40182734Sjulian
41182734Sjulian
42182734Sjulian#include <sys/param.h>
43182734Sjulian#include <sys/errno.h>
44182734Sjulian#include <sys/systm.h>
45182734Sjulian#include <sys/kernel.h>
46182734Sjulian#include <sys/malloc.h>
47182734Sjulian#include <sys/mbuf.h>
48182734Sjulian#include <sys/time.h>
49182734Sjulian#include <sys/vimage.h>
50182734Sjulian
51182734Sjulian#include <vm/uma.h>
52182734Sjulian
53182734Sjulian#include <netinet/in.h>
54182734Sjulian#include <netinet/in_systm.h>
55182734Sjulian#include <netinet/ip.h>
56182734Sjulian
57182734Sjulian#include <netgraph/ng_message.h>
58182734Sjulian#include <netgraph/netgraph.h>
59182734Sjulian#include <netgraph/ng_parse.h>
60182734Sjulian#include <netgraph/ng_pipe.h>
61182734Sjulian
62182734Sjulianstatic MALLOC_DEFINE(M_NG_PIPE, "ng_pipe", "ng_pipe");
63182734Sjulian
64182734Sjulianstruct mtx ng_pipe_giant;
65182734Sjulian
66182734Sjulian/* Packet header struct */
67182734Sjulianstruct ngp_hdr {
68182734Sjulian	TAILQ_ENTRY(ngp_hdr)	ngp_link;	/* next pkt in queue */
69182734Sjulian	struct timeval		when;		/* this packet's due time */
70182734Sjulian	struct mbuf		*m;		/* ptr to the packet data */
71182734Sjulian};
72182734SjulianTAILQ_HEAD(p_head, ngp_hdr);
73182734Sjulian
74182734Sjulian/* FIFO queue struct */
75182734Sjulianstruct ngp_fifo {
76182734Sjulian	TAILQ_ENTRY(ngp_fifo)	fifo_le;	/* list of active queues only */
77182734Sjulian	struct p_head		packet_head;	/* FIFO queue head */
78182734Sjulian	u_int32_t		hash;		/* flow signature */
79182734Sjulian	struct timeval		vtime;		/* virtual time, for WFQ */
80182734Sjulian	u_int32_t		rr_deficit;	/* for DRR */
81182734Sjulian	u_int32_t		packets;	/* # of packets in this queue */
82182734Sjulian};
83182734Sjulian
84182734Sjulian/* Per hook info */
85182734Sjulianstruct hookinfo {
86182734Sjulian	hook_p			hook;
87182734Sjulian	int			noqueue;	/* bypass any processing */
88182734Sjulian	TAILQ_HEAD(, ngp_fifo)	fifo_head;	/* FIFO queues */
89182734Sjulian	TAILQ_HEAD(, ngp_hdr)	qout_head;	/* delay queue head */
90182734Sjulian	LIST_ENTRY(hookinfo)	active_le;	/* active hooks */
91182734Sjulian	struct timeval		qin_utime;
92182734Sjulian	struct ng_pipe_hookcfg	cfg;
93182734Sjulian	struct ng_pipe_hookrun	run;
94182734Sjulian	struct ng_pipe_hookstat	stats;
95182734Sjulian	uint64_t		*ber_p;		/* loss_p(BER,psize) map */
96182734Sjulian};
97182734Sjulian
98182734Sjulian/* Per node info */
99182734Sjulianstruct node_priv {
100182734Sjulian	u_int64_t		delay;
101182734Sjulian	u_int32_t		overhead;
102182734Sjulian	u_int32_t		header_offset;
103182734Sjulian	struct hookinfo		lower;
104182734Sjulian	struct hookinfo		upper;
105182734Sjulian};
106182734Sjuliantypedef struct node_priv *priv_p;
107182734Sjulian
108182734Sjulian/* Macro for calculating the virtual time for packet dequeueing in WFQ */
109182734Sjulian#define FIFO_VTIME_SORT(plen)						\
110182734Sjulian	if (hinfo->cfg.wfq && hinfo->cfg.bandwidth) {			\
111182734Sjulian		ngp_f->vtime.tv_usec = now->tv_usec + ((uint64_t) (plen) \
112182734Sjulian			+ priv->overhead ) * hinfo->run.fifo_queues *	\
113182734Sjulian			8000000 / hinfo->cfg.bandwidth;			\
114182734Sjulian		ngp_f->vtime.tv_sec = now->tv_sec +			\
115182734Sjulian			ngp_f->vtime.tv_usec / 1000000;			\
116182734Sjulian		ngp_f->vtime.tv_usec = ngp_f->vtime.tv_usec % 1000000;	\
117182734Sjulian		TAILQ_FOREACH(ngp_f1, &hinfo->fifo_head, fifo_le)	\
118182734Sjulian			if (ngp_f1->vtime.tv_sec > ngp_f->vtime.tv_sec || \
119182734Sjulian			    (ngp_f1->vtime.tv_sec == ngp_f->vtime.tv_sec && \
120182734Sjulian			    ngp_f1->vtime.tv_usec > ngp_f->vtime.tv_usec)) \
121182734Sjulian				break;					\
122182734Sjulian		if (ngp_f1 == NULL)					\
123182734Sjulian			TAILQ_INSERT_TAIL(&hinfo->fifo_head, ngp_f, fifo_le); \
124182734Sjulian		else							\
125182734Sjulian			TAILQ_INSERT_BEFORE(ngp_f1, ngp_f, fifo_le);	\
126182734Sjulian	} else								\
127182734Sjulian		TAILQ_INSERT_TAIL(&hinfo->fifo_head, ngp_f, fifo_le);	\
128182734Sjulian
129182734Sjulian
130182734Sjulianstatic void	parse_cfg(struct ng_pipe_hookcfg *, struct ng_pipe_hookcfg *,
131182734Sjulian			struct hookinfo *, priv_p);
132182734Sjulianstatic void	pipe_dequeue(struct hookinfo *, struct timeval *);
133182734Sjulianstatic void	pipe_scheduler(void *);
134182734Sjulianstatic void	pipe_poll(void);
135182734Sjulianstatic int	ngp_modevent(module_t, int, void *);
136182734Sjulian
137182734Sjulian/* linked list of active "pipe" hooks */
138182734Sjulianstatic LIST_HEAD(, hookinfo) active_head;
139182734Sjulianstatic int active_gen_id = 0;
140182734Sjulian
141182734Sjulian/* timeout handle for pipe_scheduler */
142182734Sjulianstatic struct callout polling_timer;
143182734Sjulian
144182734Sjulian/* zone for storing ngp_hdr-s */
145182734Sjulianstatic uma_zone_t ngp_zone;
146182734Sjulian
147182734Sjulian/* Netgraph methods */
148182734Sjulianstatic ng_constructor_t	ngp_constructor;
149182734Sjulianstatic ng_rcvmsg_t	ngp_rcvmsg;
150182734Sjulianstatic ng_shutdown_t	ngp_shutdown;
151182734Sjulianstatic ng_newhook_t	ngp_newhook;
152182734Sjulianstatic ng_rcvdata_t	ngp_rcvdata;
153182734Sjulianstatic ng_disconnect_t	ngp_disconnect;
154182734Sjulian
155182734Sjulian/* Parse type for struct ng_pipe_hookstat */
156182734Sjulianstatic const struct ng_parse_struct_field
157182734Sjulian	ng_pipe_hookstat_type_fields[] = NG_PIPE_HOOKSTAT_INFO;
158182734Sjulianstatic const struct ng_parse_type ng_pipe_hookstat_type = {
159182734Sjulian	&ng_parse_struct_type,
160182734Sjulian	&ng_pipe_hookstat_type_fields
161182734Sjulian};
162182734Sjulian
163182734Sjulian/* Parse type for struct ng_pipe_stats */
164182734Sjulianstatic const struct ng_parse_struct_field ng_pipe_stats_type_fields[] =
165182734Sjulian	NG_PIPE_STATS_INFO(&ng_pipe_hookstat_type);
166182734Sjulianstatic const struct ng_parse_type ng_pipe_stats_type = {
167182734Sjulian	&ng_parse_struct_type,
168182734Sjulian	&ng_pipe_stats_type_fields
169182734Sjulian};
170182734Sjulian
171182734Sjulian/* Parse type for struct ng_pipe_hookrun */
172182734Sjulianstatic const struct ng_parse_struct_field
173182734Sjulian	ng_pipe_hookrun_type_fields[] = NG_PIPE_HOOKRUN_INFO;
174182734Sjulianstatic const struct ng_parse_type ng_pipe_hookrun_type = {
175182734Sjulian	&ng_parse_struct_type,
176182734Sjulian	&ng_pipe_hookrun_type_fields
177182734Sjulian};
178182734Sjulian
179182734Sjulian/* Parse type for struct ng_pipe_run */
180182734Sjulianstatic const struct ng_parse_struct_field
181182734Sjulian	ng_pipe_run_type_fields[] = NG_PIPE_RUN_INFO(&ng_pipe_hookrun_type);
182182734Sjulianstatic const struct ng_parse_type ng_pipe_run_type = {
183182734Sjulian	&ng_parse_struct_type,
184182734Sjulian	&ng_pipe_run_type_fields
185182734Sjulian};
186182734Sjulian
187182734Sjulian/* Parse type for struct ng_pipe_hookcfg */
188182734Sjulianstatic const struct ng_parse_struct_field
189182734Sjulian	ng_pipe_hookcfg_type_fields[] = NG_PIPE_HOOKCFG_INFO;
190182734Sjulianstatic const struct ng_parse_type ng_pipe_hookcfg_type = {
191182734Sjulian	&ng_parse_struct_type,
192182734Sjulian	&ng_pipe_hookcfg_type_fields
193182734Sjulian};
194182734Sjulian
195182734Sjulian/* Parse type for struct ng_pipe_cfg */
196182734Sjulianstatic const struct ng_parse_struct_field
197182734Sjulian	ng_pipe_cfg_type_fields[] = NG_PIPE_CFG_INFO(&ng_pipe_hookcfg_type);
198182734Sjulianstatic const struct ng_parse_type ng_pipe_cfg_type = {
199182734Sjulian	&ng_parse_struct_type,
200182734Sjulian	&ng_pipe_cfg_type_fields
201182734Sjulian};
202182734Sjulian
203182734Sjulian/* List of commands and how to convert arguments to/from ASCII */
204182734Sjulianstatic const struct ng_cmdlist ngp_cmds[] = {
205182734Sjulian	{
206182734Sjulian		.cookie =	NGM_PIPE_COOKIE,
207182734Sjulian		.cmd =		NGM_PIPE_GET_STATS,
208182734Sjulian		.name = 	"getstats",
209182734Sjulian		.respType =	 &ng_pipe_stats_type
210182734Sjulian	},
211182734Sjulian	{
212182734Sjulian		.cookie =	NGM_PIPE_COOKIE,
213182734Sjulian		.cmd =		NGM_PIPE_CLR_STATS,
214182734Sjulian		.name =		"clrstats"
215182734Sjulian	},
216182734Sjulian	{
217182734Sjulian		.cookie =	NGM_PIPE_COOKIE,
218182734Sjulian		.cmd =		NGM_PIPE_GETCLR_STATS,
219182734Sjulian		.name =		"getclrstats",
220182734Sjulian		.respType =	&ng_pipe_stats_type
221182734Sjulian	},
222182734Sjulian	{
223182734Sjulian		.cookie =	NGM_PIPE_COOKIE,
224182734Sjulian		.cmd =		NGM_PIPE_GET_RUN,
225182734Sjulian		.name =		"getrun",
226182734Sjulian		.respType =	&ng_pipe_run_type
227182734Sjulian	},
228182734Sjulian	{
229182734Sjulian		.cookie =	NGM_PIPE_COOKIE,
230182734Sjulian		.cmd =		NGM_PIPE_GET_CFG,
231182734Sjulian		.name =		"getcfg",
232182734Sjulian		.respType =	&ng_pipe_cfg_type
233182734Sjulian	},
234182734Sjulian	{
235182734Sjulian		.cookie =	NGM_PIPE_COOKIE,
236182734Sjulian		.cmd =		NGM_PIPE_SET_CFG,
237182734Sjulian		.name =		"setcfg",
238182734Sjulian		.mesgType =	&ng_pipe_cfg_type,
239182734Sjulian	},
240182734Sjulian	{ 0 }
241182734Sjulian};
242182734Sjulian
243182734Sjulian/* Netgraph type descriptor */
244182734Sjulianstatic struct ng_type ng_pipe_typestruct = {
245182734Sjulian	.version =	NG_ABI_VERSION,
246182734Sjulian	.name =		NG_PIPE_NODE_TYPE,
247182734Sjulian	.mod_event =	ngp_modevent,
248182734Sjulian	.constructor =	ngp_constructor,
249182734Sjulian	.shutdown =	ngp_shutdown,
250182734Sjulian	.rcvmsg =	ngp_rcvmsg,
251182734Sjulian	.newhook =	ngp_newhook,
252182734Sjulian	.rcvdata =	ngp_rcvdata,
253182734Sjulian	.disconnect =	ngp_disconnect,
254182734Sjulian	.cmdlist =	ngp_cmds
255182734Sjulian};
256182734SjulianNETGRAPH_INIT(pipe, &ng_pipe_typestruct);
257182734Sjulian
258182734Sjulian/* Node constructor */
259182734Sjulianstatic int
260182734Sjulianngp_constructor(node_p node)
261182734Sjulian{
262182734Sjulian	priv_p priv;
263182734Sjulian
264182734Sjulian	MALLOC(priv, priv_p, sizeof(*priv), M_NG_PIPE, M_ZERO | M_NOWAIT);
265182734Sjulian	if (priv == NULL)
266182734Sjulian		return (ENOMEM);
267182734Sjulian	NG_NODE_SET_PRIVATE(node, priv);
268182734Sjulian
269182734Sjulian	return (0);
270182734Sjulian}
271182734Sjulian
272182734Sjulian/* Add a hook */
273182734Sjulianstatic int
274182734Sjulianngp_newhook(node_p node, hook_p hook, const char *name)
275182734Sjulian{
276182734Sjulian	const priv_p priv = NG_NODE_PRIVATE(node);
277182734Sjulian	struct hookinfo *hinfo;
278182734Sjulian
279182734Sjulian	if (strcmp(name, NG_PIPE_HOOK_UPPER) == 0) {
280182734Sjulian		bzero(&priv->upper, sizeof(priv->upper));
281182734Sjulian		priv->upper.hook = hook;
282182734Sjulian		NG_HOOK_SET_PRIVATE(hook, &priv->upper);
283182734Sjulian	} else if (strcmp(name, NG_PIPE_HOOK_LOWER) == 0) {
284182734Sjulian		bzero(&priv->lower, sizeof(priv->lower));
285182734Sjulian		priv->lower.hook = hook;
286182734Sjulian		NG_HOOK_SET_PRIVATE(hook, &priv->lower);
287182734Sjulian	} else
288182734Sjulian		return (EINVAL);
289182734Sjulian
290182734Sjulian	/* Load non-zero initial cfg values */
291182734Sjulian	hinfo = NG_HOOK_PRIVATE(hook);
292182734Sjulian	hinfo->cfg.qin_size_limit = 50;
293182734Sjulian	hinfo->cfg.fifo = 1;
294182734Sjulian	hinfo->cfg.droptail = 1;
295182734Sjulian	TAILQ_INIT(&hinfo->fifo_head);
296182734Sjulian	TAILQ_INIT(&hinfo->qout_head);
297182734Sjulian	return (0);
298182734Sjulian}
299182734Sjulian
300182734Sjulian/* Receive a control message */
301182734Sjulianstatic int
302182734Sjulianngp_rcvmsg(node_p node, item_p item, hook_p lasthook)
303182734Sjulian{
304182734Sjulian	const priv_p priv = NG_NODE_PRIVATE(node);
305182734Sjulian	struct ng_mesg *resp = NULL;
306182734Sjulian	struct ng_mesg *msg;
307182734Sjulian	struct ng_pipe_stats *stats;
308182734Sjulian	struct ng_pipe_run *run;
309182734Sjulian	struct ng_pipe_cfg *cfg;
310182734Sjulian	int error = 0;
311182734Sjulian
312182734Sjulian	mtx_lock(&ng_pipe_giant);
313182734Sjulian
314182734Sjulian	NGI_GET_MSG(item, msg);
315182734Sjulian	switch (msg->header.typecookie) {
316182734Sjulian	case NGM_PIPE_COOKIE:
317182734Sjulian		switch (msg->header.cmd) {
318182734Sjulian		case NGM_PIPE_GET_STATS:
319182734Sjulian		case NGM_PIPE_CLR_STATS:
320182734Sjulian		case NGM_PIPE_GETCLR_STATS:
321182734Sjulian			if (msg->header.cmd != NGM_PIPE_CLR_STATS) {
322182734Sjulian				NG_MKRESPONSE(resp, msg,
323182734Sjulian				    sizeof(*stats), M_NOWAIT);
324182734Sjulian				if (resp == NULL) {
325182734Sjulian					error = ENOMEM;
326182734Sjulian					break;
327182734Sjulian				}
328182734Sjulian				stats = (struct ng_pipe_stats *)resp->data;
329182734Sjulian				bcopy(&priv->upper.stats, &stats->downstream,
330182734Sjulian				    sizeof(stats->downstream));
331182734Sjulian				bcopy(&priv->lower.stats, &stats->upstream,
332182734Sjulian				    sizeof(stats->upstream));
333182734Sjulian			}
334182734Sjulian			if (msg->header.cmd != NGM_PIPE_GET_STATS) {
335182734Sjulian				bzero(&priv->upper.stats,
336182734Sjulian				    sizeof(priv->upper.stats));
337182734Sjulian				bzero(&priv->lower.stats,
338182734Sjulian				    sizeof(priv->lower.stats));
339182734Sjulian			}
340182734Sjulian			break;
341182734Sjulian		case NGM_PIPE_GET_RUN:
342182734Sjulian			NG_MKRESPONSE(resp, msg, sizeof(*run), M_NOWAIT);
343182734Sjulian			if (resp == NULL) {
344182734Sjulian				error = ENOMEM;
345182734Sjulian				break;
346182734Sjulian			}
347182734Sjulian			run = (struct ng_pipe_run *)resp->data;
348182734Sjulian			bcopy(&priv->upper.run, &run->downstream,
349182734Sjulian				sizeof(run->downstream));
350182734Sjulian			bcopy(&priv->lower.run, &run->upstream,
351182734Sjulian				sizeof(run->upstream));
352182734Sjulian			break;
353182734Sjulian		case NGM_PIPE_GET_CFG:
354182734Sjulian			NG_MKRESPONSE(resp, msg, sizeof(*cfg), M_NOWAIT);
355182734Sjulian			if (resp == NULL) {
356182734Sjulian				error = ENOMEM;
357182734Sjulian				break;
358182734Sjulian			}
359182734Sjulian			cfg = (struct ng_pipe_cfg *)resp->data;
360182734Sjulian			bcopy(&priv->upper.cfg, &cfg->downstream,
361182734Sjulian				sizeof(cfg->downstream));
362182734Sjulian			bcopy(&priv->lower.cfg, &cfg->upstream,
363182734Sjulian				sizeof(cfg->upstream));
364182734Sjulian			cfg->delay = priv->delay;
365182734Sjulian			cfg->overhead = priv->overhead;
366182734Sjulian			cfg->header_offset = priv->header_offset;
367182734Sjulian			if (cfg->upstream.bandwidth ==
368182734Sjulian			    cfg->downstream.bandwidth) {
369182734Sjulian				cfg->bandwidth = cfg->upstream.bandwidth;
370182734Sjulian				cfg->upstream.bandwidth = 0;
371182734Sjulian				cfg->downstream.bandwidth = 0;
372182734Sjulian			} else
373182734Sjulian				cfg->bandwidth = 0;
374182734Sjulian			break;
375182734Sjulian		case NGM_PIPE_SET_CFG:
376182734Sjulian			cfg = (struct ng_pipe_cfg *)msg->data;
377182734Sjulian			if (msg->header.arglen != sizeof(*cfg)) {
378182734Sjulian				error = EINVAL;
379182734Sjulian				break;
380182734Sjulian			}
381182734Sjulian
382182734Sjulian			if (cfg->delay == -1)
383182734Sjulian				priv->delay = 0;
384182734Sjulian			else if (cfg->delay > 0 && cfg->delay < 10000000)
385182734Sjulian				priv->delay = cfg->delay;
386182734Sjulian
387182734Sjulian			if (cfg->bandwidth == -1) {
388182734Sjulian				priv->upper.cfg.bandwidth = 0;
389182734Sjulian				priv->lower.cfg.bandwidth = 0;
390182734Sjulian				priv->overhead = 0;
391182734Sjulian			} else if (cfg->bandwidth >= 100 &&
392182734Sjulian			    cfg->bandwidth <= 1000000000) {
393182734Sjulian				priv->upper.cfg.bandwidth = cfg->bandwidth;
394182734Sjulian				priv->lower.cfg.bandwidth = cfg->bandwidth;
395182734Sjulian				if (cfg->bandwidth >= 10000000)
396182734Sjulian					priv->overhead = 8+4+12; /* Ethernet */
397182734Sjulian				else
398182734Sjulian					priv->overhead = 10; /* HDLC */
399182734Sjulian			}
400182734Sjulian
401182734Sjulian			if (cfg->overhead == -1)
402182734Sjulian				priv->overhead = 0;
403182734Sjulian			else if (cfg->overhead > 0 && cfg->overhead < 256)
404182734Sjulian				priv->overhead = cfg->overhead;
405182734Sjulian
406182734Sjulian			if (cfg->header_offset == -1)
407182734Sjulian				priv->header_offset = 0;
408182734Sjulian			else if (cfg->header_offset > 0 &&
409182734Sjulian			    cfg->header_offset < 64)
410182734Sjulian				priv->header_offset = cfg->header_offset;
411182734Sjulian
412182734Sjulian			parse_cfg(&priv->upper.cfg, &cfg->downstream,
413182734Sjulian				  &priv->upper, priv);
414182734Sjulian			parse_cfg(&priv->lower.cfg, &cfg->upstream,
415182734Sjulian				  &priv->lower, priv);
416182734Sjulian			break;
417182734Sjulian		default:
418182734Sjulian			error = EINVAL;
419182734Sjulian			break;
420182734Sjulian		}
421182734Sjulian		break;
422182734Sjulian	default:
423182734Sjulian		error = EINVAL;
424182734Sjulian		break;
425182734Sjulian	}
426182734Sjulian	NG_RESPOND_MSG(error, node, item, resp);
427182734Sjulian	NG_FREE_MSG(msg);
428182734Sjulian
429182734Sjulian	mtx_unlock(&ng_pipe_giant);
430182734Sjulian
431182734Sjulian	return (error);
432182734Sjulian}
433182734Sjulian
434182734Sjulianstatic void
435182734Sjulianparse_cfg(struct ng_pipe_hookcfg *current, struct ng_pipe_hookcfg *new,
436182734Sjulian	struct hookinfo *hinfo, priv_p priv)
437182734Sjulian{
438182734Sjulian
439182734Sjulian	if (new->ber == -1) {
440182734Sjulian		current->ber = 0;
441182734Sjulian		if (hinfo->ber_p) {
442182734Sjulian			FREE(hinfo->ber_p, M_NG_PIPE);
443182734Sjulian			hinfo->ber_p = NULL;
444182734Sjulian		}
445182734Sjulian	} else if (new->ber >= 1 && new->ber <= 1000000000000) {
446182734Sjulian		static const uint64_t one = 0x1000000000000; /* = 2^48 */
447182734Sjulian		uint64_t p0, p;
448182734Sjulian		uint32_t fsize, i;
449182734Sjulian
450182734Sjulian		if (hinfo->ber_p == NULL)
451182734Sjulian			MALLOC(hinfo->ber_p, uint64_t *, \
452182734Sjulian				(MAX_FSIZE + MAX_OHSIZE)*sizeof(uint64_t), \
453182734Sjulian				M_NG_PIPE, M_NOWAIT);
454182734Sjulian		current->ber = new->ber;
455182734Sjulian
456182734Sjulian		/*
457182734Sjulian		 * For given BER and each frame size N (in bytes) calculate
458182734Sjulian		 * the probability P_OK that the frame is clean:
459182734Sjulian		 *
460182734Sjulian		 * P_OK(BER,N) = (1 - 1/BER)^(N*8)
461182734Sjulian		 *
462182734Sjulian		 * We use a 64-bit fixed-point format with decimal point
463182734Sjulian		 * positioned between bits 47 and 48.
464182734Sjulian		 */
465182734Sjulian		p0 = one - one / new->ber;
466182734Sjulian		p = one;
467182734Sjulian		for (fsize = 0; fsize < MAX_FSIZE + MAX_OHSIZE; fsize++) {
468182734Sjulian			hinfo->ber_p[fsize] = p;
469182734Sjulian			for (i=0; i<8; i++)
470182734Sjulian				p = (p*(p0&0xffff)>>48) + \
471182734Sjulian				    (p*((p0>>16)&0xffff)>>32) + \
472182734Sjulian				    (p*(p0>>32)>>16);
473182734Sjulian		}
474182734Sjulian	}
475182734Sjulian
476182734Sjulian	if (new->qin_size_limit == -1)
477182734Sjulian		current->qin_size_limit = 0;
478182734Sjulian	else if (new->qin_size_limit >= 5)
479182734Sjulian		current->qin_size_limit = new->qin_size_limit;
480182734Sjulian
481182734Sjulian	if (new->qout_size_limit == -1)
482182734Sjulian		current->qout_size_limit = 0;
483182734Sjulian	else if (new->qout_size_limit >= 5)
484182734Sjulian		current->qout_size_limit = new->qout_size_limit;
485182734Sjulian
486182734Sjulian	if (new->duplicate == -1)
487182734Sjulian		current->duplicate = 0;
488182734Sjulian	else if (new->duplicate > 0 && new->duplicate <= 50)
489182734Sjulian		current->duplicate = new->duplicate;
490182734Sjulian
491182734Sjulian	if (new->fifo) {
492182734Sjulian		current->fifo = 1;
493182734Sjulian		current->wfq = 0;
494182734Sjulian		current->drr = 0;
495182734Sjulian	}
496182734Sjulian
497182734Sjulian	if (new->wfq) {
498182734Sjulian		current->fifo = 0;
499182734Sjulian		current->wfq = 1;
500182734Sjulian		current->drr = 0;
501182734Sjulian	}
502182734Sjulian
503182734Sjulian	if (new->drr) {
504182734Sjulian		current->fifo = 0;
505182734Sjulian		current->wfq = 0;
506182734Sjulian		/* DRR quantum */
507182734Sjulian		if (new->drr >= 32)
508182734Sjulian			current->drr = new->drr;
509182734Sjulian		else
510182734Sjulian			current->drr = 2048;		/* default quantum */
511182734Sjulian	}
512182734Sjulian
513182734Sjulian	if (new->droptail) {
514182734Sjulian		current->droptail = 1;
515182734Sjulian		current->drophead = 0;
516182734Sjulian	}
517182734Sjulian
518182734Sjulian	if (new->drophead) {
519182734Sjulian		current->droptail = 0;
520182734Sjulian		current->drophead = 1;
521182734Sjulian	}
522182734Sjulian
523182734Sjulian	if (new->bandwidth == -1) {
524182734Sjulian		current->bandwidth = 0;
525182734Sjulian		current->fifo = 1;
526182734Sjulian		current->wfq = 0;
527182734Sjulian		current->drr = 0;
528182734Sjulian	} else if (new->bandwidth >= 100 && new->bandwidth <= 1000000000)
529182734Sjulian		current->bandwidth = new->bandwidth;
530182734Sjulian
531182734Sjulian	if (current->bandwidth | priv->delay |
532182734Sjulian	    current->duplicate | current->ber)
533182734Sjulian		hinfo->noqueue = 0;
534182734Sjulian	else
535182734Sjulian		hinfo->noqueue = 1;
536182734Sjulian}
537182734Sjulian
538182734Sjulian/*
539182734Sjulian * Compute a hash signature for a packet. This function suffers from the
540182734Sjulian * NIH sindrome, so probably it would be wise to look around what other
541182734Sjulian * folks have found out to be a good and efficient IP hash function...
542182734Sjulian */
543182734Sjulianstatic int
544182734Sjulianip_hash(struct mbuf *m, int offset)
545182734Sjulian{
546182734Sjulian	u_int64_t i;
547182734Sjulian	struct ip *ip = (struct ip *)(mtod(m, u_char *) + offset);
548182734Sjulian
549182734Sjulian	if (m->m_len < sizeof(struct ip) + offset ||
550182734Sjulian	    ip->ip_v != 4 || ip->ip_hl << 2 != sizeof(struct ip))
551182734Sjulian		return 0;
552182734Sjulian
553182734Sjulian	i = ((u_int64_t) ip->ip_src.s_addr ^
554182734Sjulian	    ((u_int64_t) ip->ip_src.s_addr << 13) ^
555182734Sjulian	    ((u_int64_t) ip->ip_dst.s_addr << 7) ^
556182734Sjulian	    ((u_int64_t) ip->ip_dst.s_addr << 19));
557182734Sjulian	return (i ^ (i >> 32));
558182734Sjulian}
559182734Sjulian
560182734Sjulian/*
561182734Sjulian * Receive data on a hook - both in upstream and downstream direction.
562182734Sjulian * We put the frame on the inbound queue, and try to initiate dequeuing
563182734Sjulian * sequence immediately. If inbound queue is full, discard one frame
564182734Sjulian * depending on dropping policy (from the head or from the tail of the
565182734Sjulian * queue).
566182734Sjulian */
567182734Sjulianstatic int
568182734Sjulianngp_rcvdata(hook_p hook, item_p item)
569182734Sjulian{
570182734Sjulian	struct hookinfo *const hinfo = NG_HOOK_PRIVATE(hook);
571182734Sjulian	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
572182734Sjulian	struct timeval uuptime;
573182734Sjulian	struct timeval *now = &uuptime;
574182734Sjulian	struct ngp_fifo *ngp_f = NULL, *ngp_f1;
575182734Sjulian	struct ngp_hdr *ngp_h = NULL;
576182734Sjulian	struct mbuf *m;
577182734Sjulian	int hash;
578182734Sjulian	int error = 0;
579182734Sjulian
580182734Sjulian	if (hinfo->noqueue) {
581182734Sjulian		struct hookinfo *dest;
582182734Sjulian		if (hinfo == &priv->lower)
583182734Sjulian			dest = &priv->upper;
584182734Sjulian		else
585182734Sjulian			dest = &priv->lower;
586182734Sjulian		NG_FWD_ITEM_HOOK(error, item, dest->hook);
587182734Sjulian		return error;
588182734Sjulian	}
589182734Sjulian
590182734Sjulian	mtx_lock(&ng_pipe_giant);
591182734Sjulian	microuptime(now);
592182734Sjulian
593182734Sjulian	/*
594182734Sjulian	 * Attach us to the list of active ng_pipes if this was an empty
595182734Sjulian	 * one before, and also update the queue service deadline time.
596182734Sjulian	 */
597182734Sjulian	if (hinfo->run.qin_frames == 0) {
598182734Sjulian		struct timeval *when = &hinfo->qin_utime;
599182734Sjulian		if (when->tv_sec < now->tv_sec || (when->tv_sec == now->tv_sec
600182734Sjulian		    && when->tv_usec < now->tv_usec)) {
601182734Sjulian			when->tv_sec = now->tv_sec;
602182734Sjulian			when->tv_usec = now->tv_usec;
603182734Sjulian		}
604182734Sjulian		if (hinfo->run.qout_frames == 0)
605182734Sjulian			LIST_INSERT_HEAD(&active_head, hinfo, active_le);
606182734Sjulian	}
607182734Sjulian
608182734Sjulian	/* Populate the packet header */
609182734Sjulian	ngp_h = uma_zalloc(ngp_zone, M_NOWAIT);
610182734Sjulian	KASSERT((ngp_h != NULL), ("ngp_h zalloc failed (1)"));
611182734Sjulian	NGI_GET_M(item, m);
612182734Sjulian	KASSERT(m != NULL, ("NGI_GET_M failed"));
613182734Sjulian	ngp_h->m = m;
614182734Sjulian	NG_FREE_ITEM(item);
615182734Sjulian
616182734Sjulian	if (hinfo->cfg.fifo)
617182734Sjulian		hash = 0;	/* all packets go into a single FIFO queue */
618182734Sjulian	else
619182734Sjulian		hash = ip_hash(m, priv->header_offset);
620182734Sjulian
621182734Sjulian	/* Find the appropriate FIFO queue for the packet and enqueue it*/
622182734Sjulian	TAILQ_FOREACH(ngp_f, &hinfo->fifo_head, fifo_le)
623182734Sjulian		if (hash == ngp_f->hash)
624182734Sjulian			break;
625182734Sjulian	if (ngp_f == NULL) {
626182734Sjulian		ngp_f = uma_zalloc(ngp_zone, M_NOWAIT);
627182734Sjulian		KASSERT(ngp_h != NULL, ("ngp_h zalloc failed (2)"));
628182734Sjulian		TAILQ_INIT(&ngp_f->packet_head);
629182734Sjulian		ngp_f->hash = hash;
630182734Sjulian		ngp_f->packets = 1;
631182734Sjulian		ngp_f->rr_deficit = hinfo->cfg.drr;	/* DRR quantum */
632182734Sjulian		hinfo->run.fifo_queues++;
633182734Sjulian		TAILQ_INSERT_TAIL(&ngp_f->packet_head, ngp_h, ngp_link);
634182734Sjulian		FIFO_VTIME_SORT(m->m_pkthdr.len);
635182734Sjulian	} else {
636182734Sjulian		TAILQ_INSERT_TAIL(&ngp_f->packet_head, ngp_h, ngp_link);
637182734Sjulian		ngp_f->packets++;
638182734Sjulian	}
639182734Sjulian	hinfo->run.qin_frames++;
640182734Sjulian	hinfo->run.qin_octets += m->m_pkthdr.len;
641182734Sjulian
642182734Sjulian	/* Discard a frame if inbound queue limit has been reached */
643182734Sjulian	if (hinfo->run.qin_frames > hinfo->cfg.qin_size_limit) {
644182734Sjulian		struct mbuf *m1;
645182734Sjulian		int longest = 0;
646182734Sjulian
647182734Sjulian		/* Find the longest queue */
648182734Sjulian		TAILQ_FOREACH(ngp_f1, &hinfo->fifo_head, fifo_le)
649182734Sjulian			if (ngp_f1->packets > longest) {
650182734Sjulian				longest = ngp_f1->packets;
651182734Sjulian				ngp_f = ngp_f1;
652182734Sjulian			}
653182734Sjulian
654182734Sjulian		/* Drop a frame from the queue head/tail, depending on cfg */
655182734Sjulian		if (hinfo->cfg.drophead)
656182734Sjulian			ngp_h = TAILQ_FIRST(&ngp_f->packet_head);
657182734Sjulian		else
658182734Sjulian			ngp_h = TAILQ_LAST(&ngp_f->packet_head, p_head);
659182734Sjulian		TAILQ_REMOVE(&ngp_f->packet_head, ngp_h, ngp_link);
660182734Sjulian		m1 = ngp_h->m;
661182734Sjulian		uma_zfree(ngp_zone, ngp_h);
662182734Sjulian		hinfo->run.qin_octets -= m1->m_pkthdr.len;
663182734Sjulian		hinfo->stats.in_disc_octets += m1->m_pkthdr.len;
664182734Sjulian		m_freem(m1);
665182734Sjulian		if (--(ngp_f->packets) == 0) {
666182734Sjulian			TAILQ_REMOVE(&hinfo->fifo_head, ngp_f, fifo_le);
667182734Sjulian			uma_zfree(ngp_zone, ngp_f);
668182734Sjulian			hinfo->run.fifo_queues--;
669182734Sjulian		}
670182734Sjulian		hinfo->run.qin_frames--;
671182734Sjulian		hinfo->stats.in_disc_frames++;
672182734Sjulian	} else if (hinfo->run.qin_frames > hinfo->cfg.qin_size_limit) {
673182734Sjulian		struct mbuf *m1;
674182734Sjulian		int longest = 0;
675182734Sjulian
676182734Sjulian		/* Find the longest queue */
677182734Sjulian		TAILQ_FOREACH(ngp_f1, &hinfo->fifo_head, fifo_le)
678182734Sjulian			if (ngp_f1->packets > longest) {
679182734Sjulian				longest = ngp_f1->packets;
680182734Sjulian				ngp_f = ngp_f1;
681182734Sjulian			}
682182734Sjulian
683182734Sjulian		/* Drop a frame from the queue head/tail, depending on cfg */
684182734Sjulian		if (hinfo->cfg.drophead)
685182734Sjulian			ngp_h = TAILQ_FIRST(&ngp_f->packet_head);
686182734Sjulian		else
687182734Sjulian			ngp_h = TAILQ_LAST(&ngp_f->packet_head, p_head);
688182734Sjulian		TAILQ_REMOVE(&ngp_f->packet_head, ngp_h, ngp_link);
689182734Sjulian		m1 = ngp_h->m;
690182734Sjulian		uma_zfree(ngp_zone, ngp_h);
691182734Sjulian		hinfo->run.qin_octets -= m1->m_pkthdr.len;
692182734Sjulian		hinfo->stats.in_disc_octets += m1->m_pkthdr.len;
693182734Sjulian		m_freem(m1);
694182734Sjulian		if (--(ngp_f->packets) == 0) {
695182734Sjulian			TAILQ_REMOVE(&hinfo->fifo_head, ngp_f, fifo_le);
696182734Sjulian			uma_zfree(ngp_zone, ngp_f);
697182734Sjulian			hinfo->run.fifo_queues--;
698182734Sjulian		}
699182734Sjulian		hinfo->run.qin_frames--;
700182734Sjulian		hinfo->stats.in_disc_frames++;
701182734Sjulian	}
702182734Sjulian
703182734Sjulian	/*
704182734Sjulian	 * Try to start the dequeuing process immediately.  We must
705182734Sjulian	 * hold the ng_pipe_giant lock here and pipe_dequeue() will
706182734Sjulian	 * release it
707182734Sjulian	 */
708182734Sjulian	pipe_dequeue(hinfo, now);
709182734Sjulian
710182734Sjulian	return (0);
711182734Sjulian}
712182734Sjulian
713182734Sjulian
714182734Sjulian/*
715182734Sjulian * Dequeueing sequence - we basically do the following:
716182734Sjulian *  1) Try to extract the frame from the inbound (bandwidth) queue;
717182734Sjulian *  2) In accordance to BER specified, discard the frame randomly;
718182734Sjulian *  3) If the frame survives BER, prepend it with delay info and move it
719182734Sjulian *     to outbound (delay) queue;
720182734Sjulian *  4) Loop to 2) until bandwidth quota for this timeslice is reached, or
721182734Sjulian *     inbound queue is flushed completely;
722182734Sjulian *  5) Extract the first frame from the outbound queue, if it's time has
723182734Sjulian *     come.  Queue the frame for transmission on the outbound hook;
724182734Sjulian *  6) Loop to 5) until outbound queue is flushed completely, or the next
725182734Sjulian *     frame in the queue is not scheduled to be dequeued yet;
726182734Sjulian *  7) Transimit all frames queued in 5)
727182734Sjulian *
728182734Sjulian * Note: the caller must hold the ng_pipe_giant lock; this function
729182734Sjulian * returns with the lock released.
730182734Sjulian */
731182734Sjulianstatic void
732182734Sjulianpipe_dequeue(struct hookinfo *hinfo, struct timeval *now) {
733182734Sjulian	static uint64_t rand, oldrand;
734182734Sjulian	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hinfo->hook));
735182734Sjulian	struct hookinfo *dest;
736182734Sjulian	struct ngp_fifo *ngp_f, *ngp_f1;
737182734Sjulian	struct ngp_hdr *ngp_h;
738182734Sjulian	struct timeval *when;
739182734Sjulian	struct mbuf *q_head = NULL;
740182734Sjulian	struct mbuf *q_tail = NULL;
741182734Sjulian	struct mbuf *m;
742182734Sjulian	int error = 0;
743182734Sjulian
744182734Sjulian	/* Which one is the destination hook? */
745182734Sjulian	if (hinfo == &priv->lower)
746182734Sjulian		dest = &priv->upper;
747182734Sjulian	else
748182734Sjulian		dest = &priv->lower;
749182734Sjulian
750182734Sjulian	/* Bandwidth queue processing */
751182734Sjulian	while ((ngp_f = TAILQ_FIRST(&hinfo->fifo_head))) {
752182734Sjulian		when = &hinfo->qin_utime;
753182734Sjulian		if (when->tv_sec > now->tv_sec || (when->tv_sec == now->tv_sec
754182734Sjulian		    && when->tv_usec > now->tv_usec))
755182734Sjulian			break;
756182734Sjulian
757182734Sjulian		ngp_h = TAILQ_FIRST(&ngp_f->packet_head);
758182734Sjulian		m = ngp_h->m;
759182734Sjulian
760182734Sjulian		/* Deficit Round Robin (DRR) processing */
761182734Sjulian		if (hinfo->cfg.drr) {
762182734Sjulian			if (ngp_f->rr_deficit >= m->m_pkthdr.len) {
763182734Sjulian				ngp_f->rr_deficit -= m->m_pkthdr.len;
764182734Sjulian			} else {
765182734Sjulian				ngp_f->rr_deficit += hinfo->cfg.drr;
766182734Sjulian				TAILQ_REMOVE(&hinfo->fifo_head, ngp_f, fifo_le);
767182734Sjulian				TAILQ_INSERT_TAIL(&hinfo->fifo_head,
768182734Sjulian				    ngp_f, fifo_le);
769182734Sjulian				continue;
770182734Sjulian			}
771182734Sjulian		}
772182734Sjulian
773182734Sjulian		/*
774182734Sjulian		 * Either create a duplicate and pass it on, or dequeue
775182734Sjulian		 * the original packet...
776182734Sjulian		 */
777182734Sjulian		if (hinfo->cfg.duplicate &&
778182734Sjulian		    random() % 100 <= hinfo->cfg.duplicate) {
779182734Sjulian			ngp_h = uma_zalloc(ngp_zone, M_NOWAIT);
780182734Sjulian			KASSERT(ngp_h != NULL, ("ngp_h zalloc failed (3)"));
781182734Sjulian			ngp_h->m = m_dup(m, M_NOWAIT);
782182734Sjulian			KASSERT(ngp_h->m != NULL, ("m_dup failed"));
783182734Sjulian		} else {
784182734Sjulian			TAILQ_REMOVE(&ngp_f->packet_head, ngp_h, ngp_link);
785182734Sjulian			hinfo->run.qin_frames--;
786182734Sjulian			hinfo->run.qin_octets -= m->m_pkthdr.len;
787182734Sjulian			ngp_f->packets--;
788182734Sjulian		}
789182734Sjulian
790182734Sjulian		/* Calculate the serialization delay */
791182734Sjulian		if (hinfo->cfg.bandwidth) {
792182734Sjulian			hinfo->qin_utime.tv_usec += ((uint64_t) m->m_pkthdr.len
793182734Sjulian				+ priv->overhead ) *
794182734Sjulian				8000000 / hinfo->cfg.bandwidth;
795182734Sjulian			hinfo->qin_utime.tv_sec +=
796182734Sjulian				hinfo->qin_utime.tv_usec / 1000000;
797182734Sjulian			hinfo->qin_utime.tv_usec =
798182734Sjulian				hinfo->qin_utime.tv_usec % 1000000;
799182734Sjulian		}
800182734Sjulian		when = &ngp_h->when;
801182734Sjulian		when->tv_sec = hinfo->qin_utime.tv_sec;
802182734Sjulian		when->tv_usec = hinfo->qin_utime.tv_usec;
803182734Sjulian
804182734Sjulian		/* Sort / rearrange inbound queues */
805182734Sjulian		if (ngp_f->packets) {
806182734Sjulian			if (hinfo->cfg.wfq) {
807182734Sjulian				TAILQ_REMOVE(&hinfo->fifo_head, ngp_f, fifo_le);
808182734Sjulian				FIFO_VTIME_SORT(TAILQ_FIRST(
809182734Sjulian				    &ngp_f->packet_head)->m->m_pkthdr.len)
810182734Sjulian			}
811182734Sjulian		} else {
812182734Sjulian			TAILQ_REMOVE(&hinfo->fifo_head, ngp_f, fifo_le);
813182734Sjulian			uma_zfree(ngp_zone, ngp_f);
814182734Sjulian			hinfo->run.fifo_queues--;
815182734Sjulian		}
816182734Sjulian
817182734Sjulian		/* Randomly discard the frame, according to BER setting */
818182734Sjulian		if (hinfo->cfg.ber &&
819182734Sjulian		    ((oldrand = rand) ^ (rand = random())<<17) >=
820182734Sjulian		    hinfo->ber_p[priv->overhead + m->m_pkthdr.len] ) {
821182734Sjulian			hinfo->stats.out_disc_frames++;
822182734Sjulian			hinfo->stats.out_disc_octets += m->m_pkthdr.len;
823182734Sjulian			uma_zfree(ngp_zone, ngp_h);
824182734Sjulian			m_freem(m);
825182734Sjulian			continue;
826182734Sjulian		}
827182734Sjulian
828182734Sjulian		/* Discard frame if outbound queue size limit exceeded */
829182734Sjulian		if (hinfo->cfg.qout_size_limit &&
830182734Sjulian		    hinfo->run.qout_frames>=hinfo->cfg.qout_size_limit) {
831182734Sjulian			hinfo->stats.out_disc_frames++;
832182734Sjulian			hinfo->stats.out_disc_octets += m->m_pkthdr.len;
833182734Sjulian			uma_zfree(ngp_zone, ngp_h);
834182734Sjulian			m_freem(m);
835182734Sjulian			continue;
836182734Sjulian		}
837182734Sjulian
838182734Sjulian		/* Calculate the propagation delay */
839182734Sjulian		when->tv_usec += priv->delay;
840182734Sjulian		when->tv_sec += when->tv_usec / 1000000;
841182734Sjulian		when->tv_usec = when->tv_usec % 1000000;
842182734Sjulian
843182734Sjulian		/* Put the frame into the delay queue */
844182734Sjulian		TAILQ_INSERT_TAIL(&hinfo->qout_head, ngp_h, ngp_link);
845182734Sjulian		hinfo->run.qout_frames++;
846182734Sjulian		hinfo->run.qout_octets += m->m_pkthdr.len;
847182734Sjulian	}
848182734Sjulian
849182734Sjulian	/* Delay queue processing */
850182734Sjulian	while ((ngp_h = TAILQ_FIRST(&hinfo->qout_head))) {
851182734Sjulian		struct mbuf *m = ngp_h->m;
852182734Sjulian
853182734Sjulian		when = &ngp_h->when;
854182734Sjulian		if (when->tv_sec > now->tv_sec ||
855182734Sjulian		    (when->tv_sec == now->tv_sec &&
856182734Sjulian		    when->tv_usec > now->tv_usec))
857182734Sjulian			break;
858182734Sjulian
859182734Sjulian		/* Update outbound queue stats */
860182734Sjulian		hinfo->stats.fwd_frames++;
861182734Sjulian		hinfo->stats.fwd_octets += m->m_pkthdr.len;
862182734Sjulian		hinfo->run.qout_frames--;
863182734Sjulian		hinfo->run.qout_octets -= m->m_pkthdr.len;
864182734Sjulian
865182734Sjulian		/* Dequeue the packet from qout */
866182734Sjulian		TAILQ_REMOVE(&hinfo->qout_head, ngp_h, ngp_link);
867182734Sjulian		uma_zfree(ngp_zone, ngp_h);
868182734Sjulian
869182734Sjulian		/* Enqueue locally for sending downstream */
870182734Sjulian		if (q_head == NULL)
871182734Sjulian			q_head = m;
872182734Sjulian		if (q_tail)
873182734Sjulian			q_tail->m_nextpkt = m;
874182734Sjulian		q_tail = m;
875182734Sjulian		m->m_nextpkt = NULL;
876182734Sjulian	}
877182734Sjulian
878182734Sjulian	/* If both queues are empty detach us from the list of active queues */
879182734Sjulian	if (hinfo->run.qin_frames + hinfo->run.qout_frames == 0) {
880182734Sjulian		LIST_REMOVE(hinfo, active_le);
881182734Sjulian		active_gen_id++;
882182734Sjulian	}
883182734Sjulian
884182734Sjulian	mtx_unlock(&ng_pipe_giant);
885182734Sjulian
886182734Sjulian	while ((m = q_head) != NULL) {
887182734Sjulian		q_head = m->m_nextpkt;
888182734Sjulian		m->m_nextpkt = NULL;
889182734Sjulian		NG_SEND_DATA(error, dest->hook, m, meta);
890182734Sjulian	}
891182734Sjulian}
892182734Sjulian
893182734Sjulian
894182734Sjulian/*
895182734Sjulian * This routine is called on every clock tick. We poll all nodes/hooks
896182734Sjulian * for queued frames by calling pipe_dequeue().
897182734Sjulian */
898182734Sjulianstatic void
899182734Sjulianpipe_scheduler(void *arg)
900182734Sjulian{
901182734Sjulian	pipe_poll();
902182734Sjulian
903182734Sjulian	/* Reschedule  */
904182734Sjulian	callout_reset(&polling_timer, 1, &pipe_scheduler, NULL);
905182734Sjulian}
906182734Sjulian
907182734Sjulian
908182734Sjulian/*
909182734Sjulian * Traverse the list of all active hooks and attempt to dequeue
910182734Sjulian * some packets.  Hooks with empty queues are not traversed since
911182734Sjulian * they are not linked into this list.
912182734Sjulian */
913182734Sjulianstatic void
914182734Sjulianpipe_poll(void)
915182734Sjulian{
916182734Sjulian	struct hookinfo *hinfo;
917182734Sjulian	struct timeval now;
918182734Sjulian	int old_gen_id = active_gen_id;
919182734Sjulian
920182734Sjulian	mtx_lock(&ng_pipe_giant);
921182734Sjulian	microuptime(&now);
922182734Sjulian	LIST_FOREACH(hinfo, &active_head, active_le) {
923182734Sjulian		CURVNET_SET(NG_HOOK_NODE(hinfo->hook)->nd_vnet);
924182734Sjulian		pipe_dequeue(hinfo, &now);
925182734Sjulian		CURVNET_RESTORE();
926182734Sjulian		mtx_lock(&ng_pipe_giant);
927182734Sjulian		if (old_gen_id != active_gen_id) {
928182734Sjulian			/* the list was updated; restart traversing */
929182734Sjulian			hinfo = LIST_FIRST(&active_head);
930182734Sjulian			if (hinfo == NULL)
931182734Sjulian				break;
932182734Sjulian			old_gen_id = active_gen_id;
933182734Sjulian			continue;
934182734Sjulian		}
935182734Sjulian	}
936182734Sjulian	mtx_unlock(&ng_pipe_giant);
937182734Sjulian}
938182734Sjulian
939182734Sjulian
940182734Sjulian/*
941182734Sjulian * Shutdown processing
942182734Sjulian *
943182734Sjulian * This is tricky. If we have both a lower and upper hook, then we
944182734Sjulian * probably want to extricate ourselves and leave the two peers
945182734Sjulian * still linked to each other. Otherwise we should just shut down as
946182734Sjulian * a normal node would.
947182734Sjulian */
948182734Sjulianstatic int
949182734Sjulianngp_shutdown(node_p node)
950182734Sjulian{
951182734Sjulian	const priv_p priv = NG_NODE_PRIVATE(node);
952182734Sjulian
953182734Sjulian	if (priv->lower.hook && priv->upper.hook)
954182734Sjulian		ng_bypass(priv->lower.hook, priv->upper.hook);
955182734Sjulian	else {
956182734Sjulian		if (priv->upper.hook != NULL)
957182734Sjulian			ng_rmhook_self(priv->upper.hook);
958182734Sjulian		if (priv->lower.hook != NULL)
959182734Sjulian			ng_rmhook_self(priv->lower.hook);
960182734Sjulian	}
961182734Sjulian	NG_NODE_UNREF(node);
962182734Sjulian	FREE(priv, M_NG_PIPE);
963182734Sjulian	return (0);
964182734Sjulian}
965182734Sjulian
966182734Sjulian
967182734Sjulian/*
968182734Sjulian * Hook disconnection
969182734Sjulian */
970182734Sjulianstatic int
971182734Sjulianngp_disconnect(hook_p hook)
972182734Sjulian{
973182734Sjulian	struct hookinfo *const hinfo = NG_HOOK_PRIVATE(hook);
974182734Sjulian	struct ngp_fifo *ngp_f;
975182734Sjulian	struct ngp_hdr *ngp_h;
976182734Sjulian	int removed = 0;
977182734Sjulian
978182734Sjulian	mtx_lock(&ng_pipe_giant);
979182734Sjulian
980182734Sjulian	KASSERT(hinfo != NULL, ("%s: null info", __FUNCTION__));
981182734Sjulian	hinfo->hook = NULL;
982182734Sjulian
983182734Sjulian	/* Flush all fifo queues associated with the hook */
984182734Sjulian	while ((ngp_f = TAILQ_FIRST(&hinfo->fifo_head))) {
985182734Sjulian		while ((ngp_h = TAILQ_FIRST(&ngp_f->packet_head))) {
986182734Sjulian			TAILQ_REMOVE(&ngp_f->packet_head, ngp_h, ngp_link);
987182734Sjulian			m_freem(ngp_h->m);
988182734Sjulian			uma_zfree(ngp_zone, ngp_h);
989182734Sjulian			removed++;
990182734Sjulian		}
991182734Sjulian		TAILQ_REMOVE(&hinfo->fifo_head, ngp_f, fifo_le);
992182734Sjulian		uma_zfree(ngp_zone, ngp_f);
993182734Sjulian	}
994182734Sjulian
995182734Sjulian	/* Flush the delay queue */
996182734Sjulian	while ((ngp_h = TAILQ_FIRST(&hinfo->qout_head))) {
997182734Sjulian		TAILQ_REMOVE(&hinfo->qout_head, ngp_h, ngp_link);
998182734Sjulian		m_freem(ngp_h->m);
999182734Sjulian		uma_zfree(ngp_zone, ngp_h);
1000182734Sjulian		removed++;
1001182734Sjulian	}
1002182734Sjulian
1003182734Sjulian	/*
1004182734Sjulian	 * Both queues should be empty by now, so detach us from
1005182734Sjulian	 * the list of active queues
1006182734Sjulian	 */
1007182734Sjulian	if (removed) {
1008182734Sjulian		LIST_REMOVE(hinfo, active_le);
1009182734Sjulian		active_gen_id++;
1010182734Sjulian	}
1011182734Sjulian	if (hinfo->run.qin_frames + hinfo->run.qout_frames != removed)
1012182734Sjulian		printf("Mismatch: queued=%d but removed=%d !?!",
1013182734Sjulian		    hinfo->run.qin_frames + hinfo->run.qout_frames, removed);
1014182734Sjulian
1015182734Sjulian	/* Release the packet loss probability table (BER) */
1016182734Sjulian	if (hinfo->ber_p)
1017182734Sjulian		FREE(hinfo->ber_p, M_NG_PIPE);
1018182734Sjulian
1019182734Sjulian	mtx_unlock(&ng_pipe_giant);
1020182734Sjulian
1021182734Sjulian	return (0);
1022182734Sjulian}
1023182734Sjulian
1024182734Sjulianstatic int
1025182734Sjulianngp_modevent(module_t mod, int type, void *unused)
1026182734Sjulian{
1027182734Sjulian	int error = 0;
1028182734Sjulian
1029182734Sjulian	switch (type) {
1030182734Sjulian	case MOD_LOAD:
1031182734Sjulian		ngp_zone = uma_zcreate("ng_pipe", max(sizeof(struct ngp_hdr),
1032182734Sjulian		    sizeof (struct ngp_fifo)), NULL, NULL, NULL, NULL,
1033182734Sjulian		    UMA_ALIGN_PTR, 0);
1034182734Sjulian		if (ngp_zone == NULL)
1035182734Sjulian			panic("ng_pipe: couldn't allocate descriptor zone");
1036182734Sjulian
1037182734Sjulian		mtx_init(&ng_pipe_giant, "ng_pipe_giant", NULL, MTX_DEF);
1038182734Sjulian		LIST_INIT(&active_head);
1039182734Sjulian		callout_init(&polling_timer, CALLOUT_MPSAFE);
1040182734Sjulian		callout_reset(&polling_timer, 1, &pipe_scheduler, NULL);
1041182734Sjulian		break;
1042182734Sjulian	case MOD_UNLOAD:
1043182734Sjulian		callout_drain(&polling_timer);
1044182734Sjulian		uma_zdestroy(ngp_zone);
1045182734Sjulian		mtx_destroy(&ng_pipe_giant);
1046182734Sjulian		break;
1047182734Sjulian	default:
1048182734Sjulian		error = EOPNOTSUPP;
1049182734Sjulian		break;
1050182734Sjulian	}
1051182734Sjulian
1052182734Sjulian	return (error);
1053182734Sjulian}
1054