]>
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 | 38 | |
89272910 | 39 | #include "zebra/zebra_router.h" |
fb018d25 DS |
40 | #include "zebra/rib.h" |
41 | #include "zebra/rt.h" | |
42 | #include "zebra/zserv.h" | |
fe18ee2d | 43 | #include "zebra/zebra_ns.h" |
7c551956 | 44 | #include "zebra/zebra_vrf.h" |
fb018d25 DS |
45 | #include "zebra/redistribute.h" |
46 | #include "zebra/debug.h" | |
47 | #include "zebra/zebra_rnh.h" | |
8902474b | 48 | #include "zebra/zebra_routemap.h" |
a815b788 | 49 | #include "zebra/interface.h" |
4a1ab8e4 | 50 | #include "zebra/zebra_memory.h" |
43e52561 | 51 | #include "zebra/zebra_errors.h" |
fb018d25 | 52 | |
d62a17ae | 53 | static void free_state(vrf_id_t vrf_id, struct route_entry *re, |
54 | struct route_node *rn); | |
f0f77c9a | 55 | static void copy_state(struct rnh *rnh, struct route_entry *re, |
078430f6 | 56 | struct route_node *rn); |
f0f77c9a | 57 | static int compare_state(struct route_entry *r1, struct route_entry *r2); |
7076bb2f | 58 | static int send_client(struct rnh *rnh, struct zserv *client, rnh_type_t type, |
d62a17ae | 59 | vrf_id_t vrf_id); |
fb018d25 | 60 | static void print_rnh(struct route_node *rn, struct vty *vty); |
453844ab | 61 | static int zebra_client_cleanup_rnh(struct zserv *client); |
fb018d25 | 62 | |
a50b580a DS |
63 | int zebra_rnh_ip_default_route = 0; |
64 | int zebra_rnh_ipv6_default_route = 0; | |
65 | ||
453844ab QY |
66 | void zebra_rnh_init(void) |
67 | { | |
21ccc0cf | 68 | hook_register(zserv_client_close, zebra_client_cleanup_rnh); |
453844ab QY |
69 | } |
70 | ||
73bf60a0 | 71 | static inline struct route_table *get_rnh_table(vrf_id_t vrfid, afi_t afi, |
078430f6 DS |
72 | rnh_type_t type) |
73 | { | |
d62a17ae | 74 | struct zebra_vrf *zvrf; |
75 | struct route_table *t = NULL; | |
76 | ||
77 | zvrf = zebra_vrf_lookup_by_id(vrfid); | |
78 | if (zvrf) | |
79 | switch (type) { | |
80 | case RNH_NEXTHOP_TYPE: | |
73bf60a0 | 81 | t = zvrf->rnh_table[afi]; |
d62a17ae | 82 | break; |
83 | case RNH_IMPORT_CHECK_TYPE: | |
73bf60a0 | 84 | t = zvrf->import_check_table[afi]; |
d62a17ae | 85 | break; |
86 | } | |
87 | ||
88 | return t; | |
078430f6 DS |
89 | } |
90 | ||
d62a17ae | 91 | char *rnh_str(struct rnh *rnh, char *buf, int size) |
fb018d25 | 92 | { |
d62a17ae | 93 | prefix2str(&(rnh->node->p), buf, size); |
94 | return buf; | |
fb018d25 DS |
95 | } |
96 | ||
699dae23 DS |
97 | static void zebra_rnh_remove_from_routing_table(struct rnh *rnh) |
98 | { | |
99 | struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(rnh->vrf_id); | |
100 | struct route_table *table = zvrf->table[rnh->afi][SAFI_UNICAST]; | |
101 | struct route_node *rn; | |
102 | rib_dest_t *dest; | |
103 | ||
104 | if (!table) | |
105 | return; | |
106 | ||
107 | rn = route_node_match(table, &rnh->resolved_route); | |
108 | if (!rn) | |
109 | return; | |
110 | ||
50872b08 DS |
111 | if (IS_ZEBRA_DEBUG_NHT_DETAILED) { |
112 | char buf[PREFIX_STRLEN]; | |
113 | char buf1[PREFIX_STRLEN]; | |
114 | ||
115 | zlog_debug("%s: %u:%s removed from tracking on %s", | |
116 | __PRETTY_FUNCTION__, rnh->vrf_id, | |
117 | prefix2str(&rnh->node->p, buf, sizeof(buf)), | |
118 | srcdest_rnode2str(rn, buf1, sizeof(buf))); | |
119 | } | |
120 | ||
699dae23 | 121 | dest = rib_dest_from_rnode(rn); |
aa57abfb | 122 | rnh_list_del(&dest->nht, rnh); |
699dae23 DS |
123 | route_unlock_node(rn); |
124 | } | |
125 | ||
126 | static void zebra_rnh_store_in_routing_table(struct rnh *rnh) | |
127 | { | |
128 | struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(rnh->vrf_id); | |
129 | struct route_table *table = zvrf->table[rnh->afi][SAFI_UNICAST]; | |
130 | struct route_node *rn; | |
131 | rib_dest_t *dest; | |
132 | ||
133 | rn = route_node_match(table, &rnh->resolved_route); | |
134 | if (!rn) | |
135 | return; | |
136 | ||
50872b08 DS |
137 | if (IS_ZEBRA_DEBUG_NHT_DETAILED) { |
138 | char buf[PREFIX_STRLEN]; | |
139 | char buf1[PREFIX_STRLEN]; | |
140 | ||
141 | zlog_debug("%s: %u:%s added for tracking on %s", | |
142 | __PRETTY_FUNCTION__, rnh->vrf_id, | |
143 | prefix2str(&rnh->node->p, buf, sizeof(buf)), | |
144 | srcdest_rnode2str(rn, buf1, sizeof(buf))); | |
145 | } | |
146 | ||
699dae23 | 147 | dest = rib_dest_from_rnode(rn); |
aa57abfb | 148 | rnh_list_add_tail(&dest->nht, rnh); |
699dae23 DS |
149 | route_unlock_node(rn); |
150 | } | |
151 | ||
1d30d1f4 DS |
152 | struct rnh *zebra_add_rnh(struct prefix *p, vrf_id_t vrfid, rnh_type_t type, |
153 | bool *exists) | |
fb018d25 | 154 | { |
d62a17ae | 155 | struct route_table *table; |
156 | struct route_node *rn; | |
157 | struct rnh *rnh = NULL; | |
158 | char buf[PREFIX2STR_BUFFER]; | |
87554d83 | 159 | afi_t afi = family2afi(p->family); |
d62a17ae | 160 | |
161 | if (IS_ZEBRA_DEBUG_NHT) { | |
162 | prefix2str(p, buf, sizeof(buf)); | |
163 | zlog_debug("%u: Add RNH %s type %d", vrfid, buf, type); | |
164 | } | |
87554d83 | 165 | table = get_rnh_table(vrfid, afi, type); |
d62a17ae | 166 | if (!table) { |
167 | prefix2str(p, buf, sizeof(buf)); | |
e914ccbe | 168 | flog_warn(EC_ZEBRA_RNH_NO_TABLE, |
9df414fe | 169 | "%u: Add RNH %s type %d - table not found", vrfid, |
d62a17ae | 170 | buf, type); |
1d30d1f4 | 171 | exists = false; |
d62a17ae | 172 | return NULL; |
173 | } | |
174 | ||
175 | /* Make it sure prefixlen is applied to the prefix. */ | |
176 | apply_mask(p); | |
177 | ||
178 | /* Lookup (or add) route node.*/ | |
179 | rn = route_node_get(table, p); | |
180 | ||
181 | if (!rn->info) { | |
182 | rnh = XCALLOC(MTYPE_RNH, sizeof(struct rnh)); | |
a304e258 DS |
183 | |
184 | /* | |
185 | * The resolved route is already 0.0.0.0/0 or | |
186 | * 0::0/0 due to the calloc right above, but | |
187 | * we should set the family so that future | |
188 | * comparisons can just be done | |
189 | */ | |
190 | rnh->resolved_route.family = p->family; | |
d62a17ae | 191 | rnh->client_list = list_new(); |
192 | rnh->vrf_id = vrfid; | |
cead8cef | 193 | rnh->type = type; |
699dae23 | 194 | rnh->seqno = 0; |
87554d83 | 195 | rnh->afi = afi; |
731a75fe | 196 | rnh->zebra_pseudowire_list = list_new(); |
d62a17ae | 197 | route_lock_node(rn); |
198 | rn->info = rnh; | |
199 | rnh->node = rn; | |
1d30d1f4 | 200 | *exists = false; |
699dae23 DS |
201 | |
202 | zebra_rnh_store_in_routing_table(rnh); | |
1d30d1f4 DS |
203 | } else |
204 | *exists = true; | |
d62a17ae | 205 | |
206 | route_unlock_node(rn); | |
207 | return (rn->info); | |
fb018d25 DS |
208 | } |
209 | ||
d62a17ae | 210 | struct rnh *zebra_lookup_rnh(struct prefix *p, vrf_id_t vrfid, rnh_type_t type) |
fb018d25 | 211 | { |
d62a17ae | 212 | struct route_table *table; |
213 | struct route_node *rn; | |
fb018d25 | 214 | |
73bf60a0 | 215 | table = get_rnh_table(vrfid, family2afi(PREFIX_FAMILY(p)), type); |
d62a17ae | 216 | if (!table) |
217 | return NULL; | |
fb018d25 | 218 | |
d62a17ae | 219 | /* Make it sure prefixlen is applied to the prefix. */ |
220 | apply_mask(p); | |
fb018d25 | 221 | |
d62a17ae | 222 | /* Lookup route node.*/ |
223 | rn = route_node_lookup(table, p); | |
224 | if (!rn) | |
225 | return NULL; | |
fb018d25 | 226 | |
d62a17ae | 227 | route_unlock_node(rn); |
228 | return (rn->info); | |
fb018d25 DS |
229 | } |
230 | ||
d62a17ae | 231 | void zebra_free_rnh(struct rnh *rnh) |
5a8dfcd8 | 232 | { |
699dae23 DS |
233 | struct zebra_vrf *zvrf; |
234 | struct route_table *table; | |
235 | ||
236 | zebra_rnh_remove_from_routing_table(rnh); | |
d62a17ae | 237 | rnh->flags |= ZEBRA_NHT_DELETED; |
6a154c88 DL |
238 | list_delete(&rnh->client_list); |
239 | list_delete(&rnh->zebra_pseudowire_list); | |
699dae23 DS |
240 | |
241 | zvrf = zebra_vrf_lookup_by_id(rnh->vrf_id); | |
242 | table = zvrf->table[family2afi(rnh->resolved_route.family)][SAFI_UNICAST]; | |
243 | ||
244 | if (table) { | |
245 | struct route_node *rern; | |
246 | ||
247 | rern = route_node_match(table, &rnh->resolved_route); | |
248 | if (rern) { | |
249 | rib_dest_t *dest; | |
250 | ||
251 | route_unlock_node(rern); | |
252 | ||
253 | dest = rib_dest_from_rnode(rern); | |
aa57abfb | 254 | rnh_list_del(&dest->nht, rnh); |
699dae23 DS |
255 | } |
256 | } | |
d62a17ae | 257 | free_state(rnh->vrf_id, rnh->state, rnh->node); |
258 | XFREE(MTYPE_RNH, rnh); | |
5a8dfcd8 RW |
259 | } |
260 | ||
10b6a3ea | 261 | static void zebra_delete_rnh(struct rnh *rnh, rnh_type_t type) |
fb018d25 | 262 | { |
d62a17ae | 263 | struct route_node *rn; |
fb018d25 | 264 | |
8d6848dd DS |
265 | if (!list_isempty(rnh->client_list) |
266 | || !list_isempty(rnh->zebra_pseudowire_list)) | |
267 | return; | |
268 | ||
269 | if ((rnh->flags & ZEBRA_NHT_DELETED) || !(rn = rnh->node)) | |
d62a17ae | 270 | return; |
fb018d25 | 271 | |
d62a17ae | 272 | if (IS_ZEBRA_DEBUG_NHT) { |
273 | char buf[PREFIX2STR_BUFFER]; | |
274 | zlog_debug("%u: Del RNH %s type %d", rnh->vrf_id, | |
275 | rnh_str(rnh, buf, sizeof(buf)), type); | |
276 | } | |
fb018d25 | 277 | |
d62a17ae | 278 | zebra_free_rnh(rnh); |
279 | rn->info = NULL; | |
280 | route_unlock_node(rn); | |
fb018d25 DS |
281 | } |
282 | ||
1d30d1f4 DS |
283 | /* |
284 | * This code will send to the registering client | |
285 | * the looked up rnh. | |
286 | * For a rnh that was created, there is no data | |
287 | * so it will send an empty nexthop group | |
288 | * If rnh exists then we know it has been evaluated | |
289 | * and as such it will have a resolved rnh. | |
290 | */ | |
d62a17ae | 291 | void zebra_add_rnh_client(struct rnh *rnh, struct zserv *client, |
292 | rnh_type_t type, vrf_id_t vrf_id) | |
fb018d25 | 293 | { |
d62a17ae | 294 | if (IS_ZEBRA_DEBUG_NHT) { |
295 | char buf[PREFIX2STR_BUFFER]; | |
296 | zlog_debug("%u: Client %s registers for RNH %s type %d", vrf_id, | |
297 | zebra_route_string(client->proto), | |
298 | rnh_str(rnh, buf, sizeof(buf)), type); | |
299 | } | |
81446366 | 300 | if (!listnode_lookup(rnh->client_list, client)) |
d62a17ae | 301 | listnode_add(rnh->client_list, client); |
81446366 DS |
302 | |
303 | /* | |
304 | * We always need to respond with known information, | |
305 | * currently multiple daemons expect this behavior | |
306 | */ | |
307 | send_client(rnh, client, type, vrf_id); | |
fb018d25 DS |
308 | } |
309 | ||
d62a17ae | 310 | void zebra_remove_rnh_client(struct rnh *rnh, struct zserv *client, |
311 | rnh_type_t type) | |
fb018d25 | 312 | { |
d62a17ae | 313 | if (IS_ZEBRA_DEBUG_NHT) { |
314 | char buf[PREFIX2STR_BUFFER]; | |
315 | zlog_debug("Client %s unregisters for RNH %s type %d", | |
316 | zebra_route_string(client->proto), | |
317 | rnh_str(rnh, buf, sizeof(buf)), type); | |
318 | } | |
319 | listnode_delete(rnh->client_list, client); | |
8d6848dd | 320 | zebra_delete_rnh(rnh, type); |
6e26278c DS |
321 | } |
322 | ||
731a75fe RW |
323 | /* XXX move this utility function elsewhere? */ |
324 | static void addr2hostprefix(int af, const union g_addr *addr, | |
325 | struct prefix *prefix) | |
326 | { | |
327 | switch (af) { | |
328 | case AF_INET: | |
329 | prefix->family = AF_INET; | |
330 | prefix->prefixlen = IPV4_MAX_BITLEN; | |
331 | prefix->u.prefix4 = addr->ipv4; | |
332 | break; | |
333 | case AF_INET6: | |
334 | prefix->family = AF_INET6; | |
335 | prefix->prefixlen = IPV6_MAX_BITLEN; | |
336 | prefix->u.prefix6 = addr->ipv6; | |
337 | break; | |
338 | default: | |
c31a793b | 339 | memset(prefix, 0, sizeof(*prefix)); |
9df414fe | 340 | zlog_debug("%s: unknown address family %d", __func__, af); |
731a75fe RW |
341 | break; |
342 | } | |
343 | } | |
344 | ||
345 | void zebra_register_rnh_pseudowire(vrf_id_t vrf_id, struct zebra_pw *pw) | |
346 | { | |
347 | struct prefix nh; | |
348 | struct rnh *rnh; | |
1d30d1f4 | 349 | bool exists; |
6d53d7b1 | 350 | struct zebra_vrf *zvrf; |
351 | ||
352 | zvrf = vrf_info_lookup(vrf_id); | |
353 | if (!zvrf) | |
354 | return; | |
731a75fe RW |
355 | |
356 | addr2hostprefix(pw->af, &pw->nexthop, &nh); | |
1d30d1f4 | 357 | rnh = zebra_add_rnh(&nh, vrf_id, RNH_NEXTHOP_TYPE, &exists); |
731a75fe RW |
358 | if (rnh && !listnode_lookup(rnh->zebra_pseudowire_list, pw)) { |
359 | listnode_add(rnh->zebra_pseudowire_list, pw); | |
360 | pw->rnh = rnh; | |
73bf60a0 RW |
361 | zebra_evaluate_rnh(zvrf, family2afi(pw->af), 1, |
362 | RNH_NEXTHOP_TYPE, &nh); | |
731a75fe RW |
363 | } |
364 | } | |
365 | ||
366 | void zebra_deregister_rnh_pseudowire(vrf_id_t vrf_id, struct zebra_pw *pw) | |
367 | { | |
368 | struct rnh *rnh; | |
369 | ||
370 | rnh = pw->rnh; | |
371 | if (!rnh) | |
372 | return; | |
373 | ||
374 | listnode_delete(rnh->zebra_pseudowire_list, pw); | |
375 | pw->rnh = NULL; | |
376 | ||
8d6848dd | 377 | zebra_delete_rnh(rnh, RNH_NEXTHOP_TYPE); |
731a75fe RW |
378 | } |
379 | ||
e47c4d3c DS |
380 | /* Clear the NEXTHOP_FLAG_RNH_FILTERED flags on all nexthops |
381 | */ | |
382 | static void zebra_rnh_clear_nexthop_rnh_filters(struct route_entry *re) | |
383 | { | |
384 | struct nexthop *nexthop; | |
385 | ||
386 | if (re) { | |
387 | for (nexthop = re->ng.nexthop; nexthop; | |
388 | nexthop = nexthop->next) { | |
389 | UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_RNH_FILTERED); | |
390 | } | |
391 | } | |
392 | } | |
393 | ||
d50b5bdd | 394 | /* Apply the NHT route-map for a client to the route (and nexthops) |
395 | * resolving a NH. | |
396 | */ | |
73bf60a0 | 397 | static int zebra_rnh_apply_nht_rmap(afi_t afi, struct zebra_vrf *zvrf, |
6d53d7b1 | 398 | struct route_node *prn, |
d62a17ae | 399 | struct route_entry *re, int proto) |
6e26278c | 400 | { |
d62a17ae | 401 | int at_least_one = 0; |
d62a17ae | 402 | struct nexthop *nexthop; |
eadd1687 | 403 | route_map_result_t ret; |
d62a17ae | 404 | |
d62a17ae | 405 | if (prn && re) { |
7ee30f28 DS |
406 | for (nexthop = re->ng.nexthop; nexthop; |
407 | nexthop = nexthop->next) { | |
ac6eebce | 408 | ret = zebra_nht_route_map_check( |
73bf60a0 | 409 | afi, proto, &prn->p, zvrf, re, nexthop); |
e47c4d3c | 410 | if (ret != RMAP_DENYMATCH) |
d62a17ae | 411 | at_least_one++; /* at least one valid NH */ |
e47c4d3c DS |
412 | else { |
413 | SET_FLAG(nexthop->flags, | |
414 | NEXTHOP_FLAG_RNH_FILTERED); | |
d62a17ae | 415 | } |
416 | } | |
6e26278c | 417 | } |
d62a17ae | 418 | return (at_least_one); |
6e26278c DS |
419 | } |
420 | ||
d50b5bdd | 421 | /* |
fd7fd9e5 DS |
422 | * Determine appropriate route (RE entry) resolving a tracked BGP route |
423 | * for BGP route for import. | |
d50b5bdd | 424 | */ |
996c9314 | 425 | static struct route_entry * |
73bf60a0 | 426 | zebra_rnh_resolve_import_entry(struct zebra_vrf *zvrf, afi_t afi, |
996c9314 LB |
427 | struct route_node *nrn, struct rnh *rnh, |
428 | struct route_node **prn) | |
fb018d25 | 429 | { |
d62a17ae | 430 | struct route_table *route_table; |
431 | struct route_node *rn; | |
432 | struct route_entry *re; | |
433 | ||
434 | *prn = NULL; | |
435 | ||
73bf60a0 | 436 | route_table = zvrf->table[afi][SAFI_UNICAST]; |
d62a17ae | 437 | if (!route_table) // unexpected |
438 | return NULL; | |
439 | ||
440 | rn = route_node_match(route_table, &nrn->p); | |
441 | if (!rn) | |
442 | return NULL; | |
443 | ||
fd7fd9e5 DS |
444 | /* Unlock route node - we don't need to lock when walking the tree. */ |
445 | route_unlock_node(rn); | |
446 | ||
996c9314 LB |
447 | if (CHECK_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH) |
448 | && !prefix_same(&nrn->p, &rn->p)) | |
fd7fd9e5 | 449 | return NULL; |
d62a17ae | 450 | |
50872b08 DS |
451 | if (IS_ZEBRA_DEBUG_NHT_DETAILED) { |
452 | char buf[PREFIX_STRLEN]; | |
453 | char buf1[PREFIX_STRLEN]; | |
454 | ||
455 | zlog_debug("%s: %u:%s Resolved Import Entry to %s", | |
456 | __PRETTY_FUNCTION__, rnh->vrf_id, | |
457 | prefix2str(&rnh->node->p, buf, sizeof(buf)), | |
458 | srcdest_rnode2str(rn, buf1, sizeof(buf))); | |
459 | } | |
460 | ||
fd7fd9e5 | 461 | /* Identify appropriate route entry. */ |
996c9314 LB |
462 | RNODE_FOREACH_RE (rn, re) { |
463 | if (!CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED) | |
464 | && CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED) | |
465 | && (re->type != ZEBRA_ROUTE_BGP)) | |
fd7fd9e5 | 466 | break; |
d62a17ae | 467 | } |
468 | ||
d62a17ae | 469 | if (re) |
470 | *prn = rn; | |
50872b08 DS |
471 | |
472 | if (!re && IS_ZEBRA_DEBUG_NHT_DETAILED) | |
473 | zlog_debug("\tRejected due to removed or is a bgp route"); | |
474 | ||
d62a17ae | 475 | return re; |
d50b5bdd | 476 | } |
477 | ||
478 | /* | |
479 | * See if a tracked route entry for import (by BGP) has undergone any | |
480 | * change, and if so, notify the client. | |
481 | */ | |
735219e9 | 482 | static void zebra_rnh_eval_import_check_entry(struct zebra_vrf *zvrf, afi_t afi, |
d62a17ae | 483 | int force, struct route_node *nrn, |
484 | struct rnh *rnh, | |
735219e9 | 485 | struct route_node *prn, |
d62a17ae | 486 | struct route_entry *re) |
d50b5bdd | 487 | { |
d62a17ae | 488 | int state_changed = 0; |
489 | struct zserv *client; | |
490 | char bufn[INET6_ADDRSTRLEN]; | |
491 | struct listnode *node; | |
d62a17ae | 492 | |
699dae23 DS |
493 | zebra_rnh_remove_from_routing_table(rnh); |
494 | if (prn) { | |
a304e258 | 495 | prefix_copy(&rnh->resolved_route, &prn->p); |
699dae23 | 496 | } else { |
a304e258 DS |
497 | int family = rnh->resolved_route.family; |
498 | ||
499 | memset(&rnh->resolved_route.family, 0, sizeof(struct prefix)); | |
500 | rnh->resolved_route.family = family; | |
501 | } | |
699dae23 | 502 | zebra_rnh_store_in_routing_table(rnh); |
a304e258 | 503 | |
d62a17ae | 504 | if (re && (rnh->state == NULL)) { |
677c1dd5 DS |
505 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED)) |
506 | state_changed = 1; | |
d62a17ae | 507 | } else if (!re && (rnh->state != NULL)) |
508 | state_changed = 1; | |
509 | ||
a304e258 | 510 | if (compare_state(re, rnh->state)) { |
d62a17ae | 511 | copy_state(rnh, re, nrn); |
9cb8322e | 512 | state_changed = 1; |
a304e258 | 513 | } |
d62a17ae | 514 | |
515 | if (state_changed || force) { | |
516 | if (IS_ZEBRA_DEBUG_NHT) { | |
517 | prefix2str(&nrn->p, bufn, INET6_ADDRSTRLEN); | |
735219e9 DS |
518 | zlog_debug("%u:%s: Route import check %s %s", |
519 | zvrf->vrf->vrf_id, | |
d62a17ae | 520 | bufn, rnh->state ? "passed" : "failed", |
521 | state_changed ? "(state changed)" : ""); | |
522 | } | |
523 | /* state changed, notify clients */ | |
524 | for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client)) { | |
735219e9 DS |
525 | send_client(rnh, client, |
526 | RNH_IMPORT_CHECK_TYPE, zvrf->vrf->vrf_id); | |
d62a17ae | 527 | } |
528 | } | |
d50b5bdd | 529 | } |
fb018d25 | 530 | |
d50b5bdd | 531 | /* |
532 | * Notify clients registered for this nexthop about a change. | |
533 | */ | |
73bf60a0 RW |
534 | static void zebra_rnh_notify_protocol_clients(struct zebra_vrf *zvrf, afi_t afi, |
535 | struct route_node *nrn, | |
536 | struct rnh *rnh, | |
537 | struct route_node *prn, | |
538 | struct route_entry *re) | |
d50b5bdd | 539 | { |
d62a17ae | 540 | struct listnode *node; |
541 | struct zserv *client; | |
542 | char bufn[INET6_ADDRSTRLEN]; | |
543 | char bufp[INET6_ADDRSTRLEN]; | |
544 | int num_resolving_nh; | |
545 | ||
546 | if (IS_ZEBRA_DEBUG_NHT) { | |
547 | prefix2str(&nrn->p, bufn, INET6_ADDRSTRLEN); | |
548 | if (prn && re) { | |
50872b08 | 549 | srcdest_rnode2str(prn, bufp, INET6_ADDRSTRLEN); |
6d53d7b1 | 550 | zlog_debug("%u:%s: NH resolved over route %s", |
551 | zvrf->vrf->vrf_id, bufn, bufp); | |
d62a17ae | 552 | } else |
6d53d7b1 | 553 | zlog_debug("%u:%s: NH has become unresolved", |
554 | zvrf->vrf->vrf_id, bufn); | |
d62a17ae | 555 | } |
556 | ||
557 | for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client)) { | |
558 | if (prn && re) { | |
559 | /* Apply route-map for this client to route resolving | |
560 | * this | |
561 | * nexthop to see if it is filtered or not. | |
562 | */ | |
e47c4d3c | 563 | zebra_rnh_clear_nexthop_rnh_filters(re); |
d62a17ae | 564 | num_resolving_nh = zebra_rnh_apply_nht_rmap( |
73bf60a0 | 565 | afi, zvrf, prn, re, client->proto); |
d62a17ae | 566 | if (num_resolving_nh) |
567 | rnh->filtered[client->proto] = 0; | |
568 | else | |
569 | rnh->filtered[client->proto] = 1; | |
570 | ||
571 | if (IS_ZEBRA_DEBUG_NHT) | |
572 | zlog_debug( | |
573 | "%u:%s: Notifying client %s about NH %s", | |
6d53d7b1 | 574 | zvrf->vrf->vrf_id, bufn, |
d62a17ae | 575 | zebra_route_string(client->proto), |
576 | num_resolving_nh | |
577 | ? "" | |
578 | : "(filtered by route-map)"); | |
579 | } else { | |
580 | rnh->filtered[client->proto] = 0; | |
581 | if (IS_ZEBRA_DEBUG_NHT) | |
582 | zlog_debug( | |
583 | "%u:%s: Notifying client %s about NH (unreachable)", | |
6d53d7b1 | 584 | zvrf->vrf->vrf_id, bufn, |
d62a17ae | 585 | zebra_route_string(client->proto)); |
586 | } | |
587 | ||
6d53d7b1 | 588 | send_client(rnh, client, RNH_NEXTHOP_TYPE, zvrf->vrf->vrf_id); |
d62a17ae | 589 | } |
e47c4d3c DS |
590 | |
591 | if (re) | |
592 | zebra_rnh_clear_nexthop_rnh_filters(re); | |
d50b5bdd | 593 | } |
078430f6 | 594 | |
73bf60a0 | 595 | static void zebra_rnh_process_pbr_tables(afi_t afi, struct route_node *nrn, |
d61d5d88 DS |
596 | struct rnh *rnh, |
597 | struct route_node *prn, | |
598 | struct route_entry *re) | |
599 | { | |
89272910 | 600 | struct zebra_router_table *zrt; |
d61d5d88 DS |
601 | struct route_entry *o_re; |
602 | struct route_node *o_rn; | |
603 | struct listnode *node; | |
604 | struct zserv *client; | |
d61d5d88 DS |
605 | |
606 | /* | |
607 | * We are only concerned about nexthops that change for | |
608 | * anyone using PBR | |
609 | */ | |
610 | for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client)) { | |
611 | if (client->proto == ZEBRA_ROUTE_PBR) | |
612 | break; | |
613 | } | |
614 | ||
615 | if (!client) | |
616 | return; | |
617 | ||
89272910 DS |
618 | RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) { |
619 | if (afi != zrt->afi) | |
d61d5d88 DS |
620 | continue; |
621 | ||
89272910 DS |
622 | for (o_rn = route_top(zrt->table); o_rn; |
623 | o_rn = srcdest_route_next(o_rn)) { | |
d61d5d88 DS |
624 | RNODE_FOREACH_RE (o_rn, o_re) { |
625 | if (o_re->type == ZEBRA_ROUTE_PBR) | |
626 | break; | |
627 | ||
628 | } | |
629 | ||
630 | /* | |
631 | * If we have a PBR route and a nexthop changes | |
632 | * just rethink it. Yes this is a hammer, but | |
633 | * a small one | |
634 | */ | |
4d96fd9f DS |
635 | if (o_re) { |
636 | SET_FLAG(o_re->status, ROUTE_ENTRY_CHANGED); | |
d61d5d88 | 637 | rib_queue_add(o_rn); |
4d96fd9f | 638 | } |
d61d5d88 DS |
639 | } |
640 | } | |
641 | } | |
642 | ||
4dfd7a02 MS |
643 | /* |
644 | * Utility to determine whether a candidate nexthop is useable. We make this | |
645 | * check in a couple of places, so this is a single home for the logic we | |
646 | * use. | |
647 | */ | |
677c1dd5 DS |
648 | static bool rnh_nexthop_valid(const struct route_entry *re, |
649 | const struct nexthop *nh) | |
4dfd7a02 | 650 | { |
677c1dd5 | 651 | return (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED) |
e47c4d3c DS |
652 | && CHECK_FLAG(nh->flags, NEXTHOP_FLAG_ACTIVE) |
653 | && !CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE) | |
654 | && !CHECK_FLAG(nh->flags, NEXTHOP_FLAG_DUPLICATE) | |
655 | && !CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RNH_FILTERED)); | |
4dfd7a02 MS |
656 | } |
657 | ||
fd7fd9e5 DS |
658 | /* |
659 | * Determine appropriate route (route entry) resolving a tracked | |
660 | * nexthop. | |
661 | */ | |
996c9314 | 662 | static struct route_entry * |
73bf60a0 | 663 | zebra_rnh_resolve_nexthop_entry(struct zebra_vrf *zvrf, afi_t afi, |
996c9314 LB |
664 | struct route_node *nrn, struct rnh *rnh, |
665 | struct route_node **prn) | |
fd7fd9e5 DS |
666 | { |
667 | struct route_table *route_table; | |
668 | struct route_node *rn; | |
669 | struct route_entry *re; | |
f183e380 | 670 | struct nexthop *nexthop; |
fd7fd9e5 DS |
671 | |
672 | *prn = NULL; | |
673 | ||
73bf60a0 | 674 | route_table = zvrf->table[afi][SAFI_UNICAST]; |
fd7fd9e5 DS |
675 | if (!route_table) |
676 | return NULL; | |
677 | ||
678 | rn = route_node_match(route_table, &nrn->p); | |
679 | if (!rn) | |
680 | return NULL; | |
681 | ||
682 | /* Unlock route node - we don't need to lock when walking the tree. */ | |
683 | route_unlock_node(rn); | |
684 | ||
685 | /* While resolving nexthops, we may need to walk up the tree from the | |
686 | * most-specific match. Do similar logic as in zebra_rib.c | |
687 | */ | |
688 | while (rn) { | |
50872b08 DS |
689 | if (IS_ZEBRA_DEBUG_NHT_DETAILED) { |
690 | char buf[PREFIX_STRLEN]; | |
691 | char buf1[PREFIX_STRLEN]; | |
692 | ||
693 | zlog_debug("%s: %u:%s Possible Match to %s", | |
694 | __PRETTY_FUNCTION__, rnh->vrf_id, | |
695 | prefix2str(&rnh->node->p, buf, sizeof(buf)), | |
696 | srcdest_rnode2str(rn, buf1, sizeof(buf))); | |
697 | } | |
698 | ||
fd7fd9e5 DS |
699 | /* Do not resolve over default route unless allowed && |
700 | * match route to be exact if so specified | |
701 | */ | |
996c9314 | 702 | if (is_default_prefix(&rn->p) |
50872b08 DS |
703 | && !rnh_resolve_via_default(rn->p.family)) { |
704 | if (IS_ZEBRA_DEBUG_NHT_DETAILED) | |
705 | zlog_debug( | |
706 | "\tNot allowed to resolve through default prefix"); | |
fd7fd9e5 | 707 | return NULL; |
50872b08 | 708 | } |
fd7fd9e5 DS |
709 | |
710 | /* Identify appropriate route entry. */ | |
996c9314 | 711 | RNODE_FOREACH_RE (rn, re) { |
50872b08 DS |
712 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) { |
713 | if (IS_ZEBRA_DEBUG_NHT_DETAILED) | |
714 | zlog_debug( | |
715 | "\tRoute Entry %s removed", | |
716 | zebra_route_string(re->type)); | |
fd7fd9e5 | 717 | continue; |
50872b08 DS |
718 | } |
719 | if (!CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)) { | |
720 | if (IS_ZEBRA_DEBUG_NHT_DETAILED) | |
721 | zlog_debug( | |
722 | "\tRoute Entry %s !selected", | |
723 | zebra_route_string(re->type)); | |
fd7fd9e5 | 724 | continue; |
50872b08 | 725 | } |
fd7fd9e5 | 726 | |
f183e380 MS |
727 | /* Just being SELECTED isn't quite enough - must |
728 | * have an installed nexthop to be useful. | |
729 | */ | |
72366a6e | 730 | for (ALL_NEXTHOPS(re->ng, nexthop)) { |
677c1dd5 | 731 | if (rnh_nexthop_valid(re, nexthop)) |
f183e380 | 732 | break; |
4dfd7a02 | 733 | } |
f183e380 | 734 | |
50872b08 DS |
735 | if (nexthop == NULL) { |
736 | if (IS_ZEBRA_DEBUG_NHT_DETAILED) | |
737 | zlog_debug( | |
738 | "\tRoute Entry %s no nexthops", | |
739 | zebra_route_string(re->type)); | |
f183e380 | 740 | continue; |
50872b08 | 741 | } |
f183e380 | 742 | |
fd7fd9e5 DS |
743 | if (CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED)) { |
744 | if ((re->type == ZEBRA_ROUTE_CONNECT) | |
745 | || (re->type == ZEBRA_ROUTE_STATIC)) | |
746 | break; | |
747 | if (re->type == ZEBRA_ROUTE_NHRP) { | |
fd7fd9e5 | 748 | |
7ee30f28 | 749 | for (nexthop = re->ng.nexthop; nexthop; |
fd7fd9e5 DS |
750 | nexthop = nexthop->next) |
751 | if (nexthop->type | |
996c9314 | 752 | == NEXTHOP_TYPE_IFINDEX) |
fd7fd9e5 DS |
753 | break; |
754 | if (nexthop) | |
755 | break; | |
756 | } | |
757 | } else | |
758 | break; | |
759 | } | |
760 | ||
761 | /* Route entry found, we're done; else, walk up the tree. */ | |
762 | if (re) { | |
763 | *prn = rn; | |
764 | return re; | |
765 | } | |
766 | ||
699dae23 | 767 | if (!CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED)) |
fd7fd9e5 | 768 | rn = rn->parent; |
50872b08 DS |
769 | else { |
770 | if (IS_ZEBRA_DEBUG_NHT_DETAILED) | |
771 | zlog_debug( | |
772 | "\tNexthop must be connected, cannot recurse up"); | |
fd7fd9e5 | 773 | return NULL; |
50872b08 | 774 | } |
fd7fd9e5 DS |
775 | } |
776 | ||
777 | return NULL; | |
778 | } | |
779 | ||
731a75fe RW |
780 | static void zebra_rnh_process_pseudowires(vrf_id_t vrfid, struct rnh *rnh) |
781 | { | |
782 | struct zebra_pw *pw; | |
783 | struct listnode *node; | |
784 | ||
785 | for (ALL_LIST_ELEMENTS_RO(rnh->zebra_pseudowire_list, node, pw)) | |
786 | zebra_pw_update(pw); | |
787 | } | |
788 | ||
d50b5bdd | 789 | /* |
790 | * See if a tracked nexthop entry has undergone any change, and if so, | |
791 | * take appropriate action; this involves notifying any clients and/or | |
792 | * scheduling dependent static routes for processing. | |
793 | */ | |
73bf60a0 | 794 | static void zebra_rnh_eval_nexthop_entry(struct zebra_vrf *zvrf, afi_t afi, |
6d53d7b1 | 795 | int force, struct route_node *nrn, |
d62a17ae | 796 | struct rnh *rnh, |
797 | struct route_node *prn, | |
798 | struct route_entry *re) | |
d50b5bdd | 799 | { |
d62a17ae | 800 | int state_changed = 0; |
801 | ||
802 | /* If we're resolving over a different route, resolution has changed or | |
803 | * the resolving route has some change (e.g., metric), there is a state | |
804 | * change. | |
805 | */ | |
699dae23 | 806 | zebra_rnh_remove_from_routing_table(rnh); |
27d0665c | 807 | if (!prefix_same(&rnh->resolved_route, prn ? NULL : &prn->p)) { |
d62a17ae | 808 | if (prn) |
809 | prefix_copy(&rnh->resolved_route, &prn->p); | |
a304e258 DS |
810 | else { |
811 | /* | |
812 | * Just quickly store the family of the resolved | |
813 | * route so that we can reset it in a second here | |
814 | */ | |
815 | int family = rnh->resolved_route.family; | |
816 | ||
d62a17ae | 817 | memset(&rnh->resolved_route, 0, sizeof(struct prefix)); |
a304e258 DS |
818 | rnh->resolved_route.family = family; |
819 | } | |
d62a17ae | 820 | |
821 | copy_state(rnh, re, nrn); | |
822 | state_changed = 1; | |
823 | } else if (compare_state(re, rnh->state)) { | |
824 | copy_state(rnh, re, nrn); | |
825 | state_changed = 1; | |
826 | } | |
699dae23 | 827 | zebra_rnh_store_in_routing_table(rnh); |
d62a17ae | 828 | |
829 | if (state_changed || force) { | |
830 | /* NOTE: Use the "copy" of resolving route stored in 'rnh' i.e., | |
831 | * rnh->state. | |
832 | */ | |
833 | /* Notify registered protocol clients. */ | |
73bf60a0 | 834 | zebra_rnh_notify_protocol_clients(zvrf, afi, nrn, rnh, prn, |
d62a17ae | 835 | rnh->state); |
836 | ||
73bf60a0 | 837 | zebra_rnh_process_pbr_tables(afi, nrn, rnh, prn, rnh->state); |
d61d5d88 | 838 | |
731a75fe | 839 | /* Process pseudowires attached to this nexthop */ |
6d53d7b1 | 840 | zebra_rnh_process_pseudowires(zvrf->vrf->vrf_id, rnh); |
d62a17ae | 841 | } |
d50b5bdd | 842 | } |
6e26278c | 843 | |
d50b5bdd | 844 | /* Evaluate one tracked entry */ |
73bf60a0 | 845 | static void zebra_rnh_evaluate_entry(struct zebra_vrf *zvrf, afi_t afi, |
6d53d7b1 | 846 | int force, rnh_type_t type, |
847 | struct route_node *nrn) | |
d50b5bdd | 848 | { |
d62a17ae | 849 | struct rnh *rnh; |
850 | struct route_entry *re; | |
851 | struct route_node *prn; | |
852 | char bufn[INET6_ADDRSTRLEN]; | |
853 | ||
854 | if (IS_ZEBRA_DEBUG_NHT) { | |
855 | prefix2str(&nrn->p, bufn, INET6_ADDRSTRLEN); | |
6d53d7b1 | 856 | zlog_debug("%u:%s: Evaluate RNH, type %d %s", zvrf->vrf->vrf_id, |
857 | bufn, type, force ? "(force)" : ""); | |
d62a17ae | 858 | } |
859 | ||
860 | rnh = nrn->info; | |
861 | ||
862 | /* Identify route entry (RE) resolving this tracked entry. */ | |
fd7fd9e5 | 863 | if (type == RNH_IMPORT_CHECK_TYPE) |
73bf60a0 | 864 | re = zebra_rnh_resolve_import_entry(zvrf, afi, nrn, rnh, &prn); |
fd7fd9e5 | 865 | else |
73bf60a0 | 866 | re = zebra_rnh_resolve_nexthop_entry(zvrf, afi, nrn, rnh, &prn); |
d62a17ae | 867 | |
868 | /* If the entry cannot be resolved and that is also the existing state, | |
869 | * there is nothing further to do. | |
870 | */ | |
871 | if (!re && rnh->state == NULL && !force) | |
872 | return; | |
873 | ||
874 | /* Process based on type of entry. */ | |
875 | if (type == RNH_IMPORT_CHECK_TYPE) | |
735219e9 DS |
876 | zebra_rnh_eval_import_check_entry(zvrf, afi, force, nrn, rnh, |
877 | prn, re); | |
d62a17ae | 878 | else |
73bf60a0 | 879 | zebra_rnh_eval_nexthop_entry(zvrf, afi, force, nrn, rnh, prn, |
6d53d7b1 | 880 | re); |
d50b5bdd | 881 | } |
882 | ||
8ce5e6a3 | 883 | /* |
f0f77c9a DS |
884 | * Clear the ROUTE_ENTRY_NEXTHOPS_CHANGED flag |
885 | * from the re entries. | |
8ce5e6a3 DS |
886 | * |
887 | * Please note we are doing this *after* we have | |
888 | * notified the world about each nexthop as that | |
f0f77c9a | 889 | * we can have a situation where one re entry |
8ce5e6a3 DS |
890 | * covers multiple nexthops we are interested in. |
891 | */ | |
73bf60a0 | 892 | static void zebra_rnh_clear_nhc_flag(struct zebra_vrf *zvrf, afi_t afi, |
d62a17ae | 893 | rnh_type_t type, struct route_node *nrn) |
8ce5e6a3 | 894 | { |
d62a17ae | 895 | struct rnh *rnh; |
896 | struct route_entry *re; | |
897 | struct route_node *prn; | |
8ce5e6a3 | 898 | |
d62a17ae | 899 | rnh = nrn->info; |
8ce5e6a3 | 900 | |
fd7fd9e5 DS |
901 | /* Identify route entry (RIB) resolving this tracked entry. */ |
902 | if (type == RNH_IMPORT_CHECK_TYPE) | |
73bf60a0 | 903 | re = zebra_rnh_resolve_import_entry(zvrf, afi, nrn, rnh, |
996c9314 | 904 | &prn); |
fd7fd9e5 | 905 | else |
73bf60a0 | 906 | re = zebra_rnh_resolve_nexthop_entry(zvrf, afi, nrn, rnh, |
fd7fd9e5 | 907 | &prn); |
8ce5e6a3 | 908 | |
332ad713 | 909 | if (re) { |
d62a17ae | 910 | UNSET_FLAG(re->status, ROUTE_ENTRY_NEXTHOPS_CHANGED); |
332ad713 RW |
911 | UNSET_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED); |
912 | } | |
8ce5e6a3 | 913 | } |
d50b5bdd | 914 | |
915 | /* Evaluate all tracked entries (nexthops or routes for import into BGP) | |
916 | * of a particular VRF and address-family or a specific prefix. | |
917 | */ | |
73bf60a0 | 918 | void zebra_evaluate_rnh(struct zebra_vrf *zvrf, afi_t afi, int force, |
6d53d7b1 | 919 | rnh_type_t type, struct prefix *p) |
d50b5bdd | 920 | { |
d62a17ae | 921 | struct route_table *rnh_table; |
922 | struct route_node *nrn; | |
923 | ||
73bf60a0 | 924 | rnh_table = get_rnh_table(zvrf->vrf->vrf_id, afi, type); |
d62a17ae | 925 | if (!rnh_table) // unexpected |
926 | return; | |
927 | ||
928 | if (p) { | |
929 | /* Evaluating a specific entry, make sure it exists. */ | |
930 | nrn = route_node_lookup(rnh_table, p); | |
931 | if (nrn && nrn->info) | |
73bf60a0 | 932 | zebra_rnh_evaluate_entry(zvrf, afi, force, type, nrn); |
d62a17ae | 933 | |
934 | if (nrn) | |
935 | route_unlock_node(nrn); | |
936 | } else { | |
937 | /* Evaluate entire table. */ | |
938 | nrn = route_top(rnh_table); | |
939 | while (nrn) { | |
940 | if (nrn->info) | |
73bf60a0 RW |
941 | zebra_rnh_evaluate_entry(zvrf, afi, force, type, |
942 | nrn); | |
d62a17ae | 943 | nrn = route_next(nrn); /* this will also unlock nrn */ |
944 | } | |
945 | nrn = route_top(rnh_table); | |
946 | while (nrn) { | |
947 | if (nrn->info) | |
73bf60a0 | 948 | zebra_rnh_clear_nhc_flag(zvrf, afi, type, nrn); |
d62a17ae | 949 | nrn = route_next(nrn); /* this will also unlock nrn */ |
950 | } | |
951 | } | |
fb018d25 DS |
952 | } |
953 | ||
73bf60a0 | 954 | void zebra_print_rnh_table(vrf_id_t vrfid, afi_t afi, struct vty *vty, |
dbeca484 | 955 | rnh_type_t type, struct prefix *p) |
fb018d25 | 956 | { |
d62a17ae | 957 | struct route_table *table; |
958 | struct route_node *rn; | |
959 | ||
73bf60a0 | 960 | table = get_rnh_table(vrfid, afi, type); |
d62a17ae | 961 | if (!table) { |
9165c5f5 | 962 | zlog_debug("print_rnhs: rnh table not found"); |
d62a17ae | 963 | return; |
964 | } | |
965 | ||
dbeca484 DS |
966 | for (rn = route_top(table); rn; rn = route_next(rn)) { |
967 | if (p && prefix_cmp(&rn->p, p) != 0) | |
968 | continue; | |
969 | ||
d62a17ae | 970 | if (rn->info) |
971 | print_rnh(rn, vty); | |
dbeca484 | 972 | } |
fb018d25 DS |
973 | } |
974 | ||
fb018d25 | 975 | /** |
f0f77c9a | 976 | * free_state - free up the re structure associated with the rnh. |
fb018d25 | 977 | */ |
d62a17ae | 978 | static void free_state(vrf_id_t vrf_id, struct route_entry *re, |
979 | struct route_node *rn) | |
fb018d25 | 980 | { |
d62a17ae | 981 | if (!re) |
982 | return; | |
fb018d25 | 983 | |
d62a17ae | 984 | /* free RE and nexthops */ |
7ee30f28 | 985 | nexthops_free(re->ng.nexthop); |
d62a17ae | 986 | XFREE(MTYPE_RE, re); |
fb018d25 DS |
987 | } |
988 | ||
d62a17ae | 989 | static void copy_state(struct rnh *rnh, struct route_entry *re, |
990 | struct route_node *rn) | |
fb018d25 | 991 | { |
d62a17ae | 992 | struct route_entry *state; |
fb018d25 | 993 | |
d62a17ae | 994 | if (rnh->state) { |
995 | free_state(rnh->vrf_id, rnh->state, rn); | |
996 | rnh->state = NULL; | |
997 | } | |
fb018d25 | 998 | |
d62a17ae | 999 | if (!re) |
1000 | return; | |
fb018d25 | 1001 | |
d62a17ae | 1002 | state = XCALLOC(MTYPE_RE, sizeof(struct route_entry)); |
1003 | state->type = re->type; | |
7733c6c4 | 1004 | state->distance = re->distance; |
d62a17ae | 1005 | state->metric = re->metric; |
8f43b4d8 | 1006 | state->vrf_id = re->vrf_id; |
677c1dd5 | 1007 | state->status = re->status; |
fb018d25 | 1008 | |
7ee30f28 | 1009 | route_entry_copy_nexthops(state, re->ng.nexthop); |
d62a17ae | 1010 | rnh->state = state; |
fb018d25 DS |
1011 | } |
1012 | ||
d62a17ae | 1013 | static int compare_state(struct route_entry *r1, struct route_entry *r2) |
fb018d25 | 1014 | { |
fb018d25 | 1015 | |
d62a17ae | 1016 | if (!r1 && !r2) |
1017 | return 0; | |
fb018d25 | 1018 | |
d62a17ae | 1019 | if ((!r1 && r2) || (r1 && !r2)) |
1020 | return 1; | |
fb018d25 | 1021 | |
7733c6c4 JB |
1022 | if (r1->distance != r2->distance) |
1023 | return 1; | |
1024 | ||
d62a17ae | 1025 | if (r1->metric != r2->metric) |
1026 | return 1; | |
fb018d25 | 1027 | |
d62a17ae | 1028 | if (r1->nexthop_num != r2->nexthop_num) |
1029 | return 1; | |
fb018d25 | 1030 | |
332ad713 RW |
1031 | if (CHECK_FLAG(r1->status, ROUTE_ENTRY_NEXTHOPS_CHANGED) |
1032 | || CHECK_FLAG(r1->status, ROUTE_ENTRY_LABELS_CHANGED)) | |
d62a17ae | 1033 | return 1; |
fb018d25 | 1034 | |
d62a17ae | 1035 | return 0; |
fb018d25 DS |
1036 | } |
1037 | ||
d62a17ae | 1038 | static int send_client(struct rnh *rnh, struct zserv *client, rnh_type_t type, |
1039 | vrf_id_t vrf_id) | |
fb018d25 | 1040 | { |
d62a17ae | 1041 | struct stream *s; |
1042 | struct route_entry *re; | |
1043 | unsigned long nump; | |
d7c0a89a | 1044 | uint8_t num; |
0acf4df0 | 1045 | struct nexthop *nh; |
d62a17ae | 1046 | struct route_node *rn; |
1047 | int cmd = (type == RNH_IMPORT_CHECK_TYPE) ? ZEBRA_IMPORT_CHECK_UPDATE | |
1048 | : ZEBRA_NEXTHOP_UPDATE; | |
1049 | ||
1050 | rn = rnh->node; | |
1051 | re = rnh->state; | |
1052 | ||
1053 | /* Get output stream. */ | |
1002497a | 1054 | s = stream_new(ZEBRA_MAX_PACKET_SIZ); |
d62a17ae | 1055 | |
7cf15b25 | 1056 | zclient_create_header(s, cmd, vrf_id); |
d62a17ae | 1057 | |
1058 | stream_putw(s, rn->p.family); | |
1059 | switch (rn->p.family) { | |
1060 | case AF_INET: | |
1061 | stream_putc(s, rn->p.prefixlen); | |
1062 | stream_put_in_addr(s, &rn->p.u.prefix4); | |
fb018d25 | 1063 | break; |
d62a17ae | 1064 | case AF_INET6: |
1065 | stream_putc(s, rn->p.prefixlen); | |
1066 | stream_put(s, &rn->p.u.prefix6, IPV6_MAX_BYTELEN); | |
fb018d25 | 1067 | break; |
d62a17ae | 1068 | default: |
e914ccbe | 1069 | flog_err(EC_ZEBRA_RNH_UNKNOWN_FAMILY, |
1c50c1c0 QY |
1070 | "%s: Unknown family (%d) notification attempted\n", |
1071 | __FUNCTION__, rn->p.family); | |
fb018d25 | 1072 | break; |
d62a17ae | 1073 | } |
1074 | if (re) { | |
05dd5aaf DS |
1075 | stream_putc(s, re->type); |
1076 | stream_putw(s, re->instance); | |
d62a17ae | 1077 | stream_putc(s, re->distance); |
1078 | stream_putl(s, re->metric); | |
1079 | num = 0; | |
1080 | nump = stream_get_endp(s); | |
1081 | stream_putc(s, 0); | |
72366a6e | 1082 | for (ALL_NEXTHOPS(re->ng, nh)) |
677c1dd5 | 1083 | if (rnh_nexthop_valid(re, nh)) { |
b6c9de3b | 1084 | stream_putl(s, nh->vrf_id); |
0acf4df0 DS |
1085 | stream_putc(s, nh->type); |
1086 | switch (nh->type) { | |
d62a17ae | 1087 | case NEXTHOP_TYPE_IPV4: |
aab09c10 | 1088 | case NEXTHOP_TYPE_IPV4_IFINDEX: |
0acf4df0 DS |
1089 | stream_put_in_addr(s, &nh->gate.ipv4); |
1090 | stream_putl(s, nh->ifindex); | |
d62a17ae | 1091 | break; |
1092 | case NEXTHOP_TYPE_IFINDEX: | |
0acf4df0 | 1093 | stream_putl(s, nh->ifindex); |
d62a17ae | 1094 | break; |
d62a17ae | 1095 | case NEXTHOP_TYPE_IPV6: |
d62a17ae | 1096 | case NEXTHOP_TYPE_IPV6_IFINDEX: |
0acf4df0 DS |
1097 | stream_put(s, &nh->gate.ipv6, 16); |
1098 | stream_putl(s, nh->ifindex); | |
d62a17ae | 1099 | break; |
1100 | default: | |
1101 | /* do nothing */ | |
1102 | break; | |
1103 | } | |
0acf4df0 DS |
1104 | if (nh->nh_label) { |
1105 | stream_putc(s, | |
1106 | nh->nh_label->num_labels); | |
1107 | if (nh->nh_label->num_labels) | |
1108 | stream_put( | |
1109 | s, | |
1110 | &nh->nh_label->label[0], | |
1111 | nh->nh_label->num_labels | |
1112 | * sizeof(mpls_label_t)); | |
1113 | } else | |
1114 | stream_putc(s, 0); | |
d62a17ae | 1115 | num++; |
1116 | } | |
1117 | stream_putc_at(s, nump, num); | |
1118 | } else { | |
05dd5aaf DS |
1119 | stream_putc(s, 0); // type |
1120 | stream_putw(s, 0); // instance | |
d62a17ae | 1121 | stream_putc(s, 0); // distance |
1122 | stream_putl(s, 0); // metric | |
1123 | stream_putc(s, 0); // nexthops | |
1124 | } | |
1125 | stream_putw_at(s, 0, stream_get_endp(s)); | |
1126 | ||
1127 | client->nh_last_upd_time = monotime(NULL); | |
1128 | client->last_write_cmd = cmd; | |
21ccc0cf | 1129 | return zserv_send_message(client, s); |
fb018d25 DS |
1130 | } |
1131 | ||
d62a17ae | 1132 | static void print_nh(struct nexthop *nexthop, struct vty *vty) |
fb018d25 | 1133 | { |
d62a17ae | 1134 | char buf[BUFSIZ]; |
1135 | struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT); | |
1136 | ||
1137 | switch (nexthop->type) { | |
1138 | case NEXTHOP_TYPE_IPV4: | |
1139 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
1140 | vty_out(vty, " via %s", inet_ntoa(nexthop->gate.ipv4)); | |
1141 | if (nexthop->ifindex) | |
1142 | vty_out(vty, ", %s", | |
1143 | ifindex2ifname_per_ns(zns, nexthop->ifindex)); | |
1144 | break; | |
1145 | case NEXTHOP_TYPE_IPV6: | |
1146 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
1147 | vty_out(vty, " %s", | |
1148 | inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf, BUFSIZ)); | |
1149 | if (nexthop->ifindex) | |
1150 | vty_out(vty, ", via %s", | |
1151 | ifindex2ifname_per_ns(zns, nexthop->ifindex)); | |
1152 | break; | |
1153 | case NEXTHOP_TYPE_IFINDEX: | |
1154 | vty_out(vty, " is directly connected, %s", | |
1155 | ifindex2ifname_per_ns(zns, nexthop->ifindex)); | |
1156 | break; | |
1157 | case NEXTHOP_TYPE_BLACKHOLE: | |
1158 | vty_out(vty, " is directly connected, Null0"); | |
1159 | break; | |
1160 | default: | |
1161 | break; | |
1162 | } | |
1163 | vty_out(vty, "\n"); | |
fb018d25 DS |
1164 | } |
1165 | ||
d62a17ae | 1166 | static void print_rnh(struct route_node *rn, struct vty *vty) |
fb018d25 | 1167 | { |
d62a17ae | 1168 | struct rnh *rnh; |
1169 | struct nexthop *nexthop; | |
1170 | struct listnode *node; | |
1171 | struct zserv *client; | |
1172 | char buf[BUFSIZ]; | |
1173 | ||
1174 | rnh = rn->info; | |
1175 | vty_out(vty, "%s%s\n", | |
1176 | inet_ntop(rn->p.family, &rn->p.u.prefix, buf, BUFSIZ), | |
1177 | CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED) ? "(Connected)" | |
1178 | : ""); | |
1179 | if (rnh->state) { | |
1180 | vty_out(vty, " resolved via %s\n", | |
1181 | zebra_route_string(rnh->state->type)); | |
7ee30f28 | 1182 | for (nexthop = rnh->state->ng.nexthop; nexthop; |
d62a17ae | 1183 | nexthop = nexthop->next) |
1184 | print_nh(nexthop, vty); | |
1185 | } else | |
1186 | vty_out(vty, " unresolved%s\n", | |
1187 | CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED) | |
1188 | ? "(Connected)" | |
1189 | : ""); | |
1190 | ||
1191 | vty_out(vty, " Client list:"); | |
1192 | for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client)) | |
1193 | vty_out(vty, " %s(fd %d)%s", zebra_route_string(client->proto), | |
1194 | client->sock, | |
1195 | rnh->filtered[client->proto] ? "(filtered)" : ""); | |
731a75fe RW |
1196 | if (!list_isempty(rnh->zebra_pseudowire_list)) |
1197 | vty_out(vty, " zebra[pseudowires]"); | |
d62a17ae | 1198 | vty_out(vty, "\n"); |
fb018d25 | 1199 | } |
bf094f69 | 1200 | |
73bf60a0 | 1201 | static int zebra_cleanup_rnh_client(vrf_id_t vrf_id, afi_t afi, |
bf094f69 QY |
1202 | struct zserv *client, rnh_type_t type) |
1203 | { | |
1204 | struct route_table *ntable; | |
1205 | struct route_node *nrn; | |
1206 | struct rnh *rnh; | |
1207 | ||
1208 | if (IS_ZEBRA_DEBUG_NHT) | |
73bf60a0 RW |
1209 | zlog_debug("%u: Client %s RNH cleanup for family %s type %d", |
1210 | vrf_id, zebra_route_string(client->proto), | |
1211 | afi2str(afi), type); | |
bf094f69 | 1212 | |
73bf60a0 | 1213 | ntable = get_rnh_table(vrf_id, afi, type); |
bf094f69 | 1214 | if (!ntable) { |
9165c5f5 | 1215 | zlog_debug("cleanup_rnh_client: rnh table not found"); |
bf094f69 QY |
1216 | return -1; |
1217 | } | |
1218 | ||
1219 | for (nrn = route_top(ntable); nrn; nrn = route_next(nrn)) { | |
1220 | if (!nrn->info) | |
1221 | continue; | |
1222 | ||
1223 | rnh = nrn->info; | |
1224 | zebra_remove_rnh_client(rnh, client, type); | |
1225 | } | |
1226 | return 1; | |
1227 | } | |
1228 | ||
1229 | /* Cleanup registered nexthops (across VRFs) upon client disconnect. */ | |
453844ab | 1230 | static int zebra_client_cleanup_rnh(struct zserv *client) |
bf094f69 QY |
1231 | { |
1232 | struct vrf *vrf; | |
1233 | struct zebra_vrf *zvrf; | |
1234 | ||
1235 | RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) { | |
8b1766b1 QY |
1236 | zvrf = vrf->info; |
1237 | if (zvrf) { | |
73bf60a0 | 1238 | zebra_cleanup_rnh_client(zvrf_id(zvrf), AFI_IP, client, |
bf094f69 | 1239 | RNH_NEXTHOP_TYPE); |
73bf60a0 RW |
1240 | zebra_cleanup_rnh_client(zvrf_id(zvrf), AFI_IP6, client, |
1241 | RNH_NEXTHOP_TYPE); | |
1242 | zebra_cleanup_rnh_client(zvrf_id(zvrf), AFI_IP, client, | |
1243 | RNH_IMPORT_CHECK_TYPE); | |
1244 | zebra_cleanup_rnh_client(zvrf_id(zvrf), AFI_IP6, client, | |
bf094f69 | 1245 | RNH_IMPORT_CHECK_TYPE); |
bf094f69 QY |
1246 | if (client->proto == ZEBRA_ROUTE_LDP) { |
1247 | hash_iterate(zvrf->lsp_table, | |
1248 | mpls_ldp_lsp_uninstall_all, | |
1249 | zvrf->lsp_table); | |
1250 | mpls_ldp_ftn_uninstall_all(zvrf, AFI_IP); | |
1251 | mpls_ldp_ftn_uninstall_all(zvrf, AFI_IP6); | |
1252 | } | |
1253 | } | |
1254 | } | |
453844ab QY |
1255 | |
1256 | return 0; | |
bf094f69 | 1257 | } |