Nginx : add a creation flag SOCK_FSTACK(create-fstack-socket) for socket()

1. `#define SOCK_FSTACK 0x1000`
2. when we want to create socket by fstack, we code like this :
`s = ngx_socket(domain, type | SOCK_FSTACK , protocol);`
This commit is contained in:
chenwei 2018-02-24 16:45:10 +08:00
parent edbcaf6eef
commit 76e16b226f
4 changed files with 28 additions and 19 deletions

View File

@ -58,6 +58,7 @@ fi
if [ $USE_FSTACK = YES ]; then
have=NGX_HAVE_FSTACK . auto/have
have=NGX_HAVE_FSTACK . auto/have_headers
have=SOCK_FSTACK value=0x1000 . auto/define
CORE_SRCS="$CORE_SRCS $KQUEUE_SRCS"
EVENT_MODULES="$EVENT_MODULES $KQUEUE_MODULE"
fi

View File

@ -15,7 +15,7 @@ extern int fstack_territory(int domain, int type, int protocol);
extern int is_fstack_fd(int sockfd);
static ngx_inline int
ngx_ff_skip_listening_socket(ngx_cycle_t *cycle, const ngx_listening_t *ls)
ngx_ff_skip_listening_socket(ngx_cycle_t *cycle, ngx_listening_t *ls)
{
if (ngx_process <= NGX_PROCESS_MASTER) {
@ -36,6 +36,8 @@ ngx_ff_skip_listening_socket(ngx_cycle_t *cycle, const ngx_listening_t *ls)
if (!fstack_territory(ls->sockaddr->sa_family, ls->type, 0)) {
return 1;
}
ls->type |= SOCK_FSTACK;
} else {
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
"unexpected process type: %d, ignored",

View File

@ -72,6 +72,7 @@
#include <arpa/inet.h>
#include <sys/time.h>
#include <ngx_auto_config.h>
#include "ff_api.h"
#define _GNU_SOURCE
@ -208,11 +209,16 @@ ff_mod_init(const char *conf, int proc_id, int proc_type) {
int
fstack_territory(int domain, int type, int protocol)
{
if ((AF_INET != domain) || (SOCK_STREAM != type && SOCK_DGRAM != type)) {
return 0;
}
/* Remove creation flags */
type &= ~SOCK_CLOEXEC;
type &= ~SOCK_NONBLOCK;
type &= ~SOCK_FSTACK;
return 1;
if ((AF_INET != domain) || (SOCK_STREAM != type && SOCK_DGRAM != type)) {
return 0;
}
return 1;
}
int
@ -223,10 +229,15 @@ socket(int domain, int type, int protocol)
return SYSCALL(socket)(domain, type, protocol);
}
if ((AF_INET != domain) || (SOCK_STREAM != type && SOCK_DGRAM != type)) {
if (unlikely(fstack_territory(domain, type, protocol) == 0)) {
return SYSCALL(socket)(domain, type, protocol);
}
if (unlikely((type & SOCK_FSTACK) == 0)) {
return SYSCALL(socket)(domain, type, protocol);
}
type &= ~SOCK_FSTACK;
sock = ff_socket(domain, type, protocol);
if (sock != -1) {

View File

@ -39,23 +39,18 @@ ngx_event_connect_peer(ngx_peer_connection_t *pc)
type = (pc->type ? pc->type : SOCK_STREAM);
#if (NGX_HAVE_FSTACK)
/*
We need to explicitly call the needed socket() function
to create socket!
/*
We use a creation flags created by fstack's adaptable layer to
to explicitly call the needed socket() function.
*/
static int (*real_socket)(int, int, int);
if (pc->belong_to_host) {
if (!real_socket) {
real_socket = dlsym(RTLD_NEXT, "socket");
}
s = real_socket(pc->sockaddr->sa_family, type, 0);
} else {
s = ngx_socket(pc->sockaddr->sa_family, type, 0);
if (!pc->belong_to_host) {
type |= SOCK_FSTACK;
}
#else
s = ngx_socket(pc->sockaddr->sa_family, type, 0);
#endif
s = ngx_socket(pc->sockaddr->sa_family, type, 0);
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, pc->log, 0, "%s socket %d",
(type == SOCK_STREAM) ? "stream" : "dgram", s);