1##########################################################################
2# Copyright (c) 2017, ETH Zurich.
3# All rights reserved.
4#
5# This file is distributed under the terms in the attached LICENSE file.
6# If you do not find this file, copies can be found by writing to:
7# ETH Zurich D-INFK, Universitaetstrasse 6, CH-8092 Zurich. Attn: Systems Group.
8##########################################################################
9
10import re, datetime
11import debug, tests
12import subprocess
13import os
14import socket, struct, fcntl
15import thread
16from common import TestCommon, TimeoutError
17from results import RowResults, PassFailResult
18
19TEST_TIMEOUT = datetime.timedelta(minutes=240)
20
21mac = {'babybel1': 130587495626,
22       'babybel2': 130587510022,
23       'babybel3': 130587512798,
24       'babybel4': 130589790232,
25       'ziger2': 65817495764,
26       'ziger1': 116527143012, }
27
28# Fallback if gethostip does not work
29ip = {'babybel1': 174982272,
30       'babybel2': 174982270,
31       'babybel3': 174982271,
32       'ziger2': 174982183,
33       'ziger1': 174982183, }
34
35
36class DevifTests(TestCommon):
37
38    def __init__(self, options):
39        super(DevifTests, self).__init__(options)
40
41    def get_module_name(self):
42        return "devif_test"
43
44    def boot(self, *args):
45        super(DevifTests, self).boot(*args)
46        self.set_timeout(TEST_TIMEOUT)
47
48    def get_decimal_ip(self, hostname):
49        try:
50            iphex = subprocess.check_output('gethostip -x %s' % hostname, shell=True)
51            return '%d' % int(iphex, 16)
52        except:
53            return ip[hostname.split('-')[0]]
54
55    def get_local_mac(self, ifname):
56        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
57        info = fcntl.ioctl(s.fileno(), 0x8927,  struct.pack('256s', ifname[:15]))
58        hexmac = ''.join(['%02x' % ord(char) for char in info[18:24]])
59        return '%d' % int(hexmac, 16)
60
61    def get_modules(self, build, machine):
62        self.machine = machine.name
63        modules = super(DevifTests, self).get_modules(build, machine)
64        modules.add_module("e1000n", ["auto"])
65        modules.add_module("net_sockets_server", ["nospawn"])
66        modules.add_module("devif_idc", ["core=1"])
67        modules.add_module("e10k", ["auto", "function=0"])
68
69        hostname = '%s.in.barrelfish.org' % subprocess.check_output('hostname -s', shell=True).rstrip()
70        src_ip = self.get_decimal_ip(hostname)
71
72        if 'ziger2' in machine.name:
73            modules.add_module("sfn5122f", ["auto", "function=0"])
74            dst_ip = self.get_decimal_ip('%s-sf.in.barrelfish.org' % machine.name)
75        else:
76            dst_ip = self.get_decimal_ip('%s-e10k.in.barrelfish.org' % machine.name)
77
78        modules.add_module(self.get_module_name(), ["core=2", self.OP, src_ip, dst_ip, self.CARD])
79        return modules
80
81    def get_finish_string(self):
82        return "SUCCESS"
83
84
85    def thread_func (self, dummy, dummy2):
86        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
87        while True:
88            s.sendto("Data Data Data", (self.ip, 7))
89
90    def start_loop(self):
91        self.thread = thread.start_new_thread(self.thread_func, (self, 0))
92
93    def process_line(self, line):
94        m = re.match(r'# IP Addr (\d+\.\d+\.\d+\.\d+)', line)
95        if m:
96            self.start_loop()
97            self.ip = m.group(1)
98
99
100    def process_data(self, testdir, rawiter):
101        for line in rawiter:
102            if "SUCCESS" in line:
103                return PassFailResult(True)
104
105        return PassFailResult(False)
106
107
108@tests.add_test
109class DevifNetTxSF(DevifTests):
110    ''' Devif Net TX Test'''
111    name = "devif_nettx_sf"
112    OP = "net_tx"
113    CARD = "sfn5122f"
114
115@tests.add_test
116class DevifNetTxE10k(DevifTests):
117    ''' Devif Net TX Test'''
118    name = "devif_nettx_e10k"
119    OP = "net_tx"
120    CARD = "e10k:8086:10fb:0006:0000:0000"
121
122
123@tests.add_test
124class DevifNetRxSF(DevifTests):
125    ''' Devif Net RX Test'''
126    name = "devif_netrx_sf"
127    OP = "net_rx"
128    CARD = "sfn5122f"
129
130@tests.add_test
131class DevifNetRxE10k(DevifTests):
132    ''' Devif Net RX Test'''
133    name = "devif_netrx_e10k"
134    OP = "net_rx"
135    CARD = "e10k"
136
137@tests.add_test
138class DevifIdcTest(DevifTests):
139    ''' Devif IDC Test'''
140    name = "devif_idc_test"
141    OP = "idc"
142    CARD = "none"
143
144    def get_modules(self, build, machine):
145        self.machine = machine.name
146        modules = super(DevifTests, self).get_modules(build, machine)
147        modules.add_module("devif_idc", ["core=1"])
148        modules.add_module(self.get_module_name(), ["core=2", self.OP, 0, 0, "none"])
149        return modules
150
151
152@tests.add_test
153class DevifDebug(DevifTests):
154    ''' Devif Debug Backend Test'''
155    name = "devif_debug"
156
157    def get_modules(self, build, machine):
158        self.machine = machine.name
159        modules = super(DevifTests, self).get_modules(build, machine)
160        modules.add_module("devif_idc", ["core=1"])
161        modules.add_module("devif_debug_test")
162
163        return modules
164
165@tests.add_test
166class DevifDebug(DevifTests):
167    ''' Devif Benchmark'''
168    name = "devif_bench"
169
170    def get_modules(self, build, machine):
171        self.machine = machine.name
172        modules = super(DevifTests, self).get_modules(build, machine)
173        modules.add_module("e1000n", ["auto"])
174        modules.add_module("net_sockets_server", ["nospawn"])
175        modules.add_module("devif_bench", ["core=2", machine.name])
176
177        return modules
178
179
180@tests.add_test
181class DevifDebug(DevifTests):
182    ''' Devif Benchmark'''
183    name = "devif_bench_stack"
184
185    def get_modules(self, build, machine):
186        self.machine = machine.name
187        modules = super(DevifTests, self).get_modules(build, machine)
188        modules.add_module("e1000n", ["auto"])
189        modules.add_module("net_sockets_server", ["nospawn"])
190        modules.add_module("devif_bench_stack", ["core=2", machine.name])
191
192        return modules
193
194@tests.add_test
195class DevifUDP(DevifTests):
196    ''' Devif UDP Backend Test'''
197    name = "devif_udp"
198    data = ("Data Data Data Data")
199
200    def get_module_name(self):
201        return "devif_udp"
202
203    def get_modules(self, build, machine):
204        self.machine = machine.name
205        modules = super(DevifTests, self).get_modules(build, machine)
206        modules.add_module("net_sockets_server", ["nospawn"])
207        hostname = '%s.in.barrelfish.org' % subprocess.check_output('hostname -s', shell=True).rstrip()
208        dst_ip = self.get_decimal_ip(hostname)
209        dst_mac = self.get_local_mac('eno2')
210
211        if ('ziger2' in machine.name):
212        #if ('ziger2' in machine.name) or ('babybel2' in machine.name):
213            if 'ziger2' in machine.name:
214                src_ip = self.get_decimal_ip('%s-sf.in.barrelfish.org' % machine.name)
215                modules.add_module("sfn5122f", ["auto", "function=0"])
216                self.cardname = "sfn5122f"
217            else:
218                src_ip = self.get_decimal_ip('%s-sf.in.barrelfish.org' % machine.name)
219                modules.add_module("sfn5122f", ["auto", "function=0"])
220                self.cardname = "sfn5122f"
221        else:
222            modules.add_module("e10k", ["auto", "function=0"])
223            src_ip = self.get_decimal_ip('%s-e10k.in.barrelfish.org' % machine.name)
224            self.cardname = "e10k:8086:10fb:0006:0000:0000"
225
226        modules.add_module(self.get_module_name(), ["core=2", dst_ip, dst_mac, 20000, 20000, self.cardname])
227        return modules
228
229    def thread_func (self, dummy, dummy2):
230        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
231        while True:
232            s.sendto(self.data, (self.ip, 20000))
233
234    def start_loop(self):
235        self.thread = thread.start_new_thread(self.thread_func, (self, 0))
236
237    def process_line(self, line):
238        m = re.match(r'# IP Addr (\d+\.\d+\.\d+\.\d+)', line)
239        if m:
240            self.ip = m.group(1)
241
242        m1 = re.match(r'Testing receiving UDP packets', line)
243        if m1:
244            self.start_loop()
245
246    def process_data(self, testdir, rawiter):
247        for line in rawiter:
248            if "SUCCESS" in line:
249                return PassFailResult(True)
250
251        return PassFailResult(False)
252
253
254class DevifDebug(DevifTests):
255    ''' Devif Benchmark'''
256    name = "devif_bench_stack"
257
258    def get_modules(self, build, machine):
259        self.machine = machine.name
260        modules = super(DevifTests, self).get_modules(build, machine)
261        modules.add_module("e1000n", ["auto"])
262        modules.add_module("net_sockets_server", ["nospawn"])
263        modules.add_module("devif_bench_stack", ["core=2", machine.name])
264
265        return modules
266
267#@tests.add_test
268#class DevifUPDecho(DevifUDP):
269#    ''' Devif Debug Backend Test'''
270#    name = "devif_udp_echo"
271#
272#    def get_module_name(self):
273#        return "devif_echo"
274
275
276
277