]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - include/net/act_api.h
net: sched: modify stats helper functions to support regular stats
[mirror_ubuntu-jammy-kernel.git] / include / net / act_api.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4
LT
2#ifndef __NET_ACT_API_H
3#define __NET_ACT_API_H
4
5/*
0b0f43fe
JHS
6 * Public action API for classifiers/qdiscs
7*/
1da177e4 8
036bb443 9#include <linux/refcount.h>
1da177e4
LT
10#include <net/sch_generic.h>
11#include <net/pkt_sched.h>
ddf97ccd
WC
12#include <net/net_namespace.h>
13#include <net/netns/generic.h>
1da177e4 14
65a206c0 15struct tcf_idrinfo {
95278dda 16 struct mutex lock;
65a206c0 17 struct idr action_idr;
981471bd 18 struct net *net;
a85a970a
WC
19};
20
21struct tc_action_ops;
22
23struct tc_action {
24 const struct tc_action_ops *ops;
ec0595cc
WC
25 __u32 type; /* for backward compat(TCA_OLD_COMPAT) */
26 __u32 order;
65a206c0 27 struct tcf_idrinfo *idrinfo;
a85a970a 28
ec0595cc 29 u32 tcfa_index;
036bb443
VB
30 refcount_t tcfa_refcnt;
31 atomic_t tcfa_bindcnt;
ec0595cc
WC
32 int tcfa_action;
33 struct tcf_t tcfa_tm;
34 struct gnet_stats_basic_packed tcfa_bstats;
28169aba 35 struct gnet_stats_basic_packed tcfa_bstats_hw;
ec0595cc 36 struct gnet_stats_queue tcfa_qstats;
1c0d32fd 37 struct net_rate_estimator __rcu *tcfa_rate_est;
ec0595cc 38 spinlock_t tcfa_lock;
519c818e 39 struct gnet_stats_basic_cpu __percpu *cpu_bstats;
28169aba 40 struct gnet_stats_basic_cpu __percpu *cpu_bstats_hw;
519c818e 41 struct gnet_stats_queue __percpu *cpu_qstats;
eec94fdb 42 struct tc_cookie __rcu *act_cookie;
ee3bbfe8 43 struct tcf_chain __rcu *goto_chain;
e9ce1cd3 44};
ec0595cc
WC
45#define tcf_index common.tcfa_index
46#define tcf_refcnt common.tcfa_refcnt
47#define tcf_bindcnt common.tcfa_bindcnt
ec0595cc
WC
48#define tcf_action common.tcfa_action
49#define tcf_tm common.tcfa_tm
50#define tcf_bstats common.tcfa_bstats
51#define tcf_qstats common.tcfa_qstats
52#define tcf_rate_est common.tcfa_rate_est
53#define tcf_lock common.tcfa_lock
e9ce1cd3 54
56e5d1ca
ED
55/* Update lastuse only if needed, to avoid dirtying a cache line.
56 * We use a temp variable to avoid fetching jiffies twice.
57 */
58static inline void tcf_lastuse_update(struct tcf_t *tm)
59{
60 unsigned long now = jiffies;
61
62 if (tm->lastuse != now)
63 tm->lastuse = now;
53eb440f
JHS
64 if (unlikely(!tm->firstuse))
65 tm->firstuse = now;
56e5d1ca
ED
66}
67
48d8ee16
JHS
68static inline void tcf_tm_dump(struct tcf_t *dtm, const struct tcf_t *stm)
69{
70 dtm->install = jiffies_to_clock_t(jiffies - stm->install);
71 dtm->lastuse = jiffies_to_clock_t(jiffies - stm->lastuse);
72 dtm->firstuse = jiffies_to_clock_t(jiffies - stm->firstuse);
73 dtm->expires = jiffies_to_clock_t(stm->expires);
74}
75
00175aec
AV
76#ifdef CONFIG_NET_CLS_ACT
77
78#define ACT_P_CREATED 1
79#define ACT_P_DELETED 1
80
4a5da47d
VB
81typedef void (*tc_action_priv_destructor)(void *priv);
82
e9ce1cd3 83struct tc_action_ops {
1f747c26 84 struct list_head head;
1da177e4 85 char kind[IFNAMSIZ];
eddd2cf1 86 enum tca_id id; /* identifier should match kind */
a85a970a 87 size_t size;
1da177e4 88 struct module *owner;
0b0f43fe 89 int (*act)(struct sk_buff *, const struct tc_action *,
7fd4b288 90 struct tcf_result *); /* called under RCU BH lock*/
e9ce1cd3 91 int (*dump)(struct sk_buff *, struct tc_action *, int, int);
9a63b255 92 void (*cleanup)(struct tc_action *);
f061b48c 93 int (*lookup)(struct net *net, struct tc_action **a, u32 index);
c1b52739 94 int (*init)(struct net *net, struct nlattr *nla,
a85a970a 95 struct nlattr *est, struct tc_action **act, int ovr,
85d0966f 96 int bind, bool rtnl_held, struct tcf_proto *tp,
789871bb 97 struct netlink_ext_ack *extack);
ddf97ccd 98 int (*walk)(struct net *, struct sk_buff *,
1af85155 99 struct netlink_callback *, int,
41780105
AA
100 const struct tc_action_ops *,
101 struct netlink_ext_ack *);
28169aba 102 void (*stats_update)(struct tc_action *, u64, u32, u64, bool);
a03b91b1 103 size_t (*get_fill_size)(const struct tc_action *act);
470d5060
VB
104 struct net_device *(*get_dev)(const struct tc_action *a,
105 tc_action_priv_destructor *destructor);
4a5da47d
VB
106 struct psample_group *
107 (*get_psample_group)(const struct tc_action *a,
108 tc_action_priv_destructor *destructor);
ddf97ccd
WC
109};
110
111struct tc_action_net {
65a206c0 112 struct tcf_idrinfo *idrinfo;
ddf97ccd 113 const struct tc_action_ops *ops;
1da177e4
LT
114};
115
ddf97ccd 116static inline
981471bd 117int tc_action_net_init(struct net *net, struct tc_action_net *tn,
c7e460ce 118 const struct tc_action_ops *ops)
ddf97ccd
WC
119{
120 int err = 0;
121
65a206c0
CM
122 tn->idrinfo = kmalloc(sizeof(*tn->idrinfo), GFP_KERNEL);
123 if (!tn->idrinfo)
ddf97ccd
WC
124 return -ENOMEM;
125 tn->ops = ops;
981471bd 126 tn->idrinfo->net = net;
95278dda 127 mutex_init(&tn->idrinfo->lock);
65a206c0 128 idr_init(&tn->idrinfo->action_idr);
ddf97ccd
WC
129 return err;
130}
131
65a206c0
CM
132void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
133 struct tcf_idrinfo *idrinfo);
ddf97ccd 134
039af9c6
CW
135static inline void tc_action_net_exit(struct list_head *net_list,
136 unsigned int id)
ddf97ccd 137{
039af9c6
CW
138 struct net *net;
139
a159d3c4 140 rtnl_lock();
039af9c6
CW
141 list_for_each_entry(net, net_list, exit_list) {
142 struct tc_action_net *tn = net_generic(net, id);
143
144 tcf_idrinfo_destroy(tn->ops, tn->idrinfo);
145 kfree(tn->idrinfo);
146 }
a159d3c4 147 rtnl_unlock();
ddf97ccd
WC
148}
149
150int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
151 struct netlink_callback *cb, int type,
b3620145
AA
152 const struct tc_action_ops *ops,
153 struct netlink_ext_ack *extack);
65a206c0 154int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index);
65a206c0
CM
155int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
156 struct tc_action **a, const struct tc_action_ops *ops,
157 int bind, bool cpustats);
65a206c0 158void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a);
e9ce1cd3 159
0190c1d4
VB
160void tcf_idr_cleanup(struct tc_action_net *tn, u32 index);
161int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index,
162 struct tc_action **a, int bind);
65a206c0 163int __tcf_idr_release(struct tc_action *a, bool bind, bool strict);
28e6b67f 164
65a206c0 165static inline int tcf_idr_release(struct tc_action *a, bool bind)
28e6b67f 166{
65a206c0 167 return __tcf_idr_release(a, bind, false);
28e6b67f
DB
168}
169
ddf97ccd 170int tcf_register_action(struct tc_action_ops *a, struct pernet_operations *ops);
0b0f43fe
JHS
171int tcf_unregister_action(struct tc_action_ops *a,
172 struct pernet_operations *ops);
90b73b77 173int tcf_action_destroy(struct tc_action *actions[], int bind);
22dc13c8
WC
174int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
175 int nr_actions, struct tcf_result *res);
9fb9f251
JP
176int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
177 struct nlattr *est, char *name, int ovr, int bind,
90b73b77 178 struct tc_action *actions[], size_t *attr_size,
789871bb 179 bool rtnl_held, struct netlink_ext_ack *extack);
9fb9f251
JP
180struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
181 struct nlattr *nla, struct nlattr *est,
aea0d727 182 char *name, int ovr, int bind,
789871bb 183 bool rtnl_held,
aea0d727 184 struct netlink_ext_ack *extack);
90b73b77
VB
185int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[], int bind,
186 int ref);
5c15257f
JP
187int tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int, int);
188int tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int, int);
5e1ad95b
VB
189
190static inline void tcf_action_update_bstats(struct tc_action *a,
191 struct sk_buff *skb)
192{
5e174d5e
VB
193 if (likely(a->cpu_bstats)) {
194 bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), skb);
195 return;
196 }
197 spin_lock(&a->tcfa_lock);
198 bstats_update(&a->tcfa_bstats, skb);
199 spin_unlock(&a->tcfa_lock);
26b537a8
VB
200}
201
202static inline void tcf_action_inc_drop_qstats(struct tc_action *a)
203{
5e174d5e
VB
204 if (likely(a->cpu_qstats)) {
205 qstats_drop_inc(this_cpu_ptr(a->cpu_qstats));
206 return;
207 }
208 spin_lock(&a->tcfa_lock);
209 qstats_drop_inc(&a->tcfa_qstats);
210 spin_unlock(&a->tcfa_lock);
26b537a8
VB
211}
212
213static inline void tcf_action_inc_overlimit_qstats(struct tc_action *a)
214{
5e174d5e
VB
215 if (likely(a->cpu_qstats)) {
216 qstats_overlimit_inc(this_cpu_ptr(a->cpu_qstats));
217 return;
218 }
219 spin_lock(&a->tcfa_lock);
220 qstats_overlimit_inc(&a->tcfa_qstats);
221 spin_unlock(&a->tcfa_lock);
26b537a8
VB
222}
223
c8ecebd0
VB
224void tcf_action_update_stats(struct tc_action *a, u64 bytes, u32 packets,
225 bool drop, bool hw);
5c15257f 226int tcf_action_copy_stats(struct sk_buff *, struct tc_action *, int);
00175aec 227
85d0966f
DC
228int tcf_action_check_ctrlact(int action, struct tcf_proto *tp,
229 struct tcf_chain **handle,
230 struct netlink_ext_ack *newchain);
231struct tcf_chain *tcf_action_set_ctrlact(struct tc_action *a, int action,
232 struct tcf_chain *newchain);
2734437e 233#endif /* CONFIG_NET_CLS_ACT */
86cb13e4 234
38040702 235static inline void tcf_action_stats_update(struct tc_action *a, u64 bytes,
28169aba 236 u64 packets, u64 lastuse, bool hw)
38040702 237{
2734437e 238#ifdef CONFIG_NET_CLS_ACT
38040702
AV
239 if (!a->ops->stats_update)
240 return;
241
28169aba 242 a->ops->stats_update(a, bytes, packets, lastuse, hw);
2734437e 243#endif
38040702
AV
244}
245
b3f55bdd 246
1da177e4 247#endif