xref: /freebsd/sys/netpfil/ipfw/ip_dn_io.c (revision 31cf66d7554c2fa6a5aea77f4cd54712e611cdd0)
13b3a8eb9SGleb Smirnoff /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni  *
43b3a8eb9SGleb Smirnoff  * Copyright (c) 2010 Luigi Rizzo, Riccardo Panicucci, Universita` di Pisa
53b3a8eb9SGleb Smirnoff  * All rights reserved
63b3a8eb9SGleb Smirnoff  *
73b3a8eb9SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
83b3a8eb9SGleb Smirnoff  * modification, are permitted provided that the following conditions
93b3a8eb9SGleb Smirnoff  * are met:
103b3a8eb9SGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
113b3a8eb9SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
123b3a8eb9SGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
133b3a8eb9SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
143b3a8eb9SGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
153b3a8eb9SGleb Smirnoff  *
163b3a8eb9SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
173b3a8eb9SGleb Smirnoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
183b3a8eb9SGleb Smirnoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
193b3a8eb9SGleb Smirnoff  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
203b3a8eb9SGleb Smirnoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
213b3a8eb9SGleb Smirnoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
223b3a8eb9SGleb Smirnoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
233b3a8eb9SGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
243b3a8eb9SGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
253b3a8eb9SGleb Smirnoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
263b3a8eb9SGleb Smirnoff  * SUCH DAMAGE.
273b3a8eb9SGleb Smirnoff  */
283b3a8eb9SGleb Smirnoff 
293b3a8eb9SGleb Smirnoff /*
303b3a8eb9SGleb Smirnoff  * Dummynet portions related to packet handling.
313b3a8eb9SGleb Smirnoff  */
323b3a8eb9SGleb Smirnoff #include <sys/cdefs.h>
333b3a8eb9SGleb Smirnoff #include "opt_inet6.h"
343b3a8eb9SGleb Smirnoff 
353b3a8eb9SGleb Smirnoff #include <sys/param.h>
363b3a8eb9SGleb Smirnoff #include <sys/systm.h>
373b3a8eb9SGleb Smirnoff #include <sys/malloc.h>
383b3a8eb9SGleb Smirnoff #include <sys/mbuf.h>
393b3a8eb9SGleb Smirnoff #include <sys/kernel.h>
403b3a8eb9SGleb Smirnoff #include <sys/lock.h>
413b3a8eb9SGleb Smirnoff #include <sys/module.h>
42eedc7fd9SGleb Smirnoff #include <sys/mutex.h>
433b3a8eb9SGleb Smirnoff #include <sys/priv.h>
443b3a8eb9SGleb Smirnoff #include <sys/proc.h>
453b3a8eb9SGleb Smirnoff #include <sys/rwlock.h>
463b3a8eb9SGleb Smirnoff #include <sys/socket.h>
473b3a8eb9SGleb Smirnoff #include <sys/time.h>
483b3a8eb9SGleb Smirnoff #include <sys/sysctl.h>
493b3a8eb9SGleb Smirnoff 
503b3a8eb9SGleb Smirnoff #include <net/if.h>	/* IFNAMSIZ, struct ifaddr, ifq head, lock.h mutex.h */
51b8a6e03fSGleb Smirnoff #include <net/if_var.h>	/* NET_EPOCH_... */
523d0d5b21SJustin Hibbits #include <net/if_private.h>
533b3a8eb9SGleb Smirnoff #include <net/netisr.h>
543b3a8eb9SGleb Smirnoff #include <net/vnet.h>
553b3a8eb9SGleb Smirnoff 
563b3a8eb9SGleb Smirnoff #include <netinet/in.h>
573b3a8eb9SGleb Smirnoff #include <netinet/ip.h>		/* ip_len, ip_off */
583b3a8eb9SGleb Smirnoff #include <netinet/ip_var.h>	/* ip_output(), IP_FORWARDING */
593b3a8eb9SGleb Smirnoff #include <netinet/ip_fw.h>
603b3a8eb9SGleb Smirnoff #include <netinet/ip_dummynet.h>
613b3a8eb9SGleb Smirnoff #include <netinet/if_ether.h> /* various ether_* routines */
623b3a8eb9SGleb Smirnoff #include <netinet/ip6.h>       /* for ip6_input, ip6_output prototypes */
633b3a8eb9SGleb Smirnoff #include <netinet6/ip6_var.h>
643b3a8eb9SGleb Smirnoff 
653b3a8eb9SGleb Smirnoff #include <netpfil/ipfw/ip_fw_private.h>
663b3a8eb9SGleb Smirnoff #include <netpfil/ipfw/dn_heap.h>
673b3a8eb9SGleb Smirnoff #include <netpfil/ipfw/ip_dn_private.h>
6891336b40SDon Lewis #ifdef NEW_AQM
6991336b40SDon Lewis #include <netpfil/ipfw/dn_aqm.h>
7091336b40SDon Lewis #endif
713b3a8eb9SGleb Smirnoff #include <netpfil/ipfw/dn_sched.h>
723b3a8eb9SGleb Smirnoff 
733b3a8eb9SGleb Smirnoff /*
743b3a8eb9SGleb Smirnoff  * We keep a private variable for the simulation time, but we could
753b3a8eb9SGleb Smirnoff  * probably use an existing one ("softticks" in sys/kern/kern_timeout.c)
76fe3bcfbdSTom Jones  * instead of V_dn_cfg.curr_time
773b3a8eb9SGleb Smirnoff  */
78fe3bcfbdSTom Jones VNET_DEFINE(struct dn_parms, dn_cfg);
79fe3bcfbdSTom Jones #define V_dn_cfg VNET(dn_cfg)
803b3a8eb9SGleb Smirnoff 
813b3a8eb9SGleb Smirnoff /*
823b3a8eb9SGleb Smirnoff  * We use a heap to store entities for which we have pending timer events.
833b3a8eb9SGleb Smirnoff  * The heap is checked at every tick and all entities with expired events
843b3a8eb9SGleb Smirnoff  * are extracted.
853b3a8eb9SGleb Smirnoff  */
863b3a8eb9SGleb Smirnoff 
873b3a8eb9SGleb Smirnoff MALLOC_DEFINE(M_DUMMYNET, "dummynet", "dummynet heap");
883b3a8eb9SGleb Smirnoff 
893b3a8eb9SGleb Smirnoff extern	void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
903b3a8eb9SGleb Smirnoff 
913b3a8eb9SGleb Smirnoff #ifdef SYSCTL_NODE
923b3a8eb9SGleb Smirnoff 
933b3a8eb9SGleb Smirnoff /*
943b3a8eb9SGleb Smirnoff  * Because of the way the SYSBEGIN/SYSEND macros work on other
953b3a8eb9SGleb Smirnoff  * platforms, there should not be functions between them.
963b3a8eb9SGleb Smirnoff  * So keep the handlers outside the block.
973b3a8eb9SGleb Smirnoff  */
983b3a8eb9SGleb Smirnoff static int
sysctl_hash_size(SYSCTL_HANDLER_ARGS)993b3a8eb9SGleb Smirnoff sysctl_hash_size(SYSCTL_HANDLER_ARGS)
1003b3a8eb9SGleb Smirnoff {
1013b3a8eb9SGleb Smirnoff 	int error, value;
1023b3a8eb9SGleb Smirnoff 
103fe3bcfbdSTom Jones 	value = V_dn_cfg.hash_size;
1043b3a8eb9SGleb Smirnoff 	error = sysctl_handle_int(oidp, &value, 0, req);
1053b3a8eb9SGleb Smirnoff 	if (error != 0 || req->newptr == NULL)
1063b3a8eb9SGleb Smirnoff 		return (error);
1073b3a8eb9SGleb Smirnoff 	if (value < 16 || value > 65536)
1083b3a8eb9SGleb Smirnoff 		return (EINVAL);
109fe3bcfbdSTom Jones 	V_dn_cfg.hash_size = value;
1103b3a8eb9SGleb Smirnoff 	return (0);
1113b3a8eb9SGleb Smirnoff }
1123b3a8eb9SGleb Smirnoff 
1133b3a8eb9SGleb Smirnoff static int
sysctl_limits(SYSCTL_HANDLER_ARGS)1143b3a8eb9SGleb Smirnoff sysctl_limits(SYSCTL_HANDLER_ARGS)
1153b3a8eb9SGleb Smirnoff {
1163b3a8eb9SGleb Smirnoff 	int error;
1173b3a8eb9SGleb Smirnoff 	long value;
1183b3a8eb9SGleb Smirnoff 
1193b3a8eb9SGleb Smirnoff 	if (arg2 != 0)
120fe3bcfbdSTom Jones 		value = V_dn_cfg.slot_limit;
1213b3a8eb9SGleb Smirnoff 	else
122fe3bcfbdSTom Jones 		value = V_dn_cfg.byte_limit;
1233b3a8eb9SGleb Smirnoff 	error = sysctl_handle_long(oidp, &value, 0, req);
1243b3a8eb9SGleb Smirnoff 
1253b3a8eb9SGleb Smirnoff 	if (error != 0 || req->newptr == NULL)
1263b3a8eb9SGleb Smirnoff 		return (error);
1273b3a8eb9SGleb Smirnoff 	if (arg2 != 0) {
1283b3a8eb9SGleb Smirnoff 		if (value < 1)
1293b3a8eb9SGleb Smirnoff 			return (EINVAL);
130fe3bcfbdSTom Jones 		V_dn_cfg.slot_limit = value;
1313b3a8eb9SGleb Smirnoff 	} else {
1323b3a8eb9SGleb Smirnoff 		if (value < 1500)
1333b3a8eb9SGleb Smirnoff 			return (EINVAL);
134fe3bcfbdSTom Jones 		V_dn_cfg.byte_limit = value;
1353b3a8eb9SGleb Smirnoff 	}
1363b3a8eb9SGleb Smirnoff 	return (0);
1373b3a8eb9SGleb Smirnoff }
1383b3a8eb9SGleb Smirnoff 
1393b3a8eb9SGleb Smirnoff SYSBEGIN(f4)
1403b3a8eb9SGleb Smirnoff 
1413b3a8eb9SGleb Smirnoff SYSCTL_DECL(_net_inet);
1423b3a8eb9SGleb Smirnoff SYSCTL_DECL(_net_inet_ip);
14391336b40SDon Lewis #ifdef NEW_AQM
1447029da5cSPawel Biernacki SYSCTL_NODE(_net_inet_ip, OID_AUTO, dummynet, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1457029da5cSPawel Biernacki     "Dummynet");
14691336b40SDon Lewis #else
1477029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_ip, OID_AUTO, dummynet,
1487029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1497029da5cSPawel Biernacki     "Dummynet");
15091336b40SDon Lewis #endif
1513b3a8eb9SGleb Smirnoff 
152fe3bcfbdSTom Jones /* wrapper to pass V_dn_cfg fields to SYSCTL_* */
153fe3bcfbdSTom Jones #define DC(x)	(&(VNET_NAME(dn_cfg).x))
154fe3bcfbdSTom Jones 
1553b3a8eb9SGleb Smirnoff /* parameters */
1563b3a8eb9SGleb Smirnoff 
1573b3a8eb9SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_dummynet, OID_AUTO, hash_size,
1587029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1597029da5cSPawel Biernacki     0, 0, sysctl_hash_size, "I",
1607029da5cSPawel Biernacki     "Default hash table size");
1613b3a8eb9SGleb Smirnoff 
1623b3a8eb9SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_dummynet, OID_AUTO, pipe_slot_limit,
1637029da5cSPawel Biernacki     CTLTYPE_LONG | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1647029da5cSPawel Biernacki     0, 1, sysctl_limits, "L",
1657029da5cSPawel Biernacki     "Upper limit in slots for pipe queue.");
1663b3a8eb9SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_dummynet, OID_AUTO, pipe_byte_limit,
1677029da5cSPawel Biernacki     CTLTYPE_LONG | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1687029da5cSPawel Biernacki     0, 0, sysctl_limits, "L",
1697029da5cSPawel Biernacki     "Upper limit in bytes for pipe queue.");
1703b3a8eb9SGleb Smirnoff SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, io_fast,
1718f76eebcSKristof Provost     CTLFLAG_RW | CTLFLAG_VNET, DC(io_fast), 0, "Enable fast dummynet io.");
1723b3a8eb9SGleb Smirnoff SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, debug,
1738f76eebcSKristof Provost     CTLFLAG_RW | CTLFLAG_VNET, DC(debug), 0, "Dummynet debug level");
1743b3a8eb9SGleb Smirnoff 
1753b3a8eb9SGleb Smirnoff /* RED parameters */
1763b3a8eb9SGleb Smirnoff SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, red_lookup_depth,
1778f76eebcSKristof Provost     CTLFLAG_RD | CTLFLAG_VNET, DC(red_lookup_depth), 0, "Depth of RED lookup table");
1783b3a8eb9SGleb Smirnoff SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, red_avg_pkt_size,
1798f76eebcSKristof Provost     CTLFLAG_RD | CTLFLAG_VNET, DC(red_avg_pkt_size), 0, "RED Medium packet size");
1803b3a8eb9SGleb Smirnoff SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, red_max_pkt_size,
1818f76eebcSKristof Provost     CTLFLAG_RD | CTLFLAG_VNET, DC(red_max_pkt_size), 0, "RED Max packet size");
1823b3a8eb9SGleb Smirnoff 
1833b3a8eb9SGleb Smirnoff /* time adjustment */
1843b3a8eb9SGleb Smirnoff SYSCTL_LONG(_net_inet_ip_dummynet, OID_AUTO, tick_delta,
1858f76eebcSKristof Provost     CTLFLAG_RD | CTLFLAG_VNET, DC(tick_delta), 0, "Last vs standard tick difference (usec).");
1863b3a8eb9SGleb Smirnoff SYSCTL_LONG(_net_inet_ip_dummynet, OID_AUTO, tick_delta_sum,
1878f76eebcSKristof Provost     CTLFLAG_RD | CTLFLAG_VNET, DC(tick_delta_sum), 0, "Accumulated tick difference (usec).");
1883b3a8eb9SGleb Smirnoff SYSCTL_LONG(_net_inet_ip_dummynet, OID_AUTO, tick_adjustment,
1898f76eebcSKristof Provost     CTLFLAG_RD | CTLFLAG_VNET, DC(tick_adjustment), 0, "Tick adjustments done.");
1903b3a8eb9SGleb Smirnoff SYSCTL_LONG(_net_inet_ip_dummynet, OID_AUTO, tick_diff,
1918f76eebcSKristof Provost     CTLFLAG_RD | CTLFLAG_VNET, DC(tick_diff), 0,
1923b3a8eb9SGleb Smirnoff     "Adjusted vs non-adjusted curr_time difference (ticks).");
1933b3a8eb9SGleb Smirnoff SYSCTL_LONG(_net_inet_ip_dummynet, OID_AUTO, tick_lost,
1948f76eebcSKristof Provost     CTLFLAG_RD | CTLFLAG_VNET, DC(tick_lost), 0,
1953b3a8eb9SGleb Smirnoff     "Number of ticks coalesced by dummynet taskqueue.");
1963b3a8eb9SGleb Smirnoff 
1973b3a8eb9SGleb Smirnoff /* Drain parameters */
1983b3a8eb9SGleb Smirnoff SYSCTL_UINT(_net_inet_ip_dummynet, OID_AUTO, expire,
1998f76eebcSKristof Provost     CTLFLAG_RW | CTLFLAG_VNET, DC(expire), 0, "Expire empty queues/pipes");
2003b3a8eb9SGleb Smirnoff SYSCTL_UINT(_net_inet_ip_dummynet, OID_AUTO, expire_cycle,
2018f76eebcSKristof Provost     CTLFLAG_RD | CTLFLAG_VNET, DC(expire_cycle), 0, "Expire cycle for queues/pipes");
2023b3a8eb9SGleb Smirnoff 
2033b3a8eb9SGleb Smirnoff /* statistics */
2043b3a8eb9SGleb Smirnoff SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, schk_count,
2058f76eebcSKristof Provost     CTLFLAG_RD | CTLFLAG_VNET, DC(schk_count), 0, "Number of schedulers");
2063b3a8eb9SGleb Smirnoff SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, si_count,
2078f76eebcSKristof Provost     CTLFLAG_RD | CTLFLAG_VNET, DC(si_count), 0, "Number of scheduler instances");
2083b3a8eb9SGleb Smirnoff SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, fsk_count,
2098f76eebcSKristof Provost     CTLFLAG_RD | CTLFLAG_VNET, DC(fsk_count), 0, "Number of flowsets");
2103b3a8eb9SGleb Smirnoff SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, queue_count,
2118f76eebcSKristof Provost     CTLFLAG_RD | CTLFLAG_VNET, DC(queue_count), 0, "Number of queues");
2123b3a8eb9SGleb Smirnoff SYSCTL_ULONG(_net_inet_ip_dummynet, OID_AUTO, io_pkt,
2138f76eebcSKristof Provost     CTLFLAG_RD | CTLFLAG_VNET, DC(io_pkt), 0,
2143b3a8eb9SGleb Smirnoff     "Number of packets passed to dummynet.");
2153b3a8eb9SGleb Smirnoff SYSCTL_ULONG(_net_inet_ip_dummynet, OID_AUTO, io_pkt_fast,
2168f76eebcSKristof Provost     CTLFLAG_RD | CTLFLAG_VNET, DC(io_pkt_fast), 0,
2173b3a8eb9SGleb Smirnoff     "Number of packets bypassed dummynet scheduler.");
2183b3a8eb9SGleb Smirnoff SYSCTL_ULONG(_net_inet_ip_dummynet, OID_AUTO, io_pkt_drop,
2198f76eebcSKristof Provost     CTLFLAG_RD | CTLFLAG_VNET, DC(io_pkt_drop), 0,
2203b3a8eb9SGleb Smirnoff     "Number of packets dropped by dummynet.");
2213b3a8eb9SGleb Smirnoff #undef DC
2223b3a8eb9SGleb Smirnoff SYSEND
2233b3a8eb9SGleb Smirnoff 
2243b3a8eb9SGleb Smirnoff #endif
2253b3a8eb9SGleb Smirnoff 
2263b3a8eb9SGleb Smirnoff static void	dummynet_send(struct mbuf *);
2273b3a8eb9SGleb Smirnoff 
2283b3a8eb9SGleb Smirnoff /*
2293b3a8eb9SGleb Smirnoff  * Return the mbuf tag holding the dummynet state (it should
2303b3a8eb9SGleb Smirnoff  * be the first one on the list).
2313b3a8eb9SGleb Smirnoff  */
2324001fcbeSDon Lewis struct dn_pkt_tag *
dn_tag_get(struct mbuf * m)2333b3a8eb9SGleb Smirnoff dn_tag_get(struct mbuf *m)
2343b3a8eb9SGleb Smirnoff {
2353b3a8eb9SGleb Smirnoff 	struct m_tag *mtag = m_tag_first(m);
23691336b40SDon Lewis #ifdef NEW_AQM
23791336b40SDon Lewis 	/* XXX: to skip ts m_tag. For Debugging only*/
23891336b40SDon Lewis 	if (mtag != NULL && mtag->m_tag_id == DN_AQM_MTAG_TS) {
23991336b40SDon Lewis 		m_tag_delete(m,mtag);
24091336b40SDon Lewis 		mtag = m_tag_first(m);
24191336b40SDon Lewis 		D("skip TS tag");
24291336b40SDon Lewis 	}
24391336b40SDon Lewis #endif
2443b3a8eb9SGleb Smirnoff 	KASSERT(mtag != NULL &&
2453b3a8eb9SGleb Smirnoff 	    mtag->m_tag_cookie == MTAG_ABI_COMPAT &&
2463b3a8eb9SGleb Smirnoff 	    mtag->m_tag_id == PACKET_TAG_DUMMYNET,
2473b3a8eb9SGleb Smirnoff 	    ("packet on dummynet queue w/o dummynet tag!"));
2483b3a8eb9SGleb Smirnoff 	return (struct dn_pkt_tag *)(mtag+1);
2493b3a8eb9SGleb Smirnoff }
2503b3a8eb9SGleb Smirnoff 
25191336b40SDon Lewis #ifndef NEW_AQM
2523b3a8eb9SGleb Smirnoff static inline void
mq_append(struct mq * q,struct mbuf * m)2533b3a8eb9SGleb Smirnoff mq_append(struct mq *q, struct mbuf *m)
2543b3a8eb9SGleb Smirnoff {
25541d10f90SLuigi Rizzo #ifdef USERSPACE
25641d10f90SLuigi Rizzo 	// buffers from netmap need to be copied
25741d10f90SLuigi Rizzo 	// XXX note that the routine is not expected to fail
25841d10f90SLuigi Rizzo 	ND("append %p to %p", m, q);
25941d10f90SLuigi Rizzo 	if (m->m_flags & M_STACK) {
26041d10f90SLuigi Rizzo 		struct mbuf *m_new;
26141d10f90SLuigi Rizzo 		void *p;
26241d10f90SLuigi Rizzo 		int l, ofs;
26341d10f90SLuigi Rizzo 
26441d10f90SLuigi Rizzo 		ofs = m->m_data - m->__m_extbuf;
26541d10f90SLuigi Rizzo 		// XXX allocate
26641d10f90SLuigi Rizzo 		MGETHDR(m_new, M_NOWAIT, MT_DATA);
26741d10f90SLuigi Rizzo 		ND("*** WARNING, volatile buf %p ext %p %d dofs %d m_new %p",
26841d10f90SLuigi Rizzo 			m, m->__m_extbuf, m->__m_extlen, ofs, m_new);
26941d10f90SLuigi Rizzo 		p = m_new->__m_extbuf;	/* new pointer */
27041d10f90SLuigi Rizzo 		l = m_new->__m_extlen;	/* new len */
27141d10f90SLuigi Rizzo 		if (l <= m->__m_extlen) {
27241d10f90SLuigi Rizzo 			panic("extlen too large");
27341d10f90SLuigi Rizzo 		}
27441d10f90SLuigi Rizzo 
27541d10f90SLuigi Rizzo 		*m_new = *m;	// copy
27641d10f90SLuigi Rizzo 		m_new->m_flags &= ~M_STACK;
27741d10f90SLuigi Rizzo 		m_new->__m_extbuf = p; // point to new buffer
27803be41e6SLuigi Rizzo 		_pkt_copy(m->__m_extbuf, p, m->__m_extlen);
27941d10f90SLuigi Rizzo 		m_new->m_data = p + ofs;
28041d10f90SLuigi Rizzo 		m = m_new;
28141d10f90SLuigi Rizzo 	}
28241d10f90SLuigi Rizzo #endif /* USERSPACE */
2833b3a8eb9SGleb Smirnoff 	if (q->head == NULL)
2843b3a8eb9SGleb Smirnoff 		q->head = m;
2853b3a8eb9SGleb Smirnoff 	else
2863b3a8eb9SGleb Smirnoff 		q->tail->m_nextpkt = m;
28741d10f90SLuigi Rizzo 	q->count++;
2883b3a8eb9SGleb Smirnoff 	q->tail = m;
2893b3a8eb9SGleb Smirnoff 	m->m_nextpkt = NULL;
2903b3a8eb9SGleb Smirnoff }
29191336b40SDon Lewis #endif
2923b3a8eb9SGleb Smirnoff 
2933b3a8eb9SGleb Smirnoff /*
2943b3a8eb9SGleb Smirnoff  * Dispose a list of packet. Use a functions so if we need to do
2953b3a8eb9SGleb Smirnoff  * more work, this is a central point to do it.
2963b3a8eb9SGleb Smirnoff  */
dn_free_pkts(struct mbuf * mnext)2973b3a8eb9SGleb Smirnoff void dn_free_pkts(struct mbuf *mnext)
2983b3a8eb9SGleb Smirnoff {
2993b3a8eb9SGleb Smirnoff         struct mbuf *m;
3003b3a8eb9SGleb Smirnoff 
3013b3a8eb9SGleb Smirnoff         while ((m = mnext) != NULL) {
3023b3a8eb9SGleb Smirnoff                 mnext = m->m_nextpkt;
3033b3a8eb9SGleb Smirnoff                 FREE_PKT(m);
3043b3a8eb9SGleb Smirnoff         }
3053b3a8eb9SGleb Smirnoff }
3063b3a8eb9SGleb Smirnoff 
3073b3a8eb9SGleb Smirnoff static int
red_drops(struct dn_queue * q,int len)3083b3a8eb9SGleb Smirnoff red_drops (struct dn_queue *q, int len)
3093b3a8eb9SGleb Smirnoff {
3103b3a8eb9SGleb Smirnoff 	/*
3113b3a8eb9SGleb Smirnoff 	 * RED algorithm
3123b3a8eb9SGleb Smirnoff 	 *
3133b3a8eb9SGleb Smirnoff 	 * RED calculates the average queue size (avg) using a low-pass filter
3143b3a8eb9SGleb Smirnoff 	 * with an exponential weighted (w_q) moving average:
3153b3a8eb9SGleb Smirnoff 	 * 	avg  <-  (1-w_q) * avg + w_q * q_size
3163b3a8eb9SGleb Smirnoff 	 * where q_size is the queue length (measured in bytes or * packets).
3173b3a8eb9SGleb Smirnoff 	 *
3183b3a8eb9SGleb Smirnoff 	 * If q_size == 0, we compute the idle time for the link, and set
3193b3a8eb9SGleb Smirnoff 	 *	avg = (1 - w_q)^(idle/s)
3203b3a8eb9SGleb Smirnoff 	 * where s is the time needed for transmitting a medium-sized packet.
3213b3a8eb9SGleb Smirnoff 	 *
3223b3a8eb9SGleb Smirnoff 	 * Now, if avg < min_th the packet is enqueued.
3233b3a8eb9SGleb Smirnoff 	 * If avg > max_th the packet is dropped. Otherwise, the packet is
3243b3a8eb9SGleb Smirnoff 	 * dropped with probability P function of avg.
3253b3a8eb9SGleb Smirnoff 	 */
3263b3a8eb9SGleb Smirnoff 
3273b3a8eb9SGleb Smirnoff 	struct dn_fsk *fs = q->fs;
3283b3a8eb9SGleb Smirnoff 	int64_t p_b = 0;
3293b3a8eb9SGleb Smirnoff 
3303b3a8eb9SGleb Smirnoff 	/* Queue in bytes or packets? */
3313b3a8eb9SGleb Smirnoff 	uint32_t q_size = (fs->fs.flags & DN_QSIZE_BYTES) ?
3323b3a8eb9SGleb Smirnoff 	    q->ni.len_bytes : q->ni.length;
3333b3a8eb9SGleb Smirnoff 
3343b3a8eb9SGleb Smirnoff 	/* Average queue size estimation. */
3353b3a8eb9SGleb Smirnoff 	if (q_size != 0) {
3363b3a8eb9SGleb Smirnoff 		/* Queue is not empty, avg <- avg + (q_size - avg) * w_q */
3373b3a8eb9SGleb Smirnoff 		int diff = SCALE(q_size) - q->avg;
3383b3a8eb9SGleb Smirnoff 		int64_t v = SCALE_MUL((int64_t)diff, (int64_t)fs->w_q);
3393b3a8eb9SGleb Smirnoff 
3403b3a8eb9SGleb Smirnoff 		q->avg += (int)v;
3413b3a8eb9SGleb Smirnoff 	} else {
3423b3a8eb9SGleb Smirnoff 		/*
3433b3a8eb9SGleb Smirnoff 		 * Queue is empty, find for how long the queue has been
3443b3a8eb9SGleb Smirnoff 		 * empty and use a lookup table for computing
3453b3a8eb9SGleb Smirnoff 		 * (1 - * w_q)^(idle_time/s) where s is the time to send a
3463b3a8eb9SGleb Smirnoff 		 * (small) packet.
3473b3a8eb9SGleb Smirnoff 		 * XXX check wraps...
3483b3a8eb9SGleb Smirnoff 		 */
3493b3a8eb9SGleb Smirnoff 		if (q->avg) {
350fe3bcfbdSTom Jones 			u_int t = div64((V_dn_cfg.curr_time - q->q_time), fs->lookup_step);
3513b3a8eb9SGleb Smirnoff 
3523b3a8eb9SGleb Smirnoff 			q->avg = (t < fs->lookup_depth) ?
3533b3a8eb9SGleb Smirnoff 			    SCALE_MUL(q->avg, fs->w_q_lookup[t]) : 0;
3543b3a8eb9SGleb Smirnoff 		}
3553b3a8eb9SGleb Smirnoff 	}
3563b3a8eb9SGleb Smirnoff 
3573b3a8eb9SGleb Smirnoff 	/* Should i drop? */
3583b3a8eb9SGleb Smirnoff 	if (q->avg < fs->min_th) {
3593b3a8eb9SGleb Smirnoff 		q->count = -1;
3603b3a8eb9SGleb Smirnoff 		return (0);	/* accept packet */
3613b3a8eb9SGleb Smirnoff 	}
3623b3a8eb9SGleb Smirnoff 	if (q->avg >= fs->max_th) {	/* average queue >=  max threshold */
363fc5e1956SHiren Panchasara 		if (fs->fs.flags & DN_IS_ECN)
364fc5e1956SHiren Panchasara 			return (1);
3653b3a8eb9SGleb Smirnoff 		if (fs->fs.flags & DN_IS_GENTLE_RED) {
3663b3a8eb9SGleb Smirnoff 			/*
3673b3a8eb9SGleb Smirnoff 			 * According to Gentle-RED, if avg is greater than
3683b3a8eb9SGleb Smirnoff 			 * max_th the packet is dropped with a probability
3693b3a8eb9SGleb Smirnoff 			 *	 p_b = c_3 * avg - c_4
3703b3a8eb9SGleb Smirnoff 			 * where c_3 = (1 - max_p) / max_th
3713b3a8eb9SGleb Smirnoff 			 *       c_4 = 1 - 2 * max_p
3723b3a8eb9SGleb Smirnoff 			 */
3733b3a8eb9SGleb Smirnoff 			p_b = SCALE_MUL((int64_t)fs->c_3, (int64_t)q->avg) -
3743b3a8eb9SGleb Smirnoff 			    fs->c_4;
3753b3a8eb9SGleb Smirnoff 		} else {
3763b3a8eb9SGleb Smirnoff 			q->count = -1;
3773b3a8eb9SGleb Smirnoff 			return (1);
3783b3a8eb9SGleb Smirnoff 		}
3793b3a8eb9SGleb Smirnoff 	} else if (q->avg > fs->min_th) {
380fc5e1956SHiren Panchasara 		if (fs->fs.flags & DN_IS_ECN)
381fc5e1956SHiren Panchasara 			return (1);
3823b3a8eb9SGleb Smirnoff 		/*
3833b3a8eb9SGleb Smirnoff 		 * We compute p_b using the linear dropping function
3843b3a8eb9SGleb Smirnoff 		 *	 p_b = c_1 * avg - c_2
3853b3a8eb9SGleb Smirnoff 		 * where c_1 = max_p / (max_th - min_th)
3863b3a8eb9SGleb Smirnoff 		 * 	 c_2 = max_p * min_th / (max_th - min_th)
3873b3a8eb9SGleb Smirnoff 		 */
3883b3a8eb9SGleb Smirnoff 		p_b = SCALE_MUL((int64_t)fs->c_1, (int64_t)q->avg) - fs->c_2;
3893b3a8eb9SGleb Smirnoff 	}
3903b3a8eb9SGleb Smirnoff 
3913b3a8eb9SGleb Smirnoff 	if (fs->fs.flags & DN_QSIZE_BYTES)
3923b3a8eb9SGleb Smirnoff 		p_b = div64((p_b * len) , fs->max_pkt_size);
3933b3a8eb9SGleb Smirnoff 	if (++q->count == 0)
3943b3a8eb9SGleb Smirnoff 		q->random = random() & 0xffff;
3953b3a8eb9SGleb Smirnoff 	else {
3963b3a8eb9SGleb Smirnoff 		/*
3973b3a8eb9SGleb Smirnoff 		 * q->count counts packets arrived since last drop, so a greater
3983b3a8eb9SGleb Smirnoff 		 * value of q->count means a greater packet drop probability.
3993b3a8eb9SGleb Smirnoff 		 */
4003b3a8eb9SGleb Smirnoff 		if (SCALE_MUL(p_b, SCALE((int64_t)q->count)) > q->random) {
4013b3a8eb9SGleb Smirnoff 			q->count = 0;
4023b3a8eb9SGleb Smirnoff 			/* After a drop we calculate a new random value. */
4033b3a8eb9SGleb Smirnoff 			q->random = random() & 0xffff;
4043b3a8eb9SGleb Smirnoff 			return (1);	/* drop */
4053b3a8eb9SGleb Smirnoff 		}
4063b3a8eb9SGleb Smirnoff 	}
4073b3a8eb9SGleb Smirnoff 	/* End of RED algorithm. */
4083b3a8eb9SGleb Smirnoff 
4093b3a8eb9SGleb Smirnoff 	return (0);	/* accept */
4103b3a8eb9SGleb Smirnoff 
4113b3a8eb9SGleb Smirnoff }
4123b3a8eb9SGleb Smirnoff 
4133b3a8eb9SGleb Smirnoff /*
414fc5e1956SHiren Panchasara  * ECN/ECT Processing (partially adopted from altq)
415fc5e1956SHiren Panchasara  */
41691336b40SDon Lewis #ifndef NEW_AQM
41791336b40SDon Lewis static
41891336b40SDon Lewis #endif
41991336b40SDon Lewis int
ecn_mark(struct mbuf * m)420fc5e1956SHiren Panchasara ecn_mark(struct mbuf* m)
421fc5e1956SHiren Panchasara {
422fc5e1956SHiren Panchasara 	struct ip *ip;
4234001fcbeSDon Lewis 	ip = (struct ip *)mtodo(m, dn_tag_get(m)->iphdr_off);
424fc5e1956SHiren Panchasara 
425fc5e1956SHiren Panchasara 	switch (ip->ip_v) {
426fc5e1956SHiren Panchasara 	case IPVERSION:
427fc5e1956SHiren Panchasara 	{
428af9aa0a8SAndrey V. Elsukov 		uint16_t old;
429fc5e1956SHiren Panchasara 
430fc5e1956SHiren Panchasara 		if ((ip->ip_tos & IPTOS_ECN_MASK) == IPTOS_ECN_NOTECT)
431fc5e1956SHiren Panchasara 			return (0);	/* not-ECT */
432fc5e1956SHiren Panchasara 		if ((ip->ip_tos & IPTOS_ECN_MASK) == IPTOS_ECN_CE)
433fc5e1956SHiren Panchasara 			return (1);	/* already marked */
434fc5e1956SHiren Panchasara 
435fc5e1956SHiren Panchasara 		/*
436fc5e1956SHiren Panchasara 		 * ecn-capable but not marked,
437fc5e1956SHiren Panchasara 		 * mark CE and update checksum
438fc5e1956SHiren Panchasara 		 */
439af9aa0a8SAndrey V. Elsukov 		old = *(uint16_t *)ip;
440fc5e1956SHiren Panchasara 		ip->ip_tos |= IPTOS_ECN_CE;
441af9aa0a8SAndrey V. Elsukov 		ip->ip_sum = cksum_adjust(ip->ip_sum, old, *(uint16_t *)ip);
442fc5e1956SHiren Panchasara 		return (1);
443fc5e1956SHiren Panchasara 	}
444fc5e1956SHiren Panchasara #ifdef INET6
445fc5e1956SHiren Panchasara 	case (IPV6_VERSION >> 4):
446fc5e1956SHiren Panchasara 	{
4474001fcbeSDon Lewis 		struct ip6_hdr *ip6 = (struct ip6_hdr *)ip;
448fc5e1956SHiren Panchasara 		u_int32_t flowlabel;
449fc5e1956SHiren Panchasara 
450fc5e1956SHiren Panchasara 		flowlabel = ntohl(ip6->ip6_flow);
451fc5e1956SHiren Panchasara 		if ((flowlabel >> 28) != 6)
452fc5e1956SHiren Panchasara 			return (0);	/* version mismatch! */
453fc5e1956SHiren Panchasara 		if ((flowlabel & (IPTOS_ECN_MASK << 20)) ==
454fc5e1956SHiren Panchasara 		    (IPTOS_ECN_NOTECT << 20))
455fc5e1956SHiren Panchasara 			return (0);	/* not-ECT */
456fc5e1956SHiren Panchasara 		if ((flowlabel & (IPTOS_ECN_MASK << 20)) ==
457fc5e1956SHiren Panchasara 		    (IPTOS_ECN_CE << 20))
458fc5e1956SHiren Panchasara 			return (1);	/* already marked */
459fc5e1956SHiren Panchasara 		/*
460fc5e1956SHiren Panchasara 		 * ecn-capable but not marked, mark CE
461fc5e1956SHiren Panchasara 		 */
462fc5e1956SHiren Panchasara 		flowlabel |= (IPTOS_ECN_CE << 20);
463fc5e1956SHiren Panchasara 		ip6->ip6_flow = htonl(flowlabel);
464fc5e1956SHiren Panchasara 		return (1);
465fc5e1956SHiren Panchasara 	}
466fc5e1956SHiren Panchasara #endif
467fc5e1956SHiren Panchasara 	}
468fc5e1956SHiren Panchasara 	return (0);
469fc5e1956SHiren Panchasara }
470fc5e1956SHiren Panchasara 
471fc5e1956SHiren Panchasara /*
4723b3a8eb9SGleb Smirnoff  * Enqueue a packet in q, subject to space and queue management policy
4733b3a8eb9SGleb Smirnoff  * (whose parameters are in q->fs).
4743b3a8eb9SGleb Smirnoff  * Update stats for the queue and the scheduler.
4753b3a8eb9SGleb Smirnoff  * Return 0 on success, 1 on drop. The packet is consumed anyways.
4763b3a8eb9SGleb Smirnoff  */
4773b3a8eb9SGleb Smirnoff int
dn_enqueue(struct dn_queue * q,struct mbuf * m,int drop)4783b3a8eb9SGleb Smirnoff dn_enqueue(struct dn_queue *q, struct mbuf* m, int drop)
4793b3a8eb9SGleb Smirnoff {
4803b3a8eb9SGleb Smirnoff 	struct dn_fs *f;
4813b3a8eb9SGleb Smirnoff 	struct dn_flow *ni;	/* stats for scheduler instance */
4823b3a8eb9SGleb Smirnoff 	uint64_t len;
4833b3a8eb9SGleb Smirnoff 
4843b3a8eb9SGleb Smirnoff 	if (q->fs == NULL || q->_si == NULL) {
4853b3a8eb9SGleb Smirnoff 		printf("%s fs %p si %p, dropping\n",
4863b3a8eb9SGleb Smirnoff 			__FUNCTION__, q->fs, q->_si);
4873b3a8eb9SGleb Smirnoff 		FREE_PKT(m);
4883b3a8eb9SGleb Smirnoff 		return 1;
4893b3a8eb9SGleb Smirnoff 	}
4903b3a8eb9SGleb Smirnoff 	f = &(q->fs->fs);
4913b3a8eb9SGleb Smirnoff 	ni = &q->_si->ni;
4923b3a8eb9SGleb Smirnoff 	len = m->m_pkthdr.len;
4933b3a8eb9SGleb Smirnoff 	/* Update statistics, then check reasons to drop pkt. */
4943b3a8eb9SGleb Smirnoff 	q->ni.tot_bytes += len;
4953b3a8eb9SGleb Smirnoff 	q->ni.tot_pkts++;
4963b3a8eb9SGleb Smirnoff 	ni->tot_bytes += len;
4973b3a8eb9SGleb Smirnoff 	ni->tot_pkts++;
4983b3a8eb9SGleb Smirnoff 	if (drop)
4993b3a8eb9SGleb Smirnoff 		goto drop;
500*31cf66d7SRichard Scheffenegger 	if (f->plr[0] || f->plr[1]) {
501*31cf66d7SRichard Scheffenegger 		if (__predict_true(f->plr[1] == 0)) {
502*31cf66d7SRichard Scheffenegger 			if (random() < f->plr[0])
5033b3a8eb9SGleb Smirnoff 				goto drop;
504*31cf66d7SRichard Scheffenegger 		} else {
505*31cf66d7SRichard Scheffenegger 			switch (f->pl_state) {
506*31cf66d7SRichard Scheffenegger 			case PLR_STATE_B:
507*31cf66d7SRichard Scheffenegger 				if (random() < f->plr[3])
508*31cf66d7SRichard Scheffenegger 					f->pl_state = PLR_STATE_G;
509*31cf66d7SRichard Scheffenegger 				if (random() < f->plr[2])
510*31cf66d7SRichard Scheffenegger 					goto drop;
511*31cf66d7SRichard Scheffenegger 				break;
512*31cf66d7SRichard Scheffenegger 			case PLR_STATE_G: /* FALLTHROUGH */
513*31cf66d7SRichard Scheffenegger 			default:
514*31cf66d7SRichard Scheffenegger 				if (random() < f->plr[1])
515*31cf66d7SRichard Scheffenegger 					f->pl_state = PLR_STATE_B;
516*31cf66d7SRichard Scheffenegger 				if (random() < f->plr[0])
517*31cf66d7SRichard Scheffenegger 					goto drop;
518*31cf66d7SRichard Scheffenegger 				break;
519*31cf66d7SRichard Scheffenegger 			}
520*31cf66d7SRichard Scheffenegger 		}
521*31cf66d7SRichard Scheffenegger 	}
5228ef7beb2SGleb Smirnoff 	if (m->m_pkthdr.rcvif != NULL)
5238ef7beb2SGleb Smirnoff 		m_rcvif_serialize(m);
52491336b40SDon Lewis #ifdef NEW_AQM
52591336b40SDon Lewis 	/* Call AQM enqueue function */
52691336b40SDon Lewis 	if (q->fs->aqmfp)
52791336b40SDon Lewis 		return q->fs->aqmfp->enqueue(q ,m);
52891336b40SDon Lewis #endif
529fc5e1956SHiren Panchasara 	if (f->flags & DN_IS_RED && red_drops(q, m->m_pkthdr.len)) {
530fc5e1956SHiren Panchasara 		if (!(f->flags & DN_IS_ECN) || !ecn_mark(m))
5313b3a8eb9SGleb Smirnoff 			goto drop;
532fc5e1956SHiren Panchasara 	}
5333b3a8eb9SGleb Smirnoff 	if (f->flags & DN_QSIZE_BYTES) {
5343b3a8eb9SGleb Smirnoff 		if (q->ni.len_bytes > f->qsize)
5353b3a8eb9SGleb Smirnoff 			goto drop;
5363b3a8eb9SGleb Smirnoff 	} else if (q->ni.length >= f->qsize) {
5373b3a8eb9SGleb Smirnoff 		goto drop;
5383b3a8eb9SGleb Smirnoff 	}
5393b3a8eb9SGleb Smirnoff 	mq_append(&q->mq, m);
5403b3a8eb9SGleb Smirnoff 	q->ni.length++;
5413b3a8eb9SGleb Smirnoff 	q->ni.len_bytes += len;
5423b3a8eb9SGleb Smirnoff 	ni->length++;
5433b3a8eb9SGleb Smirnoff 	ni->len_bytes += len;
544fc5e1956SHiren Panchasara 	return (0);
5453b3a8eb9SGleb Smirnoff 
5463b3a8eb9SGleb Smirnoff drop:
547fe3bcfbdSTom Jones 	V_dn_cfg.io_pkt_drop++;
5483b3a8eb9SGleb Smirnoff 	q->ni.drops++;
5493b3a8eb9SGleb Smirnoff 	ni->drops++;
5503b3a8eb9SGleb Smirnoff 	FREE_PKT(m);
551fc5e1956SHiren Panchasara 	return (1);
5523b3a8eb9SGleb Smirnoff }
5533b3a8eb9SGleb Smirnoff 
5543b3a8eb9SGleb Smirnoff /*
5553b3a8eb9SGleb Smirnoff  * Fetch packets from the delay line which are due now. If there are
5563b3a8eb9SGleb Smirnoff  * leftover packets, reinsert the delay line in the heap.
5573b3a8eb9SGleb Smirnoff  * Runs under scheduler lock.
5583b3a8eb9SGleb Smirnoff  */
5593b3a8eb9SGleb Smirnoff static void
transmit_event(struct mq * q,struct delay_line * dline,uint64_t now)5603b3a8eb9SGleb Smirnoff transmit_event(struct mq *q, struct delay_line *dline, uint64_t now)
5613b3a8eb9SGleb Smirnoff {
5623b3a8eb9SGleb Smirnoff 	struct mbuf *m;
5633b3a8eb9SGleb Smirnoff 	struct dn_pkt_tag *pkt = NULL;
5643b3a8eb9SGleb Smirnoff 
5653b3a8eb9SGleb Smirnoff 	dline->oid.subtype = 0; /* not in heap */
5663b3a8eb9SGleb Smirnoff 	while ((m = dline->mq.head) != NULL) {
5673b3a8eb9SGleb Smirnoff 		pkt = dn_tag_get(m);
5683b3a8eb9SGleb Smirnoff 		if (!DN_KEY_LEQ(pkt->output_time, now))
5693b3a8eb9SGleb Smirnoff 			break;
5703b3a8eb9SGleb Smirnoff 		dline->mq.head = m->m_nextpkt;
57141d10f90SLuigi Rizzo 		dline->mq.count--;
5728ef7beb2SGleb Smirnoff 		if (m->m_pkthdr.rcvif != NULL &&
5738ef7beb2SGleb Smirnoff 		  __predict_false(m_rcvif_restore(m) == NULL))
5748ef7beb2SGleb Smirnoff 			m_freem(m);
5758ef7beb2SGleb Smirnoff 		else
5763b3a8eb9SGleb Smirnoff 			mq_append(q, m);
5773b3a8eb9SGleb Smirnoff 	}
5783b3a8eb9SGleb Smirnoff 	if (m != NULL) {
5793b3a8eb9SGleb Smirnoff 		dline->oid.subtype = 1; /* in heap */
580fe3bcfbdSTom Jones 		heap_insert(&V_dn_cfg.evheap, pkt->output_time, dline);
5813b3a8eb9SGleb Smirnoff 	}
5823b3a8eb9SGleb Smirnoff }
5833b3a8eb9SGleb Smirnoff 
5843b3a8eb9SGleb Smirnoff /*
5853b3a8eb9SGleb Smirnoff  * Convert the additional MAC overheads/delays into an equivalent
5863b3a8eb9SGleb Smirnoff  * number of bits for the given data rate. The samples are
5873b3a8eb9SGleb Smirnoff  * in milliseconds so we need to divide by 1000.
5883b3a8eb9SGleb Smirnoff  */
5893b3a8eb9SGleb Smirnoff static uint64_t
extra_bits(struct mbuf * m,struct dn_schk * s)5903b3a8eb9SGleb Smirnoff extra_bits(struct mbuf *m, struct dn_schk *s)
5913b3a8eb9SGleb Smirnoff {
5923b3a8eb9SGleb Smirnoff 	int index;
5933b3a8eb9SGleb Smirnoff 	uint64_t bits;
5943b3a8eb9SGleb Smirnoff 	struct dn_profile *pf = s->profile;
5953b3a8eb9SGleb Smirnoff 
5963b3a8eb9SGleb Smirnoff 	if (!pf || pf->samples_no == 0)
5973b3a8eb9SGleb Smirnoff 		return 0;
5983b3a8eb9SGleb Smirnoff 	index  = random() % pf->samples_no;
5993b3a8eb9SGleb Smirnoff 	bits = div64((uint64_t)pf->samples[index] * s->link.bandwidth, 1000);
6003b3a8eb9SGleb Smirnoff 	if (index >= pf->loss_level) {
6013b3a8eb9SGleb Smirnoff 		struct dn_pkt_tag *dt = dn_tag_get(m);
6023b3a8eb9SGleb Smirnoff 		if (dt)
6033b3a8eb9SGleb Smirnoff 			dt->dn_dir = DIR_DROP;
6043b3a8eb9SGleb Smirnoff 	}
6053b3a8eb9SGleb Smirnoff 	return bits;
6063b3a8eb9SGleb Smirnoff }
6073b3a8eb9SGleb Smirnoff 
6083b3a8eb9SGleb Smirnoff /*
6093b3a8eb9SGleb Smirnoff  * Send traffic from a scheduler instance due by 'now'.
6103b3a8eb9SGleb Smirnoff  * Return a pointer to the head of the queue.
6113b3a8eb9SGleb Smirnoff  */
6123b3a8eb9SGleb Smirnoff static struct mbuf *
serve_sched(struct mq * q,struct dn_sch_inst * si,uint64_t now)6133b3a8eb9SGleb Smirnoff serve_sched(struct mq *q, struct dn_sch_inst *si, uint64_t now)
6143b3a8eb9SGleb Smirnoff {
6153b3a8eb9SGleb Smirnoff 	struct mq def_q;
6163b3a8eb9SGleb Smirnoff 	struct dn_schk *s = si->sched;
6173b3a8eb9SGleb Smirnoff 	struct mbuf *m = NULL;
6183b3a8eb9SGleb Smirnoff 	int delay_line_idle = (si->dline.mq.head == NULL);
61920ffd88eSLuiz Otavio O Souza 	int done;
62020ffd88eSLuiz Otavio O Souza 	uint32_t bw;
6213b3a8eb9SGleb Smirnoff 
6223b3a8eb9SGleb Smirnoff 	if (q == NULL) {
6233b3a8eb9SGleb Smirnoff 		q = &def_q;
6243b3a8eb9SGleb Smirnoff 		q->head = NULL;
6253b3a8eb9SGleb Smirnoff 	}
6263b3a8eb9SGleb Smirnoff 
6273b3a8eb9SGleb Smirnoff 	bw = s->link.bandwidth;
6283b3a8eb9SGleb Smirnoff 	si->kflags &= ~DN_ACTIVE;
6293b3a8eb9SGleb Smirnoff 
6303b3a8eb9SGleb Smirnoff 	if (bw > 0)
6313b3a8eb9SGleb Smirnoff 		si->credit += (now - si->sched_time) * bw;
6323b3a8eb9SGleb Smirnoff 	else
6333b3a8eb9SGleb Smirnoff 		si->credit = 0;
6343b3a8eb9SGleb Smirnoff 	si->sched_time = now;
6353b3a8eb9SGleb Smirnoff 	done = 0;
6363b3a8eb9SGleb Smirnoff 	while (si->credit >= 0 && (m = s->fp->dequeue(si)) != NULL) {
6373b3a8eb9SGleb Smirnoff 		uint64_t len_scaled;
6383b3a8eb9SGleb Smirnoff 
6393b3a8eb9SGleb Smirnoff 		done++;
6403b3a8eb9SGleb Smirnoff 		len_scaled = (bw == 0) ? 0 : hz *
6413b3a8eb9SGleb Smirnoff 			(m->m_pkthdr.len * 8 + extra_bits(m, s));
6423b3a8eb9SGleb Smirnoff 		si->credit -= len_scaled;
6433b3a8eb9SGleb Smirnoff 		/* Move packet in the delay line */
644fe3bcfbdSTom Jones 		dn_tag_get(m)->output_time = V_dn_cfg.curr_time + s->link.delay ;
6458ef7beb2SGleb Smirnoff 		if (m->m_pkthdr.rcvif != NULL)
6468ef7beb2SGleb Smirnoff 			m_rcvif_serialize(m);
6473b3a8eb9SGleb Smirnoff 		mq_append(&si->dline.mq, m);
6483b3a8eb9SGleb Smirnoff 	}
6493b3a8eb9SGleb Smirnoff 
6503b3a8eb9SGleb Smirnoff 	/*
6513b3a8eb9SGleb Smirnoff 	 * If credit >= 0 the instance is idle, mark time.
6523b3a8eb9SGleb Smirnoff 	 * Otherwise put back in the heap, and adjust the output
6533b3a8eb9SGleb Smirnoff 	 * time of the last inserted packet, m, which was too early.
6543b3a8eb9SGleb Smirnoff 	 */
6553b3a8eb9SGleb Smirnoff 	if (si->credit >= 0) {
6563b3a8eb9SGleb Smirnoff 		si->idle_time = now;
6573b3a8eb9SGleb Smirnoff 	} else {
6583b3a8eb9SGleb Smirnoff 		uint64_t t;
6593b3a8eb9SGleb Smirnoff 		KASSERT (bw > 0, ("bw=0 and credit<0 ?"));
6603b3a8eb9SGleb Smirnoff 		t = div64(bw - 1 - si->credit, bw);
6613b3a8eb9SGleb Smirnoff 		if (m)
6623b3a8eb9SGleb Smirnoff 			dn_tag_get(m)->output_time += t;
6633b3a8eb9SGleb Smirnoff 		si->kflags |= DN_ACTIVE;
664fe3bcfbdSTom Jones 		heap_insert(&V_dn_cfg.evheap, now + t, si);
6653b3a8eb9SGleb Smirnoff 	}
6663b3a8eb9SGleb Smirnoff 	if (delay_line_idle && done)
6673b3a8eb9SGleb Smirnoff 		transmit_event(q, &si->dline, now);
6683b3a8eb9SGleb Smirnoff 	return q->head;
6693b3a8eb9SGleb Smirnoff }
6703b3a8eb9SGleb Smirnoff 
6713b3a8eb9SGleb Smirnoff /*
6723b3a8eb9SGleb Smirnoff  * The timer handler for dummynet. Time is computed in ticks, but
6733b3a8eb9SGleb Smirnoff  * but the code is tolerant to the actual rate at which this is called.
6743b3a8eb9SGleb Smirnoff  * Once complete, the function reschedules itself for the next tick.
6753b3a8eb9SGleb Smirnoff  */
6763b3a8eb9SGleb Smirnoff void
dummynet_task(void * context,int pending)6773b3a8eb9SGleb Smirnoff dummynet_task(void *context, int pending)
6783b3a8eb9SGleb Smirnoff {
6793b3a8eb9SGleb Smirnoff 	struct timeval t;
6803b3a8eb9SGleb Smirnoff 	struct mq q = { NULL, NULL }; /* queue to accumulate results */
681fe3bcfbdSTom Jones 	struct epoch_tracker et;
6823b3a8eb9SGleb Smirnoff 
683fe3bcfbdSTom Jones 	VNET_ITERATOR_DECL(vnet_iter);
684fe3bcfbdSTom Jones 	VNET_LIST_RLOCK();
685fe3bcfbdSTom Jones 	NET_EPOCH_ENTER(et);
686fe3bcfbdSTom Jones 
687fe3bcfbdSTom Jones 	VNET_FOREACH(vnet_iter) {
688fe3bcfbdSTom Jones 		memset(&q, 0, sizeof(struct mq));
689fe3bcfbdSTom Jones 		CURVNET_SET(vnet_iter);
6903b3a8eb9SGleb Smirnoff 
691cbb019b8SKristof Provost 		if (! V_dn_cfg.init_done) {
692cbb019b8SKristof Provost 			CURVNET_RESTORE();
693cbb019b8SKristof Provost 			continue;
694cbb019b8SKristof Provost 		}
695cbb019b8SKristof Provost 
6963b3a8eb9SGleb Smirnoff 		DN_BH_WLOCK();
6973b3a8eb9SGleb Smirnoff 
6983b3a8eb9SGleb Smirnoff 		/* Update number of lost(coalesced) ticks. */
699fe3bcfbdSTom Jones 		V_dn_cfg.tick_lost += pending - 1;
7003b3a8eb9SGleb Smirnoff 
7013b3a8eb9SGleb Smirnoff 		getmicrouptime(&t);
7023b3a8eb9SGleb Smirnoff 		/* Last tick duration (usec). */
703fe3bcfbdSTom Jones 		V_dn_cfg.tick_last = (t.tv_sec - V_dn_cfg.prev_t.tv_sec) * 1000000 +
704fe3bcfbdSTom Jones 		(t.tv_usec - V_dn_cfg.prev_t.tv_usec);
7053b3a8eb9SGleb Smirnoff 		/* Last tick vs standard tick difference (usec). */
706fe3bcfbdSTom Jones 		V_dn_cfg.tick_delta = (V_dn_cfg.tick_last * hz - 1000000) / hz;
7073b3a8eb9SGleb Smirnoff 		/* Accumulated tick difference (usec). */
708fe3bcfbdSTom Jones 		V_dn_cfg.tick_delta_sum += V_dn_cfg.tick_delta;
7093b3a8eb9SGleb Smirnoff 
710fe3bcfbdSTom Jones 		V_dn_cfg.prev_t = t;
7113b3a8eb9SGleb Smirnoff 
7123b3a8eb9SGleb Smirnoff 		/*
7133b3a8eb9SGleb Smirnoff 		* Adjust curr_time if the accumulated tick difference is
7143b3a8eb9SGleb Smirnoff 		* greater than the 'standard' tick. Since curr_time should
7153b3a8eb9SGleb Smirnoff 		* be monotonically increasing, we do positive adjustments
7163b3a8eb9SGleb Smirnoff 		* as required, and throttle curr_time in case of negative
7173b3a8eb9SGleb Smirnoff 		* adjustment.
7183b3a8eb9SGleb Smirnoff 		*/
719fe3bcfbdSTom Jones 		V_dn_cfg.curr_time++;
720fe3bcfbdSTom Jones 		if (V_dn_cfg.tick_delta_sum - tick >= 0) {
721fe3bcfbdSTom Jones 			int diff = V_dn_cfg.tick_delta_sum / tick;
7223b3a8eb9SGleb Smirnoff 
723fe3bcfbdSTom Jones 			V_dn_cfg.curr_time += diff;
724fe3bcfbdSTom Jones 			V_dn_cfg.tick_diff += diff;
725fe3bcfbdSTom Jones 			V_dn_cfg.tick_delta_sum %= tick;
726fe3bcfbdSTom Jones 			V_dn_cfg.tick_adjustment++;
727fe3bcfbdSTom Jones 		} else if (V_dn_cfg.tick_delta_sum + tick <= 0) {
728fe3bcfbdSTom Jones 			V_dn_cfg.curr_time--;
729fe3bcfbdSTom Jones 			V_dn_cfg.tick_diff--;
730fe3bcfbdSTom Jones 			V_dn_cfg.tick_delta_sum += tick;
731fe3bcfbdSTom Jones 			V_dn_cfg.tick_adjustment++;
7323b3a8eb9SGleb Smirnoff 		}
7333b3a8eb9SGleb Smirnoff 
7343b3a8eb9SGleb Smirnoff 		/* serve pending events, accumulate in q */
7353b3a8eb9SGleb Smirnoff 		for (;;) {
7363b3a8eb9SGleb Smirnoff 			struct dn_id *p;    /* generic parameter to handler */
7373b3a8eb9SGleb Smirnoff 
738fe3bcfbdSTom Jones 			if (V_dn_cfg.evheap.elements == 0 ||
739fe3bcfbdSTom Jones 			    DN_KEY_LT(V_dn_cfg.curr_time, HEAP_TOP(&V_dn_cfg.evheap)->key))
7403b3a8eb9SGleb Smirnoff 				break;
741fe3bcfbdSTom Jones 			p = HEAP_TOP(&V_dn_cfg.evheap)->object;
742fe3bcfbdSTom Jones 			heap_extract(&V_dn_cfg.evheap, NULL);
7433b3a8eb9SGleb Smirnoff 			if (p->type == DN_SCH_I) {
744fe3bcfbdSTom Jones 				serve_sched(&q, (struct dn_sch_inst *)p, V_dn_cfg.curr_time);
7453b3a8eb9SGleb Smirnoff 			} else { /* extracted a delay line */
746fe3bcfbdSTom Jones 				transmit_event(&q, (struct delay_line *)p, V_dn_cfg.curr_time);
7473b3a8eb9SGleb Smirnoff 			}
7483b3a8eb9SGleb Smirnoff 		}
749fe3bcfbdSTom Jones 		if (V_dn_cfg.expire && ++V_dn_cfg.expire_cycle >= V_dn_cfg.expire) {
750fe3bcfbdSTom Jones 			V_dn_cfg.expire_cycle = 0;
7513b3a8eb9SGleb Smirnoff 			dn_drain_scheduler();
7523b3a8eb9SGleb Smirnoff 			dn_drain_queue();
7533b3a8eb9SGleb Smirnoff 		}
754c8cfbc06SHans Petter Selasky 		DN_BH_WUNLOCK();
7553b3a8eb9SGleb Smirnoff 		if (q.head != NULL)
7563b3a8eb9SGleb Smirnoff 			dummynet_send(q.head);
757fe3bcfbdSTom Jones 
7583b3a8eb9SGleb Smirnoff 		CURVNET_RESTORE();
7593b3a8eb9SGleb Smirnoff 	}
760fe3bcfbdSTom Jones 	NET_EPOCH_EXIT(et);
761fe3bcfbdSTom Jones 	VNET_LIST_RUNLOCK();
762fe3bcfbdSTom Jones 
763fe3bcfbdSTom Jones 	/* Schedule our next run. */
764fe3bcfbdSTom Jones 	dn_reschedule();
765fe3bcfbdSTom Jones }
7663b3a8eb9SGleb Smirnoff 
7673b3a8eb9SGleb Smirnoff /*
7683b3a8eb9SGleb Smirnoff  * forward a chain of packets to the proper destination.
7693b3a8eb9SGleb Smirnoff  * This runs outside the dummynet lock.
7703b3a8eb9SGleb Smirnoff  */
7713b3a8eb9SGleb Smirnoff static void
dummynet_send(struct mbuf * m)7723b3a8eb9SGleb Smirnoff dummynet_send(struct mbuf *m)
7733b3a8eb9SGleb Smirnoff {
7743b3a8eb9SGleb Smirnoff 	struct mbuf *n;
7753b3a8eb9SGleb Smirnoff 
776b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
777b8a6e03fSGleb Smirnoff 
7783b3a8eb9SGleb Smirnoff 	for (; m != NULL; m = n) {
7793b3a8eb9SGleb Smirnoff 		struct ifnet *ifp = NULL;	/* gcc 3.4.6 complains */
7803b3a8eb9SGleb Smirnoff         	struct m_tag *tag;
7813b3a8eb9SGleb Smirnoff 		int dst;
7823b3a8eb9SGleb Smirnoff 
7833b3a8eb9SGleb Smirnoff 		n = m->m_nextpkt;
7843b3a8eb9SGleb Smirnoff 		m->m_nextpkt = NULL;
7853b3a8eb9SGleb Smirnoff 		tag = m_tag_first(m);
7863b3a8eb9SGleb Smirnoff 		if (tag == NULL) { /* should not happen */
7873b3a8eb9SGleb Smirnoff 			dst = DIR_DROP;
7883b3a8eb9SGleb Smirnoff 		} else {
7893b3a8eb9SGleb Smirnoff 			struct dn_pkt_tag *pkt = dn_tag_get(m);
7903b3a8eb9SGleb Smirnoff 			/* extract the dummynet info, rename the tag
7913b3a8eb9SGleb Smirnoff 			 * to carry reinject info.
7923b3a8eb9SGleb Smirnoff 			 */
79312c542cdSKristof Provost 			ifp = ifnet_byindexgen(pkt->if_index, pkt->if_idxgen);
79416899291SKristof Provost 			if (((pkt->dn_dir == (DIR_OUT | PROTO_LAYER2)) ||
79516899291SKristof Provost 			    (pkt->dn_dir == (DIR_OUT | PROTO_LAYER2 | PROTO_IPV6))) &&
79612c542cdSKristof Provost 				ifp == NULL) {
797488c0a7cSSean Bruno 				dst = DIR_DROP;
798488c0a7cSSean Bruno 			} else {
7993b3a8eb9SGleb Smirnoff 				dst = pkt->dn_dir;
8003b3a8eb9SGleb Smirnoff 				tag->m_tag_cookie = MTAG_IPFW_RULE;
8013b3a8eb9SGleb Smirnoff 				tag->m_tag_id = 0;
8023b3a8eb9SGleb Smirnoff 			}
803488c0a7cSSean Bruno 		}
8043b3a8eb9SGleb Smirnoff 
8053b3a8eb9SGleb Smirnoff 		switch (dst) {
8063b3a8eb9SGleb Smirnoff 		case DIR_OUT:
8073b3a8eb9SGleb Smirnoff 			ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
8083b3a8eb9SGleb Smirnoff 			break ;
8093b3a8eb9SGleb Smirnoff 
8103b3a8eb9SGleb Smirnoff 		case DIR_IN :
8113b3a8eb9SGleb Smirnoff 			netisr_dispatch(NETISR_IP, m);
8123b3a8eb9SGleb Smirnoff 			break;
8133b3a8eb9SGleb Smirnoff 
8143b3a8eb9SGleb Smirnoff #ifdef INET6
8153b3a8eb9SGleb Smirnoff 		case DIR_IN | PROTO_IPV6:
8163b3a8eb9SGleb Smirnoff 			netisr_dispatch(NETISR_IPV6, m);
8173b3a8eb9SGleb Smirnoff 			break;
8183b3a8eb9SGleb Smirnoff 
8193b3a8eb9SGleb Smirnoff 		case DIR_OUT | PROTO_IPV6:
8203b3a8eb9SGleb Smirnoff 			ip6_output(m, NULL, NULL, IPV6_FORWARDING, NULL, NULL, NULL);
8213b3a8eb9SGleb Smirnoff 			break;
8223b3a8eb9SGleb Smirnoff #endif
8233b3a8eb9SGleb Smirnoff 
8243b3a8eb9SGleb Smirnoff 		case DIR_FWD | PROTO_IFB: /* DN_TO_IFB_FWD: */
8253b3a8eb9SGleb Smirnoff 			if (bridge_dn_p != NULL)
8263b3a8eb9SGleb Smirnoff 				((*bridge_dn_p)(m, ifp));
8273b3a8eb9SGleb Smirnoff 			else
8283b3a8eb9SGleb Smirnoff 				printf("dummynet: if_bridge not loaded\n");
8293b3a8eb9SGleb Smirnoff 
8303b3a8eb9SGleb Smirnoff 			break;
8313b3a8eb9SGleb Smirnoff 
832c21cbacaSKristof Provost 		case DIR_IN | PROTO_LAYER2 | PROTO_IPV6:
8333b3a8eb9SGleb Smirnoff 		case DIR_IN | PROTO_LAYER2: /* DN_TO_ETH_DEMUX: */
8343b3a8eb9SGleb Smirnoff 			/*
8353b3a8eb9SGleb Smirnoff 			 * The Ethernet code assumes the Ethernet header is
8363b3a8eb9SGleb Smirnoff 			 * contiguous in the first mbuf header.
8373b3a8eb9SGleb Smirnoff 			 * Insure this is true.
8383b3a8eb9SGleb Smirnoff 			 */
8393b3a8eb9SGleb Smirnoff 			if (m->m_len < ETHER_HDR_LEN &&
8403b3a8eb9SGleb Smirnoff 			    (m = m_pullup(m, ETHER_HDR_LEN)) == NULL) {
8413b3a8eb9SGleb Smirnoff 				printf("dummynet/ether: pullup failed, "
8423b3a8eb9SGleb Smirnoff 				    "dropping packet\n");
8433b3a8eb9SGleb Smirnoff 				break;
8443b3a8eb9SGleb Smirnoff 			}
8453b3a8eb9SGleb Smirnoff 			ether_demux(m->m_pkthdr.rcvif, m);
8463b3a8eb9SGleb Smirnoff 			break;
8473b3a8eb9SGleb Smirnoff 
848c21cbacaSKristof Provost 		case DIR_OUT | PROTO_LAYER2 | PROTO_IPV6:
849a0376d4dSLuiz Otavio O Souza 		case DIR_OUT | PROTO_LAYER2: /* DN_TO_ETH_OUT: */
85016899291SKristof Provost 			MPASS(ifp != NULL);
8513b3a8eb9SGleb Smirnoff 			ether_output_frame(ifp, m);
8523b3a8eb9SGleb Smirnoff 			break;
8533b3a8eb9SGleb Smirnoff 
8543b3a8eb9SGleb Smirnoff 		case DIR_DROP:
8553b3a8eb9SGleb Smirnoff 			/* drop the packet after some time */
8563b3a8eb9SGleb Smirnoff 			FREE_PKT(m);
8573b3a8eb9SGleb Smirnoff 			break;
8583b3a8eb9SGleb Smirnoff 
8593b3a8eb9SGleb Smirnoff 		default:
8603b3a8eb9SGleb Smirnoff 			printf("dummynet: bad switch %d!\n", dst);
8613b3a8eb9SGleb Smirnoff 			FREE_PKT(m);
8623b3a8eb9SGleb Smirnoff 			break;
8633b3a8eb9SGleb Smirnoff 		}
8643b3a8eb9SGleb Smirnoff 	}
8653b3a8eb9SGleb Smirnoff }
8663b3a8eb9SGleb Smirnoff 
8673b3a8eb9SGleb Smirnoff static inline int
tag_mbuf(struct mbuf * m,int dir,struct ip_fw_args * fwa)8683b3a8eb9SGleb Smirnoff tag_mbuf(struct mbuf *m, int dir, struct ip_fw_args *fwa)
8693b3a8eb9SGleb Smirnoff {
8703b3a8eb9SGleb Smirnoff 	struct dn_pkt_tag *dt;
8713b3a8eb9SGleb Smirnoff 	struct m_tag *mtag;
8723b3a8eb9SGleb Smirnoff 
8733b3a8eb9SGleb Smirnoff 	mtag = m_tag_get(PACKET_TAG_DUMMYNET,
8743b3a8eb9SGleb Smirnoff 		    sizeof(*dt), M_NOWAIT | M_ZERO);
8753b3a8eb9SGleb Smirnoff 	if (mtag == NULL)
8763b3a8eb9SGleb Smirnoff 		return 1;		/* Cannot allocate packet header. */
8773b3a8eb9SGleb Smirnoff 	m_tag_prepend(m, mtag);		/* Attach to mbuf chain. */
8783b3a8eb9SGleb Smirnoff 	dt = (struct dn_pkt_tag *)(mtag + 1);
8793b3a8eb9SGleb Smirnoff 	dt->rule = fwa->rule;
88063b3c1c7SKristof Provost 	/* only keep this info */
88163b3c1c7SKristof Provost 	dt->rule.info &= (IPFW_ONEPASS | IPFW_IS_DUMMYNET);
8823b3a8eb9SGleb Smirnoff 	dt->dn_dir = dir;
88312c542cdSKristof Provost 	if (fwa->flags & IPFW_ARGS_OUT && fwa->ifp != NULL) {
88412c542cdSKristof Provost 		NET_EPOCH_ASSERT();
88512c542cdSKristof Provost 		dt->if_index = fwa->ifp->if_index;
88612c542cdSKristof Provost 		dt->if_idxgen = fwa->ifp->if_idxgen;
88712c542cdSKristof Provost 	}
88875556db5SIgor Ostapenko 	/* dt->output_time is updated as we move through */
889fe3bcfbdSTom Jones 	dt->output_time = V_dn_cfg.curr_time;
8904001fcbeSDon Lewis 	dt->iphdr_off = (dir & PROTO_LAYER2) ? ETHER_HDR_LEN : 0;
8913b3a8eb9SGleb Smirnoff 	return 0;
8923b3a8eb9SGleb Smirnoff }
8933b3a8eb9SGleb Smirnoff 
8943b3a8eb9SGleb Smirnoff /*
8953b3a8eb9SGleb Smirnoff  * dummynet hook for packets.
8963b3a8eb9SGleb Smirnoff  * We use the argument to locate the flowset fs and the sched_set sch
8973b3a8eb9SGleb Smirnoff  * associated to it. The we apply flow_mask and sched_mask to
8983b3a8eb9SGleb Smirnoff  * determine the queue and scheduler instances.
8993b3a8eb9SGleb Smirnoff  */
9003b3a8eb9SGleb Smirnoff int
dummynet_io(struct mbuf ** m0,struct ip_fw_args * fwa)901dc0fa4f7SGleb Smirnoff dummynet_io(struct mbuf **m0, struct ip_fw_args *fwa)
9023b3a8eb9SGleb Smirnoff {
9033b3a8eb9SGleb Smirnoff 	struct mbuf *m = *m0;
9043b3a8eb9SGleb Smirnoff 	struct dn_fsk *fs = NULL;
9053b3a8eb9SGleb Smirnoff 	struct dn_sch_inst *si;
9063b3a8eb9SGleb Smirnoff 	struct dn_queue *q = NULL;	/* default */
907dc0fa4f7SGleb Smirnoff 	int fs_id, dir;
9083b3a8eb9SGleb Smirnoff 
909dc0fa4f7SGleb Smirnoff 	fs_id = (fwa->rule.info & IPFW_INFO_MASK) +
9103b3a8eb9SGleb Smirnoff 		((fwa->rule.info & IPFW_IS_PIPE) ? 2*DN_MAX_ID : 0);
911dc0fa4f7SGleb Smirnoff 	/* XXXGL: convert args to dir */
912dc0fa4f7SGleb Smirnoff 	if (fwa->flags & IPFW_ARGS_IN)
913dc0fa4f7SGleb Smirnoff 		dir = DIR_IN;
914dc0fa4f7SGleb Smirnoff 	else
915dc0fa4f7SGleb Smirnoff 		dir = DIR_OUT;
916dc0fa4f7SGleb Smirnoff 	if (fwa->flags & IPFW_ARGS_ETHER)
917dc0fa4f7SGleb Smirnoff 		dir |= PROTO_LAYER2;
918dc0fa4f7SGleb Smirnoff 	else if (fwa->flags & IPFW_ARGS_IP6)
919dc0fa4f7SGleb Smirnoff 		dir |= PROTO_IPV6;
9203b3a8eb9SGleb Smirnoff 	DN_BH_WLOCK();
921fe3bcfbdSTom Jones 	V_dn_cfg.io_pkt++;
9223b3a8eb9SGleb Smirnoff 	/* we could actually tag outside the lock, but who cares... */
9233b3a8eb9SGleb Smirnoff 	if (tag_mbuf(m, dir, fwa))
9243b3a8eb9SGleb Smirnoff 		goto dropit;
9253b3a8eb9SGleb Smirnoff 	/* XXX locate_flowset could be optimised with a direct ref. */
926fe3bcfbdSTom Jones 	fs = dn_ht_find(V_dn_cfg.fshash, fs_id, 0, NULL);
9273b3a8eb9SGleb Smirnoff 	if (fs == NULL)
9283b3a8eb9SGleb Smirnoff 		goto dropit;	/* This queue/pipe does not exist! */
9293b3a8eb9SGleb Smirnoff 	if (fs->sched == NULL)	/* should not happen */
9303b3a8eb9SGleb Smirnoff 		goto dropit;
9313b3a8eb9SGleb Smirnoff 	/* find scheduler instance, possibly applying sched_mask */
9323b3a8eb9SGleb Smirnoff 	si = ipdn_si_find(fs->sched, &(fwa->f_id));
9333b3a8eb9SGleb Smirnoff 	if (si == NULL)
9343b3a8eb9SGleb Smirnoff 		goto dropit;
9353b3a8eb9SGleb Smirnoff 	/*
9363b3a8eb9SGleb Smirnoff 	 * If the scheduler supports multiple queues, find the right one
9373b3a8eb9SGleb Smirnoff 	 * (otherwise it will be ignored by enqueue).
9383b3a8eb9SGleb Smirnoff 	 */
9393b3a8eb9SGleb Smirnoff 	if (fs->sched->fp->flags & DN_MULTIQUEUE) {
9403b3a8eb9SGleb Smirnoff 		q = ipdn_q_find(fs, si, &(fwa->f_id));
9413b3a8eb9SGleb Smirnoff 		if (q == NULL)
9423b3a8eb9SGleb Smirnoff 			goto dropit;
9433b3a8eb9SGleb Smirnoff 	}
9443b3a8eb9SGleb Smirnoff 	if (fs->sched->fp->enqueue(si, q, m)) {
9453b3a8eb9SGleb Smirnoff 		/* packet was dropped by enqueue() */
9463b3a8eb9SGleb Smirnoff 		m = *m0 = NULL;
94791336b40SDon Lewis 
94891336b40SDon Lewis 		/* dn_enqueue already increases io_pkt_drop */
949fe3bcfbdSTom Jones 		V_dn_cfg.io_pkt_drop--;
95091336b40SDon Lewis 
9513b3a8eb9SGleb Smirnoff 		goto dropit;
9523b3a8eb9SGleb Smirnoff 	}
9533b3a8eb9SGleb Smirnoff 
9543b3a8eb9SGleb Smirnoff 	if (si->kflags & DN_ACTIVE) {
9553b3a8eb9SGleb Smirnoff 		m = *m0 = NULL; /* consumed */
9563b3a8eb9SGleb Smirnoff 		goto done; /* already active, nothing to do */
9573b3a8eb9SGleb Smirnoff 	}
9583b3a8eb9SGleb Smirnoff 
9593b3a8eb9SGleb Smirnoff 	/* compute the initial allowance */
960fe3bcfbdSTom Jones 	if (si->idle_time < V_dn_cfg.curr_time) {
9613b3a8eb9SGleb Smirnoff 	    /* Do this only on the first packet on an idle pipe */
9623b3a8eb9SGleb Smirnoff 	    struct dn_link *p = &fs->sched->link;
9633b3a8eb9SGleb Smirnoff 
964fe3bcfbdSTom Jones 	    si->sched_time = V_dn_cfg.curr_time;
965fe3bcfbdSTom Jones 	    si->credit = V_dn_cfg.io_fast ? p->bandwidth : 0;
9663b3a8eb9SGleb Smirnoff 	    if (p->burst) {
967fe3bcfbdSTom Jones 		uint64_t burst = (V_dn_cfg.curr_time - si->idle_time) * p->bandwidth;
9683b3a8eb9SGleb Smirnoff 		if (burst > p->burst)
9693b3a8eb9SGleb Smirnoff 			burst = p->burst;
9703b3a8eb9SGleb Smirnoff 		si->credit += burst;
9713b3a8eb9SGleb Smirnoff 	    }
9723b3a8eb9SGleb Smirnoff 	}
9733b3a8eb9SGleb Smirnoff 	/* pass through scheduler and delay line */
974fe3bcfbdSTom Jones 	m = serve_sched(NULL, si, V_dn_cfg.curr_time);
9753b3a8eb9SGleb Smirnoff 
9763b3a8eb9SGleb Smirnoff 	/* optimization -- pass it back to ipfw for immediate send */
9773b3a8eb9SGleb Smirnoff 	/* XXX Don't call dummynet_send() if scheduler return the packet
9783b3a8eb9SGleb Smirnoff 	 *     just enqueued. This avoid a lock order reversal.
9793b3a8eb9SGleb Smirnoff 	 *
9803b3a8eb9SGleb Smirnoff 	 */
981fe3bcfbdSTom Jones 	if (/*V_dn_cfg.io_fast &&*/ m == *m0 && (dir & PROTO_LAYER2) == 0 ) {
9823b3a8eb9SGleb Smirnoff 		/* fast io, rename the tag * to carry reinject info. */
9833b3a8eb9SGleb Smirnoff 		struct m_tag *tag = m_tag_first(m);
9843b3a8eb9SGleb Smirnoff 
9853b3a8eb9SGleb Smirnoff 		tag->m_tag_cookie = MTAG_IPFW_RULE;
9863b3a8eb9SGleb Smirnoff 		tag->m_tag_id = 0;
987fe3bcfbdSTom Jones 		V_dn_cfg.io_pkt_fast++;
9883b3a8eb9SGleb Smirnoff 		if (m->m_nextpkt != NULL) {
9893b3a8eb9SGleb Smirnoff 			printf("dummynet: fast io: pkt chain detected!\n");
9903b3a8eb9SGleb Smirnoff 			m->m_nextpkt = NULL;
9913b3a8eb9SGleb Smirnoff 		}
9923b3a8eb9SGleb Smirnoff 		m = NULL;
9933b3a8eb9SGleb Smirnoff 	} else {
9943b3a8eb9SGleb Smirnoff 		*m0 = NULL;
9953b3a8eb9SGleb Smirnoff 	}
9963b3a8eb9SGleb Smirnoff done:
9973b3a8eb9SGleb Smirnoff 	DN_BH_WUNLOCK();
9983b3a8eb9SGleb Smirnoff 	if (m)
9993b3a8eb9SGleb Smirnoff 		dummynet_send(m);
10003b3a8eb9SGleb Smirnoff 	return 0;
10013b3a8eb9SGleb Smirnoff 
10023b3a8eb9SGleb Smirnoff dropit:
1003fe3bcfbdSTom Jones 	V_dn_cfg.io_pkt_drop++;
10043b3a8eb9SGleb Smirnoff 	DN_BH_WUNLOCK();
10053b3a8eb9SGleb Smirnoff 	if (m)
10063b3a8eb9SGleb Smirnoff 		FREE_PKT(m);
10073b3a8eb9SGleb Smirnoff 	*m0 = NULL;
10083b3a8eb9SGleb Smirnoff 	return (fs && (fs->fs.flags & DN_NOERROR)) ? 0 : ENOBUFS;
10093b3a8eb9SGleb Smirnoff }
1010