blob: add275233154ea195b7d1614c7e308c89b4f78d6 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-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
Jonathan Hart2ffcd102015-01-16 16:47:50 -080018
Brian O'Connor72cb19a2015-01-16 16:14:41 -080019import com.google.common.cache.Cache;
20import com.google.common.cache.CacheBuilder;
21import com.google.common.cache.RemovalCause;
22import com.google.common.cache.RemovalNotification;
23import com.google.common.collect.Maps;
24import com.google.common.collect.Sets;
alshabib1cc04f72014-09-16 16:09:58 -070025import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
28import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.core.ApplicationId;
31import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.flow.CompletedBatchOperation;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.flow.FlowEntry;
34import org.onosproject.net.flow.FlowRule;
35import org.onosproject.net.flow.FlowRuleBatchEntry;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080036import org.onosproject.net.flow.FlowRuleBatchOperation;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.net.flow.FlowRuleProvider;
38import org.onosproject.net.flow.FlowRuleProviderRegistry;
39import org.onosproject.net.flow.FlowRuleProviderService;
40import org.onosproject.net.provider.AbstractProvider;
41import org.onosproject.net.provider.ProviderId;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.openflow.controller.Dpid;
43import org.onosproject.openflow.controller.OpenFlowController;
44import org.onosproject.openflow.controller.OpenFlowEventListener;
45import org.onosproject.openflow.controller.OpenFlowSwitch;
46import org.onosproject.openflow.controller.OpenFlowSwitchListener;
47import org.onosproject.openflow.controller.RoleState;
alshabib902d41b2014-10-07 16:52:05 -070048import org.projectfloodlight.openflow.protocol.OFBarrierRequest;
49import org.projectfloodlight.openflow.protocol.OFErrorMsg;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080050import org.projectfloodlight.openflow.protocol.OFErrorType;
alshabib193525b2014-10-08 18:58:03 -070051import org.projectfloodlight.openflow.protocol.OFFlowMod;
alshabib8f1cf4a2014-09-17 14:44:48 -070052import org.projectfloodlight.openflow.protocol.OFFlowRemoved;
alshabib5c370ff2014-09-18 10:12:14 -070053import org.projectfloodlight.openflow.protocol.OFFlowStatsReply;
alshabib8f1cf4a2014-09-17 14:44:48 -070054import org.projectfloodlight.openflow.protocol.OFMessage;
55import org.projectfloodlight.openflow.protocol.OFPortStatus;
alshabib5c370ff2014-09-18 10:12:14 -070056import org.projectfloodlight.openflow.protocol.OFStatsReply;
sangho89bf6fb2015-02-09 09:33:13 -080057import org.projectfloodlight.openflow.protocol.OFStatsType;
alshabib193525b2014-10-08 18:58:03 -070058import org.projectfloodlight.openflow.protocol.errormsg.OFFlowModFailedErrorMsg;
alshabib1cc04f72014-09-16 16:09:58 -070059import org.slf4j.Logger;
60
Brian O'Connor72cb19a2015-01-16 16:14:41 -080061import java.util.Collections;
62import java.util.List;
63import java.util.Map;
64import java.util.Optional;
65import java.util.Set;
66import java.util.concurrent.TimeUnit;
67import java.util.stream.Collectors;
68
69import static org.slf4j.LoggerFactory.getLogger;
70
alshabibeec3a062014-09-17 18:01:26 -070071
alshabib1cc04f72014-09-16 16:09:58 -070072/**
73 * Provider which uses an OpenFlow controller to detect network
74 * end-station hosts.
75 */
76@Component(immediate = true)
77public class OpenFlowRuleProvider extends AbstractProvider implements FlowRuleProvider {
78
79 private final Logger log = getLogger(getClass());
80
81 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
82 protected FlowRuleProviderRegistry providerRegistry;
83
84 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
85 protected OpenFlowController controller;
86
alshabib1cc04f72014-09-16 16:09:58 -070087
88 private FlowRuleProviderService providerService;
89
alshabibeec3a062014-09-17 18:01:26 -070090 private final InternalFlowProvider listener = new InternalFlowProvider();
91
Brian O'Connor72cb19a2015-01-16 16:14:41 -080092 private Cache<Long, InternalCacheEntry> pendingBatches;
alshabib193525b2014-10-08 18:58:03 -070093
alshabib3d643ec2014-10-22 18:33:00 -070094 private final Map<Dpid, FlowStatsCollector> collectors = Maps.newHashMap();
95
Thomas Vachuska9b2da212014-11-10 19:30:25 -080096
alshabib1cc04f72014-09-16 16:09:58 -070097 /**
98 * Creates an OpenFlow host provider.
99 */
100 public OpenFlowRuleProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800101 super(new ProviderId("of", "org.onosproject.provider.openflow"));
alshabib1cc04f72014-09-16 16:09:58 -0700102 }
103
104 @Activate
105 public void activate() {
106 providerService = providerRegistry.register(this);
alshabibeec3a062014-09-17 18:01:26 -0700107 controller.addListener(listener);
108 controller.addEventListener(listener);
alshabib3d643ec2014-10-22 18:33:00 -0700109
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800110 pendingBatches = CacheBuilder.newBuilder()
111 .expireAfterWrite(10, TimeUnit.SECONDS)
112 .removalListener((RemovalNotification<Long, InternalCacheEntry> notification) -> {
113 if (notification.getCause() == RemovalCause.EXPIRED) {
114 providerService.batchOperationCompleted(notification.getKey(),
115 notification.getValue().failedCompletion());
116 }
117 }).build();
118
119
alshabib3d643ec2014-10-22 18:33:00 -0700120 for (OpenFlowSwitch sw : controller.getSwitches()) {
121 FlowStatsCollector fsc = new FlowStatsCollector(sw, POLL_INTERVAL);
122 fsc.start();
123 collectors.put(new Dpid(sw.getId()), fsc);
124 }
125
126
alshabib1cc04f72014-09-16 16:09:58 -0700127 log.info("Started");
128 }
129
130 @Deactivate
131 public void deactivate() {
132 providerRegistry.unregister(this);
133 providerService = null;
134
135 log.info("Stopped");
136 }
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800137
alshabib1cc04f72014-09-16 16:09:58 -0700138 @Override
139 public void applyFlowRule(FlowRule... flowRules) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800140 for (FlowRule flowRule : flowRules) {
141 applyRule(flowRule);
alshabib35edb1a2014-09-16 17:44:44 -0700142 }
alshabib1cc04f72014-09-16 16:09:58 -0700143 }
144
alshabib35edb1a2014-09-16 17:44:44 -0700145 private void applyRule(FlowRule flowRule) {
146 OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri()));
alshabibbdcbb102015-04-22 14:16:38 -0700147 sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(),
sangho87af8112015-01-29 12:53:08 -0800148 Optional.empty()).buildFlowAdd());
alshabibbdcbb102015-04-22 14:16:38 -0700149
alshabib35edb1a2014-09-16 17:44:44 -0700150 }
151
alshabib35edb1a2014-09-16 17:44:44 -0700152
alshabib1cc04f72014-09-16 16:09:58 -0700153 @Override
154 public void removeFlowRule(FlowRule... flowRules) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800155 for (FlowRule flowRule : flowRules) {
156 removeRule(flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700157 }
alshabib1cc04f72014-09-16 16:09:58 -0700158
159 }
160
alshabib219ebaa2014-09-22 15:41:24 -0700161 private void removeRule(FlowRule flowRule) {
162 OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri()));
alshabibbdcbb102015-04-22 14:16:38 -0700163
164 sw.sendMsg(FlowModBuilder.builder(flowRule, sw.factory(),
sangho87af8112015-01-29 12:53:08 -0800165 Optional.empty()).buildFlowDel());
alshabib219ebaa2014-09-22 15:41:24 -0700166 }
167
alshabiba68eb962014-09-24 20:34:13 -0700168 @Override
169 public void removeRulesById(ApplicationId id, FlowRule... flowRules) {
170 // TODO: optimize using the ApplicationId
171 removeFlowRule(flowRules);
172 }
173
alshabib193525b2014-10-08 18:58:03 -0700174 @Override
sangho87af8112015-01-29 12:53:08 -0800175
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800176 public void executeBatch(FlowRuleBatchOperation batch) {
177
178 pendingBatches.put(batch.id(), new InternalCacheEntry(batch));
179
180
181 OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(batch.deviceId().uri()));
182 OFFlowMod mod;
183
alshabib193525b2014-10-08 18:58:03 -0700184 for (FlowRuleBatchEntry fbe : batch.getOperations()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800185
Brian O'Connor427a1762014-11-19 18:40:32 -0800186 FlowModBuilder builder =
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800187 FlowModBuilder.builder(fbe.target(), sw.factory(),
188 Optional.of(batch.id()));
Sho SHIMIZUaba9d002015-01-29 14:51:04 -0800189 switch (fbe.operator()) {
alshabib193525b2014-10-08 18:58:03 -0700190 case ADD:
191 mod = builder.buildFlowAdd();
192 break;
193 case REMOVE:
194 mod = builder.buildFlowDel();
195 break;
196 case MODIFY:
197 mod = builder.buildFlowMod();
198 break;
199 default:
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800200 log.error("Unsupported batch operation {}; skipping flowmod {}",
201 fbe.operator(), fbe);
202 continue;
203 }
Saurav Das3ea46622015-04-22 14:01:34 -0700204 sw.sendMsg(mod);
alshabib193525b2014-10-08 18:58:03 -0700205 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800206 OFBarrierRequest.Builder builder = sw.factory()
207 .buildBarrierRequest()
208 .setXid(batch.id());
209 sw.sendMsg(builder.build());
alshabib193525b2014-10-08 18:58:03 -0700210 }
211
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800212
alshabib8f1cf4a2014-09-17 14:44:48 -0700213 private class InternalFlowProvider
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800214 implements OpenFlowSwitchListener, OpenFlowEventListener {
alshabib8f1cf4a2014-09-17 14:44:48 -0700215
alshabib8f1cf4a2014-09-17 14:44:48 -0700216 @Override
217 public void switchAdded(Dpid dpid) {
alshabibba5ac482014-10-02 17:15:20 -0700218 FlowStatsCollector fsc = new FlowStatsCollector(controller.getSwitch(dpid), POLL_INTERVAL);
alshabibeec3a062014-09-17 18:01:26 -0700219 fsc.start();
220 collectors.put(dpid, fsc);
alshabib8f1cf4a2014-09-17 14:44:48 -0700221 }
222
223 @Override
224 public void switchRemoved(Dpid dpid) {
alshabibdfc7afb2014-10-21 20:13:27 -0700225 FlowStatsCollector collector = collectors.remove(dpid);
226 if (collector != null) {
227 collector.stop();
228 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700229 }
230
231 @Override
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700232 public void switchChanged(Dpid dpid) {
233 }
234
235 @Override
alshabib8f1cf4a2014-09-17 14:44:48 -0700236 public void portChanged(Dpid dpid, OFPortStatus status) {
237 //TODO: Decide whether to evict flows internal store.
238 }
239
240 @Override
241 public void handleMessage(Dpid dpid, OFMessage msg) {
alshabibda1644e2015-03-13 14:01:35 -0700242 OpenFlowSwitch sw = controller.getSwitch(dpid);
alshabib8f1cf4a2014-09-17 14:44:48 -0700243 switch (msg.getType()) {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800244 case FLOW_REMOVED:
245 OFFlowRemoved removed = (OFFlowRemoved) msg;
alshabib6b5cfec2014-09-18 17:42:18 -0700246
alshabibbdcbb102015-04-22 14:16:38 -0700247 FlowEntry fr = new FlowEntryBuilder(dpid, removed).build();
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800248 providerService.flowRemoved(fr);
249 break;
250 case STATS_REPLY:
sangho20f91162015-02-12 11:24:23 -0800251 if (((OFStatsReply) msg).getStatsType() == OFStatsType.FLOW) {
sangho89bf6fb2015-02-09 09:33:13 -0800252 pushFlowMetrics(dpid, (OFFlowStatsReply) msg);
253 }
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800254 break;
255 case BARRIER_REPLY:
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800256 try {
257 InternalCacheEntry entry = pendingBatches.getIfPresent(msg.getXid());
258 if (entry != null) {
259 providerService.batchOperationCompleted(msg.getXid(), entry.completed());
260 } else {
261 log.warn("Received unknown Barrier Reply: {}", msg.getXid());
262 }
263 } finally {
264 pendingBatches.invalidate(msg.getXid());
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800265 }
266 break;
267 case ERROR:
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800268 log.warn("received Error message {} from {}", msg, dpid);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800269
270 OFErrorMsg error = (OFErrorMsg) msg;
271 if (error.getErrType() == OFErrorType.FLOW_MOD_FAILED) {
272 OFFlowModFailedErrorMsg fmFailed = (OFFlowModFailedErrorMsg) error;
273 if (fmFailed.getData().getParsedMessage().isPresent()) {
274 OFMessage m = fmFailed.getData().getParsedMessage().get();
275 OFFlowMod fm = (OFFlowMod) m;
276 InternalCacheEntry entry = pendingBatches.getIfPresent(msg.getXid());
277 if (entry != null) {
alshabibbdcbb102015-04-22 14:16:38 -0700278 entry.appendFailure(new FlowEntryBuilder(dpid, fm)
alshabibda1644e2015-03-13 14:01:35 -0700279 .build());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800280 } else {
281 log.error("No matching batch for this error: {}", error);
282 }
283 } else {
284 //FIXME: Potentially add flowtracking to avoid this message.
285 log.error("Flow installation failed but switch didn't" +
286 " tell us which one.");
287 }
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800288 } else {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800289 log.warn("Received error {}", error);
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800290 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800291
292
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800293 default:
294 log.debug("Unhandled message type: {}", msg.getType());
alshabib8f1cf4a2014-09-17 14:44:48 -0700295 }
296
297 }
298
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700299 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700300 public void receivedRoleReply(Dpid dpid, RoleState requested,
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800301 RoleState response) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700302 // Do nothing here for now.
303 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700304
sangho89bf6fb2015-02-09 09:33:13 -0800305 private void pushFlowMetrics(Dpid dpid, OFFlowStatsReply replies) {
alshabib64def642014-12-02 23:27:37 -0800306
alshabib54ce5892014-09-23 17:50:51 -0700307 DeviceId did = DeviceId.deviceId(Dpid.uri(dpid));
Saurav Dasfa2fa932015-03-03 11:29:48 -0800308 OpenFlowSwitch sw = controller.getSwitch(dpid);
alshabib54ce5892014-09-23 17:50:51 -0700309
alshabib64def642014-12-02 23:27:37 -0800310 List<FlowEntry> flowEntries = replies.getEntries().stream()
alshabibbdcbb102015-04-22 14:16:38 -0700311 .map(entry -> new FlowEntryBuilder(dpid, entry).build())
alshabib64def642014-12-02 23:27:37 -0800312 .collect(Collectors.toList());
alshabib54ce5892014-09-23 17:50:51 -0700313
alshabib64def642014-12-02 23:27:37 -0800314 providerService.pushFlowMetrics(did, flowEntries);
315
alshabib5c370ff2014-09-18 10:12:14 -0700316 }
317
alshabib8f1cf4a2014-09-17 14:44:48 -0700318 }
alshabib1cc04f72014-09-16 16:09:58 -0700319
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800320 /**
321 * The internal cache entry holding the original request as well
322 * as accumulating the any failures along the way.
323 *
324 * If this entry is evicted from the cache then the entire operation
325 * is considered failed. Otherwise, only the failures reported by the device
326 * will be propagated up.
327 */
328 private class InternalCacheEntry {
alshabib902d41b2014-10-07 16:52:05 -0700329
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800330 private final FlowRuleBatchOperation operation;
331 private final Set<FlowRule> failures = Sets.newConcurrentHashSet();
alshabib193525b2014-10-08 18:58:03 -0700332
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800333 public InternalCacheEntry(FlowRuleBatchOperation operation) {
334 this.operation = operation;
alshabib902d41b2014-10-07 16:52:05 -0700335 }
336
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800337 /**
338 * Appends a failed rule to the set of failed items.
339 * @param rule the failed rule
340 */
341 public void appendFailure(FlowRule rule) {
342 failures.add(rule);
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800343 }
344
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800345 /**
346 * Fails the entire batch and returns the failed operation.
347 * @return the failed operation
348 */
349 public CompletedBatchOperation failedCompletion() {
350 Set<FlowRule> fails = operation.getOperations().stream()
351 .map(op -> op.target()).collect(Collectors.toSet());
352 return new CompletedBatchOperation(false, Collections.unmodifiableSet(fails), operation.deviceId());
alshabib902d41b2014-10-07 16:52:05 -0700353 }
354
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800355 /**
356 * Returns the completed operation and whether the batch suceeded.
357 * @return the completed operation
358 */
359 public CompletedBatchOperation completed() {
360 return new CompletedBatchOperation(failures.isEmpty(),
361 Collections.unmodifiableSet(failures), operation.deviceId());
alshabib902d41b2014-10-07 16:52:05 -0700362 }
363
alshabib902d41b2014-10-07 16:52:05 -0700364 }
alshabiba68eb962014-09-24 20:34:13 -0700365
alshabib1cc04f72014-09-16 16:09:58 -0700366}