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 <asm/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 __inline__ unsigned prio_classify(struct sk_buff *skb, struct Qdisc *sch) 51 { 52 struct prio_sched_data *q = (struct prio_sched_data *)sch->data; 53 struct tcf_result res; 54 u32 band; 55 56 band = skb->priority; 57 if (TC_H_MAJ(skb->priority) != sch->handle) { 58 if (!q->filter_list || tc_classify(skb, q->filter_list, &res)) { 59 if (TC_H_MAJ(band)) 60 band = 0; 61 return q->prio2band[band&TC_PRIO_MAX]; 62 } 63 band = res.classid; 64 } 65 band = TC_H_MIN(band) - 1; 66 return band < q->bands ? band : q->prio2band[0]; 67 } 68 69 static int 70 prio_enqueue(struct sk_buff *skb, struct Qdisc* sch) 71 { 72 struct prio_sched_data *q = (struct prio_sched_data *)sch->data; 73 struct Qdisc *qdisc; 74 int ret; 75 76 qdisc = q->queues[prio_classify(skb, sch)]; 77 78 if ((ret = qdisc->enqueue(skb, qdisc)) == 0) { 79 sch->stats.bytes += skb->len; 80 sch->stats.packets++; 81 sch->q.qlen++; 82 return 0; 83 } 84 sch->stats.drops++; 85 return ret; 86 } 87 88 89 static int 90 prio_requeue(struct sk_buff *skb, struct Qdisc* sch) 91 { 92 struct prio_sched_data *q = (struct prio_sched_data *)sch->data; 93 struct Qdisc *qdisc; 94 int ret; 95 96 qdisc = q->queues[prio_classify(skb, sch)]; 97 98 if ((ret = qdisc->ops->requeue(skb, qdisc)) == 0) { 99 sch->q.qlen++; 100 return 0; 101 } 102 sch->stats.drops++; 103 return ret; 104 } 105 106 107 static struct sk_buff * 108 prio_dequeue(struct Qdisc* sch) 109 { 110 struct sk_buff *skb; 111 struct prio_sched_data *q = (struct prio_sched_data *)sch->data; 112 int prio; 113 struct Qdisc *qdisc; 114 115 for (prio = 0; prio < q->bands; prio++) { 116 qdisc = q->queues[prio]; 117 skb = qdisc->dequeue(qdisc); 118 if (skb) { 119 sch->q.qlen--; 120 return skb; 121 } 122 } 123 return NULL; 124 125 } 126 127 static unsigned int prio_drop(struct Qdisc* sch) 128 { 129 struct prio_sched_data *q = (struct prio_sched_data *)sch->data; 130 int prio; 131 unsigned int len; 132 struct Qdisc *qdisc; 133 134 for (prio = q->bands-1; prio >= 0; prio--) { 135 qdisc = q->queues[prio]; 136 if ((len = qdisc->ops->drop(qdisc)) != 0) { 137 sch->q.qlen--; 138 return len; 139 } 140 } 141 return 0; 142 } 143 144 145 static void 146 prio_reset(struct Qdisc* sch) 147 { 148 int prio; 149 struct prio_sched_data *q = (struct prio_sched_data *)sch->data; 150 151 for (prio=0; prio<q->bands; prio++) 152 qdisc_reset(q->queues[prio]); 153 sch->q.qlen = 0; 154 } 155 156 static void 157 prio_destroy(struct Qdisc* sch) 158 { 159 int prio; 160 struct prio_sched_data *q = (struct prio_sched_data *)sch->data; 161 struct tcf_proto *tp; 162 163 while ((tp = q->filter_list) != NULL) { 164 q->filter_list = tp->next; 165 tp->ops->destroy(tp); 166 } 167 168 for (prio=0; prio<q->bands; prio++) { 169 qdisc_destroy(q->queues[prio]); 170 q->queues[prio] = &noop_qdisc; 171 } 172 } 173 174 static int prio_tune(struct Qdisc *sch, struct rtattr *opt) 175 { 176 struct prio_sched_data *q = (struct prio_sched_data *)sch->data; 177 struct tc_prio_qopt *qopt = RTA_DATA(opt); 178 int i; 179 180 if (opt->rta_len < RTA_LENGTH(sizeof(*qopt))) 181 return -EINVAL; 182 if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2) 183 return -EINVAL; 184 185 for (i=0; i<=TC_PRIO_MAX; i++) { 186 if (qopt->priomap[i] >= qopt->bands) 187 return -EINVAL; 188 } 189 190 sch_tree_lock(sch); 191 q->bands = qopt->bands; 192 memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1); 193 194 for (i=q->bands; i<TCQ_PRIO_BANDS; i++) { 195 struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc); 196 if (child != &noop_qdisc) 197 qdisc_destroy(child); 198 } 199 sch_tree_unlock(sch); 200 201 for (i=0; i<=TC_PRIO_MAX; i++) { 202 int band = q->prio2band[i]; 203 if (q->queues[band] == &noop_qdisc) { 204 struct Qdisc *child; 205 child = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops); 206 if (child) { 207 sch_tree_lock(sch); 208 child = xchg(&q->queues[band], child); 209 210 if (child != &noop_qdisc) 211 qdisc_destroy(child); 212 sch_tree_unlock(sch); 213 } 214 } 215 } 216 return 0; 217 } 218 219 static int prio_init(struct Qdisc *sch, struct rtattr *opt) 220 { 221 struct prio_sched_data *q = (struct prio_sched_data *)sch->data; 222 int i; 223 224 for (i=0; i<TCQ_PRIO_BANDS; i++) 225 q->queues[i] = &noop_qdisc; 226 227 if (opt == NULL) { 228 return -EINVAL; 229 } else { 230 int err; 231 232 if ((err= prio_tune(sch, opt)) != 0) 233 return err; 234 } 235 return 0; 236 } 237 238 static int prio_dump(struct Qdisc *sch, struct sk_buff *skb) 239 { 240 struct prio_sched_data *q = (struct prio_sched_data *)sch->data; 241 unsigned char *b = skb->tail; 242 struct tc_prio_qopt opt; 243 244 opt.bands = q->bands; 245 memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1); 246 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt); 247 return skb->len; 248 249 rtattr_failure: 250 skb_trim(skb, b - skb->data); 251 return -1; 252 } 253 254 static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new, 255 struct Qdisc **old) 256 { 257 struct prio_sched_data *q = (struct prio_sched_data *)sch->data; 258 unsigned long band = arg - 1; 259 260 if (band >= q->bands) 261 return -EINVAL; 262 263 if (new == NULL) 264 new = &noop_qdisc; 265 266 sch_tree_lock(sch); 267 *old = q->queues[band]; 268 q->queues[band] = new; 269 sch->q.qlen -= (*old)->q.qlen; 270 qdisc_reset(*old); 271 sch_tree_unlock(sch); 272 273 return 0; 274 } 275 276 static struct Qdisc * 277 prio_leaf(struct Qdisc *sch, unsigned long arg) 278 { 279 struct prio_sched_data *q = (struct prio_sched_data *)sch->data; 280 unsigned long band = arg - 1; 281 282 if (band >= q->bands) 283 return NULL; 284 285 return q->queues[band]; 286 } 287 288 static unsigned long prio_get(struct Qdisc *sch, u32 classid) 289 { 290 struct prio_sched_data *q = (struct prio_sched_data *)sch->data; 291 unsigned long band = TC_H_MIN(classid); 292 293 if (band - 1 >= q->bands) 294 return 0; 295 return band; 296 } 297 298 static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid) 299 { 300 return prio_get(sch, classid); 301 } 302 303 304 static void prio_put(struct Qdisc *q, unsigned long cl) 305 { 306 return; 307 } 308 309 static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct rtattr **tca, unsigned long *arg) 310 { 311 unsigned long cl = *arg; 312 struct prio_sched_data *q = (struct prio_sched_data *)sch->data; 313 314 if (cl - 1 > q->bands) 315 return -ENOENT; 316 return 0; 317 } 318 319 static int prio_delete(struct Qdisc *sch, unsigned long cl) 320 { 321 struct prio_sched_data *q = (struct prio_sched_data *)sch->data; 322 if (cl - 1 > q->bands) 323 return -ENOENT; 324 return 0; 325 } 326 327 328 static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb, 329 struct tcmsg *tcm) 330 { 331 struct prio_sched_data *q = (struct prio_sched_data *)sch->data; 332 333 if (cl - 1 > q->bands) 334 return -ENOENT; 335 tcm->tcm_handle |= TC_H_MIN(cl); 336 if (q->queues[cl-1]) 337 tcm->tcm_info = q->queues[cl-1]->handle; 338 return 0; 339 } 340 341 static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg) 342 { 343 struct prio_sched_data *q = (struct prio_sched_data *)sch->data; 344 int prio; 345 346 if (arg->stop) 347 return; 348 349 for (prio = 0; prio < q->bands; prio++) { 350 if (arg->count < arg->skip) { 351 arg->count++; 352 continue; 353 } 354 if (arg->fn(sch, prio+1, arg) < 0) { 355 arg->stop = 1; 356 break; 357 } 358 arg->count++; 359 } 360 } 361 362 static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl) 363 { 364 struct prio_sched_data *q = (struct prio_sched_data *)sch->data; 365 366 if (cl) 367 return NULL; 368 return &q->filter_list; 369 } 370 371 static struct Qdisc_class_ops prio_class_ops = { 372 .graft = prio_graft, 373 .leaf = prio_leaf, 374 .get = prio_get, 375 .put = prio_put, 376 .change = prio_change, 377 .delete = prio_delete, 378 .walk = prio_walk, 379 .tcf_chain = prio_find_tcf, 380 .bind_tcf = prio_bind, 381 .unbind_tcf = prio_put, 382 .dump = prio_dump_class, 383 }; 384 385 struct Qdisc_ops prio_qdisc_ops = { 386 .next = NULL, 387 .cl_ops = &prio_class_ops, 388 .id = "prio", 389 .priv_size = sizeof(struct prio_sched_data), 390 .enqueue = prio_enqueue, 391 .dequeue = prio_dequeue, 392 .requeue = prio_requeue, 393 .drop = prio_drop, 394 .init = prio_init, 395 .reset = prio_reset, 396 .destroy = prio_destroy, 397 .change = prio_tune, 398 .dump = prio_dump, 399 .owner = THIS_MODULE, 400 }; 401 402 #ifdef MODULE 403 404 int init_module(void) 405 { 406 return register_qdisc(&prio_qdisc_ops); 407 } 408 409 void cleanup_module(void) 410 { 411 unregister_qdisc(&prio_qdisc_ops); 412 } 413 414 #endif 415 MODULE_LICENSE("GPL"); 416
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.