tdc_batch.py 3.3 KB

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