Skip to content

Commit 5174ee2

Browse files
committed
FPM add routing view global option (for FreeBSD for now).
set the route table FIB id to the sockets created within FPM up to the max set by the system, avoiding having to use setfib command line. Closes #8470.
1 parent 2ee6e0d commit 5174ee2

File tree

7 files changed

+104
-0
lines changed

7 files changed

+104
-0
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.2.0beta1
44

5+
- FPM:
6+
- Added listen.setfib pool option to set route FIB on FreeBSD. (David Carlier)
7+
58
07 Jul 2022, PHP 8.2.0alpha3
69

710
- Core:

sapi/fpm/fpm/fpm_conf.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ static struct ini_value_parser_s ini_fpm_pool_options[] = {
125125
{ "listen.group", &fpm_conf_set_string, WPO(listen_group) },
126126
{ "listen.mode", &fpm_conf_set_string, WPO(listen_mode) },
127127
{ "listen.allowed_clients", &fpm_conf_set_string, WPO(listen_allowed_clients) },
128+
#ifdef SO_SETFIB
129+
{ "listen.setfib", &fpm_conf_set_integer, WPO(listen_setfib) },
130+
#endif
128131
{ "process.priority", &fpm_conf_set_integer, WPO(process_priority) },
129132
{ "process.dumpable", &fpm_conf_set_boolean, WPO(process_dumpable) },
130133
{ "pm", &fpm_conf_set_pm, WPO(pm) },
@@ -618,6 +621,9 @@ static void *fpm_worker_pool_config_alloc(void) /* {{{ */
618621
wp->config->process_dumpable = 0;
619622
wp->config->clear_env = 1;
620623
wp->config->decorate_workers_output = 1;
624+
#ifdef SO_SETFIB
625+
wp->config->listen_setfib = -1;
626+
#endif
621627

622628
if (!fpm_worker_all_pools) {
623629
fpm_worker_all_pools = wp;
@@ -1715,6 +1721,9 @@ static void fpm_conf_dump(void) /* {{{ */
17151721
zlog(ZLOG_NOTICE, "\tlisten.group = %s", STR2STR(wp->config->listen_group));
17161722
zlog(ZLOG_NOTICE, "\tlisten.mode = %s", STR2STR(wp->config->listen_mode));
17171723
zlog(ZLOG_NOTICE, "\tlisten.allowed_clients = %s", STR2STR(wp->config->listen_allowed_clients));
1724+
#ifdef SO_SETFIB
1725+
zlog(ZLOG_NOTICE, "\tlisten.setfib = %d", wp->config->listen_setfib);
1726+
#endif
17181727
if (wp->config->process_priority == 64) {
17191728
zlog(ZLOG_NOTICE, "\tprocess.priority = undefined");
17201729
} else {

sapi/fpm/fpm/fpm_conf.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ struct fpm_worker_pool_config_s {
103103
char *listen_acl_users;
104104
char *listen_acl_groups;
105105
#endif
106+
#ifdef SO_SETFIB
107+
int listen_setfib;
108+
#endif
106109
};
107110

108111
struct ini_value_parser_s {

sapi/fpm/fpm/fpm_sockets.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ static struct fpm_array_s sockets_list;
3939

4040
enum { FPM_GET_USE_SOCKET = 1, FPM_STORE_SOCKET = 2, FPM_STORE_USE_SOCKET = 3 };
4141

42+
#ifdef SO_SETFIB
43+
static int routemax = -1;
44+
#endif
45+
4246
static inline void fpm_sockets_get_env_name(char *envname, unsigned idx) /* {{{ */
4347
{
4448
if (!idx) {
@@ -250,6 +254,20 @@ static int fpm_sockets_new_listening_socket(struct fpm_worker_pool_s *wp, struct
250254
return -1;
251255
}
252256

257+
#ifdef SO_SETFIB
258+
if (-1 < wp->config->listen_setfib) {
259+
if (routemax < wp->config->listen_setfib) {
260+
zlog(ZLOG_ERROR, "Invalid routing table id %d, max is %d", wp->config->listen_setfib, routemax);
261+
close(sock);
262+
return -1;
263+
}
264+
265+
if (0 > setsockopt(sock, SOL_SOCKET, SO_SETFIB, &wp->config->listen_setfib, sizeof(wp->config->listen_setfib))) {
266+
zlog(ZLOG_WARNING, "failed to change socket SO_SETFIB attribute");
267+
}
268+
}
269+
#endif
270+
253271
return sock;
254272
}
255273
/* }}} */
@@ -386,6 +404,20 @@ static int fpm_socket_af_unix_listening_socket(struct fpm_worker_pool_s *wp) /*
386404
}
387405
/* }}} */
388406

407+
#ifdef SO_SETFIB
408+
static zend_result fpm_socket_setfib_init(void)
409+
{
410+
/* potentially up to 65536 but needs to check the actual cap beforehand */
411+
size_t len = sizeof(routemax);
412+
if (sysctlbyname("net.fibs", &routemax, &len, NULL, 0) < 0) {
413+
zlog(ZLOG_ERROR, "failed to get max routing table");
414+
return FAILURE;
415+
}
416+
417+
return SUCCESS;
418+
}
419+
#endif
420+
389421
int fpm_sockets_init_main() /* {{{ */
390422
{
391423
unsigned i, lq_len;
@@ -399,6 +431,12 @@ int fpm_sockets_init_main() /* {{{ */
399431
return -1;
400432
}
401433

434+
#ifdef SO_SETFIB
435+
if (fpm_socket_setfib_init() == FAILURE) {
436+
return -1;
437+
}
438+
#endif
439+
402440
/* import inherited sockets */
403441
for (i = 0; i < FPM_ENV_SOCKET_SET_MAX; i++) {
404442
fpm_sockets_get_env_name(envname, i);

sapi/fpm/fpm/fpm_sockets.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
#include <sys/types.h>
77
#include <sys/socket.h>
8+
#if defined(__FreeBSD__)
9+
#include <sys/sysctl.h>
10+
#endif
811
#include <sys/un.h>
912
#include <unistd.h>
1013
#include <fcntl.h>

sapi/fpm/tests/setsofib.phpt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
--TEST--
2+
FPM: listen.setfib`
3+
--SKIPIF--
4+
<?php
5+
include "skipif.inc";
6+
7+
if (!str_contains(PHP_OS, 'FreeBSD')) {
8+
die('skipped supported only on FreeBSD');
9+
}
10+
?>
11+
--FILE--
12+
<?php
13+
14+
require_once "tester.inc";
15+
16+
$cfg = <<<EOT
17+
[global]
18+
error_log = {{FILE:LOG}}
19+
pid = {{FILE:PID}}
20+
[setfib]
21+
listen = {{ADDR}}
22+
listen.setfib = 68000
23+
pm = dynamic
24+
pm.max_children = 5
25+
pm.start_servers = 2
26+
pm.min_spare_servers = 1
27+
pm.max_spare_servers = 3
28+
EOT;
29+
30+
$tester = new FPM\Tester($cfg);
31+
$tester->start();
32+
$tester->expectLogError('Invalid routing table id 68000, max is 1');
33+
$tester->terminate();
34+
$tester->close();
35+
36+
?>
37+
Done
38+
--EXPECT--
39+
Done
40+
--CLEAN--
41+
<?php
42+
require_once "tester.inc";
43+
FPM\Tester::clean();
44+
?>

sapi/fpm/www.conf.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ listen = 127.0.0.1:9000
6262
; Default Value: any
6363
;listen.allowed_clients = 127.0.0.1
6464

65+
; Set the associated the route table (FIB). FreeBSD only
66+
; Default Value: -1
67+
;listen.setfib = 1
68+
6569
; Specify the nice(2) priority to apply to the pool processes (only if set)
6670
; The value can vary from -19 (highest priority) to 20 (lower priority)
6771
; Note: - It will only work if the FPM master process is launched as root

0 commit comments

Comments
 (0)