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