Skip to content

Commit 860d79d

Browse files
committed
ext/sockets update the sock_filter usage for the CPBF filter.
disapatching now (modulo) number of available cores.
1 parent 6ca6b46 commit 860d79d

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

ext/sockets/sockets.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,17 +1969,39 @@ PHP_FUNCTION(socket_set_option)
19691969
opt_ptr = &ov;
19701970
optname = SO_DETACH_BPF;
19711971
} else {
1972+
/**
1973+
* This socket flag offers a tigher control over the SO_REUSEPORT's algorithm,
1974+
* here binding a socket to a particular cpu set as opposed to the Round-Robin
1975+
* default one.
1976+
*/
19721977
uint32_t k = (uint32_t)Z_LVAL_P(arg4);
19731978
static struct sock_filter cbpf[8] = {0};
19741979
static struct sock_fprog bpfprog;
19751980

1981+
// TODO: Might be a good enough approximation, more native api could be used potentially tough.
1982+
long onln = sysconf(_SC_NPROCESSORS_ONLN);
1983+
1984+
if (onln == -1) {
1985+
php_error_docref(NULL, E_WARNING, "Online cores fetching failed");
1986+
RETURN_FALSE;
1987+
}
1988+
19761989
switch (k) {
19771990
case SKF_AD_CPU:
19781991
case SKF_AD_QUEUE:
1992+
/**
1993+
* We are putting the current socket's filter operation into `A`
1994+
* instead of the current core, we are dispatching (modulo)
1995+
* among `onln` cores as opposed to bind it via the raw_smp_processor_id()
1996+
* kernel call.
1997+
* Then the sock's filter returns `A`
1998+
*/
19791999
cbpf[0].code = (BPF_LD|BPF_W|BPF_ABS);
19802000
cbpf[0].k = (uint32_t)(SKF_AD_OFF + k);
1981-
cbpf[1].code = (BPF_RET|BPF_A);
1982-
bpfprog.len = 2;
2001+
cbpf[1].code = (BPF_ALU | BPF_MOD);
2002+
cbpf[1].k = (uint32_t)(onln);
2003+
cbpf[2].code = (BPF_RET|BPF_A);
2004+
bpfprog.len = 3;
19832005
break;
19842006
default:
19852007
php_error_docref(NULL, E_WARNING, "Unsupported CBPF filter");

0 commit comments

Comments
 (0)