Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.forwarding; |
| 2 | |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
Jonathan Hart | d33a6cf | 2013-12-10 14:29:08 -0800 | [diff] [blame] | 5 | import java.util.HashMap; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 6 | import java.util.Iterator; |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 7 | import java.util.List; |
| 8 | import java.util.Map; |
Jonathan Hart | 5e44878 | 2013-12-10 12:36:35 -0800 | [diff] [blame] | 9 | import java.util.concurrent.ConcurrentHashMap; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 10 | |
| 11 | import net.floodlightcontroller.core.FloodlightContext; |
| 12 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 13 | import net.floodlightcontroller.core.IOFMessageListener; |
| 14 | import net.floodlightcontroller.core.IOFSwitch; |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 15 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 16 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 17 | import net.floodlightcontroller.core.module.IFloodlightService; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 18 | import net.floodlightcontroller.packet.Ethernet; |
| 19 | import net.floodlightcontroller.util.MACAddress; |
| 20 | import net.onrc.onos.ofcontroller.core.IDeviceStorage; |
| 21 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject; |
| 22 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 23 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
| 24 | import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 25 | import net.onrc.onos.ofcontroller.flowmanager.IFlowService; |
Jonathan Hart | 7e6df36 | 2013-12-10 23:33:59 -0800 | [diff] [blame] | 26 | import net.onrc.onos.ofcontroller.flowprogrammer.IFlowPusherService; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 27 | import net.onrc.onos.ofcontroller.topology.TopologyManager; |
| 28 | import net.onrc.onos.ofcontroller.util.CallerId; |
| 29 | import net.onrc.onos.ofcontroller.util.DataPath; |
| 30 | import net.onrc.onos.ofcontroller.util.Dpid; |
Jonathan Hart | 4fb16d8 | 2013-11-07 22:41:32 -0800 | [diff] [blame] | 31 | import net.onrc.onos.ofcontroller.util.FlowEntryMatch; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 32 | import net.onrc.onos.ofcontroller.util.FlowId; |
| 33 | import net.onrc.onos.ofcontroller.util.FlowPath; |
| 34 | import net.onrc.onos.ofcontroller.util.FlowPathType; |
| 35 | import net.onrc.onos.ofcontroller.util.FlowPathUserState; |
| 36 | import net.onrc.onos.ofcontroller.util.Port; |
| 37 | import net.onrc.onos.ofcontroller.util.SwitchPort; |
| 38 | |
| 39 | import org.openflow.protocol.OFMessage; |
| 40 | import org.openflow.protocol.OFPacketIn; |
Jonathan Hart | 41d1e91 | 2013-11-24 16:50:25 -0800 | [diff] [blame] | 41 | import org.openflow.protocol.OFPacketOut; |
| 42 | import org.openflow.protocol.OFPort; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 43 | import org.openflow.protocol.OFType; |
Jonathan Hart | 41d1e91 | 2013-11-24 16:50:25 -0800 | [diff] [blame] | 44 | import org.openflow.protocol.action.OFAction; |
| 45 | import org.openflow.protocol.action.OFActionOutput; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 46 | import org.openflow.util.HexString; |
| 47 | import org.slf4j.Logger; |
| 48 | import org.slf4j.LoggerFactory; |
| 49 | |
Jonathan Hart | 5e44878 | 2013-12-10 12:36:35 -0800 | [diff] [blame] | 50 | import com.google.common.collect.HashMultimap; |
| 51 | import com.google.common.collect.Multimap; |
Jonathan Hart | 5e44878 | 2013-12-10 12:36:35 -0800 | [diff] [blame] | 52 | |
| 53 | public class Forwarding implements IOFMessageListener, IFloodlightModule, |
| 54 | IForwardingService { |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 55 | private final static Logger log = LoggerFactory.getLogger(Forwarding.class); |
| 56 | |
Jonathan Hart | 7e6df36 | 2013-12-10 23:33:59 -0800 | [diff] [blame] | 57 | private final int IDLE_TIMEOUT = 5; // seconds |
| 58 | private final int HARD_TIMEOUT = 0; // seconds |
| 59 | |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 60 | private IFloodlightProviderService floodlightProvider; |
| 61 | private IFlowService flowService; |
Jonathan Hart | 7e6df36 | 2013-12-10 23:33:59 -0800 | [diff] [blame] | 62 | private IFlowPusherService flowPusher; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 63 | |
| 64 | private IDeviceStorage deviceStorage; |
| 65 | private TopologyManager topologyService; |
| 66 | |
Jonathan Hart | 5e44878 | 2013-12-10 12:36:35 -0800 | [diff] [blame] | 67 | private Map<Path, Long> pendingFlows; |
| 68 | private Multimap<Long, PacketToPush> waitingPackets; |
| 69 | |
Jonathan Hart | 7e6df36 | 2013-12-10 23:33:59 -0800 | [diff] [blame] | 70 | private final Object lock = new Object(); |
| 71 | |
Jonathan Hart | 5e44878 | 2013-12-10 12:36:35 -0800 | [diff] [blame] | 72 | public class PacketToPush { |
| 73 | public final OFPacketOut packet; |
| 74 | public final long dpid; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 75 | |
Jonathan Hart | 5e44878 | 2013-12-10 12:36:35 -0800 | [diff] [blame] | 76 | public PacketToPush(OFPacketOut packet, long dpid) { |
| 77 | this.packet = packet; |
| 78 | this.dpid = dpid; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | public final class Path { |
| 83 | public final SwitchPort srcPort; |
| 84 | public final SwitchPort dstPort; |
| 85 | |
| 86 | public Path(SwitchPort src, SwitchPort dst) { |
| 87 | srcPort = new SwitchPort(new Dpid(src.dpid().value()), |
| 88 | new Port(src.port().value())); |
| 89 | dstPort = new SwitchPort(new Dpid(dst.dpid().value()), |
| 90 | new Port(dst.port().value())); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public boolean equals(Object other) { |
| 95 | if (!(other instanceof Path)) { |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | Path otherPath = (Path) other; |
| 100 | return srcPort.equals(otherPath.srcPort) && |
| 101 | dstPort.equals(otherPath.dstPort); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public int hashCode() { |
| 106 | int hash = 17; |
| 107 | hash = 31 * hash + srcPort.hashCode(); |
| 108 | hash = 31 * hash + dstPort.hashCode(); |
| 109 | return hash; |
| 110 | } |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 113 | @Override |
| 114 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Jonathan Hart | d33a6cf | 2013-12-10 14:29:08 -0800 | [diff] [blame] | 115 | List<Class<? extends IFloodlightService>> services = |
| 116 | new ArrayList<Class<? extends IFloodlightService>>(1); |
| 117 | services.add(IForwardingService.class); |
| 118 | return services; |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
Jonathan Hart | d33a6cf | 2013-12-10 14:29:08 -0800 | [diff] [blame] | 123 | Map<Class<? extends IFloodlightService>, IFloodlightService> impls = |
| 124 | new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(1); |
| 125 | impls.put(IForwardingService.class, this); |
| 126 | return impls; |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 131 | List<Class<? extends IFloodlightService>> dependencies = |
| 132 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 133 | dependencies.add(IFloodlightProviderService.class); |
| 134 | dependencies.add(IFlowService.class); |
Jonathan Hart | 7e6df36 | 2013-12-10 23:33:59 -0800 | [diff] [blame] | 135 | dependencies.add(IFlowPusherService.class); |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 136 | return dependencies; |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public void init(FloodlightModuleContext context) { |
Jonathan Hart | 7e6df36 | 2013-12-10 23:33:59 -0800 | [diff] [blame] | 141 | floodlightProvider = |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 142 | context.getServiceImpl(IFloodlightProviderService.class); |
Jonathan Hart | 7e6df36 | 2013-12-10 23:33:59 -0800 | [diff] [blame] | 143 | flowService = context.getServiceImpl(IFlowService.class); |
| 144 | flowPusher = context.getServiceImpl(IFlowPusherService.class); |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 145 | |
| 146 | floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this); |
| 147 | |
Jonathan Hart | 5e44878 | 2013-12-10 12:36:35 -0800 | [diff] [blame] | 148 | pendingFlows = new ConcurrentHashMap<Path, Long>(); |
Jonathan Hart | 7e6df36 | 2013-12-10 23:33:59 -0800 | [diff] [blame] | 149 | //waitingPackets = Multimaps.synchronizedSetMultimap( |
| 150 | //HashMultimap.<Long, PacketToPush>create()); |
| 151 | waitingPackets = HashMultimap.create(); |
Jonathan Hart | 5e44878 | 2013-12-10 12:36:35 -0800 | [diff] [blame] | 152 | |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 153 | deviceStorage = new DeviceStorageImpl(); |
| 154 | deviceStorage.init(""); |
| 155 | topologyService = new TopologyManager(); |
| 156 | topologyService.init(""); |
| 157 | } |
| 158 | |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 159 | @Override |
| 160 | public void startUp(FloodlightModuleContext context) { |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 161 | // no-op |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public String getName() { |
| 166 | return "onosforwarding"; |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public boolean isCallbackOrderingPrereq(OFType type, String name) { |
| 171 | return (type == OFType.PACKET_IN) && |
| 172 | (name.equals("devicemanager") || name.equals("proxyarpmanager")); |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public boolean isCallbackOrderingPostreq(OFType type, String name) { |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public Command receive( |
| 182 | IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { |
| 183 | |
| 184 | if (msg.getType() != OFType.PACKET_IN) { |
| 185 | return Command.CONTINUE; |
| 186 | } |
| 187 | |
| 188 | OFPacketIn pi = (OFPacketIn) msg; |
| 189 | |
| 190 | Ethernet eth = IFloodlightProviderService.bcStore. |
| 191 | get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD); |
| 192 | |
Jonathan Hart | 4fb16d8 | 2013-11-07 22:41:32 -0800 | [diff] [blame] | 193 | // We only want to handle unicast IPv4 |
| 194 | if (eth.isBroadcast() || eth.isMulticast() || |
| 195 | eth.getEtherType() != Ethernet.TYPE_IPv4) { |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 196 | return Command.CONTINUE; |
| 197 | } |
| 198 | |
| 199 | handlePacketIn(sw, pi, eth); |
| 200 | |
| 201 | return Command.STOP; |
| 202 | } |
| 203 | |
| 204 | private void handlePacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth) { |
Jonathan Hart | 5e44878 | 2013-12-10 12:36:35 -0800 | [diff] [blame] | 205 | String destinationMac = |
| 206 | HexString.toHexString(eth.getDestinationMACAddress()); |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 207 | |
| 208 | IDeviceObject deviceObject = deviceStorage.getDeviceByMac( |
| 209 | destinationMac); |
| 210 | |
| 211 | if (deviceObject == null) { |
| 212 | log.debug("No device entry found for {}", destinationMac); |
| 213 | return; |
| 214 | } |
| 215 | |
Jonathan Hart | 5e44878 | 2013-12-10 12:36:35 -0800 | [diff] [blame] | 216 | Iterator<IPortObject> ports = deviceObject.getAttachedPorts().iterator(); |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 217 | if (!ports.hasNext()) { |
| 218 | log.debug("No attachment point found for device {}", destinationMac); |
| 219 | return; |
| 220 | } |
| 221 | IPortObject portObject = ports.next(); |
| 222 | short destinationPort = portObject.getNumber(); |
| 223 | ISwitchObject switchObject = portObject.getSwitch(); |
| 224 | long destinationDpid = HexString.toLong(switchObject.getDPID()); |
| 225 | |
Jonathan Hart | 41d1e91 | 2013-11-24 16:50:25 -0800 | [diff] [blame] | 226 | // TODO SwitchPort, Dpid and Port should probably be immutable |
| 227 | // (also, are Dpid and Port are even necessary?) |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 228 | SwitchPort srcSwitchPort = new SwitchPort( |
| 229 | new Dpid(sw.getId()), new Port(pi.getInPort())); |
| 230 | SwitchPort dstSwitchPort = new SwitchPort( |
| 231 | new Dpid(destinationDpid), new Port(destinationPort)); |
Jonathan Hart | dc3ad70 | 2013-11-14 11:34:59 -0800 | [diff] [blame] | 232 | |
| 233 | MACAddress srcMacAddress = MACAddress.valueOf(eth.getSourceMACAddress()); |
| 234 | MACAddress dstMacAddress = MACAddress.valueOf(eth.getDestinationMACAddress()); |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 235 | |
Jonathan Hart | 5e44878 | 2013-12-10 12:36:35 -0800 | [diff] [blame] | 236 | |
Jonathan Hart | 7e6df36 | 2013-12-10 23:33:59 -0800 | [diff] [blame] | 237 | FlowPath flowPath, reverseFlowPath; |
Jonathan Hart | 5e44878 | 2013-12-10 12:36:35 -0800 | [diff] [blame] | 238 | |
| 239 | Path pathspec = new Path(srcSwitchPort, dstSwitchPort); |
| 240 | // TODO check concurrency |
Jonathan Hart | 7e6df36 | 2013-12-10 23:33:59 -0800 | [diff] [blame] | 241 | synchronized (lock) { |
| 242 | Long existingFlowId = pendingFlows.get(pathspec); |
Jonathan Hart | dc3ad70 | 2013-11-14 11:34:59 -0800 | [diff] [blame] | 243 | |
Jonathan Hart | 7e6df36 | 2013-12-10 23:33:59 -0800 | [diff] [blame] | 244 | if (existingFlowId != null) { |
| 245 | log.debug("Found existing flow {}", |
| 246 | HexString.toHexString(existingFlowId)); |
| 247 | |
| 248 | OFPacketOut po = constructPacketOut(pi, sw); |
| 249 | waitingPackets.put(existingFlowId, new PacketToPush(po, sw.getId())); |
| 250 | return; |
| 251 | } |
| 252 | |
Jonathan Hart | 7e6df36 | 2013-12-10 23:33:59 -0800 | [diff] [blame] | 253 | log.debug("Adding new flow between {} at {} and {} at {}", |
| 254 | new Object[]{srcMacAddress, srcSwitchPort, dstMacAddress, dstSwitchPort}); |
| 255 | |
| 256 | |
| 257 | CallerId callerId = new CallerId("Forwarding"); |
| 258 | |
| 259 | DataPath datapath = new DataPath(); |
| 260 | datapath.setSrcPort(srcSwitchPort); |
| 261 | datapath.setDstPort(dstSwitchPort); |
| 262 | |
| 263 | flowPath = new FlowPath(); |
| 264 | flowPath.setInstallerId(callerId); |
| 265 | |
| 266 | flowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH); |
| 267 | flowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD); |
| 268 | flowPath.setFlowEntryMatch(new FlowEntryMatch()); |
| 269 | flowPath.setIdleTimeout(IDLE_TIMEOUT); |
| 270 | flowPath.setHardTimeout(HARD_TIMEOUT); |
| 271 | flowPath.flowEntryMatch().enableSrcMac(srcMacAddress); |
| 272 | flowPath.flowEntryMatch().enableDstMac(dstMacAddress); |
| 273 | flowPath.flowEntryMatch().enableEthernetFrameType(Ethernet.TYPE_IPv4); |
| 274 | flowPath.setDataPath(datapath); |
| 275 | |
| 276 | |
| 277 | DataPath reverseDataPath = new DataPath(); |
| 278 | // Reverse the ports for the reverse path |
| 279 | reverseDataPath.setSrcPort(dstSwitchPort); |
| 280 | reverseDataPath.setDstPort(srcSwitchPort); |
| 281 | |
| 282 | // TODO implement copy constructor for FlowPath |
| 283 | reverseFlowPath = new FlowPath(); |
| 284 | reverseFlowPath.setInstallerId(callerId); |
| 285 | reverseFlowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH); |
| 286 | reverseFlowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD); |
| 287 | reverseFlowPath.setIdleTimeout(IDLE_TIMEOUT); |
| 288 | reverseFlowPath.setHardTimeout(HARD_TIMEOUT); |
| 289 | reverseFlowPath.setFlowEntryMatch(new FlowEntryMatch()); |
| 290 | // Reverse the MAC addresses for the reverse path |
| 291 | reverseFlowPath.flowEntryMatch().enableSrcMac(dstMacAddress); |
| 292 | reverseFlowPath.flowEntryMatch().enableDstMac(srcMacAddress); |
| 293 | reverseFlowPath.flowEntryMatch().enableEthernetFrameType(Ethernet.TYPE_IPv4); |
| 294 | reverseFlowPath.setDataPath(reverseDataPath); |
| 295 | reverseFlowPath.dataPath().srcPort().dpid().toString(); |
| 296 | |
| 297 | // TODO what happens if no path exists? cleanup |
| 298 | |
| 299 | FlowId flowId = new FlowId(flowService.getNextFlowEntryId()); |
| 300 | FlowId reverseFlowId = new FlowId(flowService.getNextFlowEntryId()); |
| 301 | |
| 302 | flowPath.setFlowId(flowId); |
| 303 | reverseFlowPath.setFlowId(reverseFlowId); |
| 304 | |
| 305 | OFPacketOut po = constructPacketOut(pi, sw); |
| 306 | Path reversePathSpec = new Path(dstSwitchPort, srcSwitchPort); |
| 307 | |
| 308 | // Add to waiting lists |
| 309 | pendingFlows.put(pathspec, flowId.value()); |
| 310 | pendingFlows.put(reversePathSpec, reverseFlowId.value()); |
| 311 | waitingPackets.put(flowId.value(), new PacketToPush(po, sw.getId())); |
| 312 | |
Jonathan Hart | 41d1e91 | 2013-11-24 16:50:25 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Jonathan Hart | 5e44878 | 2013-12-10 12:36:35 -0800 | [diff] [blame] | 315 | flowService.addFlow(reverseFlowPath); |
| 316 | flowService.addFlow(flowPath); |
Jonathan Hart | 7e6df36 | 2013-12-10 23:33:59 -0800 | [diff] [blame] | 317 | |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 318 | } |
Jonathan Hart | dc3ad70 | 2013-11-14 11:34:59 -0800 | [diff] [blame] | 319 | |
Jonathan Hart | 5e44878 | 2013-12-10 12:36:35 -0800 | [diff] [blame] | 320 | /* |
Jonathan Hart | dc3ad70 | 2013-11-14 11:34:59 -0800 | [diff] [blame] | 321 | private boolean flowExists(SwitchPort srcPort, MACAddress srcMac, |
| 322 | SwitchPort dstPort, MACAddress dstMac) { |
| 323 | for (FlowPath flow : datagridService.getAllFlows()) { |
| 324 | FlowEntryMatch match = flow.flowEntryMatch(); |
| 325 | // TODO implement FlowEntryMatch.equals(); |
| 326 | // This is painful to do properly without support in the FlowEntryMatch |
| 327 | boolean same = true; |
| 328 | if (!match.srcMac().equals(srcMac) || |
| 329 | !match.dstMac().equals(dstMac)) { |
| 330 | same = false; |
| 331 | } |
| 332 | if (!flow.dataPath().srcPort().equals(srcPort) || |
| 333 | !flow.dataPath().dstPort().equals(dstPort)) { |
| 334 | same = false; |
| 335 | } |
| 336 | |
| 337 | if (same) { |
| 338 | log.debug("found flow entry that's the same {}-{}:::{}-{}", |
| 339 | new Object[] {srcPort, srcMac, dstPort, dstMac}); |
| 340 | return true; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | return false; |
| 345 | } |
Jonathan Hart | 5e44878 | 2013-12-10 12:36:35 -0800 | [diff] [blame] | 346 | */ |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 347 | |
Jonathan Hart | 7e6df36 | 2013-12-10 23:33:59 -0800 | [diff] [blame] | 348 | private OFPacketOut constructPacketOut(OFPacketIn pi, IOFSwitch sw) { |
Jonathan Hart | 41d1e91 | 2013-11-24 16:50:25 -0800 | [diff] [blame] | 349 | OFPacketOut po = new OFPacketOut(); |
| 350 | po.setInPort(OFPort.OFPP_NONE) |
| 351 | .setInPort(pi.getInPort()) |
Jonathan Hart | 5e44878 | 2013-12-10 12:36:35 -0800 | [diff] [blame] | 352 | .setActions(new ArrayList<OFAction>()) |
| 353 | .setLengthU(OFPacketOut.MINIMUM_LENGTH); |
Jonathan Hart | 41d1e91 | 2013-11-24 16:50:25 -0800 | [diff] [blame] | 354 | |
| 355 | if (sw.getBuffers() == 0) { |
| 356 | po.setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 357 | .setPacketData(pi.getPacketData()) |
| 358 | .setLengthU(po.getLengthU() + po.getPacketData().length); |
| 359 | } |
| 360 | else { |
| 361 | po.setBufferId(pi.getBufferId()); |
| 362 | } |
| 363 | |
Jonathan Hart | 5e44878 | 2013-12-10 12:36:35 -0800 | [diff] [blame] | 364 | return po; |
| 365 | } |
Pavlin Radoslavov | 7208e9a | 2013-12-11 14:31:07 -0800 | [diff] [blame] | 366 | |
Jonathan Hart | 5e44878 | 2013-12-10 12:36:35 -0800 | [diff] [blame] | 367 | @Override |
Pavlin Radoslavov | 7208e9a | 2013-12-11 14:31:07 -0800 | [diff] [blame] | 368 | public void flowsInstalled(Collection<FlowPath> installedFlowPaths) { |
| 369 | for (FlowPath flowPath : installedFlowPaths) { |
| 370 | flowInstalled(flowPath); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | private void flowInstalled(FlowPath installedFlowPath) { |
Jonathan Hart | 5e44878 | 2013-12-10 12:36:35 -0800 | [diff] [blame] | 375 | // TODO check concurrency |
| 376 | // will need to sync and access both collections at once. |
| 377 | long flowId = installedFlowPath.flowId().value(); |
Jonathan Hart | 5e44878 | 2013-12-10 12:36:35 -0800 | [diff] [blame] | 378 | |
Jonathan Hart | 7e6df36 | 2013-12-10 23:33:59 -0800 | [diff] [blame] | 379 | Collection<PacketToPush> packets; |
| 380 | synchronized (lock) { |
| 381 | packets = waitingPackets.removeAll(flowId); |
| 382 | |
| 383 | //remove pending flows entry |
| 384 | Path pathToRemove = new Path(installedFlowPath.dataPath().srcPort(), |
| 385 | installedFlowPath.dataPath().dstPort()); |
| 386 | pendingFlows.remove(pathToRemove); |
| 387 | |
| 388 | } |
Jonathan Hart | 5e44878 | 2013-12-10 12:36:35 -0800 | [diff] [blame] | 389 | |
| 390 | for (PacketToPush packet : packets) { |
| 391 | IOFSwitch sw = floodlightProvider.getSwitches().get(packet.dpid); |
| 392 | |
| 393 | OFPacketOut po = packet.packet; |
| 394 | short outPort = |
| 395 | installedFlowPath.flowEntries().get(0).outPort().value(); |
| 396 | po.getActions().add(new OFActionOutput(outPort)); |
| 397 | po.setActionsLength((short) |
| 398 | (po.getActionsLength() + OFActionOutput.MINIMUM_LENGTH)); |
| 399 | po.setLengthU(po.getLengthU() + OFActionOutput.MINIMUM_LENGTH); |
| 400 | |
Jonathan Hart | 7e6df36 | 2013-12-10 23:33:59 -0800 | [diff] [blame] | 401 | flowPusher.add(sw, po); |
Jonathan Hart | 41d1e91 | 2013-11-24 16:50:25 -0800 | [diff] [blame] | 402 | } |
| 403 | } |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 404 | } |