kore/src/bsd.c

332 lines
6.5 KiB
C
Raw Permalink Normal View History

/*
2022-01-31 22:02:06 +01:00
* Copyright (c) 2013-2022 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;
static int scheduled = 0;
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)
2019-11-03 19:52:47 +01:00
static char pledges[256] = { "stdio rpath inet" };
#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) {
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)
{
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));
}
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);
}
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
kore_platform_event_level_all(int fd, void *c)
{
kore_platform_event_schedule(fd, EVFILT_READ, EV_ADD, c);
kore_platform_event_schedule(fd, EVFILT_WRITE, EV_ADD, c);
}
void
kore_platform_event_level_read(int fd, void *c)
{
kore_platform_event_schedule(fd, EVFILT_READ, EV_ADD, 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 && errno != ENOENT)
fatal("kevent: %s", errno_s);
}
void
kore_platform_enable_accept(void)
{
struct listener *l;
struct kore_server *srv;
int flags;
if (scheduled == 0) {
scheduled = 1;
flags = EV_ADD | EV_ENABLE;
} else {
flags = EV_ENABLE;
}
LIST_FOREACH(srv, &kore_servers, list) {
LIST_FOREACH(l, &srv->listeners, list) {
kore_platform_event_schedule(l->fd,
EVFILT_READ, flags, l);
}
}
}
void
kore_platform_disable_accept(void)
{
struct listener *l;
struct kore_server *srv;
LIST_FOREACH(srv, &kore_servers, list) {
LIST_FOREACH(l, &srv->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
Add acmev2 (RFC8555) support to Kore. A new acme process is created that communicates with the acme servers. This process does not hold any of your private keys (no account keys, no domain keys etc). Whenever the acme process requires a signed payload it will ask the keymgr process to do the signing with the relevant keys. This process is also sandboxed with pledge+unveil on OpenBSD and seccomp syscall filtering on Linux. The implementation only supports the tls-alpn-01 challenge. This means that you do not need to open additional ports on your machine. http-01 and dns-01 are currently not supported (no wildcard support). A new configuration option "acme_provider" is available and can be set to the acme server its directory. By default this will point to the live letsencrypt environment: https://acme-v02.api.letsencrypt.org/directory The acme process can be controlled via the following config options: - acme_root (where the acme process will chroot/chdir into). - acme_runas (the user the acme process will run as). If none are set, the values from 'root' and 'runas' are taken. If you want to turn on acme for domains you do it as follows: domain kore.io { acme yes } You do not need to specify certkey/certfile anymore, if they are present still they will be overwritten by the acme system. The keymgr will store all certificates and keys under its root (keymgr_root), the account key is stored as "/account-key.pem" and all obtained certificates go under "certificates/<domain>/fullchain.pem" while keys go under "certificates/<domain>/key.pem". Kore will automatically renew certificates if they will expire in 7 days or less.
2019-11-06 19:33:53 +01:00
kore_platform_proctitle(const 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
}
2023-01-04 11:48:19 +01:00
u_int32_t
kore_platform_random_uint32(void)
{
return (arc4random());
}
#if defined(KORE_USE_PLATFORM_PLEDGE)
void
kore_platform_pledge(void)
{
Add acmev2 (RFC8555) support to Kore. A new acme process is created that communicates with the acme servers. This process does not hold any of your private keys (no account keys, no domain keys etc). Whenever the acme process requires a signed payload it will ask the keymgr process to do the signing with the relevant keys. This process is also sandboxed with pledge+unveil on OpenBSD and seccomp syscall filtering on Linux. The implementation only supports the tls-alpn-01 challenge. This means that you do not need to open additional ports on your machine. http-01 and dns-01 are currently not supported (no wildcard support). A new configuration option "acme_provider" is available and can be set to the acme server its directory. By default this will point to the live letsencrypt environment: https://acme-v02.api.letsencrypt.org/directory The acme process can be controlled via the following config options: - acme_root (where the acme process will chroot/chdir into). - acme_runas (the user the acme process will run as). If none are set, the values from 'root' and 'runas' are taken. If you want to turn on acme for domains you do it as follows: domain kore.io { acme yes } You do not need to specify certkey/certfile anymore, if they are present still they will be overwritten by the acme system. The keymgr will store all certificates and keys under its root (keymgr_root), the account key is stored as "/account-key.pem" and all obtained certificates go under "certificates/<domain>/fullchain.pem" while keys go under "certificates/<domain>/key.pem". Kore will automatically renew certificates if they will expire in 7 days or less.
2019-11-06 19:33:53 +01:00
if (worker->id == KORE_WORKER_KEYMGR || worker->id == KORE_WORKER_ACME)
return;
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