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