| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/module.h> | 
|  | 15 | #include <asm/uaccess.h> | 
|  | 16 | #include <asm/system.h> | 
|  | 17 | #include <linux/bitops.h> | 
|  | 18 | #include <linux/types.h> | 
|  | 19 | #include <linux/kernel.h> | 
|  | 20 | #include <linux/sched.h> | 
|  | 21 | #include <linux/string.h> | 
|  | 22 | #include <linux/mm.h> | 
|  | 23 | #include <linux/socket.h> | 
|  | 24 | #include <linux/sockios.h> | 
|  | 25 | #include <linux/in.h> | 
|  | 26 | #include <linux/errno.h> | 
|  | 27 | #include <linux/interrupt.h> | 
|  | 28 | #include <linux/if_ether.h> | 
|  | 29 | #include <linux/inet.h> | 
|  | 30 | #include <linux/netdevice.h> | 
|  | 31 | #include <linux/etherdevice.h> | 
|  | 32 | #include <linux/notifier.h> | 
|  | 33 | #include <net/ip.h> | 
|  | 34 | #include <net/route.h> | 
|  | 35 | #include <linux/skbuff.h> | 
|  | 36 | #include <net/sock.h> | 
|  | 37 | #include <net/pkt_sched.h> | 
|  | 38 |  | 
|  | 39 |  | 
|  | 40 | struct prio_sched_data | 
|  | 41 | { | 
|  | 42 | int bands; | 
|  | 43 | struct tcf_proto *filter_list; | 
|  | 44 | u8  prio2band[TC_PRIO_MAX+1]; | 
|  | 45 | struct Qdisc *queues[TCQ_PRIO_BANDS]; | 
|  | 46 | }; | 
|  | 47 |  | 
|  | 48 |  | 
|  | 49 | static struct Qdisc * | 
|  | 50 | prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr) | 
|  | 51 | { | 
|  | 52 | struct prio_sched_data *q = qdisc_priv(sch); | 
|  | 53 | u32 band = skb->priority; | 
|  | 54 | struct tcf_result res; | 
|  | 55 |  | 
| Jamal Hadi Salim | 29f1df6 | 2006-01-08 22:35:55 -0800 | [diff] [blame] | 56 | *qerr = NET_XMIT_BYPASS; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | if (TC_H_MAJ(skb->priority) != sch->handle) { | 
|  | 58 | #ifdef CONFIG_NET_CLS_ACT | 
|  | 59 | switch (tc_classify(skb, q->filter_list, &res)) { | 
|  | 60 | case TC_ACT_STOLEN: | 
|  | 61 | case TC_ACT_QUEUED: | 
|  | 62 | *qerr = NET_XMIT_SUCCESS; | 
|  | 63 | case TC_ACT_SHOT: | 
|  | 64 | return NULL; | 
|  | 65 | }; | 
|  | 66 |  | 
|  | 67 | if (!q->filter_list ) { | 
|  | 68 | #else | 
|  | 69 | if (!q->filter_list || tc_classify(skb, q->filter_list, &res)) { | 
|  | 70 | #endif | 
|  | 71 | if (TC_H_MAJ(band)) | 
|  | 72 | band = 0; | 
|  | 73 | return q->queues[q->prio2band[band&TC_PRIO_MAX]]; | 
|  | 74 | } | 
|  | 75 | band = res.classid; | 
|  | 76 | } | 
|  | 77 | band = TC_H_MIN(band) - 1; | 
|  | 78 | if (band > q->bands) | 
|  | 79 | return q->queues[q->prio2band[0]]; | 
|  | 80 |  | 
|  | 81 | return q->queues[band]; | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | static int | 
|  | 85 | prio_enqueue(struct sk_buff *skb, struct Qdisc *sch) | 
|  | 86 | { | 
|  | 87 | struct Qdisc *qdisc; | 
|  | 88 | int ret; | 
|  | 89 |  | 
|  | 90 | qdisc = prio_classify(skb, sch, &ret); | 
|  | 91 | #ifdef CONFIG_NET_CLS_ACT | 
|  | 92 | if (qdisc == NULL) { | 
| Jamal Hadi Salim | 29f1df6 | 2006-01-08 22:35:55 -0800 | [diff] [blame] | 93 |  | 
|  | 94 | if (ret == NET_XMIT_BYPASS) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { | 
| Jamal Hadi Salim | 29f1df6 | 2006-01-08 22:35:55 -0800 | [diff] [blame] | 121 | if (ret == NET_XMIT_BYPASS) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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]; | 
| Patrick McHardy | 6d037a2 | 2006-03-20 19:00:49 -0800 | [diff] [blame] | 167 | if (qdisc->ops->drop && (len = qdisc->ops->drop(qdisc)) != 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); | 
| Patrick McHardy | 5e50da0 | 2006-11-29 17:36:20 -0800 | [diff] [blame] | 225 | if (child != &noop_qdisc) { | 
|  | 226 | qdisc_tree_decrease_qlen(child, child->q.qlen); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | qdisc_destroy(child); | 
| Patrick McHardy | 5e50da0 | 2006-11-29 17:36:20 -0800 | [diff] [blame] | 228 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | } | 
|  | 230 | sch_tree_unlock(sch); | 
|  | 231 |  | 
| Amnon Aaronsohn | dd914b4 | 2006-01-17 02:24:26 -0800 | [diff] [blame] | 232 | for (i=0; i<q->bands; i++) { | 
|  | 233 | if (q->queues[i] == &noop_qdisc) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | struct Qdisc *child; | 
| Patrick McHardy | 9f9afec | 2006-11-29 17:35:18 -0800 | [diff] [blame] | 235 | child = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops, | 
|  | 236 | TC_H_MAKE(sch->handle, i + 1)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | if (child) { | 
|  | 238 | sch_tree_lock(sch); | 
| Amnon Aaronsohn | dd914b4 | 2006-01-17 02:24:26 -0800 | [diff] [blame] | 239 | child = xchg(&q->queues[i], child); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 |  | 
| Patrick McHardy | 5e50da0 | 2006-11-29 17:36:20 -0800 | [diff] [blame] | 241 | if (child != &noop_qdisc) { | 
|  | 242 | qdisc_tree_decrease_qlen(child, | 
|  | 243 | child->q.qlen); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | qdisc_destroy(child); | 
| Patrick McHardy | 5e50da0 | 2006-11-29 17:36:20 -0800 | [diff] [blame] | 245 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | sch_tree_unlock(sch); | 
|  | 247 | } | 
|  | 248 | } | 
|  | 249 | } | 
|  | 250 | return 0; | 
|  | 251 | } | 
|  | 252 |  | 
|  | 253 | static int prio_init(struct Qdisc *sch, struct rtattr *opt) | 
|  | 254 | { | 
|  | 255 | struct prio_sched_data *q = qdisc_priv(sch); | 
|  | 256 | int i; | 
|  | 257 |  | 
|  | 258 | for (i=0; i<TCQ_PRIO_BANDS; i++) | 
|  | 259 | q->queues[i] = &noop_qdisc; | 
|  | 260 |  | 
|  | 261 | if (opt == NULL) { | 
|  | 262 | return -EINVAL; | 
|  | 263 | } else { | 
|  | 264 | int err; | 
|  | 265 |  | 
|  | 266 | if ((err= prio_tune(sch, opt)) != 0) | 
|  | 267 | return err; | 
|  | 268 | } | 
|  | 269 | return 0; | 
|  | 270 | } | 
|  | 271 |  | 
|  | 272 | static int prio_dump(struct Qdisc *sch, struct sk_buff *skb) | 
|  | 273 | { | 
|  | 274 | struct prio_sched_data *q = qdisc_priv(sch); | 
|  | 275 | unsigned char	 *b = skb->tail; | 
|  | 276 | struct tc_prio_qopt opt; | 
|  | 277 |  | 
|  | 278 | opt.bands = q->bands; | 
|  | 279 | memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1); | 
|  | 280 | RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt); | 
|  | 281 | return skb->len; | 
|  | 282 |  | 
|  | 283 | rtattr_failure: | 
|  | 284 | skb_trim(skb, b - skb->data); | 
|  | 285 | return -1; | 
|  | 286 | } | 
|  | 287 |  | 
|  | 288 | static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new, | 
|  | 289 | struct Qdisc **old) | 
|  | 290 | { | 
|  | 291 | struct prio_sched_data *q = qdisc_priv(sch); | 
|  | 292 | unsigned long band = arg - 1; | 
|  | 293 |  | 
|  | 294 | if (band >= q->bands) | 
|  | 295 | return -EINVAL; | 
|  | 296 |  | 
|  | 297 | if (new == NULL) | 
|  | 298 | new = &noop_qdisc; | 
|  | 299 |  | 
|  | 300 | sch_tree_lock(sch); | 
|  | 301 | *old = q->queues[band]; | 
|  | 302 | q->queues[band] = new; | 
| Patrick McHardy | 5e50da0 | 2006-11-29 17:36:20 -0800 | [diff] [blame] | 303 | qdisc_tree_decrease_qlen(*old, (*old)->q.qlen); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | qdisc_reset(*old); | 
|  | 305 | sch_tree_unlock(sch); | 
|  | 306 |  | 
|  | 307 | return 0; | 
|  | 308 | } | 
|  | 309 |  | 
|  | 310 | static struct Qdisc * | 
|  | 311 | prio_leaf(struct Qdisc *sch, unsigned long arg) | 
|  | 312 | { | 
|  | 313 | struct prio_sched_data *q = qdisc_priv(sch); | 
|  | 314 | unsigned long band = arg - 1; | 
|  | 315 |  | 
|  | 316 | if (band >= q->bands) | 
|  | 317 | return NULL; | 
|  | 318 |  | 
|  | 319 | return q->queues[band]; | 
|  | 320 | } | 
|  | 321 |  | 
|  | 322 | static unsigned long prio_get(struct Qdisc *sch, u32 classid) | 
|  | 323 | { | 
|  | 324 | struct prio_sched_data *q = qdisc_priv(sch); | 
|  | 325 | unsigned long band = TC_H_MIN(classid); | 
|  | 326 |  | 
|  | 327 | if (band - 1 >= q->bands) | 
|  | 328 | return 0; | 
|  | 329 | return band; | 
|  | 330 | } | 
|  | 331 |  | 
|  | 332 | static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid) | 
|  | 333 | { | 
|  | 334 | return prio_get(sch, classid); | 
|  | 335 | } | 
|  | 336 |  | 
|  | 337 |  | 
|  | 338 | static void prio_put(struct Qdisc *q, unsigned long cl) | 
|  | 339 | { | 
|  | 340 | return; | 
|  | 341 | } | 
|  | 342 |  | 
|  | 343 | static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct rtattr **tca, unsigned long *arg) | 
|  | 344 | { | 
|  | 345 | unsigned long cl = *arg; | 
|  | 346 | struct prio_sched_data *q = qdisc_priv(sch); | 
|  | 347 |  | 
|  | 348 | if (cl - 1 > q->bands) | 
|  | 349 | return -ENOENT; | 
|  | 350 | return 0; | 
|  | 351 | } | 
|  | 352 |  | 
|  | 353 | static int prio_delete(struct Qdisc *sch, unsigned long cl) | 
|  | 354 | { | 
|  | 355 | struct prio_sched_data *q = qdisc_priv(sch); | 
|  | 356 | if (cl - 1 > q->bands) | 
|  | 357 | return -ENOENT; | 
|  | 358 | return 0; | 
|  | 359 | } | 
|  | 360 |  | 
|  | 361 |  | 
|  | 362 | static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb, | 
|  | 363 | struct tcmsg *tcm) | 
|  | 364 | { | 
|  | 365 | struct prio_sched_data *q = qdisc_priv(sch); | 
|  | 366 |  | 
|  | 367 | if (cl - 1 > q->bands) | 
|  | 368 | return -ENOENT; | 
|  | 369 | tcm->tcm_handle |= TC_H_MIN(cl); | 
|  | 370 | if (q->queues[cl-1]) | 
|  | 371 | tcm->tcm_info = q->queues[cl-1]->handle; | 
|  | 372 | return 0; | 
|  | 373 | } | 
|  | 374 |  | 
|  | 375 | static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg) | 
|  | 376 | { | 
|  | 377 | struct prio_sched_data *q = qdisc_priv(sch); | 
|  | 378 | int prio; | 
|  | 379 |  | 
|  | 380 | if (arg->stop) | 
|  | 381 | return; | 
|  | 382 |  | 
|  | 383 | for (prio = 0; prio < q->bands; prio++) { | 
|  | 384 | if (arg->count < arg->skip) { | 
|  | 385 | arg->count++; | 
|  | 386 | continue; | 
|  | 387 | } | 
|  | 388 | if (arg->fn(sch, prio+1, arg) < 0) { | 
|  | 389 | arg->stop = 1; | 
|  | 390 | break; | 
|  | 391 | } | 
|  | 392 | arg->count++; | 
|  | 393 | } | 
|  | 394 | } | 
|  | 395 |  | 
|  | 396 | static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl) | 
|  | 397 | { | 
|  | 398 | struct prio_sched_data *q = qdisc_priv(sch); | 
|  | 399 |  | 
|  | 400 | if (cl) | 
|  | 401 | return NULL; | 
|  | 402 | return &q->filter_list; | 
|  | 403 | } | 
|  | 404 |  | 
|  | 405 | static struct Qdisc_class_ops prio_class_ops = { | 
|  | 406 | .graft		=	prio_graft, | 
|  | 407 | .leaf		=	prio_leaf, | 
|  | 408 | .get		=	prio_get, | 
|  | 409 | .put		=	prio_put, | 
|  | 410 | .change		=	prio_change, | 
|  | 411 | .delete		=	prio_delete, | 
|  | 412 | .walk		=	prio_walk, | 
|  | 413 | .tcf_chain	=	prio_find_tcf, | 
|  | 414 | .bind_tcf	=	prio_bind, | 
|  | 415 | .unbind_tcf	=	prio_put, | 
|  | 416 | .dump		=	prio_dump_class, | 
|  | 417 | }; | 
|  | 418 |  | 
|  | 419 | static struct Qdisc_ops prio_qdisc_ops = { | 
|  | 420 | .next		=	NULL, | 
|  | 421 | .cl_ops		=	&prio_class_ops, | 
|  | 422 | .id		=	"prio", | 
|  | 423 | .priv_size	=	sizeof(struct prio_sched_data), | 
|  | 424 | .enqueue	=	prio_enqueue, | 
|  | 425 | .dequeue	=	prio_dequeue, | 
|  | 426 | .requeue	=	prio_requeue, | 
|  | 427 | .drop		=	prio_drop, | 
|  | 428 | .init		=	prio_init, | 
|  | 429 | .reset		=	prio_reset, | 
|  | 430 | .destroy	=	prio_destroy, | 
|  | 431 | .change		=	prio_tune, | 
|  | 432 | .dump		=	prio_dump, | 
|  | 433 | .owner		=	THIS_MODULE, | 
|  | 434 | }; | 
|  | 435 |  | 
|  | 436 | static int __init prio_module_init(void) | 
|  | 437 | { | 
|  | 438 | return register_qdisc(&prio_qdisc_ops); | 
|  | 439 | } | 
|  | 440 |  | 
|  | 441 | static void __exit prio_module_exit(void) | 
|  | 442 | { | 
|  | 443 | unregister_qdisc(&prio_qdisc_ops); | 
|  | 444 | } | 
|  | 445 |  | 
|  | 446 | module_init(prio_module_init) | 
|  | 447 | module_exit(prio_module_exit) | 
|  | 448 |  | 
|  | 449 | MODULE_LICENSE("GPL"); |