1 /* $KAME: altq_subr.c,v 1.23 2004/04/20 16:10:06 itojun Exp $ */
2 /* $DragonFly: src/sys/net/altq/altq_subr.c,v 1.10 2008/04/06 18:58:15 dillon Exp $ */
5 * Copyright (C) 1997-2003
6 * Sony Computer Science Laboratories Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include "opt_inet6.h"
34 #include <sys/param.h>
35 #include <sys/malloc.h>
37 #include <sys/systm.h>
39 #include <sys/socket.h>
40 #include <sys/socketvar.h>
41 #include <sys/kernel.h>
42 #include <sys/callout.h>
43 #include <sys/errno.h>
44 #include <sys/syslog.h>
45 #include <sys/sysctl.h>
46 #include <sys/queue.h>
47 #include <sys/thread2.h>
50 #include <net/if_dl.h>
51 #include <net/if_types.h>
52 #include <net/ifq_var.h>
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/ip.h>
58 #include <netinet/ip6.h>
60 #include <netinet/tcp.h>
61 #include <netinet/udp.h>
63 #include <net/pf/pfvar.h>
64 #include <net/altq/altq.h>
66 /* machine dependent clock related includes */
68 #include <machine/clock.h> /* for tsc_freq */
69 #include <machine/md_var.h> /* for cpu_feature */
70 #include <machine/specialreg.h> /* for CPUID_TSC */
74 * internal function prototypes
76 static void tbr_timeout(void *);
77 int (*altq_input)(struct mbuf *, int) = NULL;
78 static int tbr_timer = 0; /* token bucket regulator timer */
79 static struct callout tbr_callout;
81 int pfaltq_running; /* keep track of running state */
83 MALLOC_DEFINE(M_ALTQ, "altq", "ALTQ structures");
86 * alternate queueing support routines
89 /* look up the queue state by the interface name and the queueing type. */
91 altq_lookup(const char *name, int type)
95 if ((ifp = ifunit(name)) != NULL) {
96 if (type != ALTQT_NONE && ifp->if_snd.altq_type == type)
97 return (ifp->if_snd.altq_disc);
104 altq_attach(struct ifaltq *ifq, int type, void *discipline,
105 int (*enqueue)(struct ifaltq *, struct mbuf *, struct altq_pktattr *),
106 struct mbuf *(*dequeue)(struct ifaltq *, struct mbuf *, int),
107 int (*request)(struct ifaltq *, int, void *),
109 void *(*classify)(struct ifaltq *, struct mbuf *,
110 struct altq_pktattr *))
112 if (!ifq_is_ready(ifq))
115 ifq->altq_type = type;
116 ifq->altq_disc = discipline;
117 ifq->altq_enqueue = enqueue;
118 ifq->altq_dequeue = dequeue;
119 ifq->altq_request = request;
120 ifq->altq_clfier = clfier;
121 ifq->altq_classify = classify;
122 ifq->altq_flags &= (ALTQF_CANTCHANGE|ALTQF_ENABLED);
127 altq_detach(struct ifaltq *ifq)
129 if (!ifq_is_ready(ifq))
131 if (ifq_is_enabled(ifq))
133 if (!ifq_is_attached(ifq))
136 ifq_set_classic(ifq);
137 ifq->altq_type = ALTQT_NONE;
138 ifq->altq_disc = NULL;
139 ifq->altq_clfier = NULL;
140 ifq->altq_classify = NULL;
141 ifq->altq_flags &= ALTQF_CANTCHANGE;
146 altq_enable(struct ifaltq *ifq)
148 if (!ifq_is_ready(ifq))
150 if (ifq_is_enabled(ifq))
155 KKASSERT(ifq->ifq_len == 0);
156 ifq->altq_flags |= ALTQF_ENABLED;
157 if (ifq->altq_clfier != NULL)
158 ifq->altq_flags |= ALTQF_CLASSIFY;
165 altq_disable(struct ifaltq *ifq)
167 if (!ifq_is_enabled(ifq))
172 KKASSERT(ifq->ifq_len == 0);
173 ifq->altq_flags &= ~(ALTQF_ENABLED|ALTQF_CLASSIFY);
179 * internal representation of token bucket parameters
180 * rate: byte_per_unittime << 32
181 * (((bits_per_sec) / 8) << 32) / machclk_freq
186 #define TBR_SCALE(x) ((int64_t)(x) << TBR_SHIFT)
187 #define TBR_UNSCALE(x) ((x) >> TBR_SHIFT)
190 tbr_dequeue(struct ifaltq *ifq, struct mbuf *mpolled, int op)
192 struct tb_regulator *tbr;
199 if (op == ALTDQ_REMOVE && tbr->tbr_lastop == ALTDQ_POLL) {
200 /* if this is a remove after poll, bypass tbr check */
202 /* update token only when it is negative */
203 if (tbr->tbr_token <= 0) {
204 now = read_machclk();
205 interval = now - tbr->tbr_last;
206 if (interval >= tbr->tbr_filluptime)
207 tbr->tbr_token = tbr->tbr_depth;
209 tbr->tbr_token += interval * tbr->tbr_rate;
210 if (tbr->tbr_token > tbr->tbr_depth)
211 tbr->tbr_token = tbr->tbr_depth;
215 /* if token is still negative, don't allow dequeue */
216 if (tbr->tbr_token <= 0) {
222 if (ifq_is_enabled(ifq)) {
223 m = (*ifq->altq_dequeue)(ifq, mpolled, op);
224 } else if (op == ALTDQ_POLL) {
228 KKASSERT(mpolled == NULL || mpolled == m);
231 if (m != NULL && op == ALTDQ_REMOVE)
232 tbr->tbr_token -= TBR_SCALE(m_pktlen(m));
233 tbr->tbr_lastop = op;
239 * set a token bucket regulator.
240 * if the specified rate is zero, the token bucket regulator is deleted.
243 tbr_set(struct ifaltq *ifq, struct tb_profile *profile)
245 struct tb_regulator *tbr, *otbr;
247 if (machclk_freq == 0)
249 if (machclk_freq == 0) {
250 kprintf("tbr_set: no cpu clock available!\n");
254 if (profile->rate == 0) {
255 /* delete this tbr */
256 if ((tbr = ifq->altq_tbr) == NULL)
258 ifq->altq_tbr = NULL;
263 tbr = kmalloc(sizeof(*tbr), M_ALTQ, M_WAITOK | M_ZERO);
264 tbr->tbr_rate = TBR_SCALE(profile->rate / 8) / machclk_freq;
265 tbr->tbr_depth = TBR_SCALE(profile->depth);
266 if (tbr->tbr_rate > 0)
267 tbr->tbr_filluptime = tbr->tbr_depth / tbr->tbr_rate;
269 tbr->tbr_filluptime = 0xffffffffffffffffLL;
270 tbr->tbr_token = tbr->tbr_depth;
271 tbr->tbr_last = read_machclk();
272 tbr->tbr_lastop = ALTDQ_REMOVE;
274 otbr = ifq->altq_tbr;
275 ifq->altq_tbr = tbr; /* set the new tbr */
279 else if (tbr_timer == 0) {
280 callout_reset(&tbr_callout, 1, tbr_timeout, NULL);
287 * tbr_timeout goes through the interface list, and kicks the drivers
291 tbr_timeout(void *arg)
298 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
299 if (ifp->if_snd.altq_tbr == NULL)
302 if (!ifq_is_empty(&ifp->if_snd) && ifp->if_start != NULL) {
303 lwkt_serialize_enter(ifp->if_serializer);
304 (*ifp->if_start)(ifp);
305 lwkt_serialize_exit(ifp->if_serializer);
310 callout_reset(&tbr_callout, 1, tbr_timeout, NULL);
312 tbr_timer = 0; /* don't need tbr_timer anymore */
316 * get token bucket regulator profile
319 tbr_get(struct ifaltq *ifq, struct tb_profile *profile)
321 struct tb_regulator *tbr;
323 if ((tbr = ifq->altq_tbr) == NULL) {
328 (u_int)TBR_UNSCALE(tbr->tbr_rate * 8 * machclk_freq);
329 profile->depth = (u_int)TBR_UNSCALE(tbr->tbr_depth);
335 * attach a discipline to the interface. if one already exists, it is
339 altq_pfattach(struct pf_altq *a)
342 struct tb_profile tb;
345 switch (a->scheduler) {
350 error = cbq_pfattach(a);
355 error = priq_pfattach(a);
360 error = hfsc_pfattach(a);
365 error = fairq_pfattach(a);
372 ifp = ifunit(a->ifname);
374 /* if the state is running, enable altq */
375 if (error == 0 && pfaltq_running &&
376 ifp != NULL && ifp->if_snd.altq_type != ALTQT_NONE &&
377 !ifq_is_enabled(&ifp->if_snd))
378 error = altq_enable(&ifp->if_snd);
380 /* if altq is already enabled, reset set tokenbucket regulator */
381 if (error == 0 && ifp != NULL && ifq_is_enabled(&ifp->if_snd)) {
382 tb.rate = a->ifbandwidth;
383 tb.depth = a->tbrsize;
385 error = tbr_set(&ifp->if_snd, &tb);
393 * detach a discipline from the interface.
394 * it is possible that the discipline was already overridden by another
398 altq_pfdetach(struct pf_altq *a)
403 if ((ifp = ifunit(a->ifname)) == NULL)
406 /* if this discipline is no longer referenced, just return */
407 if (a->altq_disc == NULL || a->altq_disc != ifp->if_snd.altq_disc)
411 if (ifq_is_enabled(&ifp->if_snd))
412 error = altq_disable(&ifp->if_snd);
414 error = altq_detach(&ifp->if_snd);
421 * add a discipline or a queue
424 altq_add(struct pf_altq *a)
428 if (a->qname[0] != 0)
429 return (altq_add_queue(a));
431 if (machclk_freq == 0)
433 if (machclk_freq == 0)
434 panic("altq_add: no cpu clock");
436 switch (a->scheduler) {
439 error = cbq_add_altq(a);
444 error = priq_add_altq(a);
449 error = hfsc_add_altq(a);
454 error = fairq_add_altq(a);
465 * remove a discipline or a queue
468 altq_remove(struct pf_altq *a)
472 if (a->qname[0] != 0)
473 return (altq_remove_queue(a));
475 switch (a->scheduler) {
478 error = cbq_remove_altq(a);
483 error = priq_remove_altq(a);
488 error = hfsc_remove_altq(a);
493 error = fairq_remove_altq(a);
504 * add a queue to the discipline
507 altq_add_queue(struct pf_altq *a)
511 switch (a->scheduler) {
514 error = cbq_add_queue(a);
519 error = priq_add_queue(a);
524 error = hfsc_add_queue(a);
529 error = fairq_add_queue(a);
540 * remove a queue from the discipline
543 altq_remove_queue(struct pf_altq *a)
547 switch (a->scheduler) {
550 error = cbq_remove_queue(a);
555 error = priq_remove_queue(a);
560 error = hfsc_remove_queue(a);
565 error = fairq_remove_queue(a);
576 * get queue statistics
579 altq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
583 switch (a->scheduler) {
586 error = cbq_getqstats(a, ubuf, nbytes);
591 error = priq_getqstats(a, ubuf, nbytes);
596 error = hfsc_getqstats(a, ubuf, nbytes);
601 error = fairq_getqstats(a, ubuf, nbytes);
612 * read and write diffserv field in IPv4 or IPv6 header
615 read_dsfield(struct mbuf *m, struct altq_pktattr *pktattr)
618 uint8_t ds_field = 0;
620 if (pktattr == NULL ||
621 (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
624 /* verify that pattr_hdr is within the mbuf data */
625 for (m0 = m; m0 != NULL; m0 = m0->m_next) {
626 if ((pktattr->pattr_hdr >= m0->m_data) &&
627 (pktattr->pattr_hdr < m0->m_data + m0->m_len))
631 /* ick, pattr_hdr is stale */
632 pktattr->pattr_af = AF_UNSPEC;
634 kprintf("read_dsfield: can't locate header!\n");
639 if (pktattr->pattr_af == AF_INET) {
640 struct ip *ip = (struct ip *)pktattr->pattr_hdr;
643 return ((uint8_t)0); /* version mismatch! */
644 ds_field = ip->ip_tos;
647 else if (pktattr->pattr_af == AF_INET6) {
648 struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
651 flowlabel = ntohl(ip6->ip6_flow);
652 if ((flowlabel >> 28) != 6)
653 return ((uint8_t)0); /* version mismatch! */
654 ds_field = (flowlabel >> 20) & 0xff;
661 write_dsfield(struct mbuf *m, struct altq_pktattr *pktattr, uint8_t dsfield)
665 if (pktattr == NULL ||
666 (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
669 /* verify that pattr_hdr is within the mbuf data */
670 for (m0 = m; m0 != NULL; m0 = m0->m_next) {
671 if ((pktattr->pattr_hdr >= m0->m_data) &&
672 (pktattr->pattr_hdr < m0->m_data + m0->m_len))
676 /* ick, pattr_hdr is stale */
677 pktattr->pattr_af = AF_UNSPEC;
679 kprintf("write_dsfield: can't locate header!\n");
684 if (pktattr->pattr_af == AF_INET) {
685 struct ip *ip = (struct ip *)pktattr->pattr_hdr;
690 return; /* version mismatch! */
692 dsfield |= old & 3; /* leave CU bits */
695 ip->ip_tos = dsfield;
697 * update checksum (from RFC1624)
698 * HC' = ~(~HC + ~m + m')
700 sum = ~ntohs(ip->ip_sum) & 0xffff;
701 sum += 0xff00 + (~old & 0xff) + dsfield;
702 sum = (sum >> 16) + (sum & 0xffff);
703 sum += (sum >> 16); /* add carry */
705 ip->ip_sum = htons(~sum & 0xffff);
708 else if (pktattr->pattr_af == AF_INET6) {
709 struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
712 flowlabel = ntohl(ip6->ip6_flow);
713 if ((flowlabel >> 28) != 6)
714 return; /* version mismatch! */
715 flowlabel = (flowlabel & 0xf03fffff) | (dsfield << 20);
716 ip6->ip6_flow = htonl(flowlabel);
722 * high resolution clock support taking advantage of a machine dependent
723 * high resolution time counter (e.g., timestamp counter of intel pentium).
725 * - 64-bit-long monotonically-increasing counter
726 * - frequency range is 100M-4GHz (CPU speed)
728 /* if pcc is not available or disabled, emulate 256MHz using microtime() */
729 #define MACHCLK_SHIFT 8
732 uint32_t machclk_freq = 0;
733 uint32_t machclk_per_tick = 0;
738 callout_init(&tbr_callout);
742 #if !defined(__i386__) || defined(ALTQ_NOPCC)
744 #elif defined(__DragonFly__) && defined(SMP)
746 #elif defined(__i386__)
747 /* check if TSC is available */
748 if (machclk_usepcc == 1 && (cpu_feature & CPUID_TSC) == 0)
752 if (machclk_usepcc == 0) {
753 /* emulate 256MHz using microtime() */
754 machclk_freq = 1000000 << MACHCLK_SHIFT;
755 machclk_per_tick = machclk_freq / hz;
757 kprintf("altq: emulate %uHz cpu clock\n", machclk_freq);
763 * if the clock frequency (of Pentium TSC or Alpha PCC) is
764 * accessible, just use it.
767 machclk_freq = tsc_freq;
769 #error "machclk_freq interface not implemented"
773 * if we don't know the clock frequency, measure it.
775 if (machclk_freq == 0) {
777 struct timeval tv_start, tv_end;
778 uint64_t start, end, diff;
781 microtime(&tv_start);
782 start = read_machclk();
783 timo = hz; /* 1 sec */
784 tsleep(&wait, PCATCH, "init_machclk", timo);
786 end = read_machclk();
787 diff = (uint64_t)(tv_end.tv_sec - tv_start.tv_sec) * 1000000
788 + tv_end.tv_usec - tv_start.tv_usec;
790 machclk_freq = (u_int)((end - start) * 1000000 / diff);
793 machclk_per_tick = machclk_freq / hz;
796 kprintf("altq: CPU clock: %uHz\n", machclk_freq);
805 if (machclk_usepcc) {
806 #if defined(__i386__)
809 panic("read_machclk");
815 val = (((uint64_t)(tv.tv_sec - boottime.tv_sec) * 1000000
816 + tv.tv_usec) << MACHCLK_SHIFT);