Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp681576rwb; Wed, 26 Jul 2023 00:39:30 -0700 (PDT) X-Google-Smtp-Source: APBJJlEboPi/sJGasY0sh/8mMc0QhweTw4gE+xHbC278PwcLfVDtxcjCjcEFxr71OOW88J9VtkA/ X-Received: by 2002:a17:902:bf02:b0:1b3:c7c1:8ded with SMTP id bi2-20020a170902bf0200b001b3c7c18dedmr1017218plb.27.1690357169723; Wed, 26 Jul 2023 00:39:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690357169; cv=none; d=google.com; s=arc-20160816; b=s7IHprr02dXb2rKCbUH+Z+jasfoQp1eGbGIUl1zFSSOtJyQmqJZ7S9iA70bdImNEWS y3xX0tuTuYHuB99plUhzJbiugP+S7pVAVadchTxm/qaRN2grXLVV5Sj6c/8av4fR6Br/ AC36blhA1T+4LfP+//qQiGaUWTc/Q9yRpRmPQDp+zi0cHhVrerIQQ/PKJ8DkEkOdSbDZ BpfohG+Pshg8NlASQERjRmX1k87jsVsPIZg5jroY0L4u/rLcUHdiYXkmIjaYIII5XepA JhYmVxK5maHI7e5c4b9SJbyMm411h6HATY4rhdH9cJs/Ic/iq3uUoMBa8B4no8PBDaqM 6GrQ== 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 :message-id:date:subject:cc:to:from; bh=yBaTM3BqCWZN0Kq38w+Bvq4wPn99gMkn96W5kpDYNhg=; fh=LGnUkh4OQA6gbcpk1kIR7eUQNGO+LHn6QG3r0+w+rHs=; b=zLa1yGk1cEw5gU/afP8VgLJoVNYaxv45JZlgifE2aLv4GprIQHchKsQOr3ixHthjeD vEym2/FAsjl960quBE+eO8nZRKb0EydvOZZWzEyR7PS3VyxfEzEfnfBIFoQb6trhiDCr OJ+XUodMFjRf1Qfk74R+Gs4mYE5B2YOY5iA8oXZzJxHxopg7idQPt0ydxCt3wN6M46ue 9TdljnomzK7IKl1oYzlmCwLRcdNgtTnZASh7KlORqs5xylf7sdqW9zVdlVqhKU4VRVKG u6kxf7ks2+9lx8OhtAPDn3giim/dl4Mz1aGjGFaJwGMTJbxZ91hqPSMJ9ky1rF8eldM6 nZiQ== 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 q17-20020a17090311d100b001b8690d7181si13244960plh.550.2023.07.26.00.39.17; Wed, 26 Jul 2023 00:39:29 -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 S231978AbjGZHOK (ORCPT + 99 others); Wed, 26 Jul 2023 03:14:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231841AbjGZHNi (ORCPT ); Wed, 26 Jul 2023 03:13:38 -0400 Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B06CC559E; Wed, 26 Jul 2023 00:09:49 -0700 (PDT) X-UUID: 48d18b7fbb5e4399a646d23469c1a346-20230726 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.28,REQID:308e6581-c474-4014-b1a3-53283994d9aa,IP:15, URL:0,TC:0,Content:0,EDM:0,RT:0,SF:-15,FILE:0,BULK:0,RULE:Release_Ham,ACTI ON:release,TS:0 X-CID-INFO: VERSION:1.1.28,REQID:308e6581-c474-4014-b1a3-53283994d9aa,IP:15,UR L:0,TC:0,Content:0,EDM:0,RT:0,SF:-15,FILE:0,BULK:0,RULE:Release_Ham,ACTION :release,TS:0 X-CID-META: VersionHash:176cd25,CLOUDID:2dd756d2-cd77-4e67-bbfd-aa4eaace762f,B ulkID:230726150927DGBQ9DXH,BulkQuantity:0,Recheck:0,SF:38|24|17|19|44|102, TC:nil,Content:0,EDM:-3,IP:-2,URL:0,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:0 ,OSI:0,OSA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0 X-CID-BVR: 0,NGT X-CID-BAS: 0,NGT,0,_ X-CID-FACTOR: TF_CID_SPAM_FSI,TF_CID_SPAM_SNR,TF_CID_SPAM_FAS,TF_CID_SPAM_FSD X-UUID: 48d18b7fbb5e4399a646d23469c1a346-20230726 X-User: guodongtai@kylinos.cn Received: from localhost.localdomain [(39.156.73.12)] by mailgw (envelope-from ) (Generic MTA) with ESMTP id 586468305; Wed, 26 Jul 2023 15:09:24 +0800 From: George Guo To: masahiroy@kernel.org, ndesaulniers@google.com, nathan@kernel.org, nicolas@fjasle.eu Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH] samples/bpf: Update sockex2: get the expected output results Date: Wed, 26 Jul 2023 15:09:55 +0800 Message-Id: <20230726070955.178288-1-guodongtai@kylinos.cn> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 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 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); } return 0; } -- 2.34.1