1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2023 Isovalent */
7 #include <linux/bpf_mprog.h>
9 #include <net/sch_generic.h>
14 struct mini_Qdisc __rcu *miniq;
15 struct bpf_mprog_bundle bundle;
22 struct net_device *dev;
26 static inline void tcx_set_ingress(struct sk_buff *skb, bool ingress)
28 #ifdef CONFIG_NET_XGRESS
29 skb->tc_at_ingress = ingress;
33 #ifdef CONFIG_NET_XGRESS
34 static inline struct tcx_entry *tcx_entry(struct bpf_mprog_entry *entry)
36 struct bpf_mprog_bundle *bundle = entry->parent;
38 return container_of(bundle, struct tcx_entry, bundle);
41 static inline struct tcx_link *tcx_link(struct bpf_link *link)
43 return container_of(link, struct tcx_link, link);
46 static inline const struct tcx_link *tcx_link_const(const struct bpf_link *link)
48 return tcx_link((struct bpf_link *)link);
54 static inline void tcx_entry_sync(void)
56 /* bpf_mprog_entry got a/b swapped, therefore ensure that
57 * there are no inflight users on the old one anymore.
63 tcx_entry_update(struct net_device *dev, struct bpf_mprog_entry *entry,
68 rcu_assign_pointer(dev->tcx_ingress, entry);
70 rcu_assign_pointer(dev->tcx_egress, entry);
73 static inline struct bpf_mprog_entry *
74 tcx_entry_fetch(struct net_device *dev, bool ingress)
78 return rcu_dereference_rtnl(dev->tcx_ingress);
80 return rcu_dereference_rtnl(dev->tcx_egress);
83 static inline struct bpf_mprog_entry *tcx_entry_create(void)
85 struct tcx_entry *tcx = kzalloc(sizeof(*tcx), GFP_KERNEL);
88 bpf_mprog_bundle_init(&tcx->bundle);
89 return &tcx->bundle.a;
94 static inline void tcx_entry_free(struct bpf_mprog_entry *entry)
96 kfree_rcu(tcx_entry(entry), rcu);
99 static inline struct bpf_mprog_entry *
100 tcx_entry_fetch_or_create(struct net_device *dev, bool ingress, bool *created)
102 struct bpf_mprog_entry *entry = tcx_entry_fetch(dev, ingress);
106 entry = tcx_entry_create();
114 static inline void tcx_skeys_inc(bool ingress)
118 net_inc_ingress_queue();
120 net_inc_egress_queue();
123 static inline void tcx_skeys_dec(bool ingress)
126 net_dec_ingress_queue();
128 net_dec_egress_queue();
132 static inline void tcx_miniq_set_active(struct bpf_mprog_entry *entry,
136 tcx_entry(entry)->miniq_active = active;
139 static inline bool tcx_entry_is_active(struct bpf_mprog_entry *entry)
142 return bpf_mprog_total(entry) || tcx_entry(entry)->miniq_active;
145 static inline enum tcx_action_base tcx_action_code(struct sk_buff *skb,
150 skb->tc_index = qdisc_skb_cb(skb)->tc_classid;
160 #endif /* CONFIG_NET_XGRESS */
162 #if defined(CONFIG_NET_XGRESS) && defined(CONFIG_BPF_SYSCALL)
163 int tcx_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog);
164 int tcx_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
165 int tcx_prog_detach(const union bpf_attr *attr, struct bpf_prog *prog);
166 void tcx_uninstall(struct net_device *dev, bool ingress);
168 int tcx_prog_query(const union bpf_attr *attr,
169 union bpf_attr __user *uattr);
171 static inline void dev_tcx_uninstall(struct net_device *dev)
174 tcx_uninstall(dev, true);
175 tcx_uninstall(dev, false);
178 static inline int tcx_prog_attach(const union bpf_attr *attr,
179 struct bpf_prog *prog)
184 static inline int tcx_link_attach(const union bpf_attr *attr,
185 struct bpf_prog *prog)
190 static inline int tcx_prog_detach(const union bpf_attr *attr,
191 struct bpf_prog *prog)
196 static inline int tcx_prog_query(const union bpf_attr *attr,
197 union bpf_attr __user *uattr)
202 static inline void dev_tcx_uninstall(struct net_device *dev)
205 #endif /* CONFIG_NET_XGRESS && CONFIG_BPF_SYSCALL */
206 #endif /* __NET_TCX_H */