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