Lines Matching full:node
18 /* Intermediate node */
48 * lead to more nodes containing more specific matches. Each node also stores
58 * As the trie is empty initially, the new node (1) will be places as root
59 * node, denoted as (R) in the example below. As there are no other node, both
69 * Next, let's add a new node (2) matching 192.168.0.0/24. As there is already
70 * a node with the same data and a smaller prefix (ie, a less specific one),
71 * node (2) will become a child of (1). In child index depends on the next bit
89 * The child[1] slot of (1) could be filled with another node which has bit #17
107 * Let's add another node (4) to the game for 192.168.1.0/24. In order to place
108 * it, node (1) is looked at first, and because (4) of the semantics laid out
110 * However, that slot is already allocated, so a new node is needed in between.
111 * That node does not have a value attached to it and it will never be
139 * An intermediate node will be turned into a 'real' node on demand. In the
145 * The lookup starts at the root node. If the current node matches and if there
147 * downwards. The last node in the traversal that is a non-intermediate one is
159 * @node: The node to operate on
160 * @key: The key to compare to @node
162 * Determine the longest prefix of @node that matches the bits in @key.
165 const struct lpm_trie_node *node, in longest_prefix_match() argument
168 u32 limit = min(node->prefixlen, key->prefixlen); in longest_prefix_match()
180 u64 diff = be64_to_cpu(*(__be64 *)node->data ^ in longest_prefix_match()
193 u32 diff = be32_to_cpu(*(__be32 *)&node->data[i] ^ in longest_prefix_match()
205 u16 diff = be16_to_cpu(*(__be16 *)&node->data[i] ^ in longest_prefix_match()
217 prefixlen += 8 - fls(node->data[i] ^ key->data[i]); in longest_prefix_match()
230 struct lpm_trie_node *node, *found = NULL; in trie_lookup_elem() local
233 /* Start walking the trie from the root node ... */ in trie_lookup_elem()
235 for (node = rcu_dereference(trie->root); node;) { in trie_lookup_elem()
239 /* Determine the longest prefix of @node that matches @key. in trie_lookup_elem()
243 matchlen = longest_prefix_match(trie, node, key); in trie_lookup_elem()
245 found = node; in trie_lookup_elem()
250 * length of @node, bail out and return the node we have seen in trie_lookup_elem()
253 if (matchlen < node->prefixlen) in trie_lookup_elem()
256 /* Consider this node as return candidate unless it is an in trie_lookup_elem()
259 if (!(node->flags & LPM_TREE_NODE_FLAG_IM)) in trie_lookup_elem()
260 found = node; in trie_lookup_elem()
262 /* If the node match is fully satisfied, let's see if we can in trie_lookup_elem()
266 next_bit = extract_bit(key->data, node->prefixlen); in trie_lookup_elem()
267 node = rcu_dereference(node->child[next_bit]); in trie_lookup_elem()
279 struct lpm_trie_node *node; in lpm_trie_node_alloc() local
285 node = kmalloc_node(size, GFP_ATOMIC | __GFP_NOWARN, in lpm_trie_node_alloc()
287 if (!node) in lpm_trie_node_alloc()
290 node->flags = 0; in lpm_trie_node_alloc()
293 memcpy(node->data + trie->data_size, value, in lpm_trie_node_alloc()
296 return node; in lpm_trie_node_alloc()
304 struct lpm_trie_node *node, *im_node = NULL, *new_node = NULL; in trie_update_elem() local
320 /* Allocate and fill a new node */ in trie_update_elem()
340 /* Now find a slot to attach the new node. To do that, walk the tree in trie_update_elem()
341 * from the root and match as many bits as possible for each node until in trie_update_elem()
343 * an intermediate node. in trie_update_elem()
347 while ((node = rcu_dereference_protected(*slot, in trie_update_elem()
349 matchlen = longest_prefix_match(trie, node, key); in trie_update_elem()
351 if (node->prefixlen != matchlen || in trie_update_elem()
352 node->prefixlen == key->prefixlen || in trie_update_elem()
353 node->prefixlen == trie->max_prefixlen) in trie_update_elem()
356 next_bit = extract_bit(key->data, node->prefixlen); in trie_update_elem()
357 slot = &node->child[next_bit]; in trie_update_elem()
363 if (!node) { in trie_update_elem()
371 if (node->prefixlen == matchlen) { in trie_update_elem()
372 new_node->child[0] = node->child[0]; in trie_update_elem()
373 new_node->child[1] = node->child[1]; in trie_update_elem()
375 if (!(node->flags & LPM_TREE_NODE_FLAG_IM)) in trie_update_elem()
379 kfree_rcu(node, rcu); in trie_update_elem()
384 /* If the new node matches the prefix completely, it must be inserted in trie_update_elem()
385 * as an ancestor. Simply insert it between @node and *@slot. in trie_update_elem()
388 next_bit = extract_bit(node->data, matchlen); in trie_update_elem()
389 rcu_assign_pointer(new_node->child[next_bit], node); in trie_update_elem()
402 memcpy(im_node->data, node->data, trie->data_size); in trie_update_elem()
406 rcu_assign_pointer(im_node->child[0], node); in trie_update_elem()
410 rcu_assign_pointer(im_node->child[1], node); in trie_update_elem()
413 /* Finally, assign the intermediate node to the determined spot */ in trie_update_elem()
436 struct lpm_trie_node *node, *parent; in trie_delete_elem() local
448 * track of the path we traverse. We will need to know the node in trie_delete_elem()
449 * we wish to delete, and the slot that points to the node we want in trie_delete_elem()
456 while ((node = rcu_dereference_protected( in trie_delete_elem()
458 matchlen = longest_prefix_match(trie, node, key); in trie_delete_elem()
460 if (node->prefixlen != matchlen || in trie_delete_elem()
461 node->prefixlen == key->prefixlen) in trie_delete_elem()
464 parent = node; in trie_delete_elem()
466 next_bit = extract_bit(key->data, node->prefixlen); in trie_delete_elem()
467 trim = &node->child[next_bit]; in trie_delete_elem()
470 if (!node || node->prefixlen != key->prefixlen || in trie_delete_elem()
471 node->prefixlen != matchlen || in trie_delete_elem()
472 (node->flags & LPM_TREE_NODE_FLAG_IM)) { in trie_delete_elem()
479 /* If the node we are removing has two children, simply mark it in trie_delete_elem()
482 if (rcu_access_pointer(node->child[0]) && in trie_delete_elem()
483 rcu_access_pointer(node->child[1])) { in trie_delete_elem()
484 node->flags |= LPM_TREE_NODE_FLAG_IM; in trie_delete_elem()
488 /* If the parent of the node we are about to delete is an intermediate in trie_delete_elem()
489 * node, and the deleted node doesn't have any children, we can delete in trie_delete_elem()
496 !node->child[0] && !node->child[1]) { in trie_delete_elem()
497 if (node == rcu_access_pointer(parent->child[0])) in trie_delete_elem()
504 kfree_rcu(node, rcu); in trie_delete_elem()
508 /* The node we are removing has either zero or one child. If there in trie_delete_elem()
509 * is a child, move it into the removed node's slot then delete in trie_delete_elem()
510 * the node. Otherwise just clear the slot and delete the node. in trie_delete_elem()
512 if (node->child[0]) in trie_delete_elem()
513 rcu_assign_pointer(*trim, rcu_access_pointer(node->child[0])); in trie_delete_elem()
514 else if (node->child[1]) in trie_delete_elem()
515 rcu_assign_pointer(*trim, rcu_access_pointer(node->child[1])); in trie_delete_elem()
518 kfree_rcu(node, rcu); in trie_delete_elem()
590 struct lpm_trie_node *node; in trie_free() local
592 /* Always start at the root and walk down to a node that has no in trie_free()
593 * children. Then free that node, nullify its reference in the parent in trie_free()
601 node = rcu_dereference_protected(*slot, 1); in trie_free()
602 if (!node) in trie_free()
605 if (rcu_access_pointer(node->child[0])) { in trie_free()
606 slot = &node->child[0]; in trie_free()
610 if (rcu_access_pointer(node->child[1])) { in trie_free()
611 slot = &node->child[1]; in trie_free()
615 kfree(node); in trie_free()
627 struct lpm_trie_node *node, *next_node = NULL, *parent, *search_root; in trie_get_next_key() local
635 /* The get_next_key follows postorder. For the 4 node example in in trie_get_next_key()
651 /* For invalid key, find the leftmost node in the trie */ in trie_get_next_key()
661 /* Try to find the exact node for the given key */ in trie_get_next_key()
662 for (node = search_root; node;) { in trie_get_next_key()
663 node_stack[++stack_ptr] = node; in trie_get_next_key()
664 matchlen = longest_prefix_match(trie, node, key); in trie_get_next_key()
665 if (node->prefixlen != matchlen || in trie_get_next_key()
666 node->prefixlen == key->prefixlen) in trie_get_next_key()
669 next_bit = extract_bit(key->data, node->prefixlen); in trie_get_next_key()
670 node = rcu_dereference(node->child[next_bit]); in trie_get_next_key()
672 if (!node || node->prefixlen != key->prefixlen || in trie_get_next_key()
673 (node->flags & LPM_TREE_NODE_FLAG_IM)) in trie_get_next_key()
676 /* The node with the exactly-matching key has been found, in trie_get_next_key()
677 * find the first node in postorder after the matched node. in trie_get_next_key()
679 node = node_stack[stack_ptr]; in trie_get_next_key()
682 if (rcu_dereference(parent->child[0]) == node) { in trie_get_next_key()
692 node = parent; in trie_get_next_key()
701 /* Find the leftmost non-intermediate node, all intermediate nodes in trie_get_next_key()
704 for (node = search_root; node;) { in trie_get_next_key()
705 if (node->flags & LPM_TREE_NODE_FLAG_IM) { in trie_get_next_key()
706 node = rcu_dereference(node->child[0]); in trie_get_next_key()
708 next_node = node; in trie_get_next_key()
709 node = rcu_dereference(node->child[0]); in trie_get_next_key()
710 if (!node) in trie_get_next_key()
711 node = rcu_dereference(next_node->child[1]); in trie_get_next_key()