]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_ptm.c
*: Convert thread_add_XXX functions to event_add_XXX
[mirror_frr.git] / zebra / zebra_ptm.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
244c1cdc
DS
2/* Kernel routing table updates using netlink over GNU/Linux system.
3 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
244c1cdc
DS
4 */
5
6#include <zebra.h>
43e52561 7
d62a17ae 8#include <sys/un.h> /* for sockaddr_un */
244c1cdc 9#include <net/if.h>
43e52561
QY
10
11#include "bfd.h"
12#include "buffer.h"
13#include "command.h"
14#include "if.h"
15#include "network.h"
16#include "ptm_lib.h"
17#include "rib.h"
18#include "stream.h"
43e52561 19#include "vrf.h"
c8ed14dd 20#include "vty.h"
364fed6b 21#include "lib_errors.h"
43e52561 22
244c1cdc 23#include "zebra/debug.h"
43e52561
QY
24#include "zebra/interface.h"
25#include "zebra/zebra_errors.h"
244c1cdc 26#include "zebra/zebra_ptm.h"
c43ed2e4 27#include "zebra/zebra_ptm_redistribute.h"
161e9ab7 28#include "zebra/zebra_router.h"
7c551956 29#include "zebra_vrf.h"
244c1cdc 30
d3af6147
RZ
31/*
32 * Choose the BFD implementation that we'll use.
33 *
34 * There are two implementations:
35 * - PTM BFD: which uses an external daemon;
36 * - bfdd: FRR's own BFD daemon;
37 */
38#if HAVE_BFDD == 0
39
244c1cdc
DS
40#define ZEBRA_PTM_RECONNECT_TIME_INITIAL 1 /* initial reconnect is 1s */
41#define ZEBRA_PTM_RECONNECT_TIME_MAX 300
c01acf98 42
c01acf98 43#define PTM_MSG_LEN 4
0a56c844 44#define PTM_HEADER_LEN 37
c43ed2e4
DS
45
46const char ZEBRA_PTM_GET_STATUS_CMD[] = "get-status";
47const char ZEBRA_PTM_BFD_START_CMD[] = "start-bfd-sess";
48const char ZEBRA_PTM_BFD_STOP_CMD[] = "stop-bfd-sess";
055c4dfc 49const char ZEBRA_PTM_BFD_CLIENT_REG_CMD[] = "reg-bfd-client";
567b877d 50const char ZEBRA_PTM_BFD_CLIENT_DEREG_CMD[] = "dereg-bfd-client";
c43ed2e4 51
c8ed14dd 52const char ZEBRA_PTM_CMD_STR[] = "cmd";
b255a4b1 53const char ZEBRA_PTM_CMD_STATUS_STR[] = "cmd_status";
c43ed2e4
DS
54const char ZEBRA_PTM_PORT_STR[] = "port";
55const char ZEBRA_PTM_CBL_STR[] = "cbl status";
56const char ZEBRA_PTM_PASS_STR[] = "pass";
57const char ZEBRA_PTM_FAIL_STR[] = "fail";
58const char ZEBRA_PTM_BFDSTATUS_STR[] = "state";
59const char ZEBRA_PTM_BFDSTATUS_UP_STR[] = "Up";
60const char ZEBRA_PTM_BFDSTATUS_DOWN_STR[] = "Down";
61const char ZEBRA_PTM_BFDDEST_STR[] = "peer";
62const char ZEBRA_PTM_BFDSRC_STR[] = "local";
d553294e 63const char ZEBRA_PTM_BFDVRF_STR[] = "vrf";
c43ed2e4
DS
64const char ZEBRA_PTM_INVALID_PORT_NAME[] = "N/A";
65const char ZEBRA_PTM_INVALID_SRC_IP[] = "N/A";
d553294e 66const char ZEBRA_PTM_INVALID_VRF[] = "N/A";
c43ed2e4
DS
67
68const char ZEBRA_PTM_BFD_DST_IP_FIELD[] = "dstIPaddr";
69const char ZEBRA_PTM_BFD_SRC_IP_FIELD[] = "srcIPaddr";
70const char ZEBRA_PTM_BFD_MIN_RX_FIELD[] = "requiredMinRx";
71const char ZEBRA_PTM_BFD_MIN_TX_FIELD[] = "upMinTx";
72const char ZEBRA_PTM_BFD_DETECT_MULT_FIELD[] = "detectMult";
73const char ZEBRA_PTM_BFD_MULTI_HOP_FIELD[] = "multiHop";
74const char ZEBRA_PTM_BFD_CLIENT_FIELD[] = "client";
75const char ZEBRA_PTM_BFD_SEQID_FIELD[] = "seqid";
76const char ZEBRA_PTM_BFD_IFNAME_FIELD[] = "ifName";
77const char ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD[] = "maxHopCnt";
68fe91d6 78const char ZEBRA_PTM_BFD_SEND_EVENT[] = "sendEvent";
d553294e 79const char ZEBRA_PTM_BFD_VRF_NAME_FIELD[] = "vrfName";
9beff0bd 80const char ZEBRA_PTM_BFD_CBIT_FIELD[] = "bfdcbit";
c01acf98 81
c43ed2e4 82static ptm_lib_handle_t *ptm_hdl;
244c1cdc 83
c8ed14dd
DS
84struct zebra_ptm_cb ptm_cb;
85
244c1cdc 86static int zebra_ptm_socket_init(void);
e6685141 87void zebra_ptm_sock_read(struct event *thread);
d62a17ae 88static void zebra_ptm_install_commands(void);
b255a4b1 89static int zebra_ptm_handle_msg_cb(void *arg, void *in_ctxt);
d62a17ae 90void zebra_bfd_peer_replay_req(void);
950bd436 91void zebra_ptm_send_status_req(void);
92void zebra_ptm_reset_status(int ptm_disable);
8b1766b1 93static int zebra_ptm_bfd_client_deregister(struct zserv *client);
244c1cdc
DS
94
95const char ZEBRA_PTM_SOCK_NAME[] = "\0/var/run/ptmd.socket";
96
d62a17ae 97void zebra_ptm_init(void)
244c1cdc 98{
d62a17ae 99 char buf[64];
c43ed2e4 100
6006b807 101 memset(&ptm_cb, 0, sizeof(ptm_cb));
c8ed14dd 102
d62a17ae 103 ptm_cb.out_data = calloc(1, ZEBRA_PTM_SEND_MAX_SOCKBUF);
104 if (!ptm_cb.out_data) {
9df414fe 105 zlog_debug("%s: Allocation of send data failed", __func__);
d62a17ae 106 return;
107 }
c8ed14dd 108
d62a17ae 109 ptm_cb.in_data = calloc(1, ZEBRA_PTM_MAX_SOCKBUF);
110 if (!ptm_cb.in_data) {
9df414fe 111 zlog_debug("%s: Allocation of recv data failed", __func__);
d62a17ae 112 free(ptm_cb.out_data);
113 return;
114 }
c8ed14dd 115
d62a17ae 116 ptm_cb.pid = getpid();
117 zebra_ptm_install_commands();
c43ed2e4 118
772270f3 119 snprintf(buf, sizeof(buf), "%s", FRR_PTM_NAME);
d62a17ae 120 ptm_hdl = ptm_lib_register(buf, NULL, zebra_ptm_handle_msg_cb,
121 zebra_ptm_handle_msg_cb);
122 ptm_cb.wb = buffer_new(0);
c8ed14dd 123
d62a17ae 124 ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_INITIAL;
c8ed14dd 125
d62a17ae 126 ptm_cb.ptm_sock = -1;
453844ab 127
21ccc0cf 128 hook_register(zserv_client_close, zebra_ptm_bfd_client_deregister);
c8ed14dd
DS
129}
130
d62a17ae 131void zebra_ptm_finish(void)
c8ed14dd 132{
d62a17ae 133 buffer_flush_all(ptm_cb.wb, ptm_cb.ptm_sock);
c8ed14dd 134
d62a17ae 135 free(ptm_hdl);
58ac32e2 136
d62a17ae 137 if (ptm_cb.out_data)
138 free(ptm_cb.out_data);
c8ed14dd 139
d62a17ae 140 if (ptm_cb.in_data)
141 free(ptm_cb.in_data);
c8ed14dd 142
b3d6bc6e 143 /* Cancel events. */
146bcb9b
DS
144 THREAD_OFF(ptm_cb.t_read);
145 THREAD_OFF(ptm_cb.t_write);
146 THREAD_OFF(ptm_cb.t_timer);
2376c3f2 147
d62a17ae 148 if (ptm_cb.wb)
149 buffer_free(ptm_cb.wb);
2376c3f2 150
1e9f448f 151 if (ptm_cb.ptm_sock >= 0)
d62a17ae 152 close(ptm_cb.ptm_sock);
c8ed14dd
DS
153}
154
e6685141 155static void zebra_ptm_flush_messages(struct event *thread)
c8ed14dd 156{
d62a17ae 157 ptm_cb.t_write = NULL;
158
159 if (ptm_cb.ptm_sock == -1)
cc9f21da 160 return;
d62a17ae 161
162 errno = 0;
163
164 switch (buffer_flush_available(ptm_cb.wb, ptm_cb.ptm_sock)) {
165 case BUFFER_ERROR:
1c50c1c0
QY
166 flog_err_sys(EC_LIB_SOCKET, "%s ptm socket error: %s", __func__,
167 safe_strerror(errno));
d62a17ae 168 close(ptm_cb.ptm_sock);
169 ptm_cb.ptm_sock = -1;
170 zebra_ptm_reset_status(0);
171 ptm_cb.t_timer = NULL;
907a2395
DS
172 event_add_timer(zrouter.master, zebra_ptm_connect, NULL,
173 ptm_cb.reconnect_time, &ptm_cb.t_timer);
cc9f21da 174 return;
d62a17ae 175 case BUFFER_PENDING:
176 ptm_cb.t_write = NULL;
907a2395
DS
177 event_add_write(zrouter.master, zebra_ptm_flush_messages, NULL,
178 ptm_cb.ptm_sock, &ptm_cb.t_write);
d62a17ae 179 break;
180 case BUFFER_EMPTY:
181 break;
182 }
c8ed14dd
DS
183}
184
d62a17ae 185static int zebra_ptm_send_message(char *data, int size)
c8ed14dd 186{
d62a17ae 187 errno = 0;
188 switch (buffer_write(ptm_cb.wb, ptm_cb.ptm_sock, data, size)) {
189 case BUFFER_ERROR:
1c50c1c0
QY
190 flog_err_sys(EC_LIB_SOCKET, "%s ptm socket error: %s", __func__,
191 safe_strerror(errno));
d62a17ae 192 close(ptm_cb.ptm_sock);
193 ptm_cb.ptm_sock = -1;
194 zebra_ptm_reset_status(0);
195 ptm_cb.t_timer = NULL;
907a2395
DS
196 event_add_timer(zrouter.master, zebra_ptm_connect, NULL,
197 ptm_cb.reconnect_time, &ptm_cb.t_timer);
d62a17ae 198 return -1;
199 case BUFFER_EMPTY:
146bcb9b 200 THREAD_OFF(ptm_cb.t_write);
d62a17ae 201 break;
202 case BUFFER_PENDING:
907a2395
DS
203 event_add_write(zrouter.master, zebra_ptm_flush_messages, NULL,
204 ptm_cb.ptm_sock, &ptm_cb.t_write);
d62a17ae 205 break;
206 }
207
208 return 0;
244c1cdc
DS
209}
210
e6685141 211void zebra_ptm_connect(struct event *t)
244c1cdc 212{
d62a17ae 213 int init = 0;
214
215 if (ptm_cb.ptm_sock == -1) {
216 zebra_ptm_socket_init();
217 init = 1;
218 }
219
220 if (ptm_cb.ptm_sock != -1) {
221 if (init) {
222 ptm_cb.t_read = NULL;
907a2395
DS
223 event_add_read(zrouter.master, zebra_ptm_sock_read,
224 NULL, ptm_cb.ptm_sock, &ptm_cb.t_read);
d62a17ae 225 zebra_bfd_peer_replay_req();
226 }
227 zebra_ptm_send_status_req();
228 ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_INITIAL;
229 } else if (ptm_cb.reconnect_time < ZEBRA_PTM_RECONNECT_TIME_MAX) {
230 ptm_cb.reconnect_time *= 2;
231 if (ptm_cb.reconnect_time > ZEBRA_PTM_RECONNECT_TIME_MAX)
232 ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_MAX;
233
234 ptm_cb.t_timer = NULL;
907a2395
DS
235 event_add_timer(zrouter.master, zebra_ptm_connect, NULL,
236 ptm_cb.reconnect_time, &ptm_cb.t_timer);
d62a17ae 237 } else if (ptm_cb.reconnect_time >= ZEBRA_PTM_RECONNECT_TIME_MAX) {
238 ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_INITIAL;
239 }
244c1cdc
DS
240}
241
244c1cdc
DS
242DEFUN (zebra_ptm_enable,
243 zebra_ptm_enable_cmd,
244 "ptm-enable",
245 "Enable neighbor check with specified topology\n")
246{
d62a17ae 247 struct vrf *vrf;
d62a17ae 248 struct interface *ifp;
249 struct zebra_if *if_data;
250
251 ptm_cb.ptm_enable = ZEBRA_IF_PTM_ENABLE_ON;
252
a2addae8 253 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
451fda4f 254 FOR_ALL_INTERFACES (vrf, ifp)
a2addae8
RW
255 if (!ifp->ptm_enable) {
256 if_data = (struct zebra_if *)ifp->info;
257 if (if_data
258 && (if_data->ptm_enable
9d303b37 259 == ZEBRA_IF_PTM_ENABLE_UNSPEC)) {
a2addae8
RW
260 ifp->ptm_enable =
261 ZEBRA_IF_PTM_ENABLE_ON;
262 }
263 /* Assign a default unknown status */
264 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
d62a17ae 265 }
d62a17ae 266
267 zebra_ptm_connect(NULL);
268
269 return CMD_SUCCESS;
244c1cdc
DS
270}
271
272DEFUN (no_zebra_ptm_enable,
273 no_zebra_ptm_enable_cmd,
274 "no ptm-enable",
275 NO_STR
276 "Enable neighbor check with specified topology\n")
277{
d62a17ae 278 ptm_cb.ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF;
279 zebra_ptm_reset_status(1);
280 return CMD_SUCCESS;
244c1cdc
DS
281}
282
986aa00f 283DEFUN (zebra_ptm_enable_if,
284 zebra_ptm_enable_if_cmd,
285 "ptm-enable",
286 "Enable neighbor check with specified topology\n")
287{
d62a17ae 288 VTY_DECLVAR_CONTEXT(interface, ifp);
289 struct zebra_if *if_data;
290 int old_ptm_enable;
291 int send_linkdown = 0;
292
a2023fab 293 if_data = ifp->info;
294 if_data->ptm_enable = ZEBRA_IF_PTM_ENABLE_UNSPEC;
295
d62a17ae 296 if (ifp->ifindex == IFINDEX_INTERNAL) {
297 return CMD_SUCCESS;
298 }
299
300 old_ptm_enable = ifp->ptm_enable;
301 ifp->ptm_enable = ptm_cb.ptm_enable;
302
303 if (if_is_no_ptm_operative(ifp))
304 send_linkdown = 1;
305
306 if (!old_ptm_enable && ptm_cb.ptm_enable) {
307 if (!if_is_operative(ifp) && send_linkdown) {
308 if (IS_ZEBRA_DEBUG_EVENT)
9165c5f5 309 zlog_debug("%s: Bringing down interface %s",
d62a17ae 310 __func__, ifp->name);
311 if_down(ifp);
312 }
313 }
314
d62a17ae 315 return CMD_SUCCESS;
986aa00f 316}
317
318DEFUN (no_zebra_ptm_enable_if,
319 no_zebra_ptm_enable_if_cmd,
320 "no ptm-enable",
321 NO_STR
322 "Enable neighbor check with specified topology\n")
323{
d62a17ae 324 VTY_DECLVAR_CONTEXT(interface, ifp);
325 int send_linkup = 0;
326 struct zebra_if *if_data;
327
328 if ((ifp->ifindex != IFINDEX_INTERNAL) && (ifp->ptm_enable)) {
329 if (!if_is_operative(ifp))
330 send_linkup = 1;
331
332 ifp->ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF;
333 if (if_is_no_ptm_operative(ifp) && send_linkup) {
334 if (IS_ZEBRA_DEBUG_EVENT)
9165c5f5 335 zlog_debug("%s: Bringing up interface %s",
d62a17ae 336 __func__, ifp->name);
8b48cdb9 337 if_up(ifp, true);
d62a17ae 338 }
339 }
340
341 if_data = ifp->info;
342 if_data->ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF;
343
344 return CMD_SUCCESS;
986aa00f 345}
346
347
d62a17ae 348void zebra_ptm_write(struct vty *vty)
244c1cdc 349{
d62a17ae 350 if (ptm_cb.ptm_enable)
351 vty_out(vty, "ptm-enable\n");
244c1cdc 352
d62a17ae 353 return;
244c1cdc
DS
354}
355
d62a17ae 356static int zebra_ptm_socket_init(void)
244c1cdc 357{
d62a17ae 358 int ret;
359 int sock;
360 struct sockaddr_un addr;
361
362 ptm_cb.ptm_sock = -1;
363
364 sock = socket(PF_UNIX, SOCK_STREAM, 0);
365 if (sock < 0)
366 return -1;
367 if (set_nonblocking(sock) < 0) {
368 if (IS_ZEBRA_DEBUG_EVENT)
369 zlog_debug("%s: Unable to set socket non blocking[%s]",
15569c58 370 __func__, safe_strerror(errno));
d62a17ae 371 close(sock);
372 return -1;
373 }
374
375 /* Make server socket. */
6006b807 376 memset(&addr, 0, sizeof(addr));
d62a17ae 377 addr.sun_family = AF_UNIX;
378 memcpy(&addr.sun_path, ZEBRA_PTM_SOCK_NAME,
379 sizeof(ZEBRA_PTM_SOCK_NAME));
380
381 ret = connect(sock, (struct sockaddr *)&addr,
382 sizeof(addr.sun_family) + sizeof(ZEBRA_PTM_SOCK_NAME)
383 - 1);
384 if (ret < 0) {
385 if (IS_ZEBRA_DEBUG_EVENT)
386 zlog_debug("%s: Unable to connect to socket %s [%s]",
387 __func__, ZEBRA_PTM_SOCK_NAME,
388 safe_strerror(errno));
389 close(sock);
390 return -1;
391 }
392 ptm_cb.ptm_sock = sock;
393 return sock;
244c1cdc
DS
394}
395
d62a17ae 396static void zebra_ptm_install_commands(void)
244c1cdc 397{
d62a17ae 398 install_element(CONFIG_NODE, &zebra_ptm_enable_cmd);
399 install_element(CONFIG_NODE, &no_zebra_ptm_enable_cmd);
400 install_element(INTERFACE_NODE, &zebra_ptm_enable_if_cmd);
401 install_element(INTERFACE_NODE, &no_zebra_ptm_enable_if_cmd);
244c1cdc
DS
402}
403
c43ed2e4 404/* BFD session goes down, send message to the protocols. */
d62a17ae 405static void if_bfd_session_update(struct interface *ifp, struct prefix *dp,
406 struct prefix *sp, int status,
407 vrf_id_t vrf_id)
c01acf98 408{
d62a17ae 409 if (IS_ZEBRA_DEBUG_EVENT) {
410 char buf[2][INET6_ADDRSTRLEN];
411
412 if (ifp) {
413 zlog_debug(
3efd0893 414 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d on %s %s event",
d62a17ae 415 inet_ntop(dp->family, &dp->u.prefix, buf[0],
416 INET6_ADDRSTRLEN),
417 dp->prefixlen, ifp->name,
418 bfd_get_status_str(status));
419 } else {
c479e756
DS
420 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
421
d62a17ae 422 zlog_debug(
3efd0893 423 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d with src %s/%d and vrf %s(%u) %s event",
d62a17ae 424 inet_ntop(dp->family, &dp->u.prefix, buf[0],
425 INET6_ADDRSTRLEN),
426 dp->prefixlen,
427 inet_ntop(sp->family, &sp->u.prefix, buf[1],
428 INET6_ADDRSTRLEN),
c479e756 429 sp->prefixlen, VRF_LOGNAME(vrf), vrf_id,
d62a17ae 430 bfd_get_status_str(status));
431 }
432 }
433
434 zebra_interface_bfd_update(ifp, dp, sp, status, vrf_id);
c01acf98
DS
435}
436
d62a17ae 437static int zebra_ptm_handle_bfd_msg(void *arg, void *in_ctxt,
438 struct interface *ifp)
244c1cdc 439{
d62a17ae 440 char bfdst_str[32];
441 char dest_str[64];
442 char src_str[64];
443 char vrf_str[64];
444 struct prefix dest_prefix;
445 struct prefix src_prefix;
446 vrf_id_t vrf_id;
447
448 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDSTATUS_STR, bfdst_str);
449
450 if (bfdst_str[0] == '\0') {
451 return -1;
452 }
453
454 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDDEST_STR, dest_str);
455
456 if (dest_str[0] == '\0') {
457 zlog_debug("%s: Key %s not found in PTM msg", __func__,
458 ZEBRA_PTM_BFDDEST_STR);
459 return -1;
460 }
461
462 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDSRC_STR, src_str);
463
464 if (src_str[0] == '\0') {
465 zlog_debug("%s: Key %s not found in PTM msg", __func__,
466 ZEBRA_PTM_BFDSRC_STR);
467 return -1;
468 }
469
470 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDVRF_STR, vrf_str);
471
472 if (vrf_str[0] == '\0') {
473 zlog_debug("%s: Key %s not found in PTM msg", __func__,
474 ZEBRA_PTM_BFDVRF_STR);
475 return -1;
476 }
477
478 if (IS_ZEBRA_DEBUG_EVENT)
479 zlog_debug(
3efd0893 480 "%s: Recv Port [%s] bfd status [%s] vrf [%s] peer [%s] local [%s]",
d62a17ae 481 __func__, ifp ? ifp->name : "N/A", bfdst_str, vrf_str,
482 dest_str, src_str);
483
484 if (str2prefix(dest_str, &dest_prefix) == 0) {
e914ccbe 485 flog_err(EC_ZEBRA_PREFIX_PARSE_ERROR,
1c50c1c0 486 "%s: Peer addr %s not found", __func__, dest_str);
d62a17ae 487 return -1;
488 }
489
6006b807 490 memset(&src_prefix, 0, sizeof(src_prefix));
d62a17ae 491 if (strcmp(ZEBRA_PTM_INVALID_SRC_IP, src_str)) {
492 if (str2prefix(src_str, &src_prefix) == 0) {
e914ccbe 493 flog_err(EC_ZEBRA_PREFIX_PARSE_ERROR,
1c50c1c0
QY
494 "%s: Local addr %s not found", __func__,
495 src_str);
d62a17ae 496 return -1;
497 }
498 }
499
500 if (!strcmp(ZEBRA_PTM_INVALID_VRF, vrf_str) && ifp) {
096f7609 501 vrf_id = ifp->vrf->vrf_id;
d62a17ae 502 } else {
a383d4d2
PZ
503 struct vrf *pVrf;
504
505 pVrf = vrf_lookup_by_name(vrf_str);
506 if (pVrf)
507 vrf_id = pVrf->vrf_id;
508 else
509 vrf_id = VRF_DEFAULT;
d62a17ae 510 }
511
512 if (!strcmp(bfdst_str, ZEBRA_PTM_BFDSTATUS_DOWN_STR)) {
513 if_bfd_session_update(ifp, &dest_prefix, &src_prefix,
514 BFD_STATUS_DOWN, vrf_id);
515 } else {
516 if_bfd_session_update(ifp, &dest_prefix, &src_prefix,
517 BFD_STATUS_UP, vrf_id);
518 }
519
520 return 0;
c01acf98
DS
521}
522
d62a17ae 523static int zebra_ptm_handle_cbl_msg(void *arg, void *in_ctxt,
524 struct interface *ifp, char *cbl_str)
c01acf98 525{
d62a17ae 526 int send_linkup = 0;
527
528 if (IS_ZEBRA_DEBUG_EVENT)
529 zlog_debug("%s: Recv Port [%s] cbl status [%s]", __func__,
530 ifp->name, cbl_str);
531
532 if (!strcmp(cbl_str, ZEBRA_PTM_PASS_STR)
533 && (ifp->ptm_status != ZEBRA_PTM_STATUS_UP)) {
534
535 if (ifp->ptm_status == ZEBRA_PTM_STATUS_DOWN)
536 send_linkup = 1;
537 ifp->ptm_status = ZEBRA_PTM_STATUS_UP;
538 if (ifp->ptm_enable && if_is_no_ptm_operative(ifp)
539 && send_linkup)
8b48cdb9 540 if_up(ifp, true);
d62a17ae 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))
545 if_down(ifp);
546 }
547
548 return 0;
b255a4b1 549}
950bd436 550
b255a4b1 551/*
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.
554 *
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.
562 *
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.
566 *
567 */
d62a17ae 568static int zebra_ptm_handle_msg_cb(void *arg, void *in_ctxt)
b255a4b1 569{
d62a17ae 570 struct interface *ifp = NULL;
571 char port_str[128];
572 char cbl_str[32];
573 char cmd_status_str[32];
574
575 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_CMD_STATUS_STR,
576 cmd_status_str);
577
578 /* Drop command response messages */
579 if (cmd_status_str[0] != '\0') {
580 return 0;
581 }
582
583 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_PORT_STR, port_str);
584
585 if (port_str[0] == '\0') {
586 zlog_debug("%s: Key %s not found in PTM msg", __func__,
587 ZEBRA_PTM_PORT_STR);
588 return -1;
589 }
590
591 if (strcmp(ZEBRA_PTM_INVALID_PORT_NAME, port_str)) {
f13fdc67
IR
592 struct vrf *vrf;
593 int count = 0;
594
595 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
77712f66 596 ifp = if_lookup_by_name_vrf(port_str, vrf);
f13fdc67
IR
597 if (ifp) {
598 count++;
599 if (!vrf_is_backend_netns())
600 break;
601 }
602 }
d62a17ae 603
604 if (!ifp) {
e914ccbe 605 flog_warn(EC_ZEBRA_UNKNOWN_INTERFACE,
9df414fe 606 "%s: %s not found in interface list",
43e52561 607 __func__, port_str);
d62a17ae 608 return -1;
609 }
f13fdc67
IR
610 if (count > 1) {
611 flog_warn(EC_ZEBRA_UNKNOWN_INTERFACE,
612 "%s: multiple interface with name %s",
613 __func__, port_str);
614 return -1;
615 }
d62a17ae 616 }
617
618 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_CBL_STR, cbl_str);
619
620 if (cbl_str[0] == '\0') {
621 return zebra_ptm_handle_bfd_msg(arg, in_ctxt, ifp);
622 } else {
623 if (ifp) {
624 return zebra_ptm_handle_cbl_msg(arg, in_ctxt, ifp,
625 cbl_str);
626 } else {
627 return -1;
628 }
629 }
c01acf98
DS
630}
631
e6685141 632void zebra_ptm_sock_read(struct event *thread)
c01acf98 633{
2e1cc436 634 int sock;
d62a17ae 635 int rc;
636
637 errno = 0;
638 sock = THREAD_FD(thread);
639
640 if (sock == -1)
cc9f21da 641 return;
d62a17ae 642
643 /* PTM communicates in CSV format */
2e1cc436 644 do {
d62a17ae 645 rc = ptm_lib_process_msg(ptm_hdl, sock, ptm_cb.in_data,
646 ZEBRA_PTM_MAX_SOCKBUF, NULL);
2e1cc436 647 } while (rc > 0);
d62a17ae 648
2e1cc436
A
649 if (((rc == 0) && !errno)
650 || (errno && (errno != EWOULDBLOCK) && (errno != EAGAIN))) {
450971aa 651 flog_err_sys(EC_LIB_SOCKET,
9df414fe
QY
652 "%s routing socket error: %s(%d) bytes %d",
653 __func__, safe_strerror(errno), errno, rc);
2e1cc436
A
654
655 close(ptm_cb.ptm_sock);
656 ptm_cb.ptm_sock = -1;
657 zebra_ptm_reset_status(0);
658 ptm_cb.t_timer = NULL;
907a2395
DS
659 event_add_timer(zrouter.master, zebra_ptm_connect, NULL,
660 ptm_cb.reconnect_time, &ptm_cb.t_timer);
cc9f21da 661 return;
d62a17ae 662 }
663
664 ptm_cb.t_read = NULL;
907a2395
DS
665 event_add_read(zrouter.master, zebra_ptm_sock_read, NULL,
666 ptm_cb.ptm_sock, &ptm_cb.t_read);
c43ed2e4
DS
667}
668
669/* BFD peer/dst register/update */
89f4e507 670void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS)
c43ed2e4 671{
d62a17ae 672 struct stream *s;
673 struct prefix src_p;
674 struct prefix dst_p;
d7c0a89a
QY
675 uint8_t multi_hop;
676 uint8_t multi_hop_cnt;
677 uint8_t detect_mul;
d62a17ae 678 unsigned int min_rx_timer;
679 unsigned int min_tx_timer;
680 char if_name[INTERFACE_NAMSIZ];
d7c0a89a 681 uint8_t len;
d62a17ae 682 void *out_ctxt;
683 char buf[INET6_ADDRSTRLEN];
684 char tmp_buf[64];
685 int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
686 unsigned int pid;
9beff0bd 687 uint8_t cbit_set;
d62a17ae 688
89f4e507 689 if (hdr->command == ZEBRA_BFD_DEST_UPDATE)
d62a17ae 690 client->bfd_peer_upd8_cnt++;
691 else
692 client->bfd_peer_add_cnt++;
693
694 if (IS_ZEBRA_DEBUG_EVENT)
695 zlog_debug("bfd_dst_register msg from client %s: length=%d",
89f4e507 696 zebra_route_string(client->proto), hdr->length);
d62a17ae 697
698 if (ptm_cb.ptm_sock == -1) {
699 ptm_cb.t_timer = NULL;
907a2395
DS
700 event_add_timer(zrouter.master, zebra_ptm_connect, NULL,
701 ptm_cb.reconnect_time, &ptm_cb.t_timer);
8068a649 702 return;
d62a17ae 703 }
704
705 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
772270f3 706 snprintf(tmp_buf, sizeof(tmp_buf), "%s", ZEBRA_PTM_BFD_START_CMD);
d62a17ae 707 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
772270f3
QY
708 snprintf(tmp_buf, sizeof(tmp_buf), "%s",
709 zebra_route_string(client->proto));
d62a17ae 710 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
711 tmp_buf);
712
1002497a 713 s = msg;
d62a17ae 714
ec93aa12 715 STREAM_GETL(s, pid);
772270f3 716 snprintf(tmp_buf, sizeof(tmp_buf), "%d", pid);
d62a17ae 717 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD,
718 tmp_buf);
719
ec93aa12 720 STREAM_GETW(s, dst_p.family);
d62a17ae 721
722 if (dst_p.family == AF_INET)
723 dst_p.prefixlen = IPV4_MAX_BYTELEN;
724 else
725 dst_p.prefixlen = IPV6_MAX_BYTELEN;
726
ec93aa12 727 STREAM_GET(&dst_p.u.prefix, s, dst_p.prefixlen);
d62a17ae 728 if (dst_p.family == AF_INET) {
729 inet_ntop(AF_INET, &dst_p.u.prefix4, buf, sizeof(buf));
730 ptm_lib_append_msg(ptm_hdl, out_ctxt,
731 ZEBRA_PTM_BFD_DST_IP_FIELD, buf);
732 } else {
733 inet_ntop(AF_INET6, &dst_p.u.prefix6, buf, sizeof(buf));
734 ptm_lib_append_msg(ptm_hdl, out_ctxt,
735 ZEBRA_PTM_BFD_DST_IP_FIELD, buf);
736 }
737
ec93aa12 738 STREAM_GETL(s, min_rx_timer);
772270f3 739 snprintf(tmp_buf, sizeof(tmp_buf), "%d", min_rx_timer);
d62a17ae 740 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_MIN_RX_FIELD,
741 tmp_buf);
ec93aa12 742 STREAM_GETL(s, min_tx_timer);
772270f3 743 snprintf(tmp_buf, sizeof(tmp_buf), "%d", min_tx_timer);
d62a17ae 744 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_MIN_TX_FIELD,
745 tmp_buf);
ec93aa12 746 STREAM_GETC(s, detect_mul);
772270f3 747 snprintf(tmp_buf, sizeof(tmp_buf), "%d", detect_mul);
d62a17ae 748 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_DETECT_MULT_FIELD,
749 tmp_buf);
750
ec93aa12 751 STREAM_GETC(s, multi_hop);
d62a17ae 752 if (multi_hop) {
772270f3 753 snprintf(tmp_buf, sizeof(tmp_buf), "%d", 1);
d62a17ae 754 ptm_lib_append_msg(ptm_hdl, out_ctxt,
755 ZEBRA_PTM_BFD_MULTI_HOP_FIELD, tmp_buf);
ec93aa12 756 STREAM_GETW(s, src_p.family);
d62a17ae 757
758 if (src_p.family == AF_INET)
759 src_p.prefixlen = IPV4_MAX_BYTELEN;
760 else
761 src_p.prefixlen = IPV6_MAX_BYTELEN;
762
ec93aa12 763 STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen);
d62a17ae 764 if (src_p.family == AF_INET) {
765 inet_ntop(AF_INET, &src_p.u.prefix4, buf, sizeof(buf));
766 ptm_lib_append_msg(ptm_hdl, out_ctxt,
767 ZEBRA_PTM_BFD_SRC_IP_FIELD, buf);
768 } else {
769 inet_ntop(AF_INET6, &src_p.u.prefix6, buf, sizeof(buf));
770 ptm_lib_append_msg(ptm_hdl, out_ctxt,
771 ZEBRA_PTM_BFD_SRC_IP_FIELD, buf);
772 }
773
ec93aa12 774 STREAM_GETC(s, multi_hop_cnt);
772270f3 775 snprintf(tmp_buf, sizeof(tmp_buf), "%d", multi_hop_cnt);
d62a17ae 776 ptm_lib_append_msg(ptm_hdl, out_ctxt,
777 ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD, tmp_buf);
778
779 if (zvrf_id(zvrf) != VRF_DEFAULT)
780 ptm_lib_append_msg(ptm_hdl, out_ctxt,
781 ZEBRA_PTM_BFD_VRF_NAME_FIELD,
782 zvrf_name(zvrf));
783 } else {
784 if (dst_p.family == AF_INET6) {
ec93aa12 785 STREAM_GETW(s, src_p.family);
d62a17ae 786
787 if (src_p.family == AF_INET)
788 src_p.prefixlen = IPV4_MAX_BYTELEN;
789 else
790 src_p.prefixlen = IPV6_MAX_BYTELEN;
791
ec93aa12 792 STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen);
d62a17ae 793 if (src_p.family == AF_INET) {
794 inet_ntop(AF_INET, &src_p.u.prefix4, buf,
795 sizeof(buf));
796 ptm_lib_append_msg(ptm_hdl, out_ctxt,
797 ZEBRA_PTM_BFD_SRC_IP_FIELD,
798 buf);
799 } else {
800 inet_ntop(AF_INET6, &src_p.u.prefix6, buf,
801 sizeof(buf));
802 ptm_lib_append_msg(ptm_hdl, out_ctxt,
803 ZEBRA_PTM_BFD_SRC_IP_FIELD,
804 buf);
805 }
806 }
ec93aa12
DS
807 STREAM_GETC(s, len);
808 STREAM_GET(if_name, s, len);
d62a17ae 809 if_name[len] = '\0';
810
811 ptm_lib_append_msg(ptm_hdl, out_ctxt,
812 ZEBRA_PTM_BFD_IFNAME_FIELD, if_name);
813 }
9beff0bd 814 STREAM_GETC(s, cbit_set);
772270f3 815 snprintf(tmp_buf, sizeof(tmp_buf), "%d", cbit_set);
9beff0bd
PG
816 ptm_lib_append_msg(ptm_hdl, out_ctxt,
817 ZEBRA_PTM_BFD_CBIT_FIELD, tmp_buf);
d62a17ae 818
772270f3 819 snprintf(tmp_buf, sizeof(tmp_buf), "%d", 1);
d62a17ae 820 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEND_EVENT,
821 tmp_buf);
822
823 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
824
825 if (IS_ZEBRA_DEBUG_SEND)
826 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
827 ptm_cb.out_data);
828 zebra_ptm_send_message(ptm_cb.out_data, data_len);
ec93aa12 829
8068a649 830 return;
9cc46248 831
ec93aa12 832stream_failure:
a928d464 833 ptm_lib_cleanup_msg(ptm_hdl, out_ctxt);
c43ed2e4
DS
834}
835
836/* BFD peer/dst deregister */
89f4e507 837void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS)
c43ed2e4 838{
d62a17ae 839 struct stream *s;
840 struct prefix src_p;
841 struct prefix dst_p;
d7c0a89a 842 uint8_t multi_hop;
d62a17ae 843 char if_name[INTERFACE_NAMSIZ];
d7c0a89a 844 uint8_t len;
d62a17ae 845 char buf[INET6_ADDRSTRLEN];
846 char tmp_buf[64];
847 int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
848 void *out_ctxt;
849 unsigned int pid;
850
851 client->bfd_peer_del_cnt++;
852
853 if (IS_ZEBRA_DEBUG_EVENT)
854 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
89f4e507 855 zebra_route_string(client->proto), hdr->length);
d62a17ae 856
857 if (ptm_cb.ptm_sock == -1) {
858 ptm_cb.t_timer = NULL;
907a2395
DS
859 event_add_timer(zrouter.master, zebra_ptm_connect, NULL,
860 ptm_cb.reconnect_time, &ptm_cb.t_timer);
8068a649 861 return;
d62a17ae 862 }
863
864 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
865
772270f3 866 snprintf(tmp_buf, sizeof(tmp_buf), "%s", ZEBRA_PTM_BFD_STOP_CMD);
d62a17ae 867 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
868
772270f3
QY
869 snprintf(tmp_buf, sizeof(tmp_buf), "%s",
870 zebra_route_string(client->proto));
d62a17ae 871 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
872 tmp_buf);
873
1002497a 874 s = msg;
d62a17ae 875
ec93aa12 876 STREAM_GETL(s, pid);
772270f3 877 snprintf(tmp_buf, sizeof(tmp_buf), "%d", pid);
d62a17ae 878 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD,
879 tmp_buf);
880
ec93aa12 881 STREAM_GETW(s, dst_p.family);
d62a17ae 882
883 if (dst_p.family == AF_INET)
884 dst_p.prefixlen = IPV4_MAX_BYTELEN;
885 else
886 dst_p.prefixlen = IPV6_MAX_BYTELEN;
887
ec93aa12 888 STREAM_GET(&dst_p.u.prefix, s, dst_p.prefixlen);
d62a17ae 889 if (dst_p.family == AF_INET)
890 inet_ntop(AF_INET, &dst_p.u.prefix4, buf, sizeof(buf));
891 else
892 inet_ntop(AF_INET6, &dst_p.u.prefix6, buf, sizeof(buf));
893 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_DST_IP_FIELD, buf);
894
895
ec93aa12 896 STREAM_GETC(s, multi_hop);
d62a17ae 897 if (multi_hop) {
772270f3 898 snprintf(tmp_buf, sizeof(tmp_buf), "%d", 1);
d62a17ae 899 ptm_lib_append_msg(ptm_hdl, out_ctxt,
900 ZEBRA_PTM_BFD_MULTI_HOP_FIELD, tmp_buf);
901
ec93aa12 902 STREAM_GETW(s, src_p.family);
d62a17ae 903
904 if (src_p.family == AF_INET)
905 src_p.prefixlen = IPV4_MAX_BYTELEN;
906 else
907 src_p.prefixlen = IPV6_MAX_BYTELEN;
908
ec93aa12 909 STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen);
d62a17ae 910 if (src_p.family == AF_INET)
911 inet_ntop(AF_INET, &src_p.u.prefix4, buf, sizeof(buf));
912 else
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);
916
917 if (zvrf_id(zvrf) != VRF_DEFAULT)
918 ptm_lib_append_msg(ptm_hdl, out_ctxt,
919 ZEBRA_PTM_BFD_VRF_NAME_FIELD,
920 zvrf_name(zvrf));
921 } else {
922 if (dst_p.family == AF_INET6) {
ec93aa12 923 STREAM_GETW(s, src_p.family);
d62a17ae 924
925 if (src_p.family == AF_INET)
926 src_p.prefixlen = IPV4_MAX_BYTELEN;
927 else
928 src_p.prefixlen = IPV6_MAX_BYTELEN;
929
ec93aa12 930 STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen);
d62a17ae 931 if (src_p.family == AF_INET) {
932 inet_ntop(AF_INET, &src_p.u.prefix4, buf,
933 sizeof(buf));
934 ptm_lib_append_msg(ptm_hdl, out_ctxt,
935 ZEBRA_PTM_BFD_SRC_IP_FIELD,
936 buf);
937 } else {
938 inet_ntop(AF_INET6, &src_p.u.prefix6, buf,
939 sizeof(buf));
940 ptm_lib_append_msg(ptm_hdl, out_ctxt,
941 ZEBRA_PTM_BFD_SRC_IP_FIELD,
942 buf);
943 }
944 }
945
ec93aa12
DS
946 STREAM_GETC(s, len);
947 STREAM_GET(if_name, s, len);
d62a17ae 948 if_name[len] = '\0';
949
950 ptm_lib_append_msg(ptm_hdl, out_ctxt,
951 ZEBRA_PTM_BFD_IFNAME_FIELD, if_name);
952 }
953
954 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
955 if (IS_ZEBRA_DEBUG_SEND)
956 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
957 ptm_cb.out_data);
958
959 zebra_ptm_send_message(ptm_cb.out_data, data_len);
ec93aa12 960
8068a649 961 return;
9cc46248 962
ec93aa12 963stream_failure:
a928d464 964 ptm_lib_cleanup_msg(ptm_hdl, out_ctxt);
c01acf98 965}
c8ed14dd 966
055c4dfc 967/* BFD client register */
89f4e507 968void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS)
055c4dfc 969{
d62a17ae 970 struct stream *s;
971 unsigned int pid;
9cc46248 972 void *out_ctxt = NULL;
d62a17ae 973 char tmp_buf[64];
974 int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
055c4dfc 975
d62a17ae 976 client->bfd_client_reg_cnt++;
055c4dfc 977
d62a17ae 978 if (IS_ZEBRA_DEBUG_EVENT)
979 zlog_debug("bfd_client_register msg from client %s: length=%d",
89f4e507 980 zebra_route_string(client->proto), hdr->length);
055c4dfc 981
1002497a 982 s = msg;
ec93aa12 983 STREAM_GETL(s, pid);
bf6e101c 984
d62a17ae 985 if (ptm_cb.ptm_sock == -1) {
986 ptm_cb.t_timer = NULL;
907a2395
DS
987 event_add_timer(zrouter.master, zebra_ptm_connect, NULL,
988 ptm_cb.reconnect_time, &ptm_cb.t_timer);
8068a649 989 return;
d62a17ae 990 }
055c4dfc 991
d62a17ae 992 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
055c4dfc 993
772270f3 994 snprintf(tmp_buf, sizeof(tmp_buf), "%s", ZEBRA_PTM_BFD_CLIENT_REG_CMD);
d62a17ae 995 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
055c4dfc 996
772270f3
QY
997 snprintf(tmp_buf, sizeof(tmp_buf), "%s",
998 zebra_route_string(client->proto));
d62a17ae 999 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
1000 tmp_buf);
055c4dfc 1001
772270f3 1002 snprintf(tmp_buf, sizeof(tmp_buf), "%d", pid);
d62a17ae 1003 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD,
1004 tmp_buf);
055c4dfc 1005
d62a17ae 1006 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
055c4dfc 1007
d62a17ae 1008 if (IS_ZEBRA_DEBUG_SEND)
1009 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
1010 ptm_cb.out_data);
1011 zebra_ptm_send_message(ptm_cb.out_data, data_len);
2376c3f2 1012
d62a17ae 1013 SET_FLAG(ptm_cb.client_flags[client->proto],
1014 ZEBRA_PTM_BFD_CLIENT_FLAG_REG);
9cc46248 1015
8068a649 1016 return;
9cc46248 1017
ec93aa12 1018stream_failure:
6447dbb3
DS
1019 /*
1020 * IF we ever add more STREAM_GETXXX functions after the out_ctxt
1021 * is allocated then we need to add this code back in
1022 *
1023 * if (out_ctxt)
1024 * ptm_lib_cleanup_msg(ptm_hdl, out_ctxt);
1025 */
8068a649 1026 return;
055c4dfc 1027}
1028
567b877d 1029/* BFD client deregister */
453844ab 1030int zebra_ptm_bfd_client_deregister(struct zserv *client)
567b877d 1031{
453844ab 1032 uint8_t proto = client->proto;
d62a17ae 1033 void *out_ctxt;
1034 char tmp_buf[64];
1035 int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
567b877d 1036
4943f243 1037 if (!IS_BFD_ENABLED_PROTOCOL(proto))
453844ab 1038 return 0;
567b877d 1039
d62a17ae 1040 if (IS_ZEBRA_DEBUG_EVENT)
9df414fe
QY
1041 zlog_debug("bfd_client_deregister msg for client %s",
1042 zebra_route_string(proto));
567b877d 1043
d62a17ae 1044 if (ptm_cb.ptm_sock == -1) {
1045 ptm_cb.t_timer = NULL;
907a2395
DS
1046 event_add_timer(zrouter.master, zebra_ptm_connect, NULL,
1047 ptm_cb.reconnect_time, &ptm_cb.t_timer);
453844ab 1048 return 0;
d62a17ae 1049 }
567b877d 1050
d62a17ae 1051 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
567b877d 1052
772270f3
QY
1053 snprintf(tmp_buf, sizeof(tmp_buf), "%s",
1054 ZEBRA_PTM_BFD_CLIENT_DEREG_CMD);
d62a17ae 1055 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
567b877d 1056
772270f3 1057 snprintf(tmp_buf, sizeof(tmp_buf), "%s", zebra_route_string(proto));
d62a17ae 1058 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
1059 tmp_buf);
567b877d 1060
d62a17ae 1061 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
567b877d 1062
d62a17ae 1063 if (IS_ZEBRA_DEBUG_SEND)
1064 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
1065 ptm_cb.out_data);
2376c3f2 1066
d62a17ae 1067 zebra_ptm_send_message(ptm_cb.out_data, data_len);
1068 UNSET_FLAG(ptm_cb.client_flags[proto], ZEBRA_PTM_BFD_CLIENT_FLAG_REG);
453844ab
QY
1069
1070 return 0;
567b877d 1071}
1072
d62a17ae 1073int zebra_ptm_get_enable_state(void)
c8ed14dd 1074{
d62a17ae 1075 return ptm_cb.ptm_enable;
c8ed14dd 1076}
950bd436 1077
1078/*
1079 * zebra_ptm_get_status_str - Convert status to a display string.
1080 */
d62a17ae 1081static const char *zebra_ptm_get_status_str(int status)
950bd436 1082{
d62a17ae 1083 switch (status) {
1084 case ZEBRA_PTM_STATUS_DOWN:
1085 return "fail";
1086 case ZEBRA_PTM_STATUS_UP:
1087 return "pass";
1088 case ZEBRA_PTM_STATUS_UNKNOWN:
1089 default:
1090 return "n/a";
1091 }
950bd436 1092}
1093
c15dc24f
RW
1094void zebra_ptm_show_status(struct vty *vty, json_object *json,
1095 struct interface *ifp)
950bd436 1096{
c15dc24f
RW
1097 const char *status;
1098
1099 if (ifp->ptm_enable)
1100 status = zebra_ptm_get_status_str(ifp->ptm_status);
1101 else
1102 status = "disabled";
1103
1104 if (json)
1105 json_object_string_add(json, "ptmStatus", status);
1106 else
1107 vty_out(vty, " PTM status: %s\n", status);
950bd436 1108}
1109
d62a17ae 1110void zebra_ptm_send_status_req(void)
950bd436 1111{
d62a17ae 1112 void *out_ctxt;
1113 int len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
1114
1115 if (ptm_cb.ptm_enable) {
1116 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL,
1117 &out_ctxt);
1118 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR,
1119 ZEBRA_PTM_GET_STATUS_CMD);
1120 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &len);
1121
1122 zebra_ptm_send_message(ptm_cb.out_data, len);
1123 }
950bd436 1124}
1125
d62a17ae 1126void zebra_ptm_reset_status(int ptm_disable)
950bd436 1127{
d62a17ae 1128 struct vrf *vrf;
d62a17ae 1129 struct interface *ifp;
1130 int send_linkup;
1131
a2addae8 1132 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
451fda4f 1133 FOR_ALL_INTERFACES (vrf, ifp) {
a2addae8
RW
1134 send_linkup = 0;
1135 if (ifp->ptm_enable) {
1136 if (!if_is_operative(ifp))
1137 send_linkup = 1;
1138
1139 if (ptm_disable)
1140 ifp->ptm_enable =
1141 ZEBRA_IF_PTM_ENABLE_OFF;
1142 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
1143
1144 if (if_is_operative(ifp) && send_linkup) {
1145 if (IS_ZEBRA_DEBUG_EVENT)
1146 zlog_debug(
1147 "%s: Bringing up interface %s",
1148 __func__, ifp->name);
8b48cdb9 1149 if_up(ifp, true);
a2addae8 1150 }
d62a17ae 1151 }
1152 }
950bd436 1153}
986aa00f 1154
d62a17ae 1155void zebra_ptm_if_init(struct zebra_if *zebra_ifp)
986aa00f 1156{
d62a17ae 1157 zebra_ifp->ptm_enable = ZEBRA_IF_PTM_ENABLE_UNSPEC;
986aa00f 1158}
1159
d62a17ae 1160void zebra_ptm_if_set_ptm_state(struct interface *ifp,
1161 struct zebra_if *zebra_ifp)
986aa00f 1162{
d62a17ae 1163 if (zebra_ifp && zebra_ifp->ptm_enable != ZEBRA_IF_PTM_ENABLE_UNSPEC)
1164 ifp->ptm_enable = zebra_ifp->ptm_enable;
986aa00f 1165}
1166
d62a17ae 1167void zebra_ptm_if_write(struct vty *vty, struct zebra_if *zebra_ifp)
986aa00f 1168{
d62a17ae 1169 if (zebra_ifp->ptm_enable == ZEBRA_IF_PTM_ENABLE_OFF)
1170 vty_out(vty, " no ptm-enable\n");
986aa00f 1171}
d3af6147
RZ
1172
1173#else /* HAVE_BFDD */
1174
d3af6147
RZ
1175/*
1176 * Data structures.
1177 */
1178struct ptm_process {
1179 struct zserv *pp_zs;
1180 pid_t pp_pid;
1181
1182 TAILQ_ENTRY(ptm_process) pp_entry;
1183};
1184TAILQ_HEAD(ppqueue, ptm_process) ppqueue;
1185
1186DEFINE_MTYPE_STATIC(ZEBRA, ZEBRA_PTM_BFD_PROCESS,
1187 "PTM BFD process registration table.");
1188
1189/*
1190 * Prototypes.
1191 */
1192static struct ptm_process *pp_new(pid_t pid, struct zserv *zs);
1193static struct ptm_process *pp_lookup_byzs(struct zserv *zs);
1194static void pp_free(struct ptm_process *pp);
788378fe 1195static void pp_free_all(void);
d3af6147
RZ
1196
1197static void zebra_ptm_send_bfdd(struct stream *msg);
1198static void zebra_ptm_send_clients(struct stream *msg);
1199static int _zebra_ptm_bfd_client_deregister(struct zserv *zs);
45b000d0
PG
1200static void _zebra_ptm_reroute(struct zserv *zs, struct zebra_vrf *zvrf,
1201 struct stream *msg, uint32_t command);
d3af6147
RZ
1202
1203
1204/*
1205 * Process PID registration.
1206 */
1207static struct ptm_process *pp_new(pid_t pid, struct zserv *zs)
1208{
1209 struct ptm_process *pp;
1210
1211#ifdef PTM_DEBUG
1212 /* Sanity check: more than one client can't have the same PID. */
1213 TAILQ_FOREACH(pp, &ppqueue, pp_entry) {
1214 if (pp->pp_pid == pid && pp->pp_zs != zs)
1215 zlog_err("%s:%d pid and client pointer doesn't match",
1216 __FILE__, __LINE__);
1217 }
1218#endif /* PTM_DEBUG */
1219
1220 /* Lookup for duplicates. */
1221 pp = pp_lookup_byzs(zs);
1222 if (pp != NULL)
1223 return pp;
1224
1225 /* Allocate and register new process. */
1226 pp = XCALLOC(MTYPE_ZEBRA_PTM_BFD_PROCESS, sizeof(*pp));
d3af6147
RZ
1227
1228 pp->pp_pid = pid;
1229 pp->pp_zs = zs;
1230 TAILQ_INSERT_HEAD(&ppqueue, pp, pp_entry);
1231
1232 return pp;
1233}
1234
1235static struct ptm_process *pp_lookup_byzs(struct zserv *zs)
1236{
1237 struct ptm_process *pp;
1238
1239 TAILQ_FOREACH(pp, &ppqueue, pp_entry) {
1240 if (pp->pp_zs != zs)
1241 continue;
1242
1243 break;
1244 }
1245
1246 return pp;
1247}
1248
1249static void pp_free(struct ptm_process *pp)
1250{
1251 if (pp == NULL)
1252 return;
1253
1254 TAILQ_REMOVE(&ppqueue, pp, pp_entry);
1255 XFREE(MTYPE_ZEBRA_PTM_BFD_PROCESS, pp);
1256}
1257
788378fe
RZ
1258static void pp_free_all(void)
1259{
1260 struct ptm_process *pp;
1261
1262 while (!TAILQ_EMPTY(&ppqueue)) {
1263 pp = TAILQ_FIRST(&ppqueue);
1264 pp_free(pp);
1265 }
1266}
1267
d3af6147
RZ
1268
1269/*
1270 * Use the FRR's internal daemon implementation.
1271 */
1272static void zebra_ptm_send_bfdd(struct stream *msg)
1273{
1274 struct listnode *node;
1275 struct zserv *client;
1276 struct stream *msgc;
1277
1278 /* Create copy for replication. */
1279 msgc = stream_dup(msg);
d3af6147
RZ
1280
1281 /* Send message to all running BFDd daemons. */
161e9ab7 1282 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
d3af6147
RZ
1283 if (client->proto != ZEBRA_ROUTE_BFD)
1284 continue;
1285
1286 zserv_send_message(client, msg);
1287
1288 /* Allocate more messages. */
1289 msg = stream_dup(msgc);
d3af6147
RZ
1290 }
1291
1292 stream_free(msgc);
011a7139 1293 stream_free(msg);
d3af6147
RZ
1294}
1295
1296static void zebra_ptm_send_clients(struct stream *msg)
1297{
1298 struct listnode *node;
1299 struct zserv *client;
1300 struct stream *msgc;
1301
1302 /* Create copy for replication. */
1303 msgc = stream_dup(msg);
d3af6147
RZ
1304
1305 /* Send message to all running client daemons. */
161e9ab7 1306 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
4943f243 1307 if (!IS_BFD_ENABLED_PROTOCOL(client->proto))
d3af6147 1308 continue;
d3af6147
RZ
1309
1310 zserv_send_message(client, msg);
1311
1312 /* Allocate more messages. */
1313 msg = stream_dup(msgc);
d3af6147
RZ
1314 }
1315
1316 stream_free(msgc);
011a7139 1317 stream_free(msg);
d3af6147
RZ
1318}
1319
1320static int _zebra_ptm_bfd_client_deregister(struct zserv *zs)
1321{
1322 struct stream *msg;
1323 struct ptm_process *pp;
1324
4943f243 1325 if (!IS_BFD_ENABLED_PROTOCOL(zs->proto))
d3af6147
RZ
1326 return 0;
1327
d3af6147
RZ
1328 /* Find daemon pid by zebra connection pointer. */
1329 pp = pp_lookup_byzs(zs);
1330 if (pp == NULL) {
1331 zlog_err("%s:%d failed to find process pid registration",
1332 __FILE__, __LINE__);
1333 return -1;
1334 }
1335
1336 /* Generate, send message and free() daemon related data. */
1337 msg = stream_new(ZEBRA_MAX_PACKET_SIZ);
1338 if (msg == NULL) {
9df414fe 1339 zlog_debug("%s: not enough memory", __func__);
d3af6147
RZ
1340 return 0;
1341 }
1342
1343 /*
5498a9f1 1344 * The message type will be ZEBRA_BFD_DEST_REPLAY so we can use only
d3af6147
RZ
1345 * one callback at the `bfdd` side, however the real command
1346 * number will be included right after the zebra header.
1347 */
1348 zclient_create_header(msg, ZEBRA_BFD_DEST_REPLAY, 0);
1349 stream_putl(msg, ZEBRA_BFD_CLIENT_DEREGISTER);
1350
1351 /* Put process PID. */
1352 stream_putl(msg, pp->pp_pid);
1353
1354 /* Update the data pointers. */
1355 stream_putw_at(msg, 0, stream_get_endp(msg));
1356
1357 zebra_ptm_send_bfdd(msg);
1358
1359 pp_free(pp);
1360
1361 return 0;
1362}
1363
1364void zebra_ptm_init(void)
1365{
1366 /* Initialize the ptm process information list. */
1367 TAILQ_INIT(&ppqueue);
1368
1369 /*
1370 * Send deregistration messages to BFD daemon when some other
1371 * daemon closes. This will help avoid sending daemons
1372 * unnecessary notification messages.
1373 */
1374 hook_register(zserv_client_close, _zebra_ptm_bfd_client_deregister);
1375}
1376
788378fe
RZ
1377void zebra_ptm_finish(void)
1378{
1379 /* Remove the client disconnect hook and free all memory. */
1380 hook_unregister(zserv_client_close, _zebra_ptm_bfd_client_deregister);
1381 pp_free_all();
1382}
1383
d3af6147
RZ
1384
1385/*
1386 * Message handling.
1387 */
45b000d0
PG
1388static void _zebra_ptm_reroute(struct zserv *zs, struct zebra_vrf *zvrf,
1389 struct stream *msg, uint32_t command)
d3af6147
RZ
1390{
1391 struct stream *msgc;
ab12ca85 1392 char buf[ZEBRA_MAX_PACKET_SIZ];
d3af6147
RZ
1393 pid_t ppid;
1394
ab12ca85 1395 /* Create BFD header */
d3af6147 1396 msgc = stream_new(ZEBRA_MAX_PACKET_SIZ);
45b000d0 1397 zclient_create_header(msgc, ZEBRA_BFD_DEST_REPLAY, zvrf->vrf->vrf_id);
d3af6147
RZ
1398 stream_putl(msgc, command);
1399
ab12ca85
QY
1400 if (STREAM_READABLE(msg) > STREAM_WRITEABLE(msgc)) {
1401 zlog_warn("Cannot fit extended BFD header plus original message contents into ZAPI packet; dropping message");
1402 goto stream_failure;
1403 }
1404
1405 /* Copy original message, excluding header, into new message */
1406 stream_get_from(buf, msg, stream_get_getp(msg), STREAM_READABLE(msg));
1407 stream_put(msgc, buf, STREAM_READABLE(msg));
1408
1409 /* Update length field */
1410 stream_putw_at(msgc, 0, STREAM_READABLE(msgc));
d3af6147
RZ
1411
1412 zebra_ptm_send_bfdd(msgc);
011a7139 1413 msgc = NULL;
d3af6147
RZ
1414
1415 /* Registrate process PID for shutdown hook. */
1416 STREAM_GETL(msg, ppid);
1417 pp_new(ppid, zs);
1418
1419 return;
1420
1421stream_failure:
011a7139
QY
1422 if (msgc)
1423 stream_free(msgc);
d3af6147
RZ
1424 zlog_err("%s:%d failed to registrate client pid", __FILE__, __LINE__);
1425}
1426
1427void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS)
1428{
1429 if (IS_ZEBRA_DEBUG_EVENT)
1430 zlog_debug("bfd_dst_register msg from client %s: length=%d",
1431 zebra_route_string(client->proto), hdr->length);
1432
45b000d0 1433 _zebra_ptm_reroute(client, zvrf, msg, ZEBRA_BFD_DEST_REGISTER);
d3af6147
RZ
1434}
1435
1436void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS)
1437{
1438 if (IS_ZEBRA_DEBUG_EVENT)
1439 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
1440 zebra_route_string(client->proto), hdr->length);
1441
45b000d0 1442 _zebra_ptm_reroute(client, zvrf, msg, ZEBRA_BFD_DEST_DEREGISTER);
d3af6147
RZ
1443}
1444
1445void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS)
1446{
1447 if (IS_ZEBRA_DEBUG_EVENT)
1448 zlog_debug("bfd_client_register msg from client %s: length=%d",
1449 zebra_route_string(client->proto), hdr->length);
1450
45b000d0 1451 _zebra_ptm_reroute(client, zvrf, msg, ZEBRA_BFD_CLIENT_REGISTER);
d3af6147
RZ
1452}
1453
1454void zebra_ptm_bfd_dst_replay(ZAPI_HANDLER_ARGS)
1455{
1456 struct stream *msgc;
1457 size_t zmsglen, zhdrlen;
971532e2 1458 uint32_t cmd;
d3af6147
RZ
1459
1460 /*
1461 * NOTE:
1462 * Replay messages with HAVE_BFDD are meant to be replayed to
1463 * the client daemons. These messages are composed and
1464 * originated from the `bfdd` daemon.
1465 */
1466 if (IS_ZEBRA_DEBUG_EVENT)
1467 zlog_debug("bfd_dst_update msg from client %s: length=%d",
1468 zebra_route_string(client->proto), hdr->length);
1469
971532e2
RZ
1470 /*
1471 * Client messages must be re-routed, otherwise do the `bfdd`
1472 * special treatment.
1473 */
1474 if (client->proto != ZEBRA_ROUTE_BFD) {
45b000d0 1475 _zebra_ptm_reroute(client, zvrf, msg, ZEBRA_BFD_DEST_REPLAY);
971532e2
RZ
1476 return;
1477 }
1478
1479 /* Figure out if this is an DEST_UPDATE or DEST_REPLAY. */
1480 if (stream_getl2(msg, &cmd) == false) {
1481 zlog_err("%s: expected at least 4 bytes (command)", __func__);
1482 return;
1483 }
1484
d3af6147
RZ
1485 /*
1486 * Don't modify message in the zebra API. In order to do that we
1487 * need to allocate a new message stream and copy the message
1488 * provided by zebra.
1489 */
1490 msgc = stream_new(ZEBRA_MAX_PACKET_SIZ);
1491 if (msgc == NULL) {
9df414fe 1492 zlog_debug("%s: not enough memory", __func__);
d3af6147
RZ
1493 return;
1494 }
1495
1496 /* Calculate our header size plus the message contents. */
971532e2
RZ
1497 if (cmd != ZEBRA_BFD_DEST_REPLAY) {
1498 zhdrlen = ZEBRA_HEADER_SIZE;
1499 zmsglen = msg->endp - msg->getp;
1500 memcpy(msgc->data + zhdrlen, msg->data + msg->getp, zmsglen);
d3af6147 1501
971532e2
RZ
1502 zclient_create_header(msgc, cmd, zvrf_id(zvrf));
1503
1504 msgc->getp = 0;
1505 msgc->endp = zhdrlen + zmsglen;
1506 } else
1507 zclient_create_header(msgc, cmd, zvrf_id(zvrf));
d3af6147
RZ
1508
1509 /* Update the data pointers. */
d3af6147
RZ
1510 stream_putw_at(msgc, 0, stream_get_endp(msgc));
1511
1512 zebra_ptm_send_clients(msgc);
1513}
1514
1515/*
1516 * Unused functions.
1517 */
d3af6147
RZ
1518void zebra_ptm_if_init(struct zebra_if *zifp __attribute__((__unused__)))
1519{
1520 /* NOTHING */
1521}
1522
1523int zebra_ptm_get_enable_state(void)
1524{
5efdb801 1525 return 0;
d3af6147
RZ
1526}
1527
1528void zebra_ptm_show_status(struct vty *vty __attribute__((__unused__)),
c15dc24f 1529 json_object *json __attribute__((__unused__)),
d3af6147
RZ
1530 struct interface *ifp __attribute__((__unused__)))
1531{
1532 /* NOTHING */
1533}
1534
1535void zebra_ptm_write(struct vty *vty __attribute__((__unused__)))
1536{
1537 /* NOTHING */
1538}
1539
1540void zebra_ptm_if_write(struct vty *vty __attribute__((__unused__)),
1541 struct zebra_if *zifp __attribute__((__unused__)))
1542{
1543 /* NOTHING */
1544}
1545void zebra_ptm_if_set_ptm_state(struct interface *i __attribute__((__unused__)),
1546 struct zebra_if *zi __attribute__((__unused__)))
1547{
1548 /* NOTHING */
1549}
1550
1551#endif /* HAVE_BFDD */