[U-Boot] [PATCH 23/28] net: Refactor NetSendUDPPacket to share more code

Joe Hershberger joe.hershberger at ni.com
Fri Jan 20 01:53:26 CET 2012


Signed-off-by: Joe Hershberger <joe.hershberger at ni.com>
Cc: Joe Hershberger <joe.hershberger at gmail.com>
Cc: Wolfgang Denk <wd at denx.de>
---
 net/net.c |   44 +++++++++++++++++++++++++-------------------
 1 files changed, 25 insertions(+), 19 deletions(-)

diff --git a/net/net.c b/net/net.c
index 51d899b..59ac167 100644
--- a/net/net.c
+++ b/net/net.c
@@ -638,6 +638,9 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
 		int payload_len)
 {
 	uchar *pkt;
+	int need_arp = 0;
+	int eth_hdr_size;
+	int pkt_hdr_size;
 
 	/* make sure the NetTxPacket is initialized (NetInit() was called) */
 	if (NetTxPacket == NULL) {
@@ -658,40 +661,43 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
 	 * an ARP request
 	 */
 	if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
+		need_arp = 1;
+		pkt = NetArpWaitTxPacket;
+	} else
+		pkt = (uchar *)NetTxPacket;
+
+	eth_hdr_size = NetSetEther(pkt, ether, PROT_IP);
+	pkt += eth_hdr_size;
+	NetSetUDPHeader(pkt, dest, dport, sport, payload_len);
+	pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
 
-		debug("sending ARP for %08x\n", dest);
+	if (need_arp) {
+		debug("sending ARP for %pI4\n", &dest);
 
+		/* save the ip and eth addr for the packet to send after arp */
 		NetArpWaitPacketIP = dest;
 		NetArpWaitPacketMAC = ether;
 
-		pkt = NetArpWaitTxPacket;
-		pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP);
-
-		NetSetUDPHeader(pkt, dest, dport, sport, payload_len);
+		/*
+		 * Copy the packet data from the NetTxPacket into the
+		 *   NetArpWaitTxPacket to send after arp
+		 */
 		memcpy(pkt + IP_UDP_HDR_SIZE, (uchar *)NetTxPacket +
-		       (pkt - (uchar *)NetArpWaitTxPacket) +
-		       IP_UDP_HDR_SIZE, payload_len);
+			pkt_hdr_size, payload_len);
 
 		/* size of the waiting packet */
-		NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) +
-			IP_UDP_HDR_SIZE + payload_len;
+		NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
 
 		/* and do the ARP request */
 		NetArpWaitTry = 1;
 		NetArpWaitTimerStart = get_timer(0);
 		ArpRequest();
 		return 1;	/* waiting */
+	} else {
+		debug("sending UDP to %pI4/%pM\n", &dest, ether);
+		NetSendPacket(NetTxPacket, pkt_hdr_size + payload_len);
+		return 0;	/* transmitted */
 	}
-
-	debug("sending UDP to %08x/%pM\n", dest, ether);
-
-	pkt = (uchar *)NetTxPacket;
-	pkt += NetSetEther(pkt, ether, PROT_IP);
-	NetSetUDPHeader(pkt, dest, dport, sport, payload_len);
-	NetSendPacket(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE +
-		payload_len);
-
-	return 0;	/* transmitted */
 }
 
 #ifdef CONFIG_IP_DEFRAG
-- 
1.6.0.2



More information about the U-Boot mailing list