blob: 1835cf67e1927554d9c3d6cbafbfb6425db44e51 [file] [log] [blame]
sangho5afd02a2015-02-03 20:07:35 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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
21import java.util.Collection;
22import java.util.Map;
23import java.util.Optional;
24import java.util.concurrent.atomic.AtomicLong;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070025
sangho5afd02a2015-02-03 20:07:35 -080026import org.apache.felix.scr.annotations.Activate;
27import org.apache.felix.scr.annotations.Component;
28import org.apache.felix.scr.annotations.Deactivate;
29import org.apache.felix.scr.annotations.Reference;
30import org.apache.felix.scr.annotations.ReferenceCardinality;
31import org.onosproject.core.DefaultGroupId;
32import org.onosproject.core.GroupId;
33import org.onosproject.net.DeviceId;
jiangruia6b46092015-11-11 16:28:51 +080034import org.onosproject.net.driver.DriverService;
sangho5afd02a2015-02-03 20:07:35 -080035import org.onosproject.net.group.DefaultGroup;
36import org.onosproject.net.group.Group;
37import org.onosproject.net.group.GroupBuckets;
38import org.onosproject.net.group.GroupDescription;
39import org.onosproject.net.group.GroupOperation;
Saurav Das0fd79d92016-03-07 10:58:36 -080040import org.onosproject.net.group.GroupOperation.GroupMsgErrorCode;
sangho5afd02a2015-02-03 20:07:35 -080041import org.onosproject.net.group.GroupOperations;
42import org.onosproject.net.group.GroupProvider;
43import org.onosproject.net.group.GroupProviderRegistry;
44import org.onosproject.net.group.GroupProviderService;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070045import org.onosproject.net.group.StoredGroupBucketEntry;
sangho5afd02a2015-02-03 20:07:35 -080046import org.onosproject.net.provider.AbstractProvider;
47import org.onosproject.net.provider.ProviderId;
48import org.onosproject.openflow.controller.Dpid;
49import org.onosproject.openflow.controller.OpenFlowController;
50import org.onosproject.openflow.controller.OpenFlowEventListener;
51import org.onosproject.openflow.controller.OpenFlowSwitch;
52import org.onosproject.openflow.controller.OpenFlowSwitchListener;
53import org.onosproject.openflow.controller.RoleState;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070054import org.projectfloodlight.openflow.protocol.OFBucketCounter;
sangho5afd02a2015-02-03 20:07:35 -080055import org.projectfloodlight.openflow.protocol.OFErrorMsg;
56import org.projectfloodlight.openflow.protocol.OFErrorType;
57import org.projectfloodlight.openflow.protocol.OFGroupDescStatsEntry;
58import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply;
59import org.projectfloodlight.openflow.protocol.OFGroupMod;
Saurav Das0fd79d92016-03-07 10:58:36 -080060import org.projectfloodlight.openflow.protocol.OFGroupModFailedCode;
sangho5afd02a2015-02-03 20:07:35 -080061import org.projectfloodlight.openflow.protocol.OFGroupStatsEntry;
62import org.projectfloodlight.openflow.protocol.OFGroupStatsReply;
63import org.projectfloodlight.openflow.protocol.OFGroupType;
64import org.projectfloodlight.openflow.protocol.OFMessage;
65import org.projectfloodlight.openflow.protocol.OFPortStatus;
66import org.projectfloodlight.openflow.protocol.OFStatsReply;
67import org.projectfloodlight.openflow.protocol.OFStatsType;
sangho01a883c2015-02-23 11:01:50 -080068import org.projectfloodlight.openflow.protocol.OFVersion;
Saurav Das0fd79d92016-03-07 10:58:36 -080069import org.projectfloodlight.openflow.protocol.errormsg.OFGroupModFailedErrorMsg;
sangho5afd02a2015-02-03 20:07:35 -080070import org.slf4j.Logger;
71
jiangruia6b46092015-11-11 16:28:51 +080072import com.google.common.collect.Maps;
sangho5afd02a2015-02-03 20:07:35 -080073
74/**
75 * Provider which uses an OpenFlow controller to handle Group.
76 */
77@Component(immediate = true)
78public class OpenFlowGroupProvider extends AbstractProvider implements GroupProvider {
79
80 private final Logger log = getLogger(getClass());
81
82 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
83 protected OpenFlowController controller;
84
85 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
86 protected GroupProviderRegistry providerRegistry;
87
jiangruia6b46092015-11-11 16:28:51 +080088 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
89 protected DriverService driverService;
90
sangho5afd02a2015-02-03 20:07:35 -080091 private GroupProviderService providerService;
92
93 static final int POLL_INTERVAL = 10;
94
95 private final InternalGroupProvider listener = new InternalGroupProvider();
96
sanghoa09f2742015-02-06 14:49:47 -080097 private static final AtomicLong XID_COUNTER = new AtomicLong(1);
sangho5afd02a2015-02-03 20:07:35 -080098 private final Map<Dpid, GroupStatsCollector> collectors = Maps.newHashMap();
sanghoa09f2742015-02-06 14:49:47 -080099 private final Map<Long, OFStatsReply> groupStats = Maps.newConcurrentMap();
sangho22a805d2015-02-13 09:45:43 -0800100 private final Map<GroupId, GroupOperation> pendingGroupOperations =
sangho5afd02a2015-02-03 20:07:35 -0800101 Maps.newConcurrentMap();
102
sanghoa09f2742015-02-06 14:49:47 -0800103 /* Map<Group ID, Transaction ID> */
sangho22a805d2015-02-13 09:45:43 -0800104 private final Map<GroupId, Long> pendingXidMaps = Maps.newConcurrentMap();
sanghoa09f2742015-02-06 14:49:47 -0800105
sangho5afd02a2015-02-03 20:07:35 -0800106 /**
107 * Creates a OpenFlow group provider.
108 */
109 public OpenFlowGroupProvider() {
110 super(new ProviderId("of", "org.onosproject.provider.group"));
111 }
112
113 @Activate
114 public void activate() {
115 providerService = providerRegistry.register(this);
116 controller.addListener(listener);
117 controller.addEventListener(listener);
118
119 for (OpenFlowSwitch sw : controller.getSwitches()) {
sangho01a883c2015-02-23 11:01:50 -0800120 if (isGroupSupported(sw)) {
121 GroupStatsCollector gsc = new GroupStatsCollector(sw, POLL_INTERVAL);
122 gsc.start();
123 collectors.put(new Dpid(sw.getId()), gsc);
124 }
sangho5afd02a2015-02-03 20:07:35 -0800125 }
126
127 log.info("Started");
128 }
129
130 @Deactivate
131 public void deactivate() {
132 providerRegistry.unregister(this);
133 providerService = null;
Charles Chanecfdfb72015-11-24 19:05:50 -0800134 collectors.values().forEach(GroupStatsCollector::stop);
135 collectors.clear();
sangho5afd02a2015-02-03 20:07:35 -0800136 log.info("Stopped");
137 }
138
139 @Override
140 public void performGroupOperation(DeviceId deviceId, GroupOperations groupOps) {
sangho5afd02a2015-02-03 20:07:35 -0800141 final Dpid dpid = Dpid.dpid(deviceId.uri());
142 OpenFlowSwitch sw = controller.getSwitch(dpid);
143 for (GroupOperation groupOperation: groupOps.operations()) {
144 if (sw == null) {
sanghoa09f2742015-02-06 14:49:47 -0800145 log.error("SW {} is not found", dpid);
sangho5afd02a2015-02-03 20:07:35 -0800146 return;
147 }
sanghoa09f2742015-02-06 14:49:47 -0800148 final Long groupModXid = XID_COUNTER.getAndIncrement();
jiangruia6b46092015-11-11 16:28:51 +0800149 GroupModBuilder builder = null;
150 if (driverService == null) {
151 builder = GroupModBuilder.builder(groupOperation.buckets(),
152 groupOperation.groupId(),
153 groupOperation.groupType(),
154 sw.factory(),
155 Optional.of(groupModXid));
156 } else {
157 builder = GroupModBuilder.builder(groupOperation.buckets(),
158 groupOperation.groupId(),
159 groupOperation.groupType(),
160 sw.factory(),
161 Optional.of(groupModXid),
162 Optional.of(driverService));
163 }
sangho5afd02a2015-02-03 20:07:35 -0800164 OFGroupMod groupMod = null;
165 switch (groupOperation.opType()) {
166 case ADD:
167 groupMod = builder.buildGroupAdd();
168 break;
169 case MODIFY:
170 groupMod = builder.buildGroupMod();
171 break;
172 case DELETE:
173 groupMod = builder.buildGroupDel();
174 break;
175 default:
176 log.error("Unsupported Group operation");
Satish Ke6d91c52015-11-21 19:04:20 +0530177 return;
sangho5afd02a2015-02-03 20:07:35 -0800178 }
179 sw.sendMsg(groupMod);
sangho22a805d2015-02-13 09:45:43 -0800180 GroupId groudId = new DefaultGroupId(groupMod.getGroup().getGroupNumber());
181 pendingGroupOperations.put(groudId, groupOperation);
182 pendingXidMaps.put(groudId, groupModXid);
sangho5afd02a2015-02-03 20:07:35 -0800183 }
184 }
185
186 private void pushGroupMetrics(Dpid dpid, OFStatsReply statsReply) {
187 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
188
189 OFGroupStatsReply groupStatsReply = null;
190 OFGroupDescStatsReply groupDescStatsReply = null;
191
sanghoa09f2742015-02-06 14:49:47 -0800192 synchronized (groupStats) {
193 if (statsReply.getStatsType() == OFStatsType.GROUP) {
194 OFStatsReply reply = groupStats.get(statsReply.getXid() + 1);
195 if (reply != null) {
196 groupStatsReply = (OFGroupStatsReply) statsReply;
197 groupDescStatsReply = (OFGroupDescStatsReply) reply;
198 groupStats.remove(statsReply.getXid() + 1);
199 } else {
200 groupStats.put(statsReply.getXid(), statsReply);
201 }
202 } else if (statsReply.getStatsType() == OFStatsType.GROUP_DESC) {
203 OFStatsReply reply = groupStats.get(statsReply.getXid() - 1);
204 if (reply != null) {
205 groupStatsReply = (OFGroupStatsReply) reply;
206 groupDescStatsReply = (OFGroupDescStatsReply) statsReply;
207 groupStats.remove(statsReply.getXid() - 1);
208 } else {
209 groupStats.put(statsReply.getXid(), statsReply);
210 }
sangho5afd02a2015-02-03 20:07:35 -0800211 }
212 }
213
214 if (groupStatsReply != null && groupDescStatsReply != null) {
215 Collection<Group> groups = buildGroupMetrics(deviceId,
216 groupStatsReply, groupDescStatsReply);
217 providerService.pushGroupMetrics(deviceId, groups);
218 for (Group group: groups) {
219 pendingGroupOperations.remove(group.id());
sanghoa09f2742015-02-06 14:49:47 -0800220 pendingXidMaps.remove(group.id());
sangho5afd02a2015-02-03 20:07:35 -0800221 }
222 }
223 }
224
225 private Collection<Group> buildGroupMetrics(DeviceId deviceId,
226 OFGroupStatsReply groupStatsReply,
227 OFGroupDescStatsReply groupDescStatsReply) {
228
229 Map<Integer, Group> groups = Maps.newHashMap();
Hyunsun Moona834c182015-12-16 03:01:56 -0800230 Dpid dpid = Dpid.dpid(deviceId.uri());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700231
sangho5afd02a2015-02-03 20:07:35 -0800232 for (OFGroupDescStatsEntry entry: groupDescStatsReply.getEntries()) {
233 int id = entry.getGroup().getGroupNumber();
234 GroupId groupId = new DefaultGroupId(id);
235 GroupDescription.Type type = getGroupType(entry.getGroupType());
Hyunsun Moona834c182015-12-16 03:01:56 -0800236 GroupBuckets buckets = new GroupBucketEntryBuilder(dpid, entry.getBuckets(),
237 entry.getGroupType(), driverService).build();
sangho5afd02a2015-02-03 20:07:35 -0800238 DefaultGroup group = new DefaultGroup(groupId, deviceId, type, buckets);
239 groups.put(id, group);
240 }
241
242 for (OFGroupStatsEntry entry: groupStatsReply.getEntries()) {
243 int groupId = entry.getGroup().getGroupNumber();
244 DefaultGroup group = (DefaultGroup) groups.get(groupId);
245 if (group != null) {
246 group.setBytes(entry.getByteCount().getValue());
247 group.setLife(entry.getDurationSec());
248 group.setPackets(entry.getPacketCount().getValue());
249 group.setReferenceCount(entry.getRefCount());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700250 int bucketIndex = 0;
251 for (OFBucketCounter bucketStats:entry.getBucketStats()) {
252 ((StoredGroupBucketEntry) group.buckets().buckets()
253 .get(bucketIndex))
254 .setPackets(bucketStats
255 .getPacketCount().getValue());
256 ((StoredGroupBucketEntry) group.buckets().buckets()
257 .get(bucketIndex))
258 .setBytes(entry.getBucketStats()
259 .get(bucketIndex)
260 .getByteCount().getValue());
261 bucketIndex++;
262 }
sangho5afd02a2015-02-03 20:07:35 -0800263 }
264 }
265
266 return groups.values();
267 }
268
269 private GroupDescription.Type getGroupType(OFGroupType type) {
270 switch (type) {
271 case ALL:
Charles Chanc42e84e2015-10-20 16:24:19 -0700272 return GroupDescription.Type.ALL;
sangho5afd02a2015-02-03 20:07:35 -0800273 case INDIRECT:
274 return GroupDescription.Type.INDIRECT;
275 case SELECT:
276 return GroupDescription.Type.SELECT;
277 case FF:
278 return GroupDescription.Type.FAILOVER;
279 default:
280 log.error("Unsupported OF group type : {}", type);
281 break;
282 }
283 return null;
284 }
285
sanghoa09f2742015-02-06 14:49:47 -0800286 /**
287 * Returns a transaction ID for entire group operations and increases
288 * the counter by the number given.
289 *
290 * @param increase the amount to increase the counter by
291 * @return a transaction ID
292 */
293 public static long getXidAndAdd(int increase) {
294 return XID_COUNTER.getAndAdd(increase);
295 }
296
sangho01a883c2015-02-23 11:01:50 -0800297 private boolean isGroupSupported(OpenFlowSwitch sw) {
298 if (sw.factory().getVersion() == OFVersion.OF_10 ||
299 sw.factory().getVersion() == OFVersion.OF_11 ||
300 sw.factory().getVersion() == OFVersion.OF_12) {
301 return false;
302 }
303
304 return true;
305 }
306
sangho5afd02a2015-02-03 20:07:35 -0800307 private class InternalGroupProvider
308 implements OpenFlowSwitchListener, OpenFlowEventListener {
309
310 @Override
311 public void handleMessage(Dpid dpid, OFMessage msg) {
312 switch (msg.getType()) {
313 case STATS_REPLY:
314 pushGroupMetrics(dpid, (OFStatsReply) msg);
315 break;
316 case ERROR:
317 OFErrorMsg errorMsg = (OFErrorMsg) msg;
318 if (errorMsg.getErrType() == OFErrorType.GROUP_MOD_FAILED) {
sangho22a805d2015-02-13 09:45:43 -0800319 GroupId pendingGroupId = null;
320 for (Map.Entry<GroupId, Long> entry: pendingXidMaps.entrySet()) {
sanghoa09f2742015-02-06 14:49:47 -0800321 if (entry.getValue() == errorMsg.getXid()) {
322 pendingGroupId = entry.getKey();
323 break;
324 }
325 }
sangho22a805d2015-02-13 09:45:43 -0800326 if (pendingGroupId == null) {
sanghoa09f2742015-02-06 14:49:47 -0800327 log.warn("Error for unknown group operation: {}",
328 errorMsg.getXid());
329 } else {
330 GroupOperation operation =
331 pendingGroupOperations.get(pendingGroupId);
sangho7ff01812015-02-09 16:21:53 -0800332 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
sanghoa09f2742015-02-06 14:49:47 -0800333 if (operation != null) {
Saurav Das0fd79d92016-03-07 10:58:36 -0800334 OFGroupModFailedCode code =
335 ((OFGroupModFailedErrorMsg) errorMsg).getCode();
336 GroupMsgErrorCode failureCode =
337 GroupMsgErrorCode.values()[(code.ordinal())];
338 GroupOperation failedOperation = GroupOperation
339 .createFailedGroupOperation(operation, failureCode);
Saurav Das8be4e3a2016-03-11 17:19:07 -0800340 log.warn("Received a group mod error {}", msg);
sangho7ff01812015-02-09 16:21:53 -0800341 providerService.groupOperationFailed(deviceId,
Saurav Das0fd79d92016-03-07 10:58:36 -0800342 failedOperation);
sanghoa09f2742015-02-06 14:49:47 -0800343 pendingGroupOperations.remove(pendingGroupId);
344 pendingXidMaps.remove(pendingGroupId);
sanghoa09f2742015-02-06 14:49:47 -0800345 } else {
346 log.error("Cannot find pending group operation with group ID: {}",
347 pendingGroupId);
348 }
sangho5afd02a2015-02-03 20:07:35 -0800349 }
350 break;
351 }
352 default:
Jonathan Hart7baba072015-02-23 14:27:59 -0800353 break;
sangho5afd02a2015-02-03 20:07:35 -0800354 }
355 }
356
357 @Override
358 public void switchAdded(Dpid dpid) {
sangho01a883c2015-02-23 11:01:50 -0800359 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500360 if (sw == null) {
361 return;
362 }
sangho01a883c2015-02-23 11:01:50 -0800363 if (isGroupSupported(sw)) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800364 GroupStatsCollector gsc = new GroupStatsCollector(sw, POLL_INTERVAL);
sangho01a883c2015-02-23 11:01:50 -0800365 gsc.start();
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800366 GroupStatsCollector prevGsc = collectors.put(dpid, gsc);
367 if (prevGsc != null) {
368 prevGsc.stop();
369 }
sangho01a883c2015-02-23 11:01:50 -0800370 }
Lei Xudee1aff2015-10-16 00:45:10 -0500371
372 //figure out race condition
373 if (controller.getSwitch(dpid) == null) {
374 switchRemoved(dpid);
375 }
sangho5afd02a2015-02-03 20:07:35 -0800376 }
377
378 @Override
379 public void switchRemoved(Dpid dpid) {
380 GroupStatsCollector collector = collectors.remove(dpid);
381 if (collector != null) {
382 collector.stop();
383 }
384 }
385
386 @Override
387 public void switchChanged(Dpid dpid) {
388 }
389
390 @Override
391 public void portChanged(Dpid dpid, OFPortStatus status) {
392 }
393
394 @Override
395 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
396 }
397 }
398
399}