]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* Routing Information Base. |
2 | * Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro | |
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 | |
718e3744 | 19 | */ |
20 | ||
21 | #include <zebra.h> | |
22 | ||
718e3744 | 23 | #include "command.h" |
43e52561 QY |
24 | #include "if.h" |
25 | #include "linklist.h" | |
718e3744 | 26 | #include "log.h" |
43e52561 QY |
27 | #include "memory.h" |
28 | #include "mpls.h" | |
29 | #include "nexthop.h" | |
30 | #include "prefix.h" | |
7514fb77 PJ |
31 | #include "prefix.h" |
32 | #include "routemap.h" | |
43e52561 | 33 | #include "sockunion.h" |
05737783 | 34 | #include "srcdest_table.h" |
43e52561 QY |
35 | #include "table.h" |
36 | #include "thread.h" | |
37 | #include "vrf.h" | |
38 | #include "workqueue.h" | |
50d89650 | 39 | #include "nexthop_group_private.h" |
00dffa8c | 40 | #include "frr_pthread.h" |
718e3744 | 41 | |
89272910 | 42 | #include "zebra/zebra_router.h" |
43e52561 QY |
43 | #include "zebra/connected.h" |
44 | #include "zebra/debug.h" | |
45 | #include "zebra/interface.h" | |
46 | #include "zebra/redistribute.h" | |
718e3744 | 47 | #include "zebra/rib.h" |
48 | #include "zebra/rt.h" | |
43e52561 QY |
49 | #include "zebra/zapi_msg.h" |
50 | #include "zebra/zebra_errors.h" | |
51 | #include "zebra/zebra_memory.h" | |
7c551956 | 52 | #include "zebra/zebra_ns.h" |
fb018d25 | 53 | #include "zebra/zebra_rnh.h" |
43e52561 QY |
54 | #include "zebra/zebra_routemap.h" |
55 | #include "zebra/zebra_vrf.h" | |
6134fd82 | 56 | #include "zebra/zebra_vxlan.h" |
7cdb1a84 MS |
57 | #include "zebra/zapi_msg.h" |
58 | #include "zebra/zebra_dplane.h" | |
59 | ||
2a99ab95 SW |
60 | DEFINE_MTYPE_STATIC(ZEBRA, RIB_UPDATE_CTX, "Rib update context object"); |
61 | ||
7cdb1a84 MS |
62 | /* |
63 | * Event, list, and mutex for delivery of dataplane results | |
64 | */ | |
65 | static pthread_mutex_t dplane_mutex; | |
66 | static struct thread *t_dplane; | |
25779064 | 67 | static struct dplane_ctx_q rib_dplane_q; |
718e3744 | 68 | |
d62a17ae | 69 | DEFINE_HOOK(rib_update, (struct route_node * rn, const char *reason), |
70 | (rn, reason)) | |
4f8ea50c | 71 | |
6baf7bb8 DS |
72 | /* Should we allow non Quagga processes to delete our routes */ |
73 | extern int allow_delete; | |
74 | ||
718e3744 | 75 | /* Each route type's string and default distance value. */ |
d62a17ae | 76 | static const struct { |
77 | int key; | |
8dc7a759 | 78 | uint8_t distance; |
73547a75 | 79 | uint8_t meta_q_map; |
d62a17ae | 80 | } route_info[ZEBRA_ROUTE_MAX] = { |
e22e8001 SW |
81 | [ZEBRA_ROUTE_NHG] = {ZEBRA_ROUTE_NHG, 255 /* Uneeded for nhg's */, 0}, |
82 | [ZEBRA_ROUTE_SYSTEM] = {ZEBRA_ROUTE_SYSTEM, 0, 5}, | |
83 | [ZEBRA_ROUTE_KERNEL] = {ZEBRA_ROUTE_KERNEL, 0, 1}, | |
84 | [ZEBRA_ROUTE_CONNECT] = {ZEBRA_ROUTE_CONNECT, 0, 1}, | |
85 | [ZEBRA_ROUTE_STATIC] = {ZEBRA_ROUTE_STATIC, 1, 2}, | |
86 | [ZEBRA_ROUTE_RIP] = {ZEBRA_ROUTE_RIP, 120, 3}, | |
87 | [ZEBRA_ROUTE_RIPNG] = {ZEBRA_ROUTE_RIPNG, 120, 3}, | |
88 | [ZEBRA_ROUTE_OSPF] = {ZEBRA_ROUTE_OSPF, 110, 3}, | |
89 | [ZEBRA_ROUTE_OSPF6] = {ZEBRA_ROUTE_OSPF6, 110, 3}, | |
90 | [ZEBRA_ROUTE_ISIS] = {ZEBRA_ROUTE_ISIS, 115, 3}, | |
91 | [ZEBRA_ROUTE_BGP] = {ZEBRA_ROUTE_BGP, 20 /* IBGP is 200. */, 4}, | |
92 | [ZEBRA_ROUTE_PIM] = {ZEBRA_ROUTE_PIM, 255, 5}, | |
93 | [ZEBRA_ROUTE_EIGRP] = {ZEBRA_ROUTE_EIGRP, 90, 3}, | |
94 | [ZEBRA_ROUTE_NHRP] = {ZEBRA_ROUTE_NHRP, 10, 3}, | |
95 | [ZEBRA_ROUTE_HSLS] = {ZEBRA_ROUTE_HSLS, 255, 5}, | |
96 | [ZEBRA_ROUTE_OLSR] = {ZEBRA_ROUTE_OLSR, 255, 5}, | |
97 | [ZEBRA_ROUTE_TABLE] = {ZEBRA_ROUTE_TABLE, 150, 2}, | |
98 | [ZEBRA_ROUTE_LDP] = {ZEBRA_ROUTE_LDP, 150, 5}, | |
99 | [ZEBRA_ROUTE_VNC] = {ZEBRA_ROUTE_VNC, 20, 4}, | |
100 | [ZEBRA_ROUTE_VNC_DIRECT] = {ZEBRA_ROUTE_VNC_DIRECT, 20, 4}, | |
101 | [ZEBRA_ROUTE_VNC_DIRECT_RH] = {ZEBRA_ROUTE_VNC_DIRECT_RH, 20, 4}, | |
102 | [ZEBRA_ROUTE_BGP_DIRECT] = {ZEBRA_ROUTE_BGP_DIRECT, 20, 4}, | |
103 | [ZEBRA_ROUTE_BGP_DIRECT_EXT] = {ZEBRA_ROUTE_BGP_DIRECT_EXT, 20, 4}, | |
104 | [ZEBRA_ROUTE_BABEL] = {ZEBRA_ROUTE_BABEL, 100, 3}, | |
105 | [ZEBRA_ROUTE_SHARP] = {ZEBRA_ROUTE_SHARP, 150, 5}, | |
106 | [ZEBRA_ROUTE_PBR] = {ZEBRA_ROUTE_PBR, 200, 5}, | |
107 | [ZEBRA_ROUTE_BFD] = {ZEBRA_ROUTE_BFD, 255, 5}, | |
108 | [ZEBRA_ROUTE_OPENFABRIC] = {ZEBRA_ROUTE_OPENFABRIC, 115, 3}, | |
109 | [ZEBRA_ROUTE_VRRP] = {ZEBRA_ROUTE_VRRP, 255, 5} | |
d6abd8b0 | 110 | /* Any new route type added to zebra, should be mirrored here */ |
c710b277 | 111 | |
d62a17ae | 112 | /* no entry/default: 150 */ |
718e3744 | 113 | }; |
6b0655a2 | 114 | |
d62a17ae | 115 | static void __attribute__((format(printf, 5, 6))) |
116 | _rnode_zlog(const char *_func, vrf_id_t vrf_id, struct route_node *rn, | |
117 | int priority, const char *msgfmt, ...) | |
2263a412 | 118 | { |
d62a17ae | 119 | char buf[SRCDEST2STR_BUFFER + sizeof(" (MRIB)")]; |
120 | char msgbuf[512]; | |
121 | va_list ap; | |
2263a412 | 122 | |
d62a17ae | 123 | va_start(ap, msgfmt); |
124 | vsnprintf(msgbuf, sizeof(msgbuf), msgfmt, ap); | |
125 | va_end(ap); | |
2263a412 | 126 | |
d62a17ae | 127 | if (rn) { |
128 | rib_table_info_t *info = srcdest_rnode_table_info(rn); | |
129 | srcdest_rnode2str(rn, buf, sizeof(buf)); | |
cb653491 | 130 | |
d62a17ae | 131 | if (info->safi == SAFI_MULTICAST) |
eab4a5c2 | 132 | strlcat(buf, " (MRIB)", sizeof(buf)); |
d62a17ae | 133 | } else { |
134 | snprintf(buf, sizeof(buf), "{(route_node *) NULL}"); | |
135 | } | |
2263a412 | 136 | |
d62a17ae | 137 | zlog(priority, "%s: %d:%s: %s", _func, vrf_id, buf, msgbuf); |
2263a412 DL |
138 | } |
139 | ||
d62a17ae | 140 | #define rnode_debug(node, vrf_id, ...) \ |
2263a412 | 141 | _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__) |
d62a17ae | 142 | #define rnode_info(node, ...) \ |
2263a412 DL |
143 | _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__) |
144 | ||
fd289fc8 | 145 | uint8_t route_distance(int type) |
40c7bdb0 | 146 | { |
fd289fc8 | 147 | uint8_t distance; |
40c7bdb0 | 148 | |
d62a17ae | 149 | if ((unsigned)type >= array_size(route_info)) |
150 | distance = 150; | |
151 | else | |
152 | distance = route_info[type].distance; | |
40c7bdb0 | 153 | |
d62a17ae | 154 | return distance; |
40c7bdb0 | 155 | } |
156 | ||
d7c0a89a | 157 | int is_zebra_valid_kernel_table(uint32_t table_id) |
7a4bb9c5 | 158 | { |
8f500a1c | 159 | #ifdef linux |
d62a17ae | 160 | if ((table_id == RT_TABLE_UNSPEC) || (table_id == RT_TABLE_LOCAL) |
161 | || (table_id == RT_TABLE_COMPAT)) | |
162 | return 0; | |
8f500a1c RW |
163 | #endif |
164 | ||
d62a17ae | 165 | return 1; |
7a4bb9c5 DS |
166 | } |
167 | ||
d7c0a89a | 168 | int is_zebra_main_routing_table(uint32_t table_id) |
7a4bb9c5 | 169 | { |
c447ad08 | 170 | if (table_id == RT_TABLE_MAIN) |
d62a17ae | 171 | return 1; |
172 | return 0; | |
7a4bb9c5 DS |
173 | } |
174 | ||
86391e56 | 175 | int zebra_check_addr(const struct prefix *p) |
0aabccc0 | 176 | { |
d62a17ae | 177 | if (p->family == AF_INET) { |
d7c0a89a | 178 | uint32_t addr; |
0aabccc0 | 179 | |
d62a17ae | 180 | addr = p->u.prefix4.s_addr; |
181 | addr = ntohl(addr); | |
0aabccc0 | 182 | |
d62a17ae | 183 | if (IPV4_NET127(addr) || IN_CLASSD(addr) |
184 | || IPV4_LINKLOCAL(addr)) | |
185 | return 0; | |
186 | } | |
187 | if (p->family == AF_INET6) { | |
188 | if (IN6_IS_ADDR_LOOPBACK(&p->u.prefix6)) | |
189 | return 0; | |
190 | if (IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6)) | |
191 | return 0; | |
192 | } | |
193 | return 1; | |
0aabccc0 DD |
194 | } |
195 | ||
fa713d9e | 196 | /* Add nexthop to the end of a rib node's nexthop list */ |
d62a17ae | 197 | void route_entry_nexthop_add(struct route_entry *re, struct nexthop *nexthop) |
fa713d9e | 198 | { |
6b468511 | 199 | _nexthop_group_add_sorted(re->ng, nexthop); |
718e3744 | 200 | } |
201 | ||
6e26278c | 202 | |
6e26278c DS |
203 | /** |
204 | * copy_nexthop - copy a nexthop to the rib structure. | |
205 | */ | |
d62a17ae | 206 | void route_entry_copy_nexthops(struct route_entry *re, struct nexthop *nh) |
6e26278c | 207 | { |
6b468511 DS |
208 | assert(!re->ng->nexthop); |
209 | copy_nexthops(&re->ng->nexthop, nh, NULL); | |
6e26278c DS |
210 | } |
211 | ||
718e3744 | 212 | /* Delete specified nexthop from the list. */ |
d62a17ae | 213 | void route_entry_nexthop_delete(struct route_entry *re, struct nexthop *nexthop) |
718e3744 | 214 | { |
d62a17ae | 215 | if (nexthop->next) |
216 | nexthop->next->prev = nexthop->prev; | |
217 | if (nexthop->prev) | |
218 | nexthop->prev->next = nexthop->next; | |
219 | else | |
6b468511 | 220 | re->ng->nexthop = nexthop->next; |
718e3744 | 221 | } |
222 | ||
fa713d9e | 223 | |
d62a17ae | 224 | struct nexthop *route_entry_nexthop_ifindex_add(struct route_entry *re, |
4a7371e9 DS |
225 | ifindex_t ifindex, |
226 | vrf_id_t nh_vrf_id) | |
718e3744 | 227 | { |
d62a17ae | 228 | struct nexthop *nexthop; |
718e3744 | 229 | |
d62a17ae | 230 | nexthop = nexthop_new(); |
231 | nexthop->type = NEXTHOP_TYPE_IFINDEX; | |
232 | nexthop->ifindex = ifindex; | |
4a7371e9 | 233 | nexthop->vrf_id = nh_vrf_id; |
718e3744 | 234 | |
d62a17ae | 235 | route_entry_nexthop_add(re, nexthop); |
718e3744 | 236 | |
d62a17ae | 237 | return nexthop; |
718e3744 | 238 | } |
239 | ||
d62a17ae | 240 | struct nexthop *route_entry_nexthop_ipv4_add(struct route_entry *re, |
241 | struct in_addr *ipv4, | |
4a7371e9 DS |
242 | struct in_addr *src, |
243 | vrf_id_t nh_vrf_id) | |
718e3744 | 244 | { |
d62a17ae | 245 | struct nexthop *nexthop; |
718e3744 | 246 | |
d62a17ae | 247 | nexthop = nexthop_new(); |
248 | nexthop->type = NEXTHOP_TYPE_IPV4; | |
4a7371e9 | 249 | nexthop->vrf_id = nh_vrf_id; |
d62a17ae | 250 | nexthop->gate.ipv4 = *ipv4; |
251 | if (src) | |
252 | nexthop->src.ipv4 = *src; | |
718e3744 | 253 | |
d62a17ae | 254 | route_entry_nexthop_add(re, nexthop); |
718e3744 | 255 | |
d62a17ae | 256 | return nexthop; |
718e3744 | 257 | } |
258 | ||
d62a17ae | 259 | struct nexthop *route_entry_nexthop_ipv4_ifindex_add(struct route_entry *re, |
260 | struct in_addr *ipv4, | |
261 | struct in_addr *src, | |
4a7371e9 DS |
262 | ifindex_t ifindex, |
263 | vrf_id_t nh_vrf_id) | |
718e3744 | 264 | { |
d62a17ae | 265 | struct nexthop *nexthop; |
266 | struct interface *ifp; | |
718e3744 | 267 | |
d62a17ae | 268 | nexthop = nexthop_new(); |
4a7371e9 | 269 | nexthop->vrf_id = nh_vrf_id; |
d62a17ae | 270 | nexthop->type = NEXTHOP_TYPE_IPV4_IFINDEX; |
271 | nexthop->gate.ipv4 = *ipv4; | |
272 | if (src) | |
273 | nexthop->src.ipv4 = *src; | |
274 | nexthop->ifindex = ifindex; | |
4a7371e9 | 275 | ifp = if_lookup_by_index(nexthop->ifindex, nh_vrf_id); |
d62a17ae | 276 | /*Pending: need to think if null ifp here is ok during bootup? |
277 | There was a crash because ifp here was coming to be NULL */ | |
278 | if (ifp) | |
2b83602b | 279 | if (connected_is_unnumbered(ifp)) |
d62a17ae | 280 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK); |
718e3744 | 281 | |
d62a17ae | 282 | route_entry_nexthop_add(re, nexthop); |
718e3744 | 283 | |
d62a17ae | 284 | return nexthop; |
718e3744 | 285 | } |
286 | ||
d62a17ae | 287 | struct nexthop *route_entry_nexthop_ipv6_add(struct route_entry *re, |
4a7371e9 DS |
288 | struct in6_addr *ipv6, |
289 | vrf_id_t nh_vrf_id) | |
718e3744 | 290 | { |
d62a17ae | 291 | struct nexthop *nexthop; |
718e3744 | 292 | |
d62a17ae | 293 | nexthop = nexthop_new(); |
4a7371e9 | 294 | nexthop->vrf_id = nh_vrf_id; |
d62a17ae | 295 | nexthop->type = NEXTHOP_TYPE_IPV6; |
296 | nexthop->gate.ipv6 = *ipv6; | |
718e3744 | 297 | |
d62a17ae | 298 | route_entry_nexthop_add(re, nexthop); |
718e3744 | 299 | |
d62a17ae | 300 | return nexthop; |
718e3744 | 301 | } |
302 | ||
d62a17ae | 303 | struct nexthop *route_entry_nexthop_ipv6_ifindex_add(struct route_entry *re, |
304 | struct in6_addr *ipv6, | |
4a7371e9 DS |
305 | ifindex_t ifindex, |
306 | vrf_id_t nh_vrf_id) | |
718e3744 | 307 | { |
d62a17ae | 308 | struct nexthop *nexthop; |
718e3744 | 309 | |
d62a17ae | 310 | nexthop = nexthop_new(); |
4a7371e9 | 311 | nexthop->vrf_id = nh_vrf_id; |
d62a17ae | 312 | nexthop->type = NEXTHOP_TYPE_IPV6_IFINDEX; |
313 | nexthop->gate.ipv6 = *ipv6; | |
314 | nexthop->ifindex = ifindex; | |
718e3744 | 315 | |
d62a17ae | 316 | route_entry_nexthop_add(re, nexthop); |
718e3744 | 317 | |
d62a17ae | 318 | return nexthop; |
718e3744 | 319 | } |
718e3744 | 320 | |
a8309422 | 321 | struct nexthop *route_entry_nexthop_blackhole_add(struct route_entry *re, |
60466a63 | 322 | enum blackhole_type bh_type) |
595db7f1 | 323 | { |
d62a17ae | 324 | struct nexthop *nexthop; |
595db7f1 | 325 | |
d62a17ae | 326 | nexthop = nexthop_new(); |
4a7371e9 | 327 | nexthop->vrf_id = VRF_DEFAULT; |
d62a17ae | 328 | nexthop->type = NEXTHOP_TYPE_BLACKHOLE; |
a8309422 | 329 | nexthop->bh_type = bh_type; |
595db7f1 | 330 | |
d62a17ae | 331 | route_entry_nexthop_add(re, nexthop); |
595db7f1 | 332 | |
d62a17ae | 333 | return nexthop; |
595db7f1 | 334 | } |
335 | ||
d62a17ae | 336 | struct route_entry *rib_match(afi_t afi, safi_t safi, vrf_id_t vrf_id, |
337 | union g_addr *addr, struct route_node **rn_out) | |
338 | { | |
339 | struct prefix p; | |
340 | struct route_table *table; | |
341 | struct route_node *rn; | |
5f7a4718 | 342 | struct route_entry *match = NULL; |
d62a17ae | 343 | |
344 | /* Lookup table. */ | |
345 | table = zebra_vrf_table(afi, safi, vrf_id); | |
346 | if (!table) | |
347 | return 0; | |
348 | ||
349 | memset(&p, 0, sizeof(struct prefix)); | |
350 | p.family = afi; | |
351 | if (afi == AFI_IP) { | |
352 | p.u.prefix4 = addr->ipv4; | |
353 | p.prefixlen = IPV4_MAX_PREFIXLEN; | |
354 | } else { | |
355 | p.u.prefix6 = addr->ipv6; | |
356 | p.prefixlen = IPV6_MAX_PREFIXLEN; | |
357 | } | |
718e3744 | 358 | |
d62a17ae | 359 | rn = route_node_match(table, (struct prefix *)&p); |
718e3744 | 360 | |
d62a17ae | 361 | while (rn) { |
5f7a4718 DS |
362 | rib_dest_t *dest; |
363 | ||
d62a17ae | 364 | route_unlock_node(rn); |
718e3744 | 365 | |
5f7a4718 | 366 | dest = rib_dest_from_rnode(rn); |
996c9314 LB |
367 | if (dest && dest->selected_fib |
368 | && !CHECK_FLAG(dest->selected_fib->status, | |
369 | ROUTE_ENTRY_REMOVED)) | |
5f7a4718 | 370 | match = dest->selected_fib; |
718e3744 | 371 | |
d62a17ae | 372 | /* If there is no selected route or matched route is EGP, go up |
373 | tree. */ | |
374 | if (!match) { | |
375 | do { | |
376 | rn = rn->parent; | |
377 | } while (rn && rn->info == NULL); | |
378 | if (rn) | |
379 | route_lock_node(rn); | |
380 | } else { | |
381 | if (match->type != ZEBRA_ROUTE_CONNECT) { | |
677c1dd5 DS |
382 | if (!CHECK_FLAG(match->status, |
383 | ROUTE_ENTRY_INSTALLED)) | |
d62a17ae | 384 | return NULL; |
385 | } | |
386 | ||
387 | if (rn_out) | |
388 | *rn_out = rn; | |
389 | return match; | |
390 | } | |
16814f96 | 391 | } |
d62a17ae | 392 | return NULL; |
393 | } | |
394 | ||
395 | struct route_entry *rib_match_ipv4_multicast(vrf_id_t vrf_id, | |
396 | struct in_addr addr, | |
397 | struct route_node **rn_out) | |
398 | { | |
399 | struct route_entry *re = NULL, *mre = NULL, *ure = NULL; | |
400 | struct route_node *m_rn = NULL, *u_rn = NULL; | |
401 | union g_addr gaddr = {.ipv4 = addr}; | |
402 | ||
526052fb | 403 | switch (zrouter.ipv4_multicast_mode) { |
d62a17ae | 404 | case MCAST_MRIB_ONLY: |
405 | return rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr, | |
406 | rn_out); | |
407 | case MCAST_URIB_ONLY: | |
408 | return rib_match(AFI_IP, SAFI_UNICAST, vrf_id, &gaddr, rn_out); | |
409 | case MCAST_NO_CONFIG: | |
410 | case MCAST_MIX_MRIB_FIRST: | |
411 | re = mre = rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr, | |
412 | &m_rn); | |
413 | if (!mre) | |
414 | re = ure = rib_match(AFI_IP, SAFI_UNICAST, vrf_id, | |
415 | &gaddr, &u_rn); | |
416 | break; | |
417 | case MCAST_MIX_DISTANCE: | |
418 | mre = rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr, &m_rn); | |
419 | ure = rib_match(AFI_IP, SAFI_UNICAST, vrf_id, &gaddr, &u_rn); | |
420 | if (mre && ure) | |
421 | re = ure->distance < mre->distance ? ure : mre; | |
422 | else if (mre) | |
423 | re = mre; | |
424 | else if (ure) | |
425 | re = ure; | |
426 | break; | |
427 | case MCAST_MIX_PFXLEN: | |
428 | mre = rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr, &m_rn); | |
429 | ure = rib_match(AFI_IP, SAFI_UNICAST, vrf_id, &gaddr, &u_rn); | |
430 | if (mre && ure) | |
431 | re = u_rn->p.prefixlen > m_rn->p.prefixlen ? ure : mre; | |
432 | else if (mre) | |
433 | re = mre; | |
434 | else if (ure) | |
435 | re = ure; | |
436 | break; | |
718e3744 | 437 | } |
33550aa8 | 438 | |
d62a17ae | 439 | if (rn_out) |
440 | *rn_out = (re == mre) ? m_rn : u_rn; | |
718e3744 | 441 | |
d62a17ae | 442 | if (IS_ZEBRA_DEBUG_RIB) { |
443 | char buf[BUFSIZ]; | |
444 | inet_ntop(AF_INET, &addr, buf, BUFSIZ); | |
4623d897 | 445 | |
32391aff DS |
446 | zlog_debug("%s: %s: vrf: %u found %s, using %s", |
447 | __func__, buf, vrf_id, | |
d62a17ae | 448 | mre ? (ure ? "MRIB+URIB" : "MRIB") |
449 | : ure ? "URIB" : "nothing", | |
450 | re == ure ? "URIB" : re == mre ? "MRIB" : "none"); | |
451 | } | |
452 | return re; | |
4623d897 DL |
453 | } |
454 | ||
d62a17ae | 455 | struct route_entry *rib_lookup_ipv4(struct prefix_ipv4 *p, vrf_id_t vrf_id) |
718e3744 | 456 | { |
d62a17ae | 457 | struct route_table *table; |
458 | struct route_node *rn; | |
5f7a4718 | 459 | struct route_entry *match = NULL; |
5f7a4718 | 460 | rib_dest_t *dest; |
718e3744 | 461 | |
d62a17ae | 462 | /* Lookup table. */ |
463 | table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id); | |
464 | if (!table) | |
465 | return 0; | |
718e3744 | 466 | |
d62a17ae | 467 | rn = route_node_lookup(table, (struct prefix *)p); |
718e3744 | 468 | |
d62a17ae | 469 | /* No route for this prefix. */ |
470 | if (!rn) | |
471 | return NULL; | |
718e3744 | 472 | |
d62a17ae | 473 | /* Unlock node. */ |
474 | route_unlock_node(rn); | |
5f7a4718 | 475 | dest = rib_dest_from_rnode(rn); |
718e3744 | 476 | |
996c9314 LB |
477 | if (dest && dest->selected_fib |
478 | && !CHECK_FLAG(dest->selected_fib->status, ROUTE_ENTRY_REMOVED)) | |
5f7a4718 | 479 | match = dest->selected_fib; |
718e3744 | 480 | |
d62a17ae | 481 | if (!match) |
482 | return NULL; | |
718e3744 | 483 | |
d62a17ae | 484 | if (match->type == ZEBRA_ROUTE_CONNECT) |
485 | return match; | |
f9e1b38e | 486 | |
677c1dd5 DS |
487 | if (CHECK_FLAG(match->status, ROUTE_ENTRY_INSTALLED)) |
488 | return match; | |
718e3744 | 489 | |
d62a17ae | 490 | return NULL; |
718e3744 | 491 | } |
492 | ||
a64448ba DS |
493 | /* |
494 | * Is this RIB labeled-unicast? It must be of type BGP and all paths | |
495 | * (nexthops) must have a label. | |
496 | */ | |
d62a17ae | 497 | int zebra_rib_labeled_unicast(struct route_entry *re) |
a64448ba | 498 | { |
d62a17ae | 499 | struct nexthop *nexthop = NULL; |
a64448ba | 500 | |
d62a17ae | 501 | if (re->type != ZEBRA_ROUTE_BGP) |
502 | return 0; | |
a64448ba | 503 | |
6b468511 | 504 | for (ALL_NEXTHOPS_PTR(re->ng, nexthop)) |
d62a17ae | 505 | if (!nexthop->nh_label || !nexthop->nh_label->num_labels) |
506 | return 0; | |
6b0655a2 | 507 | |
d62a17ae | 508 | return 1; |
a64448ba | 509 | } |
718e3744 | 510 | |
6ae24471 DS |
511 | /* Update flag indicates whether this is a "replace" or not. Currently, this |
512 | * is only used for IPv4. | |
513 | */ | |
0c555cc6 DS |
514 | void rib_install_kernel(struct route_node *rn, struct route_entry *re, |
515 | struct route_entry *old) | |
718e3744 | 516 | { |
d62a17ae | 517 | struct nexthop *nexthop; |
518 | rib_table_info_t *info = srcdest_rnode_table_info(rn); | |
d62a17ae | 519 | struct zebra_vrf *zvrf = vrf_info_lookup(re->vrf_id); |
97f5b441 MS |
520 | const struct prefix *p, *src_p; |
521 | enum zebra_dplane_result ret; | |
522 | ||
523 | rib_dest_t *dest = rib_dest_from_rnode(rn); | |
718e3744 | 524 | |
d62a17ae | 525 | srcdest_rnode_prefixes(rn, &p, &src_p); |
416ec78d | 526 | |
d62a17ae | 527 | if (info->safi != SAFI_UNICAST) { |
6b468511 | 528 | for (ALL_NEXTHOPS_PTR(re->ng, nexthop)) |
d62a17ae | 529 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB); |
0c555cc6 | 530 | return; |
d62a17ae | 531 | } |
718e3744 | 532 | |
9ef49038 | 533 | |
8dfbc657 SW |
534 | /* |
535 | * Install the resolved nexthop object first. | |
536 | */ | |
f429bd1b | 537 | zebra_nhg_install_kernel(zebra_nhg_lookup_id(re->nhe_id)); |
8dfbc657 | 538 | |
2063a814 DS |
539 | /* |
540 | * If this is a replace to a new RE let the originator of the RE | |
541 | * know that they've lost | |
542 | */ | |
9a9f8926 | 543 | if (old && (old != re) && (old->type != re->type)) |
28610f7e | 544 | zsend_route_notify_owner(old, p, ZAPI_ROUTE_BETTER_ADMIN_WON); |
25b9cb0c | 545 | |
97f5b441 MS |
546 | /* Update fib selection */ |
547 | dest->selected_fib = re; | |
548 | ||
d62a17ae | 549 | /* |
550 | * Make sure we update the FPM any time we send new information to | |
551 | * the kernel. | |
552 | */ | |
553 | hook_call(rib_update, rn, "installing in kernel"); | |
97f5b441 MS |
554 | |
555 | /* Send add or update */ | |
8263d1d0 | 556 | if (old) |
97f5b441 | 557 | ret = dplane_route_update(rn, re, old); |
5709131c | 558 | else |
97f5b441 | 559 | ret = dplane_route_add(rn, re); |
97f5b441 MS |
560 | |
561 | switch (ret) { | |
ea1c14f6 | 562 | case ZEBRA_DPLANE_REQUEST_QUEUED: |
60f98b23 | 563 | SET_FLAG(re->status, ROUTE_ENTRY_QUEUED); |
efe6c026 MS |
564 | |
565 | if (old) { | |
60f98b23 | 566 | SET_FLAG(old->status, ROUTE_ENTRY_QUEUED); |
efe6c026 MS |
567 | |
568 | /* Free old FIB nexthop group */ | |
569 | if (old->fib_ng.nexthop) { | |
570 | nexthops_free(old->fib_ng.nexthop); | |
571 | old->fib_ng.nexthop = NULL; | |
572 | } | |
573 | ||
574 | if (!RIB_SYSTEM_ROUTE(old)) { | |
575 | /* Clear old route's FIB flags */ | |
6b468511 | 576 | for (ALL_NEXTHOPS_PTR(old->ng, nexthop)) { |
efe6c026 MS |
577 | UNSET_FLAG(nexthop->flags, |
578 | NEXTHOP_FLAG_FIB); | |
579 | } | |
580 | } | |
581 | } | |
582 | ||
97f5b441 MS |
583 | if (zvrf) |
584 | zvrf->installs_queued++; | |
1e885672 | 585 | break; |
ea1c14f6 | 586 | case ZEBRA_DPLANE_REQUEST_FAILURE: |
97f5b441 MS |
587 | { |
588 | char str[SRCDEST2STR_BUFFER]; | |
589 | ||
590 | srcdest_rnode2str(rn, str, sizeof(str)); | |
591 | flog_err(EC_ZEBRA_DP_INSTALL_FAIL, | |
592 | "%u:%s: Failed to enqueue dataplane install", | |
593 | re->vrf_id, str); | |
1e885672 | 594 | break; |
97f5b441 | 595 | } |
ea1c14f6 | 596 | case ZEBRA_DPLANE_REQUEST_SUCCESS: |
97f5b441 MS |
597 | if (zvrf) |
598 | zvrf->installs++; | |
1e885672 DS |
599 | break; |
600 | } | |
d62a17ae | 601 | |
0c555cc6 | 602 | return; |
718e3744 | 603 | } |
604 | ||
605 | /* Uninstall the route from kernel. */ | |
0c555cc6 | 606 | void rib_uninstall_kernel(struct route_node *rn, struct route_entry *re) |
718e3744 | 607 | { |
d62a17ae | 608 | struct nexthop *nexthop; |
609 | rib_table_info_t *info = srcdest_rnode_table_info(rn); | |
d62a17ae | 610 | struct zebra_vrf *zvrf = vrf_info_lookup(re->vrf_id); |
05737783 | 611 | |
d62a17ae | 612 | if (info->safi != SAFI_UNICAST) { |
677c1dd5 | 613 | UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED); |
6b468511 | 614 | for (ALL_NEXTHOPS_PTR(re->ng, nexthop)) |
d6792f9d | 615 | UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB); |
0c555cc6 | 616 | return; |
d62a17ae | 617 | } |
416ec78d | 618 | |
d62a17ae | 619 | /* |
620 | * Make sure we update the FPM any time we send new information to | |
97f5b441 | 621 | * the dataplane. |
d62a17ae | 622 | */ |
623 | hook_call(rib_update, rn, "uninstalling from kernel"); | |
97f5b441 MS |
624 | |
625 | switch (dplane_route_delete(rn, re)) { | |
ea1c14f6 | 626 | case ZEBRA_DPLANE_REQUEST_QUEUED: |
97f5b441 MS |
627 | if (zvrf) |
628 | zvrf->removals_queued++; | |
1e885672 | 629 | break; |
ea1c14f6 | 630 | case ZEBRA_DPLANE_REQUEST_FAILURE: |
97f5b441 MS |
631 | { |
632 | char str[SRCDEST2STR_BUFFER]; | |
633 | ||
634 | srcdest_rnode2str(rn, str, sizeof(str)); | |
635 | flog_err(EC_ZEBRA_DP_INSTALL_FAIL, | |
636 | "%u:%s: Failed to enqueue dataplane uninstall", | |
637 | re->vrf_id, str); | |
1e885672 | 638 | break; |
97f5b441 | 639 | } |
ea1c14f6 | 640 | case ZEBRA_DPLANE_REQUEST_SUCCESS: |
1e885672 DS |
641 | if (zvrf) |
642 | zvrf->removals++; | |
643 | break; | |
644 | } | |
718e3744 | 645 | |
0c555cc6 | 646 | return; |
718e3744 | 647 | } |
648 | ||
649 | /* Uninstall the route from kernel. */ | |
d62a17ae | 650 | static void rib_uninstall(struct route_node *rn, struct route_entry *re) |
718e3744 | 651 | { |
d62a17ae | 652 | rib_table_info_t *info = srcdest_rnode_table_info(rn); |
5f7a4718 | 653 | rib_dest_t *dest = rib_dest_from_rnode(rn); |
97f5b441 | 654 | struct nexthop *nexthop; |
416ec78d | 655 | |
5f7a4718 | 656 | if (dest && dest->selected_fib == re) { |
d62a17ae | 657 | if (info->safi == SAFI_UNICAST) |
658 | hook_call(rib_update, rn, "rib_uninstall"); | |
5adc2528 | 659 | |
d62a17ae | 660 | /* If labeled-unicast route, uninstall transit LSP. */ |
661 | if (zebra_rib_labeled_unicast(re)) | |
662 | zebra_mpls_lsp_uninstall(info->zvrf, rn, re); | |
7cdb1a84 | 663 | |
3cdba47a | 664 | rib_uninstall_kernel(rn, re); |
97f5b441 MS |
665 | |
666 | dest->selected_fib = NULL; | |
667 | ||
efe6c026 MS |
668 | /* Free FIB nexthop group, if present */ |
669 | if (re->fib_ng.nexthop) { | |
670 | nexthops_free(re->fib_ng.nexthop); | |
671 | re->fib_ng.nexthop = NULL; | |
672 | } | |
673 | ||
6b468511 | 674 | for (ALL_NEXTHOPS_PTR(re->ng, nexthop)) |
97f5b441 | 675 | UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB); |
d62a17ae | 676 | } |
446bb95e | 677 | |
d62a17ae | 678 | if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)) { |
86391e56 MS |
679 | const struct prefix *p, *src_p; |
680 | ||
d62a17ae | 681 | srcdest_rnode_prefixes(rn, &p, &src_p); |
05737783 | 682 | |
40f321c0 | 683 | redistribute_delete(p, src_p, re, NULL); |
d62a17ae | 684 | UNSET_FLAG(re->flags, ZEBRA_FLAG_SELECTED); |
685 | } | |
718e3744 | 686 | } |
687 | ||
9fd92e3c AS |
688 | /* |
689 | * rib_can_delete_dest | |
690 | * | |
2951a7a4 | 691 | * Returns true if the given dest can be deleted from the table. |
9fd92e3c | 692 | */ |
d62a17ae | 693 | static int rib_can_delete_dest(rib_dest_t *dest) |
9fd92e3c | 694 | { |
045207e2 | 695 | if (re_list_first(&dest->routes)) { |
d62a17ae | 696 | return 0; |
697 | } | |
9fd92e3c | 698 | |
c86ba6c2 DS |
699 | /* |
700 | * Unresolved rnh's are stored on the default route's list | |
701 | * | |
702 | * dest->rnode can also be the source prefix node in an | |
703 | * ipv6 sourcedest table. Fortunately the prefix of a | |
704 | * source prefix node can never be the default prefix. | |
705 | */ | |
706 | if (is_default_prefix(&dest->rnode->p)) | |
707 | return 0; | |
708 | ||
d62a17ae | 709 | /* |
710 | * Don't delete the dest if we have to update the FPM about this | |
711 | * prefix. | |
712 | */ | |
713 | if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_FPM) | |
714 | || CHECK_FLAG(dest->flags, RIB_DEST_SENT_TO_FPM)) | |
715 | return 0; | |
5adc2528 | 716 | |
d62a17ae | 717 | return 1; |
9fd92e3c AS |
718 | } |
719 | ||
699dae23 DS |
720 | void zebra_rib_evaluate_rn_nexthops(struct route_node *rn, uint32_t seq) |
721 | { | |
722 | rib_dest_t *dest = rib_dest_from_rnode(rn); | |
699dae23 DS |
723 | struct rnh *rnh; |
724 | ||
725 | /* | |
50872b08 | 726 | * We are storing the rnh's associated withb |
699dae23 DS |
727 | * the tracked nexthop as a list of the rn's. |
728 | * Unresolved rnh's are placed at the top | |
729 | * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 ) | |
730 | * As such for each rn we need to walk up the tree | |
731 | * and see if any rnh's need to see if they | |
732 | * would match a more specific route | |
733 | */ | |
734 | while (rn) { | |
50872b08 DS |
735 | if (IS_ZEBRA_DEBUG_NHT_DETAILED) { |
736 | char buf[PREFIX_STRLEN]; | |
737 | ||
0a7be328 | 738 | zlog_debug("%s: %s Being examined for Nexthop Tracking Count: %zd", |
50872b08 | 739 | __PRETTY_FUNCTION__, |
0a7be328 DS |
740 | srcdest_rnode2str(rn, buf, sizeof(buf)), |
741 | dest ? rnh_list_count(&dest->nht) : 0); | |
50872b08 | 742 | } |
699dae23 DS |
743 | if (!dest) { |
744 | rn = rn->parent; | |
745 | if (rn) | |
746 | dest = rib_dest_from_rnode(rn); | |
747 | continue; | |
748 | } | |
749 | /* | |
750 | * If we have any rnh's stored in the nht list | |
751 | * then we know that this route node was used for | |
752 | * nht resolution and as such we need to call the | |
753 | * nexthop tracking evaluation code | |
754 | */ | |
739c9c90 | 755 | frr_each_safe(rnh_list, &dest->nht, rnh) { |
699dae23 DS |
756 | struct zebra_vrf *zvrf = |
757 | zebra_vrf_lookup_by_id(rnh->vrf_id); | |
758 | struct prefix *p = &rnh->node->p; | |
759 | ||
50872b08 | 760 | if (IS_ZEBRA_DEBUG_NHT_DETAILED) { |
699dae23 DS |
761 | char buf1[PREFIX_STRLEN]; |
762 | char buf2[PREFIX_STRLEN]; | |
763 | ||
0a7be328 | 764 | zlog_debug("%u:%s has Nexthop(%s) Type: %s depending on it, evaluating %u:%u", |
699dae23 | 765 | zvrf->vrf->vrf_id, |
50872b08 | 766 | srcdest_rnode2str(rn, buf1, |
699dae23 DS |
767 | sizeof(buf1)), |
768 | prefix2str(p, buf2, sizeof(buf2)), | |
0a7be328 | 769 | rnh_type2str(rnh->type), |
699dae23 DS |
770 | seq, rnh->seqno); |
771 | } | |
772 | ||
773 | /* | |
774 | * If we have evaluated this node on this pass | |
775 | * already, due to following the tree up | |
776 | * then we know that we can move onto the next | |
777 | * rnh to process. | |
778 | * | |
779 | * Additionally we call zebra_evaluate_rnh | |
780 | * when we gc the dest. In this case we know | |
781 | * that there must be no other re's where | |
782 | * we were originally as such we know that | |
783 | * that sequence number is ok to respect. | |
784 | */ | |
50872b08 DS |
785 | if (rnh->seqno == seq) { |
786 | if (IS_ZEBRA_DEBUG_NHT_DETAILED) | |
787 | zlog_debug( | |
788 | "\tNode processed and moved already"); | |
699dae23 | 789 | continue; |
50872b08 | 790 | } |
699dae23 DS |
791 | |
792 | rnh->seqno = seq; | |
793 | zebra_evaluate_rnh(zvrf, family2afi(p->family), 0, | |
794 | rnh->type, p); | |
795 | } | |
796 | ||
797 | rn = rn->parent; | |
798 | if (rn) | |
799 | dest = rib_dest_from_rnode(rn); | |
800 | } | |
801 | } | |
802 | ||
9fd92e3c AS |
803 | /* |
804 | * rib_gc_dest | |
805 | * | |
806 | * Garbage collect the rib dest corresponding to the given route node | |
807 | * if appropriate. | |
808 | * | |
2951a7a4 | 809 | * Returns true if the dest was deleted, false otherwise. |
9fd92e3c | 810 | */ |
d62a17ae | 811 | int rib_gc_dest(struct route_node *rn) |
9fd92e3c | 812 | { |
d62a17ae | 813 | rib_dest_t *dest; |
9fd92e3c | 814 | |
d62a17ae | 815 | dest = rib_dest_from_rnode(rn); |
816 | if (!dest) | |
817 | return 0; | |
9fd92e3c | 818 | |
d62a17ae | 819 | if (!rib_can_delete_dest(dest)) |
820 | return 0; | |
9fd92e3c | 821 | |
c9abf558 DS |
822 | if (IS_ZEBRA_DEBUG_RIB) { |
823 | struct zebra_vrf *zvrf; | |
824 | ||
825 | zvrf = rib_dest_vrf(dest); | |
d62a17ae | 826 | rnode_debug(rn, zvrf_id(zvrf), "removing dest from table"); |
c9abf558 | 827 | } |
9fd92e3c | 828 | |
699dae23 DS |
829 | zebra_rib_evaluate_rn_nexthops(rn, zebra_router_get_next_sequence()); |
830 | ||
d62a17ae | 831 | dest->rnode = NULL; |
aa57abfb | 832 | rnh_list_fini(&dest->nht); |
d62a17ae | 833 | XFREE(MTYPE_RIB_DEST, dest); |
834 | rn->info = NULL; | |
9fd92e3c | 835 | |
d62a17ae | 836 | /* |
837 | * Release the one reference that we keep on the route node. | |
838 | */ | |
839 | route_unlock_node(rn); | |
840 | return 1; | |
9fd92e3c AS |
841 | } |
842 | ||
d62a17ae | 843 | static void rib_process_add_fib(struct zebra_vrf *zvrf, struct route_node *rn, |
844 | struct route_entry *new) | |
3e5c6e00 | 845 | { |
d62a17ae | 846 | hook_call(rib_update, rn, "new route selected"); |
3e5c6e00 | 847 | |
d62a17ae | 848 | /* Update real nexthop. This may actually determine if nexthop is active |
849 | * or not. */ | |
6b468511 | 850 | if (!nexthop_group_active_nexthop_num(new->ng)) { |
d62a17ae | 851 | UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED); |
852 | return; | |
853 | } | |
3e5c6e00 | 854 | |
d62a17ae | 855 | if (IS_ZEBRA_DEBUG_RIB) { |
856 | char buf[SRCDEST2STR_BUFFER]; | |
857 | srcdest_rnode2str(rn, buf, sizeof(buf)); | |
2da33d6b DS |
858 | zlog_debug("%u:%s: Adding route rn %p, re %p (%s)", |
859 | zvrf_id(zvrf), buf, rn, new, | |
860 | zebra_route_string(new->type)); | |
d62a17ae | 861 | } |
3e5c6e00 | 862 | |
d62a17ae | 863 | /* If labeled-unicast route, install transit LSP. */ |
864 | if (zebra_rib_labeled_unicast(new)) | |
865 | zebra_mpls_lsp_install(zvrf, rn, new); | |
a64448ba | 866 | |
3cdba47a | 867 | rib_install_kernel(rn, new, NULL); |
3e5c6e00 | 868 | |
d62a17ae | 869 | UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED); |
3e5c6e00 | 870 | } |
871 | ||
d62a17ae | 872 | static void rib_process_del_fib(struct zebra_vrf *zvrf, struct route_node *rn, |
873 | struct route_entry *old) | |
3e5c6e00 | 874 | { |
d62a17ae | 875 | hook_call(rib_update, rn, "removing existing route"); |
3e5c6e00 | 876 | |
d62a17ae | 877 | /* Uninstall from kernel. */ |
878 | if (IS_ZEBRA_DEBUG_RIB) { | |
879 | char buf[SRCDEST2STR_BUFFER]; | |
880 | srcdest_rnode2str(rn, buf, sizeof(buf)); | |
2da33d6b DS |
881 | zlog_debug("%u:%s: Deleting route rn %p, re %p (%s)", |
882 | zvrf_id(zvrf), buf, rn, old, | |
883 | zebra_route_string(old->type)); | |
d62a17ae | 884 | } |
3e5c6e00 | 885 | |
d62a17ae | 886 | /* If labeled-unicast route, uninstall transit LSP. */ |
887 | if (zebra_rib_labeled_unicast(old)) | |
888 | zebra_mpls_lsp_uninstall(zvrf, rn, old); | |
889 | ||
3cdba47a | 890 | rib_uninstall_kernel(rn, old); |
d62a17ae | 891 | |
892 | /* Update nexthop for route, reset changed flag. */ | |
ef57f35f DL |
893 | /* Note: this code also handles the Linux case when an interface goes |
894 | * down, causing the kernel to delete routes without sending DELROUTE | |
895 | * notifications | |
896 | */ | |
6883bf8d | 897 | if (RIB_KERNEL_ROUTE(old)) |
212df1de PG |
898 | SET_FLAG(old->status, ROUTE_ENTRY_REMOVED); |
899 | else | |
900 | UNSET_FLAG(old->status, ROUTE_ENTRY_CHANGED); | |
d62a17ae | 901 | } |
902 | ||
903 | static void rib_process_update_fib(struct zebra_vrf *zvrf, | |
904 | struct route_node *rn, | |
905 | struct route_entry *old, | |
906 | struct route_entry *new) | |
907 | { | |
d62a17ae | 908 | int nh_active = 0; |
d62a17ae | 909 | |
910 | /* | |
911 | * We have to install or update if a new route has been selected or | |
912 | * something has changed. | |
913 | */ | |
914 | if (new != old || CHECK_FLAG(new->status, ROUTE_ENTRY_CHANGED)) { | |
915 | hook_call(rib_update, rn, "updating existing route"); | |
916 | ||
917 | /* Update the nexthop; we could determine here that nexthop is | |
918 | * inactive. */ | |
6b468511 | 919 | if (nexthop_group_active_nexthop_num(new->ng)) |
d62a17ae | 920 | nh_active = 1; |
921 | ||
922 | /* If nexthop is active, install the selected route, if | |
923 | * appropriate. If | |
924 | * the install succeeds, cleanup flags for prior route, if | |
925 | * different from | |
926 | * newly selected. | |
927 | */ | |
928 | if (nh_active) { | |
929 | if (IS_ZEBRA_DEBUG_RIB) { | |
930 | char buf[SRCDEST2STR_BUFFER]; | |
931 | srcdest_rnode2str(rn, buf, sizeof(buf)); | |
932 | if (new != old) | |
933 | zlog_debug( | |
2da33d6b | 934 | "%u:%s: Updating route rn %p, re %p (%s) old %p (%s)", |
d62a17ae | 935 | zvrf_id(zvrf), buf, rn, new, |
2da33d6b DS |
936 | zebra_route_string(new->type), |
937 | old, | |
938 | zebra_route_string(old->type)); | |
d62a17ae | 939 | else |
940 | zlog_debug( | |
2da33d6b | 941 | "%u:%s: Updating route rn %p, re %p (%s)", |
d62a17ae | 942 | zvrf_id(zvrf), buf, rn, new, |
2da33d6b | 943 | zebra_route_string(new->type)); |
d62a17ae | 944 | } |
945 | ||
946 | /* If labeled-unicast route, uninstall transit LSP. */ | |
947 | if (zebra_rib_labeled_unicast(old)) | |
948 | zebra_mpls_lsp_uninstall(zvrf, rn, old); | |
949 | ||
3cdba47a DS |
950 | /* |
951 | * Non-system route should be installed. | |
952 | * If labeled-unicast route, install transit | |
953 | * LSP. | |
954 | */ | |
955 | if (zebra_rib_labeled_unicast(new)) | |
956 | zebra_mpls_lsp_install(zvrf, rn, new); | |
d62a17ae | 957 | |
3cdba47a | 958 | rib_install_kernel(rn, new, old); |
d62a17ae | 959 | } |
a64448ba | 960 | |
d62a17ae | 961 | /* |
962 | * If nexthop for selected route is not active or install | |
963 | * failed, we | |
964 | * may need to uninstall and delete for redistribution. | |
965 | */ | |
ed216282 | 966 | if (!nh_active) { |
d62a17ae | 967 | if (IS_ZEBRA_DEBUG_RIB) { |
968 | char buf[SRCDEST2STR_BUFFER]; | |
969 | srcdest_rnode2str(rn, buf, sizeof(buf)); | |
970 | if (new != old) | |
971 | zlog_debug( | |
2da33d6b | 972 | "%u:%s: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive", |
d62a17ae | 973 | zvrf_id(zvrf), buf, rn, new, |
2da33d6b DS |
974 | zebra_route_string(new->type), |
975 | old, | |
976 | zebra_route_string(old->type)); | |
d62a17ae | 977 | else |
978 | zlog_debug( | |
2da33d6b | 979 | "%u:%s: Deleting route rn %p, re %p (%s) - nexthop inactive", |
d62a17ae | 980 | zvrf_id(zvrf), buf, rn, new, |
2da33d6b | 981 | zebra_route_string(new->type)); |
d62a17ae | 982 | } |
983 | ||
984 | /* If labeled-unicast route, uninstall transit LSP. */ | |
985 | if (zebra_rib_labeled_unicast(old)) | |
986 | zebra_mpls_lsp_uninstall(zvrf, rn, old); | |
987 | ||
3cdba47a | 988 | rib_uninstall_kernel(rn, old); |
d62a17ae | 989 | } |
990 | } else { | |
991 | /* | |
992 | * Same route selected; check if in the FIB and if not, | |
3cdba47a DS |
993 | * re-install. This is housekeeping code to deal with |
994 | * race conditions in kernel with linux netlink reporting | |
995 | * interface up before IPv4 or IPv6 protocol is ready | |
d62a17ae | 996 | * to add routes. |
997 | */ | |
3cdba47a DS |
998 | if (!CHECK_FLAG(new->status, ROUTE_ENTRY_INSTALLED) || |
999 | RIB_SYSTEM_ROUTE(new)) | |
677c1dd5 | 1000 | rib_install_kernel(rn, new, NULL); |
d62a17ae | 1001 | } |
1002 | ||
1003 | /* Update prior route. */ | |
6883bf8d | 1004 | if (new != old) |
d62a17ae | 1005 | UNSET_FLAG(old->status, ROUTE_ENTRY_CHANGED); |
3e5c6e00 | 1006 | |
d62a17ae | 1007 | /* Clear changed flag. */ |
1008 | UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED); | |
3e5c6e00 | 1009 | } |
1010 | ||
d62a17ae | 1011 | /* Check if 'alternate' RIB entry is better than 'current'. */ |
1012 | static struct route_entry *rib_choose_best(struct route_entry *current, | |
1013 | struct route_entry *alternate) | |
1014 | { | |
1015 | if (current == NULL) | |
1016 | return alternate; | |
1017 | ||
1018 | /* filter route selection in following order: | |
1019 | * - connected beats other types | |
fec4ca19 | 1020 | * - if both connected, loopback or vrf wins |
d62a17ae | 1021 | * - lower distance beats higher |
1022 | * - lower metric beats higher for equal distance | |
1023 | * - last, hence oldest, route wins tie break. | |
1024 | */ | |
1025 | ||
fec4ca19 DS |
1026 | /* Connected routes. Check to see if either are a vrf |
1027 | * or loopback interface. If not, pick the last connected | |
d62a17ae | 1028 | * route of the set of lowest metric connected routes. |
1029 | */ | |
1030 | if (alternate->type == ZEBRA_ROUTE_CONNECT) { | |
fec4ca19 DS |
1031 | if (current->type != ZEBRA_ROUTE_CONNECT) |
1032 | return alternate; | |
1033 | ||
1034 | /* both are connected. are either loop or vrf? */ | |
1035 | struct nexthop *nexthop = NULL; | |
1036 | ||
6b468511 | 1037 | for (ALL_NEXTHOPS_PTR(alternate->ng, nexthop)) { |
873fde8c | 1038 | struct interface *ifp = if_lookup_by_index( |
1039 | nexthop->ifindex, alternate->vrf_id); | |
1040 | ||
1041 | if (ifp && if_is_loopback_or_vrf(ifp)) | |
fec4ca19 DS |
1042 | return alternate; |
1043 | } | |
1044 | ||
6b468511 | 1045 | for (ALL_NEXTHOPS_PTR(current->ng, nexthop)) { |
873fde8c | 1046 | struct interface *ifp = if_lookup_by_index( |
1047 | nexthop->ifindex, current->vrf_id); | |
1048 | ||
1049 | if (ifp && if_is_loopback_or_vrf(ifp)) | |
fec4ca19 DS |
1050 | return current; |
1051 | } | |
1052 | ||
1053 | /* Neither are loop or vrf so pick best metric */ | |
1054 | if (alternate->metric <= current->metric) | |
d62a17ae | 1055 | return alternate; |
1056 | ||
1057 | return current; | |
1058 | } | |
3e5c6e00 | 1059 | |
d62a17ae | 1060 | if (current->type == ZEBRA_ROUTE_CONNECT) |
1061 | return current; | |
3e5c6e00 | 1062 | |
d62a17ae | 1063 | /* higher distance loses */ |
1064 | if (alternate->distance < current->distance) | |
1065 | return alternate; | |
1066 | if (current->distance < alternate->distance) | |
1067 | return current; | |
3e5c6e00 | 1068 | |
d62a17ae | 1069 | /* metric tie-breaks equal distance */ |
1070 | if (alternate->metric <= current->metric) | |
1071 | return alternate; | |
3e5c6e00 | 1072 | |
d62a17ae | 1073 | return current; |
3e5c6e00 | 1074 | } |
1075 | ||
e22e8001 SW |
1076 | /* Core function for processing nexthop group contexts's off metaq */ |
1077 | static void rib_nhg_process(struct nhg_ctx *ctx) | |
1078 | { | |
1079 | nhg_ctx_process(ctx); | |
1080 | } | |
1081 | ||
d62a17ae | 1082 | /* Core function for processing routing information base. */ |
1083 | static void rib_process(struct route_node *rn) | |
1084 | { | |
1085 | struct route_entry *re; | |
1086 | struct route_entry *next; | |
1087 | struct route_entry *old_selected = NULL; | |
1088 | struct route_entry *new_selected = NULL; | |
1089 | struct route_entry *old_fib = NULL; | |
1090 | struct route_entry *new_fib = NULL; | |
1091 | struct route_entry *best = NULL; | |
1092 | char buf[SRCDEST2STR_BUFFER]; | |
1093 | rib_dest_t *dest; | |
1094 | struct zebra_vrf *zvrf = NULL; | |
86391e56 MS |
1095 | const struct prefix *p, *src_p; |
1096 | ||
d62a17ae | 1097 | srcdest_rnode_prefixes(rn, &p, &src_p); |
1098 | vrf_id_t vrf_id = VRF_UNKNOWN; | |
1099 | ||
1100 | assert(rn); | |
1101 | ||
1102 | dest = rib_dest_from_rnode(rn); | |
1103 | if (dest) { | |
1104 | zvrf = rib_dest_vrf(dest); | |
1105 | vrf_id = zvrf_id(zvrf); | |
1106 | } | |
bab85d4f | 1107 | |
d62a17ae | 1108 | if (IS_ZEBRA_DEBUG_RIB) |
1109 | srcdest_rnode2str(rn, buf, sizeof(buf)); | |
bab85d4f | 1110 | |
d62a17ae | 1111 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) |
1112 | zlog_debug("%u:%s: Processing rn %p", vrf_id, buf, rn); | |
bab85d4f | 1113 | |
607425e5 DS |
1114 | /* |
1115 | * we can have rn's that have a NULL info pointer | |
1116 | * (dest). As such let's not let the deref happen | |
1117 | * additionally we know RNODE_FOREACH_RE_SAFE | |
1118 | * will not iterate so we are ok. | |
1119 | */ | |
1120 | if (dest) | |
1121 | old_fib = dest->selected_fib; | |
5f7a4718 | 1122 | |
a2addae8 | 1123 | RNODE_FOREACH_RE_SAFE (rn, re, next) { |
d62a17ae | 1124 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) |
1125 | zlog_debug( | |
2da33d6b DS |
1126 | "%u:%s: Examine re %p (%s) status %x flags %x dist %d metric %d", |
1127 | vrf_id, buf, re, zebra_route_string(re->type), | |
1128 | re->status, re->flags, re->distance, | |
1129 | re->metric); | |
d62a17ae | 1130 | |
d62a17ae | 1131 | /* Currently selected re. */ |
1132 | if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)) { | |
1133 | assert(old_selected == NULL); | |
1134 | old_selected = re; | |
1135 | } | |
bab85d4f | 1136 | |
d62a17ae | 1137 | /* Skip deleted entries from selection */ |
1138 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) | |
1139 | continue; | |
1140 | ||
1141 | /* Skip unreachable nexthop. */ | |
1142 | /* This first call to nexthop_active_update is merely to | |
6883bf8d DS |
1143 | * determine if there's any change to nexthops associated |
1144 | * with this RIB entry. Now, rib_process() can be invoked due | |
1145 | * to an external event such as link down or due to | |
1146 | * next-hop-tracking evaluation. In the latter case, | |
d62a17ae | 1147 | * a decision has already been made that the NHs have changed. |
6883bf8d DS |
1148 | * So, no need to invoke a potentially expensive call again. |
1149 | * Further, since the change might be in a recursive NH which | |
1150 | * is not caught in the nexthop_active_update() code. Thus, we | |
1151 | * might miss changes to recursive NHs. | |
d62a17ae | 1152 | */ |
6883bf8d | 1153 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED) |
99eabcec | 1154 | && !nexthop_active_update(rn, re)) { |
d62a17ae | 1155 | if (re->type == ZEBRA_ROUTE_TABLE) { |
1156 | /* XXX: HERE BE DRAGONS!!!!! | |
1157 | * In all honesty, I have not yet figured out | |
6883bf8d DS |
1158 | * what this part does or why the |
1159 | * ROUTE_ENTRY_CHANGED test above is correct | |
d62a17ae | 1160 | * or why we need to delete a route here, and |
6883bf8d DS |
1161 | * also not whether this concerns both selected |
1162 | * and fib route, or only selected | |
1163 | * or only fib | |
1164 | * | |
1165 | * This entry was denied by the 'ip protocol | |
1166 | * table' route-map, we need to delete it */ | |
d62a17ae | 1167 | if (re != old_selected) { |
1168 | if (IS_ZEBRA_DEBUG_RIB) | |
1169 | zlog_debug( | |
32391aff | 1170 | "%s: %u:%s: imported via import-table but denied " |
d62a17ae | 1171 | "by the ip protocol table route-map", |
32391aff | 1172 | __func__, vrf_id, buf); |
d62a17ae | 1173 | rib_unlink(rn, re); |
1174 | } else | |
1175 | SET_FLAG(re->status, | |
1176 | ROUTE_ENTRY_REMOVED); | |
1177 | } | |
1178 | ||
1179 | continue; | |
1180 | } | |
bab85d4f | 1181 | |
d62a17ae | 1182 | /* Infinite distance. */ |
1183 | if (re->distance == DISTANCE_INFINITY) { | |
1184 | UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED); | |
1185 | continue; | |
1186 | } | |
bab85d4f | 1187 | |
d62a17ae | 1188 | if (CHECK_FLAG(re->flags, ZEBRA_FLAG_FIB_OVERRIDE)) { |
1189 | best = rib_choose_best(new_fib, re); | |
1190 | if (new_fib && best != new_fib) | |
1191 | UNSET_FLAG(new_fib->status, | |
1192 | ROUTE_ENTRY_CHANGED); | |
1193 | new_fib = best; | |
1194 | } else { | |
1195 | best = rib_choose_best(new_selected, re); | |
1196 | if (new_selected && best != new_selected) | |
1197 | UNSET_FLAG(new_selected->status, | |
1198 | ROUTE_ENTRY_CHANGED); | |
1199 | new_selected = best; | |
1200 | } | |
1201 | if (best != re) | |
1202 | UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED); | |
1203 | } /* RNODE_FOREACH_RE */ | |
1204 | ||
1205 | /* If no FIB override route, use the selected route also for FIB */ | |
1206 | if (new_fib == NULL) | |
1207 | new_fib = new_selected; | |
1208 | ||
1209 | /* After the cycle is finished, the following pointers will be set: | |
1210 | * old_selected --- RE entry currently having SELECTED | |
1211 | * new_selected --- RE entry that is newly SELECTED | |
1212 | * old_fib --- RE entry currently in kernel FIB | |
1213 | * new_fib --- RE entry that is newly to be in kernel FIB | |
1214 | * | |
1215 | * new_selected will get SELECTED flag, and is going to be redistributed | |
1216 | * the zclients. new_fib (which can be new_selected) will be installed | |
1217 | * in kernel. | |
1218 | */ | |
1219 | ||
1220 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) { | |
1221 | zlog_debug( | |
1222 | "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p", | |
1223 | vrf_id, buf, (void *)old_selected, (void *)new_selected, | |
1224 | (void *)old_fib, (void *)new_fib); | |
1225 | } | |
446bb95e | 1226 | |
d62a17ae | 1227 | /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if |
1228 | * fib == selected */ | |
9d303b37 DL |
1229 | bool selected_changed = new_selected && CHECK_FLAG(new_selected->status, |
1230 | ROUTE_ENTRY_CHANGED); | |
d62a17ae | 1231 | |
1232 | /* Update fib according to selection results */ | |
1233 | if (new_fib && old_fib) | |
1234 | rib_process_update_fib(zvrf, rn, old_fib, new_fib); | |
1235 | else if (new_fib) | |
1236 | rib_process_add_fib(zvrf, rn, new_fib); | |
1237 | else if (old_fib) | |
1238 | rib_process_del_fib(zvrf, rn, old_fib); | |
1239 | ||
8cb41cd6 | 1240 | /* Update SELECTED entry */ |
d62a17ae | 1241 | if (old_selected != new_selected || selected_changed) { |
93bdadae | 1242 | |
6883bf8d | 1243 | if (new_selected && new_selected != new_fib) |
d62a17ae | 1244 | UNSET_FLAG(new_selected->status, ROUTE_ENTRY_CHANGED); |
41ec9222 | 1245 | |
3cdba47a | 1246 | if (new_selected) |
5af4b346 MS |
1247 | SET_FLAG(new_selected->flags, ZEBRA_FLAG_SELECTED); |
1248 | ||
d62a17ae | 1249 | if (old_selected) { |
40f321c0 MS |
1250 | /* |
1251 | * If we're removing the old entry, we should tell | |
1252 | * redist subscribers about that *if* they aren't | |
1253 | * going to see a redist for the new entry. | |
1254 | */ | |
1255 | if (!new_selected || CHECK_FLAG(old_selected->status, | |
1256 | ROUTE_ENTRY_REMOVED)) | |
1257 | redistribute_delete(p, src_p, | |
1258 | old_selected, | |
1259 | new_selected); | |
1260 | ||
d62a17ae | 1261 | if (old_selected != new_selected) |
1262 | UNSET_FLAG(old_selected->flags, | |
1263 | ZEBRA_FLAG_SELECTED); | |
f857321e | 1264 | } |
d62a17ae | 1265 | } |
3e5c6e00 | 1266 | |
d62a17ae | 1267 | /* Remove all RE entries queued for removal */ |
a2addae8 | 1268 | RNODE_FOREACH_RE_SAFE (rn, re, next) { |
d62a17ae | 1269 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) { |
1270 | if (IS_ZEBRA_DEBUG_RIB) { | |
1271 | rnode_debug(rn, vrf_id, "rn %p, removing re %p", | |
1272 | (void *)rn, (void *)re); | |
1273 | } | |
1274 | rib_unlink(rn, re); | |
1275 | } | |
1276 | } | |
4d38fdb4 | 1277 | |
d62a17ae | 1278 | /* |
1279 | * Check if the dest can be deleted now. | |
1280 | */ | |
1281 | rib_gc_dest(rn); | |
e96f9203 DO |
1282 | } |
1283 | ||
a1494c25 DS |
1284 | static void zebra_rib_evaluate_mpls(struct route_node *rn) |
1285 | { | |
1286 | rib_dest_t *dest = rib_dest_from_rnode(rn); | |
1287 | struct zebra_vrf *zvrf = vrf_info_lookup(VRF_DEFAULT); | |
1288 | ||
1289 | if (!dest) | |
1290 | return; | |
1291 | ||
1292 | if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_LSPS)) { | |
1293 | if (IS_ZEBRA_DEBUG_MPLS) | |
1294 | zlog_debug( | |
1295 | "%u: Scheduling all LSPs upon RIB completion", | |
1296 | zvrf_id(zvrf)); | |
1297 | zebra_mpls_lsp_schedule(zvrf); | |
1298 | mpls_unmark_lsps_for_processing(rn); | |
1299 | } | |
1300 | } | |
1301 | ||
e5ac2adf MS |
1302 | /* |
1303 | * Utility to match route with dplane context data | |
1304 | */ | |
1305 | static bool rib_route_match_ctx(const struct route_entry *re, | |
25779064 MS |
1306 | const struct zebra_dplane_ctx *ctx, |
1307 | bool is_update) | |
e5ac2adf MS |
1308 | { |
1309 | bool result = false; | |
1310 | ||
1311 | if (is_update) { | |
1312 | /* | |
1313 | * In 'update' case, we test info about the 'previous' or | |
1314 | * 'old' route | |
1315 | */ | |
1316 | if ((re->type == dplane_ctx_get_old_type(ctx)) && | |
1317 | (re->instance == dplane_ctx_get_old_instance(ctx))) { | |
1318 | result = true; | |
1319 | ||
1320 | /* TODO -- we're using this extra test, but it's not | |
1321 | * exactly clear why. | |
1322 | */ | |
1323 | if (re->type == ZEBRA_ROUTE_STATIC && | |
1324 | (re->distance != dplane_ctx_get_old_distance(ctx) || | |
1325 | re->tag != dplane_ctx_get_old_tag(ctx))) { | |
1326 | result = false; | |
1327 | } | |
1328 | } | |
1329 | ||
1330 | } else { | |
1331 | /* | |
1332 | * Ordinary, single-route case using primary context info | |
1333 | */ | |
1334 | if ((dplane_ctx_get_op(ctx) != DPLANE_OP_ROUTE_DELETE) && | |
1335 | CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) { | |
1336 | /* Skip route that's been deleted */ | |
1337 | goto done; | |
1338 | } | |
1339 | ||
1340 | if ((re->type == dplane_ctx_get_type(ctx)) && | |
1341 | (re->instance == dplane_ctx_get_instance(ctx))) { | |
1342 | result = true; | |
1343 | ||
1344 | /* TODO -- we're using this extra test, but it's not | |
1345 | * exactly clear why. | |
1346 | */ | |
1347 | if (re->type == ZEBRA_ROUTE_STATIC && | |
1348 | (re->distance != dplane_ctx_get_distance(ctx) || | |
1349 | re->tag != dplane_ctx_get_tag(ctx))) { | |
1350 | result = false; | |
1351 | } | |
1352 | } | |
1353 | } | |
1354 | ||
1355 | done: | |
1356 | ||
1357 | return (result); | |
1358 | } | |
1359 | ||
3cdba47a DS |
1360 | static void zebra_rib_fixup_system(struct route_node *rn) |
1361 | { | |
1362 | struct route_entry *re; | |
1363 | ||
1364 | RNODE_FOREACH_RE(rn, re) { | |
1365 | struct nexthop *nhop; | |
1366 | ||
1367 | if (!RIB_SYSTEM_ROUTE(re)) | |
1368 | continue; | |
1369 | ||
1370 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) | |
1371 | continue; | |
1372 | ||
1373 | SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED); | |
6d0ee6a0 | 1374 | UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED); |
3cdba47a | 1375 | |
6b468511 | 1376 | for (ALL_NEXTHOPS_PTR(re->ng, nhop)) { |
3cdba47a DS |
1377 | if (CHECK_FLAG(nhop->flags, NEXTHOP_FLAG_RECURSIVE)) |
1378 | continue; | |
1379 | ||
1380 | SET_FLAG(nhop->flags, NEXTHOP_FLAG_FIB); | |
1381 | } | |
1382 | } | |
1383 | } | |
1384 | ||
e5ac2adf | 1385 | /* |
78bf56b0 MS |
1386 | * Update a route from a dplane context. This consolidates common code |
1387 | * that can be used in processing of results from FIB updates, and in | |
1388 | * async notification processing. | |
efe6c026 | 1389 | * The return is 'true' if the installed nexthops changed; 'false' otherwise. |
e5ac2adf | 1390 | */ |
efe6c026 MS |
1391 | static bool rib_update_re_from_ctx(struct route_entry *re, |
1392 | struct route_node *rn, | |
1393 | struct zebra_dplane_ctx *ctx) | |
78bf56b0 | 1394 | { |
941e261c | 1395 | char dest_str[PREFIX_STRLEN] = ""; |
efe6c026 | 1396 | char nh_str[NEXTHOP_STRLEN]; |
78bf56b0 | 1397 | struct nexthop *nexthop, *ctx_nexthop; |
941e261c MS |
1398 | bool matched; |
1399 | const struct nexthop_group *ctxnhg; | |
efe6c026 MS |
1400 | bool is_selected = false; /* Is 're' currently the selected re? */ |
1401 | bool changed_p = false; /* Change to nexthops? */ | |
1402 | rib_dest_t *dest; | |
78bf56b0 | 1403 | |
941e261c MS |
1404 | /* Note well: only capturing the prefix string if debug is enabled here; |
1405 | * unconditional log messages will have to generate the string. | |
1406 | */ | |
1407 | if (IS_ZEBRA_DEBUG_RIB) | |
1408 | prefix2str(&(rn->p), dest_str, sizeof(dest_str)); | |
78bf56b0 | 1409 | |
efe6c026 MS |
1410 | dest = rib_dest_from_rnode(rn); |
1411 | if (dest) | |
1412 | is_selected = (re == dest->selected_fib); | |
1413 | ||
1414 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
1415 | zlog_debug("update_from_ctx: %u:%s: %sSELECTED", | |
1416 | re->vrf_id, dest_str, (is_selected ? "" : "NOT ")); | |
1417 | ||
1418 | /* Update zebra's nexthop FIB flag for each nexthop that was installed. | |
941e261c | 1419 | * If the installed set differs from the set requested by the rib/owner, |
efe6c026 MS |
1420 | * we use the fib-specific nexthop-group to record the actual FIB |
1421 | * status. | |
78bf56b0 | 1422 | */ |
941e261c | 1423 | |
986a6617 SW |
1424 | /* Check both fib group and notif group for equivalence. |
1425 | * | |
1426 | * Let's assume the nexthops are ordered here to save time. | |
941e261c | 1427 | */ |
986a6617 SW |
1428 | if (nexthop_group_equal(&re->fib_ng, dplane_ctx_get_ng(ctx)) == false) { |
1429 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) { | |
1430 | zlog_debug( | |
1431 | "%u:%s update_from_ctx: notif nh and fib nh mismatch", | |
1432 | re->vrf_id, dest_str); | |
941e261c MS |
1433 | } |
1434 | ||
986a6617 SW |
1435 | matched = false; |
1436 | } else | |
1437 | matched = true; | |
941e261c MS |
1438 | |
1439 | /* If the new FIB set matches the existing FIB set, we're done. */ | |
1440 | if (matched) { | |
1441 | if (IS_ZEBRA_DEBUG_RIB) | |
1442 | zlog_debug("%u:%s update_from_ctx(): existing fib nhg, no change", | |
1443 | re->vrf_id, dest_str); | |
1444 | goto done; | |
1445 | ||
1446 | } else if (re->fib_ng.nexthop) { | |
1447 | /* | |
1448 | * Free stale fib list and move on to check the rib nhg. | |
1449 | */ | |
1450 | if (IS_ZEBRA_DEBUG_RIB) | |
1451 | zlog_debug("%u:%s update_from_ctx(): replacing fib nhg", | |
1452 | re->vrf_id, dest_str); | |
1453 | nexthops_free(re->fib_ng.nexthop); | |
1454 | re->fib_ng.nexthop = NULL; | |
efe6c026 MS |
1455 | |
1456 | /* Note that the installed nexthops have changed */ | |
1457 | changed_p = true; | |
941e261c MS |
1458 | } else { |
1459 | if (IS_ZEBRA_DEBUG_RIB) | |
1460 | zlog_debug("%u:%s update_from_ctx(): no fib nhg", | |
1461 | re->vrf_id, dest_str); | |
1462 | } | |
1463 | ||
1464 | /* | |
1465 | * Compare with the rib nexthop group. The comparison here is different: | |
1466 | * the RIB group may be a superset of the list installed in the FIB. We | |
1467 | * walk the RIB group, looking for the 'installable' candidate | |
1468 | * nexthops, and then check those against the set | |
1469 | * that is actually installed. | |
986a6617 SW |
1470 | * |
1471 | * Assume nexthops are ordered here as well. | |
941e261c MS |
1472 | */ |
1473 | matched = true; | |
986a6617 SW |
1474 | |
1475 | ctx_nexthop = dplane_ctx_get_ng(ctx)->nexthop; | |
1476 | ||
1477 | /* Get the first `installed` one to check against. | |
1478 | * If the dataplane doesn't set these to be what was actually installed, | |
1479 | * it will just be whatever was in re->ng? | |
1480 | */ | |
1481 | if (CHECK_FLAG(ctx_nexthop->flags, NEXTHOP_FLAG_RECURSIVE) | |
1482 | || !CHECK_FLAG(ctx_nexthop->flags, NEXTHOP_FLAG_ACTIVE)) | |
1483 | ctx_nexthop = nexthop_next_active_resolved(ctx_nexthop); | |
1484 | ||
6b468511 | 1485 | for (ALL_NEXTHOPS_PTR(re->ng, nexthop)) { |
78bf56b0 | 1486 | |
5695d9ac | 1487 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) |
78bf56b0 MS |
1488 | continue; |
1489 | ||
941e261c MS |
1490 | if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
1491 | continue; | |
5695d9ac | 1492 | |
941e261c | 1493 | /* Check for a FIB nexthop corresponding to the RIB nexthop */ |
986a6617 SW |
1494 | if (nexthop_same(ctx_nexthop, nexthop) == false) { |
1495 | /* If the FIB doesn't know about the nexthop, | |
1496 | * it's not installed | |
1497 | */ | |
efe6c026 MS |
1498 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) { |
1499 | nexthop2str(nexthop, nh_str, sizeof(nh_str)); | |
986a6617 SW |
1500 | zlog_debug( |
1501 | "update_from_ctx: no notif match for rib nh %s", | |
1502 | nh_str); | |
efe6c026 | 1503 | } |
941e261c | 1504 | matched = false; |
efe6c026 MS |
1505 | |
1506 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) | |
1507 | changed_p = true; | |
1508 | ||
1509 | UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB); | |
11260e70 MS |
1510 | |
1511 | /* Keep checking nexthops */ | |
1512 | continue; | |
5695d9ac | 1513 | } |
78bf56b0 | 1514 | |
efe6c026 MS |
1515 | if (CHECK_FLAG(ctx_nexthop->flags, NEXTHOP_FLAG_FIB)) { |
1516 | if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) | |
1517 | changed_p = true; | |
1518 | ||
5695d9ac | 1519 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB); |
efe6c026 MS |
1520 | } else { |
1521 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) | |
1522 | changed_p = true; | |
1523 | ||
5695d9ac | 1524 | UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB); |
efe6c026 | 1525 | } |
986a6617 SW |
1526 | |
1527 | ctx_nexthop = nexthop_next_active_resolved(ctx_nexthop); | |
78bf56b0 MS |
1528 | } |
1529 | ||
941e261c MS |
1530 | /* If all nexthops were processed, we're done */ |
1531 | if (matched) { | |
1532 | if (IS_ZEBRA_DEBUG_RIB) | |
efe6c026 MS |
1533 | zlog_debug("%u:%s update_from_ctx(): rib nhg matched, changed '%s'", |
1534 | re->vrf_id, dest_str, | |
1535 | (changed_p ? "true" : "false")); | |
941e261c MS |
1536 | goto done; |
1537 | } | |
78bf56b0 | 1538 | |
941e261c MS |
1539 | /* FIB nexthop set differs from the RIB set: |
1540 | * create a fib-specific nexthop-group | |
1541 | */ | |
1542 | if (IS_ZEBRA_DEBUG_RIB) | |
efe6c026 MS |
1543 | zlog_debug("%u:%s update_from_ctx(): changed %s, adding new fib nhg", |
1544 | re->vrf_id, dest_str, | |
1545 | (changed_p ? "true" : "false")); | |
941e261c MS |
1546 | |
1547 | ctxnhg = dplane_ctx_get_ng(ctx); | |
188a00e0 MS |
1548 | |
1549 | if (ctxnhg->nexthop) | |
1550 | copy_nexthops(&(re->fib_ng.nexthop), ctxnhg->nexthop, NULL); | |
1551 | else { | |
1552 | /* Bit of a special case when the fib has _no_ installed | |
1553 | * nexthops. | |
1554 | */ | |
1555 | nexthop = nexthop_new(); | |
1556 | nexthop->type = NEXTHOP_TYPE_IPV4; | |
50d89650 | 1557 | _nexthop_add(&(re->fib_ng.nexthop), nexthop); |
188a00e0 | 1558 | } |
941e261c MS |
1559 | |
1560 | done: | |
efe6c026 | 1561 | return changed_p; |
78bf56b0 MS |
1562 | } |
1563 | ||
e5ac2adf | 1564 | /* |
fad4d69c MS |
1565 | * Helper to locate a zebra route-node from a dplane context. This is used |
1566 | * when processing dplane results, e.g. Note well: the route-node is returned | |
1567 | * with a ref held - route_unlock_node() must be called eventually. | |
e5ac2adf | 1568 | */ |
fad4d69c MS |
1569 | static struct route_node * |
1570 | rib_find_rn_from_ctx(const struct zebra_dplane_ctx *ctx) | |
e5ac2adf MS |
1571 | { |
1572 | struct route_table *table = NULL; | |
1573 | struct route_node *rn = NULL; | |
e5ac2adf MS |
1574 | const struct prefix *dest_pfx, *src_pfx; |
1575 | ||
1576 | /* Locate rn and re(s) from ctx */ | |
1577 | ||
1578 | table = zebra_vrf_table_with_table_id(dplane_ctx_get_afi(ctx), | |
1579 | dplane_ctx_get_safi(ctx), | |
1580 | dplane_ctx_get_vrf(ctx), | |
1581 | dplane_ctx_get_table(ctx)); | |
1582 | if (table == NULL) { | |
1583 | if (IS_ZEBRA_DEBUG_DPLANE) { | |
fad4d69c | 1584 | zlog_debug("Failed to find route for ctx: no table for afi %d, safi %d, vrf %u", |
e5ac2adf MS |
1585 | dplane_ctx_get_afi(ctx), |
1586 | dplane_ctx_get_safi(ctx), | |
1587 | dplane_ctx_get_vrf(ctx)); | |
1588 | } | |
1589 | goto done; | |
1590 | } | |
1591 | ||
fad4d69c MS |
1592 | dest_pfx = dplane_ctx_get_dest(ctx); |
1593 | src_pfx = dplane_ctx_get_src(ctx); | |
1594 | ||
1595 | rn = srcdest_rnode_get(table, dest_pfx, | |
1596 | src_pfx ? (struct prefix_ipv6 *)src_pfx : NULL); | |
1597 | ||
1598 | done: | |
1599 | return rn; | |
1600 | } | |
1601 | ||
1602 | ||
97f5b441 | 1603 | |
fad4d69c MS |
1604 | /* |
1605 | * Route-update results processing after async dataplane update. | |
1606 | */ | |
1607 | static void rib_process_result(struct zebra_dplane_ctx *ctx) | |
1608 | { | |
1609 | struct zebra_vrf *zvrf = NULL; | |
1610 | struct route_node *rn = NULL; | |
1611 | struct route_entry *re = NULL, *old_re = NULL, *rib; | |
1612 | bool is_update = false; | |
1613 | char dest_str[PREFIX_STRLEN] = ""; | |
1614 | enum dplane_op_e op; | |
1615 | enum zebra_dplane_result status; | |
1616 | const struct prefix *dest_pfx, *src_pfx; | |
1617 | uint32_t seq; | |
efe6c026 | 1618 | bool fib_changed = false; |
97f5b441 | 1619 | |
fad4d69c | 1620 | zvrf = vrf_info_lookup(dplane_ctx_get_vrf(ctx)); |
e5ac2adf MS |
1621 | dest_pfx = dplane_ctx_get_dest(ctx); |
1622 | ||
1623 | /* Note well: only capturing the prefix string if debug is enabled here; | |
1624 | * unconditional log messages will have to generate the string. | |
1625 | */ | |
5709131c | 1626 | if (IS_ZEBRA_DEBUG_DPLANE) |
e5ac2adf | 1627 | prefix2str(dest_pfx, dest_str, sizeof(dest_str)); |
e5ac2adf | 1628 | |
fad4d69c MS |
1629 | /* Locate rn and re(s) from ctx */ |
1630 | rn = rib_find_rn_from_ctx(ctx); | |
e5ac2adf MS |
1631 | if (rn == NULL) { |
1632 | if (IS_ZEBRA_DEBUG_DPLANE) { | |
fe2c53d4 | 1633 | zlog_debug("Failed to process dplane results: no route for %u:%s", |
e5ac2adf MS |
1634 | dplane_ctx_get_vrf(ctx), dest_str); |
1635 | } | |
1636 | goto done; | |
1637 | } | |
1638 | ||
1639 | srcdest_rnode_prefixes(rn, &dest_pfx, &src_pfx); | |
1640 | ||
1641 | op = dplane_ctx_get_op(ctx); | |
1642 | status = dplane_ctx_get_status(ctx); | |
1643 | ||
c831033f | 1644 | if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) |
f183e380 | 1645 | zlog_debug("%u:%s Processing dplane ctx %p, op %s result %s", |
e5ac2adf | 1646 | dplane_ctx_get_vrf(ctx), dest_str, ctx, |
f183e380 | 1647 | dplane_op2str(op), dplane_res2str(status)); |
e5ac2adf | 1648 | |
e5ac2adf MS |
1649 | /* |
1650 | * Update is a bit of a special case, where we may have both old and new | |
1651 | * routes to post-process. | |
1652 | */ | |
1653 | is_update = dplane_ctx_is_update(ctx); | |
1654 | ||
1655 | /* | |
1656 | * Take a pass through the routes, look for matches with the context | |
1657 | * info. | |
1658 | */ | |
1659 | RNODE_FOREACH_RE(rn, rib) { | |
1660 | ||
1661 | if (re == NULL) { | |
5709131c | 1662 | if (rib_route_match_ctx(rib, ctx, false)) |
e5ac2adf | 1663 | re = rib; |
e5ac2adf MS |
1664 | } |
1665 | ||
1666 | /* Check for old route match */ | |
1667 | if (is_update && (old_re == NULL)) { | |
5709131c | 1668 | if (rib_route_match_ctx(rib, ctx, true /*is_update*/)) |
e5ac2adf | 1669 | old_re = rib; |
e5ac2adf MS |
1670 | } |
1671 | ||
1672 | /* Have we found the routes we need to work on? */ | |
5709131c | 1673 | if (re && ((!is_update || old_re))) |
e5ac2adf | 1674 | break; |
e5ac2adf MS |
1675 | } |
1676 | ||
699dae23 DS |
1677 | seq = dplane_ctx_get_seq(ctx); |
1678 | ||
e5ac2adf MS |
1679 | /* |
1680 | * Check sequence number(s) to detect stale results before continuing | |
1681 | */ | |
60f98b23 | 1682 | if (re) { |
699dae23 | 1683 | if (re->dplane_sequence != seq) { |
60f98b23 DS |
1684 | if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) |
1685 | zlog_debug("%u:%s Stale dplane result for re %p", | |
1686 | dplane_ctx_get_vrf(ctx), | |
1687 | dest_str, re); | |
1688 | } else | |
1689 | UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED); | |
e5ac2adf MS |
1690 | } |
1691 | ||
60f98b23 DS |
1692 | if (old_re) { |
1693 | if (old_re->dplane_sequence != dplane_ctx_get_old_seq(ctx)) { | |
1694 | if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) | |
1695 | zlog_debug("%u:%s Stale dplane result for old_re %p", | |
1696 | dplane_ctx_get_vrf(ctx), | |
1697 | dest_str, old_re); | |
1698 | } else | |
9ef0c6ba | 1699 | UNSET_FLAG(old_re->status, ROUTE_ENTRY_QUEUED); |
e5ac2adf MS |
1700 | } |
1701 | ||
12e7fe3a | 1702 | switch (op) { |
12e7fe3a DS |
1703 | case DPLANE_OP_ROUTE_INSTALL: |
1704 | case DPLANE_OP_ROUTE_UPDATE: | |
1705 | if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) { | |
677c1dd5 DS |
1706 | if (re) { |
1707 | UNSET_FLAG(re->status, ROUTE_ENTRY_FAILED); | |
1708 | SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED); | |
1709 | } | |
b9f0e5ee DS |
1710 | /* |
1711 | * On an update operation from the same route type | |
1712 | * context retrieval currently has no way to know | |
1713 | * which was the old and which was the new. | |
1714 | * So don't unset our flags that we just set. | |
1715 | * We know redistribution is ok because the | |
1716 | * old_re in this case is used for nothing | |
1717 | * more than knowing whom to contact if necessary. | |
1718 | */ | |
1719 | if (old_re && old_re != re) { | |
677c1dd5 DS |
1720 | UNSET_FLAG(old_re->status, ROUTE_ENTRY_FAILED); |
1721 | UNSET_FLAG(old_re->status, | |
1722 | ROUTE_ENTRY_INSTALLED); | |
1723 | } | |
f183e380 | 1724 | |
78bf56b0 | 1725 | /* Update zebra route based on the results in |
941e261c | 1726 | * the context struct. |
78bf56b0 | 1727 | */ |
941e261c | 1728 | if (re) { |
efe6c026 MS |
1729 | fib_changed = |
1730 | rib_update_re_from_ctx(re, rn, ctx); | |
1731 | ||
1732 | if (!fib_changed) { | |
1733 | if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) | |
1734 | zlog_debug("%u:%s no fib change for re", | |
1735 | dplane_ctx_get_vrf( | |
1736 | ctx), | |
1737 | dest_str); | |
12e7fe3a DS |
1738 | } |
1739 | ||
941e261c MS |
1740 | /* Redistribute */ |
1741 | redistribute_update(dest_pfx, src_pfx, | |
228a811a | 1742 | re, old_re); |
f183e380 MS |
1743 | } |
1744 | ||
3cdba47a DS |
1745 | /* |
1746 | * System routes are weird in that they | |
1747 | * allow multiple to be installed that match | |
1748 | * to the same prefix, so after we get the | |
1749 | * result we need to clean them up so that | |
1750 | * we can actually use them. | |
1751 | */ | |
1752 | if ((re && RIB_SYSTEM_ROUTE(re)) || | |
1753 | (old_re && RIB_SYSTEM_ROUTE(old_re))) | |
1754 | zebra_rib_fixup_system(rn); | |
1755 | ||
f4c6e2a8 | 1756 | if (zvrf) |
12e7fe3a | 1757 | zvrf->installs++; |
f183e380 | 1758 | |
12e7fe3a | 1759 | /* Notify route owner */ |
677c1dd5 | 1760 | zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_INSTALLED); |
e5ac2adf | 1761 | |
12e7fe3a | 1762 | } else { |
7a230a9d | 1763 | if (re) { |
677c1dd5 | 1764 | SET_FLAG(re->status, ROUTE_ENTRY_FAILED); |
7a230a9d DS |
1765 | UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED); |
1766 | } if (old_re) | |
677c1dd5 DS |
1767 | SET_FLAG(old_re->status, ROUTE_ENTRY_FAILED); |
1768 | if (re) | |
1769 | zsend_route_notify_owner(re, dest_pfx, | |
1770 | ZAPI_ROUTE_FAIL_INSTALL); | |
97f5b441 | 1771 | |
12e7fe3a DS |
1772 | zlog_warn("%u:%s: Route install failed", |
1773 | dplane_ctx_get_vrf(ctx), | |
1774 | prefix2str(dest_pfx, | |
1775 | dest_str, sizeof(dest_str))); | |
1776 | } | |
1777 | break; | |
1778 | case DPLANE_OP_ROUTE_DELETE: | |
677c1dd5 DS |
1779 | if (re) |
1780 | SET_FLAG(re->status, ROUTE_ENTRY_FAILED); | |
12e7fe3a DS |
1781 | /* |
1782 | * In the delete case, the zebra core datastructs were | |
1783 | * updated (or removed) at the time the delete was issued, | |
1784 | * so we're just notifying the route owner. | |
e5ac2adf | 1785 | */ |
12e7fe3a | 1786 | if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) { |
677c1dd5 DS |
1787 | if (re) { |
1788 | UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED); | |
1789 | UNSET_FLAG(re->status, ROUTE_ENTRY_FAILED); | |
1790 | } | |
12e7fe3a | 1791 | zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_REMOVED); |
e5ac2adf | 1792 | |
12e7fe3a DS |
1793 | if (zvrf) |
1794 | zvrf->removals++; | |
1795 | } else { | |
677c1dd5 DS |
1796 | if (re) |
1797 | SET_FLAG(re->status, ROUTE_ENTRY_FAILED); | |
12e7fe3a DS |
1798 | zsend_route_notify_owner_ctx(ctx, |
1799 | ZAPI_ROUTE_REMOVE_FAIL); | |
e5ac2adf | 1800 | |
12e7fe3a DS |
1801 | zlog_warn("%u:%s: Route Deletion failure", |
1802 | dplane_ctx_get_vrf(ctx), | |
1803 | prefix2str(dest_pfx, | |
1804 | dest_str, sizeof(dest_str))); | |
1805 | } | |
3cdba47a DS |
1806 | |
1807 | /* | |
1808 | * System routes are weird in that they | |
1809 | * allow multiple to be installed that match | |
1810 | * to the same prefix, so after we get the | |
1811 | * result we need to clean them up so that | |
1812 | * we can actually use them. | |
1813 | */ | |
1814 | if ((re && RIB_SYSTEM_ROUTE(re)) || | |
1815 | (old_re && RIB_SYSTEM_ROUTE(old_re))) | |
1816 | zebra_rib_fixup_system(rn); | |
12e7fe3a | 1817 | break; |
d37f4d6c MS |
1818 | default: |
1819 | break; | |
e5ac2adf | 1820 | } |
699dae23 DS |
1821 | |
1822 | zebra_rib_evaluate_rn_nexthops(rn, seq); | |
a1494c25 | 1823 | zebra_rib_evaluate_mpls(rn); |
e5ac2adf MS |
1824 | done: |
1825 | ||
5f27bcba DS |
1826 | if (rn) |
1827 | route_unlock_node(rn); | |
1828 | ||
e5ac2adf MS |
1829 | /* Return context to dataplane module */ |
1830 | dplane_ctx_fini(&ctx); | |
1831 | } | |
1832 | ||
54818e3b MS |
1833 | /* |
1834 | * Handle notification from async dataplane: the dataplane has detected | |
1835 | * some change to a route, and notifies zebra so that the control plane | |
1836 | * can reflect that change. | |
1837 | */ | |
1838 | static void rib_process_dplane_notify(struct zebra_dplane_ctx *ctx) | |
1839 | { | |
1840 | struct route_node *rn = NULL; | |
efe6c026 MS |
1841 | struct route_entry *re = NULL; |
1842 | struct nexthop *nexthop; | |
54818e3b MS |
1843 | char dest_str[PREFIX_STRLEN] = ""; |
1844 | const struct prefix *dest_pfx, *src_pfx; | |
941e261c | 1845 | rib_dest_t *dest; |
efe6c026 | 1846 | bool fib_changed = false; |
941e261c | 1847 | bool debug_p = IS_ZEBRA_DEBUG_DPLANE | IS_ZEBRA_DEBUG_RIB; |
efe6c026 | 1848 | int start_count, end_count; |
54818e3b MS |
1849 | dest_pfx = dplane_ctx_get_dest(ctx); |
1850 | ||
1851 | /* Note well: only capturing the prefix string if debug is enabled here; | |
1852 | * unconditional log messages will have to generate the string. | |
1853 | */ | |
941e261c | 1854 | if (debug_p) |
54818e3b MS |
1855 | prefix2str(dest_pfx, dest_str, sizeof(dest_str)); |
1856 | ||
1857 | /* Locate rn and re(s) from ctx */ | |
1858 | rn = rib_find_rn_from_ctx(ctx); | |
1859 | if (rn == NULL) { | |
941e261c MS |
1860 | if (debug_p) { |
1861 | zlog_debug("Failed to process dplane notification: no routes for %u:%s", | |
54818e3b MS |
1862 | dplane_ctx_get_vrf(ctx), dest_str); |
1863 | } | |
1864 | goto done; | |
1865 | } | |
1866 | ||
941e261c | 1867 | dest = rib_dest_from_rnode(rn); |
54818e3b MS |
1868 | srcdest_rnode_prefixes(rn, &dest_pfx, &src_pfx); |
1869 | ||
efe6c026 | 1870 | if (debug_p) |
54818e3b MS |
1871 | zlog_debug("%u:%s Processing dplane notif ctx %p", |
1872 | dplane_ctx_get_vrf(ctx), dest_str, ctx); | |
1873 | ||
1874 | /* | |
1875 | * Take a pass through the routes, look for matches with the context | |
1876 | * info. | |
1877 | */ | |
efe6c026 MS |
1878 | RNODE_FOREACH_RE(rn, re) { |
1879 | if (rib_route_match_ctx(re, ctx, false /*!update*/)) | |
54818e3b MS |
1880 | break; |
1881 | } | |
1882 | ||
1883 | /* No match? Nothing we can do */ | |
1884 | if (re == NULL) { | |
941e261c MS |
1885 | if (debug_p) |
1886 | zlog_debug("%u:%s Unable to process dplane notification: no entry for type %s", | |
54818e3b | 1887 | dplane_ctx_get_vrf(ctx), dest_str, |
941e261c MS |
1888 | zebra_route_string( |
1889 | dplane_ctx_get_type(ctx))); | |
54818e3b MS |
1890 | |
1891 | goto done; | |
1892 | } | |
1893 | ||
272e8903 MS |
1894 | /* Ensure we clear the QUEUED flag */ |
1895 | UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED); | |
1896 | ||
941e261c MS |
1897 | /* Is this a notification that ... matters? We only really care about |
1898 | * the route that is currently selected for installation. | |
1899 | */ | |
1900 | if (re != dest->selected_fib) { | |
1901 | /* TODO -- don't skip processing entirely? We might like to | |
1902 | * at least report on the event. | |
1903 | */ | |
1904 | if (debug_p) | |
efe6c026 | 1905 | zlog_debug("%u:%s dplane notif, but type %s not selected_fib", |
941e261c MS |
1906 | dplane_ctx_get_vrf(ctx), dest_str, |
1907 | zebra_route_string( | |
1908 | dplane_ctx_get_type(ctx))); | |
1909 | goto done; | |
1910 | } | |
1911 | ||
efe6c026 MS |
1912 | /* We'll want to determine whether the installation status of the |
1913 | * route has changed: we'll check the status before processing, | |
1914 | * and then again if there's been a change. | |
1915 | */ | |
1916 | start_count = 0; | |
1917 | for (ALL_NEXTHOPS_PTR(rib_active_nhg(re), nexthop)) { | |
1918 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) | |
1919 | start_count++; | |
1920 | } | |
1921 | ||
54818e3b MS |
1922 | /* Update zebra's nexthop FIB flags based on the context struct's |
1923 | * nexthops. | |
1924 | */ | |
efe6c026 | 1925 | fib_changed = rib_update_re_from_ctx(re, rn, ctx); |
54818e3b | 1926 | |
efe6c026 MS |
1927 | if (!fib_changed) { |
1928 | if (debug_p) | |
54818e3b MS |
1929 | zlog_debug("%u:%s No change from dplane notification", |
1930 | dplane_ctx_get_vrf(ctx), dest_str); | |
efe6c026 MS |
1931 | |
1932 | goto done; | |
54818e3b MS |
1933 | } |
1934 | ||
188a00e0 MS |
1935 | /* |
1936 | * Perform follow-up work if the actual status of the prefix | |
efe6c026 | 1937 | * changed. |
941e261c MS |
1938 | */ |
1939 | ||
efe6c026 MS |
1940 | end_count = 0; |
1941 | for (ALL_NEXTHOPS_PTR(rib_active_nhg(re), nexthop)) { | |
1942 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) | |
1943 | end_count++; | |
1944 | } | |
941e261c | 1945 | |
188a00e0 MS |
1946 | /* Various fib transitions: changed nexthops; from installed to |
1947 | * not-installed; or not-installed to installed. | |
efe6c026 | 1948 | */ |
188a00e0 | 1949 | if (start_count > 0 && end_count > 0) { |
11260e70 MS |
1950 | if (debug_p) |
1951 | zlog_debug("%u:%s applied nexthop changes from dplane notification", | |
1952 | dplane_ctx_get_vrf(ctx), dest_str); | |
188a00e0 MS |
1953 | |
1954 | /* Changed nexthops - update kernel/others */ | |
1955 | dplane_route_notif_update(rn, re, | |
1956 | DPLANE_OP_ROUTE_UPDATE, ctx); | |
1957 | ||
1958 | } else if (start_count == 0 && end_count > 0) { | |
efe6c026 MS |
1959 | if (debug_p) |
1960 | zlog_debug("%u:%s installed transition from dplane notification", | |
1961 | dplane_ctx_get_vrf(ctx), dest_str); | |
1962 | ||
1963 | /* We expect this to be the selected route, so we want | |
272e8903 | 1964 | * to tell others about this transition. |
efe6c026 MS |
1965 | */ |
1966 | SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED); | |
1967 | ||
188a00e0 MS |
1968 | /* Changed nexthops - update kernel/others */ |
1969 | dplane_route_notif_update(rn, re, DPLANE_OP_ROUTE_INSTALL, ctx); | |
1970 | ||
efe6c026 MS |
1971 | /* Redistribute, lsp, and nht update */ |
1972 | redistribute_update(dest_pfx, src_pfx, re, NULL); | |
1973 | ||
1974 | zebra_rib_evaluate_rn_nexthops( | |
1975 | rn, zebra_router_get_next_sequence()); | |
1976 | ||
1977 | zebra_rib_evaluate_mpls(rn); | |
1978 | ||
1979 | } else if (start_count > 0 && end_count == 0) { | |
1980 | if (debug_p) | |
1981 | zlog_debug("%u:%s un-installed transition from dplane notification", | |
1982 | dplane_ctx_get_vrf(ctx), dest_str); | |
1983 | ||
1984 | /* Transition from _something_ installed to _nothing_ | |
1985 | * installed. | |
1986 | */ | |
1987 | /* We expect this to be the selected route, so we want | |
1988 | * to tell others about this transistion. | |
1989 | */ | |
1990 | UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED); | |
1991 | ||
188a00e0 MS |
1992 | /* Changed nexthops - update kernel/others */ |
1993 | dplane_route_notif_update(rn, re, DPLANE_OP_ROUTE_DELETE, ctx); | |
1994 | ||
efe6c026 | 1995 | /* Redistribute, lsp, and nht update */ |
40f321c0 | 1996 | redistribute_delete(dest_pfx, src_pfx, re, NULL); |
efe6c026 MS |
1997 | |
1998 | zebra_rib_evaluate_rn_nexthops( | |
1999 | rn, zebra_router_get_next_sequence()); | |
2000 | ||
2001 | zebra_rib_evaluate_mpls(rn); | |
2002 | } | |
941e261c | 2003 | |
54818e3b | 2004 | done: |
941e261c MS |
2005 | if (rn) |
2006 | route_unlock_node(rn); | |
2007 | ||
54818e3b MS |
2008 | /* Return context to dataplane module */ |
2009 | dplane_ctx_fini(&ctx); | |
2010 | } | |
2011 | ||
e22e8001 | 2012 | static void process_subq_nhg(struct listnode *lnode) |
e96f9203 | 2013 | { |
e22e8001 SW |
2014 | struct nhg_ctx *ctx = NULL; |
2015 | uint8_t qindex = route_info[ZEBRA_ROUTE_NHG].meta_q_map; | |
5110a0c6 | 2016 | |
e22e8001 SW |
2017 | ctx = listgetdata(lnode); |
2018 | ||
2019 | if (!ctx) | |
2020 | return; | |
2021 | ||
2022 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
2023 | zlog_debug("NHG Context id=%u dequeued from sub-queue %u", | |
2024 | ctx->id, qindex); | |
2025 | ||
2026 | rib_nhg_process(ctx); | |
2027 | } | |
2028 | ||
2029 | static void process_subq_route(struct listnode *lnode, uint8_t qindex) | |
2030 | { | |
2031 | struct route_node *rnode = NULL; | |
2032 | rib_dest_t *dest = NULL; | |
2033 | struct zebra_vrf *zvrf = NULL; | |
5110a0c6 | 2034 | |
d62a17ae | 2035 | rnode = listgetdata(lnode); |
2036 | dest = rib_dest_from_rnode(rnode); | |
2037 | if (dest) | |
2038 | zvrf = rib_dest_vrf(dest); | |
41ec9222 | 2039 | |
d62a17ae | 2040 | rib_process(rnode); |
5110a0c6 | 2041 | |
d62a17ae | 2042 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) { |
2043 | char buf[SRCDEST2STR_BUFFER]; | |
efe6c026 | 2044 | |
d62a17ae | 2045 | srcdest_rnode2str(rnode, buf, sizeof(buf)); |
2046 | zlog_debug("%u:%s: rn %p dequeued from sub-queue %u", | |
2047 | zvrf ? zvrf_id(zvrf) : 0, buf, rnode, qindex); | |
2048 | } | |
41ec9222 | 2049 | |
d62a17ae | 2050 | if (rnode->info) |
2051 | UNSET_FLAG(rib_dest_from_rnode(rnode)->flags, | |
2052 | RIB_ROUTE_QUEUED(qindex)); | |
9fd92e3c | 2053 | |
67b9467f | 2054 | #if 0 |
5110a0c6 SH |
2055 | else |
2056 | { | |
2057 | zlog_debug ("%s: called for route_node (%p, %d) with no ribs", | |
2058 | __func__, rnode, rnode->lock); | |
2059 | zlog_backtrace(LOG_DEBUG); | |
2060 | } | |
67b9467f | 2061 | #endif |
d62a17ae | 2062 | route_unlock_node(rnode); |
e22e8001 SW |
2063 | } |
2064 | ||
2065 | /* Take a list of route_node structs and return 1, if there was a record | |
2066 | * picked from it and processed by rib_process(). Don't process more, | |
2067 | * than one RN record; operate only in the specified sub-queue. | |
2068 | */ | |
2069 | static unsigned int process_subq(struct list *subq, uint8_t qindex) | |
2070 | { | |
2071 | struct listnode *lnode = listhead(subq); | |
2072 | ||
2073 | if (!lnode) | |
2074 | return 0; | |
2075 | ||
2076 | if (qindex == route_info[ZEBRA_ROUTE_NHG].meta_q_map) | |
2077 | process_subq_nhg(lnode); | |
2078 | else | |
2079 | process_subq_route(lnode, qindex); | |
2080 | ||
d62a17ae | 2081 | list_delete_node(subq, lnode); |
e22e8001 | 2082 | |
d62a17ae | 2083 | return 1; |
e96f9203 DO |
2084 | } |
2085 | ||
2086 | /* Dispatch the meta queue by picking, processing and unlocking the next RN from | |
d62a17ae | 2087 | * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and |
2088 | * data | |
e96f9203 DO |
2089 | * is pointed to the meta queue structure. |
2090 | */ | |
d62a17ae | 2091 | static wq_item_status meta_queue_process(struct work_queue *dummy, void *data) |
e96f9203 | 2092 | { |
d62a17ae | 2093 | struct meta_queue *mq = data; |
2094 | unsigned i; | |
91f16812 MS |
2095 | uint32_t queue_len, queue_limit; |
2096 | ||
2097 | /* Ensure there's room for more dataplane updates */ | |
2098 | queue_limit = dplane_get_in_queue_limit(); | |
2099 | queue_len = dplane_get_in_queue_len(); | |
2100 | if (queue_len > queue_limit) { | |
2101 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
8b962e77 MS |
2102 | zlog_debug("rib queue: dplane queue len %u, limit %u, retrying", |
2103 | queue_len, queue_limit); | |
91f16812 MS |
2104 | |
2105 | /* Ensure that the meta-queue is actually enqueued */ | |
489a9614 | 2106 | if (work_queue_empty(zrouter.ribq)) |
ea45a4e7 | 2107 | work_queue_add(zrouter.ribq, zrouter.mq); |
91f16812 MS |
2108 | |
2109 | return WQ_QUEUE_BLOCKED; | |
2110 | } | |
5110a0c6 | 2111 | |
d62a17ae | 2112 | for (i = 0; i < MQ_SIZE; i++) |
2113 | if (process_subq(mq->subq[i], i)) { | |
2114 | mq->size--; | |
2115 | break; | |
2116 | } | |
2117 | return mq->size ? WQ_REQUEUE : WQ_SUCCESS; | |
e96f9203 DO |
2118 | } |
2119 | ||
f52ed677 DS |
2120 | |
2121 | /* | |
2122 | * Look into the RN and queue it into the highest priority queue | |
2123 | * at this point in time for processing. | |
2124 | * | |
2125 | * We will enqueue a route node only once per invocation. | |
2126 | * | |
2127 | * There are two possibilities here that should be kept in mind. | |
2128 | * If the original invocation has not been pulled off for processing | |
2129 | * yet, A subsuquent invocation can have a route entry with a better | |
2130 | * meta queue index value and we can have a situation where | |
2131 | * we might have the same node enqueued 2 times. Not necessarily | |
2132 | * an optimal situation but it should be ok. | |
2133 | * | |
2134 | * The other possibility is that the original invocation has not | |
2135 | * been pulled off for processing yet, A subsusquent invocation | |
2136 | * doesn't have a route_entry with a better meta-queue and the | |
2137 | * original metaqueue index value will win and we'll end up with | |
2138 | * the route node enqueued once. | |
e96f9203 | 2139 | */ |
e22e8001 | 2140 | static int rib_meta_queue_add(struct meta_queue *mq, void *data) |
e96f9203 | 2141 | { |
e22e8001 | 2142 | struct route_node *rn = NULL; |
f52ed677 DS |
2143 | struct route_entry *re = NULL, *curr_re = NULL; |
2144 | uint8_t qindex = MQ_SIZE, curr_qindex = MQ_SIZE; | |
5110a0c6 | 2145 | |
e22e8001 SW |
2146 | rn = (struct route_node *)data; |
2147 | ||
f52ed677 DS |
2148 | RNODE_FOREACH_RE (rn, curr_re) { |
2149 | curr_qindex = route_info[curr_re->type].meta_q_map; | |
d62a17ae | 2150 | |
f52ed677 DS |
2151 | if (curr_qindex <= qindex) { |
2152 | re = curr_re; | |
2153 | qindex = curr_qindex; | |
d62a17ae | 2154 | } |
f52ed677 | 2155 | } |
5110a0c6 | 2156 | |
f52ed677 | 2157 | if (!re) |
e22e8001 | 2158 | return -1; |
5110a0c6 | 2159 | |
f52ed677 DS |
2160 | /* Invariant: at this point we always have rn->info set. */ |
2161 | if (CHECK_FLAG(rib_dest_from_rnode(rn)->flags, | |
2162 | RIB_ROUTE_QUEUED(qindex))) { | |
d62a17ae | 2163 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) |
2164 | rnode_debug(rn, re->vrf_id, | |
f52ed677 | 2165 | "rn %p is already queued in sub-queue %u", |
d62a17ae | 2166 | (void *)rn, qindex); |
e22e8001 | 2167 | return -1; |
d62a17ae | 2168 | } |
f52ed677 DS |
2169 | |
2170 | SET_FLAG(rib_dest_from_rnode(rn)->flags, RIB_ROUTE_QUEUED(qindex)); | |
2171 | listnode_add(mq->subq[qindex], rn); | |
2172 | route_lock_node(rn); | |
2173 | mq->size++; | |
2174 | ||
2175 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
2176 | rnode_debug(rn, re->vrf_id, "queued rn %p into sub-queue %u", | |
2177 | (void *)rn, qindex); | |
e22e8001 SW |
2178 | |
2179 | return 0; | |
4d38fdb4 | 2180 | } |
2181 | ||
e22e8001 | 2182 | static int rib_meta_queue_nhg_add(struct meta_queue *mq, void *data) |
4d38fdb4 | 2183 | { |
e22e8001 SW |
2184 | struct nhg_ctx *ctx = NULL; |
2185 | uint8_t qindex = route_info[ZEBRA_ROUTE_NHG].meta_q_map; | |
fc328ac9 | 2186 | |
e22e8001 SW |
2187 | ctx = (struct nhg_ctx *)data; |
2188 | ||
2189 | if (!ctx) | |
2190 | return -1; | |
4d38fdb4 | 2191 | |
e22e8001 SW |
2192 | listnode_add(mq->subq[qindex], ctx); |
2193 | mq->size++; | |
2194 | ||
2195 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
2196 | zlog_debug("NHG Context id=%u queued into sub-queue %u", | |
2197 | ctx->id, qindex); | |
2198 | ||
2199 | return 0; | |
2200 | } | |
2201 | ||
2202 | static int mq_add_handler(void *data, | |
2203 | int (*mq_add_func)(struct meta_queue *mq, void *data)) | |
2204 | { | |
489a9614 | 2205 | if (zrouter.ribq == NULL) { |
e914ccbe | 2206 | flog_err(EC_ZEBRA_WQ_NONEXISTENT, |
1c50c1c0 | 2207 | "%s: work_queue does not exist!", __func__); |
e22e8001 | 2208 | return -1; |
d62a17ae | 2209 | } |
2210 | ||
2211 | /* | |
2212 | * The RIB queue should normally be either empty or holding the only | |
2213 | * work_queue_item element. In the latter case this element would | |
2214 | * hold a pointer to the meta queue structure, which must be used to | |
2215 | * actually queue the route nodes to process. So create the MQ | |
2216 | * holder, if necessary, then push the work into it in any case. | |
2217 | * This semantics was introduced after 0.99.9 release. | |
2218 | */ | |
489a9614 | 2219 | if (work_queue_empty(zrouter.ribq)) |
ea45a4e7 | 2220 | work_queue_add(zrouter.ribq, zrouter.mq); |
e96f9203 | 2221 | |
e22e8001 SW |
2222 | return mq_add_func(zrouter.mq, data); |
2223 | } | |
fc328ac9 | 2224 | |
e22e8001 SW |
2225 | /* Add route_node to work queue and schedule processing */ |
2226 | int rib_queue_add(struct route_node *rn) | |
2227 | { | |
2228 | assert(rn); | |
2229 | ||
2230 | /* Pointless to queue a route_node with no RIB entries to add or remove | |
2231 | */ | |
2232 | if (!rnode_to_ribs(rn)) { | |
2233 | zlog_debug("%s: called for route_node (%p, %d) with no ribs", | |
2234 | __func__, (void *)rn, rn->lock); | |
2235 | zlog_backtrace(LOG_DEBUG); | |
2236 | return -1; | |
2237 | } | |
2238 | ||
2239 | return mq_add_handler(rn, &rib_meta_queue_add); | |
2240 | } | |
2241 | ||
2242 | int rib_queue_nhg_add(struct nhg_ctx *ctx) | |
2243 | { | |
2244 | assert(ctx); | |
2245 | ||
2246 | return mq_add_handler(ctx, &rib_meta_queue_nhg_add); | |
4d38fdb4 | 2247 | } |
2248 | ||
5110a0c6 SH |
2249 | /* Create new meta queue. |
2250 | A destructor function doesn't seem to be necessary here. | |
2251 | */ | |
d62a17ae | 2252 | static struct meta_queue *meta_queue_new(void) |
e96f9203 | 2253 | { |
d62a17ae | 2254 | struct meta_queue *new; |
2255 | unsigned i; | |
5110a0c6 | 2256 | |
d62a17ae | 2257 | new = XCALLOC(MTYPE_WORK_QUEUE, sizeof(struct meta_queue)); |
e96f9203 | 2258 | |
d62a17ae | 2259 | for (i = 0; i < MQ_SIZE; i++) { |
2260 | new->subq[i] = list_new(); | |
2261 | assert(new->subq[i]); | |
2262 | } | |
5110a0c6 | 2263 | |
d62a17ae | 2264 | return new; |
e96f9203 DO |
2265 | } |
2266 | ||
d62a17ae | 2267 | void meta_queue_free(struct meta_queue *mq) |
5a8dfcd8 | 2268 | { |
d62a17ae | 2269 | unsigned i; |
5a8dfcd8 | 2270 | |
d62a17ae | 2271 | for (i = 0; i < MQ_SIZE; i++) |
6a154c88 | 2272 | list_delete(&mq->subq[i]); |
5a8dfcd8 | 2273 | |
d62a17ae | 2274 | XFREE(MTYPE_WORK_QUEUE, mq); |
5a8dfcd8 RW |
2275 | } |
2276 | ||
4d38fdb4 | 2277 | /* initialise zebra rib work queue */ |
2561d12e | 2278 | static void rib_queue_init(void) |
4d38fdb4 | 2279 | { |
489a9614 DS |
2280 | if (!(zrouter.ribq = work_queue_new(zrouter.master, |
2281 | "route_node processing"))) { | |
e914ccbe | 2282 | flog_err(EC_ZEBRA_WQ_NONEXISTENT, |
1c50c1c0 | 2283 | "%s: could not initialise work queue!", __func__); |
d62a17ae | 2284 | return; |
2285 | } | |
4d38fdb4 | 2286 | |
d62a17ae | 2287 | /* fill in the work queue spec */ |
489a9614 DS |
2288 | zrouter.ribq->spec.workfunc = &meta_queue_process; |
2289 | zrouter.ribq->spec.errorfunc = NULL; | |
46a4e345 | 2290 | zrouter.ribq->spec.completion_func = NULL; |
d62a17ae | 2291 | /* XXX: TODO: These should be runtime configurable via vty */ |
489a9614 DS |
2292 | zrouter.ribq->spec.max_retries = 3; |
2293 | zrouter.ribq->spec.hold = ZEBRA_RIB_PROCESS_HOLD_TIME; | |
2294 | zrouter.ribq->spec.retry = ZEBRA_RIB_PROCESS_RETRY_TIME; | |
d62a17ae | 2295 | |
ea45a4e7 | 2296 | if (!(zrouter.mq = meta_queue_new())) { |
e914ccbe | 2297 | flog_err(EC_ZEBRA_WQ_NONEXISTENT, |
1c50c1c0 | 2298 | "%s: could not initialise meta queue!", __func__); |
d62a17ae | 2299 | return; |
2300 | } | |
2301 | return; | |
718e3744 | 2302 | } |
2303 | ||
434434f7 DS |
2304 | rib_dest_t *zebra_rib_create_dest(struct route_node *rn) |
2305 | { | |
2306 | rib_dest_t *dest; | |
2307 | ||
2308 | dest = XCALLOC(MTYPE_RIB_DEST, sizeof(rib_dest_t)); | |
aa57abfb | 2309 | rnh_list_init(&dest->nht); |
434434f7 DS |
2310 | route_lock_node(rn); /* rn route table reference */ |
2311 | rn->info = dest; | |
2312 | dest->rnode = rn; | |
2313 | ||
2314 | return dest; | |
2315 | } | |
2316 | ||
6d691129 PJ |
2317 | /* RIB updates are processed via a queue of pointers to route_nodes. |
2318 | * | |
2319 | * The queue length is bounded by the maximal size of the routing table, | |
2320 | * as a route_node will not be requeued, if already queued. | |
2321 | * | |
f0f77c9a DS |
2322 | * REs are submitted via rib_addnode or rib_delnode which set minimal |
2323 | * state, or static_install_route (when an existing RE is updated) | |
3c0755dc | 2324 | * and then submit route_node to queue for best-path selection later. |
f0f77c9a | 2325 | * Order of add/delete state changes are preserved for any given RE. |
6d691129 | 2326 | * |
f0f77c9a | 2327 | * Deleted REs are reaped during best-path selection. |
6d691129 PJ |
2328 | * |
2329 | * rib_addnode | |
f0f77c9a DS |
2330 | * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with |
2331 | * |-------->| | best RE, if required | |
3c0755dc PJ |
2332 | * | | |
2333 | * static_install->|->rib_addqueue...... -> rib_process | |
2334 | * | | | |
2335 | * |-------->| |-> rib_unlink | |
f0f77c9a DS |
2336 | * |-> set ROUTE_ENTRY_REMOVE | |
2337 | * rib_delnode (RE freed) | |
6d691129 | 2338 | * |
9fd92e3c AS |
2339 | * The 'info' pointer of a route_node points to a rib_dest_t |
2340 | * ('dest'). Queueing state for a route_node is kept on the dest. The | |
2341 | * dest is created on-demand by rib_link() and is kept around at least | |
2342 | * as long as there are ribs hanging off it (@see rib_gc_dest()). | |
d62a17ae | 2343 | * |
6d691129 PJ |
2344 | * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code): |
2345 | * | |
2346 | * - route_nodes: refcounted by: | |
9fd92e3c AS |
2347 | * - dest attached to route_node: |
2348 | * - managed by: rib_link/rib_gc_dest | |
6d691129 PJ |
2349 | * - route_node processing queue |
2350 | * - managed by: rib_addqueue, rib_process. | |
2351 | * | |
2352 | */ | |
d62a17ae | 2353 | |
f0f77c9a | 2354 | /* Add RE to head of the route node. */ |
d62a17ae | 2355 | static void rib_link(struct route_node *rn, struct route_entry *re, int process) |
2356 | { | |
d62a17ae | 2357 | rib_dest_t *dest; |
2358 | afi_t afi; | |
2359 | const char *rmap_name; | |
9fd92e3c | 2360 | |
d62a17ae | 2361 | assert(re && rn); |
9fd92e3c | 2362 | |
d62a17ae | 2363 | dest = rib_dest_from_rnode(rn); |
2364 | if (!dest) { | |
2365 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
2366 | rnode_debug(rn, re->vrf_id, "rn %p adding dest", rn); | |
7a4bb9c5 | 2367 | |
434434f7 | 2368 | dest = zebra_rib_create_dest(rn); |
d62a17ae | 2369 | } |
2263a412 | 2370 | |
045207e2 | 2371 | re_list_add_head(&dest->routes, re); |
d62a17ae | 2372 | |
2373 | afi = (rn->p.family == AF_INET) | |
2374 | ? AFI_IP | |
2375 | : (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX; | |
fe257ae7 DS |
2376 | if (is_zebra_import_table_enabled(afi, re->vrf_id, re->table)) { |
2377 | struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(re->vrf_id); | |
2378 | ||
d62a17ae | 2379 | rmap_name = zebra_get_import_table_route_map(afi, re->table); |
fe257ae7 | 2380 | zebra_add_import_table_entry(zvrf, rn, re, rmap_name); |
d62a17ae | 2381 | } else if (process) |
2382 | rib_queue_add(rn); | |
2383 | } | |
2384 | ||
7e24fdf3 DS |
2385 | static void rib_addnode(struct route_node *rn, |
2386 | struct route_entry *re, int process) | |
d62a17ae | 2387 | { |
2388 | /* RE node has been un-removed before route-node is processed. | |
2389 | * route_node must hence already be on the queue for processing.. | |
2390 | */ | |
2391 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) { | |
2392 | if (IS_ZEBRA_DEBUG_RIB) | |
2393 | rnode_debug(rn, re->vrf_id, "rn %p, un-removed re %p", | |
2394 | (void *)rn, (void *)re); | |
2395 | ||
2396 | UNSET_FLAG(re->status, ROUTE_ENTRY_REMOVED); | |
2397 | return; | |
2398 | } | |
2399 | rib_link(rn, re, process); | |
6d691129 PJ |
2400 | } |
2401 | ||
9fd92e3c AS |
2402 | /* |
2403 | * rib_unlink | |
2404 | * | |
2405 | * Detach a rib structure from a route_node. | |
2406 | * | |
2407 | * Note that a call to rib_unlink() should be followed by a call to | |
2408 | * rib_gc_dest() at some point. This allows a rib_dest_t that is no | |
2409 | * longer required to be deleted. | |
2410 | */ | |
d62a17ae | 2411 | void rib_unlink(struct route_node *rn, struct route_entry *re) |
6d691129 | 2412 | { |
d62a17ae | 2413 | rib_dest_t *dest; |
8e766379 | 2414 | struct nhg_hash_entry *nhe = NULL; |
9fd92e3c | 2415 | |
d62a17ae | 2416 | assert(rn && re); |
6d691129 | 2417 | |
d62a17ae | 2418 | if (IS_ZEBRA_DEBUG_RIB) |
2419 | rnode_debug(rn, re->vrf_id, "rn %p, re %p", (void *)rn, | |
2420 | (void *)re); | |
6d691129 | 2421 | |
d62a17ae | 2422 | dest = rib_dest_from_rnode(rn); |
6d691129 | 2423 | |
045207e2 | 2424 | re_list_del(&dest->routes, re); |
7a4bb9c5 | 2425 | |
2eb07de3 DS |
2426 | if (dest->selected_fib == re) |
2427 | dest->selected_fib = NULL; | |
2428 | ||
e22e8001 SW |
2429 | if (re->nhe_id) { |
2430 | nhe = zebra_nhg_lookup_id(re->nhe_id); | |
2431 | if (nhe) | |
2432 | zebra_nhg_decrement_ref(nhe); | |
2433 | } else if (re->ng) | |
2434 | nexthop_group_free_delete(&re->ng); | |
22bcedb2 | 2435 | |
ee5e8a48 MS |
2436 | nexthops_free(re->fib_ng.nexthop); |
2437 | ||
d62a17ae | 2438 | XFREE(MTYPE_RE, re); |
2439 | } | |
2440 | ||
2441 | void rib_delnode(struct route_node *rn, struct route_entry *re) | |
2442 | { | |
2443 | afi_t afi; | |
2444 | ||
2445 | if (IS_ZEBRA_DEBUG_RIB) | |
2446 | rnode_debug(rn, re->vrf_id, "rn %p, re %p, removing", | |
2447 | (void *)rn, (void *)re); | |
2448 | SET_FLAG(re->status, ROUTE_ENTRY_REMOVED); | |
2449 | ||
2450 | afi = (rn->p.family == AF_INET) | |
2451 | ? AFI_IP | |
2452 | : (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX; | |
fe257ae7 DS |
2453 | if (is_zebra_import_table_enabled(afi, re->vrf_id, re->table)) { |
2454 | struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(re->vrf_id); | |
2455 | ||
2456 | zebra_del_import_table_entry(zvrf, rn, re); | |
d62a17ae | 2457 | /* Just clean up if non main table */ |
2458 | if (IS_ZEBRA_DEBUG_RIB) { | |
2459 | char buf[SRCDEST2STR_BUFFER]; | |
2460 | srcdest_rnode2str(rn, buf, sizeof(buf)); | |
2da33d6b DS |
2461 | zlog_debug("%u:%s: Freeing route rn %p, re %p (%s)", |
2462 | re->vrf_id, buf, rn, re, | |
2463 | zebra_route_string(re->type)); | |
d62a17ae | 2464 | } |
7a4bb9c5 | 2465 | |
d62a17ae | 2466 | rib_unlink(rn, re); |
2467 | } else { | |
2468 | rib_queue_add(rn); | |
2469 | } | |
718e3744 | 2470 | } |
2471 | ||
f0f77c9a | 2472 | /* This function dumps the contents of a given RE entry into |
dc95824a DO |
2473 | * standard debug log. Calling function name and IP prefix in |
2474 | * question are passed as 1st and 2nd arguments. | |
2475 | */ | |
2476 | ||
d62a17ae | 2477 | void _route_entry_dump(const char *func, union prefixconstptr pp, |
2478 | union prefixconstptr src_pp, | |
2479 | const struct route_entry *re) | |
2480 | { | |
d62a17ae | 2481 | const struct prefix *src_p = src_pp.p; |
2482 | bool is_srcdst = src_p && src_p->prefixlen; | |
2483 | char straddr[PREFIX_STRLEN]; | |
2484 | char srcaddr[PREFIX_STRLEN]; | |
53c16fbe | 2485 | char nhname[PREFIX_STRLEN]; |
d62a17ae | 2486 | struct nexthop *nexthop; |
2487 | ||
2488 | zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func, | |
2489 | (const void *)re, prefix2str(pp, straddr, sizeof(straddr)), | |
2490 | is_srcdst ? " from " : "", | |
2491 | is_srcdst ? prefix2str(src_pp, srcaddr, sizeof(srcaddr)) | |
2492 | : "", | |
2493 | re->vrf_id); | |
cc54cfee | 2494 | zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d", |
53c16fbe | 2495 | straddr, (unsigned long)re->uptime, re->type, re->instance, |
cc54cfee | 2496 | re->table); |
d62a17ae | 2497 | zlog_debug( |
2498 | "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u", | |
53c16fbe DS |
2499 | straddr, re->metric, re->mtu, re->distance, re->flags, re->status); |
2500 | zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", straddr, | |
9a0d4dd3 DS |
2501 | nexthop_group_nexthop_num(re->ng), |
2502 | nexthop_group_active_nexthop_num(re->ng)); | |
d62a17ae | 2503 | |
6b468511 | 2504 | for (ALL_NEXTHOPS_PTR(re->ng, nexthop)) { |
2d68a0f2 DS |
2505 | struct interface *ifp; |
2506 | struct vrf *vrf = vrf_lookup_by_id(nexthop->vrf_id); | |
2507 | ||
2508 | switch (nexthop->type) { | |
2509 | case NEXTHOP_TYPE_BLACKHOLE: | |
53c16fbe | 2510 | sprintf(nhname, "Blackhole"); |
2d68a0f2 DS |
2511 | break; |
2512 | case NEXTHOP_TYPE_IFINDEX: | |
2513 | ifp = if_lookup_by_index(nexthop->ifindex, | |
2514 | nexthop->vrf_id); | |
53c16fbe | 2515 | sprintf(nhname, "%s", ifp ? ifp->name : "Unknown"); |
2d68a0f2 DS |
2516 | break; |
2517 | case NEXTHOP_TYPE_IPV4: | |
2518 | /* fallthrough */ | |
2519 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
53c16fbe | 2520 | inet_ntop(AF_INET, &nexthop->gate, nhname, |
2d68a0f2 DS |
2521 | INET6_ADDRSTRLEN); |
2522 | break; | |
2523 | case NEXTHOP_TYPE_IPV6: | |
2524 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
53c16fbe | 2525 | inet_ntop(AF_INET6, &nexthop->gate, nhname, |
2d68a0f2 DS |
2526 | INET6_ADDRSTRLEN); |
2527 | break; | |
2528 | } | |
df38b099 | 2529 | zlog_debug("%s: %s %s[%u] vrf %s(%u) with flags %s%s%s%s%s%s", |
53c16fbe | 2530 | straddr, (nexthop->rparent ? " NH" : "NH"), nhname, |
2d68a0f2 DS |
2531 | nexthop->ifindex, vrf ? vrf->name : "Unknown", |
2532 | nexthop->vrf_id, | |
d62a17ae | 2533 | (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE) |
2534 | ? "ACTIVE " | |
2535 | : ""), | |
677c1dd5 | 2536 | (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED) |
d62a17ae | 2537 | ? "FIB " |
2538 | : ""), | |
2539 | (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE) | |
df38b099 DS |
2540 | ? "RECURSIVE " |
2541 | : ""), | |
2542 | (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK) | |
2543 | ? "ONLINK " | |
2544 | : ""), | |
2545 | (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_MATCHED) | |
2546 | ? "MATCHED " | |
2547 | : ""), | |
2548 | (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_DUPLICATE) | |
2549 | ? "DUPLICATE " | |
d62a17ae | 2550 | : "")); |
2551 | } | |
53c16fbe | 2552 | zlog_debug("%s: dump complete", straddr); |
dc95824a DO |
2553 | } |
2554 | ||
2555 | /* This is an exported helper to rtm_read() to dump the strange | |
f0f77c9a | 2556 | * RE entry found by rib_lookup_ipv4_route() |
dc95824a DO |
2557 | */ |
2558 | ||
d62a17ae | 2559 | void rib_lookup_and_dump(struct prefix_ipv4 *p, vrf_id_t vrf_id) |
2560 | { | |
2561 | struct route_table *table; | |
2562 | struct route_node *rn; | |
2563 | struct route_entry *re; | |
2564 | char prefix_buf[INET_ADDRSTRLEN]; | |
2565 | ||
2566 | /* Lookup table. */ | |
2567 | table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id); | |
2568 | if (!table) { | |
e914ccbe | 2569 | flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED, |
1c50c1c0 QY |
2570 | "%s:%u zebra_vrf_table() returned NULL", __func__, |
2571 | vrf_id); | |
d62a17ae | 2572 | return; |
2573 | } | |
2574 | ||
2575 | /* Scan the RIB table for exactly matching RE entry. */ | |
2576 | rn = route_node_lookup(table, (struct prefix *)p); | |
2577 | ||
2578 | /* No route for this prefix. */ | |
2579 | if (!rn) { | |
32391aff | 2580 | zlog_debug("%s:%u lookup failed for %s", __func__, vrf_id, |
d62a17ae | 2581 | prefix2str((struct prefix *)p, prefix_buf, |
2582 | sizeof(prefix_buf))); | |
2583 | return; | |
2584 | } | |
2585 | ||
2586 | /* Unlock node. */ | |
2587 | route_unlock_node(rn); | |
2588 | ||
2589 | /* let's go */ | |
a2addae8 | 2590 | RNODE_FOREACH_RE (rn, re) { |
32391aff DS |
2591 | zlog_debug("%s:%u rn %p, re %p: %s, %s", |
2592 | __func__, vrf_id, | |
2593 | (void *)rn, (void *)re, | |
d62a17ae | 2594 | (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED) |
2595 | ? "removed" | |
2596 | : "NOT removed"), | |
2597 | (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED) | |
2598 | ? "selected" | |
2599 | : "NOT selected")); | |
2600 | route_entry_dump(p, NULL, re); | |
2601 | } | |
dc95824a DO |
2602 | } |
2603 | ||
20e5ff0a DO |
2604 | /* Check if requested address assignment will fail due to another |
2605 | * route being installed by zebra in FIB already. Take necessary | |
2606 | * actions, if needed: remove such a route from FIB and deSELECT | |
f0f77c9a | 2607 | * corresponding RE entry. Then put affected RN into RIBQ head. |
20e5ff0a | 2608 | */ |
d62a17ae | 2609 | void rib_lookup_and_pushup(struct prefix_ipv4 *p, vrf_id_t vrf_id) |
2610 | { | |
2611 | struct route_table *table; | |
2612 | struct route_node *rn; | |
5f7a4718 | 2613 | rib_dest_t *dest; |
d62a17ae | 2614 | |
2615 | if (NULL == (table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id))) { | |
e914ccbe | 2616 | flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED, |
1c50c1c0 QY |
2617 | "%s:%u zebra_vrf_table() returned NULL", __func__, |
2618 | vrf_id); | |
d62a17ae | 2619 | return; |
2620 | } | |
2621 | ||
2622 | /* No matches would be the simplest case. */ | |
2623 | if (NULL == (rn = route_node_lookup(table, (struct prefix *)p))) | |
2624 | return; | |
2625 | ||
2626 | /* Unlock node. */ | |
2627 | route_unlock_node(rn); | |
2628 | ||
5f7a4718 | 2629 | dest = rib_dest_from_rnode(rn); |
d62a17ae | 2630 | /* Check all RE entries. In case any changes have to be done, requeue |
2631 | * the RN into RIBQ head. If the routing message about the new connected | |
2632 | * route (generated by the IP address we are going to assign very soon) | |
2633 | * comes before the RIBQ is processed, the new RE entry will join | |
2634 | * RIBQ record already on head. This is necessary for proper | |
2635 | * revalidation | |
2636 | * of the rest of the RE. | |
2637 | */ | |
3cdba47a | 2638 | if (dest->selected_fib) { |
5f7a4718 DS |
2639 | if (IS_ZEBRA_DEBUG_RIB) { |
2640 | char buf[PREFIX_STRLEN]; | |
2641 | ||
2642 | zlog_debug("%u:%s: freeing way for connected prefix", | |
2643 | dest->selected_fib->vrf_id, | |
2644 | prefix2str(&rn->p, buf, sizeof(buf))); | |
2645 | route_entry_dump(&rn->p, NULL, dest->selected_fib); | |
d62a17ae | 2646 | } |
5f7a4718 | 2647 | rib_uninstall(rn, dest->selected_fib); |
d62a17ae | 2648 | rib_queue_add(rn); |
ade4a886 | 2649 | } |
20e5ff0a DO |
2650 | } |
2651 | ||
d62a17ae | 2652 | int rib_add_multipath(afi_t afi, safi_t safi, struct prefix *p, |
2653 | struct prefix_ipv6 *src_p, struct route_entry *re) | |
718e3744 | 2654 | { |
98cda54a | 2655 | struct nhg_hash_entry *nhe = NULL; |
d62a17ae | 2656 | struct route_table *table; |
2657 | struct route_node *rn; | |
40ecd8e4 | 2658 | struct route_entry *same = NULL; |
d62a17ae | 2659 | int ret = 0; |
b4c034b0 | 2660 | |
d62a17ae | 2661 | if (!re) |
2662 | return 0; | |
b4c034b0 | 2663 | |
1f610a1f | 2664 | assert(!src_p || !src_p->prefixlen || afi == AFI_IP6); |
05737783 | 2665 | |
d62a17ae | 2666 | /* Lookup table. */ |
7865c65d RW |
2667 | table = zebra_vrf_table_with_table_id(afi, safi, re->vrf_id, re->table); |
2668 | if (!table) { | |
13dfea50 SW |
2669 | if (re->ng) |
2670 | nexthop_group_free_delete(&re->ng); | |
7865c65d | 2671 | XFREE(MTYPE_RE, re); |
d62a17ae | 2672 | return 0; |
7865c65d | 2673 | } |
cddf391b | 2674 | |
98cda54a SW |
2675 | if (re->nhe_id) { |
2676 | nhe = zebra_nhg_lookup_id(re->nhe_id); | |
2677 | ||
2678 | if (!nhe) { | |
2679 | flog_err( | |
2680 | EC_ZEBRA_TABLE_LOOKUP_FAILED, | |
2681 | "Zebra failed to find the nexthop hash entry for id=%u in a route entry", | |
2682 | re->nhe_id); | |
2683 | XFREE(MTYPE_RE, re); | |
2684 | return -1; | |
2685 | } | |
2686 | } else { | |
7f997721 | 2687 | nhe = zebra_nhg_rib_find(0, re->ng, afi); |
98cda54a SW |
2688 | |
2689 | /* | |
2690 | * The nexthops got copied over into an nhe, | |
2691 | * so free them now. | |
2692 | */ | |
2693 | nexthop_group_free_delete(&re->ng); | |
2694 | ||
2695 | if (!nhe) { | |
2696 | char buf[PREFIX_STRLEN] = ""; | |
2697 | char buf2[PREFIX_STRLEN] = ""; | |
2698 | ||
2699 | flog_err( | |
2700 | EC_ZEBRA_TABLE_LOOKUP_FAILED, | |
2701 | "Zebra failed to find or create a nexthop hash entry for %s%s%s", | |
2702 | prefix2str(p, buf, sizeof(buf)), | |
2703 | src_p ? " from " : "", | |
2704 | src_p ? prefix2str(src_p, buf2, sizeof(buf2)) | |
2705 | : ""); | |
2706 | ||
2707 | XFREE(MTYPE_RE, re); | |
2708 | return -1; | |
2709 | } | |
98cda54a SW |
2710 | } |
2711 | ||
4b87c90d SW |
2712 | /* |
2713 | * Attach the re to the nhe's nexthop group. | |
2714 | * | |
2715 | * TODO: This will need to change when we start getting IDs from upper | |
2716 | * level protocols, as the refcnt might be wrong, since it checks | |
2717 | * if old_id != new_id. | |
2718 | */ | |
144a1b34 | 2719 | zebra_nhg_re_update_ref(re, nhe); |
98cda54a | 2720 | |
d62a17ae | 2721 | /* Make it sure prefixlen is applied to the prefix. */ |
2722 | apply_mask(p); | |
2723 | if (src_p) | |
2724 | apply_mask_ipv6(src_p); | |
718e3744 | 2725 | |
d62a17ae | 2726 | /* Set default distance by route type. */ |
eea2ef07 | 2727 | if (re->distance == 0) |
0492eea0 | 2728 | re->distance = route_distance(re->type); |
718e3744 | 2729 | |
d62a17ae | 2730 | /* Lookup route node.*/ |
2731 | rn = srcdest_rnode_get(table, p, src_p); | |
718e3744 | 2732 | |
40ecd8e4 DS |
2733 | /* |
2734 | * If same type of route are installed, treat it as a implicit | |
2735 | * withdraw. | |
2736 | * If the user has specified the No route replace semantics | |
2737 | * for the install don't do a route replace. | |
2738 | */ | |
a2addae8 | 2739 | RNODE_FOREACH_RE (rn, same) { |
d62a17ae | 2740 | if (CHECK_FLAG(same->status, ROUTE_ENTRY_REMOVED)) |
2741 | continue; | |
41ec9222 | 2742 | |
eb327fa5 RW |
2743 | if (same->type != re->type) |
2744 | continue; | |
2745 | if (same->instance != re->instance) | |
2746 | continue; | |
996c9314 LB |
2747 | if (same->type == ZEBRA_ROUTE_KERNEL |
2748 | && same->metric != re->metric) | |
eb327fa5 | 2749 | continue; |
40ecd8e4 DS |
2750 | |
2751 | if (CHECK_FLAG(re->flags, ZEBRA_FLAG_RR_USE_DISTANCE) && | |
2752 | same->distance != re->distance) | |
2753 | continue; | |
2754 | ||
844b3a87 | 2755 | /* |
40ecd8e4 DS |
2756 | * We should allow duplicate connected routes |
2757 | * because of IPv6 link-local routes and unnumbered | |
2758 | * interfaces on Linux. | |
844b3a87 RW |
2759 | */ |
2760 | if (same->type != ZEBRA_ROUTE_CONNECT) | |
d62a17ae | 2761 | break; |
2762 | } | |
718e3744 | 2763 | |
cf363e1b MS |
2764 | /* If this route is kernel/connected route, notify the dataplane. */ |
2765 | if (RIB_SYSTEM_ROUTE(re)) { | |
2766 | /* Notify dataplane */ | |
2767 | dplane_sys_route_add(rn, re); | |
2768 | } | |
2769 | ||
d62a17ae | 2770 | /* Link new re to node.*/ |
2771 | if (IS_ZEBRA_DEBUG_RIB) { | |
2da33d6b DS |
2772 | rnode_debug(rn, re->vrf_id, |
2773 | "Inserting route rn %p, re %p (%s) existing %p", | |
2774 | rn, re, zebra_route_string(re->type), same); | |
718e3744 | 2775 | |
d62a17ae | 2776 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) |
2777 | route_entry_dump(p, src_p, re); | |
718e3744 | 2778 | } |
6883bf8d DS |
2779 | |
2780 | SET_FLAG(re->status, ROUTE_ENTRY_CHANGED); | |
d62a17ae | 2781 | rib_addnode(rn, re, 1); |
2782 | ret = 1; | |
6b0655a2 | 2783 | |
d62a17ae | 2784 | /* Free implicit route.*/ |
2785 | if (same) { | |
2786 | rib_delnode(rn, same); | |
2787 | ret = -1; | |
2788 | } | |
718e3744 | 2789 | |
d62a17ae | 2790 | route_unlock_node(rn); |
2791 | return ret; | |
2792 | } | |
2793 | ||
2794 | void rib_delete(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type, | |
d7c0a89a | 2795 | unsigned short instance, int flags, struct prefix *p, |
fd36be7e | 2796 | struct prefix_ipv6 *src_p, const struct nexthop *nh, |
bc541126 SW |
2797 | uint32_t nhe_id, uint32_t table_id, uint32_t metric, |
2798 | uint8_t distance, bool fromkernel) | |
d62a17ae | 2799 | { |
2800 | struct route_table *table; | |
2801 | struct route_node *rn; | |
2802 | struct route_entry *re; | |
2803 | struct route_entry *fib = NULL; | |
2804 | struct route_entry *same = NULL; | |
fd36be7e | 2805 | struct nexthop *rtnh; |
d62a17ae | 2806 | char buf2[INET6_ADDRSTRLEN]; |
5f7a4718 | 2807 | rib_dest_t *dest; |
d62a17ae | 2808 | |
1f610a1f | 2809 | assert(!src_p || !src_p->prefixlen || afi == AFI_IP6); |
d62a17ae | 2810 | |
2811 | /* Lookup table. */ | |
2812 | table = zebra_vrf_table_with_table_id(afi, safi, vrf_id, table_id); | |
2813 | if (!table) | |
2814 | return; | |
2815 | ||
2816 | /* Apply mask. */ | |
2817 | apply_mask(p); | |
2818 | if (src_p) | |
2819 | apply_mask_ipv6(src_p); | |
2820 | ||
2821 | /* Lookup route node. */ | |
2822 | rn = srcdest_rnode_lookup(table, p, src_p); | |
2823 | if (!rn) { | |
2824 | char dst_buf[PREFIX_STRLEN], src_buf[PREFIX_STRLEN]; | |
2825 | ||
2826 | prefix2str(p, dst_buf, sizeof(dst_buf)); | |
2827 | if (src_p && src_p->prefixlen) | |
2828 | prefix2str(src_p, src_buf, sizeof(src_buf)); | |
2829 | else | |
2830 | src_buf[0] = '\0'; | |
2831 | ||
82d7d0e2 DS |
2832 | if (IS_ZEBRA_DEBUG_RIB) { |
2833 | struct vrf *vrf = vrf_lookup_by_id(vrf_id); | |
2834 | ||
2835 | zlog_debug("%s[%d]:%s%s%s doesn't exist in rib", | |
2836 | vrf->name, table_id, dst_buf, | |
d62a17ae | 2837 | (src_buf[0] != '\0') ? " from " : "", |
2838 | src_buf); | |
82d7d0e2 | 2839 | } |
d62a17ae | 2840 | return; |
2841 | } | |
718e3744 | 2842 | |
5f7a4718 DS |
2843 | dest = rib_dest_from_rnode(rn); |
2844 | fib = dest->selected_fib; | |
2845 | ||
d62a17ae | 2846 | /* Lookup same type route. */ |
a2addae8 | 2847 | RNODE_FOREACH_RE (rn, re) { |
d62a17ae | 2848 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) |
2849 | continue; | |
2850 | ||
d62a17ae | 2851 | if (re->type != type) |
2852 | continue; | |
2853 | if (re->instance != instance) | |
2854 | continue; | |
40ecd8e4 DS |
2855 | if (CHECK_FLAG(re->flags, ZEBRA_FLAG_RR_USE_DISTANCE) && |
2856 | distance != re->distance) | |
2857 | continue; | |
2858 | ||
996c9314 | 2859 | if (re->type == ZEBRA_ROUTE_KERNEL && re->metric != metric) |
f19435a8 | 2860 | continue; |
6b468511 | 2861 | if (re->type == ZEBRA_ROUTE_CONNECT && (rtnh = re->ng->nexthop) |
fd36be7e DL |
2862 | && rtnh->type == NEXTHOP_TYPE_IFINDEX && nh) { |
2863 | if (rtnh->ifindex != nh->ifindex) | |
d62a17ae | 2864 | continue; |
d62a17ae | 2865 | same = re; |
2866 | break; | |
2867 | } | |
2868 | /* Make sure that the route found has the same gateway. */ | |
bc541126 SW |
2869 | else if (nhe_id && re->nhe_id == nhe_id) { |
2870 | same = re; | |
2871 | break; | |
2872 | } else { | |
fd36be7e | 2873 | if (nh == NULL) { |
d62a17ae | 2874 | same = re; |
2875 | break; | |
2876 | } | |
6b468511 | 2877 | for (ALL_NEXTHOPS_PTR(re->ng, rtnh)) |
a5a2d802 SW |
2878 | /* |
2879 | * No guarantee all kernel send nh with labels | |
2880 | * on delete. | |
2881 | */ | |
2882 | if (nexthop_same_no_labels(rtnh, nh)) { | |
d62a17ae | 2883 | same = re; |
2884 | break; | |
2885 | } | |
2886 | if (same) | |
2887 | break; | |
2888 | } | |
2889 | } | |
2890 | /* If same type of route can't be found and this message is from | |
2891 | kernel. */ | |
2892 | if (!same) { | |
5dfeba19 DS |
2893 | /* |
2894 | * In the past(HA!) we could get here because | |
2895 | * we were receiving a route delete from the | |
2896 | * kernel and we're not marking the proto | |
2897 | * as coming from it's appropriate originator. | |
2898 | * Now that we are properly noticing the fact | |
2899 | * that the kernel has deleted our route we | |
2900 | * are not going to get called in this path | |
2901 | * I am going to leave this here because | |
2902 | * this might still work this way on non-linux | |
2903 | * platforms as well as some weird state I have | |
2904 | * not properly thought of yet. | |
2905 | * If we can show that this code path is | |
2906 | * dead then we can remove it. | |
2907 | */ | |
b8faa875 | 2908 | if (fib && CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE)) { |
d62a17ae | 2909 | if (IS_ZEBRA_DEBUG_RIB) { |
2da33d6b DS |
2910 | rnode_debug(rn, vrf_id, |
2911 | "rn %p, re %p (%s) was deleted from kernel, adding", | |
2912 | rn, fib, | |
2913 | zebra_route_string(fib->type)); | |
d62a17ae | 2914 | } |
2915 | if (allow_delete) { | |
677c1dd5 | 2916 | UNSET_FLAG(fib->status, ROUTE_ENTRY_INSTALLED); |
d62a17ae | 2917 | /* Unset flags. */ |
6b468511 | 2918 | for (rtnh = fib->ng->nexthop; rtnh; |
fd36be7e DL |
2919 | rtnh = rtnh->next) |
2920 | UNSET_FLAG(rtnh->flags, | |
d62a17ae | 2921 | NEXTHOP_FLAG_FIB); |
2922 | ||
ed216282 DS |
2923 | /* |
2924 | * This is a non FRR route | |
2925 | * as such we should mark | |
2926 | * it as deleted | |
2927 | */ | |
5f7a4718 | 2928 | dest->selected_fib = NULL; |
d62a17ae | 2929 | } else { |
2930 | /* This means someone else, other than Zebra, | |
2931 | * has deleted | |
2932 | * a Zebra router from the kernel. We will add | |
2933 | * it back */ | |
2934 | rib_install_kernel(rn, fib, NULL); | |
2935 | } | |
2936 | } else { | |
2937 | if (IS_ZEBRA_DEBUG_RIB) { | |
fd36be7e | 2938 | if (nh) |
d62a17ae | 2939 | rnode_debug( |
2940 | rn, vrf_id, | |
2941 | "via %s ifindex %d type %d " | |
2942 | "doesn't exist in rib", | |
36228974 | 2943 | inet_ntop(afi2family(afi), |
2944 | &nh->gate, buf2, | |
2945 | sizeof(buf2)), | |
2946 | nh->ifindex, type); | |
d62a17ae | 2947 | else |
2948 | rnode_debug( | |
2949 | rn, vrf_id, | |
fd36be7e DL |
2950 | "type %d doesn't exist in rib", |
2951 | type); | |
d62a17ae | 2952 | } |
2953 | route_unlock_node(rn); | |
2954 | return; | |
2955 | } | |
2956 | } | |
718e3744 | 2957 | |
5dfeba19 | 2958 | if (same) { |
996c9314 LB |
2959 | if (fromkernel && CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE) |
2960 | && !allow_delete) { | |
5dfeba19 DS |
2961 | rib_install_kernel(rn, same, NULL); |
2962 | route_unlock_node(rn); | |
2963 | ||
2964 | return; | |
2965 | } | |
6134fd82 | 2966 | |
2b83602b | 2967 | /* Special handling for IPv4 or IPv6 routes sourced from |
2968 | * EVPN - the nexthop (and associated MAC) need to be | |
2969 | * uninstalled if no more refs. | |
2970 | */ | |
90264d64 | 2971 | if (CHECK_FLAG(flags, ZEBRA_FLAG_EVPN_ROUTE)) { |
6134fd82 | 2972 | struct nexthop *tmp_nh; |
2973 | ||
6b468511 | 2974 | for (ALL_NEXTHOPS_PTR(re->ng, tmp_nh)) { |
6134fd82 | 2975 | struct ipaddr vtep_ip; |
2976 | ||
2977 | memset(&vtep_ip, 0, sizeof(struct ipaddr)); | |
1ec31309 | 2978 | if (afi == AFI_IP) { |
2979 | vtep_ip.ipa_type = IPADDR_V4; | |
2980 | memcpy(&(vtep_ip.ipaddr_v4), | |
2981 | &(tmp_nh->gate.ipv4), | |
2982 | sizeof(struct in_addr)); | |
2983 | } else { | |
2984 | vtep_ip.ipa_type = IPADDR_V6; | |
2985 | memcpy(&(vtep_ip.ipaddr_v6), | |
2986 | &(tmp_nh->gate.ipv6), | |
2987 | sizeof(struct in6_addr)); | |
2988 | } | |
a317a9b9 | 2989 | zebra_vxlan_evpn_vrf_route_del(re->vrf_id, |
6134fd82 | 2990 | &vtep_ip, p); |
2991 | } | |
2992 | } | |
cf363e1b MS |
2993 | |
2994 | /* Notify dplane if system route changes */ | |
2995 | if (RIB_SYSTEM_ROUTE(re)) | |
2996 | dplane_sys_route_del(rn, same); | |
2997 | ||
d62a17ae | 2998 | rib_delnode(rn, same); |
5dfeba19 | 2999 | } |
05737783 | 3000 | |
d62a17ae | 3001 | route_unlock_node(rn); |
3002 | return; | |
3003 | } | |
718e3744 | 3004 | |
718e3744 | 3005 | |
d7c0a89a QY |
3006 | int rib_add(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type, |
3007 | unsigned short instance, int flags, struct prefix *p, | |
3008 | struct prefix_ipv6 *src_p, const struct nexthop *nh, | |
8032b717 SW |
3009 | uint32_t nhe_id, uint32_t table_id, uint32_t metric, uint32_t mtu, |
3010 | uint8_t distance, route_tag_t tag) | |
d62a17ae | 3011 | { |
bbb322f2 SW |
3012 | struct route_entry *re = NULL; |
3013 | struct nexthop *nexthop = NULL; | |
718e3744 | 3014 | |
66af6845 | 3015 | /* Allocate new route_entry structure. */ |
d62a17ae | 3016 | re = XCALLOC(MTYPE_RE, sizeof(struct route_entry)); |
d62a17ae | 3017 | re->type = type; |
3018 | re->instance = instance; | |
3019 | re->distance = distance; | |
3020 | re->flags = flags; | |
3021 | re->metric = metric; | |
3022 | re->mtu = mtu; | |
3023 | re->table = table_id; | |
3024 | re->vrf_id = vrf_id; | |
98572489 | 3025 | re->uptime = monotime(NULL); |
4e40b6d6 | 3026 | re->tag = tag; |
bbb322f2 SW |
3027 | re->nhe_id = nhe_id; |
3028 | ||
e22e8001 SW |
3029 | if (!nhe_id) { |
3030 | re->ng = nexthop_group_new(); | |
d62a17ae | 3031 | |
e22e8001 SW |
3032 | /* Add nexthop. */ |
3033 | nexthop = nexthop_new(); | |
3034 | *nexthop = *nh; | |
3035 | route_entry_nexthop_add(re, nexthop); | |
3036 | } | |
718e3744 | 3037 | |
66af6845 | 3038 | return rib_add_multipath(afi, safi, p, src_p, re); |
718e3744 | 3039 | } |
3040 | ||
2a99ab95 SW |
3041 | static const char *rib_update_event2str(rib_update_event_t event) |
3042 | { | |
3043 | const char *ret = "UNKNOWN"; | |
3044 | ||
3045 | switch (event) { | |
3046 | case RIB_UPDATE_KERNEL: | |
3047 | ret = "RIB_UPDATE_KERNEL"; | |
3048 | break; | |
3049 | case RIB_UPDATE_RMAP_CHANGE: | |
3050 | ret = "RIB_UPDATE_RMAP_CHANGE"; | |
3051 | break; | |
3052 | case RIB_UPDATE_OTHER: | |
3053 | ret = "RIB_UPDATE_OTHER"; | |
3054 | break; | |
3055 | case RIB_UPDATE_MAX: | |
3056 | break; | |
3057 | } | |
3058 | ||
3059 | return ret; | |
3060 | } | |
3061 | ||
3062 | ||
3063 | /* Schedule route nodes to be processed if they match the type */ | |
3064 | static void rib_update_route_node(struct route_node *rn, int type) | |
3065 | { | |
3066 | struct route_entry *re, *next; | |
3067 | bool re_changed = false; | |
3068 | ||
3069 | RNODE_FOREACH_RE_SAFE (rn, re, next) { | |
3070 | if (type == ZEBRA_ROUTE_ALL || type == re->type) { | |
3071 | SET_FLAG(re->status, ROUTE_ENTRY_CHANGED); | |
3072 | re_changed = true; | |
3073 | } | |
3074 | } | |
3075 | ||
3076 | if (re_changed) | |
3077 | rib_queue_add(rn); | |
3078 | } | |
3079 | ||
1c848137 | 3080 | /* Schedule routes of a particular table (address-family) based on event. */ |
d5b8c216 | 3081 | void rib_update_table(struct route_table *table, rib_update_event_t event) |
d62a17ae | 3082 | { |
3083 | struct route_node *rn; | |
2a99ab95 SW |
3084 | |
3085 | if (IS_ZEBRA_DEBUG_EVENT) { | |
3086 | struct zebra_vrf *zvrf; | |
3087 | struct vrf *vrf; | |
3088 | ||
3089 | zvrf = table->info ? ((rib_table_info_t *)table->info)->zvrf | |
3090 | : NULL; | |
3091 | vrf = zvrf ? zvrf->vrf : NULL; | |
3092 | ||
3093 | zlog_debug("%s: %s VRF %s Table %u event %s", __func__, | |
3094 | table->info ? afi2str( | |
3095 | ((rib_table_info_t *)table->info)->afi) | |
3096 | : "Unknown", | |
3097 | vrf ? vrf->name : "Unknown", | |
3098 | zvrf ? zvrf->table_id : 0, | |
3099 | rib_update_event2str(event)); | |
3100 | } | |
d62a17ae | 3101 | |
3102 | /* Walk all routes and queue for processing, if appropriate for | |
3103 | * the trigger event. | |
3104 | */ | |
3105 | for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) { | |
1ca60f2c DS |
3106 | /* |
3107 | * If we are looking at a route node and the node | |
3108 | * has already been queued we don't | |
3109 | * need to queue it up again | |
3110 | */ | |
2a99ab95 SW |
3111 | if (rn->info |
3112 | && CHECK_FLAG(rib_dest_from_rnode(rn)->flags, | |
3113 | RIB_ROUTE_ANY_QUEUED)) | |
1ca60f2c | 3114 | continue; |
2a99ab95 | 3115 | |
d62a17ae | 3116 | switch (event) { |
2a99ab95 SW |
3117 | case RIB_UPDATE_KERNEL: |
3118 | rib_update_route_node(rn, ZEBRA_ROUTE_KERNEL); | |
3119 | break; | |
d62a17ae | 3120 | case RIB_UPDATE_RMAP_CHANGE: |
3121 | case RIB_UPDATE_OTHER: | |
2a99ab95 | 3122 | rib_update_route_node(rn, ZEBRA_ROUTE_ALL); |
d62a17ae | 3123 | break; |
d62a17ae | 3124 | default: |
3125 | break; | |
3126 | } | |
3127 | } | |
b84c7253 | 3128 | } |
3129 | ||
2a99ab95 | 3130 | static void rib_update_handle_vrf(vrf_id_t vrf_id, rib_update_event_t event) |
718e3744 | 3131 | { |
d62a17ae | 3132 | struct route_table *table; |
1c848137 | 3133 | |
2a99ab95 SW |
3134 | if (IS_ZEBRA_DEBUG_EVENT) |
3135 | zlog_debug("%s: Handling VRF %s event %s", __func__, | |
3136 | vrf_id_to_name(vrf_id), rib_update_event2str(event)); | |
3137 | ||
d62a17ae | 3138 | /* Process routes of interested address-families. */ |
3139 | table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id); | |
2a99ab95 | 3140 | if (table) |
d62a17ae | 3141 | rib_update_table(table, event); |
718e3744 | 3142 | |
d62a17ae | 3143 | table = zebra_vrf_table(AFI_IP6, SAFI_UNICAST, vrf_id); |
2a99ab95 | 3144 | if (table) |
d62a17ae | 3145 | rib_update_table(table, event); |
2a99ab95 SW |
3146 | } |
3147 | ||
3148 | static void rib_update_handle_vrf_all(rib_update_event_t event) | |
3149 | { | |
3150 | struct zebra_router_table *zrt; | |
3151 | ||
3152 | if (IS_ZEBRA_DEBUG_EVENT) | |
3153 | zlog_debug("%s: Handling VRF (ALL) event %s", __func__, | |
3154 | rib_update_event2str(event)); | |
3155 | ||
3156 | /* Just iterate over all the route tables, rather than vrf lookups */ | |
3157 | RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) | |
3158 | rib_update_table(zrt->table, event); | |
3159 | } | |
3160 | ||
3161 | struct rib_update_ctx { | |
3162 | rib_update_event_t event; | |
3163 | bool vrf_all; | |
3164 | vrf_id_t vrf_id; | |
3165 | }; | |
3166 | ||
3167 | static struct rib_update_ctx *rib_update_ctx_init(vrf_id_t vrf_id, | |
3168 | rib_update_event_t event) | |
3169 | { | |
3170 | struct rib_update_ctx *ctx; | |
3171 | ||
3172 | ctx = XCALLOC(MTYPE_RIB_UPDATE_CTX, sizeof(struct rib_update_ctx)); | |
3173 | ||
3174 | ctx->event = event; | |
3175 | ctx->vrf_id = vrf_id; | |
3176 | ||
3177 | return ctx; | |
3178 | } | |
3179 | ||
3180 | static void rib_update_ctx_fini(struct rib_update_ctx **ctx) | |
3181 | { | |
3182 | XFREE(MTYPE_RIB_UPDATE_CTX, *ctx); | |
3183 | ||
3184 | *ctx = NULL; | |
3185 | } | |
3186 | ||
3187 | static int rib_update_handler(struct thread *thread) | |
3188 | { | |
3189 | struct rib_update_ctx *ctx; | |
3190 | ||
3191 | ctx = THREAD_ARG(thread); | |
3192 | ||
3193 | if (ctx->vrf_all) | |
3194 | rib_update_handle_vrf_all(ctx->event); | |
3195 | else | |
3196 | rib_update_handle_vrf(ctx->vrf_id, ctx->event); | |
3197 | ||
3198 | rib_update_ctx_fini(&ctx); | |
3199 | ||
3200 | return 0; | |
3201 | } | |
3202 | ||
3203 | /* | |
3204 | * Thread list to ensure we don't schedule a ton of events | |
3205 | * if interfaces are flapping for instance. | |
3206 | */ | |
3207 | static struct thread *t_rib_update_threads[RIB_UPDATE_MAX]; | |
3208 | ||
3209 | /* Schedule a RIB update event for specific vrf */ | |
3210 | void rib_update_vrf(vrf_id_t vrf_id, rib_update_event_t event) | |
3211 | { | |
3212 | struct rib_update_ctx *ctx; | |
3213 | ||
3214 | ctx = rib_update_ctx_init(vrf_id, event); | |
3215 | ||
3216 | /* Don't worry about making sure multiple rib updates for specific vrf | |
3217 | * are scheduled at once for now. If it becomes a problem, we can use a | |
3218 | * lookup of some sort to keep track of running threads via t_vrf_id | |
3219 | * like how we are doing it in t_rib_update_threads[]. | |
3220 | */ | |
3221 | thread_add_event(zrouter.master, rib_update_handler, ctx, 0, NULL); | |
3222 | ||
3223 | if (IS_ZEBRA_DEBUG_EVENT) | |
3224 | zlog_debug("%s: Scheduled VRF %s, event %s", __func__, | |
3225 | vrf_id_to_name(ctx->vrf_id), | |
3226 | rib_update_event2str(event)); | |
3227 | } | |
3228 | ||
3229 | /* Schedule a RIB update event for all vrfs */ | |
3230 | void rib_update(rib_update_event_t event) | |
3231 | { | |
3232 | struct rib_update_ctx *ctx; | |
3233 | ||
3234 | ctx = rib_update_ctx_init(0, event); | |
3235 | ||
3236 | ctx->vrf_all = true; | |
3237 | ||
3238 | if (!thread_add_event(zrouter.master, rib_update_handler, ctx, 0, | |
3239 | &t_rib_update_threads[event])) | |
3240 | rib_update_ctx_fini(&ctx); /* Already scheduled */ | |
3241 | else if (IS_ZEBRA_DEBUG_EVENT) | |
3242 | zlog_debug("%s: Schedued VRF (ALL), event %s", __func__, | |
3243 | rib_update_event2str(event)); | |
718e3744 | 3244 | } |
3245 | ||
718e3744 | 3246 | /* Delete self installed routes after zebra is relaunched. */ |
95a29032 | 3247 | void rib_sweep_table(struct route_table *table) |
d62a17ae | 3248 | { |
3249 | struct route_node *rn; | |
3250 | struct route_entry *re; | |
3251 | struct route_entry *next; | |
915902cb | 3252 | struct nexthop *nexthop; |
d62a17ae | 3253 | |
915902cb DS |
3254 | if (!table) |
3255 | return; | |
d62a17ae | 3256 | |
915902cb | 3257 | for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) { |
a2addae8 | 3258 | RNODE_FOREACH_RE_SAFE (rn, re, next) { |
d4644d41 | 3259 | |
915902cb DS |
3260 | if (IS_ZEBRA_DEBUG_RIB) |
3261 | route_entry_dump(&rn->p, NULL, re); | |
3262 | ||
3263 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) | |
3264 | continue; | |
3265 | ||
3266 | if (!CHECK_FLAG(re->flags, ZEBRA_FLAG_SELFROUTE)) | |
3267 | continue; | |
3268 | ||
d4644d41 DS |
3269 | /* |
3270 | * If routes are older than startup_time then | |
3271 | * we know we read them in from the kernel. | |
3272 | * As such we can safely remove them. | |
3273 | */ | |
3274 | if (zrouter.startup_time < re->uptime) | |
3275 | continue; | |
3276 | ||
915902cb DS |
3277 | /* |
3278 | * So we are starting up and have received | |
3279 | * routes from the kernel that we have installed | |
3280 | * from a previous run of zebra but not cleaned | |
3281 | * up ( say a kill -9 ) | |
3282 | * But since we haven't actually installed | |
3283 | * them yet( we received them from the kernel ) | |
3284 | * we don't think they are active. | |
3285 | * So let's pretend they are active to actually | |
3286 | * remove them. | |
3287 | * In all honesty I'm not sure if we should | |
3288 | * mark them as active when we receive them | |
3289 | * This is startup only so probably ok. | |
3290 | * | |
3291 | * If we ever decide to move rib_sweep_table | |
3292 | * to a different spot (ie startup ) | |
3293 | * this decision needs to be revisited | |
3294 | */ | |
677c1dd5 | 3295 | SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED); |
6b468511 | 3296 | for (ALL_NEXTHOPS_PTR(re->ng, nexthop)) |
915902cb DS |
3297 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB); |
3298 | ||
0c555cc6 DS |
3299 | rib_uninstall_kernel(rn, re); |
3300 | rib_delnode(rn, re); | |
915902cb DS |
3301 | } |
3302 | } | |
718e3744 | 3303 | } |
3304 | ||
3305 | /* Sweep all RIB tables. */ | |
d4644d41 | 3306 | int rib_sweep_route(struct thread *t) |
718e3744 | 3307 | { |
d62a17ae | 3308 | struct vrf *vrf; |
3309 | struct zebra_vrf *zvrf; | |
78104b9b | 3310 | |
a2addae8 | 3311 | RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) { |
915902cb DS |
3312 | if ((zvrf = vrf->info) == NULL) |
3313 | continue; | |
3314 | ||
d62a17ae | 3315 | rib_sweep_table(zvrf->table[AFI_IP][SAFI_UNICAST]); |
3316 | rib_sweep_table(zvrf->table[AFI_IP6][SAFI_UNICAST]); | |
3317 | } | |
95a29032 | 3318 | |
89272910 | 3319 | zebra_router_sweep_route(); |
38e40db1 | 3320 | zebra_router_sweep_nhgs(); |
d4644d41 DS |
3321 | |
3322 | return 0; | |
718e3744 | 3323 | } |
2ea1ab1c VT |
3324 | |
3325 | /* Remove specific by protocol routes from 'table'. */ | |
d7c0a89a | 3326 | unsigned long rib_score_proto_table(uint8_t proto, unsigned short instance, |
47a08aa9 | 3327 | struct route_table *table) |
d62a17ae | 3328 | { |
3329 | struct route_node *rn; | |
3330 | struct route_entry *re; | |
3331 | struct route_entry *next; | |
3332 | unsigned long n = 0; | |
3333 | ||
3334 | if (table) | |
3335 | for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) | |
a2addae8 | 3336 | RNODE_FOREACH_RE_SAFE (rn, re, next) { |
d62a17ae | 3337 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) |
3338 | continue; | |
3339 | if (re->type == proto | |
3340 | && re->instance == instance) { | |
3341 | rib_delnode(rn, re); | |
3342 | n++; | |
3343 | } | |
3344 | } | |
3345 | return n; | |
2ea1ab1c VT |
3346 | } |
3347 | ||
3348 | /* Remove specific by protocol routes. */ | |
d7c0a89a | 3349 | unsigned long rib_score_proto(uint8_t proto, unsigned short instance) |
2ea1ab1c | 3350 | { |
d62a17ae | 3351 | struct vrf *vrf; |
3352 | struct zebra_vrf *zvrf; | |
d8612e65 | 3353 | struct other_route_table *ort; |
d62a17ae | 3354 | unsigned long cnt = 0; |
78104b9b | 3355 | |
d8612e65 DS |
3356 | RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) { |
3357 | zvrf = vrf->info; | |
3358 | if (!zvrf) | |
3359 | continue; | |
78104b9b | 3360 | |
d8612e65 DS |
3361 | cnt += rib_score_proto_table(proto, instance, |
3362 | zvrf->table[AFI_IP][SAFI_UNICAST]) | |
3363 | + rib_score_proto_table( | |
3364 | proto, instance, | |
3365 | zvrf->table[AFI_IP6][SAFI_UNICAST]); | |
3366 | ||
81fddbe7 | 3367 | frr_each(otable, &zvrf->other_tables, ort) cnt += |
d8612e65 DS |
3368 | rib_score_proto_table(proto, instance, ort->table); |
3369 | } | |
47a08aa9 | 3370 | |
d62a17ae | 3371 | return cnt; |
2ea1ab1c VT |
3372 | } |
3373 | ||
718e3744 | 3374 | /* Close RIB and clean up kernel routes. */ |
d62a17ae | 3375 | void rib_close_table(struct route_table *table) |
718e3744 | 3376 | { |
d62a17ae | 3377 | struct route_node *rn; |
1e9f448f | 3378 | rib_table_info_t *info; |
5f7a4718 | 3379 | rib_dest_t *dest; |
718e3744 | 3380 | |
1e9f448f DS |
3381 | if (!table) |
3382 | return; | |
9fd92e3c | 3383 | |
6ca30e9e | 3384 | info = route_table_get_info(table); |
5adc2528 | 3385 | |
5f7a4718 DS |
3386 | for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) { |
3387 | dest = rib_dest_from_rnode(rn); | |
1e9f448f | 3388 | |
5f7a4718 | 3389 | if (dest && dest->selected_fib) { |
1e9f448f DS |
3390 | if (info->safi == SAFI_UNICAST) |
3391 | hook_call(rib_update, rn, NULL); | |
3392 | ||
3cdba47a DS |
3393 | rib_uninstall_kernel(rn, dest->selected_fib); |
3394 | dest->selected_fib = NULL; | |
1e9f448f | 3395 | } |
5f7a4718 | 3396 | } |
718e3744 | 3397 | } |
3398 | ||
9bd9717b MS |
3399 | /* |
3400 | * Handler for async dataplane results after a pseudowire installation | |
3401 | */ | |
3402 | static int handle_pw_result(struct zebra_dplane_ctx *ctx) | |
3403 | { | |
9bd9717b MS |
3404 | struct zebra_pw *pw; |
3405 | struct zebra_vrf *vrf; | |
3406 | ||
3407 | /* The pseudowire code assumes success - we act on an error | |
3408 | * result for installation attempts here. | |
3409 | */ | |
3410 | if (dplane_ctx_get_op(ctx) != DPLANE_OP_PW_INSTALL) | |
3411 | goto done; | |
3412 | ||
3413 | if (dplane_ctx_get_status(ctx) != ZEBRA_DPLANE_REQUEST_SUCCESS) { | |
3414 | vrf = zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx)); | |
7c7ef4a8 | 3415 | pw = zebra_pw_find(vrf, dplane_ctx_get_ifname(ctx)); |
9bd9717b MS |
3416 | if (pw) |
3417 | zebra_pw_install_failure(pw); | |
3418 | } | |
3419 | ||
3420 | done: | |
3421 | ||
9f2d0354 | 3422 | return 0; |
9bd9717b MS |
3423 | } |
3424 | ||
3425 | ||
7cdb1a84 | 3426 | /* |
d37f4d6c MS |
3427 | * Handle results from the dataplane system. Dequeue update context |
3428 | * structs, dispatch to appropriate internal handlers. | |
7cdb1a84 MS |
3429 | */ |
3430 | static int rib_process_dplane_results(struct thread *thread) | |
3431 | { | |
25779064 | 3432 | struct zebra_dplane_ctx *ctx; |
4c206c8f | 3433 | struct dplane_ctx_q ctxlist; |
2fc69f03 | 3434 | bool shut_p = false; |
4c206c8f MS |
3435 | |
3436 | /* Dequeue a list of completed updates with one lock/unlock cycle */ | |
7cdb1a84 MS |
3437 | |
3438 | do { | |
4c206c8f MS |
3439 | TAILQ_INIT(&ctxlist); |
3440 | ||
7cdb1a84 | 3441 | /* Take lock controlling queue of results */ |
00dffa8c | 3442 | frr_with_mutex(&dplane_mutex) { |
d37f4d6c | 3443 | /* Dequeue list of context structs */ |
4c206c8f | 3444 | dplane_ctx_list_append(&ctxlist, &rib_dplane_q); |
7cdb1a84 | 3445 | } |
7cdb1a84 | 3446 | |
4c206c8f MS |
3447 | /* Dequeue context block */ |
3448 | ctx = dplane_ctx_dequeue(&ctxlist); | |
3449 | ||
3450 | /* If we've emptied the results queue, we're done */ | |
3451 | if (ctx == NULL) | |
7cdb1a84 | 3452 | break; |
7cdb1a84 | 3453 | |
2fc69f03 MS |
3454 | /* If zebra is shutting down, avoid processing results, |
3455 | * just drain the results queue. | |
3456 | */ | |
3457 | shut_p = atomic_load_explicit(&zrouter.in_shutdown, | |
3458 | memory_order_relaxed); | |
3459 | if (shut_p) { | |
3460 | while (ctx) { | |
3461 | dplane_ctx_fini(&ctx); | |
3462 | ||
3463 | ctx = dplane_ctx_dequeue(&ctxlist); | |
3464 | } | |
3465 | ||
3466 | continue; | |
3467 | } | |
3468 | ||
4c206c8f | 3469 | while (ctx) { |
d37f4d6c MS |
3470 | switch (dplane_ctx_get_op(ctx)) { |
3471 | case DPLANE_OP_ROUTE_INSTALL: | |
3472 | case DPLANE_OP_ROUTE_UPDATE: | |
3473 | case DPLANE_OP_ROUTE_DELETE: | |
188a00e0 MS |
3474 | { |
3475 | /* Bit of special case for route updates | |
3476 | * that were generated by async notifications: | |
3477 | * we don't want to continue processing these | |
3478 | * in the rib. | |
3479 | */ | |
3480 | if (dplane_ctx_get_notif_provider(ctx) == 0) | |
3481 | rib_process_result(ctx); | |
3482 | else | |
3483 | dplane_ctx_fini(&ctx); | |
3484 | } | |
3485 | break; | |
d37f4d6c | 3486 | |
54818e3b MS |
3487 | case DPLANE_OP_ROUTE_NOTIFY: |
3488 | rib_process_dplane_notify(ctx); | |
d37f4d6c MS |
3489 | break; |
3490 | ||
9865e1c3 SW |
3491 | case DPLANE_OP_NH_INSTALL: |
3492 | case DPLANE_OP_NH_UPDATE: | |
3493 | case DPLANE_OP_NH_DELETE: | |
3494 | zebra_nhg_dplane_result(ctx); | |
3495 | break; | |
3496 | ||
d37f4d6c MS |
3497 | case DPLANE_OP_LSP_INSTALL: |
3498 | case DPLANE_OP_LSP_UPDATE: | |
3499 | case DPLANE_OP_LSP_DELETE: | |
188a00e0 MS |
3500 | { |
3501 | /* Bit of special case for LSP updates | |
3502 | * that were generated by async notifications: | |
3503 | * we don't want to continue processing these. | |
3504 | */ | |
3505 | if (dplane_ctx_get_notif_provider(ctx) == 0) | |
3506 | zebra_mpls_lsp_dplane_result(ctx); | |
3507 | else | |
3508 | dplane_ctx_fini(&ctx); | |
3509 | } | |
3510 | break; | |
d37f4d6c | 3511 | |
104e3ad9 MS |
3512 | case DPLANE_OP_LSP_NOTIFY: |
3513 | zebra_mpls_process_dplane_notify(ctx); | |
d37f4d6c MS |
3514 | break; |
3515 | ||
9bd9717b MS |
3516 | case DPLANE_OP_PW_INSTALL: |
3517 | case DPLANE_OP_PW_UNINSTALL: | |
3518 | handle_pw_result(ctx); | |
3519 | break; | |
3520 | ||
cf363e1b MS |
3521 | case DPLANE_OP_SYS_ROUTE_ADD: |
3522 | case DPLANE_OP_SYS_ROUTE_DELETE: | |
3523 | /* No further processing in zebra for these. */ | |
3524 | dplane_ctx_fini(&ctx); | |
3525 | break; | |
3526 | ||
036d93c0 MS |
3527 | case DPLANE_OP_MAC_INSTALL: |
3528 | case DPLANE_OP_MAC_DELETE: | |
3529 | zebra_vxlan_handle_result(ctx); | |
3530 | break; | |
3531 | ||
931fa60c MS |
3532 | /* Some op codes not handled here */ |
3533 | case DPLANE_OP_ADDR_INSTALL: | |
3534 | case DPLANE_OP_ADDR_UNINSTALL: | |
3535 | case DPLANE_OP_NEIGH_INSTALL: | |
3536 | case DPLANE_OP_NEIGH_UPDATE: | |
3537 | case DPLANE_OP_NEIGH_DELETE: | |
0bbd4ff4 MS |
3538 | case DPLANE_OP_VTEP_ADD: |
3539 | case DPLANE_OP_VTEP_DELETE: | |
931fa60c | 3540 | case DPLANE_OP_NONE: |
d37f4d6c MS |
3541 | /* Don't expect this: just return the struct? */ |
3542 | dplane_ctx_fini(&ctx); | |
3543 | break; | |
931fa60c | 3544 | |
d37f4d6c | 3545 | } /* Dispatch by op code */ |
4c206c8f MS |
3546 | |
3547 | ctx = dplane_ctx_dequeue(&ctxlist); | |
3548 | } | |
3549 | ||
5709131c | 3550 | } while (1); |
7cdb1a84 | 3551 | |
5709131c | 3552 | return 0; |
7cdb1a84 MS |
3553 | } |
3554 | ||
3555 | /* | |
3556 | * Results are returned from the dataplane subsystem, in the context of | |
1bcea841 | 3557 | * the dataplane pthread. We enqueue the results here for processing by |
7cdb1a84 MS |
3558 | * the main thread later. |
3559 | */ | |
4c206c8f | 3560 | static int rib_dplane_results(struct dplane_ctx_q *ctxlist) |
7cdb1a84 MS |
3561 | { |
3562 | /* Take lock controlling queue of results */ | |
00dffa8c | 3563 | frr_with_mutex(&dplane_mutex) { |
4c206c8f MS |
3564 | /* Enqueue context blocks */ |
3565 | dplane_ctx_list_append(&rib_dplane_q, ctxlist); | |
7cdb1a84 | 3566 | } |
7cdb1a84 | 3567 | |
4c206c8f | 3568 | /* Ensure event is signalled to zebra main pthread */ |
3801e764 | 3569 | thread_add_event(zrouter.master, rib_process_dplane_results, NULL, 0, |
7cdb1a84 MS |
3570 | &t_dplane); |
3571 | ||
5709131c | 3572 | return 0; |
7cdb1a84 MS |
3573 | } |
3574 | ||
eaa2716d SW |
3575 | /* |
3576 | * Ensure there are no empty slots in the route_info array. | |
3577 | * Every route type in zebra should be present there. | |
3578 | */ | |
3579 | static void check_route_info(void) | |
3580 | { | |
3581 | int len = array_size(route_info); | |
3582 | ||
3583 | /* | |
3584 | * ZEBRA_ROUTE_SYSTEM is special cased since | |
3585 | * its key is 0 anyway. | |
3586 | * | |
3587 | * ZEBRA_ROUTE_ALL is also ignored. | |
3588 | */ | |
3589 | for (int i = 0; i < len; i++) { | |
3590 | if (i == ZEBRA_ROUTE_SYSTEM || i == ZEBRA_ROUTE_ALL) | |
3591 | continue; | |
3592 | assert(route_info[i].key); | |
8dc7a759 | 3593 | assert(route_info[i].meta_q_map < MQ_SIZE); |
eaa2716d SW |
3594 | } |
3595 | } | |
3596 | ||
718e3744 | 3597 | /* Routing information base initialize. */ |
d62a17ae | 3598 | void rib_init(void) |
718e3744 | 3599 | { |
eaa2716d SW |
3600 | check_route_info(); |
3601 | ||
2561d12e | 3602 | rib_queue_init(); |
7cdb1a84 MS |
3603 | |
3604 | /* Init dataplane, and register for results */ | |
3605 | pthread_mutex_init(&dplane_mutex, NULL); | |
3606 | TAILQ_INIT(&rib_dplane_q); | |
4c206c8f | 3607 | zebra_dplane_init(rib_dplane_results); |
718e3744 | 3608 | } |
0915bb0c AS |
3609 | |
3610 | /* | |
3611 | * vrf_id_get_next | |
3612 | * | |
3613 | * Get the first vrf id that is greater than the given vrf id if any. | |
3614 | * | |
2951a7a4 | 3615 | * Returns true if a vrf id was found, false otherwise. |
0915bb0c | 3616 | */ |
d62a17ae | 3617 | static inline int vrf_id_get_next(vrf_id_t vrf_id, vrf_id_t *next_id_p) |
0915bb0c | 3618 | { |
d62a17ae | 3619 | struct vrf *vrf; |
b72ede27 | 3620 | |
d62a17ae | 3621 | vrf = vrf_lookup_by_id(vrf_id); |
3622 | if (vrf) { | |
3623 | vrf = RB_NEXT(vrf_id_head, vrf); | |
3624 | if (vrf) { | |
3625 | *next_id_p = vrf->vrf_id; | |
3626 | return 1; | |
3627 | } | |
3628 | } | |
0915bb0c | 3629 | |
d62a17ae | 3630 | return 0; |
0915bb0c AS |
3631 | } |
3632 | ||
3633 | /* | |
3634 | * rib_tables_iter_next | |
3635 | * | |
3636 | * Returns the next table in the iteration. | |
3637 | */ | |
d62a17ae | 3638 | struct route_table *rib_tables_iter_next(rib_tables_iter_t *iter) |
3639 | { | |
3640 | struct route_table *table; | |
3641 | ||
3642 | /* | |
3643 | * Array that helps us go over all AFI/SAFI combinations via one | |
3644 | * index. | |
3645 | */ | |
3646 | static struct { | |
3647 | afi_t afi; | |
3648 | safi_t safi; | |
3649 | } afi_safis[] = { | |
3650 | {AFI_IP, SAFI_UNICAST}, {AFI_IP, SAFI_MULTICAST}, | |
3651 | {AFI_IP, SAFI_LABELED_UNICAST}, {AFI_IP6, SAFI_UNICAST}, | |
3652 | {AFI_IP6, SAFI_MULTICAST}, {AFI_IP6, SAFI_LABELED_UNICAST}, | |
3653 | }; | |
3654 | ||
3655 | table = NULL; | |
3656 | ||
3657 | switch (iter->state) { | |
3658 | ||
3659 | case RIB_TABLES_ITER_S_INIT: | |
3660 | iter->vrf_id = VRF_DEFAULT; | |
3661 | iter->afi_safi_ix = -1; | |
3662 | ||
3663 | /* Fall through */ | |
3664 | ||
3665 | case RIB_TABLES_ITER_S_ITERATING: | |
3666 | iter->afi_safi_ix++; | |
3667 | while (1) { | |
3668 | ||
3669 | while (iter->afi_safi_ix | |
7e3a1ec7 | 3670 | < (int)array_size(afi_safis)) { |
d62a17ae | 3671 | table = zebra_vrf_table( |
3672 | afi_safis[iter->afi_safi_ix].afi, | |
3673 | afi_safis[iter->afi_safi_ix].safi, | |
3674 | iter->vrf_id); | |
3675 | if (table) | |
3676 | break; | |
3677 | ||
3678 | iter->afi_safi_ix++; | |
3679 | } | |
3680 | ||
3681 | /* | |
3682 | * Found another table in this vrf. | |
3683 | */ | |
3684 | if (table) | |
3685 | break; | |
3686 | ||
3687 | /* | |
3688 | * Done with all tables in the current vrf, go to the | |
3689 | * next | |
3690 | * one. | |
3691 | */ | |
3692 | if (!vrf_id_get_next(iter->vrf_id, &iter->vrf_id)) | |
3693 | break; | |
3694 | ||
3695 | iter->afi_safi_ix = 0; | |
3696 | } | |
0915bb0c | 3697 | |
0915bb0c AS |
3698 | break; |
3699 | ||
d62a17ae | 3700 | case RIB_TABLES_ITER_S_DONE: |
3701 | return NULL; | |
0915bb0c AS |
3702 | } |
3703 | ||
d62a17ae | 3704 | if (table) |
3705 | iter->state = RIB_TABLES_ITER_S_ITERATING; | |
3706 | else | |
3707 | iter->state = RIB_TABLES_ITER_S_DONE; | |
0915bb0c | 3708 | |
d62a17ae | 3709 | return table; |
0915bb0c | 3710 | } |