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