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