summaryrefslogtreecommitdiffstats
path: root/VNFs/DPPD-PROX/helper-scripts/testvRouter/characterize_vRouter.py
blob: f4d211f628008c0818e6d04fe934afedc8e43a3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
#!/bin/env python

##
## Copyright (c) 2010-2017 Intel Corporation
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
##     http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##

import socket
import sys
import os
from time import *
from datetime import  datetime
from optparse import OptionParser
import time
from remote_system import *
from math import log

# General parameters
accuracy = 0.1 	# in percent of line rate
max_dropped = 0.001		# in percent
all_pkt_size = [64,128,256,512,1024,1280,1518]
#all_pkt_size = [64]

# vRouter parameters, in case commands must be sent
vRouter_host = "192.168.1.96" 

# Stear parameters
step_time = 0.01		# in seconds
step_delta = 0.025		# in percent of line rate

# Use case dependent parameters
##### Use case 0: influence of number of routes and next hops #####
max_number_next_hops = 256			# Maximum number of next-hops per interface
max_number_routes = 8192			# Maximum number of routes per interface
max_number_addresses_local_network = 262144

##### Use case 1: packet loss and latency #####
low_steps_delta_for_loss = 0.01                 # Use increment of 0.01% from 0 to low_steps
medium_steps_delta_for_loss = 0.1               # Use increment of 0.1% from low_steps to medium_steps
normal_steps_delta_for_loss = 1.0               # Use increment of 1% from medium_steps till 100%
low_steps = 0.1 
medium_steps = 1.0 

# Prox parameters
tx_port4 = [19,27,55,63]
tx_port5 = [20,28,56,64]
tx_port6 = [21,29,57,65]
tx_port7 = [22,30,58,66]
tx_port2 = [23,31,59,67]
tx_port3 = [24,32,60,68]
tx_port0 = [25,33,61,69]
tx_port1 = [26,34,62,70]
tx_task = 0

all_rx_cores = [1,2,3,4,5,6,7,10]
rx_lat_cores = [1,2,3,4,5,6,7,10]
rx_task = 1

# Some variables, do not change

# Program arguments
parser = OptionParser()
parser.add_option("-d", "--duration", dest="test_duration", help="Duration of each steps", metavar="integer", default=10)
parser.add_option("-s", "--speed", dest="init_speed", help="Initial speed", metavar="integer", default=100)
parser.add_option("-u", "--use-case", dest="use_case", help="Use Case Number", metavar="integer", default=0)
parser.add_option("-r", "--run", dest="run", help="Run test", metavar="integer", default=0)
parser.add_option("-c", "--configure", dest="configure", help="Configure Test", metavar="integer", default=0)
(options, args) = parser.parse_args()

init_speed = int(options.init_speed)
test_duration = int(options.test_duration)
use_case = int(options.use_case)
configure = int(options.configure)
run = int(options.run)

nb_cores_per_interface = len(tx_port0)
max_speed = (100.0/nb_cores_per_interface)
init_speed = (init_speed * 1.0/nb_cores_per_interface)
accuracy = (accuracy * 1.0/nb_cores_per_interface)
normal_steps_delta_for_loss = (normal_steps_delta_for_loss /nb_cores_per_interface)
medium_steps_delta_for_loss = (medium_steps_delta_for_loss /nb_cores_per_interface)
low_steps_delta_for_loss = (low_steps_delta_for_loss /nb_cores_per_interface)
medium_steps = (medium_steps /nb_cores_per_interface)
low_steps = (low_steps /nb_cores_per_interface)

max_dropped = max_dropped / 100

def to_str(arr):
    ret = ""
    first = 1;
    for a in arr:
        if (first == 0):
            ret += ","

        ret += str(a)
        first = 0;
    return ret;

tx_cores = tx_port0 + tx_port1 + tx_port2 + tx_port3 + tx_port4 + tx_port5 + tx_port6 + tx_port7

def send_all_pkt_size(cores, pkt_size):
    for c in cores:
        sock.sendall("pkt_size " + str(c) + " 0 " + str(pkt_size) + "\n");

def send_all_value(cores, offset, value, len):
    for c in cores:
        sock.sendall("set value " + str(c) + " 0 " + str(offset) + " " + str(value) + " " + str(len)+ "\n");

def send_all_random(cores, offset, rand_str, len):
    for c in cores:
        sock.sendall("set random " + str(c) + " 0 " + str(offset) + " " + str(rand_str) + " " + str(len)+ "\n");
        #print("set random " + str(c) + " 0 " + str(offset) + " " + str(rand_str) + " " + str(len)+ "\n");

def send_all_speed(cores, speed_perc):
    for c in cores:
        sock.sendall("speed " + str(c) + " 0 " + str(speed_perc) + "\n");

def send_reset_random():
        sock.sendall("reset randoms all" + "\n");

def send_reset_value():
        sock.sendall("reset values all" + "\n");

def rx_stats(tx_cores, tx_task, rx_cores, rx_task):
	rx = tx = drop = tsc = tsc_hs = ierrors = 0
	for e in tx_cores:
		sock.sendall("core stats " + str(e) + " " + str(tx_task) +  "\n")
		recv = recv_once()
		rx += int(recv.split(",")[0])
		tx += int(recv.split(",")[1])
		drop += int(recv.split(",")[2])
		tsc = int(recv.split(",")[3])
		tsc_hz = int(recv.split(",")[4])
	for e in rx_cores:
		sock.sendall("core stats " + str(e) + " " + str(rx_task) +  "\n")
		recv = recv_once()
		rx += int(recv.split(",")[0])
		tx += int(recv.split(",")[1])
		drop += int(recv.split(",")[2])
		tsc = int(recv.split(",")[3])
		tsc_hz = int(recv.split(",")[4])
	# Also get the ierrors as generators might be the bottleneck...
	sock.sendall("tot ierrors tot\n")
	recv = recv_once()
	ierrors += int(recv.split(",")[0])
	rx+=ierrors
	return rx,tx,drop,tsc,tsc_hz

def lat_stats(cores,task):
	lat_min = [0 for e in range(127)]
	lat_max = [0 for e in range(127)]
	lat_avg = [0 for e in range(127)]
	for e in cores:
		sock.sendall("lat stats " + str(e) + " " + str(task) + " " +  "\n")
		recv = recv_once()
		lat_min[e] = int(recv.split(",")[0])
		lat_max[e] = int(recv.split(",")[1])
		lat_avg[e] = int(recv.split(",")[2])
	return lat_min, lat_max, lat_avg

def recv_once():
    ret_str = "";
    done = 0;
    while done == 0:
        dat = sock.recv(256);
        i = 0;
        while(i < len(dat)):
            if (dat[i] == '\n'):
                done = 1
            else:
                ret_str += dat[i];
            i = i + 1;
    return ret_str

def wait_vRouter_restarted(host):
	while (1):
		ret = os.system("ping " + host + " -c 1 > /dev/null")
		if ret == 0:
			print "still up..."
		else:
			break;
		sleep(1)
	
	while (1):
		ret = os.system("ping " + host + " -c 1 > /dev/null")
		if (ret == 0):
			print "UP"
			break;
		else:
			print "still down..."
			sleep(1)

def reload_vRouter_config(config):
	print "connecting to vRouter...and copying " + str(config)
	sut = remote_system("root", vRouter_host)
	cmd = "cp /config/prox/" + str(config) + " /config/config.boot"
	sut.run(cmd)
	print "Rebooting system at " + str(datetime.now().time())
	sut.run_forked("reboot")
	sleep(5)
	wait_vRouter_restarted(vRouter_host)
	print "Waiting for last startup scripts to start..."
	last_script = "l2tp"
	while(1):
		dmesg = str(sut.run("dmesg"))
		if last_script in dmesg:
			print "found l2tp - UP"
			break;
		sleep(1)
	print "vRouter started - waiting 5 last seconds before starting test"
	sleep(5)
	print datetime.now().time()

def set_pkt_sizes(tx_cores, p):
	send_all_pkt_size(tx_cores, p-4)
	# For all cores, need to adapt IP Length (byte 16) and UDP Length (byte 38) to pkt size
	send_all_value(tx_cores, 16, p - 18, 2)		# 14 for MAC (12) EthType (2) 
	send_all_value(tx_cores, 38, p - 38, 2)		# 34 for MAC (12) EthType (2) IP (20)

def run_measure_throughput(speed):
	done = 0
	# Intialize tests by stopping cores and resetting stats
	step=0
	steps_done = 0
	sock.sendall("start " + to_str(all_rx_cores) + "\n")
	sleep(2)
	sock.sendall("stop " + to_str(all_rx_cores) + "\n")
	sock.sendall("reset stats\n")
	print "Speed    = " + str(speed * nb_cores_per_interface) 
	sleep(1);
	
	send_all_speed(tx_cores, step);

	# Now starting the steps. First go to the common speed, then increase steps for the faster one.
	sock.sendall("start " + to_str(tx_cores) + "," + to_str(rx_lat_cores) + "\n")
	while (steps_done == 0):
		sleep(step_time)
		if (step + step_delta <= speed):
			step+=step_delta
		else:
			steps_done = 1;
		send_all_speed(tx_cores, step)
	
	# Steps are now OK.  Set speed
	send_all_speed(tx_cores, speed);
	sleep(2);

	# Getting statistics to calculate PPS at right speed....
	rx_pps_beg,tx_pps_beg,drop_pps_beg,tsc_pps_beg,tsc_hz = rx_stats(tx_cores, tx_task, all_rx_cores, rx_task);
	sleep(test_duration);

	# Collect statistics before test stops...and stop the test. Important to get stats before stopping as stops take some time...
	rx_pps_end,tx_pps_end,drop_pps_end,tsc_pps_end,tsc_hz = rx_stats(tx_cores, tx_task, all_rx_cores, rx_task);
	lat_min,lat_max,lat_avg = lat_stats(rx_lat_cores, rx_task)
	sock.sendall("stop " + "," + to_str(tx_cores) + "\n")
	sock.sendall("start " + to_str(all_rx_cores) + "\n")
	sleep(3);
	sock.sendall("stop " + to_str(all_rx_cores) + "\n")
	
	rx_end, tx_end,drop_end,tsc_end,tsc_hz = rx_stats(tx_cores, tx_task, all_rx_cores, rx_task);
	rx = rx_pps_end - rx_pps_beg
	tsc = tsc_pps_end - tsc_pps_beg
	mpps = rx / (tsc/float(tsc_hz)) / 1000000
	tx = tx_pps_end - tx_pps_beg
	tx_mpps = tx / (tsc/float(tsc_hz)) / 1000000
	
	#print "Runtime = " +  str((tsc)/float(tsc_hz));
	if (tx_end == 0):
		dropped_tot = tx_end - rx_end
		dropped_pct = 0
	else:
		dropped_tot = tx_end - rx_end
		dropped_pct = ((dropped_tot) * 1.0) / tx_end

	if (dropped_tot > 0):
		if (dropped_pct >= max_dropped):
			print "** FAILED **: lost " + str(100*dropped_pct) + "% packets RX = " + str(rx_end) + " TX = " + str(tx_end) + " DROPPED = " + str(tx_end - rx_end)
		else:
			print "OK but lost " + str(100*dropped_pct) + "% packets RX = " + str(rx_end) + " TX = " + str(tx_end) + " DROPPED = " + str(tx_end - rx_end)
	else:
		if (dropped_tot < 0):
			print "Something wrong happened - received more packets than transmitted"
		else:
			print "**   OK   **: RX = " + str(rx_end) + " TX = " + str(tx_end) + " DROPPED = " + str(tx_end - rx_end) 
	print "MPPS = " + str(mpps)
	print "===================================================="
	return dropped_pct, mpps, tx_mpps, dropped_tot,lat_min,lat_max,lat_avg

def write_results(f, pkt_size, tx_mpps, mpps, dropped_pct, dropped_tot, speed, nb_cores_per_interface, number_next_hops, number_routes, traffic, lat_min, lat_max, lat_avg):
	f.write(str(pkt_size) + "; " + str(tx_mpps) + "; " + str(mpps) + "; " + str(100 * dropped_pct) + "; " + str(dropped_tot) + "; " + str(speed * nb_cores_per_interface) + "; " + str(number_next_hops) + "; " + str(number_routes) + "; " + str(traffic) + "; ")
	for e in rx_lat_cores:
		f.write(str(lat_min[e]) + "; " + str(lat_max[e]) + "; " + str(lat_avg[e]) + "; ")
	f.write("\n");
	f.flush()

def run_loss_graph(number_next_hops, number_routes, pkt_size, traffic):
	speed = init_speed * 1.0
	done = 0;
	while done == 0:
		dropped_pct, mpps, tx_mpps, dropped_tot,lat_min,lat_max,lat_avg = run_measure_throughput(speed)
		write_results(f, pkt_size, tx_mpps, mpps, dropped_pct, dropped_tot, speed, nb_cores_per_interface, number_next_hops, number_routes, traffic, lat_min, lat_max, lat_avg);
		if (speed <= low_steps_delta_for_loss):
			done = 1
			return
		if (speed >= (medium_steps+normal_steps_delta_for_loss)):
			speed -= normal_steps_delta_for_loss
		else:
			if (speed >= (low_steps+medium_steps_delta_for_loss)):
				speed -= medium_steps_delta_for_loss
			else:
				speed -= low_steps_delta_for_loss

def run_dicho_search(number_next_hops, number_routes, pkt_size, traffic):
	previous_success_speed = 0.0
	previous_error_speed = max_speed
	speed = init_speed * 1.0
	done = 0;
	good_tx_mpps = 0
	good_mpps = 0
	good_dropped_pct = 0
	good_dropped_tot = 0
	good_speed = 0
	good_lat_min = [0 for e in range(127)]
	good_lat_max = [0 for e in range(127)]
	good_lat_avg = [0 for e in range(127)]

	while done == 0:
		dropped_pct, mpps, tx_mpps, dropped_tot,lat_min,lat_max,lat_avg = run_measure_throughput(speed)
		if ((dropped_tot >= 0) and (dropped_pct <= max_dropped)):
			good_tx_mpps = tx_mpps
			good_mpps = mpps
			good_dropped_pct = dropped_pct
			good_dropped_tot = dropped_tot
			good_speed = speed
			good_lat_min = lat_min
			good_lat_max = lat_max
			good_lat_avg = lat_avg
			write_results(f, pkt_size, tx_mpps, mpps, dropped_pct, dropped_tot, speed, nb_cores_per_interface, number_next_hops, number_routes, traffic, lat_min, lat_max, lat_avg);
			write_results(f_all, pkt_size, tx_mpps, mpps, dropped_pct, dropped_tot, speed, nb_cores_per_interface, number_next_hops, number_routes, traffic, lat_min, lat_max, lat_avg);
		else:
			write_results(f_all, pkt_size, tx_mpps, mpps, dropped_pct, dropped_tot, speed, nb_cores_per_interface, number_next_hops, number_routes, traffic, lat_min, lat_max, lat_avg);

		if ((speed == max_speed) and (dropped_pct <= max_dropped)):
			write_results(f_minimal, pkt_size, tx_mpps, mpps, dropped_pct, dropped_tot, speed, nb_cores_per_interface, number_next_hops, number_routes, traffic, lat_min, lat_max, lat_avg);
			done = 1
		if (dropped_pct <= max_dropped):
			previous_success_speed = speed
			if (speed > max_speed - accuracy):
				speed = max_speed
			else:
				if (previous_error_speed - speed < accuracy):
					write_results(f_minimal, pkt_size, good_tx_mpps, good_mpps, good_dropped_pct, good_dropped_tot, good_speed, nb_cores_per_interface, number_next_hops, number_routes, traffic, good_lat_min, good_lat_max, good_lat_avg);
					done = 1
				else:
					speed = speed + (previous_error_speed - speed)/2;
		else:
			previous_error_speed = speed
			if (speed - previous_success_speed < accuracy):
				write_results(f_minimal, pkt_size, good_tx_mpps, good_mpps, good_dropped_pct, good_dropped_tot, good_speed, nb_cores_per_interface, number_next_hops, number_routes, traffic, good_lat_min, good_lat_max, good_lat_avg);
				done = 1	
			else:
				speed  = speed - (speed - previous_success_speed) / 2;

	
def set_destination_ip(use_case, nb_destinations, traffic):
	# minimmum 8 routes i.e. 1 per interface 
	# Destination addressese: "00XXXYY1" "Z00ZZ0ZZ" "AA0AA0AA" "BBBBBB10"
	# Where X = interface id. Starting with 00 to be in class A and skipping 0.x.y.z and 127.x.y.z
	# Y, Z and A = additional routes
	# B = IP in routes. 10 to avoid x.y.z.0 and x.y.z.255
	# Gaps in A and B to void "too good" distributions e.g. using LPM and 
	# First changing Y

	mask = ""
	for i in range (2):
       		mask = str(mask)+"0"
	end_mask = ""
	if (use_case != 2):
		end_mask = "XXXXXX10"		# Last 8 bits

		if (nb_destinations == 1):
			end_mask = "0010000000000000000" + str(end_mask)
		if (nb_destinations == 2):
			end_mask = "X010000000000000000" + str(end_mask)
		if (nb_destinations == 4):
			end_mask = "XX10000000000000000" + str(end_mask)
		if (nb_destinations == 8):
			end_mask = "XX1X000000000000000" + str(end_mask)
		elif (nb_destinations == 16):
			end_mask = "XX1X00X000000000000" + str(end_mask)
		elif (nb_destinations == 32):
			end_mask = "XX1X00XX00000000000" + str(end_mask)
		elif (nb_destinations == 64):
			end_mask = "XX1X00XX0X000000000" + str(end_mask)
		elif (nb_destinations == 128):
			end_mask = "XX1X00XX0XX00000000" + str(end_mask)
		elif (nb_destinations == 256):
			end_mask = "XX1X00XX0XXX0000000" + str(end_mask)
		elif (nb_destinations == 512):
			end_mask = "XX1X00XX0XXXX000000" + str(end_mask)
		elif (nb_destinations == 1024):
			end_mask = "XX1X00XX0XXXX0X0000" + str(end_mask)
		elif (nb_destinations == 2048):
			end_mask = "XX1X00XX0XXXX0XX000" + str(end_mask)
		elif (nb_destinations == 4096):
			end_mask = "XX1X00XX0XXXX0XX0X0" + str(end_mask)
		elif (nb_destinations == 8192):
			end_mask = "XX1X00XX0XXXX0XX0XX" + str(end_mask)
	else:
		if (nb_destinations <= 64 * 1):
			end_mask = "0010000000000000000"
			n_dest = int(log(nb_destinations, 2))
			for i in range (n_dest):
				end_mask = str(end_mask) + "X"
			for i in range (6 - n_dest):
				end_mask = str(end_mask) + "0"
			end_mask = str(end_mask) + "10"
		else:
			end_mask = "XXXXXX10"		# Last 8 bits

		if (nb_destinations == 64 * 2):
			end_mask = "001X000000000000000" + str(end_mask)
		elif (nb_destinations == 64 * 4):
			end_mask = "001X00X000000000000" + str(end_mask)
		elif (nb_destinations == 64 * 8):
			end_mask = "001X00XX00000000000" + str(end_mask)
		elif (nb_destinations == 64 * 16):
			end_mask = "001X00XX0X000000000" + str(end_mask)
		elif (nb_destinations == 64 * 32):
			end_mask = "001X00XX0XX00000000" + str(end_mask)
		elif (nb_destinations == 64 * 64):
			end_mask = "001X00XX0XXX0000000" + str(end_mask)
		elif (nb_destinations == 64 * 128):
			end_mask = "001X00XX0XXXX000000" + str(end_mask)
		elif (nb_destinations == 64 * 256):
			end_mask = "001X00XX0XXXX0X0000" + str(end_mask)
		elif (nb_destinations == 64 * 512):
			end_mask = "001X00XX0XXXX0XX000" + str(end_mask)
		elif (nb_destinations == 64 * 1024):
			end_mask = "001X00XX0XXXX0XX0X0" + str(end_mask)
		elif (nb_destinations == 64 * 2048):
			end_mask = "001X00XX0XXXX0XX0XX" + str(end_mask)
		elif (nb_destinations == 64 * 4096):
			end_mask = "001XX0XX0XXXX0XX0XX" + str(end_mask)
		elif (nb_destinations == 64 * 8192):
			end_mask = "001XXXXX0XXXX0XX0XX" + str(end_mask)
		elif (nb_destinations == 64 * 16384):
			end_mask = "001XXXXXXXXXX0XX0XX" + str(end_mask)
		elif (nb_destinations == 64 * 32768):
			end_mask = "001XXXXXXXXXXXXX0XX" + str(end_mask)
		elif (nb_destinations == 64 * 65536):
			end_mask = "001XXXXXXXXXXXXXXXX" + str(end_mask)
	
	if (traffic == 0):	# One-to-one. From odd interface to even interface and vice versa, no QPI cross
		mask1 = str(mask) + "001" + str(end_mask)
		mask2 = str(mask) + "000" + str(end_mask)
		mask3 = str(mask) + "011" + str(end_mask)
		mask4 = str(mask) + "010" + str(end_mask)
		mask5 = str(mask) + "101" + str(end_mask)
		mask6 = str(mask) + "100" + str(end_mask)
		mask7 = str(mask) + "111" + str(end_mask)
		mask8 = str(mask) + "110" + str(end_mask)

	elif (traffic == 1):	# Full mesh within QPI (i.e. 1 to 4)
		mask1 = str(mask) + "0XX" + str(end_mask)
		mask2 = str(mask) + "0XX" + str(end_mask)
		mask3 = str(mask) + "0XX" + str(end_mask)
		mask4 = str(mask) + "0XX" + str(end_mask)
		mask5 = str(mask) + "1XX" + str(end_mask)
		mask6 = str(mask) + "1XX" + str(end_mask)
		mask7 = str(mask) + "1XX" + str(end_mask)
		mask8 = str(mask) + "1XX" + str(end_mask)
	
	elif (traffic == 2):	# One to one, crossing QPI (100% QPI)
		mask1 = str(mask) + "100" + str(end_mask)
		mask2 = str(mask) + "101" + str(end_mask)
		mask3 = str(mask) + "110" + str(end_mask)
		mask4 = str(mask) + "111" + str(end_mask)
		mask5 = str(mask) + "000" + str(end_mask)
		mask6 = str(mask) + "001" + str(end_mask)
		mask7 = str(mask) + "010" + str(end_mask)
		mask8 = str(mask) + "011" + str(end_mask)

	elif (traffic == 3):	# 1 to 4 crossing QPI (100% QPI)
		mask1 = str(mask) + "1XX" + str(end_mask)
		mask2 = str(mask) + "1XX" + str(end_mask)
		mask3 = str(mask) + "1XX" + str(end_mask)
		mask4 = str(mask) + "1XX" + str(end_mask)
		mask5 = str(mask) + "0XX" + str(end_mask)
		mask6 = str(mask) + "0XX" + str(end_mask)
		mask7 = str(mask) + "0XX" + str(end_mask)
		mask8 = str(mask) + "0XX" + str(end_mask)

	elif (traffic == 4):	# 1 to 4 (50% QPI)
		mask1 = str(mask) + "XX1" + str(end_mask)
		mask2 = str(mask) + "XX0" + str(end_mask)
		mask3 = str(mask) + "XX1" + str(end_mask)
		mask4 = str(mask) + "XX0" + str(end_mask)
		mask5 = str(mask) + "XX1" + str(end_mask)
		mask6 = str(mask) + "XX0" + str(end_mask)
		mask7 = str(mask) + "XX1" + str(end_mask)
		mask8 = str(mask) + "XX0" + str(end_mask)

	elif (traffic == 5):	# Full mesh (50% QPI)
		mask1 = str(mask) + "XXX" + str(end_mask)
		mask2 = str(mask) + "XXX" + str(end_mask)
		mask3 = str(mask) + "XXX" + str(end_mask)
		mask4 = str(mask) + "XXX" + str(end_mask)
		mask5 = str(mask) + "XXX" + str(end_mask)
		mask6 = str(mask) + "XXX" + str(end_mask)
		mask7 = str(mask) + "XXX" + str(end_mask)
		mask8 = str(mask) + "XXX" + str(end_mask)

	for c in tx_port0:
		send_all_random([c], 30, mask1, 4)
	for c in tx_port1:
		send_all_random([c], 30, mask2, 4)
	for c in tx_port2:
		send_all_random([c], 30, mask3, 4)
	for c in tx_port3:
		send_all_random([c], 30, mask4, 4)
	for c in tx_port4:
		send_all_random([c], 30, mask5, 4)
	for c in tx_port5:
		send_all_random([c], 30, mask6, 4)
	for c in tx_port6:
		send_all_random([c], 30, mask7, 4)
	for c in tx_port7:
		send_all_random([c], 30, mask8, 4)
	for c in tx_cores:
		send_all_random([c], 34, "0XXXXXXXXXXXXX10", 2)
		send_all_random([c], 36, "0XXXXXXXXXXXXX10", 2)
		
#========================================================================
class TestDefinition():
    "Stores test parameters"
    def __init__(self, use_case, next_hops, number_routes, pkt_size, traffic, reload):
        self.use_case = use_case
        self.next_hops = next_hops
        self.number_routes = number_routes
        self.pkt_size = pkt_size
        self.traffic = traffic
        self.reload = reload

#========================================================================
# Use case 0 increases input load and measure output load => show dropped packets at low loads, show overload behavior
# Use case 1 and use case 2 run dichotomic searches, searching for 0 packet loss (or whaever loss is configured)
# Use case 1 shows the effect of number of routes and next-hops
# Use case 2 shows the effect of the number of destination, using a fixed (low) number of routes and next-hops
#========================================================================
def run_use_case(use_case, number_next_hops, number_routes, pkt_size, traffic, reload):
	if (reload):
		if (use_case == 2):
			config = "config.1_1" + "_" + str(use_case) + ".boot"
		else:
			config = "config." + str(number_routes) + "_" + str(number_next_hops) + ".boot"
		reload_vRouter_config(config)
	send_reset_random()
	send_reset_value()
	set_destination_ip(use_case, number_routes, traffic)
	set_pkt_sizes(tx_cores, pkt_size)
	print "Running test with pkt size= " + str(pkt_size) + " Next hops = " + str(number_next_hops) + "; number of routes = " + str(number_routes) + "; Traffic = " + str(traffic) + " \n"
	if (use_case == 0):
		run_loss_graph(number_next_hops, number_routes, pkt_size, traffic)
	else:
		run_dicho_search(number_next_hops, number_routes, pkt_size, traffic)
	sleep(3)

#========================================================================
def run_all_use_cases():
	use_case_nb = 1
	# Connect to dppd 
	file_path = '/tmp/prox.sock'
	sock.connect(file_path)

	f.write("pkt_size; tx_mpps; rx_mpps; dropped_pct; dropped_tot; percent_line_rate; latency per core\n")
	f_all.write("pkt_size; tx_mpps; rx_mpps; dropped_pct; dropped_tot; percent_line_rate; latency per core\n")
	f_minimal.write("pkt_size; tx_mpps; rx_mpps; dropped_pct; dropped_tot; percent_line_rate; latency per core\n")
	f.flush();
	f_all.flush();
	f_minimal.flush();

	# Starting tests
	print "Stopping all cores and resetting all values and randoms before starting\n"
	sock.sendall("stop all")
	sock.sendall("reset stats\n")
	sleep(3);
	for line in file_tests:
		info = line.split(';')
		if (info[0][0] == '#'):
			continue
		if (info[0][0] == ''):
			break
		use_case = int(info[0])
		next_hops = int(info[1])
		number_routes = int(info[2])
		pkt_size = int(info[3])
		traffic = int(info[4])
		reload = int(info[5])
		print str(use_case_nb) + " : Running use case " + str(use_case) + " next_hops = " + str(next_hops) + " routes = " + str(number_routes) + " pkt_size = " + str(pkt_size) + " traffic = " + str(traffic) + " reload = " + str(reload)
		run_use_case(use_case, next_hops, number_routes, pkt_size, traffic, reload)
		use_case_nb = use_case_nb + 1

#========================================================================
def configure_use_case(use_case):
	Tests = []
	if (use_case == 0):
		for pkt_size in all_pkt_size:
			Tests.append(TestDefinition("0", "1", "1", pkt_size, "0", "1"))
		for pkt_size in all_pkt_size:
			Tests.append(TestDefinition("0", "1", "1", pkt_size, "1", "1"))
	if (use_case == 1):
		number_next_hops = 1
		reload = 0

		number_routes = number_next_hops	# At least same number of routes that number of next hops
		while number_routes <= max_number_routes:
			reload = 1
			for traffic in range(6):
				for pkt_size in all_pkt_size:
					Tests.append(TestDefinition(use_case, number_next_hops, number_routes, pkt_size, traffic, reload))
					reload = 0
			if (number_routes < max_number_routes / 2):
				number_routes = number_routes * 4
			else:
				number_routes = number_routes * 2

		number_routes = max_number_next_hops
		while number_next_hops <= max_number_next_hops:
			reload = 1
			for traffic in range(6):
				for pkt_size in all_pkt_size:
					Tests.append(TestDefinition(use_case, number_next_hops, number_routes, pkt_size, traffic, reload))
					reload = 0
			number_next_hops = number_next_hops * 2
	if (use_case == 2):
		number_next_hops = 1
		reload = 1
		for traffic in range(6):
			nb_destinations = 1
			while nb_destinations <= max_number_addresses_local_network:
				for pkt_size in all_pkt_size:
					Tests.append(TestDefinition(use_case, number_next_hops, nb_destinations, pkt_size, traffic, reload))
					reload = 0
				nb_destinations = nb_destinations * 2
			reload = 1

	file_tests = open('test_description.txt', 'w')
	file_tests.write("# Use case; next_hops; routes; pkt_size; traffic; reload;\n")
	for test in Tests:
		file_tests.write(str(test.use_case) + "; " + str(test.next_hops) + "; " +  str(test.number_routes) + "; " + str(test.pkt_size) + "; " + str(test.traffic) + "; " + str(test.reload) + ";\n")
	file_tests.close()

#========================================================================
if ((configure == 0) and (run == 0)):
	print "Nothing to do - please use -r 1 or -c 1"
if (configure == 1):
	configure_use_case(use_case)
if (run == 1):
	print "****************************************************************************************************************"
	print "** Running vRouter Characterization with " + str(test_duration) + " seconds steps and starting at " + str(init_speed)   + " percent of line rate **"
	print "****************************************************************************************************************"
	sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
	f_all = open('all_results.txt', 'w')
	f = open('detailed_results.txt', 'w')
	f_minimal = open('minimal_results.txt', 'w')
	file_tests = open('test_description.txt', 'r')
	run_all_use_cases()
	f.close();
	sock.close();