fix: IPV6 packet generation, packet loss reporting
[samplevnf.git] / VNFs / DPPD-PROX / helper-scripts / rapid / rapid_generator_machine.py
1 #!/usr/bin/python
2
3 ##
4 ## Copyright (c) 2020 Intel Corporation
5 ##
6 ## Licensed under the Apache License, Version 2.0 (the "License");
7 ## you may not use this file except in compliance with the License.
8 ## You may obtain a copy of the License at
9 ##
10 ##     http://www.apache.org/licenses/LICENSE-2.0
11 ##
12 ## Unless required by applicable law or agreed to in writing, software
13 ## distributed under the License is distributed on an "AS IS" BASIS,
14 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ## See the License for the specific language governing permissions and
16 ## limitations under the License.
17 ##
18
19 from rapid_log import RapidLog 
20 from prox_ctrl import prox_ctrl
21 from rapid_machine import RapidMachine
22 from math import ceil, log2
23
24
25 class RandomPortBits(object):
26     """
27     Class to generate PROX bitmaps for random bit generation
28     in source & dst UPD ports to emulate mutiple flows
29     """
30     @staticmethod
31     def get_bitmap(flow_number):
32         number_of_random_bits = ceil(log2(flow_number))
33         if number_of_random_bits > 30:
34             raise Exception("Not able to support that many flows")
35             # throw exeption since we need the first bit to be 1
36             # Otherwise, the randomization could results in all 0's
37             # and that might be an invalid UDP port and result in 
38             # packets being discarded
39         src_number_of_random_bits = number_of_random_bits // 2
40         dst_number_of_random_bits = (number_of_random_bits -
41                 src_number_of_random_bits)
42         src_port_bitmap = '1000000000000000'.replace ('0','X',
43                 src_number_of_random_bits)
44         dst_port_bitmap = '1000000000000000'.replace ('0','X',
45                 dst_number_of_random_bits)
46         return [src_port_bitmap, dst_port_bitmap, 1 << number_of_random_bits]
47
48 class RapidGeneratorMachine(RapidMachine):
49     """
50     Class to deal with a generator PROX instance (VM, bare metal, container)
51     """
52     def __init__(self, key, user, vim, rundir, machine_params, ipv6):
53         mac_address_size = 6
54         ethertype_size = 2
55         FCS_size = 4
56         if ipv6:
57             ip_header_size = 40
58             self.ip_length_offset = 18
59             # In IPV6, the IP size is the size of the IP content
60             self.frame_size_minus_ip_size = (2 * mac_address_size +
61                     ethertype_size + ip_header_size + FCS_size)
62         else:
63             ip_header_size = 20
64             self.ip_length_offset = 16
65             # In IPV4, the IP size is the size of the IP header + IP content
66             self.frame_size_minus_ip_size = (2 * mac_address_size +
67                     ethertype_size + FCS_size)
68         self.frame_size_minus_udp_header_and_content = (2 * mac_address_size +
69                 ethertype_size + ip_header_size + FCS_size )
70         udp_header_start_offset = (2 * mac_address_size + ethertype_size +
71                 ip_header_size)
72         self.udp_source_port_offset = udp_header_start_offset 
73         self.udp_dest_port_offset = udp_header_start_offset + 2
74         self.udp_length_offset = udp_header_start_offset + 4
75         self.ipv6 = ipv6
76         super().__init__(key, user, vim, rundir, machine_params)
77
78     def get_cores(self):
79         return (self.machine_params['gencores'] +
80                 self.machine_params['latcores'])
81
82     def generate_lua(self, vim):
83         appendix = 'gencores="%s"\n'% ','.join(map(str,
84             self.machine_params['gencores']))
85         appendix = appendix + 'latcores="%s"\n'% ','.join(map(str,
86             self.machine_params['latcores']))
87         if 'gw_vm' in self.machine_params.keys():
88             for index, gw_ip in enumerate(self.machine_params['gw_ips'],
89                     start = 1):
90                 appendix = appendix + 'gw_ip{}="{}"\n'.format(index, gw_ip)
91                 appendix = (appendix + 'gw_hex_ip{}=convertIPToHex(gw_ip{})\n'.
92                         format(index, index))
93         if 'bucket_size_exp' in self.machine_params.keys():
94             self.bucket_size_exp = self.machine_params['bucket_size_exp']
95         else:
96             self.bucket_size_exp = 11
97         appendix = (appendix +
98                 'bucket_size_exp="{}"\n'.format(self.bucket_size_exp))
99         if 'heartbeat' in self.machine_params.keys():
100             appendix = (appendix +
101                     'heartbeat="%s"\n'% self.machine_params['heartbeat'])
102         else:
103             appendix = appendix + 'heartbeat="60"\n'
104         super().generate_lua(vim, appendix)
105
106     def start_prox(self):
107         # Start the generator with the -e option so that the cores don't
108         # start automatically
109         super().start_prox('-e')
110
111     def set_generator_speed(self, speed):
112         # The assumption is that we only use task 0 for generating
113         # We should check the gen.cfg file to make sure there is only task=0
114         speed_per_gen_core = speed / len(self.machine_params['gencores']) 
115         self.socket.speed(speed_per_gen_core, self.machine_params['gencores'])
116
117     def set_udp_packet_size(self, imix_frame_sizes):
118         # We should check the gen.cfg to make sure we only send UDP packets
119         # If only 1 packet size, still using the 'old' way of setting the 
120         # packet sizes in PROX. Otherwise, using the 'new' way which
121         # automatically sets IP and UDP sizes. We should switch to the new way
122         # eventually for all cases.
123         if len(imix_frame_sizes) == 1:
124             # Frame size = PROX pkt size + 4 bytes CRC
125             # The set_size function takes the PROX packet size as a parameter
126             self.socket.set_size(self.machine_params['gencores'], 0,
127                     imix_frame_sizes[0] - 4)
128             # Writing length in the ip header
129             self.socket.set_value(self.machine_params['gencores'], 0,
130                     self.ip_length_offset, imix_frame_sizes[0] - 
131                     self.frame_size_minus_ip_size, 2)
132             # Writing length in the udp header
133             self.socket.set_value(self.machine_params['gencores'], 0,
134                     self.udp_length_offset, imix_frame_sizes[0] -
135                     self.frame_size_minus_udp_header_and_content, 2)
136         else:
137             if self.ipv6:
138                 RapidLog.critical('IMIX not supported for IPV6')
139             prox_sizes = [frame_size - 4 for frame_size in imix_frame_sizes]
140             self.socket.set_imix(self.machine_params['gencores'], 0,
141                     prox_sizes)
142
143     def set_flows(self, number_of_flows):
144         source_port, destination_port, actualflows = RandomPortBits.get_bitmap(
145                 number_of_flows)
146         self.socket.set_random(self.machine_params['gencores'],0,
147                 self.udp_source_port_offset, source_port,2)
148         self.socket.set_random(self.machine_params['gencores'],0,
149                 self.udp_dest_port_offset, destination_port,2)
150         return actualflows
151
152     def start_gen_cores(self):
153         self.socket.start(self.machine_params['gencores'])
154
155     def stop_gen_cores(self):
156         self.socket.stop(self.machine_params['gencores'])
157
158     def start_latency_cores(self):
159         self.socket.start(self.machine_params['latcores'])
160
161     def stop_latency_cores(self):
162         self.socket.stop(self.machine_params['latcores'])
163
164     def lat_stats(self):
165         # Checking all tasks in the cfg file. In this way, we can have more
166         # latency tasks on the same core
167         return (self.socket.lat_stats(self.machine_params['latcores'],
168                 self.all_tasks_for_this_cfg))