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