xref: /dragonfly/sys/net/altq/altq_subr.c (revision d4ef6694)
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 $ */
3 
4 /*
5  * Copyright (C) 1997-2003
6  *	Sony Computer Science Laboratories Inc.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #include "opt_altq.h"
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33 
34 #include <sys/param.h>
35 #include <sys/malloc.h>
36 #include <sys/mbuf.h>
37 #include <sys/systm.h>
38 #include <sys/proc.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>
48 
49 #include <net/if.h>
50 #include <net/if_dl.h>
51 #include <net/if_types.h>
52 #include <net/ifq_var.h>
53 
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/ip.h>
57 #ifdef INET6
58 #include <netinet/ip6.h>
59 #endif
60 #include <netinet/tcp.h>
61 #include <netinet/udp.h>
62 
63 #include <net/pf/pfvar.h>
64 #include <net/altq/altq.h>
65 
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 */
70 
71 /*
72  * internal function prototypes
73  */
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 *);
79 
80 int (*altq_input)(struct mbuf *, int) = NULL;
81 static int tbr_timer = 0;	/* token bucket regulator timer */
82 static struct callout tbr_callout;
83 
84 int pfaltq_running;	/* keep track of running state */
85 
86 MALLOC_DEFINE(M_ALTQ, "altq", "ALTQ structures");
87 
88 /*
89  * alternate queueing support routines
90  */
91 
92 /* look up the queue state by the interface name and the queueing type. */
93 void *
94 altq_lookup(const char *name, int type)
95 {
96 	struct ifnet *ifp;
97 
98 	if ((ifp = ifunit(name)) != NULL) {
99 		if (type != ALTQT_NONE && ifp->if_snd.altq_type == type)
100 			return (ifp->if_snd.altq_disc);
101 	}
102 
103 	return (NULL);
104 }
105 
106 int
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,
110     void *clfier,
111     void *(*classify)(struct ifaltq *, struct mbuf *, struct altq_pktattr *))
112 {
113 	if (!ifq_is_ready(ifq))
114 		return ENXIO;
115 
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);
122 	return 0;
123 }
124 
125 static int
126 altq_detach_locked(struct ifaltq *ifq)
127 {
128 	if (!ifq_is_ready(ifq))
129 		return ENXIO;
130 	if (ifq_is_enabled(ifq))
131 		return EBUSY;
132 	if (!ifq_is_attached(ifq))
133 		return (0);
134 
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;
141 	return 0;
142 }
143 
144 int
145 altq_detach(struct ifaltq *ifq)
146 {
147 	int error;
148 
149 	ifq_lock_all(ifq);
150 	error = altq_detach_locked(ifq);
151 	ifq_unlock_all(ifq);
152 	return error;
153 }
154 
155 static int
156 altq_enable_locked(struct ifaltq *ifq)
157 {
158 	if (!ifq_is_ready(ifq))
159 		return ENXIO;
160 	if (ifq_is_enabled(ifq))
161 		return 0;
162 
163 	ifq_purge_all_locked(ifq);
164 
165 	ifq->altq_flags |= ALTQF_ENABLED;
166 	if (ifq->altq_clfier != NULL)
167 		ifq->altq_flags |= ALTQF_CLASSIFY;
168 	return 0;
169 }
170 
171 int
172 altq_enable(struct ifaltq *ifq)
173 {
174 	int error;
175 
176 	ifq_lock_all(ifq);
177 	error = altq_enable_locked(ifq);
178 	ifq_unlock_all(ifq);
179 	return error;
180 }
181 
182 static int
183 altq_disable_locked(struct ifaltq *ifq)
184 {
185 	if (!ifq_is_enabled(ifq))
186 		return 0;
187 
188 	ifq_purge_all_locked(ifq);
189 	ifq->altq_flags &= ~(ALTQF_ENABLED|ALTQF_CLASSIFY);
190 	return 0;
191 }
192 
193 int
194 altq_disable(struct ifaltq *ifq)
195 {
196 	int error;
197 
198 	ifq_lock_all(ifq);
199 	error = altq_disable_locked(ifq);
200 	ifq_unlock_all(ifq);
201 	return error;
202 }
203 
204 /*
205  * internal representation of token bucket parameters
206  *	rate:	byte_per_unittime << 32
207  *		(((bits_per_sec) / 8) << 32) / machclk_freq
208  *	depth:	byte << 32
209  *
210  */
211 #define	TBR_SHIFT	32
212 #define	TBR_SCALE(x)	((int64_t)(x) << TBR_SHIFT)
213 #define	TBR_UNSCALE(x)	((x) >> TBR_SHIFT)
214 
215 struct mbuf *
216 tbr_dequeue(struct ifaltq_subque *ifsq, int op)
217 {
218 	struct ifaltq *ifq = ifsq->ifsq_altq;
219 	struct tb_regulator *tbr;
220 	struct mbuf *m;
221 	int64_t interval;
222 	uint64_t now;
223 
224 	if (ifsq_get_index(ifsq) != ALTQ_SUBQ_INDEX_DEFAULT) {
225 		/*
226 		 * Race happened, the unrelated subqueue was
227 		 * picked during the packet scheduler transition.
228 		 */
229 		ifsq_classic_request(ifsq, ALTRQ_PURGE, NULL);
230 		return NULL;
231 	}
232 
233 	crit_enter();
234 	tbr = ifq->altq_tbr;
235 	if (op == ALTDQ_REMOVE && tbr->tbr_lastop == ALTDQ_POLL) {
236 		/* if this is a remove after poll, bypass tbr check */
237 	} else {
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;
244 			else {
245 				tbr->tbr_token += interval * tbr->tbr_rate;
246 				if (tbr->tbr_token > tbr->tbr_depth)
247 					tbr->tbr_token = tbr->tbr_depth;
248 			}
249 			tbr->tbr_last = now;
250 		}
251 		/* if token is still negative, don't allow dequeue */
252 		if (tbr->tbr_token <= 0) {
253 			crit_exit();
254 			return (NULL);
255 		}
256 	}
257 
258 	if (ifq_is_enabled(ifq))
259 		m = (*ifsq->ifsq_dequeue)(ifsq, op);
260 	else
261 		m = ifsq_classic_dequeue(ifsq, op);
262 
263 	if (m != NULL && op == ALTDQ_REMOVE)
264 		tbr->tbr_token -= TBR_SCALE(m_pktlen(m));
265 	tbr->tbr_lastop = op;
266 	crit_exit();
267 	return (m);
268 }
269 
270 /*
271  * set a token bucket regulator.
272  * if the specified rate is zero, the token bucket regulator is deleted.
273  */
274 static int
275 tbr_set_locked(struct ifaltq *ifq, struct tb_profile *profile)
276 {
277 	struct tb_regulator *tbr, *otbr;
278 
279 	if (machclk_freq == 0)
280 		init_machclk();
281 	if (machclk_freq == 0) {
282 		kprintf("%s: no cpu clock available!\n", __func__);
283 		return (ENXIO);
284 	}
285 
286 	if (profile->rate == 0) {
287 		/* delete this tbr */
288 		if ((tbr = ifq->altq_tbr) == NULL)
289 			return (ENOENT);
290 		ifq->altq_tbr = NULL;
291 		kfree(tbr, M_ALTQ);
292 		return (0);
293 	}
294 
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;
300 	else
301 		tbr->tbr_filluptime = 0xffffffffffffffffLL;
302 	tbr->tbr_token = tbr->tbr_depth;
303 	tbr->tbr_last = read_machclk();
304 	tbr->tbr_lastop = ALTDQ_REMOVE;
305 
306 	otbr = ifq->altq_tbr;
307 	ifq->altq_tbr = tbr;	/* set the new tbr */
308 
309 	if (otbr != NULL)
310 		kfree(otbr, M_ALTQ);
311 	else if (tbr_timer == 0) {
312 		callout_reset(&tbr_callout, 1, tbr_timeout, NULL);
313 		tbr_timer = 1;
314 	}
315 	return (0);
316 }
317 
318 int
319 tbr_set(struct ifaltq *ifq, struct tb_profile *profile)
320 {
321 	int error;
322 
323 	ifq_lock_all(ifq);
324 	error = tbr_set_locked(ifq, profile);
325 	ifq_unlock_all(ifq);
326 	return error;
327 }
328 
329 /*
330  * tbr_timeout goes through the interface list, and kicks the drivers
331  * if necessary.
332  */
333 static void
334 tbr_timeout(void *arg)
335 {
336 	struct ifnet *ifp;
337 	int active;
338 
339 	active = 0;
340 	crit_enter();
341 	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
342 		struct ifaltq_subque *ifsq;
343 
344 		if (ifp->if_snd.altq_tbr == NULL)
345 			continue;
346 
347 		ifsq = &ifp->if_snd.altq_subq[ALTQ_SUBQ_INDEX_DEFAULT];
348 		active++;
349 		if (!ifsq_is_empty(ifsq) && ifp->if_start != NULL) {
350 			ifsq_serialize_hw(ifsq);
351 			(*ifp->if_start)(ifp, ifsq);
352 			ifsq_deserialize_hw(ifsq);
353 		}
354 	}
355 	crit_exit();
356 	if (active > 0)
357 		callout_reset(&tbr_callout, 1, tbr_timeout, NULL);
358 	else
359 		tbr_timer = 0;	/* don't need tbr_timer anymore */
360 }
361 
362 /*
363  * get token bucket regulator profile
364  */
365 int
366 tbr_get(struct ifaltq *ifq, struct tb_profile *profile)
367 {
368 	struct tb_regulator *tbr;
369 
370 	if ((tbr = ifq->altq_tbr) == NULL) {
371 		profile->rate = 0;
372 		profile->depth = 0;
373 	} else {
374 		profile->rate =
375 		    (u_int)TBR_UNSCALE(tbr->tbr_rate * 8 * machclk_freq);
376 		profile->depth = (u_int)TBR_UNSCALE(tbr->tbr_depth);
377 	}
378 	return (0);
379 }
380 
381 /*
382  * attach a discipline to the interface.  if one already exists, it is
383  * overridden.
384  */
385 int
386 altq_pfattach(struct pf_altq *a)
387 {
388 	struct ifaltq *ifq;
389 	struct ifnet *ifp;
390 	int error;
391 
392 	if (a->scheduler == ALTQT_NONE)
393 		return 0;
394 
395 	if (a->altq_disc == NULL)
396 		return EINVAL;
397 
398 	ifp = ifunit(a->ifname);
399 	if (ifp == NULL)
400 		return EINVAL;
401 	ifq = &ifp->if_snd;
402 
403 	ifq_lock_all(ifq);
404 
405 	switch (a->scheduler) {
406 #ifdef ALTQ_CBQ
407 	case ALTQT_CBQ:
408 		error = cbq_pfattach(a, ifq);
409 		break;
410 #endif
411 #ifdef ALTQ_PRIQ
412 	case ALTQT_PRIQ:
413 		error = priq_pfattach(a, ifq);
414 		break;
415 #endif
416 #ifdef ALTQ_HFSC
417 	case ALTQT_HFSC:
418 		error = hfsc_pfattach(a, ifq);
419 		break;
420 #endif
421 #ifdef ALTQ_FAIRQ
422 	case ALTQT_FAIRQ:
423 		error = fairq_pfattach(a, ifq);
424 		break;
425 #endif
426 	default:
427 		error = ENXIO;
428 		goto back;
429 	}
430 
431 	/* if the state is running, enable altq */
432 	if (error == 0 && pfaltq_running && ifq->altq_type != ALTQT_NONE &&
433 	    !ifq_is_enabled(ifq))
434 		error = altq_enable_locked(ifq);
435 
436 	/* if altq is already enabled, reset set tokenbucket regulator */
437 	if (error == 0 && ifq_is_enabled(ifq)) {
438 		struct tb_profile tb;
439 
440 		tb.rate = a->ifbandwidth;
441 		tb.depth = a->tbrsize;
442 		error = tbr_set_locked(ifq, &tb);
443 	}
444 back:
445 	ifq_unlock_all(ifq);
446 	return (error);
447 }
448 
449 /*
450  * detach a discipline from the interface.
451  * it is possible that the discipline was already overridden by another
452  * discipline.
453  */
454 int
455 altq_pfdetach(struct pf_altq *a)
456 {
457 	struct ifnet *ifp;
458 	struct ifaltq *ifq;
459 	int error = 0;
460 
461 	ifp = ifunit(a->ifname);
462 	if (ifp == NULL)
463 		return (EINVAL);
464 	ifq = &ifp->if_snd;
465 
466 	/* if this discipline is no longer referenced, just return */
467 	if (a->altq_disc == NULL)
468 		return (0);
469 
470 	ifq_lock_all(ifq);
471 
472 	if (a->altq_disc != ifq->altq_disc)
473 		goto back;
474 
475 	if (ifq_is_enabled(ifq))
476 		error = altq_disable_locked(ifq);
477 	if (error == 0)
478 		error = altq_detach_locked(ifq);
479 
480 back:
481 	ifq_unlock_all(ifq);
482 	return (error);
483 }
484 
485 /*
486  * add a discipline or a queue
487  */
488 int
489 altq_add(struct pf_altq *a)
490 {
491 	int error = 0;
492 
493 	if (a->qname[0] != 0)
494 		return (altq_add_queue(a));
495 
496 	if (machclk_freq == 0)
497 		init_machclk();
498 	if (machclk_freq == 0)
499 		panic("altq_add: no cpu clock");
500 
501 	switch (a->scheduler) {
502 #ifdef ALTQ_CBQ
503 	case ALTQT_CBQ:
504 		error = cbq_add_altq(a);
505 		break;
506 #endif
507 #ifdef ALTQ_PRIQ
508 	case ALTQT_PRIQ:
509 		error = priq_add_altq(a);
510 		break;
511 #endif
512 #ifdef ALTQ_HFSC
513 	case ALTQT_HFSC:
514 		error = hfsc_add_altq(a);
515 		break;
516 #endif
517 #ifdef ALTQ_FAIRQ
518 	case ALTQT_FAIRQ:
519 		error = fairq_add_altq(a);
520 		break;
521 #endif
522 	default:
523 		error = ENXIO;
524 	}
525 
526 	return (error);
527 }
528 
529 /*
530  * remove a discipline or a queue
531  */
532 int
533 altq_remove(struct pf_altq *a)
534 {
535 	int error = 0;
536 
537 	if (a->qname[0] != 0)
538 		return (altq_remove_queue(a));
539 
540 	switch (a->scheduler) {
541 #ifdef ALTQ_CBQ
542 	case ALTQT_CBQ:
543 		error = cbq_remove_altq(a);
544 		break;
545 #endif
546 #ifdef ALTQ_PRIQ
547 	case ALTQT_PRIQ:
548 		error = priq_remove_altq(a);
549 		break;
550 #endif
551 #ifdef ALTQ_HFSC
552 	case ALTQT_HFSC:
553 		error = hfsc_remove_altq(a);
554 		break;
555 #endif
556 #ifdef ALTQ_FAIRQ
557 	case ALTQT_FAIRQ:
558 		error = fairq_remove_altq(a);
559 		break;
560 #endif
561 	default:
562 		error = ENXIO;
563 	}
564 
565 	return (error);
566 }
567 
568 /*
569  * add a queue to the discipline
570  */
571 int
572 altq_add_queue(struct pf_altq *a)
573 {
574 	int error = 0;
575 
576 	switch (a->scheduler) {
577 #ifdef ALTQ_CBQ
578 	case ALTQT_CBQ:
579 		error = cbq_add_queue(a);
580 		break;
581 #endif
582 #ifdef ALTQ_PRIQ
583 	case ALTQT_PRIQ:
584 		error = priq_add_queue(a);
585 		break;
586 #endif
587 #ifdef ALTQ_HFSC
588 	case ALTQT_HFSC:
589 		error = hfsc_add_queue(a);
590 		break;
591 #endif
592 #ifdef ALTQ_FAIRQ
593 	case ALTQT_FAIRQ:
594 		error = fairq_add_queue(a);
595 		break;
596 #endif
597 	default:
598 		error = ENXIO;
599 	}
600 
601 	return (error);
602 }
603 
604 /*
605  * remove a queue from the discipline
606  */
607 int
608 altq_remove_queue(struct pf_altq *a)
609 {
610 	int error = 0;
611 
612 	switch (a->scheduler) {
613 #ifdef ALTQ_CBQ
614 	case ALTQT_CBQ:
615 		error = cbq_remove_queue(a);
616 		break;
617 #endif
618 #ifdef ALTQ_PRIQ
619 	case ALTQT_PRIQ:
620 		error = priq_remove_queue(a);
621 		break;
622 #endif
623 #ifdef ALTQ_HFSC
624 	case ALTQT_HFSC:
625 		error = hfsc_remove_queue(a);
626 		break;
627 #endif
628 #ifdef ALTQ_FAIRQ
629 	case ALTQT_FAIRQ:
630 		error = fairq_remove_queue(a);
631 		break;
632 #endif
633 	default:
634 		error = ENXIO;
635 	}
636 
637 	return (error);
638 }
639 
640 /*
641  * get queue statistics
642  */
643 int
644 altq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
645 {
646 	int error = 0;
647 
648 	switch (a->scheduler) {
649 #ifdef ALTQ_CBQ
650 	case ALTQT_CBQ:
651 		error = cbq_getqstats(a, ubuf, nbytes);
652 		break;
653 #endif
654 #ifdef ALTQ_PRIQ
655 	case ALTQT_PRIQ:
656 		error = priq_getqstats(a, ubuf, nbytes);
657 		break;
658 #endif
659 #ifdef ALTQ_HFSC
660 	case ALTQT_HFSC:
661 		error = hfsc_getqstats(a, ubuf, nbytes);
662 		break;
663 #endif
664 #ifdef ALTQ_FAIRQ
665 	case ALTQT_FAIRQ:
666 		error = fairq_getqstats(a, ubuf, nbytes);
667 		break;
668 #endif
669 	default:
670 		error = ENXIO;
671 	}
672 
673 	return (error);
674 }
675 
676 /*
677  * read and write diffserv field in IPv4 or IPv6 header
678  */
679 uint8_t
680 read_dsfield(struct mbuf *m, struct altq_pktattr *pktattr)
681 {
682 	struct mbuf *m0;
683 	uint8_t ds_field = 0;
684 
685 	if (pktattr == NULL ||
686 	    (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
687 		return ((uint8_t)0);
688 
689 	/* verify that pattr_hdr is within the mbuf data */
690 	for (m0 = m; m0 != NULL; m0 = m0->m_next) {
691 		if ((pktattr->pattr_hdr >= m0->m_data) &&
692 		    (pktattr->pattr_hdr < m0->m_data + m0->m_len))
693 			break;
694 	}
695 	if (m0 == NULL) {
696 		/* ick, pattr_hdr is stale */
697 		pktattr->pattr_af = AF_UNSPEC;
698 #ifdef ALTQ_DEBUG
699 		kprintf("read_dsfield: can't locate header!\n");
700 #endif
701 		return ((uint8_t)0);
702 	}
703 
704 	if (pktattr->pattr_af == AF_INET) {
705 		struct ip *ip = (struct ip *)pktattr->pattr_hdr;
706 
707 		if (ip->ip_v != 4)
708 			return ((uint8_t)0);	/* version mismatch! */
709 		ds_field = ip->ip_tos;
710 	}
711 #ifdef INET6
712 	else if (pktattr->pattr_af == AF_INET6) {
713 		struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
714 		uint32_t flowlabel;
715 
716 		flowlabel = ntohl(ip6->ip6_flow);
717 		if ((flowlabel >> 28) != 6)
718 			return ((uint8_t)0);	/* version mismatch! */
719 		ds_field = (flowlabel >> 20) & 0xff;
720 	}
721 #endif
722 	return (ds_field);
723 }
724 
725 void
726 write_dsfield(struct mbuf *m, struct altq_pktattr *pktattr, uint8_t dsfield)
727 {
728 	struct mbuf *m0;
729 
730 	if (pktattr == NULL ||
731 	    (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
732 		return;
733 
734 	/* verify that pattr_hdr is within the mbuf data */
735 	for (m0 = m; m0 != NULL; m0 = m0->m_next) {
736 		if ((pktattr->pattr_hdr >= m0->m_data) &&
737 		    (pktattr->pattr_hdr < m0->m_data + m0->m_len))
738 			break;
739 	}
740 	if (m0 == NULL) {
741 		/* ick, pattr_hdr is stale */
742 		pktattr->pattr_af = AF_UNSPEC;
743 #ifdef ALTQ_DEBUG
744 		kprintf("write_dsfield: can't locate header!\n");
745 #endif
746 		return;
747 	}
748 
749 	if (pktattr->pattr_af == AF_INET) {
750 		struct ip *ip = (struct ip *)pktattr->pattr_hdr;
751 		uint8_t old;
752 		int32_t sum;
753 
754 		if (ip->ip_v != 4)
755 			return;		/* version mismatch! */
756 		old = ip->ip_tos;
757 		dsfield |= old & 3;	/* leave CU bits */
758 		if (old == dsfield)
759 			return;
760 		ip->ip_tos = dsfield;
761 		/*
762 		 * update checksum (from RFC1624)
763 		 *	   HC' = ~(~HC + ~m + m')
764 		 */
765 		sum = ~ntohs(ip->ip_sum) & 0xffff;
766 		sum += 0xff00 + (~old & 0xff) + dsfield;
767 		sum = (sum >> 16) + (sum & 0xffff);
768 		sum += (sum >> 16);  /* add carry */
769 
770 		ip->ip_sum = htons(~sum & 0xffff);
771 	}
772 #ifdef INET6
773 	else if (pktattr->pattr_af == AF_INET6) {
774 		struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
775 		uint32_t flowlabel;
776 
777 		flowlabel = ntohl(ip6->ip6_flow);
778 		if ((flowlabel >> 28) != 6)
779 			return;		/* version mismatch! */
780 		flowlabel = (flowlabel & 0xf03fffff) | (dsfield << 20);
781 		ip6->ip6_flow = htonl(flowlabel);
782 	}
783 #endif
784 }
785 
786 /*
787  * high resolution clock support taking advantage of a machine dependent
788  * high resolution time counter (e.g., timestamp counter of intel pentium).
789  * we assume
790  *  - 64-bit-long monotonically-increasing counter
791  *  - frequency range is 100M-4GHz (CPU speed)
792  */
793 /* if pcc is not available or disabled, emulate 256MHz using microtime() */
794 #define	MACHCLK_SHIFT	8
795 
796 static int machclk_usepcc;
797 uint64_t machclk_freq = 0;
798 uint32_t machclk_per_tick = 0;
799 
800 void
801 init_machclk(void)
802 {
803 	callout_init(&tbr_callout);
804 
805 #ifdef ALTQ_NOPCC
806 	machclk_usepcc = 0;
807 #else
808 	machclk_usepcc = 1;
809 #endif
810 
811 #if defined(__i386__) || defined(__x86_64__)
812 	if (!tsc_mpsync)
813 		machclk_usepcc = 0;
814 #else
815 	machclk_usepcc = 0;
816 #endif
817 
818 	if (!machclk_usepcc) {
819 		/* emulate 256MHz using microtime() */
820 		machclk_freq = 1000000LLU << MACHCLK_SHIFT;
821 		machclk_per_tick = machclk_freq / hz;
822 #ifdef ALTQ_DEBUG
823 		kprintf("altq: emulate %juHz cpu clock\n",
824 		    (uintmax_t)machclk_freq);
825 #endif
826 		return;
827 	}
828 
829 	/*
830 	 * If the clock frequency (of Pentium TSC) is accessible,
831 	 * just use it.
832 	 */
833 #ifdef _RDTSC_SUPPORTED_
834 	if (tsc_present)
835 		machclk_freq = (uint64_t)tsc_frequency;
836 #endif
837 
838 	/*
839 	 * If we don't know the clock frequency, measure it.
840 	 */
841 	if (machclk_freq == 0) {
842 		static int	wait;
843 		struct timeval	tv_start, tv_end;
844 		uint64_t	start, end, diff;
845 		int		timo;
846 
847 		microtime(&tv_start);
848 		start = read_machclk();
849 		timo = hz;	/* 1 sec */
850 		tsleep(&wait, PCATCH, "init_machclk", timo);
851 		microtime(&tv_end);
852 		end = read_machclk();
853 		diff = (uint64_t)(tv_end.tv_sec - tv_start.tv_sec) * 1000000
854 		    + tv_end.tv_usec - tv_start.tv_usec;
855 		if (diff != 0)
856 			machclk_freq = (end - start) * 1000000 / diff;
857 	}
858 
859 	machclk_per_tick = machclk_freq / hz;
860 
861 #ifdef ALTQ_DEBUG
862 	kprintf("altq: CPU clock: %juHz\n", (uintmax_t)machclk_freq);
863 #endif
864 }
865 
866 uint64_t
867 read_machclk(void)
868 {
869 	uint64_t val;
870 
871 	if (machclk_usepcc) {
872 #ifdef _RDTSC_SUPPORTED_
873 		val = rdtsc();
874 #else
875 		panic("read_machclk");
876 #endif
877 	} else {
878 		struct timeval tv;
879 
880 		microtime(&tv);
881 		val = (((uint64_t)(tv.tv_sec - boottime.tv_sec) * 1000000
882 		    + tv.tv_usec) << MACHCLK_SHIFT);
883 	}
884 	return (val);
885 }
886