cregit-Linux how code gets into the kernel

Release 4.14 net/sched/act_sample.c

Directory: net/sched
/*
 * net/sched/act_sample.c - Packet sampling tc action
 * Copyright (c) 2017 Yotam Gigi <yotamg@mellanox.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#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>

#include <linux/if_arp.h>


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, int bind) { struct tc_action_net *tn = net_generic(net, sample_net_id); struct nlattr *tb[TCA_SAMPLE_MAX + 1]; struct psample_group *psample_group; struct tc_sample *parm; struct tcf_sample *s; bool exists = false; int ret; if (!nla) return -EINVAL; ret = nla_parse_nested(tb, TCA_SAMPLE_MAX, nla, sample_policy, NULL); 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]); exists = tcf_idr_check(tn, parm->index, a, bind); if (exists && bind) return 0; if (!exists) { ret = tcf_idr_create(tn, parm->index, est, a, &act_sample_ops, bind, false); if (ret) return ret; ret = ACT_P_CREATED; } else { tcf_idr_release(*a, bind); if (!ovr) return -EEXIST; } s = to_sample(*a); s->tcf_action = parm->action; s->rate = nla_get_u32(tb[TCA_SAMPLE_RATE]); s->psample_group_num = nla_get_u32(tb[TCA_SAMPLE_PSAMPLE_GROUP]); psample_group = psample_group_get(net, s->psample_group_num); if (!psample_group) { if (ret == ACT_P_CREATED) tcf_idr_release(*a, bind); return -ENOMEM; } RCU_INIT_POINTER(s->psample_group, psample_group); if (tb[TCA_SAMPLE_TRUNC_SIZE]) { s->truncate = true; s->trunc_size = nla_get_u32(tb[TCA_SAMPLE_TRUNC_SIZE]); } if (ret == ACT_P_CREATED) tcf_idr_insert(tn, *a); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Yotam Gigi35198.04%250.00%
Chris Mi51.40%125.00%
Johannes Berg20.56%125.00%
Total358100.00%4100.00%


static void tcf_sample_cleanup_rcu(struct rcu_head *rcu) { struct tcf_sample *s = container_of(rcu, struct tcf_sample, rcu); struct psample_group *psample_group; psample_group = rcu_dereference_protected(s->psample_group, 1); RCU_INIT_POINTER(s->psample_group, NULL); psample_group_put(psample_group); }

Contributors

PersonTokensPropCommitsCommitProp
Yotam Gigi56100.00%1100.00%
Total56100.00%1100.00%


static void tcf_sample_cleanup(struct tc_action *a, int bind) { struct tcf_sample *s = to_sample(a); call_rcu(&s->rcu, tcf_sample_cleanup_rcu); }

Contributors

PersonTokensPropCommitsCommitProp
Yotam Gigi34100.00%1100.00%
Total34100.00%1100.00%


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: case ARPHRD_VOID: case ARPHRD_NONE: return false; default: return true; } }

Contributors

PersonTokensPropCommitsCommitProp
Yotam Gigi44100.00%1100.00%
Total44100.00%1100.00%


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); rcu_read_lock(); psample_group = rcu_dereference(s->psample_group); /* 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); } rcu_read_unlock(); return retval; }

Contributors

PersonTokensPropCommitsCommitProp
Yotam Gigi233100.00%1100.00%
Total233100.00%1100.00%


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, .action = s->tcf_action, .refcnt = s->tcf_refcnt - ref, .bindcnt = s->tcf_bindcnt - bind, }; struct tcf_t t; 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; return skb->len; nla_put_failure: nlmsg_trim(skb, b); return -1; }

Contributors

PersonTokensPropCommitsCommitProp
Yotam Gigi209100.00%1100.00%
Total209100.00%1100.00%


static int tcf_sample_walker(struct net *net, struct sk_buff *skb, struct netlink_callback *cb, int type, const struct tc_action_ops *ops) { struct tc_action_net *tn = net_generic(net, sample_net_id); return tcf_generic_walker(tn, skb, cb, type, ops); }

Contributors

PersonTokensPropCommitsCommitProp
Yotam Gigi56100.00%1100.00%
Total56100.00%1100.00%


static int tcf_sample_search(struct net *net, struct tc_action **a, u32 index) { struct tc_action_net *tn = net_generic(net, sample_net_id); return tcf_idr_search(tn, a, index); }

Contributors

PersonTokensPropCommitsCommitProp
Yotam Gigi4197.62%150.00%
Chris Mi12.38%150.00%
Total42100.00%2100.00%

static struct tc_action_ops act_sample_ops = { .kind = "sample", .type = TCA_ACT_SAMPLE, .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, .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); return tc_action_net_init(tn, &act_sample_ops); }

Contributors

PersonTokensPropCommitsCommitProp
Yotam Gigi33100.00%1100.00%
Total33100.00%1100.00%


static void __net_exit sample_exit_net(struct net *net) { struct tc_action_net *tn = net_generic(net, sample_net_id); tc_action_net_exit(tn); }

Contributors

PersonTokensPropCommitsCommitProp
Yotam Gigi29100.00%1100.00%
Total29100.00%1100.00%

static struct pernet_operations sample_net_ops = { .init = sample_init_net, .exit = sample_exit_net, .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); }

Contributors

PersonTokensPropCommitsCommitProp
Yotam Gigi19100.00%1100.00%
Total19100.00%1100.00%


static void __exit sample_cleanup_module(void) { rcu_barrier(); tcf_unregister_action(&act_sample_ops, &sample_net_ops); }

Contributors

PersonTokensPropCommitsCommitProp
Yotam Gigi1885.71%150.00%
Américo Wang314.29%150.00%
Total21100.00%2100.00%

module_init(sample_init_module); module_exit(sample_cleanup_module); MODULE_AUTHOR("Yotam Gigi <yotam.gi@gmail.com>"); MODULE_DESCRIPTION("Packet sampling action"); MODULE_LICENSE("GPL v2");

Overall Contributors

PersonTokensPropCommitsCommitProp
Yotam Gigi136199.20%350.00%
Chris Mi60.44%116.67%
Américo Wang30.22%116.67%
Johannes Berg20.15%116.67%
Total1372100.00%6100.00%
Directory: net/sched
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
Created with cregit.