|
39 | 39 | from pyretic.modules.mac_learner import mac_learner
|
40 | 40 | from pyretic.lib.path import *
|
41 | 41 | from pyretic.lib.query import counts
|
| 42 | +from pyretic.core import util |
| 43 | +import copy |
42 | 44 | import threading
|
43 | 45 |
|
44 | 46 | import time
|
@@ -93,22 +95,66 @@ def query_func(bucket, interval):
|
93 | 95 |
|
94 | 96 | def query_callback(test_num):
|
95 | 97 | global only_count_results
|
| 98 | + |
96 | 99 | def actual_callback(pkt):
|
| 100 | + ac = actual_callback |
| 101 | + |
| 102 | + def touch_vars(): |
| 103 | + """ Initialize function-specific counters, if uninitialized. """ |
| 104 | + try: |
| 105 | + val = ac.pkt_count |
| 106 | + val = ac.byte_count |
| 107 | + val = ac.predwise_pkt_count |
| 108 | + val = ac.predwise_byte_count |
| 109 | + except AttributeError: |
| 110 | + ac.pkt_count = 0 |
| 111 | + ac.byte_count = 0 |
| 112 | + ac.predwise_pkt_count = {} |
| 113 | + ac.predwise_byte_count = {} |
| 114 | + |
| 115 | + def get_count_key(pkt): |
| 116 | + predwise_count_key = ['ethtype', 'srcip', 'dstip', 'switch', 'inport'] |
| 117 | + return util.frozendict({k: pkt[k] for k in predwise_count_key}) |
| 118 | + |
| 119 | + def update_predwise_counts(pkt): |
| 120 | + curr_key = get_count_key(pkt) |
| 121 | + curr_pkt_count = ac.predwise_pkt_count.get(curr_key, 0) |
| 122 | + ac.predwise_pkt_count[curr_key] = curr_pkt_count + 1 |
| 123 | + curr_byte_count = ac.predwise_byte_count.get(curr_key, 0) |
| 124 | + ac.predwise_byte_count[curr_key] = (curr_byte_count + |
| 125 | + pkt['payload_len']) |
| 126 | + |
| 127 | + def get_key_str(pred): |
| 128 | + try: |
| 129 | + out = "int:%s,ethtype:%s,srcip:%s,dstip:%s" % ( |
| 130 | + "s%d-eth%d" % (pred['switch'], pred['inport']), |
| 131 | + "ip" if pred['ethtype']==2048 else "arp", |
| 132 | + str(pred['srcip']), str(pred['dstip'])) |
| 133 | + except KeyError: |
| 134 | + raise RuntimeError("Missing keys from count predicate!") |
| 135 | + return out |
| 136 | + |
| 137 | + def print_predwise_entries(): |
| 138 | + pkt_counts = ac.predwise_pkt_count |
| 139 | + byte_counts = ac.predwise_byte_count |
| 140 | + for pred in pkt_counts.keys(): |
| 141 | + assert pred in byte_counts.keys() |
| 142 | + print "Bucket %s %s counts: [%d, %d]" % ( |
| 143 | + str(test_num), |
| 144 | + get_key_str(pred), |
| 145 | + pkt_counts[pred], |
| 146 | + byte_counts[pred]) |
| 147 | + |
97 | 148 | print '**************'
|
98 | 149 | print datetime.now()
|
99 | 150 | print 'Test', test_num, ' -- got a callback from installed path query!'
|
100 | 151 | if only_count_results:
|
101 | 152 | if isinstance(pkt, pyretic.core.packet.Packet):
|
102 |
| - try: |
103 |
| - actual_callback.pkt_count += 1 |
104 |
| - actual_callback.byte_count += pkt['payload_len'] |
105 |
| - except AttributeError: |
106 |
| - actual_callback.pkt_count = 1 |
107 |
| - actual_callback.byte_count = pkt['payload_len'] |
108 |
| - print "Bucket %s (packet, byte) counts: [%d, %d]" % ( |
109 |
| - str(test_num), |
110 |
| - actual_callback.pkt_count, |
111 |
| - actual_callback.byte_count) |
| 153 | + touch_vars() |
| 154 | + ac.pkt_count += 1 |
| 155 | + ac.byte_count += pkt['payload_len'] |
| 156 | + update_predwise_counts(pkt) |
| 157 | + print_predwise_entries() |
112 | 158 | else:
|
113 | 159 | print "Bucket %s (packet, byte) counts: %s" % (
|
114 | 160 | str(test_num), pkt)
|
|
0 commit comments