blob: 66231ba6c3482986e18a16483668a7abe99f2ea9 [file] [log] [blame]
Björn Töpelfbfc504a2018-05-02 13:01:28 +02001// SPDX-License-Identifier: GPL-2.0
2/* XSKMAP used for AF_XDP sockets
3 * Copyright(c) 2018 Intel Corporation.
Björn Töpelfbfc504a2018-05-02 13:01:28 +02004 */
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öpeld20a16762020-05-20 21:20:50 +020012#include "xsk.h"
13
Björn Töpel0402acd2019-08-15 11:30:13 +020014static 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öpel0402acd2019-08-15 11:30:13 +020018
19 node = kzalloc(sizeof(*node), GFP_ATOMIC | __GFP_NOWARN);
20 if (!node)
Jonathan Lemonfcd30ae2019-09-24 09:25:21 -070021 return ERR_PTR(-ENOMEM);
Björn Töpel0402acd2019-08-15 11:30:13 +020022
Zhu Yanjunbb1b25c2020-11-26 23:03:18 +080023 bpf_map_inc(&map->map);
Björn Töpel0402acd2019-08-15 11:30:13 +020024
25 node->map = map;
26 node->map_entry = map_entry;
27 return node;
28}
29
30static void xsk_map_node_free(struct xsk_map_node *node)
31{
Zhu Yanjunbb1b25c2020-11-26 23:03:18 +080032 bpf_map_put(&node->map->map);
Björn Töpel0402acd2019-08-15 11:30:13 +020033 kfree(node);
34}
35
36static void xsk_map_sock_add(struct xdp_sock *xs, struct xsk_map_node *node)
37{
38 spin_lock_bh(&xs->map_list_lock);
39 list_add_tail(&node->node, &xs->map_list);
40 spin_unlock_bh(&xs->map_list_lock);
41}
42
43static void xsk_map_sock_delete(struct xdp_sock *xs,
44 struct xdp_sock **map_entry)
45{
46 struct xsk_map_node *n, *tmp;
47
48 spin_lock_bh(&xs->map_list_lock);
49 list_for_each_entry_safe(n, tmp, &xs->map_list, node) {
50 if (map_entry == n->map_entry) {
51 list_del(&n->node);
52 xsk_map_node_free(n);
53 }
54 }
55 spin_unlock_bh(&xs->map_list_lock);
56}
57
Björn Töpelfbfc504a2018-05-02 13:01:28 +020058static struct bpf_map *xsk_map_alloc(union bpf_attr *attr)
59{
Björn Töpel64fe8c02019-11-01 12:03:44 +010060 struct bpf_map_memory mem;
Björn Töpele312b9e2019-12-19 07:10:02 +010061 int err, numa_node;
Björn Töpelfbfc504a2018-05-02 13:01:28 +020062 struct xsk_map *m;
Björn Töpele312b9e2019-12-19 07:10:02 +010063 u64 size;
Björn Töpelfbfc504a2018-05-02 13:01:28 +020064
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öpel64fe8c02019-11-01 12:03:44 +010073 numa_node = bpf_map_attr_numa_node(attr);
74 size = struct_size(m, xsk_map, attr->max_entries);
Björn Töpel64fe8c02019-11-01 12:03:44 +010075
Björn Töpele312b9e2019-12-19 07:10:02 +010076 err = bpf_map_charge_init(&mem, size);
Björn Töpel64fe8c02019-11-01 12:03:44 +010077 if (err < 0)
78 return ERR_PTR(err);
79
80 m = bpf_map_area_alloc(size, numa_node);
81 if (!m) {
82 bpf_map_charge_finish(&mem);
Björn Töpelfbfc504a2018-05-02 13:01:28 +020083 return ERR_PTR(-ENOMEM);
Björn Töpel64fe8c02019-11-01 12:03:44 +010084 }
Björn Töpelfbfc504a2018-05-02 13:01:28 +020085
86 bpf_map_init_from_attr(&m->map, attr);
Björn Töpel64fe8c02019-11-01 12:03:44 +010087 bpf_map_charge_move(&m->map.memory, &mem);
Björn Töpel0402acd2019-08-15 11:30:13 +020088 spin_lock_init(&m->lock);
Björn Töpelfbfc504a2018-05-02 13:01:28 +020089
Björn Töpelfbfc504a2018-05-02 13:01:28 +020090 return &m->map;
Björn Töpelfbfc504a2018-05-02 13:01:28 +020091}
92
93static void xsk_map_free(struct bpf_map *map)
94{
95 struct xsk_map *m = container_of(map, struct xsk_map, map);
Björn Töpelfbfc504a2018-05-02 13:01:28 +020096
Daniel Borkmannf6069b92018-08-17 23:26:14 +020097 bpf_clear_redirect_map(map);
Björn Töpelfbfc504a2018-05-02 13:01:28 +020098 synchronize_net();
Björn Töpel64fe8c02019-11-01 12:03:44 +010099 bpf_map_area_free(m);
Björn Töpelfbfc504a2018-05-02 13:01:28 +0200100}
101
102static int xsk_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
103{
104 struct xsk_map *m = container_of(map, struct xsk_map, map);
105 u32 index = key ? *(u32 *)key : U32_MAX;
106 u32 *next = next_key;
107
108 if (index >= m->map.max_entries) {
109 *next = 0;
110 return 0;
111 }
112
113 if (index == m->map.max_entries - 1)
114 return -ENOENT;
115 *next = index + 1;
116 return 0;
117}
118
Daniel Borkmann4a8f87e2020-10-11 01:40:03 +0200119static int xsk_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
Maciej Fijalkowskie65650f2019-11-01 12:03:45 +0100120{
121 const int ret = BPF_REG_0, mp = BPF_REG_1, index = BPF_REG_2;
122 struct bpf_insn *insn = insn_buf;
123
124 *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
125 *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 5);
126 *insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(sizeof(struct xsk_sock *)));
127 *insn++ = BPF_ALU64_IMM(BPF_ADD, mp, offsetof(struct xsk_map, xsk_map));
128 *insn++ = BPF_ALU64_REG(BPF_ADD, ret, mp);
129 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(struct xsk_sock *), ret, ret, 0);
130 *insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
131 *insn++ = BPF_MOV64_IMM(ret, 0);
132 return insn - insn_buf;
133}
134
Björn Töpelfbfc504a2018-05-02 13:01:28 +0200135static void *xsk_map_lookup_elem(struct bpf_map *map, void *key)
136{
Jonathan Lemonfada7fd2019-06-06 13:59:40 -0700137 WARN_ON_ONCE(!rcu_read_lock_held());
138 return __xsk_map_lookup_elem(map, *(u32 *)key);
139}
140
141static void *xsk_map_lookup_elem_sys_only(struct bpf_map *map, void *key)
142{
Prashant Bhole3b4a63f2018-10-09 10:04:50 +0900143 return ERR_PTR(-EOPNOTSUPP);
Björn Töpelfbfc504a2018-05-02 13:01:28 +0200144}
145
146static int xsk_map_update_elem(struct bpf_map *map, void *key, void *value,
147 u64 map_flags)
148{
149 struct xsk_map *m = container_of(map, struct xsk_map, map);
Björn Töpel0402acd2019-08-15 11:30:13 +0200150 struct xdp_sock *xs, *old_xs, **map_entry;
Björn Töpelfbfc504a2018-05-02 13:01:28 +0200151 u32 i = *(u32 *)key, fd = *(u32 *)value;
Björn Töpel0402acd2019-08-15 11:30:13 +0200152 struct xsk_map_node *node;
Björn Töpelfbfc504a2018-05-02 13:01:28 +0200153 struct socket *sock;
154 int err;
155
156 if (unlikely(map_flags > BPF_EXIST))
157 return -EINVAL;
158 if (unlikely(i >= m->map.max_entries))
159 return -E2BIG;
Björn Töpelfbfc504a2018-05-02 13:01:28 +0200160
161 sock = sockfd_lookup(fd, &err);
162 if (!sock)
163 return err;
164
165 if (sock->sk->sk_family != PF_XDP) {
166 sockfd_put(sock);
167 return -EOPNOTSUPP;
168 }
169
170 xs = (struct xdp_sock *)sock->sk;
171
Björn Töpel0402acd2019-08-15 11:30:13 +0200172 map_entry = &m->xsk_map[i];
173 node = xsk_map_node_alloc(m, map_entry);
174 if (IS_ERR(node)) {
175 sockfd_put(sock);
176 return PTR_ERR(node);
177 }
Björn Töpelfbfc504a2018-05-02 13:01:28 +0200178
Björn Töpel0402acd2019-08-15 11:30:13 +0200179 spin_lock_bh(&m->lock);
180 old_xs = READ_ONCE(*map_entry);
181 if (old_xs == xs) {
182 err = 0;
183 goto out;
Björn Töpel36cc34352019-08-15 11:30:14 +0200184 } else if (old_xs && map_flags == BPF_NOEXIST) {
185 err = -EEXIST;
186 goto out;
187 } else if (!old_xs && map_flags == BPF_EXIST) {
188 err = -ENOENT;
189 goto out;
Björn Töpel0402acd2019-08-15 11:30:13 +0200190 }
191 xsk_map_sock_add(xs, node);
192 WRITE_ONCE(*map_entry, xs);
Björn Töpelcee27162018-10-08 19:40:16 +0200193 if (old_xs)
Björn Töpel0402acd2019-08-15 11:30:13 +0200194 xsk_map_sock_delete(old_xs, map_entry);
195 spin_unlock_bh(&m->lock);
Björn Töpelfbfc504a2018-05-02 13:01:28 +0200196 sockfd_put(sock);
197 return 0;
Björn Töpel0402acd2019-08-15 11:30:13 +0200198
199out:
200 spin_unlock_bh(&m->lock);
201 sockfd_put(sock);
202 xsk_map_node_free(node);
203 return err;
Björn Töpelfbfc504a2018-05-02 13:01:28 +0200204}
205
206static int xsk_map_delete_elem(struct bpf_map *map, void *key)
207{
208 struct xsk_map *m = container_of(map, struct xsk_map, map);
Björn Töpel0402acd2019-08-15 11:30:13 +0200209 struct xdp_sock *old_xs, **map_entry;
Björn Töpelfbfc504a2018-05-02 13:01:28 +0200210 int k = *(u32 *)key;
211
212 if (k >= map->max_entries)
213 return -EINVAL;
214
Björn Töpel0402acd2019-08-15 11:30:13 +0200215 spin_lock_bh(&m->lock);
216 map_entry = &m->xsk_map[k];
217 old_xs = xchg(map_entry, NULL);
Björn Töpelcee27162018-10-08 19:40:16 +0200218 if (old_xs)
Björn Töpel0402acd2019-08-15 11:30:13 +0200219 xsk_map_sock_delete(old_xs, map_entry);
220 spin_unlock_bh(&m->lock);
Björn Töpelfbfc504a2018-05-02 13:01:28 +0200221
222 return 0;
223}
224
Björn Töpel0402acd2019-08-15 11:30:13 +0200225void xsk_map_try_sock_delete(struct xsk_map *map, struct xdp_sock *xs,
226 struct xdp_sock **map_entry)
227{
228 spin_lock_bh(&map->lock);
229 if (READ_ONCE(*map_entry) == xs) {
230 WRITE_ONCE(*map_entry, NULL);
231 xsk_map_sock_delete(xs, map_entry);
232 }
233 spin_unlock_bh(&map->lock);
234}
235
Martin KaFai Lau134fede2020-08-27 18:18:13 -0700236static bool xsk_map_meta_equal(const struct bpf_map *meta0,
237 const struct bpf_map *meta1)
238{
239 return meta0->max_entries == meta1->max_entries &&
240 bpf_map_meta_equal(meta0, meta1);
241}
242
Andrey Ignatov2872e9a2020-06-19 14:11:44 -0700243static int xsk_map_btf_id;
Björn Töpelfbfc504a2018-05-02 13:01:28 +0200244const struct bpf_map_ops xsk_map_ops = {
Martin KaFai Lau134fede2020-08-27 18:18:13 -0700245 .map_meta_equal = xsk_map_meta_equal,
Björn Töpelfbfc504a2018-05-02 13:01:28 +0200246 .map_alloc = xsk_map_alloc,
247 .map_free = xsk_map_free,
248 .map_get_next_key = xsk_map_get_next_key,
249 .map_lookup_elem = xsk_map_lookup_elem,
Maciej Fijalkowskie65650f2019-11-01 12:03:45 +0100250 .map_gen_lookup = xsk_map_gen_lookup,
Jonathan Lemonfada7fd2019-06-06 13:59:40 -0700251 .map_lookup_elem_sys_only = xsk_map_lookup_elem_sys_only,
Björn Töpelfbfc504a2018-05-02 13:01:28 +0200252 .map_update_elem = xsk_map_update_elem,
253 .map_delete_elem = xsk_map_delete_elem,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200254 .map_check_btf = map_check_no_btf,
Andrey Ignatov2872e9a2020-06-19 14:11:44 -0700255 .map_btf_name = "xsk_map",
256 .map_btf_id = &xsk_map_btf_id,
Björn Töpelfbfc504a2018-05-02 13:01:28 +0200257};