blob: 159b7ddde86448d35b56ce0673e3f2d9e4ddb6a6 [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;
34import org.onosproject.core.DefaultGroupId;
35import org.onosproject.core.GroupId;
36import org.onosproject.net.DeviceId;
helenyrwu89470f12016-08-12 13:18:10 -070037import org.onosproject.net.PortNumber;
38import org.onosproject.net.device.DeviceService;
jiangruia6b46092015-11-11 16:28:51 +080039import org.onosproject.net.driver.DriverService;
sangho5afd02a2015-02-03 20:07:35 -080040import org.onosproject.net.group.DefaultGroup;
41import org.onosproject.net.group.Group;
helenyrwu89470f12016-08-12 13:18:10 -070042import org.onosproject.net.group.GroupBucket;
sangho5afd02a2015-02-03 20:07:35 -080043import org.onosproject.net.group.GroupBuckets;
44import org.onosproject.net.group.GroupDescription;
45import org.onosproject.net.group.GroupOperation;
Saurav Das0fd79d92016-03-07 10:58:36 -080046import org.onosproject.net.group.GroupOperation.GroupMsgErrorCode;
sangho5afd02a2015-02-03 20:07:35 -080047import org.onosproject.net.group.GroupOperations;
48import org.onosproject.net.group.GroupProvider;
49import org.onosproject.net.group.GroupProviderRegistry;
50import org.onosproject.net.group.GroupProviderService;
helenyrwu89470f12016-08-12 13:18:10 -070051import org.onosproject.net.group.GroupService;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070052import org.onosproject.net.group.StoredGroupBucketEntry;
sangho5afd02a2015-02-03 20:07:35 -080053import org.onosproject.net.provider.AbstractProvider;
54import org.onosproject.net.provider.ProviderId;
55import org.onosproject.openflow.controller.Dpid;
56import org.onosproject.openflow.controller.OpenFlowController;
57import org.onosproject.openflow.controller.OpenFlowEventListener;
58import org.onosproject.openflow.controller.OpenFlowSwitch;
59import org.onosproject.openflow.controller.OpenFlowSwitchListener;
60import org.onosproject.openflow.controller.RoleState;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070061import org.projectfloodlight.openflow.protocol.OFBucketCounter;
sangho5afd02a2015-02-03 20:07:35 -080062import org.projectfloodlight.openflow.protocol.OFErrorMsg;
63import org.projectfloodlight.openflow.protocol.OFErrorType;
64import org.projectfloodlight.openflow.protocol.OFGroupDescStatsEntry;
65import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply;
66import org.projectfloodlight.openflow.protocol.OFGroupMod;
Saurav Das0fd79d92016-03-07 10:58:36 -080067import org.projectfloodlight.openflow.protocol.OFGroupModFailedCode;
sangho5afd02a2015-02-03 20:07:35 -080068import org.projectfloodlight.openflow.protocol.OFGroupStatsEntry;
69import org.projectfloodlight.openflow.protocol.OFGroupStatsReply;
70import org.projectfloodlight.openflow.protocol.OFGroupType;
71import org.projectfloodlight.openflow.protocol.OFMessage;
helenyrwu89470f12016-08-12 13:18:10 -070072import org.projectfloodlight.openflow.protocol.OFPortDesc;
sangho5afd02a2015-02-03 20:07:35 -080073import org.projectfloodlight.openflow.protocol.OFPortStatus;
74import org.projectfloodlight.openflow.protocol.OFStatsReply;
75import org.projectfloodlight.openflow.protocol.OFStatsType;
sangho01a883c2015-02-23 11:01:50 -080076import org.projectfloodlight.openflow.protocol.OFVersion;
Saurav Das0fd79d92016-03-07 10:58:36 -080077import org.projectfloodlight.openflow.protocol.errormsg.OFGroupModFailedErrorMsg;
sangho5afd02a2015-02-03 20:07:35 -080078import org.slf4j.Logger;
79
jiangruia6b46092015-11-11 16:28:51 +080080import com.google.common.collect.Maps;
sangho5afd02a2015-02-03 20:07:35 -080081
82/**
83 * Provider which uses an OpenFlow controller to handle Group.
84 */
85@Component(immediate = true)
86public class OpenFlowGroupProvider extends AbstractProvider implements GroupProvider {
87
88 private final Logger log = getLogger(getClass());
89
90 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
91 protected OpenFlowController controller;
92
93 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
94 protected GroupProviderRegistry providerRegistry;
95
jiangruia6b46092015-11-11 16:28:51 +080096 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
97 protected DriverService driverService;
98
helenyrwu89470f12016-08-12 13:18:10 -070099 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
100 protected DeviceService deviceService;
101
102 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
103 protected GroupService groupService;
104
sangho5afd02a2015-02-03 20:07:35 -0800105 private GroupProviderService providerService;
106
107 static final int POLL_INTERVAL = 10;
108
109 private final InternalGroupProvider listener = new InternalGroupProvider();
110
sanghoa09f2742015-02-06 14:49:47 -0800111 private static final AtomicLong XID_COUNTER = new AtomicLong(1);
sangho5afd02a2015-02-03 20:07:35 -0800112 private final Map<Dpid, GroupStatsCollector> collectors = Maps.newHashMap();
sanghoa09f2742015-02-06 14:49:47 -0800113 private final Map<Long, OFStatsReply> groupStats = Maps.newConcurrentMap();
sangho22a805d2015-02-13 09:45:43 -0800114 private final Map<GroupId, GroupOperation> pendingGroupOperations =
sangho5afd02a2015-02-03 20:07:35 -0800115 Maps.newConcurrentMap();
116
sanghoa09f2742015-02-06 14:49:47 -0800117 /* Map<Group ID, Transaction ID> */
sangho22a805d2015-02-13 09:45:43 -0800118 private final Map<GroupId, Long> pendingXidMaps = Maps.newConcurrentMap();
sanghoa09f2742015-02-06 14:49:47 -0800119
sangho5afd02a2015-02-03 20:07:35 -0800120 /**
121 * Creates a OpenFlow group provider.
122 */
123 public OpenFlowGroupProvider() {
124 super(new ProviderId("of", "org.onosproject.provider.group"));
125 }
126
127 @Activate
128 public void activate() {
129 providerService = providerRegistry.register(this);
130 controller.addListener(listener);
131 controller.addEventListener(listener);
132
133 for (OpenFlowSwitch sw : controller.getSwitches()) {
sangho01a883c2015-02-23 11:01:50 -0800134 if (isGroupSupported(sw)) {
135 GroupStatsCollector gsc = new GroupStatsCollector(sw, POLL_INTERVAL);
136 gsc.start();
137 collectors.put(new Dpid(sw.getId()), gsc);
138 }
sangho5afd02a2015-02-03 20:07:35 -0800139 }
140
141 log.info("Started");
142 }
143
144 @Deactivate
145 public void deactivate() {
146 providerRegistry.unregister(this);
147 providerService = null;
Charles Chanecfdfb72015-11-24 19:05:50 -0800148 collectors.values().forEach(GroupStatsCollector::stop);
149 collectors.clear();
sangho5afd02a2015-02-03 20:07:35 -0800150 log.info("Stopped");
151 }
152
153 @Override
154 public void performGroupOperation(DeviceId deviceId, GroupOperations groupOps) {
sangho5afd02a2015-02-03 20:07:35 -0800155 final Dpid dpid = Dpid.dpid(deviceId.uri());
156 OpenFlowSwitch sw = controller.getSwitch(dpid);
157 for (GroupOperation groupOperation: groupOps.operations()) {
158 if (sw == null) {
sanghoa09f2742015-02-06 14:49:47 -0800159 log.error("SW {} is not found", dpid);
sangho5afd02a2015-02-03 20:07:35 -0800160 return;
161 }
sanghoa09f2742015-02-06 14:49:47 -0800162 final Long groupModXid = XID_COUNTER.getAndIncrement();
jiangruia6b46092015-11-11 16:28:51 +0800163 GroupModBuilder builder = null;
164 if (driverService == null) {
165 builder = GroupModBuilder.builder(groupOperation.buckets(),
166 groupOperation.groupId(),
167 groupOperation.groupType(),
168 sw.factory(),
169 Optional.of(groupModXid));
170 } else {
171 builder = GroupModBuilder.builder(groupOperation.buckets(),
172 groupOperation.groupId(),
173 groupOperation.groupType(),
174 sw.factory(),
175 Optional.of(groupModXid),
176 Optional.of(driverService));
177 }
sangho5afd02a2015-02-03 20:07:35 -0800178 OFGroupMod groupMod = null;
179 switch (groupOperation.opType()) {
180 case ADD:
181 groupMod = builder.buildGroupAdd();
182 break;
183 case MODIFY:
184 groupMod = builder.buildGroupMod();
185 break;
186 case DELETE:
187 groupMod = builder.buildGroupDel();
188 break;
189 default:
190 log.error("Unsupported Group operation");
Satish Ke6d91c52015-11-21 19:04:20 +0530191 return;
sangho5afd02a2015-02-03 20:07:35 -0800192 }
193 sw.sendMsg(groupMod);
sangho22a805d2015-02-13 09:45:43 -0800194 GroupId groudId = new DefaultGroupId(groupMod.getGroup().getGroupNumber());
195 pendingGroupOperations.put(groudId, groupOperation);
196 pendingXidMaps.put(groudId, groupModXid);
sangho5afd02a2015-02-03 20:07:35 -0800197 }
198 }
199
200 private void pushGroupMetrics(Dpid dpid, OFStatsReply statsReply) {
201 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
202
203 OFGroupStatsReply groupStatsReply = null;
204 OFGroupDescStatsReply groupDescStatsReply = null;
205
sanghoa09f2742015-02-06 14:49:47 -0800206 synchronized (groupStats) {
207 if (statsReply.getStatsType() == OFStatsType.GROUP) {
208 OFStatsReply reply = groupStats.get(statsReply.getXid() + 1);
209 if (reply != null) {
210 groupStatsReply = (OFGroupStatsReply) statsReply;
211 groupDescStatsReply = (OFGroupDescStatsReply) reply;
212 groupStats.remove(statsReply.getXid() + 1);
213 } else {
214 groupStats.put(statsReply.getXid(), statsReply);
215 }
216 } else if (statsReply.getStatsType() == OFStatsType.GROUP_DESC) {
217 OFStatsReply reply = groupStats.get(statsReply.getXid() - 1);
218 if (reply != null) {
219 groupStatsReply = (OFGroupStatsReply) reply;
220 groupDescStatsReply = (OFGroupDescStatsReply) statsReply;
221 groupStats.remove(statsReply.getXid() - 1);
222 } else {
223 groupStats.put(statsReply.getXid(), statsReply);
224 }
sangho5afd02a2015-02-03 20:07:35 -0800225 }
226 }
227
Ray Milkey0ae473d2016-04-04 10:56:47 -0700228 if (providerService != null && groupStatsReply != null) {
sangho5afd02a2015-02-03 20:07:35 -0800229 Collection<Group> groups = buildGroupMetrics(deviceId,
230 groupStatsReply, groupDescStatsReply);
231 providerService.pushGroupMetrics(deviceId, groups);
232 for (Group group: groups) {
233 pendingGroupOperations.remove(group.id());
sanghoa09f2742015-02-06 14:49:47 -0800234 pendingXidMaps.remove(group.id());
sangho5afd02a2015-02-03 20:07:35 -0800235 }
236 }
237 }
238
239 private Collection<Group> buildGroupMetrics(DeviceId deviceId,
240 OFGroupStatsReply groupStatsReply,
241 OFGroupDescStatsReply groupDescStatsReply) {
242
243 Map<Integer, Group> groups = Maps.newHashMap();
Hyunsun Moona834c182015-12-16 03:01:56 -0800244 Dpid dpid = Dpid.dpid(deviceId.uri());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700245
sangho5afd02a2015-02-03 20:07:35 -0800246 for (OFGroupDescStatsEntry entry: groupDescStatsReply.getEntries()) {
247 int id = entry.getGroup().getGroupNumber();
248 GroupId groupId = new DefaultGroupId(id);
249 GroupDescription.Type type = getGroupType(entry.getGroupType());
Hyunsun Moona834c182015-12-16 03:01:56 -0800250 GroupBuckets buckets = new GroupBucketEntryBuilder(dpid, entry.getBuckets(),
251 entry.getGroupType(), driverService).build();
sangho5afd02a2015-02-03 20:07:35 -0800252 DefaultGroup group = new DefaultGroup(groupId, deviceId, type, buckets);
253 groups.put(id, group);
254 }
255
256 for (OFGroupStatsEntry entry: groupStatsReply.getEntries()) {
257 int groupId = entry.getGroup().getGroupNumber();
258 DefaultGroup group = (DefaultGroup) groups.get(groupId);
259 if (group != null) {
260 group.setBytes(entry.getByteCount().getValue());
261 group.setLife(entry.getDurationSec());
262 group.setPackets(entry.getPacketCount().getValue());
263 group.setReferenceCount(entry.getRefCount());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700264 int bucketIndex = 0;
265 for (OFBucketCounter bucketStats:entry.getBucketStats()) {
266 ((StoredGroupBucketEntry) group.buckets().buckets()
267 .get(bucketIndex))
268 .setPackets(bucketStats
269 .getPacketCount().getValue());
270 ((StoredGroupBucketEntry) group.buckets().buckets()
271 .get(bucketIndex))
272 .setBytes(entry.getBucketStats()
273 .get(bucketIndex)
274 .getByteCount().getValue());
275 bucketIndex++;
276 }
sangho5afd02a2015-02-03 20:07:35 -0800277 }
278 }
279
280 return groups.values();
281 }
282
283 private GroupDescription.Type getGroupType(OFGroupType type) {
284 switch (type) {
285 case ALL:
Charles Chanc42e84e2015-10-20 16:24:19 -0700286 return GroupDescription.Type.ALL;
sangho5afd02a2015-02-03 20:07:35 -0800287 case INDIRECT:
288 return GroupDescription.Type.INDIRECT;
289 case SELECT:
290 return GroupDescription.Type.SELECT;
291 case FF:
292 return GroupDescription.Type.FAILOVER;
293 default:
294 log.error("Unsupported OF group type : {}", type);
295 break;
296 }
297 return null;
298 }
299
sanghoa09f2742015-02-06 14:49:47 -0800300 /**
301 * Returns a transaction ID for entire group operations and increases
302 * the counter by the number given.
303 *
304 * @param increase the amount to increase the counter by
305 * @return a transaction ID
306 */
307 public static long getXidAndAdd(int increase) {
308 return XID_COUNTER.getAndAdd(increase);
309 }
310
sangho01a883c2015-02-23 11:01:50 -0800311 private boolean isGroupSupported(OpenFlowSwitch sw) {
312 if (sw.factory().getVersion() == OFVersion.OF_10 ||
313 sw.factory().getVersion() == OFVersion.OF_11 ||
314 sw.factory().getVersion() == OFVersion.OF_12) {
315 return false;
316 }
317
318 return true;
319 }
320
sangho5afd02a2015-02-03 20:07:35 -0800321 private class InternalGroupProvider
322 implements OpenFlowSwitchListener, OpenFlowEventListener {
323
324 @Override
325 public void handleMessage(Dpid dpid, OFMessage msg) {
326 switch (msg.getType()) {
327 case STATS_REPLY:
328 pushGroupMetrics(dpid, (OFStatsReply) msg);
329 break;
330 case ERROR:
331 OFErrorMsg errorMsg = (OFErrorMsg) msg;
332 if (errorMsg.getErrType() == OFErrorType.GROUP_MOD_FAILED) {
sangho22a805d2015-02-13 09:45:43 -0800333 GroupId pendingGroupId = null;
334 for (Map.Entry<GroupId, Long> entry: pendingXidMaps.entrySet()) {
sanghoa09f2742015-02-06 14:49:47 -0800335 if (entry.getValue() == errorMsg.getXid()) {
336 pendingGroupId = entry.getKey();
337 break;
338 }
339 }
sangho22a805d2015-02-13 09:45:43 -0800340 if (pendingGroupId == null) {
sanghoa09f2742015-02-06 14:49:47 -0800341 log.warn("Error for unknown group operation: {}",
342 errorMsg.getXid());
343 } else {
344 GroupOperation operation =
345 pendingGroupOperations.get(pendingGroupId);
sangho7ff01812015-02-09 16:21:53 -0800346 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
sanghoa09f2742015-02-06 14:49:47 -0800347 if (operation != null) {
Saurav Das0fd79d92016-03-07 10:58:36 -0800348 OFGroupModFailedCode code =
349 ((OFGroupModFailedErrorMsg) errorMsg).getCode();
350 GroupMsgErrorCode failureCode =
351 GroupMsgErrorCode.values()[(code.ordinal())];
352 GroupOperation failedOperation = GroupOperation
353 .createFailedGroupOperation(operation, failureCode);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800354 log.warn("Received a group mod error {}", msg);
sangho7ff01812015-02-09 16:21:53 -0800355 providerService.groupOperationFailed(deviceId,
Saurav Das0fd79d92016-03-07 10:58:36 -0800356 failedOperation);
sanghoa09f2742015-02-06 14:49:47 -0800357 pendingGroupOperations.remove(pendingGroupId);
358 pendingXidMaps.remove(pendingGroupId);
sanghoa09f2742015-02-06 14:49:47 -0800359 } else {
360 log.error("Cannot find pending group operation with group ID: {}",
361 pendingGroupId);
362 }
sangho5afd02a2015-02-03 20:07:35 -0800363 }
364 break;
365 }
366 default:
Jonathan Hart7baba072015-02-23 14:27:59 -0800367 break;
sangho5afd02a2015-02-03 20:07:35 -0800368 }
369 }
370
371 @Override
372 public void switchAdded(Dpid dpid) {
sangho01a883c2015-02-23 11:01:50 -0800373 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500374 if (sw == null) {
375 return;
376 }
sangho01a883c2015-02-23 11:01:50 -0800377 if (isGroupSupported(sw)) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800378 GroupStatsCollector gsc = new GroupStatsCollector(sw, POLL_INTERVAL);
sangho01a883c2015-02-23 11:01:50 -0800379 gsc.start();
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700380 stopCollectorIfNeeded(collectors.put(dpid, gsc));
sangho01a883c2015-02-23 11:01:50 -0800381 }
Lei Xudee1aff2015-10-16 00:45:10 -0500382
383 //figure out race condition
384 if (controller.getSwitch(dpid) == null) {
385 switchRemoved(dpid);
386 }
sangho5afd02a2015-02-03 20:07:35 -0800387 }
388
389 @Override
390 public void switchRemoved(Dpid dpid) {
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700391 stopCollectorIfNeeded(collectors.remove(dpid));
392 }
393
394 private void stopCollectorIfNeeded(GroupStatsCollector collector) {
sangho5afd02a2015-02-03 20:07:35 -0800395 if (collector != null) {
396 collector.stop();
397 }
398 }
399
400 @Override
401 public void switchChanged(Dpid dpid) {
402 }
403
404 @Override
405 public void portChanged(Dpid dpid, OFPortStatus status) {
helenyrwu89470f12016-08-12 13:18:10 -0700406 providerService.notifyOfFailovers(checkFailoverGroups(dpid, status));
sangho5afd02a2015-02-03 20:07:35 -0800407 }
408
409 @Override
410 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
411 }
412 }
413
helenyrwu89470f12016-08-12 13:18:10 -0700414 /**
415 * Builds a list of failover Groups whose primary live bucket failed over
416 * (i.e. bucket in use has changed).
417 *
418 * @param dpid DPID of switch whose port's status changed
419 * @param status new status of port
420 * @return list of groups whose primary live bucket failed over
421 */
422 private List<Group> checkFailoverGroups(Dpid dpid, OFPortStatus status) {
423 List<Group> groupList = new ArrayList<>();
424 OFPortDesc desc = status.getDesc();
425 PortNumber portNumber = PortNumber.portNumber(desc.getPortNo().getPortNumber());
426 DeviceId id = DeviceId.deviceId(Dpid.uri(dpid));
427 if (desc.isEnabled()) {
428 return groupList;
429 }
430 Iterator<Group> iterator = groupService.getGroups(id).iterator();
431 while (iterator.hasNext()) {
432 Group group = iterator.next();
433 if (group.type() == GroupDescription.Type.FAILOVER &&
434 checkFailoverGroup(group, id, portNumber)) {
435 groupList.add(group);
436 }
437 }
438 return groupList;
439 }
440
441 /**
442 * Checks whether the first live port in the failover group's bucket
443 * has failed over.
444 *
445 * @param group failover group to be checked for failover
446 * @param id device ID of switch whose port's status changed
447 * @param portNumber port number of port that was disabled
448 * @return whether the failover group experienced failover
449 */
450 private boolean checkFailoverGroup(Group group, DeviceId id,
451 PortNumber portNumber) {
452 boolean portReached = false;
453 boolean portEnabled = false;
454 Iterator<GroupBucket> bIterator = group.buckets().buckets().iterator();
455 GroupBucket bucket;
456 while (bIterator.hasNext() && !portReached) {
457 bucket = bIterator.next();
458 if (deviceService.getPort(id, bucket.watchPort()).isEnabled()) {
459 portEnabled = true;
460 }
461 if (bucket.watchPort().equals(portNumber)) {
462 portReached = true;
463 }
464 }
465 return portReached && !portEnabled;
466 }
467
sangho5afd02a2015-02-03 20:07:35 -0800468}