3 # Copyright (c) 2016 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 """VPP Configuration Main Entry"""
23 from vpplib.AutoConfig import AutoConfig
24 from vpplib.VPPUtil import VPPUtil
26 VPP_DRYRUNDIR = '/vpp/vpp-config/dryrun'
27 VPP_AUTO_CONFIGURATION_FILE = '/vpp/vpp-config/configs/auto-config.yaml'
28 VPP_HUGE_PAGE_FILE = '/vpp/vpp-config/dryrun/sysctl.d/80-vpp.conf'
29 VPP_STARTUP_FILE = '/vpp/vpp-config/dryrun/vpp/startup.conf'
30 VPP_GRUB_FILE = '/vpp/vpp-config/dryrun/default/grub'
31 VPP_REAL_HUGE_PAGE_FILE = '/etc/sysctl.d/80-vpp.conf'
32 VPP_REAL_STARTUP_FILE = '/etc/vpp/startup.conf'
33 VPP_REAL_GRUB_FILE = '/etc/default/grub'
38 def autoconfig_yn(question, default):
40 Ask the user a yes or no question.
42 :param question: The text of the question
43 :param default: Value to be returned if '\n' is entered
44 :type question: string
50 default = default.lower()
52 while not input_valid:
53 answer = raw_input(question)
56 if re.findall(r'[YyNn]', answer):
58 answer = answer[0].lower()
60 print "Please answer Y, N or Return."
65 def autoconfig_cp(node, src, dst):
67 Copies a file, saving the original if needed.
69 :param node: Node dictionary with cpuinfo.
70 :param src: Source File
71 :param dst: Destination file
75 :raises RuntimeError: If command fails
78 # If the destination file exist, create a copy if one does not already
81 (ret, stdout, stderr) = VPPUtil.exec_command('ls {}'.format(dst))
83 cmd = 'cp {} {}'.format(dst, ofile)
84 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
86 raise RuntimeError('{} failed on node {} {} {}'.
92 # Copy the source file
93 cmd = 'cp {} {}'.format(src, os.path.dirname(dst))
94 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
96 raise RuntimeError('{} failed on node {} {}'.
97 format(cmd, node['host'], stderr))
100 def autoconfig_diff(node, src, dst):
102 Returns the diffs of 2 files.
104 :param node: Node dictionary with cpuinfo.
105 :param src: Source File
106 :param dst: Destination file
112 :raises RuntimeError: If command fails
115 # Diff the files and return the output
116 cmd = "diff {} {}".format(src, dst)
117 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
119 raise RuntimeError('{} failed on node {} {} {}'.
128 def autoconfig_show_system():
130 Shows the system information.
134 acfg = AutoConfig(rootdir, VPP_AUTO_CONFIGURATION_FILE)
141 def autoconfig_hugepage_apply(node):
143 Apply the huge page configuration.
144 :param node: The node structure
146 :returns: -1 if the caller should return, 0 if not
151 diffs = autoconfig_diff(node, VPP_REAL_HUGE_PAGE_FILE, rootdir + VPP_HUGE_PAGE_FILE)
153 print "These are the changes we will apply to"
154 print "the huge page file ({}).\n".format(VPP_REAL_HUGE_PAGE_FILE)
156 answer = autoconfig_yn(
157 "\nAre you sure you want to apply these changes [Y/n]? ",
163 autoconfig_cp(node, rootdir + VPP_HUGE_PAGE_FILE, VPP_REAL_HUGE_PAGE_FILE)
164 cmd = "sysctl -p {}".format(VPP_REAL_HUGE_PAGE_FILE)
165 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
167 raise RuntimeError('{} failed on node {} {} {}'.
168 format(cmd, node['host'], stdout, stderr))
170 print '\nThere are no changes to the huge page configuration.'
175 def autoconfig_vpp_apply(node):
177 Apply the vpp configuration.
179 :param node: The node structure
181 :returns: -1 if the caller should return, 0 if not
186 cmd = "service vpp stop"
187 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
189 raise RuntimeError('{} failed on node {} {} {}'.
190 format(cmd, node['host'], stdout, stderr))
192 diffs = autoconfig_diff(node, VPP_REAL_STARTUP_FILE, rootdir + VPP_STARTUP_FILE)
194 print "These are the changes we will apply to"
195 print "the VPP startup file ({}).\n".format(VPP_REAL_STARTUP_FILE)
197 answer = autoconfig_yn(
198 "\nAre you sure you want to apply these changes [Y/n]? ",
203 # Copy the VPP startup
204 autoconfig_cp(node, rootdir + VPP_STARTUP_FILE, VPP_REAL_STARTUP_FILE)
206 print '\nThere are no changes to VPP startup.'
211 def autoconfig_grub_apply(node):
213 Apply the grub configuration.
215 :param node: The node structure
217 :returns: -1 if the caller should return, 0 if not
221 print "\nThe configured grub cmdline looks like this:"
222 configured_cmdline = node['grub']['default_cmdline']
223 current_cmdline = node['grub']['current_cmdline']
224 print configured_cmdline
225 print "\nThe current boot cmdline looks like this:"
226 print current_cmdline
227 question = "\nDo you want to keep the current boot cmdline [Y/n]? "
228 answer = autoconfig_yn(question, 'y')
230 node['grub']['keep_cmdline'] = False
233 diffs = autoconfig_diff(node, VPP_REAL_GRUB_FILE, rootdir + VPP_GRUB_FILE)
235 print "These are the changes we will apply to"
236 print "the GRUB file ({}).\n".format(VPP_REAL_GRUB_FILE)
238 answer = autoconfig_yn(
239 "\nAre you sure you want to apply these changes [y/N]? ",
244 # Copy and update grub
245 autoconfig_cp(node, rootdir + VPP_GRUB_FILE, VPP_REAL_GRUB_FILE)
246 distro = VPPUtil.get_linux_distro()
247 if distro[0] == 'Ubuntu':
250 cmd = "grub2-mkconfig -o /boot/grub2/grub.cfg"
251 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
253 raise RuntimeError('{} failed on node {} {} {}'.
258 print "There have been changes to the GRUB config a",
259 print "reboot will be required."
262 print '\nThere are no changes to the GRUB config.'
267 def autoconfig_apply():
269 Apply the configuration.
271 Show the diff of the dryrun file and the actual configuration file
272 Copy the files from the dryrun directory to the actual file.
273 Peform the system function
278 pkgs = vutil.get_installed_vpp_pkgs()
280 print "\nVPP is not installed, Install VPP with option 4."
283 acfg = AutoConfig(rootdir, VPP_AUTO_CONFIGURATION_FILE)
285 print "\nWe are now going to configure your system(s).\n"
286 answer = autoconfig_yn("Are you sure you want to do this [Y/n]? ", 'y')
290 nodes = acfg.get_nodes()
291 for i in nodes.items():
294 # Check the system resources
295 if not acfg.min_system_resources(node):
299 ret = autoconfig_hugepage_apply(node)
304 ret = autoconfig_vpp_apply(node)
309 ret = autoconfig_grub_apply(node)
313 # Everything is configured start vpp
314 cmd = "service vpp start"
315 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
317 raise RuntimeError('{} failed on node {} {} {}'.
318 format(cmd, node['host'], stdout, stderr))
321 def autoconfig_dryrun():
323 Execute the dryrun function.
328 pkgs = vutil.get_installed_vpp_pkgs()
330 print "\nVPP is not installed, install VPP with option 4."
333 acfg = AutoConfig(rootdir, VPP_AUTO_CONFIGURATION_FILE)
335 # Stop VPP on each node
336 nodes = acfg.get_nodes()
337 for i in nodes.items():
344 # Check the system resources
345 nodes = acfg.get_nodes()
346 for i in nodes.items():
348 if not acfg.min_system_resources(node):
352 acfg.modify_devices()
357 # Calculate the cpu parameters
358 acfg.calculate_cpu_parameters()
360 # Acquire TCP stack parameters
361 acfg.acquire_tcp_params()
364 acfg.apply_vpp_startup()
366 # Apply the grub configuration
367 acfg.apply_grub_cmdline()
370 acfg.modify_huge_pages()
371 acfg.apply_huge_pages()
374 def autoconfig_install():
376 Install or Uninstall VPP.
380 # Since these commands will take a while, we
381 # want to see the progress
382 logger = logging.getLogger()
384 acfg = AutoConfig(rootdir, VPP_AUTO_CONFIGURATION_FILE)
387 nodes = acfg.get_nodes()
388 for i in nodes.items():
391 pkgs = vutil.get_installed_vpp_pkgs()
394 print "\nThese packages are installed on node {}" \
395 .format(node['host'])
396 print "{:25} {}".format("Name", "Version")
399 print "{:25} {}".format(
400 pkg['name'], pkg['version'])
402 print "{}".format(pkg['name'])
404 question = "\nDo you want to uninstall these "
405 question += "packages [y/N]? "
406 answer = autoconfig_yn(question, 'n')
408 logger.setLevel(logging.INFO)
409 vutil.uninstall_vpp(node)
411 print "\nThere are no VPP packages on node {}." \
412 .format(node['host'])
413 question = "Do you want to install VPP [Y/n]? "
414 answer = autoconfig_yn(question, 'y')
416 logger.setLevel(logging.INFO)
417 vutil.install_vpp(node)
419 # Set the logging level back
420 logger.setLevel(logging.ERROR)
423 def autoconfig_patch_qemu():
425 Patch the correct qemu version that is needed for openstack
429 # Since these commands will take a while, we
430 # want to see the progress
431 logger = logging.getLogger()
433 acfg = AutoConfig(rootdir, VPP_AUTO_CONFIGURATION_FILE)
435 nodes = acfg.get_nodes()
436 for i in nodes.items():
439 logger.setLevel(logging.INFO)
440 acfg.patch_qemu(node)
443 def autoconfig_ipv4_setup():
445 Setup IPv4 interfaces
449 acfg = AutoConfig(rootdir, VPP_AUTO_CONFIGURATION_FILE)
450 acfg.ipv4_interface_setup()
453 def autoconfig_not_implemented():
455 This feature is not implemented
459 print "\nThis Feature is not implemented yet...."
462 def autoconfig_basic_test_menu():
464 The auto configuration basic test menu
468 basic_menu_text = '\nWhat would you like to do?\n\n\
469 1) List/Create Simple IPv4 Setup\n\
470 9 or q) Back to main menu.'
472 print "{}".format(basic_menu_text)
476 while not input_valid:
477 answer = raw_input("\nCommand: ")
479 print "Please enter only 1 character."
481 if re.findall(r'[Qq1-29]', answer):
483 answer = answer[0].lower()
485 print "Please enter a character between 1 and 2 or 9."
493 def autoconfig_basic_test():
495 The auto configuration basic test menu
499 pkgs = vutil.get_installed_vpp_pkgs()
501 print "\nVPP is not installed, install VPP with option 4."
506 answer = autoconfig_basic_test_menu()
508 autoconfig_ipv4_setup()
509 elif answer == '9' or answer == 'q':
512 autoconfig_not_implemented()
515 def autoconfig_main_menu():
517 The auto configuration main menu
521 main_menu_text = '\nWhat would you like to do?\n\n\
522 1) Show basic system information\n\
523 2) Dry Run (Will save the configuration files in {}/vpp/vpp-config/dryrun for inspection)\n\
524 and user input in {}/vpp/vpp-config/configs/auto-config.yaml\n\
525 3) Full configuration (WARNING: This will change the system configuration)\n\
526 4) List/Install/Uninstall VPP.\n\
527 5) Execute some basic tests.\n\
528 9 or q) Quit'.format(rootdir, rootdir)
530 # 5) Dry Run from {}/vpp/vpp-config/auto-config.yaml (will not ask questions).\n\
531 # 6) Install QEMU patch (Needed when running openstack).\n\
533 print "{}".format(main_menu_text)
537 while not input_valid:
538 answer = raw_input("\nCommand: ")
540 print "Please enter only 1 character."
542 if re.findall(r'[Qq1-79]', answer):
544 answer = answer[0].lower()
546 print "Please enter a character between 1 and 5 or 9."
553 def autoconfig_main():
555 The auto configuration main entry point
561 answer = autoconfig_main_menu()
563 autoconfig_show_system()
571 autoconfig_basic_test()
572 elif answer == '9' or answer == 'q':
575 autoconfig_not_implemented()
578 def autoconfig_setup():
580 The auto configuration setup function.
582 We will copy the configuration files to the dryrun directory.
588 logging.basicConfig(level=logging.ERROR)
590 distro = VPPUtil.get_linux_distro()
591 if distro[0] == 'Ubuntu':
592 rootdir = '/usr/local'
596 # If there is a system configuration file use that, if not use the initial auto-config file
597 filename = rootdir + VPP_AUTO_CONFIGURATION_FILE
598 if os.path.isfile(filename) is True:
599 acfg = AutoConfig(rootdir, VPP_AUTO_CONFIGURATION_FILE)
601 raise RuntimeError('The Auto configuration file does not exist {}'.
604 print "\nWelcome to the VPP system configuration utility"
606 print "\nThese are the files we will modify:"
607 print " /etc/vpp/startup.conf"
608 print " /etc/sysctl.d/80-vpp.conf"
609 print " /etc/default/grub"
611 print "\nBefore we change them, we'll create working copies in {}".format(rootdir + VPP_DRYRUNDIR)
612 print "Please inspect them carefully before applying the actual configuration (option 3)!"
614 nodes = acfg.get_nodes()
615 for i in nodes.items():
618 if (os.path.isfile(rootdir + VPP_STARTUP_FILE) is not True) and \
619 (os.path.isfile(VPP_REAL_STARTUP_FILE) is True):
620 autoconfig_cp(node, VPP_REAL_STARTUP_FILE, '{}'.format(rootdir + VPP_STARTUP_FILE))
621 if (os.path.isfile(rootdir + VPP_HUGE_PAGE_FILE) is not True) and \
622 (os.path.isfile(VPP_REAL_HUGE_PAGE_FILE) is True):
623 autoconfig_cp(node, VPP_REAL_HUGE_PAGE_FILE, '{}'.format(rootdir + VPP_HUGE_PAGE_FILE))
624 if (os.path.isfile(rootdir + VPP_GRUB_FILE) is not True) and \
625 (os.path.isfile(VPP_REAL_GRUB_FILE) is True):
626 autoconfig_cp(node, VPP_REAL_GRUB_FILE, '{}'.format(rootdir + VPP_GRUB_FILE))
629 if __name__ == '__main__':
632 if not os.geteuid() == 0:
633 sys.exit('\nPlease run the VPP Configuration Utility as root.')