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 | 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) { |
Jonathan Hart | 1109640 | 2014-10-20 17:31:49 -0700 | [diff] [blame] | 163 | final Set<Dpid> sws = |
| 164 | Collections.newSetFromMap(new ConcurrentHashMap<Dpid, Boolean>()); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 165 | final Map<Long, FlowRuleBatchEntry> fmXids = new HashMap<Long, FlowRuleBatchEntry>(); |
| 166 | OFFlowMod mod = null; |
| 167 | for (FlowRuleBatchEntry fbe : batch.getOperations()) { |
| 168 | FlowRule flowRule = fbe.getTarget(); |
| 169 | OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri())); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 170 | if (sw == null) { |
alshabib | 3effd04 | 2014-10-17 12:00:31 -0700 | [diff] [blame] | 171 | /* |
| 172 | * if a switch we are supposed to install to is gone then |
| 173 | * cancel (ie. rollback) the work that has been done so far |
| 174 | * and return the associated future. |
| 175 | */ |
| 176 | InstallationFuture failed = new InstallationFuture(sws, fmXids); |
| 177 | failed.cancel(true); |
| 178 | return failed; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 179 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 180 | sws.add(new Dpid(sw.getId())); |
| 181 | FlowModBuilder builder = new FlowModBuilder(flowRule, sw.factory()); |
| 182 | switch (fbe.getOperator()) { |
| 183 | case ADD: |
| 184 | mod = builder.buildFlowAdd(); |
| 185 | break; |
| 186 | case REMOVE: |
| 187 | mod = builder.buildFlowDel(); |
| 188 | break; |
| 189 | case MODIFY: |
| 190 | mod = builder.buildFlowMod(); |
| 191 | break; |
| 192 | default: |
| 193 | log.error("Unsupported batch operation {}", fbe.getOperator()); |
| 194 | } |
| 195 | if (mod != null) { |
| 196 | sw.sendMsg(mod); |
| 197 | fmXids.put(mod.getXid(), fbe); |
| 198 | } else { |
| 199 | log.error("Conversion of flowrule {} failed.", flowRule); |
| 200 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 201 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 202 | } |
| 203 | InstallationFuture installation = new InstallationFuture(sws, fmXids); |
| 204 | for (Long xid : fmXids.keySet()) { |
| 205 | pendingFMs.put(xid, installation); |
| 206 | } |
| 207 | pendingFutures.put(U32.f(batch.hashCode()), installation); |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 208 | installation.verify(U32.f(batch.hashCode())); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 209 | return installation; |
| 210 | } |
| 211 | |
| 212 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 213 | private class InternalFlowProvider |
| 214 | implements OpenFlowSwitchListener, OpenFlowEventListener { |
| 215 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 216 | private final Map<Dpid, FlowStatsCollector> collectors = Maps.newHashMap(); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 217 | private final Multimap<DeviceId, FlowEntry> completeEntries = |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 218 | ArrayListMultimap.create(); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 219 | |
| 220 | @Override |
| 221 | public void switchAdded(Dpid dpid) { |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 222 | FlowStatsCollector fsc = new FlowStatsCollector(controller.getSwitch(dpid), POLL_INTERVAL); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 223 | fsc.start(); |
| 224 | collectors.put(dpid, fsc); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public void switchRemoved(Dpid dpid) { |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 229 | FlowStatsCollector collector = collectors.remove(dpid); |
| 230 | if (collector != null) { |
| 231 | collector.stop(); |
| 232 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | @Override |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame^] | 236 | public void switchChanged(Dpid dpid) { |
| 237 | } |
| 238 | |
| 239 | @Override |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 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 | } |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame^] | 320 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 321 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 322 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 323 | private class InstallationFuture implements Future<CompletedBatchOperation> { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 324 | |
| 325 | private final Set<Dpid> sws; |
| 326 | private final AtomicBoolean ok = new AtomicBoolean(true); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 327 | private final Map<Long, FlowRuleBatchEntry> fms; |
| 328 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 329 | private final List<FlowEntry> offendingFlowMods = Lists.newLinkedList(); |
| 330 | |
| 331 | private final CountDownLatch countDownLatch; |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 332 | private Long pendingXid; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 333 | private BatchState state; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 334 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 335 | public InstallationFuture(Set<Dpid> sws, Map<Long, FlowRuleBatchEntry> fmXids) { |
| 336 | this.state = BatchState.STARTED; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 337 | this.sws = sws; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 338 | this.fms = fmXids; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 339 | countDownLatch = new CountDownLatch(sws.size()); |
| 340 | } |
| 341 | |
| 342 | public void fail(OFErrorMsg msg, Dpid dpid) { |
| 343 | ok.set(false); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 344 | removeRequirement(dpid); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 345 | FlowEntry fe = null; |
| 346 | FlowRuleBatchEntry fbe = fms.get(msg.getXid()); |
| 347 | FlowRule offending = fbe.getTarget(); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 348 | //TODO handle specific error msgs |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 349 | switch (msg.getErrType()) { |
| 350 | case BAD_ACTION: |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 351 | OFBadActionErrorMsg bad = (OFBadActionErrorMsg) msg; |
| 352 | fe = new DefaultFlowEntry(offending, bad.getErrType().ordinal(), |
| 353 | bad.getCode().ordinal()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 354 | break; |
| 355 | case BAD_INSTRUCTION: |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 356 | OFBadInstructionErrorMsg badins = (OFBadInstructionErrorMsg) msg; |
| 357 | fe = new DefaultFlowEntry(offending, badins.getErrType().ordinal(), |
| 358 | badins.getCode().ordinal()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 359 | break; |
| 360 | case BAD_MATCH: |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 361 | OFBadMatchErrorMsg badMatch = (OFBadMatchErrorMsg) msg; |
| 362 | fe = new DefaultFlowEntry(offending, badMatch.getErrType().ordinal(), |
| 363 | badMatch.getCode().ordinal()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 364 | break; |
| 365 | case BAD_REQUEST: |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 366 | OFBadRequestErrorMsg badReq = (OFBadRequestErrorMsg) msg; |
| 367 | fe = new DefaultFlowEntry(offending, badReq.getErrType().ordinal(), |
| 368 | badReq.getCode().ordinal()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 369 | break; |
| 370 | case FLOW_MOD_FAILED: |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 371 | OFFlowModFailedErrorMsg fmFail = (OFFlowModFailedErrorMsg) msg; |
| 372 | fe = new DefaultFlowEntry(offending, fmFail.getErrType().ordinal(), |
| 373 | fmFail.getCode().ordinal()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 374 | break; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 375 | case EXPERIMENTER: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 376 | case GROUP_MOD_FAILED: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 377 | case HELLO_FAILED: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 378 | case METER_MOD_FAILED: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 379 | case PORT_MOD_FAILED: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 380 | case QUEUE_OP_FAILED: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 381 | case ROLE_REQUEST_FAILED: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 382 | case SWITCH_CONFIG_FAILED: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 383 | case TABLE_FEATURES_FAILED: |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 384 | case TABLE_MOD_FAILED: |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 385 | fe = new DefaultFlowEntry(offending, msg.getErrType().ordinal(), 0); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 386 | break; |
| 387 | default: |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 388 | log.error("Unknown error type {}", msg.getErrType()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 389 | |
| 390 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 391 | offendingFlowMods.add(fe); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 392 | |
| 393 | } |
| 394 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 395 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 396 | public void satisfyRequirement(Dpid dpid) { |
alshabib | 3effd04 | 2014-10-17 12:00:31 -0700 | [diff] [blame] | 397 | log.debug("Satisfaction from switch {}", dpid); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 398 | removeRequirement(dpid); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 399 | } |
| 400 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 401 | |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 402 | public void verify(Long id) { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 403 | pendingXid = id; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 404 | for (Dpid dpid : sws) { |
| 405 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 406 | OFBarrierRequest.Builder builder = sw.factory() |
| 407 | .buildBarrierRequest() |
| 408 | .setXid(id); |
| 409 | sw.sendMsg(builder.build()); |
| 410 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | @Override |
| 414 | public boolean cancel(boolean mayInterruptIfRunning) { |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 415 | ok.set(false); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 416 | this.state = BatchState.CANCELLED; |
| 417 | cleanUp(); |
| 418 | for (FlowRuleBatchEntry fbe : fms.values()) { |
| 419 | if (fbe.getOperator() == FlowRuleOperation.ADD || |
| 420 | fbe.getOperator() == FlowRuleOperation.MODIFY) { |
| 421 | removeFlowRule(fbe.getTarget()); |
| 422 | } else if (fbe.getOperator() == FlowRuleOperation.REMOVE) { |
| 423 | applyRule(fbe.getTarget()); |
| 424 | } |
| 425 | |
| 426 | } |
| 427 | return isCancelled(); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | @Override |
| 431 | public boolean isCancelled() { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 432 | return this.state == BatchState.CANCELLED; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | @Override |
| 436 | public boolean isDone() { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 437 | return this.state == BatchState.FINISHED; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | @Override |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 441 | public CompletedBatchOperation get() throws InterruptedException, ExecutionException { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 442 | countDownLatch.await(); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 443 | this.state = BatchState.FINISHED; |
| 444 | return new CompletedBatchOperation(ok.get(), offendingFlowMods); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | @Override |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 448 | public CompletedBatchOperation get(long timeout, TimeUnit unit) |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 449 | throws InterruptedException, ExecutionException, |
| 450 | TimeoutException { |
alshabib | 2683458 | 2014-10-08 20:15:46 -0700 | [diff] [blame] | 451 | if (countDownLatch.await(timeout, unit)) { |
| 452 | this.state = BatchState.FINISHED; |
| 453 | return new CompletedBatchOperation(ok.get(), offendingFlowMods); |
| 454 | } |
| 455 | throw new TimeoutException(); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | private void cleanUp() { |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 459 | if (isDone() || isCancelled()) { |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 460 | if (pendingXid != null) { |
| 461 | pendingFutures.remove(pendingXid); |
| 462 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 463 | for (Long xid : fms.keySet()) { |
| 464 | pendingFMs.remove(xid); |
| 465 | } |
| 466 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 467 | } |
| 468 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 469 | private void removeRequirement(Dpid dpid) { |
| 470 | countDownLatch.countDown(); |
| 471 | sws.remove(dpid); |
| 472 | cleanUp(); |
| 473 | } |
| 474 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 475 | } |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 476 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 477 | } |