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
;
63 static int static_nht_data_cmp(const struct static_nht_data
*nhtd1
,
64 const struct static_nht_data
*nhtd2
)
66 if (nhtd1
->nh_vrf_id
!= nhtd2
->nh_vrf_id
)
67 return numcmp(nhtd1
->nh_vrf_id
, nhtd2
->nh_vrf_id
);
68 if (nhtd1
->safi
!= nhtd2
->safi
)
69 return numcmp(nhtd1
->safi
, nhtd2
->safi
);
71 return prefix_cmp(&nhtd1
->nh
, &nhtd2
->nh
);
74 static unsigned int static_nht_data_hash(const struct static_nht_data
*nhtd
)
78 key
= prefix_hash_key(&nhtd
->nh
);
79 return jhash_2words(nhtd
->nh_vrf_id
, nhtd
->safi
, key
);
82 DECLARE_HASH(static_nht_hash
, struct static_nht_data
, itm
, static_nht_data_cmp
,
83 static_nht_data_hash
);
85 static struct static_nht_hash_head static_nht_hash
[1];
87 /* Zebra structure to hold current status. */
88 struct zclient
*zclient
;
89 uint32_t zebra_ecmp_count
= MULTIPATH_NUM
;
91 /* Interface addition message from zebra. */
92 static int static_ifp_create(struct interface
*ifp
)
94 static_ifindex_update(ifp
, true);
99 static int static_ifp_destroy(struct interface
*ifp
)
101 static_ifindex_update(ifp
, false);
105 static int interface_address_add(ZAPI_CALLBACK_ARGS
)
107 zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
112 static int interface_address_delete(ZAPI_CALLBACK_ARGS
)
116 c
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
125 static int static_ifp_up(struct interface
*ifp
)
127 /* Install any static reliant on this interface coming up */
128 static_install_intf_nh(ifp
);
129 static_ifindex_update(ifp
, true);
134 static int static_ifp_down(struct interface
*ifp
)
136 static_ifindex_update(ifp
, false);
141 static int route_notify_owner(ZAPI_CALLBACK_ARGS
)
144 enum zapi_route_notify_owner note
;
148 if (!zapi_route_notify_decode(zclient
->ibuf
, &p
, &table_id
, ¬e
, NULL
,
153 case ZAPI_ROUTE_FAIL_INSTALL
:
154 static_nht_mark_state(&p
, safi
, vrf_id
, STATIC_NOT_INSTALLED
);
155 zlog_warn("%s: Route %pFX failed to install for table: %u",
156 __func__
, &p
, table_id
);
158 case ZAPI_ROUTE_BETTER_ADMIN_WON
:
159 static_nht_mark_state(&p
, safi
, vrf_id
, STATIC_NOT_INSTALLED
);
161 "%s: Route %pFX over-ridden by better route for table: %u",
162 __func__
, &p
, table_id
);
164 case ZAPI_ROUTE_INSTALLED
:
165 static_nht_mark_state(&p
, safi
, vrf_id
, STATIC_INSTALLED
);
167 case ZAPI_ROUTE_REMOVED
:
168 static_nht_mark_state(&p
, safi
, vrf_id
, STATIC_NOT_INSTALLED
);
170 case ZAPI_ROUTE_REMOVE_FAIL
:
171 static_nht_mark_state(&p
, safi
, vrf_id
, STATIC_INSTALLED
);
172 zlog_warn("%s: Route %pFX failure to remove for table: %u",
173 __func__
, &p
, table_id
);
180 static void zebra_connected(struct zclient
*zclient
)
182 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
184 static_fixup_vrf_ids(vrf_info_lookup(VRF_DEFAULT
));
187 /* API to check whether the configured nexthop address is
188 * one of its local connected address or not.
191 static_nexthop_is_local(vrf_id_t vrfid
, struct prefix
*addr
, int family
)
193 if (family
== AF_INET
) {
194 if (if_address_is_local(&addr
->u
.prefix4
, AF_INET
, vrfid
))
196 } else if (family
== AF_INET6
) {
197 if (if_address_is_local(&addr
->u
.prefix6
, AF_INET6
, vrfid
))
202 static int static_zebra_nexthop_update(ZAPI_CALLBACK_ARGS
)
204 struct static_nht_data
*nhtd
, lookup
;
205 struct zapi_route nhr
;
206 struct prefix matched
;
209 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &matched
, &nhr
)) {
210 zlog_err("Failure to decode nexthop update message");
214 if (matched
.family
== AF_INET6
)
217 if (nhr
.type
== ZEBRA_ROUTE_CONNECT
) {
218 if (static_nexthop_is_local(vrf_id
, &matched
,
223 memset(&lookup
, 0, sizeof(lookup
));
225 lookup
.nh_vrf_id
= vrf_id
;
226 lookup
.safi
= nhr
.safi
;
228 nhtd
= static_nht_hash_find(static_nht_hash
, &lookup
);
231 nhtd
->nh_num
= nhr
.nexthop_num
;
233 static_nht_reset_start(&matched
, afi
, nhr
.safi
,
235 static_nht_update(NULL
, &matched
, nhr
.nexthop_num
, afi
,
236 nhr
.safi
, nhtd
->nh_vrf_id
);
238 zlog_err("No nhtd?");
243 static void static_zebra_capabilities(struct zclient_capabilities
*cap
)
245 mpls_enabled
= cap
->mpls_enabled
;
246 zebra_ecmp_count
= cap
->ecmp
;
249 static struct static_nht_data
*
250 static_nht_hash_getref(const struct static_nht_data
*ref
)
252 struct static_nht_data
*nhtd
;
254 nhtd
= static_nht_hash_find(static_nht_hash
, ref
);
256 nhtd
= XCALLOC(MTYPE_STATIC_NHT_DATA
, sizeof(*nhtd
));
258 prefix_copy(&nhtd
->nh
, &ref
->nh
);
259 nhtd
->nh_vrf_id
= ref
->nh_vrf_id
;
260 nhtd
->safi
= ref
->safi
;
262 static_nht_hash_add(static_nht_hash
, nhtd
);
269 static bool static_nht_hash_decref(struct static_nht_data
**nhtd_p
)
271 struct static_nht_data
*nhtd
= *nhtd_p
;
275 if (--nhtd
->refcount
> 0)
278 static_nht_hash_del(static_nht_hash
, nhtd
);
279 XFREE(MTYPE_STATIC_NHT_DATA
, nhtd
);
283 static void static_nht_hash_clear(void)
285 struct static_nht_data
*nhtd
;
287 while ((nhtd
= static_nht_hash_pop(static_nht_hash
)))
288 XFREE(MTYPE_STATIC_NHT_DATA
, nhtd
);
291 static bool static_zebra_nht_get_prefix(const struct static_nexthop
*nh
,
296 case STATIC_BLACKHOLE
:
297 p
->family
= AF_UNSPEC
;
300 case STATIC_IPV4_GATEWAY
:
301 case STATIC_IPV4_GATEWAY_IFNAME
:
303 p
->prefixlen
= IPV4_MAX_BITLEN
;
304 p
->u
.prefix4
= nh
->addr
.ipv4
;
307 case STATIC_IPV6_GATEWAY
:
308 case STATIC_IPV6_GATEWAY_IFNAME
:
309 p
->family
= AF_INET6
;
310 p
->prefixlen
= IPV6_MAX_BITLEN
;
311 p
->u
.prefix6
= nh
->addr
.ipv6
;
315 assertf(0, "BUG: someone forgot to add nexthop type %u", nh
->type
);
319 void static_zebra_nht_register(struct static_nexthop
*nh
, bool reg
)
321 struct static_path
*pn
= nh
->pn
;
322 struct route_node
*rn
= pn
->rn
;
323 struct static_route_info
*si
= static_route_info_from_rnode(rn
);
324 struct static_nht_data
*nhtd
, lookup
= {};
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 if (nh
->nh_registered
) {
333 /* nh->nh_registered means we own a reference on the nhtd */
334 nhtd
= static_nht_hash_find(static_nht_hash
, &lookup
);
336 assertf(nhtd
, "BUG: NH %pFX registered but not in hashtable",
339 nhtd
= static_nht_hash_getref(&lookup
);
341 if (nhtd
->refcount
> 1)
342 DEBUGD(&static_dbg_route
,
343 "Reusing registered nexthop(%pFX) for %pRN %d",
344 &lookup
.nh
, rn
, nhtd
->nh_num
);
346 /* !reg && !nh->nh_registered */
347 zlog_warn("trying to unregister nexthop %pFX twice",
352 nh
->nh_registered
= reg
;
356 /* refresh with existing data */
357 afi_t afi
= prefix_afi(&lookup
.nh
);
359 if (nh
->state
== STATIC_NOT_INSTALLED
)
360 nh
->state
= STATIC_START
;
361 static_nht_update(&rn
->p
, &nhtd
->nh
, nhtd
->nh_num
, afi
,
362 si
->safi
, nh
->nh_vrf_id
);
366 if (nhtd
->registered
)
367 /* have no data, but did send register */
370 cmd
= ZEBRA_NEXTHOP_REGISTER
;
371 DEBUGD(&static_dbg_route
, "Registering nexthop(%pFX) for %pRN",
374 bool was_zebra_registered
;
376 was_zebra_registered
= nhtd
->registered
;
377 if (static_nht_hash_decref(&nhtd
))
378 /* still got references alive */
381 /* NB: nhtd is now NULL. */
382 if (!was_zebra_registered
)
385 cmd
= ZEBRA_NEXTHOP_UNREGISTER
;
386 DEBUGD(&static_dbg_route
,
387 "Unregistering nexthop(%pFX) for %pRN", &lookup
.nh
, rn
);
390 if (zclient_send_rnh(zclient
, cmd
, &lookup
.nh
, si
->safi
, false, false,
391 nh
->nh_vrf_id
) == ZCLIENT_SEND_FAILURE
)
392 zlog_warn("%s: Failure to send nexthop %pFX for %pRN to zebra",
393 __func__
, &lookup
.nh
, rn
);
395 nhtd
->registered
= true;
398 extern void static_zebra_route_add(struct static_path
*pn
, bool install
)
400 struct route_node
*rn
= pn
->rn
;
401 struct static_route_info
*si
= rn
->info
;
402 struct static_nexthop
*nh
;
403 const struct prefix
*p
, *src_pp
;
404 struct zapi_nexthop
*api_nh
;
405 struct zapi_route api
;
409 srcdest_rnode_prefixes(rn
, &p
, &src_pp
);
411 memset(&api
, 0, sizeof(api
));
412 api
.vrf_id
= si
->svrf
->vrf
->vrf_id
;
413 api
.type
= ZEBRA_ROUTE_STATIC
;
415 memcpy(&api
.prefix
, p
, sizeof(api
.prefix
));
418 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
419 memcpy(&api
.src_prefix
, src_pp
, sizeof(api
.src_prefix
));
421 SET_FLAG(api
.flags
, ZEBRA_FLAG_RR_USE_DISTANCE
);
422 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
423 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
425 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
426 api
.distance
= pn
->distance
;
429 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
432 if (pn
->table_id
!= 0) {
433 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
434 api
.tableid
= pn
->table_id
;
436 frr_each(static_nexthop_list
, &pn
->nexthop_list
, nh
) {
437 /* Don't overrun the nexthop array */
438 if (nh_num
== zebra_ecmp_count
)
441 api_nh
= &api
.nexthops
[nh_num
];
442 if (nh
->nh_vrf_id
== VRF_UNKNOWN
)
444 /* Skip next hop which peer is down. */
448 api_nh
->vrf_id
= nh
->nh_vrf_id
;
450 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_ONLINK
);
451 if (nh
->color
!= 0) {
452 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRTE
);
453 api_nh
->srte_color
= nh
->color
;
456 nh
->state
= STATIC_SENT_TO_ZEBRA
;
460 if (nh
->ifindex
== IFINDEX_INTERNAL
)
462 api_nh
->ifindex
= nh
->ifindex
;
463 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
465 case STATIC_IPV4_GATEWAY
:
468 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
469 api_nh
->gate
= nh
->addr
;
471 case STATIC_IPV4_GATEWAY_IFNAME
:
472 if (nh
->ifindex
== IFINDEX_INTERNAL
)
474 api_nh
->ifindex
= nh
->ifindex
;
475 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
476 api_nh
->gate
= nh
->addr
;
478 case STATIC_IPV6_GATEWAY
:
481 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
482 api_nh
->gate
= nh
->addr
;
484 case STATIC_IPV6_GATEWAY_IFNAME
:
485 if (nh
->ifindex
== IFINDEX_INTERNAL
)
487 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
488 api_nh
->ifindex
= nh
->ifindex
;
489 api_nh
->gate
= nh
->addr
;
491 case STATIC_BLACKHOLE
:
492 api_nh
->type
= NEXTHOP_TYPE_BLACKHOLE
;
493 switch (nh
->bh_type
) {
494 case STATIC_BLACKHOLE_DROP
:
495 case STATIC_BLACKHOLE_NULL
:
496 api_nh
->bh_type
= BLACKHOLE_NULL
;
498 case STATIC_BLACKHOLE_REJECT
:
499 api_nh
->bh_type
= BLACKHOLE_REJECT
;
504 if (nh
->snh_label
.num_labels
) {
507 SET_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
);
508 api_nh
->label_num
= nh
->snh_label
.num_labels
;
509 for (i
= 0; i
< api_nh
->label_num
; i
++)
510 api_nh
->labels
[i
] = nh
->snh_label
.label
[i
];
515 api
.nexthop_num
= nh_num
;
518 * If we have been given an install but nothing is valid
519 * go ahead and delete the route for double plus fun
521 if (!nh_num
&& install
)
524 zclient_route_send(install
?
525 ZEBRA_ROUTE_ADD
: ZEBRA_ROUTE_DELETE
,
529 static zclient_handler
*const static_handlers
[] = {
530 [ZEBRA_INTERFACE_ADDRESS_ADD
] = interface_address_add
,
531 [ZEBRA_INTERFACE_ADDRESS_DELETE
] = interface_address_delete
,
532 [ZEBRA_ROUTE_NOTIFY_OWNER
] = route_notify_owner
,
533 [ZEBRA_NEXTHOP_UPDATE
] = static_zebra_nexthop_update
,
536 void static_zebra_init(void)
538 struct zclient_options opt
= { .receive_notify
= true };
540 if_zapi_callbacks(static_ifp_create
, static_ifp_up
,
541 static_ifp_down
, static_ifp_destroy
);
543 zclient
= zclient_new(master
, &opt
, static_handlers
,
544 array_size(static_handlers
));
546 zclient_init(zclient
, ZEBRA_ROUTE_STATIC
, 0, &static_privs
);
547 zclient
->zebra_capabilities
= static_zebra_capabilities
;
548 zclient
->zebra_connected
= zebra_connected
;
550 static_nht_hash_init(static_nht_hash
);
551 static_bfd_initialize(zclient
, master
);
554 /* static_zebra_stop used by tests/lib/test_grpc.cpp */
555 void static_zebra_stop(void)
557 static_nht_hash_clear();
558 static_nht_hash_fini(static_nht_hash
);
562 zclient_stop(zclient
);
563 zclient_free(zclient
);
567 void static_zebra_vrf_register(struct vrf
*vrf
)
569 if (vrf
->vrf_id
== VRF_DEFAULT
)
571 zclient_send_reg_requests(zclient
, vrf
->vrf_id
);
574 void static_zebra_vrf_unregister(struct vrf
*vrf
)
576 if (vrf
->vrf_id
== VRF_DEFAULT
)
578 zclient_send_dereg_requests(zclient
, vrf
->vrf_id
);