alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 1 | package org.onlab.onos.provider.of.flow.impl; |
| 2 | |
Jonathan Hart | 1109640 | 2014-10-20 17:31:49 -0700 | [diff] [blame] | 3 | import static org.slf4j.LoggerFactory.getLogger; |
| 4 | |
| 5 | import java.util.Collections; |
| 6 | import java.util.HashMap; |
| 7 | import java.util.List; |
| 8 | import java.util.Map; |
| 9 | import java.util.Set; |
| 10 | import java.util.concurrent.ConcurrentHashMap; |
| 11 | import java.util.concurrent.CountDownLatch; |
| 12 | import java.util.concurrent.ExecutionException; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 13 | import java.util.concurrent.Executor; |
Jonathan Hart | 1109640 | 2014-10-20 17:31:49 -0700 | [diff] [blame] | 14 | import java.util.concurrent.TimeUnit; |
| 15 | import java.util.concurrent.TimeoutException; |
| 16 | import java.util.concurrent.atomic.AtomicBoolean; |
| 17 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 23 | import org.onlab.onos.ApplicationId; |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 24 | import org.onlab.onos.net.DeviceId; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.flow.BatchOperation; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.flow.CompletedBatchOperation; |
| 27 | import org.onlab.onos.net.flow.DefaultFlowEntry; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.flow.FlowEntry; |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.flow.FlowRule; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.flow.FlowRuleBatchEntry; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 31 | import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.flow.FlowRuleProvider; |
| 33 | import org.onlab.onos.net.flow.FlowRuleProviderRegistry; |
| 34 | import org.onlab.onos.net.flow.FlowRuleProviderService; |
| 35 | import org.onlab.onos.net.provider.AbstractProvider; |
| 36 | import org.onlab.onos.net.provider.ProviderId; |
| 37 | import org.onlab.onos.net.topology.TopologyService; |
tom | 9c94c5b | 2014-09-17 13:14:42 -0700 | [diff] [blame] | 38 | import org.onlab.onos.openflow.controller.Dpid; |
| 39 | import org.onlab.onos.openflow.controller.OpenFlowController; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 40 | import org.onlab.onos.openflow.controller.OpenFlowEventListener; |
tom | 9c94c5b | 2014-09-17 13:14:42 -0700 | [diff] [blame] | 41 | import org.onlab.onos.openflow.controller.OpenFlowSwitch; |
alshabib | ce4e578 | 2014-09-17 14:56:42 -0700 | [diff] [blame] | 42 | import org.onlab.onos.openflow.controller.OpenFlowSwitchListener; |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 43 | import org.onlab.onos.openflow.controller.RoleState; |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 44 | import org.projectfloodlight.openflow.protocol.OFActionType; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 45 | import org.projectfloodlight.openflow.protocol.OFBarrierRequest; |
| 46 | import org.projectfloodlight.openflow.protocol.OFErrorMsg; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 47 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 48 | import org.projectfloodlight.openflow.protocol.OFFlowRemoved; |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 49 | import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry; |
| 50 | import org.projectfloodlight.openflow.protocol.OFFlowStatsReply; |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 51 | import org.projectfloodlight.openflow.protocol.OFInstructionType; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 52 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 53 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 54 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 55 | import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags; |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 56 | import org.projectfloodlight.openflow.protocol.OFStatsType; |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 57 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 58 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
| 59 | import org.projectfloodlight.openflow.protocol.action.OFActionOutput; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 60 | import org.projectfloodlight.openflow.protocol.errormsg.OFBadActionErrorMsg; |
| 61 | import org.projectfloodlight.openflow.protocol.errormsg.OFBadInstructionErrorMsg; |
| 62 | import org.projectfloodlight.openflow.protocol.errormsg.OFBadMatchErrorMsg; |
| 63 | import org.projectfloodlight.openflow.protocol.errormsg.OFBadRequestErrorMsg; |
| 64 | import org.projectfloodlight.openflow.protocol.errormsg.OFFlowModFailedErrorMsg; |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 65 | import org.projectfloodlight.openflow.protocol.instruction.OFInstruction; |
| 66 | import org.projectfloodlight.openflow.protocol.instruction.OFInstructionApplyActions; |
| 67 | import org.projectfloodlight.openflow.types.OFPort; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 68 | import org.projectfloodlight.openflow.types.U32; |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 69 | import org.slf4j.Logger; |
| 70 | |
Jonathan Hart | 1109640 | 2014-10-20 17:31:49 -0700 | [diff] [blame] | 71 | import com.google.common.collect.ArrayListMultimap; |
Jonathan Hart | 1109640 | 2014-10-20 17:31:49 -0700 | [diff] [blame] | 72 | import com.google.common.collect.Maps; |
| 73 | import com.google.common.collect.Multimap; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 74 | import com.google.common.collect.Sets; |
| 75 | import com.google.common.util.concurrent.ExecutionList; |
| 76 | import com.google.common.util.concurrent.ListenableFuture; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 77 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 78 | /** |
| 79 | * Provider which uses an OpenFlow controller to detect network |
| 80 | * end-station hosts. |
| 81 | */ |
| 82 | @Component(immediate = true) |
| 83 | public class OpenFlowRuleProvider extends AbstractProvider implements FlowRuleProvider { |
| 84 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 85 | enum BatchState { STARTED, FINISHED, CANCELLED }; |
| 86 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 87 | private final Logger log = getLogger(getClass()); |
| 88 | |
| 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 90 | protected FlowRuleProviderRegistry providerRegistry; |
| 91 | |
| 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 93 | protected OpenFlowController controller; |
| 94 | |
| 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 96 | protected TopologyService topologyService; |
| 97 | |
| 98 | private FlowRuleProviderService providerService; |
| 99 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 100 | private final InternalFlowProvider listener = new InternalFlowProvider(); |
| 101 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 102 | // FIXME: This should be an expiring map to ensure futures that don't have |
| 103 | // a future eventually get garbage collected. |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 104 | private final Map<Long, InstallationFuture> pendingFutures = |
| 105 | new ConcurrentHashMap<Long, InstallationFuture>(); |
| 106 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 107 | private final Map<Long, InstallationFuture> pendingFMs = |
| 108 | new ConcurrentHashMap<Long, InstallationFuture>(); |
| 109 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 110 | private final Map<Dpid, FlowStatsCollector> collectors = Maps.newHashMap(); |
| 111 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 112 | /** |
| 113 | * Creates an OpenFlow host provider. |
| 114 | */ |
| 115 | public OpenFlowRuleProvider() { |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame] | 116 | super(new ProviderId("of", "org.onlab.onos.provider.openflow")); |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Activate |
| 120 | public void activate() { |
| 121 | providerService = providerRegistry.register(this); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 122 | controller.addListener(listener); |
| 123 | controller.addEventListener(listener); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 124 | |
| 125 | for (OpenFlowSwitch sw : controller.getSwitches()) { |
| 126 | FlowStatsCollector fsc = new FlowStatsCollector(sw, POLL_INTERVAL); |
| 127 | fsc.start(); |
| 128 | collectors.put(new Dpid(sw.getId()), fsc); |
| 129 | } |
| 130 | |
| 131 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 132 | log.info("Started"); |
| 133 | } |
| 134 | |
| 135 | @Deactivate |
| 136 | public void deactivate() { |
| 137 | providerRegistry.unregister(this); |
| 138 | providerService = null; |
| 139 | |
| 140 | log.info("Stopped"); |
| 141 | } |
| 142 | @Override |
| 143 | public void applyFlowRule(FlowRule... flowRules) { |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 144 | for (int i = 0; i < flowRules.length; i++) { |
| 145 | applyRule(flowRules[i]); |
| 146 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 147 | } |
| 148 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 149 | private void applyRule(FlowRule flowRule) { |
| 150 | OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri())); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 151 | sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory()).buildFlowAdd()); |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 152 | } |
| 153 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 154 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 155 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 156 | @Override |
| 157 | public void removeFlowRule(FlowRule... flowRules) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 158 | for (int i = 0; i < flowRules.length; i++) { |
| 159 | removeRule(flowRules[i]); |
| 160 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 161 | |
| 162 | } |
| 163 | |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 164 | private void removeRule(FlowRule flowRule) { |
| 165 | OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri())); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 166 | sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory()).buildFlowDel()); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 167 | } |
| 168 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 169 | @Override |
| 170 | public void removeRulesById(ApplicationId id, FlowRule... flowRules) { |
| 171 | // TODO: optimize using the ApplicationId |
| 172 | removeFlowRule(flowRules); |
| 173 | } |
| 174 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 175 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 176 | public ListenableFuture<CompletedBatchOperation> executeBatch(BatchOperation<FlowRuleBatchEntry> batch) { |
Jonathan Hart | 1109640 | 2014-10-20 17:31:49 -0700 | [diff] [blame] | 177 | final Set<Dpid> sws = |
| 178 | Collections.newSetFromMap(new ConcurrentHashMap<Dpid, Boolean>()); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 179 | final Map<Long, FlowRuleBatchEntry> fmXids = new HashMap<Long, FlowRuleBatchEntry>(); |
| 180 | OFFlowMod mod = null; |
| 181 | for (FlowRuleBatchEntry fbe : batch.getOperations()) { |
| 182 | FlowRule flowRule = fbe.getTarget(); |
| 183 | OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri())); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 184 | if (sw == null) { |
alshabib | 3effd04 | 2014-10-17 12:00:31 -0700 | [diff] [blame] | 185 | /* |
| 186 | * if a switch we are supposed to install to is gone then |
| 187 | * cancel (ie. rollback) the work that has been done so far |
| 188 | * and return the associated future. |
| 189 | */ |
| 190 | InstallationFuture failed = new InstallationFuture(sws, fmXids); |
| 191 | failed.cancel(true); |
| 192 | return failed; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 193 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 194 | sws.add(new Dpid(sw.getId())); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 195 | FlowModBuilder builder = FlowModBuilder.builder(flowRule, sw.factory()); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 196 | switch (fbe.getOperator()) { |
| 197 | case ADD: |
| 198 | mod = builder.buildFlowAdd(); |
| 199 | break; |
| 200 | case REMOVE: |
| 201 | mod = builder.buildFlowDel(); |
| 202 | break; |
| 203 | case MODIFY: |
| 204 | mod = builder.buildFlowMod(); |
| 205 | break; |
| 206 | default: |
| 207 | log.error("Unsupported batch operation {}", fbe.getOperator()); |
| 208 | } |
| 209 | if (mod != null) { |
| 210 | sw.sendMsg(mod); |
| 211 | fmXids.put(mod.getXid(), fbe); |
| 212 | } else { |
| 213 | log.error("Conversion of flowrule {} failed.", flowRule); |
| 214 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 215 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 216 | } |
| 217 | InstallationFuture installation = new InstallationFuture(sws, fmXids); |
| 218 | for (Long xid : fmXids.keySet()) { |
| 219 | pendingFMs.put(xid, installation); |
| 220 | } |
| 221 | pendingFutures.put(U32.f(batch.hashCode()), installation); |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 222 | installation.verify(U32.f(batch.hashCode())); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 223 | return installation; |
| 224 | } |
| 225 | |
| 226 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 227 | private class InternalFlowProvider |
| 228 | implements OpenFlowSwitchListener, OpenFlowEventListener { |
| 229 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 230 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 231 | private final Multimap<DeviceId, FlowEntry> completeEntries = |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 232 | ArrayListMultimap.create(); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 233 | |
| 234 | @Override |
| 235 | public void switchAdded(Dpid dpid) { |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 236 | FlowStatsCollector fsc = new FlowStatsCollector(controller.getSwitch(dpid), POLL_INTERVAL); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 237 | fsc.start(); |
| 238 | collectors.put(dpid, fsc); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public void switchRemoved(Dpid dpid) { |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 243 | FlowStatsCollector collector = collectors.remove(dpid); |
| 244 | if (collector != null) { |
| 245 | collector.stop(); |
| 246 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | @Override |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 250 | public void switchChanged(Dpid dpid) { |
| 251 | } |
| 252 | |
| 253 | @Override |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 254 | public void portChanged(Dpid dpid, OFPortStatus status) { |
| 255 | //TODO: Decide whether to evict flows internal store. |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public void handleMessage(Dpid dpid, OFMessage msg) { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 260 | InstallationFuture future = null; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 261 | switch (msg.getType()) { |
| 262 | case FLOW_REMOVED: |
| 263 | OFFlowRemoved removed = (OFFlowRemoved) msg; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 264 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 265 | FlowEntry fr = new FlowEntryBuilder(dpid, removed).build(); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 266 | providerService.flowRemoved(fr); |
| 267 | break; |
| 268 | case STATS_REPLY: |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 269 | pushFlowMetrics(dpid, (OFStatsReply) msg); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 270 | break; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 271 | case BARRIER_REPLY: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 272 | future = pendingFutures.get(msg.getXid()); |
| 273 | if (future != null) { |
| 274 | future.satisfyRequirement(dpid); |
| 275 | } |
| 276 | break; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 277 | case ERROR: |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 278 | future = pendingFMs.get(msg.getXid()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 279 | if (future != null) { |
| 280 | future.fail((OFErrorMsg) msg, dpid); |
| 281 | } |
| 282 | break; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 283 | default: |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 284 | log.debug("Unhandled message type: {}", msg.getType()); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | } |
| 288 | |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 289 | @Override |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 290 | public void roleAssertFailed(Dpid dpid, RoleState role) {} |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 291 | |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 292 | private synchronized void pushFlowMetrics(Dpid dpid, OFStatsReply stats) { |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 293 | if (stats.getStatsType() != OFStatsType.FLOW) { |
| 294 | return; |
| 295 | } |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 296 | DeviceId did = DeviceId.deviceId(Dpid.uri(dpid)); |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 297 | final OFFlowStatsReply replies = (OFFlowStatsReply) stats; |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 298 | //final List<FlowRule> entries = Lists.newLinkedList(); |
| 299 | |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 300 | for (OFFlowStatsEntry reply : replies.getEntries()) { |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 301 | if (!tableMissRule(dpid, reply)) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 302 | completeEntries.put(did, new FlowEntryBuilder(dpid, reply).build()); |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 303 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 304 | } |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 305 | |
| 306 | if (!stats.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
| 307 | log.debug("sending flowstats to core {}", completeEntries.get(did)); |
| 308 | providerService.pushFlowMetrics(did, completeEntries.get(did)); |
| 309 | completeEntries.removeAll(did); |
| 310 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 311 | } |
| 312 | |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 313 | private boolean tableMissRule(Dpid dpid, OFFlowStatsEntry reply) { |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 314 | if (reply.getVersion().equals(OFVersion.OF_10) || |
| 315 | reply.getMatch().getMatchFields().iterator().hasNext()) { |
| 316 | return false; |
| 317 | } |
| 318 | for (OFInstruction ins : reply.getInstructions()) { |
| 319 | if (ins.getType() == OFInstructionType.APPLY_ACTIONS) { |
| 320 | OFInstructionApplyActions apply = (OFInstructionApplyActions) ins; |
| 321 | List<OFAction> acts = apply.getActions(); |
| 322 | for (OFAction act : acts) { |
| 323 | if (act.getType() == OFActionType.OUTPUT) { |
| 324 | OFActionOutput out = (OFActionOutput) act; |
| 325 | if (out.getPort() == OFPort.CONTROLLER) { |
| 326 | return true; |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | return false; |
| 333 | } |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 334 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 335 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 336 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 337 | private class InstallationFuture implements ListenableFuture<CompletedBatchOperation> { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 338 | |
| 339 | private final Set<Dpid> sws; |
| 340 | private final AtomicBoolean ok = new AtomicBoolean(true); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 341 | private final Map<Long, FlowRuleBatchEntry> fms; |
| 342 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 343 | private final Set<FlowEntry> offendingFlowMods = Sets.newHashSet(); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 344 | |
| 345 | private final CountDownLatch countDownLatch; |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 346 | private Long pendingXid; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 347 | private BatchState state; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 348 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 349 | private final ExecutionList executionList = new ExecutionList(); |
| 350 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 351 | public InstallationFuture(Set<Dpid> sws, Map<Long, FlowRuleBatchEntry> fmXids) { |
| 352 | this.state = BatchState.STARTED; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 353 | this.sws = sws; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 354 | this.fms = fmXids; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 355 | countDownLatch = new CountDownLatch(sws.size()); |
| 356 | } |
| 357 | |
| 358 | public void fail(OFErrorMsg msg, Dpid dpid) { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 359 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 360 | ok.set(false); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 361 | removeRequirement(dpid); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 362 | FlowEntry fe = null; |
| 363 | FlowRuleBatchEntry fbe = fms.get(msg.getXid()); |
| 364 | FlowRule offending = fbe.getTarget(); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 365 | //TODO handle specific error msgs |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 366 | switch (msg.getErrType()) { |
| 367 | case BAD_ACTION: |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 368 | OFBadActionErrorMsg bad = (OFBadActionErrorMsg) msg; |
| 369 | fe = new DefaultFlowEntry(offending, bad.getErrType().ordinal(), |
| 370 | bad.getCode().ordinal()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 371 | break; |
| 372 | case BAD_INSTRUCTION: |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 373 | OFBadInstructionErrorMsg badins = (OFBadInstructionErrorMsg) msg; |
| 374 | fe = new DefaultFlowEntry(offending, badins.getErrType().ordinal(), |
| 375 | badins.getCode().ordinal()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 376 | break; |
| 377 | case BAD_MATCH: |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 378 | OFBadMatchErrorMsg badMatch = (OFBadMatchErrorMsg) msg; |
| 379 | fe = new DefaultFlowEntry(offending, badMatch.getErrType().ordinal(), |
| 380 | badMatch.getCode().ordinal()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 381 | break; |
| 382 | case BAD_REQUEST: |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 383 | OFBadRequestErrorMsg badReq = (OFBadRequestErrorMsg) msg; |
| 384 | fe = new DefaultFlowEntry(offending, badReq.getErrType().ordinal(), |
| 385 | badReq.getCode().ordinal()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 386 | break; |
| 387 | case FLOW_MOD_FAILED: |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 388 | OFFlowModFailedErrorMsg fmFail = (OFFlowModFailedErrorMsg) msg; |
| 389 | fe = new DefaultFlowEntry(offending, fmFail.getErrType().ordinal(), |
| 390 | fmFail.getCode().ordinal()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 391 | break; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 392 | case EXPERIMENTER: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 393 | case GROUP_MOD_FAILED: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 394 | case HELLO_FAILED: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 395 | case METER_MOD_FAILED: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 396 | case PORT_MOD_FAILED: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 397 | case QUEUE_OP_FAILED: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 398 | case ROLE_REQUEST_FAILED: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 399 | case SWITCH_CONFIG_FAILED: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 400 | case TABLE_FEATURES_FAILED: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 401 | case TABLE_MOD_FAILED: |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 402 | fe = new DefaultFlowEntry(offending, msg.getErrType().ordinal(), 0); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 403 | break; |
| 404 | default: |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 405 | log.error("Unknown error type {}", msg.getErrType()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 406 | |
| 407 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 408 | offendingFlowMods.add(fe); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 409 | |
| 410 | } |
| 411 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 412 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 413 | public void satisfyRequirement(Dpid dpid) { |
alshabib | 3effd04 | 2014-10-17 12:00:31 -0700 | [diff] [blame] | 414 | log.debug("Satisfaction from switch {}", dpid); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 415 | removeRequirement(dpid); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 416 | } |
| 417 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 418 | |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 419 | public void verify(Long id) { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 420 | pendingXid = id; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 421 | for (Dpid dpid : sws) { |
| 422 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 423 | OFBarrierRequest.Builder builder = sw.factory() |
| 424 | .buildBarrierRequest() |
| 425 | .setXid(id); |
| 426 | sw.sendMsg(builder.build()); |
| 427 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | @Override |
| 431 | public boolean cancel(boolean mayInterruptIfRunning) { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 432 | if (isDone()) { |
| 433 | return false; |
| 434 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 435 | ok.set(false); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 436 | this.state = BatchState.CANCELLED; |
| 437 | cleanUp(); |
| 438 | for (FlowRuleBatchEntry fbe : fms.values()) { |
| 439 | if (fbe.getOperator() == FlowRuleOperation.ADD || |
| 440 | fbe.getOperator() == FlowRuleOperation.MODIFY) { |
| 441 | removeFlowRule(fbe.getTarget()); |
| 442 | } else if (fbe.getOperator() == FlowRuleOperation.REMOVE) { |
| 443 | applyRule(fbe.getTarget()); |
| 444 | } |
| 445 | |
| 446 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 447 | invokeCallbacks(); |
| 448 | return true; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | @Override |
| 452 | public boolean isCancelled() { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 453 | return this.state == BatchState.CANCELLED; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | @Override |
| 457 | public boolean isDone() { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 458 | return this.state == BatchState.FINISHED || isCancelled(); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | @Override |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 462 | public CompletedBatchOperation get() throws InterruptedException, ExecutionException { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 463 | countDownLatch.await(); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 464 | this.state = BatchState.FINISHED; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 465 | CompletedBatchOperation result = new CompletedBatchOperation(ok.get(), offendingFlowMods); |
| 466 | return result; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | @Override |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 470 | public CompletedBatchOperation get(long timeout, TimeUnit unit) |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 471 | throws InterruptedException, ExecutionException, |
| 472 | TimeoutException { |
alshabib | 2683458 | 2014-10-08 20:15:46 -0700 | [diff] [blame] | 473 | if (countDownLatch.await(timeout, unit)) { |
| 474 | this.state = BatchState.FINISHED; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 475 | CompletedBatchOperation result = new CompletedBatchOperation(ok.get(), offendingFlowMods); |
| 476 | return result; |
alshabib | 2683458 | 2014-10-08 20:15:46 -0700 | [diff] [blame] | 477 | } |
| 478 | throw new TimeoutException(); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | private void cleanUp() { |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 482 | if (isDone() || isCancelled()) { |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 483 | if (pendingXid != null) { |
| 484 | pendingFutures.remove(pendingXid); |
| 485 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 486 | for (Long xid : fms.keySet()) { |
| 487 | pendingFMs.remove(xid); |
| 488 | } |
| 489 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 490 | } |
| 491 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 492 | private void removeRequirement(Dpid dpid) { |
| 493 | countDownLatch.countDown(); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 494 | if (countDownLatch.getCount() == 0) { |
| 495 | invokeCallbacks(); |
| 496 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 497 | sws.remove(dpid); |
| 498 | cleanUp(); |
| 499 | } |
| 500 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 501 | @Override |
| 502 | public void addListener(Runnable runnable, Executor executor) { |
| 503 | executionList.add(runnable, executor); |
| 504 | } |
| 505 | |
| 506 | private void invokeCallbacks() { |
| 507 | executionList.execute(); |
| 508 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 509 | } |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 510 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 511 | } |