2 * Copyright (c) 2010 Luigi Rizzo, Riccardo Panicucci, Universita` di Pisa
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Dummynet portions related to packet handling.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "opt_inet6.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
39 #include <sys/kernel.h>
41 #include <sys/module.h>
44 #include <sys/rwlock.h>
45 #include <sys/socket.h>
47 #include <sys/sysctl.h>
49 #include <net/if.h> /* IFNAMSIZ, struct ifaddr, ifq head, lock.h mutex.h */
50 #include <net/netisr.h>
53 #include <netinet/in.h>
54 #include <netinet/ip.h> /* ip_len, ip_off */
55 #include <netinet/ip_var.h> /* ip_output(), IP_FORWARDING */
56 #include <netinet/ip_fw.h>
57 #include <netinet/ipfw/ip_fw_private.h>
58 #include <netinet/ipfw/dn_heap.h>
59 #include <netinet/ip_dummynet.h>
60 #include <netinet/ipfw/ip_dn_private.h>
61 #include <netinet/ipfw/dn_sched.h>
63 #include <netinet/if_ether.h> /* various ether_* routines */
65 #include <netinet/ip6.h> /* for ip6_input, ip6_output prototypes */
66 #include <netinet6/ip6_var.h>
69 * We keep a private variable for the simulation time, but we could
70 * probably use an existing one ("softticks" in sys/kern/kern_timeout.c)
71 * instead of dn_cfg.curr_time
74 struct dn_parms dn_cfg;
75 //VNET_DEFINE(struct dn_parms, _base_dn_cfg);
77 static long tick_last; /* Last tick duration (usec). */
78 static long tick_delta; /* Last vs standard tick diff (usec). */
79 static long tick_delta_sum; /* Accumulated tick difference (usec).*/
80 static long tick_adjustment; /* Tick adjustments done. */
81 static long tick_lost; /* Lost(coalesced) ticks number. */
82 /* Adjusted vs non-adjusted curr_time difference (ticks). */
83 static long tick_diff;
85 static unsigned long io_pkt;
86 static unsigned long io_pkt_fast;
87 static unsigned long io_pkt_drop;
90 * We use a heap to store entities for which we have pending timer events.
91 * The heap is checked at every tick and all entities with expired events
95 MALLOC_DEFINE(M_DUMMYNET, "dummynet", "dummynet heap");
97 extern void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
103 SYSCTL_DECL(_net_inet);
104 SYSCTL_DECL(_net_inet_ip);
105 static SYSCTL_NODE(_net_inet_ip, OID_AUTO, dummynet, CTLFLAG_RW, 0, "Dummynet");
107 /* wrapper to pass dn_cfg fields to SYSCTL_* */
108 //#define DC(x) (&(VNET_NAME(_base_dn_cfg).x))
109 #define DC(x) (&(dn_cfg.x))
113 sysctl_hash_size(SYSCTL_HANDLER_ARGS)
117 value = dn_cfg.hash_size;
118 error = sysctl_handle_int(oidp, &value, 0, req);
119 if (error != 0 || req->newptr == NULL)
121 if (value < 16 || value > 65536)
123 dn_cfg.hash_size = value;
127 SYSCTL_PROC(_net_inet_ip_dummynet, OID_AUTO, hash_size,
128 CTLTYPE_INT | CTLFLAG_RW, 0, 0, sysctl_hash_size,
129 "I", "Default hash table size");
132 sysctl_limits(SYSCTL_HANDLER_ARGS)
138 value = dn_cfg.slot_limit;
140 value = dn_cfg.byte_limit;
141 error = sysctl_handle_long(oidp, &value, 0, req);
143 if (error != 0 || req->newptr == NULL)
148 dn_cfg.slot_limit = value;
152 dn_cfg.byte_limit = value;
157 SYSCTL_PROC(_net_inet_ip_dummynet, OID_AUTO, pipe_slot_limit,
158 CTLTYPE_LONG | CTLFLAG_RW, 0, 1, sysctl_limits,
159 "L", "Upper limit in slots for pipe queue.");
160 SYSCTL_PROC(_net_inet_ip_dummynet, OID_AUTO, pipe_byte_limit,
161 CTLTYPE_LONG | CTLFLAG_RW, 0, 0, sysctl_limits,
162 "L", "Upper limit in bytes for pipe queue.");
163 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, io_fast,
164 CTLFLAG_RW, DC(io_fast), 0, "Enable fast dummynet io.");
165 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, debug,
166 CTLFLAG_RW, DC(debug), 0, "Dummynet debug level");
169 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, red_lookup_depth,
170 CTLFLAG_RD, DC(red_lookup_depth), 0, "Depth of RED lookup table");
171 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, red_avg_pkt_size,
172 CTLFLAG_RD, DC(red_avg_pkt_size), 0, "RED Medium packet size");
173 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, red_max_pkt_size,
174 CTLFLAG_RD, DC(red_max_pkt_size), 0, "RED Max packet size");
176 /* time adjustment */
177 SYSCTL_LONG(_net_inet_ip_dummynet, OID_AUTO, tick_delta,
178 CTLFLAG_RD, &tick_delta, 0, "Last vs standard tick difference (usec).");
179 SYSCTL_LONG(_net_inet_ip_dummynet, OID_AUTO, tick_delta_sum,
180 CTLFLAG_RD, &tick_delta_sum, 0, "Accumulated tick difference (usec).");
181 SYSCTL_LONG(_net_inet_ip_dummynet, OID_AUTO, tick_adjustment,
182 CTLFLAG_RD, &tick_adjustment, 0, "Tick adjustments done.");
183 SYSCTL_LONG(_net_inet_ip_dummynet, OID_AUTO, tick_diff,
184 CTLFLAG_RD, &tick_diff, 0,
185 "Adjusted vs non-adjusted curr_time difference (ticks).");
186 SYSCTL_LONG(_net_inet_ip_dummynet, OID_AUTO, tick_lost,
187 CTLFLAG_RD, &tick_lost, 0,
188 "Number of ticks coalesced by dummynet taskqueue.");
190 /* Drain parameters */
191 SYSCTL_UINT(_net_inet_ip_dummynet, OID_AUTO, expire,
192 CTLFLAG_RW, DC(expire), 0, "Expire empty queues/pipes");
193 SYSCTL_UINT(_net_inet_ip_dummynet, OID_AUTO, expire_cycle,
194 CTLFLAG_RD, DC(expire_cycle), 0, "Expire cycle for queues/pipes");
197 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, schk_count,
198 CTLFLAG_RD, DC(schk_count), 0, "Number of schedulers");
199 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, si_count,
200 CTLFLAG_RD, DC(si_count), 0, "Number of scheduler instances");
201 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, fsk_count,
202 CTLFLAG_RD, DC(fsk_count), 0, "Number of flowsets");
203 SYSCTL_INT(_net_inet_ip_dummynet, OID_AUTO, queue_count,
204 CTLFLAG_RD, DC(queue_count), 0, "Number of queues");
205 SYSCTL_ULONG(_net_inet_ip_dummynet, OID_AUTO, io_pkt,
206 CTLFLAG_RD, &io_pkt, 0,
207 "Number of packets passed to dummynet.");
208 SYSCTL_ULONG(_net_inet_ip_dummynet, OID_AUTO, io_pkt_fast,
209 CTLFLAG_RD, &io_pkt_fast, 0,
210 "Number of packets bypassed dummynet scheduler.");
211 SYSCTL_ULONG(_net_inet_ip_dummynet, OID_AUTO, io_pkt_drop,
212 CTLFLAG_RD, &io_pkt_drop, 0,
213 "Number of packets dropped by dummynet.");
219 static void dummynet_send(struct mbuf *);
222 * Packets processed by dummynet have an mbuf tag associated with
223 * them that carries their dummynet state.
224 * Outside dummynet, only the 'rule' field is relevant, and it must
225 * be at the beginning of the structure.
228 struct ipfw_rule_ref rule; /* matching rule */
230 /* second part, dummynet specific */
231 int dn_dir; /* action when packet comes out.*/
232 /* see ip_fw_private.h */
233 uint64_t output_time; /* when the pkt is due for delivery*/
234 struct ifnet *ifp; /* interface, for ip_output */
235 struct _ip6dn_args ip6opt; /* XXX ipv6 options */
239 * Return the mbuf tag holding the dummynet state (it should
240 * be the first one on the list).
242 static struct dn_pkt_tag *
243 dn_tag_get(struct mbuf *m)
245 struct m_tag *mtag = m_tag_first(m);
246 KASSERT(mtag != NULL &&
247 mtag->m_tag_cookie == MTAG_ABI_COMPAT &&
248 mtag->m_tag_id == PACKET_TAG_DUMMYNET,
249 ("packet on dummynet queue w/o dummynet tag!"));
250 return (struct dn_pkt_tag *)(mtag+1);
254 mq_append(struct mq *q, struct mbuf *m)
259 q->tail->m_nextpkt = m;
265 * Dispose a list of packet. Use a functions so if we need to do
266 * more work, this is a central point to do it.
268 void dn_free_pkts(struct mbuf *mnext)
272 while ((m = mnext) != NULL) {
273 mnext = m->m_nextpkt;
279 red_drops (struct dn_queue *q, int len)
284 * RED calculates the average queue size (avg) using a low-pass filter
285 * with an exponential weighted (w_q) moving average:
286 * avg <- (1-w_q) * avg + w_q * q_size
287 * where q_size is the queue length (measured in bytes or * packets).
289 * If q_size == 0, we compute the idle time for the link, and set
290 * avg = (1 - w_q)^(idle/s)
291 * where s is the time needed for transmitting a medium-sized packet.
293 * Now, if avg < min_th the packet is enqueued.
294 * If avg > max_th the packet is dropped. Otherwise, the packet is
295 * dropped with probability P function of avg.
298 struct dn_fsk *fs = q->fs;
301 /* Queue in bytes or packets? */
302 uint32_t q_size = (fs->fs.flags & DN_QSIZE_BYTES) ?
303 q->ni.len_bytes : q->ni.length;
305 /* Average queue size estimation. */
307 /* Queue is not empty, avg <- avg + (q_size - avg) * w_q */
308 int diff = SCALE(q_size) - q->avg;
309 int64_t v = SCALE_MUL((int64_t)diff, (int64_t)fs->w_q);
314 * Queue is empty, find for how long the queue has been
315 * empty and use a lookup table for computing
316 * (1 - * w_q)^(idle_time/s) where s is the time to send a
321 u_int t = div64((dn_cfg.curr_time - q->q_time), fs->lookup_step);
323 q->avg = (t < fs->lookup_depth) ?
324 SCALE_MUL(q->avg, fs->w_q_lookup[t]) : 0;
329 if (q->avg < fs->min_th) {
331 return (0); /* accept packet */
333 if (q->avg >= fs->max_th) { /* average queue >= max threshold */
334 if (fs->fs.flags & DN_IS_GENTLE_RED) {
336 * According to Gentle-RED, if avg is greater than
337 * max_th the packet is dropped with a probability
338 * p_b = c_3 * avg - c_4
339 * where c_3 = (1 - max_p) / max_th
340 * c_4 = 1 - 2 * max_p
342 p_b = SCALE_MUL((int64_t)fs->c_3, (int64_t)q->avg) -
348 } else if (q->avg > fs->min_th) {
350 * We compute p_b using the linear dropping function
351 * p_b = c_1 * avg - c_2
352 * where c_1 = max_p / (max_th - min_th)
353 * c_2 = max_p * min_th / (max_th - min_th)
355 p_b = SCALE_MUL((int64_t)fs->c_1, (int64_t)q->avg) - fs->c_2;
358 if (fs->fs.flags & DN_QSIZE_BYTES)
359 p_b = div64((p_b * len) , fs->max_pkt_size);
361 q->random = random() & 0xffff;
364 * q->count counts packets arrived since last drop, so a greater
365 * value of q->count means a greater packet drop probability.
367 if (SCALE_MUL(p_b, SCALE((int64_t)q->count)) > q->random) {
369 /* After a drop we calculate a new random value. */
370 q->random = random() & 0xffff;
371 return (1); /* drop */
374 /* End of RED algorithm. */
376 return (0); /* accept */
381 * Enqueue a packet in q, subject to space and queue management policy
382 * (whose parameters are in q->fs).
383 * Update stats for the queue and the scheduler.
384 * Return 0 on success, 1 on drop. The packet is consumed anyways.
387 dn_enqueue(struct dn_queue *q, struct mbuf* m, int drop)
390 struct dn_flow *ni; /* stats for scheduler instance */
393 if (q->fs == NULL || q->_si == NULL) {
394 printf("%s fs %p si %p, dropping\n",
395 __FUNCTION__, q->fs, q->_si);
401 len = m->m_pkthdr.len;
402 /* Update statistics, then check reasons to drop pkt. */
403 q->ni.tot_bytes += len;
405 ni->tot_bytes += len;
409 if (f->plr && random() < f->plr)
411 if (f->flags & DN_IS_RED && red_drops(q, m->m_pkthdr.len))
413 if (f->flags & DN_QSIZE_BYTES) {
414 if (q->ni.len_bytes > f->qsize)
416 } else if (q->ni.length >= f->qsize) {
419 mq_append(&q->mq, m);
421 q->ni.len_bytes += len;
423 ni->len_bytes += len;
435 * Fetch packets from the delay line which are due now. If there are
436 * leftover packets, reinsert the delay line in the heap.
437 * Runs under scheduler lock.
440 transmit_event(struct mq *q, struct delay_line *dline, uint64_t now)
443 struct dn_pkt_tag *pkt = NULL;
445 dline->oid.subtype = 0; /* not in heap */
446 while ((m = dline->mq.head) != NULL) {
448 if (!DN_KEY_LEQ(pkt->output_time, now))
450 dline->mq.head = m->m_nextpkt;
454 dline->oid.subtype = 1; /* in heap */
455 heap_insert(&dn_cfg.evheap, pkt->output_time, dline);
460 * Convert the additional MAC overheads/delays into an equivalent
461 * number of bits for the given data rate. The samples are
462 * in milliseconds so we need to divide by 1000.
465 extra_bits(struct mbuf *m, struct dn_schk *s)
469 struct dn_profile *pf = s->profile;
471 if (!pf || pf->samples_no == 0)
473 index = random() % pf->samples_no;
474 bits = div64((uint64_t)pf->samples[index] * s->link.bandwidth, 1000);
475 if (index >= pf->loss_level) {
476 struct dn_pkt_tag *dt = dn_tag_get(m);
478 dt->dn_dir = DIR_DROP;
484 * Send traffic from a scheduler instance due by 'now'.
485 * Return a pointer to the head of the queue.
488 serve_sched(struct mq *q, struct dn_sch_inst *si, uint64_t now)
491 struct dn_schk *s = si->sched;
492 struct mbuf *m = NULL;
493 int delay_line_idle = (si->dline.mq.head == NULL);
501 bw = s->link.bandwidth;
502 si->kflags &= ~DN_ACTIVE;
505 si->credit += (now - si->sched_time) * bw;
508 si->sched_time = now;
510 while (si->credit >= 0 && (m = s->fp->dequeue(si)) != NULL) {
514 len_scaled = (bw == 0) ? 0 : hz *
515 (m->m_pkthdr.len * 8 + extra_bits(m, s));
516 si->credit -= len_scaled;
517 /* Move packet in the delay line */
518 dn_tag_get(m)->output_time = dn_cfg.curr_time + s->link.delay ;
519 mq_append(&si->dline.mq, m);
523 * If credit >= 0 the instance is idle, mark time.
524 * Otherwise put back in the heap, and adjust the output
525 * time of the last inserted packet, m, which was too early.
527 if (si->credit >= 0) {
531 KASSERT (bw > 0, ("bw=0 and credit<0 ?"));
532 t = div64(bw - 1 - si->credit, bw);
534 dn_tag_get(m)->output_time += t;
535 si->kflags |= DN_ACTIVE;
536 heap_insert(&dn_cfg.evheap, now + t, si);
538 if (delay_line_idle && done)
539 transmit_event(q, &si->dline, now);
544 * The timer handler for dummynet. Time is computed in ticks, but
545 * but the code is tolerant to the actual rate at which this is called.
546 * Once complete, the function reschedules itself for the next tick.
549 dummynet_task(void *context, int pending)
552 struct mq q = { NULL, NULL }; /* queue to accumulate results */
554 CURVNET_SET((struct vnet *)context);
558 /* Update number of lost(coalesced) ticks. */
559 tick_lost += pending - 1;
562 /* Last tick duration (usec). */
563 tick_last = (t.tv_sec - dn_cfg.prev_t.tv_sec) * 1000000 +
564 (t.tv_usec - dn_cfg.prev_t.tv_usec);
565 /* Last tick vs standard tick difference (usec). */
566 tick_delta = (tick_last * hz - 1000000) / hz;
567 /* Accumulated tick difference (usec). */
568 tick_delta_sum += tick_delta;
573 * Adjust curr_time if the accumulated tick difference is
574 * greater than the 'standard' tick. Since curr_time should
575 * be monotonically increasing, we do positive adjustments
576 * as required, and throttle curr_time in case of negative
580 if (tick_delta_sum - tick >= 0) {
581 int diff = tick_delta_sum / tick;
583 dn_cfg.curr_time += diff;
585 tick_delta_sum %= tick;
587 } else if (tick_delta_sum + tick <= 0) {
590 tick_delta_sum += tick;
594 /* serve pending events, accumulate in q */
596 struct dn_id *p; /* generic parameter to handler */
598 if (dn_cfg.evheap.elements == 0 ||
599 DN_KEY_LT(dn_cfg.curr_time, HEAP_TOP(&dn_cfg.evheap)->key))
601 p = HEAP_TOP(&dn_cfg.evheap)->object;
602 heap_extract(&dn_cfg.evheap, NULL);
604 if (p->type == DN_SCH_I) {
605 serve_sched(&q, (struct dn_sch_inst *)p, dn_cfg.curr_time);
606 } else { /* extracted a delay line */
607 transmit_event(&q, (struct delay_line *)p, dn_cfg.curr_time);
610 if (dn_cfg.expire && ++dn_cfg.expire_cycle >= dn_cfg.expire) {
611 dn_cfg.expire_cycle = 0;
612 dn_drain_scheduler();
619 dummynet_send(q.head);
624 * forward a chain of packets to the proper destination.
625 * This runs outside the dummynet lock.
628 dummynet_send(struct mbuf *m)
632 for (; m != NULL; m = n) {
633 struct ifnet *ifp = NULL; /* gcc 3.4.6 complains */
639 tag = m_tag_first(m);
640 if (tag == NULL) { /* should not happen */
643 struct dn_pkt_tag *pkt = dn_tag_get(m);
644 /* extract the dummynet info, rename the tag
645 * to carry reinject info.
649 tag->m_tag_cookie = MTAG_IPFW_RULE;
655 SET_HOST_IPLEN(mtod(m, struct ip *));
656 ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
660 /* put header in network format for ip_input() */
661 //SET_NET_IPLEN(mtod(m, struct ip *));
662 netisr_dispatch(NETISR_IP, m);
666 case DIR_IN | PROTO_IPV6:
667 netisr_dispatch(NETISR_IPV6, m);
670 case DIR_OUT | PROTO_IPV6:
671 ip6_output(m, NULL, NULL, IPV6_FORWARDING, NULL, NULL, NULL);
675 case DIR_FWD | PROTO_IFB: /* DN_TO_IFB_FWD: */
676 if (bridge_dn_p != NULL)
677 ((*bridge_dn_p)(m, ifp));
679 printf("dummynet: if_bridge not loaded\n");
683 case DIR_IN | PROTO_LAYER2: /* DN_TO_ETH_DEMUX: */
685 * The Ethernet code assumes the Ethernet header is
686 * contiguous in the first mbuf header.
687 * Insure this is true.
689 if (m->m_len < ETHER_HDR_LEN &&
690 (m = m_pullup(m, ETHER_HDR_LEN)) == NULL) {
691 printf("dummynet/ether: pullup failed, "
692 "dropping packet\n");
695 ether_demux(m->m_pkthdr.rcvif, m);
698 case DIR_OUT | PROTO_LAYER2: /* N_TO_ETH_OUT: */
699 ether_output_frame(ifp, m);
703 /* drop the packet after some time */
708 printf("dummynet: bad switch %d!\n", dst);
716 tag_mbuf(struct mbuf *m, int dir, struct ip_fw_args *fwa)
718 struct dn_pkt_tag *dt;
721 mtag = m_tag_get(PACKET_TAG_DUMMYNET,
722 sizeof(*dt), M_NOWAIT | M_ZERO);
724 return 1; /* Cannot allocate packet header. */
725 m_tag_prepend(m, mtag); /* Attach to mbuf chain. */
726 dt = (struct dn_pkt_tag *)(mtag + 1);
727 dt->rule = fwa->rule;
728 dt->rule.info &= IPFW_ONEPASS; /* only keep this info */
731 /* dt->output tame is updated as we move through */
732 dt->output_time = dn_cfg.curr_time;
738 * dummynet hook for packets.
739 * We use the argument to locate the flowset fs and the sched_set sch
740 * associated to it. The we apply flow_mask and sched_mask to
741 * determine the queue and scheduler instances.
743 * dir where shall we send the packet after dummynet.
744 * *m0 the mbuf with the packet
745 * ifp the 'ifp' parameter from the caller.
746 * NULL in ip_input, destination interface in ip_output,
749 dummynet_io(struct mbuf **m0, int dir, struct ip_fw_args *fwa)
751 struct mbuf *m = *m0;
752 struct dn_fsk *fs = NULL;
753 struct dn_sch_inst *si;
754 struct dn_queue *q = NULL; /* default */
756 int fs_id = (fwa->rule.info & IPFW_INFO_MASK) +
757 ((fwa->rule.info & IPFW_IS_PIPE) ? 2*DN_MAX_ID : 0);
760 /* we could actually tag outside the lock, but who cares... */
761 if (tag_mbuf(m, dir, fwa))
764 /* if the upper half is busy doing something expensive,
765 * lets queue the packet and move forward
767 mq_append(&dn_cfg.pending, m);
768 m = *m0 = NULL; /* consumed */
769 goto done; /* already active, nothing to do */
771 /* XXX locate_flowset could be optimised with a direct ref. */
772 fs = dn_ht_find(dn_cfg.fshash, fs_id, 0, NULL);
774 goto dropit; /* This queue/pipe does not exist! */
775 if (fs->sched == NULL) /* should not happen */
777 /* find scheduler instance, possibly applying sched_mask */
778 si = ipdn_si_find(fs->sched, &(fwa->f_id));
782 * If the scheduler supports multiple queues, find the right one
783 * (otherwise it will be ignored by enqueue).
785 if (fs->sched->fp->flags & DN_MULTIQUEUE) {
786 q = ipdn_q_find(fs, si, &(fwa->f_id));
790 if (fs->sched->fp->enqueue(si, q, m)) {
791 /* packet was dropped by enqueue() */
796 if (si->kflags & DN_ACTIVE) {
797 m = *m0 = NULL; /* consumed */
798 goto done; /* already active, nothing to do */
801 /* compute the initial allowance */
802 if (si->idle_time < dn_cfg.curr_time) {
803 /* Do this only on the first packet on an idle pipe */
804 struct dn_link *p = &fs->sched->link;
806 si->sched_time = dn_cfg.curr_time;
807 si->credit = dn_cfg.io_fast ? p->bandwidth : 0;
809 uint64_t burst = (dn_cfg.curr_time - si->idle_time) * p->bandwidth;
810 if (burst > p->burst)
815 /* pass through scheduler and delay line */
816 m = serve_sched(NULL, si, dn_cfg.curr_time);
818 /* optimization -- pass it back to ipfw for immediate send */
819 /* XXX Don't call dummynet_send() if scheduler return the packet
820 * just enqueued. This avoid a lock order reversal.
823 if (/*dn_cfg.io_fast &&*/ m == *m0 && (dir & PROTO_LAYER2) == 0 ) {
824 /* fast io, rename the tag * to carry reinject info. */
825 struct m_tag *tag = m_tag_first(m);
827 tag->m_tag_cookie = MTAG_IPFW_RULE;
830 if (m->m_nextpkt != NULL) {
831 printf("dummynet: fast io: pkt chain detected!\n");
850 return (fs && (fs->fs.flags & DN_NOERROR)) ? 0 : ENOBUFS;