2019-01-11 01:19:36 +00:00
|
|
|
#include <ev/ev_win32.hpp>
|
2019-01-19 00:41:50 +00:00
|
|
|
|
2019-07-06 17:03:40 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
2019-09-03 19:56:16 +00:00
|
|
|
#include <util/logging/logger.hpp>
|
|
|
|
|
2019-01-19 00:41:50 +00:00
|
|
|
// a single event queue for the TUN interface
|
2019-02-05 20:33:49 +00:00
|
|
|
static HANDLE tun_event_queue = INVALID_HANDLE_VALUE;
|
2019-01-19 00:41:50 +00:00
|
|
|
|
|
|
|
// we hand the kernel our thread handles to process completion events
|
2019-02-05 20:33:49 +00:00
|
|
|
static HANDLE* kThreadPool;
|
2019-02-05 21:19:20 +00:00
|
|
|
static int poolSize;
|
2019-01-19 00:41:50 +00:00
|
|
|
|
|
|
|
// list of TUN listeners (useful for exits or other nodes with multiple TUNs)
|
2020-04-07 18:38:56 +00:00
|
|
|
std::list<win32_tun_io*> tun_listeners;
|
2019-01-19 00:41:50 +00:00
|
|
|
|
|
|
|
void
|
2019-12-11 21:18:47 +00:00
|
|
|
begin_tun_loop(int nThreads, llarp_ev_loop* loop)
|
2019-01-19 00:41:50 +00:00
|
|
|
{
|
|
|
|
kThreadPool = new HANDLE[nThreads];
|
2020-04-07 18:38:56 +00:00
|
|
|
for (int i = 0; i < nThreads; ++i)
|
2019-01-19 00:41:50 +00:00
|
|
|
{
|
2019-12-11 21:18:47 +00:00
|
|
|
kThreadPool[i] = CreateThread(nullptr, 0, &tun_ev_loop, loop, 0, nullptr);
|
2019-01-19 00:41:50 +00:00
|
|
|
}
|
|
|
|
llarp::LogInfo("created ", nThreads, " threads for TUN event queue");
|
2019-02-05 21:19:20 +00:00
|
|
|
poolSize = nThreads;
|
2019-01-19 00:41:50 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 04:20:21 +00:00
|
|
|
// this one is called from the TUN handler
|
2019-01-19 00:41:50 +00:00
|
|
|
bool
|
|
|
|
win32_tun_io::queue_write(const byte_t* buf, size_t sz)
|
|
|
|
{
|
2020-04-16 09:25:20 +00:00
|
|
|
return do_write((void*)buf, sz);
|
2019-01-19 00:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
win32_tun_io::setup()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (tuntap_start(tunif, TUNTAP_MODE_TUNNEL, 0) == -1)
|
2019-01-19 00:41:50 +00:00
|
|
|
{
|
|
|
|
llarp::LogWarn("failed to start interface");
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (tuntap_up(tunif) == -1)
|
2019-01-19 00:41:50 +00:00
|
|
|
{
|
|
|
|
char ebuf[1024];
|
|
|
|
int err = GetLastError();
|
2020-04-07 18:38:56 +00:00
|
|
|
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, LANG_NEUTRAL, ebuf, 1024, nullptr);
|
2019-01-19 00:41:50 +00:00
|
|
|
llarp::LogWarn("failed to put interface up: ", ebuf);
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-31 19:47:16 +00:00
|
|
|
tunif->bindaddr = t->dnsaddr;
|
2019-01-19 00:41:50 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (tuntap_set_ip(tunif, t->ifaddr, t->ifaddr, t->netmask) == -1)
|
2019-03-21 02:55:58 +00:00
|
|
|
{
|
|
|
|
llarp::LogWarn("failed to set ip");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (tunif->tun_fd == INVALID_HANDLE_VALUE)
|
2019-01-19 00:41:50 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// first TUN device gets to set up the event port
|
|
|
|
bool
|
2019-12-11 21:18:47 +00:00
|
|
|
win32_tun_io::add_ev(llarp_ev_loop* loop)
|
2019-01-19 00:41:50 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (tun_event_queue == INVALID_HANDLE_VALUE)
|
2019-01-19 00:41:50 +00:00
|
|
|
{
|
|
|
|
SYSTEM_INFO sys_info;
|
|
|
|
GetSystemInfo(&sys_info);
|
|
|
|
unsigned long numCPU = sys_info.dwNumberOfProcessors;
|
|
|
|
// let the system handle 2x the number of CPUs or hardware
|
|
|
|
// threads
|
2020-04-07 18:38:56 +00:00
|
|
|
tun_event_queue = CreateIoCompletionPort(tunif->tun_fd, nullptr, (ULONG_PTR)this, numCPU * 2);
|
2019-12-11 21:18:47 +00:00
|
|
|
begin_tun_loop(numCPU * 2, loop);
|
2019-01-19 00:41:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
CreateIoCompletionPort(tunif->tun_fd, tun_event_queue, (ULONG_PTR)this, 0);
|
|
|
|
|
|
|
|
// we're already non-blocking
|
|
|
|
// add to list
|
2020-05-01 22:25:18 +00:00
|
|
|
tun_listeners.push_back(this);
|
2020-04-16 11:28:02 +00:00
|
|
|
byte_t* readbuf = (byte_t*)malloc(1500);
|
2020-04-16 09:45:50 +00:00
|
|
|
read(readbuf, 1500);
|
2019-01-19 00:41:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// places data in event queue for kernel to process
|
2020-04-16 09:25:20 +00:00
|
|
|
bool
|
2019-01-19 00:41:50 +00:00
|
|
|
win32_tun_io::do_write(void* data, size_t sz)
|
2020-05-01 22:25:18 +00:00
|
|
|
{
|
2020-04-16 09:18:06 +00:00
|
|
|
DWORD code;
|
2019-01-19 00:41:50 +00:00
|
|
|
asio_evt_pkt* pkt = new asio_evt_pkt;
|
2020-04-07 18:38:56 +00:00
|
|
|
pkt->buf = data;
|
|
|
|
pkt->sz = sz;
|
|
|
|
pkt->write = true;
|
2019-01-19 00:41:50 +00:00
|
|
|
memset(&pkt->pkt, '\0', sizeof(pkt->pkt));
|
2020-05-01 22:25:18 +00:00
|
|
|
WriteFile(tunif->tun_fd, data, sz, nullptr, &pkt->pkt);
|
|
|
|
code = GetLastError();
|
|
|
|
// llarp::LogInfo("wrote data, error ", code);
|
2020-04-16 09:27:27 +00:00
|
|
|
return (code == 0 || code == 997);
|
2019-01-19 00:41:50 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 04:20:21 +00:00
|
|
|
// while this one is called from the event loop
|
|
|
|
// eventually comes back and calls queue_write()
|
2019-01-19 00:41:50 +00:00
|
|
|
void
|
|
|
|
win32_tun_io::flush_write()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (t->before_write)
|
2019-01-19 00:41:50 +00:00
|
|
|
t->before_write(t);
|
|
|
|
}
|
|
|
|
|
2020-04-16 09:27:27 +00:00
|
|
|
void
|
2019-01-19 00:41:50 +00:00
|
|
|
win32_tun_io::read(byte_t* buf, size_t sz)
|
2020-05-01 22:25:18 +00:00
|
|
|
{
|
2019-01-19 00:41:50 +00:00
|
|
|
asio_evt_pkt* pkt = new asio_evt_pkt;
|
2020-04-07 18:38:56 +00:00
|
|
|
pkt->buf = buf;
|
2019-01-19 00:41:50 +00:00
|
|
|
memset(&pkt->pkt, '\0', sizeof(OVERLAPPED));
|
2020-04-07 18:38:56 +00:00
|
|
|
pkt->sz = sz;
|
2019-01-19 00:41:50 +00:00
|
|
|
pkt->write = false;
|
2020-04-16 09:45:50 +00:00
|
|
|
ReadFile(tunif->tun_fd, buf, sz, nullptr, &pkt->pkt);
|
2019-01-19 00:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// and now the event loop itself
|
|
|
|
extern "C" DWORD FAR PASCAL
|
2019-12-11 21:18:47 +00:00
|
|
|
tun_ev_loop(void* u)
|
2019-01-19 00:41:50 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_ev_loop* logic = static_cast<llarp_ev_loop*>(u);
|
2019-01-19 00:41:50 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
DWORD size = 0;
|
|
|
|
OVERLAPPED* ovl = nullptr;
|
2019-01-19 00:41:50 +00:00
|
|
|
ULONG_PTR listener = 0;
|
2020-04-07 18:38:56 +00:00
|
|
|
asio_evt_pkt* pkt = nullptr;
|
2019-01-19 00:41:50 +00:00
|
|
|
BOOL alert;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
while (true)
|
2019-01-19 00:41:50 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
alert = GetQueuedCompletionStatus(tun_event_queue, &size, &listener, &ovl, EV_TICK_INTERVAL);
|
2019-01-19 00:41:50 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!alert)
|
2019-01-19 00:41:50 +00:00
|
|
|
{
|
|
|
|
// tick listeners on io timeout, this is required to be done every tick
|
|
|
|
// cycle regardless of any io being done, this manages the internal state
|
|
|
|
// of the tun logic
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& tun : tun_listeners)
|
2019-01-19 00:41:50 +00:00
|
|
|
{
|
2020-05-01 22:25:18 +00:00
|
|
|
logic->call_soon([tun]() {
|
2020-04-16 08:49:28 +00:00
|
|
|
tun->flush_write();
|
2020-04-07 18:38:56 +00:00
|
|
|
if (tun->t->tick)
|
2019-12-11 21:18:47 +00:00
|
|
|
tun->t->tick(tun->t);
|
|
|
|
});
|
2019-01-19 00:41:50 +00:00
|
|
|
}
|
|
|
|
continue; // let's go at it once more
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (listener == (ULONG_PTR)~0)
|
2019-01-19 00:41:50 +00:00
|
|
|
break;
|
|
|
|
// if we're here, then we got something interesting :>
|
2020-04-07 18:38:56 +00:00
|
|
|
pkt = (asio_evt_pkt*)ovl;
|
|
|
|
win32_tun_io* ev = reinterpret_cast<win32_tun_io*>(listener);
|
|
|
|
if (!pkt->write)
|
2019-01-19 00:41:50 +00:00
|
|
|
{
|
|
|
|
// llarp::LogInfo("read tun ", size, " bytes, pass to handler");
|
2019-12-11 21:18:47 +00:00
|
|
|
logic->call_soon([pkt, size, ev]() {
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ev->t->recvpkt)
|
2020-05-01 22:25:18 +00:00
|
|
|
ev->t->recvpkt(ev->t, llarp_buffer_t(pkt->buf, size));
|
2020-04-16 11:28:02 +00:00
|
|
|
free(pkt->buf);
|
2020-04-16 10:53:30 +00:00
|
|
|
delete pkt;
|
2020-05-01 22:25:18 +00:00
|
|
|
});
|
2020-04-16 11:28:02 +00:00
|
|
|
byte_t* readbuf = (byte_t*)malloc(1500);
|
2020-04-16 09:45:50 +00:00
|
|
|
ev->read(readbuf, 1500);
|
2019-01-19 00:41:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-01 22:25:18 +00:00
|
|
|
// ok let's queue another read!
|
2020-04-16 11:28:02 +00:00
|
|
|
byte_t* readbuf = (byte_t*)malloc(1500);
|
2020-04-16 09:45:50 +00:00
|
|
|
ev->read(readbuf, 1500);
|
2019-01-19 00:41:50 +00:00
|
|
|
}
|
2019-12-11 21:18:47 +00:00
|
|
|
logic->call_soon([ev]() {
|
2020-05-01 22:25:18 +00:00
|
|
|
ev->flush_write();
|
|
|
|
if (ev->t->tick)
|
2019-12-11 21:18:47 +00:00
|
|
|
ev->t->tick(ev->t);
|
|
|
|
});
|
2019-01-19 00:41:50 +00:00
|
|
|
}
|
2019-02-05 21:19:20 +00:00
|
|
|
llarp::LogDebug("exit TUN event loop thread from system managed thread pool");
|
2019-01-19 00:41:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
exit_tun_loop()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (kThreadPool)
|
2019-01-19 00:41:50 +00:00
|
|
|
{
|
2019-02-05 20:33:49 +00:00
|
|
|
// kill the kernel's thread pool
|
2019-02-05 21:19:20 +00:00
|
|
|
// int i = (&kThreadPool)[1] - kThreadPool; // get the size of our thread
|
|
|
|
// pool
|
|
|
|
llarp::LogInfo("closing ", poolSize, " threads");
|
|
|
|
// if we get all-ones in the queue, thread exits, and we clean up
|
2020-04-07 18:38:56 +00:00
|
|
|
for (int j = 0; j < poolSize; ++j)
|
2019-02-05 21:19:20 +00:00
|
|
|
PostQueuedCompletionStatus(tun_event_queue, 0, ~0, nullptr);
|
|
|
|
WaitForMultipleObjects(poolSize, kThreadPool, TRUE, INFINITE);
|
2020-04-07 18:38:56 +00:00
|
|
|
for (int j = 0; j < poolSize; ++j)
|
2019-02-05 20:33:49 +00:00
|
|
|
CloseHandle(kThreadPool[j]);
|
|
|
|
delete[] kThreadPool;
|
2019-02-05 21:19:20 +00:00
|
|
|
kThreadPool = nullptr;
|
2019-02-05 20:33:49 +00:00
|
|
|
|
|
|
|
// the IOCP refcount is decreased each time an associated fd
|
|
|
|
// is closed
|
|
|
|
// the fds are closed in their destructors
|
|
|
|
// once we get to zero, we can safely close the event port
|
|
|
|
auto itr = tun_listeners.begin();
|
2020-04-07 18:38:56 +00:00
|
|
|
while (itr != tun_listeners.end())
|
2019-02-05 20:33:49 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
delete (*itr);
|
2019-02-05 20:33:49 +00:00
|
|
|
itr = tun_listeners.erase(itr);
|
|
|
|
}
|
|
|
|
CloseHandle(tun_event_queue);
|
2019-01-19 00:41:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-06 17:03:40 +00:00
|
|
|
#endif
|