Code clean up in NetLib:

1. Add GLOBAL_REMOVE_IF_UNREFERENCED to all globals
2. Update NTOHL and NTOHS to be BaseLib func SwapBytes32/SwapBytes16
3. Remove duplicate NET_SWAP_SHORT (to use NTOHS instead)

git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@9648 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
xdu2 2009-12-30 13:44:11 +00:00
parent 9ae650ef6c
commit 1204fe8319
13 changed files with 473 additions and 475 deletions

View File

@ -1,5 +1,5 @@
/** @file
Ihis library is only intended to be used by UEFI network stack modules.
This library is only intended to be used by UEFI network stack modules.
It provides basic functions for the UEFI network stack.
Copyright (c) 2005 - 2009, Intel Corporation
@ -18,6 +18,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#include <Protocol/Ip6.h>
#include <Library/BaseLib.h>
typedef UINT32 IP4_ADDR;
typedef UINT32 TCP_SEQNO;
typedef UINT16 TCP_PORTNO;
@ -160,15 +162,11 @@ typedef struct {
#define NET_MAC_IS_MULTICAST(Mac, BMac, Len) \
(((*((UINT8 *) Mac) & 0x01) == 0x01) && (!NET_MAC_EQUAL (Mac, BMac, Len)))
#define NTOHL(x) (UINT32)((((UINT32) (x) & 0xff) << 24) | \
(((UINT32) (x) & 0xff00) << 8) | \
(((UINT32) (x) & 0xff0000) >> 8) | \
(((UINT32) (x) & 0xff000000) >> 24))
#define NTOHL(x) SwapBytes32 (x)
#define HTONL(x) NTOHL(x)
#define NTOHS(x) (UINT16)((((UINT16) (x) & 0xff) << 8) | \
(((UINT16) (x) & 0xff00) >> 8))
#define NTOHS(x) SwapBytes16 (x)
#define HTONS(x) NTOHS(x)
#define NTOHLL(x) SwapBytes64 (x)
@ -211,7 +209,7 @@ typedef struct {
//
#define NET_SYSLOG_FACILITY 16 // Syslog local facility local use
#define NET_SYSLOG_PACKET_LEN 512
#define NET_SYSLOG_TX_TIMEOUT 500 *1000 *10 // 500ms
#define NET_SYSLOG_TX_TIMEOUT (500 * 1000 * 10) // 500ms
#define NET_DEBUG_MSG_LEN 470 // 512 - (ether+ip4+udp4 head length)
//
@ -978,8 +976,8 @@ NetLibDestroyServiceChild (
@param[out] MacString The pointer to store the address of the string
representation of the mac address.
@retval EFI_SUCCESS Converted the mac address a unicode string successfully.
@retval EFI_OUT_OF_RESOURCES There are not enough memory resources.
@retval EFI_SUCCESS Convert the mac address a unicode string successfully.
@retval EFI_OUT_OF_RESOURCES There are not enough memory resource.
@retval Others Failed to open the simple network protocol.
**/
@ -1111,9 +1109,6 @@ NetLibDefaultUnload (
#define NET_CHECK_SIGNATURE(PData, SIGNATURE) \
ASSERT (((PData) != NULL) && ((PData)->Signature == (SIGNATURE)))
#define NET_SWAP_SHORT(Value) \
((((Value) & 0xff) << 8) | (((Value) >> 8) & 0xff))
//
// Single memory block in the vector.
//

View File

@ -24,12 +24,12 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#include <Library/DpcLib.h>
LIST_ENTRY mActiveIpIoList = {
GLOBAL_REMOVE_IF_UNREFERENCED LIST_ENTRY mActiveIpIoList = {
&mActiveIpIoList,
&mActiveIpIoList
};
EFI_IP4_CONFIG_DATA mIp4IoDefaultIpConfigData = {
GLOBAL_REMOVE_IF_UNREFERENCED EFI_IP4_CONFIG_DATA mIp4IoDefaultIpConfigData = {
EFI_IP_PROTO_UDP,
FALSE,
TRUE,
@ -46,7 +46,7 @@ EFI_IP4_CONFIG_DATA mIp4IoDefaultIpConfigData = {
0
};
EFI_IP6_CONFIG_DATA mIp6IoDefaultIpConfigData = {
GLOBAL_REMOVE_IF_UNREFERENCED EFI_IP6_CONFIG_DATA mIp6IoDefaultIpConfigData = {
EFI_IP_PROTO_UDP,
FALSE,
TRUE,
@ -60,7 +60,7 @@ EFI_IP6_CONFIG_DATA mIp6IoDefaultIpConfigData = {
0
};
ICMP_ERROR_INFO mIcmpErrMap[10] = {
GLOBAL_REMOVE_IF_UNREFERENCED ICMP_ERROR_INFO mIcmpErrMap[10] = {
{FALSE, TRUE }, // ICMP_ERR_UNREACH_NET
{FALSE, TRUE }, // ICMP_ERR_UNREACH_HOST
{TRUE, TRUE }, // ICMP_ERR_UNREACH_PROTOCOL
@ -73,7 +73,7 @@ ICMP_ERROR_INFO mIcmpErrMap[10] = {
{FALSE, TRUE } // ICMP_ERR_PARAMPROB
};
ICMP_ERROR_INFO mIcmp6ErrMap[10] = {
GLOBAL_REMOVE_IF_UNREFERENCED ICMP_ERROR_INFO mIcmp6ErrMap[10] = {
{FALSE, TRUE}, // ICMP6_ERR_UNREACH_NET
{FALSE, TRUE}, // ICMP6_ERR_UNREACH_HOST
{TRUE, TRUE}, // ICMP6_ERR_UNREACH_PROTOCOL

View File

@ -16,6 +16,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#include <Protocol/DriverBinding.h>
#include <Protocol/ServiceBinding.h>
#include <Protocol/SimpleNetwork.h>
#include <Protocol/ManagedNetwork.h>
#include <Protocol/HiiConfigRouting.h>
#include <Protocol/ComponentName.h>
#include <Protocol/ComponentName2.h>
@ -33,14 +34,12 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#include <Library/HiiLib.h>
#include <Library/PrintLib.h>
GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mNetLibHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
#define NIC_ITEM_CONFIG_SIZE sizeof (NIC_IP4_CONFIG_INFO) + sizeof (EFI_IP4_ROUTE_TABLE) * MAX_IP4_CONFIG_IN_VARIABLE
//
// All the supported IP4 maskes in host byte order.
//
IP4_ADDR gIp4AllMasks[IP4_MASK_NUM] = {
GLOBAL_REMOVE_IF_UNREFERENCED IP4_ADDR gIp4AllMasks[IP4_MASK_NUM] = {
0x00000000,
0x80000000,
0xC0000000,
@ -79,26 +78,25 @@ IP4_ADDR gIp4AllMasks[IP4_MASK_NUM] = {
0xFFFFFFFF,
};
EFI_IPv4_ADDRESS mZeroIp4Addr = {{0, 0, 0, 0}};
GLOBAL_REMOVE_IF_UNREFERENCED EFI_IPv4_ADDRESS mZeroIp4Addr = {{0, 0, 0, 0}};
//
// Any error level digitally larger than mNetDebugLevelMax
// will be silently discarded.
//
UINTN mNetDebugLevelMax = NETDEBUG_LEVEL_ERROR;
UINT32 mSyslogPacketSeq = 0xDEADBEEF;
GLOBAL_REMOVE_IF_UNREFERENCED UINTN mNetDebugLevelMax = NETDEBUG_LEVEL_ERROR;
GLOBAL_REMOVE_IF_UNREFERENCED UINT32 mSyslogPacketSeq = 0xDEADBEEF;
//
// You can change mSyslogDstMac mSyslogDstIp and mSyslogSrcIp
// here to direct the syslog packets to the syslog deamon. The
// default is broadcast to both the ethernet and IP.
//
UINT8 mSyslogDstMac[NET_ETHER_ADDR_LEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
UINT32 mSyslogDstIp = 0xffffffff;
UINT32 mSyslogSrcIp = 0;
GLOBAL_REMOVE_IF_UNREFERENCED UINT8 mSyslogDstMac[NET_ETHER_ADDR_LEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
GLOBAL_REMOVE_IF_UNREFERENCED UINT32 mSyslogDstIp = 0xffffffff;
GLOBAL_REMOVE_IF_UNREFERENCED UINT32 mSyslogSrcIp = 0;
CHAR8 *
mMonthName[] = {
GLOBAL_REMOVE_IF_UNREFERENCED CHAR8 *mMonthName[] = {
"Jan",
"Feb",
"Mar",
@ -181,12 +179,12 @@ SyslogLocateSnp (
time it is called to avoid the problem if SNP is unloaded.
This code snip is copied from MNP.
@param[in] Packet - The Syslog packet
@param[in] Length - The length of the packet
@param[in] Packet The Syslog packet
@param[in] Length The length of the packet
@retval EFI_DEVICE_ERROR - Failed to locate a usable SNP protocol
@retval EFI_TIMEOUT - Timeout happened to send the packet.
@retval EFI_SUCCESS - Packet is sent.
@retval EFI_DEVICE_ERROR Failed to locate a usable SNP protocol
@retval EFI_TIMEOUT Timeout happened to send the packet.
@retval EFI_SUCCESS Packet is sent.
**/
EFI_STATUS
@ -284,17 +282,15 @@ ON_EXIT:
Build a syslog packet, including the Ethernet/Ip/Udp headers
and user's message.
@param[in] Level - Syslog servity level
@param[in] Module - The module that generates the log
@param[in] File - The file that contains the current log
@param[in] Line - The line of code in the File that contains the current log
@param[in] Message - The log message
@param[in] BufLen - The lenght of the Buf
@param[out] Buf - The buffer to put the packet data
@param[in] Level Syslog servity level
@param[in] Module The module that generates the log
@param[in] File The file that contains the current log
@param[in] Line The line of code in the File that contains the current log
@param[in] Message The log message
@param[in] BufLen The lenght of the Buf
@param[out] Buf The buffer to put the packet data
Returns:
The length of the syslog packet built.
@return The length of the syslog packet built.
**/
UINT32
@ -1811,6 +1807,7 @@ NetLibGetMacString (
EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
EFI_SIMPLE_NETWORK_MODE *Mode;
CHAR16 *MacAddress;
UINT8 *HwAddress;
UINTN Index;
*MacString = NULL;
@ -1840,18 +1837,18 @@ NetLibGetMacString (
if (MacAddress == NULL) {
return EFI_OUT_OF_RESOURCES;
}
*MacString = MacAddress;
//
// Convert the mac address into a unicode string.
//
HwAddress = Mode->CurrentAddress.Addr;
for (Index = 0; Index < Mode->HwAddressSize; Index++) {
MacAddress[Index * 2] = (CHAR16) mNetLibHexStr[(Mode->CurrentAddress.Addr[Index] >> 4) & 0x0F];
MacAddress[Index * 2 + 1] = (CHAR16) mNetLibHexStr[Mode->CurrentAddress.Addr[Index] & 0x0F];
MacAddress += UnicodeValueToString (MacAddress, PREFIX_ZERO | RADIX_HEX, *(HwAddress++), 2);
}
MacAddress[Mode->HwAddressSize * 2] = L'\0';
*MacString = MacAddress;
return EFI_SUCCESS;
}

View File

@ -54,6 +54,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
[Protocols]
gEfiSimpleNetworkProtocolGuid # PROTOCOL ALWAYS_CONSUMED
gEfiManagedNetworkProtocolGuid # PROTOCOL ALWAYS_CONSUMED
gEfiManagedNetworkServiceBindingProtocolGuid # PROTOCOL ALWAYS_CONSUMED
gEfiComponentNameProtocolGuid # PROTOCOL ALWAYS_CONSUMED
gEfiComponentName2ProtocolGuid # PROTOCOL ALWAYS_CONSUMED
gEfiHiiConfigRoutingProtocolGuid # PROTOCOL ALWAYS_CONSUMED

View File

@ -1756,7 +1756,7 @@ NetbufChecksum (
// The checksum starts with an odd byte, swap
// the checksum before added to total checksum
//
BlockSum = (UINT16) NET_SWAP_SHORT (BlockSum);
BlockSum = SwapBytes16 (BlockSum);
}
TotalSum = NetAddChecksum (BlockSum, TotalSum);

View File

@ -1857,7 +1857,7 @@ IScsiNewScsiCmdPdu (
CopyMem (ScsiCmd->Cdb, Packet->Cdb, sizeof (ScsiCmd->Cdb));
if (Packet->CdbLength > 16) {
Header->Length = NTOHS (Packet->CdbLength - 15);
Header->Length = NTOHS ((UINT16) (Packet->CdbLength - 15));
Header->Type = ISCSI_AHS_TYPE_EXT_CDB;
CopyMem (Header + 1, (UINT8 *) Packet->Cdb + 16, Packet->CdbLength - 16);

View File

@ -105,6 +105,8 @@ Ip4ProcessIcmpRedirect (
IP4_ROUTE_CACHE_ENTRY *CacheEntry;
IP4_INTERFACE *IpIf;
IP4_ADDR Gateway;
IP4_ADDR Src;
IP4_ADDR Dst;
//
// Find the interface whose IP address is the source of the
@ -133,11 +135,9 @@ Ip4ProcessIcmpRedirect (
continue;
}
CacheEntry = Ip4FindRouteCache (
Ip4Instance->RouteTable,
NTOHL (Icmp->IpHead.Dst),
NTOHL (Icmp->IpHead.Src)
);
Dst = NTOHL (Icmp->IpHead.Dst);
Src = NTOHL (Icmp->IpHead.Src);
CacheEntry = Ip4FindRouteCache (Ip4Instance->RouteTable, Dst, Src);
//
// Only update the route cache's gateway if the source of the

View File

@ -561,6 +561,10 @@ Ip4AutoConfigCallBackDpc (
EFI_STATUS Status;
UINTN Len;
UINT32 Index;
IP4_ADDR StationAddress;
IP4_ADDR SubnetMask;
IP4_ADDR SubnetAddress;
IP4_ADDR GatewayAddress;
IpSb = (IP4_SERVICE *) Context;
NET_CHECK_SIGNATURE (IpSb, IP4_SERVICE_SIGNATURE);
@ -646,11 +650,9 @@ Ip4AutoConfigCallBackDpc (
// Set the default interface's address, then add a directed
// route for it, that is, the route whose nexthop is zero.
//
Status = Ip4SetAddress (
IpIf,
EFI_NTOHL (Data->StationAddress),
EFI_NTOHL (Data->SubnetMask)
);
StationAddress = EFI_NTOHL (Data->StationAddress);
SubnetMask = EFI_NTOHL (Data->SubnetMask);
Status = Ip4SetAddress (IpIf, StationAddress, SubnetMask);
if (EFI_ERROR (Status)) {
goto ON_EXIT;
@ -658,8 +660,8 @@ Ip4AutoConfigCallBackDpc (
Ip4AddRoute (
IpSb->DefaultRouteTable,
EFI_NTOHL (Data->StationAddress),
EFI_NTOHL (Data->SubnetMask),
StationAddress,
SubnetMask,
IP4_ALLZERO_ADDRESS
);
@ -669,12 +671,10 @@ Ip4AutoConfigCallBackDpc (
for (Index = 0; Index < Data->RouteTableSize; Index++) {
RouteEntry = &Data->RouteTable[Index];
Ip4AddRoute (
IpSb->DefaultRouteTable,
EFI_NTOHL (RouteEntry->SubnetAddress),
EFI_NTOHL (RouteEntry->SubnetMask),
EFI_NTOHL (RouteEntry->GatewayAddress)
);
SubnetAddress = EFI_NTOHL (RouteEntry->SubnetAddress);
SubnetMask = EFI_NTOHL (RouteEntry->SubnetMask);
GatewayAddress = EFI_NTOHL (RouteEntry->GatewayAddress);
Ip4AddRoute (IpSb->DefaultRouteTable, SubnetAddress, SubnetMask, GatewayAddress);
}
IpSb->State = IP4_SERVICE_CONFIGED;

View File

@ -73,7 +73,7 @@ Ip4PrependHead (
PacketHead->Ver = 4;
PacketHead->HeadLen = (UINT8) (HeadLen >> 2);
PacketHead->Tos = Head->Tos;
PacketHead->TotalLen = HTONS (Packet->TotalSize);
PacketHead->TotalLen = HTONS ((UINT16) Packet->TotalSize);
PacketHead->Id = HTONS (Head->Id);
PacketHead->Fragment = HTONS (Head->Fragment);
PacketHead->Checksum = 0;

View File

@ -799,7 +799,7 @@ TcpInput (
Tcb = TcpCloneTcb (Parent);
if (Tcb == NULL) {
DEBUG ((EFI_D_ERROR, "TcpInput: discard a segment because"
"failed to clone a child for TCB%x\n", Tcb));
" failed to clone a child for TCB%x\n", Tcb));
goto DISCARD;
}

View File

@ -1090,18 +1090,22 @@ TcpInstallDevicePath (
TCP_CB *Tcb;
IPv4_DEVICE_PATH Ip4DPathNode;
EFI_STATUS Status;
TCP_PORTNO LocalPort;
TCP_PORTNO RemotePort;
TcpProto = (TCP4_PROTO_DATA *) Sock->ProtoReserved;
TcpService = TcpProto->TcpService;
Tcb = TcpProto->TcpPcb;
LocalPort = NTOHS (Tcb->LocalEnd.Port);
RemotePort = NTOHS (Tcb->RemoteEnd.Port);
NetLibCreateIPv4DPathNode (
&Ip4DPathNode,
TcpService->ControllerHandle,
Tcb->LocalEnd.Ip,
NTOHS (Tcb->LocalEnd.Port),
LocalPort,
Tcb->RemoteEnd.Ip,
NTOHS (Tcb->RemoteEnd.Port),
RemotePort,
EFI_IP_PROTO_TCP,
Tcb->UseDefaultAddr
);

View File

@ -586,7 +586,7 @@ Udp4Transmit (
//
Udp4Header->SrcPort = HTONS (ConfigData->StationPort);
Udp4Header->DstPort = HTONS (ConfigData->RemotePort);
Udp4Header->Length = HTONS (Packet->TotalSize);
Udp4Header->Length = HTONS ((UINT16) Packet->TotalSize);
Udp4Header->Checksum = 0;
UdpSessionData = TxData->UdpSessionData;

View File

@ -1182,7 +1182,7 @@ PxeBcDiscvBootService (
Xid = NET_RANDOM (NetRandomInitSeed ());
Token.Packet->Dhcp4.Header.Xid = HTONL(Xid);
Token.Packet->Dhcp4.Header.Reserved = HTONS((IsBCast) ? 0x8000 : 0);
Token.Packet->Dhcp4.Header.Reserved = HTONS((UINT16) ((IsBCast) ? 0x8000 : 0));
CopyMem (&Token.Packet->Dhcp4.Header.ClientAddr, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS));
Token.RemotePort = Sport;