Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 1 | """ |
| 2 | OpenFlow Test Framework |
| 3 | |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 4 | DataPlane and DataPlanePort classes |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 5 | |
| 6 | Provide the interface to the control the set of ports being used |
| 7 | to stimulate the switch under test. |
| 8 | |
| 9 | See the class dataplaneport for more details. This class wraps |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 10 | a set of those objects allowing general calls and parsing |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 11 | configuration. |
| 12 | |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 13 | @todo Add "filters" for matching packets. Actions supported |
| 14 | for filters should include a callback or a counter |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 15 | """ |
| 16 | |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 17 | import sys |
| 18 | import os |
| 19 | import socket |
| 20 | import time |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 21 | import select |
| 22 | import logging |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 23 | from threading import Thread |
| 24 | from threading import Lock |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 25 | from threading import Condition |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 26 | import ofutils |
| 27 | import netutils |
Rich Lane | 472aaea | 2013-08-27 09:27:38 -0700 | [diff] [blame] | 28 | from pcap_writer import PcapWriter |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 29 | |
Rich Lane | b42a31c | 2012-10-05 17:54:17 -0700 | [diff] [blame] | 30 | have_pypcap = False |
| 31 | try: |
| 32 | import pcap |
Ed Swierk | ab0bab3 | 2012-11-30 13:31:00 -0800 | [diff] [blame] | 33 | if hasattr(pcap, "pcap"): |
| 34 | # the incompatible pylibpcap library masquerades as pcap |
| 35 | have_pypcap = True |
Rich Lane | b42a31c | 2012-10-05 17:54:17 -0700 | [diff] [blame] | 36 | except: |
| 37 | pass |
| 38 | |
Ed Swierk | 506614a | 2012-03-29 08:16:59 -0700 | [diff] [blame] | 39 | def match_exp_pkt(exp_pkt, pkt): |
| 40 | """ |
| 41 | Compare the string value of pkt with the string value of exp_pkt, |
| 42 | and return True iff they are identical. If the length of exp_pkt is |
| 43 | less than the minimum Ethernet frame size (60 bytes), then padding |
| 44 | bytes in pkt are ignored. |
| 45 | """ |
| 46 | e = str(exp_pkt) |
| 47 | p = str(pkt) |
| 48 | if len(e) < 60: |
| 49 | p = p[:len(e)] |
| 50 | return e == p |
| 51 | |
Jeffrey Townsend | 0e8b092 | 2012-07-11 11:37:46 -0700 | [diff] [blame] | 52 | |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 53 | class DataPlanePort: |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 54 | """ |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 55 | Uses raw sockets to capture and send packets on a network interface. |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 56 | """ |
| 57 | |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 58 | RCV_SIZE_DEFAULT = 4096 |
| 59 | ETH_P_ALL = 0x03 |
| 60 | RCV_TIMEOUT = 10000 |
| 61 | |
| 62 | def __init__(self, interface_name, port_number): |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 63 | """ |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 64 | @param interface_name The name of the physical interface like eth1 |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 65 | """ |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 66 | self.interface_name = interface_name |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 67 | self.socket = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, |
| 68 | socket.htons(self.ETH_P_ALL)) |
| 69 | self.socket.bind((interface_name, 0)) |
| 70 | netutils.set_promisc(self.socket, interface_name) |
| 71 | self.socket.settimeout(self.RCV_TIMEOUT) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 72 | |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 73 | def __del__(self): |
| 74 | if self.socket: |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 75 | self.socket.close() |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 76 | |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 77 | def fileno(self): |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 78 | """ |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 79 | Return an integer file descriptor that can be passed to select(2). |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 80 | """ |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 81 | return self.socket.fileno() |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 82 | |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 83 | def recv(self): |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 84 | """ |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 85 | Receive a packet from this port. |
| 86 | @retval (packet data, timestamp) |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 87 | """ |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 88 | pkt = self.socket.recv(self.RCV_SIZE_DEFAULT) |
| 89 | return (pkt, time.time()) |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 90 | |
| 91 | def send(self, packet): |
| 92 | """ |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 93 | Send a packet out this port. |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 94 | @param packet The packet data to send to the port |
| 95 | @retval The number of bytes sent |
| 96 | """ |
| 97 | return self.socket.send(packet) |
| 98 | |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 99 | def down(self): |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 100 | """ |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 101 | Bring the physical link down. |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 102 | """ |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 103 | os.system("ifconfig down %s" % self.interface_name) |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 104 | |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 105 | def up(self): |
ShreyaPandita | cd8e1cf | 2012-11-28 11:44:42 -0500 | [diff] [blame] | 106 | """ |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 107 | Bring the physical link up. |
ShreyaPandita | cd8e1cf | 2012-11-28 11:44:42 -0500 | [diff] [blame] | 108 | """ |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 109 | os.system("ifconfig up %s" % self.interface_name) |
ShreyaPandita | cd8e1cf | 2012-11-28 11:44:42 -0500 | [diff] [blame] | 110 | |
| 111 | |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 112 | class DataPlanePortPcap: |
Rich Lane | b42a31c | 2012-10-05 17:54:17 -0700 | [diff] [blame] | 113 | """ |
| 114 | Alternate port implementation using libpcap. This is required for recent |
| 115 | versions of Linux (such as Linux 3.2 included in Ubuntu 12.04) which |
| 116 | offload the VLAN tag, so it isn't in the data returned from a read on a raw |
| 117 | socket. libpcap understands how to read the VLAN tag from the kernel. |
| 118 | """ |
| 119 | |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 120 | def __init__(self, interface_name, port_number): |
Rich Lane | b42a31c | 2012-10-05 17:54:17 -0700 | [diff] [blame] | 121 | self.pcap = pcap.pcap(interface_name) |
| 122 | self.pcap.setnonblock() |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 123 | |
| 124 | def fileno(self): |
Rich Lane | b42a31c | 2012-10-05 17:54:17 -0700 | [diff] [blame] | 125 | return self.pcap.fileno() |
| 126 | |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 127 | def recv(self): |
| 128 | (timestamp, pkt) = next(self.pcap) |
Rich Lane | 0415fd7 | 2014-02-25 22:04:17 -0800 | [diff] [blame] | 129 | return (pkt[:], timestamp) |
Rich Lane | b42a31c | 2012-10-05 17:54:17 -0700 | [diff] [blame] | 130 | |
| 131 | def send(self, packet): |
Rich Lane | b42a31c | 2012-10-05 17:54:17 -0700 | [diff] [blame] | 132 | return self.pcap.inject(packet, len(packet)) |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 133 | |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 134 | def down(self): |
| 135 | pass |
| 136 | |
| 137 | def up(self): |
| 138 | pass |
| 139 | |
| 140 | class DataPlane(Thread): |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 141 | """ |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 142 | This class provides methods to send and receive packets on the dataplane. |
| 143 | It uses the DataPlanePort class, or an alternative implementation of that |
| 144 | interface, to do IO on a particular port. A background thread is used to |
| 145 | read packets from the dataplane ports and enqueue them to be read by the |
| 146 | test. The kill() method must be called to shutdown this thread. |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 147 | """ |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 148 | |
| 149 | MAX_QUEUE_LEN = 100 |
| 150 | |
Jeffrey Townsend | 0e8b092 | 2012-07-11 11:37:46 -0700 | [diff] [blame] | 151 | def __init__(self, config=None): |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 152 | Thread.__init__(self) |
| 153 | |
| 154 | # dict from port number to port object |
| 155 | self.ports = {} |
| 156 | |
| 157 | # dict from port number to list of (timestamp, packet) |
| 158 | self.packet_queues = {} |
| 159 | |
| 160 | # cvar serves double duty as a regular top level lock and |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 161 | # as a condition variable |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 162 | self.cvar = Condition() |
| 163 | |
| 164 | # Used to wake up the event loop from another thread |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 165 | self.waker = ofutils.EventDescriptor() |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 166 | self.killed = False |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 167 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 168 | self.logger = logging.getLogger("dataplane") |
Rich Lane | 472aaea | 2013-08-27 09:27:38 -0700 | [diff] [blame] | 169 | self.pcap_writer = None |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 170 | |
Jeffrey Townsend | 0e8b092 | 2012-07-11 11:37:46 -0700 | [diff] [blame] | 171 | if config is None: |
| 172 | self.config = {} |
| 173 | else: |
| 174 | self.config = config; |
| 175 | |
| 176 | ############################################################ |
| 177 | # |
Jeffrey Townsend | 0e8b092 | 2012-07-11 11:37:46 -0700 | [diff] [blame] | 178 | # The platform/config can provide a custom DataPlanePort class |
| 179 | # here if you have a custom implementation with different |
| 180 | # behavior. |
| 181 | # |
| 182 | # Set config.dataplane.portclass = MyDataPlanePortClass |
| 183 | # where MyDataPlanePortClass has the same interface as the class |
| 184 | # DataPlanePort defined here. |
| 185 | # |
Rich Lane | fb71830 | 2012-12-26 21:02:31 -0800 | [diff] [blame] | 186 | if "dataplane" in self.config and "portclass" in self.config["dataplane"]: |
| 187 | self.dppclass = self.config["dataplane"]["portclass"] |
| 188 | elif have_pypcap: |
| 189 | self.dppclass = DataPlanePortPcap |
| 190 | else: |
| 191 | self.logger.warning("Missing pypcap, VLAN tests may fail. See README for installation instructions.") |
| 192 | self.dppclass = DataPlanePort |
Jeffrey Townsend | 0e8b092 | 2012-07-11 11:37:46 -0700 | [diff] [blame] | 193 | |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 194 | self.start() |
| 195 | |
| 196 | def run(self): |
| 197 | """ |
| 198 | Activity function for class |
| 199 | """ |
| 200 | while not self.killed: |
| 201 | sockets = [self.waker] + self.ports.values() |
| 202 | try: |
| 203 | sel_in, sel_out, sel_err = select.select(sockets, [], [], 1) |
| 204 | except: |
| 205 | print sys.exc_info() |
| 206 | self.logger.error("Select error, exiting") |
| 207 | break |
| 208 | |
| 209 | with self.cvar: |
| 210 | for port in sel_in: |
| 211 | if port == self.waker: |
| 212 | self.waker.wait() |
| 213 | continue |
| 214 | else: |
| 215 | # Enqueue packet |
| 216 | pkt, timestamp = port.recv() |
| 217 | port_number = port._port_number |
| 218 | self.logger.debug("Pkt len %d in on port %d", |
| 219 | len(pkt), port_number) |
Rich Lane | 472aaea | 2013-08-27 09:27:38 -0700 | [diff] [blame] | 220 | if self.pcap_writer: |
| 221 | self.pcap_writer.write(pkt, timestamp, port_number) |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 222 | queue = self.packet_queues[port_number] |
| 223 | if len(queue) >= self.MAX_QUEUE_LEN: |
| 224 | # Queue full, throw away oldest |
| 225 | queue.pop(0) |
| 226 | self.logger.debug("Discarding oldest packet to make room") |
| 227 | queue.append((pkt, timestamp)) |
| 228 | self.cvar.notify_all() |
| 229 | |
| 230 | self.logger.info("Thread exit") |
| 231 | |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 232 | def port_add(self, interface_name, port_number): |
| 233 | """ |
| 234 | Add a port to the dataplane |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 235 | @param interface_name The name of the physical interface like eth1 |
| 236 | @param port_number The port number used to refer to the port |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 237 | Stashes the port number on the created port object. |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 238 | """ |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 239 | self.ports[port_number] = self.dppclass(interface_name, port_number) |
| 240 | self.ports[port_number]._port_number = port_number |
| 241 | self.packet_queues[port_number] = [] |
| 242 | # Need to wake up event loop to change the sockets being selected on. |
| 243 | self.waker.notify() |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 244 | |
| 245 | def send(self, port_number, packet): |
| 246 | """ |
| 247 | Send a packet to the given port |
| 248 | @param port_number The port to send the data to |
| 249 | @param packet Raw packet data to send to port |
| 250 | """ |
Dan Talayco | 11c26e7 | 2010-03-07 22:03:57 -0800 | [diff] [blame] | 251 | self.logger.debug("Sending %d bytes to port %d" % |
| 252 | (len(packet), port_number)) |
Rich Lane | 472aaea | 2013-08-27 09:27:38 -0700 | [diff] [blame] | 253 | if self.pcap_writer: |
| 254 | self.pcap_writer.write(packet, time.time(), port_number) |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 255 | bytes = self.ports[port_number].send(packet) |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 256 | if bytes != len(packet): |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 257 | self.logger.error("Unhandled send error, length mismatch %d != %d" % |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 258 | (bytes, len(packet))) |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 259 | return bytes |
| 260 | |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 261 | def oldest_port_number(self): |
| 262 | """ |
| 263 | Returns the port number with the oldest packet, or |
| 264 | None if no packets are queued. |
| 265 | """ |
| 266 | min_port_number = None |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 267 | min_time = float('inf') |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 268 | for (port_number, queue) in self.packet_queues.items(): |
| 269 | if queue and queue[0][1] < min_time: |
| 270 | min_time = queue[0][1] |
| 271 | min_port_number = port_number |
| 272 | return min_port_number |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 273 | |
| 274 | # Dequeues and yields packets in the order they were received. |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 275 | # Yields (port number, packet, received time). |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 276 | # If port_number is not specified yields packets from all ports. |
| 277 | def packets(self, port_number=None): |
| 278 | while True: |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 279 | rcv_port_number = port_number or self.oldest_port_number() |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 280 | |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 281 | if rcv_port_number == None: |
| 282 | self.logger.debug("Out of packets on all ports") |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 283 | break |
| 284 | |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 285 | queue = self.packet_queues[rcv_port_number] |
| 286 | |
| 287 | if len(queue) == 0: |
| 288 | self.logger.debug("Out of packets on port %d", rcv_port_number) |
| 289 | break |
| 290 | |
| 291 | pkt, time = queue.pop(0) |
| 292 | yield (rcv_port_number, pkt, time) |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 293 | |
Rich Lane | 8806bc4 | 2012-07-26 19:18:37 -0700 | [diff] [blame] | 294 | def poll(self, port_number=None, timeout=-1, exp_pkt=None): |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 295 | """ |
| 296 | Poll one or all dataplane ports for a packet |
| 297 | |
| 298 | If port_number is given, get the oldest packet from that port. |
| 299 | Otherwise, find the port with the oldest packet and return |
| 300 | that packet. |
Dan Talayco | 1729fdb | 2012-05-03 09:35:56 -0700 | [diff] [blame] | 301 | |
| 302 | If exp_pkt is true, discard all packets until that one is found |
| 303 | |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 304 | @param port_number If set, get packet from this port |
Dan Talayco | 11c26e7 | 2010-03-07 22:03:57 -0800 | [diff] [blame] | 305 | @param timeout If positive and no packet is available, block |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 306 | until a packet is received or for this many seconds |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 307 | @param exp_pkt If not None, look for this packet and ignore any |
Dan Talayco | 1729fdb | 2012-05-03 09:35:56 -0700 | [diff] [blame] | 308 | others received. Note that if port_number is None, all packets |
| 309 | from all ports will be discarded until the exp_pkt is found |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 310 | @return The triple port_number, packet, pkt_time where packet |
| 311 | is received from port_number at time pkt_time. If a timeout |
| 312 | occurs, return None, None, None |
| 313 | """ |
| 314 | |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 315 | if exp_pkt and not port_number: |
Dan Talayco | 1729fdb | 2012-05-03 09:35:56 -0700 | [diff] [blame] | 316 | self.logger.warn("Dataplane poll with exp_pkt but no port number") |
| 317 | |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 318 | # Retrieve the packet. Returns (port number, packet, time). |
| 319 | def grab(): |
| 320 | self.logger.debug("Grabbing packet") |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 321 | for (rcv_port_number, pkt, time) in self.packets(port_number): |
| 322 | self.logger.debug("Checking packet from port %d", rcv_port_number) |
Dan Talayco | 1729fdb | 2012-05-03 09:35:56 -0700 | [diff] [blame] | 323 | if not exp_pkt or match_exp_pkt(exp_pkt, pkt): |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 324 | return (rcv_port_number, pkt, time) |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 325 | self.logger.debug("Did not find packet") |
| 326 | return None |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 327 | |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 328 | with self.cvar: |
Rich Lane | cd97d3d | 2013-01-07 18:50:06 -0800 | [diff] [blame] | 329 | ret = ofutils.timed_wait(self.cvar, grab, timeout=timeout) |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 330 | |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 331 | if ret != None: |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 332 | return ret |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 333 | else: |
| 334 | self.logger.debug("Poll time out, no packet from " + str(port_number)) |
| 335 | return (None, None, None) |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 336 | |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 337 | def kill(self): |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 338 | """ |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 339 | Stop the dataplane thread. |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 340 | """ |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 341 | self.killed = True |
| 342 | self.waker.notify() |
| 343 | self.join() |
| 344 | # Explicitly release ports to ensure we don't run out of sockets |
| 345 | # even if someone keeps holding a reference to the dataplane. |
| 346 | del self.ports |
Rich Lane | 4d46dbd | 2012-12-22 19:26:19 -0800 | [diff] [blame] | 347 | |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 348 | def port_down(self, port_number): |
ShreyaPandita | cd8e1cf | 2012-11-28 11:44:42 -0500 | [diff] [blame] | 349 | """Brings the specified port down""" |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 350 | self.ports[port_number].down() |
ShreyaPandita | cd8e1cf | 2012-11-28 11:44:42 -0500 | [diff] [blame] | 351 | |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 352 | def port_up(self, port_number): |
ShreyaPandita | cd8e1cf | 2012-11-28 11:44:42 -0500 | [diff] [blame] | 353 | """Brings the specified port up""" |
Rich Lane | e1b8da9 | 2012-12-26 22:47:13 -0800 | [diff] [blame] | 354 | self.ports[port_number].up() |
Rich Lane | 2c7812c | 2012-12-27 17:52:23 -0800 | [diff] [blame] | 355 | |
| 356 | def flush(self): |
| 357 | """ |
| 358 | Drop any queued packets. |
| 359 | """ |
| 360 | for port_number in self.packet_queues.keys(): |
| 361 | self.packet_queues[port_number] = [] |
Rich Lane | 472aaea | 2013-08-27 09:27:38 -0700 | [diff] [blame] | 362 | |
| 363 | def start_pcap(self, filename): |
| 364 | assert(self.pcap_writer == None) |
| 365 | self.pcap_writer = PcapWriter(filename) |
| 366 | |
| 367 | def stop_pcap(self): |
| 368 | if self.pcap_writer: |
| 369 | self.pcap_writer.close() |
| 370 | self.pcap_writer = None |