1 /* $NetBSD: altq_priq.c,v 1.23 2016/04/20 08:58:48 knakahara Exp $ */ 2 /* $KAME: altq_priq.c,v 1.13 2005/04/13 03:44:25 suz Exp $ */ 3 /* 4 * Copyright (C) 2000-2003 5 * Sony Computer Science Laboratories Inc. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 /* 30 * priority queue 31 */ 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: altq_priq.c,v 1.23 2016/04/20 08:58:48 knakahara Exp $"); 35 36 #ifdef _KERNEL_OPT 37 #include "opt_altq.h" 38 #include "opt_inet.h" 39 #include "pf.h" 40 #endif 41 42 #ifdef ALTQ_PRIQ /* priq is enabled by ALTQ_PRIQ option in opt_altq.h */ 43 44 #include <sys/param.h> 45 #include <sys/malloc.h> 46 #include <sys/mbuf.h> 47 #include <sys/socket.h> 48 #include <sys/sockio.h> 49 #include <sys/systm.h> 50 #include <sys/proc.h> 51 #include <sys/errno.h> 52 #include <sys/kernel.h> 53 #include <sys/queue.h> 54 #include <sys/kauth.h> 55 56 #include <net/if.h> 57 #include <netinet/in.h> 58 59 #if NPF > 0 60 #include <net/pfvar.h> 61 #endif 62 #include <altq/altq.h> 63 #include <altq/altq_conf.h> 64 #include <altq/altq_priq.h> 65 66 /* 67 * function prototypes 68 */ 69 #ifdef ALTQ3_COMPAT 70 static struct priq_if *priq_attach(struct ifaltq *, u_int); 71 static void priq_detach(struct priq_if *); 72 #endif 73 static int priq_clear_interface(struct priq_if *); 74 static int priq_request(struct ifaltq *, int, void *); 75 static void priq_purge(struct priq_if *); 76 static struct priq_class *priq_class_create(struct priq_if *, int, int, int, 77 int); 78 static int priq_class_destroy(struct priq_class *); 79 static int priq_enqueue(struct ifaltq *, struct mbuf *); 80 static struct mbuf *priq_dequeue(struct ifaltq *, int); 81 82 static int priq_addq(struct priq_class *, struct mbuf *); 83 static struct mbuf *priq_getq(struct priq_class *); 84 static struct mbuf *priq_pollq(struct priq_class *); 85 static void priq_purgeq(struct priq_class *); 86 87 #ifdef ALTQ3_COMPAT 88 static int priqcmd_if_attach(struct priq_interface *); 89 static int priqcmd_if_detach(struct priq_interface *); 90 static int priqcmd_add_class(struct priq_add_class *); 91 static int priqcmd_delete_class(struct priq_delete_class *); 92 static int priqcmd_modify_class(struct priq_modify_class *); 93 static int priqcmd_add_filter(struct priq_add_filter *); 94 static int priqcmd_delete_filter(struct priq_delete_filter *); 95 static int priqcmd_class_stats(struct priq_class_stats *); 96 #endif /* ALTQ3_COMPAT */ 97 98 static void get_class_stats(struct priq_classstats *, struct priq_class *); 99 static struct priq_class *clh_to_clp(struct priq_if *, u_int32_t); 100 101 #ifdef ALTQ3_COMPAT 102 altqdev_decl(priq); 103 104 /* pif_list keeps all priq_if's allocated. */ 105 static struct priq_if *pif_list = NULL; 106 #endif /* ALTQ3_COMPAT */ 107 108 #if NPF > 0 109 int 110 priq_pfattach(struct pf_altq *a) 111 { 112 struct ifnet *ifp; 113 int s, error; 114 115 if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL) 116 return (EINVAL); 117 s = splnet(); 118 error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, a->altq_disc, 119 priq_enqueue, priq_dequeue, priq_request, NULL, NULL); 120 splx(s); 121 return (error); 122 } 123 124 int 125 priq_add_altq(struct pf_altq *a) 126 { 127 struct priq_if *pif; 128 struct ifnet *ifp; 129 130 if ((ifp = ifunit(a->ifname)) == NULL) 131 return (EINVAL); 132 if (!ALTQ_IS_READY(&ifp->if_snd)) 133 return (ENODEV); 134 135 pif = malloc(sizeof(struct priq_if), M_DEVBUF, M_WAITOK|M_ZERO); 136 if (pif == NULL) 137 return (ENOMEM); 138 pif->pif_bandwidth = a->ifbandwidth; 139 pif->pif_maxpri = -1; 140 pif->pif_ifq = &ifp->if_snd; 141 142 /* keep the state in pf_altq */ 143 a->altq_disc = pif; 144 145 return (0); 146 } 147 148 int 149 priq_remove_altq(struct pf_altq *a) 150 { 151 struct priq_if *pif; 152 153 if ((pif = a->altq_disc) == NULL) 154 return (EINVAL); 155 a->altq_disc = NULL; 156 157 (void)priq_clear_interface(pif); 158 159 free(pif, M_DEVBUF); 160 return (0); 161 } 162 163 int 164 priq_add_queue(struct pf_altq *a) 165 { 166 struct priq_if *pif; 167 struct priq_class *cl; 168 169 if ((pif = a->altq_disc) == NULL) 170 return (EINVAL); 171 172 /* check parameters */ 173 if (a->priority >= PRIQ_MAXPRI) 174 return (EINVAL); 175 if (a->qid == 0) 176 return (EINVAL); 177 if (pif->pif_classes[a->priority] != NULL) 178 return (EBUSY); 179 if (clh_to_clp(pif, a->qid) != NULL) 180 return (EBUSY); 181 182 cl = priq_class_create(pif, a->priority, a->qlimit, 183 a->pq_u.priq_opts.flags, a->qid); 184 if (cl == NULL) 185 return (ENOMEM); 186 187 return (0); 188 } 189 190 int 191 priq_remove_queue(struct pf_altq *a) 192 { 193 struct priq_if *pif; 194 struct priq_class *cl; 195 196 if ((pif = a->altq_disc) == NULL) 197 return (EINVAL); 198 199 if ((cl = clh_to_clp(pif, a->qid)) == NULL) 200 return (EINVAL); 201 202 return (priq_class_destroy(cl)); 203 } 204 205 int 206 priq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes) 207 { 208 struct priq_if *pif; 209 struct priq_class *cl; 210 struct priq_classstats stats; 211 int error = 0; 212 213 if ((pif = altq_lookup(a->ifname, ALTQT_PRIQ)) == NULL) 214 return (EBADF); 215 216 if ((cl = clh_to_clp(pif, a->qid)) == NULL) 217 return (EINVAL); 218 219 if (*nbytes < sizeof(stats)) 220 return (EINVAL); 221 222 get_class_stats(&stats, cl); 223 224 if ((error = copyout((void *)&stats, ubuf, sizeof(stats))) != 0) 225 return (error); 226 *nbytes = sizeof(stats); 227 return (0); 228 } 229 #endif /* NPF > 0 */ 230 231 /* 232 * bring the interface back to the initial state by discarding 233 * all the filters and classes. 234 */ 235 static int 236 priq_clear_interface(struct priq_if *pif) 237 { 238 struct priq_class *cl; 239 int pri; 240 241 #ifdef ALTQ3_CLFIER_COMPAT 242 /* free the filters for this interface */ 243 acc_discard_filters(&pif->pif_classifier, NULL, 1); 244 #endif 245 246 /* clear out the classes */ 247 for (pri = 0; pri <= pif->pif_maxpri; pri++) 248 if ((cl = pif->pif_classes[pri]) != NULL) 249 priq_class_destroy(cl); 250 251 return (0); 252 } 253 254 static int 255 priq_request(struct ifaltq *ifq, int req, void *arg) 256 { 257 struct priq_if *pif = (struct priq_if *)ifq->altq_disc; 258 259 switch (req) { 260 case ALTRQ_PURGE: 261 priq_purge(pif); 262 break; 263 } 264 return (0); 265 } 266 267 /* discard all the queued packets on the interface */ 268 static void 269 priq_purge(struct priq_if *pif) 270 { 271 struct priq_class *cl; 272 int pri; 273 274 for (pri = 0; pri <= pif->pif_maxpri; pri++) { 275 if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q)) 276 priq_purgeq(cl); 277 } 278 if (ALTQ_IS_ENABLED(pif->pif_ifq)) 279 pif->pif_ifq->ifq_len = 0; 280 } 281 282 static struct priq_class * 283 priq_class_create(struct priq_if *pif, int pri, int qlimit, int flags, int qid) 284 { 285 struct priq_class *cl; 286 int s; 287 288 #ifndef ALTQ_RED 289 if (flags & PRCF_RED) { 290 #ifdef ALTQ_DEBUG 291 printf("priq_class_create: RED not configured for PRIQ!\n"); 292 #endif 293 return (NULL); 294 } 295 #endif 296 297 if ((cl = pif->pif_classes[pri]) != NULL) { 298 /* modify the class instead of creating a new one */ 299 s = splnet(); 300 if (!qempty(cl->cl_q)) 301 priq_purgeq(cl); 302 splx(s); 303 #ifdef ALTQ_RIO 304 if (q_is_rio(cl->cl_q)) 305 rio_destroy((rio_t *)cl->cl_red); 306 #endif 307 #ifdef ALTQ_RED 308 if (q_is_red(cl->cl_q)) 309 red_destroy(cl->cl_red); 310 #endif 311 } else { 312 cl = malloc(sizeof(struct priq_class), M_DEVBUF, 313 M_WAITOK|M_ZERO); 314 if (cl == NULL) 315 return (NULL); 316 317 cl->cl_q = malloc(sizeof(class_queue_t), M_DEVBUF, 318 M_WAITOK|M_ZERO); 319 if (cl->cl_q == NULL) 320 goto err_ret; 321 } 322 323 pif->pif_classes[pri] = cl; 324 if (flags & PRCF_DEFAULTCLASS) 325 pif->pif_default = cl; 326 if (qlimit == 0) 327 qlimit = 50; /* use default */ 328 qlimit(cl->cl_q) = qlimit; 329 qtype(cl->cl_q) = Q_DROPTAIL; 330 qlen(cl->cl_q) = 0; 331 cl->cl_flags = flags; 332 cl->cl_pri = pri; 333 if (pri > pif->pif_maxpri) 334 pif->pif_maxpri = pri; 335 cl->cl_pif = pif; 336 cl->cl_handle = qid; 337 338 #ifdef ALTQ_RED 339 if (flags & (PRCF_RED|PRCF_RIO)) { 340 int red_flags, red_pkttime; 341 342 red_flags = 0; 343 if (flags & PRCF_ECN) 344 red_flags |= REDF_ECN; 345 #ifdef ALTQ_RIO 346 if (flags & PRCF_CLEARDSCP) 347 red_flags |= RIOF_CLEARDSCP; 348 #endif 349 if (pif->pif_bandwidth < 8) 350 red_pkttime = 1000 * 1000 * 1000; /* 1 sec */ 351 else 352 red_pkttime = (int64_t)pif->pif_ifq->altq_ifp->if_mtu 353 * 1000 * 1000 * 1000 / (pif->pif_bandwidth / 8); 354 #ifdef ALTQ_RIO 355 if (flags & PRCF_RIO) { 356 cl->cl_red = (red_t *)rio_alloc(0, NULL, 357 red_flags, red_pkttime); 358 if (cl->cl_red != NULL) 359 qtype(cl->cl_q) = Q_RIO; 360 } else 361 #endif 362 if (flags & PRCF_RED) { 363 cl->cl_red = red_alloc(0, 0, 364 qlimit(cl->cl_q) * 10/100, 365 qlimit(cl->cl_q) * 30/100, 366 red_flags, red_pkttime); 367 if (cl->cl_red != NULL) 368 qtype(cl->cl_q) = Q_RED; 369 } 370 } 371 #endif /* ALTQ_RED */ 372 373 return (cl); 374 375 err_ret: 376 if (cl->cl_red != NULL) { 377 #ifdef ALTQ_RIO 378 if (q_is_rio(cl->cl_q)) 379 rio_destroy((rio_t *)cl->cl_red); 380 #endif 381 #ifdef ALTQ_RED 382 if (q_is_red(cl->cl_q)) 383 red_destroy(cl->cl_red); 384 #endif 385 } 386 if (cl->cl_q != NULL) 387 free(cl->cl_q, M_DEVBUF); 388 free(cl, M_DEVBUF); 389 return (NULL); 390 } 391 392 static int 393 priq_class_destroy(struct priq_class *cl) 394 { 395 struct priq_if *pif; 396 int s, pri; 397 398 s = splnet(); 399 400 #ifdef ALTQ3_CLFIER_COMPAT 401 /* delete filters referencing to this class */ 402 acc_discard_filters(&cl->cl_pif->pif_classifier, cl, 0); 403 #endif 404 405 if (!qempty(cl->cl_q)) 406 priq_purgeq(cl); 407 408 pif = cl->cl_pif; 409 pif->pif_classes[cl->cl_pri] = NULL; 410 if (pif->pif_maxpri == cl->cl_pri) { 411 for (pri = cl->cl_pri; pri >= 0; pri--) 412 if (pif->pif_classes[pri] != NULL) { 413 pif->pif_maxpri = pri; 414 break; 415 } 416 if (pri < 0) 417 pif->pif_maxpri = -1; 418 } 419 splx(s); 420 421 if (cl->cl_red != NULL) { 422 #ifdef ALTQ_RIO 423 if (q_is_rio(cl->cl_q)) 424 rio_destroy((rio_t *)cl->cl_red); 425 #endif 426 #ifdef ALTQ_RED 427 if (q_is_red(cl->cl_q)) 428 red_destroy(cl->cl_red); 429 #endif 430 } 431 free(cl->cl_q, M_DEVBUF); 432 free(cl, M_DEVBUF); 433 return (0); 434 } 435 436 /* 437 * priq_enqueue is an enqueue function to be registered to 438 * (*altq_enqueue) in struct ifaltq. 439 */ 440 static int 441 priq_enqueue(struct ifaltq *ifq, struct mbuf *m) 442 { 443 struct altq_pktattr pktattr; 444 struct priq_if *pif = (struct priq_if *)ifq->altq_disc; 445 struct priq_class *cl; 446 struct m_tag *t; 447 int len; 448 449 /* grab class set by classifier */ 450 if ((m->m_flags & M_PKTHDR) == 0) { 451 /* should not happen */ 452 printf("altq: packet for %s does not have pkthdr\n", 453 ifq->altq_ifp->if_xname); 454 m_freem(m); 455 return (ENOBUFS); 456 } 457 cl = NULL; 458 if ((t = m_tag_find(m, PACKET_TAG_ALTQ_QID, NULL)) != NULL) 459 cl = clh_to_clp(pif, ((struct altq_tag *)(t+1))->qid); 460 #ifdef ALTQ3_COMPAT 461 else if (ifq->altq_flags & ALTQF_CLASSIFY) 462 cl = m->m_pkthdr.pattr_class; 463 #endif 464 if (cl == NULL) { 465 cl = pif->pif_default; 466 if (cl == NULL) { 467 m_freem(m); 468 return (ENOBUFS); 469 } 470 } 471 #ifdef ALTQ3_COMPAT 472 if (m->m_pkthdr.pattr_af != AF_UNSPEC) { 473 pktattr.pattr_class = m->m_pkthdr.pattr_class; 474 pktattr.pattr_af = m->m_pkthdr.pattr_af; 475 pktattr.pattr_hdr = m->m_pkthdr.pattr_hdr; 476 477 cl->cl_pktattr = &pktattr; /* save proto hdr used by ECN */ 478 } else 479 #endif 480 cl->cl_pktattr = NULL; 481 len = m_pktlen(m); 482 if (priq_addq(cl, m) != 0) { 483 /* drop occurred. mbuf was freed in priq_addq. */ 484 PKTCNTR_ADD(&cl->cl_dropcnt, len); 485 return (ENOBUFS); 486 } 487 IFQ_INC_LEN(ifq); 488 489 /* successfully queued. */ 490 return (0); 491 } 492 493 /* 494 * priq_dequeue is a dequeue function to be registered to 495 * (*altq_dequeue) in struct ifaltq. 496 * 497 * note: ALTDQ_POLL returns the next packet without removing the packet 498 * from the queue. ALTDQ_REMOVE is a normal dequeue operation. 499 * ALTDQ_REMOVE must return the same packet if called immediately 500 * after ALTDQ_POLL. 501 */ 502 static struct mbuf * 503 priq_dequeue(struct ifaltq *ifq, int op) 504 { 505 struct priq_if *pif = (struct priq_if *)ifq->altq_disc; 506 struct priq_class *cl; 507 struct mbuf *m; 508 int pri; 509 510 if (IFQ_IS_EMPTY(ifq)) 511 /* no packet in the queue */ 512 return (NULL); 513 514 for (pri = pif->pif_maxpri; pri >= 0; pri--) { 515 if ((cl = pif->pif_classes[pri]) != NULL && 516 !qempty(cl->cl_q)) { 517 if (op == ALTDQ_POLL) 518 return (priq_pollq(cl)); 519 520 m = priq_getq(cl); 521 if (m != NULL) { 522 IFQ_DEC_LEN(ifq); 523 if (qempty(cl->cl_q)) 524 cl->cl_period++; 525 PKTCNTR_ADD(&cl->cl_xmitcnt, m_pktlen(m)); 526 } 527 return (m); 528 } 529 } 530 return (NULL); 531 } 532 533 static int 534 priq_addq(struct priq_class *cl, struct mbuf *m) 535 { 536 537 #ifdef ALTQ_RIO 538 if (q_is_rio(cl->cl_q)) 539 return rio_addq((rio_t *)cl->cl_red, cl->cl_q, m, 540 cl->cl_pktattr); 541 #endif 542 #ifdef ALTQ_RED 543 if (q_is_red(cl->cl_q)) 544 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr); 545 #endif 546 if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) { 547 m_freem(m); 548 return (-1); 549 } 550 551 if (cl->cl_flags & PRCF_CLEARDSCP) 552 write_dsfield(m, cl->cl_pktattr, 0); 553 554 _addq(cl->cl_q, m); 555 556 return (0); 557 } 558 559 static struct mbuf * 560 priq_getq(struct priq_class *cl) 561 { 562 #ifdef ALTQ_RIO 563 if (q_is_rio(cl->cl_q)) 564 return rio_getq((rio_t *)cl->cl_red, cl->cl_q); 565 #endif 566 #ifdef ALTQ_RED 567 if (q_is_red(cl->cl_q)) 568 return red_getq(cl->cl_red, cl->cl_q); 569 #endif 570 return _getq(cl->cl_q); 571 } 572 573 static struct mbuf * 574 priq_pollq(struct priq_class *cl) 575 { 576 return qhead(cl->cl_q); 577 } 578 579 static void 580 priq_purgeq(struct priq_class *cl) 581 { 582 struct mbuf *m; 583 584 if (qempty(cl->cl_q)) 585 return; 586 587 while ((m = _getq(cl->cl_q)) != NULL) { 588 PKTCNTR_ADD(&cl->cl_dropcnt, m_pktlen(m)); 589 m_freem(m); 590 } 591 ASSERT(qlen(cl->cl_q) == 0); 592 } 593 594 static void 595 get_class_stats(struct priq_classstats *sp, struct priq_class *cl) 596 { 597 sp->class_handle = cl->cl_handle; 598 sp->qlength = qlen(cl->cl_q); 599 sp->qlimit = qlimit(cl->cl_q); 600 sp->period = cl->cl_period; 601 sp->xmitcnt = cl->cl_xmitcnt; 602 sp->dropcnt = cl->cl_dropcnt; 603 604 sp->qtype = qtype(cl->cl_q); 605 #ifdef ALTQ_RED 606 if (q_is_red(cl->cl_q)) 607 red_getstats(cl->cl_red, &sp->red[0]); 608 #endif 609 #ifdef ALTQ_RIO 610 if (q_is_rio(cl->cl_q)) 611 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]); 612 #endif 613 614 } 615 616 /* convert a class handle to the corresponding class pointer */ 617 static struct priq_class * 618 clh_to_clp(struct priq_if *pif, u_int32_t chandle) 619 { 620 struct priq_class *cl; 621 int idx; 622 623 if (chandle == 0) 624 return (NULL); 625 626 for (idx = pif->pif_maxpri; idx >= 0; idx--) 627 if ((cl = pif->pif_classes[idx]) != NULL && 628 cl->cl_handle == chandle) 629 return (cl); 630 631 return (NULL); 632 } 633 634 635 #ifdef ALTQ3_COMPAT 636 637 static struct priq_if * 638 priq_attach(struct ifaltq *ifq, u_int bandwidth) 639 { 640 struct priq_if *pif; 641 642 pif = malloc(sizeof(struct priq_if), M_DEVBUF, M_WAITOK|M_ZERO); 643 if (pif == NULL) 644 return (NULL); 645 pif->pif_bandwidth = bandwidth; 646 pif->pif_maxpri = -1; 647 pif->pif_ifq = ifq; 648 649 /* add this state to the priq list */ 650 pif->pif_next = pif_list; 651 pif_list = pif; 652 653 return (pif); 654 } 655 656 static void 657 priq_detach(struct priq_if *pif) 658 { 659 (void)priq_clear_interface(pif); 660 661 /* remove this interface from the pif list */ 662 if (pif_list == pif) 663 pif_list = pif->pif_next; 664 else { 665 struct priq_if *p; 666 667 for (p = pif_list; p != NULL; p = p->pif_next) 668 if (p->pif_next == pif) { 669 p->pif_next = pif->pif_next; 670 break; 671 } 672 ASSERT(p != NULL); 673 } 674 675 free(pif, M_DEVBUF); 676 } 677 678 /* 679 * priq device interface 680 */ 681 int 682 priqopen(dev_t dev, int flag, int fmt, 683 struct lwp *l) 684 { 685 /* everything will be done when the queueing scheme is attached. */ 686 return 0; 687 } 688 689 int 690 priqclose(dev_t dev, int flag, int fmt, 691 struct lwp *l) 692 { 693 struct priq_if *pif; 694 695 while ((pif = pif_list) != NULL) { 696 /* destroy all */ 697 if (ALTQ_IS_ENABLED(pif->pif_ifq)) 698 altq_disable(pif->pif_ifq); 699 700 int error = altq_detach(pif->pif_ifq); 701 switch (error) { 702 case 0: 703 case ENXIO: /* already disabled */ 704 break; 705 default: 706 return error; 707 } 708 priq_detach(pif); 709 } 710 711 return 0; 712 } 713 714 int 715 priqioctl(dev_t dev, ioctlcmd_t cmd, void *addr, int flag, 716 struct lwp *l) 717 { 718 struct priq_if *pif; 719 struct priq_interface *ifacep; 720 int error = 0; 721 722 /* check super-user privilege */ 723 switch (cmd) { 724 case PRIQ_GETSTATS: 725 break; 726 default: 727 #if (__FreeBSD_version > 400000) 728 if ((error = suser(p)) != 0) 729 return (error); 730 #else 731 if ((error = kauth_authorize_network(l->l_cred, 732 KAUTH_NETWORK_ALTQ, KAUTH_REQ_NETWORK_ALTQ_PRIQ, NULL, 733 NULL, NULL)) != 0) 734 return (error); 735 #endif 736 break; 737 } 738 739 switch (cmd) { 740 741 case PRIQ_IF_ATTACH: 742 error = priqcmd_if_attach((struct priq_interface *)addr); 743 break; 744 745 case PRIQ_IF_DETACH: 746 error = priqcmd_if_detach((struct priq_interface *)addr); 747 break; 748 749 case PRIQ_ENABLE: 750 case PRIQ_DISABLE: 751 case PRIQ_CLEAR: 752 ifacep = (struct priq_interface *)addr; 753 if ((pif = altq_lookup(ifacep->ifname, 754 ALTQT_PRIQ)) == NULL) { 755 error = EBADF; 756 break; 757 } 758 759 switch (cmd) { 760 case PRIQ_ENABLE: 761 if (pif->pif_default == NULL) { 762 #ifdef ALTQ_DEBUG 763 printf("priq: no default class\n"); 764 #endif 765 error = EINVAL; 766 break; 767 } 768 error = altq_enable(pif->pif_ifq); 769 break; 770 771 case PRIQ_DISABLE: 772 error = altq_disable(pif->pif_ifq); 773 break; 774 775 case PRIQ_CLEAR: 776 priq_clear_interface(pif); 777 break; 778 } 779 break; 780 781 case PRIQ_ADD_CLASS: 782 error = priqcmd_add_class((struct priq_add_class *)addr); 783 break; 784 785 case PRIQ_DEL_CLASS: 786 error = priqcmd_delete_class((struct priq_delete_class *)addr); 787 break; 788 789 case PRIQ_MOD_CLASS: 790 error = priqcmd_modify_class((struct priq_modify_class *)addr); 791 break; 792 793 case PRIQ_ADD_FILTER: 794 error = priqcmd_add_filter((struct priq_add_filter *)addr); 795 break; 796 797 case PRIQ_DEL_FILTER: 798 error = priqcmd_delete_filter((struct priq_delete_filter *)addr); 799 break; 800 801 case PRIQ_GETSTATS: 802 error = priqcmd_class_stats((struct priq_class_stats *)addr); 803 break; 804 805 default: 806 error = EINVAL; 807 break; 808 } 809 return error; 810 } 811 812 static int 813 priqcmd_if_attach(struct priq_interface *ap) 814 { 815 struct priq_if *pif; 816 struct ifnet *ifp; 817 int error; 818 819 if ((ifp = ifunit(ap->ifname)) == NULL) 820 return (ENXIO); 821 822 if ((pif = priq_attach(&ifp->if_snd, ap->arg)) == NULL) 823 return (ENOMEM); 824 825 /* 826 * set PRIQ to this ifnet structure. 827 */ 828 if ((error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, pif, 829 priq_enqueue, priq_dequeue, priq_request, 830 &pif->pif_classifier, acc_classify)) != 0) 831 priq_detach(pif); 832 833 return (error); 834 } 835 836 static int 837 priqcmd_if_detach(struct priq_interface *ap) 838 { 839 struct priq_if *pif; 840 int error; 841 842 if ((pif = altq_lookup(ap->ifname, ALTQT_PRIQ)) == NULL) 843 return (EBADF); 844 845 if (ALTQ_IS_ENABLED(pif->pif_ifq)) 846 altq_disable(pif->pif_ifq); 847 848 if ((error = altq_detach(pif->pif_ifq))) 849 return (error); 850 851 priq_detach(pif); 852 return 0; 853 } 854 855 static int 856 priqcmd_add_class(struct priq_add_class *ap) 857 { 858 struct priq_if *pif; 859 struct priq_class *cl; 860 int qid; 861 862 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL) 863 return (EBADF); 864 865 if (ap->pri < 0 || ap->pri >= PRIQ_MAXPRI) 866 return (EINVAL); 867 if (pif->pif_classes[ap->pri] != NULL) 868 return (EBUSY); 869 870 qid = ap->pri + 1; 871 if ((cl = priq_class_create(pif, ap->pri, 872 ap->qlimit, ap->flags, qid)) == NULL) 873 return (ENOMEM); 874 875 /* return a class handle to the user */ 876 ap->class_handle = cl->cl_handle; 877 878 return (0); 879 } 880 881 static int 882 priqcmd_delete_class(struct priq_delete_class *ap) 883 { 884 struct priq_if *pif; 885 struct priq_class *cl; 886 887 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL) 888 return (EBADF); 889 890 if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL) 891 return (EINVAL); 892 893 return priq_class_destroy(cl); 894 } 895 896 static int 897 priqcmd_modify_class(struct priq_modify_class *ap) 898 { 899 struct priq_if *pif; 900 struct priq_class *cl; 901 902 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL) 903 return (EBADF); 904 905 if (ap->pri < 0 || ap->pri >= PRIQ_MAXPRI) 906 return (EINVAL); 907 908 if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL) 909 return (EINVAL); 910 911 /* 912 * if priority is changed, move the class to the new priority 913 */ 914 if (pif->pif_classes[ap->pri] != cl) { 915 if (pif->pif_classes[ap->pri] != NULL) 916 return (EEXIST); 917 pif->pif_classes[cl->cl_pri] = NULL; 918 pif->pif_classes[ap->pri] = cl; 919 cl->cl_pri = ap->pri; 920 } 921 922 /* call priq_class_create to change class parameters */ 923 if ((cl = priq_class_create(pif, ap->pri, 924 ap->qlimit, ap->flags, ap->class_handle)) == NULL) 925 return (ENOMEM); 926 return 0; 927 } 928 929 static int 930 priqcmd_add_filter(struct priq_add_filter *ap) 931 { 932 struct priq_if *pif; 933 struct priq_class *cl; 934 935 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL) 936 return (EBADF); 937 938 if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL) 939 return (EINVAL); 940 941 return acc_add_filter(&pif->pif_classifier, &ap->filter, 942 cl, &ap->filter_handle); 943 } 944 945 static int 946 priqcmd_delete_filter(struct priq_delete_filter *ap) 947 { 948 struct priq_if *pif; 949 950 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL) 951 return (EBADF); 952 953 return acc_delete_filter(&pif->pif_classifier, 954 ap->filter_handle); 955 } 956 957 static int 958 priqcmd_class_stats(struct priq_class_stats *ap) 959 { 960 struct priq_if *pif; 961 struct priq_class *cl; 962 struct priq_classstats stats, *usp; 963 int pri, error; 964 965 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL) 966 return (EBADF); 967 968 ap->maxpri = pif->pif_maxpri; 969 970 /* then, read the next N classes in the tree */ 971 usp = ap->stats; 972 for (pri = 0; pri <= pif->pif_maxpri; pri++) { 973 cl = pif->pif_classes[pri]; 974 if (cl != NULL) 975 get_class_stats(&stats, cl); 976 else 977 memset(&stats, 0, sizeof(stats)); 978 if ((error = copyout((void *)&stats, (void *)usp++, 979 sizeof(stats))) != 0) 980 return (error); 981 } 982 return (0); 983 } 984 985 #ifdef KLD_MODULE 986 987 static struct altqsw priq_sw = 988 {"priq", priqopen, priqclose, priqioctl}; 989 990 ALTQ_MODULE(altq_priq, ALTQT_PRIQ, &priq_sw); 991 MODULE_DEPEND(altq_priq, altq_red, 1, 1, 1); 992 MODULE_DEPEND(altq_priq, altq_rio, 1, 1, 1); 993 994 #endif /* KLD_MODULE */ 995 996 #endif /* ALTQ3_COMPAT */ 997 #endif /* ALTQ_PRIQ */ 998