From patchwork Mon Mar 25 12:26:08 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 230693 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id E6FAE2C00A7 for ; Mon, 25 Mar 2013 23:27:14 +1100 (EST) Received: from localhost ([::1]:40113 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UK6UZ-0003M4-HJ for incoming@patchwork.ozlabs.org; Mon, 25 Mar 2013 08:27:11 -0400 Received: from eggs.gnu.org ([208.118.235.92]:43876) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UK6Tu-00038U-5L for qemu-devel@nongnu.org; Mon, 25 Mar 2013 08:26:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UK6To-000497-Sd for qemu-devel@nongnu.org; Mon, 25 Mar 2013 08:26:30 -0400 Received: from mx1.redhat.com ([209.132.183.28]:28510) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UK6To-00048h-Ka for qemu-devel@nongnu.org; Mon, 25 Mar 2013 08:26:24 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r2PCQMO8027590 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 25 Mar 2013 08:26:23 -0400 Received: from localhost (ovpn-112-26.ams2.redhat.com [10.36.112.26]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id r2PCQLOT022678; Mon, 25 Mar 2013 08:26:22 -0400 From: Stefan Hajnoczi To: Date: Mon, 25 Mar 2013 13:26:08 +0100 Message-Id: <1364214373-2444-3-git-send-email-stefanha@redhat.com> In-Reply-To: <1364214373-2444-1-git-send-email-stefanha@redhat.com> References: <1364214373-2444-1-git-send-email-stefanha@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Dmitry Fleytman , Yan Vugenfirer , Anthony Liguori , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH 2/7] Checksum-related utility functions X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Dmitry Fleytman net_checksum_add_cont() checksum calculation for scattered data with odd chunk sizes net_raw_checksum() checksum calculation for a buffer Signed-off-by: Dmitry Fleytman Signed-off-by: Yan Vugenfirer Signed-off-by: Stefan Hajnoczi --- include/net/checksum.h | 14 +++++++++++++- net/checksum.c | 13 +++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/include/net/checksum.h b/include/net/checksum.h index 1f05298..3e7b93d 100644 --- a/include/net/checksum.h +++ b/include/net/checksum.h @@ -20,10 +20,22 @@ #include -uint32_t net_checksum_add(int len, uint8_t *buf); +uint32_t net_checksum_add_cont(int len, uint8_t *buf, int seq); uint16_t net_checksum_finish(uint32_t sum); uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto, uint8_t *addrs, uint8_t *buf); void net_checksum_calculate(uint8_t *data, int length); +static inline uint32_t +net_checksum_add(int len, uint8_t *buf) +{ + return net_checksum_add_cont(len, buf, 0); +} + +static inline uint16_t +net_raw_checksum(uint8_t *data, int length) +{ + return net_checksum_finish(net_checksum_add(length, data)); +} + #endif /* QEMU_NET_CHECKSUM_H */ diff --git a/net/checksum.c b/net/checksum.c index 9919b2e..4fa5563 100644 --- a/net/checksum.c +++ b/net/checksum.c @@ -20,16 +20,17 @@ #define PROTO_TCP 6 #define PROTO_UDP 17 -uint32_t net_checksum_add(int len, uint8_t *buf) +uint32_t net_checksum_add_cont(int len, uint8_t *buf, int seq) { uint32_t sum = 0; int i; - for (i = 0; i < len; i++) { - if (i & 1) - sum += (uint32_t)buf[i]; - else - sum += (uint32_t)buf[i] << 8; + for (i = seq; i < seq + len; i++) { + if (i & 1) { + sum += (uint32_t)buf[i - seq]; + } else { + sum += (uint32_t)buf[i - seq] << 8; + } } return sum; }