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 */
19 #include "lib/version.h"
22 #include "lib_errors.h"
24 #include "zebra/debug.h"
25 #include "zebra/interface.h"
26 #include "zebra/zebra_errors.h"
27 #include "zebra/zebra_ptm.h"
28 #include "zebra/zebra_ptm_redistribute.h"
29 #include "zebra/zebra_router.h"
30 #include "zebra_vrf.h"
33 * Choose the BFD implementation that we'll use.
35 * There are two implementations:
36 * - PTM BFD: which uses an external daemon;
37 * - bfdd: FRR's own BFD daemon;
41 #define ZEBRA_PTM_RECONNECT_TIME_INITIAL 1 /* initial reconnect is 1s */
42 #define ZEBRA_PTM_RECONNECT_TIME_MAX 300
45 #define PTM_HEADER_LEN 37
47 const char ZEBRA_PTM_GET_STATUS_CMD
[] = "get-status";
48 const char ZEBRA_PTM_BFD_START_CMD
[] = "start-bfd-sess";
49 const char ZEBRA_PTM_BFD_STOP_CMD
[] = "stop-bfd-sess";
50 const char ZEBRA_PTM_BFD_CLIENT_REG_CMD
[] = "reg-bfd-client";
51 const char ZEBRA_PTM_BFD_CLIENT_DEREG_CMD
[] = "dereg-bfd-client";
53 const char ZEBRA_PTM_CMD_STR
[] = "cmd";
54 const char ZEBRA_PTM_CMD_STATUS_STR
[] = "cmd_status";
55 const char ZEBRA_PTM_PORT_STR
[] = "port";
56 const char ZEBRA_PTM_CBL_STR
[] = "cbl status";
57 const char ZEBRA_PTM_PASS_STR
[] = "pass";
58 const char ZEBRA_PTM_FAIL_STR
[] = "fail";
59 const char ZEBRA_PTM_BFDSTATUS_STR
[] = "state";
60 const char ZEBRA_PTM_BFDSTATUS_UP_STR
[] = "Up";
61 const char ZEBRA_PTM_BFDSTATUS_DOWN_STR
[] = "Down";
62 const char ZEBRA_PTM_BFDDEST_STR
[] = "peer";
63 const char ZEBRA_PTM_BFDSRC_STR
[] = "local";
64 const char ZEBRA_PTM_BFDVRF_STR
[] = "vrf";
65 const char ZEBRA_PTM_INVALID_PORT_NAME
[] = "N/A";
66 const char ZEBRA_PTM_INVALID_SRC_IP
[] = "N/A";
67 const char ZEBRA_PTM_INVALID_VRF
[] = "N/A";
69 const char ZEBRA_PTM_BFD_DST_IP_FIELD
[] = "dstIPaddr";
70 const char ZEBRA_PTM_BFD_SRC_IP_FIELD
[] = "srcIPaddr";
71 const char ZEBRA_PTM_BFD_MIN_RX_FIELD
[] = "requiredMinRx";
72 const char ZEBRA_PTM_BFD_MIN_TX_FIELD
[] = "upMinTx";
73 const char ZEBRA_PTM_BFD_DETECT_MULT_FIELD
[] = "detectMult";
74 const char ZEBRA_PTM_BFD_MULTI_HOP_FIELD
[] = "multiHop";
75 const char ZEBRA_PTM_BFD_CLIENT_FIELD
[] = "client";
76 const char ZEBRA_PTM_BFD_SEQID_FIELD
[] = "seqid";
77 const char ZEBRA_PTM_BFD_IFNAME_FIELD
[] = "ifName";
78 const char ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD
[] = "maxHopCnt";
79 const char ZEBRA_PTM_BFD_SEND_EVENT
[] = "sendEvent";
80 const char ZEBRA_PTM_BFD_VRF_NAME_FIELD
[] = "vrfName";
81 const char ZEBRA_PTM_BFD_CBIT_FIELD
[] = "bfdcbit";
83 static ptm_lib_handle_t
*ptm_hdl
;
85 struct zebra_ptm_cb ptm_cb
;
87 static int zebra_ptm_socket_init(void);
88 void zebra_ptm_sock_read(struct event
*thread
);
89 static void zebra_ptm_install_commands(void);
90 static int zebra_ptm_handle_msg_cb(void *arg
, void *in_ctxt
);
91 void zebra_bfd_peer_replay_req(void);
92 void zebra_ptm_send_status_req(void);
93 void zebra_ptm_reset_status(int ptm_disable
);
94 static int zebra_ptm_bfd_client_deregister(struct zserv
*client
);
96 const char ZEBRA_PTM_SOCK_NAME
[] = "\0/var/run/ptmd.socket";
98 void zebra_ptm_init(void)
102 memset(&ptm_cb
, 0, sizeof(ptm_cb
));
104 ptm_cb
.out_data
= calloc(1, ZEBRA_PTM_SEND_MAX_SOCKBUF
);
105 if (!ptm_cb
.out_data
) {
106 zlog_debug("%s: Allocation of send data failed", __func__
);
110 ptm_cb
.in_data
= calloc(1, ZEBRA_PTM_MAX_SOCKBUF
);
111 if (!ptm_cb
.in_data
) {
112 zlog_debug("%s: Allocation of recv data failed", __func__
);
113 free(ptm_cb
.out_data
);
117 ptm_cb
.pid
= getpid();
118 zebra_ptm_install_commands();
120 snprintf(buf
, sizeof(buf
), "%s", FRR_PTM_NAME
);
121 ptm_hdl
= ptm_lib_register(buf
, NULL
, zebra_ptm_handle_msg_cb
,
122 zebra_ptm_handle_msg_cb
);
123 ptm_cb
.wb
= buffer_new(0);
125 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
127 ptm_cb
.ptm_sock
= -1;
129 hook_register(zserv_client_close
, zebra_ptm_bfd_client_deregister
);
132 void zebra_ptm_finish(void)
134 buffer_flush_all(ptm_cb
.wb
, ptm_cb
.ptm_sock
);
139 free(ptm_cb
.out_data
);
142 free(ptm_cb
.in_data
);
145 EVENT_OFF(ptm_cb
.t_read
);
146 EVENT_OFF(ptm_cb
.t_write
);
147 EVENT_OFF(ptm_cb
.t_timer
);
150 buffer_free(ptm_cb
.wb
);
152 if (ptm_cb
.ptm_sock
>= 0)
153 close(ptm_cb
.ptm_sock
);
156 static void zebra_ptm_flush_messages(struct event
*thread
)
158 ptm_cb
.t_write
= NULL
;
160 if (ptm_cb
.ptm_sock
== -1)
165 switch (buffer_flush_available(ptm_cb
.wb
, ptm_cb
.ptm_sock
)) {
167 flog_err_sys(EC_LIB_SOCKET
, "%s ptm socket error: %s", __func__
,
168 safe_strerror(errno
));
169 close(ptm_cb
.ptm_sock
);
170 ptm_cb
.ptm_sock
= -1;
171 zebra_ptm_reset_status(0);
172 ptm_cb
.t_timer
= NULL
;
173 event_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
174 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
177 ptm_cb
.t_write
= NULL
;
178 event_add_write(zrouter
.master
, zebra_ptm_flush_messages
, NULL
,
179 ptm_cb
.ptm_sock
, &ptm_cb
.t_write
);
186 static int zebra_ptm_send_message(char *data
, int size
)
189 switch (buffer_write(ptm_cb
.wb
, ptm_cb
.ptm_sock
, data
, size
)) {
191 flog_err_sys(EC_LIB_SOCKET
, "%s ptm socket error: %s", __func__
,
192 safe_strerror(errno
));
193 close(ptm_cb
.ptm_sock
);
194 ptm_cb
.ptm_sock
= -1;
195 zebra_ptm_reset_status(0);
196 ptm_cb
.t_timer
= NULL
;
197 event_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
198 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
201 EVENT_OFF(ptm_cb
.t_write
);
204 event_add_write(zrouter
.master
, zebra_ptm_flush_messages
, NULL
,
205 ptm_cb
.ptm_sock
, &ptm_cb
.t_write
);
212 void zebra_ptm_connect(struct event
*t
)
216 if (ptm_cb
.ptm_sock
== -1) {
217 zebra_ptm_socket_init();
221 if (ptm_cb
.ptm_sock
!= -1) {
223 ptm_cb
.t_read
= NULL
;
224 event_add_read(zrouter
.master
, zebra_ptm_sock_read
,
225 NULL
, ptm_cb
.ptm_sock
, &ptm_cb
.t_read
);
226 zebra_bfd_peer_replay_req();
228 zebra_ptm_send_status_req();
229 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
230 } else if (ptm_cb
.reconnect_time
< ZEBRA_PTM_RECONNECT_TIME_MAX
) {
231 ptm_cb
.reconnect_time
*= 2;
232 if (ptm_cb
.reconnect_time
> ZEBRA_PTM_RECONNECT_TIME_MAX
)
233 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_MAX
;
235 ptm_cb
.t_timer
= NULL
;
236 event_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
237 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
238 } else if (ptm_cb
.reconnect_time
>= ZEBRA_PTM_RECONNECT_TIME_MAX
) {
239 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
243 DEFUN (zebra_ptm_enable
,
244 zebra_ptm_enable_cmd
,
246 "Enable neighbor check with specified topology\n")
249 struct interface
*ifp
;
250 struct zebra_if
*if_data
;
252 ptm_cb
.ptm_enable
= ZEBRA_IF_PTM_ENABLE_ON
;
254 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
255 FOR_ALL_INTERFACES (vrf
, ifp
)
256 if (!ifp
->ptm_enable
) {
257 if_data
= (struct zebra_if
*)ifp
->info
;
259 && (if_data
->ptm_enable
260 == ZEBRA_IF_PTM_ENABLE_UNSPEC
)) {
262 ZEBRA_IF_PTM_ENABLE_ON
;
264 /* Assign a default unknown status */
265 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
268 zebra_ptm_connect(NULL
);
273 DEFUN (no_zebra_ptm_enable
,
274 no_zebra_ptm_enable_cmd
,
277 "Enable neighbor check with specified topology\n")
279 ptm_cb
.ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
280 zebra_ptm_reset_status(1);
284 DEFUN (zebra_ptm_enable_if
,
285 zebra_ptm_enable_if_cmd
,
287 "Enable neighbor check with specified topology\n")
289 VTY_DECLVAR_CONTEXT(interface
, ifp
);
290 struct zebra_if
*if_data
;
292 int send_linkdown
= 0;
295 if_data
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_UNSPEC
;
297 if (ifp
->ifindex
== IFINDEX_INTERNAL
) {
301 old_ptm_enable
= ifp
->ptm_enable
;
302 ifp
->ptm_enable
= ptm_cb
.ptm_enable
;
304 if (if_is_no_ptm_operative(ifp
))
307 if (!old_ptm_enable
&& ptm_cb
.ptm_enable
) {
308 if (!if_is_operative(ifp
) && send_linkdown
) {
309 if (IS_ZEBRA_DEBUG_EVENT
)
310 zlog_debug("%s: Bringing down interface %s",
311 __func__
, ifp
->name
);
319 DEFUN (no_zebra_ptm_enable_if
,
320 no_zebra_ptm_enable_if_cmd
,
323 "Enable neighbor check with specified topology\n")
325 VTY_DECLVAR_CONTEXT(interface
, ifp
);
327 struct zebra_if
*if_data
;
329 if ((ifp
->ifindex
!= IFINDEX_INTERNAL
) && (ifp
->ptm_enable
)) {
330 if (!if_is_operative(ifp
))
333 ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
334 if (if_is_no_ptm_operative(ifp
) && send_linkup
) {
335 if (IS_ZEBRA_DEBUG_EVENT
)
336 zlog_debug("%s: Bringing up interface %s",
337 __func__
, ifp
->name
);
343 if_data
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
349 void zebra_ptm_write(struct vty
*vty
)
351 if (ptm_cb
.ptm_enable
)
352 vty_out(vty
, "ptm-enable\n");
357 static int zebra_ptm_socket_init(void)
361 struct sockaddr_un addr
;
363 ptm_cb
.ptm_sock
= -1;
365 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
368 if (set_nonblocking(sock
) < 0) {
369 if (IS_ZEBRA_DEBUG_EVENT
)
370 zlog_debug("%s: Unable to set socket non blocking[%s]",
371 __func__
, safe_strerror(errno
));
376 /* Make server socket. */
377 memset(&addr
, 0, sizeof(addr
));
378 addr
.sun_family
= AF_UNIX
;
379 memcpy(&addr
.sun_path
, ZEBRA_PTM_SOCK_NAME
,
380 sizeof(ZEBRA_PTM_SOCK_NAME
));
382 ret
= connect(sock
, (struct sockaddr
*)&addr
,
383 sizeof(addr
.sun_family
) + sizeof(ZEBRA_PTM_SOCK_NAME
)
386 if (IS_ZEBRA_DEBUG_EVENT
)
387 zlog_debug("%s: Unable to connect to socket %s [%s]",
388 __func__
, ZEBRA_PTM_SOCK_NAME
,
389 safe_strerror(errno
));
393 ptm_cb
.ptm_sock
= sock
;
397 static void zebra_ptm_install_commands(void)
399 install_element(CONFIG_NODE
, &zebra_ptm_enable_cmd
);
400 install_element(CONFIG_NODE
, &no_zebra_ptm_enable_cmd
);
401 install_element(INTERFACE_NODE
, &zebra_ptm_enable_if_cmd
);
402 install_element(INTERFACE_NODE
, &no_zebra_ptm_enable_if_cmd
);
405 /* BFD session goes down, send message to the protocols. */
406 static void if_bfd_session_update(struct interface
*ifp
, struct prefix
*dp
,
407 struct prefix
*sp
, int status
,
410 if (IS_ZEBRA_DEBUG_EVENT
) {
411 char buf
[2][INET6_ADDRSTRLEN
];
415 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d on %s %s event",
416 inet_ntop(dp
->family
, &dp
->u
.prefix
, buf
[0],
418 dp
->prefixlen
, ifp
->name
,
419 bfd_get_status_str(status
));
421 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
424 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d with src %s/%d and vrf %s(%u) %s event",
425 inet_ntop(dp
->family
, &dp
->u
.prefix
, buf
[0],
428 inet_ntop(sp
->family
, &sp
->u
.prefix
, buf
[1],
430 sp
->prefixlen
, VRF_LOGNAME(vrf
), vrf_id
,
431 bfd_get_status_str(status
));
435 zebra_interface_bfd_update(ifp
, dp
, sp
, status
, vrf_id
);
438 static int zebra_ptm_handle_bfd_msg(void *arg
, void *in_ctxt
,
439 struct interface
*ifp
)
445 struct prefix dest_prefix
;
446 struct prefix src_prefix
;
449 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDSTATUS_STR
, bfdst_str
);
451 if (bfdst_str
[0] == '\0') {
455 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDDEST_STR
, dest_str
);
457 if (dest_str
[0] == '\0') {
458 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
459 ZEBRA_PTM_BFDDEST_STR
);
463 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDSRC_STR
, src_str
);
465 if (src_str
[0] == '\0') {
466 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
467 ZEBRA_PTM_BFDSRC_STR
);
471 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDVRF_STR
, vrf_str
);
473 if (vrf_str
[0] == '\0') {
474 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
475 ZEBRA_PTM_BFDVRF_STR
);
479 if (IS_ZEBRA_DEBUG_EVENT
)
481 "%s: Recv Port [%s] bfd status [%s] vrf [%s] peer [%s] local [%s]",
482 __func__
, ifp
? ifp
->name
: "N/A", bfdst_str
, vrf_str
,
485 if (str2prefix(dest_str
, &dest_prefix
) == 0) {
486 flog_err(EC_ZEBRA_PREFIX_PARSE_ERROR
,
487 "%s: Peer addr %s not found", __func__
, dest_str
);
491 memset(&src_prefix
, 0, sizeof(src_prefix
));
492 if (strcmp(ZEBRA_PTM_INVALID_SRC_IP
, src_str
)) {
493 if (str2prefix(src_str
, &src_prefix
) == 0) {
494 flog_err(EC_ZEBRA_PREFIX_PARSE_ERROR
,
495 "%s: Local addr %s not found", __func__
,
501 if (!strcmp(ZEBRA_PTM_INVALID_VRF
, vrf_str
) && ifp
) {
502 vrf_id
= ifp
->vrf
->vrf_id
;
506 pVrf
= vrf_lookup_by_name(vrf_str
);
508 vrf_id
= pVrf
->vrf_id
;
510 vrf_id
= VRF_DEFAULT
;
513 if (!strcmp(bfdst_str
, ZEBRA_PTM_BFDSTATUS_DOWN_STR
)) {
514 if_bfd_session_update(ifp
, &dest_prefix
, &src_prefix
,
515 BFD_STATUS_DOWN
, vrf_id
);
517 if_bfd_session_update(ifp
, &dest_prefix
, &src_prefix
,
518 BFD_STATUS_UP
, vrf_id
);
524 static int zebra_ptm_handle_cbl_msg(void *arg
, void *in_ctxt
,
525 struct interface
*ifp
, char *cbl_str
)
529 if (IS_ZEBRA_DEBUG_EVENT
)
530 zlog_debug("%s: Recv Port [%s] cbl status [%s]", __func__
,
533 if (!strcmp(cbl_str
, ZEBRA_PTM_PASS_STR
)
534 && (ifp
->ptm_status
!= ZEBRA_PTM_STATUS_UP
)) {
536 if (ifp
->ptm_status
== ZEBRA_PTM_STATUS_DOWN
)
538 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UP
;
539 if (ifp
->ptm_enable
&& if_is_no_ptm_operative(ifp
)
542 } else if (!strcmp(cbl_str
, ZEBRA_PTM_FAIL_STR
)
543 && (ifp
->ptm_status
!= ZEBRA_PTM_STATUS_DOWN
)) {
544 ifp
->ptm_status
= ZEBRA_PTM_STATUS_DOWN
;
545 if (ifp
->ptm_enable
&& if_is_no_ptm_operative(ifp
))
553 * zebra_ptm_handle_msg_cb - The purpose of this callback function is to handle
554 * all the command responses and notifications received from PTM.
556 * Command responses: Upon establishing connection with PTM, Zebra requests
557 * status of all interfaces using 'get-status' command if global ptm-enable
558 * knob is enabled. As a response to the get-status command PTM sends status
559 * of all the interfaces as command responses. All other type of command
560 * responses with cmd_status key word are dropped. The sole purpose of
561 * registering this function as callback for the command responses is to
562 * handle the responses to get-status command.
564 * Notifications: Cable status and BFD session status changes are sent as
565 * notifications by PTM. So, this function is also the callback function for
566 * processing all the notifications from the PTM.
569 static int zebra_ptm_handle_msg_cb(void *arg
, void *in_ctxt
)
571 struct interface
*ifp
= NULL
;
574 char cmd_status_str
[32];
576 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_CMD_STATUS_STR
,
579 /* Drop command response messages */
580 if (cmd_status_str
[0] != '\0') {
584 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_PORT_STR
, port_str
);
586 if (port_str
[0] == '\0') {
587 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
592 if (strcmp(ZEBRA_PTM_INVALID_PORT_NAME
, port_str
)) {
596 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
597 ifp
= if_lookup_by_name_vrf(port_str
, vrf
);
600 if (!vrf_is_backend_netns())
606 flog_warn(EC_ZEBRA_UNKNOWN_INTERFACE
,
607 "%s: %s not found in interface list",
612 flog_warn(EC_ZEBRA_UNKNOWN_INTERFACE
,
613 "%s: multiple interface with name %s",
619 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_CBL_STR
, cbl_str
);
621 if (cbl_str
[0] == '\0') {
622 return zebra_ptm_handle_bfd_msg(arg
, in_ctxt
, ifp
);
625 return zebra_ptm_handle_cbl_msg(arg
, in_ctxt
, ifp
,
633 void zebra_ptm_sock_read(struct event
*thread
)
639 sock
= EVENT_FD(thread
);
644 /* PTM communicates in CSV format */
646 rc
= ptm_lib_process_msg(ptm_hdl
, sock
, ptm_cb
.in_data
,
647 ZEBRA_PTM_MAX_SOCKBUF
, NULL
);
650 if (((rc
== 0) && !errno
)
651 || (errno
&& (errno
!= EWOULDBLOCK
) && (errno
!= EAGAIN
))) {
652 flog_err_sys(EC_LIB_SOCKET
,
653 "%s routing socket error: %s(%d) bytes %d",
654 __func__
, safe_strerror(errno
), errno
, rc
);
656 close(ptm_cb
.ptm_sock
);
657 ptm_cb
.ptm_sock
= -1;
658 zebra_ptm_reset_status(0);
659 ptm_cb
.t_timer
= NULL
;
660 event_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
661 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
665 ptm_cb
.t_read
= NULL
;
666 event_add_read(zrouter
.master
, zebra_ptm_sock_read
, NULL
,
667 ptm_cb
.ptm_sock
, &ptm_cb
.t_read
);
670 /* BFD peer/dst register/update */
671 void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS
)
677 uint8_t multi_hop_cnt
;
679 unsigned int min_rx_timer
;
680 unsigned int min_tx_timer
;
681 char if_name
[INTERFACE_NAMSIZ
];
684 char buf
[INET6_ADDRSTRLEN
];
686 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
690 if (hdr
->command
== ZEBRA_BFD_DEST_UPDATE
)
691 client
->bfd_peer_upd8_cnt
++;
693 client
->bfd_peer_add_cnt
++;
695 if (IS_ZEBRA_DEBUG_EVENT
)
696 zlog_debug("bfd_dst_register msg from client %s: length=%d",
697 zebra_route_string(client
->proto
), hdr
->length
);
699 if (ptm_cb
.ptm_sock
== -1) {
700 ptm_cb
.t_timer
= NULL
;
701 event_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
702 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
706 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
707 snprintf(tmp_buf
, sizeof(tmp_buf
), "%s", ZEBRA_PTM_BFD_START_CMD
);
708 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
709 snprintf(tmp_buf
, sizeof(tmp_buf
), "%s",
710 zebra_route_string(client
->proto
));
711 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
717 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", pid
);
718 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
721 STREAM_GETW(s
, dst_p
.family
);
723 if (dst_p
.family
== AF_INET
)
724 dst_p
.prefixlen
= IPV4_MAX_BYTELEN
;
726 dst_p
.prefixlen
= IPV6_MAX_BYTELEN
;
728 STREAM_GET(&dst_p
.u
.prefix
, s
, dst_p
.prefixlen
);
729 if (dst_p
.family
== AF_INET
) {
730 inet_ntop(AF_INET
, &dst_p
.u
.prefix4
, buf
, sizeof(buf
));
731 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
732 ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
734 inet_ntop(AF_INET6
, &dst_p
.u
.prefix6
, buf
, sizeof(buf
));
735 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
736 ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
739 STREAM_GETL(s
, min_rx_timer
);
740 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", min_rx_timer
);
741 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_MIN_RX_FIELD
,
743 STREAM_GETL(s
, min_tx_timer
);
744 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", min_tx_timer
);
745 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_MIN_TX_FIELD
,
747 STREAM_GETC(s
, detect_mul
);
748 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", detect_mul
);
749 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_DETECT_MULT_FIELD
,
752 STREAM_GETC(s
, multi_hop
);
754 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", 1);
755 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
756 ZEBRA_PTM_BFD_MULTI_HOP_FIELD
, tmp_buf
);
757 STREAM_GETW(s
, src_p
.family
);
759 if (src_p
.family
== AF_INET
)
760 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
762 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
764 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
765 if (src_p
.family
== AF_INET
) {
766 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
, sizeof(buf
));
767 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
768 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
770 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
, sizeof(buf
));
771 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
772 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
775 STREAM_GETC(s
, multi_hop_cnt
);
776 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", multi_hop_cnt
);
777 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
778 ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD
, tmp_buf
);
780 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
781 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
782 ZEBRA_PTM_BFD_VRF_NAME_FIELD
,
785 if (dst_p
.family
== AF_INET6
) {
786 STREAM_GETW(s
, src_p
.family
);
788 if (src_p
.family
== AF_INET
)
789 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
791 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
793 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
794 if (src_p
.family
== AF_INET
) {
795 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
,
797 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
798 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
801 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
,
803 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
804 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
809 STREAM_GET(if_name
, s
, len
);
812 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
813 ZEBRA_PTM_BFD_IFNAME_FIELD
, if_name
);
815 STREAM_GETC(s
, cbit_set
);
816 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", cbit_set
);
817 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
818 ZEBRA_PTM_BFD_CBIT_FIELD
, tmp_buf
);
820 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", 1);
821 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEND_EVENT
,
824 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
826 if (IS_ZEBRA_DEBUG_SEND
)
827 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
829 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
834 ptm_lib_cleanup_msg(ptm_hdl
, out_ctxt
);
837 /* BFD peer/dst deregister */
838 void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS
)
844 char if_name
[INTERFACE_NAMSIZ
];
846 char buf
[INET6_ADDRSTRLEN
];
848 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
852 client
->bfd_peer_del_cnt
++;
854 if (IS_ZEBRA_DEBUG_EVENT
)
855 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
856 zebra_route_string(client
->proto
), hdr
->length
);
858 if (ptm_cb
.ptm_sock
== -1) {
859 ptm_cb
.t_timer
= NULL
;
860 event_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
861 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
865 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
867 snprintf(tmp_buf
, sizeof(tmp_buf
), "%s", ZEBRA_PTM_BFD_STOP_CMD
);
868 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
870 snprintf(tmp_buf
, sizeof(tmp_buf
), "%s",
871 zebra_route_string(client
->proto
));
872 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
878 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", pid
);
879 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
882 STREAM_GETW(s
, dst_p
.family
);
884 if (dst_p
.family
== AF_INET
)
885 dst_p
.prefixlen
= IPV4_MAX_BYTELEN
;
887 dst_p
.prefixlen
= IPV6_MAX_BYTELEN
;
889 STREAM_GET(&dst_p
.u
.prefix
, s
, dst_p
.prefixlen
);
890 if (dst_p
.family
== AF_INET
)
891 inet_ntop(AF_INET
, &dst_p
.u
.prefix4
, buf
, sizeof(buf
));
893 inet_ntop(AF_INET6
, &dst_p
.u
.prefix6
, buf
, sizeof(buf
));
894 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
897 STREAM_GETC(s
, multi_hop
);
899 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", 1);
900 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
901 ZEBRA_PTM_BFD_MULTI_HOP_FIELD
, tmp_buf
);
903 STREAM_GETW(s
, src_p
.family
);
905 if (src_p
.family
== AF_INET
)
906 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
908 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
910 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
911 if (src_p
.family
== AF_INET
)
912 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
, sizeof(buf
));
914 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
, sizeof(buf
));
915 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
916 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
918 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
919 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
920 ZEBRA_PTM_BFD_VRF_NAME_FIELD
,
923 if (dst_p
.family
== AF_INET6
) {
924 STREAM_GETW(s
, src_p
.family
);
926 if (src_p
.family
== AF_INET
)
927 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
929 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
931 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
932 if (src_p
.family
== AF_INET
) {
933 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
,
935 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
936 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
939 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
,
941 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
942 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
948 STREAM_GET(if_name
, s
, len
);
951 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
952 ZEBRA_PTM_BFD_IFNAME_FIELD
, if_name
);
955 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
956 if (IS_ZEBRA_DEBUG_SEND
)
957 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
960 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
965 ptm_lib_cleanup_msg(ptm_hdl
, out_ctxt
);
968 /* BFD client register */
969 void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS
)
973 void *out_ctxt
= NULL
;
975 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
977 client
->bfd_client_reg_cnt
++;
979 if (IS_ZEBRA_DEBUG_EVENT
)
980 zlog_debug("bfd_client_register msg from client %s: length=%d",
981 zebra_route_string(client
->proto
), hdr
->length
);
986 if (ptm_cb
.ptm_sock
== -1) {
987 ptm_cb
.t_timer
= NULL
;
988 event_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
989 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
993 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
995 snprintf(tmp_buf
, sizeof(tmp_buf
), "%s", ZEBRA_PTM_BFD_CLIENT_REG_CMD
);
996 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
998 snprintf(tmp_buf
, sizeof(tmp_buf
), "%s",
999 zebra_route_string(client
->proto
));
1000 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
1003 snprintf(tmp_buf
, sizeof(tmp_buf
), "%d", pid
);
1004 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
1007 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
1009 if (IS_ZEBRA_DEBUG_SEND
)
1010 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
1012 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
1014 SET_FLAG(ptm_cb
.client_flags
[client
->proto
],
1015 ZEBRA_PTM_BFD_CLIENT_FLAG_REG
);
1021 * IF we ever add more STREAM_GETXXX functions after the out_ctxt
1022 * is allocated then we need to add this code back in
1025 * ptm_lib_cleanup_msg(ptm_hdl, out_ctxt);
1030 /* BFD client deregister */
1031 int zebra_ptm_bfd_client_deregister(struct zserv
*client
)
1033 uint8_t proto
= client
->proto
;
1036 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
1038 if (!IS_BFD_ENABLED_PROTOCOL(proto
))
1041 if (IS_ZEBRA_DEBUG_EVENT
)
1042 zlog_debug("bfd_client_deregister msg for client %s",
1043 zebra_route_string(proto
));
1045 if (ptm_cb
.ptm_sock
== -1) {
1046 ptm_cb
.t_timer
= NULL
;
1047 event_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
1048 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
1052 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
1054 snprintf(tmp_buf
, sizeof(tmp_buf
), "%s",
1055 ZEBRA_PTM_BFD_CLIENT_DEREG_CMD
);
1056 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
1058 snprintf(tmp_buf
, sizeof(tmp_buf
), "%s", zebra_route_string(proto
));
1059 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
1062 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
1064 if (IS_ZEBRA_DEBUG_SEND
)
1065 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
1068 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
1069 UNSET_FLAG(ptm_cb
.client_flags
[proto
], ZEBRA_PTM_BFD_CLIENT_FLAG_REG
);
1074 int zebra_ptm_get_enable_state(void)
1076 return ptm_cb
.ptm_enable
;
1080 * zebra_ptm_get_status_str - Convert status to a display string.
1082 static const char *zebra_ptm_get_status_str(int status
)
1085 case ZEBRA_PTM_STATUS_DOWN
:
1087 case ZEBRA_PTM_STATUS_UP
:
1089 case ZEBRA_PTM_STATUS_UNKNOWN
:
1095 void zebra_ptm_show_status(struct vty
*vty
, json_object
*json
,
1096 struct interface
*ifp
)
1100 if (ifp
->ptm_enable
)
1101 status
= zebra_ptm_get_status_str(ifp
->ptm_status
);
1103 status
= "disabled";
1106 json_object_string_add(json
, "ptmStatus", status
);
1108 vty_out(vty
, " PTM status: %s\n", status
);
1111 void zebra_ptm_send_status_req(void)
1114 int len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
1116 if (ptm_cb
.ptm_enable
) {
1117 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
,
1119 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
,
1120 ZEBRA_PTM_GET_STATUS_CMD
);
1121 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &len
);
1123 zebra_ptm_send_message(ptm_cb
.out_data
, len
);
1127 void zebra_ptm_reset_status(int ptm_disable
)
1130 struct interface
*ifp
;
1133 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
1134 FOR_ALL_INTERFACES (vrf
, ifp
) {
1136 if (ifp
->ptm_enable
) {
1137 if (!if_is_operative(ifp
))
1142 ZEBRA_IF_PTM_ENABLE_OFF
;
1143 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1145 if (if_is_operative(ifp
) && send_linkup
) {
1146 if (IS_ZEBRA_DEBUG_EVENT
)
1148 "%s: Bringing up interface %s",
1149 __func__
, ifp
->name
);
1156 void zebra_ptm_if_init(struct zebra_if
*zebra_ifp
)
1158 zebra_ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_UNSPEC
;
1161 void zebra_ptm_if_set_ptm_state(struct interface
*ifp
,
1162 struct zebra_if
*zebra_ifp
)
1164 if (zebra_ifp
&& zebra_ifp
->ptm_enable
!= ZEBRA_IF_PTM_ENABLE_UNSPEC
)
1165 ifp
->ptm_enable
= zebra_ifp
->ptm_enable
;
1168 void zebra_ptm_if_write(struct vty
*vty
, struct zebra_if
*zebra_ifp
)
1170 if (zebra_ifp
->ptm_enable
== ZEBRA_IF_PTM_ENABLE_OFF
)
1171 vty_out(vty
, " no ptm-enable\n");
1174 #else /* HAVE_BFDD */
1179 struct ptm_process
{
1180 struct zserv
*pp_zs
;
1183 TAILQ_ENTRY(ptm_process
) pp_entry
;
1185 TAILQ_HEAD(ppqueue
, ptm_process
) ppqueue
;
1187 DEFINE_MTYPE_STATIC(ZEBRA
, ZEBRA_PTM_BFD_PROCESS
,
1188 "PTM BFD process reg table");
1193 static struct ptm_process
*pp_new(pid_t pid
, struct zserv
*zs
);
1194 static struct ptm_process
*pp_lookup_byzs(struct zserv
*zs
);
1195 static void pp_free(struct ptm_process
*pp
);
1196 static void pp_free_all(void);
1198 static void zebra_ptm_send_bfdd(struct stream
*msg
);
1199 static void zebra_ptm_send_clients(struct stream
*msg
);
1200 static int _zebra_ptm_bfd_client_deregister(struct zserv
*zs
);
1201 static void _zebra_ptm_reroute(struct zserv
*zs
, struct zebra_vrf
*zvrf
,
1202 struct stream
*msg
, uint32_t command
);
1206 * Process PID registration.
1208 static struct ptm_process
*pp_new(pid_t pid
, struct zserv
*zs
)
1210 struct ptm_process
*pp
;
1213 /* Sanity check: more than one client can't have the same PID. */
1214 TAILQ_FOREACH(pp
, &ppqueue
, pp_entry
) {
1215 if (pp
->pp_pid
== pid
&& pp
->pp_zs
!= zs
)
1216 zlog_err("%s:%d pid and client pointer doesn't match",
1217 __FILE__
, __LINE__
);
1219 #endif /* PTM_DEBUG */
1221 /* Lookup for duplicates. */
1222 pp
= pp_lookup_byzs(zs
);
1226 /* Allocate and register new process. */
1227 pp
= XCALLOC(MTYPE_ZEBRA_PTM_BFD_PROCESS
, sizeof(*pp
));
1231 TAILQ_INSERT_HEAD(&ppqueue
, pp
, pp_entry
);
1236 static struct ptm_process
*pp_lookup_byzs(struct zserv
*zs
)
1238 struct ptm_process
*pp
;
1240 TAILQ_FOREACH(pp
, &ppqueue
, pp_entry
) {
1241 if (pp
->pp_zs
!= zs
)
1250 static void pp_free(struct ptm_process
*pp
)
1255 TAILQ_REMOVE(&ppqueue
, pp
, pp_entry
);
1256 XFREE(MTYPE_ZEBRA_PTM_BFD_PROCESS
, pp
);
1259 static void pp_free_all(void)
1261 struct ptm_process
*pp
;
1263 while (!TAILQ_EMPTY(&ppqueue
)) {
1264 pp
= TAILQ_FIRST(&ppqueue
);
1271 * Use the FRR's internal daemon implementation.
1273 static void zebra_ptm_send_bfdd(struct stream
*msg
)
1275 struct listnode
*node
;
1276 struct zserv
*client
;
1277 struct stream
*msgc
;
1279 /* Create copy for replication. */
1280 msgc
= stream_dup(msg
);
1282 /* Send message to all running BFDd daemons. */
1283 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
1284 if (client
->proto
!= ZEBRA_ROUTE_BFD
)
1287 zserv_send_message(client
, msg
);
1289 /* Allocate more messages. */
1290 msg
= stream_dup(msgc
);
1297 static void zebra_ptm_send_clients(struct stream
*msg
)
1299 struct listnode
*node
;
1300 struct zserv
*client
;
1301 struct stream
*msgc
;
1303 /* Create copy for replication. */
1304 msgc
= stream_dup(msg
);
1306 /* Send message to all running client daemons. */
1307 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
1308 if (!IS_BFD_ENABLED_PROTOCOL(client
->proto
))
1311 zserv_send_message(client
, msg
);
1313 /* Allocate more messages. */
1314 msg
= stream_dup(msgc
);
1321 static int _zebra_ptm_bfd_client_deregister(struct zserv
*zs
)
1324 struct ptm_process
*pp
;
1326 if (!IS_BFD_ENABLED_PROTOCOL(zs
->proto
))
1329 /* Find daemon pid by zebra connection pointer. */
1330 pp
= pp_lookup_byzs(zs
);
1332 zlog_err("%s:%d failed to find process pid registration",
1333 __FILE__
, __LINE__
);
1337 /* Generate, send message and free() daemon related data. */
1338 msg
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1340 zlog_debug("%s: not enough memory", __func__
);
1345 * The message type will be ZEBRA_BFD_DEST_REPLAY so we can use only
1346 * one callback at the `bfdd` side, however the real command
1347 * number will be included right after the zebra header.
1349 zclient_create_header(msg
, ZEBRA_BFD_DEST_REPLAY
, 0);
1350 stream_putl(msg
, ZEBRA_BFD_CLIENT_DEREGISTER
);
1352 /* Put process PID. */
1353 stream_putl(msg
, pp
->pp_pid
);
1355 /* Update the data pointers. */
1356 stream_putw_at(msg
, 0, stream_get_endp(msg
));
1358 zebra_ptm_send_bfdd(msg
);
1365 void zebra_ptm_init(void)
1367 /* Initialize the ptm process information list. */
1368 TAILQ_INIT(&ppqueue
);
1371 * Send deregistration messages to BFD daemon when some other
1372 * daemon closes. This will help avoid sending daemons
1373 * unnecessary notification messages.
1375 hook_register(zserv_client_close
, _zebra_ptm_bfd_client_deregister
);
1378 void zebra_ptm_finish(void)
1380 /* Remove the client disconnect hook and free all memory. */
1381 hook_unregister(zserv_client_close
, _zebra_ptm_bfd_client_deregister
);
1389 static void _zebra_ptm_reroute(struct zserv
*zs
, struct zebra_vrf
*zvrf
,
1390 struct stream
*msg
, uint32_t command
)
1392 struct stream
*msgc
;
1393 char buf
[ZEBRA_MAX_PACKET_SIZ
];
1396 /* Create BFD header */
1397 msgc
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1398 zclient_create_header(msgc
, ZEBRA_BFD_DEST_REPLAY
, zvrf
->vrf
->vrf_id
);
1399 stream_putl(msgc
, command
);
1401 if (STREAM_READABLE(msg
) > STREAM_WRITEABLE(msgc
)) {
1402 zlog_warn("Cannot fit extended BFD header plus original message contents into ZAPI packet; dropping message");
1403 goto stream_failure
;
1406 /* Copy original message, excluding header, into new message */
1407 stream_get_from(buf
, msg
, stream_get_getp(msg
), STREAM_READABLE(msg
));
1408 stream_put(msgc
, buf
, STREAM_READABLE(msg
));
1410 /* Update length field */
1411 stream_putw_at(msgc
, 0, STREAM_READABLE(msgc
));
1413 zebra_ptm_send_bfdd(msgc
);
1416 /* Registrate process PID for shutdown hook. */
1417 STREAM_GETL(msg
, ppid
);
1425 zlog_err("%s:%d failed to registrate client pid", __FILE__
, __LINE__
);
1428 void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS
)
1430 if (IS_ZEBRA_DEBUG_EVENT
)
1431 zlog_debug("bfd_dst_register msg from client %s: length=%d",
1432 zebra_route_string(client
->proto
), hdr
->length
);
1434 _zebra_ptm_reroute(client
, zvrf
, msg
, ZEBRA_BFD_DEST_REGISTER
);
1437 void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS
)
1439 if (IS_ZEBRA_DEBUG_EVENT
)
1440 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
1441 zebra_route_string(client
->proto
), hdr
->length
);
1443 _zebra_ptm_reroute(client
, zvrf
, msg
, ZEBRA_BFD_DEST_DEREGISTER
);
1446 void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS
)
1448 if (IS_ZEBRA_DEBUG_EVENT
)
1449 zlog_debug("bfd_client_register msg from client %s: length=%d",
1450 zebra_route_string(client
->proto
), hdr
->length
);
1452 _zebra_ptm_reroute(client
, zvrf
, msg
, ZEBRA_BFD_CLIENT_REGISTER
);
1455 void zebra_ptm_bfd_dst_replay(ZAPI_HANDLER_ARGS
)
1457 struct stream
*msgc
;
1458 size_t zmsglen
, zhdrlen
;
1463 * Replay messages with HAVE_BFDD are meant to be replayed to
1464 * the client daemons. These messages are composed and
1465 * originated from the `bfdd` daemon.
1467 if (IS_ZEBRA_DEBUG_EVENT
)
1468 zlog_debug("bfd_dst_update msg from client %s: length=%d",
1469 zebra_route_string(client
->proto
), hdr
->length
);
1472 * Client messages must be re-routed, otherwise do the `bfdd`
1473 * special treatment.
1475 if (client
->proto
!= ZEBRA_ROUTE_BFD
) {
1476 _zebra_ptm_reroute(client
, zvrf
, msg
, ZEBRA_BFD_DEST_REPLAY
);
1480 /* Figure out if this is an DEST_UPDATE or DEST_REPLAY. */
1481 if (stream_getl2(msg
, &cmd
) == false) {
1482 zlog_err("%s: expected at least 4 bytes (command)", __func__
);
1487 * Don't modify message in the zebra API. In order to do that we
1488 * need to allocate a new message stream and copy the message
1489 * provided by zebra.
1491 msgc
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1493 zlog_debug("%s: not enough memory", __func__
);
1497 /* Calculate our header size plus the message contents. */
1498 if (cmd
!= ZEBRA_BFD_DEST_REPLAY
) {
1499 zhdrlen
= ZEBRA_HEADER_SIZE
;
1500 zmsglen
= msg
->endp
- msg
->getp
;
1501 memcpy(msgc
->data
+ zhdrlen
, msg
->data
+ msg
->getp
, zmsglen
);
1503 zclient_create_header(msgc
, cmd
, zvrf_id(zvrf
));
1506 msgc
->endp
= zhdrlen
+ zmsglen
;
1508 zclient_create_header(msgc
, cmd
, zvrf_id(zvrf
));
1510 /* Update the data pointers. */
1511 stream_putw_at(msgc
, 0, stream_get_endp(msgc
));
1513 zebra_ptm_send_clients(msgc
);
1519 void zebra_ptm_if_init(struct zebra_if
*zifp
__attribute__((__unused__
)))
1524 int zebra_ptm_get_enable_state(void)
1529 void zebra_ptm_show_status(struct vty
*vty
__attribute__((__unused__
)),
1530 json_object
*json
__attribute__((__unused__
)),
1531 struct interface
*ifp
__attribute__((__unused__
)))
1536 void zebra_ptm_write(struct vty
*vty
__attribute__((__unused__
)))
1541 void zebra_ptm_if_write(struct vty
*vty
__attribute__((__unused__
)),
1542 struct zebra_if
*zifp
__attribute__((__unused__
)))
1546 void zebra_ptm_if_set_ptm_state(struct interface
*i
__attribute__((__unused__
)),
1547 struct zebra_if
*zi
__attribute__((__unused__
)))
1552 #endif /* HAVE_BFDD */