eBPF-based security platform
xdp: read what each device runs, in every mode, from the link dump
John Fastabend99ed3d95465a
diff --git a/Documentation/internals/kernel-interfaces.rst b/Documentation/internals/kernel-interfaces.rstindex 27e77cfe..38e433f1 100644--- a/Documentation/internals/kernel-interfaces.rst+++ b/Documentation/internals/kernel-interfaces.rst@@ -70,7 +70,10 @@ XDP uses ``BPF_LINK_CREATE`` with ``attach_type=BPF_XDP``,shirudo verifies ``BPF_LINK_TYPE_XDP`` and the returned ``xdp.ifindex``before pinning. Discovery uses kernel ``RTM_GETLINK`` replies,``ETHTOOL_GDRVINFO`` and devlink ``DEVLINK_CMD_PORT_GET``; it does not-read sysfs to classify devices. Before attaching, shirudo reads+read sysfs to classify devices. The program a device runs, in any mode,+comes from the ``IFLA_XDP`` attributes of the same dump:+``IFLA_XDP_DRV_PROG_ID``, and ``IFLA_XDP_SKB_PROG_ID`` or+``IFLA_XDP_HW_PROG_ID`` for a program shirudo did not attach. Before attaching, shirudo reads``NETDEV_A_DEV_XDP_FEATURES`` with ``NETDEV_CMD_DEV_GET`` on the ``netdev``generic netlink family and refuses a device that reports``NETDEV_XDP_ACT_BASIC`` without ``NETDEV_XDP_ACT_RX_SG``, as the kerneldiff --git a/Documentation/operate/run/read-status.rst b/Documentation/operate/run/read-status.rstindex 9434d7e7..080668c2 100644--- a/Documentation/operate/run/read-status.rst+++ b/Documentation/operate/run/read-status.rst@@ -267,8 +267,9 @@ verdict is derived from them.moving namespace, for up to a minute. One that stays listed is unfiltered. Its driverrefused the program, or a setting such as LRO turned its XDP off,or the reconcile units are stopped. ``WARN`` ``XDP coverage not- checked: <error>`` means the check could not run, or could not ask- a device what it runs; such a device may be unfiltered. ``XDP+ checked: <error>`` means the check could not run; a device may+ then be unfiltered. ``another XDP program attached`` counts a+ generic or offloaded program too, which shirudo never attaches. ``XDPcoverage not checked: not in the initial network namespace`` is a``status`` run in a container or another namespace, whose devicesare not the host's: run it in the host's namespace.diff --git a/Documentation/upatches/write/attach-kinds.rst b/Documentation/upatches/write/attach-kinds.rstindex 7e53f0f8..77141583 100644--- a/Documentation/upatches/write/attach-kinds.rst+++ b/Documentation/upatches/write/attach-kinds.rst@@ -322,7 +322,13 @@ made at all, out of memory or descriptors, timed out, refused withdevice out. A kernel that does not answer a netlink request within5 seconds fails the load with ``ETIMEDOUT``, and is not asked again.-Links use ``XDP_FLAGS_DRV_MODE``. A device without native XDP, which+Links use ``XDP_FLAGS_DRV_MODE``: shirudo runs XDP in the driver only.+Generic XDP, which runs in the stack after the socket buffer is built,+is not supported, as it filters too late to be of use, so a driver+without native XDP is not covered. A device that runs a generic or+offloaded program, which shirudo never attaches, holds its XDP slot: it+is occupied like one running another driver-mode program, and ``status``+says ``another XDP program attached``. A device without native XDP, whichrefuses with ``EOPNOTSUPP`` and reports no XDP features, such as abridge, is logged and skipped. Any other refusal is the driver ruling XDPout in its current settings, and fails with the load categorydiff --git a/builtin-upatch.c b/builtin-upatch.cindex 7dd57b65..bd788a6f 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -4455,15 +4455,10 @@ static int reconcile_walk(const struct xdp_work *w,struct reconcile_pass pass = {.name = w->name, .next = next, .needed = needed, .rep = rep,};- int ret, query_err;+ int ret;ret = upatch_xdp_each_uncovered(w->name, w->specs, w->u.hdr.nr_progs,- devices, count, reconcile_device, &pass,- &query_err);- /* The device is tried again next pass; status reports the gap meanwhile. */- if (query_err && next)- pr_warn("XDP: upatch %s: cannot ask a device what it runs: %s",- w->name, strerror(-query_err));+ devices, count, reconcile_device, &pass);if (ret >= 0)return ret;if (!next) {diff --git a/testing/unit/xdp.c b/testing/unit/xdp.cindex 26843b7d..5b548c4d 100644--- a/testing/unit/xdp.c+++ b/testing/unit/xdp.c@@ -280,6 +280,7 @@ static void link_messages_are_checked(void)EXPECT_UEQ(dev.ifindex, 12);EXPECT_UEQ(dev.peer_ifindex, 13);EXPECT_TRUE(dev.peer_other_netns);+ EXPECT_UEQ(xdp_device_attached(&dev), 0);for (n = 0; n < sizeof(info) + NLA_HDRLEN + sizeof("veth0"); n++)EXPECT_EQ(xdp_device_parse(buf, n, &dev), -EINVAL);EXPECT_EQ(xdp_device_parse(buf, len - 1, &dev), -EINVAL);@@ -291,6 +292,39 @@ static void link_messages_are_checked(void)EXPECT_EQ(xdp_device_parse(buf, len, &dev), -EINVAL);}+static void every_xdp_mode_is_read_from_the_dump(void)+{+ struct ifinfomsg info = { .ifi_index = 3 };+ __u32 drv = 21, skb = 22, hw = 23;+ __u8 buf[256] = {}, nested[64];+ struct xdp_device dev;+ size_t len, n, base;++ memcpy(buf, &info, sizeof(info));+ base = add_attr(buf, sizeof(info), IFLA_IFNAME, "eth0", sizeof("eth0"));+ memset(nested, 0, sizeof(nested));+ n = add_attr(nested, 0, IFLA_XDP_SKB_PROG_ID, &skb, sizeof(skb));+ len = add_attr(buf, base, IFLA_XDP | NLA_F_NESTED, nested, n);+ ASSERT_EQ(xdp_device_parse(buf, len, &dev), 0);+ EXPECT_UEQ(dev.xdp_drv_id, 0);+ EXPECT_UEQ(xdp_device_attached(&dev), skb);+ EXPECT_EQ(xdp_reconcile_action(true, xdp_device_attached(&dev), 7),+ XDP_RECONCILE_OCCUPIED);++ memset(nested, 0, sizeof(nested));+ n = add_attr(nested, 0, IFLA_XDP_HW_PROG_ID, &hw, sizeof(hw));+ n = add_attr(nested, n, IFLA_XDP_DRV_PROG_ID, &drv, sizeof(drv));+ len = add_attr(buf, base, IFLA_XDP | NLA_F_NESTED, nested, n);+ ASSERT_EQ(xdp_device_parse(buf, len, &dev), 0);+ EXPECT_UEQ(xdp_device_attached(&dev), drv);+ EXPECT_UEQ(dev.xdp_other_id, hw);++ memset(nested, 0, sizeof(nested));+ n = add_attr(nested, 0, IFLA_XDP_DRV_PROG_ID, &drv, sizeof(__u16));+ len = add_attr(buf, base, IFLA_XDP | NLA_F_NESTED, nested, n);+ EXPECT_EQ(xdp_device_parse(buf, len, &dev), -EINVAL);+}+static void device_features_are_checked(void){__u64 value = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_RX_SG, out = 0;@@ -356,6 +390,7 @@ static const struct unit_case cases[] = {UNIT_CASE(only_a_device_without_xdp_lacks_it),UNIT_CASE(a_driver_refusal_is_told_from_other_errors),UNIT_CASE(attach_errors_never_alias_an_errno),+ UNIT_CASE(every_xdp_mode_is_read_from_the_dump),UNIT_CASE(device_features_are_checked),UNIT_CASE(link_messages_are_checked),UNIT_CASE(the_network_namespace_is_known),diff --git a/util/upatch_verify.c b/util/upatch_verify.cindex d881a43b..f8c306dc 100644--- a/util/upatch_verify.c+++ b/util/upatch_verify.c@@ -8,7 +8,6 @@#include <errno.h>#include <limits.h>#include <string.h>-#include <sys/socket.h>#include <sys/stat.h>#include <sys/xattr.h>#include <unistd.h>@@ -16,7 +15,6 @@#include <bpf/bpf.h>#include <bpf/libbpf.h>#include <linux/bpf.h>-#include <linux/if_link.h>#include <shirudo/misc.h>#include <shirudo/overflow.h>@@ -215,11 +213,10 @@ int upatch_xdp_each_uncovered(const char *name, const struct prog_spec *specs,int (*cb)(int prog_fd, const struct prog_spec *spec,const struct xdp_device *dev,__u32 attached, void *ctx),- void *ctx, int *query_err)+ void *ctx){__u32 i;- *query_err = 0;for (i = 0; i < nr_progs; i++) {__u32 prog_id = 0;size_t j;@@ -231,19 +228,12 @@ int upatch_xdp_each_uncovered(const char *name, const struct prog_spec *specs,if (fd < 0)return fd;for (j = 0; j < count; j++) {- __u32 attached = 0;+ __u32 attached = xdp_device_attached(&devices[j]);int ret;if (!xdp_device_selected(devices, count, j,specs[i].device_classes))continue;- ret = bpf_xdp_query_id((int)devices[j].ifindex,- XDP_FLAGS_DRV_MODE, &attached);- if (ret) {- if (!*query_err)- *query_err = ret;- continue;- }if (xdp_reconcile_action(true, attached, prog_id) ==XDP_RECONCILE_COVERED)continue;@@ -392,7 +382,7 @@ static int note_uncovered(int prog_fd, const struct prog_spec *spec,if (!attached) {err = xdp_device_verify(dev);- /* bpf_xdp_query() finds nothing, not an error, for a gone device. */+ /* The read finds nothing, not an error, for a device gone since. */if (err == -ENODEV || err == -EXDEV)return 0;err = xdp_device_features(dev->ifindex, &features);@@ -433,13 +423,9 @@ static int note_unclassified(struct uncovered_walk *w, const char *name,for (i = 0; i < w->count; i++) {const struct xdp_device *dev = &w->devices[i];- __u32 attached = 0;- if (!dev->unclassified_err)- continue;- if (!bpf_xdp_query_id((int)dev->ifindex, XDP_FLAGS_DRV_MODE,- &attached) &&- runs_program_of(name, specs, nr_progs, attached))+ if (!dev->unclassified_err ||+ runs_program_of(name, specs, nr_progs, dev->xdp_drv_id))continue;if (strsprintf(w->why, sizeof(w->why), "not classified: %s",strerror(-dev->unclassified_err)) < 0)@@ -461,7 +447,7 @@ static int xdp_uncovered(const struct measured_set *s, const char *name,struct prog_spec *specs = NULL;struct upatch_pins pins;struct upatch u;- int ret, query_err = 0;+ int ret;if (upatch_pins_read(name, &pins) || !pins.nr_xdp_progs ||!upatch_pin_measured(s, name, payload))@@ -477,15 +463,14 @@ static int xdp_uncovered(const struct measured_set *s, const char *name,w->name = name;ret = upatch_xdp_each_uncovered(name, specs, u.hdr.nr_progs,w->devices, w->count,- note_uncovered, w, &query_err);+ note_uncovered, w);if (!ret)ret = note_unclassified(w, name, specs,u.hdr.nr_progs);}prog_specs_free(specs, u.hdr.nr_progs);upatch_close(&u);- /* A device that could not be asked may run nothing: say so. */- return ret < 0 ? ret : query_err;+ return ret;}int upatch_xdp_uncovered(const struct measured_set *s,diff --git a/util/upatch_verify.h b/util/upatch_verify.hindex 95a4dd9d..0ed948d5 100644--- a/util/upatch_verify.h+++ b/util/upatch_verify.h@@ -49,10 +49,9 @@ int upatch_xdp_prog_open(const char *name, __u32 index, __u32 *id);/** Calls cb for each device an XDP program of name selects and that does not- * run it, with what the device runs instead, 0 for nothing. A non-zero- * return of cb ends the walk with that value, and a program pin that cannot- * be opened ends it with its -errno. A device that cannot be asked what it- * runs is passed over and the first such -errno left in *query_err.+ * run it, with what the device runs instead as devices were read, 0 for+ * nothing. A non-zero return of cb ends the walk with that value, and a+ * program pin that cannot be opened ends it with its -errno.*/int upatch_xdp_each_uncovered(const char *name, const struct prog_spec *specs,__u32 nr_progs, const struct xdp_device *devices,@@ -60,7 +59,7 @@ int upatch_xdp_each_uncovered(const char *name, const struct prog_spec *specs,int (*cb)(int prog_fd, const struct prog_spec *spec,const struct xdp_device *dev,__u32 attached, void *ctx),- void *ctx, int *query_err);+ void *ctx);struct measured_set;diff --git a/util/xdp.c b/util/xdp.cindex 47c1f3eb..9f987adf 100644--- a/util/xdp.c+++ b/util/xdp.c@@ -216,9 +216,28 @@ out:return ret;}+static int xdp_ids_parse(const struct nlattr *xdp, struct xdp_device *out)+{+ const __u8 *attrs = (const __u8 *)xdp + NLA_HDRLEN;+ size_t len = xdp->nla_len - NLA_HDRLEN;+ __u32 skb = 0, hw = 0;+ int ret;++ ret = attr_number(attrs, len, IFLA_XDP_DRV_PROG_ID, &out->xdp_drv_id,+ sizeof(out->xdp_drv_id), NULL);+ if (!ret)+ ret = attr_number(attrs, len, IFLA_XDP_SKB_PROG_ID, &skb,+ sizeof(skb), NULL);+ if (!ret)+ ret = attr_number(attrs, len, IFLA_XDP_HW_PROG_ID, &hw,+ sizeof(hw), NULL);+ out->xdp_other_id = skb ? skb : hw;+ return ret;+}+int xdp_device_parse(const void *data, size_t len, struct xdp_device *out){- const struct nlattr *linkinfo, *port;+ const struct nlattr *linkinfo, *port, *xdp;bool port_present = false;struct ifinfomsg info;__s32 peer_netns = 0;@@ -261,6 +280,11 @@ int xdp_device_parse(const void *data, size_t len, struct xdp_device *out)if (ret)return ret;}+ ret = attr_find(attrs, len, IFLA_XDP, &xdp);+ if (!ret && xdp)+ ret = xdp_ids_parse(xdp, out);+ if (ret)+ return ret;ret = attr_find(attrs, len, IFLA_DEVLINK_PORT, &port);if (ret || !port || port->nla_len == NLA_HDRLEN)return ret;@@ -320,6 +344,11 @@ __u32 xdp_device_class(const struct xdp_device *dev)return UPATCH_XDP_UNKNOWN;}+__u32 xdp_device_attached(const struct xdp_device *dev)+{+ return dev->xdp_drv_id ? dev->xdp_drv_id : dev->xdp_other_id;+}+static const struct xdp_device *veth_peer(const struct xdp_device *devs,size_t count,const struct xdp_device *dev)diff --git a/util/xdp.h b/util/xdp.hindex 61e81e6e..201978c0 100644--- a/util/xdp.h+++ b/util/xdp.h@@ -38,6 +38,9 @@ struct xdp_device {char name[XDP_IFNAME_MAX];__u32 ifindex;__u32 peer_ifindex;+ /* The driver-mode XDP program it runs, and a generic or offloaded one. */+ __u32 xdp_drv_id;+ __u32 xdp_other_id;/* Why the driver or devlink facts could not be read, or 0. */int unclassified_err;__u32 flags;@@ -64,6 +67,12 @@ bool xdp_attach_skippable(int err, const struct xdp_device *dev);int xdp_device_parse(const void *data, size_t len, struct xdp_device *out);__u32 xdp_device_class(const struct xdp_device *dev);++/*+ * The program the device runs, 0 for none. shirudo attaches in driver+ * mode only, so a generic or offloaded program is another's.+ */+__u32 xdp_device_attached(const struct xdp_device *dev);bool xdp_device_selected(const struct xdp_device *devs, size_t count,size_t index, __u32 mask);/*