kore/src/bsd.c

305 lines
6.1 KiB
C
Raw Normal View History

/*
2019-02-22 16:57:28 +01:00
* Copyright (c) 2013-2019 Joris Vink <joris@coders.se>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/event.h>
#include <sys/sysctl.h>
#include <sys/socket.h>
#include <sys/uio.h>
#if defined(__FreeBSD_version)
#include <sys/cpuset.h>
#endif
2016-01-22 12:08:13 +01:00
#include <errno.h>
#include <string.h>
#include "kore.h"
2014-04-02 00:06:24 +02:00
#if defined(KORE_USE_PGSQL)
2014-07-03 22:14:46 +02:00
#include "pgsql.h"
2014-04-02 00:06:24 +02:00
#endif
2014-06-30 14:43:34 +02:00
#if defined(KORE_USE_TASKS)
2014-07-03 22:14:46 +02:00
#include "tasks.h"
2014-06-30 14:43:34 +02:00
#endif
static int kfd = -1;
Rework HTTP and worker processes. The HTTP layer used to make a copy of each incoming header and its value for a request. Stop doing that and make HTTP headers zero-copy all across the board. This change comes with some api function changes, notably the http_request_header() function which now takes a const char ** rather than a char ** out pointer. This commit also constifies several members of http_request, beware. Additional rework how the worker processes deal with the accept lock. Before: if a worker held the accept lock and it accepted a new connection it would release the lock for others and back off for 500ms before attempting to grab the lock again. This approach worked but under high load this starts becoming obvious. Now: - workers not holding the accept lock and not having any connections will wait less long before returning from kore_platform_event_wait(). - workers not holding the accept lock will no longer blindly wait an arbitrary amount in kore_platform_event_wait() but will look at how long until the next lock grab is and base their timeout on that. - if a worker its next_lock timeout is up and failed to grab the lock it will try again in half the time again. - the worker process holding the lock will when releasing the lock double check if it still has space for newer connections, if it does it will keep the lock until it is full. This prevents the lock from bouncing between several non busy worker processes all the time. Additional fixes: - Reduce the number of times we check the timeout list, only do it twice per second rather then every event tick. - Fix solo worker count for TLS (we actually hold two processes, not one). - Make sure we don't accidentally miscalculate the idle time causing new connections under heavy load to instantly drop. - Swap from gettimeofday() to clock_gettime() now that MacOS caught up.
2018-02-14 13:48:49 +01:00
static struct kevent *events = NULL;
2013-06-27 12:37:14 +02:00
static u_int32_t event_count = 0;
#if defined(KORE_USE_PLATFORM_PLEDGE)
static char pledges[256] = { "stdio rpath inet error" };
#endif
void
2013-06-24 09:36:40 +02:00
kore_platform_init(void)
{
long n;
size_t len = sizeof(n);
int mib[] = { CTL_HW, HW_NCPU };
if (sysctl(mib, 2, &n, &len, NULL, 0) == -1) {
kore_debug("kore_platform_init(): sysctl %s", errno_s);
cpu_count = 1;
} else {
cpu_count = (u_int16_t)n;
}
}
void
2013-06-26 11:18:32 +02:00
kore_platform_worker_setcpu(struct kore_worker *kw)
{
#if defined(__FreeBSD_version)
cpuset_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(kw->cpu, &cpuset);
if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID,
-1, sizeof(cpuset), &cpuset) == -1) {
fatal("failed: %s", errno_s);
}
#endif /* __FreeBSD_version */
}
void
2013-06-26 11:18:32 +02:00
kore_platform_event_init(void)
{
struct listener *l;
Rework HTTP and worker processes. The HTTP layer used to make a copy of each incoming header and its value for a request. Stop doing that and make HTTP headers zero-copy all across the board. This change comes with some api function changes, notably the http_request_header() function which now takes a const char ** rather than a char ** out pointer. This commit also constifies several members of http_request, beware. Additional rework how the worker processes deal with the accept lock. Before: if a worker held the accept lock and it accepted a new connection it would release the lock for others and back off for 500ms before attempting to grab the lock again. This approach worked but under high load this starts becoming obvious. Now: - workers not holding the accept lock and not having any connections will wait less long before returning from kore_platform_event_wait(). - workers not holding the accept lock will no longer blindly wait an arbitrary amount in kore_platform_event_wait() but will look at how long until the next lock grab is and base their timeout on that. - if a worker its next_lock timeout is up and failed to grab the lock it will try again in half the time again. - the worker process holding the lock will when releasing the lock double check if it still has space for newer connections, if it does it will keep the lock until it is full. This prevents the lock from bouncing between several non busy worker processes all the time. Additional fixes: - Reduce the number of times we check the timeout list, only do it twice per second rather then every event tick. - Fix solo worker count for TLS (we actually hold two processes, not one). - Make sure we don't accidentally miscalculate the idle time causing new connections under heavy load to instantly drop. - Swap from gettimeofday() to clock_gettime() now that MacOS caught up.
2018-02-14 13:48:49 +01:00
if (kfd != -1)
close(kfd);
if (events != NULL)
kore_free(events);
if ((kfd = kqueue()) == -1)
fatal("kqueue(): %s", errno_s);
event_count = (worker_max_connections * 2) + nlisteners;
2013-06-27 12:37:14 +02:00
events = kore_calloc(event_count, sizeof(struct kevent));
/* Hack to check if we're running under the parent or not. */
if (worker != NULL) {
LIST_FOREACH(l, &listeners, list) {
kore_platform_event_schedule(l->fd,
EVFILT_READ, EV_ADD | EV_DISABLE, l);
}
}
}
void
kore_platform_event_cleanup(void)
{
if (kfd != -1) {
close(kfd);
kfd = -1;
}
if (events != NULL) {
kore_free(events);
events = NULL;
}
}
void
2014-07-28 23:35:12 +02:00
kore_platform_event_wait(u_int64_t timer)
{
u_int32_t r;
struct kore_event *evt;
int n, i;
struct timespec timeo, *ts;
if (timer == KORE_WAIT_INFINITE) {
ts = NULL;
} else {
timeo.tv_sec = timer / 1000;
timeo.tv_nsec = (timer % 1000) * 1000000;
ts = &timeo;
}
n = kevent(kfd, NULL, 0, events, event_count, ts);
if (n == -1) {
if (errno == EINTR)
return;
fatal("kevent(): %s", errno_s);
}
if (n > 0)
kore_debug("main(): %d sockets available", n);
for (i = 0; i < n; i++) {
evt = (struct kore_event *)events[i].udata;
if (evt == NULL)
fatal("evt == NULL");
r = 0;
if (events[i].filter == EVFILT_READ)
evt->flags |= KORE_EVENT_READ;
2014-04-02 00:06:24 +02:00
if (events[i].filter == EVFILT_WRITE)
evt->flags |= KORE_EVENT_WRITE;
if (events[i].flags & EV_EOF || events[i].flags & EV_ERROR)
r = 1;
evt->handle(evt, r);
}
}
void
kore_platform_event_all(int fd, void *c)
{
kore_platform_event_schedule(fd, EVFILT_READ, EV_ADD | EV_CLEAR, c);
kore_platform_event_schedule(fd, EVFILT_WRITE, EV_ADD | EV_CLEAR, c);
}
void
2013-06-26 11:18:32 +02:00
kore_platform_event_schedule(int fd, int type, int flags, void *data)
{
struct kevent event[1];
EV_SET(&event[0], fd, type, flags, 0, 0, data);
if (kevent(kfd, event, 1, NULL, 0, NULL) == -1)
fatal("kevent: %s", errno_s);
}
void
kore_platform_enable_accept(void)
{
struct listener *l;
LIST_FOREACH(l, &listeners, list)
kore_platform_event_schedule(l->fd, EVFILT_READ, EV_ENABLE, l);
}
void
kore_platform_disable_accept(void)
{
struct listener *l;
LIST_FOREACH(l, &listeners, list)
kore_platform_event_schedule(l->fd, EVFILT_READ, EV_DISABLE, l);
}
2014-04-02 00:06:24 +02:00
void
kore_platform_schedule_read(int fd, void *data)
{
kore_platform_event_schedule(fd, EVFILT_READ, EV_ADD | EV_CLEAR, data);
2014-04-02 00:06:24 +02:00
}
2015-12-09 21:29:44 +01:00
void
kore_platform_schedule_write(int fd, void *data)
{
kore_platform_event_schedule(fd, EVFILT_WRITE, EV_ADD | EV_CLEAR, data);
2015-12-09 21:29:44 +01:00
}
2014-04-02 00:06:24 +02:00
void
kore_platform_disable_read(int fd)
{
kore_platform_event_schedule(fd, EVFILT_READ, EV_DELETE, NULL);
}
void
kore_platform_disable_write(int fd)
{
kore_platform_event_schedule(fd, EVFILT_WRITE, EV_DELETE, NULL);
}
void
2013-06-26 11:18:32 +02:00
kore_platform_proctitle(char *title)
{
#ifdef __MACH__
kore_proctitle(title);
#else
setproctitle("%s", title);
#endif
}
#if defined(KORE_USE_PLATFORM_SENDFILE)
int
kore_platform_sendfile(struct connection *c, struct netbuf *nb)
{
int ret;
off_t len, smin;
smin = nb->fd_len - nb->fd_off;
len = MIN(SENDFILE_PAYLOAD_MAX, smin);
#if defined(__MACH__)
ret = sendfile(nb->file_ref->fd, c->fd, nb->fd_off, &len, NULL, 0);
#else
ret = sendfile(nb->file_ref->fd, c->fd, nb->fd_off, len, NULL, &len, 0);
#endif
if (ret == -1) {
if (errno == EAGAIN) {
nb->fd_off += len;
c->evt.flags &= ~KORE_EVENT_WRITE;
return (KORE_RESULT_OK);
}
if (errno == EINTR) {
nb->fd_off += len;
return (KORE_RESULT_OK);
}
return (KORE_RESULT_ERROR);
}
nb->fd_off += len;
if (len == 0 || nb->fd_off == nb->fd_len) {
net_remove_netbuf(c, nb);
c->snb = NULL;
}
return (KORE_RESULT_OK);
}
#endif
void
kore_platform_sandbox(void)
{
#if defined(KORE_USE_PLATFORM_PLEDGE)
kore_platform_pledge();
#endif
}
#if defined(KORE_USE_PLATFORM_PLEDGE)
void
kore_platform_pledge(void)
{
if (pledge(pledges, NULL) == -1)
fatal("failed to pledge process");
}
void
kore_platform_add_pledge(const char *pledge)
{
size_t len;
len = strlcat(pledges, " ", sizeof(pledges));
if (len >= sizeof(pledges))
fatal("truncation on pledges");
len = strlcat(pledges, pledge, sizeof(pledges));
if (len >= sizeof(pledges))
fatal("truncation on pledges (%s)", pledge);
}
#endif