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")
261 struct interface
*ifp
;
262 struct zebra_if
*if_data
;
264 ptm_cb
.ptm_enable
= ZEBRA_IF_PTM_ENABLE_ON
;
266 RB_FOREACH(vrf
, vrf_name_head
, &vrfs_by_name
)
267 for (ALL_LIST_ELEMENTS_RO(vrf
->iflist
, i
, ifp
))
268 if (!ifp
->ptm_enable
) {
269 if_data
= (struct zebra_if
*)ifp
->info
;
270 if (if_data
&& (if_data
->ptm_enable
271 == ZEBRA_IF_PTM_ENABLE_UNSPEC
)) {
272 ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_ON
;
274 /* Assign a default unknown status */
275 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
278 zebra_ptm_connect(NULL
);
283 DEFUN (no_zebra_ptm_enable
,
284 no_zebra_ptm_enable_cmd
,
287 "Enable neighbor check with specified topology\n")
289 ptm_cb
.ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
290 zebra_ptm_reset_status(1);
294 DEFUN (zebra_ptm_enable_if
,
295 zebra_ptm_enable_if_cmd
,
297 "Enable neighbor check with specified topology\n")
299 VTY_DECLVAR_CONTEXT(interface
, ifp
);
300 struct zebra_if
*if_data
;
302 int send_linkdown
= 0;
304 if (ifp
->ifindex
== IFINDEX_INTERNAL
) {
308 old_ptm_enable
= ifp
->ptm_enable
;
309 ifp
->ptm_enable
= ptm_cb
.ptm_enable
;
311 if (if_is_no_ptm_operative(ifp
))
314 if (!old_ptm_enable
&& ptm_cb
.ptm_enable
) {
315 if (!if_is_operative(ifp
) && send_linkdown
) {
316 if (IS_ZEBRA_DEBUG_EVENT
)
317 zlog_debug("%s: Bringing down interface %s\n",
318 __func__
, ifp
->name
);
324 if_data
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_UNSPEC
;
329 DEFUN (no_zebra_ptm_enable_if
,
330 no_zebra_ptm_enable_if_cmd
,
333 "Enable neighbor check with specified topology\n")
335 VTY_DECLVAR_CONTEXT(interface
, ifp
);
337 struct zebra_if
*if_data
;
339 if ((ifp
->ifindex
!= IFINDEX_INTERNAL
) && (ifp
->ptm_enable
)) {
340 if (!if_is_operative(ifp
))
343 ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
344 if (if_is_no_ptm_operative(ifp
) && send_linkup
) {
345 if (IS_ZEBRA_DEBUG_EVENT
)
346 zlog_debug("%s: Bringing up interface %s\n",
347 __func__
, ifp
->name
);
353 if_data
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
359 void zebra_ptm_write(struct vty
*vty
)
361 if (ptm_cb
.ptm_enable
)
362 vty_out(vty
, "ptm-enable\n");
367 static int zebra_ptm_socket_init(void)
371 struct sockaddr_un addr
;
373 ptm_cb
.ptm_sock
= -1;
375 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
378 if (set_nonblocking(sock
) < 0) {
379 if (IS_ZEBRA_DEBUG_EVENT
)
380 zlog_debug("%s: Unable to set socket non blocking[%s]",
381 __PRETTY_FUNCTION__
, safe_strerror(errno
));
386 /* Make server socket. */
387 memset(&addr
, 0, sizeof(struct sockaddr_un
));
388 addr
.sun_family
= AF_UNIX
;
389 memcpy(&addr
.sun_path
, ZEBRA_PTM_SOCK_NAME
,
390 sizeof(ZEBRA_PTM_SOCK_NAME
));
392 ret
= connect(sock
, (struct sockaddr
*)&addr
,
393 sizeof(addr
.sun_family
) + sizeof(ZEBRA_PTM_SOCK_NAME
)
396 if (IS_ZEBRA_DEBUG_EVENT
)
397 zlog_debug("%s: Unable to connect to socket %s [%s]",
398 __func__
, ZEBRA_PTM_SOCK_NAME
,
399 safe_strerror(errno
));
403 ptm_cb
.ptm_sock
= sock
;
407 static void zebra_ptm_install_commands(void)
409 install_element(CONFIG_NODE
, &zebra_ptm_enable_cmd
);
410 install_element(CONFIG_NODE
, &no_zebra_ptm_enable_cmd
);
411 install_element(INTERFACE_NODE
, &zebra_ptm_enable_if_cmd
);
412 install_element(INTERFACE_NODE
, &no_zebra_ptm_enable_if_cmd
);
415 /* BFD session goes down, send message to the protocols. */
416 static void if_bfd_session_update(struct interface
*ifp
, struct prefix
*dp
,
417 struct prefix
*sp
, int status
,
420 if (IS_ZEBRA_DEBUG_EVENT
) {
421 char buf
[2][INET6_ADDRSTRLEN
];
425 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d on %s"
427 inet_ntop(dp
->family
, &dp
->u
.prefix
, buf
[0],
429 dp
->prefixlen
, ifp
->name
,
430 bfd_get_status_str(status
));
433 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d "
434 "with src %s/%d and vrf %d %s event",
435 inet_ntop(dp
->family
, &dp
->u
.prefix
, buf
[0],
438 inet_ntop(sp
->family
, &sp
->u
.prefix
, buf
[1],
440 sp
->prefixlen
, vrf_id
,
441 bfd_get_status_str(status
));
445 zebra_interface_bfd_update(ifp
, dp
, sp
, status
, vrf_id
);
448 static int zebra_ptm_handle_bfd_msg(void *arg
, void *in_ctxt
,
449 struct interface
*ifp
)
455 struct prefix dest_prefix
;
456 struct prefix src_prefix
;
459 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDSTATUS_STR
, bfdst_str
);
461 if (bfdst_str
[0] == '\0') {
465 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDDEST_STR
, dest_str
);
467 if (dest_str
[0] == '\0') {
468 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
469 ZEBRA_PTM_BFDDEST_STR
);
473 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDSRC_STR
, src_str
);
475 if (src_str
[0] == '\0') {
476 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
477 ZEBRA_PTM_BFDSRC_STR
);
481 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDVRF_STR
, vrf_str
);
483 if (vrf_str
[0] == '\0') {
484 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
485 ZEBRA_PTM_BFDVRF_STR
);
489 if (IS_ZEBRA_DEBUG_EVENT
)
491 "%s: Recv Port [%s] bfd status [%s] vrf [%s]"
492 " peer [%s] local [%s]",
493 __func__
, ifp
? ifp
->name
: "N/A", bfdst_str
, vrf_str
,
496 if (str2prefix(dest_str
, &dest_prefix
) == 0) {
497 zlog_err("%s: Peer addr %s not found", __func__
, dest_str
);
501 memset(&src_prefix
, 0, sizeof(struct prefix
));
502 if (strcmp(ZEBRA_PTM_INVALID_SRC_IP
, src_str
)) {
503 if (str2prefix(src_str
, &src_prefix
) == 0) {
504 zlog_err("%s: Local addr %s not found", __func__
,
510 if (!strcmp(ZEBRA_PTM_INVALID_VRF
, vrf_str
) && ifp
) {
511 vrf_id
= ifp
->vrf_id
;
513 vrf_id
= vrf_name_to_id(vrf_str
);
516 if (!strcmp(bfdst_str
, ZEBRA_PTM_BFDSTATUS_DOWN_STR
)) {
517 if_bfd_session_update(ifp
, &dest_prefix
, &src_prefix
,
518 BFD_STATUS_DOWN
, vrf_id
);
520 if_bfd_session_update(ifp
, &dest_prefix
, &src_prefix
,
521 BFD_STATUS_UP
, vrf_id
);
527 static int zebra_ptm_handle_cbl_msg(void *arg
, void *in_ctxt
,
528 struct interface
*ifp
, char *cbl_str
)
532 if (IS_ZEBRA_DEBUG_EVENT
)
533 zlog_debug("%s: Recv Port [%s] cbl status [%s]", __func__
,
536 if (!strcmp(cbl_str
, ZEBRA_PTM_PASS_STR
)
537 && (ifp
->ptm_status
!= ZEBRA_PTM_STATUS_UP
)) {
539 if (ifp
->ptm_status
== ZEBRA_PTM_STATUS_DOWN
)
541 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UP
;
542 if (ifp
->ptm_enable
&& if_is_no_ptm_operative(ifp
)
545 } else if (!strcmp(cbl_str
, ZEBRA_PTM_FAIL_STR
)
546 && (ifp
->ptm_status
!= ZEBRA_PTM_STATUS_DOWN
)) {
547 ifp
->ptm_status
= ZEBRA_PTM_STATUS_DOWN
;
548 if (ifp
->ptm_enable
&& if_is_no_ptm_operative(ifp
))
556 * zebra_ptm_handle_msg_cb - The purpose of this callback function is to handle
557 * all the command responses and notifications received from PTM.
559 * Command responses: Upon establishing connection with PTM, Zebra requests
560 * status of all interfaces using 'get-status' command if global ptm-enable
561 * knob is enabled. As a response to the get-status command PTM sends status
562 * of all the interfaces as command responses. All other type of command
563 * responses with cmd_status key word are dropped. The sole purpose of
564 * registering this function as callback for the command responses is to
565 * handle the responses to get-status command.
567 * Notifications: Cable status and BFD session status changes are sent as
568 * notifications by PTM. So, this function is also the callback function for
569 * processing all the notifications from the PTM.
572 static int zebra_ptm_handle_msg_cb(void *arg
, void *in_ctxt
)
574 struct interface
*ifp
= NULL
;
577 char cmd_status_str
[32];
579 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_CMD_STATUS_STR
,
582 /* Drop command response messages */
583 if (cmd_status_str
[0] != '\0') {
587 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_PORT_STR
, port_str
);
589 if (port_str
[0] == '\0') {
590 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
595 if (strcmp(ZEBRA_PTM_INVALID_PORT_NAME
, port_str
)) {
596 ifp
= if_lookup_by_name_all_vrf(port_str
);
599 zlog_err("%s: %s not found in interface list", __func__
,
605 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_CBL_STR
, cbl_str
);
607 if (cbl_str
[0] == '\0') {
608 return zebra_ptm_handle_bfd_msg(arg
, in_ctxt
, ifp
);
611 return zebra_ptm_handle_cbl_msg(arg
, in_ctxt
, ifp
,
619 int zebra_ptm_sock_read(struct thread
*thread
)
625 sock
= THREAD_FD(thread
);
630 /* PTM communicates in CSV format */
632 rc
= ptm_lib_process_msg(ptm_hdl
, sock
, ptm_cb
.in_data
,
633 ZEBRA_PTM_MAX_SOCKBUF
, NULL
);
639 if (((rc
== 0) && !errno
)
640 || (errno
&& (errno
!= EWOULDBLOCK
) && (errno
!= EAGAIN
))) {
641 zlog_warn("%s routing socket error: %s(%d) bytes %d",
642 __func__
, safe_strerror(errno
), errno
, rc
);
644 close(ptm_cb
.ptm_sock
);
645 ptm_cb
.ptm_sock
= -1;
646 zebra_ptm_reset_status(0);
647 ptm_cb
.t_timer
= NULL
;
648 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
649 ptm_cb
.reconnect_time
,
655 ptm_cb
.t_read
= NULL
;
656 thread_add_read(zebrad
.master
, zebra_ptm_sock_read
, NULL
,
657 ptm_cb
.ptm_sock
, &ptm_cb
.t_read
);
662 /* BFD peer/dst register/update */
663 int zebra_ptm_bfd_dst_register(struct zserv
*client
, int sock
, u_short length
,
664 int command
, struct zebra_vrf
*zvrf
)
670 u_char multi_hop_cnt
;
672 unsigned int min_rx_timer
;
673 unsigned int min_tx_timer
;
674 char if_name
[INTERFACE_NAMSIZ
];
677 char buf
[INET6_ADDRSTRLEN
];
679 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
682 if (command
== ZEBRA_BFD_DEST_UPDATE
)
683 client
->bfd_peer_upd8_cnt
++;
685 client
->bfd_peer_add_cnt
++;
687 if (IS_ZEBRA_DEBUG_EVENT
)
688 zlog_debug("bfd_dst_register msg from client %s: length=%d",
689 zebra_route_string(client
->proto
), length
);
691 if (ptm_cb
.ptm_sock
== -1) {
692 ptm_cb
.t_timer
= NULL
;
693 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
694 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
698 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
699 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_START_CMD
);
700 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
701 sprintf(tmp_buf
, "%s", zebra_route_string(client
->proto
));
702 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
707 pid
= stream_getl(s
);
708 sprintf(tmp_buf
, "%d", pid
);
709 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
712 dst_p
.family
= stream_getw(s
);
714 if (dst_p
.family
== AF_INET
)
715 dst_p
.prefixlen
= IPV4_MAX_BYTELEN
;
717 dst_p
.prefixlen
= IPV6_MAX_BYTELEN
;
719 stream_get(&dst_p
.u
.prefix
, s
, dst_p
.prefixlen
);
720 if (dst_p
.family
== AF_INET
) {
721 inet_ntop(AF_INET
, &dst_p
.u
.prefix4
, buf
, sizeof(buf
));
722 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
723 ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
725 inet_ntop(AF_INET6
, &dst_p
.u
.prefix6
, buf
, sizeof(buf
));
726 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
727 ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
730 min_rx_timer
= stream_getl(s
);
731 sprintf(tmp_buf
, "%d", min_rx_timer
);
732 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_MIN_RX_FIELD
,
734 min_tx_timer
= stream_getl(s
);
735 sprintf(tmp_buf
, "%d", min_tx_timer
);
736 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_MIN_TX_FIELD
,
738 detect_mul
= stream_getc(s
);
739 sprintf(tmp_buf
, "%d", detect_mul
);
740 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_DETECT_MULT_FIELD
,
743 multi_hop
= stream_getc(s
);
745 sprintf(tmp_buf
, "%d", 1);
746 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
747 ZEBRA_PTM_BFD_MULTI_HOP_FIELD
, tmp_buf
);
748 src_p
.family
= stream_getw(s
);
750 if (src_p
.family
== AF_INET
)
751 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
753 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
755 stream_get(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
756 if (src_p
.family
== AF_INET
) {
757 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
, sizeof(buf
));
758 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
759 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
761 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
, sizeof(buf
));
762 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
763 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
766 multi_hop_cnt
= stream_getc(s
);
767 sprintf(tmp_buf
, "%d", multi_hop_cnt
);
768 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
769 ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD
, tmp_buf
);
771 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
772 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
773 ZEBRA_PTM_BFD_VRF_NAME_FIELD
,
776 if (dst_p
.family
== AF_INET6
) {
777 src_p
.family
= stream_getw(s
);
779 if (src_p
.family
== AF_INET
)
780 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
782 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
784 stream_get(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
785 if (src_p
.family
== AF_INET
) {
786 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
,
788 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
789 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
792 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
,
794 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
795 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
799 len
= stream_getc(s
);
800 stream_get(if_name
, s
, len
);
803 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
804 ZEBRA_PTM_BFD_IFNAME_FIELD
, if_name
);
807 sprintf(tmp_buf
, "%d", 1);
808 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEND_EVENT
,
811 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
813 if (IS_ZEBRA_DEBUG_SEND
)
814 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
816 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
820 /* BFD peer/dst deregister */
821 int zebra_ptm_bfd_dst_deregister(struct zserv
*client
, int sock
, u_short length
,
822 struct zebra_vrf
*zvrf
)
828 char if_name
[INTERFACE_NAMSIZ
];
830 char buf
[INET6_ADDRSTRLEN
];
832 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
836 client
->bfd_peer_del_cnt
++;
838 if (IS_ZEBRA_DEBUG_EVENT
)
839 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
840 zebra_route_string(client
->proto
), length
);
842 if (ptm_cb
.ptm_sock
== -1) {
843 ptm_cb
.t_timer
= NULL
;
844 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
845 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
849 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
851 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_STOP_CMD
);
852 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
854 sprintf(tmp_buf
, "%s", zebra_route_string(client
->proto
));
855 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
860 pid
= stream_getl(s
);
861 sprintf(tmp_buf
, "%d", pid
);
862 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
865 dst_p
.family
= stream_getw(s
);
867 if (dst_p
.family
== AF_INET
)
868 dst_p
.prefixlen
= IPV4_MAX_BYTELEN
;
870 dst_p
.prefixlen
= IPV6_MAX_BYTELEN
;
872 stream_get(&dst_p
.u
.prefix
, s
, dst_p
.prefixlen
);
873 if (dst_p
.family
== AF_INET
)
874 inet_ntop(AF_INET
, &dst_p
.u
.prefix4
, buf
, sizeof(buf
));
876 inet_ntop(AF_INET6
, &dst_p
.u
.prefix6
, buf
, sizeof(buf
));
877 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
880 multi_hop
= stream_getc(s
);
882 sprintf(tmp_buf
, "%d", 1);
883 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
884 ZEBRA_PTM_BFD_MULTI_HOP_FIELD
, tmp_buf
);
886 src_p
.family
= stream_getw(s
);
888 if (src_p
.family
== AF_INET
)
889 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
891 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
893 stream_get(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
894 if (src_p
.family
== AF_INET
)
895 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
, sizeof(buf
));
897 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
, sizeof(buf
));
898 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
899 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
901 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
902 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
903 ZEBRA_PTM_BFD_VRF_NAME_FIELD
,
906 if (dst_p
.family
== AF_INET6
) {
907 src_p
.family
= stream_getw(s
);
909 if (src_p
.family
== AF_INET
)
910 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
912 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
914 stream_get(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
915 if (src_p
.family
== AF_INET
) {
916 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
,
918 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
919 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
922 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
,
924 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
925 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
930 len
= stream_getc(s
);
931 stream_get(if_name
, s
, len
);
934 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
935 ZEBRA_PTM_BFD_IFNAME_FIELD
, if_name
);
938 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
939 if (IS_ZEBRA_DEBUG_SEND
)
940 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
943 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
947 /* BFD client register */
948 int zebra_ptm_bfd_client_register(struct zserv
*client
, int sock
,
955 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
957 client
->bfd_client_reg_cnt
++;
959 if (IS_ZEBRA_DEBUG_EVENT
)
960 zlog_debug("bfd_client_register msg from client %s: length=%d",
961 zebra_route_string(client
->proto
), length
);
963 if (ptm_cb
.ptm_sock
== -1) {
964 ptm_cb
.t_timer
= NULL
;
965 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
966 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
970 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
972 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_CLIENT_REG_CMD
);
973 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
975 sprintf(tmp_buf
, "%s", zebra_route_string(client
->proto
));
976 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
981 pid
= stream_getl(s
);
982 sprintf(tmp_buf
, "%d", pid
);
983 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
986 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
988 if (IS_ZEBRA_DEBUG_SEND
)
989 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
991 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
993 SET_FLAG(ptm_cb
.client_flags
[client
->proto
],
994 ZEBRA_PTM_BFD_CLIENT_FLAG_REG
);
998 /* BFD client deregister */
999 void zebra_ptm_bfd_client_deregister(int proto
)
1003 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
1005 if (proto
!= ZEBRA_ROUTE_OSPF
&& proto
!= ZEBRA_ROUTE_BGP
1006 && proto
!= ZEBRA_ROUTE_OSPF6
&& proto
!= ZEBRA_ROUTE_PIM
)
1009 if (IS_ZEBRA_DEBUG_EVENT
)
1010 zlog_err("bfd_client_deregister msg for client %s",
1011 zebra_route_string(proto
));
1013 if (ptm_cb
.ptm_sock
== -1) {
1014 ptm_cb
.t_timer
= NULL
;
1015 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
1016 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
1020 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
1022 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_CLIENT_DEREG_CMD
);
1023 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
1025 sprintf(tmp_buf
, "%s", zebra_route_string(proto
));
1026 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
1029 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
1031 if (IS_ZEBRA_DEBUG_SEND
)
1032 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
1035 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
1036 UNSET_FLAG(ptm_cb
.client_flags
[proto
], ZEBRA_PTM_BFD_CLIENT_FLAG_REG
);
1039 int zebra_ptm_get_enable_state(void)
1041 return ptm_cb
.ptm_enable
;
1045 * zebra_ptm_get_status_str - Convert status to a display string.
1047 static const char *zebra_ptm_get_status_str(int status
)
1050 case ZEBRA_PTM_STATUS_DOWN
:
1052 case ZEBRA_PTM_STATUS_UP
:
1054 case ZEBRA_PTM_STATUS_UNKNOWN
:
1060 void zebra_ptm_show_status(struct vty
*vty
, struct interface
*ifp
)
1062 vty_out(vty
, " PTM status: ");
1063 if (ifp
->ptm_enable
) {
1064 vty_out(vty
, "%s\n", zebra_ptm_get_status_str(ifp
->ptm_status
));
1066 vty_out(vty
, "disabled\n");
1070 void zebra_ptm_send_status_req(void)
1073 int len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
1075 if (ptm_cb
.ptm_enable
) {
1076 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
,
1078 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
,
1079 ZEBRA_PTM_GET_STATUS_CMD
);
1080 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &len
);
1082 zebra_ptm_send_message(ptm_cb
.out_data
, len
);
1086 void zebra_ptm_reset_status(int ptm_disable
)
1090 struct interface
*ifp
;
1093 RB_FOREACH(vrf
, vrf_id_head
, &vrfs_by_id
)
1094 for (ALL_LIST_ELEMENTS_RO(vrf
->iflist
, i
, ifp
)) {
1096 if (ifp
->ptm_enable
) {
1097 if (!if_is_operative(ifp
))
1101 ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
1102 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1104 if (if_is_operative(ifp
) && send_linkup
) {
1105 if (IS_ZEBRA_DEBUG_EVENT
)
1107 "%s: Bringing up interface %s",
1108 __func__
, ifp
->name
);
1115 void zebra_ptm_if_init(struct zebra_if
*zebra_ifp
)
1117 zebra_ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_UNSPEC
;
1120 void zebra_ptm_if_set_ptm_state(struct interface
*ifp
,
1121 struct zebra_if
*zebra_ifp
)
1123 if (zebra_ifp
&& zebra_ifp
->ptm_enable
!= ZEBRA_IF_PTM_ENABLE_UNSPEC
)
1124 ifp
->ptm_enable
= zebra_ifp
->ptm_enable
;
1127 void zebra_ptm_if_write(struct vty
*vty
, struct zebra_if
*zebra_ifp
)
1129 if (zebra_ifp
->ptm_enable
== ZEBRA_IF_PTM_ENABLE_OFF
)
1130 vty_out(vty
, " no ptm-enable\n");