eBPF-based security platform
xdp: give shirudo's own attach verdicts codes no errno can take
John Fastabendf6dc57abb306
diff --git a/builtin-upatch.c b/builtin-upatch.cindex cd371e40..7c186666 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -1348,12 +1348,10 @@ static int pin_xdp_prog(int prog_fd, const char *name, __u32 index)}/*- * 0 with the link pinned at pin, XDP_ATTACH_UNSUPPORTED when the driver- * has no native XDP, or a negative errno. Unpinned, a closed link detaches,- * so a failure here leaves the device as it was.+ * 0 with the link pinned at pin, an enum xdp_attach_err or a negative errno.+ * Unpinned, a closed link detaches, so a failure here leaves the device as+ * it was.*/-#define XDP_ATTACH_UNSUPPORTED 1-static int xdp_attach_one(int prog_fd, const struct prog_spec *spec,const struct xdp_device *dev, const char *pin){@@ -1372,7 +1370,7 @@ static int xdp_attach_one(int prog_fd, const struct prog_spec *spec,return XDP_ERR_NO_MULTIBUF;fd = bpf_link_create(prog_fd, (int)dev->ifindex, BPF_XDP, &opts);if (xdp_attach_unsupported(fd, features))- return XDP_ATTACH_UNSUPPORTED;+ return XDP_ERR_NO_NATIVE;/* Not a kernel without the link type: EOPNOTSUPP would read as one. */if (fd == -EOPNOTSUPP)return XDP_ERR_DRIVER_SETTINGS;@@ -1411,7 +1409,7 @@ static int attach_xdp_devices(int prog_fd, const struct prog_spec *spec,if (strsprintf(pin, sizeof(pin), "%s/link_%u", dir, *pinned) < 0)return -ENAMETOOLONG;ret = xdp_attach_one(prog_fd, spec, dev, pin);- if (ret == XDP_ATTACH_UNSUPPORTED) {+ if (ret == XDP_ERR_NO_NATIVE) {pr_warn("XDP: skipping %s (ifindex %u, class %u): native XDP unsupported",dev->name, dev->ifindex, xdp_device_class(dev));continue;@@ -1421,7 +1419,7 @@ static int attach_xdp_devices(int prog_fd, const struct prog_spec *spec,dev->ifindex, xdp_attach_strerror(ret));continue;}- if (ret < 0) {+ if (ret) {pr_err("XDP: attach %s (ifindex %u): %s", dev->name,dev->ifindex, xdp_attach_strerror(ret));return ret;@@ -2310,19 +2308,21 @@ static int __upatch_apply(const char *path, bool verify_only,ret = attach_and_pin_progs(cx, prog_desc_off, u.hdr.nr_progs,upatch_name, prog_specs);if (ret) {- int e = -ret;+ int e = -xdp_attach_errno(ret);const char *cat;- if (e == ENOENT || e == ENOTDIR)- cat = "attach_target_missing";- else if (e == ESTALE)- cat = "binary_mismatch";- else if (e == EMEDIUMTYPE)+ if (ret == XDP_ERR_NO_MULTIBUF)cat = "xdp_multibuf_unsupported";- else if (e == ENOPROTOOPT)+ else if (ret == XDP_ERR_DRIVER_SETTINGS)cat = "xdp_driver_refused";- else if (e == EXDEV || e == EREMOTE)+ else if (ret == XDP_ERR_LINK_SCOPE || ret == -EXDEV)cat = "attach_scope_mismatch";+ else if (ret > 0)+ cat = "unknown";+ else if (e == ENOENT || e == ENOTDIR)+ cat = "attach_target_missing";+ else if (e == ESTALE)+ cat = "binary_mismatch";else if (e == EOPNOTSUPP)cat = "kernel_abi_unsupported";else if (e == EACCES || e == EPERM)@@ -4409,10 +4409,10 @@ static int reconcile_attach(int prog_fd, const char *name,"the guards refused the XDP link");return 1;} else if (ret == XDP_ERR_LINK_SCOPE) {- rep_fail(rep, "link_create", "attach_scope_mismatch", ret,- xdp_attach_strerror(ret));+ rep_fail(rep, "link_create", "attach_scope_mismatch",+ xdp_attach_errno(ret), xdp_attach_strerror(ret));return 1;- } else if (ret < 0 && ret != XDP_ERR_OCCUPIED &&+ } else if (ret != XDP_ERR_NO_NATIVE && ret != XDP_ERR_OCCUPIED &&!xdp_attach_skippable(ret, dev)) {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.cindex 5ea733b6..919c0e2f 100644--- a/testing/unit/xdp.c+++ b/testing/unit/xdp.c@@ -124,9 +124,9 @@ static void attach_fails_on_a_refusal_of_a_device_still_there(void){static const int skipped[] = { -EXDEV, -ENODEV };static const int refused[] = {- -EINVAL, -ENOSPC, -ERANGE, XDP_ERR_NO_MULTIBUF,- XDP_ERR_DRIVER_SETTINGS, XDP_ERR_LINK_SCOPE, -EEXIST,- XDP_ERR_OCCUPIED,+ -EINVAL, -ENOSPC, -ERANGE, -EEXIST, -EBUSY, -EMEDIUMTYPE,+ -ENOPROTOOPT, -EREMOTE, XDP_ERR_NO_MULTIBUF,+ XDP_ERR_DRIVER_SETTINGS, XDP_ERR_LINK_SCOPE, XDP_ERR_OCCUPIED,};/* No device has this index, so the kernel answers ENODEV. */struct xdp_device *devices = NULL, gone = { .ifindex = 0x7ffffff0 };@@ -143,7 +143,6 @@ static void attach_fails_on_a_refusal_of_a_device_still_there(void)EXPECT_FALSE(xdp_attach_skippable(-EACCES, &gone));EXPECT_FALSE(xdp_attach_skippable(-EPERM, &gone));EXPECT_FALSE(xdp_attach_skippable(0, &gone));- EXPECT_FALSE(xdp_attach_skippable(1, &gone));ret = xdp_devices_read(&devices, &count);if (ret == -EPERM || ret == -EACCES || ret == -EAFNOSUPPORT)@@ -163,6 +162,7 @@ static void attach_stops_on_an_occupied_device(void)EXPECT_FALSE(xdp_attach_skippable(XDP_ERR_OCCUPIED, &dev));EXPECT_FALSE(xdp_attach_skippable(XDP_ERR_OCCUPIED, &gone));EXPECT_FALSE(xdp_attach_skippable(-EEXIST, &dev));+ EXPECT_FALSE(xdp_attach_skippable(-EBUSY, &dev));EXPECT_TRUE(xdp_attach_skippable(-EXDEV, &dev));EXPECT_TRUE(xdp_attach_skippable(-ENODEV, &dev));EXPECT_FALSE(xdp_attach_skippable(0, &dev));@@ -197,6 +197,19 @@ static void only_a_device_without_xdp_lacks_it(void)"the link is not on the device and namespace it was made for");}+static void attach_errors_never_alias_an_errno(void)+{+ EXPECT_EQ(xdp_attach_errno(XDP_ERR_NO_MULTIBUF), -EMEDIUMTYPE);+ EXPECT_EQ(xdp_attach_errno(XDP_ERR_DRIVER_SETTINGS), -ENOPROTOOPT);+ EXPECT_EQ(xdp_attach_errno(XDP_ERR_OCCUPIED), -EBUSY);+ EXPECT_EQ(xdp_attach_errno(XDP_ERR_LINK_SCOPE), -EREMOTE);+ EXPECT_EQ(xdp_attach_errno(-EBUSY), -EBUSY);+ EXPECT_TRUE(XDP_ERR_NO_NATIVE > 0);+ EXPECT_STREQ(xdp_attach_strerror(XDP_ERR_OCCUPIED),+ "the device runs another XDP program");+ EXPECT_STREQ(xdp_attach_strerror(-EBUSY), strerror(EBUSY));+}+static size_t add_attr(__u8 *buf, size_t len, __u16 type,const void *value, size_t size){@@ -305,6 +318,7 @@ static const struct unit_case cases[] = {UNIT_CASE(multibuf_is_required_of_native_xdp_alone),UNIT_CASE(uncovered_devices_say_why),UNIT_CASE(only_a_device_without_xdp_lacks_it),+ UNIT_CASE(attach_errors_never_alias_an_errno),UNIT_CASE(device_features_are_checked),UNIT_CASE(link_messages_are_checked),UNIT_CASE(kernel_discovery_revalidates_devices),diff --git a/util/xdp.c b/util/xdp.cindex eaca3ed8..c4b9fede 100644--- a/util/xdp.c+++ b/util/xdp.c@@ -538,13 +538,38 @@ bool xdp_attach_unsupported(int err, __u64 features)const char *xdp_attach_strerror(int err){- if (err == XDP_ERR_NO_MULTIBUF)+ switch (err) {+ case XDP_ERR_NO_NATIVE:+ return "the driver has no native XDP";+ case XDP_ERR_NO_MULTIBUF:return "the device does not report multi-buffer XDP support";- if (err == XDP_ERR_DRIVER_SETTINGS)+ case XDP_ERR_DRIVER_SETTINGS:return "the driver refuses XDP in its current settings";- if (err == XDP_ERR_LINK_SCOPE)+ case XDP_ERR_OCCUPIED:+ return "the device runs another XDP program";+ case XDP_ERR_LINK_SCOPE:return "the link is not on the device and namespace it was made for";- return strerror(-err);+ default:+ return strerror(-err);+ }+}++int xdp_attach_errno(int err)+{+ switch (err) {+ case XDP_ERR_NO_NATIVE:+ return -EOPNOTSUPP;+ case XDP_ERR_NO_MULTIBUF:+ return -EMEDIUMTYPE;+ case XDP_ERR_DRIVER_SETTINGS:+ return -ENOPROTOOPT;+ case XDP_ERR_OCCUPIED:+ return -EBUSY;+ case XDP_ERR_LINK_SCOPE:+ return -EREMOTE;+ default:+ return err;+ }}const char *xdp_uncovered_reason(__u32 attached, int features_err,@@ -637,7 +662,7 @@ int xdp_devices_read(struct xdp_device **out, size_t *count)*/bool xdp_attach_skippable(int err, const struct xdp_device *dev){- if (err >= 0 || err == -EACCES || err == -EPERM ||+ if (!err || err == -EACCES || err == -EPERM ||err == XDP_ERR_LINK_SCOPE || err == XDP_ERR_OCCUPIED)return false;if (err == -EXDEV || err == -ENODEV)diff --git a/util/xdp.h b/util/xdp.hindex 8f723104..1f3af186 100644--- a/util/xdp.h+++ b/util/xdp.h@@ -12,20 +12,22 @@#define XDP_IFNAME_MAX 16-/* Every XDP program is loaded with frags, so the device must report them. */-#define XDP_ERR_NO_MULTIBUF (-EMEDIUMTYPE)--/* 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.+ * What shirudo makes of a failed XDP attach. Positive, so that no errno a+ * lookup or the kernel returns along the way can be read as one of them.*/-#define XDP_ERR_OCCUPIED (-EBUSY)--/* The kernel made the link for another device or namespace. */-#define XDP_ERR_LINK_SCOPE (-EREMOTE)+enum xdp_attach_err {+ /* The driver has no native XDP: the device is passed over. */+ XDP_ERR_NO_NATIVE = 1,+ /* Every XDP program is loaded with frags, so the device must report them. */+ XDP_ERR_NO_MULTIBUF,+ /* A driver with native XDP that rules it out in its current settings. */+ XDP_ERR_DRIVER_SETTINGS,+ /* The device runs another XDP program: the link create says so. */+ XDP_ERR_OCCUPIED,+ /* The kernel made the link for another device or namespace. */+ XDP_ERR_LINK_SCOPE,+};struct xdp_device {char devlink_device[128];@@ -76,8 +78,13 @@ bool xdp_multibuf_refused(__u64 features);* virtio_net under host GRO_HW or bnxt with header-data split.*/bool xdp_attach_unsupported(int err, __u64 features);++/* err is an enum xdp_attach_err or a negative errno. */const char *xdp_attach_strerror(int err);+/* The negative errno a report carries for err. */+int xdp_attach_errno(int err);+/** Why a device a program selects does not run it: attached as above,* features as read, unless features_err says they could not be.