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"
42 #define ZEBRA_PTM_RECONNECT_TIME_INITIAL 1 /* initial reconnect is 1s */
43 #define ZEBRA_PTM_RECONNECT_TIME_MAX 300
46 #define PTM_HEADER_LEN 37
48 const char ZEBRA_PTM_GET_STATUS_CMD
[] = "get-status";
49 const char ZEBRA_PTM_BFD_START_CMD
[] = "start-bfd-sess";
50 const char ZEBRA_PTM_BFD_STOP_CMD
[] = "stop-bfd-sess";
51 const char ZEBRA_PTM_BFD_CLIENT_REG_CMD
[] = "reg-bfd-client";
52 const char ZEBRA_PTM_BFD_CLIENT_DEREG_CMD
[] = "dereg-bfd-client";
54 const char ZEBRA_PTM_CMD_STR
[] = "cmd";
55 const char ZEBRA_PTM_CMD_STATUS_STR
[] = "cmd_status";
56 const char ZEBRA_PTM_PORT_STR
[] = "port";
57 const char ZEBRA_PTM_CBL_STR
[] = "cbl status";
58 const char ZEBRA_PTM_PASS_STR
[] = "pass";
59 const char ZEBRA_PTM_FAIL_STR
[] = "fail";
60 const char ZEBRA_PTM_BFDSTATUS_STR
[] = "state";
61 const char ZEBRA_PTM_BFDSTATUS_UP_STR
[] = "Up";
62 const char ZEBRA_PTM_BFDSTATUS_DOWN_STR
[] = "Down";
63 const char ZEBRA_PTM_BFDDEST_STR
[] = "peer";
64 const char ZEBRA_PTM_BFDSRC_STR
[] = "local";
65 const char ZEBRA_PTM_BFDVRF_STR
[] = "vrf";
66 const char ZEBRA_PTM_INVALID_PORT_NAME
[] = "N/A";
67 const char ZEBRA_PTM_INVALID_SRC_IP
[] = "N/A";
68 const char ZEBRA_PTM_INVALID_VRF
[] = "N/A";
70 const char ZEBRA_PTM_BFD_DST_IP_FIELD
[] = "dstIPaddr";
71 const char ZEBRA_PTM_BFD_SRC_IP_FIELD
[] = "srcIPaddr";
72 const char ZEBRA_PTM_BFD_MIN_RX_FIELD
[] = "requiredMinRx";
73 const char ZEBRA_PTM_BFD_MIN_TX_FIELD
[] = "upMinTx";
74 const char ZEBRA_PTM_BFD_DETECT_MULT_FIELD
[] = "detectMult";
75 const char ZEBRA_PTM_BFD_MULTI_HOP_FIELD
[] = "multiHop";
76 const char ZEBRA_PTM_BFD_CLIENT_FIELD
[] = "client";
77 const char ZEBRA_PTM_BFD_SEQID_FIELD
[] = "seqid";
78 const char ZEBRA_PTM_BFD_IFNAME_FIELD
[] = "ifName";
79 const char ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD
[] = "maxHopCnt";
80 const char ZEBRA_PTM_BFD_SEND_EVENT
[] = "sendEvent";
81 const char ZEBRA_PTM_BFD_VRF_NAME_FIELD
[] = "vrfName";
83 static ptm_lib_handle_t
*ptm_hdl
;
85 struct zebra_ptm_cb ptm_cb
;
87 static int zebra_ptm_socket_init(void);
88 int zebra_ptm_sock_read(struct 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
);
95 const char ZEBRA_PTM_SOCK_NAME
[] = "\0/var/run/ptmd.socket";
97 void zebra_ptm_init(void)
101 memset(&ptm_cb
, 0, sizeof(struct zebra_ptm_cb
));
103 ptm_cb
.out_data
= calloc(1, ZEBRA_PTM_SEND_MAX_SOCKBUF
);
104 if (!ptm_cb
.out_data
) {
105 zlog_warn("%s: Allocation of send data failed", __func__
);
109 ptm_cb
.in_data
= calloc(1, ZEBRA_PTM_MAX_SOCKBUF
);
110 if (!ptm_cb
.in_data
) {
111 zlog_warn("%s: Allocation of recv data failed", __func__
);
112 free(ptm_cb
.out_data
);
116 ptm_cb
.pid
= getpid();
117 zebra_ptm_install_commands();
119 sprintf(buf
, "%s", FRR_PTM_NAME
);
120 ptm_hdl
= ptm_lib_register(buf
, NULL
, zebra_ptm_handle_msg_cb
,
121 zebra_ptm_handle_msg_cb
);
122 ptm_cb
.wb
= buffer_new(0);
124 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
126 ptm_cb
.ptm_sock
= -1;
129 void zebra_ptm_finish(void)
133 for (proto
= 0; proto
< ZEBRA_ROUTE_MAX
; proto
++)
134 if (CHECK_FLAG(ptm_cb
.client_flags
[proto
],
135 ZEBRA_PTM_BFD_CLIENT_FLAG_REG
))
136 zebra_ptm_bfd_client_deregister(proto
);
138 buffer_flush_all(ptm_cb
.wb
, ptm_cb
.ptm_sock
);
143 free(ptm_cb
.out_data
);
146 free(ptm_cb
.in_data
);
148 /* Release threads. */
150 thread_cancel(ptm_cb
.t_read
);
152 thread_cancel(ptm_cb
.t_write
);
154 thread_cancel(ptm_cb
.t_timer
);
157 buffer_free(ptm_cb
.wb
);
159 if (ptm_cb
.ptm_sock
>= 0)
160 close(ptm_cb
.ptm_sock
);
163 static int zebra_ptm_flush_messages(struct thread
*thread
)
165 ptm_cb
.t_write
= NULL
;
167 if (ptm_cb
.ptm_sock
== -1)
172 switch (buffer_flush_available(ptm_cb
.wb
, ptm_cb
.ptm_sock
)) {
174 zlog_warn("%s ptm socket error: %s", __func__
,
175 safe_strerror(errno
));
176 close(ptm_cb
.ptm_sock
);
177 ptm_cb
.ptm_sock
= -1;
178 zebra_ptm_reset_status(0);
179 ptm_cb
.t_timer
= NULL
;
180 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
181 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
184 ptm_cb
.t_write
= NULL
;
185 thread_add_write(zebrad
.master
, zebra_ptm_flush_messages
, NULL
,
186 ptm_cb
.ptm_sock
, &ptm_cb
.t_write
);
195 static int zebra_ptm_send_message(char *data
, int size
)
198 switch (buffer_write(ptm_cb
.wb
, ptm_cb
.ptm_sock
, data
, size
)) {
200 zlog_warn("%s ptm socket error: %s", __func__
,
201 safe_strerror(errno
));
202 close(ptm_cb
.ptm_sock
);
203 ptm_cb
.ptm_sock
= -1;
204 zebra_ptm_reset_status(0);
205 ptm_cb
.t_timer
= NULL
;
206 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
207 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
210 THREAD_OFF(ptm_cb
.t_write
);
213 thread_add_write(zebrad
.master
, zebra_ptm_flush_messages
, NULL
,
214 ptm_cb
.ptm_sock
, &ptm_cb
.t_write
);
221 int zebra_ptm_connect(struct thread
*t
)
225 if (ptm_cb
.ptm_sock
== -1) {
226 zebra_ptm_socket_init();
230 if (ptm_cb
.ptm_sock
!= -1) {
232 ptm_cb
.t_read
= NULL
;
233 thread_add_read(zebrad
.master
, zebra_ptm_sock_read
,
234 NULL
, ptm_cb
.ptm_sock
, &ptm_cb
.t_read
);
235 zebra_bfd_peer_replay_req();
237 zebra_ptm_send_status_req();
238 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
239 } else if (ptm_cb
.reconnect_time
< ZEBRA_PTM_RECONNECT_TIME_MAX
) {
240 ptm_cb
.reconnect_time
*= 2;
241 if (ptm_cb
.reconnect_time
> ZEBRA_PTM_RECONNECT_TIME_MAX
)
242 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_MAX
;
244 ptm_cb
.t_timer
= NULL
;
245 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
246 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
247 } else if (ptm_cb
.reconnect_time
>= ZEBRA_PTM_RECONNECT_TIME_MAX
) {
248 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
254 DEFUN (zebra_ptm_enable
,
255 zebra_ptm_enable_cmd
,
257 "Enable neighbor check with specified topology\n")
260 struct interface
*ifp
;
261 struct zebra_if
*if_data
;
263 ptm_cb
.ptm_enable
= ZEBRA_IF_PTM_ENABLE_ON
;
265 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
266 FOR_ALL_INTERFACES (vrf
, ifp
)
267 if (!ifp
->ptm_enable
) {
268 if_data
= (struct zebra_if
*)ifp
->info
;
270 && (if_data
->ptm_enable
271 == ZEBRA_IF_PTM_ENABLE_UNSPEC
)) {
273 ZEBRA_IF_PTM_ENABLE_ON
;
275 /* Assign a default unknown status */
276 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
279 zebra_ptm_connect(NULL
);
284 DEFUN (no_zebra_ptm_enable
,
285 no_zebra_ptm_enable_cmd
,
288 "Enable neighbor check with specified topology\n")
290 ptm_cb
.ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
291 zebra_ptm_reset_status(1);
295 DEFUN (zebra_ptm_enable_if
,
296 zebra_ptm_enable_if_cmd
,
298 "Enable neighbor check with specified topology\n")
300 VTY_DECLVAR_CONTEXT(interface
, ifp
);
301 struct zebra_if
*if_data
;
303 int send_linkdown
= 0;
305 if (ifp
->ifindex
== IFINDEX_INTERNAL
) {
309 old_ptm_enable
= ifp
->ptm_enable
;
310 ifp
->ptm_enable
= ptm_cb
.ptm_enable
;
312 if (if_is_no_ptm_operative(ifp
))
315 if (!old_ptm_enable
&& ptm_cb
.ptm_enable
) {
316 if (!if_is_operative(ifp
) && send_linkdown
) {
317 if (IS_ZEBRA_DEBUG_EVENT
)
318 zlog_debug("%s: Bringing down interface %s\n",
319 __func__
, ifp
->name
);
325 if_data
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_UNSPEC
;
330 DEFUN (no_zebra_ptm_enable_if
,
331 no_zebra_ptm_enable_if_cmd
,
334 "Enable neighbor check with specified topology\n")
336 VTY_DECLVAR_CONTEXT(interface
, ifp
);
338 struct zebra_if
*if_data
;
340 if ((ifp
->ifindex
!= IFINDEX_INTERNAL
) && (ifp
->ptm_enable
)) {
341 if (!if_is_operative(ifp
))
344 ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
345 if (if_is_no_ptm_operative(ifp
) && send_linkup
) {
346 if (IS_ZEBRA_DEBUG_EVENT
)
347 zlog_debug("%s: Bringing up interface %s\n",
348 __func__
, ifp
->name
);
354 if_data
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
360 void zebra_ptm_write(struct vty
*vty
)
362 if (ptm_cb
.ptm_enable
)
363 vty_out(vty
, "ptm-enable\n");
368 static int zebra_ptm_socket_init(void)
372 struct sockaddr_un addr
;
374 ptm_cb
.ptm_sock
= -1;
376 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
379 if (set_nonblocking(sock
) < 0) {
380 if (IS_ZEBRA_DEBUG_EVENT
)
381 zlog_debug("%s: Unable to set socket non blocking[%s]",
382 __PRETTY_FUNCTION__
, safe_strerror(errno
));
387 /* Make server socket. */
388 memset(&addr
, 0, sizeof(struct sockaddr_un
));
389 addr
.sun_family
= AF_UNIX
;
390 memcpy(&addr
.sun_path
, ZEBRA_PTM_SOCK_NAME
,
391 sizeof(ZEBRA_PTM_SOCK_NAME
));
393 ret
= connect(sock
, (struct sockaddr
*)&addr
,
394 sizeof(addr
.sun_family
) + sizeof(ZEBRA_PTM_SOCK_NAME
)
397 if (IS_ZEBRA_DEBUG_EVENT
)
398 zlog_debug("%s: Unable to connect to socket %s [%s]",
399 __func__
, ZEBRA_PTM_SOCK_NAME
,
400 safe_strerror(errno
));
404 ptm_cb
.ptm_sock
= sock
;
408 static void zebra_ptm_install_commands(void)
410 install_element(CONFIG_NODE
, &zebra_ptm_enable_cmd
);
411 install_element(CONFIG_NODE
, &no_zebra_ptm_enable_cmd
);
412 install_element(INTERFACE_NODE
, &zebra_ptm_enable_if_cmd
);
413 install_element(INTERFACE_NODE
, &no_zebra_ptm_enable_if_cmd
);
416 /* BFD session goes down, send message to the protocols. */
417 static void if_bfd_session_update(struct interface
*ifp
, struct prefix
*dp
,
418 struct prefix
*sp
, int status
,
421 if (IS_ZEBRA_DEBUG_EVENT
) {
422 char buf
[2][INET6_ADDRSTRLEN
];
426 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d on %s"
428 inet_ntop(dp
->family
, &dp
->u
.prefix
, buf
[0],
430 dp
->prefixlen
, ifp
->name
,
431 bfd_get_status_str(status
));
434 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d "
435 "with src %s/%d and vrf %u %s event",
436 inet_ntop(dp
->family
, &dp
->u
.prefix
, buf
[0],
439 inet_ntop(sp
->family
, &sp
->u
.prefix
, buf
[1],
441 sp
->prefixlen
, vrf_id
,
442 bfd_get_status_str(status
));
446 zebra_interface_bfd_update(ifp
, dp
, sp
, status
, vrf_id
);
449 static int zebra_ptm_handle_bfd_msg(void *arg
, void *in_ctxt
,
450 struct interface
*ifp
)
456 struct prefix dest_prefix
;
457 struct prefix src_prefix
;
460 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDSTATUS_STR
, bfdst_str
);
462 if (bfdst_str
[0] == '\0') {
466 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDDEST_STR
, dest_str
);
468 if (dest_str
[0] == '\0') {
469 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
470 ZEBRA_PTM_BFDDEST_STR
);
474 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDSRC_STR
, src_str
);
476 if (src_str
[0] == '\0') {
477 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
478 ZEBRA_PTM_BFDSRC_STR
);
482 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDVRF_STR
, vrf_str
);
484 if (vrf_str
[0] == '\0') {
485 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
486 ZEBRA_PTM_BFDVRF_STR
);
490 if (IS_ZEBRA_DEBUG_EVENT
)
492 "%s: Recv Port [%s] bfd status [%s] vrf [%s]"
493 " peer [%s] local [%s]",
494 __func__
, ifp
? ifp
->name
: "N/A", bfdst_str
, vrf_str
,
497 if (str2prefix(dest_str
, &dest_prefix
) == 0) {
498 zlog_err("%s: Peer addr %s not found", __func__
, dest_str
);
502 memset(&src_prefix
, 0, sizeof(struct prefix
));
503 if (strcmp(ZEBRA_PTM_INVALID_SRC_IP
, src_str
)) {
504 if (str2prefix(src_str
, &src_prefix
) == 0) {
505 zlog_err("%s: Local addr %s not found", __func__
,
511 if (!strcmp(ZEBRA_PTM_INVALID_VRF
, vrf_str
) && ifp
) {
512 vrf_id
= ifp
->vrf_id
;
514 vrf_id
= vrf_name_to_id(vrf_str
);
517 if (!strcmp(bfdst_str
, ZEBRA_PTM_BFDSTATUS_DOWN_STR
)) {
518 if_bfd_session_update(ifp
, &dest_prefix
, &src_prefix
,
519 BFD_STATUS_DOWN
, vrf_id
);
521 if_bfd_session_update(ifp
, &dest_prefix
, &src_prefix
,
522 BFD_STATUS_UP
, vrf_id
);
528 static int zebra_ptm_handle_cbl_msg(void *arg
, void *in_ctxt
,
529 struct interface
*ifp
, char *cbl_str
)
533 if (IS_ZEBRA_DEBUG_EVENT
)
534 zlog_debug("%s: Recv Port [%s] cbl status [%s]", __func__
,
537 if (!strcmp(cbl_str
, ZEBRA_PTM_PASS_STR
)
538 && (ifp
->ptm_status
!= ZEBRA_PTM_STATUS_UP
)) {
540 if (ifp
->ptm_status
== ZEBRA_PTM_STATUS_DOWN
)
542 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UP
;
543 if (ifp
->ptm_enable
&& if_is_no_ptm_operative(ifp
)
546 } else if (!strcmp(cbl_str
, ZEBRA_PTM_FAIL_STR
)
547 && (ifp
->ptm_status
!= ZEBRA_PTM_STATUS_DOWN
)) {
548 ifp
->ptm_status
= ZEBRA_PTM_STATUS_DOWN
;
549 if (ifp
->ptm_enable
&& if_is_no_ptm_operative(ifp
))
557 * zebra_ptm_handle_msg_cb - The purpose of this callback function is to handle
558 * all the command responses and notifications received from PTM.
560 * Command responses: Upon establishing connection with PTM, Zebra requests
561 * status of all interfaces using 'get-status' command if global ptm-enable
562 * knob is enabled. As a response to the get-status command PTM sends status
563 * of all the interfaces as command responses. All other type of command
564 * responses with cmd_status key word are dropped. The sole purpose of
565 * registering this function as callback for the command responses is to
566 * handle the responses to get-status command.
568 * Notifications: Cable status and BFD session status changes are sent as
569 * notifications by PTM. So, this function is also the callback function for
570 * processing all the notifications from the PTM.
573 static int zebra_ptm_handle_msg_cb(void *arg
, void *in_ctxt
)
575 struct interface
*ifp
= NULL
;
578 char cmd_status_str
[32];
580 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_CMD_STATUS_STR
,
583 /* Drop command response messages */
584 if (cmd_status_str
[0] != '\0') {
588 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_PORT_STR
, port_str
);
590 if (port_str
[0] == '\0') {
591 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
596 if (strcmp(ZEBRA_PTM_INVALID_PORT_NAME
, port_str
)) {
597 ifp
= if_lookup_by_name_all_vrf(port_str
);
600 zlog_err("%s: %s not found in interface list", __func__
,
606 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_CBL_STR
, cbl_str
);
608 if (cbl_str
[0] == '\0') {
609 return zebra_ptm_handle_bfd_msg(arg
, in_ctxt
, ifp
);
612 return zebra_ptm_handle_cbl_msg(arg
, in_ctxt
, ifp
,
620 int zebra_ptm_sock_read(struct thread
*thread
)
626 sock
= THREAD_FD(thread
);
631 /* PTM communicates in CSV format */
633 rc
= ptm_lib_process_msg(ptm_hdl
, sock
, ptm_cb
.in_data
,
634 ZEBRA_PTM_MAX_SOCKBUF
, NULL
);
640 if (((rc
== 0) && !errno
)
641 || (errno
&& (errno
!= EWOULDBLOCK
) && (errno
!= EAGAIN
))) {
642 zlog_warn("%s routing socket error: %s(%d) bytes %d",
643 __func__
, safe_strerror(errno
), errno
, rc
);
645 close(ptm_cb
.ptm_sock
);
646 ptm_cb
.ptm_sock
= -1;
647 zebra_ptm_reset_status(0);
648 ptm_cb
.t_timer
= NULL
;
649 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
650 ptm_cb
.reconnect_time
,
656 ptm_cb
.t_read
= NULL
;
657 thread_add_read(zebrad
.master
, zebra_ptm_sock_read
, NULL
,
658 ptm_cb
.ptm_sock
, &ptm_cb
.t_read
);
663 /* BFD peer/dst register/update */
664 void zebra_ptm_bfd_dst_register(struct zserv
*client
, u_short length
,
665 int command
, struct zebra_vrf
*zvrf
)
671 u_char 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 (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
), 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
);
826 /* BFD peer/dst deregister */
827 void zebra_ptm_bfd_dst_deregister(struct zserv
*client
, u_short length
,
828 struct zebra_vrf
*zvrf
)
834 char if_name
[INTERFACE_NAMSIZ
];
836 char buf
[INET6_ADDRSTRLEN
];
838 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
842 client
->bfd_peer_del_cnt
++;
844 if (IS_ZEBRA_DEBUG_EVENT
)
845 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
846 zebra_route_string(client
->proto
), length
);
848 if (ptm_cb
.ptm_sock
== -1) {
849 ptm_cb
.t_timer
= NULL
;
850 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
851 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
855 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
857 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_STOP_CMD
);
858 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
860 sprintf(tmp_buf
, "%s", zebra_route_string(client
->proto
));
861 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
867 sprintf(tmp_buf
, "%d", pid
);
868 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
871 STREAM_GETW(s
, dst_p
.family
);
873 if (dst_p
.family
== AF_INET
)
874 dst_p
.prefixlen
= IPV4_MAX_BYTELEN
;
876 dst_p
.prefixlen
= IPV6_MAX_BYTELEN
;
878 STREAM_GET(&dst_p
.u
.prefix
, s
, dst_p
.prefixlen
);
879 if (dst_p
.family
== AF_INET
)
880 inet_ntop(AF_INET
, &dst_p
.u
.prefix4
, buf
, sizeof(buf
));
882 inet_ntop(AF_INET6
, &dst_p
.u
.prefix6
, buf
, sizeof(buf
));
883 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
886 STREAM_GETC(s
, multi_hop
);
888 sprintf(tmp_buf
, "%d", 1);
889 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
890 ZEBRA_PTM_BFD_MULTI_HOP_FIELD
, tmp_buf
);
892 STREAM_GETW(s
, src_p
.family
);
894 if (src_p
.family
== AF_INET
)
895 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
897 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
899 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
900 if (src_p
.family
== AF_INET
)
901 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
, sizeof(buf
));
903 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
, sizeof(buf
));
904 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
905 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
907 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
908 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
909 ZEBRA_PTM_BFD_VRF_NAME_FIELD
,
912 if (dst_p
.family
== AF_INET6
) {
913 STREAM_GETW(s
, src_p
.family
);
915 if (src_p
.family
== AF_INET
)
916 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
918 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
920 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
921 if (src_p
.family
== AF_INET
) {
922 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
,
924 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
925 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
928 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
,
930 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
931 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
937 STREAM_GET(if_name
, s
, len
);
940 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
941 ZEBRA_PTM_BFD_IFNAME_FIELD
, if_name
);
944 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
945 if (IS_ZEBRA_DEBUG_SEND
)
946 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
949 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
954 ptm_lib_cleanup_msg(ptm_hdl
, out_ctxt
);
958 /* BFD client register */
959 void zebra_ptm_bfd_client_register(struct zserv
*client
, u_short length
)
963 void *out_ctxt
= NULL
;
965 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
967 client
->bfd_client_reg_cnt
++;
969 if (IS_ZEBRA_DEBUG_EVENT
)
970 zlog_debug("bfd_client_register msg from client %s: length=%d",
971 zebra_route_string(client
->proto
), length
);
976 if (ptm_cb
.ptm_sock
== -1) {
977 ptm_cb
.t_timer
= NULL
;
978 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
979 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
983 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
985 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_CLIENT_REG_CMD
);
986 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
988 sprintf(tmp_buf
, "%s", zebra_route_string(client
->proto
));
989 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
992 sprintf(tmp_buf
, "%d", pid
);
993 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
996 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
998 if (IS_ZEBRA_DEBUG_SEND
)
999 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
1001 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
1003 SET_FLAG(ptm_cb
.client_flags
[client
->proto
],
1004 ZEBRA_PTM_BFD_CLIENT_FLAG_REG
);
1010 * IF we ever add more STREAM_GETXXX functions after the out_ctxt
1011 * is allocated then we need to add this code back in
1014 * ptm_lib_cleanup_msg(ptm_hdl, out_ctxt);
1019 /* BFD client deregister */
1020 void zebra_ptm_bfd_client_deregister(int proto
)
1024 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
1026 if (proto
!= ZEBRA_ROUTE_OSPF
&& proto
!= ZEBRA_ROUTE_BGP
1027 && proto
!= ZEBRA_ROUTE_OSPF6
&& proto
!= ZEBRA_ROUTE_PIM
)
1030 if (IS_ZEBRA_DEBUG_EVENT
)
1031 zlog_err("bfd_client_deregister msg for client %s",
1032 zebra_route_string(proto
));
1034 if (ptm_cb
.ptm_sock
== -1) {
1035 ptm_cb
.t_timer
= NULL
;
1036 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
1037 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
1041 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
1043 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_CLIENT_DEREG_CMD
);
1044 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
1046 sprintf(tmp_buf
, "%s", zebra_route_string(proto
));
1047 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
1050 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
1052 if (IS_ZEBRA_DEBUG_SEND
)
1053 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
1056 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
1057 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");