3 * Copyright (C) 2018 Network Device Education Foundation, Inc. ("NetDEF")
5 * FRR is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2, or (at your option) any
10 * FRR is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with FRR; see the file COPYING. If not, write to the Free
17 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 #include "lib/libfrr.h"
24 #include "lib/queue.h"
25 #include "lib/stream.h"
26 #include "lib/zclient.h"
27 #include "lib/printfrr.h"
36 struct ptm_client_notification
{
37 struct bfd_session
*pcn_bs
;
38 struct ptm_client
*pcn_pc
;
40 TAILQ_ENTRY(ptm_client_notification
) pcn_entry
;
42 TAILQ_HEAD(pcnqueue
, ptm_client_notification
);
46 struct pcnqueue pc_pcnqueue
;
48 TAILQ_ENTRY(ptm_client
) pc_entry
;
50 TAILQ_HEAD(pcqueue
, ptm_client
);
52 static struct pcqueue pcqueue
;
53 static struct zclient
*zclient
;
59 static int _ptm_msg_address(struct stream
*msg
, int family
, const void *addr
);
61 static void _ptm_msg_read_address(struct stream
*msg
, struct sockaddr_any
*sa
);
62 static int _ptm_msg_read(struct stream
*msg
, int command
, vrf_id_t vrf_id
,
63 struct bfd_peer_cfg
*bpc
, struct ptm_client
**pc
);
65 static struct ptm_client
*pc_lookup(uint32_t pid
);
66 static struct ptm_client
*pc_new(uint32_t pid
);
67 static void pc_free(struct ptm_client
*pc
);
68 static void pc_free_all(void);
69 static struct ptm_client_notification
*pcn_new(struct ptm_client
*pc
,
70 struct bfd_session
*bs
);
71 static struct ptm_client_notification
*pcn_lookup(struct ptm_client
*pc
,
72 struct bfd_session
*bs
);
73 static void pcn_free(struct ptm_client_notification
*pcn
);
76 static void bfdd_dest_register(struct stream
*msg
, vrf_id_t vrf_id
);
77 static void bfdd_dest_deregister(struct stream
*msg
, vrf_id_t vrf_id
);
78 static void bfdd_client_register(struct stream
*msg
);
79 static void bfdd_client_deregister(struct stream
*msg
);
85 static void debug_printbpc(const struct bfd_peer_cfg
*bpc
, const char *fmt
, ...)
87 char timers
[3][128] = {};
88 char minttl_str
[32] = {};
89 char addr
[3][128] = {};
90 char profile
[128] = {};
95 /* Avoid debug calculations if it's disabled. */
96 if (bglobal
.debug_zebra
== false)
99 snprintf(addr
[0], sizeof(addr
[0]), "peer:%s", satostr(&bpc
->bpc_peer
));
100 if (bpc
->bpc_local
.sa_sin
.sin_family
)
101 snprintf(addr
[1], sizeof(addr
[1]), " local:%s",
102 satostr(&bpc
->bpc_local
));
104 if (bpc
->bpc_has_localif
)
105 snprintf(addr
[2], sizeof(addr
[2]), " ifname:%s",
108 if (bpc
->bpc_has_vrfname
)
109 snprintf(addr
[2], sizeof(addr
[2]), " vrf:%s", bpc
->bpc_vrfname
);
111 if (bpc
->bpc_has_recvinterval
)
112 snprintfrr(timers
[0], sizeof(timers
[0]), " rx:%" PRIu64
,
113 bpc
->bpc_recvinterval
);
115 if (bpc
->bpc_has_txinterval
)
116 snprintfrr(timers
[1], sizeof(timers
[1]), " tx:%" PRIu64
,
117 bpc
->bpc_recvinterval
);
119 if (bpc
->bpc_has_detectmultiplier
)
120 snprintf(timers
[2], sizeof(timers
[2]), " detect-multiplier:%d",
121 bpc
->bpc_detectmultiplier
);
123 snprintf(cbit_str
, sizeof(cbit_str
), " cbit:0x%02x", bpc
->bpc_cbit
);
125 if (bpc
->bpc_has_minimum_ttl
)
126 snprintf(minttl_str
, sizeof(minttl_str
), " minimum-ttl:%d",
127 bpc
->bpc_minimum_ttl
);
129 if (bpc
->bpc_has_profile
)
130 snprintf(profile
, sizeof(profile
), " profile:%s",
134 vsnprintf(msgbuf
, sizeof(msgbuf
), fmt
, vl
);
137 zlog_debug("%s [mhop:%s %s%s%s%s%s%s%s%s%s]", msgbuf
,
138 bpc
->bpc_mhop
? "yes" : "no", addr
[0], addr
[1], addr
[2],
139 timers
[0], timers
[1], timers
[2], cbit_str
, minttl_str
,
143 static void _ptm_bfd_session_del(struct bfd_session
*bs
, uint8_t diag
)
145 if (bglobal
.debug_peer_event
)
146 zlog_debug("session-delete: %s", bs_to_string(bs
));
148 /* Change state and notify peer. */
149 bs
->ses_state
= PTM_BFD_DOWN
;
150 bs
->local_diag
= diag
;
153 /* Session reached refcount == 0, lets delete it. */
154 if (bs
->refcount
== 0) {
156 * Sanity check: if there is a refcount bug, we can't delete
157 * the session a user configured manually. Lets leave a
158 * message here so we can catch the bug if it exists.
160 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_CONFIG
)) {
162 "ptm-del-session: [%s] session refcount is zero but it was configured by CLI",
165 control_notify_config(BCM_NOTIFY_CONFIG_DELETE
, bs
);
166 bfd_session_free(bs
);
171 static int _ptm_msg_address(struct stream
*msg
, int family
, const void *addr
)
173 stream_putc(msg
, family
);
177 stream_put(msg
, addr
, sizeof(struct in_addr
));
178 stream_putc(msg
, 32);
182 stream_put(msg
, addr
, sizeof(struct in6_addr
));
183 stream_putc(msg
, 128);
194 int ptm_bfd_notify(struct bfd_session
*bs
, uint8_t notify_state
)
198 bs
->stats
.znotification
++;
202 * - header: command, vrf
203 * - l: interface index
219 * Commands: ZEBRA_BFD_DEST_REPLAY
221 * q(64), l(32), w(16), c(8)
226 /* TODO: VRF handling */
228 zclient_create_header(msg
, ZEBRA_BFD_DEST_REPLAY
, bs
->vrf
->vrf_id
);
230 zclient_create_header(msg
, ZEBRA_BFD_DEST_REPLAY
, VRF_DEFAULT
);
232 /* This header will be handled by `zebra_ptm.c`. */
233 stream_putl(msg
, ZEBRA_INTERFACE_BFD_DEST_UPDATE
);
235 /* NOTE: Interface is a shortcut to avoid comparing source address. */
236 if (!CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
) && bs
->ifp
!= NULL
)
237 stream_putl(msg
, bs
->ifp
->ifindex
);
239 stream_putl(msg
, IFINDEX_INTERNAL
);
241 /* BFD destination prefix information. */
242 _ptm_msg_address(msg
, bs
->key
.family
, &bs
->key
.peer
);
245 switch (notify_state
) {
247 stream_putl(msg
, BFD_STATUS_UP
);
250 case PTM_BFD_ADM_DOWN
:
251 stream_putl(msg
, BFD_STATUS_ADMIN_DOWN
);
256 stream_putl(msg
, BFD_STATUS_DOWN
);
260 stream_putl(msg
, BFD_STATUS_UNKNOWN
);
264 /* BFD source prefix information. */
265 _ptm_msg_address(msg
, bs
->key
.family
, &bs
->key
.local
);
267 stream_putc(msg
, bs
->remote_cbit
);
269 /* Write packet size. */
270 stream_putw_at(msg
, 0, stream_get_endp(msg
));
272 return zclient_send_message(zclient
);
275 static void _ptm_msg_read_address(struct stream
*msg
, struct sockaddr_any
*sa
)
279 STREAM_GETW(msg
, family
);
283 sa
->sa_sin
.sin_family
= family
;
284 STREAM_GET(&sa
->sa_sin
.sin_addr
, msg
,
285 sizeof(sa
->sa_sin
.sin_addr
));
286 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
287 sa
->sa_sin
.sin_len
= sizeof(sa
->sa_sin
);
288 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
292 sa
->sa_sin6
.sin6_family
= family
;
293 STREAM_GET(&sa
->sa_sin6
.sin6_addr
, msg
,
294 sizeof(sa
->sa_sin6
.sin6_addr
));
295 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
296 sa
->sa_sin6
.sin6_len
= sizeof(sa
->sa_sin6
);
297 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
301 zlog_warn("ptm-read-address: invalid family: %d", family
);
306 memset(sa
, 0, sizeof(*sa
));
309 static int _ptm_msg_read(struct stream
*msg
, int command
, vrf_id_t vrf_id
,
310 struct bfd_peer_cfg
*bpc
, struct ptm_client
**pc
)
316 * Register/Deregister/Update Message format:
318 * Old format (being used by PTM BFD).
319 * - header: Command, VRF
323 * - l: destination ipv4
325 * - 16 bytes: destination IPv6
326 * - command != ZEBRA_BFD_DEST_DEREGISTER
329 * - c: detect multiplier
334 * - l: source IPv4 address
336 * - 16 bytes: source IPv6 address
341 * - 16 bytes: source IPv6 address
343 * - X bytes: interface name
346 * - header: Command, VRF
350 * - l: destination IPv4 address
352 * - 16 bytes: destination IPv6 address
355 * - c: detect multiplier
359 * - l: source IPv4 address
361 * - 16 bytes: source IPv6 address
364 * - X bytes: interface name
366 * - c: profile name length.
367 * - X bytes: profile name.
369 * q(64), l(32), w(16), c(8)
372 /* Initialize parameters return values. */
373 memset(bpc
, 0, sizeof(*bpc
));
376 /* Find or allocate process context data. */
377 STREAM_GETL(msg
, pid
);
381 /* Register/update peer information. */
382 _ptm_msg_read_address(msg
, &bpc
->bpc_peer
);
384 /* Determine IP type from peer destination. */
385 bpc
->bpc_ipv4
= (bpc
->bpc_peer
.sa_sin
.sin_family
== AF_INET
);
387 /* Get peer configuration. */
388 STREAM_GETL(msg
, bpc
->bpc_recvinterval
);
389 bpc
->bpc_has_recvinterval
=
390 (bpc
->bpc_recvinterval
!= BPC_DEF_RECEIVEINTERVAL
);
392 STREAM_GETL(msg
, bpc
->bpc_txinterval
);
393 bpc
->bpc_has_txinterval
=
394 (bpc
->bpc_txinterval
!= BPC_DEF_TRANSMITINTERVAL
);
396 STREAM_GETC(msg
, bpc
->bpc_detectmultiplier
);
397 bpc
->bpc_has_detectmultiplier
=
398 (bpc
->bpc_detectmultiplier
!= BPC_DEF_DETECTMULTIPLIER
);
400 /* Read (single|multi)hop and its options. */
401 STREAM_GETC(msg
, bpc
->bpc_mhop
);
403 /* Read multihop source address and TTL. */
404 _ptm_msg_read_address(msg
, &bpc
->bpc_local
);
406 /* Read the minimum TTL (0 means unset or invalid). */
407 STREAM_GETC(msg
, bpc
->bpc_minimum_ttl
);
408 if (bpc
->bpc_minimum_ttl
== 0) {
409 bpc
->bpc_minimum_ttl
= BFD_DEF_MHOP_TTL
;
410 bpc
->bpc_has_minimum_ttl
= false;
412 bpc
->bpc_minimum_ttl
= (BFD_TTL_VAL
+ 1) - bpc
->bpc_minimum_ttl
;
413 bpc
->bpc_has_minimum_ttl
= true;
417 * Read interface name and make sure it fits our data
418 * structure, otherwise fail.
420 STREAM_GETC(msg
, ifnamelen
);
421 if (ifnamelen
>= sizeof(bpc
->bpc_localif
)) {
422 zlog_err("ptm-read: interface name is too big");
426 bpc
->bpc_has_localif
= ifnamelen
> 0;
427 if (bpc
->bpc_has_localif
) {
428 STREAM_GET(bpc
->bpc_localif
, msg
, ifnamelen
);
429 bpc
->bpc_localif
[ifnamelen
] = 0;
432 if (vrf_id
!= VRF_DEFAULT
) {
435 vrf
= vrf_lookup_by_id(vrf_id
);
437 bpc
->bpc_has_vrfname
= true;
438 strlcpy(bpc
->bpc_vrfname
, vrf
->name
, sizeof(bpc
->bpc_vrfname
));
440 zlog_err("ptm-read: vrf id %u could not be identified",
445 bpc
->bpc_has_vrfname
= true;
446 strlcpy(bpc
->bpc_vrfname
, VRF_DEFAULT_NAME
, sizeof(bpc
->bpc_vrfname
));
449 /* Read control plane independant configuration. */
450 STREAM_GETC(msg
, bpc
->bpc_cbit
);
452 /* Handle profile names. */
453 STREAM_GETC(msg
, ifnamelen
);
454 bpc
->bpc_has_profile
= ifnamelen
> 0;
455 if (bpc
->bpc_has_profile
) {
456 STREAM_GET(bpc
->bpc_profile
, msg
, ifnamelen
);
457 bpc
->bpc_profile
[ifnamelen
] = 0;
460 /* Sanity check: peer and local address must match IP types. */
461 if (bpc
->bpc_local
.sa_sin
.sin_family
!= AF_UNSPEC
462 && (bpc
->bpc_local
.sa_sin
.sin_family
463 != bpc
->bpc_peer
.sa_sin
.sin_family
)) {
464 zlog_warn("ptm-read: peer family doesn't match local type");
474 static void bfdd_dest_register(struct stream
*msg
, vrf_id_t vrf_id
)
476 struct ptm_client
*pc
;
477 struct bfd_session
*bs
;
478 struct bfd_peer_cfg bpc
;
480 /* Read the client context and peer data. */
481 if (_ptm_msg_read(msg
, ZEBRA_BFD_DEST_REGISTER
, vrf_id
, &bpc
, &pc
) == -1)
484 debug_printbpc(&bpc
, "ptm-add-dest: register peer");
486 /* Find or start new BFD session. */
487 bs
= bs_peer_find(&bpc
);
489 bs
= ptm_bfd_sess_new(&bpc
);
491 if (bglobal
.debug_zebra
)
493 "ptm-add-dest: failed to create BFD session");
498 * BFD session was already created, we are just updating the
501 * `ptm-bfd` (or `HAVE_BFDD == 0`) is the only implementation
502 * that allow users to set peer specific timers via protocol.
503 * BFD daemon (this code) on the other hand only supports
504 * changing peer configuration manually (through `peer` node)
507 if (bpc
.bpc_has_profile
)
508 bfd_profile_apply(bpc
.bpc_profile
, bs
);
511 /* Create client peer notification register. */
514 ptm_bfd_notify(bs
, bs
->ses_state
);
517 static void bfdd_dest_deregister(struct stream
*msg
, vrf_id_t vrf_id
)
519 struct ptm_client
*pc
;
520 struct ptm_client_notification
*pcn
;
521 struct bfd_session
*bs
;
522 struct bfd_peer_cfg bpc
;
524 /* Read the client context and peer data. */
525 if (_ptm_msg_read(msg
, ZEBRA_BFD_DEST_DEREGISTER
, vrf_id
, &bpc
, &pc
) == -1)
528 debug_printbpc(&bpc
, "ptm-del-dest: deregister peer");
530 /* Find or start new BFD session. */
531 bs
= bs_peer_find(&bpc
);
533 if (bglobal
.debug_zebra
)
534 zlog_debug("ptm-del-dest: failed to find BFD session");
538 /* Unregister client peer notification. */
539 pcn
= pcn_lookup(pc
, bs
);
545 if (bglobal
.debug_zebra
)
546 zlog_debug("ptm-del-dest: failed to find BFD session");
549 * XXX: We either got a double deregistration or the daemon who
550 * created this is no longer around. Lets try to delete it anyway
551 * and the worst case is the refcount will detain us.
553 _ptm_bfd_session_del(bs
, BD_NEIGHBOR_DOWN
);
557 * header: command, VRF
560 static void bfdd_client_register(struct stream
*msg
)
564 /* Find or allocate process context data. */
565 STREAM_GETL(msg
, pid
);
572 zlog_err("ptm-add-client: failed to register client");
576 * header: command, VRF
579 static void bfdd_client_deregister(struct stream
*msg
)
581 struct ptm_client
*pc
;
584 /* Find or allocate process context data. */
585 STREAM_GETL(msg
, pid
);
589 if (bglobal
.debug_zebra
)
590 zlog_debug("ptm-del-client: failed to find client: %u",
595 if (bglobal
.debug_zebra
)
596 zlog_debug("ptm-del-client: client pid %u", pid
);
603 zlog_err("ptm-del-client: failed to deregister client");
606 static int bfdd_replay(ZAPI_CALLBACK_ARGS
)
608 struct stream
*msg
= zclient
->ibuf
;
611 STREAM_GETL(msg
, rcmd
);
614 case ZEBRA_BFD_DEST_REGISTER
:
615 case ZEBRA_BFD_DEST_UPDATE
:
616 bfdd_dest_register(msg
, vrf_id
);
618 case ZEBRA_BFD_DEST_DEREGISTER
:
619 bfdd_dest_deregister(msg
, vrf_id
);
621 case ZEBRA_BFD_CLIENT_REGISTER
:
622 bfdd_client_register(msg
);
624 case ZEBRA_BFD_CLIENT_DEREGISTER
:
625 bfdd_client_deregister(msg
);
629 if (bglobal
.debug_zebra
)
630 zlog_debug("ptm-replay: invalid message type %u", rcmd
);
637 zlog_err("ptm-replay: failed to find command");
641 static void bfdd_zebra_connected(struct zclient
*zc
)
643 struct stream
*msg
= zc
->obuf
;
645 /* Clean-up and free ptm clients data memory. */
649 * The replay is an empty message just to trigger client daemons
650 * configuration replay.
653 zclient_create_header(msg
, ZEBRA_BFD_DEST_REPLAY
, VRF_DEFAULT
);
654 stream_putl(msg
, ZEBRA_BFD_DEST_REPLAY
);
655 stream_putw_at(msg
, 0, stream_get_endp(msg
));
657 /* Ask for interfaces information. */
658 zclient_create_header(msg
, ZEBRA_INTERFACE_ADD
, VRF_DEFAULT
);
661 zclient_send_message(zclient
);
664 static void bfdd_sessions_enable_interface(struct interface
*ifp
)
666 struct bfd_session_observer
*bso
;
667 struct bfd_session
*bs
;
672 TAILQ_FOREACH(bso
, &bglobal
.bg_obslist
, bso_entry
) {
675 if (bs
->key
.vrfname
[0] &&
676 strcmp(vrf
->name
, bs
->key
.vrfname
))
679 /* If Interface matches vrfname, then bypass iface check */
680 if (vrf_is_backend_netns() || strcmp(ifp
->name
, vrf
->name
)) {
681 /* Interface name mismatch. */
682 if (bs
->key
.ifname
[0] &&
683 strcmp(ifp
->name
, bs
->key
.ifname
))
687 /* Skip enabled sessions. */
691 /* Try to enable it. */
692 bfd_session_enable(bs
);
696 static void bfdd_sessions_disable_interface(struct interface
*ifp
)
698 struct bfd_session_observer
*bso
;
699 struct bfd_session
*bs
;
701 TAILQ_FOREACH(bso
, &bglobal
.bg_obslist
, bso_entry
) {
707 /* Skip disabled sessions. */
708 if (bs
->sock
== -1) {
713 bfd_session_disable(bs
);
718 void bfdd_sessions_enable_vrf(struct vrf
*vrf
)
720 struct bfd_session_observer
*bso
;
721 struct bfd_session
*bs
;
723 /* it may affect configs without interfaces */
724 TAILQ_FOREACH(bso
, &bglobal
.bg_obslist
, bso_entry
) {
728 if (bs
->key
.vrfname
[0] &&
729 strcmp(vrf
->name
, bs
->key
.vrfname
))
731 /* need to update the vrf information on
732 * bs so that callbacks are handled
735 /* Skip enabled sessions. */
738 /* Try to enable it. */
739 bfd_session_enable(bs
);
743 void bfdd_sessions_disable_vrf(struct vrf
*vrf
)
745 struct bfd_session_observer
*bso
;
746 struct bfd_session
*bs
;
748 TAILQ_FOREACH(bso
, &bglobal
.bg_obslist
, bso_entry
) {
750 if (bs
->key
.vrfname
[0] &&
751 strcmp(vrf
->name
, bs
->key
.vrfname
))
753 /* Skip disabled sessions. */
757 bfd_session_disable(bs
);
762 static int bfd_ifp_destroy(struct interface
*ifp
)
764 if (bglobal
.debug_zebra
)
765 zlog_debug("zclient: delete interface %s (VRF %s(%u))",
766 ifp
->name
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
);
768 bfdd_sessions_disable_interface(ifp
);
773 static int bfdd_interface_vrf_update(ZAPI_CALLBACK_ARGS
)
775 struct interface
*ifp
;
778 ifp
= zebra_interface_vrf_update_read(zclient
->ibuf
, vrf_id
, &nvrfid
);
782 if_update_to_new_vrf(ifp
, nvrfid
);
787 static void bfdd_sessions_enable_address(struct connected
*ifc
)
789 struct bfd_session_observer
*bso
;
790 struct bfd_session
*bs
;
791 struct prefix prefix
;
793 TAILQ_FOREACH(bso
, &bglobal
.bg_obslist
, bso_entry
) {
794 /* Skip enabled sessions. */
800 prefix
= bso
->bso_addr
;
801 prefix
.prefixlen
= ifc
->address
->prefixlen
;
802 if (prefix_cmp(&prefix
, ifc
->address
))
805 /* Try to enable it. */
806 bfd_session_enable(bs
);
810 static int bfdd_interface_address_update(ZAPI_CALLBACK_ARGS
)
812 struct connected
*ifc
;
814 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
818 if (bglobal
.debug_zebra
)
819 zlog_debug("zclient: %s local address %pFX (VRF %u)",
820 cmd
== ZEBRA_INTERFACE_ADDRESS_ADD
? "add"
822 ifc
->address
, vrf_id
);
824 if (cmd
== ZEBRA_INTERFACE_ADDRESS_ADD
)
825 bfdd_sessions_enable_address(ifc
);
827 connected_free(&ifc
);
832 static int bfd_ifp_create(struct interface
*ifp
)
834 if (bglobal
.debug_zebra
)
835 zlog_debug("zclient: add interface %s (VRF %s(%u))", ifp
->name
,
836 ifp
->vrf
->name
, ifp
->vrf
->vrf_id
);
837 bfdd_sessions_enable_interface(ifp
);
842 static zclient_handler
*const bfd_handlers
[] = {
844 * We'll receive all messages through replay, however it will
845 * contain a special field with the real command inside so we
846 * avoid having to create too many handlers.
848 [ZEBRA_BFD_DEST_REPLAY
] = bfdd_replay
,
850 /* Learn about interface VRF. */
851 [ZEBRA_INTERFACE_VRF_UPDATE
] = bfdd_interface_vrf_update
,
853 /* Learn about new addresses being registered. */
854 [ZEBRA_INTERFACE_ADDRESS_ADD
] = bfdd_interface_address_update
,
855 [ZEBRA_INTERFACE_ADDRESS_DELETE
] = bfdd_interface_address_update
,
858 void bfdd_zclient_init(struct zebra_privs_t
*bfdd_priv
)
860 if_zapi_callbacks(bfd_ifp_create
, NULL
, NULL
, bfd_ifp_destroy
);
861 zclient
= zclient_new(master
, &zclient_options_default
, bfd_handlers
,
862 array_size(bfd_handlers
));
863 assert(zclient
!= NULL
);
864 zclient_init(zclient
, ZEBRA_ROUTE_BFD
, 0, bfdd_priv
);
866 /* Send replay request on zebra connect. */
867 zclient
->zebra_connected
= bfdd_zebra_connected
;
870 void bfdd_zclient_register(vrf_id_t vrf_id
)
872 if (!zclient
|| zclient
->sock
< 0)
874 zclient_send_reg_requests(zclient
, vrf_id
);
877 void bfdd_zclient_unregister(vrf_id_t vrf_id
)
879 if (!zclient
|| zclient
->sock
< 0)
881 zclient_send_dereg_requests(zclient
, vrf_id
);
884 void bfdd_zclient_stop(void)
886 zclient_stop(zclient
);
888 /* Clean-up and free ptm clients data memory. */
896 static struct ptm_client
*pc_lookup(uint32_t pid
)
898 struct ptm_client
*pc
;
900 TAILQ_FOREACH (pc
, &pcqueue
, pc_entry
) {
901 if (pc
->pc_pid
!= pid
)
910 static struct ptm_client
*pc_new(uint32_t pid
)
912 struct ptm_client
*pc
;
914 /* Look up first, if not found create the client. */
919 /* Allocate the client data and save it. */
920 pc
= XCALLOC(MTYPE_BFDD_CONTROL
, sizeof(*pc
));
923 TAILQ_INSERT_HEAD(&pcqueue
, pc
, pc_entry
);
927 static void pc_free(struct ptm_client
*pc
)
929 struct ptm_client_notification
*pcn
;
931 TAILQ_REMOVE(&pcqueue
, pc
, pc_entry
);
933 while (!TAILQ_EMPTY(&pc
->pc_pcnqueue
)) {
934 pcn
= TAILQ_FIRST(&pc
->pc_pcnqueue
);
938 XFREE(MTYPE_BFDD_CONTROL
, pc
);
941 static void pc_free_all(void)
943 struct ptm_client
*pc
;
945 while (!TAILQ_EMPTY(&pcqueue
)) {
946 pc
= TAILQ_FIRST(&pcqueue
);
951 static struct ptm_client_notification
*pcn_new(struct ptm_client
*pc
,
952 struct bfd_session
*bs
)
954 struct ptm_client_notification
*pcn
;
956 /* Try to find an existing pcn fist. */
957 pcn
= pcn_lookup(pc
, bs
);
961 /* Save the client notification data. */
962 pcn
= XCALLOC(MTYPE_BFDD_NOTIFICATION
, sizeof(*pcn
));
964 TAILQ_INSERT_HEAD(&pc
->pc_pcnqueue
, pcn
, pcn_entry
);
972 static struct ptm_client_notification
*pcn_lookup(struct ptm_client
*pc
,
973 struct bfd_session
*bs
)
975 struct ptm_client_notification
*pcn
;
977 TAILQ_FOREACH (pcn
, &pc
->pc_pcnqueue
, pcn_entry
) {
978 if (pcn
->pcn_bs
!= bs
)
987 static void pcn_free(struct ptm_client_notification
*pcn
)
989 struct ptm_client
*pc
;
990 struct bfd_session
*bs
;
992 /* Handle session de-registration. */
997 /* Log modification to users. */
998 if (bglobal
.debug_zebra
)
999 zlog_debug("ptm-del-session: [%s] refcount=%" PRIu64
,
1000 bs_to_string(bs
), bs
->refcount
);
1002 /* Set session down. */
1003 _ptm_bfd_session_del(bs
, BD_NEIGHBOR_DOWN
);
1005 /* Handle ptm_client deregistration. */
1008 TAILQ_REMOVE(&pc
->pc_pcnqueue
, pcn
, pcn_entry
);
1010 XFREE(MTYPE_BFDD_NOTIFICATION
, pcn
);