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"
48 /* Zebra structure to hold current status. */
49 struct zclient
*zclient
;
50 static struct hash
*static_nht_hash
;
52 static struct interface
*zebra_interface_if_lookup(struct stream
*s
)
54 char ifname_tmp
[INTERFACE_NAMSIZ
];
56 /* Read interface name. */
57 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
60 return if_lookup_by_name(ifname_tmp
, VRF_DEFAULT
);
63 /* Inteface addition message from zebra. */
64 static int interface_add(ZAPI_CALLBACK_ARGS
)
66 struct interface
*ifp
;
68 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
73 static_ifindex_update(ifp
, true);
77 static int interface_delete(ZAPI_CALLBACK_ARGS
)
79 struct interface
*ifp
;
83 /* zebra_interface_state_read () updates interface structure in iflist
85 ifp
= zebra_interface_state_read(s
, vrf_id
);
90 if_set_index(ifp
, IFINDEX_INTERNAL
);
92 static_ifindex_update(ifp
, false);
96 static int interface_address_add(ZAPI_CALLBACK_ARGS
)
98 zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
103 static int interface_address_delete(ZAPI_CALLBACK_ARGS
)
107 c
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
116 static int interface_state_up(ZAPI_CALLBACK_ARGS
)
118 struct interface
*ifp
;
120 ifp
= zebra_interface_if_lookup(zclient
->ibuf
);
123 if (if_is_vrf(ifp
)) {
124 struct static_vrf
*svrf
=
125 static_vrf_lookup_by_id(vrf_id
);
127 static_fixup_vrf_ids(svrf
);
128 static_config_install_delayed_routes(svrf
);
131 /* Install any static reliant on this interface coming up */
132 static_install_intf_nh(ifp
);
133 static_ifindex_update(ifp
, true);
139 static int interface_state_down(ZAPI_CALLBACK_ARGS
)
141 struct interface
*ifp
;
143 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
146 static_ifindex_update(ifp
, false);
151 static int route_notify_owner(ZAPI_CALLBACK_ARGS
)
154 enum zapi_route_notify_owner note
;
156 char buf
[PREFIX_STRLEN
];
158 if (!zapi_route_notify_decode(zclient
->ibuf
, &p
, &table_id
, ¬e
))
161 prefix2str(&p
, buf
, sizeof(buf
));
164 case ZAPI_ROUTE_FAIL_INSTALL
:
165 static_nht_mark_state(&p
, vrf_id
, STATIC_NOT_INSTALLED
);
166 zlog_warn("%s: Route %s failed to install for table: %u",
167 __PRETTY_FUNCTION__
, buf
, table_id
);
169 case ZAPI_ROUTE_BETTER_ADMIN_WON
:
170 static_nht_mark_state(&p
, vrf_id
, STATIC_NOT_INSTALLED
);
171 zlog_warn("%s: Route %s over-ridden by better route for table: %u",
172 __PRETTY_FUNCTION__
, buf
, table_id
);
174 case ZAPI_ROUTE_INSTALLED
:
175 static_nht_mark_state(&p
, vrf_id
, STATIC_INSTALLED
);
177 case ZAPI_ROUTE_REMOVED
:
178 static_nht_mark_state(&p
, vrf_id
, STATIC_NOT_INSTALLED
);
180 case ZAPI_ROUTE_REMOVE_FAIL
:
181 static_nht_mark_state(&p
, vrf_id
, STATIC_INSTALLED
);
182 zlog_warn("%s: Route %s failure to remove for table: %u",
183 __PRETTY_FUNCTION__
, buf
, table_id
);
189 static void zebra_connected(struct zclient
*zclient
)
191 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
194 struct static_nht_data
{
203 static int static_zebra_nexthop_update(ZAPI_CALLBACK_ARGS
)
205 struct static_nht_data
*nhtd
, lookup
;
206 struct zapi_route nhr
;
209 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &nhr
)) {
210 zlog_warn("Failure to decode nexthop update message");
214 if (nhr
.prefix
.family
== AF_INET6
)
217 memset(&lookup
, 0, sizeof(lookup
));
218 lookup
.nh
= &nhr
.prefix
;
219 lookup
.nh_vrf_id
= vrf_id
;
221 nhtd
= hash_lookup(static_nht_hash
, &lookup
);
224 nhtd
->nh_num
= nhr
.nexthop_num
;
226 static_nht_reset_start(&nhr
.prefix
, afi
, nhtd
->nh_vrf_id
);
227 static_nht_update(NULL
, &nhr
.prefix
, nhr
.nexthop_num
, afi
,
230 zlog_err("No nhtd?");
235 static void static_zebra_capabilities(struct zclient_capabilities
*cap
)
237 mpls_enabled
= cap
->mpls_enabled
;
240 static unsigned int static_nht_hash_key(const void *data
)
242 const struct static_nht_data
*nhtd
= data
;
243 unsigned int key
= 0;
245 key
= prefix_hash_key(nhtd
->nh
);
246 return jhash_1word(nhtd
->nh_vrf_id
, key
);
249 static bool static_nht_hash_cmp(const void *d1
, const void *d2
)
251 const struct static_nht_data
*nhtd1
= d1
;
252 const struct static_nht_data
*nhtd2
= d2
;
254 if (nhtd1
->nh_vrf_id
!= nhtd2
->nh_vrf_id
)
257 return prefix_same(nhtd1
->nh
, nhtd2
->nh
);
260 static void *static_nht_hash_alloc(void *data
)
262 struct static_nht_data
*copy
= data
;
263 struct static_nht_data
*new;
265 new = XMALLOC(MTYPE_TMP
, sizeof(*new));
267 new->nh
= prefix_new();
268 prefix_copy(new->nh
, copy
->nh
);
271 new->nh_vrf_id
= copy
->nh_vrf_id
;
276 static void static_nht_hash_free(void *data
)
278 struct static_nht_data
*nhtd
= data
;
280 prefix_free(nhtd
->nh
);
281 XFREE(MTYPE_TMP
, nhtd
);
284 void static_zebra_nht_register(struct route_node
*rn
,
285 struct static_route
*si
, bool reg
)
287 struct static_nht_data
*nhtd
, lookup
;
293 ZEBRA_NEXTHOP_REGISTER
: ZEBRA_NEXTHOP_UNREGISTER
;
295 if (si
->nh_registered
&& reg
)
298 if (!si
->nh_registered
&& !reg
)
301 memset(&p
, 0, sizeof(p
));
304 case STATIC_BLACKHOLE
:
306 case STATIC_IPV4_GATEWAY
:
307 case STATIC_IPV4_GATEWAY_IFNAME
:
309 p
.prefixlen
= IPV4_MAX_BITLEN
;
310 p
.u
.prefix4
= si
->addr
.ipv4
;
313 case STATIC_IPV6_GATEWAY
:
314 case STATIC_IPV6_GATEWAY_IFNAME
:
316 p
.prefixlen
= IPV6_MAX_BITLEN
;
317 p
.u
.prefix6
= si
->addr
.ipv6
;
322 memset(&lookup
, 0, sizeof(lookup
));
324 lookup
.nh_vrf_id
= si
->nh_vrf_id
;
326 si
->nh_registered
= reg
;
329 nhtd
= hash_get(static_nht_hash
, &lookup
,
330 static_nht_hash_alloc
);
334 zlog_debug("Registered nexthop(%pFX) for %pRN %d", &p
,
336 if (nhtd
->refcount
> 1 && nhtd
->nh_num
) {
337 static_nht_update(&rn
->p
, nhtd
->nh
, nhtd
->nh_num
,
342 nhtd
= hash_lookup(static_nht_hash
, &lookup
);
347 if (nhtd
->refcount
>= 1)
350 hash_release(static_nht_hash
, nhtd
);
351 static_nht_hash_free(nhtd
);
354 if (zclient_send_rnh(zclient
, cmd
, &p
, false, si
->nh_vrf_id
) < 0)
355 zlog_warn("%s: Failure to send nexthop to zebra",
356 __PRETTY_FUNCTION__
);
359 extern void static_zebra_route_add(struct route_node
*rn
,
360 struct static_route
*si_changed
,
361 vrf_id_t vrf_id
, safi_t safi
, bool install
)
363 struct static_route
*si
= rn
->info
;
364 const struct prefix
*p
, *src_pp
;
365 struct zapi_nexthop
*api_nh
;
366 struct zapi_route api
;
370 srcdest_rnode_prefixes(rn
, &p
, &src_pp
);
372 memset(&api
, 0, sizeof(api
));
374 api
.type
= ZEBRA_ROUTE_STATIC
;
376 memcpy(&api
.prefix
, p
, sizeof(api
.prefix
));
379 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
380 memcpy(&api
.src_prefix
, src_pp
, sizeof(api
.src_prefix
));
382 SET_FLAG(api
.flags
, ZEBRA_FLAG_RR_USE_DISTANCE
);
383 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
384 if (si_changed
->distance
) {
385 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
386 api
.distance
= si_changed
->distance
;
388 if (si_changed
->tag
) {
389 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
390 api
.tag
= si_changed
->tag
;
392 if (si_changed
->table_id
!= 0) {
393 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
394 api
.tableid
= si_changed
->table_id
;
396 for (/*loaded above*/; si
; si
= si
->next
) {
397 api_nh
= &api
.nexthops
[nh_num
];
398 if (si
->nh_vrf_id
== VRF_UNKNOWN
)
401 if (si
->distance
!= si_changed
->distance
)
404 if (si
->table_id
!= si_changed
->table_id
)
407 api_nh
->vrf_id
= si
->nh_vrf_id
;
408 api_nh
->onlink
= si
->onlink
;
410 si
->state
= STATIC_SENT_TO_ZEBRA
;
414 if (si
->ifindex
== IFINDEX_INTERNAL
)
416 api_nh
->ifindex
= si
->ifindex
;
417 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
419 case STATIC_IPV4_GATEWAY
:
422 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
423 api_nh
->gate
= si
->addr
;
425 case STATIC_IPV4_GATEWAY_IFNAME
:
426 if (si
->ifindex
== IFINDEX_INTERNAL
)
428 api_nh
->ifindex
= si
->ifindex
;
429 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
430 api_nh
->gate
= si
->addr
;
432 case STATIC_IPV6_GATEWAY
:
435 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
436 api_nh
->gate
= si
->addr
;
438 case STATIC_IPV6_GATEWAY_IFNAME
:
439 if (si
->ifindex
== IFINDEX_INTERNAL
)
441 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
442 api_nh
->ifindex
= si
->ifindex
;
443 api_nh
->gate
= si
->addr
;
445 case STATIC_BLACKHOLE
:
446 api_nh
->type
= NEXTHOP_TYPE_BLACKHOLE
;
447 switch (si
->bh_type
) {
448 case STATIC_BLACKHOLE_DROP
:
449 case STATIC_BLACKHOLE_NULL
:
450 api_nh
->bh_type
= BLACKHOLE_NULL
;
452 case STATIC_BLACKHOLE_REJECT
:
453 api_nh
->bh_type
= BLACKHOLE_REJECT
;
458 if (si
->snh_label
.num_labels
) {
461 SET_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
);
462 api_nh
->label_num
= si
->snh_label
.num_labels
;
463 for (i
= 0; i
< api_nh
->label_num
; i
++)
464 api_nh
->labels
[i
] = si
->snh_label
.label
[i
];
469 api
.nexthop_num
= nh_num
;
472 * If we have been given an install but nothing is valid
473 * go ahead and delete the route for double plus fun
475 if (!nh_num
&& install
)
478 zclient_route_send(install
?
479 ZEBRA_ROUTE_ADD
: ZEBRA_ROUTE_DELETE
,
482 void static_zebra_init(void)
484 struct zclient_options opt
= { .receive_notify
= true };
486 zclient
= zclient_new(master
, &opt
);
488 zclient_init(zclient
, ZEBRA_ROUTE_STATIC
, 0, &static_privs
);
489 zclient
->zebra_capabilities
= static_zebra_capabilities
;
490 zclient
->zebra_connected
= zebra_connected
;
491 zclient
->interface_add
= interface_add
;
492 zclient
->interface_delete
= interface_delete
;
493 zclient
->interface_up
= interface_state_up
;
494 zclient
->interface_down
= interface_state_down
;
495 zclient
->interface_address_add
= interface_address_add
;
496 zclient
->interface_address_delete
= interface_address_delete
;
497 zclient
->route_notify_owner
= route_notify_owner
;
498 zclient
->nexthop_update
= static_zebra_nexthop_update
;
500 static_nht_hash
= hash_create(static_nht_hash_key
,
502 "Static Nexthop Tracking hash");