]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
718e3744 | 2 | /* Routing Information Base. |
3 | * Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro | |
718e3744 | 4 | */ |
5 | ||
6 | #include <zebra.h> | |
7 | ||
718e3744 | 8 | #include "command.h" |
43e52561 QY |
9 | #include "if.h" |
10 | #include "linklist.h" | |
718e3744 | 11 | #include "log.h" |
43e52561 QY |
12 | #include "memory.h" |
13 | #include "mpls.h" | |
14 | #include "nexthop.h" | |
15 | #include "prefix.h" | |
7514fb77 PJ |
16 | #include "prefix.h" |
17 | #include "routemap.h" | |
43e52561 | 18 | #include "sockunion.h" |
05737783 | 19 | #include "srcdest_table.h" |
43e52561 QY |
20 | #include "table.h" |
21 | #include "thread.h" | |
22 | #include "vrf.h" | |
23 | #include "workqueue.h" | |
50d89650 | 24 | #include "nexthop_group_private.h" |
00dffa8c | 25 | #include "frr_pthread.h" |
61e6de9d | 26 | #include "printfrr.h" |
1247efcc | 27 | #include "frrscript.h" |
718e3744 | 28 | |
89272910 | 29 | #include "zebra/zebra_router.h" |
43e52561 QY |
30 | #include "zebra/connected.h" |
31 | #include "zebra/debug.h" | |
32 | #include "zebra/interface.h" | |
33 | #include "zebra/redistribute.h" | |
718e3744 | 34 | #include "zebra/rib.h" |
35 | #include "zebra/rt.h" | |
43e52561 QY |
36 | #include "zebra/zapi_msg.h" |
37 | #include "zebra/zebra_errors.h" | |
7c551956 | 38 | #include "zebra/zebra_ns.h" |
fb018d25 | 39 | #include "zebra/zebra_rnh.h" |
43e52561 QY |
40 | #include "zebra/zebra_routemap.h" |
41 | #include "zebra/zebra_vrf.h" | |
6134fd82 | 42 | #include "zebra/zebra_vxlan.h" |
7cdb1a84 MS |
43 | #include "zebra/zapi_msg.h" |
44 | #include "zebra/zebra_dplane.h" | |
32367e7a | 45 | #include "zebra/zebra_evpn_mh.h" |
1247efcc | 46 | #include "zebra/zebra_script.h" |
7cdb1a84 | 47 | |
224ccf29 DL |
48 | DEFINE_MGROUP(ZEBRA, "zebra"); |
49 | ||
50 | DEFINE_MTYPE(ZEBRA, RE, "Route Entry"); | |
51 | DEFINE_MTYPE_STATIC(ZEBRA, RIB_DEST, "RIB destination"); | |
2a99ab95 | 52 | DEFINE_MTYPE_STATIC(ZEBRA, RIB_UPDATE_CTX, "Rib update context object"); |
32367e7a | 53 | DEFINE_MTYPE_STATIC(ZEBRA, WQ_WRAPPER, "WQ wrapper"); |
2a99ab95 | 54 | |
7cdb1a84 MS |
55 | /* |
56 | * Event, list, and mutex for delivery of dataplane results | |
57 | */ | |
58 | static pthread_mutex_t dplane_mutex; | |
59 | static struct thread *t_dplane; | |
ac96497c | 60 | static struct dplane_ctx_list_head rib_dplane_q; |
718e3744 | 61 | |
d62a17ae | 62 | DEFINE_HOOK(rib_update, (struct route_node * rn, const char *reason), |
8451921b | 63 | (rn, reason)); |
0d0f516c | 64 | DEFINE_HOOK(rib_shutdown, (struct route_node * rn), (rn)); |
65 | ||
4f8ea50c | 66 | |
014e732d DS |
67 | /* Meta Q's specific names */ |
68 | enum meta_queue_indexes { | |
69 | META_QUEUE_NHG, | |
70 | META_QUEUE_EVPN, | |
d7ac4c4d | 71 | META_QUEUE_EARLY_ROUTE, |
53216dff | 72 | META_QUEUE_EARLY_LABEL, |
014e732d DS |
73 | META_QUEUE_CONNECTED, |
74 | META_QUEUE_KERNEL, | |
75 | META_QUEUE_STATIC, | |
76 | META_QUEUE_NOTBGP, | |
77 | META_QUEUE_BGP, | |
78 | META_QUEUE_OTHER, | |
79 | }; | |
80 | ||
718e3744 | 81 | /* Each route type's string and default distance value. */ |
d62a17ae | 82 | static const struct { |
83 | int key; | |
8dc7a759 | 84 | uint8_t distance; |
014e732d | 85 | enum meta_queue_indexes meta_q_map; |
d62a17ae | 86 | } route_info[ZEBRA_ROUTE_MAX] = { |
3d55a4ef DS |
87 | [ZEBRA_ROUTE_NHG] = |
88 | {ZEBRA_ROUTE_NHG, | |
89 | ZEBRA_MAX_DISTANCE_DEFAULT /* Unneeded for nhg's */, | |
90 | META_QUEUE_NHG}, | |
91 | [ZEBRA_ROUTE_SYSTEM] = {ZEBRA_ROUTE_SYSTEM, | |
92 | ZEBRA_KERNEL_DISTANCE_DEFAULT, | |
93 | META_QUEUE_KERNEL}, | |
94 | [ZEBRA_ROUTE_KERNEL] = {ZEBRA_ROUTE_KERNEL, | |
95 | ZEBRA_KERNEL_DISTANCE_DEFAULT, | |
96 | META_QUEUE_KERNEL}, | |
97 | [ZEBRA_ROUTE_CONNECT] = {ZEBRA_ROUTE_CONNECT, | |
98 | ZEBRA_CONNECT_DISTANCE_DEFAULT, | |
99 | META_QUEUE_CONNECTED}, | |
100 | [ZEBRA_ROUTE_STATIC] = {ZEBRA_ROUTE_STATIC, | |
101 | ZEBRA_STATIC_DISTANCE_DEFAULT, | |
102 | META_QUEUE_STATIC}, | |
103 | [ZEBRA_ROUTE_RIP] = {ZEBRA_ROUTE_RIP, ZEBRA_RIP_DISTANCE_DEFAULT, | |
104 | META_QUEUE_NOTBGP}, | |
105 | [ZEBRA_ROUTE_RIPNG] = {ZEBRA_ROUTE_RIPNG, ZEBRA_RIP_DISTANCE_DEFAULT, | |
106 | META_QUEUE_NOTBGP}, | |
107 | [ZEBRA_ROUTE_OSPF] = {ZEBRA_ROUTE_OSPF, ZEBRA_OSPF_DISTANCE_DEFAULT, | |
108 | META_QUEUE_NOTBGP}, | |
109 | [ZEBRA_ROUTE_OSPF6] = {ZEBRA_ROUTE_OSPF6, ZEBRA_OSPF6_DISTANCE_DEFAULT, | |
110 | META_QUEUE_NOTBGP}, | |
111 | [ZEBRA_ROUTE_ISIS] = {ZEBRA_ROUTE_ISIS, ZEBRA_ISIS_DISTANCE_DEFAULT, | |
112 | META_QUEUE_NOTBGP}, | |
113 | [ZEBRA_ROUTE_BGP] = {ZEBRA_ROUTE_BGP, | |
114 | ZEBRA_EBGP_DISTANCE_DEFAULT /* IBGP is 200. */, | |
014e732d | 115 | META_QUEUE_BGP}, |
3d55a4ef DS |
116 | [ZEBRA_ROUTE_PIM] = {ZEBRA_ROUTE_PIM, ZEBRA_MAX_DISTANCE_DEFAULT, |
117 | META_QUEUE_OTHER}, | |
118 | [ZEBRA_ROUTE_EIGRP] = {ZEBRA_ROUTE_EIGRP, ZEBRA_EIGRP_DISTANCE_DEFAULT, | |
119 | META_QUEUE_NOTBGP}, | |
120 | [ZEBRA_ROUTE_NHRP] = {ZEBRA_ROUTE_NHRP, ZEBRA_NHRP_DISTANCE_DEFAULT, | |
121 | META_QUEUE_NOTBGP}, | |
122 | [ZEBRA_ROUTE_HSLS] = {ZEBRA_ROUTE_HSLS, ZEBRA_MAX_DISTANCE_DEFAULT, | |
123 | META_QUEUE_OTHER}, | |
124 | [ZEBRA_ROUTE_OLSR] = {ZEBRA_ROUTE_OLSR, ZEBRA_MAX_DISTANCE_DEFAULT, | |
125 | META_QUEUE_OTHER}, | |
8b6a9cca | 126 | [ZEBRA_ROUTE_TABLE] = {ZEBRA_ROUTE_TABLE, ZEBRA_TABLE_DISTANCE_DEFAULT, META_QUEUE_STATIC}, |
3d55a4ef DS |
127 | [ZEBRA_ROUTE_LDP] = {ZEBRA_ROUTE_LDP, ZEBRA_LDP_DISTANCE_DEFAULT, |
128 | META_QUEUE_OTHER}, | |
129 | [ZEBRA_ROUTE_VNC] = {ZEBRA_ROUTE_VNC, ZEBRA_EBGP_DISTANCE_DEFAULT, | |
130 | META_QUEUE_BGP}, | |
131 | [ZEBRA_ROUTE_VNC_DIRECT] = {ZEBRA_ROUTE_VNC_DIRECT, | |
132 | ZEBRA_EBGP_DISTANCE_DEFAULT, | |
133 | META_QUEUE_BGP}, | |
134 | [ZEBRA_ROUTE_VNC_DIRECT_RH] = {ZEBRA_ROUTE_VNC_DIRECT_RH, | |
135 | ZEBRA_EBGP_DISTANCE_DEFAULT, | |
014e732d | 136 | META_QUEUE_BGP}, |
3d55a4ef DS |
137 | [ZEBRA_ROUTE_BGP_DIRECT] = {ZEBRA_ROUTE_BGP_DIRECT, |
138 | ZEBRA_EBGP_DISTANCE_DEFAULT, | |
139 | META_QUEUE_BGP}, | |
140 | [ZEBRA_ROUTE_BGP_DIRECT_EXT] = {ZEBRA_ROUTE_BGP_DIRECT_EXT, | |
141 | ZEBRA_EBGP_DISTANCE_DEFAULT, | |
014e732d | 142 | META_QUEUE_BGP}, |
3d55a4ef DS |
143 | [ZEBRA_ROUTE_BABEL] = {ZEBRA_ROUTE_BABEL, ZEBRA_BABEL_DISTANCE_DEFAULT, |
144 | META_QUEUE_NOTBGP}, | |
145 | [ZEBRA_ROUTE_SHARP] = {ZEBRA_ROUTE_SHARP, ZEBRA_SHARP_DISTANCE_DEFAULT, | |
146 | META_QUEUE_OTHER}, | |
147 | [ZEBRA_ROUTE_PBR] = {ZEBRA_ROUTE_PBR, ZEBRA_PBR_DISTANCE_DEFAULT, | |
148 | META_QUEUE_OTHER}, | |
149 | [ZEBRA_ROUTE_BFD] = {ZEBRA_ROUTE_BFD, ZEBRA_MAX_DISTANCE_DEFAULT, | |
150 | META_QUEUE_OTHER}, | |
151 | [ZEBRA_ROUTE_OPENFABRIC] = {ZEBRA_ROUTE_OPENFABRIC, | |
152 | ZEBRA_OPENFABRIC_DISTANCE_DEFAULT, | |
014e732d | 153 | META_QUEUE_NOTBGP}, |
3d55a4ef DS |
154 | [ZEBRA_ROUTE_VRRP] = {ZEBRA_ROUTE_VRRP, ZEBRA_MAX_DISTANCE_DEFAULT, |
155 | META_QUEUE_OTHER}, | |
156 | [ZEBRA_ROUTE_SRTE] = {ZEBRA_ROUTE_SRTE, ZEBRA_MAX_DISTANCE_DEFAULT, | |
157 | META_QUEUE_OTHER}, | |
158 | [ZEBRA_ROUTE_ALL] = {ZEBRA_ROUTE_ALL, ZEBRA_MAX_DISTANCE_DEFAULT, | |
159 | META_QUEUE_OTHER}, | |
d6abd8b0 | 160 | /* Any new route type added to zebra, should be mirrored here */ |
c710b277 | 161 | |
d62a17ae | 162 | /* no entry/default: 150 */ |
718e3744 | 163 | }; |
6b0655a2 | 164 | |
04bec7b2 MS |
165 | /* Wrapper struct for nhg workqueue items; a 'ctx' is an incoming update |
166 | * from the OS, and an 'nhe' is a nhe update. | |
167 | */ | |
168 | struct wq_nhg_wrapper { | |
169 | int type; | |
170 | union { | |
171 | struct nhg_ctx *ctx; | |
172 | struct nhg_hash_entry *nhe; | |
173 | } u; | |
174 | }; | |
175 | ||
176 | #define WQ_NHG_WRAPPER_TYPE_CTX 0x01 | |
177 | #define WQ_NHG_WRAPPER_TYPE_NHG 0x02 | |
178 | ||
32367e7a MS |
179 | /* Wrapper structs for evpn/vxlan workqueue items. */ |
180 | struct wq_evpn_wrapper { | |
181 | int type; | |
182 | bool add_p; | |
183 | vrf_id_t vrf_id; | |
184 | bool esr_rxed; | |
185 | uint8_t df_alg; | |
186 | uint16_t df_pref; | |
7f7e49d1 MS |
187 | uint32_t flags; |
188 | uint32_t seq; | |
32367e7a | 189 | esi_t esi; |
7f7e49d1 | 190 | vni_t vni; |
32367e7a | 191 | struct ipaddr ip; |
7f7e49d1 | 192 | struct ethaddr macaddr; |
32367e7a | 193 | struct prefix prefix; |
7f7e49d1 | 194 | struct in_addr vtep_ip; |
32367e7a MS |
195 | }; |
196 | ||
7f7e49d1 MS |
197 | #define WQ_EVPN_WRAPPER_TYPE_VRFROUTE 0x01 |
198 | #define WQ_EVPN_WRAPPER_TYPE_REM_ES 0x02 | |
199 | #define WQ_EVPN_WRAPPER_TYPE_REM_MACIP 0x03 | |
7e5b0b2b | 200 | #define WQ_EVPN_WRAPPER_TYPE_REM_VTEP 0x04 |
32367e7a | 201 | |
53216dff DS |
202 | enum wq_label_types { |
203 | WQ_LABEL_FTN_UNINSTALL, | |
204 | WQ_LABEL_LABELS_PROCESS, | |
205 | }; | |
206 | ||
207 | struct wq_label_wrapper { | |
208 | enum wq_label_types type; | |
209 | vrf_id_t vrf_id; | |
210 | ||
211 | struct prefix p; | |
212 | enum lsp_types_t ltype; | |
213 | uint8_t route_type; | |
214 | uint8_t route_instance; | |
215 | ||
216 | bool add_p; | |
217 | struct zapi_labels zl; | |
218 | ||
219 | int afi; | |
220 | }; | |
221 | ||
d7ac4c4d DS |
222 | static void rib_addnode(struct route_node *rn, struct route_entry *re, |
223 | int process); | |
224 | ||
9d75e309 DL |
225 | /* %pRN is already a printer for route_nodes that just prints the prefix */ |
226 | #ifdef _FRR_ATTRIBUTE_PRINTFRR | |
227 | #pragma FRR printfrr_ext "%pZN" (struct route_node *) | |
228 | #endif | |
229 | ||
014e732d | 230 | static const char *subqueue2str(enum meta_queue_indexes index) |
7a96ccc7 DS |
231 | { |
232 | switch (index) { | |
4bbbd1f6 | 233 | case META_QUEUE_NHG: |
7a96ccc7 | 234 | return "NHG Objects"; |
4bbbd1f6 | 235 | case META_QUEUE_EVPN: |
7a96ccc7 | 236 | return "EVPN/VxLan Objects"; |
d7ac4c4d DS |
237 | case META_QUEUE_EARLY_ROUTE: |
238 | return "Early Route Processing"; | |
53216dff DS |
239 | case META_QUEUE_EARLY_LABEL: |
240 | return "Early Label Handling"; | |
014e732d | 241 | case META_QUEUE_CONNECTED: |
7a96ccc7 | 242 | return "Connected Routes"; |
014e732d | 243 | case META_QUEUE_KERNEL: |
7a96ccc7 | 244 | return "Kernel Routes"; |
014e732d | 245 | case META_QUEUE_STATIC: |
7a96ccc7 | 246 | return "Static Routes"; |
014e732d | 247 | case META_QUEUE_NOTBGP: |
7a96ccc7 | 248 | return "RIP/OSPF/ISIS/EIGRP/NHRP Routes"; |
014e732d | 249 | case META_QUEUE_BGP: |
7a96ccc7 | 250 | return "BGP Routes"; |
014e732d | 251 | case META_QUEUE_OTHER: |
7a96ccc7 DS |
252 | return "Other Routes"; |
253 | } | |
254 | ||
255 | return "Unknown"; | |
256 | } | |
257 | ||
9d75e309 DL |
258 | printfrr_ext_autoreg_p("ZN", printfrr_zebra_node); |
259 | static ssize_t printfrr_zebra_node(struct fbuf *buf, struct printfrr_eargs *ea, | |
260 | const void *ptr) | |
2263a412 | 261 | { |
9d75e309 DL |
262 | struct route_node *rn = (struct route_node *)ptr; |
263 | ssize_t rv = 0; | |
264 | ||
265 | /* just the table number? */ | |
266 | if (ea->fmt[0] == 't') { | |
267 | rib_dest_t *dest; | |
c2c02b76 DS |
268 | struct route_entry *re = NULL; |
269 | ||
9d75e309 | 270 | ea->fmt++; |
cb653491 | 271 | |
9d75e309 DL |
272 | if (!rn) |
273 | return bputch(buf, '!'); | |
c2c02b76 DS |
274 | |
275 | dest = rib_dest_from_rnode(rn); | |
276 | if (dest) | |
277 | re = re_list_first(&dest->routes); | |
278 | if (re) | |
9d75e309 DL |
279 | rv += bprintfrr(buf, "%u", re->table); |
280 | else | |
281 | rv += bputch(buf, '?'); | |
282 | ||
d62a17ae | 283 | } else { |
9d75e309 DL |
284 | char cbuf[PREFIX_STRLEN * 2 + 6]; |
285 | struct rib_table_info *info; | |
286 | ||
287 | if (!rn) | |
288 | return bputs(buf, "{(route_node *) NULL}"); | |
2263a412 | 289 | |
9d75e309 DL |
290 | srcdest_rnode2str(rn, cbuf, sizeof(cbuf)); |
291 | rv += bputs(buf, cbuf); | |
292 | ||
293 | info = srcdest_rnode_table_info(rn); | |
294 | if (info->safi == SAFI_MULTICAST) | |
295 | rv += bputs(buf, " (MRIB)"); | |
296 | } | |
297 | return rv; | |
2263a412 DL |
298 | } |
299 | ||
9d75e309 DL |
300 | #define rnode_debug(node, vrf_id, msg, ...) \ |
301 | zlog_debug("%s: (%u:%pZNt):%pZN: " msg, __func__, vrf_id, node, node, \ | |
302 | ##__VA_ARGS__) | |
303 | ||
304 | #define rnode_info(node, vrf_id, msg, ...) \ | |
305 | zlog_info("%s: (%u:%pZNt):%pZN: " msg, __func__, vrf_id, node, node, \ | |
306 | ##__VA_ARGS__) | |
307 | ||
61e6de9d DS |
308 | static char *_dump_re_status(const struct route_entry *re, char *buf, |
309 | size_t len) | |
310 | { | |
311 | if (re->status == 0) { | |
312 | snprintfrr(buf, len, "None "); | |
313 | return buf; | |
314 | } | |
315 | ||
316 | snprintfrr( | |
8d4665aa | 317 | buf, len, "%s%s%s%s%s%s%s%s", |
61e6de9d DS |
318 | CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED) ? "Removed " : "", |
319 | CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED) ? "Changed " : "", | |
320 | CHECK_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED) | |
321 | ? "Label Changed " | |
322 | : "", | |
323 | CHECK_FLAG(re->status, ROUTE_ENTRY_QUEUED) ? "Queued " : "", | |
8d4665aa DS |
324 | CHECK_FLAG(re->status, ROUTE_ENTRY_ROUTE_REPLACING) |
325 | ? "Replacing" | |
326 | : "", | |
61e6de9d DS |
327 | CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED) ? "Installed " |
328 | : "", | |
329 | CHECK_FLAG(re->status, ROUTE_ENTRY_FAILED) ? "Failed " : "", | |
330 | CHECK_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG) ? "Fib NHG " | |
331 | : ""); | |
332 | return buf; | |
333 | } | |
334 | ||
fd289fc8 | 335 | uint8_t route_distance(int type) |
40c7bdb0 | 336 | { |
fd289fc8 | 337 | uint8_t distance; |
40c7bdb0 | 338 | |
d62a17ae | 339 | if ((unsigned)type >= array_size(route_info)) |
340 | distance = 150; | |
341 | else | |
342 | distance = route_info[type].distance; | |
40c7bdb0 | 343 | |
d62a17ae | 344 | return distance; |
40c7bdb0 | 345 | } |
346 | ||
d7c0a89a | 347 | int is_zebra_valid_kernel_table(uint32_t table_id) |
7a4bb9c5 | 348 | { |
8f500a1c | 349 | #ifdef linux |
d62a17ae | 350 | if ((table_id == RT_TABLE_UNSPEC) || (table_id == RT_TABLE_LOCAL) |
351 | || (table_id == RT_TABLE_COMPAT)) | |
352 | return 0; | |
8f500a1c RW |
353 | #endif |
354 | ||
d62a17ae | 355 | return 1; |
7a4bb9c5 DS |
356 | } |
357 | ||
d7c0a89a | 358 | int is_zebra_main_routing_table(uint32_t table_id) |
7a4bb9c5 | 359 | { |
c447ad08 | 360 | if (table_id == RT_TABLE_MAIN) |
d62a17ae | 361 | return 1; |
362 | return 0; | |
7a4bb9c5 DS |
363 | } |
364 | ||
86391e56 | 365 | int zebra_check_addr(const struct prefix *p) |
0aabccc0 | 366 | { |
d62a17ae | 367 | if (p->family == AF_INET) { |
d7c0a89a | 368 | uint32_t addr; |
0aabccc0 | 369 | |
d62a17ae | 370 | addr = p->u.prefix4.s_addr; |
371 | addr = ntohl(addr); | |
0aabccc0 | 372 | |
d62a17ae | 373 | if (IPV4_NET127(addr) || IN_CLASSD(addr) |
374 | || IPV4_LINKLOCAL(addr)) | |
375 | return 0; | |
376 | } | |
377 | if (p->family == AF_INET6) { | |
378 | if (IN6_IS_ADDR_LOOPBACK(&p->u.prefix6)) | |
379 | return 0; | |
380 | if (IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6)) | |
381 | return 0; | |
382 | } | |
383 | return 1; | |
0aabccc0 DD |
384 | } |
385 | ||
5463ce26 MS |
386 | static void route_entry_attach_ref(struct route_entry *re, |
387 | struct nhg_hash_entry *new) | |
718e3744 | 388 | { |
0eb97b86 | 389 | re->nhe = new; |
5463ce26 | 390 | re->nhe_id = new->id; |
002930f7 | 391 | re->nhe_installed_id = 0; |
718e3744 | 392 | |
5463ce26 | 393 | zebra_nhg_increment_ref(new); |
718e3744 | 394 | } |
395 | ||
002930f7 | 396 | /* Replace (if 'new_nhghe') or clear (if that's NULL) an re's nhe. */ |
cd7108ba EDP |
397 | int route_entry_update_nhe(struct route_entry *re, |
398 | struct nhg_hash_entry *new_nhghe) | |
718e3744 | 399 | { |
5463ce26 | 400 | int ret = 0; |
002930f7 | 401 | struct nhg_hash_entry *old_nhg = NULL; |
718e3744 | 402 | |
cd7108ba | 403 | if (new_nhghe == NULL) { |
002930f7 MS |
404 | old_nhg = re->nhe; |
405 | ||
406 | re->nhe_id = 0; | |
407 | re->nhe_installed_id = 0; | |
c415d895 | 408 | re->nhe = NULL; |
5463ce26 MS |
409 | goto done; |
410 | } | |
718e3744 | 411 | |
1f655680 | 412 | if ((re->nhe_id != 0) && re->nhe && (re->nhe != new_nhghe)) { |
002930f7 MS |
413 | /* Capture previous nhg, if any */ |
414 | old_nhg = re->nhe; | |
718e3744 | 415 | |
cd7108ba | 416 | route_entry_attach_ref(re, new_nhghe); |
84a89a8d | 417 | } else if (!re->nhe) |
5463ce26 | 418 | /* This is the first time it's being attached */ |
cd7108ba | 419 | route_entry_attach_ref(re, new_nhghe); |
595db7f1 | 420 | |
5463ce26 | 421 | done: |
002930f7 MS |
422 | /* Detach / deref previous nhg */ |
423 | if (old_nhg) | |
424 | zebra_nhg_decrement_ref(old_nhg); | |
425 | ||
5463ce26 | 426 | return ret; |
595db7f1 | 427 | } |
428 | ||
2e8db20d EDP |
429 | void rib_handle_nhg_replace(struct nhg_hash_entry *old_entry, |
430 | struct nhg_hash_entry *new_entry) | |
1f655680 SW |
431 | { |
432 | struct zebra_router_table *zrt; | |
433 | struct route_node *rn; | |
434 | struct route_entry *re, *next; | |
435 | ||
436 | if (IS_ZEBRA_DEBUG_RIB_DETAILED || IS_ZEBRA_DEBUG_NHG_DETAIL) | |
437 | zlog_debug("%s: replacing routes nhe (%u) OLD %p NEW %p", | |
2e8db20d | 438 | __func__, new_entry->id, new_entry, old_entry); |
1f655680 SW |
439 | |
440 | /* We have to do them ALL */ | |
441 | RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) { | |
442 | for (rn = route_top(zrt->table); rn; | |
443 | rn = srcdest_route_next(rn)) { | |
444 | RNODE_FOREACH_RE_SAFE (rn, re, next) { | |
2e8db20d EDP |
445 | if (re->nhe && re->nhe == old_entry) |
446 | route_entry_update_nhe(re, new_entry); | |
1f655680 SW |
447 | } |
448 | } | |
449 | } | |
450 | } | |
451 | ||
d62a17ae | 452 | struct route_entry *rib_match(afi_t afi, safi_t safi, vrf_id_t vrf_id, |
bc77c3bb MS |
453 | const union g_addr *addr, |
454 | struct route_node **rn_out) | |
d62a17ae | 455 | { |
456 | struct prefix p; | |
457 | struct route_table *table; | |
458 | struct route_node *rn; | |
5f7a4718 | 459 | struct route_entry *match = NULL; |
d62a17ae | 460 | |
461 | /* Lookup table. */ | |
462 | table = zebra_vrf_table(afi, safi, vrf_id); | |
463 | if (!table) | |
464 | return 0; | |
465 | ||
6006b807 | 466 | memset(&p, 0, sizeof(p)); |
d62a17ae | 467 | p.family = afi; |
468 | if (afi == AFI_IP) { | |
469 | p.u.prefix4 = addr->ipv4; | |
936fbaef | 470 | p.prefixlen = IPV4_MAX_BITLEN; |
d62a17ae | 471 | } else { |
472 | p.u.prefix6 = addr->ipv6; | |
f4d81e55 | 473 | p.prefixlen = IPV6_MAX_BITLEN; |
d62a17ae | 474 | } |
718e3744 | 475 | |
c4efd0f4 | 476 | rn = route_node_match(table, &p); |
718e3744 | 477 | |
d62a17ae | 478 | while (rn) { |
5f7a4718 DS |
479 | rib_dest_t *dest; |
480 | ||
d62a17ae | 481 | route_unlock_node(rn); |
718e3744 | 482 | |
5f7a4718 | 483 | dest = rib_dest_from_rnode(rn); |
996c9314 LB |
484 | if (dest && dest->selected_fib |
485 | && !CHECK_FLAG(dest->selected_fib->status, | |
486 | ROUTE_ENTRY_REMOVED)) | |
5f7a4718 | 487 | match = dest->selected_fib; |
718e3744 | 488 | |
d62a17ae | 489 | /* If there is no selected route or matched route is EGP, go up |
490 | tree. */ | |
491 | if (!match) { | |
492 | do { | |
493 | rn = rn->parent; | |
494 | } while (rn && rn->info == NULL); | |
495 | if (rn) | |
496 | route_lock_node(rn); | |
497 | } else { | |
498 | if (match->type != ZEBRA_ROUTE_CONNECT) { | |
677c1dd5 DS |
499 | if (!CHECK_FLAG(match->status, |
500 | ROUTE_ENTRY_INSTALLED)) | |
d62a17ae | 501 | return NULL; |
502 | } | |
503 | ||
504 | if (rn_out) | |
505 | *rn_out = rn; | |
506 | return match; | |
507 | } | |
16814f96 | 508 | } |
d62a17ae | 509 | return NULL; |
510 | } | |
511 | ||
312e29b0 DS |
512 | struct route_entry *rib_match_multicast(afi_t afi, vrf_id_t vrf_id, |
513 | union g_addr *gaddr, | |
514 | struct route_node **rn_out) | |
d62a17ae | 515 | { |
516 | struct route_entry *re = NULL, *mre = NULL, *ure = NULL; | |
517 | struct route_node *m_rn = NULL, *u_rn = NULL; | |
d62a17ae | 518 | |
526052fb | 519 | switch (zrouter.ipv4_multicast_mode) { |
d62a17ae | 520 | case MCAST_MRIB_ONLY: |
312e29b0 | 521 | return rib_match(afi, SAFI_MULTICAST, vrf_id, gaddr, rn_out); |
d62a17ae | 522 | case MCAST_URIB_ONLY: |
312e29b0 | 523 | return rib_match(afi, SAFI_UNICAST, vrf_id, gaddr, rn_out); |
d62a17ae | 524 | case MCAST_NO_CONFIG: |
525 | case MCAST_MIX_MRIB_FIRST: | |
312e29b0 | 526 | re = mre = rib_match(afi, SAFI_MULTICAST, vrf_id, gaddr, &m_rn); |
d62a17ae | 527 | if (!mre) |
312e29b0 DS |
528 | re = ure = rib_match(afi, SAFI_UNICAST, vrf_id, gaddr, |
529 | &u_rn); | |
d62a17ae | 530 | break; |
531 | case MCAST_MIX_DISTANCE: | |
312e29b0 DS |
532 | mre = rib_match(afi, SAFI_MULTICAST, vrf_id, gaddr, &m_rn); |
533 | ure = rib_match(afi, SAFI_UNICAST, vrf_id, gaddr, &u_rn); | |
d62a17ae | 534 | if (mre && ure) |
535 | re = ure->distance < mre->distance ? ure : mre; | |
536 | else if (mre) | |
537 | re = mre; | |
538 | else if (ure) | |
539 | re = ure; | |
540 | break; | |
541 | case MCAST_MIX_PFXLEN: | |
312e29b0 DS |
542 | mre = rib_match(afi, SAFI_MULTICAST, vrf_id, gaddr, &m_rn); |
543 | ure = rib_match(afi, SAFI_UNICAST, vrf_id, gaddr, &u_rn); | |
d62a17ae | 544 | if (mre && ure) |
545 | re = u_rn->p.prefixlen > m_rn->p.prefixlen ? ure : mre; | |
546 | else if (mre) | |
547 | re = mre; | |
548 | else if (ure) | |
549 | re = ure; | |
550 | break; | |
718e3744 | 551 | } |
33550aa8 | 552 | |
d62a17ae | 553 | if (rn_out) |
554 | *rn_out = (re == mre) ? m_rn : u_rn; | |
718e3744 | 555 | |
d62a17ae | 556 | if (IS_ZEBRA_DEBUG_RIB) { |
557 | char buf[BUFSIZ]; | |
312e29b0 DS |
558 | inet_ntop(afi == AFI_IP ? AF_INET : AF_INET6, gaddr, buf, |
559 | BUFSIZ); | |
4623d897 | 560 | |
312e29b0 DS |
561 | zlog_debug("%s: %s: %pRN vrf: %s(%u) found %s, using %s", |
562 | __func__, buf, (re == mre) ? m_rn : u_rn, | |
563 | vrf_id_to_name(vrf_id), vrf_id, | |
d62a17ae | 564 | mre ? (ure ? "MRIB+URIB" : "MRIB") |
565 | : ure ? "URIB" : "nothing", | |
566 | re == ure ? "URIB" : re == mre ? "MRIB" : "none"); | |
567 | } | |
568 | return re; | |
4623d897 | 569 | } |
425fd200 | 570 | |
d62a17ae | 571 | struct route_entry *rib_lookup_ipv4(struct prefix_ipv4 *p, vrf_id_t vrf_id) |
718e3744 | 572 | { |
d62a17ae | 573 | struct route_table *table; |
574 | struct route_node *rn; | |
5f7a4718 | 575 | struct route_entry *match = NULL; |
5f7a4718 | 576 | rib_dest_t *dest; |
718e3744 | 577 | |
d62a17ae | 578 | /* Lookup table. */ |
579 | table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id); | |
580 | if (!table) | |
581 | return 0; | |
718e3744 | 582 | |
d62a17ae | 583 | rn = route_node_lookup(table, (struct prefix *)p); |
718e3744 | 584 | |
d62a17ae | 585 | /* No route for this prefix. */ |
586 | if (!rn) | |
587 | return NULL; | |
718e3744 | 588 | |
d62a17ae | 589 | /* Unlock node. */ |
590 | route_unlock_node(rn); | |
5f7a4718 | 591 | dest = rib_dest_from_rnode(rn); |
718e3744 | 592 | |
996c9314 LB |
593 | if (dest && dest->selected_fib |
594 | && !CHECK_FLAG(dest->selected_fib->status, ROUTE_ENTRY_REMOVED)) | |
5f7a4718 | 595 | match = dest->selected_fib; |
718e3744 | 596 | |
d62a17ae | 597 | if (!match) |
598 | return NULL; | |
718e3744 | 599 | |
d62a17ae | 600 | if (match->type == ZEBRA_ROUTE_CONNECT) |
601 | return match; | |
f9e1b38e | 602 | |
677c1dd5 DS |
603 | if (CHECK_FLAG(match->status, ROUTE_ENTRY_INSTALLED)) |
604 | return match; | |
718e3744 | 605 | |
d62a17ae | 606 | return NULL; |
718e3744 | 607 | } |
608 | ||
a64448ba DS |
609 | /* |
610 | * Is this RIB labeled-unicast? It must be of type BGP and all paths | |
611 | * (nexthops) must have a label. | |
612 | */ | |
d62a17ae | 613 | int zebra_rib_labeled_unicast(struct route_entry *re) |
a64448ba | 614 | { |
d62a17ae | 615 | struct nexthop *nexthop = NULL; |
a64448ba | 616 | |
d62a17ae | 617 | if (re->type != ZEBRA_ROUTE_BGP) |
618 | return 0; | |
a64448ba | 619 | |
c415d895 | 620 | for (ALL_NEXTHOPS(re->nhe->nhg, nexthop)) |
d62a17ae | 621 | if (!nexthop->nh_label || !nexthop->nh_label->num_labels) |
622 | return 0; | |
6b0655a2 | 623 | |
d62a17ae | 624 | return 1; |
a64448ba | 625 | } |
718e3744 | 626 | |
6ae24471 DS |
627 | /* Update flag indicates whether this is a "replace" or not. Currently, this |
628 | * is only used for IPv4. | |
629 | */ | |
0c555cc6 DS |
630 | void rib_install_kernel(struct route_node *rn, struct route_entry *re, |
631 | struct route_entry *old) | |
718e3744 | 632 | { |
d62a17ae | 633 | struct nexthop *nexthop; |
630d5962 | 634 | struct rib_table_info *info = srcdest_rnode_table_info(rn); |
d62a17ae | 635 | struct zebra_vrf *zvrf = vrf_info_lookup(re->vrf_id); |
97f5b441 MS |
636 | const struct prefix *p, *src_p; |
637 | enum zebra_dplane_result ret; | |
638 | ||
639 | rib_dest_t *dest = rib_dest_from_rnode(rn); | |
718e3744 | 640 | |
d62a17ae | 641 | srcdest_rnode_prefixes(rn, &p, &src_p); |
416ec78d | 642 | |
d62a17ae | 643 | if (info->safi != SAFI_UNICAST) { |
c415d895 | 644 | for (ALL_NEXTHOPS(re->nhe->nhg, nexthop)) |
d62a17ae | 645 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB); |
0c555cc6 | 646 | return; |
d62a17ae | 647 | } |
718e3744 | 648 | |
9ef49038 | 649 | |
8dfbc657 SW |
650 | /* |
651 | * Install the resolved nexthop object first. | |
652 | */ | |
c415d895 | 653 | zebra_nhg_install_kernel(re->nhe); |
8dfbc657 | 654 | |
2063a814 DS |
655 | /* |
656 | * If this is a replace to a new RE let the originator of the RE | |
657 | * know that they've lost | |
658 | */ | |
9a9f8926 | 659 | if (old && (old != re) && (old->type != re->type)) |
a7704e1b | 660 | zsend_route_notify_owner(rn, old, ZAPI_ROUTE_BETTER_ADMIN_WON, |
77b38a4a | 661 | info->afi, info->safi); |
25b9cb0c | 662 | |
97f5b441 MS |
663 | /* Update fib selection */ |
664 | dest->selected_fib = re; | |
665 | ||
d62a17ae | 666 | /* |
667 | * Make sure we update the FPM any time we send new information to | |
668 | * the kernel. | |
669 | */ | |
670 | hook_call(rib_update, rn, "installing in kernel"); | |
97f5b441 MS |
671 | |
672 | /* Send add or update */ | |
8263d1d0 | 673 | if (old) |
97f5b441 | 674 | ret = dplane_route_update(rn, re, old); |
5709131c | 675 | else |
97f5b441 | 676 | ret = dplane_route_add(rn, re); |
97f5b441 MS |
677 | |
678 | switch (ret) { | |
ea1c14f6 | 679 | case ZEBRA_DPLANE_REQUEST_QUEUED: |
60f98b23 | 680 | SET_FLAG(re->status, ROUTE_ENTRY_QUEUED); |
efe6c026 MS |
681 | |
682 | if (old) { | |
60f98b23 | 683 | SET_FLAG(old->status, ROUTE_ENTRY_QUEUED); |
8d4665aa | 684 | SET_FLAG(re->status, ROUTE_ENTRY_ROUTE_REPLACING); |
efe6c026 MS |
685 | |
686 | /* Free old FIB nexthop group */ | |
7483dcbe | 687 | UNSET_FLAG(old->status, ROUTE_ENTRY_USE_FIB_NHG); |
efe6c026 MS |
688 | if (old->fib_ng.nexthop) { |
689 | nexthops_free(old->fib_ng.nexthop); | |
690 | old->fib_ng.nexthop = NULL; | |
691 | } | |
efe6c026 MS |
692 | } |
693 | ||
97f5b441 MS |
694 | if (zvrf) |
695 | zvrf->installs_queued++; | |
1e885672 | 696 | break; |
ea1c14f6 | 697 | case ZEBRA_DPLANE_REQUEST_FAILURE: |
97f5b441 | 698 | { |
97f5b441 | 699 | flog_err(EC_ZEBRA_DP_INSTALL_FAIL, |
e658173a DS |
700 | "%u:%u:%pRN: Failed to enqueue dataplane install", |
701 | re->vrf_id, re->table, rn); | |
1e885672 | 702 | break; |
97f5b441 | 703 | } |
ea1c14f6 | 704 | case ZEBRA_DPLANE_REQUEST_SUCCESS: |
97f5b441 MS |
705 | if (zvrf) |
706 | zvrf->installs++; | |
1e885672 DS |
707 | break; |
708 | } | |
d62a17ae | 709 | |
0c555cc6 | 710 | return; |
718e3744 | 711 | } |
712 | ||
713 | /* Uninstall the route from kernel. */ | |
0c555cc6 | 714 | void rib_uninstall_kernel(struct route_node *rn, struct route_entry *re) |
718e3744 | 715 | { |
d62a17ae | 716 | struct nexthop *nexthop; |
630d5962 | 717 | struct rib_table_info *info = srcdest_rnode_table_info(rn); |
d62a17ae | 718 | struct zebra_vrf *zvrf = vrf_info_lookup(re->vrf_id); |
05737783 | 719 | |
d62a17ae | 720 | if (info->safi != SAFI_UNICAST) { |
677c1dd5 | 721 | UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED); |
c415d895 | 722 | for (ALL_NEXTHOPS(re->nhe->nhg, nexthop)) |
d6792f9d | 723 | UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB); |
0c555cc6 | 724 | return; |
d62a17ae | 725 | } |
416ec78d | 726 | |
d62a17ae | 727 | /* |
728 | * Make sure we update the FPM any time we send new information to | |
97f5b441 | 729 | * the dataplane. |
d62a17ae | 730 | */ |
731 | hook_call(rib_update, rn, "uninstalling from kernel"); | |
97f5b441 MS |
732 | |
733 | switch (dplane_route_delete(rn, re)) { | |
ea1c14f6 | 734 | case ZEBRA_DPLANE_REQUEST_QUEUED: |
97f5b441 MS |
735 | if (zvrf) |
736 | zvrf->removals_queued++; | |
1e885672 | 737 | break; |
ea1c14f6 | 738 | case ZEBRA_DPLANE_REQUEST_FAILURE: |
97f5b441 | 739 | flog_err(EC_ZEBRA_DP_INSTALL_FAIL, |
e658173a DS |
740 | "%u:%pRN: Failed to enqueue dataplane uninstall", |
741 | re->vrf_id, rn); | |
1e885672 | 742 | break; |
ea1c14f6 | 743 | case ZEBRA_DPLANE_REQUEST_SUCCESS: |
1e885672 DS |
744 | if (zvrf) |
745 | zvrf->removals++; | |
746 | break; | |
747 | } | |
718e3744 | 748 | |
0c555cc6 | 749 | return; |
718e3744 | 750 | } |
751 | ||
9fd92e3c AS |
752 | /* |
753 | * rib_can_delete_dest | |
754 | * | |
2951a7a4 | 755 | * Returns true if the given dest can be deleted from the table. |
9fd92e3c | 756 | */ |
d62a17ae | 757 | static int rib_can_delete_dest(rib_dest_t *dest) |
9fd92e3c | 758 | { |
045207e2 | 759 | if (re_list_first(&dest->routes)) { |
d62a17ae | 760 | return 0; |
761 | } | |
9fd92e3c | 762 | |
c86ba6c2 DS |
763 | /* |
764 | * Unresolved rnh's are stored on the default route's list | |
765 | * | |
766 | * dest->rnode can also be the source prefix node in an | |
767 | * ipv6 sourcedest table. Fortunately the prefix of a | |
768 | * source prefix node can never be the default prefix. | |
769 | */ | |
770 | if (is_default_prefix(&dest->rnode->p)) | |
771 | return 0; | |
772 | ||
d62a17ae | 773 | /* |
774 | * Don't delete the dest if we have to update the FPM about this | |
775 | * prefix. | |
776 | */ | |
777 | if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_FPM) | |
778 | || CHECK_FLAG(dest->flags, RIB_DEST_SENT_TO_FPM)) | |
779 | return 0; | |
5adc2528 | 780 | |
d62a17ae | 781 | return 1; |
9fd92e3c AS |
782 | } |
783 | ||
f99f1ff5 SP |
784 | void zebra_rib_evaluate_rn_nexthops(struct route_node *rn, uint32_t seq, |
785 | bool rt_delete) | |
699dae23 DS |
786 | { |
787 | rib_dest_t *dest = rib_dest_from_rnode(rn); | |
699dae23 DS |
788 | struct rnh *rnh; |
789 | ||
790 | /* | |
50872b08 | 791 | * We are storing the rnh's associated withb |
699dae23 DS |
792 | * the tracked nexthop as a list of the rn's. |
793 | * Unresolved rnh's are placed at the top | |
794 | * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 ) | |
795 | * As such for each rn we need to walk up the tree | |
796 | * and see if any rnh's need to see if they | |
797 | * would match a more specific route | |
798 | */ | |
799 | while (rn) { | |
e658173a | 800 | if (IS_ZEBRA_DEBUG_NHT_DETAILED) |
15569c58 | 801 | zlog_debug( |
e658173a DS |
802 | "%s: %pRN Being examined for Nexthop Tracking Count: %zd", |
803 | __func__, rn, | |
15569c58 | 804 | dest ? rnh_list_count(&dest->nht) : 0); |
e658173a | 805 | |
f99f1ff5 SP |
806 | if (rt_delete && (!dest || !rnh_list_count(&dest->nht))) { |
807 | if (IS_ZEBRA_DEBUG_NHT_DETAILED) | |
808 | zlog_debug("%pRN has no tracking NHTs. Bailing", | |
809 | rn); | |
810 | break; | |
811 | } | |
699dae23 DS |
812 | if (!dest) { |
813 | rn = rn->parent; | |
814 | if (rn) | |
815 | dest = rib_dest_from_rnode(rn); | |
816 | continue; | |
817 | } | |
818 | /* | |
819 | * If we have any rnh's stored in the nht list | |
820 | * then we know that this route node was used for | |
821 | * nht resolution and as such we need to call the | |
822 | * nexthop tracking evaluation code | |
823 | */ | |
739c9c90 | 824 | frr_each_safe(rnh_list, &dest->nht, rnh) { |
699dae23 DS |
825 | struct zebra_vrf *zvrf = |
826 | zebra_vrf_lookup_by_id(rnh->vrf_id); | |
827 | struct prefix *p = &rnh->node->p; | |
828 | ||
e658173a | 829 | if (IS_ZEBRA_DEBUG_NHT_DETAILED) |
bd47f3a3 | 830 | zlog_debug( |
5ef3568f DS |
831 | "%s(%u):%pRN has Nexthop(%pRN) depending on it, evaluating %u:%u", |
832 | zvrf_name(zvrf), zvrf_id(zvrf), rn, | |
833 | rnh->node, seq, rnh->seqno); | |
699dae23 DS |
834 | |
835 | /* | |
836 | * If we have evaluated this node on this pass | |
837 | * already, due to following the tree up | |
838 | * then we know that we can move onto the next | |
839 | * rnh to process. | |
840 | * | |
841 | * Additionally we call zebra_evaluate_rnh | |
842 | * when we gc the dest. In this case we know | |
843 | * that there must be no other re's where | |
844 | * we were originally as such we know that | |
845 | * that sequence number is ok to respect. | |
846 | */ | |
50872b08 DS |
847 | if (rnh->seqno == seq) { |
848 | if (IS_ZEBRA_DEBUG_NHT_DETAILED) | |
849 | zlog_debug( | |
1d5453d6 | 850 | " Node processed and moved already"); |
699dae23 | 851 | continue; |
50872b08 | 852 | } |
699dae23 DS |
853 | |
854 | rnh->seqno = seq; | |
d597533a DS |
855 | zebra_evaluate_rnh(zvrf, family2afi(p->family), 0, p, |
856 | rnh->safi); | |
699dae23 DS |
857 | } |
858 | ||
859 | rn = rn->parent; | |
860 | if (rn) | |
861 | dest = rib_dest_from_rnode(rn); | |
862 | } | |
863 | } | |
864 | ||
9fd92e3c AS |
865 | /* |
866 | * rib_gc_dest | |
867 | * | |
868 | * Garbage collect the rib dest corresponding to the given route node | |
869 | * if appropriate. | |
870 | * | |
2951a7a4 | 871 | * Returns true if the dest was deleted, false otherwise. |
9fd92e3c | 872 | */ |
d62a17ae | 873 | int rib_gc_dest(struct route_node *rn) |
9fd92e3c | 874 | { |
d62a17ae | 875 | rib_dest_t *dest; |
9fd92e3c | 876 | |
d62a17ae | 877 | dest = rib_dest_from_rnode(rn); |
878 | if (!dest) | |
879 | return 0; | |
9fd92e3c | 880 | |
d62a17ae | 881 | if (!rib_can_delete_dest(dest)) |
882 | return 0; | |
9fd92e3c | 883 | |
c9abf558 DS |
884 | if (IS_ZEBRA_DEBUG_RIB) { |
885 | struct zebra_vrf *zvrf; | |
886 | ||
887 | zvrf = rib_dest_vrf(dest); | |
d62a17ae | 888 | rnode_debug(rn, zvrf_id(zvrf), "removing dest from table"); |
c9abf558 | 889 | } |
9fd92e3c | 890 | |
f99f1ff5 SP |
891 | zebra_rib_evaluate_rn_nexthops(rn, zebra_router_get_next_sequence(), |
892 | true); | |
699dae23 | 893 | |
d62a17ae | 894 | dest->rnode = NULL; |
aa57abfb | 895 | rnh_list_fini(&dest->nht); |
d62a17ae | 896 | XFREE(MTYPE_RIB_DEST, dest); |
897 | rn->info = NULL; | |
9fd92e3c | 898 | |
d62a17ae | 899 | /* |
900 | * Release the one reference that we keep on the route node. | |
901 | */ | |
902 | route_unlock_node(rn); | |
903 | return 1; | |
9fd92e3c AS |
904 | } |
905 | ||
224ccf29 DL |
906 | void zebra_rtable_node_cleanup(struct route_table *table, |
907 | struct route_node *node) | |
908 | { | |
909 | struct route_entry *re, *next; | |
910 | ||
911 | RNODE_FOREACH_RE_SAFE (node, re, next) { | |
912 | rib_unlink(node, re); | |
913 | } | |
914 | ||
915 | if (node->info) { | |
916 | rib_dest_t *dest = node->info; | |
917 | ||
0d0f516c | 918 | /* Remove from update queue of FPM module */ |
919 | hook_call(rib_shutdown, node); | |
920 | ||
224ccf29 DL |
921 | rnh_list_fini(&dest->nht); |
922 | XFREE(MTYPE_RIB_DEST, node->info); | |
923 | } | |
924 | } | |
925 | ||
d62a17ae | 926 | static void rib_process_add_fib(struct zebra_vrf *zvrf, struct route_node *rn, |
927 | struct route_entry *new) | |
3e5c6e00 | 928 | { |
d62a17ae | 929 | hook_call(rib_update, rn, "new route selected"); |
3e5c6e00 | 930 | |
d62a17ae | 931 | /* Update real nexthop. This may actually determine if nexthop is active |
932 | * or not. */ | |
c415d895 | 933 | if (!nexthop_group_active_nexthop_num(&(new->nhe->nhg))) { |
d62a17ae | 934 | UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED); |
935 | return; | |
936 | } | |
3e5c6e00 | 937 | |
e658173a DS |
938 | if (IS_ZEBRA_DEBUG_RIB) |
939 | zlog_debug("%s(%u:%u):%pRN: Adding route rn %p, re %p (%s)", | |
940 | zvrf_name(zvrf), zvrf_id(zvrf), new->table, rn, rn, | |
c2c02b76 | 941 | new, zebra_route_string(new->type)); |
3e5c6e00 | 942 | |
d62a17ae | 943 | /* If labeled-unicast route, install transit LSP. */ |
944 | if (zebra_rib_labeled_unicast(new)) | |
945 | zebra_mpls_lsp_install(zvrf, rn, new); | |
a64448ba | 946 | |
3cdba47a | 947 | rib_install_kernel(rn, new, NULL); |
3e5c6e00 | 948 | |
d62a17ae | 949 | UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED); |
3e5c6e00 | 950 | } |
951 | ||
d62a17ae | 952 | static void rib_process_del_fib(struct zebra_vrf *zvrf, struct route_node *rn, |
953 | struct route_entry *old) | |
3e5c6e00 | 954 | { |
d62a17ae | 955 | hook_call(rib_update, rn, "removing existing route"); |
3e5c6e00 | 956 | |
d62a17ae | 957 | /* Uninstall from kernel. */ |
e658173a DS |
958 | if (IS_ZEBRA_DEBUG_RIB) |
959 | zlog_debug("%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s)", | |
960 | zvrf_name(zvrf), zvrf_id(zvrf), old->table, rn, rn, | |
c2c02b76 | 961 | old, zebra_route_string(old->type)); |
3e5c6e00 | 962 | |
d62a17ae | 963 | /* If labeled-unicast route, uninstall transit LSP. */ |
964 | if (zebra_rib_labeled_unicast(old)) | |
965 | zebra_mpls_lsp_uninstall(zvrf, rn, old); | |
966 | ||
3cdba47a | 967 | rib_uninstall_kernel(rn, old); |
d62a17ae | 968 | |
969 | /* Update nexthop for route, reset changed flag. */ | |
ef57f35f DL |
970 | /* Note: this code also handles the Linux case when an interface goes |
971 | * down, causing the kernel to delete routes without sending DELROUTE | |
972 | * notifications | |
973 | */ | |
6883bf8d | 974 | if (RIB_KERNEL_ROUTE(old)) |
212df1de PG |
975 | SET_FLAG(old->status, ROUTE_ENTRY_REMOVED); |
976 | else | |
977 | UNSET_FLAG(old->status, ROUTE_ENTRY_CHANGED); | |
d62a17ae | 978 | } |
979 | ||
980 | static void rib_process_update_fib(struct zebra_vrf *zvrf, | |
981 | struct route_node *rn, | |
982 | struct route_entry *old, | |
983 | struct route_entry *new) | |
984 | { | |
d62a17ae | 985 | int nh_active = 0; |
d62a17ae | 986 | |
987 | /* | |
988 | * We have to install or update if a new route has been selected or | |
989 | * something has changed. | |
990 | */ | |
991 | if (new != old || CHECK_FLAG(new->status, ROUTE_ENTRY_CHANGED)) { | |
992 | hook_call(rib_update, rn, "updating existing route"); | |
993 | ||
994 | /* Update the nexthop; we could determine here that nexthop is | |
995 | * inactive. */ | |
c415d895 | 996 | if (nexthop_group_active_nexthop_num(&(new->nhe->nhg))) |
d62a17ae | 997 | nh_active = 1; |
998 | ||
999 | /* If nexthop is active, install the selected route, if | |
1000 | * appropriate. If | |
1001 | * the install succeeds, cleanup flags for prior route, if | |
1002 | * different from | |
1003 | * newly selected. | |
1004 | */ | |
1005 | if (nh_active) { | |
1006 | if (IS_ZEBRA_DEBUG_RIB) { | |
d62a17ae | 1007 | if (new != old) |
1008 | zlog_debug( | |
e658173a | 1009 | "%s(%u:%u):%pRN: Updating route rn %p, re %p (%s) old %p (%s)", |
bd47f3a3 | 1010 | zvrf_name(zvrf), zvrf_id(zvrf), |
e658173a | 1011 | new->table, rn, rn, new, |
2da33d6b DS |
1012 | zebra_route_string(new->type), |
1013 | old, | |
1014 | zebra_route_string(old->type)); | |
d62a17ae | 1015 | else |
1016 | zlog_debug( | |
e658173a | 1017 | "%s(%u:%u):%pRN: Updating route rn %p, re %p (%s)", |
bd47f3a3 | 1018 | zvrf_name(zvrf), zvrf_id(zvrf), |
e658173a | 1019 | new->table, rn, rn, new, |
2da33d6b | 1020 | zebra_route_string(new->type)); |
d62a17ae | 1021 | } |
1022 | ||
1023 | /* If labeled-unicast route, uninstall transit LSP. */ | |
1024 | if (zebra_rib_labeled_unicast(old)) | |
1025 | zebra_mpls_lsp_uninstall(zvrf, rn, old); | |
1026 | ||
3cdba47a DS |
1027 | /* |
1028 | * Non-system route should be installed. | |
1029 | * If labeled-unicast route, install transit | |
1030 | * LSP. | |
1031 | */ | |
1032 | if (zebra_rib_labeled_unicast(new)) | |
1033 | zebra_mpls_lsp_install(zvrf, rn, new); | |
d62a17ae | 1034 | |
3cdba47a | 1035 | rib_install_kernel(rn, new, old); |
d62a17ae | 1036 | } |
a64448ba | 1037 | |
d62a17ae | 1038 | /* |
1039 | * If nexthop for selected route is not active or install | |
1040 | * failed, we | |
1041 | * may need to uninstall and delete for redistribution. | |
1042 | */ | |
ed216282 | 1043 | if (!nh_active) { |
d62a17ae | 1044 | if (IS_ZEBRA_DEBUG_RIB) { |
d62a17ae | 1045 | if (new != old) |
1046 | zlog_debug( | |
e658173a | 1047 | "%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive", |
bd47f3a3 | 1048 | zvrf_name(zvrf), zvrf_id(zvrf), |
e658173a | 1049 | new->table, rn, rn, new, |
2da33d6b DS |
1050 | zebra_route_string(new->type), |
1051 | old, | |
1052 | zebra_route_string(old->type)); | |
d62a17ae | 1053 | else |
1054 | zlog_debug( | |
e658173a | 1055 | "%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s) - nexthop inactive", |
bd47f3a3 | 1056 | zvrf_name(zvrf), zvrf_id(zvrf), |
e658173a | 1057 | new->table, rn, rn, new, |
2da33d6b | 1058 | zebra_route_string(new->type)); |
d62a17ae | 1059 | } |
1060 | ||
5c18e662 DS |
1061 | /* |
1062 | * When we have gotten to this point | |
1063 | * the new route entry has no nexthops | |
1064 | * that are usable and as such we need | |
1065 | * to remove the old route, but only | |
1066 | * if we were the one who installed | |
1067 | * the old route | |
1068 | */ | |
1069 | if (!RIB_SYSTEM_ROUTE(old)) { | |
1070 | /* If labeled-unicast route, uninstall transit | |
1071 | * LSP. */ | |
1072 | if (zebra_rib_labeled_unicast(old)) | |
1073 | zebra_mpls_lsp_uninstall(zvrf, rn, old); | |
d62a17ae | 1074 | |
5c18e662 DS |
1075 | rib_uninstall_kernel(rn, old); |
1076 | } | |
d62a17ae | 1077 | } |
1078 | } else { | |
1079 | /* | |
1080 | * Same route selected; check if in the FIB and if not, | |
3cdba47a DS |
1081 | * re-install. This is housekeeping code to deal with |
1082 | * race conditions in kernel with linux netlink reporting | |
1083 | * interface up before IPv4 or IPv6 protocol is ready | |
d62a17ae | 1084 | * to add routes. |
1085 | */ | |
3cdba47a DS |
1086 | if (!CHECK_FLAG(new->status, ROUTE_ENTRY_INSTALLED) || |
1087 | RIB_SYSTEM_ROUTE(new)) | |
677c1dd5 | 1088 | rib_install_kernel(rn, new, NULL); |
d62a17ae | 1089 | } |
1090 | ||
1091 | /* Update prior route. */ | |
6883bf8d | 1092 | if (new != old) |
d62a17ae | 1093 | UNSET_FLAG(old->status, ROUTE_ENTRY_CHANGED); |
3e5c6e00 | 1094 | |
d62a17ae | 1095 | /* Clear changed flag. */ |
1096 | UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED); | |
3e5c6e00 | 1097 | } |
1098 | ||
d62a17ae | 1099 | /* Check if 'alternate' RIB entry is better than 'current'. */ |
1100 | static struct route_entry *rib_choose_best(struct route_entry *current, | |
1101 | struct route_entry *alternate) | |
1102 | { | |
1103 | if (current == NULL) | |
1104 | return alternate; | |
1105 | ||
1106 | /* filter route selection in following order: | |
1107 | * - connected beats other types | |
fec4ca19 | 1108 | * - if both connected, loopback or vrf wins |
d62a17ae | 1109 | * - lower distance beats higher |
1110 | * - lower metric beats higher for equal distance | |
1111 | * - last, hence oldest, route wins tie break. | |
1112 | */ | |
1113 | ||
fec4ca19 DS |
1114 | /* Connected routes. Check to see if either are a vrf |
1115 | * or loopback interface. If not, pick the last connected | |
d62a17ae | 1116 | * route of the set of lowest metric connected routes. |
1117 | */ | |
1118 | if (alternate->type == ZEBRA_ROUTE_CONNECT) { | |
fec4ca19 DS |
1119 | if (current->type != ZEBRA_ROUTE_CONNECT) |
1120 | return alternate; | |
1121 | ||
1122 | /* both are connected. are either loop or vrf? */ | |
1123 | struct nexthop *nexthop = NULL; | |
1124 | ||
c415d895 | 1125 | for (ALL_NEXTHOPS(alternate->nhe->nhg, nexthop)) { |
873fde8c | 1126 | struct interface *ifp = if_lookup_by_index( |
1127 | nexthop->ifindex, alternate->vrf_id); | |
1128 | ||
608c8870 | 1129 | if (ifp && if_is_loopback(ifp)) |
fec4ca19 DS |
1130 | return alternate; |
1131 | } | |
1132 | ||
c415d895 | 1133 | for (ALL_NEXTHOPS(current->nhe->nhg, nexthop)) { |
873fde8c | 1134 | struct interface *ifp = if_lookup_by_index( |
1135 | nexthop->ifindex, current->vrf_id); | |
1136 | ||
608c8870 | 1137 | if (ifp && if_is_loopback(ifp)) |
fec4ca19 DS |
1138 | return current; |
1139 | } | |
1140 | ||
1141 | /* Neither are loop or vrf so pick best metric */ | |
1142 | if (alternate->metric <= current->metric) | |
d62a17ae | 1143 | return alternate; |
1144 | ||
1145 | return current; | |
1146 | } | |
3e5c6e00 | 1147 | |
d62a17ae | 1148 | if (current->type == ZEBRA_ROUTE_CONNECT) |
1149 | return current; | |
3e5c6e00 | 1150 | |
d62a17ae | 1151 | /* higher distance loses */ |
1152 | if (alternate->distance < current->distance) | |
1153 | return alternate; | |
1154 | if (current->distance < alternate->distance) | |
1155 | return current; | |
3e5c6e00 | 1156 | |
d62a17ae | 1157 | /* metric tie-breaks equal distance */ |
1158 | if (alternate->metric <= current->metric) | |
1159 | return alternate; | |
3e5c6e00 | 1160 | |
d62a17ae | 1161 | return current; |
3e5c6e00 | 1162 | } |
1163 | ||
d62a17ae | 1164 | /* Core function for processing routing information base. */ |
1165 | static void rib_process(struct route_node *rn) | |
1166 | { | |
1167 | struct route_entry *re; | |
1168 | struct route_entry *next; | |
1169 | struct route_entry *old_selected = NULL; | |
1170 | struct route_entry *new_selected = NULL; | |
1171 | struct route_entry *old_fib = NULL; | |
1172 | struct route_entry *new_fib = NULL; | |
1173 | struct route_entry *best = NULL; | |
d62a17ae | 1174 | rib_dest_t *dest; |
1175 | struct zebra_vrf *zvrf = NULL; | |
bd47f3a3 | 1176 | struct vrf *vrf; |
86391e56 | 1177 | |
d62a17ae | 1178 | vrf_id_t vrf_id = VRF_UNKNOWN; |
1179 | ||
1180 | assert(rn); | |
1181 | ||
1182 | dest = rib_dest_from_rnode(rn); | |
f0afc61d DS |
1183 | /* |
1184 | * We have an enqueued node with nothing to process here | |
1185 | * let's just finish up and return; | |
1186 | */ | |
1187 | if (!dest) | |
1188 | return; | |
bab85d4f | 1189 | |
f0afc61d DS |
1190 | zvrf = rib_dest_vrf(dest); |
1191 | vrf_id = zvrf_id(zvrf); | |
bd47f3a3 | 1192 | |
bd47f3a3 | 1193 | vrf = vrf_lookup_by_id(vrf_id); |
bab85d4f | 1194 | |
607425e5 DS |
1195 | /* |
1196 | * we can have rn's that have a NULL info pointer | |
1197 | * (dest). As such let's not let the deref happen | |
1198 | * additionally we know RNODE_FOREACH_RE_SAFE | |
1199 | * will not iterate so we are ok. | |
1200 | */ | |
f0afc61d DS |
1201 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) { |
1202 | struct route_entry *re = re_list_first(&dest->routes); | |
f5158712 | 1203 | |
e658173a DS |
1204 | zlog_debug("%s(%u:%u):%pRN: Processing rn %p", |
1205 | VRF_LOGNAME(vrf), vrf_id, re->table, rn, | |
f0afc61d | 1206 | rn); |
f5158712 | 1207 | } |
5f7a4718 | 1208 | |
f0afc61d DS |
1209 | old_fib = dest->selected_fib; |
1210 | ||
a2addae8 | 1211 | RNODE_FOREACH_RE_SAFE (rn, re, next) { |
61e6de9d DS |
1212 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) { |
1213 | char flags_buf[128]; | |
1214 | char status_buf[128]; | |
1215 | ||
d62a17ae | 1216 | zlog_debug( |
e658173a DS |
1217 | "%s(%u:%u):%pRN: Examine re %p (%s) status: %sflags: %sdist %d metric %d", |
1218 | VRF_LOGNAME(vrf), vrf_id, re->table, rn, re, | |
61e6de9d DS |
1219 | zebra_route_string(re->type), |
1220 | _dump_re_status(re, status_buf, | |
1221 | sizeof(status_buf)), | |
1222 | zclient_dump_route_flags(re->flags, flags_buf, | |
1223 | sizeof(flags_buf)), | |
1224 | re->distance, re->metric); | |
1225 | } | |
d62a17ae | 1226 | |
d62a17ae | 1227 | /* Currently selected re. */ |
1228 | if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)) { | |
1229 | assert(old_selected == NULL); | |
1230 | old_selected = re; | |
1231 | } | |
bab85d4f | 1232 | |
d62a17ae | 1233 | /* Skip deleted entries from selection */ |
1234 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) | |
1235 | continue; | |
1236 | ||
9d221fac DS |
1237 | /* |
1238 | * If the route entry has changed, verify/resolve | |
1239 | * the nexthops associated with the entry. | |
1240 | * | |
1241 | * In any event if we have nexthops that are not active | |
1242 | * then we cannot use this particular route entry so | |
1243 | * skip it. | |
d62a17ae | 1244 | */ |
9d221fac DS |
1245 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED)) { |
1246 | if (!nexthop_active_update(rn, re)) { | |
7e010c4b DS |
1247 | const struct prefix *p; |
1248 | struct rib_table_info *info; | |
1249 | ||
9d221fac DS |
1250 | if (re->type == ZEBRA_ROUTE_TABLE) { |
1251 | /* XXX: HERE BE DRAGONS!!!!! | |
1252 | * In all honesty, I have not yet | |
1253 | * figured out what this part does or | |
1254 | * why the ROUTE_ENTRY_CHANGED test | |
1255 | * above is correct or why we need to | |
1256 | * delete a route here, and also not | |
1257 | * whether this concerns both selected | |
1258 | * and fib route, or only selected | |
1259 | * or only fib | |
1260 | * | |
1261 | * This entry was denied by the 'ip | |
1262 | * protocol | |
1263 | * table' route-map, we need to delete | |
1264 | * it */ | |
1265 | if (re != old_selected) { | |
1266 | if (IS_ZEBRA_DEBUG_RIB) | |
1267 | zlog_debug( | |
e658173a | 1268 | "%s: %s(%u):%pRN: imported via import-table but denied by the ip protocol table route-map", |
9d221fac DS |
1269 | __func__, |
1270 | VRF_LOGNAME( | |
1271 | vrf), | |
e658173a | 1272 | vrf_id, rn); |
9d221fac | 1273 | rib_unlink(rn, re); |
e96817f8 | 1274 | continue; |
9d221fac DS |
1275 | } else |
1276 | SET_FLAG(re->status, | |
1277 | ROUTE_ENTRY_REMOVED); | |
1278 | } | |
d62a17ae | 1279 | |
7e010c4b DS |
1280 | info = srcdest_rnode_table_info(rn); |
1281 | srcdest_rnode_prefixes(rn, &p, NULL); | |
a7704e1b DS |
1282 | zsend_route_notify_owner( |
1283 | rn, re, ZAPI_ROUTE_FAIL_INSTALL, | |
1284 | info->afi, info->safi); | |
9d221fac DS |
1285 | continue; |
1286 | } | |
1287 | } else { | |
1288 | /* | |
1289 | * If the re has not changed and the nhg we have is | |
1290 | * not usable, then we cannot use this route entry | |
1291 | * for consideration, as that the route will just | |
1292 | * not install if it is selected. | |
1293 | */ | |
1294 | if (!nexthop_group_active_nexthop_num(&re->nhe->nhg)) | |
1295 | continue; | |
d62a17ae | 1296 | } |
bab85d4f | 1297 | |
d62a17ae | 1298 | /* Infinite distance. */ |
3332f4f0 DS |
1299 | if (re->distance == DISTANCE_INFINITY && |
1300 | re->type != ZEBRA_ROUTE_KERNEL) { | |
d62a17ae | 1301 | UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED); |
1302 | continue; | |
1303 | } | |
bab85d4f | 1304 | |
d62a17ae | 1305 | if (CHECK_FLAG(re->flags, ZEBRA_FLAG_FIB_OVERRIDE)) { |
1306 | best = rib_choose_best(new_fib, re); | |
1307 | if (new_fib && best != new_fib) | |
1308 | UNSET_FLAG(new_fib->status, | |
1309 | ROUTE_ENTRY_CHANGED); | |
1310 | new_fib = best; | |
1311 | } else { | |
1312 | best = rib_choose_best(new_selected, re); | |
1313 | if (new_selected && best != new_selected) | |
1314 | UNSET_FLAG(new_selected->status, | |
1315 | ROUTE_ENTRY_CHANGED); | |
1316 | new_selected = best; | |
1317 | } | |
1318 | if (best != re) | |
1319 | UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED); | |
1320 | } /* RNODE_FOREACH_RE */ | |
1321 | ||
1322 | /* If no FIB override route, use the selected route also for FIB */ | |
1323 | if (new_fib == NULL) | |
1324 | new_fib = new_selected; | |
1325 | ||
1326 | /* After the cycle is finished, the following pointers will be set: | |
1327 | * old_selected --- RE entry currently having SELECTED | |
1328 | * new_selected --- RE entry that is newly SELECTED | |
1329 | * old_fib --- RE entry currently in kernel FIB | |
1330 | * new_fib --- RE entry that is newly to be in kernel FIB | |
1331 | * | |
1332 | * new_selected will get SELECTED flag, and is going to be redistributed | |
1333 | * the zclients. new_fib (which can be new_selected) will be installed | |
1334 | * in kernel. | |
1335 | */ | |
1336 | ||
1337 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) { | |
c2c02b76 DS |
1338 | struct route_entry *entry; |
1339 | ||
1340 | entry = old_selected | |
1341 | ? old_selected | |
1342 | : new_selected | |
1343 | ? new_selected | |
1344 | : old_fib ? old_fib | |
1345 | : new_fib ? new_fib : NULL; | |
1346 | ||
d62a17ae | 1347 | zlog_debug( |
e658173a DS |
1348 | "%s(%u:%u):%pRN: After processing: old_selected %p new_selected %p old_fib %p new_fib %p", |
1349 | VRF_LOGNAME(vrf), vrf_id, entry ? entry->table : 0, rn, | |
c2c02b76 DS |
1350 | (void *)old_selected, (void *)new_selected, |
1351 | (void *)old_fib, (void *)new_fib); | |
d62a17ae | 1352 | } |
446bb95e | 1353 | |
d62a17ae | 1354 | /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if |
1355 | * fib == selected */ | |
9d303b37 DL |
1356 | bool selected_changed = new_selected && CHECK_FLAG(new_selected->status, |
1357 | ROUTE_ENTRY_CHANGED); | |
d62a17ae | 1358 | |
8cb41cd6 | 1359 | /* Update SELECTED entry */ |
d62a17ae | 1360 | if (old_selected != new_selected || selected_changed) { |
93bdadae | 1361 | |
6883bf8d | 1362 | if (new_selected && new_selected != new_fib) |
d62a17ae | 1363 | UNSET_FLAG(new_selected->status, ROUTE_ENTRY_CHANGED); |
41ec9222 | 1364 | |
3cdba47a | 1365 | if (new_selected) |
5af4b346 MS |
1366 | SET_FLAG(new_selected->flags, ZEBRA_FLAG_SELECTED); |
1367 | ||
d62a17ae | 1368 | if (old_selected) { |
40f321c0 MS |
1369 | /* |
1370 | * If we're removing the old entry, we should tell | |
1371 | * redist subscribers about that *if* they aren't | |
1372 | * going to see a redist for the new entry. | |
1373 | */ | |
1374 | if (!new_selected || CHECK_FLAG(old_selected->status, | |
1375 | ROUTE_ENTRY_REMOVED)) | |
b3a9fca1 | 1376 | redistribute_delete(rn, old_selected, |
40f321c0 MS |
1377 | new_selected); |
1378 | ||
d62a17ae | 1379 | if (old_selected != new_selected) |
1380 | UNSET_FLAG(old_selected->flags, | |
1381 | ZEBRA_FLAG_SELECTED); | |
f857321e | 1382 | } |
d62a17ae | 1383 | } |
3e5c6e00 | 1384 | |
f68ce976 DL |
1385 | /* Update fib according to selection results */ |
1386 | if (new_fib && old_fib) | |
1387 | rib_process_update_fib(zvrf, rn, old_fib, new_fib); | |
1388 | else if (new_fib) | |
1389 | rib_process_add_fib(zvrf, rn, new_fib); | |
1390 | else if (old_fib) | |
1391 | rib_process_del_fib(zvrf, rn, old_fib); | |
1392 | ||
d62a17ae | 1393 | /* Remove all RE entries queued for removal */ |
a2addae8 | 1394 | RNODE_FOREACH_RE_SAFE (rn, re, next) { |
d62a17ae | 1395 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) { |
1396 | if (IS_ZEBRA_DEBUG_RIB) { | |
1397 | rnode_debug(rn, vrf_id, "rn %p, removing re %p", | |
1398 | (void *)rn, (void *)re); | |
1399 | } | |
1400 | rib_unlink(rn, re); | |
1401 | } | |
1402 | } | |
4d38fdb4 | 1403 | |
d62a17ae | 1404 | /* |
1405 | * Check if the dest can be deleted now. | |
1406 | */ | |
1407 | rib_gc_dest(rn); | |
e96f9203 DO |
1408 | } |
1409 | ||
a1494c25 DS |
1410 | static void zebra_rib_evaluate_mpls(struct route_node *rn) |
1411 | { | |
1412 | rib_dest_t *dest = rib_dest_from_rnode(rn); | |
1413 | struct zebra_vrf *zvrf = vrf_info_lookup(VRF_DEFAULT); | |
1414 | ||
1415 | if (!dest) | |
1416 | return; | |
1417 | ||
1418 | if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_LSPS)) { | |
1419 | if (IS_ZEBRA_DEBUG_MPLS) | |
1420 | zlog_debug( | |
bd47f3a3 JU |
1421 | "%s(%u): Scheduling all LSPs upon RIB completion", |
1422 | zvrf_name(zvrf), zvrf_id(zvrf)); | |
a1494c25 DS |
1423 | zebra_mpls_lsp_schedule(zvrf); |
1424 | mpls_unmark_lsps_for_processing(rn); | |
1425 | } | |
1426 | } | |
1427 | ||
e5ac2adf MS |
1428 | /* |
1429 | * Utility to match route with dplane context data | |
1430 | */ | |
1431 | static bool rib_route_match_ctx(const struct route_entry *re, | |
25779064 MS |
1432 | const struct zebra_dplane_ctx *ctx, |
1433 | bool is_update) | |
e5ac2adf MS |
1434 | { |
1435 | bool result = false; | |
1436 | ||
1437 | if (is_update) { | |
1438 | /* | |
1439 | * In 'update' case, we test info about the 'previous' or | |
1440 | * 'old' route | |
1441 | */ | |
1442 | if ((re->type == dplane_ctx_get_old_type(ctx)) && | |
1443 | (re->instance == dplane_ctx_get_old_instance(ctx))) { | |
1444 | result = true; | |
1445 | ||
9db35a5e MS |
1446 | /* We use an extra test for statics, and another for |
1447 | * kernel routes. | |
e5ac2adf MS |
1448 | */ |
1449 | if (re->type == ZEBRA_ROUTE_STATIC && | |
1450 | (re->distance != dplane_ctx_get_old_distance(ctx) || | |
1451 | re->tag != dplane_ctx_get_old_tag(ctx))) { | |
1452 | result = false; | |
9db35a5e MS |
1453 | } else if (re->type == ZEBRA_ROUTE_KERNEL && |
1454 | re->metric != | |
1455 | dplane_ctx_get_old_metric(ctx)) { | |
1456 | result = false; | |
e5ac2adf MS |
1457 | } |
1458 | } | |
1459 | ||
1460 | } else { | |
1461 | /* | |
1462 | * Ordinary, single-route case using primary context info | |
1463 | */ | |
1464 | if ((dplane_ctx_get_op(ctx) != DPLANE_OP_ROUTE_DELETE) && | |
1465 | CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) { | |
1466 | /* Skip route that's been deleted */ | |
1467 | goto done; | |
1468 | } | |
1469 | ||
1470 | if ((re->type == dplane_ctx_get_type(ctx)) && | |
1471 | (re->instance == dplane_ctx_get_instance(ctx))) { | |
1472 | result = true; | |
1473 | ||
9db35a5e MS |
1474 | /* We use an extra test for statics, and another for |
1475 | * kernel routes. | |
e5ac2adf MS |
1476 | */ |
1477 | if (re->type == ZEBRA_ROUTE_STATIC && | |
1478 | (re->distance != dplane_ctx_get_distance(ctx) || | |
1479 | re->tag != dplane_ctx_get_tag(ctx))) { | |
1480 | result = false; | |
9db35a5e MS |
1481 | } else if (re->type == ZEBRA_ROUTE_KERNEL && |
1482 | re->metric != dplane_ctx_get_metric(ctx)) { | |
1483 | result = false; | |
1484 | } else if (re->type == ZEBRA_ROUTE_CONNECT) { | |
1485 | result = nexthop_group_equal_no_recurse( | |
1486 | &re->nhe->nhg, dplane_ctx_get_ng(ctx)); | |
e5ac2adf MS |
1487 | } |
1488 | } | |
1489 | } | |
1490 | ||
1491 | done: | |
871a16cd | 1492 | return result; |
e5ac2adf MS |
1493 | } |
1494 | ||
3cdba47a DS |
1495 | static void zebra_rib_fixup_system(struct route_node *rn) |
1496 | { | |
1497 | struct route_entry *re; | |
1498 | ||
1499 | RNODE_FOREACH_RE(rn, re) { | |
1500 | struct nexthop *nhop; | |
1501 | ||
1502 | if (!RIB_SYSTEM_ROUTE(re)) | |
1503 | continue; | |
1504 | ||
1505 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) | |
1506 | continue; | |
1507 | ||
1508 | SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED); | |
6d0ee6a0 | 1509 | UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED); |
8d4665aa | 1510 | UNSET_FLAG(re->status, ROUTE_ENTRY_ROUTE_REPLACING); |
3cdba47a | 1511 | |
c415d895 | 1512 | for (ALL_NEXTHOPS(re->nhe->nhg, nhop)) { |
3cdba47a DS |
1513 | if (CHECK_FLAG(nhop->flags, NEXTHOP_FLAG_RECURSIVE)) |
1514 | continue; | |
1515 | ||
1516 | SET_FLAG(nhop->flags, NEXTHOP_FLAG_FIB); | |
1517 | } | |
1518 | } | |
1519 | } | |
1520 | ||
9db35a5e MS |
1521 | /* Route comparison logic, with various special cases. */ |
1522 | static bool rib_compare_routes(const struct route_entry *re1, | |
1523 | const struct route_entry *re2) | |
1524 | { | |
9db35a5e MS |
1525 | if (re1->type != re2->type) |
1526 | return false; | |
1527 | ||
1528 | if (re1->instance != re2->instance) | |
1529 | return false; | |
1530 | ||
1531 | if (re1->type == ZEBRA_ROUTE_KERNEL && re1->metric != re2->metric) | |
1532 | return false; | |
1533 | ||
1534 | if (CHECK_FLAG(re1->flags, ZEBRA_FLAG_RR_USE_DISTANCE) && | |
1535 | re1->distance != re2->distance) | |
1536 | return false; | |
1537 | ||
87009d7d MS |
1538 | /* We support multiple connected routes: this supports multiple |
1539 | * v6 link-locals, and we also support multiple addresses in the same | |
1540 | * subnet on a single interface. | |
1541 | */ | |
9db35a5e MS |
1542 | if (re1->type != ZEBRA_ROUTE_CONNECT) |
1543 | return true; | |
1544 | ||
87009d7d | 1545 | return false; |
9db35a5e MS |
1546 | } |
1547 | ||
9959f1da MS |
1548 | /* |
1549 | * Compare nexthop lists from a route and a dplane context; test whether | |
1550 | * the list installed in the FIB matches the route's list. | |
1551 | * Set 'changed_p' to 'true' if there were changes to the route's | |
1552 | * installed nexthops. | |
1553 | * | |
1554 | * Return 'false' if any ACTIVE route nexthops are not mentioned in the FIB | |
1555 | * list. | |
1556 | */ | |
1557 | static bool rib_update_nhg_from_ctx(struct nexthop_group *re_nhg, | |
1558 | const struct nexthop_group *ctx_nhg, | |
1559 | bool *changed_p) | |
1560 | { | |
1561 | bool matched_p = true; | |
1562 | struct nexthop *nexthop, *ctx_nexthop; | |
1563 | ||
1564 | /* Get the first `installed` one to check against. | |
1565 | * If the dataplane doesn't set these to be what was actually installed, | |
1566 | * it will just be whatever was in re->nhe->nhg? | |
1567 | */ | |
1568 | ctx_nexthop = ctx_nhg->nexthop; | |
1569 | ||
1570 | if (CHECK_FLAG(ctx_nexthop->flags, NEXTHOP_FLAG_RECURSIVE) | |
1571 | || !CHECK_FLAG(ctx_nexthop->flags, NEXTHOP_FLAG_ACTIVE)) | |
1572 | ctx_nexthop = nexthop_next_active_resolved(ctx_nexthop); | |
1573 | ||
1574 | for (ALL_NEXTHOPS_PTR(re_nhg, nexthop)) { | |
1575 | ||
1576 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) | |
1577 | continue; | |
1578 | ||
1579 | if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) | |
1580 | continue; | |
1581 | ||
1582 | /* Check for a FIB nexthop corresponding to the RIB nexthop */ | |
7483dcbe | 1583 | if (!nexthop_same(ctx_nexthop, nexthop)) { |
9959f1da MS |
1584 | /* If the FIB doesn't know about the nexthop, |
1585 | * it's not installed | |
1586 | */ | |
1587 | if (IS_ZEBRA_DEBUG_RIB_DETAILED || | |
1588 | IS_ZEBRA_DEBUG_NHG_DETAIL) { | |
1589 | zlog_debug("%s: no ctx match for rib nh %pNHv %s", | |
1590 | __func__, nexthop, | |
1591 | (CHECK_FLAG(nexthop->flags, | |
1592 | NEXTHOP_FLAG_FIB) ? | |
1593 | "(FIB)":"")); | |
1594 | } | |
1595 | matched_p = false; | |
1596 | ||
1597 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) | |
1598 | *changed_p = true; | |
1599 | ||
1600 | UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB); | |
1601 | ||
1602 | /* Keep checking nexthops */ | |
1603 | continue; | |
1604 | } | |
1605 | ||
1606 | if (CHECK_FLAG(ctx_nexthop->flags, NEXTHOP_FLAG_FIB)) { | |
1607 | if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) { | |
1608 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
1609 | zlog_debug("%s: rib nh %pNHv -> installed", | |
1610 | __func__, nexthop); | |
1611 | ||
1612 | *changed_p = true; | |
1613 | } | |
1614 | ||
1615 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB); | |
1616 | } else { | |
1617 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) { | |
1618 | if (IS_ZEBRA_DEBUG_NHG_DETAIL) | |
1619 | zlog_debug("%s: rib nh %pNHv -> uninstalled", | |
1620 | __func__, nexthop); | |
1621 | ||
1622 | *changed_p = true; | |
1623 | } | |
1624 | ||
1625 | UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB); | |
1626 | } | |
1627 | ||
1628 | ctx_nexthop = nexthop_next_active_resolved(ctx_nexthop); | |
1629 | } | |
1630 | ||
1631 | return matched_p; | |
1632 | } | |
1633 | ||
e5ac2adf | 1634 | /* |
78bf56b0 MS |
1635 | * Update a route from a dplane context. This consolidates common code |
1636 | * that can be used in processing of results from FIB updates, and in | |
1637 | * async notification processing. | |
efe6c026 | 1638 | * The return is 'true' if the installed nexthops changed; 'false' otherwise. |
e5ac2adf | 1639 | */ |
efe6c026 MS |
1640 | static bool rib_update_re_from_ctx(struct route_entry *re, |
1641 | struct route_node *rn, | |
1642 | struct zebra_dplane_ctx *ctx) | |
78bf56b0 | 1643 | { |
9959f1da | 1644 | struct nexthop *nexthop; |
941e261c MS |
1645 | bool matched; |
1646 | const struct nexthop_group *ctxnhg; | |
9959f1da | 1647 | struct nexthop_group *re_nhg; |
efe6c026 MS |
1648 | bool is_selected = false; /* Is 're' currently the selected re? */ |
1649 | bool changed_p = false; /* Change to nexthops? */ | |
1650 | rib_dest_t *dest; | |
bd47f3a3 JU |
1651 | struct vrf *vrf; |
1652 | ||
1653 | vrf = vrf_lookup_by_id(re->vrf_id); | |
78bf56b0 | 1654 | |
efe6c026 MS |
1655 | dest = rib_dest_from_rnode(rn); |
1656 | if (dest) | |
1657 | is_selected = (re == dest->selected_fib); | |
1658 | ||
1659 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
2919eea8 DS |
1660 | zlog_debug("update_from_ctx: %s(%u:%u):%pRN: %sSELECTED, re %p", |
1661 | VRF_LOGNAME(vrf), re->vrf_id, re->table, rn, | |
9db35a5e | 1662 | (is_selected ? "" : "NOT "), re); |
efe6c026 MS |
1663 | |
1664 | /* Update zebra's nexthop FIB flag for each nexthop that was installed. | |
941e261c | 1665 | * If the installed set differs from the set requested by the rib/owner, |
efe6c026 MS |
1666 | * we use the fib-specific nexthop-group to record the actual FIB |
1667 | * status. | |
78bf56b0 | 1668 | */ |
a126f120 MS |
1669 | matched = false; |
1670 | ctxnhg = dplane_ctx_get_ng(ctx); | |
941e261c | 1671 | |
9959f1da | 1672 | /* Check route's fib group and incoming notif group for equivalence. |
986a6617 SW |
1673 | * |
1674 | * Let's assume the nexthops are ordered here to save time. | |
941e261c | 1675 | */ |
9959f1da MS |
1676 | /* TODO -- this isn't testing or comparing the FIB flags; we should |
1677 | * do a more explicit loop, checking the incoming notification's flags. | |
1678 | */ | |
a126f120 MS |
1679 | if (re->fib_ng.nexthop && ctxnhg->nexthop && |
1680 | nexthop_group_equal(&re->fib_ng, ctxnhg)) | |
986a6617 | 1681 | matched = true; |
941e261c MS |
1682 | |
1683 | /* If the new FIB set matches the existing FIB set, we're done. */ | |
1684 | if (matched) { | |
1685 | if (IS_ZEBRA_DEBUG_RIB) | |
bd47f3a3 | 1686 | zlog_debug( |
2919eea8 DS |
1687 | "%s(%u:%u):%pRN update_from_ctx(): existing fib nhg, no change", |
1688 | VRF_LOGNAME(vrf), re->vrf_id, re->table, rn); | |
9959f1da | 1689 | goto check_backups; |
941e261c | 1690 | |
7483dcbe | 1691 | } else if (CHECK_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG)) { |
941e261c MS |
1692 | /* |
1693 | * Free stale fib list and move on to check the rib nhg. | |
1694 | */ | |
1695 | if (IS_ZEBRA_DEBUG_RIB) | |
bd47f3a3 | 1696 | zlog_debug( |
2919eea8 DS |
1697 | "%s(%u:%u):%pRN update_from_ctx(): replacing fib nhg", |
1698 | VRF_LOGNAME(vrf), re->vrf_id, re->table, rn); | |
941e261c MS |
1699 | nexthops_free(re->fib_ng.nexthop); |
1700 | re->fib_ng.nexthop = NULL; | |
efe6c026 | 1701 | |
7483dcbe MS |
1702 | UNSET_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG); |
1703 | ||
efe6c026 MS |
1704 | /* Note that the installed nexthops have changed */ |
1705 | changed_p = true; | |
941e261c MS |
1706 | } else { |
1707 | if (IS_ZEBRA_DEBUG_RIB) | |
2919eea8 DS |
1708 | zlog_debug( |
1709 | "%s(%u:%u):%pRN update_from_ctx(): no fib nhg", | |
1710 | VRF_LOGNAME(vrf), re->vrf_id, re->table, rn); | |
941e261c MS |
1711 | } |
1712 | ||
1713 | /* | |
1714 | * Compare with the rib nexthop group. The comparison here is different: | |
1715 | * the RIB group may be a superset of the list installed in the FIB. We | |
1716 | * walk the RIB group, looking for the 'installable' candidate | |
1717 | * nexthops, and then check those against the set | |
1718 | * that is actually installed. | |
986a6617 SW |
1719 | * |
1720 | * Assume nexthops are ordered here as well. | |
941e261c | 1721 | */ |
986a6617 | 1722 | |
9959f1da | 1723 | /* If nothing is installed, we can skip some of the checking/comparison |
1f6a5aca MS |
1724 | * of nexthops. |
1725 | */ | |
9959f1da | 1726 | if (ctxnhg->nexthop == NULL) { |
1f6a5aca MS |
1727 | changed_p = true; |
1728 | goto no_nexthops; | |
1729 | } | |
1730 | ||
9959f1da | 1731 | matched = rib_update_nhg_from_ctx(&(re->nhe->nhg), ctxnhg, &changed_p); |
78bf56b0 | 1732 | |
941e261c MS |
1733 | /* If all nexthops were processed, we're done */ |
1734 | if (matched) { | |
1735 | if (IS_ZEBRA_DEBUG_RIB) | |
bd47f3a3 | 1736 | zlog_debug( |
2919eea8 DS |
1737 | "%s(%u:%u):%pRN update_from_ctx(): rib nhg matched, changed '%s'", |
1738 | VRF_LOGNAME(vrf), re->vrf_id, re->table, rn, | |
1739 | (changed_p ? "true" : "false")); | |
9959f1da | 1740 | goto check_backups; |
941e261c | 1741 | } |
78bf56b0 | 1742 | |
1f6a5aca MS |
1743 | no_nexthops: |
1744 | ||
941e261c MS |
1745 | /* FIB nexthop set differs from the RIB set: |
1746 | * create a fib-specific nexthop-group | |
1747 | */ | |
1748 | if (IS_ZEBRA_DEBUG_RIB) | |
bd47f3a3 | 1749 | zlog_debug( |
2919eea8 DS |
1750 | "%s(%u:%u):%pRN update_from_ctx(): changed %s, adding new fib nhg%s", |
1751 | VRF_LOGNAME(vrf), re->vrf_id, re->table, rn, | |
7483dcbe MS |
1752 | (changed_p ? "true" : "false"), |
1753 | ctxnhg->nexthop != NULL ? "" : " (empty)"); | |
941e261c | 1754 | |
7483dcbe | 1755 | /* Set the flag about the dedicated fib list */ |
8ce0e517 DS |
1756 | if (zrouter.asic_notification_nexthop_control) { |
1757 | SET_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG); | |
1758 | if (ctxnhg->nexthop) | |
1759 | copy_nexthops(&(re->fib_ng.nexthop), ctxnhg->nexthop, | |
1760 | NULL); | |
1761 | } | |
941e261c | 1762 | |
9959f1da MS |
1763 | check_backups: |
1764 | ||
1765 | /* | |
1766 | * Check the status of the route's backup nexthops, if any. | |
1767 | * The logic for backups is somewhat different: if any backup is | |
1768 | * installed, a new fib nhg will be attached to the route. | |
1769 | */ | |
1770 | re_nhg = zebra_nhg_get_backup_nhg(re->nhe); | |
1771 | if (re_nhg == NULL) | |
1772 | goto done; /* No backup nexthops */ | |
1773 | ||
1774 | /* First check the route's 'fib' list of backups, if it's present | |
1775 | * from some previous event. | |
1776 | */ | |
1777 | re_nhg = &re->fib_backup_ng; | |
1778 | ctxnhg = dplane_ctx_get_backup_ng(ctx); | |
1779 | ||
1780 | matched = false; | |
1781 | if (re_nhg->nexthop && ctxnhg && nexthop_group_equal(re_nhg, ctxnhg)) | |
1782 | matched = true; | |
1783 | ||
1784 | /* If the new FIB set matches an existing FIB set, we're done. */ | |
1785 | if (matched) { | |
1786 | if (IS_ZEBRA_DEBUG_RIB) | |
1787 | zlog_debug( | |
2919eea8 DS |
1788 | "%s(%u):%pRN update_from_ctx(): existing fib backup nhg, no change", |
1789 | VRF_LOGNAME(vrf), re->vrf_id, rn); | |
9959f1da MS |
1790 | goto done; |
1791 | ||
1792 | } else if (re->fib_backup_ng.nexthop) { | |
1793 | /* | |
1794 | * Free stale fib backup list and move on to check | |
1795 | * the route's backups. | |
1796 | */ | |
1797 | if (IS_ZEBRA_DEBUG_RIB) | |
1798 | zlog_debug( | |
2919eea8 DS |
1799 | "%s(%u):%pRN update_from_ctx(): replacing fib backup nhg", |
1800 | VRF_LOGNAME(vrf), re->vrf_id, rn); | |
9959f1da MS |
1801 | nexthops_free(re->fib_backup_ng.nexthop); |
1802 | re->fib_backup_ng.nexthop = NULL; | |
1803 | ||
1804 | /* Note that the installed nexthops have changed */ | |
1805 | changed_p = true; | |
1806 | } else { | |
1807 | if (IS_ZEBRA_DEBUG_RIB) | |
2919eea8 DS |
1808 | zlog_debug( |
1809 | "%s(%u):%pRN update_from_ctx(): no fib backup nhg", | |
1810 | VRF_LOGNAME(vrf), re->vrf_id, rn); | |
9959f1da MS |
1811 | } |
1812 | ||
1813 | /* | |
7483dcbe | 1814 | * If a FIB backup nexthop set exists, attach a copy |
9959f1da MS |
1815 | * to the route if any backup is installed |
1816 | */ | |
1817 | if (ctxnhg && ctxnhg->nexthop) { | |
1818 | ||
1819 | for (ALL_NEXTHOPS_PTR(ctxnhg, nexthop)) { | |
1820 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) | |
1821 | break; | |
1822 | } | |
1823 | ||
1824 | /* If no installed backups, we're done */ | |
1825 | if (nexthop == NULL) | |
1826 | goto done; | |
1827 | ||
1828 | if (IS_ZEBRA_DEBUG_RIB) | |
2919eea8 DS |
1829 | zlog_debug( |
1830 | "%s(%u):%pRN update_from_ctx(): changed %s, adding new backup fib nhg", | |
1831 | VRF_LOGNAME(vrf), re->vrf_id, rn, | |
1832 | (changed_p ? "true" : "false")); | |
9959f1da MS |
1833 | |
1834 | copy_nexthops(&(re->fib_backup_ng.nexthop), ctxnhg->nexthop, | |
1835 | NULL); | |
1836 | } | |
1837 | ||
941e261c | 1838 | done: |
9959f1da | 1839 | |
efe6c026 | 1840 | return changed_p; |
78bf56b0 MS |
1841 | } |
1842 | ||
e5ac2adf | 1843 | /* |
fad4d69c MS |
1844 | * Helper to locate a zebra route-node from a dplane context. This is used |
1845 | * when processing dplane results, e.g. Note well: the route-node is returned | |
1846 | * with a ref held - route_unlock_node() must be called eventually. | |
e5ac2adf | 1847 | */ |
a7704e1b | 1848 | struct route_node *rib_find_rn_from_ctx(const struct zebra_dplane_ctx *ctx) |
e5ac2adf MS |
1849 | { |
1850 | struct route_table *table = NULL; | |
1851 | struct route_node *rn = NULL; | |
e5ac2adf MS |
1852 | const struct prefix *dest_pfx, *src_pfx; |
1853 | ||
1854 | /* Locate rn and re(s) from ctx */ | |
1855 | ||
c7c0b007 SW |
1856 | table = zebra_vrf_lookup_table_with_table_id( |
1857 | dplane_ctx_get_afi(ctx), dplane_ctx_get_safi(ctx), | |
1858 | dplane_ctx_get_vrf(ctx), dplane_ctx_get_table(ctx)); | |
e5ac2adf MS |
1859 | if (table == NULL) { |
1860 | if (IS_ZEBRA_DEBUG_DPLANE) { | |
bd47f3a3 JU |
1861 | zlog_debug( |
1862 | "Failed to find route for ctx: no table for afi %d, safi %d, vrf %s(%u)", | |
1863 | dplane_ctx_get_afi(ctx), | |
1864 | dplane_ctx_get_safi(ctx), | |
1865 | vrf_id_to_name(dplane_ctx_get_vrf(ctx)), | |
1866 | dplane_ctx_get_vrf(ctx)); | |
e5ac2adf MS |
1867 | } |
1868 | goto done; | |
1869 | } | |
1870 | ||
fad4d69c MS |
1871 | dest_pfx = dplane_ctx_get_dest(ctx); |
1872 | src_pfx = dplane_ctx_get_src(ctx); | |
1873 | ||
1874 | rn = srcdest_rnode_get(table, dest_pfx, | |
1875 | src_pfx ? (struct prefix_ipv6 *)src_pfx : NULL); | |
1876 | ||
1877 | done: | |
1878 | return rn; | |
1879 | } | |
1880 | ||
1881 | ||
97f5b441 | 1882 | |
fad4d69c MS |
1883 | /* |
1884 | * Route-update results processing after async dataplane update. | |
1885 | */ | |
1886 | static void rib_process_result(struct zebra_dplane_ctx *ctx) | |
1887 | { | |
1888 | struct zebra_vrf *zvrf = NULL; | |
bd47f3a3 | 1889 | struct vrf *vrf; |
fad4d69c MS |
1890 | struct route_node *rn = NULL; |
1891 | struct route_entry *re = NULL, *old_re = NULL, *rib; | |
1892 | bool is_update = false; | |
fad4d69c MS |
1893 | enum dplane_op_e op; |
1894 | enum zebra_dplane_result status; | |
fad4d69c | 1895 | uint32_t seq; |
9db35a5e | 1896 | rib_dest_t *dest; |
efe6c026 | 1897 | bool fib_changed = false; |
77b38a4a | 1898 | struct rib_table_info *info; |
f99f1ff5 | 1899 | bool rt_delete = false; |
97f5b441 | 1900 | |
fad4d69c | 1901 | zvrf = vrf_info_lookup(dplane_ctx_get_vrf(ctx)); |
bd47f3a3 | 1902 | vrf = vrf_lookup_by_id(dplane_ctx_get_vrf(ctx)); |
e5ac2adf | 1903 | |
fad4d69c MS |
1904 | /* Locate rn and re(s) from ctx */ |
1905 | rn = rib_find_rn_from_ctx(ctx); | |
e5ac2adf MS |
1906 | if (rn == NULL) { |
1907 | if (IS_ZEBRA_DEBUG_DPLANE) { | |
bd47f3a3 | 1908 | zlog_debug( |
69a2d597 DS |
1909 | "Failed to process dplane results: no route for %s(%u):%pRN", |
1910 | VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx), rn); | |
e5ac2adf MS |
1911 | } |
1912 | goto done; | |
1913 | } | |
1914 | ||
9db35a5e | 1915 | dest = rib_dest_from_rnode(rn); |
77b38a4a | 1916 | info = srcdest_rnode_table_info(rn); |
e5ac2adf MS |
1917 | |
1918 | op = dplane_ctx_get_op(ctx); | |
1919 | status = dplane_ctx_get_status(ctx); | |
1920 | ||
c831033f | 1921 | if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) |
bd47f3a3 | 1922 | zlog_debug( |
69a2d597 | 1923 | "%s(%u:%u):%pRN Processing dplane result ctx %p, op %s result %s", |
c2c02b76 | 1924 | VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx), |
69a2d597 DS |
1925 | dplane_ctx_get_table(ctx), rn, ctx, dplane_op2str(op), |
1926 | dplane_res2str(status)); | |
e5ac2adf | 1927 | |
e5ac2adf MS |
1928 | /* |
1929 | * Update is a bit of a special case, where we may have both old and new | |
1930 | * routes to post-process. | |
1931 | */ | |
1932 | is_update = dplane_ctx_is_update(ctx); | |
1933 | ||
1934 | /* | |
1935 | * Take a pass through the routes, look for matches with the context | |
1936 | * info. | |
1937 | */ | |
1938 | RNODE_FOREACH_RE(rn, rib) { | |
1939 | ||
1940 | if (re == NULL) { | |
5709131c | 1941 | if (rib_route_match_ctx(rib, ctx, false)) |
e5ac2adf | 1942 | re = rib; |
e5ac2adf MS |
1943 | } |
1944 | ||
1945 | /* Check for old route match */ | |
1946 | if (is_update && (old_re == NULL)) { | |
5709131c | 1947 | if (rib_route_match_ctx(rib, ctx, true /*is_update*/)) |
e5ac2adf | 1948 | old_re = rib; |
e5ac2adf MS |
1949 | } |
1950 | ||
1951 | /* Have we found the routes we need to work on? */ | |
5709131c | 1952 | if (re && ((!is_update || old_re))) |
e5ac2adf | 1953 | break; |
e5ac2adf MS |
1954 | } |
1955 | ||
699dae23 DS |
1956 | seq = dplane_ctx_get_seq(ctx); |
1957 | ||
e5ac2adf MS |
1958 | /* |
1959 | * Check sequence number(s) to detect stale results before continuing | |
1960 | */ | |
60f98b23 | 1961 | if (re) { |
699dae23 | 1962 | if (re->dplane_sequence != seq) { |
60f98b23 | 1963 | if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) |
bd47f3a3 | 1964 | zlog_debug( |
69a2d597 | 1965 | "%s(%u):%pRN Stale dplane result for re %p", |
bd47f3a3 | 1966 | VRF_LOGNAME(vrf), |
69a2d597 | 1967 | dplane_ctx_get_vrf(ctx), rn, re); |
e4876266 DS |
1968 | } else { |
1969 | if (!zrouter.asic_offloaded || | |
1970 | (CHECK_FLAG(re->flags, ZEBRA_FLAG_OFFLOADED) || | |
8d4665aa DS |
1971 | CHECK_FLAG(re->flags, |
1972 | ZEBRA_FLAG_OFFLOAD_FAILED))) { | |
1973 | UNSET_FLAG(re->status, | |
1974 | ROUTE_ENTRY_ROUTE_REPLACING); | |
e4876266 | 1975 | UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED); |
8d4665aa | 1976 | } |
e4876266 | 1977 | } |
e5ac2adf MS |
1978 | } |
1979 | ||
60f98b23 DS |
1980 | if (old_re) { |
1981 | if (old_re->dplane_sequence != dplane_ctx_get_old_seq(ctx)) { | |
1982 | if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) | |
bd47f3a3 | 1983 | zlog_debug( |
69a2d597 | 1984 | "%s(%u:%u):%pRN Stale dplane result for old_re %p", |
bd47f3a3 | 1985 | VRF_LOGNAME(vrf), |
c2c02b76 | 1986 | dplane_ctx_get_vrf(ctx), old_re->table, |
69a2d597 | 1987 | rn, old_re); |
60f98b23 | 1988 | } else |
9ef0c6ba | 1989 | UNSET_FLAG(old_re->status, ROUTE_ENTRY_QUEUED); |
e5ac2adf MS |
1990 | } |
1991 | ||
12e7fe3a | 1992 | switch (op) { |
12e7fe3a DS |
1993 | case DPLANE_OP_ROUTE_INSTALL: |
1994 | case DPLANE_OP_ROUTE_UPDATE: | |
1995 | if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) { | |
677c1dd5 DS |
1996 | if (re) { |
1997 | UNSET_FLAG(re->status, ROUTE_ENTRY_FAILED); | |
1998 | SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED); | |
1999 | } | |
b9f0e5ee DS |
2000 | /* |
2001 | * On an update operation from the same route type | |
2002 | * context retrieval currently has no way to know | |
2003 | * which was the old and which was the new. | |
2004 | * So don't unset our flags that we just set. | |
2005 | * We know redistribution is ok because the | |
2006 | * old_re in this case is used for nothing | |
2007 | * more than knowing whom to contact if necessary. | |
2008 | */ | |
2009 | if (old_re && old_re != re) { | |
677c1dd5 DS |
2010 | UNSET_FLAG(old_re->status, ROUTE_ENTRY_FAILED); |
2011 | UNSET_FLAG(old_re->status, | |
2012 | ROUTE_ENTRY_INSTALLED); | |
2013 | } | |
f183e380 | 2014 | |
78bf56b0 | 2015 | /* Update zebra route based on the results in |
941e261c | 2016 | * the context struct. |
78bf56b0 | 2017 | */ |
941e261c | 2018 | if (re) { |
efe6c026 MS |
2019 | fib_changed = |
2020 | rib_update_re_from_ctx(re, rn, ctx); | |
2021 | ||
2022 | if (!fib_changed) { | |
2023 | if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) | |
bd47f3a3 | 2024 | zlog_debug( |
69a2d597 | 2025 | "%s(%u:%u):%pRN no fib change for re", |
bd47f3a3 JU |
2026 | VRF_LOGNAME(vrf), |
2027 | dplane_ctx_get_vrf(ctx), | |
c2c02b76 DS |
2028 | dplane_ctx_get_table( |
2029 | ctx), | |
69a2d597 | 2030 | rn); |
12e7fe3a DS |
2031 | } |
2032 | ||
9db35a5e MS |
2033 | /* Redistribute if this is the selected re */ |
2034 | if (dest && re == dest->selected_fib) | |
ee78ed68 | 2035 | redistribute_update(rn, re, old_re); |
f183e380 MS |
2036 | } |
2037 | ||
3cdba47a DS |
2038 | /* |
2039 | * System routes are weird in that they | |
2040 | * allow multiple to be installed that match | |
2041 | * to the same prefix, so after we get the | |
2042 | * result we need to clean them up so that | |
2043 | * we can actually use them. | |
2044 | */ | |
2045 | if ((re && RIB_SYSTEM_ROUTE(re)) || | |
2046 | (old_re && RIB_SYSTEM_ROUTE(old_re))) | |
2047 | zebra_rib_fixup_system(rn); | |
2048 | ||
f4c6e2a8 | 2049 | if (zvrf) |
12e7fe3a | 2050 | zvrf->installs++; |
f183e380 | 2051 | |
12e7fe3a | 2052 | /* Notify route owner */ |
e4876266 DS |
2053 | if (zebra_router_notify_on_ack()) |
2054 | zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_INSTALLED); | |
2055 | else { | |
2056 | if (re) { | |
2057 | if (CHECK_FLAG(re->flags, | |
2058 | ZEBRA_FLAG_OFFLOADED)) | |
2059 | zsend_route_notify_owner_ctx( | |
2060 | ctx, | |
2061 | ZAPI_ROUTE_INSTALLED); | |
2062 | if (CHECK_FLAG( | |
2063 | re->flags, | |
2064 | ZEBRA_FLAG_OFFLOAD_FAILED)) | |
2065 | zsend_route_notify_owner_ctx( | |
2066 | ctx, | |
2067 | ZAPI_ROUTE_FAIL_INSTALL); | |
2068 | } | |
2069 | } | |
12e7fe3a | 2070 | } else { |
7a230a9d | 2071 | if (re) { |
677c1dd5 | 2072 | SET_FLAG(re->status, ROUTE_ENTRY_FAILED); |
7a230a9d DS |
2073 | UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED); |
2074 | } if (old_re) | |
677c1dd5 DS |
2075 | SET_FLAG(old_re->status, ROUTE_ENTRY_FAILED); |
2076 | if (re) | |
a7704e1b DS |
2077 | zsend_route_notify_owner( |
2078 | rn, re, ZAPI_ROUTE_FAIL_INSTALL, | |
2079 | info->afi, info->safi); | |
97f5b441 | 2080 | |
69a2d597 | 2081 | zlog_warn("%s(%u:%u):%pRN: Route install failed", |
bd47f3a3 | 2082 | VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx), |
69a2d597 | 2083 | dplane_ctx_get_table(ctx), rn); |
12e7fe3a DS |
2084 | } |
2085 | break; | |
2086 | case DPLANE_OP_ROUTE_DELETE: | |
f99f1ff5 | 2087 | rt_delete = true; |
677c1dd5 DS |
2088 | if (re) |
2089 | SET_FLAG(re->status, ROUTE_ENTRY_FAILED); | |
12e7fe3a DS |
2090 | /* |
2091 | * In the delete case, the zebra core datastructs were | |
2092 | * updated (or removed) at the time the delete was issued, | |
2093 | * so we're just notifying the route owner. | |
e5ac2adf | 2094 | */ |
12e7fe3a | 2095 | if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) { |
677c1dd5 DS |
2096 | if (re) { |
2097 | UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED); | |
2098 | UNSET_FLAG(re->status, ROUTE_ENTRY_FAILED); | |
2099 | } | |
12e7fe3a | 2100 | zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_REMOVED); |
e5ac2adf | 2101 | |
12e7fe3a DS |
2102 | if (zvrf) |
2103 | zvrf->removals++; | |
2104 | } else { | |
677c1dd5 DS |
2105 | if (re) |
2106 | SET_FLAG(re->status, ROUTE_ENTRY_FAILED); | |
12e7fe3a DS |
2107 | zsend_route_notify_owner_ctx(ctx, |
2108 | ZAPI_ROUTE_REMOVE_FAIL); | |
e5ac2adf | 2109 | |
69a2d597 | 2110 | zlog_warn("%s(%u:%u):%pRN: Route Deletion failure", |
bd47f3a3 | 2111 | VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx), |
69a2d597 | 2112 | dplane_ctx_get_table(ctx), rn); |
12e7fe3a | 2113 | } |
3cdba47a DS |
2114 | |
2115 | /* | |
2116 | * System routes are weird in that they | |
2117 | * allow multiple to be installed that match | |
2118 | * to the same prefix, so after we get the | |
2119 | * result we need to clean them up so that | |
2120 | * we can actually use them. | |
2121 | */ | |
2122 | if ((re && RIB_SYSTEM_ROUTE(re)) || | |
2123 | (old_re && RIB_SYSTEM_ROUTE(old_re))) | |
2124 | zebra_rib_fixup_system(rn); | |
12e7fe3a | 2125 | break; |
a98701f0 DS |
2126 | |
2127 | case DPLANE_OP_NONE: | |
2128 | case DPLANE_OP_ROUTE_NOTIFY: | |
2129 | case DPLANE_OP_NH_INSTALL: | |
2130 | case DPLANE_OP_NH_UPDATE: | |
2131 | case DPLANE_OP_NH_DELETE: | |
2132 | case DPLANE_OP_LSP_INSTALL: | |
2133 | case DPLANE_OP_LSP_UPDATE: | |
2134 | case DPLANE_OP_LSP_DELETE: | |
2135 | case DPLANE_OP_LSP_NOTIFY: | |
2136 | case DPLANE_OP_PW_INSTALL: | |
2137 | case DPLANE_OP_PW_UNINSTALL: | |
2138 | case DPLANE_OP_SYS_ROUTE_ADD: | |
2139 | case DPLANE_OP_SYS_ROUTE_DELETE: | |
2140 | case DPLANE_OP_ADDR_INSTALL: | |
2141 | case DPLANE_OP_ADDR_UNINSTALL: | |
2142 | case DPLANE_OP_MAC_INSTALL: | |
2143 | case DPLANE_OP_MAC_DELETE: | |
2144 | case DPLANE_OP_NEIGH_INSTALL: | |
2145 | case DPLANE_OP_NEIGH_UPDATE: | |
2146 | case DPLANE_OP_NEIGH_DELETE: | |
2147 | case DPLANE_OP_VTEP_ADD: | |
2148 | case DPLANE_OP_VTEP_DELETE: | |
2149 | case DPLANE_OP_RULE_ADD: | |
2150 | case DPLANE_OP_RULE_DELETE: | |
2151 | case DPLANE_OP_RULE_UPDATE: | |
2152 | case DPLANE_OP_NEIGH_DISCOVER: | |
2153 | case DPLANE_OP_BR_PORT_UPDATE: | |
2154 | case DPLANE_OP_IPTABLE_ADD: | |
2155 | case DPLANE_OP_IPTABLE_DELETE: | |
2156 | case DPLANE_OP_IPSET_ADD: | |
2157 | case DPLANE_OP_IPSET_DELETE: | |
2158 | case DPLANE_OP_IPSET_ENTRY_ADD: | |
2159 | case DPLANE_OP_IPSET_ENTRY_DELETE: | |
2160 | case DPLANE_OP_NEIGH_IP_INSTALL: | |
2161 | case DPLANE_OP_NEIGH_IP_DELETE: | |
2162 | case DPLANE_OP_NEIGH_TABLE_UPDATE: | |
2163 | case DPLANE_OP_GRE_SET: | |
2164 | case DPLANE_OP_INTF_ADDR_ADD: | |
2165 | case DPLANE_OP_INTF_ADDR_DEL: | |
2166 | case DPLANE_OP_INTF_NETCONFIG: | |
2167 | case DPLANE_OP_INTF_INSTALL: | |
2168 | case DPLANE_OP_INTF_UPDATE: | |
2169 | case DPLANE_OP_INTF_DELETE: | |
2170 | case DPLANE_OP_TC_QDISC_INSTALL: | |
2171 | case DPLANE_OP_TC_QDISC_UNINSTALL: | |
2172 | case DPLANE_OP_TC_CLASS_ADD: | |
2173 | case DPLANE_OP_TC_CLASS_DELETE: | |
2174 | case DPLANE_OP_TC_CLASS_UPDATE: | |
2175 | case DPLANE_OP_TC_FILTER_ADD: | |
2176 | case DPLANE_OP_TC_FILTER_DELETE: | |
2177 | case DPLANE_OP_TC_FILTER_UPDATE: | |
d37f4d6c | 2178 | break; |
e5ac2adf | 2179 | } |
699dae23 | 2180 | |
f99f1ff5 | 2181 | zebra_rib_evaluate_rn_nexthops(rn, seq, rt_delete); |
a1494c25 | 2182 | zebra_rib_evaluate_mpls(rn); |
e5ac2adf MS |
2183 | done: |
2184 | ||
5f27bcba DS |
2185 | if (rn) |
2186 | route_unlock_node(rn); | |
e5ac2adf MS |
2187 | } |
2188 | ||
9959f1da MS |
2189 | /* |
2190 | * Count installed/FIB nexthops | |
2191 | */ | |
2192 | static int rib_count_installed_nh(struct route_entry *re) | |
2193 | { | |
2194 | int count = 0; | |
2195 | struct nexthop *nexthop; | |
2196 | struct nexthop_group *nhg; | |
2197 | ||
2198 | nhg = rib_get_fib_nhg(re); | |
2199 | ||
2200 | for (ALL_NEXTHOPS_PTR(nhg, nexthop)) { | |
2201 | /* The meaningful flag depends on where the installed | |
2202 | * nexthops reside. | |
2203 | */ | |
43a9f66c | 2204 | if (nhg == &(re->fib_ng)) { |
9959f1da MS |
2205 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) |
2206 | count++; | |
2207 | } else { | |
2208 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) | |
2209 | count++; | |
2210 | } | |
2211 | } | |
2212 | ||
43a9f66c MS |
2213 | nhg = rib_get_fib_backup_nhg(re); |
2214 | if (nhg) { | |
2215 | for (ALL_NEXTHOPS_PTR(nhg, nexthop)) { | |
2216 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) | |
2217 | count++; | |
2218 | } | |
9959f1da MS |
2219 | } |
2220 | ||
2221 | return count; | |
2222 | } | |
2223 | ||
54818e3b MS |
2224 | /* |
2225 | * Handle notification from async dataplane: the dataplane has detected | |
2226 | * some change to a route, and notifies zebra so that the control plane | |
2227 | * can reflect that change. | |
2228 | */ | |
2229 | static void rib_process_dplane_notify(struct zebra_dplane_ctx *ctx) | |
2230 | { | |
2231 | struct route_node *rn = NULL; | |
efe6c026 | 2232 | struct route_entry *re = NULL; |
bd47f3a3 | 2233 | struct vrf *vrf; |
efe6c026 | 2234 | struct nexthop *nexthop; |
941e261c | 2235 | rib_dest_t *dest; |
efe6c026 | 2236 | bool fib_changed = false; |
941e261c | 2237 | bool debug_p = IS_ZEBRA_DEBUG_DPLANE | IS_ZEBRA_DEBUG_RIB; |
efe6c026 | 2238 | int start_count, end_count; |
898b4a6d | 2239 | |
bd47f3a3 | 2240 | vrf = vrf_lookup_by_id(dplane_ctx_get_vrf(ctx)); |
54818e3b | 2241 | |
54818e3b MS |
2242 | /* Locate rn and re(s) from ctx */ |
2243 | rn = rib_find_rn_from_ctx(ctx); | |
2244 | if (rn == NULL) { | |
941e261c | 2245 | if (debug_p) { |
bd47f3a3 | 2246 | zlog_debug( |
898b4a6d | 2247 | "Failed to process dplane notification: no routes for %s(%u:%u):%pRN", |
bd47f3a3 | 2248 | VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx), |
898b4a6d | 2249 | dplane_ctx_get_table(ctx), rn); |
54818e3b MS |
2250 | } |
2251 | goto done; | |
2252 | } | |
2253 | ||
941e261c | 2254 | dest = rib_dest_from_rnode(rn); |
54818e3b | 2255 | |
efe6c026 | 2256 | if (debug_p) |
898b4a6d | 2257 | zlog_debug("%s(%u:%u):%pRN Processing dplane notif ctx %p", |
c2c02b76 | 2258 | VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx), |
898b4a6d | 2259 | dplane_ctx_get_table(ctx), rn, ctx); |
54818e3b MS |
2260 | |
2261 | /* | |
2262 | * Take a pass through the routes, look for matches with the context | |
2263 | * info. | |
2264 | */ | |
efe6c026 MS |
2265 | RNODE_FOREACH_RE(rn, re) { |
2266 | if (rib_route_match_ctx(re, ctx, false /*!update*/)) | |
54818e3b MS |
2267 | break; |
2268 | } | |
2269 | ||
2270 | /* No match? Nothing we can do */ | |
2271 | if (re == NULL) { | |
941e261c | 2272 | if (debug_p) |
bd47f3a3 | 2273 | zlog_debug( |
898b4a6d | 2274 | "%s(%u:%u):%pRN Unable to process dplane notification: no entry for type %s", |
bd47f3a3 | 2275 | VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx), |
898b4a6d | 2276 | dplane_ctx_get_table(ctx), rn, |
bd47f3a3 | 2277 | zebra_route_string(dplane_ctx_get_type(ctx))); |
54818e3b MS |
2278 | |
2279 | goto done; | |
2280 | } | |
2281 | ||
272e8903 | 2282 | /* Ensure we clear the QUEUED flag */ |
a0e11736 DS |
2283 | UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED); |
2284 | UNSET_FLAG(re->status, ROUTE_ENTRY_ROUTE_REPLACING); | |
272e8903 | 2285 | |
4c0b5436 MS |
2286 | /* Is this a notification that ... matters? We mostly care about |
2287 | * the route that is currently selected for installation; we may also | |
2288 | * get an un-install notification, and handle that too. | |
941e261c MS |
2289 | */ |
2290 | if (re != dest->selected_fib) { | |
4c0b5436 MS |
2291 | /* |
2292 | * If we need to, clean up after a delete that was part of | |
2293 | * an update operation. | |
941e261c | 2294 | */ |
4c0b5436 MS |
2295 | end_count = 0; |
2296 | for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) { | |
2297 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) | |
2298 | end_count++; | |
2299 | } | |
2300 | ||
2301 | /* If no nexthops or none installed, ensure that this re | |
2302 | * gets its 'installed' flag cleared. | |
2303 | */ | |
2304 | if (end_count == 0) { | |
2305 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED)) | |
2306 | UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED); | |
2307 | if (debug_p) | |
bd47f3a3 | 2308 | zlog_debug( |
898b4a6d | 2309 | "%s(%u:%u):%pRN dplane notif, uninstalled type %s route", |
bd47f3a3 | 2310 | VRF_LOGNAME(vrf), |
c2c02b76 | 2311 | dplane_ctx_get_vrf(ctx), |
898b4a6d | 2312 | dplane_ctx_get_table(ctx), rn, |
bd47f3a3 JU |
2313 | zebra_route_string( |
2314 | dplane_ctx_get_type(ctx))); | |
4c0b5436 MS |
2315 | } else { |
2316 | /* At least report on the event. */ | |
2317 | if (debug_p) | |
bd47f3a3 | 2318 | zlog_debug( |
898b4a6d | 2319 | "%s(%u:%u):%pRN dplane notif, but type %s not selected_fib", |
bd47f3a3 | 2320 | VRF_LOGNAME(vrf), |
c2c02b76 | 2321 | dplane_ctx_get_vrf(ctx), |
898b4a6d | 2322 | dplane_ctx_get_table(ctx), rn, |
bd47f3a3 JU |
2323 | zebra_route_string( |
2324 | dplane_ctx_get_type(ctx))); | |
4c0b5436 | 2325 | } |
941e261c | 2326 | goto done; |
a0e11736 DS |
2327 | } else { |
2328 | uint32_t flags = dplane_ctx_get_flags(ctx); | |
2329 | ||
2330 | if (CHECK_FLAG(flags, ZEBRA_FLAG_OFFLOADED)) { | |
2331 | UNSET_FLAG(re->flags, ZEBRA_FLAG_OFFLOAD_FAILED); | |
2332 | SET_FLAG(re->flags, ZEBRA_FLAG_OFFLOADED); | |
2333 | } | |
2334 | if (CHECK_FLAG(flags, ZEBRA_FLAG_OFFLOAD_FAILED)) { | |
2335 | UNSET_FLAG(re->flags, ZEBRA_FLAG_OFFLOADED); | |
2336 | SET_FLAG(re->flags, ZEBRA_FLAG_OFFLOAD_FAILED); | |
2337 | } | |
2338 | if (CHECK_FLAG(flags, ZEBRA_FLAG_TRAPPED)) | |
2339 | SET_FLAG(re->flags, ZEBRA_FLAG_TRAPPED); | |
941e261c MS |
2340 | } |
2341 | ||
efe6c026 MS |
2342 | /* We'll want to determine whether the installation status of the |
2343 | * route has changed: we'll check the status before processing, | |
2344 | * and then again if there's been a change. | |
2345 | */ | |
2346 | start_count = 0; | |
1c30d64b | 2347 | |
9959f1da MS |
2348 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED)) |
2349 | start_count = rib_count_installed_nh(re); | |
efe6c026 | 2350 | |
54818e3b MS |
2351 | /* Update zebra's nexthop FIB flags based on the context struct's |
2352 | * nexthops. | |
2353 | */ | |
efe6c026 | 2354 | fib_changed = rib_update_re_from_ctx(re, rn, ctx); |
54818e3b | 2355 | |
efe6c026 MS |
2356 | if (!fib_changed) { |
2357 | if (debug_p) | |
bd47f3a3 | 2358 | zlog_debug( |
898b4a6d | 2359 | "%s(%u:%u):%pRN dplane notification: rib_update returns FALSE", |
bd47f3a3 | 2360 | VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx), |
898b4a6d | 2361 | dplane_ctx_get_table(ctx), rn); |
54818e3b MS |
2362 | } |
2363 | ||
188a00e0 MS |
2364 | /* |
2365 | * Perform follow-up work if the actual status of the prefix | |
efe6c026 | 2366 | * changed. |
941e261c | 2367 | */ |
9959f1da | 2368 | end_count = rib_count_installed_nh(re); |
941e261c | 2369 | |
188a00e0 MS |
2370 | /* Various fib transitions: changed nexthops; from installed to |
2371 | * not-installed; or not-installed to installed. | |
efe6c026 | 2372 | */ |
06525c4f DS |
2373 | if (zrouter.asic_notification_nexthop_control) { |
2374 | if (start_count > 0 && end_count > 0) { | |
2375 | if (debug_p) | |
2376 | zlog_debug( | |
2377 | "%s(%u:%u):%pRN applied nexthop changes from dplane notification", | |
2378 | VRF_LOGNAME(vrf), | |
2379 | dplane_ctx_get_vrf(ctx), | |
2380 | dplane_ctx_get_table(ctx), rn); | |
188a00e0 | 2381 | |
06525c4f DS |
2382 | /* Changed nexthops - update kernel/others */ |
2383 | dplane_route_notif_update(rn, re, | |
2384 | DPLANE_OP_ROUTE_UPDATE, ctx); | |
188a00e0 | 2385 | |
06525c4f DS |
2386 | } else if (start_count == 0 && end_count > 0) { |
2387 | if (debug_p) | |
2388 | zlog_debug( | |
2389 | "%s(%u:%u):%pRN installed transition from dplane notification", | |
2390 | VRF_LOGNAME(vrf), | |
2391 | dplane_ctx_get_vrf(ctx), | |
2392 | dplane_ctx_get_table(ctx), rn); | |
efe6c026 | 2393 | |
06525c4f DS |
2394 | /* We expect this to be the selected route, so we want |
2395 | * to tell others about this transition. | |
2396 | */ | |
2397 | SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED); | |
efe6c026 | 2398 | |
06525c4f DS |
2399 | /* Changed nexthops - update kernel/others */ |
2400 | dplane_route_notif_update(rn, re, | |
2401 | DPLANE_OP_ROUTE_UPDATE, ctx); | |
188a00e0 | 2402 | |
06525c4f DS |
2403 | /* Redistribute, lsp, and nht update */ |
2404 | redistribute_update(rn, re, NULL); | |
efe6c026 | 2405 | |
06525c4f DS |
2406 | } else if (start_count > 0 && end_count == 0) { |
2407 | if (debug_p) | |
2408 | zlog_debug( | |
2409 | "%s(%u:%u):%pRN un-installed transition from dplane notification", | |
2410 | VRF_LOGNAME(vrf), | |
2411 | dplane_ctx_get_vrf(ctx), | |
2412 | dplane_ctx_get_table(ctx), rn); | |
efe6c026 | 2413 | |
06525c4f DS |
2414 | /* Transition from _something_ installed to _nothing_ |
2415 | * installed. | |
2416 | */ | |
2417 | /* We expect this to be the selected route, so we want | |
2418 | * to tell others about this transistion. | |
2419 | */ | |
2420 | UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED); | |
efe6c026 | 2421 | |
06525c4f DS |
2422 | /* Changed nexthops - update kernel/others */ |
2423 | dplane_route_notif_update(rn, re, | |
2424 | DPLANE_OP_ROUTE_DELETE, ctx); | |
188a00e0 | 2425 | |
06525c4f DS |
2426 | /* Redistribute, lsp, and nht update */ |
2427 | redistribute_delete(rn, re, NULL); | |
2428 | } | |
2429 | } | |
2430 | ||
2431 | if (!zebra_router_notify_on_ack()) { | |
2432 | if (CHECK_FLAG(re->flags, ZEBRA_FLAG_OFFLOADED)) | |
2433 | zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_INSTALLED); | |
2434 | if (CHECK_FLAG(re->flags, ZEBRA_FLAG_OFFLOAD_FAILED)) | |
2435 | zsend_route_notify_owner_ctx(ctx, | |
2436 | ZAPI_ROUTE_FAIL_INSTALL); | |
9287b4c5 | 2437 | } |
efe6c026 | 2438 | |
9287b4c5 | 2439 | /* Make any changes visible for lsp and nexthop-tracking processing */ |
f99f1ff5 SP |
2440 | zebra_rib_evaluate_rn_nexthops(rn, zebra_router_get_next_sequence(), |
2441 | false); | |
efe6c026 | 2442 | |
9287b4c5 | 2443 | zebra_rib_evaluate_mpls(rn); |
941e261c | 2444 | |
54818e3b | 2445 | done: |
941e261c MS |
2446 | if (rn) |
2447 | route_unlock_node(rn); | |
54818e3b MS |
2448 | } |
2449 | ||
32367e7a MS |
2450 | /* |
2451 | * Process a node from the EVPN/VXLAN subqueue. | |
2452 | */ | |
2453 | static void process_subq_evpn(struct listnode *lnode) | |
2454 | { | |
2455 | struct wq_evpn_wrapper *w; | |
2456 | ||
2457 | /* In general, the list node points to a wrapper object | |
2458 | * holding the info necessary to make some update. | |
2459 | */ | |
2460 | w = listgetdata(lnode); | |
2461 | if (!w) | |
2462 | return; | |
2463 | ||
2464 | if (w->type == WQ_EVPN_WRAPPER_TYPE_VRFROUTE) { | |
2465 | if (w->add_p) | |
7f7e49d1 | 2466 | zebra_vxlan_evpn_vrf_route_add(w->vrf_id, &w->macaddr, |
32367e7a MS |
2467 | &w->ip, &w->prefix); |
2468 | else | |
2469 | zebra_vxlan_evpn_vrf_route_del(w->vrf_id, &w->ip, | |
2470 | &w->prefix); | |
2471 | } else if (w->type == WQ_EVPN_WRAPPER_TYPE_REM_ES) { | |
2472 | if (w->add_p) | |
2473 | zebra_evpn_remote_es_add(&w->esi, w->ip.ipaddr_v4, | |
2474 | w->esr_rxed, w->df_alg, | |
2475 | w->df_pref); | |
2476 | else | |
2477 | zebra_evpn_remote_es_del(&w->esi, w->ip.ipaddr_v4); | |
7f7e49d1 MS |
2478 | } else if (w->type == WQ_EVPN_WRAPPER_TYPE_REM_MACIP) { |
2479 | uint16_t ipa_len = 0; | |
2480 | ||
2481 | if (w->ip.ipa_type == IPADDR_V4) | |
2482 | ipa_len = IPV4_MAX_BYTELEN; | |
2483 | else if (w->ip.ipa_type == IPADDR_V6) | |
2484 | ipa_len = IPV6_MAX_BYTELEN; | |
2485 | ||
2486 | if (w->add_p) | |
2487 | zebra_evpn_rem_macip_add(w->vni, &w->macaddr, ipa_len, | |
2488 | &w->ip, w->flags, w->seq, | |
2489 | w->vtep_ip, &w->esi); | |
2490 | else | |
2491 | zebra_evpn_rem_macip_del(w->vni, &w->macaddr, ipa_len, | |
2492 | &w->ip, w->vtep_ip); | |
7e5b0b2b MS |
2493 | } else if (w->type == WQ_EVPN_WRAPPER_TYPE_REM_VTEP) { |
2494 | if (w->add_p) | |
2495 | zebra_vxlan_remote_vtep_add(w->vrf_id, w->vni, | |
2496 | w->vtep_ip, w->flags); | |
2497 | else | |
2498 | zebra_vxlan_remote_vtep_del(w->vrf_id, w->vni, | |
2499 | w->vtep_ip); | |
32367e7a MS |
2500 | } |
2501 | ||
7e5b0b2b | 2502 | |
32367e7a MS |
2503 | XFREE(MTYPE_WQ_WRAPPER, w); |
2504 | } | |
2505 | ||
04bec7b2 MS |
2506 | /* |
2507 | * Process the nexthop-group workqueue subqueue | |
2508 | */ | |
e22e8001 | 2509 | static void process_subq_nhg(struct listnode *lnode) |
e96f9203 | 2510 | { |
04bec7b2 MS |
2511 | struct nhg_ctx *ctx; |
2512 | struct nhg_hash_entry *nhe, *newnhe; | |
2513 | struct wq_nhg_wrapper *w; | |
4bbbd1f6 | 2514 | uint8_t qindex = META_QUEUE_NHG; |
5110a0c6 | 2515 | |
04bec7b2 | 2516 | w = listgetdata(lnode); |
e22e8001 | 2517 | |
04bec7b2 | 2518 | if (!w) |
e22e8001 SW |
2519 | return; |
2520 | ||
04bec7b2 MS |
2521 | /* Two types of object - an update from the local kernel, or |
2522 | * an nhg update from a daemon. | |
2523 | */ | |
2524 | if (w->type == WQ_NHG_WRAPPER_TYPE_CTX) { | |
2525 | ctx = w->u.ctx; | |
2526 | ||
2527 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
2528 | zlog_debug( | |
7a96ccc7 DS |
2529 | "NHG Context id=%u dequeued from sub-queue %s", |
2530 | ctx->id, subqueue2str(qindex)); | |
04bec7b2 MS |
2531 | |
2532 | ||
2533 | /* Process nexthop group updates coming 'up' from the OS */ | |
2534 | nhg_ctx_process(ctx); | |
e22e8001 | 2535 | |
04bec7b2 MS |
2536 | } else if (w->type == WQ_NHG_WRAPPER_TYPE_NHG) { |
2537 | nhe = w->u.nhe; | |
2538 | ||
2539 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
7a96ccc7 DS |
2540 | zlog_debug("NHG %u dequeued from sub-queue %s", nhe->id, |
2541 | subqueue2str(qindex)); | |
04bec7b2 MS |
2542 | |
2543 | /* Process incoming nhg update, probably from a proto daemon */ | |
2544 | newnhe = zebra_nhg_proto_add(nhe->id, nhe->type, | |
2545 | nhe->zapi_instance, | |
32367e7a | 2546 | nhe->zapi_session, &nhe->nhg, 0); |
04bec7b2 MS |
2547 | |
2548 | /* Report error to daemon via ZAPI */ | |
2549 | if (newnhe == NULL) | |
2550 | zsend_nhg_notify(nhe->type, nhe->zapi_instance, | |
2551 | nhe->zapi_session, nhe->id, | |
2552 | ZAPI_NHG_FAIL_INSTALL); | |
2553 | ||
2554 | /* Free temp nhe - we own that memory. */ | |
2555 | zebra_nhg_free(nhe); | |
2556 | } | |
e22e8001 | 2557 | |
32367e7a | 2558 | XFREE(MTYPE_WQ_WRAPPER, w); |
e22e8001 SW |
2559 | } |
2560 | ||
53216dff DS |
2561 | static void process_subq_early_label(struct listnode *lnode) |
2562 | { | |
2563 | struct wq_label_wrapper *w = listgetdata(lnode); | |
2564 | struct zebra_vrf *zvrf; | |
2565 | ||
2566 | if (!w) | |
2567 | return; | |
2568 | ||
2569 | zvrf = vrf_info_lookup(w->vrf_id); | |
2570 | if (!zvrf) { | |
2571 | XFREE(MTYPE_WQ_WRAPPER, w); | |
2572 | return; | |
2573 | } | |
2574 | ||
2575 | switch (w->type) { | |
2576 | case WQ_LABEL_FTN_UNINSTALL: | |
2577 | zebra_mpls_ftn_uninstall(zvrf, w->ltype, &w->p, w->route_type, | |
2578 | w->route_instance); | |
2579 | break; | |
2580 | case WQ_LABEL_LABELS_PROCESS: | |
2581 | zebra_mpls_zapi_labels_process(w->add_p, zvrf, &w->zl); | |
2582 | break; | |
2583 | } | |
2584 | ||
2585 | XFREE(MTYPE_WQ_WRAPPER, w); | |
2586 | } | |
2587 | ||
e22e8001 SW |
2588 | static void process_subq_route(struct listnode *lnode, uint8_t qindex) |
2589 | { | |
2590 | struct route_node *rnode = NULL; | |
2591 | rib_dest_t *dest = NULL; | |
2592 | struct zebra_vrf *zvrf = NULL; | |
5110a0c6 | 2593 | |
d62a17ae | 2594 | rnode = listgetdata(lnode); |
2595 | dest = rib_dest_from_rnode(rnode); | |
c2c02b76 DS |
2596 | assert(dest); |
2597 | ||
2598 | zvrf = rib_dest_vrf(dest); | |
41ec9222 | 2599 | |
d62a17ae | 2600 | rib_process(rnode); |
5110a0c6 | 2601 | |
d62a17ae | 2602 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) { |
7e26b9d4 | 2603 | struct route_entry *re = NULL; |
efe6c026 | 2604 | |
7e26b9d4 DS |
2605 | /* |
2606 | * rib_process may have freed the dest | |
2607 | * as part of the garbage collection. Let's | |
2608 | * prevent stupidity from happening. | |
2609 | */ | |
2610 | dest = rib_dest_from_rnode(rnode); | |
2611 | if (dest) | |
2612 | re = re_list_first(&dest->routes); | |
2613 | ||
7a96ccc7 | 2614 | zlog_debug("%s(%u:%u):%pRN rn %p dequeued from sub-queue %s", |
04bec7b2 | 2615 | zvrf_name(zvrf), zvrf_id(zvrf), re ? re->table : 0, |
7a96ccc7 | 2616 | rnode, rnode, subqueue2str(qindex)); |
d62a17ae | 2617 | } |
41ec9222 | 2618 | |
d62a17ae | 2619 | if (rnode->info) |
2620 | UNSET_FLAG(rib_dest_from_rnode(rnode)->flags, | |
2621 | RIB_ROUTE_QUEUED(qindex)); | |
9fd92e3c | 2622 | |
d62a17ae | 2623 | route_unlock_node(rnode); |
e22e8001 SW |
2624 | } |
2625 | ||
d7ac4c4d | 2626 | static void rib_re_nhg_free(struct route_entry *re) |
e22e8001 | 2627 | { |
d7ac4c4d DS |
2628 | if (re->nhe && re->nhe_id) { |
2629 | assert(re->nhe->id == re->nhe_id); | |
2630 | route_entry_update_nhe(re, NULL); | |
2631 | } else if (re->nhe && re->nhe->nhg.nexthop) | |
2632 | nexthops_free(re->nhe->nhg.nexthop); | |
e22e8001 | 2633 | |
d7ac4c4d DS |
2634 | nexthops_free(re->fib_ng.nexthop); |
2635 | } | |
e22e8001 | 2636 | |
d7ac4c4d DS |
2637 | struct zebra_early_route { |
2638 | afi_t afi; | |
2639 | safi_t safi; | |
2640 | struct prefix p; | |
2641 | struct prefix_ipv6 src_p; | |
2642 | bool src_p_provided; | |
2643 | struct route_entry *re; | |
2644 | struct nhg_hash_entry *re_nhe; | |
2645 | bool startup; | |
2646 | bool deletion; | |
2647 | bool fromkernel; | |
2648 | }; | |
e22e8001 | 2649 | |
d7ac4c4d DS |
2650 | static void early_route_memory_free(struct zebra_early_route *ere) |
2651 | { | |
2652 | if (ere->re_nhe) | |
2653 | zebra_nhg_free(ere->re_nhe); | |
e22e8001 | 2654 | |
d7ac4c4d DS |
2655 | XFREE(MTYPE_RE, ere->re); |
2656 | XFREE(MTYPE_WQ_WRAPPER, ere); | |
e96f9203 DO |
2657 | } |
2658 | ||
d7ac4c4d | 2659 | static void process_subq_early_route_add(struct zebra_early_route *ere) |
e96f9203 | 2660 | { |
d7ac4c4d DS |
2661 | struct route_entry *re = ere->re; |
2662 | struct route_table *table; | |
2663 | struct nhg_hash_entry *nhe = NULL; | |
2664 | struct route_node *rn; | |
2665 | struct route_entry *same = NULL, *first_same = NULL; | |
2666 | int same_count = 0; | |
2667 | rib_dest_t *dest; | |
91f16812 | 2668 | |
d7ac4c4d DS |
2669 | /* Lookup table. */ |
2670 | table = zebra_vrf_get_table_with_table_id(ere->afi, ere->safi, | |
2671 | re->vrf_id, re->table); | |
2672 | if (!table) { | |
2673 | early_route_memory_free(ere); | |
2674 | return; | |
2675 | } | |
91f16812 | 2676 | |
d7ac4c4d DS |
2677 | if (re->nhe_id > 0) { |
2678 | nhe = zebra_nhg_lookup_id(re->nhe_id); | |
91f16812 | 2679 | |
d7ac4c4d DS |
2680 | if (!nhe) { |
2681 | /* | |
2682 | * We've received from the kernel a nexthop id | |
2683 | * that we don't have saved yet. More than likely | |
2684 | * it has not been processed and is on the | |
2685 | * queue to be processed. Let's stop what we | |
2686 | * are doing and cause the meta q to be processed | |
2687 | * storing this for later. | |
2688 | * | |
2689 | * This is being done this way because zebra | |
2690 | * runs with the assumption t | |
2691 | */ | |
2692 | flog_err( | |
2693 | EC_ZEBRA_TABLE_LOOKUP_FAILED, | |
2694 | "Zebra failed to find the nexthop hash entry for id=%u in a route entry %pFX", | |
2695 | re->nhe_id, &ere->p); | |
5110a0c6 | 2696 | |
d7ac4c4d DS |
2697 | early_route_memory_free(ere); |
2698 | return; | |
d62a17ae | 2699 | } |
d7ac4c4d DS |
2700 | } else { |
2701 | /* Lookup nhe from route information */ | |
2702 | nhe = zebra_nhg_rib_find_nhe(ere->re_nhe, ere->afi); | |
2703 | if (!nhe) { | |
2704 | char buf2[PREFIX_STRLEN] = ""; | |
e96f9203 | 2705 | |
d7ac4c4d DS |
2706 | flog_err( |
2707 | EC_ZEBRA_TABLE_LOOKUP_FAILED, | |
2708 | "Zebra failed to find or create a nexthop hash entry for %pFX%s%s", | |
2709 | &ere->p, ere->src_p_provided ? " from " : "", | |
2710 | ere->src_p_provided | |
2711 | ? prefix2str(&ere->src_p, buf2, | |
2712 | sizeof(buf2)) | |
2713 | : ""); | |
f52ed677 | 2714 | |
d7ac4c4d DS |
2715 | early_route_memory_free(ere); |
2716 | return; | |
2717 | } | |
2718 | } | |
5110a0c6 | 2719 | |
d7ac4c4d DS |
2720 | /* |
2721 | * Attach the re to the nhe's nexthop group. | |
2722 | * | |
2723 | * TODO: This will need to change when we start getting IDs from upper | |
2724 | * level protocols, as the refcnt might be wrong, since it checks | |
2725 | * if old_id != new_id. | |
2726 | */ | |
2727 | route_entry_update_nhe(re, nhe); | |
e22e8001 | 2728 | |
d7ac4c4d DS |
2729 | /* Make it sure prefixlen is applied to the prefix. */ |
2730 | apply_mask(&ere->p); | |
2731 | if (ere->src_p_provided) | |
2732 | apply_mask_ipv6(&ere->src_p); | |
d62a17ae | 2733 | |
d7ac4c4d DS |
2734 | /* Lookup route node.*/ |
2735 | rn = srcdest_rnode_get(table, &ere->p, | |
2736 | ere->src_p_provided ? &ere->src_p : NULL); | |
2737 | ||
2738 | /* | |
2739 | * If same type of route are installed, treat it as a implicit | |
2740 | * withdraw. If the user has specified the No route replace semantics | |
2741 | * for the install don't do a route replace. | |
2742 | */ | |
2743 | RNODE_FOREACH_RE (rn, same) { | |
2744 | if (CHECK_FLAG(same->status, ROUTE_ENTRY_REMOVED)) { | |
2745 | same_count++; | |
2746 | continue; | |
d62a17ae | 2747 | } |
5110a0c6 | 2748 | |
d7ac4c4d DS |
2749 | /* Compare various route_entry properties */ |
2750 | if (rib_compare_routes(re, same)) { | |
2751 | same_count++; | |
5110a0c6 | 2752 | |
d7ac4c4d DS |
2753 | if (first_same == NULL) |
2754 | first_same = same; | |
2755 | } | |
d62a17ae | 2756 | } |
f52ed677 | 2757 | |
d7ac4c4d | 2758 | same = first_same; |
e22e8001 | 2759 | |
d7ac4c4d DS |
2760 | if (!ere->startup && (re->flags & ZEBRA_FLAG_SELFROUTE) && |
2761 | zrouter.asic_offloaded) { | |
2762 | if (!same) { | |
2763 | if (IS_ZEBRA_DEBUG_RIB) | |
2764 | zlog_debug( | |
2765 | "prefix: %pRN is a self route where we do not have an entry for it. Dropping this update, it's useless", | |
2766 | rn); | |
2767 | /* | |
2768 | * We are not on startup, this is a self route | |
2769 | * and we have asic offload. Which means | |
2770 | * we are getting a callback for a entry | |
2771 | * that was already deleted to the kernel | |
2772 | * but an earlier response was just handed | |
2773 | * back. Drop it on the floor | |
2774 | */ | |
2775 | early_route_memory_free(ere); | |
2776 | return; | |
2777 | } | |
2778 | } | |
4d38fdb4 | 2779 | |
1cd8bd05 DS |
2780 | /* Set default distance by route type. */ |
2781 | if (re->distance == 0) { | |
2782 | if (same && !zebra_router_notify_on_ack()) | |
2783 | re->distance = same->distance; | |
2784 | else | |
2785 | re->distance = route_distance(re->type); | |
2786 | } | |
2787 | ||
68ff69fa DS |
2788 | if (re->metric == ROUTE_INSTALLATION_METRIC && |
2789 | CHECK_FLAG(re->flags, ZEBRA_FLAG_SELFROUTE)) { | |
2790 | if (same && !zebra_router_notify_on_ack()) | |
2791 | re->metric = same->metric; | |
2792 | else | |
2793 | re->metric = 0; | |
2794 | } | |
2795 | ||
d7ac4c4d DS |
2796 | /* If this route is kernel/connected route, notify the dataplane. */ |
2797 | if (RIB_SYSTEM_ROUTE(re)) { | |
2798 | /* Notify dataplane */ | |
2799 | dplane_sys_route_add(rn, re); | |
2800 | } | |
53216dff | 2801 | |
d7ac4c4d DS |
2802 | /* Link new re to node.*/ |
2803 | if (IS_ZEBRA_DEBUG_RIB) { | |
2804 | rnode_debug( | |
2805 | rn, re->vrf_id, | |
2806 | "Inserting route rn %p, re %p (%s) existing %p, same_count %d", | |
2807 | rn, re, zebra_route_string(re->type), same, same_count); | |
fc328ac9 | 2808 | |
d7ac4c4d DS |
2809 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) |
2810 | route_entry_dump( | |
2811 | &ere->p, | |
2812 | ere->src_p_provided ? &ere->src_p : NULL, re); | |
2813 | } | |
e22e8001 | 2814 | |
d7ac4c4d DS |
2815 | SET_FLAG(re->status, ROUTE_ENTRY_CHANGED); |
2816 | rib_addnode(rn, re, 1); | |
4d38fdb4 | 2817 | |
d7ac4c4d DS |
2818 | /* Free implicit route.*/ |
2819 | if (same) | |
2820 | rib_delnode(rn, same); | |
04bec7b2 | 2821 | |
d7ac4c4d DS |
2822 | /* See if we can remove some RE entries that are queued for |
2823 | * removal, but won't be considered in rib processing. | |
2824 | */ | |
2825 | dest = rib_dest_from_rnode(rn); | |
2826 | RNODE_FOREACH_RE_SAFE (rn, re, same) { | |
2827 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) { | |
2828 | /* If the route was used earlier, must retain it. */ | |
2829 | if (dest && re == dest->selected_fib) | |
2830 | continue; | |
04bec7b2 | 2831 | |
d7ac4c4d DS |
2832 | if (IS_ZEBRA_DEBUG_RIB) |
2833 | rnode_debug(rn, re->vrf_id, | |
2834 | "rn %p, removing unneeded re %p", | |
2835 | rn, re); | |
e22e8001 | 2836 | |
d7ac4c4d DS |
2837 | rib_unlink(rn, re); |
2838 | } | |
2839 | } | |
e22e8001 | 2840 | |
d7ac4c4d DS |
2841 | route_unlock_node(rn); |
2842 | if (ere->re_nhe) | |
2843 | zebra_nhg_free(ere->re_nhe); | |
2844 | XFREE(MTYPE_WQ_WRAPPER, ere); | |
e22e8001 SW |
2845 | } |
2846 | ||
d7ac4c4d | 2847 | static void process_subq_early_route_delete(struct zebra_early_route *ere) |
04bec7b2 | 2848 | { |
d7ac4c4d DS |
2849 | struct route_table *table; |
2850 | struct route_node *rn; | |
2851 | struct route_entry *re; | |
2852 | struct route_entry *fib = NULL; | |
2853 | struct route_entry *same = NULL; | |
2854 | struct nexthop *rtnh; | |
2855 | char buf2[INET6_ADDRSTRLEN]; | |
2856 | rib_dest_t *dest; | |
04bec7b2 | 2857 | |
d7ac4c4d DS |
2858 | if (ere->src_p_provided) |
2859 | assert(!ere->src_p.prefixlen || ere->afi == AFI_IP6); | |
2860 | ||
2861 | /* Lookup table. */ | |
2862 | table = zebra_vrf_lookup_table_with_table_id( | |
2863 | ere->afi, ere->safi, ere->re->vrf_id, ere->re->table); | |
2864 | if (!table) { | |
2865 | early_route_memory_free(ere); | |
2866 | return; | |
2867 | } | |
2868 | ||
2869 | /* Apply mask. */ | |
2870 | apply_mask(&ere->p); | |
2871 | if (ere->src_p_provided) | |
2872 | apply_mask_ipv6(&ere->src_p); | |
2873 | ||
2874 | /* Lookup route node. */ | |
2875 | rn = srcdest_rnode_lookup(table, &ere->p, | |
2876 | ere->src_p_provided ? &ere->src_p : NULL); | |
2877 | if (!rn) { | |
2878 | if (IS_ZEBRA_DEBUG_RIB) { | |
2879 | char src_buf[PREFIX_STRLEN]; | |
2880 | struct vrf *vrf = vrf_lookup_by_id(ere->re->vrf_id); | |
2881 | ||
2882 | if (ere->src_p_provided && ere->src_p.prefixlen) | |
2883 | prefix2str(&ere->src_p, src_buf, | |
2884 | sizeof(src_buf)); | |
2885 | else | |
2886 | src_buf[0] = '\0'; | |
2887 | ||
2888 | zlog_debug("%s[%d]:%pRN%s%s doesn't exist in rib", | |
2889 | vrf->name, ere->re->table, rn, | |
2890 | (src_buf[0] != '\0') ? " from " : "", | |
2891 | src_buf); | |
2892 | } | |
2893 | early_route_memory_free(ere); | |
2894 | return; | |
2895 | } | |
2896 | ||
2897 | dest = rib_dest_from_rnode(rn); | |
2898 | fib = dest->selected_fib; | |
2899 | ||
2900 | struct nexthop *nh = NULL; | |
2901 | ||
2902 | if (ere->re->nhe) | |
2903 | nh = ere->re->nhe->nhg.nexthop; | |
2904 | ||
2905 | /* Lookup same type route. */ | |
2906 | RNODE_FOREACH_RE (rn, re) { | |
2907 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) | |
2908 | continue; | |
2909 | ||
2910 | if (re->type != ere->re->type) | |
2911 | continue; | |
2912 | if (re->instance != ere->re->instance) | |
2913 | continue; | |
2914 | if (CHECK_FLAG(re->flags, ZEBRA_FLAG_RR_USE_DISTANCE) && | |
2915 | ere->re->distance != re->distance) | |
2916 | continue; | |
2917 | ||
2918 | if (re->type == ZEBRA_ROUTE_KERNEL && | |
2919 | re->metric != ere->re->metric) | |
2920 | continue; | |
2921 | if (re->type == ZEBRA_ROUTE_CONNECT && (rtnh = nh) && | |
2922 | rtnh->type == NEXTHOP_TYPE_IFINDEX && nh) { | |
2923 | if (rtnh->ifindex != nh->ifindex) | |
2924 | continue; | |
2925 | same = re; | |
2926 | break; | |
2927 | } | |
2928 | ||
2929 | /* Make sure that the route found has the same gateway. */ | |
2930 | if (ere->re->nhe_id && re->nhe_id == ere->re->nhe_id) { | |
2931 | same = re; | |
2932 | break; | |
2933 | } | |
2934 | ||
2935 | if (nh == NULL) { | |
2936 | same = re; | |
2937 | break; | |
2938 | } | |
2939 | for (ALL_NEXTHOPS(re->nhe->nhg, rtnh)) { | |
2940 | /* | |
2941 | * No guarantee all kernel send nh with labels | |
2942 | * on delete. | |
2943 | */ | |
2944 | if (nexthop_same_no_labels(rtnh, nh)) { | |
2945 | same = re; | |
2946 | break; | |
2947 | } | |
2948 | } | |
2949 | ||
2950 | if (same) | |
2951 | break; | |
2952 | } | |
2953 | /* | |
2954 | * If same type of route can't be found and this message is from | |
2955 | * kernel. | |
2956 | */ | |
2957 | if (!same) { | |
2958 | /* | |
2959 | * In the past(HA!) we could get here because | |
2960 | * we were receiving a route delete from the | |
2961 | * kernel and we're not marking the proto | |
2962 | * as coming from it's appropriate originator. | |
2963 | * Now that we are properly noticing the fact | |
2964 | * that the kernel has deleted our route we | |
2965 | * are not going to get called in this path | |
2966 | * I am going to leave this here because | |
2967 | * this might still work this way on non-linux | |
2968 | * platforms as well as some weird state I have | |
2969 | * not properly thought of yet. | |
2970 | * If we can show that this code path is | |
2971 | * dead then we can remove it. | |
2972 | */ | |
2973 | if (fib && CHECK_FLAG(ere->re->flags, ZEBRA_FLAG_SELFROUTE)) { | |
2974 | if (IS_ZEBRA_DEBUG_RIB) { | |
2975 | rnode_debug( | |
2976 | rn, ere->re->vrf_id, | |
2977 | "rn %p, re %p (%s) was deleted from kernel, adding", | |
2978 | rn, fib, zebra_route_string(fib->type)); | |
2979 | } | |
2980 | if (zrouter.allow_delete || | |
2981 | CHECK_FLAG(dest->flags, RIB_ROUTE_ANY_QUEUED)) { | |
2982 | UNSET_FLAG(fib->status, ROUTE_ENTRY_INSTALLED); | |
2983 | /* Unset flags. */ | |
2984 | for (rtnh = fib->nhe->nhg.nexthop; rtnh; | |
2985 | rtnh = rtnh->next) | |
2986 | UNSET_FLAG(rtnh->flags, | |
2987 | NEXTHOP_FLAG_FIB); | |
2988 | ||
2989 | /* | |
2990 | * This is a non FRR route | |
2991 | * as such we should mark | |
2992 | * it as deleted | |
2993 | */ | |
2994 | dest->selected_fib = NULL; | |
2995 | } else { | |
2996 | /* | |
2997 | * This means someone else, other than Zebra, | |
2998 | * has deleted a Zebra router from the kernel. | |
2999 | * We will add it back | |
3000 | */ | |
3001 | rib_install_kernel(rn, fib, NULL); | |
3002 | } | |
3003 | } else { | |
3004 | if (IS_ZEBRA_DEBUG_RIB) { | |
3005 | if (nh) | |
3006 | rnode_debug( | |
3007 | rn, ere->re->vrf_id, | |
3008 | "via %s ifindex %d type %d doesn't exist in rib", | |
3009 | inet_ntop(afi2family(ere->afi), | |
3010 | &nh->gate, buf2, | |
3011 | sizeof(buf2)), | |
3012 | nh->ifindex, ere->re->type); | |
3013 | else | |
3014 | rnode_debug( | |
3015 | rn, ere->re->vrf_id, | |
3016 | "type %d doesn't exist in rib", | |
3017 | ere->re->type); | |
3018 | } | |
3019 | route_unlock_node(rn); | |
3020 | early_route_memory_free(ere); | |
3021 | return; | |
3022 | } | |
3023 | } | |
3024 | ||
3025 | if (same) { | |
3026 | struct nexthop *tmp_nh; | |
3027 | ||
3028 | if (ere->fromkernel && | |
3029 | CHECK_FLAG(ere->re->flags, ZEBRA_FLAG_SELFROUTE) && | |
3030 | !zrouter.allow_delete) { | |
3031 | rib_install_kernel(rn, same, NULL); | |
3032 | route_unlock_node(rn); | |
3033 | ||
3034 | early_route_memory_free(ere); | |
3035 | return; | |
3036 | } | |
3037 | ||
3038 | /* Special handling for IPv4 or IPv6 routes sourced from | |
3039 | * EVPN - the nexthop (and associated MAC) need to be | |
3040 | * uninstalled if no more refs. | |
3041 | */ | |
3042 | for (ALL_NEXTHOPS(re->nhe->nhg, tmp_nh)) { | |
3043 | struct ipaddr vtep_ip; | |
3044 | ||
3045 | if (CHECK_FLAG(tmp_nh->flags, NEXTHOP_FLAG_EVPN)) { | |
3046 | memset(&vtep_ip, 0, sizeof(struct ipaddr)); | |
3047 | if (ere->afi == AFI_IP) { | |
3048 | vtep_ip.ipa_type = IPADDR_V4; | |
3049 | memcpy(&(vtep_ip.ipaddr_v4), | |
3050 | &(tmp_nh->gate.ipv4), | |
3051 | sizeof(struct in_addr)); | |
3052 | } else { | |
3053 | vtep_ip.ipa_type = IPADDR_V6; | |
3054 | memcpy(&(vtep_ip.ipaddr_v6), | |
3055 | &(tmp_nh->gate.ipv6), | |
3056 | sizeof(struct in6_addr)); | |
3057 | } | |
3058 | zebra_rib_queue_evpn_route_del( | |
3059 | re->vrf_id, &vtep_ip, &ere->p); | |
3060 | } | |
3061 | } | |
3062 | ||
3063 | /* Notify dplane if system route changes */ | |
3064 | if (RIB_SYSTEM_ROUTE(re)) | |
3065 | dplane_sys_route_del(rn, same); | |
3066 | ||
3067 | rib_delnode(rn, same); | |
3068 | } | |
3069 | ||
3070 | route_unlock_node(rn); | |
3071 | ||
3072 | early_route_memory_free(ere); | |
3073 | } | |
3074 | ||
3075 | /* | |
3076 | * When FRR receives a route we need to match the route up to | |
3077 | * nexthop groups. That we also may have just received | |
3078 | * place the data on this queue so that this work of finding | |
3079 | * the nexthop group entries for the route entry is always | |
3080 | * done after the nexthop group has had a chance to be processed | |
3081 | */ | |
3082 | static void process_subq_early_route(struct listnode *lnode) | |
3083 | { | |
3084 | struct zebra_early_route *ere = listgetdata(lnode); | |
3085 | ||
3086 | if (ere->deletion) | |
3087 | process_subq_early_route_delete(ere); | |
3088 | else | |
3089 | process_subq_early_route_add(ere); | |
3090 | } | |
3091 | ||
3092 | /* | |
3093 | * Examine the specified subqueue; process one entry and return 1 if | |
3094 | * there is a node, return 0 otherwise. | |
3095 | */ | |
3096 | static unsigned int process_subq(struct list *subq, | |
3097 | enum meta_queue_indexes qindex) | |
3098 | { | |
3099 | struct listnode *lnode = listhead(subq); | |
3100 | ||
3101 | if (!lnode) | |
3102 | return 0; | |
3103 | ||
3104 | switch (qindex) { | |
3105 | case META_QUEUE_EVPN: | |
3106 | process_subq_evpn(lnode); | |
3107 | break; | |
3108 | case META_QUEUE_NHG: | |
3109 | process_subq_nhg(lnode); | |
3110 | break; | |
3111 | case META_QUEUE_EARLY_ROUTE: | |
3112 | process_subq_early_route(lnode); | |
3113 | break; | |
3114 | case META_QUEUE_EARLY_LABEL: | |
3115 | process_subq_early_label(lnode); | |
3116 | break; | |
3117 | case META_QUEUE_CONNECTED: | |
3118 | case META_QUEUE_KERNEL: | |
3119 | case META_QUEUE_STATIC: | |
3120 | case META_QUEUE_NOTBGP: | |
3121 | case META_QUEUE_BGP: | |
3122 | case META_QUEUE_OTHER: | |
3123 | process_subq_route(lnode, qindex); | |
3124 | break; | |
3125 | } | |
3126 | ||
3127 | list_delete_node(subq, lnode); | |
3128 | ||
3129 | return 1; | |
3130 | } | |
3131 | ||
3132 | /* Dispatch the meta queue by picking and processing the next node from | |
3133 | * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and | |
3134 | * data is pointed to the meta queue structure. | |
3135 | */ | |
3136 | static wq_item_status meta_queue_process(struct work_queue *dummy, void *data) | |
3137 | { | |
3138 | struct meta_queue *mq = data; | |
3139 | unsigned i; | |
3140 | uint32_t queue_len, queue_limit; | |
3141 | ||
3142 | /* Ensure there's room for more dataplane updates */ | |
3143 | queue_limit = dplane_get_in_queue_limit(); | |
3144 | queue_len = dplane_get_in_queue_len(); | |
3145 | if (queue_len > queue_limit) { | |
3146 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
3147 | zlog_debug( | |
3148 | "rib queue: dplane queue len %u, limit %u, retrying", | |
3149 | queue_len, queue_limit); | |
3150 | ||
3151 | /* Ensure that the meta-queue is actually enqueued */ | |
3152 | if (work_queue_empty(zrouter.ribq)) | |
3153 | work_queue_add(zrouter.ribq, zrouter.mq); | |
3154 | ||
3155 | return WQ_QUEUE_BLOCKED; | |
3156 | } | |
3157 | ||
3158 | for (i = 0; i < MQ_SIZE; i++) | |
3159 | if (process_subq(mq->subq[i], i)) { | |
3160 | mq->size--; | |
3161 | break; | |
3162 | } | |
3163 | return mq->size ? WQ_REQUEUE : WQ_SUCCESS; | |
3164 | } | |
3165 | ||
3166 | ||
3167 | /* | |
3168 | * Look into the RN and queue it into the highest priority queue | |
3169 | * at this point in time for processing. | |
3170 | * | |
3171 | * We will enqueue a route node only once per invocation. | |
3172 | * | |
3173 | * There are two possibilities here that should be kept in mind. | |
3174 | * If the original invocation has not been pulled off for processing | |
3175 | * yet, A subsuquent invocation can have a route entry with a better | |
3176 | * meta queue index value and we can have a situation where | |
3177 | * we might have the same node enqueued 2 times. Not necessarily | |
3178 | * an optimal situation but it should be ok. | |
3179 | * | |
3180 | * The other possibility is that the original invocation has not | |
3181 | * been pulled off for processing yet, A subsusquent invocation | |
3182 | * doesn't have a route_entry with a better meta-queue and the | |
3183 | * original metaqueue index value will win and we'll end up with | |
3184 | * the route node enqueued once. | |
3185 | */ | |
3186 | static int rib_meta_queue_add(struct meta_queue *mq, void *data) | |
3187 | { | |
3188 | struct route_node *rn = NULL; | |
3189 | struct route_entry *re = NULL, *curr_re = NULL; | |
3190 | uint8_t qindex = MQ_SIZE, curr_qindex = MQ_SIZE; | |
3191 | ||
3192 | rn = (struct route_node *)data; | |
3193 | ||
3194 | RNODE_FOREACH_RE (rn, curr_re) { | |
3195 | curr_qindex = route_info[curr_re->type].meta_q_map; | |
3196 | ||
3197 | if (curr_qindex <= qindex) { | |
3198 | re = curr_re; | |
3199 | qindex = curr_qindex; | |
3200 | } | |
3201 | } | |
3202 | ||
3203 | if (!re) | |
3204 | return -1; | |
3205 | ||
3206 | /* Invariant: at this point we always have rn->info set. */ | |
3207 | if (CHECK_FLAG(rib_dest_from_rnode(rn)->flags, | |
3208 | RIB_ROUTE_QUEUED(qindex))) { | |
3209 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
3210 | rnode_debug(rn, re->vrf_id, | |
3211 | "rn %p is already queued in sub-queue %s", | |
3212 | (void *)rn, subqueue2str(qindex)); | |
3213 | return -1; | |
3214 | } | |
3215 | ||
3216 | SET_FLAG(rib_dest_from_rnode(rn)->flags, RIB_ROUTE_QUEUED(qindex)); | |
3217 | listnode_add(mq->subq[qindex], rn); | |
3218 | route_lock_node(rn); | |
3219 | mq->size++; | |
3220 | ||
3221 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
3222 | rnode_debug(rn, re->vrf_id, "queued rn %p into sub-queue %s", | |
3223 | (void *)rn, subqueue2str(qindex)); | |
3224 | ||
3225 | return 0; | |
3226 | } | |
3227 | ||
3228 | static int early_label_meta_queue_add(struct meta_queue *mq, void *data) | |
3229 | { | |
3230 | listnode_add(mq->subq[META_QUEUE_EARLY_LABEL], data); | |
3231 | mq->size++; | |
3232 | return 0; | |
3233 | } | |
3234 | ||
3235 | static int rib_meta_queue_nhg_ctx_add(struct meta_queue *mq, void *data) | |
3236 | { | |
3237 | struct nhg_ctx *ctx = NULL; | |
3238 | uint8_t qindex = META_QUEUE_NHG; | |
3239 | struct wq_nhg_wrapper *w; | |
3240 | ||
3241 | ctx = (struct nhg_ctx *)data; | |
3242 | ||
3243 | if (!ctx) | |
3244 | return -1; | |
3245 | ||
3246 | w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_nhg_wrapper)); | |
3247 | ||
3248 | w->type = WQ_NHG_WRAPPER_TYPE_CTX; | |
3249 | w->u.ctx = ctx; | |
3250 | ||
3251 | listnode_add(mq->subq[qindex], w); | |
3252 | mq->size++; | |
3253 | ||
3254 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
3255 | zlog_debug("NHG Context id=%u queued into sub-queue %s", | |
3256 | ctx->id, subqueue2str(qindex)); | |
3257 | ||
3258 | return 0; | |
3259 | } | |
3260 | ||
3261 | static int rib_meta_queue_nhg_add(struct meta_queue *mq, void *data) | |
3262 | { | |
3263 | struct nhg_hash_entry *nhe = NULL; | |
3264 | uint8_t qindex = META_QUEUE_NHG; | |
3265 | struct wq_nhg_wrapper *w; | |
3266 | ||
3267 | nhe = (struct nhg_hash_entry *)data; | |
04bec7b2 MS |
3268 | |
3269 | if (!nhe) | |
3270 | return -1; | |
3271 | ||
32367e7a | 3272 | w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_nhg_wrapper)); |
04bec7b2 MS |
3273 | |
3274 | w->type = WQ_NHG_WRAPPER_TYPE_NHG; | |
3275 | w->u.nhe = nhe; | |
3276 | ||
3277 | listnode_add(mq->subq[qindex], w); | |
3278 | mq->size++; | |
3279 | ||
3280 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
7a96ccc7 DS |
3281 | zlog_debug("NHG id=%u queued into sub-queue %s", nhe->id, |
3282 | subqueue2str(qindex)); | |
04bec7b2 MS |
3283 | |
3284 | return 0; | |
3285 | } | |
3286 | ||
32367e7a MS |
3287 | static int rib_meta_queue_evpn_add(struct meta_queue *mq, void *data) |
3288 | { | |
3289 | listnode_add(mq->subq[META_QUEUE_EVPN], data); | |
3290 | mq->size++; | |
3291 | ||
3292 | return 0; | |
3293 | } | |
3294 | ||
e22e8001 SW |
3295 | static int mq_add_handler(void *data, |
3296 | int (*mq_add_func)(struct meta_queue *mq, void *data)) | |
3297 | { | |
489a9614 | 3298 | if (zrouter.ribq == NULL) { |
e914ccbe | 3299 | flog_err(EC_ZEBRA_WQ_NONEXISTENT, |
1c50c1c0 | 3300 | "%s: work_queue does not exist!", __func__); |
e22e8001 | 3301 | return -1; |
d62a17ae | 3302 | } |
3303 | ||
3304 | /* | |
3305 | * The RIB queue should normally be either empty or holding the only | |
3306 | * work_queue_item element. In the latter case this element would | |
3307 | * hold a pointer to the meta queue structure, which must be used to | |
3308 | * actually queue the route nodes to process. So create the MQ | |
3309 | * holder, if necessary, then push the work into it in any case. | |
3310 | * This semantics was introduced after 0.99.9 release. | |
3311 | */ | |
489a9614 | 3312 | if (work_queue_empty(zrouter.ribq)) |
ea45a4e7 | 3313 | work_queue_add(zrouter.ribq, zrouter.mq); |
e96f9203 | 3314 | |
e22e8001 SW |
3315 | return mq_add_func(zrouter.mq, data); |
3316 | } | |
fc328ac9 | 3317 | |
53216dff DS |
3318 | void mpls_ftn_uninstall(struct zebra_vrf *zvrf, enum lsp_types_t type, |
3319 | struct prefix *prefix, uint8_t route_type, | |
3320 | uint8_t route_instance) | |
3321 | { | |
3322 | struct wq_label_wrapper *w; | |
3323 | ||
3324 | w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_label_wrapper)); | |
3325 | ||
3326 | w->type = WQ_LABEL_FTN_UNINSTALL; | |
3327 | w->vrf_id = zvrf->vrf->vrf_id; | |
3328 | w->p = *prefix; | |
3329 | w->ltype = type; | |
3330 | w->route_type = route_type; | |
3331 | w->route_instance = route_instance; | |
3332 | ||
3333 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
3334 | zlog_debug("Early Label Handling for %pFX", prefix); | |
3335 | ||
3336 | mq_add_handler(w, early_label_meta_queue_add); | |
3337 | } | |
3338 | ||
3339 | void mpls_zapi_labels_process(bool add_p, struct zebra_vrf *zvrf, | |
3340 | const struct zapi_labels *zl) | |
3341 | { | |
3342 | struct wq_label_wrapper *w; | |
3343 | ||
3344 | w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_label_wrapper)); | |
3345 | w->type = WQ_LABEL_LABELS_PROCESS; | |
3346 | w->vrf_id = zvrf->vrf->vrf_id; | |
3347 | w->add_p = add_p; | |
3348 | w->zl = *zl; | |
3349 | ||
3350 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
3351 | zlog_debug("Early Label Handling: Labels Process"); | |
3352 | ||
3353 | mq_add_handler(w, early_label_meta_queue_add); | |
3354 | } | |
3355 | ||
e22e8001 SW |
3356 | /* Add route_node to work queue and schedule processing */ |
3357 | int rib_queue_add(struct route_node *rn) | |
3358 | { | |
3359 | assert(rn); | |
3360 | ||
3361 | /* Pointless to queue a route_node with no RIB entries to add or remove | |
3362 | */ | |
3363 | if (!rnode_to_ribs(rn)) { | |
c10e14e9 DS |
3364 | zlog_debug("%s: called for route_node (%p, %u) with no ribs", |
3365 | __func__, (void *)rn, route_node_get_lock_count(rn)); | |
e22e8001 SW |
3366 | zlog_backtrace(LOG_DEBUG); |
3367 | return -1; | |
3368 | } | |
3369 | ||
04bec7b2 | 3370 | return mq_add_handler(rn, rib_meta_queue_add); |
e22e8001 SW |
3371 | } |
3372 | ||
04bec7b2 MS |
3373 | /* |
3374 | * Enqueue incoming nhg info from OS for processing | |
3375 | */ | |
3376 | int rib_queue_nhg_ctx_add(struct nhg_ctx *ctx) | |
e22e8001 SW |
3377 | { |
3378 | assert(ctx); | |
3379 | ||
04bec7b2 MS |
3380 | return mq_add_handler(ctx, rib_meta_queue_nhg_ctx_add); |
3381 | } | |
3382 | ||
3383 | /* | |
3384 | * Enqueue incoming nhg from proto daemon for processing | |
3385 | */ | |
3386 | int rib_queue_nhe_add(struct nhg_hash_entry *nhe) | |
3387 | { | |
3388 | if (nhe == NULL) | |
3389 | return -1; | |
3390 | ||
3391 | return mq_add_handler(nhe, rib_meta_queue_nhg_add); | |
3392 | } | |
3393 | ||
32367e7a MS |
3394 | /* |
3395 | * Enqueue evpn route for processing | |
3396 | */ | |
3397 | int zebra_rib_queue_evpn_route_add(vrf_id_t vrf_id, const struct ethaddr *rmac, | |
3398 | const struct ipaddr *vtep_ip, | |
3399 | const struct prefix *host_prefix) | |
3400 | { | |
3401 | struct wq_evpn_wrapper *w; | |
3402 | ||
3403 | w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper)); | |
3404 | ||
3405 | w->type = WQ_EVPN_WRAPPER_TYPE_VRFROUTE; | |
3406 | w->add_p = true; | |
3407 | w->vrf_id = vrf_id; | |
7f7e49d1 | 3408 | w->macaddr = *rmac; |
32367e7a MS |
3409 | w->ip = *vtep_ip; |
3410 | w->prefix = *host_prefix; | |
3411 | ||
3412 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
3413 | zlog_debug("%s: (%u)%pIA, host prefix %pFX enqueued", __func__, | |
3414 | vrf_id, vtep_ip, host_prefix); | |
3415 | ||
3416 | return mq_add_handler(w, rib_meta_queue_evpn_add); | |
3417 | } | |
3418 | ||
3419 | int zebra_rib_queue_evpn_route_del(vrf_id_t vrf_id, | |
3420 | const struct ipaddr *vtep_ip, | |
3421 | const struct prefix *host_prefix) | |
3422 | { | |
3423 | struct wq_evpn_wrapper *w; | |
3424 | ||
3425 | w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper)); | |
3426 | ||
3427 | w->type = WQ_EVPN_WRAPPER_TYPE_VRFROUTE; | |
3428 | w->add_p = false; | |
3429 | w->vrf_id = vrf_id; | |
3430 | w->ip = *vtep_ip; | |
3431 | w->prefix = *host_prefix; | |
3432 | ||
3433 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
3434 | zlog_debug("%s: (%u)%pIA, host prefix %pFX enqueued", __func__, | |
3435 | vrf_id, vtep_ip, host_prefix); | |
3436 | ||
3437 | return mq_add_handler(w, rib_meta_queue_evpn_add); | |
3438 | } | |
3439 | ||
3440 | /* Enqueue EVPN remote ES for processing */ | |
3441 | int zebra_rib_queue_evpn_rem_es_add(const esi_t *esi, | |
3442 | const struct in_addr *vtep_ip, | |
3443 | bool esr_rxed, uint8_t df_alg, | |
3444 | uint16_t df_pref) | |
3445 | { | |
3446 | struct wq_evpn_wrapper *w; | |
3447 | char buf[ESI_STR_LEN]; | |
3448 | ||
3449 | w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper)); | |
3450 | ||
3451 | w->type = WQ_EVPN_WRAPPER_TYPE_REM_ES; | |
3452 | w->add_p = true; | |
3453 | w->esi = *esi; | |
3454 | w->ip.ipa_type = IPADDR_V4; | |
3455 | w->ip.ipaddr_v4 = *vtep_ip; | |
3456 | w->esr_rxed = esr_rxed; | |
3457 | w->df_alg = df_alg; | |
3458 | w->df_pref = df_pref; | |
3459 | ||
3460 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
3461 | zlog_debug("%s: vtep %pI4, esi %s enqueued", __func__, vtep_ip, | |
3462 | esi_to_str(esi, buf, sizeof(buf))); | |
3463 | ||
3464 | return mq_add_handler(w, rib_meta_queue_evpn_add); | |
3465 | } | |
3466 | ||
3467 | int zebra_rib_queue_evpn_rem_es_del(const esi_t *esi, | |
3468 | const struct in_addr *vtep_ip) | |
3469 | { | |
3470 | struct wq_evpn_wrapper *w; | |
3471 | char buf[ESI_STR_LEN]; | |
3472 | ||
3473 | w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper)); | |
3474 | ||
3475 | w->type = WQ_EVPN_WRAPPER_TYPE_REM_ES; | |
3476 | w->add_p = false; | |
3477 | w->esi = *esi; | |
3478 | w->ip.ipa_type = IPADDR_V4; | |
3479 | w->ip.ipaddr_v4 = *vtep_ip; | |
3480 | ||
3481 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) { | |
3482 | if (memcmp(esi, zero_esi, sizeof(esi_t)) != 0) | |
3483 | esi_to_str(esi, buf, sizeof(buf)); | |
3484 | else | |
3485 | strlcpy(buf, "-", sizeof(buf)); | |
3486 | ||
3487 | zlog_debug("%s: vtep %pI4, esi %s enqueued", __func__, vtep_ip, | |
3488 | buf); | |
3489 | } | |
3490 | ||
3491 | return mq_add_handler(w, rib_meta_queue_evpn_add); | |
3492 | } | |
3493 | ||
7f7e49d1 MS |
3494 | /* |
3495 | * Enqueue EVPN remote macip update for processing | |
3496 | */ | |
3497 | int zebra_rib_queue_evpn_rem_macip_add(vni_t vni, const struct ethaddr *macaddr, | |
3498 | const struct ipaddr *ipaddr, | |
3499 | uint8_t flags, uint32_t seq, | |
3500 | struct in_addr vtep_ip, const esi_t *esi) | |
3501 | { | |
3502 | struct wq_evpn_wrapper *w; | |
3503 | char buf[ESI_STR_LEN]; | |
3504 | ||
3505 | w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper)); | |
3506 | ||
3507 | w->type = WQ_EVPN_WRAPPER_TYPE_REM_MACIP; | |
3508 | w->add_p = true; | |
3509 | w->vni = vni; | |
3510 | w->macaddr = *macaddr; | |
3511 | w->ip = *ipaddr; | |
3512 | w->flags = flags; | |
3513 | w->seq = seq; | |
3514 | w->vtep_ip = vtep_ip; | |
3515 | w->esi = *esi; | |
3516 | ||
3517 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) { | |
3518 | if (memcmp(esi, zero_esi, sizeof(esi_t)) != 0) | |
3519 | esi_to_str(esi, buf, sizeof(buf)); | |
3520 | else | |
3521 | strlcpy(buf, "-", sizeof(buf)); | |
3522 | ||
3523 | zlog_debug("%s: mac %pEA, vtep %pI4, esi %s enqueued", __func__, | |
3524 | macaddr, &vtep_ip, buf); | |
3525 | } | |
3526 | ||
3527 | return mq_add_handler(w, rib_meta_queue_evpn_add); | |
3528 | } | |
3529 | ||
3530 | int zebra_rib_queue_evpn_rem_macip_del(vni_t vni, const struct ethaddr *macaddr, | |
3531 | const struct ipaddr *ip, | |
3532 | struct in_addr vtep_ip) | |
3533 | { | |
3534 | struct wq_evpn_wrapper *w; | |
3535 | ||
3536 | w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper)); | |
3537 | ||
3538 | w->type = WQ_EVPN_WRAPPER_TYPE_REM_MACIP; | |
3539 | w->add_p = false; | |
3540 | w->vni = vni; | |
3541 | w->macaddr = *macaddr; | |
3542 | w->ip = *ip; | |
3543 | w->vtep_ip = vtep_ip; | |
3544 | ||
3545 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
3546 | zlog_debug("%s: mac %pEA, vtep %pI4 enqueued", __func__, | |
3547 | macaddr, &vtep_ip); | |
3548 | ||
3549 | return mq_add_handler(w, rib_meta_queue_evpn_add); | |
3550 | } | |
3551 | ||
7e5b0b2b MS |
3552 | /* |
3553 | * Enqueue remote VTEP address for processing | |
3554 | */ | |
3555 | int zebra_rib_queue_evpn_rem_vtep_add(vrf_id_t vrf_id, vni_t vni, | |
3556 | struct in_addr vtep_ip, int flood_control) | |
3557 | { | |
3558 | struct wq_evpn_wrapper *w; | |
3559 | ||
3560 | w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper)); | |
3561 | ||
3562 | w->type = WQ_EVPN_WRAPPER_TYPE_REM_VTEP; | |
3563 | w->add_p = true; | |
3564 | w->vrf_id = vrf_id; | |
3565 | w->vni = vni; | |
3566 | w->vtep_ip = vtep_ip; | |
3567 | w->flags = flood_control; | |
3568 | ||
3569 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
3570 | zlog_debug("%s: vrf %u, vtep %pI4 enqueued", __func__, vrf_id, | |
3571 | &vtep_ip); | |
3572 | ||
3573 | return mq_add_handler(w, rib_meta_queue_evpn_add); | |
3574 | } | |
3575 | ||
3576 | int zebra_rib_queue_evpn_rem_vtep_del(vrf_id_t vrf_id, vni_t vni, | |
3577 | struct in_addr vtep_ip) | |
3578 | { | |
3579 | struct wq_evpn_wrapper *w; | |
3580 | ||
3581 | w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper)); | |
3582 | ||
3583 | w->type = WQ_EVPN_WRAPPER_TYPE_REM_VTEP; | |
3584 | w->add_p = false; | |
3585 | w->vrf_id = vrf_id; | |
3586 | w->vni = vni; | |
3587 | w->vtep_ip = vtep_ip; | |
3588 | ||
3589 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
3590 | zlog_debug("%s: vrf %u, vtep %pI4 enqueued", __func__, vrf_id, | |
3591 | &vtep_ip); | |
3592 | ||
3593 | return mq_add_handler(w, rib_meta_queue_evpn_add); | |
3594 | } | |
3595 | ||
a310ebc1 DS |
3596 | /* Create new meta queue. |
3597 | A destructor function doesn't seem to be necessary here. | |
3598 | */ | |
3599 | static struct meta_queue *meta_queue_new(void) | |
3600 | { | |
3601 | struct meta_queue *new; | |
3602 | unsigned i; | |
3603 | ||
3604 | new = XCALLOC(MTYPE_WORK_QUEUE, sizeof(struct meta_queue)); | |
3605 | ||
3606 | for (i = 0; i < MQ_SIZE; i++) { | |
3607 | new->subq[i] = list_new(); | |
3608 | assert(new->subq[i]); | |
3609 | } | |
3610 | ||
3611 | return new; | |
3612 | } | |
3613 | ||
32367e7a | 3614 | /* Clean up the EVPN meta-queue list */ |
a310ebc1 DS |
3615 | static void evpn_meta_queue_free(struct meta_queue *mq, struct list *l, |
3616 | struct zebra_vrf *zvrf) | |
32367e7a | 3617 | { |
a310ebc1 | 3618 | struct listnode *node, *nnode; |
32367e7a MS |
3619 | struct wq_evpn_wrapper *w; |
3620 | ||
3621 | /* Free the node wrapper object, and the struct it wraps */ | |
a310ebc1 DS |
3622 | for (ALL_LIST_ELEMENTS(l, node, nnode, w)) { |
3623 | if (zvrf) { | |
3624 | vrf_id_t vrf_id = zvrf->vrf->vrf_id; | |
3625 | ||
3626 | if (w->vrf_id != vrf_id) | |
3627 | continue; | |
3628 | } | |
3629 | ||
32367e7a MS |
3630 | node->data = NULL; |
3631 | ||
3632 | XFREE(MTYPE_WQ_WRAPPER, w); | |
3633 | ||
3634 | list_delete_node(l, node); | |
a310ebc1 | 3635 | mq->size--; |
32367e7a MS |
3636 | } |
3637 | } | |
3638 | ||
04bec7b2 | 3639 | /* Clean up the nhg meta-queue list */ |
a310ebc1 DS |
3640 | static void nhg_meta_queue_free(struct meta_queue *mq, struct list *l, |
3641 | struct zebra_vrf *zvrf) | |
04bec7b2 MS |
3642 | { |
3643 | struct wq_nhg_wrapper *w; | |
a310ebc1 | 3644 | struct listnode *node, *nnode; |
04bec7b2 MS |
3645 | |
3646 | /* Free the node wrapper object, and the struct it wraps */ | |
a310ebc1 DS |
3647 | for (ALL_LIST_ELEMENTS(l, node, nnode, w)) { |
3648 | if (zvrf) { | |
3649 | vrf_id_t vrf_id = zvrf->vrf->vrf_id; | |
04bec7b2 | 3650 | |
a310ebc1 DS |
3651 | if (w->type == WQ_NHG_WRAPPER_TYPE_CTX && |
3652 | w->u.ctx->vrf_id != vrf_id) | |
3653 | continue; | |
3654 | else if (w->type == WQ_NHG_WRAPPER_TYPE_NHG && | |
3655 | w->u.nhe->vrf_id != vrf_id) | |
3656 | continue; | |
3657 | } | |
04bec7b2 MS |
3658 | if (w->type == WQ_NHG_WRAPPER_TYPE_CTX) |
3659 | nhg_ctx_free(&w->u.ctx); | |
3660 | else if (w->type == WQ_NHG_WRAPPER_TYPE_NHG) | |
3661 | zebra_nhg_free(w->u.nhe); | |
3662 | ||
a310ebc1 | 3663 | node->data = NULL; |
32367e7a | 3664 | XFREE(MTYPE_WQ_WRAPPER, w); |
04bec7b2 MS |
3665 | |
3666 | list_delete_node(l, node); | |
a310ebc1 | 3667 | mq->size--; |
04bec7b2 | 3668 | } |
4d38fdb4 | 3669 | } |
3670 | ||
53216dff DS |
3671 | static void early_label_meta_queue_free(struct meta_queue *mq, struct list *l, |
3672 | struct zebra_vrf *zvrf) | |
3673 | { | |
3674 | struct wq_label_wrapper *w; | |
3675 | struct listnode *node, *nnode; | |
3676 | ||
3677 | for (ALL_LIST_ELEMENTS(l, node, nnode, w)) { | |
3678 | if (zvrf && zvrf->vrf->vrf_id != w->vrf_id) | |
3679 | continue; | |
3680 | ||
3681 | switch (w->type) { | |
3682 | case WQ_LABEL_FTN_UNINSTALL: | |
3683 | case WQ_LABEL_LABELS_PROCESS: | |
3684 | break; | |
3685 | } | |
3686 | ||
3687 | node->data = NULL; | |
3688 | XFREE(MTYPE_WQ_WRAPPER, w); | |
3689 | list_delete_node(l, node); | |
3690 | mq->size--; | |
3691 | } | |
3692 | } | |
3693 | ||
a310ebc1 DS |
3694 | static void rib_meta_queue_free(struct meta_queue *mq, struct list *l, |
3695 | struct zebra_vrf *zvrf) | |
e96f9203 | 3696 | { |
a310ebc1 DS |
3697 | struct route_node *rnode; |
3698 | struct listnode *node, *nnode; | |
5110a0c6 | 3699 | |
a310ebc1 DS |
3700 | for (ALL_LIST_ELEMENTS(l, node, nnode, rnode)) { |
3701 | rib_dest_t *dest = rib_dest_from_rnode(rnode); | |
e96f9203 | 3702 | |
a310ebc1 DS |
3703 | if (dest && rib_dest_vrf(dest) != zvrf) |
3704 | continue; | |
5110a0c6 | 3705 | |
a310ebc1 DS |
3706 | route_unlock_node(rnode); |
3707 | node->data = NULL; | |
3708 | list_delete_node(l, node); | |
3709 | mq->size--; | |
3710 | } | |
e96f9203 DO |
3711 | } |
3712 | ||
d7ac4c4d DS |
3713 | static void early_route_meta_queue_free(struct meta_queue *mq, struct list *l, |
3714 | struct zebra_vrf *zvrf) | |
3715 | { | |
739bc9fd | 3716 | struct zebra_early_route *ere; |
d7ac4c4d DS |
3717 | struct listnode *node, *nnode; |
3718 | ||
739bc9fd DS |
3719 | for (ALL_LIST_ELEMENTS(l, node, nnode, ere)) { |
3720 | if (zvrf && ere->re->vrf_id != zvrf->vrf->vrf_id) | |
d7ac4c4d DS |
3721 | continue; |
3722 | ||
739bc9fd | 3723 | early_route_memory_free(ere); |
d7ac4c4d DS |
3724 | node->data = NULL; |
3725 | list_delete_node(l, node); | |
3726 | mq->size--; | |
3727 | } | |
3728 | } | |
a310ebc1 DS |
3729 | |
3730 | void meta_queue_free(struct meta_queue *mq, struct zebra_vrf *zvrf) | |
5a8dfcd8 | 3731 | { |
014e732d | 3732 | enum meta_queue_indexes i; |
5a8dfcd8 | 3733 | |
04bec7b2 MS |
3734 | for (i = 0; i < MQ_SIZE; i++) { |
3735 | /* Some subqueues may need cleanup - nhgs for example */ | |
014e732d DS |
3736 | switch (i) { |
3737 | case META_QUEUE_NHG: | |
a310ebc1 | 3738 | nhg_meta_queue_free(mq, mq->subq[i], zvrf); |
014e732d DS |
3739 | break; |
3740 | case META_QUEUE_EVPN: | |
a310ebc1 | 3741 | evpn_meta_queue_free(mq, mq->subq[i], zvrf); |
014e732d | 3742 | break; |
d7ac4c4d DS |
3743 | case META_QUEUE_EARLY_ROUTE: |
3744 | early_route_meta_queue_free(mq, mq->subq[i], zvrf); | |
3745 | break; | |
53216dff DS |
3746 | case META_QUEUE_EARLY_LABEL: |
3747 | early_label_meta_queue_free(mq, mq->subq[i], zvrf); | |
3748 | break; | |
014e732d DS |
3749 | case META_QUEUE_CONNECTED: |
3750 | case META_QUEUE_KERNEL: | |
3751 | case META_QUEUE_STATIC: | |
3752 | case META_QUEUE_NOTBGP: | |
3753 | case META_QUEUE_BGP: | |
3754 | case META_QUEUE_OTHER: | |
a310ebc1 | 3755 | rib_meta_queue_free(mq, mq->subq[i], zvrf); |
014e732d DS |
3756 | break; |
3757 | } | |
a310ebc1 DS |
3758 | if (!zvrf) |
3759 | list_delete(&mq->subq[i]); | |
04bec7b2 | 3760 | } |
5a8dfcd8 | 3761 | |
a310ebc1 DS |
3762 | if (!zvrf) |
3763 | XFREE(MTYPE_WORK_QUEUE, mq); | |
0ef6eacc IR |
3764 | } |
3765 | ||
4d38fdb4 | 3766 | /* initialise zebra rib work queue */ |
2561d12e | 3767 | static void rib_queue_init(void) |
4d38fdb4 | 3768 | { |
489a9614 DS |
3769 | if (!(zrouter.ribq = work_queue_new(zrouter.master, |
3770 | "route_node processing"))) { | |
e914ccbe | 3771 | flog_err(EC_ZEBRA_WQ_NONEXISTENT, |
1c50c1c0 | 3772 | "%s: could not initialise work queue!", __func__); |
d62a17ae | 3773 | return; |
3774 | } | |
4d38fdb4 | 3775 | |
d62a17ae | 3776 | /* fill in the work queue spec */ |
489a9614 | 3777 | zrouter.ribq->spec.workfunc = &meta_queue_process; |
46a4e345 | 3778 | zrouter.ribq->spec.completion_func = NULL; |
d62a17ae | 3779 | /* XXX: TODO: These should be runtime configurable via vty */ |
489a9614 DS |
3780 | zrouter.ribq->spec.max_retries = 3; |
3781 | zrouter.ribq->spec.hold = ZEBRA_RIB_PROCESS_HOLD_TIME; | |
3782 | zrouter.ribq->spec.retry = ZEBRA_RIB_PROCESS_RETRY_TIME; | |
d62a17ae | 3783 | |
ea45a4e7 | 3784 | if (!(zrouter.mq = meta_queue_new())) { |
e914ccbe | 3785 | flog_err(EC_ZEBRA_WQ_NONEXISTENT, |
1c50c1c0 | 3786 | "%s: could not initialise meta queue!", __func__); |
d62a17ae | 3787 | return; |
3788 | } | |
3789 | return; | |
718e3744 | 3790 | } |
3791 | ||
434434f7 DS |
3792 | rib_dest_t *zebra_rib_create_dest(struct route_node *rn) |
3793 | { | |
3794 | rib_dest_t *dest; | |
3795 | ||
3796 | dest = XCALLOC(MTYPE_RIB_DEST, sizeof(rib_dest_t)); | |
aa57abfb | 3797 | rnh_list_init(&dest->nht); |
daaeaa21 | 3798 | re_list_init(&dest->routes); |
434434f7 DS |
3799 | route_lock_node(rn); /* rn route table reference */ |
3800 | rn->info = dest; | |
3801 | dest->rnode = rn; | |
3802 | ||
3803 | return dest; | |
3804 | } | |
3805 | ||
6d691129 PJ |
3806 | /* RIB updates are processed via a queue of pointers to route_nodes. |
3807 | * | |
3808 | * The queue length is bounded by the maximal size of the routing table, | |
3809 | * as a route_node will not be requeued, if already queued. | |
3810 | * | |
f0f77c9a DS |
3811 | * REs are submitted via rib_addnode or rib_delnode which set minimal |
3812 | * state, or static_install_route (when an existing RE is updated) | |
3c0755dc | 3813 | * and then submit route_node to queue for best-path selection later. |
f0f77c9a | 3814 | * Order of add/delete state changes are preserved for any given RE. |
6d691129 | 3815 | * |
f0f77c9a | 3816 | * Deleted REs are reaped during best-path selection. |
6d691129 PJ |
3817 | * |
3818 | * rib_addnode | |
f0f77c9a DS |
3819 | * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with |
3820 | * |-------->| | best RE, if required | |
3c0755dc PJ |
3821 | * | | |
3822 | * static_install->|->rib_addqueue...... -> rib_process | |
3823 | * | | | |
3824 | * |-------->| |-> rib_unlink | |
f0f77c9a DS |
3825 | * |-> set ROUTE_ENTRY_REMOVE | |
3826 | * rib_delnode (RE freed) | |
6d691129 | 3827 | * |
9fd92e3c AS |
3828 | * The 'info' pointer of a route_node points to a rib_dest_t |
3829 | * ('dest'). Queueing state for a route_node is kept on the dest. The | |
3830 | * dest is created on-demand by rib_link() and is kept around at least | |
3831 | * as long as there are ribs hanging off it (@see rib_gc_dest()). | |
d62a17ae | 3832 | * |
32367e7a | 3833 | * Refcounting (aka "locking" throughout the Zebra and FRR code): |
6d691129 PJ |
3834 | * |
3835 | * - route_nodes: refcounted by: | |
9fd92e3c AS |
3836 | * - dest attached to route_node: |
3837 | * - managed by: rib_link/rib_gc_dest | |
6d691129 PJ |
3838 | * - route_node processing queue |
3839 | * - managed by: rib_addqueue, rib_process. | |
3840 | * | |
3841 | */ | |
d62a17ae | 3842 | |
f0f77c9a | 3843 | /* Add RE to head of the route node. */ |
d62a17ae | 3844 | static void rib_link(struct route_node *rn, struct route_entry *re, int process) |
3845 | { | |
d62a17ae | 3846 | rib_dest_t *dest; |
3847 | afi_t afi; | |
3848 | const char *rmap_name; | |
9fd92e3c | 3849 | |
d62a17ae | 3850 | assert(re && rn); |
9fd92e3c | 3851 | |
d62a17ae | 3852 | dest = rib_dest_from_rnode(rn); |
3853 | if (!dest) { | |
3854 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
3855 | rnode_debug(rn, re->vrf_id, "rn %p adding dest", rn); | |
7a4bb9c5 | 3856 | |
434434f7 | 3857 | dest = zebra_rib_create_dest(rn); |
d62a17ae | 3858 | } |
2263a412 | 3859 | |
045207e2 | 3860 | re_list_add_head(&dest->routes, re); |
d62a17ae | 3861 | |
3862 | afi = (rn->p.family == AF_INET) | |
3863 | ? AFI_IP | |
3864 | : (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX; | |
fe257ae7 DS |
3865 | if (is_zebra_import_table_enabled(afi, re->vrf_id, re->table)) { |
3866 | struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(re->vrf_id); | |
3867 | ||
d62a17ae | 3868 | rmap_name = zebra_get_import_table_route_map(afi, re->table); |
fe257ae7 | 3869 | zebra_add_import_table_entry(zvrf, rn, re, rmap_name); |
207207c0 S |
3870 | } |
3871 | ||
3872 | if (process) | |
d62a17ae | 3873 | rib_queue_add(rn); |
3874 | } | |
3875 | ||
7e24fdf3 DS |
3876 | static void rib_addnode(struct route_node *rn, |
3877 | struct route_entry *re, int process) | |
d62a17ae | 3878 | { |
3879 | /* RE node has been un-removed before route-node is processed. | |
3880 | * route_node must hence already be on the queue for processing.. | |
3881 | */ | |
3882 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) { | |
3883 | if (IS_ZEBRA_DEBUG_RIB) | |
3884 | rnode_debug(rn, re->vrf_id, "rn %p, un-removed re %p", | |
3885 | (void *)rn, (void *)re); | |
3886 | ||
3887 | UNSET_FLAG(re->status, ROUTE_ENTRY_REMOVED); | |
3888 | return; | |
3889 | } | |
3890 | rib_link(rn, re, process); | |
6d691129 PJ |
3891 | } |
3892 | ||
9fd92e3c AS |
3893 | /* |
3894 | * rib_unlink | |
3895 | * | |
3896 | * Detach a rib structure from a route_node. | |
3897 | * | |
3898 | * Note that a call to rib_unlink() should be followed by a call to | |
3899 | * rib_gc_dest() at some point. This allows a rib_dest_t that is no | |
3900 | * longer required to be deleted. | |
3901 | */ | |
d62a17ae | 3902 | void rib_unlink(struct route_node *rn, struct route_entry *re) |
6d691129 | 3903 | { |
d62a17ae | 3904 | rib_dest_t *dest; |
9fd92e3c | 3905 | |
d62a17ae | 3906 | assert(rn && re); |
6d691129 | 3907 | |
d62a17ae | 3908 | if (IS_ZEBRA_DEBUG_RIB) |
3909 | rnode_debug(rn, re->vrf_id, "rn %p, re %p", (void *)rn, | |
3910 | (void *)re); | |
6d691129 | 3911 | |
d62a17ae | 3912 | dest = rib_dest_from_rnode(rn); |
6d691129 | 3913 | |
045207e2 | 3914 | re_list_del(&dest->routes, re); |
7a4bb9c5 | 3915 | |
2eb07de3 DS |
3916 | if (dest->selected_fib == re) |
3917 | dest->selected_fib = NULL; | |
3918 | ||
ce649b9d | 3919 | rib_re_nhg_free(re); |
ee5e8a48 | 3920 | |
b86c1f4f | 3921 | zapi_re_opaque_free(re->opaque); |
a29a6001 | 3922 | |
d62a17ae | 3923 | XFREE(MTYPE_RE, re); |
3924 | } | |
3925 | ||
3926 | void rib_delnode(struct route_node *rn, struct route_entry *re) | |
3927 | { | |
3928 | afi_t afi; | |
3929 | ||
3930 | if (IS_ZEBRA_DEBUG_RIB) | |
3931 | rnode_debug(rn, re->vrf_id, "rn %p, re %p, removing", | |
3932 | (void *)rn, (void *)re); | |
3933 | SET_FLAG(re->status, ROUTE_ENTRY_REMOVED); | |
3934 | ||
3935 | afi = (rn->p.family == AF_INET) | |
3936 | ? AFI_IP | |
3937 | : (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX; | |
fe257ae7 DS |
3938 | if (is_zebra_import_table_enabled(afi, re->vrf_id, re->table)) { |
3939 | struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(re->vrf_id); | |
3940 | ||
3941 | zebra_del_import_table_entry(zvrf, rn, re); | |
d62a17ae | 3942 | /* Just clean up if non main table */ |
e658173a DS |
3943 | if (IS_ZEBRA_DEBUG_RIB) |
3944 | zlog_debug("%s(%u):%pRN: Freeing route rn %p, re %p (%s)", | |
3945 | vrf_id_to_name(re->vrf_id), re->vrf_id, rn, | |
bd47f3a3 | 3946 | rn, re, zebra_route_string(re->type)); |
d62a17ae | 3947 | } |
207207c0 S |
3948 | |
3949 | rib_queue_add(rn); | |
718e3744 | 3950 | } |
3951 | ||
6d81b590 MS |
3952 | /* |
3953 | * Helper that debugs a single nexthop within a route-entry | |
3954 | */ | |
3955 | static void _route_entry_dump_nh(const struct route_entry *re, | |
3956 | const char *straddr, | |
3957 | const struct nexthop *nexthop) | |
3958 | { | |
3959 | char nhname[PREFIX_STRLEN]; | |
3960 | char backup_str[50]; | |
3961 | char wgt_str[50]; | |
474aebd9 | 3962 | char temp_str[10]; |
cbe5bafb | 3963 | char label_str[MPLS_LABEL_STRLEN]; |
474aebd9 | 3964 | int i; |
6d81b590 MS |
3965 | struct interface *ifp; |
3966 | struct vrf *vrf = vrf_lookup_by_id(nexthop->vrf_id); | |
3967 | ||
3968 | switch (nexthop->type) { | |
3969 | case NEXTHOP_TYPE_BLACKHOLE: | |
772270f3 | 3970 | snprintf(nhname, sizeof(nhname), "Blackhole"); |
6d81b590 MS |
3971 | break; |
3972 | case NEXTHOP_TYPE_IFINDEX: | |
3973 | ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id); | |
772270f3 QY |
3974 | snprintf(nhname, sizeof(nhname), "%s", |
3975 | ifp ? ifp->name : "Unknown"); | |
6d81b590 MS |
3976 | break; |
3977 | case NEXTHOP_TYPE_IPV4: | |
3978 | /* fallthrough */ | |
3979 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
3980 | inet_ntop(AF_INET, &nexthop->gate, nhname, INET6_ADDRSTRLEN); | |
3981 | break; | |
3982 | case NEXTHOP_TYPE_IPV6: | |
3983 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
3984 | inet_ntop(AF_INET6, &nexthop->gate, nhname, INET6_ADDRSTRLEN); | |
3985 | break; | |
3986 | } | |
3987 | ||
cbe5bafb MS |
3988 | /* Label stack */ |
3989 | label_str[0] = '\0'; | |
3990 | if (nexthop->nh_label && nexthop->nh_label->num_labels > 0) { | |
3991 | mpls_label2str(nexthop->nh_label->num_labels, | |
3992 | nexthop->nh_label->label, label_str, | |
4645cb6b SW |
3993 | sizeof(label_str), nexthop->nh_label_type, |
3994 | 0 /*pretty*/); | |
cbe5bafb MS |
3995 | strlcat(label_str, ", ", sizeof(label_str)); |
3996 | } | |
3997 | ||
6d81b590 MS |
3998 | backup_str[0] = '\0'; |
3999 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP)) { | |
474aebd9 MS |
4000 | snprintf(backup_str, sizeof(backup_str), "backup "); |
4001 | for (i = 0; i < nexthop->backup_num; i++) { | |
4002 | snprintf(temp_str, sizeof(temp_str), "%d, ", | |
4003 | nexthop->backup_idx[i]); | |
4004 | strlcat(backup_str, temp_str, sizeof(backup_str)); | |
4005 | } | |
6d81b590 MS |
4006 | } |
4007 | ||
4008 | wgt_str[0] = '\0'; | |
4009 | if (nexthop->weight) | |
4010 | snprintf(wgt_str, sizeof(wgt_str), "wgt %d,", nexthop->weight); | |
4011 | ||
5609e70f | 4012 | zlog_debug("%s: %s %s[%u] %svrf %s(%u) %s%s with flags %s%s%s%s%s%s%s%s%s", |
6d81b590 | 4013 | straddr, (nexthop->rparent ? " NH" : "NH"), nhname, |
cbe5bafb | 4014 | nexthop->ifindex, label_str, vrf ? vrf->name : "Unknown", |
6d81b590 MS |
4015 | nexthop->vrf_id, |
4016 | wgt_str, backup_str, | |
4017 | (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE) | |
4018 | ? "ACTIVE " | |
4019 | : ""), | |
4020 | (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED) | |
4021 | ? "FIB " | |
4022 | : ""), | |
4023 | (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE) | |
4024 | ? "RECURSIVE " | |
4025 | : ""), | |
4026 | (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK) | |
4027 | ? "ONLINK " | |
4028 | : ""), | |
4029 | (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_DUPLICATE) | |
4030 | ? "DUPLICATE " | |
aab4eca1 DS |
4031 | : ""), |
4032 | (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RNH_FILTERED) | |
4033 | ? "FILTERED " : ""), | |
4034 | (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP) | |
4035 | ? "BACKUP " : ""), | |
4036 | (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_SRTE) | |
5609e70f XL |
4037 | ? "SRTE " : ""), |
4038 | (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_EVPN) | |
4039 | ? "EVPN " : "")); | |
6d81b590 MS |
4040 | |
4041 | } | |
4042 | ||
f0f77c9a | 4043 | /* This function dumps the contents of a given RE entry into |
dc95824a DO |
4044 | * standard debug log. Calling function name and IP prefix in |
4045 | * question are passed as 1st and 2nd arguments. | |
4046 | */ | |
d62a17ae | 4047 | void _route_entry_dump(const char *func, union prefixconstptr pp, |
4048 | union prefixconstptr src_pp, | |
4049 | const struct route_entry *re) | |
4050 | { | |
d62a17ae | 4051 | const struct prefix *src_p = src_pp.p; |
4052 | bool is_srcdst = src_p && src_p->prefixlen; | |
4053 | char straddr[PREFIX_STRLEN]; | |
4054 | char srcaddr[PREFIX_STRLEN]; | |
61e6de9d DS |
4055 | char flags_buf[128]; |
4056 | char status_buf[128]; | |
d62a17ae | 4057 | struct nexthop *nexthop; |
c479e756 | 4058 | struct vrf *vrf = vrf_lookup_by_id(re->vrf_id); |
6d81b590 | 4059 | struct nexthop_group *nhg; |
d62a17ae | 4060 | |
5d27875b DL |
4061 | prefix2str(pp, straddr, sizeof(straddr)); |
4062 | ||
c479e756 | 4063 | zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %s(%u)", func, |
5d27875b | 4064 | (const void *)re, straddr, |
d62a17ae | 4065 | is_srcdst ? " from " : "", |
4066 | is_srcdst ? prefix2str(src_pp, srcaddr, sizeof(srcaddr)) | |
4067 | : "", | |
c479e756 | 4068 | VRF_LOGNAME(vrf), re->vrf_id); |
cc54cfee | 4069 | zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d", |
53c16fbe | 4070 | straddr, (unsigned long)re->uptime, re->type, re->instance, |
cc54cfee | 4071 | re->table); |
61e6de9d DS |
4072 | zlog_debug( |
4073 | "%s: metric == %u, mtu == %u, distance == %u, flags == %sstatus == %s", | |
4074 | straddr, re->metric, re->mtu, re->distance, | |
4075 | zclient_dump_route_flags(re->flags, flags_buf, | |
4076 | sizeof(flags_buf)), | |
4077 | _dump_re_status(re, status_buf, sizeof(status_buf))); | |
53c16fbe | 4078 | zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", straddr, |
c415d895 MS |
4079 | nexthop_group_nexthop_num(&(re->nhe->nhg)), |
4080 | nexthop_group_active_nexthop_num(&(re->nhe->nhg))); | |
d62a17ae | 4081 | |
6d81b590 MS |
4082 | /* Dump nexthops */ |
4083 | for (ALL_NEXTHOPS(re->nhe->nhg, nexthop)) | |
4084 | _route_entry_dump_nh(re, straddr, nexthop); | |
2d68a0f2 | 4085 | |
d7ac4c4d DS |
4086 | if (zebra_nhg_get_backup_nhg(re->nhe)) { |
4087 | zlog_debug("%s: backup nexthops:", straddr); | |
c6eee91f | 4088 | |
d7ac4c4d DS |
4089 | nhg = zebra_nhg_get_backup_nhg(re->nhe); |
4090 | for (ALL_NEXTHOPS_PTR(nhg, nexthop)) | |
4091 | _route_entry_dump_nh(re, straddr, nexthop); | |
c6eee91f DS |
4092 | } |
4093 | ||
d7ac4c4d DS |
4094 | zlog_debug("%s: dump complete", straddr); |
4095 | } | |
cf363e1b | 4096 | |
d7ac4c4d DS |
4097 | static int rib_meta_queue_early_route_add(struct meta_queue *mq, void *data) |
4098 | { | |
4099 | struct zebra_early_route *ere = data; | |
718e3744 | 4100 | |
d7ac4c4d DS |
4101 | listnode_add(mq->subq[META_QUEUE_EARLY_ROUTE], data); |
4102 | mq->size++; | |
6883bf8d | 4103 | |
d7ac4c4d DS |
4104 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) |
4105 | zlog_debug( | |
4106 | "Route %pFX(%u) queued for processing into sub-queue %s", | |
4107 | &ere->p, ere->re->vrf_id, | |
4108 | subqueue2str(META_QUEUE_EARLY_ROUTE)); | |
6b0655a2 | 4109 | |
d7ac4c4d DS |
4110 | return 0; |
4111 | } | |
718e3744 | 4112 | |
b0385873 DS |
4113 | struct route_entry *zebra_rib_route_entry_new(vrf_id_t vrf_id, int type, |
4114 | uint8_t instance, uint32_t flags, | |
4115 | uint32_t nhe_id, | |
4116 | uint32_t table_id, | |
4117 | uint32_t metric, uint32_t mtu, | |
4118 | uint8_t distance, route_tag_t tag) | |
4119 | { | |
4120 | struct route_entry *re; | |
4121 | ||
4122 | re = XCALLOC(MTYPE_RE, sizeof(struct route_entry)); | |
4123 | re->type = type; | |
4124 | re->instance = instance; | |
4125 | re->distance = distance; | |
4126 | re->flags = flags; | |
4127 | re->metric = metric; | |
4128 | re->mtu = mtu; | |
4129 | re->table = table_id; | |
4130 | re->vrf_id = vrf_id; | |
4131 | re->uptime = monotime(NULL); | |
4132 | re->tag = tag; | |
4133 | re->nhe_id = nhe_id; | |
4134 | ||
4135 | return re; | |
4136 | } | |
d7ac4c4d DS |
4137 | /* |
4138 | * Internal route-add implementation; there are a couple of different public | |
4139 | * signatures. Callers in this path are responsible for the memory they | |
4140 | * allocate: if they allocate a nexthop_group or backup nexthop info, they | |
4141 | * must free those objects. If this returns < 0, an error has occurred and the | |
4142 | * route_entry 're' has not been captured; the caller should free that also. | |
4143 | * | |
4144 | * -1 -> error | |
4145 | * 0 -> Add | |
4146 | * 1 -> update | |
4147 | */ | |
4148 | int rib_add_multipath_nhe(afi_t afi, safi_t safi, struct prefix *p, | |
4149 | struct prefix_ipv6 *src_p, struct route_entry *re, | |
4150 | struct nhg_hash_entry *re_nhe, bool startup) | |
4151 | { | |
4152 | struct zebra_early_route *ere; | |
0ca6f3b1 | 4153 | |
d7ac4c4d DS |
4154 | if (!re) |
4155 | return -1; | |
0ca6f3b1 | 4156 | |
d7ac4c4d | 4157 | assert(!src_p || !src_p->prefixlen || afi == AFI_IP6); |
0ca6f3b1 | 4158 | |
d7ac4c4d DS |
4159 | ere = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(*ere)); |
4160 | ere->afi = afi; | |
4161 | ere->safi = safi; | |
4162 | ere->p = *p; | |
4163 | if (src_p) | |
4164 | ere->src_p = *src_p; | |
4165 | ere->src_p_provided = !!src_p; | |
4166 | ere->re = re; | |
4167 | ere->re_nhe = re_nhe; | |
4168 | ere->startup = startup; | |
4169 | ||
4170 | return mq_add_handler(ere, rib_meta_queue_early_route_add); | |
d62a17ae | 4171 | } |
4172 | ||
1d48702e MS |
4173 | /* |
4174 | * Add a single route. | |
4175 | */ | |
4176 | int rib_add_multipath(afi_t afi, safi_t safi, struct prefix *p, | |
4177 | struct prefix_ipv6 *src_p, struct route_entry *re, | |
c6eee91f | 4178 | struct nexthop_group *ng, bool startup) |
1d48702e MS |
4179 | { |
4180 | int ret; | |
d7ac4c4d | 4181 | struct nhg_hash_entry nhe, *n; |
1d48702e MS |
4182 | |
4183 | if (!re) | |
4184 | return -1; | |
4185 | ||
4186 | /* We either need nexthop(s) or an existing nexthop id */ | |
4187 | if (ng == NULL && re->nhe_id == 0) | |
4188 | return -1; | |
4189 | ||
4190 | /* | |
4191 | * Use a temporary nhe to convey info to the common/main api. | |
4192 | */ | |
377e29f7 | 4193 | zebra_nhe_init(&nhe, afi, (ng ? ng->nexthop : NULL)); |
1d48702e MS |
4194 | if (ng) |
4195 | nhe.nhg.nexthop = ng->nexthop; | |
4196 | else if (re->nhe_id > 0) | |
4197 | nhe.id = re->nhe_id; | |
4198 | ||
d7ac4c4d DS |
4199 | n = zebra_nhe_copy(&nhe, 0); |
4200 | ret = rib_add_multipath_nhe(afi, safi, p, src_p, re, n, startup); | |
1d48702e MS |
4201 | |
4202 | /* In error cases, free the route also */ | |
4203 | if (ret < 0) | |
4204 | XFREE(MTYPE_RE, re); | |
4205 | ||
4206 | return ret; | |
4207 | } | |
4208 | ||
d62a17ae | 4209 | void rib_delete(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type, |
acde7f6b | 4210 | unsigned short instance, uint32_t flags, struct prefix *p, |
fd36be7e | 4211 | struct prefix_ipv6 *src_p, const struct nexthop *nh, |
bc541126 | 4212 | uint32_t nhe_id, uint32_t table_id, uint32_t metric, |
3ceae22b | 4213 | uint8_t distance, bool fromkernel) |
d62a17ae | 4214 | { |
d7ac4c4d DS |
4215 | struct zebra_early_route *ere; |
4216 | struct route_entry *re = NULL; | |
4217 | struct nhg_hash_entry *nhe = NULL; | |
cf363e1b | 4218 | |
b0385873 DS |
4219 | re = zebra_rib_route_entry_new(vrf_id, type, instance, flags, nhe_id, |
4220 | table_id, metric, 0, distance, 0); | |
cf363e1b | 4221 | |
d7ac4c4d DS |
4222 | if (nh) { |
4223 | nhe = zebra_nhg_alloc(); | |
4224 | nhe->nhg.nexthop = nexthop_dup(nh, NULL); | |
5dfeba19 | 4225 | } |
05737783 | 4226 | |
d7ac4c4d DS |
4227 | ere = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(*ere)); |
4228 | ere->afi = afi; | |
4229 | ere->safi = safi; | |
4230 | ere->p = *p; | |
4231 | if (src_p) | |
4232 | ere->src_p = *src_p; | |
4233 | ere->src_p_provided = !!src_p; | |
4234 | ere->re = re; | |
4235 | ere->re_nhe = nhe; | |
4236 | ere->startup = false; | |
4237 | ere->deletion = true; | |
4238 | ere->fromkernel = fromkernel; | |
4239 | ||
4240 | mq_add_handler(ere, rib_meta_queue_early_route_add); | |
d62a17ae | 4241 | } |
718e3744 | 4242 | |
718e3744 | 4243 | |
d7c0a89a | 4244 | int rib_add(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type, |
acde7f6b | 4245 | unsigned short instance, uint32_t flags, struct prefix *p, |
d7c0a89a | 4246 | struct prefix_ipv6 *src_p, const struct nexthop *nh, |
8032b717 | 4247 | uint32_t nhe_id, uint32_t table_id, uint32_t metric, uint32_t mtu, |
c6eee91f | 4248 | uint8_t distance, route_tag_t tag, bool startup) |
d62a17ae | 4249 | { |
bbb322f2 | 4250 | struct route_entry *re = NULL; |
d7ac4c4d DS |
4251 | struct nexthop nexthop = {}; |
4252 | struct nexthop_group ng = {}; | |
718e3744 | 4253 | |
66af6845 | 4254 | /* Allocate new route_entry structure. */ |
b0385873 DS |
4255 | re = zebra_rib_route_entry_new(vrf_id, type, instance, flags, nhe_id, |
4256 | table_id, metric, mtu, distance, tag); | |
bbb322f2 | 4257 | |
0eb97b86 MS |
4258 | /* If the owner of the route supplies a shared nexthop-group id, |
4259 | * we'll use that. Otherwise, pass the nexthop along directly. | |
4260 | */ | |
e22e8001 | 4261 | if (!nhe_id) { |
e22e8001 | 4262 | /* Add nexthop. */ |
d7ac4c4d DS |
4263 | nexthop = *nh; |
4264 | nexthop_group_add_sorted(&ng, &nexthop); | |
e22e8001 | 4265 | } |
718e3744 | 4266 | |
d7ac4c4d | 4267 | return rib_add_multipath(afi, safi, p, src_p, re, &ng, startup); |
718e3744 | 4268 | } |
4269 | ||
91e6f25b | 4270 | static const char *rib_update_event2str(enum rib_update_event event) |
2a99ab95 SW |
4271 | { |
4272 | const char *ret = "UNKNOWN"; | |
4273 | ||
4274 | switch (event) { | |
4275 | case RIB_UPDATE_KERNEL: | |
4276 | ret = "RIB_UPDATE_KERNEL"; | |
4277 | break; | |
4278 | case RIB_UPDATE_RMAP_CHANGE: | |
4279 | ret = "RIB_UPDATE_RMAP_CHANGE"; | |
4280 | break; | |
4281 | case RIB_UPDATE_OTHER: | |
4282 | ret = "RIB_UPDATE_OTHER"; | |
4283 | break; | |
4284 | case RIB_UPDATE_MAX: | |
4285 | break; | |
4286 | } | |
4287 | ||
4288 | return ret; | |
4289 | } | |
4290 | ||
4291 | ||
4292 | /* Schedule route nodes to be processed if they match the type */ | |
4293 | static void rib_update_route_node(struct route_node *rn, int type) | |
4294 | { | |
4295 | struct route_entry *re, *next; | |
4296 | bool re_changed = false; | |
4297 | ||
4298 | RNODE_FOREACH_RE_SAFE (rn, re, next) { | |
4299 | if (type == ZEBRA_ROUTE_ALL || type == re->type) { | |
4300 | SET_FLAG(re->status, ROUTE_ENTRY_CHANGED); | |
4301 | re_changed = true; | |
4302 | } | |
4303 | } | |
4304 | ||
4305 | if (re_changed) | |
4306 | rib_queue_add(rn); | |
4307 | } | |
4308 | ||
1c848137 | 4309 | /* Schedule routes of a particular table (address-family) based on event. */ |
54aeba35 DS |
4310 | void rib_update_table(struct route_table *table, enum rib_update_event event, |
4311 | int rtype) | |
d62a17ae | 4312 | { |
4313 | struct route_node *rn; | |
2a99ab95 SW |
4314 | |
4315 | if (IS_ZEBRA_DEBUG_EVENT) { | |
4316 | struct zebra_vrf *zvrf; | |
4317 | struct vrf *vrf; | |
4318 | ||
630d5962 DS |
4319 | zvrf = table->info |
4320 | ? ((struct rib_table_info *)table->info)->zvrf | |
4321 | : NULL; | |
2a99ab95 SW |
4322 | vrf = zvrf ? zvrf->vrf : NULL; |
4323 | ||
54aeba35 | 4324 | zlog_debug("%s: %s VRF %s Table %u event %s Route type: %s", __func__, |
2a99ab95 | 4325 | table->info ? afi2str( |
630d5962 | 4326 | ((struct rib_table_info *)table->info)->afi) |
2a99ab95 | 4327 | : "Unknown", |
bd47f3a3 | 4328 | VRF_LOGNAME(vrf), zvrf ? zvrf->table_id : 0, |
54aeba35 | 4329 | rib_update_event2str(event), zebra_route_string(rtype)); |
2a99ab95 | 4330 | } |
d62a17ae | 4331 | |
4332 | /* Walk all routes and queue for processing, if appropriate for | |
4333 | * the trigger event. | |
4334 | */ | |
4335 | for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) { | |
1ca60f2c DS |
4336 | /* |
4337 | * If we are looking at a route node and the node | |
4338 | * has already been queued we don't | |
4339 | * need to queue it up again | |
4340 | */ | |
2a99ab95 SW |
4341 | if (rn->info |
4342 | && CHECK_FLAG(rib_dest_from_rnode(rn)->flags, | |
4343 | RIB_ROUTE_ANY_QUEUED)) | |
1ca60f2c | 4344 | continue; |
2a99ab95 | 4345 | |
d62a17ae | 4346 | switch (event) { |
2a99ab95 SW |
4347 | case RIB_UPDATE_KERNEL: |
4348 | rib_update_route_node(rn, ZEBRA_ROUTE_KERNEL); | |
4349 | break; | |
d62a17ae | 4350 | case RIB_UPDATE_RMAP_CHANGE: |
4351 | case RIB_UPDATE_OTHER: | |
54aeba35 | 4352 | rib_update_route_node(rn, rtype); |
d62a17ae | 4353 | break; |
a98701f0 | 4354 | case RIB_UPDATE_MAX: |
d62a17ae | 4355 | break; |
4356 | } | |
4357 | } | |
b84c7253 | 4358 | } |
4359 | ||
54aeba35 | 4360 | static void rib_update_handle_vrf_all(enum rib_update_event event, int rtype) |
2a99ab95 SW |
4361 | { |
4362 | struct zebra_router_table *zrt; | |
4363 | ||
4364 | if (IS_ZEBRA_DEBUG_EVENT) | |
4365 | zlog_debug("%s: Handling VRF (ALL) event %s", __func__, | |
4366 | rib_update_event2str(event)); | |
4367 | ||
4368 | /* Just iterate over all the route tables, rather than vrf lookups */ | |
4369 | RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) | |
54aeba35 | 4370 | rib_update_table(zrt->table, event, rtype); |
2a99ab95 SW |
4371 | } |
4372 | ||
4373 | struct rib_update_ctx { | |
91e6f25b | 4374 | enum rib_update_event event; |
2a99ab95 SW |
4375 | vrf_id_t vrf_id; |
4376 | }; | |
4377 | ||
4378 | static struct rib_update_ctx *rib_update_ctx_init(vrf_id_t vrf_id, | |
91e6f25b | 4379 | enum rib_update_event event) |
2a99ab95 SW |
4380 | { |
4381 | struct rib_update_ctx *ctx; | |
4382 | ||
4383 | ctx = XCALLOC(MTYPE_RIB_UPDATE_CTX, sizeof(struct rib_update_ctx)); | |
4384 | ||
4385 | ctx->event = event; | |
4386 | ctx->vrf_id = vrf_id; | |
4387 | ||
4388 | return ctx; | |
4389 | } | |
4390 | ||
4391 | static void rib_update_ctx_fini(struct rib_update_ctx **ctx) | |
4392 | { | |
4393 | XFREE(MTYPE_RIB_UPDATE_CTX, *ctx); | |
2a99ab95 SW |
4394 | } |
4395 | ||
cc9f21da | 4396 | static void rib_update_handler(struct thread *thread) |
2a99ab95 SW |
4397 | { |
4398 | struct rib_update_ctx *ctx; | |
4399 | ||
4400 | ctx = THREAD_ARG(thread); | |
4401 | ||
d2a17423 | 4402 | rib_update_handle_vrf_all(ctx->event, ZEBRA_ROUTE_ALL); |
2a99ab95 SW |
4403 | |
4404 | rib_update_ctx_fini(&ctx); | |
2a99ab95 SW |
4405 | } |
4406 | ||
4407 | /* | |
4408 | * Thread list to ensure we don't schedule a ton of events | |
4409 | * if interfaces are flapping for instance. | |
4410 | */ | |
4411 | static struct thread *t_rib_update_threads[RIB_UPDATE_MAX]; | |
4412 | ||
2a99ab95 | 4413 | /* Schedule a RIB update event for all vrfs */ |
91e6f25b | 4414 | void rib_update(enum rib_update_event event) |
2a99ab95 SW |
4415 | { |
4416 | struct rib_update_ctx *ctx; | |
4417 | ||
e13f12a7 DS |
4418 | if (thread_is_scheduled(t_rib_update_threads[event])) |
4419 | return; | |
2a99ab95 | 4420 | |
e13f12a7 | 4421 | ctx = rib_update_ctx_init(0, event); |
2a99ab95 | 4422 | |
e13f12a7 DS |
4423 | thread_add_event(zrouter.master, rib_update_handler, ctx, 0, |
4424 | &t_rib_update_threads[event]); | |
4425 | ||
4426 | if (IS_ZEBRA_DEBUG_EVENT) | |
05267678 | 4427 | zlog_debug("%s: Scheduled VRF (ALL), event %s", __func__, |
2a99ab95 | 4428 | rib_update_event2str(event)); |
718e3744 | 4429 | } |
4430 | ||
718e3744 | 4431 | /* Delete self installed routes after zebra is relaunched. */ |
95a29032 | 4432 | void rib_sweep_table(struct route_table *table) |
d62a17ae | 4433 | { |
4434 | struct route_node *rn; | |
4435 | struct route_entry *re; | |
4436 | struct route_entry *next; | |
915902cb | 4437 | struct nexthop *nexthop; |
d62a17ae | 4438 | |
915902cb DS |
4439 | if (!table) |
4440 | return; | |
d62a17ae | 4441 | |
6d81b590 MS |
4442 | if (IS_ZEBRA_DEBUG_RIB) |
4443 | zlog_debug("%s: starting", __func__); | |
4444 | ||
915902cb | 4445 | for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) { |
a2addae8 | 4446 | RNODE_FOREACH_RE_SAFE (rn, re, next) { |
d4644d41 | 4447 | |
915902cb DS |
4448 | if (IS_ZEBRA_DEBUG_RIB) |
4449 | route_entry_dump(&rn->p, NULL, re); | |
4450 | ||
4451 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) | |
4452 | continue; | |
4453 | ||
4454 | if (!CHECK_FLAG(re->flags, ZEBRA_FLAG_SELFROUTE)) | |
4455 | continue; | |
4456 | ||
d4644d41 DS |
4457 | /* |
4458 | * If routes are older than startup_time then | |
4459 | * we know we read them in from the kernel. | |
4460 | * As such we can safely remove them. | |
4461 | */ | |
4462 | if (zrouter.startup_time < re->uptime) | |
4463 | continue; | |
4464 | ||
915902cb DS |
4465 | /* |
4466 | * So we are starting up and have received | |
4467 | * routes from the kernel that we have installed | |
4468 | * from a previous run of zebra but not cleaned | |
4469 | * up ( say a kill -9 ) | |
4470 | * But since we haven't actually installed | |
4471 | * them yet( we received them from the kernel ) | |
4472 | * we don't think they are active. | |
4473 | * So let's pretend they are active to actually | |
4474 | * remove them. | |
4475 | * In all honesty I'm not sure if we should | |
4476 | * mark them as active when we receive them | |
4477 | * This is startup only so probably ok. | |
4478 | * | |
4479 | * If we ever decide to move rib_sweep_table | |
4480 | * to a different spot (ie startup ) | |
4481 | * this decision needs to be revisited | |
4482 | */ | |
677c1dd5 | 4483 | SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED); |
c415d895 | 4484 | for (ALL_NEXTHOPS(re->nhe->nhg, nexthop)) |
915902cb DS |
4485 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB); |
4486 | ||
0c555cc6 DS |
4487 | rib_uninstall_kernel(rn, re); |
4488 | rib_delnode(rn, re); | |
915902cb DS |
4489 | } |
4490 | } | |
6d81b590 MS |
4491 | |
4492 | if (IS_ZEBRA_DEBUG_RIB) | |
4493 | zlog_debug("%s: ends", __func__); | |
718e3744 | 4494 | } |
4495 | ||
4496 | /* Sweep all RIB tables. */ | |
cc9f21da | 4497 | void rib_sweep_route(struct thread *t) |
718e3744 | 4498 | { |
d62a17ae | 4499 | struct vrf *vrf; |
4500 | struct zebra_vrf *zvrf; | |
78104b9b | 4501 | |
a2addae8 | 4502 | RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) { |
915902cb DS |
4503 | if ((zvrf = vrf->info) == NULL) |
4504 | continue; | |
4505 | ||
d62a17ae | 4506 | rib_sweep_table(zvrf->table[AFI_IP][SAFI_UNICAST]); |
4507 | rib_sweep_table(zvrf->table[AFI_IP6][SAFI_UNICAST]); | |
4508 | } | |
95a29032 | 4509 | |
89272910 | 4510 | zebra_router_sweep_route(); |
38e40db1 | 4511 | zebra_router_sweep_nhgs(); |
718e3744 | 4512 | } |
2ea1ab1c VT |
4513 | |
4514 | /* Remove specific by protocol routes from 'table'. */ | |
d7c0a89a | 4515 | unsigned long rib_score_proto_table(uint8_t proto, unsigned short instance, |
47a08aa9 | 4516 | struct route_table *table) |
d62a17ae | 4517 | { |
4518 | struct route_node *rn; | |
4519 | struct route_entry *re; | |
4520 | struct route_entry *next; | |
4521 | unsigned long n = 0; | |
4522 | ||
4523 | if (table) | |
4524 | for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) | |
a2addae8 | 4525 | RNODE_FOREACH_RE_SAFE (rn, re, next) { |
d62a17ae | 4526 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) |
4527 | continue; | |
4528 | if (re->type == proto | |
4529 | && re->instance == instance) { | |
4530 | rib_delnode(rn, re); | |
4531 | n++; | |
4532 | } | |
4533 | } | |
4534 | return n; | |
2ea1ab1c VT |
4535 | } |
4536 | ||
4537 | /* Remove specific by protocol routes. */ | |
d7c0a89a | 4538 | unsigned long rib_score_proto(uint8_t proto, unsigned short instance) |
2ea1ab1c | 4539 | { |
d62a17ae | 4540 | struct vrf *vrf; |
4541 | struct zebra_vrf *zvrf; | |
d8612e65 | 4542 | struct other_route_table *ort; |
d62a17ae | 4543 | unsigned long cnt = 0; |
78104b9b | 4544 | |
d8612e65 DS |
4545 | RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) { |
4546 | zvrf = vrf->info; | |
4547 | if (!zvrf) | |
4548 | continue; | |
78104b9b | 4549 | |
d8612e65 DS |
4550 | cnt += rib_score_proto_table(proto, instance, |
4551 | zvrf->table[AFI_IP][SAFI_UNICAST]) | |
4552 | + rib_score_proto_table( | |
4553 | proto, instance, | |
4554 | zvrf->table[AFI_IP6][SAFI_UNICAST]); | |
4555 | ||
81fddbe7 | 4556 | frr_each(otable, &zvrf->other_tables, ort) cnt += |
d8612e65 DS |
4557 | rib_score_proto_table(proto, instance, ort->table); |
4558 | } | |
47a08aa9 | 4559 | |
d62a17ae | 4560 | return cnt; |
2ea1ab1c VT |
4561 | } |
4562 | ||
718e3744 | 4563 | /* Close RIB and clean up kernel routes. */ |
d62a17ae | 4564 | void rib_close_table(struct route_table *table) |
718e3744 | 4565 | { |
d62a17ae | 4566 | struct route_node *rn; |
5f7a4718 | 4567 | rib_dest_t *dest; |
718e3744 | 4568 | |
1e9f448f DS |
4569 | if (!table) |
4570 | return; | |
9fd92e3c | 4571 | |
5f7a4718 DS |
4572 | for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) { |
4573 | dest = rib_dest_from_rnode(rn); | |
1e9f448f | 4574 | |
5f7a4718 | 4575 | if (dest && dest->selected_fib) { |
3cdba47a DS |
4576 | rib_uninstall_kernel(rn, dest->selected_fib); |
4577 | dest->selected_fib = NULL; | |
1e9f448f | 4578 | } |
5f7a4718 | 4579 | } |
718e3744 | 4580 | } |
4581 | ||
9bd9717b MS |
4582 | /* |
4583 | * Handler for async dataplane results after a pseudowire installation | |
4584 | */ | |
f00b37e7 | 4585 | static void handle_pw_result(struct zebra_dplane_ctx *ctx) |
9bd9717b | 4586 | { |
9bd9717b MS |
4587 | struct zebra_pw *pw; |
4588 | struct zebra_vrf *vrf; | |
4589 | ||
4590 | /* The pseudowire code assumes success - we act on an error | |
4591 | * result for installation attempts here. | |
4592 | */ | |
4593 | if (dplane_ctx_get_op(ctx) != DPLANE_OP_PW_INSTALL) | |
f00b37e7 | 4594 | return; |
9bd9717b MS |
4595 | |
4596 | if (dplane_ctx_get_status(ctx) != ZEBRA_DPLANE_REQUEST_SUCCESS) { | |
4597 | vrf = zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx)); | |
7c7ef4a8 | 4598 | pw = zebra_pw_find(vrf, dplane_ctx_get_ifname(ctx)); |
9bd9717b | 4599 | if (pw) |
fd563cc7 KS |
4600 | zebra_pw_install_failure(pw, |
4601 | dplane_ctx_get_pw_status(ctx)); | |
9bd9717b | 4602 | } |
9bd9717b MS |
4603 | } |
4604 | ||
7cdb1a84 | 4605 | /* |
d37f4d6c MS |
4606 | * Handle results from the dataplane system. Dequeue update context |
4607 | * structs, dispatch to appropriate internal handlers. | |
7cdb1a84 | 4608 | */ |
cc9f21da | 4609 | static void rib_process_dplane_results(struct thread *thread) |
7cdb1a84 | 4610 | { |
25779064 | 4611 | struct zebra_dplane_ctx *ctx; |
ac96497c | 4612 | struct dplane_ctx_list_head ctxlist; |
2fc69f03 | 4613 | bool shut_p = false; |
4c206c8f MS |
4614 | |
4615 | /* Dequeue a list of completed updates with one lock/unlock cycle */ | |
7cdb1a84 MS |
4616 | |
4617 | do { | |
ac96497c | 4618 | dplane_ctx_q_init(&ctxlist); |
4c206c8f | 4619 | |
7cdb1a84 | 4620 | /* Take lock controlling queue of results */ |
cb1991af | 4621 | frr_with_mutex (&dplane_mutex) { |
d37f4d6c | 4622 | /* Dequeue list of context structs */ |
4c206c8f | 4623 | dplane_ctx_list_append(&ctxlist, &rib_dplane_q); |
7cdb1a84 | 4624 | } |
7cdb1a84 | 4625 | |
4c206c8f MS |
4626 | /* Dequeue context block */ |
4627 | ctx = dplane_ctx_dequeue(&ctxlist); | |
4628 | ||
4629 | /* If we've emptied the results queue, we're done */ | |
4630 | if (ctx == NULL) | |
7cdb1a84 | 4631 | break; |
7cdb1a84 | 4632 | |
2fc69f03 MS |
4633 | /* If zebra is shutting down, avoid processing results, |
4634 | * just drain the results queue. | |
4635 | */ | |
4636 | shut_p = atomic_load_explicit(&zrouter.in_shutdown, | |
4637 | memory_order_relaxed); | |
4638 | if (shut_p) { | |
4639 | while (ctx) { | |
4640 | dplane_ctx_fini(&ctx); | |
4641 | ||
4642 | ctx = dplane_ctx_dequeue(&ctxlist); | |
4643 | } | |
4644 | ||
4645 | continue; | |
4646 | } | |
4647 | ||
1247efcc DL |
4648 | #ifdef HAVE_SCRIPTING |
4649 | char *script_name = frrscript_names_get_script_name( | |
4650 | ZEBRA_ON_RIB_PROCESS_HOOK_CALL); | |
4651 | ||
4652 | int ret = 1; | |
4653 | struct frrscript *fs; | |
4654 | ||
4655 | if (script_name) { | |
4656 | fs = frrscript_new(script_name); | |
4657 | if (fs) | |
4658 | ret = frrscript_load( | |
4659 | fs, ZEBRA_ON_RIB_PROCESS_HOOK_CALL, | |
4660 | NULL); | |
4661 | } | |
4662 | #endif /* HAVE_SCRIPTING */ | |
4663 | ||
4c206c8f | 4664 | while (ctx) { |
1247efcc DL |
4665 | |
4666 | #ifdef HAVE_SCRIPTING | |
4667 | if (ret == 0) | |
4668 | frrscript_call(fs, | |
4669 | ZEBRA_ON_RIB_PROCESS_HOOK_CALL, | |
4670 | ("ctx", ctx)); | |
4671 | #endif /* HAVE_SCRIPTING */ | |
4672 | ||
d37f4d6c MS |
4673 | switch (dplane_ctx_get_op(ctx)) { |
4674 | case DPLANE_OP_ROUTE_INSTALL: | |
4675 | case DPLANE_OP_ROUTE_UPDATE: | |
4676 | case DPLANE_OP_ROUTE_DELETE: | |
188a00e0 MS |
4677 | /* Bit of special case for route updates |
4678 | * that were generated by async notifications: | |
4679 | * we don't want to continue processing these | |
4680 | * in the rib. | |
4681 | */ | |
4682 | if (dplane_ctx_get_notif_provider(ctx) == 0) | |
4683 | rib_process_result(ctx); | |
f00b37e7 | 4684 | break; |
d37f4d6c | 4685 | |
54818e3b MS |
4686 | case DPLANE_OP_ROUTE_NOTIFY: |
4687 | rib_process_dplane_notify(ctx); | |
d37f4d6c MS |
4688 | break; |
4689 | ||
9865e1c3 SW |
4690 | case DPLANE_OP_NH_INSTALL: |
4691 | case DPLANE_OP_NH_UPDATE: | |
4692 | case DPLANE_OP_NH_DELETE: | |
4693 | zebra_nhg_dplane_result(ctx); | |
4694 | break; | |
4695 | ||
d37f4d6c MS |
4696 | case DPLANE_OP_LSP_INSTALL: |
4697 | case DPLANE_OP_LSP_UPDATE: | |
4698 | case DPLANE_OP_LSP_DELETE: | |
188a00e0 MS |
4699 | /* Bit of special case for LSP updates |
4700 | * that were generated by async notifications: | |
4701 | * we don't want to continue processing these. | |
4702 | */ | |
4703 | if (dplane_ctx_get_notif_provider(ctx) == 0) | |
4704 | zebra_mpls_lsp_dplane_result(ctx); | |
f00b37e7 | 4705 | break; |
d37f4d6c | 4706 | |
104e3ad9 MS |
4707 | case DPLANE_OP_LSP_NOTIFY: |
4708 | zebra_mpls_process_dplane_notify(ctx); | |
d37f4d6c MS |
4709 | break; |
4710 | ||
9bd9717b MS |
4711 | case DPLANE_OP_PW_INSTALL: |
4712 | case DPLANE_OP_PW_UNINSTALL: | |
4713 | handle_pw_result(ctx); | |
4714 | break; | |
4715 | ||
cf363e1b MS |
4716 | case DPLANE_OP_SYS_ROUTE_ADD: |
4717 | case DPLANE_OP_SYS_ROUTE_DELETE: | |
cf363e1b MS |
4718 | break; |
4719 | ||
036d93c0 MS |
4720 | case DPLANE_OP_MAC_INSTALL: |
4721 | case DPLANE_OP_MAC_DELETE: | |
4722 | zebra_vxlan_handle_result(ctx); | |
4723 | break; | |
4724 | ||
f62e5480 JU |
4725 | case DPLANE_OP_RULE_ADD: |
4726 | case DPLANE_OP_RULE_DELETE: | |
4727 | case DPLANE_OP_RULE_UPDATE: | |
5162e000 PG |
4728 | case DPLANE_OP_IPTABLE_ADD: |
4729 | case DPLANE_OP_IPTABLE_DELETE: | |
ef524230 PG |
4730 | case DPLANE_OP_IPSET_ADD: |
4731 | case DPLANE_OP_IPSET_DELETE: | |
4732 | case DPLANE_OP_IPSET_ENTRY_ADD: | |
4733 | case DPLANE_OP_IPSET_ENTRY_DELETE: | |
f62e5480 JU |
4734 | zebra_pbr_dplane_result(ctx); |
4735 | break; | |
4736 | ||
d166308b MS |
4737 | case DPLANE_OP_INTF_ADDR_ADD: |
4738 | case DPLANE_OP_INTF_ADDR_DEL: | |
5d414138 SW |
4739 | case DPLANE_OP_INTF_INSTALL: |
4740 | case DPLANE_OP_INTF_UPDATE: | |
4741 | case DPLANE_OP_INTF_DELETE: | |
cd787a8a | 4742 | case DPLANE_OP_INTF_NETCONFIG: |
5d414138 | 4743 | zebra_if_dplane_result(ctx); |
cd787a8a MS |
4744 | break; |
4745 | ||
c317d3f2 SY |
4746 | case DPLANE_OP_TC_QDISC_INSTALL: |
4747 | case DPLANE_OP_TC_QDISC_UNINSTALL: | |
4748 | case DPLANE_OP_TC_CLASS_ADD: | |
4749 | case DPLANE_OP_TC_CLASS_DELETE: | |
4750 | case DPLANE_OP_TC_CLASS_UPDATE: | |
4751 | case DPLANE_OP_TC_FILTER_ADD: | |
4752 | case DPLANE_OP_TC_FILTER_DELETE: | |
4753 | case DPLANE_OP_TC_FILTER_UPDATE: | |
449a30ed SY |
4754 | break; |
4755 | ||
931fa60c MS |
4756 | /* Some op codes not handled here */ |
4757 | case DPLANE_OP_ADDR_INSTALL: | |
4758 | case DPLANE_OP_ADDR_UNINSTALL: | |
4759 | case DPLANE_OP_NEIGH_INSTALL: | |
4760 | case DPLANE_OP_NEIGH_UPDATE: | |
4761 | case DPLANE_OP_NEIGH_DELETE: | |
0a27a2fe PG |
4762 | case DPLANE_OP_NEIGH_IP_INSTALL: |
4763 | case DPLANE_OP_NEIGH_IP_DELETE: | |
0bbd4ff4 MS |
4764 | case DPLANE_OP_VTEP_ADD: |
4765 | case DPLANE_OP_VTEP_DELETE: | |
d68e74b4 | 4766 | case DPLANE_OP_NEIGH_DISCOVER: |
c60522f7 | 4767 | case DPLANE_OP_BR_PORT_UPDATE: |
e18747a9 | 4768 | case DPLANE_OP_NEIGH_TABLE_UPDATE: |
62b4b7e4 | 4769 | case DPLANE_OP_GRE_SET: |
931fa60c | 4770 | case DPLANE_OP_NONE: |
d37f4d6c | 4771 | break; |
931fa60c | 4772 | |
d37f4d6c | 4773 | } /* Dispatch by op code */ |
4c206c8f | 4774 | |
f00b37e7 | 4775 | dplane_ctx_fini(&ctx); |
4c206c8f MS |
4776 | ctx = dplane_ctx_dequeue(&ctxlist); |
4777 | } | |
4778 | ||
5709131c | 4779 | } while (1); |
7cdb1a84 MS |
4780 | } |
4781 | ||
4782 | /* | |
4783 | * Results are returned from the dataplane subsystem, in the context of | |
1bcea841 | 4784 | * the dataplane pthread. We enqueue the results here for processing by |
7cdb1a84 MS |
4785 | * the main thread later. |
4786 | */ | |
ac96497c | 4787 | static int rib_dplane_results(struct dplane_ctx_list_head *ctxlist) |
7cdb1a84 MS |
4788 | { |
4789 | /* Take lock controlling queue of results */ | |
cb1991af | 4790 | frr_with_mutex (&dplane_mutex) { |
4c206c8f MS |
4791 | /* Enqueue context blocks */ |
4792 | dplane_ctx_list_append(&rib_dplane_q, ctxlist); | |
7cdb1a84 | 4793 | } |
7cdb1a84 | 4794 | |
4c206c8f | 4795 | /* Ensure event is signalled to zebra main pthread */ |
3801e764 | 4796 | thread_add_event(zrouter.master, rib_process_dplane_results, NULL, 0, |
7cdb1a84 MS |
4797 | &t_dplane); |
4798 | ||
5709131c | 4799 | return 0; |
7cdb1a84 MS |
4800 | } |
4801 | ||
eaa2716d SW |
4802 | /* |
4803 | * Ensure there are no empty slots in the route_info array. | |
4804 | * Every route type in zebra should be present there. | |
4805 | */ | |
4806 | static void check_route_info(void) | |
4807 | { | |
4808 | int len = array_size(route_info); | |
4809 | ||
4810 | /* | |
4811 | * ZEBRA_ROUTE_SYSTEM is special cased since | |
4812 | * its key is 0 anyway. | |
4813 | * | |
4814 | * ZEBRA_ROUTE_ALL is also ignored. | |
4815 | */ | |
4816 | for (int i = 0; i < len; i++) { | |
16d91fce DS |
4817 | assert(route_info[i].key >= ZEBRA_ROUTE_SYSTEM && |
4818 | route_info[i].key < ZEBRA_ROUTE_MAX); | |
8dc7a759 | 4819 | assert(route_info[i].meta_q_map < MQ_SIZE); |
eaa2716d SW |
4820 | } |
4821 | } | |
4822 | ||
718e3744 | 4823 | /* Routing information base initialize. */ |
d62a17ae | 4824 | void rib_init(void) |
718e3744 | 4825 | { |
eaa2716d SW |
4826 | check_route_info(); |
4827 | ||
2561d12e | 4828 | rib_queue_init(); |
7cdb1a84 MS |
4829 | |
4830 | /* Init dataplane, and register for results */ | |
4831 | pthread_mutex_init(&dplane_mutex, NULL); | |
ac96497c | 4832 | dplane_ctx_q_init(&rib_dplane_q); |
4c206c8f | 4833 | zebra_dplane_init(rib_dplane_results); |
718e3744 | 4834 | } |
0915bb0c AS |
4835 | |
4836 | /* | |
4837 | * vrf_id_get_next | |
4838 | * | |
4839 | * Get the first vrf id that is greater than the given vrf id if any. | |
4840 | * | |
2951a7a4 | 4841 | * Returns true if a vrf id was found, false otherwise. |
0915bb0c | 4842 | */ |
d62a17ae | 4843 | static inline int vrf_id_get_next(vrf_id_t vrf_id, vrf_id_t *next_id_p) |
0915bb0c | 4844 | { |
d62a17ae | 4845 | struct vrf *vrf; |
b72ede27 | 4846 | |
d62a17ae | 4847 | vrf = vrf_lookup_by_id(vrf_id); |
4848 | if (vrf) { | |
4849 | vrf = RB_NEXT(vrf_id_head, vrf); | |
4850 | if (vrf) { | |
4851 | *next_id_p = vrf->vrf_id; | |
4852 | return 1; | |
4853 | } | |
4854 | } | |
0915bb0c | 4855 | |
d62a17ae | 4856 | return 0; |
0915bb0c AS |
4857 | } |
4858 | ||
4859 | /* | |
4860 | * rib_tables_iter_next | |
4861 | * | |
4862 | * Returns the next table in the iteration. | |
4863 | */ | |
d62a17ae | 4864 | struct route_table *rib_tables_iter_next(rib_tables_iter_t *iter) |
4865 | { | |
4866 | struct route_table *table; | |
4867 | ||
4868 | /* | |
4869 | * Array that helps us go over all AFI/SAFI combinations via one | |
4870 | * index. | |
4871 | */ | |
2b64873d | 4872 | static const struct { |
d62a17ae | 4873 | afi_t afi; |
4874 | safi_t safi; | |
4875 | } afi_safis[] = { | |
4876 | {AFI_IP, SAFI_UNICAST}, {AFI_IP, SAFI_MULTICAST}, | |
4877 | {AFI_IP, SAFI_LABELED_UNICAST}, {AFI_IP6, SAFI_UNICAST}, | |
4878 | {AFI_IP6, SAFI_MULTICAST}, {AFI_IP6, SAFI_LABELED_UNICAST}, | |
4879 | }; | |
4880 | ||
4881 | table = NULL; | |
4882 | ||
4883 | switch (iter->state) { | |
4884 | ||
4885 | case RIB_TABLES_ITER_S_INIT: | |
4886 | iter->vrf_id = VRF_DEFAULT; | |
4887 | iter->afi_safi_ix = -1; | |
4888 | ||
4889 | /* Fall through */ | |
4890 | ||
4891 | case RIB_TABLES_ITER_S_ITERATING: | |
4892 | iter->afi_safi_ix++; | |
4893 | while (1) { | |
4894 | ||
4895 | while (iter->afi_safi_ix | |
7e3a1ec7 | 4896 | < (int)array_size(afi_safis)) { |
d62a17ae | 4897 | table = zebra_vrf_table( |
4898 | afi_safis[iter->afi_safi_ix].afi, | |
4899 | afi_safis[iter->afi_safi_ix].safi, | |
4900 | iter->vrf_id); | |
4901 | if (table) | |
4902 | break; | |
4903 | ||
4904 | iter->afi_safi_ix++; | |
4905 | } | |
4906 | ||
4907 | /* | |
4908 | * Found another table in this vrf. | |
4909 | */ | |
4910 | if (table) | |
4911 | break; | |
4912 | ||
4913 | /* | |
4914 | * Done with all tables in the current vrf, go to the | |
4915 | * next | |
4916 | * one. | |
4917 | */ | |
4918 | if (!vrf_id_get_next(iter->vrf_id, &iter->vrf_id)) | |
4919 | break; | |
4920 | ||
4921 | iter->afi_safi_ix = 0; | |
4922 | } | |
0915bb0c | 4923 | |
0915bb0c AS |
4924 | break; |
4925 | ||
d62a17ae | 4926 | case RIB_TABLES_ITER_S_DONE: |
4927 | return NULL; | |
0915bb0c AS |
4928 | } |
4929 | ||
d62a17ae | 4930 | if (table) |
4931 | iter->state = RIB_TABLES_ITER_S_ITERATING; | |
4932 | else | |
4933 | iter->state = RIB_TABLES_ITER_S_DONE; | |
0915bb0c | 4934 | |
d62a17ae | 4935 | return table; |
0915bb0c | 4936 | } |