blob: e69fd6b931ffd57c401b385540ccb25b4529b904 [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;
131
132 log.info("Stopped");
133 }
134
135 @Override
136 public void performGroupOperation(DeviceId deviceId, GroupOperations groupOps) {
137 Map<OFGroupMod, OpenFlowSwitch> mods = Maps.newIdentityHashMap();
138 final Dpid dpid = Dpid.dpid(deviceId.uri());
139 OpenFlowSwitch sw = controller.getSwitch(dpid);
140 for (GroupOperation groupOperation: groupOps.operations()) {
141 if (sw == null) {
sanghoa09f2742015-02-06 14:49:47 -0800142 log.error("SW {} is not found", dpid);
sangho5afd02a2015-02-03 20:07:35 -0800143 return;
144 }
sanghoa09f2742015-02-06 14:49:47 -0800145 final Long groupModXid = XID_COUNTER.getAndIncrement();
jiangruia6b46092015-11-11 16:28:51 +0800146 GroupModBuilder builder = null;
147 if (driverService == null) {
148 builder = GroupModBuilder.builder(groupOperation.buckets(),
149 groupOperation.groupId(),
150 groupOperation.groupType(),
151 sw.factory(),
152 Optional.of(groupModXid));
153 } else {
154 builder = GroupModBuilder.builder(groupOperation.buckets(),
155 groupOperation.groupId(),
156 groupOperation.groupType(),
157 sw.factory(),
158 Optional.of(groupModXid),
159 Optional.of(driverService));
160 }
sangho5afd02a2015-02-03 20:07:35 -0800161 OFGroupMod groupMod = null;
162 switch (groupOperation.opType()) {
163 case ADD:
164 groupMod = builder.buildGroupAdd();
165 break;
166 case MODIFY:
167 groupMod = builder.buildGroupMod();
168 break;
169 case DELETE:
170 groupMod = builder.buildGroupDel();
171 break;
172 default:
173 log.error("Unsupported Group operation");
Satish Ke6d91c52015-11-21 19:04:20 +0530174 return;
sangho5afd02a2015-02-03 20:07:35 -0800175 }
176 sw.sendMsg(groupMod);
sangho22a805d2015-02-13 09:45:43 -0800177 GroupId groudId = new DefaultGroupId(groupMod.getGroup().getGroupNumber());
178 pendingGroupOperations.put(groudId, groupOperation);
179 pendingXidMaps.put(groudId, groupModXid);
sangho5afd02a2015-02-03 20:07:35 -0800180 }
181 }
182
183 private void pushGroupMetrics(Dpid dpid, OFStatsReply statsReply) {
184 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
185
186 OFGroupStatsReply groupStatsReply = null;
187 OFGroupDescStatsReply groupDescStatsReply = null;
188
sanghoa09f2742015-02-06 14:49:47 -0800189 synchronized (groupStats) {
190 if (statsReply.getStatsType() == OFStatsType.GROUP) {
191 OFStatsReply reply = groupStats.get(statsReply.getXid() + 1);
192 if (reply != null) {
193 groupStatsReply = (OFGroupStatsReply) statsReply;
194 groupDescStatsReply = (OFGroupDescStatsReply) reply;
195 groupStats.remove(statsReply.getXid() + 1);
196 } else {
197 groupStats.put(statsReply.getXid(), statsReply);
198 }
199 } else if (statsReply.getStatsType() == OFStatsType.GROUP_DESC) {
200 OFStatsReply reply = groupStats.get(statsReply.getXid() - 1);
201 if (reply != null) {
202 groupStatsReply = (OFGroupStatsReply) reply;
203 groupDescStatsReply = (OFGroupDescStatsReply) statsReply;
204 groupStats.remove(statsReply.getXid() - 1);
205 } else {
206 groupStats.put(statsReply.getXid(), statsReply);
207 }
sangho5afd02a2015-02-03 20:07:35 -0800208 }
209 }
210
211 if (groupStatsReply != null && groupDescStatsReply != null) {
212 Collection<Group> groups = buildGroupMetrics(deviceId,
213 groupStatsReply, groupDescStatsReply);
214 providerService.pushGroupMetrics(deviceId, groups);
215 for (Group group: groups) {
216 pendingGroupOperations.remove(group.id());
sanghoa09f2742015-02-06 14:49:47 -0800217 pendingXidMaps.remove(group.id());
sangho5afd02a2015-02-03 20:07:35 -0800218 }
219 }
220 }
221
222 private Collection<Group> buildGroupMetrics(DeviceId deviceId,
223 OFGroupStatsReply groupStatsReply,
224 OFGroupDescStatsReply groupDescStatsReply) {
225
226 Map<Integer, Group> groups = Maps.newHashMap();
227
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700228
sangho5afd02a2015-02-03 20:07:35 -0800229 for (OFGroupDescStatsEntry entry: groupDescStatsReply.getEntries()) {
230 int id = entry.getGroup().getGroupNumber();
231 GroupId groupId = new DefaultGroupId(id);
232 GroupDescription.Type type = getGroupType(entry.getGroupType());
233 GroupBuckets buckets = new GroupBucketEntryBuilder(entry.getBuckets(),
234 entry.getGroupType()).build();
235 DefaultGroup group = new DefaultGroup(groupId, deviceId, type, buckets);
236 groups.put(id, group);
237 }
238
239 for (OFGroupStatsEntry entry: groupStatsReply.getEntries()) {
240 int groupId = entry.getGroup().getGroupNumber();
241 DefaultGroup group = (DefaultGroup) groups.get(groupId);
242 if (group != null) {
243 group.setBytes(entry.getByteCount().getValue());
244 group.setLife(entry.getDurationSec());
245 group.setPackets(entry.getPacketCount().getValue());
246 group.setReferenceCount(entry.getRefCount());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700247 int bucketIndex = 0;
248 for (OFBucketCounter bucketStats:entry.getBucketStats()) {
249 ((StoredGroupBucketEntry) group.buckets().buckets()
250 .get(bucketIndex))
251 .setPackets(bucketStats
252 .getPacketCount().getValue());
253 ((StoredGroupBucketEntry) group.buckets().buckets()
254 .get(bucketIndex))
255 .setBytes(entry.getBucketStats()
256 .get(bucketIndex)
257 .getByteCount().getValue());
258 bucketIndex++;
259 }
sangho5afd02a2015-02-03 20:07:35 -0800260 }
261 }
262
263 return groups.values();
264 }
265
266 private GroupDescription.Type getGroupType(OFGroupType type) {
267 switch (type) {
268 case ALL:
Charles Chanc42e84e2015-10-20 16:24:19 -0700269 return GroupDescription.Type.ALL;
sangho5afd02a2015-02-03 20:07:35 -0800270 case INDIRECT:
271 return GroupDescription.Type.INDIRECT;
272 case SELECT:
273 return GroupDescription.Type.SELECT;
274 case FF:
275 return GroupDescription.Type.FAILOVER;
276 default:
277 log.error("Unsupported OF group type : {}", type);
278 break;
279 }
280 return null;
281 }
282
sanghoa09f2742015-02-06 14:49:47 -0800283 /**
284 * Returns a transaction ID for entire group operations and increases
285 * the counter by the number given.
286 *
287 * @param increase the amount to increase the counter by
288 * @return a transaction ID
289 */
290 public static long getXidAndAdd(int increase) {
291 return XID_COUNTER.getAndAdd(increase);
292 }
293
sangho01a883c2015-02-23 11:01:50 -0800294 private boolean isGroupSupported(OpenFlowSwitch sw) {
295 if (sw.factory().getVersion() == OFVersion.OF_10 ||
296 sw.factory().getVersion() == OFVersion.OF_11 ||
297 sw.factory().getVersion() == OFVersion.OF_12) {
298 return false;
299 }
300
301 return true;
302 }
303
sangho5afd02a2015-02-03 20:07:35 -0800304 private class InternalGroupProvider
305 implements OpenFlowSwitchListener, OpenFlowEventListener {
306
307 @Override
308 public void handleMessage(Dpid dpid, OFMessage msg) {
309 switch (msg.getType()) {
310 case STATS_REPLY:
311 pushGroupMetrics(dpid, (OFStatsReply) msg);
312 break;
313 case ERROR:
314 OFErrorMsg errorMsg = (OFErrorMsg) msg;
315 if (errorMsg.getErrType() == OFErrorType.GROUP_MOD_FAILED) {
sangho22a805d2015-02-13 09:45:43 -0800316 GroupId pendingGroupId = null;
317 for (Map.Entry<GroupId, Long> entry: pendingXidMaps.entrySet()) {
sanghoa09f2742015-02-06 14:49:47 -0800318 if (entry.getValue() == errorMsg.getXid()) {
319 pendingGroupId = entry.getKey();
320 break;
321 }
322 }
sangho22a805d2015-02-13 09:45:43 -0800323 if (pendingGroupId == null) {
sanghoa09f2742015-02-06 14:49:47 -0800324 log.warn("Error for unknown group operation: {}",
325 errorMsg.getXid());
326 } else {
327 GroupOperation operation =
328 pendingGroupOperations.get(pendingGroupId);
sangho7ff01812015-02-09 16:21:53 -0800329 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
sanghoa09f2742015-02-06 14:49:47 -0800330 if (operation != null) {
sangho7ff01812015-02-09 16:21:53 -0800331 providerService.groupOperationFailed(deviceId,
332 operation);
sanghoa09f2742015-02-06 14:49:47 -0800333 pendingGroupOperations.remove(pendingGroupId);
334 pendingXidMaps.remove(pendingGroupId);
335 log.warn("Received an group mod error {}", msg);
336 } else {
337 log.error("Cannot find pending group operation with group ID: {}",
338 pendingGroupId);
339 }
sangho5afd02a2015-02-03 20:07:35 -0800340 }
341 break;
342 }
343 default:
Jonathan Hart7baba072015-02-23 14:27:59 -0800344 break;
sangho5afd02a2015-02-03 20:07:35 -0800345 }
346 }
347
348 @Override
349 public void switchAdded(Dpid dpid) {
sangho01a883c2015-02-23 11:01:50 -0800350 OpenFlowSwitch sw = controller.getSwitch(dpid);
Lei Xudee1aff2015-10-16 00:45:10 -0500351 if (sw == null) {
352 return;
353 }
sangho01a883c2015-02-23 11:01:50 -0800354 if (isGroupSupported(sw)) {
355 GroupStatsCollector gsc = new GroupStatsCollector(
356 controller.getSwitch(dpid), POLL_INTERVAL);
357 gsc.start();
358 collectors.put(dpid, gsc);
359 }
Lei Xudee1aff2015-10-16 00:45:10 -0500360
361 //figure out race condition
362 if (controller.getSwitch(dpid) == null) {
363 switchRemoved(dpid);
364 }
sangho5afd02a2015-02-03 20:07:35 -0800365 }
366
367 @Override
368 public void switchRemoved(Dpid dpid) {
369 GroupStatsCollector collector = collectors.remove(dpid);
370 if (collector != null) {
371 collector.stop();
372 }
373 }
374
375 @Override
376 public void switchChanged(Dpid dpid) {
377 }
378
379 @Override
380 public void portChanged(Dpid dpid, OFPortStatus status) {
381 }
382
383 @Override
384 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
385 }
386 }
387
388}