From aaef3f95683bfa1574537c543e4ffc86afb0480f Mon Sep 17 00:00:00 2001 From: imarom Date: Thu, 25 Feb 2016 09:54:45 -0500 Subject: port attributes - promiscuous and etc. --- .../unit_tests/functional_tests/stl_basic_tests.py | 12 ++-- .../trex_control_plane/stl/console/trex_console.py | 6 ++ .../stl/trex_stl_lib/trex_stl_client.py | 82 ++++++++++++++++++++++ .../stl/trex_stl_lib/trex_stl_port.py | 30 +++++++- .../stl/trex_stl_lib/utils/parsing_opts.py | 21 ++++++ scripts/stl/udp_1pkt_simple.py | 6 +- 6 files changed, 148 insertions(+), 9 deletions(-) (limited to 'scripts') diff --git a/scripts/automation/regression/unit_tests/functional_tests/stl_basic_tests.py b/scripts/automation/regression/unit_tests/functional_tests/stl_basic_tests.py index 5ae890ba..f7638746 100644 --- a/scripts/automation/regression/unit_tests/functional_tests/stl_basic_tests.py +++ b/scripts/automation/regression/unit_tests/functional_tests/stl_basic_tests.py @@ -61,13 +61,13 @@ class CStlBasic_Test(functional_general_test.CGeneralFunctional_Test): def compare_caps (self, cap1, cap2, max_diff_sec = 0.01): - f1 = open(cap1, 'r') - reader1 = pcap.Reader(f1) - pkts1 = reader1.readpkts() + with open(cap1, 'r') as f1: + reader1 = pcap.Reader(f1) + pkts1 = reader1.readpkts() - f2 = open(cap2, 'r') - reader2 = pcap.Reader(f2) - pkts2 = reader2.readpkts() + with open(cap2, 'r') as f2: + reader2 = pcap.Reader(f2) + pkts2 = reader2.readpkts() assert_equal(len(pkts1), len(pkts2)) diff --git a/scripts/automation/trex_control_plane/stl/console/trex_console.py b/scripts/automation/trex_control_plane/stl/console/trex_console.py index 0beb10df..53817464 100755 --- a/scripts/automation/trex_control_plane/stl/console/trex_console.py +++ b/scripts/automation/trex_control_plane/stl/console/trex_console.py @@ -332,6 +332,12 @@ class TRexConsole(TRexGeneralCmd): def help_push (self): return self.do_push("-h") + def do_portattr (self, line): + '''Change/show port(s) attributes\n''' + return self.stateless_client.set_port_attr_line(line) + + def help_portattr (self): + return self.do_portattr("-h") def do_history (self, line): '''Manage the command history\n''' diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_client.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_client.py index fe162096..90a17ce2 100644 --- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_client.py +++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_client.py @@ -609,6 +609,15 @@ class STLClient(object): return rc + def __set_port_attr (self, port_id_list = None, attr_dict = None): + + port_id_list = self.__ports(port_id_list) + rc = RC() + + for port_id in port_id_list: + rc.add(self.ports[port_id].set_attr(attr_dict)) + + return rc # connect to server def __connect(self): @@ -1639,6 +1648,47 @@ class STLClient(object): raise STLTimeoutError(timeout) + # + """ + set port(s) attributes + + :parameters: + promiscuous - set this to True or False + + :raises: + None + + """ + @__api_check(True) + def set_port_attr (self, ports = None, promiscuous = None): + # by default use all acquired ports + if ports == None: + ports = self.get_acquired_ports() + + # verify valid port id list + rc = self._validate_port_list(ports) + if not rc: + raise STLArgumentError('ports', ports, valid_values = self.get_all_ports()) + + # check arguments + validate_type('promiscuous', promiscuous, bool) + + # build attributes + attr_dict = {} + if promiscuous is not None: + attr_dict['promiscuous'] = {'enabled': bool(promiscuous)} + + # no attributes to set + if not attr_dict: + return + + self.logger.pre_cmd("Applying attributes on port(s) {0}:".format(ports)) + rc = self.__set_port_attr(ports, attr_dict) + self.logger.post_cmd(rc) + + if not rc: + raise STLError(rc) + """ clear all events @@ -2022,3 +2072,35 @@ class STLClient(object): return True + + + @__console + def set_port_attr_line (self, line): + '''Sets port attributes ''' + + parser = parsing_opts.gen_parser(self, + "port", + self.set_port_attr_line.__doc__, + parsing_opts.PORT_LIST_WITH_ALL, + parsing_opts.PROMISCUOUS_SWITCH) + + opts = parser.parse_args(line.split()) + if opts is None: + return + + try: + self.set_port_attr(opts.ports, opts.prom) + except STLError as e: + print e.brief() + return + + # show + print "" + for port_id in opts.ports: + print format_text('Port {0}:\n'.format(port_id), 'bold', 'underline') + for k, v in self.get_port(port_id).get_attr().iteritems(): + print "{0}:".format(k) + for pk, pv in v.iteritems(): + print " {0}: {1}".format(pk, format_text(str(pv), 'bold')) + print "" + diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_port.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_port.py index 4dd07a13..8d542b35 100644 --- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_port.py +++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_port.py @@ -54,6 +54,7 @@ class Port(object): self.streams = {} self.profile = None self.session_id = session_id + self.attr = {} self.port_stats = trex_stl_stats.CPortStats(self) @@ -138,6 +139,9 @@ class Port(object): self.next_available_id = long(rc.data()['max_stream_id']) + 1 + # attributes + self.attr = rc.data()['attr'] + # sync the streams params = {"port_id": self.port_id} @@ -448,10 +452,34 @@ class Port(object): return self.ok() + def set_attr (self, attr_dict): + if not self.is_acquired(): + return self.err("port is not owned") + + if (self.state == self.STATE_DOWN): + return self.err("port is down") + + params = {"handler": self.handler, + "port_id": self.port_id, + "attr": attr_dict} + + rc = self.transmit("set_port_attr", params) + if rc.bad(): + return self.err(rc.err()) + + + self.attr.update(attr_dict) + + return self.ok() + + + def get_attr (self): + return self.attr + def get_profile (self): return self.profile - + def print_profile (self, mult, duration): if not self.get_profile(): return diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/parsing_opts.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/parsing_opts.py index 92e9a1d6..0c70801f 100755 --- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/parsing_opts.py +++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/parsing_opts.py @@ -27,6 +27,9 @@ FULL_OUTPUT = 15 IPG = 16 SPEEDUP = 17 COUNT = 18 +PROMISCUOUS = 19 +NO_PROMISCUOUS = 20 +PROMISCUOUS_SWITCH = 21 GLOBAL_STATS = 50 PORT_STATS = 51 @@ -223,6 +226,19 @@ OPTIONS_DB = {MULTIPLIER: ArgumentPack(['-m', '--multiplier'], 'default': 1, 'type': int}), + PROMISCUOUS: ArgumentPack(['--prom'], + {'help': "sets port promiscuous on", + 'dest': "prom", + 'default': None, + 'action': "store_true"}), + + NO_PROMISCUOUS: ArgumentPack(['--no_prom'], + {'help': "sets port promiscuous off", + 'dest': "prom", + 'default': None, + 'action': "store_false"}), + + PORT_LIST: ArgumentPack(['--port'], {"nargs": '+', 'dest':'ports', @@ -305,6 +321,11 @@ OPTIONS_DB = {MULTIPLIER: ArgumentPack(['-m', '--multiplier'], 'default': []}), + # promiscuous + PROMISCUOUS_SWITCH: ArgumentGroup(MUTEX, [PROMISCUOUS, + NO_PROMISCUOUS], + {'required': False}), + # advanced options PORT_LIST_WITH_ALL: ArgumentGroup(MUTEX, [PORT_LIST, ALL_PORTS], diff --git a/scripts/stl/udp_1pkt_simple.py b/scripts/stl/udp_1pkt_simple.py index 31492291..2cfb9f1d 100644 --- a/scripts/stl/udp_1pkt_simple.py +++ b/scripts/stl/udp_1pkt_simple.py @@ -3,8 +3,10 @@ from trex_stl_lib.api import * class STLS1(object): def create_stream (self): - return STLStream( packet = STLPktBuilder(pkt = Ether()/IP(src="16.0.0.1",dst="48.0.0.1")/UDP(dport=12,sport=1025)/(10*'x')), - mode = STLTXCont() ) + return STLStream( packet = STLPktBuilder(pkt = Ether(dst = "5")/IP(src="16.0.0.1",dst="48.0.0.1")/UDP(dport=12,sport=1025)/(10*'x')), + #mode = STLTXCont() + mode = STLTXSingleBurst(total_pkts = 1) + ) def get_streams (self, direction = 0): # create 1 stream -- cgit 1.2.3-korg