| 1 | |
| 2 |
|
| 3 | |
| 4 | tdc_batch.py - a script to generate TC batch file
|
| 5 |
|
| 6 | Copyright (C) 2017 Chris Mi <[email protected]>
|
| 7 |
|
| 8 |
|
| 9 | import argparse
|
| 10 |
|
| 11 | parser = argparse.ArgumentParser(description='TC batch file generator')
|
| 12 | parser.add_argument("device", help="device name")
|
| 13 | parser.add_argument("file", help="batch file name")
|
| 14 | parser.add_argument("-n", "--number", type=int,
|
| 15 | help="how many lines in batch file")
|
| 16 | parser.add_argument(
|
| 17 | "-a",
|
| 18 | "--handle_start",
|
| 19 | type=int,
|
| 20 | default=1,
|
| 21 | help="start handle range from (default: 1)")
|
| 22 | parser.add_argument("-o", "--skip_sw",
|
| 23 | help="skip_sw (offload), by default skip_hw",
|
| 24 | action="store_true")
|
| 25 | parser.add_argument("-s", "--share_action",
|
| 26 | help="all filters share the same action",
|
| 27 | action="store_true")
|
| 28 | parser.add_argument("-p", "--prio",
|
| 29 | help="all filters have different prio",
|
| 30 | action="store_true")
|
| 31 | parser.add_argument(
|
| 32 | "-e",
|
| 33 | "--operation",
|
| 34 | choices=['add', 'del', 'replace'],
|
| 35 | default='add',
|
| 36 | help="operation to perform on filters"
|
| 37 | "(default: add filter)")
|
| 38 | parser.add_argument(
|
| 39 | "-m",
|
| 40 | "--mac_prefix",
|
| 41 | type=int,
|
| 42 | default=0,
|
| 43 | choices=range(0, 256),
|
| 44 | help="third byte of source MAC address of flower filter"
|
| 45 | "(default: 0)")
|
| 46 | args = parser.parse_args()
|
| 47 |
|
| 48 | device = args.device
|
| 49 | file = open(args.file, 'w')
|
| 50 |
|
| 51 | number = 1
|
| 52 | if args.number:
|
| 53 | number = args.number
|
| 54 |
|
| 55 | handle_start = args.handle_start
|
| 56 |
|
| 57 | skip = "skip_hw"
|
| 58 | if args.skip_sw:
|
| 59 | skip = "skip_sw"
|
| 60 |
|
| 61 | share_action = ""
|
| 62 | if args.share_action:
|
| 63 | share_action = "index 1"
|
| 64 |
|
| 65 | prio = "prio 1"
|
| 66 | if args.prio:
|
| 67 | prio = ""
|
| 68 | if number > 0x4000:
|
| 69 | number = 0x4000
|
| 70 |
|
| 71 | mac_prefix = args.mac_prefix
|
| 72 |
|
| 73 | def format_add_filter(device, prio, handle, skip, src_mac, dst_mac,
|
| 74 | share_action):
|
| 75 | return ("filter add dev {} {} protocol ip ingress handle {} "
|
| 76 | " flower {} src_mac {} dst_mac {} action drop {}".format(
|
| 77 | device, prio, handle, skip, src_mac, dst_mac, share_action))
|
| 78 |
|
| 79 |
|
| 80 | def format_rep_filter(device, prio, handle, skip, src_mac, dst_mac,
|
| 81 | share_action):
|
| 82 | return ("filter replace dev {} {} protocol ip ingress handle {} "
|
| 83 | " flower {} src_mac {} dst_mac {} action drop {}".format(
|
| 84 | device, prio, handle, skip, src_mac, dst_mac, share_action))
|
| 85 |
|
| 86 |
|
| 87 | def format_del_filter(device, prio, handle, skip, src_mac, dst_mac,
|
| 88 | share_action):
|
| 89 | return ("filter del dev {} {} protocol ip ingress handle {} "
|
| 90 | "flower".format(device, prio, handle))
|
| 91 |
|
| 92 |
|
| 93 | formatter = format_add_filter
|
| 94 | if args.operation == "del":
|
| 95 | formatter = format_del_filter
|
| 96 | elif args.operation == "replace":
|
| 97 | formatter = format_rep_filter
|
| 98 |
|
| 99 | index = 0
|
| 100 | for i in range(0x100):
|
| 101 | for j in range(0x100):
|
| 102 | for k in range(0x100):
|
| 103 | mac = ("{:02x}:{:02x}:{:02x}".format(i, j, k))
|
| 104 | src_mac = "e4:11:{:02x}:{}".format(mac_prefix, mac)
|
| 105 | dst_mac = "e4:12:00:" + mac
|
| 106 | cmd = formatter(device, prio, handle_start + index, skip, src_mac,
|
| 107 | dst_mac, share_action)
|
| 108 | file.write("{}\n".format(cmd))
|
| 109 | index += 1
|
| 110 | if index >= number:
|
| 111 | file.close()
|
| 112 | exit(0)
|