Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp9802088ybi; Wed, 24 Jul 2019 10:11:16 -0700 (PDT) X-Google-Smtp-Source: APXvYqwWhew7qY6LEDlfFc0Ml6smJ6RzeY5kQLNBAPsW5X9YSLvI9sijWp813/CaOrdp93nA3I9V X-Received: by 2002:a62:187:: with SMTP id 129mr12458611pfb.128.1563988276779; Wed, 24 Jul 2019 10:11:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563988276; cv=none; d=google.com; s=arc-20160816; b=FJ6ibMWOdp5TKHDRt+MaDMBW+nU/K4TpRg9v9sYRgY/wXcx+OtTXaHHuH+8F7a1br7 wCigjlACbdMy6ilVLR8Zg0PPyGQgS1UgLPjQkFXpWLiskBp0+B4ILUo52xQO8z6r1Otx CvSHTaB6X5Xpes3CnpIbIBHl0IBvr5BPXdJdw/TstT5w0NhLWqS65eHvuAnclbMVtdiC SdYTR6hzFvUu9nZzI3+xW7nMBvqFDUKBR9kUVcYmX0Eqei6R0Vt2ivYCTWDDNzTLwbDi DMhjHCK8bkzP0nuIm0KHsrhxCNumCTYSCoynakGjDst437DZWt1n7ik0XBGbT6dl3lXa JHyg== 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=1tJN/OG1NU+LcE8k1olHxJVBbmDDdzToejoDtp6IxoE=; b=oy4kgIiGfa4xLp1LTrK+hdiW8qn4rONAGC83i4yfABOYYSa4vnatmdnIIs3sAMEX97 /e2VSEZzxDw1fVexi4QyWKLMT9Dn8AW+YB0P2myS7XUAEU0nT72chdJHzqN4vgAObdDy lVC4JldDJQObA3k4/wrUaZGZPjUhBScYiLSR0Fx9zBBiZ5Ypw6LF8P5HeL+GY2XiO3wa TlOu4slfQzwTQcz2oXWvOHuQt5xZUN+BUwsTR9JL13qaX8ftv1NZgtFKUO2PRxh8oZSc vVWoIdpeW9kgpbj9+m+tdqRNVKDfFwUY5fkD6pOOe+TARPY81Q6jTrpzK8fWM5Cp0koC e80w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ttSizfIU; 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 r72si13482046pjb.33.2019.07.24.10.11.01; Wed, 24 Jul 2019 10:11:16 -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=ttSizfIU; 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 S2387671AbfGXQ6x (ORCPT + 99 others); Wed, 24 Jul 2019 12:58:53 -0400 Received: from mail-pg1-f202.google.com ([209.85.215.202]:45272 "EHLO mail-pg1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727876AbfGXQ6j (ORCPT ); Wed, 24 Jul 2019 12:58:39 -0400 Received: by mail-pg1-f202.google.com with SMTP id n3so18846240pgh.12 for ; Wed, 24 Jul 2019 09:58:39 -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=1tJN/OG1NU+LcE8k1olHxJVBbmDDdzToejoDtp6IxoE=; b=ttSizfIUX852pbuQqkoRYrU31qBiP2arQzJJnPHCO8oLFLDMyA4R8eilHNC1gUbyMc LVaegQTPgggspcFsqbfxXy3tWH5yB8LBw5YDmMqXtTJBYMDZt1doXnY9KyoDv9kwJgcR DmLo4m72uamj1T7sqJxj7DrhkVqZWjAzDRcaexno9w6tfTbzxV2ZIAQZgbZC+SuzTALZ W6qPhnzQa67Xp87AeTIhvus9AQS1PrShJzeh6E7LPcn8yHJwKJ9v08ccpkRsS18zNxlD cmjASiRla7C+k6xKVn/HdqMICDTqqkgZGxY36QcePw9WbXmAom8WrhVD0whs6Y+WGmt3 nwRQ== 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=1tJN/OG1NU+LcE8k1olHxJVBbmDDdzToejoDtp6IxoE=; b=r9MJnG2cMXCKsUN/ozGZLmH3S4Ta9WuWWjsN1V+jmzbGcdodnLrH2Xl2Eig3kkiVKu NtNd4xUzt72Wa9WBrBQSnY7KpozQlXohneNGm4C0MqE5JAliX3/rZOScXD+ntqNbIdEw HMFLgR5Ffk4Xx4Uc8TxoCJaRrhZDg7arHJ67CQrZG+pOQuC4iYSBFcaAc6hZR1t6LUVL F+PjwbtIn12erPeGVNyWIuMOQxqtRPdtmiVaNKdeVWT4Veisg7t4bOL89T8jfcWWyLJm 5UivOsL7PuFMRu+KcXfUJY8+2VntgQT3z1gYX/CaZVcveXDWgnA8Piw+MbiOILH3AhFU ZlVw== X-Gm-Message-State: APjAAAW3yKhl2CoS6E8ajpFWL+cM6d4MfCBdMsu4tJEpJxQwAfqo17lL 8N7zF/hl1lpIrYyDZBBJCB/8lSwuYNfR X-Received: by 2002:a63:7455:: with SMTP id e21mr77221768pgn.439.1563987518569; Wed, 24 Jul 2019 09:58:38 -0700 (PDT) Date: Wed, 24 Jul 2019 09:58:02 -0700 In-Reply-To: <20190724165803.87470-1-brianvv@google.com> Message-Id: <20190724165803.87470-6-brianvv@google.com> Mime-Version: 1.0 References: <20190724165803.87470-1-brianvv@google.com> X-Mailer: git-send-email 2.22.0.657.g960e92d24f-goog Subject: [PATCH bpf-next 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 | 83 ++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/bpf/test_maps.c b/tools/testing/selftests/bpf/test_maps.c index 5443b9bd75ed7..f7ab401399d40 100644 --- a/tools/testing/selftests/bpf/test_maps.c +++ b/tools/testing/selftests/bpf/test_maps.c @@ -309,6 +309,86 @@ static void test_hashmap_walk(unsigned int task, void *data) close(fd); } +static void test_hashmap_dump(void) +{ + int fd, i, max_entries = 5; + uint64_t keys[max_entries], values[max_entries]; + uint64_t key, value, next_key, prev_key; + bool next_key_valid = true; + void *buf, *elem; + 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; + i = 0; + assert(bpf_map_dump(fd, NULL, 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++; + + /* Check that prev_key contains key from last_elem retrieved in previous + * call + */ + prev_key = *((uint64_t *)elem); + assert(bpf_map_dump(fd, &prev_key, buf, &buf_len) == 0 && + buf_len == elem_size*2); + 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++; + assert(prev_key == (*(uint64_t *)elem)); + + /* Continue reading from map and verify buf_len only contains 1 element + * even though buf_len is 2 elem_size and it returns err = 0. + */ + 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]); + + // Verify there's no more entries and err = ENOENT + 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; @@ -1677,6 +1757,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); @@ -1714,11 +1795,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.657.g960e92d24f-goog