Jeffrey Townsend | fdea52c | 2013-12-10 18:20:11 -0800 | [diff] [blame] | 1 | ############################################################################### |
| 2 | # |
| 3 | # DataPlanePort implementation for VPI platforms. |
| 4 | # |
| 5 | # The VPI-based platforms available here (bpp and vpi) depend upon |
| 6 | # this module for the implementation of the OFTest DataPlanePort interface. |
| 7 | # |
| 8 | ############################################################################### |
| 9 | import sys |
| 10 | import os |
| 11 | import logging |
| 12 | import time |
| 13 | |
| 14 | from oftest import config |
| 15 | |
| 16 | # Requires the libvpi and pyvpi packages |
| 17 | from vpi import vpi |
| 18 | |
| 19 | class DataPlanePortVPI: |
| 20 | """ |
| 21 | Class defining a port monitoring VPI object. |
| 22 | |
| 23 | """ |
| 24 | |
| 25 | # |
| 26 | # OFTest creates and destroys DataPlanePorts for each test. |
| 27 | # We just cache them here. |
| 28 | cachedVPIs = {} |
| 29 | |
| 30 | def vpiInit(self, interface_name, port_number, pcap_dir="."): |
| 31 | self.vpiSpec = interface_name; |
| 32 | if self.vpiSpec in self.cachedVPIs: |
| 33 | self.vpi = self.cachedVPIs[self.vpiSpec] |
| 34 | else: |
| 35 | self.vpi = vpi.Vpi(self.vpiSpec) |
| 36 | pcapspec = "pcapdump|%s/oft-port%.2d.pcap|mpls|PORT%d" % (pcap_dir, port_number, port_number) |
| 37 | self.vpi.AddSendRecvListener(pcapspec); |
| 38 | self.cachedVPIs[self.vpiSpec] = self.vpi |
| 39 | |
| 40 | return self.vpi |
| 41 | |
| 42 | |
| 43 | def __init__(self, interface_name, port_number): |
| 44 | """ |
| 45 | Set up a port monitor object |
| 46 | @param interface_name The name of the physical interface like eth1 |
| 47 | @param port_number The port number associated with this port |
| 48 | """ |
| 49 | self.interface_name = interface_name |
| 50 | self.port_number = port_number |
| 51 | logname = "VPI:" + interface_name |
| 52 | self.logger = logging.getLogger(logname) |
| 53 | |
| 54 | path = "." |
| 55 | if config['log_file']: |
| 56 | path = config['log_file'] |
| 57 | |
| 58 | if self.vpiInit(interface_name, port_number, |
| 59 | os.path.dirname(os.path.abspath(path))) == None: |
| 60 | raise Exception("Could not create VPI interface %s" % interface_name) |
| 61 | |
| 62 | self.logger.info("VPI: %s:%d\n" % (interface_name, port_number)) |
| 63 | |
| 64 | def fileno(self): |
| 65 | return self.vpi.DescriptorGet() |
| 66 | |
| 67 | def recv(self): |
| 68 | pkt = self.vpi.Recv(False) |
| 69 | return (pkt, time.time()) |
| 70 | |
| 71 | def send(self, packet): |
| 72 | """ |
| 73 | Send a packet to the dataplane port |
| 74 | @param packet The packet data to send to the port |
| 75 | @retval The number of bytes sent |
| 76 | """ |
| 77 | _len = len(packet); |
| 78 | self.vpi.Send(packet) |
| 79 | return _len; |