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";
103 memset(&ptm_cb
, 0, sizeof(struct zebra_ptm_cb
));
105 ptm_cb
.out_data
= calloc(1, ZEBRA_PTM_SEND_MAX_SOCKBUF
);
106 if (!ptm_cb
.out_data
)
108 zlog_warn("%s: Allocation of send data failed", __func__
);
112 ptm_cb
.in_data
= calloc(1, ZEBRA_PTM_MAX_SOCKBUF
);
115 zlog_warn("%s: Allocation of recv data failed", __func__
);
116 free(ptm_cb
.out_data
);
120 ptm_cb
.pid
= getpid();
121 zebra_ptm_install_commands();
123 sprintf(buf
, "%s", FRR_PTM_NAME
);
124 ptm_hdl
= ptm_lib_register(buf
, NULL
, zebra_ptm_handle_msg_cb
,
125 zebra_ptm_handle_msg_cb
);
126 ptm_cb
.wb
= buffer_new(0);
128 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
130 ptm_cb
.ptm_sock
= -1;
134 zebra_ptm_finish(void)
138 for (proto
= 0; proto
< ZEBRA_ROUTE_MAX
; proto
++)
139 if (CHECK_FLAG(ptm_cb
.client_flags
[proto
], ZEBRA_PTM_BFD_CLIENT_FLAG_REG
))
140 zebra_ptm_bfd_client_deregister(proto
);
142 buffer_flush_all(ptm_cb
.wb
, ptm_cb
.ptm_sock
);
147 free(ptm_cb
.out_data
);
150 free(ptm_cb
.in_data
);
152 /* Release threads. */
154 thread_cancel (ptm_cb
.t_read
);
156 thread_cancel (ptm_cb
.t_write
);
158 thread_cancel (ptm_cb
.t_timer
);
161 buffer_free(ptm_cb
.wb
);
163 if (ptm_cb
.ptm_sock
!= -1)
164 close(ptm_cb
.ptm_sock
);
168 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
))
180 zlog_warn ("%s ptm socket error: %s", __func__
,
181 safe_strerror (errno
));
182 close(ptm_cb
.ptm_sock
);
183 ptm_cb
.ptm_sock
= -1;
184 zebra_ptm_reset_status(0);
185 ptm_cb
.t_timer
= thread_add_timer (zebrad
.master
, zebra_ptm_connect
,
186 NULL
, ptm_cb
.reconnect_time
);
189 ptm_cb
.t_write
= thread_add_write(zebrad
.master
, zebra_ptm_flush_messages
,
190 NULL
, ptm_cb
.ptm_sock
);
200 zebra_ptm_send_message(char *data
, int size
)
203 switch (buffer_write(ptm_cb
.wb
, ptm_cb
.ptm_sock
, data
, size
))
206 zlog_warn ("%s ptm socket error: %s", __func__
, 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
= thread_add_timer (zebrad
.master
, zebra_ptm_connect
,
211 NULL
, ptm_cb
.reconnect_time
);
214 THREAD_OFF(ptm_cb
.t_write
);
217 THREAD_WRITE_ON(zebrad
.master
, ptm_cb
.t_write
,
218 zebra_ptm_flush_messages
, NULL
, ptm_cb
.ptm_sock
);
226 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
= thread_add_read (zebrad
.master
, zebra_ptm_sock_read
,
238 NULL
, ptm_cb
.ptm_sock
);
239 zebra_bfd_peer_replay_req();
241 zebra_ptm_send_status_req();
242 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
243 } else if (ptm_cb
.reconnect_time
< ZEBRA_PTM_RECONNECT_TIME_MAX
){
244 ptm_cb
.reconnect_time
*= 2;
245 if (ptm_cb
.reconnect_time
> ZEBRA_PTM_RECONNECT_TIME_MAX
)
246 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_MAX
;
248 ptm_cb
.t_timer
= thread_add_timer (zebrad
.master
, zebra_ptm_connect
, NULL
,
249 ptm_cb
.reconnect_time
);
250 } else if (ptm_cb
.reconnect_time
>= ZEBRA_PTM_RECONNECT_TIME_MAX
){
251 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
257 DEFUN (zebra_ptm_enable
,
258 zebra_ptm_enable_cmd
,
260 "Enable neighbor check with specified topology\n")
264 struct interface
*ifp
;
265 struct zebra_if
*if_data
;
267 ptm_cb
.ptm_enable
= ZEBRA_IF_PTM_ENABLE_ON
;
269 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
270 for (ALL_LIST_ELEMENTS_RO (vrf
->iflist
, i
, ifp
))
271 if (!ifp
->ptm_enable
)
273 if_data
= (struct zebra_if
*)ifp
->info
;
275 (if_data
->ptm_enable
== ZEBRA_IF_PTM_ENABLE_UNSPEC
))
277 ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_ON
;
279 /* Assign a default unknown status */
280 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
283 zebra_ptm_connect(NULL
);
288 DEFUN (no_zebra_ptm_enable
,
289 no_zebra_ptm_enable_cmd
,
292 "Enable neighbor check with specified topology\n")
294 ptm_cb
.ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
295 zebra_ptm_reset_status(1);
299 DEFUN (zebra_ptm_enable_if
,
300 zebra_ptm_enable_if_cmd
,
302 "Enable neighbor check with specified topology\n")
304 VTY_DECLVAR_CONTEXT (interface
, ifp
);
305 struct zebra_if
*if_data
;
307 int send_linkdown
= 0;
309 if (ifp
->ifindex
== IFINDEX_INTERNAL
)
314 old_ptm_enable
= ifp
->ptm_enable
;
315 ifp
->ptm_enable
= ptm_cb
.ptm_enable
;
317 if (if_is_no_ptm_operative(ifp
))
320 if (!old_ptm_enable
&& ptm_cb
.ptm_enable
)
322 if (!if_is_operative (ifp
) && send_linkdown
)
324 if (IS_ZEBRA_DEBUG_EVENT
)
325 zlog_debug ("%s: Bringing down interface %s\n", __func__
,
332 if_data
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_UNSPEC
;
337 DEFUN (no_zebra_ptm_enable_if
,
338 no_zebra_ptm_enable_if_cmd
,
341 "Enable neighbor check with specified topology\n")
343 VTY_DECLVAR_CONTEXT (interface
, ifp
);
345 struct zebra_if
*if_data
;
347 if ((ifp
->ifindex
!= IFINDEX_INTERNAL
) && (ifp
->ptm_enable
))
349 if (!if_is_operative(ifp
))
352 ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
353 if (if_is_no_ptm_operative (ifp
) && send_linkup
)
355 if (IS_ZEBRA_DEBUG_EVENT
)
356 zlog_debug ("%s: Bringing up interface %s\n", __func__
,
363 if_data
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
370 zebra_ptm_write (struct vty
*vty
)
372 if (ptm_cb
.ptm_enable
)
373 vty_out (vty
, "ptm-enable%s", VTY_NEWLINE
);
379 zebra_ptm_socket_init (void)
383 struct sockaddr_un addr
;
385 ptm_cb
.ptm_sock
= -1;
387 sock
= socket (PF_UNIX
, SOCK_STREAM
, 0);
390 if (set_nonblocking(sock
) < 0)
393 /* Make server socket. */
394 memset (&addr
, 0, sizeof (struct sockaddr_un
));
395 addr
.sun_family
= AF_UNIX
;
396 memcpy (&addr
.sun_path
, ZEBRA_PTM_SOCK_NAME
,
397 sizeof(ZEBRA_PTM_SOCK_NAME
));
399 ret
= connect(sock
, (struct sockaddr
*) &addr
,
400 sizeof (addr
.sun_family
)+sizeof (ZEBRA_PTM_SOCK_NAME
)-1);
403 if (IS_ZEBRA_DEBUG_EVENT
)
404 zlog_debug("%s: Unable to connect to socket %s [%s]",
405 __func__
, ZEBRA_PTM_SOCK_NAME
, safe_strerror(errno
));
409 ptm_cb
.ptm_sock
= sock
;
414 zebra_ptm_install_commands (void)
416 install_element (CONFIG_NODE
, &zebra_ptm_enable_cmd
);
417 install_element (CONFIG_NODE
, &no_zebra_ptm_enable_cmd
);
418 install_element (INTERFACE_NODE
, &zebra_ptm_enable_if_cmd
);
419 install_element (INTERFACE_NODE
, &no_zebra_ptm_enable_if_cmd
);
422 /* BFD session goes down, send message to the protocols. */
424 if_bfd_session_update (struct interface
*ifp
, struct prefix
*dp
,
425 struct prefix
*sp
, int status
, vrf_id_t vrf_id
)
427 if (IS_ZEBRA_DEBUG_EVENT
)
429 char buf
[2][INET6_ADDRSTRLEN
];
433 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d on %s"
435 inet_ntop (dp
->family
, &dp
->u
.prefix
, buf
[0],
436 INET6_ADDRSTRLEN
), dp
->prefixlen
, ifp
->name
,
437 bfd_get_status_str(status
));
441 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d "
442 "with src %s/%d and vrf %d %s event",
443 inet_ntop (dp
->family
, &dp
->u
.prefix
, buf
[0], INET6_ADDRSTRLEN
),
445 inet_ntop (sp
->family
, &sp
->u
.prefix
, buf
[1], INET6_ADDRSTRLEN
),
446 sp
->prefixlen
, vrf_id
, bfd_get_status_str(status
));
450 zebra_interface_bfd_update (ifp
, dp
, sp
, status
, vrf_id
);
454 zebra_ptm_handle_bfd_msg(void *arg
, void *in_ctxt
, struct interface
*ifp
)
460 struct prefix dest_prefix
;
461 struct prefix src_prefix
;
463 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDSTATUS_STR
, bfdst_str
);
465 if (bfdst_str
[0] == '\0') {
469 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDDEST_STR
, dest_str
);
471 if (dest_str
[0] == '\0') {
472 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
473 ZEBRA_PTM_BFDDEST_STR
);
477 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDSRC_STR
, src_str
);
479 if (src_str
[0] == '\0') {
480 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
481 ZEBRA_PTM_BFDSRC_STR
);
485 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDVRF_STR
, vrf_str
);
487 if (vrf_str
[0] == '\0') {
488 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
489 ZEBRA_PTM_BFDVRF_STR
);
493 if (IS_ZEBRA_DEBUG_EVENT
)
494 zlog_debug("%s: Recv Port [%s] bfd status [%s] vrf [%s] peer [%s] local [%s]",
495 __func__
, ifp
? ifp
->name
: "N/A", bfdst_str
,
496 vrf_str
, dest_str
, src_str
);
498 if (str2prefix(dest_str
, &dest_prefix
) == 0) {
499 zlog_err("%s: Peer addr %s not found", __func__
,
504 memset(&src_prefix
, 0, sizeof(struct prefix
));
505 if (strcmp(ZEBRA_PTM_INVALID_SRC_IP
, src_str
)) {
506 if (str2prefix(src_str
, &src_prefix
) == 0) {
507 zlog_err("%s: Local addr %s not found", __func__
,
513 if (!strcmp (bfdst_str
, ZEBRA_PTM_BFDSTATUS_DOWN_STR
)) {
514 if_bfd_session_update(ifp
, &dest_prefix
, &src_prefix
, BFD_STATUS_DOWN
,
515 vrf_name_to_id(vrf_str
));
517 if_bfd_session_update(ifp
, &dest_prefix
, &src_prefix
, BFD_STATUS_UP
,
518 vrf_name_to_id(vrf_str
));
525 zebra_ptm_handle_cbl_msg(void *arg
, void *in_ctxt
, struct interface
*ifp
,
530 if (IS_ZEBRA_DEBUG_EVENT
)
531 zlog_debug("%s: Recv Port [%s] cbl status [%s]", __func__
,
534 if (!strcmp(cbl_str
, ZEBRA_PTM_PASS_STR
) &&
535 (ifp
->ptm_status
!= ZEBRA_PTM_STATUS_UP
)) {
537 if (ifp
->ptm_status
== ZEBRA_PTM_STATUS_DOWN
)
539 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UP
;
540 if (ifp
->ptm_enable
&& if_is_no_ptm_operative (ifp
) && send_linkup
)
542 } else if (!strcmp (cbl_str
, ZEBRA_PTM_FAIL_STR
) &&
543 (ifp
->ptm_status
!= ZEBRA_PTM_STATUS_DOWN
)) {
544 ifp
->ptm_status
= ZEBRA_PTM_STATUS_DOWN
;
545 if (ifp
->ptm_enable
&& if_is_no_ptm_operative (ifp
))
553 * zebra_ptm_handle_msg_cb - The purpose of this callback function is to handle
554 * all the command responses and notifications received from PTM.
556 * Command responses: Upon establishing connection with PTM, Zebra requests
557 * status of all interfaces using 'get-status' command if global ptm-enable
558 * knob is enabled. As a response to the get-status command PTM sends status
559 * of all the interfaces as command responses. All other type of command
560 * responses with cmd_status key word are dropped. The sole purpose of
561 * registering this function as callback for the command responses is to
562 * handle the responses to get-status command.
564 * Notifications: Cable status and BFD session status changes are sent as
565 * notifications by PTM. So, this function is also the callback function for
566 * processing all the notifications from the PTM.
570 zebra_ptm_handle_msg_cb(void *arg
, void *in_ctxt
)
572 struct interface
*ifp
= NULL
;
575 char cmd_status_str
[32];
577 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_CMD_STATUS_STR
, cmd_status_str
);
579 /* Drop command response messages */
580 if (cmd_status_str
[0] != '\0') {
584 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_PORT_STR
, port_str
);
586 if (port_str
[0] == '\0') {
587 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
592 if (strcmp(ZEBRA_PTM_INVALID_PORT_NAME
, port_str
)) {
593 ifp
= if_lookup_by_name_all_vrf(port_str
);
596 zlog_err("%s: %s not found in interface list", __func__
, port_str
);
601 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_CBL_STR
, cbl_str
);
603 if (cbl_str
[0] == '\0') {
604 return zebra_ptm_handle_bfd_msg(arg
, in_ctxt
, ifp
);
607 return zebra_ptm_handle_cbl_msg(arg
, in_ctxt
, ifp
, cbl_str
);
615 zebra_ptm_sock_read (struct thread
*thread
)
621 sock
= THREAD_FD (thread
);
626 /* PTM communicates in CSV format */
628 rc
= ptm_lib_process_msg(ptm_hdl
, sock
, ptm_cb
.in_data
, ZEBRA_PTM_MAX_SOCKBUF
,
635 if (((rc
== 0) && !errno
) || (errno
&& (errno
!= EWOULDBLOCK
) && (errno
!= EAGAIN
))) {
636 zlog_warn ("%s routing socket error: %s(%d) bytes %d", __func__
,
637 safe_strerror (errno
), errno
, rc
);
639 close (ptm_cb
.ptm_sock
);
640 ptm_cb
.ptm_sock
= -1;
641 zebra_ptm_reset_status(0);
642 ptm_cb
.t_timer
= thread_add_timer (zebrad
.master
, zebra_ptm_connect
,
643 NULL
, ptm_cb
.reconnect_time
);
648 ptm_cb
.t_read
= thread_add_read (zebrad
.master
, zebra_ptm_sock_read
,
649 NULL
, ptm_cb
.ptm_sock
);
654 /* BFD peer/dst register/update */
656 zebra_ptm_bfd_dst_register (struct zserv
*client
, int sock
, u_short length
,
657 int command
, struct zebra_vrf
*zvrf
)
663 u_char multi_hop_cnt
;
665 unsigned int min_rx_timer
;
666 unsigned int min_tx_timer
;
667 char if_name
[INTERFACE_NAMSIZ
];
670 char buf
[INET6_ADDRSTRLEN
];
672 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
675 if (command
== ZEBRA_BFD_DEST_UPDATE
)
676 client
->bfd_peer_upd8_cnt
++;
678 client
->bfd_peer_add_cnt
++;
680 if (IS_ZEBRA_DEBUG_EVENT
)
681 zlog_debug("bfd_dst_register msg from client %s: length=%d",
682 zebra_route_string(client
->proto
), length
);
684 if (ptm_cb
.ptm_sock
== -1)
686 ptm_cb
.t_timer
= thread_add_timer (zebrad
.master
, zebra_ptm_connect
,
687 NULL
, ptm_cb
.reconnect_time
);
691 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
692 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_START_CMD
);
693 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
694 sprintf(tmp_buf
, "%s", zebra_route_string(client
->proto
));
695 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
700 pid
= stream_getl(s
);
701 sprintf(tmp_buf
, "%d", pid
);
702 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
, tmp_buf
);
704 dst_p
.family
= stream_getw(s
);
706 if (dst_p
.family
== AF_INET
)
707 dst_p
.prefixlen
= IPV4_MAX_BYTELEN
;
709 dst_p
.prefixlen
= IPV6_MAX_BYTELEN
;
711 stream_get(&dst_p
.u
.prefix
, s
, dst_p
.prefixlen
);
712 if (dst_p
.family
== AF_INET
)
714 inet_ntop(AF_INET
, &dst_p
.u
.prefix4
, buf
, sizeof(buf
));
715 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
719 inet_ntop(AF_INET6
, &dst_p
.u
.prefix6
, buf
, sizeof(buf
));
720 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
723 min_rx_timer
= stream_getl(s
);
724 sprintf(tmp_buf
, "%d", min_rx_timer
);
725 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_MIN_RX_FIELD
,
727 min_tx_timer
= stream_getl(s
);
728 sprintf(tmp_buf
, "%d", min_tx_timer
);
729 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_MIN_TX_FIELD
,
731 detect_mul
= stream_getc(s
);
732 sprintf(tmp_buf
, "%d", detect_mul
);
733 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_DETECT_MULT_FIELD
,
736 multi_hop
= stream_getc(s
);
739 sprintf(tmp_buf
, "%d", 1);
740 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_MULTI_HOP_FIELD
,
742 src_p
.family
= stream_getw(s
);
744 if (src_p
.family
== AF_INET
)
745 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
747 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
749 stream_get(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
750 if (src_p
.family
== AF_INET
)
752 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
, sizeof(buf
));
753 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
754 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
758 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
, sizeof(buf
));
759 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
760 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
763 multi_hop_cnt
= stream_getc(s
);
764 sprintf(tmp_buf
, "%d", multi_hop_cnt
);
765 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD
,
768 if (zvrf_id (zvrf
) != VRF_DEFAULT
)
769 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_VRF_NAME_FIELD
,
774 if (dst_p
.family
== AF_INET6
)
776 src_p
.family
= stream_getw(s
);
778 if (src_p
.family
== AF_INET
)
779 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
781 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
783 stream_get(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
784 if (src_p
.family
== AF_INET
)
786 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
, sizeof(buf
));
787 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
788 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
792 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
, sizeof(buf
));
793 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
794 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
797 len
= stream_getc(s
);
798 stream_get(if_name
, s
, len
);
801 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_IFNAME_FIELD
,
805 sprintf(tmp_buf
, "%d", 1);
806 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEND_EVENT
,
809 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
811 if (IS_ZEBRA_DEBUG_SEND
)
812 zlog_debug ("%s: Sent message (%d) %s", __func__
, data_len
,
814 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
818 /* BFD peer/dst deregister */
820 zebra_ptm_bfd_dst_deregister (struct zserv
*client
, int sock
, u_short length
,
821 struct zebra_vrf
*zvrf
)
827 char if_name
[INTERFACE_NAMSIZ
];
829 char buf
[INET6_ADDRSTRLEN
];
831 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
835 client
->bfd_peer_del_cnt
++;
837 if (IS_ZEBRA_DEBUG_EVENT
)
838 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
839 zebra_route_string(client
->proto
), length
);
841 if (ptm_cb
.ptm_sock
== -1)
843 ptm_cb
.t_timer
= thread_add_timer (zebrad
.master
, zebra_ptm_connect
,
844 NULL
, ptm_cb
.reconnect_time
);
848 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
850 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_STOP_CMD
);
851 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
853 sprintf(tmp_buf
, "%s", zebra_route_string(client
->proto
));
854 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
859 pid
= stream_getl(s
);
860 sprintf(tmp_buf
, "%d", pid
);
861 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
, tmp_buf
);
863 dst_p
.family
= stream_getw(s
);
865 if (dst_p
.family
== AF_INET
)
866 dst_p
.prefixlen
= IPV4_MAX_BYTELEN
;
868 dst_p
.prefixlen
= IPV6_MAX_BYTELEN
;
870 stream_get(&dst_p
.u
.prefix
, s
, dst_p
.prefixlen
);
871 if (dst_p
.family
== AF_INET
)
872 inet_ntop(AF_INET
, &dst_p
.u
.prefix4
, buf
, sizeof(buf
));
874 inet_ntop(AF_INET6
, &dst_p
.u
.prefix6
, buf
, sizeof(buf
));
875 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
878 multi_hop
= stream_getc(s
);
881 sprintf(tmp_buf
, "%d", 1);
882 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_MULTI_HOP_FIELD
,
885 src_p
.family
= stream_getw(s
);
887 if (src_p
.family
== AF_INET
)
888 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
890 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
892 stream_get(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
893 if (src_p
.family
== AF_INET
)
894 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
, sizeof(buf
));
896 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
, sizeof(buf
));
897 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
898 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
900 if (zvrf_id (zvrf
) != VRF_DEFAULT
)
901 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_VRF_NAME_FIELD
,
906 if (dst_p
.family
== AF_INET6
)
908 src_p
.family
= stream_getw(s
);
910 if (src_p
.family
== AF_INET
)
911 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
913 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
915 stream_get(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
916 if (src_p
.family
== AF_INET
)
918 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
, sizeof(buf
));
919 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
920 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
924 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
, sizeof(buf
));
925 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
926 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
930 len
= stream_getc(s
);
931 stream_get(if_name
, s
, len
);
934 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_IFNAME_FIELD
,
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 */
949 zebra_ptm_bfd_client_register (struct zserv
*client
, int sock
, u_short length
)
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 ptm_cb
.t_timer
= thread_add_timer (zebrad
.master
, zebra_ptm_connect
,
966 NULL
, 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
], ZEBRA_PTM_BFD_CLIENT_FLAG_REG
);
997 /* BFD client deregister */
999 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 ptm_cb
.t_timer
= thread_add_timer (zebrad
.master
, zebra_ptm_connect
,
1016 NULL
, 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
);
1040 zebra_ptm_get_enable_state(void)
1042 return ptm_cb
.ptm_enable
;
1046 * zebra_ptm_get_status_str - Convert status to a display string.
1049 zebra_ptm_get_status_str(int status
)
1053 case ZEBRA_PTM_STATUS_DOWN
:
1055 case ZEBRA_PTM_STATUS_UP
:
1057 case ZEBRA_PTM_STATUS_UNKNOWN
:
1064 zebra_ptm_show_status(struct vty
*vty
, struct interface
*ifp
)
1066 vty_out (vty
, " PTM status: ");
1067 if (ifp
->ptm_enable
) {
1068 vty_out (vty
, "%s%s", zebra_ptm_get_status_str (ifp
->ptm_status
),
1071 vty_out (vty
, "disabled%s", VTY_NEWLINE
);
1076 zebra_ptm_send_status_req(void)
1079 int len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
1081 if (ptm_cb
.ptm_enable
)
1083 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
1084 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
,
1085 ZEBRA_PTM_GET_STATUS_CMD
);
1086 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &len
);
1088 zebra_ptm_send_message(ptm_cb
.out_data
, len
);
1093 zebra_ptm_reset_status(int ptm_disable
)
1097 struct interface
*ifp
;
1100 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
1101 for (ALL_LIST_ELEMENTS_RO (vrf
->iflist
, i
, ifp
))
1104 if (ifp
->ptm_enable
)
1106 if (!if_is_operative(ifp
))
1110 ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
1111 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1113 if (if_is_operative (ifp
) && send_linkup
)
1115 if (IS_ZEBRA_DEBUG_EVENT
)
1116 zlog_debug ("%s: Bringing up interface %s", __func__
,
1125 zebra_ptm_if_init(struct zebra_if
*zebra_ifp
)
1127 zebra_ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_UNSPEC
;
1131 zebra_ptm_if_set_ptm_state(struct interface
*ifp
, struct zebra_if
*zebra_ifp
)
1133 if (zebra_ifp
&& zebra_ifp
->ptm_enable
!= ZEBRA_IF_PTM_ENABLE_UNSPEC
)
1134 ifp
->ptm_enable
= zebra_ifp
->ptm_enable
;
1138 zebra_ptm_if_write (struct vty
*vty
, struct zebra_if
*zebra_ifp
)
1140 if (zebra_ifp
->ptm_enable
== ZEBRA_IF_PTM_ENABLE_OFF
)
1141 vty_out (vty
, " no ptm-enable%s", VTY_NEWLINE
);