]>
git.proxmox.com Git - mirror_frr.git/blob - bfdd/control.c
1 /*********************************************************************
2 * Copyright 2017-2018 Network Device Education Foundation, Inc. ("NetDEF")
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along
15 * with this program; see the file COPYING; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * control.c: implements the BFD daemon control socket. It will be used
19 * to talk with clients daemon/scripts/consumers.
23 * Rafael Zalamena <rzalamena@opensourcerouting.org>
35 static int sock_set_nonblock(int fd
);
36 struct bfd_control_queue
*control_queue_new(struct bfd_control_socket
*bcs
);
37 static void control_queue_free(struct bfd_control_socket
*bcs
,
38 struct bfd_control_queue
*bcq
);
39 static int control_queue_dequeue(struct bfd_control_socket
*bcs
);
40 static int control_queue_enqueue(struct bfd_control_socket
*bcs
,
41 struct bfd_control_msg
*bcm
);
42 static int control_queue_enqueue_first(struct bfd_control_socket
*bcs
,
43 struct bfd_control_msg
*bcm
);
44 struct bfd_notify_peer
*control_notifypeer_new(struct bfd_control_socket
*bcs
,
45 struct bfd_session
*bs
);
46 static void control_notifypeer_free(struct bfd_control_socket
*bcs
,
47 struct bfd_notify_peer
*bnp
);
48 struct bfd_notify_peer
*control_notifypeer_find(struct bfd_control_socket
*bcs
,
49 struct bfd_session
*bs
);
52 struct bfd_control_socket
*control_new(int sd
);
53 static void control_free(struct bfd_control_socket
*bcs
);
54 static void control_reset_buf(struct bfd_control_buffer
*bcb
);
55 static int control_read(struct thread
*t
);
56 static int control_write(struct thread
*t
);
58 static void control_handle_request_add(struct bfd_control_socket
*bcs
,
59 struct bfd_control_msg
*bcm
);
60 static void control_handle_request_del(struct bfd_control_socket
*bcs
,
61 struct bfd_control_msg
*bcm
);
62 static int notify_add_cb(struct bfd_peer_cfg
*bpc
, void *arg
);
63 static int notify_del_cb(struct bfd_peer_cfg
*bpc
, void *arg
);
64 static void control_handle_notify_add(struct bfd_control_socket
*bcs
,
65 struct bfd_control_msg
*bcm
);
66 static void control_handle_notify_del(struct bfd_control_socket
*bcs
,
67 struct bfd_control_msg
*bcm
);
68 static void _control_handle_notify(struct hash_bucket
*hb
, void *arg
);
69 static void control_handle_notify(struct bfd_control_socket
*bcs
,
70 struct bfd_control_msg
*bcm
);
71 static void control_response(struct bfd_control_socket
*bcs
, uint16_t id
,
72 const char *status
, const char *error
);
74 static void _control_notify_config(struct bfd_control_socket
*bcs
,
75 const char *op
, struct bfd_session
*bs
);
76 static void _control_notify(struct bfd_control_socket
*bcs
,
77 struct bfd_session
*bs
);
83 static int sock_set_nonblock(int fd
)
87 flags
= fcntl(fd
, F_GETFL
, 0);
89 zlog_warn("%s: fcntl F_GETFL: %s", __func__
, strerror(errno
));
94 if (fcntl(fd
, F_SETFL
, flags
) == -1) {
95 zlog_warn("%s: fcntl F_SETFL: %s", __func__
, strerror(errno
));
102 int control_init(const char *path
)
106 struct sockaddr_un sun_
= {
107 .sun_family
= AF_UNIX
,
108 .sun_path
= BFDD_CONTROL_SOCKET
,
112 strlcpy(sun_
.sun_path
, path
, sizeof(sun_
.sun_path
));
114 /* Remove previously created sockets. */
115 unlink(sun_
.sun_path
);
117 sd
= socket(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
);
119 zlog_err("%s: socket: %s", __func__
, strerror(errno
));
124 if (bind(sd
, (struct sockaddr
*)&sun_
, sizeof(sun_
)) == -1) {
125 zlog_err("%s: bind: %s", __func__
, strerror(errno
));
131 if (listen(sd
, SOMAXCONN
) == -1) {
132 zlog_err("%s: listen: %s", __func__
, strerror(errno
));
137 sock_set_nonblock(sd
);
139 bglobal
.bg_csock
= sd
;
144 void control_shutdown(void)
146 struct bfd_control_socket
*bcs
;
148 thread_cancel(&bglobal
.bg_csockev
);
150 socket_close(&bglobal
.bg_csock
);
152 while (!TAILQ_EMPTY(&bglobal
.bg_bcslist
)) {
153 bcs
= TAILQ_FIRST(&bglobal
.bg_bcslist
);
158 int control_accept(struct thread
*t
)
160 int csock
, sd
= THREAD_FD(t
);
162 csock
= accept(sd
, NULL
, 0);
164 zlog_warn("%s: accept: %s", __func__
, strerror(errno
));
170 bglobal
.bg_csockev
= NULL
;
171 thread_add_read(master
, control_accept
, NULL
, sd
, &bglobal
.bg_csockev
);
180 struct bfd_control_socket
*control_new(int sd
)
182 struct bfd_control_socket
*bcs
;
184 bcs
= XCALLOC(MTYPE_BFDD_CONTROL
, sizeof(*bcs
));
186 /* Disable notifications by default. */
190 thread_add_read(master
, control_read
, bcs
, sd
, &bcs
->bcs_ev
);
192 TAILQ_INIT(&bcs
->bcs_bcqueue
);
193 TAILQ_INIT(&bcs
->bcs_bnplist
);
194 TAILQ_INSERT_TAIL(&bglobal
.bg_bcslist
, bcs
, bcs_entry
);
199 static void control_free(struct bfd_control_socket
*bcs
)
201 struct bfd_control_queue
*bcq
;
202 struct bfd_notify_peer
*bnp
;
204 thread_cancel(&(bcs
->bcs_ev
));
205 thread_cancel(&(bcs
->bcs_outev
));
209 TAILQ_REMOVE(&bglobal
.bg_bcslist
, bcs
, bcs_entry
);
211 /* Empty output queue. */
212 while (!TAILQ_EMPTY(&bcs
->bcs_bcqueue
)) {
213 bcq
= TAILQ_FIRST(&bcs
->bcs_bcqueue
);
214 control_queue_free(bcs
, bcq
);
217 /* Empty notification list. */
218 while (!TAILQ_EMPTY(&bcs
->bcs_bnplist
)) {
219 bnp
= TAILQ_FIRST(&bcs
->bcs_bnplist
);
220 control_notifypeer_free(bcs
, bnp
);
223 control_reset_buf(&bcs
->bcs_bin
);
224 XFREE(MTYPE_BFDD_CONTROL
, bcs
);
227 struct bfd_notify_peer
*control_notifypeer_new(struct bfd_control_socket
*bcs
,
228 struct bfd_session
*bs
)
230 struct bfd_notify_peer
*bnp
;
232 bnp
= control_notifypeer_find(bcs
, bs
);
236 bnp
= XCALLOC(MTYPE_BFDD_CONTROL
, sizeof(*bnp
));
238 TAILQ_INSERT_TAIL(&bcs
->bcs_bnplist
, bnp
, bnp_entry
);
245 static void control_notifypeer_free(struct bfd_control_socket
*bcs
,
246 struct bfd_notify_peer
*bnp
)
248 TAILQ_REMOVE(&bcs
->bcs_bnplist
, bnp
, bnp_entry
);
249 bnp
->bnp_bs
->refcount
--;
250 XFREE(MTYPE_BFDD_CONTROL
, bnp
);
253 struct bfd_notify_peer
*control_notifypeer_find(struct bfd_control_socket
*bcs
,
254 struct bfd_session
*bs
)
256 struct bfd_notify_peer
*bnp
;
258 TAILQ_FOREACH (bnp
, &bcs
->bcs_bnplist
, bnp_entry
) {
259 if (bnp
->bnp_bs
== bs
)
266 struct bfd_control_queue
*control_queue_new(struct bfd_control_socket
*bcs
)
268 struct bfd_control_queue
*bcq
;
270 bcq
= XCALLOC(MTYPE_BFDD_NOTIFICATION
, sizeof(*bcq
));
272 control_reset_buf(&bcq
->bcq_bcb
);
273 TAILQ_INSERT_TAIL(&bcs
->bcs_bcqueue
, bcq
, bcq_entry
);
278 static void control_queue_free(struct bfd_control_socket
*bcs
,
279 struct bfd_control_queue
*bcq
)
281 control_reset_buf(&bcq
->bcq_bcb
);
282 TAILQ_REMOVE(&bcs
->bcs_bcqueue
, bcq
, bcq_entry
);
283 XFREE(MTYPE_BFDD_NOTIFICATION
, bcq
);
286 static int control_queue_dequeue(struct bfd_control_socket
*bcs
)
288 struct bfd_control_queue
*bcq
;
290 /* List is empty, nothing to do. */
291 if (TAILQ_EMPTY(&bcs
->bcs_bcqueue
))
294 bcq
= TAILQ_FIRST(&bcs
->bcs_bcqueue
);
295 control_queue_free(bcs
, bcq
);
297 /* Get the next buffer to send. */
298 if (TAILQ_EMPTY(&bcs
->bcs_bcqueue
))
301 bcq
= TAILQ_FIRST(&bcs
->bcs_bcqueue
);
302 bcs
->bcs_bout
= &bcq
->bcq_bcb
;
304 bcs
->bcs_outev
= NULL
;
305 thread_add_write(master
, control_write
, bcs
, bcs
->bcs_sd
,
311 thread_cancel(&(bcs
->bcs_outev
));
312 bcs
->bcs_bout
= NULL
;
316 static int control_queue_enqueue(struct bfd_control_socket
*bcs
,
317 struct bfd_control_msg
*bcm
)
319 struct bfd_control_queue
*bcq
;
320 struct bfd_control_buffer
*bcb
;
322 bcq
= control_queue_new(bcs
);
325 bcb
->bcb_left
= sizeof(struct bfd_control_msg
) + ntohl(bcm
->bcm_length
);
329 /* If this is the first item, then dequeue and start using it. */
330 if (bcs
->bcs_bout
== NULL
) {
333 /* New messages, active write events. */
334 thread_add_write(master
, control_write
, bcs
, bcs
->bcs_sd
,
341 static int control_queue_enqueue_first(struct bfd_control_socket
*bcs
,
342 struct bfd_control_msg
*bcm
)
344 struct bfd_control_queue
*bcq
, *bcqn
;
345 struct bfd_control_buffer
*bcb
;
347 /* Enqueue it somewhere. */
348 if (control_queue_enqueue(bcs
, bcm
) == -1)
352 * The item is either the first or the last. So we must first
353 * check the best case where the item is already the first.
355 bcq
= TAILQ_FIRST(&bcs
->bcs_bcqueue
);
357 if (bcm
== bcb
->bcb_bcm
)
361 * The item was not the first, so it is the last. We'll try to
362 * assign it to the head of the queue, however if there is a
363 * transfer in progress, then we have to make the item as the
366 * Interrupting the transfer of in progress message will cause
367 * the client to lose track of the message position/data.
369 bcqn
= TAILQ_LAST(&bcs
->bcs_bcqueue
, bcqueue
);
370 TAILQ_REMOVE(&bcs
->bcs_bcqueue
, bcqn
, bcq_entry
);
371 if (bcb
->bcb_pos
!= 0) {
373 * First position is already being sent, insert into
376 TAILQ_INSERT_AFTER(&bcs
->bcs_bcqueue
, bcq
, bcqn
, bcq_entry
);
379 * Old message didn't start being sent, we still have
380 * time to put this one in the head of the queue.
382 TAILQ_INSERT_HEAD(&bcs
->bcs_bcqueue
, bcqn
, bcq_entry
);
383 bcb
= &bcqn
->bcq_bcb
;
390 static void control_reset_buf(struct bfd_control_buffer
*bcb
)
392 /* Get ride of old data. */
393 XFREE(MTYPE_BFDD_NOTIFICATION
, bcb
->bcb_buf
);
398 static int control_read(struct thread
*t
)
400 struct bfd_control_socket
*bcs
= THREAD_ARG(t
);
401 struct bfd_control_buffer
*bcb
= &bcs
->bcs_bin
;
402 int sd
= bcs
->bcs_sd
;
403 struct bfd_control_msg bcm
;
408 * Check if we have already downloaded message content, if so then skip
410 * download the rest of it and process.
412 * Otherwise download a new message header and allocate the necessary
415 if (bcb
->bcb_buf
!= NULL
)
418 bread
= read(sd
, &bcm
, sizeof(bcm
));
424 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
|| errno
== EINTR
)
425 goto schedule_next_read
;
427 zlog_warn("%s: read: %s", __func__
, strerror(errno
));
432 /* Validate header fields. */
433 plen
= ntohl(bcm
.bcm_length
);
435 zlog_debug("%s: client closed due small message length: %d",
436 __func__
, bcm
.bcm_length
);
441 if (bcm
.bcm_ver
!= BMV_VERSION_1
) {
442 zlog_debug("%s: client closed due bad version: %d", __func__
,
448 /* Prepare the buffer to load the message. */
449 bcs
->bcs_version
= bcm
.bcm_ver
;
450 bcs
->bcs_type
= bcm
.bcm_type
;
452 bcb
->bcb_pos
= sizeof(bcm
);
453 bcb
->bcb_left
= plen
;
454 bcb
->bcb_buf
= XMALLOC(MTYPE_BFDD_NOTIFICATION
,
455 sizeof(bcm
) + bcb
->bcb_left
+ 1);
456 if (bcb
->bcb_buf
== NULL
) {
457 zlog_warn("%s: not enough memory for message size: %zu",
458 __func__
, bcb
->bcb_left
);
463 memcpy(bcb
->bcb_buf
, &bcm
, sizeof(bcm
));
465 /* Terminate data string with NULL for later processing. */
466 bcb
->bcb_buf
[sizeof(bcm
) + bcb
->bcb_left
] = 0;
469 /* Download the remaining data of the message and process it. */
470 bread
= read(sd
, &bcb
->bcb_buf
[bcb
->bcb_pos
], bcb
->bcb_left
);
476 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
|| errno
== EINTR
)
477 goto schedule_next_read
;
479 zlog_warn("%s: read: %s", __func__
, strerror(errno
));
484 bcb
->bcb_pos
+= bread
;
485 bcb
->bcb_left
-= bread
;
486 /* We need more data, return to wait more. */
487 if (bcb
->bcb_left
> 0)
488 goto schedule_next_read
;
490 switch (bcb
->bcb_bcm
->bcm_type
) {
491 case BMT_REQUEST_ADD
:
492 control_handle_request_add(bcs
, bcb
->bcb_bcm
);
494 case BMT_REQUEST_DEL
:
495 control_handle_request_del(bcs
, bcb
->bcb_bcm
);
498 control_handle_notify(bcs
, bcb
->bcb_bcm
);
501 control_handle_notify_add(bcs
, bcb
->bcb_bcm
);
504 control_handle_notify_del(bcs
, bcb
->bcb_bcm
);
508 zlog_debug("%s: unhandled message type: %d", __func__
,
509 bcb
->bcb_bcm
->bcm_type
);
510 control_response(bcs
, bcb
->bcb_bcm
->bcm_id
, BCM_RESPONSE_ERROR
,
511 "invalid message type");
515 bcs
->bcs_version
= 0;
517 control_reset_buf(bcb
);
521 thread_add_read(master
, control_read
, bcs
, sd
, &bcs
->bcs_ev
);
526 static int control_write(struct thread
*t
)
528 struct bfd_control_socket
*bcs
= THREAD_ARG(t
);
529 struct bfd_control_buffer
*bcb
= bcs
->bcs_bout
;
530 int sd
= bcs
->bcs_sd
;
533 bwrite
= write(sd
, &bcb
->bcb_buf
[bcb
->bcb_pos
], bcb
->bcb_left
);
539 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
|| errno
== EINTR
) {
540 bcs
->bcs_outev
= NULL
;
541 thread_add_write(master
, control_write
, bcs
,
542 bcs
->bcs_sd
, &bcs
->bcs_outev
);
546 zlog_warn("%s: write: %s", __func__
, strerror(errno
));
551 bcb
->bcb_pos
+= bwrite
;
552 bcb
->bcb_left
-= bwrite
;
553 if (bcb
->bcb_left
> 0) {
554 bcs
->bcs_outev
= NULL
;
555 thread_add_write(master
, control_write
, bcs
, bcs
->bcs_sd
,
560 control_queue_dequeue(bcs
);
569 static void control_handle_request_add(struct bfd_control_socket
*bcs
,
570 struct bfd_control_msg
*bcm
)
572 const char *json
= (const char *)bcm
->bcm_data
;
574 if (config_request_add(json
) == 0)
575 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_OK
, NULL
);
577 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_ERROR
,
578 "request add failed");
581 static void control_handle_request_del(struct bfd_control_socket
*bcs
,
582 struct bfd_control_msg
*bcm
)
584 const char *json
= (const char *)bcm
->bcm_data
;
586 if (config_request_del(json
) == 0)
587 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_OK
, NULL
);
589 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_ERROR
,
590 "request del failed");
593 static struct bfd_session
*_notify_find_peer(struct bfd_peer_cfg
*bpc
)
595 struct peer_label
*pl
;
597 if (bpc
->bpc_has_label
) {
598 pl
= pl_find(bpc
->bpc_label
);
603 return bs_peer_find(bpc
);
606 static void _control_handle_notify(struct hash_bucket
*hb
, void *arg
)
608 struct bfd_control_socket
*bcs
= arg
;
609 struct bfd_session
*bs
= hb
->data
;
611 /* Notify peer configuration. */
612 if (bcs
->bcs_notify
& BCM_NOTIFY_CONFIG
)
613 _control_notify_config(bcs
, BCM_NOTIFY_CONFIG_ADD
, bs
);
615 /* Notify peer status. */
616 if (bcs
->bcs_notify
& BCM_NOTIFY_PEER_STATE
)
617 _control_notify(bcs
, bs
);
620 static void control_handle_notify(struct bfd_control_socket
*bcs
,
621 struct bfd_control_msg
*bcm
)
623 memcpy(&bcs
->bcs_notify
, bcm
->bcm_data
, sizeof(bcs
->bcs_notify
));
625 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_OK
, NULL
);
628 * If peer asked for notification configuration, send everything that
629 * was configured until the moment to sync up.
631 if (bcs
->bcs_notify
& (BCM_NOTIFY_CONFIG
| BCM_NOTIFY_PEER_STATE
))
632 bfd_id_iterate(_control_handle_notify
, bcs
);
635 static int notify_add_cb(struct bfd_peer_cfg
*bpc
, void *arg
)
637 struct bfd_control_socket
*bcs
= arg
;
638 struct bfd_session
*bs
= _notify_find_peer(bpc
);
643 control_notifypeer_new(bcs
, bs
);
645 /* Notify peer status. */
646 _control_notify(bcs
, bs
);
651 static int notify_del_cb(struct bfd_peer_cfg
*bpc
, void *arg
)
653 struct bfd_control_socket
*bcs
= arg
;
654 struct bfd_session
*bs
= _notify_find_peer(bpc
);
655 struct bfd_notify_peer
*bnp
;
660 bnp
= control_notifypeer_find(bcs
, bs
);
662 control_notifypeer_free(bcs
, bnp
);
667 static void control_handle_notify_add(struct bfd_control_socket
*bcs
,
668 struct bfd_control_msg
*bcm
)
670 const char *json
= (const char *)bcm
->bcm_data
;
672 if (config_notify_request(bcs
, json
, notify_add_cb
) == 0) {
673 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_OK
, NULL
);
677 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_ERROR
,
678 "failed to parse notify data");
681 static void control_handle_notify_del(struct bfd_control_socket
*bcs
,
682 struct bfd_control_msg
*bcm
)
684 const char *json
= (const char *)bcm
->bcm_data
;
686 if (config_notify_request(bcs
, json
, notify_del_cb
) == 0) {
687 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_OK
, NULL
);
691 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_ERROR
,
692 "failed to parse notify data");
697 * Internal functions used by the BFD daemon.
699 static void control_response(struct bfd_control_socket
*bcs
, uint16_t id
,
700 const char *status
, const char *error
)
702 struct bfd_control_msg
*bcm
;
706 /* Generate JSON response. */
707 jsonstr
= config_response(status
, error
);
708 if (jsonstr
== NULL
) {
709 zlog_warn("%s: config_response: failed to get JSON str",
714 /* Allocate data and answer. */
715 jsonstrlen
= strlen(jsonstr
);
716 bcm
= XMALLOC(MTYPE_BFDD_NOTIFICATION
,
717 sizeof(struct bfd_control_msg
) + jsonstrlen
);
719 bcm
->bcm_length
= htonl(jsonstrlen
);
720 bcm
->bcm_ver
= BMV_VERSION_1
;
721 bcm
->bcm_type
= BMT_RESPONSE
;
723 memcpy(bcm
->bcm_data
, jsonstr
, jsonstrlen
);
724 XFREE(MTYPE_BFDD_NOTIFICATION
, jsonstr
);
726 control_queue_enqueue_first(bcs
, bcm
);
729 static void _control_notify(struct bfd_control_socket
*bcs
,
730 struct bfd_session
*bs
)
732 struct bfd_control_msg
*bcm
;
736 /* Generate JSON response. */
737 jsonstr
= config_notify(bs
);
738 if (jsonstr
== NULL
) {
739 zlog_warn("%s: config_notify: failed to get JSON str",
744 /* Allocate data and answer. */
745 jsonstrlen
= strlen(jsonstr
);
746 bcm
= XMALLOC(MTYPE_BFDD_NOTIFICATION
,
747 sizeof(struct bfd_control_msg
) + jsonstrlen
);
749 bcm
->bcm_length
= htonl(jsonstrlen
);
750 bcm
->bcm_ver
= BMV_VERSION_1
;
751 bcm
->bcm_type
= BMT_NOTIFY
;
752 bcm
->bcm_id
= htons(BCM_NOTIFY_ID
);
753 memcpy(bcm
->bcm_data
, jsonstr
, jsonstrlen
);
754 XFREE(MTYPE_BFDD_NOTIFICATION
, jsonstr
);
756 control_queue_enqueue(bcs
, bcm
);
759 int control_notify(struct bfd_session
*bs
, uint8_t notify_state
)
761 struct bfd_control_socket
*bcs
;
762 struct bfd_notify_peer
*bnp
;
764 /* Notify zebra listeners as well. */
765 ptm_bfd_notify(bs
, notify_state
);
768 * PERFORMANCE: reuse the bfd_control_msg allocated data for
769 * all control sockets to avoid wasting memory.
771 TAILQ_FOREACH (bcs
, &bglobal
.bg_bcslist
, bcs_entry
) {
773 * Test for all notifications first, then search for
776 if ((bcs
->bcs_notify
& BCM_NOTIFY_PEER_STATE
) == 0) {
777 bnp
= control_notifypeer_find(bcs
, bs
);
779 * If the notification is not configured here,
786 _control_notify(bcs
, bs
);
792 static void _control_notify_config(struct bfd_control_socket
*bcs
,
793 const char *op
, struct bfd_session
*bs
)
795 struct bfd_control_msg
*bcm
;
799 /* Generate JSON response. */
800 jsonstr
= config_notify_config(op
, bs
);
801 if (jsonstr
== NULL
) {
802 zlog_warn("%s: config_notify_config: failed to get JSON str",
807 /* Allocate data and answer. */
808 jsonstrlen
= strlen(jsonstr
);
809 bcm
= XMALLOC(MTYPE_BFDD_NOTIFICATION
,
810 sizeof(struct bfd_control_msg
) + jsonstrlen
);
812 bcm
->bcm_length
= htonl(jsonstrlen
);
813 bcm
->bcm_ver
= BMV_VERSION_1
;
814 bcm
->bcm_type
= BMT_NOTIFY
;
815 bcm
->bcm_id
= htons(BCM_NOTIFY_ID
);
816 memcpy(bcm
->bcm_data
, jsonstr
, jsonstrlen
);
817 XFREE(MTYPE_BFDD_NOTIFICATION
, jsonstr
);
819 control_queue_enqueue(bcs
, bcm
);
822 int control_notify_config(const char *op
, struct bfd_session
*bs
)
824 struct bfd_control_socket
*bcs
;
825 struct bfd_notify_peer
*bnp
;
827 /* Remove the control sockets notification for this peer. */
828 if (strcmp(op
, BCM_NOTIFY_CONFIG_DELETE
) == 0 && bs
->refcount
> 0) {
829 TAILQ_FOREACH (bcs
, &bglobal
.bg_bcslist
, bcs_entry
) {
830 bnp
= control_notifypeer_find(bcs
, bs
);
832 control_notifypeer_free(bcs
, bnp
);
837 * PERFORMANCE: reuse the bfd_control_msg allocated data for
838 * all control sockets to avoid wasting memory.
840 TAILQ_FOREACH (bcs
, &bglobal
.bg_bcslist
, bcs_entry
) {
842 * Test for all notifications first, then search for
845 if ((bcs
->bcs_notify
& BCM_NOTIFY_CONFIG
) == 0)
848 _control_notify_config(bcs
, op
, bs
);