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
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 #include <sys/un.h> /* for sockaddr_un */
26 #include "zebra/zserv.h"
27 #include "zebra/interface.h"
28 #include "zebra/debug.h"
29 #include "zebra/zebra_ptm.h"
36 #include "zebra/zebra_ptm_redistribute.h"
40 #include "zebra_vrf.h"
43 #define ZEBRA_PTM_RECONNECT_TIME_INITIAL 1 /* initial reconnect is 1s */
44 #define ZEBRA_PTM_RECONNECT_TIME_MAX 300
47 #define PTM_HEADER_LEN 37
49 const char ZEBRA_PTM_GET_STATUS_CMD
[] = "get-status";
50 const char ZEBRA_PTM_BFD_START_CMD
[] = "start-bfd-sess";
51 const char ZEBRA_PTM_BFD_STOP_CMD
[] = "stop-bfd-sess";
52 const char ZEBRA_PTM_BFD_CLIENT_REG_CMD
[] = "reg-bfd-client";
53 const char ZEBRA_PTM_BFD_CLIENT_DEREG_CMD
[] = "dereg-bfd-client";
55 const char ZEBRA_PTM_CMD_STR
[] = "cmd";
56 const char ZEBRA_PTM_CMD_STATUS_STR
[] = "cmd_status";
57 const char ZEBRA_PTM_PORT_STR
[] = "port";
58 const char ZEBRA_PTM_CBL_STR
[] = "cbl status";
59 const char ZEBRA_PTM_PASS_STR
[] = "pass";
60 const char ZEBRA_PTM_FAIL_STR
[] = "fail";
61 const char ZEBRA_PTM_BFDSTATUS_STR
[] = "state";
62 const char ZEBRA_PTM_BFDSTATUS_UP_STR
[] = "Up";
63 const char ZEBRA_PTM_BFDSTATUS_DOWN_STR
[] = "Down";
64 const char ZEBRA_PTM_BFDDEST_STR
[] = "peer";
65 const char ZEBRA_PTM_BFDSRC_STR
[] = "local";
66 const char ZEBRA_PTM_BFDVRF_STR
[] = "vrf";
67 const char ZEBRA_PTM_INVALID_PORT_NAME
[] = "N/A";
68 const char ZEBRA_PTM_INVALID_SRC_IP
[] = "N/A";
69 const char ZEBRA_PTM_INVALID_VRF
[] = "N/A";
71 const char ZEBRA_PTM_BFD_DST_IP_FIELD
[] = "dstIPaddr";
72 const char ZEBRA_PTM_BFD_SRC_IP_FIELD
[] = "srcIPaddr";
73 const char ZEBRA_PTM_BFD_MIN_RX_FIELD
[] = "requiredMinRx";
74 const char ZEBRA_PTM_BFD_MIN_TX_FIELD
[] = "upMinTx";
75 const char ZEBRA_PTM_BFD_DETECT_MULT_FIELD
[] = "detectMult";
76 const char ZEBRA_PTM_BFD_MULTI_HOP_FIELD
[] = "multiHop";
77 const char ZEBRA_PTM_BFD_CLIENT_FIELD
[] = "client";
78 const char ZEBRA_PTM_BFD_SEQID_FIELD
[] = "seqid";
79 const char ZEBRA_PTM_BFD_IFNAME_FIELD
[] = "ifName";
80 const char ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD
[] = "maxHopCnt";
81 const char ZEBRA_PTM_BFD_SEND_EVENT
[] = "sendEvent";
82 const char ZEBRA_PTM_BFD_VRF_NAME_FIELD
[] = "vrfName";
84 static ptm_lib_handle_t
*ptm_hdl
;
86 struct zebra_ptm_cb ptm_cb
;
88 static int zebra_ptm_socket_init(void);
89 int zebra_ptm_sock_read(struct thread
*);
90 static void zebra_ptm_install_commands(void);
91 static int zebra_ptm_handle_msg_cb(void *arg
, void *in_ctxt
);
92 void zebra_bfd_peer_replay_req(void);
93 void zebra_ptm_send_status_req(void);
94 void zebra_ptm_reset_status(int ptm_disable
);
96 const char ZEBRA_PTM_SOCK_NAME
[] = "\0/var/run/ptmd.socket";
98 void zebra_ptm_init(void)
102 memset(&ptm_cb
, 0, sizeof(struct zebra_ptm_cb
));
104 ptm_cb
.out_data
= calloc(1, ZEBRA_PTM_SEND_MAX_SOCKBUF
);
105 if (!ptm_cb
.out_data
) {
106 zlog_warn("%s: Allocation of send data failed", __func__
);
110 ptm_cb
.in_data
= calloc(1, ZEBRA_PTM_MAX_SOCKBUF
);
111 if (!ptm_cb
.in_data
) {
112 zlog_warn("%s: Allocation of recv data failed", __func__
);
113 free(ptm_cb
.out_data
);
117 ptm_cb
.pid
= getpid();
118 zebra_ptm_install_commands();
120 sprintf(buf
, "%s", FRR_PTM_NAME
);
121 ptm_hdl
= ptm_lib_register(buf
, NULL
, zebra_ptm_handle_msg_cb
,
122 zebra_ptm_handle_msg_cb
);
123 ptm_cb
.wb
= buffer_new(0);
125 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
127 ptm_cb
.ptm_sock
= -1;
130 void zebra_ptm_finish(void)
134 for (proto
= 0; proto
< ZEBRA_ROUTE_MAX
; proto
++)
135 if (CHECK_FLAG(ptm_cb
.client_flags
[proto
],
136 ZEBRA_PTM_BFD_CLIENT_FLAG_REG
))
137 zebra_ptm_bfd_client_deregister(proto
);
139 buffer_flush_all(ptm_cb
.wb
, ptm_cb
.ptm_sock
);
144 free(ptm_cb
.out_data
);
147 free(ptm_cb
.in_data
);
149 /* Release threads. */
151 thread_cancel(ptm_cb
.t_read
);
153 thread_cancel(ptm_cb
.t_write
);
155 thread_cancel(ptm_cb
.t_timer
);
158 buffer_free(ptm_cb
.wb
);
160 if (ptm_cb
.ptm_sock
!= -1)
161 close(ptm_cb
.ptm_sock
);
164 static int zebra_ptm_flush_messages(struct thread
*thread
)
166 ptm_cb
.t_write
= NULL
;
168 if (ptm_cb
.ptm_sock
== -1)
173 switch (buffer_flush_available(ptm_cb
.wb
, ptm_cb
.ptm_sock
)) {
175 zlog_warn("%s ptm socket error: %s", __func__
,
176 safe_strerror(errno
));
177 close(ptm_cb
.ptm_sock
);
178 ptm_cb
.ptm_sock
= -1;
179 zebra_ptm_reset_status(0);
181 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
182 ptm_cb
.reconnect_time
);
185 ptm_cb
.t_write
= thread_add_write(zebrad
.master
,
186 zebra_ptm_flush_messages
,
187 NULL
, ptm_cb
.ptm_sock
);
196 static int zebra_ptm_send_message(char *data
, int size
)
199 switch (buffer_write(ptm_cb
.wb
, ptm_cb
.ptm_sock
, data
, size
)) {
201 zlog_warn("%s ptm socket error: %s", __func__
,
202 safe_strerror(errno
));
203 close(ptm_cb
.ptm_sock
);
204 ptm_cb
.ptm_sock
= -1;
205 zebra_ptm_reset_status(0);
207 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
208 ptm_cb
.reconnect_time
);
211 THREAD_OFF(ptm_cb
.t_write
);
214 THREAD_WRITE_ON(zebrad
.master
, ptm_cb
.t_write
,
215 zebra_ptm_flush_messages
, NULL
,
223 int zebra_ptm_connect(struct thread
*t
)
227 if (ptm_cb
.ptm_sock
== -1) {
228 zebra_ptm_socket_init();
232 if (ptm_cb
.ptm_sock
!= -1) {
234 ptm_cb
.t_read
= thread_add_read(zebrad
.master
,
236 NULL
, ptm_cb
.ptm_sock
);
237 zebra_bfd_peer_replay_req();
239 zebra_ptm_send_status_req();
240 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
241 } else if (ptm_cb
.reconnect_time
< ZEBRA_PTM_RECONNECT_TIME_MAX
) {
242 ptm_cb
.reconnect_time
*= 2;
243 if (ptm_cb
.reconnect_time
> ZEBRA_PTM_RECONNECT_TIME_MAX
)
244 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_MAX
;
247 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
248 ptm_cb
.reconnect_time
);
249 } else if (ptm_cb
.reconnect_time
>= ZEBRA_PTM_RECONNECT_TIME_MAX
) {
250 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
256 DEFUN (zebra_ptm_enable
,
257 zebra_ptm_enable_cmd
,
259 "Enable neighbor check with specified topology\n")
263 struct interface
*ifp
;
264 struct zebra_if
*if_data
;
266 ptm_cb
.ptm_enable
= ZEBRA_IF_PTM_ENABLE_ON
;
268 RB_FOREACH(vrf
, vrf_name_head
, &vrfs_by_name
)
269 for (ALL_LIST_ELEMENTS_RO(vrf
->iflist
, i
, ifp
))
270 if (!ifp
->ptm_enable
) {
271 if_data
= (struct zebra_if
*)ifp
->info
;
272 if (if_data
&& (if_data
->ptm_enable
273 == ZEBRA_IF_PTM_ENABLE_UNSPEC
)) {
274 ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_ON
;
276 /* Assign a default unknown status */
277 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
280 zebra_ptm_connect(NULL
);
285 DEFUN (no_zebra_ptm_enable
,
286 no_zebra_ptm_enable_cmd
,
289 "Enable neighbor check with specified topology\n")
291 ptm_cb
.ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
292 zebra_ptm_reset_status(1);
296 DEFUN (zebra_ptm_enable_if
,
297 zebra_ptm_enable_if_cmd
,
299 "Enable neighbor check with specified topology\n")
301 VTY_DECLVAR_CONTEXT(interface
, ifp
);
302 struct zebra_if
*if_data
;
304 int send_linkdown
= 0;
306 if (ifp
->ifindex
== IFINDEX_INTERNAL
) {
310 old_ptm_enable
= ifp
->ptm_enable
;
311 ifp
->ptm_enable
= ptm_cb
.ptm_enable
;
313 if (if_is_no_ptm_operative(ifp
))
316 if (!old_ptm_enable
&& ptm_cb
.ptm_enable
) {
317 if (!if_is_operative(ifp
) && send_linkdown
) {
318 if (IS_ZEBRA_DEBUG_EVENT
)
319 zlog_debug("%s: Bringing down interface %s\n",
320 __func__
, ifp
->name
);
326 if_data
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_UNSPEC
;
331 DEFUN (no_zebra_ptm_enable_if
,
332 no_zebra_ptm_enable_if_cmd
,
335 "Enable neighbor check with specified topology\n")
337 VTY_DECLVAR_CONTEXT(interface
, ifp
);
339 struct zebra_if
*if_data
;
341 if ((ifp
->ifindex
!= IFINDEX_INTERNAL
) && (ifp
->ptm_enable
)) {
342 if (!if_is_operative(ifp
))
345 ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
346 if (if_is_no_ptm_operative(ifp
) && send_linkup
) {
347 if (IS_ZEBRA_DEBUG_EVENT
)
348 zlog_debug("%s: Bringing up interface %s\n",
349 __func__
, ifp
->name
);
355 if_data
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
361 void zebra_ptm_write(struct vty
*vty
)
363 if (ptm_cb
.ptm_enable
)
364 vty_out(vty
, "ptm-enable%s", VTY_NEWLINE
);
369 static int zebra_ptm_socket_init(void)
373 struct sockaddr_un addr
;
375 ptm_cb
.ptm_sock
= -1;
377 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
380 if (set_nonblocking(sock
) < 0) {
381 if (IS_ZEBRA_DEBUG_EVENT
)
382 zlog_debug("%s: Unable to set socket non blocking[%s]",
383 __PRETTY_FUNCTION__
, safe_strerror(errno
));
388 /* Make server socket. */
389 memset(&addr
, 0, sizeof(struct sockaddr_un
));
390 addr
.sun_family
= AF_UNIX
;
391 memcpy(&addr
.sun_path
, ZEBRA_PTM_SOCK_NAME
,
392 sizeof(ZEBRA_PTM_SOCK_NAME
));
394 ret
= connect(sock
, (struct sockaddr
*)&addr
,
395 sizeof(addr
.sun_family
) + sizeof(ZEBRA_PTM_SOCK_NAME
)
398 if (IS_ZEBRA_DEBUG_EVENT
)
399 zlog_debug("%s: Unable to connect to socket %s [%s]",
400 __func__
, ZEBRA_PTM_SOCK_NAME
,
401 safe_strerror(errno
));
405 ptm_cb
.ptm_sock
= sock
;
409 static void zebra_ptm_install_commands(void)
411 install_element(CONFIG_NODE
, &zebra_ptm_enable_cmd
);
412 install_element(CONFIG_NODE
, &no_zebra_ptm_enable_cmd
);
413 install_element(INTERFACE_NODE
, &zebra_ptm_enable_if_cmd
);
414 install_element(INTERFACE_NODE
, &no_zebra_ptm_enable_if_cmd
);
417 /* BFD session goes down, send message to the protocols. */
418 static void if_bfd_session_update(struct interface
*ifp
, struct prefix
*dp
,
419 struct prefix
*sp
, int status
,
422 if (IS_ZEBRA_DEBUG_EVENT
) {
423 char buf
[2][INET6_ADDRSTRLEN
];
427 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d on %s"
429 inet_ntop(dp
->family
, &dp
->u
.prefix
, buf
[0],
431 dp
->prefixlen
, ifp
->name
,
432 bfd_get_status_str(status
));
435 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d "
436 "with src %s/%d and vrf %d %s event",
437 inet_ntop(dp
->family
, &dp
->u
.prefix
, buf
[0],
440 inet_ntop(sp
->family
, &sp
->u
.prefix
, buf
[1],
442 sp
->prefixlen
, vrf_id
,
443 bfd_get_status_str(status
));
447 zebra_interface_bfd_update(ifp
, dp
, sp
, status
, vrf_id
);
450 static int zebra_ptm_handle_bfd_msg(void *arg
, void *in_ctxt
,
451 struct interface
*ifp
)
457 struct prefix dest_prefix
;
458 struct prefix src_prefix
;
461 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDSTATUS_STR
, bfdst_str
);
463 if (bfdst_str
[0] == '\0') {
467 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDDEST_STR
, dest_str
);
469 if (dest_str
[0] == '\0') {
470 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
471 ZEBRA_PTM_BFDDEST_STR
);
475 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDSRC_STR
, src_str
);
477 if (src_str
[0] == '\0') {
478 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
479 ZEBRA_PTM_BFDSRC_STR
);
483 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDVRF_STR
, vrf_str
);
485 if (vrf_str
[0] == '\0') {
486 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
487 ZEBRA_PTM_BFDVRF_STR
);
491 if (IS_ZEBRA_DEBUG_EVENT
)
493 "%s: Recv Port [%s] bfd status [%s] vrf [%s]"
494 " peer [%s] local [%s]",
495 __func__
, ifp
? ifp
->name
: "N/A", bfdst_str
, vrf_str
,
498 if (str2prefix(dest_str
, &dest_prefix
) == 0) {
499 zlog_err("%s: Peer addr %s not found", __func__
, dest_str
);
503 memset(&src_prefix
, 0, sizeof(struct prefix
));
504 if (strcmp(ZEBRA_PTM_INVALID_SRC_IP
, src_str
)) {
505 if (str2prefix(src_str
, &src_prefix
) == 0) {
506 zlog_err("%s: Local addr %s not found", __func__
,
512 if (!strcmp(ZEBRA_PTM_INVALID_VRF
, vrf_str
) && ifp
) {
513 vrf_id
= ifp
->vrf_id
;
515 vrf_id
= vrf_name_to_id(vrf_str
);
518 if (!strcmp(bfdst_str
, ZEBRA_PTM_BFDSTATUS_DOWN_STR
)) {
519 if_bfd_session_update(ifp
, &dest_prefix
, &src_prefix
,
520 BFD_STATUS_DOWN
, vrf_id
);
522 if_bfd_session_update(ifp
, &dest_prefix
, &src_prefix
,
523 BFD_STATUS_UP
, vrf_id
);
529 static int zebra_ptm_handle_cbl_msg(void *arg
, void *in_ctxt
,
530 struct interface
*ifp
, char *cbl_str
)
534 if (IS_ZEBRA_DEBUG_EVENT
)
535 zlog_debug("%s: Recv Port [%s] cbl status [%s]", __func__
,
538 if (!strcmp(cbl_str
, ZEBRA_PTM_PASS_STR
)
539 && (ifp
->ptm_status
!= ZEBRA_PTM_STATUS_UP
)) {
541 if (ifp
->ptm_status
== ZEBRA_PTM_STATUS_DOWN
)
543 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UP
;
544 if (ifp
->ptm_enable
&& if_is_no_ptm_operative(ifp
)
547 } else if (!strcmp(cbl_str
, ZEBRA_PTM_FAIL_STR
)
548 && (ifp
->ptm_status
!= ZEBRA_PTM_STATUS_DOWN
)) {
549 ifp
->ptm_status
= ZEBRA_PTM_STATUS_DOWN
;
550 if (ifp
->ptm_enable
&& if_is_no_ptm_operative(ifp
))
558 * zebra_ptm_handle_msg_cb - The purpose of this callback function is to handle
559 * all the command responses and notifications received from PTM.
561 * Command responses: Upon establishing connection with PTM, Zebra requests
562 * status of all interfaces using 'get-status' command if global ptm-enable
563 * knob is enabled. As a response to the get-status command PTM sends status
564 * of all the interfaces as command responses. All other type of command
565 * responses with cmd_status key word are dropped. The sole purpose of
566 * registering this function as callback for the command responses is to
567 * handle the responses to get-status command.
569 * Notifications: Cable status and BFD session status changes are sent as
570 * notifications by PTM. So, this function is also the callback function for
571 * processing all the notifications from the PTM.
574 static int zebra_ptm_handle_msg_cb(void *arg
, void *in_ctxt
)
576 struct interface
*ifp
= NULL
;
579 char cmd_status_str
[32];
581 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_CMD_STATUS_STR
,
584 /* Drop command response messages */
585 if (cmd_status_str
[0] != '\0') {
589 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_PORT_STR
, port_str
);
591 if (port_str
[0] == '\0') {
592 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
597 if (strcmp(ZEBRA_PTM_INVALID_PORT_NAME
, port_str
)) {
598 ifp
= if_lookup_by_name_all_vrf(port_str
);
601 zlog_err("%s: %s not found in interface list", __func__
,
607 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_CBL_STR
, cbl_str
);
609 if (cbl_str
[0] == '\0') {
610 return zebra_ptm_handle_bfd_msg(arg
, in_ctxt
, ifp
);
613 return zebra_ptm_handle_cbl_msg(arg
, in_ctxt
, ifp
,
621 int zebra_ptm_sock_read(struct thread
*thread
)
627 sock
= THREAD_FD(thread
);
632 /* PTM communicates in CSV format */
634 rc
= ptm_lib_process_msg(ptm_hdl
, sock
, ptm_cb
.in_data
,
635 ZEBRA_PTM_MAX_SOCKBUF
, NULL
);
641 if (((rc
== 0) && !errno
)
642 || (errno
&& (errno
!= EWOULDBLOCK
) && (errno
!= EAGAIN
))) {
643 zlog_warn("%s routing socket error: %s(%d) bytes %d",
644 __func__
, safe_strerror(errno
), errno
, rc
);
646 close(ptm_cb
.ptm_sock
);
647 ptm_cb
.ptm_sock
= -1;
648 zebra_ptm_reset_status(0);
649 ptm_cb
.t_timer
= thread_add_timer(
650 zebrad
.master
, zebra_ptm_connect
, NULL
,
651 ptm_cb
.reconnect_time
);
656 ptm_cb
.t_read
= thread_add_read(zebrad
.master
, zebra_ptm_sock_read
,
657 NULL
, ptm_cb
.ptm_sock
);
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) {
693 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
694 ptm_cb
.reconnect_time
);
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) {
844 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
845 ptm_cb
.reconnect_time
);
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) {
965 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
966 ptm_cb
.reconnect_time
);
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
)
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) {
1015 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
1016 ptm_cb
.reconnect_time
);
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%s", zebra_ptm_get_status_str(ifp
->ptm_status
),
1067 vty_out(vty
, "disabled%s", VTY_NEWLINE
);
1071 void zebra_ptm_send_status_req(void)
1074 int len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
1076 if (ptm_cb
.ptm_enable
) {
1077 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
,
1079 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
,
1080 ZEBRA_PTM_GET_STATUS_CMD
);
1081 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &len
);
1083 zebra_ptm_send_message(ptm_cb
.out_data
, len
);
1087 void zebra_ptm_reset_status(int ptm_disable
)
1091 struct interface
*ifp
;
1094 RB_FOREACH(vrf
, vrf_id_head
, &vrfs_by_id
)
1095 for (ALL_LIST_ELEMENTS_RO(vrf
->iflist
, i
, ifp
)) {
1097 if (ifp
->ptm_enable
) {
1098 if (!if_is_operative(ifp
))
1102 ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
1103 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1105 if (if_is_operative(ifp
) && send_linkup
) {
1106 if (IS_ZEBRA_DEBUG_EVENT
)
1108 "%s: Bringing up interface %s",
1109 __func__
, ifp
->name
);
1116 void zebra_ptm_if_init(struct zebra_if
*zebra_ifp
)
1118 zebra_ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_UNSPEC
;
1121 void zebra_ptm_if_set_ptm_state(struct interface
*ifp
,
1122 struct zebra_if
*zebra_ifp
)
1124 if (zebra_ifp
&& zebra_ifp
->ptm_enable
!= ZEBRA_IF_PTM_ENABLE_UNSPEC
)
1125 ifp
->ptm_enable
= zebra_ifp
->ptm_enable
;
1128 void zebra_ptm_if_write(struct vty
*vty
, struct zebra_if
*zebra_ifp
)
1130 if (zebra_ifp
->ptm_enable
== ZEBRA_IF_PTM_ENABLE_OFF
)
1131 vty_out(vty
, " no ptm-enable%s", VTY_NEWLINE
);