summaryrefslogtreecommitdiff
path: root/test/ptf_test/load_balance_test.py
blob: ba61356353e69cf55d9d757d2d2eeadb8138f775 (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
import ptf
from ptf.base_tests import BaseTest
from ptf.testutils import *
from scapy.all import *
from mrzcpd import Mrzcpd
from common_pkt import *
import ptf.mask as mask

start_conf = """
[device]
device=veth0,veth1,veth2,veth3,veth4
sz_tunnel=8192
sz_buffer=0

[device:veth0]
promisc=1
mtu=1500
driver=2
role=1

[device:veth1]
promisc=1
mtu=1500
driver=2
role=1

[device:veth2]
promisc=1
mtu=1500
driver=2
role=1

[device:veth3]
promisc=1
mtu=1500
driver=2
role=1

[device:veth4]
promisc=1
mtu=1500
driver=2
role=1

[service]
iocore=1
distmode=2
hashmode=0

[limits]
nr_max_ef_adapters=32
nr_max_vwires=32
nr_max_tera_adapters=32
nr_max_link_dbs=32

[eal]
virtaddr=0x600000000000
loglevel=7
nohuge=1
mem=65535

[keepalive]
check_spinlock=1

[http_server]
listen_addr=127.0.0.1
[ctrlzone]
ctrlzone0=tunnat, 64
ctrlzone1=vsys, 64

[pool]
create_mode=3
sz_direct_pktmbuf=4096
sz_indirect_pktmbuf=4096
sz_cache=256
sz_data=3000

[ctrlmsg]
listen_addr=0.0.0.0
listen_port=46789

[rpc]
addr=127.0.0.1
port=56789

# sid
[ef_adapters]
sid_start=100
sid_end=200
max_rules=256

[vwires]
sid_start=300
sid_end=400
max_rules=256

# vwire
[vwire:0]
interface_int=veth3
interface_ext=veth4

[service_lb]
sid_start=1000
sid_end=2000

# load balance
[load_balance:0]
sid=1000
mode=0
devices=veth0,0,veth1,0,veth2,0

"""

dynamic_conf = """
[classifier_rule:0]
rule_id=1
ruleset_type=0  
action=2
priority=1
sid=1000
vwire_id=0

"""
"""
    +-------+            +-------+         +------------+
    | Vwire |            |  LB   |         |     PTF    |
    +-------+            +-------+   100   +------------+
    | veth3 |            | veth0 |  ---->  | veth0-ptf0 |
    +-------+    300     +-------+   100   +------------+
    | veth4 |   ---->    | veth1 |  ---->  | veth1-ptf1 |
    +-------+            +-------+   100   +------------+
                         | veth2 |  ---->  | veth2-ptf2 |
                         +-------+         +------------+
"""


@group("lb_base_test")
class TestForBase(BaseTest):
    def setUp(self):
        self.dataplane = ptf.dataplane_instance

    def __init__(self):
        BaseTest.__init__(self)

    def runTest(self):
        try:
            # Init & Start mrzcpd
            mrzcpd = Mrzcpd(start_conf,dynamic_conf)
            mrzcpd.start()

            # Create packet list
            send_pkt_list = PacketList()

            # Create send packet
            for port in range(0, 300):
                pkt = simple_tcp_packet(ip_src=generate_random_ipv4(), ip_dst=generate_random_ipv4(
                ), tcp_sport=generate_random_tcp_port(), tcp_dport=generate_random_tcp_port())
                send_pkt_list.append(pkt)

            # Send packet
            for pkt in send_pkt_list:
                send_packet(self, 4, pkt)

            # Create verify packet
            tcp_pkt = simple_tcp_packet()
            verify_pkt = mask.Mask(tcp_pkt)
            verify_pkt.set_do_not_care(0, len(tcp_pkt) * 8)

            # Wait for a while to ensure enough packets arrive
            time.sleep(3)

            # Get packet count for each port
            packet_counts = []
            for port in range(0, 3):
                packet_count = get_port_packet_count(self, (0, port))
                print("port {} packet count: {}".format(port, packet_count))
                packet_counts.append(packet_count)

            # Calculate average packet count
            average_packet_count = sum(packet_counts) / len(packet_counts)

            # Set tolerance
            tolerance = 0.15 * average_packet_count

            # Check if packet count is abnormal
            for port in range(0, 3):
                if abs(packet_counts[port] - average_packet_count) > tolerance:
                    self.fail("Port %d has an abnormal packet count: %d (average is %d)" % (
                        port, packet_counts[port], average_packet_count))

        finally:
            mrzcpd.stop()


"""
    +-------+            +--------------+         +------------+
    | Vwire |            |     LB       |         |     PTF    |
    +-------+            +--------------+   150   +------------+
    | veth3 |            |     veth0    |  ---->  | veth0-ptf0 |
    +-------+    300     +--------------+   150   +------------+
    | veth4 |   ---->    |     veth1    |  ---->  | veth1-ptf1 |
    +-------+            +--------------+    0    +------------+
                         | veth2 (down) |  ---->  | veth2-ptf2 |
                         +--------------+         +------------+
"""


# The test current is invalid,because 'ifconfig veth2 down' can't affect the mrzcpd link status.
@group("lb_one_dev_down_test")
class TestForOneDevDown(BaseTest):
    def setUp(self):
        self.dataplane = ptf.dataplane_instance

    def __init__(self):
        BaseTest.__init__(self)

    def runTest(self):
        try:
            # Init & Start mrzcpd
            mrzcpd = Mrzcpd(start_conf,dynamic_conf)
            mrzcpd.start()

            # Set veth2 down
            os.system("ifconfig veth2 down")

            # Create packet list
            send_pkt_list = PacketList()

            # Create send packet
            for port in range(0, 300):
                pkt = simple_tcp_packet(ip_src=generate_random_ipv4(), ip_dst=generate_random_ipv4(
                ), tcp_sport=generate_random_tcp_port(), tcp_dport=generate_random_tcp_port())
                send_pkt_list.append(pkt)

            # Send packet
            for pkt in send_pkt_list:
                send_packet(self, 4, pkt)

            # Create verify packet
            tcp_pkt = simple_tcp_packet()
            verify_pkt = mask.Mask(tcp_pkt)
            verify_pkt.set_do_not_care(0, len(tcp_pkt) * 8)

            # Wait for a while to ensure enough packets arrive
            time.sleep(3)

            # Get packet count for each port
            packet_counts = []
            for port in range(0, 2):
                packet_count = get_port_packet_count(self, (0, port))
                print("port {} packet count: {}".format(port, packet_count))
                packet_counts.append(packet_count)

            # Calculate average packet count
            average_packet_count = sum(packet_counts) / len(packet_counts)

            # Set tolerance
            tolerance = 0.15 * average_packet_count

            # Check veth2 rx packet count
            if get_port_packet_count(self, (0, 2)) != 0:
                self.fail("veth2 is down, but rx packet count is not 0")

            # Check if packet count is abnormal
            for port in range(0, 2):
                if abs(packet_counts[port] - average_packet_count) > tolerance:
                    self.fail("Port %d has an abnormal packet count: %d (average is %d)" % (
                        port, packet_counts[port], average_packet_count))

        finally:
            # Set veth2 up
            os.system("ifconfig veth2 up")
            mrzcpd.stop()