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