xref: /linux/kernel/bpf/devmap.c (revision e8d2bec0)
1546ac1ffSJohn Fastabend /* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io
2546ac1ffSJohn Fastabend  *
3546ac1ffSJohn Fastabend  * This program is free software; you can redistribute it and/or
4546ac1ffSJohn Fastabend  * modify it under the terms of version 2 of the GNU General Public
5546ac1ffSJohn Fastabend  * License as published by the Free Software Foundation.
6546ac1ffSJohn Fastabend  *
7546ac1ffSJohn Fastabend  * This program is distributed in the hope that it will be useful, but
8546ac1ffSJohn Fastabend  * WITHOUT ANY WARRANTY; without even the implied warranty of
9546ac1ffSJohn Fastabend  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10546ac1ffSJohn Fastabend  * General Public License for more details.
11546ac1ffSJohn Fastabend  */
12546ac1ffSJohn Fastabend 
13546ac1ffSJohn Fastabend /* Devmaps primary use is as a backend map for XDP BPF helper call
14546ac1ffSJohn Fastabend  * bpf_redirect_map(). Because XDP is mostly concerned with performance we
15546ac1ffSJohn Fastabend  * spent some effort to ensure the datapath with redirect maps does not use
16546ac1ffSJohn Fastabend  * any locking. This is a quick note on the details.
17546ac1ffSJohn Fastabend  *
18546ac1ffSJohn Fastabend  * We have three possible paths to get into the devmap control plane bpf
19546ac1ffSJohn Fastabend  * syscalls, bpf programs, and driver side xmit/flush operations. A bpf syscall
20546ac1ffSJohn Fastabend  * will invoke an update, delete, or lookup operation. To ensure updates and
21546ac1ffSJohn Fastabend  * deletes appear atomic from the datapath side xchg() is used to modify the
22546ac1ffSJohn Fastabend  * netdev_map array. Then because the datapath does a lookup into the netdev_map
23546ac1ffSJohn Fastabend  * array (read-only) from an RCU critical section we use call_rcu() to wait for
24546ac1ffSJohn Fastabend  * an rcu grace period before free'ing the old data structures. This ensures the
25546ac1ffSJohn Fastabend  * datapath always has a valid copy. However, the datapath does a "flush"
26546ac1ffSJohn Fastabend  * operation that pushes any pending packets in the driver outside the RCU
27546ac1ffSJohn Fastabend  * critical section. Each bpf_dtab_netdev tracks these pending operations using
28546ac1ffSJohn Fastabend  * an atomic per-cpu bitmap. The bpf_dtab_netdev object will not be destroyed
29546ac1ffSJohn Fastabend  * until all bits are cleared indicating outstanding flush operations have
30546ac1ffSJohn Fastabend  * completed.
31546ac1ffSJohn Fastabend  *
32546ac1ffSJohn Fastabend  * BPF syscalls may race with BPF program calls on any of the update, delete
33546ac1ffSJohn Fastabend  * or lookup operations. As noted above the xchg() operation also keep the
34546ac1ffSJohn Fastabend  * netdev_map consistent in this case. From the devmap side BPF programs
35546ac1ffSJohn Fastabend  * calling into these operations are the same as multiple user space threads
36546ac1ffSJohn Fastabend  * making system calls.
372ddf71e2SJohn Fastabend  *
382ddf71e2SJohn Fastabend  * Finally, any of the above may race with a netdev_unregister notifier. The
392ddf71e2SJohn Fastabend  * unregister notifier must search for net devices in the map structure that
402ddf71e2SJohn Fastabend  * contain a reference to the net device and remove them. This is a two step
412ddf71e2SJohn Fastabend  * process (a) dereference the bpf_dtab_netdev object in netdev_map and (b)
422ddf71e2SJohn Fastabend  * check to see if the ifindex is the same as the net_device being removed.
434cc7b954SJohn Fastabend  * When removing the dev a cmpxchg() is used to ensure the correct dev is
444cc7b954SJohn Fastabend  * removed, in the case of a concurrent update or delete operation it is
454cc7b954SJohn Fastabend  * possible that the initially referenced dev is no longer in the map. As the
464cc7b954SJohn Fastabend  * notifier hook walks the map we know that new dev references can not be
474cc7b954SJohn Fastabend  * added by the user because core infrastructure ensures dev_get_by_index()
484cc7b954SJohn Fastabend  * calls will fail at this point.
49546ac1ffSJohn Fastabend  */
50546ac1ffSJohn Fastabend #include <linux/bpf.h>
5167f29e07SJesper Dangaard Brouer #include <net/xdp.h>
52546ac1ffSJohn Fastabend #include <linux/filter.h>
5367f29e07SJesper Dangaard Brouer #include <trace/events/xdp.h>
54546ac1ffSJohn Fastabend 
556e71b04aSChenbo Feng #define DEV_CREATE_FLAG_MASK \
566e71b04aSChenbo Feng 	(BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
576e71b04aSChenbo Feng 
585d053f9dSJesper Dangaard Brouer #define DEV_MAP_BULK_SIZE 16
595d053f9dSJesper Dangaard Brouer struct xdp_bulk_queue {
605d053f9dSJesper Dangaard Brouer 	struct xdp_frame *q[DEV_MAP_BULK_SIZE];
6138edddb8SJesper Dangaard Brouer 	struct net_device *dev_rx;
625d053f9dSJesper Dangaard Brouer 	unsigned int count;
635d053f9dSJesper Dangaard Brouer };
645d053f9dSJesper Dangaard Brouer 
65546ac1ffSJohn Fastabend struct bpf_dtab_netdev {
6667f29e07SJesper Dangaard Brouer 	struct net_device *dev; /* must be first member, due to tracepoint */
67546ac1ffSJohn Fastabend 	struct bpf_dtab *dtab;
68af4d045cSDaniel Borkmann 	unsigned int bit;
695d053f9dSJesper Dangaard Brouer 	struct xdp_bulk_queue __percpu *bulkq;
70af4d045cSDaniel Borkmann 	struct rcu_head rcu;
71546ac1ffSJohn Fastabend };
72546ac1ffSJohn Fastabend 
73546ac1ffSJohn Fastabend struct bpf_dtab {
74546ac1ffSJohn Fastabend 	struct bpf_map map;
75546ac1ffSJohn Fastabend 	struct bpf_dtab_netdev **netdev_map;
76af4d045cSDaniel Borkmann 	unsigned long __percpu *flush_needed;
772ddf71e2SJohn Fastabend 	struct list_head list;
78546ac1ffSJohn Fastabend };
79546ac1ffSJohn Fastabend 
804cc7b954SJohn Fastabend static DEFINE_SPINLOCK(dev_map_lock);
812ddf71e2SJohn Fastabend static LIST_HEAD(dev_map_list);
822ddf71e2SJohn Fastabend 
83af4d045cSDaniel Borkmann static u64 dev_map_bitmap_size(const union bpf_attr *attr)
84af4d045cSDaniel Borkmann {
858695a539SJohn Fastabend 	return BITS_TO_LONGS((u64) attr->max_entries) * sizeof(unsigned long);
86af4d045cSDaniel Borkmann }
87af4d045cSDaniel Borkmann 
88546ac1ffSJohn Fastabend static struct bpf_map *dev_map_alloc(union bpf_attr *attr)
89546ac1ffSJohn Fastabend {
90546ac1ffSJohn Fastabend 	struct bpf_dtab *dtab;
91582db7e0STobias Klauser 	int err = -EINVAL;
92546ac1ffSJohn Fastabend 	u64 cost;
93546ac1ffSJohn Fastabend 
949ef2a8cdSJohn Fastabend 	if (!capable(CAP_NET_ADMIN))
959ef2a8cdSJohn Fastabend 		return ERR_PTR(-EPERM);
969ef2a8cdSJohn Fastabend 
97546ac1ffSJohn Fastabend 	/* check sanity of attributes */
98546ac1ffSJohn Fastabend 	if (attr->max_entries == 0 || attr->key_size != 4 ||
996e71b04aSChenbo Feng 	    attr->value_size != 4 || attr->map_flags & ~DEV_CREATE_FLAG_MASK)
100546ac1ffSJohn Fastabend 		return ERR_PTR(-EINVAL);
101546ac1ffSJohn Fastabend 
102546ac1ffSJohn Fastabend 	dtab = kzalloc(sizeof(*dtab), GFP_USER);
103546ac1ffSJohn Fastabend 	if (!dtab)
104546ac1ffSJohn Fastabend 		return ERR_PTR(-ENOMEM);
105546ac1ffSJohn Fastabend 
106bd475643SJakub Kicinski 	bpf_map_init_from_attr(&dtab->map, attr);
107546ac1ffSJohn Fastabend 
108546ac1ffSJohn Fastabend 	/* make sure page count doesn't overflow */
109546ac1ffSJohn Fastabend 	cost = (u64) dtab->map.max_entries * sizeof(struct bpf_dtab_netdev *);
110af4d045cSDaniel Borkmann 	cost += dev_map_bitmap_size(attr) * num_possible_cpus();
111546ac1ffSJohn Fastabend 	if (cost >= U32_MAX - PAGE_SIZE)
112546ac1ffSJohn Fastabend 		goto free_dtab;
113546ac1ffSJohn Fastabend 
114546ac1ffSJohn Fastabend 	dtab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
115546ac1ffSJohn Fastabend 
116546ac1ffSJohn Fastabend 	/* if map size is larger than memlock limit, reject it early */
117546ac1ffSJohn Fastabend 	err = bpf_map_precharge_memlock(dtab->map.pages);
118546ac1ffSJohn Fastabend 	if (err)
119546ac1ffSJohn Fastabend 		goto free_dtab;
120546ac1ffSJohn Fastabend 
121582db7e0STobias Klauser 	err = -ENOMEM;
122582db7e0STobias Klauser 
12311393cc9SJohn Fastabend 	/* A per cpu bitfield with a bit per possible net device */
12482f8dd28SDaniel Borkmann 	dtab->flush_needed = __alloc_percpu_gfp(dev_map_bitmap_size(attr),
12582f8dd28SDaniel Borkmann 						__alignof__(unsigned long),
12682f8dd28SDaniel Borkmann 						GFP_KERNEL | __GFP_NOWARN);
12711393cc9SJohn Fastabend 	if (!dtab->flush_needed)
12811393cc9SJohn Fastabend 		goto free_dtab;
12911393cc9SJohn Fastabend 
130546ac1ffSJohn Fastabend 	dtab->netdev_map = bpf_map_area_alloc(dtab->map.max_entries *
13196eabe7aSMartin KaFai Lau 					      sizeof(struct bpf_dtab_netdev *),
13296eabe7aSMartin KaFai Lau 					      dtab->map.numa_node);
133546ac1ffSJohn Fastabend 	if (!dtab->netdev_map)
134546ac1ffSJohn Fastabend 		goto free_dtab;
135546ac1ffSJohn Fastabend 
1364cc7b954SJohn Fastabend 	spin_lock(&dev_map_lock);
1374cc7b954SJohn Fastabend 	list_add_tail_rcu(&dtab->list, &dev_map_list);
1384cc7b954SJohn Fastabend 	spin_unlock(&dev_map_lock);
139546ac1ffSJohn Fastabend 
140af4d045cSDaniel Borkmann 	return &dtab->map;
141546ac1ffSJohn Fastabend free_dtab:
14211393cc9SJohn Fastabend 	free_percpu(dtab->flush_needed);
143546ac1ffSJohn Fastabend 	kfree(dtab);
144582db7e0STobias Klauser 	return ERR_PTR(err);
145546ac1ffSJohn Fastabend }
146546ac1ffSJohn Fastabend 
147546ac1ffSJohn Fastabend static void dev_map_free(struct bpf_map *map)
148546ac1ffSJohn Fastabend {
149546ac1ffSJohn Fastabend 	struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
15011393cc9SJohn Fastabend 	int i, cpu;
151546ac1ffSJohn Fastabend 
152546ac1ffSJohn Fastabend 	/* At this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
153546ac1ffSJohn Fastabend 	 * so the programs (can be more than one that used this map) were
154546ac1ffSJohn Fastabend 	 * disconnected from events. Wait for outstanding critical sections in
155546ac1ffSJohn Fastabend 	 * these programs to complete. The rcu critical section only guarantees
156546ac1ffSJohn Fastabend 	 * no further reads against netdev_map. It does __not__ ensure pending
157546ac1ffSJohn Fastabend 	 * flush operations (if any) are complete.
158546ac1ffSJohn Fastabend 	 */
159274043c6SDaniel Borkmann 
160274043c6SDaniel Borkmann 	spin_lock(&dev_map_lock);
161274043c6SDaniel Borkmann 	list_del_rcu(&dtab->list);
162274043c6SDaniel Borkmann 	spin_unlock(&dev_map_lock);
163274043c6SDaniel Borkmann 
164546ac1ffSJohn Fastabend 	synchronize_rcu();
165546ac1ffSJohn Fastabend 
16611393cc9SJohn Fastabend 	/* To ensure all pending flush operations have completed wait for flush
16711393cc9SJohn Fastabend 	 * bitmap to indicate all flush_needed bits to be zero on _all_ cpus.
16811393cc9SJohn Fastabend 	 * Because the above synchronize_rcu() ensures the map is disconnected
16911393cc9SJohn Fastabend 	 * from the program we can assume no new bits will be set.
17011393cc9SJohn Fastabend 	 */
17111393cc9SJohn Fastabend 	for_each_online_cpu(cpu) {
17211393cc9SJohn Fastabend 		unsigned long *bitmap = per_cpu_ptr(dtab->flush_needed, cpu);
17311393cc9SJohn Fastabend 
17411393cc9SJohn Fastabend 		while (!bitmap_empty(bitmap, dtab->map.max_entries))
175374fb014SJohn Fastabend 			cond_resched();
17611393cc9SJohn Fastabend 	}
17711393cc9SJohn Fastabend 
178546ac1ffSJohn Fastabend 	for (i = 0; i < dtab->map.max_entries; i++) {
179546ac1ffSJohn Fastabend 		struct bpf_dtab_netdev *dev;
180546ac1ffSJohn Fastabend 
181546ac1ffSJohn Fastabend 		dev = dtab->netdev_map[i];
182546ac1ffSJohn Fastabend 		if (!dev)
183546ac1ffSJohn Fastabend 			continue;
184546ac1ffSJohn Fastabend 
185546ac1ffSJohn Fastabend 		dev_put(dev->dev);
186546ac1ffSJohn Fastabend 		kfree(dev);
187546ac1ffSJohn Fastabend 	}
188546ac1ffSJohn Fastabend 
18911393cc9SJohn Fastabend 	free_percpu(dtab->flush_needed);
190546ac1ffSJohn Fastabend 	bpf_map_area_free(dtab->netdev_map);
191546ac1ffSJohn Fastabend 	kfree(dtab);
192546ac1ffSJohn Fastabend }
193546ac1ffSJohn Fastabend 
194546ac1ffSJohn Fastabend static int dev_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
195546ac1ffSJohn Fastabend {
196546ac1ffSJohn Fastabend 	struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
197546ac1ffSJohn Fastabend 	u32 index = key ? *(u32 *)key : U32_MAX;
198af4d045cSDaniel Borkmann 	u32 *next = next_key;
199546ac1ffSJohn Fastabend 
200546ac1ffSJohn Fastabend 	if (index >= dtab->map.max_entries) {
201546ac1ffSJohn Fastabend 		*next = 0;
202546ac1ffSJohn Fastabend 		return 0;
203546ac1ffSJohn Fastabend 	}
204546ac1ffSJohn Fastabend 
205546ac1ffSJohn Fastabend 	if (index == dtab->map.max_entries - 1)
206546ac1ffSJohn Fastabend 		return -ENOENT;
207546ac1ffSJohn Fastabend 	*next = index + 1;
208546ac1ffSJohn Fastabend 	return 0;
209546ac1ffSJohn Fastabend }
210546ac1ffSJohn Fastabend 
211af4d045cSDaniel Borkmann void __dev_map_insert_ctx(struct bpf_map *map, u32 bit)
21211393cc9SJohn Fastabend {
21311393cc9SJohn Fastabend 	struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
21411393cc9SJohn Fastabend 	unsigned long *bitmap = this_cpu_ptr(dtab->flush_needed);
21511393cc9SJohn Fastabend 
216af4d045cSDaniel Borkmann 	__set_bit(bit, bitmap);
21797f91a7cSJohn Fastabend }
21897f91a7cSJohn Fastabend 
2195d053f9dSJesper Dangaard Brouer static int bq_xmit_all(struct bpf_dtab_netdev *obj,
220c1ece6b2SJesper Dangaard Brouer 		       struct xdp_bulk_queue *bq, u32 flags)
2215d053f9dSJesper Dangaard Brouer {
2225d053f9dSJesper Dangaard Brouer 	struct net_device *dev = obj->dev;
223e74de52eSJesper Dangaard Brouer 	int sent = 0, drops = 0, err = 0;
2245d053f9dSJesper Dangaard Brouer 	int i;
2255d053f9dSJesper Dangaard Brouer 
2265d053f9dSJesper Dangaard Brouer 	if (unlikely(!bq->count))
2275d053f9dSJesper Dangaard Brouer 		return 0;
2285d053f9dSJesper Dangaard Brouer 
2295d053f9dSJesper Dangaard Brouer 	for (i = 0; i < bq->count; i++) {
2305d053f9dSJesper Dangaard Brouer 		struct xdp_frame *xdpf = bq->q[i];
2315d053f9dSJesper Dangaard Brouer 
2325d053f9dSJesper Dangaard Brouer 		prefetch(xdpf);
2335d053f9dSJesper Dangaard Brouer 	}
2345d053f9dSJesper Dangaard Brouer 
235c1ece6b2SJesper Dangaard Brouer 	sent = dev->netdev_ops->ndo_xdp_xmit(dev, bq->count, bq->q, flags);
236735fc405SJesper Dangaard Brouer 	if (sent < 0) {
237e74de52eSJesper Dangaard Brouer 		err = sent;
238735fc405SJesper Dangaard Brouer 		sent = 0;
239735fc405SJesper Dangaard Brouer 		goto error;
24038edddb8SJesper Dangaard Brouer 	}
241735fc405SJesper Dangaard Brouer 	drops = bq->count - sent;
242735fc405SJesper Dangaard Brouer out:
2435d053f9dSJesper Dangaard Brouer 	bq->count = 0;
2445d053f9dSJesper Dangaard Brouer 
24538edddb8SJesper Dangaard Brouer 	trace_xdp_devmap_xmit(&obj->dtab->map, obj->bit,
246e74de52eSJesper Dangaard Brouer 			      sent, drops, bq->dev_rx, dev, err);
24738edddb8SJesper Dangaard Brouer 	bq->dev_rx = NULL;
2485d053f9dSJesper Dangaard Brouer 	return 0;
249735fc405SJesper Dangaard Brouer error:
250735fc405SJesper Dangaard Brouer 	/* If ndo_xdp_xmit fails with an errno, no frames have been
251735fc405SJesper Dangaard Brouer 	 * xmit'ed and it's our responsibility to them free all.
252735fc405SJesper Dangaard Brouer 	 */
253735fc405SJesper Dangaard Brouer 	for (i = 0; i < bq->count; i++) {
254735fc405SJesper Dangaard Brouer 		struct xdp_frame *xdpf = bq->q[i];
255735fc405SJesper Dangaard Brouer 
256735fc405SJesper Dangaard Brouer 		/* RX path under NAPI protection, can return frames faster */
257735fc405SJesper Dangaard Brouer 		xdp_return_frame_rx_napi(xdpf);
258735fc405SJesper Dangaard Brouer 		drops++;
259735fc405SJesper Dangaard Brouer 	}
260735fc405SJesper Dangaard Brouer 	goto out;
2615d053f9dSJesper Dangaard Brouer }
2625d053f9dSJesper Dangaard Brouer 
26311393cc9SJohn Fastabend /* __dev_map_flush is called from xdp_do_flush_map() which _must_ be signaled
26411393cc9SJohn Fastabend  * from the driver before returning from its napi->poll() routine. The poll()
26511393cc9SJohn Fastabend  * routine is called either from busy_poll context or net_rx_action signaled
26611393cc9SJohn Fastabend  * from NET_RX_SOFTIRQ. Either way the poll routine must complete before the
26711393cc9SJohn Fastabend  * net device can be torn down. On devmap tear down we ensure the ctx bitmap
26811393cc9SJohn Fastabend  * is zeroed before completing to ensure all flush operations have completed.
26911393cc9SJohn Fastabend  */
27011393cc9SJohn Fastabend void __dev_map_flush(struct bpf_map *map)
27111393cc9SJohn Fastabend {
27211393cc9SJohn Fastabend 	struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
27311393cc9SJohn Fastabend 	unsigned long *bitmap = this_cpu_ptr(dtab->flush_needed);
27411393cc9SJohn Fastabend 	u32 bit;
27511393cc9SJohn Fastabend 
27611393cc9SJohn Fastabend 	for_each_set_bit(bit, bitmap, map->max_entries) {
27711393cc9SJohn Fastabend 		struct bpf_dtab_netdev *dev = READ_ONCE(dtab->netdev_map[bit]);
2785d053f9dSJesper Dangaard Brouer 		struct xdp_bulk_queue *bq;
27911393cc9SJohn Fastabend 
28011393cc9SJohn Fastabend 		/* This is possible if the dev entry is removed by user space
28111393cc9SJohn Fastabend 		 * between xdp redirect and flush op.
28211393cc9SJohn Fastabend 		 */
28311393cc9SJohn Fastabend 		if (unlikely(!dev))
28411393cc9SJohn Fastabend 			continue;
28511393cc9SJohn Fastabend 
28611393cc9SJohn Fastabend 		__clear_bit(bit, bitmap);
2875d053f9dSJesper Dangaard Brouer 
2885d053f9dSJesper Dangaard Brouer 		bq = this_cpu_ptr(dev->bulkq);
289c1ece6b2SJesper Dangaard Brouer 		bq_xmit_all(dev, bq, XDP_XMIT_FLUSH);
29011393cc9SJohn Fastabend 	}
29111393cc9SJohn Fastabend }
29211393cc9SJohn Fastabend 
293546ac1ffSJohn Fastabend /* rcu_read_lock (from syscall and BPF contexts) ensures that if a delete and/or
294546ac1ffSJohn Fastabend  * update happens in parallel here a dev_put wont happen until after reading the
295546ac1ffSJohn Fastabend  * ifindex.
296546ac1ffSJohn Fastabend  */
29767f29e07SJesper Dangaard Brouer struct bpf_dtab_netdev *__dev_map_lookup_elem(struct bpf_map *map, u32 key)
298546ac1ffSJohn Fastabend {
299546ac1ffSJohn Fastabend 	struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
30067f29e07SJesper Dangaard Brouer 	struct bpf_dtab_netdev *obj;
301546ac1ffSJohn Fastabend 
302af4d045cSDaniel Borkmann 	if (key >= map->max_entries)
303546ac1ffSJohn Fastabend 		return NULL;
304546ac1ffSJohn Fastabend 
30567f29e07SJesper Dangaard Brouer 	obj = READ_ONCE(dtab->netdev_map[key]);
30667f29e07SJesper Dangaard Brouer 	return obj;
30767f29e07SJesper Dangaard Brouer }
30867f29e07SJesper Dangaard Brouer 
3095d053f9dSJesper Dangaard Brouer /* Runs under RCU-read-side, plus in softirq under NAPI protection.
3105d053f9dSJesper Dangaard Brouer  * Thus, safe percpu variable access.
3115d053f9dSJesper Dangaard Brouer  */
31238edddb8SJesper Dangaard Brouer static int bq_enqueue(struct bpf_dtab_netdev *obj, struct xdp_frame *xdpf,
31338edddb8SJesper Dangaard Brouer 		      struct net_device *dev_rx)
31438edddb8SJesper Dangaard Brouer 
3155d053f9dSJesper Dangaard Brouer {
3165d053f9dSJesper Dangaard Brouer 	struct xdp_bulk_queue *bq = this_cpu_ptr(obj->bulkq);
3175d053f9dSJesper Dangaard Brouer 
3185d053f9dSJesper Dangaard Brouer 	if (unlikely(bq->count == DEV_MAP_BULK_SIZE))
319c1ece6b2SJesper Dangaard Brouer 		bq_xmit_all(obj, bq, 0);
3205d053f9dSJesper Dangaard Brouer 
32138edddb8SJesper Dangaard Brouer 	/* Ingress dev_rx will be the same for all xdp_frame's in
32238edddb8SJesper Dangaard Brouer 	 * bulk_queue, because bq stored per-CPU and must be flushed
32338edddb8SJesper Dangaard Brouer 	 * from net_device drivers NAPI func end.
32438edddb8SJesper Dangaard Brouer 	 */
32538edddb8SJesper Dangaard Brouer 	if (!bq->dev_rx)
32638edddb8SJesper Dangaard Brouer 		bq->dev_rx = dev_rx;
32738edddb8SJesper Dangaard Brouer 
3285d053f9dSJesper Dangaard Brouer 	bq->q[bq->count++] = xdpf;
3295d053f9dSJesper Dangaard Brouer 	return 0;
3305d053f9dSJesper Dangaard Brouer }
3315d053f9dSJesper Dangaard Brouer 
33238edddb8SJesper Dangaard Brouer int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
33338edddb8SJesper Dangaard Brouer 		    struct net_device *dev_rx)
33467f29e07SJesper Dangaard Brouer {
33567f29e07SJesper Dangaard Brouer 	struct net_device *dev = dst->dev;
33667f29e07SJesper Dangaard Brouer 	struct xdp_frame *xdpf;
337d8d7218aSToshiaki Makita 	int err;
33867f29e07SJesper Dangaard Brouer 
33967f29e07SJesper Dangaard Brouer 	if (!dev->netdev_ops->ndo_xdp_xmit)
34067f29e07SJesper Dangaard Brouer 		return -EOPNOTSUPP;
34167f29e07SJesper Dangaard Brouer 
342d8d7218aSToshiaki Makita 	err = xdp_ok_fwd_dev(dev, xdp->data_end - xdp->data);
343d8d7218aSToshiaki Makita 	if (unlikely(err))
344d8d7218aSToshiaki Makita 		return err;
345d8d7218aSToshiaki Makita 
34667f29e07SJesper Dangaard Brouer 	xdpf = convert_to_xdp_frame(xdp);
34767f29e07SJesper Dangaard Brouer 	if (unlikely(!xdpf))
34867f29e07SJesper Dangaard Brouer 		return -EOVERFLOW;
34967f29e07SJesper Dangaard Brouer 
35038edddb8SJesper Dangaard Brouer 	return bq_enqueue(dst, xdpf, dev_rx);
351546ac1ffSJohn Fastabend }
352546ac1ffSJohn Fastabend 
3536d5fc195SToshiaki Makita int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
3546d5fc195SToshiaki Makita 			     struct bpf_prog *xdp_prog)
3556d5fc195SToshiaki Makita {
3566d5fc195SToshiaki Makita 	int err;
3576d5fc195SToshiaki Makita 
358d8d7218aSToshiaki Makita 	err = xdp_ok_fwd_dev(dst->dev, skb->len);
3596d5fc195SToshiaki Makita 	if (unlikely(err))
3606d5fc195SToshiaki Makita 		return err;
3616d5fc195SToshiaki Makita 	skb->dev = dst->dev;
3626d5fc195SToshiaki Makita 	generic_xdp_tx(skb, xdp_prog);
3636d5fc195SToshiaki Makita 
3646d5fc195SToshiaki Makita 	return 0;
3656d5fc195SToshiaki Makita }
3666d5fc195SToshiaki Makita 
367af4d045cSDaniel Borkmann static void *dev_map_lookup_elem(struct bpf_map *map, void *key)
36811393cc9SJohn Fastabend {
36967f29e07SJesper Dangaard Brouer 	struct bpf_dtab_netdev *obj = __dev_map_lookup_elem(map, *(u32 *)key);
37071b2c87dSColin Ian King 	struct net_device *dev = obj ? obj->dev : NULL;
371af4d045cSDaniel Borkmann 
372af4d045cSDaniel Borkmann 	return dev ? &dev->ifindex : NULL;
373af4d045cSDaniel Borkmann }
374af4d045cSDaniel Borkmann 
375af4d045cSDaniel Borkmann static void dev_map_flush_old(struct bpf_dtab_netdev *dev)
376af4d045cSDaniel Borkmann {
377c1ece6b2SJesper Dangaard Brouer 	if (dev->dev->netdev_ops->ndo_xdp_xmit) {
3785d053f9dSJesper Dangaard Brouer 		struct xdp_bulk_queue *bq;
37911393cc9SJohn Fastabend 		unsigned long *bitmap;
3805d053f9dSJesper Dangaard Brouer 
38111393cc9SJohn Fastabend 		int cpu;
38211393cc9SJohn Fastabend 
38311393cc9SJohn Fastabend 		for_each_online_cpu(cpu) {
384af4d045cSDaniel Borkmann 			bitmap = per_cpu_ptr(dev->dtab->flush_needed, cpu);
385af4d045cSDaniel Borkmann 			__clear_bit(dev->bit, bitmap);
38611393cc9SJohn Fastabend 
3875d053f9dSJesper Dangaard Brouer 			bq = per_cpu_ptr(dev->bulkq, cpu);
388c1ece6b2SJesper Dangaard Brouer 			bq_xmit_all(dev, bq, XDP_XMIT_FLUSH);
38911393cc9SJohn Fastabend 		}
39011393cc9SJohn Fastabend 	}
39111393cc9SJohn Fastabend }
39211393cc9SJohn Fastabend 
393546ac1ffSJohn Fastabend static void __dev_map_entry_free(struct rcu_head *rcu)
394546ac1ffSJohn Fastabend {
395af4d045cSDaniel Borkmann 	struct bpf_dtab_netdev *dev;
396546ac1ffSJohn Fastabend 
397af4d045cSDaniel Borkmann 	dev = container_of(rcu, struct bpf_dtab_netdev, rcu);
398af4d045cSDaniel Borkmann 	dev_map_flush_old(dev);
3995d053f9dSJesper Dangaard Brouer 	free_percpu(dev->bulkq);
400af4d045cSDaniel Borkmann 	dev_put(dev->dev);
401af4d045cSDaniel Borkmann 	kfree(dev);
402546ac1ffSJohn Fastabend }
403546ac1ffSJohn Fastabend 
404546ac1ffSJohn Fastabend static int dev_map_delete_elem(struct bpf_map *map, void *key)
405546ac1ffSJohn Fastabend {
406546ac1ffSJohn Fastabend 	struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
407546ac1ffSJohn Fastabend 	struct bpf_dtab_netdev *old_dev;
408546ac1ffSJohn Fastabend 	int k = *(u32 *)key;
409546ac1ffSJohn Fastabend 
410546ac1ffSJohn Fastabend 	if (k >= map->max_entries)
411546ac1ffSJohn Fastabend 		return -EINVAL;
412546ac1ffSJohn Fastabend 
413af4d045cSDaniel Borkmann 	/* Use call_rcu() here to ensure any rcu critical sections have
414af4d045cSDaniel Borkmann 	 * completed, but this does not guarantee a flush has happened
415546ac1ffSJohn Fastabend 	 * yet. Because driver side rcu_read_lock/unlock only protects the
416546ac1ffSJohn Fastabend 	 * running XDP program. However, for pending flush operations the
417546ac1ffSJohn Fastabend 	 * dev and ctx are stored in another per cpu map. And additionally,
418546ac1ffSJohn Fastabend 	 * the driver tear down ensures all soft irqs are complete before
419546ac1ffSJohn Fastabend 	 * removing the net device in the case of dev_put equals zero.
420546ac1ffSJohn Fastabend 	 */
421546ac1ffSJohn Fastabend 	old_dev = xchg(&dtab->netdev_map[k], NULL);
422546ac1ffSJohn Fastabend 	if (old_dev)
423546ac1ffSJohn Fastabend 		call_rcu(&old_dev->rcu, __dev_map_entry_free);
424546ac1ffSJohn Fastabend 	return 0;
425546ac1ffSJohn Fastabend }
426546ac1ffSJohn Fastabend 
427546ac1ffSJohn Fastabend static int dev_map_update_elem(struct bpf_map *map, void *key, void *value,
428546ac1ffSJohn Fastabend 				u64 map_flags)
429546ac1ffSJohn Fastabend {
430546ac1ffSJohn Fastabend 	struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
431546ac1ffSJohn Fastabend 	struct net *net = current->nsproxy->net_ns;
4325d053f9dSJesper Dangaard Brouer 	gfp_t gfp = GFP_ATOMIC | __GFP_NOWARN;
433546ac1ffSJohn Fastabend 	struct bpf_dtab_netdev *dev, *old_dev;
434546ac1ffSJohn Fastabend 	u32 i = *(u32 *)key;
435546ac1ffSJohn Fastabend 	u32 ifindex = *(u32 *)value;
436546ac1ffSJohn Fastabend 
437546ac1ffSJohn Fastabend 	if (unlikely(map_flags > BPF_EXIST))
438546ac1ffSJohn Fastabend 		return -EINVAL;
439546ac1ffSJohn Fastabend 	if (unlikely(i >= dtab->map.max_entries))
440546ac1ffSJohn Fastabend 		return -E2BIG;
441546ac1ffSJohn Fastabend 	if (unlikely(map_flags == BPF_NOEXIST))
442546ac1ffSJohn Fastabend 		return -EEXIST;
443546ac1ffSJohn Fastabend 
444546ac1ffSJohn Fastabend 	if (!ifindex) {
445546ac1ffSJohn Fastabend 		dev = NULL;
446546ac1ffSJohn Fastabend 	} else {
4475d053f9dSJesper Dangaard Brouer 		dev = kmalloc_node(sizeof(*dev), gfp, map->numa_node);
448546ac1ffSJohn Fastabend 		if (!dev)
449546ac1ffSJohn Fastabend 			return -ENOMEM;
450546ac1ffSJohn Fastabend 
4515d053f9dSJesper Dangaard Brouer 		dev->bulkq = __alloc_percpu_gfp(sizeof(*dev->bulkq),
4525d053f9dSJesper Dangaard Brouer 						sizeof(void *), gfp);
4535d053f9dSJesper Dangaard Brouer 		if (!dev->bulkq) {
4545d053f9dSJesper Dangaard Brouer 			kfree(dev);
4555d053f9dSJesper Dangaard Brouer 			return -ENOMEM;
4565d053f9dSJesper Dangaard Brouer 		}
4575d053f9dSJesper Dangaard Brouer 
458546ac1ffSJohn Fastabend 		dev->dev = dev_get_by_index(net, ifindex);
459546ac1ffSJohn Fastabend 		if (!dev->dev) {
4605d053f9dSJesper Dangaard Brouer 			free_percpu(dev->bulkq);
461546ac1ffSJohn Fastabend 			kfree(dev);
462546ac1ffSJohn Fastabend 			return -EINVAL;
463546ac1ffSJohn Fastabend 		}
464546ac1ffSJohn Fastabend 
465af4d045cSDaniel Borkmann 		dev->bit = i;
466546ac1ffSJohn Fastabend 		dev->dtab = dtab;
467546ac1ffSJohn Fastabend 	}
468546ac1ffSJohn Fastabend 
469546ac1ffSJohn Fastabend 	/* Use call_rcu() here to ensure rcu critical sections have completed
470546ac1ffSJohn Fastabend 	 * Remembering the driver side flush operation will happen before the
471546ac1ffSJohn Fastabend 	 * net device is removed.
472546ac1ffSJohn Fastabend 	 */
473546ac1ffSJohn Fastabend 	old_dev = xchg(&dtab->netdev_map[i], dev);
474546ac1ffSJohn Fastabend 	if (old_dev)
475546ac1ffSJohn Fastabend 		call_rcu(&old_dev->rcu, __dev_map_entry_free);
476546ac1ffSJohn Fastabend 
477546ac1ffSJohn Fastabend 	return 0;
478546ac1ffSJohn Fastabend }
479546ac1ffSJohn Fastabend 
480546ac1ffSJohn Fastabend const struct bpf_map_ops dev_map_ops = {
481546ac1ffSJohn Fastabend 	.map_alloc = dev_map_alloc,
482546ac1ffSJohn Fastabend 	.map_free = dev_map_free,
483546ac1ffSJohn Fastabend 	.map_get_next_key = dev_map_get_next_key,
484546ac1ffSJohn Fastabend 	.map_lookup_elem = dev_map_lookup_elem,
485546ac1ffSJohn Fastabend 	.map_update_elem = dev_map_update_elem,
486546ac1ffSJohn Fastabend 	.map_delete_elem = dev_map_delete_elem,
487*e8d2bec0SDaniel Borkmann 	.map_check_btf = map_check_no_btf,
488546ac1ffSJohn Fastabend };
4892ddf71e2SJohn Fastabend 
4902ddf71e2SJohn Fastabend static int dev_map_notification(struct notifier_block *notifier,
4912ddf71e2SJohn Fastabend 				ulong event, void *ptr)
4922ddf71e2SJohn Fastabend {
4932ddf71e2SJohn Fastabend 	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
4942ddf71e2SJohn Fastabend 	struct bpf_dtab *dtab;
4952ddf71e2SJohn Fastabend 	int i;
4962ddf71e2SJohn Fastabend 
4972ddf71e2SJohn Fastabend 	switch (event) {
4982ddf71e2SJohn Fastabend 	case NETDEV_UNREGISTER:
4994cc7b954SJohn Fastabend 		/* This rcu_read_lock/unlock pair is needed because
5004cc7b954SJohn Fastabend 		 * dev_map_list is an RCU list AND to ensure a delete
5014cc7b954SJohn Fastabend 		 * operation does not free a netdev_map entry while we
5024cc7b954SJohn Fastabend 		 * are comparing it against the netdev being unregistered.
5034cc7b954SJohn Fastabend 		 */
5044cc7b954SJohn Fastabend 		rcu_read_lock();
5054cc7b954SJohn Fastabend 		list_for_each_entry_rcu(dtab, &dev_map_list, list) {
5062ddf71e2SJohn Fastabend 			for (i = 0; i < dtab->map.max_entries; i++) {
5074cc7b954SJohn Fastabend 				struct bpf_dtab_netdev *dev, *odev;
5082ddf71e2SJohn Fastabend 
5094cc7b954SJohn Fastabend 				dev = READ_ONCE(dtab->netdev_map[i]);
5102ddf71e2SJohn Fastabend 				if (!dev ||
5112ddf71e2SJohn Fastabend 				    dev->dev->ifindex != netdev->ifindex)
5122ddf71e2SJohn Fastabend 					continue;
5134cc7b954SJohn Fastabend 				odev = cmpxchg(&dtab->netdev_map[i], dev, NULL);
5144cc7b954SJohn Fastabend 				if (dev == odev)
5152ddf71e2SJohn Fastabend 					call_rcu(&dev->rcu,
5162ddf71e2SJohn Fastabend 						 __dev_map_entry_free);
5172ddf71e2SJohn Fastabend 			}
5182ddf71e2SJohn Fastabend 		}
5194cc7b954SJohn Fastabend 		rcu_read_unlock();
5202ddf71e2SJohn Fastabend 		break;
5212ddf71e2SJohn Fastabend 	default:
5222ddf71e2SJohn Fastabend 		break;
5232ddf71e2SJohn Fastabend 	}
5242ddf71e2SJohn Fastabend 	return NOTIFY_OK;
5252ddf71e2SJohn Fastabend }
5262ddf71e2SJohn Fastabend 
5272ddf71e2SJohn Fastabend static struct notifier_block dev_map_notifier = {
5282ddf71e2SJohn Fastabend 	.notifier_call = dev_map_notification,
5292ddf71e2SJohn Fastabend };
5302ddf71e2SJohn Fastabend 
5312ddf71e2SJohn Fastabend static int __init dev_map_init(void)
5322ddf71e2SJohn Fastabend {
53367f29e07SJesper Dangaard Brouer 	/* Assure tracepoint shadow struct _bpf_dtab_netdev is in sync */
53467f29e07SJesper Dangaard Brouer 	BUILD_BUG_ON(offsetof(struct bpf_dtab_netdev, dev) !=
53567f29e07SJesper Dangaard Brouer 		     offsetof(struct _bpf_dtab_netdev, dev));
5362ddf71e2SJohn Fastabend 	register_netdevice_notifier(&dev_map_notifier);
5372ddf71e2SJohn Fastabend 	return 0;
5382ddf71e2SJohn Fastabend }
5392ddf71e2SJohn Fastabend 
5402ddf71e2SJohn Fastabend subsys_initcall(dev_map_init);
541