0001
0002
0003 """
0004 tdc_batch.py - a script to generate TC batch file
0005
0006 Copyright (C) 2017 Chris Mi <chrism@mellanox.com>
0007 """
0008
0009 import argparse
0010
0011 parser = argparse.ArgumentParser(description='TC batch file generator')
0012 parser.add_argument("device", help="device name")
0013 parser.add_argument("file", help="batch file name")
0014 parser.add_argument("-n", "--number", type=int,
0015 help="how many lines in batch file")
0016 parser.add_argument(
0017 "-a",
0018 "--handle_start",
0019 type=int,
0020 default=1,
0021 help="start handle range from (default: 1)")
0022 parser.add_argument("-o", "--skip_sw",
0023 help="skip_sw (offload), by default skip_hw",
0024 action="store_true")
0025 parser.add_argument("-s", "--share_action",
0026 help="all filters share the same action",
0027 action="store_true")
0028 parser.add_argument("-p", "--prio",
0029 help="all filters have different prio",
0030 action="store_true")
0031 parser.add_argument(
0032 "-e",
0033 "--operation",
0034 choices=['add', 'del', 'replace'],
0035 default='add',
0036 help="operation to perform on filters"
0037 "(default: add filter)")
0038 parser.add_argument(
0039 "-m",
0040 "--mac_prefix",
0041 type=int,
0042 default=0,
0043 choices=range(0, 256),
0044 help="third byte of source MAC address of flower filter"
0045 "(default: 0)")
0046 args = parser.parse_args()
0047
0048 device = args.device
0049 file = open(args.file, 'w')
0050
0051 number = 1
0052 if args.number:
0053 number = args.number
0054
0055 handle_start = args.handle_start
0056
0057 skip = "skip_hw"
0058 if args.skip_sw:
0059 skip = "skip_sw"
0060
0061 share_action = ""
0062 if args.share_action:
0063 share_action = "index 1"
0064
0065 prio = "prio 1"
0066 if args.prio:
0067 prio = ""
0068 if number > 0x4000:
0069 number = 0x4000
0070
0071 mac_prefix = args.mac_prefix
0072
0073 def format_add_filter(device, prio, handle, skip, src_mac, dst_mac,
0074 share_action):
0075 return ("filter add dev {} {} protocol ip ingress handle {} "
0076 " flower {} src_mac {} dst_mac {} action drop {}".format(
0077 device, prio, handle, skip, src_mac, dst_mac, share_action))
0078
0079
0080 def format_rep_filter(device, prio, handle, skip, src_mac, dst_mac,
0081 share_action):
0082 return ("filter replace dev {} {} protocol ip ingress handle {} "
0083 " flower {} src_mac {} dst_mac {} action drop {}".format(
0084 device, prio, handle, skip, src_mac, dst_mac, share_action))
0085
0086
0087 def format_del_filter(device, prio, handle, skip, src_mac, dst_mac,
0088 share_action):
0089 return ("filter del dev {} {} protocol ip ingress handle {} "
0090 "flower".format(device, prio, handle))
0091
0092
0093 formatter = format_add_filter
0094 if args.operation == "del":
0095 formatter = format_del_filter
0096 elif args.operation == "replace":
0097 formatter = format_rep_filter
0098
0099 index = 0
0100 for i in range(0x100):
0101 for j in range(0x100):
0102 for k in range(0x100):
0103 mac = ("{:02x}:{:02x}:{:02x}".format(i, j, k))
0104 src_mac = "e4:11:{:02x}:{}".format(mac_prefix, mac)
0105 dst_mac = "e4:12:00:" + mac
0106 cmd = formatter(device, prio, handle_start + index, skip, src_mac,
0107 dst_mac, share_action)
0108 file.write("{}\n".format(cmd))
0109 index += 1
0110 if index >= number:
0111 file.close()
0112 exit(0)