summaryrefslogtreecommitdiffstats
path: root/lib/mgmt_msg.c
blob: aff9af7e8460a37058a2ff197f03a3f458185434 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
167
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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * March 6 2023, Christian Hopps <chopps@labn.net>
 *
 * Copyright (C) 2021  Vmware, Inc.
 *		       Pushpasis Sarkar <spushpasis@vmware.com>
 * Copyright (c) 2023, LabN Consulting, L.L.C.
 */
#include <zebra.h>
#include <sys/stat.h>

#include "debug.h"
#include "network.h"
#include "sockopt.h"
#include "stream.h"
#include "frrevent.h"
#include "mgmt_msg.h"
#include "mgmt_msg_native.h"


#define MGMT_MSG_DBG(dbgtag, fmt, ...)                                         \
	do {                                                                   \
		if (dbgtag)                                                    \
			zlog_debug("%s: %s: " fmt, dbgtag, __func__,           \
				   ##__VA_ARGS__);                             \
	} while (0)

#define MGMT_MSG_ERR(ms, fmt, ...)                                             \
	zlog_err("%s: %s: " fmt, (ms)->idtag, __func__, ##__VA_ARGS__)

DEFINE_MTYPE(LIB, MSG_CONN, "msg connection state");

/**
 * Read data from a socket into streams containing 1 or more full msgs headed by
 * mgmt_msg_hdr which contain API messages (currently protobuf).
 *
 * Args:
 *	ms: mgmt_msg_state for this process.
 *	fd: socket/file to read data from.
 *	debug: true to enable debug logging.
 *
 * Returns:
 *	MPP_DISCONNECT - socket should be closed and connect retried.
 *	MSV_SCHED_STREAM - this call should be rescheduled to run.
 *	MPP_SCHED_BOTH - this call and the procmsg buf should be scheduled to
 *run.
 */
enum mgmt_msg_rsched mgmt_msg_read(struct mgmt_msg_state *ms, int fd,
				   bool debug)
{
	const char *dbgtag = debug ? ms->idtag : NULL;
	size_t avail = STREAM_WRITEABLE(ms->ins);
	struct mgmt_msg_hdr *mhdr = NULL;
	size_t total = 0;
	size_t mcount = 0;
	ssize_t n, left;

	assert(ms && fd != -1);

	/*
	 * Read as much as we can into the stream.
	 */
	while (avail > sizeof(struct mgmt_msg_hdr)) {
		n = stream_read_try(ms->ins, fd, avail);

		/* -2 is normal nothing read, and to retry */
		if (n == -2) {
			MGMT_MSG_DBG(dbgtag, "nothing more to read");
			break;
		}
		if (n <= 0) {
			if (n == 0)
				MGMT_MSG_ERR(ms, "got EOF/disconnect");
			else
				MGMT_MSG_ERR(ms,
					     "got error while reading: '%s'",
					     safe_strerror(errno));
			return MSR_DISCONNECT;
		}
		MGMT_MSG_DBG(dbgtag, "read %zd bytes", n);
		ms->nrxb += n;
		avail -= n;
	}

	/*
	 * Check if we have read a complete messages or not.
	 */
	assert(stream_get_getp(ms->ins) == 0);
	left = stream_get_endp(ms->ins);
	while (left > (ssize_t)sizeof(struct mgmt_msg_hdr)) {
		mhdr = (struct mgmt_msg_hdr *)(STREAM_DATA(ms->ins) + total);
		if (!MGMT_MSG_IS_MARKER(mhdr->marker)) {
			MGMT_MSG_DBG(dbgtag, "recv corrupt buffer, disconnect");
			return MSR_DISCONNECT;
		}
		if ((ssize_t)mhdr->len > left)
			break;

		MGMT_MSG_DBG(dbgtag, "read full message len %u", mhdr->len);
		total += mhdr->len;
		left -= mhdr->len;
		mcount++;
	}

	if (!mcount) {
		/* Didn't manage to read a full message */
		if (mhdr && avail == 0) {
			struct stream *news;
			/*
			 * Message was longer than what was left and we have no
			 * available space to read more in. B/c mcount == 0 the
			 * message starts at the beginning of the stream so
			 * therefor the stream is too small to fit the message..
			 * Resize the stream to fit.
			 */
			if (mhdr->len > MGMT_MSG_MAX_MSG_ALLOC_LEN) {
				MGMT_MSG_ERR(ms, "corrupt msg len rcvd %u",
					     mhdr->len);
				return MSR_DISCONNECT;
			}
			news = stream_new(mhdr->len);
			stream_put(news, mhdr, left);
			stream_set_endp(news, left);
			stream_free(ms->ins);
			ms->ins = news;
		}
		return MSR_SCHED_STREAM;
	}

	/*
	 * We have read at least one message into the stream, queue it up.
	 */
	mhdr = (struct mgmt_msg_hdr *)(STREAM_DATA(ms->ins) + total);
	stream_set_endp(ms->ins, total);
	stream_fifo_push(&ms->inq, ms->ins);
	if (left < (ssize_t)sizeof(struct mgmt_msg_hdr))
		ms->ins = stream_new(ms->max_msg_sz);
	else
		/* handle case where message is greater than max */
		ms->ins = stream_new(MAX(ms->max_msg_sz, mhdr->len));
	if (left) {
		stream_put(ms->ins, mhdr, left);
		stream_set_endp(ms->ins, left);
	}

	return MSR_SCHED_BOTH;
}

/**
 * Process streams containing whole messages that have been pushed onto the
 * FIFO. This should be called from an event/timer handler and should be
 * reschedulable.
 *
 * Args:
 *	ms: mgmt_msg_state for this process.
 *	handle_mgs: function to call for each received message.
 *	user: opaque value passed through to handle_msg.
 *	debug: true to enable debug logging.
 *
 * Returns:
 *	true if more to process (so reschedule) else false
 */
bool mgmt_msg_procbufs(struct mgmt_msg_state *ms,
		       void (*handle_msg)(uint8_t version, uint8_t *msg,
					  size_t msglen, void *user),
		       void *user, bool debug)
{
	const char *dbgtag = debug ? ms->idtag : NULL;
	struct mgmt_msg_hdr *mhdr;
	struct stream *work;
	uint8_t *data;
	size_t left, nproc;

	MGMT_MSG_DBG(dbgtag, "Have %zu streams to process", ms->inq.count);

	nproc = 0;
	while (nproc < ms->max_read_buf) {
		work = stream_fifo_pop(&ms->inq);
		if (!work)
			break;

		data = STREAM_DATA(work);
		left = stream_get_endp(work);
		MGMT_MSG_DBG(dbgtag, "Processing stream of len %zu", left);

		for (; left > sizeof(struct mgmt_msg_hdr);
		     left -= mhdr->len, data += mhdr->len) {
			mhdr = (struct mgmt_msg_hdr *)data;

			assert(MGMT_MSG_IS_MARKER(mhdr->marker));
			assert(left >= mhdr->len);

			handle_msg(MGMT_MSG_MARKER_VERSION(mhdr->marker),
				   (uint8_t *)(mhdr + 1),
				   mhdr->len - sizeof(struct mgmt_msg_hdr),
				   user);
			ms->nrxm++;
			nproc++;
		}

		if (work != ms->ins)
			stream_free(work); /* Free it up */
		else
			stream_reset(work); /* Reset stream for next read */
	}

	/* return true if should reschedule b/c more to process. */
	return stream_fifo_head(&ms->inq) != NULL;
}

/**
 * Write data onto the socket, using streams that have been queued for
 * sending by mgmt_msg_send_msg. This function should be reschedulable.
 *
 * Args:
 *	ms: mgmt_msg_state for this process.
 *	fd: socket/file to read data from.
 *	debug: true to enable debug logging.
 *
 * Returns:
 *	MSW_SCHED_NONE - do not reschedule anything.
 *	MSW_SCHED_STREAM - this call should be rescheduled to run again.
 *	MSW_SCHED_WRITES_OFF - writes should be disabled with a timer to
 *	    re-enable them a short time later
 *	MSW_DISCONNECT - socket should be closed and reconnect retried.
 *run.
 */
enum mgmt_msg_wsched mgmt_msg_write(struct mgmt_msg_state *ms, int fd,
				    bool debug)
{
	const char *dbgtag = debug ? ms->idtag : NULL;
	struct stream *s;
	size_t nproc = 0;
	ssize_t left;
	ssize_t n;

	if (ms->outs) {
		MGMT_MSG_DBG(dbgtag,
			     "found unqueued stream with %zu bytes, queueing",
			     stream_get_endp(ms->outs));
		stream_fifo_push(&ms->outq, ms->outs);
		ms->outs = NULL;
	}

	for (s = stream_fifo_head(&ms->outq); s && nproc < ms->max_write_buf;
	     s = stream_fifo_head(&ms->outq)) {
		left = STREAM_READABLE(s);
		assert(left);

		n = stream_flush(s, fd);
		if (n <= 0) {
			if (n == 0)
				MGMT_MSG_ERR(ms,
					     "connection closed while writing");
			else if (ERRNO_IO_RETRY(errno)) {
				MGMT_MSG_DBG(
					dbgtag,
					"retry error while writing %zd bytes: %s (%d)",
					left, safe_strerror(errno), errno);
				return MSW_SCHED_STREAM;
			} else
				MGMT_MSG_ERR(
					ms,
					"error while writing %zd bytes: %s (%d)",
					left, safe_strerror(errno), errno);

			n = mgmt_msg_reset_writes(ms);
			MGMT_MSG_DBG(dbgtag, "drop and freed %zd streams", n);

			return MSW_DISCONNECT;
		}

		ms->ntxb += n;
		if (n != left) {
			MGMT_MSG_DBG(dbgtag, "short stream write %zd of %zd", n,
				     left);
			stream_forward_getp(s, n);
			return MSW_SCHED_STREAM;
		}

		stream_free(stream_fifo_pop(&ms->outq));
		MGMT_MSG_DBG(dbgtag, "wrote stream of %zd bytes", n);
		nproc++;
	}
	if (s) {
		MGMT_MSG_DBG(
			dbgtag,
			"reached %zu buffer writes, pausing with %zu streams left",
			ms->max_write_buf, ms->outq.count);
		return MSW_SCHED_STREAM;
	}
	MGMT_MSG_DBG(dbgtag, "flushed all streams from output q");
	return MSW_SCHED_NONE;
}


/**
 * Send a message by enqueueing it to be written over the socket by
 * mgmt_msg_write.
 *
 * Args:
 *	ms: mgmt_msg_state for this process.
 *	version: version of this message, will be given to receiving side.
 *	msg: the message to be sent.
 *	len: the length of the message.
 *	packf: a function to pack the message.
 *	debug: true to enable debug logging.
 *
 * Returns:
 *      0 on success, otherwise -1 on failure. The only failure mode is if a
 *      the message exceeds the maximum message size configured on init.
 */
int mgmt_msg_send_msg(struct mgmt_msg_state *ms, uint8_t version, void *msg,
		      size_t len, size_t (*packf)(void *msg, void *buf),
		      bool debug)
{
	const char *dbgtag = debug ? ms->idtag : NULL;
	struct mgmt_msg_hdr *mhdr;
	struct stream *s;
	uint8_t *dstbuf;
	size_t endp, n;
	size_t mlen = len + sizeof(*mhdr);

	if (mlen > ms->max_msg_sz)
		MGMT_MSG_DBG(dbgtag, "Sending large msg size %zu > max size %zu",
			     mlen, ms->max_msg_sz);

	if (!ms->outs) {
		MGMT_MSG_DBG(dbgtag, "creating new stream for msg len %zu", mlen);
		ms->outs = stream_new(MAX(ms->max_msg_sz, mlen));
	} else if (mlen > ms->max_msg_sz && ms->outs->endp == 0) {
		/* msg is larger than stream max size get a fit-to-size stream */
		MGMT_MSG_DBG(dbgtag,
			     "replacing old stream with fit-to-size for msg len %zu",
			     mlen);
		stream_free(ms->outs);
		ms->outs = stream_new(mlen);
	} else if (STREAM_WRITEABLE(ms->outs) < mlen) {
		MGMT_MSG_DBG(dbgtag,
			     "enq existing stream len %zu and creating new stream for msg len %zu",
			     STREAM_WRITEABLE(ms->outs), mlen);
		stream_fifo_push(&ms->outq, ms->outs);
		ms->outs = stream_new(MAX(ms->max_msg_sz, mlen));
	} else {
		MGMT_MSG_DBG(
			dbgtag,
			"using existing stream with avail %zu for msg len %zu",
			STREAM_WRITEABLE(ms->outs), mlen);
	}
	s = ms->outs;

	if (dbgtag && version == MGMT_MSG_VERSION_NATIVE) {
		struct mgmt_msg_header *native_msg = msg;

		MGMT_MSG_DBG(
			dbgtag,
			"Sending native msg sess/txn-id %"PRIu64" req-id %"PRIu64" code %u",
			native_msg->refer_id, native_msg->req_id, native_msg->code);

	}

	/* We have a stream with space, pack the message into it. */
	mhdr = (struct mgmt_msg_hdr *)(STREAM_DATA(s) + s->endp);
	mhdr->marker = MGMT_MSG_MARKER(version);
	mhdr->len = mlen;
	stream_forward_endp(s, sizeof(*mhdr));
	endp = stream_get_endp(s);
	dstbuf = STREAM_DATA(s) + endp;
	if (packf)
		n = packf(msg, dstbuf);
	else {
		memcpy(dstbuf, msg, len);
		n = len;
	}
	stream_set_endp(s, endp + n);
	ms->ntxm++;

	return 0;
}

/**
 * Create and open a unix domain stream socket on the given path
 * setting non-blocking and send and receive buffer sizes.
 *
 * Args:
 *	path: path of unix domain socket to connect to.
 *	sendbuf: size of socket send buffer.
 *	recvbuf: size of socket receive buffer.
 *	dbgtag: if non-NULL enable log debug, and use this tag.
 *
 * Returns:
 *	socket fd or -1 on error.
 */
int mgmt_msg_connect(const char *path, size_t sendbuf, size_t recvbuf,
		     const char *dbgtag)
{
	int ret, sock, len;
	struct sockaddr_un addr;

	MGMT_MSG_DBG(dbgtag, "connecting to server on %s", path);
	sock = socket(AF_UNIX, SOCK_STREAM, 0);
	if (sock < 0) {
		MGMT_MSG_DBG(dbgtag, "socket failed: %s", safe_strerror(errno));
		return -1;
	}

	memset(&addr, 0, sizeof(struct sockaddr_un));
	addr.sun_family = AF_UNIX;
	strlcpy(addr.sun_path, path, sizeof(addr.sun_path));
#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
	len = addr.sun_len = SUN_LEN(&addr);
#else
	len = sizeof(addr.sun_family) + strlen(addr.sun_path);
#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
	ret = connect(sock, (struct sockaddr *)&addr, len);
	if (ret < 0) {
		MGMT_MSG_DBG(dbgtag, "failed to connect on %s: %s", path,
			     safe_strerror(errno));
		close(sock);
		return -1;
	}

	MGMT_MSG_DBG(dbgtag, "connected to server on %s", path);
	set_nonblocking(sock);
	setsockopt_so_sendbuf(sock, sendbuf);
	setsockopt_so_recvbuf(sock, recvbuf);
	return sock;
}

/**
 * Reset the sending queue, by dequeueing all streams and freeing them. Return
 * the number of streams freed.
 *
 * Args:
 *	ms: mgmt_msg_state for this process.
 *
 * Returns:
 *      Number of streams that were freed.
 *
 */
size_t mgmt_msg_reset_writes(struct mgmt_msg_state *ms)
{
	struct stream *s;
	size_t nproc = 0;

	for (s = stream_fifo_pop(&ms->outq); s;
	     s = stream_fifo_pop(&ms->outq), nproc++)
		stream_free(s);

	return nproc;
}


void mgmt_msg_init(struct mgmt_msg_state *ms, size_t max_read_buf,
		   size_t max_write_buf, size_t max_msg_sz, const char *idtag)
{
	memset(ms, 0, sizeof(*ms));
	ms->ins = stream_new(max_msg_sz);
	stream_fifo_init(&ms->inq);
	stream_fifo_init(&ms->outq);
	ms->max_read_buf = max_write_buf;
	ms->max_write_buf = max_read_buf;
	ms->max_msg_sz = max_msg_sz;
	ms->idtag = strdup(idtag);
}

void mgmt_msg_destroy(struct mgmt_msg_state *ms)
{
	mgmt_msg_reset_writes(ms);
	if (ms->ins)
		stream_free(ms->ins);
	if (ms->outs)
		stream_free(ms->outs);
	free(ms->idtag);
}

/*
 * Connections
 */

#define MSG_CONN_DEFAULT_CONN_RETRY_MSEC 250
#define MSG_CONN_SEND_BUF_SIZE (1u << 16)
#define MSG_CONN_RECV_BUF_SIZE (1u << 16)

static void msg_client_sched_connect(struct msg_client *client,
				     unsigned long msec);

static void msg_conn_sched_proc_msgs(struct msg_conn *conn);
static void msg_conn_sched_read(struct msg_conn *conn);
static void msg_conn_sched_write(struct msg_conn *conn);

static void msg_conn_write(struct event *thread)
{
	struct msg_conn *conn = EVENT_ARG(thread);
	enum mgmt_msg_wsched rv;

	rv = mgmt_msg_write(&conn->mstate, conn->fd, conn->debug);
	if (rv == MSW_SCHED_STREAM)
		msg_conn_sched_write(conn);
	else if (rv == MSW_DISCONNECT)
		msg_conn_disconnect(conn, conn->is_client);
	else
		assert(rv == MSW_SCHED_NONE);
}

static void msg_conn_read(struct event *thread)
{
	struct msg_conn *conn = EVENT_ARG(thread);
	enum mgmt_msg_rsched rv;

	rv = mgmt_msg_read(&conn->mstate, conn->fd, conn->debug);
	if (rv == MSR_DISCONNECT) {
		msg_conn_disconnect(conn, conn->is_client);
		return;
	}
	if (rv == MSR_SCHED_BOTH)
		msg_conn_sched_proc_msgs(conn);
	msg_conn_sched_read(conn);
}

/* collapse this into mgmt_msg_procbufs */
static void msg_conn_proc_msgs(struct event *thread)
{
	struct msg_conn *conn = EVENT_ARG(thread);

	if (mgmt_msg_procbufs(&conn->mstate,
			      (void (*)(uint8_t, uint8_t *, size_t,
					void *))conn->handle_msg,
			      conn, conn->debug))
		/* there's more, schedule handling more */
		msg_conn_sched_proc_msgs(conn);
}

static void msg_conn_sched_read(struct msg_conn *conn)
{
	event_add_read(conn->loop, msg_conn_read, conn, conn->fd,
		       &conn->read_ev);
}

static void msg_conn_sched_write(struct msg_conn *conn)
{
	event_add_write(conn->loop, msg_conn_write, conn, conn->fd,
			&conn->write_ev);
}

static void msg_conn_sched_proc_msgs(struct msg_conn *conn)
{
	event_add_event(conn->loop, msg_conn_proc_msgs, conn, 0,
			&conn->proc_msg_ev);
}


void msg_conn_disconnect(struct msg_conn *conn, bool reconnect)
{

	/* disconnect short-circuit if present */
	if (conn->remote_conn) {
		conn->remote_conn->remote_conn = NULL;
		conn->remote_conn = NULL;
	}

	if (conn->fd != -1) {
		close(conn->fd);
		conn->fd = -1;

		/* Notify client through registered callback (if any) */
		if (conn->notify_disconnect)
			(void)(*conn->notify_disconnect)(conn);
	}

	if (reconnect) {
		assert(conn->is_client);
		msg_client_sched_connect(
			container_of(conn, struct msg_client, conn),
			MSG_CONN_DEFAULT_CONN_RETRY_MSEC);
	}
}

int msg_conn_send_msg(struct msg_conn *conn, uint8_t version, void *msg,
		      size_t mlen, size_t (*packf)(void *, void *),
		      bool short_circuit_ok)
{
	const char *dbgtag = conn->debug ? conn->mstate.idtag : NULL;

	if (conn->fd == -1) {
		MGMT_MSG_ERR(&conn->mstate,
			     "can't send message on closed connection");
		return -1;
	}

	/* immediately handle the message if short-circuit is present */
	if (conn->remote_conn && short_circuit_ok) {
		uint8_t *buf = msg;
		size_t n = mlen;
		bool old;

		if (packf) {
			buf = XMALLOC(MTYPE_TMP, mlen);
			n = packf(msg, buf);
		}

		++conn->short_circuit_depth;
		MGMT_MSG_DBG(dbgtag, "SC send: depth %u msg: %p",
			     conn->short_circuit_depth, msg);

		old = conn->remote_conn->is_short_circuit;
		conn->remote_conn->is_short_circuit = true;
		conn->remote_conn->handle_msg(version, buf, n,
					      conn->remote_conn);
		conn->remote_conn->is_short_circuit = old;

		--conn->short_circuit_depth;
		MGMT_MSG_DBG(dbgtag, "SC return from depth: %u msg: %p",
			     conn->short_circuit_depth, msg);

		if (packf)
			XFREE(MTYPE_TMP, buf);
		return 0;
	}

	int rv = mgmt_msg_send_msg(&conn->mstate, version, msg, mlen, packf,
				   conn->debug);

	msg_conn_sched_write(conn);

	return rv;
}

void msg_conn_cleanup(struct msg_conn *conn)
{
	struct mgmt_msg_state *ms = &conn->mstate;

	/* disconnect short-circuit if present */
	if (conn->remote_conn) {
		conn->remote_conn->remote_conn = NULL;
		conn->remote_conn = NULL;
	}

	if (conn->fd != -1) {
		close(conn->fd);
		conn->fd = -1;
	}

	EVENT_OFF(conn->read_ev);
	EVENT_OFF(conn->write_ev);
	EVENT_OFF(conn->proc_msg_ev);

	mgmt_msg_destroy(ms);
}

/*
 * Client Connections
 */

DECLARE_LIST(msg_server_list, struct msg_server, link);

static struct msg_server_list_head msg_servers;

static void msg_client_connect(struct msg_client *conn);

static void msg_client_connect_timer(struct event *thread)
{
	msg_client_connect(EVENT_ARG(thread));
}

static void msg_client_sched_connect(struct msg_client *client,
				     unsigned long msec)
{
	struct msg_conn *conn = &client->conn;
	const char *dbgtag = conn->debug ? conn->mstate.idtag : NULL;

	MGMT_MSG_DBG(dbgtag, "connection retry in %lu msec", msec);
	if (msec)
		event_add_timer_msec(conn->loop, msg_client_connect_timer,
				     client, msec, &client->conn_retry_tmr);
	else
		event_add_event(conn->loop, msg_client_connect_timer, client, 0,
				&client->conn_retry_tmr);
}

static int msg_client_connect_short_circuit(struct msg_client *client)
{
	struct msg_conn *server_conn;
	struct msg_server *server;
	const char *dbgtag =
		client->conn.debug ? client->conn.mstate.idtag : NULL;
	union sockunion su = {};
	int sockets[2];

	frr_each (msg_server_list, &msg_servers, server)
		if (!strcmp(server->sopath, client->sopath))
			break;
	if (!server) {
		MGMT_MSG_DBG(dbgtag,
			     "no short-circuit server available yet for %s",
			     client->sopath);
		return -1;
	}

	if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets)) {
		MGMT_MSG_ERR(
			&client->conn.mstate,
			"socketpair failed trying to short-circuit connection on %s: %s",
			client->sopath, safe_strerror(errno));
		return -1;
	}

	/* client side */
	client->conn.fd = sockets[0];
	set_nonblocking(sockets[0]);
	setsockopt_so_sendbuf(sockets[0], client->conn.mstate.max_write_buf);
	setsockopt_so_recvbuf(sockets[0], client->conn.mstate.max_read_buf);

	/* server side */
	memset(&su, 0, sizeof(union sockunion));
	server_conn = server->create(sockets[1], &su);
	server_conn->debug = DEBUG_MODE_CHECK(server->debug, DEBUG_MODE_ALL)
				     ? true
				     : false;

	client->conn.remote_conn = server_conn;
	server_conn->remote_conn = &client->conn;

	MGMT_MSG_DBG(
		dbgtag,
		"short-circuit connection on %s server %s:%d to client %s:%d",
		client->sopath, server_conn->mstate.idtag, server_conn->fd,
		client->conn.mstate.idtag, client->conn.fd);

	MGMT_MSG_DBG(
		server_conn->debug ? server_conn->mstate.idtag : NULL,
		"short-circuit connection on %s client %s:%d to server %s:%d",
		client->sopath, client->conn.mstate.idtag, client->conn.fd,
		server_conn->mstate.idtag, server_conn->fd);

	return 0;
}


/* Connect and start reading from the socket */
static void msg_client_connect(struct msg_client *client)
{
	struct msg_conn *conn = &client->conn;
	const char *dbgtag = conn->debug ? conn->mstate.idtag : NULL;

	if (!client->short_circuit_ok)
		conn->fd =
			mgmt_msg_connect(client->sopath, MSG_CONN_SEND_BUF_SIZE,
					 MSG_CONN_RECV_BUF_SIZE, dbgtag);
	else if (msg_client_connect_short_circuit(client))
		conn->fd = -1;

	if (conn->fd == -1)
		/* retry the connection */
		msg_client_sched_connect(client,
					 MSG_CONN_DEFAULT_CONN_RETRY_MSEC);
	else if (client->notify_connect && client->notify_connect(client))
		/* client connect notify failed */
		msg_conn_disconnect(conn, true);
	else
		/* start reading */
		msg_conn_sched_read(conn);
}

void msg_client_init(struct msg_client *client, struct event_loop *tm,
		     const char *sopath,
		     int (*notify_connect)(struct msg_client *client),
		     int (*notify_disconnect)(struct msg_conn *client),
		     void (*handle_msg)(uint8_t version, uint8_t *data,
					size_t len, struct msg_conn *client),
		     size_t max_read_buf, size_t max_write_buf,
		     size_t max_msg_sz, bool short_circuit_ok,
		     const char *idtag, bool debug)
{
	struct msg_conn *conn = &client->conn;
	memset(client, 0, sizeof(*client));

	conn->loop = tm;
	conn->fd = -1;
	conn->handle_msg = handle_msg;
	conn->notify_disconnect = notify_disconnect;
	conn->is_client = true;
	conn->debug = debug;
	client->short_circuit_ok = short_circuit_ok;
	client->sopath = strdup(sopath);
	client->notify_connect = notify_connect;

	mgmt_msg_init(&conn->mstate, max_read_buf, max_write_buf, max_msg_sz,
		      idtag);

	/* Start trying to connect to server */
	msg_client_sched_connect(client, 0);
}

void msg_client_cleanup(struct msg_client *client)
{
	assert(client->conn.is_client);

	EVENT_OFF(client->conn_retry_tmr);
	free(client->sopath);

	msg_conn_cleanup(&client->conn);
}


/*
 * Server-side connections
 */

static void msg_server_accept(struct event *event)
{
	struct msg_server *server = EVENT_ARG(event);
	struct msg_conn *conn;
	union sockunion su;
	int fd;

	if (server->fd < 0)
		return;

	/* We continue hearing server listen socket. */
	event_add_read(server->loop, msg_server_accept, server, server->fd,
		       &server->listen_ev);

	memset(&su, 0, sizeof(union sockunion));

	/* We can handle IPv4 or IPv6 socket. */
	fd = sockunion_accept(server->fd, &su);
	if (fd < 0) {
		zlog_err("Failed to accept %s client connection: %s",
			 server->idtag, safe_strerror(errno));
		return;
	}
	set_nonblocking(fd);
	set_cloexec(fd);

	DEBUGD(server->debug, "Accepted new %s connection", server->idtag);

	conn = server->create(fd, &su);
	if (conn)
		conn->debug = DEBUG_MODE_CHECK(server->debug, DEBUG_MODE_ALL)
				      ? true
				      : false;
}

int msg_server_init(struct msg_server *server, const char *sopath,
		    struct event_loop *loop,
		    struct msg_conn *(*create)(int fd, union sockunion *su),
		    const char *idtag, struct debug *debug)
{
	int ret;
	int sock;
	struct sockaddr_un addr;
	mode_t old_mask;

	memset(server, 0, sizeof(*server));
	server->fd = -1;

	sock = socket(AF_UNIX, SOCK_STREAM, PF_UNSPEC);
	if (sock < 0) {
		zlog_err("Failed to create %s server socket: %s", server->idtag,
			 safe_strerror(errno));
		goto fail;
	}

	addr.sun_family = AF_UNIX,
	strlcpy(addr.sun_path, sopath, sizeof(addr.sun_path));
	unlink(addr.sun_path);
	old_mask = umask(0077);
	ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
	if (ret < 0) {
		zlog_err("Failed to bind %s server socket to '%s': %s",
			 server->idtag, addr.sun_path, safe_strerror(errno));
		umask(old_mask);
		goto fail;
	}
	umask(old_mask);

	ret = listen(sock, MGMTD_MAX_CONN);
	if (ret < 0) {
		zlog_err("Failed to listen on %s server socket: %s",
			 server->idtag, safe_strerror(errno));
		goto fail;
	}

	server->fd = sock;
	server->loop = loop;
	server->sopath = strdup(sopath);
	server->idtag = strdup(idtag);
	server->create = create;
	server->debug = debug;

	msg_server_list_add_head(&msg_servers, server);

	event_add_read(server->loop, msg_server_accept, server, server->fd,
		       &server->listen_ev);


	DEBUGD(debug, "Started %s server, listening on %s", idtag, sopath);
	return 0;

fail:
	if (sock >= 0)
		close(sock);
	server->fd = -1;
	return -1;
}

void msg_server_cleanup(struct msg_server *server)
{
	DEBUGD(server->debug, "Closing %s server", server->idtag);

	if (server->listen_ev)
		EVENT_OFF(server->listen_ev);

	msg_server_list_del(&msg_servers, server);

	if (server->fd >= 0)
		close(server->fd);
	free((char *)server->sopath);
	free((char *)server->idtag);

	memset(server, 0, sizeof(*server));
	server->fd = -1;
}

/*
 * Initialize and start reading from the accepted socket
 *
 *     notify_connect - only called for disconnect i.e., connected == false
 */
void msg_conn_accept_init(struct msg_conn *conn, struct event_loop *tm, int fd,
			  int (*notify_disconnect)(struct msg_conn *conn),
			  void (*handle_msg)(uint8_t version, uint8_t *data,
					     size_t len, struct msg_conn *conn),
			  size_t max_read, size_t max_write, size_t max_size,
			  const char *idtag)
{
	conn->loop = tm;
	conn->fd = fd;
	conn->notify_disconnect = notify_disconnect;
	conn->handle_msg = handle_msg;
	conn->is_client = false;

	mgmt_msg_init(&conn->mstate, max_read, max_write, max_size, idtag);

	/* start reading */
	msg_conn_sched_read(conn);

	/* Make socket non-blocking.  */
	set_nonblocking(conn->fd);
	setsockopt_so_sendbuf(conn->fd, MSG_CONN_SEND_BUF_SIZE);
	setsockopt_so_recvbuf(conn->fd, MSG_CONN_RECV_BUF_SIZE);
}

struct msg_conn *
msg_server_conn_create(struct event_loop *tm, int fd,
		       int (*notify_disconnect)(struct msg_conn *conn),
		       void (*handle_msg)(uint8_t version, uint8_t *data,
					  size_t len, struct msg_conn *conn),
		       size_t max_read, size_t max_write, size_t max_size,
		       void *user, const char *idtag)
{
	struct msg_conn *conn = XMALLOC(MTYPE_MSG_CONN, sizeof(*conn));
	memset(conn, 0, sizeof(*conn));
	msg_conn_accept_init(conn, tm, fd, notify_disconnect, handle_msg,
			     max_read, max_write, max_size, idtag);
	conn->user = user;
	return conn;
}

void msg_server_conn_delete(struct msg_conn *conn)
{
	if (!conn)
		return;
	msg_conn_cleanup(conn);
	XFREE(MTYPE_MSG_CONN, conn);
}