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.5 2005/11/22 00:24:35 dillon 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 = malloc(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 111 /* keep the state in pf_altq */ 112 a->altq_disc = pif; 113 114 return (0); 115 } 116 117 int 118 priq_remove_altq(struct pf_altq *a) 119 { 120 struct priq_if *pif; 121 122 if ((pif = a->altq_disc) == NULL) 123 return (EINVAL); 124 a->altq_disc = NULL; 125 126 priq_clear_interface(pif); 127 128 free(pif, M_ALTQ); 129 return (0); 130 } 131 132 int 133 priq_add_queue(struct pf_altq *a) 134 { 135 struct priq_if *pif; 136 struct priq_class *cl; 137 138 if ((pif = a->altq_disc) == NULL) 139 return (EINVAL); 140 141 /* check parameters */ 142 if (a->priority >= PRIQ_MAXPRI) 143 return (EINVAL); 144 if (a->qid == 0) 145 return (EINVAL); 146 if (pif->pif_classes[a->priority] != NULL) 147 return (EBUSY); 148 if (clh_to_clp(pif, a->qid) != NULL) 149 return (EBUSY); 150 151 cl = priq_class_create(pif, a->priority, a->qlimit, 152 a->pq_u.priq_opts.flags, a->qid); 153 if (cl == NULL) 154 return (ENOMEM); 155 156 return (0); 157 } 158 159 int 160 priq_remove_queue(struct pf_altq *a) 161 { 162 struct priq_if *pif; 163 struct priq_class *cl; 164 165 if ((pif = a->altq_disc) == NULL) 166 return (EINVAL); 167 168 if ((cl = clh_to_clp(pif, a->qid)) == NULL) 169 return (EINVAL); 170 171 return (priq_class_destroy(cl)); 172 } 173 174 int 175 priq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes) 176 { 177 struct priq_if *pif; 178 struct priq_class *cl; 179 struct priq_classstats stats; 180 int error = 0; 181 182 if ((pif = altq_lookup(a->ifname, ALTQT_PRIQ)) == NULL) 183 return (EBADF); 184 185 if ((cl = clh_to_clp(pif, a->qid)) == NULL) 186 return (EINVAL); 187 188 if (*nbytes < sizeof(stats)) 189 return (EINVAL); 190 191 get_class_stats(&stats, cl); 192 193 if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0) 194 return (error); 195 *nbytes = sizeof(stats); 196 return (0); 197 } 198 199 /* 200 * bring the interface back to the initial state by discarding 201 * all the filters and classes. 202 */ 203 static int 204 priq_clear_interface(struct priq_if *pif) 205 { 206 struct priq_class *cl; 207 int pri; 208 209 /* clear out the classes */ 210 for (pri = 0; pri <= pif->pif_maxpri; pri++) { 211 if ((cl = pif->pif_classes[pri]) != NULL) 212 priq_class_destroy(cl); 213 } 214 215 return (0); 216 } 217 218 static int 219 priq_request(struct ifaltq *ifq, int req, void *arg) 220 { 221 struct priq_if *pif = (struct priq_if *)ifq->altq_disc; 222 223 crit_enter(); 224 switch (req) { 225 case ALTRQ_PURGE: 226 priq_purge(pif); 227 break; 228 } 229 crit_exit(); 230 return (0); 231 } 232 233 /* discard all the queued packets on the interface */ 234 static void 235 priq_purge(struct priq_if *pif) 236 { 237 struct priq_class *cl; 238 int pri; 239 240 for (pri = 0; pri <= pif->pif_maxpri; pri++) { 241 if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q)) 242 priq_purgeq(cl); 243 } 244 if (ifq_is_enabled(pif->pif_ifq)) 245 pif->pif_ifq->ifq_len = 0; 246 } 247 248 static struct priq_class * 249 priq_class_create(struct priq_if *pif, int pri, int qlimit, int flags, int qid) 250 { 251 struct priq_class *cl; 252 253 #ifndef ALTQ_RED 254 if (flags & PRCF_RED) { 255 #ifdef ALTQ_DEBUG 256 printf("priq_class_create: RED not configured for PRIQ!\n"); 257 #endif 258 return (NULL); 259 } 260 #endif 261 262 if ((cl = pif->pif_classes[pri]) != NULL) { 263 /* modify the class instead of creating a new one */ 264 crit_enter(); 265 if (!qempty(cl->cl_q)) 266 priq_purgeq(cl); 267 crit_exit(); 268 #ifdef ALTQ_RIO 269 if (q_is_rio(cl->cl_q)) 270 rio_destroy((rio_t *)cl->cl_red); 271 #endif 272 #ifdef ALTQ_RED 273 if (q_is_red(cl->cl_q)) 274 red_destroy(cl->cl_red); 275 #endif 276 } else { 277 cl = malloc(sizeof(*cl), M_ALTQ, M_WAITOK | M_ZERO); 278 cl->cl_q = malloc(sizeof(*cl->cl_q), M_ALTQ, M_WAITOK | M_ZERO); 279 } 280 281 pif->pif_classes[pri] = cl; 282 if (flags & PRCF_DEFAULTCLASS) 283 pif->pif_default = cl; 284 if (qlimit == 0) 285 qlimit = 50; /* use default */ 286 qlimit(cl->cl_q) = qlimit; 287 qtype(cl->cl_q) = Q_DROPTAIL; 288 qlen(cl->cl_q) = 0; 289 cl->cl_flags = flags; 290 cl->cl_pri = pri; 291 if (pri > pif->pif_maxpri) 292 pif->pif_maxpri = pri; 293 cl->cl_pif = pif; 294 cl->cl_handle = qid; 295 296 #ifdef ALTQ_RED 297 if (flags & (PRCF_RED|PRCF_RIO)) { 298 int red_flags, red_pkttime; 299 300 red_flags = 0; 301 if (flags & PRCF_ECN) 302 red_flags |= REDF_ECN; 303 #ifdef ALTQ_RIO 304 if (flags & PRCF_CLEARDSCP) 305 red_flags |= RIOF_CLEARDSCP; 306 #endif 307 if (pif->pif_bandwidth < 8) 308 red_pkttime = 1000 * 1000 * 1000; /* 1 sec */ 309 else 310 red_pkttime = (int64_t)pif->pif_ifq->altq_ifp->if_mtu 311 * 1000 * 1000 * 1000 / (pif->pif_bandwidth / 8); 312 #ifdef ALTQ_RIO 313 if (flags & PRCF_RIO) { 314 cl->cl_red = (red_t *)rio_alloc(0, NULL, 315 red_flags, red_pkttime); 316 if (cl->cl_red != NULL) 317 qtype(cl->cl_q) = Q_RIO; 318 } else 319 #endif 320 if (flags & PRCF_RED) { 321 cl->cl_red = red_alloc(0, 0, 322 qlimit(cl->cl_q) * 10/100, 323 qlimit(cl->cl_q) * 30/100, 324 red_flags, red_pkttime); 325 if (cl->cl_red != NULL) 326 qtype(cl->cl_q) = Q_RED; 327 } 328 } 329 #endif /* ALTQ_RED */ 330 331 return (cl); 332 } 333 334 static int 335 priq_class_destroy(struct priq_class *cl) 336 { 337 struct priq_if *pif; 338 int pri; 339 340 crit_enter(); 341 342 if (!qempty(cl->cl_q)) 343 priq_purgeq(cl); 344 345 pif = cl->cl_pif; 346 pif->pif_classes[cl->cl_pri] = NULL; 347 if (pif->pif_maxpri == cl->cl_pri) { 348 for (pri = cl->cl_pri; pri >= 0; pri--) 349 if (pif->pif_classes[pri] != NULL) { 350 pif->pif_maxpri = pri; 351 break; 352 } 353 if (pri < 0) 354 pif->pif_maxpri = -1; 355 } 356 crit_exit(); 357 358 if (cl->cl_red != NULL) { 359 #ifdef ALTQ_RIO 360 if (q_is_rio(cl->cl_q)) 361 rio_destroy((rio_t *)cl->cl_red); 362 #endif 363 #ifdef ALTQ_RED 364 if (q_is_red(cl->cl_q)) 365 red_destroy(cl->cl_red); 366 #endif 367 } 368 free(cl->cl_q, M_ALTQ); 369 free(cl, M_ALTQ); 370 return (0); 371 } 372 373 /* 374 * priq_enqueue is an enqueue function to be registered to 375 * (*altq_enqueue) in struct ifaltq. 376 */ 377 static int 378 priq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr) 379 { 380 struct priq_if *pif = (struct priq_if *)ifq->altq_disc; 381 struct priq_class *cl; 382 int error; 383 int len; 384 385 crit_enter(); 386 387 /* grab class set by classifier */ 388 if ((m->m_flags & M_PKTHDR) == 0) { 389 /* should not happen */ 390 if_printf(ifq->altq_ifp, "altq: packet does not have pkthdr\n"); 391 m_freem(m); 392 error = ENOBUFS; 393 goto done; 394 } 395 396 if (m->m_pkthdr.fw_flags & ALTQ_MBUF_TAGGED) 397 cl = clh_to_clp(pif, m->m_pkthdr.altq_qid); 398 else 399 cl = NULL; 400 if (cl == NULL) { 401 cl = pif->pif_default; 402 if (cl == NULL) { 403 m_freem(m); 404 error = ENOBUFS; 405 goto done; 406 } 407 } 408 cl->cl_pktattr = NULL; 409 len = m_pktlen(m); 410 if (priq_addq(cl, m) != 0) { 411 /* drop occurred. mbuf was freed in priq_addq. */ 412 PKTCNTR_ADD(&cl->cl_dropcnt, len); 413 error = ENOBUFS; 414 goto done; 415 } 416 ifq->ifq_len++; 417 error = 0; 418 done: 419 crit_exit(); 420 return (error); 421 } 422 423 /* 424 * priq_dequeue is a dequeue function to be registered to 425 * (*altq_dequeue) in struct ifaltq. 426 * 427 * note: ALTDQ_POLL returns the next packet without removing the packet 428 * from the queue. ALTDQ_REMOVE is a normal dequeue operation. 429 * ALTDQ_REMOVE must return the same packet if called immediately 430 * after ALTDQ_POLL. 431 */ 432 static struct mbuf * 433 priq_dequeue(struct ifaltq *ifq, struct mbuf *mpolled, int op) 434 { 435 struct priq_if *pif = (struct priq_if *)ifq->altq_disc; 436 struct priq_class *cl; 437 struct mbuf *m; 438 int pri; 439 440 if (ifq_is_empty(ifq)) { 441 /* no packet in the queue */ 442 KKASSERT(mpolled == NULL); 443 return (NULL); 444 } 445 446 crit_enter(); 447 m = NULL; 448 for (pri = pif->pif_maxpri; pri >= 0; pri--) { 449 if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q)) { 450 if (op == ALTDQ_POLL) { 451 m = priq_pollq(cl); 452 break; 453 } 454 455 m = priq_getq(cl); 456 if (m != NULL) { 457 ifq->ifq_len--; 458 if (qempty(cl->cl_q)) 459 cl->cl_period++; 460 PKTCNTR_ADD(&cl->cl_xmitcnt, m_pktlen(m)); 461 } 462 break; 463 } 464 } 465 crit_exit(); 466 KKASSERT(mpolled == NULL || mpolled == m); 467 return (m); 468 } 469 470 static int 471 priq_addq(struct priq_class *cl, struct mbuf *m) 472 { 473 #ifdef ALTQ_RIO 474 if (q_is_rio(cl->cl_q)) 475 return rio_addq((rio_t *)cl->cl_red, cl->cl_q, m, 476 cl->cl_pktattr); 477 #endif 478 #ifdef ALTQ_RED 479 if (q_is_red(cl->cl_q)) 480 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr); 481 #endif 482 if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) { 483 m_freem(m); 484 return (-1); 485 } 486 487 if (cl->cl_flags & PRCF_CLEARDSCP) 488 write_dsfield(m, cl->cl_pktattr, 0); 489 490 _addq(cl->cl_q, m); 491 492 return (0); 493 } 494 495 static struct mbuf * 496 priq_getq(struct priq_class *cl) 497 { 498 #ifdef ALTQ_RIO 499 if (q_is_rio(cl->cl_q)) 500 return rio_getq((rio_t *)cl->cl_red, cl->cl_q); 501 #endif 502 #ifdef ALTQ_RED 503 if (q_is_red(cl->cl_q)) 504 return red_getq(cl->cl_red, cl->cl_q); 505 #endif 506 return _getq(cl->cl_q); 507 } 508 509 static struct mbuf * 510 priq_pollq(struct priq_class *cl) 511 { 512 return qhead(cl->cl_q); 513 } 514 515 static void 516 priq_purgeq(struct priq_class *cl) 517 { 518 struct mbuf *m; 519 520 if (qempty(cl->cl_q)) 521 return; 522 523 while ((m = _getq(cl->cl_q)) != NULL) { 524 PKTCNTR_ADD(&cl->cl_dropcnt, m_pktlen(m)); 525 m_freem(m); 526 } 527 KKASSERT(qlen(cl->cl_q) == 0); 528 } 529 530 static void 531 get_class_stats(struct priq_classstats *sp, struct priq_class *cl) 532 { 533 sp->class_handle = cl->cl_handle; 534 sp->qlength = qlen(cl->cl_q); 535 sp->qlimit = qlimit(cl->cl_q); 536 sp->period = cl->cl_period; 537 sp->xmitcnt = cl->cl_xmitcnt; 538 sp->dropcnt = cl->cl_dropcnt; 539 540 sp->qtype = qtype(cl->cl_q); 541 #ifdef ALTQ_RED 542 if (q_is_red(cl->cl_q)) 543 red_getstats(cl->cl_red, &sp->red[0]); 544 #endif 545 #ifdef ALTQ_RIO 546 if (q_is_rio(cl->cl_q)) 547 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]); 548 #endif 549 } 550 551 /* convert a class handle to the corresponding class pointer */ 552 static struct priq_class * 553 clh_to_clp(struct priq_if *pif, uint32_t chandle) 554 { 555 struct priq_class *cl; 556 int idx; 557 558 if (chandle == 0) 559 return (NULL); 560 561 for (idx = pif->pif_maxpri; idx >= 0; idx--) 562 if ((cl = pif->pif_classes[idx]) != NULL && 563 cl->cl_handle == chandle) 564 return (cl); 565 566 return (NULL); 567 } 568 569 #endif /* ALTQ_PRIQ */ 570