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.12 2008/05/14 11:59:23 sephe 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 */
67 #include <machine/clock.h> /* for tsc_frequency */
68 #include <machine/md_var.h> /* for cpu_feature */
69 #include <machine/specialreg.h> /* for CPUID_TSC */
72 * internal function prototypes
74 static void tbr_timeout(void *);
75 static int altq_enable_locked(struct ifaltq *);
76 static int altq_disable_locked(struct ifaltq *);
77 static int altq_detach_locked(struct ifaltq *);
78 static int tbr_set_locked(struct ifaltq *, struct tb_profile *);
80 int (*altq_input)(struct mbuf *, int) = NULL;
81 static int tbr_timer = 0; /* token bucket regulator timer */
82 static struct callout tbr_callout;
84 int pfaltq_running; /* keep track of running state */
86 MALLOC_DEFINE(M_ALTQ, "altq", "ALTQ structures");
89 * alternate queueing support routines
92 /* look up the queue state by the interface name and the queueing type. */
94 altq_lookup(const char *name, int type)
98 if ((ifp = ifunit(name)) != NULL) {
99 if (type != ALTQT_NONE && ifp->if_snd.altq_type == type)
100 return (ifp->if_snd.altq_disc);
107 altq_attach(struct ifaltq *ifq, int type, void *discipline,
108 int (*enqueue)(struct ifaltq *, struct mbuf *, struct altq_pktattr *),
109 struct mbuf *(*dequeue)(struct ifaltq *, struct mbuf *, int),
110 int (*request)(struct ifaltq *, int, void *),
112 void *(*classify)(struct ifaltq *, struct mbuf *,
113 struct altq_pktattr *))
115 if (!ifq_is_ready(ifq))
118 ifq->altq_type = type;
119 ifq->altq_disc = discipline;
120 ifq->altq_enqueue = enqueue;
121 ifq->altq_dequeue = dequeue;
122 ifq->altq_request = request;
123 ifq->altq_clfier = clfier;
124 ifq->altq_classify = classify;
125 ifq->altq_flags &= (ALTQF_CANTCHANGE|ALTQF_ENABLED);
130 altq_detach_locked(struct ifaltq *ifq)
132 if (!ifq_is_ready(ifq))
134 if (ifq_is_enabled(ifq))
136 if (!ifq_is_attached(ifq))
139 ifq_set_classic(ifq);
140 ifq->altq_type = ALTQT_NONE;
141 ifq->altq_disc = NULL;
142 ifq->altq_clfier = NULL;
143 ifq->altq_classify = NULL;
144 ifq->altq_flags &= ALTQF_CANTCHANGE;
149 altq_detach(struct ifaltq *ifq)
154 error = altq_detach_locked(ifq);
160 altq_enable_locked(struct ifaltq *ifq)
162 if (!ifq_is_ready(ifq))
164 if (ifq_is_enabled(ifq))
167 ifq_purge_locked(ifq);
168 KKASSERT(ifq->ifq_len == 0);
170 ifq->altq_flags |= ALTQF_ENABLED;
171 if (ifq->altq_clfier != NULL)
172 ifq->altq_flags |= ALTQF_CLASSIFY;
177 altq_enable(struct ifaltq *ifq)
182 error = altq_enable_locked(ifq);
188 altq_disable_locked(struct ifaltq *ifq)
190 if (!ifq_is_enabled(ifq))
193 ifq_purge_locked(ifq);
194 KKASSERT(ifq->ifq_len == 0);
195 ifq->altq_flags &= ~(ALTQF_ENABLED|ALTQF_CLASSIFY);
200 altq_disable(struct ifaltq *ifq)
205 error = altq_disable_locked(ifq);
211 * internal representation of token bucket parameters
212 * rate: byte_per_unittime << 32
213 * (((bits_per_sec) / 8) << 32) / machclk_freq
218 #define TBR_SCALE(x) ((int64_t)(x) << TBR_SHIFT)
219 #define TBR_UNSCALE(x) ((x) >> TBR_SHIFT)
222 tbr_dequeue(struct ifaltq *ifq, struct mbuf *mpolled, int op)
224 struct tb_regulator *tbr;
231 if (op == ALTDQ_REMOVE && tbr->tbr_lastop == ALTDQ_POLL) {
232 /* if this is a remove after poll, bypass tbr check */
234 /* update token only when it is negative */
235 if (tbr->tbr_token <= 0) {
236 now = read_machclk();
237 interval = now - tbr->tbr_last;
238 if (interval >= tbr->tbr_filluptime)
239 tbr->tbr_token = tbr->tbr_depth;
241 tbr->tbr_token += interval * tbr->tbr_rate;
242 if (tbr->tbr_token > tbr->tbr_depth)
243 tbr->tbr_token = tbr->tbr_depth;
247 /* if token is still negative, don't allow dequeue */
248 if (tbr->tbr_token <= 0) {
254 if (ifq_is_enabled(ifq)) {
255 m = (*ifq->altq_dequeue)(ifq, mpolled, op);
256 } else if (op == ALTDQ_POLL) {
260 KKASSERT(mpolled == NULL || mpolled == m);
263 if (m != NULL && op == ALTDQ_REMOVE)
264 tbr->tbr_token -= TBR_SCALE(m_pktlen(m));
265 tbr->tbr_lastop = op;
271 * set a token bucket regulator.
272 * if the specified rate is zero, the token bucket regulator is deleted.
275 tbr_set_locked(struct ifaltq *ifq, struct tb_profile *profile)
277 struct tb_regulator *tbr, *otbr;
279 if (machclk_freq == 0)
281 if (machclk_freq == 0) {
282 kprintf("%s: no cpu clock available!\n", __func__);
286 if (profile->rate == 0) {
287 /* delete this tbr */
288 if ((tbr = ifq->altq_tbr) == NULL)
290 ifq->altq_tbr = NULL;
295 tbr = kmalloc(sizeof(*tbr), M_ALTQ, M_WAITOK | M_ZERO);
296 tbr->tbr_rate = TBR_SCALE(profile->rate / 8) / machclk_freq;
297 tbr->tbr_depth = TBR_SCALE(profile->depth);
298 if (tbr->tbr_rate > 0)
299 tbr->tbr_filluptime = tbr->tbr_depth / tbr->tbr_rate;
301 tbr->tbr_filluptime = 0xffffffffffffffffLL;
302 tbr->tbr_token = tbr->tbr_depth;
303 tbr->tbr_last = read_machclk();
304 tbr->tbr_lastop = ALTDQ_REMOVE;
306 otbr = ifq->altq_tbr;
307 ifq->altq_tbr = tbr; /* set the new tbr */
311 else if (tbr_timer == 0) {
312 callout_reset(&tbr_callout, 1, tbr_timeout, NULL);
319 tbr_set(struct ifaltq *ifq, struct tb_profile *profile)
324 error = tbr_set_locked(ifq, profile);
330 * tbr_timeout goes through the interface list, and kicks the drivers
334 tbr_timeout(void *arg)
341 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
342 if (ifp->if_snd.altq_tbr == NULL)
345 if (!ifq_is_empty(&ifp->if_snd) && ifp->if_start != NULL) {
346 ifnet_serialize_tx(ifp);
347 (*ifp->if_start)(ifp);
348 ifnet_deserialize_tx(ifp);
353 callout_reset(&tbr_callout, 1, tbr_timeout, NULL);
355 tbr_timer = 0; /* don't need tbr_timer anymore */
359 * get token bucket regulator profile
362 tbr_get(struct ifaltq *ifq, struct tb_profile *profile)
364 struct tb_regulator *tbr;
366 if ((tbr = ifq->altq_tbr) == NULL) {
371 (u_int)TBR_UNSCALE(tbr->tbr_rate * 8 * machclk_freq);
372 profile->depth = (u_int)TBR_UNSCALE(tbr->tbr_depth);
378 * attach a discipline to the interface. if one already exists, it is
382 altq_pfattach(struct pf_altq *a)
388 if (a->scheduler == ALTQT_NONE)
391 if (a->altq_disc == NULL)
394 ifp = ifunit(a->ifname);
401 switch (a->scheduler) {
404 error = cbq_pfattach(a, ifq);
409 error = priq_pfattach(a, ifq);
414 error = hfsc_pfattach(a, ifq);
419 error = fairq_pfattach(a, ifq);
427 /* if the state is running, enable altq */
428 if (error == 0 && pfaltq_running && ifq->altq_type != ALTQT_NONE &&
429 !ifq_is_enabled(ifq))
430 error = altq_enable_locked(ifq);
432 /* if altq is already enabled, reset set tokenbucket regulator */
433 if (error == 0 && ifq_is_enabled(ifq)) {
434 struct tb_profile tb;
436 tb.rate = a->ifbandwidth;
437 tb.depth = a->tbrsize;
438 error = tbr_set_locked(ifq, &tb);
446 * detach a discipline from the interface.
447 * it is possible that the discipline was already overridden by another
451 altq_pfdetach(struct pf_altq *a)
457 ifp = ifunit(a->ifname);
462 /* if this discipline is no longer referenced, just return */
463 if (a->altq_disc == NULL)
468 if (a->altq_disc != ifq->altq_disc)
471 if (ifq_is_enabled(ifq))
472 error = altq_disable_locked(ifq);
474 error = altq_detach_locked(ifq);
482 * add a discipline or a queue
485 altq_add(struct pf_altq *a)
489 if (a->qname[0] != 0)
490 return (altq_add_queue(a));
492 if (machclk_freq == 0)
494 if (machclk_freq == 0)
495 panic("altq_add: no cpu clock");
497 switch (a->scheduler) {
500 error = cbq_add_altq(a);
505 error = priq_add_altq(a);
510 error = hfsc_add_altq(a);
515 error = fairq_add_altq(a);
526 * remove a discipline or a queue
529 altq_remove(struct pf_altq *a)
533 if (a->qname[0] != 0)
534 return (altq_remove_queue(a));
536 switch (a->scheduler) {
539 error = cbq_remove_altq(a);
544 error = priq_remove_altq(a);
549 error = hfsc_remove_altq(a);
554 error = fairq_remove_altq(a);
565 * add a queue to the discipline
568 altq_add_queue(struct pf_altq *a)
572 switch (a->scheduler) {
575 error = cbq_add_queue(a);
580 error = priq_add_queue(a);
585 error = hfsc_add_queue(a);
590 error = fairq_add_queue(a);
601 * remove a queue from the discipline
604 altq_remove_queue(struct pf_altq *a)
608 switch (a->scheduler) {
611 error = cbq_remove_queue(a);
616 error = priq_remove_queue(a);
621 error = hfsc_remove_queue(a);
626 error = fairq_remove_queue(a);
637 * get queue statistics
640 altq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
644 switch (a->scheduler) {
647 error = cbq_getqstats(a, ubuf, nbytes);
652 error = priq_getqstats(a, ubuf, nbytes);
657 error = hfsc_getqstats(a, ubuf, nbytes);
662 error = fairq_getqstats(a, ubuf, nbytes);
673 * read and write diffserv field in IPv4 or IPv6 header
676 read_dsfield(struct mbuf *m, struct altq_pktattr *pktattr)
679 uint8_t ds_field = 0;
681 if (pktattr == NULL ||
682 (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
685 /* verify that pattr_hdr is within the mbuf data */
686 for (m0 = m; m0 != NULL; m0 = m0->m_next) {
687 if ((pktattr->pattr_hdr >= m0->m_data) &&
688 (pktattr->pattr_hdr < m0->m_data + m0->m_len))
692 /* ick, pattr_hdr is stale */
693 pktattr->pattr_af = AF_UNSPEC;
695 kprintf("read_dsfield: can't locate header!\n");
700 if (pktattr->pattr_af == AF_INET) {
701 struct ip *ip = (struct ip *)pktattr->pattr_hdr;
704 return ((uint8_t)0); /* version mismatch! */
705 ds_field = ip->ip_tos;
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 ((uint8_t)0); /* version mismatch! */
715 ds_field = (flowlabel >> 20) & 0xff;
722 write_dsfield(struct mbuf *m, struct altq_pktattr *pktattr, uint8_t dsfield)
726 if (pktattr == NULL ||
727 (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
730 /* verify that pattr_hdr is within the mbuf data */
731 for (m0 = m; m0 != NULL; m0 = m0->m_next) {
732 if ((pktattr->pattr_hdr >= m0->m_data) &&
733 (pktattr->pattr_hdr < m0->m_data + m0->m_len))
737 /* ick, pattr_hdr is stale */
738 pktattr->pattr_af = AF_UNSPEC;
740 kprintf("write_dsfield: can't locate header!\n");
745 if (pktattr->pattr_af == AF_INET) {
746 struct ip *ip = (struct ip *)pktattr->pattr_hdr;
751 return; /* version mismatch! */
753 dsfield |= old & 3; /* leave CU bits */
756 ip->ip_tos = dsfield;
758 * update checksum (from RFC1624)
759 * HC' = ~(~HC + ~m + m')
761 sum = ~ntohs(ip->ip_sum) & 0xffff;
762 sum += 0xff00 + (~old & 0xff) + dsfield;
763 sum = (sum >> 16) + (sum & 0xffff);
764 sum += (sum >> 16); /* add carry */
766 ip->ip_sum = htons(~sum & 0xffff);
769 else if (pktattr->pattr_af == AF_INET6) {
770 struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
773 flowlabel = ntohl(ip6->ip6_flow);
774 if ((flowlabel >> 28) != 6)
775 return; /* version mismatch! */
776 flowlabel = (flowlabel & 0xf03fffff) | (dsfield << 20);
777 ip6->ip6_flow = htonl(flowlabel);
783 * high resolution clock support taking advantage of a machine dependent
784 * high resolution time counter (e.g., timestamp counter of intel pentium).
786 * - 64-bit-long monotonically-increasing counter
787 * - frequency range is 100M-4GHz (CPU speed)
789 /* if pcc is not available or disabled, emulate 256MHz using microtime() */
790 #define MACHCLK_SHIFT 8
793 uint64_t machclk_freq = 0;
794 uint32_t machclk_per_tick = 0;
799 callout_init(&tbr_callout);
803 #if !defined(__i386__) || defined(ALTQ_NOPCC)
805 #elif defined(__DragonFly__) && defined(SMP)
807 #elif defined(__i386__)
808 /* check if TSC is available */
809 if (machclk_usepcc == 1 && (cpu_feature & CPUID_TSC) == 0)
813 if (machclk_usepcc == 0) {
814 /* emulate 256MHz using microtime() */
815 machclk_freq = 1000000LLU << MACHCLK_SHIFT;
816 machclk_per_tick = machclk_freq / hz;
818 kprintf("altq: emulate %juHz cpu clock\n", (uintmax_t)machclk_freq);
824 * if the clock frequency (of Pentium TSC or Alpha PCC) is
825 * accessible, just use it.
827 #ifdef _RDTSC_SUPPORTED_
828 if (cpu_feature & CPUID_TSC)
829 machclk_freq = (uint64_t)tsc_frequency;
833 * if we don't know the clock frequency, measure it.
835 if (machclk_freq == 0) {
837 struct timeval tv_start, tv_end;
838 uint64_t start, end, diff;
841 microtime(&tv_start);
842 start = read_machclk();
843 timo = hz; /* 1 sec */
844 tsleep(&wait, PCATCH, "init_machclk", timo);
846 end = read_machclk();
847 diff = (uint64_t)(tv_end.tv_sec - tv_start.tv_sec) * 1000000
848 + tv_end.tv_usec - tv_start.tv_usec;
850 machclk_freq = (end - start) * 1000000 / diff;
853 machclk_per_tick = machclk_freq / hz;
856 kprintf("altq: CPU clock: %juHz\n", (uintmax_t)machclk_freq);
865 if (machclk_usepcc) {
866 #ifdef _RDTSC_SUPPORTED_
869 panic("read_machclk");
875 val = (((uint64_t)(tv.tv_sec - boottime.tv_sec) * 1000000
876 + tv.tv_usec) << MACHCLK_SHIFT);