blob: 9dddf77a69ccbcb003cfa66bcc0de337f78f3dae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __NET_ACT_API_H
2#define __NET_ACT_API_H
3
4/*
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -04005 * Public action API for classifiers/qdiscs
6*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
8#include <net/sch_generic.h>
9#include <net/pkt_sched.h>
WANG Congddf97cc2016-02-22 15:57:53 -080010#include <net/net_namespace.h>
11#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
WANG Conga85a9702016-07-25 16:09:41 -070013
14struct tcf_hashinfo {
15 struct hlist_head *htab;
16 unsigned int hmask;
17 spinlock_t lock;
18 u32 index;
19};
20
21struct tc_action_ops;
22
23struct tc_action {
24 const struct tc_action_ops *ops;
WANG Congec0595c2016-07-25 16:09:42 -070025 __u32 type; /* for backward compat(TCA_OLD_COMPAT) */
26 __u32 order;
27 struct list_head list;
28 struct tcf_hashinfo *hinfo;
WANG Conga85a9702016-07-25 16:09:41 -070029
WANG Congec0595c2016-07-25 16:09:42 -070030 struct hlist_node tcfa_head;
31 u32 tcfa_index;
32 int tcfa_refcnt;
33 int tcfa_bindcnt;
34 u32 tcfa_capab;
35 int tcfa_action;
36 struct tcf_t tcfa_tm;
37 struct gnet_stats_basic_packed tcfa_bstats;
38 struct gnet_stats_queue tcfa_qstats;
39 struct gnet_stats_rate_est64 tcfa_rate_est;
40 spinlock_t tcfa_lock;
41 struct rcu_head tcfa_rcu;
Eric Dumazet519c8182015-07-06 05:18:04 -070042 struct gnet_stats_basic_cpu __percpu *cpu_bstats;
43 struct gnet_stats_queue __percpu *cpu_qstats;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044};
WANG Congec0595c2016-07-25 16:09:42 -070045#define tcf_head common.tcfa_head
46#define tcf_index common.tcfa_index
47#define tcf_refcnt common.tcfa_refcnt
48#define tcf_bindcnt common.tcfa_bindcnt
49#define tcf_capab common.tcfa_capab
50#define tcf_action common.tcfa_action
51#define tcf_tm common.tcfa_tm
52#define tcf_bstats common.tcfa_bstats
53#define tcf_qstats common.tcfa_qstats
54#define tcf_rate_est common.tcfa_rate_est
55#define tcf_lock common.tcfa_lock
56#define tcf_rcu common.tcfa_rcu
David S. Millere9ce1cd2006-08-21 23:54:55 -070057
David S. Millere9ce1cd2006-08-21 23:54:55 -070058static inline unsigned int tcf_hash(u32 index, unsigned int hmask)
59{
60 return index & hmask;
61}
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
WANG Cong369ba562013-12-15 20:15:08 -080063static inline int tcf_hashinfo_init(struct tcf_hashinfo *hf, unsigned int mask)
64{
WANG Cong89819dc2013-12-15 20:15:09 -080065 int i;
66
67 spin_lock_init(&hf->lock);
WANG Congddafd342014-01-09 16:13:59 -080068 hf->index = 0;
WANG Cong369ba562013-12-15 20:15:08 -080069 hf->hmask = mask;
WANG Cong89819dc2013-12-15 20:15:09 -080070 hf->htab = kzalloc((mask + 1) * sizeof(struct hlist_head),
WANG Cong369ba562013-12-15 20:15:08 -080071 GFP_KERNEL);
72 if (!hf->htab)
73 return -ENOMEM;
WANG Cong89819dc2013-12-15 20:15:09 -080074 for (i = 0; i < mask + 1; i++)
75 INIT_HLIST_HEAD(&hf->htab[i]);
WANG Cong369ba562013-12-15 20:15:08 -080076 return 0;
77}
78
Eric Dumazet56e5d1c2015-07-06 05:18:08 -070079/* Update lastuse only if needed, to avoid dirtying a cache line.
80 * We use a temp variable to avoid fetching jiffies twice.
81 */
82static inline void tcf_lastuse_update(struct tcf_t *tm)
83{
84 unsigned long now = jiffies;
85
86 if (tm->lastuse != now)
87 tm->lastuse = now;
Jamal Hadi Salim53eb4402016-06-06 06:32:54 -040088 if (unlikely(!tm->firstuse))
89 tm->firstuse = now;
Eric Dumazet56e5d1c2015-07-06 05:18:08 -070090}
91
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -040092static inline void tcf_tm_dump(struct tcf_t *dtm, const struct tcf_t *stm)
93{
94 dtm->install = jiffies_to_clock_t(jiffies - stm->install);
95 dtm->lastuse = jiffies_to_clock_t(jiffies - stm->lastuse);
96 dtm->firstuse = jiffies_to_clock_t(jiffies - stm->firstuse);
97 dtm->expires = jiffies_to_clock_t(stm->expires);
98}
99
Amir Vadai00175ae2016-03-08 12:42:31 +0200100#ifdef CONFIG_NET_CLS_ACT
101
102#define ACT_P_CREATED 1
103#define ACT_P_DELETED 1
104
David S. Millere9ce1cd2006-08-21 23:54:55 -0700105struct tc_action_ops {
WANG Cong1f747c22013-12-15 20:15:10 -0800106 struct list_head head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 char kind[IFNAMSIZ];
108 __u32 type; /* TBD to match kind */
WANG Conga85a9702016-07-25 16:09:41 -0700109 size_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 struct module *owner;
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400111 int (*act)(struct sk_buff *, const struct tc_action *,
112 struct tcf_result *);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700113 int (*dump)(struct sk_buff *, struct tc_action *, int, int);
WANG Conga5b5c952014-02-11 17:07:32 -0800114 void (*cleanup)(struct tc_action *, int bind);
WANG Conga85a9702016-07-25 16:09:41 -0700115 int (*lookup)(struct net *, struct tc_action **, u32);
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000116 int (*init)(struct net *net, struct nlattr *nla,
WANG Conga85a9702016-07-25 16:09:41 -0700117 struct nlattr *est, struct tc_action **act, int ovr,
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000118 int bind);
WANG Congddf97cc2016-02-22 15:57:53 -0800119 int (*walk)(struct net *, struct sk_buff *,
WANG Conga85a9702016-07-25 16:09:41 -0700120 struct netlink_callback *, int, const struct tc_action_ops *);
Amir Vadai38040702016-05-13 12:55:35 +0000121 void (*stats_update)(struct tc_action *, u64, u32, u64);
Hadar Hen Zion255cb302016-12-01 14:06:36 +0200122 int (*get_dev)(const struct tc_action *a, struct net *net,
123 struct net_device **mirred_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
WANG Congddf97cc2016-02-22 15:57:53 -0800126struct tc_action_net {
127 struct tcf_hashinfo *hinfo;
128 const struct tc_action_ops *ops;
129};
130
131static inline
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400132int tc_action_net_init(struct tc_action_net *tn,
133 const struct tc_action_ops *ops, unsigned int mask)
WANG Congddf97cc2016-02-22 15:57:53 -0800134{
135 int err = 0;
136
137 tn->hinfo = kmalloc(sizeof(*tn->hinfo), GFP_KERNEL);
138 if (!tn->hinfo)
139 return -ENOMEM;
140 tn->ops = ops;
141 err = tcf_hashinfo_init(tn->hinfo, mask);
142 if (err)
143 kfree(tn->hinfo);
144 return err;
145}
146
147void tcf_hashinfo_destroy(const struct tc_action_ops *ops,
148 struct tcf_hashinfo *hinfo);
149
150static inline void tc_action_net_exit(struct tc_action_net *tn)
151{
152 tcf_hashinfo_destroy(tn->ops, tn->hinfo);
WANG Cong18fcf492016-04-04 10:32:48 -0700153 kfree(tn->hinfo);
WANG Congddf97cc2016-02-22 15:57:53 -0800154}
155
156int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
157 struct netlink_callback *cb, int type,
WANG Conga85a9702016-07-25 16:09:41 -0700158 const struct tc_action_ops *ops);
159int tcf_hash_search(struct tc_action_net *tn, struct tc_action **a, u32 index);
WANG Congddf97cc2016-02-22 15:57:53 -0800160u32 tcf_hash_new_index(struct tc_action_net *tn);
WANG Conga85a9702016-07-25 16:09:41 -0700161bool tcf_hash_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
WANG Congb2313072016-06-13 13:46:28 -0700162 int bind);
WANG Congddf97cc2016-02-22 15:57:53 -0800163int tcf_hash_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
WANG Conga85a9702016-07-25 16:09:41 -0700164 struct tc_action **a, const struct tc_action_ops *ops, int bind,
165 bool cpustats);
WANG Cong86062032014-02-11 17:07:31 -0800166void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est);
WANG Congddf97cc2016-02-22 15:57:53 -0800167void tcf_hash_insert(struct tc_action_net *tn, struct tc_action *a);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700168
Daniel Borkmann28e6b672015-07-29 23:35:25 +0200169int __tcf_hash_release(struct tc_action *a, bool bind, bool strict);
170
171static inline int tcf_hash_release(struct tc_action *a, bool bind)
172{
173 return __tcf_hash_release(a, bind, false);
174}
175
WANG Congddf97cc2016-02-22 15:57:53 -0800176int tcf_register_action(struct tc_action_ops *a, struct pernet_operations *ops);
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400177int tcf_unregister_action(struct tc_action_ops *a,
178 struct pernet_operations *ops);
WANG Cong55334a52014-02-11 17:07:34 -0800179int tcf_action_destroy(struct list_head *actions, int bind);
WANG Cong22dc13c2016-08-13 22:35:00 -0700180int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
181 int nr_actions, struct tcf_result *res);
WANG Cong33be6272013-12-15 20:15:05 -0800182int tcf_action_init(struct net *net, struct nlattr *nla,
Joe Perches5c152572013-07-30 22:47:13 -0700183 struct nlattr *est, char *n, int ovr,
WANG Cong33be6272013-12-15 20:15:05 -0800184 int bind, struct list_head *);
Joe Perches5c152572013-07-30 22:47:13 -0700185struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
186 struct nlattr *est, char *n, int ovr,
187 int bind);
WANG Cong33be6272013-12-15 20:15:05 -0800188int tcf_action_dump(struct sk_buff *skb, struct list_head *, int, int);
Joe Perches5c152572013-07-30 22:47:13 -0700189int tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int, int);
190int tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int, int);
191int tcf_action_copy_stats(struct sk_buff *, struct tc_action *, int);
Amir Vadai00175ae2016-03-08 12:42:31 +0200192
WANG Cong2734437e2016-08-13 22:34:59 -0700193#endif /* CONFIG_NET_CLS_ACT */
Ido Schimmel86cb13e2016-07-25 13:12:33 +0300194
Amir Vadai38040702016-05-13 12:55:35 +0000195static inline void tcf_action_stats_update(struct tc_action *a, u64 bytes,
196 u64 packets, u64 lastuse)
197{
WANG Cong2734437e2016-08-13 22:34:59 -0700198#ifdef CONFIG_NET_CLS_ACT
Amir Vadai38040702016-05-13 12:55:35 +0000199 if (!a->ops->stats_update)
200 return;
201
202 a->ops->stats_update(a, bytes, packets, lastuse);
WANG Cong2734437e2016-08-13 22:34:59 -0700203#endif
Amir Vadai38040702016-05-13 12:55:35 +0000204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206#endif