-
Notifications
You must be signed in to change notification settings - Fork 0
/
OFH_tgen.py
executable file
·116 lines (96 loc) · 4.13 KB
/
OFH_tgen.py
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
import socket
import csv
import os
import time
import argparse
import sys
parser = argparse.ArgumentParser()
parser.add_argument("-r", "--RU", help="specify if this is the RU", action="store_true")
parser.add_argument("-i", "--ip", help="enter the distant end IP address", type=str, required=True)
parser.add_argument("-f", "--file", help="full path to the csv file", type=str, required=True)
args = parser.parse_args()
# These are the default values
RU_PORT = 5005
DU_PORT = 5115
data_size = 0
# add some arguments so we can specify a few options at run time
DU = not args.RU
file_name = args.file
if DU:
local_port = DU_PORT
distant_port = RU_PORT
else:
local_port = RU_PORT
distant_port = DU_PORT
Distant_IP = args.ip
print("UDP target IP: %s" % Distant_IP)
# sending UDP socket
send_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# receiving UDP socket
rec_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
rec_sock.bind(('', local_port))
with open(file_name, 'r') as csvfile:
datareader = csv.reader(csvfile)
row1 = next(datareader)
if DU: # The DU should always start
print("I am the DU, I start communication")
row2 = next(datareader)
# print(row2)
# chose one of the following lines based on the .csv file you will use
# if row2[2] != 'Mellanox_5e:6b:2a': # for layer 2 switched traffic
if row2[2] != '192.168.1.9': # for layer 3 routed traffic
print('RU starts, send start message')
send_sock.sendto(str.encode('Start'), (Distant_IP, distant_port))
start_time = time.time()
print('starting experiment')
print('listening for packet number %s' % row2[0])
# we also need to wait and listen for the first message
while time.time() - start_time <= float(row2[1]):
continue
else:
# it is our turn to start
data_size = int(row2[5]) -42
print('DU starting')
start_time = time.time()
Sdata = os.urandom(data_size)
send_sock.sendto(Sdata, (Distant_IP, distant_port))
for row in datareader:
# if row[2] == 'Mellanox_5e:6b:2a':
if row[2] == '192.168.1.9':
data_size = int(row[5]) -42
Sdata = os.urandom(data_size)
while time.time()-start_time <= float(row[1]): # but first, we have to check the time!
continue
send_sock.sendto(Sdata, (Distant_IP, distant_port))
print('Sending packet number %s' % row[0])
else:
# we should listen until we get data, or it is our turn to send again
print('listening for packet number %s' % row[0])
while time.time() - start_time <= float(row[1]):
continue
else: # if we are the RU, we need to wait for a message from the DU before moving on
print("waiting for DU")
while True:
data, address = rec_sock.recvfrom(8192)
if data:
start_time = time.time()
print("Starting experiment")
break
for row in datareader:
# chose one of the following lines based on the .csv file you will use
# if row[2] == 'Chongqin_00:04:de': # for layer 2 switched traffic
if row[2] == '192.168.1.8': # for layer 3 routed traffic
data_size = int(row[5]) -42
Sdata = os.urandom(data_size)
while time.time()-start_time <= float(row[1]): # but first, we have to check the time!
continue
send_sock.sendto(Sdata, (Distant_IP, distant_port))
print('Sending packet number %s' % row[0])
else:
print('listening for packet number %s' % row[0])
# we should listen until we get data, or it is our turn to send again
while time.time() - start_time <= float(row[1]):
continue
print('Test completed with the following parameters:')
print("UDP target IP: %s" % Distant_IP)
print("File used: %s" % file_name)