Improved PROX cleanup when exiting
[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, resultsdir, machine_params,
53             configonly, ipv6):
54         mac_address_size = 6
55         ethertype_size = 2
56         FCS_size = 4
57         if ipv6:
58             ip_header_size = 40
59             self.ip_length_offset = 18
60             # In IPV6, the IP size is the size of the IP content
61             self.frame_size_minus_ip_size = (2 * mac_address_size +
62                     ethertype_size + ip_header_size + FCS_size)
63         else:
64             ip_header_size = 20
65             self.ip_length_offset = 16
66             # In IPV4, the IP size is the size of the IP header + IP content
67             self.frame_size_minus_ip_size = (2 * mac_address_size +
68                     ethertype_size + FCS_size)
69         self.frame_size_minus_udp_header_and_content = (2 * mac_address_size +
70                 ethertype_size + ip_header_size + FCS_size )
71         udp_header_start_offset = (2 * mac_address_size + ethertype_size +
72                 ip_header_size)
73         self.udp_source_port_offset = udp_header_start_offset 
74         self.udp_dest_port_offset = udp_header_start_offset + 2
75         self.udp_length_offset = udp_header_start_offset + 4
76         self.ipv6 = ipv6
77         super().__init__(key, user, vim, rundir, resultsdir, machine_params,
78                 configonly)
79
80     def get_cores(self):
81         return (self.machine_params['gencores'] +
82                 self.machine_params['latcores'])
83
84     def generate_lua(self, vim):
85         appendix = 'gencores="%s"\n'% ','.join(map(str,
86             self.machine_params['gencores']))
87         appendix = appendix + 'latcores="%s"\n'% ','.join(map(str,
88             self.machine_params['latcores']))
89         if 'gw_vm' in self.machine_params.keys():
90             for index, gw_ip in enumerate(self.machine_params['gw_ips'],
91                     start = 1):
92                 appendix = appendix + 'gw_ip{}="{}"\n'.format(index, gw_ip)
93                 appendix = (appendix + 'gw_hex_ip{}=convertIPToHex(gw_ip{})\n'.
94                         format(index, index))
95         if 'bucket_size_exp' in self.machine_params.keys():
96             self.bucket_size_exp = self.machine_params['bucket_size_exp']
97         else:
98             self.bucket_size_exp = 11
99         appendix = (appendix +
100                 'bucket_size_exp="{}"\n'.format(self.bucket_size_exp))
101         if 'heartbeat' in self.machine_params.keys():
102             appendix = (appendix +
103                     'heartbeat="%s"\n'% self.machine_params['heartbeat'])
104         else:
105             appendix = appendix + 'heartbeat="60"\n'
106         super().generate_lua(vim, appendix)
107
108     def start_prox(self):
109         # Start the generator with the -e option so that the cores don't
110         # start automatically
111         super().start_prox('-e')
112
113     def set_generator_speed(self, speed):
114         # The assumption is that we only use task 0 for generating
115         # We should check the gen.cfg file to make sure there is only task=0
116         speed_per_gen_core = speed / len(self.machine_params['gencores']) 
117         self.socket.speed(speed_per_gen_core, self.machine_params['gencores'])
118
119     def set_udp_packet_size(self, imix_frame_sizes):
120         # We should check the gen.cfg to make sure we only send UDP packets
121         # If only 1 packet size, still using the 'old' way of setting the 
122         # packet sizes in PROX. Otherwise, using the 'new' way which
123         # automatically sets IP and UDP sizes. We should switch to the new way
124         # eventually for all cases.
125         if len(imix_frame_sizes) == 1:
126             # Frame size = PROX pkt size + 4 bytes CRC
127             # The set_size function takes the PROX packet size as a parameter
128             self.socket.set_size(self.machine_params['gencores'], 0,
129                     imix_frame_sizes[0] - 4)
130             # Writing length in the ip header
131             self.socket.set_value(self.machine_params['gencores'], 0,
132                     self.ip_length_offset, imix_frame_sizes[0] - 
133                     self.frame_size_minus_ip_size, 2)
134             # Writing length in the udp header
135             self.socket.set_value(self.machine_params['gencores'], 0,
136                     self.udp_length_offset, imix_frame_sizes[0] -
137                     self.frame_size_minus_udp_header_and_content, 2)
138         else:
139             if self.ipv6:
140                 RapidLog.critical('IMIX not supported for IPV6')
141             prox_sizes = [frame_size - 4 for frame_size in imix_frame_sizes]
142             self.socket.set_imix(self.machine_params['gencores'], 0,
143                     prox_sizes)
144
145     def set_flows(self, number_of_flows):
146         source_port, destination_port, actualflows = RandomPortBits.get_bitmap(
147                 number_of_flows)
148         self.socket.set_random(self.machine_params['gencores'],0,
149                 self.udp_source_port_offset, source_port,2)
150         self.socket.set_random(self.machine_params['gencores'],0,
151                 self.udp_dest_port_offset, destination_port,2)
152         return actualflows
153
154     def start_gen_cores(self):
155         self.socket.start(self.machine_params['gencores'])
156
157     def stop_gen_cores(self):
158         self.socket.stop(self.machine_params['gencores'])
159
160     def start_latency_cores(self):
161         self.socket.start(self.machine_params['latcores'])
162
163     def stop_latency_cores(self):
164         self.socket.stop(self.machine_params['latcores'])
165
166     def lat_stats(self):
167         # Checking all tasks in the cfg file. In this way, we can have more
168         # latency tasks on the same core
169         return (self.socket.lat_stats(self.machine_params['latcores'],
170                 self.all_tasks_for_this_cfg))