def eject(self): cmdline = "ip netns exec %s ip link set dev %s up" \ % (self._netns, self._interface) LOG.debug('set dev %s up in %s' \ % (self._interface, self._netns)) subprocess_compat.check_output(cmdline.split()) time.sleep(self._wait)
def eject(self): cmdline = "ip netns exec %s iptables -i %s -D INPUT -j DROP" \ % (self._netns, self._interface) LOG.debug('take packets coming from %s in %s' \ % (self._interface, self._netns)) subprocess_compat.check_output(cmdline.split()) time.sleep(self._wait)
def delete(self): """ Delete the interface from bridge """ LOG.debug("delete") brname = self._interface['bridge']['brname'] ifname_peer = self._get_peer_ifname() cmdline = "brctl delif %s %s" % (brname, ifname_peer) LOG.debug('Trunk: deleting %s from %s' % (brname, ifname_peer)) try: subprocess_compat.check_output(cmdline.split()) except subprocess.CalledProcessError: pass except OSError: pass cmdline = "ip link delete " + ifname_peer LOG.debug('Trunk: deleting the iface: ' + ifname_peer) try: subprocess_compat.check_output(cmdline.split()) except subprocess.CalledProcessError: pass except OSError: pass
def inject(self): cmdline = "ip netns exec %s ip link set dev %s down" \ % (self._netns, self._interface) LOG.debug('set dev %s down in %s' \ % (self._interface, self._netns)) subprocess_compat.check_output(cmdline.split()) # check the zk host actually went down check_zookeeper_host(self._ip, alive=False)
def eject(self): cmdline = "ip netns exec %s ip link set dev %s up" \ % (self._netns, self._interface) LOG.debug('set dev %s up in %s' \ % (self._interface, self._netns)) subprocess_compat.check_output(cmdline.split()) # Check the zk host actually went up and in quorum check_zookeeper_host(self._ip, alive=True)
def put_veth_to_mm_ns(future): self._interface = future.result() ifname = self._interface['ifname'] cmdline = 'ip link set %s netns %s' % (ifname, nsname) LOG.debug('VethNs: putting host veth to MM ns ' + cmdline) subprocess_compat.check_output(cmdline.split()) cmdline = 'ip netns exec %s ip link set %s up' % (nsname, ifname) LOG.debug('VethNs: setting IF up ' + cmdline) subprocess_compat.check_output(cmdline.split())
def delete(self): """ Delete this veth + NS """ cmdline = "ip netns del " + self._get_nsname() LOG.debug('VethNs: deleting the ns: ' + cmdline) try: subprocess_compat.check_output(cmdline.split()) except subprocess.CalledProcessError as e: LOG.exception(e) except OSError as e: LOG.exception(e)
def _clear(): cmdline = ("for ns in `ip netns | grep -- ns-if-`; " "do sudo -i ip netns delete $ns 2>/dev/null; done") logging.debug("cmdline: " + cmdline) try: subprocess_compat.check_output(cmdline, shell=True) except subprocess.CalledProcessError: pass cmdline = ("for iface in `ip link | grep -- if- | cut -d ':' -f 2`; " "do sudo -i ip link delete $iface 2>/dev/null; done") logging.debug("cmdline: " + cmdline) try: subprocess_compat.check_output(cmdline, shell=True) except subprocess.CalledProcessError: pass
def execute(self, cmdline, timeout=None): """Executes cmdline inside the namespace Args: cmdline: command line string that'll be executed in the network namespace. timeout: timeout in second Returns: output as a bytestring Raises: subprocess.CalledProcessError: when the command exists with non-zero value, including timeout. OSError: when the executable is not found or some other error invoking """ cmdline = self._process_cmdline(cmdline, timeout) LOG.debug('VethNs: executing command: %s', cmdline) #NOTE(tomoe): I know shell=True is insecure, but I trust people here :) result = subprocess_compat.check_output(cmdline, shell=True) LOG.debug('Result=%r', result) return result
def _get_midolman_logs_for_test(self, test_id): """Returns a string that contains midolman logs relevant to the test""" markers = self._get_markers_for_test(test_id) cmdline = self.tools_path + '/' + \ "dump_midolman_logs_for_test.sh '%s' '%s' '%s'" % ( markers['start'], markers['end'], test_id) return subprocess_compat.check_output(cmdline, shell=True)
def create(self): LOG.debug('create') brname = self._interface['bridge']['brname'] ifname = self._get_ifname() ifname_peer = self._get_peer_ifname() #NOTE: may want to have idempotent version? try : # create a veth pair cmdline = "ip link add dev %s type veth peer name %s" % ( ifname, ifname_peer) LOG.debug('VethNs: creating a veth pair: ' + cmdline) subprocess_compat.check_output(cmdline.split()) # add an end of the pair to bridge ifname_peer = self._get_peer_ifname() LOG.debug('Trunk: adding %s to %s' % (brname, ifname_peer)) cmdline = "brctl addif %s %s" % (brname, ifname_peer) subprocess_compat.check_output(cmdline.split()) cmdline = "ip link set dev %s up" % (ifname_peer,) subprocess_compat.check_output(cmdline.split()) return self._interface except Exception as e: LOG.error('Either creating veth or namespace failed: ' + str(e)) raise e
def create(self): LOG.debug('create') brname = self._interface['bridge']['brname'] ifname = self._get_ifname() ifname_peer = self._get_peer_ifname() #NOTE: may want to have idempotent version? try: # create a veth pair cmdline = "ip link add dev %s type veth peer name %s" % ( ifname, ifname_peer) LOG.debug('VethNs: creating a veth pair: ' + cmdline) subprocess_compat.check_output(cmdline.split()) # add an end of the pair to bridge ifname_peer = self._get_peer_ifname() LOG.debug('Trunk: adding %s to %s' % (brname, ifname_peer)) cmdline = "brctl addif %s %s" % (brname, ifname_peer) subprocess_compat.check_output(cmdline.split()) cmdline = "ip link set dev %s up" % (ifname_peer, ) subprocess_compat.check_output(cmdline.split()) return self._interface except Exception as e: LOG.error('Either creating veth or namespace failed: ' + str(e)) raise e
def report(self, stream): """Add per suite logs right at the end of a test run""" cmd = self.tools_path + '/' + 'dump_per_suite_logs.sh' out = subprocess_compat.check_output(cmd) stream.writeln(out)
def _mark_mm_logs(self, marker_text): cmdline = self.tools_path + '/' + 'mark_mm_logs.sh ' + marker_text subprocess_compat.check_output(cmdline, shell=True)
def _get_per_test_logs(self, test): """Returns mmm log in string""" cmd = self.tools_path + '/' + 'dump_per_test_logs.sh' return subprocess_compat.check_output(cmd)
def stop_midolman_agents(): """Stops all Midolman agents.""" subprocess_compat.check_output('cd %s; ./stop' % get_midolman_script_dir(), shell=True)
def stop_midolman_agents(): """Stops all Midolman agents.""" subprocess_compat.check_output( 'cd %s; ./stop' % get_midolman_script_dir(), shell=True)
def brctl(instr, trunk): cmd = 'brctl %s brv0 %s' % (instr, ifname(trunk)) subprocess_compat.check_output(cmd.split())
def create(self): """Create a veth + network namespace. Args: N/A Returns: None Raises: subprocess.CalledProcessError: when one of the commands to set up the veth+ns environment fails """ LOG.debug('create') ifname = self._get_ifname() ifname_peer = self._get_peer_ifname() #NOTE: may want to have idempotent version? try: # create a veth pair cmdline = "ip link add dev %s type veth peer name %s" % ( ifname, ifname_peer) LOG.debug('VethNs: creating a veth pair: ' + cmdline) subprocess_compat.check_output(cmdline.split()) # create a network namespace cmdline = "ip netns add " + self._get_nsname() LOG.debug('VethNs: creating a network namespace: ' + cmdline) subprocess_compat.check_output(cmdline.split()) if not self._interface.get('ipv6_addr'): # disable ipv6 if there's no address configured cmdline = 'sysctl -w net.ipv6.conf.default.disable_ipv6=1' self.execute(cmdline) # configure hw address if any if self._interface.get('hw_addr'): cmdline = 'ip link set address %s dev %s' % ( self._interface['hw_addr'], ifname_peer) LOG.debug('VethNs: setting HW address: ' + cmdline) subprocess_compat.check_output(cmdline.split()) # put the veth peer IF to the namespace that's just created. cmdline = "ip link set dev %s up netns %s" % (ifname_peer, self._get_nsname()) LOG.debug('VethNs: putting a veth to a ns: ' + cmdline) subprocess_compat.check_output(cmdline.split()) # configure ipv4 address if any for addr in self._interface.get('ipv4_addr'): cmdline = 'ip addr add %s dev %s' % (addr, ifname_peer) LOG.debug('VethNs: adding ip address in the ns: ' + cmdline) self.execute(cmdline) # add default route if gateway is configured if self._interface.get('ipv4_gw'): cmdline = 'ip netns exec %s ip route add default via %s' %\ (self._get_nsname(), self._interface['ipv4_gw']) LOG.debug('VethNs: adding default route:' + cmdline) self.execute(cmdline) # takes care of ipv6 address if self._interface.get('ipv6_addr'): #TODO pass else: cmdline = 'ip -6 addr flush dev %s' % ifname_peer LOG.debug('VethNs: flushing ipv6 address: ' + cmdline) self.execute(cmdline) # set MTU if any if self._interface.get('mtu'): cmdline = "ip link set mtu %s dev %s" % ( self._interface['mtu'], ifname_peer) LOG.debug('VethNs: configuring mtu in the ns: ' + cmdline) self.execute(cmdline) return self._interface except Exception as e: LOG.error('Either creating veth or namespace failed: ' + str(e)) raise e