+ # Execute the script
+ cmd = 'vppctl exec {}'.format(filename)
+ (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
+ if ret != 0:
+ logging.debug(stderr)
+
+ print("\nA script as been created at {}".format(filename))
+ print("This script can be run using the following:")
+ print("vppctl exec {}\n".format(filename))
+
+ def _iperf_vm_questions(self, node):
+ """
+ Ask the user some questions and get a list of interfaces
+ and IPv4 addresses associated with those interfaces
+
+ :param node: Node dictionary.
+ :type node: dict
+ :returns: A list or interfaces with ip addresses
+ :rtype: list
+ """
+
+ vpputl = VPPUtil()
+ interfaces = vpputl.get_hardware(node)
+ if interfaces == {}:
+ return []
+
+ # First delete all the Virtual interfaces
+ for intf in sorted(interfaces.items()):
+ name = intf[0]
+ if name[:7] == 'Virtual':
+ cmd = 'vppctl delete vhost-user {}'.format(name)
+ (ret, stdout, stderr) = vpputl.exec_command(cmd)
+ if ret != 0:
+ logging.debug('{} failed on node {} {}'.format(
+ cmd, node['host'], stderr))
+
+ # Create a virtual interface, for each interface the user wants to use
+ interfaces = vpputl.get_hardware(node)
+ if interfaces == {}:
+ return []
+ interfaces_with_virtual_interfaces = []
+ inum = 1
+
+ while True:
+ print('\nPlease pick one interface to connect to the iperf VM.')
+ for intf in sorted(interfaces.items()):
+ name = intf[0]
+ if name == 'local0':
+ continue
+
+ question = "Would you like connect this interface {} to " \
+ "the VM [y/N]? ".format(name)
+ answer = self._ask_user_yn(question, 'n')
+ if answer == 'y':
+ self._sockfilename = '/var/run/vpp/{}.sock'.format(
+ name.replace('/', '_'))
+ if os.path.exists(self._sockfilename):
+ os.remove(self._sockfilename)
+ cmd = 'vppctl create vhost-user socket {} server'.format(
+ self._sockfilename)
+ (ret, stdout, stderr) = vpputl.exec_command(cmd)
+ if ret != 0:
+ raise RuntimeError(
+ "Couldn't execute the command {}, {}.".format(
+ cmd, stderr))
+ vintname = stdout.rstrip('\r\n')
+
+ cmd = 'chmod 777 {}'.format(self._sockfilename)
+ (ret, stdout, stderr) = vpputl.exec_command(cmd)
+ if ret != 0:
+ raise RuntimeError(
+ "Couldn't execute the command {}, {}.".format(
+ cmd, stderr))
+
+ interface = {'name': name,
+ 'virtualinterface': '{}'.format(vintname),
+ 'bridge': '{}'.format(inum)}
+ inum += 1
+ interfaces_with_virtual_interfaces.append(interface)
+ return interfaces_with_virtual_interfaces
+
+ def create_and_bridge_iperf_virtual_interface(self):
+ """
+ After asking the user some questions, and create and bridge a
+ virtual interface to be used with iperf VM
+
+ """
+
+ for i in self._nodes.items():
+ node = i[1]
+
+ # Show the current bridge and interface configuration
+ print("\nThis the current bridge configuration:")
+ ifaces = VPPUtil.show_bridge(node)
+ question = "\nWould you like to keep this configuration [Y/n]? "
+ answer = self._ask_user_yn(question, 'y')
+ if answer == 'y':
+ self._sockfilename = '/var/run/vpp/{}.sock'.format(
+ ifaces[0]['name'].replace('/', '_'))
+ if os.path.exists(self._sockfilename):
+ continue
+
+ # Create a script that builds a bridge configuration with
+ # physical interfaces and virtual interfaces
+ ints_with_vints = self._iperf_vm_questions(node)
+ content = ''
+ for intf in ints_with_vints:
+ vhoststr = '\n'.join([
+ 'comment { The following command creates the socket }',
+ 'comment { and returns a virtual interface }',
+ 'comment {{ create vhost-user socket '
+ '/var/run/vpp/sock{}.sock server }}\n'.format(
+ intf['bridge'])
+ ])
+
+ setintdnstr = 'set interface state {} down\n'.format(
+ intf['name'])
+
+ setintbrstr = 'set interface l2 bridge {} {}\n'.format(
+ intf['name'], intf['bridge'])
+ setvintbrstr = 'set interface l2 bridge {} {}\n'.format(
+ intf['virtualinterface'], intf['bridge'])