3 # Copyright (c) 2016 Cisco and/or its affiliates.
4 # Copyright (c) 2018 Vinci Consulting Corp. All rights reserved.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at:
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 """VPP Configuration Main Entry"""
18 from __future__ import absolute_import, division, print_function
26 from vpplib.AutoConfig import AutoConfig
27 from vpplib.VPPUtil import VPPUtil
29 # Python2/3 compatible
31 input = raw_input # noqa
35 VPP_DRYRUNDIR = "/vpp/vpp-config/dryrun"
36 VPP_AUTO_CONFIGURATION_FILE = "/vpp/vpp-config/configs/auto-config.yaml"
37 VPP_HUGE_PAGE_FILE = "/vpp/vpp-config/dryrun/sysctl.d/80-vpp.conf"
38 VPP_STARTUP_FILE = "/vpp/vpp-config/dryrun/vpp/startup.conf"
39 VPP_GRUB_FILE = "/vpp/vpp-config/dryrun/default/grub"
40 VPP_REAL_HUGE_PAGE_FILE = "/etc/sysctl.d/80-vpp.conf"
41 VPP_REAL_STARTUP_FILE = "/etc/vpp/startup.conf"
42 VPP_REAL_GRUB_FILE = "/etc/default/grub"
47 def autoconfig_yn(question, default):
49 Ask the user a yes or no question.
51 :param question: The text of the question
52 :param default: Value to be returned if '\n' is entered
53 :type question: string
59 default = default.lower()
61 while not input_valid:
62 answer = input(question)
65 if re.findall(r"[YyNn]", answer):
67 answer = answer[0].lower()
69 print("Please answer Y, N or Return.")
74 def autoconfig_cp(node, src, dst):
76 Copies a file, saving the original if needed.
78 :param node: Node dictionary with cpuinfo.
79 :param src: Source File
80 :param dst: Destination file
84 :raises RuntimeError: If command fails
87 # If the destination file exist, create a copy if one does not already
90 (ret, stdout, stderr) = VPPUtil.exec_command("ls {}".format(dst))
92 cmd = "cp {} {}".format(dst, ofile)
93 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
96 "{} failed on node {} {} {}".format(cmd, node["host"], stdout, stderr)
99 # Copy the source file
100 cmd = "cp {} {}".format(src, os.path.dirname(dst))
101 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
103 raise RuntimeError("{} failed on node {} {}".format(cmd, node["host"], stderr))
106 def autoconfig_diff(node, src, dst):
108 Returns the diffs of 2 files.
110 :param node: Node dictionary with cpuinfo.
111 :param src: Source File
112 :param dst: Destination file
118 :raises RuntimeError: If command fails
121 # Diff the files and return the output
122 cmd = "diff {} {}".format(src, dst)
123 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
126 "{} failed on node {} {} {}".format(cmd, node["host"], ret, stderr)
132 def autoconfig_show_system():
134 Shows the system information.
138 acfg = AutoConfig(rootdir, VPP_AUTO_CONFIGURATION_FILE)
145 def autoconfig_hugepage_apply(node, ask_questions=True):
147 Apply the huge page configuration.
148 :param node: The node structure
150 :param ask_questions: When True ask the user questions
151 :type ask_questions: bool
152 :returns: -1 if the caller should return, 0 if not
157 diffs = autoconfig_diff(node, VPP_REAL_HUGE_PAGE_FILE, rootdir + VPP_HUGE_PAGE_FILE)
159 print("These are the changes we will apply to")
160 print("the huge page file ({}).\n".format(VPP_REAL_HUGE_PAGE_FILE))
163 answer = autoconfig_yn(
164 "\nAre you sure you want to apply these changes [Y/n]? ", "y"
170 autoconfig_cp(node, rootdir + VPP_HUGE_PAGE_FILE, VPP_REAL_HUGE_PAGE_FILE)
171 cmd = "sysctl -p {}".format(VPP_REAL_HUGE_PAGE_FILE)
172 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
175 "{} failed on node {} {} {}".format(cmd, node["host"], stdout, stderr)
178 print("\nThere are no changes to the huge page configuration.")
183 def autoconfig_vpp_apply(node, ask_questions=True):
185 Apply the vpp configuration.
187 :param node: The node structure
189 :param ask_questions: When True ask the user questions
190 :type ask_questions: bool
191 :returns: -1 if the caller should return, 0 if not
196 diffs = autoconfig_diff(node, VPP_REAL_STARTUP_FILE, rootdir + VPP_STARTUP_FILE)
198 print("These are the changes we will apply to")
199 print("the VPP startup file ({}).\n".format(VPP_REAL_STARTUP_FILE))
202 answer = autoconfig_yn(
203 "\nAre you sure you want to apply these changes [Y/n]? ", "y"
208 # Copy the VPP startup
209 autoconfig_cp(node, rootdir + VPP_STARTUP_FILE, VPP_REAL_STARTUP_FILE)
211 print("\nThere are no changes to VPP startup.")
216 def autoconfig_grub_apply(node, ask_questions=True):
218 Apply the grub configuration.
220 :param node: The node structure
222 :param ask_questions: When True ask the user questions
223 :type ask_questions: bool
224 :returns: -1 if the caller should return, 0 if not
229 print("\nThe configured grub cmdline looks like this:")
230 configured_cmdline = node["grub"]["default_cmdline"]
231 current_cmdline = node["grub"]["current_cmdline"]
232 print(configured_cmdline)
233 print("\nThe current boot cmdline looks like this:")
234 print(current_cmdline)
236 question = "\nDo you want to keep the current boot cmdline [Y/n]? "
237 answer = autoconfig_yn(question, "y")
241 node["grub"]["keep_cmdline"] = False
244 diffs = autoconfig_diff(node, VPP_REAL_GRUB_FILE, rootdir + VPP_GRUB_FILE)
246 print("These are the changes we will apply to")
247 print("the GRUB file ({}).\n".format(VPP_REAL_GRUB_FILE))
250 answer = autoconfig_yn(
251 "\nAre you sure you want to apply these changes [y/N]? ", "n"
256 # Copy and update grub
257 autoconfig_cp(node, rootdir + VPP_GRUB_FILE, VPP_REAL_GRUB_FILE)
258 distro = VPPUtil.get_linux_distro()
259 if distro[0] == "Ubuntu":
262 cmd = "grub2-mkconfig -o /boot/grub2/grub.cfg"
264 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
267 "{} failed on node {} {} {}".format(cmd, node["host"], stdout, stderr)
270 print("There have been changes to the GRUB config a", end=" ")
271 print("reboot will be required.")
274 print("\nThere are no changes to the GRUB config.")
279 def autoconfig_apply(ask_questions=True):
281 Apply the configuration.
283 Show the diff of the dryrun file and the actual configuration file
284 Copy the files from the dryrun directory to the actual file.
285 Peform the system function
287 :param ask_questions: When true ask the user questions
288 :type ask_questions: bool
293 pkgs = vutil.get_installed_vpp_pkgs()
295 print("\nVPP is not installed, Install VPP with option 4.")
298 acfg = AutoConfig(rootdir, VPP_AUTO_CONFIGURATION_FILE)
301 print("\nWe are now going to configure your system(s).\n")
302 answer = autoconfig_yn("Are you sure you want to do this [Y/n]? ", "y")
306 nodes = acfg.get_nodes()
307 for i in nodes.items():
310 # Check the system resources
311 if not acfg.min_system_resources(node):
318 ret = autoconfig_hugepage_apply(node, ask_questions)
323 ret = autoconfig_vpp_apply(node, ask_questions)
328 ret = autoconfig_grub_apply(node, ask_questions)
330 # We can still start VPP, even if we haven't configured grub
334 # Everything is configured start vpp
338 def autoconfig_dryrun(ask_questions=True):
340 Execute the dryrun function.
342 :param ask_questions: When true ask the user for paraameters
343 :type ask_questions: bool
347 acfg = AutoConfig(rootdir, VPP_AUTO_CONFIGURATION_FILE, clean=True)
349 # Stop VPP on each node
350 nodes = acfg.get_nodes()
351 for i in nodes.items():
358 # Check the system resources
359 nodes = acfg.get_nodes()
360 for i in nodes.items():
362 if not acfg.min_system_resources(node):
367 acfg.modify_devices()
369 acfg.update_interfaces_config()
371 # If there are no interfaces, just return
372 for i in nodes.items():
374 if not acfg.has_interfaces(node):
376 "\nThere are no VPP interfaces configured, please configure at least 1."
381 acfg.modify_cpu(ask_questions)
383 # Calculate the cpu parameters
384 acfg.calculate_cpu_parameters()
386 # Acquire TCP stack parameters
388 acfg.acquire_tcp_params()
391 acfg.apply_vpp_startup()
393 # Apply the grub configuration
394 acfg.apply_grub_cmdline()
398 acfg.modify_huge_pages()
399 acfg.apply_huge_pages()
402 def autoconfig_install():
404 Install or Uninstall VPP.
408 # Since these commands will take a while, we
409 # want to see the progress
410 logger = logging.getLogger()
412 acfg = AutoConfig(rootdir, VPP_AUTO_CONFIGURATION_FILE)
415 nodes = acfg.get_nodes()
416 for i in nodes.items():
419 pkgs = vutil.get_installed_vpp_pkgs()
422 print("\nThese packages are installed on node {}".format(node["host"]))
423 print("{:25} {}".format("Name", "Version"))
426 print("{:25} {}".format(pkg["name"], pkg["version"]))
428 print("{}".format(pkg["name"]))
430 question = "\nDo you want to uninstall these "
431 question += "packages [y/N]? "
432 answer = autoconfig_yn(question, "n")
434 logger.setLevel(logging.INFO)
435 vutil.uninstall_vpp(node)
437 print("\nThere are no VPP packages on node {}.".format(node["host"]))
438 question = "Do you want to install VPP [Y/n]? "
439 answer = autoconfig_yn(question, "y")
441 question = "Do you want to install the release version [Y/n]? "
442 answer = autoconfig_yn(question, "y")
447 logger.setLevel(logging.INFO)
448 vutil.install_vpp(node, branch)
450 # Set the logging level back
451 logger.setLevel(logging.ERROR)
454 def autoconfig_patch_qemu():
456 Patch the correct qemu version that is needed for openstack
460 # Since these commands will take a while, we
461 # want to see the progress
462 logger = logging.getLogger()
464 acfg = AutoConfig(rootdir, VPP_AUTO_CONFIGURATION_FILE)
466 nodes = acfg.get_nodes()
467 for i in nodes.items():
470 logger.setLevel(logging.INFO)
471 acfg.patch_qemu(node)
474 def autoconfig_ipv4_setup():
476 Setup IPv4 interfaces
480 acfg = AutoConfig(rootdir, VPP_AUTO_CONFIGURATION_FILE)
481 acfg.ipv4_interface_setup()
484 def autoconfig_create_iperf_vm():
486 Setup IPv4 interfaces
490 acfg = AutoConfig(rootdir, VPP_AUTO_CONFIGURATION_FILE)
491 acfg.destroy_iperf_vm("iperf-server")
492 acfg.create_and_bridge_iperf_virtual_interface()
493 acfg.create_iperf_vm("iperf-server")
496 def autoconfig_not_implemented():
498 This feature is not implemented
502 print("\nThis Feature is not implemented yet....")
505 def autoconfig_basic_test_menu():
507 The auto configuration basic test menu
511 basic_menu_text = "\nWhat would you like to do?\n\n\
512 1) List/Create Simple IPv4 Setup\n\
513 2) Create an iperf VM and Connect to VPP an interface\n\
514 9 or q) Back to main menu."
516 print("{}".format(basic_menu_text))
520 while not input_valid:
521 answer = input("\nCommand: ")
523 print("Please enter only 1 character.")
525 if re.findall(r"[Qq1-29]", answer):
527 answer = answer[0].lower()
529 print("Please enter a character between 1 and 2 or 9.")
537 def autoconfig_basic_test():
539 The auto configuration basic test menu
543 pkgs = vutil.get_installed_vpp_pkgs()
545 print("\nVPP is not installed, install VPP with option 4.")
550 answer = autoconfig_basic_test_menu()
552 autoconfig_ipv4_setup()
554 autoconfig_create_iperf_vm()
555 elif answer == "9" or answer == "q":
558 autoconfig_not_implemented()
561 def autoconfig_main_menu():
563 The auto configuration main menu
567 main_menu_text = "\nWhat would you like to do?\n\n\
568 1) Show basic system information\n\
569 2) Dry Run (Saves the configuration files in {}/vpp/vpp-config/dryrun.\n\
570 3) Full configuration (WARNING: This will change the system configuration)\n\
571 4) List/Install/Uninstall VPP.\n\
576 # 5) Dry Run from {}/vpp/vpp-config/auto-config.yaml (will not ask questions).\n\
577 # 6) Install QEMU patch (Needed when running openstack).\n\
579 print("{}".format(main_menu_text))
583 while not input_valid:
584 answer = input("\nCommand: ")
586 print("Please enter only 1 character.")
588 if re.findall(r"[Qq1-4]", answer):
590 answer = answer[0].lower()
592 print("Please enter a character between 1 and 4 or q.")
597 def autoconfig_main():
599 The auto configuration main entry point
608 answer = autoconfig_main_menu()
610 autoconfig_show_system()
620 autoconfig_not_implemented()
623 def autoconfig_setup(ask_questions=True):
625 The auto configuration setup function.
627 We will copy the configuration files to the dryrun directory.
633 distro = VPPUtil.get_linux_distro()
634 if distro[0] == "Ubuntu":
635 rootdir = "/usr/local"
639 # If there is a system configuration file use that, if not use the initial auto-config file
640 filename = rootdir + VPP_AUTO_CONFIGURATION_FILE
641 if os.path.isfile(filename) is True:
642 acfg = AutoConfig(rootdir, VPP_AUTO_CONFIGURATION_FILE)
645 "The Auto configuration file does not exist {}".format(filename)
649 print("\nWelcome to the VPP system configuration utility")
651 print("\nThese are the files we will modify:")
652 print(" /etc/vpp/startup.conf")
653 print(" /etc/sysctl.d/80-vpp.conf")
654 print(" /etc/default/grub")
657 "\nBefore we change them, we'll create working copies in "
658 "{}".format(rootdir + VPP_DRYRUNDIR)
661 "Please inspect them carefully before applying the actual "
662 "configuration (option 3)!"
665 nodes = acfg.get_nodes()
666 for i in nodes.items():
669 if (os.path.isfile(rootdir + VPP_STARTUP_FILE) is not True) and (
670 os.path.isfile(VPP_REAL_STARTUP_FILE) is True
673 node, VPP_REAL_STARTUP_FILE, "{}".format(rootdir + VPP_STARTUP_FILE)
675 if (os.path.isfile(rootdir + VPP_HUGE_PAGE_FILE) is not True) and (
676 os.path.isfile(VPP_REAL_HUGE_PAGE_FILE) is True
679 node, VPP_REAL_HUGE_PAGE_FILE, "{}".format(rootdir + VPP_HUGE_PAGE_FILE)
681 if (os.path.isfile(rootdir + VPP_GRUB_FILE) is not True) and (
682 os.path.isfile(VPP_REAL_GRUB_FILE) is True
685 node, VPP_REAL_GRUB_FILE, "{}".format(rootdir + VPP_GRUB_FILE)
688 # Be sure the uio_pci_generic driver is installed
689 cmd = "modprobe uio_pci_generic"
690 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
692 logging.warning("{} failed on node {} {}".format(cmd, node["host"], stderr))
695 # noinspection PyUnresolvedReferences
696 def execute_with_args(args):
698 Execute the configuration utility with agruments.
700 :param args: The Command line arguments
705 autoconfig_setup(ask_questions=False)
707 # Execute the command
709 autoconfig_show_system()
711 autoconfig_dryrun(ask_questions=False)
713 autoconfig_apply(ask_questions=False)
715 autoconfig_not_implemented()
720 The vpp configuration utility main entry point.
725 if not os.geteuid() == 0:
726 sys.exit("\nPlease run the VPP Configuration Utility as root.")
728 if len(sys.argv) > 1 and ((sys.argv[1] == "-d") or (sys.argv[1] == "--debug")):
729 logging.basicConfig(level=logging.DEBUG)
731 logging.basicConfig(level=logging.ERROR)
733 # If no arguments were entered, ask the user questions to
734 # get the main parameters
735 if len(sys.argv) == 1:
738 elif len(sys.argv) == 2 and ((sys.argv[1] == "-d") or (sys.argv[1] == "--debug")):
742 # There were arguments specified, so execute the utility using
743 # command line arguments
744 description = "The VPP configuration utility allows the user to "
745 "configure VPP in a simple and safe manner. The utility takes input "
746 "from the user or the specified .yaml file. The user should then "
747 "examine these files to be sure they are correct and then actually "
748 "apply the configuration. When run without arguments the utility run "
749 "in an interactive mode"
751 main_parser = argparse.ArgumentParser(
753 description=description,
754 epilog='See "%(prog)s help COMMAND" for help on a specific command.',
756 main_parser.add_argument(
757 "--apply", "-a", action="store_true", help="Apply the cofiguration."
759 main_parser.add_argument(
763 help="Create the dryrun configuration files.",
765 main_parser.add_argument(
766 "--show", "-s", action="store_true", help="Shows basic system information"
768 main_parser.add_argument(
769 "--debug", "-d", action="count", help="Print debug output (multiple levels)"
772 args = main_parser.parse_args()
774 return execute_with_args(args)
777 if __name__ == "__main__":