From: Craig Gallek <kraigatgoog@gmail.com>
To: Daniel Mack <daniel@zonque.org>, Alexei Starovoitov <ast@fb.com>,
Daniel Borkmann <daniel@iogearbox.net>,
"David S . Miller" <davem@davemloft.net>
Cc: netdev@vger.kernel.org
Subject: [PATCH net-next 1/3] bpf: Implement map_delete_elem for BPF_MAP_TYPE_LPM_TRIE
Date: Mon, 18 Sep 2017 15:30:55 -0400 [thread overview]
Message-ID: <20170918193057.37644-2-kraigatgoog@gmail.com> (raw)
In-Reply-To: <20170918193057.37644-1-kraigatgoog@gmail.com>
From: Craig Gallek <kraig@google.com>
This is a simple non-recursive delete operation. It prunes paths
of empty nodes in the tree, but it does not try to further compress
the tree as nodes are removed.
Signed-off-by: Craig Gallek <kraig@google.com>
---
kernel/bpf/lpm_trie.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 77 insertions(+), 3 deletions(-)
diff --git a/kernel/bpf/lpm_trie.c b/kernel/bpf/lpm_trie.c
index 1b767844a76f..9d58a576b2ae 100644
--- a/kernel/bpf/lpm_trie.c
+++ b/kernel/bpf/lpm_trie.c
@@ -389,10 +389,84 @@ static int trie_update_elem(struct bpf_map *map,
return ret;
}
-static int trie_delete_elem(struct bpf_map *map, void *key)
+/* Called from syscall or from eBPF program */
+static int trie_delete_elem(struct bpf_map *map, void *_key)
{
- /* TODO */
- return -ENOSYS;
+ struct lpm_trie *trie = container_of(map, struct lpm_trie, map);
+ struct bpf_lpm_trie_key *key = _key;
+ struct lpm_trie_node __rcu **trim;
+ struct lpm_trie_node *node;
+ unsigned long irq_flags;
+ unsigned int next_bit;
+ size_t matchlen = 0;
+ int ret = 0;
+
+ if (key->prefixlen > trie->max_prefixlen)
+ return -EINVAL;
+
+ raw_spin_lock_irqsave(&trie->lock, irq_flags);
+
+ /* Walk the tree looking for an exact key/length match and keeping
+ * track of where we could begin trimming the tree. The trim-point
+ * is the sub-tree along the walk consisting of only single-child
+ * intermediate nodes and ending at a leaf node that we want to
+ * remove.
+ */
+ trim = &trie->root;
+ node = rcu_dereference_protected(
+ trie->root, lockdep_is_held(&trie->lock));
+ while (node) {
+ matchlen = longest_prefix_match(trie, node, key);
+
+ if (node->prefixlen != matchlen ||
+ node->prefixlen == key->prefixlen)
+ break;
+
+ next_bit = extract_bit(key->data, node->prefixlen);
+ /* If we hit a node that has more than one child or is a valid
+ * prefix itself, do not remove it. Reset the root of the trim
+ * path to its descendant on our path.
+ */
+ if (!(node->flags & LPM_TREE_NODE_FLAG_IM) ||
+ (node->child[0] && node->child[1]))
+ trim = &node->child[next_bit];
+ node = rcu_dereference_protected(
+ node->child[next_bit], lockdep_is_held(&trie->lock));
+ }
+
+ if (!node || node->prefixlen != key->prefixlen ||
+ (node->flags & LPM_TREE_NODE_FLAG_IM)) {
+ ret = -ENOENT;
+ goto out;
+ }
+
+ trie->n_entries--;
+
+ /* If the node we are removing is not a leaf node, simply mark it
+ * as intermediate and we are done.
+ */
+ if (rcu_access_pointer(node->child[0]) ||
+ rcu_access_pointer(node->child[1])) {
+ node->flags |= LPM_TREE_NODE_FLAG_IM;
+ goto out;
+ }
+
+ /* trim should now point to the slot holding the start of a path from
+ * zero or more intermediate nodes to our leaf node for deletion.
+ */
+ while ((node = rcu_dereference_protected(
+ *trim, lockdep_is_held(&trie->lock)))) {
+ RCU_INIT_POINTER(*trim, NULL);
+ trim = rcu_access_pointer(node->child[0]) ?
+ &node->child[0] :
+ &node->child[1];
+ kfree_rcu(node, rcu);
+ }
+
+out:
+ raw_spin_unlock_irqrestore(&trie->lock, irq_flags);
+
+ return ret;
}
#define LPM_DATA_SIZE_MAX 256
--
2.14.1.690.gbb1197296e-goog
next prev parent reply other threads:[~2017-09-18 19:31 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-09-18 19:30 [PATCH net-next 0/3] Implement delete for BPF LPM trie Craig Gallek
2017-09-18 19:30 ` Craig Gallek [this message]
2017-09-18 22:53 ` [PATCH net-next 1/3] bpf: Implement map_delete_elem for BPF_MAP_TYPE_LPM_TRIE Alexei Starovoitov
2017-09-19 15:08 ` Craig Gallek
2017-09-19 16:12 ` Daniel Borkmann
2017-09-19 19:48 ` Daniel Mack
2017-09-18 19:30 ` [PATCH net-next 2/3] bpf: Add uniqueness invariant to trivial lpm test implementation Craig Gallek
2017-09-18 22:54 ` Alexei Starovoitov
2017-09-19 16:12 ` Daniel Borkmann
2017-09-18 19:30 ` [PATCH net-next 3/3] bpf: Test deletion in BPF_MAP_TYPE_LPM_TRIE Craig Gallek
2017-09-18 22:54 ` Alexei Starovoitov
2017-09-19 16:12 ` Daniel Borkmann
2017-09-19 20:55 ` [PATCH net-next 0/3] Implement delete for BPF LPM trie David Miller
2017-09-19 21:13 ` Daniel Mack
2017-09-19 21:16 ` Craig Gallek
2017-09-19 21:29 ` David Miller
2017-09-19 21:31 ` Daniel Mack
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170918193057.37644-2-kraigatgoog@gmail.com \
--to=kraigatgoog@gmail.com \
--cc=ast@fb.com \
--cc=daniel@iogearbox.net \
--cc=daniel@zonque.org \
--cc=davem@davemloft.net \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).