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