Received: by 2002:a25:f815:0:0:0:0:0 with SMTP id u21csp2495545ybd; Thu, 27 Jun 2019 13:25:47 -0700 (PDT) X-Google-Smtp-Source: APXvYqzraSww6RLE44RFF4z/c/VOZOW07LVNRPMBoEIrbJkr8g4TTMXmrLcg/coxjGH85mL/MPN2 X-Received: by 2002:a17:90a:fa12:: with SMTP id cm18mr8265510pjb.137.1561667147491; Thu, 27 Jun 2019 13:25:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561667147; cv=none; d=google.com; s=arc-20160816; b=wyRGQZZ9QoC85rG0+Ltrd2MYUWkOLwDzv/im4qcrycyhU2RtOPRMJL9TuelxSEOfTP hMa+LC+op9FIVFZkCqpr/I7eR7O+1gcgb0P25nBLgFhYTklNZ0XgTbYvbk9H+tjFkwvg NndPFUdC2HsfSmWsI6cXwdsfgJaS6rJwuB00yyHijFvWKoNgOjg3pbkAqDAW0YQ54DxN cUu5XHtqLzAm4Zv5Ng/1qFKunzHbJ03GJ3GN8hPzfuriezsk0CAhZ4Q6R7I0oMkp3NNf 0c7Zo63iKZ7BV61PpMdHOLdWZpQxYcXLpmO7Qx3Vw1l+Rh1QMlpmX4KXEugWzDxu80en hT1Q== 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=lLn95CdgH1dmGs8r5dRHO7Qrd5ZtUcRaeOKeGSQIHgWosoCM/+BxL6oR8hDnWi4hxc sQmagAWwYAgxdL/5rd9g1BNPkQLGhVu42dc9t+bdJzJHb1wEv9ZxvtgN6zXFpepDpGs4 uxMfkKKtennLULsonbZkeuZVz8hkQ3vuyfsYYCfSbOruEpNksIu1DP4o7ZZNrH/O/FBo fTwyM+w277jCX3dEwrvs1q24p9CVm1P9r9AlXJM8WA783S2XmmX1zWbBYLp6+CMID90H eheOd0nTJTokS4Pv7fugT7NOwgdkc5FF+zPAm+q0JmbU8zq4MnqEMANQAfB/8R68ugs/ Hkdw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=wKyqjXW+; 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 u16si22805pjb.15.2019.06.27.13.25.31; Thu, 27 Jun 2019 13:25:47 -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=wKyqjXW+; 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 S1726696AbfF0UYv (ORCPT + 99 others); Thu, 27 Jun 2019 16:24:51 -0400 Received: from mail-qt1-f201.google.com ([209.85.160.201]:45651 "EHLO mail-qt1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726648AbfF0UYq (ORCPT ); Thu, 27 Jun 2019 16:24:46 -0400 Received: by mail-qt1-f201.google.com with SMTP id k8so3644628qtb.12 for ; Thu, 27 Jun 2019 13:24:45 -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=wKyqjXW+i8bJU7mw8YLb9CipelcwkZ19jDfD8mhZg6zX8WN7guD9x4twWyioOsfUr5 B8Catk+137ZQ7uAvobbbPYWcGnPjhZQVvEhjSxL5YMiRhzw43C3kQEcdh4Y/fAvahwQK YmwmH2G6+6sOi+6VRubL8X/3UYMrcUvESFFkh+FJ6ZkZVrePX+V7sYg2175Rqs7N6+GT 3D1d57hURzUbUDHOHRBQaXuBjMUZazLLoSV2zoMqsPqCYiHwMYCNe+fA51dDwgE5XYlI rxM0/YCNJRGtfDdyW1VYd8nC80yvPJn2awyYjbqsrRtiuMrYLzuonr7bk+51Rd+YZhMH 2qcw== 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=hDZTxQ1fRLmvdIRs+UQUBB/GL4KCRZA4S6JScKQaTRpZR3W52x5L7w5kEXywvJ9lkr DUVIMZNu3AAhruKac6kZHbaZzCUbgbaqx+uBigzJPoMW5r8W4D1uTj9h1wkS14aG/vJQ k7+pmXJJMNTQI4ZStse6YwXbNpmOZdYRbRkxW1ITVwwUYBYb7MPWRx+VbpdjZHR6lZMV yWUbT2L3R3PwXmPOF1IOXQU+BUs4jrt1vZ/Z05nQrBAcKWKSGs644leg+p1Z9J6cm3W4 /MqJH1SmWhft3dclJcY5cAnBJY46armkG+2urKx3rJODEkp+TjTO4X0SU9C/R1G8lvhz lmCA== X-Gm-Message-State: APjAAAWQTbZzyj70YC84UENeqQA62LDfZ5EAVd8T3MMG37ObsSlvJmnc lLj638b67WXbDm0ksRTeXyTUszfQ05T7 X-Received: by 2002:ac8:3f55:: with SMTP id w21mr4914843qtk.217.1561667085274; Thu, 27 Jun 2019 13:24:45 -0700 (PDT) Date: Thu, 27 Jun 2019 13:24:16 -0700 In-Reply-To: <20190627202417.33370-1-brianvv@google.com> Message-Id: <20190627202417.33370-6-brianvv@google.com> Mime-Version: 1.0 References: <20190627202417.33370-1-brianvv@google.com> X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog Subject: [RFC PATCH bpf-next v2 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