From: Quentin Deslandes <qde@naccy.de>
To: <qde@naccy.de>
Cc: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Martin KaFai Lau <martin.lau@linux.dev>,
Song Liu <song@kernel.org>, Yonghong Song <yhs@fb.com>,
John Fastabend <john.fastabend@gmail.com>,
KP Singh <kpsingh@kernel.org>,
Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>,
Jiri Olsa <jolsa@kernel.org>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Mykola Lysenko <mykolal@fb.com>, Shuah Khan <shuah@kernel.org>,
Dmitrii Banshchikov <me@ubique.spb.ru>,
<linux-kernel@vger.kernel.org>, <bpf@vger.kernel.org>,
<linux-kselftest@vger.kernel.org>, <netdev@vger.kernel.org>,
Kernel Team <kernel-team@meta.com>
Subject: [PATCH bpf-next v3 04/16] bpfilter: add map container
Date: Sat, 24 Dec 2022 01:03:50 +0100 [thread overview]
Message-ID: <20221224000402.476079-5-qde@naccy.de> (raw)
In-Reply-To: <20221224000402.476079-1-qde@naccy.de>
Introduce common code for an associative container. This common code
will be used for maps of matches, targets, and tables. Hash search
tables from libc are used as an index.
The supported sets of operations is: create, find, upsert, free.
Co-developed-by: Dmitrii Banshchikov <me@ubique.spb.ru>
Signed-off-by: Dmitrii Banshchikov <me@ubique.spb.ru>
Signed-off-by: Quentin Deslandes <qde@naccy.de>
---
net/bpfilter/Makefile | 2 +-
net/bpfilter/map-common.c | 51 +++++++++++++++
net/bpfilter/map-common.h | 19 ++++++
.../testing/selftests/bpf/bpfilter/.gitignore | 2 +
tools/testing/selftests/bpf/bpfilter/Makefile | 19 ++++++
.../testing/selftests/bpf/bpfilter/test_map.c | 63 +++++++++++++++++++
6 files changed, 155 insertions(+), 1 deletion(-)
create mode 100644 net/bpfilter/map-common.c
create mode 100644 net/bpfilter/map-common.h
create mode 100644 tools/testing/selftests/bpf/bpfilter/.gitignore
create mode 100644 tools/testing/selftests/bpf/bpfilter/Makefile
create mode 100644 tools/testing/selftests/bpf/bpfilter/test_map.c
diff --git a/net/bpfilter/Makefile b/net/bpfilter/Makefile
index 8d9c726ba1a5..1b0c399c19df 100644
--- a/net/bpfilter/Makefile
+++ b/net/bpfilter/Makefile
@@ -4,7 +4,7 @@
#
userprogs := bpfilter_umh
-bpfilter_umh-objs := main.o logger.o
+bpfilter_umh-objs := main.o logger.o map-common.o
userccflags += -I $(srctree)/tools/include/ -I $(srctree)/tools/include/uapi
ifeq ($(CONFIG_BPFILTER_UMH), y)
diff --git a/net/bpfilter/map-common.c b/net/bpfilter/map-common.c
new file mode 100644
index 000000000000..cc6c3a59b315
--- /dev/null
+++ b/net/bpfilter/map-common.c
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2021 Telegram FZ-LLC
+ * Copyright (c) 2022 Meta Platforms, Inc. and affiliates.
+ */
+
+#include "map-common.h"
+
+#include <linux/err.h>
+
+#include <errno.h>
+#include <string.h>
+
+int create_map(struct hsearch_data *htab, size_t nelem)
+{
+ memset(htab, 0, sizeof(*htab));
+ if (!hcreate_r(nelem, htab))
+ return -errno;
+
+ return 0;
+}
+
+void *map_find(struct hsearch_data *htab, const char *key)
+{
+ const ENTRY needle = { .key = (char *)key };
+ ENTRY *found;
+
+ if (!hsearch_r(needle, FIND, &found, htab))
+ return ERR_PTR(-ENOENT);
+
+ return found->data;
+}
+
+int map_upsert(struct hsearch_data *htab, const char *key, void *value)
+{
+ const ENTRY needle = { .key = (char *)key, .data = value };
+ ENTRY *found;
+
+ if (!hsearch_r(needle, ENTER, &found, htab))
+ return -errno;
+
+ found->key = (char *)key;
+ found->data = value;
+
+ return 0;
+}
+
+void free_map(struct hsearch_data *htab)
+{
+ hdestroy_r(htab);
+}
diff --git a/net/bpfilter/map-common.h b/net/bpfilter/map-common.h
new file mode 100644
index 000000000000..666a4ffe9b29
--- /dev/null
+++ b/net/bpfilter/map-common.h
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2021 Telegram FZ-LLC
+ * Copyright (c) 2022 Meta Platforms, Inc. and affiliates.
+ */
+
+#ifndef NET_BPFILTER_MAP_COMMON_H
+#define NET_BPFILTER_MAP_COMMON_H
+
+#define _GNU_SOURCE
+
+#include <search.h>
+
+int create_map(struct hsearch_data *htab, size_t nelem);
+void *map_find(struct hsearch_data *htab, const char *key);
+int map_upsert(struct hsearch_data *htab, const char *key, void *value);
+void free_map(struct hsearch_data *htab);
+
+#endif // NET_BPFILTER_MAP_COMMON_H
diff --git a/tools/testing/selftests/bpf/bpfilter/.gitignore b/tools/testing/selftests/bpf/bpfilter/.gitignore
new file mode 100644
index 000000000000..983fd06cbefa
--- /dev/null
+++ b/tools/testing/selftests/bpf/bpfilter/.gitignore
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+test_map
diff --git a/tools/testing/selftests/bpf/bpfilter/Makefile b/tools/testing/selftests/bpf/bpfilter/Makefile
new file mode 100644
index 000000000000..c262aad8c2a4
--- /dev/null
+++ b/tools/testing/selftests/bpf/bpfilter/Makefile
@@ -0,0 +1,19 @@
+# SPDX-License-Identifier: GPL-2.0
+
+top_srcdir = ../../../../..
+TOOLSDIR := $(abspath ../../../../)
+TOOLSINCDIR := $(TOOLSDIR)/include
+APIDIR := $(TOOLSINCDIR)/uapi
+BPFILTERSRCDIR := $(top_srcdir)/net/bpfilter
+
+CFLAGS += -Wall -g -pthread -I$(TOOLSINCDIR) -I$(APIDIR) -I$(BPFILTERSRCDIR)
+
+TEST_GEN_PROGS += test_map
+
+KSFT_KHDR_INSTALL := 1
+
+include ../../lib.mk
+
+BPFILTER_MAP_SRCS := $(BPFILTERSRCDIR)/map-common.c
+
+$(OUTPUT)/test_map: test_map.c $(BPFILTER_MAP_SRCS)
diff --git a/tools/testing/selftests/bpf/bpfilter/test_map.c b/tools/testing/selftests/bpf/bpfilter/test_map.c
new file mode 100644
index 000000000000..7ed737b78816
--- /dev/null
+++ b/tools/testing/selftests/bpf/bpfilter/test_map.c
@@ -0,0 +1,63 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "map-common.h"
+
+#include <linux/err.h>
+
+#include "../../kselftest_harness.h"
+
+FIXTURE(test_map)
+{
+ struct hsearch_data map;
+ const char *key;
+ void *expected;
+ void *actual;
+};
+
+FIXTURE_SETUP(test_map)
+{
+ const int max_nelements = 100;
+
+ create_map(&self->map, max_nelements);
+ self->key = "key";
+ self->expected = "expected";
+ self->actual = "actual";
+}
+
+FIXTURE_TEARDOWN(test_map)
+{
+ free_map(&self->map);
+}
+
+TEST_F(test_map, upsert_and_find)
+{
+ void *found;
+
+ found = map_find(&self->map, self->key);
+ ASSERT_TRUE(IS_ERR(found))
+ ASSERT_EQ(-ENOENT, PTR_ERR(found))
+
+ ASSERT_EQ(0, map_upsert(&self->map, self->key, self->expected));
+ ASSERT_EQ(0, map_upsert(&self->map, self->key, self->expected));
+ ASSERT_EQ(0, map_upsert(&self->map, self->key, self->actual));
+
+ found = map_find(&self->map, self->key);
+
+ ASSERT_FALSE(IS_ERR(found));
+ ASSERT_STREQ(self->actual, found);
+}
+
+TEST_F(test_map, update)
+{
+ void *found;
+
+ ASSERT_EQ(0, map_upsert(&self->map, self->key, self->actual));
+ ASSERT_EQ(0, map_upsert(&self->map, self->key, self->expected));
+
+ found = map_find(&self->map, self->key);
+
+ ASSERT_FALSE(IS_ERR(found));
+ ASSERT_STREQ(self->expected, found);
+}
+
+TEST_HARNESS_MAIN
--
2.38.1
next prev parent reply other threads:[~2022-12-24 0:12 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-24 0:03 [PATCH bpf-next v3 00/16] bpfilter Quentin Deslandes
2022-12-24 0:03 ` [PATCH bpf-next v3 01/16] bpfilter: add types for usermode helper Quentin Deslandes
2022-12-24 0:03 ` [PATCH bpf-next v3 02/16] tools: add bpfilter usermode helper header Quentin Deslandes
2022-12-24 0:03 ` [PATCH bpf-next v3 03/16] bpfilter: add logging facility Quentin Deslandes
2022-12-24 0:03 ` Quentin Deslandes [this message]
2022-12-24 0:03 ` [PATCH bpf-next v3 05/16] bpfilter: add runtime context Quentin Deslandes
2022-12-24 0:03 ` [PATCH bpf-next v3 06/16] bpfilter: add BPF bytecode generation infrastructure Quentin Deslandes
2022-12-24 0:03 ` [PATCH bpf-next v3 07/16] bpfilter: add support for TC bytecode generation Quentin Deslandes
2022-12-24 0:03 ` [PATCH bpf-next v3 08/16] bpfilter: add match structure Quentin Deslandes
2022-12-24 0:03 ` [PATCH bpf-next v3 09/16] bpfilter: add support for src/dst addr and ports Quentin Deslandes
2022-12-24 0:03 ` [PATCH bpf-next v3 10/16] bpfilter: add target structure Quentin Deslandes
2022-12-24 0:03 ` [PATCH bpf-next v3 11/16] bpfilter: add rule structure Quentin Deslandes
2022-12-24 0:03 ` [PATCH bpf-next v3 12/16] bpfilter: add table structure Quentin Deslandes
2022-12-24 0:03 ` [PATCH bpf-next v3 13/16] bpfilter: add table code generation Quentin Deslandes
2022-12-24 0:04 ` [PATCH bpf-next v3 14/16] bpfilter: add setsockopt() support Quentin Deslandes
2022-12-24 0:04 ` [PATCH bpf-next v3 15/16] bpfilter: add filter table Quentin Deslandes
2022-12-24 0:04 ` [PATCH bpf-next v3 16/16] bpfilter: handle setsockopt() calls Quentin Deslandes
2022-12-27 18:22 ` [PATCH bpf-next v3 00/16] bpfilter Alexei Starovoitov
2023-01-03 11:38 ` Florian Westphal
2023-01-06 14:15 ` Quentin Deslandes
2023-01-12 3:03 ` Florian Westphal
2023-01-03 11:45 ` Florian Westphal
2023-01-06 14:43 ` Quentin Deslandes
2023-01-12 3:17 ` Florian Westphal
2023-01-25 10:25 ` Quentin Deslandes
-- strict thread matches above, loose matches on Subject: below --
2022-12-23 23:40 Quentin Deslandes
2022-12-23 23:40 ` [PATCH bpf-next v3 04/16] bpfilter: add map container Quentin Deslandes
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=20221224000402.476079-5-qde@naccy.de \
--to=qde@naccy.de \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kernel-team@meta.com \
--cc=kpsingh@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=me@ubique.spb.ru \
--cc=mykolal@fb.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=sdf@google.com \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=yhs@fb.com \
/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