blob: cabd8dc6ec1c8b1081d07b9b6db80428a8f2c09e [file] [log] [blame]
sangho5afd02a2015-02-03 20:07:35 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
sangho5afd02a2015-02-03 20:07:35 -08003 *
4 * 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
7 *
8 * 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.
15 */
16
17package org.onosproject.provider.of.group.impl;
18
jiangruia6b46092015-11-11 16:28:51 +080019import static org.slf4j.LoggerFactory.getLogger;
20
helenyrwu89470f12016-08-12 13:18:10 -070021import java.util.ArrayList;
jiangruia6b46092015-11-11 16:28:51 +080022import java.util.Collection;
helenyrwu89470f12016-08-12 13:18:10 -070023import java.util.Iterator;
24import java.util.List;
jiangruia6b46092015-11-11 16:28:51 +080025import java.util.Map;
26import java.util.Optional;
27import java.util.concurrent.atomic.AtomicLong;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070028
sangho5afd02a2015-02-03 20:07:35 -080029import org.apache.felix.scr.annotations.Activate;
30import org.apache.felix.scr.annotations.Component;
31import org.apache.felix.scr.annotations.Deactivate;
32import org.apache.felix.scr.annotations.Reference;
33import org.apache.felix.scr.annotations.ReferenceCardinality;
sangho5afd02a2015-02-03 20:07:35 -080034import org.onosproject.core.GroupId;
35import org.onosproject.net.DeviceId;
helenyrwu89470f12016-08-12 13:18:10 -070036import org.onosproject.net.PortNumber;
37import org.onosproject.net.device.DeviceService;
jiangruia6b46092015-11-11 16:28:51 +080038import org.onosproject.net.driver.DriverService;
sangho5afd02a2015-02-03 20:07:35 -080039import org.onosproject.net.group.DefaultGroup;
40import org.onosproject.net.group.Group;
helenyrwu89470f12016-08-12 13:18:10 -070041import org.onosproject.net.group.GroupBucket;
sangho5afd02a2015-02-03 20:07:35 -080042import org.onosproject.net.group.GroupBuckets;
43import org.onosproject.net.group.GroupDescription;
44import org.onosproject.net.group.GroupOperation;
Saurav Das0fd79d92016-03-07 10:58:36 -080045import org.onosproject.net.group.GroupOperation.GroupMsgErrorCode;
sangho5afd02a2015-02-03 20:07:35 -080046import org.onosproject.net.group.GroupOperations;
47import org.onosproject.net.group.GroupProvider;
48import org.onosproject.net.group.GroupProviderRegistry;
49import org.onosproject.net.group.GroupProviderService;
helenyrwu89470f12016-08-12 13:18:10 -070050import org.onosproject.net.group.GroupService;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070051import org.onosproject.net.group.StoredGroupBucketEntry;
sangho5afd02a2015-02-03 20:07:35 -080052import org.onosproject.net.provider.AbstractProvider;
53import org.onosproject.net.provider.ProviderId;
54import org.onosproject.openflow.controller.Dpid;
55import org.onosproject.openflow.controller.OpenFlowController;
56import org.onosproject.openflow.controller.OpenFlowEventListener;
57import org.onosproject.openflow.controller.OpenFlowSwitch;
58import org.onosproject.openflow.controller.OpenFlowSwitchListener;
59import org.onosproject.openflow.controller.RoleState;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070060import org.projectfloodlight.openflow.protocol.OFBucketCounter;
sangho5afd02a2015-02-03 20:07:35 -080061import org.projectfloodlight.openflow.protocol.OFErrorMsg;
62import org.projectfloodlight.openflow.protocol.OFErrorType;
63import org.projectfloodlight.openflow.protocol.OFGroupDescStatsEntry;
64import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply;
65import org.projectfloodlight.openflow.protocol.OFGroupMod;
Saurav Das0fd79d92016-03-07 10:58:36 -080066import org.projectfloodlight.openflow.protocol.OFGroupModFailedCode;
sangho5afd02a2015-02-03 20:07:35 -080067import org.projectfloodlight.openflow.protocol.OFGroupStatsEntry;
68import org.projectfloodlight.openflow.protocol.OFGroupStatsReply;
69import org.projectfloodlight.openflow.protocol.OFGroupType;
70import org.projectfloodlight.openflow.protocol.OFMessage;
helenyrwu89470f12016-08-12 13:18:10 -070071import org.projectfloodlight.openflow.protocol.OFPortDesc;
sangho5afd02a2015-02-03 20:07:35 -080072import org.projectfloodlight.openflow.protocol.OFPortStatus;
73import org.projectfloodlight.openflow.protocol.OFStatsReply;
74import org.projectfloodlight.openflow.protocol.OFStatsType;
sangho01a883c2015-02-23 11:01:50 -080075import org.projectfloodlight.openflow.protocol.OFVersion;
Saurav Das0fd79d92016-03-07 10:58:36 -080076import org.projectfloodlight.openflow.protocol.errormsg.OFGroupModFailedErrorMsg;
sangho5afd02a2015-02-03 20:07:35 -080077import org.slf4j.Logger;
78
jiangruia6b46092015-11-11 16:28:51 +080079import com.google.common.collect.Maps;
sangho5afd02a2015-02-03 20:07:35 -080080
81/**
82 * Provider which uses an OpenFlow controller to handle Group.
83 */
84@Component(immediate = true)
85public class OpenFlowGroupProvider extends AbstractProvider implements GroupProvider {
86
87 private final Logger log = getLogger(getClass());
88
89 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
90 protected OpenFlowController controller;
91
92 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
93 protected GroupProviderRegistry providerRegistry;
94
jiangruia6b46092015-11-11 16:28:51 +080095 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 protected DriverService driverService;
97
helenyrwu89470f12016-08-12 13:18:10 -070098 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
99 protected DeviceService deviceService;
100
101 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
102 protected GroupService groupService;
103
sangho5afd02a2015-02-03 20:07:35 -0800104 private GroupProviderService providerService;
105
106 static final int POLL_INTERVAL = 10;
107
108 private final InternalGroupProvider listener = new InternalGroupProvider();
109
sanghoa09f2742015-02-06 14:49:47 -0800110 private static final AtomicLong XID_COUNTER = new AtomicLong(1);
sangho5afd02a2015-02-03 20:07:35 -0800111 private final Map<Dpid, GroupStatsCollector> collectors = Maps.newHashMap();
sanghoa09f2742015-02-06 14:49:47 -0800112 private final Map<Long, OFStatsReply> groupStats = Maps.newConcurrentMap();
sangho22a805d2015-02-13 09:45:43 -0800113 private final Map<GroupId, GroupOperation> pendingGroupOperations =
sangho5afd02a2015-02-03 20:07:35 -0800114 Maps.newConcurrentMap();
115
sanghoa09f2742015-02-06 14:49:47 -0800116 /* Map<Group ID, Transaction ID> */
sangho22a805d2015-02-13 09:45:43 -0800117 private final Map<GroupId, Long> pendingXidMaps = Maps.newConcurrentMap();
sanghoa09f2742015-02-06 14:49:47 -0800118
sangho5afd02a2015-02-03 20:07:35 -0800119 /**
120 * Creates a OpenFlow group provider.
121 */
122 public OpenFlowGroupProvider() {
123 super(new ProviderId("of", "org.onosproject.provider.group"));
124 }
125
126 @Activate
127 public void activate() {
128 providerService = providerRegistry.register(this);
129 controller.addListener(listener);
130 controller.addEventListener(listener);
131
132 for (OpenFlowSwitch sw : controller.getSwitches()) {
sangho01a883c2015-02-23 11:01:50 -0800133 if (isGroupSupported(sw)) {
134 GroupStatsCollector gsc = new GroupStatsCollector(sw, POLL_INTERVAL);
135 gsc.start();
136 collectors.put(new Dpid(sw.getId()), gsc);
137 }
sangho5afd02a2015-02-03 20:07:35 -0800138 }
139
140 log.info("Started");
141 }
142
143 @Deactivate
144 public void deactivate() {
145 providerRegistry.unregister(this);
146 providerService = null;
Charles Chanecfdfb72015-11-24 19:05:50 -0800147 collectors.values().forEach(GroupStatsCollector::stop);
148 collectors.clear();
sangho5afd02a2015-02-03 20:07:35 -0800149 log.info("Stopped");
150 }
151
152 @Override
153 public void performGroupOperation(DeviceId deviceId, GroupOperations groupOps) {
sangho5afd02a2015-02-03 20:07:35 -0800154 final Dpid dpid = Dpid.dpid(deviceId.uri());
155 OpenFlowSwitch sw = controller.getSwitch(dpid);
156 for (GroupOperation groupOperation: groupOps.operations()) {
157 if (sw == null) {
sanghoa09f2742015-02-06 14:49:47 -0800158 log.error("SW {} is not found", dpid);
sangho5afd02a2015-02-03 20:07:35 -0800159 return;
160 }
sanghoa09f2742015-02-06 14:49:47 -0800161 final Long groupModXid = XID_COUNTER.getAndIncrement();
jiangruia6b46092015-11-11 16:28:51 +0800162 GroupModBuilder builder = null;
163 if (driverService == null) {
164 builder = GroupModBuilder.builder(groupOperation.buckets(),
165 groupOperation.groupId(),
166 groupOperation.groupType(),
167 sw.factory(),
168 Optional.of(groupModXid));
169 } else {
170 builder = GroupModBuilder.builder(groupOperation.buckets(),
171 groupOperation.groupId(),
172 groupOperation.groupType(),
173 sw.factory(),
174 Optional.of(groupModXid),
175 Optional.of(driverService));
176 }
sangho5afd02a2015-02-03 20:07:35 -0800177 OFGroupMod groupMod = null;
178 switch (groupOperation.opType()) {
179 case ADD:
180 groupMod = builder.buildGroupAdd();
181 break;
182 case MODIFY:
183 groupMod = builder.buildGroupMod();
184 break;
185 case DELETE:
186 groupMod = builder.buildGroupDel();
187 break;
188 default:
189 log.error("Unsupported Group operation");
Satish Ke6d91c52015-11-21 19:04:20 +0530190 return;
sangho5afd02a2015-02-03 20:07:35 -0800191 }
192 sw.sendMsg(groupMod);
Yi Tsengfa394de2017-02-01 11:26:40 -0800193 GroupId groudId = new GroupId(groupMod.getGroup().getGroupNumber());
sangho22a805d2015-02-13 09:45:43 -0800194 pendingGroupOperations.put(groudId, groupOperation);
195 pendingXidMaps.put(groudId, groupModXid);
sangho5afd02a2015-02-03 20:07:35 -0800196 }
197 }
198
199 private void pushGroupMetrics(Dpid dpid, OFStatsReply statsReply) {
200 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
201
202 OFGroupStatsReply groupStatsReply = null;
203 OFGroupDescStatsReply groupDescStatsReply = null;
204
sanghoa09f2742015-02-06 14:49:47 -0800205 synchronized (groupStats) {
206 if (statsReply.getStatsType() == OFStatsType.GROUP) {
207 OFStatsReply reply = groupStats.get(statsReply.getXid() + 1);
208 if (reply != null) {
209 groupStatsReply = (OFGroupStatsReply) statsReply;
210 groupDescStatsReply = (OFGroupDescStatsReply) reply;
211 groupStats.remove(statsReply.getXid() + 1);
212 } else {
213 groupStats.put(statsReply.getXid(), statsReply);
214 }
215 } else if (statsReply.getStatsType() == OFStatsType.GROUP_DESC) {
216 OFStatsReply reply = groupStats.get(statsReply.getXid() - 1);
217 if (reply != null) {
218 groupStatsReply = (OFGroupStatsReply) reply;
219 groupDescStatsReply = (OFGroupDescStatsReply) statsReply;
220 groupStats.remove(statsReply.getXid() - 1);
221 } else {
222 groupStats.put(statsReply.getXid(), statsReply);
223 }
sangho5afd02a2015-02-03 20:07:35 -0800224 }
225 }
226
Ray Milkey0ae473d2016-04-04 10:56:47 -0700227 if (providerService != null && groupStatsReply != null) {
sangho5afd02a2015-02-03 20:07:35 -0800228 Collection<Group> groups = buildGroupMetrics(deviceId,
229 groupStatsReply, groupDescStatsReply);
230 providerService.pushGroupMetrics(deviceId, groups);
231 for (Group group: groups) {
232 pendingGroupOperations.remove(group.id());
sanghoa09f2742015-02-06 14:49:47 -0800233 pendingXidMaps.remove(group.id());
sangho5afd02a2015-02-03 20:07:35 -0800234 }
235 }
236 }
237
238 private Collection<Group> buildGroupMetrics(DeviceId deviceId,
239 OFGroupStatsReply groupStatsReply,
240 OFGroupDescStatsReply groupDescStatsReply) {
241
242 Map<Integer, Group> groups = Maps.newHashMap();
Hyunsun Moona834c182015-12-16 03:01:56 -0800243 Dpid dpid = Dpid.dpid(deviceId.uri());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700244
sangho5afd02a2015-02-03 20:07:35 -0800245 for (OFGroupDescStatsEntry entry: groupDescStatsReply.getEntries()) {
246 int id = entry.getGroup().getGroupNumber();
Yi Tsengfa394de2017-02-01 11:26:40 -0800247 GroupId groupId = new GroupId(id);
sangho5afd02a2015-02-03 20:07:35 -0800248 GroupDescription.Type type = getGroupType(entry.getGroupType());
Hyunsun Moona834c182015-12-16 03:01:56 -0800249 GroupBuckets buckets = new GroupBucketEntryBuilder(dpid, entry.getBuckets(),
250 entry.getGroupType(), driverService).build();
sangho5afd02a2015-02-03 20:07:35 -0800251 DefaultGroup group = new DefaultGroup(groupId, deviceId, type, buckets);
252 groups.put(id, group);
253 }
254
255 for (OFGroupStatsEntry entry: groupStatsReply.getEntries()) {
256 int groupId = entry.getGroup().getGroupNumber();
257 DefaultGroup group = (DefaultGroup) groups.get(groupId);
258 if (group != null) {
259 group.setBytes(entry.getByteCount().getValue());
260 group.setLife(entry.getDurationSec());
261 group.setPackets(entry.getPacketCount().getValue());
262 group.setReferenceCount(entry.getRefCount());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700263 int bucketIndex = 0;
264 for (OFBucketCounter bucketStats:entry.getBucketStats()) {
265 ((StoredGroupBucketEntry) group.buckets().buckets()
266 .get(bucketIndex))
267 .setPackets(bucketStats
268 .getPacketCount().getValue());
269 ((StoredGroupBucketEntry) group.buckets().buckets()
270 .get(bucketIndex))
271 .setBytes(entry.getBucketStats()
272 .get(bucketIndex)
273 .getByteCount().getValue());
274 bucketIndex++;
275 }
sangho5afd02a2015-02-03 20:07:35 -0800276 }
277 }
278
279 return groups.values();
280 }
281
282 private GroupDescription.Type getGroupType(OFGroupType type) {
283 switch (type) {
284 case ALL:
Charles Chanc42e84e2015-10-20 16:24:19 -0700285 return GroupDescription.Type.ALL;
sangho5afd02a2015-02-03 20:07:35 -0800286 case INDIRECT:
287 return GroupDescription.Type.INDIRECT;
288 case SELECT:
289 return GroupDescription.Type.SELECT;
290 case FF:
291 return GroupDescription.Type.FAILOVER;
292 default:
293 log.error("Unsupported OF group type : {}", type);
294 break;
295 }
296 return null;
297 }
298
sanghoa09f2742015-02-06 14:49:47 -0800299 /**
300 * Returns a transaction ID for entire group operations and increases
301 * the counter by the number given.
302 *
303 * @param increase the amount to increase the counter by
304 * @return a transaction ID
305 */
306 public static long getXidAndAdd(int increase) {
307 return XID_COUNTER.getAndAdd(increase);
308 }
309
sangho01a883c2015-02-23 11:01:50 -0800310 private boolean isGroupSupported(OpenFlowSwitch sw) {
311 if (sw.factory().getVersion() == OFVersion.OF_10 ||
312 sw.factory().getVersion() == OFVersion.OF_11 ||
313 sw.factory().getVersion() == OFVersion.OF_12) {
314 return false;
315 }
316
317 return true;
318 }
319
sangho5afd02a2015-02-03 20:07:35 -0800320 private class InternalGroupProvider
321 implements OpenFlowSwitchListener, OpenFlowEventListener {
322
323 @Override
324 public void handleMessage(Dpid dpid, OFMessage msg) {
325 switch (msg.getType()) {
326 case STATS_REPLY:
327 pushGroupMetrics(dpid, (OFStatsReply) msg);
328 break;
329 case ERROR:
330 OFErrorMsg errorMsg = (OFErrorMsg) msg;
331 if (errorMsg.getErrType() == OFErrorType.GROUP_MOD_FAILED) {
sangho22a805d2015-02-13 09:45:43 -0800332 GroupId pendingGroupId = null;
333 for (Map.Entry<GroupId, Long> entry: pendingXidMaps.entrySet()) {
sanghoa09f2742015-02-06 14:49:47 -0800334 if (entry.getValue() == errorMsg.getXid()) {
335 pendingGroupId = entry.getKey();
336 break;
337 }
338 }
sangho22a805d2015-02-13 09:45:43 -0800339 if (pendingGroupId == null) {
sanghoa09f2742015-02-06 14:49:47 -0800340 log.warn("Error for unknown group operation: {}",
341 errorMsg.getXid());
342 } else {
343 GroupOperation operation =
344 pendingGroupOperations.get(pendingGroupId);
sangho7ff01812015-02-09 16:21:53 -0800345 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
sanghoa09f2742015-02-06 14:49:47 -0800346 if (operation != null) {
Saurav Das0fd79d92016-03-07 10:58:36 -0800347 OFGroupModFailedCode code =
348 ((OFGroupModFailedErrorMsg) errorMsg).getCode();
349 GroupMsgErrorCode failureCode =
350 GroupMsgErrorCode.values()[(code.ordinal())];
351 GroupOperation failedOperation = GroupOperation
352 .createFailedGroupOperation(operation, failureCode);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800353 log.warn("Received a group mod error {}", msg);
sangho7ff01812015-02-09 16:21:53 -0800354 providerService.groupOperationFailed(deviceId,
Saurav Das0fd79d92016-03-07 10:58:36 -0800355 failedOperation);
sanghoa09f2742015-02-06 14:49:47 -0800356 pendingGroupOperations.remove(pendingGroupId);
357 pendingXidMaps.remove(pendingGroupId);
sanghoa09f2742015-02-06 14:49:47 -0800358 } else {
359 log.error("Cannot find pending group operation with group ID: {}",
360 pendingGroupId);
361 }
sangho5afd02a2015-02-03 20:07:35 -0800362 }
363 break;
364 }
365 default:
Jonathan Hart7baba072015-02-23 14:27:59 -0800366 break;
sangho5afd02a2015-02-03 20:07:35 -0800367 }
368 }
369
370 @Override
371 public void switchAdded(Dpid dpid) {
sangho01a883c2015-02-23 11:01:50 -0800372 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500373 if (sw == null) {
374 return;
375 }
sangho01a883c2015-02-23 11:01:50 -0800376 if (isGroupSupported(sw)) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800377 GroupStatsCollector gsc = new GroupStatsCollector(sw, POLL_INTERVAL);
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700378 stopCollectorIfNeeded(collectors.put(dpid, gsc));
Palash Kalaa439afe2017-05-16 14:53:15 +0900379 gsc.start();
sangho01a883c2015-02-23 11:01:50 -0800380 }
Lei Xudee1aff2015-10-16 00:45:10 -0500381
382 //figure out race condition
383 if (controller.getSwitch(dpid) == null) {
384 switchRemoved(dpid);
385 }
sangho5afd02a2015-02-03 20:07:35 -0800386 }
387
388 @Override
389 public void switchRemoved(Dpid dpid) {
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700390 stopCollectorIfNeeded(collectors.remove(dpid));
391 }
392
393 private void stopCollectorIfNeeded(GroupStatsCollector collector) {
sangho5afd02a2015-02-03 20:07:35 -0800394 if (collector != null) {
395 collector.stop();
396 }
397 }
398
399 @Override
400 public void switchChanged(Dpid dpid) {
401 }
402
403 @Override
404 public void portChanged(Dpid dpid, OFPortStatus status) {
helenyrwu89470f12016-08-12 13:18:10 -0700405 providerService.notifyOfFailovers(checkFailoverGroups(dpid, status));
sangho5afd02a2015-02-03 20:07:35 -0800406 }
407
408 @Override
409 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
410 }
411 }
412
helenyrwu89470f12016-08-12 13:18:10 -0700413 /**
414 * Builds a list of failover Groups whose primary live bucket failed over
415 * (i.e. bucket in use has changed).
416 *
417 * @param dpid DPID of switch whose port's status changed
418 * @param status new status of port
419 * @return list of groups whose primary live bucket failed over
420 */
421 private List<Group> checkFailoverGroups(Dpid dpid, OFPortStatus status) {
422 List<Group> groupList = new ArrayList<>();
423 OFPortDesc desc = status.getDesc();
424 PortNumber portNumber = PortNumber.portNumber(desc.getPortNo().getPortNumber());
425 DeviceId id = DeviceId.deviceId(Dpid.uri(dpid));
426 if (desc.isEnabled()) {
427 return groupList;
428 }
429 Iterator<Group> iterator = groupService.getGroups(id).iterator();
430 while (iterator.hasNext()) {
431 Group group = iterator.next();
432 if (group.type() == GroupDescription.Type.FAILOVER &&
433 checkFailoverGroup(group, id, portNumber)) {
434 groupList.add(group);
435 }
436 }
437 return groupList;
438 }
439
440 /**
441 * Checks whether the first live port in the failover group's bucket
442 * has failed over.
443 *
444 * @param group failover group to be checked for failover
445 * @param id device ID of switch whose port's status changed
446 * @param portNumber port number of port that was disabled
447 * @return whether the failover group experienced failover
448 */
449 private boolean checkFailoverGroup(Group group, DeviceId id,
450 PortNumber portNumber) {
451 boolean portReached = false;
452 boolean portEnabled = false;
453 Iterator<GroupBucket> bIterator = group.buckets().buckets().iterator();
454 GroupBucket bucket;
455 while (bIterator.hasNext() && !portReached) {
456 bucket = bIterator.next();
457 if (deviceService.getPort(id, bucket.watchPort()).isEnabled()) {
458 portEnabled = true;
459 }
460 if (bucket.watchPort().equals(portNumber)) {
461 portReached = true;
462 }
463 }
464 return portReached && !portEnabled;
465 }
466
sangho5afd02a2015-02-03 20:07:35 -0800467}