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