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