Skip to content

eBPF-based security platform

xdp: treat only the kernel's busy answer as an occupied device

John Fastabendd41d00f06978
diff --git a/builtin-upatch.c b/builtin-upatch.c
index e673e9b3..e6f0a0d2 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -1376,6 +1376,8 @@ static int xdp_attach_one(int prog_fd, const struct prog_spec *spec,
/* Not a kernel without the link type: EOPNOTSUPP would read as one. */
if (fd == -EOPNOTSUPP)
return XDP_ERR_DRIVER_SETTINGS;
+ if (fd == -EBUSY || fd == -EEXIST)
+ return XDP_ERR_OCCUPIED;
if (fd < 0)
return fd;
ret = link_matches_spec(fd, spec, dev->ifindex);
@@ -4410,7 +4412,7 @@ static int reconcile_attach(int prog_fd, const char *name,
rep_fail(rep, "link_create", "attach_scope_mismatch", ret,
xdp_attach_strerror(ret));
return 1;
- } else if (ret < 0 && ret != -EBUSY && ret != -EEXIST &&
+ } else if (ret < 0 && ret != XDP_ERR_OCCUPIED &&
!xdp_attach_skippable(ret, dev, false)) {
pr_warn("XDP: upatch %s: attach %s (ifindex %u): %s", name,
dev->name, dev->ifindex, xdp_attach_strerror(ret));
diff --git a/testing/unit/xdp.c b/testing/unit/xdp.c
index 37c4b049..97e51927 100644
--- a/testing/unit/xdp.c
+++ b/testing/unit/xdp.c
@@ -122,10 +122,10 @@ static void reconcile_never_touches_another_program(void)
static void restore_fails_on_a_driver_refusal(void)
{
- static const int skipped[] = { -EBUSY, -EEXIST, -EXDEV, -ENODEV };
+ static const int skipped[] = { XDP_ERR_OCCUPIED, -EXDEV, -ENODEV };
static const int refused[] = {
-EINVAL, -ENOSPC, -ERANGE, XDP_ERR_NO_MULTIBUF,
- XDP_ERR_DRIVER_SETTINGS, XDP_ERR_LINK_SCOPE,
+ XDP_ERR_DRIVER_SETTINGS, XDP_ERR_LINK_SCOPE, -EEXIST,
};
/* No device has this index, so the kernel answers ENODEV. */
struct xdp_device *devices = NULL, gone = { .ifindex = 0x7ffffff0 };
@@ -160,7 +160,7 @@ static void load_stops_on_an_occupied_device(void)
{
struct xdp_device dev = {};
- EXPECT_FALSE(xdp_attach_skippable(-EBUSY, &dev, false));
+ EXPECT_FALSE(xdp_attach_skippable(XDP_ERR_OCCUPIED, &dev, false));
EXPECT_FALSE(xdp_attach_skippable(-EEXIST, &dev, false));
EXPECT_TRUE(xdp_attach_skippable(-EXDEV, &dev, false));
EXPECT_TRUE(xdp_attach_skippable(-ENODEV, &dev, false));
diff --git a/util/xdp.c b/util/xdp.c
index a7e45bb1..4e2497dd 100644
--- a/util/xdp.c
+++ b/util/xdp.c
@@ -641,7 +641,7 @@ bool xdp_attach_skippable(int err, const struct xdp_device *dev,
if (err >= 0 || err == -EACCES || err == -EPERM ||
err == XDP_ERR_LINK_SCOPE)
return false;
- if (err == -EBUSY || err == -EEXIST)
+ if (err == XDP_ERR_OCCUPIED)
return restoring;
if (err == -EXDEV || err == -ENODEV)
return true;
diff --git a/util/xdp.h b/util/xdp.h
index 9f175941..ac2f30fd 100644
--- a/util/xdp.h
+++ b/util/xdp.h
@@ -18,6 +18,12 @@
/* A driver with native XDP that rules it out in its current settings. */
#define XDP_ERR_DRIVER_SETTINGS (-ENOPROTOOPT)
+/*
+ * The device runs another XDP program: the kernel's EBUSY or EEXIST from
+ * the link create alone, not a pin that happens to exist.
+ */
+#define XDP_ERR_OCCUPIED (-EBUSY)
+
/* The kernel made the link for another device or namespace. */
#define XDP_ERR_LINK_SCOPE (-EREMOTE)