Skip to content

socket module add SO_ATTACH_REUSEPORT_CPBF for Linux. #8062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/sockets/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ PHP_ARG_ENABLE([sockets],

if test "$PHP_SOCKETS" != "no"; then
AC_CHECK_FUNCS([hstrerror if_nametoindex if_indextoname])
AC_CHECK_HEADERS([netinet/tcp.h sys/un.h sys/sockio.h])
AC_CHECK_HEADERS([netinet/tcp.h sys/un.h sys/sockio.h linux/filter.h])
AC_DEFINE([HAVE_SOCKETS], 1, [ ])

dnl Check for fied ss_family in sockaddr_storage (missing in AIX until 5.3)
Expand Down
35 changes: 35 additions & 0 deletions ext/sockets/sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -1741,6 +1741,7 @@ PHP_FUNCTION(socket_get_option)
return;
}
#endif

}
}

Expand Down Expand Up @@ -1960,6 +1961,40 @@ PHP_FUNCTION(socket_set_option)
}
#endif

#ifdef SO_ATTACH_REUSEPORT_CBPF
case SO_ATTACH_REUSEPORT_CBPF: {
convert_to_long(arg4);

if (!Z_LVAL_P(arg4)) {
ov = 1;
optlen = sizeof(ov);
opt_ptr = &ov;
optname = SO_DETACH_BPF;
} else {
uint32_t k = (uint32_t)Z_LVAL_P(arg4);
static struct sock_filter cbpf[8] = {0};
static struct sock_fprog bpfprog;

switch (k) {
case SKF_AD_CPU:
cbpf[0].code = (BPF_LD|BPF_W|BPF_ABS);
cbpf[0].k = (uint32_t)(SKF_AD_OFF + k);
cbpf[1].code = (BPF_RET|BPF_A);
bpfprog.len = 2;
break;
default:
php_error_docref(NULL, E_WARNING, "Unsupported CBPF filter");
RETURN_FALSE;
}

bpfprog.filter = cbpf;
optlen = sizeof(bpfprog);
opt_ptr = &bpfprog;
}
break;
}
#endif

default:
default_case:
convert_to_long(arg4);
Expand Down
21 changes: 21 additions & 0 deletions ext/sockets/sockets.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -1678,6 +1678,27 @@
*/
const LOCAL_CREDS = UNKNOWN;
#endif
#if defined(SO_ATTACH_REUSEPORT_CBPF)
/**
* @var int
* @cvalue SO_ATTACH_REUSEPORT_CBPF
*/
const SO_ATTACH_REUSEPORT_CBPF = UNKNOWN;
#endif
#if defined(SO_DETACH_FILTER)
/**
* @var int
* @cvalue SO_DETACH_FILTER
*/
const SO_DETACH_FILTER = UNKNOWN;
#endif
#if defined(SO_DETACH_BPF)
/**
* @var int
* @cvalue SO_DETACH_BPF
*/
const SO_DETACH_BPF = UNKNOWN;
#endif

/**
* @strict-properties
Expand Down
11 changes: 10 additions & 1 deletion ext/sockets/sockets_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions ext/sockets/tests/socket_reuseport_cbpf.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
Test if socket_set_option() works, option:SO_ATTACH_REUSEPORT_CBPF
--EXTENSIONS--
sockets
--SKIPIF--
<?php

if (!defined("SO_ATTACH_REUSEPORT_CBPF")) {
die('SKIP on platforms not supporting SO_ATTACH_REUSEPORT_CBPF');
}
?>
--FILE--
<?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

if (!$socket) {
die('Unable to create AF_INET socket [socket]');
}
var_dump(socket_set_option( $socket, SOL_SOCKET, SO_REUSEADDR, true));
var_dump(socket_set_option( $socket, SOL_SOCKET, SO_REUSEPORT, true));
var_dump(socket_set_option( $socket, SOL_SOCKET, SO_ATTACH_REUSEPORT_CBPF, SKF_AD_CPU));
var_dump(socket_bind($socket, '0.0.0.0'));
socket_listen($socket);
socket_close($socket);
?>
--EXPECT--
bool(true)
bool(true)
bool(true)
bool(true)