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 |
Rich Lane | d7d3292 | 2012-12-24 15:03:20 -0800 | [diff] [blame] | 97 | self.killed = False |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 98 | |
Rich Lane | 5b5da2d | 2012-12-22 19:56:32 -0800 | [diff] [blame] | 99 | # Used to wake up the event loop in kill() |
| 100 | self.waker = EventDescriptor() |
| 101 | |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 102 | def interface_open(self, interface_name): |
| 103 | """ |
| 104 | Open a socket in a promiscuous mode for a data connection. |
| 105 | @param interface_name port name as a string such as 'eth1' |
| 106 | @retval s socket |
| 107 | """ |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 108 | s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 109 | socket.htons(ETH_P_ALL)) |
| 110 | s.bind((interface_name, 0)) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 111 | netutils.set_promisc(s, interface_name) |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 112 | s.settimeout(RCV_TIMEOUT) |
| 113 | return s |
| 114 | |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 115 | def run(self): |
| 116 | """ |
| 117 | Activity function for class |
| 118 | """ |
Rich Lane | 5b5da2d | 2012-12-22 19:56:32 -0800 | [diff] [blame] | 119 | self.socs = [self.socket, self.waker] |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 120 | error_warned = False # Have we warned about error? |
Rich Lane | d7d3292 | 2012-12-24 15:03:20 -0800 | [diff] [blame] | 121 | while not self.killed: |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 122 | try: |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 123 | sel_in, sel_out, sel_err = \ |
| 124 | select.select(self.socs, [], [], 1) |
| 125 | except: |
| 126 | print sys.exc_info() |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 127 | self.logger.error("Select error, exiting") |
| 128 | break |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 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 | |
Rich Lane | 5b5da2d | 2012-12-22 19:56:32 -0800 | [diff] [blame] | 133 | if self.waker in sel_in: |
| 134 | self.waker.wait() |
| 135 | continue |
| 136 | |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 137 | try: |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 138 | rcvmsg = self.socket.recv(RCV_SIZE_DEFAULT) |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 139 | except socket.error: |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 140 | if not error_warned: |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 141 | self.logger.info("Socket error on recv") |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 142 | error_warned = True |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 143 | continue |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 144 | |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 145 | if len(rcvmsg) == 0: |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 146 | self.logger.info("Zero len pkt rcvd") |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 147 | self.kill() |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 148 | break |
| 149 | |
Rich Lane | d2e93aa | 2012-12-05 17:55:46 -0800 | [diff] [blame] | 150 | rcvtime = time.time() |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 151 | self.logger.debug("Pkt len " + str(len(rcvmsg)) + |
Ed Swierk | 4e20030 | 2012-03-19 14:53:31 -0700 | [diff] [blame] | 152 | " in at " + str(rcvtime) + " on port " + |
| 153 | str(self.port_number)) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 154 | |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 155 | # Enqueue packet |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 156 | with self.parent.pkt_sync: |
| 157 | if len(self.packets) >= self.max_pkts: |
| 158 | # Queue full, throw away oldest |
| 159 | self.packets.pop(0) |
| 160 | self.packets_discarded += 1 |
| 161 | self.logger.debug("Discarding oldest packet to make room") |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 162 | self.packets.append((rcvmsg, rcvtime)) |
| 163 | self.packets_total += 1 |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 164 | self.parent.pkt_sync.notify_all() |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 165 | |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 166 | self.logger.info("Thread exit") |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 167 | |
| 168 | def kill(self): |
| 169 | """ |
| 170 | Terminate the running thread |
| 171 | """ |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 172 | self.logger.debug("Port monitor kill") |
Rich Lane | d7d3292 | 2012-12-24 15:03:20 -0800 | [diff] [blame] | 173 | self.killed = True |
Rich Lane | 5b5da2d | 2012-12-22 19:56:32 -0800 | [diff] [blame] | 174 | self.waker.notify() |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 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 | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 180 | def timestamp_head(self): |
| 181 | """ |
| 182 | Return the timestamp of the head of queue or None if empty |
| 183 | """ |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 184 | rv = None |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 185 | try: |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 186 | rv = self.packets[0][1] |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 187 | except: |
| 188 | rv = None |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 189 | return rv |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 190 | |
| 191 | def flush(self): |
| 192 | """ |
| 193 | Clear the packet queue |
| 194 | """ |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 195 | with self.parent.pkt_sync: |
| 196 | self.packets_discarded += len(self.packets) |
| 197 | self.packets = [] |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 198 | |
| 199 | def send(self, packet): |
| 200 | """ |
| 201 | Send a packet to the dataplane port |
| 202 | @param packet The packet data to send to the port |
| 203 | @retval The number of bytes sent |
| 204 | """ |
| 205 | return self.socket.send(packet) |
| 206 | |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 207 | def register(self, handler): |
| 208 | """ |
| 209 | Register a callback function to receive packets from this |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 210 | port. The callback will be passed the packet, the |
| 211 | interface name and the port number (if set) on which the |
Dan Talayco | 3087a46 | 2010-02-13 14:01:47 -0800 | [diff] [blame] | 212 | packet was received. |
| 213 | |
| 214 | To be implemented |
| 215 | """ |
| 216 | pass |
| 217 | |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 218 | def show(self, prefix=''): |
ShreyaPandita | cd8e1cf | 2012-11-28 11:44:42 -0500 | [diff] [blame] | 219 | |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 220 | print prefix + "Name: " + self.interface_name |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 221 | print prefix + "Pkts pending: " + str(len(self.packets)) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 222 | print prefix + "Pkts total: " + str(self.packets_total) |
| 223 | print prefix + "socket: " + str(self.socket) |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 224 | |
ShreyaPandita | cd8e1cf | 2012-11-28 11:44:42 -0500 | [diff] [blame] | 225 | |
| 226 | def port_down(self,port_number,config): |
| 227 | |
| 228 | """ |
| 229 | Grabs a port from the dataplane ports and brings it down by |
| 230 | shutting the corresponding interface |
| 231 | @port_number The port number which has brought to be down |
| 232 | @interface_name The interface corresponding to the port that needs to |
| 233 | be brought down |
| 234 | |
| 235 | """ |
| 236 | interface_name = config["port_map"].get(port_number) |
| 237 | cmd = 'ifdown '+ interface_name |
| 238 | os.system(cmd) |
| 239 | |
| 240 | def port_up(self,port_number,config): |
| 241 | |
| 242 | """ |
| 243 | Grabs a port from the dataplane ports and brings it up by |
| 244 | starting up the corresponding interface |
| 245 | @port_number The port number which has to brought up |
| 246 | @interface_name The interface corresponding to the port that has to |
| 247 | be brought up |
| 248 | |
| 249 | """ |
| 250 | interface_name = config["port_map"].get(port_number) |
| 251 | cmd = 'ifup '+ interface_name |
| 252 | os.system(cmd) |
| 253 | |
| 254 | |
Rich Lane | b42a31c | 2012-10-05 17:54:17 -0700 | [diff] [blame] | 255 | class DataPlanePortPcap(DataPlanePort): |
| 256 | """ |
| 257 | Alternate port implementation using libpcap. This is required for recent |
| 258 | versions of Linux (such as Linux 3.2 included in Ubuntu 12.04) which |
| 259 | offload the VLAN tag, so it isn't in the data returned from a read on a raw |
| 260 | socket. libpcap understands how to read the VLAN tag from the kernel. |
| 261 | """ |
| 262 | |
| 263 | def __init__(self, interface_name, port_number, parent, max_pkts=1024): |
| 264 | DataPlanePort.__init__(self, interface_name, port_number, parent, max_pkts) |
| 265 | |
| 266 | def interface_open(self, interface_name): |
| 267 | """ |
| 268 | Open a PCAP interface. |
| 269 | """ |
| 270 | self.pcap = pcap.pcap(interface_name) |
| 271 | self.pcap.setnonblock() |
| 272 | return self.pcap.fileno() |
| 273 | |
| 274 | def run(self): |
| 275 | """ |
| 276 | Activity function for class |
| 277 | """ |
Rich Lane | d7d3292 | 2012-12-24 15:03:20 -0800 | [diff] [blame] | 278 | while not self.killed: |
Rich Lane | b42a31c | 2012-10-05 17:54:17 -0700 | [diff] [blame] | 279 | try: |
Rich Lane | 5b5da2d | 2012-12-22 19:56:32 -0800 | [diff] [blame] | 280 | sel_in, sel_out, sel_err = select.select([self.socket, self.waker], [], [], 1) |
Rich Lane | b42a31c | 2012-10-05 17:54:17 -0700 | [diff] [blame] | 281 | except: |
| 282 | print sys.exc_info() |
| 283 | self.logger.error("Select error, exiting") |
| 284 | break |
| 285 | |
Rich Lane | b42a31c | 2012-10-05 17:54:17 -0700 | [diff] [blame] | 286 | if (sel_in is None) or (len(sel_in) == 0): |
| 287 | continue |
| 288 | |
Rich Lane | 5b5da2d | 2012-12-22 19:56:32 -0800 | [diff] [blame] | 289 | if self.waker in sel_in: |
| 290 | self.waker.wait() |
| 291 | continue |
| 292 | |
Rich Lane | b42a31c | 2012-10-05 17:54:17 -0700 | [diff] [blame] | 293 | # Enqueue packet |
| 294 | with self.parent.pkt_sync: |
| 295 | for (timestamp, rcvmsg) in self.pcap.readpkts(): |
Rich Lane | b42a31c | 2012-10-05 17:54:17 -0700 | [diff] [blame] | 296 | self.logger.debug("Pkt len " + str(len(rcvmsg)) + |
Rich Lane | d2e93aa | 2012-12-05 17:55:46 -0800 | [diff] [blame] | 297 | " in at " + str(timestamp) + " on port " + |
Rich Lane | b42a31c | 2012-10-05 17:54:17 -0700 | [diff] [blame] | 298 | str(self.port_number)) |
| 299 | |
| 300 | if len(self.packets) >= self.max_pkts: |
| 301 | # Queue full, throw away oldest |
| 302 | self.packets.pop(0) |
| 303 | self.packets_discarded += 1 |
| 304 | self.logger.debug("Discarding oldest packet to make room") |
Rich Lane | d2e93aa | 2012-12-05 17:55:46 -0800 | [diff] [blame] | 305 | self.packets.append((rcvmsg, timestamp)) |
Rich Lane | b42a31c | 2012-10-05 17:54:17 -0700 | [diff] [blame] | 306 | self.packets_total += 1 |
| 307 | self.parent.pkt_sync.notify_all() |
| 308 | |
| 309 | self.logger.info("Thread exit") |
| 310 | |
| 311 | def kill(self): |
| 312 | """ |
| 313 | Terminate the running thread |
| 314 | """ |
| 315 | self.logger.debug("Port monitor kill") |
Rich Lane | d7d3292 | 2012-12-24 15:03:20 -0800 | [diff] [blame] | 316 | self.killed = True |
Rich Lane | 5b5da2d | 2012-12-22 19:56:32 -0800 | [diff] [blame] | 317 | self.waker.notify() |
Rich Lane | b42a31c | 2012-10-05 17:54:17 -0700 | [diff] [blame] | 318 | # pcap object is closed on GC. |
| 319 | |
| 320 | def send(self, packet): |
| 321 | """ |
| 322 | Send a packet to the dataplane port |
| 323 | @param packet The packet data to send to the port |
| 324 | @retval The number of bytes sent |
| 325 | """ |
| 326 | return self.pcap.inject(packet, len(packet)) |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 327 | |
| 328 | class DataPlane: |
| 329 | """ |
| 330 | Class defining access primitives to the data plane |
| 331 | Controls a list of DataPlanePort objects |
| 332 | """ |
Jeffrey Townsend | 0e8b092 | 2012-07-11 11:37:46 -0700 | [diff] [blame] | 333 | def __init__(self, config=None): |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 334 | self.port_list = {} |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 335 | # pkt_sync serves double duty as a regular top level lock and |
| 336 | # as a condition variable |
| 337 | self.pkt_sync = Condition() |
| 338 | |
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 | # |
Jeffrey Townsend | 0e8b092 | 2012-07-11 11:37:46 -0700 | [diff] [blame] | 348 | # The platform/config can provide a custom DataPlanePort class |
| 349 | # here if you have a custom implementation with different |
| 350 | # behavior. |
| 351 | # |
| 352 | # Set config.dataplane.portclass = MyDataPlanePortClass |
| 353 | # where MyDataPlanePortClass has the same interface as the class |
| 354 | # DataPlanePort defined here. |
| 355 | # |
Rich Lane | fb71830 | 2012-12-26 21:02:31 -0800 | [diff] [blame^] | 356 | if "dataplane" in self.config and "portclass" in self.config["dataplane"]: |
| 357 | self.dppclass = self.config["dataplane"]["portclass"] |
| 358 | elif have_pypcap: |
| 359 | self.dppclass = DataPlanePortPcap |
| 360 | else: |
| 361 | self.logger.warning("Missing pypcap, VLAN tests may fail. See README for installation instructions.") |
| 362 | self.dppclass = DataPlanePort |
Jeffrey Townsend | 0e8b092 | 2012-07-11 11:37:46 -0700 | [diff] [blame] | 363 | |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 364 | def port_add(self, interface_name, port_number): |
| 365 | """ |
| 366 | Add a port to the dataplane |
| 367 | TBD: Max packets for queue? |
| 368 | @param interface_name The name of the physical interface like eth1 |
| 369 | @param port_number The port number used to refer to the port |
| 370 | """ |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 371 | |
Jeffrey Townsend | 0e8b092 | 2012-07-11 11:37:46 -0700 | [diff] [blame] | 372 | self.port_list[port_number] = self.dppclass(interface_name, |
| 373 | port_number, self); |
| 374 | |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 375 | self.port_list[port_number].start() |
| 376 | |
| 377 | def send(self, port_number, packet): |
| 378 | """ |
| 379 | Send a packet to the given port |
| 380 | @param port_number The port to send the data to |
| 381 | @param packet Raw packet data to send to port |
| 382 | """ |
Dan Talayco | 11c26e7 | 2010-03-07 22:03:57 -0800 | [diff] [blame] | 383 | self.logger.debug("Sending %d bytes to port %d" % |
| 384 | (len(packet), port_number)) |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 385 | bytes = self.port_list[port_number].send(packet) |
| 386 | if bytes != len(packet): |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 387 | self.logger.error("Unhandled send error, length mismatch %d != %d" % |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 388 | (bytes, len(packet))) |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 389 | return bytes |
| 390 | |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 391 | # Returns the port with the oldest packet, or None if no packets are queued. |
| 392 | def oldest_port(self): |
| 393 | min_port = None |
| 394 | min_time = float('inf') |
| 395 | for port in self.port_list.values(): |
| 396 | ptime = port.timestamp_head() |
| 397 | if ptime and ptime < min_time: |
| 398 | min_time = ptime |
| 399 | min_port = port |
| 400 | return min_port |
| 401 | |
| 402 | # Dequeues and yields packets in the order they were received. |
| 403 | # Yields (port, packet, received time). |
| 404 | # If port_number is not specified yields packets from all ports. |
| 405 | def packets(self, port_number=None): |
| 406 | while True: |
| 407 | if port_number == None: |
| 408 | port = self.oldest_port() |
| 409 | else: |
| 410 | port = self.port_list[port_number] |
| 411 | |
| 412 | if port == None or len(port.packets) == 0: |
| 413 | self.logger.debug("Out of packets for port %s" % str(port_number)) |
| 414 | # Out of packets |
| 415 | break |
| 416 | |
| 417 | pkt, time = port.packets.pop(0) |
| 418 | yield (port, pkt, time) |
| 419 | |
Rich Lane | 8806bc4 | 2012-07-26 19:18:37 -0700 | [diff] [blame] | 420 | def poll(self, port_number=None, timeout=-1, exp_pkt=None): |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 421 | """ |
| 422 | Poll one or all dataplane ports for a packet |
| 423 | |
| 424 | If port_number is given, get the oldest packet from that port. |
| 425 | Otherwise, find the port with the oldest packet and return |
| 426 | that packet. |
Dan Talayco | 1729fdb | 2012-05-03 09:35:56 -0700 | [diff] [blame] | 427 | |
| 428 | If exp_pkt is true, discard all packets until that one is found |
| 429 | |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 430 | @param port_number If set, get packet from this port |
Dan Talayco | 11c26e7 | 2010-03-07 22:03:57 -0800 | [diff] [blame] | 431 | @param timeout If positive and no packet is available, block |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 432 | until a packet is received or for this many seconds |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 433 | @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] | 434 | others received. Note that if port_number is None, all packets |
| 435 | from all ports will be discarded until the exp_pkt is found |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 436 | @return The triple port_number, packet, pkt_time where packet |
| 437 | is received from port_number at time pkt_time. If a timeout |
| 438 | occurs, return None, None, None |
| 439 | """ |
| 440 | |
Dan Talayco | cf26b7a | 2011-08-05 10:15:35 -0700 | [diff] [blame] | 441 | if exp_pkt and not port_number: |
Dan Talayco | 1729fdb | 2012-05-03 09:35:56 -0700 | [diff] [blame] | 442 | self.logger.warn("Dataplane poll with exp_pkt but no port number") |
| 443 | |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 444 | # Retrieve the packet. Returns (port number, packet, time). |
| 445 | def grab(): |
| 446 | self.logger.debug("Grabbing packet") |
| 447 | for (port, pkt, time) in self.packets(port_number): |
| 448 | self.logger.debug("Checking packet from port %d" % port.port_number) |
Dan Talayco | 1729fdb | 2012-05-03 09:35:56 -0700 | [diff] [blame] | 449 | if not exp_pkt or match_exp_pkt(exp_pkt, pkt): |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 450 | return (port, pkt, time) |
| 451 | self.logger.debug("Did not find packet") |
| 452 | return None |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 453 | |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 454 | with self.pkt_sync: |
| 455 | ret = timed_wait(self.pkt_sync, grab, timeout=timeout) |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 456 | |
Rich Lane | db9d866 | 2012-07-26 18:04:24 -0700 | [diff] [blame] | 457 | if ret != None: |
| 458 | (port, pkt, time) = ret |
| 459 | return (port.port_number, pkt, time) |
| 460 | else: |
| 461 | self.logger.debug("Poll time out, no packet from " + str(port_number)) |
| 462 | return (None, None, None) |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 463 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 464 | def kill(self, join_threads=True): |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 465 | """ |
| 466 | Close all sockets for dataplane |
Dan Talayco | 710438c | 2010-02-18 15:16:07 -0800 | [diff] [blame] | 467 | @param join_threads If True call join on each thread |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 468 | """ |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 469 | for port_number in self.port_list.keys(): |
| 470 | self.port_list[port_number].kill() |
Rich Lane | 4d46dbd | 2012-12-22 19:26:19 -0800 | [diff] [blame] | 471 | |
| 472 | if join_threads: |
| 473 | for port_number in self.port_list.keys(): |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 474 | self.logger.debug("Joining " + str(port_number)) |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 475 | self.port_list[port_number].join() |
Dan Talayco | 3408952 | 2010-02-07 23:07:41 -0800 | [diff] [blame] | 476 | |
Rich Lane | e7b0ecb | 2012-12-26 10:01:01 -0800 | [diff] [blame] | 477 | self.port_list = None |
| 478 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 479 | self.logger.info("DataPlane shutdown") |
Dan Talayco | 1b3f690 | 2010-02-15 14:14:19 -0800 | [diff] [blame] | 480 | |
| 481 | def show(self, prefix=''): |
| 482 | print prefix + "Dataplane Controller" |
| 483 | for pnum, port in self.port_list.items(): |
| 484 | print prefix + "OpenFlow Port Number " + str(pnum) |
| 485 | port.show(prefix + ' ') |
| 486 | |
ShreyaPandita | cd8e1cf | 2012-11-28 11:44:42 -0500 | [diff] [blame] | 487 | def port_down(self,port_number): |
| 488 | """Brings the specified port down""" |
| 489 | self.port_list[port_number].port_down(port_number,self.config) |
| 490 | |
ShreyaPandita | cd8e1cf | 2012-11-28 11:44:42 -0500 | [diff] [blame] | 491 | def port_up(self,port_number): |
| 492 | """Brings the specified port up""" |
| 493 | self.port_list[port_number].port_up(port_number,self.config) |