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