xskmap.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* XSKMAP used for AF_XDP sockets
  3. * Copyright(c) 2018 Intel Corporation.
  4. */
  5. #include <linux/bpf.h>
  6. #include <linux/filter.h>
  7. #include <linux/capability.h>
  8. #include <net/xdp_sock.h>
  9. #include <linux/slab.h>
  10. #include <linux/sched.h>
  11. #include <linux/btf_ids.h>
  12. #include "xsk.h"
  13. static struct xsk_map_node *xsk_map_node_alloc(struct xsk_map *map,
  14. struct xdp_sock __rcu **map_entry)
  15. {
  16. struct xsk_map_node *node;
  17. node = bpf_map_kzalloc(&map->map, sizeof(*node),
  18. GFP_ATOMIC | __GFP_NOWARN);
  19. if (!node)
  20. return ERR_PTR(-ENOMEM);
  21. bpf_map_inc(&map->map);
  22. node->map = map;
  23. node->map_entry = map_entry;
  24. return node;
  25. }
  26. static void xsk_map_node_free(struct xsk_map_node *node)
  27. {
  28. bpf_map_put(&node->map->map);
  29. kfree(node);
  30. }
  31. static void xsk_map_sock_add(struct xdp_sock *xs, struct xsk_map_node *node)
  32. {
  33. spin_lock_bh(&xs->map_list_lock);
  34. list_add_tail(&node->node, &xs->map_list);
  35. spin_unlock_bh(&xs->map_list_lock);
  36. }
  37. static void xsk_map_sock_delete(struct xdp_sock *xs,
  38. struct xdp_sock __rcu **map_entry)
  39. {
  40. struct xsk_map_node *n, *tmp;
  41. spin_lock_bh(&xs->map_list_lock);
  42. list_for_each_entry_safe(n, tmp, &xs->map_list, node) {
  43. if (map_entry == n->map_entry) {
  44. list_del(&n->node);
  45. xsk_map_node_free(n);
  46. }
  47. }
  48. spin_unlock_bh(&xs->map_list_lock);
  49. }
  50. static struct bpf_map *xsk_map_alloc(union bpf_attr *attr)
  51. {
  52. struct xsk_map *m;
  53. int numa_node;
  54. u64 size;
  55. if (!capable(CAP_NET_ADMIN))
  56. return ERR_PTR(-EPERM);
  57. if (attr->max_entries == 0 || attr->key_size != 4 ||
  58. attr->value_size != 4 ||
  59. attr->map_flags & ~(BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY))
  60. return ERR_PTR(-EINVAL);
  61. numa_node = bpf_map_attr_numa_node(attr);
  62. size = struct_size(m, xsk_map, attr->max_entries);
  63. m = bpf_map_area_alloc(size, numa_node);
  64. if (!m)
  65. return ERR_PTR(-ENOMEM);
  66. bpf_map_init_from_attr(&m->map, attr);
  67. spin_lock_init(&m->lock);
  68. return &m->map;
  69. }
  70. static void xsk_map_free(struct bpf_map *map)
  71. {
  72. struct xsk_map *m = container_of(map, struct xsk_map, map);
  73. synchronize_net();
  74. bpf_map_area_free(m);
  75. }
  76. static int xsk_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
  77. {
  78. struct xsk_map *m = container_of(map, struct xsk_map, map);
  79. u32 index = key ? *(u32 *)key : U32_MAX;
  80. u32 *next = next_key;
  81. if (index >= m->map.max_entries) {
  82. *next = 0;
  83. return 0;
  84. }
  85. if (index == m->map.max_entries - 1)
  86. return -ENOENT;
  87. *next = index + 1;
  88. return 0;
  89. }
  90. static int xsk_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
  91. {
  92. const int ret = BPF_REG_0, mp = BPF_REG_1, index = BPF_REG_2;
  93. struct bpf_insn *insn = insn_buf;
  94. *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
  95. *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 5);
  96. *insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(sizeof(struct xsk_sock *)));
  97. *insn++ = BPF_ALU64_IMM(BPF_ADD, mp, offsetof(struct xsk_map, xsk_map));
  98. *insn++ = BPF_ALU64_REG(BPF_ADD, ret, mp);
  99. *insn++ = BPF_LDX_MEM(BPF_SIZEOF(struct xsk_sock *), ret, ret, 0);
  100. *insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
  101. *insn++ = BPF_MOV64_IMM(ret, 0);
  102. return insn - insn_buf;
  103. }
  104. /* Elements are kept alive by RCU; either by rcu_read_lock() (from syscall) or
  105. * by local_bh_disable() (from XDP calls inside NAPI). The
  106. * rcu_read_lock_bh_held() below makes lockdep accept both.
  107. */
  108. static void *__xsk_map_lookup_elem(struct bpf_map *map, u32 key)
  109. {
  110. struct xsk_map *m = container_of(map, struct xsk_map, map);
  111. if (key >= map->max_entries)
  112. return NULL;
  113. return rcu_dereference_check(m->xsk_map[key], rcu_read_lock_bh_held());
  114. }
  115. static void *xsk_map_lookup_elem(struct bpf_map *map, void *key)
  116. {
  117. return __xsk_map_lookup_elem(map, *(u32 *)key);
  118. }
  119. static void *xsk_map_lookup_elem_sys_only(struct bpf_map *map, void *key)
  120. {
  121. return ERR_PTR(-EOPNOTSUPP);
  122. }
  123. static int xsk_map_update_elem(struct bpf_map *map, void *key, void *value,
  124. u64 map_flags)
  125. {
  126. struct xsk_map *m = container_of(map, struct xsk_map, map);
  127. struct xdp_sock __rcu **map_entry;
  128. struct xdp_sock *xs, *old_xs;
  129. u32 i = *(u32 *)key, fd = *(u32 *)value;
  130. struct xsk_map_node *node;
  131. struct socket *sock;
  132. int err;
  133. if (unlikely(map_flags > BPF_EXIST))
  134. return -EINVAL;
  135. if (unlikely(i >= m->map.max_entries))
  136. return -E2BIG;
  137. sock = sockfd_lookup(fd, &err);
  138. if (!sock)
  139. return err;
  140. if (sock->sk->sk_family != PF_XDP) {
  141. sockfd_put(sock);
  142. return -EOPNOTSUPP;
  143. }
  144. xs = (struct xdp_sock *)sock->sk;
  145. map_entry = &m->xsk_map[i];
  146. node = xsk_map_node_alloc(m, map_entry);
  147. if (IS_ERR(node)) {
  148. sockfd_put(sock);
  149. return PTR_ERR(node);
  150. }
  151. spin_lock_bh(&m->lock);
  152. old_xs = rcu_dereference_protected(*map_entry, lockdep_is_held(&m->lock));
  153. if (old_xs == xs) {
  154. err = 0;
  155. goto out;
  156. } else if (old_xs && map_flags == BPF_NOEXIST) {
  157. err = -EEXIST;
  158. goto out;
  159. } else if (!old_xs && map_flags == BPF_EXIST) {
  160. err = -ENOENT;
  161. goto out;
  162. }
  163. xsk_map_sock_add(xs, node);
  164. rcu_assign_pointer(*map_entry, xs);
  165. if (old_xs)
  166. xsk_map_sock_delete(old_xs, map_entry);
  167. spin_unlock_bh(&m->lock);
  168. sockfd_put(sock);
  169. return 0;
  170. out:
  171. spin_unlock_bh(&m->lock);
  172. sockfd_put(sock);
  173. xsk_map_node_free(node);
  174. return err;
  175. }
  176. static int xsk_map_delete_elem(struct bpf_map *map, void *key)
  177. {
  178. struct xsk_map *m = container_of(map, struct xsk_map, map);
  179. struct xdp_sock __rcu **map_entry;
  180. struct xdp_sock *old_xs;
  181. int k = *(u32 *)key;
  182. if (k >= map->max_entries)
  183. return -EINVAL;
  184. spin_lock_bh(&m->lock);
  185. map_entry = &m->xsk_map[k];
  186. old_xs = unrcu_pointer(xchg(map_entry, NULL));
  187. if (old_xs)
  188. xsk_map_sock_delete(old_xs, map_entry);
  189. spin_unlock_bh(&m->lock);
  190. return 0;
  191. }
  192. static int xsk_map_redirect(struct bpf_map *map, u32 ifindex, u64 flags)
  193. {
  194. return __bpf_xdp_redirect_map(map, ifindex, flags, 0,
  195. __xsk_map_lookup_elem);
  196. }
  197. void xsk_map_try_sock_delete(struct xsk_map *map, struct xdp_sock *xs,
  198. struct xdp_sock __rcu **map_entry)
  199. {
  200. spin_lock_bh(&map->lock);
  201. if (rcu_access_pointer(*map_entry) == xs) {
  202. rcu_assign_pointer(*map_entry, NULL);
  203. xsk_map_sock_delete(xs, map_entry);
  204. }
  205. spin_unlock_bh(&map->lock);
  206. }
  207. static bool xsk_map_meta_equal(const struct bpf_map *meta0,
  208. const struct bpf_map *meta1)
  209. {
  210. return meta0->max_entries == meta1->max_entries &&
  211. bpf_map_meta_equal(meta0, meta1);
  212. }
  213. BTF_ID_LIST_SINGLE(xsk_map_btf_ids, struct, xsk_map)
  214. const struct bpf_map_ops xsk_map_ops = {
  215. .map_meta_equal = xsk_map_meta_equal,
  216. .map_alloc = xsk_map_alloc,
  217. .map_free = xsk_map_free,
  218. .map_get_next_key = xsk_map_get_next_key,
  219. .map_lookup_elem = xsk_map_lookup_elem,
  220. .map_gen_lookup = xsk_map_gen_lookup,
  221. .map_lookup_elem_sys_only = xsk_map_lookup_elem_sys_only,
  222. .map_update_elem = xsk_map_update_elem,
  223. .map_delete_elem = xsk_map_delete_elem,
  224. .map_check_btf = map_check_no_btf,
  225. .map_btf_id = &xsk_map_btf_ids[0],
  226. .map_redirect = xsk_map_redirect,
  227. };