1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25c5670faSYotam Gigi /*
35c5670faSYotam Gigi * net/sched/act_sample.c - Packet sampling tc action
45c5670faSYotam Gigi * Copyright (c) 2017 Yotam Gigi <yotamg@mellanox.com>
55c5670faSYotam Gigi */
65c5670faSYotam Gigi
75c5670faSYotam Gigi #include <linux/types.h>
85c5670faSYotam Gigi #include <linux/kernel.h>
95c5670faSYotam Gigi #include <linux/string.h>
105c5670faSYotam Gigi #include <linux/errno.h>
115c5670faSYotam Gigi #include <linux/skbuff.h>
125c5670faSYotam Gigi #include <linux/rtnetlink.h>
135c5670faSYotam Gigi #include <linux/module.h>
145c5670faSYotam Gigi #include <linux/init.h>
155c5670faSYotam Gigi #include <linux/gfp.h>
165c5670faSYotam Gigi #include <net/net_namespace.h>
175c5670faSYotam Gigi #include <net/netlink.h>
185c5670faSYotam Gigi #include <net/pkt_sched.h>
195c5670faSYotam Gigi #include <linux/tc_act/tc_sample.h>
205c5670faSYotam Gigi #include <net/tc_act/tc_sample.h>
215c5670faSYotam Gigi #include <net/psample.h>
22e8c87c64SDavide Caratti #include <net/pkt_cls.h>
23871cf386SPedro Tammela #include <net/tc_wrapper.h>
245c5670faSYotam Gigi
255c5670faSYotam Gigi #include <linux/if_arp.h>
265c5670faSYotam Gigi
275c5670faSYotam Gigi static struct tc_action_ops act_sample_ops;
285c5670faSYotam Gigi
295c5670faSYotam Gigi static const struct nla_policy sample_policy[TCA_SAMPLE_MAX + 1] = {
305c5670faSYotam Gigi [TCA_SAMPLE_PARMS] = { .len = sizeof(struct tc_sample) },
315c5670faSYotam Gigi [TCA_SAMPLE_RATE] = { .type = NLA_U32 },
325c5670faSYotam Gigi [TCA_SAMPLE_TRUNC_SIZE] = { .type = NLA_U32 },
335c5670faSYotam Gigi [TCA_SAMPLE_PSAMPLE_GROUP] = { .type = NLA_U32 },
345c5670faSYotam Gigi };
355c5670faSYotam Gigi
tcf_sample_init(struct net * net,struct nlattr * nla,struct nlattr * est,struct tc_action ** a,struct tcf_proto * tp,u32 flags,struct netlink_ext_ack * extack)365c5670faSYotam Gigi static int tcf_sample_init(struct net *net, struct nlattr *nla,
37695176bfSCong Wang struct nlattr *est, struct tc_action **a,
38695176bfSCong Wang struct tcf_proto *tp,
39abbb0d33SVlad Buslov u32 flags, struct netlink_ext_ack *extack)
405c5670faSYotam Gigi {
41acd0a7abSZhengchao Shao struct tc_action_net *tn = net_generic(net, act_sample_ops.net_id);
42695176bfSCong Wang bool bind = flags & TCA_ACT_FLAGS_BIND;
435c5670faSYotam Gigi struct nlattr *tb[TCA_SAMPLE_MAX + 1];
445c5670faSYotam Gigi struct psample_group *psample_group;
457be8ef2cSDmytro Linkin u32 psample_group_num, rate, index;
46e8c87c64SDavide Caratti struct tcf_chain *goto_ch = NULL;
475c5670faSYotam Gigi struct tc_sample *parm;
485c5670faSYotam Gigi struct tcf_sample *s;
495c5670faSYotam Gigi bool exists = false;
500190c1d4SVlad Buslov int ret, err;
515c5670faSYotam Gigi
525c5670faSYotam Gigi if (!nla)
535c5670faSYotam Gigi return -EINVAL;
548cb08174SJohannes Berg ret = nla_parse_nested_deprecated(tb, TCA_SAMPLE_MAX, nla,
558cb08174SJohannes Berg sample_policy, NULL);
565c5670faSYotam Gigi if (ret < 0)
575c5670faSYotam Gigi return ret;
58*4a20056aSPedro Tammela
59*4a20056aSPedro Tammela if (!tb[TCA_SAMPLE_PARMS])
605c5670faSYotam Gigi return -EINVAL;
615c5670faSYotam Gigi
625c5670faSYotam Gigi parm = nla_data(tb[TCA_SAMPLE_PARMS]);
637be8ef2cSDmytro Linkin index = parm->index;
647be8ef2cSDmytro Linkin err = tcf_idr_check_alloc(tn, &index, a, bind);
650190c1d4SVlad Buslov if (err < 0)
660190c1d4SVlad Buslov return err;
670190c1d4SVlad Buslov exists = err;
685c5670faSYotam Gigi if (exists && bind)
695c5670faSYotam Gigi return 0;
705c5670faSYotam Gigi
715c5670faSYotam Gigi if (!exists) {
727be8ef2cSDmytro Linkin ret = tcf_idr_create(tn, index, est, a,
7340bd094dSBaowen Zheng &act_sample_ops, bind, true, flags);
740190c1d4SVlad Buslov if (ret) {
757be8ef2cSDmytro Linkin tcf_idr_cleanup(tn, index);
765c5670faSYotam Gigi return ret;
770190c1d4SVlad Buslov }
785c5670faSYotam Gigi ret = ACT_P_CREATED;
79695176bfSCong Wang } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
8065a206c0SChris Mi tcf_idr_release(*a, bind);
815c5670faSYotam Gigi return -EEXIST;
825c5670faSYotam Gigi }
83*4a20056aSPedro Tammela
84*4a20056aSPedro Tammela if (!tb[TCA_SAMPLE_RATE] || !tb[TCA_SAMPLE_PSAMPLE_GROUP]) {
85*4a20056aSPedro Tammela NL_SET_ERR_MSG(extack, "sample rate and group are required");
86*4a20056aSPedro Tammela err = -EINVAL;
87*4a20056aSPedro Tammela goto release_idr;
88*4a20056aSPedro Tammela }
89*4a20056aSPedro Tammela
90e8c87c64SDavide Caratti err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
91e8c87c64SDavide Caratti if (err < 0)
92e8c87c64SDavide Caratti goto release_idr;
935c5670faSYotam Gigi
94fae27081SDavide Caratti rate = nla_get_u32(tb[TCA_SAMPLE_RATE]);
95fae27081SDavide Caratti if (!rate) {
96fae27081SDavide Caratti NL_SET_ERR_MSG(extack, "invalid sample rate");
97fae27081SDavide Caratti err = -EINVAL;
98fae27081SDavide Caratti goto put_chain;
99fae27081SDavide Caratti }
100653cd284SVlad Buslov psample_group_num = nla_get_u32(tb[TCA_SAMPLE_PSAMPLE_GROUP]);
101653cd284SVlad Buslov psample_group = psample_group_get(net, psample_group_num);
102cadb9c9fSYotam Gigi if (!psample_group) {
103e8c87c64SDavide Caratti err = -ENOMEM;
104e8c87c64SDavide Caratti goto put_chain;
105cadb9c9fSYotam Gigi }
106653cd284SVlad Buslov
107653cd284SVlad Buslov s = to_sample(*a);
108653cd284SVlad Buslov
109653cd284SVlad Buslov spin_lock_bh(&s->tcf_lock);
110e8c87c64SDavide Caratti goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
111fae27081SDavide Caratti s->rate = rate;
112653cd284SVlad Buslov s->psample_group_num = psample_group_num;
113445d3749SPaul E. McKenney psample_group = rcu_replace_pointer(s->psample_group, psample_group,
114dbf47a2aSVlad Buslov lockdep_is_held(&s->tcf_lock));
1155c5670faSYotam Gigi
1165c5670faSYotam Gigi if (tb[TCA_SAMPLE_TRUNC_SIZE]) {
1175c5670faSYotam Gigi s->truncate = true;
1185c5670faSYotam Gigi s->trunc_size = nla_get_u32(tb[TCA_SAMPLE_TRUNC_SIZE]);
1195c5670faSYotam Gigi }
120653cd284SVlad Buslov spin_unlock_bh(&s->tcf_lock);
121dbf47a2aSVlad Buslov
122dbf47a2aSVlad Buslov if (psample_group)
123dbf47a2aSVlad Buslov psample_group_put(psample_group);
124e8c87c64SDavide Caratti if (goto_ch)
125e8c87c64SDavide Caratti tcf_chain_put_by_act(goto_ch);
1265c5670faSYotam Gigi
1275c5670faSYotam Gigi return ret;
128e8c87c64SDavide Caratti put_chain:
129e8c87c64SDavide Caratti if (goto_ch)
130e8c87c64SDavide Caratti tcf_chain_put_by_act(goto_ch);
131e8c87c64SDavide Caratti release_idr:
132e8c87c64SDavide Caratti tcf_idr_release(*a, bind);
133e8c87c64SDavide Caratti return err;
1345c5670faSYotam Gigi }
1355c5670faSYotam Gigi
tcf_sample_cleanup(struct tc_action * a)1369a63b255SCong Wang static void tcf_sample_cleanup(struct tc_action *a)
1375c5670faSYotam Gigi {
1385c5670faSYotam Gigi struct tcf_sample *s = to_sample(a);
13990a6ec85SCong Wang struct psample_group *psample_group;
1405c5670faSYotam Gigi
141d7728495SVlad Buslov /* last reference to action, no need to lock */
142d7728495SVlad Buslov psample_group = rcu_dereference_protected(s->psample_group, 1);
14390a6ec85SCong Wang RCU_INIT_POINTER(s->psample_group, NULL);
1441f110e7cSDavide Caratti if (psample_group)
14590a6ec85SCong Wang psample_group_put(psample_group);
1465c5670faSYotam Gigi }
1475c5670faSYotam Gigi
tcf_sample_dev_ok_push(struct net_device * dev)1485c5670faSYotam Gigi static bool tcf_sample_dev_ok_push(struct net_device *dev)
1495c5670faSYotam Gigi {
1505c5670faSYotam Gigi switch (dev->type) {
1515c5670faSYotam Gigi case ARPHRD_TUNNEL:
1525c5670faSYotam Gigi case ARPHRD_TUNNEL6:
1535c5670faSYotam Gigi case ARPHRD_SIT:
1545c5670faSYotam Gigi case ARPHRD_IPGRE:
15592974a1dSDavide Caratti case ARPHRD_IP6GRE:
1565c5670faSYotam Gigi case ARPHRD_VOID:
1575c5670faSYotam Gigi case ARPHRD_NONE:
1585c5670faSYotam Gigi return false;
1595c5670faSYotam Gigi default:
1605c5670faSYotam Gigi return true;
1615c5670faSYotam Gigi }
1625c5670faSYotam Gigi }
1635c5670faSYotam Gigi
tcf_sample_act(struct sk_buff * skb,const struct tc_action * a,struct tcf_result * res)164871cf386SPedro Tammela TC_INDIRECT_SCOPE int tcf_sample_act(struct sk_buff *skb,
165871cf386SPedro Tammela const struct tc_action *a,
1665c5670faSYotam Gigi struct tcf_result *res)
1675c5670faSYotam Gigi {
1685c5670faSYotam Gigi struct tcf_sample *s = to_sample(a);
1695c5670faSYotam Gigi struct psample_group *psample_group;
170a03e99d3SIdo Schimmel struct psample_metadata md = {};
1715c5670faSYotam Gigi int retval;
1725c5670faSYotam Gigi
1735c5670faSYotam Gigi tcf_lastuse_update(&s->tcf_tm);
17450dc9a85SAhmed S. Darwish bstats_update(this_cpu_ptr(s->common.cpu_bstats), skb);
1755c5670faSYotam Gigi retval = READ_ONCE(s->tcf_action);
1765c5670faSYotam Gigi
1777fd4b288SPaolo Abeni psample_group = rcu_dereference_bh(s->psample_group);
1785c5670faSYotam Gigi
1795c5670faSYotam Gigi /* randomly sample packets according to rate */
1808032bf12SJason A. Donenfeld if (psample_group && (get_random_u32_below(s->rate) == 0)) {
1815c5670faSYotam Gigi if (!skb_at_tc_ingress(skb)) {
182a03e99d3SIdo Schimmel md.in_ifindex = skb->skb_iif;
183a03e99d3SIdo Schimmel md.out_ifindex = skb->dev->ifindex;
1845c5670faSYotam Gigi } else {
185a03e99d3SIdo Schimmel md.in_ifindex = skb->dev->ifindex;
1865c5670faSYotam Gigi }
1875c5670faSYotam Gigi
1885c5670faSYotam Gigi /* on ingress, the mac header gets popped, so push it back */
1895c5670faSYotam Gigi if (skb_at_tc_ingress(skb) && tcf_sample_dev_ok_push(skb->dev))
1905c5670faSYotam Gigi skb_push(skb, skb->mac_len);
1915c5670faSYotam Gigi
192a03e99d3SIdo Schimmel md.trunc_size = s->truncate ? s->trunc_size : skb->len;
193a03e99d3SIdo Schimmel psample_sample_packet(psample_group, skb, s->rate, &md);
1945c5670faSYotam Gigi
1955c5670faSYotam Gigi if (skb_at_tc_ingress(skb) && tcf_sample_dev_ok_push(skb->dev))
1965c5670faSYotam Gigi skb_pull(skb, skb->mac_len);
1975c5670faSYotam Gigi }
1985c5670faSYotam Gigi
1995c5670faSYotam Gigi return retval;
2005c5670faSYotam Gigi }
2015c5670faSYotam Gigi
tcf_sample_stats_update(struct tc_action * a,u64 bytes,u64 packets,u64 drops,u64 lastuse,bool hw)20258c04397SIdo Schimmel static void tcf_sample_stats_update(struct tc_action *a, u64 bytes, u64 packets,
20358c04397SIdo Schimmel u64 drops, u64 lastuse, bool hw)
20458c04397SIdo Schimmel {
20558c04397SIdo Schimmel struct tcf_sample *s = to_sample(a);
20658c04397SIdo Schimmel struct tcf_t *tm = &s->tcf_tm;
20758c04397SIdo Schimmel
20858c04397SIdo Schimmel tcf_action_update_stats(a, bytes, packets, drops, hw);
20958c04397SIdo Schimmel tm->lastuse = max_t(u64, tm->lastuse, lastuse);
21058c04397SIdo Schimmel }
21158c04397SIdo Schimmel
tcf_sample_dump(struct sk_buff * skb,struct tc_action * a,int bind,int ref)2125c5670faSYotam Gigi static int tcf_sample_dump(struct sk_buff *skb, struct tc_action *a,
2135c5670faSYotam Gigi int bind, int ref)
2145c5670faSYotam Gigi {
2155c5670faSYotam Gigi unsigned char *b = skb_tail_pointer(skb);
2165c5670faSYotam Gigi struct tcf_sample *s = to_sample(a);
2175c5670faSYotam Gigi struct tc_sample opt = {
2185c5670faSYotam Gigi .index = s->tcf_index,
219036bb443SVlad Buslov .refcnt = refcount_read(&s->tcf_refcnt) - ref,
220036bb443SVlad Buslov .bindcnt = atomic_read(&s->tcf_bindcnt) - bind,
2215c5670faSYotam Gigi };
2225c5670faSYotam Gigi struct tcf_t t;
2235c5670faSYotam Gigi
224653cd284SVlad Buslov spin_lock_bh(&s->tcf_lock);
225d7728495SVlad Buslov opt.action = s->tcf_action;
2265c5670faSYotam Gigi if (nla_put(skb, TCA_SAMPLE_PARMS, sizeof(opt), &opt))
2275c5670faSYotam Gigi goto nla_put_failure;
2285c5670faSYotam Gigi
2295c5670faSYotam Gigi tcf_tm_dump(&t, &s->tcf_tm);
2305c5670faSYotam Gigi if (nla_put_64bit(skb, TCA_SAMPLE_TM, sizeof(t), &t, TCA_SAMPLE_PAD))
2315c5670faSYotam Gigi goto nla_put_failure;
2325c5670faSYotam Gigi
2335c5670faSYotam Gigi if (nla_put_u32(skb, TCA_SAMPLE_RATE, s->rate))
2345c5670faSYotam Gigi goto nla_put_failure;
2355c5670faSYotam Gigi
2365c5670faSYotam Gigi if (s->truncate)
2375c5670faSYotam Gigi if (nla_put_u32(skb, TCA_SAMPLE_TRUNC_SIZE, s->trunc_size))
2385c5670faSYotam Gigi goto nla_put_failure;
2395c5670faSYotam Gigi
2405c5670faSYotam Gigi if (nla_put_u32(skb, TCA_SAMPLE_PSAMPLE_GROUP, s->psample_group_num))
2415c5670faSYotam Gigi goto nla_put_failure;
242653cd284SVlad Buslov spin_unlock_bh(&s->tcf_lock);
243d7728495SVlad Buslov
2445c5670faSYotam Gigi return skb->len;
2455c5670faSYotam Gigi
2465c5670faSYotam Gigi nla_put_failure:
247653cd284SVlad Buslov spin_unlock_bh(&s->tcf_lock);
2485c5670faSYotam Gigi nlmsg_trim(skb, b);
2495c5670faSYotam Gigi return -1;
2505c5670faSYotam Gigi }
2515c5670faSYotam Gigi
tcf_psample_group_put(void * priv)2524a5da47dSVlad Buslov static void tcf_psample_group_put(void *priv)
2534a5da47dSVlad Buslov {
2544a5da47dSVlad Buslov struct psample_group *group = priv;
2554a5da47dSVlad Buslov
2564a5da47dSVlad Buslov psample_group_put(group);
2574a5da47dSVlad Buslov }
2584a5da47dSVlad Buslov
2594a5da47dSVlad Buslov static struct psample_group *
tcf_sample_get_group(const struct tc_action * a,tc_action_priv_destructor * destructor)2604a5da47dSVlad Buslov tcf_sample_get_group(const struct tc_action *a,
2614a5da47dSVlad Buslov tc_action_priv_destructor *destructor)
2624a5da47dSVlad Buslov {
2634a5da47dSVlad Buslov struct tcf_sample *s = to_sample(a);
2644a5da47dSVlad Buslov struct psample_group *group;
2654a5da47dSVlad Buslov
2664a5da47dSVlad Buslov group = rcu_dereference_protected(s->psample_group,
2674a5da47dSVlad Buslov lockdep_is_held(&s->tcf_lock));
2684a5da47dSVlad Buslov if (group) {
2694a5da47dSVlad Buslov psample_group_take(group);
2704a5da47dSVlad Buslov *destructor = tcf_psample_group_put;
2714a5da47dSVlad Buslov }
2724a5da47dSVlad Buslov
2734a5da47dSVlad Buslov return group;
2744a5da47dSVlad Buslov }
2754a5da47dSVlad Buslov
tcf_offload_sample_get_group(struct flow_action_entry * entry,const struct tc_action * act)276c54e1d92SBaowen Zheng static void tcf_offload_sample_get_group(struct flow_action_entry *entry,
277c54e1d92SBaowen Zheng const struct tc_action *act)
278c54e1d92SBaowen Zheng {
279c54e1d92SBaowen Zheng entry->sample.psample_group =
280c54e1d92SBaowen Zheng act->ops->get_psample_group(act, &entry->destructor);
281c54e1d92SBaowen Zheng entry->destructor_priv = entry->sample.psample_group;
282c54e1d92SBaowen Zheng }
283c54e1d92SBaowen Zheng
tcf_sample_offload_act_setup(struct tc_action * act,void * entry_data,u32 * index_inc,bool bind,struct netlink_ext_ack * extack)284c54e1d92SBaowen Zheng static int tcf_sample_offload_act_setup(struct tc_action *act, void *entry_data,
285c2ccf84eSIdo Schimmel u32 *index_inc, bool bind,
286c2ccf84eSIdo Schimmel struct netlink_ext_ack *extack)
287c54e1d92SBaowen Zheng {
288c54e1d92SBaowen Zheng if (bind) {
289c54e1d92SBaowen Zheng struct flow_action_entry *entry = entry_data;
290c54e1d92SBaowen Zheng
291c54e1d92SBaowen Zheng entry->id = FLOW_ACTION_SAMPLE;
292c54e1d92SBaowen Zheng entry->sample.trunc_size = tcf_sample_trunc_size(act);
293c54e1d92SBaowen Zheng entry->sample.truncate = tcf_sample_truncate(act);
294c54e1d92SBaowen Zheng entry->sample.rate = tcf_sample_rate(act);
295c54e1d92SBaowen Zheng tcf_offload_sample_get_group(entry, act);
296c54e1d92SBaowen Zheng *index_inc = 1;
297c54e1d92SBaowen Zheng } else {
2988cbfe939SBaowen Zheng struct flow_offload_action *fl_action = entry_data;
2998cbfe939SBaowen Zheng
3008cbfe939SBaowen Zheng fl_action->id = FLOW_ACTION_SAMPLE;
301c54e1d92SBaowen Zheng }
302c54e1d92SBaowen Zheng
303c54e1d92SBaowen Zheng return 0;
304c54e1d92SBaowen Zheng }
305c54e1d92SBaowen Zheng
3065c5670faSYotam Gigi static struct tc_action_ops act_sample_ops = {
3075c5670faSYotam Gigi .kind = "sample",
308eddd2cf1SEli Cohen .id = TCA_ID_SAMPLE,
3095c5670faSYotam Gigi .owner = THIS_MODULE,
3105c5670faSYotam Gigi .act = tcf_sample_act,
31158c04397SIdo Schimmel .stats_update = tcf_sample_stats_update,
3125c5670faSYotam Gigi .dump = tcf_sample_dump,
3135c5670faSYotam Gigi .init = tcf_sample_init,
3145c5670faSYotam Gigi .cleanup = tcf_sample_cleanup,
3154a5da47dSVlad Buslov .get_psample_group = tcf_sample_get_group,
316c54e1d92SBaowen Zheng .offload_act_setup = tcf_sample_offload_act_setup,
3175c5670faSYotam Gigi .size = sizeof(struct tcf_sample),
3185c5670faSYotam Gigi };
3195c5670faSYotam Gigi
sample_init_net(struct net * net)3205c5670faSYotam Gigi static __net_init int sample_init_net(struct net *net)
3215c5670faSYotam Gigi {
322acd0a7abSZhengchao Shao struct tc_action_net *tn = net_generic(net, act_sample_ops.net_id);
3235c5670faSYotam Gigi
324981471bdSCong Wang return tc_action_net_init(net, tn, &act_sample_ops);
3255c5670faSYotam Gigi }
3265c5670faSYotam Gigi
sample_exit_net(struct list_head * net_list)327039af9c6SCong Wang static void __net_exit sample_exit_net(struct list_head *net_list)
3285c5670faSYotam Gigi {
329acd0a7abSZhengchao Shao tc_action_net_exit(net_list, act_sample_ops.net_id);
3305c5670faSYotam Gigi }
3315c5670faSYotam Gigi
3325c5670faSYotam Gigi static struct pernet_operations sample_net_ops = {
3335c5670faSYotam Gigi .init = sample_init_net,
334039af9c6SCong Wang .exit_batch = sample_exit_net,
335acd0a7abSZhengchao Shao .id = &act_sample_ops.net_id,
3365c5670faSYotam Gigi .size = sizeof(struct tc_action_net),
3375c5670faSYotam Gigi };
3385c5670faSYotam Gigi
sample_init_module(void)3395c5670faSYotam Gigi static int __init sample_init_module(void)
3405c5670faSYotam Gigi {
3415c5670faSYotam Gigi return tcf_register_action(&act_sample_ops, &sample_net_ops);
3425c5670faSYotam Gigi }
3435c5670faSYotam Gigi
sample_cleanup_module(void)3445c5670faSYotam Gigi static void __exit sample_cleanup_module(void)
3455c5670faSYotam Gigi {
3465c5670faSYotam Gigi tcf_unregister_action(&act_sample_ops, &sample_net_ops);
3475c5670faSYotam Gigi }
3485c5670faSYotam Gigi
3495c5670faSYotam Gigi module_init(sample_init_module);
3505c5670faSYotam Gigi module_exit(sample_cleanup_module);
3515c5670faSYotam Gigi
352f1fd20c3SYotam Gigi MODULE_AUTHOR("Yotam Gigi <yotam.gi@gmail.com>");
3535c5670faSYotam Gigi MODULE_DESCRIPTION("Packet sampling action");
3545c5670faSYotam Gigi MODULE_LICENSE("GPL v2");
355