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