From patchwork Sat Dec 1 00:23:05 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Croce X-Patchwork-Id: 1006269 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 436Bmy14TGz9s9G for ; Sat, 1 Dec 2018 11:23:14 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726586AbeLALeT (ORCPT ); Sat, 1 Dec 2018 06:34:19 -0500 Received: from mail-wm1-f67.google.com ([209.85.128.67]:50252 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725867AbeLALeS (ORCPT ); Sat, 1 Dec 2018 06:34:18 -0500 Received: by mail-wm1-f67.google.com with SMTP id 125so595027wmh.0 for ; Fri, 30 Nov 2018 16:23:10 -0800 (PST) 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=5/bHV9VcgwIxAF6RjVffiNnbLV0J0u9xZU4SiuiNI9I=; b=ghJLdyOKBqTJz8WZnU4/Kj3+mrXsV0qDCJ44/dRa6lwREnDE2FTqTwLV2z88HI1E43 n/e9ORi7apqxnIkpVNReNOSHNZxzcG+BZuiP++C3bVIxKVNz/f2X4quWRo+uMIlxsIQ4 3n33l+xrsvntXg7G7WxCDQDfBZP+EnD7kCWJ07rVAJpE8jJ89yDzblqLGp8UaAxjWT/9 Dwa/gRNrOqvryShfihixQiqE97ms0PKmiI97xtIuOPOpgaB59xTftj3f7y8LoEb4kZnt NbUNUM52YIUbNGCHVK27XfyRzymkg8IDy2f14HvJByY3XveGp2YFZuNxdhBhxRobyoXJ JEGw== X-Gm-Message-State: AA+aEWbrc44hpbpvFsenCh6Zw5hNHV3boeJvPcl7GqcoF4ML6e+Is805 B/NaKKyEZKz31jSQVybt3m9U/Lb1fNE= X-Google-Smtp-Source: AFSGD/U6jVJtBQuSNnN+elJTzHAD3JSKWT6vqRigKcmXFW9Y4OTjj20lqa25CmA2zBA5x4EXzjuPRQ== X-Received: by 2002:a1c:be11:: with SMTP id o17mr681166wmf.111.1543623789490; Fri, 30 Nov 2018 16:23:09 -0800 (PST) Received: from raver.teknoraver.net (net-93-66-170-114.cust.vodafonedsl.it. [93.66.170.114]) by smtp.gmail.com with ESMTPSA id t187-v6sm429542wmt.45.2018.11.30.16.23.08 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 30 Nov 2018 16:23:08 -0800 (PST) From: Matteo Croce To: netdev@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Y Song Subject: [PATCH bpf-next v2 1/2] samples: bpf: improve xdp1 example Date: Sat, 1 Dec 2018 01:23:05 +0100 Message-Id: <20181201002306.25834-2-mcroce@redhat.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181201002306.25834-1-mcroce@redhat.com> References: <20181201002306.25834-1-mcroce@redhat.com> MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Store only the total packet count for every protocol, instead of the whole per-cpu array. Use bpf_map_get_next_key() to iterate the map, instead of looking up all the protocols. Signed-off-by: Matteo Croce --- samples/bpf/xdp1_user.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/samples/bpf/xdp1_user.c b/samples/bpf/xdp1_user.c index b02c531510ed..4f3d824fc044 100644 --- a/samples/bpf/xdp1_user.c +++ b/samples/bpf/xdp1_user.c @@ -34,26 +34,24 @@ static void int_exit(int sig) static void poll_stats(int map_fd, int interval) { unsigned int nr_cpus = bpf_num_possible_cpus(); - const unsigned int nr_keys = 256; - __u64 values[nr_cpus], prev[nr_keys][nr_cpus]; - __u32 key; + __u64 values[nr_cpus], prev[UINT8_MAX] = { 0 }; int i; - memset(prev, 0, sizeof(prev)); - while (1) { + __u32 key = UINT32_MAX; + sleep(interval); - for (key = 0; key < nr_keys; key++) { + while (bpf_map_get_next_key(map_fd, &key, &key) != -1) { __u64 sum = 0; assert(bpf_map_lookup_elem(map_fd, &key, values) == 0); for (i = 0; i < nr_cpus; i++) - sum += (values[i] - prev[key][i]); - if (sum) + sum += values[i]; + if (sum > prev[key]) printf("proto %u: %10llu pkt/s\n", - key, sum / interval); - memcpy(prev[key], values, sizeof(values)); + key, (sum - prev[key]) / interval); + prev[key] = sum; } } } From patchwork Sat Dec 1 00:23:06 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Croce X-Patchwork-Id: 1006270 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 436Bn00kCtz9s8r for ; Sat, 1 Dec 2018 11:23:16 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726600AbeLALeV (ORCPT ); Sat, 1 Dec 2018 06:34:21 -0500 Received: from mail-wm1-f66.google.com ([209.85.128.66]:50252 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726454AbeLALeU (ORCPT ); Sat, 1 Dec 2018 06:34:20 -0500 Received: by mail-wm1-f66.google.com with SMTP id 125so595066wmh.0 for ; Fri, 30 Nov 2018 16:23:12 -0800 (PST) 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=sCNeK13HVH0BD10eMzSj1hgncJwl1YEnmk5CFsYdS+E=; b=UsfYTrNT6Fv9JHKgsM2+Ms6sh24lPoskjIayAj6zmnRytWaNPIAdUHyNueUZC9Roza SgNnLbalY6mty6y/kcAqkGHkjo3abjrReugsSq4WXpJintdLUCWor7nNKVFNQQp4whre 0X+0cLuiphwy6WB2G96t6wKfLvbk30rq41genAa7SgAq7LSoM0zlIPgVxbfParLiKcoZ FD2GZkp5WRU5PCXamhJIWBVTpNq6+xOR6nIU8TydWOpsgUgGUDNtZmhcsjGlEvaSUR7m PCkIKg7It/U6/9sY0d0GQfD9KHIFOy/Tiw16k4piS2hia0wk9pkUqmJqOtxvTfInuLqa ZNsw== X-Gm-Message-State: AA+aEWbhU1DSCpsHJmhnlamYkPfF/HTYFlP+tXINB8+DljmWU3qbIbjX CeozAf+KO8cxqgDQzPjQemlJ+MnRshc= X-Google-Smtp-Source: AFSGD/XZzGfJdO1Awkqyel+3UHsFo6YrV7qf5vD8KHfegKTEpD7+AMe5HKJh2ksvtouXWh1qlnghvA== X-Received: by 2002:a1c:48c2:: with SMTP id v185-v6mr736458wma.1.1543623790903; Fri, 30 Nov 2018 16:23:10 -0800 (PST) Received: from raver.teknoraver.net (net-93-66-170-114.cust.vodafonedsl.it. [93.66.170.114]) by smtp.gmail.com with ESMTPSA id t187-v6sm429542wmt.45.2018.11.30.16.23.09 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 30 Nov 2018 16:23:09 -0800 (PST) From: Matteo Croce To: netdev@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Y Song Subject: [PATCH bpf-next v2 2/2] samples: bpf: get ifindex from ifname Date: Sat, 1 Dec 2018 01:23:06 +0100 Message-Id: <20181201002306.25834-3-mcroce@redhat.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181201002306.25834-2-mcroce@redhat.com> References: <20181201002306.25834-1-mcroce@redhat.com> <20181201002306.25834-2-mcroce@redhat.com> MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Find the ifindex with if_nametoindex() instead of requiring the numeric ifindex. Signed-off-by: Matteo Croce --- v2: use if_nametoindex() instead of ioctl() samples/bpf/xdp1_user.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/samples/bpf/xdp1_user.c b/samples/bpf/xdp1_user.c index 4f3d824fc044..0a197f86ac43 100644 --- a/samples/bpf/xdp1_user.c +++ b/samples/bpf/xdp1_user.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "bpf_util.h" #include "bpf/bpf.h" @@ -59,7 +60,7 @@ static void poll_stats(int map_fd, int interval) static void usage(const char *prog) { fprintf(stderr, - "usage: %s [OPTS] IFINDEX\n\n" + "usage: %s [OPTS] IFACE\n\n" "OPTS:\n" " -S use skb-mode\n" " -N enforce native mode\n", @@ -102,7 +103,11 @@ int main(int argc, char **argv) return 1; } - ifindex = strtoul(argv[optind], NULL, 0); + ifindex = if_nametoindex(argv[1]); + if (!ifindex) { + perror("if_nametoindex"); + return 1; + } snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); prog_load_attr.file = filename;