From e5d1d82fc5a104895cf8d04ec0d90ce4cb9f86b2 Mon Sep 17 00:00:00 2001 From: Michele Di Giorgio Date: Tue, 9 Feb 2016 16:17:47 +0000 Subject: [PATCH 1/4] libs/utils/env: add ping_time parameter in reboot When rebooting, the reboot function repeatedly pings the target. However, rebooting might require some seconds and in order to avoid pinging the machine before it actually reboots, we can set the ping_time option in target.config to tell the function how long to wait before pinging. Also, a comment has been added in target.config to specify what the file represents and to highlight that commented parameters are optional. --- libs/utils/env.py | 7 +++++-- target.config | 13 ++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/libs/utils/env.py b/libs/utils/env.py index 8d8ba5f42..619be6e4a 100644 --- a/libs/utils/env.py +++ b/libs/utils/env.py @@ -644,11 +644,14 @@ class TestEnv(ShareState): 'HostResolver', host, ipaddr) return (host, ipaddr) - def reboot(self, reboot_time=120): + def reboot(self, reboot_time=120, ping_time=15): # Send remote target a reboot command if self._feature('no-reboot'): logging.warning('%14s - Reboot disabled by conf features', 'Reboot') else: + if 'ping_time' in self.conf: + ping_time = int(self.conf['ping_time']) + self.target.execute('sleep 2 && reboot -f &', as_root=True) # Wait for the target to complete the reboot @@ -659,7 +662,7 @@ class TestEnv(ShareState): elapsed = 0 start = time.time() while elapsed <= reboot_time: - time.sleep(5) + time.sleep(ping_time) logging.debug('%14s - Trying to connect to [%s] target...', 'Reboot', self.ip) if os.system(ping_cmd) == 0: diff --git a/target.config b/target.config index ade4bd746..8ae5f4cfe 100644 --- a/target.config +++ b/target.config @@ -1,3 +1,11 @@ +/* target.config */ +/* */ +/* Configuration file to specify target parameters such as: the */ +/* kind of platform/board, and login credentials. */ +/* */ +/* NOTE: all commented parameters are optional and report their */ +/* default value. */ + { /* Platform */ /* - linux : accessed via SSH connection */ @@ -19,7 +27,7 @@ "username" : "root", /* Login credentials */ - /* You can specified either a password or keyfile */ + /* You can specify either a password or keyfile */ "password" : "", // "keyfile" : "/complete/path/of/your/keyfile", @@ -49,6 +57,9 @@ /* Binary tools to install by default for all experiments */ "tools" : [], + /* Wait time before trying to access the target after reboot */ + // "ping_time" : "15", + /* List of test environment features to enable */ /* no-kernel : do not deploy kernel/dtb images */ /* no-reboot : do not force reboot the target at each */ -- GitLab From da58c39830ef4cdc1e311def5d6273765204072c Mon Sep 17 00:00:00 2001 From: Michele Di Giorgio Date: Thu, 11 Feb 2016 12:27:41 +0000 Subject: [PATCH 2/4] target.config: add reboot_time parameter This parameter is optional and specifies the maximum time to wait before accessing the target after a reboot. --- target.config | 3 +++ 1 file changed, 3 insertions(+) diff --git a/target.config b/target.config index 8ae5f4cfe..18f5c1fc4 100644 --- a/target.config +++ b/target.config @@ -60,6 +60,9 @@ /* Wait time before trying to access the target after reboot */ // "ping_time" : "15", + /* Total time to wait after rebooting the target */ + // "reboot_time" : "120", + /* List of test environment features to enable */ /* no-kernel : do not deploy kernel/dtb images */ /* no-reboot : do not force reboot the target at each */ -- GitLab From 3ab7ca569093e1056f27a206ba3037c27e90a608 Mon Sep 17 00:00:00 2001 From: Michele Di Giorgio Date: Mon, 15 Feb 2016 12:43:28 +0000 Subject: [PATCH 3/4] libs/utils/env: refactoring for parsing ARP cache Host IP or MAC address resolution has been improved by using an auxiliary function that reads the output of the ARP cache and retrieves MAC and IP of the target host. --- libs/utils/env.py | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/libs/utils/env.py b/libs/utils/env.py index 619be6e4a..4033a202d 100644 --- a/libs/utils/env.py +++ b/libs/utils/env.py @@ -615,6 +615,12 @@ class TestEnv(ShareState): logging.debug('%14s - %s', 'HostResolver', cmd) os.popen(cmd) + return self.parse_arp_cache(host) + + def parse_arp_cache(self, host): + output = os.popen(r'arp -n') + ipaddr = None + macaddr = None if ':' in host: # Assuming this is a MAC address # TODO add a suitable check on MAC address format @@ -622,6 +628,13 @@ class TestEnv(ShareState): ARP_RE = re.compile( r'([^ ]*).*({}|{})'.format(host.lower(), host.upper()) ) + macaddr = host + for line in output: + match = ARP_RE.search(line) + if not match: + continue + ipaddr = match.group(1) + break else: # Assuming this is an IP address # TODO add a suitable check on IP address format @@ -629,20 +642,19 @@ class TestEnv(ShareState): ARP_RE = re.compile( r'{}.*ether *([0-9a-fA-F:]*)'.format(host) ) - - output = os.popen(r'arp -n') - ipaddr = '0.0.0.0' - for line in output: - match = ARP_RE.search(line) - if not match: - continue - ipaddr = match.group(1) - break - if ipaddr == '0.0.0.0': - raise ValueError('Unable to lookup for target IP address') + ipaddr = host + for line in output: + match = ARP_RE.search(line) + if not match: + continue + macaddr = match.group(1) + break + + if not ipaddr or not macaddr: + raise ValueError('Unable to lookup for target IP/MAC address') logging.info('%14s - Target (%s) at IP address: %s', - 'HostResolver', host, ipaddr) - return (host, ipaddr) + 'HostResolver', macaddr, ipaddr) + return (macaddr, ipaddr) def reboot(self, reboot_time=120, ping_time=15): # Send remote target a reboot command -- GitLab From 691cbfe6ead1b2082e47b87d46d33b70581dc664 Mon Sep 17 00:00:00 2001 From: Michele Di Giorgio Date: Mon, 15 Feb 2016 12:46:06 +0000 Subject: [PATCH 4/4] libs/utils/env: make reboot more robust by improving host resolution reboot has been made more robust by reading the ARP cache before shutting down the target so that we save target IP and MAC address. Then, reboot tries to ping the target using the IP, but in case the IP changes it will try to resolve the MAC address and use the new IP instead. --- libs/utils/env.py | 14 +++++++++++++- target.config | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/libs/utils/env.py b/libs/utils/env.py index 4033a202d..0785068c6 100644 --- a/libs/utils/env.py +++ b/libs/utils/env.py @@ -661,9 +661,16 @@ class TestEnv(ShareState): if self._feature('no-reboot'): logging.warning('%14s - Reboot disabled by conf features', 'Reboot') else: + if 'reboot_time' in self.conf: + reboot_time = int(self.conf['reboot_time']) + if 'ping_time' in self.conf: ping_time = int(self.conf['ping_time']) + # Before rebooting make sure to have IP and MAC addresses + # of the target + (self.mac, self.ip) = self.parse_arp_cache(self.ip) + self.target.execute('sleep 2 && reboot -f &', as_root=True) # Wait for the target to complete the reboot @@ -681,7 +688,12 @@ class TestEnv(ShareState): break elapsed = time.time() - start if elapsed > reboot_time: - logging.warning('%14s - target [%s] not reposing to PINGs, trying to continue...', + if self.mac: + logging.warning('%14s - target [%s] not responding to \ + PINGs, trying to resolve MAC address...', 'Reboot', self.ip) + (self.mac, self.ip) = self.resolv_host(self.mac) + else: + logging.warning('%14s - target [%s] not responding to PINGs, trying to continue...', 'Reboot', self.ip) # Force re-initialization of all the devlib modules diff --git a/target.config b/target.config index 18f5c1fc4..47a0e7da8 100644 --- a/target.config +++ b/target.config @@ -60,7 +60,7 @@ /* Wait time before trying to access the target after reboot */ // "ping_time" : "15", - /* Total time to wait after rebooting the target */ + /* Maximum time to wait after rebooting the target */ // "reboot_time" : "120", /* List of test environment features to enable */ -- GitLab