Matteo Scandolo | a229eca | 2017-08-08 13:05:28 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Jeffrey Townsend | fdea52c | 2013-12-10 18:20:11 -0800 | [diff] [blame] | 17 | ############################################################################### |
| 18 | # |
| 19 | # DataPlanePort implementation for VPI platforms. |
| 20 | # |
| 21 | # The VPI-based platforms available here (bpp and vpi) depend upon |
| 22 | # this module for the implementation of the OFTest DataPlanePort interface. |
| 23 | # |
| 24 | ############################################################################### |
| 25 | import sys |
| 26 | import os |
| 27 | import logging |
| 28 | import time |
| 29 | |
| 30 | from oftest import config |
| 31 | |
| 32 | # Requires the libvpi and pyvpi packages |
| 33 | from vpi import vpi |
| 34 | |
| 35 | class DataPlanePortVPI: |
| 36 | """ |
| 37 | Class defining a port monitoring VPI object. |
| 38 | |
| 39 | """ |
| 40 | |
| 41 | # |
| 42 | # OFTest creates and destroys DataPlanePorts for each test. |
| 43 | # We just cache them here. |
| 44 | cachedVPIs = {} |
| 45 | |
| 46 | def vpiInit(self, interface_name, port_number, pcap_dir="."): |
| 47 | self.vpiSpec = interface_name; |
| 48 | if self.vpiSpec in self.cachedVPIs: |
| 49 | self.vpi = self.cachedVPIs[self.vpiSpec] |
| 50 | else: |
| 51 | self.vpi = vpi.Vpi(self.vpiSpec) |
| 52 | pcapspec = "pcapdump|%s/oft-port%.2d.pcap|mpls|PORT%d" % (pcap_dir, port_number, port_number) |
| 53 | self.vpi.AddSendRecvListener(pcapspec); |
| 54 | self.cachedVPIs[self.vpiSpec] = self.vpi |
| 55 | |
| 56 | return self.vpi |
| 57 | |
| 58 | |
| 59 | def __init__(self, interface_name, port_number): |
| 60 | """ |
| 61 | Set up a port monitor object |
| 62 | @param interface_name The name of the physical interface like eth1 |
| 63 | @param port_number The port number associated with this port |
| 64 | """ |
| 65 | self.interface_name = interface_name |
| 66 | self.port_number = port_number |
| 67 | logname = "VPI:" + interface_name |
| 68 | self.logger = logging.getLogger(logname) |
| 69 | |
| 70 | path = "." |
| 71 | if config['log_file']: |
| 72 | path = config['log_file'] |
| 73 | |
| 74 | if self.vpiInit(interface_name, port_number, |
| 75 | os.path.dirname(os.path.abspath(path))) == None: |
| 76 | raise Exception("Could not create VPI interface %s" % interface_name) |
| 77 | |
| 78 | self.logger.info("VPI: %s:%d\n" % (interface_name, port_number)) |
| 79 | |
| 80 | def fileno(self): |
| 81 | return self.vpi.DescriptorGet() |
| 82 | |
| 83 | def recv(self): |
| 84 | pkt = self.vpi.Recv(False) |
| 85 | return (pkt, time.time()) |
| 86 | |
| 87 | def send(self, packet): |
| 88 | """ |
| 89 | Send a packet to the dataplane port |
| 90 | @param packet The packet data to send to the port |
| 91 | @retval The number of bytes sent |
| 92 | """ |
| 93 | _len = len(packet); |
| 94 | self.vpi.Send(packet) |
| 95 | return _len; |