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