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

Commit a2431a1

Browse files
committed
w.i.p.: adding fine-grained counting from tshark packet capture
1 parent d737d34 commit a2431a1

File tree

1 file changed

+64
-17
lines changed

1 file changed

+64
-17
lines changed

pyretic/tests/test_bucket.py

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,26 +90,26 @@ def capture_packets(t_out, t_err, ints_list, capture_dir):
9090
def workload(net, hosts):
9191
net.pingAll()
9292

93-
def get_tshark_counts(t_outfile, tshark_filter_funs, ctlr):
93+
def get_tshark_counts(t_outfile, params, ctlr):
94+
filter_funs = params['filter_funs'].split(',')
95+
test_nums = params['test_nums'].split(',')
96+
assert len(filter_funs) == len(test_nums)
97+
tshark_counts = {}
98+
9499
if ctlr == 'bucket':
95-
return bucket_get_tshark_counts(t_outfile, tshark_filter_funs)
100+
counting_fun = bucket_tshark_filter_count
96101
elif ctlr == 'path_query':
97-
return path_query_get_tshark_counts(t_outfile, tshark_filter_funs)
102+
counting_fun = path_query_tshark_filter_count
98103
else:
99104
raise RuntimeError('unknown controller!')
100105

101-
def bucket_get_tshark_counts(t_outfile, tshark_filter_funs):
102-
tshark_counts = {}
103-
for f in tshark_filter_funs.split(','):
104-
count_ref = len(tshark_counts.keys())
105-
tshark_counts.update([(count_ref,
106-
tshark_filter_count(t_outfile, f))])
106+
for i in range(0, len(filter_funs)):
107+
bucket_ref = test_nums[i]
108+
f = filter_funs[i]
109+
tshark_counts.update([(bucket_ref, counting_fun(t_outfile, f))])
107110
return tshark_counts
108111

109-
def path_query_get_tshark_counts(t_outfile, tshark_filter_funs):
110-
pass
111-
112-
def tshark_filter_count(t_outfile, filter_fun):
112+
def bucket_tshark_filter_count(t_outfile, filter_fun):
113113
global ints_map
114114
t_out = open(t_outfile, 'r')
115115
pkt_count = 0
@@ -125,6 +125,37 @@ def tshark_filter_count(t_outfile, filter_fun):
125125
byte_count += bytes_fun(line)
126126
return (pkt_count, byte_count)
127127

128+
def get_key_str(line):
129+
""" This function matches closely with the get_key_str() function in
130+
path_query.py, since they both generate keys to aggregate the same set of
131+
packets in different ways -- the latter to group packet counts retrieved
132+
from query-matched packets, and this one to group packets filtered from
133+
tshark."""
134+
global rev_ints_map
135+
ethtype = 'ip' if __get_ip_srcip(line) != '' else 'arp'
136+
srcip_fun = __get_ip_srcip if ethtype == 'ip' else __get_arp_srcip
137+
dstip_fun = __get_ip_dstip if ethtype == 'ip' else __get_arp_dstip
138+
pred = "int:%s,ethtype:%s,srcip:%s,dstip:%s" % (
139+
rev_ints_map[__get_interface_id(line)],
140+
ethtype, srcip_fun(line), dstip_fun(line))
141+
return pred
142+
143+
def path_query_tshark_filter_count(t_outfile, filter_fun):
144+
global ints_map
145+
t_out = open(t_outfile, 'r')
146+
predwise_count = {}
147+
filter_fun = globals()[filter_fun]
148+
if 'any' not in ints_map:
149+
bytes_fun = get_bytes
150+
else:
151+
bytes_fun = get_bytes_cooked_capture
152+
for line in t_out:
153+
if filter_fun(line.strip()):
154+
pred = get_key_str(line)
155+
(pkt_count, byte_count) = predwise_count.get(pred, (0, 0))
156+
predwise_count[pred] = (pkt_count + 1, byte_count + bytes_fun(line))
157+
return predwise_count
158+
128159
def ctlr_counts(c_outfile, c_name):
129160
c_out = open(c_outfile, 'r')
130161
if c_name == 'bucket':
@@ -159,8 +190,9 @@ def path_query_ctlr_counts(c_out):
159190
bucket_p = re.compile("Bucket [0-9a-zA-Z._]+ [0-9a-zA-Z,:._\-]+ counts: \[[0-9]+, [0-9]+\]$")
160191
for line in c_out:
161192
if bucket_p.match(line.strip()):
162-
(bucket_id, pkt_count, byte_count, pred) = (
193+
(bucket_id, pkt_count, byte_count, inter_str) = (
163194
__parse_ctlr_count_line__(line))
195+
pred = inter_str.split()[0]
164196
try:
165197
buckets_preds_counts[bucket_id][pred] = (pkt_count, byte_count)
166198
except KeyError:
@@ -220,7 +252,9 @@ def test_bucket_single_test():
220252

221253
""" Verify results """
222254
print "Verifying correctness..."
223-
tshark_counts = get_tshark_counts(t_outfile, args.tshark_filter_funs, c_name)
255+
tshark_filter_params = {'filter_funs': args.tshark_filter_funs,
256+
'test_nums': args.test_nums }
257+
tshark_counts = get_tshark_counts(t_outfile, tshark_filter_params, c_name)
224258
buckets_counts = ctlr_counts(c_outfile, c_name)
225259
success_file = adjust_path(args.success_file)
226260
write_passfail_info(success_file, tshark_counts, buckets_counts, c_name)
@@ -241,6 +275,9 @@ def parse_args():
241275
parser.add_argument("--tshark_filter_funs", default="filt_test0",
242276
help="Filter functions to parse tshark output " +
243277
"(multiple values can be comma separated")
278+
parser.add_argument("--test_nums", default="0",
279+
help="Test numbers to distinguish controller outputs" +
280+
" (multiple values can be comma separated)")
244281
parser.add_argument("--topo_args", default="3",
245282
help="Arguments to the topology class constructor " +
246283
"(separated by commas)")
@@ -298,8 +335,18 @@ def bucket_write_passfail_info(success_file, tshark_counts, buckets_counts):
298335
passfail.write(output_str)
299336
passfail.close()
300337

301-
def path_query_write_passfail_info():
302-
pass
338+
def path_query_write_passfail_info(success_file, tshark_counts, buckets_counts):
339+
print "In path query passfail info. I got the following counts:"
340+
print "TShark:"
341+
print "Bucket references:", tshark_counts.keys()
342+
for vals in tshark_counts.values():
343+
for (k, v) in vals.iteritems():
344+
print k, v
345+
print "Buckets:"
346+
print "Bucket references:", buckets_counts.keys()
347+
for vals in buckets_counts.values():
348+
for (k, v) in vals.iteritems():
349+
print k, v
303350

304351
### Helpers to extract specific headers from tshark output ###
305352
ints_map = {}

0 commit comments

Comments
 (0)