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 |
Dan Talayco | d7e2dbe | 2010-02-13 21:51:15 -0800 | [diff] [blame] | 21 | import netutils |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 22 | from threading import Thread |
| 23 | from threading import Lock |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 24 | from threading import Condition |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 25 | import select |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 26 | import logging |
| 27 | from oft_assert import oft_assert |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 28 | from ofutils import * |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 29 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 30 | ##@todo Find a better home for these identifiers (dataplane) |
| 31 | RCV_SIZE_DEFAULT = 4096 |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 32 | ETH_P_ALL = 0x03 |
| 33 | RCV_TIMEOUT = 10000 |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [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 | |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 49 | class DataPlanePort(Thread): |
| 50 | """ |
| 51 | Class defining a port monitoring object. |
| 52 | |
| 53 | Control a dataplane port connected to the switch under test. |
| 54 | Creates a promiscuous socket on a physical interface. |
| 55 | Queues the packets received on that interface with time stamps. |
| 56 | Inherits from Thread class as meant to run in background. Also |
| 57 | supports polling. |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 58 | |
| 59 | Currently assumes a controlling 'parent' which maintains a |
| 60 | common Lock object and a total packet-pending count. May want |
| 61 | to decouple that some day. |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 62 | """ |
| 63 | |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 64 | def __init__(self, interface_name, port_number, parent, max_pkts=1024): |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 65 | """ |
| 66 | Set up a port monitor object |
| 67 | @param interface_name The name of the physical interface like eth1 |
Dan Talayco | 4d06597 | 2010-02-18 23:11:32 -0800 | [diff] [blame] | 68 | @param port_number The port number associated with this port |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 69 | @param parent The controlling dataplane object; for pkt wait CV |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 70 | @param max_pkts Maximum number of pkts to keep in queue |
| 71 | """ |
| 72 | Thread.__init__(self) |
| 73 | self.interface_name = interface_name |
| 74 | self.max_pkts = max_pkts |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 75 | self.packets_total = 0 |
| 76 | self.packets = [] |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 77 | self.packets_discarded = 0 |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 78 | self.port_number = port_number |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 79 | logname = "dp-" + interface_name |
| 80 | self.logger = logging.getLogger(logname) |
Dan Talayco | 0db53eb | 2010-03-10 14:00:02 -0800 | [diff] [blame] | 81 | try: |
| 82 | self.socket = self.interface_open(interface_name) |
| 83 | except: |
| 84 | self.logger.info("Could not open socket") |
| 85 | sys.exit(1) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 86 | self.logger.info("Openned port monitor socket") |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 87 | self.parent = parent |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 88 | |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 89 | def interface_open(self, interface_name): |
| 90 | """ |
| 91 | Open a socket in a promiscuous mode for a data connection. |
| 92 | @param interface_name port name as a string such as 'eth1' |
| 93 | @retval s socket |
| 94 | """ |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 95 | s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 96 | socket.htons(ETH_P_ALL)) |
| 97 | s.bind((interface_name, 0)) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 98 | netutils.set_promisc(s, interface_name) |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 99 | s.settimeout(RCV_TIMEOUT) |
| 100 | return s |
| 101 | |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 102 | def run(self): |
| 103 | """ |
| 104 | Activity function for class |
| 105 | """ |
| 106 | self.running = True |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 107 | self.socs = [self.socket] |
| 108 | error_warned = False # Have we warned about error? |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 109 | while self.running: |
| 110 | try: |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 111 | sel_in, sel_out, sel_err = \ |
| 112 | select.select(self.socs, [], [], 1) |
| 113 | except: |
| 114 | print sys.exc_info() |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 115 | self.logger.error("Select error, exiting") |
| 116 | break |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 117 | |
| 118 | if not self.running: |
| 119 | break |
| 120 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 121 | if (sel_in is None) or (len(sel_in) == 0): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 122 | continue |
| 123 | |
| 124 | try: |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 125 | rcvmsg = self.socket.recv(RCV_SIZE_DEFAULT) |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 126 | except socket.error: |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 127 | if not error_warned: |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 128 | self.logger.info("Socket error on recv") |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 129 | error_warned = True |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 130 | continue |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 131 | |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 132 | if len(rcvmsg) == 0: |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 133 | self.logger.info("Zero len pkt rcvd") |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 134 | self.kill() |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 135 | break |
| 136 | |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 137 | rcvtime = time.clock() |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 138 | self.logger.debug("Pkt len " + str(len(rcvmsg)) + |
Ed Swierk | 4e20030 | 2012-03-19 14:53:31 -0700 | [diff] [blame] | 139 | " in at " + str(rcvtime) + " on port " + |
| 140 | str(self.port_number)) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 141 | |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 142 | # Enqueue packet |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 143 | with self.parent.pkt_sync: |
| 144 | if len(self.packets) >= self.max_pkts: |
| 145 | # Queue full, throw away oldest |
| 146 | self.packets.pop(0) |
| 147 | self.packets_discarded += 1 |
| 148 | self.logger.debug("Discarding oldest packet to make room") |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 149 | self.packets.append((rcvmsg, rcvtime)) |
| 150 | self.packets_total += 1 |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 151 | self.parent.pkt_sync.notify_all() |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 152 | |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 153 | self.logger.info("Thread exit") |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 154 | |
| 155 | def kill(self): |
| 156 | """ |
| 157 | Terminate the running thread |
| 158 | """ |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 159 | self.logger.debug("Port monitor kill") |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 160 | self.running = False |
| 161 | try: |
| 162 | self.socket.close() |
| 163 | except: |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 164 | self.logger.info("Ignoring dataplane soc shutdown error") |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 165 | |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 166 | def timestamp_head(self): |
| 167 | """ |
| 168 | Return the timestamp of the head of queue or None if empty |
| 169 | """ |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 170 | rv = None |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 171 | try: |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 172 | rv = self.packets[0][1] |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 173 | except: |
| 174 | rv = None |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 175 | return rv |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 176 | |
| 177 | def flush(self): |
| 178 | """ |
| 179 | Clear the packet queue |
| 180 | """ |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 181 | with self.parent.pkt_sync: |
| 182 | self.packets_discarded += len(self.packets) |
| 183 | self.packets = [] |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 184 | |
| 185 | def send(self, packet): |
| 186 | """ |
| 187 | Send a packet to the dataplane port |
| 188 | @param packet The packet data to send to the port |
| 189 | @retval The number of bytes sent |
| 190 | """ |
| 191 | return self.socket.send(packet) |
| 192 | |
| 193 | |
| 194 | def register(self, handler): |
| 195 | """ |
| 196 | Register a callback function to receive packets from this |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 197 | port. The callback will be passed the packet, the |
| 198 | interface name and the port number (if set) on which the |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 199 | packet was received. |
| 200 | |
| 201 | To be implemented |
| 202 | """ |
| 203 | pass |
| 204 | |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 205 | def show(self, prefix=''): |
| 206 | print prefix + "Name: " + self.interface_name |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 207 | print prefix + "Pkts pending: " + str(len(self.packets)) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 208 | print prefix + "Pkts total: " + str(self.packets_total) |
| 209 | print prefix + "socket: " + str(self.socket) |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 210 | |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 211 | |
| 212 | class DataPlane: |
| 213 | """ |
| 214 | Class defining access primitives to the data plane |
| 215 | Controls a list of DataPlanePort objects |
| 216 | """ |
Jeffrey Townsend | 0e8b092 | 2012-07-11 11:37:46 -0700 | [diff] [blame] | 217 | def __init__(self, config=None): |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 218 | self.port_list = {} |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 219 | # pkt_sync serves double duty as a regular top level lock and |
| 220 | # as a condition variable |
| 221 | self.pkt_sync = Condition() |
| 222 | |
| 223 | # These are used to signal async pkt arrival for polling |
| 224 | self.want_pkt = False |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 225 | self.exp_pkt = None |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 226 | self.want_pkt_port = None # What port required (or None) |
| 227 | self.got_pkt_port = None # On what port received? |
| 228 | self.packets_pending = 0 # Total pkts in all port queues |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 229 | self.logger = logging.getLogger("dataplane") |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 230 | |
Jeffrey Townsend | 0e8b092 | 2012-07-11 11:37:46 -0700 | [diff] [blame] | 231 | if config is None: |
| 232 | self.config = {} |
| 233 | else: |
| 234 | self.config = config; |
| 235 | |
| 236 | ############################################################ |
| 237 | # |
| 238 | # We use the DataPlanePort class defined here by |
| 239 | # default for all port traffic: |
| 240 | # |
| 241 | self.dppclass = DataPlanePort |
| 242 | |
| 243 | ############################################################ |
| 244 | # |
| 245 | # The platform/config can provide a custom DataPlanePort class |
| 246 | # here if you have a custom implementation with different |
| 247 | # behavior. |
| 248 | # |
| 249 | # Set config.dataplane.portclass = MyDataPlanePortClass |
| 250 | # where MyDataPlanePortClass has the same interface as the class |
| 251 | # DataPlanePort defined here. |
| 252 | # |
| 253 | if "dataplane" in self.config: |
| 254 | if "portclass" in self.config["dataplane"]: |
| 255 | self.dppclass = self.config["dataplane"]["portclass"] |
| 256 | |
| 257 | if self.dppclass == None: |
| 258 | raise Exception("Problem determining DataPlanePort class.") |
| 259 | |
| 260 | |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 261 | def port_add(self, interface_name, port_number): |
| 262 | """ |
| 263 | Add a port to the dataplane |
| 264 | TBD: Max packets for queue? |
| 265 | @param interface_name The name of the physical interface like eth1 |
| 266 | @param port_number The port number used to refer to the port |
| 267 | """ |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 268 | |
Jeffrey Townsend | 0e8b092 | 2012-07-11 11:37:46 -0700 | [diff] [blame] | 269 | self.port_list[port_number] = self.dppclass(interface_name, |
| 270 | port_number, self); |
| 271 | |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 272 | self.port_list[port_number].start() |
| 273 | |
Jeffrey Townsend | 0e8b092 | 2012-07-11 11:37:46 -0700 | [diff] [blame] | 274 | |
| 275 | |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 276 | def send(self, port_number, packet): |
| 277 | """ |
| 278 | Send a packet to the given port |
| 279 | @param port_number The port to send the data to |
| 280 | @param packet Raw packet data to send to port |
| 281 | """ |
Dan Talayco | 11c26e7 | 2010-03-07 22:03:57 -0800 | [diff] [blame] | 282 | self.logger.debug("Sending %d bytes to port %d" % |
| 283 | (len(packet), port_number)) |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 284 | bytes = self.port_list[port_number].send(packet) |
| 285 | if bytes != len(packet): |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 286 | self.logger.error("Unhandled send error, length mismatch %d != %d" % |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 287 | (bytes, len(packet))) |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 288 | return bytes |
| 289 | |
| 290 | def flood(self, packet): |
| 291 | """ |
| 292 | Send a packet to all ports |
| 293 | @param packet Raw packet data to send to port |
| 294 | """ |
| 295 | for port_number in self.port_list.keys(): |
| 296 | bytes = self.port_list[port_number].send(packet) |
| 297 | if bytes != len(packet): |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 298 | self.logger.error("Unhandled send error" + |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 299 | ", port %d, length mismatch %d != %d" % |
| 300 | (port_number, bytes, len(packet))) |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 301 | |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 302 | # Returns the port with the oldest packet, or None if no packets are queued. |
| 303 | def oldest_port(self): |
| 304 | min_port = None |
| 305 | min_time = float('inf') |
| 306 | for port in self.port_list.values(): |
| 307 | ptime = port.timestamp_head() |
| 308 | if ptime and ptime < min_time: |
| 309 | min_time = ptime |
| 310 | min_port = port |
| 311 | return min_port |
| 312 | |
| 313 | # Dequeues and yields packets in the order they were received. |
| 314 | # Yields (port, packet, received time). |
| 315 | # If port_number is not specified yields packets from all ports. |
| 316 | def packets(self, port_number=None): |
| 317 | while True: |
| 318 | if port_number == None: |
| 319 | port = self.oldest_port() |
| 320 | else: |
| 321 | port = self.port_list[port_number] |
| 322 | |
| 323 | if port == None or len(port.packets) == 0: |
| 324 | self.logger.debug("Out of packets for port %s" % str(port_number)) |
| 325 | # Out of packets |
| 326 | break |
| 327 | |
| 328 | pkt, time = port.packets.pop(0) |
| 329 | yield (port, pkt, time) |
| 330 | |
Rich Lane | 8806bc4 | 2012-07-26 19:18:37 -0700 | [diff] [blame] | 331 | def poll(self, port_number=None, timeout=-1, exp_pkt=None): |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 332 | """ |
| 333 | Poll one or all dataplane ports for a packet |
| 334 | |
| 335 | If port_number is given, get the oldest packet from that port. |
| 336 | Otherwise, find the port with the oldest packet and return |
| 337 | that packet. |
Dan Talayco | 1729fdb | 2012-05-03 09:35:56 -0700 | [diff] [blame] | 338 | |
| 339 | If exp_pkt is true, discard all packets until that one is found |
| 340 | |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 341 | @param port_number If set, get packet from this port |
Dan Talayco | 11c26e7 | 2010-03-07 22:03:57 -0800 | [diff] [blame] | 342 | @param timeout If positive and no packet is available, block |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 343 | until a packet is received or for this many seconds |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 344 | @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] | 345 | others received. Note that if port_number is None, all packets |
| 346 | from all ports will be discarded until the exp_pkt is found |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 347 | @return The triple port_number, packet, pkt_time where packet |
| 348 | is received from port_number at time pkt_time. If a timeout |
| 349 | occurs, return None, None, None |
| 350 | """ |
| 351 | |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 352 | if exp_pkt and not port_number: |
Dan Talayco | 1729fdb | 2012-05-03 09:35:56 -0700 | [diff] [blame] | 353 | self.logger.warn("Dataplane poll with exp_pkt but no port number") |
| 354 | |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 355 | # Retrieve the packet. Returns (port number, packet, time). |
| 356 | def grab(): |
| 357 | self.logger.debug("Grabbing packet") |
| 358 | for (port, pkt, time) in self.packets(port_number): |
| 359 | self.logger.debug("Checking packet from port %d" % port.port_number) |
Dan Talayco | 1729fdb | 2012-05-03 09:35:56 -0700 | [diff] [blame] | 360 | if not exp_pkt or match_exp_pkt(exp_pkt, pkt): |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 361 | return (port, pkt, time) |
| 362 | self.logger.debug("Did not find packet") |
| 363 | return None |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 364 | |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 365 | with self.pkt_sync: |
| 366 | ret = timed_wait(self.pkt_sync, grab, timeout=timeout) |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 367 | |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 368 | if ret != None: |
| 369 | (port, pkt, time) = ret |
| 370 | return (port.port_number, pkt, time) |
| 371 | else: |
| 372 | self.logger.debug("Poll time out, no packet from " + str(port_number)) |
| 373 | return (None, None, None) |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 374 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 375 | def kill(self, join_threads=True): |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 376 | """ |
| 377 | Close all sockets for dataplane |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 378 | @param join_threads If True call join on each thread |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 379 | """ |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 380 | for port_number in self.port_list.keys(): |
| 381 | self.port_list[port_number].kill() |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 382 | if join_threads: |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 383 | self.logger.debug("Joining " + str(port_number)) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 384 | self.port_list[port_number].join() |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 385 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 386 | self.logger.info("DataPlane shutdown") |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 387 | |
| 388 | def show(self, prefix=''): |
| 389 | print prefix + "Dataplane Controller" |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 390 | print prefix + "Packets pending" + str(self.packets_pending) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 391 | for pnum, port in self.port_list.items(): |
| 392 | print prefix + "OpenFlow Port Number " + str(pnum) |
| 393 | port.show(prefix + ' ') |
| 394 | |