xskmap.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. bpf_clear_redirect_map(map);
  62. synchronize_net();
  63. for (i = 0; i < map->max_entries; i++) {
  64. struct xdp_sock *xs;
  65. xs = m->xsk_map[i];
  66. if (!xs)
  67. continue;
  68. sock_put((struct sock *)xs);
  69. }
  70. free_percpu(m->flush_list);
  71. bpf_map_area_free(m->xsk_map);
  72. kfree(m);
  73. }
  74. static int xsk_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
  75. {
  76. struct xsk_map *m = container_of(map, struct xsk_map, map);
  77. u32 index = key ? *(u32 *)key : U32_MAX;
  78. u32 *next = next_key;
  79. if (index >= m->map.max_entries) {
  80. *next = 0;
  81. return 0;
  82. }
  83. if (index == m->map.max_entries - 1)
  84. return -ENOENT;
  85. *next = index + 1;
  86. return 0;
  87. }
  88. struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map, u32 key)
  89. {
  90. struct xsk_map *m = container_of(map, struct xsk_map, map);
  91. struct xdp_sock *xs;
  92. if (key >= map->max_entries)
  93. return NULL;
  94. xs = READ_ONCE(m->xsk_map[key]);
  95. return xs;
  96. }
  97. int __xsk_map_redirect(struct bpf_map *map, struct xdp_buff *xdp,
  98. struct xdp_sock *xs)
  99. {
  100. struct xsk_map *m = container_of(map, struct xsk_map, map);
  101. struct list_head *flush_list = this_cpu_ptr(m->flush_list);
  102. int err;
  103. err = xsk_rcv(xs, xdp);
  104. if (err)
  105. return err;
  106. if (!xs->flush_node.prev)
  107. list_add(&xs->flush_node, flush_list);
  108. return 0;
  109. }
  110. void __xsk_map_flush(struct bpf_map *map)
  111. {
  112. struct xsk_map *m = container_of(map, struct xsk_map, map);
  113. struct list_head *flush_list = this_cpu_ptr(m->flush_list);
  114. struct xdp_sock *xs, *tmp;
  115. list_for_each_entry_safe(xs, tmp, flush_list, flush_node) {
  116. xsk_flush(xs);
  117. __list_del(xs->flush_node.prev, xs->flush_node.next);
  118. xs->flush_node.prev = NULL;
  119. }
  120. }
  121. static void *xsk_map_lookup_elem(struct bpf_map *map, void *key)
  122. {
  123. return NULL;
  124. }
  125. static int xsk_map_update_elem(struct bpf_map *map, void *key, void *value,
  126. u64 map_flags)
  127. {
  128. struct xsk_map *m = container_of(map, struct xsk_map, map);
  129. u32 i = *(u32 *)key, fd = *(u32 *)value;
  130. struct xdp_sock *xs, *old_xs;
  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. if (unlikely(map_flags == BPF_NOEXIST))
  138. return -EEXIST;
  139. sock = sockfd_lookup(fd, &err);
  140. if (!sock)
  141. return err;
  142. if (sock->sk->sk_family != PF_XDP) {
  143. sockfd_put(sock);
  144. return -EOPNOTSUPP;
  145. }
  146. xs = (struct xdp_sock *)sock->sk;
  147. if (!xsk_is_setup_for_bpf_map(xs)) {
  148. sockfd_put(sock);
  149. return -EOPNOTSUPP;
  150. }
  151. sock_hold(sock->sk);
  152. old_xs = xchg(&m->xsk_map[i], xs);
  153. if (old_xs)
  154. sock_put((struct sock *)old_xs);
  155. sockfd_put(sock);
  156. return 0;
  157. }
  158. static int xsk_map_delete_elem(struct bpf_map *map, void *key)
  159. {
  160. struct xsk_map *m = container_of(map, struct xsk_map, map);
  161. struct xdp_sock *old_xs;
  162. int k = *(u32 *)key;
  163. if (k >= map->max_entries)
  164. return -EINVAL;
  165. old_xs = xchg(&m->xsk_map[k], NULL);
  166. if (old_xs)
  167. sock_put((struct sock *)old_xs);
  168. return 0;
  169. }
  170. const struct bpf_map_ops xsk_map_ops = {
  171. .map_alloc = xsk_map_alloc,
  172. .map_free = xsk_map_free,
  173. .map_get_next_key = xsk_map_get_next_key,
  174. .map_lookup_elem = xsk_map_lookup_elem,
  175. .map_update_elem = xsk_map_update_elem,
  176. .map_delete_elem = xsk_map_delete_elem,
  177. .map_check_btf = map_check_no_btf,
  178. };