Statistics
| Branch: | Revision:

nepatest_olsr1 / test_code / olsr.py @ 63e61e90

History | View | Annotate | Download (5.92 KB)

1

    
2
import sys
3
import os
4
sys.path.append('../')
5
from network_builder import *
6
import random
7
import time
8
from test_generic import *
9

    
10

    
11
class OLSRTest(MininetTest):
12

    
13
    def launch_sniffer(self, host):
14
        """ helper function """
15

    
16
        cmd = "tcpdump -i any -n -X -e "
17

    
18
        logfile = self.prefix + host.name + "-dump.log"
19

    
20
        params = {}
21
        params['>'] = logfile
22
        params['2>'] = logfile
23

    
24
        return self.bgCmd(host, True, cmd,
25
                          *reduce(lambda x, y: x + y, params.items()))
26

    
27
    def launch_ping(self, host, dest_array, run_id=0):
28

    
29
        for idx, ip in enumerate(dest_array):
30
            cmd = "ping " + ip
31

    
32
            log_str = "Host " + host.name + " launching command:\n"
33
            info(log_str)
34
            info(cmd + "\n")
35
            logfile = self.prefix + host.name + \
36
                "_ping_" + str(idx) + "_runId_" + str(run_id) + ".log"
37
            params = {}
38
            params['>'] = logfile
39
            params['2>'] = logfile
40
            self.bgCmd(host, True, cmd,
41
                       reduce(lambda x, y: x + y, params.items()))
42

    
43
    def launch_OLSR(self, host,  args):
44

    
45
        cmd = self.olsrd + " " + args 
46

    
47
        log_str = "Host " + host.name + " launching command:\n"
48
        info(log_str)
49
        info(cmd + "\n")
50
        logfile = self.prefix + host.name + ".log"
51

    
52
        params = {}
53
        params['>'] = logfile
54
        params['2>'] = logfile
55

    
56
        return self.bgCmd(host, True, cmd,
57
                          *reduce(lambda x, y: x + y, params.items()))
58
    def start_ping(self, exclude=[], number=1, run_id=0):
59

    
60
        host_list = self.getAllHosts()
61
        exclude_ips = []
62
        destinations = {}
63

    
64
        for h in host_list:
65
            destinations[h] = []
66

    
67
        for h in destinations:
68
            intf = h.intfList()
69
            counter = 1000
70
            while len(destinations[h]) < self.NumPing:
71
                d = self.get_random_destination()
72
                if d != intf[0].ip and d not in exclude_ips:
73
                    destinations[h].append(d)
74
                counter -= 1
75
                if counter < 0:
76
                    error("Can not find ping destination for host " +
77
                          h.name + " and NumPing " + str(self.NumPing) +
78
                          " your configuration may be wicked." +
79
                          "Nodes that will fail are:" + str(exclude))
80
                    exit(1)
81

    
82
        for (h, ip_list) in destinations.items():
83
                self.launch_ping(h, ip_list, run_id=run_id)
84

    
85
    def get_random_destination(self):
86

    
87
        host_list = self.getAllHosts()
88
        d = random.sample(host_list, 1)[0]
89
        dest_ip = d.intfList()[0].ip
90
        return dest_ip
91

    
92
    def runTest(self, run_id=0):
93

    
94
        host_list = self.getAllHosts()
95

    
96
        # default values
97
        hello_timer = self.HelloInterval
98
        tc_timer = self.TcInterval
99

    
100
        for idx, h in enumerate(host_list):
101
            intf = h.intfList()
102
            intf_list = ' '.join(["\"" + i.name + "\"" for i in intf])
103

    
104
            try:
105
                os.makedirs(self.conf_folder)
106
            except:
107
                pass
108
            olsr_conf_file = self.conf_folder + h.name + ".conf"
109
            olsr_lock_file = "/var/run/" + h.name + str(time.time()) + ".lock"
110
            f = open(olsr_conf_file, "w")
111
            hello_validity = hello_timer * 3
112
            tc_validity = tc_timer * 3
113
            print >> f, self.conf_file % (olsr_lock_file, intf_list,
114
                                          hello_timer, hello_validity,
115
                                          tc_timer, tc_validity)
116
            f.close()
117
            args = "-f " + os.path.abspath(olsr_conf_file)
118

    
119
            launch_pid = self.launch_OLSR(h, args)
120

    
121
            if self.dump:
122
                self.launch_sniffer(h)
123
            #if self.numPing:
124

    
125
        info("Waiting completion...\n")
126
        self.wait(self.duration, log_resources={'net': 'netusage.csv',
127
                                                'cpu': 'cpuusage.csv',
128
                                                'mem': 'memusage.csv'})
129
        self.killAll()
130
        return launch_pid
131

    
132

    
133

    
134
    def __init__(self, mininet, name, args):
135

    
136
        super(OLSRTest, self).__init__(mininet, name, args)
137

    
138
        self.mininet = mininet
139
        self.centList = []
140
        self.stopNodes = []
141
        self.stopNodeList = []
142

    
143
        self.conf_file = """
144
        DebugLevel  1
145
        IpVersion 4
146
        FIBMetric "flat"
147
        LinkQualityFishEye  0
148
        LockFile "%s"
149
        Hna4
150
        {
151
        }
152
        Hna6
153
        {
154
        }
155

156
        InterfaceDefaults {
157
        }
158

159
        LoadPlugin "../olsrd/lib/txtinfo/olsrd_txtinfo.so.1.1"
160
        {
161
        }
162

163
        LoadPlugin "../olsrd/lib/jsoninfo/olsrd_jsoninfo.so.1.1"
164
        {
165
        PlParam "accept" "0.0.0.0"
166
        }
167

168
        Interface %s
169
        {
170
        HelloInterval       %f
171
        HelloValidityTime   %f
172
        TcInterval          %f
173
        TcValidityTime      %f
174
        }
175
        """
176

    
177

    
178
        if not "olsrPath" in args.keys():
179
            print "ERROR: you must set olsrPath config option, " + \
180
                  "specifying where the OLSR binary is"
181
            exit()
182
        else:
183
            self.olsrd = args['olsrPath']
184

    
185
        if "NumPing" in args.keys():
186
            self.NumPing = int(args["NumPing"])
187
        else:
188
            self.NumPing = 1
189

    
190
        if "HelloInterval" in args.keys():
191
            self.HelloInterval = float(args["HelloInterval"])
192
        else:
193
            self.HelloInterval = 2
194

    
195
        if "TcInterval" in args.keys():
196
            self.TcInterval = float(args["TcInterval"])
197
        else:
198
            self.TcInterval = 5
199

    
200
        if "dumpTraffic" in args.keys():
201
            self.dump = True
202
        else:
203
            self.dump = False
204

    
205
        if "duration" in args.keys():
206
            self.duration = float(args["duration"])
207
        else:
208
            self.duration = 60
209

    
210
        self.setPrefix(name)
211
        self.conf_folder = self.prefix + "OLSR-conf/"
212
        print "XXX", self.duration