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