def run(self): dev_names = self.get_multi_mopt("netdev_name") pktgen_options = self.get_multi_mopt("pktgen_option") thread_options = self.get_multi_opt("thread_option") default_pktgen_options = ["count 10000000", "clone_skb 100000", "pkt_size 60"] pktgen_options = pktget_options_merge(pktgen_options, default_pktgen_options) exec_cmd("modprobe pktgen") pktgen_devices_remove() pgctl = Pktgen("/proc/net/pktgen/pgctrl") pgwrkr = PktgenWorkers() try: for idx, dev_name in enumerate(dev_names): pgwrkr.add_device(dev_name) pg = Pktgen("/proc/net/pktgen/%s" % dev_name) for pktgen_option in pktgen_options: pg.set(pktgen_option) if not thread_options: continue for thread_option in re.split(",", thread_options[idx]): pg.set(thread_option) pgctl.set("start") except ExecCmdFail: res_data = {"msg": "pktgen failed"} return self.set_fail(res_data) return self.set_pass()
def create(self): dev_cfg = self._dev_config br_name = dev_cfg["name"] exec_cmd("ovs-vsctl add-br %s" % br_name) self._add_internal_ports() self._add_tunnels()
def destroy(self): self._del_tunnels() self._del_internal_ports() dev_cfg = self._dev_config br_name = dev_cfg["name"] exec_cmd("ovs-vsctl del-br %s" % br_name)
def create(self): config = self._dev_config dev_name = config["name"] params = [] slaves = get_slaves(config) if len(slaves) == 1: ul_id = slaves[0] ul_name = self._if_manager.get_mapped_device(ul_id).get_name() params.append(" dev %s" % ul_name) for k in ("ttl", "tos", "key", "ikey", "okey", "local_ip", "remote_ip"): v = get_option(config, k) if v is not None: flag = {"local_ip": "local", "remote_ip": "remote"}.get(k, k) params.append(" %s %s" % (flag, v)) for k in ("seq", "iseq", "oseq", "csum", "icsum", "ocsum"): v = get_option(config, k) if v is not None and bool_it(v): params.append(" " + k) exec_cmd("ip tunnel add name %s mode gre%s" % (dev_name, "".join(params)))
def create(self): config = self._dev_config slaves = get_slaves(config) if len(slaves) == 1: realdev_id = slaves[0] name = self._if_manager.get_mapped_device(realdev_id).get_name() dev_param = "dev %s" % name else: dev_param = "" dev_name = config["name"] vxlan_id = int(get_option(config, "id")) group_ip = get_option(config, "group_ip") remote_ip = get_option(config, "remote_ip") if group_ip: group_or_remote = "group %s" % group_ip elif remote_ip: group_or_remote = "remote %s" % remote_ip else: raise Exception("group or remote must be specified for vxlan") dstport = get_option(config, "dstport") if not dstport: dstport = 0 else: dstport = int(dstport) exec_cmd("ip link add %s type vxlan id %d %s %s dstport %d" % (dev_name, vxlan_id, dev_param, group_or_remote, dstport))
def hello(self, recipe_path): self.machine_cleanup() self.restore_nm_option() logging.info("Recieved a controller connection.") self.clear_resource_table() self._cache.del_old_entries() self.reset_file_transfers() self._if_manager.rescan_devices() date = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S") self._log_ctl.set_recipe(recipe_path, expand=date) sleep(1) slave_desc = {} if check_process_running("NetworkManager"): logging.warning("=============================================") logging.warning("NetworkManager is running on a slave machine!") if lnst_config.get_option("environment", "use_nm"): logging.warning("Support of NM is still experimental!") else: logging.warning("Usage of NM is disabled!") logging.warning("=============================================") slave_desc["nm_running"] = True else: slave_desc["nm_running"] = False k_release, _ = exec_cmd("uname -r", False, False, False) r_release, _ = exec_cmd("cat /etc/redhat-release", False, False, False) slave_desc["kernel_release"] = k_release.strip() slave_desc["redhat_release"] = r_release.strip() return ("hello", slave_desc)
def configure(self): config = self._dev_config exec_cmd("ethtool -A %s rx off tx off" % config["name"], die_on_err=False, log_outputs=False) hw_addr = self._convert_hwaddr(config) s_eth = dbus.Dictionary({"mac-address": hw_addr}, signature="sv") s_con = dbus.Dictionary( { "type": "802-3-ethernet", "autoconnect": dbus.Boolean(False), "uuid": str(uuid.uuid4()), "id": "lnst_ethcon", }, signature="sv", ) connection = dbus.Dictionary( { "802-3-ethernet": s_eth, "connection": s_con, "ipv4": dbus.Dictionary({"method": "disabled"}, signature="sv"), "ipv6": dbus.Dictionary({"method": "ignore"}, signature="sv"), }, signature="sa{sv}", ) self._connection = connection self._nm_add_connection() if config["netem"] is not None: cmd = "tc qdisc add dev %s root netem %s" % (config["name"], parse_netem(config["netem"])) exec_cmd(cmd) config["netem_cmd"] = cmd
def set_mcast_flood(self, on = True): cmd = "ip link set dev %s type bridge_slave mcast_flood " % self._name if on: cmd += "on" else: cmd += "off" exec_cmd(cmd)
def deconfigure(self): for slave_id in get_slaves(self._dev_config): self.slave_del(slave_id) dev_name = self._dev_config["name"] exec_cmd("teamd -k -t %s" % dev_name)
def _configure_service(self, service, start=True): action = "start" if start else "stop" if self._is_systemd(): exec_cmd("systemctl {} {}".format(action, service)) else: exec_cmd("service {} {}".format(service, action)) return True
def configure(self): config = self._dev_config exec_cmd("ethtool -A %s rx off tx off" % config["name"], die_on_err=False, log_outputs=False) hw_addr = self._convert_hwaddr(config) s_eth = dbus.Dictionary({'mac-address': hw_addr}, signature='sv') s_con = dbus.Dictionary({ 'type': '802-3-ethernet', 'autoconnect': dbus.Boolean(False), 'uuid': str(uuid.uuid4()), 'id': 'lnst_ethcon'}, signature='sv') connection = dbus.Dictionary({ '802-3-ethernet': s_eth, 'connection': s_con, 'ipv4': dbus.Dictionary({'method': 'disabled'}, signature='sv'), 'ipv6': dbus.Dictionary({'method': 'ignore'}, signature='sv'), }, signature='sa{sv}') self._connection = connection self._nm_add_connection() if config["netem"] is not None: cmd = "tc qdisc add dev %s root netem %s" % (config["name"], parse_netem(config["netem"])) exec_cmd(cmd) config["netem_cmd"] = cmd
def down(self): config = self._dev_config if "addresses" in config: for address in config["addresses"]: exec_cmd("ip addr del %s dev %s" % (address, config["name"]), die_on_err=False) exec_cmd("ip link set %s down" % config["name"])
def set_device_down(self, hwaddr): devs = self.get_devices_by_hwaddr(hwaddr) for dev in devs: if is_nm_managed_by_name(dev["name"]): bus = dbus.SystemBus() nm_obj = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager") nm_if = dbus.Interface(nm_obj, "org.freedesktop.NetworkManager") dev_obj_path = nm_if.GetDeviceByIpIface(dev["name"]) dev_obj = bus.get_object("org.freedesktop.NetworkManager", dev_obj_path) dev_props = dbus.Interface(dev_obj, "org.freedesktop.DBus.Properties") if dev_props.Get("org.freedesktop.NetworkManager.Device", "ActiveConnection") != "/": dev_if = dbus.Interface(dev_obj, "org.freedesktop.NetworkManager.Device") logging.debug("Disconnecting device %s: %s" % (dev["name"], dev_obj_path)) dev_if.Disconnect() else: exec_cmd("ip link set %s down" % dev["name"]) return True
def deconfigure(self): config = self._dev_config exec_cmd("ethtool -s %s autoneg on" % config["name"]) if "netem_cmd" in config: exec_cmd(config["netem_cmd"].replace("add", "del")) if "lldp" in config: self.deconfigure_lldp()
def down(self): int_ports = self._dev_config["ovs_conf"]["internals"] br_name = self._dev_config["name"] for iport in int_ports: exec_cmd("ip link set %s down" % iport["name"]) super(NetConfigDeviceOvsBridge, self).down()
def run(self): dev_names = self.get_multi_mopt("netdev_name") pktgen_options = self.get_multi_mopt("pktgen_option") default_pktgen_options = [ "count 10000000", "clone_skb 100000", "pkt_size 60", ] pktgen_options = pktget_options_merge(pktgen_options, default_pktgen_options) exec_cmd("modprobe pktgen") pgctl = Pktgen("/proc/net/pktgen/pgctrl") pgwrkr = PktgenWorkers() try: for dev_name in dev_names: pgwrkr.add_device(dev_name) pg = Pktgen("/proc/net/pktgen/%s" % dev_name) for pktgen_option in pktgen_options: pg.set(pktgen_option) pgctl.set("start") except ExecCmdFail: res_data = {"msg": "pktgen failed"} return self.set_fail(res_data) return self.set_pass()
def _add_ports(self): slaves = self._dev_config["slaves"] vlans = self._dev_config["ovs_conf"]["vlans"] br_name = self._dev_config["name"] bond_ports = [] for bond in self._dev_config["ovs_conf"]["bonds"].itervalues(): for slave_id in bond["slaves"]: bond_ports.append(slave_id) for slave_id in slaves: if slave_id in bond_ports: continue slave_dev = self._if_manager.get_mapped_device(slave_id) slave_name = slave_dev.get_name() options = "" for opt in get_slave_options(self._dev_config, slave_id): options += " %s=%s" % (opt[0], opt[1]) vlan_tags = [] for tag, vlan in vlans.iteritems(): if slave_id in vlan["slaves"]: vlan_tags.append(tag) if len(vlan_tags) == 0: tags = "" elif len(vlan_tags) == 1: tags = " tag=%s" % vlan_tags[0] elif len(vlan_tags) > 1: tags = " trunks=" + ",".join(vlan_tags) exec_cmd("ovs-vsctl add-port %s %s%s" % (br_name, slave_name, tags)) if options != "": exec_cmd("ovs-vsctl set Interface %s%s" % (slave_name, options))
def _add_rm_slaves(self, mark): for slave in get_slaves(self._netdev): slavenetdev = self._config[slave] slave_name = slavenetdev["name"] if (mark == "+"): NetConfigDevice(slavenetdev, self._config).down() exec_cmd('echo "%s%s" > %s/slaves' % (mark, slave_name, self._get_bond_dir()))
def create(self): teamd_config = get_option(self._dev_config, "teamd_config") teamd_config = prepare_json_str(teamd_config) dev_name = self._dev_config["name"] dbus_option = " -D" if self._should_enable_dbus() else "" exec_cmd("teamd -r -d -c \"%s\" -t %s %s" % (teamd_config, dev_name, dbus_option))
def set_state(self, br_state_info): cmd = "bridge link set dev %s state %s" % (self._dev_name, br_state_info["state"]) if br_state_info["self"]: cmd += " self" if br_state_info["master"]: cmd += " master" exec_cmd(cmd)
def deconfigure(self): dev_cfg = self._dev_config self._del_bonds() self._del_ports() br_name = dev_cfg["name"] exec_cmd("ovs-vsctl del-br %s" % br_name)
def address_setup(self): super(NetConfigDeviceOvsBridge, self).up() int_ports = self._dev_config["ovs_conf"]["internals"] br_name = self._dev_config["name"] for iport in int_ports: if "addresses" in iport: for address in iport["addresses"]: exec_cmd("ip addr add %s dev %s" % (address, iport["name"]))
def _setup_options(self): if not "options" in self._dev_config: return options = self._dev_config["options"] for option, value in options: exec_cmd('echo "%s" > %s/%s' % (value, self._get_bridge_dir(), option))
def _add_bonds(self): br_name = self._dev_config["name"] bonds = self._dev_config["ovs_conf"]["bonds"] for bond_id, bond in bonds.iteritems(): ifaces = "" for slave in bond["slaves"]: ifaces += " %s" % slave exec_cmd("ovs-vsctl add-bond %s %s %s" % (br_name, bond_id, ifaces))
def configure(self): dev_cfg = self._dev_config br_name = dev_cfg["name"] exec_cmd("ovs-vsctl add-br %s" % br_name) self._add_ports() self._add_bonds()
def _add_rm_slaves(self, mark): for slave_id in get_slaves(self._dev_config): slave_dev = self._if_manager.get_mapped_device(slave_id) slave_conf = slave_dev.get_conf_dict() slave_name = slave_dev.get_name() if (mark == "+"): slave_dev.down() exec_cmd('echo "%s%s" > %s/slaves' % (mark, slave_name, self._get_bond_dir()))
def _add_del_fdb(self, op, br_fdb_info): cmd = "bridge fdb %s %s dev %s" % (op, br_fdb_info["hwaddr"], self._dev_name) if br_fdb_info["self"]: cmd += " self" if br_fdb_info["master"]: cmd += " master" if br_fdb_info["vlan_id"]: cmd += " vlan %s" % br_fdb_info["vlan_id"] exec_cmd(cmd)
def _del_ports(self): slaves = self._dev_config["slaves"] br_name = self._dev_config["name"] for slave_id in slaves: slave_dev = self._if_manager.get_mapped_device(slave_id) slave_name = slave_dev.get_name() exec_cmd("ovs-vsctl del-port %s %s" % (br_name, slave_name))
def down(self): int_ports = self._dev_config["ovs_conf"]["internals"] br_name = self._dev_config["name"] for iport in int_ports: if "addresses" in iport: for address in iport["addresses"]: exec_cmd("ip addr del %s dev %s" % (address, iport["name"])) exec_cmd("ip link set %s down" % iport["name"]) super(NetConfigDeviceOvsBridge, self).down()
def address_cleanup(self): super(NetConfigDeviceOvsBridge, self).down() super(NetConfigDeviceOvsBridge, self).address_cleanup() int_ports = self._dev_config["ovs_conf"]["internals"] br_name = self._dev_config["name"] for iport in int_ports: if "addresses" in iport: for address in iport["addresses"]: exec_cmd("ip addr del %s dev %s" % (address, iport["name"]), die_on_err=False)
def get_fdbs(_self): output = exec_cmd("bridge fdb show dev %s" % _self._dev_name, die_on_err=False)[0] br_fdb_info_list = [] for line in output.split("\n"): match = re.match( r'([0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2})', line) if match: hwaddr = normalize_hwaddr(match.groups()[0]) match = re.match(r'.*\s+vlan (\d+)', line) vlan_id = int(match.groups()[0]) if match else 0 self = True if re.match(r'.*\s+self', line) else False master = True if re.match(r'.*\s+master', line) else False offload = True if re.match(r'.*\s+offload', line) else False extern_learn = True if re.match(r'.*\s+extern_learn', line) else False br_fdb_info = { "hwaddr": hwaddr, "vlan_id": vlan_id, "self": self, "master": master, "offload": offload, "extern_learn": extern_learn } br_fdb_info_list.append(br_fdb_info) return br_fdb_info_list
def qdisc_red_stats(self, parent = None): try: out, _ = exec_cmd("tc -s qdisc show dev %s" % self._name) except ExecCmdFail: return {} if parent: parent = "parent 3:%d" % parent else: parent = "root" p = r"[.\n]*?qdisc red.+?%s.+?(offloaded)? limit.*?\n\s*Sent " \ r"(?P<tx_bytes>\d+) bytes (?P<tx_packets>\d+) pkt \(dropped " \ r"(?P<drops>\d+), overlimits (?P<overlimits>\d+).*\n\s*backlog " \ r"(?P<backlog>\d+\w?)b.*\n.*?marked (?P<marked>\d+) early " \ r"(?P<early>\d+) pdrop (?P<pdrop>\d+)" % parent red_pattern = re.compile(p, re.MULTILINE) stats_raw = red_pattern.search(out) if not stats_raw: return {} stats = {key: int(val.replace("K", "000").replace("M", "000000")) for key, val in stats_raw.groupdict().iteritems()} stats["offload"] = "offloaded" in stats_raw.groups() stats["devname"] = self._name stats["hwaddr"] = self._hwaddr return stats
def link_stats(self): stats = {"devname": self._name, "hwaddr": self._hwaddr} try: out, _ = exec_cmd("ip -s link show %s" % self._name) except: return {} lines = iter(out.split("\n")) for line in lines: if (len(line.split()) == 0): continue if (line.split()[0] == 'vf'): break if (line.split()[0] == "RX:"): rx_stats = map(int, lines.next().split()) stats.update({ "rx_bytes": rx_stats[0], "rx_packets": rx_stats[1], "rx_errors": rx_stats[2], "rx_dropped": rx_stats[3], "rx_overrun": rx_stats[4], "rx_mcast": rx_stats[5] }) if (line.split()[0] == "TX:"): tx_stats = map(int, lines.next().split()) stats.update({ "tx_bytes": tx_stats[0], "tx_packets": tx_stats[1], "tx_errors": tx_stats[2], "tx_dropped": tx_stats[3], "tx_carrier": tx_stats[4], "tx_collsns": tx_stats[5] }) return stats
def run(self): cmd = self.compose_cmd() logging.debug("%s" % cmd) limit_rate = self.get_opt("limit_rate ", default=80) data_stdout = exec_cmd(cmd, die_on_err=False)[0] stat_pttr1 = r'(\d+) packets transmitted, (\d+) received' stat_pttr2 = r' rtt min/avg/max/mdev = \ (\d+\.\d+)/(\d+\.\d+)/(\d+\.\d+)/(\d+\.\d+) ms' match = re.search(stat_pttr1, data_stdout) if not match: return self.set_fail("expected pattern not found") trans_pkts, recv_pkts = match.groups() rate = int(round((float(recv_pkts) / float(trans_pkts)) * 100)) logging.debug("Transmitted \"%s\", received \"%s\", " \ "rate \"%d%%\", limit_rate \"%d%%\"" \ % (trans_pkts, recv_pkts, rate, limit_rate )) match = re.search(stat_pttr2, data_stdout) if match: tmin, tavg, tmax, tmdev = [float(x) for x in match.groups()] logging .debug("rtt min \"%.3f\", avg \"%.3f\", max \"%.3f\", " "mdev \"%.3f\"" % (tmin, tavg, tmax, tmdev)) if rate < limit_rate : return self.set_fail("rate is lower that limit") return self.set_pass()
def exec_from(self, tools_name, cmd, *args, **kwargs): if not tools_name in self._resource_table["tools"]: msg = "Tools '%s' not found" % tools_name raise CommandException(msg) tools_path = self._resource_table["tools"][tools_name] return exec_cmd("cd \"%s\" && %s" % (tools_path, cmd), *args, **kwargs)
def link_cpu_ifstat(self): stats = {"devname": self._name, "hwaddr": self._hwaddr} if len(self._slaves) != 0: return self.link_cpu_ifstat_from_slaves() try: out, _ = exec_cmd("ifstat -x c %s" % self._name) except: return {} lines = iter(out.split("\n")) line_first = "" line_decond = "" for line in lines: if (len(line.split()) == 0): continue if (line.split()[0] == self._name): break else: return {} stats_data = line.split()[1:] for i in range(len(stats_data)): stats_data[i] = stats_data[i].replace("K", "000") stats_data[i] = stats_data[i].replace("M", "000000") stats_data = map(int, stats_data) stats["rx_packets"] = stats_data[0] stats["tx_packets"] = stats_data[2] stats["rx_bytes"] = stats_data[4] stats["tx_bytes"] = stats_data[6] return stats
def hello(self): logging.info("Recieved a controller connection.") agent_desc = {} if check_process_running("NetworkManager"): agent_desc["nm_running"] = True else: agent_desc["nm_running"] = False k_release, _ = exec_cmd("uname -r", False, False, False) r_release, _ = exec_cmd("cat /etc/redhat-release", False, False, False) agent_desc["kernel_release"] = k_release.strip() agent_desc["redhat_release"] = r_release.strip() agent_desc["lnst_version"] = lnst_version return ("hello", agent_desc)
def _check_ip_link_add(self): output = exec_cmd("ip link help", die_on_err=False, log_outputs=False)[1] for line in output.split("\n"): if re.match(r'^.*ip link add [\[]{0,1}link.*$', line): return True return False
def run(self): cfg_file = tempfile.NamedTemporaryFile(delete=False) yaml.dump(self._trex_config, cfg_file) exec_cmd( "unset TMUX; tmux new-session -d -s trex 'cd {trex_dir}; ./t-rex-64 --cfg {cfg_path} -i'" .format(trex_dir=self._trex_path, cfg_path=cfg_file.name)) self.wait_on_interrupt() exec_cmd("unset TMUX; tmux kill-session -t trex") os.remove(cfg_file.name) res_data = {} res_data["msg"] = "" return self.set_pass(res_data)
def _del_ports(self): slaves = self._dev_config["slaves"] br_name = self._dev_config["name"] bond_ports = [] for bond in self._dev_config["ovs_conf"]["bonds"].values(): for slave_id in bond["slaves"]: bond_ports.append(slave_id) for slave_id in slaves: if slave_id in bond_ports: continue slave_dev = self._if_manager.get_mapped_device(slave_id) slave_name = slave_dev.get_name() exec_cmd("ovs-vsctl del-port %s %s" % (br_name, slave_name))
def create_tar_archive(input_path, target_path, compression=False): if compression: args = "cfj" else: args = "cf" input_path = input_path.rstrip("/") input_file = os.path.basename(input_path) parent = os.path.dirname(input_path) if os.path.isdir(target_path): target_path += "/%s.tar.bz" % os.path.basename(input_file.rstrip("/")) exec_cmd("cd \"%s\" && tar %s \"%s\" \"%s\"" % \ (parent, args, target_path, input_file)) return target_path
def _add_tunnels(self): tunnels = self._dev_config["ovs_conf"]["tunnels"] br_name = self._dev_config["name"] for i in tunnels: i["name"] = self._if_manager.assign_name_generic(prefix=i["type"]) options = "" for opt in i["options"]: options += " %s=%s" % (opt["name"], opt["value"]) if opt["name"] == "name": i["name"] = opt["value"] exec_cmd("ovs-vsctl add-port %s %s -- set Interface %s "\ "type=%s %s" % (br_name, i["name"], i["name"], i["type"], options))
def set_qdisc_red(self, limit, avpkt, _min, _max, prob = 0, ecn = False, change = False, burst = None, parent = None): cmd = "change" if change else "add" if parent: parent = "parent 3:%d" % parent else: parent = "root" cmd = "tc qdisc %s dev %s %s red limit %d avpkt %d min %d max %d" % \ (cmd, self._name, parent, limit, avpkt, _min, _max) if prob: cmd += " prob %d" % prob if ecn: cmd += " ecn" if burst: cmd += " burst %d" % burst exec_cmd(cmd)
def set_if_netns(self, if_id, netns): netns_pid = self._net_namespaces[netns]["pid"] device = self._if_manager.get_mapped_device(if_id) dev_name = device.get_name() device.set_netns(netns) hwaddr = device.get_hwaddr() exec_cmd("ip link set %s netns %d" % (dev_name, netns_pid)) msg = { "type": "command", "method_name": "map_if_by_hwaddr", "args": [if_id, hwaddr] } self._server_handler.send_data_to_netns(netns, msg) result = self._slave_server.wait_for_result(netns) return result
def internal_port_add(self, **kwargs): name = self._if_manager.assign_name("int") options = "" for opt_name, opt_value in kwargs.items(): if opt_name == "name": name = opt_value continue options += " %s=%s" % (opt_name, opt_value) exec_cmd("ovs-vsctl add-port %s %s -- set Interface %s "\ "type=internal %s" % (self.name, name, name, options)) dev = self._if_manager.get_device_by_name(name) return dev
def deconfigure_lldp(self): config = self._dev_config up2tc_def = ','.join(["{}:0".format(x) for x in range(8)]) tsa_def = ','.join(["{}:strict".format(x) for x in range(8)]) exec_cmd("lldptool -i %s -V ETS-CFG -T up2tc=%s" % (config["name"], up2tc_def)) exec_cmd("lldptool -i %s -V ETS-CFG -T tsa=%s" % (config["name"], tsa_def)) exec_cmd("lldptool -i %s -V PFC -T enabled=none" % config["name"]) exec_cmd("lldptool -i %s -L adminStatus=disabled" % config["name"])
def run(self): try: stdout, stderr = exec_cmd(self._what["command"], self._what["json"]) self._result["passed"] = True self._result["res_data"] = {"stdout": stdout, "stderr": stderr} except ExecCmdFail as e: self._result["passed"] = False self._result["res_data"] = res_data = {"stdout": e.get_stdout(), "stderr": e.get_stderr()}
def _ethtool_get_driver(self): if self._ifi_type == 772: #loopback ifi type return 'loopback' out, _ = exec_cmd("ethtool -i %s" % self._name, False, False, False) match = re.search("^driver: (.*)$", out, re.MULTILINE) if match is not None: return match.group(1) else: return None
def _clear_tc_qdisc(self): try: #checks device existence as it might have been removed by #calling self.deconfigure() exec_cmd("ip l show %s" % self._name, log_outputs=False) except: return exec_cmd("tc qdisc del dev %s root" % self._name, die_on_err=False) out, _ = exec_cmd("tc filter show dev %s" % self._name) ingress_handles = re.findall("ingress (\\d+):", out) for ingress_handle in ingress_handles: exec_cmd("tc qdisc del dev %s handle %s: ingress" % (self._name, ingress_handle)) out, _ = exec_cmd("tc qdisc show dev %s" % self._name) ingress_qdiscs = re.findall("qdisc ingress (\\w+):", out) if len(ingress_qdiscs) != 0: exec_cmd("tc qdisc del dev %s ingress" % self._name)
def _add_internal_ports(self): int_ports = self._dev_config["ovs_conf"]["internals"] br_name = self._dev_config["name"] for i in int_ports: i["name"] = self._if_manager.assign_name_generic(prefix="int") options = "" if "options" in i: for opt in i["options"]: options += " %s=%s" % (opt["name"], opt["value"]) if opt["name"] == "name": i["name"] = opt["value"] exec_cmd("ovs-vsctl add-port %s %s -- set Interface %s "\ "type=internal %s" % (br_name, i["name"], i["name"], options))
def get_ethtool_stats(self): stdout, _ = exec_cmd("ethtool -S %s" % self._name) d = {} # First and last lines don't contain stats. for line in stdout.split('\n')[1:-1]: stat, count = line.split(':') d[stat.strip()] = int(count) return d
def _clear_tc_qdisc(self): exec_cmd("tc qdisc del dev %s root" % self.name, die_on_err=False) out, _ = exec_cmd("tc filter show dev %s" % self.name) ingress_handles = re.findall("ingress (\\d+):", out) for ingress_handle in ingress_handles: exec_cmd("tc qdisc del dev %s handle %s: ingress" % (self.name, ingress_handle)) out, _ = exec_cmd("tc qdisc show dev %s" % self.name) ingress_qdiscs = re.findall("qdisc ingress (\\w+):", out) if len(ingress_qdiscs) != 0: exec_cmd("tc qdisc del dev %s ingress" % self.name)
def port_add(self, device=None, port_options={}, interface_options={}): if device is None: dev_name = interface_options.get( 'name', self._if_manager.assign_name(interface_options['type'])) else: dev_name = device.name exec_cmd("ovs-vsctl add-port {} {}{}{}".format( self.name, dev_name, self._dict_to_keyvalues(port_options), self._interface_cmd(dev_name, interface_options))) iface = None if 'type' in interface_options and interface_options[ 'type'] == 'internal': iface = self._if_manager.get_device_by_name(dev_name) iface._enable() return iface
def add_cache_entry(self, entry_hash, filepath, entry_name, entry_type): if entry_hash in self._entries: raise ResourceCacheError("File already in cache") entry_dir = "%s/%s" % (self._root, entry_hash) if os.path.exists(entry_dir): try: shutil.rmtree(entry_dir) except OSError as e: if e.errno != 2: raise os.makedirs(entry_dir) shutil.move(filepath, entry_dir) entry_path = "%s/%s" % (entry_dir, os.path.basename(filepath)) if entry_type == "module": filename = "%s.py" % entry_name shutil.move(entry_path, "%s/%s" % (entry_dir, filename)) elif entry_type == "tools": filename = entry_name tools_dir = "%s/%s" % (entry_dir, filename) exec_cmd("tar xjmf \"%s\" -C \"%s\"" % (entry_path, entry_dir)) if os.path.exists("%s/%s" % (tools_dir, SETUP_SCRIPT_NAME)): exec_cmd("cd \"%s\" && ./%s" % (tools_dir, SETUP_SCRIPT_NAME)) else: msg = "%s not found in %s tools, skipping initialization." % \ (SETUP_SCRIPT_NAME, entry_name) logging.warn(msg) entry = { "type": entry_type, "name": entry_name, "last_used": int(time.time()), "path": "%s/%s" % (entry_hash, filename) } self._entries[entry_hash] = entry self._save_index() return entry_hash
def _get_port_info(self): numbered_ports = {} port_lines = [] dumped_ports = exec_cmd("ovs-ofctl dump-ports-desc %s" % self.name, log_outputs=False)[0] for match in re.finditer(r'(\w+)\((\w*)\)', dumped_ports): numbered_ports[match.groups()[1]] = match.groups()[0] ovs_show = exec_cmd("ovs-vsctl show", log_outputs=False)[0] regex = r'(Port[\w\W]*?)(?=Port|ovs_version)' for match in re.finditer(regex, ovs_show): line = match.groups()[0].replace('\n', ' ') line = self._port_format(line) port_lines.append(line) return numbered_ports, port_lines
def _read_adaptive_coalescing(self): res, _ = exec_cmd("ethtool -c %s" % self.name, die_on_err=False) regex = "Adaptive RX: (on|off) TX: (on|off)" try: res = re.search(regex, res).groups() except AttributeError: raise DeviceFeatureNotSupported( "No values for coalescence of %s." % self.name) return list(res)
def is_up(self, max_time): start_time = time() while(time() - start_time < max_time): out, _ = exec_cmd("ip l show dev %s up | grep 'state UP'" % self._name, die_on_err = False) if out == "": logging.info("sleep 20!") sleep(20) else: logging.info("port up!") return True raise IfMgrError("Time out - device %s not up." % self._name)
def create(self): conf = self._dev_config local = '' remote = '' key = None for opt, val in conf['options']: if opt == 'local': local = 'local ' + val elif opt == 'remote': remote = 'remote ' + val elif opt == 'key': key = val else: pass if key == None: raise Exception("Option 'key' not set for a vti device") exec_cmd("ip link add %s type vti %s %s key %s" % (conf["name"], local, remote, key))
def restore_system_config(self): logging.info("Restoring system configuration") for option, values in self._system_config.iteritems(): try: cmd_str = "echo \"%s\" >%s" % (values["initial_val"], option) (stdout, stderr) = exec_cmd(cmd_str) except ExecCmdFail: logging.warn("Unable to restore '%s' config option!", option) return False self._system_config = {} return True
def _setup_options(self): if not "options" in self._dev_config: return options = self._dev_config["options"] #Make sure that the device is down before configuring options #this is a temporary workaround for NM setting the device IFF_UP on #creation, which means that there is still a race condition here. #Related to RH bgz #1114685 exec_cmd('ip link set %s down' % self._dev_config["name"]) for option, value in options: if option == "primary": ''' "primary" option is not direct value but it's index of netdevice. So take the appropriate name from config ''' slave_dev = self._if_manager.get_mapped_device(value) value = slave_dev.get_name() exec_cmd('echo "%s" > %s/%s' % (value, self._get_bond_dir(), option))