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