smc_hs_bpf.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Shared Memory Communications over RDMA (SMC-R) and RoCE
  4. *
  5. * Generic hook for SMC handshake flow.
  6. *
  7. * Copyright IBM Corp. 2016
  8. * Copyright (c) 2025, Alibaba Inc.
  9. *
  10. * Author: D. Wythe <alibuda@linux.alibaba.com>
  11. */
  12. #include <linux/bpf_verifier.h>
  13. #include <linux/bpf.h>
  14. #include <linux/btf.h>
  15. #include <linux/rculist.h>
  16. #include "smc_hs_bpf.h"
  17. static DEFINE_SPINLOCK(smc_hs_ctrl_list_lock);
  18. static LIST_HEAD(smc_hs_ctrl_list);
  19. static int smc_hs_ctrl_reg(struct smc_hs_ctrl *ctrl)
  20. {
  21. int ret = 0;
  22. spin_lock(&smc_hs_ctrl_list_lock);
  23. /* already exist or duplicate name */
  24. if (smc_hs_ctrl_find_by_name(ctrl->name))
  25. ret = -EEXIST;
  26. else
  27. list_add_tail_rcu(&ctrl->list, &smc_hs_ctrl_list);
  28. spin_unlock(&smc_hs_ctrl_list_lock);
  29. return ret;
  30. }
  31. static void smc_hs_ctrl_unreg(struct smc_hs_ctrl *ctrl)
  32. {
  33. spin_lock(&smc_hs_ctrl_list_lock);
  34. list_del_rcu(&ctrl->list);
  35. spin_unlock(&smc_hs_ctrl_list_lock);
  36. /* Ensure that all readers to complete */
  37. synchronize_rcu();
  38. }
  39. struct smc_hs_ctrl *smc_hs_ctrl_find_by_name(const char *name)
  40. {
  41. struct smc_hs_ctrl *ctrl;
  42. list_for_each_entry_rcu(ctrl, &smc_hs_ctrl_list, list) {
  43. if (strcmp(ctrl->name, name) == 0)
  44. return ctrl;
  45. }
  46. return NULL;
  47. }
  48. static int __smc_bpf_stub_set_tcp_option(struct tcp_sock *tp) { return 1; }
  49. static int __smc_bpf_stub_set_tcp_option_cond(const struct tcp_sock *tp,
  50. struct inet_request_sock *ireq)
  51. {
  52. return 1;
  53. }
  54. static struct smc_hs_ctrl __smc_bpf_hs_ctrl = {
  55. .syn_option = __smc_bpf_stub_set_tcp_option,
  56. .synack_option = __smc_bpf_stub_set_tcp_option_cond,
  57. };
  58. static int smc_bpf_hs_ctrl_init(struct btf *btf) { return 0; }
  59. static int smc_bpf_hs_ctrl_reg(void *kdata, struct bpf_link *link)
  60. {
  61. if (link)
  62. return -EOPNOTSUPP;
  63. return smc_hs_ctrl_reg(kdata);
  64. }
  65. static void smc_bpf_hs_ctrl_unreg(void *kdata, struct bpf_link *link)
  66. {
  67. smc_hs_ctrl_unreg(kdata);
  68. }
  69. static int smc_bpf_hs_ctrl_init_member(const struct btf_type *t,
  70. const struct btf_member *member,
  71. void *kdata, const void *udata)
  72. {
  73. const struct smc_hs_ctrl *u_ctrl;
  74. struct smc_hs_ctrl *k_ctrl;
  75. u32 moff;
  76. u_ctrl = (const struct smc_hs_ctrl *)udata;
  77. k_ctrl = (struct smc_hs_ctrl *)kdata;
  78. moff = __btf_member_bit_offset(t, member) / 8;
  79. switch (moff) {
  80. case offsetof(struct smc_hs_ctrl, name):
  81. if (bpf_obj_name_cpy(k_ctrl->name, u_ctrl->name,
  82. sizeof(u_ctrl->name)) <= 0)
  83. return -EINVAL;
  84. return 1;
  85. case offsetof(struct smc_hs_ctrl, flags):
  86. if (u_ctrl->flags & ~SMC_HS_CTRL_ALL_FLAGS)
  87. return -EINVAL;
  88. k_ctrl->flags = u_ctrl->flags;
  89. return 1;
  90. default:
  91. break;
  92. }
  93. return 0;
  94. }
  95. static const struct bpf_func_proto *
  96. bpf_smc_hs_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  97. {
  98. return bpf_base_func_proto(func_id, prog);
  99. }
  100. static const struct bpf_verifier_ops smc_bpf_verifier_ops = {
  101. .get_func_proto = bpf_smc_hs_func_proto,
  102. .is_valid_access = bpf_tracing_btf_ctx_access,
  103. };
  104. static struct bpf_struct_ops bpf_smc_hs_ctrl_ops = {
  105. .name = "smc_hs_ctrl",
  106. .init = smc_bpf_hs_ctrl_init,
  107. .reg = smc_bpf_hs_ctrl_reg,
  108. .unreg = smc_bpf_hs_ctrl_unreg,
  109. .cfi_stubs = &__smc_bpf_hs_ctrl,
  110. .verifier_ops = &smc_bpf_verifier_ops,
  111. .init_member = smc_bpf_hs_ctrl_init_member,
  112. .owner = THIS_MODULE,
  113. };
  114. int bpf_smc_hs_ctrl_init(void)
  115. {
  116. return register_bpf_struct_ops(&bpf_smc_hs_ctrl_ops, smc_hs_ctrl);
  117. }