Jonathan Hart | 0961fe8 | 2014-04-03 09:56:25 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.forwarding; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 2 | |
Jonathan Hart | 41d1e91 | 2013-11-24 16:50:25 -0800 | [diff] [blame] | 3 | import java.util.ArrayList; |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 4 | import java.util.Collection; |
Jonathan Hart | d33a6cf | 2013-12-10 14:29:08 -0800 | [diff] [blame] | 5 | import java.util.HashMap; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 6 | import java.util.Iterator; |
Jonathan Hart | 41d1e91 | 2013-11-24 16:50:25 -0800 | [diff] [blame] | 7 | import java.util.List; |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 8 | import java.util.Map; |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 9 | import java.util.Map.Entry; |
TeruU | 417fe02 | 2014-02-04 12:59:30 -0800 | [diff] [blame] | 10 | import java.util.concurrent.Executors; |
| 11 | import java.util.concurrent.ScheduledExecutorService; |
| 12 | import java.util.concurrent.TimeUnit; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 13 | |
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 | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 21 | import net.onrc.onos.core.datagrid.IDatagridService; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 22 | import net.onrc.onos.core.datagrid.IEventChannelListener; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 23 | import net.onrc.onos.core.devicemanager.IOnosDeviceService; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 24 | import net.onrc.onos.core.intent.Intent; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 25 | import net.onrc.onos.core.intent.Intent.IntentState; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 26 | import net.onrc.onos.core.intent.IntentMap; |
| 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; |
| 32 | import net.onrc.onos.core.intent.runtime.IntentStateList; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 33 | import net.onrc.onos.core.packet.Ethernet; |
| 34 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 35 | import net.onrc.onos.core.topology.Device; |
| 36 | import net.onrc.onos.core.topology.INetworkGraphService; |
| 37 | import net.onrc.onos.core.topology.LinkEvent; |
| 38 | import net.onrc.onos.core.topology.NetworkGraph; |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 39 | import net.onrc.onos.core.topology.Port; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 40 | import net.onrc.onos.core.topology.Switch; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 41 | import net.onrc.onos.core.util.Dpid; |
| 42 | import net.onrc.onos.core.util.FlowPath; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 43 | import net.onrc.onos.core.util.SwitchPort; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 44 | |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 45 | import org.openflow.util.HexString; |
| 46 | import org.slf4j.Logger; |
| 47 | import org.slf4j.LoggerFactory; |
| 48 | |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 49 | import com.google.common.collect.LinkedListMultimap; |
| 50 | import com.google.common.collect.ListMultimap; |
Jonathan Hart | 5e44878 | 2013-12-10 12:36:35 -0800 | [diff] [blame] | 51 | |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 52 | public class Forwarding implements /*IOFMessageListener,*/ IFloodlightModule, |
| 53 | IForwardingService, IEventChannelListener<Long, IntentStateList>, |
| 54 | IPacketListener { |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 55 | private static final Logger log = LoggerFactory.getLogger(Forwarding.class); |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 56 | |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 57 | private static final int SLEEP_TIME_FOR_DB_DEVICE_INSTALLED = 100; // milliseconds |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 58 | private static final int NUMBER_OF_THREAD_FOR_EXECUTOR = 1; |
Pavlin Radoslavov | 902fe52 | 2014-03-31 10:11:31 -0700 | [diff] [blame] | 59 | |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 60 | 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] | 61 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 62 | private final String callerId = "Forwarding"; |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 63 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 64 | private IDatagridService datagrid; |
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 | |
| 68 | private INetworkGraphService networkGraphService; |
| 69 | private NetworkGraph networkGraph; |
| 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); |
| 156 | dependencies.add(IDatagridService.class); |
| 157 | dependencies.add(INetworkGraphService.class); |
| 158 | dependencies.add(IPathCalcRuntimeService.class); |
| 159 | // We don't use the IProxyArpService directly, but reactive forwarding |
| 160 | // requires it to be loaded and answering ARP requests |
| 161 | dependencies.add(IProxyArpService.class); |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 162 | dependencies.add(IPacketService.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 163 | return dependencies; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public void init(FloodlightModuleContext context) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 168 | datagrid = context.getServiceImpl(IDatagridService.class); |
| 169 | controllerRegistryService = context.getServiceImpl(IControllerRegistryService.class); |
| 170 | networkGraphService = context.getServiceImpl(INetworkGraphService.class); |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 171 | pathRuntime = context.getServiceImpl(IPathCalcRuntimeService.class); |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 172 | packetService = context.getServiceImpl(IPacketService.class); |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 173 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 174 | pendingFlows = new HashMap<Path, PushedFlow>(); |
| 175 | waitingPackets = LinkedListMultimap.create(); |
| 176 | } |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 177 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 178 | @Override |
| 179 | public void startUp(FloodlightModuleContext context) { |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 180 | packetService.registerPacketListener(this); |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 181 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 182 | networkGraph = networkGraphService.getNetworkGraph(); |
| 183 | intentMap = pathRuntime.getPathIntents(); |
| 184 | datagrid.addListener("onos.pathintent_state", this, Long.class, IntentStateList.class); |
| 185 | } |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 186 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 187 | @Override |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 188 | public void receive(Switch sw, Port inPort, Ethernet eth) { |
| 189 | log.debug("Receive PACKET_IN swId {}, portId {}", sw.getDpid(), inPort.getNumber()); |
TeruU | 417fe02 | 2014-02-04 12:59:30 -0800 | [diff] [blame] | 190 | |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 191 | if (eth.getEtherType() != Ethernet.TYPE_IPV4) { |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 192 | // Only handle IPv4 packets right now |
| 193 | return; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 194 | } |
TeruU | 417fe02 | 2014-02-04 12:59:30 -0800 | [diff] [blame] | 195 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 196 | if (eth.isBroadcast() || eth.isMulticast()) { |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 197 | handleBroadcast(sw, inPort, eth); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 198 | } else { |
| 199 | // Unicast |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 200 | handlePacketIn(sw, inPort, eth); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 201 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 202 | } |
TeruU | 417fe02 | 2014-02-04 12:59:30 -0800 | [diff] [blame] | 203 | |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 204 | private void handleBroadcast(Switch sw, Port inPort, Ethernet eth) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 205 | if (log.isTraceEnabled()) { |
| 206 | log.trace("Sending broadcast packet to other ONOS instances"); |
| 207 | } |
Jonathan Hart | 0444d93 | 2014-01-22 15:06:17 -0800 | [diff] [blame] | 208 | |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 209 | packetService.broadcastPacket(eth, |
| 210 | new SwitchPort(sw.getDpid(), inPort.getNumber().shortValue())); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 211 | } |
Pavlin Radoslavov | a3818db | 2014-03-20 19:26:08 -0700 | [diff] [blame] | 212 | |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 213 | private void handlePacketIn(Switch sw, Port inPort, Ethernet eth) { |
| 214 | log.debug("Start handlePacketIn swId {}, portId {}", sw.getDpid(), inPort.getNumber()); |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 215 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 216 | String destinationMac = |
| 217 | HexString.toHexString(eth.getDestinationMACAddress()); |
Jonathan Hart | 0444d93 | 2014-01-22 15:06:17 -0800 | [diff] [blame] | 218 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 219 | //FIXME getDeviceByMac() is a blocking call, so it may be better way to handle it to avoid the condition. |
| 220 | Device deviceObject = networkGraph.getDeviceByMac(MACAddress.valueOf(destinationMac)); |
TeruU | 6464af0 | 2014-02-06 21:38:45 -0800 | [diff] [blame] | 221 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 222 | if (deviceObject == null) { |
| 223 | log.debug("No device entry found for {}", |
| 224 | destinationMac); |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 225 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 226 | //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] | 227 | 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] | 228 | return; |
| 229 | } |
Pavlin Radoslavov | 7208e9a | 2013-12-11 14:31:07 -0800 | [diff] [blame] | 230 | |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 231 | continueHandlePacketIn(sw, inPort, eth, deviceObject); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 232 | } |
TeruU | 417fe02 | 2014-02-04 12:59:30 -0800 | [diff] [blame] | 233 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 234 | private class WaitDeviceArp implements Runnable { |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 235 | Switch sw; |
| 236 | Port inPort; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 237 | Ethernet eth; |
TeruU | 417fe02 | 2014-02-04 12:59:30 -0800 | [diff] [blame] | 238 | |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 239 | public WaitDeviceArp(Switch sw, Port inPort, Ethernet eth) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 240 | super(); |
| 241 | this.sw = sw; |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 242 | this.inPort = inPort; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 243 | this.eth = eth; |
| 244 | } |
TeruU | 417fe02 | 2014-02-04 12:59:30 -0800 | [diff] [blame] | 245 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 246 | @Override |
| 247 | public void run() { |
| 248 | Device deviceObject = networkGraph.getDeviceByMac(MACAddress.valueOf(eth.getDestinationMACAddress())); |
| 249 | if (deviceObject == null) { |
| 250 | 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] | 251 | handleBroadcast(sw, inPort, eth); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 252 | return; |
| 253 | } |
| 254 | 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] | 255 | continueHandlePacketIn(sw, inPort, eth, deviceObject); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
Jonathan Hart | 0444d93 | 2014-01-22 15:06:17 -0800 | [diff] [blame] | 258 | |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 259 | private void continueHandlePacketIn(Switch sw, Port inPort, Ethernet eth, Device deviceObject) { |
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 | log.debug("Start continuehandlePacketIn"); |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 262 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 263 | //Iterator<IPortObject> ports = deviceObject.getAttachedPorts().iterator(); |
| 264 | Iterator<net.onrc.onos.core.topology.Port> ports = deviceObject.getAttachmentPoints().iterator(); |
| 265 | if (!ports.hasNext()) { |
| 266 | log.debug("No attachment point found for device {} - broadcasting packet", |
| 267 | deviceObject.getMacAddress()); |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 268 | handleBroadcast(sw, inPort, eth); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 269 | return; |
| 270 | } |
TeruU | 7feef8a | 2014-04-03 00:15:49 -0700 | [diff] [blame] | 271 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 272 | //This code assumes the device has only one port. It should be problem. |
| 273 | net.onrc.onos.core.topology.Port portObject = ports.next(); |
| 274 | short destinationPort = portObject.getNumber().shortValue(); |
| 275 | Switch switchObject = portObject.getSwitch(); |
| 276 | long destinationDpid = switchObject.getDpid(); |
| 277 | |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 278 | // TODO eliminate cast |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 279 | SwitchPort srcSwitchPort = new SwitchPort( |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 280 | new Dpid(sw.getDpid()), |
| 281 | new net.onrc.onos.core.util.Port((short) inPort.getNumber().longValue())); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 282 | SwitchPort dstSwitchPort = new SwitchPort( |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 283 | new Dpid(destinationDpid), |
| 284 | new net.onrc.onos.core.util.Port(destinationPort)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 285 | |
| 286 | MACAddress srcMacAddress = MACAddress.valueOf(eth.getSourceMACAddress()); |
| 287 | MACAddress dstMacAddress = MACAddress.valueOf(eth.getDestinationMACAddress()); |
| 288 | |
| 289 | synchronized (lock) { |
| 290 | //TODO check concurrency |
| 291 | Path pathspec = new Path(srcMacAddress, dstMacAddress); |
| 292 | PushedFlow existingFlow = pendingFlows.get(pathspec); |
| 293 | |
| 294 | //A path is installed side by side to reduce a path timeout and a wrong state. |
| 295 | if (existingFlow != null) { |
| 296 | // We've already start to install a flow for this pair of MAC addresses |
| 297 | if (log.isDebugEnabled()) { |
| 298 | log.debug("Found existing the same pathspec {}, intent ID is {}", |
| 299 | pathspec, |
| 300 | existingFlow.intentId); |
| 301 | } |
| 302 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 303 | // Find the correct port here. We just assume the PI is from |
| 304 | // the first hop switch, but this is definitely not always |
| 305 | // the case. We'll have to retrieve the flow from HZ every time |
| 306 | // because it could change (be rerouted) sometimes. |
| 307 | if (existingFlow.installed) { |
| 308 | // Flow has been sent to the switches so it is safe to |
| 309 | // send a packet out now |
| 310 | |
| 311 | Intent intent = intentMap.getIntent(existingFlow.intentId); |
| 312 | PathIntent pathIntent = null; |
| 313 | if (intent instanceof PathIntent) { |
| 314 | pathIntent = (PathIntent) intent; |
| 315 | } else { |
| 316 | log.debug("Intent {} is not PathIntent. Return.", intent.getId()); |
| 317 | return; |
| 318 | } |
| 319 | |
| 320 | Boolean isflowEntryForThisSwitch = false; |
| 321 | net.onrc.onos.core.topology.Path path = pathIntent.getPath(); |
| 322 | |
Ray Milkey | 7f1567c | 2014-04-08 13:53:32 -0700 | [diff] [blame] | 323 | for (Iterator<LinkEvent> i = path.iterator(); i.hasNext();) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 324 | LinkEvent le = (LinkEvent) i.next(); |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 325 | if (le.getSrc().dpid.equals(sw.getDpid())) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 326 | log.debug("src {} dst {}", le.getSrc(), le.getDst()); |
| 327 | isflowEntryForThisSwitch = true; |
| 328 | break; |
| 329 | } |
| 330 | } |
| 331 | |
Ray Milkey | 6c4f2fe | 2014-04-11 09:47:23 -0700 | [diff] [blame] | 332 | if (!isflowEntryForThisSwitch) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 333 | // If we don't find a flow entry for that switch, then we're |
| 334 | // in the middle of a rerouting (or something's gone wrong). |
| 335 | // This packet will be dropped as a victim of the rerouting. |
| 336 | log.debug("Dropping packet on flow {} between {}-{}", |
| 337 | existingFlow.intentId, |
| 338 | srcMacAddress, dstMacAddress); |
| 339 | } else { |
| 340 | log.debug("Sending packet out from sw {}, outport{}", sw, existingFlow.firstOutPort); |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 341 | |
| 342 | packetService.sendPacket(new SwitchPort( |
| 343 | sw.getDpid(), existingFlow.firstOutPort), eth); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 344 | } |
| 345 | } else { |
| 346 | // Flow path has not yet been installed to switches so save the |
| 347 | // packet out for later |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 348 | log.debug("Put a packet into the waiting list. flowId {}", existingFlow.intentId); |
| 349 | waitingPackets.put(existingFlow.intentId, new PacketToPush(eth, sw.getDpid())); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 350 | } |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | log.debug("Adding new flow between {} at {} and {} at {}", |
| 355 | new Object[]{srcMacAddress, srcSwitchPort, dstMacAddress, dstSwitchPort}); |
| 356 | |
| 357 | String intentId = callerId + ":" + controllerRegistryService.getNextUniqueId(); |
| 358 | IntentOperationList operations = new IntentOperationList(); |
| 359 | ShortestPathIntent intent = new ShortestPathIntent(intentId, |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 360 | sw.getDpid(), inPort.getNumber(), srcMacAddress.toLong(), |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 361 | destinationDpid, destinationPort, dstMacAddress.toLong()); |
| 362 | IntentOperation.Operator operator = IntentOperation.Operator.ADD; |
| 363 | operations.add(operator, intent); |
| 364 | pathRuntime.executeIntentOperations(operations); |
| 365 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 366 | // Add to waiting lists |
| 367 | pendingFlows.put(pathspec, new PushedFlow(intentId)); |
| 368 | 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] | 369 | waitingPackets.put(intentId, new PacketToPush(eth, sw.getDpid())); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 370 | log.debug("Put a Packet in the wating list. related pathspec {}", pathspec); |
| 371 | |
| 372 | } |
| 373 | } |
| 374 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 375 | @Override |
| 376 | public void flowsInstalled(Collection<FlowPath> installedFlowPaths) { |
| 377 | } |
| 378 | |
| 379 | @Override |
| 380 | public void flowRemoved(FlowPath removedFlowPath) { |
| 381 | } |
| 382 | |
| 383 | public void flowRemoved(PathIntent removedIntent) { |
| 384 | if (log.isTraceEnabled()) { |
| 385 | log.trace("Path {} was removed", removedIntent.getParentIntent().getId()); |
| 386 | } |
| 387 | |
| 388 | ShortestPathIntent spfIntent = (ShortestPathIntent) removedIntent.getParentIntent(); |
| 389 | MACAddress srcMacAddress = MACAddress.valueOf(spfIntent.getSrcMac()); |
| 390 | MACAddress dstMacAddress = MACAddress.valueOf(spfIntent.getDstMac()); |
| 391 | Path removedPath = new Path(srcMacAddress, dstMacAddress); |
| 392 | |
| 393 | synchronized (lock) { |
| 394 | // There *shouldn't* be any packets queued if the flow has |
| 395 | // just been removed. |
| 396 | List<PacketToPush> packets = waitingPackets.removeAll(spfIntent.getId()); |
| 397 | if (!packets.isEmpty()) { |
| 398 | log.warn("Removed flow {} has packets queued.", spfIntent.getId()); |
| 399 | } |
| 400 | pendingFlows.remove(removedPath); |
| 401 | log.debug("Removed from the pendingFlow: Path {}, Flow ID {}", removedPath, spfIntent.getId()); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | private void flowInstalled(PathIntent installedPath) { |
| 406 | if (log.isTraceEnabled()) { |
| 407 | log.trace("Path {} was installed", installedPath.getParentIntent().getId()); |
| 408 | } |
| 409 | |
| 410 | ShortestPathIntent spfIntent = (ShortestPathIntent) installedPath.getParentIntent(); |
| 411 | MACAddress srcMacAddress = MACAddress.valueOf(spfIntent.getSrcMac()); |
| 412 | MACAddress dstMacAddress = MACAddress.valueOf(spfIntent.getDstMac()); |
| 413 | Path path = new Path(srcMacAddress, dstMacAddress); |
| 414 | log.debug("Path spec {}", path); |
| 415 | |
| 416 | // TODO waiting packets should time out. We could request a path that |
| 417 | // can't be installed right now because of a network partition. The path |
| 418 | // may eventually be installed, but we may have received thousands of |
| 419 | // packets in the meantime and probably don't want to send very old packets. |
| 420 | |
| 421 | List<PacketToPush> packets = null; |
| 422 | net.onrc.onos.core.topology.Path graphPath = installedPath.getPath(); |
| 423 | |
TeruU | 220c45e | 2014-04-10 18:56:26 -0700 | [diff] [blame] | 424 | short outPort; |
| 425 | if (graphPath.isEmpty()) { |
| 426 | outPort = (short) spfIntent.getDstPortNumber(); |
| 427 | log.debug("Path is empty. Maybe devices on the same switch. outPort {}", outPort); |
| 428 | } else { |
| 429 | outPort = graphPath.get(0).getSrc().getNumber().shortValue(); |
| 430 | log.debug("path{}, outPort {}", graphPath, outPort); |
| 431 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 432 | |
| 433 | PushedFlow existingFlow = null; |
| 434 | |
| 435 | synchronized (lock) { |
| 436 | existingFlow = pendingFlows.get(path); |
| 437 | |
| 438 | if (existingFlow != null) { |
| 439 | existingFlow.installed = true; |
| 440 | existingFlow.firstOutPort = outPort; |
| 441 | } else { |
| 442 | log.debug("ExistingFlow {} is null", path); |
| 443 | return; |
| 444 | } |
| 445 | |
| 446 | //Check both existing flow are installed status. |
| 447 | if (existingFlow.installed) { |
| 448 | packets = waitingPackets.removeAll(existingFlow.intentId); |
| 449 | if (log.isDebugEnabled()) { |
| 450 | log.debug("removed my packets {} to push from waitingPackets. outPort {} size {}", |
| 451 | existingFlow.intentId, existingFlow.firstOutPort, packets.size()); |
| 452 | } |
| 453 | } else { |
| 454 | log.debug("Forward or reverse flows hasn't been pushed yet. return"); |
| 455 | return; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | for (PacketToPush packet : packets) { |
| 460 | log.debug("Start packetToPush to sw {}, outPort {}, path {}", packet.dpid, existingFlow.firstOutPort, path); |
Jonathan Hart | f5bd258 | 2014-04-09 17:43:41 -0700 | [diff] [blame] | 461 | packetService.sendPacket(new SwitchPort( |
| 462 | packet.dpid, existingFlow.firstOutPort), packet.eth); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 466 | @Override |
| 467 | public void entryAdded(IntentStateList value) { |
| 468 | entryUpdated(value); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | @Override |
| 472 | public void entryRemoved(IntentStateList value) { |
| 473 | //no-op |
| 474 | } |
| 475 | |
| 476 | @Override |
| 477 | public void entryUpdated(IntentStateList value) { |
| 478 | for (Entry<String, IntentState> entry : value.entrySet()) { |
| 479 | log.debug("path intent key {}, value {}", entry.getKey(), entry.getValue()); |
| 480 | PathIntent pathIntent = (PathIntent) intentMap.getIntent(entry.getKey()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 481 | if (pathIntent == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 482 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 483 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 484 | |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 485 | if (!(pathIntent.getParentIntent() instanceof ShortestPathIntent)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 486 | continue; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 487 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 488 | |
| 489 | IntentState state = entry.getValue(); |
| 490 | switch (state) { |
| 491 | case INST_REQ: |
| 492 | break; |
| 493 | case INST_ACK: |
| 494 | flowInstalled(pathIntent); |
| 495 | break; |
| 496 | case INST_NACK: |
| 497 | break; |
| 498 | case DEL_REQ: |
| 499 | break; |
| 500 | case DEL_ACK: |
| 501 | flowRemoved(pathIntent); |
| 502 | break; |
| 503 | case DEL_PENDING: |
| 504 | break; |
| 505 | default: |
| 506 | break; |
| 507 | } |
| 508 | } |
| 509 | } |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 510 | } |