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 48 #include <net/if.h> 49 #include <net/if_var.h> 50 #include <netinet/in.h> 51 52 #include <netpfil/pf/pf.h> 53 #include <netpfil/pf/pf_altq.h> 54 #include <netpfil/pf/pf_mtag.h> 55 #include <net/altq/altq.h> 56 #include <net/altq/altq_cbq.h> 57 58 /* 59 * Forward Declarations. 60 */ 61 static int cbq_class_destroy(cbq_state_t *, struct rm_class *); 62 static struct rm_class *clh_to_clp(cbq_state_t *, u_int32_t); 63 static int cbq_clear_interface(cbq_state_t *); 64 static int cbq_request(struct ifaltq *, int, void *); 65 static int cbq_enqueue(struct ifaltq *, struct mbuf *, 66 struct altq_pktattr *); 67 static struct mbuf *cbq_dequeue(struct ifaltq *, int); 68 static void cbqrestart(struct ifaltq *); 69 static void get_class_stats(class_stats_t *, struct rm_class *); 70 static void cbq_purge(cbq_state_t *); 71 72 /* 73 * int 74 * cbq_class_destroy(cbq_mod_state_t *, struct rm_class *) - This 75 * function destroys a given traffic class. Before destroying 76 * the class, all traffic for that class is released. 77 */ 78 static int 79 cbq_class_destroy(cbq_state_t *cbqp, struct rm_class *cl) 80 { 81 int i; 82 83 /* delete the class */ 84 rmc_delete_class(&cbqp->ifnp, cl); 85 86 /* 87 * free the class handle 88 */ 89 for (i = 0; i < CBQ_MAX_CLASSES; i++) 90 if (cbqp->cbq_class_tbl[i] == cl) 91 cbqp->cbq_class_tbl[i] = NULL; 92 93 if (cl == cbqp->ifnp.root_) 94 cbqp->ifnp.root_ = NULL; 95 if (cl == cbqp->ifnp.default_) 96 cbqp->ifnp.default_ = NULL; 97 return (0); 98 } 99 100 /* convert class handle to class pointer */ 101 static struct rm_class * 102 clh_to_clp(cbq_state_t *cbqp, u_int32_t chandle) 103 { 104 int i; 105 struct rm_class *cl; 106 107 if (chandle == 0) 108 return (NULL); 109 /* 110 * first, try optimistically the slot matching the lower bits of 111 * the handle. if it fails, do the linear table search. 112 */ 113 i = chandle % CBQ_MAX_CLASSES; 114 if ((cl = cbqp->cbq_class_tbl[i]) != NULL && 115 cl->stats_.handle == chandle) 116 return (cl); 117 for (i = 0; i < CBQ_MAX_CLASSES; i++) 118 if ((cl = cbqp->cbq_class_tbl[i]) != NULL && 119 cl->stats_.handle == chandle) 120 return (cl); 121 return (NULL); 122 } 123 124 static int 125 cbq_clear_interface(cbq_state_t *cbqp) 126 { 127 int again, i; 128 struct rm_class *cl; 129 130 #ifdef ALTQ3_CLFIER_COMPAT 131 /* free the filters for this interface */ 132 acc_discard_filters(&cbqp->cbq_classifier, NULL, 1); 133 #endif 134 135 /* clear out the classes now */ 136 do { 137 again = 0; 138 for (i = 0; i < CBQ_MAX_CLASSES; i++) { 139 if ((cl = cbqp->cbq_class_tbl[i]) != NULL) { 140 if (is_a_parent_class(cl)) 141 again++; 142 else { 143 cbq_class_destroy(cbqp, cl); 144 cbqp->cbq_class_tbl[i] = NULL; 145 if (cl == cbqp->ifnp.root_) 146 cbqp->ifnp.root_ = NULL; 147 if (cl == cbqp->ifnp.default_) 148 cbqp->ifnp.default_ = NULL; 149 } 150 } 151 } 152 } while (again); 153 154 return (0); 155 } 156 157 static int 158 cbq_request(struct ifaltq *ifq, int req, void *arg) 159 { 160 cbq_state_t *cbqp = (cbq_state_t *)ifq->altq_disc; 161 162 IFQ_LOCK_ASSERT(ifq); 163 164 switch (req) { 165 case ALTRQ_PURGE: 166 cbq_purge(cbqp); 167 break; 168 } 169 return (0); 170 } 171 172 /* copy the stats info in rm_class to class_states_t */ 173 static void 174 get_class_stats(class_stats_t *statsp, struct rm_class *cl) 175 { 176 statsp->xmit_cnt = cl->stats_.xmit_cnt; 177 statsp->drop_cnt = cl->stats_.drop_cnt; 178 statsp->over = cl->stats_.over; 179 statsp->borrows = cl->stats_.borrows; 180 statsp->overactions = cl->stats_.overactions; 181 statsp->delays = cl->stats_.delays; 182 183 statsp->depth = cl->depth_; 184 statsp->priority = cl->pri_; 185 statsp->maxidle = cl->maxidle_; 186 statsp->minidle = cl->minidle_; 187 statsp->offtime = cl->offtime_; 188 statsp->qmax = qlimit(cl->q_); 189 statsp->ns_per_byte = cl->ns_per_byte_; 190 statsp->wrr_allot = cl->w_allotment_; 191 statsp->qcnt = qlen(cl->q_); 192 statsp->avgidle = cl->avgidle_; 193 194 statsp->qtype = qtype(cl->q_); 195 #ifdef ALTQ_RED 196 if (q_is_red(cl->q_)) 197 red_getstats(cl->red_, &statsp->red[0]); 198 #endif 199 #ifdef ALTQ_RIO 200 if (q_is_rio(cl->q_)) 201 rio_getstats((rio_t *)cl->red_, &statsp->red[0]); 202 #endif 203 #ifdef ALTQ_CODEL 204 if (q_is_codel(cl->q_)) 205 codel_getstats(cl->codel_, &statsp->codel); 206 #endif 207 } 208 209 int 210 cbq_pfattach(struct pf_altq *a) 211 { 212 struct ifnet *ifp; 213 int s, error; 214 215 if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL) 216 return (EINVAL); 217 s = splnet(); 218 error = altq_attach(&ifp->if_snd, ALTQT_CBQ, a->altq_disc, 219 cbq_enqueue, cbq_dequeue, cbq_request, NULL, NULL); 220 splx(s); 221 return (error); 222 } 223 224 int 225 cbq_add_altq(struct ifnet *ifp, struct pf_altq *a) 226 { 227 cbq_state_t *cbqp; 228 229 if (ifp == NULL) 230 return (EINVAL); 231 if (!ALTQ_IS_READY(&ifp->if_snd)) 232 return (ENODEV); 233 234 /* allocate and initialize cbq_state_t */ 235 cbqp = malloc(sizeof(cbq_state_t), M_DEVBUF, M_NOWAIT | M_ZERO); 236 if (cbqp == NULL) 237 return (ENOMEM); 238 CALLOUT_INIT(&cbqp->cbq_callout); 239 cbqp->cbq_qlen = 0; 240 cbqp->ifnp.ifq_ = &ifp->if_snd; /* keep the ifq */ 241 242 /* keep the state in pf_altq */ 243 a->altq_disc = cbqp; 244 245 return (0); 246 } 247 248 int 249 cbq_remove_altq(struct pf_altq *a) 250 { 251 cbq_state_t *cbqp; 252 253 if ((cbqp = a->altq_disc) == NULL) 254 return (EINVAL); 255 a->altq_disc = NULL; 256 257 cbq_clear_interface(cbqp); 258 259 if (cbqp->ifnp.default_) 260 cbq_class_destroy(cbqp, cbqp->ifnp.default_); 261 if (cbqp->ifnp.root_) 262 cbq_class_destroy(cbqp, cbqp->ifnp.root_); 263 264 /* deallocate cbq_state_t */ 265 free(cbqp, M_DEVBUF); 266 267 return (0); 268 } 269 270 int 271 cbq_add_queue(struct pf_altq *a) 272 { 273 struct rm_class *borrow, *parent; 274 cbq_state_t *cbqp; 275 struct rm_class *cl; 276 struct cbq_opts *opts; 277 int i; 278 279 if ((cbqp = a->altq_disc) == NULL) 280 return (EINVAL); 281 if (a->qid == 0) 282 return (EINVAL); 283 284 /* 285 * find a free slot in the class table. if the slot matching 286 * the lower bits of qid is free, use this slot. otherwise, 287 * use the first free slot. 288 */ 289 i = a->qid % CBQ_MAX_CLASSES; 290 if (cbqp->cbq_class_tbl[i] != NULL) { 291 for (i = 0; i < CBQ_MAX_CLASSES; i++) 292 if (cbqp->cbq_class_tbl[i] == NULL) 293 break; 294 if (i == CBQ_MAX_CLASSES) 295 return (EINVAL); 296 } 297 298 opts = &a->pq_u.cbq_opts; 299 /* check parameters */ 300 if (a->priority >= CBQ_MAXPRI) 301 return (EINVAL); 302 303 /* Get pointers to parent and borrow classes. */ 304 parent = clh_to_clp(cbqp, a->parent_qid); 305 if (opts->flags & CBQCLF_BORROW) 306 borrow = parent; 307 else 308 borrow = NULL; 309 310 /* 311 * A class must borrow from it's parent or it can not 312 * borrow at all. Hence, borrow can be null. 313 */ 314 if (parent == NULL && (opts->flags & CBQCLF_ROOTCLASS) == 0) { 315 printf("cbq_add_queue: no parent class!\n"); 316 return (EINVAL); 317 } 318 319 if ((borrow != parent) && (borrow != NULL)) { 320 printf("cbq_add_class: borrow class != parent\n"); 321 return (EINVAL); 322 } 323 324 /* 325 * check parameters 326 */ 327 switch (opts->flags & CBQCLF_CLASSMASK) { 328 case CBQCLF_ROOTCLASS: 329 if (parent != NULL) 330 return (EINVAL); 331 if (cbqp->ifnp.root_) 332 return (EINVAL); 333 break; 334 case CBQCLF_DEFCLASS: 335 if (cbqp->ifnp.default_) 336 return (EINVAL); 337 break; 338 case 0: 339 if (a->qid == 0) 340 return (EINVAL); 341 break; 342 default: 343 /* more than two flags bits set */ 344 return (EINVAL); 345 } 346 347 /* 348 * create a class. if this is a root class, initialize the 349 * interface. 350 */ 351 if ((opts->flags & CBQCLF_CLASSMASK) == CBQCLF_ROOTCLASS) { 352 rmc_init(cbqp->ifnp.ifq_, &cbqp->ifnp, opts->ns_per_byte, 353 cbqrestart, a->qlimit, RM_MAXQUEUED, 354 opts->maxidle, opts->minidle, opts->offtime, 355 opts->flags); 356 cl = cbqp->ifnp.root_; 357 } else { 358 cl = rmc_newclass(a->priority, 359 &cbqp->ifnp, opts->ns_per_byte, 360 rmc_delay_action, a->qlimit, parent, borrow, 361 opts->maxidle, opts->minidle, opts->offtime, 362 opts->pktsize, opts->flags); 363 } 364 if (cl == NULL) 365 return (ENOMEM); 366 367 /* return handle to user space. */ 368 cl->stats_.handle = a->qid; 369 cl->stats_.depth = cl->depth_; 370 371 /* save the allocated class */ 372 cbqp->cbq_class_tbl[i] = cl; 373 374 if ((opts->flags & CBQCLF_CLASSMASK) == CBQCLF_DEFCLASS) 375 cbqp->ifnp.default_ = cl; 376 377 return (0); 378 } 379 380 int 381 cbq_remove_queue(struct pf_altq *a) 382 { 383 struct rm_class *cl; 384 cbq_state_t *cbqp; 385 int i; 386 387 if ((cbqp = a->altq_disc) == NULL) 388 return (EINVAL); 389 390 if ((cl = clh_to_clp(cbqp, a->qid)) == NULL) 391 return (EINVAL); 392 393 /* if we are a parent class, then return an error. */ 394 if (is_a_parent_class(cl)) 395 return (EINVAL); 396 397 /* delete the class */ 398 rmc_delete_class(&cbqp->ifnp, cl); 399 400 /* 401 * free the class handle 402 */ 403 for (i = 0; i < CBQ_MAX_CLASSES; i++) 404 if (cbqp->cbq_class_tbl[i] == cl) { 405 cbqp->cbq_class_tbl[i] = NULL; 406 if (cl == cbqp->ifnp.root_) 407 cbqp->ifnp.root_ = NULL; 408 if (cl == cbqp->ifnp.default_) 409 cbqp->ifnp.default_ = NULL; 410 break; 411 } 412 413 return (0); 414 } 415 416 int 417 cbq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes, int version) 418 { 419 cbq_state_t *cbqp; 420 struct rm_class *cl; 421 class_stats_t stats; 422 int error = 0; 423 424 if ((cbqp = altq_lookup(a->ifname, ALTQT_CBQ)) == NULL) 425 return (EBADF); 426 427 if ((cl = clh_to_clp(cbqp, a->qid)) == NULL) 428 return (EINVAL); 429 430 if (*nbytes < sizeof(stats)) 431 return (EINVAL); 432 433 get_class_stats(&stats, cl); 434 435 if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0) 436 return (error); 437 *nbytes = sizeof(stats); 438 return (0); 439 } 440 441 /* 442 * int 443 * cbq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pattr) 444 * - Queue data packets. 445 * 446 * cbq_enqueue is set to ifp->if_altqenqueue and called by an upper 447 * layer (e.g. ether_output). cbq_enqueue queues the given packet 448 * to the cbq, then invokes the driver's start routine. 449 * 450 * Assumptions: called in splimp 451 * Returns: 0 if the queueing is successful. 452 * ENOBUFS if a packet dropping occurred as a result of 453 * the queueing. 454 */ 455 456 static int 457 cbq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr) 458 { 459 cbq_state_t *cbqp = (cbq_state_t *)ifq->altq_disc; 460 struct rm_class *cl; 461 struct pf_mtag *t; 462 int len; 463 464 IFQ_LOCK_ASSERT(ifq); 465 466 /* grab class set by classifier */ 467 if ((m->m_flags & M_PKTHDR) == 0) { 468 /* should not happen */ 469 printf("altq: packet for %s does not have pkthdr\n", 470 ifq->altq_ifp->if_xname); 471 m_freem(m); 472 return (ENOBUFS); 473 } 474 cl = NULL; 475 if ((t = pf_find_mtag(m)) != NULL) 476 cl = clh_to_clp(cbqp, t->qid); 477 if (cl == NULL) { 478 cl = cbqp->ifnp.default_; 479 if (cl == NULL) { 480 m_freem(m); 481 return (ENOBUFS); 482 } 483 } 484 cl->pktattr_ = NULL; 485 len = m_pktlen(m); 486 if (rmc_queue_packet(cl, m) != 0) { 487 /* drop occurred. some mbuf was freed in rmc_queue_packet. */ 488 PKTCNTR_ADD(&cl->stats_.drop_cnt, len); 489 return (ENOBUFS); 490 } 491 492 /* successfully queued. */ 493 ++cbqp->cbq_qlen; 494 IFQ_INC_LEN(ifq); 495 return (0); 496 } 497 498 static struct mbuf * 499 cbq_dequeue(struct ifaltq *ifq, int op) 500 { 501 cbq_state_t *cbqp = (cbq_state_t *)ifq->altq_disc; 502 struct mbuf *m; 503 504 IFQ_LOCK_ASSERT(ifq); 505 506 m = rmc_dequeue_next(&cbqp->ifnp, op); 507 508 if (m && op == ALTDQ_REMOVE) { 509 --cbqp->cbq_qlen; /* decrement # of packets in cbq */ 510 IFQ_DEC_LEN(ifq); 511 512 /* Update the class. */ 513 rmc_update_class_util(&cbqp->ifnp); 514 } 515 return (m); 516 } 517 518 /* 519 * void 520 * cbqrestart(queue_t *) - Restart sending of data. 521 * called from rmc_restart in splimp via timeout after waking up 522 * a suspended class. 523 * Returns: NONE 524 */ 525 526 static void 527 cbqrestart(struct ifaltq *ifq) 528 { 529 cbq_state_t *cbqp; 530 struct ifnet *ifp; 531 532 IFQ_LOCK_ASSERT(ifq); 533 534 if (!ALTQ_IS_ENABLED(ifq)) 535 /* cbq must have been detached */ 536 return; 537 538 if ((cbqp = (cbq_state_t *)ifq->altq_disc) == NULL) 539 /* should not happen */ 540 return; 541 542 ifp = ifq->altq_ifp; 543 if (ifp->if_start && 544 cbqp->cbq_qlen > 0 && (ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0) { 545 IFQ_UNLOCK(ifq); 546 (*ifp->if_start)(ifp); 547 IFQ_LOCK(ifq); 548 } 549 } 550 551 static void cbq_purge(cbq_state_t *cbqp) 552 { 553 struct rm_class *cl; 554 int i; 555 556 for (i = 0; i < CBQ_MAX_CLASSES; i++) 557 if ((cl = cbqp->cbq_class_tbl[i]) != NULL) 558 rmc_dropall(cl); 559 if (ALTQ_IS_ENABLED(cbqp->ifnp.ifq_)) 560 cbqp->ifnp.ifq_->ifq_len = 0; 561 } 562 563 #endif /* ALTQ_CBQ */ 564