def create_userspace_bridge(name): # sudo ovs-vsctl add-br br1 -- set Bridge br1 datapath_type=netdev cmd = [ 'sudo', 'ovs-vsctl', 'add-br', name, "--", 'set', 'Bridge', name, 'datapath_type=netdev' ] utils.run_cmd(cmd)
def set_mgmt_ip(name, ips): #sudo ip addr add 192.168.1.1/24 dev sdn_rtr1 #sudo ip link set sdn_rtr1 up for ip in ips: cmd = ['sudo', 'ip', 'addr', 'add', ip, 'dev', name] utils.run_cmd(cmd) cmd1 = ['sudo', 'ip', 'link', 'set', name, 'up'] utils.run_cmd(cmd1)
def create_link(swname, ifname, nodename, cidr, mac=None): # sudo ovs-docker add-port ovs-br1 eth1 node1 --ipaddress=192.168.1.1/24 # --macaddress 00:00:00:00:00:01 ipa = "--ipaddress=" + cidr cmd = ['sudo', 'ovs-docker', 'add-port', swname, ifname, nodename, ipa] if mac: maca = "--macaddress=" + mac cmd.append(maca) utils.run_cmd(cmd)
def config_qos(tapif, qos): cmd = [ 'sudo', 'tc', 'qdisc', 'add', 'dev', tapif, 'root', 'handle', '1:0', 'netem', 'delay', qos["latency"], qos["jitter"], 'loss', qos["pktloss"] ] response = utils.run_cmd(cmd) cmd = [ 'sudo', 'tc', 'qdisc', 'add', 'dev', tapif, 'parent', '1:1', 'handle', '10:', 'tbf', 'rate', qos["bandwidth"], 'buffer', '1600', 'limit', '3000' ] response = utils.run_cmd(cmd)
def set_protocol_version(name, version): # sudo ovs-vsctl set bridge ovs-br1 protocols=OpenFlow13 if str("1.3") == version: v = "OpenFlow13" elif str("1.2") == version: v = "OpenFlow12" else: v = "OpenFlow10" version = "protocols=" + v cmd = ['sudo', 'ovs-vsctl', 'set', 'bridge', name, version] # version 1.4,1.5 support to be checked utils.run_cmd(cmd)
def add_static_route(name, subnet, via): # sudo docker exec -it a3 ip route add 10.20.20.0/24 via 10.10.10.1 cmd = [ 'sudo', 'docker', 'exec', '-it', name, 'ip', 'route', 'add', subnet, 'via', via ] return utils.run_cmd(cmd)
def run_iperfc_detach_in_container(name, serverip, connections=1, t=30): # sudo docker exec -it a3 iperf -c 10.10.10.1 -P 5 cmd = [ 'sudo', 'docker', 'exec', '-d', name, 'iperf', '-i', '10', '-c', serverip, '-P', connections, '-t', t ] return utils.run_cmd(cmd)
def run_iperf_udpc_in_container(name, serverip, bandwidth, connections, t=30): # sudo docker exec -it a3 iperf -c 10.10.10.1 -P 5 bandwidth = str(bandwidth) + "m" cmd = [ 'sudo', 'docker', 'exec', '-t', name, 'iperf', '-u', '-i', '10', '-c', serverip, '-b', bandwidth, '-P', connections, '-t', t ] result = utils.run_cmd(cmd) print result return
def verify_image_exits(img): img_name, img_version = img.split(':') # sudo docker images ubuntu:trusty cmd = ['sudo', 'docker', 'images', img] response = utils.run_cmd(cmd) # verify the img exists in the output if img_name in response and img_version in response: return {"result": "pass", "detail": response} else: return {"result": "fail", "detail": response}
def create_container(name, img, net="none"): # sudo docker run -itd --name=node1 ubuntu:trusty n = "--name=" + name network = "--network=" + net hostname = "--hostname=" + name cmd = [ 'sudo', 'docker', 'run', '--cap-add=NET_ADMIN', '--privileged', '-itd', hostname, n, network, img ] return utils.run_cmd(cmd)
def get_port_name(container, ifname): # ref: https://github.com/openvswitch/ovs/blob/master/utilities/ovs-docker # fn : get_port_for_container_interface () cn = "external_ids:container_id=" + str(container) ifname = "external_ids:container_iface=" + str(ifname) cmd = [ 'sudo', 'ovs-vsctl', '--data=bare', '--no-heading', '--columns=name', 'find', 'interface', cn, ifname ] response = utils.run_cmd(cmd) return response.strip("\n")
def create_container(name, img, net="none", cpu="0.1", mem="100m"): # sudo docker run -itd --name=node1 ubuntu:trusty n = "--name=" + name network = "--network=" + net hostname = "--hostname=" + name cpus = "--cpus=" + cpu memory = "--memory=" + mem cmd = [ 'sudo', 'docker', 'run', cpus, memory, '--cap-add=NET_ADMIN', '--privileged', '-itd', hostname, n, network, img ] return utils.run_cmd(cmd)
def adminup_link(ifname): cmd = ['sudo', 'ifconfig', ifname, 'up'] utils.run_cmd(cmd)
def admindown_link(ifname): cmd = ['sudo', 'ifconfig', ifname, 'down'] utils.run_cmd(cmd)
def run_ping_in_container(name, ip): # sudo docker exec -it a3 ping 10.20.20.2 -c 5 cmd = ['sudo', 'docker', 'exec', '-t', name, 'ping', '-c', '2', ip] return utils.run_cmd(cmd)
def delete_container(name): cmd = ['sudo', 'docker', 'rm', name] return utils.run_cmd(cmd)
def stop_container(name): # sudo docker stop node1 cmd = ['sudo', 'docker', 'stop', name] return utils.run_cmd(cmd)
def config_port_as_patch(portname): # ovs-vsctl add-port br0 tap0 cmd = ['sudo', 'ovs-vsctl', 'set', 'interface', portname, 'type=patch'] utils.run_cmd(cmd)
def run_pkill_in_container(name, pname): # sudo docker exec -it a3 pkill -9 iperf cmd = ['sudo', 'docker', 'exec', '-t', name, 'pkill', '-9', pname] return utils.run_cmd(cmd)
def run_cmd_in_container(args): cmd = ['sudo', 'docker', 'exec', '-t'] + args output = utils.run_cmd(cmd) print output return
def cleanup(self): cpath = os.path.dirname(os.path.abspath(__file__)) + "/cleanup.sh" cmd = ['sh', cpath] return utils.run_cmd(cmd)
def create_bridge(name): # sudo ovs-vsctl add-br ovs-br1 cmd = ['sudo', 'ovs-vsctl', 'add-br', name] utils.run_cmd(cmd)
def create_container(name, img): # sudo docker run -itd --name=node1 ubuntu:trusty n = "--name=" + name cmd = ['sudo', 'docker', 'run', '-itd', n, img] return utils.run_cmd(cmd)
def delete_bridge(name): # sudo ovs-vsctl del-br ovs-br1 cmd = ['sudo', 'ovs-vsctl', 'del-br', name] utils.run_cmd(cmd)
def disable_inband(name): #sudo ovs-vsctl set bridge br0 other-config:disable-in-band=true option = 'other-config:disable-in-band=true' cmd = ['sudo', 'ovs-vsctl', 'set', 'bridge', name, option] utils.run_cmd(cmd)
def addport(name, portname): # ovs-vsctl add-port br0 tap0 cmd = ['sudo', 'ovs-vsctl', 'add-port', name, portname] utils.run_cmd(cmd)
def set_controller(name, controller): # sudo ovs-vsctl set-controller ovs-br1 tcp:0.0.0.0:6633 cmd = ['sudo', 'ovs-vsctl', 'set-controller', name, controller] utils.run_cmd(cmd)
def set_datapath_id(name, id): # sudo ovs-vsctl set bridge ovs-br1 other_config:datapath-id=#{dpid}" # convert id in to 16digit hex datapath = "other_config:datapath-id=" + str(dpid(id)) cmd = ['sudo', 'ovs-vsctl', 'set', 'bridge', name, datapath] utils.run_cmd(cmd)
def peer_patch_ports(portname, peerport): # ovs-vsctl add-port br0 tap0 peer = "options:peer=" + peerport cmd = ['sudo', 'ovs-vsctl', 'set', 'interface', portname, peer] utils.run_cmd(cmd)