[U-Boot] [PATCH 16/28] net: Refactor to protect access to the NetState variable
Joe Hershberger
joe.hershberger at ni.com
Fri Jan 20 01:53:19 CET 2012
Changes to NetState now go through an accessor function called
NetSetState()
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>
---
drivers/net/netconsole.c | 6 +++---
include/net.h | 13 +++++++------
net/cdp.c | 2 +-
net/dns.c | 8 ++++----
net/net.c | 21 ++++++++++++++-------
net/nfs.c | 6 +++---
net/ping.c | 4 ++--
net/sntp.c | 4 ++--
net/tftp.c | 8 ++++----
9 files changed, 40 insertions(+), 32 deletions(-)
diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c
index ba02fd7..89b5956 100644
--- a/drivers/net/netconsole.c
+++ b/drivers/net/netconsole.c
@@ -44,19 +44,19 @@ static void nc_wait_arp_handler(uchar *pkt, unsigned dest,
IPaddr_t sip, unsigned src,
unsigned len)
{
- NetState = NETLOOP_SUCCESS; /* got arp reply - quit net loop */
+ NetSetState(NETLOOP_SUCCESS); /* got arp reply - quit net loop */
}
static void nc_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
unsigned len)
{
if (input_size)
- NetState = NETLOOP_SUCCESS; /* got input - quit net loop */
+ NetSetState(NETLOOP_SUCCESS); /* got input - quit net loop */
}
static void nc_timeout(void)
{
- NetState = NETLOOP_SUCCESS;
+ NetSetState(NETLOOP_SUCCESS);
}
void NcStart(void)
diff --git a/include/net.h b/include/net.h
index 19e9463..fe49ff7 100644
--- a/include/net.h
+++ b/include/net.h
@@ -364,12 +364,6 @@ extern uchar NetEtherNullAddr[6];
extern ushort NetOurVLAN; /* Our VLAN */
extern ushort NetOurNativeVLAN; /* Our Native VLAN */
-extern int NetState; /* Network loop state */
-#define NETLOOP_CONTINUE 1
-#define NETLOOP_RESTART 2
-#define NETLOOP_SUCCESS 3
-#define NETLOOP_FAIL 4
-
extern int NetRestartWrap; /* Tried all network devices */
enum proto_t {
@@ -435,6 +429,13 @@ extern void NetSetHandler(rxhand_f *); /* Set RX packet handler */
extern void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */
extern void NetSetTimeout(ulong, thand_f *);/* Set timeout handler */
+/* Network loop state */
+extern void NetSetState(int state);
+#define NETLOOP_CONTINUE 1
+#define NETLOOP_RESTART 2
+#define NETLOOP_SUCCESS 3
+#define NETLOOP_FAIL 4
+
/* Transmit "NetTxPacket" */
extern void NetSendPacket(uchar *, int);
diff --git a/net/cdp.c b/net/cdp.c
index 305ecad..31f9ce7 100644
--- a/net/cdp.c
+++ b/net/cdp.c
@@ -234,7 +234,7 @@ CDPTimeout(void)
if (!CDPOK)
NetStartAgain();
else
- NetState = NETLOOP_SUCCESS;
+ NetSetState(NETLOOP_SUCCESS);
}
static void
diff --git a/net/dns.c b/net/dns.c
index cc7ed51..0b655cc 100644
--- a/net/dns.c
+++ b/net/dns.c
@@ -98,7 +98,7 @@ static void
DnsTimeout(void)
{
puts("Timeout\n");
- NetState = NETLOOP_FAIL;
+ NetSetState(NETLOOP_FAIL);
}
static void
@@ -128,7 +128,7 @@ DnsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len)
/* Received 0 answers */
if (header->nanswers == 0) {
puts("DNS: host not found\n");
- NetState = NETLOOP_SUCCESS;
+ NetSetState(NETLOOP_SUCCESS);
return;
}
@@ -141,7 +141,7 @@ DnsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len)
/* We sent query class 1, query type 1 */
if (&p[5] > e || get_unaligned_be16(p+1) != DNS_A_RECORD) {
puts("DNS: response was not an A record\n");
- NetState = NETLOOP_SUCCESS;
+ NetSetState(NETLOOP_SUCCESS);
return;
}
@@ -191,7 +191,7 @@ DnsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len)
puts("server responded with invalid IP number\n");
}
- NetState = NETLOOP_SUCCESS;
+ NetSetState(NETLOOP_SUCCESS);
}
void
diff --git a/net/net.c b/net/net.c
index 16cbadc..a92df90 100644
--- a/net/net.c
+++ b/net/net.c
@@ -154,7 +154,7 @@ uchar NetEtherNullAddr[6];
void (*push_packet)(volatile void *, int len) = 0;
#endif
/* Network loop state */
-int NetState;
+static int NetState;
/* Tried all network devices */
int NetRestartWrap;
/* Network loop restarted */
@@ -217,7 +217,7 @@ void net_auto_load(void)
* Just use BOOTP/RARP to configure system;
* Do not use TFTP to load the bootfile.
*/
- NetState = NETLOOP_SUCCESS;
+ NetSetState(NETLOOP_SUCCESS);
return;
}
#if defined(CONFIG_CMD_NFS)
@@ -296,7 +296,7 @@ int NetLoop(enum proto_t protocol)
restart:
memcpy(NetOurEther, eth_get_dev()->enetaddr, 6);
- NetState = NETLOOP_CONTINUE;
+ NetSetState(NETLOOP_CONTINUE);
/*
* Start the ball rolling with the given start function. From
@@ -498,7 +498,7 @@ done:
static void
startAgainTimeout(void)
{
- NetState = NETLOOP_RESTART;
+ NetSetState(NETLOOP_RESTART);
}
static void
@@ -529,7 +529,7 @@ void NetStartAgain(void)
if ((!retry_forever) && (NetTryCount >= retrycnt)) {
eth_halt();
- NetState = NETLOOP_FAIL;
+ NetSetState(NETLOOP_FAIL);
return;
}
@@ -546,10 +546,10 @@ void NetStartAgain(void)
NetSetTimeout(10000UL, startAgainTimeout);
NetSetHandler(startAgainHandler);
} else {
- NetState = NETLOOP_FAIL;
+ NetSetState(NETLOOP_FAIL);
}
} else {
- NetState = NETLOOP_RESTART;
+ NetSetState(NETLOOP_RESTART);
}
}
@@ -592,6 +592,13 @@ NetSetTimeout(ulong iv, thand_f *f)
void
+NetSetState(int state)
+{
+ NetState = state;
+}
+
+
+void
NetSendPacket(uchar *pkt, int len)
{
(void) eth_send(pkt, len);
diff --git a/net/nfs.c b/net/nfs.c
index b6188fe..e366505 100644
--- a/net/nfs.c
+++ b/net/nfs.c
@@ -613,10 +613,10 @@ NfsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len)
case STATE_UMOUNT_REQ:
if (nfs_umountall_reply(pkt, len)) {
puts("*** ERROR: Cannot umount\n");
- NetState = NETLOOP_FAIL;
+ NetSetState(NETLOOP_FAIL);
} else {
puts("\ndone\n");
- NetState = NfsDownloadState;
+ NetSetState(NfsDownloadState);
}
break;
@@ -679,7 +679,7 @@ NfsStart(void)
nfs_path = (char *)nfs_path_buff;
if (nfs_path == NULL) {
- NetState = NETLOOP_FAIL;
+ NetSetState(NETLOOP_FAIL);
puts("*** ERROR: Fail allocate memory\n");
return;
}
diff --git a/net/ping.c b/net/ping.c
index 7d03072..795d5c9 100644
--- a/net/ping.c
+++ b/net/ping.c
@@ -73,7 +73,7 @@ static void
PingTimeout(void)
{
eth_halt();
- NetState = NETLOOP_FAIL; /* we did not get the reply */
+ NetSetState(NETLOOP_FAIL); /* we did not get the reply */
}
void
@@ -94,7 +94,7 @@ void PingReceive(struct Ethernet_t *et, struct IP_UDP_t *ip, int len)
case ICMP_ECHO_REPLY:
src_ip = NetReadIP((void *)&ip->ip_src);
if (src_ip == NetPingIP)
- NetState = NETLOOP_SUCCESS;
+ NetSetState(NETLOOP_SUCCESS);
return;
case ICMP_ECHO_REQUEST:
debug("Got ICMP ECHO REQUEST, return "
diff --git a/net/sntp.c b/net/sntp.c
index 7997f98..f5fa6f7 100644
--- a/net/sntp.c
+++ b/net/sntp.c
@@ -45,7 +45,7 @@ static void
SntpTimeout(void)
{
puts("Timeout\n");
- NetState = NETLOOP_FAIL;
+ NetSetState(NETLOOP_FAIL);
return;
}
@@ -76,7 +76,7 @@ SntpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
tm.tm_year, tm.tm_mon, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec);
- NetState = NETLOOP_SUCCESS;
+ NetSetState(NETLOOP_SUCCESS);
}
void
diff --git a/net/tftp.c b/net/tftp.c
index a04a832..49ddbd7 100644
--- a/net/tftp.c
+++ b/net/tftp.c
@@ -177,7 +177,7 @@ store_block(unsigned block, uchar *src, unsigned len)
rc = flash_write((char *)src, (ulong)(load_addr+offset), len);
if (rc) {
flash_perror(rc);
- NetState = NETLOOP_FAIL;
+ NetSetState(NETLOOP_FAIL);
return;
}
} else
@@ -300,7 +300,7 @@ static void tftp_complete(void)
}
#endif
puts("\ndone\n");
- NetState = NETLOOP_SUCCESS;
+ NetSetState(NETLOOP_SUCCESS);
}
static void
@@ -627,7 +627,7 @@ TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
puts("\nMulticast tftp done\n");
mcast_cleanup();
- NetState = NETLOOP_SUCCESS;
+ NetSetState(NETLOOP_SUCCESS);
}
} else
#endif
@@ -644,7 +644,7 @@ TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
case TFTP_ERR_ACCESS_DENIED:
puts("Not retrying...\n");
eth_halt();
- NetState = NETLOOP_FAIL;
+ NetSetState(NETLOOP_FAIL);
break;
case TFTP_ERR_UNDEFINED:
case TFTP_ERR_DISK_FULL:
--
1.6.0.2
More information about the U-Boot
mailing list