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> |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 10 | * Fixes: 19990609: J Hadi Salim <hadi@nortelnetworks.com>: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/types.h> |
| 16 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/skbuff.h> |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 20 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <net/pkt_sched.h> |
| 22 | |
| 23 | |
| 24 | struct prio_sched_data |
| 25 | { |
| 26 | int bands; |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 27 | int curband; /* for round-robin */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | struct tcf_proto *filter_list; |
| 29 | u8 prio2band[TC_PRIO_MAX+1]; |
| 30 | struct Qdisc *queues[TCQ_PRIO_BANDS]; |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 31 | int mq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | |
| 35 | static struct Qdisc * |
| 36 | prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr) |
| 37 | { |
| 38 | struct prio_sched_data *q = qdisc_priv(sch); |
| 39 | u32 band = skb->priority; |
| 40 | struct tcf_result res; |
Patrick McHardy | bdba91e | 2007-07-30 17:07:14 -0700 | [diff] [blame] | 41 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Jamal Hadi Salim | 29f1df6 | 2006-01-08 22:35:55 -0800 | [diff] [blame] | 43 | *qerr = NET_XMIT_BYPASS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | if (TC_H_MAJ(skb->priority) != sch->handle) { |
Patrick McHardy | bdba91e | 2007-07-30 17:07:14 -0700 | [diff] [blame] | 45 | err = tc_classify(skb, q->filter_list, &res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #ifdef CONFIG_NET_CLS_ACT |
Lucas Nussbaum | dbaaa07 | 2007-08-30 22:35:46 -0700 | [diff] [blame] | 47 | switch (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | case TC_ACT_STOLEN: |
| 49 | case TC_ACT_QUEUED: |
| 50 | *qerr = NET_XMIT_SUCCESS; |
| 51 | case TC_ACT_SHOT: |
| 52 | return NULL; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 53 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #endif |
Patrick McHardy | bdba91e | 2007-07-30 17:07:14 -0700 | [diff] [blame] | 55 | if (!q->filter_list || err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | if (TC_H_MAJ(band)) |
| 57 | band = 0; |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 58 | band = q->prio2band[band&TC_PRIO_MAX]; |
| 59 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
| 61 | band = res.classid; |
| 62 | } |
| 63 | band = TC_H_MIN(band) - 1; |
Jamal Hadi Salim | 3e5c2d3 | 2007-05-14 02:57:19 -0700 | [diff] [blame] | 64 | if (band >= q->bands) |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 65 | band = q->prio2band[0]; |
| 66 | out: |
| 67 | if (q->mq) |
| 68 | skb_set_queue_mapping(skb, band); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | return q->queues[band]; |
| 70 | } |
| 71 | |
| 72 | static int |
| 73 | prio_enqueue(struct sk_buff *skb, struct Qdisc *sch) |
| 74 | { |
| 75 | struct Qdisc *qdisc; |
| 76 | int ret; |
| 77 | |
| 78 | qdisc = prio_classify(skb, sch, &ret); |
| 79 | #ifdef CONFIG_NET_CLS_ACT |
| 80 | if (qdisc == NULL) { |
Jamal Hadi Salim | 29f1df6 | 2006-01-08 22:35:55 -0800 | [diff] [blame] | 81 | |
| 82 | if (ret == NET_XMIT_BYPASS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | sch->qstats.drops++; |
| 84 | kfree_skb(skb); |
| 85 | return ret; |
| 86 | } |
| 87 | #endif |
| 88 | |
| 89 | if ((ret = qdisc->enqueue(skb, qdisc)) == NET_XMIT_SUCCESS) { |
| 90 | sch->bstats.bytes += skb->len; |
| 91 | sch->bstats.packets++; |
| 92 | sch->q.qlen++; |
| 93 | return NET_XMIT_SUCCESS; |
| 94 | } |
| 95 | sch->qstats.drops++; |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 96 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | |
| 100 | static int |
| 101 | prio_requeue(struct sk_buff *skb, struct Qdisc* sch) |
| 102 | { |
| 103 | struct Qdisc *qdisc; |
| 104 | int ret; |
| 105 | |
| 106 | qdisc = prio_classify(skb, sch, &ret); |
| 107 | #ifdef CONFIG_NET_CLS_ACT |
| 108 | if (qdisc == NULL) { |
Jamal Hadi Salim | 29f1df6 | 2006-01-08 22:35:55 -0800 | [diff] [blame] | 109 | if (ret == NET_XMIT_BYPASS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | sch->qstats.drops++; |
| 111 | kfree_skb(skb); |
| 112 | return ret; |
| 113 | } |
| 114 | #endif |
| 115 | |
| 116 | if ((ret = qdisc->ops->requeue(skb, qdisc)) == NET_XMIT_SUCCESS) { |
| 117 | sch->q.qlen++; |
| 118 | sch->qstats.requeues++; |
| 119 | return 0; |
| 120 | } |
| 121 | sch->qstats.drops++; |
| 122 | return NET_XMIT_DROP; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | static struct sk_buff * |
| 127 | prio_dequeue(struct Qdisc* sch) |
| 128 | { |
| 129 | struct sk_buff *skb; |
| 130 | struct prio_sched_data *q = qdisc_priv(sch); |
| 131 | int prio; |
| 132 | struct Qdisc *qdisc; |
| 133 | |
| 134 | for (prio = 0; prio < q->bands; prio++) { |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 135 | /* Check if the target subqueue is available before |
| 136 | * pulling an skb. This way we avoid excessive requeues |
| 137 | * for slower queues. |
| 138 | */ |
David S. Miller | 5ce2d48 | 2008-07-08 17:06:30 -0700 | [diff] [blame^] | 139 | if (!__netif_subqueue_stopped(qdisc_dev(sch), |
| 140 | (q->mq ? prio : 0))) { |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 141 | qdisc = q->queues[prio]; |
| 142 | skb = qdisc->dequeue(qdisc); |
| 143 | if (skb) { |
| 144 | sch->q.qlen--; |
| 145 | return skb; |
| 146 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | return NULL; |
| 150 | |
| 151 | } |
| 152 | |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 153 | static struct sk_buff *rr_dequeue(struct Qdisc* sch) |
| 154 | { |
| 155 | struct sk_buff *skb; |
| 156 | struct prio_sched_data *q = qdisc_priv(sch); |
| 157 | struct Qdisc *qdisc; |
| 158 | int bandcount; |
| 159 | |
| 160 | /* Only take one pass through the queues. If nothing is available, |
| 161 | * return nothing. |
| 162 | */ |
| 163 | for (bandcount = 0; bandcount < q->bands; bandcount++) { |
| 164 | /* Check if the target subqueue is available before |
| 165 | * pulling an skb. This way we avoid excessive requeues |
| 166 | * for slower queues. If the queue is stopped, try the |
| 167 | * next queue. |
| 168 | */ |
David S. Miller | 5ce2d48 | 2008-07-08 17:06:30 -0700 | [diff] [blame^] | 169 | if (!__netif_subqueue_stopped(qdisc_dev(sch), |
| 170 | (q->mq ? q->curband : 0))) { |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 171 | qdisc = q->queues[q->curband]; |
| 172 | skb = qdisc->dequeue(qdisc); |
| 173 | if (skb) { |
| 174 | sch->q.qlen--; |
| 175 | q->curband++; |
| 176 | if (q->curband >= q->bands) |
| 177 | q->curband = 0; |
| 178 | return skb; |
| 179 | } |
| 180 | } |
| 181 | q->curband++; |
| 182 | if (q->curband >= q->bands) |
| 183 | q->curband = 0; |
| 184 | } |
| 185 | return NULL; |
| 186 | } |
| 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | static unsigned int prio_drop(struct Qdisc* sch) |
| 189 | { |
| 190 | struct prio_sched_data *q = qdisc_priv(sch); |
| 191 | int prio; |
| 192 | unsigned int len; |
| 193 | struct Qdisc *qdisc; |
| 194 | |
| 195 | for (prio = q->bands-1; prio >= 0; prio--) { |
| 196 | qdisc = q->queues[prio]; |
Patrick McHardy | 6d037a2 | 2006-03-20 19:00:49 -0800 | [diff] [blame] | 197 | if (qdisc->ops->drop && (len = qdisc->ops->drop(qdisc)) != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | sch->q.qlen--; |
| 199 | return len; |
| 200 | } |
| 201 | } |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | |
| 206 | static void |
| 207 | prio_reset(struct Qdisc* sch) |
| 208 | { |
| 209 | int prio; |
| 210 | struct prio_sched_data *q = qdisc_priv(sch); |
| 211 | |
| 212 | for (prio=0; prio<q->bands; prio++) |
| 213 | qdisc_reset(q->queues[prio]); |
| 214 | sch->q.qlen = 0; |
| 215 | } |
| 216 | |
| 217 | static void |
| 218 | prio_destroy(struct Qdisc* sch) |
| 219 | { |
| 220 | int prio; |
| 221 | struct prio_sched_data *q = qdisc_priv(sch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
Patrick McHardy | ff31ab5 | 2008-07-01 19:52:38 -0700 | [diff] [blame] | 223 | tcf_destroy_chain(&q->filter_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | for (prio=0; prio<q->bands; prio++) |
| 225 | qdisc_destroy(q->queues[prio]); |
| 226 | } |
| 227 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 228 | static int prio_tune(struct Qdisc *sch, struct nlattr *opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | { |
| 230 | struct prio_sched_data *q = qdisc_priv(sch); |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 231 | struct tc_prio_qopt *qopt; |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 232 | struct nlattr *tb[TCA_PRIO_MAX + 1]; |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 233 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | int i; |
| 235 | |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 236 | err = nla_parse_nested_compat(tb, TCA_PRIO_MAX, opt, NULL, qopt, |
| 237 | sizeof(*qopt)); |
| 238 | if (err < 0) |
| 239 | return err; |
| 240 | |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 241 | q->bands = qopt->bands; |
| 242 | /* If we're multiqueue, make sure the number of incoming bands |
| 243 | * matches the number of queues on the device we're associating with. |
| 244 | * If the number of bands requested is zero, then set q->bands to |
Peter P Waskiewicz Jr | 0773192 | 2007-07-30 17:13:45 -0700 | [diff] [blame] | 245 | * dev->egress_subqueue_count. Also, the root qdisc must be the |
| 246 | * only one that is enabled for multiqueue, since it's the only one |
| 247 | * that interacts with the underlying device. |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 248 | */ |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 249 | q->mq = nla_get_flag(tb[TCA_PRIO_MQ]); |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 250 | if (q->mq) { |
Peter P Waskiewicz Jr | 0773192 | 2007-07-30 17:13:45 -0700 | [diff] [blame] | 251 | if (sch->parent != TC_H_ROOT) |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 252 | return -EINVAL; |
David S. Miller | 5ce2d48 | 2008-07-08 17:06:30 -0700 | [diff] [blame^] | 253 | if (netif_is_multiqueue(qdisc_dev(sch))) { |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 254 | if (q->bands == 0) |
David S. Miller | 5ce2d48 | 2008-07-08 17:06:30 -0700 | [diff] [blame^] | 255 | q->bands = qdisc_dev(sch)->egress_subqueue_count; |
| 256 | else if (q->bands != qdisc_dev(sch)->egress_subqueue_count) |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 257 | return -EINVAL; |
| 258 | } else |
| 259 | return -EOPNOTSUPP; |
| 260 | } |
| 261 | |
| 262 | if (q->bands > TCQ_PRIO_BANDS || q->bands < 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | return -EINVAL; |
| 264 | |
| 265 | for (i=0; i<=TC_PRIO_MAX; i++) { |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 266 | if (qopt->priomap[i] >= q->bands) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | return -EINVAL; |
| 268 | } |
| 269 | |
| 270 | sch_tree_lock(sch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1); |
| 272 | |
| 273 | for (i=q->bands; i<TCQ_PRIO_BANDS; i++) { |
| 274 | struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc); |
Patrick McHardy | 5e50da0 | 2006-11-29 17:36:20 -0800 | [diff] [blame] | 275 | if (child != &noop_qdisc) { |
| 276 | qdisc_tree_decrease_qlen(child, child->q.qlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | qdisc_destroy(child); |
Patrick McHardy | 5e50da0 | 2006-11-29 17:36:20 -0800 | [diff] [blame] | 278 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | } |
| 280 | sch_tree_unlock(sch); |
| 281 | |
Amnon Aaronsohn | dd914b4 | 2006-01-17 02:24:26 -0800 | [diff] [blame] | 282 | for (i=0; i<q->bands; i++) { |
| 283 | if (q->queues[i] == &noop_qdisc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | struct Qdisc *child; |
David S. Miller | 5ce2d48 | 2008-07-08 17:06:30 -0700 | [diff] [blame^] | 285 | child = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue, |
David S. Miller | bb949fb | 2008-07-08 16:55:56 -0700 | [diff] [blame] | 286 | &pfifo_qdisc_ops, |
Patrick McHardy | 9f9afec | 2006-11-29 17:35:18 -0800 | [diff] [blame] | 287 | TC_H_MAKE(sch->handle, i + 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | if (child) { |
| 289 | sch_tree_lock(sch); |
Amnon Aaronsohn | dd914b4 | 2006-01-17 02:24:26 -0800 | [diff] [blame] | 290 | child = xchg(&q->queues[i], child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
Patrick McHardy | 5e50da0 | 2006-11-29 17:36:20 -0800 | [diff] [blame] | 292 | if (child != &noop_qdisc) { |
| 293 | qdisc_tree_decrease_qlen(child, |
| 294 | child->q.qlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | qdisc_destroy(child); |
Patrick McHardy | 5e50da0 | 2006-11-29 17:36:20 -0800 | [diff] [blame] | 296 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | sch_tree_unlock(sch); |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | return 0; |
| 302 | } |
| 303 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 304 | static int prio_init(struct Qdisc *sch, struct nlattr *opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | { |
| 306 | struct prio_sched_data *q = qdisc_priv(sch); |
| 307 | int i; |
| 308 | |
| 309 | for (i=0; i<TCQ_PRIO_BANDS; i++) |
| 310 | q->queues[i] = &noop_qdisc; |
| 311 | |
| 312 | if (opt == NULL) { |
| 313 | return -EINVAL; |
| 314 | } else { |
| 315 | int err; |
| 316 | |
| 317 | if ((err= prio_tune(sch, opt)) != 0) |
| 318 | return err; |
| 319 | } |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | static int prio_dump(struct Qdisc *sch, struct sk_buff *skb) |
| 324 | { |
| 325 | struct prio_sched_data *q = qdisc_priv(sch); |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 326 | unsigned char *b = skb_tail_pointer(skb); |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 327 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | struct tc_prio_qopt opt; |
| 329 | |
| 330 | opt.bands = q->bands; |
| 331 | memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1); |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 332 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 333 | nest = nla_nest_compat_start(skb, TCA_OPTIONS, sizeof(opt), &opt); |
| 334 | if (nest == NULL) |
| 335 | goto nla_put_failure; |
| 336 | if (q->mq) { |
| 337 | if (nla_put_flag(skb, TCA_PRIO_MQ) < 0) |
| 338 | goto nla_put_failure; |
| 339 | } |
| 340 | nla_nest_compat_end(skb, nest); |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | return skb->len; |
| 343 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 344 | nla_put_failure: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 345 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | return -1; |
| 347 | } |
| 348 | |
| 349 | static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new, |
| 350 | struct Qdisc **old) |
| 351 | { |
| 352 | struct prio_sched_data *q = qdisc_priv(sch); |
| 353 | unsigned long band = arg - 1; |
| 354 | |
| 355 | if (band >= q->bands) |
| 356 | return -EINVAL; |
| 357 | |
| 358 | if (new == NULL) |
| 359 | new = &noop_qdisc; |
| 360 | |
| 361 | sch_tree_lock(sch); |
| 362 | *old = q->queues[band]; |
| 363 | q->queues[band] = new; |
Patrick McHardy | 5e50da0 | 2006-11-29 17:36:20 -0800 | [diff] [blame] | 364 | qdisc_tree_decrease_qlen(*old, (*old)->q.qlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | qdisc_reset(*old); |
| 366 | sch_tree_unlock(sch); |
| 367 | |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | static struct Qdisc * |
| 372 | prio_leaf(struct Qdisc *sch, unsigned long arg) |
| 373 | { |
| 374 | struct prio_sched_data *q = qdisc_priv(sch); |
| 375 | unsigned long band = arg - 1; |
| 376 | |
| 377 | if (band >= q->bands) |
| 378 | return NULL; |
| 379 | |
| 380 | return q->queues[band]; |
| 381 | } |
| 382 | |
| 383 | static unsigned long prio_get(struct Qdisc *sch, u32 classid) |
| 384 | { |
| 385 | struct prio_sched_data *q = qdisc_priv(sch); |
| 386 | unsigned long band = TC_H_MIN(classid); |
| 387 | |
| 388 | if (band - 1 >= q->bands) |
| 389 | return 0; |
| 390 | return band; |
| 391 | } |
| 392 | |
| 393 | static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid) |
| 394 | { |
| 395 | return prio_get(sch, classid); |
| 396 | } |
| 397 | |
| 398 | |
| 399 | static void prio_put(struct Qdisc *q, unsigned long cl) |
| 400 | { |
| 401 | return; |
| 402 | } |
| 403 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 404 | static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct nlattr **tca, unsigned long *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | { |
| 406 | unsigned long cl = *arg; |
| 407 | struct prio_sched_data *q = qdisc_priv(sch); |
| 408 | |
| 409 | if (cl - 1 > q->bands) |
| 410 | return -ENOENT; |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | static int prio_delete(struct Qdisc *sch, unsigned long cl) |
| 415 | { |
| 416 | struct prio_sched_data *q = qdisc_priv(sch); |
| 417 | if (cl - 1 > q->bands) |
| 418 | return -ENOENT; |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | |
| 423 | static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb, |
| 424 | struct tcmsg *tcm) |
| 425 | { |
| 426 | struct prio_sched_data *q = qdisc_priv(sch); |
| 427 | |
| 428 | if (cl - 1 > q->bands) |
| 429 | return -ENOENT; |
| 430 | tcm->tcm_handle |= TC_H_MIN(cl); |
| 431 | if (q->queues[cl-1]) |
| 432 | tcm->tcm_info = q->queues[cl-1]->handle; |
| 433 | return 0; |
| 434 | } |
| 435 | |
Jarek Poplawski | 2cf6c36 | 2007-01-31 12:21:24 -0800 | [diff] [blame] | 436 | static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl, |
| 437 | struct gnet_dump *d) |
| 438 | { |
| 439 | struct prio_sched_data *q = qdisc_priv(sch); |
| 440 | struct Qdisc *cl_q; |
| 441 | |
| 442 | cl_q = q->queues[cl - 1]; |
| 443 | if (gnet_stats_copy_basic(d, &cl_q->bstats) < 0 || |
| 444 | gnet_stats_copy_queue(d, &cl_q->qstats) < 0) |
| 445 | return -1; |
| 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg) |
| 451 | { |
| 452 | struct prio_sched_data *q = qdisc_priv(sch); |
| 453 | int prio; |
| 454 | |
| 455 | if (arg->stop) |
| 456 | return; |
| 457 | |
| 458 | for (prio = 0; prio < q->bands; prio++) { |
| 459 | if (arg->count < arg->skip) { |
| 460 | arg->count++; |
| 461 | continue; |
| 462 | } |
| 463 | if (arg->fn(sch, prio+1, arg) < 0) { |
| 464 | arg->stop = 1; |
| 465 | break; |
| 466 | } |
| 467 | arg->count++; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl) |
| 472 | { |
| 473 | struct prio_sched_data *q = qdisc_priv(sch); |
| 474 | |
| 475 | if (cl) |
| 476 | return NULL; |
| 477 | return &q->filter_list; |
| 478 | } |
| 479 | |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 480 | static const struct Qdisc_class_ops prio_class_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | .graft = prio_graft, |
| 482 | .leaf = prio_leaf, |
| 483 | .get = prio_get, |
| 484 | .put = prio_put, |
| 485 | .change = prio_change, |
| 486 | .delete = prio_delete, |
| 487 | .walk = prio_walk, |
| 488 | .tcf_chain = prio_find_tcf, |
| 489 | .bind_tcf = prio_bind, |
| 490 | .unbind_tcf = prio_put, |
| 491 | .dump = prio_dump_class, |
Jarek Poplawski | 2cf6c36 | 2007-01-31 12:21:24 -0800 | [diff] [blame] | 492 | .dump_stats = prio_dump_class_stats, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | }; |
| 494 | |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 495 | static struct Qdisc_ops prio_qdisc_ops __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | .next = NULL, |
| 497 | .cl_ops = &prio_class_ops, |
| 498 | .id = "prio", |
| 499 | .priv_size = sizeof(struct prio_sched_data), |
| 500 | .enqueue = prio_enqueue, |
| 501 | .dequeue = prio_dequeue, |
| 502 | .requeue = prio_requeue, |
| 503 | .drop = prio_drop, |
| 504 | .init = prio_init, |
| 505 | .reset = prio_reset, |
| 506 | .destroy = prio_destroy, |
| 507 | .change = prio_tune, |
| 508 | .dump = prio_dump, |
| 509 | .owner = THIS_MODULE, |
| 510 | }; |
| 511 | |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 512 | static struct Qdisc_ops rr_qdisc_ops __read_mostly = { |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 513 | .next = NULL, |
| 514 | .cl_ops = &prio_class_ops, |
| 515 | .id = "rr", |
| 516 | .priv_size = sizeof(struct prio_sched_data), |
| 517 | .enqueue = prio_enqueue, |
| 518 | .dequeue = rr_dequeue, |
| 519 | .requeue = prio_requeue, |
| 520 | .drop = prio_drop, |
| 521 | .init = prio_init, |
| 522 | .reset = prio_reset, |
| 523 | .destroy = prio_destroy, |
| 524 | .change = prio_tune, |
| 525 | .dump = prio_dump, |
| 526 | .owner = THIS_MODULE, |
| 527 | }; |
| 528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | static int __init prio_module_init(void) |
| 530 | { |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 531 | int err; |
| 532 | |
| 533 | err = register_qdisc(&prio_qdisc_ops); |
| 534 | if (err < 0) |
| 535 | return err; |
| 536 | err = register_qdisc(&rr_qdisc_ops); |
| 537 | if (err < 0) |
| 538 | unregister_qdisc(&prio_qdisc_ops); |
| 539 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | } |
| 541 | |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 542 | static void __exit prio_module_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | { |
| 544 | unregister_qdisc(&prio_qdisc_ops); |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 545 | unregister_qdisc(&rr_qdisc_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | module_init(prio_module_init) |
| 549 | module_exit(prio_module_exit) |
| 550 | |
| 551 | MODULE_LICENSE("GPL"); |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 552 | MODULE_ALIAS("sch_rr"); |