]>
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 | |
453844ab QY |
65 | void zebra_rnh_init(void) |
66 | { | |
21ccc0cf | 67 | hook_register(zserv_client_close, zebra_client_cleanup_rnh); |
453844ab QY |
68 | } |
69 | ||
73bf60a0 | 70 | static inline struct route_table *get_rnh_table(vrf_id_t vrfid, afi_t afi, |
078430f6 DS |
71 | rnh_type_t type) |
72 | { | |
d62a17ae | 73 | struct zebra_vrf *zvrf; |
74 | struct route_table *t = NULL; | |
75 | ||
76 | zvrf = zebra_vrf_lookup_by_id(vrfid); | |
77 | if (zvrf) | |
78 | switch (type) { | |
79 | case RNH_NEXTHOP_TYPE: | |
73bf60a0 | 80 | t = zvrf->rnh_table[afi]; |
d62a17ae | 81 | break; |
82 | case RNH_IMPORT_CHECK_TYPE: | |
73bf60a0 | 83 | t = zvrf->import_check_table[afi]; |
d62a17ae | 84 | break; |
85 | } | |
86 | ||
87 | return t; | |
078430f6 DS |
88 | } |
89 | ||
d62a17ae | 90 | char *rnh_str(struct rnh *rnh, char *buf, int size) |
fb018d25 | 91 | { |
d62a17ae | 92 | prefix2str(&(rnh->node->p), buf, size); |
93 | return buf; | |
fb018d25 DS |
94 | } |
95 | ||
699dae23 DS |
96 | static void zebra_rnh_remove_from_routing_table(struct rnh *rnh) |
97 | { | |
98 | struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(rnh->vrf_id); | |
99 | struct route_table *table = zvrf->table[rnh->afi][SAFI_UNICAST]; | |
100 | struct route_node *rn; | |
101 | rib_dest_t *dest; | |
102 | ||
103 | if (!table) | |
104 | return; | |
105 | ||
106 | rn = route_node_match(table, &rnh->resolved_route); | |
107 | if (!rn) | |
108 | return; | |
109 | ||
50872b08 DS |
110 | if (IS_ZEBRA_DEBUG_NHT_DETAILED) { |
111 | char buf[PREFIX_STRLEN]; | |
112 | char buf1[PREFIX_STRLEN]; | |
113 | ||
114 | zlog_debug("%s: %u:%s removed from tracking on %s", | |
115 | __PRETTY_FUNCTION__, rnh->vrf_id, | |
116 | prefix2str(&rnh->node->p, buf, sizeof(buf)), | |
117 | srcdest_rnode2str(rn, buf1, sizeof(buf))); | |
118 | } | |
119 | ||
699dae23 | 120 | dest = rib_dest_from_rnode(rn); |
aa57abfb | 121 | rnh_list_del(&dest->nht, rnh); |
699dae23 DS |
122 | route_unlock_node(rn); |
123 | } | |
124 | ||
125 | static void zebra_rnh_store_in_routing_table(struct rnh *rnh) | |
126 | { | |
127 | struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(rnh->vrf_id); | |
128 | struct route_table *table = zvrf->table[rnh->afi][SAFI_UNICAST]; | |
129 | struct route_node *rn; | |
130 | rib_dest_t *dest; | |
131 | ||
132 | rn = route_node_match(table, &rnh->resolved_route); | |
133 | if (!rn) | |
134 | return; | |
135 | ||
50872b08 DS |
136 | if (IS_ZEBRA_DEBUG_NHT_DETAILED) { |
137 | char buf[PREFIX_STRLEN]; | |
138 | char buf1[PREFIX_STRLEN]; | |
139 | ||
140 | zlog_debug("%s: %u:%s added for tracking on %s", | |
141 | __PRETTY_FUNCTION__, rnh->vrf_id, | |
142 | prefix2str(&rnh->node->p, buf, sizeof(buf)), | |
143 | srcdest_rnode2str(rn, buf1, sizeof(buf))); | |
144 | } | |
145 | ||
699dae23 | 146 | dest = rib_dest_from_rnode(rn); |
aa57abfb | 147 | rnh_list_add_tail(&dest->nht, rnh); |
699dae23 DS |
148 | route_unlock_node(rn); |
149 | } | |
150 | ||
1d30d1f4 DS |
151 | struct rnh *zebra_add_rnh(struct prefix *p, vrf_id_t vrfid, rnh_type_t type, |
152 | bool *exists) | |
fb018d25 | 153 | { |
d62a17ae | 154 | struct route_table *table; |
155 | struct route_node *rn; | |
156 | struct rnh *rnh = NULL; | |
157 | char buf[PREFIX2STR_BUFFER]; | |
87554d83 | 158 | afi_t afi = family2afi(p->family); |
d62a17ae | 159 | |
160 | if (IS_ZEBRA_DEBUG_NHT) { | |
161 | prefix2str(p, buf, sizeof(buf)); | |
0a7be328 DS |
162 | zlog_debug("%u: Add RNH %s type %s", vrfid, buf, |
163 | rnh_type2str(type)); | |
d62a17ae | 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, |
0a7be328 DS |
169 | "%u: Add RNH %s type %s - table not found", vrfid, |
170 | buf, rnh_type2str(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]; | |
0a7be328 DS |
274 | zlog_debug("%u: Del RNH %s type %s", rnh->vrf_id, |
275 | rnh_str(rnh, buf, sizeof(buf)), rnh_type2str(type)); | |
d62a17ae | 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]; | |
0a7be328 | 296 | zlog_debug("%u: Client %s registers for RNH %s type %s", vrf_id, |
d62a17ae | 297 | zebra_route_string(client->proto), |
0a7be328 | 298 | rnh_str(rnh, buf, sizeof(buf)), rnh_type2str(type)); |
d62a17ae | 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]; | |
0a7be328 | 315 | zlog_debug("Client %s unregisters for RNH %s type %s", |
d62a17ae | 316 | zebra_route_string(client->proto), |
0a7be328 | 317 | rnh_str(rnh, buf, sizeof(buf)), rnh_type2str(type)); |
d62a17ae | 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)); |
14a4d9d0 | 340 | zlog_warn("%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) { | |
0eb97b86 | 387 | for (nexthop = re->nhe->nhg->nexthop; nexthop; |
e47c4d3c DS |
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; |
b68885f9 | 403 | route_map_result_t ret; |
d62a17ae | 404 | |
d62a17ae | 405 | if (prn && re) { |
0eb97b86 | 406 | for (nexthop = re->nhe->nhg->nexthop; nexthop; |
7ee30f28 | 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) | |
6d0ee6a0 | 465 | && !CHECK_FLAG(re->status, ROUTE_ENTRY_QUEUED) |
996c9314 | 466 | && (re->type != ZEBRA_ROUTE_BGP)) |
fd7fd9e5 | 467 | break; |
d62a17ae | 468 | } |
469 | ||
d62a17ae | 470 | if (re) |
471 | *prn = rn; | |
50872b08 DS |
472 | |
473 | if (!re && IS_ZEBRA_DEBUG_NHT_DETAILED) | |
474 | zlog_debug("\tRejected due to removed or is a bgp route"); | |
475 | ||
d62a17ae | 476 | return re; |
d50b5bdd | 477 | } |
478 | ||
479 | /* | |
480 | * See if a tracked route entry for import (by BGP) has undergone any | |
481 | * change, and if so, notify the client. | |
482 | */ | |
735219e9 | 483 | static void zebra_rnh_eval_import_check_entry(struct zebra_vrf *zvrf, afi_t afi, |
d62a17ae | 484 | int force, struct route_node *nrn, |
485 | struct rnh *rnh, | |
735219e9 | 486 | struct route_node *prn, |
d62a17ae | 487 | struct route_entry *re) |
d50b5bdd | 488 | { |
d62a17ae | 489 | int state_changed = 0; |
490 | struct zserv *client; | |
491 | char bufn[INET6_ADDRSTRLEN]; | |
492 | struct listnode *node; | |
d62a17ae | 493 | |
699dae23 DS |
494 | zebra_rnh_remove_from_routing_table(rnh); |
495 | if (prn) { | |
a304e258 | 496 | prefix_copy(&rnh->resolved_route, &prn->p); |
699dae23 | 497 | } else { |
a304e258 DS |
498 | int family = rnh->resolved_route.family; |
499 | ||
500 | memset(&rnh->resolved_route.family, 0, sizeof(struct prefix)); | |
501 | rnh->resolved_route.family = family; | |
502 | } | |
699dae23 | 503 | zebra_rnh_store_in_routing_table(rnh); |
a304e258 | 504 | |
d62a17ae | 505 | if (re && (rnh->state == NULL)) { |
677c1dd5 DS |
506 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED)) |
507 | state_changed = 1; | |
d62a17ae | 508 | } else if (!re && (rnh->state != NULL)) |
509 | state_changed = 1; | |
510 | ||
a304e258 | 511 | if (compare_state(re, rnh->state)) { |
d62a17ae | 512 | copy_state(rnh, re, nrn); |
9cb8322e | 513 | state_changed = 1; |
a304e258 | 514 | } |
d62a17ae | 515 | |
516 | if (state_changed || force) { | |
517 | if (IS_ZEBRA_DEBUG_NHT) { | |
518 | prefix2str(&nrn->p, bufn, INET6_ADDRSTRLEN); | |
735219e9 DS |
519 | zlog_debug("%u:%s: Route import check %s %s", |
520 | zvrf->vrf->vrf_id, | |
d62a17ae | 521 | bufn, rnh->state ? "passed" : "failed", |
522 | state_changed ? "(state changed)" : ""); | |
523 | } | |
524 | /* state changed, notify clients */ | |
525 | for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client)) { | |
735219e9 DS |
526 | send_client(rnh, client, |
527 | RNH_IMPORT_CHECK_TYPE, zvrf->vrf->vrf_id); | |
d62a17ae | 528 | } |
529 | } | |
d50b5bdd | 530 | } |
fb018d25 | 531 | |
d50b5bdd | 532 | /* |
533 | * Notify clients registered for this nexthop about a change. | |
534 | */ | |
73bf60a0 RW |
535 | static void zebra_rnh_notify_protocol_clients(struct zebra_vrf *zvrf, afi_t afi, |
536 | struct route_node *nrn, | |
537 | struct rnh *rnh, | |
538 | struct route_node *prn, | |
539 | struct route_entry *re) | |
d50b5bdd | 540 | { |
d62a17ae | 541 | struct listnode *node; |
542 | struct zserv *client; | |
543 | char bufn[INET6_ADDRSTRLEN]; | |
544 | char bufp[INET6_ADDRSTRLEN]; | |
545 | int num_resolving_nh; | |
546 | ||
547 | if (IS_ZEBRA_DEBUG_NHT) { | |
548 | prefix2str(&nrn->p, bufn, INET6_ADDRSTRLEN); | |
549 | if (prn && re) { | |
50872b08 | 550 | srcdest_rnode2str(prn, bufp, INET6_ADDRSTRLEN); |
6d53d7b1 | 551 | zlog_debug("%u:%s: NH resolved over route %s", |
552 | zvrf->vrf->vrf_id, bufn, bufp); | |
d62a17ae | 553 | } else |
6d53d7b1 | 554 | zlog_debug("%u:%s: NH has become unresolved", |
555 | zvrf->vrf->vrf_id, bufn); | |
d62a17ae | 556 | } |
557 | ||
558 | for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client)) { | |
559 | if (prn && re) { | |
560 | /* Apply route-map for this client to route resolving | |
561 | * this | |
562 | * nexthop to see if it is filtered or not. | |
563 | */ | |
e47c4d3c | 564 | zebra_rnh_clear_nexthop_rnh_filters(re); |
d62a17ae | 565 | num_resolving_nh = zebra_rnh_apply_nht_rmap( |
73bf60a0 | 566 | afi, zvrf, prn, re, client->proto); |
d62a17ae | 567 | if (num_resolving_nh) |
568 | rnh->filtered[client->proto] = 0; | |
569 | else | |
570 | rnh->filtered[client->proto] = 1; | |
571 | ||
572 | if (IS_ZEBRA_DEBUG_NHT) | |
573 | zlog_debug( | |
574 | "%u:%s: Notifying client %s about NH %s", | |
6d53d7b1 | 575 | zvrf->vrf->vrf_id, bufn, |
d62a17ae | 576 | zebra_route_string(client->proto), |
577 | num_resolving_nh | |
578 | ? "" | |
579 | : "(filtered by route-map)"); | |
580 | } else { | |
581 | rnh->filtered[client->proto] = 0; | |
582 | if (IS_ZEBRA_DEBUG_NHT) | |
583 | zlog_debug( | |
584 | "%u:%s: Notifying client %s about NH (unreachable)", | |
6d53d7b1 | 585 | zvrf->vrf->vrf_id, bufn, |
d62a17ae | 586 | zebra_route_string(client->proto)); |
587 | } | |
588 | ||
6d53d7b1 | 589 | send_client(rnh, client, RNH_NEXTHOP_TYPE, zvrf->vrf->vrf_id); |
d62a17ae | 590 | } |
e47c4d3c DS |
591 | |
592 | if (re) | |
593 | zebra_rnh_clear_nexthop_rnh_filters(re); | |
d50b5bdd | 594 | } |
078430f6 | 595 | |
4dfd7a02 MS |
596 | /* |
597 | * Utility to determine whether a candidate nexthop is useable. We make this | |
598 | * check in a couple of places, so this is a single home for the logic we | |
599 | * use. | |
600 | */ | |
677c1dd5 DS |
601 | static bool rnh_nexthop_valid(const struct route_entry *re, |
602 | const struct nexthop *nh) | |
4dfd7a02 | 603 | { |
677c1dd5 | 604 | return (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED) |
e47c4d3c DS |
605 | && CHECK_FLAG(nh->flags, NEXTHOP_FLAG_ACTIVE) |
606 | && !CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE) | |
607 | && !CHECK_FLAG(nh->flags, NEXTHOP_FLAG_DUPLICATE) | |
608 | && !CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RNH_FILTERED)); | |
4dfd7a02 MS |
609 | } |
610 | ||
fd7fd9e5 DS |
611 | /* |
612 | * Determine appropriate route (route entry) resolving a tracked | |
613 | * nexthop. | |
614 | */ | |
996c9314 | 615 | static struct route_entry * |
73bf60a0 | 616 | zebra_rnh_resolve_nexthop_entry(struct zebra_vrf *zvrf, afi_t afi, |
996c9314 LB |
617 | struct route_node *nrn, struct rnh *rnh, |
618 | struct route_node **prn) | |
fd7fd9e5 DS |
619 | { |
620 | struct route_table *route_table; | |
621 | struct route_node *rn; | |
622 | struct route_entry *re; | |
f183e380 | 623 | struct nexthop *nexthop; |
fd7fd9e5 DS |
624 | |
625 | *prn = NULL; | |
626 | ||
73bf60a0 | 627 | route_table = zvrf->table[afi][SAFI_UNICAST]; |
fd7fd9e5 DS |
628 | if (!route_table) |
629 | return NULL; | |
630 | ||
631 | rn = route_node_match(route_table, &nrn->p); | |
632 | if (!rn) | |
633 | return NULL; | |
634 | ||
635 | /* Unlock route node - we don't need to lock when walking the tree. */ | |
636 | route_unlock_node(rn); | |
637 | ||
638 | /* While resolving nexthops, we may need to walk up the tree from the | |
639 | * most-specific match. Do similar logic as in zebra_rib.c | |
640 | */ | |
641 | while (rn) { | |
50872b08 DS |
642 | if (IS_ZEBRA_DEBUG_NHT_DETAILED) { |
643 | char buf[PREFIX_STRLEN]; | |
644 | char buf1[PREFIX_STRLEN]; | |
645 | ||
646 | zlog_debug("%s: %u:%s Possible Match to %s", | |
647 | __PRETTY_FUNCTION__, rnh->vrf_id, | |
648 | prefix2str(&rnh->node->p, buf, sizeof(buf)), | |
649 | srcdest_rnode2str(rn, buf1, sizeof(buf))); | |
650 | } | |
651 | ||
fd7fd9e5 DS |
652 | /* Do not resolve over default route unless allowed && |
653 | * match route to be exact if so specified | |
654 | */ | |
996c9314 | 655 | if (is_default_prefix(&rn->p) |
5a0bdc78 | 656 | && !rnh_resolve_via_default(zvrf, rn->p.family)) { |
50872b08 DS |
657 | if (IS_ZEBRA_DEBUG_NHT_DETAILED) |
658 | zlog_debug( | |
659 | "\tNot allowed to resolve through default prefix"); | |
fd7fd9e5 | 660 | return NULL; |
50872b08 | 661 | } |
fd7fd9e5 DS |
662 | |
663 | /* Identify appropriate route entry. */ | |
996c9314 | 664 | RNODE_FOREACH_RE (rn, re) { |
50872b08 DS |
665 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) { |
666 | if (IS_ZEBRA_DEBUG_NHT_DETAILED) | |
667 | zlog_debug( | |
668 | "\tRoute Entry %s removed", | |
669 | zebra_route_string(re->type)); | |
fd7fd9e5 | 670 | continue; |
50872b08 DS |
671 | } |
672 | if (!CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)) { | |
673 | if (IS_ZEBRA_DEBUG_NHT_DETAILED) | |
674 | zlog_debug( | |
675 | "\tRoute Entry %s !selected", | |
676 | zebra_route_string(re->type)); | |
fd7fd9e5 | 677 | continue; |
50872b08 | 678 | } |
fd7fd9e5 | 679 | |
6d0ee6a0 DS |
680 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_QUEUED)) { |
681 | if (IS_ZEBRA_DEBUG_NHT_DETAILED) | |
682 | zlog_debug( | |
683 | "\tRoute Entry %s queued", | |
684 | zebra_route_string(re->type)); | |
685 | continue; | |
686 | } | |
687 | ||
f183e380 MS |
688 | /* Just being SELECTED isn't quite enough - must |
689 | * have an installed nexthop to be useful. | |
690 | */ | |
0eb97b86 | 691 | for (ALL_NEXTHOPS_PTR(re->nhe->nhg, nexthop)) { |
677c1dd5 | 692 | if (rnh_nexthop_valid(re, nexthop)) |
f183e380 | 693 | break; |
4dfd7a02 | 694 | } |
f183e380 | 695 | |
50872b08 DS |
696 | if (nexthop == NULL) { |
697 | if (IS_ZEBRA_DEBUG_NHT_DETAILED) | |
698 | zlog_debug( | |
699 | "\tRoute Entry %s no nexthops", | |
700 | zebra_route_string(re->type)); | |
f183e380 | 701 | continue; |
50872b08 | 702 | } |
f183e380 | 703 | |
fd7fd9e5 DS |
704 | if (CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED)) { |
705 | if ((re->type == ZEBRA_ROUTE_CONNECT) | |
706 | || (re->type == ZEBRA_ROUTE_STATIC)) | |
707 | break; | |
708 | if (re->type == ZEBRA_ROUTE_NHRP) { | |
fd7fd9e5 | 709 | |
0eb97b86 MS |
710 | for (nexthop = re->nhe->nhg->nexthop; |
711 | nexthop; | |
fd7fd9e5 DS |
712 | nexthop = nexthop->next) |
713 | if (nexthop->type | |
996c9314 | 714 | == NEXTHOP_TYPE_IFINDEX) |
fd7fd9e5 DS |
715 | break; |
716 | if (nexthop) | |
717 | break; | |
718 | } | |
719 | } else | |
720 | break; | |
721 | } | |
722 | ||
723 | /* Route entry found, we're done; else, walk up the tree. */ | |
724 | if (re) { | |
725 | *prn = rn; | |
726 | return re; | |
727 | } | |
728 | ||
699dae23 | 729 | if (!CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED)) |
fd7fd9e5 | 730 | rn = rn->parent; |
50872b08 DS |
731 | else { |
732 | if (IS_ZEBRA_DEBUG_NHT_DETAILED) | |
733 | zlog_debug( | |
734 | "\tNexthop must be connected, cannot recurse up"); | |
fd7fd9e5 | 735 | return NULL; |
50872b08 | 736 | } |
fd7fd9e5 DS |
737 | } |
738 | ||
739 | return NULL; | |
740 | } | |
741 | ||
731a75fe RW |
742 | static void zebra_rnh_process_pseudowires(vrf_id_t vrfid, struct rnh *rnh) |
743 | { | |
744 | struct zebra_pw *pw; | |
745 | struct listnode *node; | |
746 | ||
747 | for (ALL_LIST_ELEMENTS_RO(rnh->zebra_pseudowire_list, node, pw)) | |
748 | zebra_pw_update(pw); | |
749 | } | |
750 | ||
d50b5bdd | 751 | /* |
752 | * See if a tracked nexthop entry has undergone any change, and if so, | |
753 | * take appropriate action; this involves notifying any clients and/or | |
754 | * scheduling dependent static routes for processing. | |
755 | */ | |
73bf60a0 | 756 | static void zebra_rnh_eval_nexthop_entry(struct zebra_vrf *zvrf, afi_t afi, |
6d53d7b1 | 757 | int force, struct route_node *nrn, |
d62a17ae | 758 | struct rnh *rnh, |
759 | struct route_node *prn, | |
760 | struct route_entry *re) | |
d50b5bdd | 761 | { |
d62a17ae | 762 | int state_changed = 0; |
763 | ||
764 | /* If we're resolving over a different route, resolution has changed or | |
765 | * the resolving route has some change (e.g., metric), there is a state | |
766 | * change. | |
767 | */ | |
699dae23 | 768 | zebra_rnh_remove_from_routing_table(rnh); |
60c67010 | 769 | if (!prefix_same(&rnh->resolved_route, prn ? &prn->p : NULL)) { |
d62a17ae | 770 | if (prn) |
771 | prefix_copy(&rnh->resolved_route, &prn->p); | |
a304e258 DS |
772 | else { |
773 | /* | |
774 | * Just quickly store the family of the resolved | |
775 | * route so that we can reset it in a second here | |
776 | */ | |
777 | int family = rnh->resolved_route.family; | |
778 | ||
d62a17ae | 779 | memset(&rnh->resolved_route, 0, sizeof(struct prefix)); |
a304e258 DS |
780 | rnh->resolved_route.family = family; |
781 | } | |
d62a17ae | 782 | |
783 | copy_state(rnh, re, nrn); | |
784 | state_changed = 1; | |
785 | } else if (compare_state(re, rnh->state)) { | |
786 | copy_state(rnh, re, nrn); | |
787 | state_changed = 1; | |
788 | } | |
699dae23 | 789 | zebra_rnh_store_in_routing_table(rnh); |
d62a17ae | 790 | |
791 | if (state_changed || force) { | |
792 | /* NOTE: Use the "copy" of resolving route stored in 'rnh' i.e., | |
793 | * rnh->state. | |
794 | */ | |
795 | /* Notify registered protocol clients. */ | |
73bf60a0 | 796 | zebra_rnh_notify_protocol_clients(zvrf, afi, nrn, rnh, prn, |
d62a17ae | 797 | rnh->state); |
798 | ||
731a75fe | 799 | /* Process pseudowires attached to this nexthop */ |
6d53d7b1 | 800 | zebra_rnh_process_pseudowires(zvrf->vrf->vrf_id, rnh); |
d62a17ae | 801 | } |
d50b5bdd | 802 | } |
6e26278c | 803 | |
d50b5bdd | 804 | /* Evaluate one tracked entry */ |
73bf60a0 | 805 | static void zebra_rnh_evaluate_entry(struct zebra_vrf *zvrf, afi_t afi, |
6d53d7b1 | 806 | int force, rnh_type_t type, |
807 | struct route_node *nrn) | |
d50b5bdd | 808 | { |
d62a17ae | 809 | struct rnh *rnh; |
810 | struct route_entry *re; | |
811 | struct route_node *prn; | |
812 | char bufn[INET6_ADDRSTRLEN]; | |
813 | ||
814 | if (IS_ZEBRA_DEBUG_NHT) { | |
815 | prefix2str(&nrn->p, bufn, INET6_ADDRSTRLEN); | |
0a7be328 DS |
816 | zlog_debug("%u:%s: Evaluate RNH, type %s %s", zvrf->vrf->vrf_id, |
817 | bufn, rnh_type2str(type), force ? "(force)" : ""); | |
d62a17ae | 818 | } |
819 | ||
820 | rnh = nrn->info; | |
821 | ||
822 | /* Identify route entry (RE) resolving this tracked entry. */ | |
fd7fd9e5 | 823 | if (type == RNH_IMPORT_CHECK_TYPE) |
73bf60a0 | 824 | re = zebra_rnh_resolve_import_entry(zvrf, afi, nrn, rnh, &prn); |
fd7fd9e5 | 825 | else |
73bf60a0 | 826 | re = zebra_rnh_resolve_nexthop_entry(zvrf, afi, nrn, rnh, &prn); |
d62a17ae | 827 | |
828 | /* If the entry cannot be resolved and that is also the existing state, | |
829 | * there is nothing further to do. | |
830 | */ | |
831 | if (!re && rnh->state == NULL && !force) | |
832 | return; | |
833 | ||
834 | /* Process based on type of entry. */ | |
835 | if (type == RNH_IMPORT_CHECK_TYPE) | |
735219e9 DS |
836 | zebra_rnh_eval_import_check_entry(zvrf, afi, force, nrn, rnh, |
837 | prn, re); | |
d62a17ae | 838 | else |
73bf60a0 | 839 | zebra_rnh_eval_nexthop_entry(zvrf, afi, force, nrn, rnh, prn, |
6d53d7b1 | 840 | re); |
d50b5bdd | 841 | } |
842 | ||
8ce5e6a3 | 843 | /* |
f0f77c9a DS |
844 | * Clear the ROUTE_ENTRY_NEXTHOPS_CHANGED flag |
845 | * from the re entries. | |
8ce5e6a3 DS |
846 | * |
847 | * Please note we are doing this *after* we have | |
848 | * notified the world about each nexthop as that | |
f0f77c9a | 849 | * we can have a situation where one re entry |
8ce5e6a3 DS |
850 | * covers multiple nexthops we are interested in. |
851 | */ | |
73bf60a0 | 852 | static void zebra_rnh_clear_nhc_flag(struct zebra_vrf *zvrf, afi_t afi, |
d62a17ae | 853 | rnh_type_t type, struct route_node *nrn) |
8ce5e6a3 | 854 | { |
d62a17ae | 855 | struct rnh *rnh; |
856 | struct route_entry *re; | |
857 | struct route_node *prn; | |
8ce5e6a3 | 858 | |
d62a17ae | 859 | rnh = nrn->info; |
8ce5e6a3 | 860 | |
fd7fd9e5 DS |
861 | /* Identify route entry (RIB) resolving this tracked entry. */ |
862 | if (type == RNH_IMPORT_CHECK_TYPE) | |
73bf60a0 | 863 | re = zebra_rnh_resolve_import_entry(zvrf, afi, nrn, rnh, |
996c9314 | 864 | &prn); |
fd7fd9e5 | 865 | else |
73bf60a0 | 866 | re = zebra_rnh_resolve_nexthop_entry(zvrf, afi, nrn, rnh, |
fd7fd9e5 | 867 | &prn); |
8ce5e6a3 | 868 | |
42fc558e | 869 | if (re) |
332ad713 | 870 | UNSET_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED); |
8ce5e6a3 | 871 | } |
d50b5bdd | 872 | |
873 | /* Evaluate all tracked entries (nexthops or routes for import into BGP) | |
874 | * of a particular VRF and address-family or a specific prefix. | |
875 | */ | |
73bf60a0 | 876 | void zebra_evaluate_rnh(struct zebra_vrf *zvrf, afi_t afi, int force, |
6d53d7b1 | 877 | rnh_type_t type, struct prefix *p) |
d50b5bdd | 878 | { |
d62a17ae | 879 | struct route_table *rnh_table; |
880 | struct route_node *nrn; | |
881 | ||
73bf60a0 | 882 | rnh_table = get_rnh_table(zvrf->vrf->vrf_id, afi, type); |
d62a17ae | 883 | if (!rnh_table) // unexpected |
884 | return; | |
885 | ||
886 | if (p) { | |
887 | /* Evaluating a specific entry, make sure it exists. */ | |
888 | nrn = route_node_lookup(rnh_table, p); | |
889 | if (nrn && nrn->info) | |
73bf60a0 | 890 | zebra_rnh_evaluate_entry(zvrf, afi, force, type, nrn); |
d62a17ae | 891 | |
892 | if (nrn) | |
893 | route_unlock_node(nrn); | |
894 | } else { | |
895 | /* Evaluate entire table. */ | |
896 | nrn = route_top(rnh_table); | |
897 | while (nrn) { | |
898 | if (nrn->info) | |
73bf60a0 RW |
899 | zebra_rnh_evaluate_entry(zvrf, afi, force, type, |
900 | nrn); | |
d62a17ae | 901 | nrn = route_next(nrn); /* this will also unlock nrn */ |
902 | } | |
903 | nrn = route_top(rnh_table); | |
904 | while (nrn) { | |
905 | if (nrn->info) | |
73bf60a0 | 906 | zebra_rnh_clear_nhc_flag(zvrf, afi, type, nrn); |
d62a17ae | 907 | nrn = route_next(nrn); /* this will also unlock nrn */ |
908 | } | |
909 | } | |
fb018d25 DS |
910 | } |
911 | ||
73bf60a0 | 912 | void zebra_print_rnh_table(vrf_id_t vrfid, afi_t afi, struct vty *vty, |
dbeca484 | 913 | rnh_type_t type, struct prefix *p) |
fb018d25 | 914 | { |
d62a17ae | 915 | struct route_table *table; |
916 | struct route_node *rn; | |
917 | ||
73bf60a0 | 918 | table = get_rnh_table(vrfid, afi, type); |
d62a17ae | 919 | if (!table) { |
14a4d9d0 | 920 | if (IS_ZEBRA_DEBUG_NHT) |
921 | zlog_debug("print_rnhs: rnh table not found"); | |
d62a17ae | 922 | return; |
923 | } | |
924 | ||
dbeca484 | 925 | for (rn = route_top(table); rn; rn = route_next(rn)) { |
e15ed56c | 926 | if (p && !prefix_match(&rn->p, p)) |
dbeca484 DS |
927 | continue; |
928 | ||
d62a17ae | 929 | if (rn->info) |
930 | print_rnh(rn, vty); | |
dbeca484 | 931 | } |
fb018d25 DS |
932 | } |
933 | ||
fb018d25 | 934 | /** |
f0f77c9a | 935 | * free_state - free up the re structure associated with the rnh. |
fb018d25 | 936 | */ |
d62a17ae | 937 | static void free_state(vrf_id_t vrf_id, struct route_entry *re, |
938 | struct route_node *rn) | |
fb018d25 | 939 | { |
d62a17ae | 940 | if (!re) |
941 | return; | |
fb018d25 | 942 | |
d62a17ae | 943 | /* free RE and nexthops */ |
0eb97b86 | 944 | zebra_nhg_free(re->nhe); |
d62a17ae | 945 | XFREE(MTYPE_RE, re); |
fb018d25 DS |
946 | } |
947 | ||
d62a17ae | 948 | static void copy_state(struct rnh *rnh, struct route_entry *re, |
949 | struct route_node *rn) | |
fb018d25 | 950 | { |
d62a17ae | 951 | struct route_entry *state; |
fb018d25 | 952 | |
d62a17ae | 953 | if (rnh->state) { |
954 | free_state(rnh->vrf_id, rnh->state, rn); | |
955 | rnh->state = NULL; | |
956 | } | |
fb018d25 | 957 | |
d62a17ae | 958 | if (!re) |
959 | return; | |
fb018d25 | 960 | |
d62a17ae | 961 | state = XCALLOC(MTYPE_RE, sizeof(struct route_entry)); |
962 | state->type = re->type; | |
7733c6c4 | 963 | state->distance = re->distance; |
d62a17ae | 964 | state->metric = re->metric; |
8f43b4d8 | 965 | state->vrf_id = re->vrf_id; |
677c1dd5 | 966 | state->status = re->status; |
fb018d25 | 967 | |
0eb97b86 MS |
968 | state->nhe = zebra_nhg_alloc(); |
969 | state->nhe->nhg = nexthop_group_new(); | |
970 | ||
971 | nexthop_group_copy(state->nhe->nhg, re->nhe->nhg); | |
d62a17ae | 972 | rnh->state = state; |
fb018d25 DS |
973 | } |
974 | ||
d62a17ae | 975 | static int compare_state(struct route_entry *r1, struct route_entry *r2) |
fb018d25 | 976 | { |
d62a17ae | 977 | if (!r1 && !r2) |
978 | return 0; | |
fb018d25 | 979 | |
d62a17ae | 980 | if ((!r1 && r2) || (r1 && !r2)) |
981 | return 1; | |
fb018d25 | 982 | |
7733c6c4 JB |
983 | if (r1->distance != r2->distance) |
984 | return 1; | |
985 | ||
d62a17ae | 986 | if (r1->metric != r2->metric) |
987 | return 1; | |
fb018d25 | 988 | |
0eb97b86 MS |
989 | if (nexthop_group_nexthop_num(r1->nhe->nhg) |
990 | != nexthop_group_nexthop_num(r2->nhe->nhg)) | |
d62a17ae | 991 | return 1; |
fb018d25 | 992 | |
0eb97b86 MS |
993 | if (nexthop_group_hash(r1->nhe->nhg) != |
994 | nexthop_group_hash(r2->nhe->nhg)) | |
d62a17ae | 995 | return 1; |
fb018d25 | 996 | |
d62a17ae | 997 | return 0; |
fb018d25 DS |
998 | } |
999 | ||
d62a17ae | 1000 | static int send_client(struct rnh *rnh, struct zserv *client, rnh_type_t type, |
1001 | vrf_id_t vrf_id) | |
fb018d25 | 1002 | { |
d62a17ae | 1003 | struct stream *s; |
1004 | struct route_entry *re; | |
1005 | unsigned long nump; | |
d7c0a89a | 1006 | uint8_t num; |
0acf4df0 | 1007 | struct nexthop *nh; |
d62a17ae | 1008 | struct route_node *rn; |
1009 | int cmd = (type == RNH_IMPORT_CHECK_TYPE) ? ZEBRA_IMPORT_CHECK_UPDATE | |
1010 | : ZEBRA_NEXTHOP_UPDATE; | |
1011 | ||
1012 | rn = rnh->node; | |
1013 | re = rnh->state; | |
1014 | ||
1015 | /* Get output stream. */ | |
1002497a | 1016 | s = stream_new(ZEBRA_MAX_PACKET_SIZ); |
d62a17ae | 1017 | |
7cf15b25 | 1018 | zclient_create_header(s, cmd, vrf_id); |
d62a17ae | 1019 | |
1020 | stream_putw(s, rn->p.family); | |
1021 | switch (rn->p.family) { | |
1022 | case AF_INET: | |
1023 | stream_putc(s, rn->p.prefixlen); | |
1024 | stream_put_in_addr(s, &rn->p.u.prefix4); | |
fb018d25 | 1025 | break; |
d62a17ae | 1026 | case AF_INET6: |
1027 | stream_putc(s, rn->p.prefixlen); | |
1028 | stream_put(s, &rn->p.u.prefix6, IPV6_MAX_BYTELEN); | |
fb018d25 | 1029 | break; |
d62a17ae | 1030 | default: |
e914ccbe | 1031 | flog_err(EC_ZEBRA_RNH_UNKNOWN_FAMILY, |
1c50c1c0 QY |
1032 | "%s: Unknown family (%d) notification attempted\n", |
1033 | __FUNCTION__, rn->p.family); | |
fb018d25 | 1034 | break; |
d62a17ae | 1035 | } |
1036 | if (re) { | |
68a02e06 MS |
1037 | struct zapi_nexthop znh; |
1038 | ||
05dd5aaf DS |
1039 | stream_putc(s, re->type); |
1040 | stream_putw(s, re->instance); | |
d62a17ae | 1041 | stream_putc(s, re->distance); |
1042 | stream_putl(s, re->metric); | |
1043 | num = 0; | |
1044 | nump = stream_get_endp(s); | |
1045 | stream_putc(s, 0); | |
0eb97b86 | 1046 | for (ALL_NEXTHOPS_PTR(re->nhe->nhg, nh)) |
677c1dd5 | 1047 | if (rnh_nexthop_valid(re, nh)) { |
68a02e06 MS |
1048 | zapi_nexthop_from_nexthop(&znh, nh); |
1049 | zapi_nexthop_encode(s, &znh, 0 /* flags */); | |
d62a17ae | 1050 | num++; |
1051 | } | |
1052 | stream_putc_at(s, nump, num); | |
1053 | } else { | |
05dd5aaf DS |
1054 | stream_putc(s, 0); // type |
1055 | stream_putw(s, 0); // instance | |
d62a17ae | 1056 | stream_putc(s, 0); // distance |
1057 | stream_putl(s, 0); // metric | |
1058 | stream_putc(s, 0); // nexthops | |
1059 | } | |
1060 | stream_putw_at(s, 0, stream_get_endp(s)); | |
1061 | ||
1062 | client->nh_last_upd_time = monotime(NULL); | |
1063 | client->last_write_cmd = cmd; | |
21ccc0cf | 1064 | return zserv_send_message(client, s); |
fb018d25 DS |
1065 | } |
1066 | ||
d62a17ae | 1067 | static void print_nh(struct nexthop *nexthop, struct vty *vty) |
fb018d25 | 1068 | { |
d62a17ae | 1069 | char buf[BUFSIZ]; |
ce5a9887 | 1070 | struct zebra_ns *zns = zebra_ns_lookup(nexthop->vrf_id); |
d62a17ae | 1071 | |
1072 | switch (nexthop->type) { | |
1073 | case NEXTHOP_TYPE_IPV4: | |
1074 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
1075 | vty_out(vty, " via %s", inet_ntoa(nexthop->gate.ipv4)); | |
1076 | if (nexthop->ifindex) | |
1077 | vty_out(vty, ", %s", | |
1078 | ifindex2ifname_per_ns(zns, nexthop->ifindex)); | |
1079 | break; | |
1080 | case NEXTHOP_TYPE_IPV6: | |
1081 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
1082 | vty_out(vty, " %s", | |
1083 | inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf, BUFSIZ)); | |
1084 | if (nexthop->ifindex) | |
1085 | vty_out(vty, ", via %s", | |
1086 | ifindex2ifname_per_ns(zns, nexthop->ifindex)); | |
1087 | break; | |
1088 | case NEXTHOP_TYPE_IFINDEX: | |
1089 | vty_out(vty, " is directly connected, %s", | |
1090 | ifindex2ifname_per_ns(zns, nexthop->ifindex)); | |
1091 | break; | |
1092 | case NEXTHOP_TYPE_BLACKHOLE: | |
1093 | vty_out(vty, " is directly connected, Null0"); | |
1094 | break; | |
1095 | default: | |
1096 | break; | |
1097 | } | |
1098 | vty_out(vty, "\n"); | |
fb018d25 DS |
1099 | } |
1100 | ||
d62a17ae | 1101 | static void print_rnh(struct route_node *rn, struct vty *vty) |
fb018d25 | 1102 | { |
d62a17ae | 1103 | struct rnh *rnh; |
1104 | struct nexthop *nexthop; | |
1105 | struct listnode *node; | |
1106 | struct zserv *client; | |
1107 | char buf[BUFSIZ]; | |
1108 | ||
1109 | rnh = rn->info; | |
1110 | vty_out(vty, "%s%s\n", | |
1111 | inet_ntop(rn->p.family, &rn->p.u.prefix, buf, BUFSIZ), | |
1112 | CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED) ? "(Connected)" | |
1113 | : ""); | |
1114 | if (rnh->state) { | |
1115 | vty_out(vty, " resolved via %s\n", | |
1116 | zebra_route_string(rnh->state->type)); | |
0eb97b86 | 1117 | for (nexthop = rnh->state->nhe->nhg->nexthop; nexthop; |
d62a17ae | 1118 | nexthop = nexthop->next) |
1119 | print_nh(nexthop, vty); | |
1120 | } else | |
1121 | vty_out(vty, " unresolved%s\n", | |
1122 | CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED) | |
1123 | ? "(Connected)" | |
1124 | : ""); | |
1125 | ||
1126 | vty_out(vty, " Client list:"); | |
1127 | for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client)) | |
1128 | vty_out(vty, " %s(fd %d)%s", zebra_route_string(client->proto), | |
1129 | client->sock, | |
1130 | rnh->filtered[client->proto] ? "(filtered)" : ""); | |
731a75fe RW |
1131 | if (!list_isempty(rnh->zebra_pseudowire_list)) |
1132 | vty_out(vty, " zebra[pseudowires]"); | |
d62a17ae | 1133 | vty_out(vty, "\n"); |
fb018d25 | 1134 | } |
bf094f69 | 1135 | |
73bf60a0 | 1136 | static int zebra_cleanup_rnh_client(vrf_id_t vrf_id, afi_t afi, |
bf094f69 QY |
1137 | struct zserv *client, rnh_type_t type) |
1138 | { | |
1139 | struct route_table *ntable; | |
1140 | struct route_node *nrn; | |
1141 | struct rnh *rnh; | |
1142 | ||
1143 | if (IS_ZEBRA_DEBUG_NHT) | |
0a7be328 | 1144 | zlog_debug("%u: Client %s RNH cleanup for family %s type %s", |
73bf60a0 | 1145 | vrf_id, zebra_route_string(client->proto), |
0a7be328 | 1146 | afi2str(afi), rnh_type2str(type)); |
bf094f69 | 1147 | |
73bf60a0 | 1148 | ntable = get_rnh_table(vrf_id, afi, type); |
bf094f69 | 1149 | if (!ntable) { |
9165c5f5 | 1150 | zlog_debug("cleanup_rnh_client: rnh table not found"); |
bf094f69 QY |
1151 | return -1; |
1152 | } | |
1153 | ||
1154 | for (nrn = route_top(ntable); nrn; nrn = route_next(nrn)) { | |
1155 | if (!nrn->info) | |
1156 | continue; | |
1157 | ||
1158 | rnh = nrn->info; | |
1159 | zebra_remove_rnh_client(rnh, client, type); | |
1160 | } | |
1161 | return 1; | |
1162 | } | |
1163 | ||
1164 | /* Cleanup registered nexthops (across VRFs) upon client disconnect. */ | |
453844ab | 1165 | static int zebra_client_cleanup_rnh(struct zserv *client) |
bf094f69 QY |
1166 | { |
1167 | struct vrf *vrf; | |
1168 | struct zebra_vrf *zvrf; | |
1169 | ||
1170 | RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) { | |
8b1766b1 QY |
1171 | zvrf = vrf->info; |
1172 | if (zvrf) { | |
73bf60a0 | 1173 | zebra_cleanup_rnh_client(zvrf_id(zvrf), AFI_IP, client, |
bf094f69 | 1174 | RNH_NEXTHOP_TYPE); |
73bf60a0 RW |
1175 | zebra_cleanup_rnh_client(zvrf_id(zvrf), AFI_IP6, client, |
1176 | RNH_NEXTHOP_TYPE); | |
1177 | zebra_cleanup_rnh_client(zvrf_id(zvrf), AFI_IP, client, | |
1178 | RNH_IMPORT_CHECK_TYPE); | |
1179 | zebra_cleanup_rnh_client(zvrf_id(zvrf), AFI_IP6, client, | |
bf094f69 | 1180 | RNH_IMPORT_CHECK_TYPE); |
bf094f69 QY |
1181 | } |
1182 | } | |
453844ab QY |
1183 | |
1184 | return 0; | |
bf094f69 | 1185 | } |
5a0bdc78 PG |
1186 | |
1187 | int rnh_resolve_via_default(struct zebra_vrf *zvrf, int family) | |
1188 | { | |
1189 | if (((family == AF_INET) && zvrf->zebra_rnh_ip_default_route) | |
1190 | || ((family == AF_INET6) && zvrf->zebra_rnh_ipv6_default_route)) | |
1191 | return 1; | |
1192 | else | |
1193 | return 0; | |
1194 | } |