net: Don't copy every packet that waits for an ARP

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.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>
Acked-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Joe Hershberger 2012-05-23 07:59:24 +00:00
parent d7310c7e63
commit e94070c443
5 changed files with 23 additions and 38 deletions

View File

@ -483,14 +483,14 @@ static inline void net_set_state(enum net_loop_state state)
net_state = state; net_state = state;
} }
/* Transmit "NetTxPacket" */ /* Transmit a packet */
static inline void NetSendPacket(uchar *pkt, int len) static inline void NetSendPacket(uchar *pkt, int len)
{ {
(void) eth_send(pkt, len); (void) eth_send(pkt, len);
} }
/* /*
* Transmit UDP packet, performing ARP request if needed * Transmit "NetTxPacket" as UDP packet, performing ARP request if needed
* (ether will be populated) * (ether will be populated)
* *
* @param ether Raw packet buffer * @param ether Raw packet buffer
@ -499,7 +499,7 @@ static inline void NetSendPacket(uchar *pkt, int len)
* @param sport Source UDP port * @param sport Source UDP port
* @param payload_len Length of data after the UDP header * @param payload_len Length of data after the UDP header
*/ */
extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, extern int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport,
int sport, int payload_len); int sport, int payload_len);
/* Processes a received packet */ /* Processes a received packet */

View File

@ -30,22 +30,22 @@ IPaddr_t NetArpWaitPacketIP;
IPaddr_t NetArpWaitReplyIP; IPaddr_t NetArpWaitReplyIP;
/* MAC address of waiting packet's destination */ /* MAC address of waiting packet's destination */
uchar *NetArpWaitPacketMAC; uchar *NetArpWaitPacketMAC;
/* THE transmit packet */
uchar *NetArpWaitTxPacket;
int NetArpWaitTxPacketSize; int NetArpWaitTxPacketSize;
uchar NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
ulong NetArpWaitTimerStart; ulong NetArpWaitTimerStart;
int NetArpWaitTry; int NetArpWaitTry;
uchar *NetArpTxPacket; /* THE ARP transmit packet */
uchar NetArpPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
void ArpInit(void) void ArpInit(void)
{ {
/* XXX problem with bss workaround */ /* XXX problem with bss workaround */
NetArpWaitPacketMAC = NULL; NetArpWaitPacketMAC = NULL;
NetArpWaitPacketIP = 0; NetArpWaitPacketIP = 0;
NetArpWaitReplyIP = 0; NetArpWaitReplyIP = 0;
NetArpWaitTxPacket = &NetArpWaitPacketBuf[0] + (PKTALIGN - 1);
NetArpWaitTxPacket -= (ulong)NetArpWaitTxPacket % PKTALIGN;
NetArpWaitTxPacketSize = 0; NetArpWaitTxPacketSize = 0;
NetArpTxPacket = &NetArpPacketBuf[0] + (PKTALIGN - 1);
NetArpTxPacket -= (ulong)NetArpTxPacket % PKTALIGN;
} }
void ArpRequest(void) void ArpRequest(void)
@ -56,7 +56,7 @@ void ArpRequest(void)
debug("ARP broadcast %d\n", NetArpWaitTry); debug("ARP broadcast %d\n", NetArpWaitTry);
pkt = NetTxPacket; pkt = NetArpTxPacket;
eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_ARP); eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_ARP);
pkt += eth_hdr_size; pkt += eth_hdr_size;
@ -88,7 +88,7 @@ void ArpRequest(void)
} }
NetWriteIP(&arp->ar_tpa, NetArpWaitReplyIP); NetWriteIP(&arp->ar_tpa, NetArpWaitReplyIP);
NetSendPacket(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE); NetSendPacket(NetArpTxPacket, eth_hdr_size + ARP_HDR_SIZE);
} }
void ArpTimeoutCheck(void) void ArpTimeoutCheck(void)
@ -196,11 +196,11 @@ void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
net_get_arp_handler()((uchar *)arp, 0, reply_ip_addr, net_get_arp_handler()((uchar *)arp, 0, reply_ip_addr,
0, len); 0, len);
/* modify header, and transmit it */ /* set the mac address in the waiting packet's header
memcpy(((struct ethernet_hdr *)NetArpWaitTxPacket)-> and transmit it */
et_dest, &arp->ar_sha, ARP_HLEN); memcpy(((struct ethernet_hdr *)NetTxPacket)->et_dest,
NetSendPacket(NetArpWaitTxPacket, &arp->ar_sha, ARP_HLEN);
NetArpWaitTxPacketSize); NetSendPacket(NetTxPacket, NetArpWaitTxPacketSize);
/* no arp request pending now */ /* no arp request pending now */
NetArpWaitPacketIP = 0; NetArpWaitPacketIP = 0;

View File

@ -16,8 +16,6 @@
extern IPaddr_t NetArpWaitPacketIP; extern IPaddr_t NetArpWaitPacketIP;
/* MAC address of waiting packet's destination */ /* MAC address of waiting packet's destination */
extern uchar *NetArpWaitPacketMAC; extern uchar *NetArpWaitPacketMAC;
/* THE transmit packet */
extern uchar *NetArpWaitTxPacket;
extern int NetArpWaitTxPacketSize; extern int NetArpWaitTxPacketSize;
extern ulong NetArpWaitTimerStart; extern ulong NetArpWaitTimerStart;
extern int NetArpWaitTry; extern int NetArpWaitTry;

View File

@ -442,6 +442,9 @@ restart:
* Abort if ctrl-c was pressed. * Abort if ctrl-c was pressed.
*/ */
if (ctrlc()) { if (ctrlc()) {
/* cancel any ARP that may not have completed */
NetArpWaitPacketIP = 0;
net_cleanup_loop(); net_cleanup_loop();
eth_halt(); eth_halt();
puts("\nAbort\n"); puts("\nAbort\n");
@ -632,7 +635,6 @@ int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
int payload_len) int payload_len)
{ {
uchar *pkt; uchar *pkt;
int need_arp = 0;
int eth_hdr_size; int eth_hdr_size;
int pkt_hdr_size; int pkt_hdr_size;
@ -649,35 +651,21 @@ int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
if (dest == 0xFFFFFFFF) if (dest == 0xFFFFFFFF)
ether = NetBcastAddr; ether = NetBcastAddr;
/* pkt = (uchar *)NetTxPacket;
* 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;
eth_hdr_size = NetSetEther(pkt, ether, PROT_IP); eth_hdr_size = NetSetEther(pkt, ether, PROT_IP);
pkt += eth_hdr_size; pkt += eth_hdr_size;
net_set_udp_header(pkt, dest, dport, sport, payload_len); net_set_udp_header(pkt, dest, dport, sport, payload_len);
pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE; 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); debug("sending ARP for %pI4\n", &dest);
/* save the ip and eth addr for the packet to send after arp */ /* save the ip and eth addr for the packet to send after arp */
NetArpWaitPacketIP = dest; NetArpWaitPacketIP = dest;
NetArpWaitPacketMAC = ether; 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 */ /* size of the waiting packet */
NetArpWaitTxPacketSize = pkt_hdr_size + payload_len; NetArpWaitTxPacketSize = pkt_hdr_size + payload_len;

View File

@ -49,9 +49,8 @@ static int ping_send(void)
NetArpWaitPacketIP = NetPingIP; NetArpWaitPacketIP = NetPingIP;
eth_hdr_size = NetSetEther(NetArpWaitTxPacket, NetEtherNullAddr, eth_hdr_size = NetSetEther(NetTxPacket, NetEtherNullAddr, PROT_IP);
PROT_IP); pkt = (uchar *)NetTxPacket + eth_hdr_size;
pkt = NetArpWaitTxPacket + eth_hdr_size;
set_icmp_header(pkt, NetPingIP); set_icmp_header(pkt, NetPingIP);