]>
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 thread_add_read(master
, control_accept
, NULL
, sd
, &bglobal
.bg_csockev
);
179 struct bfd_control_socket
*control_new(int sd
)
181 struct bfd_control_socket
*bcs
;
183 bcs
= XCALLOC(MTYPE_BFDD_CONTROL
, sizeof(*bcs
));
185 /* Disable notifications by default. */
189 thread_add_read(master
, control_read
, bcs
, sd
, &bcs
->bcs_ev
);
191 TAILQ_INIT(&bcs
->bcs_bcqueue
);
192 TAILQ_INIT(&bcs
->bcs_bnplist
);
193 TAILQ_INSERT_TAIL(&bglobal
.bg_bcslist
, bcs
, bcs_entry
);
198 static void control_free(struct bfd_control_socket
*bcs
)
200 struct bfd_control_queue
*bcq
;
201 struct bfd_notify_peer
*bnp
;
203 thread_cancel(&(bcs
->bcs_ev
));
204 thread_cancel(&(bcs
->bcs_outev
));
208 TAILQ_REMOVE(&bglobal
.bg_bcslist
, bcs
, bcs_entry
);
210 /* Empty output queue. */
211 while (!TAILQ_EMPTY(&bcs
->bcs_bcqueue
)) {
212 bcq
= TAILQ_FIRST(&bcs
->bcs_bcqueue
);
213 control_queue_free(bcs
, bcq
);
216 /* Empty notification list. */
217 while (!TAILQ_EMPTY(&bcs
->bcs_bnplist
)) {
218 bnp
= TAILQ_FIRST(&bcs
->bcs_bnplist
);
219 control_notifypeer_free(bcs
, bnp
);
222 control_reset_buf(&bcs
->bcs_bin
);
223 XFREE(MTYPE_BFDD_CONTROL
, bcs
);
226 struct bfd_notify_peer
*control_notifypeer_new(struct bfd_control_socket
*bcs
,
227 struct bfd_session
*bs
)
229 struct bfd_notify_peer
*bnp
;
231 bnp
= control_notifypeer_find(bcs
, bs
);
235 bnp
= XCALLOC(MTYPE_BFDD_CONTROL
, sizeof(*bnp
));
237 TAILQ_INSERT_TAIL(&bcs
->bcs_bnplist
, bnp
, bnp_entry
);
244 static void control_notifypeer_free(struct bfd_control_socket
*bcs
,
245 struct bfd_notify_peer
*bnp
)
247 TAILQ_REMOVE(&bcs
->bcs_bnplist
, bnp
, bnp_entry
);
248 bnp
->bnp_bs
->refcount
--;
249 XFREE(MTYPE_BFDD_CONTROL
, bnp
);
252 struct bfd_notify_peer
*control_notifypeer_find(struct bfd_control_socket
*bcs
,
253 struct bfd_session
*bs
)
255 struct bfd_notify_peer
*bnp
;
257 TAILQ_FOREACH (bnp
, &bcs
->bcs_bnplist
, bnp_entry
) {
258 if (bnp
->bnp_bs
== bs
)
265 struct bfd_control_queue
*control_queue_new(struct bfd_control_socket
*bcs
)
267 struct bfd_control_queue
*bcq
;
269 bcq
= XCALLOC(MTYPE_BFDD_NOTIFICATION
, sizeof(*bcq
));
271 control_reset_buf(&bcq
->bcq_bcb
);
272 TAILQ_INSERT_TAIL(&bcs
->bcs_bcqueue
, bcq
, bcq_entry
);
277 static void control_queue_free(struct bfd_control_socket
*bcs
,
278 struct bfd_control_queue
*bcq
)
280 control_reset_buf(&bcq
->bcq_bcb
);
281 TAILQ_REMOVE(&bcs
->bcs_bcqueue
, bcq
, bcq_entry
);
282 XFREE(MTYPE_BFDD_NOTIFICATION
, bcq
);
285 static int control_queue_dequeue(struct bfd_control_socket
*bcs
)
287 struct bfd_control_queue
*bcq
;
289 /* List is empty, nothing to do. */
290 if (TAILQ_EMPTY(&bcs
->bcs_bcqueue
))
293 bcq
= TAILQ_FIRST(&bcs
->bcs_bcqueue
);
294 control_queue_free(bcs
, bcq
);
296 /* Get the next buffer to send. */
297 if (TAILQ_EMPTY(&bcs
->bcs_bcqueue
))
300 bcq
= TAILQ_FIRST(&bcs
->bcs_bcqueue
);
301 bcs
->bcs_bout
= &bcq
->bcq_bcb
;
303 bcs
->bcs_outev
= NULL
;
304 thread_add_write(master
, control_write
, bcs
, bcs
->bcs_sd
,
310 thread_cancel(&(bcs
->bcs_outev
));
311 bcs
->bcs_bout
= NULL
;
315 static int control_queue_enqueue(struct bfd_control_socket
*bcs
,
316 struct bfd_control_msg
*bcm
)
318 struct bfd_control_queue
*bcq
;
319 struct bfd_control_buffer
*bcb
;
321 bcq
= control_queue_new(bcs
);
324 bcb
->bcb_left
= sizeof(struct bfd_control_msg
) + ntohl(bcm
->bcm_length
);
328 /* If this is the first item, then dequeue and start using it. */
329 if (bcs
->bcs_bout
== NULL
) {
332 /* New messages, active write events. */
333 thread_add_write(master
, control_write
, bcs
, bcs
->bcs_sd
,
340 static int control_queue_enqueue_first(struct bfd_control_socket
*bcs
,
341 struct bfd_control_msg
*bcm
)
343 struct bfd_control_queue
*bcq
, *bcqn
;
344 struct bfd_control_buffer
*bcb
;
346 /* Enqueue it somewhere. */
347 if (control_queue_enqueue(bcs
, bcm
) == -1)
351 * The item is either the first or the last. So we must first
352 * check the best case where the item is already the first.
354 bcq
= TAILQ_FIRST(&bcs
->bcs_bcqueue
);
356 if (bcm
== bcb
->bcb_bcm
)
360 * The item was not the first, so it is the last. We'll try to
361 * assign it to the head of the queue, however if there is a
362 * transfer in progress, then we have to make the item as the
365 * Interrupting the transfer of in progress message will cause
366 * the client to lose track of the message position/data.
368 bcqn
= TAILQ_LAST(&bcs
->bcs_bcqueue
, bcqueue
);
369 TAILQ_REMOVE(&bcs
->bcs_bcqueue
, bcqn
, bcq_entry
);
370 if (bcb
->bcb_pos
!= 0) {
372 * First position is already being sent, insert into
375 TAILQ_INSERT_AFTER(&bcs
->bcs_bcqueue
, bcq
, bcqn
, bcq_entry
);
378 * Old message didn't start being sent, we still have
379 * time to put this one in the head of the queue.
381 TAILQ_INSERT_HEAD(&bcs
->bcs_bcqueue
, bcqn
, bcq_entry
);
382 bcb
= &bcqn
->bcq_bcb
;
389 static void control_reset_buf(struct bfd_control_buffer
*bcb
)
391 /* Get ride of old data. */
392 XFREE(MTYPE_BFDD_NOTIFICATION
, bcb
->bcb_buf
);
397 static int control_read(struct thread
*t
)
399 struct bfd_control_socket
*bcs
= THREAD_ARG(t
);
400 struct bfd_control_buffer
*bcb
= &bcs
->bcs_bin
;
401 int sd
= bcs
->bcs_sd
;
402 struct bfd_control_msg bcm
;
407 * Check if we have already downloaded message content, if so then skip
409 * download the rest of it and process.
411 * Otherwise download a new message header and allocate the necessary
414 if (bcb
->bcb_buf
!= NULL
)
417 bread
= read(sd
, &bcm
, sizeof(bcm
));
423 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
|| errno
== EINTR
)
424 goto schedule_next_read
;
426 zlog_warn("%s: read: %s", __func__
, strerror(errno
));
431 /* Validate header fields. */
432 plen
= ntohl(bcm
.bcm_length
);
434 zlog_debug("%s: client closed due small message length: %d",
435 __func__
, bcm
.bcm_length
);
440 if (bcm
.bcm_ver
!= BMV_VERSION_1
) {
441 zlog_debug("%s: client closed due bad version: %d", __func__
,
447 /* Prepare the buffer to load the message. */
448 bcs
->bcs_version
= bcm
.bcm_ver
;
449 bcs
->bcs_type
= bcm
.bcm_type
;
451 bcb
->bcb_pos
= sizeof(bcm
);
452 bcb
->bcb_left
= plen
;
453 bcb
->bcb_buf
= XMALLOC(MTYPE_BFDD_NOTIFICATION
,
454 sizeof(bcm
) + bcb
->bcb_left
+ 1);
455 if (bcb
->bcb_buf
== NULL
) {
456 zlog_warn("%s: not enough memory for message size: %zu",
457 __func__
, bcb
->bcb_left
);
462 memcpy(bcb
->bcb_buf
, &bcm
, sizeof(bcm
));
464 /* Terminate data string with NULL for later processing. */
465 bcb
->bcb_buf
[sizeof(bcm
) + bcb
->bcb_left
] = 0;
468 /* Download the remaining data of the message and process it. */
469 bread
= read(sd
, &bcb
->bcb_buf
[bcb
->bcb_pos
], bcb
->bcb_left
);
475 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
|| errno
== EINTR
)
476 goto schedule_next_read
;
478 zlog_warn("%s: read: %s", __func__
, strerror(errno
));
483 bcb
->bcb_pos
+= bread
;
484 bcb
->bcb_left
-= bread
;
485 /* We need more data, return to wait more. */
486 if (bcb
->bcb_left
> 0)
487 goto schedule_next_read
;
489 switch (bcb
->bcb_bcm
->bcm_type
) {
490 case BMT_REQUEST_ADD
:
491 control_handle_request_add(bcs
, bcb
->bcb_bcm
);
493 case BMT_REQUEST_DEL
:
494 control_handle_request_del(bcs
, bcb
->bcb_bcm
);
497 control_handle_notify(bcs
, bcb
->bcb_bcm
);
500 control_handle_notify_add(bcs
, bcb
->bcb_bcm
);
503 control_handle_notify_del(bcs
, bcb
->bcb_bcm
);
507 zlog_debug("%s: unhandled message type: %d", __func__
,
508 bcb
->bcb_bcm
->bcm_type
);
509 control_response(bcs
, bcb
->bcb_bcm
->bcm_id
, BCM_RESPONSE_ERROR
,
510 "invalid message type");
514 bcs
->bcs_version
= 0;
516 control_reset_buf(bcb
);
520 thread_add_read(master
, control_read
, bcs
, sd
, &bcs
->bcs_ev
);
525 static int control_write(struct thread
*t
)
527 struct bfd_control_socket
*bcs
= THREAD_ARG(t
);
528 struct bfd_control_buffer
*bcb
= bcs
->bcs_bout
;
529 int sd
= bcs
->bcs_sd
;
532 bwrite
= write(sd
, &bcb
->bcb_buf
[bcb
->bcb_pos
], bcb
->bcb_left
);
538 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
|| errno
== EINTR
) {
539 bcs
->bcs_outev
= NULL
;
540 thread_add_write(master
, control_write
, bcs
,
541 bcs
->bcs_sd
, &bcs
->bcs_outev
);
545 zlog_warn("%s: write: %s", __func__
, strerror(errno
));
550 bcb
->bcb_pos
+= bwrite
;
551 bcb
->bcb_left
-= bwrite
;
552 if (bcb
->bcb_left
> 0) {
553 bcs
->bcs_outev
= NULL
;
554 thread_add_write(master
, control_write
, bcs
, bcs
->bcs_sd
,
559 control_queue_dequeue(bcs
);
568 static void control_handle_request_add(struct bfd_control_socket
*bcs
,
569 struct bfd_control_msg
*bcm
)
571 const char *json
= (const char *)bcm
->bcm_data
;
573 if (config_request_add(json
) == 0)
574 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_OK
, NULL
);
576 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_ERROR
,
577 "request add failed");
580 static void control_handle_request_del(struct bfd_control_socket
*bcs
,
581 struct bfd_control_msg
*bcm
)
583 const char *json
= (const char *)bcm
->bcm_data
;
585 if (config_request_del(json
) == 0)
586 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_OK
, NULL
);
588 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_ERROR
,
589 "request del failed");
592 static struct bfd_session
*_notify_find_peer(struct bfd_peer_cfg
*bpc
)
594 struct peer_label
*pl
;
596 if (bpc
->bpc_has_label
) {
597 pl
= pl_find(bpc
->bpc_label
);
602 return bs_peer_find(bpc
);
605 static void _control_handle_notify(struct hash_bucket
*hb
, void *arg
)
607 struct bfd_control_socket
*bcs
= arg
;
608 struct bfd_session
*bs
= hb
->data
;
610 /* Notify peer configuration. */
611 if (bcs
->bcs_notify
& BCM_NOTIFY_CONFIG
)
612 _control_notify_config(bcs
, BCM_NOTIFY_CONFIG_ADD
, bs
);
614 /* Notify peer status. */
615 if (bcs
->bcs_notify
& BCM_NOTIFY_PEER_STATE
)
616 _control_notify(bcs
, bs
);
619 static void control_handle_notify(struct bfd_control_socket
*bcs
,
620 struct bfd_control_msg
*bcm
)
622 memcpy(&bcs
->bcs_notify
, bcm
->bcm_data
, sizeof(bcs
->bcs_notify
));
624 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_OK
, NULL
);
627 * If peer asked for notification configuration, send everything that
628 * was configured until the moment to sync up.
630 if (bcs
->bcs_notify
& (BCM_NOTIFY_CONFIG
| BCM_NOTIFY_PEER_STATE
))
631 bfd_id_iterate(_control_handle_notify
, bcs
);
634 static int notify_add_cb(struct bfd_peer_cfg
*bpc
, void *arg
)
636 struct bfd_control_socket
*bcs
= arg
;
637 struct bfd_session
*bs
= _notify_find_peer(bpc
);
642 control_notifypeer_new(bcs
, bs
);
644 /* Notify peer status. */
645 _control_notify(bcs
, bs
);
650 static int notify_del_cb(struct bfd_peer_cfg
*bpc
, void *arg
)
652 struct bfd_control_socket
*bcs
= arg
;
653 struct bfd_session
*bs
= _notify_find_peer(bpc
);
654 struct bfd_notify_peer
*bnp
;
659 bnp
= control_notifypeer_find(bcs
, bs
);
661 control_notifypeer_free(bcs
, bnp
);
666 static void control_handle_notify_add(struct bfd_control_socket
*bcs
,
667 struct bfd_control_msg
*bcm
)
669 const char *json
= (const char *)bcm
->bcm_data
;
671 if (config_notify_request(bcs
, json
, notify_add_cb
) == 0) {
672 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_OK
, NULL
);
676 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_ERROR
,
677 "failed to parse notify data");
680 static void control_handle_notify_del(struct bfd_control_socket
*bcs
,
681 struct bfd_control_msg
*bcm
)
683 const char *json
= (const char *)bcm
->bcm_data
;
685 if (config_notify_request(bcs
, json
, notify_del_cb
) == 0) {
686 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_OK
, NULL
);
690 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_ERROR
,
691 "failed to parse notify data");
696 * Internal functions used by the BFD daemon.
698 static void control_response(struct bfd_control_socket
*bcs
, uint16_t id
,
699 const char *status
, const char *error
)
701 struct bfd_control_msg
*bcm
;
705 /* Generate JSON response. */
706 jsonstr
= config_response(status
, error
);
707 if (jsonstr
== NULL
) {
708 zlog_warn("%s: config_response: failed to get JSON str",
713 /* Allocate data and answer. */
714 jsonstrlen
= strlen(jsonstr
);
715 bcm
= XMALLOC(MTYPE_BFDD_NOTIFICATION
,
716 sizeof(struct bfd_control_msg
) + jsonstrlen
);
718 bcm
->bcm_length
= htonl(jsonstrlen
);
719 bcm
->bcm_ver
= BMV_VERSION_1
;
720 bcm
->bcm_type
= BMT_RESPONSE
;
722 memcpy(bcm
->bcm_data
, jsonstr
, jsonstrlen
);
723 XFREE(MTYPE_BFDD_NOTIFICATION
, jsonstr
);
725 control_queue_enqueue_first(bcs
, bcm
);
728 static void _control_notify(struct bfd_control_socket
*bcs
,
729 struct bfd_session
*bs
)
731 struct bfd_control_msg
*bcm
;
735 /* Generate JSON response. */
736 jsonstr
= config_notify(bs
);
737 if (jsonstr
== NULL
) {
738 zlog_warn("%s: config_notify: failed to get JSON str",
743 /* Allocate data and answer. */
744 jsonstrlen
= strlen(jsonstr
);
745 bcm
= XMALLOC(MTYPE_BFDD_NOTIFICATION
,
746 sizeof(struct bfd_control_msg
) + jsonstrlen
);
748 bcm
->bcm_length
= htonl(jsonstrlen
);
749 bcm
->bcm_ver
= BMV_VERSION_1
;
750 bcm
->bcm_type
= BMT_NOTIFY
;
751 bcm
->bcm_id
= htons(BCM_NOTIFY_ID
);
752 memcpy(bcm
->bcm_data
, jsonstr
, jsonstrlen
);
753 XFREE(MTYPE_BFDD_NOTIFICATION
, jsonstr
);
755 control_queue_enqueue(bcs
, bcm
);
758 int control_notify(struct bfd_session
*bs
, uint8_t notify_state
)
760 struct bfd_control_socket
*bcs
;
761 struct bfd_notify_peer
*bnp
;
763 /* Notify zebra listeners as well. */
764 ptm_bfd_notify(bs
, notify_state
);
767 * PERFORMANCE: reuse the bfd_control_msg allocated data for
768 * all control sockets to avoid wasting memory.
770 TAILQ_FOREACH (bcs
, &bglobal
.bg_bcslist
, bcs_entry
) {
772 * Test for all notifications first, then search for
775 if ((bcs
->bcs_notify
& BCM_NOTIFY_PEER_STATE
) == 0) {
776 bnp
= control_notifypeer_find(bcs
, bs
);
778 * If the notification is not configured here,
785 _control_notify(bcs
, bs
);
791 static void _control_notify_config(struct bfd_control_socket
*bcs
,
792 const char *op
, struct bfd_session
*bs
)
794 struct bfd_control_msg
*bcm
;
798 /* Generate JSON response. */
799 jsonstr
= config_notify_config(op
, bs
);
800 if (jsonstr
== NULL
) {
801 zlog_warn("%s: config_notify_config: failed to get JSON str",
806 /* Allocate data and answer. */
807 jsonstrlen
= strlen(jsonstr
);
808 bcm
= XMALLOC(MTYPE_BFDD_NOTIFICATION
,
809 sizeof(struct bfd_control_msg
) + jsonstrlen
);
811 bcm
->bcm_length
= htonl(jsonstrlen
);
812 bcm
->bcm_ver
= BMV_VERSION_1
;
813 bcm
->bcm_type
= BMT_NOTIFY
;
814 bcm
->bcm_id
= htons(BCM_NOTIFY_ID
);
815 memcpy(bcm
->bcm_data
, jsonstr
, jsonstrlen
);
816 XFREE(MTYPE_BFDD_NOTIFICATION
, jsonstr
);
818 control_queue_enqueue(bcs
, bcm
);
821 int control_notify_config(const char *op
, struct bfd_session
*bs
)
823 struct bfd_control_socket
*bcs
;
824 struct bfd_notify_peer
*bnp
;
826 /* Remove the control sockets notification for this peer. */
827 if (strcmp(op
, BCM_NOTIFY_CONFIG_DELETE
) == 0 && bs
->refcount
> 0) {
828 TAILQ_FOREACH (bcs
, &bglobal
.bg_bcslist
, bcs_entry
) {
829 bnp
= control_notifypeer_find(bcs
, bs
);
831 control_notifypeer_free(bcs
, bnp
);
836 * PERFORMANCE: reuse the bfd_control_msg allocated data for
837 * all control sockets to avoid wasting memory.
839 TAILQ_FOREACH (bcs
, &bglobal
.bg_bcslist
, bcs_entry
) {
841 * Test for all notifications first, then search for
844 if ((bcs
->bcs_notify
& BCM_NOTIFY_CONFIG
) == 0)
847 _control_notify_config(bcs
, op
, bs
);