|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +############################################ |
| 4 | +# |
| 5 | +# script to test storm_control functionality |
| 6 | +# |
| 7 | +############################################ |
| 8 | + |
| 9 | +import argparse |
| 10 | +import sys |
| 11 | +import os |
| 12 | + |
| 13 | +# mock the redis for unit test purposes # |
| 14 | +try: |
| 15 | + if os.environ["UTILITIES_UNIT_TESTING"] == "2": |
| 16 | + modules_path = os.path.join(os.path.dirname(__file__), "..") |
| 17 | + test_path = os.path.join(modules_path, "tests") |
| 18 | + sys.path.insert(0, modules_path) |
| 19 | + sys.path.insert(0, test_path) |
| 20 | + import mock_tables.dbconnector |
| 21 | + |
| 22 | +except KeyError: |
| 23 | + pass |
| 24 | + |
| 25 | +from natsort import natsorted |
| 26 | +from tabulate import tabulate |
| 27 | +from swsscommon.swsscommon import SonicV2Connector, ConfigDBConnector |
| 28 | +from utilities_common.general import load_db_config |
| 29 | + |
| 30 | +STORM_TABLE_NAME = "PORT_STORM_CONTROL" |
| 31 | + |
| 32 | +class storm_control(object): |
| 33 | + def __init__(self): |
| 34 | + self.config_db = ConfigDBConnector() |
| 35 | + self.config_db.connect() |
| 36 | + self.db = SonicV2Connector(use_unix_socket_path=False) |
| 37 | + self.db.connect(self.db.CONFIG_DB) |
| 38 | + def show_storm_config(self, port): |
| 39 | + header = ['Interface Name', 'Storm Type', 'Rate (kbps)'] |
| 40 | + storm_type_list = ['broadcast','unknown-unicast','unknown-multicast'] |
| 41 | + body = [] |
| 42 | + configs = self.db.get_table(STORM_TABLE_NAME) |
| 43 | + if not configs: |
| 44 | + return |
| 45 | + storm_configs = natsorted(configs) |
| 46 | + if port is not None: |
| 47 | + for storm_type in storm_type_list: |
| 48 | + storm_key = port + '|' + storm_type |
| 49 | + data = self.db.get_entry(STORM_TABLE_NAME, storm_key) |
| 50 | + if data: |
| 51 | + kbps = data['kbps'] |
| 52 | + body.append([port, storm_type, kbps]) |
| 53 | + else: |
| 54 | + for storm_key in storm_configs: |
| 55 | + interface_name = storm_key[0] |
| 56 | + storm_type = storm_key[1] |
| 57 | + data = self.db.get_entry(STORM_TABLE_NAME, storm_key) |
| 58 | + if data: |
| 59 | + kbps = data['kbps'] |
| 60 | + body.append([interface_name, storm_type, kbps]) |
| 61 | + print(tabulate(body,header,tablefmt="grid")) |
| 62 | + |
| 63 | + def validate_interface(self, port): |
| 64 | + if not (port.startswith("Eth")): |
| 65 | + return False |
| 66 | + return True |
| 67 | + |
| 68 | + def validate_kbps(self, kbps): |
| 69 | + return True |
| 70 | + |
| 71 | + def add_storm_config(self, port, storm_type, kbps): |
| 72 | + if not validate_interface(port): |
| 73 | + print ("Invalid Interface:{}".format(port)) |
| 74 | + return False |
| 75 | + if not validate_kbps(kbps): |
| 76 | + print ("Invalid kbps value:{}".format(kbps)) |
| 77 | + return False |
| 78 | + key = port + '|' + storm_type |
| 79 | + entry = self.db.get_entry(STORM_TABLE_NAME,key) |
| 80 | + if len(entry) == 0: |
| 81 | + self.db.set_entry(STORM_TABLE_NAME, key, {'kbps':kbps}) |
| 82 | + else: |
| 83 | + kbps_value = int(entry.get('kbps',0)) |
| 84 | + if kbps_value != kbps: |
| 85 | + self.db.mod_entry(STORM_TABLE_NAME, key, {'kbps':kbps}) |
| 86 | + return True |
| 87 | + |
| 88 | + def del_storm_config(self, port, storm_type): |
| 89 | + if not validate_interface(port): |
| 90 | + print ("Invalid Interface:{}".format(port)) |
| 91 | + return False |
| 92 | + key = port_name + '|' + storm_type |
| 93 | + entry = self.db.get_entry(STORM_TABLE_NAME, key) |
| 94 | + if len(entry): |
| 95 | + self.db.set_entry(STORM_TABLE_NAME, key, None) |
| 96 | + return True |
| 97 | + |
| 98 | +def main(): |
| 99 | + parser = argparse.ArgumentParser(description='Configure and Display storm-control configuration', |
| 100 | + formatter_class=argparse.RawTextHelpFormatter) |
| 101 | + parser.add_argument('-l', '--list', action='store_true', help='show storm-control configuration', default=False) |
| 102 | + parser.add_argument('-p', '--port', type=str, help='port name (e.g. Ethernet0)', required=True, default=None) |
| 103 | + parser.add_argument('-t', '--storm-type', type=str, help='storm-type (broadcast, unknown-unicast, unknown-multicast)', required=True, default=None) |
| 104 | + parser.add_argument('-r', '--rate-kbps', type=int, help='kbps value', required=True, default=None) |
| 105 | + parser.add_argument('-d', '--delete', help='delete storm-control') |
| 106 | + parser.add_argument('-f', '--filename', help='file used by mock test', type=str, default=None) |
| 107 | + args = parser.parse_args() |
| 108 | + |
| 109 | + # Load database config files |
| 110 | + load_db_config() |
| 111 | + try: |
| 112 | + storm = storm_control() |
| 113 | + if args.list: |
| 114 | + input_port="" |
| 115 | + if args.port: |
| 116 | + input_port = args.port |
| 117 | + storm.show_storm_config(input_port) |
| 118 | + elif args.port and args.storm_type and args.rate_kbps: |
| 119 | + if args.delete: |
| 120 | + storm.del_storm_config(args.port, args.storm_type) |
| 121 | + else: |
| 122 | + storm.add_storm_config(args.port, args.storm_type, args.rate_kbps) |
| 123 | + else: |
| 124 | + parser.print_help() |
| 125 | + sys.exit(1) |
| 126 | + |
| 127 | + except Exception as e: |
| 128 | + try: |
| 129 | + if os.environ["UTILITIES_UNIT_TESTING"] == "1" or os.environ["UTILITIES_UNIT_TESTING"] == "2": |
| 130 | + print(str(e), file=sys.stdout) |
| 131 | + except KeyError: |
| 132 | + print(str(e), file=sys.stderr) |
| 133 | + |
| 134 | + sys.exit(1) |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + main() |
0 commit comments