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