1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef __NET_ACT_API_H
31da177e4SLinus Torvalds #define __NET_ACT_API_H
41da177e4SLinus Torvalds
51da177e4SLinus Torvalds /*
60b0f43feSJamal Hadi Salim * Public action API for classifiers/qdiscs
71da177e4SLinus Torvalds */
81da177e4SLinus Torvalds
9036bb443SVlad Buslov #include <linux/refcount.h>
1013926d19SBaowen Zheng #include <net/flow_offload.h>
111da177e4SLinus Torvalds #include <net/sch_generic.h>
121da177e4SLinus Torvalds #include <net/pkt_sched.h>
13ddf97ccdSWANG Cong #include <net/net_namespace.h>
14ddf97ccdSWANG Cong #include <net/netns/generic.h>
151da177e4SLinus Torvalds
1665a206c0SChris Mi struct tcf_idrinfo {
1795278ddaSCong Wang struct mutex lock;
1865a206c0SChris Mi struct idr action_idr;
19981471bdSCong Wang struct net *net;
20a85a970aSWANG Cong };
21a85a970aSWANG Cong
22a85a970aSWANG Cong struct tc_action_ops;
23a85a970aSWANG Cong
24a85a970aSWANG Cong struct tc_action {
25a85a970aSWANG Cong const struct tc_action_ops *ops;
26a85a970aSWANG Cong __u32 type; /* for backward compat(TCA_OLD_COMPAT) */
2765a206c0SChris Mi struct tcf_idrinfo *idrinfo;
28a85a970aSWANG Cong
29ec0595ccSWANG Cong u32 tcfa_index;
30036bb443SVlad Buslov refcount_t tcfa_refcnt;
31036bb443SVlad Buslov atomic_t tcfa_bindcnt;
32ec0595ccSWANG Cong int tcfa_action;
33ec0595ccSWANG Cong struct tcf_t tcfa_tm;
3450dc9a85SAhmed S. Darwish struct gnet_stats_basic_sync tcfa_bstats;
3550dc9a85SAhmed S. Darwish struct gnet_stats_basic_sync tcfa_bstats_hw;
36ec0595ccSWANG Cong struct gnet_stats_queue tcfa_qstats;
371c0d32fdSEric Dumazet struct net_rate_estimator __rcu *tcfa_rate_est;
38ec0595ccSWANG Cong spinlock_t tcfa_lock;
3950dc9a85SAhmed S. Darwish struct gnet_stats_basic_sync __percpu *cpu_bstats;
4050dc9a85SAhmed S. Darwish struct gnet_stats_basic_sync __percpu *cpu_bstats_hw;
41519c818eSEric Dumazet struct gnet_stats_queue __percpu *cpu_qstats;
42*db4b4902SPaul Blakey struct tc_cookie __rcu *user_cookie;
43ee3bbfe8SDavide Caratti struct tcf_chain __rcu *goto_chain;
44e3822678SVlad Buslov u32 tcfa_flags;
450dfb2d82SJakub Kicinski u8 hw_stats;
4693a129ebSJiri Pirko u8 used_hw_stats;
4793a129ebSJiri Pirko bool used_hw_stats_valid;
487adc5765SBaowen Zheng u32 in_hw_count;
491da177e4SLinus Torvalds };
50ec0595ccSWANG Cong #define tcf_index common.tcfa_index
51ec0595ccSWANG Cong #define tcf_refcnt common.tcfa_refcnt
52ec0595ccSWANG Cong #define tcf_bindcnt common.tcfa_bindcnt
53ec0595ccSWANG Cong #define tcf_action common.tcfa_action
54ec0595ccSWANG Cong #define tcf_tm common.tcfa_tm
55ec0595ccSWANG Cong #define tcf_bstats common.tcfa_bstats
56ec0595ccSWANG Cong #define tcf_qstats common.tcfa_qstats
57ec0595ccSWANG Cong #define tcf_rate_est common.tcfa_rate_est
58ec0595ccSWANG Cong #define tcf_lock common.tcfa_lock
59e9ce1cd3SDavid S. Miller
600dfb2d82SJakub Kicinski #define TCA_ACT_HW_STATS_ANY (TCA_ACT_HW_STATS_IMMEDIATE | \
610dfb2d82SJakub Kicinski TCA_ACT_HW_STATS_DELAYED)
6244f86580SJiri Pirko
63695176bfSCong Wang /* Reserve 16 bits for user-space. See TCA_ACT_FLAGS_NO_PERCPU_STATS. */
64695176bfSCong Wang #define TCA_ACT_FLAGS_USER_BITS 16
65695176bfSCong Wang #define TCA_ACT_FLAGS_USER_MASK 0xffff
66695176bfSCong Wang #define TCA_ACT_FLAGS_POLICE (1U << TCA_ACT_FLAGS_USER_BITS)
67695176bfSCong Wang #define TCA_ACT_FLAGS_BIND (1U << (TCA_ACT_FLAGS_USER_BITS + 1))
68695176bfSCong Wang #define TCA_ACT_FLAGS_REPLACE (1U << (TCA_ACT_FLAGS_USER_BITS + 2))
69695176bfSCong Wang #define TCA_ACT_FLAGS_NO_RTNL (1U << (TCA_ACT_FLAGS_USER_BITS + 3))
704a6a676fSAmritha Nambiar #define TCA_ACT_FLAGS_AT_INGRESS (1U << (TCA_ACT_FLAGS_USER_BITS + 4))
71695176bfSCong Wang
7256e5d1caSEric Dumazet /* Update lastuse only if needed, to avoid dirtying a cache line.
7356e5d1caSEric Dumazet * We use a temp variable to avoid fetching jiffies twice.
7456e5d1caSEric Dumazet */
tcf_lastuse_update(struct tcf_t * tm)7556e5d1caSEric Dumazet static inline void tcf_lastuse_update(struct tcf_t *tm)
7656e5d1caSEric Dumazet {
7756e5d1caSEric Dumazet unsigned long now = jiffies;
7856e5d1caSEric Dumazet
7956e5d1caSEric Dumazet if (tm->lastuse != now)
8056e5d1caSEric Dumazet tm->lastuse = now;
8153eb440fSJamal Hadi Salim if (unlikely(!tm->firstuse))
8253eb440fSJamal Hadi Salim tm->firstuse = now;
8356e5d1caSEric Dumazet }
8456e5d1caSEric Dumazet
tcf_tm_dump(struct tcf_t * dtm,const struct tcf_t * stm)8548d8ee16SJamal Hadi Salim static inline void tcf_tm_dump(struct tcf_t *dtm, const struct tcf_t *stm)
8648d8ee16SJamal Hadi Salim {
8748d8ee16SJamal Hadi Salim dtm->install = jiffies_to_clock_t(jiffies - stm->install);
8848d8ee16SJamal Hadi Salim dtm->lastuse = jiffies_to_clock_t(jiffies - stm->lastuse);
89b15e6263SRoman Mashak dtm->firstuse = stm->firstuse ?
90b15e6263SRoman Mashak jiffies_to_clock_t(jiffies - stm->firstuse) : 0;
9148d8ee16SJamal Hadi Salim dtm->expires = jiffies_to_clock_t(stm->expires);
9248d8ee16SJamal Hadi Salim }
9348d8ee16SJamal Hadi Salim
tc_act_hw_stats(u8 hw_stats)94c54e1d92SBaowen Zheng static inline enum flow_action_hw_stats tc_act_hw_stats(u8 hw_stats)
95c54e1d92SBaowen Zheng {
96c54e1d92SBaowen Zheng if (WARN_ON_ONCE(hw_stats > TCA_ACT_HW_STATS_ANY))
97c54e1d92SBaowen Zheng return FLOW_ACTION_HW_STATS_DONT_CARE;
98c54e1d92SBaowen Zheng else if (!hw_stats)
99c54e1d92SBaowen Zheng return FLOW_ACTION_HW_STATS_DISABLED;
100c54e1d92SBaowen Zheng
101c54e1d92SBaowen Zheng return hw_stats;
102c54e1d92SBaowen Zheng }
103c54e1d92SBaowen Zheng
1044a5da47dSVlad Buslov typedef void (*tc_action_priv_destructor)(void *priv);
1054a5da47dSVlad Buslov
106e9ce1cd3SDavid S. Miller struct tc_action_ops {
1071f747c26SWANG Cong struct list_head head;
1081da177e4SLinus Torvalds char kind[IFNAMSIZ];
109eddd2cf1SEli Cohen enum tca_id id; /* identifier should match kind */
110acd0a7abSZhengchao Shao unsigned int net_id;
111a85a970aSWANG Cong size_t size;
1121da177e4SLinus Torvalds struct module *owner;
1130b0f43feSJamal Hadi Salim int (*act)(struct sk_buff *, const struct tc_action *,
1147fd4b288SPaolo Abeni struct tcf_result *); /* called under RCU BH lock*/
1151da177e4SLinus Torvalds int (*dump)(struct sk_buff *, struct tc_action *, int, int);
1169a63b255SCong Wang void (*cleanup)(struct tc_action *);
117f061b48cSCong Wang int (*lookup)(struct net *net, struct tc_action **a, u32 index);
118c1b52739SBenjamin LaHaise int (*init)(struct net *net, struct nlattr *nla,
119695176bfSCong Wang struct nlattr *est, struct tc_action **act,
120695176bfSCong Wang struct tcf_proto *tp,
121abbb0d33SVlad Buslov u32 flags, struct netlink_ext_ack *extack);
122ddf97ccdSWANG Cong int (*walk)(struct net *, struct sk_buff *,
1231af85155SAlexander Aring struct netlink_callback *, int,
12441780105SAlexander Aring const struct tc_action_ops *,
12541780105SAlexander Aring struct netlink_ext_ack *);
1264b61d3e8SPo Liu void (*stats_update)(struct tc_action *, u64, u64, u64, u64, bool);
127a03b91b1SRoman Mashak size_t (*get_fill_size)(const struct tc_action *act);
128470d5060SVlad Buslov struct net_device *(*get_dev)(const struct tc_action *a,
129470d5060SVlad Buslov tc_action_priv_destructor *destructor);
1304a5da47dSVlad Buslov struct psample_group *
1314a5da47dSVlad Buslov (*get_psample_group)(const struct tc_action *a,
1324a5da47dSVlad Buslov tc_action_priv_destructor *destructor);
133c54e1d92SBaowen Zheng int (*offload_act_setup)(struct tc_action *act, void *entry_data,
134c2ccf84eSIdo Schimmel u32 *index_inc, bool bind,
135c2ccf84eSIdo Schimmel struct netlink_ext_ack *extack);
1361da177e4SLinus Torvalds };
1371da177e4SLinus Torvalds
1382a7d228fSPedro Tammela #ifdef CONFIG_NET_CLS_ACT
1392a7d228fSPedro Tammela
1402a7d228fSPedro Tammela #define ACT_P_CREATED 1
1412a7d228fSPedro Tammela #define ACT_P_DELETED 1
1422a7d228fSPedro Tammela
143ddf97ccdSWANG Cong struct tc_action_net {
14465a206c0SChris Mi struct tcf_idrinfo *idrinfo;
145ddf97ccdSWANG Cong const struct tc_action_ops *ops;
146ddf97ccdSWANG Cong };
147ddf97ccdSWANG Cong
148ddf97ccdSWANG Cong static inline
tc_action_net_init(struct net * net,struct tc_action_net * tn,const struct tc_action_ops * ops)149981471bdSCong Wang int tc_action_net_init(struct net *net, struct tc_action_net *tn,
150c7e460ceSCong Wang const struct tc_action_ops *ops)
151ddf97ccdSWANG Cong {
152ddf97ccdSWANG Cong int err = 0;
153ddf97ccdSWANG Cong
15465a206c0SChris Mi tn->idrinfo = kmalloc(sizeof(*tn->idrinfo), GFP_KERNEL);
15565a206c0SChris Mi if (!tn->idrinfo)
156ddf97ccdSWANG Cong return -ENOMEM;
157ddf97ccdSWANG Cong tn->ops = ops;
158981471bdSCong Wang tn->idrinfo->net = net;
15995278ddaSCong Wang mutex_init(&tn->idrinfo->lock);
16065a206c0SChris Mi idr_init(&tn->idrinfo->action_idr);
161ddf97ccdSWANG Cong return err;
162ddf97ccdSWANG Cong }
163ddf97ccdSWANG Cong
16465a206c0SChris Mi void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
16565a206c0SChris Mi struct tcf_idrinfo *idrinfo);
166ddf97ccdSWANG Cong
tc_action_net_exit(struct list_head * net_list,unsigned int id)167039af9c6SCong Wang static inline void tc_action_net_exit(struct list_head *net_list,
168039af9c6SCong Wang unsigned int id)
169ddf97ccdSWANG Cong {
170039af9c6SCong Wang struct net *net;
171039af9c6SCong Wang
172a159d3c4SCong Wang rtnl_lock();
173039af9c6SCong Wang list_for_each_entry(net, net_list, exit_list) {
174039af9c6SCong Wang struct tc_action_net *tn = net_generic(net, id);
175039af9c6SCong Wang
17665a206c0SChris Mi tcf_idrinfo_destroy(tn->ops, tn->idrinfo);
17765a206c0SChris Mi kfree(tn->idrinfo);
178ddf97ccdSWANG Cong }
179039af9c6SCong Wang rtnl_unlock();
180039af9c6SCong Wang }
181ddf97ccdSWANG Cong
182ddf97ccdSWANG Cong int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
183ddf97ccdSWANG Cong struct netlink_callback *cb, int type,
184b3620145SAlexander Aring const struct tc_action_ops *ops,
185b3620145SAlexander Aring struct netlink_ext_ack *extack);
18665a206c0SChris Mi int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index);
18765a206c0SChris Mi int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
18865a206c0SChris Mi struct tc_action **a, const struct tc_action_ops *ops,
189e3822678SVlad Buslov int bind, bool cpustats, u32 flags);
190e3822678SVlad Buslov int tcf_idr_create_from_flags(struct tc_action_net *tn, u32 index,
191e3822678SVlad Buslov struct nlattr *est, struct tc_action **a,
192e3822678SVlad Buslov const struct tc_action_ops *ops, int bind,
193e3822678SVlad Buslov u32 flags);
194396d7f23SVlad Buslov void tcf_idr_insert_many(struct tc_action *actions[]);
1950190c1d4SVlad Buslov void tcf_idr_cleanup(struct tc_action_net *tn, u32 index);
1960190c1d4SVlad Buslov int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
1970190c1d4SVlad Buslov struct tc_action **a, int bind);
198b3650bf7SVlad Buslov int tcf_idr_release(struct tc_action *a, bool bind);
19928e6b67fSDaniel Borkmann
200ddf97ccdSWANG Cong int tcf_register_action(struct tc_action_ops *a, struct pernet_operations *ops);
2010b0f43feSJamal Hadi Salim int tcf_unregister_action(struct tc_action_ops *a,
2020b0f43feSJamal Hadi Salim struct pernet_operations *ops);
20390b73b77SVlad Buslov int tcf_action_destroy(struct tc_action *actions[], int bind);
20422dc13c8SWANG Cong int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
20522dc13c8SWANG Cong int nr_actions, struct tcf_result *res);
2069fb9f251SJiri Pirko int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
207695176bfSCong Wang struct nlattr *est,
20887c750e8SVlad Buslov struct tc_action *actions[], int init_res[], size_t *attr_size,
209c86e0209SBaowen Zheng u32 flags, u32 fl_flags, struct netlink_ext_ack *extack);
210695176bfSCong Wang struct tc_action_ops *tc_action_load_ops(struct nlattr *nla, bool police,
211d349f997SCong Wang bool rtnl_held,
212d349f997SCong Wang struct netlink_ext_ack *extack);
2139fb9f251SJiri Pirko struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
2149fb9f251SJiri Pirko struct nlattr *nla, struct nlattr *est,
21587c750e8SVlad Buslov struct tc_action_ops *a_o, int *init_res,
216695176bfSCong Wang u32 flags, struct netlink_ext_ack *extack);
21790b73b77SVlad Buslov int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[], int bind,
218ca44b738SVlad Buslov int ref, bool terse);
2195c15257fSJoe Perches int tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int, int);
2205c15257fSJoe Perches int tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int, int);
2215e1ad95bSVlad Buslov
tcf_action_update_bstats(struct tc_action * a,struct sk_buff * skb)2225e1ad95bSVlad Buslov static inline void tcf_action_update_bstats(struct tc_action *a,
2235e1ad95bSVlad Buslov struct sk_buff *skb)
2245e1ad95bSVlad Buslov {
2255e174d5eSVlad Buslov if (likely(a->cpu_bstats)) {
22650dc9a85SAhmed S. Darwish bstats_update(this_cpu_ptr(a->cpu_bstats), skb);
2275e174d5eSVlad Buslov return;
2285e1ad95bSVlad Buslov }
2295e174d5eSVlad Buslov spin_lock(&a->tcfa_lock);
2305e174d5eSVlad Buslov bstats_update(&a->tcfa_bstats, skb);
2315e174d5eSVlad Buslov spin_unlock(&a->tcfa_lock);
23226b537a8SVlad Buslov }
23326b537a8SVlad Buslov
tcf_action_inc_drop_qstats(struct tc_action * a)23426b537a8SVlad Buslov static inline void tcf_action_inc_drop_qstats(struct tc_action *a)
23526b537a8SVlad Buslov {
2365e174d5eSVlad Buslov if (likely(a->cpu_qstats)) {
23726b537a8SVlad Buslov qstats_drop_inc(this_cpu_ptr(a->cpu_qstats));
2385e174d5eSVlad Buslov return;
2395e174d5eSVlad Buslov }
2405e174d5eSVlad Buslov spin_lock(&a->tcfa_lock);
2415e174d5eSVlad Buslov qstats_drop_inc(&a->tcfa_qstats);
2425e174d5eSVlad Buslov spin_unlock(&a->tcfa_lock);
24326b537a8SVlad Buslov }
24426b537a8SVlad Buslov
tcf_action_inc_overlimit_qstats(struct tc_action * a)24526b537a8SVlad Buslov static inline void tcf_action_inc_overlimit_qstats(struct tc_action *a)
24626b537a8SVlad Buslov {
2475e174d5eSVlad Buslov if (likely(a->cpu_qstats)) {
24826b537a8SVlad Buslov qstats_overlimit_inc(this_cpu_ptr(a->cpu_qstats));
2495e174d5eSVlad Buslov return;
2505e174d5eSVlad Buslov }
2515e174d5eSVlad Buslov spin_lock(&a->tcfa_lock);
2525e174d5eSVlad Buslov qstats_overlimit_inc(&a->tcfa_qstats);
2535e174d5eSVlad Buslov spin_unlock(&a->tcfa_lock);
25426b537a8SVlad Buslov }
25526b537a8SVlad Buslov
2564b61d3e8SPo Liu void tcf_action_update_stats(struct tc_action *a, u64 bytes, u64 packets,
2574b61d3e8SPo Liu u64 drops, bool hw);
2585c15257fSJoe Perches int tcf_action_copy_stats(struct sk_buff *, struct tc_action *, int);
25900175aecSAmir Vadai
260c7a66f8dSBaowen Zheng int tcf_action_update_hw_stats(struct tc_action *action);
26113926d19SBaowen Zheng int tcf_action_reoffload_cb(flow_indr_block_bind_cb_t *cb,
26213926d19SBaowen Zheng void *cb_priv, bool add);
26385d0966fSDavide Caratti int tcf_action_check_ctrlact(int action, struct tcf_proto *tp,
26485d0966fSDavide Caratti struct tcf_chain **handle,
26585d0966fSDavide Caratti struct netlink_ext_ack *newchain);
26685d0966fSDavide Caratti struct tcf_chain *tcf_action_set_ctrlact(struct tc_action *a, int action,
26785d0966fSDavide Caratti struct tcf_chain *newchain);
268c129412fSwenxu
269c129412fSwenxu #ifdef CONFIG_INET
270c129412fSwenxu DECLARE_STATIC_KEY_FALSE(tcf_frag_xmit_count);
271c129412fSwenxu #endif
272c129412fSwenxu
273c129412fSwenxu int tcf_dev_queue_xmit(struct sk_buff *skb, int (*xmit)(struct sk_buff *skb));
27413926d19SBaowen Zheng
27513926d19SBaowen Zheng #else /* !CONFIG_NET_CLS_ACT */
27613926d19SBaowen Zheng
tcf_action_reoffload_cb(flow_indr_block_bind_cb_t * cb,void * cb_priv,bool add)27713926d19SBaowen Zheng static inline int tcf_action_reoffload_cb(flow_indr_block_bind_cb_t *cb,
27813926d19SBaowen Zheng void *cb_priv, bool add) {
27913926d19SBaowen Zheng return 0;
28013926d19SBaowen Zheng }
28113926d19SBaowen Zheng
2822734437eSWANG Cong #endif /* CONFIG_NET_CLS_ACT */
28386cb13e4SIdo Schimmel
tcf_action_stats_update(struct tc_action * a,u64 bytes,u64 packets,u64 drops,u64 lastuse,bool hw)28438040702SAmir Vadai static inline void tcf_action_stats_update(struct tc_action *a, u64 bytes,
2854b61d3e8SPo Liu u64 packets, u64 drops,
2864b61d3e8SPo Liu u64 lastuse, bool hw)
28738040702SAmir Vadai {
2882734437eSWANG Cong #ifdef CONFIG_NET_CLS_ACT
28938040702SAmir Vadai if (!a->ops->stats_update)
29038040702SAmir Vadai return;
29138040702SAmir Vadai
2924b61d3e8SPo Liu a->ops->stats_update(a, bytes, packets, drops, lastuse, hw);
2932734437eSWANG Cong #endif
29438040702SAmir Vadai }
29538040702SAmir Vadai
296b3f55bddSJiri Pirko
2971da177e4SLinus Torvalds #endif
298