From patchwork Tue Mar 23 02:50:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Tammela X-Patchwork-Id: 408496 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_INVALID, DKIM_SIGNED, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B1D03C433C1 for ; Tue, 23 Mar 2021 02:52:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 784FF6191F for ; Tue, 23 Mar 2021 02:52:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229962AbhCWCvn (ORCPT ); Mon, 22 Mar 2021 22:51:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229993AbhCWCvi (ORCPT ); Mon, 22 Mar 2021 22:51:38 -0400 Received: from mail-qk1-x72b.google.com (mail-qk1-x72b.google.com [IPv6:2607:f8b0:4864:20::72b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BBBF4C061574; Mon, 22 Mar 2021 19:51:36 -0700 (PDT) Received: by mail-qk1-x72b.google.com with SMTP id i9so12960333qka.2; Mon, 22 Mar 2021 19:51:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Ptq96JfaAwnF7JCWYyRFzxbIiqtchclo0BTDiv9S/Wk=; b=YfdKwY6l+UiL6SO97vppfXP0/18kSVySI/bGL12f7axAMlgizwigCeGO7zHYKa7leS uxGuoBaT+HH4RU0BuHVmcmqozwVLJ7QFFWstjHABRSomlsLaNU+Xu8NH8/FVgp8Rko/G lpKN3ta7W/wUQuPN45GYlv/BXvhxiBca+K1z/ZMVlSniIs5qu3M5s5OpP5i7C/pZKY24 9v5Tvj01jhZ+GgMj3ccw5SR2r1M7c/TVZh3vQs+3GIryMwuQzwVOQ2d1Fpfqy5579mvl Ev4QYn+EdRBGnGXos9vEZ7GvK2p6EvwpB2Yv5Eb1DAa7mTDS5eU5Y5HrUhBL0N1GQKYE wK2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Ptq96JfaAwnF7JCWYyRFzxbIiqtchclo0BTDiv9S/Wk=; b=LO8OntzWronFGUCNJ/dvLwMZW7a7oeiDmvbuRjg4QQxEBrTKhvt3wEqPm/M+gI6PZk a2Eg3i2BHNTo/F3ki9CEuEy1ZZ19JbFON/utne0VPjWFcg4TO+srJquR+w7HBaW7nrv8 Igr5/JAB2xz8wWnG9yqbl5pYh28ucDdR2UzRyxbI34PmFSTsjUo7afjft877UhkEyBMJ R69/A00ON9deOkur7sjUyezdB2Eh7tGn12VoJBmWG2dXjmqLoqdA00qrvfkcoLeFkqNX 8btDRfNv9yd5y70bH8ebCsVpx6HHo8vRHYvBshwfvQvt6l3VvQS1HfJL15wFa0N0W+30 5SCg== X-Gm-Message-State: AOAM5311oZlcHFGthx16MQX/OPf0WBSlaSrm9rFW2Bh6/t+yRhnuNZiP OMq1gPql7hdWBv3CPFktzEE= X-Google-Smtp-Source: ABdhPJyrDLNwa9vOgD7S+DxD/8ZF1lJdxwqEtepBPaA6ozDvH2mjwsGEM6ntAKQ4gQy3wfl//an36A== X-Received: by 2002:ae9:ee07:: with SMTP id i7mr3271884qkg.233.1616467896061; Mon, 22 Mar 2021 19:51:36 -0700 (PDT) Received: from localhost.localdomain ([179.218.4.27]) by smtp.gmail.com with ESMTPSA id h13sm292265qtn.26.2021.03.22.19.51.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Mar 2021 19:51:35 -0700 (PDT) From: Pedro Tammela Cc: Jamal Hadi Salim , Pedro Tammela , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Shuah Khan , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v2 1/2] bpf: add support for batched ops in LPM trie maps Date: Mon, 22 Mar 2021 23:50:53 -0300 Message-Id: <20210323025058.315763-2-pctammela@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210323025058.315763-1-pctammela@gmail.com> References: <20210323025058.315763-1-pctammela@gmail.com> MIME-Version: 1.0 To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Pedro Tammela Suggested-by: Jamal Hadi Salim Signed-off-by: Pedro Tammela --- kernel/bpf/lpm_trie.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/kernel/bpf/lpm_trie.c b/kernel/bpf/lpm_trie.c index cec792a17e5f..1b7b8a6f34ee 100644 --- a/kernel/bpf/lpm_trie.c +++ b/kernel/bpf/lpm_trie.c @@ -726,6 +726,9 @@ const struct bpf_map_ops trie_map_ops = { .map_lookup_elem = trie_lookup_elem, .map_update_elem = trie_update_elem, .map_delete_elem = trie_delete_elem, + .map_lookup_batch = generic_map_lookup_batch, + .map_update_batch = generic_map_update_batch, + .map_delete_batch = generic_map_delete_batch, .map_check_btf = trie_check_btf, .map_btf_name = "lpm_trie", .map_btf_id = &trie_map_btf_id, From patchwork Tue Mar 23 02:50:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Tammela X-Patchwork-Id: 408495 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_INVALID, DKIM_SIGNED, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 260A4C433E0 for ; Tue, 23 Mar 2021 02:52:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E6506619AF for ; Tue, 23 Mar 2021 02:52:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230006AbhCWCwO (ORCPT ); Mon, 22 Mar 2021 22:52:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229993AbhCWCv4 (ORCPT ); Mon, 22 Mar 2021 22:51:56 -0400 Received: from mail-qk1-x734.google.com (mail-qk1-x734.google.com [IPv6:2607:f8b0:4864:20::734]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C5BEC061574; Mon, 22 Mar 2021 19:51:45 -0700 (PDT) Received: by mail-qk1-x734.google.com with SMTP id g20so12966959qkk.1; Mon, 22 Mar 2021 19:51:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=aXRkbPr5evH4vo/g8h5sz3BYjvs8YssE9s0+neI0mfM=; b=sLoellt9v8ugioZgjloCW57uqmvQpkX5N68fYMQBim1adkY8rp52LO9FrxuZjxc/GF MOSVOcFeJC9T8ounFkOCRklJhx5mTPT05AhWwNEH4HNDWzPXaOFwXoId+AcmjKyC8U35 aH+8aVwJS4T6VzUp1KZbyilfGxX3LZyFAIOMfo3JAqGbcu3obxTNQ5sKPcfEuZOZlVeV mWLGCssRcItCK6AbZQSr5ZR98OA/WOCHivERRPzoSG6MWUFkCfbifHYkOJcxXALrchm2 u/gqyKt0APyFN16HGTnijfaI1FA1AxRuaAtNP6LHffinOIIAP3KUC3YjYgGbn+/pGGCb AgZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=aXRkbPr5evH4vo/g8h5sz3BYjvs8YssE9s0+neI0mfM=; b=iPKr/dkjHKXBFYhCMtkXQXWiPb76iVvJh49nFQO0MyrOpgmJ8ddWVAWmjDxiW3m+XZ u6YjcFwTSJtkL/oPm7sZcDoXmOZ4UUPQ6ZAlogGRWs6g8qgMicmNjoJlAvgZpg+y+ZUq im2dJXEimo6A47KdiXyzo7ByKPYqlO+4LVeAyVscB+fp+Ic1SE7v7StCaadAMfSuR4De TsnxTddYgRHzI8OQuH5QTqCyp9byFodmLIiP39Ps64gIbunSifEeOlkBIfKQHNIFGttL 8RMbEE2jOS8XlNhv1P0o7XkaBHTHex2dmC8V3CHzreTJcyprC49spqc9iRboqG/KtE4z hA1g== X-Gm-Message-State: AOAM530pM/OiiDt5t1rrwJdYEa/JkZ2f+a3smlpmEXJOUUWFd7wyNvdF rkcwjizJy69MrnvELm2zYZA= X-Google-Smtp-Source: ABdhPJxG99u8YAT+7kA3y9w1Ww1RgiU3DlfrhRnyw1CNtbCpSenq+mSDO/hV6O2RF13QPCS15+sSBA== X-Received: by 2002:a05:620a:791:: with SMTP id 17mr3443873qka.170.1616467904835; Mon, 22 Mar 2021 19:51:44 -0700 (PDT) Received: from localhost.localdomain ([179.218.4.27]) by smtp.gmail.com with ESMTPSA id h13sm292265qtn.26.2021.03.22.19.51.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Mar 2021 19:51:44 -0700 (PDT) From: Pedro Tammela Cc: Jamal Hadi Salim , Pedro Tammela , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Shuah Khan , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v2 2/2] bpf: selftests: add tests for batched ops in LPM trie maps Date: Mon, 22 Mar 2021 23:50:54 -0300 Message-Id: <20210323025058.315763-3-pctammela@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210323025058.315763-1-pctammela@gmail.com> References: <20210323025058.315763-1-pctammela@gmail.com> MIME-Version: 1.0 To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Pedro Tammela Uses the already existing infrastructure for testing batched ops. The testing code is essentially the same, with minor tweaks for this use case. Suggested-by: Jamal Hadi Salim Signed-off-by: Pedro Tammela --- .../map_tests/lpm_trie_map_batch_ops.c (new) | 158 ++++++++++++++++++ 1 file changed, 158 insertions(+) diff --git a/tools/testing/selftests/bpf/map_tests/lpm_trie_map_batch_ops.c b/tools/testing/selftests/bpf/map_tests/lpm_trie_map_batch_ops.c new file mode 100644 index 000000000000..2e986e5e4cac --- /dev/null +++ b/tools/testing/selftests/bpf/map_tests/lpm_trie_map_batch_ops.c @@ -0,0 +1,158 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +struct test_lpm_key { + __u32 prefix; + struct in_addr ipv4; +}; + +static void map_batch_update(int map_fd, __u32 max_entries, + struct test_lpm_key *keys, int *values) +{ + __u32 i; + int err; + char buff[16] = { 0 }; + DECLARE_LIBBPF_OPTS(bpf_map_batch_opts, opts, + .elem_flags = 0, + .flags = 0, + ); + + for (i = 0; i < max_entries; i++) { + keys[i].prefix = 32; + snprintf(buff, 16, "192.168.1.%d", i + 1); + inet_pton(AF_INET, buff, &keys[i].ipv4); + values[i] = i + 1; + } + + err = bpf_map_update_batch(map_fd, keys, values, &max_entries, &opts); + CHECK(err, "bpf_map_update_batch()", "error:%s\n", strerror(errno)); +} + +static void map_batch_verify(int *visited, __u32 max_entries, + struct test_lpm_key *keys, int *values) +{ + char buff[16] = { 0 }; + int lower_byte = 0; + __u32 i; + + memset(visited, 0, max_entries * sizeof(*visited)); + for (i = 0; i < max_entries; i++) { + inet_ntop(AF_INET, &keys[i].ipv4, buff, 32); + CHECK(sscanf(buff, "192.168.1.%d", &lower_byte) == EOF, + "sscanf()", "error: i %d\n", i); + CHECK(lower_byte != values[i], "key/value checking", + "error: i %d key %s value %d\n", i, buff, values[i]); + visited[i] = 1; + } + for (i = 0; i < max_entries; i++) { + CHECK(visited[i] != 1, "visited checking", + "error: keys array at index %d missing\n", i); + } +} + +void test_lpm_trie_map_batch_ops(void) +{ + struct bpf_create_map_attr xattr = { + .name = "lpm_trie_map", + .map_type = BPF_MAP_TYPE_LPM_TRIE, + .key_size = sizeof(struct test_lpm_key), + .value_size = sizeof(int), + .map_flags = BPF_F_NO_PREALLOC, + }; + struct test_lpm_key *keys, key; + int map_fd, *values, *visited; + __u32 step, count, total, total_success; + const __u32 max_entries = 10; + __u64 batch = 0; + int err; + DECLARE_LIBBPF_OPTS(bpf_map_batch_opts, opts, + .elem_flags = 0, + .flags = 0, + ); + + xattr.max_entries = max_entries; + map_fd = bpf_create_map_xattr(&xattr); + CHECK(map_fd == -1, "bpf_create_map_xattr()", "error:%s\n", + strerror(errno)); + + keys = malloc(max_entries * sizeof(struct test_lpm_key)); + values = malloc(max_entries * sizeof(int)); + visited = malloc(max_entries * sizeof(int)); + CHECK(!keys || !values || !visited, "malloc()", "error:%s\n", + strerror(errno)); + + total_success = 0; + for (step = 1; step < max_entries; step++) { + map_batch_update(map_fd, max_entries, keys, values); + map_batch_verify(visited, max_entries, keys, values); + memset(keys, 0, max_entries * sizeof(*keys)); + memset(values, 0, max_entries * sizeof(*values)); + batch = 0; + total = 0; + /* iteratively lookup/delete elements with 'step' + * elements each. + */ + count = step; + while (true) { + err = bpf_map_lookup_batch(map_fd, + total ? &batch : NULL, &batch, + keys + total, values + total, &count, &opts); + + CHECK((err && errno != ENOENT), "lookup with steps", + "error: %s\n", strerror(errno)); + + total += count; + if (err) + break; + } + + CHECK(total != max_entries, "lookup with steps", + "total = %u, max_entries = %u\n", total, max_entries); + + map_batch_verify(visited, max_entries, keys, values); + + total = 0; + count = step; + while (total < max_entries) { + if (max_entries - total < step) + count = max_entries - total; + err = bpf_map_delete_batch(map_fd, keys + total, &count, + &opts); + CHECK((err && errno != ENOENT), "delete batch", + "error: %s\n", strerror(errno)); + total += count; + if (err) + break; + } + CHECK(total != max_entries, "delete with steps", + "total = %u, max_entries = %u\n", total, max_entries); + + /* check map is empty, errono == ENOENT */ + err = bpf_map_get_next_key(map_fd, NULL, &key); + CHECK(!err || errno != ENOENT, "bpf_map_get_next_key()", + "error: %s\n", strerror(errno)); + + total_success++; + } + + CHECK(total_success == 0, "check total_success", + "unexpected failure\n"); + + printf("%s:PASS\n", __func__); + + free(keys); + free(values); + free(visited); +}