sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.provider.of.group.impl; |
| 18 | |
dvaddire | b57fdb0 | 2017-06-20 00:14:08 +0530 | [diff] [blame] | 19 | import static org.onlab.util.Tools.getIntegerProperty; |
jiangrui | a6b4609 | 2015-11-11 16:28:51 +0800 | [diff] [blame] | 20 | import static org.slf4j.LoggerFactory.getLogger; |
| 21 | |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 22 | import java.util.ArrayList; |
jiangrui | a6b4609 | 2015-11-11 16:28:51 +0800 | [diff] [blame] | 23 | import java.util.Collection; |
dvaddire | b57fdb0 | 2017-06-20 00:14:08 +0530 | [diff] [blame] | 24 | import java.util.Dictionary; |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 25 | import java.util.Iterator; |
| 26 | import java.util.List; |
jiangrui | a6b4609 | 2015-11-11 16:28:51 +0800 | [diff] [blame] | 27 | import java.util.Map; |
| 28 | import java.util.Optional; |
dvaddire | b57fdb0 | 2017-06-20 00:14:08 +0530 | [diff] [blame] | 29 | import java.util.Properties; |
jiangrui | a6b4609 | 2015-11-11 16:28:51 +0800 | [diff] [blame] | 30 | import java.util.concurrent.atomic.AtomicLong; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 31 | |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 32 | import org.apache.felix.scr.annotations.Activate; |
| 33 | import org.apache.felix.scr.annotations.Component; |
| 34 | import org.apache.felix.scr.annotations.Deactivate; |
dvaddire | b57fdb0 | 2017-06-20 00:14:08 +0530 | [diff] [blame] | 35 | import org.apache.felix.scr.annotations.Modified; |
| 36 | import org.apache.felix.scr.annotations.Property; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 37 | import org.apache.felix.scr.annotations.Reference; |
| 38 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
dvaddire | b57fdb0 | 2017-06-20 00:14:08 +0530 | [diff] [blame] | 39 | import org.onosproject.cfg.ComponentConfigService; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 40 | import org.onosproject.core.GroupId; |
| 41 | import org.onosproject.net.DeviceId; |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 42 | import org.onosproject.net.PortNumber; |
| 43 | import org.onosproject.net.device.DeviceService; |
Laszlo Papp | 84adcac | 2018-03-13 12:11:51 +0000 | [diff] [blame^] | 44 | import org.onosproject.net.driver.Driver; |
jiangrui | a6b4609 | 2015-11-11 16:28:51 +0800 | [diff] [blame] | 45 | import org.onosproject.net.driver.DriverService; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 46 | import org.onosproject.net.group.DefaultGroup; |
| 47 | import org.onosproject.net.group.Group; |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 48 | import org.onosproject.net.group.GroupBucket; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 49 | import org.onosproject.net.group.GroupBuckets; |
| 50 | import org.onosproject.net.group.GroupDescription; |
| 51 | import org.onosproject.net.group.GroupOperation; |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 52 | import org.onosproject.net.group.GroupOperation.GroupMsgErrorCode; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 53 | import org.onosproject.net.group.GroupOperations; |
| 54 | import org.onosproject.net.group.GroupProvider; |
| 55 | import org.onosproject.net.group.GroupProviderRegistry; |
| 56 | import org.onosproject.net.group.GroupProviderService; |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 57 | import org.onosproject.net.group.GroupService; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 58 | import org.onosproject.net.group.StoredGroupBucketEntry; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 59 | import org.onosproject.net.provider.AbstractProvider; |
| 60 | import org.onosproject.net.provider.ProviderId; |
| 61 | import org.onosproject.openflow.controller.Dpid; |
| 62 | import org.onosproject.openflow.controller.OpenFlowController; |
| 63 | import org.onosproject.openflow.controller.OpenFlowEventListener; |
| 64 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 65 | import org.onosproject.openflow.controller.OpenFlowSwitchListener; |
| 66 | import org.onosproject.openflow.controller.RoleState; |
dvaddire | b57fdb0 | 2017-06-20 00:14:08 +0530 | [diff] [blame] | 67 | import org.osgi.service.component.ComponentContext; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 68 | import org.projectfloodlight.openflow.protocol.OFBucketCounter; |
Maksym Polovyi | 3585a7a | 2018-02-15 20:21:16 +0200 | [diff] [blame] | 69 | import org.projectfloodlight.openflow.protocol.OFCapabilities; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 70 | import org.projectfloodlight.openflow.protocol.OFErrorMsg; |
| 71 | import org.projectfloodlight.openflow.protocol.OFErrorType; |
| 72 | import org.projectfloodlight.openflow.protocol.OFGroupDescStatsEntry; |
| 73 | import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply; |
| 74 | import org.projectfloodlight.openflow.protocol.OFGroupMod; |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 75 | import org.projectfloodlight.openflow.protocol.OFGroupModFailedCode; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 76 | import org.projectfloodlight.openflow.protocol.OFGroupStatsEntry; |
| 77 | import org.projectfloodlight.openflow.protocol.OFGroupStatsReply; |
| 78 | import org.projectfloodlight.openflow.protocol.OFGroupType; |
| 79 | import org.projectfloodlight.openflow.protocol.OFMessage; |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 80 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 81 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
| 82 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
| 83 | import org.projectfloodlight.openflow.protocol.OFStatsType; |
sangho | 01a883c | 2015-02-23 11:01:50 -0800 | [diff] [blame] | 84 | import org.projectfloodlight.openflow.protocol.OFVersion; |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 85 | import org.projectfloodlight.openflow.protocol.errormsg.OFGroupModFailedErrorMsg; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 86 | import org.slf4j.Logger; |
| 87 | |
jiangrui | a6b4609 | 2015-11-11 16:28:51 +0800 | [diff] [blame] | 88 | import com.google.common.collect.Maps; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * Provider which uses an OpenFlow controller to handle Group. |
| 92 | */ |
| 93 | @Component(immediate = true) |
| 94 | public class OpenFlowGroupProvider extends AbstractProvider implements GroupProvider { |
| 95 | |
| 96 | private final Logger log = getLogger(getClass()); |
| 97 | |
| 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 99 | protected OpenFlowController controller; |
| 100 | |
| 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 102 | protected GroupProviderRegistry providerRegistry; |
| 103 | |
jiangrui | a6b4609 | 2015-11-11 16:28:51 +0800 | [diff] [blame] | 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 105 | protected DriverService driverService; |
| 106 | |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 107 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 108 | protected DeviceService deviceService; |
| 109 | |
| 110 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 111 | protected GroupService groupService; |
| 112 | |
dvaddire | b57fdb0 | 2017-06-20 00:14:08 +0530 | [diff] [blame] | 113 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 114 | protected ComponentConfigService cfgService; |
| 115 | |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 116 | private GroupProviderService providerService; |
| 117 | |
dvaddire | b57fdb0 | 2017-06-20 00:14:08 +0530 | [diff] [blame] | 118 | private static final int DEFAULT_POLL_INTERVAL = 10; |
| 119 | private static final String COMPONENT = "org.onosproject.provider.of.group.impl.OpenFlowGroupProvider"; |
| 120 | private static final String GROUP_POLL_INTERVAL_CONST = "groupPollInterval"; |
| 121 | |
| 122 | @Property(name = "groupPollInterval", intValue = DEFAULT_POLL_INTERVAL, |
| 123 | label = "Frequency (in seconds) for polling group statistics") |
| 124 | private int groupPollInterval = DEFAULT_POLL_INTERVAL; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 125 | |
| 126 | private final InternalGroupProvider listener = new InternalGroupProvider(); |
| 127 | |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 128 | private static final AtomicLong XID_COUNTER = new AtomicLong(1); |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 129 | private final Map<Dpid, GroupStatsCollector> collectors = Maps.newHashMap(); |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 130 | private final Map<Long, OFStatsReply> groupStats = Maps.newConcurrentMap(); |
sangho | 22a805d | 2015-02-13 09:45:43 -0800 | [diff] [blame] | 131 | private final Map<GroupId, GroupOperation> pendingGroupOperations = |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 132 | Maps.newConcurrentMap(); |
| 133 | |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 134 | /* Map<Group ID, Transaction ID> */ |
sangho | 22a805d | 2015-02-13 09:45:43 -0800 | [diff] [blame] | 135 | private final Map<GroupId, Long> pendingXidMaps = Maps.newConcurrentMap(); |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 136 | |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 137 | /** |
| 138 | * Creates a OpenFlow group provider. |
| 139 | */ |
| 140 | public OpenFlowGroupProvider() { |
| 141 | super(new ProviderId("of", "org.onosproject.provider.group")); |
| 142 | } |
| 143 | |
| 144 | @Activate |
dvaddire | b57fdb0 | 2017-06-20 00:14:08 +0530 | [diff] [blame] | 145 | public void activate(ComponentContext context) { |
| 146 | cfgService.registerProperties(getClass()); |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 147 | providerService = providerRegistry.register(this); |
| 148 | controller.addListener(listener); |
| 149 | controller.addEventListener(listener); |
| 150 | |
dvaddire | b57fdb0 | 2017-06-20 00:14:08 +0530 | [diff] [blame] | 151 | modified(context); |
| 152 | |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 153 | for (OpenFlowSwitch sw : controller.getSwitches()) { |
sangho | 01a883c | 2015-02-23 11:01:50 -0800 | [diff] [blame] | 154 | if (isGroupSupported(sw)) { |
dvaddire | b57fdb0 | 2017-06-20 00:14:08 +0530 | [diff] [blame] | 155 | GroupStatsCollector gsc = new GroupStatsCollector(sw, groupPollInterval); |
sangho | 01a883c | 2015-02-23 11:01:50 -0800 | [diff] [blame] | 156 | gsc.start(); |
| 157 | collectors.put(new Dpid(sw.getId()), gsc); |
| 158 | } |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | log.info("Started"); |
| 162 | } |
| 163 | |
| 164 | @Deactivate |
| 165 | public void deactivate() { |
dvaddire | b57fdb0 | 2017-06-20 00:14:08 +0530 | [diff] [blame] | 166 | cfgService.unregisterProperties(getClass(), false); |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 167 | providerRegistry.unregister(this); |
| 168 | providerService = null; |
Charles Chan | ecfdfb7 | 2015-11-24 19:05:50 -0800 | [diff] [blame] | 169 | collectors.values().forEach(GroupStatsCollector::stop); |
| 170 | collectors.clear(); |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 171 | log.info("Stopped"); |
| 172 | } |
| 173 | |
dvaddire | b57fdb0 | 2017-06-20 00:14:08 +0530 | [diff] [blame] | 174 | @Modified |
| 175 | public void modified(ComponentContext context) { |
| 176 | Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties(); |
| 177 | Integer newGroupPollInterval = getIntegerProperty(properties, GROUP_POLL_INTERVAL_CONST); |
| 178 | if (newGroupPollInterval != null && newGroupPollInterval > 0 |
| 179 | && newGroupPollInterval != groupPollInterval) { |
| 180 | groupPollInterval = newGroupPollInterval; |
| 181 | modifyPollInterval(); |
| 182 | } else if (newGroupPollInterval != null && newGroupPollInterval <= 0) { |
| 183 | log.warn("groupPollInterval must be greater than 0"); |
| 184 | //If the new value <= 0 reset property with old value. |
| 185 | cfgService.setProperty(COMPONENT, GROUP_POLL_INTERVAL_CONST, Integer.toString(groupPollInterval)); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | private void modifyPollInterval() { |
| 190 | collectors.values().forEach(gsc -> gsc.adjustRate(groupPollInterval)); |
| 191 | |
| 192 | } |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 193 | @Override |
| 194 | public void performGroupOperation(DeviceId deviceId, GroupOperations groupOps) { |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 195 | final Dpid dpid = Dpid.dpid(deviceId.uri()); |
| 196 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 197 | for (GroupOperation groupOperation: groupOps.operations()) { |
| 198 | if (sw == null) { |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 199 | log.error("SW {} is not found", dpid); |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 200 | return; |
| 201 | } |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 202 | final Long groupModXid = XID_COUNTER.getAndIncrement(); |
jiangrui | a6b4609 | 2015-11-11 16:28:51 +0800 | [diff] [blame] | 203 | GroupModBuilder builder = null; |
| 204 | if (driverService == null) { |
| 205 | builder = GroupModBuilder.builder(groupOperation.buckets(), |
| 206 | groupOperation.groupId(), |
| 207 | groupOperation.groupType(), |
| 208 | sw.factory(), |
| 209 | Optional.of(groupModXid)); |
| 210 | } else { |
| 211 | builder = GroupModBuilder.builder(groupOperation.buckets(), |
| 212 | groupOperation.groupId(), |
| 213 | groupOperation.groupType(), |
| 214 | sw.factory(), |
| 215 | Optional.of(groupModXid), |
| 216 | Optional.of(driverService)); |
| 217 | } |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 218 | OFGroupMod groupMod = null; |
| 219 | switch (groupOperation.opType()) { |
| 220 | case ADD: |
| 221 | groupMod = builder.buildGroupAdd(); |
| 222 | break; |
| 223 | case MODIFY: |
| 224 | groupMod = builder.buildGroupMod(); |
| 225 | break; |
| 226 | case DELETE: |
| 227 | groupMod = builder.buildGroupDel(); |
| 228 | break; |
| 229 | default: |
| 230 | log.error("Unsupported Group operation"); |
Satish K | e6d91c5 | 2015-11-21 19:04:20 +0530 | [diff] [blame] | 231 | return; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 232 | } |
| 233 | sw.sendMsg(groupMod); |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 234 | GroupId groudId = new GroupId(groupMod.getGroup().getGroupNumber()); |
sangho | 22a805d | 2015-02-13 09:45:43 -0800 | [diff] [blame] | 235 | pendingGroupOperations.put(groudId, groupOperation); |
| 236 | pendingXidMaps.put(groudId, groupModXid); |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
| 240 | private void pushGroupMetrics(Dpid dpid, OFStatsReply statsReply) { |
| 241 | DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid)); |
Maksym Polovyi | 3585a7a | 2018-02-15 20:21:16 +0200 | [diff] [blame] | 242 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 243 | boolean containsGroupStats = false; |
| 244 | if (sw != null && sw.features() != null) { |
| 245 | containsGroupStats = sw.features() |
| 246 | .getCapabilities() |
| 247 | .contains(OFCapabilities.GROUP_STATS); |
| 248 | } |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 249 | |
| 250 | OFGroupStatsReply groupStatsReply = null; |
| 251 | OFGroupDescStatsReply groupDescStatsReply = null; |
| 252 | |
Maksym Polovyi | 3585a7a | 2018-02-15 20:21:16 +0200 | [diff] [blame] | 253 | if (containsGroupStats) { |
| 254 | synchronized (groupStats) { |
| 255 | if (statsReply.getStatsType() == OFStatsType.GROUP) { |
| 256 | OFStatsReply reply = groupStats.get(statsReply.getXid() + 1); |
| 257 | if (reply != null) { |
| 258 | groupStatsReply = (OFGroupStatsReply) statsReply; |
| 259 | groupDescStatsReply = (OFGroupDescStatsReply) reply; |
| 260 | groupStats.remove(statsReply.getXid() + 1); |
| 261 | } else { |
| 262 | groupStats.put(statsReply.getXid(), statsReply); |
| 263 | } |
| 264 | } else if (statsReply.getStatsType() == OFStatsType.GROUP_DESC) { |
| 265 | OFStatsReply reply = groupStats.get(statsReply.getXid() - 1); |
| 266 | if (reply != null) { |
| 267 | groupStatsReply = (OFGroupStatsReply) reply; |
| 268 | groupDescStatsReply = (OFGroupDescStatsReply) statsReply; |
| 269 | groupStats.remove(statsReply.getXid() - 1); |
| 270 | } else { |
| 271 | groupStats.put(statsReply.getXid(), statsReply); |
| 272 | } |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 273 | } |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 274 | } |
Maksym Polovyi | 3585a7a | 2018-02-15 20:21:16 +0200 | [diff] [blame] | 275 | } else if (statsReply.getStatsType() == OFStatsType.GROUP_DESC) { |
| 276 | // We are only requesting group desc stats; see GroupStatsCollector.java:sendGroupStatisticRequests() |
| 277 | groupDescStatsReply = (OFGroupDescStatsReply) statsReply; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Maksym Polovyi | 3585a7a | 2018-02-15 20:21:16 +0200 | [diff] [blame] | 280 | if (providerService != null && groupDescStatsReply != null) { |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 281 | Collection<Group> groups = buildGroupMetrics(deviceId, |
| 282 | groupStatsReply, groupDescStatsReply); |
| 283 | providerService.pushGroupMetrics(deviceId, groups); |
| 284 | for (Group group: groups) { |
| 285 | pendingGroupOperations.remove(group.id()); |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 286 | pendingXidMaps.remove(group.id()); |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | private Collection<Group> buildGroupMetrics(DeviceId deviceId, |
| 292 | OFGroupStatsReply groupStatsReply, |
| 293 | OFGroupDescStatsReply groupDescStatsReply) { |
| 294 | |
| 295 | Map<Integer, Group> groups = Maps.newHashMap(); |
Hyunsun Moon | a834c18 | 2015-12-16 03:01:56 -0800 | [diff] [blame] | 296 | Dpid dpid = Dpid.dpid(deviceId.uri()); |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 297 | |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 298 | for (OFGroupDescStatsEntry entry: groupDescStatsReply.getEntries()) { |
| 299 | int id = entry.getGroup().getGroupNumber(); |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 300 | GroupId groupId = new GroupId(id); |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 301 | GroupDescription.Type type = getGroupType(entry.getGroupType()); |
Hyunsun Moon | a834c18 | 2015-12-16 03:01:56 -0800 | [diff] [blame] | 302 | GroupBuckets buckets = new GroupBucketEntryBuilder(dpid, entry.getBuckets(), |
| 303 | entry.getGroupType(), driverService).build(); |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 304 | DefaultGroup group = new DefaultGroup(groupId, deviceId, type, buckets); |
| 305 | groups.put(id, group); |
| 306 | } |
| 307 | |
Maksym Polovyi | 3585a7a | 2018-02-15 20:21:16 +0200 | [diff] [blame] | 308 | if (groupStatsReply == null) { |
| 309 | return groups.values(); |
| 310 | } |
| 311 | |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 312 | for (OFGroupStatsEntry entry: groupStatsReply.getEntries()) { |
| 313 | int groupId = entry.getGroup().getGroupNumber(); |
| 314 | DefaultGroup group = (DefaultGroup) groups.get(groupId); |
| 315 | if (group != null) { |
| 316 | group.setBytes(entry.getByteCount().getValue()); |
| 317 | group.setLife(entry.getDurationSec()); |
| 318 | group.setPackets(entry.getPacketCount().getValue()); |
| 319 | group.setReferenceCount(entry.getRefCount()); |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 320 | int bucketIndex = 0; |
| 321 | for (OFBucketCounter bucketStats:entry.getBucketStats()) { |
| 322 | ((StoredGroupBucketEntry) group.buckets().buckets() |
| 323 | .get(bucketIndex)) |
| 324 | .setPackets(bucketStats |
| 325 | .getPacketCount().getValue()); |
| 326 | ((StoredGroupBucketEntry) group.buckets().buckets() |
| 327 | .get(bucketIndex)) |
| 328 | .setBytes(entry.getBucketStats() |
| 329 | .get(bucketIndex) |
| 330 | .getByteCount().getValue()); |
| 331 | bucketIndex++; |
| 332 | } |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
| 336 | return groups.values(); |
| 337 | } |
| 338 | |
| 339 | private GroupDescription.Type getGroupType(OFGroupType type) { |
| 340 | switch (type) { |
| 341 | case ALL: |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 342 | return GroupDescription.Type.ALL; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 343 | case INDIRECT: |
| 344 | return GroupDescription.Type.INDIRECT; |
| 345 | case SELECT: |
| 346 | return GroupDescription.Type.SELECT; |
| 347 | case FF: |
| 348 | return GroupDescription.Type.FAILOVER; |
| 349 | default: |
| 350 | log.error("Unsupported OF group type : {}", type); |
| 351 | break; |
| 352 | } |
| 353 | return null; |
| 354 | } |
| 355 | |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 356 | /** |
| 357 | * Returns a transaction ID for entire group operations and increases |
| 358 | * the counter by the number given. |
| 359 | * |
| 360 | * @param increase the amount to increase the counter by |
| 361 | * @return a transaction ID |
| 362 | */ |
| 363 | public static long getXidAndAdd(int increase) { |
| 364 | return XID_COUNTER.getAndAdd(increase); |
| 365 | } |
| 366 | |
sangho | 01a883c | 2015-02-23 11:01:50 -0800 | [diff] [blame] | 367 | private boolean isGroupSupported(OpenFlowSwitch sw) { |
| 368 | if (sw.factory().getVersion() == OFVersion.OF_10 || |
| 369 | sw.factory().getVersion() == OFVersion.OF_11 || |
Laszlo Papp | 84adcac | 2018-03-13 12:11:51 +0000 | [diff] [blame^] | 370 | sw.factory().getVersion() == OFVersion.OF_12 || |
| 371 | !isGroupCapable(sw)) { |
sangho | 01a883c | 2015-02-23 11:01:50 -0800 | [diff] [blame] | 372 | return false; |
| 373 | } |
| 374 | |
| 375 | return true; |
| 376 | } |
| 377 | |
Laszlo Papp | 84adcac | 2018-03-13 12:11:51 +0000 | [diff] [blame^] | 378 | /** |
| 379 | * Determine whether the given switch is group-capable. |
| 380 | * |
| 381 | * @param sw switch |
| 382 | * @return the boolean value of groupCapable property, or true if it is not configured. |
| 383 | */ |
| 384 | private boolean isGroupCapable(OpenFlowSwitch sw) { |
| 385 | Driver driver = driverService.getDriver(DeviceId.deviceId(Dpid.uri(sw.getDpid()))); |
| 386 | if (driver == null) { |
| 387 | return true; |
| 388 | } |
| 389 | String isGroupCapable = driver.getProperty(GROUP_CAPABLE); |
| 390 | return isGroupCapable == null || Boolean.parseBoolean(isGroupCapable); |
| 391 | } |
| 392 | |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 393 | private class InternalGroupProvider |
| 394 | implements OpenFlowSwitchListener, OpenFlowEventListener { |
| 395 | |
| 396 | @Override |
| 397 | public void handleMessage(Dpid dpid, OFMessage msg) { |
| 398 | switch (msg.getType()) { |
| 399 | case STATS_REPLY: |
| 400 | pushGroupMetrics(dpid, (OFStatsReply) msg); |
| 401 | break; |
| 402 | case ERROR: |
| 403 | OFErrorMsg errorMsg = (OFErrorMsg) msg; |
| 404 | if (errorMsg.getErrType() == OFErrorType.GROUP_MOD_FAILED) { |
sangho | 22a805d | 2015-02-13 09:45:43 -0800 | [diff] [blame] | 405 | GroupId pendingGroupId = null; |
| 406 | for (Map.Entry<GroupId, Long> entry: pendingXidMaps.entrySet()) { |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 407 | if (entry.getValue() == errorMsg.getXid()) { |
| 408 | pendingGroupId = entry.getKey(); |
| 409 | break; |
| 410 | } |
| 411 | } |
sangho | 22a805d | 2015-02-13 09:45:43 -0800 | [diff] [blame] | 412 | if (pendingGroupId == null) { |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 413 | log.warn("Error for unknown group operation: {}", |
| 414 | errorMsg.getXid()); |
| 415 | } else { |
| 416 | GroupOperation operation = |
| 417 | pendingGroupOperations.get(pendingGroupId); |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 418 | DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid)); |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 419 | if (operation != null) { |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 420 | OFGroupModFailedCode code = |
| 421 | ((OFGroupModFailedErrorMsg) errorMsg).getCode(); |
| 422 | GroupMsgErrorCode failureCode = |
| 423 | GroupMsgErrorCode.values()[(code.ordinal())]; |
| 424 | GroupOperation failedOperation = GroupOperation |
| 425 | .createFailedGroupOperation(operation, failureCode); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 426 | log.warn("Received a group mod error {}", msg); |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 427 | providerService.groupOperationFailed(deviceId, |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 428 | failedOperation); |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 429 | pendingGroupOperations.remove(pendingGroupId); |
| 430 | pendingXidMaps.remove(pendingGroupId); |
sangho | a09f274 | 2015-02-06 14:49:47 -0800 | [diff] [blame] | 431 | } else { |
| 432 | log.error("Cannot find pending group operation with group ID: {}", |
| 433 | pendingGroupId); |
| 434 | } |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 435 | } |
| 436 | break; |
| 437 | } |
| 438 | default: |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame] | 439 | break; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
| 443 | @Override |
| 444 | public void switchAdded(Dpid dpid) { |
sangho | 01a883c | 2015-02-23 11:01:50 -0800 | [diff] [blame] | 445 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
Lei Xu | dee1aff | 2015-10-16 00:45:10 -0500 | [diff] [blame] | 446 | if (sw == null) { |
| 447 | return; |
| 448 | } |
Laszlo Papp | cd91bfd | 2018-01-29 14:30:34 +0000 | [diff] [blame] | 449 | if (isGroupSupported(sw)) { |
dvaddire | b57fdb0 | 2017-06-20 00:14:08 +0530 | [diff] [blame] | 450 | GroupStatsCollector gsc = new GroupStatsCollector(sw, groupPollInterval); |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 451 | stopCollectorIfNeeded(collectors.put(dpid, gsc)); |
Palash Kala | a439afe | 2017-05-16 14:53:15 +0900 | [diff] [blame] | 452 | gsc.start(); |
sangho | 01a883c | 2015-02-23 11:01:50 -0800 | [diff] [blame] | 453 | } |
Lei Xu | dee1aff | 2015-10-16 00:45:10 -0500 | [diff] [blame] | 454 | |
| 455 | //figure out race condition |
| 456 | if (controller.getSwitch(dpid) == null) { |
| 457 | switchRemoved(dpid); |
| 458 | } |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | @Override |
| 462 | public void switchRemoved(Dpid dpid) { |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 463 | stopCollectorIfNeeded(collectors.remove(dpid)); |
| 464 | } |
| 465 | |
| 466 | private void stopCollectorIfNeeded(GroupStatsCollector collector) { |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 467 | if (collector != null) { |
| 468 | collector.stop(); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | @Override |
| 473 | public void switchChanged(Dpid dpid) { |
| 474 | } |
| 475 | |
| 476 | @Override |
| 477 | public void portChanged(Dpid dpid, OFPortStatus status) { |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 478 | providerService.notifyOfFailovers(checkFailoverGroups(dpid, status)); |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | @Override |
| 482 | public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) { |
| 483 | } |
| 484 | } |
| 485 | |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 486 | /** |
| 487 | * Builds a list of failover Groups whose primary live bucket failed over |
| 488 | * (i.e. bucket in use has changed). |
| 489 | * |
| 490 | * @param dpid DPID of switch whose port's status changed |
| 491 | * @param status new status of port |
| 492 | * @return list of groups whose primary live bucket failed over |
| 493 | */ |
| 494 | private List<Group> checkFailoverGroups(Dpid dpid, OFPortStatus status) { |
| 495 | List<Group> groupList = new ArrayList<>(); |
| 496 | OFPortDesc desc = status.getDesc(); |
| 497 | PortNumber portNumber = PortNumber.portNumber(desc.getPortNo().getPortNumber()); |
| 498 | DeviceId id = DeviceId.deviceId(Dpid.uri(dpid)); |
| 499 | if (desc.isEnabled()) { |
| 500 | return groupList; |
| 501 | } |
| 502 | Iterator<Group> iterator = groupService.getGroups(id).iterator(); |
| 503 | while (iterator.hasNext()) { |
| 504 | Group group = iterator.next(); |
| 505 | if (group.type() == GroupDescription.Type.FAILOVER && |
| 506 | checkFailoverGroup(group, id, portNumber)) { |
| 507 | groupList.add(group); |
| 508 | } |
| 509 | } |
| 510 | return groupList; |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * Checks whether the first live port in the failover group's bucket |
| 515 | * has failed over. |
| 516 | * |
| 517 | * @param group failover group to be checked for failover |
| 518 | * @param id device ID of switch whose port's status changed |
| 519 | * @param portNumber port number of port that was disabled |
| 520 | * @return whether the failover group experienced failover |
| 521 | */ |
| 522 | private boolean checkFailoverGroup(Group group, DeviceId id, |
| 523 | PortNumber portNumber) { |
| 524 | boolean portReached = false; |
| 525 | boolean portEnabled = false; |
| 526 | Iterator<GroupBucket> bIterator = group.buckets().buckets().iterator(); |
| 527 | GroupBucket bucket; |
| 528 | while (bIterator.hasNext() && !portReached) { |
| 529 | bucket = bIterator.next(); |
| 530 | if (deviceService.getPort(id, bucket.watchPort()).isEnabled()) { |
| 531 | portEnabled = true; |
| 532 | } |
| 533 | if (bucket.watchPort().equals(portNumber)) { |
| 534 | portReached = true; |
| 535 | } |
| 536 | } |
| 537 | return portReached && !portEnabled; |
| 538 | } |
| 539 | |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 540 | } |