]> git.kaiwu.me - nginx.git/commitdiff
QUIC: fixed bpf compilation with newer Linux kernels.
authorRoman Arutyunyan <arut@nginx.com>
Tue, 24 Sep 2024 10:53:45 +0000 (14:53 +0400)
committerRoman Arutyunyan <arutyunyan.roman@gmail.com>
Thu, 26 Feb 2026 16:43:03 +0000 (20:43 +0400)
QUIC bpf program previously used struct bpf_map_def which was
deprecated in [1] (kernel version 5.18) and removed in [2] (kernel 6.1).
New-style BTF map definitions were added in [3] (linux kernel 5.3).

Switching the program to BTF is however not necessary since nginx has
its own relocation procedure which allows referencing the real map
structure by its file descriptor allocated earlier.  In particular,
bpf instruction BPF_LD_IMM64 (0x18/0x0) is substituted with instruction
BPF_LD_MAP_FD (0x18/0x1) and map_fd is stored in the imm field, see [4]
and [5] for details.

To fix compilation, struct bpf_map_def is changed to a known type (int)
and "extern" is added to indicate external linkage and reduce object
file size.

[1] https://github.com/torvalds/linux/commit/93b8952d223af03c51fba0c6258173d2ffbd2cb7
[2] https://github.com/torvalds/linux/commit/dc567045f1590f6460d3e9a6ea6ad5e600b58b84
[3] https://github.com/torvalds/linux/commit/abd29c9314595b1ee5ec6c61d7c49a497ffb30a3
[4] https://github.com/torvalds/linux/blob/master/include/linux/filter.h
[5] https://datatracker.ietf.org/doc/rfc9669/

src/event/quic/bpf/ngx_quic_reuseport_helper.c
src/event/quic/ngx_event_quic_bpf_code.c

index 999e7607c7a5663946b10448d616354d5a9980c9..51b78f043600d1c3315f4bf52ca3f9343b033aab 100644 (file)
@@ -67,7 +67,7 @@ char _license[] SEC("license") = LICENSE;
  * actual map object is created by the "bpf" system call,
  * all pointers to this variable are replaced by the bpf loader
  */
-struct bpf_map_def SEC("maps") ngx_quic_sockmap;
+extern int ngx_quic_sockmap;
 
 
 SEC(PROGNAME)
index 5c9dea1c1018c4d8f873b1657494de07cf38c320..625f404056d9bd02e5757305a09f39b637b2b659 100644 (file)
@@ -7,38 +7,42 @@
 
 
 static ngx_bpf_reloc_t bpf_reloc_prog_ngx_quic_reuseport_helper[] = {
-    { "ngx_quic_sockmap", 55 },
+    { "ngx_quic_sockmap", 59 },
 };
 
 static struct bpf_insn bpf_insn_prog_ngx_quic_reuseport_helper[] = {
     /* opcode dst          src         offset imm */
-    { 0x79,   BPF_REG_4,   BPF_REG_1, (int16_t)      0,        0x0 },
+    { 0x79,   BPF_REG_2,   BPF_REG_1, (int16_t)      0,        0x0 },
     { 0x79,   BPF_REG_3,   BPF_REG_1, (int16_t)      8,        0x0 },
-    { 0xbf,   BPF_REG_2,   BPF_REG_4, (int16_t)      0,        0x0 },
-    {  0x7,   BPF_REG_2,   BPF_REG_0, (int16_t)      0,        0x8 },
-    { 0x2d,   BPF_REG_2,   BPF_REG_3, (int16_t)     54,        0x0 },
-    { 0xbf,   BPF_REG_5,   BPF_REG_4, (int16_t)      0,        0x0 },
-    {  0x7,   BPF_REG_5,   BPF_REG_0, (int16_t)      0,        0x9 },
-    { 0x2d,   BPF_REG_5,   BPF_REG_3, (int16_t)     51,        0x0 },
+    { 0xbf,   BPF_REG_6,   BPF_REG_2, (int16_t)      0,        0x0 },
+    {  0x7,   BPF_REG_6,   BPF_REG_0, (int16_t)      0,        0x8 },
+    { 0x2d,   BPF_REG_6,   BPF_REG_3, (int16_t)     58,        0x0 },
+    { 0xbf,   BPF_REG_4,   BPF_REG_2, (int16_t)      0,        0x0 },
+    {  0x7,   BPF_REG_4,   BPF_REG_0, (int16_t)      0,        0x9 },
+    { 0x2d,   BPF_REG_4,   BPF_REG_3, (int16_t)     55,        0x0 },
+    { 0xb7,   BPF_REG_4,   BPF_REG_0, (int16_t)      0,        0x8 },
     { 0xb7,   BPF_REG_5,   BPF_REG_0, (int16_t)      0,       0x14 },
     { 0xb7,   BPF_REG_0,   BPF_REG_0, (int16_t)      0,        0x9 },
-    { 0x71,   BPF_REG_6,   BPF_REG_2, (int16_t)      0,        0x0 },
+    { 0x71,   BPF_REG_6,   BPF_REG_6, (int16_t)      0,        0x0 },
     { 0x67,   BPF_REG_6,   BPF_REG_0, (int16_t)      0,       0x38 },
     { 0xc7,   BPF_REG_6,   BPF_REG_0, (int16_t)      0,       0x38 },
-    { 0x65,   BPF_REG_6,   BPF_REG_0, (int16_t)     10, 0xffffffff },
-    { 0xbf,   BPF_REG_2,   BPF_REG_4, (int16_t)      0,        0x0 },
-    {  0x7,   BPF_REG_2,   BPF_REG_0, (int16_t)      0,        0xd },
-    { 0x2d,   BPF_REG_2,   BPF_REG_3, (int16_t)     42,        0x0 },
-    { 0xbf,   BPF_REG_5,   BPF_REG_4, (int16_t)      0,        0x0 },
-    {  0x7,   BPF_REG_5,   BPF_REG_0, (int16_t)      0,        0xe },
-    { 0x2d,   BPF_REG_5,   BPF_REG_3, (int16_t)     39,        0x0 },
+    { 0x65,   BPF_REG_6,   BPF_REG_0, (int16_t)     11, 0xffffffff },
+    { 0xbf,   BPF_REG_5,   BPF_REG_2, (int16_t)      0,        0x0 },
+    {  0x7,   BPF_REG_5,   BPF_REG_0, (int16_t)      0,        0xd },
+    { 0x2d,   BPF_REG_5,   BPF_REG_3, (int16_t)     45,        0x0 },
+    { 0xbf,   BPF_REG_4,   BPF_REG_2, (int16_t)      0,        0x0 },
+    {  0x7,   BPF_REG_4,   BPF_REG_0, (int16_t)      0,        0xe },
+    { 0x2d,   BPF_REG_4,   BPF_REG_3, (int16_t)     42,        0x0 },
+    { 0xb7,   BPF_REG_4,   BPF_REG_0, (int16_t)      0,        0xd },
     { 0xb7,   BPF_REG_0,   BPF_REG_0, (int16_t)      0,        0xe },
-    { 0x71,   BPF_REG_5,   BPF_REG_2, (int16_t)      0,        0x0 },
+    { 0x71,   BPF_REG_5,   BPF_REG_5, (int16_t)      0,        0x0 },
     { 0xb7,   BPF_REG_6,   BPF_REG_0, (int16_t)      0,        0x8 },
-    { 0x2d,   BPF_REG_6,   BPF_REG_5, (int16_t)     35,        0x0 },
-    {  0xf,   BPF_REG_5,   BPF_REG_0, (int16_t)      0,        0x0 },
-    {  0xf,   BPF_REG_4,   BPF_REG_5, (int16_t)      0,        0x0 },
-    { 0x2d,   BPF_REG_4,   BPF_REG_3, (int16_t)     32,        0x0 },
+    { 0x2d,   BPF_REG_6,   BPF_REG_5, (int16_t)     37,        0x0 },
+    { 0xbf,   BPF_REG_6,   BPF_REG_2, (int16_t)      0,        0x0 },
+    {  0xf,   BPF_REG_6,   BPF_REG_0, (int16_t)      0,        0x0 },
+    {  0xf,   BPF_REG_6,   BPF_REG_5, (int16_t)      0,        0x0 },
+    { 0x2d,   BPF_REG_6,   BPF_REG_3, (int16_t)     33,        0x0 },
+    {  0xf,   BPF_REG_2,   BPF_REG_4, (int16_t)      0,        0x0 },
     { 0xbf,   BPF_REG_4,   BPF_REG_2, (int16_t)      0,        0x0 },
     {  0x7,   BPF_REG_4,   BPF_REG_0, (int16_t)      0,        0x9 },
     { 0x2d,   BPF_REG_4,   BPF_REG_3, (int16_t)     29,        0x0 },
@@ -86,3 +90,4 @@ ngx_bpf_program_t ngx_quic_reuseport_helper = {
     .license = "BSD",
     .type = BPF_PROG_TYPE_SK_REUSEPORT,
 };
+