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> |
Subash Abhinov Kasiviswanathan | fe6344b | 2017-08-25 17:58:46 -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; |
John Fastabend | 25d8c0d | 2014-09-12 20:05:27 -0700 | [diff] [blame] | 28 | struct tcf_proto __rcu *filter_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | u8 prio2band[TC_PRIO_MAX+1]; |
| 30 | struct Qdisc *queues[TCQ_PRIO_BANDS]; |
Subash Abhinov Kasiviswanathan | abb705d | 2017-03-22 17:34:29 -0600 | [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; |
John Fastabend | 25d8c0d | 2014-09-12 20:05:27 -0700 | [diff] [blame] | 41 | struct tcf_proto *fl; |
Patrick McHardy | bdba91e | 2007-07-30 17:07:14 -0700 | [diff] [blame] | 42 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Jarek Poplawski | c27f339 | 2008-08-04 22:39:11 -0700 | [diff] [blame] | 44 | *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | if (TC_H_MAJ(skb->priority) != sch->handle) { |
John Fastabend | 25d8c0d | 2014-09-12 20:05:27 -0700 | [diff] [blame] | 46 | fl = rcu_dereference_bh(q->filter_list); |
Daniel Borkmann | 3b3ae88 | 2015-08-26 23:00:06 +0200 | [diff] [blame] | 47 | err = tc_classify(skb, fl, &res, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #ifdef CONFIG_NET_CLS_ACT |
Lucas Nussbaum | dbaaa07 | 2007-08-30 22:35:46 -0700 | [diff] [blame] | 49 | switch (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | case TC_ACT_STOLEN: |
| 51 | case TC_ACT_QUEUED: |
Jarek Poplawski | 378a2f0 | 2008-08-04 22:31:03 -0700 | [diff] [blame] | 52 | *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | case TC_ACT_SHOT: |
| 54 | return NULL; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 55 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | #endif |
John Fastabend | 25d8c0d | 2014-09-12 20:05:27 -0700 | [diff] [blame] | 57 | if (!fl || err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | if (TC_H_MAJ(band)) |
| 59 | band = 0; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 60 | return q->queues[q->prio2band[band & TC_PRIO_MAX]]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | } |
| 62 | band = res.classid; |
| 63 | } |
| 64 | band = TC_H_MIN(band) - 1; |
Jamal Hadi Salim | 3e5c2d3 | 2007-05-14 02:57:19 -0700 | [diff] [blame] | 65 | if (band >= q->bands) |
David S. Miller | 1d8ae3f | 2008-07-15 02:52:19 -0700 | [diff] [blame] | 66 | return q->queues[q->prio2band[0]]; |
| 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | return q->queues[band]; |
| 69 | } |
| 70 | |
| 71 | static int |
Eric Dumazet | 520ac30 | 2016-06-21 23:16:49 -0700 | [diff] [blame] | 72 | prio_enqueue(struct sk_buff *skb, struct Qdisc *sch, struct sk_buff **to_free) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | { |
| 74 | struct Qdisc *qdisc; |
| 75 | int ret; |
| 76 | |
| 77 | qdisc = prio_classify(skb, sch, &ret); |
| 78 | #ifdef CONFIG_NET_CLS_ACT |
| 79 | if (qdisc == NULL) { |
Jamal Hadi Salim | 29f1df6 | 2006-01-08 22:35:55 -0800 | [diff] [blame] | 80 | |
Jarek Poplawski | c27f339 | 2008-08-04 22:39:11 -0700 | [diff] [blame] | 81 | if (ret & __NET_XMIT_BYPASS) |
John Fastabend | 25331d6 | 2014-09-28 11:53:29 -0700 | [diff] [blame] | 82 | qdisc_qstats_drop(sch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | kfree_skb(skb); |
| 84 | return ret; |
| 85 | } |
| 86 | #endif |
| 87 | |
Eric Dumazet | 520ac30 | 2016-06-21 23:16:49 -0700 | [diff] [blame] | 88 | ret = qdisc_enqueue(skb, qdisc, to_free); |
Jussi Kivilinna | 5f86173 | 2008-07-20 00:08:04 -0700 | [diff] [blame] | 89 | if (ret == NET_XMIT_SUCCESS) { |
WANG Cong | 6529d75 | 2016-06-01 16:15:16 -0700 | [diff] [blame] | 90 | qdisc_qstats_backlog_inc(sch, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | sch->q.qlen++; |
| 92 | return NET_XMIT_SUCCESS; |
| 93 | } |
Jarek Poplawski | 378a2f0 | 2008-08-04 22:31:03 -0700 | [diff] [blame] | 94 | if (net_xmit_drop_count(ret)) |
John Fastabend | 25331d6 | 2014-09-28 11:53:29 -0700 | [diff] [blame] | 95 | qdisc_qstats_drop(sch); |
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 | |
Patrick McHardy | 48a8f51 | 2008-10-31 00:44:18 -0700 | [diff] [blame] | 99 | static struct sk_buff *prio_peek(struct Qdisc *sch) |
| 100 | { |
| 101 | struct prio_sched_data *q = qdisc_priv(sch); |
| 102 | int prio; |
| 103 | |
Subash Abhinov Kasiviswanathan | abb705d | 2017-03-22 17:34:29 -0600 | [diff] [blame] | 104 | if (!q->enable_flow) |
| 105 | return NULL; |
| 106 | |
Patrick McHardy | 48a8f51 | 2008-10-31 00:44:18 -0700 | [diff] [blame] | 107 | for (prio = 0; prio < q->bands; prio++) { |
| 108 | struct Qdisc *qdisc = q->queues[prio]; |
| 109 | struct sk_buff *skb = qdisc->ops->peek(qdisc); |
| 110 | if (skb) |
| 111 | return skb; |
| 112 | } |
| 113 | return NULL; |
| 114 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 116 | static struct sk_buff *prio_dequeue(struct Qdisc *sch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | struct prio_sched_data *q = qdisc_priv(sch); |
| 119 | int prio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
Subash Abhinov Kasiviswanathan | abb705d | 2017-03-22 17:34:29 -0600 | [diff] [blame] | 121 | if (!q->enable_flow) |
| 122 | return NULL; |
| 123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | for (prio = 0; prio < q->bands; prio++) { |
David S. Miller | 1d8ae3f | 2008-07-15 02:52:19 -0700 | [diff] [blame] | 125 | struct Qdisc *qdisc = q->queues[prio]; |
Florian Westphal | 3557619 | 2011-08-09 02:04:43 +0000 | [diff] [blame] | 126 | struct sk_buff *skb = qdisc_dequeue_peeked(qdisc); |
David S. Miller | 1d8ae3f | 2008-07-15 02:52:19 -0700 | [diff] [blame] | 127 | if (skb) { |
Eric Dumazet | 9190b3b | 2011-01-20 23:31:33 -0800 | [diff] [blame] | 128 | qdisc_bstats_update(sch, skb); |
WANG Cong | 6529d75 | 2016-06-01 16:15:16 -0700 | [diff] [blame] | 129 | qdisc_qstats_backlog_dec(sch, skb); |
David S. Miller | 1d8ae3f | 2008-07-15 02:52:19 -0700 | [diff] [blame] | 130 | sch->q.qlen--; |
| 131 | return skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | return NULL; |
| 135 | |
| 136 | } |
| 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | static void |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 139 | prio_reset(struct Qdisc *sch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | { |
| 141 | int prio; |
| 142 | struct prio_sched_data *q = qdisc_priv(sch); |
| 143 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 144 | for (prio = 0; prio < q->bands; prio++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | qdisc_reset(q->queues[prio]); |
WANG Cong | 6529d75 | 2016-06-01 16:15:16 -0700 | [diff] [blame] | 146 | sch->qstats.backlog = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | sch->q.qlen = 0; |
Subash Abhinov Kasiviswanathan | abb705d | 2017-03-22 17:34:29 -0600 | [diff] [blame] | 148 | q->enable_flow = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static void |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 152 | prio_destroy(struct Qdisc *sch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | { |
| 154 | int prio; |
| 155 | struct prio_sched_data *q = qdisc_priv(sch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
Patrick McHardy | ff31ab5 | 2008-07-01 19:52:38 -0700 | [diff] [blame] | 157 | tcf_destroy_chain(&q->filter_list); |
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_destroy(q->queues[prio]); |
| 160 | } |
| 161 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 162 | static int prio_tune(struct Qdisc *sch, struct nlattr *opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | { |
| 164 | struct prio_sched_data *q = qdisc_priv(sch); |
Eric Dumazet | 3d7c825 | 2016-06-13 11:33:32 -0700 | [diff] [blame] | 165 | struct Qdisc *queues[TCQ_PRIO_BANDS]; |
| 166 | int oldbands = q->bands, i; |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 167 | struct tc_prio_qopt *qopt; |
Subash Abhinov Kasiviswanathan | fe6344b | 2017-08-25 17:58:46 -0600 | [diff] [blame] | 168 | int flow_change = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
David S. Miller | 1d8ae3f | 2008-07-15 02:52:19 -0700 | [diff] [blame] | 170 | if (nla_len(opt) < sizeof(*qopt)) |
| 171 | return -EINVAL; |
| 172 | qopt = nla_data(opt); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 173 | |
David S. Miller | 1d8ae3f | 2008-07-15 02:52:19 -0700 | [diff] [blame] | 174 | if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | return -EINVAL; |
| 176 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 177 | for (i = 0; i <= TC_PRIO_MAX; i++) { |
David S. Miller | 1d8ae3f | 2008-07-15 02:52:19 -0700 | [diff] [blame] | 178 | if (qopt->priomap[i] >= qopt->bands) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | return -EINVAL; |
| 180 | } |
| 181 | |
Eric Dumazet | 3d7c825 | 2016-06-13 11:33:32 -0700 | [diff] [blame] | 182 | /* Before commit, make sure we can allocate all new qdiscs */ |
| 183 | for (i = oldbands; i < qopt->bands; i++) { |
| 184 | queues[i] = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops, |
| 185 | TC_H_MAKE(sch->handle, i + 1)); |
| 186 | if (!queues[i]) { |
| 187 | while (i > oldbands) |
| 188 | qdisc_destroy(queues[--i]); |
| 189 | return -ENOMEM; |
| 190 | } |
| 191 | } |
| 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | sch_tree_lock(sch); |
Subash Abhinov Kasiviswanathan | fe6344b | 2017-08-25 17:58:46 -0600 | [diff] [blame] | 194 | if (q->enable_flow != qopt->enable_flow) { |
| 195 | q->enable_flow = qopt->enable_flow; |
| 196 | flow_change = 1; |
| 197 | } |
David S. Miller | 1d8ae3f | 2008-07-15 02:52:19 -0700 | [diff] [blame] | 198 | q->bands = qopt->bands; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1); |
| 200 | |
Eric Dumazet | 3d7c825 | 2016-06-13 11:33:32 -0700 | [diff] [blame] | 201 | for (i = q->bands; i < oldbands; i++) { |
Patrick McHardy | b94c8af | 2008-11-20 04:11:36 -0800 | [diff] [blame] | 202 | struct Qdisc *child = q->queues[i]; |
Eric Dumazet | 3d7c825 | 2016-06-13 11:33:32 -0700 | [diff] [blame] | 203 | |
| 204 | qdisc_tree_reduce_backlog(child, child->q.qlen, |
| 205 | child->qstats.backlog); |
| 206 | qdisc_destroy(child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
Eric Dumazet | 3d7c825 | 2016-06-13 11:33:32 -0700 | [diff] [blame] | 208 | |
| 209 | for (i = oldbands; i < q->bands; i++) |
| 210 | q->queues[i] = queues[i]; |
| 211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | sch_tree_unlock(sch); |
Subash Abhinov Kasiviswanathan | fe6344b | 2017-08-25 17:58:46 -0600 | [diff] [blame] | 213 | |
| 214 | /* Schedule qdisc when flow re-enabled */ |
| 215 | if (flow_change && q->enable_flow) { |
| 216 | if (!test_bit(__QDISC_STATE_DEACTIVATED, &sch->state)) |
| 217 | __netif_schedule(qdisc_root(sch)); |
| 218 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | return 0; |
| 220 | } |
| 221 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 222 | static int prio_init(struct Qdisc *sch, struct nlattr *opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | { |
Eric Dumazet | 3d7c825 | 2016-06-13 11:33:32 -0700 | [diff] [blame] | 224 | if (!opt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
Eric Dumazet | 3d7c825 | 2016-06-13 11:33:32 -0700 | [diff] [blame] | 227 | return prio_tune(sch, opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static int prio_dump(struct Qdisc *sch, struct sk_buff *skb) |
| 231 | { |
| 232 | struct prio_sched_data *q = qdisc_priv(sch); |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 233 | unsigned char *b = skb_tail_pointer(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | struct tc_prio_qopt opt; |
| 235 | |
| 236 | opt.bands = q->bands; |
Subash Abhinov Kasiviswanathan | abb705d | 2017-03-22 17:34:29 -0600 | [diff] [blame] | 237 | opt.enable_flow = q->enable_flow; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 238 | memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX + 1); |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 239 | |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 240 | if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt)) |
| 241 | goto nla_put_failure; |
Peter P Waskiewicz Jr | d62733c | 2007-06-28 21:04:31 -0700 | [diff] [blame] | 242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | return skb->len; |
| 244 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 245 | nla_put_failure: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 246 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new, |
| 251 | struct Qdisc **old) |
| 252 | { |
| 253 | struct prio_sched_data *q = qdisc_priv(sch); |
| 254 | unsigned long band = arg - 1; |
| 255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | if (new == NULL) |
| 257 | new = &noop_qdisc; |
| 258 | |
WANG Cong | 86a7996 | 2016-02-25 14:55:00 -0800 | [diff] [blame] | 259 | *old = qdisc_replace(sch, new, &q->queues[band]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | static struct Qdisc * |
| 264 | prio_leaf(struct Qdisc *sch, unsigned long arg) |
| 265 | { |
| 266 | struct prio_sched_data *q = qdisc_priv(sch); |
| 267 | unsigned long band = arg - 1; |
| 268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | return q->queues[band]; |
| 270 | } |
| 271 | |
| 272 | static unsigned long prio_get(struct Qdisc *sch, u32 classid) |
| 273 | { |
| 274 | struct prio_sched_data *q = qdisc_priv(sch); |
| 275 | unsigned long band = TC_H_MIN(classid); |
| 276 | |
| 277 | if (band - 1 >= q->bands) |
| 278 | return 0; |
| 279 | return band; |
| 280 | } |
| 281 | |
| 282 | static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid) |
| 283 | { |
| 284 | return prio_get(sch, classid); |
| 285 | } |
| 286 | |
| 287 | |
| 288 | static void prio_put(struct Qdisc *q, unsigned long cl) |
| 289 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb, |
| 293 | struct tcmsg *tcm) |
| 294 | { |
| 295 | struct prio_sched_data *q = qdisc_priv(sch); |
| 296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | tcm->tcm_handle |= TC_H_MIN(cl); |
Patrick McHardy | 5b9a9cc | 2009-09-04 06:41:17 +0000 | [diff] [blame] | 298 | tcm->tcm_info = q->queues[cl-1]->handle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | return 0; |
| 300 | } |
| 301 | |
Jarek Poplawski | 2cf6c36 | 2007-01-31 12:21:24 -0800 | [diff] [blame] | 302 | static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl, |
| 303 | struct gnet_dump *d) |
| 304 | { |
| 305 | struct prio_sched_data *q = qdisc_priv(sch); |
| 306 | struct Qdisc *cl_q; |
| 307 | |
| 308 | cl_q = q->queues[cl - 1]; |
Eric Dumazet | edb09eb | 2016-06-06 09:37:16 -0700 | [diff] [blame] | 309 | if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch), |
| 310 | d, NULL, &cl_q->bstats) < 0 || |
John Fastabend | b0ab6f9 | 2014-09-28 11:54:24 -0700 | [diff] [blame] | 311 | gnet_stats_copy_queue(d, NULL, &cl_q->qstats, cl_q->q.qlen) < 0) |
Jarek Poplawski | 2cf6c36 | 2007-01-31 12:21:24 -0800 | [diff] [blame] | 312 | return -1; |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg) |
| 318 | { |
| 319 | struct prio_sched_data *q = qdisc_priv(sch); |
| 320 | int prio; |
| 321 | |
| 322 | if (arg->stop) |
| 323 | return; |
| 324 | |
| 325 | for (prio = 0; prio < q->bands; prio++) { |
| 326 | if (arg->count < arg->skip) { |
| 327 | arg->count++; |
| 328 | continue; |
| 329 | } |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 330 | if (arg->fn(sch, prio + 1, arg) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | arg->stop = 1; |
| 332 | break; |
| 333 | } |
| 334 | arg->count++; |
| 335 | } |
| 336 | } |
| 337 | |
John Fastabend | 25d8c0d | 2014-09-12 20:05:27 -0700 | [diff] [blame] | 338 | static struct tcf_proto __rcu **prio_find_tcf(struct Qdisc *sch, |
| 339 | unsigned long cl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | { |
| 341 | struct prio_sched_data *q = qdisc_priv(sch); |
| 342 | |
| 343 | if (cl) |
| 344 | return NULL; |
| 345 | return &q->filter_list; |
| 346 | } |
| 347 | |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 348 | static const struct Qdisc_class_ops prio_class_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | .graft = prio_graft, |
| 350 | .leaf = prio_leaf, |
| 351 | .get = prio_get, |
| 352 | .put = prio_put, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | .walk = prio_walk, |
| 354 | .tcf_chain = prio_find_tcf, |
| 355 | .bind_tcf = prio_bind, |
| 356 | .unbind_tcf = prio_put, |
| 357 | .dump = prio_dump_class, |
Jarek Poplawski | 2cf6c36 | 2007-01-31 12:21:24 -0800 | [diff] [blame] | 358 | .dump_stats = prio_dump_class_stats, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | }; |
| 360 | |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 361 | static struct Qdisc_ops prio_qdisc_ops __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | .next = NULL, |
| 363 | .cl_ops = &prio_class_ops, |
| 364 | .id = "prio", |
| 365 | .priv_size = sizeof(struct prio_sched_data), |
| 366 | .enqueue = prio_enqueue, |
| 367 | .dequeue = prio_dequeue, |
Patrick McHardy | 48a8f51 | 2008-10-31 00:44:18 -0700 | [diff] [blame] | 368 | .peek = prio_peek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | .init = prio_init, |
| 370 | .reset = prio_reset, |
| 371 | .destroy = prio_destroy, |
| 372 | .change = prio_tune, |
| 373 | .dump = prio_dump, |
| 374 | .owner = THIS_MODULE, |
| 375 | }; |
| 376 | |
| 377 | static int __init prio_module_init(void) |
| 378 | { |
David S. Miller | 1d8ae3f | 2008-07-15 02:52:19 -0700 | [diff] [blame] | 379 | return register_qdisc(&prio_qdisc_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | } |
| 381 | |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 382 | static void __exit prio_module_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | { |
| 384 | unregister_qdisc(&prio_qdisc_ops); |
| 385 | } |
| 386 | |
| 387 | module_init(prio_module_init) |
| 388 | module_exit(prio_module_exit) |
| 389 | |
| 390 | MODULE_LICENSE("GPL"); |