netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: bpf@vger.kernel.org
Cc: "Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
	"Alexei Starovoitov" <ast@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"Martin KaFai Lau" <kafai@fb.com>,
	"Song Liu" <songliubraving@fb.com>, "Yonghong Song" <yhs@fb.com>,
	"John Fastabend" <john.fastabend@gmail.com>,
	"KP Singh" <kpsingh@kernel.org>,
	"David S. Miller" <davem@davemloft.net>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Jesper Dangaard Brouer" <brouer@redhat.com>,
	"Toke Høiland-Jørgensen" <toke@redhat.com>,
	netdev@vger.kernel.org
Subject: [PATCH RFC bpf-next 13/15] samples: bpf: add new options for xdp samples
Date: Sat, 29 May 2021 05:22:48 +0530	[thread overview]
Message-ID: <20210528235250.2635167-14-memxor@gmail.com> (raw)
In-Reply-To: <20210528235250.2635167-1-memxor@gmail.com>

These are -i for polling interval and -v for verbose output by default.
Some of these tools already supported -s, but we now use that to enable
performance impacting success case reporting tracepoint. This is
disabled by default, but can be enabled explicitly by user.

Use separators (-z) is also dropped.

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
 samples/bpf/xdp_monitor_user.c      | 16 ++++---
 samples/bpf/xdp_redirect_cpu_user.c | 41 ++++++++++++-----
 samples/bpf/xdp_redirect_map_user.c | 71 +++++++++++++++++++++++------
 3 files changed, 96 insertions(+), 32 deletions(-)

diff --git a/samples/bpf/xdp_monitor_user.c b/samples/bpf/xdp_monitor_user.c
index 73d6d35f0c65..b37d8f7379ec 100644
--- a/samples/bpf/xdp_monitor_user.c
+++ b/samples/bpf/xdp_monitor_user.c
@@ -38,9 +38,10 @@ struct bpf_object *obj;
 static const struct option long_options[] = {
 	{"help",	no_argument,		NULL, 'h' },
 	{"debug",	no_argument,		NULL, 'D' },
-	{"stats",	no_argument,		NULL, 'S' },
-	{"sec", 	required_argument,	NULL, 's' },
-	{0, 0, NULL,  0 }
+	{"stats",	no_argument,		NULL, 's' },
+	{"interval",	required_argument,	NULL, 'i' },
+	{"verbose",	no_argument,		NULL, 'v' },
+	{}
 };
 
 static void int_exit(int sig)
@@ -121,18 +122,21 @@ int main(int argc, char **argv)
 	int interval = 2;
 
 	/* Parse commands line args */
-	while ((opt = getopt_long(argc, argv, "hDSs:",
+	while ((opt = getopt_long(argc, argv, "hDi:vs",
 				  long_options, &longindex)) != -1) {
 		switch (opt) {
 		case 'D':
 			debug = true;
 			break;
-		case 'S':
+		case 's':
 			errors_only = false;
 			break;
-		case 's':
+		case 'i':
 			interval = atoi(optarg);
 			break;
+		case 'v':
+			sample_log_level ^= LL_DEBUG - 1;
+			break;
 		case 'h':
 		default:
 			usage(argv);
diff --git a/samples/bpf/xdp_redirect_cpu_user.c b/samples/bpf/xdp_redirect_cpu_user.c
index 103ac5c24163..d56b89254cd1 100644
--- a/samples/bpf/xdp_redirect_cpu_user.c
+++ b/samples/bpf/xdp_redirect_cpu_user.c
@@ -42,19 +42,20 @@ static const struct option long_options[] = {
 	{"help",	no_argument,		NULL, 'h' },
 	{"dev",		required_argument,	NULL, 'd' },
 	{"skb-mode",	no_argument,		NULL, 'S' },
-	{"sec",		required_argument,	NULL, 's' },
 	{"progname",	required_argument,	NULL, 'p' },
 	{"qsize",	required_argument,	NULL, 'q' },
 	{"cpu",		required_argument,	NULL, 'c' },
 	{"stress-mode", no_argument,		NULL, 'x' },
-	{"no-separators", no_argument,		NULL, 'z' },
 	{"force",	no_argument,		NULL, 'F' },
 	{"mprog-disable", no_argument,		NULL, 'n' },
 	{"mprog-name",	required_argument,	NULL, 'e' },
 	{"mprog-filename", required_argument,	NULL, 'f' },
 	{"redirect-device", required_argument,	NULL, 'r' },
 	{"redirect-map", required_argument,	NULL, 'm' },
-	{0, 0, NULL,  0 }
+	{"interval", required_argument,		NULL, 'i' },
+	{"verbose", no_argument,		NULL, 'v' },
+	{"stats", no_argument,			NULL, 's' },
+	{}
 };
 
 static void int_exit(int sig)
@@ -196,7 +197,7 @@ static void stress_cpumap(struct bpf_cpumap_val *value)
 	create_cpu_entry(1, value, 0, false);
 }
 
-static void __stats_poll(int interval, bool use_separators, char *prog_name,
+static void __stats_poll(int interval, bool redir_suc, char *prog_name,
 			 char *mprog_name, struct bpf_cpumap_val *value,
 			 bool stress_mode)
 {
@@ -210,8 +211,10 @@ static void __stats_poll(int interval, bool use_separators, char *prog_name,
 	sample_stats_collect(mask, record);
 
 	/* Trick to pretty printf with thousands separators use %' */
-	if (use_separators)
-		setlocale(LC_NUMERIC, "en_US");
+	setlocale(LC_NUMERIC, "en_US");
+
+	if (redir_suc)
+		mask |= SAMPLE_REDIRECT_CNT;
 
 	for (;;) {
 		struct timespec ots, nts;
@@ -298,12 +301,12 @@ int main(int argc, char **argv)
 	struct bpf_prog_info info = {};
 	__u32 info_len = sizeof(info);
 	struct bpf_cpumap_val value;
-	bool use_separators = true;
 	bool stress_mode = false;
 	struct bpf_program *prog;
 	struct bpf_object *obj;
 	int err = EXIT_FAIL;
 	char filename[256];
+	bool redir = false;
 	int added_cpus = 0;
 	int longindex = 0;
 	int interval = 2;
@@ -356,7 +359,7 @@ int main(int argc, char **argv)
 	memset(cpu, 0, n_cpus * sizeof(int));
 
 	/* Parse commands line args */
-	while ((opt = getopt_long(argc, argv, "hSd:s:p:q:c:xzFf:e:r:m:",
+	while ((opt = getopt_long(argc, argv, "hSd:sp:q:c:xi:vFf:e:r:m:",
 				  long_options, &longindex)) != -1) {
 		switch (opt) {
 		case 'd':
@@ -375,6 +378,9 @@ int main(int argc, char **argv)
 			}
 			break;
 		case 's':
+			redir = true;
+			break;
+		case 'i':
 			interval = atoi(optarg);
 			break;
 		case 'S':
@@ -383,9 +389,6 @@ int main(int argc, char **argv)
 		case 'x':
 			stress_mode = true;
 			break;
-		case 'z':
-			use_separators = false;
-			break;
 		case 'p':
 			/* Selecting eBPF prog to load */
 			prog_name = optarg;
@@ -422,6 +425,9 @@ int main(int argc, char **argv)
 		case 'F':
 			xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
 			break;
+		case 'v':
+			sample_log_level ^= LL_DEBUG - 1;
+			break;
 		case 'h':
 		error:
 		default:
@@ -492,7 +498,18 @@ int main(int argc, char **argv)
 	}
 	prog_id = info.id;
 
-	__stats_poll(interval, use_separators, prog_name, mprog_name,
+	if (!redir) {
+		/* The bpf_link[i] depend on the order of
+		 * the functions was defined in _kern.c
+		 */
+		bpf_link__destroy(tp_links[2]);	/* tracepoint/xdp/xdp_redirect */
+		tp_links[2] = NULL;
+
+		bpf_link__destroy(tp_links[3]);	/* tracepoint/xdp/xdp_redirect_map */
+		tp_links[3] = NULL;
+	}
+
+	__stats_poll(interval, redir, prog_name, mprog_name,
 		     &value, stress_mode);
 
 	err = EXIT_OK;
diff --git a/samples/bpf/xdp_redirect_map_user.c b/samples/bpf/xdp_redirect_map_user.c
index ed53dd2cd93a..eb4013fa58cb 100644
--- a/samples/bpf/xdp_redirect_map_user.c
+++ b/samples/bpf/xdp_redirect_map_user.c
@@ -13,6 +13,7 @@
 #include <net/if.h>
 #include <unistd.h>
 #include <libgen.h>
+#include <getopt.h>
 
 #include "bpf_util.h"
 #include <bpf/bpf.h>
@@ -28,6 +29,18 @@ static __u32 dummy_prog_id;
 
 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
 
+static const struct option long_options[] = {
+	{"help",	no_argument,		NULL, 'h' },
+	{"skb-mode",	no_argument,		NULL, 'S' },
+	{"native-mode", no_argument,		NULL, 'N' },
+	{"force",	no_argument,		NULL, 'F' },
+	{"load-egress", no_argument,		NULL, 'X' },
+	{"stats",	no_argument,		NULL, 's' },
+	{"interval",	required_argument,	NULL, 'i' },
+	{"verbose",	no_argument,		NULL, 'v' },
+	{}
+};
+
 static void int_exit(int sig)
 {
 	__u32 curr_prog_id = 0;
@@ -61,16 +74,25 @@ static void int_exit(int sig)
 	sample_exit(EXIT_OK);
 }
 
-static void usage(const char *prog)
+static void usage(char *argv[])
 {
-	fprintf(stderr,
-		"usage: %s [OPTS] <IFNAME|IFINDEX>_IN <IFNAME|IFINDEX>_OUT\n\n"
-		"OPTS:\n"
-		"    -S    use skb-mode\n"
-		"    -N    enforce native mode\n"
-		"    -F    force loading prog\n"
-		"    -X    load xdp program on egress\n",
-		prog);
+	int i;
+
+	printf("\n");
+	printf(" Usage: %s (options-see-below)\n",
+	       argv[0]);
+	printf(" Listing options:\n");
+	for (i = 0; long_options[i].name != 0; i++) {
+		printf(" --%-15s", long_options[i].name);
+		if (long_options[i].flag != NULL)
+			printf(" flag (internal value:%d)",
+			       *long_options[i].flag);
+		else
+			printf("short-option: -%c",
+			       long_options[i].val);
+		printf("\n");
+	}
+	printf("\n");
 }
 
 int main(int argc, char **argv)
@@ -88,13 +110,14 @@ int main(int argc, char **argv)
 	char str[2 * IF_NAMESIZE + 1];
 	__u32 info_len = sizeof(info);
 	char ifname_out[IF_NAMESIZE];
-	const char *optstr = "FSNX";
 	char ifname_in[IF_NAMESIZE];
 	struct bpf_object *obj;
 	int ret, opt, key = 0;
 	char filename[256];
+	int interval = 2;
 
-	while ((opt = getopt(argc, argv, optstr)) != -1) {
+	while ((opt = getopt_long(argc, argv, "FSNXi:vs",
+				  long_options, NULL)) != -1) {
 		switch (opt) {
 		case 'S':
 			xdp_flags |= XDP_FLAGS_SKB_MODE;
@@ -108,8 +131,17 @@ int main(int argc, char **argv)
 		case 'X':
 			xdp_devmap_attached = true;
 			break;
+		case 'i':
+			interval = atoi(optarg);
+			break;
+		case 'v':
+			sample_log_level ^= LL_DEBUG - 1;
+			break;
+		case 's':
+			mask |= SAMPLE_REDIRECT_MAP_CNT;
+			break;
 		default:
-			usage(basename(argv[0]));
+			usage(argv);
 			return 1;
 		}
 	}
@@ -122,7 +154,7 @@ int main(int argc, char **argv)
 	}
 
 	if (argc <= optind + 1) {
-		usage(basename(argv[0]));
+		usage(argv);
 		return 1;
 	}
 
@@ -252,9 +284,20 @@ int main(int argc, char **argv)
 	       ifname_in, ifindex_in, str, ifname_out, ifindex_out,
 	       get_driver_name(ifindex_out) ?: "(err)");
 
+	if ((mask & SAMPLE_REDIRECT_CNT) == 0) {
+		/* The bpf_link[i] depend on the order of
+		 * the functions was defined in _kern.c
+		 */
+		bpf_link__destroy(tp_links[2]);	/* tracepoint/xdp/xdp_redirect */
+		tp_links[2] = NULL;
+
+		bpf_link__destroy(tp_links[3]);	/* tracepoint/xdp/xdp_redirect_map */
+		tp_links[3] = NULL;
+	}
+
 	snprintf(str, sizeof(str), "%s->%s", ifname_in, ifname_out);
 
-	sample_stats_poll(1, mask, str, true);
+	sample_stats_poll(interval, mask, str, true);
 
 	return 0;
 
-- 
2.31.1


  parent reply	other threads:[~2021-05-28 23:54 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-28 23:52 [PATCH RFC bpf-next 00/15] Improve XDP samples usability and output Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` [PATCH RFC bpf-next 01/15] samples: bpf: fix a couple of NULL dereferences Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` [PATCH RFC bpf-next 02/15] samples: bpf: fix a couple of warnings Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` [PATCH RFC bpf-next 03/15] samples: bpf: split out common bpf progs to its own file Kumar Kartikeya Dwivedi
2021-05-30  3:05   ` Andrii Nakryiko
2021-05-28 23:52 ` [PATCH RFC bpf-next 04/15] samples: bpf: refactor generic parts out of xdp_redirect_cpu_user Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` [PATCH RFC bpf-next 05/15] samples: bpf: convert xdp_redirect_map to use xdp_samples Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` [PATCH RFC bpf-next 06/15] samples: bpf: prepare devmap_xmit support in xdp_sample Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` [PATCH RFC bpf-next 07/15] samples: bpf: add extended reporting for xdp redirect error Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` [PATCH RFC bpf-next 08/15] samples: bpf: add per exception reporting for xdp_exception Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` [PATCH RFC bpf-next 09/15] samples: bpf: convert xdp_monitor to use xdp_samples Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` [PATCH RFC bpf-next 10/15] samples: bpf: implement terse output mode and make it default Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` [PATCH RFC bpf-next 11/15] samples: bpf: print summary of session on exit Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` [PATCH RFC bpf-next 12/15] samples: bpf: subtract time spent in collection from polling interval Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` Kumar Kartikeya Dwivedi [this message]
2021-05-28 23:52 ` [PATCH RFC bpf-next 14/15] samples: bpf: add documentation Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` [PATCH RFC bpf-next 15/15] samples: bpf: convert xdp_samples to use raw_tracepoints Kumar Kartikeya Dwivedi
2021-05-30  3:07   ` Andrii Nakryiko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210528235250.2635167-14-memxor@gmail.com \
    --to=memxor@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=brouer@redhat.com \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@kernel.org \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=songliubraving@fb.com \
    --cc=toke@redhat.com \
    --cc=yhs@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).