1#!/usr/bin/python3
2
3"""
4tdc_batch.py - a script to generate TC batch file
5
6Copyright (C) 2017 Chris Mi <chrism@mellanox.com>
7"""
8
9import argparse
10
11parser = argparse.ArgumentParser(description='TC batch file generator')
12parser.add_argument("device", help="device name")
13parser.add_argument("file", help="batch file name")
14parser.add_argument("-n", "--number", type=int,
15                    help="how many lines in batch file")
16parser.add_argument(
17    "-a",
18    "--handle_start",
19    type=int,
20    default=1,
21    help="start handle range from (default: 1)")
22parser.add_argument("-o", "--skip_sw",
23                    help="skip_sw (offload), by default skip_hw",
24                    action="store_true")
25parser.add_argument("-s", "--share_action",
26                    help="all filters share the same action",
27                    action="store_true")
28parser.add_argument("-p", "--prio",
29                    help="all filters have different prio",
30                    action="store_true")
31parser.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)")
38parser.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)")
46args = parser.parse_args()
47
48device = args.device
49file = open(args.file, 'w')
50
51number = 1
52if args.number:
53    number = args.number
54
55handle_start = args.handle_start
56
57skip = "skip_hw"
58if args.skip_sw:
59    skip = "skip_sw"
60
61share_action = ""
62if args.share_action:
63    share_action = "index 1"
64
65prio = "prio 1"
66if args.prio:
67    prio = ""
68    if number > 0x4000:
69        number = 0x4000
70
71mac_prefix = args.mac_prefix
72
73def 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
80def 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
87def 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
93formatter = format_add_filter
94if args.operation == "del":
95    formatter = format_del_filter
96elif args.operation == "replace":
97    formatter = format_rep_filter
98
99index = 0
100for 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)
113