|
| 1 | +#!/usr/bin/python |
| 2 | +""" |
| 3 | +mmuconfig is the utility to show and change mmu configuration |
| 4 | +
|
| 5 | +usage: mmuconfig [-h] [-v] [-l] [-p PROFILE] [-a ALPHA] [-vv] |
| 6 | +
|
| 7 | +optional arguments: |
| 8 | + -h --help show this help message and exit |
| 9 | + -v --version show program's version number and exit |
| 10 | + -vv --verbose verbose output |
| 11 | + -l --list show mmu configuration |
| 12 | + -p --profile specify buffer profile name |
| 13 | + -a --alpha set n for dyanmic threshold alpha 2^(n) |
| 14 | +
|
| 15 | +""" |
| 16 | + |
| 17 | +from __future__ import print_function |
| 18 | + |
| 19 | +import os |
| 20 | +import sys |
| 21 | +import argparse |
| 22 | +import swsssdk |
| 23 | +import tabulate |
| 24 | + |
| 25 | +BUFFER_POOL_TABLE_NAME = "BUFFER_POOL" |
| 26 | +BUFFER_PROFILE_TABLE_NAME = "BUFFER_PROFILE" |
| 27 | + |
| 28 | +DYNAMIC_THRESHOLD = "dynamic_th" |
| 29 | +BUFFER_PROFILE_FIELDS = { |
| 30 | + "alpha": DYNAMIC_THRESHOLD |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +class MmuConfig(object): |
| 35 | + def __init__(self, verbose): |
| 36 | + self.verbose = verbose |
| 37 | + |
| 38 | + # Set up db connections |
| 39 | + self.db = swsssdk.ConfigDBConnector() |
| 40 | + self.db.connect() |
| 41 | + |
| 42 | + def list(self): |
| 43 | + buf_pools = self.db.get_table(BUFFER_POOL_TABLE_NAME) |
| 44 | + for pool_name, pool_data in buf_pools.items(): |
| 45 | + config = [] |
| 46 | + |
| 47 | + print("Pool: " + pool_name) |
| 48 | + for field, value in pool_data.items(): |
| 49 | + config.append([field, value]) |
| 50 | + print(tabulate.tabulate(config) + "\n") |
| 51 | + if self.verbose: |
| 52 | + print("Total pools: %d\n\n" % len(buf_pools)) |
| 53 | + |
| 54 | + buf_profs = self.db.get_table(BUFFER_PROFILE_TABLE_NAME) |
| 55 | + for prof_name, prof_data in buf_profs.items(): |
| 56 | + config = [] |
| 57 | + |
| 58 | + print("Profile: " + prof_name) |
| 59 | + for field, value in prof_data.items(): |
| 60 | + config.append([field, value]) |
| 61 | + print(tabulate.tabulate(config) + "\n") |
| 62 | + if self.verbose: |
| 63 | + print("Total profiles: %d" % len(buf_profs)) |
| 64 | + |
| 65 | + def set(self, profile, field_alias, value): |
| 66 | + if os.geteuid() != 0: |
| 67 | + sys.exit("Root privileges required for this operation") |
| 68 | + |
| 69 | + field = BUFFER_PROFILE_FIELDS[field_alias] |
| 70 | + if field == DYNAMIC_THRESHOLD: |
| 71 | + v = int(value) |
| 72 | + if v < -8 or v > 8: |
| 73 | + sys.exit("Invalid alpha value: 2^(%s)" % (value)) |
| 74 | + |
| 75 | + buf_profs = self.db.get_table(BUFFER_PROFILE_TABLE_NAME) |
| 76 | + if profile in buf_profs and DYNAMIC_THRESHOLD not in buf_profs[profile]: |
| 77 | + sys.exit("%s not using dynamic thresholding" % (profile)) |
| 78 | + else: |
| 79 | + sys.exit("Set field %s not supported" % (field)) |
| 80 | + |
| 81 | + if self.verbose: |
| 82 | + print("Setting %s %s value to %s" % (profile, field, value)) |
| 83 | + self.db.mod_entry(BUFFER_PROFILE_TABLE_NAME, profile, {field: value}) |
| 84 | + |
| 85 | + |
| 86 | +def main(): |
| 87 | + parser = argparse.ArgumentParser(description='Show and change: mmu configuration', |
| 88 | + version='1.0.0', |
| 89 | + formatter_class=argparse.RawTextHelpFormatter) |
| 90 | + |
| 91 | + parser.add_argument('-l', '--list', action='store_true', help='show mmu configuration') |
| 92 | + parser.add_argument('-p', '--profile', type=str, help='specify buffer profile name', default=None) |
| 93 | + parser.add_argument('-a', '--alpha', type=str, help='set n for dyanmic threshold alpha 2^(n)', default=None) |
| 94 | + parser.add_argument('-vv', '--verbose', action='store_true', help='verbose output', default=False) |
| 95 | + |
| 96 | + args = parser.parse_args() |
| 97 | + |
| 98 | + try: |
| 99 | + mmu_cfg = MmuConfig(args.verbose) |
| 100 | + if args.list: |
| 101 | + mmu_cfg.list() |
| 102 | + elif args.profile: |
| 103 | + if args.alpha: |
| 104 | + mmu_cfg.set(args.profile, "alpha", args.alpha) |
| 105 | + else: |
| 106 | + parser.print_help() |
| 107 | + sys.exit(1) |
| 108 | + |
| 109 | + except Exception as e: |
| 110 | + print("Exception caught:", e.message, file=sys.stderr) |
| 111 | + sys.exit(1) |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + main() |
0 commit comments