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 altq_mapsubq_t mapsubq,
109 ifsq_enqueue_t enqueue, ifsq_dequeue_t dequeue, ifsq_request_t request,
111 void *(*classify)(struct ifaltq *, struct mbuf *, struct altq_pktattr *))
113 if (!ifq_is_ready(ifq))
116 ifq->altq_type = type;
117 ifq->altq_disc = discipline;
118 ifq->altq_clfier = clfier;
119 ifq->altq_classify = classify;
120 ifq->altq_flags &= (ALTQF_CANTCHANGE|ALTQF_ENABLED);
121 ifq_set_methods(ifq, mapsubq, enqueue, dequeue, request);
126 altq_detach_locked(struct ifaltq *ifq)
128 if (!ifq_is_ready(ifq))
130 if (ifq_is_enabled(ifq))
132 if (!ifq_is_attached(ifq))
135 ifq_set_classic(ifq);
136 ifq->altq_type = ALTQT_NONE;
137 ifq->altq_disc = NULL;
138 ifq->altq_clfier = NULL;
139 ifq->altq_classify = NULL;
140 ifq->altq_flags &= ALTQF_CANTCHANGE;
145 altq_detach(struct ifaltq *ifq)
150 error = altq_detach_locked(ifq);
156 altq_enable_locked(struct ifaltq *ifq)
158 if (!ifq_is_ready(ifq))
160 if (ifq_is_enabled(ifq))
163 ifq_purge_all_locked(ifq);
165 ifq->altq_flags |= ALTQF_ENABLED;
166 if (ifq->altq_clfier != NULL)
167 ifq->altq_flags |= ALTQF_CLASSIFY;
172 altq_enable(struct ifaltq *ifq)
177 error = altq_enable_locked(ifq);
183 altq_disable_locked(struct ifaltq *ifq)
185 if (!ifq_is_enabled(ifq))
188 ifq_purge_all_locked(ifq);
189 ifq->altq_flags &= ~(ALTQF_ENABLED|ALTQF_CLASSIFY);
194 altq_disable(struct ifaltq *ifq)
199 error = altq_disable_locked(ifq);
205 * internal representation of token bucket parameters
206 * rate: byte_per_unittime << 32
207 * (((bits_per_sec) / 8) << 32) / machclk_freq
212 #define TBR_SCALE(x) ((int64_t)(x) << TBR_SHIFT)
213 #define TBR_UNSCALE(x) ((x) >> TBR_SHIFT)
216 tbr_dequeue(struct ifaltq_subque *ifsq, struct mbuf *mpolled, int op)
218 struct ifaltq *ifq = ifsq->ifsq_altq;
219 struct tb_regulator *tbr;
224 if (ifsq_get_index(ifsq) != ALTQ_SUBQ_INDEX_DEFAULT) {
226 * Race happened, the unrelated subqueue was
227 * picked during the packet scheduler transition.
229 ifsq_classic_request(ifsq, ALTRQ_PURGE, NULL);
235 if (op == ALTDQ_REMOVE && tbr->tbr_lastop == ALTDQ_POLL) {
236 /* if this is a remove after poll, bypass tbr check */
238 /* update token only when it is negative */
239 if (tbr->tbr_token <= 0) {
240 now = read_machclk();
241 interval = now - tbr->tbr_last;
242 if (interval >= tbr->tbr_filluptime)
243 tbr->tbr_token = tbr->tbr_depth;
245 tbr->tbr_token += interval * tbr->tbr_rate;
246 if (tbr->tbr_token > tbr->tbr_depth)
247 tbr->tbr_token = tbr->tbr_depth;
251 /* if token is still negative, don't allow dequeue */
252 if (tbr->tbr_token <= 0) {
258 if (ifq_is_enabled(ifq)) {
259 m = (*ifsq->ifsq_dequeue)(ifsq, mpolled, op);
260 } else if (op == ALTDQ_POLL) {
264 KKASSERT(mpolled == NULL || mpolled == m);
267 if (m != NULL && op == ALTDQ_REMOVE)
268 tbr->tbr_token -= TBR_SCALE(m_pktlen(m));
269 tbr->tbr_lastop = op;
275 * set a token bucket regulator.
276 * if the specified rate is zero, the token bucket regulator is deleted.
279 tbr_set_locked(struct ifaltq *ifq, struct tb_profile *profile)
281 struct tb_regulator *tbr, *otbr;
283 if (machclk_freq == 0)
285 if (machclk_freq == 0) {
286 kprintf("%s: no cpu clock available!\n", __func__);
290 if (profile->rate == 0) {
291 /* delete this tbr */
292 if ((tbr = ifq->altq_tbr) == NULL)
294 ifq->altq_tbr = NULL;
299 tbr = kmalloc(sizeof(*tbr), M_ALTQ, M_WAITOK | M_ZERO);
300 tbr->tbr_rate = TBR_SCALE(profile->rate / 8) / machclk_freq;
301 tbr->tbr_depth = TBR_SCALE(profile->depth);
302 if (tbr->tbr_rate > 0)
303 tbr->tbr_filluptime = tbr->tbr_depth / tbr->tbr_rate;
305 tbr->tbr_filluptime = 0xffffffffffffffffLL;
306 tbr->tbr_token = tbr->tbr_depth;
307 tbr->tbr_last = read_machclk();
308 tbr->tbr_lastop = ALTDQ_REMOVE;
310 otbr = ifq->altq_tbr;
311 ifq->altq_tbr = tbr; /* set the new tbr */
315 else if (tbr_timer == 0) {
316 callout_reset(&tbr_callout, 1, tbr_timeout, NULL);
323 tbr_set(struct ifaltq *ifq, struct tb_profile *profile)
328 error = tbr_set_locked(ifq, profile);
334 * tbr_timeout goes through the interface list, and kicks the drivers
338 tbr_timeout(void *arg)
345 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
346 struct ifaltq_subque *ifsq;
348 if (ifp->if_snd.altq_tbr == NULL)
351 ifsq = &ifp->if_snd.altq_subq[ALTQ_SUBQ_INDEX_DEFAULT];
353 if (!ifsq_is_empty(ifsq) && ifp->if_start != NULL) {
354 ifnet_serialize_tx(ifp, ifsq);
355 (*ifp->if_start)(ifp, ifsq);
356 ifnet_deserialize_tx(ifp, ifsq);
361 callout_reset(&tbr_callout, 1, tbr_timeout, NULL);
363 tbr_timer = 0; /* don't need tbr_timer anymore */
367 * get token bucket regulator profile
370 tbr_get(struct ifaltq *ifq, struct tb_profile *profile)
372 struct tb_regulator *tbr;
374 if ((tbr = ifq->altq_tbr) == NULL) {
379 (u_int)TBR_UNSCALE(tbr->tbr_rate * 8 * machclk_freq);
380 profile->depth = (u_int)TBR_UNSCALE(tbr->tbr_depth);
386 * attach a discipline to the interface. if one already exists, it is
390 altq_pfattach(struct pf_altq *a)
396 if (a->scheduler == ALTQT_NONE)
399 if (a->altq_disc == NULL)
402 ifp = ifunit(a->ifname);
409 switch (a->scheduler) {
412 error = cbq_pfattach(a, ifq);
417 error = priq_pfattach(a, ifq);
422 error = hfsc_pfattach(a, ifq);
427 error = fairq_pfattach(a, ifq);
435 /* if the state is running, enable altq */
436 if (error == 0 && pfaltq_running && ifq->altq_type != ALTQT_NONE &&
437 !ifq_is_enabled(ifq))
438 error = altq_enable_locked(ifq);
440 /* if altq is already enabled, reset set tokenbucket regulator */
441 if (error == 0 && ifq_is_enabled(ifq)) {
442 struct tb_profile tb;
444 tb.rate = a->ifbandwidth;
445 tb.depth = a->tbrsize;
446 error = tbr_set_locked(ifq, &tb);
454 * detach a discipline from the interface.
455 * it is possible that the discipline was already overridden by another
459 altq_pfdetach(struct pf_altq *a)
465 ifp = ifunit(a->ifname);
470 /* if this discipline is no longer referenced, just return */
471 if (a->altq_disc == NULL)
476 if (a->altq_disc != ifq->altq_disc)
479 if (ifq_is_enabled(ifq))
480 error = altq_disable_locked(ifq);
482 error = altq_detach_locked(ifq);
490 * add a discipline or a queue
493 altq_add(struct pf_altq *a)
497 if (a->qname[0] != 0)
498 return (altq_add_queue(a));
500 if (machclk_freq == 0)
502 if (machclk_freq == 0)
503 panic("altq_add: no cpu clock");
505 switch (a->scheduler) {
508 error = cbq_add_altq(a);
513 error = priq_add_altq(a);
518 error = hfsc_add_altq(a);
523 error = fairq_add_altq(a);
534 * remove a discipline or a queue
537 altq_remove(struct pf_altq *a)
541 if (a->qname[0] != 0)
542 return (altq_remove_queue(a));
544 switch (a->scheduler) {
547 error = cbq_remove_altq(a);
552 error = priq_remove_altq(a);
557 error = hfsc_remove_altq(a);
562 error = fairq_remove_altq(a);
573 * add a queue to the discipline
576 altq_add_queue(struct pf_altq *a)
580 switch (a->scheduler) {
583 error = cbq_add_queue(a);
588 error = priq_add_queue(a);
593 error = hfsc_add_queue(a);
598 error = fairq_add_queue(a);
609 * remove a queue from the discipline
612 altq_remove_queue(struct pf_altq *a)
616 switch (a->scheduler) {
619 error = cbq_remove_queue(a);
624 error = priq_remove_queue(a);
629 error = hfsc_remove_queue(a);
634 error = fairq_remove_queue(a);
645 * get queue statistics
648 altq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
652 switch (a->scheduler) {
655 error = cbq_getqstats(a, ubuf, nbytes);
660 error = priq_getqstats(a, ubuf, nbytes);
665 error = hfsc_getqstats(a, ubuf, nbytes);
670 error = fairq_getqstats(a, ubuf, nbytes);
681 * read and write diffserv field in IPv4 or IPv6 header
684 read_dsfield(struct mbuf *m, struct altq_pktattr *pktattr)
687 uint8_t ds_field = 0;
689 if (pktattr == NULL ||
690 (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
693 /* verify that pattr_hdr is within the mbuf data */
694 for (m0 = m; m0 != NULL; m0 = m0->m_next) {
695 if ((pktattr->pattr_hdr >= m0->m_data) &&
696 (pktattr->pattr_hdr < m0->m_data + m0->m_len))
700 /* ick, pattr_hdr is stale */
701 pktattr->pattr_af = AF_UNSPEC;
703 kprintf("read_dsfield: can't locate header!\n");
708 if (pktattr->pattr_af == AF_INET) {
709 struct ip *ip = (struct ip *)pktattr->pattr_hdr;
712 return ((uint8_t)0); /* version mismatch! */
713 ds_field = ip->ip_tos;
716 else if (pktattr->pattr_af == AF_INET6) {
717 struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
720 flowlabel = ntohl(ip6->ip6_flow);
721 if ((flowlabel >> 28) != 6)
722 return ((uint8_t)0); /* version mismatch! */
723 ds_field = (flowlabel >> 20) & 0xff;
730 write_dsfield(struct mbuf *m, struct altq_pktattr *pktattr, uint8_t dsfield)
734 if (pktattr == NULL ||
735 (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
738 /* verify that pattr_hdr is within the mbuf data */
739 for (m0 = m; m0 != NULL; m0 = m0->m_next) {
740 if ((pktattr->pattr_hdr >= m0->m_data) &&
741 (pktattr->pattr_hdr < m0->m_data + m0->m_len))
745 /* ick, pattr_hdr is stale */
746 pktattr->pattr_af = AF_UNSPEC;
748 kprintf("write_dsfield: can't locate header!\n");
753 if (pktattr->pattr_af == AF_INET) {
754 struct ip *ip = (struct ip *)pktattr->pattr_hdr;
759 return; /* version mismatch! */
761 dsfield |= old & 3; /* leave CU bits */
764 ip->ip_tos = dsfield;
766 * update checksum (from RFC1624)
767 * HC' = ~(~HC + ~m + m')
769 sum = ~ntohs(ip->ip_sum) & 0xffff;
770 sum += 0xff00 + (~old & 0xff) + dsfield;
771 sum = (sum >> 16) + (sum & 0xffff);
772 sum += (sum >> 16); /* add carry */
774 ip->ip_sum = htons(~sum & 0xffff);
777 else if (pktattr->pattr_af == AF_INET6) {
778 struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
781 flowlabel = ntohl(ip6->ip6_flow);
782 if ((flowlabel >> 28) != 6)
783 return; /* version mismatch! */
784 flowlabel = (flowlabel & 0xf03fffff) | (dsfield << 20);
785 ip6->ip6_flow = htonl(flowlabel);
791 * high resolution clock support taking advantage of a machine dependent
792 * high resolution time counter (e.g., timestamp counter of intel pentium).
794 * - 64-bit-long monotonically-increasing counter
795 * - frequency range is 100M-4GHz (CPU speed)
797 /* if pcc is not available or disabled, emulate 256MHz using microtime() */
798 #define MACHCLK_SHIFT 8
801 uint64_t machclk_freq = 0;
802 uint32_t machclk_per_tick = 0;
807 callout_init(&tbr_callout);
811 #if !defined(__i386__) || defined(ALTQ_NOPCC)
813 #elif defined(__DragonFly__)
815 #elif defined(__i386__)
816 /* check if TSC is available */
817 if (machclk_usepcc == 1 && (cpu_feature & CPUID_TSC) == 0)
821 if (machclk_usepcc == 0) {
822 /* emulate 256MHz using microtime() */
823 machclk_freq = 1000000LLU << MACHCLK_SHIFT;
824 machclk_per_tick = machclk_freq / hz;
826 kprintf("altq: emulate %juHz cpu clock\n", (uintmax_t)machclk_freq);
832 * if the clock frequency (of Pentium TSC or Alpha PCC) is
833 * accessible, just use it.
835 #ifdef _RDTSC_SUPPORTED_
836 if (cpu_feature & CPUID_TSC)
837 machclk_freq = (uint64_t)tsc_frequency;
841 * if we don't know the clock frequency, measure it.
843 if (machclk_freq == 0) {
845 struct timeval tv_start, tv_end;
846 uint64_t start, end, diff;
849 microtime(&tv_start);
850 start = read_machclk();
851 timo = hz; /* 1 sec */
852 tsleep(&wait, PCATCH, "init_machclk", timo);
854 end = read_machclk();
855 diff = (uint64_t)(tv_end.tv_sec - tv_start.tv_sec) * 1000000
856 + tv_end.tv_usec - tv_start.tv_usec;
858 machclk_freq = (end - start) * 1000000 / diff;
861 machclk_per_tick = machclk_freq / hz;
864 kprintf("altq: CPU clock: %juHz\n", (uintmax_t)machclk_freq);
873 if (machclk_usepcc) {
874 #ifdef _RDTSC_SUPPORTED_
877 panic("read_machclk");
883 val = (((uint64_t)(tv.tv_sec - boottime.tv_sec) * 1000000
884 + tv.tv_usec) << MACHCLK_SHIFT);