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