Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

libbpf: Add documentation for bpf_map batch operations

This adds documention for:

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

This also updates the public API for the `keys` parameter
of `bpf_map_delete_batch()`, and both the
`keys` and `values` parameters of `bpf_map_update_batch()`
to be constants.

Signed-off-by: Grant Seltzer <grantseltzer@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20220106201304.112675-1-grantseltzer@gmail.com

authored by

Grant Seltzer and committed by
Andrii Nakryiko
e59618f0 70bc7933

+117 -6
+4 -4
tools/lib/bpf/bpf.c
··· 691 691 return libbpf_err_errno(ret); 692 692 } 693 693 694 - int bpf_map_delete_batch(int fd, void *keys, __u32 *count, 694 + int bpf_map_delete_batch(int fd, const void *keys, __u32 *count, 695 695 const struct bpf_map_batch_opts *opts) 696 696 { 697 697 return bpf_map_batch_common(BPF_MAP_DELETE_BATCH, fd, NULL, 698 - NULL, keys, NULL, count, opts); 698 + NULL, (void *)keys, NULL, count, opts); 699 699 } 700 700 701 701 int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch, void *keys, ··· 715 715 count, opts); 716 716 } 717 717 718 - int bpf_map_update_batch(int fd, void *keys, void *values, __u32 *count, 718 + int bpf_map_update_batch(int fd, const void *keys, const void *values, __u32 *count, 719 719 const struct bpf_map_batch_opts *opts) 720 720 { 721 721 return bpf_map_batch_common(BPF_MAP_UPDATE_BATCH, fd, NULL, NULL, 722 - keys, values, count, opts); 722 + (void *)keys, (void *)values, count, opts); 723 723 } 724 724 725 725 int bpf_obj_pin(int fd, const char *pathname)
+113 -2
tools/lib/bpf/bpf.h
··· 254 254 }; 255 255 #define bpf_map_batch_opts__last_field flags 256 256 257 - LIBBPF_API int bpf_map_delete_batch(int fd, void *keys, 257 + 258 + /** 259 + * @brief **bpf_map_delete_batch()** allows for batch deletion of multiple 260 + * elements in a BPF map. 261 + * 262 + * @param fd BPF map file descriptor 263 + * @param keys pointer to an array of *count* keys 264 + * @param count input and output parameter; on input **count** represents the 265 + * number of elements in the map to delete in batch; 266 + * on output if a non-EFAULT error is returned, **count** represents the number of deleted 267 + * elements if the output **count** value is not equal to the input **count** value 268 + * If EFAULT is returned, **count** should not be trusted to be correct. 269 + * @param opts options for configuring the way the batch deletion works 270 + * @return 0, on success; negative error code, otherwise (errno is also set to 271 + * the error code) 272 + */ 273 + LIBBPF_API int bpf_map_delete_batch(int fd, const void *keys, 258 274 __u32 *count, 259 275 const struct bpf_map_batch_opts *opts); 276 + 277 + /** 278 + * @brief **bpf_map_lookup_batch()** allows for batch lookup of BPF map elements. 279 + * 280 + * The parameter *in_batch* is the address of the first element in the batch to read. 281 + * *out_batch* is an output parameter that should be passed as *in_batch* to subsequent 282 + * calls to **bpf_map_lookup_batch()**. NULL can be passed for *in_batch* to indicate 283 + * that the batched lookup starts from the beginning of the map. 284 + * 285 + * The *keys* and *values* are output parameters which must point to memory large enough to 286 + * hold *count* items based on the key and value size of the map *map_fd*. The *keys* 287 + * buffer must be of *key_size* * *count*. The *values* buffer must be of 288 + * *value_size* * *count*. 289 + * 290 + * @param fd BPF map file descriptor 291 + * @param in_batch address of the first element in batch to read, can pass NULL to 292 + * indicate that the batched lookup starts from the beginning of the map. 293 + * @param out_batch output parameter that should be passed to next call as *in_batch* 294 + * @param keys pointer to an array large enough for *count* keys 295 + * @param values pointer to an array large enough for *count* values 296 + * @param count input and output parameter; on input it's the number of elements 297 + * in the map to read in batch; on output it's the number of elements that were 298 + * successfully read. 299 + * If a non-EFAULT error is returned, count will be set as the number of elements 300 + * that were read before the error occurred. 301 + * If EFAULT is returned, **count** should not be trusted to be correct. 302 + * @param opts options for configuring the way the batch lookup works 303 + * @return 0, on success; negative error code, otherwise (errno is also set to 304 + * the error code) 305 + */ 260 306 LIBBPF_API int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch, 261 307 void *keys, void *values, __u32 *count, 262 308 const struct bpf_map_batch_opts *opts); 309 + 310 + /** 311 + * @brief **bpf_map_lookup_and_delete_batch()** allows for batch lookup and deletion 312 + * of BPF map elements where each element is deleted after being retrieved. 313 + * 314 + * @param fd BPF map file descriptor 315 + * @param in_batch address of the first element in batch to read, can pass NULL to 316 + * get address of the first element in *out_batch* 317 + * @param out_batch output parameter that should be passed to next call as *in_batch* 318 + * @param keys pointer to an array of *count* keys 319 + * @param values pointer to an array large enough for *count* values 320 + * @param count input and output parameter; on input it's the number of elements 321 + * in the map to read and delete in batch; on output it represents the number of 322 + * elements that were successfully read and deleted 323 + * If a non-**EFAULT** error code is returned and if the output **count** value 324 + * is not equal to the input **count** value, up to **count** elements may 325 + * have been deleted. 326 + * if **EFAULT** is returned up to *count* elements may have been deleted without 327 + * being returned via the *keys* and *values* output parameters. 328 + * @param opts options for configuring the way the batch lookup and delete works 329 + * @return 0, on success; negative error code, otherwise (errno is also set to 330 + * the error code) 331 + */ 263 332 LIBBPF_API int bpf_map_lookup_and_delete_batch(int fd, void *in_batch, 264 333 void *out_batch, void *keys, 265 334 void *values, __u32 *count, 266 335 const struct bpf_map_batch_opts *opts); 267 - LIBBPF_API int bpf_map_update_batch(int fd, void *keys, void *values, 336 + 337 + /** 338 + * @brief **bpf_map_update_batch()** updates multiple elements in a map 339 + * by specifying keys and their corresponding values. 340 + * 341 + * The *keys* and *values* parameters must point to memory large enough 342 + * to hold *count* items based on the key and value size of the map. 343 + * 344 + * The *opts* parameter can be used to control how *bpf_map_update_batch()* 345 + * should handle keys that either do or do not already exist in the map. 346 + * In particular the *flags* parameter of *bpf_map_batch_opts* can be 347 + * one of the following: 348 + * 349 + * Note that *count* is an input and output parameter, where on output it 350 + * represents how many elements were successfully updated. Also note that if 351 + * **EFAULT** then *count* should not be trusted to be correct. 352 + * 353 + * **BPF_ANY** 354 + * Create new elements or update existing. 355 + * 356 + * **BPF_NOEXIST** 357 + * Create new elements only if they do not exist. 358 + * 359 + * **BPF_EXIST** 360 + * Update existing elements. 361 + * 362 + * **BPF_F_LOCK** 363 + * Update spin_lock-ed map elements. This must be 364 + * specified if the map value contains a spinlock. 365 + * 366 + * @param fd BPF map file descriptor 367 + * @param keys pointer to an array of *count* keys 368 + * @param values pointer to an array of *count* values 369 + * @param count input and output parameter; on input it's the number of elements 370 + * in the map to update in batch; on output if a non-EFAULT error is returned, 371 + * **count** represents the number of updated elements if the output **count** 372 + * value is not equal to the input **count** value. 373 + * If EFAULT is returned, **count** should not be trusted to be correct. 374 + * @param opts options for configuring the way the batch update works 375 + * @return 0, on success; negative error code, otherwise (errno is also set to 376 + * the error code) 377 + */ 378 + LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values, 268 379 __u32 *count, 269 380 const struct bpf_map_batch_opts *opts); 270 381