1#!/usr/bin/env python3 2# 3# Copyright 2017 - Google, Inc. 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17import logging 18from acts.libs.proc import job 19 20GET_ALL_INTERFACE = 'ls /sys/class/net' 21GET_VIRTUAL_INTERFACE = 'ls /sys/devices/virtual/net' 22BRCTL_SHOW = 'brctl show' 23 24 25class ApInterfacesError(Exception): 26 """Error related to AP interfaces.""" 27 28 29class ApInterfaces(object): 30 """Class to get network interface information for the device. 31 32 """ 33 34 def __init__(self, ap): 35 """Initialize the ApInterface class. 36 37 Args: 38 ap: the ap object within ACTS 39 """ 40 self.ssh = ap.ssh 41 42 def get_all_interface(self): 43 """Get all network interfaces on the device. 44 45 Returns: 46 interfaces_all: list of all the network interfaces on device 47 """ 48 output = self.ssh.run(GET_ALL_INTERFACE) 49 interfaces_all = output.stdout.split('\n') 50 51 return interfaces_all 52 53 def get_virtual_interface(self): 54 """Get all virtual interfaces on the device. 55 56 Returns: 57 interfaces_virtual: list of all the virtual interfaces on device 58 """ 59 output = self.ssh.run(GET_VIRTUAL_INTERFACE) 60 interfaces_virtual = output.stdout.split('\n') 61 62 return interfaces_virtual 63 64 def get_physical_interface(self): 65 """Get all the physical interfaces of the device. 66 67 Get all physical interfaces such as eth ports and wlan ports 68 Returns: 69 interfaces_phy: list of all the physical interfaces 70 """ 71 interfaces_all = self.get_all_interface() 72 interfaces_virtual = self.get_virtual_interface() 73 interfaces_phy = list(set(interfaces_all) - set(interfaces_virtual)) 74 75 return interfaces_phy 76 77 def get_bridge_interface(self): 78 """Get all the bridge interfaces of the device. 79 80 Returns: 81 interfaces_bridge: the list of bridge interfaces, return None if 82 bridge utility is not available on the device 83 """ 84 interfaces_bridge = [] 85 try: 86 output = self.ssh.run(BRCTL_SHOW) 87 lines = output.stdout.split('\n') 88 for line in lines: 89 interfaces_bridge.append(line.split('\t')[0]) 90 interfaces_bridge.pop(0) 91 interfaces_bridge = [x for x in interfaces_bridge if x is not ''] 92 return interfaces_bridge 93 except job.Error: 94 logging.info('No brctl utility is available') 95 return None 96 97 def get_wlan_interface(self): 98 """Get all WLAN interfaces and specify 2.4 GHz and 5 GHz interfaces. 99 100 Returns: 101 interfaces_wlan: all wlan interfaces 102 Raises: 103 ApInterfacesError: Missing at least one WLAN interface 104 """ 105 wlan_2g = None 106 wlan_5g = None 107 interfaces_phy = self.get_physical_interface() 108 for iface in interfaces_phy: 109 IW_LIST_FREQ = 'iwlist %s freq' % iface 110 output = self.ssh.run(IW_LIST_FREQ) 111 if 'Channel 06' in output.stdout and 'Channel 36' not in output.stdout: 112 wlan_2g = iface 113 elif 'Channel 36' in output.stdout and 'Channel 06' not in output.stdout: 114 wlan_5g = iface 115 116 interfaces_wlan = [wlan_2g, wlan_5g] 117 118 if None not in interfaces_wlan: 119 return interfaces_wlan 120 121 raise ApInterfacesError('Missing at least one WLAN interface') 122 123 def get_wan_interface(self): 124 """Get the WAN interface which has internet connectivity. 125 126 Returns: 127 wan: the only one WAN interface 128 Raises: 129 ApInterfacesError: no running WAN can be found 130 """ 131 wan = None 132 interfaces_phy = self.get_physical_interface() 133 interfaces_wlan = self.get_wlan_interface() 134 interfaces_eth = list(set(interfaces_phy) - set(interfaces_wlan)) 135 for iface in interfaces_eth: 136 network_status = self.check_ping(iface) 137 if network_status == 1: 138 wan = iface 139 break 140 if wan: 141 return wan 142 143 output = self.ssh.run('ifconfig') 144 interfaces_all = output.stdout.split('\n') 145 logging.info("IFCONFIG output = %s" % interfaces_all) 146 147 raise ApInterfacesError('No WAN interface available') 148 149 def get_lan_interface(self): 150 """Get the LAN interface connecting to local devices. 151 152 Returns: 153 lan: the only one running LAN interface of the devices 154 None, if nothing was found. 155 """ 156 lan = None 157 interfaces_phy = self.get_physical_interface() 158 interfaces_wlan = self.get_wlan_interface() 159 interfaces_eth = list(set(interfaces_phy) - set(interfaces_wlan)) 160 interface_wan = self.get_wan_interface() 161 interfaces_eth.remove(interface_wan) 162 for iface in interfaces_eth: 163 LAN_CHECK = 'ifconfig %s' % iface 164 output = self.ssh.run(LAN_CHECK) 165 if 'RUNNING' in output.stdout: 166 lan = iface 167 break 168 return lan 169 170 def check_ping(self, iface): 171 """Check the ping status on specific interface to determine the WAN. 172 173 Args: 174 iface: the specific interface to check 175 Returns: 176 network_status: the connectivity status of the interface 177 """ 178 PING = 'ping -c 1 -I %s 8.8.8.8' % iface 179 try: 180 self.ssh.run(PING) 181 return 1 182 except job.Error: 183 return 0 184