[U-Boot] [PATCH 24/28] net: Don't copy every packet that waits for an ARP
Joe Hershberger
joe.hershberger at ni.com
Fri Jan 20 01:53:27 CET 2012
Use the NetArpTxPacket for the ARP packet, not to hold what used to be
in NetTxPacket
This saves a copy and makes the code easier to understand
Generalize the formation of the ARP packet
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/arp.c | 44 ++++++++++++++++++++++++--------------------
net/arp.h | 4 ++--
net/net.c | 24 ++++++------------------
net/ping.c | 5 ++---
4 files changed, 34 insertions(+), 43 deletions(-)
diff --git a/net/arp.c b/net/arp.c
index b3be0a6..938db59 100644
--- a/net/arp.c
+++ b/net/arp.c
@@ -30,25 +30,26 @@ IPaddr_t NetArpWaitPacketIP;
IPaddr_t NetArpWaitReplyIP;
/* MAC address of waiting packet's destination */
uchar *NetArpWaitPacketMAC;
-/* THE transmit packet */
-uchar *NetArpWaitTxPacket;
int NetArpWaitTxPacketSize;
-uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
ulong NetArpWaitTimerStart;
int NetArpWaitTry;
+uchar *NetArpTxPacket; /* THE ARP transmit packet */
+uchar NetArpPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
+
void ArpInit(void)
{
/* XXX problem with bss workaround */
NetArpWaitPacketMAC = NULL;
NetArpWaitPacketIP = 0;
NetArpWaitReplyIP = 0;
- NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
- NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
NetArpWaitTxPacketSize = 0;
+ NetArpTxPacket = &NetArpPacketBuf[0] + (PKTALIGN - 1);
+ NetArpTxPacket -= (ulong)NetArpTxPacket % PKTALIGN;
}
-void ArpRequest(void)
+void ArpRawRequest(IPaddr_t sourceIP, const uchar *targetEther,
+ IPaddr_t targetIP)
{
uchar *pkt;
struct ARP_t *arp;
@@ -56,7 +57,7 @@ void ArpRequest(void)
debug("ARP broadcast %d\n", NetArpWaitTry);
- pkt = NetTxPacket;
+ pkt = NetArpTxPacket;
eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_ARP);
pkt += eth_hdr_size;
@@ -69,12 +70,16 @@ void ArpRequest(void)
arp->ar_pln = ARP_PLEN;
arp->ar_op = htons(ARPOP_REQUEST);
- /* source ET addr */
- memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN);
- /* source IP addr */
- NetWriteIP(&arp->ar_spa, NetOurIP);
- /* dest ET addr = 0 */
- memset(&arp->ar_tha, 0, ARP_HLEN);
+ memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN); /* source ET addr */
+ NetWriteIP(&arp->ar_spa, sourceIP); /* source IP addr */
+ memcpy(&arp->ar_tha, targetEther, ARP_HLEN); /* target ET addr */
+ NetWriteIP(&arp->ar_tpa, targetIP); /* target IP addr */
+
+ NetSendPacket(NetArpTxPacket, eth_hdr_size + ARP_HDR_SIZE);
+}
+
+void ArpRequest(void)
+{
if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
(NetOurIP & NetOurSubnetMask)) {
if (NetOurGatewayIP == 0) {
@@ -87,8 +92,7 @@ void ArpRequest(void)
NetArpWaitReplyIP = NetArpWaitPacketIP;
}
- NetWriteIP(&arp->ar_tpa, NetArpWaitReplyIP);
- NetSendPacket(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE);
+ ArpRawRequest(NetOurIP, NetEtherNullAddr, NetArpWaitReplyIP);
}
void ArpTimeoutCheck(void)
@@ -197,11 +201,11 @@ void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len)
NetGetARPHandler()((uchar *)arp, 0,
reply_ip_addr, 0, len);
- /* modify header, and transmit it */
- memcpy(((struct Ethernet_t *)NetArpWaitTxPacket)->
- et_dest, &arp->ar_sha, ARP_HLEN);
- NetSendPacket(NetArpWaitTxPacket,
- NetArpWaitTxPacketSize);
+ /* set the mac address in the waiting packet's header
+ and transmit it */
+ memcpy(((struct Ethernet_t *)NetTxPacket)->et_dest,
+ &arp->ar_sha, ARP_HLEN);
+ NetSendPacket(NetTxPacket, NetArpWaitTxPacketSize);
/* no arp request pending now */
NetArpWaitPacketIP = 0;
diff --git a/net/arp.h b/net/arp.h
index d9ce03d..9657e67 100644
--- a/net/arp.h
+++ b/net/arp.h
@@ -16,14 +16,14 @@
extern IPaddr_t NetArpWaitPacketIP;
/* MAC address of waiting packet's destination */
extern uchar *NetArpWaitPacketMAC;
-/* THE transmit packet */
-extern uchar *NetArpWaitTxPacket;
extern int NetArpWaitTxPacketSize;
extern ulong NetArpWaitTimerStart;
extern int NetArpWaitTry;
void ArpInit(void);
void ArpRequest(void);
+void ArpRawRequest(IPaddr_t sourceIP, const uchar *targetEther,
+ IPaddr_t targetIP);
void ArpTimeoutCheck(void);
void ArpReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len);
diff --git a/net/net.c b/net/net.c
index 59ac167..fe4e528 100644
--- a/net/net.c
+++ b/net/net.c
@@ -444,6 +444,9 @@ restart:
* Abort if ctrl-c was pressed.
*/
if (ctrlc()) {
+ /* cancel any ARP that may not have completed */
+ NetArpWaitPacketIP = 0;
+
NetCleanupLoop();
eth_halt();
puts("\nAbort\n");
@@ -638,7 +641,6 @@ 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;
@@ -656,35 +658,21 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
if (dest == 0xFFFFFFFF)
ether = NetBcastAddr;
- /*
- * if MAC address was not discovered yet, save the packet and do
- * an ARP request
- */
- if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
- need_arp = 1;
- pkt = NetArpWaitTxPacket;
- } else
- pkt = (uchar *)NetTxPacket;
+ 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;
- if (need_arp) {
+ /* if MAC address was not discovered yet, do an ARP request */
+ if (memcmp(ether, NetEtherNullAddr, 6) == 0) {
debug("sending ARP for %pI4\n", &dest);
/* save the ip and eth addr for the packet to send after arp */
NetArpWaitPacketIP = dest;
NetArpWaitPacketMAC = ether;
- /*
- * Copy the packet data from the NetTxPacket into the
- * NetArpWaitTxPacket to send after arp
- */
- memcpy(pkt + IP_UDP_HDR_SIZE, (uchar *)NetTxPacket +
- pkt_hdr_size, payload_len);
-
/* size of the waiting packet */
NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;
diff --git a/net/ping.c b/net/ping.c
index 687c5aa..8332c88 100644
--- a/net/ping.c
+++ b/net/ping.c
@@ -49,9 +49,8 @@ static int PingSend(void)
NetArpWaitPacketIP = NetPingIP;
- eth_hdr_size = NetSetEther(NetArpWaitTxPacket, NetEtherNullAddr,
- PROT_IP);
- pkt = NetArpWaitTxPacket + eth_hdr_size;
+ eth_hdr_size = NetSetEther(NetTxPacket, NetEtherNullAddr, PROT_IP);
+ pkt = (uchar *)NetTxPacket + eth_hdr_size;
SetICMPHeader(pkt, NetPingIP);
--
1.6.0.2
More information about the U-Boot
mailing list