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(ZAPI_CALLBACK_ARGS
)
63 struct interface
*ifp
;
65 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
73 static int interface_delete(ZAPI_CALLBACK_ARGS
)
75 struct interface
*ifp
;
79 /* zebra_interface_state_read () updates interface structure in iflist
81 ifp
= zebra_interface_state_read(s
, vrf_id
);
86 if_set_index(ifp
, IFINDEX_INTERNAL
);
91 static int interface_address_add(ZAPI_CALLBACK_ARGS
)
94 zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
99 static int interface_address_delete(ZAPI_CALLBACK_ARGS
)
103 c
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
112 static int interface_state_up(ZAPI_CALLBACK_ARGS
)
115 zebra_interface_if_lookup(zclient
->ibuf
);
120 static int interface_state_down(ZAPI_CALLBACK_ARGS
)
123 zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
128 void sharp_install_routes_helper(struct prefix
*p
, vrf_id_t vrf_id
,
129 uint8_t instance
, struct nexthop_group
*nhg
,
135 zlog_debug("Inserting %u routes", routes
);
137 if (p
->family
== AF_INET
) {
139 temp
= ntohl(p
->u
.prefix4
.s_addr
);
141 temp
= ntohl(p
->u
.val32
[3]);
143 monotime(&sg
.r
.t_start
);
144 for (i
= 0; i
< routes
; i
++) {
145 route_add(p
, vrf_id
, (uint8_t)instance
, nhg
);
147 p
->u
.prefix4
.s_addr
= htonl(++temp
);
149 p
->u
.val32
[3] = htonl(++temp
);
153 void sharp_remove_routes_helper(struct prefix
*p
, vrf_id_t vrf_id
,
154 uint8_t instance
, uint32_t routes
)
159 zlog_debug("Removing %u routes", routes
);
161 if (p
->family
== AF_INET
) {
163 temp
= ntohl(p
->u
.prefix4
.s_addr
);
165 temp
= ntohl(p
->u
.val32
[3]);
167 monotime(&sg
.r
.t_start
);
168 for (i
= 0; i
< routes
; i
++) {
169 route_delete(p
, vrf_id
, (uint8_t)instance
);
171 p
->u
.prefix4
.s_addr
= htonl(++temp
);
173 p
->u
.val32
[3] = htonl(++temp
);
177 static void handle_repeated(bool installed
)
179 struct prefix p
= sg
.r
.orig_prefix
;
182 if (sg
.r
.repeat
<= 0)
186 sg
.r
.removed_routes
= 0;
187 sharp_remove_routes_helper(&p
, sg
.r
.vrf_id
,
188 sg
.r
.inst
, sg
.r
.total_routes
);
192 sg
.r
.installed_routes
= 0;
193 sharp_install_routes_helper(&p
, sg
.r
.vrf_id
, sg
.r
.inst
,
199 static int route_notify_owner(ZAPI_CALLBACK_ARGS
)
203 enum zapi_route_notify_owner note
;
206 if (!zapi_route_notify_decode(zclient
->ibuf
, &p
, &table
, ¬e
))
210 case ZAPI_ROUTE_INSTALLED
:
211 sg
.r
.installed_routes
++;
212 if (sg
.r
.total_routes
== sg
.r
.installed_routes
) {
213 monotime(&sg
.r
.t_end
);
214 timersub(&sg
.r
.t_end
, &sg
.r
.t_start
, &r
);
215 zlog_debug("Installed All Items %ld.%ld", r
.tv_sec
,
216 (long int)r
.tv_usec
);
217 handle_repeated(true);
220 case ZAPI_ROUTE_FAIL_INSTALL
:
221 zlog_debug("Failed install of route");
223 case ZAPI_ROUTE_BETTER_ADMIN_WON
:
224 zlog_debug("Better Admin Distance won over us");
226 case ZAPI_ROUTE_REMOVED
:
227 sg
.r
.removed_routes
++;
228 if (sg
.r
.total_routes
== sg
.r
.removed_routes
) {
229 monotime(&sg
.r
.t_end
);
230 timersub(&sg
.r
.t_end
, &sg
.r
.t_start
, &r
);
231 zlog_debug("Removed all Items %ld.%ld", r
.tv_sec
,
232 (long int)r
.tv_usec
);
233 handle_repeated(false);
236 case ZAPI_ROUTE_REMOVE_FAIL
:
237 zlog_debug("Route removal Failure");
243 static void zebra_connected(struct zclient
*zclient
)
245 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
248 void vrf_label_add(vrf_id_t vrf_id
, afi_t afi
, mpls_label_t label
)
250 zclient_send_vrf_label(zclient
, vrf_id
, afi
, label
, ZEBRA_LSP_SHARP
);
253 void route_add(struct prefix
*p
, vrf_id_t vrf_id
,
254 uint8_t instance
, struct nexthop_group
*nhg
)
256 struct zapi_route api
;
257 struct zapi_nexthop
*api_nh
;
261 memset(&api
, 0, sizeof(api
));
263 api
.type
= ZEBRA_ROUTE_SHARP
;
264 api
.instance
= instance
;
265 api
.safi
= SAFI_UNICAST
;
266 memcpy(&api
.prefix
, p
, sizeof(*p
));
268 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
269 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
271 for (ALL_NEXTHOPS_PTR(nhg
, nh
)) {
272 api_nh
= &api
.nexthops
[i
];
273 api_nh
->vrf_id
= nh
->vrf_id
;
274 api_nh
->type
= nh
->type
;
276 case NEXTHOP_TYPE_IPV4
:
277 api_nh
->gate
= nh
->gate
;
279 case NEXTHOP_TYPE_IPV4_IFINDEX
:
280 api_nh
->gate
= nh
->gate
;
281 api_nh
->ifindex
= nh
->ifindex
;
283 case NEXTHOP_TYPE_IFINDEX
:
284 api_nh
->ifindex
= nh
->ifindex
;
286 case NEXTHOP_TYPE_IPV6
:
287 memcpy(&api_nh
->gate
.ipv6
, &nh
->gate
.ipv6
, 16);
289 case NEXTHOP_TYPE_IPV6_IFINDEX
:
290 api_nh
->ifindex
= nh
->ifindex
;
291 memcpy(&api_nh
->gate
.ipv6
, &nh
->gate
.ipv6
, 16);
293 case NEXTHOP_TYPE_BLACKHOLE
:
294 api_nh
->bh_type
= nh
->bh_type
;
301 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
304 void 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
));
314 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
319 void sharp_zebra_nexthop_watch(struct prefix
*p
, vrf_id_t vrf_id
, bool import
,
320 bool watch
, bool connected
)
325 command
= ZEBRA_NEXTHOP_REGISTER
;
328 command
= ZEBRA_NEXTHOP_UNREGISTER
;
330 command
= ZEBRA_IMPORT_ROUTE_REGISTER
;
333 command
= ZEBRA_IMPORT_ROUTE_UNREGISTER
;
336 if (zclient_send_rnh(zclient
, command
, p
, connected
, vrf_id
) < 0)
337 zlog_warn("%s: Failure to send nexthop to zebra",
338 __PRETTY_FUNCTION__
);
341 static int sharp_nexthop_update(ZAPI_CALLBACK_ARGS
)
343 struct sharp_nh_tracker
*nht
;
344 struct zapi_route nhr
;
345 char buf
[PREFIX_STRLEN
];
348 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &nhr
)) {
349 zlog_warn("%s: Decode of update failed", __PRETTY_FUNCTION__
);
354 zlog_debug("Received update for %s",
355 prefix2str(&nhr
.prefix
, buf
, sizeof(buf
)));
357 nht
= sharp_nh_tracker_get(&nhr
.prefix
);
358 nht
->nhop_num
= nhr
.nexthop_num
;
361 for (i
= 0; i
< nhr
.nexthop_num
; i
++) {
362 struct zapi_nexthop
*znh
= &nhr
.nexthops
[i
];
365 case NEXTHOP_TYPE_IPV4_IFINDEX
:
366 case NEXTHOP_TYPE_IPV4
:
368 "\tNexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d",
369 inet_ntop(AF_INET
, &znh
->gate
.ipv4
.s_addr
, buf
,
371 znh
->type
, znh
->ifindex
, znh
->vrf_id
,
374 case NEXTHOP_TYPE_IPV6_IFINDEX
:
375 case NEXTHOP_TYPE_IPV6
:
377 "\tNexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d",
378 inet_ntop(AF_INET6
, &znh
->gate
.ipv6
, buf
,
380 znh
->type
, znh
->ifindex
, znh
->vrf_id
,
383 case NEXTHOP_TYPE_IFINDEX
:
384 zlog_debug("\tNexthop IFINDEX: %d, ifindex: %d",
385 znh
->type
, znh
->ifindex
);
387 case NEXTHOP_TYPE_BLACKHOLE
:
388 zlog_debug("\tNexthop blackhole");
395 extern struct zebra_privs_t sharp_privs
;
397 void sharp_zebra_init(void)
399 struct zclient_options opt
= {.receive_notify
= true};
401 zclient
= zclient_new(master
, &opt
);
403 zclient_init(zclient
, ZEBRA_ROUTE_SHARP
, 0, &sharp_privs
);
404 zclient
->zebra_connected
= zebra_connected
;
405 zclient
->interface_add
= interface_add
;
406 zclient
->interface_delete
= interface_delete
;
407 zclient
->interface_up
= interface_state_up
;
408 zclient
->interface_down
= interface_state_down
;
409 zclient
->interface_address_add
= interface_address_add
;
410 zclient
->interface_address_delete
= interface_address_delete
;
411 zclient
->route_notify_owner
= route_notify_owner
;
412 zclient
->nexthop_update
= sharp_nexthop_update
;
413 zclient
->import_check_update
= sharp_nexthop_update
;