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