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