Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present 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; |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 34 | import org.onosproject.net.driver.DriverService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.net.flow.CompletedBatchOperation; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 36 | import org.onosproject.net.flow.DefaultTableStatisticsEntry; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 37 | import org.onosproject.net.flow.FlowEntry; |
| 38 | import org.onosproject.net.flow.FlowRule; |
| 39 | import org.onosproject.net.flow.FlowRuleBatchEntry; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 40 | import org.onosproject.net.flow.FlowRuleBatchOperation; |
Thomas Vachuska | a6c0d04 | 2015-04-23 10:17:37 -0700 | [diff] [blame] | 41 | import org.onosproject.net.flow.FlowRuleExtPayLoad; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 42 | import org.onosproject.net.flow.FlowRuleProvider; |
| 43 | import org.onosproject.net.flow.FlowRuleProviderRegistry; |
| 44 | import org.onosproject.net.flow.FlowRuleProviderService; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 45 | import org.onosproject.net.flow.TableStatisticsEntry; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 46 | import org.onosproject.net.provider.AbstractProvider; |
| 47 | import org.onosproject.net.provider.ProviderId; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 48 | import org.onosproject.net.statistic.DefaultLoad; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 49 | import org.onosproject.openflow.controller.Dpid; |
| 50 | import org.onosproject.openflow.controller.OpenFlowController; |
| 51 | import org.onosproject.openflow.controller.OpenFlowEventListener; |
| 52 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 53 | import org.onosproject.openflow.controller.OpenFlowSwitchListener; |
| 54 | import org.onosproject.openflow.controller.RoleState; |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 55 | import org.onosproject.openflow.controller.ThirdPartyMessage; |
Thomas Vachuska | 95caba3 | 2016-04-04 10:42:05 -0700 | [diff] [blame] | 56 | import org.onosproject.provider.of.flow.util.FlowEntryBuilder; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 57 | import org.osgi.service.component.ComponentContext; |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 58 | import org.projectfloodlight.openflow.protocol.OFBadRequestCode; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 59 | import org.projectfloodlight.openflow.protocol.OFBarrierRequest; |
| 60 | import org.projectfloodlight.openflow.protocol.OFErrorMsg; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 61 | import org.projectfloodlight.openflow.protocol.OFErrorType; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 62 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 63 | import org.projectfloodlight.openflow.protocol.OFFlowRemoved; |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 64 | import org.projectfloodlight.openflow.protocol.OFFlowStatsReply; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 65 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 66 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 67 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
sangho | 89bf6fb | 2015-02-09 09:33:13 -0800 | [diff] [blame] | 68 | import org.projectfloodlight.openflow.protocol.OFStatsType; |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 69 | import org.projectfloodlight.openflow.protocol.OFTableStatsEntry; |
| 70 | import org.projectfloodlight.openflow.protocol.OFTableStatsReply; |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 71 | import org.projectfloodlight.openflow.protocol.errormsg.OFBadRequestErrorMsg; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 72 | import org.projectfloodlight.openflow.protocol.errormsg.OFFlowModFailedErrorMsg; |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 73 | import org.slf4j.Logger; |
| 74 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 75 | import java.util.Collections; |
| 76 | import java.util.Dictionary; |
| 77 | import java.util.List; |
| 78 | import java.util.Map; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 79 | import java.util.Objects; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 80 | import java.util.Optional; |
| 81 | import java.util.Set; |
| 82 | import java.util.Timer; |
| 83 | import java.util.concurrent.TimeUnit; |
| 84 | import java.util.stream.Collectors; |
| 85 | |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 86 | import static com.google.common.base.Preconditions.checkNotNull; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 87 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 88 | import static org.onlab.util.Tools.get; |
| 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 | /** |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 92 | * Provider which uses an OpenFlow controller to detect network end-station |
| 93 | * hosts. |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 94 | */ |
| 95 | @Component(immediate = true) |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 96 | public class OpenFlowRuleProvider extends AbstractProvider |
| 97 | implements FlowRuleProvider { |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 98 | |
| 99 | private final Logger log = getLogger(getClass()); |
| 100 | |
| 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 102 | protected FlowRuleProviderRegistry providerRegistry; |
| 103 | |
| 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 105 | protected OpenFlowController controller; |
| 106 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 107 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 108 | protected ComponentConfigService cfgService; |
| 109 | |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 110 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 111 | protected DriverService driverService; |
| 112 | |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 113 | private static final int DEFAULT_POLL_FREQUENCY = 5; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 114 | @Property(name = "flowPollFrequency", intValue = DEFAULT_POLL_FREQUENCY, |
| 115 | label = "Frequency (in seconds) for polling flow statistics") |
| 116 | private int flowPollFrequency = DEFAULT_POLL_FREQUENCY; |
| 117 | |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 118 | private static final boolean DEFAULT_ADAPTIVE_FLOW_SAMPLING = true; |
| 119 | @Property(name = "adaptiveFlowSampling", boolValue = DEFAULT_ADAPTIVE_FLOW_SAMPLING, |
| 120 | label = "Adaptive Flow Sampling is on or off") |
| 121 | private boolean adaptiveFlowSampling = DEFAULT_ADAPTIVE_FLOW_SAMPLING; |
| 122 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 123 | private FlowRuleProviderService providerService; |
| 124 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 125 | private final InternalFlowProvider listener = new InternalFlowProvider(); |
| 126 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 127 | private Cache<Long, InternalCacheEntry> pendingBatches; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 128 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 129 | private final Timer timer = new Timer("onos-openflow-collector"); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 130 | private final Map<Dpid, FlowStatsCollector> simpleCollectors = Maps.newHashMap(); |
| 131 | |
| 132 | // NewAdaptiveFlowStatsCollector Set |
| 133 | private final Map<Dpid, NewAdaptiveFlowStatsCollector> afsCollectors = Maps.newHashMap(); |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 134 | private final Map<Dpid, FlowStatsCollector> collectors = Maps.newHashMap(); |
| 135 | private final Map<Dpid, TableStatisticsCollector> tableStatsCollectors = Maps.newHashMap(); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 136 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 137 | /** |
| 138 | * Creates an OpenFlow host provider. |
| 139 | */ |
| 140 | public OpenFlowRuleProvider() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 141 | super(new ProviderId("of", "org.onosproject.provider.openflow")); |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Activate |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 145 | public void activate(ComponentContext context) { |
| 146 | cfgService.registerProperties(getClass()); |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 147 | providerService = providerRegistry.register(this); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 148 | controller.addListener(listener); |
| 149 | controller.addEventListener(listener); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 150 | |
Antonio Marsico | 1c5ae1f | 2015-12-15 15:31:56 +0100 | [diff] [blame] | 151 | modified(context); |
| 152 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 153 | pendingBatches = createBatchCache(); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 154 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 155 | createCollectors(); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 156 | |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 157 | log.info("Started with flowPollFrequency = {}, adaptiveFlowSampling = {}", |
| 158 | flowPollFrequency, adaptiveFlowSampling); |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | @Deactivate |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 162 | public void deactivate(ComponentContext context) { |
| 163 | cfgService.unregisterProperties(getClass(), false); |
| 164 | stopCollectors(); |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 165 | providerRegistry.unregister(this); |
| 166 | providerService = null; |
| 167 | |
| 168 | log.info("Stopped"); |
| 169 | } |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 170 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 171 | @Modified |
| 172 | public void modified(ComponentContext context) { |
| 173 | Dictionary<?, ?> properties = context.getProperties(); |
| 174 | int newFlowPollFrequency; |
| 175 | try { |
| 176 | String s = get(properties, "flowPollFrequency"); |
| 177 | newFlowPollFrequency = isNullOrEmpty(s) ? flowPollFrequency : Integer.parseInt(s.trim()); |
| 178 | |
| 179 | } catch (NumberFormatException | ClassCastException e) { |
| 180 | newFlowPollFrequency = flowPollFrequency; |
| 181 | } |
| 182 | |
| 183 | if (newFlowPollFrequency != flowPollFrequency) { |
| 184 | flowPollFrequency = newFlowPollFrequency; |
| 185 | adjustRate(); |
| 186 | } |
| 187 | |
| 188 | log.info("Settings: flowPollFrequency={}", flowPollFrequency); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 189 | |
| 190 | boolean newAdaptiveFlowSampling; |
| 191 | String s = get(properties, "adaptiveFlowSampling"); |
| 192 | newAdaptiveFlowSampling = isNullOrEmpty(s) ? adaptiveFlowSampling : Boolean.parseBoolean(s.trim()); |
| 193 | |
| 194 | if (newAdaptiveFlowSampling != adaptiveFlowSampling) { |
| 195 | // stop previous collector |
| 196 | stopCollectors(); |
| 197 | adaptiveFlowSampling = newAdaptiveFlowSampling; |
| 198 | // create new collectors |
| 199 | createCollectors(); |
| 200 | } |
| 201 | |
| 202 | log.info("Settings: adaptiveFlowSampling={}", adaptiveFlowSampling); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | private Cache<Long, InternalCacheEntry> createBatchCache() { |
| 206 | return CacheBuilder.newBuilder() |
| 207 | .expireAfterWrite(10, TimeUnit.SECONDS) |
| 208 | .removalListener((RemovalNotification<Long, InternalCacheEntry> notification) -> { |
| 209 | if (notification.getCause() == RemovalCause.EXPIRED) { |
| 210 | providerService.batchOperationCompleted(notification.getKey(), |
| 211 | notification.getValue().failedCompletion()); |
| 212 | } |
| 213 | }).build(); |
| 214 | } |
| 215 | |
| 216 | private void createCollectors() { |
| 217 | controller.getSwitches().forEach(this::createCollector); |
| 218 | } |
| 219 | |
| 220 | private void createCollector(OpenFlowSwitch sw) { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 221 | if (adaptiveFlowSampling) { |
| 222 | // NewAdaptiveFlowStatsCollector Constructor |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 223 | NewAdaptiveFlowStatsCollector fsc = |
| 224 | new NewAdaptiveFlowStatsCollector(driverService, sw, flowPollFrequency); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 225 | fsc.start(); |
| 226 | afsCollectors.put(new Dpid(sw.getId()), fsc); |
| 227 | } else { |
| 228 | FlowStatsCollector fsc = new FlowStatsCollector(timer, sw, flowPollFrequency); |
| 229 | fsc.start(); |
| 230 | simpleCollectors.put(new Dpid(sw.getId()), fsc); |
| 231 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 232 | TableStatisticsCollector tsc = new TableStatisticsCollector(timer, sw, flowPollFrequency); |
| 233 | tsc.start(); |
| 234 | tableStatsCollectors.put(new Dpid(sw.getId()), tsc); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | private void stopCollectors() { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 238 | if (adaptiveFlowSampling) { |
| 239 | // NewAdaptiveFlowStatsCollector Destructor |
| 240 | afsCollectors.values().forEach(NewAdaptiveFlowStatsCollector::stop); |
| 241 | afsCollectors.clear(); |
| 242 | } else { |
| 243 | simpleCollectors.values().forEach(FlowStatsCollector::stop); |
| 244 | simpleCollectors.clear(); |
| 245 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 246 | tableStatsCollectors.values().forEach(TableStatisticsCollector::stop); |
| 247 | tableStatsCollectors.clear(); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | private void adjustRate() { |
| 251 | DefaultLoad.setPollInterval(flowPollFrequency); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 252 | if (adaptiveFlowSampling) { |
| 253 | // NewAdaptiveFlowStatsCollector calAndPollInterval |
| 254 | afsCollectors.values().forEach(fsc -> fsc.adjustCalAndPollInterval(flowPollFrequency)); |
| 255 | } else { |
| 256 | simpleCollectors.values().forEach(fsc -> fsc.adjustPollInterval(flowPollFrequency)); |
| 257 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 258 | tableStatsCollectors.values().forEach(tsc -> tsc.adjustPollInterval(flowPollFrequency)); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 259 | } |
| 260 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 261 | @Override |
| 262 | public void applyFlowRule(FlowRule... flowRules) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 263 | for (FlowRule flowRule : flowRules) { |
| 264 | applyRule(flowRule); |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 265 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 266 | } |
| 267 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 268 | private void applyRule(FlowRule flowRule) { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 269 | Dpid dpid = Dpid.dpid(flowRule.deviceId().uri()); |
| 270 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 271 | |
Ray Milkey | 0ae473d | 2016-04-04 10:56:47 -0700 | [diff] [blame] | 272 | if (sw == null) { |
| 273 | return; |
| 274 | } |
| 275 | |
Thomas Vachuska | a6c0d04 | 2015-04-23 10:17:37 -0700 | [diff] [blame] | 276 | FlowRuleExtPayLoad flowRuleExtPayLoad = flowRule.payLoad(); |
| 277 | if (hasPayload(flowRuleExtPayLoad)) { |
| 278 | OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad()); |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 279 | sw.sendMsg(msg); |
| 280 | return; |
| 281 | } |
alshabib | bdcbb10 | 2015-04-22 14:16:38 -0700 | [diff] [blame] | 282 | sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(), |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 283 | Optional.empty(), Optional.of(driverService)).buildFlowAdd()); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 284 | |
| 285 | if (adaptiveFlowSampling) { |
| 286 | // Add TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 287 | NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid); |
| 288 | if (collector != null) { |
| 289 | collector.addWithFlowRule(flowRule); |
| 290 | } |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 291 | } |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 292 | } |
| 293 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 294 | @Override |
| 295 | public void removeFlowRule(FlowRule... flowRules) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 296 | for (FlowRule flowRule : flowRules) { |
| 297 | removeRule(flowRule); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 298 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 299 | } |
| 300 | |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 301 | private void removeRule(FlowRule flowRule) { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 302 | Dpid dpid = Dpid.dpid(flowRule.deviceId().uri()); |
| 303 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 304 | |
Ray Milkey | 0ae473d | 2016-04-04 10:56:47 -0700 | [diff] [blame] | 305 | if (sw == null) { |
| 306 | return; |
| 307 | } |
| 308 | |
Thomas Vachuska | a6c0d04 | 2015-04-23 10:17:37 -0700 | [diff] [blame] | 309 | FlowRuleExtPayLoad flowRuleExtPayLoad = flowRule.payLoad(); |
| 310 | if (hasPayload(flowRuleExtPayLoad)) { |
| 311 | OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad()); |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 312 | sw.sendMsg(msg); |
| 313 | return; |
| 314 | } |
alshabib | bdcbb10 | 2015-04-22 14:16:38 -0700 | [diff] [blame] | 315 | sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(), |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 316 | Optional.empty(), Optional.of(driverService)).buildFlowDel()); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 317 | |
| 318 | if (adaptiveFlowSampling) { |
| 319 | // Remove TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 320 | NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid); |
| 321 | if (collector != null) { |
| 322 | collector.removeFlows(flowRule); |
| 323 | } |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 324 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 325 | } |
| 326 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 327 | @Override |
| 328 | public void removeRulesById(ApplicationId id, FlowRule... flowRules) { |
| 329 | // TODO: optimize using the ApplicationId |
| 330 | removeFlowRule(flowRules); |
| 331 | } |
| 332 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 333 | @Override |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 334 | public void executeBatch(FlowRuleBatchOperation batch) { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 335 | checkNotNull(batch); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 336 | |
| 337 | pendingBatches.put(batch.id(), new InternalCacheEntry(batch)); |
| 338 | |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 339 | Dpid dpid = Dpid.dpid(batch.deviceId().uri()); |
| 340 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 341 | OFFlowMod mod; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 342 | for (FlowRuleBatchEntry fbe : batch.getOperations()) { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 343 | // flow is the third party privacy flow |
Thomas Vachuska | a6c0d04 | 2015-04-23 10:17:37 -0700 | [diff] [blame] | 344 | |
| 345 | FlowRuleExtPayLoad flowRuleExtPayLoad = fbe.target().payLoad(); |
| 346 | if (hasPayload(flowRuleExtPayLoad)) { |
| 347 | OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad()); |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 348 | sw.sendMsg(msg); |
| 349 | continue; |
| 350 | } |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 351 | FlowModBuilder builder = |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 352 | FlowModBuilder.builder(fbe.target(), sw.factory(), |
| 353 | Optional.of(batch.id()), Optional.of(driverService)); |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 354 | NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid); |
Sho SHIMIZU | aba9d00 | 2015-01-29 14:51:04 -0800 | [diff] [blame] | 355 | switch (fbe.operator()) { |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 356 | case ADD: |
| 357 | mod = builder.buildFlowAdd(); |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 358 | if (adaptiveFlowSampling && collector != null) { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 359 | // Add TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 360 | collector.addWithFlowRule(fbe.target()); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 361 | } |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 362 | break; |
| 363 | case REMOVE: |
| 364 | mod = builder.buildFlowDel(); |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 365 | if (adaptiveFlowSampling && collector != null) { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 366 | // Remove TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 367 | collector.removeFlows(fbe.target()); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 368 | } |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 369 | break; |
| 370 | case MODIFY: |
| 371 | mod = builder.buildFlowMod(); |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 372 | if (adaptiveFlowSampling && collector != null) { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 373 | // Add or Update TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector |
| 374 | // afsCollectors.get(dpid).addWithFlowRule(fbe.target()); //check if add is good or not |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 375 | collector.addOrUpdateFlows((FlowEntry) fbe.target()); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 376 | } |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 377 | break; |
| 378 | default: |
| 379 | log.error("Unsupported batch operation {}; skipping flowmod {}", |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 380 | fbe.operator(), fbe); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 381 | continue; |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 382 | } |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 383 | sw.sendMsg(mod); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 384 | } |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 385 | OFBarrierRequest.Builder builder = sw.factory().buildBarrierRequest() |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 386 | .setXid(batch.id()); |
| 387 | sw.sendMsg(builder.build()); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Thomas Vachuska | a6c0d04 | 2015-04-23 10:17:37 -0700 | [diff] [blame] | 390 | private boolean hasPayload(FlowRuleExtPayLoad flowRuleExtPayLoad) { |
| 391 | return flowRuleExtPayLoad != null && |
| 392 | flowRuleExtPayLoad.payLoad() != null && |
| 393 | flowRuleExtPayLoad.payLoad().length > 0; |
| 394 | } |
| 395 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 396 | private class InternalFlowProvider |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 397 | implements OpenFlowSwitchListener, OpenFlowEventListener { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 398 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 399 | @Override |
| 400 | public void switchAdded(Dpid dpid) { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 401 | |
| 402 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 403 | |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 404 | createCollector(controller.getSwitch(dpid)); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | @Override |
| 408 | public void switchRemoved(Dpid dpid) { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 409 | if (adaptiveFlowSampling) { |
| 410 | NewAdaptiveFlowStatsCollector collector = afsCollectors.remove(dpid); |
| 411 | if (collector != null) { |
| 412 | collector.stop(); |
| 413 | } |
| 414 | } else { |
| 415 | FlowStatsCollector collector = simpleCollectors.remove(dpid); |
| 416 | if (collector != null) { |
| 417 | collector.stop(); |
| 418 | } |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 419 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 420 | TableStatisticsCollector tsc = tableStatsCollectors.remove(dpid); |
| 421 | if (tsc != null) { |
| 422 | tsc.stop(); |
| 423 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | @Override |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 427 | public void switchChanged(Dpid dpid) { |
| 428 | } |
| 429 | |
| 430 | @Override |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 431 | public void portChanged(Dpid dpid, OFPortStatus status) { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 432 | // TODO: Decide whether to evict flows internal store. |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | @Override |
| 436 | public void handleMessage(Dpid dpid, OFMessage msg) { |
Ray Milkey | ada9e2d | 2016-04-05 16:42:35 -0700 | [diff] [blame] | 437 | if (providerService == null) { |
| 438 | // We are shutting down, nothing to be done |
| 439 | return; |
| 440 | } |
Jonathan Hart | e4e74f0 | 2016-03-03 12:57:40 -0800 | [diff] [blame] | 441 | DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid)); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 442 | switch (msg.getType()) { |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 443 | case FLOW_REMOVED: |
| 444 | OFFlowRemoved removed = (OFFlowRemoved) msg; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 445 | |
Jonathan Hart | e4e74f0 | 2016-03-03 12:57:40 -0800 | [diff] [blame] | 446 | FlowEntry fr = new FlowEntryBuilder(deviceId, removed, driverService).build(); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 447 | providerService.flowRemoved(fr); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 448 | |
| 449 | if (adaptiveFlowSampling) { |
| 450 | // Removed TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 451 | NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid); |
| 452 | if (collector != null) { |
| 453 | collector.flowRemoved(fr); |
| 454 | } |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 455 | } |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 456 | break; |
| 457 | case STATS_REPLY: |
| 458 | if (((OFStatsReply) msg).getStatsType() == OFStatsType.FLOW) { |
| 459 | pushFlowMetrics(dpid, (OFFlowStatsReply) msg); |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 460 | } else if (((OFStatsReply) msg).getStatsType() == OFStatsType.TABLE) { |
| 461 | pushTableStatistics(dpid, (OFTableStatsReply) msg); |
sangho | 89bf6fb | 2015-02-09 09:33:13 -0800 | [diff] [blame] | 462 | } |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 463 | break; |
| 464 | case BARRIER_REPLY: |
| 465 | try { |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 466 | InternalCacheEntry entry = pendingBatches.getIfPresent(msg.getXid()); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 467 | if (entry != null) { |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 468 | providerService |
| 469 | .batchOperationCompleted(msg.getXid(), |
| 470 | entry.completed()); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 471 | } else { |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 472 | log.warn("Received unknown Barrier Reply: {}", |
| 473 | msg.getXid()); |
| 474 | } |
| 475 | } finally { |
| 476 | pendingBatches.invalidate(msg.getXid()); |
| 477 | } |
| 478 | break; |
| 479 | case ERROR: |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 480 | // TODO: This needs to get suppressed in a better way. |
| 481 | if (msg instanceof OFBadRequestErrorMsg && |
| 482 | ((OFBadRequestErrorMsg) msg).getCode() == OFBadRequestCode.BAD_TYPE) { |
| 483 | log.debug("Received error message {} from {}", msg, dpid); |
| 484 | } else { |
| 485 | log.warn("Received error message {} from {}", msg, dpid); |
| 486 | } |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 487 | |
| 488 | OFErrorMsg error = (OFErrorMsg) msg; |
| 489 | if (error.getErrType() == OFErrorType.FLOW_MOD_FAILED) { |
| 490 | OFFlowModFailedErrorMsg fmFailed = (OFFlowModFailedErrorMsg) error; |
| 491 | if (fmFailed.getData().getParsedMessage().isPresent()) { |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 492 | OFMessage m = fmFailed.getData().getParsedMessage().get(); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 493 | OFFlowMod fm = (OFFlowMod) m; |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 494 | InternalCacheEntry entry = |
| 495 | pendingBatches.getIfPresent(msg.getXid()); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 496 | if (entry != null) { |
Jonathan Hart | e4e74f0 | 2016-03-03 12:57:40 -0800 | [diff] [blame] | 497 | entry.appendFailure(new FlowEntryBuilder(deviceId, fm, driverService).build()); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 498 | } else { |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 499 | log.error("No matching batch for this error: {}", error); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 500 | } |
| 501 | } else { |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 502 | // FIXME: Potentially add flowtracking to avoid this message. |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 503 | log.error("Flow installation failed but switch didn't" |
| 504 | + " tell us which one."); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 505 | } |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 506 | } |
Ray Milkey | 4fd3ceb | 2015-12-10 14:43:08 -0800 | [diff] [blame] | 507 | break; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 508 | default: |
| 509 | log.debug("Unhandled message type: {}", msg.getType()); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 510 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 513 | @Override |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 514 | public void receivedRoleReply(Dpid dpid, RoleState requested, |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 515 | RoleState response) { |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 516 | // Do nothing here for now. |
| 517 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 518 | |
sangho | 89bf6fb | 2015-02-09 09:33:13 -0800 | [diff] [blame] | 519 | private void pushFlowMetrics(Dpid dpid, OFFlowStatsReply replies) { |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 520 | |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 521 | DeviceId did = DeviceId.deviceId(Dpid.uri(dpid)); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 522 | |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 523 | List<FlowEntry> flowEntries = replies.getEntries().stream() |
Jonathan Hart | e4e74f0 | 2016-03-03 12:57:40 -0800 | [diff] [blame] | 524 | .map(entry -> new FlowEntryBuilder(did, entry, driverService).build()) |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 525 | .collect(Collectors.toList()); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 526 | |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 527 | if (adaptiveFlowSampling) { |
| 528 | NewAdaptiveFlowStatsCollector afsc = afsCollectors.get(dpid); |
| 529 | |
| 530 | synchronized (afsc) { |
| 531 | if (afsc.getFlowMissingXid() != NewAdaptiveFlowStatsCollector.NO_FLOW_MISSING_XID) { |
| 532 | log.debug("OpenFlowRuleProvider:pushFlowMetrics, flowMissingXid={}, " |
| 533 | + "OFFlowStatsReply Xid={}, for {}", |
| 534 | afsc.getFlowMissingXid(), replies.getXid(), dpid); |
| 535 | } |
| 536 | |
| 537 | // Check that OFFlowStatsReply Xid is same with the one of OFFlowStatsRequest? |
| 538 | if (afsc.getFlowMissingXid() != NewAdaptiveFlowStatsCollector.NO_FLOW_MISSING_XID) { |
| 539 | if (afsc.getFlowMissingXid() == replies.getXid()) { |
| 540 | // call entire flow stats update with flowMissing synchronization. |
| 541 | // used existing pushFlowMetrics |
| 542 | providerService.pushFlowMetrics(did, flowEntries); |
| 543 | } |
| 544 | // reset flowMissingXid to NO_FLOW_MISSING_XID |
| 545 | afsc.setFlowMissingXid(NewAdaptiveFlowStatsCollector.NO_FLOW_MISSING_XID); |
| 546 | |
| 547 | } else { |
| 548 | // call individual flow stats update |
| 549 | providerService.pushFlowMetricsWithoutFlowMissing(did, flowEntries); |
| 550 | } |
| 551 | |
| 552 | // Update TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector |
| 553 | afsc.pushFlowMetrics(flowEntries); |
| 554 | } |
| 555 | } else { |
| 556 | // call existing entire flow stats update with flowMissing synchronization |
| 557 | providerService.pushFlowMetrics(did, flowEntries); |
| 558 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 559 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 560 | |
| 561 | private void pushTableStatistics(Dpid dpid, OFTableStatsReply replies) { |
| 562 | |
| 563 | DeviceId did = DeviceId.deviceId(Dpid.uri(dpid)); |
| 564 | List<TableStatisticsEntry> tableStatsEntries = replies.getEntries().stream() |
| 565 | .map(entry -> buildTableStatistics(did, entry)) |
| 566 | .filter(Objects::nonNull) |
| 567 | .collect(Collectors.toList()); |
| 568 | providerService.pushTableStatistics(did, tableStatsEntries); |
| 569 | } |
| 570 | |
| 571 | private TableStatisticsEntry buildTableStatistics(DeviceId deviceId, |
| 572 | OFTableStatsEntry ofEntry) { |
| 573 | TableStatisticsEntry entry = null; |
| 574 | if (ofEntry != null) { |
| 575 | entry = new DefaultTableStatisticsEntry(deviceId, |
| 576 | ofEntry.getTableId().getValue(), |
| 577 | ofEntry.getActiveCount(), |
| 578 | ofEntry.getLookupCount().getValue(), |
| 579 | ofEntry.getMatchedCount().getValue()); |
| 580 | } |
| 581 | |
| 582 | return entry; |
| 583 | |
| 584 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 585 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 586 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 587 | /** |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 588 | * The internal cache entry holding the original request as well as |
| 589 | * accumulating the any failures along the way. |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 590 | * <p/> |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 591 | * If this entry is evicted from the cache then the entire operation is |
| 592 | * considered failed. Otherwise, only the failures reported by the device |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 593 | * will be propagated up. |
| 594 | */ |
| 595 | private class InternalCacheEntry { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 596 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 597 | private final FlowRuleBatchOperation operation; |
| 598 | private final Set<FlowRule> failures = Sets.newConcurrentHashSet(); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 599 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 600 | public InternalCacheEntry(FlowRuleBatchOperation operation) { |
| 601 | this.operation = operation; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 604 | /** |
| 605 | * Appends a failed rule to the set of failed items. |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 606 | * |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 607 | * @param rule the failed rule |
| 608 | */ |
| 609 | public void appendFailure(FlowRule rule) { |
| 610 | failures.add(rule); |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 611 | } |
| 612 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 613 | /** |
| 614 | * Fails the entire batch and returns the failed operation. |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 615 | * |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 616 | * @return the failed operation |
| 617 | */ |
| 618 | public CompletedBatchOperation failedCompletion() { |
| 619 | Set<FlowRule> fails = operation.getOperations().stream() |
| 620 | .map(op -> op.target()).collect(Collectors.toSet()); |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 621 | return new CompletedBatchOperation(false, |
| 622 | Collections |
| 623 | .unmodifiableSet(fails), |
| 624 | operation.deviceId()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 627 | /** |
| 628 | * Returns the completed operation and whether the batch suceeded. |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 629 | * |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 630 | * @return the completed operation |
| 631 | */ |
| 632 | public CompletedBatchOperation completed() { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 633 | return new CompletedBatchOperation( |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 634 | failures.isEmpty(), |
| 635 | Collections |
| 636 | .unmodifiableSet(failures), |
| 637 | operation.deviceId()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 638 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 639 | } |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 640 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 641 | } |