Skip to content

Instantly share code, notes, and snippets.

@djoreilly
Created January 19, 2021 15:16
Show Gist options
  • Save djoreilly/8523f96cf53e535459ad75642544dba4 to your computer and use it in GitHub Desktop.
Save djoreilly/8523f96cf53e535459ad75642544dba4 to your computer and use it in GitHub Desktop.
Sort and tabulate the output of ovs-ofctl dump-flows
'''
Make the output of ovs-ofctl dump-flows more readable
'''
import re
import sys
from tabulate import tabulate
PAT = re.compile("^ cookie.*table=(\d+), n_packets=(\d+).+ priority=(\d+),*(.*) actions=(.+)")
if len(sys.argv) == 2:
flows_file = sys.argv[1]
else:
print("Usage: python ovs-sort-flows.py /tmp/dump-flows.txt")
sys.exit()
rows = []
with open(flows_file) as f:
for line in f.readlines():
m = re.match(PAT, line)
if not m:
continue
priority = m.group(3)
table = m.group(1)
n_packets = m.group(2)
match = m.group(4)
actions = m.group(5)
rows.append((int(table), int(priority), n_packets, match, actions))
# sort by table asending and then priority descending
rows.sort(key=lambda k: (k[0],-k[1]))
headers = ['T', 'P', 'Packets', 'Match', 'Actions']
print(tabulate(rows, headers))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment