xref: /freebsd/sys/netpfil/ipfw/ip_dn_io.c (revision 70b56f4b92877578741e5aba2a72d6088cb13ecb)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010 Luigi Rizzo, Riccardo Panicucci, Universita` di Pisa
5  * All rights reserved
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * Dummynet portions related to packet handling.
31  */
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_inet6.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/module.h>
44 #include <sys/mutex.h>
45 #include <sys/priv.h>
46 #include <sys/proc.h>
47 #include <sys/rwlock.h>
48 #include <sys/socket.h>
49 #include <sys/time.h>
50 #include <sys/sysctl.h>
51 
52 #include <net/if.h>	/* IFNAMSIZ, struct ifaddr, ifq head, lock.h mutex.h */
53 #include <net/if_var.h>	/* NET_EPOCH_... */
54 #include <net/netisr.h>
55 #include <net/vnet.h>
56 
57 #include <netinet/in.h>
58 #include <netinet/ip.h>		/* ip_len, ip_off */
59 #include <netinet/ip_var.h>	/* ip_output(), IP_FORWARDING */
60 #include <netinet/ip_fw.h>
61 #include <netinet/ip_dummynet.h>
62 #include <netinet/if_ether.h> /* various ether_* routines */
63 #include <netinet/ip6.h>       /* for ip6_input, ip6_output prototypes */
64 #include <netinet6/ip6_var.h>
65 
66 #include <netpfil/ipfw/ip_fw_private.h>
67 #include <netpfil/ipfw/dn_heap.h>
68 #include <netpfil/ipfw/ip_dn_private.h>
69 #ifdef NEW_AQM
70 #include <netpfil/ipfw/dn_aqm.h>
71 #endif
72 #include <netpfil/ipfw/dn_sched.h>
73 
74 /*
75  * We keep a private variable for the simulation time, but we could
76  * probably use an existing one ("softticks" in sys/kern/kern_timeout.c)
77  * instead of V_dn_cfg.curr_time
78  */
79 VNET_DEFINE(struct dn_parms, dn_cfg);
80 #define V_dn_cfg VNET(dn_cfg)
81 
82 /*
83  * We use a heap to store entities for which we have pending timer events.
84  * The heap is checked at every tick and all entities with expired events
85  * are extracted.
86  */
87 
88 MALLOC_DEFINE(M_DUMMYNET, "dummynet", "dummynet heap");
89 
90 extern	void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
91 
92 #ifdef SYSCTL_NODE
93 
94 /*
95  * Because of the way the SYSBEGIN/SYSEND macros work on other
96  * platforms, there should not be functions between them.
97  * So keep the handlers outside the block.
98  */
99 static int
100 sysctl_hash_size(SYSCTL_HANDLER_ARGS)
101 {
102 	int error, value;
103 
104 	value = V_dn_cfg.hash_size;
105 	error = sysctl_handle_int(oidp, &value, 0, req);
106 	if (error != 0 || req->newptr == NULL)
107 		return (error);
108 	if (value < 16 || value > 65536)
109 		return (EINVAL);
110 	V_dn_cfg.hash_size = value;
111 	return (0);
112 }
113 
114 static int
115 sysctl_limits(SYSCTL_HANDLER_ARGS)
116 {
117 	int error;
118 	long value;
119 
120 	if (arg2 != 0)
121 		value = V_dn_cfg.slot_limit;
122 	else
123 		value = V_dn_cfg.byte_limit;
124 	error = sysctl_handle_long(oidp, &value, 0, req);
125 
126 	if (error != 0 || req->newptr == NULL)
127 		return (error);
128 	if (arg2 != 0) {
129 		if (value < 1)
130 			return (EINVAL);
131 		V_dn_cfg.slot_limit = value;
132 	} else {
133 		if (value < 1500)
134 			return (EINVAL);
135 		V_dn_cfg.byte_limit = value;
136 	}
137 	return (0);
138 }
139 
140 SYSBEGIN(f4)
141 
142 SYSCTL_DECL(_net_inet);
143 SYSCTL_DECL(_net_inet_ip);
144 #ifdef NEW_AQM
145 SYSCTL_NODE(_net_inet_ip, OID_AUTO, dummynet, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
146     "Dummynet");
147 #else
148 static SYSCTL_NODE(_net_inet_ip, OID_AUTO, dummynet,
149     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
150     "Dummynet");
151 #endif
152 
153 /* wrapper to pass V_dn_cfg fields to SYSCTL_* */
154 #define DC(x)	(&(VNET_NAME(dn_cfg).x))
155 
156 /* parameters */
157 
158 SYSCTL_PROC(_net_inet_ip_dummynet, OID_AUTO, hash_size,
159     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
160     0, 0, sysctl_hash_size, "I",
161     "Default hash table size");
162 
163 SYSCTL_PROC(_net_inet_ip_dummynet, OID_AUTO, pipe_slot_limit,
164     CTLTYPE_LONG | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
165     0, 1, sysctl_limits, "L",
166     "Upper limit in slots for pipe queue.");
167 SYSCTL_PROC(_net_inet_ip_dummynet, OID_AUTO, pipe_byte_limit,
168     CTLTYPE_LONG | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
169     0, 0, sysctl_limits, "L",
170     "Upper limit in bytes for pipe queue.");
171 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, io_fast,
172     CTLFLAG_RW | CTLFLAG_VNET, DC(io_fast), 0, "Enable fast dummynet io.");
173 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, debug,
174     CTLFLAG_RW | CTLFLAG_VNET, DC(debug), 0, "Dummynet debug level");
175 
176 /* RED parameters */
177 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, red_lookup_depth,
178     CTLFLAG_RD | CTLFLAG_VNET, DC(red_lookup_depth), 0, "Depth of RED lookup table");
179 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, red_avg_pkt_size,
180     CTLFLAG_RD | CTLFLAG_VNET, DC(red_avg_pkt_size), 0, "RED Medium packet size");
181 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, red_max_pkt_size,
182     CTLFLAG_RD | CTLFLAG_VNET, DC(red_max_pkt_size), 0, "RED Max packet size");
183 
184 /* time adjustment */
185 SYSCTL_LONG(_net_inet_ip_dummynet, OID_AUTO, tick_delta,
186     CTLFLAG_RD | CTLFLAG_VNET, DC(tick_delta), 0, "Last vs standard tick difference (usec).");
187 SYSCTL_LONG(_net_inet_ip_dummynet, OID_AUTO, tick_delta_sum,
188     CTLFLAG_RD | CTLFLAG_VNET, DC(tick_delta_sum), 0, "Accumulated tick difference (usec).");
189 SYSCTL_LONG(_net_inet_ip_dummynet, OID_AUTO, tick_adjustment,
190     CTLFLAG_RD | CTLFLAG_VNET, DC(tick_adjustment), 0, "Tick adjustments done.");
191 SYSCTL_LONG(_net_inet_ip_dummynet, OID_AUTO, tick_diff,
192     CTLFLAG_RD | CTLFLAG_VNET, DC(tick_diff), 0,
193     "Adjusted vs non-adjusted curr_time difference (ticks).");
194 SYSCTL_LONG(_net_inet_ip_dummynet, OID_AUTO, tick_lost,
195     CTLFLAG_RD | CTLFLAG_VNET, DC(tick_lost), 0,
196     "Number of ticks coalesced by dummynet taskqueue.");
197 
198 /* Drain parameters */
199 SYSCTL_UINT(_net_inet_ip_dummynet, OID_AUTO, expire,
200     CTLFLAG_RW | CTLFLAG_VNET, DC(expire), 0, "Expire empty queues/pipes");
201 SYSCTL_UINT(_net_inet_ip_dummynet, OID_AUTO, expire_cycle,
202     CTLFLAG_RD | CTLFLAG_VNET, DC(expire_cycle), 0, "Expire cycle for queues/pipes");
203 
204 /* statistics */
205 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, schk_count,
206     CTLFLAG_RD | CTLFLAG_VNET, DC(schk_count), 0, "Number of schedulers");
207 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, si_count,
208     CTLFLAG_RD | CTLFLAG_VNET, DC(si_count), 0, "Number of scheduler instances");
209 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, fsk_count,
210     CTLFLAG_RD | CTLFLAG_VNET, DC(fsk_count), 0, "Number of flowsets");
211 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, queue_count,
212     CTLFLAG_RD | CTLFLAG_VNET, DC(queue_count), 0, "Number of queues");
213 SYSCTL_ULONG(_net_inet_ip_dummynet, OID_AUTO, io_pkt,
214     CTLFLAG_RD | CTLFLAG_VNET, DC(io_pkt), 0,
215     "Number of packets passed to dummynet.");
216 SYSCTL_ULONG(_net_inet_ip_dummynet, OID_AUTO, io_pkt_fast,
217     CTLFLAG_RD | CTLFLAG_VNET, DC(io_pkt_fast), 0,
218     "Number of packets bypassed dummynet scheduler.");
219 SYSCTL_ULONG(_net_inet_ip_dummynet, OID_AUTO, io_pkt_drop,
220     CTLFLAG_RD | CTLFLAG_VNET, DC(io_pkt_drop), 0,
221     "Number of packets dropped by dummynet.");
222 #undef DC
223 SYSEND
224 
225 #endif
226 
227 static void	dummynet_send(struct mbuf *);
228 
229 /*
230  * Return the mbuf tag holding the dummynet state (it should
231  * be the first one on the list).
232  */
233 struct dn_pkt_tag *
234 dn_tag_get(struct mbuf *m)
235 {
236 	struct m_tag *mtag = m_tag_first(m);
237 #ifdef NEW_AQM
238 	/* XXX: to skip ts m_tag. For Debugging only*/
239 	if (mtag != NULL && mtag->m_tag_id == DN_AQM_MTAG_TS) {
240 		m_tag_delete(m,mtag);
241 		mtag = m_tag_first(m);
242 		D("skip TS tag");
243 	}
244 #endif
245 	KASSERT(mtag != NULL &&
246 	    mtag->m_tag_cookie == MTAG_ABI_COMPAT &&
247 	    mtag->m_tag_id == PACKET_TAG_DUMMYNET,
248 	    ("packet on dummynet queue w/o dummynet tag!"));
249 	return (struct dn_pkt_tag *)(mtag+1);
250 }
251 
252 #ifndef NEW_AQM
253 static inline void
254 mq_append(struct mq *q, struct mbuf *m)
255 {
256 #ifdef USERSPACE
257 	// buffers from netmap need to be copied
258 	// XXX note that the routine is not expected to fail
259 	ND("append %p to %p", m, q);
260 	if (m->m_flags & M_STACK) {
261 		struct mbuf *m_new;
262 		void *p;
263 		int l, ofs;
264 
265 		ofs = m->m_data - m->__m_extbuf;
266 		// XXX allocate
267 		MGETHDR(m_new, M_NOWAIT, MT_DATA);
268 		ND("*** WARNING, volatile buf %p ext %p %d dofs %d m_new %p",
269 			m, m->__m_extbuf, m->__m_extlen, ofs, m_new);
270 		p = m_new->__m_extbuf;	/* new pointer */
271 		l = m_new->__m_extlen;	/* new len */
272 		if (l <= m->__m_extlen) {
273 			panic("extlen too large");
274 		}
275 
276 		*m_new = *m;	// copy
277 		m_new->m_flags &= ~M_STACK;
278 		m_new->__m_extbuf = p; // point to new buffer
279 		_pkt_copy(m->__m_extbuf, p, m->__m_extlen);
280 		m_new->m_data = p + ofs;
281 		m = m_new;
282 	}
283 #endif /* USERSPACE */
284 	if (q->head == NULL)
285 		q->head = m;
286 	else
287 		q->tail->m_nextpkt = m;
288 	q->count++;
289 	q->tail = m;
290 	m->m_nextpkt = NULL;
291 }
292 #endif
293 
294 /*
295  * Dispose a list of packet. Use a functions so if we need to do
296  * more work, this is a central point to do it.
297  */
298 void dn_free_pkts(struct mbuf *mnext)
299 {
300         struct mbuf *m;
301 
302         while ((m = mnext) != NULL) {
303                 mnext = m->m_nextpkt;
304                 FREE_PKT(m);
305         }
306 }
307 
308 static int
309 red_drops (struct dn_queue *q, int len)
310 {
311 	/*
312 	 * RED algorithm
313 	 *
314 	 * RED calculates the average queue size (avg) using a low-pass filter
315 	 * with an exponential weighted (w_q) moving average:
316 	 * 	avg  <-  (1-w_q) * avg + w_q * q_size
317 	 * where q_size is the queue length (measured in bytes or * packets).
318 	 *
319 	 * If q_size == 0, we compute the idle time for the link, and set
320 	 *	avg = (1 - w_q)^(idle/s)
321 	 * where s is the time needed for transmitting a medium-sized packet.
322 	 *
323 	 * Now, if avg < min_th the packet is enqueued.
324 	 * If avg > max_th the packet is dropped. Otherwise, the packet is
325 	 * dropped with probability P function of avg.
326 	 */
327 
328 	struct dn_fsk *fs = q->fs;
329 	int64_t p_b = 0;
330 
331 	/* Queue in bytes or packets? */
332 	uint32_t q_size = (fs->fs.flags & DN_QSIZE_BYTES) ?
333 	    q->ni.len_bytes : q->ni.length;
334 
335 	/* Average queue size estimation. */
336 	if (q_size != 0) {
337 		/* Queue is not empty, avg <- avg + (q_size - avg) * w_q */
338 		int diff = SCALE(q_size) - q->avg;
339 		int64_t v = SCALE_MUL((int64_t)diff, (int64_t)fs->w_q);
340 
341 		q->avg += (int)v;
342 	} else {
343 		/*
344 		 * Queue is empty, find for how long the queue has been
345 		 * empty and use a lookup table for computing
346 		 * (1 - * w_q)^(idle_time/s) where s is the time to send a
347 		 * (small) packet.
348 		 * XXX check wraps...
349 		 */
350 		if (q->avg) {
351 			u_int t = div64((V_dn_cfg.curr_time - q->q_time), fs->lookup_step);
352 
353 			q->avg = (t < fs->lookup_depth) ?
354 			    SCALE_MUL(q->avg, fs->w_q_lookup[t]) : 0;
355 		}
356 	}
357 
358 	/* Should i drop? */
359 	if (q->avg < fs->min_th) {
360 		q->count = -1;
361 		return (0);	/* accept packet */
362 	}
363 	if (q->avg >= fs->max_th) {	/* average queue >=  max threshold */
364 		if (fs->fs.flags & DN_IS_ECN)
365 			return (1);
366 		if (fs->fs.flags & DN_IS_GENTLE_RED) {
367 			/*
368 			 * According to Gentle-RED, if avg is greater than
369 			 * max_th the packet is dropped with a probability
370 			 *	 p_b = c_3 * avg - c_4
371 			 * where c_3 = (1 - max_p) / max_th
372 			 *       c_4 = 1 - 2 * max_p
373 			 */
374 			p_b = SCALE_MUL((int64_t)fs->c_3, (int64_t)q->avg) -
375 			    fs->c_4;
376 		} else {
377 			q->count = -1;
378 			return (1);
379 		}
380 	} else if (q->avg > fs->min_th) {
381 		if (fs->fs.flags & DN_IS_ECN)
382 			return (1);
383 		/*
384 		 * We compute p_b using the linear dropping function
385 		 *	 p_b = c_1 * avg - c_2
386 		 * where c_1 = max_p / (max_th - min_th)
387 		 * 	 c_2 = max_p * min_th / (max_th - min_th)
388 		 */
389 		p_b = SCALE_MUL((int64_t)fs->c_1, (int64_t)q->avg) - fs->c_2;
390 	}
391 
392 	if (fs->fs.flags & DN_QSIZE_BYTES)
393 		p_b = div64((p_b * len) , fs->max_pkt_size);
394 	if (++q->count == 0)
395 		q->random = random() & 0xffff;
396 	else {
397 		/*
398 		 * q->count counts packets arrived since last drop, so a greater
399 		 * value of q->count means a greater packet drop probability.
400 		 */
401 		if (SCALE_MUL(p_b, SCALE((int64_t)q->count)) > q->random) {
402 			q->count = 0;
403 			/* After a drop we calculate a new random value. */
404 			q->random = random() & 0xffff;
405 			return (1);	/* drop */
406 		}
407 	}
408 	/* End of RED algorithm. */
409 
410 	return (0);	/* accept */
411 
412 }
413 
414 /*
415  * ECN/ECT Processing (partially adopted from altq)
416  */
417 #ifndef NEW_AQM
418 static
419 #endif
420 int
421 ecn_mark(struct mbuf* m)
422 {
423 	struct ip *ip;
424 	ip = (struct ip *)mtodo(m, dn_tag_get(m)->iphdr_off);
425 
426 	switch (ip->ip_v) {
427 	case IPVERSION:
428 	{
429 		uint16_t old;
430 
431 		if ((ip->ip_tos & IPTOS_ECN_MASK) == IPTOS_ECN_NOTECT)
432 			return (0);	/* not-ECT */
433 		if ((ip->ip_tos & IPTOS_ECN_MASK) == IPTOS_ECN_CE)
434 			return (1);	/* already marked */
435 
436 		/*
437 		 * ecn-capable but not marked,
438 		 * mark CE and update checksum
439 		 */
440 		old = *(uint16_t *)ip;
441 		ip->ip_tos |= IPTOS_ECN_CE;
442 		ip->ip_sum = cksum_adjust(ip->ip_sum, old, *(uint16_t *)ip);
443 		return (1);
444 	}
445 #ifdef INET6
446 	case (IPV6_VERSION >> 4):
447 	{
448 		struct ip6_hdr *ip6 = (struct ip6_hdr *)ip;
449 		u_int32_t flowlabel;
450 
451 		flowlabel = ntohl(ip6->ip6_flow);
452 		if ((flowlabel >> 28) != 6)
453 			return (0);	/* version mismatch! */
454 		if ((flowlabel & (IPTOS_ECN_MASK << 20)) ==
455 		    (IPTOS_ECN_NOTECT << 20))
456 			return (0);	/* not-ECT */
457 		if ((flowlabel & (IPTOS_ECN_MASK << 20)) ==
458 		    (IPTOS_ECN_CE << 20))
459 			return (1);	/* already marked */
460 		/*
461 		 * ecn-capable but not marked, mark CE
462 		 */
463 		flowlabel |= (IPTOS_ECN_CE << 20);
464 		ip6->ip6_flow = htonl(flowlabel);
465 		return (1);
466 	}
467 #endif
468 	}
469 	return (0);
470 }
471 
472 /*
473  * Enqueue a packet in q, subject to space and queue management policy
474  * (whose parameters are in q->fs).
475  * Update stats for the queue and the scheduler.
476  * Return 0 on success, 1 on drop. The packet is consumed anyways.
477  */
478 int
479 dn_enqueue(struct dn_queue *q, struct mbuf* m, int drop)
480 {
481 	struct dn_fs *f;
482 	struct dn_flow *ni;	/* stats for scheduler instance */
483 	uint64_t len;
484 
485 	if (q->fs == NULL || q->_si == NULL) {
486 		printf("%s fs %p si %p, dropping\n",
487 			__FUNCTION__, q->fs, q->_si);
488 		FREE_PKT(m);
489 		return 1;
490 	}
491 	f = &(q->fs->fs);
492 	ni = &q->_si->ni;
493 	len = m->m_pkthdr.len;
494 	/* Update statistics, then check reasons to drop pkt. */
495 	q->ni.tot_bytes += len;
496 	q->ni.tot_pkts++;
497 	ni->tot_bytes += len;
498 	ni->tot_pkts++;
499 	if (drop)
500 		goto drop;
501 	if (f->plr && random() < f->plr)
502 		goto drop;
503 	if (m->m_pkthdr.rcvif != NULL)
504 		m_rcvif_serialize(m);
505 #ifdef NEW_AQM
506 	/* Call AQM enqueue function */
507 	if (q->fs->aqmfp)
508 		return q->fs->aqmfp->enqueue(q ,m);
509 #endif
510 	if (f->flags & DN_IS_RED && red_drops(q, m->m_pkthdr.len)) {
511 		if (!(f->flags & DN_IS_ECN) || !ecn_mark(m))
512 			goto drop;
513 	}
514 	if (f->flags & DN_QSIZE_BYTES) {
515 		if (q->ni.len_bytes > f->qsize)
516 			goto drop;
517 	} else if (q->ni.length >= f->qsize) {
518 		goto drop;
519 	}
520 	mq_append(&q->mq, m);
521 	q->ni.length++;
522 	q->ni.len_bytes += len;
523 	ni->length++;
524 	ni->len_bytes += len;
525 	return (0);
526 
527 drop:
528 	V_dn_cfg.io_pkt_drop++;
529 	q->ni.drops++;
530 	ni->drops++;
531 	FREE_PKT(m);
532 	return (1);
533 }
534 
535 /*
536  * Fetch packets from the delay line which are due now. If there are
537  * leftover packets, reinsert the delay line in the heap.
538  * Runs under scheduler lock.
539  */
540 static void
541 transmit_event(struct mq *q, struct delay_line *dline, uint64_t now)
542 {
543 	struct mbuf *m;
544 	struct dn_pkt_tag *pkt = NULL;
545 
546 	dline->oid.subtype = 0; /* not in heap */
547 	while ((m = dline->mq.head) != NULL) {
548 		pkt = dn_tag_get(m);
549 		if (!DN_KEY_LEQ(pkt->output_time, now))
550 			break;
551 		dline->mq.head = m->m_nextpkt;
552 		dline->mq.count--;
553 		if (m->m_pkthdr.rcvif != NULL &&
554 		  __predict_false(m_rcvif_restore(m) == NULL))
555 			m_freem(m);
556 		else
557 			mq_append(q, m);
558 	}
559 	if (m != NULL) {
560 		dline->oid.subtype = 1; /* in heap */
561 		heap_insert(&V_dn_cfg.evheap, pkt->output_time, dline);
562 	}
563 }
564 
565 /*
566  * Convert the additional MAC overheads/delays into an equivalent
567  * number of bits for the given data rate. The samples are
568  * in milliseconds so we need to divide by 1000.
569  */
570 static uint64_t
571 extra_bits(struct mbuf *m, struct dn_schk *s)
572 {
573 	int index;
574 	uint64_t bits;
575 	struct dn_profile *pf = s->profile;
576 
577 	if (!pf || pf->samples_no == 0)
578 		return 0;
579 	index  = random() % pf->samples_no;
580 	bits = div64((uint64_t)pf->samples[index] * s->link.bandwidth, 1000);
581 	if (index >= pf->loss_level) {
582 		struct dn_pkt_tag *dt = dn_tag_get(m);
583 		if (dt)
584 			dt->dn_dir = DIR_DROP;
585 	}
586 	return bits;
587 }
588 
589 /*
590  * Send traffic from a scheduler instance due by 'now'.
591  * Return a pointer to the head of the queue.
592  */
593 static struct mbuf *
594 serve_sched(struct mq *q, struct dn_sch_inst *si, uint64_t now)
595 {
596 	struct mq def_q;
597 	struct dn_schk *s = si->sched;
598 	struct mbuf *m = NULL;
599 	int delay_line_idle = (si->dline.mq.head == NULL);
600 	int done;
601 	uint32_t bw;
602 
603 	if (q == NULL) {
604 		q = &def_q;
605 		q->head = NULL;
606 	}
607 
608 	bw = s->link.bandwidth;
609 	si->kflags &= ~DN_ACTIVE;
610 
611 	if (bw > 0)
612 		si->credit += (now - si->sched_time) * bw;
613 	else
614 		si->credit = 0;
615 	si->sched_time = now;
616 	done = 0;
617 	while (si->credit >= 0 && (m = s->fp->dequeue(si)) != NULL) {
618 		uint64_t len_scaled;
619 
620 		done++;
621 		len_scaled = (bw == 0) ? 0 : hz *
622 			(m->m_pkthdr.len * 8 + extra_bits(m, s));
623 		si->credit -= len_scaled;
624 		/* Move packet in the delay line */
625 		dn_tag_get(m)->output_time = V_dn_cfg.curr_time + s->link.delay ;
626 		if (m->m_pkthdr.rcvif != NULL)
627 			m_rcvif_serialize(m);
628 		mq_append(&si->dline.mq, m);
629 	}
630 
631 	/*
632 	 * If credit >= 0 the instance is idle, mark time.
633 	 * Otherwise put back in the heap, and adjust the output
634 	 * time of the last inserted packet, m, which was too early.
635 	 */
636 	if (si->credit >= 0) {
637 		si->idle_time = now;
638 	} else {
639 		uint64_t t;
640 		KASSERT (bw > 0, ("bw=0 and credit<0 ?"));
641 		t = div64(bw - 1 - si->credit, bw);
642 		if (m)
643 			dn_tag_get(m)->output_time += t;
644 		si->kflags |= DN_ACTIVE;
645 		heap_insert(&V_dn_cfg.evheap, now + t, si);
646 	}
647 	if (delay_line_idle && done)
648 		transmit_event(q, &si->dline, now);
649 	return q->head;
650 }
651 
652 /*
653  * The timer handler for dummynet. Time is computed in ticks, but
654  * but the code is tolerant to the actual rate at which this is called.
655  * Once complete, the function reschedules itself for the next tick.
656  */
657 void
658 dummynet_task(void *context, int pending)
659 {
660 	struct timeval t;
661 	struct mq q = { NULL, NULL }; /* queue to accumulate results */
662 	struct epoch_tracker et;
663 
664 	VNET_ITERATOR_DECL(vnet_iter);
665 	VNET_LIST_RLOCK();
666 	NET_EPOCH_ENTER(et);
667 
668 	VNET_FOREACH(vnet_iter) {
669 		memset(&q, 0, sizeof(struct mq));
670 		CURVNET_SET(vnet_iter);
671 
672 		if (! V_dn_cfg.init_done) {
673 			CURVNET_RESTORE();
674 			continue;
675 		}
676 
677 		DN_BH_WLOCK();
678 
679 		/* Update number of lost(coalesced) ticks. */
680 		V_dn_cfg.tick_lost += pending - 1;
681 
682 		getmicrouptime(&t);
683 		/* Last tick duration (usec). */
684 		V_dn_cfg.tick_last = (t.tv_sec - V_dn_cfg.prev_t.tv_sec) * 1000000 +
685 		(t.tv_usec - V_dn_cfg.prev_t.tv_usec);
686 		/* Last tick vs standard tick difference (usec). */
687 		V_dn_cfg.tick_delta = (V_dn_cfg.tick_last * hz - 1000000) / hz;
688 		/* Accumulated tick difference (usec). */
689 		V_dn_cfg.tick_delta_sum += V_dn_cfg.tick_delta;
690 
691 		V_dn_cfg.prev_t = t;
692 
693 		/*
694 		* Adjust curr_time if the accumulated tick difference is
695 		* greater than the 'standard' tick. Since curr_time should
696 		* be monotonically increasing, we do positive adjustments
697 		* as required, and throttle curr_time in case of negative
698 		* adjustment.
699 		*/
700 		V_dn_cfg.curr_time++;
701 		if (V_dn_cfg.tick_delta_sum - tick >= 0) {
702 			int diff = V_dn_cfg.tick_delta_sum / tick;
703 
704 			V_dn_cfg.curr_time += diff;
705 			V_dn_cfg.tick_diff += diff;
706 			V_dn_cfg.tick_delta_sum %= tick;
707 			V_dn_cfg.tick_adjustment++;
708 		} else if (V_dn_cfg.tick_delta_sum + tick <= 0) {
709 			V_dn_cfg.curr_time--;
710 			V_dn_cfg.tick_diff--;
711 			V_dn_cfg.tick_delta_sum += tick;
712 			V_dn_cfg.tick_adjustment++;
713 		}
714 
715 		/* serve pending events, accumulate in q */
716 		for (;;) {
717 			struct dn_id *p;    /* generic parameter to handler */
718 
719 			if (V_dn_cfg.evheap.elements == 0 ||
720 			    DN_KEY_LT(V_dn_cfg.curr_time, HEAP_TOP(&V_dn_cfg.evheap)->key))
721 				break;
722 			p = HEAP_TOP(&V_dn_cfg.evheap)->object;
723 			heap_extract(&V_dn_cfg.evheap, NULL);
724 			if (p->type == DN_SCH_I) {
725 				serve_sched(&q, (struct dn_sch_inst *)p, V_dn_cfg.curr_time);
726 			} else { /* extracted a delay line */
727 				transmit_event(&q, (struct delay_line *)p, V_dn_cfg.curr_time);
728 			}
729 		}
730 		if (V_dn_cfg.expire && ++V_dn_cfg.expire_cycle >= V_dn_cfg.expire) {
731 			V_dn_cfg.expire_cycle = 0;
732 			dn_drain_scheduler();
733 			dn_drain_queue();
734 		}
735 		DN_BH_WUNLOCK();
736 		if (q.head != NULL)
737 			dummynet_send(q.head);
738 
739 		CURVNET_RESTORE();
740 	}
741 	NET_EPOCH_EXIT(et);
742 	VNET_LIST_RUNLOCK();
743 
744 	/* Schedule our next run. */
745 	dn_reschedule();
746 }
747 
748 /*
749  * forward a chain of packets to the proper destination.
750  * This runs outside the dummynet lock.
751  */
752 static void
753 dummynet_send(struct mbuf *m)
754 {
755 	struct mbuf *n;
756 
757 	NET_EPOCH_ASSERT();
758 
759 	for (; m != NULL; m = n) {
760 		struct ifnet *ifp = NULL;	/* gcc 3.4.6 complains */
761         	struct m_tag *tag;
762 		int dst;
763 
764 		n = m->m_nextpkt;
765 		m->m_nextpkt = NULL;
766 		tag = m_tag_first(m);
767 		if (tag == NULL) { /* should not happen */
768 			dst = DIR_DROP;
769 		} else {
770 			struct dn_pkt_tag *pkt = dn_tag_get(m);
771 			/* extract the dummynet info, rename the tag
772 			 * to carry reinject info.
773 			 */
774 			ifp = ifnet_byindexgen(pkt->if_index, pkt->if_idxgen);
775 			if (pkt->dn_dir == (DIR_OUT | PROTO_LAYER2) &&
776 				ifp == NULL) {
777 				dst = DIR_DROP;
778 			} else {
779 				dst = pkt->dn_dir;
780 				tag->m_tag_cookie = MTAG_IPFW_RULE;
781 				tag->m_tag_id = 0;
782 			}
783 		}
784 
785 		switch (dst) {
786 		case DIR_OUT:
787 			ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
788 			break ;
789 
790 		case DIR_IN :
791 			netisr_dispatch(NETISR_IP, m);
792 			break;
793 
794 #ifdef INET6
795 		case DIR_IN | PROTO_IPV6:
796 			netisr_dispatch(NETISR_IPV6, m);
797 			break;
798 
799 		case DIR_OUT | PROTO_IPV6:
800 			ip6_output(m, NULL, NULL, IPV6_FORWARDING, NULL, NULL, NULL);
801 			break;
802 #endif
803 
804 		case DIR_FWD | PROTO_IFB: /* DN_TO_IFB_FWD: */
805 			if (bridge_dn_p != NULL)
806 				((*bridge_dn_p)(m, ifp));
807 			else
808 				printf("dummynet: if_bridge not loaded\n");
809 
810 			break;
811 
812 		case DIR_IN | PROTO_LAYER2 | PROTO_IPV6:
813 		case DIR_IN | PROTO_LAYER2: /* DN_TO_ETH_DEMUX: */
814 			/*
815 			 * The Ethernet code assumes the Ethernet header is
816 			 * contiguous in the first mbuf header.
817 			 * Insure this is true.
818 			 */
819 			if (m->m_len < ETHER_HDR_LEN &&
820 			    (m = m_pullup(m, ETHER_HDR_LEN)) == NULL) {
821 				printf("dummynet/ether: pullup failed, "
822 				    "dropping packet\n");
823 				break;
824 			}
825 			ether_demux(m->m_pkthdr.rcvif, m);
826 			break;
827 
828 		case DIR_OUT | PROTO_LAYER2 | PROTO_IPV6:
829 		case DIR_OUT | PROTO_LAYER2: /* DN_TO_ETH_OUT: */
830 			ether_output_frame(ifp, m);
831 			break;
832 
833 		case DIR_DROP:
834 			/* drop the packet after some time */
835 			FREE_PKT(m);
836 			break;
837 
838 		default:
839 			printf("dummynet: bad switch %d!\n", dst);
840 			FREE_PKT(m);
841 			break;
842 		}
843 	}
844 }
845 
846 static inline int
847 tag_mbuf(struct mbuf *m, int dir, struct ip_fw_args *fwa)
848 {
849 	struct dn_pkt_tag *dt;
850 	struct m_tag *mtag;
851 
852 	mtag = m_tag_get(PACKET_TAG_DUMMYNET,
853 		    sizeof(*dt), M_NOWAIT | M_ZERO);
854 	if (mtag == NULL)
855 		return 1;		/* Cannot allocate packet header. */
856 	m_tag_prepend(m, mtag);		/* Attach to mbuf chain. */
857 	dt = (struct dn_pkt_tag *)(mtag + 1);
858 	dt->rule = fwa->rule;
859 	/* only keep this info */
860 	dt->rule.info &= (IPFW_ONEPASS | IPFW_IS_DUMMYNET);
861 	dt->dn_dir = dir;
862 	if (fwa->flags & IPFW_ARGS_OUT && fwa->ifp != NULL) {
863 		NET_EPOCH_ASSERT();
864 		dt->if_index = fwa->ifp->if_index;
865 		dt->if_idxgen = fwa->ifp->if_idxgen;
866 	}
867 	/* dt->output tame is updated as we move through */
868 	dt->output_time = V_dn_cfg.curr_time;
869 	dt->iphdr_off = (dir & PROTO_LAYER2) ? ETHER_HDR_LEN : 0;
870 	return 0;
871 }
872 
873 /*
874  * dummynet hook for packets.
875  * We use the argument to locate the flowset fs and the sched_set sch
876  * associated to it. The we apply flow_mask and sched_mask to
877  * determine the queue and scheduler instances.
878  */
879 int
880 dummynet_io(struct mbuf **m0, struct ip_fw_args *fwa)
881 {
882 	struct mbuf *m = *m0;
883 	struct dn_fsk *fs = NULL;
884 	struct dn_sch_inst *si;
885 	struct dn_queue *q = NULL;	/* default */
886 	int fs_id, dir;
887 
888 	fs_id = (fwa->rule.info & IPFW_INFO_MASK) +
889 		((fwa->rule.info & IPFW_IS_PIPE) ? 2*DN_MAX_ID : 0);
890 	/* XXXGL: convert args to dir */
891 	if (fwa->flags & IPFW_ARGS_IN)
892 		dir = DIR_IN;
893 	else
894 		dir = DIR_OUT;
895 	if (fwa->flags & IPFW_ARGS_ETHER)
896 		dir |= PROTO_LAYER2;
897 	else if (fwa->flags & IPFW_ARGS_IP6)
898 		dir |= PROTO_IPV6;
899 	DN_BH_WLOCK();
900 	V_dn_cfg.io_pkt++;
901 	/* we could actually tag outside the lock, but who cares... */
902 	if (tag_mbuf(m, dir, fwa))
903 		goto dropit;
904 	/* XXX locate_flowset could be optimised with a direct ref. */
905 	fs = dn_ht_find(V_dn_cfg.fshash, fs_id, 0, NULL);
906 	if (fs == NULL)
907 		goto dropit;	/* This queue/pipe does not exist! */
908 	if (fs->sched == NULL)	/* should not happen */
909 		goto dropit;
910 	/* find scheduler instance, possibly applying sched_mask */
911 	si = ipdn_si_find(fs->sched, &(fwa->f_id));
912 	if (si == NULL)
913 		goto dropit;
914 	/*
915 	 * If the scheduler supports multiple queues, find the right one
916 	 * (otherwise it will be ignored by enqueue).
917 	 */
918 	if (fs->sched->fp->flags & DN_MULTIQUEUE) {
919 		q = ipdn_q_find(fs, si, &(fwa->f_id));
920 		if (q == NULL)
921 			goto dropit;
922 	}
923 	if (fs->sched->fp->enqueue(si, q, m)) {
924 		/* packet was dropped by enqueue() */
925 		m = *m0 = NULL;
926 
927 		/* dn_enqueue already increases io_pkt_drop */
928 		V_dn_cfg.io_pkt_drop--;
929 
930 		goto dropit;
931 	}
932 
933 	if (si->kflags & DN_ACTIVE) {
934 		m = *m0 = NULL; /* consumed */
935 		goto done; /* already active, nothing to do */
936 	}
937 
938 	/* compute the initial allowance */
939 	if (si->idle_time < V_dn_cfg.curr_time) {
940 	    /* Do this only on the first packet on an idle pipe */
941 	    struct dn_link *p = &fs->sched->link;
942 
943 	    si->sched_time = V_dn_cfg.curr_time;
944 	    si->credit = V_dn_cfg.io_fast ? p->bandwidth : 0;
945 	    if (p->burst) {
946 		uint64_t burst = (V_dn_cfg.curr_time - si->idle_time) * p->bandwidth;
947 		if (burst > p->burst)
948 			burst = p->burst;
949 		si->credit += burst;
950 	    }
951 	}
952 	/* pass through scheduler and delay line */
953 	m = serve_sched(NULL, si, V_dn_cfg.curr_time);
954 
955 	/* optimization -- pass it back to ipfw for immediate send */
956 	/* XXX Don't call dummynet_send() if scheduler return the packet
957 	 *     just enqueued. This avoid a lock order reversal.
958 	 *
959 	 */
960 	if (/*V_dn_cfg.io_fast &&*/ m == *m0 && (dir & PROTO_LAYER2) == 0 ) {
961 		/* fast io, rename the tag * to carry reinject info. */
962 		struct m_tag *tag = m_tag_first(m);
963 
964 		tag->m_tag_cookie = MTAG_IPFW_RULE;
965 		tag->m_tag_id = 0;
966 		V_dn_cfg.io_pkt_fast++;
967 		if (m->m_nextpkt != NULL) {
968 			printf("dummynet: fast io: pkt chain detected!\n");
969 			m->m_nextpkt = NULL;
970 		}
971 		m = NULL;
972 	} else {
973 		*m0 = NULL;
974 	}
975 done:
976 	DN_BH_WUNLOCK();
977 	if (m)
978 		dummynet_send(m);
979 	return 0;
980 
981 dropit:
982 	V_dn_cfg.io_pkt_drop++;
983 	DN_BH_WUNLOCK();
984 	if (m)
985 		FREE_PKT(m);
986 	*m0 = NULL;
987 	return (fs && (fs->fs.flags & DN_NOERROR)) ? 0 : ENOBUFS;
988 }
989