blob: 0089d56617ecb32894abcb42fccb63400e06774b [file] [log] [blame]
Thomas Gleixner5b497af2019-05-29 07:18:09 -07001// SPDX-License-Identifier: GPL-2.0-only
John Fastabend546ac1f2017-07-17 09:28:56 -07002/* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io
John Fastabend546ac1f2017-07-17 09:28:56 -07003 */
4
5/* Devmaps primary use is as a backend map for XDP BPF helper call
6 * bpf_redirect_map(). Because XDP is mostly concerned with performance we
7 * spent some effort to ensure the datapath with redirect maps does not use
8 * any locking. This is a quick note on the details.
9 *
10 * We have three possible paths to get into the devmap control plane bpf
11 * syscalls, bpf programs, and driver side xmit/flush operations. A bpf syscall
12 * will invoke an update, delete, or lookup operation. To ensure updates and
13 * deletes appear atomic from the datapath side xchg() is used to modify the
14 * netdev_map array. Then because the datapath does a lookup into the netdev_map
15 * array (read-only) from an RCU critical section we use call_rcu() to wait for
16 * an rcu grace period before free'ing the old data structures. This ensures the
17 * datapath always has a valid copy. However, the datapath does a "flush"
18 * operation that pushes any pending packets in the driver outside the RCU
19 * critical section. Each bpf_dtab_netdev tracks these pending operations using
Toke Høiland-Jørgensend5df2832019-06-28 11:12:34 +020020 * a per-cpu flush list. The bpf_dtab_netdev object will not be destroyed until
21 * this list is empty, indicating outstanding flush operations have completed.
John Fastabend546ac1f2017-07-17 09:28:56 -070022 *
23 * BPF syscalls may race with BPF program calls on any of the update, delete
24 * or lookup operations. As noted above the xchg() operation also keep the
25 * netdev_map consistent in this case. From the devmap side BPF programs
26 * calling into these operations are the same as multiple user space threads
27 * making system calls.
John Fastabend2ddf71e2017-07-17 09:30:02 -070028 *
29 * Finally, any of the above may race with a netdev_unregister notifier. The
30 * unregister notifier must search for net devices in the map structure that
31 * contain a reference to the net device and remove them. This is a two step
32 * process (a) dereference the bpf_dtab_netdev object in netdev_map and (b)
33 * check to see if the ifindex is the same as the net_device being removed.
John Fastabend4cc7b952017-08-04 22:02:19 -070034 * When removing the dev a cmpxchg() is used to ensure the correct dev is
35 * removed, in the case of a concurrent update or delete operation it is
36 * possible that the initially referenced dev is no longer in the map. As the
37 * notifier hook walks the map we know that new dev references can not be
38 * added by the user because core infrastructure ensures dev_get_by_index()
39 * calls will fail at this point.
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +020040 *
41 * The devmap_hash type is a map type which interprets keys as ifindexes and
42 * indexes these using a hashmap. This allows maps that use ifindex as key to be
43 * densely packed instead of having holes in the lookup array for unused
44 * ifindexes. The setup and packet enqueue/send code is shared between the two
45 * types of devmap; only the lookup and insertion is different.
John Fastabend546ac1f2017-07-17 09:28:56 -070046 */
47#include <linux/bpf.h>
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +020048#include <net/xdp.h>
John Fastabend546ac1f2017-07-17 09:28:56 -070049#include <linux/filter.h>
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +020050#include <trace/events/xdp.h>
John Fastabend546ac1f2017-07-17 09:28:56 -070051
Chenbo Feng6e71b042017-10-18 13:00:22 -070052#define DEV_CREATE_FLAG_MASK \
53 (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
54
Toke Høiland-Jørgensen75ccae62020-01-16 16:14:44 +010055struct xdp_dev_bulk_queue {
Jesper Dangaard Brouer5d053f92018-05-24 16:45:51 +020056 struct xdp_frame *q[DEV_MAP_BULK_SIZE];
Toke Høiland-Jørgensend5df2832019-06-28 11:12:34 +020057 struct list_head flush_node;
Toke Høiland-Jørgensen75ccae62020-01-16 16:14:44 +010058 struct net_device *dev;
Jesper Dangaard Brouer38edddb2018-05-24 16:45:57 +020059 struct net_device *dev_rx;
Jesper Dangaard Brouer5d053f92018-05-24 16:45:51 +020060 unsigned int count;
61};
62
David Ahern7f1c0422020-05-29 16:07:12 -060063/* DEVMAP values */
64struct bpf_devmap_val {
65 u32 ifindex; /* device index */
David Ahernfbee97f2020-05-29 16:07:13 -060066 union {
67 int fd; /* prog fd on map write */
68 u32 id; /* prog id on map read */
69 } bpf_prog;
David Ahern7f1c0422020-05-29 16:07:12 -060070};
71
John Fastabend546ac1f2017-07-17 09:28:56 -070072struct bpf_dtab_netdev {
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +020073 struct net_device *dev; /* must be first member, due to tracepoint */
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +020074 struct hlist_node index_hlist;
John Fastabend546ac1f2017-07-17 09:28:56 -070075 struct bpf_dtab *dtab;
David Ahernfbee97f2020-05-29 16:07:13 -060076 struct bpf_prog *xdp_prog;
Daniel Borkmannaf4d0452017-08-23 01:47:54 +020077 struct rcu_head rcu;
Toke Høiland-Jørgensen75ccae62020-01-16 16:14:44 +010078 unsigned int idx;
David Ahern7f1c0422020-05-29 16:07:12 -060079 struct bpf_devmap_val val;
John Fastabend546ac1f2017-07-17 09:28:56 -070080};
81
82struct bpf_dtab {
83 struct bpf_map map;
Toke Høiland-Jørgensen071cdec2019-11-21 14:36:12 +010084 struct bpf_dtab_netdev **netdev_map; /* DEVMAP type only */
John Fastabend2ddf71e2017-07-17 09:30:02 -070085 struct list_head list;
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +020086
87 /* these are only used for DEVMAP_HASH type maps */
88 struct hlist_head *dev_index_head;
89 spinlock_t index_lock;
90 unsigned int items;
91 u32 n_buckets;
John Fastabend546ac1f2017-07-17 09:28:56 -070092};
93
Toke Høiland-Jørgensen1d233882020-01-16 16:14:45 +010094static DEFINE_PER_CPU(struct list_head, dev_flush_list);
John Fastabend4cc7b952017-08-04 22:02:19 -070095static DEFINE_SPINLOCK(dev_map_lock);
John Fastabend2ddf71e2017-07-17 09:30:02 -070096static LIST_HEAD(dev_map_list);
97
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +020098static struct hlist_head *dev_map_create_hash(unsigned int entries)
99{
100 int i;
101 struct hlist_head *hash;
102
103 hash = kmalloc_array(entries, sizeof(*hash), GFP_KERNEL);
104 if (hash != NULL)
105 for (i = 0; i < entries; i++)
106 INIT_HLIST_HEAD(&hash[i]);
107
108 return hash;
109}
110
Toke Høiland-Jørgensen071cdec2019-11-21 14:36:12 +0100111static inline struct hlist_head *dev_map_index_hash(struct bpf_dtab *dtab,
112 int idx)
113{
114 return &dtab->dev_index_head[idx & (dtab->n_buckets - 1)];
115}
116
Toke Høiland-Jørgensenfca16e52019-07-26 18:06:53 +0200117static int dev_map_init_map(struct bpf_dtab *dtab, union bpf_attr *attr)
John Fastabend546ac1f2017-07-17 09:28:56 -0700118{
David Ahernfbee97f2020-05-29 16:07:13 -0600119 u32 valsize = attr->value_size;
Björn Töpel96360002019-12-19 07:10:03 +0100120 u64 cost = 0;
121 int err;
John Fastabend546ac1f2017-07-17 09:28:56 -0700122
David Ahernfbee97f2020-05-29 16:07:13 -0600123 /* check sanity of attributes. 2 value sizes supported:
124 * 4 bytes: ifindex
125 * 8 bytes: ifindex + prog fd
126 */
John Fastabend546ac1f2017-07-17 09:28:56 -0700127 if (attr->max_entries == 0 || attr->key_size != 4 ||
David Ahernfbee97f2020-05-29 16:07:13 -0600128 (valsize != offsetofend(struct bpf_devmap_val, ifindex) &&
129 valsize != offsetofend(struct bpf_devmap_val, bpf_prog.fd)) ||
130 attr->map_flags & ~DEV_CREATE_FLAG_MASK)
Toke Høiland-Jørgensenfca16e52019-07-26 18:06:53 +0200131 return -EINVAL;
John Fastabend546ac1f2017-07-17 09:28:56 -0700132
Toke Høiland-Jørgensen0cdbb4b2019-06-28 11:12:35 +0200133 /* Lookup returns a pointer straight to dev->ifindex, so make sure the
134 * verifier prevents writes from the BPF side
135 */
136 attr->map_flags |= BPF_F_RDONLY_PROG;
137
John Fastabend546ac1f2017-07-17 09:28:56 -0700138
Jakub Kicinskibd475642018-01-11 20:29:06 -0800139 bpf_map_init_from_attr(&dtab->map, attr);
John Fastabend546ac1f2017-07-17 09:28:56 -0700140
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +0200141 if (attr->map_type == BPF_MAP_TYPE_DEVMAP_HASH) {
142 dtab->n_buckets = roundup_pow_of_two(dtab->map.max_entries);
143
144 if (!dtab->n_buckets) /* Overflow check */
145 return -EINVAL;
Toke Høiland-Jørgensen05679ca2019-10-17 12:57:02 +0200146 cost += (u64) sizeof(struct hlist_head) * dtab->n_buckets;
Toke Høiland-Jørgensen071cdec2019-11-21 14:36:12 +0100147 } else {
148 cost += (u64) dtab->map.max_entries * sizeof(struct bpf_dtab_netdev *);
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +0200149 }
150
Roman Gushchinb936ca62019-05-29 18:03:58 -0700151 /* if map size is larger than memlock limit, reject it */
Roman Gushchinc85d6912019-05-29 18:03:59 -0700152 err = bpf_map_charge_init(&dtab->map.memory, cost);
John Fastabend546ac1f2017-07-17 09:28:56 -0700153 if (err)
Toke Høiland-Jørgensenfca16e52019-07-26 18:06:53 +0200154 return -EINVAL;
Tobias Klauser582db7e2017-09-18 15:03:46 +0200155
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +0200156 if (attr->map_type == BPF_MAP_TYPE_DEVMAP_HASH) {
157 dtab->dev_index_head = dev_map_create_hash(dtab->n_buckets);
158 if (!dtab->dev_index_head)
Björn Töpel96360002019-12-19 07:10:03 +0100159 goto free_charge;
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +0200160
161 spin_lock_init(&dtab->index_lock);
Toke Høiland-Jørgensen071cdec2019-11-21 14:36:12 +0100162 } else {
163 dtab->netdev_map = bpf_map_area_alloc(dtab->map.max_entries *
164 sizeof(struct bpf_dtab_netdev *),
165 dtab->map.numa_node);
166 if (!dtab->netdev_map)
Björn Töpel96360002019-12-19 07:10:03 +0100167 goto free_charge;
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +0200168 }
169
Toke Høiland-Jørgensenfca16e52019-07-26 18:06:53 +0200170 return 0;
Toke Høiland-Jørgensend5df2832019-06-28 11:12:34 +0200171
Roman Gushchinb936ca62019-05-29 18:03:58 -0700172free_charge:
173 bpf_map_charge_finish(&dtab->map.memory);
Toke Høiland-Jørgensenfca16e52019-07-26 18:06:53 +0200174 return -ENOMEM;
175}
176
177static struct bpf_map *dev_map_alloc(union bpf_attr *attr)
178{
179 struct bpf_dtab *dtab;
180 int err;
181
182 if (!capable(CAP_NET_ADMIN))
183 return ERR_PTR(-EPERM);
184
185 dtab = kzalloc(sizeof(*dtab), GFP_USER);
186 if (!dtab)
187 return ERR_PTR(-ENOMEM);
188
189 err = dev_map_init_map(dtab, attr);
190 if (err) {
191 kfree(dtab);
192 return ERR_PTR(err);
193 }
194
195 spin_lock(&dev_map_lock);
196 list_add_tail_rcu(&dtab->list, &dev_map_list);
197 spin_unlock(&dev_map_lock);
198
199 return &dtab->map;
John Fastabend546ac1f2017-07-17 09:28:56 -0700200}
201
202static void dev_map_free(struct bpf_map *map)
203{
204 struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
Björn Töpel0536b852019-12-19 07:09:59 +0100205 int i;
John Fastabend546ac1f2017-07-17 09:28:56 -0700206
207 /* At this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
208 * so the programs (can be more than one that used this map) were
John Fastabend42a84a82020-01-26 16:14:00 -0800209 * disconnected from events. The following synchronize_rcu() guarantees
210 * both rcu read critical sections complete and waits for
211 * preempt-disable regions (NAPI being the relevant context here) so we
212 * are certain there will be no further reads against the netdev_map and
213 * all flush operations are complete. Flush operations can only be done
214 * from NAPI context for this reason.
John Fastabend546ac1f2017-07-17 09:28:56 -0700215 */
Daniel Borkmann274043c2017-08-21 01:48:12 +0200216
217 spin_lock(&dev_map_lock);
218 list_del_rcu(&dtab->list);
219 spin_unlock(&dev_map_lock);
220
Daniel Borkmannf6069b92018-08-17 23:26:14 +0200221 bpf_clear_redirect_map(map);
John Fastabend546ac1f2017-07-17 09:28:56 -0700222 synchronize_rcu();
223
Eric Dumazet2baae352019-05-13 09:59:16 -0700224 /* Make sure prior __dev_map_entry_free() have completed. */
225 rcu_barrier();
226
Toke Høiland-Jørgensen071cdec2019-11-21 14:36:12 +0100227 if (dtab->map.map_type == BPF_MAP_TYPE_DEVMAP_HASH) {
228 for (i = 0; i < dtab->n_buckets; i++) {
229 struct bpf_dtab_netdev *dev;
230 struct hlist_head *head;
231 struct hlist_node *next;
John Fastabend546ac1f2017-07-17 09:28:56 -0700232
Toke Høiland-Jørgensen071cdec2019-11-21 14:36:12 +0100233 head = dev_map_index_hash(dtab, i);
John Fastabend546ac1f2017-07-17 09:28:56 -0700234
Toke Høiland-Jørgensen071cdec2019-11-21 14:36:12 +0100235 hlist_for_each_entry_safe(dev, next, head, index_hlist) {
236 hlist_del_rcu(&dev->index_hlist);
David Ahernfbee97f2020-05-29 16:07:13 -0600237 if (dev->xdp_prog)
238 bpf_prog_put(dev->xdp_prog);
Toke Høiland-Jørgensen071cdec2019-11-21 14:36:12 +0100239 dev_put(dev->dev);
240 kfree(dev);
241 }
242 }
243
244 kfree(dtab->dev_index_head);
245 } else {
246 for (i = 0; i < dtab->map.max_entries; i++) {
247 struct bpf_dtab_netdev *dev;
248
249 dev = dtab->netdev_map[i];
250 if (!dev)
251 continue;
252
David Ahernfbee97f2020-05-29 16:07:13 -0600253 if (dev->xdp_prog)
254 bpf_prog_put(dev->xdp_prog);
Toke Høiland-Jørgensen071cdec2019-11-21 14:36:12 +0100255 dev_put(dev->dev);
256 kfree(dev);
257 }
258
259 bpf_map_area_free(dtab->netdev_map);
John Fastabend546ac1f2017-07-17 09:28:56 -0700260 }
261
John Fastabend546ac1f2017-07-17 09:28:56 -0700262 kfree(dtab);
263}
264
265static int dev_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
266{
267 struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
268 u32 index = key ? *(u32 *)key : U32_MAX;
Daniel Borkmannaf4d0452017-08-23 01:47:54 +0200269 u32 *next = next_key;
John Fastabend546ac1f2017-07-17 09:28:56 -0700270
271 if (index >= dtab->map.max_entries) {
272 *next = 0;
273 return 0;
274 }
275
276 if (index == dtab->map.max_entries - 1)
277 return -ENOENT;
John Fastabend546ac1f2017-07-17 09:28:56 -0700278 *next = index + 1;
279 return 0;
280}
281
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +0200282struct bpf_dtab_netdev *__dev_map_hash_lookup_elem(struct bpf_map *map, u32 key)
283{
284 struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
285 struct hlist_head *head = dev_map_index_hash(dtab, key);
286 struct bpf_dtab_netdev *dev;
287
Amol Grover485ec2e2020-01-23 17:34:38 +0530288 hlist_for_each_entry_rcu(dev, head, index_hlist,
289 lockdep_is_held(&dtab->index_lock))
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +0200290 if (dev->idx == key)
291 return dev;
292
293 return NULL;
294}
295
296static int dev_map_hash_get_next_key(struct bpf_map *map, void *key,
297 void *next_key)
298{
299 struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
300 u32 idx, *next = next_key;
301 struct bpf_dtab_netdev *dev, *next_dev;
302 struct hlist_head *head;
303 int i = 0;
304
305 if (!key)
306 goto find_first;
307
308 idx = *(u32 *)key;
309
310 dev = __dev_map_hash_lookup_elem(map, idx);
311 if (!dev)
312 goto find_first;
313
314 next_dev = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&dev->index_hlist)),
315 struct bpf_dtab_netdev, index_hlist);
316
317 if (next_dev) {
318 *next = next_dev->idx;
319 return 0;
320 }
321
322 i = idx & (dtab->n_buckets - 1);
323 i++;
324
325 find_first:
326 for (; i < dtab->n_buckets; i++) {
327 head = dev_map_index_hash(dtab, i);
328
329 next_dev = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),
330 struct bpf_dtab_netdev,
331 index_hlist);
332 if (next_dev) {
333 *next = next_dev->idx;
334 return 0;
335 }
336 }
337
338 return -ENOENT;
339}
340
David Ahernfbee97f2020-05-29 16:07:13 -0600341bool dev_map_can_have_prog(struct bpf_map *map)
342{
343 if ((map->map_type == BPF_MAP_TYPE_DEVMAP ||
344 map->map_type == BPF_MAP_TYPE_DEVMAP_HASH) &&
345 map->value_size != offsetofend(struct bpf_devmap_val, ifindex))
346 return true;
347
348 return false;
349}
350
Toke Høiland-Jørgensen75ccae62020-01-16 16:14:44 +0100351static int bq_xmit_all(struct xdp_dev_bulk_queue *bq, u32 flags)
Jesper Dangaard Brouer5d053f92018-05-24 16:45:51 +0200352{
Toke Høiland-Jørgensen75ccae62020-01-16 16:14:44 +0100353 struct net_device *dev = bq->dev;
Jesper Dangaard Brouere74de522018-05-24 16:46:17 +0200354 int sent = 0, drops = 0, err = 0;
Jesper Dangaard Brouer5d053f92018-05-24 16:45:51 +0200355 int i;
356
357 if (unlikely(!bq->count))
358 return 0;
359
360 for (i = 0; i < bq->count; i++) {
361 struct xdp_frame *xdpf = bq->q[i];
362
363 prefetch(xdpf);
364 }
365
Jesper Dangaard Brouerc1ece6b2018-05-31 11:00:23 +0200366 sent = dev->netdev_ops->ndo_xdp_xmit(dev, bq->count, bq->q, flags);
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200367 if (sent < 0) {
Jesper Dangaard Brouere74de522018-05-24 16:46:17 +0200368 err = sent;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200369 sent = 0;
370 goto error;
Jesper Dangaard Brouer5d053f92018-05-24 16:45:51 +0200371 }
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200372 drops = bq->count - sent;
373out:
Jesper Dangaard Brouer5d053f92018-05-24 16:45:51 +0200374 bq->count = 0;
375
Jesper Dangaard Brouer58aa94f2020-01-16 16:14:46 +0100376 trace_xdp_devmap_xmit(bq->dev_rx, dev, sent, drops, err);
Jesper Dangaard Brouer38edddb2018-05-24 16:45:57 +0200377 bq->dev_rx = NULL;
Toke Høiland-Jørgensend5df2832019-06-28 11:12:34 +0200378 __list_del_clearprev(&bq->flush_node);
Jesper Dangaard Brouer5d053f92018-05-24 16:45:51 +0200379 return 0;
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200380error:
381 /* If ndo_xdp_xmit fails with an errno, no frames have been
382 * xmit'ed and it's our responsibility to them free all.
383 */
384 for (i = 0; i < bq->count; i++) {
385 struct xdp_frame *xdpf = bq->q[i];
386
Björn Töpel0536b852019-12-19 07:09:59 +0100387 xdp_return_frame_rx_napi(xdpf);
Jesper Dangaard Brouer735fc402018-05-24 16:46:12 +0200388 drops++;
389 }
390 goto out;
Jesper Dangaard Brouer5d053f92018-05-24 16:45:51 +0200391}
392
Toke Høiland-Jørgensen1d233882020-01-16 16:14:45 +0100393/* __dev_flush is called from xdp_do_flush() which _must_ be signaled
John Fastabend11393cc2017-07-17 09:29:40 -0700394 * from the driver before returning from its napi->poll() routine. The poll()
395 * routine is called either from busy_poll context or net_rx_action signaled
396 * from NET_RX_SOFTIRQ. Either way the poll routine must complete before the
Toke Høiland-Jørgensend5df2832019-06-28 11:12:34 +0200397 * net device can be torn down. On devmap tear down we ensure the flush list
398 * is empty before completing to ensure all flush operations have completed.
John Fastabendb23bfa52020-01-26 16:14:02 -0800399 * When drivers update the bpf program they may need to ensure any flush ops
400 * are also complete. Using synchronize_rcu or call_rcu will suffice for this
401 * because both wait for napi context to exit.
John Fastabend11393cc2017-07-17 09:29:40 -0700402 */
Toke Høiland-Jørgensen1d233882020-01-16 16:14:45 +0100403void __dev_flush(void)
John Fastabend11393cc2017-07-17 09:29:40 -0700404{
Toke Høiland-Jørgensen1d233882020-01-16 16:14:45 +0100405 struct list_head *flush_list = this_cpu_ptr(&dev_flush_list);
Toke Høiland-Jørgensen75ccae62020-01-16 16:14:44 +0100406 struct xdp_dev_bulk_queue *bq, *tmp;
John Fastabend11393cc2017-07-17 09:29:40 -0700407
Toke Høiland-Jørgensend5df2832019-06-28 11:12:34 +0200408 list_for_each_entry_safe(bq, tmp, flush_list, flush_node)
Björn Töpel0536b852019-12-19 07:09:59 +0100409 bq_xmit_all(bq, XDP_XMIT_FLUSH);
John Fastabend11393cc2017-07-17 09:29:40 -0700410}
411
John Fastabend546ac1f2017-07-17 09:28:56 -0700412/* rcu_read_lock (from syscall and BPF contexts) ensures that if a delete and/or
413 * update happens in parallel here a dev_put wont happen until after reading the
414 * ifindex.
415 */
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +0200416struct bpf_dtab_netdev *__dev_map_lookup_elem(struct bpf_map *map, u32 key)
John Fastabend546ac1f2017-07-17 09:28:56 -0700417{
418 struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +0200419 struct bpf_dtab_netdev *obj;
John Fastabend546ac1f2017-07-17 09:28:56 -0700420
Daniel Borkmannaf4d0452017-08-23 01:47:54 +0200421 if (key >= map->max_entries)
John Fastabend546ac1f2017-07-17 09:28:56 -0700422 return NULL;
423
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +0200424 obj = READ_ONCE(dtab->netdev_map[key]);
425 return obj;
426}
427
Jesper Dangaard Brouer5d053f92018-05-24 16:45:51 +0200428/* Runs under RCU-read-side, plus in softirq under NAPI protection.
429 * Thus, safe percpu variable access.
430 */
Toke Høiland-Jørgensen75ccae62020-01-16 16:14:44 +0100431static int bq_enqueue(struct net_device *dev, struct xdp_frame *xdpf,
Jesper Dangaard Brouer38edddb2018-05-24 16:45:57 +0200432 struct net_device *dev_rx)
Jesper Dangaard Brouer5d053f92018-05-24 16:45:51 +0200433{
Toke Høiland-Jørgensen1d233882020-01-16 16:14:45 +0100434 struct list_head *flush_list = this_cpu_ptr(&dev_flush_list);
Toke Høiland-Jørgensen75ccae62020-01-16 16:14:44 +0100435 struct xdp_dev_bulk_queue *bq = this_cpu_ptr(dev->xdp_bulkq);
Jesper Dangaard Brouer5d053f92018-05-24 16:45:51 +0200436
437 if (unlikely(bq->count == DEV_MAP_BULK_SIZE))
Björn Töpel0536b852019-12-19 07:09:59 +0100438 bq_xmit_all(bq, 0);
Jesper Dangaard Brouer5d053f92018-05-24 16:45:51 +0200439
Jesper Dangaard Brouer38edddb2018-05-24 16:45:57 +0200440 /* Ingress dev_rx will be the same for all xdp_frame's in
441 * bulk_queue, because bq stored per-CPU and must be flushed
442 * from net_device drivers NAPI func end.
443 */
444 if (!bq->dev_rx)
445 bq->dev_rx = dev_rx;
446
Jesper Dangaard Brouer5d053f92018-05-24 16:45:51 +0200447 bq->q[bq->count++] = xdpf;
Toke Høiland-Jørgensend5df2832019-06-28 11:12:34 +0200448
449 if (!bq->flush_node.prev)
450 list_add(&bq->flush_node, flush_list);
451
Jesper Dangaard Brouer5d053f92018-05-24 16:45:51 +0200452 return 0;
453}
454
Toke Høiland-Jørgensen1d233882020-01-16 16:14:45 +0100455static inline int __xdp_enqueue(struct net_device *dev, struct xdp_buff *xdp,
456 struct net_device *dev_rx)
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +0200457{
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +0200458 struct xdp_frame *xdpf;
Toshiaki Makitad8d72182018-07-06 11:49:00 +0900459 int err;
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +0200460
461 if (!dev->netdev_ops->ndo_xdp_xmit)
462 return -EOPNOTSUPP;
463
Toshiaki Makitad8d72182018-07-06 11:49:00 +0900464 err = xdp_ok_fwd_dev(dev, xdp->data_end - xdp->data);
465 if (unlikely(err))
466 return err;
467
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +0200468 xdpf = convert_to_xdp_frame(xdp);
469 if (unlikely(!xdpf))
470 return -EOVERFLOW;
471
Toke Høiland-Jørgensen75ccae62020-01-16 16:14:44 +0100472 return bq_enqueue(dev, xdpf, dev_rx);
John Fastabend546ac1f2017-07-17 09:28:56 -0700473}
474
David Ahernfbee97f2020-05-29 16:07:13 -0600475static struct xdp_buff *dev_map_run_prog(struct net_device *dev,
476 struct xdp_buff *xdp,
477 struct bpf_prog *xdp_prog)
478{
479 u32 act;
480
481 act = bpf_prog_run_xdp(xdp_prog, xdp);
482 switch (act) {
483 case XDP_PASS:
484 return xdp;
485 case XDP_DROP:
486 break;
487 default:
488 bpf_warn_invalid_xdp_action(act);
489 fallthrough;
490 case XDP_ABORTED:
491 trace_xdp_exception(dev, xdp_prog, act);
492 break;
493 }
494
495 xdp_return_buff(xdp);
496 return NULL;
497}
498
Toke Høiland-Jørgensen1d233882020-01-16 16:14:45 +0100499int dev_xdp_enqueue(struct net_device *dev, struct xdp_buff *xdp,
500 struct net_device *dev_rx)
501{
502 return __xdp_enqueue(dev, xdp, dev_rx);
503}
504
505int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
506 struct net_device *dev_rx)
507{
508 struct net_device *dev = dst->dev;
509
David Ahernfbee97f2020-05-29 16:07:13 -0600510 if (dst->xdp_prog) {
511 xdp = dev_map_run_prog(dev, xdp, dst->xdp_prog);
512 if (!xdp)
513 return 0;
514 }
Toke Høiland-Jørgensen1d233882020-01-16 16:14:45 +0100515 return __xdp_enqueue(dev, xdp, dev_rx);
516}
517
Toshiaki Makita6d5fc192018-06-14 11:07:42 +0900518int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
519 struct bpf_prog *xdp_prog)
520{
521 int err;
522
Toshiaki Makitad8d72182018-07-06 11:49:00 +0900523 err = xdp_ok_fwd_dev(dst->dev, skb->len);
Toshiaki Makita6d5fc192018-06-14 11:07:42 +0900524 if (unlikely(err))
525 return err;
526 skb->dev = dst->dev;
527 generic_xdp_tx(skb, xdp_prog);
528
529 return 0;
530}
531
Daniel Borkmannaf4d0452017-08-23 01:47:54 +0200532static void *dev_map_lookup_elem(struct bpf_map *map, void *key)
John Fastabend11393cc2017-07-17 09:29:40 -0700533{
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +0200534 struct bpf_dtab_netdev *obj = __dev_map_lookup_elem(map, *(u32 *)key);
Daniel Borkmannaf4d0452017-08-23 01:47:54 +0200535
David Ahern7f1c0422020-05-29 16:07:12 -0600536 return obj ? &obj->val : NULL;
Daniel Borkmannaf4d0452017-08-23 01:47:54 +0200537}
538
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +0200539static void *dev_map_hash_lookup_elem(struct bpf_map *map, void *key)
540{
541 struct bpf_dtab_netdev *obj = __dev_map_hash_lookup_elem(map,
542 *(u32 *)key);
David Ahern7f1c0422020-05-29 16:07:12 -0600543 return obj ? &obj->val : NULL;
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +0200544}
545
John Fastabend546ac1f2017-07-17 09:28:56 -0700546static void __dev_map_entry_free(struct rcu_head *rcu)
547{
Daniel Borkmannaf4d0452017-08-23 01:47:54 +0200548 struct bpf_dtab_netdev *dev;
John Fastabend546ac1f2017-07-17 09:28:56 -0700549
Daniel Borkmannaf4d0452017-08-23 01:47:54 +0200550 dev = container_of(rcu, struct bpf_dtab_netdev, rcu);
David Ahernfbee97f2020-05-29 16:07:13 -0600551 if (dev->xdp_prog)
552 bpf_prog_put(dev->xdp_prog);
Daniel Borkmannaf4d0452017-08-23 01:47:54 +0200553 dev_put(dev->dev);
554 kfree(dev);
John Fastabend546ac1f2017-07-17 09:28:56 -0700555}
556
557static int dev_map_delete_elem(struct bpf_map *map, void *key)
558{
559 struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
560 struct bpf_dtab_netdev *old_dev;
561 int k = *(u32 *)key;
562
563 if (k >= map->max_entries)
564 return -EINVAL;
565
Daniel Borkmannaf4d0452017-08-23 01:47:54 +0200566 /* Use call_rcu() here to ensure any rcu critical sections have
John Fastabend42a84a82020-01-26 16:14:00 -0800567 * completed as well as any flush operations because call_rcu
568 * will wait for preempt-disable region to complete, NAPI in this
569 * context. And additionally, the driver tear down ensures all
570 * soft irqs are complete before removing the net device in the
571 * case of dev_put equals zero.
John Fastabend546ac1f2017-07-17 09:28:56 -0700572 */
573 old_dev = xchg(&dtab->netdev_map[k], NULL);
574 if (old_dev)
575 call_rcu(&old_dev->rcu, __dev_map_entry_free);
576 return 0;
577}
578
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +0200579static int dev_map_hash_delete_elem(struct bpf_map *map, void *key)
580{
581 struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
582 struct bpf_dtab_netdev *old_dev;
583 int k = *(u32 *)key;
584 unsigned long flags;
585 int ret = -ENOENT;
586
587 spin_lock_irqsave(&dtab->index_lock, flags);
588
589 old_dev = __dev_map_hash_lookup_elem(map, k);
590 if (old_dev) {
591 dtab->items--;
592 hlist_del_init_rcu(&old_dev->index_hlist);
593 call_rcu(&old_dev->rcu, __dev_map_entry_free);
594 ret = 0;
595 }
596 spin_unlock_irqrestore(&dtab->index_lock, flags);
597
598 return ret;
599}
600
Toke Høiland-Jørgensenfca16e52019-07-26 18:06:53 +0200601static struct bpf_dtab_netdev *__dev_map_alloc_node(struct net *net,
602 struct bpf_dtab *dtab,
David Ahern7f1c0422020-05-29 16:07:12 -0600603 struct bpf_devmap_val *val,
Toke Høiland-Jørgensenfca16e52019-07-26 18:06:53 +0200604 unsigned int idx)
605{
David Ahernfbee97f2020-05-29 16:07:13 -0600606 struct bpf_prog *prog = NULL;
Toke Høiland-Jørgensenfca16e52019-07-26 18:06:53 +0200607 struct bpf_dtab_netdev *dev;
Toke Høiland-Jørgensenfca16e52019-07-26 18:06:53 +0200608
Toke Høiland-Jørgensen75ccae62020-01-16 16:14:44 +0100609 dev = kmalloc_node(sizeof(*dev), GFP_ATOMIC | __GFP_NOWARN,
610 dtab->map.numa_node);
Toke Høiland-Jørgensenfca16e52019-07-26 18:06:53 +0200611 if (!dev)
612 return ERR_PTR(-ENOMEM);
613
David Ahern7f1c0422020-05-29 16:07:12 -0600614 dev->dev = dev_get_by_index(net, val->ifindex);
615 if (!dev->dev)
616 goto err_out;
Toke Høiland-Jørgensenfca16e52019-07-26 18:06:53 +0200617
David Ahernfbee97f2020-05-29 16:07:13 -0600618 if (val->bpf_prog.fd >= 0) {
619 prog = bpf_prog_get_type_dev(val->bpf_prog.fd,
620 BPF_PROG_TYPE_XDP, false);
621 if (IS_ERR(prog))
622 goto err_put_dev;
623 if (prog->expected_attach_type != BPF_XDP_DEVMAP)
624 goto err_put_prog;
625 }
626
Toke Høiland-Jørgensenfca16e52019-07-26 18:06:53 +0200627 dev->idx = idx;
628 dev->dtab = dtab;
David Ahernfbee97f2020-05-29 16:07:13 -0600629 if (prog) {
630 dev->xdp_prog = prog;
631 dev->val.bpf_prog.id = prog->aux->id;
632 } else {
633 dev->xdp_prog = NULL;
634 dev->val.bpf_prog.id = 0;
635 }
David Ahern7f1c0422020-05-29 16:07:12 -0600636 dev->val.ifindex = val->ifindex;
Toke Høiland-Jørgensenfca16e52019-07-26 18:06:53 +0200637
638 return dev;
David Ahernfbee97f2020-05-29 16:07:13 -0600639err_put_prog:
640 bpf_prog_put(prog);
641err_put_dev:
642 dev_put(dev->dev);
David Ahern7f1c0422020-05-29 16:07:12 -0600643err_out:
644 kfree(dev);
645 return ERR_PTR(-EINVAL);
Toke Høiland-Jørgensenfca16e52019-07-26 18:06:53 +0200646}
647
648static int __dev_map_update_elem(struct net *net, struct bpf_map *map,
649 void *key, void *value, u64 map_flags)
John Fastabend546ac1f2017-07-17 09:28:56 -0700650{
651 struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
David Ahernfbee97f2020-05-29 16:07:13 -0600652 struct bpf_devmap_val val = { .bpf_prog.fd = -1 };
John Fastabend546ac1f2017-07-17 09:28:56 -0700653 struct bpf_dtab_netdev *dev, *old_dev;
Toke Høiland-Jørgensend5df2832019-06-28 11:12:34 +0200654 u32 i = *(u32 *)key;
John Fastabend546ac1f2017-07-17 09:28:56 -0700655
656 if (unlikely(map_flags > BPF_EXIST))
657 return -EINVAL;
John Fastabend546ac1f2017-07-17 09:28:56 -0700658 if (unlikely(i >= dtab->map.max_entries))
659 return -E2BIG;
John Fastabend546ac1f2017-07-17 09:28:56 -0700660 if (unlikely(map_flags == BPF_NOEXIST))
661 return -EEXIST;
662
David Ahern7f1c0422020-05-29 16:07:12 -0600663 /* already verified value_size <= sizeof val */
664 memcpy(&val, value, map->value_size);
665
666 if (!val.ifindex) {
John Fastabend546ac1f2017-07-17 09:28:56 -0700667 dev = NULL;
David Ahernfbee97f2020-05-29 16:07:13 -0600668 /* can not specify fd if ifindex is 0 */
669 if (val.bpf_prog.fd != -1)
670 return -EINVAL;
John Fastabend546ac1f2017-07-17 09:28:56 -0700671 } else {
David Ahern7f1c0422020-05-29 16:07:12 -0600672 dev = __dev_map_alloc_node(net, dtab, &val, i);
Toke Høiland-Jørgensenfca16e52019-07-26 18:06:53 +0200673 if (IS_ERR(dev))
674 return PTR_ERR(dev);
John Fastabend546ac1f2017-07-17 09:28:56 -0700675 }
676
677 /* Use call_rcu() here to ensure rcu critical sections have completed
678 * Remembering the driver side flush operation will happen before the
679 * net device is removed.
680 */
681 old_dev = xchg(&dtab->netdev_map[i], dev);
682 if (old_dev)
683 call_rcu(&old_dev->rcu, __dev_map_entry_free);
684
685 return 0;
686}
687
Toke Høiland-Jørgensenfca16e52019-07-26 18:06:53 +0200688static int dev_map_update_elem(struct bpf_map *map, void *key, void *value,
689 u64 map_flags)
690{
691 return __dev_map_update_elem(current->nsproxy->net_ns,
692 map, key, value, map_flags);
693}
694
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +0200695static int __dev_map_hash_update_elem(struct net *net, struct bpf_map *map,
696 void *key, void *value, u64 map_flags)
697{
698 struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
David Ahernfbee97f2020-05-29 16:07:13 -0600699 struct bpf_devmap_val val = { .bpf_prog.fd = -1 };
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +0200700 struct bpf_dtab_netdev *dev, *old_dev;
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +0200701 u32 idx = *(u32 *)key;
702 unsigned long flags;
Toke Høiland-Jørgensenaf58e7e2019-09-08 09:20:16 +0100703 int err = -EEXIST;
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +0200704
David Ahern7f1c0422020-05-29 16:07:12 -0600705 /* already verified value_size <= sizeof val */
706 memcpy(&val, value, map->value_size);
707
708 if (unlikely(map_flags > BPF_EXIST || !val.ifindex))
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +0200709 return -EINVAL;
710
Toke Høiland-Jørgensenaf58e7e2019-09-08 09:20:16 +0100711 spin_lock_irqsave(&dtab->index_lock, flags);
712
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +0200713 old_dev = __dev_map_hash_lookup_elem(map, idx);
714 if (old_dev && (map_flags & BPF_NOEXIST))
Toke Høiland-Jørgensenaf58e7e2019-09-08 09:20:16 +0100715 goto out_err;
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +0200716
David Ahern7f1c0422020-05-29 16:07:12 -0600717 dev = __dev_map_alloc_node(net, dtab, &val, idx);
Toke Høiland-Jørgensenaf58e7e2019-09-08 09:20:16 +0100718 if (IS_ERR(dev)) {
719 err = PTR_ERR(dev);
720 goto out_err;
721 }
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +0200722
723 if (old_dev) {
724 hlist_del_rcu(&old_dev->index_hlist);
725 } else {
726 if (dtab->items >= dtab->map.max_entries) {
727 spin_unlock_irqrestore(&dtab->index_lock, flags);
728 call_rcu(&dev->rcu, __dev_map_entry_free);
729 return -E2BIG;
730 }
731 dtab->items++;
732 }
733
734 hlist_add_head_rcu(&dev->index_hlist,
735 dev_map_index_hash(dtab, idx));
736 spin_unlock_irqrestore(&dtab->index_lock, flags);
737
738 if (old_dev)
739 call_rcu(&old_dev->rcu, __dev_map_entry_free);
740
741 return 0;
Toke Høiland-Jørgensenaf58e7e2019-09-08 09:20:16 +0100742
743out_err:
744 spin_unlock_irqrestore(&dtab->index_lock, flags);
745 return err;
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +0200746}
747
748static int dev_map_hash_update_elem(struct bpf_map *map, void *key, void *value,
749 u64 map_flags)
750{
751 return __dev_map_hash_update_elem(current->nsproxy->net_ns,
752 map, key, value, map_flags);
753}
754
John Fastabend546ac1f2017-07-17 09:28:56 -0700755const struct bpf_map_ops dev_map_ops = {
756 .map_alloc = dev_map_alloc,
757 .map_free = dev_map_free,
758 .map_get_next_key = dev_map_get_next_key,
759 .map_lookup_elem = dev_map_lookup_elem,
760 .map_update_elem = dev_map_update_elem,
761 .map_delete_elem = dev_map_delete_elem,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200762 .map_check_btf = map_check_no_btf,
John Fastabend546ac1f2017-07-17 09:28:56 -0700763};
John Fastabend2ddf71e2017-07-17 09:30:02 -0700764
Toke Høiland-Jørgensen6f9d4512019-07-26 18:06:55 +0200765const struct bpf_map_ops dev_map_hash_ops = {
766 .map_alloc = dev_map_alloc,
767 .map_free = dev_map_free,
768 .map_get_next_key = dev_map_hash_get_next_key,
769 .map_lookup_elem = dev_map_hash_lookup_elem,
770 .map_update_elem = dev_map_hash_update_elem,
771 .map_delete_elem = dev_map_hash_delete_elem,
772 .map_check_btf = map_check_no_btf,
773};
774
Toke Høiland-Jørgensence197d82019-10-19 13:19:31 +0200775static void dev_map_hash_remove_netdev(struct bpf_dtab *dtab,
776 struct net_device *netdev)
777{
778 unsigned long flags;
779 u32 i;
780
781 spin_lock_irqsave(&dtab->index_lock, flags);
782 for (i = 0; i < dtab->n_buckets; i++) {
783 struct bpf_dtab_netdev *dev;
784 struct hlist_head *head;
785 struct hlist_node *next;
786
787 head = dev_map_index_hash(dtab, i);
788
789 hlist_for_each_entry_safe(dev, next, head, index_hlist) {
790 if (netdev != dev->dev)
791 continue;
792
793 dtab->items--;
794 hlist_del_rcu(&dev->index_hlist);
795 call_rcu(&dev->rcu, __dev_map_entry_free);
796 }
797 }
798 spin_unlock_irqrestore(&dtab->index_lock, flags);
799}
800
John Fastabend2ddf71e2017-07-17 09:30:02 -0700801static int dev_map_notification(struct notifier_block *notifier,
802 ulong event, void *ptr)
803{
804 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
805 struct bpf_dtab *dtab;
Toke Høiland-Jørgensen75ccae62020-01-16 16:14:44 +0100806 int i, cpu;
John Fastabend2ddf71e2017-07-17 09:30:02 -0700807
808 switch (event) {
Toke Høiland-Jørgensen75ccae62020-01-16 16:14:44 +0100809 case NETDEV_REGISTER:
810 if (!netdev->netdev_ops->ndo_xdp_xmit || netdev->xdp_bulkq)
811 break;
812
813 /* will be freed in free_netdev() */
814 netdev->xdp_bulkq =
815 __alloc_percpu_gfp(sizeof(struct xdp_dev_bulk_queue),
816 sizeof(void *), GFP_ATOMIC);
817 if (!netdev->xdp_bulkq)
818 return NOTIFY_BAD;
819
820 for_each_possible_cpu(cpu)
821 per_cpu_ptr(netdev->xdp_bulkq, cpu)->dev = netdev;
822 break;
John Fastabend2ddf71e2017-07-17 09:30:02 -0700823 case NETDEV_UNREGISTER:
John Fastabend4cc7b952017-08-04 22:02:19 -0700824 /* This rcu_read_lock/unlock pair is needed because
825 * dev_map_list is an RCU list AND to ensure a delete
826 * operation does not free a netdev_map entry while we
827 * are comparing it against the netdev being unregistered.
828 */
829 rcu_read_lock();
830 list_for_each_entry_rcu(dtab, &dev_map_list, list) {
Toke Høiland-Jørgensence197d82019-10-19 13:19:31 +0200831 if (dtab->map.map_type == BPF_MAP_TYPE_DEVMAP_HASH) {
832 dev_map_hash_remove_netdev(dtab, netdev);
833 continue;
834 }
835
John Fastabend2ddf71e2017-07-17 09:30:02 -0700836 for (i = 0; i < dtab->map.max_entries; i++) {
John Fastabend4cc7b952017-08-04 22:02:19 -0700837 struct bpf_dtab_netdev *dev, *odev;
John Fastabend2ddf71e2017-07-17 09:30:02 -0700838
John Fastabend4cc7b952017-08-04 22:02:19 -0700839 dev = READ_ONCE(dtab->netdev_map[i]);
Taehee Yoof592f802018-10-24 20:15:17 +0900840 if (!dev || netdev != dev->dev)
John Fastabend2ddf71e2017-07-17 09:30:02 -0700841 continue;
John Fastabend4cc7b952017-08-04 22:02:19 -0700842 odev = cmpxchg(&dtab->netdev_map[i], dev, NULL);
843 if (dev == odev)
John Fastabend2ddf71e2017-07-17 09:30:02 -0700844 call_rcu(&dev->rcu,
845 __dev_map_entry_free);
846 }
847 }
John Fastabend4cc7b952017-08-04 22:02:19 -0700848 rcu_read_unlock();
John Fastabend2ddf71e2017-07-17 09:30:02 -0700849 break;
850 default:
851 break;
852 }
853 return NOTIFY_OK;
854}
855
856static struct notifier_block dev_map_notifier = {
857 .notifier_call = dev_map_notification,
858};
859
860static int __init dev_map_init(void)
861{
Björn Töpel96360002019-12-19 07:10:03 +0100862 int cpu;
863
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +0200864 /* Assure tracepoint shadow struct _bpf_dtab_netdev is in sync */
865 BUILD_BUG_ON(offsetof(struct bpf_dtab_netdev, dev) !=
866 offsetof(struct _bpf_dtab_netdev, dev));
John Fastabend2ddf71e2017-07-17 09:30:02 -0700867 register_netdevice_notifier(&dev_map_notifier);
Björn Töpel96360002019-12-19 07:10:03 +0100868
869 for_each_possible_cpu(cpu)
Toke Høiland-Jørgensen1d233882020-01-16 16:14:45 +0100870 INIT_LIST_HEAD(&per_cpu(dev_flush_list, cpu));
John Fastabend2ddf71e2017-07-17 09:30:02 -0700871 return 0;
872}
873
874subsys_initcall(dev_map_init);