3 * Copyright (C) Cumulus Networks, Inc.
6 * This file is part of FRR.
8 * FRR is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
13 * FRR is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include "nexthop_group.h"
33 #include "link_state.h"
35 #include "sharp_globals.h"
36 #include "sharp_nht.h"
37 #include "sharp_zebra.h"
39 /* Zebra structure to hold current status. */
40 struct zclient
*zclient
= NULL
;
42 /* For registering threads. */
43 extern struct thread_master
*master
;
46 extern struct zebra_privs_t sharp_privs
;
48 DEFINE_MTYPE_STATIC(SHARPD
, ZC
, "Test zclients");
50 /* Struct to hold list of test zclients */
51 struct sharp_zclient
{
52 struct sharp_zclient
*prev
;
53 struct sharp_zclient
*next
;
54 struct zclient
*client
;
57 /* Head of test zclient list */
58 static struct sharp_zclient
*sharp_clients_head
;
60 static int sharp_opaque_handler(ZAPI_CALLBACK_ARGS
);
62 /* Utility to add a test zclient struct to the list */
63 static void add_zclient(struct zclient
*client
)
65 struct sharp_zclient
*node
;
67 node
= XCALLOC(MTYPE_ZC
, sizeof(struct sharp_zclient
));
69 node
->client
= client
;
71 node
->next
= sharp_clients_head
;
72 if (sharp_clients_head
)
73 sharp_clients_head
->prev
= node
;
74 sharp_clients_head
= node
;
77 /* Interface addition message from zebra. */
78 static int sharp_ifp_create(struct interface
*ifp
)
83 static int sharp_ifp_destroy(struct interface
*ifp
)
88 static int interface_address_add(ZAPI_CALLBACK_ARGS
)
90 zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
95 static int interface_address_delete(ZAPI_CALLBACK_ARGS
)
99 c
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
108 static int sharp_ifp_up(struct interface
*ifp
)
113 static int sharp_ifp_down(struct interface
*ifp
)
118 int sharp_install_lsps_helper(bool install_p
, bool update_p
,
119 const struct prefix
*p
, uint8_t type
,
120 int instance
, uint32_t in_label
,
121 const struct nexthop_group
*nhg
,
122 const struct nexthop_group
*backup_nhg
)
124 struct zapi_labels zl
= {};
125 struct zapi_nexthop
*znh
;
126 const struct nexthop
*nh
;
129 zl
.type
= ZEBRA_LSP_SHARP
;
130 zl
.local_label
= in_label
;
133 SET_FLAG(zl
.message
, ZAPI_LABELS_FTN
);
134 prefix_copy(&zl
.route
.prefix
, p
);
135 zl
.route
.type
= type
;
136 zl
.route
.instance
= instance
;
139 /* List of nexthops is optional for delete */
142 for (ALL_NEXTHOPS_PTR(nhg
, nh
)) {
143 znh
= &zl
.nexthops
[i
];
145 /* Must have labels to be useful */
146 if (nh
->nh_label
== NULL
||
147 nh
->nh_label
->num_labels
== 0)
150 if (nh
->type
== NEXTHOP_TYPE_IFINDEX
||
151 nh
->type
== NEXTHOP_TYPE_BLACKHOLE
)
152 /* Hmm - can't really deal with these types */
155 ret
= zapi_nexthop_from_nexthop(znh
, nh
);
160 if (i
>= MULTIPATH_NUM
)
165 /* Whoops - no nexthops isn't very useful for install */
166 if (i
== 0 && install_p
)
171 /* Add optional backup nexthop info. Since these are used by index,
172 * we can't just skip over an invalid backup nexthop: we will
173 * invalidate the entire operation.
175 if (backup_nhg
!= NULL
) {
177 for (ALL_NEXTHOPS_PTR(backup_nhg
, nh
)) {
178 znh
= &zl
.backup_nexthops
[i
];
180 /* Must have labels to be useful */
181 if (nh
->nh_label
== NULL
||
182 nh
->nh_label
->num_labels
== 0)
185 if (nh
->type
== NEXTHOP_TYPE_IFINDEX
||
186 nh
->type
== NEXTHOP_TYPE_BLACKHOLE
)
187 /* Hmm - can't really deal with these types */
190 ret
= zapi_nexthop_from_nexthop(znh
, nh
);
195 if (i
>= MULTIPATH_NUM
)
200 SET_FLAG(zl
.message
, ZAPI_LABELS_HAS_BACKUPS
);
202 zl
.backup_nexthop_num
= i
;
208 cmd
= ZEBRA_MPLS_LABELS_REPLACE
;
210 cmd
= ZEBRA_MPLS_LABELS_ADD
;
212 cmd
= ZEBRA_MPLS_LABELS_DELETE
;
215 if (zebra_send_mpls_labels(zclient
, cmd
, &zl
) == ZCLIENT_SEND_FAILURE
)
221 enum where_to_restart
{
222 SHARP_INSTALL_ROUTES_RESTART
,
223 SHARP_DELETE_ROUTES_RESTART
,
226 struct buffer_delay
{
234 const struct nexthop_group
*nhg
;
235 const struct nexthop_group
*backup_nhg
;
236 enum where_to_restart restart
;
241 * route_add - Encodes a route to zebra
243 * This function returns true when the route was buffered
244 * by the underlying stream system
246 static bool route_add(const struct prefix
*p
, vrf_id_t vrf_id
, uint8_t instance
,
247 uint32_t nhgid
, const struct nexthop_group
*nhg
,
248 const struct nexthop_group
*backup_nhg
, uint32_t flags
,
251 struct zapi_route api
;
252 struct zapi_nexthop
*api_nh
;
256 memset(&api
, 0, sizeof(api
));
258 api
.type
= ZEBRA_ROUTE_SHARP
;
259 api
.instance
= instance
;
260 api
.safi
= SAFI_UNICAST
;
261 memcpy(&api
.prefix
, p
, sizeof(*p
));
264 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
265 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
267 /* Only send via ID if nhgroup has been successfully installed */
268 if (nhgid
&& sharp_nhgroup_id_is_installed(nhgid
)) {
269 SET_FLAG(api
.message
, ZAPI_MESSAGE_NHG
);
272 for (ALL_NEXTHOPS_PTR(nhg
, nh
)) {
273 api_nh
= &api
.nexthops
[i
];
275 zapi_nexthop_from_nexthop(api_nh
, nh
);
282 /* Include backup nexthops, if present */
283 if (backup_nhg
&& backup_nhg
->nexthop
) {
284 SET_FLAG(api
.message
, ZAPI_MESSAGE_BACKUP_NEXTHOPS
);
287 for (ALL_NEXTHOPS_PTR(backup_nhg
, nh
)) {
288 api_nh
= &api
.backup_nexthops
[i
];
290 zapi_backup_nexthop_from_nexthop(api_nh
, nh
);
295 api
.backup_nexthop_num
= i
;
298 if (strlen(opaque
)) {
299 SET_FLAG(api
.message
, ZAPI_MESSAGE_OPAQUE
);
300 api
.opaque
.length
= strlen(opaque
) + 1;
301 assert(api
.opaque
.length
<= ZAPI_MESSAGE_OPAQUE_LENGTH
);
302 memcpy(api
.opaque
.data
, opaque
, api
.opaque
.length
);
305 if (zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
)
306 == ZCLIENT_SEND_BUFFERED
)
313 * route_delete - Encodes a route for deletion to zebra
315 * This function returns true when the route sent was
316 * buffered by the underlying stream system.
318 static bool route_delete(struct prefix
*p
, vrf_id_t vrf_id
, uint8_t instance
)
320 struct zapi_route api
;
322 memset(&api
, 0, sizeof(api
));
324 api
.type
= ZEBRA_ROUTE_SHARP
;
325 api
.safi
= SAFI_UNICAST
;
326 api
.instance
= instance
;
327 memcpy(&api
.prefix
, p
, sizeof(*p
));
329 if (zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
)
330 == ZCLIENT_SEND_BUFFERED
)
336 static void sharp_install_routes_restart(struct prefix
*p
, uint32_t count
,
337 vrf_id_t vrf_id
, uint8_t instance
,
339 const struct nexthop_group
*nhg
,
340 const struct nexthop_group
*backup_nhg
,
341 uint32_t routes
, uint32_t flags
,
347 if (p
->family
== AF_INET
) {
349 temp
= ntohl(p
->u
.prefix4
.s_addr
);
351 temp
= ntohl(p
->u
.val32
[3]);
353 for (i
= count
; i
< routes
; i
++) {
354 bool buffered
= route_add(p
, vrf_id
, (uint8_t)instance
, nhgid
,
355 nhg
, backup_nhg
, flags
, opaque
);
357 p
->u
.prefix4
.s_addr
= htonl(++temp
);
359 p
->u
.val32
[3] = htonl(++temp
);
366 wb
.instance
= instance
;
370 wb
.backup_nhg
= backup_nhg
;
372 wb
.restart
= SHARP_INSTALL_ROUTES_RESTART
;
379 void sharp_install_routes_helper(struct prefix
*p
, vrf_id_t vrf_id
,
380 uint8_t instance
, uint32_t nhgid
,
381 const struct nexthop_group
*nhg
,
382 const struct nexthop_group
*backup_nhg
,
383 uint32_t routes
, uint32_t flags
, char *opaque
)
385 zlog_debug("Inserting %u routes", routes
);
387 /* Only use backup route/nexthops if present */
388 if (backup_nhg
&& (backup_nhg
->nexthop
== NULL
))
391 monotime(&sg
.r
.t_start
);
392 sharp_install_routes_restart(p
, 0, vrf_id
, instance
, nhgid
, nhg
,
393 backup_nhg
, routes
, flags
, opaque
);
396 static void sharp_remove_routes_restart(struct prefix
*p
, uint32_t count
,
397 vrf_id_t vrf_id
, uint8_t instance
,
403 if (p
->family
== AF_INET
) {
405 temp
= ntohl(p
->u
.prefix4
.s_addr
);
407 temp
= ntohl(p
->u
.val32
[3]);
409 for (i
= count
; i
< routes
; i
++) {
410 bool buffered
= route_delete(p
, vrf_id
, (uint8_t)instance
);
413 p
->u
.prefix4
.s_addr
= htonl(++temp
);
415 p
->u
.val32
[3] = htonl(++temp
);
421 wb
.instance
= instance
;
423 wb
.restart
= SHARP_DELETE_ROUTES_RESTART
;
430 void sharp_remove_routes_helper(struct prefix
*p
, vrf_id_t vrf_id
,
431 uint8_t instance
, uint32_t routes
)
433 zlog_debug("Removing %u routes", routes
);
435 monotime(&sg
.r
.t_start
);
437 sharp_remove_routes_restart(p
, 0, vrf_id
, instance
, routes
);
440 static void handle_repeated(bool installed
)
442 struct prefix p
= sg
.r
.orig_prefix
;
445 if (sg
.r
.repeat
<= 0)
449 sg
.r
.removed_routes
= 0;
450 sharp_remove_routes_helper(&p
, sg
.r
.vrf_id
,
451 sg
.r
.inst
, sg
.r
.total_routes
);
455 sg
.r
.installed_routes
= 0;
456 sharp_install_routes_helper(&p
, sg
.r
.vrf_id
, sg
.r
.inst
,
457 sg
.r
.nhgid
, &sg
.r
.nhop_group
,
458 &sg
.r
.backup_nhop_group
,
459 sg
.r
.total_routes
, sg
.r
.flags
,
464 static void sharp_zclient_buffer_ready(void)
466 switch (wb
.restart
) {
467 case SHARP_INSTALL_ROUTES_RESTART
:
468 sharp_install_routes_restart(
469 &wb
.p
, wb
.count
, wb
.vrf_id
, wb
.instance
, wb
.nhgid
,
470 wb
.nhg
, wb
.backup_nhg
, wb
.routes
, wb
.flags
,
473 case SHARP_DELETE_ROUTES_RESTART
:
474 sharp_remove_routes_restart(&wb
.p
, wb
.count
, wb
.vrf_id
,
475 wb
.instance
, wb
.routes
);
480 static int route_notify_owner(ZAPI_CALLBACK_ARGS
)
484 enum zapi_route_notify_owner note
;
487 if (!zapi_route_notify_decode(zclient
->ibuf
, &p
, &table
, ¬e
,
492 case ZAPI_ROUTE_INSTALLED
:
493 sg
.r
.installed_routes
++;
494 if (sg
.r
.total_routes
== sg
.r
.installed_routes
) {
495 monotime(&sg
.r
.t_end
);
496 timersub(&sg
.r
.t_end
, &sg
.r
.t_start
, &r
);
497 zlog_debug("Installed All Items %jd.%ld",
498 (intmax_t)r
.tv_sec
, (long)r
.tv_usec
);
499 handle_repeated(true);
502 case ZAPI_ROUTE_FAIL_INSTALL
:
503 zlog_debug("Failed install of route");
505 case ZAPI_ROUTE_BETTER_ADMIN_WON
:
506 zlog_debug("Better Admin Distance won over us");
508 case ZAPI_ROUTE_REMOVED
:
509 sg
.r
.removed_routes
++;
510 if (sg
.r
.total_routes
== sg
.r
.removed_routes
) {
511 monotime(&sg
.r
.t_end
);
512 timersub(&sg
.r
.t_end
, &sg
.r
.t_start
, &r
);
513 zlog_debug("Removed all Items %jd.%ld",
514 (intmax_t)r
.tv_sec
, (long)r
.tv_usec
);
515 handle_repeated(false);
518 case ZAPI_ROUTE_REMOVE_FAIL
:
519 zlog_debug("Route removal Failure");
525 static void zebra_connected(struct zclient
*zclient
)
527 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
530 * Do not actually turn this on yet
531 * This is just the start of the infrastructure needed here
532 * This can be fixed at a later time.
534 * zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP,
535 * ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT);
539 void vrf_label_add(vrf_id_t vrf_id
, afi_t afi
, mpls_label_t label
)
541 zclient_send_vrf_label(zclient
, vrf_id
, afi
, label
, ZEBRA_LSP_SHARP
);
544 void nhg_add(uint32_t id
, const struct nexthop_group
*nhg
,
545 const struct nexthop_group
*backup_nhg
)
547 struct zapi_nhg api_nhg
= {};
548 struct zapi_nexthop
*api_nh
;
550 bool is_valid
= true;
554 api_nhg
.resilience
= nhg
->nhgr
;
556 for (ALL_NEXTHOPS_PTR(nhg
, nh
)) {
557 if (api_nhg
.nexthop_num
>= MULTIPATH_NUM
) {
559 "%s: number of nexthops greater than max multipath size, truncating",
564 /* Unresolved nexthops will lead to failure - only send
565 * nexthops that zebra will consider valid.
567 if (nh
->ifindex
== 0)
570 api_nh
= &api_nhg
.nexthops
[api_nhg
.nexthop_num
];
572 zapi_nexthop_from_nexthop(api_nh
, nh
);
573 api_nhg
.nexthop_num
++;
576 if (api_nhg
.nexthop_num
== 0) {
577 zlog_debug("%s: nhg %u not sent: no valid nexthops",
584 for (ALL_NEXTHOPS_PTR(backup_nhg
, nh
)) {
585 if (api_nhg
.backup_nexthop_num
>= MULTIPATH_NUM
) {
587 "%s: number of backup nexthops greater than max multipath size, truncating",
592 /* Unresolved nexthop: will be rejected by zebra.
593 * That causes a problem, since the primary nexthops
594 * rely on array indexing into the backup nexthops. If
595 * that array isn't valid, the backup indexes won't be
598 if (nh
->ifindex
== 0) {
599 zlog_debug("%s: nhg %u: invalid backup nexthop",
605 api_nh
= &api_nhg
.backup_nexthops
606 [api_nhg
.backup_nexthop_num
];
608 zapi_backup_nexthop_from_nexthop(api_nh
, nh
);
609 api_nhg
.backup_nexthop_num
++;
615 zclient_nhg_send(zclient
, ZEBRA_NHG_ADD
, &api_nhg
);
618 void nhg_del(uint32_t id
)
620 struct zapi_nhg api_nhg
= {};
624 zclient_nhg_send(zclient
, ZEBRA_NHG_DEL
, &api_nhg
);
627 void sharp_zebra_nexthop_watch(struct prefix
*p
, vrf_id_t vrf_id
, bool import
,
628 bool watch
, bool connected
)
632 command
= ZEBRA_NEXTHOP_REGISTER
;
635 command
= ZEBRA_NEXTHOP_UNREGISTER
;
637 if (zclient_send_rnh(zclient
, command
, p
, SAFI_UNICAST
, connected
,
639 == ZCLIENT_SEND_FAILURE
)
640 zlog_warn("%s: Failure to send nexthop to zebra", __func__
);
643 static int sharp_debug_nexthops(struct zapi_route
*api
)
647 if (api
->nexthop_num
== 0) {
653 for (i
= 0; i
< api
->nexthop_num
; i
++) {
654 struct zapi_nexthop
*znh
= &api
->nexthops
[i
];
657 case NEXTHOP_TYPE_IPV4_IFINDEX
:
658 case NEXTHOP_TYPE_IPV4
:
660 " Nexthop %pI4, type: %d, ifindex: %d, vrf: %d, label_num: %d",
661 &znh
->gate
.ipv4
.s_addr
, znh
->type
, znh
->ifindex
,
662 znh
->vrf_id
, znh
->label_num
);
664 case NEXTHOP_TYPE_IPV6_IFINDEX
:
665 case NEXTHOP_TYPE_IPV6
:
667 " Nexthop %pI6, type: %d, ifindex: %d, vrf: %d, label_num: %d",
668 &znh
->gate
.ipv6
, znh
->type
, znh
->ifindex
,
669 znh
->vrf_id
, znh
->label_num
);
671 case NEXTHOP_TYPE_IFINDEX
:
672 zlog_debug(" Nexthop IFINDEX: %d, ifindex: %d",
673 znh
->type
, znh
->ifindex
);
675 case NEXTHOP_TYPE_BLACKHOLE
:
676 zlog_debug(" Nexthop blackhole");
683 static int sharp_nexthop_update(ZAPI_CALLBACK_ARGS
)
685 struct sharp_nh_tracker
*nht
;
686 struct zapi_route nhr
;
687 struct prefix matched
;
689 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &matched
, &nhr
)) {
690 zlog_err("%s: Decode of update failed", __func__
);
694 zlog_debug("Received update for %pFX actual match: %pFX metric: %u",
695 &matched
, &nhr
.prefix
, nhr
.metric
);
697 nht
= sharp_nh_tracker_get(&matched
);
698 nht
->nhop_num
= nhr
.nexthop_num
;
701 sharp_debug_nexthops(&nhr
);
706 static int sharp_redistribute_route(ZAPI_CALLBACK_ARGS
)
708 struct zapi_route api
;
710 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
711 zlog_warn("%s: Decode of redistribute failed: %d", __func__
,
712 ZEBRA_REDISTRIBUTE_ROUTE_ADD
);
714 zlog_debug("%s: %pFX (%s)", zserv_command_string(cmd
),
715 &api
.prefix
, zebra_route_string(api
.type
));
717 sharp_debug_nexthops(&api
);
722 void sharp_redistribute_vrf(struct vrf
*vrf
, int type
)
724 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
, type
,
728 static zclient_handler
*const sharp_opaque_handlers
[] = {
729 [ZEBRA_OPAQUE_MESSAGE
] = sharp_opaque_handler
,
732 /* Add a zclient with a specified session id, for testing. */
733 int sharp_zclient_create(uint32_t session_id
)
735 struct zclient
*client
;
736 struct sharp_zclient
*node
;
738 /* Check for duplicates */
739 for (node
= sharp_clients_head
; node
!= NULL
; node
= node
->next
) {
740 if (node
->client
->session_id
== session_id
)
744 client
= zclient_new(master
, &zclient_options_default
,
745 sharp_opaque_handlers
,
746 array_size(sharp_opaque_handlers
));
748 client
->session_id
= session_id
;
750 zclient_init(client
, ZEBRA_ROUTE_SHARP
, 0, &sharp_privs
);
752 /* Enqueue on the list of test clients */
758 /* Delete one of the extra test zclients */
759 int sharp_zclient_delete(uint32_t session_id
)
761 struct sharp_zclient
*node
;
763 /* Search for session */
764 for (node
= sharp_clients_head
; node
!= NULL
; node
= node
->next
) {
765 if (node
->client
->session_id
== session_id
) {
766 /* Dequeue from list */
768 node
->next
->prev
= node
->prev
;
770 node
->prev
->next
= node
->next
;
771 if (node
== sharp_clients_head
)
772 sharp_clients_head
= node
->next
;
774 /* Clean up zclient */
775 zclient_stop(node
->client
);
776 zclient_free(node
->client
);
779 XFREE(MTYPE_ZC
, node
);
787 static const char *const type2txt
[] = { "Generic", "Vertex", "Edge", "Subnet" };
788 static const char *const status2txt
[] = { "Unknown", "New", "Update",
789 "Delete", "Sync", "Orphan"};
790 /* Handler for opaque messages */
791 static int sharp_opaque_handler(ZAPI_CALLBACK_ARGS
)
794 struct zapi_opaque_msg info
;
795 struct ls_element
*lse
;
799 if (zclient_opaque_decode(s
, &info
) != 0)
802 zlog_debug("%s: [%u] received opaque type %u", __func__
,
803 zclient
->session_id
, info
.type
);
805 if (info
.type
== LINK_STATE_UPDATE
) {
806 lse
= ls_stream2ted(sg
.ted
, s
, false);
808 zlog_debug(" |- Got %s %s from Link State Database",
809 status2txt
[lse
->status
],
810 type2txt
[lse
->type
]);
815 "%s: Error to convert Stream into Link State",
823 * Send OPAQUE messages, using subtype 'type'.
825 void sharp_opaque_send(uint32_t type
, uint32_t proto
, uint32_t instance
,
826 uint32_t session_id
, uint32_t count
)
832 /* Prepare a small payload */
833 for (i
= 0; i
< sizeof(buf
); i
++) {
840 /* Send some messages - broadcast and unicast are supported */
841 for (i
= 0; i
< count
; i
++) {
843 ret
= zclient_send_opaque(zclient
, type
, buf
,
846 ret
= zclient_send_opaque_unicast(zclient
, type
, proto
,
847 instance
, session_id
,
849 if (ret
== ZCLIENT_SEND_FAILURE
) {
850 zlog_debug("%s: send_opaque() failed => %d",
859 * Send OPAQUE registration messages, using subtype 'type'.
861 void sharp_opaque_reg_send(bool is_reg
, uint32_t proto
, uint32_t instance
,
862 uint32_t session_id
, uint32_t type
)
870 zclient_create_header(s
, ZEBRA_OPAQUE_REGISTER
, VRF_DEFAULT
);
872 zclient_create_header(s
, ZEBRA_OPAQUE_UNREGISTER
, VRF_DEFAULT
);
875 stream_putl(s
, type
);
877 /* Add zclient info */
878 stream_putc(s
, proto
);
879 stream_putw(s
, instance
);
880 stream_putl(s
, session_id
);
882 /* Put length at the first point of the stream. */
883 stream_putw_at(s
, 0, stream_get_endp(s
));
885 (void)zclient_send_message(zclient
);
889 /* Link State registration */
890 void sharp_zebra_register_te(void)
892 /* First register to received Link State Update messages */
893 zclient_register_opaque(zclient
, LINK_STATE_UPDATE
);
895 /* Then, request initial TED with SYNC message */
896 ls_request_sync(zclient
);
899 void sharp_zebra_send_arp(const struct interface
*ifp
, const struct prefix
*p
)
901 zclient_send_neigh_discovery_req(zclient
, ifp
, p
);
904 static int nhg_notify_owner(ZAPI_CALLBACK_ARGS
)
906 enum zapi_nhg_notify_owner note
;
909 if (!zapi_nhg_notify_decode(zclient
->ibuf
, &id
, ¬e
))
913 case ZAPI_NHG_INSTALLED
:
914 sharp_nhgroup_id_set_installed(id
, true);
915 zlog_debug("Installed nhg %u", id
);
917 case ZAPI_NHG_FAIL_INSTALL
:
918 zlog_debug("Failed install of nhg %u", id
);
920 case ZAPI_NHG_REMOVED
:
921 zlog_debug("Removed nhg %u", id
);
923 case ZAPI_NHG_REMOVE_FAIL
:
924 zlog_debug("Failed removal of nhg %u", id
);
931 int sharp_zebra_srv6_manager_get_locator_chunk(const char *locator_name
)
933 return srv6_manager_get_locator_chunk(zclient
, locator_name
);
936 int sharp_zebra_srv6_manager_release_locator_chunk(const char *locator_name
)
938 return srv6_manager_release_locator_chunk(zclient
, locator_name
);
941 static int sharp_zebra_process_srv6_locator_chunk(ZAPI_CALLBACK_ARGS
)
943 struct stream
*s
= NULL
;
944 struct srv6_locator_chunk s6c
= {};
945 struct listnode
*node
, *nnode
;
946 struct sharp_srv6_locator
*loc
;
949 zapi_srv6_locator_chunk_decode(s
, &s6c
);
951 for (ALL_LIST_ELEMENTS(sg
.srv6_locators
, node
, nnode
, loc
)) {
952 struct prefix_ipv6
*chunk
= NULL
;
953 struct listnode
*chunk_node
;
954 struct prefix_ipv6
*c
;
956 if (strcmp(loc
->name
, s6c
.locator_name
) != 0) {
957 zlog_err("%s: Locator name unmatch %s:%s", __func__
,
958 loc
->name
, s6c
.locator_name
);
962 for (ALL_LIST_ELEMENTS_RO(loc
->chunks
, chunk_node
, c
))
963 if (!prefix_cmp(c
, &s6c
.prefix
))
966 chunk
= prefix_ipv6_new();
968 listnode_add(loc
->chunks
, chunk
);
972 zlog_err("%s: can't get locator_chunk!!", __func__
);
976 int sharp_zebra_send_interface_protodown(struct interface
*ifp
, bool down
)
978 zlog_debug("Sending zebra to set %s protodown %s", ifp
->name
,
979 down
? "on" : "off");
981 if (zclient_send_interface_protodown(zclient
, ifp
->vrf
->vrf_id
, ifp
,
982 down
) == ZCLIENT_SEND_FAILURE
)
988 static zclient_handler
*const sharp_handlers
[] = {
989 [ZEBRA_INTERFACE_ADDRESS_ADD
] = interface_address_add
,
990 [ZEBRA_INTERFACE_ADDRESS_DELETE
] = interface_address_delete
,
991 [ZEBRA_ROUTE_NOTIFY_OWNER
] = route_notify_owner
,
992 [ZEBRA_NEXTHOP_UPDATE
] = sharp_nexthop_update
,
993 [ZEBRA_NHG_NOTIFY_OWNER
] = nhg_notify_owner
,
994 [ZEBRA_REDISTRIBUTE_ROUTE_ADD
] = sharp_redistribute_route
,
995 [ZEBRA_REDISTRIBUTE_ROUTE_DEL
] = sharp_redistribute_route
,
996 [ZEBRA_OPAQUE_MESSAGE
] = sharp_opaque_handler
,
997 [ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK
] =
998 sharp_zebra_process_srv6_locator_chunk
,
1001 void sharp_zebra_init(void)
1003 struct zclient_options opt
= {.receive_notify
= true};
1005 if_zapi_callbacks(sharp_ifp_create
, sharp_ifp_up
,
1006 sharp_ifp_down
, sharp_ifp_destroy
);
1008 zclient
= zclient_new(master
, &opt
, sharp_handlers
,
1009 array_size(sharp_handlers
));
1011 zclient_init(zclient
, ZEBRA_ROUTE_SHARP
, 0, &sharp_privs
);
1012 zclient
->zebra_connected
= zebra_connected
;
1013 zclient
->zebra_buffer_write_ready
= sharp_zclient_buffer_ready
;