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 } 241 242 int 243 cbq_pfattach(struct pf_altq *a) 244 { 245 struct ifnet *ifp; 246 int s, error; 247 248 if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL) 249 return (EINVAL); 250 s = splnet(); 251 error = altq_attach(&ifp->if_snd, ALTQT_CBQ, a->altq_disc, 252 cbq_enqueue, cbq_dequeue, cbq_request, NULL, NULL); 253 splx(s); 254 return (error); 255 } 256 257 int 258 cbq_add_altq(struct pf_altq *a) 259 { 260 cbq_state_t *cbqp; 261 struct ifnet *ifp; 262 263 if ((ifp = ifunit(a->ifname)) == NULL) 264 return (EINVAL); 265 if (!ALTQ_IS_READY(&ifp->if_snd)) 266 return (ENODEV); 267 268 /* allocate and initialize cbq_state_t */ 269 cbqp = malloc(sizeof(cbq_state_t), M_DEVBUF, M_NOWAIT | M_ZERO); 270 if (cbqp == NULL) 271 return (ENOMEM); 272 CALLOUT_INIT(&cbqp->cbq_callout); 273 cbqp->cbq_qlen = 0; 274 cbqp->ifnp.ifq_ = &ifp->if_snd; /* keep the ifq */ 275 276 /* keep the state in pf_altq */ 277 a->altq_disc = cbqp; 278 279 return (0); 280 } 281 282 int 283 cbq_remove_altq(struct pf_altq *a) 284 { 285 cbq_state_t *cbqp; 286 287 if ((cbqp = a->altq_disc) == NULL) 288 return (EINVAL); 289 a->altq_disc = NULL; 290 291 cbq_clear_interface(cbqp); 292 293 if (cbqp->ifnp.default_) 294 cbq_class_destroy(cbqp, cbqp->ifnp.default_); 295 if (cbqp->ifnp.root_) 296 cbq_class_destroy(cbqp, cbqp->ifnp.root_); 297 298 /* deallocate cbq_state_t */ 299 free(cbqp, M_DEVBUF); 300 301 return (0); 302 } 303 304 int 305 cbq_add_queue(struct pf_altq *a) 306 { 307 struct rm_class *borrow, *parent; 308 cbq_state_t *cbqp; 309 struct rm_class *cl; 310 struct cbq_opts *opts; 311 int i; 312 313 if ((cbqp = a->altq_disc) == NULL) 314 return (EINVAL); 315 if (a->qid == 0) 316 return (EINVAL); 317 318 /* 319 * find a free slot in the class table. if the slot matching 320 * the lower bits of qid is free, use this slot. otherwise, 321 * use the first free slot. 322 */ 323 i = a->qid % CBQ_MAX_CLASSES; 324 if (cbqp->cbq_class_tbl[i] != NULL) { 325 for (i = 0; i < CBQ_MAX_CLASSES; i++) 326 if (cbqp->cbq_class_tbl[i] == NULL) 327 break; 328 if (i == CBQ_MAX_CLASSES) 329 return (EINVAL); 330 } 331 332 opts = &a->pq_u.cbq_opts; 333 /* check parameters */ 334 if (a->priority >= CBQ_MAXPRI) 335 return (EINVAL); 336 337 /* Get pointers to parent and borrow classes. */ 338 parent = clh_to_clp(cbqp, a->parent_qid); 339 if (opts->flags & CBQCLF_BORROW) 340 borrow = parent; 341 else 342 borrow = NULL; 343 344 /* 345 * A class must borrow from it's parent or it can not 346 * borrow at all. Hence, borrow can be null. 347 */ 348 if (parent == NULL && (opts->flags & CBQCLF_ROOTCLASS) == 0) { 349 printf("cbq_add_queue: no parent class!\n"); 350 return (EINVAL); 351 } 352 353 if ((borrow != parent) && (borrow != NULL)) { 354 printf("cbq_add_class: borrow class != parent\n"); 355 return (EINVAL); 356 } 357 358 /* 359 * check parameters 360 */ 361 switch (opts->flags & CBQCLF_CLASSMASK) { 362 case CBQCLF_ROOTCLASS: 363 if (parent != NULL) 364 return (EINVAL); 365 if (cbqp->ifnp.root_) 366 return (EINVAL); 367 break; 368 case CBQCLF_DEFCLASS: 369 if (cbqp->ifnp.default_) 370 return (EINVAL); 371 break; 372 case 0: 373 if (a->qid == 0) 374 return (EINVAL); 375 break; 376 default: 377 /* more than two flags bits set */ 378 return (EINVAL); 379 } 380 381 /* 382 * create a class. if this is a root class, initialize the 383 * interface. 384 */ 385 if ((opts->flags & CBQCLF_CLASSMASK) == CBQCLF_ROOTCLASS) { 386 rmc_init(cbqp->ifnp.ifq_, &cbqp->ifnp, opts->ns_per_byte, 387 cbqrestart, a->qlimit, RM_MAXQUEUED, 388 opts->maxidle, opts->minidle, opts->offtime, 389 opts->flags); 390 cl = cbqp->ifnp.root_; 391 } else { 392 cl = rmc_newclass(a->priority, 393 &cbqp->ifnp, opts->ns_per_byte, 394 rmc_delay_action, a->qlimit, parent, borrow, 395 opts->maxidle, opts->minidle, opts->offtime, 396 opts->pktsize, opts->flags); 397 } 398 if (cl == NULL) 399 return (ENOMEM); 400 401 /* return handle to user space. */ 402 cl->stats_.handle = a->qid; 403 cl->stats_.depth = cl->depth_; 404 405 /* save the allocated class */ 406 cbqp->cbq_class_tbl[i] = cl; 407 408 if ((opts->flags & CBQCLF_CLASSMASK) == CBQCLF_DEFCLASS) 409 cbqp->ifnp.default_ = cl; 410 411 return (0); 412 } 413 414 int 415 cbq_remove_queue(struct pf_altq *a) 416 { 417 struct rm_class *cl; 418 cbq_state_t *cbqp; 419 int i; 420 421 if ((cbqp = a->altq_disc) == NULL) 422 return (EINVAL); 423 424 if ((cl = clh_to_clp(cbqp, a->qid)) == NULL) 425 return (EINVAL); 426 427 /* if we are a parent class, then return an error. */ 428 if (is_a_parent_class(cl)) 429 return (EINVAL); 430 431 /* delete the class */ 432 rmc_delete_class(&cbqp->ifnp, cl); 433 434 /* 435 * free the class handle 436 */ 437 for (i = 0; i < CBQ_MAX_CLASSES; i++) 438 if (cbqp->cbq_class_tbl[i] == cl) { 439 cbqp->cbq_class_tbl[i] = NULL; 440 if (cl == cbqp->ifnp.root_) 441 cbqp->ifnp.root_ = NULL; 442 if (cl == cbqp->ifnp.default_) 443 cbqp->ifnp.default_ = NULL; 444 break; 445 } 446 447 return (0); 448 } 449 450 int 451 cbq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes) 452 { 453 cbq_state_t *cbqp; 454 struct rm_class *cl; 455 class_stats_t stats; 456 int error = 0; 457 458 if ((cbqp = altq_lookup(a->ifname, ALTQT_CBQ)) == NULL) 459 return (EBADF); 460 461 if ((cl = clh_to_clp(cbqp, a->qid)) == NULL) 462 return (EINVAL); 463 464 if (*nbytes < sizeof(stats)) 465 return (EINVAL); 466 467 get_class_stats(&stats, cl); 468 469 if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0) 470 return (error); 471 *nbytes = sizeof(stats); 472 return (0); 473 } 474 475 /* 476 * int 477 * cbq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pattr) 478 * - Queue data packets. 479 * 480 * cbq_enqueue is set to ifp->if_altqenqueue and called by an upper 481 * layer (e.g. ether_output). cbq_enqueue queues the given packet 482 * to the cbq, then invokes the driver's start routine. 483 * 484 * Assumptions: called in splimp 485 * Returns: 0 if the queueing is successful. 486 * ENOBUFS if a packet dropping occurred as a result of 487 * the queueing. 488 */ 489 490 static int 491 cbq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr) 492 { 493 cbq_state_t *cbqp = (cbq_state_t *)ifq->altq_disc; 494 struct rm_class *cl; 495 struct pf_mtag *t; 496 int len; 497 498 IFQ_LOCK_ASSERT(ifq); 499 500 /* grab class set by classifier */ 501 if ((m->m_flags & M_PKTHDR) == 0) { 502 /* should not happen */ 503 printf("altq: packet for %s does not have pkthdr\n", 504 ifq->altq_ifp->if_xname); 505 m_freem(m); 506 return (ENOBUFS); 507 } 508 cl = NULL; 509 if ((t = pf_find_mtag(m)) != NULL) 510 cl = clh_to_clp(cbqp, t->qid); 511 #ifdef ALTQ3_COMPAT 512 else if ((ifq->altq_flags & ALTQF_CLASSIFY) && pktattr != NULL) 513 cl = pktattr->pattr_class; 514 #endif 515 if (cl == NULL) { 516 cl = cbqp->ifnp.default_; 517 if (cl == NULL) { 518 m_freem(m); 519 return (ENOBUFS); 520 } 521 } 522 #ifdef ALTQ3_COMPAT 523 if (pktattr != NULL) 524 cl->pktattr_ = pktattr; /* save proto hdr used by ECN */ 525 else 526 #endif 527 cl->pktattr_ = NULL; 528 len = m_pktlen(m); 529 if (rmc_queue_packet(cl, m) != 0) { 530 /* drop occurred. some mbuf was freed in rmc_queue_packet. */ 531 PKTCNTR_ADD(&cl->stats_.drop_cnt, len); 532 return (ENOBUFS); 533 } 534 535 /* successfully queued. */ 536 ++cbqp->cbq_qlen; 537 IFQ_INC_LEN(ifq); 538 return (0); 539 } 540 541 static struct mbuf * 542 cbq_dequeue(struct ifaltq *ifq, int op) 543 { 544 cbq_state_t *cbqp = (cbq_state_t *)ifq->altq_disc; 545 struct mbuf *m; 546 547 IFQ_LOCK_ASSERT(ifq); 548 549 m = rmc_dequeue_next(&cbqp->ifnp, op); 550 551 if (m && op == ALTDQ_REMOVE) { 552 --cbqp->cbq_qlen; /* decrement # of packets in cbq */ 553 IFQ_DEC_LEN(ifq); 554 555 /* Update the class. */ 556 rmc_update_class_util(&cbqp->ifnp); 557 } 558 return (m); 559 } 560 561 /* 562 * void 563 * cbqrestart(queue_t *) - Restart sending of data. 564 * called from rmc_restart in splimp via timeout after waking up 565 * a suspended class. 566 * Returns: NONE 567 */ 568 569 static void 570 cbqrestart(struct ifaltq *ifq) 571 { 572 cbq_state_t *cbqp; 573 struct ifnet *ifp; 574 575 IFQ_LOCK_ASSERT(ifq); 576 577 if (!ALTQ_IS_ENABLED(ifq)) 578 /* cbq must have been detached */ 579 return; 580 581 if ((cbqp = (cbq_state_t *)ifq->altq_disc) == NULL) 582 /* should not happen */ 583 return; 584 585 ifp = ifq->altq_ifp; 586 if (ifp->if_start && 587 cbqp->cbq_qlen > 0 && (ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0) { 588 IFQ_UNLOCK(ifq); 589 (*ifp->if_start)(ifp); 590 IFQ_LOCK(ifq); 591 } 592 } 593 594 static void cbq_purge(cbq_state_t *cbqp) 595 { 596 struct rm_class *cl; 597 int i; 598 599 for (i = 0; i < CBQ_MAX_CLASSES; i++) 600 if ((cl = cbqp->cbq_class_tbl[i]) != NULL) 601 rmc_dropall(cl); 602 if (ALTQ_IS_ENABLED(cbqp->ifnp.ifq_)) 603 cbqp->ifnp.ifq_->ifq_len = 0; 604 } 605 #ifdef ALTQ3_COMPAT 606 607 static int 608 cbq_add_class(acp) 609 struct cbq_add_class *acp; 610 { 611 char *ifacename; 612 struct rm_class *borrow, *parent; 613 cbq_state_t *cbqp; 614 615 ifacename = acp->cbq_iface.cbq_ifacename; 616 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL) 617 return (EBADF); 618 619 /* check parameters */ 620 if (acp->cbq_class.priority >= CBQ_MAXPRI || 621 acp->cbq_class.maxq > CBQ_MAXQSIZE) 622 return (EINVAL); 623 624 /* Get pointers to parent and borrow classes. */ 625 parent = clh_to_clp(cbqp, acp->cbq_class.parent_class_handle); 626 borrow = clh_to_clp(cbqp, acp->cbq_class.borrow_class_handle); 627 628 /* 629 * A class must borrow from it's parent or it can not 630 * borrow at all. Hence, borrow can be null. 631 */ 632 if (parent == NULL && (acp->cbq_class.flags & CBQCLF_ROOTCLASS) == 0) { 633 printf("cbq_add_class: no parent class!\n"); 634 return (EINVAL); 635 } 636 637 if ((borrow != parent) && (borrow != NULL)) { 638 printf("cbq_add_class: borrow class != parent\n"); 639 return (EINVAL); 640 } 641 642 return cbq_class_create(cbqp, acp, parent, borrow); 643 } 644 645 static int 646 cbq_delete_class(dcp) 647 struct cbq_delete_class *dcp; 648 { 649 char *ifacename; 650 struct rm_class *cl; 651 cbq_state_t *cbqp; 652 653 ifacename = dcp->cbq_iface.cbq_ifacename; 654 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL) 655 return (EBADF); 656 657 if ((cl = clh_to_clp(cbqp, dcp->cbq_class_handle)) == NULL) 658 return (EINVAL); 659 660 /* if we are a parent class, then return an error. */ 661 if (is_a_parent_class(cl)) 662 return (EINVAL); 663 664 /* if a filter has a reference to this class delete the filter */ 665 acc_discard_filters(&cbqp->cbq_classifier, cl, 0); 666 667 return cbq_class_destroy(cbqp, cl); 668 } 669 670 static int 671 cbq_modify_class(acp) 672 struct cbq_modify_class *acp; 673 { 674 char *ifacename; 675 struct rm_class *cl; 676 cbq_state_t *cbqp; 677 678 ifacename = acp->cbq_iface.cbq_ifacename; 679 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL) 680 return (EBADF); 681 682 /* Get pointer to this class */ 683 if ((cl = clh_to_clp(cbqp, acp->cbq_class_handle)) == NULL) 684 return (EINVAL); 685 686 if (rmc_modclass(cl, acp->cbq_class.nano_sec_per_byte, 687 acp->cbq_class.maxq, acp->cbq_class.maxidle, 688 acp->cbq_class.minidle, acp->cbq_class.offtime, 689 acp->cbq_class.pktsize) < 0) 690 return (EINVAL); 691 return (0); 692 } 693 694 /* 695 * struct rm_class * 696 * cbq_class_create(cbq_mod_state_t *cbqp, struct cbq_add_class *acp, 697 * struct rm_class *parent, struct rm_class *borrow) 698 * 699 * This function create a new traffic class in the CBQ class hierarchy of 700 * given paramters. The class that created is either the root, default, 701 * or a new dynamic class. If CBQ is not initilaized, the the root class 702 * will be created. 703 */ 704 static int 705 cbq_class_create(cbqp, acp, parent, borrow) 706 cbq_state_t *cbqp; 707 struct cbq_add_class *acp; 708 struct rm_class *parent, *borrow; 709 { 710 struct rm_class *cl; 711 cbq_class_spec_t *spec = &acp->cbq_class; 712 u_int32_t chandle; 713 int i; 714 715 /* 716 * allocate class handle 717 */ 718 for (i = 1; i < CBQ_MAX_CLASSES; i++) 719 if (cbqp->cbq_class_tbl[i] == NULL) 720 break; 721 if (i == CBQ_MAX_CLASSES) 722 return (EINVAL); 723 chandle = i; /* use the slot number as class handle */ 724 725 /* 726 * create a class. if this is a root class, initialize the 727 * interface. 728 */ 729 if ((spec->flags & CBQCLF_CLASSMASK) == CBQCLF_ROOTCLASS) { 730 rmc_init(cbqp->ifnp.ifq_, &cbqp->ifnp, spec->nano_sec_per_byte, 731 cbqrestart, spec->maxq, RM_MAXQUEUED, 732 spec->maxidle, spec->minidle, spec->offtime, 733 spec->flags); 734 cl = cbqp->ifnp.root_; 735 } else { 736 cl = rmc_newclass(spec->priority, 737 &cbqp->ifnp, spec->nano_sec_per_byte, 738 rmc_delay_action, spec->maxq, parent, borrow, 739 spec->maxidle, spec->minidle, spec->offtime, 740 spec->pktsize, spec->flags); 741 } 742 if (cl == NULL) 743 return (ENOMEM); 744 745 /* return handle to user space. */ 746 acp->cbq_class_handle = chandle; 747 748 cl->stats_.handle = chandle; 749 cl->stats_.depth = cl->depth_; 750 751 /* save the allocated class */ 752 cbqp->cbq_class_tbl[i] = cl; 753 754 if ((spec->flags & CBQCLF_CLASSMASK) == CBQCLF_DEFCLASS) 755 cbqp->ifnp.default_ = cl; 756 if ((spec->flags & CBQCLF_CLASSMASK) == CBQCLF_CTLCLASS) 757 cbqp->ifnp.ctl_ = cl; 758 759 return (0); 760 } 761 762 static int 763 cbq_add_filter(afp) 764 struct cbq_add_filter *afp; 765 { 766 char *ifacename; 767 cbq_state_t *cbqp; 768 struct rm_class *cl; 769 770 ifacename = afp->cbq_iface.cbq_ifacename; 771 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL) 772 return (EBADF); 773 774 /* Get the pointer to class. */ 775 if ((cl = clh_to_clp(cbqp, afp->cbq_class_handle)) == NULL) 776 return (EINVAL); 777 778 return acc_add_filter(&cbqp->cbq_classifier, &afp->cbq_filter, 779 cl, &afp->cbq_filter_handle); 780 } 781 782 static int 783 cbq_delete_filter(dfp) 784 struct cbq_delete_filter *dfp; 785 { 786 char *ifacename; 787 cbq_state_t *cbqp; 788 789 ifacename = dfp->cbq_iface.cbq_ifacename; 790 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL) 791 return (EBADF); 792 793 return acc_delete_filter(&cbqp->cbq_classifier, 794 dfp->cbq_filter_handle); 795 } 796 797 /* 798 * cbq_clear_hierarchy deletes all classes and their filters on the 799 * given interface. 800 */ 801 static int 802 cbq_clear_hierarchy(ifacep) 803 struct cbq_interface *ifacep; 804 { 805 char *ifacename; 806 cbq_state_t *cbqp; 807 808 ifacename = ifacep->cbq_ifacename; 809 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL) 810 return (EBADF); 811 812 return cbq_clear_interface(cbqp); 813 } 814 815 /* 816 * static int 817 * cbq_set_enable(struct cbq_enable *ep) - this function processed the 818 * ioctl request to enable class based queueing. It searches the list 819 * of interfaces for the specified interface and then enables CBQ on 820 * that interface. 821 * 822 * Returns: 0, for no error. 823 * EBADF, for specified inteface not found. 824 */ 825 826 static int 827 cbq_set_enable(ep, enable) 828 struct cbq_interface *ep; 829 int enable; 830 { 831 int error = 0; 832 cbq_state_t *cbqp; 833 char *ifacename; 834 835 ifacename = ep->cbq_ifacename; 836 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL) 837 return (EBADF); 838 839 switch (enable) { 840 case ENABLE: 841 if (cbqp->ifnp.root_ == NULL || cbqp->ifnp.default_ == NULL || 842 cbqp->ifnp.ctl_ == NULL) { 843 if (cbqp->ifnp.root_ == NULL) 844 printf("No Root Class for %s\n", ifacename); 845 if (cbqp->ifnp.default_ == NULL) 846 printf("No Default Class for %s\n", ifacename); 847 if (cbqp->ifnp.ctl_ == NULL) 848 printf("No Control Class for %s\n", ifacename); 849 error = EINVAL; 850 } else if ((error = altq_enable(cbqp->ifnp.ifq_)) == 0) { 851 cbqp->cbq_qlen = 0; 852 } 853 break; 854 855 case DISABLE: 856 error = altq_disable(cbqp->ifnp.ifq_); 857 break; 858 } 859 return (error); 860 } 861 862 static int 863 cbq_getstats(gsp) 864 struct cbq_getstats *gsp; 865 { 866 char *ifacename; 867 int i, n, nclasses; 868 cbq_state_t *cbqp; 869 struct rm_class *cl; 870 class_stats_t stats, *usp; 871 int error = 0; 872 873 ifacename = gsp->iface.cbq_ifacename; 874 nclasses = gsp->nclasses; 875 usp = gsp->stats; 876 877 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL) 878 return (EBADF); 879 if (nclasses <= 0) 880 return (EINVAL); 881 882 for (n = 0, i = 0; n < nclasses && i < CBQ_MAX_CLASSES; n++, i++) { 883 while ((cl = cbqp->cbq_class_tbl[i]) == NULL) 884 if (++i >= CBQ_MAX_CLASSES) 885 goto out; 886 887 get_class_stats(&stats, cl); 888 stats.handle = cl->stats_.handle; 889 890 if ((error = copyout((caddr_t)&stats, (caddr_t)usp++, 891 sizeof(stats))) != 0) 892 return (error); 893 } 894 895 out: 896 gsp->nclasses = n; 897 return (error); 898 } 899 900 static int 901 cbq_ifattach(ifacep) 902 struct cbq_interface *ifacep; 903 { 904 int error = 0; 905 char *ifacename; 906 cbq_state_t *new_cbqp; 907 struct ifnet *ifp; 908 909 ifacename = ifacep->cbq_ifacename; 910 if ((ifp = ifunit(ifacename)) == NULL) 911 return (ENXIO); 912 if (!ALTQ_IS_READY(&ifp->if_snd)) 913 return (ENXIO); 914 915 /* allocate and initialize cbq_state_t */ 916 new_cbqp = malloc(sizeof(cbq_state_t), M_DEVBUF, M_WAITOK); 917 if (new_cbqp == NULL) 918 return (ENOMEM); 919 bzero(new_cbqp, sizeof(cbq_state_t)); 920 CALLOUT_INIT(&new_cbqp->cbq_callout); 921 922 new_cbqp->cbq_qlen = 0; 923 new_cbqp->ifnp.ifq_ = &ifp->if_snd; /* keep the ifq */ 924 925 /* 926 * set CBQ to this ifnet structure. 927 */ 928 error = altq_attach(&ifp->if_snd, ALTQT_CBQ, new_cbqp, 929 cbq_enqueue, cbq_dequeue, cbq_request, 930 &new_cbqp->cbq_classifier, acc_classify); 931 if (error) { 932 free(new_cbqp, M_DEVBUF); 933 return (error); 934 } 935 936 /* prepend to the list of cbq_state_t's. */ 937 new_cbqp->cbq_next = cbq_list; 938 cbq_list = new_cbqp; 939 940 return (0); 941 } 942 943 static int 944 cbq_ifdetach(ifacep) 945 struct cbq_interface *ifacep; 946 { 947 char *ifacename; 948 cbq_state_t *cbqp; 949 950 ifacename = ifacep->cbq_ifacename; 951 if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL) 952 return (EBADF); 953 954 (void)cbq_set_enable(ifacep, DISABLE); 955 956 cbq_clear_interface(cbqp); 957 958 /* remove CBQ from the ifnet structure. */ 959 (void)altq_detach(cbqp->ifnp.ifq_); 960 961 /* remove from the list of cbq_state_t's. */ 962 if (cbq_list == cbqp) 963 cbq_list = cbqp->cbq_next; 964 else { 965 cbq_state_t *cp; 966 967 for (cp = cbq_list; cp != NULL; cp = cp->cbq_next) 968 if (cp->cbq_next == cbqp) { 969 cp->cbq_next = cbqp->cbq_next; 970 break; 971 } 972 ASSERT(cp != NULL); 973 } 974 975 /* deallocate cbq_state_t */ 976 free(cbqp, M_DEVBUF); 977 978 return (0); 979 } 980 981 /* 982 * cbq device interface 983 */ 984 985 altqdev_decl(cbq); 986 987 int 988 cbqopen(dev, flag, fmt, p) 989 dev_t dev; 990 int flag, fmt; 991 #if (__FreeBSD_version > 500000) 992 struct thread *p; 993 #else 994 struct proc *p; 995 #endif 996 { 997 return (0); 998 } 999 1000 int 1001 cbqclose(dev, flag, fmt, p) 1002 dev_t dev; 1003 int flag, fmt; 1004 #if (__FreeBSD_version > 500000) 1005 struct thread *p; 1006 #else 1007 struct proc *p; 1008 #endif 1009 { 1010 struct ifnet *ifp; 1011 struct cbq_interface iface; 1012 int err, error = 0; 1013 1014 while (cbq_list) { 1015 ifp = cbq_list->ifnp.ifq_->altq_ifp; 1016 sprintf(iface.cbq_ifacename, "%s", ifp->if_xname); 1017 err = cbq_ifdetach(&iface); 1018 if (err != 0 && error == 0) 1019 error = err; 1020 } 1021 1022 return (error); 1023 } 1024 1025 int 1026 cbqioctl(dev, cmd, addr, flag, p) 1027 dev_t dev; 1028 ioctlcmd_t cmd; 1029 caddr_t addr; 1030 int flag; 1031 #if (__FreeBSD_version > 500000) 1032 struct thread *p; 1033 #else 1034 struct proc *p; 1035 #endif 1036 { 1037 int error = 0; 1038 1039 /* check cmd for superuser only */ 1040 switch (cmd) { 1041 case CBQ_GETSTATS: 1042 /* currently only command that an ordinary user can call */ 1043 break; 1044 default: 1045 #if (__FreeBSD_version > 700000) 1046 error = priv_check(p, PRIV_ALTQ_MANAGE); 1047 #elsif (__FreeBSD_version > 400000) 1048 error = suser(p); 1049 #else 1050 error = suser(p->p_ucred, &p->p_acflag); 1051 #endif 1052 if (error) 1053 return (error); 1054 break; 1055 } 1056 1057 switch (cmd) { 1058 1059 case CBQ_ENABLE: 1060 error = cbq_set_enable((struct cbq_interface *)addr, ENABLE); 1061 break; 1062 1063 case CBQ_DISABLE: 1064 error = cbq_set_enable((struct cbq_interface *)addr, DISABLE); 1065 break; 1066 1067 case CBQ_ADD_FILTER: 1068 error = cbq_add_filter((struct cbq_add_filter *)addr); 1069 break; 1070 1071 case CBQ_DEL_FILTER: 1072 error = cbq_delete_filter((struct cbq_delete_filter *)addr); 1073 break; 1074 1075 case CBQ_ADD_CLASS: 1076 error = cbq_add_class((struct cbq_add_class *)addr); 1077 break; 1078 1079 case CBQ_DEL_CLASS: 1080 error = cbq_delete_class((struct cbq_delete_class *)addr); 1081 break; 1082 1083 case CBQ_MODIFY_CLASS: 1084 error = cbq_modify_class((struct cbq_modify_class *)addr); 1085 break; 1086 1087 case CBQ_CLEAR_HIERARCHY: 1088 error = cbq_clear_hierarchy((struct cbq_interface *)addr); 1089 break; 1090 1091 case CBQ_IF_ATTACH: 1092 error = cbq_ifattach((struct cbq_interface *)addr); 1093 break; 1094 1095 case CBQ_IF_DETACH: 1096 error = cbq_ifdetach((struct cbq_interface *)addr); 1097 break; 1098 1099 case CBQ_GETSTATS: 1100 error = cbq_getstats((struct cbq_getstats *)addr); 1101 break; 1102 1103 default: 1104 error = EINVAL; 1105 break; 1106 } 1107 1108 return error; 1109 } 1110 1111 #if 0 1112 /* for debug */ 1113 static void cbq_class_dump(int); 1114 1115 static void cbq_class_dump(i) 1116 int i; 1117 { 1118 struct rm_class *cl; 1119 rm_class_stats_t *s; 1120 struct _class_queue_ *q; 1121 1122 if (cbq_list == NULL) { 1123 printf("cbq_class_dump: no cbq_state found\n"); 1124 return; 1125 } 1126 cl = cbq_list->cbq_class_tbl[i]; 1127 1128 printf("class %d cl=%p\n", i, cl); 1129 if (cl != NULL) { 1130 s = &cl->stats_; 1131 q = cl->q_; 1132 1133 printf("pri=%d, depth=%d, maxrate=%d, allotment=%d\n", 1134 cl->pri_, cl->depth_, cl->maxrate_, cl->allotment_); 1135 printf("w_allotment=%d, bytes_alloc=%d, avgidle=%d, maxidle=%d\n", 1136 cl->w_allotment_, cl->bytes_alloc_, cl->avgidle_, 1137 cl->maxidle_); 1138 printf("minidle=%d, offtime=%d, sleeping=%d, leaf=%d\n", 1139 cl->minidle_, cl->offtime_, cl->sleeping_, cl->leaf_); 1140 printf("handle=%d, depth=%d, packets=%d, bytes=%d\n", 1141 s->handle, s->depth, 1142 (int)s->xmit_cnt.packets, (int)s->xmit_cnt.bytes); 1143 printf("over=%d\n, borrows=%d, drops=%d, overactions=%d, delays=%d\n", 1144 s->over, s->borrows, (int)s->drop_cnt.packets, 1145 s->overactions, s->delays); 1146 printf("tail=%p, head=%p, qlen=%d, qlim=%d, qthresh=%d,qtype=%d\n", 1147 q->tail_, q->head_, q->qlen_, q->qlim_, 1148 q->qthresh_, q->qtype_); 1149 } 1150 } 1151 #endif /* 0 */ 1152 1153 #ifdef KLD_MODULE 1154 1155 static struct altqsw cbq_sw = 1156 {"cbq", cbqopen, cbqclose, cbqioctl}; 1157 1158 ALTQ_MODULE(altq_cbq, ALTQT_CBQ, &cbq_sw); 1159 MODULE_DEPEND(altq_cbq, altq_red, 1, 1, 1); 1160 MODULE_DEPEND(altq_cbq, altq_rio, 1, 1, 1); 1161 1162 #endif /* KLD_MODULE */ 1163 #endif /* ALTQ3_COMPAT */ 1164 1165 #endif /* ALTQ_CBQ */ 1166