1 /*- 2 * Copyright (c) Sun Microsystems, Inc. 1993-1998 All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the SMCC Technology 18 * Development Group at Sun Microsystems, Inc. 19 * 20 * 4. The name of the Sun Microsystems, Inc nor may not be used to endorse or 21 * promote products derived from this software without specific prior 22 * written permission. 23 * 24 * SUN MICROSYSTEMS DOES NOT CLAIM MERCHANTABILITY OF THIS SOFTWARE OR THE 25 * SUITABILITY OF THIS SOFTWARE FOR ANY PARTICULAR PURPOSE. The software is 26 * provided "as is" without express or implied warranty of any kind. 27 * 28 * These notices must be retained in any copies of any part of this software. 29 * 30 * $KAME: altq_cbq.c,v 1.19 2003/09/17 14:23:25 kjc Exp $ 31 * $FreeBSD$ 32 */ 33 34 #include "opt_altq.h" 35 #include "opt_inet.h" 36 #include "opt_inet6.h" 37 #ifdef ALTQ_CBQ /* cbq is enabled by ALTQ_CBQ option in opt_altq.h */ 38 39 #include <sys/param.h> 40 #include <sys/malloc.h> 41 #include <sys/mbuf.h> 42 #include <sys/socket.h> 43 #include <sys/systm.h> 44 #include <sys/proc.h> 45 #include <sys/errno.h> 46 #include <sys/time.h> 47 #ifdef ALTQ3_COMPAT 48 #include <sys/uio.h> 49 #include <sys/kernel.h> 50 #endif 51 52 #include <net/if.h> 53 #include <net/if_var.h> 54 #include <netinet/in.h> 55 56 #include <netpfil/pf/pf.h> 57 #include <netpfil/pf/pf_altq.h> 58 #include <netpfil/pf/pf_mtag.h> 59 #include <net/altq/altq.h> 60 #include <net/altq/altq_cbq.h> 61 #ifdef ALTQ3_COMPAT 62 #include <net/altq/altq_conf.h> 63 #endif 64 65 #ifdef ALTQ3_COMPAT 66 /* 67 * Local Data structures. 68 */ 69 static cbq_state_t *cbq_list = NULL; 70 #endif 71 72 /* 73 * Forward Declarations. 74 */ 75 static int cbq_class_destroy(cbq_state_t *, struct rm_class *); 76 static struct rm_class *clh_to_clp(cbq_state_t *, u_int32_t); 77 static int cbq_clear_interface(cbq_state_t *); 78 static int cbq_request(struct ifaltq *, int, void *); 79 static int cbq_enqueue(struct ifaltq *, struct mbuf *, 80 struct altq_pktattr *); 81 static struct mbuf *cbq_dequeue(struct ifaltq *, int); 82 static void cbqrestart(struct ifaltq *); 83 static void get_class_stats(class_stats_t *, struct rm_class *); 84 static void cbq_purge(cbq_state_t *); 85 #ifdef ALTQ3_COMPAT 86 static int cbq_add_class(struct cbq_add_class *); 87 static int cbq_delete_class(struct cbq_delete_class *); 88 static int cbq_modify_class(struct cbq_modify_class *); 89 static int cbq_class_create(cbq_state_t *, struct cbq_add_class *, 90 struct rm_class *, struct rm_class *); 91 static int cbq_clear_hierarchy(struct cbq_interface *); 92 static int cbq_set_enable(struct cbq_interface *, int); 93 static int cbq_ifattach(struct cbq_interface *); 94 static int cbq_ifdetach(struct cbq_interface *); 95 static int cbq_getstats(struct cbq_getstats *); 96 97 static int cbq_add_filter(struct cbq_add_filter *); 98 static int cbq_delete_filter(struct cbq_delete_filter *); 99 #endif /* ALTQ3_COMPAT */ 100 101 /* 102 * int 103 * cbq_class_destroy(cbq_mod_state_t *, struct rm_class *) - This 104 * function destroys a given traffic class. Before destroying 105 * the class, all traffic for that class is released. 106 */ 107 static int 108 cbq_class_destroy(cbq_state_t *cbqp, struct rm_class *cl) 109 { 110 int i; 111 112 /* delete the class */ 113 rmc_delete_class(&cbqp->ifnp, cl); 114 115 /* 116 * free the class handle 117 */ 118 for (i = 0; i < CBQ_MAX_CLASSES; i++) 119 if (cbqp->cbq_class_tbl[i] == cl) 120 cbqp->cbq_class_tbl[i] = NULL; 121 122 if (cl == cbqp->ifnp.root_) 123 cbqp->ifnp.root_ = NULL; 124 if (cl == cbqp->ifnp.default_) 125 cbqp->ifnp.default_ = NULL; 126 #ifdef ALTQ3_COMPAT 127 if (cl == cbqp->ifnp.ctl_) 128 cbqp->ifnp.ctl_ = NULL; 129 #endif 130 return (0); 131 } 132 133 /* convert class handle to class pointer */ 134 static struct rm_class * 135 clh_to_clp(cbq_state_t *cbqp, u_int32_t chandle) 136 { 137 int i; 138 struct rm_class *cl; 139 140 if (chandle == 0) 141 return (NULL); 142 /* 143 * first, try optimistically the slot matching the lower bits of 144 * the handle. if it fails, do the linear table search. 145 */ 146 i = chandle % CBQ_MAX_CLASSES; 147 if ((cl = cbqp->cbq_class_tbl[i]) != NULL && 148 cl->stats_.handle == chandle) 149 return (cl); 150 for (i = 0; i < CBQ_MAX_CLASSES; i++) 151 if ((cl = cbqp->cbq_class_tbl[i]) != NULL && 152 cl->stats_.handle == chandle) 153 return (cl); 154 return (NULL); 155 } 156 157 static int 158 cbq_clear_interface(cbq_state_t *cbqp) 159 { 160 int again, i; 161 struct rm_class *cl; 162 163 #ifdef ALTQ3_CLFIER_COMPAT 164 /* free the filters for this interface */ 165 acc_discard_filters(&cbqp->cbq_classifier, NULL, 1); 166 #endif 167 168 /* clear out the classes now */ 169 do { 170 again = 0; 171 for (i = 0; i < CBQ_MAX_CLASSES; i++) { 172 if ((cl = cbqp->cbq_class_tbl[i]) != NULL) { 173 if (is_a_parent_class(cl)) 174 again++; 175 else { 176 cbq_class_destroy(cbqp, cl); 177 cbqp->cbq_class_tbl[i] = NULL; 178 if (cl == cbqp->ifnp.root_) 179 cbqp->ifnp.root_ = NULL; 180 if (cl == cbqp->ifnp.default_) 181 cbqp->ifnp.default_ = NULL; 182 #ifdef ALTQ3_COMPAT 183 if (cl == cbqp->ifnp.ctl_) 184 cbqp->ifnp.ctl_ = NULL; 185 #endif 186 } 187 } 188 } 189 } while (again); 190 191 return (0); 192 } 193 194 static int 195 cbq_request(struct ifaltq *ifq, int req, void *arg) 196 { 197 cbq_state_t *cbqp = (cbq_state_t *)ifq->altq_disc; 198 199 IFQ_LOCK_ASSERT(ifq); 200 201 switch (req) { 202 case ALTRQ_PURGE: 203 cbq_purge(cbqp); 204 break; 205 } 206 return (0); 207 } 208 209 /* copy the stats info in rm_class to class_states_t */ 210 static void 211 get_class_stats(class_stats_t *statsp, struct rm_class *cl) 212 { 213 statsp->xmit_cnt = cl->stats_.xmit_cnt; 214 statsp->drop_cnt = cl->stats_.drop_cnt; 215 statsp->over = cl->stats_.over; 216 statsp->borrows = cl->stats_.borrows; 217 statsp->overactions = cl->stats_.overactions; 218 statsp->delays = cl->stats_.delays; 219 220 statsp->depth = cl->depth_; 221 statsp->priority = cl->pri_; 222 statsp->maxidle = cl->maxidle_; 223 statsp->minidle = cl->minidle_; 224 statsp->offtime = cl->offtime_; 225 statsp->qmax = qlimit(cl->q_); 226 statsp->ns_per_byte = cl->ns_per_byte_; 227 statsp->wrr_allot = cl->w_allotment_; 228 statsp->qcnt = qlen(cl->q_); 229 statsp->avgidle = cl->avgidle_; 230 231 statsp->qtype = qtype(cl->q_); 232 #ifdef ALTQ_RED 233 if (q_is_red(cl->q_)) 234 red_getstats(cl->red_, &statsp->red[0]); 235 #endif 236 #ifdef ALTQ_RIO 237 if (q_is_rio(cl->q_)) 238 rio_getstats((rio_t *)cl->red_, &statsp->red[0]); 239 #endif 240 #ifdef ALTQ_CODEL 241 if (q_is_codel(cl->q_)) 242 codel_getstats(cl->codel_, &statsp->codel); 243 #endif 244 } 245 246 int 247 cbq_pfattach(struct pf_altq *a) 248 { 249 struct ifnet *ifp; 250 int s, error; 251 252 if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL) 253 return (EINVAL); 254 s = splnet(); 255 error = altq_attach(&ifp->if_snd, ALTQT_CBQ, a->altq_disc, 256 cbq_enqueue, cbq_dequeue, cbq_request, NULL, NULL); 257 splx(s); 258 return (error); 259 } 260 261 int 262 cbq_add_altq(struct pf_altq *a) 263 { 264 cbq_state_t *cbqp; 265 struct ifnet *ifp; 266 267 if ((ifp = ifunit(a->ifname)) == NULL) 268 return (EINVAL); 269 if (!ALTQ_IS_READY(&ifp->if_snd)) 270 return (ENODEV); 271 272 /* allocate and initialize cbq_state_t */ 273 cbqp = malloc(sizeof(cbq_state_t), M_DEVBUF, M_NOWAIT | M_ZERO); 274 if (cbqp == NULL) 275 return (ENOMEM); 276 CALLOUT_INIT(&cbqp->cbq_callout); 277 cbqp->cbq_qlen = 0; 278 cbqp->ifnp.ifq_ = &ifp->if_snd; /* keep the ifq */ 279 280 /* keep the state in pf_altq */ 281 a->altq_disc = cbqp; 282 283 return (0); 284 } 285 286 int 287 cbq_remove_altq(struct pf_altq *a) 288 { 289 cbq_state_t *cbqp; 290 291 if ((cbqp = a->altq_disc) == NULL) 292 return (EINVAL); 293 a->altq_disc = NULL; 294 295 cbq_clear_interface(cbqp); 296 297 if (cbqp->ifnp.default_) 298 cbq_class_destroy(cbqp, cbqp->ifnp.default_); 299 if (cbqp->ifnp.root_) 300 cbq_class_destroy(cbqp, cbqp->ifnp.root_); 301 302 /* deallocate cbq_state_t */ 303 free(cbqp, M_DEVBUF); 304 305 return (0); 306 } 307 308 int 309 cbq_add_queue(struct pf_altq *a) 310 { 311 struct rm_class *borrow, *parent; 312 cbq_state_t *cbqp; 313 struct rm_class *cl; 314 struct cbq_opts *opts; 315 int i; 316 317 if ((cbqp = a->altq_disc) == NULL) 318 return (EINVAL); 319 if (a->qid == 0) 320 return (EINVAL); 321 322 /* 323 * find a free slot in the class table. if the slot matching 324 * the lower bits of qid is free, use this slot. otherwise, 325 * use the first free slot. 326 */ 327 i = a->qid % CBQ_MAX_CLASSES; 328 if (cbqp->cbq_class_tbl[i] != NULL) { 329 for (i = 0; i < CBQ_MAX_CLASSES; i++) 330 if (cbqp->cbq_class_tbl[i] == NULL) 331 break; 332 if (i == CBQ_MAX_CLASSES) 333 return (EINVAL); 334 } 335 336 opts = &a->pq_u.cbq_opts; 337 /* check parameters */ 338 if (a->priority >= CBQ_MAXPRI) 339 return (EINVAL); 340 341 /* Get pointers to parent and borrow classes. */ 342 parent = clh_to_clp(cbqp, a->parent_qid); 343 if (opts->flags & CBQCLF_BORROW) 344 borrow = parent; 345 else 346 borrow = NULL; 347 348 /* 349 * A class must borrow from it's parent or it can not 350 * borrow at all. Hence, borrow can be null. 351 */ 352 if (parent == NULL && (opts->flags & CBQCLF_ROOTCLASS) == 0) { 353 printf("cbq_add_queue: no parent class!\n"); 354 return (EINVAL); 355 } 356 357 if ((borrow != parent) && (borrow != NULL)) { 358 printf("cbq_add_class: borrow class != parent\n"); 359 return (EINVAL); 360 } 361 362 /* 363 * check parameters 364 */ 365 switch (opts->flags & CBQCLF_CLASSMASK) { 366 case CBQCLF_ROOTCLASS: 367 if (parent != NULL) 368 return (EINVAL); 369 if (cbqp->ifnp.root_) 370 return (EINVAL); 371 break; 372 case CBQCLF_DEFCLASS: 373 if (cbqp->ifnp.default_) 374 return (EINVAL); 375 break; 376 case 0: 377 if (a->qid == 0) 378 return (EINVAL); 379 break; 380 default: 381 /* more than two flags bits set */ 382 return (EINVAL); 383 } 384 385 /* 386 * create a class. if this is a root class, initialize the 387 * interface. 388 */ 389 if ((opts->flags & CBQCLF_CLASSMASK) == CBQCLF_ROOTCLASS) { 390 rmc_init(cbqp->ifnp.ifq_, &cbqp->ifnp, opts->ns_per_byte, 391 cbqrestart, a->qlimit, RM_MAXQUEUED, 392 opts->maxidle, opts->minidle, opts->offtime, 393 opts->flags); 394 cl = cbqp->ifnp.root_; 395 } else { 396 cl = rmc_newclass(a->priority, 397 &cbqp->ifnp, opts->ns_per_byte, 398 rmc_delay_action, a->qlimit, parent, borrow, 399 opts->maxidle, opts->minidle, opts->offtime, 400 opts->pktsize, opts->flags); 401 } 402 if (cl == NULL) 403 return (ENOMEM); 404 405 /* return handle to user space. */ 406 cl->stats_.handle = a->qid; 407 cl->stats_.depth = cl->depth_; 408 409 /* save the allocated class */ 410 cbqp->cbq_class_tbl[i] = cl; 411 412 if ((opts->flags & CBQCLF_CLASSMASK) == CBQCLF_DEFCLASS) 413 cbqp->ifnp.default_ = cl; 414 415 return (0); 416 } 417 418 int 419 cbq_remove_queue(struct pf_altq *a) 420 { 421 struct rm_class *cl; 422 cbq_state_t *cbqp; 423 int i; 424 425 if ((cbqp = a->altq_disc) == NULL) 426 return (EINVAL); 427 428 if ((cl = clh_to_clp(cbqp, a->qid)) == NULL) 429 return (EINVAL); 430 431 /* if we are a parent class, then return an error. */ 432 if (is_a_parent_class(cl)) 433 return (EINVAL); 434 435 /* delete the class */ 436 rmc_delete_class(&cbqp->ifnp, cl); 437 438 /* 439 * free the class handle 440 */ 441 for (i = 0; i < CBQ_MAX_CLASSES; i++) 442 if (cbqp->cbq_class_tbl[i] == cl) { 443 cbqp->cbq_class_tbl[i] = NULL; 444 if (cl == cbqp->ifnp.root_) 445 cbqp->ifnp.root_ = NULL; 446 if (cl == cbqp->ifnp.default_) 447 cbqp->ifnp.default_ = NULL; 448 break; 449 } 450 451 return (0); 452 } 453 454 int 455 cbq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes) 456 { 457 cbq_state_t *cbqp; 458 struct rm_class *cl; 459 class_stats_t stats; 460 int error = 0; 461 462 if ((cbqp = altq_lookup(a->ifname, ALTQT_CBQ)) == NULL) 463 return (EBADF); 464 465 if ((cl = clh_to_clp(cbqp, a->qid)) == NULL) 466 return (EINVAL); 467 468 if (*nbytes < sizeof(stats)) 469 return (EINVAL); 470 471 get_class_stats(&stats, cl); 472 473 if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0) 474 return (error); 475 *nbytes = sizeof(stats); 476 return (0); 477 } 478 479 /* 480 * int 481 * cbq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pattr) 482 * - Queue data packets. 483 * 484 * cbq_enqueue is set to ifp->if_altqenqueue and called by an upper 485 * layer (e.g. ether_output). cbq_enqueue queues the given packet 486 * to the cbq, then invokes the driver's start routine. 487 * 488 * Assumptions: called in splimp 489 * Returns: 0 if the queueing is successful. 490 * ENOBUFS if a packet dropping occurred as a result of 491 * the queueing. 492 */ 493 494 static int 495 cbq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr) 496 { 497 cbq_state_t *cbqp = (cbq_state_t *)ifq->altq_disc; 498 struct rm_class *cl; 499 struct pf_mtag *t; 500 int len; 501 502 IFQ_LOCK_ASSERT(ifq); 503 504 /* grab class set by classifier */ 505 if ((m->m_flags & M_PKTHDR) == 0) { 506 /* should not happen */ 507 printf("altq: packet for %s does not have pkthdr\n", 508 ifq->altq_ifp->if_xname); 509 m_freem(m); 510 return (ENOBUFS); 511 } 512 cl = NULL; 513 if ((t = pf_find_mtag(m)) != NULL) 514 cl = clh_to_clp(cbqp, t->qid); 515 #ifdef ALTQ3_COMPAT 516 else if ((ifq->altq_flags & ALTQF_CLASSIFY) && pktattr != NULL) 517 cl = pktattr->pattr_class; 518 #endif 519 if (cl == NULL) { 520 cl = cbqp->ifnp.default_; 521 if (cl == NULL) { 522 m_freem(m); 523 return (ENOBUFS); 524 } 525 } 526 #ifdef ALTQ3_COMPAT 527 if (pktattr != NULL) 528 cl->pktattr_ = pktattr; /* save proto hdr used by ECN */ 529 else 530 #endif 531 cl->pktattr_ = NULL; 532 len = m_pktlen(m); 533 if (rmc_queue_packet(cl, m) != 0) { 534 /* drop occurred. some mbuf was freed in rmc_queue_packet. */ 535 PKTCNTR_ADD(&cl->stats_.drop_cnt, len); 536 return (ENOBUFS); 537 } 538 539 /* successfully queued. */ 540 ++cbqp->cbq_qlen; 541 IFQ_INC_LEN(ifq); 542 return (0); 543 } 544 545 static struct mbuf * 546 cbq_dequeue(struct ifaltq *ifq, int op) 547 { 548 cbq_state_t *cbqp = (cbq_state_t *)ifq->altq_disc; 549 struct mbuf *m; 550 551 IFQ_LOCK_ASSERT(ifq); 552 553 m = rmc_dequeue_next(&cbqp->ifnp, op); 554 555 if (m && op == ALTDQ_REMOVE) { 556 --cbqp->cbq_qlen; /* decrement # of packets in cbq */ 557 IFQ_DEC_LEN(ifq); 558 559 /* Update the class. */ 560 rmc_update_class_util(&cbqp->ifnp); 561 } 562 return (m); 563 } 564 565 /* 566 * void 567 * cbqrestart(queue_t *) - Restart sending of data. 568 * called from rmc_restart in splimp via timeout after waking up 569 * a suspended class. 570 * Returns: NONE 571 */ 572 573 static void 574 cbqrestart(struct ifaltq *ifq) 575 { 576 cbq_state_t *cbqp; 577 struct ifnet *ifp; 578 579 IFQ_LOCK_ASSERT(ifq); 580 581 if (!ALTQ_IS_ENABLED(ifq)) 582 /* cbq must have been detached */ 583 return; 584 585 if ((cbqp = (cbq_state_t *)ifq->altq_disc) == NULL) 586 /* should not happen */ 587 return; 588 589 ifp = ifq->altq_ifp; 590 if (ifp->if_start && 591 cbqp->cbq_qlen > 0 && (ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0) { 592 IFQ_UNLOCK(ifq); 593 (*ifp->if_start)(ifp); 594 IFQ_LOCK(ifq); 595 } 596 } 597 598 static void cbq_purge(cbq_state_t *cbqp) 599 { 600 struct rm_class *cl; 601 int i; 602 603 for (i = 0; i < CBQ_MAX_CLASSES; i++) 604 if ((cl = cbqp->cbq_class_tbl[i]) != NULL) 605 rmc_dropall(cl); 606 if (ALTQ_IS_ENABLED(cbqp->ifnp.ifq_)) 607 cbqp->ifnp.ifq_->ifq_len = 0; 608 } 609 #ifdef ALTQ3_COMPAT 610 611 static int 612 cbq_add_class(acp) 613 struct cbq_add_class *acp; 614 { 615 char *ifacename; 616 struct rm_class *borrow, *parent; 617 cbq_state_t *cbqp; 618 619 ifacename = acp->cbq_iface.cbq_ifacename; 620 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL) 621 return (EBADF); 622 623 /* check parameters */ 624 if (acp->cbq_class.priority >= CBQ_MAXPRI || 625 acp->cbq_class.maxq > CBQ_MAXQSIZE) 626 return (EINVAL); 627 628 /* Get pointers to parent and borrow classes. */ 629 parent = clh_to_clp(cbqp, acp->cbq_class.parent_class_handle); 630 borrow = clh_to_clp(cbqp, acp->cbq_class.borrow_class_handle); 631 632 /* 633 * A class must borrow from it's parent or it can not 634 * borrow at all. Hence, borrow can be null. 635 */ 636 if (parent == NULL && (acp->cbq_class.flags & CBQCLF_ROOTCLASS) == 0) { 637 printf("cbq_add_class: no parent class!\n"); 638 return (EINVAL); 639 } 640 641 if ((borrow != parent) && (borrow != NULL)) { 642 printf("cbq_add_class: borrow class != parent\n"); 643 return (EINVAL); 644 } 645 646 return cbq_class_create(cbqp, acp, parent, borrow); 647 } 648 649 static int 650 cbq_delete_class(dcp) 651 struct cbq_delete_class *dcp; 652 { 653 char *ifacename; 654 struct rm_class *cl; 655 cbq_state_t *cbqp; 656 657 ifacename = dcp->cbq_iface.cbq_ifacename; 658 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL) 659 return (EBADF); 660 661 if ((cl = clh_to_clp(cbqp, dcp->cbq_class_handle)) == NULL) 662 return (EINVAL); 663 664 /* if we are a parent class, then return an error. */ 665 if (is_a_parent_class(cl)) 666 return (EINVAL); 667 668 /* if a filter has a reference to this class delete the filter */ 669 acc_discard_filters(&cbqp->cbq_classifier, cl, 0); 670 671 return cbq_class_destroy(cbqp, cl); 672 } 673 674 static int 675 cbq_modify_class(acp) 676 struct cbq_modify_class *acp; 677 { 678 char *ifacename; 679 struct rm_class *cl; 680 cbq_state_t *cbqp; 681 682 ifacename = acp->cbq_iface.cbq_ifacename; 683 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL) 684 return (EBADF); 685 686 /* Get pointer to this class */ 687 if ((cl = clh_to_clp(cbqp, acp->cbq_class_handle)) == NULL) 688 return (EINVAL); 689 690 if (rmc_modclass(cl, acp->cbq_class.nano_sec_per_byte, 691 acp->cbq_class.maxq, acp->cbq_class.maxidle, 692 acp->cbq_class.minidle, acp->cbq_class.offtime, 693 acp->cbq_class.pktsize) < 0) 694 return (EINVAL); 695 return (0); 696 } 697 698 /* 699 * struct rm_class * 700 * cbq_class_create(cbq_mod_state_t *cbqp, struct cbq_add_class *acp, 701 * struct rm_class *parent, struct rm_class *borrow) 702 * 703 * This function create a new traffic class in the CBQ class hierarchy of 704 * given parameters. The class that created is either the root, default, 705 * or a new dynamic class. If CBQ is not initilaized, the the root class 706 * will be created. 707 */ 708 static int 709 cbq_class_create(cbqp, acp, parent, borrow) 710 cbq_state_t *cbqp; 711 struct cbq_add_class *acp; 712 struct rm_class *parent, *borrow; 713 { 714 struct rm_class *cl; 715 cbq_class_spec_t *spec = &acp->cbq_class; 716 u_int32_t chandle; 717 int i; 718 719 /* 720 * allocate class handle 721 */ 722 for (i = 1; i < CBQ_MAX_CLASSES; i++) 723 if (cbqp->cbq_class_tbl[i] == NULL) 724 break; 725 if (i == CBQ_MAX_CLASSES) 726 return (EINVAL); 727 chandle = i; /* use the slot number as class handle */ 728 729 /* 730 * create a class. if this is a root class, initialize the 731 * interface. 732 */ 733 if ((spec->flags & CBQCLF_CLASSMASK) == CBQCLF_ROOTCLASS) { 734 rmc_init(cbqp->ifnp.ifq_, &cbqp->ifnp, spec->nano_sec_per_byte, 735 cbqrestart, spec->maxq, RM_MAXQUEUED, 736 spec->maxidle, spec->minidle, spec->offtime, 737 spec->flags); 738 cl = cbqp->ifnp.root_; 739 } else { 740 cl = rmc_newclass(spec->priority, 741 &cbqp->ifnp, spec->nano_sec_per_byte, 742 rmc_delay_action, spec->maxq, parent, borrow, 743 spec->maxidle, spec->minidle, spec->offtime, 744 spec->pktsize, spec->flags); 745 } 746 if (cl == NULL) 747 return (ENOMEM); 748 749 /* return handle to user space. */ 750 acp->cbq_class_handle = chandle; 751 752 cl->stats_.handle = chandle; 753 cl->stats_.depth = cl->depth_; 754 755 /* save the allocated class */ 756 cbqp->cbq_class_tbl[i] = cl; 757 758 if ((spec->flags & CBQCLF_CLASSMASK) == CBQCLF_DEFCLASS) 759 cbqp->ifnp.default_ = cl; 760 if ((spec->flags & CBQCLF_CLASSMASK) == CBQCLF_CTLCLASS) 761 cbqp->ifnp.ctl_ = cl; 762 763 return (0); 764 } 765 766 static int 767 cbq_add_filter(afp) 768 struct cbq_add_filter *afp; 769 { 770 char *ifacename; 771 cbq_state_t *cbqp; 772 struct rm_class *cl; 773 774 ifacename = afp->cbq_iface.cbq_ifacename; 775 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL) 776 return (EBADF); 777 778 /* Get the pointer to class. */ 779 if ((cl = clh_to_clp(cbqp, afp->cbq_class_handle)) == NULL) 780 return (EINVAL); 781 782 return acc_add_filter(&cbqp->cbq_classifier, &afp->cbq_filter, 783 cl, &afp->cbq_filter_handle); 784 } 785 786 static int 787 cbq_delete_filter(dfp) 788 struct cbq_delete_filter *dfp; 789 { 790 char *ifacename; 791 cbq_state_t *cbqp; 792 793 ifacename = dfp->cbq_iface.cbq_ifacename; 794 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL) 795 return (EBADF); 796 797 return acc_delete_filter(&cbqp->cbq_classifier, 798 dfp->cbq_filter_handle); 799 } 800 801 /* 802 * cbq_clear_hierarchy deletes all classes and their filters on the 803 * given interface. 804 */ 805 static int 806 cbq_clear_hierarchy(ifacep) 807 struct cbq_interface *ifacep; 808 { 809 char *ifacename; 810 cbq_state_t *cbqp; 811 812 ifacename = ifacep->cbq_ifacename; 813 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL) 814 return (EBADF); 815 816 return cbq_clear_interface(cbqp); 817 } 818 819 /* 820 * static int 821 * cbq_set_enable(struct cbq_enable *ep) - this function processed the 822 * ioctl request to enable class based queueing. It searches the list 823 * of interfaces for the specified interface and then enables CBQ on 824 * that interface. 825 * 826 * Returns: 0, for no error. 827 * EBADF, for specified inteface not found. 828 */ 829 830 static int 831 cbq_set_enable(ep, enable) 832 struct cbq_interface *ep; 833 int enable; 834 { 835 int error = 0; 836 cbq_state_t *cbqp; 837 char *ifacename; 838 839 ifacename = ep->cbq_ifacename; 840 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL) 841 return (EBADF); 842 843 switch (enable) { 844 case ENABLE: 845 if (cbqp->ifnp.root_ == NULL || cbqp->ifnp.default_ == NULL || 846 cbqp->ifnp.ctl_ == NULL) { 847 if (cbqp->ifnp.root_ == NULL) 848 printf("No Root Class for %s\n", ifacename); 849 if (cbqp->ifnp.default_ == NULL) 850 printf("No Default Class for %s\n", ifacename); 851 if (cbqp->ifnp.ctl_ == NULL) 852 printf("No Control Class for %s\n", ifacename); 853 error = EINVAL; 854 } else if ((error = altq_enable(cbqp->ifnp.ifq_)) == 0) { 855 cbqp->cbq_qlen = 0; 856 } 857 break; 858 859 case DISABLE: 860 error = altq_disable(cbqp->ifnp.ifq_); 861 break; 862 } 863 return (error); 864 } 865 866 static int 867 cbq_getstats(gsp) 868 struct cbq_getstats *gsp; 869 { 870 char *ifacename; 871 int i, n, nclasses; 872 cbq_state_t *cbqp; 873 struct rm_class *cl; 874 class_stats_t stats, *usp; 875 int error = 0; 876 877 ifacename = gsp->iface.cbq_ifacename; 878 nclasses = gsp->nclasses; 879 usp = gsp->stats; 880 881 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL) 882 return (EBADF); 883 if (nclasses <= 0) 884 return (EINVAL); 885 886 for (n = 0, i = 0; n < nclasses && i < CBQ_MAX_CLASSES; n++, i++) { 887 while ((cl = cbqp->cbq_class_tbl[i]) == NULL) 888 if (++i >= CBQ_MAX_CLASSES) 889 goto out; 890 891 get_class_stats(&stats, cl); 892 stats.handle = cl->stats_.handle; 893 894 if ((error = copyout((caddr_t)&stats, (caddr_t)usp++, 895 sizeof(stats))) != 0) 896 return (error); 897 } 898 899 out: 900 gsp->nclasses = n; 901 return (error); 902 } 903 904 static int 905 cbq_ifattach(ifacep) 906 struct cbq_interface *ifacep; 907 { 908 int error = 0; 909 char *ifacename; 910 cbq_state_t *new_cbqp; 911 struct ifnet *ifp; 912 913 ifacename = ifacep->cbq_ifacename; 914 if ((ifp = ifunit(ifacename)) == NULL) 915 return (ENXIO); 916 if (!ALTQ_IS_READY(&ifp->if_snd)) 917 return (ENXIO); 918 919 /* allocate and initialize cbq_state_t */ 920 new_cbqp = malloc(sizeof(cbq_state_t), M_DEVBUF, M_WAITOK); 921 if (new_cbqp == NULL) 922 return (ENOMEM); 923 bzero(new_cbqp, sizeof(cbq_state_t)); 924 CALLOUT_INIT(&new_cbqp->cbq_callout); 925 926 new_cbqp->cbq_qlen = 0; 927 new_cbqp->ifnp.ifq_ = &ifp->if_snd; /* keep the ifq */ 928 929 /* 930 * set CBQ to this ifnet structure. 931 */ 932 error = altq_attach(&ifp->if_snd, ALTQT_CBQ, new_cbqp, 933 cbq_enqueue, cbq_dequeue, cbq_request, 934 &new_cbqp->cbq_classifier, acc_classify); 935 if (error) { 936 free(new_cbqp, M_DEVBUF); 937 return (error); 938 } 939 940 /* prepend to the list of cbq_state_t's. */ 941 new_cbqp->cbq_next = cbq_list; 942 cbq_list = new_cbqp; 943 944 return (0); 945 } 946 947 static int 948 cbq_ifdetach(ifacep) 949 struct cbq_interface *ifacep; 950 { 951 char *ifacename; 952 cbq_state_t *cbqp; 953 954 ifacename = ifacep->cbq_ifacename; 955 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL) 956 return (EBADF); 957 958 (void)cbq_set_enable(ifacep, DISABLE); 959 960 cbq_clear_interface(cbqp); 961 962 /* remove CBQ from the ifnet structure. */ 963 (void)altq_detach(cbqp->ifnp.ifq_); 964 965 /* remove from the list of cbq_state_t's. */ 966 if (cbq_list == cbqp) 967 cbq_list = cbqp->cbq_next; 968 else { 969 cbq_state_t *cp; 970 971 for (cp = cbq_list; cp != NULL; cp = cp->cbq_next) 972 if (cp->cbq_next == cbqp) { 973 cp->cbq_next = cbqp->cbq_next; 974 break; 975 } 976 ASSERT(cp != NULL); 977 } 978 979 /* deallocate cbq_state_t */ 980 free(cbqp, M_DEVBUF); 981 982 return (0); 983 } 984 985 /* 986 * cbq device interface 987 */ 988 989 altqdev_decl(cbq); 990 991 int 992 cbqopen(dev, flag, fmt, p) 993 dev_t dev; 994 int flag, fmt; 995 #if (__FreeBSD_version > 500000) 996 struct thread *p; 997 #else 998 struct proc *p; 999 #endif 1000 { 1001 return (0); 1002 } 1003 1004 int 1005 cbqclose(dev, flag, fmt, p) 1006 dev_t dev; 1007 int flag, fmt; 1008 #if (__FreeBSD_version > 500000) 1009 struct thread *p; 1010 #else 1011 struct proc *p; 1012 #endif 1013 { 1014 struct ifnet *ifp; 1015 struct cbq_interface iface; 1016 int err, error = 0; 1017 1018 while (cbq_list) { 1019 ifp = cbq_list->ifnp.ifq_->altq_ifp; 1020 sprintf(iface.cbq_ifacename, "%s", ifp->if_xname); 1021 err = cbq_ifdetach(&iface); 1022 if (err != 0 && error == 0) 1023 error = err; 1024 } 1025 1026 return (error); 1027 } 1028 1029 int 1030 cbqioctl(dev, cmd, addr, flag, p) 1031 dev_t dev; 1032 ioctlcmd_t cmd; 1033 caddr_t addr; 1034 int flag; 1035 #if (__FreeBSD_version > 500000) 1036 struct thread *p; 1037 #else 1038 struct proc *p; 1039 #endif 1040 { 1041 int error = 0; 1042 1043 /* check cmd for superuser only */ 1044 switch (cmd) { 1045 case CBQ_GETSTATS: 1046 /* currently only command that an ordinary user can call */ 1047 break; 1048 default: 1049 #if (__FreeBSD_version > 700000) 1050 error = priv_check(p, PRIV_ALTQ_MANAGE); 1051 #elsif (__FreeBSD_version > 400000) 1052 error = suser(p); 1053 #else 1054 error = suser(p->p_ucred, &p->p_acflag); 1055 #endif 1056 if (error) 1057 return (error); 1058 break; 1059 } 1060 1061 switch (cmd) { 1062 1063 case CBQ_ENABLE: 1064 error = cbq_set_enable((struct cbq_interface *)addr, ENABLE); 1065 break; 1066 1067 case CBQ_DISABLE: 1068 error = cbq_set_enable((struct cbq_interface *)addr, DISABLE); 1069 break; 1070 1071 case CBQ_ADD_FILTER: 1072 error = cbq_add_filter((struct cbq_add_filter *)addr); 1073 break; 1074 1075 case CBQ_DEL_FILTER: 1076 error = cbq_delete_filter((struct cbq_delete_filter *)addr); 1077 break; 1078 1079 case CBQ_ADD_CLASS: 1080 error = cbq_add_class((struct cbq_add_class *)addr); 1081 break; 1082 1083 case CBQ_DEL_CLASS: 1084 error = cbq_delete_class((struct cbq_delete_class *)addr); 1085 break; 1086 1087 case CBQ_MODIFY_CLASS: 1088 error = cbq_modify_class((struct cbq_modify_class *)addr); 1089 break; 1090 1091 case CBQ_CLEAR_HIERARCHY: 1092 error = cbq_clear_hierarchy((struct cbq_interface *)addr); 1093 break; 1094 1095 case CBQ_IF_ATTACH: 1096 error = cbq_ifattach((struct cbq_interface *)addr); 1097 break; 1098 1099 case CBQ_IF_DETACH: 1100 error = cbq_ifdetach((struct cbq_interface *)addr); 1101 break; 1102 1103 case CBQ_GETSTATS: 1104 error = cbq_getstats((struct cbq_getstats *)addr); 1105 break; 1106 1107 default: 1108 error = EINVAL; 1109 break; 1110 } 1111 1112 return error; 1113 } 1114 1115 #if 0 1116 /* for debug */ 1117 static void cbq_class_dump(int); 1118 1119 static void cbq_class_dump(i) 1120 int i; 1121 { 1122 struct rm_class *cl; 1123 rm_class_stats_t *s; 1124 struct _class_queue_ *q; 1125 1126 if (cbq_list == NULL) { 1127 printf("cbq_class_dump: no cbq_state found\n"); 1128 return; 1129 } 1130 cl = cbq_list->cbq_class_tbl[i]; 1131 1132 printf("class %d cl=%p\n", i, cl); 1133 if (cl != NULL) { 1134 s = &cl->stats_; 1135 q = cl->q_; 1136 1137 printf("pri=%d, depth=%d, maxrate=%d, allotment=%d\n", 1138 cl->pri_, cl->depth_, cl->maxrate_, cl->allotment_); 1139 printf("w_allotment=%d, bytes_alloc=%d, avgidle=%d, maxidle=%d\n", 1140 cl->w_allotment_, cl->bytes_alloc_, cl->avgidle_, 1141 cl->maxidle_); 1142 printf("minidle=%d, offtime=%d, sleeping=%d, leaf=%d\n", 1143 cl->minidle_, cl->offtime_, cl->sleeping_, cl->leaf_); 1144 printf("handle=%d, depth=%d, packets=%d, bytes=%d\n", 1145 s->handle, s->depth, 1146 (int)s->xmit_cnt.packets, (int)s->xmit_cnt.bytes); 1147 printf("over=%d\n, borrows=%d, drops=%d, overactions=%d, delays=%d\n", 1148 s->over, s->borrows, (int)s->drop_cnt.packets, 1149 s->overactions, s->delays); 1150 printf("tail=%p, head=%p, qlen=%d, qlim=%d, qthresh=%d,qtype=%d\n", 1151 q->tail_, q->head_, q->qlen_, q->qlim_, 1152 q->qthresh_, q->qtype_); 1153 } 1154 } 1155 #endif /* 0 */ 1156 1157 #ifdef KLD_MODULE 1158 1159 static struct altqsw cbq_sw = 1160 {"cbq", cbqopen, cbqclose, cbqioctl}; 1161 1162 ALTQ_MODULE(altq_cbq, ALTQT_CBQ, &cbq_sw); 1163 MODULE_DEPEND(altq_cbq, altq_red, 1, 1, 1); 1164 MODULE_DEPEND(altq_cbq, altq_rio, 1, 1, 1); 1165 1166 #endif /* KLD_MODULE */ 1167 #endif /* ALTQ3_COMPAT */ 1168 1169 #endif /* ALTQ_CBQ */ 1170