xref: /freebsd/sys/netpfil/ipfw/ip_dn_io.c (revision c03c5b1c)
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 		DN_BH_WLOCK();
673 
674 		/* Update number of lost(coalesced) ticks. */
675 		V_dn_cfg.tick_lost += pending - 1;
676 
677 		getmicrouptime(&t);
678 		/* Last tick duration (usec). */
679 		V_dn_cfg.tick_last = (t.tv_sec - V_dn_cfg.prev_t.tv_sec) * 1000000 +
680 		(t.tv_usec - V_dn_cfg.prev_t.tv_usec);
681 		/* Last tick vs standard tick difference (usec). */
682 		V_dn_cfg.tick_delta = (V_dn_cfg.tick_last * hz - 1000000) / hz;
683 		/* Accumulated tick difference (usec). */
684 		V_dn_cfg.tick_delta_sum += V_dn_cfg.tick_delta;
685 
686 		V_dn_cfg.prev_t = t;
687 
688 		/*
689 		* Adjust curr_time if the accumulated tick difference is
690 		* greater than the 'standard' tick. Since curr_time should
691 		* be monotonically increasing, we do positive adjustments
692 		* as required, and throttle curr_time in case of negative
693 		* adjustment.
694 		*/
695 		V_dn_cfg.curr_time++;
696 		if (V_dn_cfg.tick_delta_sum - tick >= 0) {
697 			int diff = V_dn_cfg.tick_delta_sum / tick;
698 
699 			V_dn_cfg.curr_time += diff;
700 			V_dn_cfg.tick_diff += diff;
701 			V_dn_cfg.tick_delta_sum %= tick;
702 			V_dn_cfg.tick_adjustment++;
703 		} else if (V_dn_cfg.tick_delta_sum + tick <= 0) {
704 			V_dn_cfg.curr_time--;
705 			V_dn_cfg.tick_diff--;
706 			V_dn_cfg.tick_delta_sum += tick;
707 			V_dn_cfg.tick_adjustment++;
708 		}
709 
710 		/* serve pending events, accumulate in q */
711 		for (;;) {
712 			struct dn_id *p;    /* generic parameter to handler */
713 
714 			if (V_dn_cfg.evheap.elements == 0 ||
715 			    DN_KEY_LT(V_dn_cfg.curr_time, HEAP_TOP(&V_dn_cfg.evheap)->key))
716 				break;
717 			p = HEAP_TOP(&V_dn_cfg.evheap)->object;
718 			heap_extract(&V_dn_cfg.evheap, NULL);
719 			if (p->type == DN_SCH_I) {
720 				serve_sched(&q, (struct dn_sch_inst *)p, V_dn_cfg.curr_time);
721 			} else { /* extracted a delay line */
722 				transmit_event(&q, (struct delay_line *)p, V_dn_cfg.curr_time);
723 			}
724 		}
725 		if (V_dn_cfg.expire && ++V_dn_cfg.expire_cycle >= V_dn_cfg.expire) {
726 			V_dn_cfg.expire_cycle = 0;
727 			dn_drain_scheduler();
728 			dn_drain_queue();
729 		}
730 		DN_BH_WUNLOCK();
731 		if (q.head != NULL)
732 			dummynet_send(q.head);
733 
734 		CURVNET_RESTORE();
735 	}
736 	NET_EPOCH_EXIT(et);
737 	VNET_LIST_RUNLOCK();
738 
739 	/* Schedule our next run. */
740 	dn_reschedule();
741 }
742 
743 /*
744  * forward a chain of packets to the proper destination.
745  * This runs outside the dummynet lock.
746  */
747 static void
748 dummynet_send(struct mbuf *m)
749 {
750 	struct mbuf *n;
751 
752 	NET_EPOCH_ASSERT();
753 
754 	for (; m != NULL; m = n) {
755 		struct ifnet *ifp = NULL;	/* gcc 3.4.6 complains */
756         	struct m_tag *tag;
757 		int dst;
758 
759 		n = m->m_nextpkt;
760 		m->m_nextpkt = NULL;
761 		tag = m_tag_first(m);
762 		if (tag == NULL) { /* should not happen */
763 			dst = DIR_DROP;
764 		} else {
765 			struct dn_pkt_tag *pkt = dn_tag_get(m);
766 			/* extract the dummynet info, rename the tag
767 			 * to carry reinject info.
768 			 */
769 			if (pkt->dn_dir == (DIR_OUT | PROTO_LAYER2) &&
770 				pkt->ifp == NULL) {
771 				dst = DIR_DROP;
772 			} else {
773 				dst = pkt->dn_dir;
774 				ifp = pkt->ifp;
775 				tag->m_tag_cookie = MTAG_IPFW_RULE;
776 				tag->m_tag_id = 0;
777 			}
778 		}
779 
780 		switch (dst) {
781 		case DIR_OUT:
782 			ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
783 			break ;
784 
785 		case DIR_IN :
786 			netisr_dispatch(NETISR_IP, m);
787 			break;
788 
789 #ifdef INET6
790 		case DIR_IN | PROTO_IPV6:
791 			netisr_dispatch(NETISR_IPV6, m);
792 			break;
793 
794 		case DIR_OUT | PROTO_IPV6:
795 			ip6_output(m, NULL, NULL, IPV6_FORWARDING, NULL, NULL, NULL);
796 			break;
797 #endif
798 
799 		case DIR_FWD | PROTO_IFB: /* DN_TO_IFB_FWD: */
800 			if (bridge_dn_p != NULL)
801 				((*bridge_dn_p)(m, ifp));
802 			else
803 				printf("dummynet: if_bridge not loaded\n");
804 
805 			break;
806 
807 		case DIR_IN | PROTO_LAYER2: /* DN_TO_ETH_DEMUX: */
808 			/*
809 			 * The Ethernet code assumes the Ethernet header is
810 			 * contiguous in the first mbuf header.
811 			 * Insure this is true.
812 			 */
813 			if (m->m_len < ETHER_HDR_LEN &&
814 			    (m = m_pullup(m, ETHER_HDR_LEN)) == NULL) {
815 				printf("dummynet/ether: pullup failed, "
816 				    "dropping packet\n");
817 				break;
818 			}
819 			ether_demux(m->m_pkthdr.rcvif, m);
820 			break;
821 
822 		case DIR_OUT | PROTO_LAYER2: /* DN_TO_ETH_OUT: */
823 			ether_output_frame(ifp, m);
824 			break;
825 
826 		case DIR_DROP:
827 			/* drop the packet after some time */
828 			FREE_PKT(m);
829 			break;
830 
831 		default:
832 			printf("dummynet: bad switch %d!\n", dst);
833 			FREE_PKT(m);
834 			break;
835 		}
836 	}
837 }
838 
839 static inline int
840 tag_mbuf(struct mbuf *m, int dir, struct ip_fw_args *fwa)
841 {
842 	struct dn_pkt_tag *dt;
843 	struct m_tag *mtag;
844 
845 	mtag = m_tag_get(PACKET_TAG_DUMMYNET,
846 		    sizeof(*dt), M_NOWAIT | M_ZERO);
847 	if (mtag == NULL)
848 		return 1;		/* Cannot allocate packet header. */
849 	m_tag_prepend(m, mtag);		/* Attach to mbuf chain. */
850 	dt = (struct dn_pkt_tag *)(mtag + 1);
851 	dt->rule = fwa->rule;
852 	/* only keep this info */
853 	dt->rule.info &= (IPFW_ONEPASS | IPFW_IS_DUMMYNET);
854 	dt->dn_dir = dir;
855 	dt->ifp = fwa->flags & IPFW_ARGS_OUT ? fwa->ifp : NULL;
856 	/* dt->output tame is updated as we move through */
857 	dt->output_time = V_dn_cfg.curr_time;
858 	dt->iphdr_off = (dir & PROTO_LAYER2) ? ETHER_HDR_LEN : 0;
859 	return 0;
860 }
861 
862 /*
863  * dummynet hook for packets.
864  * We use the argument to locate the flowset fs and the sched_set sch
865  * associated to it. The we apply flow_mask and sched_mask to
866  * determine the queue and scheduler instances.
867  */
868 int
869 dummynet_io(struct mbuf **m0, struct ip_fw_args *fwa)
870 {
871 	struct mbuf *m = *m0;
872 	struct dn_fsk *fs = NULL;
873 	struct dn_sch_inst *si;
874 	struct dn_queue *q = NULL;	/* default */
875 	int fs_id, dir;
876 
877 	fs_id = (fwa->rule.info & IPFW_INFO_MASK) +
878 		((fwa->rule.info & IPFW_IS_PIPE) ? 2*DN_MAX_ID : 0);
879 	/* XXXGL: convert args to dir */
880 	if (fwa->flags & IPFW_ARGS_IN)
881 		dir = DIR_IN;
882 	else
883 		dir = DIR_OUT;
884 	if (fwa->flags & IPFW_ARGS_ETHER)
885 		dir |= PROTO_LAYER2;
886 	else if (fwa->flags & IPFW_ARGS_IP6)
887 		dir |= PROTO_IPV6;
888 	DN_BH_WLOCK();
889 	V_dn_cfg.io_pkt++;
890 	/* we could actually tag outside the lock, but who cares... */
891 	if (tag_mbuf(m, dir, fwa))
892 		goto dropit;
893 	/* XXX locate_flowset could be optimised with a direct ref. */
894 	fs = dn_ht_find(V_dn_cfg.fshash, fs_id, 0, NULL);
895 	if (fs == NULL)
896 		goto dropit;	/* This queue/pipe does not exist! */
897 	if (fs->sched == NULL)	/* should not happen */
898 		goto dropit;
899 	/* find scheduler instance, possibly applying sched_mask */
900 	si = ipdn_si_find(fs->sched, &(fwa->f_id));
901 	if (si == NULL)
902 		goto dropit;
903 	/*
904 	 * If the scheduler supports multiple queues, find the right one
905 	 * (otherwise it will be ignored by enqueue).
906 	 */
907 	if (fs->sched->fp->flags & DN_MULTIQUEUE) {
908 		q = ipdn_q_find(fs, si, &(fwa->f_id));
909 		if (q == NULL)
910 			goto dropit;
911 	}
912 	if (fs->sched->fp->enqueue(si, q, m)) {
913 		/* packet was dropped by enqueue() */
914 		m = *m0 = NULL;
915 
916 		/* dn_enqueue already increases io_pkt_drop */
917 		V_dn_cfg.io_pkt_drop--;
918 
919 		goto dropit;
920 	}
921 
922 	if (si->kflags & DN_ACTIVE) {
923 		m = *m0 = NULL; /* consumed */
924 		goto done; /* already active, nothing to do */
925 	}
926 
927 	/* compute the initial allowance */
928 	if (si->idle_time < V_dn_cfg.curr_time) {
929 	    /* Do this only on the first packet on an idle pipe */
930 	    struct dn_link *p = &fs->sched->link;
931 
932 	    si->sched_time = V_dn_cfg.curr_time;
933 	    si->credit = V_dn_cfg.io_fast ? p->bandwidth : 0;
934 	    if (p->burst) {
935 		uint64_t burst = (V_dn_cfg.curr_time - si->idle_time) * p->bandwidth;
936 		if (burst > p->burst)
937 			burst = p->burst;
938 		si->credit += burst;
939 	    }
940 	}
941 	/* pass through scheduler and delay line */
942 	m = serve_sched(NULL, si, V_dn_cfg.curr_time);
943 
944 	/* optimization -- pass it back to ipfw for immediate send */
945 	/* XXX Don't call dummynet_send() if scheduler return the packet
946 	 *     just enqueued. This avoid a lock order reversal.
947 	 *
948 	 */
949 	if (/*V_dn_cfg.io_fast &&*/ m == *m0 && (dir & PROTO_LAYER2) == 0 ) {
950 		/* fast io, rename the tag * to carry reinject info. */
951 		struct m_tag *tag = m_tag_first(m);
952 
953 		tag->m_tag_cookie = MTAG_IPFW_RULE;
954 		tag->m_tag_id = 0;
955 		V_dn_cfg.io_pkt_fast++;
956 		if (m->m_nextpkt != NULL) {
957 			printf("dummynet: fast io: pkt chain detected!\n");
958 			m->m_nextpkt = NULL;
959 		}
960 		m = NULL;
961 	} else {
962 		*m0 = NULL;
963 	}
964 done:
965 	DN_BH_WUNLOCK();
966 	if (m)
967 		dummynet_send(m);
968 	return 0;
969 
970 dropit:
971 	V_dn_cfg.io_pkt_drop++;
972 	DN_BH_WUNLOCK();
973 	if (m)
974 		FREE_PKT(m);
975 	*m0 = NULL;
976 	return (fs && (fs->fs.flags & DN_NOERROR)) ? 0 : ENOBUFS;
977 }
978