alshabib | e27055b | 2015-07-09 21:43:10 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
alshabib | e27055b | 2015-07-09 21:43:10 -0700 | [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.meter.impl; |
| 18 | |
alshabib | 1d2bc40 | 2015-07-31 17:04:11 -0700 | [diff] [blame] | 19 | |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 20 | import com.google.common.cache.Cache; |
| 21 | import com.google.common.cache.CacheBuilder; |
| 22 | import com.google.common.cache.RemovalCause; |
| 23 | import com.google.common.cache.RemovalNotification; |
Yuta HIGUCHI | 67f2cca | 2017-01-19 19:31:58 -0800 | [diff] [blame] | 24 | import com.google.common.collect.ImmutableSet; |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 25 | import com.google.common.collect.Maps; |
Miguel Borges de Freitas | f0f344e | 2021-06-23 14:09:52 +0100 | [diff] [blame^] | 26 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 27 | import org.osgi.service.component.annotations.Activate; |
| 28 | import org.osgi.service.component.annotations.Component; |
| 29 | import org.osgi.service.component.annotations.Deactivate; |
| 30 | import org.osgi.service.component.annotations.Reference; |
Miguel Borges de Freitas | f0f344e | 2021-06-23 14:09:52 +0100 | [diff] [blame^] | 31 | import org.osgi.service.component.annotations.Modified; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 32 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Charles Chan | b9cfc3a | 2017-08-04 17:50:21 -0700 | [diff] [blame] | 33 | import org.onlab.util.ItemNotFoundException; |
Miguel Borges de Freitas | f0f344e | 2021-06-23 14:09:52 +0100 | [diff] [blame^] | 34 | import org.onlab.util.Tools; |
| 35 | import org.onosproject.cfg.ComponentConfigService; |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 36 | import org.onosproject.core.CoreService; |
Charles Chan | a59f9b76 | 2017-07-30 18:09:44 -0700 | [diff] [blame] | 37 | import org.onosproject.net.driver.Driver; |
| 38 | import org.onosproject.net.driver.DriverService; |
alshabib | 10c810b | 2015-08-18 16:59:04 -0700 | [diff] [blame] | 39 | import org.onosproject.net.meter.Band; |
| 40 | import org.onosproject.net.meter.DefaultBand; |
| 41 | import org.onosproject.net.meter.DefaultMeter; |
| 42 | import org.onosproject.net.meter.Meter; |
| 43 | import org.onosproject.net.meter.MeterFailReason; |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 44 | import org.onosproject.net.meter.MeterFeatures; |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 45 | import org.onosproject.net.meter.MeterId; |
alshabib | 10c810b | 2015-08-18 16:59:04 -0700 | [diff] [blame] | 46 | import org.onosproject.net.meter.MeterOperation; |
| 47 | import org.onosproject.net.meter.MeterOperations; |
| 48 | import org.onosproject.net.meter.MeterProvider; |
| 49 | import org.onosproject.net.meter.MeterProviderRegistry; |
| 50 | import org.onosproject.net.meter.MeterProviderService; |
| 51 | import org.onosproject.net.meter.MeterState; |
Yuta HIGUCHI | 67f2cca | 2017-01-19 19:31:58 -0800 | [diff] [blame] | 52 | import org.onosproject.net.Device; |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 53 | import org.onosproject.net.DeviceId; |
alshabib | e27055b | 2015-07-09 21:43:10 -0700 | [diff] [blame] | 54 | import org.onosproject.net.provider.AbstractProvider; |
| 55 | import org.onosproject.net.provider.ProviderId; |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 56 | import org.onosproject.openflow.controller.Dpid; |
alshabib | e27055b | 2015-07-09 21:43:10 -0700 | [diff] [blame] | 57 | import org.onosproject.openflow.controller.OpenFlowController; |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 58 | import org.onosproject.openflow.controller.OpenFlowEventListener; |
| 59 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 60 | import org.onosproject.openflow.controller.OpenFlowSwitchListener; |
| 61 | import org.onosproject.openflow.controller.RoleState; |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 62 | import org.onosproject.provider.of.meter.util.MeterFeaturesBuilder; |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 63 | import org.projectfloodlight.openflow.protocol.OFErrorMsg; |
| 64 | import org.projectfloodlight.openflow.protocol.OFErrorType; |
| 65 | import org.projectfloodlight.openflow.protocol.OFMessage; |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 66 | import org.projectfloodlight.openflow.protocol.OFMeterBandStats; |
| 67 | import org.projectfloodlight.openflow.protocol.OFMeterConfigStatsReply; |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 68 | import org.projectfloodlight.openflow.protocol.OFMeterFeatures; |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 69 | import org.projectfloodlight.openflow.protocol.OFMeterStats; |
| 70 | import org.projectfloodlight.openflow.protocol.OFMeterStatsReply; |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 71 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
| 72 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 73 | import org.projectfloodlight.openflow.protocol.OFStatsType; |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 74 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 75 | import org.projectfloodlight.openflow.protocol.errormsg.OFMeterModFailedErrorMsg; |
alshabib | e27055b | 2015-07-09 21:43:10 -0700 | [diff] [blame] | 76 | import org.slf4j.Logger; |
| 77 | |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 78 | import java.util.Collection; |
Miguel Borges de Freitas | f0f344e | 2021-06-23 14:09:52 +0100 | [diff] [blame^] | 79 | import java.util.Dictionary; |
Yuta HIGUCHI | 67f2cca | 2017-01-19 19:31:58 -0800 | [diff] [blame] | 80 | import java.util.EnumSet; |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 81 | import java.util.List; |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 82 | import java.util.Map; |
Yuta HIGUCHI | 67f2cca | 2017-01-19 19:31:58 -0800 | [diff] [blame] | 83 | import java.util.Set; |
Miguel Borges de Freitas | f0f344e | 2021-06-23 14:09:52 +0100 | [diff] [blame^] | 84 | import java.util.Properties; |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 85 | import java.util.concurrent.TimeUnit; |
| 86 | import java.util.concurrent.atomic.AtomicLong; |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 87 | import java.util.stream.Collectors; |
Niraj Dubey | 47bde9f | 2019-10-07 18:03:57 +0530 | [diff] [blame] | 88 | import java.util.concurrent.ConcurrentHashMap; |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 89 | |
Miguel Borges de Freitas | f0f344e | 2021-06-23 14:09:52 +0100 | [diff] [blame^] | 90 | import static org.onlab.util.Tools.getIntegerProperty; |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 91 | import static org.onosproject.net.DeviceId.deviceId; |
| 92 | import static org.onosproject.openflow.controller.Dpid.uri; |
Miguel Borges de Freitas | f0f344e | 2021-06-23 14:09:52 +0100 | [diff] [blame^] | 93 | import static org.onosproject.provider.of.meter.impl.OsgiPropertyConstants.*; |
alshabib | e27055b | 2015-07-09 21:43:10 -0700 | [diff] [blame] | 94 | import static org.slf4j.LoggerFactory.getLogger; |
| 95 | |
| 96 | /** |
| 97 | * Provider which uses an OpenFlow controller to handle meters. |
| 98 | */ |
Miguel Borges de Freitas | f0f344e | 2021-06-23 14:09:52 +0100 | [diff] [blame^] | 99 | @Component(immediate = true, enabled = true, property = { |
| 100 | FORCE_STATS_AFTER_METER_REMOVAL + ":Boolean=" + FORCE_STATS_AFTER_METER_REMOVAL_ENABLED_DEFAULT, |
| 101 | METER_STATS_POLL_INTERVAL + ":Integer=" + METER_STATS_POLL_INTERVAL_DEFAULT, |
| 102 | }) |
| 103 | |
alshabib | e27055b | 2015-07-09 21:43:10 -0700 | [diff] [blame] | 104 | public class OpenFlowMeterProvider extends AbstractProvider implements MeterProvider { |
| 105 | |
| 106 | private final Logger log = getLogger(getClass()); |
| 107 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
alshabib | e27055b | 2015-07-09 21:43:10 -0700 | [diff] [blame] | 109 | protected OpenFlowController controller; |
| 110 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 111 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 112 | protected MeterProviderRegistry providerRegistry; |
| 113 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 114 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 115 | protected CoreService coreService; |
| 116 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Charles Chan | a59f9b76 | 2017-07-30 18:09:44 -0700 | [diff] [blame] | 118 | protected DriverService driverService; |
| 119 | |
Miguel Borges de Freitas | f0f344e | 2021-06-23 14:09:52 +0100 | [diff] [blame^] | 120 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 121 | protected ComponentConfigService cfgService; |
| 122 | |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 123 | private MeterProviderService providerService; |
| 124 | |
Miguel Borges de Freitas | f0f344e | 2021-06-23 14:09:52 +0100 | [diff] [blame^] | 125 | private boolean forceStatsAfterMeterRemoval = FORCE_STATS_AFTER_METER_REMOVAL_ENABLED_DEFAULT; |
| 126 | |
| 127 | private int meterStatsPollInterval = METER_STATS_POLL_INTERVAL_DEFAULT; |
| 128 | |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 129 | private static final AtomicLong XID_COUNTER = new AtomicLong(1); |
| 130 | |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 131 | static final long TIMEOUT = 30; |
| 132 | |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 133 | private Cache<Long, MeterOperation> pendingOperations; |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 134 | |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 135 | private InternalMeterListener listener = new InternalMeterListener(); |
Niraj Dubey | 47bde9f | 2019-10-07 18:03:57 +0530 | [diff] [blame] | 136 | private Map<Dpid, MeterStatsCollector> collectors = new ConcurrentHashMap<>(); |
alshabib | e27055b | 2015-07-09 21:43:10 -0700 | [diff] [blame] | 137 | |
Yuta HIGUCHI | 67f2cca | 2017-01-19 19:31:58 -0800 | [diff] [blame] | 138 | private static final Set<Device.Type> NO_METER_SUPPORT = |
| 139 | ImmutableSet.copyOf(EnumSet.of(Device.Type.ROADM, |
| 140 | Device.Type.ROADM_OTN, |
| 141 | Device.Type.FIBER_SWITCH, |
Andrea Campanella | 1c24fb9 | 2018-12-20 16:43:59 +0100 | [diff] [blame] | 142 | Device.Type.OTN, |
| 143 | Device.Type.OLS, |
| 144 | Device.Type.TERMINAL_DEVICE)); |
Yuta HIGUCHI | 67f2cca | 2017-01-19 19:31:58 -0800 | [diff] [blame] | 145 | |
alshabib | e27055b | 2015-07-09 21:43:10 -0700 | [diff] [blame] | 146 | /** |
| 147 | * Creates a OpenFlow meter provider. |
| 148 | */ |
| 149 | public OpenFlowMeterProvider() { |
| 150 | super(new ProviderId("of", "org.onosproject.provider.meter")); |
| 151 | } |
alshabib | bc37196 | 2015-07-09 22:26:21 -0700 | [diff] [blame] | 152 | |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 153 | @Activate |
Miguel Borges de Freitas | f0f344e | 2021-06-23 14:09:52 +0100 | [diff] [blame^] | 154 | public void activate(ComponentContext context) { |
| 155 | cfgService.registerProperties(getClass()); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 156 | providerService = providerRegistry.register(this); |
| 157 | |
| 158 | pendingOperations = CacheBuilder.newBuilder() |
| 159 | .expireAfterWrite(TIMEOUT, TimeUnit.SECONDS) |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 160 | .removalListener((RemovalNotification<Long, MeterOperation> notification) -> { |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 161 | if (notification.getCause() == RemovalCause.EXPIRED) { |
Gamze Abaka | a34469f | 2019-04-19 08:30:16 +0000 | [diff] [blame] | 162 | log.debug("Expired on meter provider. Meter key {} and operation {}", |
| 163 | notification.getKey(), notification.getValue()); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 164 | providerService.meterOperationFailed(notification.getValue(), |
| 165 | MeterFailReason.TIMEOUT); |
| 166 | } |
| 167 | }).build(); |
| 168 | |
| 169 | controller.addEventListener(listener); |
| 170 | controller.addListener(listener); |
| 171 | |
Miguel Borges de Freitas | f0f344e | 2021-06-23 14:09:52 +0100 | [diff] [blame^] | 172 | modified(context); |
| 173 | |
| 174 | if (collectors.isEmpty()) { |
| 175 | controller.getSwitches().forEach((sw -> createStatsCollection(sw))); |
| 176 | } |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | @Deactivate |
| 180 | public void deactivate() { |
Miguel Borges de Freitas | f0f344e | 2021-06-23 14:09:52 +0100 | [diff] [blame^] | 181 | cfgService.unregisterProperties(getClass(), false); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 182 | providerRegistry.unregister(this); |
Charles Chan | ecfdfb7 | 2015-11-24 19:05:50 -0800 | [diff] [blame] | 183 | collectors.values().forEach(MeterStatsCollector::stop); |
| 184 | collectors.clear(); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 185 | controller.removeEventListener(listener); |
| 186 | controller.removeListener(listener); |
| 187 | providerService = null; |
| 188 | } |
| 189 | |
Miguel Borges de Freitas | f0f344e | 2021-06-23 14:09:52 +0100 | [diff] [blame^] | 190 | @Modified |
| 191 | public void modified(ComponentContext context) { |
| 192 | Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties(); |
| 193 | |
| 194 | // update FORCE_STATS_AFTER_METER_REMOVAL if needed |
| 195 | Boolean forceStatsAfterMeterRemovalEnabled = |
| 196 | Tools.isPropertyEnabled(properties, FORCE_STATS_AFTER_METER_REMOVAL); |
| 197 | if (forceStatsAfterMeterRemovalEnabled == null) { |
| 198 | log.info("forceStatsAfterMeterRemoval is not configured, " + |
| 199 | "using current value of {}", forceStatsAfterMeterRemoval); |
| 200 | } else { |
| 201 | forceStatsAfterMeterRemoval = forceStatsAfterMeterRemovalEnabled; |
| 202 | log.info("Configured. forceStatsAfterMeterRemoval is {}", |
| 203 | forceStatsAfterMeterRemovalEnabled ? "enabled" : "disabled"); |
| 204 | } |
| 205 | |
| 206 | // update METER_STATS_POLL_INTERVAL if needed |
| 207 | Integer newMeterPollInterval = getIntegerProperty(properties, METER_STATS_POLL_INTERVAL); |
| 208 | if (newMeterPollInterval != null && newMeterPollInterval > 0 |
| 209 | && newMeterPollInterval != meterStatsPollInterval) { |
| 210 | meterStatsPollInterval = newMeterPollInterval; |
| 211 | // restart meter stats collectors, old instances will be automatically purged before creation |
| 212 | // in the call to createStatsCollection |
| 213 | controller.getSwitches().forEach((sw -> { |
| 214 | createStatsCollection(sw); |
| 215 | })); |
| 216 | log.info("Configured. meterStatsPollInterval to {}", newMeterPollInterval); |
| 217 | } else if (newMeterPollInterval != null && newMeterPollInterval <= 0) { |
| 218 | log.warn("meterStatsPollInterval must be greater than 0"); |
| 219 | // Reset property with the old value |
| 220 | cfgService.setProperty(getClass().getName(), METER_STATS_POLL_INTERVAL, |
| 221 | Integer.toString(meterStatsPollInterval)); |
| 222 | } |
| 223 | } |
| 224 | |
alshabib | bc37196 | 2015-07-09 22:26:21 -0700 | [diff] [blame] | 225 | @Override |
| 226 | public void performMeterOperation(DeviceId deviceId, MeterOperations meterOps) { |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 227 | Dpid dpid = Dpid.dpid(deviceId.uri()); |
| 228 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 229 | if (sw == null) { |
| 230 | log.error("Unknown device {}", deviceId); |
| 231 | meterOps.operations().forEach(op -> |
| 232 | providerService.meterOperationFailed(op, |
| 233 | MeterFailReason.UNKNOWN_DEVICE) |
| 234 | ); |
| 235 | return; |
| 236 | } |
alshabib | bc37196 | 2015-07-09 22:26:21 -0700 | [diff] [blame] | 237 | |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 238 | meterOps.operations().forEach(op -> performOperation(sw, op)); |
alshabib | bc37196 | 2015-07-09 22:26:21 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public void performMeterOperation(DeviceId deviceId, MeterOperation meterOp) { |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 243 | Dpid dpid = Dpid.dpid(deviceId.uri()); |
| 244 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 245 | if (sw == null) { |
| 246 | log.error("Unknown device {}", deviceId); |
| 247 | providerService.meterOperationFailed(meterOp, |
| 248 | MeterFailReason.UNKNOWN_DEVICE); |
| 249 | return; |
| 250 | } |
alshabib | bc37196 | 2015-07-09 22:26:21 -0700 | [diff] [blame] | 251 | |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 252 | performOperation(sw, meterOp); |
| 253 | |
Miguel Borges de Freitas | f0f344e | 2021-06-23 14:09:52 +0100 | [diff] [blame^] | 254 | if (forceStatsAfterMeterRemoval && meterOp.type().equals(MeterOperation.Type.REMOVE)) { |
Jordi Ortiz | 9fe79a2 | 2017-04-25 12:57:11 +0200 | [diff] [blame] | 255 | forceMeterStats(deviceId); |
| 256 | } |
| 257 | |
| 258 | } |
| 259 | |
| 260 | private void forceMeterStats(DeviceId deviceId) { |
| 261 | Dpid dpid = Dpid.dpid(deviceId.uri()); |
| 262 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 263 | |
| 264 | MeterStatsCollector once = new MeterStatsCollector(sw, 1); |
Palash Kala | f95c38b | 2017-05-15 23:52:48 +0900 | [diff] [blame] | 265 | once.sendMeterStatisticRequest(); |
Jordi Ortiz | 9fe79a2 | 2017-04-25 12:57:11 +0200 | [diff] [blame] | 266 | |
alshabib | bc37196 | 2015-07-09 22:26:21 -0700 | [diff] [blame] | 267 | } |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 268 | |
| 269 | private void performOperation(OpenFlowSwitch sw, MeterOperation op) { |
| 270 | |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 271 | Meter meter = op.meter(); |
| 272 | MeterModBuilder builder = MeterModBuilder.builder(meter.id().id(), sw.factory()); |
| 273 | if (meter.isBurst()) { |
| 274 | builder.burst(); |
| 275 | } |
| 276 | builder.withBands(meter.bands()) |
| 277 | .withId(meter.id()) |
| 278 | .withRateUnit(meter.unit()); |
| 279 | |
| 280 | switch (op.type()) { |
| 281 | case ADD: |
Gamze Abaka | a34469f | 2019-04-19 08:30:16 +0000 | [diff] [blame] | 282 | pendingOperations.put(op.meter().id().id(), op); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 283 | sw.sendMsg(builder.add()); |
| 284 | break; |
| 285 | case REMOVE: |
| 286 | sw.sendMsg(builder.remove()); |
| 287 | break; |
| 288 | case MODIFY: |
Gamze Abaka | a34469f | 2019-04-19 08:30:16 +0000 | [diff] [blame] | 289 | pendingOperations.put(op.meter().id().id(), op); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 290 | sw.sendMsg(builder.modify()); |
| 291 | break; |
| 292 | default: |
| 293 | log.warn("Unknown Meter command {}; not sending anything", |
| 294 | op.type()); |
| 295 | providerService.meterOperationFailed(op, |
| 296 | MeterFailReason.UNKNOWN_COMMAND); |
| 297 | } |
| 298 | |
| 299 | } |
| 300 | |
| 301 | private void createStatsCollection(OpenFlowSwitch sw) { |
Madan Jampani | 84382b9 | 2016-06-22 08:26:49 -0700 | [diff] [blame] | 302 | if (sw != null && isMeterSupported(sw)) { |
Miguel Borges de Freitas | f0f344e | 2021-06-23 14:09:52 +0100 | [diff] [blame^] | 303 | MeterStatsCollector msc = new MeterStatsCollector(sw, meterStatsPollInterval); |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 304 | stopCollectorIfNeeded(collectors.put(new Dpid(sw.getId()), msc)); |
Palash Kala | a439afe | 2017-05-16 14:53:15 +0900 | [diff] [blame] | 305 | msc.start(); |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
| 309 | private void stopCollectorIfNeeded(MeterStatsCollector collector) { |
| 310 | if (collector != null) { |
| 311 | collector.stop(); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 315 | // TODO: ONOS-3546 Support per device enabling/disabling via network config |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 316 | private boolean isMeterSupported(OpenFlowSwitch sw) { |
| 317 | if (sw.factory().getVersion() == OFVersion.OF_10 || |
| 318 | sw.factory().getVersion() == OFVersion.OF_11 || |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 319 | sw.factory().getVersion() == OFVersion.OF_12 || |
Yuta HIGUCHI | 67f2cca | 2017-01-19 19:31:58 -0800 | [diff] [blame] | 320 | NO_METER_SUPPORT.contains(sw.deviceType()) || |
Charles Chan | a59f9b76 | 2017-07-30 18:09:44 -0700 | [diff] [blame] | 321 | !isMeterCapable(sw)) { |
sdn | 77b5cb8 | 2018-05-23 18:51:27 +0900 | [diff] [blame] | 322 | log.debug("{} does not support Meter.\n", sw.getDpid()); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 323 | return false; |
| 324 | } |
| 325 | |
| 326 | return true; |
| 327 | } |
| 328 | |
Charles Chan | a59f9b76 | 2017-07-30 18:09:44 -0700 | [diff] [blame] | 329 | /** |
| 330 | * Determine whether the given switch is meter-capable. |
| 331 | * |
| 332 | * @param sw switch |
| 333 | * @return the boolean value of meterCapable property, or true if it is not configured. |
| 334 | */ |
| 335 | private boolean isMeterCapable(OpenFlowSwitch sw) { |
Charles Chan | b9cfc3a | 2017-08-04 17:50:21 -0700 | [diff] [blame] | 336 | Driver driver; |
| 337 | |
| 338 | try { |
| 339 | driver = driverService.getDriver(DeviceId.deviceId(Dpid.uri(sw.getDpid()))); |
| 340 | } catch (ItemNotFoundException e) { |
| 341 | driver = driverService.getDriver(sw.manufacturerDescription(), sw.hardwareDescription(), |
| 342 | sw.softwareDescription()); |
| 343 | } |
| 344 | |
Charles Chan | a59f9b76 | 2017-07-30 18:09:44 -0700 | [diff] [blame] | 345 | String isMeterCapable = driver.getProperty(METER_CAPABLE); |
| 346 | return isMeterCapable == null || Boolean.parseBoolean(isMeterCapable); |
| 347 | } |
| 348 | |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 349 | private void pushMeterStats(Dpid dpid, OFStatsReply msg) { |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 350 | DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid)); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 351 | |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 352 | if (msg.getStatsType() == OFStatsType.METER) { |
| 353 | OFMeterStatsReply reply = (OFMeterStatsReply) msg; |
| 354 | Collection<Meter> meters = buildMeters(deviceId, reply.getEntries()); |
| 355 | //TODO do meter accounting here. |
| 356 | providerService.pushMeterMetrics(deviceId, meters); |
| 357 | } else if (msg.getStatsType() == OFStatsType.METER_CONFIG) { |
| 358 | OFMeterConfigStatsReply reply = (OFMeterConfigStatsReply) msg; |
| 359 | // FIXME: Map<Long, Meter> meters = collectMeters(deviceId, reply); |
| 360 | } |
| 361 | |
| 362 | } |
| 363 | |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 364 | private MeterFeatures buildMeterFeatures(Dpid dpid, OFMeterFeatures mf) { |
| 365 | if (mf != null) { |
| 366 | return new MeterFeaturesBuilder(mf, deviceId(uri(dpid))) |
| 367 | .build(); |
| 368 | } else { |
| 369 | // This will usually happen for OpenFlow devices prior to 1.3 |
| 370 | return MeterFeaturesBuilder.noMeterFeatures(deviceId(uri(dpid))); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | private void pushMeterFeatures(Dpid dpid, OFMeterFeatures meterFeatures) { |
| 375 | providerService.pushMeterFeatures(deviceId(uri(dpid)), |
| 376 | buildMeterFeatures(dpid, meterFeatures)); |
| 377 | } |
| 378 | |
| 379 | private void destroyMeterFeatures(Dpid dpid) { |
| 380 | providerService.deleteMeterFeatures(deviceId(uri(dpid))); |
| 381 | } |
| 382 | |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 383 | private Map<Long, Meter> collectMeters(DeviceId deviceId, |
| 384 | OFMeterConfigStatsReply reply) { |
| 385 | return Maps.newHashMap(); |
| 386 | //TODO: Needs a fix to be applied to loxi MeterConfig stat is incorrect |
| 387 | } |
| 388 | |
| 389 | private Collection<Meter> buildMeters(DeviceId deviceId, |
| 390 | List<OFMeterStats> entries) { |
| 391 | return entries.stream().map(stat -> { |
| 392 | DefaultMeter.Builder builder = DefaultMeter.builder(); |
| 393 | Collection<Band> bands = buildBands(stat.getBandStats()); |
| 394 | builder.forDevice(deviceId) |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 395 | .withId(MeterId.meterId(stat.getMeterId())) |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 396 | //FIXME: need to encode appId in meter id, but that makes |
| 397 | // things a little annoying for debugging |
| 398 | .fromApp(coreService.getAppId("org.onosproject.core")) |
| 399 | .withBands(bands); |
| 400 | DefaultMeter meter = builder.build(); |
| 401 | meter.setState(MeterState.ADDED); |
| 402 | meter.setLife(stat.getDurationSec()); |
| 403 | meter.setProcessedBytes(stat.getByteInCount().getValue()); |
| 404 | meter.setProcessedPackets(stat.getPacketInCount().getValue()); |
Cem Türker | 3baff67 | 2017-10-12 15:09:01 +0300 | [diff] [blame] | 405 | if (stat.getVersion().getWireVersion() < OFVersion.OF_15.getWireVersion()) { |
| 406 | meter.setReferenceCount(stat.getFlowCount()); |
| 407 | } |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 408 | // marks the meter as seen on the dataplane |
| 409 | pendingOperations.invalidate(stat.getMeterId()); |
| 410 | return meter; |
| 411 | }).collect(Collectors.toSet()); |
| 412 | } |
| 413 | |
| 414 | private Collection<Band> buildBands(List<OFMeterBandStats> bandStats) { |
| 415 | return bandStats.stream().map(stat -> { |
Jordi Ortiz | af75c13 | 2017-06-22 16:06:36 +0200 | [diff] [blame] | 416 | DefaultBand band = ((DefaultBand.Builder) DefaultBand.builder().ofType(DefaultBand.Type.DROP)).build(); |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 417 | band.setBytes(stat.getByteBandCount().getValue()); |
| 418 | band.setPackets(stat.getPacketBandCount().getValue()); |
| 419 | return band; |
Jordi Ortiz | af75c13 | 2017-06-22 16:06:36 +0200 | [diff] [blame] | 420 | }).collect(Collectors.toList()); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | private void signalMeterError(OFMeterModFailedErrorMsg meterError, |
| 424 | MeterOperation op) { |
| 425 | switch (meterError.getCode()) { |
| 426 | case UNKNOWN: |
| 427 | providerService.meterOperationFailed(op, |
| 428 | MeterFailReason.UNKNOWN_DEVICE); |
| 429 | break; |
| 430 | case METER_EXISTS: |
| 431 | providerService.meterOperationFailed(op, |
| 432 | MeterFailReason.EXISTING_METER); |
| 433 | break; |
| 434 | case INVALID_METER: |
| 435 | providerService.meterOperationFailed(op, |
| 436 | MeterFailReason.INVALID_METER); |
| 437 | break; |
| 438 | case UNKNOWN_METER: |
| 439 | providerService.meterOperationFailed(op, |
| 440 | MeterFailReason.UNKNOWN); |
| 441 | break; |
| 442 | case BAD_COMMAND: |
| 443 | providerService.meterOperationFailed(op, |
| 444 | MeterFailReason.UNKNOWN_COMMAND); |
| 445 | break; |
| 446 | case BAD_FLAGS: |
| 447 | providerService.meterOperationFailed(op, |
| 448 | MeterFailReason.UNKNOWN_FLAGS); |
| 449 | break; |
| 450 | case BAD_RATE: |
| 451 | providerService.meterOperationFailed(op, |
| 452 | MeterFailReason.BAD_RATE); |
| 453 | break; |
| 454 | case BAD_BURST: |
| 455 | providerService.meterOperationFailed(op, |
| 456 | MeterFailReason.BAD_BURST); |
| 457 | break; |
| 458 | case BAD_BAND: |
| 459 | providerService.meterOperationFailed(op, |
| 460 | MeterFailReason.BAD_BAND); |
| 461 | break; |
| 462 | case BAD_BAND_VALUE: |
| 463 | providerService.meterOperationFailed(op, |
| 464 | MeterFailReason.BAD_BAND_VALUE); |
| 465 | break; |
| 466 | case OUT_OF_METERS: |
| 467 | providerService.meterOperationFailed(op, |
| 468 | MeterFailReason.OUT_OF_METERS); |
| 469 | break; |
| 470 | case OUT_OF_BANDS: |
| 471 | providerService.meterOperationFailed(op, |
| 472 | MeterFailReason.OUT_OF_BANDS); |
| 473 | break; |
| 474 | default: |
| 475 | providerService.meterOperationFailed(op, |
| 476 | MeterFailReason.UNKNOWN); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | private class InternalMeterListener |
| 481 | implements OpenFlowSwitchListener, OpenFlowEventListener { |
| 482 | @Override |
| 483 | public void handleMessage(Dpid dpid, OFMessage msg) { |
| 484 | switch (msg.getType()) { |
| 485 | case STATS_REPLY: |
| 486 | pushMeterStats(dpid, (OFStatsReply) msg); |
| 487 | break; |
| 488 | case ERROR: |
| 489 | OFErrorMsg error = (OFErrorMsg) msg; |
| 490 | if (error.getErrType() == OFErrorType.METER_MOD_FAILED) { |
| 491 | MeterOperation op = |
| 492 | pendingOperations.getIfPresent(error.getXid()); |
| 493 | pendingOperations.invalidate(error.getXid()); |
| 494 | if (op == null) { |
| 495 | log.warn("Unknown Meter operation failed {}", error); |
| 496 | } else { |
| 497 | OFMeterModFailedErrorMsg meterError = |
| 498 | (OFMeterModFailedErrorMsg) error; |
| 499 | signalMeterError(meterError, op); |
| 500 | } |
| 501 | } |
| 502 | break; |
| 503 | default: |
| 504 | break; |
| 505 | } |
| 506 | |
| 507 | } |
| 508 | |
| 509 | @Override |
| 510 | public void switchAdded(Dpid dpid) { |
| 511 | createStatsCollection(controller.getSwitch(dpid)); |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 512 | pushMeterFeatures(dpid, controller.getSwitch(dpid).getMeterFeatures()); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | @Override |
| 516 | public void switchRemoved(Dpid dpid) { |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 517 | stopCollectorIfNeeded(collectors.remove(dpid)); |
Jordi Ortiz | aa8de49 | 2016-12-01 00:21:36 +0100 | [diff] [blame] | 518 | destroyMeterFeatures(dpid); |
alshabib | c791179 | 2015-07-30 17:55:30 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | @Override |
| 522 | public void switchChanged(Dpid dpid) { |
| 523 | |
| 524 | } |
| 525 | |
| 526 | @Override |
| 527 | public void portChanged(Dpid dpid, OFPortStatus status) { |
| 528 | |
| 529 | } |
| 530 | |
| 531 | @Override |
| 532 | public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) { |
| 533 | |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | |
| 538 | |
alshabib | e27055b | 2015-07-09 21:43:10 -0700 | [diff] [blame] | 539 | } |