kvm tools: Implement uip_csum_tcp() to calculate TCP checksum

Signed-off-by: Asias He <asias.hejun@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
This commit is contained in:
Asias He
2015-06-01 16:39:45 +01:00
committed by Will Deacon
parent 6bd80bd847
commit 89fb0bf807
2 changed files with 33 additions and 0 deletions
+3
View File
@@ -28,8 +28,10 @@
/*
* IP package maxium len == 64 KBytes
* IP header == 20 Bytes
* TCP header == 20 Bytes
* UDP header == 8 Bytes
*/
#define UIP_MAX_TCP_PAYLOAD (64*1024 - 20 - 20 - 1)
#define UIP_MAX_UDP_PAYLOAD (64*1024 - 20 - 8 - 1)
struct uip_eth_addr {
@@ -269,6 +271,7 @@ int uip_tx_do_arp(struct uip_tx_arg *arg);
u16 uip_csum_icmp(struct uip_icmp *icmp);
u16 uip_csum_udp(struct uip_udp *udp);
u16 uip_csum_tcp(struct uip_tcp *tcp);
u16 uip_csum_ip(struct uip_ip *ip);
struct uip_buf *uip_buf_set_used(struct uip_info *info, struct uip_buf *buf);
+30
View File
@@ -60,3 +60,33 @@ u16 uip_csum_udp(struct uip_udp *udp)
}
}
u16 uip_csum_tcp(struct uip_tcp *tcp)
{
struct uip_pseudo_hdr hdr;
struct uip_ip *ip;
u16 tcp_len;
u8 *pad;
ip = &tcp->ip;
tcp_len = ntohs(ip->len) - uip_ip_hdrlen(ip);
hdr.sip = ip->sip;
hdr.dip = ip->dip;
hdr.zero = 0;
hdr.proto = ip->proto;
hdr.len = htons(tcp_len);
if (tcp_len > UIP_MAX_TCP_PAYLOAD + 20)
pr_warning("tcp_len(%d) is too large", tcp_len);
if (tcp_len % 2) {
pad = (u8 *)&tcp->sport + tcp_len;
*pad = 0;
memcpy((u8 *)&tcp->sport + tcp_len + 1, &hdr, sizeof(hdr));
return uip_csum(0, (u8 *)&tcp->sport, tcp_len + 1 + sizeof(hdr));
} else {
memcpy((u8 *)&tcp->sport + tcp_len, &hdr, sizeof(hdr));
return uip_csum(0, (u8 *)&tcp->sport, tcp_len + sizeof(hdr));
}
}