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
22 #include <sys/un.h> /* for sockaddr_un */
25 #include "zebra/zserv.h"
26 #include "zebra/interface.h"
27 #include "zebra/debug.h"
28 #include "zebra/zebra_ptm.h"
35 #include "zebra/zebra_ptm_redistribute.h"
39 #include "zebra_vrf.h"
43 * Choose the BFD implementation that we'll use.
45 * There are two implementations:
46 * - PTM BFD: which uses an external daemon;
47 * - bfdd: FRR's own BFD daemon;
51 #define ZEBRA_PTM_RECONNECT_TIME_INITIAL 1 /* initial reconnect is 1s */
52 #define ZEBRA_PTM_RECONNECT_TIME_MAX 300
55 #define PTM_HEADER_LEN 37
57 const char ZEBRA_PTM_GET_STATUS_CMD
[] = "get-status";
58 const char ZEBRA_PTM_BFD_START_CMD
[] = "start-bfd-sess";
59 const char ZEBRA_PTM_BFD_STOP_CMD
[] = "stop-bfd-sess";
60 const char ZEBRA_PTM_BFD_CLIENT_REG_CMD
[] = "reg-bfd-client";
61 const char ZEBRA_PTM_BFD_CLIENT_DEREG_CMD
[] = "dereg-bfd-client";
63 const char ZEBRA_PTM_CMD_STR
[] = "cmd";
64 const char ZEBRA_PTM_CMD_STATUS_STR
[] = "cmd_status";
65 const char ZEBRA_PTM_PORT_STR
[] = "port";
66 const char ZEBRA_PTM_CBL_STR
[] = "cbl status";
67 const char ZEBRA_PTM_PASS_STR
[] = "pass";
68 const char ZEBRA_PTM_FAIL_STR
[] = "fail";
69 const char ZEBRA_PTM_BFDSTATUS_STR
[] = "state";
70 const char ZEBRA_PTM_BFDSTATUS_UP_STR
[] = "Up";
71 const char ZEBRA_PTM_BFDSTATUS_DOWN_STR
[] = "Down";
72 const char ZEBRA_PTM_BFDDEST_STR
[] = "peer";
73 const char ZEBRA_PTM_BFDSRC_STR
[] = "local";
74 const char ZEBRA_PTM_BFDVRF_STR
[] = "vrf";
75 const char ZEBRA_PTM_INVALID_PORT_NAME
[] = "N/A";
76 const char ZEBRA_PTM_INVALID_SRC_IP
[] = "N/A";
77 const char ZEBRA_PTM_INVALID_VRF
[] = "N/A";
79 const char ZEBRA_PTM_BFD_DST_IP_FIELD
[] = "dstIPaddr";
80 const char ZEBRA_PTM_BFD_SRC_IP_FIELD
[] = "srcIPaddr";
81 const char ZEBRA_PTM_BFD_MIN_RX_FIELD
[] = "requiredMinRx";
82 const char ZEBRA_PTM_BFD_MIN_TX_FIELD
[] = "upMinTx";
83 const char ZEBRA_PTM_BFD_DETECT_MULT_FIELD
[] = "detectMult";
84 const char ZEBRA_PTM_BFD_MULTI_HOP_FIELD
[] = "multiHop";
85 const char ZEBRA_PTM_BFD_CLIENT_FIELD
[] = "client";
86 const char ZEBRA_PTM_BFD_SEQID_FIELD
[] = "seqid";
87 const char ZEBRA_PTM_BFD_IFNAME_FIELD
[] = "ifName";
88 const char ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD
[] = "maxHopCnt";
89 const char ZEBRA_PTM_BFD_SEND_EVENT
[] = "sendEvent";
90 const char ZEBRA_PTM_BFD_VRF_NAME_FIELD
[] = "vrfName";
92 static ptm_lib_handle_t
*ptm_hdl
;
94 struct zebra_ptm_cb ptm_cb
;
96 static int zebra_ptm_socket_init(void);
97 int zebra_ptm_sock_read(struct thread
*);
98 static void zebra_ptm_install_commands(void);
99 static int zebra_ptm_handle_msg_cb(void *arg
, void *in_ctxt
);
100 void zebra_bfd_peer_replay_req(void);
101 void zebra_ptm_send_status_req(void);
102 void zebra_ptm_reset_status(int ptm_disable
);
103 static int zebra_ptm_bfd_client_deregister(struct zserv
*client
);
105 const char ZEBRA_PTM_SOCK_NAME
[] = "\0/var/run/ptmd.socket";
107 void zebra_ptm_init(void)
111 memset(&ptm_cb
, 0, sizeof(struct zebra_ptm_cb
));
113 ptm_cb
.out_data
= calloc(1, ZEBRA_PTM_SEND_MAX_SOCKBUF
);
114 if (!ptm_cb
.out_data
) {
115 zlog_warn("%s: Allocation of send data failed", __func__
);
119 ptm_cb
.in_data
= calloc(1, ZEBRA_PTM_MAX_SOCKBUF
);
120 if (!ptm_cb
.in_data
) {
121 zlog_warn("%s: Allocation of recv data failed", __func__
);
122 free(ptm_cb
.out_data
);
126 ptm_cb
.pid
= getpid();
127 zebra_ptm_install_commands();
129 sprintf(buf
, "%s", FRR_PTM_NAME
);
130 ptm_hdl
= ptm_lib_register(buf
, NULL
, zebra_ptm_handle_msg_cb
,
131 zebra_ptm_handle_msg_cb
);
132 ptm_cb
.wb
= buffer_new(0);
134 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
136 ptm_cb
.ptm_sock
= -1;
138 hook_register(zserv_client_close
, zebra_ptm_bfd_client_deregister
);
141 void zebra_ptm_finish(void)
143 buffer_flush_all(ptm_cb
.wb
, ptm_cb
.ptm_sock
);
148 free(ptm_cb
.out_data
);
151 free(ptm_cb
.in_data
);
153 /* Release threads. */
155 thread_cancel(ptm_cb
.t_read
);
157 thread_cancel(ptm_cb
.t_write
);
159 thread_cancel(ptm_cb
.t_timer
);
162 buffer_free(ptm_cb
.wb
);
164 if (ptm_cb
.ptm_sock
>= 0)
165 close(ptm_cb
.ptm_sock
);
168 static int zebra_ptm_flush_messages(struct thread
*thread
)
170 ptm_cb
.t_write
= NULL
;
172 if (ptm_cb
.ptm_sock
== -1)
177 switch (buffer_flush_available(ptm_cb
.wb
, ptm_cb
.ptm_sock
)) {
179 zlog_warn("%s ptm socket error: %s", __func__
,
180 safe_strerror(errno
));
181 close(ptm_cb
.ptm_sock
);
182 ptm_cb
.ptm_sock
= -1;
183 zebra_ptm_reset_status(0);
184 ptm_cb
.t_timer
= NULL
;
185 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
186 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
189 ptm_cb
.t_write
= NULL
;
190 thread_add_write(zebrad
.master
, zebra_ptm_flush_messages
, NULL
,
191 ptm_cb
.ptm_sock
, &ptm_cb
.t_write
);
200 static int zebra_ptm_send_message(char *data
, int size
)
203 switch (buffer_write(ptm_cb
.wb
, ptm_cb
.ptm_sock
, data
, size
)) {
205 zlog_warn("%s ptm socket error: %s", __func__
,
206 safe_strerror(errno
));
207 close(ptm_cb
.ptm_sock
);
208 ptm_cb
.ptm_sock
= -1;
209 zebra_ptm_reset_status(0);
210 ptm_cb
.t_timer
= NULL
;
211 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
212 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
215 THREAD_OFF(ptm_cb
.t_write
);
218 thread_add_write(zebrad
.master
, zebra_ptm_flush_messages
, NULL
,
219 ptm_cb
.ptm_sock
, &ptm_cb
.t_write
);
226 int zebra_ptm_connect(struct thread
*t
)
230 if (ptm_cb
.ptm_sock
== -1) {
231 zebra_ptm_socket_init();
235 if (ptm_cb
.ptm_sock
!= -1) {
237 ptm_cb
.t_read
= NULL
;
238 thread_add_read(zebrad
.master
, zebra_ptm_sock_read
,
239 NULL
, ptm_cb
.ptm_sock
, &ptm_cb
.t_read
);
240 zebra_bfd_peer_replay_req();
242 zebra_ptm_send_status_req();
243 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
244 } else if (ptm_cb
.reconnect_time
< ZEBRA_PTM_RECONNECT_TIME_MAX
) {
245 ptm_cb
.reconnect_time
*= 2;
246 if (ptm_cb
.reconnect_time
> ZEBRA_PTM_RECONNECT_TIME_MAX
)
247 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_MAX
;
249 ptm_cb
.t_timer
= NULL
;
250 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
251 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
252 } else if (ptm_cb
.reconnect_time
>= ZEBRA_PTM_RECONNECT_TIME_MAX
) {
253 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
259 DEFUN (zebra_ptm_enable
,
260 zebra_ptm_enable_cmd
,
262 "Enable neighbor check with specified topology\n")
265 struct interface
*ifp
;
266 struct zebra_if
*if_data
;
268 ptm_cb
.ptm_enable
= ZEBRA_IF_PTM_ENABLE_ON
;
270 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
271 FOR_ALL_INTERFACES (vrf
, ifp
)
272 if (!ifp
->ptm_enable
) {
273 if_data
= (struct zebra_if
*)ifp
->info
;
275 && (if_data
->ptm_enable
276 == ZEBRA_IF_PTM_ENABLE_UNSPEC
)) {
278 ZEBRA_IF_PTM_ENABLE_ON
;
280 /* Assign a default unknown status */
281 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
284 zebra_ptm_connect(NULL
);
289 DEFUN (no_zebra_ptm_enable
,
290 no_zebra_ptm_enable_cmd
,
293 "Enable neighbor check with specified topology\n")
295 ptm_cb
.ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
296 zebra_ptm_reset_status(1);
300 DEFUN (zebra_ptm_enable_if
,
301 zebra_ptm_enable_if_cmd
,
303 "Enable neighbor check with specified topology\n")
305 VTY_DECLVAR_CONTEXT(interface
, ifp
);
306 struct zebra_if
*if_data
;
308 int send_linkdown
= 0;
311 if_data
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_UNSPEC
;
313 if (ifp
->ifindex
== IFINDEX_INTERNAL
) {
317 old_ptm_enable
= ifp
->ptm_enable
;
318 ifp
->ptm_enable
= ptm_cb
.ptm_enable
;
320 if (if_is_no_ptm_operative(ifp
))
323 if (!old_ptm_enable
&& ptm_cb
.ptm_enable
) {
324 if (!if_is_operative(ifp
) && send_linkdown
) {
325 if (IS_ZEBRA_DEBUG_EVENT
)
326 zlog_debug("%s: Bringing down interface %s\n",
327 __func__
, ifp
->name
);
335 DEFUN (no_zebra_ptm_enable_if
,
336 no_zebra_ptm_enable_if_cmd
,
339 "Enable neighbor check with specified topology\n")
341 VTY_DECLVAR_CONTEXT(interface
, ifp
);
343 struct zebra_if
*if_data
;
345 if ((ifp
->ifindex
!= IFINDEX_INTERNAL
) && (ifp
->ptm_enable
)) {
346 if (!if_is_operative(ifp
))
349 ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
350 if (if_is_no_ptm_operative(ifp
) && send_linkup
) {
351 if (IS_ZEBRA_DEBUG_EVENT
)
352 zlog_debug("%s: Bringing up interface %s\n",
353 __func__
, ifp
->name
);
359 if_data
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
365 void zebra_ptm_write(struct vty
*vty
)
367 if (ptm_cb
.ptm_enable
)
368 vty_out(vty
, "ptm-enable\n");
373 static int zebra_ptm_socket_init(void)
377 struct sockaddr_un addr
;
379 ptm_cb
.ptm_sock
= -1;
381 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
384 if (set_nonblocking(sock
) < 0) {
385 if (IS_ZEBRA_DEBUG_EVENT
)
386 zlog_debug("%s: Unable to set socket non blocking[%s]",
387 __PRETTY_FUNCTION__
, safe_strerror(errno
));
392 /* Make server socket. */
393 memset(&addr
, 0, sizeof(struct sockaddr_un
));
394 addr
.sun_family
= AF_UNIX
;
395 memcpy(&addr
.sun_path
, ZEBRA_PTM_SOCK_NAME
,
396 sizeof(ZEBRA_PTM_SOCK_NAME
));
398 ret
= connect(sock
, (struct sockaddr
*)&addr
,
399 sizeof(addr
.sun_family
) + sizeof(ZEBRA_PTM_SOCK_NAME
)
402 if (IS_ZEBRA_DEBUG_EVENT
)
403 zlog_debug("%s: Unable to connect to socket %s [%s]",
404 __func__
, ZEBRA_PTM_SOCK_NAME
,
405 safe_strerror(errno
));
409 ptm_cb
.ptm_sock
= sock
;
413 static void zebra_ptm_install_commands(void)
415 install_element(CONFIG_NODE
, &zebra_ptm_enable_cmd
);
416 install_element(CONFIG_NODE
, &no_zebra_ptm_enable_cmd
);
417 install_element(INTERFACE_NODE
, &zebra_ptm_enable_if_cmd
);
418 install_element(INTERFACE_NODE
, &no_zebra_ptm_enable_if_cmd
);
421 /* BFD session goes down, send message to the protocols. */
422 static void if_bfd_session_update(struct interface
*ifp
, struct prefix
*dp
,
423 struct prefix
*sp
, int status
,
426 if (IS_ZEBRA_DEBUG_EVENT
) {
427 char buf
[2][INET6_ADDRSTRLEN
];
431 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d on %s"
433 inet_ntop(dp
->family
, &dp
->u
.prefix
, buf
[0],
435 dp
->prefixlen
, ifp
->name
,
436 bfd_get_status_str(status
));
439 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d "
440 "with src %s/%d and vrf %u %s event",
441 inet_ntop(dp
->family
, &dp
->u
.prefix
, buf
[0],
444 inet_ntop(sp
->family
, &sp
->u
.prefix
, buf
[1],
446 sp
->prefixlen
, vrf_id
,
447 bfd_get_status_str(status
));
451 zebra_interface_bfd_update(ifp
, dp
, sp
, status
, vrf_id
);
454 static int zebra_ptm_handle_bfd_msg(void *arg
, void *in_ctxt
,
455 struct interface
*ifp
)
461 struct prefix dest_prefix
;
462 struct prefix src_prefix
;
465 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDSTATUS_STR
, bfdst_str
);
467 if (bfdst_str
[0] == '\0') {
471 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDDEST_STR
, dest_str
);
473 if (dest_str
[0] == '\0') {
474 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
475 ZEBRA_PTM_BFDDEST_STR
);
479 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDSRC_STR
, src_str
);
481 if (src_str
[0] == '\0') {
482 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
483 ZEBRA_PTM_BFDSRC_STR
);
487 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDVRF_STR
, vrf_str
);
489 if (vrf_str
[0] == '\0') {
490 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
491 ZEBRA_PTM_BFDVRF_STR
);
495 if (IS_ZEBRA_DEBUG_EVENT
)
497 "%s: Recv Port [%s] bfd status [%s] vrf [%s]"
498 " peer [%s] local [%s]",
499 __func__
, ifp
? ifp
->name
: "N/A", bfdst_str
, vrf_str
,
502 if (str2prefix(dest_str
, &dest_prefix
) == 0) {
503 zlog_err("%s: Peer addr %s not found", __func__
, dest_str
);
507 memset(&src_prefix
, 0, sizeof(struct prefix
));
508 if (strcmp(ZEBRA_PTM_INVALID_SRC_IP
, src_str
)) {
509 if (str2prefix(src_str
, &src_prefix
) == 0) {
510 zlog_err("%s: Local addr %s not found", __func__
,
516 if (!strcmp(ZEBRA_PTM_INVALID_VRF
, vrf_str
) && ifp
) {
517 vrf_id
= ifp
->vrf_id
;
519 vrf_id
= vrf_name_to_id(vrf_str
);
522 if (!strcmp(bfdst_str
, ZEBRA_PTM_BFDSTATUS_DOWN_STR
)) {
523 if_bfd_session_update(ifp
, &dest_prefix
, &src_prefix
,
524 BFD_STATUS_DOWN
, vrf_id
);
526 if_bfd_session_update(ifp
, &dest_prefix
, &src_prefix
,
527 BFD_STATUS_UP
, vrf_id
);
533 static int zebra_ptm_handle_cbl_msg(void *arg
, void *in_ctxt
,
534 struct interface
*ifp
, char *cbl_str
)
538 if (IS_ZEBRA_DEBUG_EVENT
)
539 zlog_debug("%s: Recv Port [%s] cbl status [%s]", __func__
,
542 if (!strcmp(cbl_str
, ZEBRA_PTM_PASS_STR
)
543 && (ifp
->ptm_status
!= ZEBRA_PTM_STATUS_UP
)) {
545 if (ifp
->ptm_status
== ZEBRA_PTM_STATUS_DOWN
)
547 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UP
;
548 if (ifp
->ptm_enable
&& if_is_no_ptm_operative(ifp
)
551 } else if (!strcmp(cbl_str
, ZEBRA_PTM_FAIL_STR
)
552 && (ifp
->ptm_status
!= ZEBRA_PTM_STATUS_DOWN
)) {
553 ifp
->ptm_status
= ZEBRA_PTM_STATUS_DOWN
;
554 if (ifp
->ptm_enable
&& if_is_no_ptm_operative(ifp
))
562 * zebra_ptm_handle_msg_cb - The purpose of this callback function is to handle
563 * all the command responses and notifications received from PTM.
565 * Command responses: Upon establishing connection with PTM, Zebra requests
566 * status of all interfaces using 'get-status' command if global ptm-enable
567 * knob is enabled. As a response to the get-status command PTM sends status
568 * of all the interfaces as command responses. All other type of command
569 * responses with cmd_status key word are dropped. The sole purpose of
570 * registering this function as callback for the command responses is to
571 * handle the responses to get-status command.
573 * Notifications: Cable status and BFD session status changes are sent as
574 * notifications by PTM. So, this function is also the callback function for
575 * processing all the notifications from the PTM.
578 static int zebra_ptm_handle_msg_cb(void *arg
, void *in_ctxt
)
580 struct interface
*ifp
= NULL
;
583 char cmd_status_str
[32];
585 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_CMD_STATUS_STR
,
588 /* Drop command response messages */
589 if (cmd_status_str
[0] != '\0') {
593 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_PORT_STR
, port_str
);
595 if (port_str
[0] == '\0') {
596 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
601 if (strcmp(ZEBRA_PTM_INVALID_PORT_NAME
, port_str
)) {
602 ifp
= if_lookup_by_name_all_vrf(port_str
);
605 zlog_err("%s: %s not found in interface list", __func__
,
611 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_CBL_STR
, cbl_str
);
613 if (cbl_str
[0] == '\0') {
614 return zebra_ptm_handle_bfd_msg(arg
, in_ctxt
, ifp
);
617 return zebra_ptm_handle_cbl_msg(arg
, in_ctxt
, ifp
,
625 int zebra_ptm_sock_read(struct thread
*thread
)
631 sock
= THREAD_FD(thread
);
636 /* PTM communicates in CSV format */
638 rc
= ptm_lib_process_msg(ptm_hdl
, sock
, ptm_cb
.in_data
,
639 ZEBRA_PTM_MAX_SOCKBUF
, NULL
);
642 if (((rc
== 0) && !errno
)
643 || (errno
&& (errno
!= EWOULDBLOCK
) && (errno
!= EAGAIN
))) {
644 zlog_warn("%s routing socket error: %s(%d) bytes %d",
645 __func__
, safe_strerror(errno
), errno
, rc
);
647 close(ptm_cb
.ptm_sock
);
648 ptm_cb
.ptm_sock
= -1;
649 zebra_ptm_reset_status(0);
650 ptm_cb
.t_timer
= NULL
;
651 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
652 ptm_cb
.reconnect_time
,
657 ptm_cb
.t_read
= NULL
;
658 thread_add_read(zebrad
.master
, zebra_ptm_sock_read
, NULL
,
659 ptm_cb
.ptm_sock
, &ptm_cb
.t_read
);
664 /* BFD peer/dst register/update */
665 void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS
)
671 uint8_t multi_hop_cnt
;
673 unsigned int min_rx_timer
;
674 unsigned int min_tx_timer
;
675 char if_name
[INTERFACE_NAMSIZ
];
678 char buf
[INET6_ADDRSTRLEN
];
680 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
683 if (hdr
->command
== ZEBRA_BFD_DEST_UPDATE
)
684 client
->bfd_peer_upd8_cnt
++;
686 client
->bfd_peer_add_cnt
++;
688 if (IS_ZEBRA_DEBUG_EVENT
)
689 zlog_debug("bfd_dst_register msg from client %s: length=%d",
690 zebra_route_string(client
->proto
), hdr
->length
);
692 if (ptm_cb
.ptm_sock
== -1) {
693 ptm_cb
.t_timer
= NULL
;
694 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
695 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
699 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
700 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_START_CMD
);
701 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
702 sprintf(tmp_buf
, "%s", zebra_route_string(client
->proto
));
703 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
709 sprintf(tmp_buf
, "%d", pid
);
710 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
713 STREAM_GETW(s
, dst_p
.family
);
715 if (dst_p
.family
== AF_INET
)
716 dst_p
.prefixlen
= IPV4_MAX_BYTELEN
;
718 dst_p
.prefixlen
= IPV6_MAX_BYTELEN
;
720 STREAM_GET(&dst_p
.u
.prefix
, s
, dst_p
.prefixlen
);
721 if (dst_p
.family
== AF_INET
) {
722 inet_ntop(AF_INET
, &dst_p
.u
.prefix4
, buf
, sizeof(buf
));
723 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
724 ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
726 inet_ntop(AF_INET6
, &dst_p
.u
.prefix6
, buf
, sizeof(buf
));
727 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
728 ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
731 STREAM_GETL(s
, min_rx_timer
);
732 sprintf(tmp_buf
, "%d", min_rx_timer
);
733 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_MIN_RX_FIELD
,
735 STREAM_GETL(s
, min_tx_timer
);
736 sprintf(tmp_buf
, "%d", min_tx_timer
);
737 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_MIN_TX_FIELD
,
739 STREAM_GETC(s
, detect_mul
);
740 sprintf(tmp_buf
, "%d", detect_mul
);
741 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_DETECT_MULT_FIELD
,
744 STREAM_GETC(s
, multi_hop
);
746 sprintf(tmp_buf
, "%d", 1);
747 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
748 ZEBRA_PTM_BFD_MULTI_HOP_FIELD
, tmp_buf
);
749 STREAM_GETW(s
, src_p
.family
);
751 if (src_p
.family
== AF_INET
)
752 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
754 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
756 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
757 if (src_p
.family
== AF_INET
) {
758 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
, sizeof(buf
));
759 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
760 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
762 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
, sizeof(buf
));
763 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
764 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
767 STREAM_GETC(s
, multi_hop_cnt
);
768 sprintf(tmp_buf
, "%d", multi_hop_cnt
);
769 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
770 ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD
, tmp_buf
);
772 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
773 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
774 ZEBRA_PTM_BFD_VRF_NAME_FIELD
,
777 if (dst_p
.family
== AF_INET6
) {
778 STREAM_GETW(s
, src_p
.family
);
780 if (src_p
.family
== AF_INET
)
781 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
783 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
785 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
786 if (src_p
.family
== AF_INET
) {
787 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
,
789 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
790 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
793 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
,
795 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
796 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
801 STREAM_GET(if_name
, s
, len
);
804 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
805 ZEBRA_PTM_BFD_IFNAME_FIELD
, if_name
);
808 sprintf(tmp_buf
, "%d", 1);
809 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEND_EVENT
,
812 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
814 if (IS_ZEBRA_DEBUG_SEND
)
815 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
817 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
822 ptm_lib_cleanup_msg(ptm_hdl
, out_ctxt
);
825 /* BFD peer/dst deregister */
826 void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS
)
832 char if_name
[INTERFACE_NAMSIZ
];
834 char buf
[INET6_ADDRSTRLEN
];
836 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
840 client
->bfd_peer_del_cnt
++;
842 if (IS_ZEBRA_DEBUG_EVENT
)
843 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
844 zebra_route_string(client
->proto
), hdr
->length
);
846 if (ptm_cb
.ptm_sock
== -1) {
847 ptm_cb
.t_timer
= NULL
;
848 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
849 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
853 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
855 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_STOP_CMD
);
856 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
858 sprintf(tmp_buf
, "%s", zebra_route_string(client
->proto
));
859 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
865 sprintf(tmp_buf
, "%d", pid
);
866 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
869 STREAM_GETW(s
, dst_p
.family
);
871 if (dst_p
.family
== AF_INET
)
872 dst_p
.prefixlen
= IPV4_MAX_BYTELEN
;
874 dst_p
.prefixlen
= IPV6_MAX_BYTELEN
;
876 STREAM_GET(&dst_p
.u
.prefix
, s
, dst_p
.prefixlen
);
877 if (dst_p
.family
== AF_INET
)
878 inet_ntop(AF_INET
, &dst_p
.u
.prefix4
, buf
, sizeof(buf
));
880 inet_ntop(AF_INET6
, &dst_p
.u
.prefix6
, buf
, sizeof(buf
));
881 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
884 STREAM_GETC(s
, multi_hop
);
886 sprintf(tmp_buf
, "%d", 1);
887 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
888 ZEBRA_PTM_BFD_MULTI_HOP_FIELD
, tmp_buf
);
890 STREAM_GETW(s
, src_p
.family
);
892 if (src_p
.family
== AF_INET
)
893 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
895 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
897 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
898 if (src_p
.family
== AF_INET
)
899 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
, sizeof(buf
));
901 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
, sizeof(buf
));
902 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
903 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
905 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
906 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
907 ZEBRA_PTM_BFD_VRF_NAME_FIELD
,
910 if (dst_p
.family
== AF_INET6
) {
911 STREAM_GETW(s
, src_p
.family
);
913 if (src_p
.family
== AF_INET
)
914 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
916 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
918 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
919 if (src_p
.family
== AF_INET
) {
920 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
,
922 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
923 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
926 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
,
928 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
929 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
935 STREAM_GET(if_name
, s
, len
);
938 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
939 ZEBRA_PTM_BFD_IFNAME_FIELD
, if_name
);
942 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
943 if (IS_ZEBRA_DEBUG_SEND
)
944 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
947 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
952 ptm_lib_cleanup_msg(ptm_hdl
, out_ctxt
);
955 /* BFD client register */
956 void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS
)
960 void *out_ctxt
= NULL
;
962 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
964 client
->bfd_client_reg_cnt
++;
966 if (IS_ZEBRA_DEBUG_EVENT
)
967 zlog_debug("bfd_client_register msg from client %s: length=%d",
968 zebra_route_string(client
->proto
), hdr
->length
);
973 if (ptm_cb
.ptm_sock
== -1) {
974 ptm_cb
.t_timer
= NULL
;
975 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
976 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
980 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
982 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_CLIENT_REG_CMD
);
983 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
985 sprintf(tmp_buf
, "%s", zebra_route_string(client
->proto
));
986 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
989 sprintf(tmp_buf
, "%d", pid
);
990 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
993 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
995 if (IS_ZEBRA_DEBUG_SEND
)
996 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
998 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
1000 SET_FLAG(ptm_cb
.client_flags
[client
->proto
],
1001 ZEBRA_PTM_BFD_CLIENT_FLAG_REG
);
1007 * IF we ever add more STREAM_GETXXX functions after the out_ctxt
1008 * is allocated then we need to add this code back in
1011 * ptm_lib_cleanup_msg(ptm_hdl, out_ctxt);
1016 /* BFD client deregister */
1017 int zebra_ptm_bfd_client_deregister(struct zserv
*client
)
1019 uint8_t proto
= client
->proto
;
1022 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
1024 if (proto
!= ZEBRA_ROUTE_OSPF
&& proto
!= ZEBRA_ROUTE_BGP
1025 && proto
!= ZEBRA_ROUTE_OSPF6
&& proto
!= ZEBRA_ROUTE_PIM
)
1028 if (IS_ZEBRA_DEBUG_EVENT
)
1029 zlog_err("bfd_client_deregister msg for client %s",
1030 zebra_route_string(proto
));
1032 if (ptm_cb
.ptm_sock
== -1) {
1033 ptm_cb
.t_timer
= NULL
;
1034 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
1035 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
1039 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
1041 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_CLIENT_DEREG_CMD
);
1042 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
1044 sprintf(tmp_buf
, "%s", zebra_route_string(proto
));
1045 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
1048 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
1050 if (IS_ZEBRA_DEBUG_SEND
)
1051 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
1054 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
1055 UNSET_FLAG(ptm_cb
.client_flags
[proto
], ZEBRA_PTM_BFD_CLIENT_FLAG_REG
);
1060 int zebra_ptm_get_enable_state(void)
1062 return ptm_cb
.ptm_enable
;
1066 * zebra_ptm_get_status_str - Convert status to a display string.
1068 static const char *zebra_ptm_get_status_str(int status
)
1071 case ZEBRA_PTM_STATUS_DOWN
:
1073 case ZEBRA_PTM_STATUS_UP
:
1075 case ZEBRA_PTM_STATUS_UNKNOWN
:
1081 void zebra_ptm_show_status(struct vty
*vty
, struct interface
*ifp
)
1083 vty_out(vty
, " PTM status: ");
1084 if (ifp
->ptm_enable
) {
1085 vty_out(vty
, "%s\n", zebra_ptm_get_status_str(ifp
->ptm_status
));
1087 vty_out(vty
, "disabled\n");
1091 void zebra_ptm_send_status_req(void)
1094 int len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
1096 if (ptm_cb
.ptm_enable
) {
1097 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
,
1099 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
,
1100 ZEBRA_PTM_GET_STATUS_CMD
);
1101 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &len
);
1103 zebra_ptm_send_message(ptm_cb
.out_data
, len
);
1107 void zebra_ptm_reset_status(int ptm_disable
)
1110 struct interface
*ifp
;
1113 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
1114 FOR_ALL_INTERFACES (vrf
, ifp
) {
1116 if (ifp
->ptm_enable
) {
1117 if (!if_is_operative(ifp
))
1122 ZEBRA_IF_PTM_ENABLE_OFF
;
1123 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1125 if (if_is_operative(ifp
) && send_linkup
) {
1126 if (IS_ZEBRA_DEBUG_EVENT
)
1128 "%s: Bringing up interface %s",
1129 __func__
, ifp
->name
);
1136 void zebra_ptm_if_init(struct zebra_if
*zebra_ifp
)
1138 zebra_ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_UNSPEC
;
1141 void zebra_ptm_if_set_ptm_state(struct interface
*ifp
,
1142 struct zebra_if
*zebra_ifp
)
1144 if (zebra_ifp
&& zebra_ifp
->ptm_enable
!= ZEBRA_IF_PTM_ENABLE_UNSPEC
)
1145 ifp
->ptm_enable
= zebra_ifp
->ptm_enable
;
1148 void zebra_ptm_if_write(struct vty
*vty
, struct zebra_if
*zebra_ifp
)
1150 if (zebra_ifp
->ptm_enable
== ZEBRA_IF_PTM_ENABLE_OFF
)
1151 vty_out(vty
, " no ptm-enable\n");
1154 #else /* HAVE_BFDD */
1156 #include "zebra/zebra_memory.h"
1161 struct ptm_process
{
1162 struct zserv
*pp_zs
;
1165 TAILQ_ENTRY(ptm_process
) pp_entry
;
1167 TAILQ_HEAD(ppqueue
, ptm_process
) ppqueue
;
1169 DEFINE_MTYPE_STATIC(ZEBRA
, ZEBRA_PTM_BFD_PROCESS
,
1170 "PTM BFD process registration table.");
1175 static struct ptm_process
*pp_new(pid_t pid
, struct zserv
*zs
);
1176 static struct ptm_process
*pp_lookup_byzs(struct zserv
*zs
);
1177 static void pp_free(struct ptm_process
*pp
);
1179 static void zebra_ptm_send_bfdd(struct stream
*msg
);
1180 static void zebra_ptm_send_clients(struct stream
*msg
);
1181 static int _zebra_ptm_bfd_client_deregister(struct zserv
*zs
);
1182 static void _zebra_ptm_reroute(struct zserv
*zs
, struct stream
*msg
,
1187 * Process PID registration.
1189 static struct ptm_process
*pp_new(pid_t pid
, struct zserv
*zs
)
1191 struct ptm_process
*pp
;
1194 /* Sanity check: more than one client can't have the same PID. */
1195 TAILQ_FOREACH(pp
, &ppqueue
, pp_entry
) {
1196 if (pp
->pp_pid
== pid
&& pp
->pp_zs
!= zs
)
1197 zlog_err("%s:%d pid and client pointer doesn't match",
1198 __FILE__
, __LINE__
);
1200 #endif /* PTM_DEBUG */
1202 /* Lookup for duplicates. */
1203 pp
= pp_lookup_byzs(zs
);
1207 /* Allocate and register new process. */
1208 pp
= XCALLOC(MTYPE_ZEBRA_PTM_BFD_PROCESS
, sizeof(*pp
));
1214 TAILQ_INSERT_HEAD(&ppqueue
, pp
, pp_entry
);
1219 static struct ptm_process
*pp_lookup_byzs(struct zserv
*zs
)
1221 struct ptm_process
*pp
;
1223 TAILQ_FOREACH(pp
, &ppqueue
, pp_entry
) {
1224 if (pp
->pp_zs
!= zs
)
1233 static void pp_free(struct ptm_process
*pp
)
1238 TAILQ_REMOVE(&ppqueue
, pp
, pp_entry
);
1239 XFREE(MTYPE_ZEBRA_PTM_BFD_PROCESS
, pp
);
1244 * Use the FRR's internal daemon implementation.
1246 static void zebra_ptm_send_bfdd(struct stream
*msg
)
1248 struct listnode
*node
;
1249 struct zserv
*client
;
1250 struct stream
*msgc
;
1252 /* Create copy for replication. */
1253 msgc
= stream_dup(msg
);
1255 zlog_warn("%s: not enough memory", __func__
);
1259 /* Send message to all running BFDd daemons. */
1260 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
1261 if (client
->proto
!= ZEBRA_ROUTE_BFD
)
1264 zserv_send_message(client
, msg
);
1266 /* Allocate more messages. */
1267 msg
= stream_dup(msgc
);
1269 zlog_warn("%s: not enough memory", __func__
);
1277 static void zebra_ptm_send_clients(struct stream
*msg
)
1279 struct listnode
*node
;
1280 struct zserv
*client
;
1281 struct stream
*msgc
;
1283 /* Create copy for replication. */
1284 msgc
= stream_dup(msg
);
1286 zlog_warn("%s: not enough memory", __func__
);
1290 /* Send message to all running client daemons. */
1291 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
1292 switch (client
->proto
) {
1293 case ZEBRA_ROUTE_BGP
:
1294 case ZEBRA_ROUTE_OSPF
:
1295 case ZEBRA_ROUTE_OSPF6
:
1296 case ZEBRA_ROUTE_PIM
:
1300 /* NOTHING: skip this daemon. */
1304 zserv_send_message(client
, msg
);
1306 /* Allocate more messages. */
1307 msg
= stream_dup(msgc
);
1309 zlog_warn("%s: not enough memory", __func__
);
1317 static int _zebra_ptm_bfd_client_deregister(struct zserv
*zs
)
1320 struct ptm_process
*pp
;
1322 /* Filter daemons that must receive this treatment. */
1323 switch (zs
->proto
) {
1324 case ZEBRA_ROUTE_BGP
:
1325 case ZEBRA_ROUTE_OSPF
:
1326 case ZEBRA_ROUTE_OSPF6
:
1327 case ZEBRA_ROUTE_PIM
:
1330 case ZEBRA_ROUTE_BFD
:
1331 /* Don't try to send BFDd messages to itself. */
1335 /* Unsupported daemon. */
1339 /* Find daemon pid by zebra connection pointer. */
1340 pp
= pp_lookup_byzs(zs
);
1342 zlog_err("%s:%d failed to find process pid registration",
1343 __FILE__
, __LINE__
);
1347 /* Generate, send message and free() daemon related data. */
1348 msg
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1350 zlog_warn("%s: not enough memory", __func__
);
1355 * The message type will be BFD_DEST_REPLY so we can use only
1356 * one callback at the `bfdd` side, however the real command
1357 * number will be included right after the zebra header.
1359 zclient_create_header(msg
, ZEBRA_BFD_DEST_REPLAY
, 0);
1360 stream_putl(msg
, ZEBRA_BFD_CLIENT_DEREGISTER
);
1362 /* Put process PID. */
1363 stream_putl(msg
, pp
->pp_pid
);
1365 /* Update the data pointers. */
1366 stream_putw_at(msg
, 0, stream_get_endp(msg
));
1368 zebra_ptm_send_bfdd(msg
);
1375 void zebra_ptm_init(void)
1377 /* Initialize the ptm process information list. */
1378 TAILQ_INIT(&ppqueue
);
1381 * Send deregistration messages to BFD daemon when some other
1382 * daemon closes. This will help avoid sending daemons
1383 * unnecessary notification messages.
1385 hook_register(zserv_client_close
, _zebra_ptm_bfd_client_deregister
);
1392 static void _zebra_ptm_reroute(struct zserv
*zs
, struct stream
*msg
,
1395 struct stream
*msgc
;
1396 size_t zmsglen
, zhdrlen
;
1400 * Don't modify message in the zebra API. In order to do that we
1401 * need to allocate a new message stream and copy the message
1402 * provided by zebra.
1404 msgc
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1406 zlog_warn("%s: not enough memory", __func__
);
1410 /* Calculate our header size plus the message contents. */
1411 zhdrlen
= ZEBRA_HEADER_SIZE
+ sizeof(uint32_t);
1412 zmsglen
= msg
->endp
- msg
->getp
;
1413 memcpy(msgc
->data
+ zhdrlen
, msg
->data
+ msg
->getp
, zmsglen
);
1416 * The message type will be BFD_DEST_REPLY so we can use only
1417 * one callback at the `bfdd` side, however the real command
1418 * number will be included right after the zebra header.
1420 zclient_create_header(msgc
, ZEBRA_BFD_DEST_REPLAY
, 0);
1421 stream_putl(msgc
, command
);
1423 /* Update the data pointers. */
1425 msgc
->endp
= zhdrlen
+ zmsglen
;
1426 stream_putw_at(msgc
, 0, stream_get_endp(msgc
));
1428 zebra_ptm_send_bfdd(msgc
);
1430 /* Registrate process PID for shutdown hook. */
1431 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
, 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
, 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
, 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
, 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_warn("%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_finish(void)
1535 void zebra_ptm_if_init(struct zebra_if
*zifp
__attribute__((__unused__
)))
1540 int zebra_ptm_get_enable_state(void)
1545 void zebra_ptm_show_status(struct vty
*vty
__attribute__((__unused__
)),
1546 struct interface
*ifp
__attribute__((__unused__
)))
1551 void zebra_ptm_write(struct vty
*vty
__attribute__((__unused__
)))
1556 void zebra_ptm_if_write(struct vty
*vty
__attribute__((__unused__
)),
1557 struct zebra_if
*zifp
__attribute__((__unused__
)))
1561 void zebra_ptm_if_set_ptm_state(struct interface
*i
__attribute__((__unused__
)),
1562 struct zebra_if
*zi
__attribute__((__unused__
)))
1567 #endif /* HAVE_BFDD */