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
37 #include "nexthop_group.h"
39 #include "sharp_globals.h"
40 #include "sharp_nht.h"
41 #include "sharp_zebra.h"
43 /* Zebra structure to hold current status. */
44 struct zclient
*zclient
= NULL
;
46 /* For registering threads. */
47 extern struct thread_master
*master
;
49 /* Inteface addition message from zebra. */
50 static int sharp_ifp_create(struct interface
*ifp
)
55 static int interface_delete(ZAPI_CALLBACK_ARGS
)
57 struct interface
*ifp
;
61 /* zebra_interface_state_read () updates interface structure in iflist
63 ifp
= zebra_interface_state_read(s
, vrf_id
);
68 if_set_index(ifp
, IFINDEX_INTERNAL
);
73 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 interface_state_down(ZAPI_CALLBACK_ARGS
)
102 zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
107 void sharp_install_routes_helper(struct prefix
*p
, vrf_id_t vrf_id
,
108 uint8_t instance
, struct nexthop_group
*nhg
,
114 zlog_debug("Inserting %u routes", routes
);
116 if (p
->family
== AF_INET
) {
118 temp
= ntohl(p
->u
.prefix4
.s_addr
);
120 temp
= ntohl(p
->u
.val32
[3]);
122 monotime(&sg
.r
.t_start
);
123 for (i
= 0; i
< routes
; i
++) {
124 route_add(p
, vrf_id
, (uint8_t)instance
, nhg
);
126 p
->u
.prefix4
.s_addr
= htonl(++temp
);
128 p
->u
.val32
[3] = htonl(++temp
);
132 void sharp_remove_routes_helper(struct prefix
*p
, vrf_id_t vrf_id
,
133 uint8_t instance
, uint32_t routes
)
138 zlog_debug("Removing %u routes", routes
);
140 if (p
->family
== AF_INET
) {
142 temp
= ntohl(p
->u
.prefix4
.s_addr
);
144 temp
= ntohl(p
->u
.val32
[3]);
146 monotime(&sg
.r
.t_start
);
147 for (i
= 0; i
< routes
; i
++) {
148 route_delete(p
, vrf_id
, (uint8_t)instance
);
150 p
->u
.prefix4
.s_addr
= htonl(++temp
);
152 p
->u
.val32
[3] = htonl(++temp
);
156 static void handle_repeated(bool installed
)
158 struct prefix p
= sg
.r
.orig_prefix
;
161 if (sg
.r
.repeat
<= 0)
165 sg
.r
.removed_routes
= 0;
166 sharp_remove_routes_helper(&p
, sg
.r
.vrf_id
,
167 sg
.r
.inst
, sg
.r
.total_routes
);
171 sg
.r
.installed_routes
= 0;
172 sharp_install_routes_helper(&p
, sg
.r
.vrf_id
, sg
.r
.inst
,
178 static int route_notify_owner(ZAPI_CALLBACK_ARGS
)
182 enum zapi_route_notify_owner note
;
185 if (!zapi_route_notify_decode(zclient
->ibuf
, &p
, &table
, ¬e
))
189 case ZAPI_ROUTE_INSTALLED
:
190 sg
.r
.installed_routes
++;
191 if (sg
.r
.total_routes
== sg
.r
.installed_routes
) {
192 monotime(&sg
.r
.t_end
);
193 timersub(&sg
.r
.t_end
, &sg
.r
.t_start
, &r
);
194 zlog_debug("Installed All Items %jd.%ld",
195 (intmax_t)r
.tv_sec
, (long)r
.tv_usec
);
196 handle_repeated(true);
199 case ZAPI_ROUTE_FAIL_INSTALL
:
200 zlog_debug("Failed install of route");
202 case ZAPI_ROUTE_BETTER_ADMIN_WON
:
203 zlog_debug("Better Admin Distance won over us");
205 case ZAPI_ROUTE_REMOVED
:
206 sg
.r
.removed_routes
++;
207 if (sg
.r
.total_routes
== sg
.r
.removed_routes
) {
208 monotime(&sg
.r
.t_end
);
209 timersub(&sg
.r
.t_end
, &sg
.r
.t_start
, &r
);
210 zlog_debug("Removed all Items %jd.%ld",
211 (intmax_t)r
.tv_sec
, (long)r
.tv_usec
);
212 handle_repeated(false);
215 case ZAPI_ROUTE_REMOVE_FAIL
:
216 zlog_debug("Route removal Failure");
222 static void zebra_connected(struct zclient
*zclient
)
224 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
227 void vrf_label_add(vrf_id_t vrf_id
, afi_t afi
, mpls_label_t label
)
229 zclient_send_vrf_label(zclient
, vrf_id
, afi
, label
, ZEBRA_LSP_SHARP
);
232 void route_add(struct prefix
*p
, vrf_id_t vrf_id
,
233 uint8_t instance
, struct nexthop_group
*nhg
)
235 struct zapi_route api
;
236 struct zapi_nexthop
*api_nh
;
240 memset(&api
, 0, sizeof(api
));
242 api
.type
= ZEBRA_ROUTE_SHARP
;
243 api
.instance
= instance
;
244 api
.safi
= SAFI_UNICAST
;
245 memcpy(&api
.prefix
, p
, sizeof(*p
));
247 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
248 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
250 for (ALL_NEXTHOPS_PTR(nhg
, nh
)) {
251 api_nh
= &api
.nexthops
[i
];
252 api_nh
->vrf_id
= nh
->vrf_id
;
253 api_nh
->type
= nh
->type
;
255 case NEXTHOP_TYPE_IPV4
:
256 api_nh
->gate
= nh
->gate
;
258 case NEXTHOP_TYPE_IPV4_IFINDEX
:
259 api_nh
->gate
= nh
->gate
;
260 api_nh
->ifindex
= nh
->ifindex
;
262 case NEXTHOP_TYPE_IFINDEX
:
263 api_nh
->ifindex
= nh
->ifindex
;
265 case NEXTHOP_TYPE_IPV6
:
266 memcpy(&api_nh
->gate
.ipv6
, &nh
->gate
.ipv6
, 16);
268 case NEXTHOP_TYPE_IPV6_IFINDEX
:
269 api_nh
->ifindex
= nh
->ifindex
;
270 memcpy(&api_nh
->gate
.ipv6
, &nh
->gate
.ipv6
, 16);
272 case NEXTHOP_TYPE_BLACKHOLE
:
273 api_nh
->bh_type
= nh
->bh_type
;
280 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
283 void route_delete(struct prefix
*p
, vrf_id_t vrf_id
, uint8_t instance
)
285 struct zapi_route api
;
287 memset(&api
, 0, sizeof(api
));
289 api
.type
= ZEBRA_ROUTE_SHARP
;
290 api
.safi
= SAFI_UNICAST
;
291 api
.instance
= instance
;
292 memcpy(&api
.prefix
, p
, sizeof(*p
));
293 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
298 void sharp_zebra_nexthop_watch(struct prefix
*p
, vrf_id_t vrf_id
, bool import
,
299 bool watch
, bool connected
)
304 command
= ZEBRA_NEXTHOP_REGISTER
;
307 command
= ZEBRA_NEXTHOP_UNREGISTER
;
309 command
= ZEBRA_IMPORT_ROUTE_REGISTER
;
312 command
= ZEBRA_IMPORT_ROUTE_UNREGISTER
;
315 if (zclient_send_rnh(zclient
, command
, p
, connected
, vrf_id
) < 0)
316 zlog_warn("%s: Failure to send nexthop to zebra",
317 __PRETTY_FUNCTION__
);
320 static int sharp_nexthop_update(ZAPI_CALLBACK_ARGS
)
322 struct sharp_nh_tracker
*nht
;
323 struct zapi_route nhr
;
324 char buf
[PREFIX_STRLEN
];
327 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &nhr
)) {
328 zlog_warn("%s: Decode of update failed", __PRETTY_FUNCTION__
);
333 zlog_debug("Received update for %s",
334 prefix2str(&nhr
.prefix
, buf
, sizeof(buf
)));
336 nht
= sharp_nh_tracker_get(&nhr
.prefix
);
337 nht
->nhop_num
= nhr
.nexthop_num
;
340 for (i
= 0; i
< nhr
.nexthop_num
; i
++) {
341 struct zapi_nexthop
*znh
= &nhr
.nexthops
[i
];
344 case NEXTHOP_TYPE_IPV4_IFINDEX
:
345 case NEXTHOP_TYPE_IPV4
:
347 "\tNexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d",
348 inet_ntop(AF_INET
, &znh
->gate
.ipv4
.s_addr
, buf
,
350 znh
->type
, znh
->ifindex
, znh
->vrf_id
,
353 case NEXTHOP_TYPE_IPV6_IFINDEX
:
354 case NEXTHOP_TYPE_IPV6
:
356 "\tNexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d",
357 inet_ntop(AF_INET6
, &znh
->gate
.ipv6
, buf
,
359 znh
->type
, znh
->ifindex
, znh
->vrf_id
,
362 case NEXTHOP_TYPE_IFINDEX
:
363 zlog_debug("\tNexthop IFINDEX: %d, ifindex: %d",
364 znh
->type
, znh
->ifindex
);
366 case NEXTHOP_TYPE_BLACKHOLE
:
367 zlog_debug("\tNexthop blackhole");
374 static int sharp_ifp_down(struct interface
*ifp
)
379 static int sharp_ifp_destroy(struct interface
*ifp
)
384 extern struct zebra_privs_t sharp_privs
;
386 void sharp_zebra_init(void)
388 struct zclient_options opt
= {.receive_notify
= true};
390 if_zapi_callbacks(sharp_ifp_create
, sharp_ifp_up
,
391 sharp_ifp_down
, sharp_ifp_destroy
);
393 zclient
= zclient_new(master
, &opt
);
395 zclient_init(zclient
, ZEBRA_ROUTE_SHARP
, 0, &sharp_privs
);
396 zclient
->zebra_connected
= zebra_connected
;
397 zclient
->interface_delete
= interface_delete
;
398 zclient
->interface_down
= interface_state_down
;
399 zclient
->interface_address_add
= interface_address_add
;
400 zclient
->interface_address_delete
= interface_address_delete
;
401 zclient
->route_notify_owner
= route_notify_owner
;
402 zclient
->nexthop_update
= sharp_nexthop_update
;
403 zclient
->import_check_update
= sharp_nexthop_update
;