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) { |
Kavitha Alagesan | 6704df3 | 2016-08-18 15:15:31 +0530 | [diff] [blame^] | 225 | if (sw == null) { |
| 226 | return; |
| 227 | } |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 228 | if (adaptiveFlowSampling) { |
| 229 | // NewAdaptiveFlowStatsCollector Constructor |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 230 | NewAdaptiveFlowStatsCollector fsc = |
| 231 | new NewAdaptiveFlowStatsCollector(driverService, sw, flowPollFrequency); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 232 | fsc.start(); |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 233 | stopCollectorIfNeeded(afsCollectors.put(new Dpid(sw.getId()), fsc)); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 234 | } else { |
| 235 | FlowStatsCollector fsc = new FlowStatsCollector(timer, sw, flowPollFrequency); |
| 236 | fsc.start(); |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 237 | stopCollectorIfNeeded(simpleCollectors.put(new Dpid(sw.getId()), fsc)); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 238 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 239 | TableStatisticsCollector tsc = new TableStatisticsCollector(timer, sw, flowPollFrequency); |
| 240 | tsc.start(); |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 241 | stopCollectorIfNeeded(tableStatsCollectors.put(new Dpid(sw.getId()), tsc)); |
| 242 | } |
| 243 | |
| 244 | private void stopCollectorIfNeeded(SwitchDataCollector collector) { |
| 245 | if (collector != null) { |
| 246 | collector.stop(); |
| 247 | } |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | private void stopCollectors() { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 251 | if (adaptiveFlowSampling) { |
| 252 | // NewAdaptiveFlowStatsCollector Destructor |
| 253 | afsCollectors.values().forEach(NewAdaptiveFlowStatsCollector::stop); |
| 254 | afsCollectors.clear(); |
| 255 | } else { |
| 256 | simpleCollectors.values().forEach(FlowStatsCollector::stop); |
| 257 | simpleCollectors.clear(); |
| 258 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 259 | tableStatsCollectors.values().forEach(TableStatisticsCollector::stop); |
| 260 | tableStatsCollectors.clear(); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | private void adjustRate() { |
| 264 | DefaultLoad.setPollInterval(flowPollFrequency); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 265 | if (adaptiveFlowSampling) { |
| 266 | // NewAdaptiveFlowStatsCollector calAndPollInterval |
| 267 | afsCollectors.values().forEach(fsc -> fsc.adjustCalAndPollInterval(flowPollFrequency)); |
| 268 | } else { |
| 269 | simpleCollectors.values().forEach(fsc -> fsc.adjustPollInterval(flowPollFrequency)); |
| 270 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 271 | tableStatsCollectors.values().forEach(tsc -> tsc.adjustPollInterval(flowPollFrequency)); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 272 | } |
| 273 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 274 | @Override |
| 275 | public void applyFlowRule(FlowRule... flowRules) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 276 | for (FlowRule flowRule : flowRules) { |
| 277 | applyRule(flowRule); |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 278 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 279 | } |
| 280 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 281 | private void applyRule(FlowRule flowRule) { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 282 | Dpid dpid = Dpid.dpid(flowRule.deviceId().uri()); |
| 283 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 284 | |
Ray Milkey | 0ae473d | 2016-04-04 10:56:47 -0700 | [diff] [blame] | 285 | if (sw == null) { |
| 286 | return; |
| 287 | } |
| 288 | |
Thomas Vachuska | a6c0d04 | 2015-04-23 10:17:37 -0700 | [diff] [blame] | 289 | FlowRuleExtPayLoad flowRuleExtPayLoad = flowRule.payLoad(); |
| 290 | if (hasPayload(flowRuleExtPayLoad)) { |
| 291 | OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad()); |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 292 | sw.sendMsg(msg); |
| 293 | return; |
| 294 | } |
alshabib | bdcbb10 | 2015-04-22 14:16:38 -0700 | [diff] [blame] | 295 | sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(), |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 296 | Optional.empty(), Optional.of(driverService)).buildFlowAdd()); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 297 | |
| 298 | if (adaptiveFlowSampling) { |
| 299 | // Add TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 300 | NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid); |
| 301 | if (collector != null) { |
| 302 | collector.addWithFlowRule(flowRule); |
| 303 | } |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 304 | } |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 305 | } |
| 306 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 307 | @Override |
| 308 | public void removeFlowRule(FlowRule... flowRules) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 309 | for (FlowRule flowRule : flowRules) { |
| 310 | removeRule(flowRule); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 311 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 312 | } |
| 313 | |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 314 | private void removeRule(FlowRule flowRule) { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 315 | Dpid dpid = Dpid.dpid(flowRule.deviceId().uri()); |
| 316 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 317 | |
Ray Milkey | 0ae473d | 2016-04-04 10:56:47 -0700 | [diff] [blame] | 318 | if (sw == null) { |
| 319 | return; |
| 320 | } |
| 321 | |
Thomas Vachuska | a6c0d04 | 2015-04-23 10:17:37 -0700 | [diff] [blame] | 322 | FlowRuleExtPayLoad flowRuleExtPayLoad = flowRule.payLoad(); |
| 323 | if (hasPayload(flowRuleExtPayLoad)) { |
| 324 | OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad()); |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 325 | sw.sendMsg(msg); |
| 326 | return; |
| 327 | } |
alshabib | bdcbb10 | 2015-04-22 14:16:38 -0700 | [diff] [blame] | 328 | sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(), |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 329 | Optional.empty(), Optional.of(driverService)).buildFlowDel()); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 330 | |
| 331 | if (adaptiveFlowSampling) { |
| 332 | // Remove TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 333 | NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid); |
| 334 | if (collector != null) { |
| 335 | collector.removeFlows(flowRule); |
| 336 | } |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 337 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 338 | } |
| 339 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 340 | @Override |
| 341 | public void removeRulesById(ApplicationId id, FlowRule... flowRules) { |
| 342 | // TODO: optimize using the ApplicationId |
| 343 | removeFlowRule(flowRules); |
| 344 | } |
| 345 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 346 | @Override |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 347 | public void executeBatch(FlowRuleBatchOperation batch) { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 348 | checkNotNull(batch); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 349 | |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 350 | Dpid dpid = Dpid.dpid(batch.deviceId().uri()); |
| 351 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
Madan Jampani | 84382b9 | 2016-06-22 08:26:49 -0700 | [diff] [blame] | 352 | |
| 353 | // If switch no longer exists, simply return. |
| 354 | if (sw == null) { |
| 355 | Set<FlowRule> failures = ImmutableSet.copyOf(Lists.transform(batch.getOperations(), e -> e.target())); |
| 356 | providerService.batchOperationCompleted(batch.id(), |
| 357 | new CompletedBatchOperation(false, failures, batch.deviceId())); |
| 358 | return; |
| 359 | } |
| 360 | pendingBatches.put(batch.id(), new InternalCacheEntry(batch)); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 361 | OFFlowMod mod; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 362 | for (FlowRuleBatchEntry fbe : batch.getOperations()) { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 363 | // flow is the third party privacy flow |
Thomas Vachuska | a6c0d04 | 2015-04-23 10:17:37 -0700 | [diff] [blame] | 364 | |
| 365 | FlowRuleExtPayLoad flowRuleExtPayLoad = fbe.target().payLoad(); |
| 366 | if (hasPayload(flowRuleExtPayLoad)) { |
| 367 | OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad()); |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 368 | sw.sendMsg(msg); |
| 369 | continue; |
| 370 | } |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 371 | FlowModBuilder builder = |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 372 | FlowModBuilder.builder(fbe.target(), sw.factory(), |
| 373 | Optional.of(batch.id()), Optional.of(driverService)); |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 374 | NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid); |
Sho SHIMIZU | aba9d00 | 2015-01-29 14:51:04 -0800 | [diff] [blame] | 375 | switch (fbe.operator()) { |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 376 | case ADD: |
| 377 | mod = builder.buildFlowAdd(); |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 378 | if (adaptiveFlowSampling && collector != null) { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 379 | // Add TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 380 | collector.addWithFlowRule(fbe.target()); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 381 | } |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 382 | break; |
| 383 | case REMOVE: |
| 384 | mod = builder.buildFlowDel(); |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 385 | if (adaptiveFlowSampling && collector != null) { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 386 | // Remove TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 387 | collector.removeFlows(fbe.target()); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 388 | } |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 389 | break; |
| 390 | case MODIFY: |
| 391 | mod = builder.buildFlowMod(); |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 392 | if (adaptiveFlowSampling && collector != null) { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 393 | // Add or Update TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector |
| 394 | // 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] | 395 | collector.addOrUpdateFlows((FlowEntry) fbe.target()); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 396 | } |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 397 | break; |
| 398 | default: |
| 399 | log.error("Unsupported batch operation {}; skipping flowmod {}", |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 400 | fbe.operator(), fbe); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 401 | continue; |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 402 | } |
Saurav Das | 3ea4662 | 2015-04-22 14:01:34 -0700 | [diff] [blame] | 403 | sw.sendMsg(mod); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 404 | } |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 405 | OFBarrierRequest.Builder builder = sw.factory().buildBarrierRequest() |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 406 | .setXid(batch.id()); |
| 407 | sw.sendMsg(builder.build()); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Thomas Vachuska | a6c0d04 | 2015-04-23 10:17:37 -0700 | [diff] [blame] | 410 | private boolean hasPayload(FlowRuleExtPayLoad flowRuleExtPayLoad) { |
| 411 | return flowRuleExtPayLoad != null && |
| 412 | flowRuleExtPayLoad.payLoad() != null && |
| 413 | flowRuleExtPayLoad.payLoad().length > 0; |
| 414 | } |
| 415 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 416 | private class InternalFlowProvider |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 417 | implements OpenFlowSwitchListener, OpenFlowEventListener { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 418 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 419 | @Override |
| 420 | public void switchAdded(Dpid dpid) { |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 421 | createCollector(controller.getSwitch(dpid)); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | @Override |
| 425 | public void switchRemoved(Dpid dpid) { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 426 | if (adaptiveFlowSampling) { |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 427 | stopCollectorIfNeeded(afsCollectors.remove(dpid)); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 428 | } else { |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 429 | stopCollectorIfNeeded(simpleCollectors.remove(dpid)); |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 430 | } |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 431 | stopCollectorIfNeeded(tableStatsCollectors.remove(dpid)); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | @Override |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 435 | public void switchChanged(Dpid dpid) { |
| 436 | } |
| 437 | |
| 438 | @Override |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 439 | public void portChanged(Dpid dpid, OFPortStatus status) { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 440 | // TODO: Decide whether to evict flows internal store. |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | @Override |
| 444 | public void handleMessage(Dpid dpid, OFMessage msg) { |
Ray Milkey | ada9e2d | 2016-04-05 16:42:35 -0700 | [diff] [blame] | 445 | if (providerService == null) { |
| 446 | // We are shutting down, nothing to be done |
| 447 | return; |
| 448 | } |
Jonathan Hart | e4e74f0 | 2016-03-03 12:57:40 -0800 | [diff] [blame] | 449 | DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid)); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 450 | switch (msg.getType()) { |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 451 | case FLOW_REMOVED: |
| 452 | OFFlowRemoved removed = (OFFlowRemoved) msg; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 453 | |
Jonathan Hart | e4e74f0 | 2016-03-03 12:57:40 -0800 | [diff] [blame] | 454 | FlowEntry fr = new FlowEntryBuilder(deviceId, removed, driverService).build(); |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 455 | providerService.flowRemoved(fr); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 456 | |
| 457 | if (adaptiveFlowSampling) { |
| 458 | // Removed TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 459 | NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid); |
| 460 | if (collector != null) { |
| 461 | collector.flowRemoved(fr); |
| 462 | } |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 463 | } |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 464 | break; |
| 465 | case STATS_REPLY: |
| 466 | if (((OFStatsReply) msg).getStatsType() == OFStatsType.FLOW) { |
| 467 | pushFlowMetrics(dpid, (OFFlowStatsReply) msg); |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 468 | } else if (((OFStatsReply) msg).getStatsType() == OFStatsType.TABLE) { |
| 469 | pushTableStatistics(dpid, (OFTableStatsReply) msg); |
sangho | 89bf6fb | 2015-02-09 09:33:13 -0800 | [diff] [blame] | 470 | } |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 471 | break; |
| 472 | case BARRIER_REPLY: |
| 473 | try { |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 474 | InternalCacheEntry entry = pendingBatches.getIfPresent(msg.getXid()); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 475 | if (entry != null) { |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 476 | providerService |
| 477 | .batchOperationCompleted(msg.getXid(), |
| 478 | entry.completed()); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 479 | } else { |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 480 | log.warn("Received unknown Barrier Reply: {}", |
| 481 | msg.getXid()); |
| 482 | } |
| 483 | } finally { |
| 484 | pendingBatches.invalidate(msg.getXid()); |
| 485 | } |
| 486 | break; |
| 487 | case ERROR: |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 488 | // TODO: This needs to get suppressed in a better way. |
| 489 | if (msg instanceof OFBadRequestErrorMsg && |
| 490 | ((OFBadRequestErrorMsg) msg).getCode() == OFBadRequestCode.BAD_TYPE) { |
| 491 | log.debug("Received error message {} from {}", msg, dpid); |
| 492 | } else { |
| 493 | log.warn("Received error message {} from {}", msg, dpid); |
| 494 | } |
Prince Pereira | 788797e | 2016-08-10 11:24:14 +0530 | [diff] [blame] | 495 | handleErrorMsg(deviceId, msg); |
Ray Milkey | 4fd3ceb | 2015-12-10 14:43:08 -0800 | [diff] [blame] | 496 | break; |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 497 | default: |
| 498 | log.debug("Unhandled message type: {}", msg.getType()); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 499 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Prince Pereira | 788797e | 2016-08-10 11:24:14 +0530 | [diff] [blame] | 502 | private void handleErrorMsg(DeviceId deviceId, OFMessage msg) { |
| 503 | OFErrorMsg error = (OFErrorMsg) msg; |
| 504 | OFMessage ofMessage = null; |
| 505 | switch (error.getErrType()) { |
| 506 | case BAD_ACTION: |
| 507 | OFBadActionErrorMsg baErrorMsg = (OFBadActionErrorMsg) error; |
| 508 | if (baErrorMsg.getData().getParsedMessage().isPresent()) { |
| 509 | ofMessage = baErrorMsg.getData().getParsedMessage().get(); |
| 510 | } |
| 511 | break; |
| 512 | case BAD_INSTRUCTION: |
| 513 | OFBadInstructionErrorMsg biErrorMsg = (OFBadInstructionErrorMsg) error; |
| 514 | if (biErrorMsg.getData().getParsedMessage().isPresent()) { |
| 515 | ofMessage = biErrorMsg.getData().getParsedMessage().get(); |
| 516 | } |
| 517 | break; |
| 518 | case BAD_MATCH: |
| 519 | OFBadMatchErrorMsg bmErrorMsg = (OFBadMatchErrorMsg) error; |
| 520 | if (bmErrorMsg.getData().getParsedMessage().isPresent()) { |
| 521 | ofMessage = bmErrorMsg.getData().getParsedMessage().get(); |
| 522 | } |
| 523 | break; |
| 524 | case FLOW_MOD_FAILED: |
| 525 | OFFlowModFailedErrorMsg fmFailed = (OFFlowModFailedErrorMsg) error; |
| 526 | if (fmFailed.getData().getParsedMessage().isPresent()) { |
| 527 | ofMessage = fmFailed.getData().getParsedMessage().get(); |
| 528 | } |
| 529 | break; |
| 530 | default: |
| 531 | // Do nothing. |
| 532 | return; |
| 533 | } |
| 534 | if (ofMessage != null) { |
| 535 | InternalCacheEntry entry = |
| 536 | pendingBatches.getIfPresent(msg.getXid()); |
| 537 | if (entry != null) { |
| 538 | OFFlowMod ofFlowMod = (OFFlowMod) ofMessage; |
| 539 | entry.appendFailure(new FlowEntryBuilder(deviceId, ofFlowMod, driverService).build()); |
| 540 | } else { |
| 541 | log.error("No matching batch for this error: {}", error); |
| 542 | } |
| 543 | } else { |
| 544 | log.error("Flow installation failed but switch didn't" |
| 545 | + " tell us which one."); |
| 546 | } |
| 547 | } |
| 548 | |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 549 | @Override |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 550 | public void receivedRoleReply(Dpid dpid, RoleState requested, |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 551 | RoleState response) { |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 552 | // Do nothing here for now. |
| 553 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 554 | |
sangho | 89bf6fb | 2015-02-09 09:33:13 -0800 | [diff] [blame] | 555 | private void pushFlowMetrics(Dpid dpid, OFFlowStatsReply replies) { |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 556 | |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 557 | DeviceId did = DeviceId.deviceId(Dpid.uri(dpid)); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 558 | |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 559 | List<FlowEntry> flowEntries = replies.getEntries().stream() |
Jonathan Hart | e4e74f0 | 2016-03-03 12:57:40 -0800 | [diff] [blame] | 560 | .map(entry -> new FlowEntryBuilder(did, entry, driverService).build()) |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 561 | .collect(Collectors.toList()); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 562 | |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 563 | if (adaptiveFlowSampling) { |
| 564 | NewAdaptiveFlowStatsCollector afsc = afsCollectors.get(dpid); |
| 565 | |
| 566 | synchronized (afsc) { |
| 567 | if (afsc.getFlowMissingXid() != NewAdaptiveFlowStatsCollector.NO_FLOW_MISSING_XID) { |
| 568 | log.debug("OpenFlowRuleProvider:pushFlowMetrics, flowMissingXid={}, " |
| 569 | + "OFFlowStatsReply Xid={}, for {}", |
| 570 | afsc.getFlowMissingXid(), replies.getXid(), dpid); |
| 571 | } |
| 572 | |
| 573 | // Check that OFFlowStatsReply Xid is same with the one of OFFlowStatsRequest? |
| 574 | if (afsc.getFlowMissingXid() != NewAdaptiveFlowStatsCollector.NO_FLOW_MISSING_XID) { |
| 575 | if (afsc.getFlowMissingXid() == replies.getXid()) { |
| 576 | // call entire flow stats update with flowMissing synchronization. |
| 577 | // used existing pushFlowMetrics |
| 578 | providerService.pushFlowMetrics(did, flowEntries); |
| 579 | } |
| 580 | // reset flowMissingXid to NO_FLOW_MISSING_XID |
| 581 | afsc.setFlowMissingXid(NewAdaptiveFlowStatsCollector.NO_FLOW_MISSING_XID); |
| 582 | |
| 583 | } else { |
| 584 | // call individual flow stats update |
| 585 | providerService.pushFlowMetricsWithoutFlowMissing(did, flowEntries); |
| 586 | } |
| 587 | |
| 588 | // Update TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector |
| 589 | afsc.pushFlowMetrics(flowEntries); |
| 590 | } |
| 591 | } else { |
| 592 | // call existing entire flow stats update with flowMissing synchronization |
| 593 | providerService.pushFlowMetrics(did, flowEntries); |
| 594 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 595 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 596 | |
| 597 | private void pushTableStatistics(Dpid dpid, OFTableStatsReply replies) { |
| 598 | |
| 599 | DeviceId did = DeviceId.deviceId(Dpid.uri(dpid)); |
| 600 | List<TableStatisticsEntry> tableStatsEntries = replies.getEntries().stream() |
| 601 | .map(entry -> buildTableStatistics(did, entry)) |
| 602 | .filter(Objects::nonNull) |
| 603 | .collect(Collectors.toList()); |
| 604 | providerService.pushTableStatistics(did, tableStatsEntries); |
| 605 | } |
| 606 | |
| 607 | private TableStatisticsEntry buildTableStatistics(DeviceId deviceId, |
| 608 | OFTableStatsEntry ofEntry) { |
| 609 | TableStatisticsEntry entry = null; |
| 610 | if (ofEntry != null) { |
| 611 | entry = new DefaultTableStatisticsEntry(deviceId, |
| 612 | ofEntry.getTableId().getValue(), |
| 613 | ofEntry.getActiveCount(), |
| 614 | ofEntry.getLookupCount().getValue(), |
| 615 | ofEntry.getMatchedCount().getValue()); |
| 616 | } |
| 617 | |
| 618 | return entry; |
| 619 | |
| 620 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 621 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 622 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 623 | /** |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 624 | * The internal cache entry holding the original request as well as |
| 625 | * accumulating the any failures along the way. |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 626 | * <p/> |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 627 | * If this entry is evicted from the cache then the entire operation is |
| 628 | * considered failed. Otherwise, only the failures reported by the device |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 629 | * will be propagated up. |
| 630 | */ |
| 631 | private class InternalCacheEntry { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 632 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 633 | private final FlowRuleBatchOperation operation; |
| 634 | private final Set<FlowRule> failures = Sets.newConcurrentHashSet(); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 635 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 636 | public InternalCacheEntry(FlowRuleBatchOperation operation) { |
| 637 | this.operation = operation; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 640 | /** |
| 641 | * Appends a failed rule to the set of failed items. |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 642 | * |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 643 | * @param rule the failed rule |
| 644 | */ |
| 645 | public void appendFailure(FlowRule rule) { |
| 646 | failures.add(rule); |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 647 | } |
| 648 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 649 | /** |
| 650 | * Fails the entire batch and returns the failed operation. |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 651 | * |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 652 | * @return the failed operation |
| 653 | */ |
| 654 | public CompletedBatchOperation failedCompletion() { |
| 655 | Set<FlowRule> fails = operation.getOperations().stream() |
| 656 | .map(op -> op.target()).collect(Collectors.toSet()); |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 657 | return new CompletedBatchOperation(false, |
| 658 | Collections |
| 659 | .unmodifiableSet(fails), |
| 660 | operation.deviceId()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 663 | /** |
| 664 | * Returns the completed operation and whether the batch suceeded. |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 665 | * |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 666 | * @return the completed operation |
| 667 | */ |
| 668 | public CompletedBatchOperation completed() { |
jcc | 3d4e14a | 2015-04-21 11:32:05 +0800 | [diff] [blame] | 669 | return new CompletedBatchOperation( |
Thomas Vachuska | 75aaa67 | 2015-04-29 12:24:43 -0700 | [diff] [blame] | 670 | failures.isEmpty(), |
| 671 | Collections |
| 672 | .unmodifiableSet(failures), |
| 673 | operation.deviceId()); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 674 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 675 | } |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 676 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 677 | } |