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