xref: /linux/kernel/bpf/devmap.c (revision 6001896f00984d317fb75160ba05c4a885fbe2a0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io
3  */
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
20  * 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.
22  *
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.
28  *
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.
34  * 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.
40  *
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.
46  */
47 #include <linux/bpf.h>
48 #include <linux/local_lock.h>
49 #include <net/xdp.h>
50 #include <linux/filter.h>
51 #include <trace/events/xdp.h>
52 #include <linux/btf_ids.h>
53 
54 #define DEV_CREATE_FLAG_MASK \
55 	(BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
56 
57 struct xdp_dev_bulk_queue {
58 	struct xdp_frame *q[DEV_MAP_BULK_SIZE];
59 	struct list_head flush_node;
60 	struct net_device *dev;
61 	struct net_device *dev_rx;
62 	struct bpf_prog *xdp_prog;
63 	unsigned int count;
64 	local_lock_t bq_lock;
65 };
66 
67 struct bpf_dtab_netdev {
68 	struct net_device *dev; /* must be first member, due to tracepoint */
69 	struct hlist_node index_hlist;
70 	struct bpf_prog *xdp_prog;
71 	struct rcu_head rcu;
72 	unsigned int idx;
73 	struct bpf_devmap_val val;
74 };
75 
76 struct bpf_dtab {
77 	struct bpf_map map;
78 	struct bpf_dtab_netdev __rcu **netdev_map; /* DEVMAP type only */
79 	struct list_head list;
80 
81 	/* these are only used for DEVMAP_HASH type maps */
82 	struct hlist_head *dev_index_head;
83 	spinlock_t index_lock;
84 	unsigned int items;
85 	u32 n_buckets;
86 };
87 
88 static DEFINE_SPINLOCK(dev_map_lock);
89 static LIST_HEAD(dev_map_list);
90 
91 static struct hlist_head *dev_map_create_hash(unsigned int entries,
92 					      int numa_node)
93 {
94 	int i;
95 	struct hlist_head *hash;
96 
97 	hash = bpf_map_area_alloc((u64) entries * sizeof(*hash), numa_node);
98 	if (hash != NULL)
99 		for (i = 0; i < entries; i++)
100 			INIT_HLIST_HEAD(&hash[i]);
101 
102 	return hash;
103 }
104 
105 static inline struct hlist_head *dev_map_index_hash(struct bpf_dtab *dtab,
106 						    int idx)
107 {
108 	return &dtab->dev_index_head[idx & (dtab->n_buckets - 1)];
109 }
110 
111 static int dev_map_alloc_check(union bpf_attr *attr)
112 {
113 	u32 valsize = attr->value_size;
114 
115 	/* check sanity of attributes. 2 value sizes supported:
116 	 * 4 bytes: ifindex
117 	 * 8 bytes: ifindex + prog fd
118 	 */
119 	if (attr->max_entries == 0 || attr->key_size != 4 ||
120 	    (valsize != offsetofend(struct bpf_devmap_val, ifindex) &&
121 	     valsize != offsetofend(struct bpf_devmap_val, bpf_prog.fd)) ||
122 	    attr->map_flags & ~DEV_CREATE_FLAG_MASK)
123 		return -EINVAL;
124 
125 	if (attr->map_type == BPF_MAP_TYPE_DEVMAP_HASH) {
126 		/* Hash table size must be power of 2; roundup_pow_of_two()
127 		 * can overflow into UB on 32-bit arches
128 		 */
129 		if (attr->max_entries > 1UL << 31)
130 			return -EINVAL;
131 	}
132 
133 	return 0;
134 }
135 
136 static int dev_map_init_map(struct bpf_dtab *dtab, union bpf_attr *attr)
137 {
138 	/* Lookup returns a pointer straight to dev->ifindex, so make sure the
139 	 * verifier prevents writes from the BPF side
140 	 */
141 	attr->map_flags |= BPF_F_RDONLY_PROG;
142 	bpf_map_init_from_attr(&dtab->map, attr);
143 
144 	if (attr->map_type == BPF_MAP_TYPE_DEVMAP_HASH) {
145 		/* Hash table size must be power of 2 */
146 		dtab->n_buckets = roundup_pow_of_two(dtab->map.max_entries);
147 		dtab->dev_index_head = dev_map_create_hash(dtab->n_buckets,
148 							   dtab->map.numa_node);
149 		if (!dtab->dev_index_head)
150 			return -ENOMEM;
151 
152 		spin_lock_init(&dtab->index_lock);
153 	} else {
154 		dtab->netdev_map = bpf_map_area_alloc((u64) dtab->map.max_entries *
155 						      sizeof(struct bpf_dtab_netdev *),
156 						      dtab->map.numa_node);
157 		if (!dtab->netdev_map)
158 			return -ENOMEM;
159 	}
160 
161 	return 0;
162 }
163 
164 static struct bpf_map *dev_map_alloc(union bpf_attr *attr)
165 {
166 	struct bpf_dtab *dtab;
167 	int err;
168 
169 	dtab = bpf_map_area_alloc(sizeof(*dtab), NUMA_NO_NODE);
170 	if (!dtab)
171 		return ERR_PTR(-ENOMEM);
172 
173 	err = dev_map_init_map(dtab, attr);
174 	if (err) {
175 		bpf_map_area_free(dtab);
176 		return ERR_PTR(err);
177 	}
178 
179 	spin_lock(&dev_map_lock);
180 	list_add_tail_rcu(&dtab->list, &dev_map_list);
181 	spin_unlock(&dev_map_lock);
182 
183 	return &dtab->map;
184 }
185 
186 static void dev_map_free(struct bpf_map *map)
187 {
188 	struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
189 	u32 i;
190 
191 	/* At this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
192 	 * so the programs (can be more than one that used this map) were
193 	 * disconnected from events. The following synchronize_rcu() guarantees
194 	 * both rcu read critical sections complete and waits for
195 	 * preempt-disable regions (NAPI being the relevant context here) so we
196 	 * are certain there will be no further reads against the netdev_map and
197 	 * all flush operations are complete. Flush operations can only be done
198 	 * from NAPI context for this reason.
199 	 */
200 
201 	spin_lock(&dev_map_lock);
202 	list_del_rcu(&dtab->list);
203 	spin_unlock(&dev_map_lock);
204 
205 	/* bpf_redirect_info->map is assigned in __bpf_xdp_redirect_map()
206 	 * during NAPI callback and cleared after the XDP redirect. There is no
207 	 * explicit RCU read section which protects bpf_redirect_info->map but
208 	 * local_bh_disable() also marks the beginning an RCU section. This
209 	 * makes the complete softirq callback RCU protected. Thus after
210 	 * following synchronize_rcu() there no bpf_redirect_info->map == map
211 	 * assignment.
212 	 */
213 	synchronize_rcu();
214 
215 	/* Make sure prior __dev_map_entry_free() have completed. */
216 	rcu_barrier();
217 
218 	if (dtab->map.map_type == BPF_MAP_TYPE_DEVMAP_HASH) {
219 		for (i = 0; i < dtab->n_buckets; i++) {
220 			struct bpf_dtab_netdev *dev;
221 			struct hlist_head *head;
222 			struct hlist_node *next;
223 
224 			head = dev_map_index_hash(dtab, i);
225 
226 			hlist_for_each_entry_safe(dev, next, head, index_hlist) {
227 				hlist_del_rcu(&dev->index_hlist);
228 				if (dev->xdp_prog)
229 					bpf_prog_put(dev->xdp_prog);
230 				dev_put(dev->dev);
231 				kfree(dev);
232 			}
233 		}
234 
235 		bpf_map_area_free(dtab->dev_index_head);
236 	} else {
237 		for (i = 0; i < dtab->map.max_entries; i++) {
238 			struct bpf_dtab_netdev *dev;
239 
240 			dev = rcu_dereference_raw(dtab->netdev_map[i]);
241 			if (!dev)
242 				continue;
243 
244 			if (dev->xdp_prog)
245 				bpf_prog_put(dev->xdp_prog);
246 			dev_put(dev->dev);
247 			kfree(dev);
248 		}
249 
250 		bpf_map_area_free(dtab->netdev_map);
251 	}
252 
253 	bpf_map_area_free(dtab);
254 }
255 
256 static int dev_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
257 {
258 	struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
259 	u32 index = key ? *(u32 *)key : U32_MAX;
260 	u32 *next = next_key;
261 
262 	if (index >= dtab->map.max_entries) {
263 		*next = 0;
264 		return 0;
265 	}
266 
267 	if (index == dtab->map.max_entries - 1)
268 		return -ENOENT;
269 	*next = index + 1;
270 	return 0;
271 }
272 
273 /* Elements are kept alive by RCU; either by rcu_read_lock() (from syscall) or
274  * by local_bh_disable() (from XDP calls inside NAPI). The
275  * rcu_read_lock_bh_held() below makes lockdep accept both.
276  */
277 static void *__dev_map_hash_lookup_elem(struct bpf_map *map, u32 key)
278 {
279 	struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
280 	struct hlist_head *head = dev_map_index_hash(dtab, key);
281 	struct bpf_dtab_netdev *dev;
282 
283 	hlist_for_each_entry_rcu(dev, head, index_hlist,
284 				 lockdep_is_held(&dtab->index_lock))
285 		if (dev->idx == key)
286 			return dev;
287 
288 	return NULL;
289 }
290 
291 static int dev_map_hash_get_next_key(struct bpf_map *map, void *key,
292 				    void *next_key)
293 {
294 	struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
295 	u32 idx, *next = next_key;
296 	struct bpf_dtab_netdev *dev, *next_dev;
297 	struct hlist_head *head;
298 	int i = 0;
299 
300 	if (!key)
301 		goto find_first;
302 
303 	idx = *(u32 *)key;
304 
305 	dev = __dev_map_hash_lookup_elem(map, idx);
306 	if (!dev)
307 		goto find_first;
308 
309 	next_dev = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&dev->index_hlist)),
310 				    struct bpf_dtab_netdev, index_hlist);
311 
312 	if (next_dev) {
313 		*next = next_dev->idx;
314 		return 0;
315 	}
316 
317 	i = idx & (dtab->n_buckets - 1);
318 	i++;
319 
320  find_first:
321 	for (; i < dtab->n_buckets; i++) {
322 		head = dev_map_index_hash(dtab, i);
323 
324 		next_dev = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),
325 					    struct bpf_dtab_netdev,
326 					    index_hlist);
327 		if (next_dev) {
328 			*next = next_dev->idx;
329 			return 0;
330 		}
331 	}
332 
333 	return -ENOENT;
334 }
335 
336 static int dev_map_bpf_prog_run(struct bpf_prog *xdp_prog,
337 				struct xdp_frame **frames, int n,
338 				struct net_device *tx_dev,
339 				struct net_device *rx_dev)
340 {
341 	struct xdp_txq_info txq = { .dev = tx_dev };
342 	struct xdp_rxq_info rxq = { .dev = rx_dev };
343 	struct xdp_buff xdp;
344 	int i, nframes = 0;
345 
346 	for (i = 0; i < n; i++) {
347 		struct xdp_frame *xdpf = frames[i];
348 		u32 act;
349 		int err;
350 
351 		xdp_convert_frame_to_buff(xdpf, &xdp);
352 		xdp.txq = &txq;
353 		xdp.rxq = &rxq;
354 
355 		act = bpf_prog_run_xdp(xdp_prog, &xdp);
356 		switch (act) {
357 		case XDP_PASS:
358 			err = xdp_update_frame_from_buff(&xdp, xdpf);
359 			if (unlikely(err < 0))
360 				xdp_return_frame_rx_napi(xdpf);
361 			else
362 				frames[nframes++] = xdpf;
363 			break;
364 		default:
365 			bpf_warn_invalid_xdp_action(NULL, xdp_prog, act);
366 			fallthrough;
367 		case XDP_ABORTED:
368 			trace_xdp_exception(tx_dev, xdp_prog, act);
369 			fallthrough;
370 		case XDP_DROP:
371 			xdp_return_frame_rx_napi(xdpf);
372 			break;
373 		}
374 	}
375 	return nframes; /* sent frames count */
376 }
377 
378 static void bq_xmit_all(struct xdp_dev_bulk_queue *bq, u32 flags)
379 {
380 	struct net_device *dev = bq->dev;
381 	unsigned int cnt = bq->count;
382 	int sent = 0, err = 0;
383 	int to_send = cnt;
384 	int i;
385 
386 	lockdep_assert_held(&bq->bq_lock);
387 
388 	if (unlikely(!cnt))
389 		return;
390 
391 	for (i = 0; i < cnt; i++) {
392 		struct xdp_frame *xdpf = bq->q[i];
393 
394 		prefetch(xdpf);
395 	}
396 
397 	if (bq->xdp_prog) {
398 		to_send = dev_map_bpf_prog_run(bq->xdp_prog, bq->q, cnt, dev, bq->dev_rx);
399 		if (!to_send)
400 			goto out;
401 	}
402 
403 	sent = dev->netdev_ops->ndo_xdp_xmit(dev, to_send, bq->q, flags);
404 	if (sent < 0) {
405 		/* If ndo_xdp_xmit fails with an errno, no frames have
406 		 * been xmit'ed.
407 		 */
408 		err = sent;
409 		sent = 0;
410 	}
411 
412 	/* If not all frames have been transmitted, it is our
413 	 * responsibility to free them
414 	 */
415 	for (i = sent; unlikely(i < to_send); i++)
416 		xdp_return_frame_rx_napi(bq->q[i]);
417 
418 out:
419 	bq->count = 0;
420 	trace_xdp_devmap_xmit(bq->dev_rx, dev, sent, cnt - sent, err);
421 }
422 
423 /* __dev_flush is called from xdp_do_flush() which _must_ be signalled from the
424  * driver before returning from its napi->poll() routine. See the comment above
425  * xdp_do_flush() in filter.c.
426  */
427 void __dev_flush(struct list_head *flush_list)
428 {
429 	struct xdp_dev_bulk_queue *bq, *tmp;
430 
431 	list_for_each_entry_safe(bq, tmp, flush_list, flush_node) {
432 		local_lock_nested_bh(&bq->dev->xdp_bulkq->bq_lock);
433 		bq_xmit_all(bq, XDP_XMIT_FLUSH);
434 		bq->dev_rx = NULL;
435 		bq->xdp_prog = NULL;
436 		__list_del_clearprev(&bq->flush_node);
437 		local_unlock_nested_bh(&bq->dev->xdp_bulkq->bq_lock);
438 	}
439 }
440 
441 /* Elements are kept alive by RCU; either by rcu_read_lock() (from syscall) or
442  * by local_bh_disable() (from XDP calls inside NAPI). The
443  * rcu_read_lock_bh_held() below makes lockdep accept both.
444  */
445 static void *__dev_map_lookup_elem(struct bpf_map *map, u32 key)
446 {
447 	struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
448 	struct bpf_dtab_netdev *obj;
449 
450 	if (key >= map->max_entries)
451 		return NULL;
452 
453 	obj = rcu_dereference_check(dtab->netdev_map[key],
454 				    rcu_read_lock_bh_held());
455 	return obj;
456 }
457 
458 /* Runs in NAPI, i.e., softirq under local_bh_disable(). Thus, safe percpu
459  * variable access, and map elements stick around. See comment above
460  * xdp_do_flush() in filter.c. PREEMPT_RT relies on local_lock_nested_bh()
461  * to serialise access to the per-CPU bq.
462  */
463 static void bq_enqueue(struct net_device *dev, struct xdp_frame *xdpf,
464 		       struct net_device *dev_rx, struct bpf_prog *xdp_prog)
465 {
466 	struct xdp_dev_bulk_queue *bq;
467 
468 	local_lock_nested_bh(&dev->xdp_bulkq->bq_lock);
469 	bq = this_cpu_ptr(dev->xdp_bulkq);
470 
471 	if (unlikely(bq->count == DEV_MAP_BULK_SIZE))
472 		bq_xmit_all(bq, 0);
473 
474 	/* Ingress dev_rx will be the same for all xdp_frame's in
475 	 * bulk_queue, because bq stored per-CPU and must be flushed
476 	 * from net_device drivers NAPI func end.
477 	 *
478 	 * Do the same with xdp_prog and flush_list since these fields
479 	 * are only ever modified together.
480 	 */
481 	if (!bq->dev_rx) {
482 		struct list_head *flush_list = bpf_net_ctx_get_dev_flush_list();
483 
484 		bq->dev_rx = dev_rx;
485 		bq->xdp_prog = xdp_prog;
486 		list_add(&bq->flush_node, flush_list);
487 	}
488 
489 	bq->q[bq->count++] = xdpf;
490 
491 	local_unlock_nested_bh(&dev->xdp_bulkq->bq_lock);
492 }
493 
494 static inline int __xdp_enqueue(struct net_device *dev, struct xdp_frame *xdpf,
495 				struct net_device *dev_rx,
496 				struct bpf_prog *xdp_prog)
497 {
498 	int err;
499 
500 	if (!(dev->xdp_features & NETDEV_XDP_ACT_NDO_XMIT))
501 		return -EOPNOTSUPP;
502 
503 	if (unlikely(!(dev->xdp_features & NETDEV_XDP_ACT_NDO_XMIT_SG) &&
504 		     xdp_frame_has_frags(xdpf)))
505 		return -EOPNOTSUPP;
506 
507 	err = xdp_ok_fwd_dev(dev, xdp_get_frame_len(xdpf));
508 	if (unlikely(err))
509 		return err;
510 
511 	bq_enqueue(dev, xdpf, dev_rx, xdp_prog);
512 	return 0;
513 }
514 
515 static u32 dev_map_bpf_prog_run_skb(struct sk_buff *skb, struct bpf_dtab_netdev *dst)
516 {
517 	struct xdp_txq_info txq = { .dev = dst->dev };
518 	struct xdp_buff xdp;
519 	u32 act;
520 
521 	if (!dst->xdp_prog)
522 		return XDP_PASS;
523 
524 	__skb_pull(skb, skb->mac_len);
525 	xdp.txq = &txq;
526 
527 	act = bpf_prog_run_generic_xdp(skb, &xdp, dst->xdp_prog);
528 	switch (act) {
529 	case XDP_PASS:
530 		__skb_push(skb, skb->mac_len);
531 		break;
532 	default:
533 		bpf_warn_invalid_xdp_action(NULL, dst->xdp_prog, act);
534 		fallthrough;
535 	case XDP_ABORTED:
536 		trace_xdp_exception(dst->dev, dst->xdp_prog, act);
537 		fallthrough;
538 	case XDP_DROP:
539 		kfree_skb(skb);
540 		break;
541 	}
542 
543 	return act;
544 }
545 
546 int dev_xdp_enqueue(struct net_device *dev, struct xdp_frame *xdpf,
547 		    struct net_device *dev_rx)
548 {
549 	return __xdp_enqueue(dev, xdpf, dev_rx, NULL);
550 }
551 
552 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_frame *xdpf,
553 		    struct net_device *dev_rx)
554 {
555 	struct net_device *dev = dst->dev;
556 
557 	return __xdp_enqueue(dev, xdpf, dev_rx, dst->xdp_prog);
558 }
559 
560 static bool is_valid_dst(struct bpf_dtab_netdev *obj, struct xdp_frame *xdpf)
561 {
562 	if (!obj)
563 		return false;
564 
565 	if (!(obj->dev->xdp_features & NETDEV_XDP_ACT_NDO_XMIT))
566 		return false;
567 
568 	if (unlikely(!(obj->dev->xdp_features & NETDEV_XDP_ACT_NDO_XMIT_SG) &&
569 		     xdp_frame_has_frags(xdpf)))
570 		return false;
571 
572 	if (xdp_ok_fwd_dev(obj->dev, xdp_get_frame_len(xdpf)))
573 		return false;
574 
575 	return true;
576 }
577 
578 static int dev_map_enqueue_clone(struct bpf_dtab_netdev *obj,
579 				 struct net_device *dev_rx,
580 				 struct xdp_frame *xdpf)
581 {
582 	struct xdp_frame *nxdpf;
583 
584 	/* Frags live outside the linear frame and cannot be cloned safely. */
585 	if (unlikely(xdp_frame_has_frags(xdpf)))
586 		return -EOPNOTSUPP;
587 
588 	nxdpf = xdpf_clone(xdpf);
589 	if (!nxdpf)
590 		return -ENOMEM;
591 
592 	bq_enqueue(obj->dev, nxdpf, dev_rx, obj->xdp_prog);
593 
594 	return 0;
595 }
596 
597 static inline bool is_ifindex_excluded(int *excluded, int num_excluded, int ifindex)
598 {
599 	while (num_excluded--) {
600 		if (ifindex == excluded[num_excluded])
601 			return true;
602 	}
603 	return false;
604 }
605 
606 /* Get ifindex of each upper device. 'indexes' must be able to hold at
607  * least 'max' elements.
608  * Returns the number of ifindexes added, or -EOVERFLOW if there are too
609  * many upper devices.
610  */
611 static int get_upper_ifindexes(struct net_device *dev, int *indexes, int max)
612 {
613 	struct net_device *upper;
614 	struct list_head *iter;
615 	int n = 0;
616 
617 	netdev_for_each_upper_dev_rcu(dev, upper, iter) {
618 		if (n >= max)
619 			return -EOVERFLOW;
620 		indexes[n++] = upper->ifindex;
621 	}
622 
623 	return n;
624 }
625 
626 int dev_map_enqueue_multi(struct xdp_frame *xdpf, struct net_device *dev_rx,
627 			  struct bpf_map *map, bool exclude_ingress)
628 {
629 	struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
630 	struct bpf_dtab_netdev *dst, *last_dst = NULL;
631 	int excluded_devices[1+MAX_NEST_DEV];
632 	struct hlist_head *head;
633 	int num_excluded = 0;
634 	unsigned int i;
635 	int err;
636 
637 	if (exclude_ingress) {
638 		num_excluded = get_upper_ifindexes(dev_rx, excluded_devices,
639 						   ARRAY_SIZE(excluded_devices) - 1);
640 		if (num_excluded < 0)
641 			return num_excluded;
642 
643 		excluded_devices[num_excluded++] = dev_rx->ifindex;
644 	}
645 
646 	if (map->map_type == BPF_MAP_TYPE_DEVMAP) {
647 		for (i = 0; i < map->max_entries; i++) {
648 			dst = rcu_dereference_check(dtab->netdev_map[i],
649 						    rcu_read_lock_bh_held());
650 			if (!is_valid_dst(dst, xdpf))
651 				continue;
652 
653 			if (is_ifindex_excluded(excluded_devices, num_excluded, dst->dev->ifindex))
654 				continue;
655 
656 			/* we only need n-1 clones; last_dst enqueued below */
657 			if (!last_dst) {
658 				last_dst = dst;
659 				continue;
660 			}
661 
662 			err = dev_map_enqueue_clone(last_dst, dev_rx, xdpf);
663 			if (err)
664 				return err;
665 
666 			last_dst = dst;
667 		}
668 	} else { /* BPF_MAP_TYPE_DEVMAP_HASH */
669 		for (i = 0; i < dtab->n_buckets; i++) {
670 			head = dev_map_index_hash(dtab, i);
671 			hlist_for_each_entry_rcu(dst, head, index_hlist,
672 						rcu_read_lock_bh_held()) {
673 				if (!is_valid_dst(dst, xdpf))
674 					continue;
675 
676 				if (is_ifindex_excluded(excluded_devices, num_excluded,
677 							dst->dev->ifindex))
678 					continue;
679 
680 				/* we only need n-1 clones; last_dst enqueued below */
681 				if (!last_dst) {
682 					last_dst = dst;
683 					continue;
684 				}
685 
686 				err = dev_map_enqueue_clone(last_dst, dev_rx, xdpf);
687 				if (err)
688 					return err;
689 
690 				last_dst = dst;
691 			}
692 		}
693 	}
694 
695 	/* consume the last copy of the frame */
696 	if (last_dst)
697 		bq_enqueue(last_dst->dev, xdpf, dev_rx, last_dst->xdp_prog);
698 	else
699 		xdp_return_frame_rx_napi(xdpf); /* dtab is empty */
700 
701 	return 0;
702 }
703 
704 int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
705 			     const struct bpf_prog *xdp_prog)
706 {
707 	int err;
708 
709 	err = xdp_ok_fwd_dev(dst->dev, skb->len);
710 	if (unlikely(err))
711 		return err;
712 
713 	if (dst->xdp_prog && skb_cloned(skb)) {
714 		struct sk_buff *nskb;
715 
716 		nskb = skb_copy(skb, GFP_ATOMIC);
717 		if (!nskb)
718 			return -ENOMEM;
719 
720 		nskb->mac_len = skb->mac_len;
721 		consume_skb(skb);
722 		skb = nskb;
723 	}
724 
725 	/* Redirect has already succeeded semantically at this point, so we just
726 	 * return 0 even if packet is dropped. Helper below takes care of
727 	 * freeing skb.
728 	 */
729 	if (dev_map_bpf_prog_run_skb(skb, dst) != XDP_PASS)
730 		return 0;
731 
732 	skb->dev = dst->dev;
733 	generic_xdp_tx(skb, xdp_prog);
734 
735 	return 0;
736 }
737 
738 static int dev_map_redirect_clone(struct bpf_dtab_netdev *dst,
739 				  struct sk_buff *skb,
740 				  const struct bpf_prog *xdp_prog)
741 {
742 	struct sk_buff *nskb;
743 	int err;
744 
745 	if (unlikely(skb_is_nonlinear(skb)))
746 		return -EOPNOTSUPP;
747 
748 	nskb = skb_clone(skb, GFP_ATOMIC);
749 	if (!nskb)
750 		return -ENOMEM;
751 
752 	err = dev_map_generic_redirect(dst, nskb, xdp_prog);
753 	if (unlikely(err)) {
754 		consume_skb(nskb);
755 		return err;
756 	}
757 
758 	return 0;
759 }
760 
761 int dev_map_redirect_multi(struct net_device *dev, struct sk_buff *skb,
762 			   const struct bpf_prog *xdp_prog,
763 			   struct bpf_map *map, bool exclude_ingress)
764 {
765 	struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
766 	struct bpf_dtab_netdev *dst, *last_dst = NULL;
767 	int excluded_devices[1+MAX_NEST_DEV];
768 	struct hlist_head *head;
769 	int num_excluded = 0;
770 	unsigned int i;
771 	int err;
772 
773 	if (exclude_ingress) {
774 		num_excluded = get_upper_ifindexes(dev, excluded_devices,
775 						   ARRAY_SIZE(excluded_devices) - 1);
776 		if (num_excluded < 0)
777 			return num_excluded;
778 
779 		excluded_devices[num_excluded++] = dev->ifindex;
780 	}
781 
782 	if (map->map_type == BPF_MAP_TYPE_DEVMAP) {
783 		for (i = 0; i < map->max_entries; i++) {
784 			dst = rcu_dereference_check(dtab->netdev_map[i],
785 						    rcu_read_lock_bh_held());
786 			if (!dst)
787 				continue;
788 
789 			if (is_ifindex_excluded(excluded_devices, num_excluded, dst->dev->ifindex))
790 				continue;
791 
792 			/* we only need n-1 clones; last_dst enqueued below */
793 			if (!last_dst) {
794 				last_dst = dst;
795 				continue;
796 			}
797 
798 			err = dev_map_redirect_clone(last_dst, skb, xdp_prog);
799 			if (err)
800 				return err;
801 
802 			last_dst = dst;
803 
804 		}
805 	} else { /* BPF_MAP_TYPE_DEVMAP_HASH */
806 		for (i = 0; i < dtab->n_buckets; i++) {
807 			head = dev_map_index_hash(dtab, i);
808 			hlist_for_each_entry_rcu(dst, head, index_hlist, rcu_read_lock_bh_held()) {
809 				if (is_ifindex_excluded(excluded_devices, num_excluded,
810 							dst->dev->ifindex))
811 					continue;
812 
813 				/* we only need n-1 clones; last_dst enqueued below */
814 				if (!last_dst) {
815 					last_dst = dst;
816 					continue;
817 				}
818 
819 				err = dev_map_redirect_clone(last_dst, skb, xdp_prog);
820 				if (err)
821 					return err;
822 
823 				last_dst = dst;
824 			}
825 		}
826 	}
827 
828 	/* consume the first skb and return */
829 	if (last_dst)
830 		return dev_map_generic_redirect(last_dst, skb, xdp_prog);
831 
832 	/* dtab is empty */
833 	consume_skb(skb);
834 	return 0;
835 }
836 
837 static void *dev_map_lookup_elem(struct bpf_map *map, void *key)
838 {
839 	struct bpf_dtab_netdev *obj = __dev_map_lookup_elem(map, *(u32 *)key);
840 
841 	return obj ? &obj->val : NULL;
842 }
843 
844 static void *dev_map_hash_lookup_elem(struct bpf_map *map, void *key)
845 {
846 	struct bpf_dtab_netdev *obj = __dev_map_hash_lookup_elem(map,
847 								*(u32 *)key);
848 	return obj ? &obj->val : NULL;
849 }
850 
851 static void __dev_map_entry_free(struct rcu_head *rcu)
852 {
853 	struct bpf_dtab_netdev *dev;
854 
855 	dev = container_of(rcu, struct bpf_dtab_netdev, rcu);
856 	if (dev->xdp_prog)
857 		bpf_prog_put(dev->xdp_prog);
858 	dev_put(dev->dev);
859 	kfree(dev);
860 }
861 
862 static long dev_map_delete_elem(struct bpf_map *map, void *key)
863 {
864 	struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
865 	struct bpf_dtab_netdev *old_dev;
866 	u32 k = *(u32 *)key;
867 
868 	if (k >= map->max_entries)
869 		return -EINVAL;
870 
871 	old_dev = unrcu_pointer(xchg(&dtab->netdev_map[k], NULL));
872 	if (old_dev) {
873 		call_rcu(&old_dev->rcu, __dev_map_entry_free);
874 		atomic_dec((atomic_t *)&dtab->items);
875 	}
876 	return 0;
877 }
878 
879 static long dev_map_hash_delete_elem(struct bpf_map *map, void *key)
880 {
881 	struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
882 	struct bpf_dtab_netdev *old_dev;
883 	u32 k = *(u32 *)key;
884 	unsigned long flags;
885 	int ret = -ENOENT;
886 
887 	spin_lock_irqsave(&dtab->index_lock, flags);
888 
889 	old_dev = __dev_map_hash_lookup_elem(map, k);
890 	if (old_dev) {
891 		dtab->items--;
892 		hlist_del_init_rcu(&old_dev->index_hlist);
893 		call_rcu(&old_dev->rcu, __dev_map_entry_free);
894 		ret = 0;
895 	}
896 	spin_unlock_irqrestore(&dtab->index_lock, flags);
897 
898 	return ret;
899 }
900 
901 static struct bpf_dtab_netdev *__dev_map_alloc_node(struct net *net,
902 						    struct bpf_dtab *dtab,
903 						    struct bpf_devmap_val *val,
904 						    unsigned int idx)
905 {
906 	struct bpf_prog *prog = NULL;
907 	struct bpf_dtab_netdev *dev;
908 
909 	dev = bpf_map_kmalloc_node(&dtab->map, sizeof(*dev),
910 				   GFP_NOWAIT,
911 				   dtab->map.numa_node);
912 	if (!dev)
913 		return ERR_PTR(-ENOMEM);
914 
915 	dev->dev = dev_get_by_index(net, val->ifindex);
916 	if (!dev->dev)
917 		goto err_out;
918 
919 	if (val->bpf_prog.fd > 0) {
920 		prog = bpf_prog_get_type_dev(val->bpf_prog.fd,
921 					     BPF_PROG_TYPE_XDP, false);
922 		if (IS_ERR(prog))
923 			goto err_put_dev;
924 		if (prog->expected_attach_type != BPF_XDP_DEVMAP ||
925 		    !bpf_prog_map_compatible(&dtab->map, prog))
926 			goto err_put_prog;
927 	}
928 
929 	dev->idx = idx;
930 	if (prog) {
931 		dev->xdp_prog = prog;
932 		dev->val.bpf_prog.id = prog->aux->id;
933 	} else {
934 		dev->xdp_prog = NULL;
935 		dev->val.bpf_prog.id = 0;
936 	}
937 	dev->val.ifindex = val->ifindex;
938 
939 	return dev;
940 err_put_prog:
941 	bpf_prog_put(prog);
942 err_put_dev:
943 	dev_put(dev->dev);
944 err_out:
945 	kfree(dev);
946 	return ERR_PTR(-EINVAL);
947 }
948 
949 static long __dev_map_update_elem(struct net *net, struct bpf_map *map,
950 				  void *key, void *value, u64 map_flags)
951 {
952 	struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
953 	struct bpf_dtab_netdev *dev, *old_dev;
954 	struct bpf_devmap_val val = {};
955 	u32 i = *(u32 *)key;
956 
957 	if (unlikely(map_flags > BPF_EXIST))
958 		return -EINVAL;
959 	if (unlikely(i >= dtab->map.max_entries))
960 		return -E2BIG;
961 	if (unlikely(map_flags == BPF_NOEXIST))
962 		return -EEXIST;
963 
964 	/* already verified value_size <= sizeof val */
965 	memcpy(&val, value, map->value_size);
966 
967 	if (!val.ifindex) {
968 		dev = NULL;
969 		/* can not specify fd if ifindex is 0 */
970 		if (val.bpf_prog.fd > 0)
971 			return -EINVAL;
972 	} else {
973 		dev = __dev_map_alloc_node(net, dtab, &val, i);
974 		if (IS_ERR(dev))
975 			return PTR_ERR(dev);
976 	}
977 
978 	/* Use call_rcu() here to ensure rcu critical sections have completed
979 	 * Remembering the driver side flush operation will happen before the
980 	 * net device is removed.
981 	 */
982 	old_dev = unrcu_pointer(xchg(&dtab->netdev_map[i], RCU_INITIALIZER(dev)));
983 	if (old_dev)
984 		call_rcu(&old_dev->rcu, __dev_map_entry_free);
985 	else
986 		atomic_inc((atomic_t *)&dtab->items);
987 
988 	return 0;
989 }
990 
991 static long dev_map_update_elem(struct bpf_map *map, void *key, void *value,
992 				u64 map_flags)
993 {
994 	return __dev_map_update_elem(current->nsproxy->net_ns,
995 				     map, key, value, map_flags);
996 }
997 
998 static long __dev_map_hash_update_elem(struct net *net, struct bpf_map *map,
999 				       void *key, void *value, u64 map_flags)
1000 {
1001 	struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
1002 	struct bpf_dtab_netdev *dev, *old_dev;
1003 	struct bpf_devmap_val val = {};
1004 	u32 idx = *(u32 *)key;
1005 	unsigned long flags;
1006 	int err = -EEXIST;
1007 
1008 	/* already verified value_size <= sizeof val */
1009 	memcpy(&val, value, map->value_size);
1010 
1011 	if (unlikely(map_flags > BPF_EXIST || !val.ifindex))
1012 		return -EINVAL;
1013 
1014 	spin_lock_irqsave(&dtab->index_lock, flags);
1015 
1016 	old_dev = __dev_map_hash_lookup_elem(map, idx);
1017 	if (old_dev && (map_flags & BPF_NOEXIST))
1018 		goto out_err;
1019 
1020 	dev = __dev_map_alloc_node(net, dtab, &val, idx);
1021 	if (IS_ERR(dev)) {
1022 		err = PTR_ERR(dev);
1023 		goto out_err;
1024 	}
1025 
1026 	if (old_dev) {
1027 		hlist_del_rcu(&old_dev->index_hlist);
1028 	} else {
1029 		if (dtab->items >= dtab->map.max_entries) {
1030 			spin_unlock_irqrestore(&dtab->index_lock, flags);
1031 			call_rcu(&dev->rcu, __dev_map_entry_free);
1032 			return -E2BIG;
1033 		}
1034 		dtab->items++;
1035 	}
1036 
1037 	hlist_add_head_rcu(&dev->index_hlist,
1038 			   dev_map_index_hash(dtab, idx));
1039 	spin_unlock_irqrestore(&dtab->index_lock, flags);
1040 
1041 	if (old_dev)
1042 		call_rcu(&old_dev->rcu, __dev_map_entry_free);
1043 
1044 	return 0;
1045 
1046 out_err:
1047 	spin_unlock_irqrestore(&dtab->index_lock, flags);
1048 	return err;
1049 }
1050 
1051 static long dev_map_hash_update_elem(struct bpf_map *map, void *key, void *value,
1052 				     u64 map_flags)
1053 {
1054 	return __dev_map_hash_update_elem(current->nsproxy->net_ns,
1055 					 map, key, value, map_flags);
1056 }
1057 
1058 static long dev_map_redirect(struct bpf_map *map, u64 ifindex, u64 flags)
1059 {
1060 	return __bpf_xdp_redirect_map(map, ifindex, flags,
1061 				      BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS,
1062 				      __dev_map_lookup_elem);
1063 }
1064 
1065 static long dev_hash_map_redirect(struct bpf_map *map, u64 ifindex, u64 flags)
1066 {
1067 	return __bpf_xdp_redirect_map(map, ifindex, flags,
1068 				      BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS,
1069 				      __dev_map_hash_lookup_elem);
1070 }
1071 
1072 static u64 dev_map_mem_usage(const struct bpf_map *map)
1073 {
1074 	struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
1075 	u64 usage = sizeof(struct bpf_dtab);
1076 
1077 	if (map->map_type == BPF_MAP_TYPE_DEVMAP_HASH)
1078 		usage += (u64)dtab->n_buckets * sizeof(struct hlist_head);
1079 	else
1080 		usage += (u64)map->max_entries * sizeof(struct bpf_dtab_netdev *);
1081 	usage += atomic_read((atomic_t *)&dtab->items) *
1082 			 (u64)sizeof(struct bpf_dtab_netdev);
1083 	return usage;
1084 }
1085 
1086 BTF_ID_LIST_SINGLE(dev_map_btf_ids, struct, bpf_dtab)
1087 const struct bpf_map_ops dev_map_ops = {
1088 	.map_meta_equal = bpf_map_meta_equal,
1089 	.map_alloc_check = dev_map_alloc_check,
1090 	.map_alloc = dev_map_alloc,
1091 	.map_free = dev_map_free,
1092 	.map_get_next_key = dev_map_get_next_key,
1093 	.map_lookup_elem = dev_map_lookup_elem,
1094 	.map_update_elem = dev_map_update_elem,
1095 	.map_delete_elem = dev_map_delete_elem,
1096 	.map_check_btf = map_check_no_btf,
1097 	.map_mem_usage = dev_map_mem_usage,
1098 	.map_btf_id = &dev_map_btf_ids[0],
1099 	.map_redirect = dev_map_redirect,
1100 };
1101 
1102 const struct bpf_map_ops dev_map_hash_ops = {
1103 	.map_meta_equal = bpf_map_meta_equal,
1104 	.map_alloc_check = dev_map_alloc_check,
1105 	.map_alloc = dev_map_alloc,
1106 	.map_free = dev_map_free,
1107 	.map_get_next_key = dev_map_hash_get_next_key,
1108 	.map_lookup_elem = dev_map_hash_lookup_elem,
1109 	.map_update_elem = dev_map_hash_update_elem,
1110 	.map_delete_elem = dev_map_hash_delete_elem,
1111 	.map_check_btf = map_check_no_btf,
1112 	.map_mem_usage = dev_map_mem_usage,
1113 	.map_btf_id = &dev_map_btf_ids[0],
1114 	.map_redirect = dev_hash_map_redirect,
1115 };
1116 
1117 static void dev_map_hash_remove_netdev(struct bpf_dtab *dtab,
1118 				       struct net_device *netdev)
1119 {
1120 	unsigned long flags;
1121 	u32 i;
1122 
1123 	spin_lock_irqsave(&dtab->index_lock, flags);
1124 	for (i = 0; i < dtab->n_buckets; i++) {
1125 		struct bpf_dtab_netdev *dev;
1126 		struct hlist_head *head;
1127 		struct hlist_node *next;
1128 
1129 		head = dev_map_index_hash(dtab, i);
1130 
1131 		hlist_for_each_entry_safe(dev, next, head, index_hlist) {
1132 			if (netdev != dev->dev)
1133 				continue;
1134 
1135 			dtab->items--;
1136 			hlist_del_rcu(&dev->index_hlist);
1137 			call_rcu(&dev->rcu, __dev_map_entry_free);
1138 		}
1139 	}
1140 	spin_unlock_irqrestore(&dtab->index_lock, flags);
1141 }
1142 
1143 static int dev_map_notification(struct notifier_block *notifier,
1144 				ulong event, void *ptr)
1145 {
1146 	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1147 	struct bpf_dtab *dtab;
1148 	int i, cpu;
1149 
1150 	switch (event) {
1151 	case NETDEV_REGISTER:
1152 		if (!netdev->netdev_ops->ndo_xdp_xmit || netdev->xdp_bulkq)
1153 			break;
1154 
1155 		/* will be freed in free_netdev() */
1156 		netdev->xdp_bulkq = alloc_percpu(struct xdp_dev_bulk_queue);
1157 		if (!netdev->xdp_bulkq)
1158 			return NOTIFY_BAD;
1159 
1160 		for_each_possible_cpu(cpu) {
1161 			struct xdp_dev_bulk_queue *bq;
1162 
1163 			bq = per_cpu_ptr(netdev->xdp_bulkq, cpu);
1164 			bq->dev = netdev;
1165 			local_lock_init(&bq->bq_lock);
1166 		}
1167 		break;
1168 	case NETDEV_UNREGISTER:
1169 		/* This rcu_read_lock/unlock pair is needed because
1170 		 * dev_map_list is an RCU list AND to ensure a delete
1171 		 * operation does not free a netdev_map entry while we
1172 		 * are comparing it against the netdev being unregistered.
1173 		 */
1174 		rcu_read_lock();
1175 		list_for_each_entry_rcu(dtab, &dev_map_list, list) {
1176 			if (dtab->map.map_type == BPF_MAP_TYPE_DEVMAP_HASH) {
1177 				dev_map_hash_remove_netdev(dtab, netdev);
1178 				continue;
1179 			}
1180 
1181 			for (i = 0; i < dtab->map.max_entries; i++) {
1182 				struct bpf_dtab_netdev *dev, *odev;
1183 
1184 				dev = rcu_dereference(dtab->netdev_map[i]);
1185 				if (!dev || netdev != dev->dev)
1186 					continue;
1187 				odev = unrcu_pointer(cmpxchg(&dtab->netdev_map[i], RCU_INITIALIZER(dev), NULL));
1188 				if (dev == odev) {
1189 					call_rcu(&dev->rcu,
1190 						 __dev_map_entry_free);
1191 					atomic_dec((atomic_t *)&dtab->items);
1192 				}
1193 			}
1194 		}
1195 		rcu_read_unlock();
1196 		break;
1197 	default:
1198 		break;
1199 	}
1200 	return NOTIFY_OK;
1201 }
1202 
1203 static struct notifier_block dev_map_notifier = {
1204 	.notifier_call = dev_map_notification,
1205 };
1206 
1207 static int __init dev_map_init(void)
1208 {
1209 	/* Assure tracepoint shadow struct _bpf_dtab_netdev is in sync */
1210 	BUILD_BUG_ON(offsetof(struct bpf_dtab_netdev, dev) !=
1211 		     offsetof(struct _bpf_dtab_netdev, dev));
1212 	register_netdevice_notifier(&dev_map_notifier);
1213 
1214 	return 0;
1215 }
1216 
1217 subsys_initcall(dev_map_init);
1218