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 interface_add(int command
, struct zclient
*zclient
,
62 zebra_size_t length
, vrf_id_t vrf_id
)
64 struct interface
*ifp
;
66 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
74 static int interface_delete(int command
, struct zclient
*zclient
,
75 zebra_size_t length
, vrf_id_t vrf_id
)
77 struct interface
*ifp
;
81 /* zebra_interface_state_read () updates interface structure in iflist
83 ifp
= zebra_interface_state_read(s
, vrf_id
);
88 if_set_index(ifp
, IFINDEX_INTERNAL
);
93 static int interface_address_add(int command
, struct zclient
*zclient
,
94 zebra_size_t length
, vrf_id_t vrf_id
)
97 zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
102 static int interface_address_delete(int command
, struct zclient
*zclient
,
103 zebra_size_t length
, vrf_id_t vrf_id
)
107 c
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
116 static int interface_state_up(int command
, struct zclient
*zclient
,
117 zebra_size_t length
, vrf_id_t vrf_id
)
120 zebra_interface_if_lookup(zclient
->ibuf
);
125 static int interface_state_down(int command
, struct zclient
*zclient
,
126 zebra_size_t length
, vrf_id_t vrf_id
)
129 zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
134 void sharp_install_routes_helper(struct prefix
*p
, uint8_t instance
,
135 struct nexthop_group
*nhg
,
141 zlog_debug("Inserting %u routes", routes
);
143 if (p
->family
== AF_INET
) {
145 temp
= ntohl(p
->u
.prefix4
.s_addr
);
147 temp
= ntohl(p
->u
.val32
[3]);
149 monotime(&sg
.r
.t_start
);
150 for (i
= 0; i
< routes
; i
++) {
151 route_add(p
, (uint8_t)instance
, nhg
);
153 p
->u
.prefix4
.s_addr
= htonl(++temp
);
155 p
->u
.val32
[3] = htonl(++temp
);
159 void sharp_remove_routes_helper(struct prefix
*p
, uint8_t instance
,
165 zlog_debug("Removing %u routes", routes
);
167 if (p
->family
== AF_INET
) {
169 temp
= ntohl(p
->u
.prefix4
.s_addr
);
171 temp
= ntohl(p
->u
.val32
[3]);
173 monotime(&sg
.r
.t_start
);
174 for (i
= 0; i
< routes
; i
++) {
175 route_delete(p
, (uint8_t)instance
);
177 p
->u
.prefix4
.s_addr
= htonl(++temp
);
179 p
->u
.val32
[3] = htonl(++temp
);
183 static void handle_repeated(bool installed
)
185 struct prefix p
= sg
.r
.orig_prefix
;
188 if (sg
.r
.repeat
<= 0)
192 sg
.r
.removed_routes
= 0;
193 sharp_remove_routes_helper(&p
, sg
.r
.inst
, sg
.r
.total_routes
);
197 sg
.r
.installed_routes
= 0;
198 sharp_install_routes_helper(&p
, sg
.r
.inst
, &sg
.r
.nhop_group
,
203 static int route_notify_owner(int command
, struct zclient
*zclient
,
204 zebra_size_t length
, vrf_id_t vrf_id
)
208 enum zapi_route_notify_owner note
;
211 if (!zapi_route_notify_decode(zclient
->ibuf
, &p
, &table
, ¬e
))
215 case ZAPI_ROUTE_INSTALLED
:
216 sg
.r
.installed_routes
++;
217 if (sg
.r
.total_routes
== sg
.r
.installed_routes
) {
218 monotime(&sg
.r
.t_end
);
219 timersub(&sg
.r
.t_end
, &sg
.r
.t_start
, &r
);
220 zlog_debug("Installed All Items %ld.%ld", r
.tv_sec
,
222 handle_repeated(true);
225 case ZAPI_ROUTE_FAIL_INSTALL
:
226 zlog_debug("Failed install of route");
228 case ZAPI_ROUTE_BETTER_ADMIN_WON
:
229 zlog_debug("Better Admin Distance won over us");
231 case ZAPI_ROUTE_REMOVED
:
232 sg
.r
.removed_routes
++;
233 if (sg
.r
.total_routes
== sg
.r
.removed_routes
) {
234 monotime(&sg
.r
.t_end
);
235 timersub(&sg
.r
.t_end
, &sg
.r
.t_start
, &r
);
236 zlog_debug("Removed all Items %ld.%ld", r
.tv_sec
,
238 handle_repeated(false);
241 case ZAPI_ROUTE_REMOVE_FAIL
:
242 zlog_debug("Route removal Failure");
248 static void zebra_connected(struct zclient
*zclient
)
250 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
253 void vrf_label_add(vrf_id_t vrf_id
, afi_t afi
, mpls_label_t label
)
255 zclient_send_vrf_label(zclient
, vrf_id
, afi
, label
, ZEBRA_LSP_SHARP
);
258 void route_add(struct prefix
*p
, uint8_t instance
, struct nexthop_group
*nhg
)
260 struct zapi_route api
;
261 struct zapi_nexthop
*api_nh
;
265 memset(&api
, 0, sizeof(api
));
266 api
.vrf_id
= VRF_DEFAULT
;
267 api
.type
= ZEBRA_ROUTE_SHARP
;
268 api
.instance
= instance
;
269 api
.safi
= SAFI_UNICAST
;
270 memcpy(&api
.prefix
, p
, sizeof(*p
));
272 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
273 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
275 for (ALL_NEXTHOPS_PTR(nhg
, nh
)) {
276 api_nh
= &api
.nexthops
[i
];
277 api_nh
->vrf_id
= VRF_DEFAULT
;
278 api_nh
->type
= nh
->type
;
280 case NEXTHOP_TYPE_IPV4
:
281 api_nh
->gate
= nh
->gate
;
283 case NEXTHOP_TYPE_IPV4_IFINDEX
:
284 api_nh
->gate
= nh
->gate
;
285 api_nh
->ifindex
= nh
->ifindex
;
287 case NEXTHOP_TYPE_IFINDEX
:
288 api_nh
->ifindex
= nh
->ifindex
;
290 case NEXTHOP_TYPE_IPV6
:
291 memcpy(&api_nh
->gate
.ipv6
, &nh
->gate
.ipv6
, 16);
293 case NEXTHOP_TYPE_IPV6_IFINDEX
:
294 api_nh
->ifindex
= nh
->ifindex
;
295 memcpy(&api_nh
->gate
.ipv6
, &nh
->gate
.ipv6
, 16);
297 case NEXTHOP_TYPE_BLACKHOLE
:
298 api_nh
->bh_type
= nh
->bh_type
;
305 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
308 void route_delete(struct prefix
*p
, uint8_t instance
)
310 struct zapi_route api
;
312 memset(&api
, 0, sizeof(api
));
313 api
.vrf_id
= VRF_DEFAULT
;
314 api
.type
= ZEBRA_ROUTE_SHARP
;
315 api
.safi
= SAFI_UNICAST
;
316 api
.instance
= instance
;
317 memcpy(&api
.prefix
, p
, sizeof(*p
));
318 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
323 void sharp_zebra_nexthop_watch(struct prefix
*p
, bool import
,
324 bool watch
, bool connected
)
329 command
= ZEBRA_NEXTHOP_REGISTER
;
332 command
= ZEBRA_NEXTHOP_UNREGISTER
;
334 command
= ZEBRA_IMPORT_ROUTE_REGISTER
;
337 command
= ZEBRA_IMPORT_ROUTE_UNREGISTER
;
340 if (zclient_send_rnh(zclient
, command
, p
, connected
, VRF_DEFAULT
) < 0)
341 zlog_warn("%s: Failure to send nexthop to zebra",
342 __PRETTY_FUNCTION__
);
345 static int sharp_nexthop_update(int command
, struct zclient
*zclient
,
346 zebra_size_t length
, vrf_id_t vrf_id
)
348 struct sharp_nh_tracker
*nht
;
349 struct zapi_route nhr
;
350 char buf
[PREFIX_STRLEN
];
353 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &nhr
)) {
354 zlog_warn("%s: Decode of update failed", __PRETTY_FUNCTION__
);
359 zlog_debug("Received update for %s",
360 prefix2str(&nhr
.prefix
, buf
, sizeof(buf
)));
362 nht
= sharp_nh_tracker_get(&nhr
.prefix
);
363 nht
->nhop_num
= nhr
.nexthop_num
;
366 for (i
= 0; i
< nhr
.nexthop_num
; i
++) {
367 struct zapi_nexthop
*znh
= &nhr
.nexthops
[i
];
370 case NEXTHOP_TYPE_IPV4_IFINDEX
:
371 case NEXTHOP_TYPE_IPV4
:
373 "\tNexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d",
374 inet_ntop(AF_INET
, &znh
->gate
.ipv4
.s_addr
, buf
,
376 znh
->type
, znh
->ifindex
, znh
->vrf_id
,
379 case NEXTHOP_TYPE_IPV6_IFINDEX
:
380 case NEXTHOP_TYPE_IPV6
:
382 "\tNexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d",
383 inet_ntop(AF_INET6
, &znh
->gate
.ipv6
, buf
,
385 znh
->type
, znh
->ifindex
, znh
->vrf_id
,
388 case NEXTHOP_TYPE_IFINDEX
:
389 zlog_debug("\tNexthop IFINDEX: %d, ifindex: %d",
390 znh
->type
, znh
->ifindex
);
392 case NEXTHOP_TYPE_BLACKHOLE
:
393 zlog_debug("\tNexthop blackhole");
400 extern struct zebra_privs_t sharp_privs
;
402 void sharp_zebra_init(void)
404 struct zclient_options opt
= {.receive_notify
= true};
406 zclient
= zclient_new(master
, &opt
);
408 zclient_init(zclient
, ZEBRA_ROUTE_SHARP
, 0, &sharp_privs
);
409 zclient
->zebra_connected
= zebra_connected
;
410 zclient
->interface_add
= interface_add
;
411 zclient
->interface_delete
= interface_delete
;
412 zclient
->interface_up
= interface_state_up
;
413 zclient
->interface_down
= interface_state_down
;
414 zclient
->interface_address_add
= interface_address_add
;
415 zclient
->interface_address_delete
= interface_address_delete
;
416 zclient
->route_notify_owner
= route_notify_owner
;
417 zclient
->nexthop_update
= sharp_nexthop_update
;
418 zclient
->import_check_update
= sharp_nexthop_update
;