diff options
Diffstat (limited to 'extras/vpp_config/vpplib')
-rw-r--r-- | extras/vpp_config/vpplib/AutoConfig.py | 132 | ||||
-rw-r--r-- | extras/vpp_config/vpplib/VPPUtil.py | 41 |
2 files changed, 167 insertions, 6 deletions
diff --git a/extras/vpp_config/vpplib/AutoConfig.py b/extras/vpp_config/vpplib/AutoConfig.py index 49c7d54257c..ab943e0ef21 100644 --- a/extras/vpp_config/vpplib/AutoConfig.py +++ b/extras/vpp_config/vpplib/AutoConfig.py @@ -17,6 +17,7 @@ import logging import os import re import yaml +import ipaddress from vpplib.VPPUtil import VPPUtil from vpplib.VppPCIUtil import VppPCIUtil @@ -84,6 +85,36 @@ class AutoConfig(object): logging.debug(stderr) @staticmethod + def _ask_user_ipv4(): + """ + Asks the user for a number within a range. + default is returned if return is entered. + + :returns: IP address and prefix len + :rtype: tuple + """ + + while True: + answer = raw_input("Please enter the IPv4 Address [n.n.n.n]: ") + try: + ipaddr = ipaddress.ip_address(u'{}'.format(answer)) + except: + print "Please enter a valid IPv4 address." + continue + + answer = raw_input("Please enter the netmask [n.n.n.n]: ") + try: + netmask = ipaddress.ip_address(u'{}'.format(answer)) + pl = ipaddress.ip_network(u'0.0.0.0/{}'.format(netmask)) + plen = pl.exploded.split('/')[1] + break + except: + print "Please enter a valid IPv4 address and netmask." + continue + + return ipaddr, plen + + @staticmethod def _ask_user_range(question, first, last, default): """ Asks the user for a number within a range. @@ -1150,7 +1181,7 @@ other than VPP? [0-{}][0]? '.format(str(max_other_cores)) print "\nThere currently a total of {} huge pages.". \ format(total) question = \ - "How many huge pages do you want [{} - {}][{}]? ".\ + "How many huge pages do you want [{} - {}][{}]? ". \ format(MIN_TOTAL_HUGE_PAGES, maxpages, MIN_TOTAL_HUGE_PAGES) answer = self._ask_user_range(question, 1024, maxpages, 1024) node['hugepages']['total'] = str(answer) @@ -1363,10 +1394,10 @@ other than VPP? [0-{}][0]? '.format(str(max_other_cores)) # System Memory if 'free' in node['hugepages'] and \ - 'memfree' in node['hugepages'] and \ - 'size' in node['hugepages']: - free = node['hugepages']['free'] - memfree = float(node['hugepages']['memfree'].split(' ')[0]) + 'memfree' in node['hugepages'] and \ + 'size' in node['hugepages']: + free = node['hugepages']['free'] + memfree = float(node['hugepages']['memfree'].split(' ')[0]) hugesize = float(node['hugepages']['size'].split(' ')[0]) memhugepages = MIN_TOTAL_HUGE_PAGES * hugesize @@ -1425,3 +1456,94 @@ other than VPP? [0-{}][0]? '.format(str(max_other_cores)) self.min_system_resources(node) print "\n==============================" + + def _ipv4_interface_setup_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: dict + """ + + vpputl = VPPUtil() + interfaces = vpputl.get_hardware(node) + if interfaces == {}: + return + + interfaces_with_ip = [] + for intf in sorted(interfaces.items()): + name = intf[0] + if name == 'local0': + continue + + question = "Would you like an address to interface {} [Y/n]? ".format(name) + answer = self._ask_user_yn(question, 'y') + if answer == 'y': + address = {} + addr, plen = self._ask_user_ipv4() + address['name'] = name + address['addr'] = addr + address['plen'] = plen + interfaces_with_ip.append(address) + + return interfaces_with_ip + + def ipv4_interface_setup(self): + """ + After asking the user some questions, get a list of interfaces + and IPv4 addresses associated with those interfaces + + """ + + for i in self._nodes.items(): + node = i[1] + + # Show the current interfaces with IP addresses + current_ints = VPPUtil.get_int_ip(node) + if current_ints is not {}: + print ("\nThese are the current interfaces with IP addresses:") + for items in sorted(current_ints.items()): + name = items[0] + value = items[1] + if 'address' not in value: + address = 'Not Set' + else: + address = value['address'] + print ("{:30} {:20} {:10}".format(name, address, value['state'])) + question = "\nWould you like to keep this configuration [Y/n]? " + answer = self._ask_user_yn(question, 'y') + if answer == 'y': + continue + else: + print ("\nThere are currently no interfaces with IP addresses.") + + # Create a script that add the ip addresses to the interfaces + # and brings the interfaces up + ints_with_addrs = self._ipv4_interface_setup_questions(node) + content = '' + for ints in ints_with_addrs: + name = ints['name'] + addr = ints['addr'] + plen = ints['plen'] + setipstr = 'set int ip address {} {}/{}\n'.format(name, addr, plen) + setintupstr = 'set int state {} up\n'.format(name) + content += setipstr + setintupstr + + # Write the content to the script + rootdir = node['rootdir'] + filename = rootdir + '/vpp/vpp-config/scripts/set_int_ipv4_and_up' + with open(filename, 'w+') as sfile: + sfile.write(content) + + # 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))
\ No newline at end of file diff --git a/extras/vpp_config/vpplib/VPPUtil.py b/extras/vpp_config/vpplib/VPPUtil.py index 350b7759a03..f042e80bd8f 100644 --- a/extras/vpp_config/vpplib/VPPUtil.py +++ b/extras/vpp_config/vpplib/VPPUtil.py @@ -373,6 +373,45 @@ class VPPUtil(object): self.exec_command('vppctl sh {}'.format(value)) @staticmethod + def get_int_ip(node): + """ + Get the VPP interfaces and IP addresses + + :param node: VPP node. + :type node: dict + :returns: Dictionary containing VPP interfaces and IP addresses + :rtype: dictionary + """ + interfaces = {} + cmd = 'vppctl show int addr' + (ret, stdout, stderr) = VPPUtil.exec_command(cmd) + if ret != 0: + return interfaces + + lines = stdout.split('\n') + if len(lines[0]) is not 0: + if lines[0].split(' ')[0] == 'FileNotFoundError': + return interfaces + + for line in lines: + if len(line) is 0: + continue + + # If the first character is not whitespace + # create a new interface + if len(re.findall(r'\s', line[0])) is 0: + spl = line.split() + name = spl[0] + if name == 'local0': + continue + interfaces[name] = {} + interfaces[name]['state'] = spl[1].lstrip('(').rstrip('):\r') + else: + interfaces[name]['address'] = line.lstrip(' ').rstrip('\r') + + return interfaces + + @staticmethod def get_hardware(node): """ Get the VPP hardware information and return it in a @@ -380,7 +419,7 @@ class VPPUtil(object): :param node: VPP node. :type node: dict - :returns: Dictionary containing improtant VPP information + :returns: Dictionary containing VPP hardware information :rtype: dictionary """ |