]> git.sur5r.net Git - u-boot/commitdiff
net: Refactor to protect access to the NetState variable
authorJoe Hershberger <joe.hershberger@ni.com>
Wed, 23 May 2012 07:59:14 +0000 (07:59 +0000)
committerJoe Hershberger <joe.hershberger@ni.com>
Wed, 23 May 2012 22:46:21 +0000 (17:46 -0500)
Changes to NetState now go through an accessor function called
net_set_state()

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>
drivers/net/netconsole.c
include/net.h
net/cdp.c
net/dns.c
net/net.c
net/nfs.c
net/ping.c
net/sntp.c
net/tftp.c

index ba02fd762ae77e4253077aba4665f8291ee96f5d..28bb955de49aa5beafb17c717c588f780326e6e8 100644 (file)
@@ -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 */
+       net_set_state(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 */
+               net_set_state(NETLOOP_SUCCESS); /* got input - quit net loop */
 }
 
 static void nc_timeout(void)
 {
-       NetState = NETLOOP_SUCCESS;
+       net_set_state(NETLOOP_SUCCESS);
 }
 
 void NcStart(void)
index 3214830831f78903eb5905c6915ad90b98d7b65b..8dd407e6cfe132008f7baf948d38ac02203c37a6 100644 (file)
@@ -391,12 +391,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 {
@@ -471,6 +465,20 @@ 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 */
+enum net_loop_state {
+       NETLOOP_CONTINUE,
+       NETLOOP_RESTART,
+       NETLOOP_SUCCESS,
+       NETLOOP_FAIL
+};
+static inline void net_set_state(enum net_loop_state state)
+{
+       extern enum net_loop_state net_state;
+
+       net_state = state;
+}
+
 /* Transmit "NetTxPacket" */
 static inline void NetSendPacket(uchar *pkt, int len)
 {
index 3dab6a1462945ad7bd58c25418b9bb56b88dae62..fa924097ac8750f996c5a80d1719b851f7440593 100644 (file)
--- a/net/cdp.c
+++ b/net/cdp.c
@@ -235,7 +235,7 @@ CDPTimeout(void)
        if (!CDPOK)
                NetStartAgain();
        else
-               NetState = NETLOOP_SUCCESS;
+               net_set_state(NETLOOP_SUCCESS);
 }
 
 static void
index cc7ed51691992f6a905e8cdebc2ca53a7789a0ea..cc0aa0a2dcc936ddf735671f4111ec63d5b060b5 100644 (file)
--- a/net/dns.c
+++ b/net/dns.c
@@ -98,7 +98,7 @@ static void
 DnsTimeout(void)
 {
        puts("Timeout\n");
-       NetState = NETLOOP_FAIL;
+       net_set_state(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;
+               net_set_state(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;
+               net_set_state(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;
+       net_set_state(NETLOOP_SUCCESS);
 }
 
 void
index e5fbda3e7f1decab768fef4df5c67baa99c2d48d..60be9782c93f9ba827b131738a444c9fa2b0385d 100644 (file)
--- a/net/net.c
+++ b/net/net.c
@@ -149,7 +149,7 @@ uchar               NetEtherNullAddr[6];
 void           (*push_packet)(void *, int len) = 0;
 #endif
 /* Network loop state */
-int            NetState;
+enum net_loop_state net_state;
 /* Tried all network devices */
 int            NetRestartWrap;
 /* Network loop restarted */
@@ -212,7 +212,7 @@ void net_auto_load(void)
                         * Just use BOOTP/RARP to configure system;
                         * Do not use TFTP to load the bootfile.
                         */
-                       NetState = NETLOOP_SUCCESS;
+                       net_set_state(NETLOOP_SUCCESS);
                        return;
                }
 #if defined(CONFIG_CMD_NFS)
@@ -290,7 +290,7 @@ int NetLoop(enum proto_t protocol)
 restart:
        memcpy(NetOurEther, eth_get_dev()->enetaddr, 6);
 
-       NetState = NETLOOP_CONTINUE;
+       net_set_state(NETLOOP_CONTINUE);
 
        /*
         *      Start the ball rolling with the given start function.  From
@@ -399,7 +399,7 @@ restart:
 
        /*
         *      Main packet reception loop.  Loop receiving packets until
-        *      someone sets `NetState' to a state that terminates.
+        *      someone sets `net_state' to a state that terminates.
         */
        for (;;) {
                WATCHDOG_RESET();
@@ -451,7 +451,7 @@ restart:
                }
 
 
-               switch (NetState) {
+               switch (net_state) {
 
                case NETLOOP_RESTART:
                        NetRestarted = 1;
@@ -475,6 +475,9 @@ restart:
 
                case NETLOOP_FAIL:
                        goto done;
+
+               case NETLOOP_CONTINUE:
+                       continue;
                }
        }
 
@@ -492,7 +495,7 @@ done:
 static void
 startAgainTimeout(void)
 {
-       NetState = NETLOOP_RESTART;
+       net_set_state(NETLOOP_RESTART);
 }
 
 static void
@@ -523,7 +526,7 @@ void NetStartAgain(void)
 
        if ((!retry_forever) && (NetTryCount >= retrycnt)) {
                eth_halt();
-               NetState = NETLOOP_FAIL;
+               net_set_state(NETLOOP_FAIL);
                return;
        }
 
@@ -540,10 +543,10 @@ void NetStartAgain(void)
                        NetSetTimeout(10000UL, startAgainTimeout);
                        NetSetHandler(startAgainHandler);
                } else {
-                       NetState = NETLOOP_FAIL;
+                       net_set_state(NETLOOP_FAIL);
                }
        } else {
-               NetState = NETLOOP_RESTART;
+               net_set_state(NETLOOP_RESTART);
        }
 }
 
index b6188fed36f2476b21704c632f14f8dad2d36292..db73e93380d9e35ca8935c85a8d839416410088a 100644 (file)
--- a/net/nfs.c
+++ b/net/nfs.c
@@ -41,7 +41,7 @@ static int nfs_len;
 static char dirfh[NFS_FHSIZE]; /* file handle of directory */
 static char filefh[NFS_FHSIZE]; /* file handle of kernel image */
 
-static int     NfsDownloadState;
+static enum net_loop_state nfs_download_state;
 static IPaddr_t NfsServerIP;
 static int     NfsSrvMountPort;
 static int     NfsSrvNfsPort;
@@ -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;
+                       net_set_state(NETLOOP_FAIL);
                } else {
                        puts("\ndone\n");
-                       NetState = NfsDownloadState;
+                       net_set_state(nfs_download_state);
                }
                break;
 
@@ -660,7 +660,7 @@ NfsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len)
                        NfsSend();
                } else {
                        if (!rlen)
-                               NfsDownloadState = NETLOOP_SUCCESS;
+                               nfs_download_state = NETLOOP_SUCCESS;
                        NfsState = STATE_UMOUNT_REQ;
                        NfsSend();
                }
@@ -673,13 +673,13 @@ void
 NfsStart(void)
 {
        debug("%s\n", __func__);
-       NfsDownloadState = NETLOOP_FAIL;
+       nfs_download_state = NETLOOP_FAIL;
 
        NfsServerIP = NetServerIP;
        nfs_path = (char *)nfs_path_buff;
 
        if (nfs_path == NULL) {
-               NetState = NETLOOP_FAIL;
+               net_set_state(NETLOOP_FAIL);
                puts("*** ERROR: Fail allocate memory\n");
                return;
        }
index f0026cc420368b175a78b035e63de1af2734a713..9c690d8c85f1dbb0ff4e0f4a5af1fdb0de8e269a 100644 (file)
@@ -72,7 +72,7 @@ static int ping_send(void)
 static void ping_timeout(void)
 {
        eth_halt();
-       NetState = NETLOOP_FAIL;        /* we did not get the reply */
+       net_set_state(NETLOOP_FAIL);    /* we did not get the reply */
 }
 
 void ping_start(void)
@@ -92,7 +92,7 @@ void ping_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
        case ICMP_ECHO_REPLY:
                src_ip = NetReadIP((void *)&ip->ip_src);
                if (src_ip == NetPingIP)
-                       NetState = NETLOOP_SUCCESS;
+                       net_set_state(NETLOOP_SUCCESS);
                return;
        case ICMP_ECHO_REQUEST:
                debug("Got ICMP ECHO REQUEST, return "
index 07d036d41de85ab14108217fa1f63ada616dc9f6..48ce07f18cdb05c5dbd7ffc99eaab9099cf5957b 100644 (file)
@@ -45,7 +45,7 @@ static void
 SntpTimeout(void)
 {
        puts("Timeout\n");
-       NetState = NETLOOP_FAIL;
+       net_set_state(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;
+       net_set_state(NETLOOP_SUCCESS);
 }
 
 void
index a04a832f8c78742fc66f268ef50a4355196b108e..bf32eabe88f2172c731f5b4baeaf6608f6eed029 100644 (file)
@@ -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;
+                       net_set_state(NETLOOP_FAIL);
                        return;
                }
        } else
@@ -300,7 +300,7 @@ static void tftp_complete(void)
        }
 #endif
        puts("\ndone\n");
-       NetState = NETLOOP_SUCCESS;
+       net_set_state(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;
+                               net_set_state(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;
+                       net_set_state(NETLOOP_FAIL);
                        break;
                case TFTP_ERR_UNDEFINED:
                case TFTP_ERR_DISK_FULL: