1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) Cumulus Networks, Inc.
17 #include "nexthop_group.h"
18 #include "link_state.h"
21 #include "sharp_globals.h"
22 #include "sharp_nht.h"
23 #include "sharp_zebra.h"
25 /* Zebra structure to hold current status. */
26 struct zclient
*zclient
= NULL
;
28 /* For registering threads. */
29 extern struct thread_master
*master
;
32 extern struct zebra_privs_t sharp_privs
;
34 DEFINE_MTYPE_STATIC(SHARPD
, ZC
, "Test zclients");
36 /* Struct to hold list of test zclients */
37 struct sharp_zclient
{
38 struct sharp_zclient
*prev
;
39 struct sharp_zclient
*next
;
40 struct zclient
*client
;
43 /* Head of test zclient list */
44 static struct sharp_zclient
*sharp_clients_head
;
46 static int sharp_opaque_handler(ZAPI_CALLBACK_ARGS
);
48 /* Utility to add a test zclient struct to the list */
49 static void add_zclient(struct zclient
*client
)
51 struct sharp_zclient
*node
;
53 node
= XCALLOC(MTYPE_ZC
, sizeof(struct sharp_zclient
));
55 node
->client
= client
;
57 node
->next
= sharp_clients_head
;
58 if (sharp_clients_head
)
59 sharp_clients_head
->prev
= node
;
60 sharp_clients_head
= node
;
63 /* Interface addition message from zebra. */
64 static int sharp_ifp_create(struct interface
*ifp
)
69 static int sharp_ifp_destroy(struct interface
*ifp
)
74 static int interface_address_add(ZAPI_CALLBACK_ARGS
)
76 zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
81 static int interface_address_delete(ZAPI_CALLBACK_ARGS
)
85 c
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
94 static int sharp_ifp_up(struct interface
*ifp
)
99 static int sharp_ifp_down(struct interface
*ifp
)
104 int sharp_install_lsps_helper(bool install_p
, bool update_p
,
105 const struct prefix
*p
, uint8_t type
,
106 int instance
, uint32_t in_label
,
107 const struct nexthop_group
*nhg
,
108 const struct nexthop_group
*backup_nhg
)
110 struct zapi_labels zl
= {};
111 struct zapi_nexthop
*znh
;
112 const struct nexthop
*nh
;
115 zl
.type
= ZEBRA_LSP_SHARP
;
116 zl
.local_label
= in_label
;
119 SET_FLAG(zl
.message
, ZAPI_LABELS_FTN
);
120 prefix_copy(&zl
.route
.prefix
, p
);
121 zl
.route
.type
= type
;
122 zl
.route
.instance
= instance
;
125 /* List of nexthops is optional for delete */
128 for (ALL_NEXTHOPS_PTR(nhg
, nh
)) {
129 znh
= &zl
.nexthops
[i
];
131 /* Must have labels to be useful */
132 if (nh
->nh_label
== NULL
||
133 nh
->nh_label
->num_labels
== 0)
136 if (nh
->type
== NEXTHOP_TYPE_IFINDEX
||
137 nh
->type
== NEXTHOP_TYPE_BLACKHOLE
)
138 /* Hmm - can't really deal with these types */
141 ret
= zapi_nexthop_from_nexthop(znh
, nh
);
146 if (i
>= MULTIPATH_NUM
)
151 /* Whoops - no nexthops isn't very useful for install */
152 if (i
== 0 && install_p
)
157 /* Add optional backup nexthop info. Since these are used by index,
158 * we can't just skip over an invalid backup nexthop: we will
159 * invalidate the entire operation.
161 if (backup_nhg
!= NULL
) {
163 for (ALL_NEXTHOPS_PTR(backup_nhg
, nh
)) {
164 znh
= &zl
.backup_nexthops
[i
];
166 /* Must have labels to be useful */
167 if (nh
->nh_label
== NULL
||
168 nh
->nh_label
->num_labels
== 0)
171 if (nh
->type
== NEXTHOP_TYPE_IFINDEX
||
172 nh
->type
== NEXTHOP_TYPE_BLACKHOLE
)
173 /* Hmm - can't really deal with these types */
176 ret
= zapi_nexthop_from_nexthop(znh
, nh
);
181 if (i
>= MULTIPATH_NUM
)
186 SET_FLAG(zl
.message
, ZAPI_LABELS_HAS_BACKUPS
);
188 zl
.backup_nexthop_num
= i
;
194 cmd
= ZEBRA_MPLS_LABELS_REPLACE
;
196 cmd
= ZEBRA_MPLS_LABELS_ADD
;
198 cmd
= ZEBRA_MPLS_LABELS_DELETE
;
201 if (zebra_send_mpls_labels(zclient
, cmd
, &zl
) == ZCLIENT_SEND_FAILURE
)
207 enum where_to_restart
{
208 SHARP_INSTALL_ROUTES_RESTART
,
209 SHARP_DELETE_ROUTES_RESTART
,
212 struct buffer_delay
{
220 const struct nexthop_group
*nhg
;
221 const struct nexthop_group
*backup_nhg
;
222 enum where_to_restart restart
;
227 * route_add - Encodes a route to zebra
229 * This function returns true when the route was buffered
230 * by the underlying stream system
232 static bool route_add(const struct prefix
*p
, vrf_id_t vrf_id
, uint8_t instance
,
233 uint32_t nhgid
, const struct nexthop_group
*nhg
,
234 const struct nexthop_group
*backup_nhg
, uint32_t flags
,
237 struct zapi_route api
;
238 struct zapi_nexthop
*api_nh
;
242 memset(&api
, 0, sizeof(api
));
244 api
.type
= ZEBRA_ROUTE_SHARP
;
245 api
.instance
= instance
;
246 api
.safi
= SAFI_UNICAST
;
247 memcpy(&api
.prefix
, p
, sizeof(*p
));
250 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
251 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
253 /* Only send via ID if nhgroup has been successfully installed */
254 if (nhgid
&& sharp_nhgroup_id_is_installed(nhgid
)) {
255 SET_FLAG(api
.message
, ZAPI_MESSAGE_NHG
);
258 for (ALL_NEXTHOPS_PTR(nhg
, nh
)) {
259 api_nh
= &api
.nexthops
[i
];
261 zapi_nexthop_from_nexthop(api_nh
, nh
);
268 /* Include backup nexthops, if present */
269 if (backup_nhg
&& backup_nhg
->nexthop
) {
270 SET_FLAG(api
.message
, ZAPI_MESSAGE_BACKUP_NEXTHOPS
);
273 for (ALL_NEXTHOPS_PTR(backup_nhg
, nh
)) {
274 api_nh
= &api
.backup_nexthops
[i
];
276 zapi_backup_nexthop_from_nexthop(api_nh
, nh
);
281 api
.backup_nexthop_num
= i
;
284 if (strlen(opaque
)) {
285 SET_FLAG(api
.message
, ZAPI_MESSAGE_OPAQUE
);
286 api
.opaque
.length
= strlen(opaque
) + 1;
287 assert(api
.opaque
.length
<= ZAPI_MESSAGE_OPAQUE_LENGTH
);
288 memcpy(api
.opaque
.data
, opaque
, api
.opaque
.length
);
291 if (zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
) ==
292 ZCLIENT_SEND_BUFFERED
)
299 * route_delete - Encodes a route for deletion to zebra
301 * This function returns true when the route sent was
302 * buffered by the underlying stream system.
304 static bool route_delete(struct prefix
*p
, vrf_id_t vrf_id
, uint8_t instance
)
306 struct zapi_route api
;
308 memset(&api
, 0, sizeof(api
));
310 api
.type
= ZEBRA_ROUTE_SHARP
;
311 api
.safi
= SAFI_UNICAST
;
312 api
.instance
= instance
;
313 memcpy(&api
.prefix
, p
, sizeof(*p
));
315 if (zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
) ==
316 ZCLIENT_SEND_BUFFERED
)
322 static void sharp_install_routes_restart(struct prefix
*p
, uint32_t count
,
323 vrf_id_t vrf_id
, uint8_t instance
,
325 const struct nexthop_group
*nhg
,
326 const struct nexthop_group
*backup_nhg
,
327 uint32_t routes
, uint32_t flags
,
333 if (p
->family
== AF_INET
) {
335 temp
= ntohl(p
->u
.prefix4
.s_addr
);
337 temp
= ntohl(p
->u
.val32
[3]);
339 for (i
= count
; i
< routes
; i
++) {
340 bool buffered
= route_add(p
, vrf_id
, (uint8_t)instance
, nhgid
,
341 nhg
, backup_nhg
, flags
, opaque
);
343 p
->u
.prefix4
.s_addr
= htonl(++temp
);
345 p
->u
.val32
[3] = htonl(++temp
);
352 wb
.instance
= instance
;
356 wb
.backup_nhg
= backup_nhg
;
358 wb
.restart
= SHARP_INSTALL_ROUTES_RESTART
;
365 void sharp_install_routes_helper(struct prefix
*p
, vrf_id_t vrf_id
,
366 uint8_t instance
, uint32_t nhgid
,
367 const struct nexthop_group
*nhg
,
368 const struct nexthop_group
*backup_nhg
,
369 uint32_t routes
, uint32_t flags
, char *opaque
)
371 zlog_debug("Inserting %u routes", routes
);
373 /* Only use backup route/nexthops if present */
374 if (backup_nhg
&& (backup_nhg
->nexthop
== NULL
))
377 monotime(&sg
.r
.t_start
);
378 sharp_install_routes_restart(p
, 0, vrf_id
, instance
, nhgid
, nhg
,
379 backup_nhg
, routes
, flags
, opaque
);
382 static void sharp_remove_routes_restart(struct prefix
*p
, uint32_t count
,
383 vrf_id_t vrf_id
, uint8_t instance
,
389 if (p
->family
== AF_INET
) {
391 temp
= ntohl(p
->u
.prefix4
.s_addr
);
393 temp
= ntohl(p
->u
.val32
[3]);
395 for (i
= count
; i
< routes
; i
++) {
396 bool buffered
= route_delete(p
, vrf_id
, (uint8_t)instance
);
399 p
->u
.prefix4
.s_addr
= htonl(++temp
);
401 p
->u
.val32
[3] = htonl(++temp
);
407 wb
.instance
= instance
;
409 wb
.restart
= SHARP_DELETE_ROUTES_RESTART
;
416 void sharp_remove_routes_helper(struct prefix
*p
, vrf_id_t vrf_id
,
417 uint8_t instance
, uint32_t routes
)
419 zlog_debug("Removing %u routes", routes
);
421 monotime(&sg
.r
.t_start
);
423 sharp_remove_routes_restart(p
, 0, vrf_id
, instance
, routes
);
426 static void handle_repeated(bool installed
)
428 struct prefix p
= sg
.r
.orig_prefix
;
431 if (sg
.r
.repeat
<= 0)
435 sg
.r
.removed_routes
= 0;
436 sharp_remove_routes_helper(&p
, sg
.r
.vrf_id
, sg
.r
.inst
,
441 sg
.r
.installed_routes
= 0;
442 sharp_install_routes_helper(
443 &p
, sg
.r
.vrf_id
, sg
.r
.inst
, sg
.r
.nhgid
,
444 &sg
.r
.nhop_group
, &sg
.r
.backup_nhop_group
,
445 sg
.r
.total_routes
, sg
.r
.flags
, sg
.r
.opaque
);
449 static void sharp_zclient_buffer_ready(void)
451 switch (wb
.restart
) {
452 case SHARP_INSTALL_ROUTES_RESTART
:
453 sharp_install_routes_restart(
454 &wb
.p
, wb
.count
, wb
.vrf_id
, wb
.instance
, wb
.nhgid
,
455 wb
.nhg
, wb
.backup_nhg
, wb
.routes
, wb
.flags
, wb
.opaque
);
457 case SHARP_DELETE_ROUTES_RESTART
:
458 sharp_remove_routes_restart(&wb
.p
, wb
.count
, wb
.vrf_id
,
459 wb
.instance
, wb
.routes
);
464 static int route_notify_owner(ZAPI_CALLBACK_ARGS
)
468 enum zapi_route_notify_owner note
;
471 if (!zapi_route_notify_decode(zclient
->ibuf
, &p
, &table
, ¬e
, NULL
,
476 case ZAPI_ROUTE_INSTALLED
:
477 sg
.r
.installed_routes
++;
478 if (sg
.r
.total_routes
== sg
.r
.installed_routes
) {
479 monotime(&sg
.r
.t_end
);
480 timersub(&sg
.r
.t_end
, &sg
.r
.t_start
, &r
);
481 zlog_debug("Installed All Items %jd.%ld",
482 (intmax_t)r
.tv_sec
, (long)r
.tv_usec
);
483 handle_repeated(true);
486 case ZAPI_ROUTE_FAIL_INSTALL
:
487 zlog_debug("Failed install of route");
489 case ZAPI_ROUTE_BETTER_ADMIN_WON
:
490 zlog_debug("Better Admin Distance won over us");
492 case ZAPI_ROUTE_REMOVED
:
493 sg
.r
.removed_routes
++;
494 if (sg
.r
.total_routes
== sg
.r
.removed_routes
) {
495 monotime(&sg
.r
.t_end
);
496 timersub(&sg
.r
.t_end
, &sg
.r
.t_start
, &r
);
497 zlog_debug("Removed all Items %jd.%ld",
498 (intmax_t)r
.tv_sec
, (long)r
.tv_usec
);
499 handle_repeated(false);
502 case ZAPI_ROUTE_REMOVE_FAIL
:
503 zlog_debug("Route removal Failure");
509 static void zebra_connected(struct zclient
*zclient
)
511 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
514 * Do not actually turn this on yet
515 * This is just the start of the infrastructure needed here
516 * This can be fixed at a later time.
518 * zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP,
519 * ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT);
523 void vrf_label_add(vrf_id_t vrf_id
, afi_t afi
, mpls_label_t label
)
525 zclient_send_vrf_label(zclient
, vrf_id
, afi
, label
, ZEBRA_LSP_SHARP
);
528 void nhg_add(uint32_t id
, const struct nexthop_group
*nhg
,
529 const struct nexthop_group
*backup_nhg
)
531 struct zapi_nhg api_nhg
= {};
532 struct zapi_nexthop
*api_nh
;
534 bool is_valid
= true;
538 api_nhg
.resilience
= nhg
->nhgr
;
540 for (ALL_NEXTHOPS_PTR(nhg
, nh
)) {
541 if (api_nhg
.nexthop_num
>= MULTIPATH_NUM
) {
543 "%s: number of nexthops greater than max multipath size, truncating",
548 /* Unresolved nexthops will lead to failure - only send
549 * nexthops that zebra will consider valid.
551 if (nh
->ifindex
== 0)
554 api_nh
= &api_nhg
.nexthops
[api_nhg
.nexthop_num
];
556 zapi_nexthop_from_nexthop(api_nh
, nh
);
557 api_nhg
.nexthop_num
++;
560 if (api_nhg
.nexthop_num
== 0) {
561 zlog_debug("%s: nhg %u not sent: no valid nexthops", __func__
,
568 for (ALL_NEXTHOPS_PTR(backup_nhg
, nh
)) {
569 if (api_nhg
.backup_nexthop_num
>= MULTIPATH_NUM
) {
571 "%s: number of backup nexthops greater than max multipath size, truncating",
576 /* Unresolved nexthop: will be rejected by zebra.
577 * That causes a problem, since the primary nexthops
578 * rely on array indexing into the backup nexthops. If
579 * that array isn't valid, the backup indexes won't be
582 if (nh
->ifindex
== 0) {
583 zlog_debug("%s: nhg %u: invalid backup nexthop",
589 api_nh
= &api_nhg
.backup_nexthops
590 [api_nhg
.backup_nexthop_num
];
592 zapi_backup_nexthop_from_nexthop(api_nh
, nh
);
593 api_nhg
.backup_nexthop_num
++;
599 zclient_nhg_send(zclient
, ZEBRA_NHG_ADD
, &api_nhg
);
602 void nhg_del(uint32_t id
)
604 struct zapi_nhg api_nhg
= {};
608 zclient_nhg_send(zclient
, ZEBRA_NHG_DEL
, &api_nhg
);
611 void sharp_zebra_nexthop_watch(struct prefix
*p
, vrf_id_t vrf_id
, bool import
,
612 bool watch
, bool connected
)
616 command
= ZEBRA_NEXTHOP_REGISTER
;
619 command
= ZEBRA_NEXTHOP_UNREGISTER
;
621 if (zclient_send_rnh(zclient
, command
, p
, SAFI_UNICAST
, connected
,
622 false, vrf_id
) == ZCLIENT_SEND_FAILURE
)
623 zlog_warn("%s: Failure to send nexthop to zebra", __func__
);
626 static int sharp_debug_nexthops(struct zapi_route
*api
)
630 if (api
->nexthop_num
== 0) {
631 zlog_debug(" Not installed");
635 for (i
= 0; i
< api
->nexthop_num
; i
++) {
636 struct zapi_nexthop
*znh
= &api
->nexthops
[i
];
639 case NEXTHOP_TYPE_IPV4_IFINDEX
:
640 case NEXTHOP_TYPE_IPV4
:
642 " Nexthop %pI4, type: %d, ifindex: %d, vrf: %d, label_num: %d",
643 &znh
->gate
.ipv4
.s_addr
, znh
->type
, znh
->ifindex
,
644 znh
->vrf_id
, znh
->label_num
);
646 case NEXTHOP_TYPE_IPV6_IFINDEX
:
647 case NEXTHOP_TYPE_IPV6
:
649 " Nexthop %pI6, type: %d, ifindex: %d, vrf: %d, label_num: %d",
650 &znh
->gate
.ipv6
, znh
->type
, znh
->ifindex
,
651 znh
->vrf_id
, znh
->label_num
);
653 case NEXTHOP_TYPE_IFINDEX
:
654 zlog_debug(" Nexthop IFINDEX: %d, ifindex: %d",
655 znh
->type
, znh
->ifindex
);
657 case NEXTHOP_TYPE_BLACKHOLE
:
658 zlog_debug(" Nexthop blackhole");
665 static int sharp_nexthop_update(ZAPI_CALLBACK_ARGS
)
667 struct sharp_nh_tracker
*nht
;
668 struct zapi_route nhr
;
669 struct prefix matched
;
671 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &matched
, &nhr
)) {
672 zlog_err("%s: Decode of update failed", __func__
);
676 zlog_debug("Received update for %pFX actual match: %pFX metric: %u",
677 &matched
, &nhr
.prefix
, nhr
.metric
);
679 nht
= sharp_nh_tracker_get(&matched
);
680 nht
->nhop_num
= nhr
.nexthop_num
;
683 sharp_debug_nexthops(&nhr
);
688 static int sharp_redistribute_route(ZAPI_CALLBACK_ARGS
)
690 struct zapi_route api
;
692 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
693 zlog_warn("%s: Decode of redistribute failed: %d", __func__
,
694 ZEBRA_REDISTRIBUTE_ROUTE_ADD
);
696 zlog_debug("%s: %pFX (%s)", zserv_command_string(cmd
), &api
.prefix
,
697 zebra_route_string(api
.type
));
699 sharp_debug_nexthops(&api
);
704 void sharp_redistribute_vrf(struct vrf
*vrf
, int type
)
706 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
, type
,
710 static zclient_handler
*const sharp_opaque_handlers
[] = {
711 [ZEBRA_OPAQUE_MESSAGE
] = sharp_opaque_handler
,
714 /* Add a zclient with a specified session id, for testing. */
715 int sharp_zclient_create(uint32_t session_id
)
717 struct zclient
*client
;
718 struct sharp_zclient
*node
;
720 /* Check for duplicates */
721 for (node
= sharp_clients_head
; node
!= NULL
; node
= node
->next
) {
722 if (node
->client
->session_id
== session_id
)
726 client
= zclient_new(master
, &zclient_options_default
,
727 sharp_opaque_handlers
,
728 array_size(sharp_opaque_handlers
));
730 client
->session_id
= session_id
;
732 zclient_init(client
, ZEBRA_ROUTE_SHARP
, 0, &sharp_privs
);
734 /* Enqueue on the list of test clients */
740 /* Delete one of the extra test zclients */
741 int sharp_zclient_delete(uint32_t session_id
)
743 struct sharp_zclient
*node
;
745 /* Search for session */
746 for (node
= sharp_clients_head
; node
!= NULL
; node
= node
->next
) {
747 if (node
->client
->session_id
== session_id
) {
748 /* Dequeue from list */
750 node
->next
->prev
= node
->prev
;
752 node
->prev
->next
= node
->next
;
753 if (node
== sharp_clients_head
)
754 sharp_clients_head
= node
->next
;
756 /* Clean up zclient */
757 zclient_stop(node
->client
);
758 zclient_free(node
->client
);
761 XFREE(MTYPE_ZC
, node
);
769 static const char *const type2txt
[] = {"Generic", "Vertex", "Edge", "Subnet"};
770 static const char *const status2txt
[] = {"Unknown", "New", "Update",
771 "Delete", "Sync", "Orphan"};
772 /* Handler for opaque messages */
773 static int sharp_opaque_handler(ZAPI_CALLBACK_ARGS
)
776 struct zapi_opaque_msg info
;
777 struct ls_element
*lse
;
781 if (zclient_opaque_decode(s
, &info
) != 0)
784 zlog_debug("%s: [%u] received opaque type %u", __func__
,
785 zclient
->session_id
, info
.type
);
787 if (info
.type
== LINK_STATE_UPDATE
) {
788 lse
= ls_stream2ted(sg
.ted
, s
, false);
790 zlog_debug(" |- Got %s %s from Link State Database",
791 status2txt
[lse
->status
],
792 type2txt
[lse
->type
]);
796 "%s: Error to convert Stream into Link State",
804 * Send OPAQUE messages, using subtype 'type'.
806 void sharp_opaque_send(uint32_t type
, uint32_t proto
, uint32_t instance
,
807 uint32_t session_id
, uint32_t count
)
813 /* Prepare a small payload */
814 for (i
= 0; i
< sizeof(buf
); i
++) {
821 /* Send some messages - broadcast and unicast are supported */
822 for (i
= 0; i
< count
; i
++) {
824 ret
= zclient_send_opaque(zclient
, type
, buf
,
827 ret
= zclient_send_opaque_unicast(zclient
, type
, proto
,
828 instance
, session_id
,
830 if (ret
== ZCLIENT_SEND_FAILURE
) {
831 zlog_debug("%s: send_opaque() failed => %d", __func__
,
839 * Send OPAQUE registration messages, using subtype 'type'.
841 void sharp_opaque_reg_send(bool is_reg
, uint32_t proto
, uint32_t instance
,
842 uint32_t session_id
, uint32_t type
)
850 zclient_create_header(s
, ZEBRA_OPAQUE_REGISTER
, VRF_DEFAULT
);
852 zclient_create_header(s
, ZEBRA_OPAQUE_UNREGISTER
, VRF_DEFAULT
);
855 stream_putl(s
, type
);
857 /* Add zclient info */
858 stream_putc(s
, proto
);
859 stream_putw(s
, instance
);
860 stream_putl(s
, session_id
);
862 /* Put length at the first point of the stream. */
863 stream_putw_at(s
, 0, stream_get_endp(s
));
865 (void)zclient_send_message(zclient
);
868 /* Link State registration */
869 void sharp_zebra_register_te(void)
871 /* First register to received Link State Update messages */
872 zclient_register_opaque(zclient
, LINK_STATE_UPDATE
);
874 /* Then, request initial TED with SYNC message */
875 ls_request_sync(zclient
);
878 void sharp_zebra_send_arp(const struct interface
*ifp
, const struct prefix
*p
)
880 zclient_send_neigh_discovery_req(zclient
, ifp
, p
);
883 static int nhg_notify_owner(ZAPI_CALLBACK_ARGS
)
885 enum zapi_nhg_notify_owner note
;
888 if (!zapi_nhg_notify_decode(zclient
->ibuf
, &id
, ¬e
))
892 case ZAPI_NHG_INSTALLED
:
893 sharp_nhgroup_id_set_installed(id
, true);
894 zlog_debug("Installed nhg %u", id
);
896 case ZAPI_NHG_FAIL_INSTALL
:
897 zlog_debug("Failed install of nhg %u", id
);
899 case ZAPI_NHG_REMOVED
:
900 zlog_debug("Removed nhg %u", id
);
902 case ZAPI_NHG_REMOVE_FAIL
:
903 zlog_debug("Failed removal of nhg %u", id
);
910 int sharp_zebra_srv6_manager_get_locator_chunk(const char *locator_name
)
912 return srv6_manager_get_locator_chunk(zclient
, locator_name
);
915 int sharp_zebra_srv6_manager_release_locator_chunk(const char *locator_name
)
917 return srv6_manager_release_locator_chunk(zclient
, locator_name
);
920 static int sharp_zebra_process_srv6_locator_chunk(ZAPI_CALLBACK_ARGS
)
922 struct stream
*s
= NULL
;
923 struct srv6_locator_chunk s6c
= {};
924 struct listnode
*node
, *nnode
;
925 struct sharp_srv6_locator
*loc
;
928 zapi_srv6_locator_chunk_decode(s
, &s6c
);
930 for (ALL_LIST_ELEMENTS(sg
.srv6_locators
, node
, nnode
, loc
)) {
931 struct prefix_ipv6
*chunk
= NULL
;
932 struct listnode
*chunk_node
;
933 struct prefix_ipv6
*c
;
935 if (strcmp(loc
->name
, s6c
.locator_name
) != 0) {
936 zlog_err("%s: Locator name unmatch %s:%s", __func__
,
937 loc
->name
, s6c
.locator_name
);
941 for (ALL_LIST_ELEMENTS_RO(loc
->chunks
, chunk_node
, c
))
942 if (!prefix_cmp(c
, &s6c
.prefix
))
945 chunk
= prefix_ipv6_new();
947 listnode_add(loc
->chunks
, chunk
);
951 zlog_err("%s: can't get locator_chunk!!", __func__
);
955 int sharp_zebra_send_interface_protodown(struct interface
*ifp
, bool down
)
957 zlog_debug("Sending zebra to set %s protodown %s", ifp
->name
,
958 down
? "on" : "off");
960 if (zclient_send_interface_protodown(zclient
, ifp
->vrf
->vrf_id
, ifp
,
961 down
) == ZCLIENT_SEND_FAILURE
)
967 int sharp_zebra_send_tc_filter_rate(struct interface
*ifp
,
968 const struct prefix
*source
,
969 const struct prefix
*destination
,
970 uint8_t ip_proto
, uint16_t src_port
,
971 uint16_t dst_port
, uint64_t rate
)
973 #define SHARPD_TC_HANDLE 0x0001
978 struct tc_qdisc q
= {.ifindex
= ifp
->ifindex
, .kind
= TC_QDISC_HTB
};
980 zapi_tc_qdisc_encode(ZEBRA_TC_QDISC_INSTALL
, s
, &q
);
981 if (zclient_send_message(zclient
) == ZCLIENT_SEND_FAILURE
)
984 struct tc_class c
= {.ifindex
= ifp
->ifindex
,
985 .handle
= SHARPD_TC_HANDLE
& 0xffff,
986 .kind
= TC_QDISC_HTB
,
990 zapi_tc_class_encode(ZEBRA_TC_CLASS_ADD
, s
, &c
);
991 if (zclient_send_message(zclient
) == ZCLIENT_SEND_FAILURE
)
994 struct tc_filter f
= {.ifindex
= ifp
->ifindex
,
995 .handle
= SHARPD_TC_HANDLE
,
997 .kind
= TC_FILTER_FLOWER
,
998 .u
.flower
.filter_bm
= 0};
1001 f
.protocol
= ETH_P_IP
;
1003 f
.protocol
= 0x0800;
1006 f
.u
.flower
.filter_bm
|= TC_FLOWER_IP_PROTOCOL
;
1007 f
.u
.flower
.ip_proto
= ip_proto
;
1008 f
.u
.flower
.filter_bm
|= TC_FLOWER_SRC_IP
;
1009 prefix_copy(&f
.u
.flower
.src_ip
, source
);
1010 f
.u
.flower
.filter_bm
|= TC_FLOWER_DST_IP
;
1011 prefix_copy(&f
.u
.flower
.dst_ip
, destination
);
1012 f
.u
.flower
.filter_bm
|= TC_FLOWER_SRC_PORT
;
1013 f
.u
.flower
.src_port_min
= f
.u
.flower
.src_port_max
= src_port
;
1014 f
.u
.flower
.filter_bm
|= TC_FLOWER_DST_PORT
;
1015 f
.u
.flower
.dst_port_min
= f
.u
.flower
.dst_port_max
= dst_port
;
1016 f
.u
.flower
.classid
= SHARPD_TC_HANDLE
& 0xffff;
1018 zapi_tc_filter_encode(ZEBRA_TC_FILTER_ADD
, s
, &f
);
1019 if (zclient_send_message(zclient
) == ZCLIENT_SEND_FAILURE
)
1025 static zclient_handler
*const sharp_handlers
[] = {
1026 [ZEBRA_INTERFACE_ADDRESS_ADD
] = interface_address_add
,
1027 [ZEBRA_INTERFACE_ADDRESS_DELETE
] = interface_address_delete
,
1028 [ZEBRA_ROUTE_NOTIFY_OWNER
] = route_notify_owner
,
1029 [ZEBRA_NEXTHOP_UPDATE
] = sharp_nexthop_update
,
1030 [ZEBRA_NHG_NOTIFY_OWNER
] = nhg_notify_owner
,
1031 [ZEBRA_REDISTRIBUTE_ROUTE_ADD
] = sharp_redistribute_route
,
1032 [ZEBRA_REDISTRIBUTE_ROUTE_DEL
] = sharp_redistribute_route
,
1033 [ZEBRA_OPAQUE_MESSAGE
] = sharp_opaque_handler
,
1034 [ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK
] =
1035 sharp_zebra_process_srv6_locator_chunk
,
1038 void sharp_zebra_init(void)
1040 struct zclient_options opt
= {.receive_notify
= true};
1042 if_zapi_callbacks(sharp_ifp_create
, sharp_ifp_up
, sharp_ifp_down
,
1045 zclient
= zclient_new(master
, &opt
, sharp_handlers
,
1046 array_size(sharp_handlers
));
1048 zclient_init(zclient
, ZEBRA_ROUTE_SHARP
, 0, &sharp_privs
);
1049 zclient
->zebra_connected
= zebra_connected
;
1050 zclient
->zebra_buffer_write_ready
= sharp_zclient_buffer_ready
;