Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp207256rwb; Thu, 27 Jul 2023 11:26:39 -0700 (PDT) X-Google-Smtp-Source: APBJJlGtsM/dZhfhTg0PvLFoDJqeHXLjeHPT0nA3OeOV6rugsedKPHk0AYiLv7cYAINrnRGVen2I X-Received: by 2002:a17:90a:5502:b0:262:e84f:ad80 with SMTP id b2-20020a17090a550200b00262e84fad80mr147937pji.9.1690482399161; Thu, 27 Jul 2023 11:26:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690482399; cv=none; d=google.com; s=arc-20160816; b=Y1wQgxH8aSDpY15Ua7cF1nYeJ1oGSsMPKRMGkGbADR1U/0aJ4zUq7lz+KB0sHEcLOY NTf1yk2Q5PRfygXy9YokL53E0GjdUx4YAv43fDaaBtfMmkrejSelPo+k2ZBtqxZoyeIH 6G3zDJiuXY6euJpsNAEMAHkBueJp6tfMMQVrahZ++gfR3hC+CPRG1RXNq4xOMqwUUxWx WftfgbsO3At2LXDRLkZYKnPseku796twe8sou8tPHPuym1AbcUDt/cvONvmBMdjUdYXq sxL5CMkP32/gHU5vHkSm7FP4DU1wiUDR0zX8sF0mTMwnIAmw3EMiMssAF9b2GtYTcSwX 50yQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:mime-version:date :dkim-signature:message-id; bh=5UN2gCovcnQF/n0WdaSNMF0rqbN7wNxdtIMcf2HnumI=; fh=d9KULdRd9j90GgNJoxjwNc96VPwHy1kXorWymuiWzFs=; b=m2PlWEvZQxBdxyOI5Y3rtQmqT7aFpFjoMpk6XrxlYwl9ur1O1C8uded1fWWSNKUzhX flotSwp5z+ZXJGoK2FFzidmeQh/lBrB7UgCplNu4f7iNGNadzY1FBb++6XtRIo90GlIB ZCV1r0zyB6YWcimCgSQxWk+YFEFbyqU1XSg5RXvLKKaRfdjCCxML9oLvk76GHofBSM1x wdtjzHxM9bnVWxYVxcgP2pVuzcGSFFJ33ox9PJwKxQwvXn1ws4gId8p8uQBu24597fRf iTOm6TSf66RO9r2IyFPI2GXb6NsUoc01gAg+CILOgiXxEQDV7nqHV+ITIOjYazWhphVx wnMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=TyFpCkm7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y12-20020a1709029b8c00b001bbc40ac12dsi1576346plp.132.2023.07.27.11.26.25; Thu, 27 Jul 2023 11:26:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=TyFpCkm7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229626AbjG0SDI (ORCPT + 99 others); Thu, 27 Jul 2023 14:03:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229521AbjG0SDH (ORCPT ); Thu, 27 Jul 2023 14:03:07 -0400 Received: from out-74.mta1.migadu.com (out-74.mta1.migadu.com [IPv6:2001:41d0:203:375::4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC8BF2D5B for ; Thu, 27 Jul 2023 11:03:05 -0700 (PDT) Message-ID: <78844509-f01f-20cd-4719-49d7480d7aee@linux.dev> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1690480983; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5UN2gCovcnQF/n0WdaSNMF0rqbN7wNxdtIMcf2HnumI=; b=TyFpCkm7W6ezQm0abgCgNHNyuCaRr+aNeqQPeCuPCYKfDTNODTmIr3ZgPp1FWWux6RWWxC gdCwtJ9lSiaBCv39rCFCyUAMunEAT5jTNf0ZjEPNSvy/hWQAc64jwJZ4epGvnxjuu5chPF b8uyZ7mv6P4NJjjrCHjHI4vaPZuYbn4= Date: Thu, 27 Jul 2023 11:02:57 -0700 MIME-Version: 1.0 Subject: Re: [PATCH] samples/bpf: Update sockex2: get the expected output results Content-Language: en-US To: George Guo Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, masahiroy@kernel.org, ndesaulniers@google.com, nathan@kernel.org, nicolas@fjasle.eu References: <20230726070955.178288-1-guodongtai@kylinos.cn> X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Martin KaFai Lau In-Reply-To: <20230726070955.178288-1-guodongtai@kylinos.cn> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 7/26/23 12:09 AM, George Guo wrote: > Running "ping -4 -c5 localhost" only shows 4 times prints not 5: > > $ sudo ./samples/bpf/sockex2 > ip 127.0.0.1 bytes 392 packets 4 > ip 127.0.0.1 bytes 784 packets 8 > ip 127.0.0.1 bytes 1176 packets 12 > ip 127.0.0.1 bytes 1568 packets 16 > > debug it with num prints: > $ sudo ./samples/bpf/sockex2 > num = 1: ip 127.0.0.1 bytes 392 packets 4 > num = 2: ip 127.0.0.1 bytes 784 packets 8 > num = 3: ip 127.0.0.1 bytes 1176 packets 12 > num = 4: ip 127.0.0.1 bytes 1568 packets 16 > > The reason is that we check it faster, just put sleep(1) before check > while(bpf_map_get_next_key(map_fd, &key, &next_key) == 0). > Now we get the expected results: > > $ sudo ./samples/bpf/sockex2 > num = 0: ip 127.0.0.1 bytes 392 packets 4 > num = 1: ip 127.0.0.1 bytes 784 packets 8 > num = 2: ip 127.0.0.1 bytes 1176 packets 12 > num = 3: ip 127.0.0.1 bytes 1568 packets 16 > num = 4: ip 127.0.0.1 bytes 1960 packets 20 > > Signed-off-by: George Guo > --- > samples/bpf/sockex2_user.c | 13 +++++++------ > 1 file changed, 7 insertions(+), 6 deletions(-) > > diff --git a/samples/bpf/sockex2_user.c b/samples/bpf/sockex2_user.c > index 2c18471336f0..84bf1ab77649 100644 > --- a/samples/bpf/sockex2_user.c > +++ b/samples/bpf/sockex2_user.c > @@ -18,8 +18,8 @@ int main(int ac, char **argv) > struct bpf_program *prog; > struct bpf_object *obj; > int map_fd, prog_fd; > - char filename[256]; > - int i, sock, err; > + char filename[256], command[64]; > + int i, sock, err, num = 5; > FILE *f; > > snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); > @@ -42,21 +42,22 @@ int main(int ac, char **argv) > assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd, > sizeof(prog_fd)) == 0); > > - f = popen("ping -4 -c5 localhost", "r"); > + snprintf(command, sizeof(command), "ping -4 -c%d localhost", num); > + f = popen(command, "r"); > (void) f; > > - for (i = 0; i < 5; i++) { > + for (i = 0; i < num; i++) { > int key = 0, next_key; > struct pair value; > > + sleep(1); > while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0) { > bpf_map_lookup_elem(map_fd, &next_key, &value); > - printf("ip %s bytes %lld packets %lld\n", > + printf("num = %d: ip %s bytes %lld packets %lld\n", i, > inet_ntoa((struct in_addr){htonl(next_key)}), > value.bytes, value.packets); > key = next_key; > } > - sleep(1); Moving sleep around is paper wrapping it. e.g. what if the first ping did start later than 1s? Please address it properly. tbf, as an example instead of regression test, displaying fewer line output is fine. > } > return 0; > }