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