nds32: fix build error "relocation truncated to fit: R_NDS32_25_PCREL_RELA" when
[linux-2.6-microblaze.git] / kernel / bpf / xskmap.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* XSKMAP used for AF_XDP sockets
3  * Copyright(c) 2018 Intel Corporation.
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
12 struct xsk_map {
13         struct bpf_map map;
14         struct xdp_sock **xsk_map;
15         struct list_head __percpu *flush_list;
16 };
17
18 static struct bpf_map *xsk_map_alloc(union bpf_attr *attr)
19 {
20         int cpu, err = -EINVAL;
21         struct xsk_map *m;
22         u64 cost;
23
24         if (!capable(CAP_NET_ADMIN))
25                 return ERR_PTR(-EPERM);
26
27         if (attr->max_entries == 0 || attr->key_size != 4 ||
28             attr->value_size != 4 ||
29             attr->map_flags & ~(BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY))
30                 return ERR_PTR(-EINVAL);
31
32         m = kzalloc(sizeof(*m), GFP_USER);
33         if (!m)
34                 return ERR_PTR(-ENOMEM);
35
36         bpf_map_init_from_attr(&m->map, attr);
37
38         cost = (u64)m->map.max_entries * sizeof(struct xdp_sock *);
39         cost += sizeof(struct list_head) * num_possible_cpus();
40         if (cost >= U32_MAX - PAGE_SIZE)
41                 goto free_m;
42
43         m->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
44
45         /* Notice returns -EPERM on if map size is larger than memlock limit */
46         err = bpf_map_precharge_memlock(m->map.pages);
47         if (err)
48                 goto free_m;
49
50         err = -ENOMEM;
51
52         m->flush_list = alloc_percpu(struct list_head);
53         if (!m->flush_list)
54                 goto free_m;
55
56         for_each_possible_cpu(cpu)
57                 INIT_LIST_HEAD(per_cpu_ptr(m->flush_list, cpu));
58
59         m->xsk_map = bpf_map_area_alloc(m->map.max_entries *
60                                         sizeof(struct xdp_sock *),
61                                         m->map.numa_node);
62         if (!m->xsk_map)
63                 goto free_percpu;
64         return &m->map;
65
66 free_percpu:
67         free_percpu(m->flush_list);
68 free_m:
69         kfree(m);
70         return ERR_PTR(err);
71 }
72
73 static void xsk_map_free(struct bpf_map *map)
74 {
75         struct xsk_map *m = container_of(map, struct xsk_map, map);
76         int i;
77
78         synchronize_net();
79
80         for (i = 0; i < map->max_entries; i++) {
81                 struct xdp_sock *xs;
82
83                 xs = m->xsk_map[i];
84                 if (!xs)
85                         continue;
86
87                 sock_put((struct sock *)xs);
88         }
89
90         free_percpu(m->flush_list);
91         bpf_map_area_free(m->xsk_map);
92         kfree(m);
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
112 struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map, u32 key)
113 {
114         struct xsk_map *m = container_of(map, struct xsk_map, map);
115         struct xdp_sock *xs;
116
117         if (key >= map->max_entries)
118                 return NULL;
119
120         xs = READ_ONCE(m->xsk_map[key]);
121         return xs;
122 }
123
124 int __xsk_map_redirect(struct bpf_map *map, struct xdp_buff *xdp,
125                        struct xdp_sock *xs)
126 {
127         struct xsk_map *m = container_of(map, struct xsk_map, map);
128         struct list_head *flush_list = this_cpu_ptr(m->flush_list);
129         int err;
130
131         err = xsk_rcv(xs, xdp);
132         if (err)
133                 return err;
134
135         if (!xs->flush_node.prev)
136                 list_add(&xs->flush_node, flush_list);
137
138         return 0;
139 }
140
141 void __xsk_map_flush(struct bpf_map *map)
142 {
143         struct xsk_map *m = container_of(map, struct xsk_map, map);
144         struct list_head *flush_list = this_cpu_ptr(m->flush_list);
145         struct xdp_sock *xs, *tmp;
146
147         list_for_each_entry_safe(xs, tmp, flush_list, flush_node) {
148                 xsk_flush(xs);
149                 __list_del(xs->flush_node.prev, xs->flush_node.next);
150                 xs->flush_node.prev = NULL;
151         }
152 }
153
154 static void *xsk_map_lookup_elem(struct bpf_map *map, void *key)
155 {
156         return NULL;
157 }
158
159 static int xsk_map_update_elem(struct bpf_map *map, void *key, void *value,
160                                u64 map_flags)
161 {
162         struct xsk_map *m = container_of(map, struct xsk_map, map);
163         u32 i = *(u32 *)key, fd = *(u32 *)value;
164         struct xdp_sock *xs, *old_xs;
165         struct socket *sock;
166         int err;
167
168         if (unlikely(map_flags > BPF_EXIST))
169                 return -EINVAL;
170         if (unlikely(i >= m->map.max_entries))
171                 return -E2BIG;
172         if (unlikely(map_flags == BPF_NOEXIST))
173                 return -EEXIST;
174
175         sock = sockfd_lookup(fd, &err);
176         if (!sock)
177                 return err;
178
179         if (sock->sk->sk_family != PF_XDP) {
180                 sockfd_put(sock);
181                 return -EOPNOTSUPP;
182         }
183
184         xs = (struct xdp_sock *)sock->sk;
185
186         if (!xsk_is_setup_for_bpf_map(xs)) {
187                 sockfd_put(sock);
188                 return -EOPNOTSUPP;
189         }
190
191         sock_hold(sock->sk);
192
193         old_xs = xchg(&m->xsk_map[i], xs);
194         if (old_xs) {
195                 /* Make sure we've flushed everything. */
196                 synchronize_net();
197                 sock_put((struct sock *)old_xs);
198         }
199
200         sockfd_put(sock);
201         return 0;
202 }
203
204 static int xsk_map_delete_elem(struct bpf_map *map, void *key)
205 {
206         struct xsk_map *m = container_of(map, struct xsk_map, map);
207         struct xdp_sock *old_xs;
208         int k = *(u32 *)key;
209
210         if (k >= map->max_entries)
211                 return -EINVAL;
212
213         old_xs = xchg(&m->xsk_map[k], NULL);
214         if (old_xs) {
215                 /* Make sure we've flushed everything. */
216                 synchronize_net();
217                 sock_put((struct sock *)old_xs);
218         }
219
220         return 0;
221 }
222
223 const struct bpf_map_ops xsk_map_ops = {
224         .map_alloc = xsk_map_alloc,
225         .map_free = xsk_map_free,
226         .map_get_next_key = xsk_map_get_next_key,
227         .map_lookup_elem = xsk_map_lookup_elem,
228         .map_update_elem = xsk_map_update_elem,
229         .map_delete_elem = xsk_map_delete_elem,
230 };
231
232