public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Asias He <asias.hejun@gmail.com>
To: Pekka Enberg <penberg@kernel.org>
Cc: Cyrill Gorcunov <gorcunov@gmail.com>, Ingo Molnar <mingo@elte.hu>,
	Sasha Levin <levinsasha928@gmail.com>,
	Prasad Joshi <prasadjoshi124@gmail.com>,
	kvm@vger.kernel.org, Asias He <asias.hejun@gmail.com>
Subject: [PATCH 01/16] kvm tools: Introduce uip_udp_make_pkg()
Date: Sun, 17 Jul 2011 16:56:49 +0800	[thread overview]
Message-ID: <1310893024-21615-2-git-send-email-asias.hejun@gmail.com> (raw)
In-Reply-To: <1310893024-21615-1-git-send-email-asias.hejun@gmail.com>

This helper cooks a ethernet package and virtio header for UDP.
This patch also makes uip_udp_socket_thread() shorter.

Signed-off-by: Asias He <asias.hejun@gmail.com>
---
 tools/kvm/include/kvm/uip.h |    1 +
 tools/kvm/net/uip/udp.c     |  100 +++++++++++++++++++++++-------------------
 2 files changed, 56 insertions(+), 45 deletions(-)

diff --git a/tools/kvm/include/kvm/uip.h b/tools/kvm/include/kvm/uip.h
index 18849d2..8333004 100644
--- a/tools/kvm/include/kvm/uip.h
+++ b/tools/kvm/include/kvm/uip.h
@@ -289,4 +289,5 @@ struct uip_buf *uip_buf_get_used(struct uip_info *info);
 struct uip_buf *uip_buf_get_free(struct uip_info *info);
 struct uip_buf *uip_buf_clone(struct uip_tx_arg *arg);
 
+int uip_udp_make_pkg(struct uip_info *info, struct uip_udp_socket *sk, struct uip_buf *buf, u8 *payload, int payload_len);
 #endif /* KVM__UIP_H */
diff --git a/tools/kvm/net/uip/udp.c b/tools/kvm/net/uip/udp.c
index 5f9d7a4..fcd5018 100644
--- a/tools/kvm/net/uip/udp.c
+++ b/tools/kvm/net/uip/udp.c
@@ -98,18 +98,66 @@ static int uip_udp_socket_send(struct uip_udp_socket *sk, struct uip_udp *udp)
 	return 0;
 }
 
+int uip_udp_make_pkg(struct uip_info *info, struct uip_udp_socket *sk, struct uip_buf *buf, u8* payload, int payload_len)
+{
+	struct uip_eth *eth2;
+	struct uip_udp *udp2;
+	struct uip_ip *ip2;
+
+	/*
+	 * Cook a ethernet frame
+	 */
+	udp2		= (struct uip_udp *)(buf->eth);
+	eth2		= (struct uip_eth *)buf->eth;
+	ip2		= (struct uip_ip *)(buf->eth);
+
+	eth2->src	= info->host_mac;
+	eth2->dst	= info->guest_mac;
+	eth2->type	= htons(UIP_ETH_P_IP);
+
+	ip2->vhl	= UIP_IP_VER_4 | UIP_IP_HDR_LEN;
+	ip2->tos	= 0;
+	ip2->id		= 0;
+	ip2->flgfrag	= 0;
+	ip2->ttl	= UIP_IP_TTL;
+	ip2->proto	= UIP_IP_P_UDP;
+	ip2->csum	= 0;
+
+	ip2->sip	= sk->dip;
+	ip2->dip	= sk->sip;
+	udp2->sport	= sk->dport;
+	udp2->dport	= sk->sport;
+
+	udp2->len	= htons(payload_len + uip_udp_hdrlen(udp2));
+	udp2->csum	= 0;
+
+	if (payload)
+		memcpy(udp2->payload, payload, payload_len);
+
+	ip2->len	= udp2->len + htons(uip_ip_hdrlen(ip2));
+	ip2->csum	= uip_csum_ip(ip2);
+	udp2->csum	= uip_csum_udp(udp2);
+
+	/*
+	 * virtio_net_hdr
+	 */
+	buf->vnet_len	= sizeof(struct virtio_net_hdr);
+	memset(buf->vnet, 0, buf->vnet_len);
+
+	buf->eth_len	= ntohs(ip2->len) + uip_eth_hdrlen(&ip2->eth);
+
+	return 0;
+}
+
 static void *uip_udp_socket_thread(void *p)
 {
 	struct epoll_event events[UIP_UDP_MAX_EVENTS];
 	struct uip_udp_socket *sk;
 	struct uip_info *info;
-	struct uip_eth *eth2;
-	struct uip_udp *udp2;
 	struct uip_buf *buf;
-	struct uip_ip *ip2;
+	int payload_len;
 	u8 *payload;
 	int nfds;
-	int ret;
 	int i;
 
 	info = p;
@@ -127,8 +175,8 @@ static void *uip_udp_socket_thread(void *p)
 		for (i = 0; i < nfds; i++) {
 
 			sk = events[i].data.ptr;
-			ret = recvfrom(sk->fd, payload, UIP_MAX_UDP_PAYLOAD, 0, NULL, NULL);
-			if (ret < 0)
+			payload_len = recvfrom(sk->fd, payload, UIP_MAX_UDP_PAYLOAD, 0, NULL, NULL);
+			if (payload_len < 0)
 				continue;
 
 			/*
@@ -136,45 +184,7 @@ static void *uip_udp_socket_thread(void *p)
 			 */
 			buf		= uip_buf_get_free(info);
 
-			/*
-			 * Cook a ethernet frame
-			 */
-			udp2		= (struct uip_udp *)(buf->eth);
-			eth2		= (struct uip_eth *)buf->eth;
-			ip2		= (struct uip_ip *)(buf->eth);
-
-			eth2->src	= info->host_mac;
-			eth2->dst	= info->guest_mac;
-			eth2->type	= htons(UIP_ETH_P_IP);
-
-			ip2->vhl	= UIP_IP_VER_4 | UIP_IP_HDR_LEN;
-			ip2->tos	= 0;
-			ip2->id		= 0;
-			ip2->flgfrag	= 0;
-			ip2->ttl	= UIP_IP_TTL;
-			ip2->proto	= UIP_IP_P_UDP;
-			ip2->csum	= 0;
-			ip2->sip	= sk->dip;
-			ip2->dip	= sk->sip;
-
-			udp2->sport	= sk->dport;
-			udp2->dport	= sk->sport;
-			udp2->len	= htons(ret + uip_udp_hdrlen(udp2));
-			udp2->csum	= 0;
-
-			memcpy(udp2->payload, payload, ret);
-
-			ip2->len	= udp2->len + htons(uip_ip_hdrlen(ip2));
-			ip2->csum	= uip_csum_ip(ip2);
-			udp2->csum	= uip_csum_udp(udp2);
-
-			/*
-			 * virtio_net_hdr
-			 */
-			buf->vnet_len	= sizeof(struct virtio_net_hdr);
-			memset(buf->vnet, 0, buf->vnet_len);
-
-			buf->eth_len	= ntohs(ip2->len) + uip_eth_hdrlen(&ip2->eth);
+			uip_udp_make_pkg(info, sk, buf, payload, payload_len);
 
 			/*
 			 * Send data received from socket to guest
-- 
1.7.5.4


  reply	other threads:[~2011-07-17  8:59 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-07-17  8:56 [PATCH 00/16] Implement DHCP support for user mode network Asias He
2011-07-17  8:56 ` Asias He [this message]
2011-07-17  8:56 ` [PATCH 02/16] kvm tools: Introduce struct uip_dhcp Asias He
2011-07-17  8:56 ` [PATCH 03/16] kvm tools: Add helper to tell if a UDP package is a DHCP package Asias He
2011-07-17  8:56 ` [PATCH 04/16] kvm tools: Add helpers to tell the type of a DHCP message Asias He
2011-07-17  8:56 ` [PATCH 05/16] kvm tools: Get domain name and nameserver from host Asias He
2011-07-17  9:36   ` Sasha Levin
2011-07-18  4:48     ` Asias He
2011-07-18  8:27       ` Pekka Enberg
2011-07-17  8:56 ` [PATCH 06/16] kvm tools: Fill DHCP options with domain name and DNS server IP Asias He
2011-07-17  8:56 ` [PATCH 07/16] kvm tools: Fill all DHCP options Asias He
2011-07-17  8:56 ` [PATCH 08/16] kvm tools: Introduce uip_dhcp_make_pkg() Asias He
2011-07-17  8:56 ` [PATCH 09/16] kvm tools: Introduce uip_tx_do_ipv4_udp_dhcp() Asias He
2011-07-17  8:56 ` [PATCH 10/16] kvm tools: Get DNS information from host in uip_init() Asias He
2011-07-17  8:56 ` [PATCH 11/16] kvm tools: Handle DHCP package in gernal UDP processing path Asias He
2011-07-17  8:57 ` [PATCH 12/16] kvm tools: Introduce --guest-ip option Asias He
2011-07-17  8:57 ` [PATCH 13/16] kvm tools: Introduce --host-mac option Asias He
2011-07-17  8:57 ` [PATCH 14/16] kvm tools: Rename --host-ip-addr to --host-ip Asias He
2011-07-17  8:57 ` [PATCH 15/16] kvm tools: Initialize MAC address for virtio net properly Asias He
2011-07-17  8:57 ` [PATCH 16/16] kvm tools: Initialize MAC and IP address for uip properly Asias He
2011-07-18 10:20 ` [PATCH 00/16] Implement DHCP support for user mode network Pekka Enberg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1310893024-21615-2-git-send-email-asias.hejun@gmail.com \
    --to=asias.hejun@gmail.com \
    --cc=gorcunov@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=levinsasha928@gmail.com \
    --cc=mingo@elte.hu \
    --cc=penberg@kernel.org \
    --cc=prasadjoshi124@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox