Thomas Gleixner | 5b497af | 2019-05-29 07:18:09 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 2 | /* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 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 |
Toke Høiland-Jørgensen | d5df283 | 2019-06-28 11:12:34 +0200 | [diff] [blame] | 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. |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 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. |
John Fastabend | 2ddf71e | 2017-07-17 09:30:02 -0700 | [diff] [blame] | 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. |
John Fastabend | 4cc7b95 | 2017-08-04 22:02:19 -0700 | [diff] [blame] | 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. |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 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. |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 46 | */ |
| 47 | #include <linux/bpf.h> |
Jesper Dangaard Brouer | 67f29e0 | 2018-05-24 16:45:46 +0200 | [diff] [blame] | 48 | #include <net/xdp.h> |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 49 | #include <linux/filter.h> |
Jesper Dangaard Brouer | 67f29e0 | 2018-05-24 16:45:46 +0200 | [diff] [blame] | 50 | #include <trace/events/xdp.h> |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 51 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 52 | #define DEV_CREATE_FLAG_MASK \ |
| 53 | (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY) |
| 54 | |
Toke Høiland-Jørgensen | 75ccae6 | 2020-01-16 16:14:44 +0100 | [diff] [blame] | 55 | struct xdp_dev_bulk_queue { |
Jesper Dangaard Brouer | 5d053f9 | 2018-05-24 16:45:51 +0200 | [diff] [blame] | 56 | struct xdp_frame *q[DEV_MAP_BULK_SIZE]; |
Toke Høiland-Jørgensen | d5df283 | 2019-06-28 11:12:34 +0200 | [diff] [blame] | 57 | struct list_head flush_node; |
Toke Høiland-Jørgensen | 75ccae6 | 2020-01-16 16:14:44 +0100 | [diff] [blame] | 58 | struct net_device *dev; |
Jesper Dangaard Brouer | 38edddb | 2018-05-24 16:45:57 +0200 | [diff] [blame] | 59 | struct net_device *dev_rx; |
Jesper Dangaard Brouer | 5d053f9 | 2018-05-24 16:45:51 +0200 | [diff] [blame] | 60 | unsigned int count; |
| 61 | }; |
| 62 | |
David Ahern | 7f1c042 | 2020-05-29 16:07:12 -0600 | [diff] [blame] | 63 | /* DEVMAP values */ |
| 64 | struct bpf_devmap_val { |
| 65 | u32 ifindex; /* device index */ |
David Ahern | fbee97f | 2020-05-29 16:07:13 -0600 | [diff] [blame^] | 66 | union { |
| 67 | int fd; /* prog fd on map write */ |
| 68 | u32 id; /* prog id on map read */ |
| 69 | } bpf_prog; |
David Ahern | 7f1c042 | 2020-05-29 16:07:12 -0600 | [diff] [blame] | 70 | }; |
| 71 | |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 72 | struct bpf_dtab_netdev { |
Jesper Dangaard Brouer | 67f29e0 | 2018-05-24 16:45:46 +0200 | [diff] [blame] | 73 | struct net_device *dev; /* must be first member, due to tracepoint */ |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 74 | struct hlist_node index_hlist; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 75 | struct bpf_dtab *dtab; |
David Ahern | fbee97f | 2020-05-29 16:07:13 -0600 | [diff] [blame^] | 76 | struct bpf_prog *xdp_prog; |
Daniel Borkmann | af4d045 | 2017-08-23 01:47:54 +0200 | [diff] [blame] | 77 | struct rcu_head rcu; |
Toke Høiland-Jørgensen | 75ccae6 | 2020-01-16 16:14:44 +0100 | [diff] [blame] | 78 | unsigned int idx; |
David Ahern | 7f1c042 | 2020-05-29 16:07:12 -0600 | [diff] [blame] | 79 | struct bpf_devmap_val val; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | struct bpf_dtab { |
| 83 | struct bpf_map map; |
Toke Høiland-Jørgensen | 071cdec | 2019-11-21 14:36:12 +0100 | [diff] [blame] | 84 | struct bpf_dtab_netdev **netdev_map; /* DEVMAP type only */ |
John Fastabend | 2ddf71e | 2017-07-17 09:30:02 -0700 | [diff] [blame] | 85 | struct list_head list; |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 86 | |
| 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 Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
Toke Høiland-Jørgensen | 1d23388 | 2020-01-16 16:14:45 +0100 | [diff] [blame] | 94 | static DEFINE_PER_CPU(struct list_head, dev_flush_list); |
John Fastabend | 4cc7b95 | 2017-08-04 22:02:19 -0700 | [diff] [blame] | 95 | static DEFINE_SPINLOCK(dev_map_lock); |
John Fastabend | 2ddf71e | 2017-07-17 09:30:02 -0700 | [diff] [blame] | 96 | static LIST_HEAD(dev_map_list); |
| 97 | |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 98 | static 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ørgensen | 071cdec | 2019-11-21 14:36:12 +0100 | [diff] [blame] | 111 | static 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ørgensen | fca16e5 | 2019-07-26 18:06:53 +0200 | [diff] [blame] | 117 | static int dev_map_init_map(struct bpf_dtab *dtab, union bpf_attr *attr) |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 118 | { |
David Ahern | fbee97f | 2020-05-29 16:07:13 -0600 | [diff] [blame^] | 119 | u32 valsize = attr->value_size; |
Björn Töpel | 9636000 | 2019-12-19 07:10:03 +0100 | [diff] [blame] | 120 | u64 cost = 0; |
| 121 | int err; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 122 | |
David Ahern | fbee97f | 2020-05-29 16:07:13 -0600 | [diff] [blame^] | 123 | /* check sanity of attributes. 2 value sizes supported: |
| 124 | * 4 bytes: ifindex |
| 125 | * 8 bytes: ifindex + prog fd |
| 126 | */ |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 127 | if (attr->max_entries == 0 || attr->key_size != 4 || |
David Ahern | fbee97f | 2020-05-29 16:07:13 -0600 | [diff] [blame^] | 128 | (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ørgensen | fca16e5 | 2019-07-26 18:06:53 +0200 | [diff] [blame] | 131 | return -EINVAL; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 132 | |
Toke Høiland-Jørgensen | 0cdbb4b | 2019-06-28 11:12:35 +0200 | [diff] [blame] | 133 | /* 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 Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 138 | |
Jakub Kicinski | bd47564 | 2018-01-11 20:29:06 -0800 | [diff] [blame] | 139 | bpf_map_init_from_attr(&dtab->map, attr); |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 140 | |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 141 | 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ørgensen | 05679ca | 2019-10-17 12:57:02 +0200 | [diff] [blame] | 146 | cost += (u64) sizeof(struct hlist_head) * dtab->n_buckets; |
Toke Høiland-Jørgensen | 071cdec | 2019-11-21 14:36:12 +0100 | [diff] [blame] | 147 | } else { |
| 148 | cost += (u64) dtab->map.max_entries * sizeof(struct bpf_dtab_netdev *); |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 151 | /* if map size is larger than memlock limit, reject it */ |
Roman Gushchin | c85d691 | 2019-05-29 18:03:59 -0700 | [diff] [blame] | 152 | err = bpf_map_charge_init(&dtab->map.memory, cost); |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 153 | if (err) |
Toke Høiland-Jørgensen | fca16e5 | 2019-07-26 18:06:53 +0200 | [diff] [blame] | 154 | return -EINVAL; |
Tobias Klauser | 582db7e | 2017-09-18 15:03:46 +0200 | [diff] [blame] | 155 | |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 156 | 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öpel | 9636000 | 2019-12-19 07:10:03 +0100 | [diff] [blame] | 159 | goto free_charge; |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 160 | |
| 161 | spin_lock_init(&dtab->index_lock); |
Toke Høiland-Jørgensen | 071cdec | 2019-11-21 14:36:12 +0100 | [diff] [blame] | 162 | } 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öpel | 9636000 | 2019-12-19 07:10:03 +0100 | [diff] [blame] | 167 | goto free_charge; |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Toke Høiland-Jørgensen | fca16e5 | 2019-07-26 18:06:53 +0200 | [diff] [blame] | 170 | return 0; |
Toke Høiland-Jørgensen | d5df283 | 2019-06-28 11:12:34 +0200 | [diff] [blame] | 171 | |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 172 | free_charge: |
| 173 | bpf_map_charge_finish(&dtab->map.memory); |
Toke Høiland-Jørgensen | fca16e5 | 2019-07-26 18:06:53 +0200 | [diff] [blame] | 174 | return -ENOMEM; |
| 175 | } |
| 176 | |
| 177 | static 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 Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static void dev_map_free(struct bpf_map *map) |
| 203 | { |
| 204 | struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map); |
Björn Töpel | 0536b85 | 2019-12-19 07:09:59 +0100 | [diff] [blame] | 205 | int i; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 206 | |
| 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 Fastabend | 42a84a8 | 2020-01-26 16:14:00 -0800 | [diff] [blame] | 209 | * 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 Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 215 | */ |
Daniel Borkmann | 274043c | 2017-08-21 01:48:12 +0200 | [diff] [blame] | 216 | |
| 217 | spin_lock(&dev_map_lock); |
| 218 | list_del_rcu(&dtab->list); |
| 219 | spin_unlock(&dev_map_lock); |
| 220 | |
Daniel Borkmann | f6069b9 | 2018-08-17 23:26:14 +0200 | [diff] [blame] | 221 | bpf_clear_redirect_map(map); |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 222 | synchronize_rcu(); |
| 223 | |
Eric Dumazet | 2baae35 | 2019-05-13 09:59:16 -0700 | [diff] [blame] | 224 | /* Make sure prior __dev_map_entry_free() have completed. */ |
| 225 | rcu_barrier(); |
| 226 | |
Toke Høiland-Jørgensen | 071cdec | 2019-11-21 14:36:12 +0100 | [diff] [blame] | 227 | 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 Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 232 | |
Toke Høiland-Jørgensen | 071cdec | 2019-11-21 14:36:12 +0100 | [diff] [blame] | 233 | head = dev_map_index_hash(dtab, i); |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 234 | |
Toke Høiland-Jørgensen | 071cdec | 2019-11-21 14:36:12 +0100 | [diff] [blame] | 235 | hlist_for_each_entry_safe(dev, next, head, index_hlist) { |
| 236 | hlist_del_rcu(&dev->index_hlist); |
David Ahern | fbee97f | 2020-05-29 16:07:13 -0600 | [diff] [blame^] | 237 | if (dev->xdp_prog) |
| 238 | bpf_prog_put(dev->xdp_prog); |
Toke Høiland-Jørgensen | 071cdec | 2019-11-21 14:36:12 +0100 | [diff] [blame] | 239 | 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 Ahern | fbee97f | 2020-05-29 16:07:13 -0600 | [diff] [blame^] | 253 | if (dev->xdp_prog) |
| 254 | bpf_prog_put(dev->xdp_prog); |
Toke Høiland-Jørgensen | 071cdec | 2019-11-21 14:36:12 +0100 | [diff] [blame] | 255 | dev_put(dev->dev); |
| 256 | kfree(dev); |
| 257 | } |
| 258 | |
| 259 | bpf_map_area_free(dtab->netdev_map); |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 260 | } |
| 261 | |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 262 | kfree(dtab); |
| 263 | } |
| 264 | |
| 265 | static 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 Borkmann | af4d045 | 2017-08-23 01:47:54 +0200 | [diff] [blame] | 269 | u32 *next = next_key; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 270 | |
| 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 Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 278 | *next = index + 1; |
| 279 | return 0; |
| 280 | } |
| 281 | |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 282 | struct 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 Grover | 485ec2e | 2020-01-23 17:34:38 +0530 | [diff] [blame] | 288 | hlist_for_each_entry_rcu(dev, head, index_hlist, |
| 289 | lockdep_is_held(&dtab->index_lock)) |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 290 | if (dev->idx == key) |
| 291 | return dev; |
| 292 | |
| 293 | return NULL; |
| 294 | } |
| 295 | |
| 296 | static 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 Ahern | fbee97f | 2020-05-29 16:07:13 -0600 | [diff] [blame^] | 341 | bool 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ørgensen | 75ccae6 | 2020-01-16 16:14:44 +0100 | [diff] [blame] | 351 | static int bq_xmit_all(struct xdp_dev_bulk_queue *bq, u32 flags) |
Jesper Dangaard Brouer | 5d053f9 | 2018-05-24 16:45:51 +0200 | [diff] [blame] | 352 | { |
Toke Høiland-Jørgensen | 75ccae6 | 2020-01-16 16:14:44 +0100 | [diff] [blame] | 353 | struct net_device *dev = bq->dev; |
Jesper Dangaard Brouer | e74de52 | 2018-05-24 16:46:17 +0200 | [diff] [blame] | 354 | int sent = 0, drops = 0, err = 0; |
Jesper Dangaard Brouer | 5d053f9 | 2018-05-24 16:45:51 +0200 | [diff] [blame] | 355 | 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 Brouer | c1ece6b | 2018-05-31 11:00:23 +0200 | [diff] [blame] | 366 | sent = dev->netdev_ops->ndo_xdp_xmit(dev, bq->count, bq->q, flags); |
Jesper Dangaard Brouer | 735fc40 | 2018-05-24 16:46:12 +0200 | [diff] [blame] | 367 | if (sent < 0) { |
Jesper Dangaard Brouer | e74de52 | 2018-05-24 16:46:17 +0200 | [diff] [blame] | 368 | err = sent; |
Jesper Dangaard Brouer | 735fc40 | 2018-05-24 16:46:12 +0200 | [diff] [blame] | 369 | sent = 0; |
| 370 | goto error; |
Jesper Dangaard Brouer | 5d053f9 | 2018-05-24 16:45:51 +0200 | [diff] [blame] | 371 | } |
Jesper Dangaard Brouer | 735fc40 | 2018-05-24 16:46:12 +0200 | [diff] [blame] | 372 | drops = bq->count - sent; |
| 373 | out: |
Jesper Dangaard Brouer | 5d053f9 | 2018-05-24 16:45:51 +0200 | [diff] [blame] | 374 | bq->count = 0; |
| 375 | |
Jesper Dangaard Brouer | 58aa94f | 2020-01-16 16:14:46 +0100 | [diff] [blame] | 376 | trace_xdp_devmap_xmit(bq->dev_rx, dev, sent, drops, err); |
Jesper Dangaard Brouer | 38edddb | 2018-05-24 16:45:57 +0200 | [diff] [blame] | 377 | bq->dev_rx = NULL; |
Toke Høiland-Jørgensen | d5df283 | 2019-06-28 11:12:34 +0200 | [diff] [blame] | 378 | __list_del_clearprev(&bq->flush_node); |
Jesper Dangaard Brouer | 5d053f9 | 2018-05-24 16:45:51 +0200 | [diff] [blame] | 379 | return 0; |
Jesper Dangaard Brouer | 735fc40 | 2018-05-24 16:46:12 +0200 | [diff] [blame] | 380 | error: |
| 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öpel | 0536b85 | 2019-12-19 07:09:59 +0100 | [diff] [blame] | 387 | xdp_return_frame_rx_napi(xdpf); |
Jesper Dangaard Brouer | 735fc40 | 2018-05-24 16:46:12 +0200 | [diff] [blame] | 388 | drops++; |
| 389 | } |
| 390 | goto out; |
Jesper Dangaard Brouer | 5d053f9 | 2018-05-24 16:45:51 +0200 | [diff] [blame] | 391 | } |
| 392 | |
Toke Høiland-Jørgensen | 1d23388 | 2020-01-16 16:14:45 +0100 | [diff] [blame] | 393 | /* __dev_flush is called from xdp_do_flush() which _must_ be signaled |
John Fastabend | 11393cc | 2017-07-17 09:29:40 -0700 | [diff] [blame] | 394 | * 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ørgensen | d5df283 | 2019-06-28 11:12:34 +0200 | [diff] [blame] | 397 | * 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 Fastabend | b23bfa5 | 2020-01-26 16:14:02 -0800 | [diff] [blame] | 399 | * 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 Fastabend | 11393cc | 2017-07-17 09:29:40 -0700 | [diff] [blame] | 402 | */ |
Toke Høiland-Jørgensen | 1d23388 | 2020-01-16 16:14:45 +0100 | [diff] [blame] | 403 | void __dev_flush(void) |
John Fastabend | 11393cc | 2017-07-17 09:29:40 -0700 | [diff] [blame] | 404 | { |
Toke Høiland-Jørgensen | 1d23388 | 2020-01-16 16:14:45 +0100 | [diff] [blame] | 405 | struct list_head *flush_list = this_cpu_ptr(&dev_flush_list); |
Toke Høiland-Jørgensen | 75ccae6 | 2020-01-16 16:14:44 +0100 | [diff] [blame] | 406 | struct xdp_dev_bulk_queue *bq, *tmp; |
John Fastabend | 11393cc | 2017-07-17 09:29:40 -0700 | [diff] [blame] | 407 | |
Toke Høiland-Jørgensen | d5df283 | 2019-06-28 11:12:34 +0200 | [diff] [blame] | 408 | list_for_each_entry_safe(bq, tmp, flush_list, flush_node) |
Björn Töpel | 0536b85 | 2019-12-19 07:09:59 +0100 | [diff] [blame] | 409 | bq_xmit_all(bq, XDP_XMIT_FLUSH); |
John Fastabend | 11393cc | 2017-07-17 09:29:40 -0700 | [diff] [blame] | 410 | } |
| 411 | |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 412 | /* 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 Brouer | 67f29e0 | 2018-05-24 16:45:46 +0200 | [diff] [blame] | 416 | struct bpf_dtab_netdev *__dev_map_lookup_elem(struct bpf_map *map, u32 key) |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 417 | { |
| 418 | struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map); |
Jesper Dangaard Brouer | 67f29e0 | 2018-05-24 16:45:46 +0200 | [diff] [blame] | 419 | struct bpf_dtab_netdev *obj; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 420 | |
Daniel Borkmann | af4d045 | 2017-08-23 01:47:54 +0200 | [diff] [blame] | 421 | if (key >= map->max_entries) |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 422 | return NULL; |
| 423 | |
Jesper Dangaard Brouer | 67f29e0 | 2018-05-24 16:45:46 +0200 | [diff] [blame] | 424 | obj = READ_ONCE(dtab->netdev_map[key]); |
| 425 | return obj; |
| 426 | } |
| 427 | |
Jesper Dangaard Brouer | 5d053f9 | 2018-05-24 16:45:51 +0200 | [diff] [blame] | 428 | /* Runs under RCU-read-side, plus in softirq under NAPI protection. |
| 429 | * Thus, safe percpu variable access. |
| 430 | */ |
Toke Høiland-Jørgensen | 75ccae6 | 2020-01-16 16:14:44 +0100 | [diff] [blame] | 431 | static int bq_enqueue(struct net_device *dev, struct xdp_frame *xdpf, |
Jesper Dangaard Brouer | 38edddb | 2018-05-24 16:45:57 +0200 | [diff] [blame] | 432 | struct net_device *dev_rx) |
Jesper Dangaard Brouer | 5d053f9 | 2018-05-24 16:45:51 +0200 | [diff] [blame] | 433 | { |
Toke Høiland-Jørgensen | 1d23388 | 2020-01-16 16:14:45 +0100 | [diff] [blame] | 434 | struct list_head *flush_list = this_cpu_ptr(&dev_flush_list); |
Toke Høiland-Jørgensen | 75ccae6 | 2020-01-16 16:14:44 +0100 | [diff] [blame] | 435 | struct xdp_dev_bulk_queue *bq = this_cpu_ptr(dev->xdp_bulkq); |
Jesper Dangaard Brouer | 5d053f9 | 2018-05-24 16:45:51 +0200 | [diff] [blame] | 436 | |
| 437 | if (unlikely(bq->count == DEV_MAP_BULK_SIZE)) |
Björn Töpel | 0536b85 | 2019-12-19 07:09:59 +0100 | [diff] [blame] | 438 | bq_xmit_all(bq, 0); |
Jesper Dangaard Brouer | 5d053f9 | 2018-05-24 16:45:51 +0200 | [diff] [blame] | 439 | |
Jesper Dangaard Brouer | 38edddb | 2018-05-24 16:45:57 +0200 | [diff] [blame] | 440 | /* 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 Brouer | 5d053f9 | 2018-05-24 16:45:51 +0200 | [diff] [blame] | 447 | bq->q[bq->count++] = xdpf; |
Toke Høiland-Jørgensen | d5df283 | 2019-06-28 11:12:34 +0200 | [diff] [blame] | 448 | |
| 449 | if (!bq->flush_node.prev) |
| 450 | list_add(&bq->flush_node, flush_list); |
| 451 | |
Jesper Dangaard Brouer | 5d053f9 | 2018-05-24 16:45:51 +0200 | [diff] [blame] | 452 | return 0; |
| 453 | } |
| 454 | |
Toke Høiland-Jørgensen | 1d23388 | 2020-01-16 16:14:45 +0100 | [diff] [blame] | 455 | static inline int __xdp_enqueue(struct net_device *dev, struct xdp_buff *xdp, |
| 456 | struct net_device *dev_rx) |
Jesper Dangaard Brouer | 67f29e0 | 2018-05-24 16:45:46 +0200 | [diff] [blame] | 457 | { |
Jesper Dangaard Brouer | 67f29e0 | 2018-05-24 16:45:46 +0200 | [diff] [blame] | 458 | struct xdp_frame *xdpf; |
Toshiaki Makita | d8d7218 | 2018-07-06 11:49:00 +0900 | [diff] [blame] | 459 | int err; |
Jesper Dangaard Brouer | 67f29e0 | 2018-05-24 16:45:46 +0200 | [diff] [blame] | 460 | |
| 461 | if (!dev->netdev_ops->ndo_xdp_xmit) |
| 462 | return -EOPNOTSUPP; |
| 463 | |
Toshiaki Makita | d8d7218 | 2018-07-06 11:49:00 +0900 | [diff] [blame] | 464 | err = xdp_ok_fwd_dev(dev, xdp->data_end - xdp->data); |
| 465 | if (unlikely(err)) |
| 466 | return err; |
| 467 | |
Jesper Dangaard Brouer | 67f29e0 | 2018-05-24 16:45:46 +0200 | [diff] [blame] | 468 | xdpf = convert_to_xdp_frame(xdp); |
| 469 | if (unlikely(!xdpf)) |
| 470 | return -EOVERFLOW; |
| 471 | |
Toke Høiland-Jørgensen | 75ccae6 | 2020-01-16 16:14:44 +0100 | [diff] [blame] | 472 | return bq_enqueue(dev, xdpf, dev_rx); |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 473 | } |
| 474 | |
David Ahern | fbee97f | 2020-05-29 16:07:13 -0600 | [diff] [blame^] | 475 | static 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ørgensen | 1d23388 | 2020-01-16 16:14:45 +0100 | [diff] [blame] | 499 | int 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 | |
| 505 | int 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 Ahern | fbee97f | 2020-05-29 16:07:13 -0600 | [diff] [blame^] | 510 | 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ørgensen | 1d23388 | 2020-01-16 16:14:45 +0100 | [diff] [blame] | 515 | return __xdp_enqueue(dev, xdp, dev_rx); |
| 516 | } |
| 517 | |
Toshiaki Makita | 6d5fc19 | 2018-06-14 11:07:42 +0900 | [diff] [blame] | 518 | int 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 Makita | d8d7218 | 2018-07-06 11:49:00 +0900 | [diff] [blame] | 523 | err = xdp_ok_fwd_dev(dst->dev, skb->len); |
Toshiaki Makita | 6d5fc19 | 2018-06-14 11:07:42 +0900 | [diff] [blame] | 524 | 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 Borkmann | af4d045 | 2017-08-23 01:47:54 +0200 | [diff] [blame] | 532 | static void *dev_map_lookup_elem(struct bpf_map *map, void *key) |
John Fastabend | 11393cc | 2017-07-17 09:29:40 -0700 | [diff] [blame] | 533 | { |
Jesper Dangaard Brouer | 67f29e0 | 2018-05-24 16:45:46 +0200 | [diff] [blame] | 534 | struct bpf_dtab_netdev *obj = __dev_map_lookup_elem(map, *(u32 *)key); |
Daniel Borkmann | af4d045 | 2017-08-23 01:47:54 +0200 | [diff] [blame] | 535 | |
David Ahern | 7f1c042 | 2020-05-29 16:07:12 -0600 | [diff] [blame] | 536 | return obj ? &obj->val : NULL; |
Daniel Borkmann | af4d045 | 2017-08-23 01:47:54 +0200 | [diff] [blame] | 537 | } |
| 538 | |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 539 | static 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 Ahern | 7f1c042 | 2020-05-29 16:07:12 -0600 | [diff] [blame] | 543 | return obj ? &obj->val : NULL; |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 544 | } |
| 545 | |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 546 | static void __dev_map_entry_free(struct rcu_head *rcu) |
| 547 | { |
Daniel Borkmann | af4d045 | 2017-08-23 01:47:54 +0200 | [diff] [blame] | 548 | struct bpf_dtab_netdev *dev; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 549 | |
Daniel Borkmann | af4d045 | 2017-08-23 01:47:54 +0200 | [diff] [blame] | 550 | dev = container_of(rcu, struct bpf_dtab_netdev, rcu); |
David Ahern | fbee97f | 2020-05-29 16:07:13 -0600 | [diff] [blame^] | 551 | if (dev->xdp_prog) |
| 552 | bpf_prog_put(dev->xdp_prog); |
Daniel Borkmann | af4d045 | 2017-08-23 01:47:54 +0200 | [diff] [blame] | 553 | dev_put(dev->dev); |
| 554 | kfree(dev); |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | static 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 Borkmann | af4d045 | 2017-08-23 01:47:54 +0200 | [diff] [blame] | 566 | /* Use call_rcu() here to ensure any rcu critical sections have |
John Fastabend | 42a84a8 | 2020-01-26 16:14:00 -0800 | [diff] [blame] | 567 | * 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 Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 572 | */ |
| 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ørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 579 | static 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ørgensen | fca16e5 | 2019-07-26 18:06:53 +0200 | [diff] [blame] | 601 | static struct bpf_dtab_netdev *__dev_map_alloc_node(struct net *net, |
| 602 | struct bpf_dtab *dtab, |
David Ahern | 7f1c042 | 2020-05-29 16:07:12 -0600 | [diff] [blame] | 603 | struct bpf_devmap_val *val, |
Toke Høiland-Jørgensen | fca16e5 | 2019-07-26 18:06:53 +0200 | [diff] [blame] | 604 | unsigned int idx) |
| 605 | { |
David Ahern | fbee97f | 2020-05-29 16:07:13 -0600 | [diff] [blame^] | 606 | struct bpf_prog *prog = NULL; |
Toke Høiland-Jørgensen | fca16e5 | 2019-07-26 18:06:53 +0200 | [diff] [blame] | 607 | struct bpf_dtab_netdev *dev; |
Toke Høiland-Jørgensen | fca16e5 | 2019-07-26 18:06:53 +0200 | [diff] [blame] | 608 | |
Toke Høiland-Jørgensen | 75ccae6 | 2020-01-16 16:14:44 +0100 | [diff] [blame] | 609 | dev = kmalloc_node(sizeof(*dev), GFP_ATOMIC | __GFP_NOWARN, |
| 610 | dtab->map.numa_node); |
Toke Høiland-Jørgensen | fca16e5 | 2019-07-26 18:06:53 +0200 | [diff] [blame] | 611 | if (!dev) |
| 612 | return ERR_PTR(-ENOMEM); |
| 613 | |
David Ahern | 7f1c042 | 2020-05-29 16:07:12 -0600 | [diff] [blame] | 614 | dev->dev = dev_get_by_index(net, val->ifindex); |
| 615 | if (!dev->dev) |
| 616 | goto err_out; |
Toke Høiland-Jørgensen | fca16e5 | 2019-07-26 18:06:53 +0200 | [diff] [blame] | 617 | |
David Ahern | fbee97f | 2020-05-29 16:07:13 -0600 | [diff] [blame^] | 618 | 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ørgensen | fca16e5 | 2019-07-26 18:06:53 +0200 | [diff] [blame] | 627 | dev->idx = idx; |
| 628 | dev->dtab = dtab; |
David Ahern | fbee97f | 2020-05-29 16:07:13 -0600 | [diff] [blame^] | 629 | 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 Ahern | 7f1c042 | 2020-05-29 16:07:12 -0600 | [diff] [blame] | 636 | dev->val.ifindex = val->ifindex; |
Toke Høiland-Jørgensen | fca16e5 | 2019-07-26 18:06:53 +0200 | [diff] [blame] | 637 | |
| 638 | return dev; |
David Ahern | fbee97f | 2020-05-29 16:07:13 -0600 | [diff] [blame^] | 639 | err_put_prog: |
| 640 | bpf_prog_put(prog); |
| 641 | err_put_dev: |
| 642 | dev_put(dev->dev); |
David Ahern | 7f1c042 | 2020-05-29 16:07:12 -0600 | [diff] [blame] | 643 | err_out: |
| 644 | kfree(dev); |
| 645 | return ERR_PTR(-EINVAL); |
Toke Høiland-Jørgensen | fca16e5 | 2019-07-26 18:06:53 +0200 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | static int __dev_map_update_elem(struct net *net, struct bpf_map *map, |
| 649 | void *key, void *value, u64 map_flags) |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 650 | { |
| 651 | struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map); |
David Ahern | fbee97f | 2020-05-29 16:07:13 -0600 | [diff] [blame^] | 652 | struct bpf_devmap_val val = { .bpf_prog.fd = -1 }; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 653 | struct bpf_dtab_netdev *dev, *old_dev; |
Toke Høiland-Jørgensen | d5df283 | 2019-06-28 11:12:34 +0200 | [diff] [blame] | 654 | u32 i = *(u32 *)key; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 655 | |
| 656 | if (unlikely(map_flags > BPF_EXIST)) |
| 657 | return -EINVAL; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 658 | if (unlikely(i >= dtab->map.max_entries)) |
| 659 | return -E2BIG; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 660 | if (unlikely(map_flags == BPF_NOEXIST)) |
| 661 | return -EEXIST; |
| 662 | |
David Ahern | 7f1c042 | 2020-05-29 16:07:12 -0600 | [diff] [blame] | 663 | /* already verified value_size <= sizeof val */ |
| 664 | memcpy(&val, value, map->value_size); |
| 665 | |
| 666 | if (!val.ifindex) { |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 667 | dev = NULL; |
David Ahern | fbee97f | 2020-05-29 16:07:13 -0600 | [diff] [blame^] | 668 | /* can not specify fd if ifindex is 0 */ |
| 669 | if (val.bpf_prog.fd != -1) |
| 670 | return -EINVAL; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 671 | } else { |
David Ahern | 7f1c042 | 2020-05-29 16:07:12 -0600 | [diff] [blame] | 672 | dev = __dev_map_alloc_node(net, dtab, &val, i); |
Toke Høiland-Jørgensen | fca16e5 | 2019-07-26 18:06:53 +0200 | [diff] [blame] | 673 | if (IS_ERR(dev)) |
| 674 | return PTR_ERR(dev); |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 675 | } |
| 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ørgensen | fca16e5 | 2019-07-26 18:06:53 +0200 | [diff] [blame] | 688 | static 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ørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 695 | static 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 Ahern | fbee97f | 2020-05-29 16:07:13 -0600 | [diff] [blame^] | 699 | struct bpf_devmap_val val = { .bpf_prog.fd = -1 }; |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 700 | struct bpf_dtab_netdev *dev, *old_dev; |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 701 | u32 idx = *(u32 *)key; |
| 702 | unsigned long flags; |
Toke Høiland-Jørgensen | af58e7e | 2019-09-08 09:20:16 +0100 | [diff] [blame] | 703 | int err = -EEXIST; |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 704 | |
David Ahern | 7f1c042 | 2020-05-29 16:07:12 -0600 | [diff] [blame] | 705 | /* 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ørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 709 | return -EINVAL; |
| 710 | |
Toke Høiland-Jørgensen | af58e7e | 2019-09-08 09:20:16 +0100 | [diff] [blame] | 711 | spin_lock_irqsave(&dtab->index_lock, flags); |
| 712 | |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 713 | old_dev = __dev_map_hash_lookup_elem(map, idx); |
| 714 | if (old_dev && (map_flags & BPF_NOEXIST)) |
Toke Høiland-Jørgensen | af58e7e | 2019-09-08 09:20:16 +0100 | [diff] [blame] | 715 | goto out_err; |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 716 | |
David Ahern | 7f1c042 | 2020-05-29 16:07:12 -0600 | [diff] [blame] | 717 | dev = __dev_map_alloc_node(net, dtab, &val, idx); |
Toke Høiland-Jørgensen | af58e7e | 2019-09-08 09:20:16 +0100 | [diff] [blame] | 718 | if (IS_ERR(dev)) { |
| 719 | err = PTR_ERR(dev); |
| 720 | goto out_err; |
| 721 | } |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 722 | |
| 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ørgensen | af58e7e | 2019-09-08 09:20:16 +0100 | [diff] [blame] | 742 | |
| 743 | out_err: |
| 744 | spin_unlock_irqrestore(&dtab->index_lock, flags); |
| 745 | return err; |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | static 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 Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 755 | const 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 Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 762 | .map_check_btf = map_check_no_btf, |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 763 | }; |
John Fastabend | 2ddf71e | 2017-07-17 09:30:02 -0700 | [diff] [blame] | 764 | |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 765 | const 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ørgensen | ce197d8 | 2019-10-19 13:19:31 +0200 | [diff] [blame] | 775 | static 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 Fastabend | 2ddf71e | 2017-07-17 09:30:02 -0700 | [diff] [blame] | 801 | static 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ørgensen | 75ccae6 | 2020-01-16 16:14:44 +0100 | [diff] [blame] | 806 | int i, cpu; |
John Fastabend | 2ddf71e | 2017-07-17 09:30:02 -0700 | [diff] [blame] | 807 | |
| 808 | switch (event) { |
Toke Høiland-Jørgensen | 75ccae6 | 2020-01-16 16:14:44 +0100 | [diff] [blame] | 809 | 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 Fastabend | 2ddf71e | 2017-07-17 09:30:02 -0700 | [diff] [blame] | 823 | case NETDEV_UNREGISTER: |
John Fastabend | 4cc7b95 | 2017-08-04 22:02:19 -0700 | [diff] [blame] | 824 | /* 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ørgensen | ce197d8 | 2019-10-19 13:19:31 +0200 | [diff] [blame] | 831 | if (dtab->map.map_type == BPF_MAP_TYPE_DEVMAP_HASH) { |
| 832 | dev_map_hash_remove_netdev(dtab, netdev); |
| 833 | continue; |
| 834 | } |
| 835 | |
John Fastabend | 2ddf71e | 2017-07-17 09:30:02 -0700 | [diff] [blame] | 836 | for (i = 0; i < dtab->map.max_entries; i++) { |
John Fastabend | 4cc7b95 | 2017-08-04 22:02:19 -0700 | [diff] [blame] | 837 | struct bpf_dtab_netdev *dev, *odev; |
John Fastabend | 2ddf71e | 2017-07-17 09:30:02 -0700 | [diff] [blame] | 838 | |
John Fastabend | 4cc7b95 | 2017-08-04 22:02:19 -0700 | [diff] [blame] | 839 | dev = READ_ONCE(dtab->netdev_map[i]); |
Taehee Yoo | f592f80 | 2018-10-24 20:15:17 +0900 | [diff] [blame] | 840 | if (!dev || netdev != dev->dev) |
John Fastabend | 2ddf71e | 2017-07-17 09:30:02 -0700 | [diff] [blame] | 841 | continue; |
John Fastabend | 4cc7b95 | 2017-08-04 22:02:19 -0700 | [diff] [blame] | 842 | odev = cmpxchg(&dtab->netdev_map[i], dev, NULL); |
| 843 | if (dev == odev) |
John Fastabend | 2ddf71e | 2017-07-17 09:30:02 -0700 | [diff] [blame] | 844 | call_rcu(&dev->rcu, |
| 845 | __dev_map_entry_free); |
| 846 | } |
| 847 | } |
John Fastabend | 4cc7b95 | 2017-08-04 22:02:19 -0700 | [diff] [blame] | 848 | rcu_read_unlock(); |
John Fastabend | 2ddf71e | 2017-07-17 09:30:02 -0700 | [diff] [blame] | 849 | break; |
| 850 | default: |
| 851 | break; |
| 852 | } |
| 853 | return NOTIFY_OK; |
| 854 | } |
| 855 | |
| 856 | static struct notifier_block dev_map_notifier = { |
| 857 | .notifier_call = dev_map_notification, |
| 858 | }; |
| 859 | |
| 860 | static int __init dev_map_init(void) |
| 861 | { |
Björn Töpel | 9636000 | 2019-12-19 07:10:03 +0100 | [diff] [blame] | 862 | int cpu; |
| 863 | |
Jesper Dangaard Brouer | 67f29e0 | 2018-05-24 16:45:46 +0200 | [diff] [blame] | 864 | /* 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 Fastabend | 2ddf71e | 2017-07-17 09:30:02 -0700 | [diff] [blame] | 867 | register_netdevice_notifier(&dev_map_notifier); |
Björn Töpel | 9636000 | 2019-12-19 07:10:03 +0100 | [diff] [blame] | 868 | |
| 869 | for_each_possible_cpu(cpu) |
Toke Høiland-Jørgensen | 1d23388 | 2020-01-16 16:14:45 +0100 | [diff] [blame] | 870 | INIT_LIST_HEAD(&per_cpu(dev_flush_list, cpu)); |
John Fastabend | 2ddf71e | 2017-07-17 09:30:02 -0700 | [diff] [blame] | 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | subsys_initcall(dev_map_init); |