2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vnet/fib/fib_entry.h>
17 #include <vnet/fib/fib_table.h>
19 #include <vnet/fib/fib_attached_export.h>
20 #include <vnet/fib/fib_entry_cover.h>
21 #include <vnet/fib/fib_entry_src.h>
22 #include <vnet/fib/fib_entry_delegate.h>
23 #include <vnet/dpo/drop_dpo.h>
26 * A description of the need to import routes from the export table
28 typedef struct fib_ae_import_t_
31 * The entry in the epxort table that this importer
32 * is importing covereds from
34 fib_node_index_t faei_export_entry;
37 * The attached entry in the import table
39 fib_node_index_t faei_import_entry;
41 * the sibling index on the cover
43 u32 faei_export_sibling;
46 * The index of the exporter tracker. Not set if the
47 * export entry is not valid for export
49 fib_node_index_t faei_exporter;
52 * A vector/list of imported entry indicies
54 fib_node_index_t *faei_importeds;
57 * The FIB index and prefix we are tracking
59 fib_node_index_t faei_export_fib;
60 fib_prefix_t faei_prefix;
63 * The FIB index we are importing into
65 fib_node_index_t faei_import_fib;
69 * A description of the need to export routes to one or more export tables
71 typedef struct fib_ae_export_t_ {
73 * The vector/list of import tracker indicies
75 fib_node_index_t *faee_importers;
78 * THe connected entry this export is acting on behalf of
80 fib_node_index_t faee_ei;
83 * Reference counting locks
89 * memory pools for the importers and exportes
91 static fib_ae_import_t *fib_ae_import_pool;
92 static fib_ae_export_t *fib_ae_export_pool;
94 static fib_ae_export_t *
95 fib_entry_ae_add_or_lock (fib_node_index_t connected)
97 fib_entry_delegate_t *fed;
98 fib_ae_export_t *export;
101 entry = fib_entry_get(connected);
102 fed = fib_entry_delegate_get(entry,
103 FIB_ENTRY_DELEGATE_ATTACHED_EXPORT);
107 fed = fib_entry_delegate_find_or_add(entry,
108 FIB_ENTRY_DELEGATE_ATTACHED_EXPORT);
109 pool_get(fib_ae_export_pool, export);
110 memset(export, 0, sizeof(*export));
112 fed->fd_index = (export - fib_ae_export_pool);
113 export->faee_ei = connected;
117 export = pool_elt_at_index(fib_ae_export_pool, fed->fd_index);
120 export->faee_locks++;
126 fib_entry_import_remove (fib_ae_import_t *import,
127 fib_node_index_t entry_index)
133 * find the index in the vector of the entry we are removing
135 index = vec_search(import->faei_importeds, entry_index);
137 if (index < vec_len(import->faei_importeds))
140 * this is an entry that was previsouly imported
142 fib_entry_get_prefix(entry_index, &prefix);
144 fib_table_entry_special_remove(import->faei_import_fib,
148 fib_entry_unlock(entry_index);
149 vec_del1(import->faei_importeds, index);
154 fib_entry_import_add (fib_ae_import_t *import,
155 fib_node_index_t entry_index)
157 fib_node_index_t *existing;
161 * ensure we only add the exported entry once, since
162 * sourcing prefixes in the table is reference counted
164 vec_foreach(existing, import->faei_importeds)
166 if (*existing == entry_index)
173 * this is the first time this export entry has been imported
174 * Add it to the import FIB and to the list of importeds
176 fib_entry_get_prefix(entry_index, &prefix);
179 * don't import entries that have the same prefix the import entry
181 if (0 != fib_prefix_cmp(&prefix,
182 &import->faei_prefix))
186 dpo = fib_entry_contribute_ip_forwarding(entry_index);
188 if (dpo_id_is_valid(dpo) && !dpo_is_drop(dpo))
190 fib_table_entry_special_dpo_add(import->faei_import_fib,
193 (fib_entry_get_flags(entry_index) |
194 FIB_ENTRY_FLAG_EXCLUSIVE),
195 load_balance_get_bucket(dpo->dpoi_index, 0));
197 fib_entry_lock(entry_index);
198 vec_add1(import->faei_importeds, entry_index);
202 * the entry currently has no valid forwarding. when it
203 * does it will export itself
209 * Call back when walking a connected prefix's covered prefixes for import
212 fib_entry_covered_walk_import (fib_entry_t *cover,
213 fib_node_index_t covered,
216 fib_ae_import_t *import = ctx;
218 fib_entry_import_add(import, covered);
224 * fib_entry_ae_import_add
226 * Add an importer to a connected entry
229 fib_ae_export_import_add (fib_ae_export_t *export,
230 fib_ae_import_t *import)
234 import->faei_exporter = (export - fib_ae_export_pool);
235 entry = fib_entry_get(export->faee_ei);
237 fib_entry_cover_walk(entry,
238 fib_entry_covered_walk_import,
243 fib_attached_export_import (fib_entry_t *fib_entry,
244 fib_node_index_t export_fib)
246 fib_entry_delegate_t *fed;
247 fib_ae_import_t *import;
248 fib_node_index_t fei;
251 * save index for later post-realloc retreival
253 fei = fib_entry_get_index(fib_entry);
255 pool_get(fib_ae_import_pool, import);
257 import->faei_import_fib = fib_entry->fe_fib_index;
258 import->faei_export_fib = export_fib;
259 import->faei_prefix = fib_entry->fe_prefix;
260 import->faei_import_entry = fib_entry_get_index(fib_entry);
261 import->faei_export_sibling = ~0;
264 * do an exact match in the export table
266 import->faei_export_entry =
267 fib_table_lookup_exact_match(import->faei_export_fib,
268 &import->faei_prefix);
270 if (FIB_NODE_INDEX_INVALID == import->faei_export_entry)
273 * no exact matching entry in the export table. can't be good.
274 * track the next best thing
276 import->faei_export_entry =
277 fib_table_lookup(import->faei_export_fib,
278 &import->faei_prefix);
279 import->faei_exporter = FIB_NODE_INDEX_INVALID;
284 * found the entry in the export table. import the
285 * the prefixes that it covers.
286 * only if the prefix found in the export FIB really is
287 * attached do we want to import its covered
289 if (FIB_ENTRY_FLAG_ATTACHED &
290 fib_entry_get_flags_i(fib_entry_get(import->faei_export_entry)))
292 fib_ae_export_t *export;
294 export = fib_entry_ae_add_or_lock(import->faei_export_entry);
295 vec_add1(export->faee_importers, (import - fib_ae_import_pool));
296 fib_ae_export_import_add(export, import);
301 * track the entry in the export table so we can update appropriately
303 * Exporting prefixes will have allocated new fib_entry_t objects, so the pool
304 * may have realloc'd.
306 fib_entry = fib_entry_get(fei);
307 import->faei_export_sibling =
308 fib_entry_cover_track(fib_entry_get(import->faei_export_entry), fei);
310 fed = fib_entry_delegate_find_or_add(fib_entry,
311 FIB_ENTRY_DELEGATE_ATTACHED_IMPORT);
312 fed->fd_index = (import - fib_ae_import_pool);
316 * \brief All the imported entries need to be pruged
319 fib_attached_export_purge (fib_entry_t *fib_entry)
321 fib_entry_delegate_t *fed;
323 fed = fib_entry_delegate_get(fib_entry,
324 FIB_ENTRY_DELEGATE_ATTACHED_IMPORT);
328 fib_node_index_t *import_index;
329 fib_entry_t *export_entry;
330 fib_ae_import_t *import;
331 fib_ae_export_t *export;
333 import = pool_elt_at_index(fib_ae_import_pool, fed->fd_index);
336 * remove each imported entry
338 vec_foreach(import_index, import->faei_importeds)
342 fib_entry_get_prefix(*import_index, &prefix);
344 fib_table_entry_delete(import->faei_import_fib,
347 fib_entry_unlock(*import_index);
349 vec_free(import->faei_importeds);
352 * stop tracking the export entry
354 if (~0 != import->faei_export_sibling)
356 fib_entry_cover_untrack(fib_entry_get(import->faei_export_entry),
357 import->faei_export_sibling);
359 import->faei_export_sibling = ~0;
362 * remove this import tracker from the export's list,
363 * if it is attached to one. It won't be in the case the tracked
364 * export entry is not an attached exact match.
366 if (FIB_NODE_INDEX_INVALID != import->faei_exporter)
368 fib_entry_delegate_t *fed;
370 export_entry = fib_entry_get(import->faei_export_entry);
372 fed = fib_entry_delegate_get(export_entry,
373 FIB_ENTRY_DELEGATE_ATTACHED_EXPORT);
376 export = pool_elt_at_index(fib_ae_export_pool, fed->fd_index);
378 u32 index = vec_search(export->faee_importers,
379 (import - fib_ae_import_pool));
381 ASSERT(index < vec_len(export->faee_importers));
382 vec_del1(export->faee_importers, index);
385 * free the exporter if there are no longer importers
387 if (0 == --export->faee_locks)
389 pool_put(fib_ae_export_pool, export);
390 fib_entry_delegate_remove(export_entry,
391 FIB_ENTRY_DELEGATE_ATTACHED_EXPORT);
396 * free the import tracker
398 pool_put(fib_ae_import_pool, import);
399 fib_entry_delegate_remove(fib_entry,
400 FIB_ENTRY_DELEGATE_ATTACHED_IMPORT);
405 fib_attached_export_covered_added (fib_entry_t *cover,
406 fib_node_index_t covered)
408 fib_entry_delegate_t *fed;
410 fed = fib_entry_delegate_get(cover,
411 FIB_ENTRY_DELEGATE_ATTACHED_EXPORT);
416 * the covering prefix is exporting to other tables
418 fib_node_index_t *import_index;
419 fib_ae_import_t *import;
420 fib_ae_export_t *export;
422 export = pool_elt_at_index(fib_ae_export_pool, fed->fd_index);
425 * export the covered entry to each of the importers
427 vec_foreach(import_index, export->faee_importers)
429 import = pool_elt_at_index(fib_ae_import_pool, *import_index);
431 fib_entry_import_add(import, covered);
437 fib_attached_export_covered_removed (fib_entry_t *cover,
438 fib_node_index_t covered)
440 fib_entry_delegate_t *fed;
442 fed = fib_entry_delegate_get(cover,
443 FIB_ENTRY_DELEGATE_ATTACHED_EXPORT);
448 * the covering prefix is exporting to other tables
450 fib_node_index_t *import_index;
451 fib_ae_import_t *import;
452 fib_ae_export_t *export;
454 export = pool_elt_at_index(fib_ae_export_pool, fed->fd_index);
457 * remove the covered entry from each of the importers
459 vec_foreach(import_index, export->faee_importers)
461 import = pool_elt_at_index(fib_ae_import_pool, *import_index);
463 fib_entry_import_remove(import, covered);
469 fib_attached_export_cover_modified_i (fib_entry_t *fib_entry)
471 fib_entry_delegate_t *fed;
473 fed = fib_entry_delegate_get(fib_entry,
474 FIB_ENTRY_DELEGATE_ATTACHED_IMPORT);
478 fib_ae_import_t *import;
482 * safe the temporaries we need from the existing import
483 * since it will be toast after the purge.
485 import = pool_elt_at_index(fib_ae_import_pool, fed->fd_index);
486 export_fib = import->faei_export_fib;
489 * keep it simple. purge anything that was previously imported.
490 * then re-evaluate the need to import.
492 fib_attached_export_purge(fib_entry);
493 fib_attached_export_import(fib_entry, export_fib);
498 * \brief If this entry is tracking a cover (in another table)
499 * then that cover has changed. re-evaluate import.
502 fib_attached_export_cover_change (fib_entry_t *fib_entry)
504 fib_attached_export_cover_modified_i(fib_entry);
508 * \brief If this entry is tracking a cover (in another table)
509 * then that cover has been updated. re-evaluate import.
512 fib_attached_export_cover_update (fib_entry_t *fib_entry)
514 fib_attached_export_cover_modified_i(fib_entry);
518 fib_ae_import_format (fib_node_index_t impi,
521 fib_node_index_t *index;
522 fib_ae_import_t *import;
524 import = pool_elt_at_index(fib_ae_import_pool, impi);
526 s = format(s, "\n Attached-Import:%d:[", (import - fib_ae_import_pool));
527 s = format(s, "export-prefix:%U ", format_fib_prefix, &import->faei_prefix);
528 s = format(s, "export-entry:%d ", import->faei_export_entry);
529 s = format(s, "export-sibling:%d ", import->faei_export_sibling);
530 s = format(s, "exporter:%d ", import->faei_exporter);
531 s = format(s, "export-fib:%d ", import->faei_export_fib);
533 s = format(s, "import-entry:%d ", import->faei_import_entry);
534 s = format(s, "import-fib:%d ", import->faei_import_fib);
536 s = format(s, "importeds:[");
537 vec_foreach(index, import->faei_importeds)
539 s = format(s, "%d, ", *index);
547 fib_ae_export_format (fib_node_index_t expi,
550 fib_node_index_t *index;
551 fib_ae_export_t *export;
553 export = pool_elt_at_index(fib_ae_export_pool, expi);
555 s = format(s, "\n Attached-Export:%d:[", (export - fib_ae_export_pool));
556 s = format(s, "export-entry:%d ", export->faee_ei);
558 s = format(s, "importers:[");
559 vec_foreach(index, export->faee_importers)
561 s = format(s, "%d, ", *index);