2010-01-26 22:30:32 +00:00
|
|
|
/*
|
|
|
|
* uhub - A tiny ADC p2p connection hub
|
2014-05-14 09:38:08 +00:00
|
|
|
* Copyright (C) 2007-2014, Jan Vidar Krey
|
2010-01-26 22:30:32 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2019-03-11 23:22:38 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2010-01-26 22:30:32 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "uhub.h"
|
|
|
|
|
|
|
|
#ifdef USE_KQUEUE
|
|
|
|
|
|
|
|
#include "network/connection.h"
|
|
|
|
#include "network/common.h"
|
|
|
|
#include "network/backend.h"
|
|
|
|
|
|
|
|
#define KQUEUE_EVBUFFER 512
|
|
|
|
|
|
|
|
struct net_connection_kqueue
|
|
|
|
{
|
|
|
|
NET_CON_STRUCT_COMMON
|
2010-04-22 14:10:17 +00:00
|
|
|
struct kevent ev_r;
|
|
|
|
struct kevent ev_w;
|
2010-05-28 13:22:33 +00:00
|
|
|
int change;
|
2010-01-26 22:30:32 +00:00
|
|
|
};
|
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
struct net_backend_kqueue
|
2010-01-26 22:30:32 +00:00
|
|
|
{
|
|
|
|
int kqfd;
|
2010-01-27 16:50:02 +00:00
|
|
|
struct net_connection_kqueue** conns;
|
2010-05-28 13:22:33 +00:00
|
|
|
struct kevent* changes;
|
|
|
|
int* change_list;
|
|
|
|
size_t change_list_len;
|
2010-01-26 22:30:32 +00:00
|
|
|
struct kevent events[KQUEUE_EVBUFFER];
|
2010-02-10 20:57:27 +00:00
|
|
|
struct net_backend_common* common;
|
2010-01-26 22:30:32 +00:00
|
|
|
};
|
|
|
|
|
2010-05-28 13:22:33 +00:00
|
|
|
#define CHANGE_ACTION_ADD 0x0001
|
|
|
|
#define CHANGE_ACTION_MOD 0x0002
|
|
|
|
#define CHANGE_ACTION_DEL 0x0004
|
|
|
|
#define CHANGE_OP_WANT_READ 0x0100
|
|
|
|
#define CHANGE_OP_WANT_WRITE 0x0200
|
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
static void net_backend_set_handlers(struct net_backend_handler* handler);
|
2010-05-28 13:22:33 +00:00
|
|
|
static void add_change(struct net_backend_kqueue* backend, struct net_connection_kqueue* con, int actions);
|
|
|
|
static size_t create_change_list(struct net_backend_kqueue* backend);
|
2010-01-26 22:30:32 +00:00
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
const char* net_backend_name_kqueue()
|
|
|
|
{
|
|
|
|
return "kqueue";
|
|
|
|
}
|
2010-01-26 22:30:32 +00:00
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
int net_backend_poll_kqueue(struct net_backend* data, int ms)
|
2010-01-26 22:30:32 +00:00
|
|
|
{
|
2010-02-10 20:57:27 +00:00
|
|
|
int res;
|
|
|
|
struct timespec tspec = { 0, };
|
|
|
|
struct net_backend_kqueue* backend = (struct net_backend_kqueue*) data;
|
2010-05-28 13:22:33 +00:00
|
|
|
size_t changes;
|
2010-01-26 22:30:32 +00:00
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
tspec.tv_sec = (ms / 1000);
|
2010-04-22 14:10:17 +00:00
|
|
|
tspec.tv_nsec = ((ms % 1000) * 1000000);
|
2010-01-26 22:30:32 +00:00
|
|
|
|
2010-05-28 13:22:33 +00:00
|
|
|
changes = create_change_list(backend);
|
|
|
|
res = kevent(backend->kqfd, backend->changes, changes, backend->events, KQUEUE_EVBUFFER, &tspec);
|
2010-01-26 22:30:32 +00:00
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
if (res == -1 && errno == EINTR)
|
|
|
|
return 0;
|
|
|
|
return res;
|
2010-01-26 22:30:32 +00:00
|
|
|
}
|
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
void net_backend_process_kqueue(struct net_backend* data, int res)
|
2010-01-26 22:30:32 +00:00
|
|
|
{
|
2010-01-27 16:50:02 +00:00
|
|
|
int n;
|
2010-02-10 20:57:27 +00:00
|
|
|
struct net_backend_kqueue* backend = (struct net_backend_kqueue*) data;
|
2010-01-27 16:50:02 +00:00
|
|
|
|
|
|
|
for (n = 0; n < res; n++)
|
|
|
|
{
|
2010-02-10 20:57:27 +00:00
|
|
|
struct net_connection_kqueue* con = (struct net_connection_kqueue*) backend->events[n].udata;
|
2010-05-28 13:22:33 +00:00
|
|
|
if (con && con->sd >= 0 && backend->conns[con->sd])
|
|
|
|
{
|
|
|
|
int ev = 0;
|
|
|
|
if (backend->events[n].filter == EVFILT_READ) ev = NET_EVENT_READ;
|
|
|
|
else if (backend->events[n].filter == EVFILT_WRITE) ev = NET_EVENT_WRITE;
|
2010-04-22 14:10:17 +00:00
|
|
|
net_con_callback((struct net_connection*) con, ev);
|
2010-05-28 13:22:33 +00:00
|
|
|
}
|
2010-01-27 16:50:02 +00:00
|
|
|
}
|
2010-01-26 22:30:32 +00:00
|
|
|
}
|
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
struct net_connection* net_con_create_kqueue(struct net_backend* data)
|
2010-01-26 22:30:32 +00:00
|
|
|
{
|
|
|
|
struct net_connection* con = (struct net_connection*) hub_malloc_zero(sizeof(struct net_connection_kqueue));
|
|
|
|
con->sd = -1;
|
|
|
|
return con;
|
|
|
|
}
|
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
void net_con_initialize_kqueue(struct net_backend* data, struct net_connection* con_, int sd, net_connection_cb callback, const void* ptr)
|
2010-01-26 22:30:32 +00:00
|
|
|
{
|
|
|
|
struct net_connection_kqueue* con = (struct net_connection_kqueue*) con_;
|
|
|
|
con->sd = sd;
|
|
|
|
con->flags = 0;
|
|
|
|
con->callback = callback;
|
|
|
|
con->ptr = (void*) ptr;
|
|
|
|
}
|
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
void net_con_backend_add_kqueue(struct net_backend* data, struct net_connection* con_, int events)
|
2010-01-26 22:30:32 +00:00
|
|
|
{
|
2010-02-10 20:57:27 +00:00
|
|
|
struct net_backend_kqueue* backend = (struct net_backend_kqueue*) data;
|
|
|
|
struct net_connection_kqueue* con = (struct net_connection_kqueue*) con_;
|
2010-05-28 13:22:33 +00:00
|
|
|
int operation;
|
2010-04-22 14:10:17 +00:00
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
backend->conns[con->sd] = con;
|
2010-04-22 14:10:17 +00:00
|
|
|
|
2010-05-28 13:22:33 +00:00
|
|
|
operation = CHANGE_ACTION_ADD;
|
2010-04-22 14:10:17 +00:00
|
|
|
|
2010-05-28 13:22:33 +00:00
|
|
|
if (events & NET_EVENT_READ)
|
|
|
|
operation |= CHANGE_OP_WANT_READ;
|
2010-04-22 14:10:17 +00:00
|
|
|
|
|
|
|
if (events & NET_EVENT_WRITE)
|
2010-05-28 13:22:33 +00:00
|
|
|
operation |= CHANGE_OP_WANT_WRITE;
|
2010-04-22 14:10:17 +00:00
|
|
|
|
2010-05-28 13:22:33 +00:00
|
|
|
add_change(backend, con, operation);
|
2010-01-26 22:30:32 +00:00
|
|
|
}
|
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
void net_con_backend_mod_kqueue(struct net_backend* data, struct net_connection* con_, int events)
|
2010-01-26 22:30:32 +00:00
|
|
|
{
|
2010-02-10 20:57:27 +00:00
|
|
|
struct net_backend_kqueue* backend = (struct net_backend_kqueue*) data;
|
2010-01-26 22:30:32 +00:00
|
|
|
struct net_connection_kqueue* con = (struct net_connection_kqueue*) con_;
|
2010-01-26 23:43:10 +00:00
|
|
|
|
2010-05-28 13:22:33 +00:00
|
|
|
int operation = CHANGE_ACTION_ADD;
|
2010-04-22 14:10:17 +00:00
|
|
|
|
2010-05-28 13:22:33 +00:00
|
|
|
if (events & NET_EVENT_READ)
|
|
|
|
operation |= CHANGE_OP_WANT_READ;
|
2010-01-26 23:43:10 +00:00
|
|
|
|
2010-04-22 14:10:17 +00:00
|
|
|
if (events & NET_EVENT_WRITE)
|
2010-05-28 13:22:33 +00:00
|
|
|
operation |= CHANGE_OP_WANT_WRITE;
|
2010-01-26 23:43:10 +00:00
|
|
|
|
2010-05-28 13:22:33 +00:00
|
|
|
add_change(backend, con, operation);
|
2010-01-26 22:30:32 +00:00
|
|
|
}
|
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
void net_con_backend_del_kqueue(struct net_backend* data, struct net_connection* con_)
|
2010-01-26 22:30:32 +00:00
|
|
|
{
|
2010-02-10 20:57:27 +00:00
|
|
|
struct net_backend_kqueue* backend = (struct net_backend_kqueue*) data;
|
2010-01-27 00:04:32 +00:00
|
|
|
struct net_connection_kqueue* con = (struct net_connection_kqueue*) con_;
|
2010-01-26 22:30:32 +00:00
|
|
|
|
2010-01-27 16:50:02 +00:00
|
|
|
/* No need to remove it from the kqueue filter, the kqueue man page says
|
2010-05-28 13:22:33 +00:00
|
|
|
it is automatically removed when the descriptor is closed... */
|
|
|
|
add_change(backend, con, CHANGE_ACTION_DEL);
|
2010-04-22 14:10:17 +00:00
|
|
|
|
|
|
|
// Unmap the socket descriptor.
|
|
|
|
backend->conns[con->sd] = 0;
|
2010-02-10 20:57:27 +00:00
|
|
|
}
|
2010-01-26 22:30:32 +00:00
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
void net_backend_shutdown_kqueue(struct net_backend* data)
|
|
|
|
{
|
|
|
|
struct net_backend_kqueue* backend = (struct net_backend_kqueue*) data;
|
|
|
|
close(backend->kqfd);
|
|
|
|
hub_free(backend->conns);
|
|
|
|
hub_free(backend->changes);
|
2010-05-28 13:22:33 +00:00
|
|
|
hub_free(backend->change_list);
|
2010-02-10 20:57:27 +00:00
|
|
|
hub_free(backend);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct net_backend* net_backend_init_kqueue(struct net_backend_handler* handler, struct net_backend_common* common)
|
|
|
|
{
|
|
|
|
struct net_backend_kqueue* backend;
|
|
|
|
|
|
|
|
if (getenv("EVENT_NOKQUEUE"))
|
|
|
|
return 0;
|
2010-01-26 22:30:32 +00:00
|
|
|
|
2010-02-10 20:57:27 +00:00
|
|
|
backend = hub_malloc_zero(sizeof(struct net_backend_kqueue));
|
2010-02-11 06:54:42 +00:00
|
|
|
backend->kqfd = kqueue();
|
2010-02-10 20:57:27 +00:00
|
|
|
if (backend->kqfd == -1)
|
|
|
|
{
|
|
|
|
LOG_WARN("Unable to create kqueue socket.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
backend->conns = hub_malloc_zero(sizeof(struct net_connection_kqueue*) * common->max);
|
2010-05-28 13:22:33 +00:00
|
|
|
backend->changes = hub_malloc_zero(sizeof(struct kevent) * common->max * 2);
|
|
|
|
backend->change_list = hub_malloc_zero(sizeof(int) * common->max);
|
2010-02-10 20:57:27 +00:00
|
|
|
backend->common = common;
|
|
|
|
|
|
|
|
net_backend_set_handlers(handler);
|
|
|
|
return (struct net_backend*) backend;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void net_backend_set_handlers(struct net_backend_handler* handler)
|
|
|
|
{
|
|
|
|
handler->backend_name = net_backend_name_kqueue;
|
|
|
|
handler->backend_poll = net_backend_poll_kqueue;
|
|
|
|
handler->backend_process = net_backend_process_kqueue;
|
|
|
|
handler->backend_shutdown = net_backend_shutdown_kqueue;
|
|
|
|
handler->con_create = net_con_create_kqueue;
|
|
|
|
handler->con_init = net_con_initialize_kqueue;
|
|
|
|
handler->con_add = net_con_backend_add_kqueue;
|
|
|
|
handler->con_mod = net_con_backend_mod_kqueue;
|
|
|
|
handler->con_del = net_con_backend_del_kqueue;
|
2010-01-26 22:30:32 +00:00
|
|
|
}
|
|
|
|
|
2010-05-28 13:22:33 +00:00
|
|
|
static void add_change(struct net_backend_kqueue* backend, struct net_connection_kqueue* con, int actions)
|
|
|
|
{
|
|
|
|
if (actions && !con->change)
|
|
|
|
{
|
|
|
|
backend->change_list[backend->change_list_len++] = con->sd;
|
|
|
|
con->change = actions;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t create_change_list(struct net_backend_kqueue* backend)
|
|
|
|
{
|
|
|
|
size_t n = 0;
|
|
|
|
size_t changes = 0;
|
|
|
|
int sd;
|
|
|
|
struct net_connection_kqueue* con;
|
|
|
|
unsigned short flags_r = 0;
|
|
|
|
unsigned short flags_w = 0;
|
|
|
|
|
|
|
|
for (; n < backend->change_list_len; n++)
|
|
|
|
{
|
|
|
|
sd = backend->change_list[n];
|
|
|
|
con = backend->conns[sd];
|
|
|
|
if (con)
|
|
|
|
{
|
|
|
|
flags_r = 0;
|
|
|
|
flags_w = 0;
|
|
|
|
|
|
|
|
if (con->change & CHANGE_ACTION_ADD)
|
|
|
|
{
|
|
|
|
flags_r |= EV_ADD;
|
|
|
|
flags_w |= EV_ADD;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (con->change & CHANGE_OP_WANT_READ)
|
|
|
|
flags_r |= EV_ENABLE;
|
|
|
|
else
|
|
|
|
flags_r |= EV_DISABLE;
|
|
|
|
|
|
|
|
if (con->change & CHANGE_OP_WANT_WRITE)
|
|
|
|
flags_w |= EV_ENABLE;
|
|
|
|
else
|
|
|
|
flags_w |= EV_DISABLE;
|
|
|
|
|
|
|
|
if (con->ev_r.flags != flags_r)
|
|
|
|
{
|
|
|
|
EV_SET(&con->ev_r, sd, EVFILT_READ, flags_r, 0, 0, con);
|
|
|
|
memcpy(&backend->changes[changes++], &con->ev_r, sizeof(struct kevent));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (con->ev_w.flags != flags_w)
|
|
|
|
{
|
|
|
|
EV_SET(&con->ev_w, sd, EVFILT_WRITE, flags_w, 0, 0, con);
|
|
|
|
memcpy(&backend->changes[changes++], &con->ev_w, sizeof(struct kevent));
|
|
|
|
}
|
|
|
|
|
|
|
|
con->change = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
EV_SET(&backend->changes[changes++], sd, EVFILT_READ, EV_DELETE, 0, 0, 0);
|
|
|
|
EV_SET(&backend->changes[changes++], sd, EVFILT_READ, EV_DELETE, 0, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
backend->change_list_len = 0;
|
|
|
|
return changes;
|
|
|
|
}
|
|
|
|
|
2010-01-26 22:30:32 +00:00
|
|
|
#endif /* USE_KQUEUE */
|