1 # Copyright (c) 2016-2019 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.
17 from itertools import chain
26 from yardstick.benchmark.contexts import base as context_base
27 from yardstick.benchmark.scenarios import base as scenario_base
28 from yardstick.common.constants import LOG_DIR
29 from yardstick.common import exceptions
30 from yardstick.common.process import terminate_children
31 from yardstick.common import utils
32 from yardstick.network_services.collector.subscriber import Collector
33 from yardstick.network_services.vnf_generic import vnfdgen
34 from yardstick.network_services.vnf_generic.vnf.base import GenericVNF
35 from yardstick.network_services import traffic_profile
36 from yardstick.network_services.traffic_profile import base as tprofile_base
37 from yardstick.network_services.utils import get_nsb_option
38 from yardstick import ssh
41 traffic_profile.register_modules()
44 LOG = logging.getLogger(__name__)
47 class NetworkServiceBase(scenario_base.Scenario):
48 """Base class for Network service testing scenarios"""
50 __scenario_type__ = ""
52 def __init__(self, scenario_cfg, context_cfg): # pragma: no cover
53 super(NetworkServiceBase, self).__init__()
54 self.scenario_cfg = scenario_cfg
55 self.context_cfg = context_cfg
57 self._render_topology()
60 self.traffic_profile = None
61 self.node_netdevs = {}
62 self.bin_path = get_nsb_option('bin_path', '')
68 """ Stop the collector and terminate VNF & TG instance
77 LOG.info("Stopping %s", vnf.name)
79 LOG.debug("all VNFs terminated: %s", ", ".join(vnf.name for vnf in self.vnfs))
83 # catch any exception in teardown and convert to simple exception
84 # never pass exceptions back to multiprocessing, because some exceptions can
86 # https://bugs.python.org/issue9400
88 raise RuntimeError("Error in teardown")
91 return self.traffic_profile is not None and self.traffic_profile.is_ended()
93 def _get_ip_flow_range(self, ip_start_range):
94 """Retrieve a CIDR first and last viable IPs
96 :param ip_start_range: could be the IP range itself or a dictionary
97 with the host name and the port.
98 :return: (str) IP range (min, max) with this format "x.x.x.x-y.y.y.y"
100 if isinstance(ip_start_range, six.string_types):
101 return ip_start_range
103 node_name, range_or_interface = next(iter(ip_start_range.items()),
105 if node_name is None:
106 return range_or_interface
108 node = self.context_cfg['nodes'].get(node_name, {})
109 interface = node.get('interfaces', {}).get(range_or_interface)
111 ip = interface['local_ip']
112 mask = interface['netmask']
115 mask = '255.255.255.0'
117 ipaddr = ipaddress.ip_network(
118 six.text_type('{}/{}'.format(ip, mask)), strict=False)
119 if ipaddr.prefixlen + 2 < ipaddr.max_prefixlen:
120 ip_addr_range = '{}-{}'.format(ipaddr[2], ipaddr[-2])
122 LOG.warning('Only single IP in range %s', ipaddr)
126 def _get_traffic_flow(self):
129 # TODO: should be .0 or .1 so we can use list
130 # but this also roughly matches uplink_0, downlink_0
131 fflow = self.scenario_cfg["options"]["flow"]
132 for index, src in enumerate(fflow.get("src_ip", [])):
133 flow["src_ip_{}".format(index)] = self._get_ip_flow_range(src)
135 for index, dst in enumerate(fflow.get("dst_ip", [])):
136 flow["dst_ip_{}".format(index)] = self._get_ip_flow_range(dst)
138 for index, publicip in enumerate(fflow.get("public_ip", [])):
139 flow["public_ip_{}".format(index)] = publicip
141 for index, src_port in enumerate(fflow.get("src_port", [])):
142 flow["src_port_{}".format(index)] = src_port
144 for index, dst_port in enumerate(fflow.get("dst_port", [])):
145 flow["dst_port_{}".format(index)] = dst_port
148 flow["count"] = fflow["count"]
150 if "srcseed" in fflow:
151 flow["srcseed"] = fflow["srcseed"]
153 if "dstseed" in fflow:
154 flow["dstseed"] = fflow["dstseed"]
158 return {"flow": flow}
160 def _get_traffic_imix(self):
162 imix = {"imix": self.scenario_cfg['options']['framesize']}
167 def _get_ip_priority(self):
169 priority = self.scenario_cfg['options']['priority']
174 def _get_traffic_profile(self):
175 profile = self.scenario_cfg["traffic_profile"]
176 path = self.scenario_cfg["task_path"]
177 with utils.open_relative_file(profile, path) as infile:
180 def _get_duration(self):
181 options = self.scenario_cfg.get('options', {})
182 return options.get('duration',
183 tprofile_base.TrafficProfileConfig.DEFAULT_DURATION)
185 def _key_list_to_dict(self, key, value_list):
188 for index, count in enumerate(value_list[key]):
189 value_dict["{}_{}".format(key, index)] = count
195 def _get_simulated_users(self):
196 users = self.scenario_cfg.get("options", {}).get("simulated_users", {})
197 simulated_users = self._key_list_to_dict("uplink", users)
198 return {"simulated_users": simulated_users}
200 def _get_page_object(self):
201 objects = self.scenario_cfg.get("options", {}).get("page_object", {})
202 page_object = self._key_list_to_dict("uplink", objects)
203 return {"page_object": page_object}
205 def _fill_traffic_profile(self):
206 tprofile = self._get_traffic_profile()
207 extra_args = self.scenario_cfg.get('extra_args', {})
209 'flow': self._get_traffic_flow(),
210 'imix': self._get_traffic_imix(),
211 'priority': self._get_ip_priority(),
212 tprofile_base.TrafficProfile.UPLINK: {},
213 tprofile_base.TrafficProfile.DOWNLINK: {},
214 'extra_args': extra_args,
215 'duration': self._get_duration(),
216 'page_object': self._get_page_object(),
217 'simulated_users': self._get_simulated_users()}
218 traffic_vnfd = vnfdgen.generate_vnfd(tprofile, tprofile_data)
221 self.scenario_cfg.get("options", {}).get("traffic_config", {})
223 traffic_vnfd.setdefault("traffic_profile", {})
224 traffic_vnfd["traffic_profile"].update(traffic_config)
226 self.traffic_profile = \
227 tprofile_base.TrafficProfile.get(traffic_vnfd)
229 def _get_topology(self):
230 topology = self.scenario_cfg["topology"]
231 path = self.scenario_cfg["task_path"]
232 with utils.open_relative_file(topology, path) as infile:
235 def _render_topology(self):
236 topology = self._get_topology()
237 topology_args = self.scenario_cfg.get('extra_args', {})
239 'extra_args': topology_args
241 topology_yaml = vnfdgen.generate_vnfd(topology, topolgy_data)
242 self.topology = topology_yaml["nsd:nsd-catalog"]["nsd"][0]
244 def _find_vnf_name_from_id(self, vnf_id): # pragma: no cover
245 return next((vnfd["vnfd-id-ref"]
246 for vnfd in self.topology["constituent-vnfd"]
247 if vnf_id == vnfd["member-vnf-index"]), None)
249 def _find_vnfd_from_vnf_idx(self, vnf_id): # pragma: no cover
251 for vnfd in self.topology["constituent-vnfd"]
252 if vnf_id == vnfd["member-vnf-index"]), None)
255 def find_node_if(nodes, name, if_name, vld_id): # pragma: no cover
258 intf = nodes[name]["interfaces"][if_name]
260 # if not xe0, then maybe vld_id, uplink_0, downlink_0
261 # pop it and re-insert with the correct name from topology
262 intf = nodes[name]["interfaces"].pop(vld_id)
263 nodes[name]["interfaces"][if_name] = intf
266 def _resolve_topology(self):
267 for vld in self.topology["vld"]:
269 node0_data, node1_data = vld["vnfd-connection-point-ref"]
270 except (ValueError, TypeError):
271 raise exceptions.IncorrectConfig(
272 error_msg='Topology file corrupted, wrong endpoint count '
275 node0_name = self._find_vnf_name_from_id(node0_data["member-vnf-index-ref"])
276 node1_name = self._find_vnf_name_from_id(node1_data["member-vnf-index-ref"])
278 node0_if_name = node0_data["vnfd-connection-point-ref"]
279 node1_if_name = node1_data["vnfd-connection-point-ref"]
282 nodes = self.context_cfg["nodes"]
283 node0_if = self.find_node_if(nodes, node0_name, node0_if_name, vld["id"])
284 node1_if = self.find_node_if(nodes, node1_name, node1_if_name, vld["id"])
286 # names so we can do reverse lookups
287 node0_if["ifname"] = node0_if_name
288 node1_if["ifname"] = node1_if_name
290 node0_if["node_name"] = node0_name
291 node1_if["node_name"] = node1_name
293 node0_if["vld_id"] = vld["id"]
294 node1_if["vld_id"] = vld["id"]
297 node0_if["peer_name"] = node1_name
298 node1_if["peer_name"] = node0_name
300 # set peer interface name
301 node0_if["peer_ifname"] = node1_if_name
302 node1_if["peer_ifname"] = node0_if_name
304 # just load the network
305 vld_networks = {n.get('vld_id', name): n for name, n in
306 self.context_cfg["networks"].items()}
308 node0_if["network"] = vld_networks.get(vld["id"], {})
309 node1_if["network"] = vld_networks.get(vld["id"], {})
311 node0_if["dst_mac"] = node1_if["local_mac"]
312 node0_if["dst_ip"] = node1_if["local_ip"]
314 node1_if["dst_mac"] = node0_if["local_mac"]
315 node1_if["dst_ip"] = node0_if["local_ip"]
319 raise exceptions.IncorrectConfig(
320 error_msg='Required interface not found, topology file '
323 for vld in self.topology['vld']:
325 node0_data, node1_data = vld["vnfd-connection-point-ref"]
326 except (ValueError, TypeError):
327 raise exceptions.IncorrectConfig(
328 error_msg='Topology file corrupted, wrong endpoint count '
331 node0_name = self._find_vnf_name_from_id(node0_data["member-vnf-index-ref"])
332 node1_name = self._find_vnf_name_from_id(node1_data["member-vnf-index-ref"])
334 node0_if_name = node0_data["vnfd-connection-point-ref"]
335 node1_if_name = node1_data["vnfd-connection-point-ref"]
337 nodes = self.context_cfg["nodes"]
338 node0_if = self.find_node_if(nodes, node0_name, node0_if_name, vld["id"])
339 node1_if = self.find_node_if(nodes, node1_name, node1_if_name, vld["id"])
341 # add peer interface dict, but remove circular link
342 # TODO: don't waste memory
343 node0_copy = node0_if.copy()
344 node1_copy = node1_if.copy()
345 node0_if["peer_intf"] = node1_copy
346 node1_if["peer_intf"] = node0_copy
348 def _update_context_with_topology(self): # pragma: no cover
349 for vnfd in self.topology["constituent-vnfd"]:
350 vnf_idx = vnfd["member-vnf-index"]
351 vnf_name = self._find_vnf_name_from_id(vnf_idx)
352 vnfd = self._find_vnfd_from_vnf_idx(vnf_idx)
353 self.context_cfg["nodes"][vnf_name].update(vnfd)
355 def _generate_pod_yaml(self): # pragma: no cover
356 context_yaml = os.path.join(LOG_DIR, "pod-{}.yaml".format(self.scenario_cfg['task_id']))
357 # convert OrderedDict to a list
358 # pod.yaml nodes is a list
359 nodes = [self._serialize_node(node) for node in self.context_cfg["nodes"].values()]
362 "networks": self.context_cfg["networks"]
364 with open(context_yaml, "w") as context_out:
365 yaml.safe_dump(pod_dict, context_out, default_flow_style=False,
369 def _serialize_node(node): # pragma: no cover
370 new_node = copy.deepcopy(node)
371 # name field is required
372 # remove context suffix
373 new_node["name"] = node['name'].split('.')[0]
375 new_node["pkey"] = ssh.convert_key_to_str(node["pkey"])
380 def map_topology_to_infrastructure(self):
381 """ This method should verify if the available resources defined in pod.yaml
382 match the topology.yaml file.
384 :return: None. Side effect: context_cfg is updated
386 # 3. Use topology file to find connections & resolve dest address
387 self._resolve_topology()
388 self._update_context_with_topology()
391 def get_vnf_impl(cls, vnf_model_id): # pragma: no cover
392 """ Find the implementing class from vnf_model["vnf"]["name"] field
394 :param vnf_model_id: parsed vnfd model ID field
395 :return: subclass of GenericVNF
397 utils.import_modules_from_package(
398 "yardstick.network_services.vnf_generic.vnf")
399 expected_name = vnf_model_id
403 for name, class_ in ((c.__name__, c) for c in
404 utils.itersubclasses(GenericVNF)):
405 if name == expected_name:
407 classes_found.append(name)
411 except StopIteration:
414 message = ('No implementation for %s found in %s'
415 % (expected_name, classes_found))
416 raise exceptions.IncorrectConfig(error_msg=message)
419 def create_interfaces_from_node(vnfd, node): # pragma: no cover
420 ext_intfs = vnfd["vdu"][0]["external-interface"] = []
421 # have to sort so xe0 goes first
422 for intf_name, intf in sorted(node['interfaces'].items()):
423 # only interfaces with vld_id are added.
424 # Thus there are two layers of filters, only intefaces with vld_id
425 # show up in interfaces, and only interfaces with traffic profiles
426 # are used by the generators
427 if intf.get('vld_id'):
428 # force dpkd_port_num to int so we can do reverse lookup
430 intf['dpdk_port_num'] = int(intf['dpdk_port_num'])
435 "virtual-interface": intf,
436 "vnfd-connection-point-ref": intf_name,
438 ext_intfs.append(ext_intf)
440 def load_vnf_models(self, scenario_cfg=None, context_cfg=None):
441 """ Create VNF objects based on YAML descriptors
448 trex_lib_path = get_nsb_option('trex_client_lib')
449 sys.path[:] = list(chain([trex_lib_path], (x for x in sys.path if x != trex_lib_path)))
451 if scenario_cfg is None:
452 scenario_cfg = self.scenario_cfg
454 if context_cfg is None:
455 context_cfg = self.context_cfg
458 # we assume OrderedDict for consistency in instantiation
459 for node_name, node in context_cfg["nodes"].items():
462 file_name = node["VNF model"]
464 LOG.debug("no model for %s, skipping", node_name)
466 file_path = scenario_cfg['task_path']
467 with utils.open_relative_file(file_name, file_path) as stream:
468 vnf_model = stream.read()
469 vnfd = vnfdgen.generate_vnfd(vnf_model, node)
470 # TODO: here add extra context_cfg["nodes"] regardless of template
471 vnfd = vnfd["vnfd:vnfd-catalog"]["vnfd"][0]
472 # force inject pkey if it exists
473 # we want to standardize Heat using pkey as a string so we don't rely
476 vnfd['mgmt-interface']['pkey'] = node['pkey']
479 self.create_interfaces_from_node(vnfd, node)
480 vnf_impl = self.get_vnf_impl(vnfd['id'])
481 vnf_instance = vnf_impl(node_name, vnfd)
482 vnfs.append(vnf_instance)
487 def pre_run_wait_time(self, time_seconds): # pragma: no cover
488 """Time waited before executing the run method"""
489 time.sleep(time_seconds)
491 def post_run_wait_time(self, time_seconds): # pragma: no cover
492 """Time waited after executing the run method"""
496 class NetworkServiceTestCase(NetworkServiceBase):
497 """Class handles Generic framework to do pre-deployment VNF &
498 Network service testing """
500 __scenario_type__ = "NSPerf"
502 def __init__(self, scenario_cfg, context_cfg): # pragma: no cover
503 super(NetworkServiceTestCase, self).__init__(scenario_cfg, context_cfg)
506 """Setup infrastructure, provission VNFs & start traffic"""
507 # 1. Verify if infrastructure mapping can meet topology
508 self.map_topology_to_infrastructure()
509 # 1a. Load VNF models
510 self.load_vnf_models()
511 # 1b. Fill traffic profile with information from topology
512 self._fill_traffic_profile()
516 # link events will cause VNF application to exit
517 # so we should start traffic runners before VNFs
518 traffic_runners = [vnf for vnf in self.vnfs if vnf.runs_traffic]
519 non_traffic_runners = [vnf for vnf in self.vnfs if not vnf.runs_traffic]
521 for vnf in chain(traffic_runners, non_traffic_runners):
522 LOG.info("Instantiating %s", vnf.name)
523 vnf.instantiate(self.scenario_cfg, self.context_cfg)
524 LOG.info("Waiting for %s to instantiate", vnf.name)
525 vnf.wait_for_instantiate()
528 for vnf in self.vnfs:
532 # we have to generate pod.yaml here after VNF has probed so we know vpci and driver
533 self._generate_pod_yaml()
536 # Start listeners first to avoid losing packets
537 for traffic_gen in traffic_runners:
538 traffic_gen.listen_traffic(self.traffic_profile)
540 # register collector with yardstick for KPI collection.
541 self.collector = Collector(self.vnfs, context_base.Context.get_physical_nodes())
542 self.collector.start()
544 # Start the actual traffic
545 for traffic_gen in traffic_runners:
546 LOG.info("Starting traffic on %s", traffic_gen.name)
547 traffic_gen.run_traffic(self.traffic_profile)
549 def run(self, result): # yardstick API
550 """ Yardstick calls run() at intervals defined in the yaml and
551 produces timestamped samples
553 :param result: dictionary with results to update
557 # this is the only method that is check from the runner
558 # so if we have any fatal error it must be raised via these methods
559 # otherwise we will not terminate
561 result.update(self.collector.get_kpi())
564 class NetworkServiceRFC2544(NetworkServiceBase):
565 """Class handles RFC2544 Network service testing"""
567 __scenario_type__ = "NSPerf-RFC2544"
569 def __init__(self, scenario_cfg, context_cfg): # pragma: no cover
570 super(NetworkServiceRFC2544, self).__init__(scenario_cfg, context_cfg)
573 """Setup infrastructure, provision VNFs"""
574 self.map_topology_to_infrastructure()
575 self.load_vnf_models()
577 traffic_runners = [vnf for vnf in self.vnfs if vnf.runs_traffic]
578 non_traffic_runners = [vnf for vnf in self.vnfs if not vnf.runs_traffic]
580 for vnf in chain(traffic_runners, non_traffic_runners):
581 LOG.info("Instantiating %s", vnf.name)
582 vnf.instantiate(self.scenario_cfg, self.context_cfg)
583 LOG.info("Waiting for %s to instantiate", vnf.name)
584 vnf.wait_for_instantiate()
587 for vnf in self.vnfs:
591 self._generate_pod_yaml()
593 def run(self, output):
596 :param output: scenario output to push results
600 self._fill_traffic_profile()
602 traffic_runners = [vnf for vnf in self.vnfs if vnf.runs_traffic]
604 for traffic_gen in traffic_runners:
605 traffic_gen.listen_traffic(self.traffic_profile)
607 self.collector = Collector(self.vnfs,
608 context_base.Context.get_physical_nodes())
609 self.collector.start()
611 test_completed = False
612 while not test_completed:
613 for traffic_gen in traffic_runners:
614 LOG.info("Run traffic on %s", traffic_gen.name)
615 traffic_gen.run_traffic_once(self.traffic_profile)
617 test_completed = True
618 for traffic_gen in traffic_runners:
619 # wait for all tg to complete running traffic
620 status = traffic_gen.wait_on_traffic()
621 LOG.info("Run traffic on %s complete status=%s",
622 traffic_gen.name, status)
623 if status == 'CONTINUE':
624 # continue running if at least one tg is running
625 test_completed = False
627 output.push(self.collector.get_kpi())
629 self.collector.stop()
631 class NetworkServiceRFC3511(NetworkServiceBase):
632 """Class handles RFC3511 Network service testing"""
634 __scenario_type__ = "NSPerf-RFC3511"
636 def __init__(self, scenario_cfg, context_cfg): # pragma: no cover
637 super(NetworkServiceRFC3511, self).__init__(scenario_cfg, context_cfg)
640 """Setup infrastructure, provision VNFs"""
641 self.map_topology_to_infrastructure()
642 self.load_vnf_models()
644 traffic_runners = [vnf for vnf in self.vnfs if vnf.runs_traffic]
645 non_traffic_runners = [vnf for vnf in self.vnfs if not vnf.runs_traffic]
647 for vnf in chain(traffic_runners, non_traffic_runners):
648 LOG.info("Instantiating %s", vnf.name)
649 vnf.instantiate(self.scenario_cfg, self.context_cfg)
650 LOG.info("Waiting for %s to instantiate", vnf.name)
651 vnf.wait_for_instantiate()
654 for vnf in self.vnfs:
658 self._generate_pod_yaml()
660 def run(self, output):
663 :param output: scenario output to push results
667 self._fill_traffic_profile()
669 traffic_runners = [vnf for vnf in self.vnfs if vnf.runs_traffic]
671 for traffic_gen in traffic_runners:
672 traffic_gen.listen_traffic(self.traffic_profile)
674 self.collector = Collector(self.vnfs,
675 context_base.Context.get_physical_nodes())
676 self.collector.start()
678 for traffic_gen in traffic_runners:
679 LOG.info("Run traffic on %s", traffic_gen.name)
680 traffic_gen.run_traffic(self.traffic_profile)
682 output.push(self.collector.get_kpi())
684 self.collector.stop()