1 //
2 // Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
3 // Copyright 2018 Capitar IT Group BV <info@capitar.com>
4 //
5 // This software is supplied under the terms of the MIT License, a
6 // copy of which should be located in the distribution where this
7 // file was obtained (LICENSE.txt). A copy of the license may also be
8 // found online at https://opensource.org/licenses/MIT.
9 //
10
11 #include <stdlib.h>
12
13 #include "core/nng_impl.h"
14 #include "nng/protocol/survey0/survey.h"
15
16 // Surveyor protocol. The SURVEYOR protocol is the "survey" side of the
17 // survey pattern. This is useful for building service discovery, voting, etc.
18 // Note that this pattern is not optimized for extreme low latency, as it makes
19 // multiple use of queues for simplicity. Typically this is used in cases
20 // where a few dozen extra microseconds does not matter.
21
22 typedef struct surv0_pipe surv0_pipe;
23 typedef struct surv0_sock surv0_sock;
24 typedef struct surv0_ctx surv0_ctx;
25
26 static void surv0_pipe_send_cb(void *);
27 static void surv0_pipe_recv_cb(void *);
28 static void surv0_ctx_timeout(void *);
29
30 struct surv0_ctx {
31 surv0_sock * sock;
32 uint32_t survey_id; // survey id
33 nni_timer_node timer;
34 nni_time expire;
35 nni_lmq recv_lmq;
36 nni_list recv_queue;
37 nni_atomic_int recv_buf;
38 nni_atomic_int survey_time;
39 int err;
40 };
41
42 // surv0_sock is our per-socket protocol private structure.
43 struct surv0_sock {
44 int ttl;
45 nni_list pipes;
46 nni_mtx mtx;
47 surv0_ctx ctx;
48 nni_id_map surveys;
49 nni_pollable writable;
50 nni_pollable readable;
51 nni_atomic_int send_buf;
52 };
53
54 // surv0_pipe is our per-pipe protocol private structure.
55 struct surv0_pipe {
56 nni_pipe * pipe;
57 surv0_sock * sock;
58 nni_lmq send_queue;
59 nni_list_node node;
60 nni_aio aio_send;
61 nni_aio aio_recv;
62 bool busy;
63 bool closed;
64 };
65
66 static void
surv0_ctx_abort(surv0_ctx * ctx,int err)67 surv0_ctx_abort(surv0_ctx *ctx, int err)
68 {
69 nni_aio * aio;
70 surv0_sock *sock = ctx->sock;
71
72 while ((aio = nni_list_first(&ctx->recv_queue)) != NULL) {
73 nni_list_remove(&ctx->recv_queue, aio);
74 nni_aio_finish_error(aio, err);
75 }
76 nni_lmq_flush(&ctx->recv_lmq);
77 if (ctx->survey_id != 0) {
78 nni_id_remove(&sock->surveys, ctx->survey_id);
79 ctx->survey_id = 0;
80 }
81 if (ctx == &sock->ctx) {
82 nni_pollable_clear(&sock->readable);
83 }
84 }
85
86 static void
surv0_ctx_close(surv0_ctx * ctx)87 surv0_ctx_close(surv0_ctx *ctx)
88 {
89 surv0_sock *sock = ctx->sock;
90
91 nni_mtx_lock(&sock->mtx);
92 surv0_ctx_abort(ctx, NNG_ECLOSED);
93 nni_mtx_unlock(&sock->mtx);
94 }
95
96 static void
surv0_ctx_fini(void * arg)97 surv0_ctx_fini(void *arg)
98 {
99 surv0_ctx *ctx = arg;
100
101 surv0_ctx_close(ctx);
102 nni_timer_cancel(&ctx->timer);
103 nni_lmq_fini(&ctx->recv_lmq);
104 }
105
106 static int
surv0_ctx_init(void * c,void * s)107 surv0_ctx_init(void *c, void *s)
108 {
109 surv0_ctx * ctx = c;
110 surv0_sock * sock = s;
111 int rv;
112 int len;
113 nng_duration tmo;
114
115 nni_aio_list_init(&ctx->recv_queue);
116 nni_atomic_init(&ctx->recv_buf);
117 nni_atomic_init(&ctx->survey_time);
118
119 if (ctx == &sock->ctx) {
120 len = 128;
121 tmo = NNI_SECOND; // survey timeout
122 } else {
123 len = nni_atomic_get(&sock->ctx.recv_buf);
124 tmo = nni_atomic_get(&sock->ctx.survey_time);
125 }
126
127 nni_atomic_set(&ctx->recv_buf, len);
128 nni_atomic_set(&ctx->survey_time, tmo);
129
130 ctx->sock = sock;
131
132 if ((rv = nni_lmq_init(&ctx->recv_lmq, len)) != 0) {
133 surv0_ctx_fini(ctx);
134 return (rv);
135 }
136 nni_timer_init(&ctx->timer, surv0_ctx_timeout, ctx);
137 return (0);
138 }
139
140 static void
surv0_ctx_cancel(nni_aio * aio,void * arg,int rv)141 surv0_ctx_cancel(nni_aio *aio, void *arg, int rv)
142 {
143 surv0_ctx * ctx = arg;
144 surv0_sock *sock = ctx->sock;
145 nni_mtx_lock(&sock->mtx);
146 if (nni_list_active(&ctx->recv_queue, aio)) {
147 nni_list_remove(&ctx->recv_queue, aio);
148 nni_aio_finish_error(aio, rv);
149 }
150 if (ctx->survey_id != 0) {
151 nni_id_remove(&sock->surveys, ctx->survey_id);
152 ctx->survey_id = 0;
153 }
154 nni_mtx_unlock(&sock->mtx);
155 }
156
157 static void
surv0_ctx_recv(void * arg,nni_aio * aio)158 surv0_ctx_recv(void *arg, nni_aio *aio)
159 {
160 surv0_ctx * ctx = arg;
161 surv0_sock *sock = ctx->sock;
162 nni_msg * msg;
163
164 if (nni_aio_begin(aio) != 0) {
165 return;
166 }
167
168 nni_mtx_lock(&sock->mtx);
169 if (ctx->survey_id == 0) {
170 nni_mtx_unlock(&sock->mtx);
171 nni_aio_finish_error(aio, NNG_ESTATE);
172 return;
173 }
174 again:
175 if (nni_lmq_getq(&ctx->recv_lmq, &msg) != 0) {
176 int rv;
177 if ((rv = nni_aio_schedule(aio, &surv0_ctx_cancel, ctx)) !=
178 0) {
179 nni_mtx_unlock(&sock->mtx);
180 nni_aio_finish_error(aio, rv);
181 return;
182 }
183 nni_list_append(&ctx->recv_queue, aio);
184 nni_mtx_unlock(&sock->mtx);
185 return;
186 }
187 if (nni_lmq_empty(&ctx->recv_lmq) && (ctx == &sock->ctx)) {
188 nni_pollable_clear(&sock->readable);
189 }
190 if ((msg = nni_msg_unique(msg)) == NULL) {
191 goto again;
192 }
193
194 nni_mtx_unlock(&sock->mtx);
195 nni_aio_finish_msg(aio, msg);
196 }
197
198 void
surv0_ctx_timeout(void * arg)199 surv0_ctx_timeout(void *arg)
200 {
201 surv0_ctx * ctx = arg;
202 surv0_sock *sock = ctx->sock;
203
204 nni_mtx_lock(&sock->mtx);
205 if (nni_clock() < ctx->expire) {
206 nni_mtx_unlock(&sock->mtx);
207 return;
208 }
209
210 // Abort any pending receives.
211 surv0_ctx_abort(ctx, NNG_ETIMEDOUT);
212 nni_mtx_unlock(&sock->mtx);
213 }
214
215 static void
surv0_ctx_send(void * arg,nni_aio * aio)216 surv0_ctx_send(void *arg, nni_aio *aio)
217 {
218 surv0_ctx * ctx = arg;
219 surv0_sock * sock = ctx->sock;
220 surv0_pipe * pipe;
221 nni_msg * msg = nni_aio_get_msg(aio);
222 size_t len = nni_msg_len(msg);
223 nni_time now = nni_clock();
224 nng_duration survey_time;
225 int rv;
226
227 if (nni_aio_begin(aio) != 0) {
228 return;
229 }
230
231 survey_time = nni_atomic_get(&ctx->survey_time);
232
233 nni_mtx_lock(&sock->mtx);
234
235 // Abort everything outstanding.
236 surv0_ctx_abort(ctx, NNG_ECANCELED);
237 nni_timer_cancel(&ctx->timer);
238
239 // Allocate the new ID.
240 if ((rv = nni_id_alloc(&sock->surveys, &ctx->survey_id, ctx)) != 0) {
241 nni_mtx_unlock(&sock->mtx);
242 nni_aio_finish_error(aio, rv);
243 return;
244 }
245 nni_msg_header_clear(msg);
246 nni_msg_header_append_u32(msg, (uint32_t) ctx->survey_id);
247
248 // From this point, we're committed to success. Note that we send
249 // regardless of whether there are any pipes or not. If no pipes,
250 // then it just gets discarded.
251 nni_aio_set_msg(aio, NULL);
252 NNI_LIST_FOREACH (&sock->pipes, pipe) {
253
254 // if the pipe isn't busy, then send this message direct.
255 if (!pipe->busy) {
256 pipe->busy = true;
257 nni_msg_clone(msg);
258 nni_aio_set_msg(&pipe->aio_send, msg);
259 nni_pipe_send(pipe->pipe, &pipe->aio_send);
260 } else if (!nni_lmq_full(&pipe->send_queue)) {
261 nni_msg_clone(msg);
262 nni_lmq_putq(&pipe->send_queue, msg);
263 }
264 }
265
266 ctx->expire = now + survey_time;
267 nni_timer_schedule(&ctx->timer, ctx->expire);
268
269 nni_mtx_unlock(&sock->mtx);
270 nni_msg_free(msg);
271
272 nni_aio_finish(aio, 0, len);
273 }
274
275 static void
surv0_sock_fini(void * arg)276 surv0_sock_fini(void *arg)
277 {
278 surv0_sock *sock = arg;
279
280 surv0_ctx_fini(&sock->ctx);
281 nni_id_map_fini(&sock->surveys);
282 nni_pollable_fini(&sock->writable);
283 nni_pollable_fini(&sock->readable);
284 nni_mtx_fini(&sock->mtx);
285 }
286
287 static int
surv0_sock_init(void * arg,nni_sock * s)288 surv0_sock_init(void *arg, nni_sock *s)
289 {
290 surv0_sock *sock = arg;
291 int rv;
292
293 NNI_ARG_UNUSED(s);
294
295 NNI_LIST_INIT(&sock->pipes, surv0_pipe, node);
296 nni_mtx_init(&sock->mtx);
297 nni_pollable_init(&sock->readable);
298 nni_pollable_init(&sock->writable);
299 // We are always writable.
300 nni_pollable_raise(&sock->writable);
301
302 // We allow for some buffering on a per pipe basis, to allow for
303 // multiple contexts to have surveys outstanding. It is recommended
304 // to increase this if many contexts will want to publish
305 // at nearly the same time.
306 nni_atomic_init(&sock->send_buf);
307 nni_atomic_set(&sock->send_buf, 8);
308
309 // Survey IDs are 32 bits, with the high order bit set.
310 // We start at a random point, to minimize likelihood of
311 // accidental collision across restarts.
312 nni_id_map_init(&sock->surveys, 0x80000000u, 0xffffffffu, true);
313
314 if ((rv = surv0_ctx_init(&sock->ctx, sock)) != 0) {
315 surv0_sock_fini(sock);
316 return (rv);
317 }
318
319 sock->ttl = 8;
320
321 return (0);
322 }
323
324 static void
surv0_sock_open(void * arg)325 surv0_sock_open(void *arg)
326 {
327 NNI_ARG_UNUSED(arg);
328 }
329
330 static void
surv0_sock_close(void * arg)331 surv0_sock_close(void *arg)
332 {
333 surv0_sock *s = arg;
334
335 surv0_ctx_close(&s->ctx);
336 }
337
338 static void
surv0_pipe_stop(void * arg)339 surv0_pipe_stop(void *arg)
340 {
341 surv0_pipe *p = arg;
342
343 nni_aio_stop(&p->aio_send);
344 nni_aio_stop(&p->aio_recv);
345 }
346
347 static void
surv0_pipe_fini(void * arg)348 surv0_pipe_fini(void *arg)
349 {
350 surv0_pipe *p = arg;
351
352 nni_aio_fini(&p->aio_send);
353 nni_aio_fini(&p->aio_recv);
354 nni_lmq_fini(&p->send_queue);
355 }
356
357 static int
surv0_pipe_init(void * arg,nni_pipe * pipe,void * s)358 surv0_pipe_init(void *arg, nni_pipe *pipe, void *s)
359 {
360 surv0_pipe *p = arg;
361 surv0_sock *sock = s;
362 int rv;
363 int len;
364
365 len = nni_atomic_get(&sock->send_buf);
366 nni_aio_init(&p->aio_send, surv0_pipe_send_cb, p);
367 nni_aio_init(&p->aio_recv, surv0_pipe_recv_cb, p);
368
369 // This depth could be tunable. The deeper the queue, the more
370 // concurrent surveys that can be delivered (multiple contexts).
371 // Note that surveys can be *outstanding*, but not yet put on the wire.
372 if ((rv = nni_lmq_init(&p->send_queue, len)) != 0) {
373 surv0_pipe_fini(p);
374 return (rv);
375 }
376
377 p->pipe = pipe;
378 p->sock = sock;
379 return (0);
380 }
381
382 static int
surv0_pipe_start(void * arg)383 surv0_pipe_start(void *arg)
384 {
385 surv0_pipe *p = arg;
386 surv0_sock *s = p->sock;
387
388 if (nni_pipe_peer(p->pipe) != NNG_SURVEYOR0_PEER) {
389 return (NNG_EPROTO);
390 }
391
392 nni_mtx_lock(&s->mtx);
393 nni_list_append(&s->pipes, p);
394 nni_mtx_unlock(&s->mtx);
395
396 nni_pipe_recv(p->pipe, &p->aio_recv);
397 return (0);
398 }
399
400 static void
surv0_pipe_close(void * arg)401 surv0_pipe_close(void *arg)
402 {
403 surv0_pipe *p = arg;
404 surv0_sock *s = p->sock;
405
406 nni_aio_close(&p->aio_send);
407 nni_aio_close(&p->aio_recv);
408
409 nni_mtx_lock(&s->mtx);
410 p->closed = true;
411 nni_lmq_flush(&p->send_queue);
412 if (nni_list_active(&s->pipes, p)) {
413 nni_list_remove(&s->pipes, p);
414 }
415 nni_mtx_unlock(&s->mtx);
416 }
417
418 static void
surv0_pipe_send_cb(void * arg)419 surv0_pipe_send_cb(void *arg)
420 {
421 surv0_pipe *p = arg;
422 surv0_sock *sock = p->sock;
423 nni_msg * msg;
424
425 if (nni_aio_result(&p->aio_send) != 0) {
426 nni_msg_free(nni_aio_get_msg(&p->aio_send));
427 nni_aio_set_msg(&p->aio_send, NULL);
428 nni_pipe_close(p->pipe);
429 return;
430 }
431
432 nni_mtx_lock(&sock->mtx);
433 if (p->closed) {
434 nni_mtx_unlock(&sock->mtx);
435 return;
436 }
437 if (nni_lmq_getq(&p->send_queue, &msg) == 0) {
438 nni_aio_set_msg(&p->aio_send, msg);
439 nni_pipe_send(p->pipe, &p->aio_send);
440 } else {
441 p->busy = false;
442 }
443 nni_mtx_unlock(&sock->mtx);
444 }
445
446 static void
surv0_pipe_recv_cb(void * arg)447 surv0_pipe_recv_cb(void *arg)
448 {
449 surv0_pipe *p = arg;
450 surv0_sock *sock = p->sock;
451 surv0_ctx * ctx;
452 nni_msg * msg;
453 uint32_t id;
454 nni_aio * aio;
455
456 if (nni_aio_result(&p->aio_recv) != 0) {
457 nni_pipe_close(p->pipe);
458 return;
459 }
460
461 msg = nni_aio_get_msg(&p->aio_recv);
462 nni_aio_set_msg(&p->aio_recv, NULL);
463 nni_msg_set_pipe(msg, nni_pipe_id(p->pipe));
464
465 // We yank 4 bytes of body, and move them to the header.
466 if (nni_msg_len(msg) < 4) {
467 // Peer sent us garbage. Kick it.
468 nni_msg_free(msg);
469 nni_pipe_close(p->pipe);
470 return;
471 }
472 id = nni_msg_trim_u32(msg);
473 nni_msg_header_append_u32(msg, id);
474
475 nni_mtx_lock(&sock->mtx);
476 // Best effort at delivery. Discard if no context or context is
477 // unable to receive it.
478 if (((ctx = nni_id_get(&sock->surveys, id)) == NULL) ||
479 (nni_lmq_full(&ctx->recv_lmq))) {
480 nni_msg_free(msg);
481 } else if ((aio = nni_list_first(&ctx->recv_queue)) != NULL) {
482 nni_list_remove(&ctx->recv_queue, aio);
483 nni_aio_finish_msg(aio, msg);
484 } else {
485 nni_lmq_putq(&ctx->recv_lmq, msg);
486 if (ctx == &sock->ctx) {
487 nni_pollable_raise(&sock->readable);
488 }
489 }
490 nni_mtx_unlock(&sock->mtx);
491
492 nni_pipe_recv(p->pipe, &p->aio_recv);
493 }
494
495 static int
surv0_ctx_set_survey_time(void * arg,const void * buf,size_t sz,nni_opt_type t)496 surv0_ctx_set_survey_time(
497 void *arg, const void *buf, size_t sz, nni_opt_type t)
498 {
499 surv0_ctx * ctx = arg;
500 nng_duration expire;
501 int rv;
502 if ((rv = nni_copyin_ms(&expire, buf, sz, t)) == 0) {
503 nni_atomic_set(&ctx->survey_time, expire);
504 }
505 return (rv);
506 }
507
508 static int
surv0_ctx_get_survey_time(void * arg,void * buf,size_t * szp,nni_opt_type t)509 surv0_ctx_get_survey_time(void *arg, void *buf, size_t *szp, nni_opt_type t)
510 {
511 surv0_ctx *ctx = arg;
512 return (
513 nni_copyout_ms(nni_atomic_get(&ctx->survey_time), buf, szp, t));
514 }
515
516 static int
surv0_sock_set_max_ttl(void * arg,const void * buf,size_t sz,nni_opt_type t)517 surv0_sock_set_max_ttl(void *arg, const void *buf, size_t sz, nni_opt_type t)
518 {
519 surv0_sock *s = arg;
520 return (nni_copyin_int(&s->ttl, buf, sz, 1, NNI_MAX_MAX_TTL, t));
521 }
522
523 static int
surv0_sock_get_max_ttl(void * arg,void * buf,size_t * szp,nni_opt_type t)524 surv0_sock_get_max_ttl(void *arg, void *buf, size_t *szp, nni_opt_type t)
525 {
526 surv0_sock *s = arg;
527 return (nni_copyout_int(s->ttl, buf, szp, t));
528 }
529
530 static int
surv0_sock_set_survey_time(void * arg,const void * buf,size_t sz,nni_opt_type t)531 surv0_sock_set_survey_time(
532 void *arg, const void *buf, size_t sz, nni_opt_type t)
533 {
534 surv0_sock *s = arg;
535 return (surv0_ctx_set_survey_time(&s->ctx, buf, sz, t));
536 }
537
538 static int
surv0_sock_get_survey_time(void * arg,void * buf,size_t * szp,nni_opt_type t)539 surv0_sock_get_survey_time(void *arg, void *buf, size_t *szp, nni_opt_type t)
540 {
541 surv0_sock *s = arg;
542 return (surv0_ctx_get_survey_time(&s->ctx, buf, szp, t));
543 }
544
545 static int
surv0_sock_get_send_fd(void * arg,void * buf,size_t * szp,nni_opt_type t)546 surv0_sock_get_send_fd(void *arg, void *buf, size_t *szp, nni_opt_type t)
547 {
548 surv0_sock *sock = arg;
549 int rv;
550 int fd;
551
552 if ((rv = nni_pollable_getfd(&sock->writable, &fd)) != 0) {
553 return (rv);
554 }
555 return (nni_copyout_int(fd, buf, szp, t));
556 }
557
558 static int
surv0_sock_get_recv_fd(void * arg,void * buf,size_t * szp,nni_opt_type t)559 surv0_sock_get_recv_fd(void *arg, void *buf, size_t *szp, nni_opt_type t)
560 {
561 surv0_sock *sock = arg;
562 int rv;
563 int fd;
564
565 if ((rv = nni_pollable_getfd(&sock->readable, &fd)) != 0) {
566 return (rv);
567 }
568 return (nni_copyout_int(fd, buf, szp, t));
569 }
570
571 static void
surv0_sock_recv(void * arg,nni_aio * aio)572 surv0_sock_recv(void *arg, nni_aio *aio)
573 {
574 surv0_sock *s = arg;
575 surv0_ctx_recv(&s->ctx, aio);
576 }
577
578 static void
surv0_sock_send(void * arg,nni_aio * aio)579 surv0_sock_send(void *arg, nni_aio *aio)
580 {
581 surv0_sock *s = arg;
582 surv0_ctx_send(&s->ctx, aio);
583 }
584
585 static nni_proto_pipe_ops surv0_pipe_ops = {
586 .pipe_size = sizeof(surv0_pipe),
587 .pipe_init = surv0_pipe_init,
588 .pipe_fini = surv0_pipe_fini,
589 .pipe_start = surv0_pipe_start,
590 .pipe_close = surv0_pipe_close,
591 .pipe_stop = surv0_pipe_stop,
592 };
593
594 static nni_option surv0_ctx_options[] = {
595 {
596 .o_name = NNG_OPT_SURVEYOR_SURVEYTIME,
597 .o_get = surv0_ctx_get_survey_time,
598 .o_set = surv0_ctx_set_survey_time,
599 },
600 {
601 .o_name = NULL,
602 }
603 };
604 static nni_proto_ctx_ops surv0_ctx_ops = {
605 .ctx_size = sizeof(surv0_ctx),
606 .ctx_init = surv0_ctx_init,
607 .ctx_fini = surv0_ctx_fini,
608 .ctx_send = surv0_ctx_send,
609 .ctx_recv = surv0_ctx_recv,
610 .ctx_options = surv0_ctx_options,
611 };
612
613 static nni_option surv0_sock_options[] = {
614 {
615 .o_name = NNG_OPT_SURVEYOR_SURVEYTIME,
616 .o_get = surv0_sock_get_survey_time,
617 .o_set = surv0_sock_set_survey_time,
618 },
619 {
620 .o_name = NNG_OPT_MAXTTL,
621 .o_get = surv0_sock_get_max_ttl,
622 .o_set = surv0_sock_set_max_ttl,
623 },
624 {
625 .o_name = NNG_OPT_RECVFD,
626 .o_get = surv0_sock_get_recv_fd,
627 },
628 {
629 .o_name = NNG_OPT_SENDFD,
630 .o_get = surv0_sock_get_send_fd,
631 },
632 // terminate list
633 {
634 .o_name = NULL,
635 },
636 };
637
638 static nni_proto_sock_ops surv0_sock_ops = {
639 .sock_size = sizeof(surv0_sock),
640 .sock_init = surv0_sock_init,
641 .sock_fini = surv0_sock_fini,
642 .sock_open = surv0_sock_open,
643 .sock_close = surv0_sock_close,
644 .sock_send = surv0_sock_send,
645 .sock_recv = surv0_sock_recv,
646 .sock_options = surv0_sock_options,
647 };
648
649 static nni_proto surv0_proto = {
650 .proto_version = NNI_PROTOCOL_VERSION,
651 .proto_self = { NNG_SURVEYOR0_SELF, NNG_SURVEYOR0_SELF_NAME },
652 .proto_peer = { NNG_SURVEYOR0_PEER, NNG_SURVEYOR0_PEER_NAME },
653 .proto_flags = NNI_PROTO_FLAG_SNDRCV,
654 .proto_sock_ops = &surv0_sock_ops,
655 .proto_pipe_ops = &surv0_pipe_ops,
656 .proto_ctx_ops = &surv0_ctx_ops,
657 };
658
659 int
nng_surveyor0_open(nng_socket * sock)660 nng_surveyor0_open(nng_socket *sock)
661 {
662 return (nni_proto_open(sock, &surv0_proto));
663 }
664