1 // SPDX-License-Identifier: GPL-2.0
2 /* XSKMAP used for AF_XDP sockets
3 * Copyright(c) 2018 Intel Corporation.
7 #include <linux/filter.h>
8 #include <net/xdp_sock.h>
9 #include <linux/slab.h>
10 #include <linux/sched.h>
11 #include <linux/btf_ids.h>
15 static struct xsk_map_node *xsk_map_node_alloc(struct xsk_map *map,
16 struct xdp_sock __rcu **map_entry)
18 struct xsk_map_node *node;
20 node = bpf_map_kzalloc(&map->map, sizeof(*node),
21 GFP_ATOMIC | __GFP_NOWARN);
23 return ERR_PTR(-ENOMEM);
25 bpf_map_inc(&map->map);
26 atomic_inc(&map->count);
29 node->map_entry = map_entry;
33 static void xsk_map_node_free(struct xsk_map_node *node)
35 struct xsk_map *map = node->map;
37 bpf_map_put(&node->map->map);
39 atomic_dec(&map->count);
42 static void xsk_map_sock_add(struct xdp_sock *xs, struct xsk_map_node *node)
44 spin_lock_bh(&xs->map_list_lock);
45 list_add_tail(&node->node, &xs->map_list);
46 spin_unlock_bh(&xs->map_list_lock);
49 static void xsk_map_sock_delete(struct xdp_sock *xs,
50 struct xdp_sock __rcu **map_entry)
52 struct xsk_map_node *n, *tmp;
54 spin_lock_bh(&xs->map_list_lock);
55 list_for_each_entry_safe(n, tmp, &xs->map_list, node) {
56 if (map_entry == n->map_entry) {
61 spin_unlock_bh(&xs->map_list_lock);
64 static struct bpf_map *xsk_map_alloc(union bpf_attr *attr)
70 if (attr->max_entries == 0 || attr->key_size != 4 ||
71 attr->value_size != 4 ||
72 attr->map_flags & ~(BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY))
73 return ERR_PTR(-EINVAL);
75 numa_node = bpf_map_attr_numa_node(attr);
76 size = struct_size(m, xsk_map, attr->max_entries);
78 m = bpf_map_area_alloc(size, numa_node);
80 return ERR_PTR(-ENOMEM);
82 bpf_map_init_from_attr(&m->map, attr);
83 spin_lock_init(&m->lock);
88 static u64 xsk_map_mem_usage(const struct bpf_map *map)
90 struct xsk_map *m = container_of(map, struct xsk_map, map);
92 return struct_size(m, xsk_map, map->max_entries) +
93 (u64)atomic_read(&m->count) * sizeof(struct xsk_map_node);
96 static void xsk_map_free(struct bpf_map *map)
98 struct xsk_map *m = container_of(map, struct xsk_map, map);
101 bpf_map_area_free(m);
104 static int xsk_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
106 struct xsk_map *m = container_of(map, struct xsk_map, map);
107 u32 index = key ? *(u32 *)key : U32_MAX;
108 u32 *next = next_key;
110 if (index >= m->map.max_entries) {
115 if (index == m->map.max_entries - 1)
121 static int xsk_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
123 const int ret = BPF_REG_0, mp = BPF_REG_1, index = BPF_REG_2;
124 struct bpf_insn *insn = insn_buf;
126 *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
127 *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 5);
128 *insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(sizeof(struct xsk_sock *)));
129 *insn++ = BPF_ALU64_IMM(BPF_ADD, mp, offsetof(struct xsk_map, xsk_map));
130 *insn++ = BPF_ALU64_REG(BPF_ADD, ret, mp);
131 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(struct xsk_sock *), ret, ret, 0);
132 *insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
133 *insn++ = BPF_MOV64_IMM(ret, 0);
134 return insn - insn_buf;
137 /* Elements are kept alive by RCU; either by rcu_read_lock() (from syscall) or
138 * by local_bh_disable() (from XDP calls inside NAPI). The
139 * rcu_read_lock_bh_held() below makes lockdep accept both.
141 static void *__xsk_map_lookup_elem(struct bpf_map *map, u32 key)
143 struct xsk_map *m = container_of(map, struct xsk_map, map);
145 if (key >= map->max_entries)
148 return rcu_dereference_check(m->xsk_map[key], rcu_read_lock_bh_held());
151 static void *xsk_map_lookup_elem(struct bpf_map *map, void *key)
153 return __xsk_map_lookup_elem(map, *(u32 *)key);
156 static void *xsk_map_lookup_elem_sys_only(struct bpf_map *map, void *key)
158 return ERR_PTR(-EOPNOTSUPP);
161 static long xsk_map_update_elem(struct bpf_map *map, void *key, void *value,
164 struct xsk_map *m = container_of(map, struct xsk_map, map);
165 struct xdp_sock __rcu **map_entry;
166 struct xdp_sock *xs, *old_xs;
167 u32 i = *(u32 *)key, fd = *(u32 *)value;
168 struct xsk_map_node *node;
172 if (unlikely(map_flags > BPF_EXIST))
174 if (unlikely(i >= m->map.max_entries))
177 sock = sockfd_lookup(fd, &err);
181 if (sock->sk->sk_family != PF_XDP) {
186 xs = (struct xdp_sock *)sock->sk;
188 map_entry = &m->xsk_map[i];
189 node = xsk_map_node_alloc(m, map_entry);
192 return PTR_ERR(node);
195 spin_lock_bh(&m->lock);
196 old_xs = rcu_dereference_protected(*map_entry, lockdep_is_held(&m->lock));
200 } else if (old_xs && map_flags == BPF_NOEXIST) {
203 } else if (!old_xs && map_flags == BPF_EXIST) {
207 xsk_map_sock_add(xs, node);
208 rcu_assign_pointer(*map_entry, xs);
210 xsk_map_sock_delete(old_xs, map_entry);
211 spin_unlock_bh(&m->lock);
216 spin_unlock_bh(&m->lock);
218 xsk_map_node_free(node);
222 static long xsk_map_delete_elem(struct bpf_map *map, void *key)
224 struct xsk_map *m = container_of(map, struct xsk_map, map);
225 struct xdp_sock __rcu **map_entry;
226 struct xdp_sock *old_xs;
229 if (k >= map->max_entries)
232 spin_lock_bh(&m->lock);
233 map_entry = &m->xsk_map[k];
234 old_xs = unrcu_pointer(xchg(map_entry, NULL));
236 xsk_map_sock_delete(old_xs, map_entry);
237 spin_unlock_bh(&m->lock);
242 static long xsk_map_redirect(struct bpf_map *map, u64 index, u64 flags)
244 return __bpf_xdp_redirect_map(map, index, flags, 0,
245 __xsk_map_lookup_elem);
248 void xsk_map_try_sock_delete(struct xsk_map *map, struct xdp_sock *xs,
249 struct xdp_sock __rcu **map_entry)
251 spin_lock_bh(&map->lock);
252 if (rcu_access_pointer(*map_entry) == xs) {
253 rcu_assign_pointer(*map_entry, NULL);
254 xsk_map_sock_delete(xs, map_entry);
256 spin_unlock_bh(&map->lock);
259 static bool xsk_map_meta_equal(const struct bpf_map *meta0,
260 const struct bpf_map *meta1)
262 return meta0->max_entries == meta1->max_entries &&
263 bpf_map_meta_equal(meta0, meta1);
266 BTF_ID_LIST_SINGLE(xsk_map_btf_ids, struct, xsk_map)
267 const struct bpf_map_ops xsk_map_ops = {
268 .map_meta_equal = xsk_map_meta_equal,
269 .map_alloc = xsk_map_alloc,
270 .map_free = xsk_map_free,
271 .map_get_next_key = xsk_map_get_next_key,
272 .map_lookup_elem = xsk_map_lookup_elem,
273 .map_gen_lookup = xsk_map_gen_lookup,
274 .map_lookup_elem_sys_only = xsk_map_lookup_elem_sys_only,
275 .map_update_elem = xsk_map_update_elem,
276 .map_delete_elem = xsk_map_delete_elem,
277 .map_check_btf = map_check_no_btf,
278 .map_mem_usage = xsk_map_mem_usage,
279 .map_btf_id = &xsk_map_btf_ids[0],
280 .map_redirect = xsk_map_redirect,