xskmap.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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/capability.h>
  7. #include <net/xdp_sock.h>
  8. #include <linux/slab.h>
  9. #include <linux/sched.h>
  10. struct xsk_map {
  11. struct bpf_map map;
  12. struct xdp_sock **xsk_map;
  13. struct list_head __percpu *flush_list;
  14. };
  15. static struct bpf_map *xsk_map_alloc(union bpf_attr *attr)
  16. {
  17. int cpu, err = -EINVAL;
  18. struct xsk_map *m;
  19. u64 cost;
  20. if (!capable(CAP_NET_ADMIN))
  21. return ERR_PTR(-EPERM);
  22. if (attr->max_entries == 0 || attr->key_size != 4 ||
  23. attr->value_size != 4 ||
  24. attr->map_flags & ~(BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY))
  25. return ERR_PTR(-EINVAL);
  26. m = kzalloc(sizeof(*m), GFP_USER);
  27. if (!m)
  28. return ERR_PTR(-ENOMEM);
  29. bpf_map_init_from_attr(&m->map, attr);
  30. cost = (u64)m->map.max_entries * sizeof(struct xdp_sock *);
  31. cost += sizeof(struct list_head) * num_possible_cpus();
  32. if (cost >= U32_MAX - PAGE_SIZE)
  33. goto free_m;
  34. m->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
  35. /* Notice returns -EPERM on if map size is larger than memlock limit */
  36. err = bpf_map_precharge_memlock(m->map.pages);
  37. if (err)
  38. goto free_m;
  39. err = -ENOMEM;
  40. m->flush_list = alloc_percpu(struct list_head);
  41. if (!m->flush_list)
  42. goto free_m;
  43. for_each_possible_cpu(cpu)
  44. INIT_LIST_HEAD(per_cpu_ptr(m->flush_list, cpu));
  45. m->xsk_map = bpf_map_area_alloc(m->map.max_entries *
  46. sizeof(struct xdp_sock *),
  47. m->map.numa_node);
  48. if (!m->xsk_map)
  49. goto free_percpu;
  50. return &m->map;
  51. free_percpu:
  52. free_percpu(m->flush_list);
  53. free_m:
  54. kfree(m);
  55. return ERR_PTR(err);
  56. }
  57. static void xsk_map_free(struct bpf_map *map)
  58. {
  59. struct xsk_map *m = container_of(map, struct xsk_map, map);
  60. int i;
  61. synchronize_net();
  62. for (i = 0; i < map->max_entries; i++) {
  63. struct xdp_sock *xs;
  64. xs = m->xsk_map[i];
  65. if (!xs)
  66. continue;
  67. sock_put((struct sock *)xs);
  68. }
  69. free_percpu(m->flush_list);
  70. bpf_map_area_free(m->xsk_map);
  71. kfree(m);
  72. }
  73. static int xsk_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
  74. {
  75. struct xsk_map *m = container_of(map, struct xsk_map, map);
  76. u32 index = key ? *(u32 *)key : U32_MAX;
  77. u32 *next = next_key;
  78. if (index >= m->map.max_entries) {
  79. *next = 0;
  80. return 0;
  81. }
  82. if (index == m->map.max_entries - 1)
  83. return -ENOENT;
  84. *next = index + 1;
  85. return 0;
  86. }
  87. struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map, u32 key)
  88. {
  89. struct xsk_map *m = container_of(map, struct xsk_map, map);
  90. struct xdp_sock *xs;
  91. if (key >= map->max_entries)
  92. return NULL;
  93. xs = READ_ONCE(m->xsk_map[key]);
  94. return xs;
  95. }
  96. int __xsk_map_redirect(struct bpf_map *map, struct xdp_buff *xdp,
  97. struct xdp_sock *xs)
  98. {
  99. struct xsk_map *m = container_of(map, struct xsk_map, map);
  100. struct list_head *flush_list = this_cpu_ptr(m->flush_list);
  101. int err;
  102. err = xsk_rcv(xs, xdp);
  103. if (err)
  104. return err;
  105. if (!xs->flush_node.prev)
  106. list_add(&xs->flush_node, flush_list);
  107. return 0;
  108. }
  109. void __xsk_map_flush(struct bpf_map *map)
  110. {
  111. struct xsk_map *m = container_of(map, struct xsk_map, map);
  112. struct list_head *flush_list = this_cpu_ptr(m->flush_list);
  113. struct xdp_sock *xs, *tmp;
  114. list_for_each_entry_safe(xs, tmp, flush_list, flush_node) {
  115. xsk_flush(xs);
  116. __list_del(xs->flush_node.prev, xs->flush_node.next);
  117. xs->flush_node.prev = NULL;
  118. }
  119. }
  120. static void *xsk_map_lookup_elem(struct bpf_map *map, void *key)
  121. {
  122. return NULL;
  123. }
  124. static int xsk_map_update_elem(struct bpf_map *map, void *key, void *value,
  125. u64 map_flags)
  126. {
  127. struct xsk_map *m = container_of(map, struct xsk_map, map);
  128. u32 i = *(u32 *)key, fd = *(u32 *)value;
  129. struct xdp_sock *xs, *old_xs;
  130. struct socket *sock;
  131. int err;
  132. if (unlikely(map_flags > BPF_EXIST))
  133. return -EINVAL;
  134. if (unlikely(i >= m->map.max_entries))
  135. return -E2BIG;
  136. if (unlikely(map_flags == BPF_NOEXIST))
  137. return -EEXIST;
  138. sock = sockfd_lookup(fd, &err);
  139. if (!sock)
  140. return err;
  141. if (sock->sk->sk_family != PF_XDP) {
  142. sockfd_put(sock);
  143. return -EOPNOTSUPP;
  144. }
  145. xs = (struct xdp_sock *)sock->sk;
  146. if (!xsk_is_setup_for_bpf_map(xs)) {
  147. sockfd_put(sock);
  148. return -EOPNOTSUPP;
  149. }
  150. sock_hold(sock->sk);
  151. old_xs = xchg(&m->xsk_map[i], xs);
  152. if (old_xs) {
  153. /* Make sure we've flushed everything. */
  154. synchronize_net();
  155. sock_put((struct sock *)old_xs);
  156. }
  157. sockfd_put(sock);
  158. return 0;
  159. }
  160. static int xsk_map_delete_elem(struct bpf_map *map, void *key)
  161. {
  162. struct xsk_map *m = container_of(map, struct xsk_map, map);
  163. struct xdp_sock *old_xs;
  164. int k = *(u32 *)key;
  165. if (k >= map->max_entries)
  166. return -EINVAL;
  167. old_xs = xchg(&m->xsk_map[k], NULL);
  168. if (old_xs) {
  169. /* Make sure we've flushed everything. */
  170. synchronize_net();
  171. sock_put((struct sock *)old_xs);
  172. }
  173. return 0;
  174. }
  175. const struct bpf_map_ops xsk_map_ops = {
  176. .map_alloc = xsk_map_alloc,
  177. .map_free = xsk_map_free,
  178. .map_get_next_key = xsk_map_get_next_key,
  179. .map_lookup_elem = xsk_map_lookup_elem,
  180. .map_update_elem = xsk_map_update_elem,
  181. .map_delete_elem = xsk_map_delete_elem,
  182. .map_check_btf = map_check_no_btf,
  183. };