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; |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 7 | import java.util.LinkedList; |
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; |
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 | |
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.util.MACAddress; |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 18 | import net.onrc.onos.api.packet.IPacketListener; |
| 19 | import net.onrc.onos.api.packet.IPacketService; |
Jonathan Hart | 0961fe8 | 2014-04-03 09:56:25 -0700 | [diff] [blame] | 20 | import net.onrc.onos.apps.proxyarp.IProxyArpService; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 21 | import net.onrc.onos.core.intent.Intent; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 22 | import net.onrc.onos.core.intent.Intent.IntentState; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 23 | import net.onrc.onos.core.intent.IntentMap; |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 24 | import net.onrc.onos.core.intent.IntentMap.ChangedEvent; |
| 25 | import net.onrc.onos.core.intent.IntentMap.ChangedListener; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 26 | import net.onrc.onos.core.intent.IntentOperation; |
| 27 | import net.onrc.onos.core.intent.IntentOperationList; |
| 28 | import net.onrc.onos.core.intent.PathIntent; |
| 29 | import net.onrc.onos.core.intent.ShortestPathIntent; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 30 | import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 31 | import net.onrc.onos.core.packet.Ethernet; |
| 32 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 33 | import net.onrc.onos.core.topology.Host; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 34 | import net.onrc.onos.core.topology.ITopologyService; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 35 | import net.onrc.onos.core.topology.LinkEvent; |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 36 | import net.onrc.onos.core.topology.Port; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 37 | import net.onrc.onos.core.topology.Switch; |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 38 | import net.onrc.onos.core.topology.Topology; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 39 | import net.onrc.onos.core.util.FlowPath; |
Yuta HIGUCHI | fb56450 | 2014-06-16 21:29:00 -0700 | [diff] [blame] | 40 | import net.onrc.onos.core.util.PortNumber; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 41 | import net.onrc.onos.core.util.SwitchPort; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 42 | |
Jonathan Hart | c78b8f6 | 2014-08-07 22:31:09 -0700 | [diff] [blame] | 43 | import org.projectfloodlight.openflow.util.HexString; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 44 | import org.slf4j.Logger; |
| 45 | import org.slf4j.LoggerFactory; |
| 46 | |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 47 | import com.google.common.collect.LinkedListMultimap; |
| 48 | import com.google.common.collect.ListMultimap; |
Jonathan Hart | 5e44878 | 2013-12-10 12:36:35 -0800 | [diff] [blame] | 49 | |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 50 | public class Forwarding implements /*IOFMessageListener,*/ IFloodlightModule, |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 51 | IForwardingService, IPacketListener, ChangedListener { |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 52 | private static final Logger log = LoggerFactory.getLogger(Forwarding.class); |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 53 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 54 | private static final int SLEEP_TIME_FOR_DB_HOST_INSTALLED = 100; // milliseconds |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 55 | private static final int NUMBER_OF_THREAD_FOR_EXECUTOR = 1; |
TeruU | 30c0c93 | 2014-05-15 16:47:41 -0700 | [diff] [blame] | 56 | private static final int SRC_SWITCH_TIMEOUT_ADJUST_SECOND = 2; |
| 57 | private static final int DEFAULT_IDLE_TIMEOUT = 5; |
| 58 | private int idleTimeout = DEFAULT_IDLE_TIMEOUT; |
Pavlin Radoslavov | 902fe52 | 2014-03-31 10:11:31 -0700 | [diff] [blame] | 59 | |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 60 | private static final ScheduledExecutorService EXECUTOR_SERVICE = |
| 61 | Executors.newScheduledThreadPool(NUMBER_OF_THREAD_FOR_EXECUTOR); |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 62 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 63 | private final String callerId = "Forwarding"; |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 64 | |
TeruU | 435df32 | 2014-06-16 23:45:13 -0700 | [diff] [blame] | 65 | private final HighLevelIntentChangedHandler highLevelIntentChangedHandler = |
| 66 | new HighLevelIntentChangedHandler(); |
| 67 | |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 68 | private IPacketService packetService; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 69 | private IControllerRegistryService controllerRegistryService; |
| 70 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 71 | private ITopologyService topologyService; |
| 72 | private Topology topology; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 73 | private IPathCalcRuntimeService pathRuntime; |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 74 | private IntentMap pathIntentMap; |
| 75 | private IntentMap highLevelIntentMap; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 76 | |
| 77 | // TODO it seems there is a Guava collection that will time out entries. |
| 78 | // We should see if this will work here. |
| 79 | private Map<Path, PushedFlow> pendingFlows; |
| 80 | private ListMultimap<String, PacketToPush> waitingPackets; |
| 81 | |
| 82 | private final Object lock = new Object(); |
| 83 | |
TeruU | 435df32 | 2014-06-16 23:45:13 -0700 | [diff] [blame] | 84 | private class HighLevelIntentChangedHandler implements ChangedListener { |
| 85 | |
| 86 | @Override |
| 87 | public void intentsChange(LinkedList<ChangedEvent> events) { |
| 88 | for (ChangedEvent event : events) { |
| 89 | ShortestPathIntent spfIntent = null; |
| 90 | if (event.intent instanceof ShortestPathIntent) { |
| 91 | spfIntent = (ShortestPathIntent) event.intent; |
| 92 | log.trace("ShortestPathIntent {}", spfIntent); |
| 93 | } |
| 94 | |
| 95 | if (spfIntent == null) { |
| 96 | log.trace("ShortestPathIntent is null. Skip."); |
| 97 | continue; |
| 98 | } |
| 99 | |
| 100 | switch(event.eventType) { |
| 101 | case ADDED: |
| 102 | break; |
| 103 | case REMOVED: |
| 104 | break; |
| 105 | case STATE_CHANGED: |
| 106 | if (spfIntent.getState() == IntentState.INST_NACK) { |
| 107 | flowRemoved(spfIntent); |
| 108 | } |
| 109 | break; |
| 110 | default: |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | } |
| 117 | |
Jonathan Hart | 8ed69c5 | 2014-04-09 13:29:16 -0700 | [diff] [blame] | 118 | private static class PacketToPush { |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 119 | public final Ethernet eth; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 120 | public final long dpid; |
| 121 | |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 122 | public PacketToPush(Ethernet eth, long dpid) { |
| 123 | this.eth = eth; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 124 | this.dpid = dpid; |
| 125 | } |
| 126 | } |
| 127 | |
Jonathan Hart | 8ed69c5 | 2014-04-09 13:29:16 -0700 | [diff] [blame] | 128 | private static class PushedFlow { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 129 | public final String intentId; |
| 130 | public boolean installed = false; |
| 131 | public short firstOutPort; |
| 132 | |
| 133 | public PushedFlow(String flowId) { |
| 134 | this.intentId = flowId; |
| 135 | } |
| 136 | } |
| 137 | |
Jonathan Hart | 8ed69c5 | 2014-04-09 13:29:16 -0700 | [diff] [blame] | 138 | private static final class Path { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 139 | public final MACAddress srcMac; |
| 140 | public final MACAddress dstMac; |
| 141 | |
| 142 | public Path(MACAddress srcMac, MACAddress dstMac) { |
| 143 | this.srcMac = srcMac; |
| 144 | this.dstMac = dstMac; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public boolean equals(Object other) { |
| 149 | if (!(other instanceof Path)) { |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | Path otherPath = (Path) other; |
| 154 | return srcMac.equals(otherPath.srcMac) && |
| 155 | dstMac.equals(otherPath.dstMac); |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public int hashCode() { |
| 160 | int hash = 17; |
| 161 | hash = 31 * hash + srcMac.hashCode(); |
| 162 | hash = 31 * hash + dstMac.hashCode(); |
| 163 | return hash; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public String toString() { |
| 168 | return "(" + srcMac + ") => (" + dstMac + ")"; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 174 | List<Class<? extends IFloodlightService>> services = |
| 175 | new ArrayList<Class<? extends IFloodlightService>>(1); |
| 176 | services.add(IForwardingService.class); |
| 177 | return services; |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 182 | Map<Class<? extends IFloodlightService>, IFloodlightService> impls = |
| 183 | new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(1); |
| 184 | impls.put(IForwardingService.class, this); |
| 185 | return impls; |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 190 | List<Class<? extends IFloodlightService>> dependencies = |
| 191 | new ArrayList<Class<? extends IFloodlightService>>(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 192 | dependencies.add(IControllerRegistryService.class); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 193 | dependencies.add(ITopologyService.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 194 | dependencies.add(IPathCalcRuntimeService.class); |
| 195 | // We don't use the IProxyArpService directly, but reactive forwarding |
| 196 | // requires it to be loaded and answering ARP requests |
| 197 | dependencies.add(IProxyArpService.class); |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 198 | dependencies.add(IPacketService.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 199 | return dependencies; |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public void init(FloodlightModuleContext context) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 204 | controllerRegistryService = context.getServiceImpl(IControllerRegistryService.class); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 205 | topologyService = context.getServiceImpl(ITopologyService.class); |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 206 | pathRuntime = context.getServiceImpl(IPathCalcRuntimeService.class); |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 207 | packetService = context.getServiceImpl(IPacketService.class); |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 208 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 209 | pendingFlows = new HashMap<Path, PushedFlow>(); |
| 210 | waitingPackets = LinkedListMultimap.create(); |
| 211 | } |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 212 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 213 | @Override |
| 214 | public void startUp(FloodlightModuleContext context) { |
TeruU | 30c0c93 | 2014-05-15 16:47:41 -0700 | [diff] [blame] | 215 | Map<String, String> configOptions = context.getConfigParams(this); |
| 216 | |
| 217 | try { |
| 218 | if (Integer.parseInt(configOptions.get("idletimeout")) > 0) { |
| 219 | idleTimeout = Integer.parseInt(configOptions.get("idletimeout")); |
| 220 | log.info("idle_timeout for Forwarding is set to {}.", idleTimeout); |
| 221 | } else { |
| 222 | log.info("idle_timeout for Forwarding is less than 0. Use default {}.", idleTimeout); |
| 223 | } |
| 224 | } catch (NumberFormatException e) { |
| 225 | log.info("idle_timeout related config options were not set. Use default."); |
| 226 | } |
| 227 | |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 228 | packetService.registerPacketListener(this); |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 229 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 230 | topology = topologyService.getTopology(); |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 231 | highLevelIntentMap = pathRuntime.getHighLevelIntents(); |
TeruU | 435df32 | 2014-06-16 23:45:13 -0700 | [diff] [blame] | 232 | highLevelIntentMap.addChangeListener(highLevelIntentChangedHandler); |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 233 | pathIntentMap = pathRuntime.getPathIntents(); |
| 234 | pathIntentMap.addChangeListener(this); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 235 | } |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 236 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 237 | @Override |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 238 | public void receive(Switch sw, Port inPort, Ethernet eth) { |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 239 | if (log.isTraceEnabled()) { |
| 240 | log.trace("Receive PACKET_IN swId {}, portId {}", sw.getDpid(), inPort.getNumber()); |
| 241 | } |
TeruU | 417fe02 | 2014-02-04 12:59:30 -0800 | [diff] [blame] | 242 | |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 243 | if (eth.getEtherType() != Ethernet.TYPE_IPV4) { |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 244 | // Only handle IPv4 packets right now |
| 245 | return; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 246 | } |
TeruU | 417fe02 | 2014-02-04 12:59:30 -0800 | [diff] [blame] | 247 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 248 | if (eth.isBroadcast() || eth.isMulticast()) { |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 249 | handleBroadcast(sw, inPort, eth); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 250 | } else { |
| 251 | // Unicast |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 252 | handlePacketIn(sw, inPort, eth); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 253 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 254 | } |
TeruU | 417fe02 | 2014-02-04 12:59:30 -0800 | [diff] [blame] | 255 | |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 256 | private void handleBroadcast(Switch sw, Port inPort, Ethernet eth) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 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 | |
Jonathan Hart | e3702f2 | 2014-04-29 02:56:56 -0700 | [diff] [blame] | 261 | packetService.broadcastPacketOutEdge(eth, |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 262 | new SwitchPort(sw.getDpid(), inPort.getNumber())); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 263 | } |
Pavlin Radoslavov | a3818db | 2014-03-20 19:26:08 -0700 | [diff] [blame] | 264 | |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 265 | private void handlePacketIn(Switch sw, Port inPort, Ethernet eth) { |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 266 | if (log.isTraceEnabled()) { |
| 267 | log.trace("Start handlePacketIn swId {}, portId {}", sw.getDpid(), inPort.getNumber()); |
| 268 | } |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 269 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 270 | String destinationMac = |
| 271 | HexString.toHexString(eth.getDestinationMACAddress()); |
Jonathan Hart | 0444d93 | 2014-01-22 15:06:17 -0800 | [diff] [blame] | 272 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 273 | // FIXME #getHostByMac() is a blocking call, so it may be better way |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 274 | // to handle it to avoid the condition. |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 275 | Host hostObject = topology.getHostByMac( |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 276 | MACAddress.valueOf(destinationMac)); |
TeruU | 6464af0 | 2014-02-06 21:38:45 -0800 | [diff] [blame] | 277 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 278 | if (hostObject == null) { |
| 279 | log.debug("No host entry found for {}", |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 280 | destinationMac); |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 281 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 282 | //Host is not in the DB, so wait it until the host is added. |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 283 | EXECUTOR_SERVICE.schedule(new WaitDeviceArp(sw, inPort, eth), |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 284 | SLEEP_TIME_FOR_DB_HOST_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 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 288 | continueHandlePacketIn(sw, inPort, eth, hostObject); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 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 { |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 292 | Switch sw; |
| 293 | Port inPort; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 294 | Ethernet eth; |
TeruU | 417fe02 | 2014-02-04 12:59:30 -0800 | [diff] [blame] | 295 | |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 296 | public WaitDeviceArp(Switch sw, Port inPort, Ethernet eth) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 297 | super(); |
| 298 | this.sw = sw; |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 299 | this.inPort = inPort; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 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() { |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 305 | Host hostObject = topology.getHostByMac(MACAddress.valueOf(eth.getDestinationMACAddress())); |
| 306 | if (hostObject == null) { |
| 307 | log.debug("wait {}ms and host was not found. " + |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 308 | "Send broadcast packet and the thread finish.", |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 309 | SLEEP_TIME_FOR_DB_HOST_INSTALLED); |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 310 | handleBroadcast(sw, inPort, eth); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 311 | return; |
| 312 | } |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 313 | log.debug("wait {}ms and host {} was found, continue", |
| 314 | SLEEP_TIME_FOR_DB_HOST_INSTALLED, hostObject.getMacAddress()); |
| 315 | continueHandlePacketIn(sw, inPort, eth, hostObject); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 316 | } |
| 317 | } |
Jonathan Hart | 0444d93 | 2014-01-22 15:06:17 -0800 | [diff] [blame] | 318 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 319 | private void continueHandlePacketIn(Switch sw, Port inPort, Ethernet eth, Host hostObject) { |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 320 | |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 321 | log.trace("Start continuehandlePacketIn"); |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 322 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 323 | //Iterator<IPortObject> ports = hostObject.getAttachedPorts().iterator(); |
| 324 | Iterator<net.onrc.onos.core.topology.Port> ports = hostObject.getAttachmentPoints().iterator(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 325 | if (!ports.hasNext()) { |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 326 | log.debug("No attachment point found for host {} - broadcasting packet", |
| 327 | hostObject.getMacAddress()); |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 328 | handleBroadcast(sw, inPort, eth); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 329 | return; |
| 330 | } |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 331 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 332 | //This code assumes the host has only one port. It should be problem. |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 333 | Port destinationPort = ports.next(); |
Yuta HIGUCHI | 9da3a6e | 2014-06-10 22:11:58 -0700 | [diff] [blame] | 334 | short destinationPortNum = destinationPort.getNumber().shortValue(); |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 335 | Switch destinationSw = destinationPort.getSwitch(); |
| 336 | long destinationDpid = destinationSw.getDpid().value(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 337 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 338 | SwitchPort srcSwitchPort = new SwitchPort( |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 339 | sw.getDpid(), |
| 340 | inPort.getNumber()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 341 | SwitchPort dstSwitchPort = new SwitchPort( |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 342 | destinationSw.getDpid(), |
| 343 | destinationPort.getNumber()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 344 | |
| 345 | MACAddress srcMacAddress = MACAddress.valueOf(eth.getSourceMACAddress()); |
| 346 | MACAddress dstMacAddress = MACAddress.valueOf(eth.getDestinationMACAddress()); |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 347 | Path pathspec = new Path(srcMacAddress, dstMacAddress); |
| 348 | IntentOperationList operations = new IntentOperationList(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 349 | |
| 350 | synchronized (lock) { |
| 351 | //TODO check concurrency |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 352 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 353 | PushedFlow existingFlow = pendingFlows.get(pathspec); |
| 354 | |
| 355 | //A path is installed side by side to reduce a path timeout and a wrong state. |
| 356 | if (existingFlow != null) { |
| 357 | // We've already start to install a flow for this pair of MAC addresses |
| 358 | if (log.isDebugEnabled()) { |
| 359 | log.debug("Found existing the same pathspec {}, intent ID is {}", |
| 360 | pathspec, |
| 361 | existingFlow.intentId); |
| 362 | } |
| 363 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 364 | // Find the correct port here. We just assume the PI is from |
| 365 | // the first hop switch, but this is definitely not always |
| 366 | // the case. We'll have to retrieve the flow from HZ every time |
| 367 | // because it could change (be rerouted) sometimes. |
| 368 | if (existingFlow.installed) { |
| 369 | // Flow has been sent to the switches so it is safe to |
| 370 | // send a packet out now |
| 371 | |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 372 | // TODO Here highLevelIntentMap and pathIntentMap would be problem, |
| 373 | // because it doesn't have global information as of May 2014. |
| 374 | // However usually these lines here is used when we got packet-in and this class think |
| 375 | // the path for the packet is installed already, so it is pretty rare. |
| 376 | // I will leave it for now, and will work in the next step. |
| 377 | Intent highLevelIntent = highLevelIntentMap.getIntent(existingFlow.intentId); |
| 378 | if (highLevelIntent == null) { |
| 379 | log.debug("Intent ID {} is null in HighLevelIntentMap. return.", existingFlow.intentId); |
| 380 | return; |
| 381 | } |
| 382 | |
| 383 | if (highLevelIntent.getState() != IntentState.INST_ACK) { |
| 384 | log.debug("Intent ID {}'s state is not INST_ACK. return.", existingFlow.intentId); |
| 385 | return; |
| 386 | } |
| 387 | |
| 388 | ShortestPathIntent spfIntent = null; |
| 389 | if (highLevelIntent instanceof ShortestPathIntent) { |
| 390 | spfIntent = (ShortestPathIntent) highLevelIntent; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 391 | } else { |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 392 | log.debug("Intent ID {} is not PathIntent or null. return.", existingFlow.intentId); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 393 | return; |
| 394 | } |
| 395 | |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 396 | PathIntent pathIntent = (PathIntent) pathIntentMap.getIntent(spfIntent.getPathIntentId()); |
| 397 | if (pathIntent == null) { |
| 398 | log.debug("PathIntent ID {} is null in PathIntentMap. return.", existingFlow.intentId); |
| 399 | return; |
| 400 | } |
| 401 | |
| 402 | if (pathIntent.getState() != IntentState.INST_ACK) { |
| 403 | log.debug("Intent ID {}'s state is not INST_ACK. return.", existingFlow.intentId); |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | boolean isflowEntryForThisSwitch = false; |
Yuta HIGUCHI | 1fc395e | 2014-05-13 14:06:28 -0700 | [diff] [blame] | 408 | net.onrc.onos.core.intent.Path path = pathIntent.getPath(); |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 409 | long outPort = -1; |
| 410 | |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 411 | if (spfIntent.getDstSwitchDpid() == sw.getDpid().value()) { |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 412 | log.trace("The packet-in sw dpid {} is on the path.", sw.getDpid()); |
| 413 | isflowEntryForThisSwitch = true; |
| 414 | outPort = spfIntent.getDstPortNumber(); |
| 415 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 416 | |
Ray Milkey | 7f1567c | 2014-04-08 13:53:32 -0700 | [diff] [blame] | 417 | for (Iterator<LinkEvent> i = path.iterator(); i.hasNext();) { |
Ray Milkey | 149693c | 2014-05-20 14:58:53 -0700 | [diff] [blame] | 418 | LinkEvent le = i.next(); |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 419 | |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 420 | if (le.getSrc().getDpid().equals(sw.getDpid())) { |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 421 | log.trace("The packet-in sw dpid {} is on the path.", sw.getDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 422 | isflowEntryForThisSwitch = true; |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 423 | outPort = le.getSrc().getPortNumber().value(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 424 | break; |
| 425 | } |
| 426 | } |
| 427 | |
Ray Milkey | 6c4f2fe | 2014-04-11 09:47:23 -0700 | [diff] [blame] | 428 | if (!isflowEntryForThisSwitch) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 429 | // If we don't find a flow entry for that switch, then we're |
| 430 | // in the middle of a rerouting (or something's gone wrong). |
| 431 | // This packet will be dropped as a victim of the rerouting. |
| 432 | log.debug("Dropping packet on flow {} between {}-{}", |
| 433 | existingFlow.intentId, |
| 434 | srcMacAddress, dstMacAddress); |
| 435 | } else { |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 436 | if (outPort < 0) { |
| 437 | outPort = existingFlow.firstOutPort; |
| 438 | } |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 439 | |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 440 | log.debug("Sending packet out from sw {}, outport{}", sw.getDpid(), outPort); |
Jonathan Hart | e3702f2 | 2014-04-29 02:56:56 -0700 | [diff] [blame] | 441 | packetService.sendPacket(eth, new SwitchPort( |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 442 | sw.getDpid(), new PortNumber((short) outPort))); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 443 | } |
| 444 | } else { |
| 445 | // Flow path has not yet been installed to switches so save the |
| 446 | // packet out for later |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 447 | log.trace("Put a packet into the waiting list. flowId {}", existingFlow.intentId); |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 448 | waitingPackets.put(existingFlow.intentId, new PacketToPush(eth, sw.getDpid().value())); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 449 | } |
| 450 | return; |
| 451 | } |
| 452 | |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 453 | String intentId = Long.toString(controllerRegistryService.getNextUniqueId()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 454 | ShortestPathIntent intent = new ShortestPathIntent(intentId, |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 455 | sw.getDpid().value(), inPort.getNumber().value(), srcMacAddress.toLong(), |
| 456 | destinationDpid, destinationPortNum, dstMacAddress.toLong()); |
Ray Milkey | ff73514 | 2014-05-22 19:06:02 -0700 | [diff] [blame] | 457 | |
TeruU | 30c0c93 | 2014-05-15 16:47:41 -0700 | [diff] [blame] | 458 | intent.setIdleTimeout(idleTimeout + SRC_SWITCH_TIMEOUT_ADJUST_SECOND); |
| 459 | intent.setFirstSwitchIdleTimeout(idleTimeout); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 460 | IntentOperation.Operator operator = IntentOperation.Operator.ADD; |
| 461 | operations.add(operator, intent); |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 462 | log.debug("Adding new flow between {} at {} and {} at {}", |
| 463 | new Object[]{srcMacAddress, srcSwitchPort, dstMacAddress, dstSwitchPort}); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 464 | |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 465 | // Add to waiting lists |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 466 | waitingPackets.put(intentId, new PacketToPush(eth, sw.getDpid().value())); |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 467 | log.trace("Put a Packet in the wating list. intent ID {}, related pathspec {}", intentId, pathspec); |
| 468 | pendingFlows.put(pathspec, new PushedFlow(intentId)); |
| 469 | log.trace("Put a Path {} in the pending flow, intent ID {}", pathspec, intentId); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 470 | } |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 471 | pathRuntime.executeIntentOperations(operations); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 474 | @Override |
| 475 | public void flowsInstalled(Collection<FlowPath> installedFlowPaths) { |
| 476 | } |
| 477 | |
| 478 | @Override |
| 479 | public void flowRemoved(FlowPath removedFlowPath) { |
| 480 | } |
| 481 | |
TeruU | 435df32 | 2014-06-16 23:45:13 -0700 | [diff] [blame] | 482 | public void flowRemoved(ShortestPathIntent spfIntent) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 483 | if (log.isTraceEnabled()) { |
TeruU | 435df32 | 2014-06-16 23:45:13 -0700 | [diff] [blame] | 484 | log.trace("ShortestPathIntent {} was removed", spfIntent.getId()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 487 | MACAddress srcMacAddress = MACAddress.valueOf(spfIntent.getSrcMac()); |
| 488 | MACAddress dstMacAddress = MACAddress.valueOf(spfIntent.getDstMac()); |
| 489 | Path removedPath = new Path(srcMacAddress, dstMacAddress); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 490 | synchronized (lock) { |
| 491 | // There *shouldn't* be any packets queued if the flow has |
| 492 | // just been removed. |
| 493 | List<PacketToPush> packets = waitingPackets.removeAll(spfIntent.getId()); |
| 494 | if (!packets.isEmpty()) { |
| 495 | log.warn("Removed flow {} has packets queued.", spfIntent.getId()); |
| 496 | } |
TeruU | 9e53066 | 2014-05-18 11:49:37 -0700 | [diff] [blame] | 497 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 498 | pendingFlows.remove(removedPath); |
| 499 | log.debug("Removed from the pendingFlow: Path {}, Flow ID {}", removedPath, spfIntent.getId()); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | private void flowInstalled(PathIntent installedPath) { |
| 504 | if (log.isTraceEnabled()) { |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 505 | log.trace("Installed intent ID {}, path {}", |
| 506 | installedPath.getParentIntent().getId(), installedPath.getPath()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | ShortestPathIntent spfIntent = (ShortestPathIntent) installedPath.getParentIntent(); |
| 510 | MACAddress srcMacAddress = MACAddress.valueOf(spfIntent.getSrcMac()); |
| 511 | MACAddress dstMacAddress = MACAddress.valueOf(spfIntent.getDstMac()); |
| 512 | Path path = new Path(srcMacAddress, dstMacAddress); |
| 513 | log.debug("Path spec {}", path); |
| 514 | |
| 515 | // TODO waiting packets should time out. We could request a path that |
| 516 | // can't be installed right now because of a network partition. The path |
| 517 | // may eventually be installed, but we may have received thousands of |
| 518 | // packets in the meantime and probably don't want to send very old packets. |
| 519 | |
| 520 | List<PacketToPush> packets = null; |
Yuta HIGUCHI | 1fc395e | 2014-05-13 14:06:28 -0700 | [diff] [blame] | 521 | net.onrc.onos.core.intent.Path graphPath = installedPath.getPath(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 522 | |
TeruU | 220c45e | 2014-04-10 18:56:26 -0700 | [diff] [blame] | 523 | short outPort; |
| 524 | if (graphPath.isEmpty()) { |
| 525 | outPort = (short) spfIntent.getDstPortNumber(); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 526 | log.debug("Path is empty. Maybe hosts on the same switch. outPort {}", outPort); |
TeruU | 220c45e | 2014-04-10 18:56:26 -0700 | [diff] [blame] | 527 | } else { |
Yuta HIGUCHI | 9da3a6e | 2014-06-10 22:11:58 -0700 | [diff] [blame] | 528 | outPort = graphPath.get(0).getSrc().getPortNumber().shortValue(); |
TeruU | 220c45e | 2014-04-10 18:56:26 -0700 | [diff] [blame] | 529 | log.debug("path{}, outPort {}", graphPath, outPort); |
| 530 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 531 | |
| 532 | PushedFlow existingFlow = null; |
| 533 | |
| 534 | synchronized (lock) { |
| 535 | existingFlow = pendingFlows.get(path); |
| 536 | |
| 537 | if (existingFlow != null) { |
| 538 | existingFlow.installed = true; |
| 539 | existingFlow.firstOutPort = outPort; |
| 540 | } else { |
| 541 | log.debug("ExistingFlow {} is null", path); |
| 542 | return; |
| 543 | } |
| 544 | |
| 545 | //Check both existing flow are installed status. |
| 546 | if (existingFlow.installed) { |
| 547 | packets = waitingPackets.removeAll(existingFlow.intentId); |
| 548 | if (log.isDebugEnabled()) { |
| 549 | log.debug("removed my packets {} to push from waitingPackets. outPort {} size {}", |
| 550 | existingFlow.intentId, existingFlow.firstOutPort, packets.size()); |
| 551 | } |
| 552 | } else { |
| 553 | log.debug("Forward or reverse flows hasn't been pushed yet. return"); |
| 554 | return; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | for (PacketToPush packet : packets) { |
TeruU | bd5b90e | 2014-06-16 14:29:33 -0700 | [diff] [blame] | 559 | log.debug("Start packetToPush to sw {}, outPort {}, path {}", |
| 560 | packet.dpid, existingFlow.firstOutPort, path); |
Jonathan Hart | e3702f2 | 2014-04-29 02:56:56 -0700 | [diff] [blame] | 561 | packetService.sendPacket(packet.eth, new SwitchPort( |
| 562 | packet.dpid, existingFlow.firstOutPort)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 566 | @Override |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 567 | public void intentsChange(LinkedList<ChangedEvent> events) { |
| 568 | for (ChangedEvent event : events) { |
| 569 | log.debug("path intent ID {}, eventType {}", event.intent.getId() , event.eventType); |
TeruU | bd5b90e | 2014-06-16 14:29:33 -0700 | [diff] [blame] | 570 | |
| 571 | PathIntent pathIntent = null; |
| 572 | if (event.intent instanceof PathIntent) { |
| 573 | pathIntent = (PathIntent) event.intent; |
| 574 | log.trace("pathIntent {}", pathIntent); |
| 575 | } |
| 576 | |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 577 | if (pathIntent == null) { |
TeruU | 435df32 | 2014-06-16 23:45:13 -0700 | [diff] [blame] | 578 | log.trace("pathIntent is null. Skip."); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 579 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 580 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 581 | |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 582 | if (!(pathIntent.getParentIntent() instanceof ShortestPathIntent)) { |
TeruU | 435df32 | 2014-06-16 23:45:13 -0700 | [diff] [blame] | 583 | log.trace("parentIntent is not ShortestPathIntent. Skip."); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 584 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 585 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 586 | |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 587 | switch(event.eventType) { |
| 588 | case ADDED: |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 589 | break; |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 590 | case REMOVED: |
TeruU | 435df32 | 2014-06-16 23:45:13 -0700 | [diff] [blame] | 591 | flowRemoved((ShortestPathIntent) pathIntent.getParentIntent()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 592 | break; |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 593 | case STATE_CHANGED: |
| 594 | IntentState state = pathIntent.getState(); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 595 | log.debug("This is STATE_CHANGED. intent id {} is now state {}", |
| 596 | pathIntent.getId() , state); |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 597 | switch (state) { |
| 598 | case INST_REQ: |
| 599 | break; |
| 600 | case INST_ACK: |
| 601 | flowInstalled(pathIntent); |
| 602 | break; |
| 603 | case INST_NACK: |
TeruU | 435df32 | 2014-06-16 23:45:13 -0700 | [diff] [blame] | 604 | flowRemoved((ShortestPathIntent) pathIntent.getParentIntent()); |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 605 | break; |
| 606 | case DEL_REQ: |
| 607 | break; |
| 608 | case DEL_ACK: |
TeruU | 435df32 | 2014-06-16 23:45:13 -0700 | [diff] [blame] | 609 | flowRemoved((ShortestPathIntent) pathIntent.getParentIntent()); |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 610 | break; |
| 611 | case DEL_PENDING: |
| 612 | break; |
TeruU | bd5b90e | 2014-06-16 14:29:33 -0700 | [diff] [blame] | 613 | case REROUTE_REQ: |
| 614 | break; |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 615 | default: |
| 616 | break; |
| 617 | } |
Ray Milkey | 149693c | 2014-05-20 14:58:53 -0700 | [diff] [blame] | 618 | break; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 619 | default: |
| 620 | break; |
| 621 | } |
| 622 | } |
| 623 | } |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 624 | } |