eBPF-based security platform
xdp: tolerate device churn in the interface snapshot
John Fastabendb6b9e5261f22
diff --git a/Documentation/upatches/write/attach-kinds.rst b/Documentation/upatches/write/attach-kinds.rstindex d6a293ac..90cda4ad 100644--- a/Documentation/upatches/write/attach-kinds.rst+++ b/Documentation/upatches/write/attach-kinds.rst@@ -291,7 +291,11 @@ ifindex order. A veth pair gets one attachment: the endpoint in theinitial namespace when its peer is elsewhere, or the lower ifindex whenboth ends are in that namespace. Peer namespace information preventsequal ifindexes in separate namespaces from being mistaken for a pair.-Pairs entirely outside the initial namespace are excluded.+Pairs entirely outside the initial namespace are excluded. Discovery+tolerates interfaces changing under it: a snapshot the kernel reports+as interrupted is retried, and an interface renamed or removed during+the snapshot, or a veth whose peer is not in it, is left out rather than+failing the load.Links use ``XDP_FLAGS_DRV_MODE``. Devices that refuse native XDP with``EOPNOTSUPP``, such as a bridge, are logged and skipped. Every XDPdiff --git a/testing/unit/xdp.c b/testing/unit/xdp.cindex 020a56d7..26078e81 100644--- a/testing/unit/xdp.c+++ b/testing/unit/xdp.c@@ -61,6 +61,41 @@ static void veth_peers_are_namespace_aware(void)EXPECT_TRUE(xdp_device_selected(devs, 4, 1, UPATCH_XDP_VIRTUAL));}+static void veth_without_a_peer_is_not_selected(void)+{+ struct xdp_device devs[2] = {};++ memcpy(devs[0].kind, "veth", sizeof("veth"));+ memcpy(devs[1].kind, "veth", sizeof("veth"));+ devs[0].ifindex = 10;+ devs[0].peer_ifindex = 99;+ devs[1].ifindex = 11;+ devs[1].peer_ifindex = 11;+ EXPECT_FALSE(xdp_device_selected(devs, 2, 0, UPATCH_XDP_ALL));+ EXPECT_FALSE(xdp_device_selected(devs, 2, 1, UPATCH_XDP_ALL));+}++static void unresolved_devices_sit_out(void)+{+ struct xdp_device devs[3] = {};+ size_t i;++ for (i = 0; i < 3; i++) {+ memcpy(devs[i].kind, "veth", sizeof("veth"));+ devs[i].ifindex = (__u32)i + 10;+ }+ devs[0].peer_ifindex = 11;+ devs[1].peer_ifindex = 10;+ devs[2].peer_ifindex = 7;+ devs[2].peer_other_netns = true;+ devs[1].unresolved = true;+ EXPECT_FALSE(xdp_device_selected(devs, 3, 0, UPATCH_XDP_ALL));+ EXPECT_FALSE(xdp_device_selected(devs, 3, 1, UPATCH_XDP_ALL));+ EXPECT_TRUE(xdp_device_selected(devs, 3, 2, UPATCH_XDP_ALL));+ devs[2].unresolved = true;+ EXPECT_FALSE(xdp_device_selected(devs, 3, 2, UPATCH_XDP_ALL));+}+static size_t add_attr(__u8 *buf, size_t len, __u16 type,const void *value, size_t size){@@ -129,6 +164,8 @@ static void kernel_discovery_revalidates_devices(void)static const struct unit_case cases[] = {UNIT_CASE(classes_are_exclusive),UNIT_CASE(veth_peers_are_namespace_aware),+ UNIT_CASE(veth_without_a_peer_is_not_selected),+ UNIT_CASE(unresolved_devices_sit_out),UNIT_CASE(link_messages_are_checked),UNIT_CASE(kernel_discovery_revalidates_devices),{}diff --git a/util/xdp.c b/util/xdp.cindex 48e4da6e..5f4b0b69 100644--- a/util/xdp.c+++ b/util/xdp.c@@ -25,6 +25,7 @@#include "xdp.h"#define NETLINK_BUFFER_SIZE 65536U+#define DEVICES_READ_ATTEMPTS 5Ustruct nl_request {struct nlmsghdr hdr;@@ -312,25 +313,37 @@ __u32 xdp_device_class(const struct xdp_device *dev)return UPATCH_XDP_UNKNOWN;}-bool xdp_device_selected(const struct xdp_device *devs, size_t count,- size_t index, __u32 mask)+static const struct xdp_device *veth_peer(const struct xdp_device *devs,+ size_t count,+ const struct xdp_device *dev){- const struct xdp_device *dev = &devs[index];size_t i;- if (!(xdp_device_class(dev) & mask))- return false;- if (strcmp(dev->kind, "veth") || dev->peer_other_netns)- return true;for (i = 0; i < count; i++) {const struct xdp_device *peer = &devs[i];- if (peer->ifindex == dev->peer_ifindex &&+ if (peer->ifindex != dev->ifindex &&+ peer->ifindex == dev->peer_ifindex &&peer->peer_ifindex == dev->ifindex &&- !peer->peer_other_netns && !strcmp(peer->kind, "veth"))- return dev->ifindex < peer->ifindex;+ !peer->peer_other_netns && !peer->unresolved &&+ !strcmp(peer->kind, "veth"))+ return peer;}- return false;+ return NULL;+}++bool xdp_device_selected(const struct xdp_device *devs, size_t count,+ size_t index, __u32 mask)+{+ const struct xdp_device *dev = &devs[index];+ const struct xdp_device *peer;++ if (dev->unresolved || !(xdp_device_class(dev) & mask))+ return false;+ if (strcmp(dev->kind, "veth") || dev->peer_other_netns)+ return true;+ peer = veth_peer(devs, count, dev);+ return peer && dev->ifindex < peer->ifindex;}struct device_list {@@ -464,57 +477,62 @@ static int device_compare(const void *a, const void *b)(left->ifindex < right->ifindex);}-int xdp_devices_read(struct xdp_device **out, size_t *count)+static int devices_read_once(struct device_list *list){struct nl_request req = {.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),.hdr.nlmsg_type = RTM_GETLINK,.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP,};- struct device_list list = {};size_t i;int ret;+ ret = nl_exchange(NETLINK_ROUTE, &req, receive_device, list);+ if (ret)+ return ret;+ qsort(list->devices, list->count, sizeof(*list->devices),+ device_compare);+ for (i = 0; i < list->count; i++) {+ struct xdp_device *dev = &list->devices[i];++ if (i && list->devices[i - 1].ifindex == dev->ifindex)+ return -EAGAIN;+ /*+ * A rename or removal racing the dump says nothing about+ * the other devices; without its facts this one can't be+ * classified, so it sits out this discovery.+ */+ ret = device_details(dev);+ if (ret == -EXDEV || ret == -ENODEV)+ dev->unresolved = true;+ else if (ret)+ return ret;+ }+ return 0;+}++int xdp_devices_read(struct xdp_device **out, size_t *count)+{+ struct device_list list = {};+ int ret = -EAGAIN;+ __u32 attempt;+*out = NULL;*count = 0;- ret = nl_exchange(NETLINK_ROUTE, &req, receive_device, &list);- if (ret)- goto out;- qsort(list.devices, list.count, sizeof(*list.devices), device_compare);- for (i = 0; i < list.count; i++) {- const struct xdp_device *dev = &list.devices[i];- bool found = false;- size_t j;-- if (i && list.devices[i - 1].ifindex == list.devices[i].ifindex) {- ret = -EXDEV;- goto out;- }- ret = device_details(&list.devices[i]);- if (ret)- goto out;- if (strcmp(dev->kind, "veth") || dev->peer_other_netns)- continue;- for (j = 0; j < list.count; j++) {- const struct xdp_device *peer = &list.devices[j];-- if (peer->ifindex != dev->ifindex &&- peer->ifindex == dev->peer_ifindex &&- peer->peer_ifindex == dev->ifindex &&- !peer->peer_other_netns && !strcmp(peer->kind, "veth"))- found = true;- }- if (!found) {- ret = -EXDEV;- goto out;+ for (attempt = 0; attempt < DEVICES_READ_ATTEMPTS && ret == -EAGAIN;+ attempt++) {+ ret = devices_read_once(&list);+ if (ret) {+ xfree(list.devices,+ size_mul(list.count, sizeof(*list.devices)));+ list = (struct device_list){};}}+ if (ret)+ return ret;*out = list.devices;*count = list.count;return 0;-out:- xfree(list.devices, size_mul(list.count, sizeof(*list.devices)));- return ret;}int xdp_device_verify(const struct xdp_device *expected)diff --git a/util/xdp.h b/util/xdp.hindex 897873d5..6ce94619 100644--- a/util/xdp.h+++ b/util/xdp.h@@ -25,6 +25,7 @@ struct xdp_device {__u16 port_flavour;bool peer_other_netns;bool devlink_port;+ bool unresolved;};int xdp_device_parse(const void *data, size_t len, struct xdp_device *out);