Skip to content

eBPF-based security platform

xdp: classify netdevsim before its devlink port

John Fastabend17e63cd8b128
diff --git a/Documentation/upatches/write/attach-kinds.rst b/Documentation/upatches/write/attach-kinds.rst
index af5fc7ab..0f97f40d 100644
--- a/Documentation/upatches/write/attach-kinds.rst
+++ b/Documentation/upatches/write/attach-kinds.rst
@@ -277,10 +277,11 @@ mask is mandatory, nonzero and contains only the bits below. Bare
Bit 4 is not a class, and a mask with it is refused. Loopback has no
native XDP, so it has no class and no mask selects it. Every other
interface has exactly one class. shirudo checks the driver names ``virtio_net``, ``vmxnet3``, ``hv_netvsc``,
-``xen-netfront`` and ``vif`` for VM adapters. Next it uses devlink port
+``xen-netfront`` and ``vif`` for VM adapters, then ``netdevsim``, which
+is virtual whatever its devlink port says. Next it uses devlink port
flavour: physical ports are network-facing; CPU, DSA, PCI PF, VF, SF
and virtual ports are virtual; other flavours are unknown. Without a
-devlink port, ``netdevsim`` is virtual, a device with a hardware parent
+devlink port, a device with a hardware parent
bus is network-facing, a device with a kernel software link kind is
virtual, and the rest are unknown. Facts come from rtnetlink, devlink
and ethtool, without interface-name or sysfs heuristics.
diff --git a/testing/unit/xdp.c b/testing/unit/xdp.c
index 0ac383e2..91d7b3bb 100644
--- a/testing/unit/xdp.c
+++ b/testing/unit/xdp.c
@@ -35,6 +35,18 @@ static void classes_are_exclusive(void)
EXPECT_FALSE(xdp_device_selected(&dev, 1, 0, UPATCH_XDP_ALL));
}
+static void netdevsim_is_virtual_whatever_its_port_says(void)
+{
+ struct xdp_device dev = {};
+
+ memcpy(dev.driver, "netdevsim", sizeof("netdevsim"));
+ memcpy(dev.parent_bus, "netdevsim", sizeof("netdevsim"));
+ EXPECT_UEQ(xdp_device_class(&dev), UPATCH_XDP_VIRTUAL);
+ dev.devlink_port = true;
+ dev.port_flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
+ EXPECT_UEQ(xdp_device_class(&dev), UPATCH_XDP_VIRTUAL);
+}
+
static void veth_peers_are_namespace_aware(void)
{
struct xdp_device devs[4] = {};
@@ -215,6 +227,7 @@ static void kernel_discovery_revalidates_devices(void)
static const struct unit_case cases[] = {
UNIT_CASE(classes_are_exclusive),
+ UNIT_CASE(netdevsim_is_virtual_whatever_its_port_says),
UNIT_CASE(veth_peers_are_namespace_aware),
UNIT_CASE(veth_without_a_peer_is_not_selected),
UNIT_CASE(unresolved_devices_sit_out),
diff --git a/util/xdp.c b/util/xdp.c
index 83fcbc61..81d56136 100644
--- a/util/xdp.c
+++ b/util/xdp.c
@@ -289,6 +289,9 @@ __u32 xdp_device_class(const struct xdp_device *dev)
for (i = 0; i < ARRAY_SIZE(vm_drivers); i++)
if (!strcmp(dev->driver, vm_drivers[i]))
return UPATCH_XDP_VM;
+ /* Its ports are simulated, whatever flavour they claim. */
+ if (!strcmp(dev->driver, "netdevsim"))
+ return UPATCH_XDP_VIRTUAL;
if (dev->devlink_port) {
switch (dev->port_flavour) {
case DEVLINK_PORT_FLAVOUR_PHYSICAL:
@@ -304,8 +307,6 @@ __u32 xdp_device_class(const struct xdp_device *dev)
return UPATCH_XDP_UNKNOWN;
}
}
- if (!strcmp(dev->driver, "netdevsim"))
- return UPATCH_XDP_VIRTUAL;
if (dev->parent_bus[0])
return UPATCH_XDP_NETWORK;
if (dev->kind[0])