1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Kernel routing table updates using netlink over GNU/Linux system.
3 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
8 #include <sys/un.h> /* for sockaddr_un */
21 #include "lib_errors.h"
23 #include "zebra/debug.h"
24 #include "zebra/interface.h"
25 #include "zebra/zebra_errors.h"
26 #include "zebra/zebra_ptm.h"
27 #include "zebra/zebra_ptm_redistribute.h"
28 #include "zebra/zebra_router.h"
29 #include "zebra_vrf.h"
32 * Choose the BFD implementation that we'll use.
34 * There are two implementations:
35 * - PTM BFD: which uses an external daemon;
36 * - bfdd: FRR's own BFD daemon;
40 #define ZEBRA_PTM_RECONNECT_TIME_INITIAL 1 /* initial reconnect is 1s */
41 #define ZEBRA_PTM_RECONNECT_TIME_MAX 300
44 #define PTM_HEADER_LEN 37
46 const char ZEBRA_PTM_GET_STATUS_CMD
[] = "get-status";
47 const char ZEBRA_PTM_BFD_START_CMD
[] = "start-bfd-sess";
48 const char ZEBRA_PTM_BFD_STOP_CMD
[] = "stop-bfd-sess";
49 const char ZEBRA_PTM_BFD_CLIENT_REG_CMD
[] = "reg-bfd-client";
50 const char ZEBRA_PTM_BFD_CLIENT_DEREG_CMD
[] = "dereg-bfd-client";
52 const char ZEBRA_PTM_CMD_STR
[] = "cmd";
53 const char ZEBRA_PTM_CMD_STATUS_STR
[] = "cmd_status";
54 const char ZEBRA_PTM_PORT_STR
[] = "port";
55 const char ZEBRA_PTM_CBL_STR
[] = "cbl status";
56 const char ZEBRA_PTM_PASS_STR
[] = "pass";
57 const char ZEBRA_PTM_FAIL_STR
[] = "fail";
58 const char ZEBRA_PTM_BFDSTATUS_STR
[] = "state";
59 const char ZEBRA_PTM_BFDSTATUS_UP_STR
[] = "Up";
60 const char ZEBRA_PTM_BFDSTATUS_DOWN_STR
[] = "Down";
61 const char ZEBRA_PTM_BFDDEST_STR
[] = "peer";
62 const char ZEBRA_PTM_BFDSRC_STR
[] = "local";
63 const char ZEBRA_PTM_BFDVRF_STR
[] = "vrf";
64 const char ZEBRA_PTM_INVALID_PORT_NAME
[] = "N/A";
65 const char ZEBRA_PTM_INVALID_SRC_IP
[] = "N/A";
66 const char ZEBRA_PTM_INVALID_VRF
[] = "N/A";
68 const char ZEBRA_PTM_BFD_DST_IP_FIELD
[] = "dstIPaddr";
69 const char ZEBRA_PTM_BFD_SRC_IP_FIELD
[] = "srcIPaddr";
70 const char ZEBRA_PTM_BFD_MIN_RX_FIELD
[] = "requiredMinRx";
71 const char ZEBRA_PTM_BFD_MIN_TX_FIELD
[] = "upMinTx";
72 const char ZEBRA_PTM_BFD_DETECT_MULT_FIELD
[] = "detectMult";
73 const char ZEBRA_PTM_BFD_MULTI_HOP_FIELD
[] = "multiHop";
74 const char ZEBRA_PTM_BFD_CLIENT_FIELD
[] = "client";
75 const char ZEBRA_PTM_BFD_SEQID_FIELD
[] = "seqid";
76 const char ZEBRA_PTM_BFD_IFNAME_FIELD
[] = "ifName";
77 const char ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD
[] = "maxHopCnt";
78 const char ZEBRA_PTM_BFD_SEND_EVENT
[] = "sendEvent";
79 const char ZEBRA_PTM_BFD_VRF_NAME_FIELD
[] = "vrfName";
80 const char ZEBRA_PTM_BFD_CBIT_FIELD
[] = "bfdcbit";
82 static ptm_lib_handle_t
*ptm_hdl
;
84 struct zebra_ptm_cb ptm_cb
;
86 static int zebra_ptm_socket_init(void);
87 void zebra_ptm_sock_read(struct event
*thread
);
88 static void zebra_ptm_install_commands(void);
89 static int zebra_ptm_handle_msg_cb(void *arg
, void *in_ctxt
);
90 void zebra_bfd_peer_replay_req(void);
91 void zebra_ptm_send_status_req(void);
92 void zebra_ptm_reset_status(int ptm_disable
);
93 static int zebra_ptm_bfd_client_deregister(struct zserv
*client
);
95 const char ZEBRA_PTM_SOCK_NAME
[] = "\0/var/run/ptmd.socket";
97 void zebra_ptm_init(void)
101 memset(&ptm_cb
, 0, sizeof(ptm_cb
));
103 ptm_cb
.out_data
= calloc(1, ZEBRA_PTM_SEND_MAX_SOCKBUF
);
104 if (!ptm_cb
.out_data
) {
105 zlog_debug("%s: Allocation of send data failed", __func__
);
109 ptm_cb
.in_data
= calloc(1, ZEBRA_PTM_MAX_SOCKBUF
);
110 if (!ptm_cb
.in_data
) {
111 zlog_debug("%s: Allocation of recv data failed", __func__
);
112 free(ptm_cb
.out_data
);
116 ptm_cb
.pid
= getpid();
117 zebra_ptm_install_commands();
119 snprintf(buf
, sizeof(buf
), "%s", FRR_PTM_NAME
);
120 ptm_hdl
= ptm_lib_register(buf
, NULL
, zebra_ptm_handle_msg_cb
,
121 zebra_ptm_handle_msg_cb
);
122 ptm_cb
.wb
= buffer_new(0);
124 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
126 ptm_cb
.ptm_sock
= -1;
128 hook_register(zserv_client_close
, zebra_ptm_bfd_client_deregister
);
131 void zebra_ptm_finish(void)
133 buffer_flush_all(ptm_cb
.wb
, ptm_cb
.ptm_sock
);
138 free(ptm_cb
.out_data
);
141 free(ptm_cb
.in_data
);
144 EVENT_OFF(ptm_cb
.t_read
);
145 EVENT_OFF(ptm_cb
.t_write
);
146 EVENT_OFF(ptm_cb
.t_timer
);
149 buffer_free(ptm_cb
.wb
);
151 if (ptm_cb
.ptm_sock
>= 0)
152 close(ptm_cb
.ptm_sock
);
155 static void zebra_ptm_flush_messages(struct event
*thread
)
157 ptm_cb
.t_write
= NULL
;
159 if (ptm_cb
.ptm_sock
== -1)
164 switch (buffer_flush_available(ptm_cb
.wb
, ptm_cb
.ptm_sock
)) {
166 flog_err_sys(EC_LIB_SOCKET
, "%s ptm socket error: %s", __func__
,
167 safe_strerror(errno
));
168 close(ptm_cb
.ptm_sock
);
169 ptm_cb
.ptm_sock
= -1;
170 zebra_ptm_reset_status(0);
171 ptm_cb
.t_timer
= NULL
;
172 event_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
173 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
176 ptm_cb
.t_write
= NULL
;
177 event_add_write(zrouter
.master
, zebra_ptm_flush_messages
, NULL
,
178 ptm_cb
.ptm_sock
, &ptm_cb
.t_write
);
185 static int zebra_ptm_send_message(char *data
, int size
)
188 switch (buffer_write(ptm_cb
.wb
, ptm_cb
.ptm_sock
, data
, size
)) {
190 flog_err_sys(EC_LIB_SOCKET
, "%s ptm socket error: %s", __func__
,
191 safe_strerror(errno
));
192 close(ptm_cb
.ptm_sock
);
193 ptm_cb
.ptm_sock
= -1;
194 zebra_ptm_reset_status(0);
195 ptm_cb
.t_timer
= NULL
;
196 event_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
197 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
200 EVENT_OFF(ptm_cb
.t_write
);
203 event_add_write(zrouter
.master
, zebra_ptm_flush_messages
, NULL
,
204 ptm_cb
.ptm_sock
, &ptm_cb
.t_write
);
211 void zebra_ptm_connect(struct event
*t
)
215 if (ptm_cb
.ptm_sock
== -1) {
216 zebra_ptm_socket_init();
220 if (ptm_cb
.ptm_sock
!= -1) {
222 ptm_cb
.t_read
= NULL
;
223 event_add_read(zrouter
.master
, zebra_ptm_sock_read
,
224 NULL
, ptm_cb
.ptm_sock
, &ptm_cb
.t_read
);
225 zebra_bfd_peer_replay_req();
227 zebra_ptm_send_status_req();
228 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
229 } else if (ptm_cb
.reconnect_time
< ZEBRA_PTM_RECONNECT_TIME_MAX
) {
230 ptm_cb
.reconnect_time
*= 2;
231 if (ptm_cb
.reconnect_time
> ZEBRA_PTM_RECONNECT_TIME_MAX
)
232 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_MAX
;
234 ptm_cb
.t_timer
= NULL
;
235 event_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
236 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
237 } else if (ptm_cb
.reconnect_time
>= ZEBRA_PTM_RECONNECT_TIME_MAX
) {
238 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
242 DEFUN (zebra_ptm_enable
,
243 zebra_ptm_enable_cmd
,
245 "Enable neighbor check with specified topology\n")
248 struct interface
*ifp
;
249 struct zebra_if
*if_data
;
251 ptm_cb
.ptm_enable
= ZEBRA_IF_PTM_ENABLE_ON
;
253 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
254 FOR_ALL_INTERFACES (vrf
, ifp
)
255 if (!ifp
->ptm_enable
) {
256 if_data
= (struct zebra_if
*)ifp
->info
;
258 && (if_data
->ptm_enable
259 == ZEBRA_IF_PTM_ENABLE_UNSPEC
)) {
261 ZEBRA_IF_PTM_ENABLE_ON
;
263 /* Assign a default unknown status */
264 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
267 zebra_ptm_connect(NULL
);
272 DEFUN (no_zebra_ptm_enable
,
273 no_zebra_ptm_enable_cmd
,
276 "Enable neighbor check with specified topology\n")
278 ptm_cb
.ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
279 zebra_ptm_reset_status(1);
283 DEFUN (zebra_ptm_enable_if
,
284 zebra_ptm_enable_if_cmd
,
286 "Enable neighbor check with specified topology\n")
288 VTY_DECLVAR_CONTEXT(interface
, ifp
);
289 struct zebra_if
*if_data
;
291 int send_linkdown
= 0;
294 if_data
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_UNSPEC
;
296 if (ifp
->ifindex
== IFINDEX_INTERNAL
) {
300 old_ptm_enable
= ifp
->ptm_enable
;
301 ifp
->ptm_enable
= ptm_cb
.ptm_enable
;
303 if (if_is_no_ptm_operative(ifp
))
306 if (!old_ptm_enable
&& ptm_cb
.ptm_enable
) {
307 if (!if_is_operative(ifp
) && send_linkdown
) {
308 if (IS_ZEBRA_DEBUG_EVENT
)
309 zlog_debug("%s: Bringing down interface %s",
310 __func__
, ifp
->name
);
318 DEFUN (no_zebra_ptm_enable_if
,
319 no_zebra_ptm_enable_if_cmd
,
322 "Enable neighbor check with specified topology\n")
324 VTY_DECLVAR_CONTEXT(interface
, ifp
);
326 struct zebra_if
*if_data
;
328 if ((ifp
->ifindex
!= IFINDEX_INTERNAL
) && (ifp
->ptm_enable
)) {
329 if (!if_is_operative(ifp
))
332 ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
333 if (if_is_no_ptm_operative(ifp
) && send_linkup
) {
334 if (IS_ZEBRA_DEBUG_EVENT
)
335 zlog_debug("%s: Bringing up interface %s",
336 __func__
, ifp
->name
);
342 if_data
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
348 void zebra_ptm_write(struct vty
*vty
)
350 if (ptm_cb
.ptm_enable
)
351 vty_out(vty
, "ptm-enable\n");
356 static int zebra_ptm_socket_init(void)
360 struct sockaddr_un addr
;
362 ptm_cb
.ptm_sock
= -1;
364 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
367 if (set_nonblocking(sock
) < 0) {
368 if (IS_ZEBRA_DEBUG_EVENT
)
369 zlog_debug("%s: Unable to set socket non blocking[%s]",
370 __func__
, safe_strerror(errno
));
375 /* Make server socket. */
376 memset(&addr
, 0, sizeof(addr
));
377 addr
.sun_family
= AF_UNIX
;
378 memcpy(&addr
.sun_path
, ZEBRA_PTM_SOCK_NAME
,
379 sizeof(ZEBRA_PTM_SOCK_NAME
));
381 ret
= connect(sock
, (struct sockaddr
*)&addr
,
382 sizeof(addr
.sun_family
) + sizeof(ZEBRA_PTM_SOCK_NAME
)
385 if (IS_ZEBRA_DEBUG_EVENT
)
386 zlog_debug("%s: Unable to connect to socket %s [%s]",
387 __func__
, ZEBRA_PTM_SOCK_NAME
,
388 safe_strerror(errno
));
392 ptm_cb
.ptm_sock
= sock
;
396 static void zebra_ptm_install_commands(void)
398 install_element(CONFIG_NODE
, &zebra_ptm_enable_cmd
);
399 install_element(CONFIG_NODE
, &no_zebra_ptm_enable_cmd
);
400 install_element(INTERFACE_NODE
, &zebra_ptm_enable_if_cmd
);
401 install_element(INTERFACE_NODE
, &no_zebra_ptm_enable_if_cmd
);
404 /* BFD session goes down, send message to the protocols. */
405 static void if_bfd_session_update(struct interface
*ifp
, struct prefix
*dp
,
406 struct prefix
*sp
, int status
,
409 if (IS_ZEBRA_DEBUG_EVENT
) {
410 char buf
[2][INET6_ADDRSTRLEN
];
414 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d on %s %s event",
415 inet_ntop(dp
->family
, &dp
->u
.prefix
, buf
[0],
417 dp
->prefixlen
, ifp
->name
,
418 bfd_get_status_str(status
));
420 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
423 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d with src %s/%d and vrf %s(%u) %s event",
424 inet_ntop(dp
->family
, &dp
->u
.prefix
, buf
[0],
427 inet_ntop(sp
->family
, &sp
->u
.prefix
, buf
[1],
429 sp
->prefixlen
, VRF_LOGNAME(vrf
), vrf_id
,
430 bfd_get_status_str(status
));
434 zebra_interface_bfd_update(ifp
, dp
, sp
, status
, vrf_id
);
437 static int zebra_ptm_handle_bfd_msg(void *arg
, void *in_ctxt
,
438 struct interface
*ifp
)
444 struct prefix dest_prefix
;
445 struct prefix src_prefix
;
448 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDSTATUS_STR
, bfdst_str
);
450 if (bfdst_str
[0] == '\0') {
454 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDDEST_STR
, dest_str
);
456 if (dest_str
[0] == '\0') {
457 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
458 ZEBRA_PTM_BFDDEST_STR
);
462 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDSRC_STR
, src_str
);
464 if (src_str
[0] == '\0') {
465 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
466 ZEBRA_PTM_BFDSRC_STR
);
470 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDVRF_STR
, vrf_str
);
472 if (vrf_str
[0] == '\0') {
473 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
474 ZEBRA_PTM_BFDVRF_STR
);
478 if (IS_ZEBRA_DEBUG_EVENT
)
480 "%s: Recv Port [%s] bfd status [%s] vrf [%s] peer [%s] local [%s]",
481 __func__
, ifp
? ifp
->name
: "N/A", bfdst_str
, vrf_str
,
484 if (str2prefix(dest_str
, &dest_prefix
) == 0) {
485 flog_err(EC_ZEBRA_PREFIX_PARSE_ERROR
,
486 "%s: Peer addr %s not found", __func__
, dest_str
);
490 memset(&src_prefix
, 0, sizeof(src_prefix
));
491 if (strcmp(ZEBRA_PTM_INVALID_SRC_IP
, src_str
)) {
492 if (str2prefix(src_str
, &src_prefix
) == 0) {
493 flog_err(EC_ZEBRA_PREFIX_PARSE_ERROR
,
494 "%s: Local addr %s not found", __func__
,
500 if (!strcmp(ZEBRA_PTM_INVALID_VRF
, vrf_str
) && ifp
) {
501 vrf_id
= ifp
->vrf
->vrf_id
;
505 pVrf
= vrf_lookup_by_name(vrf_str
);
507 vrf_id
= pVrf
->vrf_id
;
509 vrf_id
= VRF_DEFAULT
;
512 if (!strcmp(bfdst_str
, ZEBRA_PTM_BFDSTATUS_DOWN_STR
)) {
513 if_bfd_session_update(ifp
, &dest_prefix
, &src_prefix
,
514 BFD_STATUS_DOWN
, vrf_id
);
516 if_bfd_session_update(ifp
, &dest_prefix
, &src_prefix
,
517 BFD_STATUS_UP
, vrf_id
);
523 static int zebra_ptm_handle_cbl_msg(void *arg
, void *in_ctxt
,
524 struct interface
*ifp
, char *cbl_str
)
528 if (IS_ZEBRA_DEBUG_EVENT
)
529 zlog_debug("%s: Recv Port [%s] cbl status [%s]", __func__
,
532 if (!strcmp(cbl_str
, ZEBRA_PTM_PASS_STR
)
533 && (ifp
->ptm_status
!= ZEBRA_PTM_STATUS_UP
)) {
535 if (ifp
->ptm_status
== ZEBRA_PTM_STATUS_DOWN
)
537 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UP
;
538 if (ifp
->ptm_enable
&& if_is_no_ptm_operative(ifp
)
541 } else if (!strcmp(cbl_str
, ZEBRA_PTM_FAIL_STR
)
542 && (ifp
->ptm_status
!= ZEBRA_PTM_STATUS_DOWN
)) {
543 ifp
->ptm_status
= ZEBRA_PTM_STATUS_DOWN
;
544 if (ifp
->ptm_enable
&& if_is_no_ptm_operative(ifp
))
552 * zebra_ptm_handle_msg_cb - The purpose of this callback function is to handle
553 * all the command responses and notifications received from PTM.
555 * Command responses: Upon establishing connection with PTM, Zebra requests
556 * status of all interfaces using 'get-status' command if global ptm-enable
557 * knob is enabled. As a response to the get-status command PTM sends status
558 * of all the interfaces as command responses. All other type of command
559 * responses with cmd_status key word are dropped. The sole purpose of
560 * registering this function as callback for the command responses is to
561 * handle the responses to get-status command.
563 * Notifications: Cable status and BFD session status changes are sent as
564 * notifications by PTM. So, this function is also the callback function for
565 * processing all the notifications from the PTM.
568 static int zebra_ptm_handle_msg_cb(void *arg
, void *in_ctxt
)
570 struct interface
*ifp
= NULL
;
573 char cmd_status_str
[32];
575 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_CMD_STATUS_STR
,
578 /* Drop command response messages */
579 if (cmd_status_str
[0] != '\0') {
583 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_PORT_STR
, port_str
);
585 if (port_str
[0] == '\0') {
586 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
591 if (strcmp(ZEBRA_PTM_INVALID_PORT_NAME
, port_str
)) {
595 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
596 ifp
= if_lookup_by_name_vrf(port_str
, vrf
);
599 if (!vrf_is_backend_netns())
605 flog_warn(EC_ZEBRA_UNKNOWN_INTERFACE
,
606 "%s: %s not found in interface list",
611 flog_warn(EC_ZEBRA_UNKNOWN_INTERFACE
,
612 "%s: multiple interface with name %s",
618 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_CBL_STR
, cbl_str
);
620 if (cbl_str
[0] == '\0') {
621 return zebra_ptm_handle_bfd_msg(arg
, in_ctxt
, ifp
);
624 return zebra_ptm_handle_cbl_msg(arg
, in_ctxt
, ifp
,
632 void zebra_ptm_sock_read(struct event
*thread
)
638 sock
= EVENT_FD(thread
);
643 /* PTM communicates in CSV format */
645 rc
= ptm_lib_process_msg(ptm_hdl
, sock
, ptm_cb
.in_data
,
646 ZEBRA_PTM_MAX_SOCKBUF
, NULL
);
649 if (((rc
== 0) && !errno
)
650 || (errno
&& (errno
!= EWOULDBLOCK
) && (errno
!= EAGAIN
))) {
651 flog_err_sys(EC_LIB_SOCKET
,
652 "%s routing socket error: %s(%d) bytes %d",
653 __func__
, safe_strerror(errno
), errno
, rc
);
655 close(ptm_cb
.ptm_sock
);
656 ptm_cb
.ptm_sock
= -1;
657 zebra_ptm_reset_status(0);
658 ptm_cb
.t_timer
= NULL
;
659 event_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
660 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
664 ptm_cb
.t_read
= NULL
;
665 event_add_read(zrouter
.master
, zebra_ptm_sock_read
, NULL
,
666 ptm_cb
.ptm_sock
, &ptm_cb
.t_read
);
669 /* BFD peer/dst register/update */
670 void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS
)
676 uint8_t multi_hop_cnt
;
678 unsigned int min_rx_timer
;
679 unsigned int min_tx_timer
;
680 char if_name
[INTERFACE_NAMSIZ
];
683 char buf
[INET6_ADDRSTRLEN
];
685 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
689 if (hdr
->command
== ZEBRA_BFD_DEST_UPDATE
)
690 client
->bfd_peer_upd8_cnt
++;
692 client
->bfd_peer_add_cnt
++;
694 if (IS_ZEBRA_DEBUG_EVENT
)
695 zlog_debug("bfd_dst_register msg from client %s: length=%d",
696 zebra_route_string(client
->proto
), hdr
->length
);
698 if (ptm_cb
.ptm_sock
== -1) {
699 ptm_cb
.t_timer
= NULL
;
700 event_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
701 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
705 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
706 snprintf(tmp_buf
, sizeof(tmp_buf
), "%s", ZEBRA_PTM_BFD_START_CMD
);
707 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
708 snprintf(tmp_buf
, sizeof(tmp_buf
), "%s",
709 zebra_route_string(client
->proto
));
710 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
716 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", pid
);
717 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
720 STREAM_GETW(s
, dst_p
.family
);
722 if (dst_p
.family
== AF_INET
)
723 dst_p
.prefixlen
= IPV4_MAX_BYTELEN
;
725 dst_p
.prefixlen
= IPV6_MAX_BYTELEN
;
727 STREAM_GET(&dst_p
.u
.prefix
, s
, dst_p
.prefixlen
);
728 if (dst_p
.family
== AF_INET
) {
729 inet_ntop(AF_INET
, &dst_p
.u
.prefix4
, buf
, sizeof(buf
));
730 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
731 ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
733 inet_ntop(AF_INET6
, &dst_p
.u
.prefix6
, buf
, sizeof(buf
));
734 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
735 ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
738 STREAM_GETL(s
, min_rx_timer
);
739 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", min_rx_timer
);
740 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_MIN_RX_FIELD
,
742 STREAM_GETL(s
, min_tx_timer
);
743 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", min_tx_timer
);
744 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_MIN_TX_FIELD
,
746 STREAM_GETC(s
, detect_mul
);
747 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", detect_mul
);
748 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_DETECT_MULT_FIELD
,
751 STREAM_GETC(s
, multi_hop
);
753 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", 1);
754 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
755 ZEBRA_PTM_BFD_MULTI_HOP_FIELD
, tmp_buf
);
756 STREAM_GETW(s
, src_p
.family
);
758 if (src_p
.family
== AF_INET
)
759 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
761 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
763 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
764 if (src_p
.family
== AF_INET
) {
765 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
, sizeof(buf
));
766 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
767 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
769 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
, sizeof(buf
));
770 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
771 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
774 STREAM_GETC(s
, multi_hop_cnt
);
775 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", multi_hop_cnt
);
776 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
777 ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD
, tmp_buf
);
779 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
780 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
781 ZEBRA_PTM_BFD_VRF_NAME_FIELD
,
784 if (dst_p
.family
== AF_INET6
) {
785 STREAM_GETW(s
, src_p
.family
);
787 if (src_p
.family
== AF_INET
)
788 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
790 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
792 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
793 if (src_p
.family
== AF_INET
) {
794 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
,
796 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
797 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
800 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
,
802 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
803 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
808 STREAM_GET(if_name
, s
, len
);
811 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
812 ZEBRA_PTM_BFD_IFNAME_FIELD
, if_name
);
814 STREAM_GETC(s
, cbit_set
);
815 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", cbit_set
);
816 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
817 ZEBRA_PTM_BFD_CBIT_FIELD
, tmp_buf
);
819 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", 1);
820 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEND_EVENT
,
823 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
825 if (IS_ZEBRA_DEBUG_SEND
)
826 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
828 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
833 ptm_lib_cleanup_msg(ptm_hdl
, out_ctxt
);
836 /* BFD peer/dst deregister */
837 void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS
)
843 char if_name
[INTERFACE_NAMSIZ
];
845 char buf
[INET6_ADDRSTRLEN
];
847 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
851 client
->bfd_peer_del_cnt
++;
853 if (IS_ZEBRA_DEBUG_EVENT
)
854 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
855 zebra_route_string(client
->proto
), hdr
->length
);
857 if (ptm_cb
.ptm_sock
== -1) {
858 ptm_cb
.t_timer
= NULL
;
859 event_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
860 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
864 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
866 snprintf(tmp_buf
, sizeof(tmp_buf
), "%s", ZEBRA_PTM_BFD_STOP_CMD
);
867 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
869 snprintf(tmp_buf
, sizeof(tmp_buf
), "%s",
870 zebra_route_string(client
->proto
));
871 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
877 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", pid
);
878 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
881 STREAM_GETW(s
, dst_p
.family
);
883 if (dst_p
.family
== AF_INET
)
884 dst_p
.prefixlen
= IPV4_MAX_BYTELEN
;
886 dst_p
.prefixlen
= IPV6_MAX_BYTELEN
;
888 STREAM_GET(&dst_p
.u
.prefix
, s
, dst_p
.prefixlen
);
889 if (dst_p
.family
== AF_INET
)
890 inet_ntop(AF_INET
, &dst_p
.u
.prefix4
, buf
, sizeof(buf
));
892 inet_ntop(AF_INET6
, &dst_p
.u
.prefix6
, buf
, sizeof(buf
));
893 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
896 STREAM_GETC(s
, multi_hop
);
898 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", 1);
899 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
900 ZEBRA_PTM_BFD_MULTI_HOP_FIELD
, tmp_buf
);
902 STREAM_GETW(s
, src_p
.family
);
904 if (src_p
.family
== AF_INET
)
905 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
907 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
909 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
910 if (src_p
.family
== AF_INET
)
911 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
, sizeof(buf
));
913 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
, sizeof(buf
));
914 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
915 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
917 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
918 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
919 ZEBRA_PTM_BFD_VRF_NAME_FIELD
,
922 if (dst_p
.family
== AF_INET6
) {
923 STREAM_GETW(s
, src_p
.family
);
925 if (src_p
.family
== AF_INET
)
926 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
928 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
930 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
931 if (src_p
.family
== AF_INET
) {
932 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
,
934 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
935 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
938 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
,
940 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
941 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
947 STREAM_GET(if_name
, s
, len
);
950 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
951 ZEBRA_PTM_BFD_IFNAME_FIELD
, if_name
);
954 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
955 if (IS_ZEBRA_DEBUG_SEND
)
956 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
959 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
964 ptm_lib_cleanup_msg(ptm_hdl
, out_ctxt
);
967 /* BFD client register */
968 void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS
)
972 void *out_ctxt
= NULL
;
974 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
976 client
->bfd_client_reg_cnt
++;
978 if (IS_ZEBRA_DEBUG_EVENT
)
979 zlog_debug("bfd_client_register msg from client %s: length=%d",
980 zebra_route_string(client
->proto
), hdr
->length
);
985 if (ptm_cb
.ptm_sock
== -1) {
986 ptm_cb
.t_timer
= NULL
;
987 event_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
988 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
992 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
994 snprintf(tmp_buf
, sizeof(tmp_buf
), "%s", ZEBRA_PTM_BFD_CLIENT_REG_CMD
);
995 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
997 snprintf(tmp_buf
, sizeof(tmp_buf
), "%s",
998 zebra_route_string(client
->proto
));
999 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
1002 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", pid
);
1003 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
1006 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
1008 if (IS_ZEBRA_DEBUG_SEND
)
1009 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
1011 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
1013 SET_FLAG(ptm_cb
.client_flags
[client
->proto
],
1014 ZEBRA_PTM_BFD_CLIENT_FLAG_REG
);
1020 * IF we ever add more STREAM_GETXXX functions after the out_ctxt
1021 * is allocated then we need to add this code back in
1024 * ptm_lib_cleanup_msg(ptm_hdl, out_ctxt);
1029 /* BFD client deregister */
1030 int zebra_ptm_bfd_client_deregister(struct zserv
*client
)
1032 uint8_t proto
= client
->proto
;
1035 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
1037 if (!IS_BFD_ENABLED_PROTOCOL(proto
))
1040 if (IS_ZEBRA_DEBUG_EVENT
)
1041 zlog_debug("bfd_client_deregister msg for client %s",
1042 zebra_route_string(proto
));
1044 if (ptm_cb
.ptm_sock
== -1) {
1045 ptm_cb
.t_timer
= NULL
;
1046 event_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
1047 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
1051 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
1053 snprintf(tmp_buf
, sizeof(tmp_buf
), "%s",
1054 ZEBRA_PTM_BFD_CLIENT_DEREG_CMD
);
1055 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
1057 snprintf(tmp_buf
, sizeof(tmp_buf
), "%s", zebra_route_string(proto
));
1058 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
1061 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
1063 if (IS_ZEBRA_DEBUG_SEND
)
1064 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
1067 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
1068 UNSET_FLAG(ptm_cb
.client_flags
[proto
], ZEBRA_PTM_BFD_CLIENT_FLAG_REG
);
1073 int zebra_ptm_get_enable_state(void)
1075 return ptm_cb
.ptm_enable
;
1079 * zebra_ptm_get_status_str - Convert status to a display string.
1081 static const char *zebra_ptm_get_status_str(int status
)
1084 case ZEBRA_PTM_STATUS_DOWN
:
1086 case ZEBRA_PTM_STATUS_UP
:
1088 case ZEBRA_PTM_STATUS_UNKNOWN
:
1094 void zebra_ptm_show_status(struct vty
*vty
, json_object
*json
,
1095 struct interface
*ifp
)
1099 if (ifp
->ptm_enable
)
1100 status
= zebra_ptm_get_status_str(ifp
->ptm_status
);
1102 status
= "disabled";
1105 json_object_string_add(json
, "ptmStatus", status
);
1107 vty_out(vty
, " PTM status: %s\n", status
);
1110 void zebra_ptm_send_status_req(void)
1113 int len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
1115 if (ptm_cb
.ptm_enable
) {
1116 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
,
1118 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
,
1119 ZEBRA_PTM_GET_STATUS_CMD
);
1120 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &len
);
1122 zebra_ptm_send_message(ptm_cb
.out_data
, len
);
1126 void zebra_ptm_reset_status(int ptm_disable
)
1129 struct interface
*ifp
;
1132 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
1133 FOR_ALL_INTERFACES (vrf
, ifp
) {
1135 if (ifp
->ptm_enable
) {
1136 if (!if_is_operative(ifp
))
1141 ZEBRA_IF_PTM_ENABLE_OFF
;
1142 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1144 if (if_is_operative(ifp
) && send_linkup
) {
1145 if (IS_ZEBRA_DEBUG_EVENT
)
1147 "%s: Bringing up interface %s",
1148 __func__
, ifp
->name
);
1155 void zebra_ptm_if_init(struct zebra_if
*zebra_ifp
)
1157 zebra_ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_UNSPEC
;
1160 void zebra_ptm_if_set_ptm_state(struct interface
*ifp
,
1161 struct zebra_if
*zebra_ifp
)
1163 if (zebra_ifp
&& zebra_ifp
->ptm_enable
!= ZEBRA_IF_PTM_ENABLE_UNSPEC
)
1164 ifp
->ptm_enable
= zebra_ifp
->ptm_enable
;
1167 void zebra_ptm_if_write(struct vty
*vty
, struct zebra_if
*zebra_ifp
)
1169 if (zebra_ifp
->ptm_enable
== ZEBRA_IF_PTM_ENABLE_OFF
)
1170 vty_out(vty
, " no ptm-enable\n");
1173 #else /* HAVE_BFDD */
1178 struct ptm_process
{
1179 struct zserv
*pp_zs
;
1182 TAILQ_ENTRY(ptm_process
) pp_entry
;
1184 TAILQ_HEAD(ppqueue
, ptm_process
) ppqueue
;
1186 DEFINE_MTYPE_STATIC(ZEBRA
, ZEBRA_PTM_BFD_PROCESS
,
1187 "PTM BFD process registration table.");
1192 static struct ptm_process
*pp_new(pid_t pid
, struct zserv
*zs
);
1193 static struct ptm_process
*pp_lookup_byzs(struct zserv
*zs
);
1194 static void pp_free(struct ptm_process
*pp
);
1195 static void pp_free_all(void);
1197 static void zebra_ptm_send_bfdd(struct stream
*msg
);
1198 static void zebra_ptm_send_clients(struct stream
*msg
);
1199 static int _zebra_ptm_bfd_client_deregister(struct zserv
*zs
);
1200 static void _zebra_ptm_reroute(struct zserv
*zs
, struct zebra_vrf
*zvrf
,
1201 struct stream
*msg
, uint32_t command
);
1205 * Process PID registration.
1207 static struct ptm_process
*pp_new(pid_t pid
, struct zserv
*zs
)
1209 struct ptm_process
*pp
;
1212 /* Sanity check: more than one client can't have the same PID. */
1213 TAILQ_FOREACH(pp
, &ppqueue
, pp_entry
) {
1214 if (pp
->pp_pid
== pid
&& pp
->pp_zs
!= zs
)
1215 zlog_err("%s:%d pid and client pointer doesn't match",
1216 __FILE__
, __LINE__
);
1218 #endif /* PTM_DEBUG */
1220 /* Lookup for duplicates. */
1221 pp
= pp_lookup_byzs(zs
);
1225 /* Allocate and register new process. */
1226 pp
= XCALLOC(MTYPE_ZEBRA_PTM_BFD_PROCESS
, sizeof(*pp
));
1230 TAILQ_INSERT_HEAD(&ppqueue
, pp
, pp_entry
);
1235 static struct ptm_process
*pp_lookup_byzs(struct zserv
*zs
)
1237 struct ptm_process
*pp
;
1239 TAILQ_FOREACH(pp
, &ppqueue
, pp_entry
) {
1240 if (pp
->pp_zs
!= zs
)
1249 static void pp_free(struct ptm_process
*pp
)
1254 TAILQ_REMOVE(&ppqueue
, pp
, pp_entry
);
1255 XFREE(MTYPE_ZEBRA_PTM_BFD_PROCESS
, pp
);
1258 static void pp_free_all(void)
1260 struct ptm_process
*pp
;
1262 while (!TAILQ_EMPTY(&ppqueue
)) {
1263 pp
= TAILQ_FIRST(&ppqueue
);
1270 * Use the FRR's internal daemon implementation.
1272 static void zebra_ptm_send_bfdd(struct stream
*msg
)
1274 struct listnode
*node
;
1275 struct zserv
*client
;
1276 struct stream
*msgc
;
1278 /* Create copy for replication. */
1279 msgc
= stream_dup(msg
);
1281 /* Send message to all running BFDd daemons. */
1282 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
1283 if (client
->proto
!= ZEBRA_ROUTE_BFD
)
1286 zserv_send_message(client
, msg
);
1288 /* Allocate more messages. */
1289 msg
= stream_dup(msgc
);
1296 static void zebra_ptm_send_clients(struct stream
*msg
)
1298 struct listnode
*node
;
1299 struct zserv
*client
;
1300 struct stream
*msgc
;
1302 /* Create copy for replication. */
1303 msgc
= stream_dup(msg
);
1305 /* Send message to all running client daemons. */
1306 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
1307 if (!IS_BFD_ENABLED_PROTOCOL(client
->proto
))
1310 zserv_send_message(client
, msg
);
1312 /* Allocate more messages. */
1313 msg
= stream_dup(msgc
);
1320 static int _zebra_ptm_bfd_client_deregister(struct zserv
*zs
)
1323 struct ptm_process
*pp
;
1325 if (!IS_BFD_ENABLED_PROTOCOL(zs
->proto
))
1328 /* Find daemon pid by zebra connection pointer. */
1329 pp
= pp_lookup_byzs(zs
);
1331 zlog_err("%s:%d failed to find process pid registration",
1332 __FILE__
, __LINE__
);
1336 /* Generate, send message and free() daemon related data. */
1337 msg
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1339 zlog_debug("%s: not enough memory", __func__
);
1344 * The message type will be ZEBRA_BFD_DEST_REPLAY so we can use only
1345 * one callback at the `bfdd` side, however the real command
1346 * number will be included right after the zebra header.
1348 zclient_create_header(msg
, ZEBRA_BFD_DEST_REPLAY
, 0);
1349 stream_putl(msg
, ZEBRA_BFD_CLIENT_DEREGISTER
);
1351 /* Put process PID. */
1352 stream_putl(msg
, pp
->pp_pid
);
1354 /* Update the data pointers. */
1355 stream_putw_at(msg
, 0, stream_get_endp(msg
));
1357 zebra_ptm_send_bfdd(msg
);
1364 void zebra_ptm_init(void)
1366 /* Initialize the ptm process information list. */
1367 TAILQ_INIT(&ppqueue
);
1370 * Send deregistration messages to BFD daemon when some other
1371 * daemon closes. This will help avoid sending daemons
1372 * unnecessary notification messages.
1374 hook_register(zserv_client_close
, _zebra_ptm_bfd_client_deregister
);
1377 void zebra_ptm_finish(void)
1379 /* Remove the client disconnect hook and free all memory. */
1380 hook_unregister(zserv_client_close
, _zebra_ptm_bfd_client_deregister
);
1388 static void _zebra_ptm_reroute(struct zserv
*zs
, struct zebra_vrf
*zvrf
,
1389 struct stream
*msg
, uint32_t command
)
1391 struct stream
*msgc
;
1392 char buf
[ZEBRA_MAX_PACKET_SIZ
];
1395 /* Create BFD header */
1396 msgc
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1397 zclient_create_header(msgc
, ZEBRA_BFD_DEST_REPLAY
, zvrf
->vrf
->vrf_id
);
1398 stream_putl(msgc
, command
);
1400 if (STREAM_READABLE(msg
) > STREAM_WRITEABLE(msgc
)) {
1401 zlog_warn("Cannot fit extended BFD header plus original message contents into ZAPI packet; dropping message");
1402 goto stream_failure
;
1405 /* Copy original message, excluding header, into new message */
1406 stream_get_from(buf
, msg
, stream_get_getp(msg
), STREAM_READABLE(msg
));
1407 stream_put(msgc
, buf
, STREAM_READABLE(msg
));
1409 /* Update length field */
1410 stream_putw_at(msgc
, 0, STREAM_READABLE(msgc
));
1412 zebra_ptm_send_bfdd(msgc
);
1415 /* Registrate process PID for shutdown hook. */
1416 STREAM_GETL(msg
, ppid
);
1424 zlog_err("%s:%d failed to registrate client pid", __FILE__
, __LINE__
);
1427 void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS
)
1429 if (IS_ZEBRA_DEBUG_EVENT
)
1430 zlog_debug("bfd_dst_register msg from client %s: length=%d",
1431 zebra_route_string(client
->proto
), hdr
->length
);
1433 _zebra_ptm_reroute(client
, zvrf
, msg
, ZEBRA_BFD_DEST_REGISTER
);
1436 void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS
)
1438 if (IS_ZEBRA_DEBUG_EVENT
)
1439 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
1440 zebra_route_string(client
->proto
), hdr
->length
);
1442 _zebra_ptm_reroute(client
, zvrf
, msg
, ZEBRA_BFD_DEST_DEREGISTER
);
1445 void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS
)
1447 if (IS_ZEBRA_DEBUG_EVENT
)
1448 zlog_debug("bfd_client_register msg from client %s: length=%d",
1449 zebra_route_string(client
->proto
), hdr
->length
);
1451 _zebra_ptm_reroute(client
, zvrf
, msg
, ZEBRA_BFD_CLIENT_REGISTER
);
1454 void zebra_ptm_bfd_dst_replay(ZAPI_HANDLER_ARGS
)
1456 struct stream
*msgc
;
1457 size_t zmsglen
, zhdrlen
;
1462 * Replay messages with HAVE_BFDD are meant to be replayed to
1463 * the client daemons. These messages are composed and
1464 * originated from the `bfdd` daemon.
1466 if (IS_ZEBRA_DEBUG_EVENT
)
1467 zlog_debug("bfd_dst_update msg from client %s: length=%d",
1468 zebra_route_string(client
->proto
), hdr
->length
);
1471 * Client messages must be re-routed, otherwise do the `bfdd`
1472 * special treatment.
1474 if (client
->proto
!= ZEBRA_ROUTE_BFD
) {
1475 _zebra_ptm_reroute(client
, zvrf
, msg
, ZEBRA_BFD_DEST_REPLAY
);
1479 /* Figure out if this is an DEST_UPDATE or DEST_REPLAY. */
1480 if (stream_getl2(msg
, &cmd
) == false) {
1481 zlog_err("%s: expected at least 4 bytes (command)", __func__
);
1486 * Don't modify message in the zebra API. In order to do that we
1487 * need to allocate a new message stream and copy the message
1488 * provided by zebra.
1490 msgc
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1492 zlog_debug("%s: not enough memory", __func__
);
1496 /* Calculate our header size plus the message contents. */
1497 if (cmd
!= ZEBRA_BFD_DEST_REPLAY
) {
1498 zhdrlen
= ZEBRA_HEADER_SIZE
;
1499 zmsglen
= msg
->endp
- msg
->getp
;
1500 memcpy(msgc
->data
+ zhdrlen
, msg
->data
+ msg
->getp
, zmsglen
);
1502 zclient_create_header(msgc
, cmd
, zvrf_id(zvrf
));
1505 msgc
->endp
= zhdrlen
+ zmsglen
;
1507 zclient_create_header(msgc
, cmd
, zvrf_id(zvrf
));
1509 /* Update the data pointers. */
1510 stream_putw_at(msgc
, 0, stream_get_endp(msgc
));
1512 zebra_ptm_send_clients(msgc
);
1518 void zebra_ptm_if_init(struct zebra_if
*zifp
__attribute__((__unused__
)))
1523 int zebra_ptm_get_enable_state(void)
1528 void zebra_ptm_show_status(struct vty
*vty
__attribute__((__unused__
)),
1529 json_object
*json
__attribute__((__unused__
)),
1530 struct interface
*ifp
__attribute__((__unused__
)))
1535 void zebra_ptm_write(struct vty
*vty
__attribute__((__unused__
)))
1540 void zebra_ptm_if_write(struct vty
*vty
__attribute__((__unused__
)),
1541 struct zebra_if
*zifp
__attribute__((__unused__
)))
1545 void zebra_ptm_if_set_ptm_state(struct interface
*i
__attribute__((__unused__
)),
1546 struct zebra_if
*zi
__attribute__((__unused__
)))
1551 #endif /* HAVE_BFDD */