1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
17 from StringIO import StringIO
19 LOG = logging.getLogger(__name__)
24 def __deco(*args, **kwargs):
25 if "shell" in kwargs and kwargs["shell"]:
28 LOG.info(' '.join(args[0]))
29 return func(*args, **kwargs)
35 def call(cmd, shell=False):
36 ret = subprocess.call(cmd, shell=shell)
38 LOG.info("warning: %s not success.", cmd)
42 def check_call(cmd, shell=False):
43 subprocess.check_call(cmd, shell=shell)
47 def check_output(cmd, shell=False):
48 return subprocess.check_output(cmd, shell=shell)
52 def my_popen(cmd, shell=False, stdout=None, stderr=None):
53 return subprocess.Popen(cmd, shell=shell, stdout=stdout, stderr=stderr)
57 cmd = "ping -w2 -c1 %s" % ip
58 p = my_popen(cmd, shell=True)
62 def get_device_name(bdf):
63 path = '/sys/bus/pci/devices/0000:%s/net/' % bdf
64 path1 = '/sys/bus/pci/devices/0000:%s/virtio*/net/' % bdf
65 if os.path.exists(path):
66 device = check_output("ls " + path, shell=True).strip()
70 device = check_output("ls " + path1, shell=True).strip()
77 LOG.info('sleep %s' % delay)
81 def my_mkdir(filepath):
83 LOG.info("mkdir -p %s" % filepath)
87 LOG.info("! %s already exists" % filepath)
92 def get_eth_by_bdf(bdf):
93 bdf = bdf.replace(' ', '')
94 path = '/sys/bus/pci/devices/0000:%s/net/' % bdf
95 if os.path.exists(path):
96 device = check_output("ls " + path, shell=True).strip()
98 raise Exception("cann't get device name of bdf:%s" % bdf)
102 def check_and_kill(process):
103 cmd = "ps -ef | grep -v grep | awk '{print $8}' | grep -w %s | wc -l" % process
104 out = check_output(cmd, shell=True)
106 check_call(['killall', process])
111 "lsmod | sed 1,1d | awk '{print $1}'",
115 def check_and_rmmod(mod):
116 if mod in list_mods():
117 check_call(['rmmod', mod])
120 def kill_by_name(process):
121 out = check_output(['ps', '-A'])
122 for line in out.splitlines():
123 values = line.split()
124 pid, name = values[0], values[3]
127 os.kill(pid, signal.SIGKILL)
128 LOG.info("os.kill(%s)" % pid)
132 netns_exec_str = "ip netns exec %s "
133 if ns in (None, 'null', 'None', 'none'):
136 cmd = (netns_exec_str % ns) + cmd
141 mac = [0x00, 0x16, 0x3e,
142 random.randint(0x00, 0x7f),
143 random.randint(0x00, 0xff),
144 random.randint(0x00, 0xff)]
145 return ':'.join(map(lambda x: "%02x" % x, mac))
148 class IPCommandHelper(object):
150 def __init__(self, ns=None):
153 self.device_mac_map = {}
154 self.mac_device_map = {}
155 self.bdf_device_map = {}
156 self.device_bdf_map = {}
157 self.mac_bdf_map = {}
158 self.bdf_mac_map = {}
161 cmd = "ip netns exec %s " % ns + cmd
162 buf = check_output(cmd, shell=True)
165 m = re.match(r'^\d+:(.*):.*', line)
166 if m and m.group(1).strip() != "lo":
167 device = m.group(1).strip()
168 self.devices.append(device)
169 mac = self._get_mac(ns, device)
170 self.macs.append(mac)
171 for device, mac in zip(self.devices, self.macs):
172 self.device_mac_map[device] = mac
173 self.mac_device_map[mac] = device
175 cmd = "ethtool -i %s"
177 cmd = "ip netns exec %s " % ns + cmd
178 for device in self.devices:
179 buf = check_output(cmd % device, shell=True)
181 r'^bus-info: \d{4}:(\d{2}:\d{2}\.\d*)$',
185 self.bdf_device_map[bdfs[0]] = device
186 self.device_bdf_map[device] = bdfs[0]
187 mac = self.device_mac_map[device]
188 self.mac_bdf_map[mac] = bdfs[0]
189 self.bdf_mac_map[bdfs[0]] = mac
192 def _get_mac(ns, device):
193 cmd = "ip addr show dev %s" % device
195 cmd = "ip netns exec %s " % ns + cmd
196 buf = check_output(cmd, shell=True)
198 r"[A-F0-9]{2}(?::[A-F0-9]{2}){5}",
199 re.IGNORECASE | re.MULTILINE)
200 for mac in macs.findall(buf):
201 if mac.lower() not in ('00:00:00:00:00:00', 'ff:ff:ff:ff:ff:ff'):
205 def get_device_verbose(self, identity):
206 if identity in self.device_mac_map:
208 elif identity in self.bdf_device_map:
209 device = self.bdf_device_map[identity]
210 elif identity in self.mac_device_map:
211 device = self.mac_device_map[identity]
213 raise Exception("cann't find the device by identity:%s" % identity)
215 'bdf': self.device_bdf_map[device] if device in self.device_bdf_map else None,
217 'mac': self.device_mac_map[device] if device in self.device_mac_map else None,
222 class AttrDict(dict):
223 """A dictionary with attribute-style access. It maps attribute access to
224 the real dictionary. """
226 def __init__(self, init={}):
227 dict.__init__(self, init)
229 def __getstate__(self):
230 return self.__dict__.items()
232 def __setstate__(self, items):
233 for key, val in items:
234 self.__dict__[key] = val
237 return "%s(%s)" % (self.__class__.__name__, dict.__repr__(self))
239 def __setitem__(self, key, value):
240 return super(AttrDict, self).__setitem__(key, value)
242 def __getitem__(self, name):
243 return super(AttrDict, self).__getitem__(name)
245 def __delitem__(self, name):
246 return super(AttrDict, self).__delitem__(name)
248 __getattr__ = __getitem__
249 __setattr__ = __setitem__
256 if __name__ == "__main__":
257 ipcmd = IPCommandHelper()
258 print ipcmd.device_mac_map
259 print ipcmd.mac_device_map
260 print ipcmd.bdf_device_map
261 print ipcmd.device_bdf_map
262 print ipcmd.mac_bdf_map
263 print ipcmd.bdf_mac_map
264 print ipcmd.get_device_verbose("tap0")