Skip to content
This repository was archived by the owner on Jun 27, 2018. It is now read-only.

Commit a90ea18

Browse files
committed
print fine-grained grouped counts from path query callbacks
1 parent cfae93a commit a90ea18

File tree

1 file changed

+56
-10
lines changed

1 file changed

+56
-10
lines changed

pyretic/examples/path_query.py

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
from pyretic.modules.mac_learner import mac_learner
4040
from pyretic.lib.path import *
4141
from pyretic.lib.query import counts
42+
from pyretic.core import util
43+
import copy
4244
import threading
4345

4446
import time
@@ -93,22 +95,66 @@ def query_func(bucket, interval):
9395

9496
def query_callback(test_num):
9597
global only_count_results
98+
9699
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+
97148
print '**************'
98149
print datetime.now()
99150
print 'Test', test_num, ' -- got a callback from installed path query!'
100151
if only_count_results:
101152
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()
112158
else:
113159
print "Bucket %s (packet, byte) counts: %s" % (
114160
str(test_num), pkt)

0 commit comments

Comments
 (0)