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 sharp_ifp_destroy(struct interface
*ifp
)
60 static int interface_address_add(ZAPI_CALLBACK_ARGS
)
62 zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
67 static int interface_address_delete(ZAPI_CALLBACK_ARGS
)
71 c
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
80 static int sharp_ifp_up(struct interface
*ifp
)
85 static int sharp_ifp_down(struct interface
*ifp
)
90 void sharp_install_routes_helper(struct prefix
*p
, vrf_id_t vrf_id
,
91 uint8_t instance
, struct nexthop_group
*nhg
,
97 zlog_debug("Inserting %u routes", routes
);
99 if (p
->family
== AF_INET
) {
101 temp
= ntohl(p
->u
.prefix4
.s_addr
);
103 temp
= ntohl(p
->u
.val32
[3]);
105 monotime(&sg
.r
.t_start
);
106 for (i
= 0; i
< routes
; i
++) {
107 route_add(p
, vrf_id
, (uint8_t)instance
, nhg
);
109 p
->u
.prefix4
.s_addr
= htonl(++temp
);
111 p
->u
.val32
[3] = htonl(++temp
);
115 void sharp_remove_routes_helper(struct prefix
*p
, vrf_id_t vrf_id
,
116 uint8_t instance
, uint32_t routes
)
121 zlog_debug("Removing %u routes", routes
);
123 if (p
->family
== AF_INET
) {
125 temp
= ntohl(p
->u
.prefix4
.s_addr
);
127 temp
= ntohl(p
->u
.val32
[3]);
129 monotime(&sg
.r
.t_start
);
130 for (i
= 0; i
< routes
; i
++) {
131 route_delete(p
, vrf_id
, (uint8_t)instance
);
133 p
->u
.prefix4
.s_addr
= htonl(++temp
);
135 p
->u
.val32
[3] = htonl(++temp
);
139 static void handle_repeated(bool installed
)
141 struct prefix p
= sg
.r
.orig_prefix
;
144 if (sg
.r
.repeat
<= 0)
148 sg
.r
.removed_routes
= 0;
149 sharp_remove_routes_helper(&p
, sg
.r
.vrf_id
,
150 sg
.r
.inst
, sg
.r
.total_routes
);
154 sg
.r
.installed_routes
= 0;
155 sharp_install_routes_helper(&p
, sg
.r
.vrf_id
, sg
.r
.inst
,
161 static int route_notify_owner(ZAPI_CALLBACK_ARGS
)
165 enum zapi_route_notify_owner note
;
168 if (!zapi_route_notify_decode(zclient
->ibuf
, &p
, &table
, ¬e
))
172 case ZAPI_ROUTE_INSTALLED
:
173 sg
.r
.installed_routes
++;
174 if (sg
.r
.total_routes
== sg
.r
.installed_routes
) {
175 monotime(&sg
.r
.t_end
);
176 timersub(&sg
.r
.t_end
, &sg
.r
.t_start
, &r
);
177 zlog_debug("Installed All Items %jd.%ld",
178 (intmax_t)r
.tv_sec
, (long)r
.tv_usec
);
179 handle_repeated(true);
182 case ZAPI_ROUTE_FAIL_INSTALL
:
183 zlog_debug("Failed install of route");
185 case ZAPI_ROUTE_BETTER_ADMIN_WON
:
186 zlog_debug("Better Admin Distance won over us");
188 case ZAPI_ROUTE_REMOVED
:
189 sg
.r
.removed_routes
++;
190 if (sg
.r
.total_routes
== sg
.r
.removed_routes
) {
191 monotime(&sg
.r
.t_end
);
192 timersub(&sg
.r
.t_end
, &sg
.r
.t_start
, &r
);
193 zlog_debug("Removed all Items %jd.%ld",
194 (intmax_t)r
.tv_sec
, (long)r
.tv_usec
);
195 handle_repeated(false);
198 case ZAPI_ROUTE_REMOVE_FAIL
:
199 zlog_debug("Route removal Failure");
205 static void zebra_connected(struct zclient
*zclient
)
207 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
210 * Do not actually turn this on yet
211 * This is just the start of the infrastructure needed here
212 * This can be fixed at a later time.
214 * zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP,
215 * ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT);
219 void vrf_label_add(vrf_id_t vrf_id
, afi_t afi
, mpls_label_t label
)
221 zclient_send_vrf_label(zclient
, vrf_id
, afi
, label
, ZEBRA_LSP_SHARP
);
224 void route_add(struct prefix
*p
, vrf_id_t vrf_id
,
225 uint8_t instance
, struct nexthop_group
*nhg
)
227 struct zapi_route api
;
228 struct zapi_nexthop
*api_nh
;
232 memset(&api
, 0, sizeof(api
));
234 api
.type
= ZEBRA_ROUTE_SHARP
;
235 api
.instance
= instance
;
236 api
.safi
= SAFI_UNICAST
;
237 memcpy(&api
.prefix
, p
, sizeof(*p
));
239 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
240 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
242 for (ALL_NEXTHOPS_PTR(nhg
, nh
)) {
243 api_nh
= &api
.nexthops
[i
];
244 api_nh
->vrf_id
= nh
->vrf_id
;
245 api_nh
->type
= nh
->type
;
247 case NEXTHOP_TYPE_IPV4
:
248 api_nh
->gate
= nh
->gate
;
250 case NEXTHOP_TYPE_IPV4_IFINDEX
:
251 api_nh
->gate
= nh
->gate
;
252 api_nh
->ifindex
= nh
->ifindex
;
254 case NEXTHOP_TYPE_IFINDEX
:
255 api_nh
->ifindex
= nh
->ifindex
;
257 case NEXTHOP_TYPE_IPV6
:
258 memcpy(&api_nh
->gate
.ipv6
, &nh
->gate
.ipv6
, 16);
260 case NEXTHOP_TYPE_IPV6_IFINDEX
:
261 api_nh
->ifindex
= nh
->ifindex
;
262 memcpy(&api_nh
->gate
.ipv6
, &nh
->gate
.ipv6
, 16);
264 case NEXTHOP_TYPE_BLACKHOLE
:
265 api_nh
->bh_type
= nh
->bh_type
;
272 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
275 void route_delete(struct prefix
*p
, vrf_id_t vrf_id
, uint8_t instance
)
277 struct zapi_route api
;
279 memset(&api
, 0, sizeof(api
));
281 api
.type
= ZEBRA_ROUTE_SHARP
;
282 api
.safi
= SAFI_UNICAST
;
283 api
.instance
= instance
;
284 memcpy(&api
.prefix
, p
, sizeof(*p
));
285 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
290 void sharp_zebra_nexthop_watch(struct prefix
*p
, vrf_id_t vrf_id
, bool import
,
291 bool watch
, bool connected
)
296 command
= ZEBRA_NEXTHOP_REGISTER
;
299 command
= ZEBRA_NEXTHOP_UNREGISTER
;
301 command
= ZEBRA_IMPORT_ROUTE_REGISTER
;
304 command
= ZEBRA_IMPORT_ROUTE_UNREGISTER
;
307 if (zclient_send_rnh(zclient
, command
, p
, connected
, vrf_id
) < 0)
308 zlog_warn("%s: Failure to send nexthop to zebra",
309 __PRETTY_FUNCTION__
);
312 static int sharp_debug_nexthops(struct zapi_route
*api
)
315 char buf
[PREFIX_STRLEN
];
317 for (i
= 0; i
< api
->nexthop_num
; i
++) {
318 struct zapi_nexthop
*znh
= &api
->nexthops
[i
];
321 case NEXTHOP_TYPE_IPV4_IFINDEX
:
322 case NEXTHOP_TYPE_IPV4
:
324 "\tNexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d",
325 inet_ntop(AF_INET
, &znh
->gate
.ipv4
.s_addr
, buf
,
327 znh
->type
, znh
->ifindex
, znh
->vrf_id
,
330 case NEXTHOP_TYPE_IPV6_IFINDEX
:
331 case NEXTHOP_TYPE_IPV6
:
333 "\tNexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d",
334 inet_ntop(AF_INET6
, &znh
->gate
.ipv6
, buf
,
336 znh
->type
, znh
->ifindex
, znh
->vrf_id
,
339 case NEXTHOP_TYPE_IFINDEX
:
340 zlog_debug("\tNexthop IFINDEX: %d, ifindex: %d",
341 znh
->type
, znh
->ifindex
);
343 case NEXTHOP_TYPE_BLACKHOLE
:
344 zlog_debug("\tNexthop blackhole");
351 static int sharp_nexthop_update(ZAPI_CALLBACK_ARGS
)
353 struct sharp_nh_tracker
*nht
;
354 struct zapi_route nhr
;
356 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &nhr
)) {
357 zlog_warn("%s: Decode of update failed", __PRETTY_FUNCTION__
);
362 zlog_debug("Received update for %pFX", &nhr
.prefix
);
364 nht
= sharp_nh_tracker_get(&nhr
.prefix
);
365 nht
->nhop_num
= nhr
.nexthop_num
;
368 sharp_debug_nexthops(&nhr
);
373 static int sharp_redistribute_route(ZAPI_CALLBACK_ARGS
)
375 struct zapi_route api
;
377 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
378 zlog_warn("%s: Decode of redistribute failed: %d",
380 ZEBRA_REDISTRIBUTE_ROUTE_ADD
);
382 zlog_debug("%s: %pFX (%s)", zserv_command_string(cmd
),
383 &api
.prefix
, zebra_route_string(api
.type
));
385 sharp_debug_nexthops(&api
);
390 extern struct zebra_privs_t sharp_privs
;
392 void sharp_zebra_init(void)
394 struct zclient_options opt
= {.receive_notify
= true};
396 if_zapi_callbacks(sharp_ifp_create
, sharp_ifp_up
,
397 sharp_ifp_down
, sharp_ifp_destroy
);
399 zclient
= zclient_new(master
, &opt
);
401 zclient_init(zclient
, ZEBRA_ROUTE_SHARP
, 0, &sharp_privs
);
402 zclient
->zebra_connected
= zebra_connected
;
403 zclient
->interface_address_add
= interface_address_add
;
404 zclient
->interface_address_delete
= interface_address_delete
;
405 zclient
->route_notify_owner
= route_notify_owner
;
406 zclient
->nexthop_update
= sharp_nexthop_update
;
407 zclient
->import_check_update
= sharp_nexthop_update
;
409 zclient
->redistribute_route_add
= sharp_redistribute_route
;
410 zclient
->redistribute_route_del
= sharp_redistribute_route
;