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