def add_qos(self, ingress_rate, ingress_peak_rate, ingress_burst, ingress_bum_rate, ingress_bum_peak_rate, ingress_bum_burst, ingress_fip_rate, ingress_fip_peak_rate, ingress_fip_burst, egress_class, egress_fip_rate, egress_fip_peak_rate, egress_fip_burst): cmd = [ self.ofctl_path, "add-flow", self.br, "flow_type=qos,interface=" + self.iface + ",type="+ self.type + ",vm_uuid=" + self.uuid + ",ingress_rate=" + str(ingress_rate) + ",ingress_peak_rate="+ str(ingress_peak_rate) + ",ingress_burst=" + str(ingress_burst) + ",ingress_bum_rate=" + str(ingress_bum_rate) + ",ingress_bum_peak_rate=" + str(ingress_bum_peak_rate)+ ",ingress_bum_burst=" + str(ingress_bum_burst) + ",ingress_fip_rate=" + str(ingress_fip_rate) + ",ingress_fip_peak_rate=" + str(ingress_fip_peak_rate)+ ",ingress_fip_burst=" + str(ingress_fip_burst) + ",egress_fip_rate=" + str(egress_fip_rate) + ",egress_fip_peak_rate=" + str(egress_fip_peak_rate) + ",egress_fip_burst=" + str(egress_fip_burst) + ",egress_class=" + str(egress_class) ] shell.run_cmd("Adding qos route to VM " + self.name, cmd, self.logfd) return
def __ifconfig_route_add(iface, ip, subnet, netmask, logfd): val = subnet.split(".") subnet = val[0] + ".0.0.0" val = netmask.split(".") netmask = val[0] + ".0.0.0" cmd = ["route", "add", "-net", subnet, "netmask", netmask, "dev", iface] shell.run_cmd("Setting route for iface " + iface + " ip " + ip, cmd, logfd)
def reset(self): iface_list = self.__get_ifaces() for iface in iface_list: cmd = ["sudo", self.vsctl_path, "del-port", self.br, iface] shell.run_cmd( "Deleting tunnel port " + iface + " on bridge " + self.br, cmd, self.logfd)
def add(self): cmd = [ self.nuage_port_config, '--secret', '--add', '--bridge', self.br, '--config', self.cfg_file ] shell.run_cmd( "Executing " + self.nuage_port_config + " for " + self.name, cmd, self.logfd)
def enable_mac_learning(self): cmd = [ self.ofctl_path, "add-flow", self.br, 'flow_type=route,type=' + self.type + ',flags=enable-learning,interface=' + self.iface + ',vm_uuid='+ self.uuid ] shell.run_cmd("Enabling mac learning for VM " + self.name, cmd, self.logfd) return
def reset(self): vrf_list = self.list_vrfs() for vrf_id in vrf_list: cmd = [ self.ofctl_path, "del-vrf", self.br, str(int(str(vrf_id), 16)) ] shell.run_cmd("Deleting VRF " + vrf_id, cmd, self.logfd)
def __ip_route_add(iface, ip, subnet, netmask, logfd): val = subnet.split(".") subnet = val[0] + ".0.0.0" val = ip.split(".") gateway = val[0] + "." + val[1] + "." + val[2] + ".1" cmd = ["ip", "route", "add", subnet + "/32", "via", gateway, "dev", iface] shell.run_cmd("Setting ip route for iface " + iface + " ip " + ip, cmd, logfd)
def route_del(iface, ip, subnet, netmask, logfd): val = subnet.split(".") subnet = val[0] + ".0.0.0" val = netmask.split(".") netmask = val[0] + ".0.0.0" cmd = ["route", "del", "-net", subnet, "netmask", netmask, "dev", iface] shell.run_cmd("Removing route for iface " + iface + " ip " + ip, cmd, logfd)
def __setup_vport_mirror(self, action): flowstr = self.mirror_flowstr + ", mirror_direction=" + self.mirror_dir if (action == "Set"): ofctl_cmd = "add-flow" else: ofctl_cmd = "del-flows" cmd = [self.ofctl_path, ofctl_cmd, self.br, flowstr] hdrstr = action + " " + self.mirror_dir + " Port mirror" shell.run_cmd(hdrstr, cmd, self.logfd)
def add_membership(self, evpn_id): cmd = [ self.ofctl_path, "add-flow", self.br, 'flow_type=route,type=' + self.type + ',ip,flags=membership,evpn_id=' + str(evpn_id)+ ',interface=' + str(self.iface) + ',vm_uuid=' + str(self.uuid) ] shell.run_cmd("Adding VM membership of " + self.name + " to evpn " + str(evpn_id), cmd, self.logfd) return
def reset(self): vrf_list = self.list_vrfs() for vrf_id in vrf_list: evpn_list = self.list_evpns() for evpn_id in evpn_list: cmd = [ self.ofctl_path, "del-evpn", self.br, vrf_id, "evpn_id=" + evpn_id ] shell.run_cmd("Deleting EVPN " + evpn_id, cmd, self.logfd)
def __build_vmxnet3(self): with cd(self.vmxnet3_path): cmd = ["make", "clean"] shell.run_cmd("Cleaning up vmxnet3", cmd, self.logfd) cmd = [ "make", "all", "T=" + self.tgt, "RTE_INCLUDE=" + self.dpdk_path + "/" + self.tgt + "/include" ] shell.run_cmd("Building vmxnet3", cmd, self.logfd)
def destroy(self): iface_list = self.__get_ifaces() for iface in iface_list: if (self.iface != iface): continue cmd = ["sudo", self.vsctl_path, "del-port", self.br, iface] shell.run_cmd( "Deleting tunnel port " + iface + " on bridge " + self.br, cmd, self.logfd)
def del_vlan(iface, logfd): vlan_tok = iface.split(".") mgmt_iface = vlan_tok[0] if (len(vlan_tok) > 1): vlan_id = vlan_tok[1] else: vlan_id = None if (vlan_id != None): cmd = ["vconfig", "rem", iface] shell.run_cmd("Deleting vlan " + iface, cmd, logfd)
def __modify_huge_pages_procfile(self, n_pages, caption): scriptfile = "/tmp/scriptfile" scriptfile_fd = open(scriptfile, "w") print >> scriptfile_fd, "echo " + str( n_pages) + " > " + self.proc_nr_hugepages_file scriptfile_fd.close() cmd = ["sh", scriptfile] shell.run_cmd(caption + " " + self.proc_nr_hugepages_file, cmd, self.logfd) os.unlink(scriptfile)
def set_flow_reg(self, type, pv_val, iface, reg, reg_val): if (type == "Ingress"): table_id = "5" elif (type == "Egress"): table_id = "6" else: table_id = None if (table_id == None): return None, None, None n_flows, n_pkts_str, flow = self.__parse_dump_flows( type, pv_val, 2, False, True, 16384) if (n_flows < 1): return None, None, None if (n_pkts_str == None): return None, None, None n_pkts_org = n_pkts_str.replace(",", "").split("=")[1] rule = flow.split()[5] nw_proto = rule.split(",")[1] if (nw_proto == "tcp"): nw_proto_val = "6" elif (nw_proto == "udp"): nw_proto_val = "17" else: return None, None, None rule_pv_tuple = rule.split(",")[2] if (rule_pv_tuple == None): return None, None, None rule_pv_val = rule_pv_tuple.split("=")[1] if (rule_pv_val != pv_val): return None, None, None pv_tuple = "interface=" + iface table_id_tuple = "table_id=" + table_id dl_type_tuple = "dl_type=0x800" nw_proto_tuple = "nw_proto=" + nw_proto_val nw_src_tuple = rule.split(",")[3] nw_dst_tuple = rule.split(",")[4] tp_src_tuple = rule.split(",")[5] tp_dst_tuple = rule.split(",")[6] match_str = table_id_tuple + "," + pv_tuple + "," + dl_type_tuple + "," + nw_proto_tuple + "," + nw_src_tuple + "," + nw_dst_tuple + "," + tp_src_tuple + "," + tp_dst_tuple action_str = "action=" + reg + ":" + reg_val flow_str = "flow_type=mirror" + "," + match_str + "," + action_str cmd = [self.ofctl_path, "mod-flows", self.br, flow_str] hdrstr = "Modifying mirror flow string for " + iface shell.run_cmd(hdrstr, cmd, self.logfd) time.sleep(1) actions, flow = self.get_flow_mirror_actions(type, pv_val) if (actions == None): return None, None, None flow_tuples = flow.split() if (flow_tuples == None): return None, None, None n_pkts_str = flow_tuples[2] n_pkts_new = n_pkts_str.replace(",", "").split("=")[1] return actions, n_pkts_org, n_pkts_new
def setup(self): output = shell.grep("lspci -k", self.device) if (output != ""): output = shell.grep("echo " + output, "vmxnet") if (output == ""): driver = "igb_uio" else: driver = "vmxnet3-usermap" cmd = [self.pci_bind_tool, "--bind=" + driver, self.device] shell.run_cmd( "Setting up " + self.device + " as DPDK device to " + driver, cmd, self.logfd)
def add_acls(self): acls = ["pre", "post", "redirect" ] for acl in acls: cmd = [ self.ofctl_path, "add-flow", self.br, 'flow_type=acl,type=' + self.type + ',priority=0,flags=' + acl + ',interface=' + self.iface + ',vm_uuid=' + self.uuid + ',action=allow' ] shell.run_cmd("Adding " + acl + " ACL for VM " + self.name, cmd, self.logfd) return
def __add_evpn(self): evpn_str = "evpn_id=" + self.evpn_id + \ ",vni_id=" + self.evpn_tnl_key + \ ",subnet=" + self.evpn_subnet + \ ",mask=" + self.evpn_netmask + \ ",gw_ip=" + self.evpn_gw_ip + \ ",gw_mac=" + self.evpn_gw_mac + \ ",flags=" + self.evpn_tnl_type + \ ",flags=" + self.evpn_dhcp_cfg cmd = [ self.ofctl_path, "-v", "add-evpn", self.br, self.vrf_id, evpn_str ] shell.run_cmd("Creating EVPN ID " + self.evpn_id, cmd, self.logfd)
def add_routes(self, vrf_id, evpn_id): cmd = [ self.ofctl_path, "add-flow", self.br, "flow_type=route,type=" + self.type + ",flags=evpn" + ",vrf_id=" + str(vrf_id) + ",evpn_id=" + str(evpn_id) + ",interface=" + self.iface + ",vm_uuid=" + self.uuid + ",dl_dst="+ self.mac ] shell.run_cmd("Adding evpn/vrf route to VM " + self.name, cmd, self.logfd) cmd = [ self.ofctl_path, "add-flow", self.br, "flow_type=route,type=" + self.type + ",flags=evpn-redirect,ip,vrf_id=" + str(vrf_id) + ",evpn_id=" + str(evpn_id) + ",nw_dst=" + self.ip ] shell.run_cmd("Adding evpn-redirect route to VM " + self.name, cmd, self.logfd) cmd = [ self.ofctl_path, "add-flow", self.br, "flow_type=route,type=" + self.type + ",flags=arp-route,ip,vrf_id=" + str(vrf_id) + ",evpn_id=" + str(evpn_id) + ",nw_dst=" + self.ip + ",dl_dst=" + self.mac ] shell.run_cmd("Adding arp route to VM " + self.name, cmd, self.logfd) cmd = [ self.ofctl_path, "add-flow", self.br, "flow_type=route,type=" + self.type + ",flags=evpn,vrf_id=" + str(vrf_id) + ",evpn_id=" + str(evpn_id) + ",interface=" + self.iface + ",vm_uuid=" + self.uuid + ",dl_dst=" + self.mac ] shell.run_cmd("Adding mac route to VM " + self.name, cmd, self.logfd)
def __setup_acl_mirror(self, action, acl_type, acl_dir): flowstr = None table_type = None if (acl_type == "Redirect"): table_type = "redirect" elif (acl_dir == "Ingress"): table_type = "pre" elif (acl_dir == "Egress"): table_type = "post" if (table_type == None): print "Mirrors for ACL dir " + acl_dir + " not supported yet" return if (acl_type == "default"): if (action == "Set"): flowstr = self.__setup_default_acl_mirror_flowstr(table_type) else: flowstr = self.__cleanup_default_acl_mirror_flowstr(table_type) elif (acl_type == "static"): if (action == "Set"): flowstr = self.__setup_static_acl_mirror_flowstr(table_type) else: flowstr = self.__cleanup_static_acl_mirror_flowstr(table_type) elif (acl_type == "Redirect"): if (action == "Set"): flowstr = self.__setup_static_acl_flowstr("pre") cmd = [self.ofctl_path, "add-flow", self.br, flowstr] hdrstr = action + " " + acl_type + " - " + " Enable ingress ACL" shell.run_cmd(hdrstr, cmd, self.logfd) flowstr = self.__setup_redirect_acl_mirror_flowstr(table_type) else: flowstr = self.__cleanup_redirect_acl_mirror_flowstr( table_type) elif (acl_type == "reflexive"): if (action == "Set"): flowstr = self.__setup_reflexive_acl_mirror_flowstr(table_type) else: flowstr = self.__cleanup_reflexive_acl_mirror_flowstr( table_type) if (flowstr == None): print "Mirrors for ACL type " + acl_type + " not supported yet" return cmd = [self.ofctl_path, "add-flow", self.br, flowstr] if (acl_dir != None): hdrstr = action + " " + acl_type + " " + acl_dir + " ACL mirror" else: hdrstr = action + " " + acl_type + " " + " ACL mirror" shell.run_cmd(hdrstr, cmd, self.logfd)
def add_vlan(iface, logfd): vlan_tok = iface.split(".") mgmt_iface = vlan_tok[0] vlan_id = vlan_tok[1] cmd = ["vconfig", "add", mgmt_iface, vlan_id] rc = shell.run_cmd("Creating vlan " + mgmt_iface + "." + vlan_id, cmd, logfd)
def __cleanup_drivers(self): cmd = ["rmmod", "vmxnet3-usermap"] shell.run_cmd("Removing vmxnet3-usermap driver", cmd, self.logfd) cmd = ["rmmod", "igb_uio"] shell.run_cmd("Removing igb_uio driver", cmd, self.logfd) cmd = ["rmmod", "rte_kni"] shell.run_cmd("Removing rte_kni driver", cmd, self.logfd) cmd = ["rmmod", "uio"] shell.run_cmd("Removing uio driver", cmd, self.logfd)
def __setup_vport_mirror(self, action): if (action == "Set"): if (self.agent == "dpi"): cmd = [ self.vsctl_path, "set", "interface", self.port_name, "other_config:dpi=enable", "other_config:dpi-id=" + self.mirror_id, ] else: cmd = [ self.vsctl_path, "set", "interface", self.port_name, "other_config:dyn-mirror=enable", "other_config:mirror-dst-port=" + self.mirror_dst_port, "other_config:mirror-dir=both", "other_config:mirror-id=" + self.mirror_id, ] hdrstr = "create mirror on vport " + self.port_name + " agent: " + self.agent else: if (self.agent == "dpi"): cmd = [ self.vsctl_path, "set", "interface", self.port_name, "other_config:dpi=disable", ] else: cmd = [ self.vsctl_path, "set", "interface", self.port_name, "other_config:dyn-mirror=disable", "other_config:mirror-dst-port=" + self.mirror_dst_port, "other_config:mirror-dir=both", "other_config:mirror-id=" + self.mirror_id, ] hdrstr = "destroy mirror on vport " + self.port_name + " agent: " + self.agent shell.run_cmd(hdrstr, cmd, self.logfd) time.sleep(1)
def __create(self): ipstr = self.__set_tnl_ipstr() keystr = self.__set_tnl_keystr() cmd = [ self.vsctl_path, "add-port", self.br, self.iface, "--", "set", "interface", self.iface, "type=" + self.tnl_type, ipstr, keystr ] rc = shell.run_cmd("Creating tunnel port " + self.iface, cmd, self.logfd)
def __setup_drivers(self): cmd = ["modprobe", "uio"] shell.run_cmd("Adding uio driver", cmd, self.logfd) cmd = ["insmod", self.dpdk_path + "/" + self.tgt + "/kmod/igb_uio.ko"] shell.run_cmd("Adding igb_uio driver", cmd, self.logfd) cmd = ["insmod", self.dpdk_path + "/" + self.tgt + "/kmod/rte_kni.ko"] shell.run_cmd("Adding rte_kni driver", cmd, self.logfd) cmd = [ "insmod", self.vmxnet3_path + "/vmxnet3-usermap.ko", "enable_shm=2", "num_rqs=1", "num_tqs=1,1" ] shell.run_cmd("Adding vmxnet3-usermap driver", cmd, self.logfd)
def do_deconfigure(config_file, logfd): vrfs_cfg_obj, all_vrfs_cfg, n_vrfs, evpns_cfg_obj, all_evpns_cfg, n_evpns, vms_cfg_obj, all_vms_cfg, n_vms = read_configuration( config_file, logfd) print print "Parameters:" print "Config File: " + config_file print "Number of VRFs: " + str(n_vrfs) print "Number of EVPNs: " + str(n_evpns) print "Number of VMs: " + str(n_vms) print ns_list = ns.namespaces(n_vms, 0) ns_list.destroy() cmd = ["service", "openvswitch", "restart"] shell.run_cmd("Restarting openvswitch", cmd, logfd) return 0
def __setup_mirror_dst(self, action): if (action == "Set"): cmd = [ self.vsctl_path, "--no-wait", "add-port", self.br, self.mirror_dst_port, "--", "set", "interface", self.mirror_dst_port, "type=internal", ] hdrstr = "create mirror destination " + self.mirror_dst_port else: cmd = [ self.vsctl_path, "--no-wait", "del-port", self.br, self.mirror_dst_port ] hdrstr = "destroy mirror destination " + self.mirror_dst_port shell.run_cmd(hdrstr, cmd, self.logfd) time.sleep(1)
def __build_dpdk(self, c_flag): with cd(self.dpdk_path): cmd = ["make", "uninstall"] shell.run_cmd("Uninstalling DPDK", cmd, self.logfd) if c_flag == 1: cmd = ["make", "config", "T=" + self.tgt] shell.run_cmd("Configuring DPDK", cmd, self.logfd) cmd = ["make", "install", "T=" + self.tgt] shell.run_cmd("Building and installing DPDK", cmd, self.logfd)
def __create(self, ip): if (self.port_type == "internal"): cmd = [ self.vsctl_path, "add-port", self.br, self.iface, "--", "set", "interface", self.iface, "type=internal" ] else: net.add_vlan(self.iface, self.logfd) cmd = [self.vsctl_path, "add-port", self.br, self.iface] rc = shell.run_cmd("Creating port " + self.iface, cmd, self.logfd) self.__init(ip) if (rc == 0): net.set_iface_ip(self.iface, self.netmask, ip, self.logfd) net.route_add(self.iface, ip, self.subnet, self.netmask, self.logfd)
def diff_file(filnm, old_file, new_file, args): # protect against referencing unset variables index = old_hdr = new_hdr = line = None new_date = old_date = '' opt_strip_level = '1' if args.opt_strip_level is None else args.opt_strip_level if opt_strip_level == 'ab': old_hdr = os.path.join('a', filnm) new_hdr = os.path.join('b', filnm) elif opt_strip_level == '0': old_hdr = filnm + '.orig' new_hdr = filnm else: dirnm = os.path.basename(os.getcwd()) old_hdr = os.path.join(dirnm + '.orig', filnm) new_hdr = os.path.join(dirnm, filnm) index = new_hdr use_timestamps = not _get_no_diff_timestamps(args) if not os.path.exists(old_file) or os.path.getsize(old_file) == 0: old_file = '/dev/null' old_hdr = '/dev/null' if use_timestamps: old_date = '\t1970-01-01 00:00:00.000000000 +0000' elif use_timestamps: old_date = '\t%s' % file_mtime_as_string(old_file) if not os.path.exists(new_file) or os.path.getsize(new_file) == 0: if opt_strip_level == '0': old_hdr = new_hdr new_file = '/dev/null' new_hdr = '/dev/null' if use_timestamps: new_date = '\t1970-01-01 00:00:00.000000000 +0000' elif use_timestamps: new_date = '\t%s' % file_mtime_as_string(new_file) diff_opts = _get_diff_opts(args) cmd = 'diff %s --label "%s" --label "%s" "%s" "%s"' % \ (diff_opts, old_hdr + old_date, new_hdr + new_date, old_file, new_file) result = shell.run_cmd(cmd) if result.eflags == 1: if not _get_no_diff_index(args): index_str = 'Index: %s\n%s\n' % (index, '=' * 67) result = result._replace(stdout=index_str + result.stdout) return result
def same_contents(file1, file2): result = shell.run_cmd('diff -q "%s" "%s"' % (file1, file2)) return result.eflags == 0