diff mbox

[v2,04/31] kvm tools: Implement IP checksum for uip

Message ID 1309423279-3093-5-git-send-email-asias.hejun@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Asias He June 30, 2011, 8:40 a.m. UTC
Other protocal, e.g. TCP, UDP, ICMP, can use uip_csum() to make
checsksum as well.

Signed-off-by: Asias He <asias.hejun@gmail.com>
---
 tools/kvm/Makefile          |    1 +
 tools/kvm/include/kvm/uip.h |    2 ++
 tools/kvm/uip/csum.c        |   25 +++++++++++++++++++++++++
 3 files changed, 28 insertions(+), 0 deletions(-)
 create mode 100644 tools/kvm/uip/csum.c
diff mbox

Patch

diff --git a/tools/kvm/Makefile b/tools/kvm/Makefile
index 7dc5bb2..ece542c 100644
--- a/tools/kvm/Makefile
+++ b/tools/kvm/Makefile
@@ -48,6 +48,7 @@  OBJS	+= irq.o
 OBJS	+= uip/arp.o
 OBJS	+= uip/ipv4.o
 OBJS	+= uip/buf.o
+OBJS	+= uip/csum.o
 OBJS	+= kvm-cmd.o
 OBJS	+= kvm-debug.o
 OBJS	+= kvm-help.o
diff --git a/tools/kvm/include/kvm/uip.h b/tools/kvm/include/kvm/uip.h
index e48420d..00100e1 100644
--- a/tools/kvm/include/kvm/uip.h
+++ b/tools/kvm/include/kvm/uip.h
@@ -103,6 +103,8 @@  static inline u16 uip_ip_len(struct uip_ip *ip)
 int uip_tx_do_ipv4(struct uip_tx_arg *arg);
 int uip_tx_do_arp(struct uip_tx_arg *arg);
 
+u16 uip_csum_ip(struct uip_ip *ip);
+
 struct uip_buf *uip_buf_set_used(struct uip_info *info, struct uip_buf *buf);
 struct uip_buf *uip_buf_set_free(struct uip_info *info, struct uip_buf *buf);
 struct uip_buf *uip_buf_get_used(struct uip_info *info);
diff --git a/tools/kvm/uip/csum.c b/tools/kvm/uip/csum.c
new file mode 100644
index 0000000..8023ddb
--- /dev/null
+++ b/tools/kvm/uip/csum.c
@@ -0,0 +1,25 @@ 
+#include "kvm/uip.h"
+
+static u16 uip_csum(u16 csum, u8 *addr, u16 count)
+{
+	long sum = csum;
+
+	while (count > 1) {
+		sum	+= *(u16 *)addr;
+		addr	+= 2;
+		count	-= 2;
+	}
+
+	if (count > 0)
+		sum += *(unsigned char *)addr;
+
+	while (sum>>16)
+		sum = (sum & 0xffff) + (sum >> 16);
+
+	return ~sum;
+}
+
+u16 uip_csum_ip(struct uip_ip *ip)
+{
+	return uip_csum(0, &ip->vhl, uip_ip_hdrlen(ip));
+}