Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp2870465rdb; Mon, 5 Feb 2024 23:24:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IE28FELI9tqblk+SUnn26NgpV81woz4eymG/l7Gh7UwFGJQDJWaeu7V/4VNbULK3dFp3Chl X-Received: by 2002:ac2:593a:0:b0:511:4edb:5acb with SMTP id v26-20020ac2593a000000b005114edb5acbmr1195438lfi.64.1707204264879; Mon, 05 Feb 2024 23:24:24 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCVK2HmjbIrXqGpDTIv0TXpwwdodzahKIQ+AFI1Fu0RqMzCT05i3XPKVxZCVVO2YFISTKD6wXqcUdVuQdd/j95lrgiziuId9jcLXehrP9A== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id p15-20020a056402074f00b0055f3e6fad98si838352edy.76.2024.02.05.23.24.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 23:24:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-54414-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=fail (body hash mismatch); spf=pass (google.com: domain of linux-kernel+bounces-54414-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54414-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 4F7FD1F23F12 for ; Tue, 6 Feb 2024 07:24:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DD60C1292E2; Tue, 6 Feb 2024 07:24:10 +0000 (UTC) Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BA4A2128810; Tue, 6 Feb 2024 07:24:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.190 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707204249; cv=none; b=utVAZW3jCGiYCM+JeDO4Xvn56rKelRcOmbZtQqrI5kTmo6TM+hEOwYinxBluGLGqn+o4jVTBFuHY4HeORiC/Eb1aidLwZDSVA9DZ8epaC9Cs/HGkLNHkPnCHXsnNzQzCb1V0lm7Xo2lw2BMbiotUUX2G/xmdztLa6qx5jpCU1mo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707204249; c=relaxed/simple; bh=iiVDTBtOx/9NOmmUzGulxkNRXQLjiCBuy+JJbqYZsAc=; h=Subject:To:CC:References:From:Message-ID:Date:MIME-Version: In-Reply-To:Content-Type; b=KHiXN7hZLyoMr/wCsgfRUpfSKw9aDn90kDpDDI9qLsWaMdvLUQ6WcPdqTElthLGcgiDaqRxOrn8A5lL2gmyCIUg+wmcPgchNj/mkSWhYZptMzR1pLklEz0MMh7hY0oAENPZeOLFH3ih8v9IVvwSz4lbsoWjmIU8srJkJ6Unwl3c= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.190 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.88.214]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4TTZTW0XDbz1xnMQ; Tue, 6 Feb 2024 15:22:55 +0800 (CST) Received: from dggpemm500005.china.huawei.com (unknown [7.185.36.74]) by mail.maildlp.com (Postfix) with ESMTPS id 5ECB31A016B; Tue, 6 Feb 2024 15:24:01 +0800 (CST) Received: from [10.69.30.204] (10.69.30.204) by dggpemm500005.china.huawei.com (7.185.36.74) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Tue, 6 Feb 2024 15:23:58 +0800 Subject: Re: [PATCH net-next v5 5/5] tools: virtio: introduce vhost_net_test To: Jason Wang CC: , , , , , "Michael S. Tsirkin" , Xuan Zhuo , References: <20240205124506.57670-1-linyunsheng@huawei.com> <20240205124506.57670-6-linyunsheng@huawei.com> From: Yunsheng Lin Message-ID: <7416cf5f-b6dc-285f-6acc-f437dcb74a42@huawei.com> Date: Tue, 6 Feb 2024 15:23:57 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpemm500005.china.huawei.com (7.185.36.74) On 2024/2/6 11:08, Jason Wang wrote: .. >> + >> +static void wait_for_interrupt(struct vq_info *vq) >> +{ >> + unsigned long long val; >> + >> + poll(&vq->fds, 1, -1); > > It's not good to wait indefinitely. How about a timeout value of 100ms as below? poll(&vq->fds, 1, 100); > >> + >> + if (vq->fds.revents & POLLIN) >> + read(vq->fds.fd, &val, sizeof(val)); >> +} >> + >> +static void verify_res_buf(char *res_buf) >> +{ >> + int i; >> + >> + for (i = ETHER_HDR_LEN; i < TEST_BUF_LEN; i++) >> + assert(res_buf[i] == (char)i); >> +} >> + >> +static void run_tx_test(struct vdev_info *dev, struct vq_info *vq, >> + bool delayed, int bufs) >> +{ >> + long long spurious = 0; >> + struct scatterlist sl; >> + unsigned int len; >> + int r; >> + >> + for (;;) { >> + long started_before = vq->started; >> + long completed_before = vq->completed; >> + >> + virtqueue_disable_cb(vq->vq); >> + do { >> + while (vq->started < bufs && >> + (vq->started - vq->completed) < 1) { >> + sg_init_one(&sl, dev->test_buf, HDR_LEN + TEST_BUF_LEN); >> + r = virtqueue_add_outbuf(vq->vq, &sl, 1, >> + dev->test_buf + vq->started, >> + GFP_ATOMIC); >> + if (unlikely(r != 0)) >> + break; >> + >> + ++vq->started; > > If we never decrease started/completed shouldn't we use unsigned here? > (as well as completed) > > Otherwise we may get unexpected results for vq->started as well as > vq->completed. We have "vq->started < bufs" checking before the increasing as above, and there is 'assert(nbufs > 0)' when getting optarg in main(), which means we never allow started/completed to be greater than nbufs as my understanding. > >> + >> + if (unlikely(!virtqueue_kick(vq->vq))) { >> + r = -1; >> + break; >> + } >> + } >> + >> + if (vq->started >= bufs) >> + r = -1; > > Which condition do we reach here? It is also a copy & paste of virtio_test.c It means we have finished adding the outbuf in virtqueue, and set 'r' to be '-1' so that we can break the inner while loop if there is no result for virtqueue_get_buf() as my understanding. > >> + >> + /* Flush out completed bufs if any */ >> + while (virtqueue_get_buf(vq->vq, &len)) { >> + int n; >> + >> + n = recvfrom(dev->sock, dev->res_buf, TEST_BUF_LEN, 0, NULL, NULL); >> + assert(n == TEST_BUF_LEN); >> + verify_res_buf(dev->res_buf); >> + >> + ++vq->completed; >> + r = 0; >> + } >> + } while (r == 0); >> + >> + if (vq->completed == completed_before && vq->started == started_before) >> + ++spurious; >> + >> + assert(vq->completed <= bufs); >> + assert(vq->started <= bufs); >> + if (vq->completed == bufs) >> + break; >> + >> + if (delayed) { >> + if (virtqueue_enable_cb_delayed(vq->vq)) >> + wait_for_interrupt(vq); >> + } else { >> + if (virtqueue_enable_cb(vq->vq)) >> + wait_for_interrupt(vq); >> + } > > This could be simplified with > > if (delayed) > else > > wait_for_interrupt(vq) I am not sure if I understand the above comment. The wait_for_interrupt() is only called conditionally depending on the returning of virtqueue_enable_cb_delayed() and virtqueue_enable_cb(). > >> + } >> + printf("TX spurious wakeups: 0x%llx started=0x%lx completed=0x%lx\n", >> + spurious, vq->started, vq->completed); >> +} >> + .. >> + >> + /* Flush out completed bufs if any */ >> + while (virtqueue_get_buf(vq->vq, &len)) { >> + struct ether_header *eh; >> + >> + eh = (struct ether_header *)(dev->res_buf + HDR_LEN); >> + >> + /* tun netdev is up and running, ignore the >> + * non-TEST_PTYPE packet. >> + */ >> + if (eh->ether_type != htons(TEST_PTYPE)) { >> + ++vq->completed; >> + r = 0; >> + continue; >> + } >> + >> + assert(len == TEST_BUF_LEN + HDR_LEN); >> + verify_res_buf(dev->res_buf + HDR_LEN); > > Let's simply the logic here: > > if (ether_type == htons()) { > assert() > verify_res_buf() > } > > r = 0; > ++vq->completed; Sure. > > Others look good. Thanks for the reviewing. > > Thanks > > . >