From nobody Sun May 5 20:20:35 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1510801704745345.75600339186747; Wed, 15 Nov 2017 19:08:24 -0800 (PST) Received: from localhost ([::1]:38743 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eFAXL-00015R-6F for importer@patchew.org; Wed, 15 Nov 2017 22:08:19 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33941) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eFAWW-0000gu-MO for qemu-devel@nongnu.org; Wed, 15 Nov 2017 22:07:29 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eFAWR-0005kK-EE for qemu-devel@nongnu.org; Wed, 15 Nov 2017 22:07:28 -0500 Received: from mail-pg0-x244.google.com ([2607:f8b0:400e:c05::244]:53926) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eFAWR-0005j6-89 for qemu-devel@nongnu.org; Wed, 15 Nov 2017 22:07:23 -0500 Received: by mail-pg0-x244.google.com with SMTP id r12so2775747pgu.10 for ; Wed, 15 Nov 2017 19:07:22 -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 p12sm123116pgn.60.2017.11.15.19.07.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 15 Nov 2017 19:07:21 -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=Rh1QP3g2D6er/LVz8Gz8LcXwa0moPwTofbbMNGM+Ce0=; b=pi0uCEvtErjCQclric2VmHzTCT4PjXSpNwPVk18956aJO1QvMbffIYa9i2gejZC+4V yst+ckvHpmaCgvI4WoA5D1jyWAzUQtR8xOPkbxz2bw9sKXcwiRtLCpKmbslaE/wtpC00 nXNaF93J43j3wVVMLwlBM+EzEpWSdINU83MFA= 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=Rh1QP3g2D6er/LVz8Gz8LcXwa0moPwTofbbMNGM+Ce0=; b=um7UxZhziZEutfX3yNTTCvEAo1OTBFMxV+3lGIOTHNAVXX8pEvhp9VxzZ9PiWDqwQi Zmz/BtvXGCZhMuhhB11J3uj8bhWwE0sXOnhByLVUF1r2bCfy3v6gS1atSI56WkJMLq4l TceJ6f6K6xQkzvO4+dKWWuQ1dl38qiDre8inS0RYHVeyX6MRTl0SYWLmLW5qTR7gwB9+ Eg+sZjbzsZBKwZwDIJHjPcyRkrM7kvqHKW78jBADzgfxN6u5R+30YnFsr+E1CRNuzkan zzOU9oPb6wqcH6c4utfNwMSUATTH8WlVYmsVqyOJ5+j7Hp9yDxiig124HGssrvUoeOKo lJRQ== X-Gm-Message-State: AJaThX6GHFr/QNEW3WMWwtreD8yPwqwEWioBNGUIBjU2+Gk8XNL0hRA+ DYXIIo8iaB1fdZ47yC6Q4vB7lQ== X-Google-Smtp-Source: AGs4zMaZ4pak/K8Atbgat+OoV0aII2mo3zjSkWr8n4exWjsfyHdTDFpQPzqUqytLN6aW6wnO+O8aLQ== X-Received: by 10.98.98.132 with SMTP id w126mr254772pfb.208.1510801641975; Wed, 15 Nov 2017 19:07:21 -0800 (PST) To: Jason Wang , "Daniel P . Berrange" , Stefan Hajnoczi , Dmitry Fleytman Date: Wed, 15 Nov 2017 19:07:13 -0800 Message-Id: <1510801633-115970-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:c05::244 Subject: [Qemu-devel] [PATCH v3] 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: , From: Ed Swierk via Qemu-devel Reply-To: Ed Swierk Cc: Ed Swierk , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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.) v3: Leave net_tx_pkt_update_ip_checksums() alone since it's only computing a partial sum of the IP pseudo-header. Rename wrapper to net_checksum_finish_nozero() for clarity. 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 | 2 +- hw/net/vmxnet3.c | 3 ++- include/net/checksum.h | 6 ++++++ 5 files changed, 11 insertions(+), 4 deletions(-) diff --git a/hw/net/e1000.c b/hw/net/e1000.c index 72a92be..804ec08 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 =3D cse + 1; if (sloc < n-1) { sum =3D net_checksum_add(n-css, data+css); - stw_be_p(data + sloc, net_checksum_finish(sum)); + stw_be_p(data + sloc, net_checksum_finish_nozero(sum)); } } =20 diff --git a/hw/net/net_rx_pkt.c b/hw/net/net_rx_pkt.c index cef1c2e..98a5030 100644 --- a/hw/net/net_rx_pkt.c +++ b/hw/net/net_rx_pkt.c @@ -518,7 +518,7 @@ _net_rx_pkt_calc_l4_csum(struct NetRxPkt *pkt) cntr +=3D net_checksum_add_iov(pkt->vec, pkt->vec_len, pkt->l4hdr_off, csl, cso); =20 - csum =3D net_checksum_finish(cntr); + csum =3D net_checksum_finish_nozero(cntr); =20 trace_net_rx_pkt_l4_csum_calc_csum(pkt->l4hdr_off, csl, cntr, csum); =20 diff --git a/hw/net/net_tx_pkt.c b/hw/net/net_tx_pkt.c index 20b2549..e29c881 100644 --- a/hw/net/net_tx_pkt.c +++ b/hw/net/net_tx_pkt.c @@ -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); =20 /* Put the checksum obtained into the packet */ - csum =3D cpu_to_be16(net_checksum_finish(csum_cntr)); + csum =3D cpu_to_be16(net_checksum_finish_nozero(csum_cntr)); iov_from_buf(iov, iov_len, csum_offset, &csum, sizeof csum); } =20 diff --git a/hw/net/vmxnet3.c b/hw/net/vmxnet3.c index 8c4bae5..cdc307d 100644 --- a/hw/net/vmxnet3.c +++ b/hw/net/vmxnet3.c @@ -972,7 +972,8 @@ static void vmxnet3_rx_need_csum_calculate(struct NetRx= Pkt *pkt, data =3D (uint8_t *)pkt_data + vhdr->csum_start; len =3D 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_nozero(net_checksum_add(len, data))); =20 vhdr->flags &=3D ~VIRTIO_NET_HDR_F_NEEDS_CSUM; vhdr->flags |=3D VIRTIO_NET_HDR_F_DATA_VALID; diff --git a/include/net/checksum.h b/include/net/checksum.h index 7df472c..05a0d27 100644 --- a/include/net/checksum.h +++ b/include/net/checksum.h @@ -34,6 +34,12 @@ net_checksum_add(int len, uint8_t *buf) } =20 static inline uint16_t +net_checksum_finish_nozero(uint32_t sum) +{ + return net_checksum_finish(sum) ?: 0xFFFF; +} + +static inline uint16_t net_raw_checksum(uint8_t *data, int length) { return net_checksum_finish(net_checksum_add(length, data)); --=20 1.9.1