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