- net_namespace = 'ovim-' + str(vlan)
- namespace_interface = 'tap-' + str(vlan)
-
- command = 'sudo ip netns add ' + net_namespace
- self.logger.debug("command: " + command)
- (_, stdout, _) = self.ssh_conn.exec_command(command)
- content = stdout.read()
-
- command = 'sudo ip link add tap-' + str(vlan) + ' type veth peer name ovs-tap-' + str(vlan)
- self.logger.debug("command: " + command)
- (_, stdout, _) = self.ssh_conn.exec_command(command)
- content = stdout.read()
-
- command = 'sudo ovs-vsctl add-port br-int ovs-tap-' + str(vlan) + ' tag=' + str(vlan)
- self.logger.debug("command: " + command)
- (_, stdout, _) = self.ssh_conn.exec_command(command)
- content = stdout.read()
-
- command = 'sudo ip link set tap-' + str(vlan) + ' netns ' + net_namespace
- self.logger.debug("command: " + command)
- (_, stdout, _) = self.ssh_conn.exec_command(command)
- content = stdout.read()
-
- command = 'sudo ip netns exec ' + net_namespace + ' ip link set dev tap-' + str(vlan) + ' up'
- self.logger.debug("command: " + command)
- (_, stdout, _) = self.ssh_conn.exec_command(command)
- content = stdout.read()
-
- command = 'sudo ip link set dev ovs-tap-' + str(vlan) + ' up'
- self.logger.debug("command: " + command)
- (_, stdout, _) = self.ssh_conn.exec_command(command)
- content = stdout.read()
-
- command = 'sudo ip netns exec ' + net_namespace + ' ip link set dev lo up'
- self.logger.debug("command: " + command)
- (_, stdout, _) = self.ssh_conn.exec_command(command)
- content = stdout.read()
-
- command = 'sudo ip netns exec ' + net_namespace + ' ' + ' ifconfig ' + namespace_interface \
- + ' ' + ip_listen_address + ' netmask ' + netmask
- self.logger.debug("command: " + command)
- (_, stdout, _) = self.ssh_conn.exec_command(command)
- content = stdout.read()
-
- if len(content) == 0:
+ ns_qouter = '{}-qrouter'.format(str(vlan))
+ qrouter_ovs_veth = '{}-vethOQ'.format(str(vlan))
+ qrouter_ns_veth = '{}-vethQO'.format(str(vlan))
+ qrouter_br_veth = '{}-vethBQ'.format(str(vlan))
+ qrouter_ns_router_veth = '{}-vethQB'.format(str(vlan))
+
+ command = 'sudo ovs-vsctl del-port br-int {}'.format(qrouter_ovs_veth)
+ self.run_command(command, ignore_exit_status=True)
+
+ # down ns veth
+ command = 'sudo ip netns exec {} ip link set dev {} down'.format(ns_qouter, qrouter_ns_veth)
+ self.run_command(command, ignore_exit_status=True)
+
+ command = 'sudo ip netns exec {} ip link delete {} '.format(ns_qouter, qrouter_ns_veth)
+ self.run_command(command, ignore_exit_status=True)
+
+ command = 'sudo ip netns del ' + ns_qouter
+ self.run_command(command)
+
+ # down ovs veth interface
+ command = 'sudo ip link set dev {} down'.format(qrouter_br_veth)
+ self.run_command(command, ignore_exit_status=True)
+
+ # down br veth interface
+ command = 'sudo ip link set dev {} down'.format(qrouter_ovs_veth)
+ self.run_command(command, ignore_exit_status=True)
+
+ # delete veth interface
+ command = 'sudo ip link delete {} '.format(link, qrouter_ovs_veth)
+ self.run_command(command, ignore_exit_status=True)
+
+ # down br veth interface
+ command = 'sudo ip link set dev {} down'.format(qrouter_ns_router_veth)
+ self.run_command(command, ignore_exit_status=True)
+
+ # delete veth interface
+ command = 'sudo ip link delete {} '.format(link, qrouter_ns_router_veth)
+ self.run_command(command, ignore_exit_status=True)
+
+ # down br veth interface
+ command = 'sudo brctl delif {} {}'.format(link, qrouter_br_veth)
+ self.run_command(command)
+
+ # delete NS
+ return True
+ except RunCommandException as e:
+ self.logger.error("delete_qrouter_connection ssh Exception: {}".format(str(e)))
+ return False
+
+ def create_qrouter_ovs_connection(self, vlan, gateway, dhcp_cidr):
+ """
+ Create qrouter Namesapce with all veth interfaces need it between NS and OVS
+ :param vlan:
+ :param gateway:
+ :return:
+ """
+
+ if self.test:
+ return True
+
+ try:
+ ns_qouter = '{}-qrouter'.format(str(vlan))
+ qrouter_ovs_veth ='{}-vethOQ'.format(str(vlan))
+ qrouter_ns_veth = '{}-vethQO'.format(str(vlan))
+
+ # Create NS
+ command = 'sudo ip netns add {}'.format(ns_qouter)
+ self.run_command(command)
+
+ # Create pait veth
+ command = 'sudo ip link add {} type veth peer name {}'.format(qrouter_ns_veth, qrouter_ovs_veth)
+ self.run_command(command, ignore_exit_status=True)
+
+ # up ovs veth interface
+ command = 'sudo ip link set dev {} up'.format(qrouter_ovs_veth)
+ self.run_command(command)
+
+ # add ovs veth to ovs br-int
+ command = 'sudo ovs-vsctl add-port br-int {} tag={}'.format(qrouter_ovs_veth, vlan)
+ self.run_command(command)
+
+ # add veth to ns
+ command = 'sudo ip link set {} netns {}'.format(qrouter_ns_veth, ns_qouter)
+ self.run_command(command)
+
+ # up ns loopback
+ command = 'sudo ip netns exec {} ip link set dev lo up'.format(ns_qouter)
+ self.run_command(command)
+
+ # up ns veth
+ command = 'sudo ip netns exec {} ip link set dev {} up'.format(ns_qouter, qrouter_ns_veth)
+ self.run_command(command)
+
+ from netaddr import IPNetwork
+ ip_tools = IPNetwork(dhcp_cidr)
+ cidr_len = ip_tools.prefixlen
+
+ # set gw to ns veth
+ command = 'sudo ip netns exec {} ip address add {}/{} dev {}'.format(ns_qouter, gateway, cidr_len, qrouter_ns_veth)
+ self.run_command(command)
+
+ return True
+
+ except RunCommandException as e:
+ self.logger.error("Create_dhcp_interfaces ssh Exception: {}".format(str(e)))
+ return False
+
+ def add_ns_routes(self, vlan, routes):
+ """
+
+ :param vlan:
+ :param routes:
+ :return:
+ """
+
+ if self.test:
+ return True
+
+ try:
+ ns_qouter = '{}-qrouter'.format(str(vlan))
+ qrouter_ns_router_veth = '{}-vethQB'.format(str(vlan))
+
+ for key, value in routes.iteritems():
+ # up ns veth
+ if key == 'default':
+ command = 'sudo ip netns exec {} ip route add {} via {} '.format(ns_qouter, key, value)
+ else:
+ command = 'sudo ip netns exec {} ip route add {} via {} dev {}'.format(ns_qouter, key, value,
+ qrouter_ns_router_veth)
+
+ self.run_command(command)
+
+ return True
+
+ except RunCommandException as e:
+ self.logger.error("add_ns_routes, error adding routes to namesapce, {}".format(str(e)))
+ return False
+
+ def create_qrouter_br_connection(self, vlan, cidr, link):
+ """
+ Create veth interfaces between user bridge (link) and OVS
+ :param vlan:
+ :param link:
+ :return:
+ """
+
+ if self.test:
+ return True
+
+ try:
+ ns_qouter = '{}-qrouter'.format(str(vlan))
+ qrouter_ns_router_veth = '{}-vethQB'.format(str(vlan))
+ qrouter_br_veth = '{}-vethBQ'.format(str(vlan))
+
+ command = 'sudo brctl show | grep {}'.format(link['iface'])
+ content = self.run_command(command, ignore_exit_status=True)
+
+ if content > '':
+ # Create pait veth
+ command = 'sudo ip link add {} type veth peer name {}'.format(qrouter_br_veth, qrouter_ns_router_veth)
+ self.run_command(command)
+
+ # up ovs veth interface
+ command = 'sudo ip link set dev {} up'.format(qrouter_br_veth)
+ self.run_command(command)
+
+ # add veth to ns
+ command = 'sudo ip link set {} netns {}'.format(qrouter_ns_router_veth, ns_qouter)
+ self.run_command(command)
+
+ # up ns veth
+ command = 'sudo ip netns exec {} ip link set dev {} up'.format(ns_qouter, qrouter_ns_router_veth)
+ self.run_command(command)
+
+ command = 'sudo ip netns exec {} ip address add {} dev {}'.format(ns_qouter,
+ link['nat'],
+ qrouter_ns_router_veth)
+ self.run_command(command)
+
+ # up ns veth
+ command = 'sudo brctl addif {} {}'.format(link['iface'], qrouter_br_veth)
+ self.run_command(command)
+
+ # up ns veth
+ command = 'sudo ip netns exec {} iptables -t nat -A POSTROUTING -o {} -s {} -d {} -j MASQUERADE' \
+ .format(ns_qouter, qrouter_ns_router_veth, link['nat'], cidr)
+ self.run_command(command)
+