Topics

[RFC PATCH 2/3] tests/bpf: add some test cases for BPF_MAP_TYPE_QUEUE

Mauricio Vasquez
 

Signed-off-by: Mauricio Vasquez B <mauricio.vasquez@...>
---
tools/include/uapi/linux/bpf.h | 5 ++
tools/testing/selftests/bpf/test_maps.c | 72 +++++++++++++++++++++++++++++++
2 files changed, 77 insertions(+)

diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 0ebaaf7f3568..2c171c40eb45 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -120,6 +120,7 @@ enum bpf_map_type {
BPF_MAP_TYPE_CPUMAP,
BPF_MAP_TYPE_XSKMAP,
BPF_MAP_TYPE_SOCKHASH,
+ BPF_MAP_TYPE_QUEUE,
};

enum bpf_prog_type {
@@ -255,6 +256,10 @@ enum bpf_attach_type {
/* Flag for stack_map, store build_id+offset instead of pointer */
#define BPF_F_STACK_BUILD_ID (1U << 5)

+/* Flags for queue_map, type of queue */
+#define BPF_F_QUEUE_FIFO (1U << 16)
+#define BPF_F_QUEUE_LIFO (2U << 16)
+
enum bpf_stack_build_id_status {
/* user space need an empty entry to identify end of a trace */
BPF_STACK_BUILD_ID_EMPTY = 0,
diff --git a/tools/testing/selftests/bpf/test_maps.c b/tools/testing/selftests/bpf/test_maps.c
index 6c253343a6f9..9d51f539f094 100644
--- a/tools/testing/selftests/bpf/test_maps.c
+++ b/tools/testing/selftests/bpf/test_maps.c
@@ -457,6 +457,77 @@ static void test_devmap(int task, void *data)
close(fd);
}

+static void test_queuemap(int task, void *data)
+{
+ int fd;
+ __u32 value;
+ int i;
+
+ /* test FIFO */
+ fd = bpf_create_map(BPF_MAP_TYPE_QUEUE, 0, sizeof(value), 32,
+ BPF_F_QUEUE_FIFO);
+ if (fd < 0) {
+ printf("Failed to create queuemap '%s'!\n", strerror(errno));
+ exit(1);
+ }
+
+ /* Push 32 elements */
+ for (i = 0; i < 32; i++) {
+ value = 1000 - i * 3;
+ assert(bpf_map_update_elem(fd, NULL, &value, 0) == 0);
+ }
+
+ /* Check that element cannot be pushed due to max_entries limit */
+ value = 1000;
+ assert(bpf_map_update_elem(fd, NULL, &value, 0) == -1 && errno == E2BIG);
+
+ /* Pop all elements */
+ for (i = 0; i < 32; i++)
+ assert(bpf_map_lookup_elem(fd, NULL, &value) == 0 &&
+ value == (1000 - i * 3));
+
+ /* Check that there are not elements left */
+ assert(bpf_map_lookup_elem(fd, NULL, &value) == -1 && errno == ENOENT);
+
+ assert(bpf_map_delete_elem(fd, NULL) == -1 && errno == EINVAL);
+ assert(bpf_map_get_next_key(fd, NULL, NULL) == -1 && errno == EINVAL);
+
+ close(fd);
+
+ /* test LIFO */
+ fd = bpf_create_map(BPF_MAP_TYPE_QUEUE, 0, sizeof(value), 32,
+ BPF_F_QUEUE_LIFO);
+ if (fd < 0) {
+ printf("Failed to create queuemap '%s'!\n", strerror(errno));
+ exit(1);
+ }
+
+ /* Push 32 elements */
+ for (i = 0; i < 32; i++) {
+ value = 1000 - i * 3;
+ assert(bpf_map_update_elem(fd, NULL, &value, 0) == 0);
+ }
+
+ /* Check that element cannot be pushed due to max_entries limit */
+ value = 1000;
+ assert(bpf_map_update_elem(fd, NULL, &value, 0) == -1 &&
+ errno == E2BIG);
+
+ /* Pop all elements */
+ for (i = 31; i >= 0; i--)
+ assert(bpf_map_lookup_elem(fd, NULL, &value) == 0 &&
+ value == (1000 - i * 3));
+
+ /* Check that there are not elements left */
+ assert(bpf_map_lookup_elem(fd, NULL, &value) == -1 &&
+ errno == ENOENT);
+
+ assert(bpf_map_delete_elem(fd, NULL) == -1 && errno == EINVAL);
+ assert(bpf_map_get_next_key(fd, NULL, NULL) == -1 && errno == EINVAL);
+
+ close(fd);
+}
+
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
@@ -1162,6 +1233,7 @@ static void run_all_tests(void)
test_arraymap_percpu_many_keys();

test_devmap(0, NULL);
+ test_queuemap(0, NULL);
test_sockmap(0, NULL);

test_map_large();

Yonghong Song
 

On Tue, Jul 31, 2018 at 1:53 PM, Mauricio Vasquez
<mauricio.vasquez@...> wrote:
Signed-off-by: Mauricio Vasquez B <mauricio.vasquez@...>
---
tools/include/uapi/linux/bpf.h | 5 ++
tools/testing/selftests/bpf/test_maps.c | 72 +++++++++++++++++++++++++++++++
2 files changed, 77 insertions(+)

diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 0ebaaf7f3568..2c171c40eb45 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -120,6 +120,7 @@ enum bpf_map_type {
BPF_MAP_TYPE_CPUMAP,
BPF_MAP_TYPE_XSKMAP,
BPF_MAP_TYPE_SOCKHASH,
+ BPF_MAP_TYPE_QUEUE,
};

enum bpf_prog_type {
@@ -255,6 +256,10 @@ enum bpf_attach_type {
/* Flag for stack_map, store build_id+offset instead of pointer */
#define BPF_F_STACK_BUILD_ID (1U << 5)

+/* Flags for queue_map, type of queue */
+#define BPF_F_QUEUE_FIFO (1U << 16)
+#define BPF_F_QUEUE_LIFO (2U << 16)
+
enum bpf_stack_build_id_status {
/* user space need an empty entry to identify end of a trace */
BPF_STACK_BUILD_ID_EMPTY = 0,
diff --git a/tools/testing/selftests/bpf/test_maps.c b/tools/testing/selftests/bpf/test_maps.c
index 6c253343a6f9..9d51f539f094 100644
--- a/tools/testing/selftests/bpf/test_maps.c
+++ b/tools/testing/selftests/bpf/test_maps.c
@@ -457,6 +457,77 @@ static void test_devmap(int task, void *data)
close(fd);
}

+static void test_queuemap(int task, void *data)
+{
+ int fd;
+ __u32 value;
+ int i;
Order the declaration based on reverse Christmas tree shape.

+
+ /* test FIFO */
+ fd = bpf_create_map(BPF_MAP_TYPE_QUEUE, 0, sizeof(value), 32,
+ BPF_F_QUEUE_FIFO);
+ if (fd < 0) {
+ printf("Failed to create queuemap '%s'!\n", strerror(errno));
+ exit(1);
+ }
+
+ /* Push 32 elements */
+ for (i = 0; i < 32; i++) {
+ value = 1000 - i * 3;
+ assert(bpf_map_update_elem(fd, NULL, &value, 0) == 0);
+ }
+
+ /* Check that element cannot be pushed due to max_entries limit */
+ value = 1000;
+ assert(bpf_map_update_elem(fd, NULL, &value, 0) == -1 && errno == E2BIG);
+
+ /* Pop all elements */
+ for (i = 0; i < 32; i++)
+ assert(bpf_map_lookup_elem(fd, NULL, &value) == 0 &&
+ value == (1000 - i * 3));
+
+ /* Check that there are not elements left */
+ assert(bpf_map_lookup_elem(fd, NULL, &value) == -1 && errno == ENOENT);
+
+ assert(bpf_map_delete_elem(fd, NULL) == -1 && errno == EINVAL);
+ assert(bpf_map_get_next_key(fd, NULL, NULL) == -1 && errno == EINVAL);
+
+ close(fd);
+
+ /* test LIFO */
+ fd = bpf_create_map(BPF_MAP_TYPE_QUEUE, 0, sizeof(value), 32,
+ BPF_F_QUEUE_LIFO);
+ if (fd < 0) {
+ printf("Failed to create queuemap '%s'!\n", strerror(errno));
+ exit(1);
+ }
+
+ /* Push 32 elements */
+ for (i = 0; i < 32; i++) {
+ value = 1000 - i * 3;
+ assert(bpf_map_update_elem(fd, NULL, &value, 0) == 0);
+ }
+
+ /* Check that element cannot be pushed due to max_entries limit */
+ value = 1000;
+ assert(bpf_map_update_elem(fd, NULL, &value, 0) == -1 &&
+ errno == E2BIG);
+
+ /* Pop all elements */
+ for (i = 31; i >= 0; i--)
+ assert(bpf_map_lookup_elem(fd, NULL, &value) == 0 &&
+ value == (1000 - i * 3));
+
+ /* Check that there are not elements left */
+ assert(bpf_map_lookup_elem(fd, NULL, &value) == -1 &&
+ errno == ENOENT);
+
+ assert(bpf_map_delete_elem(fd, NULL) == -1 && errno == EINVAL);
+ assert(bpf_map_get_next_key(fd, NULL, NULL) == -1 && errno == EINVAL);
+
+ close(fd);
+}
+
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
@@ -1162,6 +1233,7 @@ static void run_all_tests(void)
test_arraymap_percpu_many_keys();

test_devmap(0, NULL);
+ test_queuemap(0, NULL);
test_sockmap(0, NULL);

test_map_large();