From patchwork Mon Jan 6 14:53:25 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Charalampos Stylianopoulos X-Patchwork-Id: 13927567 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2AD1A8633A for ; Mon, 6 Jan 2025 14:53:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736175240; cv=none; b=p5c96+Uw+nznpqh/VDt7BmdWfksG7FwGJqPNoWQ/2gmt0Jo171/7O+WBU0BCEBMufFsdjmU3CamqsyM5xL35RhGR0xdagXkbSIdNVaOpyhzdbgde4KnD+E/vLd202JNqUhyfLfOzzYd0G4Tst9j7d7w30TccF+g6qMson2L0zjs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736175240; c=relaxed/simple; bh=PIZOsUgeNn4GFhc+HRjWRD/qEOLIPpUzY6a9KyU7klA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bjCDqlD1GCgo3BXgMHeyOKK70c2sgOORynnWlHRbMk0cJBmEEVcdg8bL021DCcc/GcofNtoKqwwHis5C1+S5yDBfCHcL21UhMC9BC5qI8zUO0wC9+0hyuE366+et5eXg2VPrhVi75sBSV9LbHtrAgFYzvxKB3taBa8EBTnLmss8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=naQMpyp8; arc=none smtp.client-ip=209.85.128.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="naQMpyp8" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-436341f575fso149748475e9.1 for ; Mon, 06 Jan 2025 06:53:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736175237; x=1736780037; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hnTmnFNraHZZOJddJKqa2n5UrakfYRlTB6KX2RcLzQ8=; b=naQMpyp8B0SyL40wgRGhz4IhqkbMSBF+IH84lBQsbTvm8/vlyrFRlYSnxklMV3R7/1 8Hzz0NhezwLtjtk1Nbh67OQiH8FIVQy9gGTsPo6rIFz8+S5odNWGQwecRvkSFb4Qya7j k0uTLB9gl+LdylKFEXshVcNIZqaG61ONpDX51Lt83zoTzaZBqZSwMLVy/tvpkpw/qW46 EyA6OSfkLcQ1CZHQZlcioWqLSsnyCkWPV3zyzt/nEn3YNlFcSRjDt2j9yJZlp5E5mFHQ nO4mDXudzPUj3y+jgZDvxmcvRPX3zrgei7rQSgFzV8uFiBvw8OlHTBaEVsXabJtb4zIR TnFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736175237; x=1736780037; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hnTmnFNraHZZOJddJKqa2n5UrakfYRlTB6KX2RcLzQ8=; b=BKtOgemGQhvpIaNW9eGZu/7pAkRjK/Dpe4+2b7dHpWMB+QpXH8f9S6yCz+s0Zq1Ubl p7nqrrUQmZytlAc0TOB5pdRJV7ldIVQxUddA5zd/LvWEv/3Iu1UFYCHYIPpNvu+g5RoL qbF5fDQNF3ae6AZqpN4sCVEE1ckiH+pdDw6VLIAA3nwGL6DGRmSjouC1YT8nDt9DSSt7 IzTwR/kE0WhX3PF8SJWuVGArOUQTJknNA3D1ZKDf/mDyvJ7IljiHGr5dpYyY9LfXQASX stPuruP6TW29PW+loSwKH/VK712w1y2Hb+JL8Sv7aXLYzoDXiUbWz62htQy/SijdBFzj lVPw== X-Gm-Message-State: AOJu0Yxjqf1h00e5Yd5tzh6g+lgcQKA+T5DEIS5sV6D4ISMK/DZ+xLRn M7VtsYjxUy1Es4samSRZDI8fF3r+GP0FkdcF3nelnDZLRvC7vY7ZWAFwP8VsJIgLRlf+ X-Gm-Gg: ASbGncunqve/xGJYpnSjGO+hXCVIh465U+CGu1RdxjjTnn+eJNxRMk23v+CergZTzqZ prqvnrwor1+qyKIYoWqsz5xYTUI5nel+HFTI7iZRnNFaph4CcqLs9C3XVz874RNKV0NwgrVPKGR WczswA8Xw+g9wF+82RtWj+MMBPrTzCr4OTCENL/b+WVACyQuC1ZxJiDstsxwRwHdxpBtxK1drme Rm2c5NOwhnODD6VxynSSEnK6EzFrWO8fVc9J4+RkFLcLqQKAmtwUDXTI9EdbqFamKuSXd1g57Y= X-Google-Smtp-Source: AGHT+IEWWW9/CZ9N03T/6d0NkYMu8csqDWf4yolHS9uL30j+K1oeRzdMYDRWNLzTocd7U54KW68ftQ== X-Received: by 2002:a05:600c:3505:b0:430:57e8:3c7e with SMTP id 5b1f17b1804b1-43668b5f39emr399209695e9.28.1736175236825; Mon, 06 Jan 2025 06:53:56 -0800 (PST) Received: from babis.. ([2a02:3033:700:3ba2:3837:7343:334:7680]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38a1c832e74sm47389982f8f.30.2025.01.06.06.53.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Jan 2025 06:53:56 -0800 (PST) From: Charalampos Stylianopoulos To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Nick Zavaritsky , Charalampos Stylianopoulos Subject: [PATCH bpf-next 1/4] bpf: Add map_num_entries map op Date: Mon, 6 Jan 2025 15:53:25 +0100 Message-ID: <20250106145328.399610-2-charalampos.stylianopoulos@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250106145328.399610-1-charalampos.stylianopoulos@gmail.com> References: <20250106145328.399610-1-charalampos.stylianopoulos@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net This patch extends map operations with map_num_entries that returns the number of entries currently present in the map. Provides implementation of the ops for maps that track the number of elements added in them. Co-developed-by: Nick Zavaritsky Signed-off-by: Nick Zavaritsky Signed-off-by: Charalampos Stylianopoulos --- include/linux/bpf.h | 3 +++ include/linux/bpf_local_storage.h | 1 + kernel/bpf/devmap.c | 14 ++++++++++++++ kernel/bpf/hashtab.c | 10 ++++++++++ kernel/bpf/lpm_trie.c | 8 ++++++++ kernel/bpf/queue_stack_maps.c | 11 ++++++++++- 6 files changed, 46 insertions(+), 1 deletion(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index feda0ce90f5a..217260a8f5f4 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -175,6 +175,7 @@ struct bpf_map_ops { void *callback_ctx, u64 flags); u64 (*map_mem_usage)(const struct bpf_map *map); + s64 (*map_num_entries)(const struct bpf_map *map); /* BTF id of struct allocated by map_alloc */ int *map_btf_id; @@ -2402,6 +2403,8 @@ static inline void bpf_map_dec_elem_count(struct bpf_map *map) this_cpu_dec(*map->elem_count); } +s64 bpf_map_sum_elem_count(const struct bpf_map *map); + extern int sysctl_unprivileged_bpf_disabled; bool bpf_token_capable(const struct bpf_token *token, int cap); diff --git a/include/linux/bpf_local_storage.h b/include/linux/bpf_local_storage.h index ab7244d8108f..3a9e69e44c1d 100644 --- a/include/linux/bpf_local_storage.h +++ b/include/linux/bpf_local_storage.h @@ -204,5 +204,6 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap, void *value, u64 map_flags, bool swap_uptrs, gfp_t gfp_flags); u64 bpf_local_storage_map_mem_usage(const struct bpf_map *map); +s64 bpf_local_storage_map_num_entries(const struct bpf_map *map); #endif /* _BPF_LOCAL_STORAGE_H */ diff --git a/kernel/bpf/devmap.c b/kernel/bpf/devmap.c index 3aa002a47a96..f43a58389f8f 100644 --- a/kernel/bpf/devmap.c +++ b/kernel/bpf/devmap.c @@ -1041,6 +1041,18 @@ static u64 dev_map_mem_usage(const struct bpf_map *map) return usage; } +static s64 dev_map_num_entries(const struct bpf_map *map) +{ + struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map); + s64 entries = 0; + + if (map->map_type == BPF_MAP_TYPE_DEVMAP_HASH) + entries = atomic_read((atomic_t *)&dtab->items); + else + entries = -EOPNOTSUPP; + return entries; +} + BTF_ID_LIST_SINGLE(dev_map_btf_ids, struct, bpf_dtab) const struct bpf_map_ops dev_map_ops = { .map_meta_equal = bpf_map_meta_equal, @@ -1053,6 +1065,7 @@ const struct bpf_map_ops dev_map_ops = { .map_delete_elem = dev_map_delete_elem, .map_check_btf = map_check_no_btf, .map_mem_usage = dev_map_mem_usage, + .map_num_entries = dev_map_num_entries, .map_btf_id = &dev_map_btf_ids[0], .map_redirect = dev_map_redirect, }; @@ -1068,6 +1081,7 @@ const struct bpf_map_ops dev_map_hash_ops = { .map_delete_elem = dev_map_hash_delete_elem, .map_check_btf = map_check_no_btf, .map_mem_usage = dev_map_mem_usage, + .map_num_entries = dev_map_num_entries, .map_btf_id = &dev_map_btf_ids[0], .map_redirect = dev_hash_map_redirect, }; diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index 3ec941a0ea41..769a4c33c81f 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -2287,6 +2287,11 @@ static u64 htab_map_mem_usage(const struct bpf_map *map) return usage; } +static s64 htab_map_num_entries(const struct bpf_map *map) +{ + return bpf_map_sum_elem_count(map); +} + BTF_ID_LIST_SINGLE(htab_map_btf_ids, struct, bpf_htab) const struct bpf_map_ops htab_map_ops = { .map_meta_equal = bpf_map_meta_equal, @@ -2304,6 +2309,7 @@ const struct bpf_map_ops htab_map_ops = { .map_set_for_each_callback_args = map_set_for_each_callback_args, .map_for_each_callback = bpf_for_each_hash_elem, .map_mem_usage = htab_map_mem_usage, + .map_num_entries = htab_map_num_entries, BATCH_OPS(htab), .map_btf_id = &htab_map_btf_ids[0], .iter_seq_info = &iter_seq_info, @@ -2326,6 +2332,7 @@ const struct bpf_map_ops htab_lru_map_ops = { .map_set_for_each_callback_args = map_set_for_each_callback_args, .map_for_each_callback = bpf_for_each_hash_elem, .map_mem_usage = htab_map_mem_usage, + .map_num_entries = htab_map_num_entries, BATCH_OPS(htab_lru), .map_btf_id = &htab_map_btf_ids[0], .iter_seq_info = &iter_seq_info, @@ -2499,6 +2506,7 @@ const struct bpf_map_ops htab_percpu_map_ops = { .map_set_for_each_callback_args = map_set_for_each_callback_args, .map_for_each_callback = bpf_for_each_hash_elem, .map_mem_usage = htab_map_mem_usage, + .map_num_entries = htab_map_num_entries, BATCH_OPS(htab_percpu), .map_btf_id = &htab_map_btf_ids[0], .iter_seq_info = &iter_seq_info, @@ -2519,6 +2527,7 @@ const struct bpf_map_ops htab_lru_percpu_map_ops = { .map_set_for_each_callback_args = map_set_for_each_callback_args, .map_for_each_callback = bpf_for_each_hash_elem, .map_mem_usage = htab_map_mem_usage, + .map_num_entries = htab_map_num_entries, BATCH_OPS(htab_lru_percpu), .map_btf_id = &htab_map_btf_ids[0], .iter_seq_info = &iter_seq_info, @@ -2663,6 +2672,7 @@ const struct bpf_map_ops htab_of_maps_map_ops = { .map_gen_lookup = htab_of_map_gen_lookup, .map_check_btf = map_check_no_btf, .map_mem_usage = htab_map_mem_usage, + .map_num_entries = htab_map_num_entries, BATCH_OPS(htab), .map_btf_id = &htab_map_btf_ids[0], }; diff --git a/kernel/bpf/lpm_trie.c b/kernel/bpf/lpm_trie.c index f8bc1e096182..5297eb2e8e97 100644 --- a/kernel/bpf/lpm_trie.c +++ b/kernel/bpf/lpm_trie.c @@ -780,6 +780,13 @@ static u64 trie_mem_usage(const struct bpf_map *map) return elem_size * READ_ONCE(trie->n_entries); } +static s64 trie_num_entries(const struct bpf_map *map) +{ + struct lpm_trie *trie = container_of(map, struct lpm_trie, map); + + return READ_ONCE(trie->n_entries); +} + BTF_ID_LIST_SINGLE(trie_map_btf_ids, struct, lpm_trie) const struct bpf_map_ops trie_map_ops = { .map_meta_equal = bpf_map_meta_equal, @@ -794,5 +801,6 @@ const struct bpf_map_ops trie_map_ops = { .map_delete_batch = generic_map_delete_batch, .map_check_btf = trie_check_btf, .map_mem_usage = trie_mem_usage, + .map_num_entries = trie_num_entries, .map_btf_id = &trie_map_btf_ids[0], }; diff --git a/kernel/bpf/queue_stack_maps.c b/kernel/bpf/queue_stack_maps.c index d869f51ea93a..f66aa31248e7 100644 --- a/kernel/bpf/queue_stack_maps.c +++ b/kernel/bpf/queue_stack_maps.c @@ -22,7 +22,7 @@ struct bpf_queue_stack { char elements[] __aligned(8); }; -static struct bpf_queue_stack *bpf_queue_stack(struct bpf_map *map) +static struct bpf_queue_stack *bpf_queue_stack(const struct bpf_map *map) { return container_of(map, struct bpf_queue_stack, map); } @@ -265,6 +265,13 @@ static u64 queue_stack_map_mem_usage(const struct bpf_map *map) return usage; } +static s64 queue_stack_map_num_entries(const struct bpf_map *map) +{ + struct bpf_queue_stack *qs = bpf_queue_stack(map); + s64 entries = qs->head - qs->tail; + return entries; +} + BTF_ID_LIST_SINGLE(queue_map_btf_ids, struct, bpf_queue_stack) const struct bpf_map_ops queue_map_ops = { .map_meta_equal = bpf_map_meta_equal, @@ -279,6 +286,7 @@ const struct bpf_map_ops queue_map_ops = { .map_peek_elem = queue_map_peek_elem, .map_get_next_key = queue_stack_map_get_next_key, .map_mem_usage = queue_stack_map_mem_usage, + .map_num_entries = queue_stack_map_num_entries, .map_btf_id = &queue_map_btf_ids[0], }; @@ -295,5 +303,6 @@ const struct bpf_map_ops stack_map_ops = { .map_peek_elem = stack_map_peek_elem, .map_get_next_key = queue_stack_map_get_next_key, .map_mem_usage = queue_stack_map_mem_usage, + .map_num_entries = queue_stack_map_num_entries, .map_btf_id = &queue_map_btf_ids[0], }; From patchwork Mon Jan 6 14:53:26 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Charalampos Stylianopoulos X-Patchwork-Id: 13927568 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4C71F433CB for ; Mon, 6 Jan 2025 14:54:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736175248; cv=none; b=M3t4NFe3Eus/wW+Zt19jHF40Tw8oK7CsBhM9Kyn2eDNR+Uvf415xFd0UanbcqL2/trvYgLTAYvhQo/IBv/7cti3ABKtZotHbGtXmalgeMlcciSO6h0+EpWrGRL8HFhkOVh+0jDH8J46ssJw4xTKhJS3emolgnUIkafJicmG55yk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736175248; c=relaxed/simple; bh=EwDzzphZzFhtltBbEjKn6ELpV9oQi7pVRaV5OiC8PVA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pgzHqwLujLq46X9o07mM6DIAcnMsKf0rBu4wLvFAWkMfzexnewUXPfvk6JNK09an2KnqOit2NBsXT3YaDu91zz9MqcikOcmRbqr10UrwiS/aCBHOig96rbWGyUCFGQV/mak62IMH8hHzj6R0xmUVN4lSMJ04FdOxvrmKnjU7UwQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=FmJh4Uvc; arc=none smtp.client-ip=209.85.221.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="FmJh4Uvc" Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-385d7f19f20so6348962f8f.1 for ; Mon, 06 Jan 2025 06:54:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736175244; x=1736780044; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=RfxLq/k3pa/bVimMye2S+vaiQlpIj+CsmbaBLY7XhHw=; b=FmJh4UvcvTxVtXJNFl0BDq59717emnd4wEckGT02prwMqzSGr/EWsD9bk7HRhqawFb eEDWC9HxGzCfqBgfX7m19iDHnrgzRh9YD1uJKm78EkXz+lRa/wUbtlxArV0tQDf2KepW i4RmKgiDwVjt4ADd6jymd9R8iNu7XiAO3xStepXvEt2HO+yQhT3xLcLnBG0G0WCH9QfN 6YkSo2L6qE4fXBQlI/wNV8IBC5aeHrK3PK8cE0TqGfDDz62BBe08JHi6baBZrB9bE9Bn EWHMJOBJwgPKdZ0ourg0mu9B0L6tZ/QpkDv69qXBaN9sYTpIM0/4vgog/t4RmB92CvoH ERLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736175244; x=1736780044; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RfxLq/k3pa/bVimMye2S+vaiQlpIj+CsmbaBLY7XhHw=; b=SpXQrGeqoyz507gdcy3XXmDLG9m+G0H1ZSHTA6BHmu3YMvSfNd1shuKRzpoLCaAo9L 9PBAdK0+a06K5MdceVpeQwOk9IZhhwpXd1ccVciYFTUvJIBbXQFjBcBP0TQL6GnxaEL9 S3dzymCr3qLm6a8L+5aGUe+6kWTvz6OKuQwtdh975NW/VL8HAx6waAh9avJTOlUYd/Wb GF3/vaC/opNVCkLvHA1Uy1Rfzyt+LALEO6+FCMNsIPCHqYFz9m1nNT6QY530lb9HdRb8 lXQRPepEUlJGJBOoxKKQnNo3PIOUhYUp55QdG42NAQP7rs3dyDIDX0/JJB3JD27nXJUR qdxw== X-Gm-Message-State: AOJu0Yxwe2PueVCsX/pnTM+YIVeS04IO48GBJPcd0Pu2H4OK2/xHju6/ rQCLSKzJhXoltxeYNRtq995qWV+YN5FqDeRQljNwLDkj7ppbIozeovIS5pEJUEE6VA== X-Gm-Gg: ASbGnctNtCZBbKDU7/RDm3BgLjKN1n4p7gKjNeY6xqDky0a4FR7iv+7gtW1fd9QnkBK huuYjBTlGCXPT3vfn8VYG06KgLP2yKkf5YcjScplFTdPLiI7V0oRbO0uWRcwgbaACfpbIymKZnl ri1eo63L7s91xebl6xI0yQNJ209uHpdSAjCu4aMR4pFXHkZabbHj727YQBFhm5oK2Fm1LUgtgdS VSx2ub57nm8IEsf7ucxN/4bLpx0yGdZirMzcikYv+GnnxECJa8NkVUUEPTQjnhAhAHrEOe0a0k= X-Google-Smtp-Source: AGHT+IGpDIIY/UlEA1BTnSqFKkJY+bKo67rTJNDCHK8rsE19keHWhr8o6QC/8+kJD/KFTfSzTiyxNQ== X-Received: by 2002:a05:6000:1fa9:b0:385:f38e:c0c3 with SMTP id ffacd0b85a97d-38a221f1391mr43705446f8f.6.1736175242481; Mon, 06 Jan 2025 06:54:02 -0800 (PST) Received: from babis.. ([2a02:3033:700:3ba2:3837:7343:334:7680]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38a1c832e74sm47389982f8f.30.2025.01.06.06.54.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Jan 2025 06:54:02 -0800 (PST) From: Charalampos Stylianopoulos To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Nick Zavaritsky , Charalampos Stylianopoulos Subject: [PATCH bpf-next 2/4] bpf: Add bpf command to get number of map entries Date: Mon, 6 Jan 2025 15:53:26 +0100 Message-ID: <20250106145328.399610-3-charalampos.stylianopoulos@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250106145328.399610-1-charalampos.stylianopoulos@gmail.com> References: <20250106145328.399610-1-charalampos.stylianopoulos@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Introduces a new command to the main bpf syscall to return the number of entries in a map. Returns EOPNOTSUPP if the relevant map operation is not implemented. Co-developed-by: Nick Zavaritsky Signed-off-by: Nick Zavaritsky Signed-off-by: Charalampos Stylianopoulos --- include/uapi/linux/bpf.h | 17 +++++++++++++++++ kernel/bpf/syscall.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 2acf9b336371..f5c7adea1387 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -903,6 +903,17 @@ union bpf_iter_link_info { * A new file descriptor (a nonnegative integer), or -1 if an * error occurred (in which case, *errno* is set appropriately). * + * BPF_MAP_GET_NUM_ENTRIES + * Description + * Get the number of entries currently present in the map. + * + * This operation is supported only for a subset of maps. If the + * map is not supported, the operation returns **EOPNOTSUPP**. + * + * Return + * Returns zero on success. On error, -1 is returned and *errno* + * is set appropriately. + * * NOTES * eBPF objects (maps and programs) can be shared between processes. * @@ -958,6 +969,7 @@ enum bpf_cmd { BPF_LINK_DETACH, BPF_PROG_BIND_MAP, BPF_TOKEN_CREATE, + BPF_MAP_GET_NUM_ENTRIES, __MAX_BPF_CMD, }; @@ -1837,6 +1849,11 @@ union bpf_attr { __u32 bpffs_fd; } token_create; + struct { /* struct used by BPF_MAP_GET_NUM_ENTRIES command*/ + __u32 map_fd; + __u32 num_entries; + } map_get_num_entries; + } __attribute__((aligned(8))); /* The description below is an attempt at providing documentation to eBPF diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 4e88797fdbeb..a31a63a4aa5d 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -5748,6 +5748,35 @@ static int token_create(union bpf_attr *attr) return bpf_token_create(attr); } +#define BPF_MAP_GET_NUM_ENTRIES_LAST_FIELD map_get_num_entries.num_entries + +static int bpf_get_num_entries(union bpf_attr *attr, union bpf_attr __user *uattr) +{ + __u32 num_entries = 0; + struct bpf_map *map; + + if (CHECK_ATTR(BPF_MAP_GET_NUM_ENTRIES)) + return -EINVAL; + + + CLASS(fd, f)(attr->map_fd); + map = __bpf_map_get(f); + if (IS_ERR(map)) + return PTR_ERR(map); + + if (!map->ops->map_num_entries) + return -EOPNOTSUPP; + + num_entries = map->ops->map_num_entries(map); + if (num_entries < 0) + return num_entries; + + if (put_user(num_entries, &uattr->map_get_num_entries.num_entries)) + return -EFAULT; + + return 0; +} + static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size) { union bpf_attr attr; @@ -5884,6 +5913,9 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size) case BPF_TOKEN_CREATE: err = token_create(&attr); break; + case BPF_MAP_GET_NUM_ENTRIES: + err = bpf_get_num_entries(&attr, uattr.user); + break; default: err = -EINVAL; break; From patchwork Mon Jan 6 14:53:27 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Charalampos Stylianopoulos X-Patchwork-Id: 13927569 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ED0CB433CB for ; Mon, 6 Jan 2025 14:54:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736175251; cv=none; b=Ndot5fDp4fstBgIVdwaDYVhIHM9tGJLYXH2meLEzrNfygCvPJ4yuFCT12K2iU0GM4cSK+ZRoEyRPyseIBuGJLd2YVbZJu8MirCe4MhylzCeYCH4K4gxoX3yD5I0DSmPLomVsTGkzh8Ux1e0D3b770fTuN5XKneuysy8m9Wkt/CA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736175251; c=relaxed/simple; bh=vD7OoCYZuWUrIQLKYDenhdTT+7LqLcv3pmLsJjxvzE4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UniWnpPDVrDCKbJl5F/Yg827xIOJQ8YbbPRH0XBoKsstHT1adIKsQLwhez9R2u0oYRQGTlntOEZXUqrI1ldQnmE0ycDWo/MEZu7zi9jFtJi8hgxwHvLXvrrhmnrLzYnRpCo4mtFNCrd9oTX8J2rzbgUDnWGXgaY4p3VmM3Dlm+s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=KoDsvIFM; arc=none smtp.client-ip=209.85.221.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KoDsvIFM" Received: by mail-wr1-f50.google.com with SMTP id ffacd0b85a97d-385ef8b64b3so11328068f8f.0 for ; Mon, 06 Jan 2025 06:54:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736175246; x=1736780046; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qLp9LvPNwq2kCY8NmpKp7heM93uAueEYhhM+Oqp3y0M=; b=KoDsvIFM4gbqiRleegWuBFPTMtjxWvJaXTRN89vrNSDF4PuusIhEX/XUBeDT5TPhva tqdEj0GRF1LycqQDE2WjtjRejAC0ltTh9Q/lzpWUAzf2p0jRbCMpZxndjT3jpyjRyaaR rdkfK8/SZE3/C4s0aAWed2nKfJMwecDsk0lh5ckAAEBC8JQnpTPHOncSTZGGI4qgf8te VqJkD84a+x85APalHpb2AOkqu9czVnbrPLKhls3eFvNhbLDIh4h2f2ZMz4V7s5W4blHj RHIvJy51YD8YFxO6GngbVFDfHSoHIL+lKKjq+at1IRzuQfjMxAtjU7VwinPKzI191vkc Lg6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736175246; x=1736780046; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qLp9LvPNwq2kCY8NmpKp7heM93uAueEYhhM+Oqp3y0M=; b=WGM3kVkYliBpwGL0qoMBneYF8Hw5LHFdwNifIG2hW6dB57ltbCR5gMx09Ddfj0f+FA BeAEfkpgvM6FT1r43zb2Fe7GcWAT4r7ibqU0yOuUnijBkscDQpPXOJ+q47lw84vDqVUF yBwz3sfnXuvnR0pIydw1jP/tW9IAz9O6Q7/p29FqyW2zFrSiD5GCTUDAlzXZXgpb+PPN somyr59n6JJtmSSm9sq6pdwjWcWSi++aqKReUH9IZg809+cXj5ZOY9eEc8z5GzNeMSyC 9dZ/M+p6/OHLgrwFMVhkVLTpEfYq6BKh5Xzs2zstm726FK9prhXRFYbmFlsequMW0RiM CPIQ== X-Gm-Message-State: AOJu0Yz3ux8JrzotBgQW9PSiJLDgUuTONRt57ykW1CxgvPKmQcuVPwVu 3OXz0SASzLsoUyMuavu8B844BZ1h2Wjs2ruTH1R5fjVb7qP3fkqwnKatpLl+BFezbA== X-Gm-Gg: ASbGnctuEeH7eCQKNmpqQCkNg/LRMXAK/Hum9371xo2a8NCMDDGuAp5oKi4ch2RVtSa dO/scDNp3sdqN5LoAkI+nKxHNPHRD2RtB0RhvBreib0KD35Cg3RAIgDZJ8KagbUSUrShZMBlG7t vIg6SMZTm2oZRFm8XDcE3AAjLACTwSMBjd3TJW1YZwFebdiXb4tntQ8SU2KtCxOOVodG+peho9K qlgAwpTJ35TdUFVeZ0B5BGvnok/Vkr2XNObJxyCRpqapAiojc9y8OlhNTZqHx45ixNaLvjhMus= X-Google-Smtp-Source: AGHT+IHsOOu+CCVWgz/n42h2Ko995f4jGOVQjt9NKSe1T9RcvY42dKw9FJCE353NCbxHI7BWZMgyfw== X-Received: by 2002:a05:6000:2ae:b0:385:ed16:c97 with SMTP id ffacd0b85a97d-38a224088aemr44648623f8f.49.1736175246287; Mon, 06 Jan 2025 06:54:06 -0800 (PST) Received: from babis.. ([2a02:3033:700:3ba2:3837:7343:334:7680]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38a1c832e74sm47389982f8f.30.2025.01.06.06.54.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Jan 2025 06:54:05 -0800 (PST) From: Charalampos Stylianopoulos To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Nick Zavaritsky , Charalampos Stylianopoulos Subject: [PATCH bpf-next 3/4] libbpf: Add support for MAP_GET_NUM_ENTRIES command Date: Mon, 6 Jan 2025 15:53:27 +0100 Message-ID: <20250106145328.399610-4-charalampos.stylianopoulos@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250106145328.399610-1-charalampos.stylianopoulos@gmail.com> References: <20250106145328.399610-1-charalampos.stylianopoulos@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Extend the libbpf API to provide support for getting the number of entries in a map. Co-developed-by: Nick Zavaritsky Signed-off-by: Nick Zavaritsky Signed-off-by: Charalampos Stylianopoulos --- tools/include/uapi/linux/bpf.h | 17 +++++++++++++++++ tools/lib/bpf/bpf.c | 16 ++++++++++++++++ tools/lib/bpf/bpf.h | 2 ++ tools/lib/bpf/libbpf.map | 1 + 4 files changed, 36 insertions(+) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 2acf9b336371..f5c7adea1387 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -903,6 +903,17 @@ union bpf_iter_link_info { * A new file descriptor (a nonnegative integer), or -1 if an * error occurred (in which case, *errno* is set appropriately). * + * BPF_MAP_GET_NUM_ENTRIES + * Description + * Get the number of entries currently present in the map. + * + * This operation is supported only for a subset of maps. If the + * map is not supported, the operation returns **EOPNOTSUPP**. + * + * Return + * Returns zero on success. On error, -1 is returned and *errno* + * is set appropriately. + * * NOTES * eBPF objects (maps and programs) can be shared between processes. * @@ -958,6 +969,7 @@ enum bpf_cmd { BPF_LINK_DETACH, BPF_PROG_BIND_MAP, BPF_TOKEN_CREATE, + BPF_MAP_GET_NUM_ENTRIES, __MAX_BPF_CMD, }; @@ -1837,6 +1849,11 @@ union bpf_attr { __u32 bpffs_fd; } token_create; + struct { /* struct used by BPF_MAP_GET_NUM_ENTRIES command*/ + __u32 map_fd; + __u32 num_entries; + } map_get_num_entries; + } __attribute__((aligned(8))); /* The description below is an attempt at providing documentation to eBPF diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index 359f73ead613..c91f43690624 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -1330,3 +1330,19 @@ int bpf_token_create(int bpffs_fd, struct bpf_token_create_opts *opts) fd = sys_bpf_fd(BPF_TOKEN_CREATE, &attr, attr_sz); return libbpf_err_errno(fd); } + +int bpf_map_get_num_entries(int fd, unsigned int *num_entries) +{ + const size_t attr_sz = offsetofend(union bpf_attr, map_get_num_entries); + union bpf_attr attr; + int ret; + + memset(&attr, 0, attr_sz); + attr.map_get_num_entries.map_fd = fd; + + ret = sys_bpf(BPF_MAP_GET_NUM_ENTRIES, &attr, attr_sz); + if (!ret) + *num_entries = attr.map_get_num_entries.num_entries; + + return libbpf_err_errno(ret); +} diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index 435da95d2058..efa5a092acc9 100644 --- a/tools/lib/bpf/bpf.h +++ b/tools/lib/bpf/bpf.h @@ -653,6 +653,8 @@ struct bpf_prog_bind_opts { LIBBPF_API int bpf_prog_bind_map(int prog_fd, int map_fd, const struct bpf_prog_bind_opts *opts); +LIBBPF_API int bpf_map_get_num_entries(int fd, unsigned int *num_entries); + struct bpf_test_run_opts { size_t sz; /* size of this struct for forward/backward compatibility */ const void *data_in; /* optional */ diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index a8b2936a1646..63dbad55cc2d 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -436,4 +436,5 @@ LIBBPF_1.6.0 { bpf_linker__add_buf; bpf_linker__add_fd; bpf_linker__new_fd; + bpf_map_get_num_entries; } LIBBPF_1.5.0; From patchwork Mon Jan 6 14:53:28 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Charalampos Stylianopoulos X-Patchwork-Id: 13927570 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 99A40149C7A for ; Mon, 6 Jan 2025 14:54:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736175260; cv=none; b=fjqFLBUvl3bvrZogyzGdqH/q6xDoFUXy0gSBZOW2F/AKoO2xUsbcR3mOb7v7vmLU7DTdecP5ltrk0M8T9FH61oBh9iAVQz4QH77TjJivWQZucMZ2a9PvMgXbVSAlNahABjOybv3dg1D5zDSlOxhAf+6IcMwJzMkskQsN/6xB2PY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736175260; c=relaxed/simple; bh=4ApDpjmxkffrRpNFhU+1HWLc7ansqaGNNqVeGbtIZxE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rMApmRCQoLyprBtZZh7w+HEsplw93AQHa+HqLkiY84M4pLQUIXtTuyfpIjIvaOW+j7wWzpnhZoLn6Yv94enSSB3xJltodY6R0KylwvSjLbjNDsa3cad4xGNgoMWmb74BbAfStbhn1u/+TCq2+kVGKG4zRy5A+Qv6+0hTIoVaa6k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=efaysKNc; arc=none smtp.client-ip=209.85.221.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="efaysKNc" Received: by mail-wr1-f41.google.com with SMTP id ffacd0b85a97d-385deda28b3so9534534f8f.0 for ; Mon, 06 Jan 2025 06:54:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736175254; x=1736780054; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+Nw6zDoZ2wfSbiPltCWmK0Kbd7iAlPXIjzSfpIYlNp8=; b=efaysKNcZBWJ4pA5sLVHxcHgYkg/8hwMWovTTuJ6UI0+i+C9YsrcIUHPhoVijoZwkV liz4ZvafPs359CyjnBxRBBs2sAM5OkuA9EFLgnpJlfJZnOwkzotQrVBvX60xh5YGhQil rhiKzCJEBYBsVqrWx+zmtoJRZtJ0lgXwIsvQeTFgygiGIkzojEMbLoVbVEL8SHZ5LY2J yelDwh2C16hWQVfP/XfcygpPNyxwr/veSXEzh4Fs1Pd9tIDn7aI032T5mRL3fO/gKGpn 7rvSMCQ1ItiWEk6h4zByYRMkR6HIISaA11RBWWXTFiHGv9obs8piA8+d/zT4qeFpVe7Q 7a8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736175254; x=1736780054; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+Nw6zDoZ2wfSbiPltCWmK0Kbd7iAlPXIjzSfpIYlNp8=; b=GwsCEXooa+Z2aAW5WZJAymDOQbwNx01Q+jBOa7INaEb3yw4qDuHA7Fn/8CrwyYOU4/ 9bgW5DWjjDN3N6UF/pKGasH9qk5cZmwTxZwy/9RyRA7VNJw3QEALaGjB7T3hQAWwRV3T RpJNM1yrCLZ16kBiycUTLZxSuWyywWem/P37aWYU10zFhqVU4i/2PanJRrVVW47ZoTTp iOuesvcwodoruYQzzhYiGTHVZATJOdMwvcLbK7CD4pWDuvSYLKDTpKcxTh54IoQFo6UP 6sS0HUoOFw0q3R9lB/QWPtb38LKuZ5JCawy7z8g52KwZtDHDSD/ypGMd7KAWscjKK5hi aTPQ== X-Gm-Message-State: AOJu0YxeNcq9H9MoXcjlB6HYJUnz42OLptKHgFC6mWOWcAsdUjWIY1y5 7O3KLGqHyHxwN12AcxfC88Z6Jxk7K63+kBs65ejNUvs8sRIkWEuOU0pRmvfz2oDOHQ== X-Gm-Gg: ASbGncvomma7W6YT19j4muqu0azisLEMRCbSlNl/ykvN+WqoM2kTv/wYVMItDvwRX4K sk0Q+A9rRdn859/6mTJH0QYGhNYNySyoszhgnL1q8LAdL2osa1i4fEj7dgUbMCfd7pmgDczQcCr c4Yn5os7gWWuAHB8drt5/4MDaG0aOfzvjOOoT4VeBoYBUX0javkUgXAfjC9RNU/JQH8OhkXFZh3 l2VFTR8UsOkyspLU5QH/RzHfGgkoBtBjph0quUk4duiUNSXWYZIhnLlzZ7H8ZkXe7/Rgp/CqsQ= X-Google-Smtp-Source: AGHT+IHg1jdNIkXuSPlEnPnr+qsq5p3Jt3KiFth+eVB2Q0u2U2PhjPxArmRPr/HdH9B45JUMs141VQ== X-Received: by 2002:a5d:47a7:0:b0:386:4034:f9a6 with SMTP id ffacd0b85a97d-38a223fd8d3mr44235049f8f.57.1736175254359; Mon, 06 Jan 2025 06:54:14 -0800 (PST) Received: from babis.. ([2a02:3033:700:3ba2:3837:7343:334:7680]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38a1c832e74sm47389982f8f.30.2025.01.06.06.54.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Jan 2025 06:54:13 -0800 (PST) From: Charalampos Stylianopoulos To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Nick Zavaritsky , Charalampos Stylianopoulos Subject: [PATCH bpf-next 4/4] selftests/bpf: Add tests for bpf_map_get_num_entries Date: Mon, 6 Jan 2025 15:53:28 +0100 Message-ID: <20250106145328.399610-5-charalampos.stylianopoulos@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250106145328.399610-1-charalampos.stylianopoulos@gmail.com> References: <20250106145328.399610-1-charalampos.stylianopoulos@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Extend the existing selftests for maps with checks that the number of map entries returned by bpf_map_get_num_entries is as expected. Co-developed-by: Nick Zavaritsky Signed-off-by: Nick Zavaritsky Signed-off-by: Charalampos Stylianopoulos --- .../bpf/map_tests/lpm_trie_map_basic_ops.c | 5 +++ tools/testing/selftests/bpf/test_maps.c | 35 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c b/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c index d32e4edac930..40265b497791 100644 --- a/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c +++ b/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c @@ -434,6 +434,11 @@ static void test_lpm_ipaddr(void) inet_pton(AF_INET6, "2a00:ffff::", key_ipv6->data); assert(bpf_map_lookup_elem(map_fd_ipv6, key_ipv6, &value) == -ENOENT); + unsigned int entries; + /* Check that the reported number of entries in the map is as expected. */ + assert(bpf_map_get_num_entries(map_fd_ipv4, &entries) == 0 && entries == 5); + assert(bpf_map_get_num_entries(map_fd_ipv6, &entries) == 0 && entries == 1); + close(map_fd_ipv4); close(map_fd_ipv6); } diff --git a/tools/testing/selftests/bpf/test_maps.c b/tools/testing/selftests/bpf/test_maps.c index 8b40e9496af1..c61cf740a6b6 100644 --- a/tools/testing/selftests/bpf/test_maps.c +++ b/tools/testing/selftests/bpf/test_maps.c @@ -109,6 +109,10 @@ static void test_hashmap(unsigned int task, void *data) assert(bpf_map_get_next_key(fd, &next_key, &next_key) < 0 && errno == ENOENT); + unsigned int entries; + /* Check that the number of entries in the map is 2. */ + assert(bpf_map_get_num_entries(fd, &entries) == 0 && entries == 2); + /* Delete both elements. */ key = 1; assert(bpf_map_delete_elem(fd, &key) == 0); @@ -122,6 +126,7 @@ static void test_hashmap(unsigned int task, void *data) errno == ENOENT); assert(bpf_map_get_next_key(fd, &key, &next_key) < 0 && errno == ENOENT); + assert(bpf_map_get_num_entries(fd, &entries) == 0 && entries == 0); close(fd); } @@ -243,6 +248,11 @@ static void test_hashmap_percpu(unsigned int task, void *data) key = 1; assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0); + + unsigned int entries; + /* Check that the number of entries in the map is 2. */ + assert(bpf_map_get_num_entries(fd, &entries) == 0 && entries == 2); + /* Delete both elements. */ key = 1; assert(bpf_map_delete_elem(fd, &key) == 0); @@ -256,6 +266,7 @@ static void test_hashmap_percpu(unsigned int task, void *data) errno == ENOENT); assert(bpf_map_get_next_key(fd, &key, &next_key) < 0 && errno == ENOENT); + assert(bpf_map_get_num_entries(fd, &entries) == 0 && entries == 0); close(fd); } @@ -529,6 +540,10 @@ static void test_devmap_hash(unsigned int task, void *data) exit(1); } + unsigned int entries; + /* Check that the number of entries in the map is 0. */ + assert(bpf_map_get_num_entries(fd, &entries) == 0 && entries == 0); + close(fd); } @@ -557,10 +572,17 @@ static void test_queuemap(unsigned int task, void *data) exit(1); } + unsigned int entries; + /* Check that the number of entries in the map is 0. */ + assert(bpf_map_get_num_entries(fd, &entries) == 0 && entries == 0); + /* Push MAP_SIZE elements */ for (i = 0; i < MAP_SIZE; i++) assert(bpf_map_update_elem(fd, NULL, &vals[i], 0) == 0); + /* Check that the number of entries in the map is MAP_SIZE. */ + assert(bpf_map_get_num_entries(fd, &entries) == 0 && entries == MAP_SIZE); + /* Check that element cannot be pushed due to max_entries limit */ assert(bpf_map_update_elem(fd, NULL, &val, 0) < 0 && errno == E2BIG); @@ -581,6 +603,9 @@ static void test_queuemap(unsigned int task, void *data) assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) < 0 && errno == ENOENT); + /* Check that the number of entries in the map is 0. */ + assert(bpf_map_get_num_entries(fd, &entries) == 0 && entries == 0); + /* Check that non supported functions set errno to EINVAL */ assert(bpf_map_delete_elem(fd, NULL) < 0 && errno == EINVAL); assert(bpf_map_get_next_key(fd, NULL, NULL) < 0 && errno == EINVAL); @@ -613,10 +638,17 @@ static void test_stackmap(unsigned int task, void *data) exit(1); } + unsigned int entries; + /* Check that the number of entries in the map is 0. */ + assert(bpf_map_get_num_entries(fd, &entries) == 0 && entries == 0); + /* Push MAP_SIZE elements */ for (i = 0; i < MAP_SIZE; i++) assert(bpf_map_update_elem(fd, NULL, &vals[i], 0) == 0); + /* Check that the number of entries in the map is MAP_SIZE. */ + assert(bpf_map_get_num_entries(fd, &entries) == 0 && entries == MAP_SIZE); + /* Check that element cannot be pushed due to max_entries limit */ assert(bpf_map_update_elem(fd, NULL, &val, 0) < 0 && errno == E2BIG); @@ -637,6 +669,9 @@ static void test_stackmap(unsigned int task, void *data) assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) < 0 && errno == ENOENT); + /* Check that the number of entries in the map is 0. */ + assert(bpf_map_get_num_entries(fd, &entries) == 0 && entries == 0); + /* Check that non supported functions set errno to EINVAL */ assert(bpf_map_delete_elem(fd, NULL) < 0 && errno == EINVAL); assert(bpf_map_get_next_key(fd, NULL, NULL) < 0 && errno == EINVAL);