18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci#ifndef __NET_ACT_API_H 38c2ecf20Sopenharmony_ci#define __NET_ACT_API_H 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci/* 68c2ecf20Sopenharmony_ci * Public action API for classifiers/qdiscs 78c2ecf20Sopenharmony_ci*/ 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/refcount.h> 108c2ecf20Sopenharmony_ci#include <net/sch_generic.h> 118c2ecf20Sopenharmony_ci#include <net/pkt_sched.h> 128c2ecf20Sopenharmony_ci#include <net/net_namespace.h> 138c2ecf20Sopenharmony_ci#include <net/netns/generic.h> 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_cistruct tcf_idrinfo { 168c2ecf20Sopenharmony_ci struct mutex lock; 178c2ecf20Sopenharmony_ci struct idr action_idr; 188c2ecf20Sopenharmony_ci struct net *net; 198c2ecf20Sopenharmony_ci}; 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_cistruct tc_action_ops; 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_cistruct tc_action { 248c2ecf20Sopenharmony_ci const struct tc_action_ops *ops; 258c2ecf20Sopenharmony_ci __u32 type; /* for backward compat(TCA_OLD_COMPAT) */ 268c2ecf20Sopenharmony_ci struct tcf_idrinfo *idrinfo; 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci u32 tcfa_index; 298c2ecf20Sopenharmony_ci refcount_t tcfa_refcnt; 308c2ecf20Sopenharmony_ci atomic_t tcfa_bindcnt; 318c2ecf20Sopenharmony_ci int tcfa_action; 328c2ecf20Sopenharmony_ci struct tcf_t tcfa_tm; 338c2ecf20Sopenharmony_ci struct gnet_stats_basic_packed tcfa_bstats; 348c2ecf20Sopenharmony_ci struct gnet_stats_basic_packed tcfa_bstats_hw; 358c2ecf20Sopenharmony_ci struct gnet_stats_queue tcfa_qstats; 368c2ecf20Sopenharmony_ci struct net_rate_estimator __rcu *tcfa_rate_est; 378c2ecf20Sopenharmony_ci spinlock_t tcfa_lock; 388c2ecf20Sopenharmony_ci struct gnet_stats_basic_cpu __percpu *cpu_bstats; 398c2ecf20Sopenharmony_ci struct gnet_stats_basic_cpu __percpu *cpu_bstats_hw; 408c2ecf20Sopenharmony_ci struct gnet_stats_queue __percpu *cpu_qstats; 418c2ecf20Sopenharmony_ci struct tc_cookie __rcu *act_cookie; 428c2ecf20Sopenharmony_ci struct tcf_chain __rcu *goto_chain; 438c2ecf20Sopenharmony_ci u32 tcfa_flags; 448c2ecf20Sopenharmony_ci u8 hw_stats; 458c2ecf20Sopenharmony_ci u8 used_hw_stats; 468c2ecf20Sopenharmony_ci bool used_hw_stats_valid; 478c2ecf20Sopenharmony_ci}; 488c2ecf20Sopenharmony_ci#define tcf_index common.tcfa_index 498c2ecf20Sopenharmony_ci#define tcf_refcnt common.tcfa_refcnt 508c2ecf20Sopenharmony_ci#define tcf_bindcnt common.tcfa_bindcnt 518c2ecf20Sopenharmony_ci#define tcf_action common.tcfa_action 528c2ecf20Sopenharmony_ci#define tcf_tm common.tcfa_tm 538c2ecf20Sopenharmony_ci#define tcf_bstats common.tcfa_bstats 548c2ecf20Sopenharmony_ci#define tcf_qstats common.tcfa_qstats 558c2ecf20Sopenharmony_ci#define tcf_rate_est common.tcfa_rate_est 568c2ecf20Sopenharmony_ci#define tcf_lock common.tcfa_lock 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci#define TCA_ACT_HW_STATS_ANY (TCA_ACT_HW_STATS_IMMEDIATE | \ 598c2ecf20Sopenharmony_ci TCA_ACT_HW_STATS_DELAYED) 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci/* Update lastuse only if needed, to avoid dirtying a cache line. 628c2ecf20Sopenharmony_ci * We use a temp variable to avoid fetching jiffies twice. 638c2ecf20Sopenharmony_ci */ 648c2ecf20Sopenharmony_cistatic inline void tcf_lastuse_update(struct tcf_t *tm) 658c2ecf20Sopenharmony_ci{ 668c2ecf20Sopenharmony_ci unsigned long now = jiffies; 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci if (tm->lastuse != now) 698c2ecf20Sopenharmony_ci tm->lastuse = now; 708c2ecf20Sopenharmony_ci if (unlikely(!tm->firstuse)) 718c2ecf20Sopenharmony_ci tm->firstuse = now; 728c2ecf20Sopenharmony_ci} 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_cistatic inline void tcf_tm_dump(struct tcf_t *dtm, const struct tcf_t *stm) 758c2ecf20Sopenharmony_ci{ 768c2ecf20Sopenharmony_ci dtm->install = jiffies_to_clock_t(jiffies - stm->install); 778c2ecf20Sopenharmony_ci dtm->lastuse = jiffies_to_clock_t(jiffies - stm->lastuse); 788c2ecf20Sopenharmony_ci dtm->firstuse = stm->firstuse ? 798c2ecf20Sopenharmony_ci jiffies_to_clock_t(jiffies - stm->firstuse) : 0; 808c2ecf20Sopenharmony_ci dtm->expires = jiffies_to_clock_t(stm->expires); 818c2ecf20Sopenharmony_ci} 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci#ifdef CONFIG_NET_CLS_ACT 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci#define ACT_P_CREATED 1 868c2ecf20Sopenharmony_ci#define ACT_P_DELETED 1 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_citypedef void (*tc_action_priv_destructor)(void *priv); 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_cistruct tc_action_ops { 918c2ecf20Sopenharmony_ci struct list_head head; 928c2ecf20Sopenharmony_ci char kind[IFNAMSIZ]; 938c2ecf20Sopenharmony_ci enum tca_id id; /* identifier should match kind */ 948c2ecf20Sopenharmony_ci size_t size; 958c2ecf20Sopenharmony_ci struct module *owner; 968c2ecf20Sopenharmony_ci int (*act)(struct sk_buff *, const struct tc_action *, 978c2ecf20Sopenharmony_ci struct tcf_result *); /* called under RCU BH lock*/ 988c2ecf20Sopenharmony_ci int (*dump)(struct sk_buff *, struct tc_action *, int, int); 998c2ecf20Sopenharmony_ci void (*cleanup)(struct tc_action *); 1008c2ecf20Sopenharmony_ci int (*lookup)(struct net *net, struct tc_action **a, u32 index); 1018c2ecf20Sopenharmony_ci int (*init)(struct net *net, struct nlattr *nla, 1028c2ecf20Sopenharmony_ci struct nlattr *est, struct tc_action **act, int ovr, 1038c2ecf20Sopenharmony_ci int bind, bool rtnl_held, struct tcf_proto *tp, 1048c2ecf20Sopenharmony_ci u32 flags, struct netlink_ext_ack *extack); 1058c2ecf20Sopenharmony_ci int (*walk)(struct net *, struct sk_buff *, 1068c2ecf20Sopenharmony_ci struct netlink_callback *, int, 1078c2ecf20Sopenharmony_ci const struct tc_action_ops *, 1088c2ecf20Sopenharmony_ci struct netlink_ext_ack *); 1098c2ecf20Sopenharmony_ci void (*stats_update)(struct tc_action *, u64, u64, u64, u64, bool); 1108c2ecf20Sopenharmony_ci size_t (*get_fill_size)(const struct tc_action *act); 1118c2ecf20Sopenharmony_ci struct net_device *(*get_dev)(const struct tc_action *a, 1128c2ecf20Sopenharmony_ci tc_action_priv_destructor *destructor); 1138c2ecf20Sopenharmony_ci struct psample_group * 1148c2ecf20Sopenharmony_ci (*get_psample_group)(const struct tc_action *a, 1158c2ecf20Sopenharmony_ci tc_action_priv_destructor *destructor); 1168c2ecf20Sopenharmony_ci}; 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_cistruct tc_action_net { 1198c2ecf20Sopenharmony_ci struct tcf_idrinfo *idrinfo; 1208c2ecf20Sopenharmony_ci const struct tc_action_ops *ops; 1218c2ecf20Sopenharmony_ci}; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_cistatic inline 1248c2ecf20Sopenharmony_ciint tc_action_net_init(struct net *net, struct tc_action_net *tn, 1258c2ecf20Sopenharmony_ci const struct tc_action_ops *ops) 1268c2ecf20Sopenharmony_ci{ 1278c2ecf20Sopenharmony_ci int err = 0; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci tn->idrinfo = kmalloc(sizeof(*tn->idrinfo), GFP_KERNEL); 1308c2ecf20Sopenharmony_ci if (!tn->idrinfo) 1318c2ecf20Sopenharmony_ci return -ENOMEM; 1328c2ecf20Sopenharmony_ci tn->ops = ops; 1338c2ecf20Sopenharmony_ci tn->idrinfo->net = net; 1348c2ecf20Sopenharmony_ci mutex_init(&tn->idrinfo->lock); 1358c2ecf20Sopenharmony_ci idr_init(&tn->idrinfo->action_idr); 1368c2ecf20Sopenharmony_ci return err; 1378c2ecf20Sopenharmony_ci} 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_civoid tcf_idrinfo_destroy(const struct tc_action_ops *ops, 1408c2ecf20Sopenharmony_ci struct tcf_idrinfo *idrinfo); 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_cistatic inline void tc_action_net_exit(struct list_head *net_list, 1438c2ecf20Sopenharmony_ci unsigned int id) 1448c2ecf20Sopenharmony_ci{ 1458c2ecf20Sopenharmony_ci struct net *net; 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci rtnl_lock(); 1488c2ecf20Sopenharmony_ci list_for_each_entry(net, net_list, exit_list) { 1498c2ecf20Sopenharmony_ci struct tc_action_net *tn = net_generic(net, id); 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci tcf_idrinfo_destroy(tn->ops, tn->idrinfo); 1528c2ecf20Sopenharmony_ci kfree(tn->idrinfo); 1538c2ecf20Sopenharmony_ci } 1548c2ecf20Sopenharmony_ci rtnl_unlock(); 1558c2ecf20Sopenharmony_ci} 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ciint tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb, 1588c2ecf20Sopenharmony_ci struct netlink_callback *cb, int type, 1598c2ecf20Sopenharmony_ci const struct tc_action_ops *ops, 1608c2ecf20Sopenharmony_ci struct netlink_ext_ack *extack); 1618c2ecf20Sopenharmony_ciint tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index); 1628c2ecf20Sopenharmony_ciint tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est, 1638c2ecf20Sopenharmony_ci struct tc_action **a, const struct tc_action_ops *ops, 1648c2ecf20Sopenharmony_ci int bind, bool cpustats, u32 flags); 1658c2ecf20Sopenharmony_ciint tcf_idr_create_from_flags(struct tc_action_net *tn, u32 index, 1668c2ecf20Sopenharmony_ci struct nlattr *est, struct tc_action **a, 1678c2ecf20Sopenharmony_ci const struct tc_action_ops *ops, int bind, 1688c2ecf20Sopenharmony_ci u32 flags); 1698c2ecf20Sopenharmony_civoid tcf_idr_insert_many(struct tc_action *actions[]); 1708c2ecf20Sopenharmony_civoid tcf_idr_cleanup(struct tc_action_net *tn, u32 index); 1718c2ecf20Sopenharmony_ciint tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index, 1728c2ecf20Sopenharmony_ci struct tc_action **a, int bind); 1738c2ecf20Sopenharmony_ciint tcf_idr_release(struct tc_action *a, bool bind); 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ciint tcf_register_action(struct tc_action_ops *a, struct pernet_operations *ops); 1768c2ecf20Sopenharmony_ciint tcf_unregister_action(struct tc_action_ops *a, 1778c2ecf20Sopenharmony_ci struct pernet_operations *ops); 1788c2ecf20Sopenharmony_ciint tcf_action_destroy(struct tc_action *actions[], int bind); 1798c2ecf20Sopenharmony_ciint tcf_action_exec(struct sk_buff *skb, struct tc_action **actions, 1808c2ecf20Sopenharmony_ci int nr_actions, struct tcf_result *res); 1818c2ecf20Sopenharmony_ciint tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla, 1828c2ecf20Sopenharmony_ci struct nlattr *est, char *name, int ovr, int bind, 1838c2ecf20Sopenharmony_ci struct tc_action *actions[], int init_res[], size_t *attr_size, 1848c2ecf20Sopenharmony_ci bool rtnl_held, struct netlink_ext_ack *extack); 1858c2ecf20Sopenharmony_cistruct tc_action_ops *tc_action_load_ops(char *name, struct nlattr *nla, 1868c2ecf20Sopenharmony_ci bool rtnl_held, 1878c2ecf20Sopenharmony_ci struct netlink_ext_ack *extack); 1888c2ecf20Sopenharmony_cistruct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp, 1898c2ecf20Sopenharmony_ci struct nlattr *nla, struct nlattr *est, 1908c2ecf20Sopenharmony_ci char *name, int ovr, int bind, 1918c2ecf20Sopenharmony_ci struct tc_action_ops *a_o, int *init_res, 1928c2ecf20Sopenharmony_ci bool rtnl_held, 1938c2ecf20Sopenharmony_ci struct netlink_ext_ack *extack); 1948c2ecf20Sopenharmony_ciint tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[], int bind, 1958c2ecf20Sopenharmony_ci int ref, bool terse); 1968c2ecf20Sopenharmony_ciint tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int, int); 1978c2ecf20Sopenharmony_ciint tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int, int); 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_cistatic inline void tcf_action_update_bstats(struct tc_action *a, 2008c2ecf20Sopenharmony_ci struct sk_buff *skb) 2018c2ecf20Sopenharmony_ci{ 2028c2ecf20Sopenharmony_ci if (likely(a->cpu_bstats)) { 2038c2ecf20Sopenharmony_ci bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), skb); 2048c2ecf20Sopenharmony_ci return; 2058c2ecf20Sopenharmony_ci } 2068c2ecf20Sopenharmony_ci spin_lock(&a->tcfa_lock); 2078c2ecf20Sopenharmony_ci bstats_update(&a->tcfa_bstats, skb); 2088c2ecf20Sopenharmony_ci spin_unlock(&a->tcfa_lock); 2098c2ecf20Sopenharmony_ci} 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_cistatic inline void tcf_action_inc_drop_qstats(struct tc_action *a) 2128c2ecf20Sopenharmony_ci{ 2138c2ecf20Sopenharmony_ci if (likely(a->cpu_qstats)) { 2148c2ecf20Sopenharmony_ci qstats_drop_inc(this_cpu_ptr(a->cpu_qstats)); 2158c2ecf20Sopenharmony_ci return; 2168c2ecf20Sopenharmony_ci } 2178c2ecf20Sopenharmony_ci spin_lock(&a->tcfa_lock); 2188c2ecf20Sopenharmony_ci qstats_drop_inc(&a->tcfa_qstats); 2198c2ecf20Sopenharmony_ci spin_unlock(&a->tcfa_lock); 2208c2ecf20Sopenharmony_ci} 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_cistatic inline void tcf_action_inc_overlimit_qstats(struct tc_action *a) 2238c2ecf20Sopenharmony_ci{ 2248c2ecf20Sopenharmony_ci if (likely(a->cpu_qstats)) { 2258c2ecf20Sopenharmony_ci qstats_overlimit_inc(this_cpu_ptr(a->cpu_qstats)); 2268c2ecf20Sopenharmony_ci return; 2278c2ecf20Sopenharmony_ci } 2288c2ecf20Sopenharmony_ci spin_lock(&a->tcfa_lock); 2298c2ecf20Sopenharmony_ci qstats_overlimit_inc(&a->tcfa_qstats); 2308c2ecf20Sopenharmony_ci spin_unlock(&a->tcfa_lock); 2318c2ecf20Sopenharmony_ci} 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_civoid tcf_action_update_stats(struct tc_action *a, u64 bytes, u64 packets, 2348c2ecf20Sopenharmony_ci u64 drops, bool hw); 2358c2ecf20Sopenharmony_ciint tcf_action_copy_stats(struct sk_buff *, struct tc_action *, int); 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ciint tcf_action_check_ctrlact(int action, struct tcf_proto *tp, 2388c2ecf20Sopenharmony_ci struct tcf_chain **handle, 2398c2ecf20Sopenharmony_ci struct netlink_ext_ack *newchain); 2408c2ecf20Sopenharmony_cistruct tcf_chain *tcf_action_set_ctrlact(struct tc_action *a, int action, 2418c2ecf20Sopenharmony_ci struct tcf_chain *newchain); 2428c2ecf20Sopenharmony_ci#endif /* CONFIG_NET_CLS_ACT */ 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_cistatic inline void tcf_action_stats_update(struct tc_action *a, u64 bytes, 2458c2ecf20Sopenharmony_ci u64 packets, u64 drops, 2468c2ecf20Sopenharmony_ci u64 lastuse, bool hw) 2478c2ecf20Sopenharmony_ci{ 2488c2ecf20Sopenharmony_ci#ifdef CONFIG_NET_CLS_ACT 2498c2ecf20Sopenharmony_ci if (!a->ops->stats_update) 2508c2ecf20Sopenharmony_ci return; 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ci a->ops->stats_update(a, bytes, packets, drops, lastuse, hw); 2538c2ecf20Sopenharmony_ci#endif 2548c2ecf20Sopenharmony_ci} 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci#endif 258