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