blob: 2801f12fdda223238da7eb3cf982e5acfd9b8e2e [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;
40import org.onosproject.net.group.GroupOperations;
41import org.onosproject.net.group.GroupProvider;
42import org.onosproject.net.group.GroupProviderRegistry;
43import org.onosproject.net.group.GroupProviderService;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070044import org.onosproject.net.group.StoredGroupBucketEntry;
sangho5afd02a2015-02-03 20:07:35 -080045import org.onosproject.net.provider.AbstractProvider;
46import org.onosproject.net.provider.ProviderId;
47import org.onosproject.openflow.controller.Dpid;
48import org.onosproject.openflow.controller.OpenFlowController;
49import org.onosproject.openflow.controller.OpenFlowEventListener;
50import org.onosproject.openflow.controller.OpenFlowSwitch;
51import org.onosproject.openflow.controller.OpenFlowSwitchListener;
52import org.onosproject.openflow.controller.RoleState;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070053import org.projectfloodlight.openflow.protocol.OFBucketCounter;
sangho5afd02a2015-02-03 20:07:35 -080054import org.projectfloodlight.openflow.protocol.OFErrorMsg;
55import org.projectfloodlight.openflow.protocol.OFErrorType;
56import org.projectfloodlight.openflow.protocol.OFGroupDescStatsEntry;
57import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply;
58import org.projectfloodlight.openflow.protocol.OFGroupMod;
59import org.projectfloodlight.openflow.protocol.OFGroupStatsEntry;
60import org.projectfloodlight.openflow.protocol.OFGroupStatsReply;
61import org.projectfloodlight.openflow.protocol.OFGroupType;
62import org.projectfloodlight.openflow.protocol.OFMessage;
63import org.projectfloodlight.openflow.protocol.OFPortStatus;
64import org.projectfloodlight.openflow.protocol.OFStatsReply;
65import org.projectfloodlight.openflow.protocol.OFStatsType;
sangho01a883c2015-02-23 11:01:50 -080066import org.projectfloodlight.openflow.protocol.OFVersion;
sangho5afd02a2015-02-03 20:07:35 -080067import org.slf4j.Logger;
68
jiangruia6b46092015-11-11 16:28:51 +080069import com.google.common.collect.Maps;
sangho5afd02a2015-02-03 20:07:35 -080070
71/**
72 * Provider which uses an OpenFlow controller to handle Group.
73 */
74@Component(immediate = true)
75public class OpenFlowGroupProvider extends AbstractProvider implements GroupProvider {
76
77 private final Logger log = getLogger(getClass());
78
79 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
80 protected OpenFlowController controller;
81
82 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
83 protected GroupProviderRegistry providerRegistry;
84
jiangruia6b46092015-11-11 16:28:51 +080085 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
86 protected DriverService driverService;
87
sangho5afd02a2015-02-03 20:07:35 -080088 private GroupProviderService providerService;
89
90 static final int POLL_INTERVAL = 10;
91
92 private final InternalGroupProvider listener = new InternalGroupProvider();
93
sanghoa09f2742015-02-06 14:49:47 -080094 private static final AtomicLong XID_COUNTER = new AtomicLong(1);
sangho5afd02a2015-02-03 20:07:35 -080095 private final Map<Dpid, GroupStatsCollector> collectors = Maps.newHashMap();
sanghoa09f2742015-02-06 14:49:47 -080096 private final Map<Long, OFStatsReply> groupStats = Maps.newConcurrentMap();
sangho22a805d2015-02-13 09:45:43 -080097 private final Map<GroupId, GroupOperation> pendingGroupOperations =
sangho5afd02a2015-02-03 20:07:35 -080098 Maps.newConcurrentMap();
99
sanghoa09f2742015-02-06 14:49:47 -0800100 /* Map<Group ID, Transaction ID> */
sangho22a805d2015-02-13 09:45:43 -0800101 private final Map<GroupId, Long> pendingXidMaps = Maps.newConcurrentMap();
sanghoa09f2742015-02-06 14:49:47 -0800102
sangho5afd02a2015-02-03 20:07:35 -0800103 /**
104 * Creates a OpenFlow group provider.
105 */
106 public OpenFlowGroupProvider() {
107 super(new ProviderId("of", "org.onosproject.provider.group"));
108 }
109
110 @Activate
111 public void activate() {
112 providerService = providerRegistry.register(this);
113 controller.addListener(listener);
114 controller.addEventListener(listener);
115
116 for (OpenFlowSwitch sw : controller.getSwitches()) {
sangho01a883c2015-02-23 11:01:50 -0800117 if (isGroupSupported(sw)) {
118 GroupStatsCollector gsc = new GroupStatsCollector(sw, POLL_INTERVAL);
119 gsc.start();
120 collectors.put(new Dpid(sw.getId()), gsc);
121 }
sangho5afd02a2015-02-03 20:07:35 -0800122 }
123
124 log.info("Started");
125 }
126
127 @Deactivate
128 public void deactivate() {
129 providerRegistry.unregister(this);
130 providerService = null;
Charles Chanecfdfb72015-11-24 19:05:50 -0800131 collectors.values().forEach(GroupStatsCollector::stop);
132 collectors.clear();
sangho5afd02a2015-02-03 20:07:35 -0800133 log.info("Stopped");
134 }
135
136 @Override
137 public void performGroupOperation(DeviceId deviceId, GroupOperations groupOps) {
138 Map<OFGroupMod, OpenFlowSwitch> mods = Maps.newIdentityHashMap();
139 final Dpid dpid = Dpid.dpid(deviceId.uri());
140 OpenFlowSwitch sw = controller.getSwitch(dpid);
141 for (GroupOperation groupOperation: groupOps.operations()) {
142 if (sw == null) {
sanghoa09f2742015-02-06 14:49:47 -0800143 log.error("SW {} is not found", dpid);
sangho5afd02a2015-02-03 20:07:35 -0800144 return;
145 }
sanghoa09f2742015-02-06 14:49:47 -0800146 final Long groupModXid = XID_COUNTER.getAndIncrement();
jiangruia6b46092015-11-11 16:28:51 +0800147 GroupModBuilder builder = null;
148 if (driverService == null) {
149 builder = GroupModBuilder.builder(groupOperation.buckets(),
150 groupOperation.groupId(),
151 groupOperation.groupType(),
152 sw.factory(),
153 Optional.of(groupModXid));
154 } else {
155 builder = GroupModBuilder.builder(groupOperation.buckets(),
156 groupOperation.groupId(),
157 groupOperation.groupType(),
158 sw.factory(),
159 Optional.of(groupModXid),
160 Optional.of(driverService));
161 }
sangho5afd02a2015-02-03 20:07:35 -0800162 OFGroupMod groupMod = null;
163 switch (groupOperation.opType()) {
164 case ADD:
165 groupMod = builder.buildGroupAdd();
166 break;
167 case MODIFY:
168 groupMod = builder.buildGroupMod();
169 break;
170 case DELETE:
171 groupMod = builder.buildGroupDel();
172 break;
173 default:
174 log.error("Unsupported Group operation");
Satish Ke6d91c52015-11-21 19:04:20 +0530175 return;
sangho5afd02a2015-02-03 20:07:35 -0800176 }
177 sw.sendMsg(groupMod);
sangho22a805d2015-02-13 09:45:43 -0800178 GroupId groudId = new DefaultGroupId(groupMod.getGroup().getGroupNumber());
179 pendingGroupOperations.put(groudId, groupOperation);
180 pendingXidMaps.put(groudId, groupModXid);
sangho5afd02a2015-02-03 20:07:35 -0800181 }
182 }
183
184 private void pushGroupMetrics(Dpid dpid, OFStatsReply statsReply) {
185 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
186
187 OFGroupStatsReply groupStatsReply = null;
188 OFGroupDescStatsReply groupDescStatsReply = null;
189
sanghoa09f2742015-02-06 14:49:47 -0800190 synchronized (groupStats) {
191 if (statsReply.getStatsType() == OFStatsType.GROUP) {
192 OFStatsReply reply = groupStats.get(statsReply.getXid() + 1);
193 if (reply != null) {
194 groupStatsReply = (OFGroupStatsReply) statsReply;
195 groupDescStatsReply = (OFGroupDescStatsReply) reply;
196 groupStats.remove(statsReply.getXid() + 1);
197 } else {
198 groupStats.put(statsReply.getXid(), statsReply);
199 }
200 } else if (statsReply.getStatsType() == OFStatsType.GROUP_DESC) {
201 OFStatsReply reply = groupStats.get(statsReply.getXid() - 1);
202 if (reply != null) {
203 groupStatsReply = (OFGroupStatsReply) reply;
204 groupDescStatsReply = (OFGroupDescStatsReply) statsReply;
205 groupStats.remove(statsReply.getXid() - 1);
206 } else {
207 groupStats.put(statsReply.getXid(), statsReply);
208 }
sangho5afd02a2015-02-03 20:07:35 -0800209 }
210 }
211
212 if (groupStatsReply != null && groupDescStatsReply != null) {
213 Collection<Group> groups = buildGroupMetrics(deviceId,
214 groupStatsReply, groupDescStatsReply);
215 providerService.pushGroupMetrics(deviceId, groups);
216 for (Group group: groups) {
217 pendingGroupOperations.remove(group.id());
sanghoa09f2742015-02-06 14:49:47 -0800218 pendingXidMaps.remove(group.id());
sangho5afd02a2015-02-03 20:07:35 -0800219 }
220 }
221 }
222
223 private Collection<Group> buildGroupMetrics(DeviceId deviceId,
224 OFGroupStatsReply groupStatsReply,
225 OFGroupDescStatsReply groupDescStatsReply) {
226
227 Map<Integer, Group> groups = Maps.newHashMap();
Hyunsun Moona834c182015-12-16 03:01:56 -0800228 Dpid dpid = Dpid.dpid(deviceId.uri());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700229
sangho5afd02a2015-02-03 20:07:35 -0800230 for (OFGroupDescStatsEntry entry: groupDescStatsReply.getEntries()) {
231 int id = entry.getGroup().getGroupNumber();
232 GroupId groupId = new DefaultGroupId(id);
233 GroupDescription.Type type = getGroupType(entry.getGroupType());
Hyunsun Moona834c182015-12-16 03:01:56 -0800234 GroupBuckets buckets = new GroupBucketEntryBuilder(dpid, entry.getBuckets(),
235 entry.getGroupType(), driverService).build();
sangho5afd02a2015-02-03 20:07:35 -0800236 DefaultGroup group = new DefaultGroup(groupId, deviceId, type, buckets);
237 groups.put(id, group);
238 }
239
240 for (OFGroupStatsEntry entry: groupStatsReply.getEntries()) {
241 int groupId = entry.getGroup().getGroupNumber();
242 DefaultGroup group = (DefaultGroup) groups.get(groupId);
243 if (group != null) {
244 group.setBytes(entry.getByteCount().getValue());
245 group.setLife(entry.getDurationSec());
246 group.setPackets(entry.getPacketCount().getValue());
247 group.setReferenceCount(entry.getRefCount());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700248 int bucketIndex = 0;
249 for (OFBucketCounter bucketStats:entry.getBucketStats()) {
250 ((StoredGroupBucketEntry) group.buckets().buckets()
251 .get(bucketIndex))
252 .setPackets(bucketStats
253 .getPacketCount().getValue());
254 ((StoredGroupBucketEntry) group.buckets().buckets()
255 .get(bucketIndex))
256 .setBytes(entry.getBucketStats()
257 .get(bucketIndex)
258 .getByteCount().getValue());
259 bucketIndex++;
260 }
sangho5afd02a2015-02-03 20:07:35 -0800261 }
262 }
263
264 return groups.values();
265 }
266
267 private GroupDescription.Type getGroupType(OFGroupType type) {
268 switch (type) {
269 case ALL:
Charles Chanc42e84e2015-10-20 16:24:19 -0700270 return GroupDescription.Type.ALL;
sangho5afd02a2015-02-03 20:07:35 -0800271 case INDIRECT:
272 return GroupDescription.Type.INDIRECT;
273 case SELECT:
274 return GroupDescription.Type.SELECT;
275 case FF:
276 return GroupDescription.Type.FAILOVER;
277 default:
278 log.error("Unsupported OF group type : {}", type);
279 break;
280 }
281 return null;
282 }
283
sanghoa09f2742015-02-06 14:49:47 -0800284 /**
285 * Returns a transaction ID for entire group operations and increases
286 * the counter by the number given.
287 *
288 * @param increase the amount to increase the counter by
289 * @return a transaction ID
290 */
291 public static long getXidAndAdd(int increase) {
292 return XID_COUNTER.getAndAdd(increase);
293 }
294
sangho01a883c2015-02-23 11:01:50 -0800295 private boolean isGroupSupported(OpenFlowSwitch sw) {
296 if (sw.factory().getVersion() == OFVersion.OF_10 ||
297 sw.factory().getVersion() == OFVersion.OF_11 ||
298 sw.factory().getVersion() == OFVersion.OF_12) {
299 return false;
300 }
301
302 return true;
303 }
304
sangho5afd02a2015-02-03 20:07:35 -0800305 private class InternalGroupProvider
306 implements OpenFlowSwitchListener, OpenFlowEventListener {
307
308 @Override
309 public void handleMessage(Dpid dpid, OFMessage msg) {
310 switch (msg.getType()) {
311 case STATS_REPLY:
312 pushGroupMetrics(dpid, (OFStatsReply) msg);
313 break;
314 case ERROR:
315 OFErrorMsg errorMsg = (OFErrorMsg) msg;
316 if (errorMsg.getErrType() == OFErrorType.GROUP_MOD_FAILED) {
sangho22a805d2015-02-13 09:45:43 -0800317 GroupId pendingGroupId = null;
318 for (Map.Entry<GroupId, Long> entry: pendingXidMaps.entrySet()) {
sanghoa09f2742015-02-06 14:49:47 -0800319 if (entry.getValue() == errorMsg.getXid()) {
320 pendingGroupId = entry.getKey();
321 break;
322 }
323 }
sangho22a805d2015-02-13 09:45:43 -0800324 if (pendingGroupId == null) {
sanghoa09f2742015-02-06 14:49:47 -0800325 log.warn("Error for unknown group operation: {}",
326 errorMsg.getXid());
327 } else {
328 GroupOperation operation =
329 pendingGroupOperations.get(pendingGroupId);
sangho7ff01812015-02-09 16:21:53 -0800330 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
sanghoa09f2742015-02-06 14:49:47 -0800331 if (operation != null) {
sangho7ff01812015-02-09 16:21:53 -0800332 providerService.groupOperationFailed(deviceId,
333 operation);
sanghoa09f2742015-02-06 14:49:47 -0800334 pendingGroupOperations.remove(pendingGroupId);
335 pendingXidMaps.remove(pendingGroupId);
336 log.warn("Received an group mod error {}", msg);
337 } else {
338 log.error("Cannot find pending group operation with group ID: {}",
339 pendingGroupId);
340 }
sangho5afd02a2015-02-03 20:07:35 -0800341 }
342 break;
343 }
344 default:
Jonathan Hart7baba072015-02-23 14:27:59 -0800345 break;
sangho5afd02a2015-02-03 20:07:35 -0800346 }
347 }
348
349 @Override
350 public void switchAdded(Dpid dpid) {
sangho01a883c2015-02-23 11:01:50 -0800351 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500352 if (sw == null) {
353 return;
354 }
sangho01a883c2015-02-23 11:01:50 -0800355 if (isGroupSupported(sw)) {
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800356 GroupStatsCollector gsc = new GroupStatsCollector(sw, POLL_INTERVAL);
sangho01a883c2015-02-23 11:01:50 -0800357 gsc.start();
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800358 GroupStatsCollector prevGsc = collectors.put(dpid, gsc);
359 if (prevGsc != null) {
360 prevGsc.stop();
361 }
sangho01a883c2015-02-23 11:01:50 -0800362 }
Lei Xudee1aff2015-10-16 00:45:10 -0500363
364 //figure out race condition
365 if (controller.getSwitch(dpid) == null) {
366 switchRemoved(dpid);
367 }
sangho5afd02a2015-02-03 20:07:35 -0800368 }
369
370 @Override
371 public void switchRemoved(Dpid dpid) {
372 GroupStatsCollector collector = collectors.remove(dpid);
373 if (collector != null) {
374 collector.stop();
375 }
376 }
377
378 @Override
379 public void switchChanged(Dpid dpid) {
380 }
381
382 @Override
383 public void portChanged(Dpid dpid, OFPortStatus status) {
384 }
385
386 @Override
387 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
388 }
389 }
390
391}