net sched actions: aggregate dumping of actions timeinfo
[deliverable/linux.git] / net / sched / cls_flower.c
CommitLineData
77b9900e
JP
1/*
2 * net/sched/cls_flower.c Flower classifier
3 *
4 * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#include <linux/kernel.h>
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/rhashtable.h>
16
17#include <linux/if_ether.h>
18#include <linux/in6.h>
19#include <linux/ip.h>
20
21#include <net/sch_generic.h>
22#include <net/pkt_cls.h>
23#include <net/ip.h>
24#include <net/flow_dissector.h>
25
26struct fl_flow_key {
27 int indev_ifindex;
42aecaa9 28 struct flow_dissector_key_control control;
77b9900e
JP
29 struct flow_dissector_key_basic basic;
30 struct flow_dissector_key_eth_addrs eth;
c3f83241 31 struct flow_dissector_key_addrs ipaddrs;
77b9900e 32 union {
c3f83241 33 struct flow_dissector_key_ipv4_addrs ipv4;
77b9900e
JP
34 struct flow_dissector_key_ipv6_addrs ipv6;
35 };
36 struct flow_dissector_key_ports tp;
37} __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
38
39struct fl_flow_mask_range {
40 unsigned short int start;
41 unsigned short int end;
42};
43
44struct fl_flow_mask {
45 struct fl_flow_key key;
46 struct fl_flow_mask_range range;
47 struct rcu_head rcu;
48};
49
50struct cls_fl_head {
51 struct rhashtable ht;
52 struct fl_flow_mask mask;
53 struct flow_dissector dissector;
54 u32 hgen;
55 bool mask_assigned;
56 struct list_head filters;
57 struct rhashtable_params ht_params;
58 struct rcu_head rcu;
59};
60
61struct cls_fl_filter {
62 struct rhash_head ht_node;
63 struct fl_flow_key mkey;
64 struct tcf_exts exts;
65 struct tcf_result res;
66 struct fl_flow_key key;
67 struct list_head list;
68 u32 handle;
e69985c6 69 u32 flags;
77b9900e
JP
70 struct rcu_head rcu;
71};
72
73static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
74{
75 return mask->range.end - mask->range.start;
76}
77
78static void fl_mask_update_range(struct fl_flow_mask *mask)
79{
80 const u8 *bytes = (const u8 *) &mask->key;
81 size_t size = sizeof(mask->key);
82 size_t i, first = 0, last = size - 1;
83
84 for (i = 0; i < sizeof(mask->key); i++) {
85 if (bytes[i]) {
86 if (!first && i)
87 first = i;
88 last = i;
89 }
90 }
91 mask->range.start = rounddown(first, sizeof(long));
92 mask->range.end = roundup(last + 1, sizeof(long));
93}
94
95static void *fl_key_get_start(struct fl_flow_key *key,
96 const struct fl_flow_mask *mask)
97{
98 return (u8 *) key + mask->range.start;
99}
100
101static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
102 struct fl_flow_mask *mask)
103{
104 const long *lkey = fl_key_get_start(key, mask);
105 const long *lmask = fl_key_get_start(&mask->key, mask);
106 long *lmkey = fl_key_get_start(mkey, mask);
107 int i;
108
109 for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
110 *lmkey++ = *lkey++ & *lmask++;
111}
112
113static void fl_clear_masked_range(struct fl_flow_key *key,
114 struct fl_flow_mask *mask)
115{
116 memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
117}
118
119static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
120 struct tcf_result *res)
121{
122 struct cls_fl_head *head = rcu_dereference_bh(tp->root);
123 struct cls_fl_filter *f;
124 struct fl_flow_key skb_key;
125 struct fl_flow_key skb_mkey;
126
e69985c6
AV
127 if (!atomic_read(&head->ht.nelems))
128 return -1;
129
77b9900e
JP
130 fl_clear_masked_range(&skb_key, &head->mask);
131 skb_key.indev_ifindex = skb->skb_iif;
132 /* skb_flow_dissect() does not set n_proto in case an unknown protocol,
133 * so do it rather here.
134 */
135 skb_key.basic.n_proto = skb->protocol;
cd79a238 136 skb_flow_dissect(skb, &head->dissector, &skb_key, 0);
77b9900e
JP
137
138 fl_set_masked_key(&skb_mkey, &skb_key, &head->mask);
139
140 f = rhashtable_lookup_fast(&head->ht,
141 fl_key_get_start(&skb_mkey, &head->mask),
142 head->ht_params);
e69985c6 143 if (f && !(f->flags & TCA_CLS_FLAGS_SKIP_SW)) {
77b9900e
JP
144 *res = f->res;
145 return tcf_exts_exec(skb, &f->exts, res);
146 }
147 return -1;
148}
149
150static int fl_init(struct tcf_proto *tp)
151{
152 struct cls_fl_head *head;
153
154 head = kzalloc(sizeof(*head), GFP_KERNEL);
155 if (!head)
156 return -ENOBUFS;
157
158 INIT_LIST_HEAD_RCU(&head->filters);
159 rcu_assign_pointer(tp->root, head);
160
161 return 0;
162}
163
164static void fl_destroy_filter(struct rcu_head *head)
165{
166 struct cls_fl_filter *f = container_of(head, struct cls_fl_filter, rcu);
167
168 tcf_exts_destroy(&f->exts);
169 kfree(f);
170}
171
8208d21b 172static void fl_hw_destroy_filter(struct tcf_proto *tp, unsigned long cookie)
5b33f488
AV
173{
174 struct net_device *dev = tp->q->dev_queue->dev;
175 struct tc_cls_flower_offload offload = {0};
176 struct tc_to_netdev tc;
177
178 if (!tc_should_offload(dev, 0))
179 return;
180
181 offload.command = TC_CLSFLOWER_DESTROY;
182 offload.cookie = cookie;
183
184 tc.type = TC_SETUP_CLSFLOWER;
185 tc.cls_flower = &offload;
186
187 dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol, &tc);
188}
189
190static void fl_hw_replace_filter(struct tcf_proto *tp,
191 struct flow_dissector *dissector,
192 struct fl_flow_key *mask,
193 struct fl_flow_key *key,
194 struct tcf_exts *actions,
8208d21b 195 unsigned long cookie, u32 flags)
5b33f488
AV
196{
197 struct net_device *dev = tp->q->dev_queue->dev;
198 struct tc_cls_flower_offload offload = {0};
199 struct tc_to_netdev tc;
200
201 if (!tc_should_offload(dev, flags))
202 return;
203
204 offload.command = TC_CLSFLOWER_REPLACE;
205 offload.cookie = cookie;
206 offload.dissector = dissector;
207 offload.mask = mask;
208 offload.key = key;
209 offload.exts = actions;
210
211 tc.type = TC_SETUP_CLSFLOWER;
212 tc.cls_flower = &offload;
213
214 dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol, &tc);
215}
216
10cbc684
AV
217static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f)
218{
219 struct net_device *dev = tp->q->dev_queue->dev;
220 struct tc_cls_flower_offload offload = {0};
221 struct tc_to_netdev tc;
222
223 if (!tc_should_offload(dev, 0))
224 return;
225
226 offload.command = TC_CLSFLOWER_STATS;
227 offload.cookie = (unsigned long)f;
228 offload.exts = &f->exts;
229
230 tc.type = TC_SETUP_CLSFLOWER;
231 tc.cls_flower = &offload;
232
233 dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol, &tc);
234}
235
77b9900e
JP
236static bool fl_destroy(struct tcf_proto *tp, bool force)
237{
238 struct cls_fl_head *head = rtnl_dereference(tp->root);
239 struct cls_fl_filter *f, *next;
240
241 if (!force && !list_empty(&head->filters))
242 return false;
243
244 list_for_each_entry_safe(f, next, &head->filters, list) {
8208d21b 245 fl_hw_destroy_filter(tp, (unsigned long)f);
77b9900e
JP
246 list_del_rcu(&f->list);
247 call_rcu(&f->rcu, fl_destroy_filter);
248 }
249 RCU_INIT_POINTER(tp->root, NULL);
250 if (head->mask_assigned)
251 rhashtable_destroy(&head->ht);
252 kfree_rcu(head, rcu);
253 return true;
254}
255
256static unsigned long fl_get(struct tcf_proto *tp, u32 handle)
257{
258 struct cls_fl_head *head = rtnl_dereference(tp->root);
259 struct cls_fl_filter *f;
260
261 list_for_each_entry(f, &head->filters, list)
262 if (f->handle == handle)
263 return (unsigned long) f;
264 return 0;
265}
266
267static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
268 [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC },
269 [TCA_FLOWER_CLASSID] = { .type = NLA_U32 },
270 [TCA_FLOWER_INDEV] = { .type = NLA_STRING,
271 .len = IFNAMSIZ },
272 [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN },
273 [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN },
274 [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN },
275 [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN },
276 [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 },
277 [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 },
278 [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 },
279 [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 },
280 [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 },
281 [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 },
282 [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
283 [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
284 [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) },
285 [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
286 [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 },
287 [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 },
b175c3a4
JHS
288 [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 },
289 [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 },
77b9900e
JP
290};
291
292static void fl_set_key_val(struct nlattr **tb,
293 void *val, int val_type,
294 void *mask, int mask_type, int len)
295{
296 if (!tb[val_type])
297 return;
298 memcpy(val, nla_data(tb[val_type]), len);
299 if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
300 memset(mask, 0xff, len);
301 else
302 memcpy(mask, nla_data(tb[mask_type]), len);
303}
304
305static int fl_set_key(struct net *net, struct nlattr **tb,
306 struct fl_flow_key *key, struct fl_flow_key *mask)
307{
dd3aa3b5 308#ifdef CONFIG_NET_CLS_IND
77b9900e 309 if (tb[TCA_FLOWER_INDEV]) {
dd3aa3b5 310 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV]);
77b9900e
JP
311 if (err < 0)
312 return err;
313 key->indev_ifindex = err;
314 mask->indev_ifindex = 0xffffffff;
315 }
dd3aa3b5 316#endif
77b9900e
JP
317
318 fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
319 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
320 sizeof(key->eth.dst));
321 fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
322 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
323 sizeof(key->eth.src));
66530bdf 324
77b9900e
JP
325 fl_set_key_val(tb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
326 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
327 sizeof(key->basic.n_proto));
66530bdf 328
77b9900e
JP
329 if (key->basic.n_proto == htons(ETH_P_IP) ||
330 key->basic.n_proto == htons(ETH_P_IPV6)) {
331 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
332 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
333 sizeof(key->basic.ip_proto));
334 }
66530bdf
JHS
335
336 if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
337 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
77b9900e
JP
338 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
339 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
340 sizeof(key->ipv4.src));
341 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
342 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
343 sizeof(key->ipv4.dst));
66530bdf
JHS
344 } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
345 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
77b9900e
JP
346 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
347 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
348 sizeof(key->ipv6.src));
349 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
350 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
351 sizeof(key->ipv6.dst));
352 }
66530bdf 353
77b9900e
JP
354 if (key->basic.ip_proto == IPPROTO_TCP) {
355 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
356 &mask->tp.src, TCA_FLOWER_UNSPEC,
357 sizeof(key->tp.src));
358 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
359 &mask->tp.dst, TCA_FLOWER_UNSPEC,
360 sizeof(key->tp.dst));
361 } else if (key->basic.ip_proto == IPPROTO_UDP) {
362 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
363 &mask->tp.src, TCA_FLOWER_UNSPEC,
364 sizeof(key->tp.src));
365 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
366 &mask->tp.dst, TCA_FLOWER_UNSPEC,
367 sizeof(key->tp.dst));
368 }
369
370 return 0;
371}
372
373static bool fl_mask_eq(struct fl_flow_mask *mask1,
374 struct fl_flow_mask *mask2)
375{
376 const long *lmask1 = fl_key_get_start(&mask1->key, mask1);
377 const long *lmask2 = fl_key_get_start(&mask2->key, mask2);
378
379 return !memcmp(&mask1->range, &mask2->range, sizeof(mask1->range)) &&
380 !memcmp(lmask1, lmask2, fl_mask_range(mask1));
381}
382
383static const struct rhashtable_params fl_ht_params = {
384 .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
385 .head_offset = offsetof(struct cls_fl_filter, ht_node),
386 .automatic_shrinking = true,
387};
388
389static int fl_init_hashtable(struct cls_fl_head *head,
390 struct fl_flow_mask *mask)
391{
392 head->ht_params = fl_ht_params;
393 head->ht_params.key_len = fl_mask_range(mask);
394 head->ht_params.key_offset += mask->range.start;
395
396 return rhashtable_init(&head->ht, &head->ht_params);
397}
398
399#define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
400#define FL_KEY_MEMBER_SIZE(member) (sizeof(((struct fl_flow_key *) 0)->member))
401#define FL_KEY_MEMBER_END_OFFSET(member) \
402 (FL_KEY_MEMBER_OFFSET(member) + FL_KEY_MEMBER_SIZE(member))
403
404#define FL_KEY_IN_RANGE(mask, member) \
405 (FL_KEY_MEMBER_OFFSET(member) <= (mask)->range.end && \
406 FL_KEY_MEMBER_END_OFFSET(member) >= (mask)->range.start)
407
408#define FL_KEY_SET(keys, cnt, id, member) \
409 do { \
410 keys[cnt].key_id = id; \
411 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
412 cnt++; \
413 } while(0);
414
415#define FL_KEY_SET_IF_IN_RANGE(mask, keys, cnt, id, member) \
416 do { \
417 if (FL_KEY_IN_RANGE(mask, member)) \
418 FL_KEY_SET(keys, cnt, id, member); \
419 } while(0);
420
421static void fl_init_dissector(struct cls_fl_head *head,
422 struct fl_flow_mask *mask)
423{
424 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
425 size_t cnt = 0;
426
42aecaa9 427 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
77b9900e
JP
428 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
429 FL_KEY_SET_IF_IN_RANGE(mask, keys, cnt,
430 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
431 FL_KEY_SET_IF_IN_RANGE(mask, keys, cnt,
432 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
433 FL_KEY_SET_IF_IN_RANGE(mask, keys, cnt,
434 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
435 FL_KEY_SET_IF_IN_RANGE(mask, keys, cnt,
436 FLOW_DISSECTOR_KEY_PORTS, tp);
437
438 skb_flow_dissector_init(&head->dissector, keys, cnt);
439}
440
441static int fl_check_assign_mask(struct cls_fl_head *head,
442 struct fl_flow_mask *mask)
443{
444 int err;
445
446 if (head->mask_assigned) {
447 if (!fl_mask_eq(&head->mask, mask))
448 return -EINVAL;
449 else
450 return 0;
451 }
452
453 /* Mask is not assigned yet. So assign it and init hashtable
454 * according to that.
455 */
456 err = fl_init_hashtable(head, mask);
457 if (err)
458 return err;
459 memcpy(&head->mask, mask, sizeof(head->mask));
460 head->mask_assigned = true;
461
462 fl_init_dissector(head, mask);
463
464 return 0;
465}
466
467static int fl_set_parms(struct net *net, struct tcf_proto *tp,
468 struct cls_fl_filter *f, struct fl_flow_mask *mask,
469 unsigned long base, struct nlattr **tb,
470 struct nlattr *est, bool ovr)
471{
472 struct tcf_exts e;
473 int err;
474
475 tcf_exts_init(&e, TCA_FLOWER_ACT, 0);
476 err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
477 if (err < 0)
478 return err;
479
480 if (tb[TCA_FLOWER_CLASSID]) {
481 f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
482 tcf_bind_filter(tp, &f->res, base);
483 }
484
485 err = fl_set_key(net, tb, &f->key, &mask->key);
486 if (err)
487 goto errout;
488
489 fl_mask_update_range(mask);
490 fl_set_masked_key(&f->mkey, &f->key, mask);
491
492 tcf_exts_change(tp, &f->exts, &e);
493
494 return 0;
495errout:
496 tcf_exts_destroy(&e);
497 return err;
498}
499
500static u32 fl_grab_new_handle(struct tcf_proto *tp,
501 struct cls_fl_head *head)
502{
503 unsigned int i = 0x80000000;
504 u32 handle;
505
506 do {
507 if (++head->hgen == 0x7FFFFFFF)
508 head->hgen = 1;
509 } while (--i > 0 && fl_get(tp, head->hgen));
510
511 if (unlikely(i == 0)) {
512 pr_err("Insufficient number of handles\n");
513 handle = 0;
514 } else {
515 handle = head->hgen;
516 }
517
518 return handle;
519}
520
521static int fl_change(struct net *net, struct sk_buff *in_skb,
522 struct tcf_proto *tp, unsigned long base,
523 u32 handle, struct nlattr **tca,
524 unsigned long *arg, bool ovr)
525{
526 struct cls_fl_head *head = rtnl_dereference(tp->root);
527 struct cls_fl_filter *fold = (struct cls_fl_filter *) *arg;
528 struct cls_fl_filter *fnew;
529 struct nlattr *tb[TCA_FLOWER_MAX + 1];
530 struct fl_flow_mask mask = {};
531 int err;
532
533 if (!tca[TCA_OPTIONS])
534 return -EINVAL;
535
536 err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS], fl_policy);
537 if (err < 0)
538 return err;
539
540 if (fold && handle && fold->handle != handle)
541 return -EINVAL;
542
543 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
544 if (!fnew)
545 return -ENOBUFS;
546
547 tcf_exts_init(&fnew->exts, TCA_FLOWER_ACT, 0);
548
549 if (!handle) {
550 handle = fl_grab_new_handle(tp, head);
551 if (!handle) {
552 err = -EINVAL;
553 goto errout;
554 }
555 }
556 fnew->handle = handle;
557
e69985c6
AV
558 if (tb[TCA_FLOWER_FLAGS]) {
559 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
560
561 if (!tc_flags_valid(fnew->flags)) {
562 err = -EINVAL;
563 goto errout;
564 }
565 }
5b33f488 566
77b9900e
JP
567 err = fl_set_parms(net, tp, fnew, &mask, base, tb, tca[TCA_RATE], ovr);
568 if (err)
569 goto errout;
570
571 err = fl_check_assign_mask(head, &mask);
572 if (err)
573 goto errout;
574
e69985c6
AV
575 if (!(fnew->flags & TCA_CLS_FLAGS_SKIP_SW)) {
576 err = rhashtable_insert_fast(&head->ht, &fnew->ht_node,
577 head->ht_params);
578 if (err)
579 goto errout;
580 }
5b33f488
AV
581
582 fl_hw_replace_filter(tp,
583 &head->dissector,
584 &mask.key,
585 &fnew->key,
586 &fnew->exts,
8208d21b 587 (unsigned long)fnew,
e69985c6 588 fnew->flags);
5b33f488
AV
589
590 if (fold) {
77b9900e
JP
591 rhashtable_remove_fast(&head->ht, &fold->ht_node,
592 head->ht_params);
8208d21b 593 fl_hw_destroy_filter(tp, (unsigned long)fold);
5b33f488 594 }
77b9900e
JP
595
596 *arg = (unsigned long) fnew;
597
598 if (fold) {
ff3532f2 599 list_replace_rcu(&fold->list, &fnew->list);
77b9900e
JP
600 tcf_unbind_filter(tp, &fold->res);
601 call_rcu(&fold->rcu, fl_destroy_filter);
602 } else {
603 list_add_tail_rcu(&fnew->list, &head->filters);
604 }
605
606 return 0;
607
608errout:
609 kfree(fnew);
610 return err;
611}
612
613static int fl_delete(struct tcf_proto *tp, unsigned long arg)
614{
615 struct cls_fl_head *head = rtnl_dereference(tp->root);
616 struct cls_fl_filter *f = (struct cls_fl_filter *) arg;
617
618 rhashtable_remove_fast(&head->ht, &f->ht_node,
619 head->ht_params);
620 list_del_rcu(&f->list);
8208d21b 621 fl_hw_destroy_filter(tp, (unsigned long)f);
77b9900e
JP
622 tcf_unbind_filter(tp, &f->res);
623 call_rcu(&f->rcu, fl_destroy_filter);
624 return 0;
625}
626
627static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg)
628{
629 struct cls_fl_head *head = rtnl_dereference(tp->root);
630 struct cls_fl_filter *f;
631
632 list_for_each_entry_rcu(f, &head->filters, list) {
633 if (arg->count < arg->skip)
634 goto skip;
635 if (arg->fn(tp, (unsigned long) f, arg) < 0) {
636 arg->stop = 1;
637 break;
638 }
639skip:
640 arg->count++;
641 }
642}
643
644static int fl_dump_key_val(struct sk_buff *skb,
645 void *val, int val_type,
646 void *mask, int mask_type, int len)
647{
648 int err;
649
650 if (!memchr_inv(mask, 0, len))
651 return 0;
652 err = nla_put(skb, val_type, len, val);
653 if (err)
654 return err;
655 if (mask_type != TCA_FLOWER_UNSPEC) {
656 err = nla_put(skb, mask_type, len, mask);
657 if (err)
658 return err;
659 }
660 return 0;
661}
662
663static int fl_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
664 struct sk_buff *skb, struct tcmsg *t)
665{
666 struct cls_fl_head *head = rtnl_dereference(tp->root);
667 struct cls_fl_filter *f = (struct cls_fl_filter *) fh;
668 struct nlattr *nest;
669 struct fl_flow_key *key, *mask;
670
671 if (!f)
672 return skb->len;
673
674 t->tcm_handle = f->handle;
675
676 nest = nla_nest_start(skb, TCA_OPTIONS);
677 if (!nest)
678 goto nla_put_failure;
679
680 if (f->res.classid &&
681 nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
682 goto nla_put_failure;
683
684 key = &f->key;
685 mask = &head->mask.key;
686
687 if (mask->indev_ifindex) {
688 struct net_device *dev;
689
690 dev = __dev_get_by_index(net, key->indev_ifindex);
691 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
692 goto nla_put_failure;
693 }
694
10cbc684
AV
695 fl_hw_update_stats(tp, f);
696
77b9900e
JP
697 if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
698 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
699 sizeof(key->eth.dst)) ||
700 fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
701 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
702 sizeof(key->eth.src)) ||
703 fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
704 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
705 sizeof(key->basic.n_proto)))
706 goto nla_put_failure;
707 if ((key->basic.n_proto == htons(ETH_P_IP) ||
708 key->basic.n_proto == htons(ETH_P_IPV6)) &&
709 fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
710 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
711 sizeof(key->basic.ip_proto)))
712 goto nla_put_failure;
713
c3f83241 714 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
77b9900e
JP
715 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
716 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
717 sizeof(key->ipv4.src)) ||
718 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
719 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
720 sizeof(key->ipv4.dst))))
721 goto nla_put_failure;
c3f83241 722 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
77b9900e
JP
723 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
724 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
725 sizeof(key->ipv6.src)) ||
726 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
727 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
728 sizeof(key->ipv6.dst))))
729 goto nla_put_failure;
730
731 if (key->basic.ip_proto == IPPROTO_TCP &&
732 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
733 &mask->tp.src, TCA_FLOWER_UNSPEC,
734 sizeof(key->tp.src)) ||
735 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
736 &mask->tp.dst, TCA_FLOWER_UNSPEC,
737 sizeof(key->tp.dst))))
738 goto nla_put_failure;
739 else if (key->basic.ip_proto == IPPROTO_UDP &&
740 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
741 &mask->tp.src, TCA_FLOWER_UNSPEC,
742 sizeof(key->tp.src)) ||
743 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
744 &mask->tp.dst, TCA_FLOWER_UNSPEC,
745 sizeof(key->tp.dst))))
746 goto nla_put_failure;
747
e69985c6
AV
748 nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags);
749
77b9900e
JP
750 if (tcf_exts_dump(skb, &f->exts))
751 goto nla_put_failure;
752
753 nla_nest_end(skb, nest);
754
755 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
756 goto nla_put_failure;
757
758 return skb->len;
759
760nla_put_failure:
761 nla_nest_cancel(skb, nest);
762 return -1;
763}
764
765static struct tcf_proto_ops cls_fl_ops __read_mostly = {
766 .kind = "flower",
767 .classify = fl_classify,
768 .init = fl_init,
769 .destroy = fl_destroy,
770 .get = fl_get,
771 .change = fl_change,
772 .delete = fl_delete,
773 .walk = fl_walk,
774 .dump = fl_dump,
775 .owner = THIS_MODULE,
776};
777
778static int __init cls_fl_init(void)
779{
780 return register_tcf_proto_ops(&cls_fl_ops);
781}
782
783static void __exit cls_fl_exit(void)
784{
785 unregister_tcf_proto_ops(&cls_fl_ops);
786}
787
788module_init(cls_fl_init);
789module_exit(cls_fl_exit);
790
791MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
792MODULE_DESCRIPTION("Flower classifier");
793MODULE_LICENSE("GPL v2");
This page took 0.098769 seconds and 5 git commands to generate.