2019-11-05 13:55:54 -08:00
|
|
|
/*
|
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2019-11-05 13:55:54 -08:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <uv.h>
|
|
|
|
|
|
|
|
#include <isc/atomic.h>
|
|
|
|
#include <isc/buffer.h>
|
|
|
|
#include <isc/condition.h>
|
2020-09-05 11:07:40 -07:00
|
|
|
#include <isc/errno.h>
|
2019-11-05 13:55:54 -08:00
|
|
|
#include <isc/magic.h>
|
|
|
|
#include <isc/mem.h>
|
|
|
|
#include <isc/netmgr.h>
|
|
|
|
#include <isc/random.h>
|
|
|
|
#include <isc/refcount.h>
|
|
|
|
#include <isc/region.h>
|
|
|
|
#include <isc/result.h>
|
|
|
|
#include <isc/sockaddr.h>
|
|
|
|
#include <isc/thread.h>
|
|
|
|
#include <isc/util.h>
|
2019-12-02 11:19:55 +01:00
|
|
|
|
2019-11-05 13:55:54 -08:00
|
|
|
#include "netmgr-int.h"
|
2020-02-12 13:59:18 +01:00
|
|
|
#include "uv-compat.h"
|
2019-11-05 13:55:54 -08:00
|
|
|
|
2020-02-14 08:14:03 +01:00
|
|
|
static isc_result_t
|
|
|
|
udp_send_direct(isc_nmsocket_t *sock, isc__nm_uvreq_t *req,
|
|
|
|
isc_sockaddr_t *peer);
|
2019-11-05 13:55:54 -08:00
|
|
|
|
2020-02-14 08:14:03 +01:00
|
|
|
static void
|
|
|
|
udp_recv_cb(uv_udp_t *handle, ssize_t nrecv, const uv_buf_t *buf,
|
|
|
|
const struct sockaddr *addr, unsigned flags);
|
2019-11-05 13:55:54 -08:00
|
|
|
|
2020-02-14 08:14:03 +01:00
|
|
|
static void
|
|
|
|
udp_send_cb(uv_udp_send_t *req, int status);
|
2019-11-05 13:55:54 -08:00
|
|
|
|
2020-09-05 11:07:40 -07:00
|
|
|
static void
|
|
|
|
udp_close_cb(uv_handle_t *uvhandle);
|
|
|
|
|
|
|
|
static void
|
|
|
|
udp_close_direct(isc_nmsocket_t *sock);
|
|
|
|
|
2019-11-05 13:55:54 -08:00
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_nm_listenudp(isc_nm_t *mgr, isc_nmiface_t *iface, isc_nm_recv_cb_t cb,
|
2020-02-13 14:44:37 -08:00
|
|
|
void *cbarg, size_t extrahandlesize, isc_nmsocket_t **sockp) {
|
2019-11-05 13:55:54 -08:00
|
|
|
isc_nmsocket_t *nsock = NULL;
|
|
|
|
|
|
|
|
REQUIRE(VALID_NM(mgr));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We are creating mgr->nworkers duplicated sockets, one
|
|
|
|
* socket for each worker thread.
|
|
|
|
*/
|
|
|
|
nsock = isc_mem_get(mgr->mctx, sizeof(isc_nmsocket_t));
|
2020-01-05 01:02:12 -08:00
|
|
|
isc__nmsocket_init(nsock, mgr, isc_nm_udplistener, iface);
|
2019-11-05 13:55:54 -08:00
|
|
|
nsock->nchildren = mgr->nworkers;
|
|
|
|
atomic_init(&nsock->rchildren, mgr->nworkers);
|
2020-02-13 14:44:37 -08:00
|
|
|
nsock->children = isc_mem_get(mgr->mctx,
|
|
|
|
mgr->nworkers * sizeof(*nsock));
|
2019-11-05 13:55:54 -08:00
|
|
|
memset(nsock->children, 0, mgr->nworkers * sizeof(*nsock));
|
|
|
|
|
2020-09-11 10:53:31 +02:00
|
|
|
INSIST(nsock->recv_cb == NULL && nsock->recv_cbarg == NULL);
|
|
|
|
nsock->recv_cb = cb;
|
|
|
|
nsock->recv_cbarg = cbarg;
|
2019-11-05 13:55:54 -08:00
|
|
|
nsock->extrahandlesize = extrahandlesize;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < mgr->nworkers; i++) {
|
2020-10-05 10:40:02 +02:00
|
|
|
isc_result_t result;
|
2020-10-05 10:51:40 +02:00
|
|
|
sa_family_t sa_family = iface->addr.type.sa.sa_family;
|
2019-11-05 13:55:54 -08:00
|
|
|
|
|
|
|
isc__netievent_udplisten_t *ievent = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_nmsocket_t *csock = &nsock->children[i];
|
2019-11-05 13:55:54 -08:00
|
|
|
|
2020-01-05 01:02:12 -08:00
|
|
|
isc__nmsocket_init(csock, mgr, isc_nm_udpsocket, iface);
|
2019-11-05 13:55:54 -08:00
|
|
|
csock->parent = nsock;
|
|
|
|
csock->tid = i;
|
|
|
|
csock->extrahandlesize = extrahandlesize;
|
|
|
|
|
2020-09-11 10:53:31 +02:00
|
|
|
INSIST(csock->recv_cb == NULL && csock->recv_cbarg == NULL);
|
|
|
|
csock->recv_cb = cb;
|
|
|
|
csock->recv_cbarg = cbarg;
|
2020-10-05 10:51:40 +02:00
|
|
|
csock->fd = socket(sa_family, SOCK_DGRAM, 0);
|
2020-06-10 11:32:39 +02:00
|
|
|
RUNTIME_CHECK(csock->fd >= 0);
|
2019-11-05 13:55:54 -08:00
|
|
|
|
2020-10-05 13:14:04 +02:00
|
|
|
result = isc__nm_socket_reuse(csock->fd);
|
|
|
|
RUNTIME_CHECK(result == ISC_R_SUCCESS ||
|
|
|
|
result == ISC_R_NOTIMPLEMENTED);
|
|
|
|
|
|
|
|
result = isc__nm_socket_reuse_lb(csock->fd);
|
2020-10-05 10:40:02 +02:00
|
|
|
RUNTIME_CHECK(result == ISC_R_SUCCESS ||
|
|
|
|
result == ISC_R_NOTIMPLEMENTED);
|
|
|
|
|
Don't check the result of setting SO_INCOMING_CPU
The SO_INCOMING_CPU is available since Linux 3.19 for getting the value,
but only since Linux 4.4 for setting the value (see below for a full
description). BIND 9 should not fail when setting the option on the
socket fails, as this is only an optimization and not hard requirement
to run BIND 9.
SO_INCOMING_CPU (gettable since Linux 3.19, settable since Linux 4.4)
Sets or gets the CPU affinity of a socket. Expects an integer flag.
int cpu = 1;
setsockopt(fd, SOL_SOCKET, SO_INCOMING_CPU, &cpu, sizeof(cpu));
Because all of the packets for a single stream (i.e., all
packets for the same 4-tuple) arrive on the single RX queue that
is associated with a particular CPU, the typical use case is to
employ one listening process per RX queue, with the incoming
flow being handled by a listener on the same CPU that is
handling the RX queue. This provides optimal NUMA behavior and
keeps CPU caches hot.
2020-06-03 11:01:19 +02:00
|
|
|
/* We don't check for the result, because SO_INCOMING_CPU can be
|
|
|
|
* available without the setter on Linux kernel version 4.4, and
|
|
|
|
* setting SO_INCOMING_CPU is just an optimization.
|
|
|
|
*/
|
2020-10-05 10:40:02 +02:00
|
|
|
(void)isc__nm_socket_incoming_cpu(csock->fd);
|
|
|
|
|
2020-10-05 10:51:40 +02:00
|
|
|
(void)isc__nm_socket_dontfrag(csock->fd, sa_family);
|
|
|
|
|
2019-11-05 13:55:54 -08:00
|
|
|
ievent = isc__nm_get_ievent(mgr, netievent_udplisten);
|
|
|
|
ievent->sock = csock;
|
|
|
|
isc__nm_enqueue_ievent(&mgr->workers[i],
|
2020-02-12 13:59:18 +01:00
|
|
|
(isc__netievent_t *)ievent);
|
2019-11-05 13:55:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
*sockp = nsock;
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2020-07-02 16:27:38 +02:00
|
|
|
/*%<
|
|
|
|
* Allocator for UDP recv operations. Limited to size 20 * (2^16 + 2),
|
|
|
|
* which allows enough space for recvmmsg() to get multiple messages at
|
|
|
|
* a time.
|
|
|
|
*
|
|
|
|
* Note this doesn't actually allocate anything, it just assigns the
|
|
|
|
* worker's receive buffer to a socket, and marks it as "in use".
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
udp_alloc_cb(uv_handle_t *handle, size_t size, uv_buf_t *buf) {
|
|
|
|
isc_nmsocket_t *sock = uv_handle_get_data(handle);
|
|
|
|
isc__networker_t *worker = NULL;
|
|
|
|
|
|
|
|
REQUIRE(VALID_NMSOCK(sock));
|
|
|
|
REQUIRE(sock->type == isc_nm_udpsocket);
|
|
|
|
REQUIRE(isc__nm_in_netthread());
|
|
|
|
REQUIRE(size <= ISC_NETMGR_RECVBUF_SIZE);
|
|
|
|
|
|
|
|
worker = &sock->mgr->workers[sock->tid];
|
|
|
|
INSIST(!worker->recvbuf_inuse);
|
|
|
|
|
|
|
|
buf->base = worker->recvbuf;
|
|
|
|
buf->len = ISC_NETMGR_RECVBUF_SIZE;
|
|
|
|
worker->recvbuf_inuse = true;
|
|
|
|
}
|
|
|
|
|
2019-11-05 13:55:54 -08:00
|
|
|
/*
|
2020-09-03 13:31:27 -07:00
|
|
|
* Asynchronous 'udplisten' call handler: start listening on a UDP socket.
|
2019-11-05 13:55:54 -08:00
|
|
|
*/
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc__nm_async_udplisten(isc__networker_t *worker, isc__netievent_t *ev0) {
|
2020-02-12 13:59:18 +01:00
|
|
|
isc__netievent_udplisten_t *ievent = (isc__netievent_udplisten_t *)ev0;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_nmsocket_t *sock = ievent->sock;
|
2020-04-27 18:57:07 +02:00
|
|
|
int r, uv_bind_flags = 0;
|
|
|
|
int uv_init_flags = 0;
|
2020-10-05 11:17:52 +02:00
|
|
|
sa_family_t sa_family;
|
2019-11-05 13:55:54 -08:00
|
|
|
|
|
|
|
REQUIRE(sock->type == isc_nm_udpsocket);
|
|
|
|
REQUIRE(sock->iface != NULL);
|
|
|
|
REQUIRE(sock->parent != NULL);
|
2020-01-16 12:13:28 +01:00
|
|
|
REQUIRE(sock->tid == isc_nm_tid());
|
2019-11-05 13:55:54 -08:00
|
|
|
|
2020-04-27 18:57:07 +02:00
|
|
|
#ifdef UV_UDP_RECVMMSG
|
|
|
|
uv_init_flags |= UV_UDP_RECVMMSG;
|
|
|
|
#endif
|
|
|
|
uv_udp_init_ex(&worker->loop, &sock->uv_handle.udp, uv_init_flags);
|
2019-12-02 11:19:55 +01:00
|
|
|
uv_handle_set_data(&sock->uv_handle.handle, NULL);
|
2020-06-04 14:54:36 -07:00
|
|
|
isc__nmsocket_attach(sock,
|
|
|
|
(isc_nmsocket_t **)&sock->uv_handle.udp.data);
|
2019-11-05 13:55:54 -08:00
|
|
|
|
2020-01-06 20:26:47 -08:00
|
|
|
r = uv_udp_open(&sock->uv_handle.udp, sock->fd);
|
|
|
|
if (r == 0) {
|
|
|
|
isc__nm_incstats(sock->mgr, sock->statsindex[STATID_OPEN]);
|
|
|
|
} else {
|
2020-02-12 13:59:18 +01:00
|
|
|
isc__nm_incstats(sock->mgr, sock->statsindex[STATID_OPENFAIL]);
|
2020-01-06 20:26:47 -08:00
|
|
|
}
|
|
|
|
|
2020-10-05 11:17:52 +02:00
|
|
|
sa_family = sock->iface->addr.type.sa.sa_family;
|
|
|
|
if (sa_family == AF_INET6) {
|
2020-04-27 18:57:07 +02:00
|
|
|
uv_bind_flags |= UV_UDP_IPV6ONLY;
|
2020-01-10 14:25:30 -08:00
|
|
|
}
|
2020-01-06 20:26:47 -08:00
|
|
|
|
|
|
|
r = uv_udp_bind(&sock->uv_handle.udp,
|
2020-04-27 18:57:07 +02:00
|
|
|
&sock->parent->iface->addr.type.sa, uv_bind_flags);
|
2020-07-21 13:29:14 +02:00
|
|
|
if (r == UV_EADDRNOTAVAIL &&
|
2020-10-05 11:17:52 +02:00
|
|
|
isc__nm_socket_freebind(sock->fd, sa_family) == ISC_R_SUCCESS)
|
2020-07-21 13:29:14 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Retry binding with IP_FREEBIND (or equivalent option) if the
|
|
|
|
* address is not available. This helps with IPv6 tentative
|
|
|
|
* addresses which are reported by the route socket, although
|
|
|
|
* named is not yet able to properly bind to them.
|
|
|
|
*/
|
|
|
|
r = uv_udp_bind(&sock->uv_handle.udp,
|
|
|
|
&sock->parent->iface->addr.type.sa,
|
|
|
|
uv_bind_flags);
|
|
|
|
}
|
|
|
|
|
2020-01-06 20:26:47 -08:00
|
|
|
if (r < 0) {
|
2020-02-12 13:59:18 +01:00
|
|
|
isc__nm_incstats(sock->mgr, sock->statsindex[STATID_BINDFAIL]);
|
2020-01-06 20:26:47 -08:00
|
|
|
}
|
2020-04-30 00:25:09 +02:00
|
|
|
#ifdef ISC_RECV_BUFFER_SIZE
|
2019-11-05 13:55:54 -08:00
|
|
|
uv_recv_buffer_size(&sock->uv_handle.handle,
|
2020-04-30 00:25:09 +02:00
|
|
|
&(int){ ISC_RECV_BUFFER_SIZE });
|
|
|
|
#endif
|
|
|
|
#ifdef ISC_SEND_BUFFER_SIZE
|
2019-11-05 13:55:54 -08:00
|
|
|
uv_send_buffer_size(&sock->uv_handle.handle,
|
2020-04-30 00:25:09 +02:00
|
|
|
&(int){ ISC_SEND_BUFFER_SIZE });
|
|
|
|
#endif
|
2020-07-02 16:27:38 +02:00
|
|
|
uv_udp_recv_start(&sock->uv_handle.udp, udp_alloc_cb, udp_recv_cb);
|
2019-11-05 13:55:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-06-10 11:32:39 +02:00
|
|
|
udp_stop_cb(uv_handle_t *handle) {
|
2019-12-02 11:19:55 +01:00
|
|
|
isc_nmsocket_t *sock = uv_handle_get_data(handle);
|
2019-11-05 13:55:54 -08:00
|
|
|
atomic_store(&sock->closed, true);
|
|
|
|
|
2020-06-04 14:54:36 -07:00
|
|
|
isc__nmsocket_detach((isc_nmsocket_t **)&sock->uv_handle.udp.data);
|
2019-11-05 13:55:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
stop_udp_child(isc_nmsocket_t *sock) {
|
2020-01-16 12:13:28 +01:00
|
|
|
REQUIRE(sock->type == isc_nm_udpsocket);
|
|
|
|
REQUIRE(sock->tid == isc_nm_tid());
|
2019-11-05 13:55:54 -08:00
|
|
|
|
|
|
|
uv_udp_recv_stop(&sock->uv_handle.udp);
|
2020-06-10 11:32:39 +02:00
|
|
|
uv_close((uv_handle_t *)&sock->uv_handle.udp, udp_stop_cb);
|
2019-11-05 13:55:54 -08:00
|
|
|
|
2020-01-06 20:26:47 -08:00
|
|
|
isc__nm_incstats(sock->mgr, sock->statsindex[STATID_CLOSE]);
|
|
|
|
|
2019-11-05 13:55:54 -08:00
|
|
|
LOCK(&sock->parent->lock);
|
|
|
|
atomic_fetch_sub(&sock->parent->rchildren, 1);
|
|
|
|
UNLOCK(&sock->parent->lock);
|
|
|
|
BROADCAST(&sock->parent->cond);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
stoplistening(isc_nmsocket_t *sock) {
|
2020-01-16 12:13:28 +01:00
|
|
|
REQUIRE(sock->type == isc_nm_udplistener);
|
|
|
|
|
2019-11-05 13:55:54 -08:00
|
|
|
for (int i = 0; i < sock->nchildren; i++) {
|
2019-12-09 12:24:46 -08:00
|
|
|
isc__netievent_udpstop_t *event = NULL;
|
2019-11-05 13:55:54 -08:00
|
|
|
|
2020-01-16 12:13:28 +01:00
|
|
|
if (isc_nm_tid() == sock->children[i].tid) {
|
2019-11-05 13:55:54 -08:00
|
|
|
stop_udp_child(&sock->children[i]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-12-09 12:24:46 -08:00
|
|
|
event = isc__nm_get_ievent(sock->mgr, netievent_udpstop);
|
2019-11-05 13:55:54 -08:00
|
|
|
event->sock = &sock->children[i];
|
|
|
|
isc__nm_enqueue_ievent(&sock->mgr->workers[i],
|
2020-02-12 13:59:18 +01:00
|
|
|
(isc__netievent_t *)event);
|
2019-11-05 13:55:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
while (atomic_load_relaxed(&sock->rchildren) > 0) {
|
|
|
|
WAIT(&sock->cond, &sock->lock);
|
|
|
|
}
|
|
|
|
atomic_store(&sock->closed, true);
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
|
|
|
isc__nmsocket_prep_destroy(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-03-20 11:55:10 +01:00
|
|
|
isc__nm_udp_stoplistening(isc_nmsocket_t *sock) {
|
2019-12-09 12:24:46 -08:00
|
|
|
isc__netievent_udpstop_t *ievent = NULL;
|
2019-11-05 13:55:54 -08:00
|
|
|
|
|
|
|
/* We can't be launched from network thread, we'd deadlock */
|
|
|
|
REQUIRE(!isc__nm_in_netthread());
|
|
|
|
REQUIRE(VALID_NMSOCK(sock));
|
|
|
|
REQUIRE(sock->type == isc_nm_udplistener);
|
|
|
|
|
2020-06-22 15:46:11 -07:00
|
|
|
/*
|
2020-10-22 10:07:56 +02:00
|
|
|
* If the socket is active, mark it inactive and
|
|
|
|
* continue. If it isn't active, stop now.
|
2020-06-22 15:46:11 -07:00
|
|
|
*/
|
2020-10-22 10:07:56 +02:00
|
|
|
if (!isc__nmsocket_deactivate(sock)) {
|
2020-06-22 15:46:11 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-05 13:55:54 -08:00
|
|
|
/*
|
|
|
|
* If the manager is interlocked, re-enqueue this as an asynchronous
|
|
|
|
* event. Otherwise, go ahead and stop listening right away.
|
|
|
|
*/
|
|
|
|
if (!isc__nm_acquire_interlocked(sock->mgr)) {
|
2019-12-09 12:24:46 -08:00
|
|
|
ievent = isc__nm_get_ievent(sock->mgr, netievent_udpstop);
|
2019-11-05 13:55:54 -08:00
|
|
|
ievent->sock = sock;
|
|
|
|
isc__nm_enqueue_ievent(&sock->mgr->workers[sock->tid],
|
2020-02-12 13:59:18 +01:00
|
|
|
(isc__netievent_t *)ievent);
|
2019-11-05 13:55:54 -08:00
|
|
|
} else {
|
|
|
|
stoplistening(sock);
|
|
|
|
isc__nm_drop_interlocked(sock->mgr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-09-03 13:31:27 -07:00
|
|
|
* Asynchronous 'udpstop' call handler: stop listening on a UDP socket.
|
2019-11-05 13:55:54 -08:00
|
|
|
*/
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc__nm_async_udpstop(isc__networker_t *worker, isc__netievent_t *ev0) {
|
2020-02-12 13:59:18 +01:00
|
|
|
isc__netievent_udpstop_t *ievent = (isc__netievent_udpstop_t *)ev0;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_nmsocket_t *sock = ievent->sock;
|
2019-11-05 13:55:54 -08:00
|
|
|
|
|
|
|
REQUIRE(sock->iface != NULL);
|
|
|
|
UNUSED(worker);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If this is a child socket, stop listening and return.
|
|
|
|
*/
|
|
|
|
if (sock->parent != NULL) {
|
|
|
|
stop_udp_child(sock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If network manager is paused, re-enqueue the event for later.
|
|
|
|
*/
|
|
|
|
if (!isc__nm_acquire_interlocked(sock->mgr)) {
|
|
|
|
isc__netievent_udplisten_t *event = NULL;
|
|
|
|
|
2019-12-09 12:24:46 -08:00
|
|
|
event = isc__nm_get_ievent(sock->mgr, netievent_udpstop);
|
2019-11-05 13:55:54 -08:00
|
|
|
event->sock = sock;
|
|
|
|
isc__nm_enqueue_ievent(&sock->mgr->workers[sock->tid],
|
2020-02-12 13:59:18 +01:00
|
|
|
(isc__netievent_t *)event);
|
2019-11-05 13:55:54 -08:00
|
|
|
} else {
|
|
|
|
stoplistening(sock);
|
|
|
|
isc__nm_drop_interlocked(sock->mgr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-01-06 20:26:47 -08:00
|
|
|
* udp_recv_cb handles incoming UDP packet from uv. The buffer here is
|
|
|
|
* reused for a series of packets, so we need to allocate a new one. This
|
|
|
|
* new one can be reused to send the response then.
|
2019-11-05 13:55:54 -08:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
udp_recv_cb(uv_udp_t *handle, ssize_t nrecv, const uv_buf_t *buf,
|
2020-02-13 14:44:37 -08:00
|
|
|
const struct sockaddr *addr, unsigned flags) {
|
|
|
|
isc_result_t result;
|
|
|
|
isc_nmhandle_t *nmhandle = NULL;
|
|
|
|
isc_sockaddr_t sockaddr;
|
2020-06-04 23:13:54 -07:00
|
|
|
isc_nmsocket_t *sock = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_region_t region;
|
|
|
|
uint32_t maxudp;
|
2020-04-29 15:19:32 +02:00
|
|
|
bool free_buf = true;
|
2020-09-11 10:53:31 +02:00
|
|
|
isc_nm_recv_cb_t cb;
|
|
|
|
void *cbarg;
|
2020-09-05 11:07:40 -07:00
|
|
|
bool connected;
|
2019-11-05 13:55:54 -08:00
|
|
|
|
2020-06-04 23:13:54 -07:00
|
|
|
/*
|
|
|
|
* Even though destruction of the socket can only happen from the
|
|
|
|
* network thread that we're in, we still attach to the socket here
|
|
|
|
* to ensure it won't be destroyed by the recv callback.
|
|
|
|
*/
|
|
|
|
isc__nmsocket_attach(uv_handle_get_data((uv_handle_t *)handle), &sock);
|
2019-11-05 13:55:54 -08:00
|
|
|
|
2020-04-29 15:19:32 +02:00
|
|
|
#ifdef UV_UDP_MMSG_CHUNK
|
|
|
|
free_buf = ((flags & UV_UDP_MMSG_CHUNK) == 0);
|
|
|
|
#else
|
2019-11-05 13:55:54 -08:00
|
|
|
UNUSED(flags);
|
2020-04-29 15:19:32 +02:00
|
|
|
#endif
|
2019-11-05 13:55:54 -08:00
|
|
|
|
|
|
|
/*
|
2020-09-03 13:31:27 -07:00
|
|
|
* Three possible reasons to return now without processing:
|
|
|
|
* - If addr == NULL, in which case it's the end of stream;
|
|
|
|
* we can free the buffer and bail.
|
2020-06-22 15:46:11 -07:00
|
|
|
* - If we're simulating a firewall blocking UDP packets
|
|
|
|
* bigger than 'maxudp' bytes for testing purposes.
|
|
|
|
* - If the socket is no longer active.
|
2019-11-05 13:55:54 -08:00
|
|
|
*/
|
2020-06-22 15:46:11 -07:00
|
|
|
maxudp = atomic_load(&sock->mgr->maxudp);
|
|
|
|
if ((addr == NULL) || (maxudp != 0 && (uint32_t)nrecv > maxudp) ||
|
|
|
|
(!isc__nmsocket_active(sock)))
|
|
|
|
{
|
2020-04-29 15:19:32 +02:00
|
|
|
if (free_buf) {
|
|
|
|
isc__nm_free_uvbuf(sock, buf);
|
|
|
|
}
|
2020-06-04 23:13:54 -07:00
|
|
|
isc__nmsocket_detach(&sock);
|
2019-11-05 13:55:54 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = isc_sockaddr_fromsockaddr(&sockaddr, addr);
|
|
|
|
RUNTIME_CHECK(result == ISC_R_SUCCESS);
|
2020-09-05 11:07:40 -07:00
|
|
|
connected = atomic_load(&sock->connected);
|
|
|
|
|
|
|
|
if (!connected) {
|
|
|
|
nmhandle = isc__nmhandle_get(sock, &sockaddr, NULL);
|
|
|
|
} else {
|
|
|
|
nmhandle = sock->statichandle;
|
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
region.base = (unsigned char *)buf->base;
|
2019-11-05 13:55:54 -08:00
|
|
|
region.length = nrecv;
|
|
|
|
|
2020-09-18 12:27:40 +02:00
|
|
|
INSIST(sock->tid == isc_nm_tid());
|
2020-09-11 10:53:31 +02:00
|
|
|
INSIST(sock->recv_cb != NULL);
|
2020-09-05 11:07:40 -07:00
|
|
|
|
2020-09-11 10:53:31 +02:00
|
|
|
cb = sock->recv_cb;
|
|
|
|
cbarg = sock->recv_cbarg;
|
|
|
|
|
|
|
|
cb(nmhandle, ISC_R_SUCCESS, ®ion, cbarg);
|
|
|
|
|
2020-04-29 15:19:32 +02:00
|
|
|
if (free_buf) {
|
|
|
|
isc__nm_free_uvbuf(sock, buf);
|
|
|
|
}
|
2019-11-05 13:55:54 -08:00
|
|
|
|
2020-06-04 23:13:54 -07:00
|
|
|
/*
|
|
|
|
* The sock is now attached to the handle, we can detach our ref.
|
|
|
|
*/
|
|
|
|
isc__nmsocket_detach(&sock);
|
|
|
|
|
2019-11-05 13:55:54 -08:00
|
|
|
/*
|
|
|
|
* If the recv callback wants to hold on to the handle,
|
|
|
|
* it needs to attach to it.
|
|
|
|
*/
|
2020-09-05 11:07:40 -07:00
|
|
|
if (!connected) {
|
|
|
|
isc_nmhandle_detach(&nmhandle);
|
|
|
|
}
|
2019-11-05 13:55:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-09-03 13:31:27 -07:00
|
|
|
* Send the data in 'region' to a peer via a UDP socket. We try to find
|
|
|
|
* a proper sibling/child socket so that we won't have to jump to another
|
|
|
|
* thread.
|
2019-11-05 13:55:54 -08:00
|
|
|
*/
|
2020-10-21 12:52:09 +02:00
|
|
|
void
|
2020-02-12 13:59:18 +01:00
|
|
|
isc__nm_udp_send(isc_nmhandle_t *handle, isc_region_t *region, isc_nm_cb_t cb,
|
2020-02-13 14:44:37 -08:00
|
|
|
void *cbarg) {
|
|
|
|
isc_nmsocket_t *sock = handle->sock;
|
2020-06-10 11:32:39 +02:00
|
|
|
isc_nmsocket_t *psock = NULL, *rsock = sock;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_sockaddr_t *peer = &handle->peer;
|
2019-12-03 00:07:59 -08:00
|
|
|
isc__netievent_udpsend_t *ievent = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc__nm_uvreq_t *uvreq = NULL;
|
|
|
|
uint32_t maxudp = atomic_load(&sock->mgr->maxudp);
|
2020-06-10 11:32:39 +02:00
|
|
|
int ntid;
|
2019-11-05 13:55:54 -08:00
|
|
|
|
2020-10-21 12:52:09 +02:00
|
|
|
if (!isc__nmsocket_active(sock)) {
|
|
|
|
isc__nm_incstats(sock->mgr, sock->statsindex[STATID_SENDFAIL]);
|
|
|
|
cb(handle, ISC_R_CANCELED, cbarg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-05 13:55:54 -08:00
|
|
|
/*
|
2020-06-22 15:46:11 -07:00
|
|
|
* We're simulating a firewall blocking UDP packets bigger than
|
2020-01-17 11:42:35 +01:00
|
|
|
* 'maxudp' bytes, for testing purposes.
|
|
|
|
*
|
|
|
|
* The client would ordinarily have unreferenced the handle
|
|
|
|
* in the callback, but that won't happen in this case, so
|
|
|
|
* we need to do so here.
|
2019-11-05 13:55:54 -08:00
|
|
|
*/
|
|
|
|
if (maxudp != 0 && region->length > maxudp) {
|
2020-09-03 13:31:27 -07:00
|
|
|
isc_nmhandle_detach(&handle);
|
2020-10-21 12:52:09 +02:00
|
|
|
return;
|
2019-11-05 13:55:54 -08:00
|
|
|
}
|
|
|
|
|
2020-06-10 11:32:39 +02:00
|
|
|
if (sock->type == isc_nm_udpsocket && !atomic_load(&sock->client)) {
|
2019-11-05 13:55:54 -08:00
|
|
|
INSIST(sock->parent != NULL);
|
|
|
|
psock = sock->parent;
|
|
|
|
} else if (sock->type == isc_nm_udplistener) {
|
|
|
|
psock = sock;
|
2020-06-10 11:32:39 +02:00
|
|
|
} else if (!atomic_load(&sock->client)) {
|
2020-01-17 12:07:34 +01:00
|
|
|
INSIST(0);
|
|
|
|
ISC_UNREACHABLE();
|
2019-11-05 13:55:54 -08:00
|
|
|
}
|
|
|
|
|
2020-01-29 09:29:19 +01:00
|
|
|
/*
|
2020-02-18 13:38:41 -08:00
|
|
|
* If we're in the network thread, we can send directly. If the
|
|
|
|
* handle is associated with a UDP socket, we can reuse its thread
|
|
|
|
* (assuming CPU affinity). Otherwise, pick a thread at random.
|
2020-01-29 09:29:19 +01:00
|
|
|
*/
|
2019-11-05 13:55:54 -08:00
|
|
|
if (isc__nm_in_netthread()) {
|
|
|
|
ntid = isc_nm_tid();
|
2020-06-10 11:32:39 +02:00
|
|
|
} else if (sock->type == isc_nm_udpsocket &&
|
|
|
|
!atomic_load(&sock->client)) {
|
2020-01-29 09:29:19 +01:00
|
|
|
ntid = sock->tid;
|
2019-11-05 13:55:54 -08:00
|
|
|
} else {
|
2020-02-12 13:59:18 +01:00
|
|
|
ntid = (int)isc_random_uniform(sock->nchildren);
|
2019-11-05 13:55:54 -08:00
|
|
|
}
|
|
|
|
|
2020-06-10 11:32:39 +02:00
|
|
|
if (psock != NULL) {
|
|
|
|
rsock = &psock->children[ntid];
|
|
|
|
}
|
2019-11-05 13:55:54 -08:00
|
|
|
|
|
|
|
uvreq = isc__nm_uvreq_get(sock->mgr, sock);
|
2020-02-12 13:59:18 +01:00
|
|
|
uvreq->uvbuf.base = (char *)region->base;
|
2019-11-05 13:55:54 -08:00
|
|
|
uvreq->uvbuf.len = region->length;
|
|
|
|
|
2020-09-03 13:31:27 -07:00
|
|
|
isc_nmhandle_attach(handle, &uvreq->handle);
|
2019-11-05 13:55:54 -08:00
|
|
|
|
|
|
|
uvreq->cb.send = cb;
|
|
|
|
uvreq->cbarg = cbarg;
|
|
|
|
|
|
|
|
if (isc_nm_tid() == rsock->tid) {
|
|
|
|
/*
|
2020-10-21 12:52:09 +02:00
|
|
|
* If we're in the same thread as the socket we can send
|
|
|
|
* the data directly, but we still need to return errors
|
|
|
|
* via the callback for API consistency.
|
2019-11-05 13:55:54 -08:00
|
|
|
*/
|
2020-10-20 08:07:44 +02:00
|
|
|
isc_result_t result = udp_send_direct(rsock, uvreq, peer);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
2020-10-21 12:52:09 +02:00
|
|
|
isc__nm_incstats(rsock->mgr,
|
|
|
|
rsock->statsindex[STATID_SENDFAIL]);
|
2020-10-20 08:07:44 +02:00
|
|
|
uvreq->cb.send(uvreq->handle, result, uvreq->cbarg);
|
|
|
|
isc__nm_uvreq_put(&uvreq, sock);
|
|
|
|
}
|
2019-11-05 13:55:54 -08:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* We need to create an event and pass it using async channel
|
|
|
|
*/
|
|
|
|
ievent = isc__nm_get_ievent(sock->mgr, netievent_udpsend);
|
|
|
|
ievent->sock = rsock;
|
|
|
|
ievent->peer = *peer;
|
|
|
|
ievent->req = uvreq;
|
|
|
|
|
|
|
|
isc__nm_enqueue_ievent(&sock->mgr->workers[rsock->tid],
|
2020-02-12 13:59:18 +01:00
|
|
|
(isc__netievent_t *)ievent);
|
2019-11-05 13:55:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-09-03 13:31:27 -07:00
|
|
|
* Asynchronous 'udpsend' event handler: send a packet on a UDP socket.
|
2019-11-05 13:55:54 -08:00
|
|
|
*/
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc__nm_async_udpsend(isc__networker_t *worker, isc__netievent_t *ev0) {
|
2020-10-20 08:07:44 +02:00
|
|
|
isc_result_t result;
|
2020-02-12 13:59:18 +01:00
|
|
|
isc__netievent_udpsend_t *ievent = (isc__netievent_udpsend_t *)ev0;
|
2020-10-20 08:07:44 +02:00
|
|
|
isc_nmsocket_t *sock = ievent->sock;
|
|
|
|
isc__nm_uvreq_t *uvreq = ievent->req;
|
2019-11-05 13:55:54 -08:00
|
|
|
|
2020-10-20 08:07:44 +02:00
|
|
|
REQUIRE(sock->type == isc_nm_udpsocket);
|
|
|
|
REQUIRE(worker->id == sock->tid);
|
2019-11-05 13:55:54 -08:00
|
|
|
|
2020-09-05 11:07:40 -07:00
|
|
|
if (!isc__nmsocket_active(ievent->sock)) {
|
|
|
|
uvreq->cb.send(uvreq->handle, ISC_R_CANCELED, uvreq->cbarg);
|
|
|
|
isc__nm_uvreq_put(&uvreq, sock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-20 08:07:44 +02:00
|
|
|
result = udp_send_direct(sock, uvreq, &ievent->peer);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
isc__nm_incstats(sock->mgr, sock->statsindex[STATID_SENDFAIL]);
|
|
|
|
uvreq->cb.send(uvreq->handle, result, uvreq->cbarg);
|
|
|
|
isc__nm_uvreq_put(&uvreq, sock);
|
2019-11-05 13:55:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
udp_send_cb(uv_udp_send_t *req, int status) {
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
2019-11-05 13:55:54 -08:00
|
|
|
isc__nm_uvreq_t *uvreq = (isc__nm_uvreq_t *)req->data;
|
2020-10-20 08:07:44 +02:00
|
|
|
isc_nmsocket_t *sock = uvreq->sock;
|
2019-11-05 13:55:54 -08:00
|
|
|
|
|
|
|
REQUIRE(VALID_UVREQ(uvreq));
|
|
|
|
REQUIRE(VALID_NMHANDLE(uvreq->handle));
|
|
|
|
|
|
|
|
if (status < 0) {
|
|
|
|
result = isc__nm_uverr2result(status);
|
2020-10-20 08:07:44 +02:00
|
|
|
isc__nm_incstats(sock->mgr, sock->statsindex[STATID_SENDFAIL]);
|
2019-11-05 13:55:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
uvreq->cb.send(uvreq->handle, result, uvreq->cbarg);
|
|
|
|
isc__nm_uvreq_put(&uvreq, uvreq->sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* udp_send_direct sends buf to a peer on a socket. Sock has to be in
|
|
|
|
* the same thread as the callee.
|
|
|
|
*/
|
|
|
|
static isc_result_t
|
|
|
|
udp_send_direct(isc_nmsocket_t *sock, isc__nm_uvreq_t *req,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_sockaddr_t *peer) {
|
2020-09-05 11:07:40 -07:00
|
|
|
const struct sockaddr *sa = &peer->type.sa;
|
2020-10-20 08:07:44 +02:00
|
|
|
int r;
|
2019-11-05 13:55:54 -08:00
|
|
|
|
2020-10-20 08:07:44 +02:00
|
|
|
REQUIRE(VALID_NMSOCK(sock));
|
|
|
|
REQUIRE(VALID_UVREQ(req));
|
2019-11-05 13:55:54 -08:00
|
|
|
REQUIRE(sock->tid == isc_nm_tid());
|
|
|
|
REQUIRE(sock->type == isc_nm_udpsocket);
|
|
|
|
|
2020-06-22 15:46:11 -07:00
|
|
|
if (!isc__nmsocket_active(sock)) {
|
|
|
|
return (ISC_R_CANCELED);
|
|
|
|
}
|
2020-06-10 11:32:39 +02:00
|
|
|
|
2020-09-05 11:07:40 -07:00
|
|
|
#ifdef HAVE_UV_UDP_CONNECT
|
|
|
|
/*
|
|
|
|
* If we used uv_udp_connect() (and not the shim version for
|
|
|
|
* older versions of libuv), then the peer address has to be
|
|
|
|
* set to NULL or else uv_udp_send() could fail or assert,
|
|
|
|
* depending on the libuv version.
|
|
|
|
*/
|
|
|
|
if (atomic_load(&sock->connected)) {
|
|
|
|
sa = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2020-10-20 08:07:44 +02:00
|
|
|
r = uv_udp_send(&req->uv_req.udp_send, &sock->uv_handle.udp,
|
|
|
|
&req->uvbuf, 1, sa, udp_send_cb);
|
|
|
|
if (r < 0) {
|
|
|
|
return (isc__nm_uverr2result(r));
|
2019-11-05 13:55:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
2020-09-05 11:07:40 -07:00
|
|
|
|
|
|
|
static int
|
|
|
|
udp_connect_direct(isc_nmsocket_t *sock, isc__nm_uvreq_t *req) {
|
|
|
|
isc__networker_t *worker = NULL;
|
|
|
|
int uv_bind_flags = UV_UDP_REUSEADDR;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
REQUIRE(isc__nm_in_netthread());
|
2020-10-26 14:19:37 +01:00
|
|
|
REQUIRE(sock->tid == isc_nm_tid());
|
2020-09-05 11:07:40 -07:00
|
|
|
|
|
|
|
worker = &sock->mgr->workers[isc_nm_tid()];
|
|
|
|
|
|
|
|
atomic_store(&sock->connecting, true);
|
|
|
|
|
|
|
|
r = uv_udp_init(&worker->loop, &sock->uv_handle.udp);
|
|
|
|
if (r != 0) {
|
|
|
|
isc__nm_incstats(sock->mgr, sock->statsindex[STATID_OPENFAIL]);
|
|
|
|
/* Socket was never opened; no need for udp_close_direct() */
|
|
|
|
atomic_store(&sock->closed, true);
|
|
|
|
atomic_store(&sock->result, isc__nm_uverr2result(r));
|
|
|
|
atomic_store(&sock->connect_error, true);
|
|
|
|
return (r);
|
|
|
|
}
|
|
|
|
|
|
|
|
r = uv_udp_open(&sock->uv_handle.udp, sock->fd);
|
|
|
|
if (r != 0) {
|
|
|
|
isc__nm_incstats(sock->mgr, sock->statsindex[STATID_OPENFAIL]);
|
|
|
|
atomic_store(&sock->closed, true);
|
|
|
|
atomic_store(&sock->connect_error, true);
|
|
|
|
atomic_store(&sock->result, isc__nm_uverr2result(r));
|
|
|
|
return (r);
|
|
|
|
}
|
|
|
|
isc__nm_incstats(sock->mgr, sock->statsindex[STATID_OPEN]);
|
|
|
|
|
|
|
|
if (sock->iface->addr.type.sa.sa_family == AF_INET6) {
|
|
|
|
uv_bind_flags |= UV_UDP_IPV6ONLY;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = uv_udp_bind(&sock->uv_handle.udp, &sock->iface->addr.type.sa,
|
|
|
|
uv_bind_flags);
|
|
|
|
if (r != 0) {
|
|
|
|
isc__nm_incstats(sock->mgr, sock->statsindex[STATID_BINDFAIL]);
|
|
|
|
atomic_store(&sock->connect_error, true);
|
|
|
|
atomic_store(&sock->result, isc__nm_uverr2result(r));
|
|
|
|
udp_close_direct(sock);
|
|
|
|
return (r);
|
|
|
|
}
|
|
|
|
|
|
|
|
uv_handle_set_data(&sock->uv_handle.handle, sock);
|
|
|
|
|
|
|
|
r = isc_uv_udp_connect(&sock->uv_handle.udp, &req->peer.type.sa);
|
|
|
|
if (r != 0) {
|
|
|
|
isc__nm_incstats(sock->mgr,
|
|
|
|
sock->statsindex[STATID_CONNECTFAIL]);
|
|
|
|
atomic_store(&sock->connect_error, true);
|
|
|
|
atomic_store(&sock->result, isc__nm_uverr2result(r));
|
|
|
|
udp_close_direct(sock);
|
|
|
|
return (r);
|
|
|
|
}
|
|
|
|
isc__nm_incstats(sock->mgr, sock->statsindex[STATID_CONNECT]);
|
2020-10-26 12:30:54 +01:00
|
|
|
atomic_store(&sock->connecting, false);
|
2020-09-05 11:07:40 -07:00
|
|
|
|
|
|
|
#ifdef ISC_RECV_BUFFER_SIZE
|
|
|
|
uv_recv_buffer_size(&sock->uv_handle.handle,
|
|
|
|
&(int){ ISC_RECV_BUFFER_SIZE });
|
|
|
|
#endif
|
|
|
|
#ifdef ISC_SEND_BUFFER_SIZE
|
|
|
|
uv_send_buffer_size(&sock->uv_handle.handle,
|
|
|
|
&(int){ ISC_SEND_BUFFER_SIZE });
|
|
|
|
#endif
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Asynchronous 'udpconnect' call handler: open a new UDP socket and call
|
|
|
|
* the 'open' callback with a handle.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
isc__nm_async_udpconnect(isc__networker_t *worker, isc__netievent_t *ev0) {
|
|
|
|
isc__netievent_udpconnect_t *ievent =
|
|
|
|
(isc__netievent_udpconnect_t *)ev0;
|
|
|
|
isc_nmsocket_t *sock = ievent->sock;
|
|
|
|
isc__nm_uvreq_t *req = ievent->req;
|
|
|
|
isc_nmhandle_t *handle = NULL;
|
|
|
|
isc_nm_cb_t cb;
|
|
|
|
void *cbarg;
|
|
|
|
int r;
|
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
UNUSED(worker);
|
|
|
|
|
|
|
|
REQUIRE(sock->type == isc_nm_udpsocket);
|
|
|
|
REQUIRE(sock->iface != NULL);
|
|
|
|
REQUIRE(sock->parent == NULL);
|
|
|
|
REQUIRE(sock->tid == isc_nm_tid());
|
|
|
|
|
|
|
|
cb = sock->connect_cb;
|
|
|
|
cbarg = sock->connect_cbarg;
|
|
|
|
|
|
|
|
r = udp_connect_direct(sock, req);
|
|
|
|
if (r != 0) {
|
|
|
|
result = isc__nm_uverr2result(r);
|
|
|
|
} else {
|
|
|
|
atomic_store(&sock->connected, true);
|
|
|
|
atomic_store(&sock->result, ISC_R_SUCCESS);
|
|
|
|
result = atomic_load(&sock->result);
|
|
|
|
}
|
|
|
|
|
|
|
|
handle = isc__nmhandle_get(sock, &req->peer, &sock->iface->addr);
|
|
|
|
cb(handle, result, cbarg);
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
SIGNAL(&sock->cond);
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The sock is now attached to the handle.
|
|
|
|
*/
|
|
|
|
isc__nmsocket_detach(&sock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The connect callback should have attached to the handle.
|
|
|
|
* If it didn't, the socket will be closed now.
|
|
|
|
*/
|
|
|
|
isc_nmhandle_detach(&handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_nm_udpconnect(isc_nm_t *mgr, isc_nmiface_t *local, isc_nmiface_t *peer,
|
|
|
|
isc_nm_cb_t cb, void *cbarg, unsigned int timeout,
|
|
|
|
size_t extrahandlesize) {
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
isc_nmsocket_t *sock = NULL, *tmp = NULL;
|
|
|
|
isc__netievent_udpconnect_t *event = NULL;
|
|
|
|
isc__nm_uvreq_t *req = NULL;
|
|
|
|
sa_family_t sa_family;
|
|
|
|
|
|
|
|
REQUIRE(VALID_NM(mgr));
|
|
|
|
REQUIRE(local != NULL);
|
|
|
|
REQUIRE(peer != NULL);
|
|
|
|
|
|
|
|
sa_family = peer->addr.type.sa.sa_family;
|
|
|
|
|
|
|
|
sock = isc_mem_get(mgr->mctx, sizeof(isc_nmsocket_t));
|
|
|
|
isc__nmsocket_init(sock, mgr, isc_nm_udpsocket, local);
|
|
|
|
|
|
|
|
INSIST(sock->connect_cb == NULL && sock->connect_cbarg == NULL);
|
|
|
|
sock->connect_cb = cb;
|
|
|
|
sock->connect_cbarg = cbarg;
|
|
|
|
sock->read_timeout = timeout;
|
|
|
|
sock->extrahandlesize = extrahandlesize;
|
|
|
|
sock->peer = peer->addr;
|
|
|
|
atomic_init(&sock->client, true);
|
|
|
|
|
|
|
|
sock->fd = socket(sa_family, SOCK_DGRAM, 0);
|
|
|
|
RUNTIME_CHECK(sock->fd >= 0);
|
|
|
|
|
|
|
|
result = isc__nm_socket_reuse(sock->fd);
|
|
|
|
RUNTIME_CHECK(result == ISC_R_SUCCESS ||
|
|
|
|
result == ISC_R_NOTIMPLEMENTED);
|
|
|
|
|
|
|
|
result = isc__nm_socket_reuse_lb(sock->fd);
|
|
|
|
RUNTIME_CHECK(result == ISC_R_SUCCESS ||
|
|
|
|
result == ISC_R_NOTIMPLEMENTED);
|
|
|
|
|
|
|
|
(void)isc__nm_socket_incoming_cpu(sock->fd);
|
|
|
|
|
|
|
|
(void)isc__nm_socket_dontfrag(sock->fd, sa_family);
|
|
|
|
|
|
|
|
req = isc__nm_uvreq_get(mgr, sock);
|
|
|
|
req->cb.connect = cb;
|
|
|
|
req->cbarg = cbarg;
|
|
|
|
req->peer = peer->addr;
|
|
|
|
req->local = local->addr;
|
|
|
|
|
|
|
|
event = isc__nm_get_ievent(mgr, netievent_udpconnect);
|
|
|
|
event->sock = sock;
|
|
|
|
event->req = req;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hold an additional sock reference so async callbacks
|
|
|
|
* can't destroy it until we're ready.
|
|
|
|
*/
|
|
|
|
isc__nmsocket_attach(sock, &tmp);
|
|
|
|
|
|
|
|
if (isc__nm_in_netthread()) {
|
|
|
|
sock->tid = isc_nm_tid();
|
|
|
|
isc__nm_async_udpconnect(&mgr->workers[sock->tid],
|
|
|
|
(isc__netievent_t *)event);
|
|
|
|
isc__nm_put_ievent(mgr, event);
|
|
|
|
isc__nm_uvreq_put(&req, sock);
|
|
|
|
} else {
|
|
|
|
sock->tid = isc_random_uniform(mgr->nworkers);
|
|
|
|
isc__nm_enqueue_ievent(&mgr->workers[sock->tid],
|
|
|
|
(isc__netievent_t *)event);
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
while (!atomic_load(&sock->connected) &&
|
|
|
|
!atomic_load(&sock->connect_error)) {
|
|
|
|
WAIT(&sock->cond, &sock->lock);
|
|
|
|
}
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
isc__nm_uvreq_put(&req, sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (atomic_load(&sock->result) != ISC_R_SUCCESS) {
|
|
|
|
result = atomic_load(&sock->result);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc__nmsocket_detach(&tmp);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
udp_read_cb(uv_udp_t *handle, ssize_t nrecv, const uv_buf_t *buf,
|
|
|
|
const struct sockaddr *addr, unsigned flags) {
|
|
|
|
isc_nmsocket_t *sock = uv_handle_get_data((uv_handle_t *)handle);
|
|
|
|
|
2020-10-26 14:19:37 +01:00
|
|
|
if (sock->timer_running) {
|
|
|
|
uv_timer_stop(&sock->timer);
|
|
|
|
}
|
2020-09-05 11:07:40 -07:00
|
|
|
udp_recv_cb(handle, nrecv, buf, addr, flags);
|
|
|
|
uv_udp_recv_stop(&sock->uv_handle.udp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-10-26 12:30:54 +01:00
|
|
|
failed_read_cb(isc_nmsocket_t *sock, isc_result_t result) {
|
2020-09-05 11:07:40 -07:00
|
|
|
isc_nm_recv_cb_t cb;
|
|
|
|
void *cbarg = NULL;
|
|
|
|
|
2020-10-26 14:19:37 +01:00
|
|
|
REQUIRE(VALID_NMSOCK(sock));
|
|
|
|
REQUIRE(sock->statichandle != NULL);
|
|
|
|
|
2020-09-05 11:07:40 -07:00
|
|
|
uv_udp_recv_stop(&sock->uv_handle.udp);
|
|
|
|
|
2020-10-26 12:30:54 +01:00
|
|
|
if (sock->timer_initialized) {
|
|
|
|
uv_timer_stop(&sock->timer);
|
|
|
|
sock->timer_running = false;
|
|
|
|
}
|
|
|
|
|
2020-09-05 11:07:40 -07:00
|
|
|
cb = sock->recv_cb;
|
|
|
|
cbarg = sock->recv_cbarg;
|
|
|
|
isc__nmsocket_clearcb(sock);
|
|
|
|
|
|
|
|
if (cb != NULL) {
|
2020-10-26 12:30:54 +01:00
|
|
|
cb(sock->statichandle, result, NULL, cbarg);
|
2020-09-05 11:07:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 12:30:54 +01:00
|
|
|
static void
|
|
|
|
readtimeout_cb(uv_timer_t *handle) {
|
|
|
|
isc_nmsocket_t *sock = uv_handle_get_data((uv_handle_t *)handle);
|
|
|
|
|
|
|
|
REQUIRE(VALID_NMSOCK(sock));
|
|
|
|
REQUIRE(sock->tid == isc_nm_tid());
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Timeout; stop reading and process whatever we have.
|
|
|
|
*/
|
|
|
|
failed_read_cb(sock, ISC_R_TIMEDOUT);
|
|
|
|
}
|
|
|
|
|
2020-09-05 11:07:40 -07:00
|
|
|
/*
|
|
|
|
* Asynchronous 'udpread' call handler: start or resume reading on a socket;
|
|
|
|
* pause reading and call the 'recv' callback after each datagram.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
isc__nm_async_udpread(isc__networker_t *worker, isc__netievent_t *ev0) {
|
|
|
|
isc__netievent_udpread_t *ievent = (isc__netievent_udpread_t *)ev0;
|
|
|
|
isc_nmsocket_t *sock = ievent->sock;
|
|
|
|
|
|
|
|
REQUIRE(worker->id == isc_nm_tid());
|
|
|
|
if (sock->read_timeout != 0) {
|
|
|
|
if (!sock->timer_initialized) {
|
|
|
|
uv_timer_init(&worker->loop, &sock->timer);
|
|
|
|
uv_handle_set_data((uv_handle_t *)&sock->timer, sock);
|
|
|
|
sock->timer_initialized = true;
|
|
|
|
}
|
|
|
|
uv_timer_start(&sock->timer, readtimeout_cb, sock->read_timeout,
|
|
|
|
0);
|
|
|
|
sock->timer_running = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uv_udp_recv_start(&sock->uv_handle.udp, udp_alloc_cb, udp_read_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc__nm_udp_read(isc_nmhandle_t *handle, isc_nm_recv_cb_t cb, void *cbarg) {
|
|
|
|
isc_nmsocket_t *sock = NULL;
|
|
|
|
isc__netievent_startread_t *ievent = NULL;
|
|
|
|
|
|
|
|
REQUIRE(VALID_NMHANDLE(handle));
|
|
|
|
REQUIRE(VALID_NMSOCK(handle->sock));
|
|
|
|
REQUIRE(handle->sock->type == isc_nm_udpsocket);
|
|
|
|
|
|
|
|
sock = handle->sock;
|
|
|
|
|
|
|
|
REQUIRE(sock->tid == isc_nm_tid());
|
|
|
|
sock->recv_cb = cb;
|
|
|
|
sock->recv_cbarg = cbarg;
|
|
|
|
|
|
|
|
ievent = isc__nm_get_ievent(sock->mgr, netievent_udpread);
|
|
|
|
ievent->sock = sock;
|
|
|
|
|
|
|
|
if (sock->tid == isc_nm_tid()) {
|
|
|
|
isc__nm_async_udpread(&sock->mgr->workers[sock->tid],
|
|
|
|
(isc__netievent_t *)ievent);
|
|
|
|
isc__nm_put_ievent(sock->mgr, ievent);
|
|
|
|
} else {
|
|
|
|
isc__nm_enqueue_ievent(&sock->mgr->workers[sock->tid],
|
|
|
|
(isc__netievent_t *)ievent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
udp_close_cb(uv_handle_t *uvhandle) {
|
|
|
|
isc_nmsocket_t *sock = uv_handle_get_data(uvhandle);
|
|
|
|
|
|
|
|
REQUIRE(VALID_NMSOCK(sock));
|
|
|
|
|
|
|
|
isc__nm_incstats(sock->mgr, sock->statsindex[STATID_CLOSE]);
|
|
|
|
atomic_store(&sock->closed, true);
|
|
|
|
isc__nmsocket_prep_destroy(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
timer_close_cb(uv_handle_t *uvhandle) {
|
|
|
|
isc_nmsocket_t *sock = uv_handle_get_data(uvhandle);
|
|
|
|
|
|
|
|
REQUIRE(VALID_NMSOCK(sock));
|
|
|
|
|
|
|
|
uv_close(&sock->uv_handle.handle, udp_close_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
udp_close_direct(isc_nmsocket_t *sock) {
|
|
|
|
uv_udp_recv_stop(&sock->uv_handle.udp);
|
|
|
|
|
|
|
|
if (sock->timer_running) {
|
|
|
|
uv_timer_stop(&sock->timer);
|
|
|
|
sock->timer_running = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sock->timer_initialized) {
|
|
|
|
sock->timer_initialized = false;
|
|
|
|
uv_handle_set_data((uv_handle_t *)&sock->timer, sock);
|
|
|
|
uv_close((uv_handle_t *)&sock->timer, timer_close_cb);
|
|
|
|
} else {
|
|
|
|
uv_close(&sock->uv_handle.handle, udp_close_cb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isc__nm_async_udpclose(isc__networker_t *worker, isc__netievent_t *ev0) {
|
|
|
|
isc__netievent_udpclose_t *ievent = (isc__netievent_udpclose_t *)ev0;
|
|
|
|
isc_nmsocket_t *sock = ievent->sock;
|
|
|
|
|
|
|
|
REQUIRE(worker->id == ievent->sock->tid);
|
|
|
|
|
|
|
|
udp_close_direct(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isc__nm_udp_close(isc_nmsocket_t *sock) {
|
|
|
|
REQUIRE(VALID_NMSOCK(sock));
|
|
|
|
REQUIRE(sock->type == isc_nm_udpsocket);
|
|
|
|
|
2020-10-26 14:19:37 +01:00
|
|
|
REQUIRE(!isc__nmsocket_active(sock));
|
|
|
|
|
2020-09-05 11:07:40 -07:00
|
|
|
if (sock->tid == isc_nm_tid()) {
|
|
|
|
udp_close_direct(sock);
|
|
|
|
} else {
|
|
|
|
isc__netievent_udpclose_t *ievent =
|
|
|
|
isc__nm_get_ievent(sock->mgr, netievent_udpclose);
|
|
|
|
ievent->sock = sock;
|
|
|
|
isc__nm_enqueue_ievent(&sock->mgr->workers[sock->tid],
|
|
|
|
(isc__netievent_t *)ievent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 12:30:54 +01:00
|
|
|
void
|
|
|
|
isc__nm_udp_shutdown(isc_nmsocket_t *sock) {
|
|
|
|
REQUIRE(VALID_NMSOCK(sock));
|
|
|
|
REQUIRE(sock->tid == isc_nm_tid());
|
|
|
|
|
|
|
|
if (atomic_load(&sock->connecting)) {
|
|
|
|
isc__nm_uvreq_t *req = NULL;
|
|
|
|
|
|
|
|
atomic_store(&sock->connecting, false);
|
|
|
|
req = uv_handle_get_data((uv_handle_t *)&sock->timer);
|
|
|
|
uv_timer_stop(&sock->timer);
|
|
|
|
sock->timer_running = false;
|
|
|
|
|
|
|
|
isc__nmsocket_clearcb(sock);
|
|
|
|
if (sock->connect_cb != NULL) {
|
|
|
|
sock->connect_cb(NULL, ISC_R_CANCELED,
|
|
|
|
sock->connect_cbarg);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc__nm_uvreq_put(&req, sock);
|
|
|
|
isc__nmsocket_detach(&sock);
|
|
|
|
} else if (sock->type == isc_nm_udpsocket && sock->statichandle != NULL)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If the socket is active, mark it inactive and
|
|
|
|
* continue. If it isn't active, stop now.
|
|
|
|
*/
|
|
|
|
if (!isc__nmsocket_deactivate(sock)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
failed_read_cb(sock, ISC_R_CANCELED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-05 11:07:40 -07:00
|
|
|
void
|
|
|
|
isc__nm_udp_cancelread(isc_nmhandle_t *handle) {
|
|
|
|
isc_nmsocket_t *sock = NULL;
|
|
|
|
isc__netievent_udpcancel_t *ievent = NULL;
|
|
|
|
|
|
|
|
REQUIRE(VALID_NMHANDLE(handle));
|
|
|
|
|
|
|
|
sock = handle->sock;
|
|
|
|
|
|
|
|
REQUIRE(VALID_NMSOCK(sock));
|
|
|
|
REQUIRE(sock->type == isc_nm_udpsocket);
|
|
|
|
|
|
|
|
ievent = isc__nm_get_ievent(sock->mgr, netievent_udpcancel);
|
|
|
|
ievent->sock = sock;
|
|
|
|
isc_nmhandle_attach(handle, &ievent->handle);
|
|
|
|
|
|
|
|
isc__nm_enqueue_ievent(&sock->mgr->workers[sock->tid],
|
|
|
|
(isc__netievent_t *)ievent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isc__nm_async_udpcancel(isc__networker_t *worker, isc__netievent_t *ev0) {
|
|
|
|
isc__netievent_udpcancel_t *ievent = (isc__netievent_udpcancel_t *)ev0;
|
|
|
|
isc_nmsocket_t *sock = ievent->sock;
|
|
|
|
isc_nmhandle_t *handle = ievent->handle;
|
|
|
|
|
|
|
|
REQUIRE(worker->id == ievent->sock->tid);
|
|
|
|
|
|
|
|
uv_udp_recv_stop(&sock->uv_handle.udp);
|
|
|
|
|
|
|
|
if (atomic_load(&sock->client)) {
|
2020-10-26 12:30:54 +01:00
|
|
|
failed_read_cb(sock, ISC_R_EOF);
|
2020-09-05 11:07:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_nmhandle_detach(&handle);
|
|
|
|
}
|