]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_ptm.c
*: reindent
[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);
244c1cdc
DS
94
95const char ZEBRA_PTM_SOCK_NAME[] = "\0/var/run/ptmd.socket";
96
d62a17ae 97void zebra_ptm_init(void)
244c1cdc 98{
d62a17ae 99 char buf[64];
c43ed2e4 100
d62a17ae 101 memset(&ptm_cb, 0, sizeof(struct zebra_ptm_cb));
c8ed14dd 102
d62a17ae 103 ptm_cb.out_data = calloc(1, ZEBRA_PTM_SEND_MAX_SOCKBUF);
104 if (!ptm_cb.out_data) {
105 zlog_warn("%s: Allocation of send data failed", __func__);
106 return;
107 }
c8ed14dd 108
d62a17ae 109 ptm_cb.in_data = calloc(1, ZEBRA_PTM_MAX_SOCKBUF);
110 if (!ptm_cb.in_data) {
111 zlog_warn("%s: Allocation of recv data failed", __func__);
112 free(ptm_cb.out_data);
113 return;
114 }
c8ed14dd 115
d62a17ae 116 ptm_cb.pid = getpid();
117 zebra_ptm_install_commands();
c43ed2e4 118
d62a17ae 119 sprintf(buf, "%s", FRR_PTM_NAME);
120 ptm_hdl = ptm_lib_register(buf, NULL, zebra_ptm_handle_msg_cb,
121 zebra_ptm_handle_msg_cb);
122 ptm_cb.wb = buffer_new(0);
c8ed14dd 123
d62a17ae 124 ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_INITIAL;
c8ed14dd 125
d62a17ae 126 ptm_cb.ptm_sock = -1;
c8ed14dd
DS
127}
128
d62a17ae 129void zebra_ptm_finish(void)
c8ed14dd 130{
d62a17ae 131 int proto;
c8ed14dd 132
d62a17ae 133 for (proto = 0; proto < ZEBRA_ROUTE_MAX; proto++)
134 if (CHECK_FLAG(ptm_cb.client_flags[proto],
135 ZEBRA_PTM_BFD_CLIENT_FLAG_REG))
136 zebra_ptm_bfd_client_deregister(proto);
2376c3f2 137
d62a17ae 138 buffer_flush_all(ptm_cb.wb, ptm_cb.ptm_sock);
c8ed14dd 139
d62a17ae 140 free(ptm_hdl);
58ac32e2 141
d62a17ae 142 if (ptm_cb.out_data)
143 free(ptm_cb.out_data);
c8ed14dd 144
d62a17ae 145 if (ptm_cb.in_data)
146 free(ptm_cb.in_data);
c8ed14dd 147
d62a17ae 148 /* Release threads. */
149 if (ptm_cb.t_read)
150 thread_cancel(ptm_cb.t_read);
151 if (ptm_cb.t_write)
152 thread_cancel(ptm_cb.t_write);
153 if (ptm_cb.t_timer)
154 thread_cancel(ptm_cb.t_timer);
2376c3f2 155
d62a17ae 156 if (ptm_cb.wb)
157 buffer_free(ptm_cb.wb);
2376c3f2 158
d62a17ae 159 if (ptm_cb.ptm_sock != -1)
160 close(ptm_cb.ptm_sock);
c8ed14dd
DS
161}
162
d62a17ae 163static int zebra_ptm_flush_messages(struct thread *thread)
c8ed14dd 164{
d62a17ae 165 ptm_cb.t_write = NULL;
166
167 if (ptm_cb.ptm_sock == -1)
168 return -1;
169
170 errno = 0;
171
172 switch (buffer_flush_available(ptm_cb.wb, ptm_cb.ptm_sock)) {
173 case BUFFER_ERROR:
174 zlog_warn("%s ptm socket error: %s", __func__,
175 safe_strerror(errno));
176 close(ptm_cb.ptm_sock);
177 ptm_cb.ptm_sock = -1;
178 zebra_ptm_reset_status(0);
179 ptm_cb.t_timer = NULL;
180 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
181 ptm_cb.reconnect_time, &ptm_cb.t_timer);
182 return (-1);
183 case BUFFER_PENDING:
184 ptm_cb.t_write = NULL;
185 thread_add_write(zebrad.master, zebra_ptm_flush_messages, NULL,
186 ptm_cb.ptm_sock, &ptm_cb.t_write);
187 break;
188 case BUFFER_EMPTY:
189 break;
190 }
191
192 return (0);
c8ed14dd
DS
193}
194
d62a17ae 195static int zebra_ptm_send_message(char *data, int size)
c8ed14dd 196{
d62a17ae 197 errno = 0;
198 switch (buffer_write(ptm_cb.wb, ptm_cb.ptm_sock, data, size)) {
199 case BUFFER_ERROR:
200 zlog_warn("%s ptm socket error: %s", __func__,
201 safe_strerror(errno));
202 close(ptm_cb.ptm_sock);
203 ptm_cb.ptm_sock = -1;
204 zebra_ptm_reset_status(0);
205 ptm_cb.t_timer = NULL;
206 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
207 ptm_cb.reconnect_time, &ptm_cb.t_timer);
208 return -1;
209 case BUFFER_EMPTY:
210 THREAD_OFF(ptm_cb.t_write);
211 break;
212 case BUFFER_PENDING:
213 thread_add_write(zebrad.master, zebra_ptm_flush_messages, NULL,
214 ptm_cb.ptm_sock, &ptm_cb.t_write);
215 break;
216 }
217
218 return 0;
244c1cdc
DS
219}
220
d62a17ae 221int zebra_ptm_connect(struct thread *t)
244c1cdc 222{
d62a17ae 223 int init = 0;
224
225 if (ptm_cb.ptm_sock == -1) {
226 zebra_ptm_socket_init();
227 init = 1;
228 }
229
230 if (ptm_cb.ptm_sock != -1) {
231 if (init) {
232 ptm_cb.t_read = NULL;
233 thread_add_read(zebrad.master, zebra_ptm_sock_read,
234 NULL, ptm_cb.ptm_sock, &ptm_cb.t_read);
235 zebra_bfd_peer_replay_req();
236 }
237 zebra_ptm_send_status_req();
238 ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_INITIAL;
239 } else if (ptm_cb.reconnect_time < ZEBRA_PTM_RECONNECT_TIME_MAX) {
240 ptm_cb.reconnect_time *= 2;
241 if (ptm_cb.reconnect_time > ZEBRA_PTM_RECONNECT_TIME_MAX)
242 ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_MAX;
243
244 ptm_cb.t_timer = NULL;
245 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
246 ptm_cb.reconnect_time, &ptm_cb.t_timer);
247 } else if (ptm_cb.reconnect_time >= ZEBRA_PTM_RECONNECT_TIME_MAX) {
248 ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_INITIAL;
249 }
250
251 return (errno);
244c1cdc
DS
252}
253
244c1cdc
DS
254DEFUN (zebra_ptm_enable,
255 zebra_ptm_enable_cmd,
256 "ptm-enable",
257 "Enable neighbor check with specified topology\n")
258{
d62a17ae 259 struct vrf *vrf;
260 struct listnode *i;
261 struct interface *ifp;
262 struct zebra_if *if_data;
263
264 ptm_cb.ptm_enable = ZEBRA_IF_PTM_ENABLE_ON;
265
266 RB_FOREACH(vrf, vrf_name_head, &vrfs_by_name)
267 for (ALL_LIST_ELEMENTS_RO(vrf->iflist, i, ifp))
268 if (!ifp->ptm_enable) {
269 if_data = (struct zebra_if *)ifp->info;
270 if (if_data && (if_data->ptm_enable
271 == ZEBRA_IF_PTM_ENABLE_UNSPEC)) {
272 ifp->ptm_enable = ZEBRA_IF_PTM_ENABLE_ON;
273 }
274 /* Assign a default unknown status */
275 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
276 }
277
278 zebra_ptm_connect(NULL);
279
280 return CMD_SUCCESS;
244c1cdc
DS
281}
282
283DEFUN (no_zebra_ptm_enable,
284 no_zebra_ptm_enable_cmd,
285 "no ptm-enable",
286 NO_STR
287 "Enable neighbor check with specified topology\n")
288{
d62a17ae 289 ptm_cb.ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF;
290 zebra_ptm_reset_status(1);
291 return CMD_SUCCESS;
244c1cdc
DS
292}
293
986aa00f 294DEFUN (zebra_ptm_enable_if,
295 zebra_ptm_enable_if_cmd,
296 "ptm-enable",
297 "Enable neighbor check with specified topology\n")
298{
d62a17ae 299 VTY_DECLVAR_CONTEXT(interface, ifp);
300 struct zebra_if *if_data;
301 int old_ptm_enable;
302 int send_linkdown = 0;
303
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
323 if_data = ifp->info;
324 if_data->ptm_enable = ZEBRA_IF_PTM_ENABLE_UNSPEC;
325
326 return CMD_SUCCESS;
986aa00f 327}
328
329DEFUN (no_zebra_ptm_enable_if,
330 no_zebra_ptm_enable_if_cmd,
331 "no ptm-enable",
332 NO_STR
333 "Enable neighbor check with specified topology\n")
334{
d62a17ae 335 VTY_DECLVAR_CONTEXT(interface, ifp);
336 int send_linkup = 0;
337 struct zebra_if *if_data;
338
339 if ((ifp->ifindex != IFINDEX_INTERNAL) && (ifp->ptm_enable)) {
340 if (!if_is_operative(ifp))
341 send_linkup = 1;
342
343 ifp->ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF;
344 if (if_is_no_ptm_operative(ifp) && send_linkup) {
345 if (IS_ZEBRA_DEBUG_EVENT)
346 zlog_debug("%s: Bringing up interface %s\n",
347 __func__, ifp->name);
348 if_up(ifp);
349 }
350 }
351
352 if_data = ifp->info;
353 if_data->ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF;
354
355 return CMD_SUCCESS;
986aa00f 356}
357
358
d62a17ae 359void zebra_ptm_write(struct vty *vty)
244c1cdc 360{
d62a17ae 361 if (ptm_cb.ptm_enable)
362 vty_out(vty, "ptm-enable\n");
244c1cdc 363
d62a17ae 364 return;
244c1cdc
DS
365}
366
d62a17ae 367static int zebra_ptm_socket_init(void)
244c1cdc 368{
d62a17ae 369 int ret;
370 int sock;
371 struct sockaddr_un addr;
372
373 ptm_cb.ptm_sock = -1;
374
375 sock = socket(PF_UNIX, SOCK_STREAM, 0);
376 if (sock < 0)
377 return -1;
378 if (set_nonblocking(sock) < 0) {
379 if (IS_ZEBRA_DEBUG_EVENT)
380 zlog_debug("%s: Unable to set socket non blocking[%s]",
381 __PRETTY_FUNCTION__, safe_strerror(errno));
382 close(sock);
383 return -1;
384 }
385
386 /* Make server socket. */
387 memset(&addr, 0, sizeof(struct sockaddr_un));
388 addr.sun_family = AF_UNIX;
389 memcpy(&addr.sun_path, ZEBRA_PTM_SOCK_NAME,
390 sizeof(ZEBRA_PTM_SOCK_NAME));
391
392 ret = connect(sock, (struct sockaddr *)&addr,
393 sizeof(addr.sun_family) + sizeof(ZEBRA_PTM_SOCK_NAME)
394 - 1);
395 if (ret < 0) {
396 if (IS_ZEBRA_DEBUG_EVENT)
397 zlog_debug("%s: Unable to connect to socket %s [%s]",
398 __func__, ZEBRA_PTM_SOCK_NAME,
399 safe_strerror(errno));
400 close(sock);
401 return -1;
402 }
403 ptm_cb.ptm_sock = sock;
404 return sock;
244c1cdc
DS
405}
406
d62a17ae 407static void zebra_ptm_install_commands(void)
244c1cdc 408{
d62a17ae 409 install_element(CONFIG_NODE, &zebra_ptm_enable_cmd);
410 install_element(CONFIG_NODE, &no_zebra_ptm_enable_cmd);
411 install_element(INTERFACE_NODE, &zebra_ptm_enable_if_cmd);
412 install_element(INTERFACE_NODE, &no_zebra_ptm_enable_if_cmd);
244c1cdc
DS
413}
414
c43ed2e4 415/* BFD session goes down, send message to the protocols. */
d62a17ae 416static void if_bfd_session_update(struct interface *ifp, struct prefix *dp,
417 struct prefix *sp, int status,
418 vrf_id_t vrf_id)
c01acf98 419{
d62a17ae 420 if (IS_ZEBRA_DEBUG_EVENT) {
421 char buf[2][INET6_ADDRSTRLEN];
422
423 if (ifp) {
424 zlog_debug(
425 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d on %s"
426 " %s event",
427 inet_ntop(dp->family, &dp->u.prefix, buf[0],
428 INET6_ADDRSTRLEN),
429 dp->prefixlen, ifp->name,
430 bfd_get_status_str(status));
431 } else {
432 zlog_debug(
433 "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d "
434 "with src %s/%d and vrf %d %s event",
435 inet_ntop(dp->family, &dp->u.prefix, buf[0],
436 INET6_ADDRSTRLEN),
437 dp->prefixlen,
438 inet_ntop(sp->family, &sp->u.prefix, buf[1],
439 INET6_ADDRSTRLEN),
440 sp->prefixlen, vrf_id,
441 bfd_get_status_str(status));
442 }
443 }
444
445 zebra_interface_bfd_update(ifp, dp, sp, status, vrf_id);
c01acf98
DS
446}
447
d62a17ae 448static int zebra_ptm_handle_bfd_msg(void *arg, void *in_ctxt,
449 struct interface *ifp)
244c1cdc 450{
d62a17ae 451 char bfdst_str[32];
452 char dest_str[64];
453 char src_str[64];
454 char vrf_str[64];
455 struct prefix dest_prefix;
456 struct prefix src_prefix;
457 vrf_id_t vrf_id;
458
459 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDSTATUS_STR, bfdst_str);
460
461 if (bfdst_str[0] == '\0') {
462 return -1;
463 }
464
465 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDDEST_STR, dest_str);
466
467 if (dest_str[0] == '\0') {
468 zlog_debug("%s: Key %s not found in PTM msg", __func__,
469 ZEBRA_PTM_BFDDEST_STR);
470 return -1;
471 }
472
473 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDSRC_STR, src_str);
474
475 if (src_str[0] == '\0') {
476 zlog_debug("%s: Key %s not found in PTM msg", __func__,
477 ZEBRA_PTM_BFDSRC_STR);
478 return -1;
479 }
480
481 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDVRF_STR, vrf_str);
482
483 if (vrf_str[0] == '\0') {
484 zlog_debug("%s: Key %s not found in PTM msg", __func__,
485 ZEBRA_PTM_BFDVRF_STR);
486 return -1;
487 }
488
489 if (IS_ZEBRA_DEBUG_EVENT)
490 zlog_debug(
491 "%s: Recv Port [%s] bfd status [%s] vrf [%s]"
492 " peer [%s] local [%s]",
493 __func__, ifp ? ifp->name : "N/A", bfdst_str, vrf_str,
494 dest_str, src_str);
495
496 if (str2prefix(dest_str, &dest_prefix) == 0) {
497 zlog_err("%s: Peer addr %s not found", __func__, dest_str);
498 return -1;
499 }
500
501 memset(&src_prefix, 0, sizeof(struct prefix));
502 if (strcmp(ZEBRA_PTM_INVALID_SRC_IP, src_str)) {
503 if (str2prefix(src_str, &src_prefix) == 0) {
504 zlog_err("%s: Local addr %s not found", __func__,
505 src_str);
506 return -1;
507 }
508 }
509
510 if (!strcmp(ZEBRA_PTM_INVALID_VRF, vrf_str) && ifp) {
511 vrf_id = ifp->vrf_id;
512 } else {
513 vrf_id = vrf_name_to_id(vrf_str);
514 }
515
516 if (!strcmp(bfdst_str, ZEBRA_PTM_BFDSTATUS_DOWN_STR)) {
517 if_bfd_session_update(ifp, &dest_prefix, &src_prefix,
518 BFD_STATUS_DOWN, vrf_id);
519 } else {
520 if_bfd_session_update(ifp, &dest_prefix, &src_prefix,
521 BFD_STATUS_UP, vrf_id);
522 }
523
524 return 0;
c01acf98
DS
525}
526
d62a17ae 527static int zebra_ptm_handle_cbl_msg(void *arg, void *in_ctxt,
528 struct interface *ifp, char *cbl_str)
c01acf98 529{
d62a17ae 530 int send_linkup = 0;
531
532 if (IS_ZEBRA_DEBUG_EVENT)
533 zlog_debug("%s: Recv Port [%s] cbl status [%s]", __func__,
534 ifp->name, cbl_str);
535
536 if (!strcmp(cbl_str, ZEBRA_PTM_PASS_STR)
537 && (ifp->ptm_status != ZEBRA_PTM_STATUS_UP)) {
538
539 if (ifp->ptm_status == ZEBRA_PTM_STATUS_DOWN)
540 send_linkup = 1;
541 ifp->ptm_status = ZEBRA_PTM_STATUS_UP;
542 if (ifp->ptm_enable && if_is_no_ptm_operative(ifp)
543 && send_linkup)
544 if_up(ifp);
545 } else if (!strcmp(cbl_str, ZEBRA_PTM_FAIL_STR)
546 && (ifp->ptm_status != ZEBRA_PTM_STATUS_DOWN)) {
547 ifp->ptm_status = ZEBRA_PTM_STATUS_DOWN;
548 if (ifp->ptm_enable && if_is_no_ptm_operative(ifp))
549 if_down(ifp);
550 }
551
552 return 0;
b255a4b1 553}
950bd436 554
b255a4b1 555/*
556 * zebra_ptm_handle_msg_cb - The purpose of this callback function is to handle
557 * all the command responses and notifications received from PTM.
558 *
559 * Command responses: Upon establishing connection with PTM, Zebra requests
560 * status of all interfaces using 'get-status' command if global ptm-enable
561 * knob is enabled. As a response to the get-status command PTM sends status
562 * of all the interfaces as command responses. All other type of command
563 * responses with cmd_status key word are dropped. The sole purpose of
564 * registering this function as callback for the command responses is to
565 * handle the responses to get-status command.
566 *
567 * Notifications: Cable status and BFD session status changes are sent as
568 * notifications by PTM. So, this function is also the callback function for
569 * processing all the notifications from the PTM.
570 *
571 */
d62a17ae 572static int zebra_ptm_handle_msg_cb(void *arg, void *in_ctxt)
b255a4b1 573{
d62a17ae 574 struct interface *ifp = NULL;
575 char port_str[128];
576 char cbl_str[32];
577 char cmd_status_str[32];
578
579 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_CMD_STATUS_STR,
580 cmd_status_str);
581
582 /* Drop command response messages */
583 if (cmd_status_str[0] != '\0') {
584 return 0;
585 }
586
587 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_PORT_STR, port_str);
588
589 if (port_str[0] == '\0') {
590 zlog_debug("%s: Key %s not found in PTM msg", __func__,
591 ZEBRA_PTM_PORT_STR);
592 return -1;
593 }
594
595 if (strcmp(ZEBRA_PTM_INVALID_PORT_NAME, port_str)) {
596 ifp = if_lookup_by_name_all_vrf(port_str);
597
598 if (!ifp) {
599 zlog_err("%s: %s not found in interface list", __func__,
600 port_str);
601 return -1;
602 }
603 }
604
605 ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_CBL_STR, cbl_str);
606
607 if (cbl_str[0] == '\0') {
608 return zebra_ptm_handle_bfd_msg(arg, in_ctxt, ifp);
609 } else {
610 if (ifp) {
611 return zebra_ptm_handle_cbl_msg(arg, in_ctxt, ifp,
612 cbl_str);
613 } else {
614 return -1;
615 }
616 }
c01acf98
DS
617}
618
d62a17ae 619int zebra_ptm_sock_read(struct thread *thread)
c01acf98 620{
d62a17ae 621 int sock, done = 0;
622 int rc;
623
624 errno = 0;
625 sock = THREAD_FD(thread);
626
627 if (sock == -1)
628 return -1;
629
630 /* PTM communicates in CSV format */
631 while (!done) {
632 rc = ptm_lib_process_msg(ptm_hdl, sock, ptm_cb.in_data,
633 ZEBRA_PTM_MAX_SOCKBUF, NULL);
634 if (rc <= 0)
635 break;
636 }
637
638 if (rc <= 0) {
639 if (((rc == 0) && !errno)
640 || (errno && (errno != EWOULDBLOCK) && (errno != EAGAIN))) {
641 zlog_warn("%s routing socket error: %s(%d) bytes %d",
642 __func__, safe_strerror(errno), errno, rc);
643
644 close(ptm_cb.ptm_sock);
645 ptm_cb.ptm_sock = -1;
646 zebra_ptm_reset_status(0);
647 ptm_cb.t_timer = NULL;
648 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
649 ptm_cb.reconnect_time,
650 &ptm_cb.t_timer);
651 return (-1);
652 }
653 }
654
655 ptm_cb.t_read = NULL;
656 thread_add_read(zebrad.master, zebra_ptm_sock_read, NULL,
657 ptm_cb.ptm_sock, &ptm_cb.t_read);
658
659 return 0;
c43ed2e4
DS
660}
661
662/* BFD peer/dst register/update */
d62a17ae 663int zebra_ptm_bfd_dst_register(struct zserv *client, int sock, u_short length,
664 int command, struct zebra_vrf *zvrf)
c43ed2e4 665{
d62a17ae 666 struct stream *s;
667 struct prefix src_p;
668 struct prefix dst_p;
669 u_char multi_hop;
670 u_char multi_hop_cnt;
671 u_char detect_mul;
672 unsigned int min_rx_timer;
673 unsigned int min_tx_timer;
674 char if_name[INTERFACE_NAMSIZ];
675 u_char len;
676 void *out_ctxt;
677 char buf[INET6_ADDRSTRLEN];
678 char tmp_buf[64];
679 int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
680 unsigned int pid;
681
682 if (command == ZEBRA_BFD_DEST_UPDATE)
683 client->bfd_peer_upd8_cnt++;
684 else
685 client->bfd_peer_add_cnt++;
686
687 if (IS_ZEBRA_DEBUG_EVENT)
688 zlog_debug("bfd_dst_register msg from client %s: length=%d",
689 zebra_route_string(client->proto), length);
690
691 if (ptm_cb.ptm_sock == -1) {
692 ptm_cb.t_timer = NULL;
693 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
694 ptm_cb.reconnect_time, &ptm_cb.t_timer);
695 return -1;
696 }
697
698 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
699 sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_START_CMD);
700 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
701 sprintf(tmp_buf, "%s", zebra_route_string(client->proto));
702 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
703 tmp_buf);
704
705 s = client->ibuf;
706
707 pid = stream_getl(s);
708 sprintf(tmp_buf, "%d", pid);
709 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD,
710 tmp_buf);
711
712 dst_p.family = stream_getw(s);
713
714 if (dst_p.family == AF_INET)
715 dst_p.prefixlen = IPV4_MAX_BYTELEN;
716 else
717 dst_p.prefixlen = IPV6_MAX_BYTELEN;
718
719 stream_get(&dst_p.u.prefix, s, dst_p.prefixlen);
720 if (dst_p.family == AF_INET) {
721 inet_ntop(AF_INET, &dst_p.u.prefix4, buf, sizeof(buf));
722 ptm_lib_append_msg(ptm_hdl, out_ctxt,
723 ZEBRA_PTM_BFD_DST_IP_FIELD, buf);
724 } else {
725 inet_ntop(AF_INET6, &dst_p.u.prefix6, buf, sizeof(buf));
726 ptm_lib_append_msg(ptm_hdl, out_ctxt,
727 ZEBRA_PTM_BFD_DST_IP_FIELD, buf);
728 }
729
730 min_rx_timer = stream_getl(s);
731 sprintf(tmp_buf, "%d", min_rx_timer);
732 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_MIN_RX_FIELD,
733 tmp_buf);
734 min_tx_timer = stream_getl(s);
735 sprintf(tmp_buf, "%d", min_tx_timer);
736 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_MIN_TX_FIELD,
737 tmp_buf);
738 detect_mul = stream_getc(s);
739 sprintf(tmp_buf, "%d", detect_mul);
740 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_DETECT_MULT_FIELD,
741 tmp_buf);
742
743 multi_hop = stream_getc(s);
744 if (multi_hop) {
745 sprintf(tmp_buf, "%d", 1);
746 ptm_lib_append_msg(ptm_hdl, out_ctxt,
747 ZEBRA_PTM_BFD_MULTI_HOP_FIELD, tmp_buf);
748 src_p.family = stream_getw(s);
749
750 if (src_p.family == AF_INET)
751 src_p.prefixlen = IPV4_MAX_BYTELEN;
752 else
753 src_p.prefixlen = IPV6_MAX_BYTELEN;
754
755 stream_get(&src_p.u.prefix, s, src_p.prefixlen);
756 if (src_p.family == AF_INET) {
757 inet_ntop(AF_INET, &src_p.u.prefix4, buf, sizeof(buf));
758 ptm_lib_append_msg(ptm_hdl, out_ctxt,
759 ZEBRA_PTM_BFD_SRC_IP_FIELD, buf);
760 } else {
761 inet_ntop(AF_INET6, &src_p.u.prefix6, buf, sizeof(buf));
762 ptm_lib_append_msg(ptm_hdl, out_ctxt,
763 ZEBRA_PTM_BFD_SRC_IP_FIELD, buf);
764 }
765
766 multi_hop_cnt = stream_getc(s);
767 sprintf(tmp_buf, "%d", multi_hop_cnt);
768 ptm_lib_append_msg(ptm_hdl, out_ctxt,
769 ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD, tmp_buf);
770
771 if (zvrf_id(zvrf) != VRF_DEFAULT)
772 ptm_lib_append_msg(ptm_hdl, out_ctxt,
773 ZEBRA_PTM_BFD_VRF_NAME_FIELD,
774 zvrf_name(zvrf));
775 } else {
776 if (dst_p.family == AF_INET6) {
777 src_p.family = stream_getw(s);
778
779 if (src_p.family == AF_INET)
780 src_p.prefixlen = IPV4_MAX_BYTELEN;
781 else
782 src_p.prefixlen = IPV6_MAX_BYTELEN;
783
784 stream_get(&src_p.u.prefix, s, src_p.prefixlen);
785 if (src_p.family == AF_INET) {
786 inet_ntop(AF_INET, &src_p.u.prefix4, buf,
787 sizeof(buf));
788 ptm_lib_append_msg(ptm_hdl, out_ctxt,
789 ZEBRA_PTM_BFD_SRC_IP_FIELD,
790 buf);
791 } else {
792 inet_ntop(AF_INET6, &src_p.u.prefix6, buf,
793 sizeof(buf));
794 ptm_lib_append_msg(ptm_hdl, out_ctxt,
795 ZEBRA_PTM_BFD_SRC_IP_FIELD,
796 buf);
797 }
798 }
799 len = stream_getc(s);
800 stream_get(if_name, s, len);
801 if_name[len] = '\0';
802
803 ptm_lib_append_msg(ptm_hdl, out_ctxt,
804 ZEBRA_PTM_BFD_IFNAME_FIELD, if_name);
805 }
806
807 sprintf(tmp_buf, "%d", 1);
808 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEND_EVENT,
809 tmp_buf);
810
811 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
812
813 if (IS_ZEBRA_DEBUG_SEND)
814 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
815 ptm_cb.out_data);
816 zebra_ptm_send_message(ptm_cb.out_data, data_len);
817 return 0;
c43ed2e4
DS
818}
819
820/* BFD peer/dst deregister */
d62a17ae 821int zebra_ptm_bfd_dst_deregister(struct zserv *client, int sock, u_short length,
822 struct zebra_vrf *zvrf)
c43ed2e4 823{
d62a17ae 824 struct stream *s;
825 struct prefix src_p;
826 struct prefix dst_p;
827 u_char multi_hop;
828 char if_name[INTERFACE_NAMSIZ];
829 u_char len;
830 char buf[INET6_ADDRSTRLEN];
831 char tmp_buf[64];
832 int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
833 void *out_ctxt;
834 unsigned int pid;
835
836 client->bfd_peer_del_cnt++;
837
838 if (IS_ZEBRA_DEBUG_EVENT)
839 zlog_debug("bfd_dst_deregister msg from client %s: length=%d",
840 zebra_route_string(client->proto), length);
841
842 if (ptm_cb.ptm_sock == -1) {
843 ptm_cb.t_timer = NULL;
844 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
845 ptm_cb.reconnect_time, &ptm_cb.t_timer);
846 return -1;
847 }
848
849 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
850
851 sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_STOP_CMD);
852 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
853
854 sprintf(tmp_buf, "%s", zebra_route_string(client->proto));
855 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
856 tmp_buf);
857
858 s = client->ibuf;
859
860 pid = stream_getl(s);
861 sprintf(tmp_buf, "%d", pid);
862 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD,
863 tmp_buf);
864
865 dst_p.family = stream_getw(s);
866
867 if (dst_p.family == AF_INET)
868 dst_p.prefixlen = IPV4_MAX_BYTELEN;
869 else
870 dst_p.prefixlen = IPV6_MAX_BYTELEN;
871
872 stream_get(&dst_p.u.prefix, s, dst_p.prefixlen);
873 if (dst_p.family == AF_INET)
874 inet_ntop(AF_INET, &dst_p.u.prefix4, buf, sizeof(buf));
875 else
876 inet_ntop(AF_INET6, &dst_p.u.prefix6, buf, sizeof(buf));
877 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_DST_IP_FIELD, buf);
878
879
880 multi_hop = stream_getc(s);
881 if (multi_hop) {
882 sprintf(tmp_buf, "%d", 1);
883 ptm_lib_append_msg(ptm_hdl, out_ctxt,
884 ZEBRA_PTM_BFD_MULTI_HOP_FIELD, tmp_buf);
885
886 src_p.family = stream_getw(s);
887
888 if (src_p.family == AF_INET)
889 src_p.prefixlen = IPV4_MAX_BYTELEN;
890 else
891 src_p.prefixlen = IPV6_MAX_BYTELEN;
892
893 stream_get(&src_p.u.prefix, s, src_p.prefixlen);
894 if (src_p.family == AF_INET)
895 inet_ntop(AF_INET, &src_p.u.prefix4, buf, sizeof(buf));
896 else
897 inet_ntop(AF_INET6, &src_p.u.prefix6, buf, sizeof(buf));
898 ptm_lib_append_msg(ptm_hdl, out_ctxt,
899 ZEBRA_PTM_BFD_SRC_IP_FIELD, buf);
900
901 if (zvrf_id(zvrf) != VRF_DEFAULT)
902 ptm_lib_append_msg(ptm_hdl, out_ctxt,
903 ZEBRA_PTM_BFD_VRF_NAME_FIELD,
904 zvrf_name(zvrf));
905 } else {
906 if (dst_p.family == AF_INET6) {
907 src_p.family = stream_getw(s);
908
909 if (src_p.family == AF_INET)
910 src_p.prefixlen = IPV4_MAX_BYTELEN;
911 else
912 src_p.prefixlen = IPV6_MAX_BYTELEN;
913
914 stream_get(&src_p.u.prefix, s, src_p.prefixlen);
915 if (src_p.family == AF_INET) {
916 inet_ntop(AF_INET, &src_p.u.prefix4, buf,
917 sizeof(buf));
918 ptm_lib_append_msg(ptm_hdl, out_ctxt,
919 ZEBRA_PTM_BFD_SRC_IP_FIELD,
920 buf);
921 } else {
922 inet_ntop(AF_INET6, &src_p.u.prefix6, buf,
923 sizeof(buf));
924 ptm_lib_append_msg(ptm_hdl, out_ctxt,
925 ZEBRA_PTM_BFD_SRC_IP_FIELD,
926 buf);
927 }
928 }
929
930 len = stream_getc(s);
931 stream_get(if_name, s, len);
932 if_name[len] = '\0';
933
934 ptm_lib_append_msg(ptm_hdl, out_ctxt,
935 ZEBRA_PTM_BFD_IFNAME_FIELD, if_name);
936 }
937
938 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
939 if (IS_ZEBRA_DEBUG_SEND)
940 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
941 ptm_cb.out_data);
942
943 zebra_ptm_send_message(ptm_cb.out_data, data_len);
944 return 0;
c01acf98 945}
c8ed14dd 946
055c4dfc 947/* BFD client register */
d62a17ae 948int zebra_ptm_bfd_client_register(struct zserv *client, int sock,
949 u_short length)
055c4dfc 950{
d62a17ae 951 struct stream *s;
952 unsigned int pid;
953 void *out_ctxt;
954 char tmp_buf[64];
955 int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
055c4dfc 956
d62a17ae 957 client->bfd_client_reg_cnt++;
055c4dfc 958
d62a17ae 959 if (IS_ZEBRA_DEBUG_EVENT)
960 zlog_debug("bfd_client_register msg from client %s: length=%d",
961 zebra_route_string(client->proto), length);
055c4dfc 962
d62a17ae 963 if (ptm_cb.ptm_sock == -1) {
964 ptm_cb.t_timer = NULL;
965 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
966 ptm_cb.reconnect_time, &ptm_cb.t_timer);
967 return -1;
968 }
055c4dfc 969
d62a17ae 970 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
055c4dfc 971
d62a17ae 972 sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_CLIENT_REG_CMD);
973 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
055c4dfc 974
d62a17ae 975 sprintf(tmp_buf, "%s", zebra_route_string(client->proto));
976 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
977 tmp_buf);
055c4dfc 978
d62a17ae 979 s = client->ibuf;
055c4dfc 980
d62a17ae 981 pid = stream_getl(s);
982 sprintf(tmp_buf, "%d", pid);
983 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD,
984 tmp_buf);
055c4dfc 985
d62a17ae 986 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
055c4dfc 987
d62a17ae 988 if (IS_ZEBRA_DEBUG_SEND)
989 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
990 ptm_cb.out_data);
991 zebra_ptm_send_message(ptm_cb.out_data, data_len);
2376c3f2 992
d62a17ae 993 SET_FLAG(ptm_cb.client_flags[client->proto],
994 ZEBRA_PTM_BFD_CLIENT_FLAG_REG);
995 return 0;
055c4dfc 996}
997
567b877d 998/* BFD client deregister */
d62a17ae 999void zebra_ptm_bfd_client_deregister(int proto)
567b877d 1000{
d62a17ae 1001 void *out_ctxt;
1002 char tmp_buf[64];
1003 int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
567b877d 1004
d62a17ae 1005 if (proto != ZEBRA_ROUTE_OSPF && proto != ZEBRA_ROUTE_BGP
1006 && proto != ZEBRA_ROUTE_OSPF6 && proto != ZEBRA_ROUTE_PIM)
1007 return;
567b877d 1008
d62a17ae 1009 if (IS_ZEBRA_DEBUG_EVENT)
1010 zlog_err("bfd_client_deregister msg for client %s",
1011 zebra_route_string(proto));
567b877d 1012
d62a17ae 1013 if (ptm_cb.ptm_sock == -1) {
1014 ptm_cb.t_timer = NULL;
1015 thread_add_timer(zebrad.master, zebra_ptm_connect, NULL,
1016 ptm_cb.reconnect_time, &ptm_cb.t_timer);
1017 return;
1018 }
567b877d 1019
d62a17ae 1020 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt);
567b877d 1021
d62a17ae 1022 sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_CLIENT_DEREG_CMD);
1023 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf);
567b877d 1024
d62a17ae 1025 sprintf(tmp_buf, "%s", zebra_route_string(proto));
1026 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD,
1027 tmp_buf);
567b877d 1028
d62a17ae 1029 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len);
567b877d 1030
d62a17ae 1031 if (IS_ZEBRA_DEBUG_SEND)
1032 zlog_debug("%s: Sent message (%d) %s", __func__, data_len,
1033 ptm_cb.out_data);
2376c3f2 1034
d62a17ae 1035 zebra_ptm_send_message(ptm_cb.out_data, data_len);
1036 UNSET_FLAG(ptm_cb.client_flags[proto], ZEBRA_PTM_BFD_CLIENT_FLAG_REG);
567b877d 1037}
1038
d62a17ae 1039int zebra_ptm_get_enable_state(void)
c8ed14dd 1040{
d62a17ae 1041 return ptm_cb.ptm_enable;
c8ed14dd 1042}
950bd436 1043
1044/*
1045 * zebra_ptm_get_status_str - Convert status to a display string.
1046 */
d62a17ae 1047static const char *zebra_ptm_get_status_str(int status)
950bd436 1048{
d62a17ae 1049 switch (status) {
1050 case ZEBRA_PTM_STATUS_DOWN:
1051 return "fail";
1052 case ZEBRA_PTM_STATUS_UP:
1053 return "pass";
1054 case ZEBRA_PTM_STATUS_UNKNOWN:
1055 default:
1056 return "n/a";
1057 }
950bd436 1058}
1059
d62a17ae 1060void zebra_ptm_show_status(struct vty *vty, struct interface *ifp)
950bd436 1061{
d62a17ae 1062 vty_out(vty, " PTM status: ");
1063 if (ifp->ptm_enable) {
1064 vty_out(vty, "%s\n", zebra_ptm_get_status_str(ifp->ptm_status));
1065 } else {
1066 vty_out(vty, "disabled\n");
1067 }
950bd436 1068}
1069
d62a17ae 1070void zebra_ptm_send_status_req(void)
950bd436 1071{
d62a17ae 1072 void *out_ctxt;
1073 int len = ZEBRA_PTM_SEND_MAX_SOCKBUF;
1074
1075 if (ptm_cb.ptm_enable) {
1076 ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL,
1077 &out_ctxt);
1078 ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR,
1079 ZEBRA_PTM_GET_STATUS_CMD);
1080 ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &len);
1081
1082 zebra_ptm_send_message(ptm_cb.out_data, len);
1083 }
950bd436 1084}
1085
d62a17ae 1086void zebra_ptm_reset_status(int ptm_disable)
950bd436 1087{
d62a17ae 1088 struct vrf *vrf;
1089 struct listnode *i;
1090 struct interface *ifp;
1091 int send_linkup;
1092
1093 RB_FOREACH(vrf, vrf_id_head, &vrfs_by_id)
1094 for (ALL_LIST_ELEMENTS_RO(vrf->iflist, i, ifp)) {
1095 send_linkup = 0;
1096 if (ifp->ptm_enable) {
1097 if (!if_is_operative(ifp))
1098 send_linkup = 1;
1099
1100 if (ptm_disable)
1101 ifp->ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF;
1102 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
1103
1104 if (if_is_operative(ifp) && send_linkup) {
1105 if (IS_ZEBRA_DEBUG_EVENT)
1106 zlog_debug(
1107 "%s: Bringing up interface %s",
1108 __func__, ifp->name);
1109 if_up(ifp);
1110 }
1111 }
1112 }
950bd436 1113}
986aa00f 1114
d62a17ae 1115void zebra_ptm_if_init(struct zebra_if *zebra_ifp)
986aa00f 1116{
d62a17ae 1117 zebra_ifp->ptm_enable = ZEBRA_IF_PTM_ENABLE_UNSPEC;
986aa00f 1118}
1119
d62a17ae 1120void zebra_ptm_if_set_ptm_state(struct interface *ifp,
1121 struct zebra_if *zebra_ifp)
986aa00f 1122{
d62a17ae 1123 if (zebra_ifp && zebra_ifp->ptm_enable != ZEBRA_IF_PTM_ENABLE_UNSPEC)
1124 ifp->ptm_enable = zebra_ifp->ptm_enable;
986aa00f 1125}
1126
d62a17ae 1127void zebra_ptm_if_write(struct vty *vty, struct zebra_if *zebra_ifp)
986aa00f 1128{
d62a17ae 1129 if (zebra_ifp->ptm_enable == ZEBRA_IF_PTM_ENABLE_OFF)
1130 vty_out(vty, " no ptm-enable\n");
986aa00f 1131}