1 /* $KAME: altq_priq.c,v 1.12 2004/04/17 10:54:48 kjc Exp $ */ 2 /* $DragonFly: src/sys/net/altq/altq_priq.c,v 1.8 2006/12/22 23:44:55 swildner Exp $ */ 3 4 /* 5 * Copyright (C) 2000-2003 6 * Sony Computer Science Laboratories Inc. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 /* 30 * priority queue 31 */ 32 33 #include "opt_altq.h" 34 #include "opt_inet.h" 35 #include "opt_inet6.h" 36 37 #ifdef ALTQ_PRIQ /* priq is enabled by ALTQ_PRIQ 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/sockio.h> 44 #include <sys/systm.h> 45 #include <sys/proc.h> 46 #include <sys/errno.h> 47 #include <sys/kernel.h> 48 #include <sys/queue.h> 49 #include <sys/thread.h> 50 51 #include <net/if.h> 52 #include <net/ifq_var.h> 53 #include <netinet/in.h> 54 55 #include <net/pf/pfvar.h> 56 #include <net/altq/altq.h> 57 #include <net/altq/altq_priq.h> 58 59 #include <sys/thread2.h> 60 61 /* 62 * function prototypes 63 */ 64 static int priq_clear_interface(struct priq_if *); 65 static int priq_request(struct ifaltq *, int, void *); 66 static void priq_purge(struct priq_if *); 67 static struct priq_class *priq_class_create(struct priq_if *, int, int, int, int); 68 static int priq_class_destroy(struct priq_class *); 69 static int priq_enqueue(struct ifaltq *, struct mbuf *, struct altq_pktattr *); 70 static struct mbuf *priq_dequeue(struct ifaltq *, struct mbuf *, int); 71 72 static int priq_addq(struct priq_class *, struct mbuf *); 73 static struct mbuf *priq_getq(struct priq_class *); 74 static struct mbuf *priq_pollq(struct priq_class *); 75 static void priq_purgeq(struct priq_class *); 76 77 static void get_class_stats(struct priq_classstats *, struct priq_class *); 78 static struct priq_class *clh_to_clp(struct priq_if *, uint32_t); 79 80 int 81 priq_pfattach(struct pf_altq *a) 82 { 83 struct ifnet *ifp; 84 int error; 85 86 if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL) 87 return (EINVAL); 88 crit_enter(); 89 error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, a->altq_disc, 90 priq_enqueue, priq_dequeue, priq_request, NULL, NULL); 91 crit_exit(); 92 return (error); 93 } 94 95 int 96 priq_add_altq(struct pf_altq *a) 97 { 98 struct priq_if *pif; 99 struct ifnet *ifp; 100 101 if ((ifp = ifunit(a->ifname)) == NULL) 102 return (EINVAL); 103 if (!ifq_is_ready(&ifp->if_snd)) 104 return (ENODEV); 105 106 pif = kmalloc(sizeof(*pif), M_ALTQ, M_WAITOK | M_ZERO); 107 pif->pif_bandwidth = a->ifbandwidth; 108 pif->pif_maxpri = -1; 109 pif->pif_ifq = &ifp->if_snd; 110 ifq_purge(&ifp->if_snd); 111 112 /* keep the state in pf_altq */ 113 a->altq_disc = pif; 114 115 return (0); 116 } 117 118 int 119 priq_remove_altq(struct pf_altq *a) 120 { 121 struct priq_if *pif; 122 123 if ((pif = a->altq_disc) == NULL) 124 return (EINVAL); 125 a->altq_disc = NULL; 126 127 priq_clear_interface(pif); 128 129 kfree(pif, M_ALTQ); 130 return (0); 131 } 132 133 int 134 priq_add_queue(struct pf_altq *a) 135 { 136 struct priq_if *pif; 137 struct priq_class *cl; 138 139 if ((pif = a->altq_disc) == NULL) 140 return (EINVAL); 141 142 /* check parameters */ 143 if (a->priority >= PRIQ_MAXPRI) 144 return (EINVAL); 145 if (a->qid == 0) 146 return (EINVAL); 147 if (pif->pif_classes[a->priority] != NULL) 148 return (EBUSY); 149 if (clh_to_clp(pif, a->qid) != NULL) 150 return (EBUSY); 151 152 cl = priq_class_create(pif, a->priority, a->qlimit, 153 a->pq_u.priq_opts.flags, a->qid); 154 if (cl == NULL) 155 return (ENOMEM); 156 157 return (0); 158 } 159 160 int 161 priq_remove_queue(struct pf_altq *a) 162 { 163 struct priq_if *pif; 164 struct priq_class *cl; 165 166 if ((pif = a->altq_disc) == NULL) 167 return (EINVAL); 168 169 if ((cl = clh_to_clp(pif, a->qid)) == NULL) 170 return (EINVAL); 171 172 return (priq_class_destroy(cl)); 173 } 174 175 int 176 priq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes) 177 { 178 struct priq_if *pif; 179 struct priq_class *cl; 180 struct priq_classstats stats; 181 int error = 0; 182 183 if ((pif = altq_lookup(a->ifname, ALTQT_PRIQ)) == NULL) 184 return (EBADF); 185 186 if ((cl = clh_to_clp(pif, a->qid)) == NULL) 187 return (EINVAL); 188 189 if (*nbytes < sizeof(stats)) 190 return (EINVAL); 191 192 get_class_stats(&stats, cl); 193 194 if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0) 195 return (error); 196 *nbytes = sizeof(stats); 197 return (0); 198 } 199 200 /* 201 * bring the interface back to the initial state by discarding 202 * all the filters and classes. 203 */ 204 static int 205 priq_clear_interface(struct priq_if *pif) 206 { 207 struct priq_class *cl; 208 int pri; 209 210 /* clear out the classes */ 211 for (pri = 0; pri <= pif->pif_maxpri; pri++) { 212 if ((cl = pif->pif_classes[pri]) != NULL) 213 priq_class_destroy(cl); 214 } 215 216 return (0); 217 } 218 219 static int 220 priq_request(struct ifaltq *ifq, int req, void *arg) 221 { 222 struct priq_if *pif = (struct priq_if *)ifq->altq_disc; 223 224 crit_enter(); 225 switch (req) { 226 case ALTRQ_PURGE: 227 priq_purge(pif); 228 break; 229 } 230 crit_exit(); 231 return (0); 232 } 233 234 /* discard all the queued packets on the interface */ 235 static void 236 priq_purge(struct priq_if *pif) 237 { 238 struct priq_class *cl; 239 int pri; 240 241 for (pri = 0; pri <= pif->pif_maxpri; pri++) { 242 if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q)) 243 priq_purgeq(cl); 244 } 245 if (ifq_is_enabled(pif->pif_ifq)) 246 pif->pif_ifq->ifq_len = 0; 247 } 248 249 static struct priq_class * 250 priq_class_create(struct priq_if *pif, int pri, int qlimit, int flags, int qid) 251 { 252 struct priq_class *cl; 253 254 #ifndef ALTQ_RED 255 if (flags & PRCF_RED) { 256 #ifdef ALTQ_DEBUG 257 kprintf("priq_class_create: RED not configured for PRIQ!\n"); 258 #endif 259 return (NULL); 260 } 261 #endif 262 263 if ((cl = pif->pif_classes[pri]) != NULL) { 264 /* modify the class instead of creating a new one */ 265 crit_enter(); 266 if (!qempty(cl->cl_q)) 267 priq_purgeq(cl); 268 crit_exit(); 269 #ifdef ALTQ_RIO 270 if (q_is_rio(cl->cl_q)) 271 rio_destroy((rio_t *)cl->cl_red); 272 #endif 273 #ifdef ALTQ_RED 274 if (q_is_red(cl->cl_q)) 275 red_destroy(cl->cl_red); 276 #endif 277 } else { 278 cl = kmalloc(sizeof(*cl), M_ALTQ, M_WAITOK | M_ZERO); 279 cl->cl_q = kmalloc(sizeof(*cl->cl_q), M_ALTQ, M_WAITOK | M_ZERO); 280 } 281 282 pif->pif_classes[pri] = cl; 283 if (flags & PRCF_DEFAULTCLASS) 284 pif->pif_default = cl; 285 if (qlimit == 0) 286 qlimit = 50; /* use default */ 287 qlimit(cl->cl_q) = qlimit; 288 qtype(cl->cl_q) = Q_DROPTAIL; 289 qlen(cl->cl_q) = 0; 290 cl->cl_flags = flags; 291 cl->cl_pri = pri; 292 if (pri > pif->pif_maxpri) 293 pif->pif_maxpri = pri; 294 cl->cl_pif = pif; 295 cl->cl_handle = qid; 296 297 #ifdef ALTQ_RED 298 if (flags & (PRCF_RED|PRCF_RIO)) { 299 int red_flags, red_pkttime; 300 301 red_flags = 0; 302 if (flags & PRCF_ECN) 303 red_flags |= REDF_ECN; 304 #ifdef ALTQ_RIO 305 if (flags & PRCF_CLEARDSCP) 306 red_flags |= RIOF_CLEARDSCP; 307 #endif 308 if (pif->pif_bandwidth < 8) 309 red_pkttime = 1000 * 1000 * 1000; /* 1 sec */ 310 else 311 red_pkttime = (int64_t)pif->pif_ifq->altq_ifp->if_mtu 312 * 1000 * 1000 * 1000 / (pif->pif_bandwidth / 8); 313 #ifdef ALTQ_RIO 314 if (flags & PRCF_RIO) { 315 cl->cl_red = (red_t *)rio_alloc(0, NULL, 316 red_flags, red_pkttime); 317 if (cl->cl_red != NULL) 318 qtype(cl->cl_q) = Q_RIO; 319 } else 320 #endif 321 if (flags & PRCF_RED) { 322 cl->cl_red = red_alloc(0, 0, 323 qlimit(cl->cl_q) * 10/100, 324 qlimit(cl->cl_q) * 30/100, 325 red_flags, red_pkttime); 326 if (cl->cl_red != NULL) 327 qtype(cl->cl_q) = Q_RED; 328 } 329 } 330 #endif /* ALTQ_RED */ 331 332 return (cl); 333 } 334 335 static int 336 priq_class_destroy(struct priq_class *cl) 337 { 338 struct priq_if *pif; 339 int pri; 340 341 crit_enter(); 342 343 if (!qempty(cl->cl_q)) 344 priq_purgeq(cl); 345 346 pif = cl->cl_pif; 347 pif->pif_classes[cl->cl_pri] = NULL; 348 if (pif->pif_maxpri == cl->cl_pri) { 349 for (pri = cl->cl_pri; pri >= 0; pri--) 350 if (pif->pif_classes[pri] != NULL) { 351 pif->pif_maxpri = pri; 352 break; 353 } 354 if (pri < 0) 355 pif->pif_maxpri = -1; 356 } 357 crit_exit(); 358 359 if (cl->cl_red != NULL) { 360 #ifdef ALTQ_RIO 361 if (q_is_rio(cl->cl_q)) 362 rio_destroy((rio_t *)cl->cl_red); 363 #endif 364 #ifdef ALTQ_RED 365 if (q_is_red(cl->cl_q)) 366 red_destroy(cl->cl_red); 367 #endif 368 } 369 kfree(cl->cl_q, M_ALTQ); 370 kfree(cl, M_ALTQ); 371 return (0); 372 } 373 374 /* 375 * priq_enqueue is an enqueue function to be registered to 376 * (*altq_enqueue) in struct ifaltq. 377 */ 378 static int 379 priq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr) 380 { 381 struct priq_if *pif = (struct priq_if *)ifq->altq_disc; 382 struct priq_class *cl; 383 int error; 384 int len; 385 386 crit_enter(); 387 388 /* grab class set by classifier */ 389 if ((m->m_flags & M_PKTHDR) == 0) { 390 /* should not happen */ 391 if_printf(ifq->altq_ifp, "altq: packet does not have pkthdr\n"); 392 m_freem(m); 393 error = ENOBUFS; 394 goto done; 395 } 396 397 if (m->m_pkthdr.fw_flags & ALTQ_MBUF_TAGGED) 398 cl = clh_to_clp(pif, m->m_pkthdr.altq_qid); 399 else 400 cl = NULL; 401 if (cl == NULL) { 402 cl = pif->pif_default; 403 if (cl == NULL) { 404 m_freem(m); 405 error = ENOBUFS; 406 goto done; 407 } 408 } 409 cl->cl_pktattr = NULL; 410 len = m_pktlen(m); 411 if (priq_addq(cl, m) != 0) { 412 /* drop occurred. mbuf was freed in priq_addq. */ 413 PKTCNTR_ADD(&cl->cl_dropcnt, len); 414 error = ENOBUFS; 415 goto done; 416 } 417 ifq->ifq_len++; 418 error = 0; 419 done: 420 crit_exit(); 421 return (error); 422 } 423 424 /* 425 * priq_dequeue is a dequeue function to be registered to 426 * (*altq_dequeue) in struct ifaltq. 427 * 428 * note: ALTDQ_POLL returns the next packet without removing the packet 429 * from the queue. ALTDQ_REMOVE is a normal dequeue operation. 430 * ALTDQ_REMOVE must return the same packet if called immediately 431 * after ALTDQ_POLL. 432 */ 433 static struct mbuf * 434 priq_dequeue(struct ifaltq *ifq, struct mbuf *mpolled, int op) 435 { 436 struct priq_if *pif = (struct priq_if *)ifq->altq_disc; 437 struct priq_class *cl; 438 struct mbuf *m; 439 int pri; 440 441 if (ifq_is_empty(ifq)) { 442 /* no packet in the queue */ 443 KKASSERT(mpolled == NULL); 444 return (NULL); 445 } 446 447 crit_enter(); 448 m = NULL; 449 for (pri = pif->pif_maxpri; pri >= 0; pri--) { 450 if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q)) { 451 if (op == ALTDQ_POLL) { 452 m = priq_pollq(cl); 453 break; 454 } 455 456 m = priq_getq(cl); 457 if (m != NULL) { 458 ifq->ifq_len--; 459 if (qempty(cl->cl_q)) 460 cl->cl_period++; 461 PKTCNTR_ADD(&cl->cl_xmitcnt, m_pktlen(m)); 462 } 463 break; 464 } 465 } 466 crit_exit(); 467 KKASSERT(mpolled == NULL || mpolled == m); 468 return (m); 469 } 470 471 static int 472 priq_addq(struct priq_class *cl, struct mbuf *m) 473 { 474 #ifdef ALTQ_RIO 475 if (q_is_rio(cl->cl_q)) 476 return rio_addq((rio_t *)cl->cl_red, cl->cl_q, m, 477 cl->cl_pktattr); 478 #endif 479 #ifdef ALTQ_RED 480 if (q_is_red(cl->cl_q)) 481 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr); 482 #endif 483 if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) { 484 m_freem(m); 485 return (-1); 486 } 487 488 if (cl->cl_flags & PRCF_CLEARDSCP) 489 write_dsfield(m, cl->cl_pktattr, 0); 490 491 _addq(cl->cl_q, m); 492 493 return (0); 494 } 495 496 static struct mbuf * 497 priq_getq(struct priq_class *cl) 498 { 499 #ifdef ALTQ_RIO 500 if (q_is_rio(cl->cl_q)) 501 return rio_getq((rio_t *)cl->cl_red, cl->cl_q); 502 #endif 503 #ifdef ALTQ_RED 504 if (q_is_red(cl->cl_q)) 505 return red_getq(cl->cl_red, cl->cl_q); 506 #endif 507 return _getq(cl->cl_q); 508 } 509 510 static struct mbuf * 511 priq_pollq(struct priq_class *cl) 512 { 513 return qhead(cl->cl_q); 514 } 515 516 static void 517 priq_purgeq(struct priq_class *cl) 518 { 519 struct mbuf *m; 520 521 if (qempty(cl->cl_q)) 522 return; 523 524 while ((m = _getq(cl->cl_q)) != NULL) { 525 PKTCNTR_ADD(&cl->cl_dropcnt, m_pktlen(m)); 526 m_freem(m); 527 } 528 KKASSERT(qlen(cl->cl_q) == 0); 529 } 530 531 static void 532 get_class_stats(struct priq_classstats *sp, struct priq_class *cl) 533 { 534 sp->class_handle = cl->cl_handle; 535 sp->qlength = qlen(cl->cl_q); 536 sp->qlimit = qlimit(cl->cl_q); 537 sp->period = cl->cl_period; 538 sp->xmitcnt = cl->cl_xmitcnt; 539 sp->dropcnt = cl->cl_dropcnt; 540 541 sp->qtype = qtype(cl->cl_q); 542 #ifdef ALTQ_RED 543 if (q_is_red(cl->cl_q)) 544 red_getstats(cl->cl_red, &sp->red[0]); 545 #endif 546 #ifdef ALTQ_RIO 547 if (q_is_rio(cl->cl_q)) 548 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]); 549 #endif 550 } 551 552 /* convert a class handle to the corresponding class pointer */ 553 static struct priq_class * 554 clh_to_clp(struct priq_if *pif, uint32_t chandle) 555 { 556 struct priq_class *cl; 557 int idx; 558 559 if (chandle == 0) 560 return (NULL); 561 562 for (idx = pif->pif_maxpri; idx >= 0; idx--) 563 if ((cl = pif->pif_classes[idx]) != NULL && 564 cl->cl_handle == chandle) 565 return (cl); 566 567 return (NULL); 568 } 569 570 #endif /* ALTQ_PRIQ */ 571