Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.provider.of.flow.impl; |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 18 | import com.google.common.cache.Cache; |
| 19 | import com.google.common.cache.CacheBuilder; |
| 20 | import com.google.common.cache.RemovalCause; |
| 21 | import com.google.common.cache.RemovalNotification; |
| 22 | import com.google.common.collect.Maps; |
| 23 | import com.google.common.collect.Sets; |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Activate; |
| 25 | import org.apache.felix.scr.annotations.Component; |
| 26 | import org.apache.felix.scr.annotations.Deactivate; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Modified; |
| 28 | import org.apache.felix.scr.annotations.Property; |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 29 | import org.apache.felix.scr.annotations.Reference; |
| 30 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 31 | import org.onosproject.cfg.ComponentConfigService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.core.ApplicationId; |
| 33 | import org.onosproject.net.DeviceId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.flow.CompletedBatchOperation; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.net.flow.FlowEntry; |
| 36 | import org.onosproject.net.flow.FlowRule; |
| 37 | import org.onosproject.net.flow.FlowRuleBatchEntry; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 38 | import org.onosproject.net.flow.FlowRuleBatchOperation; |
Thomas Vachuska | a6c0d04 | 2015-04-23 10:17:37 -0700 | [diff] [blame] | 39 | import org.onosproject.net.flow.FlowRuleExtPayLoad; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import org.onosproject.net.flow.FlowRuleProvider; |
| 41 | import org.onosproject.net.flow.FlowRuleProviderRegistry; |
| 42 | import org.onosproject.net.flow.FlowRuleProviderService; |
| 43 | import org.onosproject.net.provider.AbstractProvider; |
| 44 | import org.onosproject.net.provider.ProviderId; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 45 | import org.onosproject.net.statistic.DefaultLoad; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 46 | import org.onosproject.openflow.controller.Dpid; |
| 47 | import org.onosproject.openflow.controller.OpenFlowController; |
| 48 | import org.onosproject.openflow.controller.OpenFlowEventListener; |
| 49 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 50 | import org.onosproject.openflow.controller.OpenFlowSwitchListener; |
| 51 | import org.onosproject.openflow.controller.RoleState; |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 52 | import org.onosproject.openflow.controller.ThirdPartyMessage; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 53 | import org.osgi.service.component.ComponentContext; |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 54 | import org.projectfloodlight.openflow.protocol.OFBadRequestCode; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 55 | import org.projectfloodlight.openflow.protocol.OFBarrierRequest; |
| 56 | import org.projectfloodlight.openflow.protocol.OFErrorMsg; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 57 | import org.projectfloodlight.openflow.protocol.OFErrorType; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 58 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 59 | import org.projectfloodlight.openflow.protocol.OFFlowRemoved; |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 60 | import org.projectfloodlight.openflow.protocol.OFFlowStatsReply; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 61 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 62 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 63 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
sangho | 89bf6fb | 2015-02-09 09:33:13 -0800 | [diff] [blame] | 64 | import org.projectfloodlight.openflow.protocol.OFStatsType; |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 65 | import org.projectfloodlight.openflow.protocol.errormsg.OFBadRequestErrorMsg; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 66 | import org.projectfloodlight.openflow.protocol.errormsg.OFFlowModFailedErrorMsg; |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 67 | import org.slf4j.Logger; |
| 68 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 69 | import java.util.Collections; |
| 70 | import java.util.Dictionary; |
| 71 | import java.util.List; |
| 72 | import java.util.Map; |
| 73 | import java.util.Optional; |
| 74 | import java.util.Set; |
| 75 | import java.util.Timer; |
| 76 | import java.util.concurrent.TimeUnit; |
| 77 | import java.util.stream.Collectors; |
| 78 | |
| 79 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 80 | import static org.onlab.util.Tools.get; |
| 81 | import static org.slf4j.LoggerFactory.getLogger; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 82 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 83 | /** |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 84 | * Provider which uses an OpenFlow controller to detect network end-station |
| 85 | * hosts. |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 86 | */ |
| 87 | @Component(immediate = true) |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 88 | public class OpenFlowRuleProvider extends AbstractProvider |
| 89 | implements FlowRuleProvider { |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 90 | |
| 91 | private final Logger log = getLogger(getClass()); |
| 92 | |
| 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 94 | protected FlowRuleProviderRegistry providerRegistry; |
| 95 | |
| 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 97 | protected OpenFlowController controller; |
| 98 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 100 | protected ComponentConfigService cfgService; |
| 101 | |
| 102 | private static final int DEFAULT_POLL_FREQUENCY = 10; |
| 103 | @Property(name = "flowPollFrequency", intValue = DEFAULT_POLL_FREQUENCY, |
| 104 | label = "Frequency (in seconds) for polling flow statistics") |
| 105 | private int flowPollFrequency = DEFAULT_POLL_FREQUENCY; |
| 106 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 107 | private FlowRuleProviderService providerService; |
| 108 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 109 | private final InternalFlowProvider listener = new InternalFlowProvider(); |
| 110 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 111 | private Cache<Long, InternalCacheEntry> pendingBatches; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 112 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 113 | private final Timer timer = new Timer("onos-openflow-collector"); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 114 | private final Map<Dpid, FlowStatsCollector> collectors = Maps.newHashMap(); |
| 115 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 116 | /** |
| 117 | * Creates an OpenFlow host provider. |
| 118 | */ |
| 119 | public OpenFlowRuleProvider() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 120 | super(new ProviderId("of", "org.onosproject.provider.openflow")); |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Activate |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 124 | public void activate(ComponentContext context) { |
| 125 | cfgService.registerProperties(getClass()); |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 126 | providerService = providerRegistry.register(this); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 127 | controller.addListener(listener); |
| 128 | controller.addEventListener(listener); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 129 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 130 | pendingBatches = createBatchCache(); |
| 131 | createCollectors(); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 132 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 133 | log.info("Started"); |
| 134 | } |
| 135 | |
| 136 | @Deactivate |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 137 | public void deactivate(ComponentContext context) { |
| 138 | cfgService.unregisterProperties(getClass(), false); |
| 139 | stopCollectors(); |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 140 | providerRegistry.unregister(this); |
| 141 | providerService = null; |
| 142 | |
| 143 | log.info("Stopped"); |
| 144 | } |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 145 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 146 | @Modified |
| 147 | public void modified(ComponentContext context) { |
| 148 | Dictionary<?, ?> properties = context.getProperties(); |
| 149 | int newFlowPollFrequency; |
| 150 | try { |
| 151 | String s = get(properties, "flowPollFrequency"); |
| 152 | newFlowPollFrequency = isNullOrEmpty(s) ? flowPollFrequency : Integer.parseInt(s.trim()); |
| 153 | |
| 154 | } catch (NumberFormatException | ClassCastException e) { |
| 155 | newFlowPollFrequency = flowPollFrequency; |
| 156 | } |
| 157 | |
| 158 | if (newFlowPollFrequency != flowPollFrequency) { |
| 159 | flowPollFrequency = newFlowPollFrequency; |
| 160 | adjustRate(); |
| 161 | } |
| 162 | |
| 163 | log.info("Settings: flowPollFrequency={}", flowPollFrequency); |
| 164 | } |
| 165 | |
| 166 | private Cache<Long, InternalCacheEntry> createBatchCache() { |
| 167 | return CacheBuilder.newBuilder() |
| 168 | .expireAfterWrite(10, TimeUnit.SECONDS) |
| 169 | .removalListener((RemovalNotification<Long, InternalCacheEntry> notification) -> { |
| 170 | if (notification.getCause() == RemovalCause.EXPIRED) { |
| 171 | providerService.batchOperationCompleted(notification.getKey(), |
| 172 | notification.getValue().failedCompletion()); |
| 173 | } |
| 174 | }).build(); |
| 175 | } |
| 176 | |
| 177 | private void createCollectors() { |
| 178 | controller.getSwitches().forEach(this::createCollector); |
| 179 | } |
| 180 | |
| 181 | private void createCollector(OpenFlowSwitch sw) { |
| 182 | FlowStatsCollector fsc = new FlowStatsCollector(timer, sw, flowPollFrequency); |
| 183 | fsc.start(); |
| 184 | collectors.put(new Dpid(sw.getId()), fsc); |
| 185 | } |
| 186 | |
| 187 | private void stopCollectors() { |
| 188 | collectors.values().forEach(FlowStatsCollector::stop); |
| 189 | collectors.clear(); |
| 190 | } |
| 191 | |
| 192 | private void adjustRate() { |
| 193 | DefaultLoad.setPollInterval(flowPollFrequency); |
| 194 | collectors.values().forEach(fsc -> fsc.adjustPollInterval(flowPollFrequency)); |
| 195 | } |
| 196 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 197 | @Override |
| 198 | public void applyFlowRule(FlowRule... flowRules) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 199 | for (FlowRule flowRule : flowRules) { |
| 200 | applyRule(flowRule); |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 201 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 202 | } |
| 203 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 204 | private void applyRule(FlowRule flowRule) { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 205 | OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId() |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 206 | .uri())); |
Thomas Vachuska | a6c0d04 | 2015-04-23 10:17:37 -0700 | [diff] [blame] | 207 | FlowRuleExtPayLoad flowRuleExtPayLoad = flowRule.payLoad(); |
| 208 | if (hasPayload(flowRuleExtPayLoad)) { |
| 209 | OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad()); |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 210 | sw.sendMsg(msg); |
| 211 | return; |
| 212 | } |
alshabib | bdcbb10 | 2015-04-22 14:16:38 -0700 | [diff] [blame] | 213 | sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(), |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 214 | Optional.empty()).buildFlowAdd()); |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 215 | } |
| 216 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 217 | @Override |
| 218 | public void removeFlowRule(FlowRule... flowRules) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 219 | for (FlowRule flowRule : flowRules) { |
| 220 | removeRule(flowRule); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 221 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 222 | } |
| 223 | |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 224 | private void removeRule(FlowRule flowRule) { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 225 | OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId() |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 226 | .uri())); |
Thomas Vachuska | a6c0d04 | 2015-04-23 10:17:37 -0700 | [diff] [blame] | 227 | FlowRuleExtPayLoad flowRuleExtPayLoad = flowRule.payLoad(); |
| 228 | if (hasPayload(flowRuleExtPayLoad)) { |
| 229 | OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad()); |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 230 | sw.sendMsg(msg); |
| 231 | return; |
| 232 | } |
alshabib | bdcbb10 | 2015-04-22 14:16:38 -0700 | [diff] [blame] | 233 | sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(), |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 234 | Optional.empty()).buildFlowDel()); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 235 | } |
| 236 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 237 | @Override |
| 238 | public void removeRulesById(ApplicationId id, FlowRule... flowRules) { |
| 239 | // TODO: optimize using the ApplicationId |
| 240 | removeFlowRule(flowRules); |
| 241 | } |
| 242 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 243 | @Override |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 244 | public void executeBatch(FlowRuleBatchOperation batch) { |
| 245 | |
| 246 | pendingBatches.put(batch.id(), new InternalCacheEntry(batch)); |
| 247 | |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 248 | OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(batch.deviceId() |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 249 | .uri())); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 250 | OFFlowMod mod; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 251 | for (FlowRuleBatchEntry fbe : batch.getOperations()) { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 252 | // flow is the third party privacy flow |
Thomas Vachuska | a6c0d04 | 2015-04-23 10:17:37 -0700 | [diff] [blame] | 253 | |
| 254 | FlowRuleExtPayLoad flowRuleExtPayLoad = fbe.target().payLoad(); |
| 255 | if (hasPayload(flowRuleExtPayLoad)) { |
| 256 | OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad()); |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 257 | sw.sendMsg(msg); |
| 258 | continue; |
| 259 | } |
| 260 | FlowModBuilder builder = FlowModBuilder.builder(fbe.target(), sw |
| 261 | .factory(), Optional.of(batch.id())); |
Sho SHIMIZU | aba9d00 | 2015-01-29 14:51:04 -0800 | [diff] [blame] | 262 | switch (fbe.operator()) { |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 263 | case ADD: |
| 264 | mod = builder.buildFlowAdd(); |
| 265 | break; |
| 266 | case REMOVE: |
| 267 | mod = builder.buildFlowDel(); |
| 268 | break; |
| 269 | case MODIFY: |
| 270 | mod = builder.buildFlowMod(); |
| 271 | break; |
| 272 | default: |
| 273 | log.error("Unsupported batch operation {}; skipping flowmod {}", |
| 274 | fbe.operator(), fbe); |
| 275 | continue; |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 276 | } |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 277 | sw.sendMsg(mod); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 278 | } |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 279 | OFBarrierRequest.Builder builder = sw.factory().buildBarrierRequest() |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 280 | .setXid(batch.id()); |
| 281 | sw.sendMsg(builder.build()); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Thomas Vachuska | a6c0d04 | 2015-04-23 10:17:37 -0700 | [diff] [blame] | 284 | private boolean hasPayload(FlowRuleExtPayLoad flowRuleExtPayLoad) { |
| 285 | return flowRuleExtPayLoad != null && |
| 286 | flowRuleExtPayLoad.payLoad() != null && |
| 287 | flowRuleExtPayLoad.payLoad().length > 0; |
| 288 | } |
| 289 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 290 | private class InternalFlowProvider |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 291 | implements OpenFlowSwitchListener, OpenFlowEventListener { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 292 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 293 | @Override |
| 294 | public void switchAdded(Dpid dpid) { |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 295 | createCollector(controller.getSwitch(dpid)); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public void switchRemoved(Dpid dpid) { |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 300 | FlowStatsCollector collector = collectors.remove(dpid); |
| 301 | if (collector != null) { |
| 302 | collector.stop(); |
| 303 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | @Override |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 307 | public void switchChanged(Dpid dpid) { |
| 308 | } |
| 309 | |
| 310 | @Override |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 311 | public void portChanged(Dpid dpid, OFPortStatus status) { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 312 | // TODO: Decide whether to evict flows internal store. |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | @Override |
| 316 | public void handleMessage(Dpid dpid, OFMessage msg) { |
alshabib | da1644e | 2015-03-13 14:01:35 -0700 | [diff] [blame] | 317 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 318 | switch (msg.getType()) { |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 319 | case FLOW_REMOVED: |
| 320 | OFFlowRemoved removed = (OFFlowRemoved) msg; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 321 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 322 | FlowEntry fr = new FlowEntryBuilder(dpid, removed).build(); |
| 323 | providerService.flowRemoved(fr); |
| 324 | break; |
| 325 | case STATS_REPLY: |
| 326 | if (((OFStatsReply) msg).getStatsType() == OFStatsType.FLOW) { |
| 327 | pushFlowMetrics(dpid, (OFFlowStatsReply) msg); |
sangho | 89bf6fb | 2015-02-09 09:33:13 -0800 | [diff] [blame] | 328 | } |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 329 | break; |
| 330 | case BARRIER_REPLY: |
| 331 | try { |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 332 | InternalCacheEntry entry = pendingBatches.getIfPresent(msg.getXid()); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 333 | if (entry != null) { |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 334 | providerService |
| 335 | .batchOperationCompleted(msg.getXid(), |
| 336 | entry.completed()); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 337 | } else { |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 338 | log.warn("Received unknown Barrier Reply: {}", |
| 339 | msg.getXid()); |
| 340 | } |
| 341 | } finally { |
| 342 | pendingBatches.invalidate(msg.getXid()); |
| 343 | } |
| 344 | break; |
| 345 | case ERROR: |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 346 | // TODO: This needs to get suppressed in a better way. |
| 347 | if (msg instanceof OFBadRequestErrorMsg && |
| 348 | ((OFBadRequestErrorMsg) msg).getCode() == OFBadRequestCode.BAD_TYPE) { |
| 349 | log.debug("Received error message {} from {}", msg, dpid); |
| 350 | } else { |
| 351 | log.warn("Received error message {} from {}", msg, dpid); |
| 352 | } |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 353 | |
| 354 | OFErrorMsg error = (OFErrorMsg) msg; |
| 355 | if (error.getErrType() == OFErrorType.FLOW_MOD_FAILED) { |
| 356 | OFFlowModFailedErrorMsg fmFailed = (OFFlowModFailedErrorMsg) error; |
| 357 | if (fmFailed.getData().getParsedMessage().isPresent()) { |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 358 | OFMessage m = fmFailed.getData().getParsedMessage().get(); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 359 | OFFlowMod fm = (OFFlowMod) m; |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 360 | InternalCacheEntry entry = |
| 361 | pendingBatches.getIfPresent(msg.getXid()); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 362 | if (entry != null) { |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 363 | entry.appendFailure(new FlowEntryBuilder(dpid, fm).build()); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 364 | } else { |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 365 | log.error("No matching batch for this error: {}", error); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 366 | } |
| 367 | } else { |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 368 | // FIXME: Potentially add flowtracking to avoid this message. |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 369 | log.error("Flow installation failed but switch didn't" |
| 370 | + " tell us which one."); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 371 | } |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 372 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 373 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 374 | default: |
| 375 | log.debug("Unhandled message type: {}", msg.getType()); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | } |
| 379 | |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 380 | @Override |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 381 | public void receivedRoleReply(Dpid dpid, RoleState requested, |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 382 | RoleState response) { |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 383 | // Do nothing here for now. |
| 384 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 385 | |
sangho | 89bf6fb | 2015-02-09 09:33:13 -0800 | [diff] [blame] | 386 | private void pushFlowMetrics(Dpid dpid, OFFlowStatsReply replies) { |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 387 | |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 388 | DeviceId did = DeviceId.deviceId(Dpid.uri(dpid)); |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 389 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 390 | |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 391 | List<FlowEntry> flowEntries = replies.getEntries().stream() |
alshabib | bdcbb10 | 2015-04-22 14:16:38 -0700 | [diff] [blame] | 392 | .map(entry -> new FlowEntryBuilder(dpid, entry).build()) |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 393 | .collect(Collectors.toList()); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 394 | |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 395 | providerService.pushFlowMetrics(did, flowEntries); |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 396 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 397 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 398 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 399 | /** |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 400 | * The internal cache entry holding the original request as well as |
| 401 | * accumulating the any failures along the way. |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 402 | * <p/> |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 403 | * If this entry is evicted from the cache then the entire operation is |
| 404 | * considered failed. Otherwise, only the failures reported by the device |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 405 | * will be propagated up. |
| 406 | */ |
| 407 | private class InternalCacheEntry { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 408 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 409 | private final FlowRuleBatchOperation operation; |
| 410 | private final Set<FlowRule> failures = Sets.newConcurrentHashSet(); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 411 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 412 | public InternalCacheEntry(FlowRuleBatchOperation operation) { |
| 413 | this.operation = operation; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 416 | /** |
| 417 | * Appends a failed rule to the set of failed items. |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 418 | * |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 419 | * @param rule the failed rule |
| 420 | */ |
| 421 | public void appendFailure(FlowRule rule) { |
| 422 | failures.add(rule); |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 425 | /** |
| 426 | * Fails the entire batch and returns the failed operation. |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 427 | * |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 428 | * @return the failed operation |
| 429 | */ |
| 430 | public CompletedBatchOperation failedCompletion() { |
| 431 | Set<FlowRule> fails = operation.getOperations().stream() |
| 432 | .map(op -> op.target()).collect(Collectors.toSet()); |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 433 | return new CompletedBatchOperation(false, |
| 434 | Collections |
| 435 | .unmodifiableSet(fails), |
| 436 | operation.deviceId()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 439 | /** |
| 440 | * Returns the completed operation and whether the batch suceeded. |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 441 | * |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 442 | * @return the completed operation |
| 443 | */ |
| 444 | public CompletedBatchOperation completed() { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 445 | return new CompletedBatchOperation( |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 446 | failures.isEmpty(), |
| 447 | Collections |
| 448 | .unmodifiableSet(failures), |
| 449 | operation.deviceId()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 450 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 451 | } |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 452 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 453 | } |