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"
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";
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
)
107 zlog_warn("%s: Allocation of send data failed", __func__
);
111 ptm_cb
.in_data
= calloc(1, ZEBRA_PTM_MAX_SOCKBUF
);
114 zlog_warn("%s: Allocation of recv data failed", __func__
);
115 free(ptm_cb
.out_data
);
119 ptm_cb
.pid
= getpid();
120 zebra_ptm_install_commands();
122 sprintf(buf
, "%s", "quagga");
123 ptm_hdl
= ptm_lib_register(buf
, NULL
, zebra_ptm_handle_msg_cb
,
124 zebra_ptm_handle_msg_cb
);
125 ptm_cb
.wb
= buffer_new(0);
127 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
129 ptm_cb
.ptm_sock
= -1;
133 zebra_ptm_finish(void)
137 for (proto
= 0; proto
< ZEBRA_ROUTE_MAX
; proto
++)
138 if (CHECK_FLAG(ptm_cb
.client_flags
[proto
], ZEBRA_PTM_BFD_CLIENT_FLAG_REG
))
139 zebra_ptm_bfd_client_deregister(proto
);
141 buffer_flush_all(ptm_cb
.wb
, ptm_cb
.ptm_sock
);
146 free(ptm_cb
.out_data
);
149 free(ptm_cb
.in_data
);
151 /* Release threads. */
153 thread_cancel (ptm_cb
.t_read
);
155 thread_cancel (ptm_cb
.t_write
);
157 thread_cancel (ptm_cb
.t_timer
);
160 buffer_free(ptm_cb
.wb
);
162 if (ptm_cb
.ptm_sock
!= -1)
163 close(ptm_cb
.ptm_sock
);
167 zebra_ptm_flush_messages (struct thread
*thread
)
169 ptm_cb
.t_write
= NULL
;
171 if (ptm_cb
.ptm_sock
== -1)
176 switch (buffer_flush_available(ptm_cb
.wb
, ptm_cb
.ptm_sock
))
179 zlog_warn ("%s ptm socket error: %s", __func__
,
180 safe_strerror (errno
));
181 close(ptm_cb
.ptm_sock
);
182 ptm_cb
.ptm_sock
= -1;
183 zebra_ptm_reset_status(0);
184 ptm_cb
.t_timer
= thread_add_timer (zebrad
.master
, zebra_ptm_connect
,
185 NULL
, ptm_cb
.reconnect_time
);
188 ptm_cb
.t_write
= thread_add_write(zebrad
.master
, zebra_ptm_flush_messages
,
189 NULL
, ptm_cb
.ptm_sock
);
199 zebra_ptm_send_message(char *data
, int size
)
202 switch (buffer_write(ptm_cb
.wb
, ptm_cb
.ptm_sock
, data
, size
))
205 zlog_warn ("%s ptm socket error: %s", __func__
, safe_strerror (errno
));
206 close(ptm_cb
.ptm_sock
);
207 ptm_cb
.ptm_sock
= -1;
208 zebra_ptm_reset_status(0);
209 ptm_cb
.t_timer
= thread_add_timer (zebrad
.master
, zebra_ptm_connect
,
210 NULL
, ptm_cb
.reconnect_time
);
213 THREAD_OFF(ptm_cb
.t_write
);
216 THREAD_WRITE_ON(zebrad
.master
, ptm_cb
.t_write
,
217 zebra_ptm_flush_messages
, NULL
, ptm_cb
.ptm_sock
);
225 zebra_ptm_connect (struct thread
*t
)
229 if (ptm_cb
.ptm_sock
== -1) {
230 zebra_ptm_socket_init();
234 if (ptm_cb
.ptm_sock
!= -1) {
236 ptm_cb
.t_read
= thread_add_read (zebrad
.master
, zebra_ptm_sock_read
,
237 NULL
, ptm_cb
.ptm_sock
);
238 zebra_bfd_peer_replay_req();
240 zebra_ptm_send_status_req();
241 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
242 } else if (ptm_cb
.reconnect_time
< ZEBRA_PTM_RECONNECT_TIME_MAX
){
243 ptm_cb
.reconnect_time
*= 2;
244 if (ptm_cb
.reconnect_time
> ZEBRA_PTM_RECONNECT_TIME_MAX
)
245 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_MAX
;
247 ptm_cb
.t_timer
= 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
)
272 if_data
= (struct zebra_if
*)ifp
->info
;
274 (if_data
->ptm_enable
== ZEBRA_IF_PTM_ENABLE_UNSPEC
))
276 ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_ON
;
278 /* Assign a default unknown status */
279 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
282 zebra_ptm_connect(NULL
);
287 DEFUN (no_zebra_ptm_enable
,
288 no_zebra_ptm_enable_cmd
,
291 "Enable neighbor check with specified topology\n")
293 ptm_cb
.ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
294 zebra_ptm_reset_status(1);
298 DEFUN (zebra_ptm_enable_if
,
299 zebra_ptm_enable_if_cmd
,
301 "Enable neighbor check with specified topology\n")
303 VTY_DECLVAR_CONTEXT (interface
, ifp
);
304 struct zebra_if
*if_data
;
306 int send_linkdown
= 0;
308 if (ifp
->ifindex
== IFINDEX_INTERNAL
)
313 old_ptm_enable
= ifp
->ptm_enable
;
314 ifp
->ptm_enable
= ptm_cb
.ptm_enable
;
316 if (if_is_no_ptm_operative(ifp
))
319 if (!old_ptm_enable
&& ptm_cb
.ptm_enable
)
321 if (!if_is_operative (ifp
) && send_linkdown
)
323 if (IS_ZEBRA_DEBUG_EVENT
)
324 zlog_debug ("%s: Bringing down interface %s\n", __func__
,
331 if_data
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_UNSPEC
;
336 DEFUN (no_zebra_ptm_enable_if
,
337 no_zebra_ptm_enable_if_cmd
,
340 "Enable neighbor check with specified topology\n")
342 VTY_DECLVAR_CONTEXT (interface
, ifp
);
344 struct zebra_if
*if_data
;
346 if ((ifp
->ifindex
!= IFINDEX_INTERNAL
) && (ifp
->ptm_enable
))
348 if (!if_is_operative(ifp
))
351 ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
352 if (if_is_no_ptm_operative (ifp
) && send_linkup
)
354 if (IS_ZEBRA_DEBUG_EVENT
)
355 zlog_debug ("%s: Bringing up interface %s\n", __func__
,
362 if_data
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
369 zebra_ptm_write (struct vty
*vty
)
371 if (ptm_cb
.ptm_enable
)
372 vty_out (vty
, "ptm-enable%s", VTY_NEWLINE
);
378 zebra_ptm_socket_init (void)
382 struct sockaddr_un addr
;
384 ptm_cb
.ptm_sock
= -1;
386 sock
= socket (PF_UNIX
, SOCK_STREAM
, 0);
389 if (set_nonblocking(sock
) < 0)
392 /* Make server socket. */
393 memset (&addr
, 0, sizeof (struct sockaddr_un
));
394 addr
.sun_family
= AF_UNIX
;
395 memcpy (&addr
.sun_path
, ZEBRA_PTM_SOCK_NAME
,
396 sizeof(ZEBRA_PTM_SOCK_NAME
));
398 ret
= connect(sock
, (struct sockaddr
*) &addr
,
399 sizeof (addr
.sun_family
)+sizeof (ZEBRA_PTM_SOCK_NAME
)-1);
402 if (IS_ZEBRA_DEBUG_EVENT
)
403 zlog_debug("%s: Unable to connect to socket %s [%s]",
404 __func__
, ZEBRA_PTM_SOCK_NAME
, safe_strerror(errno
));
408 ptm_cb
.ptm_sock
= sock
;
413 zebra_ptm_install_commands (void)
415 install_element (CONFIG_NODE
, &zebra_ptm_enable_cmd
);
416 install_element (CONFIG_NODE
, &no_zebra_ptm_enable_cmd
);
417 install_element (INTERFACE_NODE
, &zebra_ptm_enable_if_cmd
);
418 install_element (INTERFACE_NODE
, &no_zebra_ptm_enable_if_cmd
);
421 /* BFD session goes down, send message to the protocols. */
423 if_bfd_session_update (struct interface
*ifp
, struct prefix
*dp
,
424 struct prefix
*sp
, int status
, vrf_id_t vrf_id
)
426 if (IS_ZEBRA_DEBUG_EVENT
)
428 char buf
[2][INET6_ADDRSTRLEN
];
432 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d on %s"
434 inet_ntop (dp
->family
, &dp
->u
.prefix
, buf
[0],
435 INET6_ADDRSTRLEN
), dp
->prefixlen
, ifp
->name
,
436 bfd_get_status_str(status
));
440 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d "
441 "with src %s/%d and vrf %d %s event",
442 inet_ntop (dp
->family
, &dp
->u
.prefix
, buf
[0], INET6_ADDRSTRLEN
),
444 inet_ntop (sp
->family
, &sp
->u
.prefix
, buf
[1], INET6_ADDRSTRLEN
),
445 sp
->prefixlen
, vrf_id
, bfd_get_status_str(status
));
449 zebra_interface_bfd_update (ifp
, dp
, sp
, status
, vrf_id
);
453 zebra_ptm_handle_bfd_msg(void *arg
, void *in_ctxt
, struct interface
*ifp
)
459 struct prefix dest_prefix
;
460 struct prefix src_prefix
;
462 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDSTATUS_STR
, bfdst_str
);
464 if (bfdst_str
[0] == '\0') {
468 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDDEST_STR
, dest_str
);
470 if (dest_str
[0] == '\0') {
471 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
472 ZEBRA_PTM_BFDDEST_STR
);
476 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDSRC_STR
, src_str
);
478 if (src_str
[0] == '\0') {
479 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
480 ZEBRA_PTM_BFDSRC_STR
);
484 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDVRF_STR
, vrf_str
);
486 if (vrf_str
[0] == '\0') {
487 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
488 ZEBRA_PTM_BFDVRF_STR
);
492 if (IS_ZEBRA_DEBUG_EVENT
)
493 zlog_debug("%s: Recv Port [%s] bfd status [%s] vrf [%s] peer [%s] local [%s]",
494 __func__
, ifp
? ifp
->name
: "N/A", bfdst_str
,
495 vrf_str
, dest_str
, src_str
);
497 if (str2prefix(dest_str
, &dest_prefix
) == 0) {
498 zlog_err("%s: Peer addr %s not found", __func__
,
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 (bfdst_str
, ZEBRA_PTM_BFDSTATUS_DOWN_STR
)) {
513 if_bfd_session_update(ifp
, &dest_prefix
, &src_prefix
, BFD_STATUS_DOWN
,
514 vrf_name_to_id(vrf_str
));
516 if_bfd_session_update(ifp
, &dest_prefix
, &src_prefix
, BFD_STATUS_UP
,
517 vrf_name_to_id(vrf_str
));
524 zebra_ptm_handle_cbl_msg(void *arg
, void *in_ctxt
, struct interface
*ifp
,
529 if (IS_ZEBRA_DEBUG_EVENT
)
530 zlog_debug("%s: Recv Port [%s] cbl status [%s]", __func__
,
533 if (!strcmp(cbl_str
, ZEBRA_PTM_PASS_STR
) &&
534 (ifp
->ptm_status
!= ZEBRA_PTM_STATUS_UP
)) {
536 if (ifp
->ptm_status
== ZEBRA_PTM_STATUS_DOWN
)
538 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UP
;
539 if (ifp
->ptm_enable
&& if_is_no_ptm_operative (ifp
) && send_linkup
)
541 } else if (!strcmp (cbl_str
, ZEBRA_PTM_FAIL_STR
) &&
542 (ifp
->ptm_status
!= ZEBRA_PTM_STATUS_DOWN
)) {
543 ifp
->ptm_status
= ZEBRA_PTM_STATUS_DOWN
;
544 if (ifp
->ptm_enable
&& if_is_no_ptm_operative (ifp
))
552 * zebra_ptm_handle_msg_cb - The purpose of this callback function is to handle
553 * all the command responses and notifications received from PTM.
555 * Command responses: Upon establishing connection with PTM, Zebra requests
556 * status of all interfaces using 'get-status' command if global ptm-enable
557 * knob is enabled. As a response to the get-status command PTM sends status
558 * of all the interfaces as command responses. All other type of command
559 * responses with cmd_status key word are dropped. The sole purpose of
560 * registering this function as callback for the command responses is to
561 * handle the responses to get-status command.
563 * Notifications: Cable status and BFD session status changes are sent as
564 * notifications by PTM. So, this function is also the callback function for
565 * processing all the notifications from the PTM.
569 zebra_ptm_handle_msg_cb(void *arg
, void *in_ctxt
)
571 struct interface
*ifp
= NULL
;
574 char cmd_status_str
[32];
576 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_CMD_STATUS_STR
, cmd_status_str
);
578 /* Drop command response messages */
579 if (cmd_status_str
[0] != '\0') {
583 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_PORT_STR
, port_str
);
585 if (port_str
[0] == '\0') {
586 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
591 if (strcmp(ZEBRA_PTM_INVALID_PORT_NAME
, port_str
)) {
592 ifp
= if_lookup_by_name_all_vrf(port_str
);
595 zlog_err("%s: %s not found in interface list", __func__
, port_str
);
600 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_CBL_STR
, cbl_str
);
602 if (cbl_str
[0] == '\0') {
603 return zebra_ptm_handle_bfd_msg(arg
, in_ctxt
, ifp
);
606 return zebra_ptm_handle_cbl_msg(arg
, in_ctxt
, ifp
, cbl_str
);
614 zebra_ptm_sock_read (struct thread
*thread
)
620 sock
= THREAD_FD (thread
);
625 /* PTM communicates in CSV format */
627 rc
= ptm_lib_process_msg(ptm_hdl
, sock
, ptm_cb
.in_data
, ZEBRA_PTM_MAX_SOCKBUF
,
634 if (((rc
== 0) && !errno
) || (errno
&& (errno
!= EWOULDBLOCK
) && (errno
!= EAGAIN
))) {
635 zlog_warn ("%s routing socket error: %s(%d) bytes %d", __func__
,
636 safe_strerror (errno
), errno
, rc
);
638 close (ptm_cb
.ptm_sock
);
639 ptm_cb
.ptm_sock
= -1;
640 zebra_ptm_reset_status(0);
641 ptm_cb
.t_timer
= thread_add_timer (zebrad
.master
, zebra_ptm_connect
,
642 NULL
, ptm_cb
.reconnect_time
);
647 ptm_cb
.t_read
= thread_add_read (zebrad
.master
, zebra_ptm_sock_read
,
648 NULL
, ptm_cb
.ptm_sock
);
653 /* BFD peer/dst register/update */
655 zebra_ptm_bfd_dst_register (struct zserv
*client
, int sock
, u_short length
,
656 int command
, struct zebra_vrf
*zvrf
)
662 u_char multi_hop_cnt
;
664 unsigned int min_rx_timer
;
665 unsigned int min_tx_timer
;
666 char if_name
[INTERFACE_NAMSIZ
];
669 char buf
[INET6_ADDRSTRLEN
];
671 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
674 if (command
== ZEBRA_BFD_DEST_UPDATE
)
675 client
->bfd_peer_upd8_cnt
++;
677 client
->bfd_peer_add_cnt
++;
679 if (IS_ZEBRA_DEBUG_EVENT
)
680 zlog_debug("bfd_dst_register msg from client %s: length=%d",
681 zebra_route_string(client
->proto
), length
);
683 if (ptm_cb
.ptm_sock
== -1)
685 ptm_cb
.t_timer
= thread_add_timer (zebrad
.master
, zebra_ptm_connect
,
686 NULL
, ptm_cb
.reconnect_time
);
690 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
691 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_START_CMD
);
692 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
693 sprintf(tmp_buf
, "%s", zebra_route_string(client
->proto
));
694 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
699 pid
= stream_getl(s
);
700 sprintf(tmp_buf
, "%d", pid
);
701 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
, tmp_buf
);
703 dst_p
.family
= stream_getw(s
);
705 if (dst_p
.family
== AF_INET
)
706 dst_p
.prefixlen
= IPV4_MAX_BYTELEN
;
708 dst_p
.prefixlen
= IPV6_MAX_BYTELEN
;
710 stream_get(&dst_p
.u
.prefix
, s
, dst_p
.prefixlen
);
711 if (dst_p
.family
== AF_INET
)
713 inet_ntop(AF_INET
, &dst_p
.u
.prefix4
, buf
, sizeof(buf
));
714 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
);
722 #endif /* HAVE_IPV6 */
724 min_rx_timer
= stream_getl(s
);
725 sprintf(tmp_buf
, "%d", min_rx_timer
);
726 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_MIN_RX_FIELD
,
728 min_tx_timer
= stream_getl(s
);
729 sprintf(tmp_buf
, "%d", min_tx_timer
);
730 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_MIN_TX_FIELD
,
732 detect_mul
= stream_getc(s
);
733 sprintf(tmp_buf
, "%d", detect_mul
);
734 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_DETECT_MULT_FIELD
,
737 multi_hop
= stream_getc(s
);
740 sprintf(tmp_buf
, "%d", 1);
741 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_MULTI_HOP_FIELD
,
743 src_p
.family
= stream_getw(s
);
745 if (src_p
.family
== AF_INET
)
746 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
748 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
750 stream_get(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
751 if (src_p
.family
== AF_INET
)
753 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
, sizeof(buf
));
754 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
755 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
760 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
, sizeof(buf
));
761 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
762 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
764 #endif /* HAVE_IPV6 */
766 multi_hop_cnt
= stream_getc(s
);
767 sprintf(tmp_buf
, "%d", multi_hop_cnt
);
768 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD
,
771 if (zvrf_id (zvrf
) != VRF_DEFAULT
)
772 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_VRF_NAME_FIELD
,
778 if (dst_p
.family
== AF_INET6
)
780 src_p
.family
= stream_getw(s
);
782 if (src_p
.family
== AF_INET
)
783 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
785 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
787 stream_get(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
788 if (src_p
.family
== AF_INET
)
790 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
, sizeof(buf
));
791 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
792 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
796 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
, sizeof(buf
));
797 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
798 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
801 #endif /* HAVE_IPV6 */
802 len
= stream_getc(s
);
803 stream_get(if_name
, s
, len
);
806 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_IFNAME_FIELD
,
810 sprintf(tmp_buf
, "%d", 1);
811 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEND_EVENT
,
814 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
816 if (IS_ZEBRA_DEBUG_SEND
)
817 zlog_debug ("%s: Sent message (%d) %s", __func__
, data_len
,
819 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
823 /* BFD peer/dst deregister */
825 zebra_ptm_bfd_dst_deregister (struct zserv
*client
, int sock
, u_short length
,
826 struct zebra_vrf
*zvrf
)
832 char if_name
[INTERFACE_NAMSIZ
];
834 char buf
[INET6_ADDRSTRLEN
];
836 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
840 client
->bfd_peer_del_cnt
++;
842 if (IS_ZEBRA_DEBUG_EVENT
)
843 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
844 zebra_route_string(client
->proto
), length
);
846 if (ptm_cb
.ptm_sock
== -1)
848 ptm_cb
.t_timer
= thread_add_timer (zebrad
.master
, zebra_ptm_connect
,
849 NULL
, ptm_cb
.reconnect_time
);
853 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
855 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_STOP_CMD
);
856 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
858 sprintf(tmp_buf
, "%s", zebra_route_string(client
->proto
));
859 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
864 pid
= stream_getl(s
);
865 sprintf(tmp_buf
, "%d", pid
);
866 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
, tmp_buf
);
868 dst_p
.family
= stream_getw(s
);
870 if (dst_p
.family
== AF_INET
)
871 dst_p
.prefixlen
= IPV4_MAX_BYTELEN
;
873 dst_p
.prefixlen
= IPV6_MAX_BYTELEN
;
875 stream_get(&dst_p
.u
.prefix
, s
, dst_p
.prefixlen
);
876 if (dst_p
.family
== AF_INET
)
878 inet_ntop(AF_INET
, &dst_p
.u
.prefix4
, buf
, sizeof(buf
));
879 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
884 inet_ntop(AF_INET6
, &dst_p
.u
.prefix6
, buf
, sizeof(buf
));
885 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
887 #endif /* HAVE_IPV6 */
889 multi_hop
= stream_getc(s
);
892 sprintf(tmp_buf
, "%d", 1);
893 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_MULTI_HOP_FIELD
,
896 src_p
.family
= stream_getw(s
);
898 if (src_p
.family
== AF_INET
)
899 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
901 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
903 stream_get(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
904 if (src_p
.family
== AF_INET
)
906 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
, sizeof(buf
));
907 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
908 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
913 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
, sizeof(buf
));
914 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
915 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
917 #endif /* HAVE_IPV6 */
918 if (zvrf_id (zvrf
) != VRF_DEFAULT
)
919 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_VRF_NAME_FIELD
,
925 if (dst_p
.family
== AF_INET6
)
927 src_p
.family
= stream_getw(s
);
929 if (src_p
.family
== AF_INET
)
930 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
932 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
934 stream_get(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
935 if (src_p
.family
== AF_INET
)
937 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
, sizeof(buf
));
938 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
939 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
943 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
, sizeof(buf
));
944 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
945 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
948 #endif /* HAVE_IPV6 */
950 len
= stream_getc(s
);
951 stream_get(if_name
, s
, len
);
954 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_IFNAME_FIELD
,
958 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
959 if (IS_ZEBRA_DEBUG_SEND
)
960 zlog_debug ("%s: Sent message (%d) %s", __func__
, data_len
,
963 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
967 /* BFD client register */
969 zebra_ptm_bfd_client_register (struct zserv
*client
, int sock
, u_short length
)
975 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
977 client
->bfd_client_reg_cnt
++;
979 if (IS_ZEBRA_DEBUG_EVENT
)
980 zlog_debug("bfd_client_register msg from client %s: length=%d",
981 zebra_route_string(client
->proto
), length
);
983 if (ptm_cb
.ptm_sock
== -1)
985 ptm_cb
.t_timer
= thread_add_timer (zebrad
.master
, zebra_ptm_connect
,
986 NULL
, ptm_cb
.reconnect_time
);
990 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
992 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_CLIENT_REG_CMD
);
993 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
995 sprintf(tmp_buf
, "%s", zebra_route_string(client
->proto
));
996 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
1001 pid
= stream_getl(s
);
1002 sprintf(tmp_buf
, "%d", pid
);
1003 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
1006 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
1008 if (IS_ZEBRA_DEBUG_SEND
)
1009 zlog_debug ("%s: Sent message (%d) %s", __func__
, data_len
,
1011 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
1013 SET_FLAG(ptm_cb
.client_flags
[client
->proto
], ZEBRA_PTM_BFD_CLIENT_FLAG_REG
);
1017 /* BFD client deregister */
1019 zebra_ptm_bfd_client_deregister (int proto
)
1023 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
1025 if (proto
!= ZEBRA_ROUTE_OSPF
&& proto
!= ZEBRA_ROUTE_BGP
1026 && proto
!= ZEBRA_ROUTE_OSPF6
)
1029 if (IS_ZEBRA_DEBUG_EVENT
)
1030 zlog_err("bfd_client_deregister msg for client %s",
1031 zebra_route_string(proto
));
1033 if (ptm_cb
.ptm_sock
== -1)
1035 ptm_cb
.t_timer
= thread_add_timer (zebrad
.master
, zebra_ptm_connect
,
1036 NULL
, ptm_cb
.reconnect_time
);
1040 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
1042 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_CLIENT_DEREG_CMD
);
1043 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
1045 sprintf(tmp_buf
, "%s", zebra_route_string(proto
));
1046 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
1049 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
1051 if (IS_ZEBRA_DEBUG_SEND
)
1052 zlog_debug ("%s: Sent message (%d) %s", __func__
, data_len
,
1055 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
1056 UNSET_FLAG(ptm_cb
.client_flags
[proto
], ZEBRA_PTM_BFD_CLIENT_FLAG_REG
);
1060 zebra_ptm_get_enable_state(void)
1062 return ptm_cb
.ptm_enable
;
1066 * zebra_ptm_get_status_str - Convert status to a display string.
1069 zebra_ptm_get_status_str(int status
)
1073 case ZEBRA_PTM_STATUS_DOWN
:
1075 case ZEBRA_PTM_STATUS_UP
:
1077 case ZEBRA_PTM_STATUS_UNKNOWN
:
1084 zebra_ptm_show_status(struct vty
*vty
, struct interface
*ifp
)
1086 vty_out (vty
, " PTM status: ");
1087 if (ifp
->ptm_enable
) {
1088 vty_out (vty
, "%s%s", zebra_ptm_get_status_str (ifp
->ptm_status
),
1091 vty_out (vty
, "disabled%s", VTY_NEWLINE
);
1096 zebra_ptm_send_status_req(void)
1099 int len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
1101 if (ptm_cb
.ptm_enable
)
1103 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
1104 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
,
1105 ZEBRA_PTM_GET_STATUS_CMD
);
1106 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &len
);
1108 zebra_ptm_send_message(ptm_cb
.out_data
, len
);
1113 zebra_ptm_reset_status(int ptm_disable
)
1117 struct interface
*ifp
;
1120 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
1121 for (ALL_LIST_ELEMENTS_RO (vrf
->iflist
, i
, ifp
))
1124 if (ifp
->ptm_enable
)
1126 if (!if_is_operative(ifp
))
1130 ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
1131 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1133 if (if_is_operative (ifp
) && send_linkup
)
1135 if (IS_ZEBRA_DEBUG_EVENT
)
1136 zlog_debug ("%s: Bringing up interface %s", __func__
,
1145 zebra_ptm_if_init(struct zebra_if
*zebra_ifp
)
1147 zebra_ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_UNSPEC
;
1151 zebra_ptm_if_set_ptm_state(struct interface
*ifp
, struct zebra_if
*zebra_ifp
)
1153 if (zebra_ifp
&& zebra_ifp
->ptm_enable
!= ZEBRA_IF_PTM_ENABLE_UNSPEC
)
1154 ifp
->ptm_enable
= zebra_ifp
->ptm_enable
;
1158 zebra_ptm_if_write (struct vty
*vty
, struct zebra_if
*zebra_ifp
)
1160 if (zebra_ifp
->ptm_enable
== ZEBRA_IF_PTM_ENABLE_OFF
)
1161 vty_out (vty
, " no ptm-enable%s", VTY_NEWLINE
);