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