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
23 #include <sys/un.h> /* for sockaddr_un */
38 #include "zebra/debug.h"
39 #include "zebra/interface.h"
40 #include "zebra/zebra_errors.h"
41 #include "zebra/zebra_ptm.h"
42 #include "zebra/zebra_ptm_redistribute.h"
43 #include "zebra/zserv.h"
44 #include "zebra_vrf.h"
47 * Choose the BFD implementation that we'll use.
49 * There are two implementations:
50 * - PTM BFD: which uses an external daemon;
51 * - bfdd: FRR's own BFD daemon;
55 #define ZEBRA_PTM_RECONNECT_TIME_INITIAL 1 /* initial reconnect is 1s */
56 #define ZEBRA_PTM_RECONNECT_TIME_MAX 300
59 #define PTM_HEADER_LEN 37
61 const char ZEBRA_PTM_GET_STATUS_CMD
[] = "get-status";
62 const char ZEBRA_PTM_BFD_START_CMD
[] = "start-bfd-sess";
63 const char ZEBRA_PTM_BFD_STOP_CMD
[] = "stop-bfd-sess";
64 const char ZEBRA_PTM_BFD_CLIENT_REG_CMD
[] = "reg-bfd-client";
65 const char ZEBRA_PTM_BFD_CLIENT_DEREG_CMD
[] = "dereg-bfd-client";
67 const char ZEBRA_PTM_CMD_STR
[] = "cmd";
68 const char ZEBRA_PTM_CMD_STATUS_STR
[] = "cmd_status";
69 const char ZEBRA_PTM_PORT_STR
[] = "port";
70 const char ZEBRA_PTM_CBL_STR
[] = "cbl status";
71 const char ZEBRA_PTM_PASS_STR
[] = "pass";
72 const char ZEBRA_PTM_FAIL_STR
[] = "fail";
73 const char ZEBRA_PTM_BFDSTATUS_STR
[] = "state";
74 const char ZEBRA_PTM_BFDSTATUS_UP_STR
[] = "Up";
75 const char ZEBRA_PTM_BFDSTATUS_DOWN_STR
[] = "Down";
76 const char ZEBRA_PTM_BFDDEST_STR
[] = "peer";
77 const char ZEBRA_PTM_BFDSRC_STR
[] = "local";
78 const char ZEBRA_PTM_BFDVRF_STR
[] = "vrf";
79 const char ZEBRA_PTM_INVALID_PORT_NAME
[] = "N/A";
80 const char ZEBRA_PTM_INVALID_SRC_IP
[] = "N/A";
81 const char ZEBRA_PTM_INVALID_VRF
[] = "N/A";
83 const char ZEBRA_PTM_BFD_DST_IP_FIELD
[] = "dstIPaddr";
84 const char ZEBRA_PTM_BFD_SRC_IP_FIELD
[] = "srcIPaddr";
85 const char ZEBRA_PTM_BFD_MIN_RX_FIELD
[] = "requiredMinRx";
86 const char ZEBRA_PTM_BFD_MIN_TX_FIELD
[] = "upMinTx";
87 const char ZEBRA_PTM_BFD_DETECT_MULT_FIELD
[] = "detectMult";
88 const char ZEBRA_PTM_BFD_MULTI_HOP_FIELD
[] = "multiHop";
89 const char ZEBRA_PTM_BFD_CLIENT_FIELD
[] = "client";
90 const char ZEBRA_PTM_BFD_SEQID_FIELD
[] = "seqid";
91 const char ZEBRA_PTM_BFD_IFNAME_FIELD
[] = "ifName";
92 const char ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD
[] = "maxHopCnt";
93 const char ZEBRA_PTM_BFD_SEND_EVENT
[] = "sendEvent";
94 const char ZEBRA_PTM_BFD_VRF_NAME_FIELD
[] = "vrfName";
96 static ptm_lib_handle_t
*ptm_hdl
;
98 struct zebra_ptm_cb ptm_cb
;
100 static int zebra_ptm_socket_init(void);
101 int zebra_ptm_sock_read(struct thread
*);
102 static void zebra_ptm_install_commands(void);
103 static int zebra_ptm_handle_msg_cb(void *arg
, void *in_ctxt
);
104 void zebra_bfd_peer_replay_req(void);
105 void zebra_ptm_send_status_req(void);
106 void zebra_ptm_reset_status(int ptm_disable
);
107 static int zebra_ptm_bfd_client_deregister(struct zserv
*client
);
109 const char ZEBRA_PTM_SOCK_NAME
[] = "\0/var/run/ptmd.socket";
111 void zebra_ptm_init(void)
115 memset(&ptm_cb
, 0, sizeof(struct zebra_ptm_cb
));
117 ptm_cb
.out_data
= calloc(1, ZEBRA_PTM_SEND_MAX_SOCKBUF
);
118 if (!ptm_cb
.out_data
) {
119 zlog_warn("%s: Allocation of send data failed", __func__
);
123 ptm_cb
.in_data
= calloc(1, ZEBRA_PTM_MAX_SOCKBUF
);
124 if (!ptm_cb
.in_data
) {
125 zlog_warn("%s: Allocation of recv data failed", __func__
);
126 free(ptm_cb
.out_data
);
130 ptm_cb
.pid
= getpid();
131 zebra_ptm_install_commands();
133 sprintf(buf
, "%s", FRR_PTM_NAME
);
134 ptm_hdl
= ptm_lib_register(buf
, NULL
, zebra_ptm_handle_msg_cb
,
135 zebra_ptm_handle_msg_cb
);
136 ptm_cb
.wb
= buffer_new(0);
138 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
140 ptm_cb
.ptm_sock
= -1;
142 hook_register(zserv_client_close
, zebra_ptm_bfd_client_deregister
);
145 void zebra_ptm_finish(void)
147 buffer_flush_all(ptm_cb
.wb
, ptm_cb
.ptm_sock
);
152 free(ptm_cb
.out_data
);
155 free(ptm_cb
.in_data
);
157 /* Release threads. */
159 thread_cancel(ptm_cb
.t_read
);
161 thread_cancel(ptm_cb
.t_write
);
163 thread_cancel(ptm_cb
.t_timer
);
166 buffer_free(ptm_cb
.wb
);
168 if (ptm_cb
.ptm_sock
>= 0)
169 close(ptm_cb
.ptm_sock
);
172 static int zebra_ptm_flush_messages(struct thread
*thread
)
174 ptm_cb
.t_write
= NULL
;
176 if (ptm_cb
.ptm_sock
== -1)
181 switch (buffer_flush_available(ptm_cb
.wb
, ptm_cb
.ptm_sock
)) {
183 zlog_warn("%s ptm socket error: %s", __func__
,
184 safe_strerror(errno
));
185 close(ptm_cb
.ptm_sock
);
186 ptm_cb
.ptm_sock
= -1;
187 zebra_ptm_reset_status(0);
188 ptm_cb
.t_timer
= NULL
;
189 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
190 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
193 ptm_cb
.t_write
= NULL
;
194 thread_add_write(zebrad
.master
, zebra_ptm_flush_messages
, NULL
,
195 ptm_cb
.ptm_sock
, &ptm_cb
.t_write
);
204 static int zebra_ptm_send_message(char *data
, int size
)
207 switch (buffer_write(ptm_cb
.wb
, ptm_cb
.ptm_sock
, data
, size
)) {
209 zlog_warn("%s ptm socket error: %s", __func__
,
210 safe_strerror(errno
));
211 close(ptm_cb
.ptm_sock
);
212 ptm_cb
.ptm_sock
= -1;
213 zebra_ptm_reset_status(0);
214 ptm_cb
.t_timer
= NULL
;
215 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
216 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
219 THREAD_OFF(ptm_cb
.t_write
);
222 thread_add_write(zebrad
.master
, zebra_ptm_flush_messages
, NULL
,
223 ptm_cb
.ptm_sock
, &ptm_cb
.t_write
);
230 int zebra_ptm_connect(struct thread
*t
)
234 if (ptm_cb
.ptm_sock
== -1) {
235 zebra_ptm_socket_init();
239 if (ptm_cb
.ptm_sock
!= -1) {
241 ptm_cb
.t_read
= NULL
;
242 thread_add_read(zebrad
.master
, zebra_ptm_sock_read
,
243 NULL
, ptm_cb
.ptm_sock
, &ptm_cb
.t_read
);
244 zebra_bfd_peer_replay_req();
246 zebra_ptm_send_status_req();
247 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
248 } else if (ptm_cb
.reconnect_time
< ZEBRA_PTM_RECONNECT_TIME_MAX
) {
249 ptm_cb
.reconnect_time
*= 2;
250 if (ptm_cb
.reconnect_time
> ZEBRA_PTM_RECONNECT_TIME_MAX
)
251 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_MAX
;
253 ptm_cb
.t_timer
= NULL
;
254 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
255 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
256 } else if (ptm_cb
.reconnect_time
>= ZEBRA_PTM_RECONNECT_TIME_MAX
) {
257 ptm_cb
.reconnect_time
= ZEBRA_PTM_RECONNECT_TIME_INITIAL
;
263 DEFUN (zebra_ptm_enable
,
264 zebra_ptm_enable_cmd
,
266 "Enable neighbor check with specified topology\n")
269 struct interface
*ifp
;
270 struct zebra_if
*if_data
;
272 ptm_cb
.ptm_enable
= ZEBRA_IF_PTM_ENABLE_ON
;
274 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
)
275 FOR_ALL_INTERFACES (vrf
, ifp
)
276 if (!ifp
->ptm_enable
) {
277 if_data
= (struct zebra_if
*)ifp
->info
;
279 && (if_data
->ptm_enable
280 == ZEBRA_IF_PTM_ENABLE_UNSPEC
)) {
282 ZEBRA_IF_PTM_ENABLE_ON
;
284 /* Assign a default unknown status */
285 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
288 zebra_ptm_connect(NULL
);
293 DEFUN (no_zebra_ptm_enable
,
294 no_zebra_ptm_enable_cmd
,
297 "Enable neighbor check with specified topology\n")
299 ptm_cb
.ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
300 zebra_ptm_reset_status(1);
304 DEFUN (zebra_ptm_enable_if
,
305 zebra_ptm_enable_if_cmd
,
307 "Enable neighbor check with specified topology\n")
309 VTY_DECLVAR_CONTEXT(interface
, ifp
);
310 struct zebra_if
*if_data
;
312 int send_linkdown
= 0;
315 if_data
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_UNSPEC
;
317 if (ifp
->ifindex
== IFINDEX_INTERNAL
) {
321 old_ptm_enable
= ifp
->ptm_enable
;
322 ifp
->ptm_enable
= ptm_cb
.ptm_enable
;
324 if (if_is_no_ptm_operative(ifp
))
327 if (!old_ptm_enable
&& ptm_cb
.ptm_enable
) {
328 if (!if_is_operative(ifp
) && send_linkdown
) {
329 if (IS_ZEBRA_DEBUG_EVENT
)
330 zlog_debug("%s: Bringing down interface %s\n",
331 __func__
, ifp
->name
);
339 DEFUN (no_zebra_ptm_enable_if
,
340 no_zebra_ptm_enable_if_cmd
,
343 "Enable neighbor check with specified topology\n")
345 VTY_DECLVAR_CONTEXT(interface
, ifp
);
347 struct zebra_if
*if_data
;
349 if ((ifp
->ifindex
!= IFINDEX_INTERNAL
) && (ifp
->ptm_enable
)) {
350 if (!if_is_operative(ifp
))
353 ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
354 if (if_is_no_ptm_operative(ifp
) && send_linkup
) {
355 if (IS_ZEBRA_DEBUG_EVENT
)
356 zlog_debug("%s: Bringing up interface %s\n",
357 __func__
, ifp
->name
);
363 if_data
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_OFF
;
369 void zebra_ptm_write(struct vty
*vty
)
371 if (ptm_cb
.ptm_enable
)
372 vty_out(vty
, "ptm-enable\n");
377 static int zebra_ptm_socket_init(void)
381 struct sockaddr_un addr
;
383 ptm_cb
.ptm_sock
= -1;
385 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
388 if (set_nonblocking(sock
) < 0) {
389 if (IS_ZEBRA_DEBUG_EVENT
)
390 zlog_debug("%s: Unable to set socket non blocking[%s]",
391 __PRETTY_FUNCTION__
, safe_strerror(errno
));
396 /* Make server socket. */
397 memset(&addr
, 0, sizeof(struct sockaddr_un
));
398 addr
.sun_family
= AF_UNIX
;
399 memcpy(&addr
.sun_path
, ZEBRA_PTM_SOCK_NAME
,
400 sizeof(ZEBRA_PTM_SOCK_NAME
));
402 ret
= connect(sock
, (struct sockaddr
*)&addr
,
403 sizeof(addr
.sun_family
) + sizeof(ZEBRA_PTM_SOCK_NAME
)
406 if (IS_ZEBRA_DEBUG_EVENT
)
407 zlog_debug("%s: Unable to connect to socket %s [%s]",
408 __func__
, ZEBRA_PTM_SOCK_NAME
,
409 safe_strerror(errno
));
413 ptm_cb
.ptm_sock
= sock
;
417 static void zebra_ptm_install_commands(void)
419 install_element(CONFIG_NODE
, &zebra_ptm_enable_cmd
);
420 install_element(CONFIG_NODE
, &no_zebra_ptm_enable_cmd
);
421 install_element(INTERFACE_NODE
, &zebra_ptm_enable_if_cmd
);
422 install_element(INTERFACE_NODE
, &no_zebra_ptm_enable_if_cmd
);
425 /* BFD session goes down, send message to the protocols. */
426 static void if_bfd_session_update(struct interface
*ifp
, struct prefix
*dp
,
427 struct prefix
*sp
, int status
,
430 if (IS_ZEBRA_DEBUG_EVENT
) {
431 char buf
[2][INET6_ADDRSTRLEN
];
435 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d on %s"
437 inet_ntop(dp
->family
, &dp
->u
.prefix
, buf
[0],
439 dp
->prefixlen
, ifp
->name
,
440 bfd_get_status_str(status
));
443 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d "
444 "with src %s/%d and vrf %u %s event",
445 inet_ntop(dp
->family
, &dp
->u
.prefix
, buf
[0],
448 inet_ntop(sp
->family
, &sp
->u
.prefix
, buf
[1],
450 sp
->prefixlen
, vrf_id
,
451 bfd_get_status_str(status
));
455 zebra_interface_bfd_update(ifp
, dp
, sp
, status
, vrf_id
);
458 static int zebra_ptm_handle_bfd_msg(void *arg
, void *in_ctxt
,
459 struct interface
*ifp
)
465 struct prefix dest_prefix
;
466 struct prefix src_prefix
;
469 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDSTATUS_STR
, bfdst_str
);
471 if (bfdst_str
[0] == '\0') {
475 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDDEST_STR
, dest_str
);
477 if (dest_str
[0] == '\0') {
478 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
479 ZEBRA_PTM_BFDDEST_STR
);
483 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDSRC_STR
, src_str
);
485 if (src_str
[0] == '\0') {
486 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
487 ZEBRA_PTM_BFDSRC_STR
);
491 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_BFDVRF_STR
, vrf_str
);
493 if (vrf_str
[0] == '\0') {
494 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
495 ZEBRA_PTM_BFDVRF_STR
);
499 if (IS_ZEBRA_DEBUG_EVENT
)
501 "%s: Recv Port [%s] bfd status [%s] vrf [%s]"
502 " peer [%s] local [%s]",
503 __func__
, ifp
? ifp
->name
: "N/A", bfdst_str
, vrf_str
,
506 if (str2prefix(dest_str
, &dest_prefix
) == 0) {
507 flog_err(ZEBRA_ERR_PREFIX_PARSE_ERROR
,
508 "%s: Peer addr %s not found", __func__
, dest_str
);
512 memset(&src_prefix
, 0, sizeof(struct prefix
));
513 if (strcmp(ZEBRA_PTM_INVALID_SRC_IP
, src_str
)) {
514 if (str2prefix(src_str
, &src_prefix
) == 0) {
515 flog_err(ZEBRA_ERR_PREFIX_PARSE_ERROR
,
516 "%s: Local addr %s not found", __func__
,
522 if (!strcmp(ZEBRA_PTM_INVALID_VRF
, vrf_str
) && ifp
) {
523 vrf_id
= ifp
->vrf_id
;
525 vrf_id
= vrf_name_to_id(vrf_str
);
528 if (!strcmp(bfdst_str
, ZEBRA_PTM_BFDSTATUS_DOWN_STR
)) {
529 if_bfd_session_update(ifp
, &dest_prefix
, &src_prefix
,
530 BFD_STATUS_DOWN
, vrf_id
);
532 if_bfd_session_update(ifp
, &dest_prefix
, &src_prefix
,
533 BFD_STATUS_UP
, vrf_id
);
539 static int zebra_ptm_handle_cbl_msg(void *arg
, void *in_ctxt
,
540 struct interface
*ifp
, char *cbl_str
)
544 if (IS_ZEBRA_DEBUG_EVENT
)
545 zlog_debug("%s: Recv Port [%s] cbl status [%s]", __func__
,
548 if (!strcmp(cbl_str
, ZEBRA_PTM_PASS_STR
)
549 && (ifp
->ptm_status
!= ZEBRA_PTM_STATUS_UP
)) {
551 if (ifp
->ptm_status
== ZEBRA_PTM_STATUS_DOWN
)
553 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UP
;
554 if (ifp
->ptm_enable
&& if_is_no_ptm_operative(ifp
)
557 } else if (!strcmp(cbl_str
, ZEBRA_PTM_FAIL_STR
)
558 && (ifp
->ptm_status
!= ZEBRA_PTM_STATUS_DOWN
)) {
559 ifp
->ptm_status
= ZEBRA_PTM_STATUS_DOWN
;
560 if (ifp
->ptm_enable
&& if_is_no_ptm_operative(ifp
))
568 * zebra_ptm_handle_msg_cb - The purpose of this callback function is to handle
569 * all the command responses and notifications received from PTM.
571 * Command responses: Upon establishing connection with PTM, Zebra requests
572 * status of all interfaces using 'get-status' command if global ptm-enable
573 * knob is enabled. As a response to the get-status command PTM sends status
574 * of all the interfaces as command responses. All other type of command
575 * responses with cmd_status key word are dropped. The sole purpose of
576 * registering this function as callback for the command responses is to
577 * handle the responses to get-status command.
579 * Notifications: Cable status and BFD session status changes are sent as
580 * notifications by PTM. So, this function is also the callback function for
581 * processing all the notifications from the PTM.
584 static int zebra_ptm_handle_msg_cb(void *arg
, void *in_ctxt
)
586 struct interface
*ifp
= NULL
;
589 char cmd_status_str
[32];
591 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_CMD_STATUS_STR
,
594 /* Drop command response messages */
595 if (cmd_status_str
[0] != '\0') {
599 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_PORT_STR
, port_str
);
601 if (port_str
[0] == '\0') {
602 zlog_debug("%s: Key %s not found in PTM msg", __func__
,
607 if (strcmp(ZEBRA_PTM_INVALID_PORT_NAME
, port_str
)) {
608 ifp
= if_lookup_by_name_all_vrf(port_str
);
611 zlog_warn("%s: %s not found in interface list",
617 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_CBL_STR
, cbl_str
);
619 if (cbl_str
[0] == '\0') {
620 return zebra_ptm_handle_bfd_msg(arg
, in_ctxt
, ifp
);
623 return zebra_ptm_handle_cbl_msg(arg
, in_ctxt
, ifp
,
631 int zebra_ptm_sock_read(struct thread
*thread
)
637 sock
= THREAD_FD(thread
);
642 /* PTM communicates in CSV format */
644 rc
= ptm_lib_process_msg(ptm_hdl
, sock
, ptm_cb
.in_data
,
645 ZEBRA_PTM_MAX_SOCKBUF
, NULL
);
648 if (((rc
== 0) && !errno
)
649 || (errno
&& (errno
!= EWOULDBLOCK
) && (errno
!= EAGAIN
))) {
650 zlog_warn("%s routing socket error: %s(%d) bytes %d",
651 __func__
, safe_strerror(errno
), errno
, rc
);
653 close(ptm_cb
.ptm_sock
);
654 ptm_cb
.ptm_sock
= -1;
655 zebra_ptm_reset_status(0);
656 ptm_cb
.t_timer
= NULL
;
657 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
658 ptm_cb
.reconnect_time
,
663 ptm_cb
.t_read
= NULL
;
664 thread_add_read(zebrad
.master
, zebra_ptm_sock_read
, NULL
,
665 ptm_cb
.ptm_sock
, &ptm_cb
.t_read
);
670 /* BFD peer/dst register/update */
671 void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS
)
677 uint8_t multi_hop_cnt
;
679 unsigned int min_rx_timer
;
680 unsigned int min_tx_timer
;
681 char if_name
[INTERFACE_NAMSIZ
];
684 char buf
[INET6_ADDRSTRLEN
];
686 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
689 if (hdr
->command
== ZEBRA_BFD_DEST_UPDATE
)
690 client
->bfd_peer_upd8_cnt
++;
692 client
->bfd_peer_add_cnt
++;
694 if (IS_ZEBRA_DEBUG_EVENT
)
695 zlog_debug("bfd_dst_register msg from client %s: length=%d",
696 zebra_route_string(client
->proto
), hdr
->length
);
698 if (ptm_cb
.ptm_sock
== -1) {
699 ptm_cb
.t_timer
= NULL
;
700 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
701 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
705 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
706 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_START_CMD
);
707 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
708 sprintf(tmp_buf
, "%s", zebra_route_string(client
->proto
));
709 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
715 sprintf(tmp_buf
, "%d", pid
);
716 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
719 STREAM_GETW(s
, dst_p
.family
);
721 if (dst_p
.family
== AF_INET
)
722 dst_p
.prefixlen
= IPV4_MAX_BYTELEN
;
724 dst_p
.prefixlen
= IPV6_MAX_BYTELEN
;
726 STREAM_GET(&dst_p
.u
.prefix
, s
, dst_p
.prefixlen
);
727 if (dst_p
.family
== AF_INET
) {
728 inet_ntop(AF_INET
, &dst_p
.u
.prefix4
, buf
, sizeof(buf
));
729 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
730 ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
732 inet_ntop(AF_INET6
, &dst_p
.u
.prefix6
, buf
, sizeof(buf
));
733 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
734 ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
737 STREAM_GETL(s
, min_rx_timer
);
738 sprintf(tmp_buf
, "%d", min_rx_timer
);
739 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_MIN_RX_FIELD
,
741 STREAM_GETL(s
, min_tx_timer
);
742 sprintf(tmp_buf
, "%d", min_tx_timer
);
743 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_MIN_TX_FIELD
,
745 STREAM_GETC(s
, detect_mul
);
746 sprintf(tmp_buf
, "%d", detect_mul
);
747 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_DETECT_MULT_FIELD
,
750 STREAM_GETC(s
, multi_hop
);
752 sprintf(tmp_buf
, "%d", 1);
753 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
754 ZEBRA_PTM_BFD_MULTI_HOP_FIELD
, tmp_buf
);
755 STREAM_GETW(s
, src_p
.family
);
757 if (src_p
.family
== AF_INET
)
758 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
760 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
762 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
763 if (src_p
.family
== AF_INET
) {
764 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
, sizeof(buf
));
765 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
766 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
768 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
, sizeof(buf
));
769 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
770 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
773 STREAM_GETC(s
, multi_hop_cnt
);
774 sprintf(tmp_buf
, "%d", multi_hop_cnt
);
775 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
776 ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD
, tmp_buf
);
778 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
779 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
780 ZEBRA_PTM_BFD_VRF_NAME_FIELD
,
783 if (dst_p
.family
== AF_INET6
) {
784 STREAM_GETW(s
, src_p
.family
);
786 if (src_p
.family
== AF_INET
)
787 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
789 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
791 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
792 if (src_p
.family
== AF_INET
) {
793 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
,
795 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
796 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
799 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
,
801 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
802 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
807 STREAM_GET(if_name
, s
, len
);
810 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
811 ZEBRA_PTM_BFD_IFNAME_FIELD
, if_name
);
814 sprintf(tmp_buf
, "%d", 1);
815 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEND_EVENT
,
818 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
820 if (IS_ZEBRA_DEBUG_SEND
)
821 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
823 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
828 ptm_lib_cleanup_msg(ptm_hdl
, out_ctxt
);
831 /* BFD peer/dst deregister */
832 void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS
)
838 char if_name
[INTERFACE_NAMSIZ
];
840 char buf
[INET6_ADDRSTRLEN
];
842 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
846 client
->bfd_peer_del_cnt
++;
848 if (IS_ZEBRA_DEBUG_EVENT
)
849 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
850 zebra_route_string(client
->proto
), hdr
->length
);
852 if (ptm_cb
.ptm_sock
== -1) {
853 ptm_cb
.t_timer
= NULL
;
854 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
855 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
859 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
861 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_STOP_CMD
);
862 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
864 sprintf(tmp_buf
, "%s", zebra_route_string(client
->proto
));
865 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
871 sprintf(tmp_buf
, "%d", pid
);
872 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
875 STREAM_GETW(s
, dst_p
.family
);
877 if (dst_p
.family
== AF_INET
)
878 dst_p
.prefixlen
= IPV4_MAX_BYTELEN
;
880 dst_p
.prefixlen
= IPV6_MAX_BYTELEN
;
882 STREAM_GET(&dst_p
.u
.prefix
, s
, dst_p
.prefixlen
);
883 if (dst_p
.family
== AF_INET
)
884 inet_ntop(AF_INET
, &dst_p
.u
.prefix4
, buf
, sizeof(buf
));
886 inet_ntop(AF_INET6
, &dst_p
.u
.prefix6
, buf
, sizeof(buf
));
887 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
890 STREAM_GETC(s
, multi_hop
);
892 sprintf(tmp_buf
, "%d", 1);
893 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
894 ZEBRA_PTM_BFD_MULTI_HOP_FIELD
, tmp_buf
);
896 STREAM_GETW(s
, src_p
.family
);
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
)
905 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
, sizeof(buf
));
907 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
, sizeof(buf
));
908 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
909 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
911 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
912 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
913 ZEBRA_PTM_BFD_VRF_NAME_FIELD
,
916 if (dst_p
.family
== AF_INET6
) {
917 STREAM_GETW(s
, src_p
.family
);
919 if (src_p
.family
== AF_INET
)
920 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
922 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
924 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
925 if (src_p
.family
== AF_INET
) {
926 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
,
928 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
929 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
932 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
,
934 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
935 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
941 STREAM_GET(if_name
, s
, len
);
944 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
945 ZEBRA_PTM_BFD_IFNAME_FIELD
, if_name
);
948 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
949 if (IS_ZEBRA_DEBUG_SEND
)
950 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
953 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
958 ptm_lib_cleanup_msg(ptm_hdl
, out_ctxt
);
961 /* BFD client register */
962 void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS
)
966 void *out_ctxt
= NULL
;
968 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
970 client
->bfd_client_reg_cnt
++;
972 if (IS_ZEBRA_DEBUG_EVENT
)
973 zlog_debug("bfd_client_register msg from client %s: length=%d",
974 zebra_route_string(client
->proto
), hdr
->length
);
979 if (ptm_cb
.ptm_sock
== -1) {
980 ptm_cb
.t_timer
= NULL
;
981 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
982 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
986 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
988 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_CLIENT_REG_CMD
);
989 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
991 sprintf(tmp_buf
, "%s", zebra_route_string(client
->proto
));
992 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
995 sprintf(tmp_buf
, "%d", pid
);
996 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
999 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
1001 if (IS_ZEBRA_DEBUG_SEND
)
1002 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
1004 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
1006 SET_FLAG(ptm_cb
.client_flags
[client
->proto
],
1007 ZEBRA_PTM_BFD_CLIENT_FLAG_REG
);
1013 * IF we ever add more STREAM_GETXXX functions after the out_ctxt
1014 * is allocated then we need to add this code back in
1017 * ptm_lib_cleanup_msg(ptm_hdl, out_ctxt);
1022 /* BFD client deregister */
1023 int zebra_ptm_bfd_client_deregister(struct zserv
*client
)
1025 uint8_t proto
= client
->proto
;
1028 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
1030 if (proto
!= ZEBRA_ROUTE_OSPF
&& proto
!= ZEBRA_ROUTE_BGP
1031 && proto
!= ZEBRA_ROUTE_OSPF6
&& proto
!= ZEBRA_ROUTE_PIM
)
1034 if (IS_ZEBRA_DEBUG_EVENT
)
1035 zlog_warn("bfd_client_deregister msg for client %s",
1036 zebra_route_string(proto
));
1038 if (ptm_cb
.ptm_sock
== -1) {
1039 ptm_cb
.t_timer
= NULL
;
1040 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
1041 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
1045 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
1047 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_CLIENT_DEREG_CMD
);
1048 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
1050 sprintf(tmp_buf
, "%s", zebra_route_string(proto
));
1051 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
1054 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
1056 if (IS_ZEBRA_DEBUG_SEND
)
1057 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
1060 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
1061 UNSET_FLAG(ptm_cb
.client_flags
[proto
], ZEBRA_PTM_BFD_CLIENT_FLAG_REG
);
1066 int zebra_ptm_get_enable_state(void)
1068 return ptm_cb
.ptm_enable
;
1072 * zebra_ptm_get_status_str - Convert status to a display string.
1074 static const char *zebra_ptm_get_status_str(int status
)
1077 case ZEBRA_PTM_STATUS_DOWN
:
1079 case ZEBRA_PTM_STATUS_UP
:
1081 case ZEBRA_PTM_STATUS_UNKNOWN
:
1087 void zebra_ptm_show_status(struct vty
*vty
, struct interface
*ifp
)
1089 vty_out(vty
, " PTM status: ");
1090 if (ifp
->ptm_enable
) {
1091 vty_out(vty
, "%s\n", zebra_ptm_get_status_str(ifp
->ptm_status
));
1093 vty_out(vty
, "disabled\n");
1097 void zebra_ptm_send_status_req(void)
1100 int len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
1102 if (ptm_cb
.ptm_enable
) {
1103 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
,
1105 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
,
1106 ZEBRA_PTM_GET_STATUS_CMD
);
1107 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &len
);
1109 zebra_ptm_send_message(ptm_cb
.out_data
, len
);
1113 void zebra_ptm_reset_status(int ptm_disable
)
1116 struct interface
*ifp
;
1119 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
1120 FOR_ALL_INTERFACES (vrf
, ifp
) {
1122 if (ifp
->ptm_enable
) {
1123 if (!if_is_operative(ifp
))
1128 ZEBRA_IF_PTM_ENABLE_OFF
;
1129 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1131 if (if_is_operative(ifp
) && send_linkup
) {
1132 if (IS_ZEBRA_DEBUG_EVENT
)
1134 "%s: Bringing up interface %s",
1135 __func__
, ifp
->name
);
1142 void zebra_ptm_if_init(struct zebra_if
*zebra_ifp
)
1144 zebra_ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_UNSPEC
;
1147 void zebra_ptm_if_set_ptm_state(struct interface
*ifp
,
1148 struct zebra_if
*zebra_ifp
)
1150 if (zebra_ifp
&& zebra_ifp
->ptm_enable
!= ZEBRA_IF_PTM_ENABLE_UNSPEC
)
1151 ifp
->ptm_enable
= zebra_ifp
->ptm_enable
;
1154 void zebra_ptm_if_write(struct vty
*vty
, struct zebra_if
*zebra_ifp
)
1156 if (zebra_ifp
->ptm_enable
== ZEBRA_IF_PTM_ENABLE_OFF
)
1157 vty_out(vty
, " no ptm-enable\n");
1160 #else /* HAVE_BFDD */
1162 #include "zebra/zebra_memory.h"
1167 struct ptm_process
{
1168 struct zserv
*pp_zs
;
1171 TAILQ_ENTRY(ptm_process
) pp_entry
;
1173 TAILQ_HEAD(ppqueue
, ptm_process
) ppqueue
;
1175 DEFINE_MTYPE_STATIC(ZEBRA
, ZEBRA_PTM_BFD_PROCESS
,
1176 "PTM BFD process registration table.");
1181 static struct ptm_process
*pp_new(pid_t pid
, struct zserv
*zs
);
1182 static struct ptm_process
*pp_lookup_byzs(struct zserv
*zs
);
1183 static void pp_free(struct ptm_process
*pp
);
1184 static void pp_free_all(void);
1186 static void zebra_ptm_send_bfdd(struct stream
*msg
);
1187 static void zebra_ptm_send_clients(struct stream
*msg
);
1188 static int _zebra_ptm_bfd_client_deregister(struct zserv
*zs
);
1189 static void _zebra_ptm_reroute(struct zserv
*zs
, struct stream
*msg
,
1194 * Process PID registration.
1196 static struct ptm_process
*pp_new(pid_t pid
, struct zserv
*zs
)
1198 struct ptm_process
*pp
;
1201 /* Sanity check: more than one client can't have the same PID. */
1202 TAILQ_FOREACH(pp
, &ppqueue
, pp_entry
) {
1203 if (pp
->pp_pid
== pid
&& pp
->pp_zs
!= zs
)
1204 zlog_err("%s:%d pid and client pointer doesn't match",
1205 __FILE__
, __LINE__
);
1207 #endif /* PTM_DEBUG */
1209 /* Lookup for duplicates. */
1210 pp
= pp_lookup_byzs(zs
);
1214 /* Allocate and register new process. */
1215 pp
= XCALLOC(MTYPE_ZEBRA_PTM_BFD_PROCESS
, sizeof(*pp
));
1221 TAILQ_INSERT_HEAD(&ppqueue
, pp
, pp_entry
);
1226 static struct ptm_process
*pp_lookup_byzs(struct zserv
*zs
)
1228 struct ptm_process
*pp
;
1230 TAILQ_FOREACH(pp
, &ppqueue
, pp_entry
) {
1231 if (pp
->pp_zs
!= zs
)
1240 static void pp_free(struct ptm_process
*pp
)
1245 TAILQ_REMOVE(&ppqueue
, pp
, pp_entry
);
1246 XFREE(MTYPE_ZEBRA_PTM_BFD_PROCESS
, pp
);
1249 static void pp_free_all(void)
1251 struct ptm_process
*pp
;
1253 while (!TAILQ_EMPTY(&ppqueue
)) {
1254 pp
= TAILQ_FIRST(&ppqueue
);
1261 * Use the FRR's internal daemon implementation.
1263 static void zebra_ptm_send_bfdd(struct stream
*msg
)
1265 struct listnode
*node
;
1266 struct zserv
*client
;
1267 struct stream
*msgc
;
1269 /* Create copy for replication. */
1270 msgc
= stream_dup(msg
);
1272 zlog_warn("%s: not enough memory", __func__
);
1276 /* Send message to all running BFDd daemons. */
1277 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
1278 if (client
->proto
!= ZEBRA_ROUTE_BFD
)
1281 zserv_send_message(client
, msg
);
1283 /* Allocate more messages. */
1284 msg
= stream_dup(msgc
);
1286 zlog_warn("%s: not enough memory", __func__
);
1295 static void zebra_ptm_send_clients(struct stream
*msg
)
1297 struct listnode
*node
;
1298 struct zserv
*client
;
1299 struct stream
*msgc
;
1301 /* Create copy for replication. */
1302 msgc
= stream_dup(msg
);
1304 zlog_warn("%s: not enough memory", __func__
);
1308 /* Send message to all running client daemons. */
1309 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
1310 switch (client
->proto
) {
1311 case ZEBRA_ROUTE_BGP
:
1312 case ZEBRA_ROUTE_OSPF
:
1313 case ZEBRA_ROUTE_OSPF6
:
1314 case ZEBRA_ROUTE_PIM
:
1318 /* NOTHING: skip this daemon. */
1322 zserv_send_message(client
, msg
);
1324 /* Allocate more messages. */
1325 msg
= stream_dup(msgc
);
1327 zlog_warn("%s: not enough memory", __func__
);
1336 static int _zebra_ptm_bfd_client_deregister(struct zserv
*zs
)
1339 struct ptm_process
*pp
;
1341 /* Filter daemons that must receive this treatment. */
1342 switch (zs
->proto
) {
1343 case ZEBRA_ROUTE_BGP
:
1344 case ZEBRA_ROUTE_OSPF
:
1345 case ZEBRA_ROUTE_OSPF6
:
1346 case ZEBRA_ROUTE_PIM
:
1349 case ZEBRA_ROUTE_BFD
:
1350 /* Don't try to send BFDd messages to itself. */
1354 /* Unsupported daemon. */
1358 /* Find daemon pid by zebra connection pointer. */
1359 pp
= pp_lookup_byzs(zs
);
1361 zlog_err("%s:%d failed to find process pid registration",
1362 __FILE__
, __LINE__
);
1366 /* Generate, send message and free() daemon related data. */
1367 msg
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1369 zlog_warn("%s: not enough memory", __func__
);
1374 * The message type will be BFD_DEST_REPLY so we can use only
1375 * one callback at the `bfdd` side, however the real command
1376 * number will be included right after the zebra header.
1378 zclient_create_header(msg
, ZEBRA_BFD_DEST_REPLAY
, 0);
1379 stream_putl(msg
, ZEBRA_BFD_CLIENT_DEREGISTER
);
1381 /* Put process PID. */
1382 stream_putl(msg
, pp
->pp_pid
);
1384 /* Update the data pointers. */
1385 stream_putw_at(msg
, 0, stream_get_endp(msg
));
1387 zebra_ptm_send_bfdd(msg
);
1394 void zebra_ptm_init(void)
1396 /* Initialize the ptm process information list. */
1397 TAILQ_INIT(&ppqueue
);
1400 * Send deregistration messages to BFD daemon when some other
1401 * daemon closes. This will help avoid sending daemons
1402 * unnecessary notification messages.
1404 hook_register(zserv_client_close
, _zebra_ptm_bfd_client_deregister
);
1407 void zebra_ptm_finish(void)
1409 /* Remove the client disconnect hook and free all memory. */
1410 hook_unregister(zserv_client_close
, _zebra_ptm_bfd_client_deregister
);
1418 static void _zebra_ptm_reroute(struct zserv
*zs
, struct stream
*msg
,
1421 struct stream
*msgc
;
1422 size_t zmsglen
, zhdrlen
;
1426 * Don't modify message in the zebra API. In order to do that we
1427 * need to allocate a new message stream and copy the message
1428 * provided by zebra.
1430 msgc
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1432 zlog_warn("%s: not enough memory", __func__
);
1436 /* Calculate our header size plus the message contents. */
1437 zhdrlen
= ZEBRA_HEADER_SIZE
+ sizeof(uint32_t);
1438 zmsglen
= msg
->endp
- msg
->getp
;
1439 memcpy(msgc
->data
+ zhdrlen
, msg
->data
+ msg
->getp
, zmsglen
);
1442 * The message type will be BFD_DEST_REPLY so we can use only
1443 * one callback at the `bfdd` side, however the real command
1444 * number will be included right after the zebra header.
1446 zclient_create_header(msgc
, ZEBRA_BFD_DEST_REPLAY
, 0);
1447 stream_putl(msgc
, command
);
1449 /* Update the data pointers. */
1451 msgc
->endp
= zhdrlen
+ zmsglen
;
1452 stream_putw_at(msgc
, 0, stream_get_endp(msgc
));
1454 zebra_ptm_send_bfdd(msgc
);
1456 /* Registrate process PID for shutdown hook. */
1457 STREAM_GETL(msg
, ppid
);
1463 zlog_err("%s:%d failed to registrate client pid", __FILE__
, __LINE__
);
1466 void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS
)
1468 if (IS_ZEBRA_DEBUG_EVENT
)
1469 zlog_debug("bfd_dst_register msg from client %s: length=%d",
1470 zebra_route_string(client
->proto
), hdr
->length
);
1472 _zebra_ptm_reroute(client
, msg
, ZEBRA_BFD_DEST_REGISTER
);
1475 void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS
)
1477 if (IS_ZEBRA_DEBUG_EVENT
)
1478 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
1479 zebra_route_string(client
->proto
), hdr
->length
);
1481 _zebra_ptm_reroute(client
, msg
, ZEBRA_BFD_DEST_DEREGISTER
);
1484 void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS
)
1486 if (IS_ZEBRA_DEBUG_EVENT
)
1487 zlog_debug("bfd_client_register msg from client %s: length=%d",
1488 zebra_route_string(client
->proto
), hdr
->length
);
1490 _zebra_ptm_reroute(client
, msg
, ZEBRA_BFD_CLIENT_REGISTER
);
1493 void zebra_ptm_bfd_dst_replay(ZAPI_HANDLER_ARGS
)
1495 struct stream
*msgc
;
1496 size_t zmsglen
, zhdrlen
;
1501 * Replay messages with HAVE_BFDD are meant to be replayed to
1502 * the client daemons. These messages are composed and
1503 * originated from the `bfdd` daemon.
1505 if (IS_ZEBRA_DEBUG_EVENT
)
1506 zlog_debug("bfd_dst_update msg from client %s: length=%d",
1507 zebra_route_string(client
->proto
), hdr
->length
);
1510 * Client messages must be re-routed, otherwise do the `bfdd`
1511 * special treatment.
1513 if (client
->proto
!= ZEBRA_ROUTE_BFD
) {
1514 _zebra_ptm_reroute(client
, msg
, ZEBRA_BFD_DEST_REPLAY
);
1518 /* Figure out if this is an DEST_UPDATE or DEST_REPLAY. */
1519 if (stream_getl2(msg
, &cmd
) == false) {
1520 zlog_err("%s: expected at least 4 bytes (command)", __func__
);
1525 * Don't modify message in the zebra API. In order to do that we
1526 * need to allocate a new message stream and copy the message
1527 * provided by zebra.
1529 msgc
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1531 zlog_warn("%s: not enough memory", __func__
);
1535 /* Calculate our header size plus the message contents. */
1536 if (cmd
!= ZEBRA_BFD_DEST_REPLAY
) {
1537 zhdrlen
= ZEBRA_HEADER_SIZE
;
1538 zmsglen
= msg
->endp
- msg
->getp
;
1539 memcpy(msgc
->data
+ zhdrlen
, msg
->data
+ msg
->getp
, zmsglen
);
1541 zclient_create_header(msgc
, cmd
, zvrf_id(zvrf
));
1544 msgc
->endp
= zhdrlen
+ zmsglen
;
1546 zclient_create_header(msgc
, cmd
, zvrf_id(zvrf
));
1548 /* Update the data pointers. */
1549 stream_putw_at(msgc
, 0, stream_get_endp(msgc
));
1551 zebra_ptm_send_clients(msgc
);
1557 void zebra_ptm_if_init(struct zebra_if
*zifp
__attribute__((__unused__
)))
1562 int zebra_ptm_get_enable_state(void)
1567 void zebra_ptm_show_status(struct vty
*vty
__attribute__((__unused__
)),
1568 struct interface
*ifp
__attribute__((__unused__
)))
1573 void zebra_ptm_write(struct vty
*vty
__attribute__((__unused__
)))
1578 void zebra_ptm_if_write(struct vty
*vty
__attribute__((__unused__
)),
1579 struct zebra_if
*zifp
__attribute__((__unused__
)))
1583 void zebra_ptm_if_set_ptm_state(struct interface
*i
__attribute__((__unused__
)),
1584 struct zebra_if
*zi
__attribute__((__unused__
)))
1589 #endif /* HAVE_BFDD */