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 |
| 10 | a set of those objects allowing general calls and parsing |
| 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 | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 24 | from oft_config import * |
| 25 | import select |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 26 | |
| 27 | #@todo Move these identifiers into config |
| 28 | ETH_P_ALL = 0x03 |
| 29 | RCV_TIMEOUT = 10000 |
| 30 | RCV_SIZE = 4096 |
| 31 | |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 32 | # class packet_queue: |
| 33 | # """ |
| 34 | # Class defining a packet queue across multiple ports |
| 35 | |
| 36 | # Items in the queue are stored as a triple (port number, pkt, pkt in time) |
| 37 | # """ |
| 38 | |
| 39 | # def __init__(self, max_pkts=1024): |
| 40 | # self.sync = Lock() |
| 41 | # self.debug_level = debug_level_default |
| 42 | # self.packets = [] |
| 43 | # self.max_pkts = max_pkts |
| 44 | # self.packets_total = 0 |
| 45 | # self.packets_discarded = 0 |
| 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. |
| 57 | """ |
| 58 | |
| 59 | def __init__(self, interface_name, max_pkts=1024): |
| 60 | """ |
| 61 | Set up a port monitor object |
| 62 | @param interface_name The name of the physical interface like eth1 |
| 63 | @param max_pkts Maximum number of pkts to keep in queue |
| 64 | """ |
| 65 | Thread.__init__(self) |
| 66 | self.interface_name = interface_name |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 67 | self.debug_level = debug_level_default |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 68 | self.max_pkts = max_pkts |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 69 | self.packets_total = 0 |
| 70 | self.packets = [] |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 71 | self.packets_discarded = 0 |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 72 | self.sync = Lock() |
| 73 | self.socket = self.interface_open(interface_name) |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 74 | self.dbg(DEBUG_INFO, "Openned port monitor socket") |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 75 | |
| 76 | def dbg(self, level, string): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 77 | debug_log("DPLANE", self.debug_level, level, |
| 78 | self.interface_name + ": " + string) |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 79 | |
| 80 | def interface_open(self, interface_name): |
| 81 | """ |
| 82 | Open a socket in a promiscuous mode for a data connection. |
| 83 | @param interface_name port name as a string such as 'eth1' |
| 84 | @retval s socket |
| 85 | """ |
| 86 | s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, |
| 87 | socket.htons(ETH_P_ALL)) |
| 88 | s.bind((interface_name, 0)) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 89 | netutils.set_promisc(s, interface_name) |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 90 | s.settimeout(RCV_TIMEOUT) |
| 91 | return s |
| 92 | |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 93 | def run(self): |
| 94 | """ |
| 95 | Activity function for class |
| 96 | """ |
| 97 | self.running = True |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 98 | self.socs = [self.socket] |
| 99 | error_warned = False # Have we warned about error? |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 100 | while self.running: |
| 101 | try: |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 102 | sel_in, sel_out, sel_err = \ |
| 103 | select.select(self.socs, [], [], 1) |
| 104 | except: |
| 105 | print sys.exc_info() |
| 106 | self.dbg(DEBUG_ERROR, "Select error, exiting") |
| 107 | sys.exit(1) |
| 108 | |
| 109 | #if not sel_err is None: |
| 110 | # self.dbg(DEBUG_VERBOSE, "Socket error from select set") |
| 111 | |
| 112 | if not self.running: |
| 113 | break |
| 114 | |
| 115 | if sel_in is None: |
| 116 | continue |
| 117 | |
| 118 | try: |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 119 | rcvmsg = self.socket.recv(RCV_SIZE) |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 120 | except socket.error: |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 121 | if not error_warned: |
| 122 | self.dbg(DEBUG_INFO, "Socket error on recv") |
| 123 | error_warned = True |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 124 | continue |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 125 | |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 126 | if len(rcvmsg) == 0: |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 127 | self.dbg(DEBUG_INFO, "Zero len pkt rcvd") |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 128 | self.kill() |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 129 | break |
| 130 | |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 131 | rcvtime = time.clock() |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 132 | self.dbg(DEBUG_VERBOSE, "Pkt len " + str(len(rcvmsg)) + |
| 133 | " in at " + str(rcvtime)) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 134 | |
| 135 | self.sync.acquire() |
| 136 | if len(self.packets) >= self.max_pkts: |
| 137 | self.packets.pop(0) |
| 138 | self.packets_discarded += 1 |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 139 | self.packets.append((rcvmsg, rcvtime)) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 140 | self.packets_total += 1 |
| 141 | self.sync.release() |
| 142 | |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 143 | self.dbg(DEBUG_INFO, "Thread exit ") |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 144 | |
| 145 | def kill(self): |
| 146 | """ |
| 147 | Terminate the running thread |
| 148 | """ |
| 149 | self.running = False |
| 150 | try: |
| 151 | self.socket.close() |
| 152 | except: |
| 153 | self.dbg(DEBUG_INFO, "Ignoring dataplane soc shutdown error") |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 154 | self.dbg(DEBUG_INFO, |
| 155 | "Port monitor exiting") |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 156 | |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 157 | def dequeue(self): |
| 158 | """ |
| 159 | Get the oldest packet in the queue |
| 160 | """ |
| 161 | self.sync.acquire() |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 162 | pkt, pkt_time = self.packets.pop(0) |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 163 | self.sync.release() |
| 164 | return pkt, pkt_time |
| 165 | |
| 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 |
| 171 | self.sync.acquire() |
| 172 | if len(self.packets) > 0: |
| 173 | rv = self.packets[0][1] |
| 174 | self.sync.release() |
| 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 | """ |
| 181 | self.sync.acquire() |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 182 | self.packets_discarded += len(self.packets) |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 183 | self.packets = [] |
| 184 | self.packet_times = [] |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 185 | self.sync.release() |
| 186 | |
| 187 | |
| 188 | def send(self, packet): |
| 189 | """ |
| 190 | Send a packet to the dataplane port |
| 191 | @param packet The packet data to send to the port |
| 192 | @retval The number of bytes sent |
| 193 | """ |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 194 | self.dbg(DEBUG_VERBOSE, |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 195 | "port sending " + str(len(packet)) + " bytes") |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 196 | return self.socket.send(packet) |
| 197 | |
| 198 | |
| 199 | def register(self, handler): |
| 200 | """ |
| 201 | Register a callback function to receive packets from this |
| 202 | port. The callback will be passed the packet, the |
| 203 | interface name and the port number (if set) on which the |
| 204 | packet was received. |
| 205 | |
| 206 | To be implemented |
| 207 | """ |
| 208 | pass |
| 209 | |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 210 | def show(self, prefix=''): |
| 211 | print prefix + "Name: " + self.interface_name |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 212 | print prefix + "Pkts pending: " + str(len(self.packets)) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 213 | print prefix + "Pkts total: " + str(self.packets_total) |
| 214 | print prefix + "socket: " + str(self.socket) |
| 215 | |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 216 | |
| 217 | class DataPlane: |
| 218 | """ |
| 219 | Class defining access primitives to the data plane |
| 220 | Controls a list of DataPlanePort objects |
| 221 | """ |
| 222 | def __init__(self): |
| 223 | self.port_list = {} |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 224 | self.debug_level = debug_level_default |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 225 | |
| 226 | def dbg(self, level, string): |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 227 | debug_log("DPORT", self.debug_level, level, string) |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 228 | |
| 229 | def port_add(self, interface_name, port_number): |
| 230 | """ |
| 231 | Add a port to the dataplane |
| 232 | TBD: Max packets for queue? |
| 233 | @param interface_name The name of the physical interface like eth1 |
| 234 | @param port_number The port number used to refer to the port |
| 235 | """ |
| 236 | |
| 237 | self.port_list[port_number] = DataPlanePort(interface_name) |
| 238 | self.port_list[port_number].start() |
| 239 | |
| 240 | def send(self, port_number, packet): |
| 241 | """ |
| 242 | Send a packet to the given port |
| 243 | @param port_number The port to send the data to |
| 244 | @param packet Raw packet data to send to port |
| 245 | """ |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 246 | self.dbg(DEBUG_VERBOSE, |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 247 | "Sending %d bytes to port %d" % (len(packet), port_number)) |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 248 | bytes = self.port_list[port_number].send(packet) |
| 249 | if bytes != len(packet): |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 250 | self.dbg(DEBUG_ERROR,"Unhandled send error, " + |
| 251 | "length mismatch %d != %d" % |
| 252 | (bytes, len(packet))) |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 253 | return bytes |
| 254 | |
| 255 | def flood(self, packet): |
| 256 | """ |
| 257 | Send a packet to all ports |
| 258 | @param packet Raw packet data to send to port |
| 259 | """ |
| 260 | for port_number in self.port_list.keys(): |
| 261 | bytes = self.port_list[port_number].send(packet) |
| 262 | if bytes != len(packet): |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 263 | self.dbg(DEBUG_ERROR, "Unhandled send error" + |
| 264 | ", port %d, length mismatch %d != %d" % |
| 265 | (port_number, bytes, len(packet))) |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 266 | |
| 267 | def packet_get(self, port_number=None): |
| 268 | """ |
| 269 | Get a packet from the data plane |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 270 | |
| 271 | If port_number is given, get the oldest packet from that port. |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 272 | Otherwise, find the port with the oldest packet and return |
| 273 | that packet. |
| 274 | @param port_number If set, get packet from this port |
| 275 | @retval The triple port_number, packet, pkt_time where packet |
| 276 | is received from port_number at time pkt_time. |
| 277 | """ |
| 278 | |
| 279 | if port_number: |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 280 | if len(self.port_list[port_number].packets) != 0: |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 281 | pkt, time = self.port_list[port_number].dequeue() |
| 282 | return port_number, pkt, time |
| 283 | else: |
| 284 | return None, None, None |
| 285 | |
| 286 | # Find port with oldest packet |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 287 | #@todo Consider using a single queue for all ports |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 288 | min_time = 0 |
| 289 | min_port = -1 |
| 290 | for port_number in self.port_list.keys(): |
| 291 | ptime = self.port_list[port_number].timestamp_head() |
| 292 | if ptime: |
| 293 | if (min_port == -1) or (ptime < min_time): |
| 294 | min_time = ptime |
| 295 | min_port = port_number |
| 296 | |
| 297 | if min_port == -1: |
| 298 | return None, None, None |
| 299 | |
| 300 | pkt, time = self.port_list[min_port].dequeue() |
| 301 | return min_port, pkt, time |
| 302 | |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 303 | def kill(self, join_threads=False): |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 304 | """ |
| 305 | Close all sockets for dataplane |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 306 | @param join_threads If True call join on each thread |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 307 | """ |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 308 | for port_number in self.port_list.keys(): |
| 309 | self.port_list[port_number].kill() |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 310 | if join_threads: |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 311 | self.dbg(DEBUG_INFO, "Joining " + str(port_number)) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 312 | self.port_list[port_number].join() |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 313 | |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 314 | self.dbg(DEBUG_INFO, "DataPlane shutdown") |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 315 | |
| 316 | def show(self, prefix=''): |
| 317 | print prefix + "Dataplane Controller" |
| 318 | for pnum, port in self.port_list.items(): |
| 319 | print prefix + "OpenFlow Port Number " + str(pnum) |
| 320 | port.show(prefix + ' ') |
| 321 | |