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