Skip to content

sockets add SO_MEMINFO data gathering which allows to check #8411

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
37 changes: 37 additions & 0 deletions ext/sockets/sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
# if HAVE_IF_NAMETOINDEX
# include <net/if.h>
# endif
# ifdef SO_MEMINFO
# include <linux/sock_diag.h>
# endif
#endif

#include <stddef.h>
Expand Down Expand Up @@ -550,6 +553,9 @@ static PHP_MINIT_FUNCTION(sockets)
#ifdef SO_INCOMING_CPU
REGISTER_LONG_CONSTANT("SO_INCOMING_CPU", SO_INCOMING_CPU, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef SO_MEMINFO
REGISTER_LONG_CONSTANT("SO_MEMINFO", SO_MEMINFO, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef TCP_NODELAY
REGISTER_LONG_CONSTANT("TCP_NODELAY", TCP_NODELAY, CONST_CS | CONST_PERSISTENT);
#endif
Expand Down Expand Up @@ -1833,6 +1839,37 @@ PHP_FUNCTION(socket_get_option)
add_assoc_long(return_value, "sec", tv.tv_sec);
add_assoc_long(return_value, "usec", tv.tv_usec);
return;
#ifdef SO_MEMINFO
case SO_MEMINFO: {
uint32_t minfo[SK_MEMINFO_VARS];
optlen = sizeof(minfo);

if (getsockopt(php_sock->bsd_socket, level, optname, (char*)minfo, &optlen) != 0) {
PHP_SOCKET_ERROR(php_sock, "Unable to retrieve socket option", errno);
RETURN_FALSE;
}

if (UNEXPECTED(optlen != sizeof(minfo))) {
// unlikely since the kernel fills up the whole array if getsockopt succeeded
// but just an extra precaution in case.
php_error_docref(NULL, E_WARNING, "Unable to retrieve all socket meminfo data");
RETURN_FALSE;
}

array_init(return_value);

add_assoc_long(return_value, "rmem_alloc", minfo[SK_MEMINFO_RMEM_ALLOC]);
add_assoc_long(return_value, "rcvbuf", minfo[SK_MEMINFO_RCVBUF]);
add_assoc_long(return_value, "wmem_alloc", minfo[SK_MEMINFO_WMEM_ALLOC]);
add_assoc_long(return_value, "sndbuf", minfo[SK_MEMINFO_SNDBUF]);
add_assoc_long(return_value, "fwd_alloc", minfo[SK_MEMINFO_FWD_ALLOC]);
add_assoc_long(return_value, "wmem_queued", minfo[SK_MEMINFO_WMEM_QUEUED]);
add_assoc_long(return_value, "optmem", minfo[SK_MEMINFO_OPTMEM]);
add_assoc_long(return_value, "backlog", minfo[SK_MEMINFO_BACKLOG]);
add_assoc_long(return_value, "drops", minfo[SK_MEMINFO_DROPS]);
return;
}
#endif
#ifdef SO_ACCEPTFILTER
case SO_ACCEPTFILTER: {

Expand Down
71 changes: 71 additions & 0 deletions ext/sockets/tests/socket_meminfo.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
--TEST--
socket_get_option MEMINFO;
--EXTENSIONS--
sockets
--SKIPIF--
<?php
if (!defined("SO_MEMINFO")) die('skip SO_MEMINFO test');
?>
--FILE--
<?php
$server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!$server) {
die('Unable to create AF_INET socket [server]');
}

if (!socket_bind($server, '127.0.0.1', 0)) {
die("Unable to bind to 127.0.0.1");
}

if (!socket_listen($server, 2)) {
die('Unable to listen on socket');
}

socket_getsockname($server, $unused, $port);

/* Connect to it */
$client = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!$client) {
die('Unable to create AF_INET socket [client]');
}
if (!socket_connect($client, '127.0.0.1', $port)) {
die('Unable to connect to server socket');
}

/* Accept that connection */
$socket = socket_accept($server);
if (!$socket) {
die('Unable to accept connection');
}

socket_write($client, "SO_MEMINFO\n");

socket_read($socket, strlen("SO_MEMINFO"), PHP_BINARY_READ);
$data = socket_get_option($socket, SOL_SOCKET, SO_MEMINFO);
var_dump($data);

socket_close($client);
socket_close($socket);
socket_close($server);
?>
--EXPECTF--
array(9) {
["rmem_alloc"]=>
int(%d)
["rcvbuf"]=>
int(%d)
["wmem_alloc"]=>
int(%d)
["sndbuf"]=>
int(%d)
["fwd_alloc"]=>
int(%d)
["wmem_queued"]=>
int(%d)
["optmem"]=>
int(%d)
["backlog"]=>
int(%d)
["drops"]=>
int(%d)
}