blob: 049714c57075c6acb212e4cfb47d962fda67b2bb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* net/sched/sch_dsmark.c - Differentiated Services field marker */
2
3/* Written 1998-2000 by Werner Almesberger, EPFL ICA */
4
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/module.h>
7#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/types.h>
10#include <linux/string.h>
11#include <linux/errno.h>
12#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/rtnetlink.h>
David S. Miller5b0ac722008-01-21 02:21:45 -080014#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <net/pkt_sched.h>
Jiri Pirkocf1facd2017-02-09 14:38:56 +010016#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <net/dsfield.h>
18#include <net/inet_ecn.h>
19#include <asm/byteorder.h>
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021/*
22 * classid class marking
23 * ------- ----- -------
24 * n/a 0 n/a
25 * x:0 1 use entry [0]
26 * ... ... ...
27 * x:y y>0 y+1 use entry [y]
28 * ... ... ...
29 * x:indices-1 indices use entry [indices-1]
30 * ... ... ...
31 * x:y y+1 use entry [y & (indices-1)]
32 * ... ... ...
33 * 0xffff 0x10000 use entry [indices-1]
34 */
35
36
37#define NO_DEFAULT_INDEX (1 << 16)
38
Eric Dumazet47bbbb32015-09-17 16:37:13 -070039struct mask_value {
40 u8 mask;
41 u8 value;
42};
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044struct dsmark_qdisc_data {
45 struct Qdisc *q;
John Fastabend25d8c0d2014-09-12 20:05:27 -070046 struct tcf_proto __rcu *filter_list;
Jiri Pirko6529eab2017-05-17 11:07:55 +020047 struct tcf_block *block;
Eric Dumazet47bbbb32015-09-17 16:37:13 -070048 struct mask_value *mv;
Thomas Grafaf0d1142005-06-18 22:53:29 -070049 u16 indices;
Eric Dumazet47bbbb32015-09-17 16:37:13 -070050 u8 set_tc_index;
Thomas Grafaf0d1142005-06-18 22:53:29 -070051 u32 default_index; /* index range is 0...0xffff */
Eric Dumazet47bbbb32015-09-17 16:37:13 -070052#define DSMARK_EMBEDDED_SZ 16
53 struct mask_value embedded[DSMARK_EMBEDDED_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -070054};
55
Thomas Graf758cc43c2005-06-18 22:52:54 -070056static inline int dsmark_valid_index(struct dsmark_qdisc_data *p, u16 index)
57{
Yang Yingliang17569fa2013-12-10 20:55:29 +080058 return index <= p->indices && index > 0;
Thomas Graf758cc43c2005-06-18 22:52:54 -070059}
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/* ------------------------- Class/flow operations ------------------------- */
62
Thomas Grafaf0d1142005-06-18 22:53:29 -070063static int dsmark_graft(struct Qdisc *sch, unsigned long arg,
Alexander Aring653d6fd2017-12-20 12:35:17 -050064 struct Qdisc *new, struct Qdisc **old,
65 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Stephen Hemminger81da99e2008-01-21 00:50:09 -080067 struct dsmark_qdisc_data *p = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Yang Yingliangc76f2a22013-12-23 17:39:00 +080069 pr_debug("%s(sch %p,[qdisc %p],new %p,old %p)\n",
70 __func__, sch, p, new, old);
Thomas Graf486b53e2005-05-31 15:16:52 -070071
72 if (new == NULL) {
Changli Gao3511c912010-10-16 13:04:08 +000073 new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
Alexander Aringa38a98822017-12-20 12:35:21 -050074 sch->handle, NULL);
Thomas Graf486b53e2005-05-31 15:16:52 -070075 if (new == NULL)
76 new = &noop_qdisc;
77 }
78
WANG Cong86a79962016-02-25 14:55:00 -080079 *old = qdisc_replace(sch, new, &p->q);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090080 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static struct Qdisc *dsmark_leaf(struct Qdisc *sch, unsigned long arg)
84{
Stephen Hemminger81da99e2008-01-21 00:50:09 -080085 struct dsmark_qdisc_data *p = qdisc_priv(sch);
86 return p->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
WANG Cong143976c2017-08-24 16:51:29 -070089static unsigned long dsmark_find(struct Qdisc *sch, u32 classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Thomas Grafaf0d1142005-06-18 22:53:29 -070091 return TC_H_MIN(classid) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static unsigned long dsmark_bind_filter(struct Qdisc *sch,
Thomas Grafaf0d1142005-06-18 22:53:29 -070095 unsigned long parent, u32 classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
WANG Cong143976c2017-08-24 16:51:29 -070097 pr_debug("%s(sch %p,[qdisc %p],classid %x)\n",
98 __func__, sch, qdisc_priv(sch), classid);
99
100 return dsmark_find(sch, classid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
WANG Cong143976c2017-08-24 16:51:29 -0700103static void dsmark_unbind_filter(struct Qdisc *sch, unsigned long cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105}
106
Patrick McHardy27a34212008-01-23 20:35:39 -0800107static const struct nla_policy dsmark_policy[TCA_DSMARK_MAX + 1] = {
108 [TCA_DSMARK_INDICES] = { .type = NLA_U16 },
109 [TCA_DSMARK_DEFAULT_INDEX] = { .type = NLA_U16 },
110 [TCA_DSMARK_SET_TC_INDEX] = { .type = NLA_FLAG },
111 [TCA_DSMARK_MASK] = { .type = NLA_U8 },
112 [TCA_DSMARK_VALUE] = { .type = NLA_U8 },
113};
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115static int dsmark_change(struct Qdisc *sch, u32 classid, u32 parent,
Alexander Aring793d81d2017-12-20 12:35:15 -0500116 struct nlattr **tca, unsigned long *arg,
117 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Stephen Hemminger81da99e2008-01-21 00:50:09 -0800119 struct dsmark_qdisc_data *p = qdisc_priv(sch);
Patrick McHardy1e904742008-01-22 22:11:17 -0800120 struct nlattr *opt = tca[TCA_OPTIONS];
121 struct nlattr *tb[TCA_DSMARK_MAX + 1];
Thomas Graf758cc43c2005-06-18 22:52:54 -0700122 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Yang Yingliangc76f2a22013-12-23 17:39:00 +0800124 pr_debug("%s(sch %p,[qdisc %p],classid %x,parent %x), arg 0x%lx\n",
125 __func__, sch, p, classid, parent, *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Thomas Graf758cc43c2005-06-18 22:52:54 -0700127 if (!dsmark_valid_index(p, *arg)) {
128 err = -ENOENT;
Patrick McHardy1e904742008-01-22 22:11:17 -0800129 goto errout;
Thomas Graf758cc43c2005-06-18 22:52:54 -0700130 }
131
Patrick McHardycee63722008-01-23 20:33:32 -0800132 if (!opt)
Patrick McHardy1e904742008-01-22 22:11:17 -0800133 goto errout;
Thomas Graf758cc43c2005-06-18 22:52:54 -0700134
Johannes Bergfceb6432017-04-12 14:34:07 +0200135 err = nla_parse_nested(tb, TCA_DSMARK_MAX, opt, dsmark_policy, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800136 if (err < 0)
Patrick McHardy27a34212008-01-23 20:35:39 -0800137 goto errout;
Patrick McHardycee63722008-01-23 20:33:32 -0800138
Patrick McHardy27a34212008-01-23 20:35:39 -0800139 if (tb[TCA_DSMARK_VALUE])
Eric Dumazet47bbbb32015-09-17 16:37:13 -0700140 p->mv[*arg - 1].value = nla_get_u8(tb[TCA_DSMARK_VALUE]);
Thomas Graf758cc43c2005-06-18 22:52:54 -0700141
Patrick McHardy1e904742008-01-22 22:11:17 -0800142 if (tb[TCA_DSMARK_MASK])
Eric Dumazet47bbbb32015-09-17 16:37:13 -0700143 p->mv[*arg - 1].mask = nla_get_u8(tb[TCA_DSMARK_MASK]);
Thomas Graf758cc43c2005-06-18 22:52:54 -0700144
145 err = 0;
146
Patrick McHardy1e904742008-01-22 22:11:17 -0800147errout:
Thomas Graf758cc43c2005-06-18 22:52:54 -0700148 return err;
149}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Thomas Grafaf0d1142005-06-18 22:53:29 -0700151static int dsmark_delete(struct Qdisc *sch, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Stephen Hemminger81da99e2008-01-21 00:50:09 -0800153 struct dsmark_qdisc_data *p = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Thomas Grafaf0d1142005-06-18 22:53:29 -0700155 if (!dsmark_valid_index(p, arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return -EINVAL;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900157
Eric Dumazet47bbbb32015-09-17 16:37:13 -0700158 p->mv[arg - 1].mask = 0xff;
159 p->mv[arg - 1].value = 0;
Thomas Grafaf0d1142005-06-18 22:53:29 -0700160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return 0;
162}
163
Stephen Hemminger9d127fb2008-01-21 02:24:21 -0800164static void dsmark_walk(struct Qdisc *sch, struct qdisc_walker *walker)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Stephen Hemminger81da99e2008-01-21 00:50:09 -0800166 struct dsmark_qdisc_data *p = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 int i;
168
Yang Yingliangc76f2a22013-12-23 17:39:00 +0800169 pr_debug("%s(sch %p,[qdisc %p],walker %p)\n",
170 __func__, sch, p, walker);
Thomas Grafaf0d1142005-06-18 22:53:29 -0700171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (walker->stop)
173 return;
Thomas Grafaf0d1142005-06-18 22:53:29 -0700174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 for (i = 0; i < p->indices; i++) {
Eric Dumazet47bbbb32015-09-17 16:37:13 -0700176 if (p->mv[i].mask == 0xff && !p->mv[i].value)
Thomas Graf0451eb02005-05-31 15:15:58 -0700177 goto ignore;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (walker->count >= walker->skip) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000179 if (walker->fn(sch, i + 1, walker) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 walker->stop = 1;
181 break;
182 }
183 }
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900184ignore:
Thomas Graf0451eb02005-05-31 15:15:58 -0700185 walker->count++;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Alexander Aringcbaacc42017-12-20 12:35:16 -0500189static struct tcf_block *dsmark_tcf_block(struct Qdisc *sch, unsigned long cl,
190 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Stephen Hemminger81da99e2008-01-21 00:50:09 -0800192 struct dsmark_qdisc_data *p = qdisc_priv(sch);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200193
194 return p->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197/* --------------------------- Qdisc operations ---------------------------- */
198
Eric Dumazet520ac302016-06-21 23:16:49 -0700199static int dsmark_enqueue(struct sk_buff *skb, struct Qdisc *sch,
200 struct sk_buff **to_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Stephen Hemminger81da99e2008-01-21 00:50:09 -0800202 struct dsmark_qdisc_data *p = qdisc_priv(sch);
Thomas Grafaf0d1142005-06-18 22:53:29 -0700203 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Yang Yingliangc76f2a22013-12-23 17:39:00 +0800205 pr_debug("%s(skb %p,sch %p,[qdisc %p])\n", __func__, skb, sch, p);
Thomas Grafaf0d1142005-06-18 22:53:29 -0700206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (p->set_tc_index) {
Eric Dumazetaea92fb2017-03-17 08:05:28 -0700208 int wlen = skb_network_offset(skb);
209
Jiri Pirkod8b96052015-01-13 17:13:43 +0100210 switch (tc_skb_protocol(skb)) {
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -0700211 case htons(ETH_P_IP):
Eric Dumazetaea92fb2017-03-17 08:05:28 -0700212 wlen += sizeof(struct iphdr);
213 if (!pskb_may_pull(skb, wlen) ||
214 skb_try_make_writable(skb, wlen))
Stephen Hemminger9d127fb2008-01-21 02:24:21 -0800215 goto drop;
Stephen Hemminger4c307192008-01-21 02:23:49 -0800216
Stephen Hemminger9d127fb2008-01-21 02:24:21 -0800217 skb->tc_index = ipv4_get_dsfield(ip_hdr(skb))
218 & ~INET_ECN_MASK;
219 break;
Stephen Hemminger4c307192008-01-21 02:23:49 -0800220
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -0700221 case htons(ETH_P_IPV6):
Eric Dumazetaea92fb2017-03-17 08:05:28 -0700222 wlen += sizeof(struct ipv6hdr);
223 if (!pskb_may_pull(skb, wlen) ||
224 skb_try_make_writable(skb, wlen))
Stephen Hemminger9d127fb2008-01-21 02:24:21 -0800225 goto drop;
Stephen Hemminger4c307192008-01-21 02:23:49 -0800226
Stephen Hemminger9d127fb2008-01-21 02:24:21 -0800227 skb->tc_index = ipv6_get_dsfield(ipv6_hdr(skb))
228 & ~INET_ECN_MASK;
229 break;
230 default:
231 skb->tc_index = 0;
232 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
Thomas Grafaf0d1142005-06-18 22:53:29 -0700235
236 if (TC_H_MAJ(skb->priority) == sch->handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 skb->tc_index = TC_H_MIN(skb->priority);
Thomas Grafaf0d1142005-06-18 22:53:29 -0700238 else {
239 struct tcf_result res;
John Fastabend25d8c0d2014-09-12 20:05:27 -0700240 struct tcf_proto *fl = rcu_dereference_bh(p->filter_list);
Jiri Pirko87d83092017-05-17 11:07:54 +0200241 int result = tcf_classify(skb, fl, &res, false);
Thomas Grafaf0d1142005-06-18 22:53:29 -0700242
Stephen Hemminger81da99e2008-01-21 00:50:09 -0800243 pr_debug("result %d class 0x%04x\n", result, res.classid);
Thomas Grafaf0d1142005-06-18 22:53:29 -0700244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 switch (result) {
Patrick McHardyf6853e22007-07-15 00:02:10 -0700246#ifdef CONFIG_NET_CLS_ACT
247 case TC_ACT_QUEUED:
248 case TC_ACT_STOLEN:
Jiri Pirkoe25ea212017-06-06 14:12:02 +0200249 case TC_ACT_TRAP:
Eric Dumazet520ac302016-06-21 23:16:49 -0700250 __qdisc_drop(skb, to_free);
Jarek Poplawski378a2f02008-08-04 22:31:03 -0700251 return NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
Stephen Hemminger4c307192008-01-21 02:23:49 -0800252
Patrick McHardyf6853e22007-07-15 00:02:10 -0700253 case TC_ACT_SHOT:
Stephen Hemminger4c307192008-01-21 02:23:49 -0800254 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255#endif
Patrick McHardyc3bc7cf2007-07-15 00:03:05 -0700256 case TC_ACT_OK:
Patrick McHardyf6853e22007-07-15 00:02:10 -0700257 skb->tc_index = TC_H_MIN(res.classid);
258 break;
Stephen Hemminger4c307192008-01-21 02:23:49 -0800259
Patrick McHardyf6853e22007-07-15 00:02:10 -0700260 default:
261 if (p->default_index != NO_DEFAULT_INDEX)
262 skb->tc_index = p->default_index;
263 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Eric Dumazet520ac302016-06-21 23:16:49 -0700267 err = qdisc_enqueue(skb, p->q, to_free);
Thomas Grafaf0d1142005-06-18 22:53:29 -0700268 if (err != NET_XMIT_SUCCESS) {
Jarek Poplawski378a2f02008-08-04 22:31:03 -0700269 if (net_xmit_drop_count(err))
John Fastabend25331d62014-09-28 11:53:29 -0700270 qdisc_qstats_drop(sch);
Thomas Grafaf0d1142005-06-18 22:53:29 -0700271 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
Thomas Grafaf0d1142005-06-18 22:53:29 -0700273
WANG Congbdf17662016-02-25 14:55:03 -0800274 qdisc_qstats_backlog_inc(sch, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 sch->q.qlen++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Thomas Grafaf0d1142005-06-18 22:53:29 -0700277 return NET_XMIT_SUCCESS;
Stephen Hemminger4c307192008-01-21 02:23:49 -0800278
279drop:
Eric Dumazet520ac302016-06-21 23:16:49 -0700280 qdisc_drop(skb, sch, to_free);
Jarek Poplawskic27f3392008-08-04 22:39:11 -0700281 return NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
Thomas Grafaf0d1142005-06-18 22:53:29 -0700282}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284static struct sk_buff *dsmark_dequeue(struct Qdisc *sch)
285{
Stephen Hemminger81da99e2008-01-21 00:50:09 -0800286 struct dsmark_qdisc_data *p = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 struct sk_buff *skb;
Thomas Grafaf0d1142005-06-18 22:53:29 -0700288 u32 index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Yang Yingliangc76f2a22013-12-23 17:39:00 +0800290 pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
Thomas Grafaf0d1142005-06-18 22:53:29 -0700291
Kyeong Yoof8b33d82016-03-07 17:07:57 +1300292 skb = qdisc_dequeue_peeked(p->q);
Thomas Grafaf0d1142005-06-18 22:53:29 -0700293 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return NULL;
Thomas Grafaf0d1142005-06-18 22:53:29 -0700295
Eric Dumazet9190b3b2011-01-20 23:31:33 -0800296 qdisc_bstats_update(sch, skb);
WANG Congbdf17662016-02-25 14:55:03 -0800297 qdisc_qstats_backlog_dec(sch, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 sch->q.qlen--;
Thomas Grafaf0d1142005-06-18 22:53:29 -0700299
300 index = skb->tc_index & (p->indices - 1);
Stephen Hemminger81da99e2008-01-21 00:50:09 -0800301 pr_debug("index %d->%d\n", skb->tc_index, index);
Thomas Grafaf0d1142005-06-18 22:53:29 -0700302
Jiri Pirkod8b96052015-01-13 17:13:43 +0100303 switch (tc_skb_protocol(skb)) {
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -0700304 case htons(ETH_P_IP):
Eric Dumazet47bbbb32015-09-17 16:37:13 -0700305 ipv4_change_dsfield(ip_hdr(skb), p->mv[index].mask,
306 p->mv[index].value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 break;
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -0700308 case htons(ETH_P_IPV6):
Eric Dumazet47bbbb32015-09-17 16:37:13 -0700309 ipv6_change_dsfield(ipv6_hdr(skb), p->mv[index].mask,
310 p->mv[index].value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 break;
Stephen Hemminger9d127fb2008-01-21 02:24:21 -0800312 default:
313 /*
314 * Only complain if a change was actually attempted.
315 * This way, we can send non-IP traffic through dsmark
316 * and don't need yet another qdisc as a bypass.
317 */
Eric Dumazet47bbbb32015-09-17 16:37:13 -0700318 if (p->mv[index].mask != 0xff || p->mv[index].value)
Yang Yingliangc76f2a22013-12-23 17:39:00 +0800319 pr_warn("%s: unsupported protocol %d\n",
Jiri Pirkod8b96052015-01-13 17:13:43 +0100320 __func__, ntohs(tc_skb_protocol(skb)));
Stephen Hemminger9d127fb2008-01-21 02:24:21 -0800321 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700322 }
Thomas Grafaf0d1142005-06-18 22:53:29 -0700323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return skb;
325}
326
Jarek Poplawski8e3af972008-10-31 00:45:55 -0700327static struct sk_buff *dsmark_peek(struct Qdisc *sch)
328{
329 struct dsmark_qdisc_data *p = qdisc_priv(sch);
330
Yang Yingliangc76f2a22013-12-23 17:39:00 +0800331 pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
Jarek Poplawski8e3af972008-10-31 00:45:55 -0700332
333 return p->q->ops->peek(p->q);
334}
335
Alexander Aringe63d7df2017-12-20 12:35:13 -0500336static int dsmark_init(struct Qdisc *sch, struct nlattr *opt,
337 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Stephen Hemminger81da99e2008-01-21 00:50:09 -0800339 struct dsmark_qdisc_data *p = qdisc_priv(sch);
Patrick McHardy1e904742008-01-22 22:11:17 -0800340 struct nlattr *tb[TCA_DSMARK_MAX + 1];
David S. Miller9d4f97f2017-05-17 16:03:16 -0400341 int err = -EINVAL;
Thomas Graf758cc43c2005-06-18 22:52:54 -0700342 u32 default_index = NO_DEFAULT_INDEX;
343 u16 indices;
Eric Dumazet47bbbb32015-09-17 16:37:13 -0700344 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Yang Yingliangc76f2a22013-12-23 17:39:00 +0800346 pr_debug("%s(sch %p,[qdisc %p],opt %p)\n", __func__, sch, p, opt);
Thomas Graf758cc43c2005-06-18 22:52:54 -0700347
Patrick McHardycee63722008-01-23 20:33:32 -0800348 if (!opt)
Thomas Graf758cc43c2005-06-18 22:52:54 -0700349 goto errout;
350
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500351 err = tcf_block_get(&p->block, &p->filter_list, sch, extack);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200352 if (err)
353 return err;
354
Johannes Bergfceb6432017-04-12 14:34:07 +0200355 err = nla_parse_nested(tb, TCA_DSMARK_MAX, opt, dsmark_policy, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800356 if (err < 0)
357 goto errout;
358
359 err = -EINVAL;
Patrick McHardy1e904742008-01-22 22:11:17 -0800360 indices = nla_get_u16(tb[TCA_DSMARK_INDICES]);
David S. Miller5b0ac722008-01-21 02:21:45 -0800361
362 if (hweight32(indices) != 1)
Thomas Graf758cc43c2005-06-18 22:52:54 -0700363 goto errout;
364
Patrick McHardy27a34212008-01-23 20:35:39 -0800365 if (tb[TCA_DSMARK_DEFAULT_INDEX])
Patrick McHardy1e904742008-01-22 22:11:17 -0800366 default_index = nla_get_u16(tb[TCA_DSMARK_DEFAULT_INDEX]);
Thomas Graf758cc43c2005-06-18 22:52:54 -0700367
Eric Dumazet47bbbb32015-09-17 16:37:13 -0700368 if (indices <= DSMARK_EMBEDDED_SZ)
369 p->mv = p->embedded;
370 else
371 p->mv = kmalloc_array(indices, sizeof(*p->mv), GFP_KERNEL);
372 if (!p->mv) {
Thomas Graf758cc43c2005-06-18 22:52:54 -0700373 err = -ENOMEM;
374 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
Eric Dumazet47bbbb32015-09-17 16:37:13 -0700376 for (i = 0; i < indices; i++) {
377 p->mv[i].mask = 0xff;
378 p->mv[i].value = 0;
379 }
Thomas Graf758cc43c2005-06-18 22:52:54 -0700380 p->indices = indices;
381 p->default_index = default_index;
Patrick McHardy1e904742008-01-22 22:11:17 -0800382 p->set_tc_index = nla_get_flag(tb[TCA_DSMARK_SET_TC_INDEX]);
Thomas Graf758cc43c2005-06-18 22:52:54 -0700383
Alexander Aringa38a98822017-12-20 12:35:21 -0500384 p->q = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops, sch->handle,
385 NULL);
Thomas Graf758cc43c2005-06-18 22:52:54 -0700386 if (p->q == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 p->q = &noop_qdisc;
Jiri Kosina49b49972017-03-08 16:03:32 +0100388 else
389 qdisc_hash_add(p->q, true);
Thomas Graf758cc43c2005-06-18 22:52:54 -0700390
Yang Yingliangc76f2a22013-12-23 17:39:00 +0800391 pr_debug("%s: qdisc %p\n", __func__, p->q);
Thomas Graf758cc43c2005-06-18 22:52:54 -0700392
393 err = 0;
394errout:
Thomas Graf758cc43c2005-06-18 22:52:54 -0700395 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398static void dsmark_reset(struct Qdisc *sch)
399{
Stephen Hemminger81da99e2008-01-21 00:50:09 -0800400 struct dsmark_qdisc_data *p = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Yang Yingliangc76f2a22013-12-23 17:39:00 +0800402 pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 qdisc_reset(p->q);
WANG Congbdf17662016-02-25 14:55:03 -0800404 sch->qstats.backlog = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 sch->q.qlen = 0;
406}
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408static void dsmark_destroy(struct Qdisc *sch)
409{
Stephen Hemminger81da99e2008-01-21 00:50:09 -0800410 struct dsmark_qdisc_data *p = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Yang Yingliangc76f2a22013-12-23 17:39:00 +0800412 pr_debug("%s(sch %p,[qdisc %p])\n", __func__, sch, p);
Thomas Grafaf0d1142005-06-18 22:53:29 -0700413
Jiri Pirko6529eab2017-05-17 11:07:55 +0200414 tcf_block_put(p->block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 qdisc_destroy(p->q);
Eric Dumazet47bbbb32015-09-17 16:37:13 -0700416 if (p->mv != p->embedded)
417 kfree(p->mv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420static int dsmark_dump_class(struct Qdisc *sch, unsigned long cl,
Thomas Graf02f23f02005-06-18 22:53:12 -0700421 struct sk_buff *skb, struct tcmsg *tcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Stephen Hemminger81da99e2008-01-21 00:50:09 -0800423 struct dsmark_qdisc_data *p = qdisc_priv(sch);
Patrick McHardy1e904742008-01-22 22:11:17 -0800424 struct nlattr *opts = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Yang Yingliangc76f2a22013-12-23 17:39:00 +0800426 pr_debug("%s(sch %p,[qdisc %p],class %ld\n", __func__, sch, p, cl);
Thomas Graf02f23f02005-06-18 22:53:12 -0700427
428 if (!dsmark_valid_index(p, cl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return -EINVAL;
Thomas Graf02f23f02005-06-18 22:53:12 -0700430
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000431 tcm->tcm_handle = TC_H_MAKE(TC_H_MAJ(sch->handle), cl - 1);
Patrick McHardycdc7f8e2006-03-20 19:01:06 -0800432 tcm->tcm_info = p->q->handle;
Thomas Graf02f23f02005-06-18 22:53:12 -0700433
Patrick McHardy1e904742008-01-22 22:11:17 -0800434 opts = nla_nest_start(skb, TCA_OPTIONS);
435 if (opts == NULL)
436 goto nla_put_failure;
Eric Dumazet47bbbb32015-09-17 16:37:13 -0700437 if (nla_put_u8(skb, TCA_DSMARK_MASK, p->mv[cl - 1].mask) ||
438 nla_put_u8(skb, TCA_DSMARK_VALUE, p->mv[cl - 1].value))
David S. Miller1b34ec42012-03-29 05:11:39 -0400439 goto nla_put_failure;
Thomas Graf02f23f02005-06-18 22:53:12 -0700440
Patrick McHardy1e904742008-01-22 22:11:17 -0800441 return nla_nest_end(skb, opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Patrick McHardy1e904742008-01-22 22:11:17 -0800443nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700444 nla_nest_cancel(skb, opts);
445 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
448static int dsmark_dump(struct Qdisc *sch, struct sk_buff *skb)
449{
Stephen Hemminger81da99e2008-01-21 00:50:09 -0800450 struct dsmark_qdisc_data *p = qdisc_priv(sch);
Patrick McHardy1e904742008-01-22 22:11:17 -0800451 struct nlattr *opts = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Patrick McHardy1e904742008-01-22 22:11:17 -0800453 opts = nla_nest_start(skb, TCA_OPTIONS);
454 if (opts == NULL)
455 goto nla_put_failure;
David S. Miller1b34ec42012-03-29 05:11:39 -0400456 if (nla_put_u16(skb, TCA_DSMARK_INDICES, p->indices))
457 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
David S. Miller1b34ec42012-03-29 05:11:39 -0400459 if (p->default_index != NO_DEFAULT_INDEX &&
460 nla_put_u16(skb, TCA_DSMARK_DEFAULT_INDEX, p->default_index))
461 goto nla_put_failure;
Thomas Graf02f23f02005-06-18 22:53:12 -0700462
David S. Miller1b34ec42012-03-29 05:11:39 -0400463 if (p->set_tc_index &&
464 nla_put_flag(skb, TCA_DSMARK_SET_TC_INDEX))
465 goto nla_put_failure;
Thomas Graf02f23f02005-06-18 22:53:12 -0700466
Patrick McHardy1e904742008-01-22 22:11:17 -0800467 return nla_nest_end(skb, opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Patrick McHardy1e904742008-01-22 22:11:17 -0800469nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700470 nla_nest_cancel(skb, opts);
471 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Eric Dumazet20fea082007-11-14 01:44:41 -0800474static const struct Qdisc_class_ops dsmark_class_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 .graft = dsmark_graft,
476 .leaf = dsmark_leaf,
WANG Cong143976c2017-08-24 16:51:29 -0700477 .find = dsmark_find,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 .change = dsmark_change,
479 .delete = dsmark_delete,
480 .walk = dsmark_walk,
Jiri Pirko6529eab2017-05-17 11:07:55 +0200481 .tcf_block = dsmark_tcf_block,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 .bind_tcf = dsmark_bind_filter,
WANG Cong143976c2017-08-24 16:51:29 -0700483 .unbind_tcf = dsmark_unbind_filter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 .dump = dsmark_dump_class,
485};
486
Eric Dumazet20fea082007-11-14 01:44:41 -0800487static struct Qdisc_ops dsmark_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 .next = NULL,
489 .cl_ops = &dsmark_class_ops,
490 .id = "dsmark",
491 .priv_size = sizeof(struct dsmark_qdisc_data),
492 .enqueue = dsmark_enqueue,
493 .dequeue = dsmark_dequeue,
Jarek Poplawski8e3af972008-10-31 00:45:55 -0700494 .peek = dsmark_peek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 .init = dsmark_init,
496 .reset = dsmark_reset,
497 .destroy = dsmark_destroy,
498 .change = NULL,
499 .dump = dsmark_dump,
500 .owner = THIS_MODULE,
501};
502
503static int __init dsmark_module_init(void)
504{
505 return register_qdisc(&dsmark_qdisc_ops);
506}
Thomas Grafaf0d1142005-06-18 22:53:29 -0700507
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900508static void __exit dsmark_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 unregister_qdisc(&dsmark_qdisc_ops);
511}
Thomas Grafaf0d1142005-06-18 22:53:29 -0700512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513module_init(dsmark_module_init)
514module_exit(dsmark_module_exit)
Thomas Grafaf0d1142005-06-18 22:53:29 -0700515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516MODULE_LICENSE("GPL");