Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp1839941rwb; Wed, 26 Jul 2023 20:45:48 -0700 (PDT) X-Google-Smtp-Source: APBJJlG3CjhOXY1Zy5ViUC2V5Mvi3cGEr6YgNjalZ9SWdESRVsexO5+nVLhIQa6wNdENP9WOGXUj X-Received: by 2002:a05:6358:4411:b0:135:ae78:56c9 with SMTP id z17-20020a056358441100b00135ae7856c9mr1218126rwc.6.1690429547799; Wed, 26 Jul 2023 20:45:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690429547; cv=none; d=google.com; s=arc-20160816; b=o7gvPWxGjLfRONB4YP1iYi+NTPZyCggWnTkTYae30Pj93HGZ/mpjQNq/j4DS94OAlI a44RHHcU7aRLFc0J3pFlP5Ntk55/M7qbY/FTaYTrkfxp3zYTVI57fIENCfQbBj7HzQwh b8u1OHp/sL5Zyxkp73A1zM0yk+ZAx7HuGIw0Qe0P9uIk/xfORy+SadxdKyyLNazIFKx+ 43PQAiyt58uODrTpc4nGg6r98RlVzbpW/bMpvZrdAQLIVANs9GURi3HzdrKcpIPJlhu2 AML4p3vddgT4gsCw56xaqkHI6NquZvZQH813wsaWBTh7FyDYpPBxksIRVTyi3pMChOEf sQ0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date; bh=BHb8XWm7kd4n7u+akNxuPTdc6gLREGogfcjfy1sXUec=; fh=DDPGvFT9NBaBxzUKCGq/mHqR85Jg5ltP3A+jSwX15n0=; b=eiJXu+wJsODm2Q/3qZyrc7Emts04t7pdwDMcLShPU12lEDbhy64IWFoPCs2BypYNQH jV8LfNkbTlt9+F2NqBPL2uT9KRm6LEdPQKWYWeisminYnEVknD4uX3LG9TOLWvphnI+y 3B4xgbl4gEvCDzrFT0n6iuBmcVaQOkAzp/a5JXKKdF6gP7sWTgDgWMs6vmAfj6b3W+Q8 b/061L6Gt0trFKgYEQN+Qzhd1oFx4pZzB4zPA7tvELj4UnIUw1ZdrEi+BcXOaPAdCX/S cBCuVi83vXAJMRJ38upFJYN9VcUjchW72sFCQnDpYylFbfgOH7gikOa/fZY/NZ4iZ/YH 8ovA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b75-20020a63344e000000b0054fd06a3585si424682pga.158.2023.07.26.20.45.35; Wed, 26 Jul 2023 20:45:47 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230392AbjG0DdL convert rfc822-to-8bit (ORCPT + 99 others); Wed, 26 Jul 2023 23:33:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41570 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229721AbjG0DdK (ORCPT ); Wed, 26 Jul 2023 23:33:10 -0400 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8634DE75; Wed, 26 Jul 2023 20:33:06 -0700 (PDT) X-UUID: 3cca24fa88994d4ba840e9aac1bc8355-20230727 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.28,REQID:f242e993-158b-4ea5-85f9-ef4642d75f6c,IP:25, URL:0,TC:0,Content:0,EDM:0,RT:0,SF:-15,FILE:0,BULK:0,RULE:Release_Ham,ACTI ON:release,TS:10 X-CID-INFO: VERSION:1.1.28,REQID:f242e993-158b-4ea5-85f9-ef4642d75f6c,IP:25,UR L:0,TC:0,Content:0,EDM:0,RT:0,SF:-15,FILE:0,BULK:0,RULE:Release_Ham,ACTION :release,TS:10 X-CID-META: VersionHash:176cd25,CLOUDID:517194a0-0933-4333-8d4f-6c3c53ebd55b,B ulkID:230727052327ADIGKCI6,BulkQuantity:4,Recheck:0,SF:19|44|24|17|102,TC: nil,Content:0,EDM:-3,IP:-2,URL:0,File:nil,Bulk:40,QS:nil,BEC:nil,COL:0,OSI :0,OSA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-CID-FACTOR: TF_CID_SPAM_FSI,TF_CID_SPAM_SNR,TF_CID_SPAM_FAS,TF_CID_SPAM_FSD X-UUID: 3cca24fa88994d4ba840e9aac1bc8355-20230727 X-User: guodongtai@kylinos.cn Received: from ky [(39.156.73.12)] by mailgw (envelope-from ) (Generic MTA) with ESMTP id 455361928; Thu, 27 Jul 2023 11:32:51 +0800 Date: Thu, 27 Jul 2023 11:32:51 +0800 From: To: Nicolas Schier Cc: masahiroy@kernel.org, ndesaulniers@google.com, nathan@kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org Subject: Re: [PATCH] samples/bpf: Update sockex2: get the expected output results Message-ID: <20230727113251.15f538ab@ky> In-Reply-To: References: <20230726070955.178288-1-guodongtai@kylinos.cn> X-Mailer: Claws Mail 3.17.5 (GTK+ 2.24.32; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8BIT X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY 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 Wed, 26 Jul 2023 23:22:49 +0200 Nicolas Schier wrote: > On Wed, Jul 26, 2023 at 03:09:55PM +0800 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 > > --- > > Thanks, sounds reasonable to me (but I haven't checked it). Might > you want to minimize the diff to only contain the move of the sleep > call? > > Kind regards, > Nicolas > > > > 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); > > } > > return 0; > > } > > -- > > 2.34.1 > hi, the diff to only contain the move of the sleep call likes this: diff --git a/samples/bpf/sockex2_user.c b/samples/bpf/sockex2_user.c index 2c18471336f0..82bb38b9cab0 100644 --- a/samples/bpf/sockex2_user.c +++ b/samples/bpf/sockex2_user.c @@ -49,6 +49,7 @@ int main(int ac, char **argv) 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", @@ -56,7 +57,6 @@ int main(int ac, char **argv) value.bytes, value.packets); key = next_key; } - sleep(1); } return 0; }