summaryrefslogtreecommitdiffstats
path: root/scripts/automation/trex_control_plane/stl/trex_stl_lib/rx_services
diff options
context:
space:
mode:
authorimarom <imarom@cisco.com>2016-12-21 17:49:38 +0200
committerimarom <imarom@cisco.com>2016-12-21 17:52:14 +0200
commit1f405257ba6caed845551b0641de914281ecfeba (patch)
treeed941041bd5d216d73566a07b1869f70b95df5ec /scripts/automation/trex_control_plane/stl/trex_stl_lib/rx_services
parentc77174ade8d36f377cfa74da4c487f04988a9679 (diff)
RX services - general API to allow addition of new features
see trex_stl_lib/rx_services/trex_stl_rx_service_api.py Signed-off-by: imarom <imarom@cisco.com>
Diffstat (limited to 'scripts/automation/trex_control_plane/stl/trex_stl_lib/rx_services')
-rw-r--r--scripts/automation/trex_control_plane/stl/trex_stl_lib/rx_services/__init__.py0
-rw-r--r--scripts/automation/trex_control_plane/stl/trex_stl_lib/rx_services/trex_stl_rx_service_api.py199
-rw-r--r--scripts/automation/trex_control_plane/stl/trex_stl_lib/rx_services/trex_stl_rx_service_arp.py57
-rw-r--r--scripts/automation/trex_control_plane/stl/trex_stl_lib/rx_services/trex_stl_rx_service_icmp.py85
4 files changed, 341 insertions, 0 deletions
diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/rx_services/__init__.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/rx_services/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/rx_services/__init__.py
diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/rx_services/trex_stl_rx_service_api.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/rx_services/trex_stl_rx_service_api.py
new file mode 100644
index 00000000..b0904382
--- /dev/null
+++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/rx_services/trex_stl_rx_service_api.py
@@ -0,0 +1,199 @@
+
+import time
+
+# a generic abstract class for implementing RX services using the server
+class RXServiceAPI(object):
+
+ # specify for which layer this service is
+ LAYER_MODE_ANY = 0
+ LAYER_MODE_L2 = 1
+ LAYER_MODE_L3 = 2
+
+ def __init__ (self, port, layer_mode = LAYER_MODE_ANY, queue_size = 100):
+ self.port = port
+ self.queue_size = queue_size
+ self.layer_mode = layer_mode
+
+ ################### virtual methods ######################
+
+ def get_name (self):
+ """
+ returns the name of the service
+
+ :returns:
+ str
+
+ """
+
+ raise NotImplementedError()
+
+ def pre_execute (self):
+ """
+ specific class code called before executing
+
+ :returns:
+ RC object
+
+ """
+ raise NotImplementedError()
+
+ def generate_request (self):
+ """
+ generate a request to be sent to the server
+
+ :returns:
+ list of streams
+
+ """
+ raise NotImplementedError()
+
+ def on_pkt_rx (self, pkt, start_ts):
+ """
+ called for each packet arriving on RX
+
+ :parameters:
+ 'pkt' - the packet received
+ 'start_ts' - the time recorded when 'start' was called
+
+ :returns:
+ None for fetching more packets
+ RC object for terminating
+
+
+
+ """
+ raise NotImplementedError()
+
+
+ def on_timeout_err (self, retries):
+ """
+ called when a timeout occurs
+
+ :parameters:
+ retries - how many times was the service retring before failing
+
+ :returns:
+ RC object
+
+ """
+ raise NotImplementedError()
+
+
+ ##################### API ######################
+ def execute (self, retries = 0):
+
+ # sanity check
+ rc = self.__sanity()
+ if not rc:
+ return rc
+
+ # first cleanup
+ rc = self.port.remove_all_streams()
+ if not rc:
+ return rc
+
+
+ # start the iteration
+ try:
+
+ # add the stream(s)
+ self.port.add_streams(self.generate_request())
+ rc = self.port.set_rx_queue(size = self.queue_size)
+ if not rc:
+ return rc
+
+ return self.__execute_internal(retries)
+
+ finally:
+ # best effort restore
+ self.port.remove_rx_queue()
+ self.port.remove_all_streams()
+
+
+ ##################### Internal ######################
+ def __sanity (self):
+ if not self.port.is_service_mode_on():
+ return self.port.err('port service mode must be enabled for performing {0}. Please enable service mode'.format(self.get_name()))
+
+ if self.layer_mode == RXServiceAPI.LAYER_MODE_L2:
+ if not self.port.is_l2_mode():
+ return self.port.err('{0} - requires L2 mode configuration'.format(self.get_name()))
+
+ elif self.layer_mode == RXServiceAPI.LAYER_MODE_L3:
+ if not self.port.is_l3_mode():
+ return self.port.err('{0} - requires L3 mode configuration'.format(self.get_name()))
+
+
+ # sanity
+ if self.port.is_active():
+ return self.port.err('{0} - port is active, please stop the port before executing command'.format(self.get_name()))
+
+ # call the specific class implementation
+ rc = self.pre_execute()
+ if not rc:
+ return rc
+
+ return True
+
+
+ # main resolve function
+ def __execute_internal (self, retries):
+
+ # retry for 'retries'
+ index = 0
+ while True:
+ rc = self.execute_iteration()
+ if rc is not None:
+ return rc
+
+ if index >= retries:
+ return self.on_timeout_err(retries)
+
+ index += 1
+ time.sleep(0.1)
+
+
+
+ def execute_iteration (self):
+
+ mult = {'op': 'abs', 'type' : 'percentage', 'value': 100}
+ rc = self.port.start(mul = mult, force = False, duration = -1, mask = 0xffffffff)
+ if not rc:
+ return rc
+
+ # save the start timestamp
+ self.start_ts = rc.data()['ts']
+
+ # block until traffic finishes
+ while self.port.is_active():
+ time.sleep(0.01)
+
+ return self.wait_for_rx_response()
+
+
+ def wait_for_rx_response (self):
+
+ # we try to fetch response for 5 times
+ polling = 5
+
+ while polling > 0:
+
+ # fetch the queue
+ rx_pkts = self.port.get_rx_queue_pkts()
+
+ # might be an error
+ if not rx_pkts:
+ return rx_pkts
+
+ # for each packet - examine it
+ for pkt in rx_pkts.data():
+ rc = self.on_pkt_rx(pkt, self.start_ts)
+ if rc is not None:
+ return rc
+
+ if polling == 0:
+ return None
+
+ polling -= 1
+ time.sleep(0.1)
+
diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/rx_services/trex_stl_rx_service_arp.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/rx_services/trex_stl_rx_service_arp.py
new file mode 100644
index 00000000..2c159313
--- /dev/null
+++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/rx_services/trex_stl_rx_service_arp.py
@@ -0,0 +1,57 @@
+from .trex_stl_rx_service_api import RXServiceAPI
+
+from ..trex_stl_streams import STLStream, STLTXSingleBurst
+from ..trex_stl_packet_builder_scapy import STLPktBuilder
+
+from scapy.layers.l2 import Ether, ARP
+
+
+class RXServiceARP(RXServiceAPI):
+
+ def __init__ (self, port_id):
+ super(RXServiceARP, self).__init__(port_id, layer_mode = RXServiceAPI.LAYER_MODE_L3)
+
+ def get_name (self):
+ return "ARP"
+
+ def pre_execute (self):
+
+ self.dst = self.port.get_dst_addr()
+ self.src = self.port.get_src_addr()
+
+ return self.port.ok()
+
+ # return a list of streams for request
+ def generate_request (self):
+
+ base_pkt = Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(psrc = self.src['ipv4'], pdst = self.dst['ipv4'], hwsrc = self.src['mac'])
+ s1 = STLStream( packet = STLPktBuilder(pkt = base_pkt), mode = STLTXSingleBurst(total_pkts = 1) )
+
+ return [s1]
+
+
+ def on_pkt_rx (self, pkt, start_ts):
+ # convert to scapy
+ scapy_pkt = Ether(pkt['binary'])
+
+ # if not ARP wait for the next one
+ if not 'ARP' in scapy_pkt:
+ return None
+
+ arp = scapy_pkt['ARP']
+
+ # check this is the right ARP (ARP reply with the address)
+ if (arp.op != 2) or (arp.psrc != self.dst['ipv4']):
+ return None
+
+
+ return self.port.ok({'psrc' : arp.psrc, 'hwsrc': arp.hwsrc})
+
+ #return self.port.ok('Port {0} - Recieved ARP reply from: {1}, hw: {2}'.format(self.port.port_id, arp.psrc, arp.hwsrc))
+
+
+ def on_timeout_err (self, retries):
+ return self.port.err('failed to receive ARP response ({0} retries)'.format(retries))
+
+
+
diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/rx_services/trex_stl_rx_service_icmp.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/rx_services/trex_stl_rx_service_icmp.py
new file mode 100644
index 00000000..486cd458
--- /dev/null
+++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/rx_services/trex_stl_rx_service_icmp.py
@@ -0,0 +1,85 @@
+from .trex_stl_rx_service_api import RXServiceAPI
+
+from ..trex_stl_streams import STLStream, STLTXSingleBurst
+from ..trex_stl_packet_builder_scapy import STLPktBuilder
+
+from scapy.layers.l2 import Ether
+from scapy.layers.inet import IP, ICMP
+
+
+class RXServiceICMP(RXServiceAPI):
+
+ def __init__ (self, port, ping_ip, pkt_size):
+
+ super(RXServiceICMP, self).__init__(port, layer_mode = RXServiceAPI.LAYER_MODE_L3)
+ self.ping_ip = ping_ip
+ self.pkt_size = pkt_size
+
+ def get_name (self):
+ return "PING"
+
+ def pre_execute (self):
+
+ if not self.port.is_resolved():
+ return self.port.err('ping - port has an unresolved destination, cannot determine next hop MAC address')
+
+ self.src = self.port.get_src_addr()
+ self.dst = self.port.get_dst_addr()
+
+
+ return self.port.ok()
+
+
+ # return a list of streams for request
+ def generate_request (self):
+
+ base_pkt = Ether(dst = self.dst['mac'])/IP(src = self.src['ipv4'], dst = self.ping_ip)/ICMP(type = 8)
+ pad = max(0, self.pkt_size - len(base_pkt))
+
+ base_pkt = base_pkt / (pad * 'x')
+
+ s1 = STLStream( packet = STLPktBuilder(pkt = base_pkt), mode = STLTXSingleBurst(total_pkts = 1) )
+
+ self.base_pkt = base_pkt
+
+ return [s1]
+
+ def on_pkt_rx (self, pkt, start_ts):
+
+ scapy_pkt = Ether(pkt['binary'])
+ if not 'ICMP' in scapy_pkt:
+ return None
+
+ ip = scapy_pkt['IP']
+ if ip.dst != self.src['ipv4']:
+ return None
+
+ icmp = scapy_pkt['ICMP']
+
+ dt = pkt['ts'] - start_ts
+
+ # echo reply
+ if icmp.type == 0:
+ # check seq
+ if icmp.seq != self.base_pkt['ICMP'].seq:
+ return None
+ return self.port.ok('Reply from {0}: bytes={1}, time={2:.2f}ms, TTL={3}'.format(ip.src, len(pkt['binary']), dt * 1000, ip.ttl))
+
+ # unreachable
+ elif icmp.type == 3:
+ # check seq
+ if icmp.payload.seq != self.base_pkt['ICMP'].seq:
+ return None
+ return self.port.ok('Reply from {0}: Destination host unreachable'.format(icmp.src))
+
+ else:
+ # skip any other types
+ #scapy_pkt.show2()
+ return None
+
+
+
+ # return the str of a timeout err
+ def on_timeout_err (self, retries):
+ return self.port.ok('Request timed out.')
+