Skip to content

eBPF-based security platform

upatch: accept XDP with no device yet

John Fastabend43eda4c67231
diff --git a/Documentation/reference/cli/upatch-load.rst b/Documentation/reference/cli/upatch-load.rst
index d2a11539..99bac1e8 100644
--- a/Documentation/reference/cli/upatch-load.rst
+++ b/Documentation/reference/cli/upatch-load.rst
@@ -89,8 +89,10 @@ package.
For XDP, the signed device-class mask selects interfaces present in the
initial network namespace at load time, with one endpoint per veth pair.
Each program can produce several ``link_<n>`` pins. Native XDP is
-required: unsupported devices are logged and skipped, and no attachable
-device is ``attach_target_missing``. Other errors undo all links the load
+required: unsupported devices are logged and skipped. An upatch with no
+attachable device yet still loads, and
+:doc:`/reference/cli/upatch-reconcile` attaches it when one appears. A
+device that is occupied, and any other error, undoes all links the load
created. :doc:`/upatches/write/attach-kinds` defines the classes and limits.
Options
@@ -183,13 +185,11 @@ Exit status and failure categories
- The upatch is loaded and measured.
* - 1
- Refused or failed. The report and the message name the category.
- Includes XDP with no attachable target (``ENODEV``).
* - 2
- A usage error: an unknown option, an option given last without
its value, a second file, no file, or an ``--expect-chain`` that
is not 64 hexadecimal characters. Also ``attach_target_missing``
- for missing probe targets; XDP ``ENODEV`` returns 1 so tree sync
- does not advance past an unattached device policy.
+ for missing probe targets.
``load`` reports these categories: ``version_mismatch``,
``kernel_abi_unsupported``, ``keyring_missing``, ``tampered``,
diff --git a/Documentation/reference/failure-categories.rst b/Documentation/reference/failure-categories.rst
index 32550fea..382477b5 100644
--- a/Documentation/reference/failure-categories.rst
+++ b/Documentation/reference/failure-categories.rst
@@ -137,14 +137,11 @@ Categories
``shirudo upatch info``.
* - ``attach_target_missing``
- ``target_verify``, ``link_create``
- - ``ENOENT``, ``ENOTDIR``, ``ENODEV``
+ - ``ENOENT``, ``ENOTDIR``
- The uprobe binary, the kprobe symbol or its module is absent.
``upatch load`` exits 2. ``bpm tree sync`` records the operation as
``skipped: module not loaded`` and continues.
- XDP with no attachable interface reports ``ENODEV`` and exits 1;
- tree sync fails without advancing that operation.
- - Load the module, install the binary or provide an interface with
- native XDP support in a selected class, then load again.
+ - Load the module or install the binary, then load again.
* - ``binary_mismatch``
- ``target_verify``, ``link_create``
- ``ESTALE``
@@ -186,8 +183,7 @@ Categories
metadata names: a cgroup link not at the root cgroup, a netfilter
link outside the initial network namespace or at another hook or
priority, a kprobe at another function or offset.
- For XDP, a non-initial network namespace, changed device class or
- peer scope, or a returned link with another ifindex.
+ For XDP, a load outside the initial network namespace.
- Run the load in the host's initial cgroup and network namespaces
and with the host's sysfs.
* - ``already_loaded``
diff --git a/Documentation/upatches/write/attach-kinds.rst b/Documentation/upatches/write/attach-kinds.rst
index e978b354..987d1198 100644
--- a/Documentation/upatches/write/attach-kinds.rst
+++ b/Documentation/upatches/write/attach-kinds.rst
@@ -298,11 +298,13 @@ 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 XDP
-program must attach to at least one device, else the load fails with
-``attach_target_missing``. Other errors, including an occupied XDP
-attachment point or changed device scope, undo the load's links. Existing
-XDP programs are never replaced. Device classification and peer identity
+``EOPNOTSUPP``, such as a bridge, are logged and skipped. A device that
+changes or goes away during attachment is skipped too. An XDP program
+with no device to attach to still loads: a pin of its own, ``xdp_prog_<i>``,
+keeps it, and ``status`` shows it as ``xdp (waiting for a device)``. Other
+errors undo the load's links, including an occupied XDP attachment point;
+at boot restore an occupied device is skipped instead. Existing XDP
+programs are never replaced. Device classification and peer identity
are rechecked around attachment, and the returned link's ifindex is
checked before pinning.
@@ -314,8 +316,6 @@ detaches its XDP link. The signed artifact and TPM measurement
cover the class policy, not a fixed device inventory. The location
``status`` and ``upatch get`` report names the first device still
attached and how many more there are, as in ``xdp/eth0 (+1)``.
-Boot restore requires a matching native-XDP interface to be present when
-``init`` restores the archive; otherwise initialization fails.
Programs that attach nowhere
----------------------------
diff --git a/builtin-upatch.c b/builtin-upatch.c
index 1d46a612..d9779b0f 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -1378,9 +1378,26 @@ static int xdp_attach_one(int prog_fd, const struct prog_spec *spec,
return ret;
}
+/*
+ * A device that changed or went away is not this upatch's failure. An
+ * occupied one is at load, where the operator can act on it, but not at
+ * boot, where two upatches that loaded with no device yet can meet on a
+ * device that has appeared since, and one failure would stop the rest of
+ * restore. The kernel reports a vanished ifindex as EINVAL, so ask.
+ */
+static bool xdp_attach_skippable(int err, const struct xdp_device *dev,
+ bool restoring)
+{
+ if (err >= 0)
+ return false;
+ if (err == -EBUSY || err == -EEXIST)
+ return restoring;
+ return err == -EXDEV || err == -ENODEV || xdp_device_verify(dev);
+}
+
static int attach_xdp_devices(int prog_fd, const struct prog_spec *spec,
const struct xdp_device *devices, size_t count,
- const char *dir, __u32 *pinned)
+ const char *dir, __u32 *pinned, bool restoring)
{
__u32 attached = 0;
size_t i;
@@ -1402,6 +1419,11 @@ static int attach_xdp_devices(int prog_fd, const struct prog_spec *spec,
dev->name, dev->ifindex, xdp_device_class(dev));
continue;
}
+ if (xdp_attach_skippable(ret, dev, restoring)) {
+ pr_warn("XDP: skipping %s (ifindex %u): %s", dev->name,
+ dev->ifindex, strerror(-ret));
+ continue;
+ }
if (ret < 0) {
pr_err("XDP: attach %s (ifindex %u): %s", dev->name,
dev->ifindex, strerror(-ret));
@@ -1412,11 +1434,9 @@ static int attach_xdp_devices(int prog_fd, const struct prog_spec *spec,
pr_info("XDP: attached %s (ifindex %u, class %u) at %s",
dev->name, dev->ifindex, xdp_device_class(dev), pin);
}
- if (!attached) {
- pr_err("XDP: no attachable interface matches signed class mask %u",
- spec->device_classes);
- return -ENODEV;
- }
+ if (!attached)
+ pr_info("XDP: no attachable interface matches signed class mask %u yet",
+ spec->device_classes);
return 0;
}
@@ -1428,7 +1448,7 @@ static int attach_xdp_devices(int prog_fd, const struct prog_spec *spec,
static int attach_and_pin_progs(const struct loader_ctx *cx,
size_t prog_desc_off, __u32 nr_progs,
const char *name,
- const struct prog_spec *specs)
+ const struct prog_spec *specs, bool restoring)
{
char dir[PATH_MAX], serializers_dir[PATH_MAX + 16];
bool made_dir = false, made_serializers = false;
@@ -1554,7 +1574,7 @@ static int attach_and_pin_progs(const struct loader_ctx *cx,
goto out;
made[i] = true;
ret = attach_xdp_devices(inner_fd, spec, devices, nr_devices,
- dir, &pinned_links);
+ dir, &pinned_links, restoring);
if (ret)
goto out;
continue;
@@ -2292,12 +2312,12 @@ static int __upatch_apply(const char *path, bool verify_only,
* exits.
*/
ret = attach_and_pin_progs(cx, prog_desc_off, u.hdr.nr_progs,
- upatch_name, prog_specs);
+ upatch_name, prog_specs, !rep);
if (ret) {
int e = -ret;
const char *cat;
- if (e == ENOENT || e == ENOTDIR || e == ENODEV)
+ if (e == ENOENT || e == ENOTDIR)
cat = "attach_target_missing";
else if (e == ESTALE)
cat = "binary_mismatch";
@@ -3080,7 +3100,7 @@ static int upatch_load(const struct load_args *args)
done:
eventlog_unlock(lock);
if (ret == 1 && rep.category &&
- !strcmp(rep.category, "attach_target_missing") && rep.err != ENODEV)
+ !strcmp(rep.category, "attach_target_missing"))
ret = UPATCH_INSTALL_TARGET_MISSING;
if (staged[0])
unlink(staged);
diff --git a/testing/functional/upatch/t3111-xdp.sh b/testing/functional/upatch/t3111-xdp.sh
index 7803df41..d360a0e5 100644
--- a/testing/functional/upatch/t3111-xdp.sh
+++ b/testing/functional/upatch/t3111-xdp.sh
@@ -52,13 +52,15 @@ test_xdp_success() {
cleanup
}
-test_xdp_success 'a device without native XDP alone is no attachable target' '
+test_xdp_success 'a device without native XDP alone loads and waits for a device' '
ip link add xdpbr type bridge && ip link set xdpbr up &&
- status=0 &&
- report=$("$SHIRUDO_BIN" upatch load "$FIXTURES/$NAME.upatch" \
- --no-precheck --report=- 2>/dev/null) || status=$?
- [ "$status" = 1 ] &&
- [ "$(printf "%s\n" "$report" | report_field category)" = attach_target_missing ] &&
+ load "$NAME" &&
+ [ -e "$B/$NAME/xdp_prog_0" ] && [ ! -e "$B/$NAME/link_0" ] &&
+ "$SHIRUDO_BIN" status | grep -q "waiting for a device" &&
+ before=$(wc -l </var/lib/shirudo/eventlog) &&
+ "$SHIRUDO_BIN" init &&
+ [ "$(wc -l </var/lib/shirudo/eventlog)" = "$before" ] &&
+ unload "$NAME" &&
[ ! -e "$B/$NAME" ]
'
diff --git a/testing/functional/upatch/t3112-xdp-reconcile.sh b/testing/functional/upatch/t3112-xdp-reconcile.sh
index 8a4d1f50..297b8dd0 100644
--- a/testing/functional/upatch/t3112-xdp-reconcile.sh
+++ b/testing/functional/upatch/t3112-xdp-reconcile.sh
@@ -54,6 +54,17 @@ test_xdp_success 'a pass with no XDP upatch does nothing' '
reconcile
'
+test_xdp_success 'an upatch with no device yet attaches when one appears' '
+ load "$NAME" &&
+ [ -e "$B/$NAME/xdp_prog_0" ] && [ ! -e "$B/$NAME/link_0" ] &&
+ "$SHIRUDO_BIN" upatch list | grep -q "$NAME" &&
+ "$SHIRUDO_BIN" upatch get "$NAME" >/dev/null &&
+ add_pair xdpa 30000 xdpb 30001 &&
+ reconcile &&
+ [ -e "$B/$NAME/link_0" ] &&
+ [ "$("$H" query xdpa)" -gt 0 ]
+'
+
test_xdp_success 'a device added after load is attached, once' '
add_pair xdpa 30000 xdpb 30001 &&
load "$NAME" &&
diff --git a/util/upatch.h b/util/upatch.h
index 49347f7c..9cad556c 100644
--- a/util/upatch.h
+++ b/util/upatch.h
@@ -178,10 +178,7 @@ int upatch_apply(const char *path);
int upatch_install(const char *path);
int upatch_remove(const char *path);
-/*
- * A missing optional kprobe module permits tree projection advancement;
- * an XDP selection without an attachable device must fail instead.
- */
+/* A missing optional kprobe module permits tree projection advancement. */
#define UPATCH_INSTALL_TARGET_MISSING 2
struct cert;