vpp-device: GENEVE tunnel test, l3 mode
[csit.git] / GPL / traffic_scripts / geneve_tunnel.py
1 #!/usr/bin/env python3
2
3 # Copyright (c) 2020 Cisco and/or its affiliates.
4 #
5 # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 #
7 # Licensed under the Apache License 2.0 or
8 # GNU General Public License v2.0 or later;  you may not use this file
9 # except in compliance with one of these Licenses. You
10 # may obtain a copy of the Licenses at:
11 #
12 #     http://www.apache.org/licenses/LICENSE-2.0
13 #     https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
14 #
15 # Note: If this file is linked with Scapy, which is GPLv2+, your use of it
16 # must be under GPLv2+.  If at any point in the future it is no longer linked
17 # with Scapy (or other GPLv2+ licensed software), you are free to choose
18 # Apache 2.
19 #
20 # Unless required by applicable law or agreed to in writing, software
21 # distributed under the License is distributed on an "AS IS" BASIS,
22 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 # See the License for the specific language governing permissions and
24 # limitations under the License.
25
26 """Traffic script for GENEVE tunnel verification."""
27
28 import sys
29
30 from ipaddress import ip_address
31 from scapy.contrib.geneve import GENEVE
32 from scapy.layers.inet import IP, UDP
33 from scapy.layers.inet6 import IPv6, ICMPv6ND_NS
34 from scapy.layers.l2 import Ether
35 from scapy.packet import Raw
36
37 from .PacketVerifier import RxQueue, TxQueue
38 from .TrafficScriptArg import TrafficScriptArg
39
40
41 def check_geneve(
42         pkt_recv, ip_layer, outer_src_mac, outer_dst_mac, inner_src_mac,
43         inner_dst_mac, outer_src_ip, outer_dst_ip, inner_src_ip, inner_dst_ip,
44         udp_dport, gen_vni):
45     """Check received GENEVE packet.
46
47     :param pkt_recv: Received packet to verify.
48     :param ip_layer: Scapy IP or IPv6 layer.
49     :param outer_src_mac: Outer source MAC address.
50     :param outer_dst_mac: Outer destination MAC address.
51     :param inner_src_mac: Inner source MAC address.
52     :param inner_dst_mac: Inner destination MAC address.
53     :param outer_src_ip: Outer source IP/IPv6 address.
54     :param outer_dst_ip: Outer destination IP/IPv6 address.
55     :param inner_src_ip: Inner source IP/IPv6 address.
56     :param inner_dst_ip: Inner destination IP/IPv6 address.
57     :param udp_dport: UDP destination port.
58     :param gen_vni: GENEVE VNI.
59     :type pkt_recv: scapy.Ether
60     :type ip_layer: scapy.layers.inet.IP or scapy.layers.inet6.IPv6
61     :type outer_src_mac: str
62     :type outer_dst_mac: str
63     :type inner_src_mac: str
64     :type inner_dst_mac: str
65     :type outer_src_ip: str
66     :type outer_dst_ip: str
67     :type inner_src_ip: str
68     :type inner_dst_ip: str
69     :type udp_dport: int
70     :type gen_vni: int
71     :raises RuntimeError: If received packet is invalid.
72     """
73     try:
74         if not isinstance(pkt_recv[0], Ether):
75             raise RuntimeError(
76                 f"Received packet hasn't outer Ether layer: {pkt_recv!r}"
77             )
78         elif not isinstance(pkt_recv[1], ip_layer):
79             raise RuntimeError(
80                 f"Received packet hasn't outer {ip_layer.__name__} layer: "
81                 f"{pkt_recv!r}"
82             )
83         elif not isinstance(pkt_recv[2], UDP):
84             raise RuntimeError(
85                 f"Received packet hasn't UDP layer: {pkt_recv!r}"
86             )
87         elif not isinstance(pkt_recv[3], GENEVE):
88             raise RuntimeError(
89                 f"Received packet hasn't GENEVE layer: {pkt_recv!r}"
90             )
91         elif not isinstance(pkt_recv[4], Ether):
92             raise RuntimeError(
93                 f"Received packet hasn't inner Ether layer: {pkt_recv!r}"
94             )
95         elif not isinstance(pkt_recv[5], ip_layer):
96             raise RuntimeError(
97                 f"Received packet hasn't inner {ip_layer.__name__} layer: "
98                 f"{pkt_recv!r}"
99             )
100         elif not isinstance(pkt_recv[6], Raw):
101             raise RuntimeError(
102                 f"Received packet hasn't inner Raw layer: {pkt_recv!r}"
103             )
104     except IndexError:
105         raise RuntimeError(
106             f"Received packet doesn't contain all layers: {pkt_recv!r}"
107         )
108
109     if pkt_recv[Ether].src != outer_src_mac:
110         raise RuntimeError(
111             f"Received frame has invalid outer source MAC address: "
112             f"{pkt_recv[Ether].src} should be: {outer_src_mac}"
113         )
114
115     if pkt_recv[Ether].dst != outer_dst_mac:
116         raise RuntimeError(
117             f"Received frame has invalid outer destination MAC address: "
118             f"{pkt_recv[Ether].dst} should be: {outer_dst_mac}"
119         )
120
121     if pkt_recv[ip_layer].src != outer_src_ip:
122         raise RuntimeError(
123             f"Received packet has invalid outer source address: "
124             f"{pkt_recv[ip_layer].src} should be: {outer_src_ip}"
125         )
126
127     if pkt_recv[ip_layer].dst != outer_dst_ip:
128         raise RuntimeError(
129             f"Received packet has invalid outer destination address: "
130             f"{pkt_recv[ip_layer].dst} should be: {outer_dst_ip}"
131         )
132
133     if pkt_recv[UDP].dport != udp_dport:
134         raise RuntimeError(
135             f"Received packet has invalid UDP dport: "
136             f"{pkt_recv[UDP].dport} should be: {udp_dport}"
137         )
138
139     if pkt_recv[GENEVE].vni != gen_vni:
140         raise RuntimeError(
141             f"Received packet has invalid GENEVE vni: "
142             f"{pkt_recv[GENEVE].vni} should be: {gen_vni}"
143         )
144
145     if pkt_recv[GENEVE].proto != 0x6558:
146         raise RuntimeError(
147             f"Received packet has invalid GENEVE protocol number: "
148             f"{pkt_recv[GENEVE].proto} should be: 0x6558"
149         )
150
151     if pkt_recv[Ether:2].src != inner_src_mac:
152         raise RuntimeError(
153             f"Received frame has invalid inner source MAC address: "
154             f"{pkt_recv[Ether:2].src} should be: {inner_src_mac}"
155         )
156
157     if pkt_recv[Ether:2].dst != inner_dst_mac:
158         raise RuntimeError(
159             f"Received frame has invalid inner destination MAC address: "
160             f"{pkt_recv[Ether:2].dst} should be: {inner_dst_mac}"
161         )
162
163     if pkt_recv[ip_layer:2].src != inner_src_ip:
164         raise RuntimeError(
165             f"Received packet has invalid inner source address: "
166             f"{pkt_recv[ip_layer:2].src} should be: {inner_src_ip}"
167         )
168
169     if pkt_recv[ip_layer:2].dst != inner_dst_ip:
170         raise RuntimeError(
171             f"Received packet has invalid inner destination address: "
172             f"{pkt_recv[ip_layer:2].dst} should be: {inner_dst_ip}"
173         )
174
175
176 def check_ip(pkt_recv, ip_layer, src_mac, dst_mac, src_ip, dst_ip):
177     """Check received IP/IPv6 packet.
178
179     :param pkt_recv: Received packet to verify.
180     :param ip_layer: Scapy IP layer.
181     :param src_mac: Source MAC address.
182     :param dst_mac: Destination MAC address.
183     :param src_ip: Source IP/IPv6 address.
184     :param dst_ip: Destination IP/IPv6 address.
185     :type pkt_recv: scapy.Ether
186     :type ip_layer: scapy.layers.inet.IP or scapy.layers.inet6.IPv6
187     :type src_mac: str
188     :type dst_mac: str
189     :type src_ip: str
190     :type dst_ip: str
191     :raises RuntimeError: If received packet is invalid.
192     """
193     if pkt_recv[Ether].src != src_mac:
194         raise RuntimeError(
195             f"Received frame has invalid source MAC address: "
196             f"{pkt_recv[Ether].src} should be: {src_mac}"
197         )
198
199     if pkt_recv[Ether].dst != dst_mac:
200         raise RuntimeError(
201             f"Received frame has invalid destination MAC address: "
202             f"{pkt_recv[Ether].dst} should be: {dst_mac}"
203         )
204
205     if not pkt_recv.haslayer(ip_layer):
206         raise RuntimeError(
207             f"Not an {ip_layer.__name__} packet received: {pkt_recv!r}"
208         )
209
210     if pkt_recv[ip_layer].dst != dst_ip:
211         raise RuntimeError(
212             f"Received packet has invalid destination {ip_layer.__name__} "
213             f"address: {pkt_recv[ip_layer.__name__].dst} should be: {dst_ip}"
214         )
215
216     if pkt_recv[ip_layer].src != src_ip:
217         raise RuntimeError(
218             f"Received packet has invalid destination {ip_layer.__name__} "
219             f"address: {pkt_recv[ip_layer.__name__].dst} should be: {src_ip}"
220         )
221
222     if ip_layer == IP and pkt_recv[ip_layer].proto != 61:
223         raise RuntimeError(
224             f"Received packet has invalid IP protocol: "
225             f"{pkt_recv[ip_layer].proto} should be: 61"
226         )
227
228
229 def main():
230     """Send and receive GENEVE packets."""
231
232     args = TrafficScriptArg(
233         [
234             u"tx_src_mac", u"tx_dst_mac", u"rx_src_mac", u"rx_dst_mac",
235             u"tun_local_ip", u"tun_remote_ip", u"tun_vni", u"tun_src_ip",
236             u"tun_dst_ip"
237         ]
238     )
239
240     tx_txq = TxQueue(args.get_arg(u"tx_if"))
241     tx_rxq = RxQueue(args.get_arg(u"tx_if"))
242     rx_txq = TxQueue(args.get_arg(u"rx_if"))
243     rx_rxq = RxQueue(args.get_arg(u"rx_if"))
244
245     rx_src_mac = args.get_arg(u"rx_src_mac")
246     rx_dst_mac = args.get_arg(u"rx_dst_mac")
247     tx_src_mac = args.get_arg(u"tx_src_mac")
248     tx_dst_mac = args.get_arg(u"tx_dst_mac")
249
250     tun_local_ip = args.get_arg(u"tun_local_ip")
251     tun_remote_ip = args.get_arg(u"tun_remote_ip")
252     tun_vni = args.get_arg(u"tun_vni")
253     tun_src_ip = args.get_arg(u"tun_src_ip")
254     tun_dst_ip = args.get_arg(u"tun_dst_ip")
255
256     geneve_tunnel_mac = u"d0:0b:ee:d0:00:00"
257     geneve_udp_dport = 6081
258
259     tx_sent_packets = list()
260     src_ip = ip_address(tun_src_ip)
261     dst_ip = ip_address(tun_dst_ip)
262     ip_layer = IP if src_ip.version == 4 else IPv6
263     tx_ip_pkt = ip_layer(src=src_ip, dst=dst_ip, proto=61) \
264         if ip_layer == IP else ip_layer(src=src_ip, dst=dst_ip)
265     tx_pkt_send = (Ether(src=tx_src_mac, dst=tx_dst_mac) / tx_ip_pkt)
266     tx_pkt_send /= Raw()
267     size_limit = 78 if ip_layer == IPv6 else 60
268     if len(tx_pkt_send) < size_limit:
269         tx_pkt_send[Raw].load += b"\0" * (size_limit - len(tx_pkt_send))
270
271     tx_sent_packets.append(tx_pkt_send)
272     tx_txq.send(tx_pkt_send)
273
274     while True:
275         rx_pkt_recv = rx_rxq.recv(2)
276
277         if rx_pkt_recv is None:
278             raise RuntimeError(f"GENEVE packet Rx timeout")
279
280         if rx_pkt_recv.haslayer(ICMPv6ND_NS):
281             # read another packet in the queue if the current one is ICMPv6ND_NS
282             continue
283         else:
284             # otherwise process the current packet
285             break
286
287     check_geneve(
288         rx_pkt_recv, ip_layer, rx_src_mac, rx_dst_mac, geneve_tunnel_mac,
289         rx_src_mac, tun_local_ip, tun_remote_ip, str(src_ip), str(dst_ip),
290         geneve_udp_dport, int(tun_vni)
291     )
292
293     rx_inner_ip_pkt = IP(
294         src=rx_pkt_recv[Ether:2][IP].dst,
295         dst=rx_pkt_recv[Ether:2][IP].src,
296         proto=61
297     ) if isinstance(rx_pkt_recv[Ether:2][1], IP) else IPv6(
298         src=rx_pkt_recv[Ether:2][IPv6].dst,
299         dst=rx_pkt_recv[Ether:2][IPv6].src
300     )
301     rx_inner_ip_pkt /= Raw()
302     rx_inner_pkt = (
303         Ether(src=rx_pkt_recv[4].dst, dst=rx_pkt_recv[4].src) /
304         rx_inner_ip_pkt
305     )
306     size_limit = 78 if isinstance(rx_pkt_recv[Ether:2][1], IPv6) else 60
307     if len(rx_inner_pkt) < size_limit:
308         rx_inner_pkt[Raw].load += b"\0" * (size_limit - len(rx_inner_pkt))
309
310     rx_outer_ip_pkt = IP(
311         src=rx_pkt_recv[Ether:1][IP].dst,
312         dst=rx_pkt_recv[Ether:1][IP].src
313     ) if isinstance(rx_pkt_recv[Ether:1][1], IP) else IPv6(
314         src=rx_pkt_recv[Ether:1][IPv6].dst,
315         dst=rx_pkt_recv[Ether:1][IPv6].src
316     )
317     rx_pkt_send = (
318         Ether(src=rx_pkt_recv[Ether:1].dst, dst=rx_pkt_recv[Ether:1].src) /
319         rx_outer_ip_pkt /
320         UDP(sport=6081, dport=6081) /
321         GENEVE(vni=rx_pkt_recv[GENEVE].vni) /
322         rx_inner_pkt
323     )
324     rx_txq.send(rx_pkt_send)
325
326     while True:
327         tx_pkt_recv = tx_rxq.recv(2, ignore=tx_sent_packets)
328         ip_layer = IP
329
330         if tx_pkt_recv is None:
331             raise RuntimeError(f"{ip_layer.__name__} packet Rx timeout")
332
333         if tx_pkt_recv.haslayer(ICMPv6ND_NS):
334             # read another packet in the queue if the current one is ICMPv6ND_NS
335             continue
336         else:
337             # otherwise process the current packet
338             break
339
340     check_ip(
341         tx_pkt_recv, ip_layer, tx_dst_mac, tx_src_mac, str(dst_ip), str(src_ip)
342     )
343
344     sys.exit(0)
345
346
347 if __name__ == u"__main__":
348     main()