854e32796629804853f08b1df643dcea524f47d4
[csit.git] / resources / traffic_profiles / trex / trex-sl-dot1qip4vxlan-ip4src10udpsrcrnd.py
1 # Copyright (c) 2019 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """Stream profile for T-rex traffic generator.
15
16 Stream profile:
17  - Two streams sent in directions 0 --> 1 and 1 --> 0 at the same time.
18  - Packet: ETH / DOT1Q / IP / VXLAN / ETH / IP
19  - Direction 0 --> 1:
20    - VLAN range:                       100
21    - Source IP address:                172.17.[0..9].2
22    - Destination IP address:           172.16.0.1
23    - Source UDP port:                  random([1024..65535])
24    - Destination UDP port:             4789
25    - VXLAN VNI:                        [0..9]
26    - Payload source MAC address:       00:aa:aa:00:00:[00..ff]
27    - Payload source IP address:        10.0.[0..255].2
28    - Payload destination MAC address:  00:bb:bb:00:00:[00..ff]
29    - Payload destination IP address:   10.0.[0..255].1
30  - Direction 1 --> 0:
31    - VLAN range:                       200
32    - Source IP address:                172.27.[0..9].2
33    - Destination IP address:           172.26.0.1
34    - Source UDP port:                  random([1024..65535])
35    - Destination UDP port:             4789
36    - VXLAN VNI:                        [0..9]
37    - Payload source MAC address:       00:bb:bb:00:00:[00..ff]
38    - Payload source IP address:        10.0.[0..255].1
39    - Payload destination MAC address:  00:aa:aa:00:00:[00..ff]
40    - Payload destination IP address:   10.0.[0..255].2
41 """
42
43 from trex.stl.api import *
44 from profile_trex_stateless_base_class import TrafficStreamsBaseClass
45
46 # RFC 7348 - Virtual eXtensible Local Area Network (VXLAN):
47 # A Framework for Overlaying Virtualized Layer 2 Networks over Layer 3 Networks
48 # http://tools.ietf.org/html/rfc7348
49 _VXLAN_FLAGS = list('R'*24 + "RRRIRRRRR")
50
51
52 class VXLAN(Packet):
53     name = 'VXLAN'
54     fields_desc = [FlagsField('flags', 0x08000000, 32, _VXLAN_FLAGS),
55                    ThreeBytesField('vni', 0),
56                    XByteField('reserved', 0x00)]
57
58     def mysummary(self):
59         return self.sprintf("VXLAN (vni=%VXLAN.vni%)")
60
61
62 bind_layers(UDP, VXLAN, dport=4789)
63 bind_layers(VXLAN, Ether)
64
65
66 class TrafficStreams(TrafficStreamsBaseClass):
67     """Stream profile."""
68
69     def __init__(self):
70         """Initialization and setting of streams' parameters."""
71
72         super(TrafficStreamsBaseClass, self).__init__()
73
74         self.nf_chains = 10
75
76     def define_packets(self):
77         """Defines the packets to be sent from the traffic generator.
78
79         Packet definition: | ETH | DOT1Q | IP | VXLAN | ETH | IP
80
81         :returns: Packets to be sent from the traffic generator.
82         :rtype: tuple
83         """
84
85         # Direction 0 --> 1
86         base_pkt_a = (
87             Ether()/
88             Dot1Q(vlan=100) /
89             IP(src='172.17.0.2', dst='172.16.0.1')/
90             UDP(sport=1024, dport=4789)/
91             VXLAN(vni=0)/
92             Ether(src='00:aa:aa:00:00:00', dst='00:bb:bb:00:00:00')/
93             IP(src='10.0.0.2', dst='10.0.0.1', proto=61))
94
95         # Direction 1 --> 0
96         base_pkt_b = (
97             Ether()/
98             Dot1Q(vlan=200) /
99             IP(src='172.27.0.2', dst='172.26.0.1')/
100             UDP(sport=1024, dport=4789)/
101             VXLAN(vni=0)/
102             Ether(src='00:bb:bb:00:00:00', dst='00:aa:aa:00:00:00')/
103             IP(src='10.0.0.1', dst='10.0.0.2', proto=61))
104
105         # Direction 0 --> 1
106         vm1 = STLScVmRaw([
107             STLVmFlowVar(name='nf_id', size=1, op='inc',
108                          min_value=0, max_value=self.nf_chains - 1),
109             STLVmFlowVar(name='in_mac', size=2, op='inc',
110                          min_value=0, max_value=255),
111             STLVmFlowVar(name='in_ip', size=1, op='inc',
112                          min_value=0, max_value=255),
113             STLVmFlowVar(name='src_port', size=2, op='random',
114                          min_value=1024, max_value=65535),
115             STLVmWrFlowVar(fv_name='nf_id', pkt_offset=32),
116             STLVmWrFlowVar(fv_name='src_port', pkt_offset='UDP.sport'),
117             STLVmWrFlowVar(fv_name='nf_id', pkt_offset=52),
118             STLVmWrFlowVar(fv_name='in_mac', pkt_offset=58),
119             STLVmWrFlowVar(fv_name='in_mac', pkt_offset=64),
120             STLVmWrFlowVar(fv_name='in_ip', pkt_offset=82),
121             STLVmWrFlowVar(fv_name='in_ip', pkt_offset=86),
122             STLVmFixIpv4(offset = 'IP')
123         ])
124
125         # Direction 1 --> 0
126         vm2 = STLScVmRaw([
127             STLVmFlowVar(name='nf_id', size=1, op='inc',
128                          min_value=0, max_value=self.nf_chains - 1),
129             STLVmFlowVar(name='in_mac', size=2, op='inc',
130                          min_value=0, max_value=255),
131             STLVmFlowVar(name='in_ip', size=1, op='inc',
132                          min_value=0, max_value=255),
133             STLVmFlowVar(name='src_port', size=2, op='random',
134                          min_value=1024, max_value=65535),
135             STLVmWrFlowVar(fv_name='nf_id', pkt_offset=32),
136             STLVmWrFlowVar(fv_name='src_port', pkt_offset='UDP.sport'),
137             STLVmWrFlowVar(fv_name='nf_id', pkt_offset=52),
138             STLVmWrFlowVar(fv_name='in_mac', pkt_offset=58),
139             STLVmWrFlowVar(fv_name='in_mac', pkt_offset=64),
140             STLVmWrFlowVar(fv_name='in_ip', pkt_offset=82),
141             STLVmWrFlowVar(fv_name='in_ip', pkt_offset=86),
142             STLVmFixIpv4(offset = 'IP')
143         ])
144
145         return base_pkt_a, base_pkt_b, vm1, vm2
146
147 def register():
148     """Register this traffic profile to T-rex.
149
150     Do not change this function.
151
152     :return: Traffic streams.
153     :rtype: Object
154     """
155     return TrafficStreams()
156