blob: 5a5b101bc63e2bdd45bac567a70a1677f9adc93d [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;
Madan Jampani84382b92016-06-22 08:26:49 -070022import com.google.common.collect.ImmutableSet;
23import com.google.common.collect.Lists;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070024import com.google.common.collect.Maps;
25import com.google.common.collect.Sets;
Madan Jampani84382b92016-06-22 08:26:49 -070026
alshabib1cc04f72014-09-16 16:09:58 -070027import org.apache.felix.scr.annotations.Activate;
28import org.apache.felix.scr.annotations.Component;
29import org.apache.felix.scr.annotations.Deactivate;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070030import org.apache.felix.scr.annotations.Modified;
31import org.apache.felix.scr.annotations.Property;
alshabib1cc04f72014-09-16 16:09:58 -070032import org.apache.felix.scr.annotations.Reference;
33import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070034import org.onosproject.cfg.ComponentConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.core.ApplicationId;
36import org.onosproject.net.DeviceId;
Jonathan Hart3c259162015-10-21 21:31:19 -070037import org.onosproject.net.driver.DriverService;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.flow.CompletedBatchOperation;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070039import org.onosproject.net.flow.DefaultTableStatisticsEntry;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.flow.FlowEntry;
41import org.onosproject.net.flow.FlowRule;
42import org.onosproject.net.flow.FlowRuleBatchEntry;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080043import org.onosproject.net.flow.FlowRuleBatchOperation;
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -070044import org.onosproject.net.flow.FlowRuleExtPayLoad;
Brian O'Connorabafb502014-12-02 22:26:20 -080045import org.onosproject.net.flow.FlowRuleProvider;
46import org.onosproject.net.flow.FlowRuleProviderRegistry;
47import org.onosproject.net.flow.FlowRuleProviderService;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070048import org.onosproject.net.flow.TableStatisticsEntry;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import org.onosproject.net.provider.AbstractProvider;
50import org.onosproject.net.provider.ProviderId;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070051import org.onosproject.net.statistic.DefaultLoad;
Brian O'Connorabafb502014-12-02 22:26:20 -080052import org.onosproject.openflow.controller.Dpid;
53import org.onosproject.openflow.controller.OpenFlowController;
54import org.onosproject.openflow.controller.OpenFlowEventListener;
55import org.onosproject.openflow.controller.OpenFlowSwitch;
56import org.onosproject.openflow.controller.OpenFlowSwitchListener;
57import org.onosproject.openflow.controller.RoleState;
jcc3d4e14a2015-04-21 11:32:05 +080058import org.onosproject.openflow.controller.ThirdPartyMessage;
Thomas Vachuska95caba32016-04-04 10:42:05 -070059import org.onosproject.provider.of.flow.util.FlowEntryBuilder;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070060import org.osgi.service.component.ComponentContext;
Thomas Vachuska3358af22015-05-19 18:40:34 -070061import org.projectfloodlight.openflow.protocol.OFBadRequestCode;
alshabib902d41b2014-10-07 16:52:05 -070062import org.projectfloodlight.openflow.protocol.OFBarrierRequest;
63import org.projectfloodlight.openflow.protocol.OFErrorMsg;
alshabib193525b2014-10-08 18:58:03 -070064import org.projectfloodlight.openflow.protocol.OFFlowMod;
alshabib8f1cf4a2014-09-17 14:44:48 -070065import org.projectfloodlight.openflow.protocol.OFFlowRemoved;
alshabib5c370ff2014-09-18 10:12:14 -070066import org.projectfloodlight.openflow.protocol.OFFlowStatsReply;
alshabib8f1cf4a2014-09-17 14:44:48 -070067import org.projectfloodlight.openflow.protocol.OFMessage;
68import org.projectfloodlight.openflow.protocol.OFPortStatus;
alshabib5c370ff2014-09-18 10:12:14 -070069import org.projectfloodlight.openflow.protocol.OFStatsReply;
sangho89bf6fb2015-02-09 09:33:13 -080070import org.projectfloodlight.openflow.protocol.OFStatsType;
Jonathan Hart3c259162015-10-21 21:31:19 -070071import org.projectfloodlight.openflow.protocol.OFTableStatsEntry;
72import org.projectfloodlight.openflow.protocol.OFTableStatsReply;
Prince Pereira788797e2016-08-10 11:24:14 +053073import org.projectfloodlight.openflow.protocol.errormsg.OFBadActionErrorMsg;
74import org.projectfloodlight.openflow.protocol.errormsg.OFBadInstructionErrorMsg;
75import org.projectfloodlight.openflow.protocol.errormsg.OFBadMatchErrorMsg;
Thomas Vachuska3358af22015-05-19 18:40:34 -070076import org.projectfloodlight.openflow.protocol.errormsg.OFBadRequestErrorMsg;
alshabib193525b2014-10-08 18:58:03 -070077import org.projectfloodlight.openflow.protocol.errormsg.OFFlowModFailedErrorMsg;
alshabib1cc04f72014-09-16 16:09:58 -070078import org.slf4j.Logger;
79
Thomas Vachuska75aaa672015-04-29 12:24:43 -070080import java.util.Collections;
81import java.util.Dictionary;
82import java.util.List;
83import java.util.Map;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070084import java.util.Objects;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070085import java.util.Optional;
86import java.util.Set;
87import java.util.Timer;
88import java.util.concurrent.TimeUnit;
89import java.util.stream.Collectors;
90
ssyoon9030fbcd92015-08-17 10:42:07 +090091import static com.google.common.base.Preconditions.checkNotNull;
Thomas Vachuska75aaa672015-04-29 12:24:43 -070092import static com.google.common.base.Strings.isNullOrEmpty;
93import static org.onlab.util.Tools.get;
94import static org.slf4j.LoggerFactory.getLogger;
alshabibeec3a062014-09-17 18:01:26 -070095
alshabib1cc04f72014-09-16 16:09:58 -070096/**
jcc3d4e14a2015-04-21 11:32:05 +080097 * Provider which uses an OpenFlow controller to detect network end-station
98 * hosts.
alshabib1cc04f72014-09-16 16:09:58 -070099 */
100@Component(immediate = true)
jcc3d4e14a2015-04-21 11:32:05 +0800101public class OpenFlowRuleProvider extends AbstractProvider
102 implements FlowRuleProvider {
alshabib1cc04f72014-09-16 16:09:58 -0700103
104 private final Logger log = getLogger(getClass());
105
106 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
107 protected FlowRuleProviderRegistry providerRegistry;
108
109 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
110 protected OpenFlowController controller;
111
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700112 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
113 protected ComponentConfigService cfgService;
114
Jonathan Hart3c259162015-10-21 21:31:19 -0700115 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
116 protected DriverService driverService;
117
ssyoon9030fbcd92015-08-17 10:42:07 +0900118 private static final int DEFAULT_POLL_FREQUENCY = 5;
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700119 @Property(name = "flowPollFrequency", intValue = DEFAULT_POLL_FREQUENCY,
120 label = "Frequency (in seconds) for polling flow statistics")
121 private int flowPollFrequency = DEFAULT_POLL_FREQUENCY;
122
Madan Jampani7a3ba962016-04-07 20:16:16 -0700123 private static final boolean DEFAULT_ADAPTIVE_FLOW_SAMPLING = false;
ssyoon9030fbcd92015-08-17 10:42:07 +0900124 @Property(name = "adaptiveFlowSampling", boolValue = DEFAULT_ADAPTIVE_FLOW_SAMPLING,
125 label = "Adaptive Flow Sampling is on or off")
126 private boolean adaptiveFlowSampling = DEFAULT_ADAPTIVE_FLOW_SAMPLING;
127
alshabib1cc04f72014-09-16 16:09:58 -0700128 private FlowRuleProviderService providerService;
129
alshabibeec3a062014-09-17 18:01:26 -0700130 private final InternalFlowProvider listener = new InternalFlowProvider();
131
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800132 private Cache<Long, InternalCacheEntry> pendingBatches;
alshabib193525b2014-10-08 18:58:03 -0700133
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700134 private final Timer timer = new Timer("onos-openflow-collector");
Madan Jampani6b266102016-06-23 00:56:36 -0700135 private final Map<Dpid, FlowStatsCollector> simpleCollectors = Maps.newConcurrentMap();
ssyoon9030fbcd92015-08-17 10:42:07 +0900136
137 // NewAdaptiveFlowStatsCollector Set
Madan Jampani6b266102016-06-23 00:56:36 -0700138 private final Map<Dpid, NewAdaptiveFlowStatsCollector> afsCollectors = Maps.newConcurrentMap();
139 private final Map<Dpid, TableStatisticsCollector> tableStatsCollectors = Maps.newConcurrentMap();
alshabib3d643ec2014-10-22 18:33:00 -0700140
alshabib1cc04f72014-09-16 16:09:58 -0700141 /**
142 * Creates an OpenFlow host provider.
143 */
144 public OpenFlowRuleProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800145 super(new ProviderId("of", "org.onosproject.provider.openflow"));
alshabib1cc04f72014-09-16 16:09:58 -0700146 }
147
148 @Activate
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700149 protected void activate(ComponentContext context) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700150 cfgService.registerProperties(getClass());
alshabib1cc04f72014-09-16 16:09:58 -0700151 providerService = providerRegistry.register(this);
alshabibeec3a062014-09-17 18:01:26 -0700152 controller.addListener(listener);
153 controller.addEventListener(listener);
alshabib3d643ec2014-10-22 18:33:00 -0700154
Antonio Marsico1c5ae1f2015-12-15 15:31:56 +0100155 modified(context);
156
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700157 pendingBatches = createBatchCache();
ssyoon9030fbcd92015-08-17 10:42:07 +0900158
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700159 createCollectors();
alshabib3d643ec2014-10-22 18:33:00 -0700160
ssyoon9030fbcd92015-08-17 10:42:07 +0900161 log.info("Started with flowPollFrequency = {}, adaptiveFlowSampling = {}",
162 flowPollFrequency, adaptiveFlowSampling);
alshabib1cc04f72014-09-16 16:09:58 -0700163 }
164
165 @Deactivate
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700166 protected void deactivate(ComponentContext context) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700167 cfgService.unregisterProperties(getClass(), false);
168 stopCollectors();
alshabib1cc04f72014-09-16 16:09:58 -0700169 providerRegistry.unregister(this);
170 providerService = null;
171
172 log.info("Stopped");
173 }
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800174
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700175 @Modified
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700176 protected void modified(ComponentContext context) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700177 Dictionary<?, ?> properties = context.getProperties();
178 int newFlowPollFrequency;
179 try {
180 String s = get(properties, "flowPollFrequency");
181 newFlowPollFrequency = isNullOrEmpty(s) ? flowPollFrequency : Integer.parseInt(s.trim());
182
183 } catch (NumberFormatException | ClassCastException e) {
184 newFlowPollFrequency = flowPollFrequency;
185 }
186
187 if (newFlowPollFrequency != flowPollFrequency) {
188 flowPollFrequency = newFlowPollFrequency;
189 adjustRate();
190 }
191
192 log.info("Settings: flowPollFrequency={}", flowPollFrequency);
ssyoon9030fbcd92015-08-17 10:42:07 +0900193
194 boolean newAdaptiveFlowSampling;
195 String s = get(properties, "adaptiveFlowSampling");
196 newAdaptiveFlowSampling = isNullOrEmpty(s) ? adaptiveFlowSampling : Boolean.parseBoolean(s.trim());
197
198 if (newAdaptiveFlowSampling != adaptiveFlowSampling) {
199 // stop previous collector
200 stopCollectors();
201 adaptiveFlowSampling = newAdaptiveFlowSampling;
202 // create new collectors
203 createCollectors();
204 }
205
206 log.info("Settings: adaptiveFlowSampling={}", adaptiveFlowSampling);
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700207 }
208
209 private Cache<Long, InternalCacheEntry> createBatchCache() {
210 return CacheBuilder.newBuilder()
211 .expireAfterWrite(10, TimeUnit.SECONDS)
212 .removalListener((RemovalNotification<Long, InternalCacheEntry> notification) -> {
213 if (notification.getCause() == RemovalCause.EXPIRED) {
214 providerService.batchOperationCompleted(notification.getKey(),
215 notification.getValue().failedCompletion());
216 }
217 }).build();
218 }
219
220 private void createCollectors() {
221 controller.getSwitches().forEach(this::createCollector);
222 }
223
224 private void createCollector(OpenFlowSwitch sw) {
Madan Jampani84382b92016-06-22 08:26:49 -0700225 checkNotNull(sw, "Null switch");
ssyoon9030fbcd92015-08-17 10:42:07 +0900226 if (adaptiveFlowSampling) {
227 // NewAdaptiveFlowStatsCollector Constructor
Charles Chan14967c22015-12-07 11:11:50 -0800228 NewAdaptiveFlowStatsCollector fsc =
229 new NewAdaptiveFlowStatsCollector(driverService, sw, flowPollFrequency);
ssyoon9030fbcd92015-08-17 10:42:07 +0900230 fsc.start();
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700231 stopCollectorIfNeeded(afsCollectors.put(new Dpid(sw.getId()), fsc));
ssyoon9030fbcd92015-08-17 10:42:07 +0900232 } else {
233 FlowStatsCollector fsc = new FlowStatsCollector(timer, sw, flowPollFrequency);
234 fsc.start();
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700235 stopCollectorIfNeeded(simpleCollectors.put(new Dpid(sw.getId()), fsc));
ssyoon9030fbcd92015-08-17 10:42:07 +0900236 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700237 TableStatisticsCollector tsc = new TableStatisticsCollector(timer, sw, flowPollFrequency);
238 tsc.start();
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700239 stopCollectorIfNeeded(tableStatsCollectors.put(new Dpid(sw.getId()), tsc));
240 }
241
242 private void stopCollectorIfNeeded(SwitchDataCollector collector) {
243 if (collector != null) {
244 collector.stop();
245 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700246 }
247
248 private void stopCollectors() {
ssyoon9030fbcd92015-08-17 10:42:07 +0900249 if (adaptiveFlowSampling) {
250 // NewAdaptiveFlowStatsCollector Destructor
251 afsCollectors.values().forEach(NewAdaptiveFlowStatsCollector::stop);
252 afsCollectors.clear();
253 } else {
254 simpleCollectors.values().forEach(FlowStatsCollector::stop);
255 simpleCollectors.clear();
256 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700257 tableStatsCollectors.values().forEach(TableStatisticsCollector::stop);
258 tableStatsCollectors.clear();
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700259 }
260
261 private void adjustRate() {
262 DefaultLoad.setPollInterval(flowPollFrequency);
ssyoon9030fbcd92015-08-17 10:42:07 +0900263 if (adaptiveFlowSampling) {
264 // NewAdaptiveFlowStatsCollector calAndPollInterval
265 afsCollectors.values().forEach(fsc -> fsc.adjustCalAndPollInterval(flowPollFrequency));
266 } else {
267 simpleCollectors.values().forEach(fsc -> fsc.adjustPollInterval(flowPollFrequency));
268 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700269 tableStatsCollectors.values().forEach(tsc -> tsc.adjustPollInterval(flowPollFrequency));
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700270 }
271
alshabib1cc04f72014-09-16 16:09:58 -0700272 @Override
273 public void applyFlowRule(FlowRule... flowRules) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800274 for (FlowRule flowRule : flowRules) {
275 applyRule(flowRule);
alshabib35edb1a2014-09-16 17:44:44 -0700276 }
alshabib1cc04f72014-09-16 16:09:58 -0700277 }
278
alshabib35edb1a2014-09-16 17:44:44 -0700279 private void applyRule(FlowRule flowRule) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900280 Dpid dpid = Dpid.dpid(flowRule.deviceId().uri());
281 OpenFlowSwitch sw = controller.getSwitch(dpid);
282
Ray Milkey0ae473d2016-04-04 10:56:47 -0700283 if (sw == null) {
284 return;
285 }
286
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -0700287 FlowRuleExtPayLoad flowRuleExtPayLoad = flowRule.payLoad();
288 if (hasPayload(flowRuleExtPayLoad)) {
289 OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad());
jcc3d4e14a2015-04-21 11:32:05 +0800290 sw.sendMsg(msg);
291 return;
292 }
alshabibbdcbb102015-04-22 14:16:38 -0700293 sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(),
Jonathan Hart3c259162015-10-21 21:31:19 -0700294 Optional.empty(), Optional.of(driverService)).buildFlowAdd());
ssyoon9030fbcd92015-08-17 10:42:07 +0900295
296 if (adaptiveFlowSampling) {
297 // Add TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700298 NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid);
299 if (collector != null) {
300 collector.addWithFlowRule(flowRule);
301 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900302 }
alshabib35edb1a2014-09-16 17:44:44 -0700303 }
304
alshabib1cc04f72014-09-16 16:09:58 -0700305 @Override
306 public void removeFlowRule(FlowRule... flowRules) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800307 for (FlowRule flowRule : flowRules) {
308 removeRule(flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700309 }
alshabib1cc04f72014-09-16 16:09:58 -0700310 }
311
alshabib219ebaa2014-09-22 15:41:24 -0700312 private void removeRule(FlowRule flowRule) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900313 Dpid dpid = Dpid.dpid(flowRule.deviceId().uri());
314 OpenFlowSwitch sw = controller.getSwitch(dpid);
315
Ray Milkey0ae473d2016-04-04 10:56:47 -0700316 if (sw == null) {
317 return;
318 }
319
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -0700320 FlowRuleExtPayLoad flowRuleExtPayLoad = flowRule.payLoad();
321 if (hasPayload(flowRuleExtPayLoad)) {
322 OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad());
jcc3d4e14a2015-04-21 11:32:05 +0800323 sw.sendMsg(msg);
324 return;
325 }
alshabibbdcbb102015-04-22 14:16:38 -0700326 sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(),
Jonathan Hart3c259162015-10-21 21:31:19 -0700327 Optional.empty(), Optional.of(driverService)).buildFlowDel());
ssyoon9030fbcd92015-08-17 10:42:07 +0900328
329 if (adaptiveFlowSampling) {
330 // Remove TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700331 NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid);
332 if (collector != null) {
333 collector.removeFlows(flowRule);
334 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900335 }
alshabib219ebaa2014-09-22 15:41:24 -0700336 }
337
alshabiba68eb962014-09-24 20:34:13 -0700338 @Override
339 public void removeRulesById(ApplicationId id, FlowRule... flowRules) {
340 // TODO: optimize using the ApplicationId
341 removeFlowRule(flowRules);
342 }
343
alshabib193525b2014-10-08 18:58:03 -0700344 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800345 public void executeBatch(FlowRuleBatchOperation batch) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900346 checkNotNull(batch);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800347
ssyoon9030fbcd92015-08-17 10:42:07 +0900348 Dpid dpid = Dpid.dpid(batch.deviceId().uri());
349 OpenFlowSwitch sw = controller.getSwitch(dpid);
Madan Jampani84382b92016-06-22 08:26:49 -0700350
351 // If switch no longer exists, simply return.
352 if (sw == null) {
353 Set<FlowRule> failures = ImmutableSet.copyOf(Lists.transform(batch.getOperations(), e -> e.target()));
354 providerService.batchOperationCompleted(batch.id(),
355 new CompletedBatchOperation(false, failures, batch.deviceId()));
356 return;
357 }
358 pendingBatches.put(batch.id(), new InternalCacheEntry(batch));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800359 OFFlowMod mod;
alshabib193525b2014-10-08 18:58:03 -0700360 for (FlowRuleBatchEntry fbe : batch.getOperations()) {
jcc3d4e14a2015-04-21 11:32:05 +0800361 // flow is the third party privacy flow
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -0700362
363 FlowRuleExtPayLoad flowRuleExtPayLoad = fbe.target().payLoad();
364 if (hasPayload(flowRuleExtPayLoad)) {
365 OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad());
jcc3d4e14a2015-04-21 11:32:05 +0800366 sw.sendMsg(msg);
367 continue;
368 }
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700369 FlowModBuilder builder =
Jonathan Hart3c259162015-10-21 21:31:19 -0700370 FlowModBuilder.builder(fbe.target(), sw.factory(),
371 Optional.of(batch.id()), Optional.of(driverService));
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700372 NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid);
Sho SHIMIZUaba9d002015-01-29 14:51:04 -0800373 switch (fbe.operator()) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700374 case ADD:
375 mod = builder.buildFlowAdd();
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700376 if (adaptiveFlowSampling && collector != null) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900377 // Add TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700378 collector.addWithFlowRule(fbe.target());
ssyoon9030fbcd92015-08-17 10:42:07 +0900379 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700380 break;
381 case REMOVE:
382 mod = builder.buildFlowDel();
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700383 if (adaptiveFlowSampling && collector != null) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900384 // Remove TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700385 collector.removeFlows(fbe.target());
ssyoon9030fbcd92015-08-17 10:42:07 +0900386 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700387 break;
388 case MODIFY:
389 mod = builder.buildFlowMod();
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700390 if (adaptiveFlowSampling && collector != null) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900391 // Add or Update TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
392 // afsCollectors.get(dpid).addWithFlowRule(fbe.target()); //check if add is good or not
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700393 collector.addOrUpdateFlows((FlowEntry) fbe.target());
ssyoon9030fbcd92015-08-17 10:42:07 +0900394 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700395 break;
396 default:
397 log.error("Unsupported batch operation {}; skipping flowmod {}",
ssyoon9030fbcd92015-08-17 10:42:07 +0900398 fbe.operator(), fbe);
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700399 continue;
jcc3d4e14a2015-04-21 11:32:05 +0800400 }
Saurav Das3ea46622015-04-22 14:01:34 -0700401 sw.sendMsg(mod);
alshabib193525b2014-10-08 18:58:03 -0700402 }
jcc3d4e14a2015-04-21 11:32:05 +0800403 OFBarrierRequest.Builder builder = sw.factory().buildBarrierRequest()
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800404 .setXid(batch.id());
405 sw.sendMsg(builder.build());
alshabib193525b2014-10-08 18:58:03 -0700406 }
407
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -0700408 private boolean hasPayload(FlowRuleExtPayLoad flowRuleExtPayLoad) {
409 return flowRuleExtPayLoad != null &&
410 flowRuleExtPayLoad.payLoad() != null &&
411 flowRuleExtPayLoad.payLoad().length > 0;
412 }
413
alshabib8f1cf4a2014-09-17 14:44:48 -0700414 private class InternalFlowProvider
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800415 implements OpenFlowSwitchListener, OpenFlowEventListener {
alshabib8f1cf4a2014-09-17 14:44:48 -0700416
alshabib8f1cf4a2014-09-17 14:44:48 -0700417 @Override
418 public void switchAdded(Dpid dpid) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700419 createCollector(controller.getSwitch(dpid));
alshabib8f1cf4a2014-09-17 14:44:48 -0700420 }
421
422 @Override
423 public void switchRemoved(Dpid dpid) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900424 if (adaptiveFlowSampling) {
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700425 stopCollectorIfNeeded(afsCollectors.remove(dpid));
ssyoon9030fbcd92015-08-17 10:42:07 +0900426 } else {
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700427 stopCollectorIfNeeded(simpleCollectors.remove(dpid));
alshabibdfc7afb2014-10-21 20:13:27 -0700428 }
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700429 stopCollectorIfNeeded(tableStatsCollectors.remove(dpid));
alshabib8f1cf4a2014-09-17 14:44:48 -0700430 }
431
432 @Override
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700433 public void switchChanged(Dpid dpid) {
434 }
435
436 @Override
alshabib8f1cf4a2014-09-17 14:44:48 -0700437 public void portChanged(Dpid dpid, OFPortStatus status) {
jcc3d4e14a2015-04-21 11:32:05 +0800438 // TODO: Decide whether to evict flows internal store.
alshabib8f1cf4a2014-09-17 14:44:48 -0700439 }
440
441 @Override
442 public void handleMessage(Dpid dpid, OFMessage msg) {
Ray Milkeyada9e2d2016-04-05 16:42:35 -0700443 if (providerService == null) {
444 // We are shutting down, nothing to be done
445 return;
446 }
Jonathan Harte4e74f02016-03-03 12:57:40 -0800447 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
alshabib8f1cf4a2014-09-17 14:44:48 -0700448 switch (msg.getType()) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700449 case FLOW_REMOVED:
450 OFFlowRemoved removed = (OFFlowRemoved) msg;
alshabib6b5cfec2014-09-18 17:42:18 -0700451
Jonathan Harte4e74f02016-03-03 12:57:40 -0800452 FlowEntry fr = new FlowEntryBuilder(deviceId, removed, driverService).build();
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700453 providerService.flowRemoved(fr);
ssyoon9030fbcd92015-08-17 10:42:07 +0900454
455 if (adaptiveFlowSampling) {
456 // Removed TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700457 NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid);
458 if (collector != null) {
459 collector.flowRemoved(fr);
460 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900461 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700462 break;
463 case STATS_REPLY:
464 if (((OFStatsReply) msg).getStatsType() == OFStatsType.FLOW) {
465 pushFlowMetrics(dpid, (OFFlowStatsReply) msg);
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700466 } else if (((OFStatsReply) msg).getStatsType() == OFStatsType.TABLE) {
467 pushTableStatistics(dpid, (OFTableStatsReply) msg);
sangho89bf6fb2015-02-09 09:33:13 -0800468 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700469 break;
470 case BARRIER_REPLY:
471 try {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700472 InternalCacheEntry entry = pendingBatches.getIfPresent(msg.getXid());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800473 if (entry != null) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700474 providerService
475 .batchOperationCompleted(msg.getXid(),
476 entry.completed());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800477 } else {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700478 log.warn("Received unknown Barrier Reply: {}",
479 msg.getXid());
480 }
481 } finally {
482 pendingBatches.invalidate(msg.getXid());
483 }
484 break;
485 case ERROR:
Thomas Vachuska3358af22015-05-19 18:40:34 -0700486 // TODO: This needs to get suppressed in a better way.
487 if (msg instanceof OFBadRequestErrorMsg &&
488 ((OFBadRequestErrorMsg) msg).getCode() == OFBadRequestCode.BAD_TYPE) {
489 log.debug("Received error message {} from {}", msg, dpid);
490 } else {
491 log.warn("Received error message {} from {}", msg, dpid);
492 }
Prince Pereira788797e2016-08-10 11:24:14 +0530493 handleErrorMsg(deviceId, msg);
Ray Milkey4fd3ceb2015-12-10 14:43:08 -0800494 break;
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700495 default:
496 log.debug("Unhandled message type: {}", msg.getType());
alshabib8f1cf4a2014-09-17 14:44:48 -0700497 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700498 }
499
Prince Pereira788797e2016-08-10 11:24:14 +0530500 private void handleErrorMsg(DeviceId deviceId, OFMessage msg) {
501 OFErrorMsg error = (OFErrorMsg) msg;
502 OFMessage ofMessage = null;
503 switch (error.getErrType()) {
504 case BAD_ACTION:
505 OFBadActionErrorMsg baErrorMsg = (OFBadActionErrorMsg) error;
506 if (baErrorMsg.getData().getParsedMessage().isPresent()) {
507 ofMessage = baErrorMsg.getData().getParsedMessage().get();
508 }
509 break;
510 case BAD_INSTRUCTION:
511 OFBadInstructionErrorMsg biErrorMsg = (OFBadInstructionErrorMsg) error;
512 if (biErrorMsg.getData().getParsedMessage().isPresent()) {
513 ofMessage = biErrorMsg.getData().getParsedMessage().get();
514 }
515 break;
516 case BAD_MATCH:
517 OFBadMatchErrorMsg bmErrorMsg = (OFBadMatchErrorMsg) error;
518 if (bmErrorMsg.getData().getParsedMessage().isPresent()) {
519 ofMessage = bmErrorMsg.getData().getParsedMessage().get();
520 }
521 break;
522 case FLOW_MOD_FAILED:
523 OFFlowModFailedErrorMsg fmFailed = (OFFlowModFailedErrorMsg) error;
524 if (fmFailed.getData().getParsedMessage().isPresent()) {
525 ofMessage = fmFailed.getData().getParsedMessage().get();
526 }
527 break;
528 default:
529 // Do nothing.
530 return;
531 }
532 if (ofMessage != null) {
533 InternalCacheEntry entry =
534 pendingBatches.getIfPresent(msg.getXid());
535 if (entry != null) {
536 OFFlowMod ofFlowMod = (OFFlowMod) ofMessage;
537 entry.appendFailure(new FlowEntryBuilder(deviceId, ofFlowMod, driverService).build());
538 } else {
539 log.error("No matching batch for this error: {}", error);
540 }
541 } else {
542 log.error("Flow installation failed but switch didn't"
543 + " tell us which one.");
544 }
545 }
546
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700547 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700548 public void receivedRoleReply(Dpid dpid, RoleState requested,
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800549 RoleState response) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700550 // Do nothing here for now.
551 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700552
sangho89bf6fb2015-02-09 09:33:13 -0800553 private void pushFlowMetrics(Dpid dpid, OFFlowStatsReply replies) {
alshabib64def642014-12-02 23:27:37 -0800554
alshabib54ce5892014-09-23 17:50:51 -0700555 DeviceId did = DeviceId.deviceId(Dpid.uri(dpid));
alshabib54ce5892014-09-23 17:50:51 -0700556
alshabib64def642014-12-02 23:27:37 -0800557 List<FlowEntry> flowEntries = replies.getEntries().stream()
Jonathan Harte4e74f02016-03-03 12:57:40 -0800558 .map(entry -> new FlowEntryBuilder(did, entry, driverService).build())
alshabib64def642014-12-02 23:27:37 -0800559 .collect(Collectors.toList());
alshabib54ce5892014-09-23 17:50:51 -0700560
ssyoon9030fbcd92015-08-17 10:42:07 +0900561 if (adaptiveFlowSampling) {
562 NewAdaptiveFlowStatsCollector afsc = afsCollectors.get(dpid);
563
564 synchronized (afsc) {
565 if (afsc.getFlowMissingXid() != NewAdaptiveFlowStatsCollector.NO_FLOW_MISSING_XID) {
566 log.debug("OpenFlowRuleProvider:pushFlowMetrics, flowMissingXid={}, "
567 + "OFFlowStatsReply Xid={}, for {}",
568 afsc.getFlowMissingXid(), replies.getXid(), dpid);
569 }
570
571 // Check that OFFlowStatsReply Xid is same with the one of OFFlowStatsRequest?
572 if (afsc.getFlowMissingXid() != NewAdaptiveFlowStatsCollector.NO_FLOW_MISSING_XID) {
573 if (afsc.getFlowMissingXid() == replies.getXid()) {
574 // call entire flow stats update with flowMissing synchronization.
575 // used existing pushFlowMetrics
576 providerService.pushFlowMetrics(did, flowEntries);
577 }
578 // reset flowMissingXid to NO_FLOW_MISSING_XID
579 afsc.setFlowMissingXid(NewAdaptiveFlowStatsCollector.NO_FLOW_MISSING_XID);
580
581 } else {
582 // call individual flow stats update
583 providerService.pushFlowMetricsWithoutFlowMissing(did, flowEntries);
584 }
585
586 // Update TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
587 afsc.pushFlowMetrics(flowEntries);
588 }
589 } else {
590 // call existing entire flow stats update with flowMissing synchronization
591 providerService.pushFlowMetrics(did, flowEntries);
592 }
alshabib5c370ff2014-09-18 10:12:14 -0700593 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700594
595 private void pushTableStatistics(Dpid dpid, OFTableStatsReply replies) {
596
597 DeviceId did = DeviceId.deviceId(Dpid.uri(dpid));
598 List<TableStatisticsEntry> tableStatsEntries = replies.getEntries().stream()
599 .map(entry -> buildTableStatistics(did, entry))
600 .filter(Objects::nonNull)
601 .collect(Collectors.toList());
602 providerService.pushTableStatistics(did, tableStatsEntries);
603 }
604
605 private TableStatisticsEntry buildTableStatistics(DeviceId deviceId,
606 OFTableStatsEntry ofEntry) {
607 TableStatisticsEntry entry = null;
608 if (ofEntry != null) {
609 entry = new DefaultTableStatisticsEntry(deviceId,
610 ofEntry.getTableId().getValue(),
611 ofEntry.getActiveCount(),
612 ofEntry.getLookupCount().getValue(),
613 ofEntry.getMatchedCount().getValue());
614 }
615
616 return entry;
617
618 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700619 }
alshabib1cc04f72014-09-16 16:09:58 -0700620
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800621 /**
jcc3d4e14a2015-04-21 11:32:05 +0800622 * The internal cache entry holding the original request as well as
623 * accumulating the any failures along the way.
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700624 * <p/>
jcc3d4e14a2015-04-21 11:32:05 +0800625 * If this entry is evicted from the cache then the entire operation is
626 * considered failed. Otherwise, only the failures reported by the device
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800627 * will be propagated up.
628 */
629 private class InternalCacheEntry {
alshabib902d41b2014-10-07 16:52:05 -0700630
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800631 private final FlowRuleBatchOperation operation;
632 private final Set<FlowRule> failures = Sets.newConcurrentHashSet();
alshabib193525b2014-10-08 18:58:03 -0700633
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800634 public InternalCacheEntry(FlowRuleBatchOperation operation) {
635 this.operation = operation;
alshabib902d41b2014-10-07 16:52:05 -0700636 }
637
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800638 /**
639 * Appends a failed rule to the set of failed items.
jcc3d4e14a2015-04-21 11:32:05 +0800640 *
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800641 * @param rule the failed rule
642 */
643 public void appendFailure(FlowRule rule) {
644 failures.add(rule);
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800645 }
646
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800647 /**
648 * Fails the entire batch and returns the failed operation.
jcc3d4e14a2015-04-21 11:32:05 +0800649 *
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800650 * @return the failed operation
651 */
652 public CompletedBatchOperation failedCompletion() {
653 Set<FlowRule> fails = operation.getOperations().stream()
654 .map(op -> op.target()).collect(Collectors.toSet());
jcc3d4e14a2015-04-21 11:32:05 +0800655 return new CompletedBatchOperation(false,
656 Collections
657 .unmodifiableSet(fails),
658 operation.deviceId());
alshabib902d41b2014-10-07 16:52:05 -0700659 }
660
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800661 /**
662 * Returns the completed operation and whether the batch suceeded.
jcc3d4e14a2015-04-21 11:32:05 +0800663 *
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800664 * @return the completed operation
665 */
666 public CompletedBatchOperation completed() {
jcc3d4e14a2015-04-21 11:32:05 +0800667 return new CompletedBatchOperation(
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700668 failures.isEmpty(),
669 Collections
670 .unmodifiableSet(failures),
671 operation.deviceId());
alshabib902d41b2014-10-07 16:52:05 -0700672 }
alshabib902d41b2014-10-07 16:52:05 -0700673 }
alshabiba68eb962014-09-24 20:34:13 -0700674
alshabib1cc04f72014-09-16 16:09:58 -0700675}