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, Haldeneggsteig 4, 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=8)
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@tests.add_test
145class DevifDebug(DevifTests):
146    ''' Devif Debug Backend Test'''
147    name = "devif_debug"
148
149    def get_modules(self, build, machine):
150        self.machine = machine.name
151        modules = super(DevifTests, self).get_modules(build, machine)
152        modules.add_module("devif_idc", ["core=1"])
153        modules.add_module("devif_debug_test")
154
155        return modules
156
157@tests.add_test
158class DevifUDP(DevifTests):
159    ''' Devif UDP Backend Test'''
160    name = "devif_udp"
161    data = ("Data Data Data Data")
162
163    def get_module_name(self):
164        return "devif_udp"
165
166    def get_modules(self, build, machine):
167        self.machine = machine.name
168        modules = super(DevifTests, self).get_modules(build, machine)
169        modules.add_module("net_sockets_server", ["nospawn"])
170        hostname = '%s.in.barrelfish.org' % subprocess.check_output('hostname -s', shell=True).rstrip()
171        dst_ip = self.get_decimal_ip(hostname)
172        dst_mac = self.get_local_mac('eno2')
173
174        if 'ziger2' in machine.name:
175            src_ip = self.get_decimal_ip('%s-sf.in.barrelfish.org' % machine.name)
176            modules.add_module("sfn5122f", ["auto", "function=0"])
177            self.cardname = "sfn5122f"
178        else:
179            modules.add_module("e10k", ["auto", "function=0"])
180            src_ip = self.get_decimal_ip('%s-e10k.in.barrelfish.org' % machine.name)
181            self.cardname = "e10k:8086:10fb:0006:0000:0000"
182
183        src_mac =  mac[machine.name]
184
185        modules.add_module(self.get_module_name(), ["core=2", dst_ip, dst_mac, 20000, 20000, self.cardname])
186        return modules
187
188    def thread_func (self, dummy, dummy2):
189        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
190        while True:
191            s.sendto(self.data, (self.ip, 20000))
192
193    def start_loop(self):
194        self.thread = thread.start_new_thread(self.thread_func, (self, 0))
195
196    def process_line(self, line):
197        m = re.match(r'# IP Addr (\d+\.\d+\.\d+\.\d+)', line)
198        if m:
199            self.ip = m.group(1)
200
201        m1 = re.match(r'Testing receiving UDP packets', line)
202        if m1:
203            self.start_loop()
204
205    def process_data(self, testdir, rawiter):
206        for line in rawiter:
207            if "SUCCESS" in line:
208                return PassFailResult(True)
209
210        return PassFailResult(False)
211
212#@tests.add_test
213#class DevifUPDecho(DevifUDP):
214#    ''' Devif Debug Backend Test'''
215#    name = "devif_udp_echo"
216#
217#    def get_module_name(self):
218#        return "devif_echo"
219
220
221
222