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