xref: /openbmc/linux/include/net/act_api.h (revision b35565bb)
1 #ifndef __NET_ACT_API_H
2 #define __NET_ACT_API_H
3 
4 /*
5  * Public action API for classifiers/qdiscs
6 */
7 
8 #include <net/sch_generic.h>
9 #include <net/pkt_sched.h>
10 #include <net/net_namespace.h>
11 #include <net/netns/generic.h>
12 
13 struct tcf_idrinfo {
14 	spinlock_t	lock;
15 	struct idr	action_idr;
16 };
17 
18 struct tc_action_ops;
19 
20 struct tc_action {
21 	const struct tc_action_ops	*ops;
22 	__u32				type; /* for backward compat(TCA_OLD_COMPAT) */
23 	__u32				order;
24 	struct list_head		list;
25 	struct tcf_idrinfo		*idrinfo;
26 
27 	u32				tcfa_index;
28 	int				tcfa_refcnt;
29 	int				tcfa_bindcnt;
30 	u32				tcfa_capab;
31 	int				tcfa_action;
32 	struct tcf_t			tcfa_tm;
33 	struct gnet_stats_basic_packed	tcfa_bstats;
34 	struct gnet_stats_queue		tcfa_qstats;
35 	struct net_rate_estimator __rcu *tcfa_rate_est;
36 	spinlock_t			tcfa_lock;
37 	struct gnet_stats_basic_cpu __percpu *cpu_bstats;
38 	struct gnet_stats_queue __percpu *cpu_qstats;
39 	struct tc_cookie	*act_cookie;
40 	struct tcf_chain	*goto_chain;
41 };
42 #define tcf_index	common.tcfa_index
43 #define tcf_refcnt	common.tcfa_refcnt
44 #define tcf_bindcnt	common.tcfa_bindcnt
45 #define tcf_capab	common.tcfa_capab
46 #define tcf_action	common.tcfa_action
47 #define tcf_tm		common.tcfa_tm
48 #define tcf_bstats	common.tcfa_bstats
49 #define tcf_qstats	common.tcfa_qstats
50 #define tcf_rate_est	common.tcfa_rate_est
51 #define tcf_lock	common.tcfa_lock
52 
53 /* Update lastuse only if needed, to avoid dirtying a cache line.
54  * We use a temp variable to avoid fetching jiffies twice.
55  */
56 static inline void tcf_lastuse_update(struct tcf_t *tm)
57 {
58 	unsigned long now = jiffies;
59 
60 	if (tm->lastuse != now)
61 		tm->lastuse = now;
62 	if (unlikely(!tm->firstuse))
63 		tm->firstuse = now;
64 }
65 
66 static inline void tcf_tm_dump(struct tcf_t *dtm, const struct tcf_t *stm)
67 {
68 	dtm->install = jiffies_to_clock_t(jiffies - stm->install);
69 	dtm->lastuse = jiffies_to_clock_t(jiffies - stm->lastuse);
70 	dtm->firstuse = jiffies_to_clock_t(jiffies - stm->firstuse);
71 	dtm->expires = jiffies_to_clock_t(stm->expires);
72 }
73 
74 #ifdef CONFIG_NET_CLS_ACT
75 
76 #define ACT_P_CREATED 1
77 #define ACT_P_DELETED 1
78 
79 struct tc_action_ops {
80 	struct list_head head;
81 	char    kind[IFNAMSIZ];
82 	__u32   type; /* TBD to match kind */
83 	size_t	size;
84 	struct module		*owner;
85 	int     (*act)(struct sk_buff *, const struct tc_action *,
86 		       struct tcf_result *);
87 	int     (*dump)(struct sk_buff *, struct tc_action *, int, int);
88 	void	(*cleanup)(struct tc_action *, int bind);
89 	int     (*lookup)(struct net *, struct tc_action **, u32);
90 	int     (*init)(struct net *net, struct nlattr *nla,
91 			struct nlattr *est, struct tc_action **act, int ovr,
92 			int bind);
93 	int     (*walk)(struct net *, struct sk_buff *,
94 			struct netlink_callback *, int, const struct tc_action_ops *);
95 	void	(*stats_update)(struct tc_action *, u64, u32, u64);
96 	int	(*get_dev)(const struct tc_action *a, struct net *net,
97 			   struct net_device **mirred_dev);
98 };
99 
100 struct tc_action_net {
101 	struct tcf_idrinfo *idrinfo;
102 	const struct tc_action_ops *ops;
103 };
104 
105 static inline
106 int tc_action_net_init(struct tc_action_net *tn,
107 		       const struct tc_action_ops *ops)
108 {
109 	int err = 0;
110 
111 	tn->idrinfo = kmalloc(sizeof(*tn->idrinfo), GFP_KERNEL);
112 	if (!tn->idrinfo)
113 		return -ENOMEM;
114 	tn->ops = ops;
115 	spin_lock_init(&tn->idrinfo->lock);
116 	idr_init(&tn->idrinfo->action_idr);
117 	return err;
118 }
119 
120 void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
121 			 struct tcf_idrinfo *idrinfo);
122 
123 static inline void tc_action_net_exit(struct tc_action_net *tn)
124 {
125 	tcf_idrinfo_destroy(tn->ops, tn->idrinfo);
126 	kfree(tn->idrinfo);
127 }
128 
129 int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
130 		       struct netlink_callback *cb, int type,
131 		       const struct tc_action_ops *ops);
132 int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index);
133 bool tcf_idr_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
134 		    int bind);
135 int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
136 		   struct tc_action **a, const struct tc_action_ops *ops,
137 		   int bind, bool cpustats);
138 void tcf_idr_cleanup(struct tc_action *a, struct nlattr *est);
139 void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a);
140 
141 int __tcf_idr_release(struct tc_action *a, bool bind, bool strict);
142 
143 static inline int tcf_idr_release(struct tc_action *a, bool bind)
144 {
145 	return __tcf_idr_release(a, bind, false);
146 }
147 
148 int tcf_register_action(struct tc_action_ops *a, struct pernet_operations *ops);
149 int tcf_unregister_action(struct tc_action_ops *a,
150 			  struct pernet_operations *ops);
151 int tcf_action_destroy(struct list_head *actions, int bind);
152 int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
153 		    int nr_actions, struct tcf_result *res);
154 int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
155 		    struct nlattr *est, char *name, int ovr, int bind,
156 		    struct list_head *actions);
157 struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
158 				    struct nlattr *nla, struct nlattr *est,
159 				    char *name, int ovr, int bind);
160 int tcf_action_dump(struct sk_buff *skb, struct list_head *, int, int);
161 int tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int, int);
162 int tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int, int);
163 int tcf_action_copy_stats(struct sk_buff *, struct tc_action *, int);
164 
165 #endif /* CONFIG_NET_CLS_ACT */
166 
167 static inline void tcf_action_stats_update(struct tc_action *a, u64 bytes,
168 					   u64 packets, u64 lastuse)
169 {
170 #ifdef CONFIG_NET_CLS_ACT
171 	if (!a->ops->stats_update)
172 		return;
173 
174 	a->ops->stats_update(a, bytes, packets, lastuse);
175 #endif
176 }
177 
178 #endif
179