def ovs_set_port(args): for p in args.port: ovs_get_br(p, args.name) if args.tags: tags = list(args.tags) print('Set vlan tag {0} for port {1}'.format(','.join(tags), p)) if len(args.tags) == 1: shell('ovs-vsctl set port {0} tag={1}'.format(p, tags[0])) elif len(args.tags) > 1: shell('ovs-vsctl set port {0} trunks={1}'.format( p, ','.join(tags)))
def main(args, cfg): r1 = args.src r2 = args.dst name = 'br-{0}{1}'.format(r1.router, r2.router) p1 = r1.ifname if r1.ifname.endswith(r1.router) else '{0}-{1}'.format( r1.ifname, r1.router) p2 = r2.ifname if r2.ifname.endswith(r2.router) else '{0}-{1}'.format( r2.ifname, r2.router) shell( 'ovs-vsctl add-br {0} -- add-port {0} {1} -- add-port {0} {2}'.format( name, p1, p2))
def main(args, cfg): # Terminate processes processes = ['dnsmasq', 'zebra', 'vtysh', 'ospfd', 'bgpd'] for p in psutil.process_iter(attrs=['name', 'pid']): if p.info['name'] in processes: print(p.info); try: os.kill(p.info['pid'], signal.SIGTERM) except Exception as e: print(e) time.sleep(1) for p in psutil.process_iter(attrs=['name', 'pid']): if p.info['name'] in processes: print(p.info); # Terminate dhclient for dh in glob.glob('/tmp/dhclient-*'): with open(dh, 'r') as fp: pid = fp.read() fp.close() try: os.kill(int(pid), signal.SIGTERM) except Exception as e: print(e) os.unlink(dh) for pn in glob.glob('/sys/class/net/veth*'): if not os.path.exists(pn): continue bn = os.path.basename(pn) shell('ip link del {0}'.format(bn)) shell('rm -f /tmp/*.leases') shell('ip --all netns delete') shell('docker rm -vf faucet') # Delete all ovs bridge out = shell_out('ovs-vsctl list-br') for br in out.splitlines(): shell('ovs-vsctl del-br {0}'.format(br))
def ovs_add_allports(br): nsmap, _ = build_netns_map() for i in range(1, 16): netns_names = [ 'r{0}'.format(i), 'c{0}'.format(i), 's{0}'.format(i), 'h{0}'.format(i), ] for ns in netns_names: if ns not in nsmap.values(): continue for ifidx in range(0, 10): port = 'veth{0}-{1}'.format(ifidx, ns) port_exists = os.path.exists('/sys/class/net/{0}'.format(port)) command = 'ovs-vsctl add-port {0} {1} 2>/dev/null'.format( br, port) if port_exists and shell(command) == 0: print("Add port {0}".format(port))
def if_exists_netns(netns, ifname): cmd = 'ip netns exec {} test -e /sys/class/net/{}'.format(netns, ifname) ret = shell(cmd) return (ret == 0)
def main(args, cfg): ifname = args.ifname ret = 0 if not if_exists_netns(args.netns, ifname): ret = shell('ip netns exec {} ip link add dev {}'.format( args.netns, ifname)) shell('ip netns exec {} ip link set dev {} up'.format( args.netns, ifname)) if args.vlan: shell( 'ip netns exec {0} ip link add link {1} name {1}.{2} type vlan id {2}' .format(args.netns, ifname, args.vlan)) shell('ip netns exec {0} ip link set dev {1}.{2} up'.format( args.netns, ifname, args.vlan)) shell('ip netns exec {0} ip addr flush dev {1}'.format( args.netns, ifname)) if isinstance(args.address, str): shell('ip netns exec {0} ip addr add dev {1}.{2} {3}'.format( args.netns, ifname, args.vlan, args.address)) return ret
def ovs_add_br(br): if shell('ovs-vsctl br-exists {0}'.format(br)) != 0: shell('ovs-vsctl add-br {0}'.format(br))
def main(args, cfg): shell('ip netns add {0}'.format(args.name)) for i in range(0, args.ifcount): pair = ['veth{0}-{1}'.format(i, args.name), 'veth{0}'.format(i)] shell('ip link add dev {0} type veth peer name {1} netns {2}'.format( pair[0], pair[1], args.name)) shell('ip link set dev {0} up'.format(pair[0])) shell('ip netns exec {0} ip link set dev {1} up'.format( args.name, pair[1])) if not args.address: continue if len(args.address) <= i: continue cidr = args.address[i] if not re.search('/[0-9]+', cidr): cidr = '{0}/24'.format(cidr) print("Set ip address {0} for {1}".format(cidr, pair[1])) shell('ip netns exec {0} ip addr add dev {1} {2}'.format( args.name, pair[1], cidr)) shell('ip netns exec {0} ip link set dev lo up'.format(args.name))