All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next v2] libbpf: Add documentation for bpf_map batch operations
@ 2021-12-25 20:37 grantseltzer
  2021-12-27  4:32   ` kernel test robot
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: grantseltzer @ 2021-12-25 20:37 UTC (permalink / raw)
  To: bpf; +Cc: andrii, grantseltzer

From: Grant Seltzer <grantseltzer@gmail.com>

This adds documentation for:

- bpf_map_delete_batch()
- bpf_map_lookup_batch()
- bpf_map_lookup_and_delete_batch()
- bpf_map_update_batch()

Signed-off-by: Grant Seltzer <grantseltzer@gmail.com>
---
 tools/lib/bpf/bpf.c |   4 +-
 tools/lib/bpf/bpf.h | 112 +++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 112 insertions(+), 4 deletions(-)

diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index 9b64eed2b003..25f3d6f85fe5 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -691,7 +691,7 @@ static int bpf_map_batch_common(int cmd, int fd, void  *in_batch,
 	return libbpf_err_errno(ret);
 }
 
-int bpf_map_delete_batch(int fd, void *keys, __u32 *count,
+int bpf_map_delete_batch(int fd, const void *keys, __u32 *count,
 			 const struct bpf_map_batch_opts *opts)
 {
 	return bpf_map_batch_common(BPF_MAP_DELETE_BATCH, fd, NULL,
@@ -715,7 +715,7 @@ int bpf_map_lookup_and_delete_batch(int fd, void *in_batch, void *out_batch,
 				    count, opts);
 }
 
-int bpf_map_update_batch(int fd, void *keys, void *values, __u32 *count,
+int bpf_map_update_batch(int fd, const void *keys, const void *values, __u32 *count,
 			 const struct bpf_map_batch_opts *opts)
 {
 	return bpf_map_batch_common(BPF_MAP_UPDATE_BATCH, fd, NULL, NULL,
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index 00619f64a040..01011747f127 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -254,20 +254,128 @@ struct bpf_map_batch_opts {
 };
 #define bpf_map_batch_opts__last_field flags
 
-LIBBPF_API int bpf_map_delete_batch(int fd, void *keys,
+
+/**
+ * @brief **bpf_map_delete_batch()** allows for batch deletion of multiple
+ * elements in a BPF map.
+ *
+ * @param fd BPF map file descriptor
+ * @param keys pointer to an array of *count* keys
+ * @param count number of elements in the map to sequentially delete
+ * @param opts options for configuring the way the batch deletion works
+ * @return 0, on success; negative error code, otherwise (errno is also set to
+ * the error code)
+ */
+LIBBPF_API int bpf_map_delete_batch(int fd, const void *keys,
 				    __u32 *count,
 				    const struct bpf_map_batch_opts *opts);
+
+/**
+ * @brief **bpf_map_lookup_batch()** allows for batch lookup of BPF map elements.
+ *
+ * The parameter *in_batch* is the address of the first element in the batch to read.
+ * *out_batch* is an output parameter that should be passed as *in_batch* to subsequent
+ * calls to **bpf_map_lookup_batch()**. NULL can be passed for *in_batch* to indicate
+ * that the batched lookup starts from the beginning of the map.
+ *
+ * The *keys* and *values* are output parameters which must point to memory large enough to
+ * hold *count* items based on the key and value size of the map *map_fd*. The *keys*
+ * buffer must be of *key_size* * *count*. The *values* buffer must be of
+ * *value_size* * *count*.
+ *
+ * @param fd BPF map file descriptor
+ * @param in_batch address of the first element in batch to read, can pass NULL to
+ * indicate that the batched lookup starts from the beginning of the map.
+ * @param out_batch output parameter that should be passed to next call as *in_batch*
+ * @param keys pointer to an array large enough for *count* keys
+ * @param values pointer to an array large enough for *count* values
+ * @param count number of elements in the map to read in batch. If ENOENT is
+ * returned, count will be set as the number of elements that were read before
+ * running out of entries in the map
+ * @param opts options for configuring the way the batch lookup works
+ * @return 0, on success; negative error code, otherwise (errno is also set to
+ * the error code)
+ */
 LIBBPF_API int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch,
 				    void *keys, void *values, __u32 *count,
 				    const struct bpf_map_batch_opts *opts);
+
+/**
+ * @brief **bpf_map_lookup_and_delete_batch()** allows for batch lookup and deletion
+ * of BPF map elements where each element is deleted after being retrieved.
+ *
+ * Note that *count* is an input and output parameter, where on output it
+ * represents how many elements were successfully deleted. Also note that if
+ * **EFAULT** is returned up to *count* elements may have been deleted without
+ * being returned via the *keys* and *values* output parameters. If **ENOENT**
+ * is returned then *count* will be set to the number of elements that were read
+ * before running out of entries in the map.
+ *
+ * @param fd BPF map file descriptor
+ * @param in_batch address of the first element in batch to read, can pass NULL to
+ * get address of the first element in *out_batch*
+ * @param out_batch output parameter that should be passed to next call as *in_batch*
+ * @param keys pointer to an array of *count* keys
+ * @param values pointer to an array large enough for *count* values
+ * @param count input and output parameter; on input it's the number of elements
+ * in the map to read and delete in batch; on output it represents number of elements
+ * that were successfully read and deleted
+ * If ENOENT is returned, count will be set as the number of elements that were
+ * read before running out of entries in the map
+ * @param opts options for configuring the way the batch lookup and delete works
+ * @return 0, on success; negative error code, otherwise (errno is also set to
+ * the error code)
+ */
 LIBBPF_API int bpf_map_lookup_and_delete_batch(int fd, void *in_batch,
 					void *out_batch, void *keys,
 					void *values, __u32 *count,
 					const struct bpf_map_batch_opts *opts);
-LIBBPF_API int bpf_map_update_batch(int fd, void *keys, void *values,
+
+/**
+ * @brief **bpf_map_update_batch()** updates multiple elements in a map
+ * by specifying keys and their corresponding values.
+ *
+ * The *keys* and *values* parameters must point to memory large enough
+ * to hold *count* items based on the key and value size of the map.
+ *
+ * The *opts* parameter can be used to control how *bpf_map_update_batch()*
+ * should handle keys that either do or do not already exist in the map.
+ * In particular the *flags* parameter of *bpf_map_batch_opts* can be
+ * one of the following:
+ *
+ * Note that *count* is an input and output parameter, where on output it
+ * represents how many elements were successfully updated. Also note that if
+ * **EFAULT** then *count* should not be trusted to be correct.
+ *
+ * **BPF_ANY**
+ *     Create new elements or update existing.
+ *
+ * **BPF_NOEXIST**
+ *    Create new elements only if they do not exist.
+ *
+ * **BPF_EXIST**
+ *    Update existing elements.
+ *
+ * **BPF_F_LOCK**
+ *    Update spin_lock-ed map elements. This must be
+ *    specified if the map value contains a spinlock.
+ *
+ * @param fd BPF map file descriptor
+ * @param keys pointer to an array of *count* keys
+ * @param values pointer to an array of *count* values
+ * @param count input and output parameter; on input it's the number of elements
+ * in the map to update in batch; on output it represents the number of elements
+ * that were successfully updated. If EFAULT is returned, *count* should not
+ * be trusted to be correct.
+ * @param opts options for configuring the way the batch update works
+ * @return 0, on success; negative error code, otherwise (errno is also set to
+ * the error code)
+ */
+LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values,
 				    __u32 *count,
 				    const struct bpf_map_batch_opts *opts);
 
+
 LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
 LIBBPF_API int bpf_obj_get(const char *pathname);
 
-- 
2.33.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH bpf-next v2] libbpf: Add documentation for bpf_map batch operations
  2021-12-25 20:37 [PATCH bpf-next v2] libbpf: Add documentation for bpf_map batch operations grantseltzer
@ 2021-12-27  4:32   ` kernel test robot
  2021-12-27 12:25 ` Hengqi Chen
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 9+ messages in thread
From: kernel test robot @ 2021-12-27  4:32 UTC (permalink / raw)
  To: grantseltzer; +Cc: llvm, kbuild-all

Hi grantseltzer,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on bpf-next/master]

url:    https://github.com/0day-ci/linux/commits/grantseltzer/libbpf-Add-documentation-for-bpf_map-batch-operations/20211226-043930
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: x86_64-randconfig-a004-20211227 (https://download.01.org/0day-ci/archive/20211227/202112271230.YWo0e6o5-lkp@intel.com/config)
compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project 511726c64d3b6cca66f7c54d457d586aa3129f67)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/0day-ci/linux/commit/f8daffaa56bc1d00f28a1bc7db7ca91d98ae3c53
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review grantseltzer/libbpf-Add-documentation-for-bpf_map-batch-operations/20211226-043930
        git checkout f8daffaa56bc1d00f28a1bc7db7ca91d98ae3c53
        # save the config file to linux build tree
        mkdir build_dir
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

>> bpf.c:698:15: error: passing 'const void *' to parameter of type 'void *' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
                                       NULL, keys, NULL, count, opts);
                                             ^~~~
   bpf.c:668:28: note: passing argument to parameter 'keys' here
                                   void *out_batch, void *keys, void *values,
                                                          ^
   bpf.c:722:9: error: passing 'const void *' to parameter of type 'void *' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
                                       keys, values, count, opts);
                                       ^~~~
   bpf.c:668:28: note: passing argument to parameter 'keys' here
                                   void *out_batch, void *keys, void *values,
                                                          ^
   bpf.c:722:15: error: passing 'const void *' to parameter of type 'void *' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
                                       keys, values, count, opts);
                                             ^~~~~~
   bpf.c:668:40: note: passing argument to parameter 'values' here
                                   void *out_batch, void *keys, void *values,
                                                                      ^
   3 errors generated.
   make[6]: *** [tools/build/Makefile.build:97: kernel/bpf/preload/libbpf/staticobjs/bpf.o] Error 1
   make[6]: Target '__build' not remade because of errors.

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH bpf-next v2] libbpf: Add documentation for bpf_map batch operations
@ 2021-12-27  4:32   ` kernel test robot
  0 siblings, 0 replies; 9+ messages in thread
From: kernel test robot @ 2021-12-27  4:32 UTC (permalink / raw)
  To: kbuild-all

[-- Attachment #1: Type: text/plain, Size: 3216 bytes --]

Hi grantseltzer,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on bpf-next/master]

url:    https://github.com/0day-ci/linux/commits/grantseltzer/libbpf-Add-documentation-for-bpf_map-batch-operations/20211226-043930
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: x86_64-randconfig-a004-20211227 (https://download.01.org/0day-ci/archive/20211227/202112271230.YWo0e6o5-lkp(a)intel.com/config)
compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project 511726c64d3b6cca66f7c54d457d586aa3129f67)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/0day-ci/linux/commit/f8daffaa56bc1d00f28a1bc7db7ca91d98ae3c53
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review grantseltzer/libbpf-Add-documentation-for-bpf_map-batch-operations/20211226-043930
        git checkout f8daffaa56bc1d00f28a1bc7db7ca91d98ae3c53
        # save the config file to linux build tree
        mkdir build_dir
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

>> bpf.c:698:15: error: passing 'const void *' to parameter of type 'void *' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
                                       NULL, keys, NULL, count, opts);
                                             ^~~~
   bpf.c:668:28: note: passing argument to parameter 'keys' here
                                   void *out_batch, void *keys, void *values,
                                                          ^
   bpf.c:722:9: error: passing 'const void *' to parameter of type 'void *' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
                                       keys, values, count, opts);
                                       ^~~~
   bpf.c:668:28: note: passing argument to parameter 'keys' here
                                   void *out_batch, void *keys, void *values,
                                                          ^
   bpf.c:722:15: error: passing 'const void *' to parameter of type 'void *' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
                                       keys, values, count, opts);
                                             ^~~~~~
   bpf.c:668:40: note: passing argument to parameter 'values' here
                                   void *out_batch, void *keys, void *values,
                                                                      ^
   3 errors generated.
   make[6]: *** [tools/build/Makefile.build:97: kernel/bpf/preload/libbpf/staticobjs/bpf.o] Error 1
   make[6]: Target '__build' not remade because of errors.

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH bpf-next v2] libbpf: Add documentation for bpf_map batch operations
  2021-12-25 20:37 [PATCH bpf-next v2] libbpf: Add documentation for bpf_map batch operations grantseltzer
  2021-12-27  4:32   ` kernel test robot
@ 2021-12-27 12:25 ` Hengqi Chen
  2022-01-03 18:08   ` Grant Seltzer Richman
  2022-01-06  2:37 ` Andrii Nakryiko
  2022-01-06  7:31 ` Yonghong Song
  3 siblings, 1 reply; 9+ messages in thread
From: Hengqi Chen @ 2021-12-27 12:25 UTC (permalink / raw)
  To: grantseltzer, bpf; +Cc: andrii



On 2021/12/26 4:37 AM, grantseltzer wrote:
> From: Grant Seltzer <grantseltzer@gmail.com>
> 
> This adds documentation for:
> 
> - bpf_map_delete_batch()
> - bpf_map_lookup_batch()
> - bpf_map_lookup_and_delete_batch()
> - bpf_map_update_batch()
> 
> Signed-off-by: Grant Seltzer <grantseltzer@gmail.com>
> ---
>  tools/lib/bpf/bpf.c |   4 +-
>  tools/lib/bpf/bpf.h | 112 +++++++++++++++++++++++++++++++++++++++++++-
>  2 files changed, 112 insertions(+), 4 deletions(-)
> 
> diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
> index 9b64eed2b003..25f3d6f85fe5 100644
> --- a/tools/lib/bpf/bpf.c
> +++ b/tools/lib/bpf/bpf.c
> @@ -691,7 +691,7 @@ static int bpf_map_batch_common(int cmd, int fd, void  *in_batch,
>  	return libbpf_err_errno(ret);
>  }
>  
> -int bpf_map_delete_batch(int fd, void *keys, __u32 *count,
> +int bpf_map_delete_batch(int fd, const void *keys, __u32 *count,

Maybe you should drop these const qualifier changes.

All batch operations use `bpf_map_batch_common`, which has the following signature:

static int bpf_map_batch_common(int cmd, int fd, void  *in_batch,
                                void *out_batch, void *keys, void *values,
                                __u32 *count,
                                const struct bpf_map_batch_opts *opts)

Adding these const qualifiers causes the following error:

bpf.c:698:15: error: passing argument 5 of ‘bpf_map_batch_common’ discards 
‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]

>  			 const struct bpf_map_batch_opts *opts)
>  {
>  	return bpf_map_batch_common(BPF_MAP_DELETE_BATCH, fd, NULL,
> @@ -715,7 +715,7 @@ int bpf_map_lookup_and_delete_batch(int fd, void *in_batch, void *out_batch,
>  				    count, opts);
>  }
>  
> -int bpf_map_update_batch(int fd, void *keys, void *values, __u32 *count,
> +int bpf_map_update_batch(int fd, const void *keys, const void *values, __u32 *count,
>  			 const struct bpf_map_batch_opts *opts)
>  {
>  	return bpf_map_batch_common(BPF_MAP_UPDATE_BATCH, fd, NULL, NULL,
> diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
> index 00619f64a040..01011747f127 100644
> --- a/tools/lib/bpf/bpf.h
> +++ b/tools/lib/bpf/bpf.h
> @@ -254,20 +254,128 @@ struct bpf_map_batch_opts {
>  };
>  #define bpf_map_batch_opts__last_field flags
>  
> -LIBBPF_API int bpf_map_delete_batch(int fd, void *keys,
> +
> +/**
> + * @brief **bpf_map_delete_batch()** allows for batch deletion of multiple
> + * elements in a BPF map.
> + *
> + * @param fd BPF map file descriptor
> + * @param keys pointer to an array of *count* keys
> + * @param count number of elements in the map to sequentially delete
> + * @param opts options for configuring the way the batch deletion works
> + * @return 0, on success; negative error code, otherwise (errno is also set to
> + * the error code)
> + */
> +LIBBPF_API int bpf_map_delete_batch(int fd, const void *keys,
>  				    __u32 *count,
>  				    const struct bpf_map_batch_opts *opts);
> +
> +/**
> + * @brief **bpf_map_lookup_batch()** allows for batch lookup of BPF map elements.
> + *
> + * The parameter *in_batch* is the address of the first element in the batch to read.
> + * *out_batch* is an output parameter that should be passed as *in_batch* to subsequent
> + * calls to **bpf_map_lookup_batch()**. NULL can be passed for *in_batch* to indicate
> + * that the batched lookup starts from the beginning of the map.
> + *
> + * The *keys* and *values* are output parameters which must point to memory large enough to
> + * hold *count* items based on the key and value size of the map *map_fd*. The *keys*
> + * buffer must be of *key_size* * *count*. The *values* buffer must be of
> + * *value_size* * *count*.
> + *
> + * @param fd BPF map file descriptor
> + * @param in_batch address of the first element in batch to read, can pass NULL to
> + * indicate that the batched lookup starts from the beginning of the map.
> + * @param out_batch output parameter that should be passed to next call as *in_batch*
> + * @param keys pointer to an array large enough for *count* keys
> + * @param values pointer to an array large enough for *count* values
> + * @param count number of elements in the map to read in batch. If ENOENT is
> + * returned, count will be set as the number of elements that were read before
> + * running out of entries in the map
> + * @param opts options for configuring the way the batch lookup works
> + * @return 0, on success; negative error code, otherwise (errno is also set to
> + * the error code)
> + */
>  LIBBPF_API int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch,
>  				    void *keys, void *values, __u32 *count,
>  				    const struct bpf_map_batch_opts *opts);
> +
> +/**
> + * @brief **bpf_map_lookup_and_delete_batch()** allows for batch lookup and deletion
> + * of BPF map elements where each element is deleted after being retrieved.
> + *
> + * Note that *count* is an input and output parameter, where on output it
> + * represents how many elements were successfully deleted. Also note that if
> + * **EFAULT** is returned up to *count* elements may have been deleted without
> + * being returned via the *keys* and *values* output parameters. If **ENOENT**
> + * is returned then *count* will be set to the number of elements that were read
> + * before running out of entries in the map.
> + *
> + * @param fd BPF map file descriptor
> + * @param in_batch address of the first element in batch to read, can pass NULL to
> + * get address of the first element in *out_batch*
> + * @param out_batch output parameter that should be passed to next call as *in_batch*
> + * @param keys pointer to an array of *count* keys
> + * @param values pointer to an array large enough for *count* values
> + * @param count input and output parameter; on input it's the number of elements
> + * in the map to read and delete in batch; on output it represents number of elements
> + * that were successfully read and deleted
> + * If ENOENT is returned, count will be set as the number of elements that were
> + * read before running out of entries in the map
> + * @param opts options for configuring the way the batch lookup and delete works
> + * @return 0, on success; negative error code, otherwise (errno is also set to
> + * the error code)
> + */
>  LIBBPF_API int bpf_map_lookup_and_delete_batch(int fd, void *in_batch,
>  					void *out_batch, void *keys,
>  					void *values, __u32 *count,
>  					const struct bpf_map_batch_opts *opts);
> -LIBBPF_API int bpf_map_update_batch(int fd, void *keys, void *values,
> +
> +/**
> + * @brief **bpf_map_update_batch()** updates multiple elements in a map
> + * by specifying keys and their corresponding values.
> + *
> + * The *keys* and *values* parameters must point to memory large enough
> + * to hold *count* items based on the key and value size of the map.
> + *
> + * The *opts* parameter can be used to control how *bpf_map_update_batch()*
> + * should handle keys that either do or do not already exist in the map.
> + * In particular the *flags* parameter of *bpf_map_batch_opts* can be
> + * one of the following:
> + *
> + * Note that *count* is an input and output parameter, where on output it
> + * represents how many elements were successfully updated. Also note that if
> + * **EFAULT** then *count* should not be trusted to be correct.
> + *
> + * **BPF_ANY**
> + *     Create new elements or update existing.
> + *
> + * **BPF_NOEXIST**
> + *    Create new elements only if they do not exist.
> + *
> + * **BPF_EXIST**
> + *    Update existing elements.
> + *
> + * **BPF_F_LOCK**
> + *    Update spin_lock-ed map elements. This must be
> + *    specified if the map value contains a spinlock.
> + *
> + * @param fd BPF map file descriptor
> + * @param keys pointer to an array of *count* keys
> + * @param values pointer to an array of *count* values
> + * @param count input and output parameter; on input it's the number of elements
> + * in the map to update in batch; on output it represents the number of elements
> + * that were successfully updated. If EFAULT is returned, *count* should not
> + * be trusted to be correct.
> + * @param opts options for configuring the way the batch update works
> + * @return 0, on success; negative error code, otherwise (errno is also set to
> + * the error code)
> + */
> +LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values,
>  				    __u32 *count,
>  				    const struct bpf_map_batch_opts *opts);
>  
> +
>  LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
>  LIBBPF_API int bpf_obj_get(const char *pathname);
>  

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH bpf-next v2] libbpf: Add documentation for bpf_map batch operations
  2021-12-27 12:25 ` Hengqi Chen
@ 2022-01-03 18:08   ` Grant Seltzer Richman
  2022-01-06  2:31     ` Andrii Nakryiko
  0 siblings, 1 reply; 9+ messages in thread
From: Grant Seltzer Richman @ 2022-01-03 18:08 UTC (permalink / raw)
  To: Hengqi Chen; +Cc: bpf, Andrii Nakryiko

On Mon, Dec 27, 2021 at 7:25 AM Hengqi Chen <hengqi.chen@gmail.com> wrote:
>
>
>
> On 2021/12/26 4:37 AM, grantseltzer wrote:
> > From: Grant Seltzer <grantseltzer@gmail.com>
> >
> > This adds documentation for:
> >
> > - bpf_map_delete_batch()
> > - bpf_map_lookup_batch()
> > - bpf_map_lookup_and_delete_batch()
> > - bpf_map_update_batch()
> >
> > Signed-off-by: Grant Seltzer <grantseltzer@gmail.com>
> > ---
> >  tools/lib/bpf/bpf.c |   4 +-
> >  tools/lib/bpf/bpf.h | 112 +++++++++++++++++++++++++++++++++++++++++++-
> >  2 files changed, 112 insertions(+), 4 deletions(-)
> >
> > diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
> > index 9b64eed2b003..25f3d6f85fe5 100644
> > --- a/tools/lib/bpf/bpf.c
> > +++ b/tools/lib/bpf/bpf.c
> > @@ -691,7 +691,7 @@ static int bpf_map_batch_common(int cmd, int fd, void  *in_batch,
> >       return libbpf_err_errno(ret);
> >  }
> >
> > -int bpf_map_delete_batch(int fd, void *keys, __u32 *count,
> > +int bpf_map_delete_batch(int fd, const void *keys, __u32 *count,
>
> Maybe you should drop these const qualifier changes.

Can you help me understand the benefit of using the const qualifier in
this context? I added it at Andrii's suggestion without proper
understanding. I understand that it will properly convey that the keys
or values aren't output parameters like in other batch operation
functions, I don't think it would change where the underlying data is
stored, just the pointer variable.

Is it worth it to have seperate 'common' functions for the sake of
having a const qualifier?
>
> All batch operations use `bpf_map_batch_common`, which has the following signature:
>
> static int bpf_map_batch_common(int cmd, int fd, void  *in_batch,
>                                 void *out_batch, void *keys, void *values,
>                                 __u32 *count,
>                                 const struct bpf_map_batch_opts *opts)
>
> Adding these const qualifiers causes the following error:
>
> bpf.c:698:15: error: passing argument 5 of ‘bpf_map_batch_common’ discards
> ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
>
> >                        const struct bpf_map_batch_opts *opts)
> >  {
> >       return bpf_map_batch_common(BPF_MAP_DELETE_BATCH, fd, NULL,
> > @@ -715,7 +715,7 @@ int bpf_map_lookup_and_delete_batch(int fd, void *in_batch, void *out_batch,
> >                                   count, opts);
> >  }
> >
> > -int bpf_map_update_batch(int fd, void *keys, void *values, __u32 *count,
> > +int bpf_map_update_batch(int fd, const void *keys, const void *values, __u32 *count,
> >                        const struct bpf_map_batch_opts *opts)
> >  {
> >       return bpf_map_batch_common(BPF_MAP_UPDATE_BATCH, fd, NULL, NULL,
> > diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
> > index 00619f64a040..01011747f127 100644
> > --- a/tools/lib/bpf/bpf.h
> > +++ b/tools/lib/bpf/bpf.h
> > @@ -254,20 +254,128 @@ struct bpf_map_batch_opts {
> >  };
> >  #define bpf_map_batch_opts__last_field flags
> >
> > -LIBBPF_API int bpf_map_delete_batch(int fd, void *keys,
> > +
> > +/**
> > + * @brief **bpf_map_delete_batch()** allows for batch deletion of multiple
> > + * elements in a BPF map.
> > + *
> > + * @param fd BPF map file descriptor
> > + * @param keys pointer to an array of *count* keys
> > + * @param count number of elements in the map to sequentially delete
> > + * @param opts options for configuring the way the batch deletion works
> > + * @return 0, on success; negative error code, otherwise (errno is also set to
> > + * the error code)
> > + */
> > +LIBBPF_API int bpf_map_delete_batch(int fd, const void *keys,
> >                                   __u32 *count,
> >                                   const struct bpf_map_batch_opts *opts);
> > +
> > +/**
> > + * @brief **bpf_map_lookup_batch()** allows for batch lookup of BPF map elements.
> > + *
> > + * The parameter *in_batch* is the address of the first element in the batch to read.
> > + * *out_batch* is an output parameter that should be passed as *in_batch* to subsequent
> > + * calls to **bpf_map_lookup_batch()**. NULL can be passed for *in_batch* to indicate
> > + * that the batched lookup starts from the beginning of the map.
> > + *
> > + * The *keys* and *values* are output parameters which must point to memory large enough to
> > + * hold *count* items based on the key and value size of the map *map_fd*. The *keys*
> > + * buffer must be of *key_size* * *count*. The *values* buffer must be of
> > + * *value_size* * *count*.
> > + *
> > + * @param fd BPF map file descriptor
> > + * @param in_batch address of the first element in batch to read, can pass NULL to
> > + * indicate that the batched lookup starts from the beginning of the map.
> > + * @param out_batch output parameter that should be passed to next call as *in_batch*
> > + * @param keys pointer to an array large enough for *count* keys
> > + * @param values pointer to an array large enough for *count* values
> > + * @param count number of elements in the map to read in batch. If ENOENT is
> > + * returned, count will be set as the number of elements that were read before
> > + * running out of entries in the map
> > + * @param opts options for configuring the way the batch lookup works
> > + * @return 0, on success; negative error code, otherwise (errno is also set to
> > + * the error code)
> > + */
> >  LIBBPF_API int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch,
> >                                   void *keys, void *values, __u32 *count,
> >                                   const struct bpf_map_batch_opts *opts);
> > +
> > +/**
> > + * @brief **bpf_map_lookup_and_delete_batch()** allows for batch lookup and deletion
> > + * of BPF map elements where each element is deleted after being retrieved.
> > + *
> > + * Note that *count* is an input and output parameter, where on output it
> > + * represents how many elements were successfully deleted. Also note that if
> > + * **EFAULT** is returned up to *count* elements may have been deleted without
> > + * being returned via the *keys* and *values* output parameters. If **ENOENT**
> > + * is returned then *count* will be set to the number of elements that were read
> > + * before running out of entries in the map.
> > + *
> > + * @param fd BPF map file descriptor
> > + * @param in_batch address of the first element in batch to read, can pass NULL to
> > + * get address of the first element in *out_batch*
> > + * @param out_batch output parameter that should be passed to next call as *in_batch*
> > + * @param keys pointer to an array of *count* keys
> > + * @param values pointer to an array large enough for *count* values
> > + * @param count input and output parameter; on input it's the number of elements
> > + * in the map to read and delete in batch; on output it represents number of elements
> > + * that were successfully read and deleted
> > + * If ENOENT is returned, count will be set as the number of elements that were
> > + * read before running out of entries in the map
> > + * @param opts options for configuring the way the batch lookup and delete works
> > + * @return 0, on success; negative error code, otherwise (errno is also set to
> > + * the error code)
> > + */
> >  LIBBPF_API int bpf_map_lookup_and_delete_batch(int fd, void *in_batch,
> >                                       void *out_batch, void *keys,
> >                                       void *values, __u32 *count,
> >                                       const struct bpf_map_batch_opts *opts);
> > -LIBBPF_API int bpf_map_update_batch(int fd, void *keys, void *values,
> > +
> > +/**
> > + * @brief **bpf_map_update_batch()** updates multiple elements in a map
> > + * by specifying keys and their corresponding values.
> > + *
> > + * The *keys* and *values* parameters must point to memory large enough
> > + * to hold *count* items based on the key and value size of the map.
> > + *
> > + * The *opts* parameter can be used to control how *bpf_map_update_batch()*
> > + * should handle keys that either do or do not already exist in the map.
> > + * In particular the *flags* parameter of *bpf_map_batch_opts* can be
> > + * one of the following:
> > + *
> > + * Note that *count* is an input and output parameter, where on output it
> > + * represents how many elements were successfully updated. Also note that if
> > + * **EFAULT** then *count* should not be trusted to be correct.
> > + *
> > + * **BPF_ANY**
> > + *     Create new elements or update existing.
> > + *
> > + * **BPF_NOEXIST**
> > + *    Create new elements only if they do not exist.
> > + *
> > + * **BPF_EXIST**
> > + *    Update existing elements.
> > + *
> > + * **BPF_F_LOCK**
> > + *    Update spin_lock-ed map elements. This must be
> > + *    specified if the map value contains a spinlock.
> > + *
> > + * @param fd BPF map file descriptor
> > + * @param keys pointer to an array of *count* keys
> > + * @param values pointer to an array of *count* values
> > + * @param count input and output parameter; on input it's the number of elements
> > + * in the map to update in batch; on output it represents the number of elements
> > + * that were successfully updated. If EFAULT is returned, *count* should not
> > + * be trusted to be correct.
> > + * @param opts options for configuring the way the batch update works
> > + * @return 0, on success; negative error code, otherwise (errno is also set to
> > + * the error code)
> > + */
> > +LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values,
> >                                   __u32 *count,
> >                                   const struct bpf_map_batch_opts *opts);
> >
> > +
> >  LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
> >  LIBBPF_API int bpf_obj_get(const char *pathname);
> >

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH bpf-next v2] libbpf: Add documentation for bpf_map batch operations
  2022-01-03 18:08   ` Grant Seltzer Richman
@ 2022-01-06  2:31     ` Andrii Nakryiko
  0 siblings, 0 replies; 9+ messages in thread
From: Andrii Nakryiko @ 2022-01-06  2:31 UTC (permalink / raw)
  To: Grant Seltzer Richman; +Cc: Hengqi Chen, bpf, Andrii Nakryiko

On Mon, Jan 3, 2022 at 10:09 AM Grant Seltzer Richman
<grantseltzer@gmail.com> wrote:
>
> On Mon, Dec 27, 2021 at 7:25 AM Hengqi Chen <hengqi.chen@gmail.com> wrote:
> >
> >
> >
> > On 2021/12/26 4:37 AM, grantseltzer wrote:
> > > From: Grant Seltzer <grantseltzer@gmail.com>
> > >
> > > This adds documentation for:
> > >
> > > - bpf_map_delete_batch()
> > > - bpf_map_lookup_batch()
> > > - bpf_map_lookup_and_delete_batch()
> > > - bpf_map_update_batch()
> > >
> > > Signed-off-by: Grant Seltzer <grantseltzer@gmail.com>
> > > ---
> > >  tools/lib/bpf/bpf.c |   4 +-
> > >  tools/lib/bpf/bpf.h | 112 +++++++++++++++++++++++++++++++++++++++++++-
> > >  2 files changed, 112 insertions(+), 4 deletions(-)
> > >
> > > diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
> > > index 9b64eed2b003..25f3d6f85fe5 100644
> > > --- a/tools/lib/bpf/bpf.c
> > > +++ b/tools/lib/bpf/bpf.c
> > > @@ -691,7 +691,7 @@ static int bpf_map_batch_common(int cmd, int fd, void  *in_batch,
> > >       return libbpf_err_errno(ret);
> > >  }
> > >
> > > -int bpf_map_delete_batch(int fd, void *keys, __u32 *count,
> > > +int bpf_map_delete_batch(int fd, const void *keys, __u32 *count,
> >
> > Maybe you should drop these const qualifier changes.
>
> Can you help me understand the benefit of using the const qualifier in
> this context? I added it at Andrii's suggestion without proper
> understanding. I understand that it will properly convey that the keys
> or values aren't output parameters like in other batch operation
> functions, I don't think it would change where the underlying data is
> stored, just the pointer variable.
>
> Is it worth it to have seperate 'common' functions for the sake of
> having a const qualifier?
> >
> > All batch operations use `bpf_map_batch_common`, which has the following signature:
> >
> > static int bpf_map_batch_common(int cmd, int fd, void  *in_batch,
> >                                 void *out_batch, void *keys, void *values,
> >                                 __u32 *count,
> >                                 const struct bpf_map_batch_opts *opts)
> >
> > Adding these const qualifiers causes the following error:
> >
> > bpf.c:698:15: error: passing argument 5 of ‘bpf_map_batch_common’ discards
> > ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]

we can either forcefully cast to (void *) internally when calling
bpf_map_batch_common(), or just make bpf_map_batch_common() take const
void *. It's a bit misleading, but either way it just gets converted
to u64.

I think it's more important to have public API reflect the guarantees
correctly, so if public API specifies that something is `const void *`
that means that no one is overwriting the contents of that memory.

> >
> > >                        const struct bpf_map_batch_opts *opts)
> > >  {
> > >       return bpf_map_batch_common(BPF_MAP_DELETE_BATCH, fd, NULL,
> > > @@ -715,7 +715,7 @@ int bpf_map_lookup_and_delete_batch(int fd, void *in_batch, void *out_batch,
> > >                                   count, opts);
> > >  }
> > >

[...]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH bpf-next v2] libbpf: Add documentation for bpf_map batch operations
  2021-12-25 20:37 [PATCH bpf-next v2] libbpf: Add documentation for bpf_map batch operations grantseltzer
  2021-12-27  4:32   ` kernel test robot
  2021-12-27 12:25 ` Hengqi Chen
@ 2022-01-06  2:37 ` Andrii Nakryiko
  2022-01-06  4:54   ` Yonghong Song
  2022-01-06  7:31 ` Yonghong Song
  3 siblings, 1 reply; 9+ messages in thread
From: Andrii Nakryiko @ 2022-01-06  2:37 UTC (permalink / raw)
  To: grantseltzer; +Cc: bpf, Andrii Nakryiko, Yonghong Song

On Sat, Dec 25, 2021 at 12:37 PM grantseltzer <grantseltzer@gmail.com> wrote:
>
> From: Grant Seltzer <grantseltzer@gmail.com>
>
> This adds documentation for:
>
> - bpf_map_delete_batch()
> - bpf_map_lookup_batch()
> - bpf_map_lookup_and_delete_batch()
> - bpf_map_update_batch()
>
> Signed-off-by: Grant Seltzer <grantseltzer@gmail.com>
> ---
>  tools/lib/bpf/bpf.c |   4 +-
>  tools/lib/bpf/bpf.h | 112 +++++++++++++++++++++++++++++++++++++++++++-
>  2 files changed, 112 insertions(+), 4 deletions(-)
>
> diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
> index 9b64eed2b003..25f3d6f85fe5 100644
> --- a/tools/lib/bpf/bpf.c
> +++ b/tools/lib/bpf/bpf.c
> @@ -691,7 +691,7 @@ static int bpf_map_batch_common(int cmd, int fd, void  *in_batch,
>         return libbpf_err_errno(ret);
>  }
>
> -int bpf_map_delete_batch(int fd, void *keys, __u32 *count,
> +int bpf_map_delete_batch(int fd, const void *keys, __u32 *count,
>                          const struct bpf_map_batch_opts *opts)
>  {
>         return bpf_map_batch_common(BPF_MAP_DELETE_BATCH, fd, NULL,
> @@ -715,7 +715,7 @@ int bpf_map_lookup_and_delete_batch(int fd, void *in_batch, void *out_batch,
>                                     count, opts);
>  }
>
> -int bpf_map_update_batch(int fd, void *keys, void *values, __u32 *count,
> +int bpf_map_update_batch(int fd, const void *keys, const void *values, __u32 *count,
>                          const struct bpf_map_batch_opts *opts)
>  {
>         return bpf_map_batch_common(BPF_MAP_UPDATE_BATCH, fd, NULL, NULL,
> diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
> index 00619f64a040..01011747f127 100644
> --- a/tools/lib/bpf/bpf.h
> +++ b/tools/lib/bpf/bpf.h
> @@ -254,20 +254,128 @@ struct bpf_map_batch_opts {
>  };
>  #define bpf_map_batch_opts__last_field flags
>
> -LIBBPF_API int bpf_map_delete_batch(int fd, void *keys,
> +
> +/**
> + * @brief **bpf_map_delete_batch()** allows for batch deletion of multiple
> + * elements in a BPF map.
> + *
> + * @param fd BPF map file descriptor
> + * @param keys pointer to an array of *count* keys
> + * @param count number of elements in the map to sequentially delete

it's important to mention that count is updated after
bpf_map_delete_batch() returns with the actual number of elements that
were deleted. Please double-check the other APIs as well whether there
are important points to mention. These batch APIs are one of the
trickiest ones in bpf() syscall, let's do a thorough job documenting
them so that users don't have to read kernel code each time they want
to use it

But other than that, great job! I've CC'ed Yonghong to take another
look, as he should know the semantics of batch APIs much better.
Yonghong, please take a look when you can. Thanks!


> + * @param opts options for configuring the way the batch deletion works
> + * @return 0, on success; negative error code, otherwise (errno is also set to
> + * the error code)
> + */
> +LIBBPF_API int bpf_map_delete_batch(int fd, const void *keys,
>                                     __u32 *count,
>                                     const struct bpf_map_batch_opts *opts);

[...]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH bpf-next v2] libbpf: Add documentation for bpf_map batch operations
  2022-01-06  2:37 ` Andrii Nakryiko
@ 2022-01-06  4:54   ` Yonghong Song
  0 siblings, 0 replies; 9+ messages in thread
From: Yonghong Song @ 2022-01-06  4:54 UTC (permalink / raw)
  To: Andrii Nakryiko, grantseltzer; +Cc: bpf, Andrii Nakryiko



On 1/5/22 6:37 PM, Andrii Nakryiko wrote:
> On Sat, Dec 25, 2021 at 12:37 PM grantseltzer <grantseltzer@gmail.com> wrote:
>>
>> From: Grant Seltzer <grantseltzer@gmail.com>
>>
>> This adds documentation for:
>>
>> - bpf_map_delete_batch()
>> - bpf_map_lookup_batch()
>> - bpf_map_lookup_and_delete_batch()
>> - bpf_map_update_batch()
>>
>> Signed-off-by: Grant Seltzer <grantseltzer@gmail.com>
>> ---
>>   tools/lib/bpf/bpf.c |   4 +-
>>   tools/lib/bpf/bpf.h | 112 +++++++++++++++++++++++++++++++++++++++++++-
>>   2 files changed, 112 insertions(+), 4 deletions(-)
>>
>> diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
>> index 9b64eed2b003..25f3d6f85fe5 100644
>> --- a/tools/lib/bpf/bpf.c
>> +++ b/tools/lib/bpf/bpf.c
>> @@ -691,7 +691,7 @@ static int bpf_map_batch_common(int cmd, int fd, void  *in_batch,
>>          return libbpf_err_errno(ret);
>>   }
>>
>> -int bpf_map_delete_batch(int fd, void *keys, __u32 *count,
>> +int bpf_map_delete_batch(int fd, const void *keys, __u32 *count,
>>                           const struct bpf_map_batch_opts *opts)
>>   {
>>          return bpf_map_batch_common(BPF_MAP_DELETE_BATCH, fd, NULL,
>> @@ -715,7 +715,7 @@ int bpf_map_lookup_and_delete_batch(int fd, void *in_batch, void *out_batch,
>>                                      count, opts);
>>   }
>>
>> -int bpf_map_update_batch(int fd, void *keys, void *values, __u32 *count,
>> +int bpf_map_update_batch(int fd, const void *keys, const void *values, __u32 *count,
>>                           const struct bpf_map_batch_opts *opts)
>>   {
>>          return bpf_map_batch_common(BPF_MAP_UPDATE_BATCH, fd, NULL, NULL,
>> diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
>> index 00619f64a040..01011747f127 100644
>> --- a/tools/lib/bpf/bpf.h
>> +++ b/tools/lib/bpf/bpf.h
>> @@ -254,20 +254,128 @@ struct bpf_map_batch_opts {
>>   };
>>   #define bpf_map_batch_opts__last_field flags
>>
>> -LIBBPF_API int bpf_map_delete_batch(int fd, void *keys,
>> +
>> +/**
>> + * @brief **bpf_map_delete_batch()** allows for batch deletion of multiple
>> + * elements in a BPF map.
>> + *
>> + * @param fd BPF map file descriptor
>> + * @param keys pointer to an array of *count* keys
>> + * @param count number of elements in the map to sequentially delete
> 
> it's important to mention that count is updated after
> bpf_map_delete_batch() returns with the actual number of elements that
> were deleted. Please double-check the other APIs as well whether there
> are important points to mention. These batch APIs are one of the
> trickiest ones in bpf() syscall, let's do a thorough job documenting
> them so that users don't have to read kernel code each time they want
> to use it
> 
> But other than that, great job! I've CC'ed Yonghong to take another
> look, as he should know the semantics of batch APIs much better.
> Yonghong, please take a look when you can. Thanks!

Will take a look later today.

> 
> 
>> + * @param opts options for configuring the way the batch deletion works
>> + * @return 0, on success; negative error code, otherwise (errno is also set to
>> + * the error code)
>> + */
>> +LIBBPF_API int bpf_map_delete_batch(int fd, const void *keys,
>>                                      __u32 *count,
>>                                      const struct bpf_map_batch_opts *opts);
> 
> [...]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH bpf-next v2] libbpf: Add documentation for bpf_map batch operations
  2021-12-25 20:37 [PATCH bpf-next v2] libbpf: Add documentation for bpf_map batch operations grantseltzer
                   ` (2 preceding siblings ...)
  2022-01-06  2:37 ` Andrii Nakryiko
@ 2022-01-06  7:31 ` Yonghong Song
  3 siblings, 0 replies; 9+ messages in thread
From: Yonghong Song @ 2022-01-06  7:31 UTC (permalink / raw)
  To: grantseltzer, bpf; +Cc: andrii



On 12/25/21 12:37 PM, grantseltzer wrote:
> From: Grant Seltzer <grantseltzer@gmail.com>
> 
> This adds documentation for:
> 
> - bpf_map_delete_batch()
> - bpf_map_lookup_batch()
> - bpf_map_lookup_and_delete_batch()
> - bpf_map_update_batch()
> 
> Signed-off-by: Grant Seltzer <grantseltzer@gmail.com>
> ---
>   tools/lib/bpf/bpf.c |   4 +-
>   tools/lib/bpf/bpf.h | 112 +++++++++++++++++++++++++++++++++++++++++++-
>   2 files changed, 112 insertions(+), 4 deletions(-)
> 
> diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
> index 9b64eed2b003..25f3d6f85fe5 100644
> --- a/tools/lib/bpf/bpf.c
> +++ b/tools/lib/bpf/bpf.c
> @@ -691,7 +691,7 @@ static int bpf_map_batch_common(int cmd, int fd, void  *in_batch,
>   	return libbpf_err_errno(ret);
>   }
>   
> -int bpf_map_delete_batch(int fd, void *keys, __u32 *count,
> +int bpf_map_delete_batch(int fd, const void *keys, __u32 *count,
>   			 const struct bpf_map_batch_opts *opts)
>   {
>   	return bpf_map_batch_common(BPF_MAP_DELETE_BATCH, fd, NULL,
> @@ -715,7 +715,7 @@ int bpf_map_lookup_and_delete_batch(int fd, void *in_batch, void *out_batch,
>   				    count, opts);
>   }
>   
> -int bpf_map_update_batch(int fd, void *keys, void *values, __u32 *count,
> +int bpf_map_update_batch(int fd, const void *keys, const void *values, __u32 *count,
>   			 const struct bpf_map_batch_opts *opts)
>   {
>   	return bpf_map_batch_common(BPF_MAP_UPDATE_BATCH, fd, NULL, NULL,
> diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
> index 00619f64a040..01011747f127 100644
> --- a/tools/lib/bpf/bpf.h
> +++ b/tools/lib/bpf/bpf.h
> @@ -254,20 +254,128 @@ struct bpf_map_batch_opts {
>   };
>   #define bpf_map_batch_opts__last_field flags
>   
> -LIBBPF_API int bpf_map_delete_batch(int fd, void *keys,
> +
> +/**
> + * @brief **bpf_map_delete_batch()** allows for batch deletion of multiple
> + * elements in a BPF map.
> + *
> + * @param fd BPF map file descriptor
> + * @param keys pointer to an array of *count* keys
> + * @param count number of elements in the map to sequentially delete

Here, "count" is an input/output parameter. We can describe its output
semantics like below.

When on output, if an error returns, **count** represents the number of
deleted elements if the output **count** value is not equal to the
input **count** value and if the error code is not EFAULT.

> + * @param opts options for configuring the way the batch deletion works
> + * @return 0, on success; negative error code, otherwise (errno is also set to
> + * the error code)
> + */
> +LIBBPF_API int bpf_map_delete_batch(int fd, const void *keys,
>   				    __u32 *count,
>   				    const struct bpf_map_batch_opts *opts);
> +
> +/**
> + * @brief **bpf_map_lookup_batch()** allows for batch lookup of BPF map elements.
> + *
> + * The parameter *in_batch* is the address of the first element in the batch to read.
> + * *out_batch* is an output parameter that should be passed as *in_batch* to subsequent
> + * calls to **bpf_map_lookup_batch()**. NULL can be passed for *in_batch* to indicate
> + * that the batched lookup starts from the beginning of the map.
> + *
> + * The *keys* and *values* are output parameters which must point to memory large enough to
> + * hold *count* items based on the key and value size of the map *map_fd*. The *keys*
> + * buffer must be of *key_size* * *count*. The *values* buffer must be of
> + * *value_size* * *count*.
> + *
> + * @param fd BPF map file descriptor
> + * @param in_batch address of the first element in batch to read, can pass NULL to
> + * indicate that the batched lookup starts from the beginning of the map.
> + * @param out_batch output parameter that should be passed to next call as *in_batch*
> + * @param keys pointer to an array large enough for *count* keys
> + * @param values pointer to an array large enough for *count* values
> + * @param count number of elements in the map to read in batch. If ENOENT is
> + * returned, count will be set as the number of elements that were read before
> + * running out of entries in the map
> + * @param opts options for configuring the way the batch lookup works
> + * @return 0, on success; negative error code, otherwise (errno is also set to
> + * the error code)
> + */
>   LIBBPF_API int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch,
>   				    void *keys, void *values, __u32 *count,
>   				    const struct bpf_map_batch_opts *opts);
> +
> +/**
> + * @brief **bpf_map_lookup_and_delete_batch()** allows for batch lookup and deletion
> + * of BPF map elements where each element is deleted after being retrieved.
> + *
> + * Note that *count* is an input and output parameter, where on output it
> + * represents how many elements were successfully deleted. Also note that if
> + * **EFAULT** is returned up to *count* elements may have been deleted without

if an non-**EFAULT** error code is returned and if the output **count** 
value is not equal to the input **count** value, up to **count** 
elements may have been deleted.

This applies to the above bpf_map_lookup_batch as well.

> + * being returned via the *keys* and *values* output parameters. If **ENOENT**
> + * is returned then *count* will be set to the number of elements that were read
> + * before running out of entries in the map.
> + *
> + * @param fd BPF map file descriptor
> + * @param in_batch address of the first element in batch to read, can pass NULL to
> + * get address of the first element in *out_batch*
> + * @param out_batch output parameter that should be passed to next call as *in_batch*
> + * @param keys pointer to an array of *count* keys
> + * @param values pointer to an array large enough for *count* values
> + * @param count input and output parameter; on input it's the number of elements
> + * in the map to read and delete in batch; on output it represents number of elements
> + * that were successfully read and deleted
> + * If ENOENT is returned, count will be set as the number of elements that were
> + * read before running out of entries in the map
> + * @param opts options for configuring the way the batch lookup and delete works
> + * @return 0, on success; negative error code, otherwise (errno is also set to
> + * the error code)
> + */
>   LIBBPF_API int bpf_map_lookup_and_delete_batch(int fd, void *in_batch,
>   					void *out_batch, void *keys,
>   					void *values, __u32 *count,
>   					const struct bpf_map_batch_opts *opts);
> -LIBBPF_API int bpf_map_update_batch(int fd, void *keys, void *values,
> +
> +/**
> + * @brief **bpf_map_update_batch()** updates multiple elements in a map
> + * by specifying keys and their corresponding values.
> + *
> + * The *keys* and *values* parameters must point to memory large enough
> + * to hold *count* items based on the key and value size of the map.
> + *
> + * The *opts* parameter can be used to control how *bpf_map_update_batch()*
> + * should handle keys that either do or do not already exist in the map.
> + * In particular the *flags* parameter of *bpf_map_batch_opts* can be
> + * one of the following:
> + *
> + * Note that *count* is an input and output parameter, where on output it
> + * represents how many elements were successfully updated. Also note that if
> + * **EFAULT** then *count* should not be trusted to be correct.

The semantics of **count** here is similar to bpf_map_delete_batch() above.

When on output, if an error returns, **count** represents the number of
deleted elements if the output **count** value is not equal to the
input **count** value and if the error code is not EFAULT.

> + *
> + * **BPF_ANY**
> + *     Create new elements or update existing.
> + *
> + * **BPF_NOEXIST**
> + *    Create new elements only if they do not exist.
> + *
> + * **BPF_EXIST**
> + *    Update existing elements.
> + *
> + * **BPF_F_LOCK**
> + *    Update spin_lock-ed map elements. This must be
> + *    specified if the map value contains a spinlock.
> + *
> + * @param fd BPF map file descriptor
> + * @param keys pointer to an array of *count* keys
> + * @param values pointer to an array of *count* values
> + * @param count input and output parameter; on input it's the number of elements
> + * in the map to update in batch; on output it represents the number of elements
> + * that were successfully updated. If EFAULT is returned, *count* should not
> + * be trusted to be correct.
> + * @param opts options for configuring the way the batch update works
> + * @return 0, on success; negative error code, otherwise (errno is also set to
> + * the error code)
> + */
> +LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values,
>   				    __u32 *count,
>   				    const struct bpf_map_batch_opts *opts);
>   
> +
>   LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
>   LIBBPF_API int bpf_obj_get(const char *pathname);
>   

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-01-06  7:31 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-25 20:37 [PATCH bpf-next v2] libbpf: Add documentation for bpf_map batch operations grantseltzer
2021-12-27  4:32 ` kernel test robot
2021-12-27  4:32   ` kernel test robot
2021-12-27 12:25 ` Hengqi Chen
2022-01-03 18:08   ` Grant Seltzer Richman
2022-01-06  2:31     ` Andrii Nakryiko
2022-01-06  2:37 ` Andrii Nakryiko
2022-01-06  4:54   ` Yonghong Song
2022-01-06  7:31 ` Yonghong Song

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.