Skip to content

Commit 1045ba7

Browse files
jhsmtdavem330
authored andcommitted
net sched actions: Add support for user cookies
Introduce optional 128-bit action cookie. Like all other cookie schemes in the networking world (eg in protocols like http or existing kernel fib protocol field, etc) the idea is to save user state that when retrieved serves as a correlator. The kernel _should not_ intepret it. The user can store whatever they wish in the 128 bits. Sample exercise(showing variable length use of cookie) .. create an accept action with cookie a1b2c3d4 sudo $TC actions add action ok index 1 cookie a1b2c3d4 .. dump all gact actions.. sudo $TC -s actions ls action gact action order 0: gact action pass random type none pass val 0 index 1 ref 1 bind 0 installed 5 sec used 5 sec Action statistics: Sent 0 bytes 0 pkt (dropped 0, overlimits 0 requeues 0) backlog 0b 0p requeues 0 cookie a1b2c3d4 .. bind the accept action to a filter.. sudo $TC filter add dev lo parent ffff: protocol ip prio 1 \ u32 match ip dst 127.0.0.1/32 flowid 1:1 action gact index 1 ... send some traffic.. $ ping 127.0.0.1 -c 3 PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.020 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.027 ms 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.038 ms Signed-off-by: David S. Miller <[email protected]>
1 parent 264e677 commit 1045ba7

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

include/net/act_api.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ struct tc_action {
4141
struct rcu_head tcfa_rcu;
4242
struct gnet_stats_basic_cpu __percpu *cpu_bstats;
4343
struct gnet_stats_queue __percpu *cpu_qstats;
44+
struct tc_cookie *act_cookie;
4445
};
4546
#define tcf_head common.tcfa_head
4647
#define tcf_index common.tcfa_index

include/net/pkt_cls.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,4 +515,12 @@ struct tc_cls_bpf_offload {
515515
u32 gen_flags;
516516
};
517517

518+
519+
/* This structure holds cookie structure that is passed from user
520+
* to the kernel for actions and classifiers
521+
*/
522+
struct tc_cookie {
523+
u8 *data;
524+
u32 len;
525+
};
518526
#endif

include/uapi/linux/pkt_cls.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <linux/types.h>
55
#include <linux/pkt_sched.h>
66

7+
#define TC_COOKIE_MAX_SIZE 16
8+
79
/* Action attributes */
810
enum {
911
TCA_ACT_UNSPEC,
@@ -12,6 +14,7 @@ enum {
1214
TCA_ACT_INDEX,
1315
TCA_ACT_STATS,
1416
TCA_ACT_PAD,
17+
TCA_ACT_COOKIE,
1518
__TCA_ACT_MAX
1619
};
1720

net/sched/act_api.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <net/net_namespace.h>
2525
#include <net/sock.h>
2626
#include <net/sch_generic.h>
27+
#include <net/pkt_cls.h>
2728
#include <net/act_api.h>
2829
#include <net/netlink.h>
2930

@@ -33,6 +34,12 @@ static void free_tcf(struct rcu_head *head)
3334

3435
free_percpu(p->cpu_bstats);
3536
free_percpu(p->cpu_qstats);
37+
38+
if (p->act_cookie) {
39+
kfree(p->act_cookie->data);
40+
kfree(p->act_cookie);
41+
}
42+
3643
kfree(p);
3744
}
3845

@@ -475,6 +482,12 @@ tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
475482
goto nla_put_failure;
476483
if (tcf_action_copy_stats(skb, a, 0))
477484
goto nla_put_failure;
485+
if (a->act_cookie) {
486+
if (nla_put(skb, TCA_ACT_COOKIE, a->act_cookie->len,
487+
a->act_cookie->data))
488+
goto nla_put_failure;
489+
}
490+
478491
nest = nla_nest_start(skb, TCA_OPTIONS);
479492
if (nest == NULL)
480493
goto nla_put_failure;
@@ -516,6 +529,22 @@ int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
516529
return err;
517530
}
518531

532+
int nla_memdup_cookie(struct tc_action *a, struct nlattr **tb)
533+
{
534+
a->act_cookie = kzalloc(sizeof(*a->act_cookie), GFP_KERNEL);
535+
if (!a->act_cookie)
536+
return -ENOMEM;
537+
538+
a->act_cookie->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
539+
if (!a->act_cookie->data) {
540+
kfree(a->act_cookie);
541+
return -ENOMEM;
542+
}
543+
a->act_cookie->len = nla_len(tb[TCA_ACT_COOKIE]);
544+
545+
return 0;
546+
}
547+
519548
struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
520549
struct nlattr *est, char *name, int ovr,
521550
int bind)
@@ -575,6 +604,22 @@ struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
575604
if (err < 0)
576605
goto err_mod;
577606

607+
if (tb[TCA_ACT_COOKIE]) {
608+
int cklen = nla_len(tb[TCA_ACT_COOKIE]);
609+
610+
if (cklen > TC_COOKIE_MAX_SIZE) {
611+
err = -EINVAL;
612+
tcf_hash_release(a, bind);
613+
goto err_mod;
614+
}
615+
616+
err = nla_memdup_cookie(a, tb);
617+
if (err < 0) {
618+
tcf_hash_release(a, bind);
619+
goto err_mod;
620+
}
621+
}
622+
578623
/* module count goes up only when brand new policy is created
579624
* if it exists and is only bound to in a_o->init() then
580625
* ACT_P_CREATED is not returned (a zero is).

0 commit comments

Comments
 (0)