Skip to content

Commit 925bb73

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.
1 parent 9e74e58 commit 925bb73

File tree

6 files changed

+77
-0
lines changed

6 files changed

+77
-0
lines changed

sapi/fpm/config.m4

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,23 @@ AC_DEFUN([AC_FPM_TIMES],
396396
])
397397
])
398398

399+
AC_DEFUN([AC_FPM_SOCKETROUTE],
400+
[
401+
have_socketroute=no
402+
AC_MSG_CHECKING([for SO_SETFIB])
403+
404+
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/socket.h>]], [[int x = SO_SETFIB;]])], [
405+
have_socketroute=yes
406+
AC_MSG_RESULT([yes])
407+
], [
408+
AC_MSG_RESULT([no])
409+
])
410+
411+
if test $have_socketroute = "yes"; then
412+
AC_DEFINE([HAVE_SOCKETROUTE], 1, [do we have socket route capabilities])
413+
fi
414+
])
415+
399416
AC_DEFUN([AC_FPM_KQUEUE],
400417
[
401418
AC_MSG_CHECKING([for kqueue])
@@ -539,6 +556,7 @@ if test "$PHP_FPM" != "no"; then
539556
AC_FPM_LQ
540557
AC_FPM_SYSCONF
541558
AC_FPM_TIMES
559+
AC_FPM_SOCKETROUTE
542560
AC_FPM_KQUEUE
543561
AC_FPM_PORT
544562
AC_FPM_DEVPOLL

sapi/fpm/fpm/fpm_conf.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ struct fpm_global_config_s fpm_global_config = {
7070
#ifdef HAVE_SYSTEMD
7171
.systemd_watchdog = 0,
7272
.systemd_interval = -1, /* -1 means not set */
73+
#endif
74+
#ifdef HAVE_SOCKETROUTE
75+
.socketroute = -1, /* -1 means not set */
7376
#endif
7477
.log_buffering = ZLOG_DEFAULT_BUFFERING,
7578
.log_limit = ZLOG_DEFAULT_LIMIT
@@ -104,6 +107,9 @@ static struct ini_value_parser_s ini_fpm_global_options[] = {
104107
{ "events.mechanism", &fpm_conf_set_string, GO(events_mechanism) },
105108
#ifdef HAVE_SYSTEMD
106109
{ "systemd_interval", &fpm_conf_set_time, GO(systemd_interval) },
110+
#endif
111+
#ifdef HAVE_SOCKETROUTE
112+
{ "socket.route", &fpm_conf_set_integer, GO(socketroute) },
107113
#endif
108114
{ 0, 0, 0 }
109115
};

sapi/fpm/fpm/fpm_conf.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ struct fpm_global_config_s {
4444
int systemd_watchdog;
4545
int systemd_interval;
4646
#endif
47+
#ifdef HAVE_SOCKETROUTE
48+
int socketroute;
49+
#endif
4750
};
4851

4952
extern struct fpm_global_config_s fpm_global_config;

sapi/fpm/fpm/fpm_sockets.c

Lines changed: 43 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,22 @@ static int fpm_sockets_new_listening_socket(struct fpm_worker_pool_s *wp, struct
250254
return -1;
251255
}
252256

257+
#ifdef HAVE_SOCKETROUTE
258+
if (-1 < fpm_global_config.socketroute) {
259+
#ifdef SO_SETFIB
260+
if (routemax < fpm_global_config.socketroute) {
261+
zlog(ZLOG_ERROR, "Invalid routing table id %d, max is %d", fpm_global_config.socketroute, routemax);
262+
close(sock);
263+
return -1;
264+
}
265+
266+
if (0 > setsockopt(sock, SOL_SOCKET, SO_SETFIB, &fpm_global_config.socketroute, sizeof(fpm_global_config.socketroute))) {
267+
zlog(ZLOG_WARNING, "failed to change socket attribute");
268+
}
269+
#endif
270+
}
271+
#endif
272+
253273
return sock;
254274
}
255275
/* }}} */
@@ -386,6 +406,23 @@ static int fpm_socket_af_unix_listening_socket(struct fpm_worker_pool_s *wp) /*
386406
}
387407
/* }}} */
388408

409+
#ifdef HAVE_SOCKETROUTE
410+
static zend_result fpm_socket_setroute_init(void) /* {{{ */
411+
{
412+
#ifdef SO_SETFIB
413+
/* potentially up to 65536 but needs to check the actual cap beforehand */
414+
size_t len = sizeof(routemax);
415+
if (sysctlbyname("net.fibs", &routemax, &len, NULL, 0) < 0) {
416+
zlog(ZLOG_ERROR, "failed to get max routing table");
417+
return FAILURE;
418+
}
419+
420+
return SUCCESS;
421+
#endif
422+
}
423+
/* }}} */
424+
#endif
425+
389426
int fpm_sockets_init_main() /* {{{ */
390427
{
391428
unsigned i, lq_len;
@@ -399,6 +436,12 @@ int fpm_sockets_init_main() /* {{{ */
399436
return -1;
400437
}
401438

439+
#ifdef HAVE_SOCKETROUTE
440+
if (fpm_socket_setroute_init() == FAILURE) {
441+
return -1;
442+
}
443+
#endif
444+
402445
/* import inherited sockets */
403446
for (i = 0; i < FPM_ENV_SOCKET_SET_MAX; i++) {
404447
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/php-fpm.conf.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@
125125
; Default value: 10
126126
;systemd_interval = 10
127127

128+
; Sets the routing table for the sockets created within FPM
129+
; - FreeBSD from 0 up to 65536
130+
;socket.route = -1
131+
128132
;;;;;;;;;;;;;;;;;;;;
129133
; Pool Definitions ;
130134
;;;;;;;;;;;;;;;;;;;;

0 commit comments

Comments
 (0)