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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/types.h> |
| 17 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/skbuff.h> |
Tianyi Gou | 835e8e8 | 2012-06-26 10:11:05 -0600 | [diff] [blame] | 21 | #include <linux/netdevice.h> |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 22 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <net/pkt_sched.h> |
| 24 | |
| 25 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 26 | struct prio_sched_data { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | int bands; |
| 28 | struct tcf_proto *filter_list; |
| 29 | u8 prio2band[TC_PRIO_MAX+1]; |
| 30 | struct Qdisc *queues[TCQ_PRIO_BANDS]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 31 | u8 enable_flow; |
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 | |
Jarek Poplawski | c27f339 | 2008-08-04 22:39:11 -0700 | [diff] [blame] | 43 | *qerr = NET_XMIT_SUCCESS | __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: |
Jarek Poplawski | 378a2f0 | 2008-08-04 22:31:03 -0700 | [diff] [blame] | 50 | *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 58 | return q->queues[q->prio2band[band & TC_PRIO_MAX]]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | } |
| 60 | band = res.classid; |
| 61 | } |
| 62 | band = TC_H_MIN(band) - 1; |
Jamal Hadi Salim | 3e5c2d3 | 2007-05-14 02:57:19 -0700 | [diff] [blame] | 63 | if (band >= q->bands) |
David S. Miller | 1d8ae3f | 2008-07-15 02:52:19 -0700 | [diff] [blame] | 64 | return q->queues[q->prio2band[0]]; |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | return q->queues[band]; |
| 67 | } |
| 68 | |
| 69 | static int |
| 70 | prio_enqueue(struct sk_buff *skb, struct Qdisc *sch) |
| 71 | { |
| 72 | struct Qdisc *qdisc; |
| 73 | int ret; |
| 74 | |
| 75 | qdisc = prio_classify(skb, sch, &ret); |
| 76 | #ifdef CONFIG_NET_CLS_ACT |
| 77 | if (qdisc == NULL) { |
Jamal Hadi Salim | 29f1df6 | 2006-01-08 22:35:55 -0800 | [diff] [blame] | 78 | |
Jarek Poplawski | c27f339 | 2008-08-04 22:39:11 -0700 | [diff] [blame] | 79 | if (ret & __NET_XMIT_BYPASS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | sch->qstats.drops++; |
| 81 | kfree_skb(skb); |
| 82 | return ret; |
| 83 | } |
| 84 | #endif |
| 85 | |
Jussi Kivilinna | 5f86173 | 2008-07-20 00:08:04 -0700 | [diff] [blame] | 86 | ret = qdisc_enqueue(skb, qdisc); |
| 87 | if (ret == NET_XMIT_SUCCESS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | sch->q.qlen++; |
| 89 | return NET_XMIT_SUCCESS; |
| 90 | } |
Jarek Poplawski | 378a2f0 | 2008-08-04 22:31:03 -0700 | [diff] [blame] | 91 | if (net_xmit_drop_count(ret)) |
| 92 | sch->qstats.drops++; |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 93 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Patrick McHardy | 48a8f51 | 2008-10-31 00:44:18 -0700 | [diff] [blame] | 96 | static struct sk_buff *prio_peek(struct Qdisc *sch) |
| 97 | { |
| 98 | struct prio_sched_data *q = qdisc_priv(sch); |
| 99 | int prio; |
| 100 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 101 | if (!q->enable_flow) |
| 102 | return NULL; |
| 103 | |
Patrick McHardy | 48a8f51 | 2008-10-31 00:44:18 -0700 | [diff] [blame] | 104 | for (prio = 0; prio < q->bands; prio++) { |
| 105 | struct Qdisc *qdisc = q->queues[prio]; |
| 106 | struct sk_buff *skb = qdisc->ops->peek(qdisc); |
| 107 | if (skb) |
| 108 | return skb; |
| 109 | } |
| 110 | return NULL; |
| 111 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 113 | static struct sk_buff *prio_dequeue(struct Qdisc *sch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | struct prio_sched_data *q = qdisc_priv(sch); |
| 116 | int prio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 118 | if (!q->enable_flow) |
| 119 | return NULL; |
| 120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | for (prio = 0; prio < q->bands; prio++) { |
David S. Miller | 1d8ae3f | 2008-07-15 02:52:19 -0700 | [diff] [blame] | 122 | struct Qdisc *qdisc = q->queues[prio]; |
Florian Westphal | 621ad27 | 2011-08-09 02:04:43 +0000 | [diff] [blame] | 123 | struct sk_buff *skb = qdisc_dequeue_peeked(qdisc); |
David S. Miller | 1d8ae3f | 2008-07-15 02:52:19 -0700 | [diff] [blame] | 124 | if (skb) { |
Eric Dumazet | 9190b3b | 2011-01-20 23:31:33 -0800 | [diff] [blame] | 125 | qdisc_bstats_update(sch, skb); |
David S. Miller | 1d8ae3f | 2008-07-15 02:52:19 -0700 | [diff] [blame] | 126 | sch->q.qlen--; |
| 127 | return skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | return NULL; |
| 131 | |
| 132 | } |
| 133 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 134 | static unsigned int prio_drop(struct Qdisc *sch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | { |
| 136 | struct prio_sched_data *q = qdisc_priv(sch); |
| 137 | int prio; |
| 138 | unsigned int len; |
| 139 | struct Qdisc *qdisc; |
| 140 | |
| 141 | for (prio = q->bands-1; prio >= 0; prio--) { |
| 142 | qdisc = q->queues[prio]; |
Patrick McHardy | 6d037a2 | 2006-03-20 19:00:49 -0800 | [diff] [blame] | 143 | if (qdisc->ops->drop && (len = qdisc->ops->drop(qdisc)) != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | sch->q.qlen--; |
| 145 | return len; |
| 146 | } |
| 147 | } |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | static void |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 153 | prio_reset(struct Qdisc *sch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
| 155 | int prio; |
| 156 | struct prio_sched_data *q = qdisc_priv(sch); |
| 157 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 158 | for (prio = 0; prio < q->bands; prio++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | qdisc_reset(q->queues[prio]); |
| 160 | sch->q.qlen = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 161 | q->enable_flow = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static void |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 165 | prio_destroy(struct Qdisc *sch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | { |
| 167 | int prio; |
| 168 | struct prio_sched_data *q = qdisc_priv(sch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Patrick McHardy | ff31ab5 | 2008-07-01 19:52:38 -0700 | [diff] [blame] | 170 | tcf_destroy_chain(&q->filter_list); |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 171 | for (prio = 0; prio < q->bands; prio++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | qdisc_destroy(q->queues[prio]); |
| 173 | } |
| 174 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 175 | static int prio_tune(struct Qdisc *sch, struct nlattr *opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | { |
| 177 | struct prio_sched_data *q = qdisc_priv(sch); |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 178 | struct tc_prio_qopt *qopt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | int i; |
Tianyi Gou | 835e8e8 | 2012-06-26 10:11:05 -0600 | [diff] [blame] | 180 | int flow_change = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
David S. Miller | 1d8ae3f | 2008-07-15 02:52:19 -0700 | [diff] [blame] | 182 | if (nla_len(opt) < sizeof(*qopt)) |
| 183 | return -EINVAL; |
| 184 | qopt = nla_data(opt); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 185 | |
David S. Miller | 1d8ae3f | 2008-07-15 02:52:19 -0700 | [diff] [blame] | 186 | if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | return -EINVAL; |
| 188 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 189 | for (i = 0; i <= TC_PRIO_MAX; i++) { |
David S. Miller | 1d8ae3f | 2008-07-15 02:52:19 -0700 | [diff] [blame] | 190 | if (qopt->priomap[i] >= qopt->bands) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | return -EINVAL; |
| 192 | } |
| 193 | |
| 194 | sch_tree_lock(sch); |
Tianyi Gou | 835e8e8 | 2012-06-26 10:11:05 -0600 | [diff] [blame] | 195 | if (q->enable_flow != qopt->enable_flow) { |
| 196 | q->enable_flow = qopt->enable_flow; |
| 197 | flow_change = 1; |
| 198 | } |
David S. Miller | 1d8ae3f | 2008-07-15 02:52:19 -0700 | [diff] [blame] | 199 | q->bands = qopt->bands; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1); |
| 201 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 202 | for (i = q->bands; i < TCQ_PRIO_BANDS; i++) { |
Patrick McHardy | b94c8af | 2008-11-20 04:11:36 -0800 | [diff] [blame] | 203 | struct Qdisc *child = q->queues[i]; |
| 204 | q->queues[i] = &noop_qdisc; |
Patrick McHardy | 5e50da0 | 2006-11-29 17:36:20 -0800 | [diff] [blame] | 205 | if (child != &noop_qdisc) { |
| 206 | qdisc_tree_decrease_qlen(child, child->q.qlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | qdisc_destroy(child); |
Patrick McHardy | 5e50da0 | 2006-11-29 17:36:20 -0800 | [diff] [blame] | 208 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | } |
| 210 | sch_tree_unlock(sch); |
| 211 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 212 | for (i = 0; i < q->bands; i++) { |
Amnon Aaronsohn | dd914b4 | 2006-01-17 02:24:26 -0800 | [diff] [blame] | 213 | if (q->queues[i] == &noop_qdisc) { |
Patrick McHardy | b94c8af | 2008-11-20 04:11:36 -0800 | [diff] [blame] | 214 | struct Qdisc *child, *old; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 215 | |
Changli Gao | 3511c91 | 2010-10-16 13:04:08 +0000 | [diff] [blame] | 216 | child = qdisc_create_dflt(sch->dev_queue, |
David S. Miller | bb949fb | 2008-07-08 16:55:56 -0700 | [diff] [blame] | 217 | &pfifo_qdisc_ops, |
Patrick McHardy | 9f9afec | 2006-11-29 17:35:18 -0800 | [diff] [blame] | 218 | TC_H_MAKE(sch->handle, i + 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | if (child) { |
| 220 | sch_tree_lock(sch); |
Patrick McHardy | b94c8af | 2008-11-20 04:11:36 -0800 | [diff] [blame] | 221 | old = q->queues[i]; |
| 222 | q->queues[i] = child; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
Patrick McHardy | b94c8af | 2008-11-20 04:11:36 -0800 | [diff] [blame] | 224 | if (old != &noop_qdisc) { |
| 225 | qdisc_tree_decrease_qlen(old, |
| 226 | old->q.qlen); |
| 227 | qdisc_destroy(old); |
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 | sch_tree_unlock(sch); |
| 230 | } |
| 231 | } |
| 232 | } |
Tianyi Gou | 835e8e8 | 2012-06-26 10:11:05 -0600 | [diff] [blame] | 233 | |
| 234 | /* Schedule qdisc when flow re-enabled */ |
| 235 | if (flow_change && q->enable_flow) { |
| 236 | if (!test_bit(__QDISC_STATE_DEACTIVATED, |
| 237 | &sch->state)) |
| 238 | __netif_schedule(qdisc_root(sch)); |
| 239 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | return 0; |
| 241 | } |
| 242 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 243 | static int prio_init(struct Qdisc *sch, struct nlattr *opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | { |
| 245 | struct prio_sched_data *q = qdisc_priv(sch); |
| 246 | int i; |
| 247 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 248 | for (i = 0; i < TCQ_PRIO_BANDS; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | q->queues[i] = &noop_qdisc; |
| 250 | |
| 251 | if (opt == NULL) { |
| 252 | return -EINVAL; |
| 253 | } else { |
| 254 | int err; |
| 255 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 256 | if ((err = prio_tune(sch, opt)) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | return err; |
| 258 | } |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | static int prio_dump(struct Qdisc *sch, struct sk_buff *skb) |
| 263 | { |
| 264 | struct prio_sched_data *q = qdisc_priv(sch); |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 265 | unsigned char *b = skb_tail_pointer(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | struct tc_prio_qopt opt; |
| 267 | |
| 268 | opt.bands = q->bands; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 269 | opt.enable_flow = q->enable_flow; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 270 | memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX + 1); |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 271 | |
Thomas Graf | 2c10b32 | 2008-09-02 17:30:27 -0700 | [diff] [blame] | 272 | NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt); |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | return skb->len; |
| 275 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 276 | nla_put_failure: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 277 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | return -1; |
| 279 | } |
| 280 | |
| 281 | static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new, |
| 282 | struct Qdisc **old) |
| 283 | { |
| 284 | struct prio_sched_data *q = qdisc_priv(sch); |
| 285 | unsigned long band = arg - 1; |
| 286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | if (new == NULL) |
| 288 | new = &noop_qdisc; |
| 289 | |
| 290 | sch_tree_lock(sch); |
| 291 | *old = q->queues[band]; |
| 292 | q->queues[band] = new; |
Patrick McHardy | 5e50da0 | 2006-11-29 17:36:20 -0800 | [diff] [blame] | 293 | qdisc_tree_decrease_qlen(*old, (*old)->q.qlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | qdisc_reset(*old); |
| 295 | sch_tree_unlock(sch); |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | static struct Qdisc * |
| 301 | prio_leaf(struct Qdisc *sch, unsigned long arg) |
| 302 | { |
| 303 | struct prio_sched_data *q = qdisc_priv(sch); |
| 304 | unsigned long band = arg - 1; |
| 305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | return q->queues[band]; |
| 307 | } |
| 308 | |
| 309 | static unsigned long prio_get(struct Qdisc *sch, u32 classid) |
| 310 | { |
| 311 | struct prio_sched_data *q = qdisc_priv(sch); |
| 312 | unsigned long band = TC_H_MIN(classid); |
| 313 | |
| 314 | if (band - 1 >= q->bands) |
| 315 | return 0; |
| 316 | return band; |
| 317 | } |
| 318 | |
| 319 | static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid) |
| 320 | { |
| 321 | return prio_get(sch, classid); |
| 322 | } |
| 323 | |
| 324 | |
| 325 | static void prio_put(struct Qdisc *q, unsigned long cl) |
| 326 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb, |
| 330 | struct tcmsg *tcm) |
| 331 | { |
| 332 | struct prio_sched_data *q = qdisc_priv(sch); |
| 333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | tcm->tcm_handle |= TC_H_MIN(cl); |
Patrick McHardy | 5b9a9cc | 2009-09-04 06:41:17 +0000 | [diff] [blame] | 335 | tcm->tcm_info = q->queues[cl-1]->handle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | return 0; |
| 337 | } |
| 338 | |
Jarek Poplawski | 2cf6c36 | 2007-01-31 12:21:24 -0800 | [diff] [blame] | 339 | static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl, |
| 340 | struct gnet_dump *d) |
| 341 | { |
| 342 | struct prio_sched_data *q = qdisc_priv(sch); |
| 343 | struct Qdisc *cl_q; |
| 344 | |
| 345 | cl_q = q->queues[cl - 1]; |
Jarek Poplawski | a19d215 | 2009-09-17 10:26:07 -0700 | [diff] [blame] | 346 | cl_q->qstats.qlen = cl_q->q.qlen; |
Jarek Poplawski | 2cf6c36 | 2007-01-31 12:21:24 -0800 | [diff] [blame] | 347 | if (gnet_stats_copy_basic(d, &cl_q->bstats) < 0 || |
| 348 | gnet_stats_copy_queue(d, &cl_q->qstats) < 0) |
| 349 | return -1; |
| 350 | |
| 351 | return 0; |
| 352 | } |
| 353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg) |
| 355 | { |
| 356 | struct prio_sched_data *q = qdisc_priv(sch); |
| 357 | int prio; |
| 358 | |
| 359 | if (arg->stop) |
| 360 | return; |
| 361 | |
| 362 | for (prio = 0; prio < q->bands; prio++) { |
| 363 | if (arg->count < arg->skip) { |
| 364 | arg->count++; |
| 365 | continue; |
| 366 | } |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 367 | if (arg->fn(sch, prio + 1, arg) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | arg->stop = 1; |
| 369 | break; |
| 370 | } |
| 371 | arg->count++; |
| 372 | } |
| 373 | } |
| 374 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 375 | static struct tcf_proto **prio_find_tcf(struct Qdisc *sch, unsigned long cl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | { |
| 377 | struct prio_sched_data *q = qdisc_priv(sch); |
| 378 | |
| 379 | if (cl) |
| 380 | return NULL; |
| 381 | return &q->filter_list; |
| 382 | } |
| 383 | |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 384 | static const struct Qdisc_class_ops prio_class_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | .graft = prio_graft, |
| 386 | .leaf = prio_leaf, |
| 387 | .get = prio_get, |
| 388 | .put = prio_put, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | .walk = prio_walk, |
| 390 | .tcf_chain = prio_find_tcf, |
| 391 | .bind_tcf = prio_bind, |
| 392 | .unbind_tcf = prio_put, |
| 393 | .dump = prio_dump_class, |
Jarek Poplawski | 2cf6c36 | 2007-01-31 12:21:24 -0800 | [diff] [blame] | 394 | .dump_stats = prio_dump_class_stats, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | }; |
| 396 | |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 397 | static struct Qdisc_ops prio_qdisc_ops __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | .next = NULL, |
| 399 | .cl_ops = &prio_class_ops, |
| 400 | .id = "prio", |
| 401 | .priv_size = sizeof(struct prio_sched_data), |
| 402 | .enqueue = prio_enqueue, |
| 403 | .dequeue = prio_dequeue, |
Patrick McHardy | 48a8f51 | 2008-10-31 00:44:18 -0700 | [diff] [blame] | 404 | .peek = prio_peek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | .drop = prio_drop, |
| 406 | .init = prio_init, |
| 407 | .reset = prio_reset, |
| 408 | .destroy = prio_destroy, |
| 409 | .change = prio_tune, |
| 410 | .dump = prio_dump, |
| 411 | .owner = THIS_MODULE, |
| 412 | }; |
| 413 | |
| 414 | static int __init prio_module_init(void) |
| 415 | { |
David S. Miller | 1d8ae3f | 2008-07-15 02:52:19 -0700 | [diff] [blame] | 416 | return register_qdisc(&prio_qdisc_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | } |
| 418 | |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 419 | static void __exit prio_module_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | { |
| 421 | unregister_qdisc(&prio_qdisc_ops); |
| 422 | } |
| 423 | |
| 424 | module_init(prio_module_init) |
| 425 | module_exit(prio_module_exit) |
| 426 | |
| 427 | MODULE_LICENSE("GPL"); |