1 # Copyright (c) 2016-2017 Intel Corporation
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """ PING acts as traffic generation and vnf definitions based on IETS Spec """
16 from __future__ import absolute_import
17 from __future__ import print_function
21 from multiprocessing import Queue
22 from ipaddress import IPv4Interface
24 from yardstick.network_services.vnf_generic.vnf.sample_vnf import SampleVNFTrafficGen
25 from yardstick.network_services.vnf_generic.vnf.sample_vnf import DpdkVnfSetupEnvHelper
27 LOG = logging.getLogger(__name__)
30 class PingParser(object):
31 """ Class providing file-like API for talking with SSH connection """
33 def __init__(self, q_out):
37 def write(self, chunk):
38 """ 64 bytes from 10.102.22.93: icmp_seq=1 ttl=64 time=0.296 ms """
39 match = re.search(r"icmp_seq=(\d+).*time=([0-9.]+)", chunk)
40 LOG.debug("Parser called on %s", chunk)
42 # IMPORTANT: in order for the data to be properly taken
43 # in by InfluxDB, it needs to be converted to numeric types
45 "packets_received": float(match.group(1)),
46 "rtt": float(match.group(2)),
50 """ close the ssh connection """
54 """ clear queue till Empty """
55 while self.queue.qsize() > 0:
59 class PingSetupEnvHelper(DpdkVnfSetupEnvHelper):
61 def setup_vnf_environment(self):
62 self._bind_kernel_devices()
65 class PingTrafficGen(SampleVNFTrafficGen):
67 This traffic generator can ping a single IP with pingsize
68 and target given in traffic profile
74 def __init__(self, name, vnfd, setup_env_helper_type=None, resource_helper_type=None):
75 if setup_env_helper_type is None:
76 setup_env_helper_type = PingSetupEnvHelper
78 super(PingTrafficGen, self).__init__(name, vnfd, setup_env_helper_type,
81 self._parser = PingParser(self._queue)
84 def scale(self, flavor=""):
85 """ scale vnf-based on flavor input """
88 def _check_status(self):
89 return self._tg_process.is_alive()
91 def instantiate(self, scenario_cfg, context_cfg):
93 "packets_received": 0,
96 self.setup_helper.setup_vnf_environment()
98 def listen_traffic(self, traffic_profile):
99 """ Not needed for ping
101 :param traffic_profile:
106 def _traffic_runner(self, traffic_profile):
107 intf = self.vnfd_helper.interfaces[0]["virtual-interface"]
108 profile = traffic_profile.params["traffic_profile"]
110 'target_ip': IPv4Interface(intf["dst_ip"]).ip.exploded,
111 'local_ip': IPv4Interface(intf["local_ip"]).ip.exploded,
112 'local_if_name': intf["local_iface_name"].split('/')[0],
113 'packet_size': profile["frame_size"],
117 "sudo ip addr flush {local_if_name}",
118 "sudo ip addr add {local_ip}/24 dev {local_if_name}",
119 "sudo ip link set {local_if_name} up",
123 self.ssh_helper.execute(cmd.format(**cmd_kwargs))
125 ping_cmd = "ping -s {packet_size} {target_ip}"
126 self.ssh_helper.run(ping_cmd.format(**cmd_kwargs),
128 keep_stdin_open=True, pty=True)