blob: a3a0738ef7552ac406b710c9ca6ee83127c26fb9 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present 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;
alshabib1cc04f72014-09-16 16:09:58 -070024import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Deactivate;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070027import org.apache.felix.scr.annotations.Modified;
28import org.apache.felix.scr.annotations.Property;
alshabib1cc04f72014-09-16 16:09:58 -070029import org.apache.felix.scr.annotations.Reference;
30import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070031import org.onosproject.cfg.ComponentConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.core.ApplicationId;
33import org.onosproject.net.DeviceId;
Jonathan Hart3c259162015-10-21 21:31:19 -070034import org.onosproject.net.driver.DriverService;
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 Vachuska95caba32016-04-04 10:42:05 -070056import org.onosproject.provider.of.flow.util.FlowEntryBuilder;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070057import org.osgi.service.component.ComponentContext;
Thomas Vachuska3358af22015-05-19 18:40:34 -070058import org.projectfloodlight.openflow.protocol.OFBadRequestCode;
alshabib902d41b2014-10-07 16:52:05 -070059import org.projectfloodlight.openflow.protocol.OFBarrierRequest;
60import org.projectfloodlight.openflow.protocol.OFErrorMsg;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080061import org.projectfloodlight.openflow.protocol.OFErrorType;
alshabib193525b2014-10-08 18:58:03 -070062import org.projectfloodlight.openflow.protocol.OFFlowMod;
alshabib8f1cf4a2014-09-17 14:44:48 -070063import org.projectfloodlight.openflow.protocol.OFFlowRemoved;
alshabib5c370ff2014-09-18 10:12:14 -070064import org.projectfloodlight.openflow.protocol.OFFlowStatsReply;
alshabib8f1cf4a2014-09-17 14:44:48 -070065import org.projectfloodlight.openflow.protocol.OFMessage;
66import org.projectfloodlight.openflow.protocol.OFPortStatus;
alshabib5c370ff2014-09-18 10:12:14 -070067import org.projectfloodlight.openflow.protocol.OFStatsReply;
sangho89bf6fb2015-02-09 09:33:13 -080068import org.projectfloodlight.openflow.protocol.OFStatsType;
Jonathan Hart3c259162015-10-21 21:31:19 -070069import org.projectfloodlight.openflow.protocol.OFTableStatsEntry;
70import org.projectfloodlight.openflow.protocol.OFTableStatsReply;
Thomas Vachuska3358af22015-05-19 18:40:34 -070071import org.projectfloodlight.openflow.protocol.errormsg.OFBadRequestErrorMsg;
alshabib193525b2014-10-08 18:58:03 -070072import org.projectfloodlight.openflow.protocol.errormsg.OFFlowModFailedErrorMsg;
alshabib1cc04f72014-09-16 16:09:58 -070073import org.slf4j.Logger;
74
Thomas Vachuska75aaa672015-04-29 12:24:43 -070075import java.util.Collections;
76import java.util.Dictionary;
77import java.util.List;
78import java.util.Map;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070079import java.util.Objects;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070080import java.util.Optional;
81import java.util.Set;
82import java.util.Timer;
83import java.util.concurrent.TimeUnit;
84import java.util.stream.Collectors;
85
ssyoon9030fbcd92015-08-17 10:42:07 +090086import static com.google.common.base.Preconditions.checkNotNull;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070087import static com.google.common.base.Strings.isNullOrEmpty;
88import static org.onlab.util.Tools.get;
89import static org.slf4j.LoggerFactory.getLogger;
alshabibeec3a062014-09-17 18:01:26 -070090
alshabib1cc04f72014-09-16 16:09:58 -070091/**
jcc3d4e14a2015-04-21 11:32:05 +080092 * Provider which uses an OpenFlow controller to detect network end-station
93 * hosts.
alshabib1cc04f72014-09-16 16:09:58 -070094 */
95@Component(immediate = true)
jcc3d4e14a2015-04-21 11:32:05 +080096public class OpenFlowRuleProvider extends AbstractProvider
97 implements FlowRuleProvider {
alshabib1cc04f72014-09-16 16:09:58 -070098
99 private final Logger log = getLogger(getClass());
100
101 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
102 protected FlowRuleProviderRegistry providerRegistry;
103
104 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
105 protected OpenFlowController controller;
106
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700107 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
108 protected ComponentConfigService cfgService;
109
Jonathan Hart3c259162015-10-21 21:31:19 -0700110 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
111 protected DriverService driverService;
112
ssyoon9030fbcd92015-08-17 10:42:07 +0900113 private static final int DEFAULT_POLL_FREQUENCY = 5;
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700114 @Property(name = "flowPollFrequency", intValue = DEFAULT_POLL_FREQUENCY,
115 label = "Frequency (in seconds) for polling flow statistics")
116 private int flowPollFrequency = DEFAULT_POLL_FREQUENCY;
117
Madan Jampani7a3ba962016-04-07 20:16:16 -0700118 private static final boolean DEFAULT_ADAPTIVE_FLOW_SAMPLING = false;
ssyoon9030fbcd92015-08-17 10:42:07 +0900119 @Property(name = "adaptiveFlowSampling", boolValue = DEFAULT_ADAPTIVE_FLOW_SAMPLING,
120 label = "Adaptive Flow Sampling is on or off")
121 private boolean adaptiveFlowSampling = DEFAULT_ADAPTIVE_FLOW_SAMPLING;
122
alshabib1cc04f72014-09-16 16:09:58 -0700123 private FlowRuleProviderService providerService;
124
alshabibeec3a062014-09-17 18:01:26 -0700125 private final InternalFlowProvider listener = new InternalFlowProvider();
126
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800127 private Cache<Long, InternalCacheEntry> pendingBatches;
alshabib193525b2014-10-08 18:58:03 -0700128
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700129 private final Timer timer = new Timer("onos-openflow-collector");
ssyoon9030fbcd92015-08-17 10:42:07 +0900130 private final Map<Dpid, FlowStatsCollector> simpleCollectors = Maps.newHashMap();
131
132 // NewAdaptiveFlowStatsCollector Set
133 private final Map<Dpid, NewAdaptiveFlowStatsCollector> afsCollectors = Maps.newHashMap();
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700134 private final Map<Dpid, TableStatisticsCollector> tableStatsCollectors = Maps.newHashMap();
alshabib3d643ec2014-10-22 18:33:00 -0700135
alshabib1cc04f72014-09-16 16:09:58 -0700136 /**
137 * Creates an OpenFlow host provider.
138 */
139 public OpenFlowRuleProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800140 super(new ProviderId("of", "org.onosproject.provider.openflow"));
alshabib1cc04f72014-09-16 16:09:58 -0700141 }
142
143 @Activate
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700144 protected void activate(ComponentContext context) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700145 cfgService.registerProperties(getClass());
alshabib1cc04f72014-09-16 16:09:58 -0700146 providerService = providerRegistry.register(this);
alshabibeec3a062014-09-17 18:01:26 -0700147 controller.addListener(listener);
148 controller.addEventListener(listener);
alshabib3d643ec2014-10-22 18:33:00 -0700149
Antonio Marsico1c5ae1f2015-12-15 15:31:56 +0100150 modified(context);
151
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700152 pendingBatches = createBatchCache();
ssyoon9030fbcd92015-08-17 10:42:07 +0900153
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700154 createCollectors();
alshabib3d643ec2014-10-22 18:33:00 -0700155
ssyoon9030fbcd92015-08-17 10:42:07 +0900156 log.info("Started with flowPollFrequency = {}, adaptiveFlowSampling = {}",
157 flowPollFrequency, adaptiveFlowSampling);
alshabib1cc04f72014-09-16 16:09:58 -0700158 }
159
160 @Deactivate
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700161 protected void deactivate(ComponentContext context) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700162 cfgService.unregisterProperties(getClass(), false);
163 stopCollectors();
alshabib1cc04f72014-09-16 16:09:58 -0700164 providerRegistry.unregister(this);
165 providerService = null;
166
167 log.info("Stopped");
168 }
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800169
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700170 @Modified
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700171 protected void modified(ComponentContext context) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700172 Dictionary<?, ?> properties = context.getProperties();
173 int newFlowPollFrequency;
174 try {
175 String s = get(properties, "flowPollFrequency");
176 newFlowPollFrequency = isNullOrEmpty(s) ? flowPollFrequency : Integer.parseInt(s.trim());
177
178 } catch (NumberFormatException | ClassCastException e) {
179 newFlowPollFrequency = flowPollFrequency;
180 }
181
182 if (newFlowPollFrequency != flowPollFrequency) {
183 flowPollFrequency = newFlowPollFrequency;
184 adjustRate();
185 }
186
187 log.info("Settings: flowPollFrequency={}", flowPollFrequency);
ssyoon9030fbcd92015-08-17 10:42:07 +0900188
189 boolean newAdaptiveFlowSampling;
190 String s = get(properties, "adaptiveFlowSampling");
191 newAdaptiveFlowSampling = isNullOrEmpty(s) ? adaptiveFlowSampling : Boolean.parseBoolean(s.trim());
192
193 if (newAdaptiveFlowSampling != adaptiveFlowSampling) {
194 // stop previous collector
195 stopCollectors();
196 adaptiveFlowSampling = newAdaptiveFlowSampling;
197 // create new collectors
198 createCollectors();
199 }
200
201 log.info("Settings: adaptiveFlowSampling={}", adaptiveFlowSampling);
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700202 }
203
204 private Cache<Long, InternalCacheEntry> createBatchCache() {
205 return CacheBuilder.newBuilder()
206 .expireAfterWrite(10, TimeUnit.SECONDS)
207 .removalListener((RemovalNotification<Long, InternalCacheEntry> notification) -> {
208 if (notification.getCause() == RemovalCause.EXPIRED) {
209 providerService.batchOperationCompleted(notification.getKey(),
210 notification.getValue().failedCompletion());
211 }
212 }).build();
213 }
214
215 private void createCollectors() {
216 controller.getSwitches().forEach(this::createCollector);
217 }
218
219 private void createCollector(OpenFlowSwitch sw) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900220 if (adaptiveFlowSampling) {
221 // NewAdaptiveFlowStatsCollector Constructor
Charles Chan14967c22015-12-07 11:11:50 -0800222 NewAdaptiveFlowStatsCollector fsc =
223 new NewAdaptiveFlowStatsCollector(driverService, sw, flowPollFrequency);
ssyoon9030fbcd92015-08-17 10:42:07 +0900224 fsc.start();
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700225 stopCollectorIfNeeded(afsCollectors.put(new Dpid(sw.getId()), fsc));
ssyoon9030fbcd92015-08-17 10:42:07 +0900226 } else {
227 FlowStatsCollector fsc = new FlowStatsCollector(timer, sw, flowPollFrequency);
228 fsc.start();
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700229 stopCollectorIfNeeded(simpleCollectors.put(new Dpid(sw.getId()), fsc));
ssyoon9030fbcd92015-08-17 10:42:07 +0900230 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700231 TableStatisticsCollector tsc = new TableStatisticsCollector(timer, sw, flowPollFrequency);
232 tsc.start();
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700233 stopCollectorIfNeeded(tableStatsCollectors.put(new Dpid(sw.getId()), tsc));
234 }
235
236 private void stopCollectorIfNeeded(SwitchDataCollector collector) {
237 if (collector != null) {
238 collector.stop();
239 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700240 }
241
242 private void stopCollectors() {
ssyoon9030fbcd92015-08-17 10:42:07 +0900243 if (adaptiveFlowSampling) {
244 // NewAdaptiveFlowStatsCollector Destructor
245 afsCollectors.values().forEach(NewAdaptiveFlowStatsCollector::stop);
246 afsCollectors.clear();
247 } else {
248 simpleCollectors.values().forEach(FlowStatsCollector::stop);
249 simpleCollectors.clear();
250 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700251 tableStatsCollectors.values().forEach(TableStatisticsCollector::stop);
252 tableStatsCollectors.clear();
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700253 }
254
255 private void adjustRate() {
256 DefaultLoad.setPollInterval(flowPollFrequency);
ssyoon9030fbcd92015-08-17 10:42:07 +0900257 if (adaptiveFlowSampling) {
258 // NewAdaptiveFlowStatsCollector calAndPollInterval
259 afsCollectors.values().forEach(fsc -> fsc.adjustCalAndPollInterval(flowPollFrequency));
260 } else {
261 simpleCollectors.values().forEach(fsc -> fsc.adjustPollInterval(flowPollFrequency));
262 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700263 tableStatsCollectors.values().forEach(tsc -> tsc.adjustPollInterval(flowPollFrequency));
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700264 }
265
alshabib1cc04f72014-09-16 16:09:58 -0700266 @Override
267 public void applyFlowRule(FlowRule... flowRules) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800268 for (FlowRule flowRule : flowRules) {
269 applyRule(flowRule);
alshabib35edb1a2014-09-16 17:44:44 -0700270 }
alshabib1cc04f72014-09-16 16:09:58 -0700271 }
272
alshabib35edb1a2014-09-16 17:44:44 -0700273 private void applyRule(FlowRule flowRule) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900274 Dpid dpid = Dpid.dpid(flowRule.deviceId().uri());
275 OpenFlowSwitch sw = controller.getSwitch(dpid);
276
Ray Milkey0ae473d2016-04-04 10:56:47 -0700277 if (sw == null) {
278 return;
279 }
280
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -0700281 FlowRuleExtPayLoad flowRuleExtPayLoad = flowRule.payLoad();
282 if (hasPayload(flowRuleExtPayLoad)) {
283 OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad());
jcc3d4e14a2015-04-21 11:32:05 +0800284 sw.sendMsg(msg);
285 return;
286 }
alshabibbdcbb102015-04-22 14:16:38 -0700287 sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(),
Jonathan Hart3c259162015-10-21 21:31:19 -0700288 Optional.empty(), Optional.of(driverService)).buildFlowAdd());
ssyoon9030fbcd92015-08-17 10:42:07 +0900289
290 if (adaptiveFlowSampling) {
291 // Add TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700292 NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid);
293 if (collector != null) {
294 collector.addWithFlowRule(flowRule);
295 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900296 }
alshabib35edb1a2014-09-16 17:44:44 -0700297 }
298
alshabib1cc04f72014-09-16 16:09:58 -0700299 @Override
300 public void removeFlowRule(FlowRule... flowRules) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800301 for (FlowRule flowRule : flowRules) {
302 removeRule(flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700303 }
alshabib1cc04f72014-09-16 16:09:58 -0700304 }
305
alshabib219ebaa2014-09-22 15:41:24 -0700306 private void removeRule(FlowRule flowRule) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900307 Dpid dpid = Dpid.dpid(flowRule.deviceId().uri());
308 OpenFlowSwitch sw = controller.getSwitch(dpid);
309
Ray Milkey0ae473d2016-04-04 10:56:47 -0700310 if (sw == null) {
311 return;
312 }
313
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -0700314 FlowRuleExtPayLoad flowRuleExtPayLoad = flowRule.payLoad();
315 if (hasPayload(flowRuleExtPayLoad)) {
316 OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad());
jcc3d4e14a2015-04-21 11:32:05 +0800317 sw.sendMsg(msg);
318 return;
319 }
alshabibbdcbb102015-04-22 14:16:38 -0700320 sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(),
Jonathan Hart3c259162015-10-21 21:31:19 -0700321 Optional.empty(), Optional.of(driverService)).buildFlowDel());
ssyoon9030fbcd92015-08-17 10:42:07 +0900322
323 if (adaptiveFlowSampling) {
324 // Remove TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700325 NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid);
326 if (collector != null) {
327 collector.removeFlows(flowRule);
328 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900329 }
alshabib219ebaa2014-09-22 15:41:24 -0700330 }
331
alshabiba68eb962014-09-24 20:34:13 -0700332 @Override
333 public void removeRulesById(ApplicationId id, FlowRule... flowRules) {
334 // TODO: optimize using the ApplicationId
335 removeFlowRule(flowRules);
336 }
337
alshabib193525b2014-10-08 18:58:03 -0700338 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800339 public void executeBatch(FlowRuleBatchOperation batch) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900340 checkNotNull(batch);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800341
342 pendingBatches.put(batch.id(), new InternalCacheEntry(batch));
343
ssyoon9030fbcd92015-08-17 10:42:07 +0900344 Dpid dpid = Dpid.dpid(batch.deviceId().uri());
345 OpenFlowSwitch sw = controller.getSwitch(dpid);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800346 OFFlowMod mod;
alshabib193525b2014-10-08 18:58:03 -0700347 for (FlowRuleBatchEntry fbe : batch.getOperations()) {
jcc3d4e14a2015-04-21 11:32:05 +0800348 // flow is the third party privacy flow
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -0700349
350 FlowRuleExtPayLoad flowRuleExtPayLoad = fbe.target().payLoad();
351 if (hasPayload(flowRuleExtPayLoad)) {
352 OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad());
jcc3d4e14a2015-04-21 11:32:05 +0800353 sw.sendMsg(msg);
354 continue;
355 }
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700356 FlowModBuilder builder =
Jonathan Hart3c259162015-10-21 21:31:19 -0700357 FlowModBuilder.builder(fbe.target(), sw.factory(),
358 Optional.of(batch.id()), Optional.of(driverService));
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700359 NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid);
Sho SHIMIZUaba9d002015-01-29 14:51:04 -0800360 switch (fbe.operator()) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700361 case ADD:
362 mod = builder.buildFlowAdd();
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700363 if (adaptiveFlowSampling && collector != null) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900364 // Add TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700365 collector.addWithFlowRule(fbe.target());
ssyoon9030fbcd92015-08-17 10:42:07 +0900366 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700367 break;
368 case REMOVE:
369 mod = builder.buildFlowDel();
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700370 if (adaptiveFlowSampling && collector != null) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900371 // Remove TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700372 collector.removeFlows(fbe.target());
ssyoon9030fbcd92015-08-17 10:42:07 +0900373 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700374 break;
375 case MODIFY:
376 mod = builder.buildFlowMod();
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700377 if (adaptiveFlowSampling && collector != null) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900378 // Add or Update TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
379 // afsCollectors.get(dpid).addWithFlowRule(fbe.target()); //check if add is good or not
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700380 collector.addOrUpdateFlows((FlowEntry) fbe.target());
ssyoon9030fbcd92015-08-17 10:42:07 +0900381 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700382 break;
383 default:
384 log.error("Unsupported batch operation {}; skipping flowmod {}",
ssyoon9030fbcd92015-08-17 10:42:07 +0900385 fbe.operator(), fbe);
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700386 continue;
jcc3d4e14a2015-04-21 11:32:05 +0800387 }
Saurav Das3ea46622015-04-22 14:01:34 -0700388 sw.sendMsg(mod);
alshabib193525b2014-10-08 18:58:03 -0700389 }
jcc3d4e14a2015-04-21 11:32:05 +0800390 OFBarrierRequest.Builder builder = sw.factory().buildBarrierRequest()
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800391 .setXid(batch.id());
392 sw.sendMsg(builder.build());
alshabib193525b2014-10-08 18:58:03 -0700393 }
394
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -0700395 private boolean hasPayload(FlowRuleExtPayLoad flowRuleExtPayLoad) {
396 return flowRuleExtPayLoad != null &&
397 flowRuleExtPayLoad.payLoad() != null &&
398 flowRuleExtPayLoad.payLoad().length > 0;
399 }
400
alshabib8f1cf4a2014-09-17 14:44:48 -0700401 private class InternalFlowProvider
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800402 implements OpenFlowSwitchListener, OpenFlowEventListener {
alshabib8f1cf4a2014-09-17 14:44:48 -0700403
alshabib8f1cf4a2014-09-17 14:44:48 -0700404 @Override
405 public void switchAdded(Dpid dpid) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700406 createCollector(controller.getSwitch(dpid));
alshabib8f1cf4a2014-09-17 14:44:48 -0700407 }
408
409 @Override
410 public void switchRemoved(Dpid dpid) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900411 if (adaptiveFlowSampling) {
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700412 stopCollectorIfNeeded(afsCollectors.remove(dpid));
ssyoon9030fbcd92015-08-17 10:42:07 +0900413 } else {
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700414 stopCollectorIfNeeded(simpleCollectors.remove(dpid));
alshabibdfc7afb2014-10-21 20:13:27 -0700415 }
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700416 stopCollectorIfNeeded(tableStatsCollectors.remove(dpid));
alshabib8f1cf4a2014-09-17 14:44:48 -0700417 }
418
419 @Override
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700420 public void switchChanged(Dpid dpid) {
421 }
422
423 @Override
alshabib8f1cf4a2014-09-17 14:44:48 -0700424 public void portChanged(Dpid dpid, OFPortStatus status) {
jcc3d4e14a2015-04-21 11:32:05 +0800425 // TODO: Decide whether to evict flows internal store.
alshabib8f1cf4a2014-09-17 14:44:48 -0700426 }
427
428 @Override
429 public void handleMessage(Dpid dpid, OFMessage msg) {
Ray Milkeyada9e2d2016-04-05 16:42:35 -0700430 if (providerService == null) {
431 // We are shutting down, nothing to be done
432 return;
433 }
Jonathan Harte4e74f02016-03-03 12:57:40 -0800434 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
alshabib8f1cf4a2014-09-17 14:44:48 -0700435 switch (msg.getType()) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700436 case FLOW_REMOVED:
437 OFFlowRemoved removed = (OFFlowRemoved) msg;
alshabib6b5cfec2014-09-18 17:42:18 -0700438
Jonathan Harte4e74f02016-03-03 12:57:40 -0800439 FlowEntry fr = new FlowEntryBuilder(deviceId, removed, driverService).build();
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700440 providerService.flowRemoved(fr);
ssyoon9030fbcd92015-08-17 10:42:07 +0900441
442 if (adaptiveFlowSampling) {
443 // Removed TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700444 NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid);
445 if (collector != null) {
446 collector.flowRemoved(fr);
447 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900448 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700449 break;
450 case STATS_REPLY:
451 if (((OFStatsReply) msg).getStatsType() == OFStatsType.FLOW) {
452 pushFlowMetrics(dpid, (OFFlowStatsReply) msg);
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700453 } else if (((OFStatsReply) msg).getStatsType() == OFStatsType.TABLE) {
454 pushTableStatistics(dpid, (OFTableStatsReply) msg);
sangho89bf6fb2015-02-09 09:33:13 -0800455 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700456 break;
457 case BARRIER_REPLY:
458 try {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700459 InternalCacheEntry entry = pendingBatches.getIfPresent(msg.getXid());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800460 if (entry != null) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700461 providerService
462 .batchOperationCompleted(msg.getXid(),
463 entry.completed());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800464 } else {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700465 log.warn("Received unknown Barrier Reply: {}",
466 msg.getXid());
467 }
468 } finally {
469 pendingBatches.invalidate(msg.getXid());
470 }
471 break;
472 case ERROR:
Thomas Vachuska3358af22015-05-19 18:40:34 -0700473 // TODO: This needs to get suppressed in a better way.
474 if (msg instanceof OFBadRequestErrorMsg &&
475 ((OFBadRequestErrorMsg) msg).getCode() == OFBadRequestCode.BAD_TYPE) {
476 log.debug("Received error message {} from {}", msg, dpid);
477 } else {
478 log.warn("Received error message {} from {}", msg, dpid);
479 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700480
481 OFErrorMsg error = (OFErrorMsg) msg;
482 if (error.getErrType() == OFErrorType.FLOW_MOD_FAILED) {
483 OFFlowModFailedErrorMsg fmFailed = (OFFlowModFailedErrorMsg) error;
484 if (fmFailed.getData().getParsedMessage().isPresent()) {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700485 OFMessage m = fmFailed.getData().getParsedMessage().get();
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700486 OFFlowMod fm = (OFFlowMod) m;
Thomas Vachuska3358af22015-05-19 18:40:34 -0700487 InternalCacheEntry entry =
488 pendingBatches.getIfPresent(msg.getXid());
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700489 if (entry != null) {
Jonathan Harte4e74f02016-03-03 12:57:40 -0800490 entry.appendFailure(new FlowEntryBuilder(deviceId, fm, driverService).build());
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700491 } else {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700492 log.error("No matching batch for this error: {}", error);
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700493 }
494 } else {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700495 // FIXME: Potentially add flowtracking to avoid this message.
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700496 log.error("Flow installation failed but switch didn't"
497 + " tell us which one.");
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800498 }
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800499 }
Ray Milkey4fd3ceb2015-12-10 14:43:08 -0800500 break;
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700501 default:
502 log.debug("Unhandled message type: {}", msg.getType());
alshabib8f1cf4a2014-09-17 14:44:48 -0700503 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700504 }
505
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700506 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700507 public void receivedRoleReply(Dpid dpid, RoleState requested,
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800508 RoleState response) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700509 // Do nothing here for now.
510 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700511
sangho89bf6fb2015-02-09 09:33:13 -0800512 private void pushFlowMetrics(Dpid dpid, OFFlowStatsReply replies) {
alshabib64def642014-12-02 23:27:37 -0800513
alshabib54ce5892014-09-23 17:50:51 -0700514 DeviceId did = DeviceId.deviceId(Dpid.uri(dpid));
alshabib54ce5892014-09-23 17:50:51 -0700515
alshabib64def642014-12-02 23:27:37 -0800516 List<FlowEntry> flowEntries = replies.getEntries().stream()
Jonathan Harte4e74f02016-03-03 12:57:40 -0800517 .map(entry -> new FlowEntryBuilder(did, entry, driverService).build())
alshabib64def642014-12-02 23:27:37 -0800518 .collect(Collectors.toList());
alshabib54ce5892014-09-23 17:50:51 -0700519
ssyoon9030fbcd92015-08-17 10:42:07 +0900520 if (adaptiveFlowSampling) {
521 NewAdaptiveFlowStatsCollector afsc = afsCollectors.get(dpid);
522
523 synchronized (afsc) {
524 if (afsc.getFlowMissingXid() != NewAdaptiveFlowStatsCollector.NO_FLOW_MISSING_XID) {
525 log.debug("OpenFlowRuleProvider:pushFlowMetrics, flowMissingXid={}, "
526 + "OFFlowStatsReply Xid={}, for {}",
527 afsc.getFlowMissingXid(), replies.getXid(), dpid);
528 }
529
530 // Check that OFFlowStatsReply Xid is same with the one of OFFlowStatsRequest?
531 if (afsc.getFlowMissingXid() != NewAdaptiveFlowStatsCollector.NO_FLOW_MISSING_XID) {
532 if (afsc.getFlowMissingXid() == replies.getXid()) {
533 // call entire flow stats update with flowMissing synchronization.
534 // used existing pushFlowMetrics
535 providerService.pushFlowMetrics(did, flowEntries);
536 }
537 // reset flowMissingXid to NO_FLOW_MISSING_XID
538 afsc.setFlowMissingXid(NewAdaptiveFlowStatsCollector.NO_FLOW_MISSING_XID);
539
540 } else {
541 // call individual flow stats update
542 providerService.pushFlowMetricsWithoutFlowMissing(did, flowEntries);
543 }
544
545 // Update TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
546 afsc.pushFlowMetrics(flowEntries);
547 }
548 } else {
549 // call existing entire flow stats update with flowMissing synchronization
550 providerService.pushFlowMetrics(did, flowEntries);
551 }
alshabib5c370ff2014-09-18 10:12:14 -0700552 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700553
554 private void pushTableStatistics(Dpid dpid, OFTableStatsReply replies) {
555
556 DeviceId did = DeviceId.deviceId(Dpid.uri(dpid));
557 List<TableStatisticsEntry> tableStatsEntries = replies.getEntries().stream()
558 .map(entry -> buildTableStatistics(did, entry))
559 .filter(Objects::nonNull)
560 .collect(Collectors.toList());
561 providerService.pushTableStatistics(did, tableStatsEntries);
562 }
563
564 private TableStatisticsEntry buildTableStatistics(DeviceId deviceId,
565 OFTableStatsEntry ofEntry) {
566 TableStatisticsEntry entry = null;
567 if (ofEntry != null) {
568 entry = new DefaultTableStatisticsEntry(deviceId,
569 ofEntry.getTableId().getValue(),
570 ofEntry.getActiveCount(),
571 ofEntry.getLookupCount().getValue(),
572 ofEntry.getMatchedCount().getValue());
573 }
574
575 return entry;
576
577 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700578 }
alshabib1cc04f72014-09-16 16:09:58 -0700579
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800580 /**
jcc3d4e14a2015-04-21 11:32:05 +0800581 * The internal cache entry holding the original request as well as
582 * accumulating the any failures along the way.
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700583 * <p/>
jcc3d4e14a2015-04-21 11:32:05 +0800584 * If this entry is evicted from the cache then the entire operation is
585 * considered failed. Otherwise, only the failures reported by the device
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800586 * will be propagated up.
587 */
588 private class InternalCacheEntry {
alshabib902d41b2014-10-07 16:52:05 -0700589
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800590 private final FlowRuleBatchOperation operation;
591 private final Set<FlowRule> failures = Sets.newConcurrentHashSet();
alshabib193525b2014-10-08 18:58:03 -0700592
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800593 public InternalCacheEntry(FlowRuleBatchOperation operation) {
594 this.operation = operation;
alshabib902d41b2014-10-07 16:52:05 -0700595 }
596
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800597 /**
598 * Appends a failed rule to the set of failed items.
jcc3d4e14a2015-04-21 11:32:05 +0800599 *
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800600 * @param rule the failed rule
601 */
602 public void appendFailure(FlowRule rule) {
603 failures.add(rule);
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800604 }
605
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800606 /**
607 * Fails the entire batch and returns the failed operation.
jcc3d4e14a2015-04-21 11:32:05 +0800608 *
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800609 * @return the failed operation
610 */
611 public CompletedBatchOperation failedCompletion() {
612 Set<FlowRule> fails = operation.getOperations().stream()
613 .map(op -> op.target()).collect(Collectors.toSet());
jcc3d4e14a2015-04-21 11:32:05 +0800614 return new CompletedBatchOperation(false,
615 Collections
616 .unmodifiableSet(fails),
617 operation.deviceId());
alshabib902d41b2014-10-07 16:52:05 -0700618 }
619
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800620 /**
621 * Returns the completed operation and whether the batch suceeded.
jcc3d4e14a2015-04-21 11:32:05 +0800622 *
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800623 * @return the completed operation
624 */
625 public CompletedBatchOperation completed() {
jcc3d4e14a2015-04-21 11:32:05 +0800626 return new CompletedBatchOperation(
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700627 failures.isEmpty(),
628 Collections
629 .unmodifiableSet(failures),
630 operation.deviceId());
alshabib902d41b2014-10-07 16:52:05 -0700631 }
alshabib902d41b2014-10-07 16:52:05 -0700632 }
alshabiba68eb962014-09-24 20:34:13 -0700633
alshabib1cc04f72014-09-16 16:09:58 -0700634}