]>
git.proxmox.com Git - mirror_frr.git/blob - lib/frr_zmq.c
2 * libzebra ZeroMQ bindings
3 * Copyright (C) 2015 David Lamparter
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * IF YOU MODIFY THIS FILE PLEASE RUN `make check` and ensure that
22 * the test_zmq.c unit test is still working. There are dependencies
23 * between the two that are extremely fragile. My understanding
24 * is that there is specialized ownership of the cb pointer based
25 * upon what is happening. Those assumptions are supposed to be
26 * tested in the test_zmq.c
35 #include "lib_errors.h"
37 DEFINE_MTYPE_STATIC(LIB
, ZEROMQ_CB
, "ZeroMQ callback");
39 /* libzmq's context */
40 void *frrzmq_context
= NULL
;
41 static unsigned frrzmq_initcount
= 0;
43 void frrzmq_init(void)
45 if (frrzmq_initcount
++ == 0) {
46 frrzmq_context
= zmq_ctx_new();
47 zmq_ctx_set(frrzmq_context
, ZMQ_IPV6
, 1);
51 void frrzmq_finish(void)
53 if (--frrzmq_initcount
== 0) {
54 zmq_ctx_term(frrzmq_context
);
55 frrzmq_context
= NULL
;
59 static int frrzmq_read_msg(struct thread
*t
)
61 struct frrzmq_cb
**cbp
= THREAD_ARG(t
);
65 unsigned char read
= 0;
72 if (!cb
|| !cb
->zmqsock
)
76 zmq_pollitem_t polli
= {.socket
= cb
->zmqsock
,
77 .events
= ZMQ_POLLIN
};
78 ret
= zmq_poll(&polli
, 1, 0);
83 if (!(polli
.revents
& ZMQ_POLLIN
))
86 if (cb
->read
.cb_msg
) {
88 cb
->read
.cb_msg(cb
->read
.arg
, cb
->zmqsock
);
93 if (cb
->read
.cancelled
) {
94 frrzmq_check_events(cbp
, &cb
->write
,
96 cb
->read
.thread
= NULL
;
97 if (cb
->write
.cancelled
&& !cb
->write
.thread
)
98 XFREE(MTYPE_ZEROMQ_CB
, *cbp
);
106 if (zmq_msg_init(&msg
))
109 ret
= zmq_msg_recv(&msg
, cb
->zmqsock
, ZMQ_NOBLOCK
);
120 cb
->read
.cb_part(cb
->read
.arg
, cb
->zmqsock
, &msg
,
124 if (cb
->read
.cancelled
) {
126 frrzmq_check_events(cbp
, &cb
->write
,
128 cb
->read
.thread
= NULL
;
129 if (cb
->write
.cancelled
&& !cb
->write
.thread
)
130 XFREE(MTYPE_ZEROMQ_CB
, *cbp
);
135 /* cb_part may have read additional parts of the
136 * message; don't use zmq_msg_more here */
137 moresz
= sizeof(more
);
139 ret
= zmq_getsockopt(cb
->zmqsock
, ZMQ_RCVMORE
, &more
,
152 frrzmq_check_events(cbp
, &cb
->write
, ZMQ_POLLOUT
);
154 thread_add_read(t
->master
, frrzmq_read_msg
, cbp
,
155 cb
->fd
, &cb
->read
.thread
);
159 flog_err(EC_LIB_ZMQ
, "ZeroMQ read error: %s(%d)", strerror(errno
),
161 if (cb
->read
.cb_error
)
162 cb
->read
.cb_error(cb
->read
.arg
, cb
->zmqsock
);
166 int _frrzmq_thread_add_read(const struct xref_threadsched
*xref
,
167 struct thread_master
*master
,
168 void (*msgfunc
)(void *arg
, void *zmqsock
),
169 void (*partfunc
)(void *arg
, void *zmqsock
,
170 zmq_msg_t
*msg
, unsigned partnum
),
171 void (*errfunc
)(void *arg
, void *zmqsock
),
172 void *arg
, void *zmqsock
,
173 struct frrzmq_cb
**cbp
)
177 struct frrzmq_cb
*cb
;
181 if (!(msgfunc
|| partfunc
) || (msgfunc
&& partfunc
))
184 if (zmq_getsockopt(zmqsock
, ZMQ_FD
, &fd
, &len
))
186 len
= sizeof(events
);
187 if (zmq_getsockopt(zmqsock
, ZMQ_EVENTS
, &events
, &len
))
193 cb
= XCALLOC(MTYPE_ZEROMQ_CB
, sizeof(struct frrzmq_cb
));
194 cb
->write
.cancelled
= true;
198 cb
->zmqsock
= zmqsock
;
201 cb
->read
.cb_msg
= msgfunc
;
202 cb
->read
.cb_part
= partfunc
;
203 cb
->read
.cb_error
= errfunc
;
204 cb
->read
.cancelled
= false;
207 if (events
& ZMQ_POLLIN
) {
208 thread_cancel(&cb
->read
.thread
);
210 thread_add_event(master
, frrzmq_read_msg
, cbp
, fd
,
213 thread_add_read(master
, frrzmq_read_msg
, cbp
, fd
,
218 static int frrzmq_write_msg(struct thread
*t
)
220 struct frrzmq_cb
**cbp
= THREAD_ARG(t
);
221 struct frrzmq_cb
*cb
;
222 unsigned char written
= 0;
228 if (!cb
|| !cb
->zmqsock
)
232 zmq_pollitem_t polli
= {.socket
= cb
->zmqsock
,
233 .events
= ZMQ_POLLOUT
};
234 ret
= zmq_poll(&polli
, 1, 0);
239 if (!(polli
.revents
& ZMQ_POLLOUT
))
242 if (cb
->write
.cb_msg
) {
244 cb
->write
.cb_msg(cb
->write
.arg
, cb
->zmqsock
);
249 if (cb
->write
.cancelled
) {
250 frrzmq_check_events(cbp
, &cb
->read
, ZMQ_POLLIN
);
251 cb
->write
.thread
= NULL
;
252 if (cb
->read
.cancelled
&& !cb
->read
.thread
)
253 XFREE(MTYPE_ZEROMQ_CB
, *cbp
);
262 frrzmq_check_events(cbp
, &cb
->read
, ZMQ_POLLIN
);
264 thread_add_write(t
->master
, frrzmq_write_msg
, cbp
,
265 cb
->fd
, &cb
->write
.thread
);
269 flog_err(EC_LIB_ZMQ
, "ZeroMQ write error: %s(%d)", strerror(errno
),
271 if (cb
->write
.cb_error
)
272 cb
->write
.cb_error(cb
->write
.arg
, cb
->zmqsock
);
276 int _frrzmq_thread_add_write(const struct xref_threadsched
*xref
,
277 struct thread_master
*master
,
278 void (*msgfunc
)(void *arg
, void *zmqsock
),
279 void (*errfunc
)(void *arg
, void *zmqsock
),
280 void *arg
, void *zmqsock
, struct frrzmq_cb
**cbp
)
284 struct frrzmq_cb
*cb
;
291 if (zmq_getsockopt(zmqsock
, ZMQ_FD
, &fd
, &len
))
293 len
= sizeof(events
);
294 if (zmq_getsockopt(zmqsock
, ZMQ_EVENTS
, &events
, &len
))
300 cb
= XCALLOC(MTYPE_ZEROMQ_CB
, sizeof(struct frrzmq_cb
));
301 cb
->read
.cancelled
= true;
305 cb
->zmqsock
= zmqsock
;
308 cb
->write
.cb_msg
= msgfunc
;
309 cb
->write
.cb_part
= NULL
;
310 cb
->write
.cb_error
= errfunc
;
311 cb
->write
.cancelled
= false;
314 if (events
& ZMQ_POLLOUT
) {
315 thread_cancel(&cb
->write
.thread
);
317 _thread_add_event(xref
, master
, frrzmq_write_msg
, cbp
, fd
,
320 thread_add_write(master
, frrzmq_write_msg
, cbp
, fd
,
325 void frrzmq_thread_cancel(struct frrzmq_cb
**cb
, struct cb_core
*core
)
329 core
->cancelled
= true;
330 thread_cancel(&core
->thread
);
332 /* If cancelled from within a callback, don't try to free memory
338 /* Ok to free the callback context if no more ... context. */
339 if ((*cb
)->read
.cancelled
&& !(*cb
)->read
.thread
340 && (*cb
)->write
.cancelled
&& ((*cb
)->write
.thread
== NULL
))
341 XFREE(MTYPE_ZEROMQ_CB
, *cb
);
344 void frrzmq_check_events(struct frrzmq_cb
**cbp
, struct cb_core
*core
,
347 struct frrzmq_cb
*cb
;
354 if (!cb
|| !cb
->zmqsock
)
357 len
= sizeof(events
);
358 if (zmq_getsockopt(cb
->zmqsock
, ZMQ_EVENTS
, &events
, &len
))
360 if ((events
& event
) && core
->thread
&& !core
->cancelled
) {
361 struct thread_master
*tm
= core
->thread
->master
;
363 thread_cancel(&core
->thread
);
365 if (event
== ZMQ_POLLIN
)
366 thread_add_event(tm
, frrzmq_read_msg
,
367 cbp
, cb
->fd
, &core
->thread
);
369 thread_add_event(tm
, frrzmq_write_msg
,
370 cbp
, cb
->fd
, &core
->thread
);