blob: 6374ca5598644eda60f6106d094009a3fd60f309 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
ssyoon9030fbcd92015-08-17 10:42:07 +09002 * Copyright 2015 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.provider.of.flow.impl;
alshabib1cc04f72014-09-16 16:09:58 -070017
Thomas Vachuska75aaa672015-04-29 12:24:43 -070018import com.google.common.cache.Cache;
19import com.google.common.cache.CacheBuilder;
20import com.google.common.cache.RemovalCause;
21import com.google.common.cache.RemovalNotification;
22import com.google.common.collect.Maps;
23import com.google.common.collect.Sets;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070024
alshabib1cc04f72014-09-16 16:09:58 -070025import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070028import org.apache.felix.scr.annotations.Modified;
29import org.apache.felix.scr.annotations.Property;
alshabib1cc04f72014-09-16 16:09:58 -070030import org.apache.felix.scr.annotations.Reference;
31import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070032import org.onosproject.cfg.ComponentConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.core.ApplicationId;
34import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.flow.CompletedBatchOperation;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070036import org.onosproject.net.flow.DefaultTableStatisticsEntry;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.net.flow.FlowEntry;
38import org.onosproject.net.flow.FlowRule;
39import org.onosproject.net.flow.FlowRuleBatchEntry;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080040import org.onosproject.net.flow.FlowRuleBatchOperation;
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -070041import org.onosproject.net.flow.FlowRuleExtPayLoad;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.net.flow.FlowRuleProvider;
43import org.onosproject.net.flow.FlowRuleProviderRegistry;
44import org.onosproject.net.flow.FlowRuleProviderService;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070045import org.onosproject.net.flow.TableStatisticsEntry;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import org.onosproject.net.provider.AbstractProvider;
47import org.onosproject.net.provider.ProviderId;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070048import org.onosproject.net.statistic.DefaultLoad;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import org.onosproject.openflow.controller.Dpid;
50import org.onosproject.openflow.controller.OpenFlowController;
51import org.onosproject.openflow.controller.OpenFlowEventListener;
52import org.onosproject.openflow.controller.OpenFlowSwitch;
53import org.onosproject.openflow.controller.OpenFlowSwitchListener;
54import org.onosproject.openflow.controller.RoleState;
jcc3d4e14a2015-04-21 11:32:05 +080055import org.onosproject.openflow.controller.ThirdPartyMessage;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070056import org.osgi.service.component.ComponentContext;
Thomas Vachuska3358af22015-05-19 18:40:34 -070057import org.projectfloodlight.openflow.protocol.OFBadRequestCode;
alshabib902d41b2014-10-07 16:52:05 -070058import org.projectfloodlight.openflow.protocol.OFBarrierRequest;
59import org.projectfloodlight.openflow.protocol.OFErrorMsg;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080060import org.projectfloodlight.openflow.protocol.OFErrorType;
alshabib193525b2014-10-08 18:58:03 -070061import org.projectfloodlight.openflow.protocol.OFFlowMod;
alshabib8f1cf4a2014-09-17 14:44:48 -070062import org.projectfloodlight.openflow.protocol.OFFlowRemoved;
alshabib5c370ff2014-09-18 10:12:14 -070063import org.projectfloodlight.openflow.protocol.OFFlowStatsReply;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070064import org.projectfloodlight.openflow.protocol.OFTableStatsReply;
65import org.projectfloodlight.openflow.protocol.OFTableStatsEntry;
alshabib8f1cf4a2014-09-17 14:44:48 -070066import org.projectfloodlight.openflow.protocol.OFMessage;
67import org.projectfloodlight.openflow.protocol.OFPortStatus;
alshabib5c370ff2014-09-18 10:12:14 -070068import org.projectfloodlight.openflow.protocol.OFStatsReply;
sangho89bf6fb2015-02-09 09:33:13 -080069import org.projectfloodlight.openflow.protocol.OFStatsType;
Thomas Vachuska3358af22015-05-19 18:40:34 -070070import org.projectfloodlight.openflow.protocol.errormsg.OFBadRequestErrorMsg;
alshabib193525b2014-10-08 18:58:03 -070071import org.projectfloodlight.openflow.protocol.errormsg.OFFlowModFailedErrorMsg;
alshabib1cc04f72014-09-16 16:09:58 -070072import org.slf4j.Logger;
73
Thomas Vachuska75aaa672015-04-29 12:24:43 -070074import java.util.Collections;
75import java.util.Dictionary;
76import java.util.List;
77import java.util.Map;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070078import java.util.Objects;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070079import java.util.Optional;
80import java.util.Set;
81import java.util.Timer;
82import java.util.concurrent.TimeUnit;
83import java.util.stream.Collectors;
84
ssyoon9030fbcd92015-08-17 10:42:07 +090085import static com.google.common.base.Preconditions.checkNotNull;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070086import static com.google.common.base.Strings.isNullOrEmpty;
87import static org.onlab.util.Tools.get;
88import static org.slf4j.LoggerFactory.getLogger;
alshabibeec3a062014-09-17 18:01:26 -070089
alshabib1cc04f72014-09-16 16:09:58 -070090/**
jcc3d4e14a2015-04-21 11:32:05 +080091 * Provider which uses an OpenFlow controller to detect network end-station
92 * hosts.
alshabib1cc04f72014-09-16 16:09:58 -070093 */
94@Component(immediate = true)
jcc3d4e14a2015-04-21 11:32:05 +080095public class OpenFlowRuleProvider extends AbstractProvider
96 implements FlowRuleProvider {
alshabib1cc04f72014-09-16 16:09:58 -070097
98 private final Logger log = getLogger(getClass());
99
100 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
101 protected FlowRuleProviderRegistry providerRegistry;
102
103 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
104 protected OpenFlowController controller;
105
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700106 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
107 protected ComponentConfigService cfgService;
108
ssyoon9030fbcd92015-08-17 10:42:07 +0900109 private static final int DEFAULT_POLL_FREQUENCY = 5;
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700110 @Property(name = "flowPollFrequency", intValue = DEFAULT_POLL_FREQUENCY,
111 label = "Frequency (in seconds) for polling flow statistics")
112 private int flowPollFrequency = DEFAULT_POLL_FREQUENCY;
113
ssyoon9030fbcd92015-08-17 10:42:07 +0900114 private static final boolean DEFAULT_ADAPTIVE_FLOW_SAMPLING = true;
115 @Property(name = "adaptiveFlowSampling", boolValue = DEFAULT_ADAPTIVE_FLOW_SAMPLING,
116 label = "Adaptive Flow Sampling is on or off")
117 private boolean adaptiveFlowSampling = DEFAULT_ADAPTIVE_FLOW_SAMPLING;
118
alshabib1cc04f72014-09-16 16:09:58 -0700119 private FlowRuleProviderService providerService;
120
alshabibeec3a062014-09-17 18:01:26 -0700121 private final InternalFlowProvider listener = new InternalFlowProvider();
122
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800123 private Cache<Long, InternalCacheEntry> pendingBatches;
alshabib193525b2014-10-08 18:58:03 -0700124
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700125 private final Timer timer = new Timer("onos-openflow-collector");
ssyoon9030fbcd92015-08-17 10:42:07 +0900126 private final Map<Dpid, FlowStatsCollector> simpleCollectors = Maps.newHashMap();
127
128 // NewAdaptiveFlowStatsCollector Set
129 private final Map<Dpid, NewAdaptiveFlowStatsCollector> afsCollectors = Maps.newHashMap();
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700130 private final Map<Dpid, FlowStatsCollector> collectors = Maps.newHashMap();
131 private final Map<Dpid, TableStatisticsCollector> tableStatsCollectors = Maps.newHashMap();
alshabib3d643ec2014-10-22 18:33:00 -0700132
alshabib1cc04f72014-09-16 16:09:58 -0700133 /**
134 * Creates an OpenFlow host provider.
135 */
136 public OpenFlowRuleProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800137 super(new ProviderId("of", "org.onosproject.provider.openflow"));
alshabib1cc04f72014-09-16 16:09:58 -0700138 }
139
140 @Activate
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700141 public void activate(ComponentContext context) {
142 cfgService.registerProperties(getClass());
alshabib1cc04f72014-09-16 16:09:58 -0700143 providerService = providerRegistry.register(this);
alshabibeec3a062014-09-17 18:01:26 -0700144 controller.addListener(listener);
145 controller.addEventListener(listener);
alshabib3d643ec2014-10-22 18:33:00 -0700146
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700147 pendingBatches = createBatchCache();
ssyoon9030fbcd92015-08-17 10:42:07 +0900148
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700149 createCollectors();
alshabib3d643ec2014-10-22 18:33:00 -0700150
ssyoon9030fbcd92015-08-17 10:42:07 +0900151 log.info("Started with flowPollFrequency = {}, adaptiveFlowSampling = {}",
152 flowPollFrequency, adaptiveFlowSampling);
alshabib1cc04f72014-09-16 16:09:58 -0700153 }
154
155 @Deactivate
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700156 public void deactivate(ComponentContext context) {
157 cfgService.unregisterProperties(getClass(), false);
158 stopCollectors();
alshabib1cc04f72014-09-16 16:09:58 -0700159 providerRegistry.unregister(this);
160 providerService = null;
161
162 log.info("Stopped");
163 }
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800164
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700165 @Modified
166 public void modified(ComponentContext context) {
167 Dictionary<?, ?> properties = context.getProperties();
168 int newFlowPollFrequency;
169 try {
170 String s = get(properties, "flowPollFrequency");
171 newFlowPollFrequency = isNullOrEmpty(s) ? flowPollFrequency : Integer.parseInt(s.trim());
172
173 } catch (NumberFormatException | ClassCastException e) {
174 newFlowPollFrequency = flowPollFrequency;
175 }
176
177 if (newFlowPollFrequency != flowPollFrequency) {
178 flowPollFrequency = newFlowPollFrequency;
179 adjustRate();
180 }
181
182 log.info("Settings: flowPollFrequency={}", flowPollFrequency);
ssyoon9030fbcd92015-08-17 10:42:07 +0900183
184 boolean newAdaptiveFlowSampling;
185 String s = get(properties, "adaptiveFlowSampling");
186 newAdaptiveFlowSampling = isNullOrEmpty(s) ? adaptiveFlowSampling : Boolean.parseBoolean(s.trim());
187
188 if (newAdaptiveFlowSampling != adaptiveFlowSampling) {
189 // stop previous collector
190 stopCollectors();
191 adaptiveFlowSampling = newAdaptiveFlowSampling;
192 // create new collectors
193 createCollectors();
194 }
195
196 log.info("Settings: adaptiveFlowSampling={}", adaptiveFlowSampling);
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700197 }
198
199 private Cache<Long, InternalCacheEntry> createBatchCache() {
200 return CacheBuilder.newBuilder()
201 .expireAfterWrite(10, TimeUnit.SECONDS)
202 .removalListener((RemovalNotification<Long, InternalCacheEntry> notification) -> {
203 if (notification.getCause() == RemovalCause.EXPIRED) {
204 providerService.batchOperationCompleted(notification.getKey(),
205 notification.getValue().failedCompletion());
206 }
207 }).build();
208 }
209
210 private void createCollectors() {
211 controller.getSwitches().forEach(this::createCollector);
212 }
213
214 private void createCollector(OpenFlowSwitch sw) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900215 if (adaptiveFlowSampling) {
216 // NewAdaptiveFlowStatsCollector Constructor
217 NewAdaptiveFlowStatsCollector fsc = new NewAdaptiveFlowStatsCollector(sw, flowPollFrequency);
218 fsc.start();
219 afsCollectors.put(new Dpid(sw.getId()), fsc);
220 } else {
221 FlowStatsCollector fsc = new FlowStatsCollector(timer, sw, flowPollFrequency);
222 fsc.start();
223 simpleCollectors.put(new Dpid(sw.getId()), fsc);
224 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700225 TableStatisticsCollector tsc = new TableStatisticsCollector(timer, sw, flowPollFrequency);
226 tsc.start();
227 tableStatsCollectors.put(new Dpid(sw.getId()), tsc);
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700228 }
229
230 private void stopCollectors() {
ssyoon9030fbcd92015-08-17 10:42:07 +0900231 if (adaptiveFlowSampling) {
232 // NewAdaptiveFlowStatsCollector Destructor
233 afsCollectors.values().forEach(NewAdaptiveFlowStatsCollector::stop);
234 afsCollectors.clear();
235 } else {
236 simpleCollectors.values().forEach(FlowStatsCollector::stop);
237 simpleCollectors.clear();
238 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700239 tableStatsCollectors.values().forEach(TableStatisticsCollector::stop);
240 tableStatsCollectors.clear();
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700241 }
242
243 private void adjustRate() {
244 DefaultLoad.setPollInterval(flowPollFrequency);
ssyoon9030fbcd92015-08-17 10:42:07 +0900245 if (adaptiveFlowSampling) {
246 // NewAdaptiveFlowStatsCollector calAndPollInterval
247 afsCollectors.values().forEach(fsc -> fsc.adjustCalAndPollInterval(flowPollFrequency));
248 } else {
249 simpleCollectors.values().forEach(fsc -> fsc.adjustPollInterval(flowPollFrequency));
250 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700251 tableStatsCollectors.values().forEach(tsc -> tsc.adjustPollInterval(flowPollFrequency));
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700252 }
253
alshabib1cc04f72014-09-16 16:09:58 -0700254 @Override
255 public void applyFlowRule(FlowRule... flowRules) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800256 for (FlowRule flowRule : flowRules) {
257 applyRule(flowRule);
alshabib35edb1a2014-09-16 17:44:44 -0700258 }
alshabib1cc04f72014-09-16 16:09:58 -0700259 }
260
alshabib35edb1a2014-09-16 17:44:44 -0700261 private void applyRule(FlowRule flowRule) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900262 Dpid dpid = Dpid.dpid(flowRule.deviceId().uri());
263 OpenFlowSwitch sw = controller.getSwitch(dpid);
264
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -0700265 FlowRuleExtPayLoad flowRuleExtPayLoad = flowRule.payLoad();
266 if (hasPayload(flowRuleExtPayLoad)) {
267 OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad());
jcc3d4e14a2015-04-21 11:32:05 +0800268 sw.sendMsg(msg);
269 return;
270 }
alshabibbdcbb102015-04-22 14:16:38 -0700271 sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(),
jcc3d4e14a2015-04-21 11:32:05 +0800272 Optional.empty()).buildFlowAdd());
ssyoon9030fbcd92015-08-17 10:42:07 +0900273
274 if (adaptiveFlowSampling) {
275 // Add TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700276 NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid);
277 if (collector != null) {
278 collector.addWithFlowRule(flowRule);
279 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900280 }
alshabib35edb1a2014-09-16 17:44:44 -0700281 }
282
alshabib1cc04f72014-09-16 16:09:58 -0700283 @Override
284 public void removeFlowRule(FlowRule... flowRules) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800285 for (FlowRule flowRule : flowRules) {
286 removeRule(flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700287 }
alshabib1cc04f72014-09-16 16:09:58 -0700288 }
289
alshabib219ebaa2014-09-22 15:41:24 -0700290 private void removeRule(FlowRule flowRule) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900291 Dpid dpid = Dpid.dpid(flowRule.deviceId().uri());
292 OpenFlowSwitch sw = controller.getSwitch(dpid);
293
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -0700294 FlowRuleExtPayLoad flowRuleExtPayLoad = flowRule.payLoad();
295 if (hasPayload(flowRuleExtPayLoad)) {
296 OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad());
jcc3d4e14a2015-04-21 11:32:05 +0800297 sw.sendMsg(msg);
298 return;
299 }
alshabibbdcbb102015-04-22 14:16:38 -0700300 sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(),
jcc3d4e14a2015-04-21 11:32:05 +0800301 Optional.empty()).buildFlowDel());
ssyoon9030fbcd92015-08-17 10:42:07 +0900302
303 if (adaptiveFlowSampling) {
304 // Remove TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700305 NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid);
306 if (collector != null) {
307 collector.removeFlows(flowRule);
308 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900309 }
alshabib219ebaa2014-09-22 15:41:24 -0700310 }
311
alshabiba68eb962014-09-24 20:34:13 -0700312 @Override
313 public void removeRulesById(ApplicationId id, FlowRule... flowRules) {
314 // TODO: optimize using the ApplicationId
315 removeFlowRule(flowRules);
316 }
317
alshabib193525b2014-10-08 18:58:03 -0700318 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800319 public void executeBatch(FlowRuleBatchOperation batch) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900320 checkNotNull(batch);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800321
322 pendingBatches.put(batch.id(), new InternalCacheEntry(batch));
323
ssyoon9030fbcd92015-08-17 10:42:07 +0900324 Dpid dpid = Dpid.dpid(batch.deviceId().uri());
325 OpenFlowSwitch sw = controller.getSwitch(dpid);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800326 OFFlowMod mod;
alshabib193525b2014-10-08 18:58:03 -0700327 for (FlowRuleBatchEntry fbe : batch.getOperations()) {
jcc3d4e14a2015-04-21 11:32:05 +0800328 // flow is the third party privacy flow
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -0700329
330 FlowRuleExtPayLoad flowRuleExtPayLoad = fbe.target().payLoad();
331 if (hasPayload(flowRuleExtPayLoad)) {
332 OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad());
jcc3d4e14a2015-04-21 11:32:05 +0800333 sw.sendMsg(msg);
334 continue;
335 }
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700336 FlowModBuilder builder =
337 FlowModBuilder.builder(fbe.target(), sw.factory(), Optional.of(batch.id()));
338 NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid);
Sho SHIMIZUaba9d002015-01-29 14:51:04 -0800339 switch (fbe.operator()) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700340 case ADD:
341 mod = builder.buildFlowAdd();
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700342 if (adaptiveFlowSampling && collector != null) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900343 // Add TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700344 collector.addWithFlowRule(fbe.target());
ssyoon9030fbcd92015-08-17 10:42:07 +0900345 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700346 break;
347 case REMOVE:
348 mod = builder.buildFlowDel();
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700349 if (adaptiveFlowSampling && collector != null) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900350 // Remove TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700351 collector.removeFlows(fbe.target());
ssyoon9030fbcd92015-08-17 10:42:07 +0900352 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700353 break;
354 case MODIFY:
355 mod = builder.buildFlowMod();
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700356 if (adaptiveFlowSampling && collector != null) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900357 // Add or Update TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
358 // afsCollectors.get(dpid).addWithFlowRule(fbe.target()); //check if add is good or not
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700359 collector.addOrUpdateFlows((FlowEntry) fbe.target());
ssyoon9030fbcd92015-08-17 10:42:07 +0900360 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700361 break;
362 default:
363 log.error("Unsupported batch operation {}; skipping flowmod {}",
ssyoon9030fbcd92015-08-17 10:42:07 +0900364 fbe.operator(), fbe);
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700365 continue;
jcc3d4e14a2015-04-21 11:32:05 +0800366 }
Saurav Das3ea46622015-04-22 14:01:34 -0700367 sw.sendMsg(mod);
alshabib193525b2014-10-08 18:58:03 -0700368 }
jcc3d4e14a2015-04-21 11:32:05 +0800369 OFBarrierRequest.Builder builder = sw.factory().buildBarrierRequest()
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800370 .setXid(batch.id());
371 sw.sendMsg(builder.build());
alshabib193525b2014-10-08 18:58:03 -0700372 }
373
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -0700374 private boolean hasPayload(FlowRuleExtPayLoad flowRuleExtPayLoad) {
375 return flowRuleExtPayLoad != null &&
376 flowRuleExtPayLoad.payLoad() != null &&
377 flowRuleExtPayLoad.payLoad().length > 0;
378 }
379
alshabib8f1cf4a2014-09-17 14:44:48 -0700380 private class InternalFlowProvider
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800381 implements OpenFlowSwitchListener, OpenFlowEventListener {
alshabib8f1cf4a2014-09-17 14:44:48 -0700382
alshabib8f1cf4a2014-09-17 14:44:48 -0700383 @Override
384 public void switchAdded(Dpid dpid) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900385
386 OpenFlowSwitch sw = controller.getSwitch(dpid);
387
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700388 createCollector(controller.getSwitch(dpid));
alshabib8f1cf4a2014-09-17 14:44:48 -0700389 }
390
391 @Override
392 public void switchRemoved(Dpid dpid) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900393 if (adaptiveFlowSampling) {
394 NewAdaptiveFlowStatsCollector collector = afsCollectors.remove(dpid);
395 if (collector != null) {
396 collector.stop();
397 }
398 } else {
399 FlowStatsCollector collector = simpleCollectors.remove(dpid);
400 if (collector != null) {
401 collector.stop();
402 }
alshabibdfc7afb2014-10-21 20:13:27 -0700403 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700404 TableStatisticsCollector tsc = tableStatsCollectors.remove(dpid);
405 if (tsc != null) {
406 tsc.stop();
407 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700408 }
409
410 @Override
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700411 public void switchChanged(Dpid dpid) {
412 }
413
414 @Override
alshabib8f1cf4a2014-09-17 14:44:48 -0700415 public void portChanged(Dpid dpid, OFPortStatus status) {
jcc3d4e14a2015-04-21 11:32:05 +0800416 // TODO: Decide whether to evict flows internal store.
alshabib8f1cf4a2014-09-17 14:44:48 -0700417 }
418
419 @Override
420 public void handleMessage(Dpid dpid, OFMessage msg) {
alshabibda1644e2015-03-13 14:01:35 -0700421 OpenFlowSwitch sw = controller.getSwitch(dpid);
alshabib8f1cf4a2014-09-17 14:44:48 -0700422 switch (msg.getType()) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700423 case FLOW_REMOVED:
424 OFFlowRemoved removed = (OFFlowRemoved) msg;
alshabib6b5cfec2014-09-18 17:42:18 -0700425
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700426 FlowEntry fr = new FlowEntryBuilder(dpid, removed).build();
427 providerService.flowRemoved(fr);
ssyoon9030fbcd92015-08-17 10:42:07 +0900428
429 if (adaptiveFlowSampling) {
430 // Removed TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700431 NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid);
432 if (collector != null) {
433 collector.flowRemoved(fr);
434 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900435 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700436 break;
437 case STATS_REPLY:
438 if (((OFStatsReply) msg).getStatsType() == OFStatsType.FLOW) {
439 pushFlowMetrics(dpid, (OFFlowStatsReply) msg);
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700440 } else if (((OFStatsReply) msg).getStatsType() == OFStatsType.TABLE) {
441 pushTableStatistics(dpid, (OFTableStatsReply) msg);
sangho89bf6fb2015-02-09 09:33:13 -0800442 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700443 break;
444 case BARRIER_REPLY:
445 try {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700446 InternalCacheEntry entry = pendingBatches.getIfPresent(msg.getXid());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800447 if (entry != null) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700448 providerService
449 .batchOperationCompleted(msg.getXid(),
450 entry.completed());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800451 } else {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700452 log.warn("Received unknown Barrier Reply: {}",
453 msg.getXid());
454 }
455 } finally {
456 pendingBatches.invalidate(msg.getXid());
457 }
458 break;
459 case ERROR:
Thomas Vachuska3358af22015-05-19 18:40:34 -0700460 // TODO: This needs to get suppressed in a better way.
461 if (msg instanceof OFBadRequestErrorMsg &&
462 ((OFBadRequestErrorMsg) msg).getCode() == OFBadRequestCode.BAD_TYPE) {
463 log.debug("Received error message {} from {}", msg, dpid);
464 } else {
465 log.warn("Received error message {} from {}", msg, dpid);
466 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700467
468 OFErrorMsg error = (OFErrorMsg) msg;
469 if (error.getErrType() == OFErrorType.FLOW_MOD_FAILED) {
470 OFFlowModFailedErrorMsg fmFailed = (OFFlowModFailedErrorMsg) error;
471 if (fmFailed.getData().getParsedMessage().isPresent()) {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700472 OFMessage m = fmFailed.getData().getParsedMessage().get();
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700473 OFFlowMod fm = (OFFlowMod) m;
Thomas Vachuska3358af22015-05-19 18:40:34 -0700474 InternalCacheEntry entry =
475 pendingBatches.getIfPresent(msg.getXid());
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700476 if (entry != null) {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700477 entry.appendFailure(new FlowEntryBuilder(dpid, fm).build());
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700478 } else {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700479 log.error("No matching batch for this error: {}", error);
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700480 }
481 } else {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700482 // FIXME: Potentially add flowtracking to avoid this message.
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700483 log.error("Flow installation failed but switch didn't"
484 + " tell us which one.");
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800485 }
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800486 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900487
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700488 default:
489 log.debug("Unhandled message type: {}", msg.getType());
alshabib8f1cf4a2014-09-17 14:44:48 -0700490 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700491 }
492
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700493 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700494 public void receivedRoleReply(Dpid dpid, RoleState requested,
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800495 RoleState response) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700496 // Do nothing here for now.
497 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700498
sangho89bf6fb2015-02-09 09:33:13 -0800499 private void pushFlowMetrics(Dpid dpid, OFFlowStatsReply replies) {
alshabib64def642014-12-02 23:27:37 -0800500
alshabib54ce5892014-09-23 17:50:51 -0700501 DeviceId did = DeviceId.deviceId(Dpid.uri(dpid));
alshabib54ce5892014-09-23 17:50:51 -0700502
alshabib64def642014-12-02 23:27:37 -0800503 List<FlowEntry> flowEntries = replies.getEntries().stream()
alshabibbdcbb102015-04-22 14:16:38 -0700504 .map(entry -> new FlowEntryBuilder(dpid, entry).build())
alshabib64def642014-12-02 23:27:37 -0800505 .collect(Collectors.toList());
alshabib54ce5892014-09-23 17:50:51 -0700506
ssyoon9030fbcd92015-08-17 10:42:07 +0900507 if (adaptiveFlowSampling) {
508 NewAdaptiveFlowStatsCollector afsc = afsCollectors.get(dpid);
509
510 synchronized (afsc) {
511 if (afsc.getFlowMissingXid() != NewAdaptiveFlowStatsCollector.NO_FLOW_MISSING_XID) {
512 log.debug("OpenFlowRuleProvider:pushFlowMetrics, flowMissingXid={}, "
513 + "OFFlowStatsReply Xid={}, for {}",
514 afsc.getFlowMissingXid(), replies.getXid(), dpid);
515 }
516
517 // Check that OFFlowStatsReply Xid is same with the one of OFFlowStatsRequest?
518 if (afsc.getFlowMissingXid() != NewAdaptiveFlowStatsCollector.NO_FLOW_MISSING_XID) {
519 if (afsc.getFlowMissingXid() == replies.getXid()) {
520 // call entire flow stats update with flowMissing synchronization.
521 // used existing pushFlowMetrics
522 providerService.pushFlowMetrics(did, flowEntries);
523 }
524 // reset flowMissingXid to NO_FLOW_MISSING_XID
525 afsc.setFlowMissingXid(NewAdaptiveFlowStatsCollector.NO_FLOW_MISSING_XID);
526
527 } else {
528 // call individual flow stats update
529 providerService.pushFlowMetricsWithoutFlowMissing(did, flowEntries);
530 }
531
532 // Update TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
533 afsc.pushFlowMetrics(flowEntries);
534 }
535 } else {
536 // call existing entire flow stats update with flowMissing synchronization
537 providerService.pushFlowMetrics(did, flowEntries);
538 }
alshabib5c370ff2014-09-18 10:12:14 -0700539 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700540
541 private void pushTableStatistics(Dpid dpid, OFTableStatsReply replies) {
542
543 DeviceId did = DeviceId.deviceId(Dpid.uri(dpid));
544 List<TableStatisticsEntry> tableStatsEntries = replies.getEntries().stream()
545 .map(entry -> buildTableStatistics(did, entry))
546 .filter(Objects::nonNull)
547 .collect(Collectors.toList());
548 providerService.pushTableStatistics(did, tableStatsEntries);
549 }
550
551 private TableStatisticsEntry buildTableStatistics(DeviceId deviceId,
552 OFTableStatsEntry ofEntry) {
553 TableStatisticsEntry entry = null;
554 if (ofEntry != null) {
555 entry = new DefaultTableStatisticsEntry(deviceId,
556 ofEntry.getTableId().getValue(),
557 ofEntry.getActiveCount(),
558 ofEntry.getLookupCount().getValue(),
559 ofEntry.getMatchedCount().getValue());
560 }
561
562 return entry;
563
564 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700565 }
alshabib1cc04f72014-09-16 16:09:58 -0700566
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800567 /**
jcc3d4e14a2015-04-21 11:32:05 +0800568 * The internal cache entry holding the original request as well as
569 * accumulating the any failures along the way.
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700570 * <p/>
jcc3d4e14a2015-04-21 11:32:05 +0800571 * If this entry is evicted from the cache then the entire operation is
572 * considered failed. Otherwise, only the failures reported by the device
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800573 * will be propagated up.
574 */
575 private class InternalCacheEntry {
alshabib902d41b2014-10-07 16:52:05 -0700576
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800577 private final FlowRuleBatchOperation operation;
578 private final Set<FlowRule> failures = Sets.newConcurrentHashSet();
alshabib193525b2014-10-08 18:58:03 -0700579
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800580 public InternalCacheEntry(FlowRuleBatchOperation operation) {
581 this.operation = operation;
alshabib902d41b2014-10-07 16:52:05 -0700582 }
583
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800584 /**
585 * Appends a failed rule to the set of failed items.
jcc3d4e14a2015-04-21 11:32:05 +0800586 *
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800587 * @param rule the failed rule
588 */
589 public void appendFailure(FlowRule rule) {
590 failures.add(rule);
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800591 }
592
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800593 /**
594 * Fails the entire batch and returns the failed operation.
jcc3d4e14a2015-04-21 11:32:05 +0800595 *
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800596 * @return the failed operation
597 */
598 public CompletedBatchOperation failedCompletion() {
599 Set<FlowRule> fails = operation.getOperations().stream()
600 .map(op -> op.target()).collect(Collectors.toSet());
jcc3d4e14a2015-04-21 11:32:05 +0800601 return new CompletedBatchOperation(false,
602 Collections
603 .unmodifiableSet(fails),
604 operation.deviceId());
alshabib902d41b2014-10-07 16:52:05 -0700605 }
606
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800607 /**
608 * Returns the completed operation and whether the batch suceeded.
jcc3d4e14a2015-04-21 11:32:05 +0800609 *
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800610 * @return the completed operation
611 */
612 public CompletedBatchOperation completed() {
jcc3d4e14a2015-04-21 11:32:05 +0800613 return new CompletedBatchOperation(
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700614 failures.isEmpty(),
615 Collections
616 .unmodifiableSet(failures),
617 operation.deviceId());
alshabib902d41b2014-10-07 16:52:05 -0700618 }
alshabib902d41b2014-10-07 16:52:05 -0700619 }
alshabiba68eb962014-09-24 20:34:13 -0700620
alshabib1cc04f72014-09-16 16:09:58 -0700621}