Implement net_start_again() when NET_LWIP=y in a very similar way to
NET. This will be used in a future commit to determine if a failed
ping needs to be tried again on a different interface.

Signed-off-by: Jerome Forissier <jerome.foriss...@linaro.org>
---
 include/net-common.h |  3 +++
 include/net-legacy.h |  3 ---
 net/lwip/net-lwip.c  | 41 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/include/net-common.h b/include/net-common.h
index 1d507b13b06..52c2a6ce027 100644
--- a/include/net-common.h
+++ b/include/net-common.h
@@ -471,6 +471,9 @@ static inline struct in_addr env_get_ip(char *var)
 
 int net_init(void);
 
+/* Called when a network operation fails to know if it should be re-tried */
+int net_start_again(void);
+
 /* NET compatibility */
 enum proto_t;
 int net_loop(enum proto_t protocol);
diff --git a/include/net-legacy.h b/include/net-legacy.h
index bc0f0cde9fe..51780999a88 100644
--- a/include/net-legacy.h
+++ b/include/net-legacy.h
@@ -347,9 +347,6 @@ extern int net_ntp_time_offset;                     /* 
offset time from UTC */
 
 int net_loop(enum proto_t);
 
-/* Load failed.         Start again. */
-int net_start_again(void);
-
 /* Get size of the ethernet header when we send */
 int net_eth_hdr_size(void);
 
diff --git a/net/lwip/net-lwip.c b/net/lwip/net-lwip.c
index 1e60fa31bfb..5d862e038a9 100644
--- a/net/lwip/net-lwip.c
+++ b/net/lwip/net-lwip.c
@@ -21,6 +21,8 @@
 #if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
 void (*push_packet)(void *, int len) = 0;
 #endif
+static int net_try_count;
+static int net_restarted;
 int net_restart_wrap;
 static uchar net_pkt_buf[(PKTBUFSRX) * PKTSIZE_ALIGN + PKTALIGN];
 uchar *net_rx_packets[PKTBUFSRX];
@@ -339,3 +341,42 @@ u32_t sys_now(void)
 {
        return get_timer(0);
 }
+
+int net_start_again(void)
+{
+       char *nretry;
+       int retry_forever = 0;
+       unsigned long retrycnt = 0;
+
+       nretry = env_get("netretry");
+       if (nretry) {
+               if (!strcmp(nretry, "yes"))
+                       retry_forever = 1;
+               else if (!strcmp(nretry, "no"))
+                       retrycnt = 0;
+               else if (!strcmp(nretry, "once"))
+                       retrycnt = 1;
+               else
+                       retrycnt = simple_strtoul(nretry, NULL, 0);
+       } else {
+               retrycnt = 0;
+               retry_forever = 0;
+       }
+
+       if ((!retry_forever) && (net_try_count > retrycnt)) {
+               eth_halt();
+               /*
+                * We don't provide a way for the protocol to return an error,
+                * but this is almost always the reason.
+                */
+               return -ETIMEDOUT;
+       }
+
+       net_try_count++;
+
+       eth_halt();
+#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
+       eth_try_another(!net_restarted);
+#endif
+       return eth_init();
+}
-- 
2.43.0

Reply via email to