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 static struct interface
*zebra_interface_if_lookup(struct stream
*s
)
51 char ifname_tmp
[INTERFACE_NAMSIZ
];
53 /* Read interface name. */
54 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
57 return if_lookup_by_name(ifname_tmp
, VRF_DEFAULT
);
60 /* Inteface addition message from zebra. */
61 static int sharp_ifp_create(struct interface
*ifp
)
66 static int interface_delete(ZAPI_CALLBACK_ARGS
)
68 struct interface
*ifp
;
72 /* zebra_interface_state_read () updates interface structure in iflist
74 ifp
= zebra_interface_state_read(s
, vrf_id
);
79 if_set_index(ifp
, IFINDEX_INTERNAL
);
84 static int interface_address_add(ZAPI_CALLBACK_ARGS
)
87 zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
92 static int interface_address_delete(ZAPI_CALLBACK_ARGS
)
96 c
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
105 static int interface_state_up(ZAPI_CALLBACK_ARGS
)
108 zebra_interface_if_lookup(zclient
->ibuf
);
113 static int interface_state_down(ZAPI_CALLBACK_ARGS
)
116 zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
121 void sharp_install_routes_helper(struct prefix
*p
, vrf_id_t vrf_id
,
122 uint8_t instance
, struct nexthop_group
*nhg
,
128 zlog_debug("Inserting %u routes", routes
);
130 if (p
->family
== AF_INET
) {
132 temp
= ntohl(p
->u
.prefix4
.s_addr
);
134 temp
= ntohl(p
->u
.val32
[3]);
136 monotime(&sg
.r
.t_start
);
137 for (i
= 0; i
< routes
; i
++) {
138 route_add(p
, vrf_id
, (uint8_t)instance
, nhg
);
140 p
->u
.prefix4
.s_addr
= htonl(++temp
);
142 p
->u
.val32
[3] = htonl(++temp
);
146 void sharp_remove_routes_helper(struct prefix
*p
, vrf_id_t vrf_id
,
147 uint8_t instance
, uint32_t routes
)
152 zlog_debug("Removing %u routes", routes
);
154 if (p
->family
== AF_INET
) {
156 temp
= ntohl(p
->u
.prefix4
.s_addr
);
158 temp
= ntohl(p
->u
.val32
[3]);
160 monotime(&sg
.r
.t_start
);
161 for (i
= 0; i
< routes
; i
++) {
162 route_delete(p
, vrf_id
, (uint8_t)instance
);
164 p
->u
.prefix4
.s_addr
= htonl(++temp
);
166 p
->u
.val32
[3] = htonl(++temp
);
170 static void handle_repeated(bool installed
)
172 struct prefix p
= sg
.r
.orig_prefix
;
175 if (sg
.r
.repeat
<= 0)
179 sg
.r
.removed_routes
= 0;
180 sharp_remove_routes_helper(&p
, sg
.r
.vrf_id
,
181 sg
.r
.inst
, sg
.r
.total_routes
);
185 sg
.r
.installed_routes
= 0;
186 sharp_install_routes_helper(&p
, sg
.r
.vrf_id
, sg
.r
.inst
,
192 static int route_notify_owner(ZAPI_CALLBACK_ARGS
)
196 enum zapi_route_notify_owner note
;
199 if (!zapi_route_notify_decode(zclient
->ibuf
, &p
, &table
, ¬e
))
203 case ZAPI_ROUTE_INSTALLED
:
204 sg
.r
.installed_routes
++;
205 if (sg
.r
.total_routes
== sg
.r
.installed_routes
) {
206 monotime(&sg
.r
.t_end
);
207 timersub(&sg
.r
.t_end
, &sg
.r
.t_start
, &r
);
208 zlog_debug("Installed All Items %jd.%ld",
209 (intmax_t)r
.tv_sec
, (long)r
.tv_usec
);
210 handle_repeated(true);
213 case ZAPI_ROUTE_FAIL_INSTALL
:
214 zlog_debug("Failed install of route");
216 case ZAPI_ROUTE_BETTER_ADMIN_WON
:
217 zlog_debug("Better Admin Distance won over us");
219 case ZAPI_ROUTE_REMOVED
:
220 sg
.r
.removed_routes
++;
221 if (sg
.r
.total_routes
== sg
.r
.removed_routes
) {
222 monotime(&sg
.r
.t_end
);
223 timersub(&sg
.r
.t_end
, &sg
.r
.t_start
, &r
);
224 zlog_debug("Removed all Items %jd.%ld",
225 (intmax_t)r
.tv_sec
, (long)r
.tv_usec
);
226 handle_repeated(false);
229 case ZAPI_ROUTE_REMOVE_FAIL
:
230 zlog_debug("Route removal Failure");
236 static void zebra_connected(struct zclient
*zclient
)
238 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
241 void vrf_label_add(vrf_id_t vrf_id
, afi_t afi
, mpls_label_t label
)
243 zclient_send_vrf_label(zclient
, vrf_id
, afi
, label
, ZEBRA_LSP_SHARP
);
246 void route_add(struct prefix
*p
, vrf_id_t vrf_id
,
247 uint8_t instance
, struct nexthop_group
*nhg
)
249 struct zapi_route api
;
250 struct zapi_nexthop
*api_nh
;
254 memset(&api
, 0, sizeof(api
));
256 api
.type
= ZEBRA_ROUTE_SHARP
;
257 api
.instance
= instance
;
258 api
.safi
= SAFI_UNICAST
;
259 memcpy(&api
.prefix
, p
, sizeof(*p
));
261 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
262 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
264 for (ALL_NEXTHOPS_PTR(nhg
, nh
)) {
265 api_nh
= &api
.nexthops
[i
];
266 api_nh
->vrf_id
= nh
->vrf_id
;
267 api_nh
->type
= nh
->type
;
269 case NEXTHOP_TYPE_IPV4
:
270 api_nh
->gate
= nh
->gate
;
272 case NEXTHOP_TYPE_IPV4_IFINDEX
:
273 api_nh
->gate
= nh
->gate
;
274 api_nh
->ifindex
= nh
->ifindex
;
276 case NEXTHOP_TYPE_IFINDEX
:
277 api_nh
->ifindex
= nh
->ifindex
;
279 case NEXTHOP_TYPE_IPV6
:
280 memcpy(&api_nh
->gate
.ipv6
, &nh
->gate
.ipv6
, 16);
282 case NEXTHOP_TYPE_IPV6_IFINDEX
:
283 api_nh
->ifindex
= nh
->ifindex
;
284 memcpy(&api_nh
->gate
.ipv6
, &nh
->gate
.ipv6
, 16);
286 case NEXTHOP_TYPE_BLACKHOLE
:
287 api_nh
->bh_type
= nh
->bh_type
;
294 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
297 void route_delete(struct prefix
*p
, vrf_id_t vrf_id
, uint8_t instance
)
299 struct zapi_route api
;
301 memset(&api
, 0, sizeof(api
));
303 api
.type
= ZEBRA_ROUTE_SHARP
;
304 api
.safi
= SAFI_UNICAST
;
305 api
.instance
= instance
;
306 memcpy(&api
.prefix
, p
, sizeof(*p
));
307 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
312 void sharp_zebra_nexthop_watch(struct prefix
*p
, vrf_id_t vrf_id
, bool import
,
313 bool watch
, bool connected
)
318 command
= ZEBRA_NEXTHOP_REGISTER
;
321 command
= ZEBRA_NEXTHOP_UNREGISTER
;
323 command
= ZEBRA_IMPORT_ROUTE_REGISTER
;
326 command
= ZEBRA_IMPORT_ROUTE_UNREGISTER
;
329 if (zclient_send_rnh(zclient
, command
, p
, connected
, vrf_id
) < 0)
330 zlog_warn("%s: Failure to send nexthop to zebra",
331 __PRETTY_FUNCTION__
);
334 static int sharp_nexthop_update(ZAPI_CALLBACK_ARGS
)
336 struct sharp_nh_tracker
*nht
;
337 struct zapi_route nhr
;
338 char buf
[PREFIX_STRLEN
];
341 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &nhr
)) {
342 zlog_warn("%s: Decode of update failed", __PRETTY_FUNCTION__
);
347 zlog_debug("Received update for %s",
348 prefix2str(&nhr
.prefix
, buf
, sizeof(buf
)));
350 nht
= sharp_nh_tracker_get(&nhr
.prefix
);
351 nht
->nhop_num
= nhr
.nexthop_num
;
354 for (i
= 0; i
< nhr
.nexthop_num
; i
++) {
355 struct zapi_nexthop
*znh
= &nhr
.nexthops
[i
];
358 case NEXTHOP_TYPE_IPV4_IFINDEX
:
359 case NEXTHOP_TYPE_IPV4
:
361 "\tNexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d",
362 inet_ntop(AF_INET
, &znh
->gate
.ipv4
.s_addr
, buf
,
364 znh
->type
, znh
->ifindex
, znh
->vrf_id
,
367 case NEXTHOP_TYPE_IPV6_IFINDEX
:
368 case NEXTHOP_TYPE_IPV6
:
370 "\tNexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d",
371 inet_ntop(AF_INET6
, &znh
->gate
.ipv6
, buf
,
373 znh
->type
, znh
->ifindex
, znh
->vrf_id
,
376 case NEXTHOP_TYPE_IFINDEX
:
377 zlog_debug("\tNexthop IFINDEX: %d, ifindex: %d",
378 znh
->type
, znh
->ifindex
);
380 case NEXTHOP_TYPE_BLACKHOLE
:
381 zlog_debug("\tNexthop blackhole");
388 static int sharp_ifp_up(struct interface
*ifp
)
393 static int sharp_ifp_down(struct interface
*ifp
)
398 static int sharp_ifp_destroy(struct interface
*ifp
)
403 extern struct zebra_privs_t sharp_privs
;
405 void sharp_zebra_init(void)
407 struct zclient_options opt
= {.receive_notify
= true};
409 if_zapi_callbacks(sharp_ifp_create
, sharp_ifp_up
,
410 sharp_ifp_down
, sharp_ifp_destroy
);
412 zclient
= zclient_new(master
, &opt
);
414 zclient_init(zclient
, ZEBRA_ROUTE_SHARP
, 0, &sharp_privs
);
415 zclient
->zebra_connected
= zebra_connected
;
416 zclient
->interface_delete
= interface_delete
;
417 zclient
->interface_up
= interface_state_up
;
418 zclient
->interface_down
= interface_state_down
;
419 zclient
->interface_address_add
= interface_address_add
;
420 zclient
->interface_address_delete
= interface_address_delete
;
421 zclient
->route_notify_owner
= route_notify_owner
;
422 zclient
->nexthop_update
= sharp_nexthop_update
;
423 zclient
->import_check_update
= sharp_nexthop_update
;