Skip to content

Commit 281920b

Browse files
netoptimizerAlexei Starovoitov
authored andcommitted
bpf: Devmap adjust uapi for attach bpf program
V2: - Defer changing BPF-syscall to start at file-descriptor 1 - Use {} to zero initialise struct. The recent commit fbee97f ("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: fbee97f ("bpf: Add support to attach bpf program to a devmap entry") Signed-off-by: Jesper Dangaard Brouer <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/159170950687.2102545.7235914718298050113.stgit@firesoul
1 parent 248e00a commit 281920b

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

include/uapi/linux/bpf.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3761,6 +3761,19 @@ struct xdp_md {
37613761
__u32 egress_ifindex; /* txq->dev->ifindex */
37623762
};
37633763

3764+
/* DEVMAP map-value layout
3765+
*
3766+
* The struct data-layout of map-value is a configuration interface.
3767+
* New members can only be added to the end of this structure.
3768+
*/
3769+
struct bpf_devmap_val {
3770+
__u32 ifindex; /* device index */
3771+
union {
3772+
int fd; /* prog fd on map write */
3773+
__u32 id; /* prog id on map read */
3774+
} bpf_prog;
3775+
};
3776+
37643777
enum sk_action {
37653778
SK_DROP = 0,
37663779
SK_PASS,

kernel/bpf/devmap.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,6 @@ struct xdp_dev_bulk_queue {
6060
unsigned int count;
6161
};
6262

63-
/* DEVMAP values */
64-
struct bpf_devmap_val {
65-
u32 ifindex; /* device index */
66-
union {
67-
int fd; /* prog fd on map write */
68-
u32 id; /* prog id on map read */
69-
} bpf_prog;
70-
};
71-
7263
struct bpf_dtab_netdev {
7364
struct net_device *dev; /* must be first member, due to tracepoint */
7465
struct hlist_node index_hlist;
@@ -619,7 +610,7 @@ static struct bpf_dtab_netdev *__dev_map_alloc_node(struct net *net,
619610
if (!dev->dev)
620611
goto err_out;
621612

622-
if (val->bpf_prog.fd >= 0) {
613+
if (val->bpf_prog.fd > 0) {
623614
prog = bpf_prog_get_type_dev(val->bpf_prog.fd,
624615
BPF_PROG_TYPE_XDP, false);
625616
if (IS_ERR(prog))
@@ -653,8 +644,8 @@ static int __dev_map_update_elem(struct net *net, struct bpf_map *map,
653644
void *key, void *value, u64 map_flags)
654645
{
655646
struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
656-
struct bpf_devmap_val val = { .bpf_prog.fd = -1 };
657647
struct bpf_dtab_netdev *dev, *old_dev;
648+
struct bpf_devmap_val val = {};
658649
u32 i = *(u32 *)key;
659650

660651
if (unlikely(map_flags > BPF_EXIST))
@@ -670,7 +661,7 @@ static int __dev_map_update_elem(struct net *net, struct bpf_map *map,
670661
if (!val.ifindex) {
671662
dev = NULL;
672663
/* can not specify fd if ifindex is 0 */
673-
if (val.bpf_prog.fd != -1)
664+
if (val.bpf_prog.fd > 0)
674665
return -EINVAL;
675666
} else {
676667
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,
700691
void *key, void *value, u64 map_flags)
701692
{
702693
struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
703-
struct bpf_devmap_val val = { .bpf_prog.fd = -1 };
704694
struct bpf_dtab_netdev *dev, *old_dev;
695+
struct bpf_devmap_val val = {};
705696
u32 idx = *(u32 *)key;
706697
unsigned long flags;
707698
int err = -EEXIST;

0 commit comments

Comments
 (0)