Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp1203743ybi; Fri, 21 Jun 2019 16:18:27 -0700 (PDT) X-Google-Smtp-Source: APXvYqyhxDqQ1EK8M1y09PLDArf5aqZjjYuPwtJZ8w9kDnkw7ab9KWHjZsFJKFOJUDsbC07/R/LN X-Received: by 2002:a17:902:a414:: with SMTP id p20mr23312219plq.187.1561159107434; Fri, 21 Jun 2019 16:18:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561159107; cv=none; d=google.com; s=arc-20160816; b=aPXOLA6KFfGLl3hohqATSTtADwuMxX7FhOMRWr8P5wbpFzwPrYiv9JmdcTeq5g7JXk d8+/C/zC8IKUq0Ahs+mQJlf8jhHoozstBpu+HFnuHjOpZauFGyFqb8IVuJXYXMzhXhjv 4F9nhpdLtVZZr/s/3LUn53259kRK8qB08prU9bQEc9MWQ3LuIOY9USsfC8uTlcymL93b Ayi8ZtojmZ+/DrJcRr/iii2B8aZVcbtxnPn3ZZyF2RL7HT2TAwSH3YNLW5aETaZEmMIm 24tp0LGniWPcwQ3OYTQSC4Gw1TaRPIVaRbCsAWEFgaaTVYLJJU4C5ZakCO5U53t4qD7Q oNUQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=rj82FQVe1sxehS1oKg3PuFxZ1lm7MWUrxe2hdxNBH9E=; b=l0ElSd5pBNqeU1tbfwwM4QuochavGaMzGKWW+LaEzoEFTQuBGiofFa1mqKTxSKVLS5 bjWjNFikdQdXaTIpZxd5aqlU/mvknA1K0zq3zPWdOqaqOSF20hWGRCh+yE+pz2KDrzNf E4Xh9Xz9EuXfDB9CQTK/uRVkJqzclLqjIvoLyGm4JXqMDE5wwZ3jkcggMXoyeVw1xF+A rAk/HkDV8NO24+w5QHiwy/csGG7vLEE8uHjlFOfXtKrDIxQaGGwHyTniHGPPoyMbjF9M akwPkmxBTzffRP+DdX+k3SXj+bVyM++WCqOd12s3BGUv9wMMlrydu+yoxJfHv4oWVyqd SrJw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ejwl+xG1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j62si3707007pgc.71.2019.06.21.16.18.12; Fri, 21 Jun 2019 16:18:27 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ejwl+xG1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726225AbfFUXRZ (ORCPT + 99 others); Fri, 21 Jun 2019 19:17:25 -0400 Received: from mail-yb1-f201.google.com ([209.85.219.201]:52655 "EHLO mail-yb1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726231AbfFUXRL (ORCPT ); Fri, 21 Jun 2019 19:17:11 -0400 Received: by mail-yb1-f201.google.com with SMTP id k79so6134659ybk.19 for ; Fri, 21 Jun 2019 16:17:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=rj82FQVe1sxehS1oKg3PuFxZ1lm7MWUrxe2hdxNBH9E=; b=ejwl+xG1axIiX3s05MG5iOCBdiQJwCRP/hHzUbUllzw29ytDqE0pElJwSom0wYfra5 5MjJoFegVuAk+nZ+WyyCb71ze2pMC/TRJ/yaPQoExGYuQt73bUOo2qzqrLCjSbZ9QeUs tNhEJ+I+L7smjvdoVUqOaTJWHTryAq4t1n6bGYN4pk6vL9lkBW7uwFAn1/BafF6fsSQ7 1oMc0vxtdOeh7BS+RnQxScDeREIzxslQIhUVwQzTFrzGrV05ok6Ctx2+dCbswF8vSUvO /3WICH5e7MoPGRoXCWn6Vfsi9QxwkjH37852nSbRIT6TEeJGIyJKnDktMvggJfpn8OZe 5a3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=rj82FQVe1sxehS1oKg3PuFxZ1lm7MWUrxe2hdxNBH9E=; b=eTTxsBcSlKrDGmPufDFazPTjpxW8UBvvs/9Q9EjxCxZ1T0li+66DSm71YABLFI8BZX L7N6ys4sM7DrOL83kmZ51uP7U8Lj2rImoD2CxTewcMKCvkl4Df1AB55LelcjQdnECiyl EGmO6JC7VvAX5zyDGTt/Ju6pRTJyicvOYC1S8VeQVh2jioqSPtQWUIOfhCUumGNLXIUT 7OUTBU4N7kXyP2Kx2h/ZdoSJRxicJK64qhEEkmiUbFKoLyS/JmNAdXLmGiwnoeIyqZ2T qUbCPertuC+ddOOCf2OX/B6ubOWRNnFFrC+qrDH1FM4CYTJE/kC1iNJFhflXPrz+dJYl KloQ== X-Gm-Message-State: APjAAAVBhKfL/gTpMttBJGnk46LcEatDtyVqyV7dOSxC6Np1UugDbC21 qzOOhFa17rq8IRybA3whaq4a7BSfjKyg X-Received: by 2002:a0d:e1c1:: with SMTP id k184mr67567296ywe.153.1561159030572; Fri, 21 Jun 2019 16:17:10 -0700 (PDT) Date: Fri, 21 Jun 2019 16:16:49 -0700 In-Reply-To: <20190621231650.32073-1-brianvv@google.com> Message-Id: <20190621231650.32073-6-brianvv@google.com> Mime-Version: 1.0 References: <20190621231650.32073-1-brianvv@google.com> X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog Subject: [RFC PATCH 5/6] selftests/bpf: test BPF_MAP_DUMP command on a bpf hashmap From: Brian Vazquez To: Brian Vazquez , Alexei Starovoitov , Daniel Borkmann , "David S . Miller" Cc: Stanislav Fomichev , Willem de Bruijn , Petar Penkov , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, Brian Vazquez Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This tests exercise the new command on a bpf hashmap and make sure it works as expected. Signed-off-by: Brian Vazquez --- tools/testing/selftests/bpf/test_maps.c | 70 ++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/bpf/test_maps.c b/tools/testing/selftests/bpf/test_maps.c index a3fbc571280a9..3df72b46fd1d9 100644 --- a/tools/testing/selftests/bpf/test_maps.c +++ b/tools/testing/selftests/bpf/test_maps.c @@ -309,6 +309,73 @@ static void test_hashmap_walk(unsigned int task, void *data) close(fd); } +static void test_hashmap_dump(void) +{ + int fd, i, max_entries = 3; + uint64_t keys[max_entries], values[max_entries]; + uint64_t key, value, next_key; + bool next_key_valid = true; + void *buf, *elem, *prev_key; + u32 buf_len; + const int elem_size = sizeof(key) + sizeof(value); + + fd = helper_fill_hashmap(max_entries); + + // Get the elements in the hashmap, and store them in that order + assert(bpf_map_get_next_key(fd, NULL, &key) == 0); + i = 0; + keys[i] = key; + for (i = 1; next_key_valid; i++) { + next_key_valid = bpf_map_get_next_key(fd, &key, &next_key) == 0; + assert(bpf_map_lookup_elem(fd, &key, &values[i - 1]) == 0); + keys[i-1] = key; + key = next_key; + } + + // Alloc memory for the whole table + buf = malloc(elem_size * max_entries); + assert(buf != NULL); + + // Check that buf_len < elem_size returns EINVAL + buf_len = elem_size-1; + errno = 0; + assert(bpf_map_dump(fd, NULL, buf, &buf_len) == -1 && errno == EINVAL); + + // Check that it returns the first two elements + errno = 0; + buf_len = elem_size * 2; + prev_key = NULL; + i = 0; + assert(bpf_map_dump(fd, prev_key, buf, &buf_len) == 0 && + buf_len == 2*elem_size); + elem = buf; + assert((*(uint64_t *)elem) == keys[i] && + (*(uint64_t *)(elem + sizeof(key))) == values[i]); + elem = buf + elem_size; + i++; + assert((*(uint64_t *)elem) == keys[i] && + (*(uint64_t *)(elem + sizeof(key))) == values[i]); + i++; + + /* Continue reading from map and verify buf_len only contains 1 element + * even though buf_len is 2 elem_size. + */ + prev_key = elem; + assert(bpf_map_dump(fd, prev_key, buf, &buf_len) == 0 && + buf_len == elem_size); + elem = buf; + assert((*(uint64_t *)elem) == keys[i] && + (*(uint64_t *)(elem + sizeof(key))) == values[i]); + + // Check that there are no more entries after last_key + prev_key = &keys[i]; + assert(bpf_map_dump(fd, prev_key, buf, &buf_len) == -1 && + errno == ENOENT); + + free(buf); + close(fd); +} + static void test_hashmap_zero_seed(void) { int i, first, second, old_flags; @@ -1668,6 +1735,7 @@ static void run_all_tests(void) test_hashmap_percpu(0, NULL); test_hashmap_walk(0, NULL); test_hashmap_zero_seed(); + test_hashmap_dump(); test_arraymap(0, NULL); test_arraymap_percpu(0, NULL); @@ -1705,11 +1773,9 @@ int main(void) map_flags = BPF_F_NO_PREALLOC; run_all_tests(); - #define CALL #include #undef CALL - printf("test_maps: OK, %d SKIPPED\n", skips); return 0; } -- 2.22.0.410.gd8fdbe21b5-goog