blob: 48155a5a02523d3f5ca1190c83897db5b6f34168 [file] [log] [blame]
Damjan Marionf56b77a2016-10-03 19:44:57 +02001#!/usr/bin/env python
2
Damjan Marionf56b77a2016-10-03 19:44:57 +02003import unittest
Klement Sekeraf62ae122016-10-11 11:47:09 +02004import socket
5from logging import *
6
Damjan Marionf56b77a2016-10-03 19:44:57 +02007from framework import VppTestCase, VppTestRunner
Klement Sekeraf62ae122016-10-11 11:47:09 +02008from vpp_sub_interface import VppSubInterface, VppDot1QSubint, VppDot1ADSubint
Damjan Marionf56b77a2016-10-03 19:44:57 +02009
10from scapy.packet import Raw
Klement Sekeraf62ae122016-10-11 11:47:09 +020011from scapy.layers.l2 import Ether, Dot1Q, ARP
Damjan Marionf56b77a2016-10-03 19:44:57 +020012from scapy.layers.inet import IP, UDP
13
14
Klement Sekeraf62ae122016-10-11 11:47:09 +020015class TestIPv4(VppTestCase):
Damjan Marionf56b77a2016-10-03 19:44:57 +020016 """ IPv4 Test Case """
17
18 @classmethod
19 def setUpClass(cls):
20 super(TestIPv4, cls).setUpClass()
21
Klement Sekeraf62ae122016-10-11 11:47:09 +020022 def setUp(self):
23 super(TestIPv4, self).setUp()
Damjan Marionf56b77a2016-10-03 19:44:57 +020024
Klement Sekeraf62ae122016-10-11 11:47:09 +020025 # create 3 pg interfaces
26 self.create_pg_interfaces(range(3))
Damjan Marionf56b77a2016-10-03 19:44:57 +020027
Klement Sekeraf62ae122016-10-11 11:47:09 +020028 # create 2 subinterfaces for pg1 and pg2
29 self.sub_interfaces = [
30 VppDot1QSubint(self, self.pg1, 100),
31 VppDot1ADSubint(self, self.pg2, 200, 300, 400)]
Damjan Marionf56b77a2016-10-03 19:44:57 +020032
Klement Sekeraf62ae122016-10-11 11:47:09 +020033 # packet flows mapping pg0 -> pg1.sub, pg2.sub, etc.
34 self.flows = dict()
35 self.flows[self.pg0] = [self.pg1.sub_if, self.pg2.sub_if]
36 self.flows[self.pg1.sub_if] = [self.pg0, self.pg2.sub_if]
37 self.flows[self.pg2.sub_if] = [self.pg0, self.pg1.sub_if]
Damjan Marionf56b77a2016-10-03 19:44:57 +020038
Klement Sekeraf62ae122016-10-11 11:47:09 +020039 # packet sizes
40 self.pg_if_packet_sizes = [64, 512, 1518, 9018]
41 self.sub_if_packet_sizes = [64, 512, 1518 + 4, 9018 + 4]
Damjan Marionf56b77a2016-10-03 19:44:57 +020042
Klement Sekeraf62ae122016-10-11 11:47:09 +020043 self.interfaces = list(self.pg_interfaces)
44 self.interfaces.extend(self.sub_interfaces)
Damjan Marionf56b77a2016-10-03 19:44:57 +020045
Klement Sekeraf62ae122016-10-11 11:47:09 +020046 # setup all interfaces
47 for i in self.interfaces:
48 i.admin_up()
49 i.config_ip4()
50 i.resolve_arp()
51
52 # config 2M FIB enries
53 self.config_fib_entries(200)
Damjan Marionf56b77a2016-10-03 19:44:57 +020054
55 def tearDown(self):
Klement Sekeraf62ae122016-10-11 11:47:09 +020056 super(TestIPv4, self).tearDown()
57 if not self.vpp_dead:
58 info(self.vapi.cli("show ip arp"))
59 # info(self.vapi.cli("show ip fib")) # many entries
Damjan Marionf56b77a2016-10-03 19:44:57 +020060
Klement Sekeraf62ae122016-10-11 11:47:09 +020061 def config_fib_entries(self, count):
62 n_int = len(self.interfaces)
63 percent = 0
64 counter = 0.0
65 dest_addr = socket.inet_pton(socket.AF_INET, "10.0.0.1")
66 dest_addr_len = 32
67 for i in self.interfaces:
68 next_hop_address = i.local_ip4n
69 for j in range(count / n_int):
70 self.vapi.ip_add_del_route(
71 dest_addr, dest_addr_len, next_hop_address)
72 counter = counter + 1
73 if counter / count * 100 > percent:
74 info("Configure %d FIB entries .. %d%% done" %
75 (count, percent))
76 percent = percent + 1
Damjan Marionf56b77a2016-10-03 19:44:57 +020077
Klement Sekeraf62ae122016-10-11 11:47:09 +020078 def create_stream(self, src_if, packet_sizes):
Damjan Marionf56b77a2016-10-03 19:44:57 +020079 pkts = []
80 for i in range(0, 257):
Klement Sekeraf62ae122016-10-11 11:47:09 +020081 dst_if = self.flows[src_if][i % 2]
82 info = self.create_packet_info(
83 src_if.sw_if_index, dst_if.sw_if_index)
Damjan Marionf56b77a2016-10-03 19:44:57 +020084 payload = self.info_to_payload(info)
Klement Sekeraf62ae122016-10-11 11:47:09 +020085 p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
86 IP(src=src_if.remote_ip4, dst=dst_if.remote_ip4) /
Damjan Marionf56b77a2016-10-03 19:44:57 +020087 UDP(sport=1234, dport=1234) /
88 Raw(payload))
89 info.data = p.copy()
Klement Sekeraf62ae122016-10-11 11:47:09 +020090 if isinstance(src_if, VppSubInterface):
91 p = src_if.add_dot1_layer(p)
92 size = packet_sizes[(i // 2) % len(packet_sizes)]
Damjan Marionf56b77a2016-10-03 19:44:57 +020093 self.extend_packet(p, size)
94 pkts.append(p)
95 return pkts
96
Klement Sekeraf62ae122016-10-11 11:47:09 +020097 def verify_capture(self, dst_if, capture):
98 info("Verifying capture on interface %s" % dst_if.name)
99 last_info = dict()
Damjan Marionf56b77a2016-10-03 19:44:57 +0200100 for i in self.interfaces:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200101 last_info[i.sw_if_index] = None
102 is_sub_if = False
103 dst_sw_if_index = dst_if.sw_if_index
104 if hasattr(dst_if, 'parent'):
105 is_sub_if = True
Damjan Marionf56b77a2016-10-03 19:44:57 +0200106 for packet in capture:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200107 if is_sub_if:
108 # Check VLAN tags and Ethernet header
109 packet = dst_if.remove_dot1_layer(packet)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200110 self.assertTrue(Dot1Q not in packet)
111 try:
112 ip = packet[IP]
113 udp = packet[UDP]
114 payload_info = self.payload_to_info(str(packet[Raw]))
115 packet_index = payload_info.index
Klement Sekeraf62ae122016-10-11 11:47:09 +0200116 self.assertEqual(payload_info.dst, dst_sw_if_index)
117 debug("Got packet on port %s: src=%u (id=%u)" %
118 (dst_if.name, payload_info.src, packet_index))
119 next_info = self.get_next_packet_info_for_interface2(
120 payload_info.src, dst_sw_if_index,
121 last_info[payload_info.src])
122 last_info[payload_info.src] = next_info
Damjan Marionf56b77a2016-10-03 19:44:57 +0200123 self.assertTrue(next_info is not None)
124 self.assertEqual(packet_index, next_info.index)
125 saved_packet = next_info.data
126 # Check standard fields
127 self.assertEqual(ip.src, saved_packet[IP].src)
128 self.assertEqual(ip.dst, saved_packet[IP].dst)
129 self.assertEqual(udp.sport, saved_packet[UDP].sport)
130 self.assertEqual(udp.dport, saved_packet[UDP].dport)
131 except:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200132 error("Unexpected or invalid packet:")
133 error(packet.show())
Damjan Marionf56b77a2016-10-03 19:44:57 +0200134 raise
135 for i in self.interfaces:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200136 remaining_packet = self.get_next_packet_info_for_interface2(
137 i.sw_if_index, dst_sw_if_index, last_info[i.sw_if_index])
138 self.assertTrue(
139 remaining_packet is None,
140 "Interface %s: Packet expected from interface %s didn't arrive" %
141 (dst_if.name, i.name))
Damjan Marionf56b77a2016-10-03 19:44:57 +0200142
143 def test_fib(self):
144 """ IPv4 FIB test """
145
Klement Sekeraf62ae122016-10-11 11:47:09 +0200146 pkts = self.create_stream(self.pg0, self.pg_if_packet_sizes)
147 self.pg0.add_stream(pkts)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200148
Klement Sekeraf62ae122016-10-11 11:47:09 +0200149 for i in self.sub_interfaces:
150 pkts = self.create_stream(i, self.sub_if_packet_sizes)
151 i.parent.add_stream(pkts)
152
153 self.pg_enable_capture(self.pg_interfaces)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200154 self.pg_start()
155
Klement Sekeraf62ae122016-10-11 11:47:09 +0200156 pkts = self.pg0.get_capture()
157 self.verify_capture(self.pg0, pkts)
158
159 for i in self.sub_interfaces:
160 pkts = i.parent.get_capture()
161 self.verify_capture(i, pkts)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200162
163
164if __name__ == '__main__':
Klement Sekeraf62ae122016-10-11 11:47:09 +0200165 unittest.main(testRunner=VppTestRunner)