Jonathan Hart | 0961fe8 | 2014-04-03 09:56:25 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.forwarding; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 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; |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 9 | import java.util.Map.Entry; |
TeruU | 417fe02 | 2014-02-04 12:59:30 -0800 | [diff] [blame] | 10 | import java.util.concurrent.Executors; |
| 11 | import java.util.concurrent.ScheduledExecutorService; |
| 12 | import java.util.concurrent.TimeUnit; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 13 | |
| 14 | import net.floodlightcontroller.core.FloodlightContext; |
| 15 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 16 | import net.floodlightcontroller.core.IOFMessageListener; |
| 17 | import net.floodlightcontroller.core.IOFSwitch; |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 18 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 19 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 20 | import net.floodlightcontroller.core.module.IFloodlightService; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 21 | import net.floodlightcontroller.util.MACAddress; |
Jonathan Hart | 0961fe8 | 2014-04-03 09:56:25 -0700 | [diff] [blame] | 22 | import net.onrc.onos.apps.proxyarp.IProxyArpService; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 23 | import net.onrc.onos.core.datagrid.IDatagridService; |
| 24 | import net.onrc.onos.core.datagrid.IEventChannel; |
| 25 | import net.onrc.onos.core.datagrid.IEventChannelListener; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 26 | import net.onrc.onos.core.devicemanager.IOnosDeviceService; |
| 27 | import net.onrc.onos.core.flowprogrammer.IFlowPusherService; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 28 | import net.onrc.onos.core.intent.Intent; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 29 | import net.onrc.onos.core.intent.Intent.IntentState; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 30 | import net.onrc.onos.core.intent.IntentMap; |
| 31 | import net.onrc.onos.core.intent.IntentOperation; |
| 32 | import net.onrc.onos.core.intent.IntentOperationList; |
| 33 | import net.onrc.onos.core.intent.PathIntent; |
| 34 | import net.onrc.onos.core.intent.ShortestPathIntent; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 35 | import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService; |
| 36 | import net.onrc.onos.core.intent.runtime.IntentStateList; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 37 | import net.onrc.onos.core.packet.Ethernet; |
Jonathan Hart | 313fdf0 | 2014-04-10 14:09:46 -0700 | [diff] [blame^] | 38 | import net.onrc.onos.core.packetservice.BroadcastPacketOutNotification; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 39 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 40 | import net.onrc.onos.core.topology.Device; |
| 41 | import net.onrc.onos.core.topology.INetworkGraphService; |
| 42 | import net.onrc.onos.core.topology.LinkEvent; |
| 43 | import net.onrc.onos.core.topology.NetworkGraph; |
| 44 | import net.onrc.onos.core.topology.Switch; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 45 | import net.onrc.onos.core.util.Dpid; |
| 46 | import net.onrc.onos.core.util.FlowPath; |
| 47 | import net.onrc.onos.core.util.Port; |
| 48 | import net.onrc.onos.core.util.SwitchPort; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 49 | |
| 50 | import org.openflow.protocol.OFMessage; |
| 51 | import org.openflow.protocol.OFPacketIn; |
Jonathan Hart | 41d1e91 | 2013-11-24 16:50:25 -0800 | [diff] [blame] | 52 | import org.openflow.protocol.OFPacketOut; |
| 53 | import org.openflow.protocol.OFPort; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 54 | import org.openflow.protocol.OFType; |
Jonathan Hart | 41d1e91 | 2013-11-24 16:50:25 -0800 | [diff] [blame] | 55 | import org.openflow.protocol.action.OFAction; |
| 56 | import org.openflow.protocol.action.OFActionOutput; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 57 | import org.openflow.util.HexString; |
| 58 | import org.slf4j.Logger; |
| 59 | import org.slf4j.LoggerFactory; |
| 60 | |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 61 | import com.google.common.collect.LinkedListMultimap; |
| 62 | import com.google.common.collect.ListMultimap; |
Jonathan Hart | 5e44878 | 2013-12-10 12:36:35 -0800 | [diff] [blame] | 63 | |
| 64 | public class Forwarding implements IOFMessageListener, IFloodlightModule, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 65 | IForwardingService, IEventChannelListener<Long, IntentStateList> { |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 66 | private static final Logger log = LoggerFactory.getLogger(Forwarding.class); |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 67 | |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 68 | private static final int SLEEP_TIME_FOR_DB_DEVICE_INSTALLED = 100; // milliseconds |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 69 | private static final int NUMBER_OF_THREAD_FOR_EXECUTOR = 1; |
Pavlin Radoslavov | 902fe52 | 2014-03-31 10:11:31 -0700 | [diff] [blame] | 70 | |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 71 | private static final ScheduledExecutorService EXECUTOR_SERVICE = Executors.newScheduledThreadPool(NUMBER_OF_THREAD_FOR_EXECUTOR); |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 72 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 73 | private final String callerId = "Forwarding"; |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 74 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 75 | private IFloodlightProviderService floodlightProvider; |
| 76 | private IFlowPusherService flowPusher; |
| 77 | private IDatagridService datagrid; |
| 78 | |
| 79 | private IEventChannel<Long, BroadcastPacketOutNotification> eventChannel; |
| 80 | private static final String SINGLE_PACKET_OUT_CHANNEL_NAME = "onos.forwarding.packet_out"; |
| 81 | |
| 82 | private IControllerRegistryService controllerRegistryService; |
| 83 | |
| 84 | private INetworkGraphService networkGraphService; |
| 85 | private NetworkGraph networkGraph; |
| 86 | private IPathCalcRuntimeService pathRuntime; |
| 87 | private IntentMap intentMap; |
| 88 | |
| 89 | // TODO it seems there is a Guava collection that will time out entries. |
| 90 | // We should see if this will work here. |
| 91 | private Map<Path, PushedFlow> pendingFlows; |
| 92 | private ListMultimap<String, PacketToPush> waitingPackets; |
| 93 | |
| 94 | private final Object lock = new Object(); |
| 95 | |
Jonathan Hart | 8ed69c5 | 2014-04-09 13:29:16 -0700 | [diff] [blame] | 96 | private static class PacketToPush { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 97 | public final OFPacketOut packet; |
| 98 | public final long dpid; |
| 99 | |
| 100 | public PacketToPush(OFPacketOut packet, long dpid) { |
| 101 | this.packet = packet; |
| 102 | this.dpid = dpid; |
| 103 | } |
| 104 | } |
| 105 | |
Jonathan Hart | 8ed69c5 | 2014-04-09 13:29:16 -0700 | [diff] [blame] | 106 | private static class PushedFlow { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 107 | public final String intentId; |
| 108 | public boolean installed = false; |
| 109 | public short firstOutPort; |
| 110 | |
| 111 | public PushedFlow(String flowId) { |
| 112 | this.intentId = flowId; |
| 113 | } |
| 114 | } |
| 115 | |
Jonathan Hart | 8ed69c5 | 2014-04-09 13:29:16 -0700 | [diff] [blame] | 116 | private static final class Path { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 117 | public final MACAddress srcMac; |
| 118 | public final MACAddress dstMac; |
| 119 | |
| 120 | public Path(MACAddress srcMac, MACAddress dstMac) { |
| 121 | this.srcMac = srcMac; |
| 122 | this.dstMac = dstMac; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public boolean equals(Object other) { |
| 127 | if (!(other instanceof Path)) { |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | Path otherPath = (Path) other; |
| 132 | return srcMac.equals(otherPath.srcMac) && |
| 133 | dstMac.equals(otherPath.dstMac); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public int hashCode() { |
| 138 | int hash = 17; |
| 139 | hash = 31 * hash + srcMac.hashCode(); |
| 140 | hash = 31 * hash + dstMac.hashCode(); |
| 141 | return hash; |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public String toString() { |
| 146 | return "(" + srcMac + ") => (" + dstMac + ")"; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 152 | List<Class<? extends IFloodlightService>> services = |
| 153 | new ArrayList<Class<? extends IFloodlightService>>(1); |
| 154 | services.add(IForwardingService.class); |
| 155 | return services; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 160 | Map<Class<? extends IFloodlightService>, IFloodlightService> impls = |
| 161 | new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(1); |
| 162 | impls.put(IForwardingService.class, this); |
| 163 | return impls; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 168 | List<Class<? extends IFloodlightService>> dependencies = |
| 169 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 170 | dependencies.add(IFloodlightProviderService.class); |
| 171 | dependencies.add(IFlowPusherService.class); |
| 172 | dependencies.add(IControllerRegistryService.class); |
| 173 | dependencies.add(IOnosDeviceService.class); |
| 174 | dependencies.add(IDatagridService.class); |
| 175 | dependencies.add(INetworkGraphService.class); |
| 176 | dependencies.add(IPathCalcRuntimeService.class); |
| 177 | // We don't use the IProxyArpService directly, but reactive forwarding |
| 178 | // requires it to be loaded and answering ARP requests |
| 179 | dependencies.add(IProxyArpService.class); |
| 180 | return dependencies; |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public void init(FloodlightModuleContext context) { |
| 185 | floodlightProvider = |
| 186 | context.getServiceImpl(IFloodlightProviderService.class); |
| 187 | flowPusher = context.getServiceImpl(IFlowPusherService.class); |
| 188 | datagrid = context.getServiceImpl(IDatagridService.class); |
| 189 | controllerRegistryService = context.getServiceImpl(IControllerRegistryService.class); |
| 190 | networkGraphService = context.getServiceImpl(INetworkGraphService.class); |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 191 | pathRuntime = context.getServiceImpl(IPathCalcRuntimeService.class); |
Jonathan Hart | 0444d93 | 2014-01-22 15:06:17 -0800 | [diff] [blame] | 192 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 193 | floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this); |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 194 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 195 | pendingFlows = new HashMap<Path, PushedFlow>(); |
| 196 | waitingPackets = LinkedListMultimap.create(); |
| 197 | } |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 198 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 199 | @Override |
| 200 | public void startUp(FloodlightModuleContext context) { |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 201 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 202 | eventChannel = datagrid.createChannel(SINGLE_PACKET_OUT_CHANNEL_NAME, |
| 203 | Long.class, |
| 204 | BroadcastPacketOutNotification.class); |
| 205 | networkGraph = networkGraphService.getNetworkGraph(); |
| 206 | intentMap = pathRuntime.getPathIntents(); |
| 207 | datagrid.addListener("onos.pathintent_state", this, Long.class, IntentStateList.class); |
| 208 | } |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 209 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 210 | @Override |
| 211 | public String getName() { |
| 212 | return "onosforwarding"; |
| 213 | } |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 214 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 215 | @Override |
| 216 | public boolean isCallbackOrderingPrereq(OFType type, String name) { |
| 217 | return (type == OFType.PACKET_IN) && |
| 218 | (name.equals("devicemanager") || name.equals("proxyarpmanager") |
| 219 | || name.equals("onosdevicemanager")); |
| 220 | } |
Jonathan Hart | 7804bea | 2014-01-07 10:50:52 -0800 | [diff] [blame] | 221 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 222 | @Override |
| 223 | public boolean isCallbackOrderingPostreq(OFType type, String name) { |
| 224 | return false; |
| 225 | } |
TeruU | 417fe02 | 2014-02-04 12:59:30 -0800 | [diff] [blame] | 226 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 227 | @Override |
| 228 | public Command receive( |
| 229 | IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { |
TeruU | d1ba0e2 | 2014-02-10 11:44:15 -0800 | [diff] [blame] | 230 | |
Jonathan Hart | 8ed69c5 | 2014-04-09 13:29:16 -0700 | [diff] [blame] | 231 | if (msg.getType() != OFType.PACKET_IN || !(msg instanceof OFPacketIn)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 232 | return Command.CONTINUE; |
| 233 | } |
TeruU | d1ba0e2 | 2014-02-10 11:44:15 -0800 | [diff] [blame] | 234 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 235 | OFPacketIn pi = (OFPacketIn) msg; |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 236 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 237 | Ethernet eth = IFloodlightProviderService.bcStore. |
| 238 | get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD); |
TeruU | 417fe02 | 2014-02-04 12:59:30 -0800 | [diff] [blame] | 239 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 240 | log.debug("Receive PACKET_IN swId {}, portId {}", sw.getId(), pi.getInPort()); |
TeruU | 417fe02 | 2014-02-04 12:59:30 -0800 | [diff] [blame] | 241 | |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 242 | if (eth.getEtherType() != Ethernet.TYPE_IPV4) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 243 | return Command.CONTINUE; |
| 244 | } |
TeruU | 417fe02 | 2014-02-04 12:59:30 -0800 | [diff] [blame] | 245 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 246 | if (eth.isBroadcast() || eth.isMulticast()) { |
| 247 | handleBroadcast(sw, pi, eth); |
| 248 | } else { |
| 249 | // Unicast |
| 250 | handlePacketIn(sw, pi, eth); |
| 251 | } |
TeruU | 417fe02 | 2014-02-04 12:59:30 -0800 | [diff] [blame] | 252 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 253 | return Command.STOP; |
| 254 | } |
TeruU | 417fe02 | 2014-02-04 12:59:30 -0800 | [diff] [blame] | 255 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [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 | 0444d93 | 2014-01-22 15:06:17 -0800 | [diff] [blame] | 260 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 261 | //We don't use address information, so 0 is put into the third argument. |
| 262 | BroadcastPacketOutNotification key = |
| 263 | new BroadcastPacketOutNotification( |
| 264 | eth.serialize(), |
| 265 | 0, sw.getId(), |
| 266 | pi.getInPort()); |
| 267 | eventChannel.addTransientEntry(eth.getDestinationMAC().toLong(), key); |
| 268 | } |
Pavlin Radoslavov | a3818db | 2014-03-20 19:26:08 -0700 | [diff] [blame] | 269 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 270 | private void handlePacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth) { |
| 271 | log.debug("Start handlePacketIn swId {}, portId {}", sw.getId(), pi.getInPort()); |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 272 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 273 | String destinationMac = |
| 274 | HexString.toHexString(eth.getDestinationMACAddress()); |
Jonathan Hart | 0444d93 | 2014-01-22 15:06:17 -0800 | [diff] [blame] | 275 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 276 | //FIXME getDeviceByMac() is a blocking call, so it may be better way to handle it to avoid the condition. |
| 277 | Device deviceObject = networkGraph.getDeviceByMac(MACAddress.valueOf(destinationMac)); |
TeruU | 6464af0 | 2014-02-06 21:38:45 -0800 | [diff] [blame] | 278 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 279 | if (deviceObject == null) { |
| 280 | log.debug("No device entry found for {}", |
| 281 | destinationMac); |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 282 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 283 | //Device is not in the DB, so wait it until the device is added. |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 284 | EXECUTOR_SERVICE.schedule(new WaitDeviceArp(sw, pi, eth), SLEEP_TIME_FOR_DB_DEVICE_INSTALLED, TimeUnit.MILLISECONDS); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 285 | return; |
| 286 | } |
Pavlin Radoslavov | 7208e9a | 2013-12-11 14:31:07 -0800 | [diff] [blame] | 287 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 288 | continueHandlePacketIn(sw, pi, eth, deviceObject); |
| 289 | } |
TeruU | 417fe02 | 2014-02-04 12:59:30 -0800 | [diff] [blame] | 290 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 291 | private class WaitDeviceArp implements Runnable { |
| 292 | IOFSwitch sw; |
| 293 | OFPacketIn pi; |
| 294 | Ethernet eth; |
TeruU | 417fe02 | 2014-02-04 12:59:30 -0800 | [diff] [blame] | 295 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 296 | public WaitDeviceArp(IOFSwitch sw, OFPacketIn pi, Ethernet eth) { |
| 297 | super(); |
| 298 | this.sw = sw; |
| 299 | this.pi = pi; |
| 300 | this.eth = eth; |
| 301 | } |
TeruU | 417fe02 | 2014-02-04 12:59:30 -0800 | [diff] [blame] | 302 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 303 | @Override |
| 304 | public void run() { |
| 305 | Device deviceObject = networkGraph.getDeviceByMac(MACAddress.valueOf(eth.getDestinationMACAddress())); |
| 306 | if (deviceObject == null) { |
| 307 | log.debug("wait {}ms and device was not found. Send broadcast packet and the thread finish.", SLEEP_TIME_FOR_DB_DEVICE_INSTALLED); |
| 308 | handleBroadcast(sw, pi, eth); |
| 309 | return; |
| 310 | } |
| 311 | log.debug("wait {}ms and device {} was found, continue", SLEEP_TIME_FOR_DB_DEVICE_INSTALLED, deviceObject.getMacAddress()); |
| 312 | continueHandlePacketIn(sw, pi, eth, deviceObject); |
| 313 | } |
| 314 | } |
Jonathan Hart | 0444d93 | 2014-01-22 15:06:17 -0800 | [diff] [blame] | 315 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 316 | private void continueHandlePacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth, Device deviceObject) { |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 317 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 318 | log.debug("Start continuehandlePacketIn"); |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 319 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 320 | //Iterator<IPortObject> ports = deviceObject.getAttachedPorts().iterator(); |
| 321 | Iterator<net.onrc.onos.core.topology.Port> ports = deviceObject.getAttachmentPoints().iterator(); |
| 322 | if (!ports.hasNext()) { |
| 323 | log.debug("No attachment point found for device {} - broadcasting packet", |
| 324 | deviceObject.getMacAddress()); |
| 325 | handleBroadcast(sw, pi, eth); |
| 326 | return; |
| 327 | } |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 328 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 329 | //This code assumes the device has only one port. It should be problem. |
| 330 | net.onrc.onos.core.topology.Port portObject = ports.next(); |
| 331 | short destinationPort = portObject.getNumber().shortValue(); |
| 332 | Switch switchObject = portObject.getSwitch(); |
| 333 | long destinationDpid = switchObject.getDpid(); |
| 334 | |
| 335 | // TODO SwitchPort, Dpid and Port should probably be immutable |
| 336 | SwitchPort srcSwitchPort = new SwitchPort( |
| 337 | new Dpid(sw.getId()), new Port(pi.getInPort())); |
| 338 | SwitchPort dstSwitchPort = new SwitchPort( |
| 339 | new Dpid(destinationDpid), new Port(destinationPort)); |
| 340 | |
| 341 | MACAddress srcMacAddress = MACAddress.valueOf(eth.getSourceMACAddress()); |
| 342 | MACAddress dstMacAddress = MACAddress.valueOf(eth.getDestinationMACAddress()); |
| 343 | |
| 344 | synchronized (lock) { |
| 345 | //TODO check concurrency |
| 346 | Path pathspec = new Path(srcMacAddress, dstMacAddress); |
| 347 | PushedFlow existingFlow = pendingFlows.get(pathspec); |
| 348 | |
| 349 | //A path is installed side by side to reduce a path timeout and a wrong state. |
| 350 | if (existingFlow != null) { |
| 351 | // We've already start to install a flow for this pair of MAC addresses |
| 352 | if (log.isDebugEnabled()) { |
| 353 | log.debug("Found existing the same pathspec {}, intent ID is {}", |
| 354 | pathspec, |
| 355 | existingFlow.intentId); |
| 356 | } |
| 357 | |
| 358 | OFPacketOut po = constructPacketOut(pi, sw); |
| 359 | |
| 360 | // Find the correct port here. We just assume the PI is from |
| 361 | // the first hop switch, but this is definitely not always |
| 362 | // the case. We'll have to retrieve the flow from HZ every time |
| 363 | // because it could change (be rerouted) sometimes. |
| 364 | if (existingFlow.installed) { |
| 365 | // Flow has been sent to the switches so it is safe to |
| 366 | // send a packet out now |
| 367 | |
| 368 | Intent intent = intentMap.getIntent(existingFlow.intentId); |
| 369 | PathIntent pathIntent = null; |
| 370 | if (intent instanceof PathIntent) { |
| 371 | pathIntent = (PathIntent) intent; |
| 372 | } else { |
| 373 | log.debug("Intent {} is not PathIntent. Return.", intent.getId()); |
| 374 | return; |
| 375 | } |
| 376 | |
| 377 | Boolean isflowEntryForThisSwitch = false; |
| 378 | net.onrc.onos.core.topology.Path path = pathIntent.getPath(); |
| 379 | |
Ray Milkey | 7f1567c | 2014-04-08 13:53:32 -0700 | [diff] [blame] | 380 | for (Iterator<LinkEvent> i = path.iterator(); i.hasNext();) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 381 | LinkEvent le = (LinkEvent) i.next(); |
| 382 | if (le.getSrc().dpid == sw.getId()) { |
| 383 | log.debug("src {} dst {}", le.getSrc(), le.getDst()); |
| 384 | isflowEntryForThisSwitch = true; |
| 385 | break; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | if (isflowEntryForThisSwitch == false) { |
| 390 | // If we don't find a flow entry for that switch, then we're |
| 391 | // in the middle of a rerouting (or something's gone wrong). |
| 392 | // This packet will be dropped as a victim of the rerouting. |
| 393 | log.debug("Dropping packet on flow {} between {}-{}", |
| 394 | existingFlow.intentId, |
| 395 | srcMacAddress, dstMacAddress); |
| 396 | } else { |
| 397 | log.debug("Sending packet out from sw {}, outport{}", sw, existingFlow.firstOutPort); |
| 398 | sendPacketOut(sw, po, existingFlow.firstOutPort); |
| 399 | } |
| 400 | } else { |
| 401 | // Flow path has not yet been installed to switches so save the |
| 402 | // packet out for later |
| 403 | log.debug("Put a packet into the waitng list. flowId {}", existingFlow.intentId); |
| 404 | waitingPackets.put(existingFlow.intentId, new PacketToPush(po, sw.getId())); |
| 405 | } |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | log.debug("Adding new flow between {} at {} and {} at {}", |
| 410 | new Object[]{srcMacAddress, srcSwitchPort, dstMacAddress, dstSwitchPort}); |
| 411 | |
| 412 | String intentId = callerId + ":" + controllerRegistryService.getNextUniqueId(); |
| 413 | IntentOperationList operations = new IntentOperationList(); |
| 414 | ShortestPathIntent intent = new ShortestPathIntent(intentId, |
| 415 | sw.getId(), pi.getInPort(), srcMacAddress.toLong(), |
| 416 | destinationDpid, destinationPort, dstMacAddress.toLong()); |
| 417 | IntentOperation.Operator operator = IntentOperation.Operator.ADD; |
| 418 | operations.add(operator, intent); |
| 419 | pathRuntime.executeIntentOperations(operations); |
| 420 | |
| 421 | OFPacketOut po = constructPacketOut(pi, sw); |
| 422 | |
| 423 | // Add to waiting lists |
| 424 | pendingFlows.put(pathspec, new PushedFlow(intentId)); |
| 425 | log.debug("Put a Path {} in the pending flow, intent ID {}", pathspec, intentId); |
| 426 | waitingPackets.put(intentId, new PacketToPush(po, sw.getId())); |
| 427 | log.debug("Put a Packet in the wating list. related pathspec {}", pathspec); |
| 428 | |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | private OFPacketOut constructPacketOut(OFPacketIn pi, IOFSwitch sw) { |
| 433 | OFPacketOut po = new OFPacketOut(); |
| 434 | po.setInPort(OFPort.OFPP_NONE) |
| 435 | .setInPort(pi.getInPort()) |
| 436 | .setActions(new ArrayList<OFAction>()) |
| 437 | .setLengthU(OFPacketOut.MINIMUM_LENGTH); |
| 438 | |
| 439 | if (sw.getBuffers() == 0) { |
| 440 | po.setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 441 | .setPacketData(pi.getPacketData()) |
| 442 | .setLengthU(po.getLengthU() + po.getPacketData().length); |
| 443 | } else { |
| 444 | po.setBufferId(pi.getBufferId()); |
| 445 | } |
| 446 | |
| 447 | return po; |
| 448 | } |
| 449 | |
| 450 | @Override |
| 451 | public void flowsInstalled(Collection<FlowPath> installedFlowPaths) { |
| 452 | } |
| 453 | |
| 454 | @Override |
| 455 | public void flowRemoved(FlowPath removedFlowPath) { |
| 456 | } |
| 457 | |
| 458 | public void flowRemoved(PathIntent removedIntent) { |
| 459 | if (log.isTraceEnabled()) { |
| 460 | log.trace("Path {} was removed", removedIntent.getParentIntent().getId()); |
| 461 | } |
| 462 | |
| 463 | ShortestPathIntent spfIntent = (ShortestPathIntent) removedIntent.getParentIntent(); |
| 464 | MACAddress srcMacAddress = MACAddress.valueOf(spfIntent.getSrcMac()); |
| 465 | MACAddress dstMacAddress = MACAddress.valueOf(spfIntent.getDstMac()); |
| 466 | Path removedPath = new Path(srcMacAddress, dstMacAddress); |
| 467 | |
| 468 | synchronized (lock) { |
| 469 | // There *shouldn't* be any packets queued if the flow has |
| 470 | // just been removed. |
| 471 | List<PacketToPush> packets = waitingPackets.removeAll(spfIntent.getId()); |
| 472 | if (!packets.isEmpty()) { |
| 473 | log.warn("Removed flow {} has packets queued.", spfIntent.getId()); |
| 474 | } |
| 475 | pendingFlows.remove(removedPath); |
| 476 | log.debug("Removed from the pendingFlow: Path {}, Flow ID {}", removedPath, spfIntent.getId()); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | private void flowInstalled(PathIntent installedPath) { |
| 481 | if (log.isTraceEnabled()) { |
| 482 | log.trace("Path {} was installed", installedPath.getParentIntent().getId()); |
| 483 | } |
| 484 | |
| 485 | ShortestPathIntent spfIntent = (ShortestPathIntent) installedPath.getParentIntent(); |
| 486 | MACAddress srcMacAddress = MACAddress.valueOf(spfIntent.getSrcMac()); |
| 487 | MACAddress dstMacAddress = MACAddress.valueOf(spfIntent.getDstMac()); |
| 488 | Path path = new Path(srcMacAddress, dstMacAddress); |
| 489 | log.debug("Path spec {}", path); |
| 490 | |
| 491 | // TODO waiting packets should time out. We could request a path that |
| 492 | // can't be installed right now because of a network partition. The path |
| 493 | // may eventually be installed, but we may have received thousands of |
| 494 | // packets in the meantime and probably don't want to send very old packets. |
| 495 | |
| 496 | List<PacketToPush> packets = null; |
| 497 | net.onrc.onos.core.topology.Path graphPath = installedPath.getPath(); |
| 498 | |
| 499 | log.debug("path{}", graphPath); |
| 500 | Short outPort = graphPath.get(0).getSrc().getNumber().shortValue(); |
| 501 | |
| 502 | PushedFlow existingFlow = null; |
| 503 | |
| 504 | synchronized (lock) { |
| 505 | existingFlow = pendingFlows.get(path); |
| 506 | |
| 507 | if (existingFlow != null) { |
| 508 | existingFlow.installed = true; |
| 509 | existingFlow.firstOutPort = outPort; |
| 510 | } else { |
| 511 | log.debug("ExistingFlow {} is null", path); |
| 512 | return; |
| 513 | } |
| 514 | |
| 515 | //Check both existing flow are installed status. |
| 516 | if (existingFlow.installed) { |
| 517 | packets = waitingPackets.removeAll(existingFlow.intentId); |
| 518 | if (log.isDebugEnabled()) { |
| 519 | log.debug("removed my packets {} to push from waitingPackets. outPort {} size {}", |
| 520 | existingFlow.intentId, existingFlow.firstOutPort, packets.size()); |
| 521 | } |
| 522 | } else { |
| 523 | log.debug("Forward or reverse flows hasn't been pushed yet. return"); |
| 524 | return; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | for (PacketToPush packet : packets) { |
| 529 | log.debug("Start packetToPush to sw {}, outPort {}, path {}", packet.dpid, existingFlow.firstOutPort, path); |
| 530 | IOFSwitch sw = floodlightProvider.getSwitches().get(packet.dpid); |
| 531 | sendPacketOut(sw, packet.packet, existingFlow.firstOutPort); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | private void sendPacketOut(IOFSwitch sw, OFPacketOut po, short outPort) { |
| 536 | po.getActions().add(new OFActionOutput(outPort)); |
| 537 | po.setActionsLength((short) |
| 538 | (po.getActionsLength() + OFActionOutput.MINIMUM_LENGTH)); |
| 539 | po.setLengthU(po.getLengthU() + OFActionOutput.MINIMUM_LENGTH); |
| 540 | |
| 541 | flowPusher.add(sw, po); |
| 542 | } |
| 543 | |
| 544 | @Override |
| 545 | public void entryAdded(IntentStateList value) { |
| 546 | entryUpdated(value); |
| 547 | |
| 548 | } |
| 549 | |
| 550 | @Override |
| 551 | public void entryRemoved(IntentStateList value) { |
| 552 | //no-op |
| 553 | } |
| 554 | |
| 555 | @Override |
| 556 | public void entryUpdated(IntentStateList value) { |
| 557 | for (Entry<String, IntentState> entry : value.entrySet()) { |
| 558 | log.debug("path intent key {}, value {}", entry.getKey(), entry.getValue()); |
| 559 | PathIntent pathIntent = (PathIntent) intentMap.getIntent(entry.getKey()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 560 | if (pathIntent == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 561 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 562 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 563 | |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 564 | if (!(pathIntent.getParentIntent() instanceof ShortestPathIntent)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 565 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 566 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 567 | |
| 568 | IntentState state = entry.getValue(); |
| 569 | switch (state) { |
| 570 | case INST_REQ: |
| 571 | break; |
| 572 | case INST_ACK: |
| 573 | flowInstalled(pathIntent); |
| 574 | break; |
| 575 | case INST_NACK: |
| 576 | break; |
| 577 | case DEL_REQ: |
| 578 | break; |
| 579 | case DEL_ACK: |
| 580 | flowRemoved(pathIntent); |
| 581 | break; |
| 582 | case DEL_PENDING: |
| 583 | break; |
| 584 | default: |
| 585 | break; |
| 586 | } |
| 587 | } |
| 588 | } |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 589 | } |