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.2 2005/04/04 17:08:16 joerg 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>
49 #include <net/if_dl.h>
50 #include <net/if_types.h>
51 #include <net/ifq_var.h>
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/ip.h>
57 #include <netinet/ip6.h>
59 #include <netinet/tcp.h>
60 #include <netinet/udp.h>
62 #include <net/pf/pfvar.h>
63 #include <net/altq/altq.h>
65 /* machine dependent clock related includes */
67 #include <machine/clock.h> /* for tsc_freq */
68 #include <machine/md_var.h> /* for cpu_feature */
69 #include <machine/specialreg.h> /* for CPUID_TSC */
73 * internal function prototypes
75 static void tbr_timeout(void *);
76 int (*altq_input)(struct mbuf *, int) = NULL;
77 static int tbr_timer = 0; /* token bucket regulator timer */
78 static struct callout tbr_callout;
80 int pfaltq_running; /* keep track of running state */
82 MALLOC_DEFINE(M_ALTQ, "altq", "ALTQ structures");
85 * alternate queueing support routines
88 /* look up the queue state by the interface name and the queueing type. */
90 altq_lookup(const char *name, int type)
94 if ((ifp = ifunit(name)) != NULL) {
95 if (type != ALTQT_NONE && ifp->if_snd.altq_type == type)
96 return (ifp->if_snd.altq_disc);
103 altq_attach(struct ifaltq *ifq, int type, void *discipline,
104 int (*enqueue)(struct ifaltq *, struct mbuf *, struct altq_pktattr *),
105 struct mbuf *(*dequeue)(struct ifaltq *, int),
106 int (*request)(struct ifaltq *, int, void *),
108 void *(*classify)(struct ifaltq *, struct mbuf *,
109 struct altq_pktattr *))
111 if (!ifq_is_ready(ifq))
114 ifq->altq_type = type;
115 ifq->altq_disc = discipline;
116 ifq->altq_enqueue = enqueue;
117 ifq->altq_dequeue = dequeue;
118 ifq->altq_request = request;
119 ifq->altq_clfier = clfier;
120 ifq->altq_classify = classify;
121 ifq->altq_flags &= (ALTQF_CANTCHANGE|ALTQF_ENABLED);
126 altq_detach(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_enable(struct ifaltq *ifq)
149 if (!ifq_is_ready(ifq))
151 if (ifq_is_enabled(ifq))
156 KKASSERT(ifq->ifq_len == 0);
157 ifq->altq_flags |= ALTQF_ENABLED;
158 if (ifq->altq_clfier != NULL)
159 ifq->altq_flags |= ALTQF_CLASSIFY;
166 altq_disable(struct ifaltq *ifq)
170 if (!ifq_is_enabled(ifq))
175 KKASSERT(ifq->ifq_len == 0);
176 ifq->altq_flags &= ~(ALTQF_ENABLED|ALTQF_CLASSIFY);
182 * internal representation of token bucket parameters
183 * rate: byte_per_unittime << 32
184 * (((bits_per_sec) / 8) << 32) / machclk_freq
189 #define TBR_SCALE(x) ((int64_t)(x) << TBR_SHIFT)
190 #define TBR_UNSCALE(x) ((x) >> TBR_SHIFT)
193 tbr_dequeue(struct ifaltq *ifq, int op)
195 struct tb_regulator *tbr;
201 if (op == ALTDQ_REMOVE && tbr->tbr_lastop == ALTDQ_POLL) {
202 /* if this is a remove after poll, bypass tbr check */
204 /* update token only when it is negative */
205 if (tbr->tbr_token <= 0) {
206 now = read_machclk();
207 interval = now - tbr->tbr_last;
208 if (interval >= tbr->tbr_filluptime)
209 tbr->tbr_token = tbr->tbr_depth;
211 tbr->tbr_token += interval * tbr->tbr_rate;
212 if (tbr->tbr_token > tbr->tbr_depth)
213 tbr->tbr_token = tbr->tbr_depth;
217 /* if token is still negative, don't allow dequeue */
218 if (tbr->tbr_token <= 0)
222 if (ifq_is_enabled(ifq))
223 m = (*ifq->altq_dequeue)(ifq, op);
224 else if (op == ALTDQ_POLL)
229 if (m != NULL && op == ALTDQ_REMOVE)
230 tbr->tbr_token -= TBR_SCALE(m_pktlen(m));
231 tbr->tbr_lastop = op;
236 * set a token bucket regulator.
237 * if the specified rate is zero, the token bucket regulator is deleted.
240 tbr_set(struct ifaltq *ifq, struct tb_profile *profile)
242 struct tb_regulator *tbr, *otbr;
244 if (machclk_freq == 0)
246 if (machclk_freq == 0) {
247 printf("tbr_set: no cpu clock available!\n");
251 if (profile->rate == 0) {
252 /* delete this tbr */
253 if ((tbr = ifq->altq_tbr) == NULL)
255 ifq->altq_tbr = NULL;
260 tbr = malloc(sizeof(*tbr), M_ALTQ, M_WAITOK | M_ZERO);
261 tbr->tbr_rate = TBR_SCALE(profile->rate / 8) / machclk_freq;
262 tbr->tbr_depth = TBR_SCALE(profile->depth);
263 if (tbr->tbr_rate > 0)
264 tbr->tbr_filluptime = tbr->tbr_depth / tbr->tbr_rate;
266 tbr->tbr_filluptime = 0xffffffffffffffffLL;
267 tbr->tbr_token = tbr->tbr_depth;
268 tbr->tbr_last = read_machclk();
269 tbr->tbr_lastop = ALTDQ_REMOVE;
271 otbr = ifq->altq_tbr;
272 ifq->altq_tbr = tbr; /* set the new tbr */
276 else if (tbr_timer == 0) {
277 callout_reset(&tbr_callout, 1, tbr_timeout, NULL);
284 * tbr_timeout goes through the interface list, and kicks the drivers
288 tbr_timeout(void *arg)
295 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
296 if (ifp->if_snd.altq_tbr == NULL)
299 if (!ifq_is_empty(&ifp->if_snd) && ifp->if_start != NULL)
300 (*ifp->if_start)(ifp);
304 callout_reset(&tbr_callout, 1, tbr_timeout, NULL);
306 tbr_timer = 0; /* don't need tbr_timer anymore */
310 * get token bucket regulator profile
313 tbr_get(struct ifaltq *ifq, struct tb_profile *profile)
315 struct tb_regulator *tbr;
317 if ((tbr = ifq->altq_tbr) == NULL) {
322 (u_int)TBR_UNSCALE(tbr->tbr_rate * 8 * machclk_freq);
323 profile->depth = (u_int)TBR_UNSCALE(tbr->tbr_depth);
329 * attach a discipline to the interface. if one already exists, it is
333 altq_pfattach(struct pf_altq *a)
336 struct tb_profile tb;
339 switch (a->scheduler) {
344 error = cbq_pfattach(a);
349 error = priq_pfattach(a);
354 error = hfsc_pfattach(a);
361 ifp = ifunit(a->ifname);
363 /* if the state is running, enable altq */
364 if (error == 0 && pfaltq_running &&
365 ifp != NULL && ifp->if_snd.altq_type != ALTQT_NONE &&
366 !ifq_is_enabled(&ifp->if_snd))
367 error = altq_enable(&ifp->if_snd);
369 /* if altq is already enabled, reset set tokenbucket regulator */
370 if (error == 0 && ifp != NULL && ifq_is_enabled(&ifp->if_snd)) {
371 tb.rate = a->ifbandwidth;
372 tb.depth = a->tbrsize;
374 error = tbr_set(&ifp->if_snd, &tb);
382 * detach a discipline from the interface.
383 * it is possible that the discipline was already overridden by another
387 altq_pfdetach(struct pf_altq *a)
392 if ((ifp = ifunit(a->ifname)) == NULL)
395 /* if this discipline is no longer referenced, just return */
396 if (a->altq_disc == NULL || a->altq_disc != ifp->if_snd.altq_disc)
400 if (ifq_is_enabled(&ifp->if_snd))
401 error = altq_disable(&ifp->if_snd);
403 error = altq_detach(&ifp->if_snd);
410 * add a discipline or a queue
413 altq_add(struct pf_altq *a)
417 if (a->qname[0] != 0)
418 return (altq_add_queue(a));
420 if (machclk_freq == 0)
422 if (machclk_freq == 0)
423 panic("altq_add: no cpu clock");
425 switch (a->scheduler) {
428 error = cbq_add_altq(a);
433 error = priq_add_altq(a);
438 error = hfsc_add_altq(a);
449 * remove a discipline or a queue
452 altq_remove(struct pf_altq *a)
456 if (a->qname[0] != 0)
457 return (altq_remove_queue(a));
459 switch (a->scheduler) {
462 error = cbq_remove_altq(a);
467 error = priq_remove_altq(a);
472 error = hfsc_remove_altq(a);
483 * add a queue to the discipline
486 altq_add_queue(struct pf_altq *a)
490 switch (a->scheduler) {
493 error = cbq_add_queue(a);
498 error = priq_add_queue(a);
503 error = hfsc_add_queue(a);
514 * remove a queue from the discipline
517 altq_remove_queue(struct pf_altq *a)
521 switch (a->scheduler) {
524 error = cbq_remove_queue(a);
529 error = priq_remove_queue(a);
534 error = hfsc_remove_queue(a);
545 * get queue statistics
548 altq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
552 switch (a->scheduler) {
555 error = cbq_getqstats(a, ubuf, nbytes);
560 error = priq_getqstats(a, ubuf, nbytes);
565 error = hfsc_getqstats(a, ubuf, nbytes);
576 * read and write diffserv field in IPv4 or IPv6 header
579 read_dsfield(struct mbuf *m, struct altq_pktattr *pktattr)
582 uint8_t ds_field = 0;
584 if (pktattr == NULL ||
585 (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
588 /* verify that pattr_hdr is within the mbuf data */
589 for (m0 = m; m0 != NULL; m0 = m0->m_next) {
590 if ((pktattr->pattr_hdr >= m0->m_data) &&
591 (pktattr->pattr_hdr < m0->m_data + m0->m_len))
595 /* ick, pattr_hdr is stale */
596 pktattr->pattr_af = AF_UNSPEC;
598 printf("read_dsfield: can't locate header!\n");
603 if (pktattr->pattr_af == AF_INET) {
604 struct ip *ip = (struct ip *)pktattr->pattr_hdr;
607 return ((uint8_t)0); /* version mismatch! */
608 ds_field = ip->ip_tos;
611 else if (pktattr->pattr_af == AF_INET6) {
612 struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
615 flowlabel = ntohl(ip6->ip6_flow);
616 if ((flowlabel >> 28) != 6)
617 return ((uint8_t)0); /* version mismatch! */
618 ds_field = (flowlabel >> 20) & 0xff;
625 write_dsfield(struct mbuf *m, struct altq_pktattr *pktattr, uint8_t dsfield)
629 if (pktattr == NULL ||
630 (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
633 /* verify that pattr_hdr is within the mbuf data */
634 for (m0 = m; m0 != NULL; m0 = m0->m_next) {
635 if ((pktattr->pattr_hdr >= m0->m_data) &&
636 (pktattr->pattr_hdr < m0->m_data + m0->m_len))
640 /* ick, pattr_hdr is stale */
641 pktattr->pattr_af = AF_UNSPEC;
643 printf("write_dsfield: can't locate header!\n");
648 if (pktattr->pattr_af == AF_INET) {
649 struct ip *ip = (struct ip *)pktattr->pattr_hdr;
654 return; /* version mismatch! */
656 dsfield |= old & 3; /* leave CU bits */
659 ip->ip_tos = dsfield;
661 * update checksum (from RFC1624)
662 * HC' = ~(~HC + ~m + m')
664 sum = ~ntohs(ip->ip_sum) & 0xffff;
665 sum += 0xff00 + (~old & 0xff) + dsfield;
666 sum = (sum >> 16) + (sum & 0xffff);
667 sum += (sum >> 16); /* add carry */
669 ip->ip_sum = htons(~sum & 0xffff);
672 else if (pktattr->pattr_af == AF_INET6) {
673 struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
676 flowlabel = ntohl(ip6->ip6_flow);
677 if ((flowlabel >> 28) != 6)
678 return; /* version mismatch! */
679 flowlabel = (flowlabel & 0xf03fffff) | (dsfield << 20);
680 ip6->ip6_flow = htonl(flowlabel);
686 * high resolution clock support taking advantage of a machine dependent
687 * high resolution time counter (e.g., timestamp counter of intel pentium).
689 * - 64-bit-long monotonically-increasing counter
690 * - frequency range is 100M-4GHz (CPU speed)
692 /* if pcc is not available or disabled, emulate 256MHz using microtime() */
693 #define MACHCLK_SHIFT 8
696 uint32_t machclk_freq = 0;
697 uint32_t machclk_per_tick = 0;
702 callout_init(&tbr_callout);
706 #if !defined(__i386__) || defined(ALTQ_NOPCC)
708 #elif defined(__DragonFly__) && defined(SMP)
710 #elif defined(__i386__)
711 /* check if TSC is available */
712 if (machclk_usepcc == 1 && (cpu_feature & CPUID_TSC) == 0)
716 if (machclk_usepcc == 0) {
717 /* emulate 256MHz using microtime() */
718 machclk_freq = 1000000 << MACHCLK_SHIFT;
719 machclk_per_tick = machclk_freq / hz;
721 printf("altq: emulate %uHz cpu clock\n", machclk_freq);
727 * if the clock frequency (of Pentium TSC or Alpha PCC) is
728 * accessible, just use it.
731 machclk_freq = tsc_freq;
733 #error "machclk_freq interface not implemented"
737 * if we don't know the clock frequency, measure it.
739 if (machclk_freq == 0) {
741 struct timeval tv_start, tv_end;
742 uint64_t start, end, diff;
745 microtime(&tv_start);
746 start = read_machclk();
747 timo = hz; /* 1 sec */
748 tsleep(&wait, PCATCH, "init_machclk", timo);
750 end = read_machclk();
751 diff = (uint64_t)(tv_end.tv_sec - tv_start.tv_sec) * 1000000
752 + tv_end.tv_usec - tv_start.tv_usec;
754 machclk_freq = (u_int)((end - start) * 1000000 / diff);
757 machclk_per_tick = machclk_freq / hz;
760 printf("altq: CPU clock: %uHz\n", machclk_freq);
769 if (machclk_usepcc) {
770 #if defined(__i386__)
773 panic("read_machclk");
779 val = (((uint64_t)(tv.tv_sec - boottime.tv_sec) * 1000000
780 + tv.tv_usec) << MACHCLK_SHIFT);