tcx.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright (c) 2023 Isovalent */
  3. #ifndef __NET_TCX_H
  4. #define __NET_TCX_H
  5. #include <linux/bpf.h>
  6. #include <linux/bpf_mprog.h>
  7. #include <net/sch_generic.h>
  8. struct mini_Qdisc;
  9. struct tcx_entry {
  10. struct mini_Qdisc __rcu *miniq;
  11. struct bpf_mprog_bundle bundle;
  12. u32 miniq_active;
  13. struct rcu_head rcu;
  14. };
  15. struct tcx_link {
  16. struct bpf_link link;
  17. struct net_device *dev;
  18. u32 location;
  19. };
  20. static inline void tcx_set_ingress(struct sk_buff *skb, bool ingress)
  21. {
  22. #ifdef CONFIG_NET_XGRESS
  23. skb->tc_at_ingress = ingress;
  24. #endif
  25. }
  26. #ifdef CONFIG_NET_XGRESS
  27. static inline struct tcx_entry *tcx_entry(struct bpf_mprog_entry *entry)
  28. {
  29. struct bpf_mprog_bundle *bundle = entry->parent;
  30. return container_of(bundle, struct tcx_entry, bundle);
  31. }
  32. static inline struct tcx_link *tcx_link(const struct bpf_link *link)
  33. {
  34. return container_of(link, struct tcx_link, link);
  35. }
  36. void tcx_inc(void);
  37. void tcx_dec(void);
  38. static inline void tcx_entry_sync(void)
  39. {
  40. /* bpf_mprog_entry got a/b swapped, therefore ensure that
  41. * there are no inflight users on the old one anymore.
  42. */
  43. synchronize_rcu();
  44. }
  45. static inline void
  46. tcx_entry_update(struct net_device *dev, struct bpf_mprog_entry *entry,
  47. bool ingress)
  48. {
  49. ASSERT_RTNL();
  50. if (ingress)
  51. rcu_assign_pointer(dev->tcx_ingress, entry);
  52. else
  53. rcu_assign_pointer(dev->tcx_egress, entry);
  54. }
  55. static inline struct bpf_mprog_entry *
  56. tcx_entry_fetch(struct net_device *dev, bool ingress)
  57. {
  58. ASSERT_RTNL();
  59. if (ingress)
  60. return rcu_dereference_rtnl(dev->tcx_ingress);
  61. else
  62. return rcu_dereference_rtnl(dev->tcx_egress);
  63. }
  64. static inline struct bpf_mprog_entry *tcx_entry_create_noprof(void)
  65. {
  66. struct tcx_entry *tcx = kzalloc_noprof(sizeof(*tcx), GFP_KERNEL);
  67. if (tcx) {
  68. bpf_mprog_bundle_init(&tcx->bundle);
  69. return &tcx->bundle.a;
  70. }
  71. return NULL;
  72. }
  73. #define tcx_entry_create(...) alloc_hooks(tcx_entry_create_noprof(__VA_ARGS__))
  74. static inline void tcx_entry_free(struct bpf_mprog_entry *entry)
  75. {
  76. kfree_rcu(tcx_entry(entry), rcu);
  77. }
  78. static inline struct bpf_mprog_entry *
  79. tcx_entry_fetch_or_create(struct net_device *dev, bool ingress, bool *created)
  80. {
  81. struct bpf_mprog_entry *entry = tcx_entry_fetch(dev, ingress);
  82. *created = false;
  83. if (!entry) {
  84. entry = tcx_entry_create();
  85. if (!entry)
  86. return NULL;
  87. *created = true;
  88. }
  89. return entry;
  90. }
  91. static inline void tcx_skeys_inc(bool ingress)
  92. {
  93. tcx_inc();
  94. if (ingress)
  95. net_inc_ingress_queue();
  96. else
  97. net_inc_egress_queue();
  98. }
  99. static inline void tcx_skeys_dec(bool ingress)
  100. {
  101. if (ingress)
  102. net_dec_ingress_queue();
  103. else
  104. net_dec_egress_queue();
  105. tcx_dec();
  106. }
  107. static inline void tcx_miniq_inc(struct bpf_mprog_entry *entry)
  108. {
  109. ASSERT_RTNL();
  110. tcx_entry(entry)->miniq_active++;
  111. }
  112. static inline void tcx_miniq_dec(struct bpf_mprog_entry *entry)
  113. {
  114. ASSERT_RTNL();
  115. tcx_entry(entry)->miniq_active--;
  116. }
  117. static inline bool tcx_entry_is_active(struct bpf_mprog_entry *entry)
  118. {
  119. ASSERT_RTNL();
  120. return bpf_mprog_total(entry) || tcx_entry(entry)->miniq_active;
  121. }
  122. static inline enum tcx_action_base tcx_action_code(struct sk_buff *skb,
  123. int code)
  124. {
  125. switch (code) {
  126. case TCX_PASS:
  127. skb->tc_index = qdisc_skb_cb(skb)->tc_classid;
  128. fallthrough;
  129. case TCX_DROP:
  130. case TCX_REDIRECT:
  131. return code;
  132. case TCX_NEXT:
  133. default:
  134. return TCX_NEXT;
  135. }
  136. }
  137. #endif /* CONFIG_NET_XGRESS */
  138. #if defined(CONFIG_NET_XGRESS) && defined(CONFIG_BPF_SYSCALL)
  139. int tcx_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog);
  140. int tcx_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
  141. int tcx_prog_detach(const union bpf_attr *attr, struct bpf_prog *prog);
  142. void tcx_uninstall(struct net_device *dev, bool ingress);
  143. int tcx_prog_query(const union bpf_attr *attr,
  144. union bpf_attr __user *uattr);
  145. static inline void dev_tcx_uninstall(struct net_device *dev)
  146. {
  147. ASSERT_RTNL();
  148. tcx_uninstall(dev, true);
  149. tcx_uninstall(dev, false);
  150. }
  151. #else
  152. static inline int tcx_prog_attach(const union bpf_attr *attr,
  153. struct bpf_prog *prog)
  154. {
  155. return -EINVAL;
  156. }
  157. static inline int tcx_link_attach(const union bpf_attr *attr,
  158. struct bpf_prog *prog)
  159. {
  160. return -EINVAL;
  161. }
  162. static inline int tcx_prog_detach(const union bpf_attr *attr,
  163. struct bpf_prog *prog)
  164. {
  165. return -EINVAL;
  166. }
  167. static inline int tcx_prog_query(const union bpf_attr *attr,
  168. union bpf_attr __user *uattr)
  169. {
  170. return -EINVAL;
  171. }
  172. static inline void dev_tcx_uninstall(struct net_device *dev)
  173. {
  174. }
  175. #endif /* CONFIG_NET_XGRESS && CONFIG_BPF_SYSCALL */
  176. #endif /* __NET_TCX_H */