SO_REUSEPORT Patch - NGINX
You can obtain the open source NGINX application from Git Hub. The following patch is provided for reference only without guarantee of support or compatibility.
diff --git a/src/core/ngx_connection.c b/src/core/ngx_connection.c
index 16ba630..93d6e1e 100644
--- a/src/core/ngx_connection.c
+++ b/src/core/ngx_connection.c
@@ -8,6 +8,8 @@
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_event.h>
+#include <linux/filter.h>
+#include <error.h>
ngx_os_io_t ngx_io;
@@ -645,6 +647,30 @@ ngx_open_listening_sockets(ngx_cycle_t *cycle)
return NGX_OK;
}
+#ifndef ARRAY_SIZE
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+#endif
+
+static void attach_bpf(int fd, uint16_t n)
+{
+ struct sock_filter code[] = {
+ /* A = skb->queue_mapping */
+ { BPF_LD | BPF_W | BPF_ABS, 0, 0, SKF_AD_OFF + SKF_AD_QUEUE },
+ /* A = A % n */
+ { BPF_ALU | BPF_MOD, 0, 0, n },
+ /* return A */
+ { BPF_RET | BPF_A, 0, 0, 0 },
+ };
+ struct sock_fprog p = {
+ .len = ARRAY_SIZE(code),
+ .filter = code,
+ };
+
+ if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_REUSEPORT_CBPF, &p, sizeof(p)))
+ error(1, errno, "failed to set SO_ATTACH_REUSEPORT_CBPF");
+ else
+ printf("bpf prog attached to fd:%d\n", fd);
+}
void
ngx_configure_listening_sockets(ngx_cycle_t *cycle)
@@ -948,6 +974,8 @@ ngx_configure_listening_sockets(ngx_cycle_t *cycle)
}
#endif
+
+ attach_bpf(ls[i].fd, cycle->listening.nelts);
}
return;