From 4c7026517965700242fe4aebf47f099a6ffd7842 Mon Sep 17 00:00:00 2001 From: Yaroslav Brustinov Date: Sun, 13 Mar 2016 10:34:53 +0200 Subject: hlt add traffic_control: action=poll, reset, sync_run, clear_stats. add wait_on_traffic. fix cleanup_session to remove streams --- .../stl/trex_stl_lib/trex_stl_hltapi.py | 70 +++++++++++++++++----- 1 file changed, 54 insertions(+), 16 deletions(-) (limited to 'scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_hltapi.py') diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_hltapi.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_hltapi.py index a74824d2..056a9afa 100755 --- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_hltapi.py +++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_hltapi.py @@ -303,6 +303,7 @@ class CStreamsPerPort(defaultdict): # save HLT args to modify streams later def save_stream_args(self, ports_list, stream_id, stream_hlt_args): + if stream_id is None: return # no stream_id, can't save TODO: remove this check ASAP if stream_hlt_args.get('load_profile'): return # can't modify profiles, don't save if not self.hlt_history: raise STLError('CStreamsPerPort: this object works only with HLT history, try init with hlt_history = True') if type(stream_id) not in (int, long): raise STLError('CStreamsPerPort: stream_id should be number') @@ -400,10 +401,18 @@ class CTRexHltApi(object): port_list = self._parse_port_list(port_list) except Exception as e: return HLT_ERR('Unable to determine which ports to release: %s' % e if isinstance(e, STLError) else traceback.format_exc()) + try: + self.trex_client.stop(port_list) + except Exception as e: + return HLT_ERR('Unable to stop traffic %s: %s' % (port_list, e if isinstance(e, STLError) else traceback.format_exc())) + try: + self.trex_client.remove_all_streams(port_list) + except Exception as e: + return HLT_ERR('Unable to remove all streams %s: %s' % (port_list, e if isinstance(e, STLError) else traceback.format_exc())) try: self.trex_client.release(port_list) except Exception as e: - return HLT_ERR('Unable to release ports %s: %s' % (port_list, e)) + return HLT_ERR('Unable to release ports %s: %s' % (port_list, e if isinstance(e, STLError) else traceback.format_exc())) try: self.trex_client.disconnect(stop_traffic = False, release_ports = False) except Exception as e: @@ -585,30 +594,53 @@ class CTRexHltApi(object): kwargs = merge_kwargs(traffic_control_kwargs, user_kwargs) action = kwargs['action'] port_handle = kwargs['port_handle'] - ALLOWED_ACTIONS = ['clear_stats', 'run', 'stop', 'sync_run'] + ALLOWED_ACTIONS = ['clear_stats', 'run', 'stop', 'sync_run', 'poll', 'reset'] if action not in ALLOWED_ACTIONS: return HLT_ERR('Action must be one of the following values: {actions}'.format(actions=ALLOWED_ACTIONS)) - if type(port_handle) is not list: - port_handle = [port_handle] if action == 'run': try: self.trex_client.start(ports = port_handle) except Exception as e: return HLT_ERR('Could not start traffic: %s' % e if isinstance(e, STLError) else traceback.format_exc()) - return HLT_OK(stopped = 0) + + elif action == 'sync_run': # (clear_stats + run) + try: + self.trex_client.clear_stats(ports = port_handle) + self.trex_client.start(ports = port_handle) + except Exception as e: + return HLT_ERR('Unable to do sync_run: %s' % e if isinstance(e, STLError) else traceback.format_exc()) elif action == 'stop': try: self.trex_client.stop(ports = port_handle) except Exception as e: - return HLT_ERR('Could not start traffic: %s' % e if isinstance(e, STLError) else traceback.format_exc()) - return HLT_OK(stopped = 1) - else: - return HLT_ERR("Action '{0}' is not supported yet on TRex".format(action)) + return HLT_ERR('Could not stop traffic: %s' % e if isinstance(e, STLError) else traceback.format_exc()) + + elif action == 'reset': + try: + self.trex_client.reset(ports = port_handle) + for port in port_handle: + if port in self._streams_history: + del self._streams_history[port] + except Exception as e: + return HLT_ERR('Could not reset traffic: %s' % e if isinstance(e, STLError) else traceback.format_exc()) + + elif action == 'clear_stats': + try: + self.trex_client.clear_stats(ports = port_handle) + except Exception as e: + return HLT_ERR('Could not clear stats: %s' % e if isinstance(e, STLError) else traceback.format_exc()) + + elif action != 'poll': # at poll just return 'stopped' status + return HLT_ERR("Action '%s' is not supported yet on TRex" % action) - # if we arrived here, this means that operation FAILED! - return HLT_ERR("Probably action '%s' is not implemented" % action) + try: + is_traffic_active = self.trex_client.is_traffic_active(ports = port_handle) + except Exception as e: + return HLT_ERR('Unable to determine ports status: %s' % e if isinstance(e, STLError) else traceback.format_exc()) + + return HLT_OK(stopped = not is_traffic_active) def traffic_stats(self, **user_kwargs): if not self.trex_client: @@ -653,6 +685,16 @@ class CTRexHltApi(object): } return HLT_OK(hlt_stats_dict) + # timeout = maximal time to wait + def wait_on_traffic(self, port_handle = None, timeout = 60): + try: + self.trex_client.wait_on_traffic(port_handle, timeout) + except Exception as e: + return HLT_ERR('Unable to run wait_on_traffic: %s' % e if isinstance(e, STLError) else traceback.format_exc()) + +########################### +# Private functions # +########################### # remove streams from given port(s). # stream_id can be: @@ -689,11 +731,6 @@ class CTRexHltApi(object): return raise STLError('_remove_stream: wrong param %s' % stream_id) - -########################### -# Private functions # -########################### - @staticmethod def _parse_port_list(port_list): if type(port_list) is str: @@ -1481,6 +1518,7 @@ def correct_direction(user_kwargs, kwargs): dst_arg = 'ipv6_dst_' + arg[9:] user_kwargs[arg], user_kwargs[dst_arg] = kwargs[dst_arg], kwargs[arg] +# we produce packets without fcs, so need to reduce produced sizes def correct_sizes(kwargs): for arg in kwargs.keys(): if type(arg) in (int, long): -- cgit 1.2.3-korg