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_debug("%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_debug("%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 flog_err_sys(LIB_ERR_SOCKET
, "%s ptm socket error: %s",
184 __func__
, 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 flog_err_sys(LIB_ERR_SOCKET
, "%s ptm socket error: %s",
210 __func__
, 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 flog_warn(ZEBRA_ERR_UNKNOWN_INTERFACE
,
612 "%s: %s not found in interface list",
618 ptm_lib_find_key_in_msg(in_ctxt
, ZEBRA_PTM_CBL_STR
, cbl_str
);
620 if (cbl_str
[0] == '\0') {
621 return zebra_ptm_handle_bfd_msg(arg
, in_ctxt
, ifp
);
624 return zebra_ptm_handle_cbl_msg(arg
, in_ctxt
, ifp
,
632 int zebra_ptm_sock_read(struct thread
*thread
)
638 sock
= THREAD_FD(thread
);
643 /* PTM communicates in CSV format */
645 rc
= ptm_lib_process_msg(ptm_hdl
, sock
, ptm_cb
.in_data
,
646 ZEBRA_PTM_MAX_SOCKBUF
, NULL
);
649 if (((rc
== 0) && !errno
)
650 || (errno
&& (errno
!= EWOULDBLOCK
) && (errno
!= EAGAIN
))) {
651 flog_err_sys(LIB_ERR_SOCKET
,
652 "%s routing socket error: %s(%d) bytes %d",
653 __func__
, safe_strerror(errno
), errno
, rc
);
655 close(ptm_cb
.ptm_sock
);
656 ptm_cb
.ptm_sock
= -1;
657 zebra_ptm_reset_status(0);
658 ptm_cb
.t_timer
= NULL
;
659 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
660 ptm_cb
.reconnect_time
,
665 ptm_cb
.t_read
= NULL
;
666 thread_add_read(zebrad
.master
, zebra_ptm_sock_read
, NULL
,
667 ptm_cb
.ptm_sock
, &ptm_cb
.t_read
);
672 /* BFD peer/dst register/update */
673 void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS
)
679 uint8_t multi_hop_cnt
;
681 unsigned int min_rx_timer
;
682 unsigned int min_tx_timer
;
683 char if_name
[INTERFACE_NAMSIZ
];
686 char buf
[INET6_ADDRSTRLEN
];
688 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
691 if (hdr
->command
== ZEBRA_BFD_DEST_UPDATE
)
692 client
->bfd_peer_upd8_cnt
++;
694 client
->bfd_peer_add_cnt
++;
696 if (IS_ZEBRA_DEBUG_EVENT
)
697 zlog_debug("bfd_dst_register msg from client %s: length=%d",
698 zebra_route_string(client
->proto
), hdr
->length
);
700 if (ptm_cb
.ptm_sock
== -1) {
701 ptm_cb
.t_timer
= NULL
;
702 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
703 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
707 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
708 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_START_CMD
);
709 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
710 sprintf(tmp_buf
, "%s", zebra_route_string(client
->proto
));
711 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
717 sprintf(tmp_buf
, "%d", pid
);
718 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
721 STREAM_GETW(s
, dst_p
.family
);
723 if (dst_p
.family
== AF_INET
)
724 dst_p
.prefixlen
= IPV4_MAX_BYTELEN
;
726 dst_p
.prefixlen
= IPV6_MAX_BYTELEN
;
728 STREAM_GET(&dst_p
.u
.prefix
, s
, dst_p
.prefixlen
);
729 if (dst_p
.family
== AF_INET
) {
730 inet_ntop(AF_INET
, &dst_p
.u
.prefix4
, buf
, sizeof(buf
));
731 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
732 ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
734 inet_ntop(AF_INET6
, &dst_p
.u
.prefix6
, buf
, sizeof(buf
));
735 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
736 ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
739 STREAM_GETL(s
, min_rx_timer
);
740 sprintf(tmp_buf
, "%d", min_rx_timer
);
741 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_MIN_RX_FIELD
,
743 STREAM_GETL(s
, min_tx_timer
);
744 sprintf(tmp_buf
, "%d", min_tx_timer
);
745 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_MIN_TX_FIELD
,
747 STREAM_GETC(s
, detect_mul
);
748 sprintf(tmp_buf
, "%d", detect_mul
);
749 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_DETECT_MULT_FIELD
,
752 STREAM_GETC(s
, multi_hop
);
754 sprintf(tmp_buf
, "%d", 1);
755 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
756 ZEBRA_PTM_BFD_MULTI_HOP_FIELD
, tmp_buf
);
757 STREAM_GETW(s
, src_p
.family
);
759 if (src_p
.family
== AF_INET
)
760 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
762 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
764 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
765 if (src_p
.family
== AF_INET
) {
766 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
, sizeof(buf
));
767 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
768 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
770 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
, sizeof(buf
));
771 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
772 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
775 STREAM_GETC(s
, multi_hop_cnt
);
776 sprintf(tmp_buf
, "%d", multi_hop_cnt
);
777 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
778 ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD
, tmp_buf
);
780 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
781 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
782 ZEBRA_PTM_BFD_VRF_NAME_FIELD
,
785 if (dst_p
.family
== AF_INET6
) {
786 STREAM_GETW(s
, src_p
.family
);
788 if (src_p
.family
== AF_INET
)
789 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
791 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
793 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
794 if (src_p
.family
== AF_INET
) {
795 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
,
797 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
798 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
801 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
,
803 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
804 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
809 STREAM_GET(if_name
, s
, len
);
812 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
813 ZEBRA_PTM_BFD_IFNAME_FIELD
, if_name
);
816 sprintf(tmp_buf
, "%d", 1);
817 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEND_EVENT
,
820 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
822 if (IS_ZEBRA_DEBUG_SEND
)
823 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
825 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
830 ptm_lib_cleanup_msg(ptm_hdl
, out_ctxt
);
833 /* BFD peer/dst deregister */
834 void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS
)
840 char if_name
[INTERFACE_NAMSIZ
];
842 char buf
[INET6_ADDRSTRLEN
];
844 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
848 client
->bfd_peer_del_cnt
++;
850 if (IS_ZEBRA_DEBUG_EVENT
)
851 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
852 zebra_route_string(client
->proto
), hdr
->length
);
854 if (ptm_cb
.ptm_sock
== -1) {
855 ptm_cb
.t_timer
= NULL
;
856 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
857 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
861 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
863 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_STOP_CMD
);
864 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
866 sprintf(tmp_buf
, "%s", zebra_route_string(client
->proto
));
867 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
873 sprintf(tmp_buf
, "%d", pid
);
874 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
877 STREAM_GETW(s
, dst_p
.family
);
879 if (dst_p
.family
== AF_INET
)
880 dst_p
.prefixlen
= IPV4_MAX_BYTELEN
;
882 dst_p
.prefixlen
= IPV6_MAX_BYTELEN
;
884 STREAM_GET(&dst_p
.u
.prefix
, s
, dst_p
.prefixlen
);
885 if (dst_p
.family
== AF_INET
)
886 inet_ntop(AF_INET
, &dst_p
.u
.prefix4
, buf
, sizeof(buf
));
888 inet_ntop(AF_INET6
, &dst_p
.u
.prefix6
, buf
, sizeof(buf
));
889 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_DST_IP_FIELD
, buf
);
892 STREAM_GETC(s
, multi_hop
);
894 sprintf(tmp_buf
, "%d", 1);
895 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
896 ZEBRA_PTM_BFD_MULTI_HOP_FIELD
, tmp_buf
);
898 STREAM_GETW(s
, src_p
.family
);
900 if (src_p
.family
== AF_INET
)
901 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
903 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
905 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
906 if (src_p
.family
== AF_INET
)
907 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
, sizeof(buf
));
909 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
, sizeof(buf
));
910 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
911 ZEBRA_PTM_BFD_SRC_IP_FIELD
, buf
);
913 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
914 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
915 ZEBRA_PTM_BFD_VRF_NAME_FIELD
,
918 if (dst_p
.family
== AF_INET6
) {
919 STREAM_GETW(s
, src_p
.family
);
921 if (src_p
.family
== AF_INET
)
922 src_p
.prefixlen
= IPV4_MAX_BYTELEN
;
924 src_p
.prefixlen
= IPV6_MAX_BYTELEN
;
926 STREAM_GET(&src_p
.u
.prefix
, s
, src_p
.prefixlen
);
927 if (src_p
.family
== AF_INET
) {
928 inet_ntop(AF_INET
, &src_p
.u
.prefix4
, buf
,
930 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
931 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
934 inet_ntop(AF_INET6
, &src_p
.u
.prefix6
, buf
,
936 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
937 ZEBRA_PTM_BFD_SRC_IP_FIELD
,
943 STREAM_GET(if_name
, s
, len
);
946 ptm_lib_append_msg(ptm_hdl
, out_ctxt
,
947 ZEBRA_PTM_BFD_IFNAME_FIELD
, if_name
);
950 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
951 if (IS_ZEBRA_DEBUG_SEND
)
952 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
955 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
960 ptm_lib_cleanup_msg(ptm_hdl
, out_ctxt
);
963 /* BFD client register */
964 void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS
)
968 void *out_ctxt
= NULL
;
970 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
972 client
->bfd_client_reg_cnt
++;
974 if (IS_ZEBRA_DEBUG_EVENT
)
975 zlog_debug("bfd_client_register msg from client %s: length=%d",
976 zebra_route_string(client
->proto
), hdr
->length
);
981 if (ptm_cb
.ptm_sock
== -1) {
982 ptm_cb
.t_timer
= NULL
;
983 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
984 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
988 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
990 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_CLIENT_REG_CMD
);
991 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
993 sprintf(tmp_buf
, "%s", zebra_route_string(client
->proto
));
994 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
997 sprintf(tmp_buf
, "%d", pid
);
998 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_SEQID_FIELD
,
1001 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
1003 if (IS_ZEBRA_DEBUG_SEND
)
1004 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
1006 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
1008 SET_FLAG(ptm_cb
.client_flags
[client
->proto
],
1009 ZEBRA_PTM_BFD_CLIENT_FLAG_REG
);
1015 * IF we ever add more STREAM_GETXXX functions after the out_ctxt
1016 * is allocated then we need to add this code back in
1019 * ptm_lib_cleanup_msg(ptm_hdl, out_ctxt);
1024 /* BFD client deregister */
1025 int zebra_ptm_bfd_client_deregister(struct zserv
*client
)
1027 uint8_t proto
= client
->proto
;
1030 int data_len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
1032 if (proto
!= ZEBRA_ROUTE_OSPF
&& proto
!= ZEBRA_ROUTE_BGP
1033 && proto
!= ZEBRA_ROUTE_OSPF6
&& proto
!= ZEBRA_ROUTE_PIM
)
1036 if (IS_ZEBRA_DEBUG_EVENT
)
1037 zlog_debug("bfd_client_deregister msg for client %s",
1038 zebra_route_string(proto
));
1040 if (ptm_cb
.ptm_sock
== -1) {
1041 ptm_cb
.t_timer
= NULL
;
1042 thread_add_timer(zebrad
.master
, zebra_ptm_connect
, NULL
,
1043 ptm_cb
.reconnect_time
, &ptm_cb
.t_timer
);
1047 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
, &out_ctxt
);
1049 sprintf(tmp_buf
, "%s", ZEBRA_PTM_BFD_CLIENT_DEREG_CMD
);
1050 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
, tmp_buf
);
1052 sprintf(tmp_buf
, "%s", zebra_route_string(proto
));
1053 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_BFD_CLIENT_FIELD
,
1056 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &data_len
);
1058 if (IS_ZEBRA_DEBUG_SEND
)
1059 zlog_debug("%s: Sent message (%d) %s", __func__
, data_len
,
1062 zebra_ptm_send_message(ptm_cb
.out_data
, data_len
);
1063 UNSET_FLAG(ptm_cb
.client_flags
[proto
], ZEBRA_PTM_BFD_CLIENT_FLAG_REG
);
1068 int zebra_ptm_get_enable_state(void)
1070 return ptm_cb
.ptm_enable
;
1074 * zebra_ptm_get_status_str - Convert status to a display string.
1076 static const char *zebra_ptm_get_status_str(int status
)
1079 case ZEBRA_PTM_STATUS_DOWN
:
1081 case ZEBRA_PTM_STATUS_UP
:
1083 case ZEBRA_PTM_STATUS_UNKNOWN
:
1089 void zebra_ptm_show_status(struct vty
*vty
, struct interface
*ifp
)
1091 vty_out(vty
, " PTM status: ");
1092 if (ifp
->ptm_enable
) {
1093 vty_out(vty
, "%s\n", zebra_ptm_get_status_str(ifp
->ptm_status
));
1095 vty_out(vty
, "disabled\n");
1099 void zebra_ptm_send_status_req(void)
1102 int len
= ZEBRA_PTM_SEND_MAX_SOCKBUF
;
1104 if (ptm_cb
.ptm_enable
) {
1105 ptm_lib_init_msg(ptm_hdl
, 0, PTMLIB_MSG_TYPE_CMD
, NULL
,
1107 ptm_lib_append_msg(ptm_hdl
, out_ctxt
, ZEBRA_PTM_CMD_STR
,
1108 ZEBRA_PTM_GET_STATUS_CMD
);
1109 ptm_lib_complete_msg(ptm_hdl
, out_ctxt
, ptm_cb
.out_data
, &len
);
1111 zebra_ptm_send_message(ptm_cb
.out_data
, len
);
1115 void zebra_ptm_reset_status(int ptm_disable
)
1118 struct interface
*ifp
;
1121 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
1122 FOR_ALL_INTERFACES (vrf
, ifp
) {
1124 if (ifp
->ptm_enable
) {
1125 if (!if_is_operative(ifp
))
1130 ZEBRA_IF_PTM_ENABLE_OFF
;
1131 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
1133 if (if_is_operative(ifp
) && send_linkup
) {
1134 if (IS_ZEBRA_DEBUG_EVENT
)
1136 "%s: Bringing up interface %s",
1137 __func__
, ifp
->name
);
1144 void zebra_ptm_if_init(struct zebra_if
*zebra_ifp
)
1146 zebra_ifp
->ptm_enable
= ZEBRA_IF_PTM_ENABLE_UNSPEC
;
1149 void zebra_ptm_if_set_ptm_state(struct interface
*ifp
,
1150 struct zebra_if
*zebra_ifp
)
1152 if (zebra_ifp
&& zebra_ifp
->ptm_enable
!= ZEBRA_IF_PTM_ENABLE_UNSPEC
)
1153 ifp
->ptm_enable
= zebra_ifp
->ptm_enable
;
1156 void zebra_ptm_if_write(struct vty
*vty
, struct zebra_if
*zebra_ifp
)
1158 if (zebra_ifp
->ptm_enable
== ZEBRA_IF_PTM_ENABLE_OFF
)
1159 vty_out(vty
, " no ptm-enable\n");
1162 #else /* HAVE_BFDD */
1164 #include "zebra/zebra_memory.h"
1169 struct ptm_process
{
1170 struct zserv
*pp_zs
;
1173 TAILQ_ENTRY(ptm_process
) pp_entry
;
1175 TAILQ_HEAD(ppqueue
, ptm_process
) ppqueue
;
1177 DEFINE_MTYPE_STATIC(ZEBRA
, ZEBRA_PTM_BFD_PROCESS
,
1178 "PTM BFD process registration table.");
1183 static struct ptm_process
*pp_new(pid_t pid
, struct zserv
*zs
);
1184 static struct ptm_process
*pp_lookup_byzs(struct zserv
*zs
);
1185 static void pp_free(struct ptm_process
*pp
);
1186 static void pp_free_all(void);
1188 static void zebra_ptm_send_bfdd(struct stream
*msg
);
1189 static void zebra_ptm_send_clients(struct stream
*msg
);
1190 static int _zebra_ptm_bfd_client_deregister(struct zserv
*zs
);
1191 static void _zebra_ptm_reroute(struct zserv
*zs
, struct stream
*msg
,
1196 * Process PID registration.
1198 static struct ptm_process
*pp_new(pid_t pid
, struct zserv
*zs
)
1200 struct ptm_process
*pp
;
1203 /* Sanity check: more than one client can't have the same PID. */
1204 TAILQ_FOREACH(pp
, &ppqueue
, pp_entry
) {
1205 if (pp
->pp_pid
== pid
&& pp
->pp_zs
!= zs
)
1206 zlog_err("%s:%d pid and client pointer doesn't match",
1207 __FILE__
, __LINE__
);
1209 #endif /* PTM_DEBUG */
1211 /* Lookup for duplicates. */
1212 pp
= pp_lookup_byzs(zs
);
1216 /* Allocate and register new process. */
1217 pp
= XCALLOC(MTYPE_ZEBRA_PTM_BFD_PROCESS
, sizeof(*pp
));
1223 TAILQ_INSERT_HEAD(&ppqueue
, pp
, pp_entry
);
1228 static struct ptm_process
*pp_lookup_byzs(struct zserv
*zs
)
1230 struct ptm_process
*pp
;
1232 TAILQ_FOREACH(pp
, &ppqueue
, pp_entry
) {
1233 if (pp
->pp_zs
!= zs
)
1242 static void pp_free(struct ptm_process
*pp
)
1247 TAILQ_REMOVE(&ppqueue
, pp
, pp_entry
);
1248 XFREE(MTYPE_ZEBRA_PTM_BFD_PROCESS
, pp
);
1251 static void pp_free_all(void)
1253 struct ptm_process
*pp
;
1255 while (!TAILQ_EMPTY(&ppqueue
)) {
1256 pp
= TAILQ_FIRST(&ppqueue
);
1263 * Use the FRR's internal daemon implementation.
1265 static void zebra_ptm_send_bfdd(struct stream
*msg
)
1267 struct listnode
*node
;
1268 struct zserv
*client
;
1269 struct stream
*msgc
;
1271 /* Create copy for replication. */
1272 msgc
= stream_dup(msg
);
1274 zlog_debug("%s: not enough memory", __func__
);
1278 /* Send message to all running BFDd daemons. */
1279 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
1280 if (client
->proto
!= ZEBRA_ROUTE_BFD
)
1283 zserv_send_message(client
, msg
);
1285 /* Allocate more messages. */
1286 msg
= stream_dup(msgc
);
1288 zlog_debug("%s: not enough memory", __func__
);
1297 static void zebra_ptm_send_clients(struct stream
*msg
)
1299 struct listnode
*node
;
1300 struct zserv
*client
;
1301 struct stream
*msgc
;
1303 /* Create copy for replication. */
1304 msgc
= stream_dup(msg
);
1306 zlog_debug("%s: not enough memory", __func__
);
1310 /* Send message to all running client daemons. */
1311 for (ALL_LIST_ELEMENTS_RO(zebrad
.client_list
, node
, client
)) {
1312 switch (client
->proto
) {
1313 case ZEBRA_ROUTE_BGP
:
1314 case ZEBRA_ROUTE_OSPF
:
1315 case ZEBRA_ROUTE_OSPF6
:
1316 case ZEBRA_ROUTE_PIM
:
1320 /* NOTHING: skip this daemon. */
1324 zserv_send_message(client
, msg
);
1326 /* Allocate more messages. */
1327 msg
= stream_dup(msgc
);
1329 zlog_debug("%s: not enough memory", __func__
);
1338 static int _zebra_ptm_bfd_client_deregister(struct zserv
*zs
)
1341 struct ptm_process
*pp
;
1343 /* Filter daemons that must receive this treatment. */
1344 switch (zs
->proto
) {
1345 case ZEBRA_ROUTE_BGP
:
1346 case ZEBRA_ROUTE_OSPF
:
1347 case ZEBRA_ROUTE_OSPF6
:
1348 case ZEBRA_ROUTE_PIM
:
1351 case ZEBRA_ROUTE_BFD
:
1352 /* Don't try to send BFDd messages to itself. */
1356 /* Unsupported daemon. */
1360 /* Find daemon pid by zebra connection pointer. */
1361 pp
= pp_lookup_byzs(zs
);
1363 zlog_err("%s:%d failed to find process pid registration",
1364 __FILE__
, __LINE__
);
1368 /* Generate, send message and free() daemon related data. */
1369 msg
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1371 zlog_debug("%s: not enough memory", __func__
);
1376 * The message type will be BFD_DEST_REPLY so we can use only
1377 * one callback at the `bfdd` side, however the real command
1378 * number will be included right after the zebra header.
1380 zclient_create_header(msg
, ZEBRA_BFD_DEST_REPLAY
, 0);
1381 stream_putl(msg
, ZEBRA_BFD_CLIENT_DEREGISTER
);
1383 /* Put process PID. */
1384 stream_putl(msg
, pp
->pp_pid
);
1386 /* Update the data pointers. */
1387 stream_putw_at(msg
, 0, stream_get_endp(msg
));
1389 zebra_ptm_send_bfdd(msg
);
1396 void zebra_ptm_init(void)
1398 /* Initialize the ptm process information list. */
1399 TAILQ_INIT(&ppqueue
);
1402 * Send deregistration messages to BFD daemon when some other
1403 * daemon closes. This will help avoid sending daemons
1404 * unnecessary notification messages.
1406 hook_register(zserv_client_close
, _zebra_ptm_bfd_client_deregister
);
1409 void zebra_ptm_finish(void)
1411 /* Remove the client disconnect hook and free all memory. */
1412 hook_unregister(zserv_client_close
, _zebra_ptm_bfd_client_deregister
);
1420 static void _zebra_ptm_reroute(struct zserv
*zs
, struct stream
*msg
,
1423 struct stream
*msgc
;
1424 size_t zmsglen
, zhdrlen
;
1428 * Don't modify message in the zebra API. In order to do that we
1429 * need to allocate a new message stream and copy the message
1430 * provided by zebra.
1432 msgc
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1434 zlog_debug("%s: not enough memory", __func__
);
1438 /* Calculate our header size plus the message contents. */
1439 zhdrlen
= ZEBRA_HEADER_SIZE
+ sizeof(uint32_t);
1440 zmsglen
= msg
->endp
- msg
->getp
;
1441 memcpy(msgc
->data
+ zhdrlen
, msg
->data
+ msg
->getp
, zmsglen
);
1444 * The message type will be BFD_DEST_REPLY so we can use only
1445 * one callback at the `bfdd` side, however the real command
1446 * number will be included right after the zebra header.
1448 zclient_create_header(msgc
, ZEBRA_BFD_DEST_REPLAY
, 0);
1449 stream_putl(msgc
, command
);
1451 /* Update the data pointers. */
1453 msgc
->endp
= zhdrlen
+ zmsglen
;
1454 stream_putw_at(msgc
, 0, stream_get_endp(msgc
));
1456 zebra_ptm_send_bfdd(msgc
);
1458 /* Registrate process PID for shutdown hook. */
1459 STREAM_GETL(msg
, ppid
);
1465 zlog_err("%s:%d failed to registrate client pid", __FILE__
, __LINE__
);
1468 void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS
)
1470 if (IS_ZEBRA_DEBUG_EVENT
)
1471 zlog_debug("bfd_dst_register msg from client %s: length=%d",
1472 zebra_route_string(client
->proto
), hdr
->length
);
1474 _zebra_ptm_reroute(client
, msg
, ZEBRA_BFD_DEST_REGISTER
);
1477 void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS
)
1479 if (IS_ZEBRA_DEBUG_EVENT
)
1480 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
1481 zebra_route_string(client
->proto
), hdr
->length
);
1483 _zebra_ptm_reroute(client
, msg
, ZEBRA_BFD_DEST_DEREGISTER
);
1486 void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS
)
1488 if (IS_ZEBRA_DEBUG_EVENT
)
1489 zlog_debug("bfd_client_register msg from client %s: length=%d",
1490 zebra_route_string(client
->proto
), hdr
->length
);
1492 _zebra_ptm_reroute(client
, msg
, ZEBRA_BFD_CLIENT_REGISTER
);
1495 void zebra_ptm_bfd_dst_replay(ZAPI_HANDLER_ARGS
)
1497 struct stream
*msgc
;
1498 size_t zmsglen
, zhdrlen
;
1503 * Replay messages with HAVE_BFDD are meant to be replayed to
1504 * the client daemons. These messages are composed and
1505 * originated from the `bfdd` daemon.
1507 if (IS_ZEBRA_DEBUG_EVENT
)
1508 zlog_debug("bfd_dst_update msg from client %s: length=%d",
1509 zebra_route_string(client
->proto
), hdr
->length
);
1512 * Client messages must be re-routed, otherwise do the `bfdd`
1513 * special treatment.
1515 if (client
->proto
!= ZEBRA_ROUTE_BFD
) {
1516 _zebra_ptm_reroute(client
, msg
, ZEBRA_BFD_DEST_REPLAY
);
1520 /* Figure out if this is an DEST_UPDATE or DEST_REPLAY. */
1521 if (stream_getl2(msg
, &cmd
) == false) {
1522 zlog_err("%s: expected at least 4 bytes (command)", __func__
);
1527 * Don't modify message in the zebra API. In order to do that we
1528 * need to allocate a new message stream and copy the message
1529 * provided by zebra.
1531 msgc
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
1533 zlog_debug("%s: not enough memory", __func__
);
1537 /* Calculate our header size plus the message contents. */
1538 if (cmd
!= ZEBRA_BFD_DEST_REPLAY
) {
1539 zhdrlen
= ZEBRA_HEADER_SIZE
;
1540 zmsglen
= msg
->endp
- msg
->getp
;
1541 memcpy(msgc
->data
+ zhdrlen
, msg
->data
+ msg
->getp
, zmsglen
);
1543 zclient_create_header(msgc
, cmd
, zvrf_id(zvrf
));
1546 msgc
->endp
= zhdrlen
+ zmsglen
;
1548 zclient_create_header(msgc
, cmd
, zvrf_id(zvrf
));
1550 /* Update the data pointers. */
1551 stream_putw_at(msgc
, 0, stream_get_endp(msgc
));
1553 zebra_ptm_send_clients(msgc
);
1559 void zebra_ptm_if_init(struct zebra_if
*zifp
__attribute__((__unused__
)))
1564 int zebra_ptm_get_enable_state(void)
1569 void zebra_ptm_show_status(struct vty
*vty
__attribute__((__unused__
)),
1570 struct interface
*ifp
__attribute__((__unused__
)))
1575 void zebra_ptm_write(struct vty
*vty
__attribute__((__unused__
)))
1580 void zebra_ptm_if_write(struct vty
*vty
__attribute__((__unused__
)),
1581 struct zebra_if
*zifp
__attribute__((__unused__
)))
1585 void zebra_ptm_if_set_ptm_state(struct interface
*i
__attribute__((__unused__
)),
1586 struct zebra_if
*zi
__attribute__((__unused__
)))
1591 #endif /* HAVE_BFDD */