Blame view
net/sched/act_sample.c
8.1 KB
d2912cb15 treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-only |
5c5670fae net/sched: Introd... |
2 3 4 |
/* * net/sched/act_sample.c - Packet sampling tc action * Copyright (c) 2017 Yotam Gigi <yotamg@mellanox.com> |
5c5670fae net/sched: Introd... |
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
*/ #include <linux/types.h> #include <linux/kernel.h> #include <linux/string.h> #include <linux/errno.h> #include <linux/skbuff.h> #include <linux/rtnetlink.h> #include <linux/module.h> #include <linux/init.h> #include <linux/gfp.h> #include <net/net_namespace.h> #include <net/netlink.h> #include <net/pkt_sched.h> #include <linux/tc_act/tc_sample.h> #include <net/tc_act/tc_sample.h> #include <net/psample.h> |
e8c87c643 net/sched: act_sa... |
22 |
#include <net/pkt_cls.h> |
5c5670fae net/sched: Introd... |
23 24 |
#include <linux/if_arp.h> |
5c5670fae net/sched: Introd... |
25 26 27 28 29 30 31 32 33 34 35 36 |
static unsigned int sample_net_id; static struct tc_action_ops act_sample_ops; static const struct nla_policy sample_policy[TCA_SAMPLE_MAX + 1] = { [TCA_SAMPLE_PARMS] = { .len = sizeof(struct tc_sample) }, [TCA_SAMPLE_RATE] = { .type = NLA_U32 }, [TCA_SAMPLE_TRUNC_SIZE] = { .type = NLA_U32 }, [TCA_SAMPLE_PSAMPLE_GROUP] = { .type = NLA_U32 }, }; static int tcf_sample_init(struct net *net, struct nlattr *nla, struct nlattr *est, struct tc_action **a, int ovr, |
85d0966fa net/sched: prepar... |
37 |
int bind, bool rtnl_held, struct tcf_proto *tp, |
abbb0d336 net: sched: exten... |
38 |
u32 flags, struct netlink_ext_ack *extack) |
5c5670fae net/sched: Introd... |
39 40 41 42 |
{ struct tc_action_net *tn = net_generic(net, sample_net_id); struct nlattr *tb[TCA_SAMPLE_MAX + 1]; struct psample_group *psample_group; |
7be8ef2cd net: sched: use t... |
43 |
u32 psample_group_num, rate, index; |
e8c87c643 net/sched: act_sa... |
44 |
struct tcf_chain *goto_ch = NULL; |
5c5670fae net/sched: Introd... |
45 46 47 |
struct tc_sample *parm; struct tcf_sample *s; bool exists = false; |
0190c1d45 net: sched: atomi... |
48 |
int ret, err; |
5c5670fae net/sched: Introd... |
49 50 51 |
if (!nla) return -EINVAL; |
8cb081746 netlink: make val... |
52 53 |
ret = nla_parse_nested_deprecated(tb, TCA_SAMPLE_MAX, nla, sample_policy, NULL); |
5c5670fae net/sched: Introd... |
54 55 56 57 58 59 60 |
if (ret < 0) return ret; if (!tb[TCA_SAMPLE_PARMS] || !tb[TCA_SAMPLE_RATE] || !tb[TCA_SAMPLE_PSAMPLE_GROUP]) return -EINVAL; parm = nla_data(tb[TCA_SAMPLE_PARMS]); |
7be8ef2cd net: sched: use t... |
61 62 |
index = parm->index; err = tcf_idr_check_alloc(tn, &index, a, bind); |
0190c1d45 net: sched: atomi... |
63 64 65 |
if (err < 0) return err; exists = err; |
5c5670fae net/sched: Introd... |
66 67 68 69 |
if (exists && bind) return 0; if (!exists) { |
7be8ef2cd net: sched: use t... |
70 |
ret = tcf_idr_create(tn, index, est, a, |
e38226786 net: sched: updat... |
71 |
&act_sample_ops, bind, true, 0); |
0190c1d45 net: sched: atomi... |
72 |
if (ret) { |
7be8ef2cd net: sched: use t... |
73 |
tcf_idr_cleanup(tn, index); |
5c5670fae net/sched: Introd... |
74 |
return ret; |
0190c1d45 net: sched: atomi... |
75 |
} |
5c5670fae net/sched: Introd... |
76 |
ret = ACT_P_CREATED; |
4e8ddd7f1 net: sched: don't... |
77 |
} else if (!ovr) { |
65a206c01 net/sched: Change... |
78 |
tcf_idr_release(*a, bind); |
4e8ddd7f1 net: sched: don't... |
79 |
return -EEXIST; |
5c5670fae net/sched: Introd... |
80 |
} |
e8c87c643 net/sched: act_sa... |
81 82 83 |
err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); if (err < 0) goto release_idr; |
5c5670fae net/sched: Introd... |
84 |
|
fae270817 net/sched: act_sa... |
85 86 87 88 89 90 |
rate = nla_get_u32(tb[TCA_SAMPLE_RATE]); if (!rate) { NL_SET_ERR_MSG(extack, "invalid sample rate"); err = -EINVAL; goto put_chain; } |
653cd284a net: sched: alway... |
91 92 |
psample_group_num = nla_get_u32(tb[TCA_SAMPLE_PSAMPLE_GROUP]); psample_group = psample_group_get(net, psample_group_num); |
cadb9c9fd net/sched: act_sa... |
93 |
if (!psample_group) { |
e8c87c643 net/sched: act_sa... |
94 95 |
err = -ENOMEM; goto put_chain; |
cadb9c9fd net/sched: act_sa... |
96 |
} |
653cd284a net: sched: alway... |
97 98 99 100 |
s = to_sample(*a); spin_lock_bh(&s->tcf_lock); |
e8c87c643 net/sched: act_sa... |
101 |
goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); |
fae270817 net/sched: act_sa... |
102 |
s->rate = rate; |
653cd284a net: sched: alway... |
103 |
s->psample_group_num = psample_group_num; |
445d37493 net/sched: Replac... |
104 105 |
psample_group = rcu_replace_pointer(s->psample_group, psample_group, lockdep_is_held(&s->tcf_lock)); |
5c5670fae net/sched: Introd... |
106 107 108 109 110 |
if (tb[TCA_SAMPLE_TRUNC_SIZE]) { s->truncate = true; s->trunc_size = nla_get_u32(tb[TCA_SAMPLE_TRUNC_SIZE]); } |
653cd284a net: sched: alway... |
111 |
spin_unlock_bh(&s->tcf_lock); |
dbf47a2a0 net: sched: act_s... |
112 113 114 |
if (psample_group) psample_group_put(psample_group); |
e8c87c643 net/sched: act_sa... |
115 116 |
if (goto_ch) tcf_chain_put_by_act(goto_ch); |
5c5670fae net/sched: Introd... |
117 |
|
5c5670fae net/sched: Introd... |
118 |
return ret; |
e8c87c643 net/sched: act_sa... |
119 120 121 122 123 124 |
put_chain: if (goto_ch) tcf_chain_put_by_act(goto_ch); release_idr: tcf_idr_release(*a, bind); return err; |
5c5670fae net/sched: Introd... |
125 |
} |
9a63b255d net_sched: remove... |
126 |
static void tcf_sample_cleanup(struct tc_action *a) |
5c5670fae net/sched: Introd... |
127 |
{ |
90a6ec853 act_sample: get r... |
128 |
struct tcf_sample *s = to_sample(a); |
5c5670fae net/sched: Introd... |
129 |
struct psample_group *psample_group; |
d77284956 net: sched: act_s... |
130 131 |
/* last reference to action, no need to lock */ psample_group = rcu_dereference_protected(s->psample_group, 1); |
5c5670fae net/sched: Introd... |
132 |
RCU_INIT_POINTER(s->psample_group, NULL); |
1f110e7ca net/sched: fix NU... |
133 134 |
if (psample_group) psample_group_put(psample_group); |
5c5670fae net/sched: Introd... |
135 |
} |
5c5670fae net/sched: Introd... |
136 137 138 139 140 141 142 |
static bool tcf_sample_dev_ok_push(struct net_device *dev) { switch (dev->type) { case ARPHRD_TUNNEL: case ARPHRD_TUNNEL6: case ARPHRD_SIT: case ARPHRD_IPGRE: |
92974a1d0 net/sched: act_sa... |
143 |
case ARPHRD_IP6GRE: |
5c5670fae net/sched: Introd... |
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
case ARPHRD_VOID: case ARPHRD_NONE: return false; default: return true; } } static int tcf_sample_act(struct sk_buff *skb, const struct tc_action *a, struct tcf_result *res) { struct tcf_sample *s = to_sample(a); struct psample_group *psample_group; int retval; int size; int iif; int oif; tcf_lastuse_update(&s->tcf_tm); bstats_cpu_update(this_cpu_ptr(s->common.cpu_bstats), skb); retval = READ_ONCE(s->tcf_action); |
7fd4b288e tc/act: remove un... |
165 |
psample_group = rcu_dereference_bh(s->psample_group); |
5c5670fae net/sched: Introd... |
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
/* randomly sample packets according to rate */ if (psample_group && (prandom_u32() % s->rate == 0)) { if (!skb_at_tc_ingress(skb)) { iif = skb->skb_iif; oif = skb->dev->ifindex; } else { iif = skb->dev->ifindex; oif = 0; } /* on ingress, the mac header gets popped, so push it back */ if (skb_at_tc_ingress(skb) && tcf_sample_dev_ok_push(skb->dev)) skb_push(skb, skb->mac_len); size = s->truncate ? s->trunc_size : skb->len; psample_sample_packet(psample_group, skb, size, iif, oif, s->rate); if (skb_at_tc_ingress(skb) && tcf_sample_dev_ok_push(skb->dev)) skb_pull(skb, skb->mac_len); } |
5c5670fae net/sched: Introd... |
188 189 190 191 192 193 194 195 196 197 |
return retval; } static int tcf_sample_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref) { unsigned char *b = skb_tail_pointer(skb); struct tcf_sample *s = to_sample(a); struct tc_sample opt = { .index = s->tcf_index, |
036bb4432 net: sched: chang... |
198 199 |
.refcnt = refcount_read(&s->tcf_refcnt) - ref, .bindcnt = atomic_read(&s->tcf_bindcnt) - bind, |
5c5670fae net/sched: Introd... |
200 201 |
}; struct tcf_t t; |
653cd284a net: sched: alway... |
202 |
spin_lock_bh(&s->tcf_lock); |
d77284956 net: sched: act_s... |
203 |
opt.action = s->tcf_action; |
5c5670fae net/sched: Introd... |
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
if (nla_put(skb, TCA_SAMPLE_PARMS, sizeof(opt), &opt)) goto nla_put_failure; tcf_tm_dump(&t, &s->tcf_tm); if (nla_put_64bit(skb, TCA_SAMPLE_TM, sizeof(t), &t, TCA_SAMPLE_PAD)) goto nla_put_failure; if (nla_put_u32(skb, TCA_SAMPLE_RATE, s->rate)) goto nla_put_failure; if (s->truncate) if (nla_put_u32(skb, TCA_SAMPLE_TRUNC_SIZE, s->trunc_size)) goto nla_put_failure; if (nla_put_u32(skb, TCA_SAMPLE_PSAMPLE_GROUP, s->psample_group_num)) goto nla_put_failure; |
653cd284a net: sched: alway... |
220 |
spin_unlock_bh(&s->tcf_lock); |
d77284956 net: sched: act_s... |
221 |
|
5c5670fae net/sched: Introd... |
222 223 224 |
return skb->len; nla_put_failure: |
653cd284a net: sched: alway... |
225 |
spin_unlock_bh(&s->tcf_lock); |
5c5670fae net/sched: Introd... |
226 227 228 229 230 231 |
nlmsg_trim(skb, b); return -1; } static int tcf_sample_walker(struct net *net, struct sk_buff *skb, struct netlink_callback *cb, int type, |
417801055 net: sched: act: ... |
232 233 |
const struct tc_action_ops *ops, struct netlink_ext_ack *extack) |
5c5670fae net/sched: Introd... |
234 235 |
{ struct tc_action_net *tn = net_generic(net, sample_net_id); |
b36201455 net: sched: act: ... |
236 |
return tcf_generic_walker(tn, skb, cb, type, ops, extack); |
5c5670fae net/sched: Introd... |
237 |
} |
f061b48c1 Revert "net: sche... |
238 |
static int tcf_sample_search(struct net *net, struct tc_action **a, u32 index) |
5c5670fae net/sched: Introd... |
239 240 |
{ struct tc_action_net *tn = net_generic(net, sample_net_id); |
65a206c01 net/sched: Change... |
241 |
return tcf_idr_search(tn, a, index); |
5c5670fae net/sched: Introd... |
242 |
} |
4a5da47d5 net: sched: take ... |
243 244 245 246 247 248 249 250 251 252 253 254 255 |
static void tcf_psample_group_put(void *priv) { struct psample_group *group = priv; psample_group_put(group); } static struct psample_group * tcf_sample_get_group(const struct tc_action *a, tc_action_priv_destructor *destructor) { struct tcf_sample *s = to_sample(a); struct psample_group *group; |
4a5da47d5 net: sched: take ... |
256 257 258 259 260 261 |
group = rcu_dereference_protected(s->psample_group, lockdep_is_held(&s->tcf_lock)); if (group) { psample_group_take(group); *destructor = tcf_psample_group_put; } |
4a5da47d5 net: sched: take ... |
262 263 264 |
return group; } |
5c5670fae net/sched: Introd... |
265 266 |
static struct tc_action_ops act_sample_ops = { .kind = "sample", |
eddd2cf19 net: Change TCA_A... |
267 |
.id = TCA_ID_SAMPLE, |
5c5670fae net/sched: Introd... |
268 269 270 271 272 273 274 |
.owner = THIS_MODULE, .act = tcf_sample_act, .dump = tcf_sample_dump, .init = tcf_sample_init, .cleanup = tcf_sample_cleanup, .walk = tcf_sample_walker, .lookup = tcf_sample_search, |
4a5da47d5 net: sched: take ... |
275 |
.get_psample_group = tcf_sample_get_group, |
5c5670fae net/sched: Introd... |
276 277 278 279 280 281 |
.size = sizeof(struct tcf_sample), }; static __net_init int sample_init_net(struct net *net) { struct tc_action_net *tn = net_generic(net, sample_net_id); |
981471bd3 net_sched: fix a ... |
282 |
return tc_action_net_init(net, tn, &act_sample_ops); |
5c5670fae net/sched: Introd... |
283 |
} |
039af9c66 net_sched: switch... |
284 |
static void __net_exit sample_exit_net(struct list_head *net_list) |
5c5670fae net/sched: Introd... |
285 |
{ |
039af9c66 net_sched: switch... |
286 |
tc_action_net_exit(net_list, sample_net_id); |
5c5670fae net/sched: Introd... |
287 288 289 290 |
} static struct pernet_operations sample_net_ops = { .init = sample_init_net, |
039af9c66 net_sched: switch... |
291 |
.exit_batch = sample_exit_net, |
5c5670fae net/sched: Introd... |
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
.id = &sample_net_id, .size = sizeof(struct tc_action_net), }; static int __init sample_init_module(void) { return tcf_register_action(&act_sample_ops, &sample_net_ops); } static void __exit sample_cleanup_module(void) { tcf_unregister_action(&act_sample_ops, &sample_net_ops); } module_init(sample_init_module); module_exit(sample_cleanup_module); |
f1fd20c36 MAINTAINERS: Upda... |
308 |
MODULE_AUTHOR("Yotam Gigi <yotam.gi@gmail.com>"); |
5c5670fae net/sched: Introd... |
309 310 |
MODULE_DESCRIPTION("Packet sampling action"); MODULE_LICENSE("GPL v2"); |