netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Martin KaFai Lau <kafai@fb.com>
To: "Daniel T. Lee" <danieltimlee@gmail.com>
Cc: "Daniel Borkmann" <daniel@iogearbox.net>,
	"Alexei Starovoitov" <ast@kernel.org>,
	"Andrii Nakryiko" <andrii@kernel.org>, brakmo <brakmo@fb.com>,
	"Jesper Dangaard Brouer" <brouer@redhat.com>,
	"Andrii Nakryiko" <andrii.nakryiko@gmail.com>,
	"Lorenzo Bianconi" <lorenzo@kernel.org>,
	"David Ahern" <dsa@cumulusnetworks.com>,
	"Yonghong Song" <yhs@fb.com>,
	"Toke Høiland-Jørgensen" <toke@redhat.com>,
	"Ira Weiny" <ira.weiny@intel.com>, "Thomas Graf" <tgraf@suug.ch>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"John Fastabend" <john.fastabend@gmail.com>,
	bpf@vger.kernel.org, netdev@vger.kernel.org,
	Xdp <xdp-newbies@vger.kernel.org>
Subject: Re: [PATCH bpf-next v2 1/7] samples: bpf: refactor hbm program with libbpf
Date: Fri, 20 Nov 2020 18:42:53 -0800	[thread overview]
Message-ID: <20201121024227.orabmvy76esenpsf@kafai-mbp.dhcp.thefacebook.com> (raw)
In-Reply-To: <20201121023405.tchtyadco4x45sf3@kafai-mbp.dhcp.thefacebook.com>

On Fri, Nov 20, 2020 at 06:34:05PM -0800, Martin KaFai Lau wrote:
> On Thu, Nov 19, 2020 at 03:06:11PM +0000, Daniel T. Lee wrote:
> [ ... ]
> 
> >  static int run_bpf_prog(char *prog, int cg_id)
> >  {
> > -	int map_fd;
> > -	int rc = 0;
> > +	struct hbm_queue_stats qstats = {0};
> > +	char cg_dir[100], cg_pin_path[100];
> > +	struct bpf_link *link = NULL;
> >  	int key = 0;
> >  	int cg1 = 0;
> > -	int type = BPF_CGROUP_INET_EGRESS;
> > -	char cg_dir[100];
> > -	struct hbm_queue_stats qstats = {0};
> > +	int rc = 0;
> >  
> >  	sprintf(cg_dir, "/hbm%d", cg_id);
> > -	map_fd = prog_load(prog);
> > -	if (map_fd  == -1)
> > -		return 1;
> > +	rc = prog_load(prog);
> > +	if (rc != 0)
> > +		return rc;
> >  
> >  	if (setup_cgroup_environment()) {
> >  		printf("ERROR: setting cgroup environment\n");
> > @@ -190,16 +183,25 @@ static int run_bpf_prog(char *prog, int cg_id)
> >  	qstats.stats = stats_flag ? 1 : 0;
> >  	qstats.loopback = loopback_flag ? 1 : 0;
> >  	qstats.no_cn = no_cn_flag ? 1 : 0;
> > -	if (bpf_map_update_elem(map_fd, &key, &qstats, BPF_ANY)) {
> > +	if (bpf_map_update_elem(queue_stats_fd, &key, &qstats, BPF_ANY)) {
> >  		printf("ERROR: Could not update map element\n");
> >  		goto err;
> >  	}
> >  
> >  	if (!outFlag)
> > -		type = BPF_CGROUP_INET_INGRESS;
> > -	if (bpf_prog_attach(bpfprog_fd, cg1, type, 0)) {
> > -		printf("ERROR: bpf_prog_attach fails!\n");
> > -		log_err("Attaching prog");
> > +		bpf_program__set_expected_attach_type(bpf_prog, BPF_CGROUP_INET_INGRESS);
> > +
> > +	link = bpf_program__attach_cgroup(bpf_prog, cg1);
> > +	if (libbpf_get_error(link)) {
> > +		fprintf(stderr, "ERROR: bpf_program__attach_cgroup failed\n");
> > +		link = NULL;
> Again, this is not needed.  bpf_link__destroy() can
> handle both NULL and error pointer.  Please take a look
> at the bpf_link__destroy() in libbpf.c
> 
> > +		goto err;
> > +	}
> > +
> > +	sprintf(cg_pin_path, "/sys/fs/bpf/hbm%d", cg_id);
> > +	rc = bpf_link__pin(link, cg_pin_path);
> > +	if (rc < 0) {
> > +		printf("ERROR: bpf_link__pin failed: %d\n", rc);
> >  		goto err;
> >  	}
> >  
> > @@ -213,7 +215,7 @@ static int run_bpf_prog(char *prog, int cg_id)
> >  #define DELTA_RATE_CHECK 10000		/* in us */
> >  #define RATE_THRESHOLD 9500000000	/* 9.5 Gbps */
> >  
> > -		bpf_map_lookup_elem(map_fd, &key, &qstats);
> > +		bpf_map_lookup_elem(queue_stats_fd, &key, &qstats);
> >  		if (gettimeofday(&t0, NULL) < 0)
> >  			do_error("gettimeofday failed", true);
> >  		t_last = t0;
> > @@ -242,7 +244,7 @@ static int run_bpf_prog(char *prog, int cg_id)
> >  			fclose(fin);
> >  			printf("  new_eth_tx_bytes:%llu\n",
> >  			       new_eth_tx_bytes);
> > -			bpf_map_lookup_elem(map_fd, &key, &qstats);
> > +			bpf_map_lookup_elem(queue_stats_fd, &key, &qstats);
> >  			new_cg_tx_bytes = qstats.bytes_total;
> >  			delta_bytes = new_eth_tx_bytes - last_eth_tx_bytes;
> >  			last_eth_tx_bytes = new_eth_tx_bytes;
> > @@ -289,14 +291,14 @@ static int run_bpf_prog(char *prog, int cg_id)
> >  					rate = minRate;
> >  				qstats.rate = rate;
> >  			}
> > -			if (bpf_map_update_elem(map_fd, &key, &qstats, BPF_ANY))
> > +			if (bpf_map_update_elem(queue_stats_fd, &key, &qstats, BPF_ANY))
> >  				do_error("update map element fails", false);
> >  		}
> >  	} else {
> >  		sleep(dur);
> >  	}
> >  	// Get stats!
> > -	if (stats_flag && bpf_map_lookup_elem(map_fd, &key, &qstats)) {
> > +	if (stats_flag && bpf_map_lookup_elem(queue_stats_fd, &key, &qstats)) {
> >  		char fname[100];
> >  		FILE *fout;
> >  
> > @@ -398,10 +400,10 @@ static int run_bpf_prog(char *prog, int cg_id)
> >  err:
> >  	rc = 1;
> >  
> > -	if (cg1)
> > -		close(cg1);
> > +	bpf_link__destroy(link);
> > +	close(cg1);
> >  	cleanup_cgroup_environment();
> > -
> > +	bpf_object__close(obj);
> The bpf_* cleanup condition still looks wrong.
> 
> I can understand why it does not want to cleanup_cgroup_environment()
> on the success case because the sh script may want to run test under this
> cgroup.
> 
> However, the bpf_link__destroy(), bpf_object__close(), and
> even close(cg1) should be done in both success and error
> cases.
> 
> The cg1 test still looks wrong also.  The cg1 should
> be init to -1 and then test for "if (cg1 == -1)".
oops.  I meant cg1 != -1 .

  reply	other threads:[~2020-11-21  2:43 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-19 15:06 [PATCH bpf-next v2 0/7] bpf: remove bpf_load loader completely Daniel T. Lee
2020-11-19 15:06 ` [PATCH bpf-next v2 1/7] samples: bpf: refactor hbm program with libbpf Daniel T. Lee
2020-11-21  2:34   ` Martin KaFai Lau
2020-11-21  2:42     ` Martin KaFai Lau [this message]
2020-11-24  8:50     ` Daniel T. Lee
2020-11-19 15:06 ` [PATCH bpf-next v2 2/7] samples: bpf: refactor test_cgrp2_sock2 " Daniel T. Lee
2020-11-19 15:06 ` [PATCH bpf-next v2 3/7] samples: bpf: refactor task_fd_query " Daniel T. Lee
2020-11-19 15:06 ` [PATCH bpf-next v2 4/7] samples: bpf: refactor ibumad " Daniel T. Lee
2020-11-19 15:06 ` [PATCH bpf-next v2 5/7] samples: bpf: refactor test_overhead " Daniel T. Lee
2020-11-19 15:06 ` [PATCH bpf-next v2 6/7] samples: bpf: fix lwt_len_hist reusing previous BPF map Daniel T. Lee
2020-11-19 15:06 ` [PATCH bpf-next v2 7/7] samples: bpf: remove bpf_load loader completely Daniel T. Lee

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=20201121024227.orabmvy76esenpsf@kafai-mbp.dhcp.thefacebook.com \
    --to=kafai@fb.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=brakmo@fb.com \
    --cc=brouer@redhat.com \
    --cc=daniel@iogearbox.net \
    --cc=danieltimlee@gmail.com \
    --cc=dsa@cumulusnetworks.com \
    --cc=ira.weiny@intel.com \
    --cc=john.fastabend@gmail.com \
    --cc=kuba@kernel.org \
    --cc=lorenzo@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=tgraf@suug.ch \
    --cc=toke@redhat.com \
    --cc=xdp-newbies@vger.kernel.org \
    --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).