tcp.c 17.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * 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
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * 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>
Evan Hunt's avatar
Evan Hunt committed
18
#include <isc/log.h>
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <isc/magic.h>
#include <isc/mem.h>
#include <isc/netmgr.h>
#include <isc/quota.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>

#include "netmgr-int.h"

static int
tcp_connect_direct(isc_nmsocket_t *sock, isc__nm_uvreq_t *req);

static void
tcp_close_direct(isc_nmsocket_t *sock);

static isc_result_t
tcp_send_direct(isc_nmsocket_t *sock, isc__nm_uvreq_t *req);
static void
tcp_connect_cb(uv_connect_t *uvreq, int status);

static void
tcp_connection_cb(uv_stream_t *server, int status);

static void
read_cb(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf);

static void
tcp_close_cb(uv_handle_t *uvhandle);

static int
tcp_connect_direct(isc_nmsocket_t *sock, isc__nm_uvreq_t *req) {
	isc__networker_t *worker;
	int r;

	REQUIRE(isc__nm_in_netthread());

	worker = &sock->mgr->workers[isc_nm_tid()];

	r = uv_tcp_init(&worker->loop, &sock->uv_handle.tcp);
	if (r != 0) {
		return (r);
	}

	if (req->local.length != 0) {
		r = uv_tcp_bind(&sock->uv_handle.tcp, &req->local.type.sa, 0);
		if (r != 0) {
			tcp_close_direct(sock);
			return (r);
		}
	}

	r = uv_tcp_connect(&req->uv_req.connect, &sock->uv_handle.tcp,
			   &req->peer.type.sa, tcp_connect_cb);
	return (r);
}

void
isc__nm_async_tcpconnect(isc__networker_t *worker, isc__netievent_t *ievent0) {
	isc__netievent_tcpconnect_t *ievent =
		(isc__netievent_tcpconnect_t *) ievent0;
	isc_nmsocket_t *sock = ievent->sock;
	isc__nm_uvreq_t *req = ievent->req;
	int r;

	REQUIRE(sock->type == isc_nm_tcpsocket);
	REQUIRE(worker->id == ievent->req->sock->mgr->workers[isc_nm_tid()].id);

	r = tcp_connect_direct(sock, req);
	if (r != 0) {
		/* We need to issue callbacks ourselves */
		tcp_connect_cb(&req->uv_req.connect, r);
	}
}

static void
tcp_connect_cb(uv_connect_t *uvreq, int status) {
	isc__nm_uvreq_t *req = (isc__nm_uvreq_t *) uvreq->data;
	isc_nmsocket_t *sock = uvreq->handle->data;

	REQUIRE(VALID_UVREQ(req));

	if (status == 0) {
		isc_result_t result;
		isc_nmhandle_t *handle = NULL;
		struct sockaddr_storage ss;

		uv_tcp_getpeername(&sock->uv_handle.tcp,
				   (struct sockaddr *) &ss,
				   &(int){sizeof(ss)});
		result = isc_sockaddr_fromsockaddr(&sock->peer,
						   (struct sockaddr *) &ss);
		RUNTIME_CHECK(result == ISC_R_SUCCESS);

		handle = isc__nmhandle_get(sock, NULL, NULL);
		req->cb.connect(handle, ISC_R_SUCCESS, req->cbarg);
	} else {
Evan Hunt's avatar
Evan Hunt committed
120
121
122
123
124
		/*
		 * TODO:
		 * Handle the connect error properly and free the socket.
		 */
		req->cb.connect(NULL, isc__nm_uverr2result(status), req->cbarg);
125
126
127
128
129
130
131
132
133
	}

	isc__nm_uvreq_put(&req, sock);
}

isc_result_t
isc_nm_listentcp(isc_nm_t *mgr, isc_nmiface_t *iface,
		 isc_nm_cb_t cb, void *cbarg,
		 size_t extrahandlesize, isc_quota_t *quota,
134
		 isc_nmsocket_t **sockp)
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
{
	isc__netievent_tcplisten_t *ievent = NULL;
	isc_nmsocket_t *nsock = NULL;

	REQUIRE(VALID_NM(mgr));

	nsock = isc_mem_get(mgr->mctx, sizeof(*nsock));
	isc__nmsocket_init(nsock, mgr, isc_nm_tcplistener);
	nsock->iface = iface;
	nsock->rcb.accept = cb;
	nsock->rcbarg = cbarg;
	nsock->extrahandlesize = extrahandlesize;
	if (quota != NULL) {
		/*
		 * We need to force it to make sure we get it attached.
		 * An example failure mode would be server under attack
		 * reconfiguring interfaces - that might cause weak attach
		 * to fail and leave this listening socket without limits.
		 * We can ignore the result.
		 */
		isc_quota_force(quota, &nsock->quota);
	}
	nsock->tid = isc_random_uniform(mgr->nworkers);

	/*
	 * Listening to TCP is rare enough not to care about the
	 * added overhead from passing this to another thread.
	 */
	ievent = isc__nm_get_ievent(mgr, netievent_tcplisten);
	ievent->sock = nsock;
	isc__nm_enqueue_ievent(&mgr->workers[nsock->tid],
			       (isc__netievent_t *) ievent);
167
	*sockp = nsock;
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245

	return (ISC_R_SUCCESS);
}

void
isc__nm_async_tcplisten(isc__networker_t *worker, isc__netievent_t *ievent0) {
	isc__netievent_tcplisten_t *ievent =
		(isc__netievent_tcplisten_t *) ievent0;
	isc_nmsocket_t *sock = ievent->sock;
	int r;

	REQUIRE(isc__nm_in_netthread());
	REQUIRE(sock->type == isc_nm_tcplistener);

	r = uv_tcp_init(&worker->loop, &sock->uv_handle.tcp);
	if (r != 0) {
		return;
	}

	uv_tcp_bind(&sock->uv_handle.tcp, &sock->iface->addr.type.sa, 0);
	r = uv_listen((uv_stream_t *) &sock->uv_handle.tcp, 10,
		      tcp_connection_cb);
	if (r != 0) {
		return;
	}

	atomic_store(&sock->listening, true);

	return;
}

void
isc_nm_tcp_stoplistening(isc_nmsocket_t *sock) {
	isc__netievent_tcpstoplisten_t *ievent = NULL;

	REQUIRE(VALID_NMSOCK(sock));
	REQUIRE(!isc__nm_in_netthread());

	ievent = isc__nm_get_ievent(sock->mgr, netievent_tcpstoplisten);
	isc_nmsocket_attach(sock, &ievent->sock);
	isc__nm_enqueue_ievent(&sock->mgr->workers[sock->tid],
			       (isc__netievent_t *) ievent);
}

static void
stoplistening_cb(uv_handle_t *handle) {
	isc_nmsocket_t *sock = handle->data;

	LOCK(&sock->lock);
	atomic_store(&sock->listening, false);
	atomic_store(&sock->closed, true);
	SIGNAL(&sock->cond);
	UNLOCK(&sock->lock);

	if (sock->quota != NULL) {
		isc_quota_detach(&sock->quota);
	}

	isc_nmsocket_detach(&sock);
}

void
isc__nm_async_tcpstoplisten(isc__networker_t *worker,
			    isc__netievent_t *ievent0)
{
	isc__netievent_tcpstoplisten_t *ievent =
		(isc__netievent_tcpstoplisten_t *) ievent0;
	isc_nmsocket_t *sock = ievent->sock;

	UNUSED(worker);

	REQUIRE(isc__nm_in_netthread());
	REQUIRE(VALID_NMSOCK(sock));
	REQUIRE(sock->type == isc_nm_tcplistener);

	uv_close(&sock->uv_handle.handle, stoplistening_cb);
}

Witold Kręcicki's avatar
Witold Kręcicki committed
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
static void
readtimeout_cb(uv_timer_t *handle) {
	isc_nmsocket_t *sock = (isc_nmsocket_t *) handle->data;

	REQUIRE(VALID_NMSOCK(sock));
	REQUIRE(sock->tid == isc_nm_tid());

	/*
	 * Socket is actively processing something, so restart the timer
	 * and return.
	 */
	if (atomic_load(&sock->processing)) {
		uv_timer_start(handle, readtimeout_cb, sock->read_timeout, 0);
		return;
	}

	/*
	 * Timeout; stop reading and process whatever we have.
	 */
	uv_read_stop(&sock->uv_handle.stream);
	if (sock->quota) {
		isc_quota_detach(&sock->quota);
	}
	sock->rcb.recv(sock->tcphandle, NULL, sock->rcbarg);
}

272
273
274
isc_result_t
isc_nm_read(isc_nmhandle_t *handle, isc_nm_recv_cb_t cb, void *cbarg) {
	isc_nmsocket_t *sock = NULL;
Witold Kręcicki's avatar
Witold Kręcicki committed
275
	isc__netievent_startread_t *ievent = NULL;
276
277
278
279
280
281

	REQUIRE(VALID_NMHANDLE(handle));
	REQUIRE(VALID_NMSOCK(handle->sock));

	sock = handle->sock;
	sock->rcb.recv = cb;
Witold Kręcicki's avatar
Witold Kręcicki committed
282
283
284
285
286
	sock->rcbarg = cbarg;

	ievent = isc__nm_get_ievent(sock->mgr, netievent_tcpstartread);
	ievent->sock = sock;

287
	if (sock->tid == isc_nm_tid()) {
Witold Kręcicki's avatar
Witold Kręcicki committed
288
289
290
		isc__nm_async_startread(&sock->mgr->workers[sock->tid],
					(isc__netievent_t *) ievent);
		isc__nm_put_ievent(sock->mgr, ievent);
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
	} else {
		isc__nm_enqueue_ievent(&sock->mgr->workers[sock->tid],
				       (isc__netievent_t *) ievent);
	}

	return (ISC_R_SUCCESS);
}

void
isc__nm_async_startread(isc__networker_t *worker, isc__netievent_t *ievent0) {
	isc__netievent_startread_t *ievent =
		(isc__netievent_startread_t *) ievent0;
	isc_nmsocket_t *sock = ievent->sock;

	REQUIRE(worker->id == isc_nm_tid());
Witold Kręcicki's avatar
Witold Kręcicki committed
306
307
308
309
310
311
312
313
314
	if (sock->read_timeout != 0) {
		if (!sock->timer_initialized) {
			uv_timer_init(&worker->loop, &sock->timer);
			sock->timer.data = sock;
			sock->timer_initialized = true;
		}
		uv_timer_start(&sock->timer, readtimeout_cb,
			       sock->read_timeout, 0);
	}
315
316
317
318
319
320

	uv_read_start(&sock->uv_handle.stream, isc__nm_alloc_cb, read_cb);
}

isc_result_t
isc_nm_pauseread(isc_nmsocket_t *sock) {
Witold Kręcicki's avatar
Witold Kręcicki committed
321
322
	isc__netievent_pauseread_t *ievent = NULL;

323
324
	REQUIRE(VALID_NMSOCK(sock));

325
326
327
328
329
	if (atomic_load(&sock->readpaused)) {
		return (ISC_R_SUCCESS);
	}

	atomic_store(&sock->readpaused, true);
Witold Kręcicki's avatar
Witold Kręcicki committed
330
331
	ievent = isc__nm_get_ievent(sock->mgr, netievent_tcppauseread);
	ievent->sock = sock;
332

333
	if (sock->tid == isc_nm_tid()) {
Witold Kręcicki's avatar
Witold Kręcicki committed
334
335
336
		isc__nm_async_pauseread(&sock->mgr->workers[sock->tid],
				       (isc__netievent_t *) ievent);
		isc__nm_put_ievent(sock->mgr, ievent);
337
338
339
340
341
342
343
344
345
346
347
348
349
350
	} else {
		isc__nm_enqueue_ievent(&sock->mgr->workers[sock->tid],
				       (isc__netievent_t *) ievent);
	}

	return (ISC_R_SUCCESS);
}

void
isc__nm_async_pauseread(isc__networker_t *worker, isc__netievent_t *ievent0) {
	isc__netievent_pauseread_t *ievent =
		(isc__netievent_pauseread_t *) ievent0;
	isc_nmsocket_t *sock = ievent->sock;

Witold Kręcicki's avatar
Witold Kręcicki committed
351
	REQUIRE(VALID_NMSOCK(sock));
352
353
	REQUIRE(worker->id == isc_nm_tid());

Witold Kręcicki's avatar
Witold Kręcicki committed
354
355
356
	if (sock->timer_initialized) {
		uv_timer_stop(&sock->timer);
	}
357
358
359
360
361
	uv_read_stop(&sock->uv_handle.stream);
}

isc_result_t
isc_nm_resumeread(isc_nmsocket_t *sock) {
Witold Kręcicki's avatar
Witold Kręcicki committed
362
363
	isc__netievent_startread_t *ievent = NULL;

364
365
366
	REQUIRE(VALID_NMSOCK(sock));
	REQUIRE(sock->rcb.recv != NULL);

367
368
369
370
371
372
	if (!atomic_load(&sock->readpaused)) {
		return (ISC_R_SUCCESS);
	}

	atomic_store(&sock->readpaused, false);

Witold Kręcicki's avatar
Witold Kręcicki committed
373
374
375
	ievent = isc__nm_get_ievent(sock->mgr, netievent_tcpstartread);
	ievent->sock = sock;

376
	if (sock->tid == isc_nm_tid()) {
Witold Kręcicki's avatar
Witold Kręcicki committed
377
378
379
		isc__nm_async_startread(&sock->mgr->workers[sock->tid],
					(isc__netievent_t *) ievent);
		isc__nm_put_ievent(sock->mgr, ievent);
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
	} else {
		isc__nm_enqueue_ievent(&sock->mgr->workers[sock->tid],
				       (isc__netievent_t *) ievent);
	}

	return (ISC_R_SUCCESS);
}

static void
read_cb(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
	isc_nmsocket_t *sock = stream->data;

	REQUIRE(VALID_NMSOCK(sock));
	REQUIRE(buf != NULL);

	if (nread >= 0) {
		isc_region_t region = {
			.base = (unsigned char *) buf->base,
			.length = nread
		};

		INSIST(sock->rcb.recv != NULL);
		sock->rcb.recv(sock->tcphandle, &region, sock->rcbarg);
403
404
405
406
407

		sock->read_timeout = (atomic_load(&sock->keepalive)
				      ? sock->mgr->keepalive
				      : sock->mgr->idle);

Witold Kręcicki's avatar
Witold Kręcicki committed
408
409
410
411
412
		if (sock->timer_initialized && sock->read_timeout != 0) {
			/* The timer will be updated */
			uv_timer_start(&sock->timer, readtimeout_cb,
				       sock->read_timeout, 0);
		}
413
414
415
416
417
418
419
420
421
422
423
		isc__nm_free_uvbuf(sock, buf);
		return;
	}

	isc__nm_free_uvbuf(sock, buf);
	if (sock->quota) {
		isc_quota_detach(&sock->quota);
	}
	sock->rcb.recv(sock->tcphandle, NULL, sock->rcbarg);

	/*
Evan Hunt's avatar
Evan Hunt committed
424
425
426
	 * We don't need to clean up now; the socket will be closed and
	 * resources and quota reclaimed when handle is freed in
	 * isc__nm_tcp_close().
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
	 */
}

static isc_result_t
accept_connection(isc_nmsocket_t *ssock) {
	isc_result_t result;
	isc_quota_t *quota = NULL;
	isc_nmsocket_t *csock = NULL;
	isc__networker_t *worker = NULL;
	isc_nmhandle_t *handle = NULL;
	struct sockaddr_storage ss;
	isc_sockaddr_t local;
	int r;

	REQUIRE(VALID_NMSOCK(ssock));
	REQUIRE(ssock->tid == isc_nm_tid());

	if (!atomic_load_relaxed(&ssock->active)) {
		/* We're closing, bail */
		return (ISC_R_CANCELED);
	}

	if (ssock->quota != NULL) {
		result = isc_quota_attach(ssock->quota, &quota);
		if (result != ISC_R_SUCCESS) {
			return (result);
		}
	}

	csock = isc_mem_get(ssock->mgr->mctx, sizeof(isc_nmsocket_t));
	isc__nmsocket_init(csock, ssock->mgr, isc_nm_tcpsocket);
	csock->tid = isc_nm_tid();
	csock->extrahandlesize = ssock->extrahandlesize;
	csock->iface = ssock->iface;
	csock->quota = quota;
	quota = NULL;

	worker = &ssock->mgr->workers[isc_nm_tid()];
	uv_tcp_init(&worker->loop, &csock->uv_handle.tcp);

	r = uv_accept(&ssock->uv_handle.stream, &csock->uv_handle.stream);
	if (r != 0) {
		if (csock->quota != NULL) {
			isc_quota_detach(&csock->quota);
		}
		isc_mem_put(ssock->mgr->mctx, csock, sizeof(isc_nmsocket_t));

		return (isc__nm_uverr2result(r));
	}

	isc_nmsocket_attach(ssock, &csock->server);

	uv_tcp_getpeername(&csock->uv_handle.tcp, (struct sockaddr *) &ss,
			   &(int){sizeof(ss)});

	result = isc_sockaddr_fromsockaddr(&csock->peer,
					   (struct sockaddr *) &ss);
	RUNTIME_CHECK(result == ISC_R_SUCCESS);
	uv_tcp_getsockname(&csock->uv_handle.tcp, (struct sockaddr *) &ss,
			   &(int){sizeof(ss)});
	result = isc_sockaddr_fromsockaddr(&local,
					   (struct sockaddr *) &ss);
	RUNTIME_CHECK(result == ISC_R_SUCCESS);

	handle = isc__nmhandle_get(csock, NULL, &local);

	INSIST(ssock->rcb.accept != NULL);
494
	csock->read_timeout = ssock->mgr->init;
495
496
497
498
499
500
501
502
503
	ssock->rcb.accept(handle, ISC_R_SUCCESS, ssock->rcbarg);
	isc_nmsocket_detach(&csock);

	return (ISC_R_SUCCESS);
}

static void
tcp_connection_cb(uv_stream_t *server, int status) {
	isc_nmsocket_t *ssock = server->data;
Evan Hunt's avatar
Evan Hunt committed
504
	isc_result_t result;
505
506
507

	UNUSED(status);

Evan Hunt's avatar
Evan Hunt committed
508
	result = accept_connection(ssock);
509
510
511
512
	if (result != ISC_R_SUCCESS) {
		if (result == ISC_R_QUOTA || result == ISC_R_SOFTQUOTA) {
			ssock->overquota = true;
		}
Evan Hunt's avatar
Evan Hunt committed
513
514
515
516
		isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
			      ISC_LOGMODULE_NETMGR, ISC_LOG_ERROR,
			      "TCP connection failed: %s",
			      isc_result_totext(result));
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
	}
}

isc_result_t
isc__nm_tcp_send(isc_nmhandle_t *handle, isc_region_t *region,
		 isc_nm_cb_t cb, void *cbarg)
{
	isc_nmsocket_t *sock = handle->sock;
	isc__netievent_tcpsend_t *ievent = NULL;
	isc__nm_uvreq_t *uvreq = NULL;

	REQUIRE(sock->type == isc_nm_tcpsocket);

	uvreq = isc__nm_uvreq_get(sock->mgr, sock);
	uvreq->uvbuf.base = (char *) region->base;
	uvreq->uvbuf.len = region->length;
	uvreq->handle = handle;
	isc_nmhandle_ref(uvreq->handle);
	uvreq->cb.send = cb;
	uvreq->cbarg = cbarg;

	if (sock->tid == isc_nm_tid()) {
		/*
		 * If we're in the same thread as the socket we can send the
		 * data directly
		 */
		return (tcp_send_direct(sock, uvreq));
	} else {
		/*
		 * We need to create an event and pass it using async channel
		 */
		ievent = isc__nm_get_ievent(sock->mgr, netievent_tcpsend);
		ievent->sock = sock;
		ievent->req = uvreq;
		isc__nm_enqueue_ievent(&sock->mgr->workers[sock->tid],
				       (isc__netievent_t *) ievent);
		return (ISC_R_SUCCESS);
	}

	return (ISC_R_UNEXPECTED);
}

static void
tcp_send_cb(uv_write_t *req, int status) {
	isc_result_t result = ISC_R_SUCCESS;
	isc__nm_uvreq_t *uvreq = (isc__nm_uvreq_t *) req->data;

	REQUIRE(VALID_UVREQ(uvreq));
	REQUIRE(VALID_NMHANDLE(uvreq->handle));

	if (status < 0) {
		result = isc__nm_uverr2result(status);
	}

	uvreq->cb.send(uvreq->handle, result, uvreq->cbarg);
	isc_nmhandle_unref(uvreq->handle);
	isc__nm_uvreq_put(&uvreq, uvreq->handle->sock);
}

/*
 * Handle 'tcpsend' async event - send a packet on the socket
 */
void
isc__nm_async_tcpsend(isc__networker_t *worker, isc__netievent_t *ievent0) {
	isc_result_t result;
	isc__netievent_tcpsend_t *ievent = (isc__netievent_tcpsend_t *) ievent0;

	REQUIRE(worker->id == ievent->sock->tid);

	if (!atomic_load(&ievent->sock->active)) {
		return;
	}

	result = tcp_send_direct(ievent->sock, ievent->req);
	if (result != ISC_R_SUCCESS) {
		ievent->req->cb.send(ievent->req->handle,
				     result, ievent->req->cbarg);
		isc__nm_uvreq_put(&ievent->req, ievent->req->handle->sock);
	}
}

static isc_result_t
tcp_send_direct(isc_nmsocket_t *sock, isc__nm_uvreq_t *req) {
	int r;

	REQUIRE(sock->tid == isc_nm_tid());
	REQUIRE(sock->type == isc_nm_tcpsocket);

	isc_nmhandle_ref(req->handle);
	r = uv_write(&req->uv_req.write, &sock->uv_handle.stream,
		     &req->uvbuf, 1, tcp_send_cb);
	if (r < 0) {
		req->cb.send(NULL, isc__nm_uverr2result(r), req->cbarg);
		isc__nm_uvreq_put(&req, sock);
		return (isc__nm_uverr2result(r));
	}

	return (ISC_R_SUCCESS);
}

static void
tcp_close_cb(uv_handle_t *uvhandle) {
	isc_nmsocket_t *sock = uvhandle->data;

	REQUIRE(VALID_NMSOCK(sock));

	atomic_store(&sock->closed, true);
	isc__nmsocket_prep_destroy(sock);
}

Witold Kręcicki's avatar
Witold Kręcicki committed
627
628
629
630
631
632
633
634
635
636
static void
timer_close_cb(uv_handle_t *uvhandle) {
	isc_nmsocket_t *sock = uvhandle->data;

	REQUIRE(VALID_NMSOCK(sock));

	isc_nmsocket_detach(&sock->server);
	uv_close(&sock->uv_handle.handle, tcp_close_cb);
}

637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
static void
tcp_close_direct(isc_nmsocket_t *sock) {
	REQUIRE(VALID_NMSOCK(sock));
	REQUIRE(sock->tid == isc_nm_tid());
	REQUIRE(sock->type == isc_nm_tcpsocket);

	if (sock->quota != NULL) {
		isc_nmsocket_t *ssock = sock->server;

		isc_quota_detach(&sock->quota);

		if (ssock->overquota) {
			isc_result_t result = accept_connection(ssock);
			if (result != ISC_R_QUOTA && result != ISC_R_SOFTQUOTA)
			{
				ssock->overquota = false;
			}
		}
	}
Witold Kręcicki's avatar
Witold Kręcicki committed
656
657
658
659
660
661
662
	if (sock->timer_initialized) {
		uv_close((uv_handle_t *)&sock->timer, timer_close_cb);
		sock->timer_initialized = false;
	} else {
		isc_nmsocket_detach(&sock->server);
		uv_close(&sock->uv_handle.handle, tcp_close_cb);
	}
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
}

void
isc__nm_tcp_close(isc_nmsocket_t *sock) {
	REQUIRE(VALID_NMSOCK(sock));
	REQUIRE(sock->type == isc_nm_tcpsocket);

	if (sock->tid == isc_nm_tid()) {
		tcp_close_direct(sock);
	} else {
		/*
		 * We need to create an event and pass it using async channel
		 */
		isc__netievent_tcpclose_t *ievent =
			isc__nm_get_ievent(sock->mgr, netievent_tcpclose);

		ievent->sock = sock;
		isc__nm_enqueue_ievent(&sock->mgr->workers[sock->tid],
				       (isc__netievent_t *) ievent);
	}
}

void
isc__nm_async_tcpclose(isc__networker_t *worker, isc__netievent_t *ievent0) {
	isc__netievent_tcpclose_t *ievent =
		(isc__netievent_tcpclose_t *) ievent0;

	REQUIRE(worker->id == ievent->sock->tid);

	tcp_close_direct(ievent->sock);
}