fib_trie.txt 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. LC-trie implementation notes.
  2. Node types
  3. ----------
  4. leaf
  5. An end node with data. This has a copy of the relevant key, along
  6. with 'hlist' with routing table entries sorted by prefix length.
  7. See struct leaf and struct leaf_info.
  8. trie node or tnode
  9. An internal node, holding an array of child (leaf or tnode) pointers,
  10. indexed through a subset of the key. See Level Compression.
  11. A few concepts explained
  12. ------------------------
  13. Bits (tnode)
  14. The number of bits in the key segment used for indexing into the
  15. child array - the "child index". See Level Compression.
  16. Pos (tnode)
  17. The position (in the key) of the key segment used for indexing into
  18. the child array. See Path Compression.
  19. Path Compression / skipped bits
  20. Any given tnode is linked to from the child array of its parent, using
  21. a segment of the key specified by the parent's "pos" and "bits"
  22. In certain cases, this tnode's own "pos" will not be immediately
  23. adjacent to the parent (pos+bits), but there will be some bits
  24. in the key skipped over because they represent a single path with no
  25. deviations. These "skipped bits" constitute Path Compression.
  26. Note that the search algorithm will simply skip over these bits when
  27. searching, making it necessary to save the keys in the leaves to
  28. verify that they actually do match the key we are searching for.
  29. Level Compression / child arrays
  30. the trie is kept level balanced moving, under certain conditions, the
  31. children of a full child (see "full_children") up one level, so that
  32. instead of a pure binary tree, each internal node ("tnode") may
  33. contain an arbitrarily large array of links to several children.
  34. Conversely, a tnode with a mostly empty child array (see empty_children)
  35. may be "halved", having some of its children moved downwards one level,
  36. in order to avoid ever-increasing child arrays.
  37. empty_children
  38. the number of positions in the child array of a given tnode that are
  39. NULL.
  40. full_children
  41. the number of children of a given tnode that aren't path compressed.
  42. (in other words, they aren't NULL or leaves and their "pos" is equal
  43. to this tnode's "pos"+"bits").
  44. (The word "full" here is used more in the sense of "complete" than
  45. as the opposite of "empty", which might be a tad confusing.)
  46. Comments
  47. ---------
  48. We have tried to keep the structure of the code as close to fib_hash as
  49. possible to allow verification and help up reviewing.
  50. fib_find_node()
  51. A good start for understanding this code. This function implements a
  52. straightforward trie lookup.
  53. fib_insert_node()
  54. Inserts a new leaf node in the trie. This is bit more complicated than
  55. fib_find_node(). Inserting a new node means we might have to run the
  56. level compression algorithm on part of the trie.
  57. trie_leaf_remove()
  58. Looks up a key, deletes it and runs the level compression algorithm.
  59. trie_rebalance()
  60. The key function for the dynamic trie after any change in the trie
  61. it is run to optimize and reorganize. It will walk the trie upwards
  62. towards the root from a given tnode, doing a resize() at each step
  63. to implement level compression.
  64. resize()
  65. Analyzes a tnode and optimizes the child array size by either inflating
  66. or shrinking it repeatedly until it fulfills the criteria for optimal
  67. level compression. This part follows the original paper pretty closely
  68. and there may be some room for experimentation here.
  69. inflate()
  70. Doubles the size of the child array within a tnode. Used by resize().
  71. halve()
  72. Halves the size of the child array within a tnode - the inverse of
  73. inflate(). Used by resize();
  74. fn_trie_insert(), fn_trie_delete(), fn_trie_select_default()
  75. The route manipulation functions. Should conform pretty closely to the
  76. corresponding functions in fib_hash.
  77. fn_trie_flush()
  78. This walks the full trie (using nextleaf()) and searches for empty
  79. leaves which have to be removed.
  80. fn_trie_dump()
  81. Dumps the routing table ordered by prefix length. This is somewhat
  82. slower than the corresponding fib_hash function, as we have to walk the
  83. entire trie for each prefix length. In comparison, fib_hash is organized
  84. as one "zone"/hash per prefix length.
  85. Locking
  86. -------
  87. fib_lock is used for an RW-lock in the same way that this is done in fib_hash.
  88. However, the functions are somewhat separated for other possible locking
  89. scenarios. It might conceivably be possible to run trie_rebalance via RCU
  90. to avoid read_lock in the fn_trie_lookup() function.
  91. Main lookup mechanism
  92. ---------------------
  93. fn_trie_lookup() is the main lookup function.
  94. The lookup is in its simplest form just like fib_find_node(). We descend the
  95. trie, key segment by key segment, until we find a leaf. check_leaf() does
  96. the fib_semantic_match in the leaf's sorted prefix hlist.
  97. If we find a match, we are done.
  98. If we don't find a match, we enter prefix matching mode. The prefix length,
  99. starting out at the same as the key length, is reduced one step at a time,
  100. and we backtrack upwards through the trie trying to find a longest matching
  101. prefix. The goal is always to reach a leaf and get a positive result from the
  102. fib_semantic_match mechanism.
  103. Inside each tnode, the search for longest matching prefix consists of searching
  104. through the child array, chopping off (zeroing) the least significant "1" of
  105. the child index until we find a match or the child index consists of nothing but
  106. zeros.
  107. At this point we backtrack (t->stats.backtrack++) up the trie, continuing to
  108. chop off part of the key in order to find the longest matching prefix.
  109. At this point we will repeatedly descend subtries to look for a match, and there
  110. are some optimizations available that can provide us with "shortcuts" to avoid
  111. descending into dead ends. Look for "HL_OPTIMIZE" sections in the code.
  112. To alleviate any doubts about the correctness of the route selection process,
  113. a new netlink operation has been added. Look for NETLINK_FIB_LOOKUP, which
  114. gives userland access to fib_lookup().