blob: 6469cdaf417abfb46b5220be794bce742eb4f347 [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;
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 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;
alshabib8f1cf4a2014-09-17 14:44:48 -070064import org.projectfloodlight.openflow.protocol.OFMessage;
65import org.projectfloodlight.openflow.protocol.OFPortStatus;
alshabib5c370ff2014-09-18 10:12:14 -070066import org.projectfloodlight.openflow.protocol.OFStatsReply;
sangho89bf6fb2015-02-09 09:33:13 -080067import org.projectfloodlight.openflow.protocol.OFStatsType;
Jonathan Hart3c259162015-10-21 21:31:19 -070068import org.projectfloodlight.openflow.protocol.OFTableStatsEntry;
69import org.projectfloodlight.openflow.protocol.OFTableStatsReply;
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
Jonathan Hart3c259162015-10-21 21:31:19 -0700109 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
110 protected DriverService driverService;
111
ssyoon9030fbcd92015-08-17 10:42:07 +0900112 private static final int DEFAULT_POLL_FREQUENCY = 5;
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700113 @Property(name = "flowPollFrequency", intValue = DEFAULT_POLL_FREQUENCY,
114 label = "Frequency (in seconds) for polling flow statistics")
115 private int flowPollFrequency = DEFAULT_POLL_FREQUENCY;
116
ssyoon9030fbcd92015-08-17 10:42:07 +0900117 private static final boolean DEFAULT_ADAPTIVE_FLOW_SAMPLING = true;
118 @Property(name = "adaptiveFlowSampling", boolValue = DEFAULT_ADAPTIVE_FLOW_SAMPLING,
119 label = "Adaptive Flow Sampling is on or off")
120 private boolean adaptiveFlowSampling = DEFAULT_ADAPTIVE_FLOW_SAMPLING;
121
alshabib1cc04f72014-09-16 16:09:58 -0700122 private FlowRuleProviderService providerService;
123
alshabibeec3a062014-09-17 18:01:26 -0700124 private final InternalFlowProvider listener = new InternalFlowProvider();
125
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800126 private Cache<Long, InternalCacheEntry> pendingBatches;
alshabib193525b2014-10-08 18:58:03 -0700127
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700128 private final Timer timer = new Timer("onos-openflow-collector");
ssyoon9030fbcd92015-08-17 10:42:07 +0900129 private final Map<Dpid, FlowStatsCollector> simpleCollectors = Maps.newHashMap();
130
131 // NewAdaptiveFlowStatsCollector Set
132 private final Map<Dpid, NewAdaptiveFlowStatsCollector> afsCollectors = Maps.newHashMap();
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700133 private final Map<Dpid, FlowStatsCollector> collectors = Maps.newHashMap();
134 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 Vachuska75aaa672015-04-29 12:24:43 -0700144 public void activate(ComponentContext context) {
145 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 Vachuska75aaa672015-04-29 12:24:43 -0700161 public void deactivate(ComponentContext context) {
162 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
171 public void modified(ComponentContext context) {
172 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
222 NewAdaptiveFlowStatsCollector fsc = new NewAdaptiveFlowStatsCollector(sw, flowPollFrequency);
223 fsc.start();
224 afsCollectors.put(new Dpid(sw.getId()), fsc);
225 } else {
226 FlowStatsCollector fsc = new FlowStatsCollector(timer, sw, flowPollFrequency);
227 fsc.start();
228 simpleCollectors.put(new Dpid(sw.getId()), fsc);
229 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700230 TableStatisticsCollector tsc = new TableStatisticsCollector(timer, sw, flowPollFrequency);
231 tsc.start();
232 tableStatsCollectors.put(new Dpid(sw.getId()), tsc);
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700233 }
234
235 private void stopCollectors() {
ssyoon9030fbcd92015-08-17 10:42:07 +0900236 if (adaptiveFlowSampling) {
237 // NewAdaptiveFlowStatsCollector Destructor
238 afsCollectors.values().forEach(NewAdaptiveFlowStatsCollector::stop);
239 afsCollectors.clear();
240 } else {
241 simpleCollectors.values().forEach(FlowStatsCollector::stop);
242 simpleCollectors.clear();
243 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700244 tableStatsCollectors.values().forEach(TableStatisticsCollector::stop);
245 tableStatsCollectors.clear();
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700246 }
247
248 private void adjustRate() {
249 DefaultLoad.setPollInterval(flowPollFrequency);
ssyoon9030fbcd92015-08-17 10:42:07 +0900250 if (adaptiveFlowSampling) {
251 // NewAdaptiveFlowStatsCollector calAndPollInterval
252 afsCollectors.values().forEach(fsc -> fsc.adjustCalAndPollInterval(flowPollFrequency));
253 } else {
254 simpleCollectors.values().forEach(fsc -> fsc.adjustPollInterval(flowPollFrequency));
255 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700256 tableStatsCollectors.values().forEach(tsc -> tsc.adjustPollInterval(flowPollFrequency));
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700257 }
258
alshabib1cc04f72014-09-16 16:09:58 -0700259 @Override
260 public void applyFlowRule(FlowRule... flowRules) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800261 for (FlowRule flowRule : flowRules) {
262 applyRule(flowRule);
alshabib35edb1a2014-09-16 17:44:44 -0700263 }
alshabib1cc04f72014-09-16 16:09:58 -0700264 }
265
alshabib35edb1a2014-09-16 17:44:44 -0700266 private void applyRule(FlowRule flowRule) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900267 Dpid dpid = Dpid.dpid(flowRule.deviceId().uri());
268 OpenFlowSwitch sw = controller.getSwitch(dpid);
269
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -0700270 FlowRuleExtPayLoad flowRuleExtPayLoad = flowRule.payLoad();
271 if (hasPayload(flowRuleExtPayLoad)) {
272 OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad());
jcc3d4e14a2015-04-21 11:32:05 +0800273 sw.sendMsg(msg);
274 return;
275 }
alshabibbdcbb102015-04-22 14:16:38 -0700276 sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(),
Jonathan Hart3c259162015-10-21 21:31:19 -0700277 Optional.empty(), Optional.of(driverService)).buildFlowAdd());
ssyoon9030fbcd92015-08-17 10:42:07 +0900278
279 if (adaptiveFlowSampling) {
280 // Add TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700281 NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid);
282 if (collector != null) {
283 collector.addWithFlowRule(flowRule);
284 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900285 }
alshabib35edb1a2014-09-16 17:44:44 -0700286 }
287
alshabib1cc04f72014-09-16 16:09:58 -0700288 @Override
289 public void removeFlowRule(FlowRule... flowRules) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800290 for (FlowRule flowRule : flowRules) {
291 removeRule(flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700292 }
alshabib1cc04f72014-09-16 16:09:58 -0700293 }
294
alshabib219ebaa2014-09-22 15:41:24 -0700295 private void removeRule(FlowRule flowRule) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900296 Dpid dpid = Dpid.dpid(flowRule.deviceId().uri());
297 OpenFlowSwitch sw = controller.getSwitch(dpid);
298
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -0700299 FlowRuleExtPayLoad flowRuleExtPayLoad = flowRule.payLoad();
300 if (hasPayload(flowRuleExtPayLoad)) {
301 OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad());
jcc3d4e14a2015-04-21 11:32:05 +0800302 sw.sendMsg(msg);
303 return;
304 }
alshabibbdcbb102015-04-22 14:16:38 -0700305 sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(),
Jonathan Hart3c259162015-10-21 21:31:19 -0700306 Optional.empty(), Optional.of(driverService)).buildFlowDel());
ssyoon9030fbcd92015-08-17 10:42:07 +0900307
308 if (adaptiveFlowSampling) {
309 // Remove TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700310 NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid);
311 if (collector != null) {
312 collector.removeFlows(flowRule);
313 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900314 }
alshabib219ebaa2014-09-22 15:41:24 -0700315 }
316
alshabiba68eb962014-09-24 20:34:13 -0700317 @Override
318 public void removeRulesById(ApplicationId id, FlowRule... flowRules) {
319 // TODO: optimize using the ApplicationId
320 removeFlowRule(flowRules);
321 }
322
alshabib193525b2014-10-08 18:58:03 -0700323 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800324 public void executeBatch(FlowRuleBatchOperation batch) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900325 checkNotNull(batch);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800326
327 pendingBatches.put(batch.id(), new InternalCacheEntry(batch));
328
ssyoon9030fbcd92015-08-17 10:42:07 +0900329 Dpid dpid = Dpid.dpid(batch.deviceId().uri());
330 OpenFlowSwitch sw = controller.getSwitch(dpid);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800331 OFFlowMod mod;
alshabib193525b2014-10-08 18:58:03 -0700332 for (FlowRuleBatchEntry fbe : batch.getOperations()) {
jcc3d4e14a2015-04-21 11:32:05 +0800333 // flow is the third party privacy flow
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -0700334
335 FlowRuleExtPayLoad flowRuleExtPayLoad = fbe.target().payLoad();
336 if (hasPayload(flowRuleExtPayLoad)) {
337 OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad());
jcc3d4e14a2015-04-21 11:32:05 +0800338 sw.sendMsg(msg);
339 continue;
340 }
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700341 FlowModBuilder builder =
Jonathan Hart3c259162015-10-21 21:31:19 -0700342 FlowModBuilder.builder(fbe.target(), sw.factory(),
343 Optional.of(batch.id()), Optional.of(driverService));
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700344 NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid);
Sho SHIMIZUaba9d002015-01-29 14:51:04 -0800345 switch (fbe.operator()) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700346 case ADD:
347 mod = builder.buildFlowAdd();
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700348 if (adaptiveFlowSampling && collector != null) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900349 // Add TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700350 collector.addWithFlowRule(fbe.target());
ssyoon9030fbcd92015-08-17 10:42:07 +0900351 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700352 break;
353 case REMOVE:
354 mod = builder.buildFlowDel();
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700355 if (adaptiveFlowSampling && collector != null) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900356 // Remove TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700357 collector.removeFlows(fbe.target());
ssyoon9030fbcd92015-08-17 10:42:07 +0900358 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700359 break;
360 case MODIFY:
361 mod = builder.buildFlowMod();
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700362 if (adaptiveFlowSampling && collector != null) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900363 // Add or Update TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
364 // afsCollectors.get(dpid).addWithFlowRule(fbe.target()); //check if add is good or not
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700365 collector.addOrUpdateFlows((FlowEntry) fbe.target());
ssyoon9030fbcd92015-08-17 10:42:07 +0900366 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700367 break;
368 default:
369 log.error("Unsupported batch operation {}; skipping flowmod {}",
ssyoon9030fbcd92015-08-17 10:42:07 +0900370 fbe.operator(), fbe);
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700371 continue;
jcc3d4e14a2015-04-21 11:32:05 +0800372 }
Saurav Das3ea46622015-04-22 14:01:34 -0700373 sw.sendMsg(mod);
alshabib193525b2014-10-08 18:58:03 -0700374 }
jcc3d4e14a2015-04-21 11:32:05 +0800375 OFBarrierRequest.Builder builder = sw.factory().buildBarrierRequest()
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800376 .setXid(batch.id());
377 sw.sendMsg(builder.build());
alshabib193525b2014-10-08 18:58:03 -0700378 }
379
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -0700380 private boolean hasPayload(FlowRuleExtPayLoad flowRuleExtPayLoad) {
381 return flowRuleExtPayLoad != null &&
382 flowRuleExtPayLoad.payLoad() != null &&
383 flowRuleExtPayLoad.payLoad().length > 0;
384 }
385
alshabib8f1cf4a2014-09-17 14:44:48 -0700386 private class InternalFlowProvider
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800387 implements OpenFlowSwitchListener, OpenFlowEventListener {
alshabib8f1cf4a2014-09-17 14:44:48 -0700388
alshabib8f1cf4a2014-09-17 14:44:48 -0700389 @Override
390 public void switchAdded(Dpid dpid) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900391
392 OpenFlowSwitch sw = controller.getSwitch(dpid);
393
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700394 createCollector(controller.getSwitch(dpid));
alshabib8f1cf4a2014-09-17 14:44:48 -0700395 }
396
397 @Override
398 public void switchRemoved(Dpid dpid) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900399 if (adaptiveFlowSampling) {
400 NewAdaptiveFlowStatsCollector collector = afsCollectors.remove(dpid);
401 if (collector != null) {
402 collector.stop();
403 }
404 } else {
405 FlowStatsCollector collector = simpleCollectors.remove(dpid);
406 if (collector != null) {
407 collector.stop();
408 }
alshabibdfc7afb2014-10-21 20:13:27 -0700409 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700410 TableStatisticsCollector tsc = tableStatsCollectors.remove(dpid);
411 if (tsc != null) {
412 tsc.stop();
413 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700414 }
415
416 @Override
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700417 public void switchChanged(Dpid dpid) {
418 }
419
420 @Override
alshabib8f1cf4a2014-09-17 14:44:48 -0700421 public void portChanged(Dpid dpid, OFPortStatus status) {
jcc3d4e14a2015-04-21 11:32:05 +0800422 // TODO: Decide whether to evict flows internal store.
alshabib8f1cf4a2014-09-17 14:44:48 -0700423 }
424
425 @Override
426 public void handleMessage(Dpid dpid, OFMessage msg) {
alshabibda1644e2015-03-13 14:01:35 -0700427 OpenFlowSwitch sw = controller.getSwitch(dpid);
alshabib8f1cf4a2014-09-17 14:44:48 -0700428 switch (msg.getType()) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700429 case FLOW_REMOVED:
430 OFFlowRemoved removed = (OFFlowRemoved) msg;
alshabib6b5cfec2014-09-18 17:42:18 -0700431
Jonathan Hart3c259162015-10-21 21:31:19 -0700432 FlowEntry fr = new FlowEntryBuilder(dpid, removed, driverService).build();
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700433 providerService.flowRemoved(fr);
ssyoon9030fbcd92015-08-17 10:42:07 +0900434
435 if (adaptiveFlowSampling) {
436 // Removed TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700437 NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid);
438 if (collector != null) {
439 collector.flowRemoved(fr);
440 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900441 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700442 break;
443 case STATS_REPLY:
444 if (((OFStatsReply) msg).getStatsType() == OFStatsType.FLOW) {
445 pushFlowMetrics(dpid, (OFFlowStatsReply) msg);
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700446 } else if (((OFStatsReply) msg).getStatsType() == OFStatsType.TABLE) {
447 pushTableStatistics(dpid, (OFTableStatsReply) msg);
sangho89bf6fb2015-02-09 09:33:13 -0800448 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700449 break;
450 case BARRIER_REPLY:
451 try {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700452 InternalCacheEntry entry = pendingBatches.getIfPresent(msg.getXid());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800453 if (entry != null) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700454 providerService
455 .batchOperationCompleted(msg.getXid(),
456 entry.completed());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800457 } else {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700458 log.warn("Received unknown Barrier Reply: {}",
459 msg.getXid());
460 }
461 } finally {
462 pendingBatches.invalidate(msg.getXid());
463 }
464 break;
465 case ERROR:
Thomas Vachuska3358af22015-05-19 18:40:34 -0700466 // TODO: This needs to get suppressed in a better way.
467 if (msg instanceof OFBadRequestErrorMsg &&
468 ((OFBadRequestErrorMsg) msg).getCode() == OFBadRequestCode.BAD_TYPE) {
469 log.debug("Received error message {} from {}", msg, dpid);
470 } else {
471 log.warn("Received error message {} from {}", msg, dpid);
472 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700473
474 OFErrorMsg error = (OFErrorMsg) msg;
475 if (error.getErrType() == OFErrorType.FLOW_MOD_FAILED) {
476 OFFlowModFailedErrorMsg fmFailed = (OFFlowModFailedErrorMsg) error;
477 if (fmFailed.getData().getParsedMessage().isPresent()) {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700478 OFMessage m = fmFailed.getData().getParsedMessage().get();
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700479 OFFlowMod fm = (OFFlowMod) m;
Thomas Vachuska3358af22015-05-19 18:40:34 -0700480 InternalCacheEntry entry =
481 pendingBatches.getIfPresent(msg.getXid());
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700482 if (entry != null) {
Jonathan Hart3c259162015-10-21 21:31:19 -0700483 entry.appendFailure(new FlowEntryBuilder(dpid, fm, driverService).build());
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700484 } else {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700485 log.error("No matching batch for this error: {}", error);
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700486 }
487 } else {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700488 // FIXME: Potentially add flowtracking to avoid this message.
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700489 log.error("Flow installation failed but switch didn't"
490 + " tell us which one.");
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800491 }
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800492 }
Ray Milkey4fd3ceb2015-12-10 14:43:08 -0800493 break;
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700494 default:
495 log.debug("Unhandled message type: {}", msg.getType());
alshabib8f1cf4a2014-09-17 14:44:48 -0700496 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700497 }
498
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700499 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700500 public void receivedRoleReply(Dpid dpid, RoleState requested,
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800501 RoleState response) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700502 // Do nothing here for now.
503 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700504
sangho89bf6fb2015-02-09 09:33:13 -0800505 private void pushFlowMetrics(Dpid dpid, OFFlowStatsReply replies) {
alshabib64def642014-12-02 23:27:37 -0800506
alshabib54ce5892014-09-23 17:50:51 -0700507 DeviceId did = DeviceId.deviceId(Dpid.uri(dpid));
alshabib54ce5892014-09-23 17:50:51 -0700508
alshabib64def642014-12-02 23:27:37 -0800509 List<FlowEntry> flowEntries = replies.getEntries().stream()
Jonathan Hart3c259162015-10-21 21:31:19 -0700510 .map(entry -> new FlowEntryBuilder(dpid, entry, driverService).build())
alshabib64def642014-12-02 23:27:37 -0800511 .collect(Collectors.toList());
alshabib54ce5892014-09-23 17:50:51 -0700512
ssyoon9030fbcd92015-08-17 10:42:07 +0900513 if (adaptiveFlowSampling) {
514 NewAdaptiveFlowStatsCollector afsc = afsCollectors.get(dpid);
515
516 synchronized (afsc) {
517 if (afsc.getFlowMissingXid() != NewAdaptiveFlowStatsCollector.NO_FLOW_MISSING_XID) {
518 log.debug("OpenFlowRuleProvider:pushFlowMetrics, flowMissingXid={}, "
519 + "OFFlowStatsReply Xid={}, for {}",
520 afsc.getFlowMissingXid(), replies.getXid(), dpid);
521 }
522
523 // Check that OFFlowStatsReply Xid is same with the one of OFFlowStatsRequest?
524 if (afsc.getFlowMissingXid() != NewAdaptiveFlowStatsCollector.NO_FLOW_MISSING_XID) {
525 if (afsc.getFlowMissingXid() == replies.getXid()) {
526 // call entire flow stats update with flowMissing synchronization.
527 // used existing pushFlowMetrics
528 providerService.pushFlowMetrics(did, flowEntries);
529 }
530 // reset flowMissingXid to NO_FLOW_MISSING_XID
531 afsc.setFlowMissingXid(NewAdaptiveFlowStatsCollector.NO_FLOW_MISSING_XID);
532
533 } else {
534 // call individual flow stats update
535 providerService.pushFlowMetricsWithoutFlowMissing(did, flowEntries);
536 }
537
538 // Update TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
539 afsc.pushFlowMetrics(flowEntries);
540 }
541 } else {
542 // call existing entire flow stats update with flowMissing synchronization
543 providerService.pushFlowMetrics(did, flowEntries);
544 }
alshabib5c370ff2014-09-18 10:12:14 -0700545 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700546
547 private void pushTableStatistics(Dpid dpid, OFTableStatsReply replies) {
548
549 DeviceId did = DeviceId.deviceId(Dpid.uri(dpid));
550 List<TableStatisticsEntry> tableStatsEntries = replies.getEntries().stream()
551 .map(entry -> buildTableStatistics(did, entry))
552 .filter(Objects::nonNull)
553 .collect(Collectors.toList());
554 providerService.pushTableStatistics(did, tableStatsEntries);
555 }
556
557 private TableStatisticsEntry buildTableStatistics(DeviceId deviceId,
558 OFTableStatsEntry ofEntry) {
559 TableStatisticsEntry entry = null;
560 if (ofEntry != null) {
561 entry = new DefaultTableStatisticsEntry(deviceId,
562 ofEntry.getTableId().getValue(),
563 ofEntry.getActiveCount(),
564 ofEntry.getLookupCount().getValue(),
565 ofEntry.getMatchedCount().getValue());
566 }
567
568 return entry;
569
570 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700571 }
alshabib1cc04f72014-09-16 16:09:58 -0700572
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800573 /**
jcc3d4e14a2015-04-21 11:32:05 +0800574 * The internal cache entry holding the original request as well as
575 * accumulating the any failures along the way.
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700576 * <p/>
jcc3d4e14a2015-04-21 11:32:05 +0800577 * If this entry is evicted from the cache then the entire operation is
578 * considered failed. Otherwise, only the failures reported by the device
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800579 * will be propagated up.
580 */
581 private class InternalCacheEntry {
alshabib902d41b2014-10-07 16:52:05 -0700582
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800583 private final FlowRuleBatchOperation operation;
584 private final Set<FlowRule> failures = Sets.newConcurrentHashSet();
alshabib193525b2014-10-08 18:58:03 -0700585
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800586 public InternalCacheEntry(FlowRuleBatchOperation operation) {
587 this.operation = operation;
alshabib902d41b2014-10-07 16:52:05 -0700588 }
589
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800590 /**
591 * Appends a failed rule to the set of failed items.
jcc3d4e14a2015-04-21 11:32:05 +0800592 *
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800593 * @param rule the failed rule
594 */
595 public void appendFailure(FlowRule rule) {
596 failures.add(rule);
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800597 }
598
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800599 /**
600 * Fails the entire batch and returns the failed operation.
jcc3d4e14a2015-04-21 11:32:05 +0800601 *
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800602 * @return the failed operation
603 */
604 public CompletedBatchOperation failedCompletion() {
605 Set<FlowRule> fails = operation.getOperations().stream()
606 .map(op -> op.target()).collect(Collectors.toSet());
jcc3d4e14a2015-04-21 11:32:05 +0800607 return new CompletedBatchOperation(false,
608 Collections
609 .unmodifiableSet(fails),
610 operation.deviceId());
alshabib902d41b2014-10-07 16:52:05 -0700611 }
612
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800613 /**
614 * Returns the completed operation and whether the batch suceeded.
jcc3d4e14a2015-04-21 11:32:05 +0800615 *
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800616 * @return the completed operation
617 */
618 public CompletedBatchOperation completed() {
jcc3d4e14a2015-04-21 11:32:05 +0800619 return new CompletedBatchOperation(
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700620 failures.isEmpty(),
621 Collections
622 .unmodifiableSet(failures),
623 operation.deviceId());
alshabib902d41b2014-10-07 16:52:05 -0700624 }
alshabib902d41b2014-10-07 16:52:05 -0700625 }
alshabiba68eb962014-09-24 20:34:13 -0700626
alshabib1cc04f72014-09-16 16:09:58 -0700627}