2013-06-17 23:39:17 +02:00
|
|
|
/*
|
2019-02-22 16:57:28 +01:00
|
|
|
* Copyright (c) 2013-2019 Joris Vink <joris@coders.se>
|
2013-06-17 23:39:17 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2018-06-28 13:27:44 +02:00
|
|
|
#include <sys/param.h>
|
2013-06-17 23:39:17 +02:00
|
|
|
#include <sys/epoll.h>
|
|
|
|
#include <sys/prctl.h>
|
2018-06-28 13:27:44 +02:00
|
|
|
#include <sys/sendfile.h>
|
2013-06-17 23:39:17 +02:00
|
|
|
|
|
|
|
#include <sched.h>
|
|
|
|
|
|
|
|
#include "kore.h"
|
|
|
|
|
2014-03-30 23:54:35 +02:00
|
|
|
#if defined(KORE_USE_PGSQL)
|
2014-07-03 22:14:46 +02:00
|
|
|
#include "pgsql.h"
|
2014-03-30 23:54:35 +02:00
|
|
|
#endif
|
|
|
|
|
2014-06-28 16:17:18 +02:00
|
|
|
#if defined(KORE_USE_TASKS)
|
2014-07-03 22:14:46 +02:00
|
|
|
#include "tasks.h"
|
2014-06-28 16:17:18 +02:00
|
|
|
#endif
|
|
|
|
|
2013-06-17 23:39:17 +02:00
|
|
|
static int efd = -1;
|
2013-06-27 08:43:07 +02:00
|
|
|
static u_int32_t event_count = 0;
|
2013-06-17 23:39:17 +02:00
|
|
|
static struct epoll_event *events = NULL;
|
|
|
|
|
|
|
|
void
|
2013-06-24 09:36:40 +02:00
|
|
|
kore_platform_init(void)
|
2013-06-17 23:39:17 +02:00
|
|
|
{
|
2013-07-05 22:17:56 +02:00
|
|
|
long n;
|
|
|
|
|
|
|
|
if ((n = sysconf(_SC_NPROCESSORS_ONLN)) == -1) {
|
2013-06-17 23:39:17 +02:00
|
|
|
kore_debug("could not get number of cpu's falling back to 1");
|
|
|
|
cpu_count = 1;
|
2013-07-05 22:17:56 +02:00
|
|
|
} else {
|
|
|
|
cpu_count = (u_int16_t)n;
|
2013-06-17 23:39:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-06-26 11:18:32 +02:00
|
|
|
kore_platform_worker_setcpu(struct kore_worker *kw)
|
2013-06-17 23:39:17 +02:00
|
|
|
{
|
|
|
|
cpu_set_t cpuset;
|
|
|
|
|
|
|
|
CPU_ZERO(&cpuset);
|
|
|
|
CPU_SET(kw->cpu, &cpuset);
|
|
|
|
if (sched_setaffinity(0, sizeof(cpu_set_t), &cpuset) == -1) {
|
|
|
|
kore_debug("kore_worker_setcpu(): %s", errno_s);
|
|
|
|
} else {
|
|
|
|
kore_debug("kore_worker_setcpu(): worker %d on cpu %d",
|
|
|
|
kw->id, kw->cpu);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-06-26 11:18:32 +02:00
|
|
|
kore_platform_event_init(void)
|
2013-06-17 23:39:17 +02:00
|
|
|
{
|
2018-02-14 13:48:49 +01:00
|
|
|
if (efd != -1)
|
|
|
|
close(efd);
|
|
|
|
if (events != NULL)
|
|
|
|
kore_free(events);
|
|
|
|
|
2013-06-26 16:37:22 +02:00
|
|
|
if ((efd = epoll_create(10000)) == -1)
|
2013-06-17 23:39:17 +02:00
|
|
|
fatal("epoll_create(): %s", errno_s);
|
|
|
|
|
2013-07-27 20:56:15 +02:00
|
|
|
event_count = worker_max_connections + nlisteners;
|
2013-06-27 08:43:07 +02:00
|
|
|
events = kore_calloc(event_count, sizeof(struct epoll_event));
|
2013-06-17 23:39:17 +02:00
|
|
|
}
|
|
|
|
|
2015-12-29 19:39:39 +00:00
|
|
|
void
|
2016-01-04 21:40:14 +00:00
|
|
|
kore_platform_event_cleanup(void)
|
2015-12-29 19:39:39 +00:00
|
|
|
{
|
2016-02-01 20:02:02 +01:00
|
|
|
if (efd != -1) {
|
2015-12-29 19:39:39 +00:00
|
|
|
close(efd);
|
|
|
|
efd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (events != NULL) {
|
2016-07-12 13:54:14 +02:00
|
|
|
kore_free(events);
|
2015-12-29 19:39:39 +00:00
|
|
|
events = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-22 09:25:00 +01:00
|
|
|
void
|
2014-07-28 23:35:12 +02:00
|
|
|
kore_platform_event_wait(u_int64_t timer)
|
2013-06-17 23:39:17 +02:00
|
|
|
{
|
2014-10-22 21:16:49 +02:00
|
|
|
u_int32_t r;
|
2018-10-09 19:34:40 +02:00
|
|
|
struct kore_event *evt;
|
2019-03-21 10:17:08 +01:00
|
|
|
int n, i, timeo;
|
2013-06-17 23:39:17 +02:00
|
|
|
|
2019-03-21 10:17:08 +01:00
|
|
|
if (timer == KORE_WAIT_INFINITE)
|
|
|
|
timeo = -1;
|
|
|
|
else
|
|
|
|
timeo = timer;
|
|
|
|
|
|
|
|
n = epoll_wait(efd, events, event_count, timeo);
|
2013-06-17 23:39:17 +02:00
|
|
|
if (n == -1) {
|
|
|
|
if (errno == EINTR)
|
2018-12-22 09:25:00 +01:00
|
|
|
return;
|
2013-06-17 23:39:17 +02:00
|
|
|
fatal("epoll_wait(): %s", errno_s);
|
|
|
|
}
|
|
|
|
|
2015-01-19 15:31:36 +01:00
|
|
|
if (n > 0) {
|
2013-06-17 23:39:17 +02:00
|
|
|
kore_debug("main(): %d sockets available", n);
|
2015-01-19 15:31:36 +01:00
|
|
|
}
|
2013-06-17 23:39:17 +02:00
|
|
|
|
2014-10-22 21:16:49 +02:00
|
|
|
r = 0;
|
2013-06-17 23:39:17 +02:00
|
|
|
for (i = 0; i < n; i++) {
|
2013-07-27 20:56:15 +02:00
|
|
|
if (events[i].data.ptr == NULL)
|
|
|
|
fatal("events[%d].data.ptr == NULL", i);
|
|
|
|
|
2018-10-09 19:34:40 +02:00
|
|
|
r = 0;
|
|
|
|
evt = (struct kore_event *)events[i].data.ptr;
|
2013-06-17 23:39:17 +02:00
|
|
|
|
2018-10-09 19:34:40 +02:00
|
|
|
if (events[i].events & EPOLLIN)
|
|
|
|
evt->flags |= KORE_EVENT_READ;
|
2014-06-29 14:15:40 +02:00
|
|
|
|
2018-10-09 19:34:40 +02:00
|
|
|
if (events[i].events & EPOLLOUT)
|
|
|
|
evt->flags |= KORE_EVENT_WRITE;
|
2013-06-17 23:39:17 +02:00
|
|
|
|
2018-10-10 14:33:26 +02:00
|
|
|
if (events[i].events & EPOLLERR ||
|
|
|
|
events[i].events & EPOLLHUP ||
|
|
|
|
events[i].events & EPOLLRDHUP)
|
2018-10-09 19:34:40 +02:00
|
|
|
r = 1;
|
|
|
|
|
|
|
|
evt->handle(events[i].data.ptr, r);
|
2013-06-17 23:39:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-17 08:25:45 +02:00
|
|
|
void
|
|
|
|
kore_platform_event_all(int fd, void *c)
|
|
|
|
{
|
|
|
|
kore_platform_event_schedule(fd,
|
|
|
|
EPOLLIN | EPOLLOUT | EPOLLRDHUP | EPOLLET, 0, c);
|
|
|
|
}
|
|
|
|
|
2013-06-17 23:39:17 +02:00
|
|
|
void
|
2013-06-26 11:18:32 +02:00
|
|
|
kore_platform_event_schedule(int fd, int type, int flags, void *udata)
|
2013-06-17 23:39:17 +02:00
|
|
|
{
|
|
|
|
struct epoll_event evt;
|
|
|
|
|
2014-10-22 21:16:49 +02:00
|
|
|
kore_debug("kore_platform_event_schedule(%d, %d, %d, %p)",
|
2013-06-26 11:18:32 +02:00
|
|
|
fd, type, flags, udata);
|
2013-06-17 23:39:17 +02:00
|
|
|
|
|
|
|
evt.events = type;
|
|
|
|
evt.data.ptr = udata;
|
|
|
|
if (epoll_ctl(efd, EPOLL_CTL_ADD, fd, &evt) == -1) {
|
|
|
|
if (errno == EEXIST) {
|
|
|
|
if (epoll_ctl(efd, EPOLL_CTL_MOD, fd, &evt) == -1)
|
|
|
|
fatal("epoll_ctl() MOD: %s", errno_s);
|
|
|
|
} else {
|
|
|
|
fatal("epoll_ctl() ADD: %s", errno_s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-30 23:54:35 +02:00
|
|
|
void
|
|
|
|
kore_platform_schedule_read(int fd, void *data)
|
|
|
|
{
|
2018-08-01 12:17:16 +02:00
|
|
|
kore_platform_event_schedule(fd, EPOLLIN | EPOLLET, 0, data);
|
2014-03-30 23:54:35 +02:00
|
|
|
}
|
|
|
|
|
2015-12-09 21:29:44 +01:00
|
|
|
void
|
|
|
|
kore_platform_schedule_write(int fd, void *data)
|
|
|
|
{
|
2018-08-01 12:17:16 +02:00
|
|
|
kore_platform_event_schedule(fd, EPOLLOUT | EPOLLET, 0, data);
|
2015-12-09 21:29:44 +01:00
|
|
|
}
|
|
|
|
|
2014-03-30 23:54:35 +02:00
|
|
|
void
|
|
|
|
kore_platform_disable_read(int fd)
|
|
|
|
{
|
|
|
|
if (epoll_ctl(efd, EPOLL_CTL_DEL, fd, NULL) == -1)
|
|
|
|
fatal("kore_platform_disable_read: %s", errno_s);
|
|
|
|
}
|
|
|
|
|
2013-06-26 16:37:22 +02:00
|
|
|
void
|
|
|
|
kore_platform_enable_accept(void)
|
|
|
|
{
|
2013-07-27 20:56:15 +02:00
|
|
|
struct listener *l;
|
|
|
|
|
2014-10-22 21:16:49 +02:00
|
|
|
kore_debug("kore_platform_enable_accept()");
|
|
|
|
|
2013-07-27 20:56:15 +02:00
|
|
|
LIST_FOREACH(l, &listeners, list)
|
|
|
|
kore_platform_event_schedule(l->fd, EPOLLIN, 0, l);
|
2013-06-26 16:37:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
kore_platform_disable_accept(void)
|
|
|
|
{
|
2013-07-27 20:56:15 +02:00
|
|
|
struct listener *l;
|
|
|
|
|
2014-10-22 21:16:49 +02:00
|
|
|
kore_debug("kore_platform_disable_accept()");
|
|
|
|
|
2013-07-27 20:56:15 +02:00
|
|
|
LIST_FOREACH(l, &listeners, list) {
|
|
|
|
if (epoll_ctl(efd, EPOLL_CTL_DEL, l->fd, NULL) == -1)
|
|
|
|
fatal("kore_platform_disable_accept: %s", errno_s);
|
|
|
|
}
|
2013-06-26 16:37:22 +02:00
|
|
|
}
|
|
|
|
|
2013-06-17 23:39:17 +02:00
|
|
|
void
|
2013-06-26 11:18:32 +02:00
|
|
|
kore_platform_proctitle(char *title)
|
2013-06-17 23:39:17 +02:00
|
|
|
{
|
2015-01-19 15:31:36 +01:00
|
|
|
if (prctl(PR_SET_NAME, title) == -1) {
|
2013-06-17 23:39:17 +02:00
|
|
|
kore_debug("prctl(): %s", errno_s);
|
2015-01-19 15:31:36 +01:00
|
|
|
}
|
2013-06-17 23:39:17 +02:00
|
|
|
}
|
2018-06-28 13:27:44 +02:00
|
|
|
|
|
|
|
#if defined(KORE_USE_PLATFORM_SENDFILE)
|
|
|
|
int
|
|
|
|
kore_platform_sendfile(struct connection *c, struct netbuf *nb)
|
|
|
|
{
|
|
|
|
off_t smin;
|
2018-07-09 10:00:38 +00:00
|
|
|
ssize_t sent;
|
|
|
|
size_t len, prevoff;
|
2018-06-28 13:27:44 +02:00
|
|
|
|
2018-07-09 10:00:38 +00:00
|
|
|
prevoff = nb->fd_off;
|
2018-06-28 13:27:44 +02:00
|
|
|
smin = nb->fd_len - nb->fd_off;
|
|
|
|
len = MIN(SENDFILE_PAYLOAD_MAX, smin);
|
|
|
|
|
2018-07-09 10:00:38 +00:00
|
|
|
resend:
|
2018-06-29 03:10:28 +00:00
|
|
|
sent = sendfile(c->fd, nb->file_ref->fd, &nb->fd_off, len);
|
|
|
|
if (sent == -1) {
|
2018-06-28 13:27:44 +02:00
|
|
|
if (errno == EAGAIN) {
|
2018-10-09 19:34:40 +02:00
|
|
|
c->evt.flags &= ~KORE_EVENT_WRITE;
|
2018-06-28 13:27:44 +02:00
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
2018-07-09 10:00:38 +00:00
|
|
|
if (nb->fd_off - prevoff != (size_t)len)
|
|
|
|
goto resend;
|
|
|
|
|
2018-06-28 13:27:44 +02:00
|
|
|
if (sent == 0 || nb->fd_off == nb->fd_len) {
|
2018-12-22 09:25:00 +01:00
|
|
|
net_remove_netbuf(c, nb);
|
2018-06-28 13:27:44 +02:00
|
|
|
c->snb = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
#endif
|