xref: /linux/include/net/act_api.h (revision 83a37b3292f4aca799b355179ad6fbdd78a08e10)
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 	struct net_device *(*get_dev)(const struct tc_action *a);
97 };
98 
99 struct tc_action_net {
100 	struct tcf_idrinfo *idrinfo;
101 	const struct tc_action_ops *ops;
102 };
103 
104 static inline
105 int tc_action_net_init(struct tc_action_net *tn,
106 		       const struct tc_action_ops *ops)
107 {
108 	int err = 0;
109 
110 	tn->idrinfo = kmalloc(sizeof(*tn->idrinfo), GFP_KERNEL);
111 	if (!tn->idrinfo)
112 		return -ENOMEM;
113 	tn->ops = ops;
114 	spin_lock_init(&tn->idrinfo->lock);
115 	idr_init(&tn->idrinfo->action_idr);
116 	return err;
117 }
118 
119 void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
120 			 struct tcf_idrinfo *idrinfo);
121 
122 static inline void tc_action_net_exit(struct tc_action_net *tn)
123 {
124 	tcf_idrinfo_destroy(tn->ops, tn->idrinfo);
125 	kfree(tn->idrinfo);
126 }
127 
128 int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
129 		       struct netlink_callback *cb, int type,
130 		       const struct tc_action_ops *ops);
131 int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index);
132 bool tcf_idr_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
133 		    int bind);
134 int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
135 		   struct tc_action **a, const struct tc_action_ops *ops,
136 		   int bind, bool cpustats);
137 void tcf_idr_cleanup(struct tc_action *a, struct nlattr *est);
138 void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a);
139 
140 int __tcf_idr_release(struct tc_action *a, bool bind, bool strict);
141 
142 static inline int tcf_idr_release(struct tc_action *a, bool bind)
143 {
144 	return __tcf_idr_release(a, bind, false);
145 }
146 
147 int tcf_register_action(struct tc_action_ops *a, struct pernet_operations *ops);
148 int tcf_unregister_action(struct tc_action_ops *a,
149 			  struct pernet_operations *ops);
150 int tcf_action_destroy(struct list_head *actions, int bind);
151 int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
152 		    int nr_actions, struct tcf_result *res);
153 int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
154 		    struct nlattr *est, char *name, int ovr, int bind,
155 		    struct list_head *actions);
156 struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
157 				    struct nlattr *nla, struct nlattr *est,
158 				    char *name, int ovr, int bind);
159 int tcf_action_dump(struct sk_buff *skb, struct list_head *, int, int);
160 int tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int, int);
161 int tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int, int);
162 int tcf_action_copy_stats(struct sk_buff *, struct tc_action *, int);
163 
164 #endif /* CONFIG_NET_CLS_ACT */
165 
166 static inline void tcf_action_stats_update(struct tc_action *a, u64 bytes,
167 					   u64 packets, u64 lastuse)
168 {
169 #ifdef CONFIG_NET_CLS_ACT
170 	if (!a->ops->stats_update)
171 		return;
172 
173 	a->ops->stats_update(a, bytes, packets, lastuse);
174 #endif
175 }
176 
177 typedef int tc_setup_cb_t(enum tc_setup_type type,
178 			  void *type_data, void *cb_priv);
179 
180 #ifdef CONFIG_NET_CLS_ACT
181 int tc_setup_cb_egdev_register(const struct net_device *dev,
182 			       tc_setup_cb_t *cb, void *cb_priv);
183 void tc_setup_cb_egdev_unregister(const struct net_device *dev,
184 				  tc_setup_cb_t *cb, void *cb_priv);
185 int tc_setup_cb_egdev_call(const struct net_device *dev,
186 			   enum tc_setup_type type, void *type_data,
187 			   bool err_stop);
188 #else
189 static inline
190 int tc_setup_cb_egdev_register(const struct net_device *dev,
191 			       tc_setup_cb_t *cb, void *cb_priv)
192 {
193 	return 0;
194 }
195 
196 static inline
197 void tc_setup_cb_egdev_unregister(const struct net_device *dev,
198 				  tc_setup_cb_t *cb, void *cb_priv)
199 {
200 }
201 
202 static inline
203 int tc_setup_cb_egdev_call(const struct net_device *dev,
204 			   enum tc_setup_type type, void *type_data,
205 			   bool err_stop)
206 {
207 	return 0;
208 }
209 #endif
210 
211 #endif
212