From patchwork Wed May 31 01:45:08 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gao Zhenyu X-Patchwork-Id: 768851 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3wctb440Jnz9s89 for ; Wed, 31 May 2017 11:45:20 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="O870QAws"; dkim-atps=neutral Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id 7820DB5F; Wed, 31 May 2017 01:45:18 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 2DF48B5E for ; Wed, 31 May 2017 01:45:17 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.7.6 Received: from mail-pf0-f194.google.com (mail-pf0-f194.google.com [209.85.192.194]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id DFD75E2 for ; Wed, 31 May 2017 01:45:16 +0000 (UTC) Received: by mail-pf0-f194.google.com with SMTP id f27so634774pfe.0 for ; Tue, 30 May 2017 18:45:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=1ciAoM7bXWKDqhmphr8gAi2YwjexDnYaWRX1WL+I+w8=; b=O870QAws2kSlzvh+6e8QIN0A28fC5hnMiTiNNiPbWDH3vgX0cYdX0Je0NWY5YLjUw9 0yj3xw+SDXumh4lQY1ytzl5CvRtyyPc0d2OypJfLreSCOLOYI0Eb61u8xT2H/S3s1tT4 i6pyAgmIuAcO71+kjVsjcrHUYNtYsxbWwjCYw79ipoMoTC96UWuhennTwVj2NMNvb7T9 l/TJ9EUuAoIjabnq63jrX7BJGI4S6kQeFHXr0BV+Sk6NGIztSLoS3FVtjS3jwR06ilRd VoeQEs2h86SW6Ws7b2532IBK/rH2Wz5hF5Eks+YZzRl2UBtaJKT8SzfySq/W/YNOC1P5 tHDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=1ciAoM7bXWKDqhmphr8gAi2YwjexDnYaWRX1WL+I+w8=; b=j9xp4gZujXfjbp/d6SuihW5NrzVZhuaSY2g0A/ohJ8trmUmij+I97V2ca+DZRkX76p 1xVgIjyyO1iZVglEUM/KtSnVAO/wpcGndHHYQC7vUrNSKRjRBP+AHuxo1wZ+OqKx/CoD VvdLnddgy+RFzcy+d0S19McMezw23WjLZ2HJkWgAmC+mK+BoYcuvHoloKSiWcfnPs0EY OMR7KUrXP68K3lQJHlDnyRqOO6a/tq+tCzyO5h+dcr3P5mD3wcVpq4Ifp2IgFgfLGbY5 F7XGY0AE1f46zLggAREhtPcEuMZ5b6THNBPhMLHO2dWshhkfY9jd4xuR9W8H9nLCu0/L +Mqw== X-Gm-Message-State: AODbwcC1Wl0lpeV3t3co9lKsxEBAUVkMNGQsA6ypK7RtYJbpS1W2l5YV jdo/ZuexYg+oXg== X-Received: by 10.99.176.9 with SMTP id h9mr29553065pgf.160.1496195116555; Tue, 30 May 2017 18:45:16 -0700 (PDT) Received: from vultr.guest ([45.77.33.88]) by smtp.gmail.com with ESMTPSA id t66sm24416515pfe.134.2017.05.30.18.45.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 30 May 2017 18:45:16 -0700 (PDT) From: Zhenyu Gao To: blp@nicira.com, u9012063@gmail.com, dev@openvswitch.org Date: Wed, 31 May 2017 01:45:08 +0000 Message-Id: <20170531014508.9061-1-sysugaozhenyu@gmail.com> X-Mailer: git-send-email 2.11.0 X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM autolearn=no version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Subject: [ovs-dev] [PATCH v2] netdev-linux: Refactoring the netdev_linux_send in forwarding batch packets X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org We don't need to initialize sock,msg and sll before calling sendmsg each time. Just initialize them before the loop, it can reduce cpu cycles. Signed-off-by: Zhenyu Gao --- lib/netdev-linux.c | 62 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/lib/netdev-linux.c b/lib/netdev-linux.c index 3ad3d45..e5ad6ae 100644 --- a/lib/netdev-linux.c +++ b/lib/netdev-linux.c @@ -1194,6 +1194,37 @@ netdev_linux_send(struct netdev *netdev_, int qid OVS_UNUSED, { int i; int error = 0; + int ifindex; + int sock = 0; + struct sockaddr_ll sll; + struct msghdr msg; + + if (!is_tap_netdev(netdev_)) { + sock = af_packet_sock(); + if (sock < 0) { + error = -sock; + goto free_batch; + } + + ifindex = netdev_get_ifindex(netdev_); + if (ifindex < 0) { + error = -ifindex; + goto free_batch; + } + + /* We don't bother setting most fields in sockaddr_ll because the + * kernel ignores them for SOCK_RAW. */ + memset(&sll, 0, sizeof sll); + sll.sll_family = AF_PACKET; + sll.sll_ifindex = ifindex; + + msg.msg_name = &sll; + msg.msg_namelen = sizeof sll; + msg.msg_iovlen = 1; + msg.msg_control = NULL; + msg.msg_controllen = 0; + msg.msg_flags = 0; + } /* 'i' is incremented only if there's no error */ for (i = 0; i < batch->count;) { @@ -1206,38 +1237,12 @@ netdev_linux_send(struct netdev *netdev_, int qid OVS_UNUSED, if (!is_tap_netdev(netdev_)) { /* Use our AF_PACKET socket to send to this device. */ - struct sockaddr_ll sll; - struct msghdr msg; struct iovec iov; - int ifindex; - int sock; - - sock = af_packet_sock(); - if (sock < 0) { - return -sock; - } - - ifindex = netdev_get_ifindex(netdev_); - if (ifindex < 0) { - return -ifindex; - } - - /* We don't bother setting most fields in sockaddr_ll because the - * kernel ignores them for SOCK_RAW. */ - memset(&sll, 0, sizeof sll); - sll.sll_family = AF_PACKET; - sll.sll_ifindex = ifindex; iov.iov_base = CONST_CAST(void *, data); iov.iov_len = size; - msg.msg_name = &sll; - msg.msg_namelen = sizeof sll; msg.msg_iov = &iov; - msg.msg_iovlen = 1; - msg.msg_control = NULL; - msg.msg_controllen = 0; - msg.msg_flags = 0; retval = sendmsg(sock, &msg, 0); } else { @@ -1278,13 +1283,14 @@ netdev_linux_send(struct netdev *netdev_, int qid OVS_UNUSED, i++; } - dp_packet_delete_batch(batch, may_steal); - if (error && error != EAGAIN) { VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s", netdev_get_name(netdev_), ovs_strerror(error)); } +free_batch: + dp_packet_delete_batch(batch, may_steal); + return error; }