3 * Copyright (C) 2018 Cumulus Networks, Inc.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "srcdest_table.h"
36 #include "nexthop_group.h"
40 #include "static_vrf.h"
41 #include "static_routes.h"
42 #include "static_zebra.h"
43 #include "static_nht.h"
44 #include "static_vty.h"
45 #include "static_debug.h"
47 DEFINE_MTYPE_STATIC(STATIC
, STATIC_NHT_DATA
, "Static Nexthop tracking data");
48 PREDECL_HASH(static_nht_hash
);
50 struct static_nht_data
{
51 struct static_nht_hash_item itm
;
62 static int static_nht_data_cmp(const struct static_nht_data
*nhtd1
,
63 const struct static_nht_data
*nhtd2
)
65 if (nhtd1
->nh_vrf_id
!= nhtd2
->nh_vrf_id
)
66 return numcmp(nhtd1
->nh_vrf_id
, nhtd2
->nh_vrf_id
);
67 if (nhtd1
->safi
!= nhtd2
->safi
)
68 return numcmp(nhtd1
->safi
, nhtd2
->safi
);
70 return prefix_cmp(&nhtd1
->nh
, &nhtd2
->nh
);
73 static unsigned int static_nht_data_hash(const struct static_nht_data
*nhtd
)
77 key
= prefix_hash_key(&nhtd
->nh
);
78 return jhash_2words(nhtd
->nh_vrf_id
, nhtd
->safi
, key
);
81 DECLARE_HASH(static_nht_hash
, struct static_nht_data
, itm
, static_nht_data_cmp
,
82 static_nht_data_hash
);
84 static struct static_nht_hash_head static_nht_hash
[1];
86 /* Zebra structure to hold current status. */
87 struct zclient
*zclient
;
88 uint32_t zebra_ecmp_count
= MULTIPATH_NUM
;
90 /* Interface addition message from zebra. */
91 static int static_ifp_create(struct interface
*ifp
)
93 static_ifindex_update(ifp
, true);
98 static int static_ifp_destroy(struct interface
*ifp
)
100 static_ifindex_update(ifp
, false);
104 static int interface_address_add(ZAPI_CALLBACK_ARGS
)
106 zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
111 static int interface_address_delete(ZAPI_CALLBACK_ARGS
)
115 c
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
124 static int static_ifp_up(struct interface
*ifp
)
126 /* Install any static reliant on this interface coming up */
127 static_install_intf_nh(ifp
);
128 static_ifindex_update(ifp
, true);
133 static int static_ifp_down(struct interface
*ifp
)
135 static_ifindex_update(ifp
, false);
140 static int route_notify_owner(ZAPI_CALLBACK_ARGS
)
143 enum zapi_route_notify_owner note
;
147 if (!zapi_route_notify_decode(zclient
->ibuf
, &p
, &table_id
, ¬e
, NULL
,
152 case ZAPI_ROUTE_FAIL_INSTALL
:
153 static_nht_mark_state(&p
, safi
, vrf_id
, STATIC_NOT_INSTALLED
);
154 zlog_warn("%s: Route %pFX failed to install for table: %u",
155 __func__
, &p
, table_id
);
157 case ZAPI_ROUTE_BETTER_ADMIN_WON
:
158 static_nht_mark_state(&p
, safi
, vrf_id
, STATIC_NOT_INSTALLED
);
160 "%s: Route %pFX over-ridden by better route for table: %u",
161 __func__
, &p
, table_id
);
163 case ZAPI_ROUTE_INSTALLED
:
164 static_nht_mark_state(&p
, safi
, vrf_id
, STATIC_INSTALLED
);
166 case ZAPI_ROUTE_REMOVED
:
167 static_nht_mark_state(&p
, safi
, vrf_id
, STATIC_NOT_INSTALLED
);
169 case ZAPI_ROUTE_REMOVE_FAIL
:
170 static_nht_mark_state(&p
, safi
, vrf_id
, STATIC_INSTALLED
);
171 zlog_warn("%s: Route %pFX failure to remove for table: %u",
172 __func__
, &p
, table_id
);
178 static void zebra_connected(struct zclient
*zclient
)
180 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
183 /* API to check whether the configured nexthop address is
184 * one of its local connected address or not.
187 static_nexthop_is_local(vrf_id_t vrfid
, struct prefix
*addr
, int family
)
189 if (family
== AF_INET
) {
190 if (if_address_is_local(&addr
->u
.prefix4
, AF_INET
, vrfid
))
192 } else if (family
== AF_INET6
) {
193 if (if_address_is_local(&addr
->u
.prefix6
, AF_INET6
, vrfid
))
198 static int static_zebra_nexthop_update(ZAPI_CALLBACK_ARGS
)
200 struct static_nht_data
*nhtd
, lookup
;
201 struct zapi_route nhr
;
202 struct prefix matched
;
205 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &matched
, &nhr
)) {
206 zlog_err("Failure to decode nexthop update message");
210 if (matched
.family
== AF_INET6
)
213 if (nhr
.type
== ZEBRA_ROUTE_CONNECT
) {
214 if (static_nexthop_is_local(vrf_id
, &matched
,
219 memset(&lookup
, 0, sizeof(lookup
));
221 lookup
.nh_vrf_id
= vrf_id
;
222 lookup
.safi
= nhr
.safi
;
224 nhtd
= static_nht_hash_find(static_nht_hash
, &lookup
);
227 nhtd
->nh_num
= nhr
.nexthop_num
;
229 static_nht_reset_start(&matched
, afi
, nhr
.safi
,
231 static_nht_update(NULL
, &matched
, nhr
.nexthop_num
, afi
,
232 nhr
.safi
, nhtd
->nh_vrf_id
);
234 zlog_err("No nhtd?");
239 static void static_zebra_capabilities(struct zclient_capabilities
*cap
)
241 mpls_enabled
= cap
->mpls_enabled
;
242 zebra_ecmp_count
= cap
->ecmp
;
245 static struct static_nht_data
*
246 static_nht_hash_getref(const struct static_nht_data
*ref
)
248 struct static_nht_data
*nhtd
;
250 nhtd
= static_nht_hash_find(static_nht_hash
, ref
);
252 nhtd
= XCALLOC(MTYPE_STATIC_NHT_DATA
, sizeof(*nhtd
));
254 prefix_copy(&nhtd
->nh
, &ref
->nh
);
255 nhtd
->nh_vrf_id
= ref
->nh_vrf_id
;
256 nhtd
->safi
= ref
->safi
;
258 static_nht_hash_add(static_nht_hash
, nhtd
);
265 static bool static_nht_hash_decref(struct static_nht_data
*nhtd
)
267 if (--nhtd
->refcount
> 0)
270 static_nht_hash_del(static_nht_hash
, nhtd
);
271 XFREE(MTYPE_STATIC_NHT_DATA
, nhtd
);
275 static void static_nht_hash_clear(void)
277 struct static_nht_data
*nhtd
;
279 while ((nhtd
= static_nht_hash_pop(static_nht_hash
)))
280 XFREE(MTYPE_STATIC_NHT_DATA
, nhtd
);
283 static bool static_zebra_nht_get_prefix(const struct static_nexthop
*nh
,
288 case STATIC_BLACKHOLE
:
289 p
->family
= AF_UNSPEC
;
292 case STATIC_IPV4_GATEWAY
:
293 case STATIC_IPV4_GATEWAY_IFNAME
:
295 p
->prefixlen
= IPV4_MAX_BITLEN
;
296 p
->u
.prefix4
= nh
->addr
.ipv4
;
299 case STATIC_IPV6_GATEWAY
:
300 case STATIC_IPV6_GATEWAY_IFNAME
:
301 p
->family
= AF_INET6
;
302 p
->prefixlen
= IPV6_MAX_BITLEN
;
303 p
->u
.prefix6
= nh
->addr
.ipv6
;
307 assertf(0, "BUG: someone forgot to add nexthop type %u", nh
->type
);
310 void static_zebra_nht_register(struct static_nexthop
*nh
, bool reg
)
312 struct static_path
*pn
= nh
->pn
;
313 struct route_node
*rn
= pn
->rn
;
314 struct static_route_info
*si
= static_route_info_from_rnode(rn
);
315 struct static_nht_data lookup
= {};
319 ZEBRA_NEXTHOP_REGISTER
: ZEBRA_NEXTHOP_UNREGISTER
;
321 if (nh
->nh_registered
&& reg
)
324 if (!nh
->nh_registered
&& !reg
)
327 if (!static_zebra_nht_get_prefix(nh
, &lookup
.nh
))
329 lookup
.nh_vrf_id
= nh
->nh_vrf_id
;
330 lookup
.safi
= si
->safi
;
332 nh
->nh_registered
= reg
;
335 struct static_nht_data
*nhtd
;
337 nhtd
= static_nht_hash_getref(&lookup
);
339 if (nhtd
->refcount
> 1) {
340 DEBUGD(&static_dbg_route
,
341 "Already registered nexthop(%pFX) for %pRN %d",
342 &lookup
.nh
, rn
, nhtd
->nh_num
);
344 afi_t afi
= prefix_afi(&lookup
.nh
);
346 static_nht_update(&rn
->p
, &nhtd
->nh
,
347 nhtd
->nh_num
, afi
, si
->safi
,
353 struct static_nht_data
*nhtd
;
355 nhtd
= static_nht_hash_find(static_nht_hash
, &lookup
);
358 if (static_nht_hash_decref(nhtd
))
362 DEBUGD(&static_dbg_route
, "%s nexthop(%pFX) for %pRN",
363 reg
? "Registering" : "Unregistering", &lookup
.nh
, rn
);
365 if (zclient_send_rnh(zclient
, cmd
, &lookup
.nh
, si
->safi
, false, false,
366 nh
->nh_vrf_id
) == ZCLIENT_SEND_FAILURE
)
367 zlog_warn("%s: Failure to send nexthop to zebra", __func__
);
370 * When nexthop gets updated via configuration then use the
371 * already registered NH and resend the route to zebra
373 int static_zebra_nh_update(struct static_nexthop
*nh
)
375 struct static_path
*pn
= nh
->pn
;
376 struct route_node
*rn
= pn
->rn
;
377 struct static_route_info
*si
= static_route_info_from_rnode(rn
);
378 struct static_nht_data
*nhtd
, lookup
= {};
380 if (!nh
->nh_registered
)
383 if (!static_zebra_nht_get_prefix(nh
, &lookup
.nh
))
385 lookup
.nh_vrf_id
= nh
->nh_vrf_id
;
386 lookup
.safi
= si
->safi
;
388 nhtd
= static_nht_hash_find(static_nht_hash
, &lookup
);
389 if (nhtd
&& nhtd
->nh_num
) {
390 afi_t afi
= prefix_afi(&lookup
.nh
);
392 nh
->state
= STATIC_START
;
393 static_nht_update(&rn
->p
, &nhtd
->nh
, nhtd
->nh_num
, afi
,
394 si
->safi
, nh
->nh_vrf_id
);
400 extern void static_zebra_route_add(struct static_path
*pn
, bool install
)
402 struct route_node
*rn
= pn
->rn
;
403 struct static_route_info
*si
= rn
->info
;
404 struct static_nexthop
*nh
;
405 const struct prefix
*p
, *src_pp
;
406 struct zapi_nexthop
*api_nh
;
407 struct zapi_route api
;
411 srcdest_rnode_prefixes(rn
, &p
, &src_pp
);
413 memset(&api
, 0, sizeof(api
));
414 api
.vrf_id
= si
->svrf
->vrf
->vrf_id
;
415 api
.type
= ZEBRA_ROUTE_STATIC
;
417 memcpy(&api
.prefix
, p
, sizeof(api
.prefix
));
420 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
421 memcpy(&api
.src_prefix
, src_pp
, sizeof(api
.src_prefix
));
423 SET_FLAG(api
.flags
, ZEBRA_FLAG_RR_USE_DISTANCE
);
424 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
425 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
427 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
428 api
.distance
= pn
->distance
;
431 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
434 if (pn
->table_id
!= 0) {
435 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
436 api
.tableid
= pn
->table_id
;
438 frr_each(static_nexthop_list
, &pn
->nexthop_list
, nh
) {
439 /* Don't overrun the nexthop array */
440 if (nh_num
== zebra_ecmp_count
)
443 api_nh
= &api
.nexthops
[nh_num
];
444 if (nh
->nh_vrf_id
== VRF_UNKNOWN
)
447 api_nh
->vrf_id
= nh
->nh_vrf_id
;
449 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_ONLINK
);
450 if (nh
->color
!= 0) {
451 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRTE
);
452 api_nh
->srte_color
= nh
->color
;
455 nh
->state
= STATIC_SENT_TO_ZEBRA
;
459 if (nh
->ifindex
== IFINDEX_INTERNAL
)
461 api_nh
->ifindex
= nh
->ifindex
;
462 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
464 case STATIC_IPV4_GATEWAY
:
467 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
468 api_nh
->gate
= nh
->addr
;
470 case STATIC_IPV4_GATEWAY_IFNAME
:
471 if (nh
->ifindex
== IFINDEX_INTERNAL
)
473 api_nh
->ifindex
= nh
->ifindex
;
474 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
475 api_nh
->gate
= nh
->addr
;
477 case STATIC_IPV6_GATEWAY
:
480 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
481 api_nh
->gate
= nh
->addr
;
483 case STATIC_IPV6_GATEWAY_IFNAME
:
484 if (nh
->ifindex
== IFINDEX_INTERNAL
)
486 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
487 api_nh
->ifindex
= nh
->ifindex
;
488 api_nh
->gate
= nh
->addr
;
490 case STATIC_BLACKHOLE
:
491 api_nh
->type
= NEXTHOP_TYPE_BLACKHOLE
;
492 switch (nh
->bh_type
) {
493 case STATIC_BLACKHOLE_DROP
:
494 case STATIC_BLACKHOLE_NULL
:
495 api_nh
->bh_type
= BLACKHOLE_NULL
;
497 case STATIC_BLACKHOLE_REJECT
:
498 api_nh
->bh_type
= BLACKHOLE_REJECT
;
503 if (nh
->snh_label
.num_labels
) {
506 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
);
507 api_nh
->label_num
= nh
->snh_label
.num_labels
;
508 for (i
= 0; i
< api_nh
->label_num
; i
++)
509 api_nh
->labels
[i
] = nh
->snh_label
.label
[i
];
514 api
.nexthop_num
= nh_num
;
517 * If we have been given an install but nothing is valid
518 * go ahead and delete the route for double plus fun
520 if (!nh_num
&& install
)
523 zclient_route_send(install
?
524 ZEBRA_ROUTE_ADD
: ZEBRA_ROUTE_DELETE
,
528 static zclient_handler
*const static_handlers
[] = {
529 [ZEBRA_INTERFACE_ADDRESS_ADD
] = interface_address_add
,
530 [ZEBRA_INTERFACE_ADDRESS_DELETE
] = interface_address_delete
,
531 [ZEBRA_ROUTE_NOTIFY_OWNER
] = route_notify_owner
,
532 [ZEBRA_NEXTHOP_UPDATE
] = static_zebra_nexthop_update
,
535 void static_zebra_init(void)
537 struct zclient_options opt
= { .receive_notify
= true };
539 if_zapi_callbacks(static_ifp_create
, static_ifp_up
,
540 static_ifp_down
, static_ifp_destroy
);
542 zclient
= zclient_new(master
, &opt
, static_handlers
,
543 array_size(static_handlers
));
545 zclient_init(zclient
, ZEBRA_ROUTE_STATIC
, 0, &static_privs
);
546 zclient
->zebra_capabilities
= static_zebra_capabilities
;
547 zclient
->zebra_connected
= zebra_connected
;
549 static_nht_hash_init(static_nht_hash
);
552 /* static_zebra_stop used by tests/lib/test_grpc.cpp */
553 void static_zebra_stop(void)
555 static_nht_hash_clear();
556 static_nht_hash_fini(static_nht_hash
);
560 zclient_stop(zclient
);
561 zclient_free(zclient
);
565 void static_zebra_vrf_register(struct vrf
*vrf
)
567 if (vrf
->vrf_id
== VRF_DEFAULT
)
569 zclient_send_reg_requests(zclient
, vrf
->vrf_id
);
572 void static_zebra_vrf_unregister(struct vrf
*vrf
)
574 if (vrf
->vrf_id
== VRF_DEFAULT
)
576 zclient_send_dereg_requests(zclient
, vrf
->vrf_id
);