Masayoshi Kobayashi | d41701f | 2013-04-08 04:31:18 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | import os |
| 3 | import re |
| 4 | import json |
| 5 | import sys |
| 6 | import os |
| 7 | |
| 8 | status=0 |
| 9 | |
| 10 | pid=os.getpid() |
| 11 | basename=os.getenv("ONOS_CLUSTER_BASENAME") |
| 12 | RestPort=8080 |
| 13 | |
| 14 | def dump_switch_table(filename): |
| 15 | cmd="dsh \"cd ONOS/scripts; ./showflow.sh\"" |
| 16 | f=open(filename, 'w') |
| 17 | result=os.popen(cmd).read() |
| 18 | f.write(result) |
| 19 | f.close() |
| 20 | |
| 21 | def dump_network_map(filename): |
| 22 | url="http://%s1:%d/wm/flow/getall/json" % (basename, RestPort) |
| 23 | cmd="curl -s %s | python -m json.tool" % url |
| 24 | f=open(filename, 'w') |
| 25 | result=os.popen(cmd).read() |
| 26 | f.write(result) |
| 27 | f.close() |
| 28 | |
| 29 | def make_key(*kargs): |
| 30 | key="" |
| 31 | for k in kargs: |
| 32 | key += str(k)+"_" |
| 33 | return key[:-1] |
| 34 | |
| 35 | def fdb_nmap(filename): |
| 36 | json_flow = json.load(open(filename, 'r')) |
| 37 | nr_flow_entries = 0 |
| 38 | fdb_nmap={} |
| 39 | for flow in json_flow: |
| 40 | fid = flow['flowId']['value'] |
| 41 | dl_src = flow['flowEntryMatch']['srcMac']['value'].lower() |
| 42 | dl_dst = flow['flowEntryMatch']['dstMac']['value'].lower() |
| 43 | e = {} |
| 44 | for entry in flow['dataPath']['flowEntries']: |
| 45 | dpid = entry['dpid']['value'].replace(":","").lower() |
| 46 | cookie = entry['flowEntryId'] |
| 47 | in_port = entry['flowEntryMatch']['inPort']['value'] |
| 48 | |
| 49 | outport = [] |
| 50 | for p in entry['flowEntryActions']: |
| 51 | outport.append(p['actionOutput']['port']['value']) |
| 52 | outport.sort() |
| 53 | |
| 54 | e['dpid']=dpid |
| 55 | e['cookie']=cookie |
| 56 | e['in_port']=in_port |
| 57 | e['dl_src']=dl_src |
| 58 | e['dl_dst']=dl_dst |
| 59 | e['actions']=outport |
| 60 | e['fid']=fid |
| 61 | key = make_key(dpid, in_port, dl_src, dl_dst, outport[0]) |
| 62 | |
| 63 | fdb_nmap[key]=e |
| 64 | nr_flow_entries += 1 |
| 65 | |
| 66 | print "nmap contained %d flow entries" % nr_flow_entries |
| 67 | return fdb_nmap |
| 68 | |
| 69 | def fdb_raw(filename): |
| 70 | f = open(filename, 'r') |
| 71 | fdb_raw={} |
| 72 | nr_flow_entries = 0 |
| 73 | for line in f: |
| 74 | e = {} |
| 75 | if line[0] == '#': |
| 76 | continue |
| 77 | dpid=re.search("dpid=([0-9]|[a-f])*", line.strip()).group().split("=")[1] |
| 78 | cookie=re.search("cookie=0x([0-9]|[a-f])*", line.strip()).group().split("=")[1] |
| 79 | in_port=re.search("in_port=[0-9]*", line.strip()).group().split("=")[1] |
| 80 | dl_src=re.search("dl_src=([0-9]|[a-f]|:)*", line.strip()).group().split("=")[1] |
| 81 | dl_dst=re.search("dl_dst=([0-9]|[a-f]|:)*", line.strip()).group().split("=")[1] |
| 82 | outport_list=re.search("actions=(output:[0-9]*,*)*", line.strip()).group().split("=")[1].split(",") |
| 83 | outport=[] |
| 84 | for i in outport_list: |
| 85 | outport.append(int(i.split(":")[1])) |
| 86 | outport.sort() |
| 87 | |
| 88 | e['dpid']=dpid |
| 89 | e['cookie']=cookie |
| 90 | e['in_port']=in_port |
| 91 | e['dl_src']=dl_src |
| 92 | e['dl_dst']=dl_dst |
| 93 | e['actions']=outport |
| 94 | key = make_key(dpid, in_port, dl_src, dl_dst, outport[0]) |
| 95 | fdb_raw[key]=e |
| 96 | nr_flow_entries += 1 |
| 97 | |
| 98 | print "real switches contained %d flow entries" % nr_flow_entries |
| 99 | f.close() |
| 100 | return fdb_raw |
| 101 | |
| 102 | if __name__ == "__main__": |
| 103 | argvs = sys.argv |
| 104 | if len(argvs) != 2: |
| 105 | f1=".nmap.%d.txt" % pid |
| 106 | f2=".rawflow.%d.txt" % pid |
| 107 | dump_network_map(f1) |
| 108 | dump_switch_table(f2) |
| 109 | |
| 110 | else: |
| 111 | f1 = sys.argv[1] |
| 112 | f2 = sys.argv[2] |
| 113 | |
| 114 | print "dumpfiles: %s %s" % (f1, f2) |
| 115 | |
| 116 | fdb_nmap = fdb_nmap(f1) |
| 117 | fdb_raw = fdb_raw(f2) |
| 118 | |
| 119 | nr_not_found_in_switch = 0 |
| 120 | for f in fdb_nmap: |
| 121 | if not fdb_raw.has_key(f): |
| 122 | nr_not_found_in_switch += 1 |
| 123 | print "fid=%s dpid=%s cookie=%s in_port=%s dl_src=%s dl_dst=%s outport=%s not found in switch" % (fdb_nmap[f]['fid'],fdb_nmap[f]['dpid'],fdb_nmap[f]['cookie'],fdb_nmap[f]['in_port'],fdb_nmap[f]['dl_src'],fdb_nmap[f]['dl_dst'],fdb_nmap[f]['actions']) |
| 124 | |
| 125 | nr_not_found_in_nmap = 0 |
| 126 | for f in fdb_raw: |
| 127 | if not fdb_nmap.has_key(f): |
| 128 | nr_not_found_in_nmap += 1 |
| 129 | print "dpid=%s cookie=%s in_port=%s dl_src=%s dl_dst=%s outport=%s not found in nmap" % (fdb_raw[f]['dpid'],fdb_raw[f]['cookie'],fdb_raw[f]['in_port'],fdb_raw[f]['dl_src'],fdb_raw[f]['dl_dst'],fdb_raw[f]['actions']) |
| 130 | |
| 131 | print "Network Map has %d flow entries, %d not found in switch" % (len(fdb_nmap), nr_not_found_in_switch) |
| 132 | print "Switches have %d flow entries, %d not found in network map" % (len(fdb_raw), nr_not_found_in_nmap) |