blob: 7b1828dbbcb10502e4bc37d4c7e8817e5f7fd3c5 [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
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700150 pendingBatches = createBatchCache();
ssyoon9030fbcd92015-08-17 10:42:07 +0900151
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700152 createCollectors();
alshabib3d643ec2014-10-22 18:33:00 -0700153
ssyoon9030fbcd92015-08-17 10:42:07 +0900154 log.info("Started with flowPollFrequency = {}, adaptiveFlowSampling = {}",
155 flowPollFrequency, adaptiveFlowSampling);
alshabib1cc04f72014-09-16 16:09:58 -0700156 }
157
158 @Deactivate
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700159 public void deactivate(ComponentContext context) {
160 cfgService.unregisterProperties(getClass(), false);
161 stopCollectors();
alshabib1cc04f72014-09-16 16:09:58 -0700162 providerRegistry.unregister(this);
163 providerService = null;
164
165 log.info("Stopped");
166 }
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800167
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700168 @Modified
169 public void modified(ComponentContext context) {
170 Dictionary<?, ?> properties = context.getProperties();
171 int newFlowPollFrequency;
172 try {
173 String s = get(properties, "flowPollFrequency");
174 newFlowPollFrequency = isNullOrEmpty(s) ? flowPollFrequency : Integer.parseInt(s.trim());
175
176 } catch (NumberFormatException | ClassCastException e) {
177 newFlowPollFrequency = flowPollFrequency;
178 }
179
180 if (newFlowPollFrequency != flowPollFrequency) {
181 flowPollFrequency = newFlowPollFrequency;
182 adjustRate();
183 }
184
185 log.info("Settings: flowPollFrequency={}", flowPollFrequency);
ssyoon9030fbcd92015-08-17 10:42:07 +0900186
187 boolean newAdaptiveFlowSampling;
188 String s = get(properties, "adaptiveFlowSampling");
189 newAdaptiveFlowSampling = isNullOrEmpty(s) ? adaptiveFlowSampling : Boolean.parseBoolean(s.trim());
190
191 if (newAdaptiveFlowSampling != adaptiveFlowSampling) {
192 // stop previous collector
193 stopCollectors();
194 adaptiveFlowSampling = newAdaptiveFlowSampling;
195 // create new collectors
196 createCollectors();
197 }
198
199 log.info("Settings: adaptiveFlowSampling={}", adaptiveFlowSampling);
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700200 }
201
202 private Cache<Long, InternalCacheEntry> createBatchCache() {
203 return CacheBuilder.newBuilder()
204 .expireAfterWrite(10, TimeUnit.SECONDS)
205 .removalListener((RemovalNotification<Long, InternalCacheEntry> notification) -> {
206 if (notification.getCause() == RemovalCause.EXPIRED) {
207 providerService.batchOperationCompleted(notification.getKey(),
208 notification.getValue().failedCompletion());
209 }
210 }).build();
211 }
212
213 private void createCollectors() {
214 controller.getSwitches().forEach(this::createCollector);
215 }
216
217 private void createCollector(OpenFlowSwitch sw) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900218 if (adaptiveFlowSampling) {
219 // NewAdaptiveFlowStatsCollector Constructor
220 NewAdaptiveFlowStatsCollector fsc = new NewAdaptiveFlowStatsCollector(sw, flowPollFrequency);
221 fsc.start();
222 afsCollectors.put(new Dpid(sw.getId()), fsc);
223 } else {
224 FlowStatsCollector fsc = new FlowStatsCollector(timer, sw, flowPollFrequency);
225 fsc.start();
226 simpleCollectors.put(new Dpid(sw.getId()), fsc);
227 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700228 TableStatisticsCollector tsc = new TableStatisticsCollector(timer, sw, flowPollFrequency);
229 tsc.start();
230 tableStatsCollectors.put(new Dpid(sw.getId()), tsc);
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700231 }
232
233 private void stopCollectors() {
ssyoon9030fbcd92015-08-17 10:42:07 +0900234 if (adaptiveFlowSampling) {
235 // NewAdaptiveFlowStatsCollector Destructor
236 afsCollectors.values().forEach(NewAdaptiveFlowStatsCollector::stop);
237 afsCollectors.clear();
238 } else {
239 simpleCollectors.values().forEach(FlowStatsCollector::stop);
240 simpleCollectors.clear();
241 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700242 tableStatsCollectors.values().forEach(TableStatisticsCollector::stop);
243 tableStatsCollectors.clear();
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700244 }
245
246 private void adjustRate() {
247 DefaultLoad.setPollInterval(flowPollFrequency);
ssyoon9030fbcd92015-08-17 10:42:07 +0900248 if (adaptiveFlowSampling) {
249 // NewAdaptiveFlowStatsCollector calAndPollInterval
250 afsCollectors.values().forEach(fsc -> fsc.adjustCalAndPollInterval(flowPollFrequency));
251 } else {
252 simpleCollectors.values().forEach(fsc -> fsc.adjustPollInterval(flowPollFrequency));
253 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700254 tableStatsCollectors.values().forEach(tsc -> tsc.adjustPollInterval(flowPollFrequency));
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700255 }
256
alshabib1cc04f72014-09-16 16:09:58 -0700257 @Override
258 public void applyFlowRule(FlowRule... flowRules) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800259 for (FlowRule flowRule : flowRules) {
260 applyRule(flowRule);
alshabib35edb1a2014-09-16 17:44:44 -0700261 }
alshabib1cc04f72014-09-16 16:09:58 -0700262 }
263
alshabib35edb1a2014-09-16 17:44:44 -0700264 private void applyRule(FlowRule flowRule) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900265 Dpid dpid = Dpid.dpid(flowRule.deviceId().uri());
266 OpenFlowSwitch sw = controller.getSwitch(dpid);
267
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -0700268 FlowRuleExtPayLoad flowRuleExtPayLoad = flowRule.payLoad();
269 if (hasPayload(flowRuleExtPayLoad)) {
270 OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad());
jcc3d4e14a2015-04-21 11:32:05 +0800271 sw.sendMsg(msg);
272 return;
273 }
alshabibbdcbb102015-04-22 14:16:38 -0700274 sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(),
Jonathan Hart3c259162015-10-21 21:31:19 -0700275 Optional.empty(), Optional.of(driverService)).buildFlowAdd());
ssyoon9030fbcd92015-08-17 10:42:07 +0900276
277 if (adaptiveFlowSampling) {
278 // Add TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700279 NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid);
280 if (collector != null) {
281 collector.addWithFlowRule(flowRule);
282 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900283 }
alshabib35edb1a2014-09-16 17:44:44 -0700284 }
285
alshabib1cc04f72014-09-16 16:09:58 -0700286 @Override
287 public void removeFlowRule(FlowRule... flowRules) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800288 for (FlowRule flowRule : flowRules) {
289 removeRule(flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700290 }
alshabib1cc04f72014-09-16 16:09:58 -0700291 }
292
alshabib219ebaa2014-09-22 15:41:24 -0700293 private void removeRule(FlowRule flowRule) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900294 Dpid dpid = Dpid.dpid(flowRule.deviceId().uri());
295 OpenFlowSwitch sw = controller.getSwitch(dpid);
296
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -0700297 FlowRuleExtPayLoad flowRuleExtPayLoad = flowRule.payLoad();
298 if (hasPayload(flowRuleExtPayLoad)) {
299 OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad());
jcc3d4e14a2015-04-21 11:32:05 +0800300 sw.sendMsg(msg);
301 return;
302 }
alshabibbdcbb102015-04-22 14:16:38 -0700303 sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(),
Jonathan Hart3c259162015-10-21 21:31:19 -0700304 Optional.empty(), Optional.of(driverService)).buildFlowDel());
ssyoon9030fbcd92015-08-17 10:42:07 +0900305
306 if (adaptiveFlowSampling) {
307 // Remove TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700308 NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid);
309 if (collector != null) {
310 collector.removeFlows(flowRule);
311 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900312 }
alshabib219ebaa2014-09-22 15:41:24 -0700313 }
314
alshabiba68eb962014-09-24 20:34:13 -0700315 @Override
316 public void removeRulesById(ApplicationId id, FlowRule... flowRules) {
317 // TODO: optimize using the ApplicationId
318 removeFlowRule(flowRules);
319 }
320
alshabib193525b2014-10-08 18:58:03 -0700321 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800322 public void executeBatch(FlowRuleBatchOperation batch) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900323 checkNotNull(batch);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800324
325 pendingBatches.put(batch.id(), new InternalCacheEntry(batch));
326
ssyoon9030fbcd92015-08-17 10:42:07 +0900327 Dpid dpid = Dpid.dpid(batch.deviceId().uri());
328 OpenFlowSwitch sw = controller.getSwitch(dpid);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800329 OFFlowMod mod;
alshabib193525b2014-10-08 18:58:03 -0700330 for (FlowRuleBatchEntry fbe : batch.getOperations()) {
jcc3d4e14a2015-04-21 11:32:05 +0800331 // flow is the third party privacy flow
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -0700332
333 FlowRuleExtPayLoad flowRuleExtPayLoad = fbe.target().payLoad();
334 if (hasPayload(flowRuleExtPayLoad)) {
335 OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad());
jcc3d4e14a2015-04-21 11:32:05 +0800336 sw.sendMsg(msg);
337 continue;
338 }
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700339 FlowModBuilder builder =
Jonathan Hart3c259162015-10-21 21:31:19 -0700340 FlowModBuilder.builder(fbe.target(), sw.factory(),
341 Optional.of(batch.id()), Optional.of(driverService));
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700342 NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid);
Sho SHIMIZUaba9d002015-01-29 14:51:04 -0800343 switch (fbe.operator()) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700344 case ADD:
345 mod = builder.buildFlowAdd();
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700346 if (adaptiveFlowSampling && collector != null) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900347 // Add TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700348 collector.addWithFlowRule(fbe.target());
ssyoon9030fbcd92015-08-17 10:42:07 +0900349 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700350 break;
351 case REMOVE:
352 mod = builder.buildFlowDel();
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700353 if (adaptiveFlowSampling && collector != null) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900354 // Remove TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700355 collector.removeFlows(fbe.target());
ssyoon9030fbcd92015-08-17 10:42:07 +0900356 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700357 break;
358 case MODIFY:
359 mod = builder.buildFlowMod();
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700360 if (adaptiveFlowSampling && collector != null) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900361 // Add or Update TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
362 // afsCollectors.get(dpid).addWithFlowRule(fbe.target()); //check if add is good or not
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700363 collector.addOrUpdateFlows((FlowEntry) fbe.target());
ssyoon9030fbcd92015-08-17 10:42:07 +0900364 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700365 break;
366 default:
367 log.error("Unsupported batch operation {}; skipping flowmod {}",
ssyoon9030fbcd92015-08-17 10:42:07 +0900368 fbe.operator(), fbe);
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700369 continue;
jcc3d4e14a2015-04-21 11:32:05 +0800370 }
Saurav Das3ea46622015-04-22 14:01:34 -0700371 sw.sendMsg(mod);
alshabib193525b2014-10-08 18:58:03 -0700372 }
jcc3d4e14a2015-04-21 11:32:05 +0800373 OFBarrierRequest.Builder builder = sw.factory().buildBarrierRequest()
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800374 .setXid(batch.id());
375 sw.sendMsg(builder.build());
alshabib193525b2014-10-08 18:58:03 -0700376 }
377
Thomas Vachuskaa6c0d042015-04-23 10:17:37 -0700378 private boolean hasPayload(FlowRuleExtPayLoad flowRuleExtPayLoad) {
379 return flowRuleExtPayLoad != null &&
380 flowRuleExtPayLoad.payLoad() != null &&
381 flowRuleExtPayLoad.payLoad().length > 0;
382 }
383
alshabib8f1cf4a2014-09-17 14:44:48 -0700384 private class InternalFlowProvider
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800385 implements OpenFlowSwitchListener, OpenFlowEventListener {
alshabib8f1cf4a2014-09-17 14:44:48 -0700386
alshabib8f1cf4a2014-09-17 14:44:48 -0700387 @Override
388 public void switchAdded(Dpid dpid) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900389
390 OpenFlowSwitch sw = controller.getSwitch(dpid);
391
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700392 createCollector(controller.getSwitch(dpid));
alshabib8f1cf4a2014-09-17 14:44:48 -0700393 }
394
395 @Override
396 public void switchRemoved(Dpid dpid) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900397 if (adaptiveFlowSampling) {
398 NewAdaptiveFlowStatsCollector collector = afsCollectors.remove(dpid);
399 if (collector != null) {
400 collector.stop();
401 }
402 } else {
403 FlowStatsCollector collector = simpleCollectors.remove(dpid);
404 if (collector != null) {
405 collector.stop();
406 }
alshabibdfc7afb2014-10-21 20:13:27 -0700407 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700408 TableStatisticsCollector tsc = tableStatsCollectors.remove(dpid);
409 if (tsc != null) {
410 tsc.stop();
411 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700412 }
413
414 @Override
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700415 public void switchChanged(Dpid dpid) {
416 }
417
418 @Override
alshabib8f1cf4a2014-09-17 14:44:48 -0700419 public void portChanged(Dpid dpid, OFPortStatus status) {
jcc3d4e14a2015-04-21 11:32:05 +0800420 // TODO: Decide whether to evict flows internal store.
alshabib8f1cf4a2014-09-17 14:44:48 -0700421 }
422
423 @Override
424 public void handleMessage(Dpid dpid, OFMessage msg) {
alshabibda1644e2015-03-13 14:01:35 -0700425 OpenFlowSwitch sw = controller.getSwitch(dpid);
alshabib8f1cf4a2014-09-17 14:44:48 -0700426 switch (msg.getType()) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700427 case FLOW_REMOVED:
428 OFFlowRemoved removed = (OFFlowRemoved) msg;
alshabib6b5cfec2014-09-18 17:42:18 -0700429
Jonathan Hart3c259162015-10-21 21:31:19 -0700430 FlowEntry fr = new FlowEntryBuilder(dpid, removed, driverService).build();
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700431 providerService.flowRemoved(fr);
ssyoon9030fbcd92015-08-17 10:42:07 +0900432
433 if (adaptiveFlowSampling) {
434 // Removed TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
Thomas Vachuskad07c0922015-10-06 14:48:06 -0700435 NewAdaptiveFlowStatsCollector collector = afsCollectors.get(dpid);
436 if (collector != null) {
437 collector.flowRemoved(fr);
438 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900439 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700440 break;
441 case STATS_REPLY:
442 if (((OFStatsReply) msg).getStatsType() == OFStatsType.FLOW) {
443 pushFlowMetrics(dpid, (OFFlowStatsReply) msg);
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700444 } else if (((OFStatsReply) msg).getStatsType() == OFStatsType.TABLE) {
445 pushTableStatistics(dpid, (OFTableStatsReply) msg);
sangho89bf6fb2015-02-09 09:33:13 -0800446 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700447 break;
448 case BARRIER_REPLY:
449 try {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700450 InternalCacheEntry entry = pendingBatches.getIfPresent(msg.getXid());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800451 if (entry != null) {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700452 providerService
453 .batchOperationCompleted(msg.getXid(),
454 entry.completed());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800455 } else {
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700456 log.warn("Received unknown Barrier Reply: {}",
457 msg.getXid());
458 }
459 } finally {
460 pendingBatches.invalidate(msg.getXid());
461 }
462 break;
463 case ERROR:
Thomas Vachuska3358af22015-05-19 18:40:34 -0700464 // TODO: This needs to get suppressed in a better way.
465 if (msg instanceof OFBadRequestErrorMsg &&
466 ((OFBadRequestErrorMsg) msg).getCode() == OFBadRequestCode.BAD_TYPE) {
467 log.debug("Received error message {} from {}", msg, dpid);
468 } else {
469 log.warn("Received error message {} from {}", msg, dpid);
470 }
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700471
472 OFErrorMsg error = (OFErrorMsg) msg;
473 if (error.getErrType() == OFErrorType.FLOW_MOD_FAILED) {
474 OFFlowModFailedErrorMsg fmFailed = (OFFlowModFailedErrorMsg) error;
475 if (fmFailed.getData().getParsedMessage().isPresent()) {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700476 OFMessage m = fmFailed.getData().getParsedMessage().get();
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700477 OFFlowMod fm = (OFFlowMod) m;
Thomas Vachuska3358af22015-05-19 18:40:34 -0700478 InternalCacheEntry entry =
479 pendingBatches.getIfPresent(msg.getXid());
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700480 if (entry != null) {
Jonathan Hart3c259162015-10-21 21:31:19 -0700481 entry.appendFailure(new FlowEntryBuilder(dpid, fm, driverService).build());
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700482 } else {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700483 log.error("No matching batch for this error: {}", error);
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700484 }
485 } else {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700486 // FIXME: Potentially add flowtracking to avoid this message.
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700487 log.error("Flow installation failed but switch didn't"
488 + " tell us which one.");
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800489 }
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800490 }
Ray Milkey4fd3ceb2015-12-10 14:43:08 -0800491 break;
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700492 default:
493 log.debug("Unhandled message type: {}", msg.getType());
alshabib8f1cf4a2014-09-17 14:44:48 -0700494 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700495 }
496
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700497 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700498 public void receivedRoleReply(Dpid dpid, RoleState requested,
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800499 RoleState response) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700500 // Do nothing here for now.
501 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700502
sangho89bf6fb2015-02-09 09:33:13 -0800503 private void pushFlowMetrics(Dpid dpid, OFFlowStatsReply replies) {
alshabib64def642014-12-02 23:27:37 -0800504
alshabib54ce5892014-09-23 17:50:51 -0700505 DeviceId did = DeviceId.deviceId(Dpid.uri(dpid));
alshabib54ce5892014-09-23 17:50:51 -0700506
alshabib64def642014-12-02 23:27:37 -0800507 List<FlowEntry> flowEntries = replies.getEntries().stream()
Jonathan Hart3c259162015-10-21 21:31:19 -0700508 .map(entry -> new FlowEntryBuilder(dpid, entry, driverService).build())
alshabib64def642014-12-02 23:27:37 -0800509 .collect(Collectors.toList());
alshabib54ce5892014-09-23 17:50:51 -0700510
ssyoon9030fbcd92015-08-17 10:42:07 +0900511 if (adaptiveFlowSampling) {
512 NewAdaptiveFlowStatsCollector afsc = afsCollectors.get(dpid);
513
514 synchronized (afsc) {
515 if (afsc.getFlowMissingXid() != NewAdaptiveFlowStatsCollector.NO_FLOW_MISSING_XID) {
516 log.debug("OpenFlowRuleProvider:pushFlowMetrics, flowMissingXid={}, "
517 + "OFFlowStatsReply Xid={}, for {}",
518 afsc.getFlowMissingXid(), replies.getXid(), dpid);
519 }
520
521 // Check that OFFlowStatsReply Xid is same with the one of OFFlowStatsRequest?
522 if (afsc.getFlowMissingXid() != NewAdaptiveFlowStatsCollector.NO_FLOW_MISSING_XID) {
523 if (afsc.getFlowMissingXid() == replies.getXid()) {
524 // call entire flow stats update with flowMissing synchronization.
525 // used existing pushFlowMetrics
526 providerService.pushFlowMetrics(did, flowEntries);
527 }
528 // reset flowMissingXid to NO_FLOW_MISSING_XID
529 afsc.setFlowMissingXid(NewAdaptiveFlowStatsCollector.NO_FLOW_MISSING_XID);
530
531 } else {
532 // call individual flow stats update
533 providerService.pushFlowMetricsWithoutFlowMissing(did, flowEntries);
534 }
535
536 // Update TypedFlowEntry to deviceFlowEntries in NewAdaptiveFlowStatsCollector
537 afsc.pushFlowMetrics(flowEntries);
538 }
539 } else {
540 // call existing entire flow stats update with flowMissing synchronization
541 providerService.pushFlowMetrics(did, flowEntries);
542 }
alshabib5c370ff2014-09-18 10:12:14 -0700543 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700544
545 private void pushTableStatistics(Dpid dpid, OFTableStatsReply replies) {
546
547 DeviceId did = DeviceId.deviceId(Dpid.uri(dpid));
548 List<TableStatisticsEntry> tableStatsEntries = replies.getEntries().stream()
549 .map(entry -> buildTableStatistics(did, entry))
550 .filter(Objects::nonNull)
551 .collect(Collectors.toList());
552 providerService.pushTableStatistics(did, tableStatsEntries);
553 }
554
555 private TableStatisticsEntry buildTableStatistics(DeviceId deviceId,
556 OFTableStatsEntry ofEntry) {
557 TableStatisticsEntry entry = null;
558 if (ofEntry != null) {
559 entry = new DefaultTableStatisticsEntry(deviceId,
560 ofEntry.getTableId().getValue(),
561 ofEntry.getActiveCount(),
562 ofEntry.getLookupCount().getValue(),
563 ofEntry.getMatchedCount().getValue());
564 }
565
566 return entry;
567
568 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700569 }
alshabib1cc04f72014-09-16 16:09:58 -0700570
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800571 /**
jcc3d4e14a2015-04-21 11:32:05 +0800572 * The internal cache entry holding the original request as well as
573 * accumulating the any failures along the way.
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700574 * <p/>
jcc3d4e14a2015-04-21 11:32:05 +0800575 * If this entry is evicted from the cache then the entire operation is
576 * considered failed. Otherwise, only the failures reported by the device
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800577 * will be propagated up.
578 */
579 private class InternalCacheEntry {
alshabib902d41b2014-10-07 16:52:05 -0700580
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800581 private final FlowRuleBatchOperation operation;
582 private final Set<FlowRule> failures = Sets.newConcurrentHashSet();
alshabib193525b2014-10-08 18:58:03 -0700583
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800584 public InternalCacheEntry(FlowRuleBatchOperation operation) {
585 this.operation = operation;
alshabib902d41b2014-10-07 16:52:05 -0700586 }
587
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800588 /**
589 * Appends a failed rule to the set of failed items.
jcc3d4e14a2015-04-21 11:32:05 +0800590 *
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800591 * @param rule the failed rule
592 */
593 public void appendFailure(FlowRule rule) {
594 failures.add(rule);
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800595 }
596
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800597 /**
598 * Fails the entire batch and returns the failed operation.
jcc3d4e14a2015-04-21 11:32:05 +0800599 *
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800600 * @return the failed operation
601 */
602 public CompletedBatchOperation failedCompletion() {
603 Set<FlowRule> fails = operation.getOperations().stream()
604 .map(op -> op.target()).collect(Collectors.toSet());
jcc3d4e14a2015-04-21 11:32:05 +0800605 return new CompletedBatchOperation(false,
606 Collections
607 .unmodifiableSet(fails),
608 operation.deviceId());
alshabib902d41b2014-10-07 16:52:05 -0700609 }
610
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800611 /**
612 * Returns the completed operation and whether the batch suceeded.
jcc3d4e14a2015-04-21 11:32:05 +0800613 *
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800614 * @return the completed operation
615 */
616 public CompletedBatchOperation completed() {
jcc3d4e14a2015-04-21 11:32:05 +0800617 return new CompletedBatchOperation(
Thomas Vachuska75aaa672015-04-29 12:24:43 -0700618 failures.isEmpty(),
619 Collections
620 .unmodifiableSet(failures),
621 operation.deviceId());
alshabib902d41b2014-10-07 16:52:05 -0700622 }
alshabib902d41b2014-10-07 16:52:05 -0700623 }
alshabiba68eb962014-09-24 20:34:13 -0700624
alshabib1cc04f72014-09-16 16:09:58 -0700625}