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