]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/commitdiff
bpf: Devmap adjust uapi for attach bpf program
authorJesper Dangaard Brouer <brouer@redhat.com>
Tue, 9 Jun 2020 13:31:46 +0000 (15:31 +0200)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 9 Jun 2020 18:36:18 +0000 (11:36 -0700)
V2:
- Defer changing BPF-syscall to start at file-descriptor 1
- Use {} to zero initialise struct.

The recent commit fbee97feed9b ("bpf: Add support to attach bpf program to a
devmap entry"), introduced ability to attach (and run) a separate XDP
bpf_prog for each devmap entry. A bpf_prog is added via a file-descriptor.
As zero were a valid FD, not using the feature requires using value minus-1.
The UAPI is extended via tail-extending struct bpf_devmap_val and using
map->value_size to determine the feature set.

This will break older userspace applications not using the bpf_prog feature.
Consider an old userspace app that is compiled against newer kernel
uapi/bpf.h, it will not know that it need to initialise the member
bpf_prog.fd to minus-1. Thus, users will be forced to update source code to
get program running on newer kernels.

This patch remove the minus-1 checks, and have zero mean feature isn't used.

Followup patches either for kernel or libbpf should handle and avoid
returning file-descriptor zero in the first place.

Fixes: fbee97feed9b ("bpf: Add support to attach bpf program to a devmap entry")
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/159170950687.2102545.7235914718298050113.stgit@firesoul
include/uapi/linux/bpf.h
kernel/bpf/devmap.c

index c65b374a509057c9744e6896f75e6b3b3c61f311..19684813faaed615a808c0eb577f3e6031f21771 100644 (file)
@@ -3761,6 +3761,19 @@ struct xdp_md {
        __u32 egress_ifindex;  /* txq->dev->ifindex */
 };
 
+/* DEVMAP map-value layout
+ *
+ * The struct data-layout of map-value is a configuration interface.
+ * New members can only be added to the end of this structure.
+ */
+struct bpf_devmap_val {
+       __u32 ifindex;   /* device index */
+       union {
+               int   fd;  /* prog fd on map write */
+               __u32 id;  /* prog id on map read */
+       } bpf_prog;
+};
+
 enum sk_action {
        SK_DROP = 0,
        SK_PASS,
index bfdff2faf5cb4323a8f74cc45100fcd75cf81eb8..0cbb72cdaf633d3b3fde68cd75e39d0a18a6d989 100644 (file)
@@ -60,15 +60,6 @@ struct xdp_dev_bulk_queue {
        unsigned int count;
 };
 
-/* DEVMAP values */
-struct bpf_devmap_val {
-       u32 ifindex;   /* device index */
-       union {
-               int fd;  /* prog fd on map write */
-               u32 id;  /* prog id on map read */
-       } bpf_prog;
-};
-
 struct bpf_dtab_netdev {
        struct net_device *dev; /* must be first member, due to tracepoint */
        struct hlist_node index_hlist;
@@ -619,7 +610,7 @@ static struct bpf_dtab_netdev *__dev_map_alloc_node(struct net *net,
        if (!dev->dev)
                goto err_out;
 
-       if (val->bpf_prog.fd >= 0) {
+       if (val->bpf_prog.fd > 0) {
                prog = bpf_prog_get_type_dev(val->bpf_prog.fd,
                                             BPF_PROG_TYPE_XDP, false);
                if (IS_ERR(prog))
@@ -653,8 +644,8 @@ static int __dev_map_update_elem(struct net *net, struct bpf_map *map,
                                 void *key, void *value, u64 map_flags)
 {
        struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
-       struct bpf_devmap_val val = { .bpf_prog.fd = -1 };
        struct bpf_dtab_netdev *dev, *old_dev;
+       struct bpf_devmap_val val = {};
        u32 i = *(u32 *)key;
 
        if (unlikely(map_flags > BPF_EXIST))
@@ -670,7 +661,7 @@ static int __dev_map_update_elem(struct net *net, struct bpf_map *map,
        if (!val.ifindex) {
                dev = NULL;
                /* can not specify fd if ifindex is 0 */
-               if (val.bpf_prog.fd != -1)
+               if (val.bpf_prog.fd > 0)
                        return -EINVAL;
        } else {
                dev = __dev_map_alloc_node(net, dtab, &val, i);
@@ -700,8 +691,8 @@ static int __dev_map_hash_update_elem(struct net *net, struct bpf_map *map,
                                     void *key, void *value, u64 map_flags)
 {
        struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
-       struct bpf_devmap_val val = { .bpf_prog.fd = -1 };
        struct bpf_dtab_netdev *dev, *old_dev;
+       struct bpf_devmap_val val = {};
        u32 idx = *(u32 *)key;
        unsigned long flags;
        int err = -EEXIST;