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 |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 28 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 29 | ##@todo Find a better home for these identifiers (dataplane) |
| 30 | RCV_SIZE_DEFAULT = 4096 |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 31 | ETH_P_ALL = 0x03 |
| 32 | RCV_TIMEOUT = 10000 |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 33 | |
Ed Swierk | 506614a | 2012-03-29 08:16:59 -0700 | [diff] [blame] | 34 | def match_exp_pkt(exp_pkt, pkt): |
| 35 | """ |
| 36 | Compare the string value of pkt with the string value of exp_pkt, |
| 37 | and return True iff they are identical. If the length of exp_pkt is |
| 38 | less than the minimum Ethernet frame size (60 bytes), then padding |
| 39 | bytes in pkt are ignored. |
| 40 | """ |
| 41 | e = str(exp_pkt) |
| 42 | p = str(pkt) |
| 43 | if len(e) < 60: |
| 44 | p = p[:len(e)] |
| 45 | return e == p |
| 46 | |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 47 | class DataPlanePort(Thread): |
| 48 | """ |
| 49 | Class defining a port monitoring object. |
| 50 | |
| 51 | Control a dataplane port connected to the switch under test. |
| 52 | Creates a promiscuous socket on a physical interface. |
| 53 | Queues the packets received on that interface with time stamps. |
| 54 | Inherits from Thread class as meant to run in background. Also |
| 55 | supports polling. |
| 56 | Use accessors to dequeue packets for proper synchronization. |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 57 | |
| 58 | Currently assumes a controlling 'parent' which maintains a |
| 59 | common Lock object and a total packet-pending count. May want |
| 60 | to decouple that some day. |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 61 | """ |
| 62 | |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 63 | def __init__(self, interface_name, port_number, parent, max_pkts=1024): |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 64 | """ |
| 65 | Set up a port monitor object |
| 66 | @param interface_name The name of the physical interface like eth1 |
Dan Talayco | 4d06597 | 2010-02-18 23:11:32 -0800 | [diff] [blame] | 67 | @param port_number The port number associated with this port |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 68 | @param parent The controlling dataplane object; for pkt wait CV |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 69 | @param max_pkts Maximum number of pkts to keep in queue |
| 70 | """ |
| 71 | Thread.__init__(self) |
| 72 | self.interface_name = interface_name |
| 73 | self.max_pkts = max_pkts |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 74 | self.packets_total = 0 |
| 75 | self.packets = [] |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 76 | self.packets_discarded = 0 |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 77 | self.port_number = port_number |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 78 | logname = "dp-" + interface_name |
| 79 | self.logger = logging.getLogger(logname) |
Dan Talayco | 0db53eb | 2010-03-10 14:00:02 -0800 | [diff] [blame] | 80 | try: |
| 81 | self.socket = self.interface_open(interface_name) |
| 82 | except: |
| 83 | self.logger.info("Could not open socket") |
| 84 | sys.exit(1) |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 85 | self.logger.info("Openned port monitor socket") |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 86 | self.parent = parent |
| 87 | self.pkt_sync = self.parent.pkt_sync |
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 |
| 143 | self.pkt_sync.acquire() |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 144 | if len(self.packets) >= self.max_pkts: |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 145 | # Queue full, throw away oldest |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 146 | self.packets.pop(0) |
| 147 | self.packets_discarded += 1 |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 148 | else: |
| 149 | self.parent.packets_pending += 1 |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 150 | # Check if parent is waiting on this (or any) port |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 151 | drop_pkt = False |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 152 | if self.parent.want_pkt: |
| 153 | if (not self.parent.want_pkt_port or |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 154 | self.parent.want_pkt_port == self.port_number): |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 155 | if self.parent.exp_pkt: |
Ed Swierk | 506614a | 2012-03-29 08:16:59 -0700 | [diff] [blame] | 156 | if not match_exp_pkt(self.parent.exp_pkt, rcvmsg): |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 157 | drop_pkt = True |
| 158 | if not drop_pkt: |
| 159 | self.parent.got_pkt_port = self.port_number |
| 160 | self.parent.want_pkt = False |
| 161 | self.parent.pkt_sync.notify() |
| 162 | if not drop_pkt: |
| 163 | self.packets.append((rcvmsg, rcvtime)) |
| 164 | self.packets_total += 1 |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 165 | self.pkt_sync.release() |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 166 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 167 | self.logger.info("Thread exit ") |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 168 | |
| 169 | def kill(self): |
| 170 | """ |
| 171 | Terminate the running thread |
| 172 | """ |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 173 | self.logger.debug("Port monitor kill") |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 174 | self.running = False |
| 175 | try: |
| 176 | self.socket.close() |
| 177 | except: |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 178 | self.logger.info("Ignoring dataplane soc shutdown error") |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 179 | |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 180 | def dequeue(self, use_lock=True): |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 181 | """ |
| 182 | Get the oldest packet in the queue |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 183 | @param use_lock If True, acquires the packet sync lock (which is |
| 184 | really the parent's lock) |
| 185 | @return The pair packet, packet time-stamp |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 186 | """ |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 187 | if use_lock: |
| 188 | self.pkt_sync.acquire() |
| 189 | if len(self.packets) > 0: |
| 190 | pkt, pkt_time = self.packets.pop(0) |
| 191 | self.parent.packets_pending -= 1 |
| 192 | else: |
| 193 | pkt = pkt_time = None |
| 194 | if use_lock: |
| 195 | self.pkt_sync.release() |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 196 | return pkt, pkt_time |
| 197 | |
| 198 | def timestamp_head(self): |
| 199 | """ |
| 200 | Return the timestamp of the head of queue or None if empty |
| 201 | """ |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 202 | rv = None |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 203 | try: |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 204 | rv = self.packets[0][1] |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 205 | except: |
| 206 | rv = None |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 207 | return rv |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 208 | |
| 209 | def flush(self): |
| 210 | """ |
| 211 | Clear the packet queue |
| 212 | """ |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 213 | self.pkt_sync.acquire() |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 214 | self.packets_discarded += len(self.packets) |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 215 | self.parent.packets_pending -= len(self.packets) |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 216 | self.packets = [] |
| 217 | self.packet_times = [] |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 218 | self.pkt_sync.release() |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 219 | |
| 220 | |
| 221 | def send(self, packet): |
| 222 | """ |
| 223 | Send a packet to the dataplane port |
| 224 | @param packet The packet data to send to the port |
| 225 | @retval The number of bytes sent |
| 226 | """ |
| 227 | return self.socket.send(packet) |
| 228 | |
| 229 | |
| 230 | def register(self, handler): |
| 231 | """ |
| 232 | Register a callback function to receive packets from this |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 233 | port. The callback will be passed the packet, the |
| 234 | interface name and the port number (if set) on which the |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 235 | packet was received. |
| 236 | |
| 237 | To be implemented |
| 238 | """ |
| 239 | pass |
| 240 | |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 241 | def show(self, prefix=''): |
| 242 | print prefix + "Name: " + self.interface_name |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 243 | print prefix + "Pkts pending: " + str(len(self.packets)) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 244 | print prefix + "Pkts total: " + str(self.packets_total) |
| 245 | print prefix + "socket: " + str(self.socket) |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 246 | |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 247 | |
| 248 | class DataPlane: |
| 249 | """ |
| 250 | Class defining access primitives to the data plane |
| 251 | Controls a list of DataPlanePort objects |
| 252 | """ |
| 253 | def __init__(self): |
| 254 | self.port_list = {} |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 255 | # pkt_sync serves double duty as a regular top level lock and |
| 256 | # as a condition variable |
| 257 | self.pkt_sync = Condition() |
| 258 | |
| 259 | # These are used to signal async pkt arrival for polling |
| 260 | self.want_pkt = False |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 261 | self.exp_pkt = None |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 262 | self.want_pkt_port = None # What port required (or None) |
| 263 | self.got_pkt_port = None # On what port received? |
| 264 | self.packets_pending = 0 # Total pkts in all port queues |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 265 | self.logger = logging.getLogger("dataplane") |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 266 | |
| 267 | def port_add(self, interface_name, port_number): |
| 268 | """ |
| 269 | Add a port to the dataplane |
| 270 | TBD: Max packets for queue? |
| 271 | @param interface_name The name of the physical interface like eth1 |
| 272 | @param port_number The port number used to refer to the port |
| 273 | """ |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 274 | |
Dan Talayco | 11c26e7 | 2010-03-07 22:03:57 -0800 | [diff] [blame] | 275 | self.port_list[port_number] = DataPlanePort(interface_name, |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 276 | port_number, self) |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 277 | self.port_list[port_number].start() |
| 278 | |
| 279 | def send(self, port_number, packet): |
| 280 | """ |
| 281 | Send a packet to the given port |
| 282 | @param port_number The port to send the data to |
| 283 | @param packet Raw packet data to send to port |
| 284 | """ |
Dan Talayco | 11c26e7 | 2010-03-07 22:03:57 -0800 | [diff] [blame] | 285 | self.logger.debug("Sending %d bytes to port %d" % |
| 286 | (len(packet), port_number)) |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 287 | bytes = self.port_list[port_number].send(packet) |
| 288 | if bytes != len(packet): |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 289 | self.logger.error("Unhandled send error, length mismatch %d != %d" % |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 290 | (bytes, len(packet))) |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 291 | return bytes |
| 292 | |
| 293 | def flood(self, packet): |
| 294 | """ |
| 295 | Send a packet to all ports |
| 296 | @param packet Raw packet data to send to port |
| 297 | """ |
| 298 | for port_number in self.port_list.keys(): |
| 299 | bytes = self.port_list[port_number].send(packet) |
| 300 | if bytes != len(packet): |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 301 | self.logger.error("Unhandled send error" + |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 302 | ", port %d, length mismatch %d != %d" % |
| 303 | (port_number, bytes, len(packet))) |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 304 | |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 305 | def _oldest_packet_find(self): |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 306 | # Find port with oldest packet |
| 307 | min_time = 0 |
| 308 | min_port = -1 |
| 309 | for port_number in self.port_list.keys(): |
| 310 | ptime = self.port_list[port_number].timestamp_head() |
| 311 | if ptime: |
| 312 | if (min_port == -1) or (ptime < min_time): |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 313 | min_time = ptime |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 314 | min_port = port_number |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 315 | oft_assert(min_port != -1, "Could not find port when pkts pending") |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 316 | |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 317 | return min_port |
| 318 | |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 319 | def poll(self, port_number=None, timeout=None, exp_pkt=None): |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 320 | """ |
| 321 | Poll one or all dataplane ports for a packet |
| 322 | |
| 323 | If port_number is given, get the oldest packet from that port. |
| 324 | Otherwise, find the port with the oldest packet and return |
| 325 | that packet. |
| 326 | @param port_number If set, get packet from this port |
Dan Talayco | 11c26e7 | 2010-03-07 22:03:57 -0800 | [diff] [blame] | 327 | @param timeout If positive and no packet is available, block |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 328 | until a packet is received or for this many seconds |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 329 | @param exp_pkt If not None, look for this packet and ignore any |
| 330 | others received. Requires port_number to be specified |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 331 | @return The triple port_number, packet, pkt_time where packet |
| 332 | is received from port_number at time pkt_time. If a timeout |
| 333 | occurs, return None, None, None |
| 334 | """ |
| 335 | |
| 336 | self.pkt_sync.acquire() |
| 337 | |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 338 | if exp_pkt and not port_number: |
| 339 | print "WARNING: Dataplane poll: exp_pkt without port number" |
| 340 | |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 341 | # Check if requested specific port and it has a packet |
| 342 | if port_number and len(self.port_list[port_number].packets) != 0: |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 343 | while len(self.port_list[port_number].packets) != 0: |
| 344 | pkt, time = self.port_list[port_number].dequeue(use_lock=False) |
| 345 | if not exp_pkt: |
| 346 | break |
Ed Swierk | 506614a | 2012-03-29 08:16:59 -0700 | [diff] [blame] | 347 | if match_exp_pkt(exp_pkt, pkt): |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 348 | break |
| 349 | pkt = None # Discard silently |
| 350 | if pkt: |
| 351 | self.pkt_sync.release() |
| 352 | oft_assert(pkt, "Poll: packet not found on port " + |
| 353 | str(port_number)) |
| 354 | return port_number, pkt, time |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 355 | |
| 356 | # Check if requested any port and some packet pending |
| 357 | if not port_number and self.packets_pending != 0: |
| 358 | port = self._oldest_packet_find() |
| 359 | pkt, time = self.port_list[port].dequeue(use_lock=False) |
| 360 | self.pkt_sync.release() |
| 361 | oft_assert(pkt, "Poll: oldest packet not found") |
| 362 | return port, pkt, time |
| 363 | |
| 364 | # No packet pending; blocking call requested? |
| 365 | if not timeout: |
| 366 | self.pkt_sync.release() |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 367 | return None, None, None |
| 368 | |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 369 | # Desired packet isn't available and timeout is specified |
| 370 | # Already holding pkt_sync; wait on pkt_sync variable |
| 371 | self.want_pkt = True |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 372 | self.exp_pkt = exp_pkt |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 373 | self.want_pkt_port = port_number |
| 374 | self.got_pkt_port = None |
| 375 | self.pkt_sync.wait(timeout) |
| 376 | self.want_pkt = False |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 377 | self.exp_pkt = None |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 378 | if self.got_pkt_port: |
| 379 | pkt, time = \ |
| 380 | self.port_list[self.got_pkt_port].dequeue(use_lock=False) |
| 381 | self.pkt_sync.release() |
| 382 | oft_assert(pkt, "Poll: pkt reported, but not found at " + |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 383 | str(self.got_pkt_port)) |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 384 | return self.got_pkt_port, pkt, time |
| 385 | |
| 386 | self.pkt_sync.release() |
Dan Talayco | a99c21a | 2010-05-07 09:23:34 -0700 | [diff] [blame] | 387 | self.logger.debug("Poll time out, no packet from " + str(port_number)) |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 388 | |
| 389 | return None, None, None |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 390 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 391 | def kill(self, join_threads=True): |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 392 | """ |
| 393 | Close all sockets for dataplane |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 394 | @param join_threads If True call join on each thread |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 395 | """ |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 396 | for port_number in self.port_list.keys(): |
| 397 | self.port_list[port_number].kill() |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 398 | if join_threads: |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 399 | self.logger.debug("Joining " + str(port_number)) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 400 | self.port_list[port_number].join() |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 401 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 402 | self.logger.info("DataPlane shutdown") |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 403 | |
| 404 | def show(self, prefix=''): |
| 405 | print prefix + "Dataplane Controller" |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 406 | print prefix + "Packets pending" + str(self.packets_pending) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 407 | for pnum, port in self.port_list.items(): |
| 408 | print prefix + "OpenFlow Port Number " + str(pnum) |
| 409 | port.show(prefix + ' ') |
| 410 | |