]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_ptm.c
Merge pull request #2684 from netravnen/feature/bgpd/well-known-communities
[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>
d62a17ae 22#include <sys/un.h> /* for sockaddr_un */
244c1cdc 23#include <net/if.h>
c8ed14dd 24#include "vty.h"
244c1cdc
DS
25#include "zebra/zserv.h"
26#include "zebra/interface.h"
27#include "zebra/debug.h"
28#include "zebra/zebra_ptm.h"
29#include "if.h"
30#include "command.h"
c43ed2e4
DS
31#include "stream.h"
32#include "ptm_lib.h"
8f500a1c 33#include "network.h"
c8ed14dd 34#include "buffer.h"
c43ed2e4 35#include "zebra/zebra_ptm_redistribute.h"
68fe91d6 36#include "bfd.h"
d553294e 37#include "vrf.h"
7c551956
DS
38#include "rib.h"
39#include "zebra_vrf.h"
ae435b19 40#include "version.h"
244c1cdc
DS
41
42#define ZEBRA_PTM_RECONNECT_TIME_INITIAL 1 /* initial reconnect is 1s */
43#define ZEBRA_PTM_RECONNECT_TIME_MAX 300
c01acf98 44
c01acf98 45#define PTM_MSG_LEN 4
0a56c844 46#define PTM_HEADER_LEN 37
c43ed2e4
DS
47
48const char ZEBRA_PTM_GET_STATUS_CMD[] = "get-status";
49const char ZEBRA_PTM_BFD_START_CMD[] = "start-bfd-sess";
50const char ZEBRA_PTM_BFD_STOP_CMD[] = "stop-bfd-sess";
055c4dfc 51const char ZEBRA_PTM_BFD_CLIENT_REG_CMD[] = "reg-bfd-client";
567b877d 52const char ZEBRA_PTM_BFD_CLIENT_DEREG_CMD[] = "dereg-bfd-client";
c43ed2e4 53
c8ed14dd 54const char ZEBRA_PTM_CMD_STR[] = "cmd";
b255a4b1 55const char ZEBRA_PTM_CMD_STATUS_STR[] = "cmd_status";
c43ed2e4
DS
56const char ZEBRA_PTM_PORT_STR[] = "port";
57const char ZEBRA_PTM_CBL_STR[] = "cbl status";
58const char ZEBRA_PTM_PASS_STR[] = "pass";
59const char ZEBRA_PTM_FAIL_STR[] = "fail";
60const char ZEBRA_PTM_BFDSTATUS_STR[] = "state";
61const char ZEBRA_PTM_BFDSTATUS_UP_STR[] = "Up";
62const char ZEBRA_PTM_BFDSTATUS_DOWN_STR[] = "Down";
63const char ZEBRA_PTM_BFDDEST_STR[] = "peer";
64const char ZEBRA_PTM_BFDSRC_STR[] = "local";
d553294e 65const char ZEBRA_PTM_BFDVRF_STR[] = "vrf";
c43ed2e4
DS
66const char ZEBRA_PTM_INVALID_PORT_NAME[] = "N/A";
67const char ZEBRA_PTM_INVALID_SRC_IP[] = "N/A";
d553294e 68const char ZEBRA_PTM_INVALID_VRF[] = "N/A";
c43ed2e4
DS
69
70const char ZEBRA_PTM_BFD_DST_IP_FIELD[] = "dstIPaddr";
71const char ZEBRA_PTM_BFD_SRC_IP_FIELD[] = "srcIPaddr";
72const char ZEBRA_PTM_BFD_MIN_RX_FIELD[] = "requiredMinRx";
73const char ZEBRA_PTM_BFD_MIN_TX_FIELD[] = "upMinTx";
74const char ZEBRA_PTM_BFD_DETECT_MULT_FIELD[] = "detectMult";
75const char ZEBRA_PTM_BFD_MULTI_HOP_FIELD[] = "multiHop";
76const char ZEBRA_PTM_BFD_CLIENT_FIELD[] = "client";
77const char ZEBRA_PTM_BFD_SEQID_FIELD[] = "seqid";
78const char ZEBRA_PTM_BFD_IFNAME_FIELD[] = "ifName";
79const char ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD[] = "maxHopCnt";
68fe91d6 80const char ZEBRA_PTM_BFD_SEND_EVENT[] = "sendEvent";
d553294e 81const char ZEBRA_PTM_BFD_VRF_NAME_FIELD[] = "vrfName";
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);
244c1cdc 88int zebra_ptm_sock_read(struct 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
d62a17ae 102 memset(&ptm_cb, 0, sizeof(struct zebra_ptm_cb));
c8ed14dd 103
d62a17ae 104 ptm_cb.out_data = calloc(1, ZEBRA_PTM_SEND_MAX_SOCKBUF);
105 if (!ptm_cb.out_data) {
106 zlog_warn("%s: Allocation of send data failed", __func__);
107 return;
108 }
c8ed14dd 109
d62a17ae 110 ptm_cb.in_data = calloc(1, ZEBRA_PTM_MAX_SOCKBUF);
111 if (!ptm_cb.in_data) {
112 zlog_warn("%s: Allocation of recv data failed", __func__);
113 free(ptm_cb.out_data);
114 return;
115 }
c8ed14dd 116
d62a17ae 117 ptm_cb.pid = getpid();
118 zebra_ptm_install_commands();
c43ed2e4 119
d62a17ae 120 sprintf(buf, "%s", FRR_PTM_NAME);
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
d62a17ae 144 /* Release threads. */
145 if (ptm_cb.t_read)
146 thread_cancel(ptm_cb.t_read);
147 if (ptm_cb.t_write)
148 thread_cancel(ptm_cb.t_write);
149 if (ptm_cb.t_timer)
150 thread_cancel(ptm_cb.t_timer);
2376c3f2 151
d62a17ae 152 if (ptm_cb.wb)
153 buffer_free(ptm_cb.wb);
2376c3f2 154
1e9f448f 155 if (ptm_cb.ptm_sock >= 0)
d62a17ae 156 close(ptm_cb.ptm_sock);
c8ed14dd
DS
157}
158
d62a17ae 159static int zebra_ptm_flush_messages(struct thread *thread)
c8ed14dd 160{
d62a17ae 161 ptm_cb.t_write = NULL;
162
163 if (ptm_cb.ptm_sock == -1)
164 return -1;
165
166 errno = 0;
167
168 switch (buffer_flush_available(ptm_cb.wb, ptm_cb.ptm_sock)) {
169 case BUFFER_ERROR:
170 zlog_warn("%s ptm socket error: %s", __func__,
171 safe_strerror(errno));
172 close(ptm_cb.ptm_sock);
173 ptm_cb.ptm_sock = -1;
174 zebra_ptm_reset_status(0);
175 ptm_cb.t_timer = NULL;
176 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
177 ptm_cb.reconnect_time, &ptm_cb.t_timer);
178 return (-1);
179 case BUFFER_PENDING:
180 ptm_cb.t_write = NULL;
181 thread_add_write(zebrad.master, zebra_ptm_flush_messages, NULL,
182 ptm_cb.ptm_sock, &ptm_cb.t_write);
183 break;
184 case BUFFER_EMPTY:
185 break;
186 }
187
188 return (0);
c8ed14dd
DS
189}
190
d62a17ae 191static int zebra_ptm_send_message(char *data, int size)
c8ed14dd 192{
d62a17ae 193 errno = 0;
194 switch (buffer_write(ptm_cb.wb, ptm_cb.ptm_sock, data, size)) {
195 case BUFFER_ERROR:
196 zlog_warn("%s ptm socket error: %s", __func__,
197 safe_strerror(errno));
198 close(ptm_cb.ptm_sock);
199 ptm_cb.ptm_sock = -1;
200 zebra_ptm_reset_status(0);
201 ptm_cb.t_timer = NULL;
202 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
203 ptm_cb.reconnect_time, &ptm_cb.t_timer);
204 return -1;
205 case BUFFER_EMPTY:
206 THREAD_OFF(ptm_cb.t_write);
207 break;
208 case BUFFER_PENDING:
209 thread_add_write(zebrad.master, zebra_ptm_flush_messages, NULL,
210 ptm_cb.ptm_sock, &ptm_cb.t_write);
211 break;
212 }
213
214 return 0;
244c1cdc
DS
215}
216
d62a17ae 217int zebra_ptm_connect(struct thread *t)
244c1cdc 218{
d62a17ae 219 int init = 0;
220
221 if (ptm_cb.ptm_sock == -1) {
222 zebra_ptm_socket_init();
223 init = 1;
224 }
225
226 if (ptm_cb.ptm_sock != -1) {
227 if (init) {
228 ptm_cb.t_read = NULL;
229 thread_add_read(zebrad.master, zebra_ptm_sock_read,
230 NULL, ptm_cb.ptm_sock, &ptm_cb.t_read);
231 zebra_bfd_peer_replay_req();
232 }
233 zebra_ptm_send_status_req();
234 ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_INITIAL;
235 } else if (ptm_cb.reconnect_time < ZEBRA_PTM_RECONNECT_TIME_MAX) {
236 ptm_cb.reconnect_time *= 2;
237 if (ptm_cb.reconnect_time > ZEBRA_PTM_RECONNECT_TIME_MAX)
238 ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_MAX;
239
240 ptm_cb.t_timer = NULL;
241 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
242 ptm_cb.reconnect_time, &ptm_cb.t_timer);
243 } else if (ptm_cb.reconnect_time >= ZEBRA_PTM_RECONNECT_TIME_MAX) {
244 ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_INITIAL;
245 }
246
247 return (errno);
244c1cdc
DS
248}
249
244c1cdc
DS
250DEFUN (zebra_ptm_enable,
251 zebra_ptm_enable_cmd,
252 "ptm-enable",
253 "Enable neighbor check with specified topology\n")
254{
d62a17ae 255 struct vrf *vrf;
d62a17ae 256 struct interface *ifp;
257 struct zebra_if *if_data;
258
259 ptm_cb.ptm_enable = ZEBRA_IF_PTM_ENABLE_ON;
260
a2addae8 261 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
451fda4f 262 FOR_ALL_INTERFACES (vrf, ifp)
a2addae8
RW
263 if (!ifp->ptm_enable) {
264 if_data = (struct zebra_if *)ifp->info;
265 if (if_data
266 && (if_data->ptm_enable
9d303b37 267 == ZEBRA_IF_PTM_ENABLE_UNSPEC)) {
a2addae8
RW
268 ifp->ptm_enable =
269 ZEBRA_IF_PTM_ENABLE_ON;
270 }
271 /* Assign a default unknown status */
272 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
d62a17ae 273 }
d62a17ae 274
275 zebra_ptm_connect(NULL);
276
277 return CMD_SUCCESS;
244c1cdc
DS
278}
279
280DEFUN (no_zebra_ptm_enable,
281 no_zebra_ptm_enable_cmd,
282 "no ptm-enable",
283 NO_STR
284 "Enable neighbor check with specified topology\n")
285{
d62a17ae 286 ptm_cb.ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF;
287 zebra_ptm_reset_status(1);
288 return CMD_SUCCESS;
244c1cdc
DS
289}
290
986aa00f 291DEFUN (zebra_ptm_enable_if,
292 zebra_ptm_enable_if_cmd,
293 "ptm-enable",
294 "Enable neighbor check with specified topology\n")
295{
d62a17ae 296 VTY_DECLVAR_CONTEXT(interface, ifp);
297 struct zebra_if *if_data;
298 int old_ptm_enable;
299 int send_linkdown = 0;
300
a2023fab 301 if_data = ifp->info;
302 if_data->ptm_enable = ZEBRA_IF_PTM_ENABLE_UNSPEC;
303
d62a17ae 304 if (ifp->ifindex == IFINDEX_INTERNAL) {
305 return CMD_SUCCESS;
306 }
307
308 old_ptm_enable = ifp->ptm_enable;
309 ifp->ptm_enable = ptm_cb.ptm_enable;
310
311 if (if_is_no_ptm_operative(ifp))
312 send_linkdown = 1;
313
314 if (!old_ptm_enable && ptm_cb.ptm_enable) {
315 if (!if_is_operative(ifp) && send_linkdown) {
316 if (IS_ZEBRA_DEBUG_EVENT)
317 zlog_debug("%s: Bringing down interface %s\n",
318 __func__, ifp->name);
319 if_down(ifp);
320 }
321 }
322
d62a17ae 323 return CMD_SUCCESS;
986aa00f 324}
325
326DEFUN (no_zebra_ptm_enable_if,
327 no_zebra_ptm_enable_if_cmd,
328 "no ptm-enable",
329 NO_STR
330 "Enable neighbor check with specified topology\n")
331{
d62a17ae 332 VTY_DECLVAR_CONTEXT(interface, ifp);
333 int send_linkup = 0;
334 struct zebra_if *if_data;
335
336 if ((ifp->ifindex != IFINDEX_INTERNAL) && (ifp->ptm_enable)) {
337 if (!if_is_operative(ifp))
338 send_linkup = 1;
339
340 ifp->ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF;
341 if (if_is_no_ptm_operative(ifp) && send_linkup) {
342 if (IS_ZEBRA_DEBUG_EVENT)
343 zlog_debug("%s: Bringing up interface %s\n",
344 __func__, ifp->name);
345 if_up(ifp);
346 }
347 }
348
349 if_data = ifp->info;
350 if_data->ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF;
351
352 return CMD_SUCCESS;
986aa00f 353}
354
355
d62a17ae 356void zebra_ptm_write(struct vty *vty)
244c1cdc 357{
d62a17ae 358 if (ptm_cb.ptm_enable)
359 vty_out(vty, "ptm-enable\n");
244c1cdc 360
d62a17ae 361 return;
244c1cdc
DS
362}
363
d62a17ae 364static int zebra_ptm_socket_init(void)
244c1cdc 365{
d62a17ae 366 int ret;
367 int sock;
368 struct sockaddr_un addr;
369
370 ptm_cb.ptm_sock = -1;
371
372 sock = socket(PF_UNIX, SOCK_STREAM, 0);
373 if (sock < 0)
374 return -1;
375 if (set_nonblocking(sock) < 0) {
376 if (IS_ZEBRA_DEBUG_EVENT)
377 zlog_debug("%s: Unable to set socket non blocking[%s]",
378 __PRETTY_FUNCTION__, safe_strerror(errno));
379 close(sock);
380 return -1;
381 }
382
383 /* Make server socket. */
384 memset(&addr, 0, sizeof(struct sockaddr_un));
385 addr.sun_family = AF_UNIX;
386 memcpy(&addr.sun_path, ZEBRA_PTM_SOCK_NAME,
387 sizeof(ZEBRA_PTM_SOCK_NAME));
388
389 ret = connect(sock, (struct sockaddr *)&addr,
390 sizeof(addr.sun_family) + sizeof(ZEBRA_PTM_SOCK_NAME)
391 - 1);
392 if (ret < 0) {
393 if (IS_ZEBRA_DEBUG_EVENT)
394 zlog_debug("%s: Unable to connect to socket %s [%s]",
395 __func__, ZEBRA_PTM_SOCK_NAME,
396 safe_strerror(errno));
397 close(sock);
398 return -1;
399 }
400 ptm_cb.ptm_sock = sock;
401 return sock;
244c1cdc
DS
402}
403
d62a17ae 404static void zebra_ptm_install_commands(void)
244c1cdc 405{
d62a17ae 406 install_element(CONFIG_NODE, &zebra_ptm_enable_cmd);
407 install_element(CONFIG_NODE, &no_zebra_ptm_enable_cmd);
408 install_element(INTERFACE_NODE, &zebra_ptm_enable_if_cmd);
409 install_element(INTERFACE_NODE, &no_zebra_ptm_enable_if_cmd);
244c1cdc
DS
410}
411
c43ed2e4 412/* BFD session goes down, send message to the protocols. */
d62a17ae 413static void if_bfd_session_update(struct interface *ifp, struct prefix *dp,
414 struct prefix *sp, int status,
415 vrf_id_t vrf_id)
c01acf98 416{
d62a17ae 417 if (IS_ZEBRA_DEBUG_EVENT) {
418 char buf[2][INET6_ADDRSTRLEN];
419
420 if (ifp) {
421 zlog_debug(
422 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d on %s"
423 " %s event",
424 inet_ntop(dp->family, &dp->u.prefix, buf[0],
425 INET6_ADDRSTRLEN),
426 dp->prefixlen, ifp->name,
427 bfd_get_status_str(status));
428 } else {
429 zlog_debug(
430 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d "
a9ff90c4 431 "with src %s/%d and vrf %u %s event",
d62a17ae 432 inet_ntop(dp->family, &dp->u.prefix, buf[0],
433 INET6_ADDRSTRLEN),
434 dp->prefixlen,
435 inet_ntop(sp->family, &sp->u.prefix, buf[1],
436 INET6_ADDRSTRLEN),
437 sp->prefixlen, vrf_id,
438 bfd_get_status_str(status));
439 }
440 }
441
442 zebra_interface_bfd_update(ifp, dp, sp, status, vrf_id);
c01acf98
DS
443}
444
d62a17ae 445static int zebra_ptm_handle_bfd_msg(void *arg, void *in_ctxt,
446 struct interface *ifp)
244c1cdc 447{
d62a17ae 448 char bfdst_str[32];
449 char dest_str[64];
450 char src_str[64];
451 char vrf_str[64];
452 struct prefix dest_prefix;
453 struct prefix src_prefix;
454 vrf_id_t vrf_id;
455
456 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDSTATUS_STR, bfdst_str);
457
458 if (bfdst_str[0] == '\0') {
459 return -1;
460 }
461
462 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDDEST_STR, dest_str);
463
464 if (dest_str[0] == '\0') {
465 zlog_debug("%s: Key %s not found in PTM msg", __func__,
466 ZEBRA_PTM_BFDDEST_STR);
467 return -1;
468 }
469
470 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDSRC_STR, src_str);
471
472 if (src_str[0] == '\0') {
473 zlog_debug("%s: Key %s not found in PTM msg", __func__,
474 ZEBRA_PTM_BFDSRC_STR);
475 return -1;
476 }
477
478 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDVRF_STR, vrf_str);
479
480 if (vrf_str[0] == '\0') {
481 zlog_debug("%s: Key %s not found in PTM msg", __func__,
482 ZEBRA_PTM_BFDVRF_STR);
483 return -1;
484 }
485
486 if (IS_ZEBRA_DEBUG_EVENT)
487 zlog_debug(
488 "%s: Recv Port [%s] bfd status [%s] vrf [%s]"
489 " peer [%s] local [%s]",
490 __func__, ifp ? ifp->name : "N/A", bfdst_str, vrf_str,
491 dest_str, src_str);
492
493 if (str2prefix(dest_str, &dest_prefix) == 0) {
494 zlog_err("%s: Peer addr %s not found", __func__, dest_str);
495 return -1;
496 }
497
498 memset(&src_prefix, 0, sizeof(struct prefix));
499 if (strcmp(ZEBRA_PTM_INVALID_SRC_IP, src_str)) {
500 if (str2prefix(src_str, &src_prefix) == 0) {
501 zlog_err("%s: Local addr %s not found", __func__,
502 src_str);
503 return -1;
504 }
505 }
506
507 if (!strcmp(ZEBRA_PTM_INVALID_VRF, vrf_str) && ifp) {
508 vrf_id = ifp->vrf_id;
509 } else {
510 vrf_id = vrf_name_to_id(vrf_str);
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)
541 if_up(ifp);
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)) {
593 ifp = if_lookup_by_name_all_vrf(port_str);
594
595 if (!ifp) {
596 zlog_err("%s: %s not found in interface list", __func__,
597 port_str);
598 return -1;
599 }
600 }
601
602 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_CBL_STR, cbl_str);
603
604 if (cbl_str[0] == '\0') {
605 return zebra_ptm_handle_bfd_msg(arg, in_ctxt, ifp);
606 } else {
607 if (ifp) {
608 return zebra_ptm_handle_cbl_msg(arg, in_ctxt, ifp,
609 cbl_str);
610 } else {
611 return -1;
612 }
613 }
c01acf98
DS
614}
615
d62a17ae 616int zebra_ptm_sock_read(struct thread *thread)
c01acf98 617{
2e1cc436 618 int sock;
d62a17ae 619 int rc;
620
621 errno = 0;
622 sock = THREAD_FD(thread);
623
624 if (sock == -1)
625 return -1;
626
627 /* PTM communicates in CSV format */
2e1cc436 628 do {
d62a17ae 629 rc = ptm_lib_process_msg(ptm_hdl, sock, ptm_cb.in_data,
630 ZEBRA_PTM_MAX_SOCKBUF, NULL);
2e1cc436 631 } while (rc > 0);
d62a17ae 632
2e1cc436
A
633 if (((rc == 0) && !errno)
634 || (errno && (errno != EWOULDBLOCK) && (errno != EAGAIN))) {
635 zlog_warn("%s routing socket error: %s(%d) bytes %d",
636 __func__, safe_strerror(errno), errno, rc);
637
638 close(ptm_cb.ptm_sock);
639 ptm_cb.ptm_sock = -1;
640 zebra_ptm_reset_status(0);
641 ptm_cb.t_timer = NULL;
642 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
643 ptm_cb.reconnect_time,
644 &ptm_cb.t_timer);
645 return (-1);
d62a17ae 646 }
647
648 ptm_cb.t_read = NULL;
649 thread_add_read(zebrad.master, zebra_ptm_sock_read, NULL,
650 ptm_cb.ptm_sock, &ptm_cb.t_read);
651
652 return 0;
c43ed2e4
DS
653}
654
655/* BFD peer/dst register/update */
89f4e507 656void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS)
c43ed2e4 657{
d62a17ae 658 struct stream *s;
659 struct prefix src_p;
660 struct prefix dst_p;
d7c0a89a
QY
661 uint8_t multi_hop;
662 uint8_t multi_hop_cnt;
663 uint8_t detect_mul;
d62a17ae 664 unsigned int min_rx_timer;
665 unsigned int min_tx_timer;
666 char if_name[INTERFACE_NAMSIZ];
d7c0a89a 667 uint8_t len;
d62a17ae 668 void *out_ctxt;
669 char buf[INET6_ADDRSTRLEN];
670 char tmp_buf[64];
671 int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
672 unsigned int pid;
673
89f4e507 674 if (hdr->command == ZEBRA_BFD_DEST_UPDATE)
d62a17ae 675 client->bfd_peer_upd8_cnt++;
676 else
677 client->bfd_peer_add_cnt++;
678
679 if (IS_ZEBRA_DEBUG_EVENT)
680 zlog_debug("bfd_dst_register msg from client %s: length=%d",
89f4e507 681 zebra_route_string(client->proto), hdr->length);
d62a17ae 682
683 if (ptm_cb.ptm_sock == -1) {
684 ptm_cb.t_timer = NULL;
685 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
686 ptm_cb.reconnect_time, &ptm_cb.t_timer);
8068a649 687 return;
d62a17ae 688 }
689
690 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
691 sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_START_CMD);
692 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
693 sprintf(tmp_buf, "%s", zebra_route_string(client->proto));
694 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
695 tmp_buf);
696
1002497a 697 s = msg;
d62a17ae 698
ec93aa12 699 STREAM_GETL(s, pid);
d62a17ae 700 sprintf(tmp_buf, "%d", pid);
701 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD,
702 tmp_buf);
703
ec93aa12 704 STREAM_GETW(s, dst_p.family);
d62a17ae 705
706 if (dst_p.family == AF_INET)
707 dst_p.prefixlen = IPV4_MAX_BYTELEN;
708 else
709 dst_p.prefixlen = IPV6_MAX_BYTELEN;
710
ec93aa12 711 STREAM_GET(&dst_p.u.prefix, s, dst_p.prefixlen);
d62a17ae 712 if (dst_p.family == AF_INET) {
713 inet_ntop(AF_INET, &dst_p.u.prefix4, buf, sizeof(buf));
714 ptm_lib_append_msg(ptm_hdl, out_ctxt,
715 ZEBRA_PTM_BFD_DST_IP_FIELD, buf);
716 } else {
717 inet_ntop(AF_INET6, &dst_p.u.prefix6, buf, sizeof(buf));
718 ptm_lib_append_msg(ptm_hdl, out_ctxt,
719 ZEBRA_PTM_BFD_DST_IP_FIELD, buf);
720 }
721
ec93aa12 722 STREAM_GETL(s, min_rx_timer);
d62a17ae 723 sprintf(tmp_buf, "%d", min_rx_timer);
724 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_MIN_RX_FIELD,
725 tmp_buf);
ec93aa12 726 STREAM_GETL(s, min_tx_timer);
d62a17ae 727 sprintf(tmp_buf, "%d", min_tx_timer);
728 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_MIN_TX_FIELD,
729 tmp_buf);
ec93aa12 730 STREAM_GETC(s, detect_mul);
d62a17ae 731 sprintf(tmp_buf, "%d", detect_mul);
732 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_DETECT_MULT_FIELD,
733 tmp_buf);
734
ec93aa12 735 STREAM_GETC(s, multi_hop);
d62a17ae 736 if (multi_hop) {
737 sprintf(tmp_buf, "%d", 1);
738 ptm_lib_append_msg(ptm_hdl, out_ctxt,
739 ZEBRA_PTM_BFD_MULTI_HOP_FIELD, tmp_buf);
ec93aa12 740 STREAM_GETW(s, src_p.family);
d62a17ae 741
742 if (src_p.family == AF_INET)
743 src_p.prefixlen = IPV4_MAX_BYTELEN;
744 else
745 src_p.prefixlen = IPV6_MAX_BYTELEN;
746
ec93aa12 747 STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen);
d62a17ae 748 if (src_p.family == AF_INET) {
749 inet_ntop(AF_INET, &src_p.u.prefix4, buf, sizeof(buf));
750 ptm_lib_append_msg(ptm_hdl, out_ctxt,
751 ZEBRA_PTM_BFD_SRC_IP_FIELD, buf);
752 } else {
753 inet_ntop(AF_INET6, &src_p.u.prefix6, buf, sizeof(buf));
754 ptm_lib_append_msg(ptm_hdl, out_ctxt,
755 ZEBRA_PTM_BFD_SRC_IP_FIELD, buf);
756 }
757
ec93aa12 758 STREAM_GETC(s, multi_hop_cnt);
d62a17ae 759 sprintf(tmp_buf, "%d", multi_hop_cnt);
760 ptm_lib_append_msg(ptm_hdl, out_ctxt,
761 ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD, tmp_buf);
762
763 if (zvrf_id(zvrf) != VRF_DEFAULT)
764 ptm_lib_append_msg(ptm_hdl, out_ctxt,
765 ZEBRA_PTM_BFD_VRF_NAME_FIELD,
766 zvrf_name(zvrf));
767 } else {
768 if (dst_p.family == AF_INET6) {
ec93aa12 769 STREAM_GETW(s, src_p.family);
d62a17ae 770
771 if (src_p.family == AF_INET)
772 src_p.prefixlen = IPV4_MAX_BYTELEN;
773 else
774 src_p.prefixlen = IPV6_MAX_BYTELEN;
775
ec93aa12 776 STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen);
d62a17ae 777 if (src_p.family == AF_INET) {
778 inet_ntop(AF_INET, &src_p.u.prefix4, buf,
779 sizeof(buf));
780 ptm_lib_append_msg(ptm_hdl, out_ctxt,
781 ZEBRA_PTM_BFD_SRC_IP_FIELD,
782 buf);
783 } else {
784 inet_ntop(AF_INET6, &src_p.u.prefix6, buf,
785 sizeof(buf));
786 ptm_lib_append_msg(ptm_hdl, out_ctxt,
787 ZEBRA_PTM_BFD_SRC_IP_FIELD,
788 buf);
789 }
790 }
ec93aa12
DS
791 STREAM_GETC(s, len);
792 STREAM_GET(if_name, s, len);
d62a17ae 793 if_name[len] = '\0';
794
795 ptm_lib_append_msg(ptm_hdl, out_ctxt,
796 ZEBRA_PTM_BFD_IFNAME_FIELD, if_name);
797 }
798
799 sprintf(tmp_buf, "%d", 1);
800 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEND_EVENT,
801 tmp_buf);
802
803 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
804
805 if (IS_ZEBRA_DEBUG_SEND)
806 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
807 ptm_cb.out_data);
808 zebra_ptm_send_message(ptm_cb.out_data, data_len);
ec93aa12 809
8068a649 810 return;
9cc46248 811
ec93aa12 812stream_failure:
a928d464 813 ptm_lib_cleanup_msg(ptm_hdl, out_ctxt);
c43ed2e4
DS
814}
815
816/* BFD peer/dst deregister */
89f4e507 817void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS)
c43ed2e4 818{
d62a17ae 819 struct stream *s;
820 struct prefix src_p;
821 struct prefix dst_p;
d7c0a89a 822 uint8_t multi_hop;
d62a17ae 823 char if_name[INTERFACE_NAMSIZ];
d7c0a89a 824 uint8_t len;
d62a17ae 825 char buf[INET6_ADDRSTRLEN];
826 char tmp_buf[64];
827 int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
828 void *out_ctxt;
829 unsigned int pid;
830
831 client->bfd_peer_del_cnt++;
832
833 if (IS_ZEBRA_DEBUG_EVENT)
834 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
89f4e507 835 zebra_route_string(client->proto), hdr->length);
d62a17ae 836
837 if (ptm_cb.ptm_sock == -1) {
838 ptm_cb.t_timer = NULL;
839 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
840 ptm_cb.reconnect_time, &ptm_cb.t_timer);
8068a649 841 return;
d62a17ae 842 }
843
844 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
845
846 sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_STOP_CMD);
847 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
848
849 sprintf(tmp_buf, "%s", zebra_route_string(client->proto));
850 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
851 tmp_buf);
852
1002497a 853 s = msg;
d62a17ae 854
ec93aa12 855 STREAM_GETL(s, pid);
d62a17ae 856 sprintf(tmp_buf, "%d", pid);
857 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD,
858 tmp_buf);
859
ec93aa12 860 STREAM_GETW(s, dst_p.family);
d62a17ae 861
862 if (dst_p.family == AF_INET)
863 dst_p.prefixlen = IPV4_MAX_BYTELEN;
864 else
865 dst_p.prefixlen = IPV6_MAX_BYTELEN;
866
ec93aa12 867 STREAM_GET(&dst_p.u.prefix, s, dst_p.prefixlen);
d62a17ae 868 if (dst_p.family == AF_INET)
869 inet_ntop(AF_INET, &dst_p.u.prefix4, buf, sizeof(buf));
870 else
871 inet_ntop(AF_INET6, &dst_p.u.prefix6, buf, sizeof(buf));
872 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_DST_IP_FIELD, buf);
873
874
ec93aa12 875 STREAM_GETC(s, multi_hop);
d62a17ae 876 if (multi_hop) {
877 sprintf(tmp_buf, "%d", 1);
878 ptm_lib_append_msg(ptm_hdl, out_ctxt,
879 ZEBRA_PTM_BFD_MULTI_HOP_FIELD, tmp_buf);
880
ec93aa12 881 STREAM_GETW(s, src_p.family);
d62a17ae 882
883 if (src_p.family == AF_INET)
884 src_p.prefixlen = IPV4_MAX_BYTELEN;
885 else
886 src_p.prefixlen = IPV6_MAX_BYTELEN;
887
ec93aa12 888 STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen);
d62a17ae 889 if (src_p.family == AF_INET)
890 inet_ntop(AF_INET, &src_p.u.prefix4, buf, sizeof(buf));
891 else
892 inet_ntop(AF_INET6, &src_p.u.prefix6, buf, sizeof(buf));
893 ptm_lib_append_msg(ptm_hdl, out_ctxt,
894 ZEBRA_PTM_BFD_SRC_IP_FIELD, buf);
895
896 if (zvrf_id(zvrf) != VRF_DEFAULT)
897 ptm_lib_append_msg(ptm_hdl, out_ctxt,
898 ZEBRA_PTM_BFD_VRF_NAME_FIELD,
899 zvrf_name(zvrf));
900 } else {
901 if (dst_p.family == AF_INET6) {
ec93aa12 902 STREAM_GETW(s, src_p.family);
d62a17ae 903
904 if (src_p.family == AF_INET)
905 src_p.prefixlen = IPV4_MAX_BYTELEN;
906 else
907 src_p.prefixlen = IPV6_MAX_BYTELEN;
908
ec93aa12 909 STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen);
d62a17ae 910 if (src_p.family == AF_INET) {
911 inet_ntop(AF_INET, &src_p.u.prefix4, buf,
912 sizeof(buf));
913 ptm_lib_append_msg(ptm_hdl, out_ctxt,
914 ZEBRA_PTM_BFD_SRC_IP_FIELD,
915 buf);
916 } else {
917 inet_ntop(AF_INET6, &src_p.u.prefix6, buf,
918 sizeof(buf));
919 ptm_lib_append_msg(ptm_hdl, out_ctxt,
920 ZEBRA_PTM_BFD_SRC_IP_FIELD,
921 buf);
922 }
923 }
924
ec93aa12
DS
925 STREAM_GETC(s, len);
926 STREAM_GET(if_name, s, len);
d62a17ae 927 if_name[len] = '\0';
928
929 ptm_lib_append_msg(ptm_hdl, out_ctxt,
930 ZEBRA_PTM_BFD_IFNAME_FIELD, if_name);
931 }
932
933 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
934 if (IS_ZEBRA_DEBUG_SEND)
935 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
936 ptm_cb.out_data);
937
938 zebra_ptm_send_message(ptm_cb.out_data, data_len);
ec93aa12 939
8068a649 940 return;
9cc46248 941
ec93aa12 942stream_failure:
a928d464 943 ptm_lib_cleanup_msg(ptm_hdl, out_ctxt);
c01acf98 944}
c8ed14dd 945
055c4dfc 946/* BFD client register */
89f4e507 947void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS)
055c4dfc 948{
d62a17ae 949 struct stream *s;
950 unsigned int pid;
9cc46248 951 void *out_ctxt = NULL;
d62a17ae 952 char tmp_buf[64];
953 int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
055c4dfc 954
d62a17ae 955 client->bfd_client_reg_cnt++;
055c4dfc 956
d62a17ae 957 if (IS_ZEBRA_DEBUG_EVENT)
958 zlog_debug("bfd_client_register msg from client %s: length=%d",
89f4e507 959 zebra_route_string(client->proto), hdr->length);
055c4dfc 960
1002497a 961 s = msg;
ec93aa12 962 STREAM_GETL(s, pid);
bf6e101c 963
d62a17ae 964 if (ptm_cb.ptm_sock == -1) {
965 ptm_cb.t_timer = NULL;
966 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
967 ptm_cb.reconnect_time, &ptm_cb.t_timer);
8068a649 968 return;
d62a17ae 969 }
055c4dfc 970
d62a17ae 971 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
055c4dfc 972
d62a17ae 973 sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_CLIENT_REG_CMD);
974 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
055c4dfc 975
d62a17ae 976 sprintf(tmp_buf, "%s", zebra_route_string(client->proto));
977 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
978 tmp_buf);
055c4dfc 979
d62a17ae 980 sprintf(tmp_buf, "%d", pid);
981 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD,
982 tmp_buf);
055c4dfc 983
d62a17ae 984 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
055c4dfc 985
d62a17ae 986 if (IS_ZEBRA_DEBUG_SEND)
987 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
988 ptm_cb.out_data);
989 zebra_ptm_send_message(ptm_cb.out_data, data_len);
2376c3f2 990
d62a17ae 991 SET_FLAG(ptm_cb.client_flags[client->proto],
992 ZEBRA_PTM_BFD_CLIENT_FLAG_REG);
9cc46248 993
8068a649 994 return;
9cc46248 995
ec93aa12 996stream_failure:
6447dbb3
DS
997 /*
998 * IF we ever add more STREAM_GETXXX functions after the out_ctxt
999 * is allocated then we need to add this code back in
1000 *
1001 * if (out_ctxt)
1002 * ptm_lib_cleanup_msg(ptm_hdl, out_ctxt);
1003 */
8068a649 1004 return;
055c4dfc 1005}
1006
567b877d 1007/* BFD client deregister */
453844ab 1008int zebra_ptm_bfd_client_deregister(struct zserv *client)
567b877d 1009{
453844ab 1010 uint8_t proto = client->proto;
d62a17ae 1011 void *out_ctxt;
1012 char tmp_buf[64];
1013 int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
567b877d 1014
d62a17ae 1015 if (proto != ZEBRA_ROUTE_OSPF && proto != ZEBRA_ROUTE_BGP
1016 && proto != ZEBRA_ROUTE_OSPF6 && proto != ZEBRA_ROUTE_PIM)
453844ab 1017 return 0;
567b877d 1018
d62a17ae 1019 if (IS_ZEBRA_DEBUG_EVENT)
1020 zlog_err("bfd_client_deregister msg for client %s",
1021 zebra_route_string(proto));
567b877d 1022
d62a17ae 1023 if (ptm_cb.ptm_sock == -1) {
1024 ptm_cb.t_timer = NULL;
1025 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
1026 ptm_cb.reconnect_time, &ptm_cb.t_timer);
453844ab 1027 return 0;
d62a17ae 1028 }
567b877d 1029
d62a17ae 1030 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
567b877d 1031
d62a17ae 1032 sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_CLIENT_DEREG_CMD);
1033 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
567b877d 1034
d62a17ae 1035 sprintf(tmp_buf, "%s", zebra_route_string(proto));
1036 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
1037 tmp_buf);
567b877d 1038
d62a17ae 1039 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
567b877d 1040
d62a17ae 1041 if (IS_ZEBRA_DEBUG_SEND)
1042 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
1043 ptm_cb.out_data);
2376c3f2 1044
d62a17ae 1045 zebra_ptm_send_message(ptm_cb.out_data, data_len);
1046 UNSET_FLAG(ptm_cb.client_flags[proto], ZEBRA_PTM_BFD_CLIENT_FLAG_REG);
453844ab
QY
1047
1048 return 0;
567b877d 1049}
1050
d62a17ae 1051int zebra_ptm_get_enable_state(void)
c8ed14dd 1052{
d62a17ae 1053 return ptm_cb.ptm_enable;
c8ed14dd 1054}
950bd436 1055
1056/*
1057 * zebra_ptm_get_status_str - Convert status to a display string.
1058 */
d62a17ae 1059static const char *zebra_ptm_get_status_str(int status)
950bd436 1060{
d62a17ae 1061 switch (status) {
1062 case ZEBRA_PTM_STATUS_DOWN:
1063 return "fail";
1064 case ZEBRA_PTM_STATUS_UP:
1065 return "pass";
1066 case ZEBRA_PTM_STATUS_UNKNOWN:
1067 default:
1068 return "n/a";
1069 }
950bd436 1070}
1071
d62a17ae 1072void zebra_ptm_show_status(struct vty *vty, struct interface *ifp)
950bd436 1073{
d62a17ae 1074 vty_out(vty, " PTM status: ");
1075 if (ifp->ptm_enable) {
1076 vty_out(vty, "%s\n", zebra_ptm_get_status_str(ifp->ptm_status));
1077 } else {
1078 vty_out(vty, "disabled\n");
1079 }
950bd436 1080}
1081
d62a17ae 1082void zebra_ptm_send_status_req(void)
950bd436 1083{
d62a17ae 1084 void *out_ctxt;
1085 int len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
1086
1087 if (ptm_cb.ptm_enable) {
1088 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL,
1089 &out_ctxt);
1090 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR,
1091 ZEBRA_PTM_GET_STATUS_CMD);
1092 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &len);
1093
1094 zebra_ptm_send_message(ptm_cb.out_data, len);
1095 }
950bd436 1096}
1097
d62a17ae 1098void zebra_ptm_reset_status(int ptm_disable)
950bd436 1099{
d62a17ae 1100 struct vrf *vrf;
d62a17ae 1101 struct interface *ifp;
1102 int send_linkup;
1103
a2addae8 1104 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
451fda4f 1105 FOR_ALL_INTERFACES (vrf, ifp) {
a2addae8
RW
1106 send_linkup = 0;
1107 if (ifp->ptm_enable) {
1108 if (!if_is_operative(ifp))
1109 send_linkup = 1;
1110
1111 if (ptm_disable)
1112 ifp->ptm_enable =
1113 ZEBRA_IF_PTM_ENABLE_OFF;
1114 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
1115
1116 if (if_is_operative(ifp) && send_linkup) {
1117 if (IS_ZEBRA_DEBUG_EVENT)
1118 zlog_debug(
1119 "%s: Bringing up interface %s",
1120 __func__, ifp->name);
1121 if_up(ifp);
1122 }
d62a17ae 1123 }
1124 }
950bd436 1125}
986aa00f 1126
d62a17ae 1127void zebra_ptm_if_init(struct zebra_if *zebra_ifp)
986aa00f 1128{
d62a17ae 1129 zebra_ifp->ptm_enable = ZEBRA_IF_PTM_ENABLE_UNSPEC;
986aa00f 1130}
1131
d62a17ae 1132void zebra_ptm_if_set_ptm_state(struct interface *ifp,
1133 struct zebra_if *zebra_ifp)
986aa00f 1134{
d62a17ae 1135 if (zebra_ifp && zebra_ifp->ptm_enable != ZEBRA_IF_PTM_ENABLE_UNSPEC)
1136 ifp->ptm_enable = zebra_ifp->ptm_enable;
986aa00f 1137}
1138
d62a17ae 1139void zebra_ptm_if_write(struct vty *vty, struct zebra_if *zebra_ifp)
986aa00f 1140{
d62a17ae 1141 if (zebra_ifp->ptm_enable == ZEBRA_IF_PTM_ENABLE_OFF)
1142 vty_out(vty, " no ptm-enable\n");
986aa00f 1143}