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