1 /* Kernel routing table updates using netlink over GNU/Linux system.
2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <sys/un.h> /* for sockaddr_un */
34 #include "lib/version.h"
37 #include "lib_errors.h"
39 #include "zebra/debug.h"
40 #include "zebra/interface.h"
41 #include "zebra/zebra_errors.h"
42 #include "zebra/zebra_ptm.h"
43 #include "zebra/zebra_ptm_redistribute.h"
44 #include "zebra/zebra_router.h"
45 #include "zebra_vrf.h"
48 * Choose the BFD implementation that we'll use.
50 * There are two implementations:
51 * - PTM BFD: which uses an external daemon;
52 * - bfdd: FRR's own BFD daemon;
56 #define ZEBRA_PTM_RECONNECT_TIME_INITIAL 1 /* initial reconnect is 1s */
57 #define ZEBRA_PTM_RECONNECT_TIME_MAX 300
60 #define PTM_HEADER_LEN 37
62 const char ZEBRA_PTM_GET_STATUS_CMD
[] = "get-status";
63 const char ZEBRA_PTM_BFD_START_CMD
[] = "start-bfd-sess";
64 const char ZEBRA_PTM_BFD_STOP_CMD
[] = "stop-bfd-sess";
65 const char ZEBRA_PTM_BFD_CLIENT_REG_CMD
[] = "reg-bfd-client";
66 const char ZEBRA_PTM_BFD_CLIENT_DEREG_CMD
[] = "dereg-bfd-client";
68 const char ZEBRA_PTM_CMD_STR
[] = "cmd";
69 const char ZEBRA_PTM_CMD_STATUS_STR
[] = "cmd_status";
70 const char ZEBRA_PTM_PORT_STR
[] = "port";
71 const char ZEBRA_PTM_CBL_STR
[] = "cbl status";
72 const char ZEBRA_PTM_PASS_STR
[] = "pass";
73 const char ZEBRA_PTM_FAIL_STR
[] = "fail";
74 const char ZEBRA_PTM_BFDSTATUS_STR
[] = "state";
75 const char ZEBRA_PTM_BFDSTATUS_UP_STR
[] = "Up";
76 const char ZEBRA_PTM_BFDSTATUS_DOWN_STR
[] = "Down";
77 const char ZEBRA_PTM_BFDDEST_STR
[] = "peer";
78 const char ZEBRA_PTM_BFDSRC_STR
[] = "local";
79 const char ZEBRA_PTM_BFDVRF_STR
[] = "vrf";
80 const char ZEBRA_PTM_INVALID_PORT_NAME
[] = "N/A";
81 const char ZEBRA_PTM_INVALID_SRC_IP
[] = "N/A";
82 const char ZEBRA_PTM_INVALID_VRF
[] = "N/A";
84 const char ZEBRA_PTM_BFD_DST_IP_FIELD
[] = "dstIPaddr";
85 const char ZEBRA_PTM_BFD_SRC_IP_FIELD
[] = "srcIPaddr";
86 const char ZEBRA_PTM_BFD_MIN_RX_FIELD
[] = "requiredMinRx";
87 const char ZEBRA_PTM_BFD_MIN_TX_FIELD
[] = "upMinTx";
88 const char ZEBRA_PTM_BFD_DETECT_MULT_FIELD
[] = "detectMult";
89 const char ZEBRA_PTM_BFD_MULTI_HOP_FIELD
[] = "multiHop";
90 const char ZEBRA_PTM_BFD_CLIENT_FIELD
[] = "client";
91 const char ZEBRA_PTM_BFD_SEQID_FIELD
[] = "seqid";
92 const char ZEBRA_PTM_BFD_IFNAME_FIELD
[] = "ifName";
93 const char ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD
[] = "maxHopCnt";
94 const char ZEBRA_PTM_BFD_SEND_EVENT
[] = "sendEvent";
95 const char ZEBRA_PTM_BFD_VRF_NAME_FIELD
[] = "vrfName";
96 const char ZEBRA_PTM_BFD_CBIT_FIELD
[] = "bfdcbit";
98 static ptm_lib_handle_t
*ptm_hdl
;
100 struct zebra_ptm_cb ptm_cb
;
102 static int zebra_ptm_socket_init(void);
103 void zebra_ptm_sock_read(struct thread
*thread
);
104 static void zebra_ptm_install_commands(void);
105 static int zebra_ptm_handle_msg_cb(void *arg
, void *in_ctxt
);
106 void zebra_bfd_peer_replay_req(void);
107 void zebra_ptm_send_status_req(void);
108 void zebra_ptm_reset_status(int ptm_disable
);
109 static int zebra_ptm_bfd_client_deregister(struct zserv
*client
);
111 const char ZEBRA_PTM_SOCK_NAME
[] = "\0/var/run/ptmd.socket";
113 void zebra_ptm_init(void)
117 memset(&ptm_cb
, 0, sizeof(ptm_cb
));
119 ptm_cb
.out_data
= calloc(1, ZEBRA_PTM_SEND_MAX_SOCKBUF
);
120 if (!ptm_cb
.out_data
) {
121 zlog_debug("%s: Allocation of send data failed", __func__
);
125 ptm_cb
.in_data
= calloc(1, ZEBRA_PTM_MAX_SOCKBUF
);
126 if (!ptm_cb
.in_data
) {
127 zlog_debug("%s: Allocation of recv data failed", __func__
);
128 free(ptm_cb
.out_data
);
132 ptm_cb
.pid
= getpid();
133 zebra_ptm_install_commands();
135 snprintf(buf
, sizeof(buf
), "%s", FRR_PTM_NAME
);
136 ptm_hdl
= ptm_lib_register(buf
, NULL
, zebra_ptm_handle_msg_cb
,
137 zebra_ptm_handle_msg_cb
);
138 ptm_cb
.wb
= buffer_new(0);
140 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
142 ptm_cb
.ptm_sock
= -1;
144 hook_register(zserv_client_close
, zebra_ptm_bfd_client_deregister
);
147 void zebra_ptm_finish(void)
149 buffer_flush_all(ptm_cb
.wb
, ptm_cb
.ptm_sock
);
154 free(ptm_cb
.out_data
);
157 free(ptm_cb
.in_data
);
160 thread_cancel(&ptm_cb
.t_read
);
161 thread_cancel(&ptm_cb
.t_write
);
162 thread_cancel(&ptm_cb
.t_timer
);
165 buffer_free(ptm_cb
.wb
);
167 if (ptm_cb
.ptm_sock
>= 0)
168 close(ptm_cb
.ptm_sock
);
171 static void zebra_ptm_flush_messages(struct thread
*thread
)
173 ptm_cb
.t_write
= NULL
;
175 if (ptm_cb
.ptm_sock
== -1)
180 switch (buffer_flush_available(ptm_cb
.wb
, ptm_cb
.ptm_sock
)) {
182 flog_err_sys(EC_LIB_SOCKET
, "%s ptm socket error: %s", __func__
,
183 safe_strerror(errno
));
184 close(ptm_cb
.ptm_sock
);
185 ptm_cb
.ptm_sock
= -1;
186 zebra_ptm_reset_status(0);
187 ptm_cb
.t_timer
= NULL
;
188 thread_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
189 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
192 ptm_cb
.t_write
= NULL
;
193 thread_add_write(zrouter
.master
, zebra_ptm_flush_messages
, NULL
,
194 ptm_cb
.ptm_sock
, &ptm_cb
.t_write
);
201 static int zebra_ptm_send_message(char *data
, int size
)
204 switch (buffer_write(ptm_cb
.wb
, ptm_cb
.ptm_sock
, data
, size
)) {
206 flog_err_sys(EC_LIB_SOCKET
, "%s ptm socket error: %s", __func__
,
207 safe_strerror(errno
));
208 close(ptm_cb
.ptm_sock
);
209 ptm_cb
.ptm_sock
= -1;
210 zebra_ptm_reset_status(0);
211 ptm_cb
.t_timer
= NULL
;
212 thread_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
213 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
216 thread_cancel(&ptm_cb
.t_write
);
219 thread_add_write(zrouter
.master
, zebra_ptm_flush_messages
, NULL
,
220 ptm_cb
.ptm_sock
, &ptm_cb
.t_write
);
227 void zebra_ptm_connect(struct thread
*t
)
231 if (ptm_cb
.ptm_sock
== -1) {
232 zebra_ptm_socket_init();
236 if (ptm_cb
.ptm_sock
!= -1) {
238 ptm_cb
.t_read
= NULL
;
239 thread_add_read(zrouter
.master
, zebra_ptm_sock_read
,
240 NULL
, ptm_cb
.ptm_sock
, &ptm_cb
.t_read
);
241 zebra_bfd_peer_replay_req();
243 zebra_ptm_send_status_req();
244 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
245 } else if (ptm_cb
.reconnect_time
< ZEBRA_PTM_RECONNECT_TIME_MAX
) {
246 ptm_cb
.reconnect_time
*= 2;
247 if (ptm_cb
.reconnect_time
> ZEBRA_PTM_RECONNECT_TIME_MAX
)
248 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_MAX
;
250 ptm_cb
.t_timer
= NULL
;
251 thread_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
252 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
253 } else if (ptm_cb
.reconnect_time
>= ZEBRA_PTM_RECONNECT_TIME_MAX
) {
254 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
258 DEFUN (zebra_ptm_enable
,
259 zebra_ptm_enable_cmd
,
261 "Enable neighbor check with specified topology\n")
264 struct interface
*ifp
;
265 struct zebra_if
*if_data
;
267 ptm_cb
.ptm_enable
= ZEBRA_IF_PTM_ENABLE_ON
;
269 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
270 FOR_ALL_INTERFACES (vrf
, ifp
)
271 if (!ifp
->ptm_enable
) {
272 if_data
= (struct zebra_if
*)ifp
->info
;
274 && (if_data
->ptm_enable
275 == ZEBRA_IF_PTM_ENABLE_UNSPEC
)) {
277 ZEBRA_IF_PTM_ENABLE_ON
;
279 /* Assign a default unknown status */
280 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
283 zebra_ptm_connect(NULL
);
288 DEFUN (no_zebra_ptm_enable
,
289 no_zebra_ptm_enable_cmd
,
292 "Enable neighbor check with specified topology\n")
294 ptm_cb
.ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
295 zebra_ptm_reset_status(1);
299 DEFUN (zebra_ptm_enable_if
,
300 zebra_ptm_enable_if_cmd
,
302 "Enable neighbor check with specified topology\n")
304 VTY_DECLVAR_CONTEXT(interface
, ifp
);
305 struct zebra_if
*if_data
;
307 int send_linkdown
= 0;
310 if_data
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_UNSPEC
;
312 if (ifp
->ifindex
== IFINDEX_INTERNAL
) {
316 old_ptm_enable
= ifp
->ptm_enable
;
317 ifp
->ptm_enable
= ptm_cb
.ptm_enable
;
319 if (if_is_no_ptm_operative(ifp
))
322 if (!old_ptm_enable
&& ptm_cb
.ptm_enable
) {
323 if (!if_is_operative(ifp
) && send_linkdown
) {
324 if (IS_ZEBRA_DEBUG_EVENT
)
325 zlog_debug("%s: Bringing down interface %s",
326 __func__
, ifp
->name
);
334 DEFUN (no_zebra_ptm_enable_if
,
335 no_zebra_ptm_enable_if_cmd
,
338 "Enable neighbor check with specified topology\n")
340 VTY_DECLVAR_CONTEXT(interface
, ifp
);
342 struct zebra_if
*if_data
;
344 if ((ifp
->ifindex
!= IFINDEX_INTERNAL
) && (ifp
->ptm_enable
)) {
345 if (!if_is_operative(ifp
))
348 ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
349 if (if_is_no_ptm_operative(ifp
) && send_linkup
) {
350 if (IS_ZEBRA_DEBUG_EVENT
)
351 zlog_debug("%s: Bringing up interface %s",
352 __func__
, ifp
->name
);
358 if_data
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
364 void zebra_ptm_write(struct vty
*vty
)
366 if (ptm_cb
.ptm_enable
)
367 vty_out(vty
, "ptm-enable\n");
372 static int zebra_ptm_socket_init(void)
376 struct sockaddr_un addr
;
378 ptm_cb
.ptm_sock
= -1;
380 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
383 if (set_nonblocking(sock
) < 0) {
384 if (IS_ZEBRA_DEBUG_EVENT
)
385 zlog_debug("%s: Unable to set socket non blocking[%s]",
386 __func__
, safe_strerror(errno
));
391 /* Make server socket. */
392 memset(&addr
, 0, sizeof(addr
));
393 addr
.sun_family
= AF_UNIX
;
394 memcpy(&addr
.sun_path
, ZEBRA_PTM_SOCK_NAME
,
395 sizeof(ZEBRA_PTM_SOCK_NAME
));
397 ret
= connect(sock
, (struct sockaddr
*)&addr
,
398 sizeof(addr
.sun_family
) + sizeof(ZEBRA_PTM_SOCK_NAME
)
401 if (IS_ZEBRA_DEBUG_EVENT
)
402 zlog_debug("%s: Unable to connect to socket %s [%s]",
403 __func__
, ZEBRA_PTM_SOCK_NAME
,
404 safe_strerror(errno
));
408 ptm_cb
.ptm_sock
= sock
;
412 static void zebra_ptm_install_commands(void)
414 install_element(CONFIG_NODE
, &zebra_ptm_enable_cmd
);
415 install_element(CONFIG_NODE
, &no_zebra_ptm_enable_cmd
);
416 install_element(INTERFACE_NODE
, &zebra_ptm_enable_if_cmd
);
417 install_element(INTERFACE_NODE
, &no_zebra_ptm_enable_if_cmd
);
420 /* BFD session goes down, send message to the protocols. */
421 static void if_bfd_session_update(struct interface
*ifp
, struct prefix
*dp
,
422 struct prefix
*sp
, int status
,
425 if (IS_ZEBRA_DEBUG_EVENT
) {
426 char buf
[2][INET6_ADDRSTRLEN
];
430 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d on %s %s event",
431 inet_ntop(dp
->family
, &dp
->u
.prefix
, buf
[0],
433 dp
->prefixlen
, ifp
->name
,
434 bfd_get_status_str(status
));
436 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
439 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d with src %s/%d and vrf %s(%u) %s event",
440 inet_ntop(dp
->family
, &dp
->u
.prefix
, buf
[0],
443 inet_ntop(sp
->family
, &sp
->u
.prefix
, buf
[1],
445 sp
->prefixlen
, VRF_LOGNAME(vrf
), vrf_id
,
446 bfd_get_status_str(status
));
450 zebra_interface_bfd_update(ifp
, dp
, sp
, status
, vrf_id
);
453 static int zebra_ptm_handle_bfd_msg(void *arg
, void *in_ctxt
,
454 struct interface
*ifp
)
460 struct prefix dest_prefix
;
461 struct prefix src_prefix
;
464 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDSTATUS_STR
, bfdst_str
);
466 if (bfdst_str
[0] == '\0') {
470 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDDEST_STR
, dest_str
);
472 if (dest_str
[0] == '\0') {
473 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
474 ZEBRA_PTM_BFDDEST_STR
);
478 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDSRC_STR
, src_str
);
480 if (src_str
[0] == '\0') {
481 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
482 ZEBRA_PTM_BFDSRC_STR
);
486 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDVRF_STR
, vrf_str
);
488 if (vrf_str
[0] == '\0') {
489 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
490 ZEBRA_PTM_BFDVRF_STR
);
494 if (IS_ZEBRA_DEBUG_EVENT
)
496 "%s: Recv Port [%s] bfd status [%s] vrf [%s] peer [%s] local [%s]",
497 __func__
, ifp
? ifp
->name
: "N/A", bfdst_str
, vrf_str
,
500 if (str2prefix(dest_str
, &dest_prefix
) == 0) {
501 flog_err(EC_ZEBRA_PREFIX_PARSE_ERROR
,
502 "%s: Peer addr %s not found", __func__
, dest_str
);
506 memset(&src_prefix
, 0, sizeof(src_prefix
));
507 if (strcmp(ZEBRA_PTM_INVALID_SRC_IP
, src_str
)) {
508 if (str2prefix(src_str
, &src_prefix
) == 0) {
509 flog_err(EC_ZEBRA_PREFIX_PARSE_ERROR
,
510 "%s: Local addr %s not found", __func__
,
516 if (!strcmp(ZEBRA_PTM_INVALID_VRF
, vrf_str
) && ifp
) {
517 vrf_id
= ifp
->vrf
->vrf_id
;
521 pVrf
= vrf_lookup_by_name(vrf_str
);
523 vrf_id
= pVrf
->vrf_id
;
525 vrf_id
= VRF_DEFAULT
;
528 if (!strcmp(bfdst_str
, ZEBRA_PTM_BFDSTATUS_DOWN_STR
)) {
529 if_bfd_session_update(ifp
, &dest_prefix
, &src_prefix
,
530 BFD_STATUS_DOWN
, vrf_id
);
532 if_bfd_session_update(ifp
, &dest_prefix
, &src_prefix
,
533 BFD_STATUS_UP
, vrf_id
);
539 static int zebra_ptm_handle_cbl_msg(void *arg
, void *in_ctxt
,
540 struct interface
*ifp
, char *cbl_str
)
544 if (IS_ZEBRA_DEBUG_EVENT
)
545 zlog_debug("%s: Recv Port [%s] cbl status [%s]", __func__
,
548 if (!strcmp(cbl_str
, ZEBRA_PTM_PASS_STR
)
549 && (ifp
->ptm_status
!= ZEBRA_PTM_STATUS_UP
)) {
551 if (ifp
->ptm_status
== ZEBRA_PTM_STATUS_DOWN
)
553 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UP
;
554 if (ifp
->ptm_enable
&& if_is_no_ptm_operative(ifp
)
557 } else if (!strcmp(cbl_str
, ZEBRA_PTM_FAIL_STR
)
558 && (ifp
->ptm_status
!= ZEBRA_PTM_STATUS_DOWN
)) {
559 ifp
->ptm_status
= ZEBRA_PTM_STATUS_DOWN
;
560 if (ifp
->ptm_enable
&& if_is_no_ptm_operative(ifp
))
568 * zebra_ptm_handle_msg_cb - The purpose of this callback function is to handle
569 * all the command responses and notifications received from PTM.
571 * Command responses: Upon establishing connection with PTM, Zebra requests
572 * status of all interfaces using 'get-status' command if global ptm-enable
573 * knob is enabled. As a response to the get-status command PTM sends status
574 * of all the interfaces as command responses. All other type of command
575 * responses with cmd_status key word are dropped. The sole purpose of
576 * registering this function as callback for the command responses is to
577 * handle the responses to get-status command.
579 * Notifications: Cable status and BFD session status changes are sent as
580 * notifications by PTM. So, this function is also the callback function for
581 * processing all the notifications from the PTM.
584 static int zebra_ptm_handle_msg_cb(void *arg
, void *in_ctxt
)
586 struct interface
*ifp
= NULL
;
589 char cmd_status_str
[32];
591 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_CMD_STATUS_STR
,
594 /* Drop command response messages */
595 if (cmd_status_str
[0] != '\0') {
599 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_PORT_STR
, port_str
);
601 if (port_str
[0] == '\0') {
602 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
607 if (strcmp(ZEBRA_PTM_INVALID_PORT_NAME
, port_str
)) {
611 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
612 ifp
= if_lookup_by_name_vrf(port_str
, vrf
);
615 if (!vrf_is_backend_netns())
621 flog_warn(EC_ZEBRA_UNKNOWN_INTERFACE
,
622 "%s: %s not found in interface list",
627 flog_warn(EC_ZEBRA_UNKNOWN_INTERFACE
,
628 "%s: multiple interface with name %s",
634 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_CBL_STR
, cbl_str
);
636 if (cbl_str
[0] == '\0') {
637 return zebra_ptm_handle_bfd_msg(arg
, in_ctxt
, ifp
);
640 return zebra_ptm_handle_cbl_msg(arg
, in_ctxt
, ifp
,
648 void zebra_ptm_sock_read(struct thread
*thread
)
654 sock
= THREAD_FD(thread
);
659 /* PTM communicates in CSV format */
661 rc
= ptm_lib_process_msg(ptm_hdl
, sock
, ptm_cb
.in_data
,
662 ZEBRA_PTM_MAX_SOCKBUF
, NULL
);
665 if (((rc
== 0) && !errno
)
666 || (errno
&& (errno
!= EWOULDBLOCK
) && (errno
!= EAGAIN
))) {
667 flog_err_sys(EC_LIB_SOCKET
,
668 "%s routing socket error: %s(%d) bytes %d",
669 __func__
, safe_strerror(errno
), errno
, rc
);
671 close(ptm_cb
.ptm_sock
);
672 ptm_cb
.ptm_sock
= -1;
673 zebra_ptm_reset_status(0);
674 ptm_cb
.t_timer
= NULL
;
675 thread_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
676 ptm_cb
.reconnect_time
,
681 ptm_cb
.t_read
= NULL
;
682 thread_add_read(zrouter
.master
, zebra_ptm_sock_read
, NULL
,
683 ptm_cb
.ptm_sock
, &ptm_cb
.t_read
);
686 /* BFD peer/dst register/update */
687 void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS
)
693 uint8_t multi_hop_cnt
;
695 unsigned int min_rx_timer
;
696 unsigned int min_tx_timer
;
697 char if_name
[INTERFACE_NAMSIZ
];
700 char buf
[INET6_ADDRSTRLEN
];
702 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
706 if (hdr
->command
== ZEBRA_BFD_DEST_UPDATE
)
707 client
->bfd_peer_upd8_cnt
++;
709 client
->bfd_peer_add_cnt
++;
711 if (IS_ZEBRA_DEBUG_EVENT
)
712 zlog_debug("bfd_dst_register msg from client %s: length=%d",
713 zebra_route_string(client
->proto
), hdr
->length
);
715 if (ptm_cb
.ptm_sock
== -1) {
716 ptm_cb
.t_timer
= NULL
;
717 thread_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
718 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
722 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
723 snprintf(tmp_buf
, sizeof(tmp_buf
), "%s", ZEBRA_PTM_BFD_START_CMD
);
724 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
725 snprintf(tmp_buf
, sizeof(tmp_buf
), "%s",
726 zebra_route_string(client
->proto
));
727 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
733 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", pid
);
734 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
737 STREAM_GETW(s
, dst_p
.family
);
739 if (dst_p
.family
== AF_INET
)
740 dst_p
.prefixlen
= IPV4_MAX_BYTELEN
;
742 dst_p
.prefixlen
= IPV6_MAX_BYTELEN
;
744 STREAM_GET(&dst_p
.u
.prefix
, s
, dst_p
.prefixlen
);
745 if (dst_p
.family
== AF_INET
) {
746 inet_ntop(AF_INET
, &dst_p
.u
.prefix4
, buf
, sizeof(buf
));
747 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
748 ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
750 inet_ntop(AF_INET6
, &dst_p
.u
.prefix6
, buf
, sizeof(buf
));
751 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
752 ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
755 STREAM_GETL(s
, min_rx_timer
);
756 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", min_rx_timer
);
757 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_MIN_RX_FIELD
,
759 STREAM_GETL(s
, min_tx_timer
);
760 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", min_tx_timer
);
761 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_MIN_TX_FIELD
,
763 STREAM_GETC(s
, detect_mul
);
764 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", detect_mul
);
765 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_DETECT_MULT_FIELD
,
768 STREAM_GETC(s
, multi_hop
);
770 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", 1);
771 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
772 ZEBRA_PTM_BFD_MULTI_HOP_FIELD
, tmp_buf
);
773 STREAM_GETW(s
, src_p
.family
);
775 if (src_p
.family
== AF_INET
)
776 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
778 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
780 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
781 if (src_p
.family
== AF_INET
) {
782 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
, sizeof(buf
));
783 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
784 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
786 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
, sizeof(buf
));
787 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
788 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
791 STREAM_GETC(s
, multi_hop_cnt
);
792 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", multi_hop_cnt
);
793 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
794 ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD
, tmp_buf
);
796 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
797 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
798 ZEBRA_PTM_BFD_VRF_NAME_FIELD
,
801 if (dst_p
.family
== AF_INET6
) {
802 STREAM_GETW(s
, src_p
.family
);
804 if (src_p
.family
== AF_INET
)
805 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
807 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
809 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
810 if (src_p
.family
== AF_INET
) {
811 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
,
813 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
814 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
817 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
,
819 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
820 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
825 STREAM_GET(if_name
, s
, len
);
828 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
829 ZEBRA_PTM_BFD_IFNAME_FIELD
, if_name
);
831 STREAM_GETC(s
, cbit_set
);
832 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", cbit_set
);
833 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
834 ZEBRA_PTM_BFD_CBIT_FIELD
, tmp_buf
);
836 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", 1);
837 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEND_EVENT
,
840 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
842 if (IS_ZEBRA_DEBUG_SEND
)
843 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
845 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
850 ptm_lib_cleanup_msg(ptm_hdl
, out_ctxt
);
853 /* BFD peer/dst deregister */
854 void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS
)
860 char if_name
[INTERFACE_NAMSIZ
];
862 char buf
[INET6_ADDRSTRLEN
];
864 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
868 client
->bfd_peer_del_cnt
++;
870 if (IS_ZEBRA_DEBUG_EVENT
)
871 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
872 zebra_route_string(client
->proto
), hdr
->length
);
874 if (ptm_cb
.ptm_sock
== -1) {
875 ptm_cb
.t_timer
= NULL
;
876 thread_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
877 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
881 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
883 snprintf(tmp_buf
, sizeof(tmp_buf
), "%s", ZEBRA_PTM_BFD_STOP_CMD
);
884 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
886 snprintf(tmp_buf
, sizeof(tmp_buf
), "%s",
887 zebra_route_string(client
->proto
));
888 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
894 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", pid
);
895 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
898 STREAM_GETW(s
, dst_p
.family
);
900 if (dst_p
.family
== AF_INET
)
901 dst_p
.prefixlen
= IPV4_MAX_BYTELEN
;
903 dst_p
.prefixlen
= IPV6_MAX_BYTELEN
;
905 STREAM_GET(&dst_p
.u
.prefix
, s
, dst_p
.prefixlen
);
906 if (dst_p
.family
== AF_INET
)
907 inet_ntop(AF_INET
, &dst_p
.u
.prefix4
, buf
, sizeof(buf
));
909 inet_ntop(AF_INET6
, &dst_p
.u
.prefix6
, buf
, sizeof(buf
));
910 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
913 STREAM_GETC(s
, multi_hop
);
915 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", 1);
916 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
917 ZEBRA_PTM_BFD_MULTI_HOP_FIELD
, tmp_buf
);
919 STREAM_GETW(s
, src_p
.family
);
921 if (src_p
.family
== AF_INET
)
922 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
924 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
926 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
927 if (src_p
.family
== AF_INET
)
928 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
, sizeof(buf
));
930 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
, sizeof(buf
));
931 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
932 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
934 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
935 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
936 ZEBRA_PTM_BFD_VRF_NAME_FIELD
,
939 if (dst_p
.family
== AF_INET6
) {
940 STREAM_GETW(s
, src_p
.family
);
942 if (src_p
.family
== AF_INET
)
943 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
945 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
947 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
948 if (src_p
.family
== AF_INET
) {
949 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
,
951 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
952 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
955 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
,
957 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
958 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
964 STREAM_GET(if_name
, s
, len
);
967 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
968 ZEBRA_PTM_BFD_IFNAME_FIELD
, if_name
);
971 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
972 if (IS_ZEBRA_DEBUG_SEND
)
973 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
976 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
981 ptm_lib_cleanup_msg(ptm_hdl
, out_ctxt
);
984 /* BFD client register */
985 void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS
)
989 void *out_ctxt
= NULL
;
991 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
993 client
->bfd_client_reg_cnt
++;
995 if (IS_ZEBRA_DEBUG_EVENT
)
996 zlog_debug("bfd_client_register msg from client %s: length=%d",
997 zebra_route_string(client
->proto
), hdr
->length
);
1000 STREAM_GETL(s
, pid
);
1002 if (ptm_cb
.ptm_sock
== -1) {
1003 ptm_cb
.t_timer
= NULL
;
1004 thread_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
1005 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
1009 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
1011 snprintf(tmp_buf
, sizeof(tmp_buf
), "%s", ZEBRA_PTM_BFD_CLIENT_REG_CMD
);
1012 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
1014 snprintf(tmp_buf
, sizeof(tmp_buf
), "%s",
1015 zebra_route_string(client
->proto
));
1016 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
1019 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", pid
);
1020 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
1023 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
1025 if (IS_ZEBRA_DEBUG_SEND
)
1026 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
1028 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
1030 SET_FLAG(ptm_cb
.client_flags
[client
->proto
],
1031 ZEBRA_PTM_BFD_CLIENT_FLAG_REG
);
1037 * IF we ever add more STREAM_GETXXX functions after the out_ctxt
1038 * is allocated then we need to add this code back in
1041 * ptm_lib_cleanup_msg(ptm_hdl, out_ctxt);
1046 /* BFD client deregister */
1047 int zebra_ptm_bfd_client_deregister(struct zserv
*client
)
1049 uint8_t proto
= client
->proto
;
1052 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
1054 if (!IS_BFD_ENABLED_PROTOCOL(proto
))
1057 if (IS_ZEBRA_DEBUG_EVENT
)
1058 zlog_debug("bfd_client_deregister msg for client %s",
1059 zebra_route_string(proto
));
1061 if (ptm_cb
.ptm_sock
== -1) {
1062 ptm_cb
.t_timer
= NULL
;
1063 thread_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
1064 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
1068 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
1070 snprintf(tmp_buf
, sizeof(tmp_buf
), "%s",
1071 ZEBRA_PTM_BFD_CLIENT_DEREG_CMD
);
1072 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
1074 snprintf(tmp_buf
, sizeof(tmp_buf
), "%s", zebra_route_string(proto
));
1075 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
1078 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
1080 if (IS_ZEBRA_DEBUG_SEND
)
1081 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
1084 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
1085 UNSET_FLAG(ptm_cb
.client_flags
[proto
], ZEBRA_PTM_BFD_CLIENT_FLAG_REG
);
1090 int zebra_ptm_get_enable_state(void)
1092 return ptm_cb
.ptm_enable
;
1096 * zebra_ptm_get_status_str - Convert status to a display string.
1098 static const char *zebra_ptm_get_status_str(int status
)
1101 case ZEBRA_PTM_STATUS_DOWN
:
1103 case ZEBRA_PTM_STATUS_UP
:
1105 case ZEBRA_PTM_STATUS_UNKNOWN
:
1111 void zebra_ptm_show_status(struct vty
*vty
, json_object
*json
,
1112 struct interface
*ifp
)
1116 if (ifp
->ptm_enable
)
1117 status
= zebra_ptm_get_status_str(ifp
->ptm_status
);
1119 status
= "disabled";
1122 json_object_string_add(json
, "ptmStatus", status
);
1124 vty_out(vty
, " PTM status: %s\n", status
);
1127 void zebra_ptm_send_status_req(void)
1130 int len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
1132 if (ptm_cb
.ptm_enable
) {
1133 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
,
1135 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
,
1136 ZEBRA_PTM_GET_STATUS_CMD
);
1137 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &len
);
1139 zebra_ptm_send_message(ptm_cb
.out_data
, len
);
1143 void zebra_ptm_reset_status(int ptm_disable
)
1146 struct interface
*ifp
;
1149 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
1150 FOR_ALL_INTERFACES (vrf
, ifp
) {
1152 if (ifp
->ptm_enable
) {
1153 if (!if_is_operative(ifp
))
1158 ZEBRA_IF_PTM_ENABLE_OFF
;
1159 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1161 if (if_is_operative(ifp
) && send_linkup
) {
1162 if (IS_ZEBRA_DEBUG_EVENT
)
1164 "%s: Bringing up interface %s",
1165 __func__
, ifp
->name
);
1172 void zebra_ptm_if_init(struct zebra_if
*zebra_ifp
)
1174 zebra_ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_UNSPEC
;
1177 void zebra_ptm_if_set_ptm_state(struct interface
*ifp
,
1178 struct zebra_if
*zebra_ifp
)
1180 if (zebra_ifp
&& zebra_ifp
->ptm_enable
!= ZEBRA_IF_PTM_ENABLE_UNSPEC
)
1181 ifp
->ptm_enable
= zebra_ifp
->ptm_enable
;
1184 void zebra_ptm_if_write(struct vty
*vty
, struct zebra_if
*zebra_ifp
)
1186 if (zebra_ifp
->ptm_enable
== ZEBRA_IF_PTM_ENABLE_OFF
)
1187 vty_out(vty
, " no ptm-enable\n");
1190 #else /* HAVE_BFDD */
1195 struct ptm_process
{
1196 struct zserv
*pp_zs
;
1199 TAILQ_ENTRY(ptm_process
) pp_entry
;
1201 TAILQ_HEAD(ppqueue
, ptm_process
) ppqueue
;
1203 DEFINE_MTYPE_STATIC(ZEBRA
, ZEBRA_PTM_BFD_PROCESS
,
1204 "PTM BFD process registration table.");
1209 static struct ptm_process
*pp_new(pid_t pid
, struct zserv
*zs
);
1210 static struct ptm_process
*pp_lookup_byzs(struct zserv
*zs
);
1211 static void pp_free(struct ptm_process
*pp
);
1212 static void pp_free_all(void);
1214 static void zebra_ptm_send_bfdd(struct stream
*msg
);
1215 static void zebra_ptm_send_clients(struct stream
*msg
);
1216 static int _zebra_ptm_bfd_client_deregister(struct zserv
*zs
);
1217 static void _zebra_ptm_reroute(struct zserv
*zs
, struct zebra_vrf
*zvrf
,
1218 struct stream
*msg
, uint32_t command
);
1222 * Process PID registration.
1224 static struct ptm_process
*pp_new(pid_t pid
, struct zserv
*zs
)
1226 struct ptm_process
*pp
;
1229 /* Sanity check: more than one client can't have the same PID. */
1230 TAILQ_FOREACH(pp
, &ppqueue
, pp_entry
) {
1231 if (pp
->pp_pid
== pid
&& pp
->pp_zs
!= zs
)
1232 zlog_err("%s:%d pid and client pointer doesn't match",
1233 __FILE__
, __LINE__
);
1235 #endif /* PTM_DEBUG */
1237 /* Lookup for duplicates. */
1238 pp
= pp_lookup_byzs(zs
);
1242 /* Allocate and register new process. */
1243 pp
= XCALLOC(MTYPE_ZEBRA_PTM_BFD_PROCESS
, sizeof(*pp
));
1247 TAILQ_INSERT_HEAD(&ppqueue
, pp
, pp_entry
);
1252 static struct ptm_process
*pp_lookup_byzs(struct zserv
*zs
)
1254 struct ptm_process
*pp
;
1256 TAILQ_FOREACH(pp
, &ppqueue
, pp_entry
) {
1257 if (pp
->pp_zs
!= zs
)
1266 static void pp_free(struct ptm_process
*pp
)
1271 TAILQ_REMOVE(&ppqueue
, pp
, pp_entry
);
1272 XFREE(MTYPE_ZEBRA_PTM_BFD_PROCESS
, pp
);
1275 static void pp_free_all(void)
1277 struct ptm_process
*pp
;
1279 while (!TAILQ_EMPTY(&ppqueue
)) {
1280 pp
= TAILQ_FIRST(&ppqueue
);
1287 * Use the FRR's internal daemon implementation.
1289 static void zebra_ptm_send_bfdd(struct stream
*msg
)
1291 struct listnode
*node
;
1292 struct zserv
*client
;
1293 struct stream
*msgc
;
1295 /* Create copy for replication. */
1296 msgc
= stream_dup(msg
);
1298 /* Send message to all running BFDd daemons. */
1299 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
1300 if (client
->proto
!= ZEBRA_ROUTE_BFD
)
1303 zserv_send_message(client
, msg
);
1305 /* Allocate more messages. */
1306 msg
= stream_dup(msgc
);
1313 static void zebra_ptm_send_clients(struct stream
*msg
)
1315 struct listnode
*node
;
1316 struct zserv
*client
;
1317 struct stream
*msgc
;
1319 /* Create copy for replication. */
1320 msgc
= stream_dup(msg
);
1322 /* Send message to all running client daemons. */
1323 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
1324 if (!IS_BFD_ENABLED_PROTOCOL(client
->proto
))
1327 zserv_send_message(client
, msg
);
1329 /* Allocate more messages. */
1330 msg
= stream_dup(msgc
);
1337 static int _zebra_ptm_bfd_client_deregister(struct zserv
*zs
)
1340 struct ptm_process
*pp
;
1342 if (!IS_BFD_ENABLED_PROTOCOL(zs
->proto
))
1345 /* Find daemon pid by zebra connection pointer. */
1346 pp
= pp_lookup_byzs(zs
);
1348 zlog_err("%s:%d failed to find process pid registration",
1349 __FILE__
, __LINE__
);
1353 /* Generate, send message and free() daemon related data. */
1354 msg
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1356 zlog_debug("%s: not enough memory", __func__
);
1361 * The message type will be ZEBRA_BFD_DEST_REPLAY so we can use only
1362 * one callback at the `bfdd` side, however the real command
1363 * number will be included right after the zebra header.
1365 zclient_create_header(msg
, ZEBRA_BFD_DEST_REPLAY
, 0);
1366 stream_putl(msg
, ZEBRA_BFD_CLIENT_DEREGISTER
);
1368 /* Put process PID. */
1369 stream_putl(msg
, pp
->pp_pid
);
1371 /* Update the data pointers. */
1372 stream_putw_at(msg
, 0, stream_get_endp(msg
));
1374 zebra_ptm_send_bfdd(msg
);
1381 void zebra_ptm_init(void)
1383 /* Initialize the ptm process information list. */
1384 TAILQ_INIT(&ppqueue
);
1387 * Send deregistration messages to BFD daemon when some other
1388 * daemon closes. This will help avoid sending daemons
1389 * unnecessary notification messages.
1391 hook_register(zserv_client_close
, _zebra_ptm_bfd_client_deregister
);
1394 void zebra_ptm_finish(void)
1396 /* Remove the client disconnect hook and free all memory. */
1397 hook_unregister(zserv_client_close
, _zebra_ptm_bfd_client_deregister
);
1405 static void _zebra_ptm_reroute(struct zserv
*zs
, struct zebra_vrf
*zvrf
,
1406 struct stream
*msg
, uint32_t command
)
1408 struct stream
*msgc
;
1409 char buf
[ZEBRA_MAX_PACKET_SIZ
];
1412 /* Create BFD header */
1413 msgc
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1414 zclient_create_header(msgc
, ZEBRA_BFD_DEST_REPLAY
, zvrf
->vrf
->vrf_id
);
1415 stream_putl(msgc
, command
);
1417 if (STREAM_READABLE(msg
) > STREAM_WRITEABLE(msgc
)) {
1418 zlog_warn("Cannot fit extended BFD header plus original message contents into ZAPI packet; dropping message");
1419 goto stream_failure
;
1422 /* Copy original message, excluding header, into new message */
1423 stream_get_from(buf
, msg
, stream_get_getp(msg
), STREAM_READABLE(msg
));
1424 stream_put(msgc
, buf
, STREAM_READABLE(msg
));
1426 /* Update length field */
1427 stream_putw_at(msgc
, 0, STREAM_READABLE(msgc
));
1429 zebra_ptm_send_bfdd(msgc
);
1432 /* Registrate process PID for shutdown hook. */
1433 STREAM_GETL(msg
, ppid
);
1441 zlog_err("%s:%d failed to registrate client pid", __FILE__
, __LINE__
);
1444 void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS
)
1446 if (IS_ZEBRA_DEBUG_EVENT
)
1447 zlog_debug("bfd_dst_register msg from client %s: length=%d",
1448 zebra_route_string(client
->proto
), hdr
->length
);
1450 _zebra_ptm_reroute(client
, zvrf
, msg
, ZEBRA_BFD_DEST_REGISTER
);
1453 void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS
)
1455 if (IS_ZEBRA_DEBUG_EVENT
)
1456 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
1457 zebra_route_string(client
->proto
), hdr
->length
);
1459 _zebra_ptm_reroute(client
, zvrf
, msg
, ZEBRA_BFD_DEST_DEREGISTER
);
1462 void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS
)
1464 if (IS_ZEBRA_DEBUG_EVENT
)
1465 zlog_debug("bfd_client_register msg from client %s: length=%d",
1466 zebra_route_string(client
->proto
), hdr
->length
);
1468 _zebra_ptm_reroute(client
, zvrf
, msg
, ZEBRA_BFD_CLIENT_REGISTER
);
1471 void zebra_ptm_bfd_dst_replay(ZAPI_HANDLER_ARGS
)
1473 struct stream
*msgc
;
1474 size_t zmsglen
, zhdrlen
;
1479 * Replay messages with HAVE_BFDD are meant to be replayed to
1480 * the client daemons. These messages are composed and
1481 * originated from the `bfdd` daemon.
1483 if (IS_ZEBRA_DEBUG_EVENT
)
1484 zlog_debug("bfd_dst_update msg from client %s: length=%d",
1485 zebra_route_string(client
->proto
), hdr
->length
);
1488 * Client messages must be re-routed, otherwise do the `bfdd`
1489 * special treatment.
1491 if (client
->proto
!= ZEBRA_ROUTE_BFD
) {
1492 _zebra_ptm_reroute(client
, zvrf
, msg
, ZEBRA_BFD_DEST_REPLAY
);
1496 /* Figure out if this is an DEST_UPDATE or DEST_REPLAY. */
1497 if (stream_getl2(msg
, &cmd
) == false) {
1498 zlog_err("%s: expected at least 4 bytes (command)", __func__
);
1503 * Don't modify message in the zebra API. In order to do that we
1504 * need to allocate a new message stream and copy the message
1505 * provided by zebra.
1507 msgc
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1509 zlog_debug("%s: not enough memory", __func__
);
1513 /* Calculate our header size plus the message contents. */
1514 if (cmd
!= ZEBRA_BFD_DEST_REPLAY
) {
1515 zhdrlen
= ZEBRA_HEADER_SIZE
;
1516 zmsglen
= msg
->endp
- msg
->getp
;
1517 memcpy(msgc
->data
+ zhdrlen
, msg
->data
+ msg
->getp
, zmsglen
);
1519 zclient_create_header(msgc
, cmd
, zvrf_id(zvrf
));
1522 msgc
->endp
= zhdrlen
+ zmsglen
;
1524 zclient_create_header(msgc
, cmd
, zvrf_id(zvrf
));
1526 /* Update the data pointers. */
1527 stream_putw_at(msgc
, 0, stream_get_endp(msgc
));
1529 zebra_ptm_send_clients(msgc
);
1535 void zebra_ptm_if_init(struct zebra_if
*zifp
__attribute__((__unused__
)))
1540 int zebra_ptm_get_enable_state(void)
1545 void zebra_ptm_show_status(struct vty
*vty
__attribute__((__unused__
)),
1546 json_object
*json
__attribute__((__unused__
)),
1547 struct interface
*ifp
__attribute__((__unused__
)))
1552 void zebra_ptm_write(struct vty
*vty
__attribute__((__unused__
)))
1557 void zebra_ptm_if_write(struct vty
*vty
__attribute__((__unused__
)),
1558 struct zebra_if
*zifp
__attribute__((__unused__
)))
1562 void zebra_ptm_if_set_ptm_state(struct interface
*i
__attribute__((__unused__
)),
1563 struct zebra_if
*zi
__attribute__((__unused__
)))
1568 #endif /* HAVE_BFDD */