]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_rnh.c
zebra: support zebra shutdown and cleanup
[mirror_frr.git] / zebra / zebra_rnh.c
1 /* Zebra next hop tracking code
2 * Copyright (C) 2013 Cumulus Networks, Inc.
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra 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
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
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
19 */
20
21 #include <zebra.h>
22
23 #include "prefix.h"
24 #include "table.h"
25 #include "memory.h"
26 #include "command.h"
27 #include "if.h"
28 #include "log.h"
29 #include "sockunion.h"
30 #include "linklist.h"
31 #include "thread.h"
32 #include "workqueue.h"
33 #include "prefix.h"
34 #include "routemap.h"
35 #include "stream.h"
36 #include "nexthop.h"
37 #include "vrf.h"
38
39 #include "zebra/rib.h"
40 #include "zebra/rt.h"
41 #include "zebra/zserv.h"
42 #include "zebra/zebra_ns.h"
43 #include "zebra/zebra_vrf.h"
44 #include "zebra/redistribute.h"
45 #include "zebra/debug.h"
46 #include "zebra/zebra_rnh.h"
47 #include "zebra/zebra_routemap.h"
48 #include "zebra/interface.h"
49 #include "zebra/zebra_memory.h"
50 #include "zebra/zebra_errors.h"
51
52 static void free_state(vrf_id_t vrf_id, struct route_entry *re,
53 struct route_node *rn);
54 static void copy_state(struct rnh *rnh, struct route_entry *re,
55 struct route_node *rn);
56 #define lookup_rnh_table(v, f) \
57 ({ \
58 struct zebra_vrf *zvrf; \
59 struct route_table *t = NULL; \
60 zvrf = zebra_vrf_lookup_by_id(v); \
61 if (zvrf) \
62 t = zvrf->rnh_table[family2afi(f)]; \
63 t; \
64 })
65
66 static int compare_state(struct route_entry *r1, struct route_entry *r2);
67 static int send_client(struct rnh *rnh, struct zserv *client, rnh_type_t type,
68 vrf_id_t vrf_id);
69 static void print_rnh(struct route_node *rn, struct vty *vty);
70 static int zebra_client_cleanup_rnh(struct zserv *client);
71
72 int zebra_rnh_ip_default_route = 0;
73 int zebra_rnh_ipv6_default_route = 0;
74
75 void zebra_rnh_init(void)
76 {
77 hook_register(zserv_client_close, zebra_client_cleanup_rnh);
78 }
79
80 static inline struct route_table *get_rnh_table(vrf_id_t vrfid, int family,
81 rnh_type_t type)
82 {
83 struct zebra_vrf *zvrf;
84 struct route_table *t = NULL;
85
86 zvrf = zebra_vrf_lookup_by_id(vrfid);
87 if (zvrf)
88 switch (type) {
89 case RNH_NEXTHOP_TYPE:
90 t = zvrf->rnh_table[family2afi(family)];
91 break;
92 case RNH_IMPORT_CHECK_TYPE:
93 t = zvrf->import_check_table[family2afi(family)];
94 break;
95 }
96
97 return t;
98 }
99
100 char *rnh_str(struct rnh *rnh, char *buf, int size)
101 {
102 prefix2str(&(rnh->node->p), buf, size);
103 return buf;
104 }
105
106 struct rnh *zebra_add_rnh(struct prefix *p, vrf_id_t vrfid, rnh_type_t type,
107 bool *exists)
108 {
109 struct route_table *table;
110 struct route_node *rn;
111 struct rnh *rnh = NULL;
112 char buf[PREFIX2STR_BUFFER];
113
114 if (IS_ZEBRA_DEBUG_NHT) {
115 prefix2str(p, buf, sizeof(buf));
116 zlog_debug("%u: Add RNH %s type %d", vrfid, buf, type);
117 }
118 table = get_rnh_table(vrfid, PREFIX_FAMILY(p), type);
119 if (!table) {
120 prefix2str(p, buf, sizeof(buf));
121 flog_warn(EC_ZEBRA_RNH_NO_TABLE,
122 "%u: Add RNH %s type %d - table not found", vrfid,
123 buf, type);
124 exists = false;
125 return NULL;
126 }
127
128 /* Make it sure prefixlen is applied to the prefix. */
129 apply_mask(p);
130
131 /* Lookup (or add) route node.*/
132 rn = route_node_get(table, p);
133
134 if (!rn->info) {
135 rnh = XCALLOC(MTYPE_RNH, sizeof(struct rnh));
136 rnh->client_list = list_new();
137 rnh->vrf_id = vrfid;
138 rnh->zebra_pseudowire_list = list_new();
139 route_lock_node(rn);
140 rn->info = rnh;
141 rnh->node = rn;
142 *exists = false;
143 } else
144 *exists = true;
145
146 route_unlock_node(rn);
147 return (rn->info);
148 }
149
150 struct rnh *zebra_lookup_rnh(struct prefix *p, vrf_id_t vrfid, rnh_type_t type)
151 {
152 struct route_table *table;
153 struct route_node *rn;
154
155 table = get_rnh_table(vrfid, PREFIX_FAMILY(p), type);
156 if (!table)
157 return NULL;
158
159 /* Make it sure prefixlen is applied to the prefix. */
160 apply_mask(p);
161
162 /* Lookup route node.*/
163 rn = route_node_lookup(table, p);
164 if (!rn)
165 return NULL;
166
167 route_unlock_node(rn);
168 return (rn->info);
169 }
170
171 void zebra_free_rnh(struct rnh *rnh)
172 {
173 rnh->flags |= ZEBRA_NHT_DELETED;
174 list_delete(&rnh->client_list);
175 list_delete(&rnh->zebra_pseudowire_list);
176 free_state(rnh->vrf_id, rnh->state, rnh->node);
177 XFREE(MTYPE_RNH, rnh);
178 }
179
180 void zebra_delete_rnh(struct rnh *rnh, rnh_type_t type)
181 {
182 struct route_node *rn;
183
184 if (!rnh || (rnh->flags & ZEBRA_NHT_DELETED) || !(rn = rnh->node))
185 return;
186
187 if (IS_ZEBRA_DEBUG_NHT) {
188 char buf[PREFIX2STR_BUFFER];
189 zlog_debug("%u: Del RNH %s type %d", rnh->vrf_id,
190 rnh_str(rnh, buf, sizeof(buf)), type);
191 }
192
193 zebra_free_rnh(rnh);
194 rn->info = NULL;
195 route_unlock_node(rn);
196 }
197
198 /*
199 * This code will send to the registering client
200 * the looked up rnh.
201 * For a rnh that was created, there is no data
202 * so it will send an empty nexthop group
203 * If rnh exists then we know it has been evaluated
204 * and as such it will have a resolved rnh.
205 */
206 void zebra_add_rnh_client(struct rnh *rnh, struct zserv *client,
207 rnh_type_t type, vrf_id_t vrf_id)
208 {
209 if (IS_ZEBRA_DEBUG_NHT) {
210 char buf[PREFIX2STR_BUFFER];
211 zlog_debug("%u: Client %s registers for RNH %s type %d", vrf_id,
212 zebra_route_string(client->proto),
213 rnh_str(rnh, buf, sizeof(buf)), type);
214 }
215 if (!listnode_lookup(rnh->client_list, client)) {
216 listnode_add(rnh->client_list, client);
217 send_client(rnh, client, type, vrf_id);
218 }
219 }
220
221 void zebra_remove_rnh_client(struct rnh *rnh, struct zserv *client,
222 rnh_type_t type)
223 {
224 if (IS_ZEBRA_DEBUG_NHT) {
225 char buf[PREFIX2STR_BUFFER];
226 zlog_debug("Client %s unregisters for RNH %s type %d",
227 zebra_route_string(client->proto),
228 rnh_str(rnh, buf, sizeof(buf)), type);
229 }
230 listnode_delete(rnh->client_list, client);
231 if (list_isempty(rnh->client_list)
232 && list_isempty(rnh->zebra_pseudowire_list))
233 zebra_delete_rnh(rnh, type);
234 }
235
236 /* XXX move this utility function elsewhere? */
237 static void addr2hostprefix(int af, const union g_addr *addr,
238 struct prefix *prefix)
239 {
240 switch (af) {
241 case AF_INET:
242 prefix->family = AF_INET;
243 prefix->prefixlen = IPV4_MAX_BITLEN;
244 prefix->u.prefix4 = addr->ipv4;
245 break;
246 case AF_INET6:
247 prefix->family = AF_INET6;
248 prefix->prefixlen = IPV6_MAX_BITLEN;
249 prefix->u.prefix6 = addr->ipv6;
250 break;
251 default:
252 memset(prefix, 0, sizeof(*prefix));
253 zlog_debug("%s: unknown address family %d", __func__, af);
254 break;
255 }
256 }
257
258 void zebra_register_rnh_pseudowire(vrf_id_t vrf_id, struct zebra_pw *pw)
259 {
260 struct prefix nh;
261 struct rnh *rnh;
262 bool exists;
263 struct zebra_vrf *zvrf;
264
265 zvrf = vrf_info_lookup(vrf_id);
266 if (!zvrf)
267 return;
268
269 addr2hostprefix(pw->af, &pw->nexthop, &nh);
270 rnh = zebra_add_rnh(&nh, vrf_id, RNH_NEXTHOP_TYPE, &exists);
271 if (rnh && !listnode_lookup(rnh->zebra_pseudowire_list, pw)) {
272 listnode_add(rnh->zebra_pseudowire_list, pw);
273 pw->rnh = rnh;
274 zebra_evaluate_rnh(zvrf, pw->af, 1, RNH_NEXTHOP_TYPE, &nh);
275 }
276 }
277
278 void zebra_deregister_rnh_pseudowire(vrf_id_t vrf_id, struct zebra_pw *pw)
279 {
280 struct rnh *rnh;
281
282 rnh = pw->rnh;
283 if (!rnh)
284 return;
285
286 listnode_delete(rnh->zebra_pseudowire_list, pw);
287 pw->rnh = NULL;
288
289 if (list_isempty(rnh->client_list)
290 && list_isempty(rnh->zebra_pseudowire_list))
291 zebra_delete_rnh(rnh, RNH_NEXTHOP_TYPE);
292 }
293
294 /* Apply the NHT route-map for a client to the route (and nexthops)
295 * resolving a NH.
296 */
297 static int zebra_rnh_apply_nht_rmap(int family, struct zebra_vrf *zvrf,
298 struct route_node *prn,
299 struct route_entry *re, int proto)
300 {
301 int at_least_one = 0;
302 int rmap_family; /* Route map has diff AF family enum */
303 struct nexthop *nexthop;
304 int ret;
305
306 rmap_family = (family == AF_INET) ? AFI_IP : AFI_IP6;
307
308 if (prn && re) {
309 for (nexthop = re->ng.nexthop; nexthop;
310 nexthop = nexthop->next) {
311 ret = zebra_nht_route_map_check(
312 rmap_family, proto, &prn->p, zvrf, re, nexthop);
313 if (ret != RMAP_DENYMATCH) {
314 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
315 at_least_one++; /* at least one valid NH */
316 } else {
317 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
318 }
319 }
320 }
321 return (at_least_one);
322 }
323
324 /*
325 * Determine appropriate route (RE entry) resolving a tracked BGP route
326 * for BGP route for import.
327 */
328 static struct route_entry *
329 zebra_rnh_resolve_import_entry(struct zebra_vrf *zvrf, int family,
330 struct route_node *nrn, struct rnh *rnh,
331 struct route_node **prn)
332 {
333 struct route_table *route_table;
334 struct route_node *rn;
335 struct route_entry *re;
336
337 *prn = NULL;
338
339 route_table = zvrf->table[family2afi(family)][SAFI_UNICAST];
340 if (!route_table) // unexpected
341 return NULL;
342
343 rn = route_node_match(route_table, &nrn->p);
344 if (!rn)
345 return NULL;
346
347 /* Unlock route node - we don't need to lock when walking the tree. */
348 route_unlock_node(rn);
349
350 if (CHECK_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH)
351 && !prefix_same(&nrn->p, &rn->p))
352 return NULL;
353
354 /* Identify appropriate route entry. */
355 RNODE_FOREACH_RE (rn, re) {
356 if (!CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)
357 && CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)
358 && (re->type != ZEBRA_ROUTE_BGP))
359 break;
360 }
361
362 if (re)
363 *prn = rn;
364 return re;
365 }
366
367 /*
368 * See if a tracked route entry for import (by BGP) has undergone any
369 * change, and if so, notify the client.
370 */
371 static void zebra_rnh_eval_import_check_entry(vrf_id_t vrfid, int family,
372 int force, struct route_node *nrn,
373 struct rnh *rnh,
374 struct route_entry *re)
375 {
376 int state_changed = 0;
377 struct zserv *client;
378 char bufn[INET6_ADDRSTRLEN];
379 struct listnode *node;
380 struct nexthop *nexthop;
381
382 if (re && (rnh->state == NULL)) {
383 for (ALL_NEXTHOPS(re->ng, nexthop))
384 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) {
385 state_changed = 1;
386 break;
387 }
388 } else if (!re && (rnh->state != NULL))
389 state_changed = 1;
390
391 if (compare_state(re, rnh->state))
392 copy_state(rnh, re, nrn);
393
394 if (state_changed || force) {
395 if (IS_ZEBRA_DEBUG_NHT) {
396 prefix2str(&nrn->p, bufn, INET6_ADDRSTRLEN);
397 zlog_debug("%u:%s: Route import check %s %s\n", vrfid,
398 bufn, rnh->state ? "passed" : "failed",
399 state_changed ? "(state changed)" : "");
400 }
401 /* state changed, notify clients */
402 for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client)) {
403 send_client(rnh, client, RNH_IMPORT_CHECK_TYPE, vrfid);
404 }
405 }
406 }
407
408 /*
409 * Notify clients registered for this nexthop about a change.
410 */
411 static void zebra_rnh_notify_protocol_clients(
412 struct zebra_vrf *zvrf, int family, struct route_node *nrn,
413 struct rnh *rnh, struct route_node *prn, struct route_entry *re)
414 {
415 struct listnode *node;
416 struct zserv *client;
417 char bufn[INET6_ADDRSTRLEN];
418 char bufp[INET6_ADDRSTRLEN];
419 int num_resolving_nh;
420
421 if (IS_ZEBRA_DEBUG_NHT) {
422 prefix2str(&nrn->p, bufn, INET6_ADDRSTRLEN);
423 if (prn && re) {
424 prefix2str(&prn->p, bufp, INET6_ADDRSTRLEN);
425 zlog_debug("%u:%s: NH resolved over route %s",
426 zvrf->vrf->vrf_id, bufn, bufp);
427 } else
428 zlog_debug("%u:%s: NH has become unresolved",
429 zvrf->vrf->vrf_id, bufn);
430 }
431
432 for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client)) {
433 if (prn && re) {
434 /* Apply route-map for this client to route resolving
435 * this
436 * nexthop to see if it is filtered or not.
437 */
438 num_resolving_nh = zebra_rnh_apply_nht_rmap(
439 family, zvrf, prn, re, client->proto);
440 if (num_resolving_nh)
441 rnh->filtered[client->proto] = 0;
442 else
443 rnh->filtered[client->proto] = 1;
444
445 if (IS_ZEBRA_DEBUG_NHT)
446 zlog_debug(
447 "%u:%s: Notifying client %s about NH %s",
448 zvrf->vrf->vrf_id, bufn,
449 zebra_route_string(client->proto),
450 num_resolving_nh
451 ? ""
452 : "(filtered by route-map)");
453 } else {
454 rnh->filtered[client->proto] = 0;
455 if (IS_ZEBRA_DEBUG_NHT)
456 zlog_debug(
457 "%u:%s: Notifying client %s about NH (unreachable)",
458 zvrf->vrf->vrf_id, bufn,
459 zebra_route_string(client->proto));
460 }
461
462 send_client(rnh, client, RNH_NEXTHOP_TYPE, zvrf->vrf->vrf_id);
463 }
464 }
465
466 static void zebra_rnh_process_pbr_tables(int family,
467 struct route_node *nrn,
468 struct rnh *rnh,
469 struct route_node *prn,
470 struct route_entry *re)
471 {
472 struct zebra_ns_table *znst;
473 struct route_entry *o_re;
474 struct route_node *o_rn;
475 struct listnode *node;
476 struct zserv *client;
477 struct zebra_ns *zns;
478 afi_t afi = AFI_IP;
479
480 if (family == AF_INET6)
481 afi = AFI_IP6;
482
483 /*
484 * We are only concerned about nexthops that change for
485 * anyone using PBR
486 */
487 for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client)) {
488 if (client->proto == ZEBRA_ROUTE_PBR)
489 break;
490 }
491
492 if (!client)
493 return;
494
495 zns = zebra_ns_lookup(NS_DEFAULT);
496 RB_FOREACH (znst, zebra_ns_table_head, &zns->ns_tables) {
497 if (afi != znst->afi)
498 continue;
499
500 for (o_rn = route_top(znst->table);
501 o_rn; o_rn = srcdest_route_next(o_rn)) {
502 RNODE_FOREACH_RE (o_rn, o_re) {
503 if (o_re->type == ZEBRA_ROUTE_PBR)
504 break;
505
506 }
507
508 /*
509 * If we have a PBR route and a nexthop changes
510 * just rethink it. Yes this is a hammer, but
511 * a small one
512 */
513 if (o_re) {
514 SET_FLAG(o_re->status, ROUTE_ENTRY_CHANGED);
515 rib_queue_add(o_rn);
516 }
517 }
518 }
519 }
520
521 /*
522 * Utility to determine whether a candidate nexthop is useable. We make this
523 * check in a couple of places, so this is a single home for the logic we
524 * use.
525 */
526 static bool rnh_nexthop_valid(const struct nexthop *nh)
527 {
528 return ((CHECK_FLAG(nh->flags, NEXTHOP_FLAG_FIB)
529 || CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE))
530 && CHECK_FLAG(nh->flags, NEXTHOP_FLAG_ACTIVE));
531 }
532
533 /*
534 * Determine appropriate route (route entry) resolving a tracked
535 * nexthop.
536 */
537 static struct route_entry *
538 zebra_rnh_resolve_nexthop_entry(struct zebra_vrf *zvrf, int family,
539 struct route_node *nrn, struct rnh *rnh,
540 struct route_node **prn)
541 {
542 struct route_table *route_table;
543 struct route_node *rn;
544 struct route_entry *re;
545 struct nexthop *nexthop;
546
547 *prn = NULL;
548
549 route_table = zvrf->table[family2afi(family)][SAFI_UNICAST];
550 if (!route_table)
551 return NULL;
552
553 rn = route_node_match(route_table, &nrn->p);
554 if (!rn)
555 return NULL;
556
557 /* Unlock route node - we don't need to lock when walking the tree. */
558 route_unlock_node(rn);
559
560 /* While resolving nexthops, we may need to walk up the tree from the
561 * most-specific match. Do similar logic as in zebra_rib.c
562 */
563 while (rn) {
564 /* Do not resolve over default route unless allowed &&
565 * match route to be exact if so specified
566 */
567 if (is_default_prefix(&rn->p)
568 && !rnh_resolve_via_default(rn->p.family))
569 return NULL;
570
571 /* Identify appropriate route entry. */
572 RNODE_FOREACH_RE (rn, re) {
573 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
574 continue;
575 if (!CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED))
576 continue;
577
578 /* Just being SELECTED isn't quite enough - must
579 * have an installed nexthop to be useful.
580 */
581 for (nexthop = re->ng.nexthop; nexthop;
582 nexthop = nexthop->next) {
583 if (rnh_nexthop_valid(nexthop))
584 break;
585 }
586
587 if (nexthop == NULL)
588 continue;
589
590 if (CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED)) {
591 if ((re->type == ZEBRA_ROUTE_CONNECT)
592 || (re->type == ZEBRA_ROUTE_STATIC))
593 break;
594 if (re->type == ZEBRA_ROUTE_NHRP) {
595
596 for (nexthop = re->ng.nexthop; nexthop;
597 nexthop = nexthop->next)
598 if (nexthop->type
599 == NEXTHOP_TYPE_IFINDEX)
600 break;
601 if (nexthop)
602 break;
603 }
604 } else
605 break;
606 }
607
608 /* Route entry found, we're done; else, walk up the tree. */
609 if (re) {
610 *prn = rn;
611 return re;
612 }
613
614 if (CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
615 rn = rn->parent;
616 else
617 return NULL;
618 }
619
620 return NULL;
621 }
622
623 static void zebra_rnh_process_pseudowires(vrf_id_t vrfid, struct rnh *rnh)
624 {
625 struct zebra_pw *pw;
626 struct listnode *node;
627
628 for (ALL_LIST_ELEMENTS_RO(rnh->zebra_pseudowire_list, node, pw))
629 zebra_pw_update(pw);
630 }
631
632 /*
633 * See if a tracked nexthop entry has undergone any change, and if so,
634 * take appropriate action; this involves notifying any clients and/or
635 * scheduling dependent static routes for processing.
636 */
637 static void zebra_rnh_eval_nexthop_entry(struct zebra_vrf *zvrf, int family,
638 int force, struct route_node *nrn,
639 struct rnh *rnh,
640 struct route_node *prn,
641 struct route_entry *re)
642 {
643 int state_changed = 0;
644
645 /* If we're resolving over a different route, resolution has changed or
646 * the resolving route has some change (e.g., metric), there is a state
647 * change.
648 */
649 if (!prefix_same(&rnh->resolved_route, &prn->p)) {
650 if (prn)
651 prefix_copy(&rnh->resolved_route, &prn->p);
652 else
653 memset(&rnh->resolved_route, 0, sizeof(struct prefix));
654
655 copy_state(rnh, re, nrn);
656 state_changed = 1;
657 } else if (compare_state(re, rnh->state)) {
658 copy_state(rnh, re, nrn);
659 state_changed = 1;
660 }
661
662 if (state_changed || force) {
663 /* NOTE: Use the "copy" of resolving route stored in 'rnh' i.e.,
664 * rnh->state.
665 */
666 /* Notify registered protocol clients. */
667 zebra_rnh_notify_protocol_clients(zvrf, family, nrn, rnh, prn,
668 rnh->state);
669
670 zebra_rnh_process_pbr_tables(family, nrn, rnh, prn, rnh->state);
671
672 /* Process pseudowires attached to this nexthop */
673 zebra_rnh_process_pseudowires(zvrf->vrf->vrf_id, rnh);
674 }
675 }
676
677 /* Evaluate one tracked entry */
678 static void zebra_rnh_evaluate_entry(struct zebra_vrf *zvrf, int family,
679 int force, rnh_type_t type,
680 struct route_node *nrn)
681 {
682 struct rnh *rnh;
683 struct route_entry *re;
684 struct route_node *prn;
685 char bufn[INET6_ADDRSTRLEN];
686
687 if (IS_ZEBRA_DEBUG_NHT) {
688 prefix2str(&nrn->p, bufn, INET6_ADDRSTRLEN);
689 zlog_debug("%u:%s: Evaluate RNH, type %d %s", zvrf->vrf->vrf_id,
690 bufn, type, force ? "(force)" : "");
691 }
692
693 rnh = nrn->info;
694
695 /* Identify route entry (RE) resolving this tracked entry. */
696 if (type == RNH_IMPORT_CHECK_TYPE)
697 re = zebra_rnh_resolve_import_entry(zvrf, family, nrn, rnh,
698 &prn);
699 else
700 re = zebra_rnh_resolve_nexthop_entry(zvrf, family, nrn, rnh,
701 &prn);
702
703 /* If the entry cannot be resolved and that is also the existing state,
704 * there is nothing further to do.
705 */
706 if (!re && rnh->state == NULL && !force)
707 return;
708
709 /* Process based on type of entry. */
710 if (type == RNH_IMPORT_CHECK_TYPE)
711 zebra_rnh_eval_import_check_entry(zvrf->vrf->vrf_id, family,
712 force, nrn, rnh, re);
713 else
714 zebra_rnh_eval_nexthop_entry(zvrf, family, force, nrn, rnh, prn,
715 re);
716 }
717
718 /*
719 * Clear the ROUTE_ENTRY_NEXTHOPS_CHANGED flag
720 * from the re entries.
721 *
722 * Please note we are doing this *after* we have
723 * notified the world about each nexthop as that
724 * we can have a situation where one re entry
725 * covers multiple nexthops we are interested in.
726 */
727 static void zebra_rnh_clear_nhc_flag(struct zebra_vrf *zvrf, int family,
728 rnh_type_t type, struct route_node *nrn)
729 {
730 struct rnh *rnh;
731 struct route_entry *re;
732 struct route_node *prn;
733
734 rnh = nrn->info;
735
736 /* Identify route entry (RIB) resolving this tracked entry. */
737 if (type == RNH_IMPORT_CHECK_TYPE)
738 re = zebra_rnh_resolve_import_entry(zvrf, family, nrn, rnh,
739 &prn);
740 else
741 re = zebra_rnh_resolve_nexthop_entry(zvrf, family, nrn, rnh,
742 &prn);
743
744 if (re) {
745 UNSET_FLAG(re->status, ROUTE_ENTRY_NEXTHOPS_CHANGED);
746 UNSET_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED);
747 }
748 }
749
750 /* Evaluate all tracked entries (nexthops or routes for import into BGP)
751 * of a particular VRF and address-family or a specific prefix.
752 */
753 void zebra_evaluate_rnh(struct zebra_vrf *zvrf, int family, int force,
754 rnh_type_t type, struct prefix *p)
755 {
756 struct route_table *rnh_table;
757 struct route_node *nrn;
758
759 rnh_table = get_rnh_table(zvrf->vrf->vrf_id, family, type);
760 if (!rnh_table) // unexpected
761 return;
762
763 if (p) {
764 /* Evaluating a specific entry, make sure it exists. */
765 nrn = route_node_lookup(rnh_table, p);
766 if (nrn && nrn->info)
767 zebra_rnh_evaluate_entry(zvrf, family, force, type,
768 nrn);
769
770 if (nrn)
771 route_unlock_node(nrn);
772 } else {
773 /* Evaluate entire table. */
774 nrn = route_top(rnh_table);
775 while (nrn) {
776 if (nrn->info)
777 zebra_rnh_evaluate_entry(zvrf, family, force,
778 type, nrn);
779 nrn = route_next(nrn); /* this will also unlock nrn */
780 }
781 nrn = route_top(rnh_table);
782 while (nrn) {
783 if (nrn->info)
784 zebra_rnh_clear_nhc_flag(zvrf, family, type,
785 nrn);
786 nrn = route_next(nrn); /* this will also unlock nrn */
787 }
788 }
789 }
790
791 void zebra_print_rnh_table(vrf_id_t vrfid, int af, struct vty *vty,
792 rnh_type_t type)
793 {
794 struct route_table *table;
795 struct route_node *rn;
796
797 table = get_rnh_table(vrfid, af, type);
798 if (!table) {
799 zlog_debug("print_rnhs: rnh table not found\n");
800 return;
801 }
802
803 for (rn = route_top(table); rn; rn = route_next(rn))
804 if (rn->info)
805 print_rnh(rn, vty);
806 }
807
808 /**
809 * free_state - free up the re structure associated with the rnh.
810 */
811 static void free_state(vrf_id_t vrf_id, struct route_entry *re,
812 struct route_node *rn)
813 {
814
815 if (!re)
816 return;
817
818 /* free RE and nexthops */
819 nexthops_free(re->ng.nexthop);
820 XFREE(MTYPE_RE, re);
821 }
822
823 static void copy_state(struct rnh *rnh, struct route_entry *re,
824 struct route_node *rn)
825 {
826 struct route_entry *state;
827
828 if (rnh->state) {
829 free_state(rnh->vrf_id, rnh->state, rn);
830 rnh->state = NULL;
831 }
832
833 if (!re)
834 return;
835
836 state = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
837 state->type = re->type;
838 state->distance = re->distance;
839 state->metric = re->metric;
840 state->vrf_id = re->vrf_id;
841
842 route_entry_copy_nexthops(state, re->ng.nexthop);
843 rnh->state = state;
844 }
845
846 static int compare_state(struct route_entry *r1, struct route_entry *r2)
847 {
848
849 if (!r1 && !r2)
850 return 0;
851
852 if ((!r1 && r2) || (r1 && !r2))
853 return 1;
854
855 if (r1->distance != r2->distance)
856 return 1;
857
858 if (r1->metric != r2->metric)
859 return 1;
860
861 if (r1->nexthop_num != r2->nexthop_num)
862 return 1;
863
864 if (CHECK_FLAG(r1->status, ROUTE_ENTRY_NEXTHOPS_CHANGED)
865 || CHECK_FLAG(r1->status, ROUTE_ENTRY_LABELS_CHANGED))
866 return 1;
867
868 return 0;
869 }
870
871 static int send_client(struct rnh *rnh, struct zserv *client, rnh_type_t type,
872 vrf_id_t vrf_id)
873 {
874 struct stream *s;
875 struct route_entry *re;
876 unsigned long nump;
877 uint8_t num;
878 struct nexthop *nh;
879 struct route_node *rn;
880 int cmd = (type == RNH_IMPORT_CHECK_TYPE) ? ZEBRA_IMPORT_CHECK_UPDATE
881 : ZEBRA_NEXTHOP_UPDATE;
882
883 rn = rnh->node;
884 re = rnh->state;
885
886 /* Get output stream. */
887 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
888
889 zclient_create_header(s, cmd, vrf_id);
890
891 stream_putw(s, rn->p.family);
892 switch (rn->p.family) {
893 case AF_INET:
894 stream_putc(s, rn->p.prefixlen);
895 stream_put_in_addr(s, &rn->p.u.prefix4);
896 break;
897 case AF_INET6:
898 stream_putc(s, rn->p.prefixlen);
899 stream_put(s, &rn->p.u.prefix6, IPV6_MAX_BYTELEN);
900 break;
901 default:
902 flog_err(EC_ZEBRA_RNH_UNKNOWN_FAMILY,
903 "%s: Unknown family (%d) notification attempted\n",
904 __FUNCTION__, rn->p.family);
905 break;
906 }
907 if (re) {
908 stream_putc(s, re->type);
909 stream_putw(s, re->instance);
910 stream_putc(s, re->distance);
911 stream_putl(s, re->metric);
912 num = 0;
913 nump = stream_get_endp(s);
914 stream_putc(s, 0);
915 for (nh = re->ng.nexthop; nh; nh = nh->next)
916 if (rnh_nexthop_valid(nh)) {
917 stream_putc(s, nh->type);
918 switch (nh->type) {
919 case NEXTHOP_TYPE_IPV4:
920 case NEXTHOP_TYPE_IPV4_IFINDEX:
921 stream_put_in_addr(s, &nh->gate.ipv4);
922 stream_putl(s, nh->ifindex);
923 break;
924 case NEXTHOP_TYPE_IFINDEX:
925 stream_putl(s, nh->ifindex);
926 break;
927 case NEXTHOP_TYPE_IPV6:
928 case NEXTHOP_TYPE_IPV6_IFINDEX:
929 stream_put(s, &nh->gate.ipv6, 16);
930 stream_putl(s, nh->ifindex);
931 break;
932 default:
933 /* do nothing */
934 break;
935 }
936 if (nh->nh_label) {
937 stream_putc(s,
938 nh->nh_label->num_labels);
939 if (nh->nh_label->num_labels)
940 stream_put(
941 s,
942 &nh->nh_label->label[0],
943 nh->nh_label->num_labels
944 * sizeof(mpls_label_t));
945 } else
946 stream_putc(s, 0);
947 num++;
948 }
949 stream_putc_at(s, nump, num);
950 } else {
951 stream_putc(s, 0); // type
952 stream_putw(s, 0); // instance
953 stream_putc(s, 0); // distance
954 stream_putl(s, 0); // metric
955 stream_putc(s, 0); // nexthops
956 }
957 stream_putw_at(s, 0, stream_get_endp(s));
958
959 client->nh_last_upd_time = monotime(NULL);
960 client->last_write_cmd = cmd;
961 return zserv_send_message(client, s);
962 }
963
964 static void print_nh(struct nexthop *nexthop, struct vty *vty)
965 {
966 char buf[BUFSIZ];
967 struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
968
969 switch (nexthop->type) {
970 case NEXTHOP_TYPE_IPV4:
971 case NEXTHOP_TYPE_IPV4_IFINDEX:
972 vty_out(vty, " via %s", inet_ntoa(nexthop->gate.ipv4));
973 if (nexthop->ifindex)
974 vty_out(vty, ", %s",
975 ifindex2ifname_per_ns(zns, nexthop->ifindex));
976 break;
977 case NEXTHOP_TYPE_IPV6:
978 case NEXTHOP_TYPE_IPV6_IFINDEX:
979 vty_out(vty, " %s",
980 inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf, BUFSIZ));
981 if (nexthop->ifindex)
982 vty_out(vty, ", via %s",
983 ifindex2ifname_per_ns(zns, nexthop->ifindex));
984 break;
985 case NEXTHOP_TYPE_IFINDEX:
986 vty_out(vty, " is directly connected, %s",
987 ifindex2ifname_per_ns(zns, nexthop->ifindex));
988 break;
989 case NEXTHOP_TYPE_BLACKHOLE:
990 vty_out(vty, " is directly connected, Null0");
991 break;
992 default:
993 break;
994 }
995 vty_out(vty, "\n");
996 }
997
998 static void print_rnh(struct route_node *rn, struct vty *vty)
999 {
1000 struct rnh *rnh;
1001 struct nexthop *nexthop;
1002 struct listnode *node;
1003 struct zserv *client;
1004 char buf[BUFSIZ];
1005
1006 rnh = rn->info;
1007 vty_out(vty, "%s%s\n",
1008 inet_ntop(rn->p.family, &rn->p.u.prefix, buf, BUFSIZ),
1009 CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED) ? "(Connected)"
1010 : "");
1011 if (rnh->state) {
1012 vty_out(vty, " resolved via %s\n",
1013 zebra_route_string(rnh->state->type));
1014 for (nexthop = rnh->state->ng.nexthop; nexthop;
1015 nexthop = nexthop->next)
1016 print_nh(nexthop, vty);
1017 } else
1018 vty_out(vty, " unresolved%s\n",
1019 CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED)
1020 ? "(Connected)"
1021 : "");
1022
1023 vty_out(vty, " Client list:");
1024 for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client))
1025 vty_out(vty, " %s(fd %d)%s", zebra_route_string(client->proto),
1026 client->sock,
1027 rnh->filtered[client->proto] ? "(filtered)" : "");
1028 if (!list_isempty(rnh->zebra_pseudowire_list))
1029 vty_out(vty, " zebra[pseudowires]");
1030 vty_out(vty, "\n");
1031 }
1032
1033 static int zebra_cleanup_rnh_client(vrf_id_t vrf_id, int family,
1034 struct zserv *client, rnh_type_t type)
1035 {
1036 struct route_table *ntable;
1037 struct route_node *nrn;
1038 struct rnh *rnh;
1039
1040 if (IS_ZEBRA_DEBUG_NHT)
1041 zlog_debug("%u: Client %s RNH cleanup for family %d type %d",
1042 vrf_id, zebra_route_string(client->proto), family,
1043 type);
1044
1045 ntable = get_rnh_table(vrf_id, family, type);
1046 if (!ntable) {
1047 zlog_debug("cleanup_rnh_client: rnh table not found\n");
1048 return -1;
1049 }
1050
1051 for (nrn = route_top(ntable); nrn; nrn = route_next(nrn)) {
1052 if (!nrn->info)
1053 continue;
1054
1055 rnh = nrn->info;
1056 zebra_remove_rnh_client(rnh, client, type);
1057 }
1058 return 1;
1059 }
1060
1061 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
1062 static int zebra_client_cleanup_rnh(struct zserv *client)
1063 {
1064 struct vrf *vrf;
1065 struct zebra_vrf *zvrf;
1066
1067 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
1068 zvrf = vrf->info;
1069 if (zvrf) {
1070 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET, client,
1071 RNH_NEXTHOP_TYPE);
1072 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET6,
1073 client, RNH_NEXTHOP_TYPE);
1074 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET, client,
1075 RNH_IMPORT_CHECK_TYPE);
1076 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET6,
1077 client, RNH_IMPORT_CHECK_TYPE);
1078 if (client->proto == ZEBRA_ROUTE_LDP) {
1079 hash_iterate(zvrf->lsp_table,
1080 mpls_ldp_lsp_uninstall_all,
1081 zvrf->lsp_table);
1082 mpls_ldp_ftn_uninstall_all(zvrf, AFI_IP);
1083 mpls_ldp_ftn_uninstall_all(zvrf, AFI_IP6);
1084 }
1085 }
1086 }
1087
1088 return 0;
1089 }