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