bpf_tcp_ca.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. static struct bpf_struct_ops bpf_tcp_congestion_ops;
  14. static const struct btf_type *tcp_sock_type;
  15. static u32 tcp_sock_id, sock_id;
  16. static const struct btf_type *tcp_congestion_ops_type;
  17. static int bpf_tcp_ca_init(struct btf *btf)
  18. {
  19. s32 type_id;
  20. type_id = btf_find_by_name_kind(btf, "sock", BTF_KIND_STRUCT);
  21. if (type_id < 0)
  22. return -EINVAL;
  23. sock_id = type_id;
  24. type_id = btf_find_by_name_kind(btf, "tcp_sock", BTF_KIND_STRUCT);
  25. if (type_id < 0)
  26. return -EINVAL;
  27. tcp_sock_id = type_id;
  28. tcp_sock_type = btf_type_by_id(btf, tcp_sock_id);
  29. type_id = btf_find_by_name_kind(btf, "tcp_congestion_ops", BTF_KIND_STRUCT);
  30. if (type_id < 0)
  31. return -EINVAL;
  32. tcp_congestion_ops_type = btf_type_by_id(btf, type_id);
  33. return 0;
  34. }
  35. static bool bpf_tcp_ca_is_valid_access(int off, int size,
  36. enum bpf_access_type type,
  37. const struct bpf_prog *prog,
  38. struct bpf_insn_access_aux *info)
  39. {
  40. if (!bpf_tracing_btf_ctx_access(off, size, type, prog, info))
  41. return false;
  42. if (base_type(info->reg_type) == PTR_TO_BTF_ID &&
  43. !bpf_type_has_unsafe_modifiers(info->reg_type) &&
  44. info->btf_id == sock_id)
  45. /* promote it to tcp_sock */
  46. info->btf_id = tcp_sock_id;
  47. return true;
  48. }
  49. static int bpf_tcp_ca_btf_struct_access(struct bpf_verifier_log *log,
  50. const struct bpf_reg_state *reg,
  51. int off, int size)
  52. {
  53. const struct btf_type *t;
  54. size_t end;
  55. t = btf_type_by_id(reg->btf, reg->btf_id);
  56. if (t != tcp_sock_type) {
  57. bpf_log(log, "only read is supported\n");
  58. return -EACCES;
  59. }
  60. switch (off) {
  61. case offsetof(struct sock, sk_pacing_rate):
  62. end = offsetofend(struct sock, sk_pacing_rate);
  63. break;
  64. case offsetof(struct sock, sk_pacing_status):
  65. end = offsetofend(struct sock, sk_pacing_status);
  66. break;
  67. case bpf_ctx_range(struct inet_connection_sock, icsk_ca_priv):
  68. end = offsetofend(struct inet_connection_sock, icsk_ca_priv);
  69. break;
  70. case offsetof(struct inet_connection_sock, icsk_ack.pending):
  71. end = offsetofend(struct inet_connection_sock,
  72. icsk_ack.pending);
  73. break;
  74. case offsetof(struct tcp_sock, snd_cwnd):
  75. end = offsetofend(struct tcp_sock, snd_cwnd);
  76. break;
  77. case offsetof(struct tcp_sock, snd_cwnd_cnt):
  78. end = offsetofend(struct tcp_sock, snd_cwnd_cnt);
  79. break;
  80. case offsetof(struct tcp_sock, snd_cwnd_stamp):
  81. end = offsetofend(struct tcp_sock, snd_cwnd_stamp);
  82. break;
  83. case offsetof(struct tcp_sock, snd_ssthresh):
  84. end = offsetofend(struct tcp_sock, snd_ssthresh);
  85. break;
  86. case offsetof(struct tcp_sock, ecn_flags):
  87. end = offsetofend(struct tcp_sock, ecn_flags);
  88. break;
  89. case offsetof(struct tcp_sock, app_limited):
  90. end = offsetofend(struct tcp_sock, app_limited);
  91. break;
  92. default:
  93. bpf_log(log, "no write support to tcp_sock at off %d\n", off);
  94. return -EACCES;
  95. }
  96. if (off + size > end) {
  97. bpf_log(log,
  98. "write access at off %d with size %d beyond the member of tcp_sock ended at %zu\n",
  99. off, size, end);
  100. return -EACCES;
  101. }
  102. return 0;
  103. }
  104. BPF_CALL_2(bpf_tcp_send_ack, struct tcp_sock *, tp, u32, rcv_nxt)
  105. {
  106. /* bpf_tcp_ca prog cannot have NULL tp */
  107. __tcp_send_ack((struct sock *)tp, rcv_nxt);
  108. return 0;
  109. }
  110. static const struct bpf_func_proto bpf_tcp_send_ack_proto = {
  111. .func = bpf_tcp_send_ack,
  112. .gpl_only = false,
  113. /* In case we want to report error later */
  114. .ret_type = RET_INTEGER,
  115. .arg1_type = ARG_PTR_TO_BTF_ID,
  116. .arg1_btf_id = &tcp_sock_id,
  117. .arg2_type = ARG_ANYTHING,
  118. };
  119. static u32 prog_ops_moff(const struct bpf_prog *prog)
  120. {
  121. const struct btf_member *m;
  122. const struct btf_type *t;
  123. u32 midx;
  124. midx = prog->expected_attach_type;
  125. t = tcp_congestion_ops_type;
  126. m = &btf_type_member(t)[midx];
  127. return __btf_member_bit_offset(t, m) / 8;
  128. }
  129. static const struct bpf_func_proto *
  130. bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id,
  131. const struct bpf_prog *prog)
  132. {
  133. switch (func_id) {
  134. case BPF_FUNC_tcp_send_ack:
  135. return &bpf_tcp_send_ack_proto;
  136. case BPF_FUNC_sk_storage_get:
  137. return &bpf_sk_storage_get_proto;
  138. case BPF_FUNC_sk_storage_delete:
  139. return &bpf_sk_storage_delete_proto;
  140. case BPF_FUNC_setsockopt:
  141. /* Does not allow release() to call setsockopt.
  142. * release() is called when the current bpf-tcp-cc
  143. * is retiring. It is not allowed to call
  144. * setsockopt() to make further changes which
  145. * may potentially allocate new resources.
  146. */
  147. if (prog_ops_moff(prog) !=
  148. offsetof(struct tcp_congestion_ops, release))
  149. return &bpf_sk_setsockopt_proto;
  150. return NULL;
  151. case BPF_FUNC_getsockopt:
  152. /* Since get/setsockopt is usually expected to
  153. * be available together, disable getsockopt for
  154. * release also to avoid usage surprise.
  155. * The bpf-tcp-cc already has a more powerful way
  156. * to read tcp_sock from the PTR_TO_BTF_ID.
  157. */
  158. if (prog_ops_moff(prog) !=
  159. offsetof(struct tcp_congestion_ops, release))
  160. return &bpf_sk_getsockopt_proto;
  161. return NULL;
  162. case BPF_FUNC_ktime_get_coarse_ns:
  163. return &bpf_ktime_get_coarse_ns_proto;
  164. default:
  165. return bpf_base_func_proto(func_id, prog);
  166. }
  167. }
  168. BTF_KFUNCS_START(bpf_tcp_ca_check_kfunc_ids)
  169. BTF_ID_FLAGS(func, tcp_reno_ssthresh)
  170. BTF_ID_FLAGS(func, tcp_reno_cong_avoid)
  171. BTF_ID_FLAGS(func, tcp_reno_undo_cwnd)
  172. BTF_ID_FLAGS(func, tcp_slow_start)
  173. BTF_ID_FLAGS(func, tcp_cong_avoid_ai)
  174. BTF_KFUNCS_END(bpf_tcp_ca_check_kfunc_ids)
  175. static const struct btf_kfunc_id_set bpf_tcp_ca_kfunc_set = {
  176. .owner = THIS_MODULE,
  177. .set = &bpf_tcp_ca_check_kfunc_ids,
  178. };
  179. static const struct bpf_verifier_ops bpf_tcp_ca_verifier_ops = {
  180. .get_func_proto = bpf_tcp_ca_get_func_proto,
  181. .is_valid_access = bpf_tcp_ca_is_valid_access,
  182. .btf_struct_access = bpf_tcp_ca_btf_struct_access,
  183. };
  184. static int bpf_tcp_ca_init_member(const struct btf_type *t,
  185. const struct btf_member *member,
  186. void *kdata, const void *udata)
  187. {
  188. const struct tcp_congestion_ops *utcp_ca;
  189. struct tcp_congestion_ops *tcp_ca;
  190. u32 moff;
  191. utcp_ca = (const struct tcp_congestion_ops *)udata;
  192. tcp_ca = (struct tcp_congestion_ops *)kdata;
  193. moff = __btf_member_bit_offset(t, member) / 8;
  194. switch (moff) {
  195. case offsetof(struct tcp_congestion_ops, flags):
  196. if (utcp_ca->flags & ~TCP_CONG_MASK)
  197. return -EINVAL;
  198. tcp_ca->flags = utcp_ca->flags;
  199. return 1;
  200. case offsetof(struct tcp_congestion_ops, name):
  201. if (bpf_obj_name_cpy(tcp_ca->name, utcp_ca->name,
  202. sizeof(tcp_ca->name)) <= 0)
  203. return -EINVAL;
  204. return 1;
  205. }
  206. return 0;
  207. }
  208. static int bpf_tcp_ca_reg(void *kdata, struct bpf_link *link)
  209. {
  210. return tcp_register_congestion_control(kdata);
  211. }
  212. static void bpf_tcp_ca_unreg(void *kdata, struct bpf_link *link)
  213. {
  214. tcp_unregister_congestion_control(kdata);
  215. }
  216. static int bpf_tcp_ca_update(void *kdata, void *old_kdata, struct bpf_link *link)
  217. {
  218. return tcp_update_congestion_control(kdata, old_kdata);
  219. }
  220. static int bpf_tcp_ca_validate(void *kdata)
  221. {
  222. return tcp_validate_congestion_control(kdata);
  223. }
  224. static u32 bpf_tcp_ca_ssthresh(struct sock *sk)
  225. {
  226. return 0;
  227. }
  228. static void bpf_tcp_ca_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  229. {
  230. }
  231. static void bpf_tcp_ca_set_state(struct sock *sk, u8 new_state)
  232. {
  233. }
  234. static void bpf_tcp_ca_cwnd_event(struct sock *sk, enum tcp_ca_event ev)
  235. {
  236. }
  237. static void bpf_tcp_ca_in_ack_event(struct sock *sk, u32 flags)
  238. {
  239. }
  240. static void bpf_tcp_ca_pkts_acked(struct sock *sk, const struct ack_sample *sample)
  241. {
  242. }
  243. static u32 bpf_tcp_ca_min_tso_segs(struct sock *sk)
  244. {
  245. return 0;
  246. }
  247. static void bpf_tcp_ca_cong_control(struct sock *sk, u32 ack, int flag,
  248. const struct rate_sample *rs)
  249. {
  250. }
  251. static u32 bpf_tcp_ca_undo_cwnd(struct sock *sk)
  252. {
  253. return 0;
  254. }
  255. static u32 bpf_tcp_ca_sndbuf_expand(struct sock *sk)
  256. {
  257. return 0;
  258. }
  259. static void __bpf_tcp_ca_init(struct sock *sk)
  260. {
  261. }
  262. static void __bpf_tcp_ca_release(struct sock *sk)
  263. {
  264. }
  265. static struct tcp_congestion_ops __bpf_ops_tcp_congestion_ops = {
  266. .ssthresh = bpf_tcp_ca_ssthresh,
  267. .cong_avoid = bpf_tcp_ca_cong_avoid,
  268. .set_state = bpf_tcp_ca_set_state,
  269. .cwnd_event = bpf_tcp_ca_cwnd_event,
  270. .in_ack_event = bpf_tcp_ca_in_ack_event,
  271. .pkts_acked = bpf_tcp_ca_pkts_acked,
  272. .min_tso_segs = bpf_tcp_ca_min_tso_segs,
  273. .cong_control = bpf_tcp_ca_cong_control,
  274. .undo_cwnd = bpf_tcp_ca_undo_cwnd,
  275. .sndbuf_expand = bpf_tcp_ca_sndbuf_expand,
  276. .init = __bpf_tcp_ca_init,
  277. .release = __bpf_tcp_ca_release,
  278. };
  279. static struct bpf_struct_ops bpf_tcp_congestion_ops = {
  280. .verifier_ops = &bpf_tcp_ca_verifier_ops,
  281. .reg = bpf_tcp_ca_reg,
  282. .unreg = bpf_tcp_ca_unreg,
  283. .update = bpf_tcp_ca_update,
  284. .init_member = bpf_tcp_ca_init_member,
  285. .init = bpf_tcp_ca_init,
  286. .validate = bpf_tcp_ca_validate,
  287. .name = "tcp_congestion_ops",
  288. .cfi_stubs = &__bpf_ops_tcp_congestion_ops,
  289. .owner = THIS_MODULE,
  290. };
  291. static int __init bpf_tcp_ca_kfunc_init(void)
  292. {
  293. int ret;
  294. ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &bpf_tcp_ca_kfunc_set);
  295. ret = ret ?: register_bpf_struct_ops(&bpf_tcp_congestion_ops, tcp_congestion_ops);
  296. return ret;
  297. }
  298. late_initcall(bpf_tcp_ca_kfunc_init);