Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* XSKMAP used for AF_XDP sockets |
| 3 | * Copyright(c) 2018 Intel Corporation. |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/bpf.h> |
Jakub Kicinski | b645941 | 2021-12-28 16:49:13 -0800 | [diff] [blame] | 7 | #include <linux/filter.h> |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 8 | #include <linux/capability.h> |
| 9 | #include <net/xdp_sock.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/sched.h> |
| 12 | |
Björn Töpel | d20a1676 | 2020-05-20 21:20:50 +0200 | [diff] [blame] | 13 | #include "xsk.h" |
| 14 | |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 15 | static struct xsk_map_node *xsk_map_node_alloc(struct xsk_map *map, |
Toke Høiland-Jørgensen | 782347b | 2021-06-24 18:05:55 +0200 | [diff] [blame] | 16 | struct xdp_sock __rcu **map_entry) |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 17 | { |
| 18 | struct xsk_map_node *node; |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 19 | |
Roman Gushchin | 28e1dcd | 2020-12-01 13:58:43 -0800 | [diff] [blame] | 20 | node = bpf_map_kzalloc(&map->map, sizeof(*node), |
| 21 | GFP_ATOMIC | __GFP_NOWARN); |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 22 | if (!node) |
Jonathan Lemon | fcd30ae | 2019-09-24 09:25:21 -0700 | [diff] [blame] | 23 | return ERR_PTR(-ENOMEM); |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 24 | |
Zhu Yanjun | bb1b25c | 2020-11-26 23:03:18 +0800 | [diff] [blame] | 25 | bpf_map_inc(&map->map); |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 26 | |
| 27 | node->map = map; |
| 28 | node->map_entry = map_entry; |
| 29 | return node; |
| 30 | } |
| 31 | |
| 32 | static void xsk_map_node_free(struct xsk_map_node *node) |
| 33 | { |
Zhu Yanjun | bb1b25c | 2020-11-26 23:03:18 +0800 | [diff] [blame] | 34 | bpf_map_put(&node->map->map); |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 35 | kfree(node); |
| 36 | } |
| 37 | |
| 38 | static void xsk_map_sock_add(struct xdp_sock *xs, struct xsk_map_node *node) |
| 39 | { |
| 40 | spin_lock_bh(&xs->map_list_lock); |
| 41 | list_add_tail(&node->node, &xs->map_list); |
| 42 | spin_unlock_bh(&xs->map_list_lock); |
| 43 | } |
| 44 | |
| 45 | static void xsk_map_sock_delete(struct xdp_sock *xs, |
Toke Høiland-Jørgensen | 782347b | 2021-06-24 18:05:55 +0200 | [diff] [blame] | 46 | struct xdp_sock __rcu **map_entry) |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 47 | { |
| 48 | struct xsk_map_node *n, *tmp; |
| 49 | |
| 50 | spin_lock_bh(&xs->map_list_lock); |
| 51 | list_for_each_entry_safe(n, tmp, &xs->map_list, node) { |
| 52 | if (map_entry == n->map_entry) { |
| 53 | list_del(&n->node); |
| 54 | xsk_map_node_free(n); |
| 55 | } |
| 56 | } |
| 57 | spin_unlock_bh(&xs->map_list_lock); |
| 58 | } |
| 59 | |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 60 | static struct bpf_map *xsk_map_alloc(union bpf_attr *attr) |
| 61 | { |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 62 | struct xsk_map *m; |
Roman Gushchin | 819a4f3 | 2020-12-01 13:58:56 -0800 | [diff] [blame] | 63 | int numa_node; |
Björn Töpel | e312b9e | 2019-12-19 07:10:02 +0100 | [diff] [blame] | 64 | u64 size; |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 65 | |
| 66 | if (!capable(CAP_NET_ADMIN)) |
| 67 | return ERR_PTR(-EPERM); |
| 68 | |
| 69 | if (attr->max_entries == 0 || attr->key_size != 4 || |
| 70 | attr->value_size != 4 || |
| 71 | attr->map_flags & ~(BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)) |
| 72 | return ERR_PTR(-EINVAL); |
| 73 | |
Björn Töpel | 64fe8c0 | 2019-11-01 12:03:44 +0100 | [diff] [blame] | 74 | numa_node = bpf_map_attr_numa_node(attr); |
| 75 | size = struct_size(m, xsk_map, attr->max_entries); |
Björn Töpel | 64fe8c0 | 2019-11-01 12:03:44 +0100 | [diff] [blame] | 76 | |
Björn Töpel | 64fe8c0 | 2019-11-01 12:03:44 +0100 | [diff] [blame] | 77 | m = bpf_map_area_alloc(size, numa_node); |
Roman Gushchin | 819a4f3 | 2020-12-01 13:58:56 -0800 | [diff] [blame] | 78 | if (!m) |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 79 | return ERR_PTR(-ENOMEM); |
| 80 | |
| 81 | bpf_map_init_from_attr(&m->map, attr); |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 82 | spin_lock_init(&m->lock); |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 83 | |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 84 | return &m->map; |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static void xsk_map_free(struct bpf_map *map) |
| 88 | { |
| 89 | struct xsk_map *m = container_of(map, struct xsk_map, map); |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 90 | |
| 91 | synchronize_net(); |
Björn Töpel | 64fe8c0 | 2019-11-01 12:03:44 +0100 | [diff] [blame] | 92 | bpf_map_area_free(m); |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static int xsk_map_get_next_key(struct bpf_map *map, void *key, void *next_key) |
| 96 | { |
| 97 | struct xsk_map *m = container_of(map, struct xsk_map, map); |
| 98 | u32 index = key ? *(u32 *)key : U32_MAX; |
| 99 | u32 *next = next_key; |
| 100 | |
| 101 | if (index >= m->map.max_entries) { |
| 102 | *next = 0; |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | if (index == m->map.max_entries - 1) |
| 107 | return -ENOENT; |
| 108 | *next = index + 1; |
| 109 | return 0; |
| 110 | } |
| 111 | |
Daniel Borkmann | 4a8f87e | 2020-10-11 01:40:03 +0200 | [diff] [blame] | 112 | static int xsk_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf) |
Maciej Fijalkowski | e65650f | 2019-11-01 12:03:45 +0100 | [diff] [blame] | 113 | { |
| 114 | const int ret = BPF_REG_0, mp = BPF_REG_1, index = BPF_REG_2; |
| 115 | struct bpf_insn *insn = insn_buf; |
| 116 | |
| 117 | *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0); |
| 118 | *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 5); |
| 119 | *insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(sizeof(struct xsk_sock *))); |
| 120 | *insn++ = BPF_ALU64_IMM(BPF_ADD, mp, offsetof(struct xsk_map, xsk_map)); |
| 121 | *insn++ = BPF_ALU64_REG(BPF_ADD, ret, mp); |
| 122 | *insn++ = BPF_LDX_MEM(BPF_SIZEOF(struct xsk_sock *), ret, ret, 0); |
| 123 | *insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1); |
| 124 | *insn++ = BPF_MOV64_IMM(ret, 0); |
| 125 | return insn - insn_buf; |
| 126 | } |
| 127 | |
Toke Høiland-Jørgensen | 782347b | 2021-06-24 18:05:55 +0200 | [diff] [blame] | 128 | /* Elements are kept alive by RCU; either by rcu_read_lock() (from syscall) or |
| 129 | * by local_bh_disable() (from XDP calls inside NAPI). The |
| 130 | * rcu_read_lock_bh_held() below makes lockdep accept both. |
| 131 | */ |
Björn Töpel | e6a4750 | 2021-03-08 12:29:06 +0100 | [diff] [blame] | 132 | static void *__xsk_map_lookup_elem(struct bpf_map *map, u32 key) |
| 133 | { |
| 134 | struct xsk_map *m = container_of(map, struct xsk_map, map); |
| 135 | |
| 136 | if (key >= map->max_entries) |
| 137 | return NULL; |
| 138 | |
Toke Høiland-Jørgensen | 782347b | 2021-06-24 18:05:55 +0200 | [diff] [blame] | 139 | return rcu_dereference_check(m->xsk_map[key], rcu_read_lock_bh_held()); |
Björn Töpel | e6a4750 | 2021-03-08 12:29:06 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 142 | static void *xsk_map_lookup_elem(struct bpf_map *map, void *key) |
| 143 | { |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 144 | return __xsk_map_lookup_elem(map, *(u32 *)key); |
| 145 | } |
| 146 | |
| 147 | static void *xsk_map_lookup_elem_sys_only(struct bpf_map *map, void *key) |
| 148 | { |
Prashant Bhole | 3b4a63f | 2018-10-09 10:04:50 +0900 | [diff] [blame] | 149 | return ERR_PTR(-EOPNOTSUPP); |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static int xsk_map_update_elem(struct bpf_map *map, void *key, void *value, |
| 153 | u64 map_flags) |
| 154 | { |
| 155 | struct xsk_map *m = container_of(map, struct xsk_map, map); |
Toke Høiland-Jørgensen | 782347b | 2021-06-24 18:05:55 +0200 | [diff] [blame] | 156 | struct xdp_sock __rcu **map_entry; |
| 157 | struct xdp_sock *xs, *old_xs; |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 158 | u32 i = *(u32 *)key, fd = *(u32 *)value; |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 159 | struct xsk_map_node *node; |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 160 | struct socket *sock; |
| 161 | int err; |
| 162 | |
| 163 | if (unlikely(map_flags > BPF_EXIST)) |
| 164 | return -EINVAL; |
| 165 | if (unlikely(i >= m->map.max_entries)) |
| 166 | return -E2BIG; |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 167 | |
| 168 | sock = sockfd_lookup(fd, &err); |
| 169 | if (!sock) |
| 170 | return err; |
| 171 | |
| 172 | if (sock->sk->sk_family != PF_XDP) { |
| 173 | sockfd_put(sock); |
| 174 | return -EOPNOTSUPP; |
| 175 | } |
| 176 | |
| 177 | xs = (struct xdp_sock *)sock->sk; |
| 178 | |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 179 | map_entry = &m->xsk_map[i]; |
| 180 | node = xsk_map_node_alloc(m, map_entry); |
| 181 | if (IS_ERR(node)) { |
| 182 | sockfd_put(sock); |
| 183 | return PTR_ERR(node); |
| 184 | } |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 185 | |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 186 | spin_lock_bh(&m->lock); |
Toke Høiland-Jørgensen | 782347b | 2021-06-24 18:05:55 +0200 | [diff] [blame] | 187 | old_xs = rcu_dereference_protected(*map_entry, lockdep_is_held(&m->lock)); |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 188 | if (old_xs == xs) { |
| 189 | err = 0; |
| 190 | goto out; |
Björn Töpel | 36cc3435 | 2019-08-15 11:30:14 +0200 | [diff] [blame] | 191 | } else if (old_xs && map_flags == BPF_NOEXIST) { |
| 192 | err = -EEXIST; |
| 193 | goto out; |
| 194 | } else if (!old_xs && map_flags == BPF_EXIST) { |
| 195 | err = -ENOENT; |
| 196 | goto out; |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 197 | } |
| 198 | xsk_map_sock_add(xs, node); |
Toke Høiland-Jørgensen | 782347b | 2021-06-24 18:05:55 +0200 | [diff] [blame] | 199 | rcu_assign_pointer(*map_entry, xs); |
Björn Töpel | cee2716 | 2018-10-08 19:40:16 +0200 | [diff] [blame] | 200 | if (old_xs) |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 201 | xsk_map_sock_delete(old_xs, map_entry); |
| 202 | spin_unlock_bh(&m->lock); |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 203 | sockfd_put(sock); |
| 204 | return 0; |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 205 | |
| 206 | out: |
| 207 | spin_unlock_bh(&m->lock); |
| 208 | sockfd_put(sock); |
| 209 | xsk_map_node_free(node); |
| 210 | return err; |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static int xsk_map_delete_elem(struct bpf_map *map, void *key) |
| 214 | { |
| 215 | struct xsk_map *m = container_of(map, struct xsk_map, map); |
Toke Høiland-Jørgensen | 782347b | 2021-06-24 18:05:55 +0200 | [diff] [blame] | 216 | struct xdp_sock __rcu **map_entry; |
| 217 | struct xdp_sock *old_xs; |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 218 | int k = *(u32 *)key; |
| 219 | |
| 220 | if (k >= map->max_entries) |
| 221 | return -EINVAL; |
| 222 | |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 223 | spin_lock_bh(&m->lock); |
| 224 | map_entry = &m->xsk_map[k]; |
Toke Høiland-Jørgensen | 782347b | 2021-06-24 18:05:55 +0200 | [diff] [blame] | 225 | old_xs = unrcu_pointer(xchg(map_entry, NULL)); |
Björn Töpel | cee2716 | 2018-10-08 19:40:16 +0200 | [diff] [blame] | 226 | if (old_xs) |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 227 | xsk_map_sock_delete(old_xs, map_entry); |
| 228 | spin_unlock_bh(&m->lock); |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
Björn Töpel | e6a4750 | 2021-03-08 12:29:06 +0100 | [diff] [blame] | 233 | static int xsk_map_redirect(struct bpf_map *map, u32 ifindex, u64 flags) |
| 234 | { |
Hangbin Liu | e624d4e | 2021-05-19 17:07:45 +0800 | [diff] [blame] | 235 | return __bpf_xdp_redirect_map(map, ifindex, flags, 0, |
| 236 | __xsk_map_lookup_elem); |
Björn Töpel | e6a4750 | 2021-03-08 12:29:06 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 239 | void xsk_map_try_sock_delete(struct xsk_map *map, struct xdp_sock *xs, |
Toke Høiland-Jørgensen | 782347b | 2021-06-24 18:05:55 +0200 | [diff] [blame] | 240 | struct xdp_sock __rcu **map_entry) |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 241 | { |
| 242 | spin_lock_bh(&map->lock); |
Toke Høiland-Jørgensen | 782347b | 2021-06-24 18:05:55 +0200 | [diff] [blame] | 243 | if (rcu_access_pointer(*map_entry) == xs) { |
| 244 | rcu_assign_pointer(*map_entry, NULL); |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 245 | xsk_map_sock_delete(xs, map_entry); |
| 246 | } |
| 247 | spin_unlock_bh(&map->lock); |
| 248 | } |
| 249 | |
Martin KaFai Lau | 134fede | 2020-08-27 18:18:13 -0700 | [diff] [blame] | 250 | static bool xsk_map_meta_equal(const struct bpf_map *meta0, |
| 251 | const struct bpf_map *meta1) |
| 252 | { |
| 253 | return meta0->max_entries == meta1->max_entries && |
| 254 | bpf_map_meta_equal(meta0, meta1); |
| 255 | } |
| 256 | |
Andrey Ignatov | 2872e9a | 2020-06-19 14:11:44 -0700 | [diff] [blame] | 257 | static int xsk_map_btf_id; |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 258 | const struct bpf_map_ops xsk_map_ops = { |
Martin KaFai Lau | 134fede | 2020-08-27 18:18:13 -0700 | [diff] [blame] | 259 | .map_meta_equal = xsk_map_meta_equal, |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 260 | .map_alloc = xsk_map_alloc, |
| 261 | .map_free = xsk_map_free, |
| 262 | .map_get_next_key = xsk_map_get_next_key, |
| 263 | .map_lookup_elem = xsk_map_lookup_elem, |
Maciej Fijalkowski | e65650f | 2019-11-01 12:03:45 +0100 | [diff] [blame] | 264 | .map_gen_lookup = xsk_map_gen_lookup, |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 265 | .map_lookup_elem_sys_only = xsk_map_lookup_elem_sys_only, |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 266 | .map_update_elem = xsk_map_update_elem, |
| 267 | .map_delete_elem = xsk_map_delete_elem, |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 268 | .map_check_btf = map_check_no_btf, |
Andrey Ignatov | 2872e9a | 2020-06-19 14:11:44 -0700 | [diff] [blame] | 269 | .map_btf_name = "xsk_map", |
| 270 | .map_btf_id = &xsk_map_btf_id, |
Björn Töpel | e6a4750 | 2021-03-08 12:29:06 +0100 | [diff] [blame] | 271 | .map_redirect = xsk_map_redirect, |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 272 | }; |