]>
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 log_warning("%s: fcntl F_GETFL: %s", __func__
, strerror(errno
));
94 if (fcntl(fd
, F_SETFL
, flags
) == -1) {
95 log_warning("%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 log_error("%s: socket: %s", __func__
, strerror(errno
));
124 if (bind(sd
, (struct sockaddr
*)&sun_
, sizeof(sun_
)) == -1) {
125 log_error("%s: bind: %s", __func__
, strerror(errno
));
131 if (listen(sd
, SOMAXCONN
) == -1) {
132 log_error("%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 if (bglobal
.bg_csockev
) {
149 thread_cancel(bglobal
.bg_csockev
);
150 bglobal
.bg_csockev
= NULL
;
153 socket_close(&bglobal
.bg_csock
);
155 while (!TAILQ_EMPTY(&bglobal
.bg_bcslist
)) {
156 bcs
= TAILQ_FIRST(&bglobal
.bg_bcslist
);
161 int control_accept(struct thread
*t
)
163 int csock
, sd
= THREAD_FD(t
);
165 csock
= accept(sd
, NULL
, 0);
167 log_warning("%s: accept: %s", __func__
, strerror(errno
));
171 if (control_new(csock
) == NULL
)
174 bglobal
.bg_csockev
= NULL
;
175 thread_add_read(master
, control_accept
, NULL
, sd
, &bglobal
.bg_csockev
);
184 struct bfd_control_socket
*control_new(int sd
)
186 struct bfd_control_socket
*bcs
;
188 bcs
= XCALLOC(MTYPE_BFDD_CONTROL
, sizeof(*bcs
));
190 /* Disable notifications by default. */
194 thread_add_read(master
, control_read
, bcs
, sd
, &bcs
->bcs_ev
);
196 TAILQ_INIT(&bcs
->bcs_bcqueue
);
197 TAILQ_INIT(&bcs
->bcs_bnplist
);
198 TAILQ_INSERT_TAIL(&bglobal
.bg_bcslist
, bcs
, bcs_entry
);
203 static void control_free(struct bfd_control_socket
*bcs
)
205 struct bfd_control_queue
*bcq
;
206 struct bfd_notify_peer
*bnp
;
209 thread_cancel(bcs
->bcs_ev
);
213 if (bcs
->bcs_outev
) {
214 thread_cancel(bcs
->bcs_outev
);
215 bcs
->bcs_outev
= NULL
;
220 TAILQ_REMOVE(&bglobal
.bg_bcslist
, bcs
, bcs_entry
);
222 /* Empty output queue. */
223 while (!TAILQ_EMPTY(&bcs
->bcs_bcqueue
)) {
224 bcq
= TAILQ_FIRST(&bcs
->bcs_bcqueue
);
225 control_queue_free(bcs
, bcq
);
228 /* Empty notification list. */
229 while (!TAILQ_EMPTY(&bcs
->bcs_bnplist
)) {
230 bnp
= TAILQ_FIRST(&bcs
->bcs_bnplist
);
231 control_notifypeer_free(bcs
, bnp
);
234 control_reset_buf(&bcs
->bcs_bin
);
235 XFREE(MTYPE_BFDD_CONTROL
, bcs
);
238 struct bfd_notify_peer
*control_notifypeer_new(struct bfd_control_socket
*bcs
,
239 struct bfd_session
*bs
)
241 struct bfd_notify_peer
*bnp
;
243 bnp
= control_notifypeer_find(bcs
, bs
);
247 bnp
= XCALLOC(MTYPE_BFDD_CONTROL
, sizeof(*bnp
));
249 TAILQ_INSERT_TAIL(&bcs
->bcs_bnplist
, bnp
, bnp_entry
);
256 static void control_notifypeer_free(struct bfd_control_socket
*bcs
,
257 struct bfd_notify_peer
*bnp
)
259 TAILQ_REMOVE(&bcs
->bcs_bnplist
, bnp
, bnp_entry
);
260 bnp
->bnp_bs
->refcount
--;
261 XFREE(MTYPE_BFDD_CONTROL
, bnp
);
264 struct bfd_notify_peer
*control_notifypeer_find(struct bfd_control_socket
*bcs
,
265 struct bfd_session
*bs
)
267 struct bfd_notify_peer
*bnp
;
269 TAILQ_FOREACH (bnp
, &bcs
->bcs_bnplist
, bnp_entry
) {
270 if (bnp
->bnp_bs
== bs
)
277 struct bfd_control_queue
*control_queue_new(struct bfd_control_socket
*bcs
)
279 struct bfd_control_queue
*bcq
;
281 bcq
= XCALLOC(MTYPE_BFDD_NOTIFICATION
, sizeof(*bcq
));
283 control_reset_buf(&bcq
->bcq_bcb
);
284 TAILQ_INSERT_TAIL(&bcs
->bcs_bcqueue
, bcq
, bcq_entry
);
289 static void control_queue_free(struct bfd_control_socket
*bcs
,
290 struct bfd_control_queue
*bcq
)
292 control_reset_buf(&bcq
->bcq_bcb
);
293 TAILQ_REMOVE(&bcs
->bcs_bcqueue
, bcq
, bcq_entry
);
294 XFREE(MTYPE_BFDD_NOTIFICATION
, bcq
);
297 static int control_queue_dequeue(struct bfd_control_socket
*bcs
)
299 struct bfd_control_queue
*bcq
;
301 /* List is empty, nothing to do. */
302 if (TAILQ_EMPTY(&bcs
->bcs_bcqueue
))
305 bcq
= TAILQ_FIRST(&bcs
->bcs_bcqueue
);
306 control_queue_free(bcs
, bcq
);
308 /* Get the next buffer to send. */
309 if (TAILQ_EMPTY(&bcs
->bcs_bcqueue
))
312 bcq
= TAILQ_FIRST(&bcs
->bcs_bcqueue
);
313 bcs
->bcs_bout
= &bcq
->bcq_bcb
;
315 bcs
->bcs_outev
= NULL
;
316 thread_add_write(master
, control_write
, bcs
, bcs
->bcs_sd
,
322 if (bcs
->bcs_outev
) {
323 thread_cancel(bcs
->bcs_outev
);
324 bcs
->bcs_outev
= NULL
;
326 bcs
->bcs_bout
= NULL
;
330 static int control_queue_enqueue(struct bfd_control_socket
*bcs
,
331 struct bfd_control_msg
*bcm
)
333 struct bfd_control_queue
*bcq
;
334 struct bfd_control_buffer
*bcb
;
336 bcq
= control_queue_new(bcs
);
341 bcb
->bcb_left
= sizeof(struct bfd_control_msg
) + ntohl(bcm
->bcm_length
);
345 /* If this is the first item, then dequeue and start using it. */
346 if (bcs
->bcs_bout
== NULL
) {
349 /* New messages, active write events. */
350 thread_add_write(master
, control_write
, bcs
, bcs
->bcs_sd
,
357 static int control_queue_enqueue_first(struct bfd_control_socket
*bcs
,
358 struct bfd_control_msg
*bcm
)
360 struct bfd_control_queue
*bcq
, *bcqn
;
361 struct bfd_control_buffer
*bcb
;
363 /* Enqueue it somewhere. */
364 if (control_queue_enqueue(bcs
, bcm
) == -1)
368 * The item is either the first or the last. So we must first
369 * check the best case where the item is already the first.
371 bcq
= TAILQ_FIRST(&bcs
->bcs_bcqueue
);
373 if (bcm
== bcb
->bcb_bcm
)
377 * The item was not the first, so it is the last. We'll try to
378 * assign it to the head of the queue, however if there is a
379 * transfer in progress, then we have to make the item as the
382 * Interrupting the transfer of in progress message will cause
383 * the client to lose track of the message position/data.
385 bcqn
= TAILQ_LAST(&bcs
->bcs_bcqueue
, bcqueue
);
386 TAILQ_REMOVE(&bcs
->bcs_bcqueue
, bcqn
, bcq_entry
);
387 if (bcb
->bcb_pos
!= 0) {
389 * First position is already being sent, insert into
392 TAILQ_INSERT_AFTER(&bcs
->bcs_bcqueue
, bcq
, bcqn
, bcq_entry
);
395 * Old message didn't start being sent, we still have
396 * time to put this one in the head of the queue.
398 TAILQ_INSERT_HEAD(&bcs
->bcs_bcqueue
, bcqn
, bcq_entry
);
399 bcb
= &bcqn
->bcq_bcb
;
406 static void control_reset_buf(struct bfd_control_buffer
*bcb
)
408 /* Get ride of old data. */
409 XFREE(MTYPE_BFDD_NOTIFICATION
, bcb
->bcb_buf
);
415 static int control_read(struct thread
*t
)
417 struct bfd_control_socket
*bcs
= THREAD_ARG(t
);
418 struct bfd_control_buffer
*bcb
= &bcs
->bcs_bin
;
419 int sd
= bcs
->bcs_sd
;
420 struct bfd_control_msg bcm
;
425 * Check if we have already downloaded message content, if so then skip
427 * download the rest of it and process.
429 * Otherwise download a new message header and allocate the necessary
432 if (bcb
->bcb_buf
!= NULL
)
435 bread
= read(sd
, &bcm
, sizeof(bcm
));
441 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
|| errno
== EINTR
)
442 goto schedule_next_read
;
444 log_warning("%s: read: %s", __func__
, strerror(errno
));
449 /* Validate header fields. */
450 plen
= ntohl(bcm
.bcm_length
);
452 log_debug("%s: client closed due small message length: %d",
453 __func__
, bcm
.bcm_length
);
458 if (bcm
.bcm_ver
!= BMV_VERSION_1
) {
459 log_debug("%s: client closed due bad version: %d", __func__
,
465 /* Prepare the buffer to load the message. */
466 bcs
->bcs_version
= bcm
.bcm_ver
;
467 bcs
->bcs_type
= bcm
.bcm_type
;
469 bcb
->bcb_pos
= sizeof(bcm
);
470 bcb
->bcb_left
= plen
;
471 bcb
->bcb_buf
= XMALLOC(MTYPE_BFDD_NOTIFICATION
,
472 sizeof(bcm
) + bcb
->bcb_left
+ 1);
473 if (bcb
->bcb_buf
== NULL
) {
474 log_warning("%s: not enough memory for message size: %u",
475 __func__
, bcb
->bcb_left
);
480 memcpy(bcb
->bcb_buf
, &bcm
, sizeof(bcm
));
482 /* Terminate data string with NULL for later processing. */
483 bcb
->bcb_buf
[sizeof(bcm
) + bcb
->bcb_left
] = 0;
486 /* Download the remaining data of the message and process it. */
487 bread
= read(sd
, &bcb
->bcb_buf
[bcb
->bcb_pos
], bcb
->bcb_left
);
493 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
|| errno
== EINTR
)
494 goto schedule_next_read
;
496 log_warning("%s: read: %s", __func__
, strerror(errno
));
501 bcb
->bcb_pos
+= bread
;
502 bcb
->bcb_left
-= bread
;
503 /* We need more data, return to wait more. */
504 if (bcb
->bcb_left
> 0)
505 goto schedule_next_read
;
507 switch (bcb
->bcb_bcm
->bcm_type
) {
508 case BMT_REQUEST_ADD
:
509 control_handle_request_add(bcs
, bcb
->bcb_bcm
);
511 case BMT_REQUEST_DEL
:
512 control_handle_request_del(bcs
, bcb
->bcb_bcm
);
515 control_handle_notify(bcs
, bcb
->bcb_bcm
);
518 control_handle_notify_add(bcs
, bcb
->bcb_bcm
);
521 control_handle_notify_del(bcs
, bcb
->bcb_bcm
);
525 log_debug("%s: unhandled message type: %d", __func__
,
526 bcb
->bcb_bcm
->bcm_type
);
527 control_response(bcs
, bcb
->bcb_bcm
->bcm_id
, BCM_RESPONSE_ERROR
,
528 "invalid message type");
532 bcs
->bcs_version
= 0;
534 control_reset_buf(bcb
);
538 thread_add_read(master
, control_read
, bcs
, sd
, &bcs
->bcs_ev
);
543 static int control_write(struct thread
*t
)
545 struct bfd_control_socket
*bcs
= THREAD_ARG(t
);
546 struct bfd_control_buffer
*bcb
= bcs
->bcs_bout
;
547 int sd
= bcs
->bcs_sd
;
550 bwrite
= write(sd
, &bcb
->bcb_buf
[bcb
->bcb_pos
], bcb
->bcb_left
);
556 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
|| errno
== EINTR
) {
557 bcs
->bcs_outev
= NULL
;
558 thread_add_write(master
, control_write
, bcs
,
559 bcs
->bcs_sd
, &bcs
->bcs_outev
);
563 log_warning("%s: write: %s", __func__
, strerror(errno
));
568 bcb
->bcb_pos
+= bwrite
;
569 bcb
->bcb_left
-= bwrite
;
570 if (bcb
->bcb_left
> 0) {
571 bcs
->bcs_outev
= NULL
;
572 thread_add_write(master
, control_write
, bcs
, bcs
->bcs_sd
,
577 control_queue_dequeue(bcs
);
586 static void control_handle_request_add(struct bfd_control_socket
*bcs
,
587 struct bfd_control_msg
*bcm
)
589 const char *json
= (const char *)bcm
->bcm_data
;
591 if (config_request_add(json
) == 0)
592 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_OK
, NULL
);
594 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_ERROR
,
595 "request add failed");
598 static void control_handle_request_del(struct bfd_control_socket
*bcs
,
599 struct bfd_control_msg
*bcm
)
601 const char *json
= (const char *)bcm
->bcm_data
;
603 if (config_request_del(json
) == 0)
604 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_OK
, NULL
);
606 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_ERROR
,
607 "request del failed");
610 static struct bfd_session
*_notify_find_peer(struct bfd_peer_cfg
*bpc
)
612 struct peer_label
*pl
;
614 if (bpc
->bpc_has_label
) {
615 pl
= pl_find(bpc
->bpc_label
);
620 return bs_peer_find(bpc
);
623 static void _control_handle_notify(struct hash_bucket
*hb
, void *arg
)
625 struct bfd_control_socket
*bcs
= arg
;
626 struct bfd_session
*bs
= hb
->data
;
628 /* Notify peer configuration. */
629 if (bcs
->bcs_notify
& BCM_NOTIFY_CONFIG
)
630 _control_notify_config(bcs
, BCM_NOTIFY_CONFIG_ADD
, bs
);
632 /* Notify peer status. */
633 if (bcs
->bcs_notify
& BCM_NOTIFY_PEER_STATE
)
634 _control_notify(bcs
, bs
);
637 static void control_handle_notify(struct bfd_control_socket
*bcs
,
638 struct bfd_control_msg
*bcm
)
640 memcpy(&bcs
->bcs_notify
, bcm
->bcm_data
, sizeof(bcs
->bcs_notify
));
642 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_OK
, NULL
);
645 * If peer asked for notification configuration, send everything that
646 * was configured until the moment to sync up.
648 if (bcs
->bcs_notify
& (BCM_NOTIFY_CONFIG
| BCM_NOTIFY_PEER_STATE
))
649 bfd_id_iterate(_control_handle_notify
, bcs
);
652 static int notify_add_cb(struct bfd_peer_cfg
*bpc
, void *arg
)
654 struct bfd_control_socket
*bcs
= arg
;
655 struct bfd_session
*bs
= _notify_find_peer(bpc
);
660 if (control_notifypeer_new(bcs
, bs
) == NULL
)
663 /* Notify peer status. */
664 _control_notify(bcs
, bs
);
669 static int notify_del_cb(struct bfd_peer_cfg
*bpc
, void *arg
)
671 struct bfd_control_socket
*bcs
= arg
;
672 struct bfd_session
*bs
= _notify_find_peer(bpc
);
673 struct bfd_notify_peer
*bnp
;
678 bnp
= control_notifypeer_find(bcs
, bs
);
680 control_notifypeer_free(bcs
, bnp
);
685 static void control_handle_notify_add(struct bfd_control_socket
*bcs
,
686 struct bfd_control_msg
*bcm
)
688 const char *json
= (const char *)bcm
->bcm_data
;
690 if (config_notify_request(bcs
, json
, notify_add_cb
) == 0) {
691 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_OK
, NULL
);
695 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_ERROR
,
696 "failed to parse notify data");
699 static void control_handle_notify_del(struct bfd_control_socket
*bcs
,
700 struct bfd_control_msg
*bcm
)
702 const char *json
= (const char *)bcm
->bcm_data
;
704 if (config_notify_request(bcs
, json
, notify_del_cb
) == 0) {
705 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_OK
, NULL
);
709 control_response(bcs
, bcm
->bcm_id
, BCM_RESPONSE_ERROR
,
710 "failed to parse notify data");
715 * Internal functions used by the BFD daemon.
717 static void control_response(struct bfd_control_socket
*bcs
, uint16_t id
,
718 const char *status
, const char *error
)
720 struct bfd_control_msg
*bcm
;
724 /* Generate JSON response. */
725 jsonstr
= config_response(status
, error
);
726 if (jsonstr
== NULL
) {
727 log_warning("%s: config_response: failed to get JSON str",
732 /* Allocate data and answer. */
733 jsonstrlen
= strlen(jsonstr
);
734 bcm
= XMALLOC(MTYPE_BFDD_NOTIFICATION
,
735 sizeof(struct bfd_control_msg
) + jsonstrlen
);
737 bcm
->bcm_length
= htonl(jsonstrlen
);
738 bcm
->bcm_ver
= BMV_VERSION_1
;
739 bcm
->bcm_type
= BMT_RESPONSE
;
741 memcpy(bcm
->bcm_data
, jsonstr
, jsonstrlen
);
742 XFREE(MTYPE_BFDD_NOTIFICATION
, jsonstr
);
744 control_queue_enqueue_first(bcs
, bcm
);
747 static void _control_notify(struct bfd_control_socket
*bcs
,
748 struct bfd_session
*bs
)
750 struct bfd_control_msg
*bcm
;
754 /* Generate JSON response. */
755 jsonstr
= config_notify(bs
);
756 if (jsonstr
== NULL
) {
757 log_warning("%s: config_notify: failed to get JSON str",
762 /* Allocate data and answer. */
763 jsonstrlen
= strlen(jsonstr
);
764 bcm
= XMALLOC(MTYPE_BFDD_NOTIFICATION
,
765 sizeof(struct bfd_control_msg
) + jsonstrlen
);
767 bcm
->bcm_length
= htonl(jsonstrlen
);
768 bcm
->bcm_ver
= BMV_VERSION_1
;
769 bcm
->bcm_type
= BMT_NOTIFY
;
770 bcm
->bcm_id
= htons(BCM_NOTIFY_ID
);
771 memcpy(bcm
->bcm_data
, jsonstr
, jsonstrlen
);
772 XFREE(MTYPE_BFDD_NOTIFICATION
, jsonstr
);
774 control_queue_enqueue(bcs
, bcm
);
777 int control_notify(struct bfd_session
*bs
)
779 struct bfd_control_socket
*bcs
;
780 struct bfd_notify_peer
*bnp
;
782 /* Notify zebra listeners as well. */
786 * PERFORMANCE: reuse the bfd_control_msg allocated data for
787 * all control sockets to avoid wasting memory.
789 TAILQ_FOREACH (bcs
, &bglobal
.bg_bcslist
, bcs_entry
) {
791 * Test for all notifications first, then search for
794 if ((bcs
->bcs_notify
& BCM_NOTIFY_PEER_STATE
) == 0) {
795 bnp
= control_notifypeer_find(bcs
, bs
);
797 * If the notification is not configured here,
804 _control_notify(bcs
, bs
);
810 static void _control_notify_config(struct bfd_control_socket
*bcs
,
811 const char *op
, struct bfd_session
*bs
)
813 struct bfd_control_msg
*bcm
;
817 /* Generate JSON response. */
818 jsonstr
= config_notify_config(op
, bs
);
819 if (jsonstr
== NULL
) {
820 log_warning("%s: config_notify_config: failed to get JSON str",
825 /* Allocate data and answer. */
826 jsonstrlen
= strlen(jsonstr
);
827 bcm
= XMALLOC(MTYPE_BFDD_NOTIFICATION
,
828 sizeof(struct bfd_control_msg
) + jsonstrlen
);
830 bcm
->bcm_length
= htonl(jsonstrlen
);
831 bcm
->bcm_ver
= BMV_VERSION_1
;
832 bcm
->bcm_type
= BMT_NOTIFY
;
833 bcm
->bcm_id
= htons(BCM_NOTIFY_ID
);
834 memcpy(bcm
->bcm_data
, jsonstr
, jsonstrlen
);
835 XFREE(MTYPE_BFDD_NOTIFICATION
, jsonstr
);
837 control_queue_enqueue(bcs
, bcm
);
840 int control_notify_config(const char *op
, struct bfd_session
*bs
)
842 struct bfd_control_socket
*bcs
;
843 struct bfd_notify_peer
*bnp
;
845 /* Remove the control sockets notification for this peer. */
846 if (strcmp(op
, BCM_NOTIFY_CONFIG_DELETE
) == 0 && bs
->refcount
> 0) {
847 TAILQ_FOREACH (bcs
, &bglobal
.bg_bcslist
, bcs_entry
) {
848 bnp
= control_notifypeer_find(bcs
, bs
);
850 control_notifypeer_free(bcs
, bnp
);
855 * PERFORMANCE: reuse the bfd_control_msg allocated data for
856 * all control sockets to avoid wasting memory.
858 TAILQ_FOREACH (bcs
, &bglobal
.bg_bcslist
, bcs_entry
) {
860 * Test for all notifications first, then search for
863 if ((bcs
->bcs_notify
& BCM_NOTIFY_CONFIG
) == 0)
866 _control_notify_config(bcs
, op
, bs
);