bpf_tcp_ca.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2019 Facebook */
  3. #include <linux/init.h>
  4. #include <linux/types.h>
  5. #include <linux/bpf_verifier.h>
  6. #include <linux/bpf.h>
  7. #include <linux/btf.h>
  8. #include <linux/btf_ids.h>
  9. #include <linux/filter.h>
  10. #include <net/tcp.h>
  11. #include <net/bpf_sk_storage.h>
  12. /* "extern" is to avoid sparse warning. It is only used in bpf_struct_ops.c. */
  13. extern struct bpf_struct_ops bpf_tcp_congestion_ops;
  14. static u32 unsupported_ops[] = {
  15. offsetof(struct tcp_congestion_ops, get_info),
  16. };
  17. static const struct btf_type *tcp_sock_type;
  18. static u32 tcp_sock_id, sock_id;
  19. static int bpf_tcp_ca_init(struct btf *btf)
  20. {
  21. s32 type_id;
  22. type_id = btf_find_by_name_kind(btf, "sock", BTF_KIND_STRUCT);
  23. if (type_id < 0)
  24. return -EINVAL;
  25. sock_id = type_id;
  26. type_id = btf_find_by_name_kind(btf, "tcp_sock", BTF_KIND_STRUCT);
  27. if (type_id < 0)
  28. return -EINVAL;
  29. tcp_sock_id = type_id;
  30. tcp_sock_type = btf_type_by_id(btf, tcp_sock_id);
  31. return 0;
  32. }
  33. static bool is_unsupported(u32 member_offset)
  34. {
  35. unsigned int i;
  36. for (i = 0; i < ARRAY_SIZE(unsupported_ops); i++) {
  37. if (member_offset == unsupported_ops[i])
  38. return true;
  39. }
  40. return false;
  41. }
  42. extern struct btf *btf_vmlinux;
  43. static bool bpf_tcp_ca_is_valid_access(int off, int size,
  44. enum bpf_access_type type,
  45. const struct bpf_prog *prog,
  46. struct bpf_insn_access_aux *info)
  47. {
  48. if (!bpf_tracing_btf_ctx_access(off, size, type, prog, info))
  49. return false;
  50. if (info->reg_type == PTR_TO_BTF_ID && info->btf_id == sock_id)
  51. /* promote it to tcp_sock */
  52. info->btf_id = tcp_sock_id;
  53. return true;
  54. }
  55. static int bpf_tcp_ca_btf_struct_access(struct bpf_verifier_log *log,
  56. const struct btf *btf,
  57. const struct btf_type *t, int off,
  58. int size, enum bpf_access_type atype,
  59. u32 *next_btf_id,
  60. enum bpf_type_flag *flag)
  61. {
  62. size_t end;
  63. if (atype == BPF_READ)
  64. return btf_struct_access(log, btf, t, off, size, atype, next_btf_id,
  65. flag);
  66. if (t != tcp_sock_type) {
  67. bpf_log(log, "only read is supported\n");
  68. return -EACCES;
  69. }
  70. switch (off) {
  71. case offsetof(struct sock, sk_pacing_rate):
  72. end = offsetofend(struct sock, sk_pacing_rate);
  73. break;
  74. case offsetof(struct sock, sk_pacing_status):
  75. end = offsetofend(struct sock, sk_pacing_status);
  76. break;
  77. case bpf_ctx_range(struct inet_connection_sock, icsk_ca_priv):
  78. end = offsetofend(struct inet_connection_sock, icsk_ca_priv);
  79. break;
  80. case offsetof(struct inet_connection_sock, icsk_ack.pending):
  81. end = offsetofend(struct inet_connection_sock,
  82. icsk_ack.pending);
  83. break;
  84. case offsetof(struct tcp_sock, snd_cwnd):
  85. end = offsetofend(struct tcp_sock, snd_cwnd);
  86. break;
  87. case offsetof(struct tcp_sock, snd_cwnd_cnt):
  88. end = offsetofend(struct tcp_sock, snd_cwnd_cnt);
  89. break;
  90. case offsetof(struct tcp_sock, snd_ssthresh):
  91. end = offsetofend(struct tcp_sock, snd_ssthresh);
  92. break;
  93. case offsetof(struct tcp_sock, ecn_flags):
  94. end = offsetofend(struct tcp_sock, ecn_flags);
  95. break;
  96. default:
  97. bpf_log(log, "no write support to tcp_sock at off %d\n", off);
  98. return -EACCES;
  99. }
  100. if (off + size > end) {
  101. bpf_log(log,
  102. "write access at off %d with size %d beyond the member of tcp_sock ended at %zu\n",
  103. off, size, end);
  104. return -EACCES;
  105. }
  106. return 0;
  107. }
  108. BPF_CALL_2(bpf_tcp_send_ack, struct tcp_sock *, tp, u32, rcv_nxt)
  109. {
  110. /* bpf_tcp_ca prog cannot have NULL tp */
  111. __tcp_send_ack((struct sock *)tp, rcv_nxt);
  112. return 0;
  113. }
  114. static const struct bpf_func_proto bpf_tcp_send_ack_proto = {
  115. .func = bpf_tcp_send_ack,
  116. .gpl_only = false,
  117. /* In case we want to report error later */
  118. .ret_type = RET_INTEGER,
  119. .arg1_type = ARG_PTR_TO_BTF_ID,
  120. .arg1_btf_id = &tcp_sock_id,
  121. .arg2_type = ARG_ANYTHING,
  122. };
  123. static u32 prog_ops_moff(const struct bpf_prog *prog)
  124. {
  125. const struct btf_member *m;
  126. const struct btf_type *t;
  127. u32 midx;
  128. midx = prog->expected_attach_type;
  129. t = bpf_tcp_congestion_ops.type;
  130. m = &btf_type_member(t)[midx];
  131. return __btf_member_bit_offset(t, m) / 8;
  132. }
  133. static const struct bpf_func_proto *
  134. bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id,
  135. const struct bpf_prog *prog)
  136. {
  137. switch (func_id) {
  138. case BPF_FUNC_tcp_send_ack:
  139. return &bpf_tcp_send_ack_proto;
  140. case BPF_FUNC_sk_storage_get:
  141. return &bpf_sk_storage_get_proto;
  142. case BPF_FUNC_sk_storage_delete:
  143. return &bpf_sk_storage_delete_proto;
  144. case BPF_FUNC_setsockopt:
  145. /* Does not allow release() to call setsockopt.
  146. * release() is called when the current bpf-tcp-cc
  147. * is retiring. It is not allowed to call
  148. * setsockopt() to make further changes which
  149. * may potentially allocate new resources.
  150. */
  151. if (prog_ops_moff(prog) !=
  152. offsetof(struct tcp_congestion_ops, release))
  153. return &bpf_sk_setsockopt_proto;
  154. return NULL;
  155. case BPF_FUNC_getsockopt:
  156. /* Since get/setsockopt is usually expected to
  157. * be available together, disable getsockopt for
  158. * release also to avoid usage surprise.
  159. * The bpf-tcp-cc already has a more powerful way
  160. * to read tcp_sock from the PTR_TO_BTF_ID.
  161. */
  162. if (prog_ops_moff(prog) !=
  163. offsetof(struct tcp_congestion_ops, release))
  164. return &bpf_sk_getsockopt_proto;
  165. return NULL;
  166. case BPF_FUNC_ktime_get_coarse_ns:
  167. return &bpf_ktime_get_coarse_ns_proto;
  168. default:
  169. return bpf_base_func_proto(func_id);
  170. }
  171. }
  172. BTF_SET8_START(bpf_tcp_ca_check_kfunc_ids)
  173. BTF_ID_FLAGS(func, tcp_reno_ssthresh)
  174. BTF_ID_FLAGS(func, tcp_reno_cong_avoid)
  175. BTF_ID_FLAGS(func, tcp_reno_undo_cwnd)
  176. BTF_ID_FLAGS(func, tcp_slow_start)
  177. BTF_ID_FLAGS(func, tcp_cong_avoid_ai)
  178. BTF_SET8_END(bpf_tcp_ca_check_kfunc_ids)
  179. static const struct btf_kfunc_id_set bpf_tcp_ca_kfunc_set = {
  180. .owner = THIS_MODULE,
  181. .set = &bpf_tcp_ca_check_kfunc_ids,
  182. };
  183. static const struct bpf_verifier_ops bpf_tcp_ca_verifier_ops = {
  184. .get_func_proto = bpf_tcp_ca_get_func_proto,
  185. .is_valid_access = bpf_tcp_ca_is_valid_access,
  186. .btf_struct_access = bpf_tcp_ca_btf_struct_access,
  187. };
  188. static int bpf_tcp_ca_init_member(const struct btf_type *t,
  189. const struct btf_member *member,
  190. void *kdata, const void *udata)
  191. {
  192. const struct tcp_congestion_ops *utcp_ca;
  193. struct tcp_congestion_ops *tcp_ca;
  194. u32 moff;
  195. utcp_ca = (const struct tcp_congestion_ops *)udata;
  196. tcp_ca = (struct tcp_congestion_ops *)kdata;
  197. moff = __btf_member_bit_offset(t, member) / 8;
  198. switch (moff) {
  199. case offsetof(struct tcp_congestion_ops, flags):
  200. if (utcp_ca->flags & ~TCP_CONG_MASK)
  201. return -EINVAL;
  202. tcp_ca->flags = utcp_ca->flags;
  203. return 1;
  204. case offsetof(struct tcp_congestion_ops, name):
  205. if (bpf_obj_name_cpy(tcp_ca->name, utcp_ca->name,
  206. sizeof(tcp_ca->name)) <= 0)
  207. return -EINVAL;
  208. if (tcp_ca_find(utcp_ca->name))
  209. return -EEXIST;
  210. return 1;
  211. }
  212. return 0;
  213. }
  214. static int bpf_tcp_ca_check_member(const struct btf_type *t,
  215. const struct btf_member *member)
  216. {
  217. if (is_unsupported(__btf_member_bit_offset(t, member) / 8))
  218. return -ENOTSUPP;
  219. return 0;
  220. }
  221. static int bpf_tcp_ca_reg(void *kdata)
  222. {
  223. return tcp_register_congestion_control(kdata);
  224. }
  225. static void bpf_tcp_ca_unreg(void *kdata)
  226. {
  227. tcp_unregister_congestion_control(kdata);
  228. }
  229. struct bpf_struct_ops bpf_tcp_congestion_ops = {
  230. .verifier_ops = &bpf_tcp_ca_verifier_ops,
  231. .reg = bpf_tcp_ca_reg,
  232. .unreg = bpf_tcp_ca_unreg,
  233. .check_member = bpf_tcp_ca_check_member,
  234. .init_member = bpf_tcp_ca_init_member,
  235. .init = bpf_tcp_ca_init,
  236. .name = "tcp_congestion_ops",
  237. };
  238. static int __init bpf_tcp_ca_kfunc_init(void)
  239. {
  240. return register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &bpf_tcp_ca_kfunc_set);
  241. }
  242. late_initcall(bpf_tcp_ca_kfunc_init);