Repository: cilium/ebpf
Pull request #1037 (2023-05-16 16:10:22)

elf_reader: Add support for XDP frags
implements #1036 

Since v5.18 of the Linux kernel, it is possible for an XDP program to process packets larger than the physical page of the system. This feature is called XDP frags.

In order for an XDP program to signal that it can process packets with large MTUs, it needs to have the "xdp.frags" annotation instead of "xdp". This new annotation causes the BPF_F_XDP_HAS_FRAGS to be added to the annotated program section of the ELF. This flag in turn causes the verifier to apply extra checks to the code. The commit along with some documentation about the feature can be seen here:
https://github.com/torvalds/linux/commit/c2f2cdbeffda7b153c19e0f3d73149c41026c0db

This commit allows the code to parse the "xdp.frags" annotation and adds the appropriate flag to the program section, along with unit tests for this behaviour. It replicated the functionality of this code: https://elixir.bootlin.com/linux/v6.0/source/tools/lib/bpf/libbpf.c#L6626


Patch:
diff --git a/elf_reader.go b/elf_reader.go
index a202f25..8d92672 100644
--- a/elf_reader.go
+++ b/elf_reader.go
@@ -1221,8 +1221,11 @@ func getProgType(sectionName string) (ProgramType, AttachType, uint32, string) {
 		{"iter/", Tracing, AttachTraceIter, 0},
 		{"iter.s/", Tracing, AttachTraceIter, unix.BPF_F_SLEEPABLE},
 		{"syscall", Syscall, AttachNone, 0},
+		{"xdp.frags_devmap/", XDP, AttachXDPDevMap, unix.BPF_F_XDP_HAS_FRAGS},
 		{"xdp_devmap/", XDP, AttachXDPDevMap, 0},
+		{"xdp.frags_cpumap/", XDP, AttachXDPCPUMap, unix.BPF_F_XDP_HAS_FRAGS},
 		{"xdp_cpumap/", XDP, AttachXDPCPUMap, 0},
+		{"xdp.frags", XDP, AttachNone, unix.BPF_F_XDP_HAS_FRAGS},
 		{"xdp", XDP, AttachNone, 0},
 		{"perf_event", PerfEvent, AttachNone, 0},
 		{"lwt_in", LWTIn, AttachNone, 0},
diff --git a/internal/unix/types_linux.go b/internal/unix/types_linux.go
index e6b4c07..31b8570 100644
--- a/internal/unix/types_linux.go
+++ b/internal/unix/types_linux.go
@@ -35,6 +35,7 @@ const (
 	BPF_F_RDONLY_PROG         = linux.BPF_F_RDONLY_PROG
 	BPF_F_WRONLY_PROG         = linux.BPF_F_WRONLY_PROG
 	BPF_F_SLEEPABLE           = linux.BPF_F_SLEEPABLE
+	BPF_F_XDP_HAS_FRAGS       = linux.BPF_F_XDP_HAS_FRAGS
 	BPF_F_MMAPABLE            = linux.BPF_F_MMAPABLE
 	BPF_F_INNER_MAP           = linux.BPF_F_INNER_MAP
 	BPF_F_KPROBE_MULTI_RETURN = linux.BPF_F_KPROBE_MULTI_RETURN
diff --git a/internal/unix/types_other.go b/internal/unix/types_other.go
index 96a7b24..f48a6b7 100644
--- a/internal/unix/types_other.go
+++ b/internal/unix/types_other.go
@@ -41,6 +41,7 @@ const (
 	BPF_F_MMAPABLE
 	BPF_F_INNER_MAP
 	BPF_F_KPROBE_MULTI_RETURN
+	BPF_F_XDP_HAS_FRAGS
 	BPF_OBJ_NAME_LEN
 	BPF_TAG_SIZE
 	BPF_RINGBUF_BUSY_BIT


Test patch:
diff --git a/elf_reader_test.go b/elf_reader_test.go
index 6bfde6d..680f9d1 100644
--- a/elf_reader_test.go
+++ b/elf_reader_test.go
@@ -1121,11 +1121,23 @@ func TestGetProgType(t *testing.T) {
 			At: AttachNone,
 			To: "",
 		},
+		"xdp.frags/foo": {
+			Pt: XDP,
+			At: AttachNone,
+			To: "",
+			Fl: unix.BPF_F_XDP_HAS_FRAGS,
+		},
 		"xdp_devmap/foo": {
 			Pt: XDP,
 			At: AttachXDPDevMap,
 			To: "foo",
 		},
+		"xdp.frags_devmap/foo": {
+			Pt: XDP,
+			At: AttachXDPDevMap,
+			To: "foo",
+			Fl: unix.BPF_F_XDP_HAS_FRAGS,
+		},
 		"cgroup_skb/ingress": {
 			Pt: CGroupSKB,
 			At: AttachCGroupInetIngress,