diff mbox series

[bpf-next,2/3] selftests/bpf: add test cases for BPF_MAP_TYPE_QUEUE

Message ID 153356391611.6981.14864460244240605372.stgit@kernel
State Changes Requested, archived
Delegated to: BPF Maintainers
Headers show
Series Implement bpf map queue | expand

Commit Message

Mauricio Vasquez Aug. 6, 2018, 1:58 p.m. UTC
Signed-off-by: Mauricio Vasquez B <mauricio.vasquez@polito.it>
---
 tools/include/uapi/linux/bpf.h          |    5 ++
 tools/testing/selftests/bpf/test_maps.c |   72 +++++++++++++++++++++++++++++++
 2 files changed, 77 insertions(+)

Comments

Daniel Borkmann Aug. 7, 2018, 1:42 p.m. UTC | #1
On 08/06/2018 03:58 PM, Mauricio Vasquez B wrote:
> Signed-off-by: Mauricio Vasquez B <mauricio.vasquez@polito.it>
> ---
>  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..34567b017dbb 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)
> +{
> +	__u32 value;
> +	int fd, i;
> +
> +	/* test FIFO */
> +	fd = bpf_create_map(BPF_MAP_TYPE_QUEUE, 0, sizeof(value), 32,
> +			    BPF_F_QUEUE_FIFO);

We should also feed in 'map_flags' so that both prealloc and non-prealloc
will be tested.

> +	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);

Ditto.

> +	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();
>
diff mbox series

Patch

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..34567b017dbb 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)
+{
+	__u32 value;
+	int fd, 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();