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