summaryrefslogtreecommitdiffstats
path: root/test/vpp_ikev2.py
AgeCommit message (Expand)AuthorFilesLines
2021-05-13tests: move test source to vpp/testDave Wallace1-0/+179
er-rls2306-230710'>oper-rls2306-230710 Integration testsGrokmirror user
aboutsummaryrefslogtreecommitdiffstats
path: root/GPL/traffic_profiles/trex/profile_trex_stateless_base_class.py
blob: 5b86192021cb142ca8e4696781e25e2ce02d218a (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
# Copyright (c) 2021 Cisco and/or its affiliates.
#
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
#
# Licensed under the Apache License 2.0 or
# GNU General Public License v2.0 or later;  you may not use this file
# except in compliance with one of these Licenses. You
# may obtain a copy of the Licenses at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#     https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
#
# Note: If this file is linked with Scapy, which is GPLv2+, your use of it
# must be under GPLv2+.  If at any point in the future it is no longer linked
# with Scapy (or other GPLv2+ licensed software), you are free to choose
# Apache 2.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Base class for stream profiles for T-rex traffic generator.
"""

import socket
import struct

from random import choice
from string import ascii_letters

from trex.stl.api import *


class TrafficStreamsBaseClass:
    """Base class for stream profiles for T-rex traffic generator."""

    STREAM_TABLE = {
        u"IMIX_v4": [
            {u"size": 60, u"pps": 28, u"isg": 0},
            {u"size": 590, u"pps": 20, u"isg": 0.1},
            {u"size": 1514, u"pps": 4, u"isg": 0.2}
        ],
        'IMIX_v4_1': [
            {u"size": 64, u"pps": 28, u"isg": 0},
            {u"size": 570, u"pps": 16, u"isg": 0.1},
            {u"size": 1518, u"pps": 4, u"isg": 0.2}
        ]
    }

    def __init__(self):
        # Default value of frame size, it will be overwritten by the value of
        # "framesize" parameter of "get_streams" method.
        self.framesize = 64

        # If needed, add your own parameters.

    def _gen_payload(self, length):
        """Generate payload.

        If needed, implement your own algorithm.

        :param length: Length of generated payload.
        :type length: int
        :returns: The generated payload.
        :rtype: str
        """
        payload = u""
        for _ in range(length):
            payload += choice(ascii_letters)

        return payload

    def _get_start_end_ipv6(self, start_ip, end_ip):
        """Get start host and number of hosts from IPv6 as integer.

        :param start_ip: Start IPv6.
        :param end_ip: End IPv6.
        :type start_ip: string
        :type end_ip: string
        :return: Start host, number of hosts.
        :rtype tuple of int
        :raises: ValueError if start_ip is greater then end_ip.
        :raises: socket.error if the IP addresses are not valid IPv6 addresses.
        """
        try:
            ip1 = socket.inet_pton(socket.AF_INET6, start_ip)
            ip2 = socket.inet_pton(socket.AF_INET6, end_ip)

            hi1, lo1 = struct.unpack(u"!QQ", ip1)
            hi2, lo2 = struct.unpack(u"!QQ", ip2)

            if ((hi1 << 64) | lo1) > ((hi2 << 64) | lo2):
                raise ValueError(u"IPv6: start_ip is greater then end_ip")

            return lo1, abs(int(lo1) - int(lo2))

        except socket.error as err:
            print(err)
            raise

    def define_packets(self):
        """Define the packets to be sent from the traffic generator.

        This method MUST return:

            return base_pkt_a, base_pkt_b, vm1, vm2

            vm1 and vm2 CAN be None.

        :returns: Packets to be sent from the traffic generator.
        :rtype: tuple
        """
        raise NotImplementedError

    def create_streams(self):
        """Create traffic streams.

        Implement your own traffic streams.

        :returns: Traffic streams.
        :rtype: list
        """
        base_pkt_a, base_pkt_b, vm1, vm2 = self.define_packets()

        # In most cases you will not have to change the code below:

        # Frame size is defined as an integer, e.g. 64, 1518:
        if isinstance(self.framesize, int):
            # Create a base packet and pad it to size; deduct FCS
            payload_len_a = max(0, self.framesize - len(base_pkt_a) - 4)
            payload_len_b = max(0, self.framesize - len(base_pkt_b) - 4)

            # Direction 0 --> 1
            pkt_a = STLPktBuilder(
                pkt=base_pkt_a / self._gen_payload(payload_len_a), vm=vm1
            )
            # Direction 1 --> 0
            pkt_b = STLPktBuilder(
                pkt=base_pkt_b / self._gen_payload(payload_len_b), vm=vm2
            )

            # Packets for latency measurement:
            # Direction 0 --> 1
            pkt_lat_a = STLPktBuilder(
                pkt=base_pkt_a / self._gen_payload(payload_len_a), vm=vm1
            )
            # Direction 1 --> 0
            pkt_lat_b = STLPktBuilder(
                pkt=base_pkt_b / self._gen_payload(payload_len_b), vm=vm2
            )

            # Create the streams:
            # Direction 0 --> 1
            stream1 = STLStream(
                packet=pkt_a, mode=STLTXCont(pps=9000)
            )
            # Direction 1 --> 0
            # second traffic stream with a phase of 10ns (inter-stream gap)
            stream2 = STLStream(
                packet=pkt_b, isg=10.0, mode=STLTXCont(pps=9000)
            )

            # Streams for latency measurement:
            # Direction 0 --> 1
            lat_stream1 = STLStream(
                packet=pkt_lat_a,
                flow_stats=STLFlowLatencyStats(pg_id=0),
                mode=STLTXCont(pps=9000)
            )
            # Direction 1 --> 0
            # second traffic stream with a phase of 10ns (inter-stream gap)
            lat_stream2 = STLStream(
                packet=pkt_lat_b,
                isg=10.0,
                flow_stats=STLFlowLatencyStats(pg_id=1),
                mode=STLTXCont(pps=9000)
            )

            return [stream1, stream2, lat_stream1, lat_stream2]

        # Frame size is defined as a string, e.g.IMIX_v4_1:
        elif isinstance(self.framesize, str):

            stream1 = list()
            stream2 = list()

            for stream in self.STREAM_TABLE[self.framesize]:
                payload_len_a = max(0, stream[u"size"] - len(base_pkt_a) - 4)
                payload_len_b = max(0, stream[u"size"] - len(base_pkt_b) - 4)
                # Create a base packet and pad it to size
                pkt_a = STLPktBuilder(
                    pkt=base_pkt_a / self._gen_payload(payload_len_a),
                    vm=vm1
                )
                pkt_b = STLPktBuilder(
                    pkt=base_pkt_b / self._gen_payload(payload_len_b),
                    vm=vm2
                )

                # Create the streams:
                stream1.append(STLStream(
                    packet=pkt_a,
                    isg=stream[u"isg"],
                    mode=STLTXCont(pps=stream[u"pps"]))
                )
                stream2.append(STLStream(
                    packet=pkt_b,
                    isg=stream[u"isg"],
                    mode=STLTXCont(pps=stream[u"pps"]))
                )
            streams = list()
            streams.extend(stream1)
            streams.extend(stream2)

            return streams

    def get_streams(self, **kwargs):
        """Get traffic streams created by "create_streams" method.

        If needed, add your own parameters.

        :param kwargs: Key-value pairs used by "create_streams" method while
        creating streams.
        :returns: Traffic streams.
        :rtype: list
        """
        self.framesize = kwargs[u"framesize"]
        self.rate = kwargs[u"rate"]

        return self.create_streams()