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 */
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 int zebra_ptm_sock_read(struct 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(struct zebra_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 sprintf(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
);
159 /* Release threads. */
161 thread_cancel(ptm_cb
.t_read
);
163 thread_cancel(ptm_cb
.t_write
);
165 thread_cancel(ptm_cb
.t_timer
);
168 buffer_free(ptm_cb
.wb
);
170 if (ptm_cb
.ptm_sock
>= 0)
171 close(ptm_cb
.ptm_sock
);
174 static int zebra_ptm_flush_messages(struct thread
*thread
)
176 ptm_cb
.t_write
= NULL
;
178 if (ptm_cb
.ptm_sock
== -1)
183 switch (buffer_flush_available(ptm_cb
.wb
, ptm_cb
.ptm_sock
)) {
185 flog_err_sys(EC_LIB_SOCKET
, "%s ptm socket error: %s", __func__
,
186 safe_strerror(errno
));
187 close(ptm_cb
.ptm_sock
);
188 ptm_cb
.ptm_sock
= -1;
189 zebra_ptm_reset_status(0);
190 ptm_cb
.t_timer
= NULL
;
191 thread_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
192 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
195 ptm_cb
.t_write
= NULL
;
196 thread_add_write(zrouter
.master
, zebra_ptm_flush_messages
, NULL
,
197 ptm_cb
.ptm_sock
, &ptm_cb
.t_write
);
206 static int zebra_ptm_send_message(char *data
, int size
)
209 switch (buffer_write(ptm_cb
.wb
, ptm_cb
.ptm_sock
, data
, size
)) {
211 flog_err_sys(EC_LIB_SOCKET
, "%s ptm socket error: %s", __func__
,
212 safe_strerror(errno
));
213 close(ptm_cb
.ptm_sock
);
214 ptm_cb
.ptm_sock
= -1;
215 zebra_ptm_reset_status(0);
216 ptm_cb
.t_timer
= NULL
;
217 thread_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
218 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
221 THREAD_OFF(ptm_cb
.t_write
);
224 thread_add_write(zrouter
.master
, zebra_ptm_flush_messages
, NULL
,
225 ptm_cb
.ptm_sock
, &ptm_cb
.t_write
);
232 int zebra_ptm_connect(struct thread
*t
)
236 if (ptm_cb
.ptm_sock
== -1) {
237 zebra_ptm_socket_init();
241 if (ptm_cb
.ptm_sock
!= -1) {
243 ptm_cb
.t_read
= NULL
;
244 thread_add_read(zrouter
.master
, zebra_ptm_sock_read
,
245 NULL
, ptm_cb
.ptm_sock
, &ptm_cb
.t_read
);
246 zebra_bfd_peer_replay_req();
248 zebra_ptm_send_status_req();
249 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
250 } else if (ptm_cb
.reconnect_time
< ZEBRA_PTM_RECONNECT_TIME_MAX
) {
251 ptm_cb
.reconnect_time
*= 2;
252 if (ptm_cb
.reconnect_time
> ZEBRA_PTM_RECONNECT_TIME_MAX
)
253 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_MAX
;
255 ptm_cb
.t_timer
= NULL
;
256 thread_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
257 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
258 } else if (ptm_cb
.reconnect_time
>= ZEBRA_PTM_RECONNECT_TIME_MAX
) {
259 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
265 DEFUN (zebra_ptm_enable
,
266 zebra_ptm_enable_cmd
,
268 "Enable neighbor check with specified topology\n")
271 struct interface
*ifp
;
272 struct zebra_if
*if_data
;
274 ptm_cb
.ptm_enable
= ZEBRA_IF_PTM_ENABLE_ON
;
276 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
277 FOR_ALL_INTERFACES (vrf
, ifp
)
278 if (!ifp
->ptm_enable
) {
279 if_data
= (struct zebra_if
*)ifp
->info
;
281 && (if_data
->ptm_enable
282 == ZEBRA_IF_PTM_ENABLE_UNSPEC
)) {
284 ZEBRA_IF_PTM_ENABLE_ON
;
286 /* Assign a default unknown status */
287 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
290 zebra_ptm_connect(NULL
);
295 DEFUN (no_zebra_ptm_enable
,
296 no_zebra_ptm_enable_cmd
,
299 "Enable neighbor check with specified topology\n")
301 ptm_cb
.ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
302 zebra_ptm_reset_status(1);
306 DEFUN (zebra_ptm_enable_if
,
307 zebra_ptm_enable_if_cmd
,
309 "Enable neighbor check with specified topology\n")
311 VTY_DECLVAR_CONTEXT(interface
, ifp
);
312 struct zebra_if
*if_data
;
314 int send_linkdown
= 0;
317 if_data
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_UNSPEC
;
319 if (ifp
->ifindex
== IFINDEX_INTERNAL
) {
323 old_ptm_enable
= ifp
->ptm_enable
;
324 ifp
->ptm_enable
= ptm_cb
.ptm_enable
;
326 if (if_is_no_ptm_operative(ifp
))
329 if (!old_ptm_enable
&& ptm_cb
.ptm_enable
) {
330 if (!if_is_operative(ifp
) && send_linkdown
) {
331 if (IS_ZEBRA_DEBUG_EVENT
)
332 zlog_debug("%s: Bringing down interface %s",
333 __func__
, ifp
->name
);
341 DEFUN (no_zebra_ptm_enable_if
,
342 no_zebra_ptm_enable_if_cmd
,
345 "Enable neighbor check with specified topology\n")
347 VTY_DECLVAR_CONTEXT(interface
, ifp
);
349 struct zebra_if
*if_data
;
351 if ((ifp
->ifindex
!= IFINDEX_INTERNAL
) && (ifp
->ptm_enable
)) {
352 if (!if_is_operative(ifp
))
355 ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
356 if (if_is_no_ptm_operative(ifp
) && send_linkup
) {
357 if (IS_ZEBRA_DEBUG_EVENT
)
358 zlog_debug("%s: Bringing up interface %s",
359 __func__
, ifp
->name
);
365 if_data
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
371 void zebra_ptm_write(struct vty
*vty
)
373 if (ptm_cb
.ptm_enable
)
374 vty_out(vty
, "ptm-enable\n");
379 static int zebra_ptm_socket_init(void)
383 struct sockaddr_un addr
;
385 ptm_cb
.ptm_sock
= -1;
387 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
390 if (set_nonblocking(sock
) < 0) {
391 if (IS_ZEBRA_DEBUG_EVENT
)
392 zlog_debug("%s: Unable to set socket non blocking[%s]",
393 __PRETTY_FUNCTION__
, safe_strerror(errno
));
398 /* Make server socket. */
399 memset(&addr
, 0, sizeof(struct sockaddr_un
));
400 addr
.sun_family
= AF_UNIX
;
401 memcpy(&addr
.sun_path
, ZEBRA_PTM_SOCK_NAME
,
402 sizeof(ZEBRA_PTM_SOCK_NAME
));
404 ret
= connect(sock
, (struct sockaddr
*)&addr
,
405 sizeof(addr
.sun_family
) + sizeof(ZEBRA_PTM_SOCK_NAME
)
408 if (IS_ZEBRA_DEBUG_EVENT
)
409 zlog_debug("%s: Unable to connect to socket %s [%s]",
410 __func__
, ZEBRA_PTM_SOCK_NAME
,
411 safe_strerror(errno
));
415 ptm_cb
.ptm_sock
= sock
;
419 static void zebra_ptm_install_commands(void)
421 install_element(CONFIG_NODE
, &zebra_ptm_enable_cmd
);
422 install_element(CONFIG_NODE
, &no_zebra_ptm_enable_cmd
);
423 install_element(INTERFACE_NODE
, &zebra_ptm_enable_if_cmd
);
424 install_element(INTERFACE_NODE
, &no_zebra_ptm_enable_if_cmd
);
427 /* BFD session goes down, send message to the protocols. */
428 static void if_bfd_session_update(struct interface
*ifp
, struct prefix
*dp
,
429 struct prefix
*sp
, int status
,
432 if (IS_ZEBRA_DEBUG_EVENT
) {
433 char buf
[2][INET6_ADDRSTRLEN
];
437 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d on %s"
439 inet_ntop(dp
->family
, &dp
->u
.prefix
, buf
[0],
441 dp
->prefixlen
, ifp
->name
,
442 bfd_get_status_str(status
));
445 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d "
446 "with src %s/%d and vrf %u %s event",
447 inet_ntop(dp
->family
, &dp
->u
.prefix
, buf
[0],
450 inet_ntop(sp
->family
, &sp
->u
.prefix
, buf
[1],
452 sp
->prefixlen
, vrf_id
,
453 bfd_get_status_str(status
));
457 zebra_interface_bfd_update(ifp
, dp
, sp
, status
, vrf_id
);
460 static int zebra_ptm_handle_bfd_msg(void *arg
, void *in_ctxt
,
461 struct interface
*ifp
)
467 struct prefix dest_prefix
;
468 struct prefix src_prefix
;
471 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDSTATUS_STR
, bfdst_str
);
473 if (bfdst_str
[0] == '\0') {
477 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDDEST_STR
, dest_str
);
479 if (dest_str
[0] == '\0') {
480 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
481 ZEBRA_PTM_BFDDEST_STR
);
485 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDSRC_STR
, src_str
);
487 if (src_str
[0] == '\0') {
488 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
489 ZEBRA_PTM_BFDSRC_STR
);
493 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDVRF_STR
, vrf_str
);
495 if (vrf_str
[0] == '\0') {
496 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
497 ZEBRA_PTM_BFDVRF_STR
);
501 if (IS_ZEBRA_DEBUG_EVENT
)
503 "%s: Recv Port [%s] bfd status [%s] vrf [%s]"
504 " peer [%s] local [%s]",
505 __func__
, ifp
? ifp
->name
: "N/A", bfdst_str
, vrf_str
,
508 if (str2prefix(dest_str
, &dest_prefix
) == 0) {
509 flog_err(EC_ZEBRA_PREFIX_PARSE_ERROR
,
510 "%s: Peer addr %s not found", __func__
, dest_str
);
514 memset(&src_prefix
, 0, sizeof(struct prefix
));
515 if (strcmp(ZEBRA_PTM_INVALID_SRC_IP
, src_str
)) {
516 if (str2prefix(src_str
, &src_prefix
) == 0) {
517 flog_err(EC_ZEBRA_PREFIX_PARSE_ERROR
,
518 "%s: Local addr %s not found", __func__
,
524 if (!strcmp(ZEBRA_PTM_INVALID_VRF
, vrf_str
) && ifp
) {
525 vrf_id
= ifp
->vrf_id
;
527 vrf_id
= vrf_name_to_id(vrf_str
);
530 if (!strcmp(bfdst_str
, ZEBRA_PTM_BFDSTATUS_DOWN_STR
)) {
531 if_bfd_session_update(ifp
, &dest_prefix
, &src_prefix
,
532 BFD_STATUS_DOWN
, vrf_id
);
534 if_bfd_session_update(ifp
, &dest_prefix
, &src_prefix
,
535 BFD_STATUS_UP
, vrf_id
);
541 static int zebra_ptm_handle_cbl_msg(void *arg
, void *in_ctxt
,
542 struct interface
*ifp
, char *cbl_str
)
546 if (IS_ZEBRA_DEBUG_EVENT
)
547 zlog_debug("%s: Recv Port [%s] cbl status [%s]", __func__
,
550 if (!strcmp(cbl_str
, ZEBRA_PTM_PASS_STR
)
551 && (ifp
->ptm_status
!= ZEBRA_PTM_STATUS_UP
)) {
553 if (ifp
->ptm_status
== ZEBRA_PTM_STATUS_DOWN
)
555 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UP
;
556 if (ifp
->ptm_enable
&& if_is_no_ptm_operative(ifp
)
559 } else if (!strcmp(cbl_str
, ZEBRA_PTM_FAIL_STR
)
560 && (ifp
->ptm_status
!= ZEBRA_PTM_STATUS_DOWN
)) {
561 ifp
->ptm_status
= ZEBRA_PTM_STATUS_DOWN
;
562 if (ifp
->ptm_enable
&& if_is_no_ptm_operative(ifp
))
570 * zebra_ptm_handle_msg_cb - The purpose of this callback function is to handle
571 * all the command responses and notifications received from PTM.
573 * Command responses: Upon establishing connection with PTM, Zebra requests
574 * status of all interfaces using 'get-status' command if global ptm-enable
575 * knob is enabled. As a response to the get-status command PTM sends status
576 * of all the interfaces as command responses. All other type of command
577 * responses with cmd_status key word are dropped. The sole purpose of
578 * registering this function as callback for the command responses is to
579 * handle the responses to get-status command.
581 * Notifications: Cable status and BFD session status changes are sent as
582 * notifications by PTM. So, this function is also the callback function for
583 * processing all the notifications from the PTM.
586 static int zebra_ptm_handle_msg_cb(void *arg
, void *in_ctxt
)
588 struct interface
*ifp
= NULL
;
591 char cmd_status_str
[32];
593 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_CMD_STATUS_STR
,
596 /* Drop command response messages */
597 if (cmd_status_str
[0] != '\0') {
601 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_PORT_STR
, port_str
);
603 if (port_str
[0] == '\0') {
604 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
609 if (strcmp(ZEBRA_PTM_INVALID_PORT_NAME
, port_str
)) {
610 ifp
= if_lookup_by_name_all_vrf(port_str
);
613 flog_warn(EC_ZEBRA_UNKNOWN_INTERFACE
,
614 "%s: %s not found in interface list",
620 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_CBL_STR
, cbl_str
);
622 if (cbl_str
[0] == '\0') {
623 return zebra_ptm_handle_bfd_msg(arg
, in_ctxt
, ifp
);
626 return zebra_ptm_handle_cbl_msg(arg
, in_ctxt
, ifp
,
634 int zebra_ptm_sock_read(struct thread
*thread
)
640 sock
= THREAD_FD(thread
);
645 /* PTM communicates in CSV format */
647 rc
= ptm_lib_process_msg(ptm_hdl
, sock
, ptm_cb
.in_data
,
648 ZEBRA_PTM_MAX_SOCKBUF
, NULL
);
651 if (((rc
== 0) && !errno
)
652 || (errno
&& (errno
!= EWOULDBLOCK
) && (errno
!= EAGAIN
))) {
653 flog_err_sys(EC_LIB_SOCKET
,
654 "%s routing socket error: %s(%d) bytes %d",
655 __func__
, safe_strerror(errno
), errno
, rc
);
657 close(ptm_cb
.ptm_sock
);
658 ptm_cb
.ptm_sock
= -1;
659 zebra_ptm_reset_status(0);
660 ptm_cb
.t_timer
= NULL
;
661 thread_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
662 ptm_cb
.reconnect_time
,
667 ptm_cb
.t_read
= NULL
;
668 thread_add_read(zrouter
.master
, zebra_ptm_sock_read
, NULL
,
669 ptm_cb
.ptm_sock
, &ptm_cb
.t_read
);
674 /* BFD peer/dst register/update */
675 void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS
)
681 uint8_t multi_hop_cnt
;
683 unsigned int min_rx_timer
;
684 unsigned int min_tx_timer
;
685 char if_name
[INTERFACE_NAMSIZ
];
688 char buf
[INET6_ADDRSTRLEN
];
690 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
694 if (hdr
->command
== ZEBRA_BFD_DEST_UPDATE
)
695 client
->bfd_peer_upd8_cnt
++;
697 client
->bfd_peer_add_cnt
++;
699 if (IS_ZEBRA_DEBUG_EVENT
)
700 zlog_debug("bfd_dst_register msg from client %s: length=%d",
701 zebra_route_string(client
->proto
), hdr
->length
);
703 if (ptm_cb
.ptm_sock
== -1) {
704 ptm_cb
.t_timer
= NULL
;
705 thread_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
706 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
710 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
711 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_START_CMD
);
712 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
713 sprintf(tmp_buf
, "%s", zebra_route_string(client
->proto
));
714 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
720 sprintf(tmp_buf
, "%d", pid
);
721 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
724 STREAM_GETW(s
, dst_p
.family
);
726 if (dst_p
.family
== AF_INET
)
727 dst_p
.prefixlen
= IPV4_MAX_BYTELEN
;
729 dst_p
.prefixlen
= IPV6_MAX_BYTELEN
;
731 STREAM_GET(&dst_p
.u
.prefix
, s
, dst_p
.prefixlen
);
732 if (dst_p
.family
== AF_INET
) {
733 inet_ntop(AF_INET
, &dst_p
.u
.prefix4
, buf
, sizeof(buf
));
734 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
735 ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
737 inet_ntop(AF_INET6
, &dst_p
.u
.prefix6
, buf
, sizeof(buf
));
738 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
739 ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
742 STREAM_GETL(s
, min_rx_timer
);
743 sprintf(tmp_buf
, "%d", min_rx_timer
);
744 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_MIN_RX_FIELD
,
746 STREAM_GETL(s
, min_tx_timer
);
747 sprintf(tmp_buf
, "%d", min_tx_timer
);
748 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_MIN_TX_FIELD
,
750 STREAM_GETC(s
, detect_mul
);
751 sprintf(tmp_buf
, "%d", detect_mul
);
752 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_DETECT_MULT_FIELD
,
755 STREAM_GETC(s
, multi_hop
);
757 sprintf(tmp_buf
, "%d", 1);
758 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
759 ZEBRA_PTM_BFD_MULTI_HOP_FIELD
, tmp_buf
);
760 STREAM_GETW(s
, src_p
.family
);
762 if (src_p
.family
== AF_INET
)
763 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
765 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
767 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
768 if (src_p
.family
== AF_INET
) {
769 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
, sizeof(buf
));
770 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
771 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
773 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
, sizeof(buf
));
774 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
775 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
778 STREAM_GETC(s
, multi_hop_cnt
);
779 sprintf(tmp_buf
, "%d", multi_hop_cnt
);
780 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
781 ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD
, tmp_buf
);
783 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
784 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
785 ZEBRA_PTM_BFD_VRF_NAME_FIELD
,
788 if (dst_p
.family
== AF_INET6
) {
789 STREAM_GETW(s
, src_p
.family
);
791 if (src_p
.family
== AF_INET
)
792 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
794 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
796 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
797 if (src_p
.family
== AF_INET
) {
798 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
,
800 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
801 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
804 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
,
806 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
807 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
812 STREAM_GET(if_name
, s
, len
);
815 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
816 ZEBRA_PTM_BFD_IFNAME_FIELD
, if_name
);
818 STREAM_GETC(s
, cbit_set
);
819 sprintf(tmp_buf
, "%d", cbit_set
);
820 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
821 ZEBRA_PTM_BFD_CBIT_FIELD
, tmp_buf
);
823 sprintf(tmp_buf
, "%d", 1);
824 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEND_EVENT
,
827 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
829 if (IS_ZEBRA_DEBUG_SEND
)
830 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
832 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
837 ptm_lib_cleanup_msg(ptm_hdl
, out_ctxt
);
840 /* BFD peer/dst deregister */
841 void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS
)
847 char if_name
[INTERFACE_NAMSIZ
];
849 char buf
[INET6_ADDRSTRLEN
];
851 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
855 client
->bfd_peer_del_cnt
++;
857 if (IS_ZEBRA_DEBUG_EVENT
)
858 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
859 zebra_route_string(client
->proto
), hdr
->length
);
861 if (ptm_cb
.ptm_sock
== -1) {
862 ptm_cb
.t_timer
= NULL
;
863 thread_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
864 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
868 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
870 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_STOP_CMD
);
871 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
873 sprintf(tmp_buf
, "%s", zebra_route_string(client
->proto
));
874 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
880 sprintf(tmp_buf
, "%d", pid
);
881 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
884 STREAM_GETW(s
, dst_p
.family
);
886 if (dst_p
.family
== AF_INET
)
887 dst_p
.prefixlen
= IPV4_MAX_BYTELEN
;
889 dst_p
.prefixlen
= IPV6_MAX_BYTELEN
;
891 STREAM_GET(&dst_p
.u
.prefix
, s
, dst_p
.prefixlen
);
892 if (dst_p
.family
== AF_INET
)
893 inet_ntop(AF_INET
, &dst_p
.u
.prefix4
, buf
, sizeof(buf
));
895 inet_ntop(AF_INET6
, &dst_p
.u
.prefix6
, buf
, sizeof(buf
));
896 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
899 STREAM_GETC(s
, multi_hop
);
901 sprintf(tmp_buf
, "%d", 1);
902 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
903 ZEBRA_PTM_BFD_MULTI_HOP_FIELD
, tmp_buf
);
905 STREAM_GETW(s
, src_p
.family
);
907 if (src_p
.family
== AF_INET
)
908 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
910 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
912 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
913 if (src_p
.family
== AF_INET
)
914 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
, sizeof(buf
));
916 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
, sizeof(buf
));
917 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
918 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
920 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
921 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
922 ZEBRA_PTM_BFD_VRF_NAME_FIELD
,
925 if (dst_p
.family
== AF_INET6
) {
926 STREAM_GETW(s
, src_p
.family
);
928 if (src_p
.family
== AF_INET
)
929 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
931 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
933 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
934 if (src_p
.family
== AF_INET
) {
935 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
,
937 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
938 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
941 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
,
943 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
944 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
950 STREAM_GET(if_name
, s
, len
);
953 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
954 ZEBRA_PTM_BFD_IFNAME_FIELD
, if_name
);
957 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
958 if (IS_ZEBRA_DEBUG_SEND
)
959 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
962 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
967 ptm_lib_cleanup_msg(ptm_hdl
, out_ctxt
);
970 /* BFD client register */
971 void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS
)
975 void *out_ctxt
= NULL
;
977 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
979 client
->bfd_client_reg_cnt
++;
981 if (IS_ZEBRA_DEBUG_EVENT
)
982 zlog_debug("bfd_client_register msg from client %s: length=%d",
983 zebra_route_string(client
->proto
), hdr
->length
);
988 if (ptm_cb
.ptm_sock
== -1) {
989 ptm_cb
.t_timer
= NULL
;
990 thread_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
991 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
995 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
997 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_CLIENT_REG_CMD
);
998 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
1000 sprintf(tmp_buf
, "%s", zebra_route_string(client
->proto
));
1001 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
1004 sprintf(tmp_buf
, "%d", pid
);
1005 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
1008 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
1010 if (IS_ZEBRA_DEBUG_SEND
)
1011 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
1013 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
1015 SET_FLAG(ptm_cb
.client_flags
[client
->proto
],
1016 ZEBRA_PTM_BFD_CLIENT_FLAG_REG
);
1022 * IF we ever add more STREAM_GETXXX functions after the out_ctxt
1023 * is allocated then we need to add this code back in
1026 * ptm_lib_cleanup_msg(ptm_hdl, out_ctxt);
1031 /* BFD client deregister */
1032 int zebra_ptm_bfd_client_deregister(struct zserv
*client
)
1034 uint8_t proto
= client
->proto
;
1037 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
1039 if (!IS_BFD_ENABLED_PROTOCOL(proto
))
1042 if (IS_ZEBRA_DEBUG_EVENT
)
1043 zlog_debug("bfd_client_deregister msg for client %s",
1044 zebra_route_string(proto
));
1046 if (ptm_cb
.ptm_sock
== -1) {
1047 ptm_cb
.t_timer
= NULL
;
1048 thread_add_timer(zrouter
.master
, zebra_ptm_connect
, NULL
,
1049 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
1053 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
1055 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_CLIENT_DEREG_CMD
);
1056 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
1058 sprintf(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
, struct interface
*ifp
)
1097 vty_out(vty
, " PTM status: ");
1098 if (ifp
->ptm_enable
) {
1099 vty_out(vty
, "%s\n", zebra_ptm_get_status_str(ifp
->ptm_status
));
1101 vty_out(vty
, "disabled\n");
1105 void zebra_ptm_send_status_req(void)
1108 int len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
1110 if (ptm_cb
.ptm_enable
) {
1111 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
,
1113 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
,
1114 ZEBRA_PTM_GET_STATUS_CMD
);
1115 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &len
);
1117 zebra_ptm_send_message(ptm_cb
.out_data
, len
);
1121 void zebra_ptm_reset_status(int ptm_disable
)
1124 struct interface
*ifp
;
1127 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
1128 FOR_ALL_INTERFACES (vrf
, ifp
) {
1130 if (ifp
->ptm_enable
) {
1131 if (!if_is_operative(ifp
))
1136 ZEBRA_IF_PTM_ENABLE_OFF
;
1137 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1139 if (if_is_operative(ifp
) && send_linkup
) {
1140 if (IS_ZEBRA_DEBUG_EVENT
)
1142 "%s: Bringing up interface %s",
1143 __func__
, ifp
->name
);
1150 void zebra_ptm_if_init(struct zebra_if
*zebra_ifp
)
1152 zebra_ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_UNSPEC
;
1155 void zebra_ptm_if_set_ptm_state(struct interface
*ifp
,
1156 struct zebra_if
*zebra_ifp
)
1158 if (zebra_ifp
&& zebra_ifp
->ptm_enable
!= ZEBRA_IF_PTM_ENABLE_UNSPEC
)
1159 ifp
->ptm_enable
= zebra_ifp
->ptm_enable
;
1162 void zebra_ptm_if_write(struct vty
*vty
, struct zebra_if
*zebra_ifp
)
1164 if (zebra_ifp
->ptm_enable
== ZEBRA_IF_PTM_ENABLE_OFF
)
1165 vty_out(vty
, " no ptm-enable\n");
1168 #else /* HAVE_BFDD */
1170 #include "zebra/zebra_memory.h"
1175 struct ptm_process
{
1176 struct zserv
*pp_zs
;
1179 TAILQ_ENTRY(ptm_process
) pp_entry
;
1181 TAILQ_HEAD(ppqueue
, ptm_process
) ppqueue
;
1183 DEFINE_MTYPE_STATIC(ZEBRA
, ZEBRA_PTM_BFD_PROCESS
,
1184 "PTM BFD process registration table.");
1189 static struct ptm_process
*pp_new(pid_t pid
, struct zserv
*zs
);
1190 static struct ptm_process
*pp_lookup_byzs(struct zserv
*zs
);
1191 static void pp_free(struct ptm_process
*pp
);
1192 static void pp_free_all(void);
1194 static void zebra_ptm_send_bfdd(struct stream
*msg
);
1195 static void zebra_ptm_send_clients(struct stream
*msg
);
1196 static int _zebra_ptm_bfd_client_deregister(struct zserv
*zs
);
1197 static void _zebra_ptm_reroute(struct zserv
*zs
, struct zebra_vrf
*zvrf
,
1198 struct stream
*msg
, uint32_t command
);
1202 * Process PID registration.
1204 static struct ptm_process
*pp_new(pid_t pid
, struct zserv
*zs
)
1206 struct ptm_process
*pp
;
1209 /* Sanity check: more than one client can't have the same PID. */
1210 TAILQ_FOREACH(pp
, &ppqueue
, pp_entry
) {
1211 if (pp
->pp_pid
== pid
&& pp
->pp_zs
!= zs
)
1212 zlog_err("%s:%d pid and client pointer doesn't match",
1213 __FILE__
, __LINE__
);
1215 #endif /* PTM_DEBUG */
1217 /* Lookup for duplicates. */
1218 pp
= pp_lookup_byzs(zs
);
1222 /* Allocate and register new process. */
1223 pp
= XCALLOC(MTYPE_ZEBRA_PTM_BFD_PROCESS
, sizeof(*pp
));
1227 TAILQ_INSERT_HEAD(&ppqueue
, pp
, pp_entry
);
1232 static struct ptm_process
*pp_lookup_byzs(struct zserv
*zs
)
1234 struct ptm_process
*pp
;
1236 TAILQ_FOREACH(pp
, &ppqueue
, pp_entry
) {
1237 if (pp
->pp_zs
!= zs
)
1246 static void pp_free(struct ptm_process
*pp
)
1251 TAILQ_REMOVE(&ppqueue
, pp
, pp_entry
);
1252 XFREE(MTYPE_ZEBRA_PTM_BFD_PROCESS
, pp
);
1255 static void pp_free_all(void)
1257 struct ptm_process
*pp
;
1259 while (!TAILQ_EMPTY(&ppqueue
)) {
1260 pp
= TAILQ_FIRST(&ppqueue
);
1267 * Use the FRR's internal daemon implementation.
1269 static void zebra_ptm_send_bfdd(struct stream
*msg
)
1271 struct listnode
*node
;
1272 struct zserv
*client
;
1273 struct stream
*msgc
;
1275 /* Create copy for replication. */
1276 msgc
= stream_dup(msg
);
1278 zlog_debug("%s: not enough memory", __func__
);
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
);
1292 zlog_debug("%s: not enough memory", __func__
);
1301 static void zebra_ptm_send_clients(struct stream
*msg
)
1303 struct listnode
*node
;
1304 struct zserv
*client
;
1305 struct stream
*msgc
;
1307 /* Create copy for replication. */
1308 msgc
= stream_dup(msg
);
1310 zlog_debug("%s: not enough memory", __func__
);
1314 /* Send message to all running client daemons. */
1315 for (ALL_LIST_ELEMENTS_RO(zrouter
.client_list
, node
, client
)) {
1316 if (!IS_BFD_ENABLED_PROTOCOL(client
->proto
))
1319 zserv_send_message(client
, msg
);
1321 /* Allocate more messages. */
1322 msg
= stream_dup(msgc
);
1324 zlog_debug("%s: not enough memory", __func__
);
1333 static int _zebra_ptm_bfd_client_deregister(struct zserv
*zs
)
1336 struct ptm_process
*pp
;
1338 if (!IS_BFD_ENABLED_PROTOCOL(zs
->proto
))
1341 /* Find daemon pid by zebra connection pointer. */
1342 pp
= pp_lookup_byzs(zs
);
1344 zlog_err("%s:%d failed to find process pid registration",
1345 __FILE__
, __LINE__
);
1349 /* Generate, send message and free() daemon related data. */
1350 msg
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1352 zlog_debug("%s: not enough memory", __func__
);
1357 * The message type will be BFD_DEST_REPLY so we can use only
1358 * one callback at the `bfdd` side, however the real command
1359 * number will be included right after the zebra header.
1361 zclient_create_header(msg
, ZEBRA_BFD_DEST_REPLAY
, 0);
1362 stream_putl(msg
, ZEBRA_BFD_CLIENT_DEREGISTER
);
1364 /* Put process PID. */
1365 stream_putl(msg
, pp
->pp_pid
);
1367 /* Update the data pointers. */
1368 stream_putw_at(msg
, 0, stream_get_endp(msg
));
1370 zebra_ptm_send_bfdd(msg
);
1377 void zebra_ptm_init(void)
1379 /* Initialize the ptm process information list. */
1380 TAILQ_INIT(&ppqueue
);
1383 * Send deregistration messages to BFD daemon when some other
1384 * daemon closes. This will help avoid sending daemons
1385 * unnecessary notification messages.
1387 hook_register(zserv_client_close
, _zebra_ptm_bfd_client_deregister
);
1390 void zebra_ptm_finish(void)
1392 /* Remove the client disconnect hook and free all memory. */
1393 hook_unregister(zserv_client_close
, _zebra_ptm_bfd_client_deregister
);
1401 static void _zebra_ptm_reroute(struct zserv
*zs
, struct zebra_vrf
*zvrf
,
1402 struct stream
*msg
, uint32_t command
)
1404 struct stream
*msgc
;
1405 char buf
[ZEBRA_MAX_PACKET_SIZ
];
1408 /* Create BFD header */
1409 msgc
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1410 zclient_create_header(msgc
, ZEBRA_BFD_DEST_REPLAY
, zvrf
->vrf
->vrf_id
);
1411 stream_putl(msgc
, command
);
1413 if (STREAM_READABLE(msg
) > STREAM_WRITEABLE(msgc
)) {
1414 zlog_warn("Cannot fit extended BFD header plus original message contents into ZAPI packet; dropping message");
1415 goto stream_failure
;
1418 /* Copy original message, excluding header, into new message */
1419 stream_get_from(buf
, msg
, stream_get_getp(msg
), STREAM_READABLE(msg
));
1420 stream_put(msgc
, buf
, STREAM_READABLE(msg
));
1422 /* Update length field */
1423 stream_putw_at(msgc
, 0, STREAM_READABLE(msgc
));
1425 zebra_ptm_send_bfdd(msgc
);
1428 /* Registrate process PID for shutdown hook. */
1429 STREAM_GETL(msg
, ppid
);
1437 zlog_err("%s:%d failed to registrate client pid", __FILE__
, __LINE__
);
1440 void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS
)
1442 if (IS_ZEBRA_DEBUG_EVENT
)
1443 zlog_debug("bfd_dst_register msg from client %s: length=%d",
1444 zebra_route_string(client
->proto
), hdr
->length
);
1446 _zebra_ptm_reroute(client
, zvrf
, msg
, ZEBRA_BFD_DEST_REGISTER
);
1449 void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS
)
1451 if (IS_ZEBRA_DEBUG_EVENT
)
1452 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
1453 zebra_route_string(client
->proto
), hdr
->length
);
1455 _zebra_ptm_reroute(client
, zvrf
, msg
, ZEBRA_BFD_DEST_DEREGISTER
);
1458 void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS
)
1460 if (IS_ZEBRA_DEBUG_EVENT
)
1461 zlog_debug("bfd_client_register msg from client %s: length=%d",
1462 zebra_route_string(client
->proto
), hdr
->length
);
1464 _zebra_ptm_reroute(client
, zvrf
, msg
, ZEBRA_BFD_CLIENT_REGISTER
);
1467 void zebra_ptm_bfd_dst_replay(ZAPI_HANDLER_ARGS
)
1469 struct stream
*msgc
;
1470 size_t zmsglen
, zhdrlen
;
1475 * Replay messages with HAVE_BFDD are meant to be replayed to
1476 * the client daemons. These messages are composed and
1477 * originated from the `bfdd` daemon.
1479 if (IS_ZEBRA_DEBUG_EVENT
)
1480 zlog_debug("bfd_dst_update msg from client %s: length=%d",
1481 zebra_route_string(client
->proto
), hdr
->length
);
1484 * Client messages must be re-routed, otherwise do the `bfdd`
1485 * special treatment.
1487 if (client
->proto
!= ZEBRA_ROUTE_BFD
) {
1488 _zebra_ptm_reroute(client
, zvrf
, msg
, ZEBRA_BFD_DEST_REPLAY
);
1492 /* Figure out if this is an DEST_UPDATE or DEST_REPLAY. */
1493 if (stream_getl2(msg
, &cmd
) == false) {
1494 zlog_err("%s: expected at least 4 bytes (command)", __func__
);
1499 * Don't modify message in the zebra API. In order to do that we
1500 * need to allocate a new message stream and copy the message
1501 * provided by zebra.
1503 msgc
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1505 zlog_debug("%s: not enough memory", __func__
);
1509 /* Calculate our header size plus the message contents. */
1510 if (cmd
!= ZEBRA_BFD_DEST_REPLAY
) {
1511 zhdrlen
= ZEBRA_HEADER_SIZE
;
1512 zmsglen
= msg
->endp
- msg
->getp
;
1513 memcpy(msgc
->data
+ zhdrlen
, msg
->data
+ msg
->getp
, zmsglen
);
1515 zclient_create_header(msgc
, cmd
, zvrf_id(zvrf
));
1518 msgc
->endp
= zhdrlen
+ zmsglen
;
1520 zclient_create_header(msgc
, cmd
, zvrf_id(zvrf
));
1522 /* Update the data pointers. */
1523 stream_putw_at(msgc
, 0, stream_get_endp(msgc
));
1525 zebra_ptm_send_clients(msgc
);
1531 void zebra_ptm_if_init(struct zebra_if
*zifp
__attribute__((__unused__
)))
1536 int zebra_ptm_get_enable_state(void)
1541 void zebra_ptm_show_status(struct vty
*vty
__attribute__((__unused__
)),
1542 struct interface
*ifp
__attribute__((__unused__
)))
1547 void zebra_ptm_write(struct vty
*vty
__attribute__((__unused__
)))
1552 void zebra_ptm_if_write(struct vty
*vty
__attribute__((__unused__
)),
1553 struct zebra_if
*zifp
__attribute__((__unused__
)))
1557 void zebra_ptm_if_set_ptm_state(struct interface
*i
__attribute__((__unused__
)),
1558 struct zebra_if
*zi
__attribute__((__unused__
)))
1563 #endif /* HAVE_BFDD */