1 /* 2 * net/sched/sch_prio.c Simple 3-band priority "scheduler". 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation; either version 7 * 2 of the License, or (at your option) any later version. 8 * 9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 10 * Fixes: 19990609: J Hadi Salim <hadi@nortelnetworks.com>: 11 * Init -- EINVAL when opt undefined 12 */ 13 14 #include <linux/config.h> 15 #include <linux/module.h> 16 #include <asm/uaccess.h> 17 #include <asm/system.h> 18 #include <linux/bitops.h> 19 #include <linux/types.h> 20 #include <linux/kernel.h> 21 #include <linux/sched.h> 22 #include <linux/string.h> 23 #include <linux/mm.h> 24 #include <linux/socket.h> 25 #include <linux/sockios.h> 26 #include <linux/in.h> 27 #include <linux/errno.h> 28 #include <linux/interrupt.h> 29 #include <linux/if_ether.h> 30 #include <linux/inet.h> 31 #include <linux/netdevice.h> 32 #include <linux/etherdevice.h> 33 #include <linux/notifier.h> 34 #include <net/ip.h> 35 #include <net/route.h> 36 #include <linux/skbuff.h> 37 #include <net/sock.h> 38 #include <net/pkt_sched.h> 39 40 41 struct prio_sched_data 42 { 43 int bands; 44 struct tcf_proto *filter_list; 45 u8 prio2band[TC_PRIO_MAX+1]; 46 struct Qdisc *queues[TCQ_PRIO_BANDS]; 47 }; 48 49 50 static struct Qdisc * 51 prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr) 52 { 53 struct prio_sched_data *q = qdisc_priv(sch); 54 u32 band = skb->priority; 55 struct tcf_result res; 56 57 *qerr = NET_XMIT_DROP; 58 if (TC_H_MAJ(skb->priority) != sch->handle) { 59 #ifdef CONFIG_NET_CLS_ACT 60 switch (tc_classify(skb, q->filter_list, &res)) { 61 case TC_ACT_STOLEN: 62 case TC_ACT_QUEUED: 63 *qerr = NET_XMIT_SUCCESS; 64 case TC_ACT_SHOT: 65 return NULL; 66 }; 67 68 if (!q->filter_list ) { 69 #else 70 if (!q->filter_list || tc_classify(skb, q->filter_list, &res)) { 71 #endif 72 if (TC_H_MAJ(band)) 73 band = 0; 74 return q->queues[q->prio2band[band&TC_PRIO_MAX]]; 75 } 76 band = res.classid; 77 } 78 band = TC_H_MIN(band) - 1; 79 if (band > q->bands) 80 return q->queues[q->prio2band[0]]; 81 82 return q->queues[band]; 83 } 84 85 static int 86 prio_enqueue(struct sk_buff *skb, struct Qdisc *sch) 87 { 88 struct Qdisc *qdisc; 89 int ret; 90 91 qdisc = prio_classify(skb, sch, &ret); 92 #ifdef CONFIG_NET_CLS_ACT 93 if (qdisc == NULL) { 94 if (ret == NET_XMIT_DROP) 95 sch->qstats.drops++; 96 kfree_skb(skb); 97 return ret; 98 } 99 #endif 100 101 if ((ret = qdisc->enqueue(skb, qdisc)) == NET_XMIT_SUCCESS) { 102 sch->bstats.bytes += skb->len; 103 sch->bstats.packets++; 104 sch->q.qlen++; 105 return NET_XMIT_SUCCESS; 106 } 107 sch->qstats.drops++; 108 return ret; 109 } 110 111 112 static int 113 prio_requeue(struct sk_buff *skb, struct Qdisc* sch) 114 { 115 struct Qdisc *qdisc; 116 int ret; 117 118 qdisc = prio_classify(skb, sch, &ret); 119 #ifdef CONFIG_NET_CLS_ACT 120 if (qdisc == NULL) { 121 if (ret == NET_XMIT_DROP) 122 sch->qstats.drops++; 123 kfree_skb(skb); 124 return ret; 125 } 126 #endif 127 128 if ((ret = qdisc->ops->requeue(skb, qdisc)) == NET_XMIT_SUCCESS) { 129 sch->q.qlen++; 130 sch->qstats.requeues++; 131 return 0; 132 } 133 sch->qstats.drops++; 134 return NET_XMIT_DROP; 135 } 136 137 138 static struct sk_buff * 139 prio_dequeue(struct Qdisc* sch) 140 { 141 struct sk_buff *skb; 142 struct prio_sched_data *q = qdisc_priv(sch); 143 int prio; 144 struct Qdisc *qdisc; 145 146 for (prio = 0; prio < q->bands; prio++) { 147 qdisc = q->queues[prio]; 148 skb = qdisc->dequeue(qdisc); 149 if (skb) { 150 sch->q.qlen--; 151 return skb; 152 } 153 } 154 return NULL; 155 156 } 157 158 static unsigned int prio_drop(struct Qdisc* sch) 159 { 160 struct prio_sched_data *q = qdisc_priv(sch); 161 int prio; 162 unsigned int len; 163 struct Qdisc *qdisc; 164 165 for (prio = q->bands-1; prio >= 0; prio--) { 166 qdisc = q->queues[prio]; 167 if ((len = qdisc->ops->drop(qdisc)) != 0) { 168 sch->q.qlen--; 169 return len; 170 } 171 } 172 return 0; 173 } 174 175 176 static void 177 prio_reset(struct Qdisc* sch) 178 { 179 int prio; 180 struct prio_sched_data *q = qdisc_priv(sch); 181 182 for (prio=0; prio<q->bands; prio++) 183 qdisc_reset(q->queues[prio]); 184 sch->q.qlen = 0; 185 } 186 187 static void 188 prio_destroy(struct Qdisc* sch) 189 { 190 int prio; 191 struct prio_sched_data *q = qdisc_priv(sch); 192 struct tcf_proto *tp; 193 194 while ((tp = q->filter_list) != NULL) { 195 q->filter_list = tp->next; 196 tcf_destroy(tp); 197 } 198 199 for (prio=0; prio<q->bands; prio++) 200 qdisc_destroy(q->queues[prio]); 201 } 202 203 static int prio_tune(struct Qdisc *sch, struct rtattr *opt) 204 { 205 struct prio_sched_data *q = qdisc_priv(sch); 206 struct tc_prio_qopt *qopt = RTA_DATA(opt); 207 int i; 208 209 if (opt->rta_len < RTA_LENGTH(sizeof(*qopt))) 210 return -EINVAL; 211 if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2) 212 return -EINVAL; 213 214 for (i=0; i<=TC_PRIO_MAX; i++) { 215 if (qopt->priomap[i] >= qopt->bands) 216 return -EINVAL; 217 } 218 219 sch_tree_lock(sch); 220 q->bands = qopt->bands; 221 memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1); 222 223 for (i=q->bands; i<TCQ_PRIO_BANDS; i++) { 224 struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc); 225 if (child != &noop_qdisc) 226 qdisc_destroy(child); 227 } 228 sch_tree_unlock(sch); 229 230 for (i=0; i<=TC_PRIO_MAX; i++) { 231 int band = q->prio2band[i]; 232 if (q->queues[band] == &noop_qdisc) { 233 struct Qdisc *child; 234 child = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops); 235 if (child) { 236 sch_tree_lock(sch); 237 child = xchg(&q->queues[band], child); 238 239 if (child != &noop_qdisc) 240 qdisc_destroy(child); 241 sch_tree_unlock(sch); 242 } 243 } 244 } 245 return 0; 246 } 247 248 static int prio_init(struct Qdisc *sch, struct rtattr *opt) 249 { 250 struct prio_sched_data *q = qdisc_priv(sch); 251 int i; 252 253 for (i=0; i<TCQ_PRIO_BANDS; i++) 254 q->queues[i] = &noop_qdisc; 255 256 if (opt == NULL) { 257 return -EINVAL; 258 } else { 259 int err; 260 261 if ((err= prio_tune(sch, opt)) != 0) 262 return err; 263 } 264 return 0; 265 } 266 267 static int prio_dump(struct Qdisc *sch, struct sk_buff *skb) 268 { 269 struct prio_sched_data *q = qdisc_priv(sch); 270 unsigned char *b = skb->tail; 271 struct tc_prio_qopt opt; 272 273 opt.bands = q->bands; 274 memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1); 275 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt); 276 return skb->len; 277 278 rtattr_failure: 279 skb_trim(skb, b - skb->data); 280 return -1; 281 } 282 283 static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new, 284 struct Qdisc **old) 285 { 286 struct prio_sched_data *q = qdisc_priv(sch); 287 unsigned long band = arg - 1; 288 289 if (band >= q->bands) 290 return -EINVAL; 291 292 if (new == NULL) 293 new = &noop_qdisc; 294 295 sch_tree_lock(sch); 296 *old = q->queues[band]; 297 q->queues[band] = new; 298 sch->q.qlen -= (*old)->q.qlen; 299 qdisc_reset(*old); 300 sch_tree_unlock(sch); 301 302 return 0; 303 } 304 305 static struct Qdisc * 306 prio_leaf(struct Qdisc *sch, unsigned long arg) 307 { 308 struct prio_sched_data *q = qdisc_priv(sch); 309 unsigned long band = arg - 1; 310 311 if (band >= q->bands) 312 return NULL; 313 314 return q->queues[band]; 315 } 316 317 static unsigned long prio_get(struct Qdisc *sch, u32 classid) 318 { 319 struct prio_sched_data *q = qdisc_priv(sch); 320 unsigned long band = TC_H_MIN(classid); 321 322 if (band - 1 >= q->bands) 323 return 0; 324 return band; 325 } 326 327 static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid) 328 { 329 return prio_get(sch, classid); 330 } 331 332 333 static void prio_put(struct Qdisc *q, unsigned long cl) 334 { 335 return; 336 } 337 338 static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct rtattr **tca, unsigned long *arg) 339 { 340 unsigned long cl = *arg; 341 struct prio_sched_data *q = qdisc_priv(sch); 342 343 if (cl - 1 > q->bands) 344 return -ENOENT; 345 return 0; 346 } 347 348 static int prio_delete(struct Qdisc *sch, unsigned long cl) 349 { 350 struct prio_sched_data *q = qdisc_priv(sch); 351 if (cl - 1 > q->bands) 352 return -ENOENT; 353 return 0; 354 } 355 356 357 static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb, 358 struct tcmsg *tcm) 359 { 360 struct prio_sched_data *q = qdisc_priv(sch); 361 362 if (cl - 1 > q->bands) 363 return -ENOENT; 364 tcm->tcm_handle |= TC_H_MIN(cl); 365 if (q->queues[cl-1]) 366 tcm->tcm_info = q->queues[cl-1]->handle; 367 return 0; 368 } 369 370 static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg) 371 { 372 struct prio_sched_data *q = qdisc_priv(sch); 373 int prio; 374 375 if (arg->stop) 376 return; 377 378 for (prio = 0; prio < q->bands; prio++) { 379 if (arg->count < arg->skip) { 380 arg->count++; 381 continue; 382 } 383 if (arg->fn(sch, prio+1, arg) < 0) { 384 arg->stop = 1; 385 break; 386 } 387 arg->count++; 388 } 389 } 390 391 static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl) 392 { 393 struct prio_sched_data *q = qdisc_priv(sch); 394 395 if (cl) 396 return NULL; 397 return &q->filter_list; 398 } 399 400 static struct Qdisc_class_ops prio_class_ops = { 401 .graft = prio_graft, 402 .leaf = prio_leaf, 403 .get = prio_get, 404 .put = prio_put, 405 .change = prio_change, 406 .delete = prio_delete, 407 .walk = prio_walk, 408 .tcf_chain = prio_find_tcf, 409 .bind_tcf = prio_bind, 410 .unbind_tcf = prio_put, 411 .dump = prio_dump_class, 412 }; 413 414 static struct Qdisc_ops prio_qdisc_ops = { 415 .next = NULL, 416 .cl_ops = &prio_class_ops, 417 .id = "prio", 418 .priv_size = sizeof(struct prio_sched_data), 419 .enqueue = prio_enqueue, 420 .dequeue = prio_dequeue, 421 .requeue = prio_requeue, 422 .drop = prio_drop, 423 .init = prio_init, 424 .reset = prio_reset, 425 .destroy = prio_destroy, 426 .change = prio_tune, 427 .dump = prio_dump, 428 .owner = THIS_MODULE, 429 }; 430 431 static int __init prio_module_init(void) 432 { 433 return register_qdisc(&prio_qdisc_ops); 434 } 435 436 static void __exit prio_module_exit(void) 437 { 438 unregister_qdisc(&prio_qdisc_ops); 439 } 440 441 module_init(prio_module_init) 442 module_exit(prio_module_exit) 443 444 MODULE_LICENSE("GPL"); 445