Skip to content

eBPF-based security platform

build: load every XDP program with frags

John Fastabenda084d2fe551c
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 2487c384..b56a0e64 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -201,7 +201,9 @@ signed config image.
endpoint index orderings, cross-namespace pairs, a bridge without
native XDP skipped, rollback, signed unload and descriptor
and alternate-attachment lockdown; a repeated init adopts the
- archived XDP without another event. Each case cleans up its state
+ archived XDP without another event. A veth with a peer MTU of 9000
+ and the ``virtio_net`` guest adapter at MTU 9000 attach, which only a
+ multi-buffer program can. Each case cleans up its state
so a failed assertion does not contaminate later cases.
* - ``t3112-xdp-reconcile``
- ``upatch reconcile`` attaches a device added after load, adds no
@@ -210,8 +212,8 @@ signed config image.
pin without reusing a live index, does nothing after unload, and leaves the link on a veth
end moved to another namespace. A changed archive is covered by
the ``upatch_verify`` unit cases. An unload racing a pass is not
- reported as tampering, and a veth whose peer MTU is too large for
- XDP is warned about without stopping the pass.
+ reported as tampering, and a veth with too few rx queues, which
+ its driver refuses, is warned about without stopping the pass.
``tpm`` (4)
~~~~~~~~~~~
diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rst
index 00e8f767..6f5e56a7 100644
--- a/Documentation/security/threat-model.rst
+++ b/Documentation/security/threat-model.rst
@@ -1570,6 +1570,15 @@ R21 upatches can be neutralized by changing their targets
reconcile and unload; driver behaviour across a namespace move is
tested only for veth.
+ Every XDP program is loaded with multi-buffer support and a selected
+ device must report it. At boot restore one that does not fails
+ ``init``. After boot it stays uncovered, with a warning.
+ The check is made at attach only: a device that later
+ stops reporting it keeps its link, as a bond that later enslaves such a
+ device does, and what happens to a frame too large for one buffer is
+ then the driver's choice. It may drop the frame or never run the
+ program on it.
+
No record says which per-device XDP link pins should exist, because
they come and go with devices. A pin that disappears while its device
remains looks like a device not yet attached: ``init`` and ``status``
diff --git a/Documentation/upatches/learn/concepts.rst b/Documentation/upatches/learn/concepts.rst
index 891385fa..614ab9b3 100644
--- a/Documentation/upatches/learn/concepts.rst
+++ b/Documentation/upatches/learn/concepts.rst
@@ -262,7 +262,7 @@ with ``attach_scope_mismatch``. For fentry, fexit, ``fmod_ret``, LSM and
``tp_btf`` programs the site is the BTF target the verifier bound at load.
It cannot use a program type that has no pinnable link. The builder
-refuses socket filters, ``sk_skb``, ``xdp`` and every other section
+refuses socket filters, ``sk_skb`` and every other section
outside the list in :doc:`/upatches/write/attach-kinds`, because only a pinned
link keeps a program attached after the command exits and lets the unload path
detach it.
diff --git a/Documentation/upatches/write/attach-kinds.rst b/Documentation/upatches/write/attach-kinds.rst
index 0f97f40d..279218ba 100644
--- a/Documentation/upatches/write/attach-kinds.rst
+++ b/Documentation/upatches/write/attach-kinds.rst
@@ -69,7 +69,7 @@ the link against the metadata and pins it.
* - ``xdp/<decimal-mask>``
- XDP
- ``BPF_LINK_CREATE``, ``BPF_XDP``, native driver mode
- - native XDP support on selected devices
+ - native XDP and multi-buffer support on selected devices
* - ``syscall``, named ``shirudo_serialize__<map>``
- syscall
- not attached, run by ``shirudo upatch map``
@@ -249,7 +249,8 @@ XDP
device name or an ifindex. For example, ``SEC("xdp/3")`` selects network
ports and virtual devices; ``SEC("xdp/27")`` selects all classes. The
mask is mandatory, nonzero and contains only the bits below. Bare
-``xdp``, ``xdp.frags``, devmap and cpumap sections are refused.
+``xdp``, ``xdp.frags``, devmap and cpumap sections are refused;
+``xdp/<decimal-mask>`` already means multi-buffer.
.. list-table::
:header-rows: 1
@@ -314,6 +315,52 @@ programs are never replaced. Device classification and peer identity
are rechecked around attachment, and the returned link's ifindex is
checked before pinning.
+Every XDP program is loaded with ``BPF_F_XDP_HAS_FRAGS``, so it can run on
+a device whose MTU does not fit one buffer. A selected device with native
+XDP must report multi-buffer support, ``NETDEV_XDP_ACT_RX_SG``. One that
+does not fails ``upatch load`` with ``xdp_multibuf_unsupported``, and the
+load is undone, so a single such device under the signed mask stops it:
+narrow the mask. Boot restore fails on such a device too. Reconcile leaves
+it unattached and logs a warning. In a tree, a failed ``add`` stops ``bpm tree sync`` at
+that operation, so scope it with a node label.
+
+These drivers report multi-buffer XDP in Linux bpf-next at 53435562a725:
+
+.. list-table::
+ :header-rows: 1
+ :widths: 30 70
+
+ * - Driver
+ - Hardware or use
+ * - ``veth``
+ - Virtual Ethernet pairs
+ * - ``virtio_net``
+ - Virtio guest adapters, with mergeable receive buffers
+ * - ``ice``, ``i40e``, ``idpf``
+ - Intel
+ * - ``mlx5``
+ - NVIDIA ConnectX, with LRO and hardware GRO off
+ * - ``bnxt``
+ - Broadcom NetXtreme
+ * - ``ionic``
+ - AMD Pensando
+ * - ``fbnic``
+ - Meta
+ * - ``atlantic``
+ - Marvell (Aquantia) AQtion
+ * - ``mvneta``
+ - Marvell Armada, without hardware buffer management
+ * - ``enetc``
+ - NXP
+ * - ``bonding``
+ - When every slave supports it
+
+The list is a guide. What counts is what the device reports when it is
+attached, and that can depend on its configuration: a ``veth`` with fewer
+rx queues than its peer has tx queues reports nothing. ``ena``, ``gve``,
+``mana``, ``netvsc``, ``vmxnet3``, ``xen-netfront``, ``igb``, ``igc``,
+``ixgbe`` and ``mlx4`` have native XDP without multi-buffer.
+
Load and restore each discover devices once.
:doc:`/reference/cli/upatch-reconcile` repeats that discovery later: it
attaches a loaded upatch to any selected device it does not run on yet
@@ -381,8 +428,8 @@ it generates a loader.
* - every other section, ``tp/``, ``raw_tp/``, ``xdp`` and ``tc`` among
them
- ``unsupported section '<section>'; supported are fentry, fexit,
- fmod_ret, lsm, tp_btf, kprobe, kretprobe, uprobe.multi, cgroup_skb
- and netfilter``
+ fmod_ret, lsm, tp_btf, kprobe, kretprobe, uprobe.multi, cgroup_skb,
+ netfilter and xdp/<mask>``
* - ``syscall`` with another program name
- ``SEC("syscall") program '<name>' must be named shirudo_test__<id>``
diff --git a/Documentation/upatches/write/pitfalls.rst b/Documentation/upatches/write/pitfalls.rst
index ca7308fe..8afd5874 100644
--- a/Documentation/upatches/write/pitfalls.rst
+++ b/Documentation/upatches/write/pitfalls.rst
@@ -96,10 +96,11 @@ What an xdp program does not see
- It sees packets as they arrive, before GRO joins them, so a transfer
is many runs.
- A VLAN tag the NIC stripped is not in the packet.
-- It sees one buffer per packet. ``xdp/<mask>`` programs are loaded without
- multi-buffer support, as ``xdp.frags`` is refused, and a driver refuses
- them outright on a device whose MTU does not fit one buffer, which
- leaves that device uncovered.
+- Direct packet access reaches only a packet's first buffer. Every
+ ``xdp/<mask>`` program is loaded with multi-buffer support, so on a large
+ MTU the rest of a frame is in fragments and ``ctx->data_end`` stops at the
+ first one. Read past it with ``bpf_xdp_load_bytes()``, and take the frame's
+ length from ``bpf_xdp_get_buff_len()``.
RCU pointers in sleepable programs
----------------------------------
diff --git a/testing/functional/upatch/t3111-xdp.sh b/testing/functional/upatch/t3111-xdp.sh
index d360a0e5..b3f0ba9a 100644
--- a/testing/functional/upatch/t3111-xdp.sh
+++ b/testing/functional/upatch/t3111-xdp.sh
@@ -37,6 +37,10 @@ cleanup() {
for name in "$NAME" "$CONFLICT" "$VM_PATCH"; do
unload "$name" >/dev/null 2>&1 || true
done
+ if [ -n "${adapter_mtu:-}" ]; then
+ ip link set "$adapter" mtu "$adapter_mtu" >/dev/null 2>&1 || true
+ adapter_mtu=
+ fi
for dev in xdph0 xdph1 xdpa xdpc xdpbr; do
ip link del "$dev" >/dev/null 2>&1 || true
done
@@ -97,6 +101,28 @@ test_xdp_success 'the VM class selects the guest adapter' '
unload "$VM_PATCH" && [ "$("$H" query "$adapter")" = 0 ]
'
+test_xdp_success 'virtio_net attaches above the single-buffer MTU' '
+ adapter=$(ip -o route show default | awk "{print \$5; exit}") &&
+ [ -n "$adapter" ] &&
+ [ "$(basename "$(readlink "/sys/class/net/$adapter/device/driver")")" = virtio_net ] &&
+ adapter_mtu=$(cat "/sys/class/net/$adapter/mtu") &&
+ ip link set "$adapter" mtu 9000 &&
+ load "$VM_PATCH" &&
+ [ "$("$H" query "$adapter")" -gt 0 ] &&
+ unload "$VM_PATCH" &&
+ ip link set "$adapter" mtu "$adapter_mtu" &&
+ adapter_mtu=
+'
+
+test_xdp_success 'veth attaches above the single-buffer MTU' '
+ ip link add xdpa index 30000 type veth peer name xdpc index 30001 &&
+ ip link set xdpc mtu 9000 &&
+ ip link set xdpa up && ip link set xdpc up &&
+ load "$NAME" &&
+ [ "$("$H" query xdpa)" -gt 0 ] &&
+ unload "$NAME"
+'
+
test_xdp_success 'both host endpoints of separate cross-namespace pairs attach' '
ip netns add xdp-test-a && ip netns add xdp-test-b &&
ip link add xdpa type veth peer name xdpa-peer netns xdp-test-a &&
diff --git a/testing/functional/upatch/t3112-xdp-reconcile.sh b/testing/functional/upatch/t3112-xdp-reconcile.sh
index fca2f755..450daceb 100644
--- a/testing/functional/upatch/t3112-xdp-reconcile.sh
+++ b/testing/functional/upatch/t3112-xdp-reconcile.sh
@@ -131,8 +131,9 @@ test_xdp_success 'a veth end moved away keeps its link and the host end attaches
test_xdp_success 'a driver refusing one device warns and spares the others' '
add_pair xdpa 30000 xdpb 30001 &&
load "$NAME" &&
- add_pair xdpc 30010 xdpd 30011 &&
- ip link set xdpd mtu 9000 &&
+ ip link add xdpc index 30010 numrxqueues 1 type veth \
+ peer name xdpd index 30011 numtxqueues 2 &&
+ ip link set xdpc up && ip link set xdpd up &&
add_pair xdpe 30020 xdpf 30021 &&
out=$(reconcile 2>&1) &&
[ "$("$H" query xdpc)" = 0 ] &&
diff --git a/tools/shirudo-build/package.c b/tools/shirudo-build/package.c
index 747fc47f..5cf5dd75 100644
--- a/tools/shirudo-build/package.c
+++ b/tools/shirudo-build/package.c
@@ -1492,10 +1492,17 @@ static int set_target_attach_types(struct bpf_object *obj)
type = BPF_CGROUP_INET_INGRESS;
else if (strcmp(sec, "cgroup_skb/egress") == 0)
type = BPF_CGROUP_INET_EGRESS;
- else if (!strncmp(sec, "xdp/", 4))
+ else if (!strncmp(sec, "xdp/", 4)) {
type = BPF_XDP;
- else
+ if (bpf_program__set_flags(p, bpf_program__flags(p) |
+ BPF_F_XDP_HAS_FRAGS) != 0) {
+ pr_err("set multi-buffer XDP for program '%s'",
+ bpf_program__name(p));
+ return -EINVAL;
+ }
+ } else {
continue;
+ }
if (bpf_program__set_expected_attach_type(p, type) != 0) {
pr_err("set expected attach type for program '%s'",