From patchwork Wed Nov 15 02:57:55 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Denis V. Lunev\" via" X-Patchwork-Id: 10058717 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 7D4246056E for ; Wed, 15 Nov 2017 02:58:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6ED0E29BD2 for ; Wed, 15 Nov 2017 02:58:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 634B829C6E; Wed, 15 Nov 2017 02:58:48 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id CAF0429BD2 for ; Wed, 15 Nov 2017 02:58:47 +0000 (UTC) Received: from localhost ([::1]:34468 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eEnuY-0004Wt-DE for patchwork-qemu-devel@patchwork.kernel.org; Tue, 14 Nov 2017 21:58:46 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37708) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eEntw-0004Wd-Te for qemu-devel@nongnu.org; Tue, 14 Nov 2017 21:58:09 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eEntr-0006yF-U6 for qemu-devel@nongnu.org; Tue, 14 Nov 2017 21:58:08 -0500 Received: from mail-pf0-x242.google.com ([2607:f8b0:400e:c00::242]:48694) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eEntr-0006xU-Mj for qemu-devel@nongnu.org; Tue, 14 Nov 2017 21:58:03 -0500 Received: by mail-pf0-x242.google.com with SMTP id r62so3370156pfd.5 for ; Tue, 14 Nov 2017 18:58:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=skyportsystems.com; s=google; h=from:to:cc:subject:date:message-id; bh=rA0tVLRHaJ7ttMION8rAeSsvnRVKqn1bz/BusYD2F+Q=; b=gA0dTZhgzJyWKudTk1DUEIyu47gTHN9HwgUVOhidPkAsMpWnf9UbkZ4H62EJK6RWTa Fr+K9hvX+PAn773PjZvb5iVL/ky0QKXYucWE6KAMDYW20auuJFLBf0IguJt+ji7xO5ah RqVmfDhv5uEbbTizm97dQflVR312msA/ByR7s= 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=rA0tVLRHaJ7ttMION8rAeSsvnRVKqn1bz/BusYD2F+Q=; b=flzmGPYmUHoEF4MugtpEYT9RSfzmT7Rt0C/Pw9OfMTJjyBXzxZOLvMzclYLIPXOBmn 5wlEgv1iHfpLBSVSNkfcb9qyUvqQ7Hym291slvRhJgQAk31tsqa2BOp18I0w48o6ru8N jsahmJMJIA5H3QLGwYbeyXb+Xw1y0YI9xYovsWbRrx/8hC+PLZVDpRtNA/cVNn50Be+9 KhXY3aNPstYSjUh1nRbmEL00Yg01o2gg1brocuAAKCS2JCYwwnUvQQcfs6ijWnyL3+da kfjKxOwsDKMPQAoqFg8hJ3Y2ZUpwmoTTuf7dKAW7jd1EFhA92J3FRNxwnqPXN/MHBDgb NVbg== X-Gm-Message-State: AJaThX7yxu11MVdWn8DVUl9suckRI/b/u2Z8UJWNKw2Uv6F+glbOqhE+ +qgGbL1HJy6vSfrxBhupKVI5sw== X-Google-Smtp-Source: AGs4zMYECTPUNmZeAVOMzF9tHkkqE37+maRtFZAdEPrIxKXO4rXVTgP2axSvVgFzjqaz0WnlKYk8Yw== X-Received: by 10.99.174.1 with SMTP id q1mr14343454pgf.125.1510714682361; Tue, 14 Nov 2017 18:58:02 -0800 (PST) Received: from eswierk-sc.localdomain (67-207-112-138.static.wiline.com. [67.207.112.138]) by smtp.gmail.com with ESMTPSA id p6sm29719535pfg.60.2017.11.14.18.58.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 14 Nov 2017 18:58:01 -0800 (PST) To: Jason Wang , "Daniel P . Berrange" , Stefan Hajnoczi Date: Tue, 14 Nov 2017 18:57:55 -0800 Message-Id: <1510714675-84054-1-git-send-email-eswierk@skyportsystems.com> X-Mailer: git-send-email 1.9.1 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::242 Subject: [Qemu-devel] [PATCH v2] net: Transmit zero UDP checksum as 0xFFFF X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Ed Swierk via Qemu-devel From: "Denis V. Lunev\" via" Reply-To: Ed Swierk Cc: Ed Swierk , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP The checksum algorithm used by IPv4, TCP and UDP allows a zero value to be represented by either 0x0000 and 0xFFFF. But per RFC 768, a zero UDP checksum must be transmitted as 0xFFFF, as 0x0000 is a special value meaning no checksum. Substitute 0xFFFF whenever a checksum is computed as zero when modifying a UDP datagram header. Doing this on IPv4 packets and TCP segments is unnecessary but legal. Add a wrapper for net_checksum_finish() that makes the substitution. (We can't just change net_checksum_finish(), as that function is also used by receivers to verify checksums, and in that case the expected value is always 0x0000.) v2: Add a wrapper net_checksum_finish_hdr() rather than duplicating the logic at every caller. Signed-off-by: Ed Swierk --- hw/net/e1000.c | 2 +- hw/net/net_rx_pkt.c | 2 +- hw/net/net_tx_pkt.c | 6 +++--- hw/net/vmxnet3.c | 3 ++- include/net/checksum.h | 7 +++++++ 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/hw/net/e1000.c b/hw/net/e1000.c index d642314..4e33667 100644 --- a/hw/net/e1000.c +++ b/hw/net/e1000.c @@ -506,7 +506,7 @@ putsum(uint8_t *data, uint32_t n, uint32_t sloc, uint32_t css, uint32_t cse) n = cse + 1; if (sloc < n-1) { sum = net_checksum_add(n-css, data+css); - stw_be_p(data + sloc, net_checksum_finish(sum)); + stw_be_p(data + sloc, net_checksum_finish_hdr(sum)); } } diff --git a/hw/net/net_rx_pkt.c b/hw/net/net_rx_pkt.c index 1019b50..a2b2c2d 100644 --- a/hw/net/net_rx_pkt.c +++ b/hw/net/net_rx_pkt.c @@ -517,7 +517,7 @@ _net_rx_pkt_calc_l4_csum(struct NetRxPkt *pkt) cntr += net_checksum_add_iov(pkt->vec, pkt->vec_len, pkt->l4hdr_off, csl, cso); - csum = net_checksum_finish(cntr); + csum = net_checksum_finish_hdr(cntr); trace_net_rx_pkt_l4_csum_calc_csum(pkt->l4hdr_off, csl, cntr, csum); diff --git a/hw/net/net_tx_pkt.c b/hw/net/net_tx_pkt.c index 20b2549..dc95f12 100644 --- a/hw/net/net_tx_pkt.c +++ b/hw/net/net_tx_pkt.c @@ -126,12 +126,12 @@ void net_tx_pkt_update_ip_checksums(struct NetTxPkt *pkt) /* Calculate IP pseudo header checksum */ cntr = eth_calc_ip4_pseudo_hdr_csum(ip_hdr, pkt->payload_len, &cso); - csum = cpu_to_be16(~net_checksum_finish(cntr)); + csum = cpu_to_be16(~net_checksum_finish_hdr(cntr)); } else if (gso_type == VIRTIO_NET_HDR_GSO_TCPV6) { /* Calculate IP pseudo header checksum */ cntr = eth_calc_ip6_pseudo_hdr_csum(ip_hdr, pkt->payload_len, IP_PROTO_TCP, &cso); - csum = cpu_to_be16(~net_checksum_finish(cntr)); + csum = cpu_to_be16(~net_checksum_finish_hdr(cntr)); } else { return; } @@ -486,7 +486,7 @@ static void net_tx_pkt_do_sw_csum(struct NetTxPkt *pkt) net_checksum_add_iov(iov, iov_len, pkt->virt_hdr.csum_start, csl, cso); /* Put the checksum obtained into the packet */ - csum = cpu_to_be16(net_checksum_finish(csum_cntr)); + csum = cpu_to_be16(net_checksum_finish_hdr(csum_cntr)); iov_from_buf(iov, iov_len, csum_offset, &csum, sizeof csum); } diff --git a/hw/net/vmxnet3.c b/hw/net/vmxnet3.c index 90f6943..ee7cdeb 100644 --- a/hw/net/vmxnet3.c +++ b/hw/net/vmxnet3.c @@ -970,7 +970,8 @@ static void vmxnet3_rx_need_csum_calculate(struct NetRxPkt *pkt, data = (uint8_t *)pkt_data + vhdr->csum_start; len = pkt_len - vhdr->csum_start; /* Put the checksum obtained into the packet */ - stw_be_p(data + vhdr->csum_offset, net_raw_checksum(data, len)); + stw_be_p(data + vhdr->csum_offset, + net_checksum_finish_hdr(net_checksum_add(len, data))); vhdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM; vhdr->flags |= VIRTIO_NET_HDR_F_DATA_VALID; diff --git a/include/net/checksum.h b/include/net/checksum.h index 7df472c..9878c8b 100644 --- a/include/net/checksum.h +++ b/include/net/checksum.h @@ -34,6 +34,13 @@ net_checksum_add(int len, uint8_t *buf) } static inline uint16_t +net_checksum_finish_hdr(uint32_t sum) +{ + uint16_t s = net_checksum_finish(sum); + return s ? s : 0xFFFF; +} + +static inline uint16_t net_raw_checksum(uint8_t *data, int length) { return net_checksum_finish(net_checksum_add(length, data));