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