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