Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ovs_dbg/ofp.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(self, sections, orig=""):
def from_string(cls, ofp_string):
"""Parse a ofproto flow string

The string is expected to have the follwoing format:
The string is expected to have the following format:
[flow data] [match] actions=[actions]

:param ofp_string: a ofproto string as dumped by ovs-ofctl tool
Expand Down
36 changes: 36 additions & 0 deletions ovs_dbg/ofptp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from ovs_dbg.ofp import OFPFlow

def string_to_dict(string, section_type):
"""Parse a section of ofproto flow string

section_type specifies the sections of an ofpf string being handed
'info' 'match' 'action'

:param ofp_string section: info, match or action string as would
be dumped by ovs-ofctl tool
* action does not contain "action="
* whitespace formatting not included

:return: a dictionary of the original string parsed using ofp.py
"""
buf_dict = {
'info' : "cookie=0x95721583",
'match' : "arp",
'actions' : "1"
}
buf_dict[section_type] = string

# ofpflow format: <infokv>, <matchkvstring> actions=<actionstring>\n
buffer = (
buf_dict['info'] + ', ' +
buf_dict['match'] + ' actions=' +
buf_dict['actions']
)

try:
ofp = OFPFlow.from_string(buffer)
dict = ofp.dict()
except:
raise ValueError("invalid OFPFlow syntax: ", string)

return dict[section_type]
Empty file added ovs_dbg/ofptparse/__init__.py
Empty file.
63 changes: 63 additions & 0 deletions ovs_dbg/ofptparse/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/bin/env python
"""
To Do:
other formats than json (ovn detrace option?)

"""
import sys
import argparse
import json

try:
from ovs_dbg.trace import OFPTTrace, parse_for_recirc
from ovs_dbg.ofptparse.process import process_ofpt
except Exception:
print("ERROR: Please install the correct support")
print(" libraries")
print(" Alternatively, check that your PYTHONPATH is pointing to")
print(" the correct location.")
sys.exit(1)


def extract_ofpt_output(FILE):
# Check for recirc. If so, process each trace individually
traces = parse_for_recirc(FILE.read())
# Convert strings to a list of OFPTTrace objects
OFPTobj_list = list ()
for trace in traces:
OFPTobj_list.append(OFPTTrace(trace))
return OFPTobj_list


def main():
parser = argparse.ArgumentParser()

parser.add_argument("-o", "--output", help="Output result to a file.")
parser.add_argument("-i", "--input", help=
"Read flows from specified filepath."
"If not provided, flows will be read from stdin")
parser.add_argument("-r", "--raw", action='store_true', help="Include raw ofprototrace as a KV pair in output")

args = parser.parse_args()

if args.input:
FILE = open(args.input)
else:
FILE = sys.stdin

OFPTobj_list = extract_ofpt_output(FILE)
ofpt_out = process_ofpt(OFPTobj_list, 'json', args.raw)

if args.output:
f = open(args.output, "a")
f.write(ofpt_out)
f.close()
else:
print(ofpt_out, file = sys.stdout)




if __name__ == "__main__":
main()

63 changes: 63 additions & 0 deletions ovs_dbg/ofptparse/process.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import sys

try:
from ovs_dbg.trace import OFPTTrace
import json
from ovs_dbg.decoders import FlowEncoder
except Exception:
print("ERROR: Please install the correct support")
print(" libraries")
print(" Alternatively, check that your PYTHONPATH is pointing to")
print(" the correct location.")
sys.exit(1)

def process_ofpt(OFPTobj_list, output_type, print_raw):
"""
Process OFPTTrace object into desired output

Args:
valid OFPTTrace obj
desired output type (str)
(currently only json)
raw_output flag (bool)
Return:
Output in output_type (currently only json)
"""

if len(OFPTobj_list) > 1:
dict_object = list ()
for trace in OFPTobj_list:
dict_object.append(to_dict(trace,print_raw))
else:
dict_object = to_dict(OFPTobj_list[0], print_raw)

# process into requested format (default json)
if output_type == 'json':
return json.dumps(dict_object, indent = 4, cls=FlowEncoder)
else:
return dict_object


def to_dict(OFPTTrace, print_raw):
trace_Dict = {}
bridge_entry_list = list ()

for table in OFPTTrace.parsed_output._bridge.bridge_entries:
table_entry_dict = {}
table_entry_dict["table"] = (int)(table.table_num)
if table.info:
table_entry_dict["info"] = table.info
table_entry_dict["match"] = table.match_string
table_entry_dict["actions"] = table.action_string
bridge_entry_list.append(table_entry_dict)

if print_raw:
trace_Dict["raw"] = OFPTTrace.raw_output

trace_Dict["Flow"] = OFPTTrace.parsed_output._ofpt_flow
trace_Dict["bridge: " + OFPTTrace.parsed_output._bridge.bridge_name] = bridge_entry_list
trace_Dict["Final flow"] = OFPTTrace.parsed_output._final_flow
trace_Dict["MegaFlow"] = OFPTTrace.parsed_output._megaflow
trace_Dict["Datapath actions"] = OFPTTrace.parsed_output._dpactions

return trace_Dict
Loading