Date: Tue, 18 Apr 2006 22:08:57 GMT From: Warner Losh <imp@FreeBSD.org> To: Perforce Change Reviews <perforce@freebsd.org> Subject: PERFORCE change 95547 for review Message-ID: <200604182208.k3IM8vG0024403@repoman.freebsd.org>
next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=95547 Change 95547 by imp@imp_Speedy on 2006/04/18 22:08:27 Some common sense optimizations of the tftp code. Saves about 300 bytes, about a 10% savings. bootspi is now < 12k, but bootiic is still just over. Affected files ... .. //depot/projects/arm/src/sys/boot/arm/at91/Makefile.inc#5 edit .. //depot/projects/arm/src/sys/boot/arm/at91/libat91/emac.c#2 edit Differences ... ==== //depot/projects/arm/src/sys/boot/arm/at91/Makefile.inc#5 (text+ko) ==== ==== //depot/projects/arm/src/sys/boot/arm/at91/libat91/emac.c#2 (text+ko) ==== @@ -32,8 +32,7 @@ static unsigned localMACSet, serverMACSet, MAC_init; static unsigned char localMACAddr[6], serverMACAddr[6]; -static unsigned localIPSet, serverIPSet; -static unsigned char localIPAddr[4], serverIPAddr[4]; +static unsigned localIPAddr, serverIPAddr; static unsigned short serverPort, localPort; static int ackBlock; @@ -89,11 +88,8 @@ p_ARP->operation = SWAP16(ARP_REQUEST); p_memcpy((char*)p_ARP->sender_mac, (char*)localMACAddr, 6); - p_memcpy((char*)p_ARP->sender_ip, (char*)localIPAddr, 4); - p_memset((char*)p_ARP->target_mac, 0, 6); - p_memcpy((char*)p_ARP->target_ip, (char*)serverIPAddr, 4); // wait until transmit is available @@ -120,7 +116,6 @@ unsigned t_checksum; p_memcpy((char*)macHdr->dest_mac, (char*)serverMACAddr, 6); - p_memcpy((char*)macHdr->src_mac, (char*)localMACAddr, 6); macHdr->proto_mac = SWAP16(PROTOCOL_IP); @@ -137,15 +132,14 @@ ipHdr->ip_sum = 0; p_memcpy((char*)ipHdr->ip_src, (char*)localIPAddr, 4); - p_memcpy((char*)ipHdr->ip_dst, (char*)serverIPAddr, 4); ipHdr->ip_sum = SWAP16(IP_checksum(ipHdr, 20)); udpHdr = (udp_header_t*)(ipHdr + 1); - udpHdr->src_port = SWAP16(localPort); - udpHdr->dst_port = SWAP16(serverPort); + udpHdr->src_port = localPort; + udpHdr->dst_port = serverPort; udpHdr->udp_len = SWAP16(8 + tftpLength); udpHdr->udp_cksum = 0; @@ -207,7 +201,7 @@ { tftp_header_t tftpHeader; - if (block_num == (ackBlock + 1)) { + if (block_num == SWAP16(ackBlock + 1)) { ++ackBlock; p_memcpy(dlAddress, data, len); dlAddress += len; @@ -215,7 +209,7 @@ } tftpHeader.opcode = SWAP16(TFTP_ACK_OPCODE); - tftpHeader.block_num = SWAP16(block_num); + tftpHeader.block_num = block_num; Send_TFTP_Packet((char*)&tftpHeader, 4); if (len < 512) @@ -231,7 +225,7 @@ * .KB_C_FN_DEFINITION_END */ static void -CheckForNewPacket(ip_header_t *pHeader) +CheckForNewPacket(void) { unsigned short *pFrameType; unsigned i; @@ -262,7 +256,8 @@ case PROTOCOL_ARP: p_ARP = (arp_header_t*)pData; - if (SWAP16(p_ARP->operation) == ARP_REPLY) { + i = SWAP16(p_ARP->operation); + if (i == ARP_REPLY) { // check if new server info is available if ((!serverMACSet) && @@ -274,30 +269,22 @@ p_memcpy((char*)serverMACAddr, (char*)p_ARP->sender_mac, 6); } - } - - if (SWAP16(p_ARP->operation) == ARP_REQUEST) { + } else if (i == ARP_REQUEST) { // ARP REPLY operation p_ARP->operation = SWAP16(ARP_REPLY); - // Fill the dest address and src address - for (i = 0; i <6; i++) { - // swap ethernet dest address and ethernet src address - pData[i] = pData[i+6]; - pData[i+6] = localMACAddr[i]; - // swap sender ethernet address and target ethernet address - pData[i+22] = localMACAddr[i]; - pData[i+32] = pData[i+6]; - } + // Swap the src/dst MAC addr + p_memcpy(p_ARP->dest_mac, p_ARP->src_mac, 6); + p_memcpy(p_ARP->src_mac, localMACAddr, 6); + + // Do IP and MAC addr at same time. + p_memcpy(p_ARP->target_mac, p_ARP->sender_mac, 10); + p_memcpy(p_ARP->sender_mac, localMACAddr, 6); + p_memcpy(p_ARP->sender_ip, (char *)&localIPAddr, 4); - // swap sender IP address and target IP address - for (i = 0; i<4; i++) { - pData[i+38] = pData[i+28]; - pData[i+28] = localIPAddr[i]; - } - - if (!(*AT91C_EMAC_TSR & AT91C_EMAC_BNQ)) break; + if (!(*AT91C_EMAC_TSR & AT91C_EMAC_BNQ)) + break; *AT91C_EMAC_TSR |= AT91C_EMAC_COMP; *AT91C_EMAC_TAR = (unsigned)pData; @@ -307,10 +294,7 @@ case PROTOCOL_IP: pIpHeader = (ip_header_t*)(pData + 14); - p_memcpy((char*)pHeader, (char*)pIpHeader,sizeof(ip_header_t)); - switch(pIpHeader->ip_p) { - case PROTOCOL_UDP: { udp_header_t *udpHdr; @@ -319,24 +303,24 @@ udpHdr = (udp_header_t*)((char*)pIpHeader+20); tftpHdr = (tftp_header_t*)((char*)udpHdr + 8); - if (SWAP16(udpHdr->dst_port) != localPort) + if (udpHdr->dst_port != localPort) break; - if (SWAP16(tftpHdr->opcode) != TFTP_DATA_OPCODE) + if (tftpHdr->opcode != SWAP16(TFTP_DATA_OPCODE)) break; if (ackBlock == -1) { - if (SWAP16(tftpHdr->block_num) != 1) + if (tftpHdr->block_num != SWAP16(1)) break; - serverPort = SWAP16(udpHdr->src_port); + serverPort = udpHdr->src_port; ackBlock = 0; } - if (serverPort != SWAP16(udpHdr->src_port)) + if (serverPort != udpHdr->src_port) break; TFTP_ACK_Data(tftpHdr->data, - SWAP16(tftpHdr->block_num), + tftpHdr->block_num, SWAP16(udpHdr->udp_len) - 12); } break; @@ -502,13 +486,7 @@ { // force update in case the IP has changed serverMACSet = 0; - - serverIPAddr[0] = (address >> 24) & 0xFF; - serverIPAddr[1] = (address >> 16) & 0xFF; - serverIPAddr[2] = (address >> 8) & 0xFF; - serverIPAddr[3] = (address >> 0) & 0xFF; - - serverIPSet = 1; + serverIPAddr = address; } @@ -523,13 +501,7 @@ { // force update in case the IP has changed serverMACSet = 0; - - localIPAddr[0] = (address >> 24) & 0xFF; - localIPAddr[1] = (address >> 16) & 0xFF; - localIPAddr[2] = (address >> 8) & 0xFF; - localIPAddr[3] = (address >> 0) & 0xFF; - - localIPSet = 1; + localIPAddr = address; } @@ -544,7 +516,6 @@ void TFTP_Download(unsigned address, char *filename) { - ip_header_t IpHeader; unsigned thisSeconds, running, state; int timeout, tickUpdate; @@ -556,7 +527,7 @@ return ; } - if ((!localMACSet) || (!localIPSet) || (!serverIPSet)) + if ((!localMACSet) || (!localIPAddr) || (!serverIPAddr)) return ; if (!MAC_init) { @@ -617,13 +588,13 @@ state = TFTP_WAITING_SERVER_MAC; timeout = 10; thisSeconds = GetSeconds(); - serverPort = 69; - ++localPort; + serverPort = SWAP16(69); + localPort++; /* In network byte order, but who cares */ ackBlock = -1; while (running && timeout) { - CheckForNewPacket(&IpHeader); + CheckForNewPacket(); tickUpdate = 0; @@ -685,9 +656,7 @@ p_rxBD = (receive_descriptor_t*)RX_BUFFER_START; localMACSet = 0; serverMACSet = 0; - localIPSet = 0; - serverIPSet = 0; - localPort = 0x8002; + localPort = SWAP16(0x8002); lastAddress = 0; lastSize = 0; MAC_init = 0;
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200604182208.k3IM8vG0024403>