From patchwork Thu Oct 18 20:47:08 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Croce X-Patchwork-Id: 986265 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 42bh1c035Qz9s8F for ; Fri, 19 Oct 2018 07:47:16 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727521AbeJSEuB (ORCPT ); Fri, 19 Oct 2018 00:50:01 -0400 Received: from mail-wr1-f66.google.com ([209.85.221.66]:36624 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727170AbeJSEuB (ORCPT ); Fri, 19 Oct 2018 00:50:01 -0400 Received: by mail-wr1-f66.google.com with SMTP id y16so35157964wrw.3 for ; Thu, 18 Oct 2018 13:47:13 -0700 (PDT) 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=mgveOdJNQcOTrBMgOzF+LUpQ5J4xdZ4vSl4a3f2xJwHT6bVBMGFluxxWR2Nr4MU5tU 7r0dVvVWmr4tNE9ZR7UGiEC8v5cIHVzUbI31IZZuoI+FnLWNibD9lR8jWSssa42eZOcR FL+dAu33YsezbNh8ymaXTxPbONIj3BHTrD1iTce0eV5VCMi+EPbUEVkxqgxtbCacxRMk ZdS6KqpYKy2r4QOT7/ZNwQpiQ3GtljdLssmT1q79LLN86aPH80QN8+c4NdXDxzQLWgR6 BKf1kbxwNdm5nhyPE6dRzPFXa9QZ9oBkDGMDW+v6HRa7l590nLe8czLZKyMiG4N4fHhs BCvg== X-Gm-Message-State: ABuFfoj5KL7+h1yb+YeJGa6hYKpA9PA+fiuUnFLtHvS3bltOQyfNF5m0 CmIpUd2yin21t0UR9/xukL5L5JyJdyY= X-Google-Smtp-Source: ACcGV63S98UsdwCOQJK/ub/evKaR/tdAX9LCrFpyGth9cwnvZBzNgSemQCVW0EgpO0AENFlv7+OXNg== X-Received: by 2002:a5d:4306:: with SMTP id h6-v6mr32475582wrq.189.1539895632590; Thu, 18 Oct 2018 13:47:12 -0700 (PDT) Received: from raver.teknoraver.net (net-188-216-3-175.cust.vodafonedsl.it. [188.216.3.175]) by smtp.gmail.com with ESMTPSA id o81-v6sm681155wmo.38.2018.10.18.13.47.10 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 18 Oct 2018 13:47:11 -0700 (PDT) From: Matteo Croce To: netdev@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann Subject: [PATCH bpf-next 1/2] samples: bpf: improve xdp1 example Date: Thu, 18 Oct 2018 22:47:08 +0200 Message-Id: <20181018204709.29547-2-mcroce@redhat.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181018204709.29547-1-mcroce@redhat.com> References: <20181018204709.29547-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 Acked-by: Yonghong Song --- 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 Thu Oct 18 20:47:09 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Croce X-Patchwork-Id: 986266 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 42bh1d0sKsz9sCW for ; Fri, 19 Oct 2018 07:47:17 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727531AbeJSEuC (ORCPT ); Fri, 19 Oct 2018 00:50:02 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:43632 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727170AbeJSEuC (ORCPT ); Fri, 19 Oct 2018 00:50:02 -0400 Received: by mail-wr1-f68.google.com with SMTP id n1-v6so35139068wrt.10 for ; Thu, 18 Oct 2018 13:47:15 -0700 (PDT) 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=FWT2o4JeCxK/y6H/lIM/UkPVdRDaiRRIatf4BsGUmbY=; b=PQqURVXViYIJmjhZez0j0ueEGknghtT+2ITSdvVXR4Q3lUHYE1MX9BEzcndIF8q6Cu +wav8dsEEXrdb5Mycq7ymndB5Qd93HQiwmVYffvtKeOLhYFE7p5TWkDUm+IFqdQ2f4DZ 5ud7m/ONX80B5fEhcaUq3qyrUKXmhtR+e4NK9dYohXg6ylc4I2egtnnvKclIeaPZsxyL nGCl7BPrMd3vVBnD9B+sDDLab64opJaLjkc6zu7XyQe3wGN402JmDgeP06j6hNx+CHNI iSNfaWJWEdj2l4rmphgjanDeiRT9M4zj7AJrGqmcGjg8ALUAIQo749A5AD6oYWrod3wp HBvw== X-Gm-Message-State: ABuFfohRv20+tNQXU9lx43lMf3Vlj+6TraAY5RKkYkYMRlcY5ULp7eSZ Q0BcVirfIbHVs8Ybkuosnj75+YHjp0U= X-Google-Smtp-Source: ACcGV63ofnPehybBUEE/6gx6FscoGkkoMF5F0f3VMJOLbMqPtfPu2nMW98NWL5LdkLc4KgReQGlaYA== X-Received: by 2002:a5d:49c4:: with SMTP id t4-v6mr30168965wrs.116.1539895634043; Thu, 18 Oct 2018 13:47:14 -0700 (PDT) Received: from raver.teknoraver.net (net-188-216-3-175.cust.vodafonedsl.it. [188.216.3.175]) by smtp.gmail.com with ESMTPSA id o81-v6sm681155wmo.38.2018.10.18.13.47.12 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 18 Oct 2018 13:47:12 -0700 (PDT) From: Matteo Croce To: netdev@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann Subject: [PATCH bpf-next 2/2] samples: bpf: get ifindex from ifname Date: Thu, 18 Oct 2018 22:47:09 +0200 Message-Id: <20181018204709.29547-3-mcroce@redhat.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181018204709.29547-2-mcroce@redhat.com> References: <20181018204709.29547-1-mcroce@redhat.com> <20181018204709.29547-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 via ioctl(SIOCGIFINDEX) instead of requiring the numeric ifindex. Signed-off-by: Matteo Croce Acked-by: John Fastabend --- samples/bpf/xdp1_user.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/samples/bpf/xdp1_user.c b/samples/bpf/xdp1_user.c index 4f3d824fc044..a1d0c5dcee9c 100644 --- a/samples/bpf/xdp1_user.c +++ b/samples/bpf/xdp1_user.c @@ -15,6 +15,9 @@ #include #include #include +#include +#include +#include #include "bpf_util.h" #include "bpf/bpf.h" @@ -59,7 +62,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", @@ -74,9 +77,11 @@ int main(int argc, char **argv) }; const char *optstr = "SN"; int prog_fd, map_fd, opt; + struct ifreq ifr = { 0 }; struct bpf_object *obj; struct bpf_map *map; char filename[256]; + int sock; while ((opt = getopt(argc, argv, optstr)) != -1) { switch (opt) { @@ -102,7 +107,24 @@ int main(int argc, char **argv) return 1; } - ifindex = strtoul(argv[optind], NULL, 0); + sock = socket(AF_UNIX, SOCK_DGRAM, 0); + if (sock == -1) { + perror("socket"); + return 1; + } + + if (strlen(argv[optind]) >= IFNAMSIZ) { + printf("invalid ifname '%s'\n", argv[optind]); + return 1; + } + + strcpy(ifr.ifr_name, argv[optind]); + if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0) { + perror("SIOCGIFINDEX"); + return 1; + } + close(sock); + ifindex = ifr.ifr_ifindex; snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); prog_load_attr.file = filename;