blob: 3d0dacd5358df542bb1d948750cf8003b134fbee [file] [log] [blame]
alshabibe27055b2015-07-09 21:43:10 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
alshabibe27055b2015-07-09 21:43:10 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.provider.of.meter.impl;
18
alshabib1d2bc402015-07-31 17:04:11 -070019
alshabibc7911792015-07-30 17:55:30 -070020import com.google.common.cache.Cache;
21import com.google.common.cache.CacheBuilder;
22import com.google.common.cache.RemovalCause;
23import com.google.common.cache.RemovalNotification;
Yuta HIGUCHI67f2cca2017-01-19 19:31:58 -080024import com.google.common.collect.ImmutableSet;
alshabibc7911792015-07-30 17:55:30 -070025import com.google.common.collect.Maps;
26import org.apache.felix.scr.annotations.Activate;
alshabibe27055b2015-07-09 21:43:10 -070027import org.apache.felix.scr.annotations.Component;
alshabibc7911792015-07-30 17:55:30 -070028import org.apache.felix.scr.annotations.Deactivate;
alshabibe27055b2015-07-09 21:43:10 -070029import org.apache.felix.scr.annotations.Reference;
30import org.apache.felix.scr.annotations.ReferenceCardinality;
Charles Chanb9cfc3a2017-08-04 17:50:21 -070031import org.onlab.util.ItemNotFoundException;
alshabib7bb05012015-08-05 10:15:09 -070032import org.onosproject.core.CoreService;
Charles Chana59f9b762017-07-30 18:09:44 -070033import org.onosproject.net.driver.Driver;
34import org.onosproject.net.driver.DriverService;
alshabib10c810b2015-08-18 16:59:04 -070035import org.onosproject.net.meter.Band;
36import org.onosproject.net.meter.DefaultBand;
37import org.onosproject.net.meter.DefaultMeter;
38import org.onosproject.net.meter.Meter;
39import org.onosproject.net.meter.MeterFailReason;
Jordi Ortizaa8de492016-12-01 00:21:36 +010040import org.onosproject.net.meter.MeterFeatures;
alshabib58fe6dc2015-08-19 17:16:13 -070041import org.onosproject.net.meter.MeterId;
alshabib10c810b2015-08-18 16:59:04 -070042import org.onosproject.net.meter.MeterOperation;
43import org.onosproject.net.meter.MeterOperations;
44import org.onosproject.net.meter.MeterProvider;
45import org.onosproject.net.meter.MeterProviderRegistry;
46import org.onosproject.net.meter.MeterProviderService;
47import org.onosproject.net.meter.MeterState;
Yuta HIGUCHI67f2cca2017-01-19 19:31:58 -080048import org.onosproject.net.Device;
alshabib7bb05012015-08-05 10:15:09 -070049import org.onosproject.net.DeviceId;
alshabibe27055b2015-07-09 21:43:10 -070050import org.onosproject.net.provider.AbstractProvider;
51import org.onosproject.net.provider.ProviderId;
alshabibc7911792015-07-30 17:55:30 -070052import org.onosproject.openflow.controller.Dpid;
alshabibe27055b2015-07-09 21:43:10 -070053import org.onosproject.openflow.controller.OpenFlowController;
alshabibc7911792015-07-30 17:55:30 -070054import org.onosproject.openflow.controller.OpenFlowEventListener;
55import org.onosproject.openflow.controller.OpenFlowSwitch;
56import org.onosproject.openflow.controller.OpenFlowSwitchListener;
57import org.onosproject.openflow.controller.RoleState;
Jordi Ortizaa8de492016-12-01 00:21:36 +010058import org.onosproject.provider.of.meter.util.MeterFeaturesBuilder;
alshabibc7911792015-07-30 17:55:30 -070059import org.projectfloodlight.openflow.protocol.OFErrorMsg;
60import org.projectfloodlight.openflow.protocol.OFErrorType;
61import org.projectfloodlight.openflow.protocol.OFMessage;
alshabib7bb05012015-08-05 10:15:09 -070062import org.projectfloodlight.openflow.protocol.OFMeterBandStats;
63import org.projectfloodlight.openflow.protocol.OFMeterConfigStatsReply;
Jordi Ortizaa8de492016-12-01 00:21:36 +010064import org.projectfloodlight.openflow.protocol.OFMeterFeatures;
alshabib7bb05012015-08-05 10:15:09 -070065import org.projectfloodlight.openflow.protocol.OFMeterStats;
66import org.projectfloodlight.openflow.protocol.OFMeterStatsReply;
alshabibc7911792015-07-30 17:55:30 -070067import org.projectfloodlight.openflow.protocol.OFPortStatus;
68import org.projectfloodlight.openflow.protocol.OFStatsReply;
alshabib7bb05012015-08-05 10:15:09 -070069import org.projectfloodlight.openflow.protocol.OFStatsType;
alshabibc7911792015-07-30 17:55:30 -070070import org.projectfloodlight.openflow.protocol.OFVersion;
71import org.projectfloodlight.openflow.protocol.errormsg.OFMeterModFailedErrorMsg;
alshabibe27055b2015-07-09 21:43:10 -070072import org.slf4j.Logger;
73
alshabib7bb05012015-08-05 10:15:09 -070074import java.util.Collection;
Yuta HIGUCHI67f2cca2017-01-19 19:31:58 -080075import java.util.EnumSet;
alshabib7bb05012015-08-05 10:15:09 -070076import java.util.List;
alshabibc7911792015-07-30 17:55:30 -070077import java.util.Map;
Yuta HIGUCHI67f2cca2017-01-19 19:31:58 -080078import java.util.Set;
alshabibc7911792015-07-30 17:55:30 -070079import java.util.concurrent.TimeUnit;
80import java.util.concurrent.atomic.AtomicLong;
alshabib7bb05012015-08-05 10:15:09 -070081import java.util.stream.Collectors;
alshabibc7911792015-07-30 17:55:30 -070082
Jordi Ortizaa8de492016-12-01 00:21:36 +010083import static org.onosproject.net.DeviceId.deviceId;
84import static org.onosproject.openflow.controller.Dpid.uri;
alshabibe27055b2015-07-09 21:43:10 -070085import static org.slf4j.LoggerFactory.getLogger;
86
87/**
88 * Provider which uses an OpenFlow controller to handle meters.
89 */
alshabib58fe6dc2015-08-19 17:16:13 -070090@Component(immediate = true, enabled = true)
alshabibe27055b2015-07-09 21:43:10 -070091public class OpenFlowMeterProvider extends AbstractProvider implements MeterProvider {
92
93 private final Logger log = getLogger(getClass());
94
95 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 protected OpenFlowController controller;
97
alshabibc7911792015-07-30 17:55:30 -070098 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
99 protected MeterProviderRegistry providerRegistry;
100
alshabib7bb05012015-08-05 10:15:09 -0700101 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
102 protected CoreService coreService;
103
Charles Chana59f9b762017-07-30 18:09:44 -0700104 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
105 protected DriverService driverService;
106
alshabibc7911792015-07-30 17:55:30 -0700107 private MeterProviderService providerService;
108
109 private static final AtomicLong XID_COUNTER = new AtomicLong(1);
110
111 static final int POLL_INTERVAL = 10;
112 static final long TIMEOUT = 30;
113
alshabib7bb05012015-08-05 10:15:09 -0700114 private Cache<Long, MeterOperation> pendingOperations;
alshabibc7911792015-07-30 17:55:30 -0700115
116
117 private InternalMeterListener listener = new InternalMeterListener();
118 private Map<Dpid, MeterStatsCollector> collectors = Maps.newHashMap();
alshabibe27055b2015-07-09 21:43:10 -0700119
Yuta HIGUCHI67f2cca2017-01-19 19:31:58 -0800120 private static final Set<Device.Type> NO_METER_SUPPORT =
121 ImmutableSet.copyOf(EnumSet.of(Device.Type.ROADM,
122 Device.Type.ROADM_OTN,
123 Device.Type.FIBER_SWITCH,
124 Device.Type.OTN));
125
alshabibe27055b2015-07-09 21:43:10 -0700126 /**
127 * Creates a OpenFlow meter provider.
128 */
129 public OpenFlowMeterProvider() {
130 super(new ProviderId("of", "org.onosproject.provider.meter"));
131 }
alshabibbc371962015-07-09 22:26:21 -0700132
alshabibc7911792015-07-30 17:55:30 -0700133 @Activate
134 public void activate() {
135 providerService = providerRegistry.register(this);
136
137 pendingOperations = CacheBuilder.newBuilder()
138 .expireAfterWrite(TIMEOUT, TimeUnit.SECONDS)
alshabib7bb05012015-08-05 10:15:09 -0700139 .removalListener((RemovalNotification<Long, MeterOperation> notification) -> {
alshabibc7911792015-07-30 17:55:30 -0700140 if (notification.getCause() == RemovalCause.EXPIRED) {
141 providerService.meterOperationFailed(notification.getValue(),
142 MeterFailReason.TIMEOUT);
143 }
144 }).build();
145
146 controller.addEventListener(listener);
147 controller.addListener(listener);
148
149 controller.getSwitches().forEach((sw -> createStatsCollection(sw)));
150 }
151
152 @Deactivate
153 public void deactivate() {
154 providerRegistry.unregister(this);
Charles Chanecfdfb72015-11-24 19:05:50 -0800155 collectors.values().forEach(MeterStatsCollector::stop);
156 collectors.clear();
alshabibc7911792015-07-30 17:55:30 -0700157 controller.removeEventListener(listener);
158 controller.removeListener(listener);
159 providerService = null;
160 }
161
alshabibbc371962015-07-09 22:26:21 -0700162 @Override
163 public void performMeterOperation(DeviceId deviceId, MeterOperations meterOps) {
alshabibc7911792015-07-30 17:55:30 -0700164 Dpid dpid = Dpid.dpid(deviceId.uri());
165 OpenFlowSwitch sw = controller.getSwitch(dpid);
166 if (sw == null) {
167 log.error("Unknown device {}", deviceId);
168 meterOps.operations().forEach(op ->
169 providerService.meterOperationFailed(op,
170 MeterFailReason.UNKNOWN_DEVICE)
171 );
172 return;
173 }
alshabibbc371962015-07-09 22:26:21 -0700174
alshabibc7911792015-07-30 17:55:30 -0700175 meterOps.operations().forEach(op -> performOperation(sw, op));
alshabibbc371962015-07-09 22:26:21 -0700176 }
177
178 @Override
179 public void performMeterOperation(DeviceId deviceId, MeterOperation meterOp) {
alshabibc7911792015-07-30 17:55:30 -0700180 Dpid dpid = Dpid.dpid(deviceId.uri());
181 OpenFlowSwitch sw = controller.getSwitch(dpid);
182 if (sw == null) {
183 log.error("Unknown device {}", deviceId);
184 providerService.meterOperationFailed(meterOp,
185 MeterFailReason.UNKNOWN_DEVICE);
186 return;
187 }
alshabibbc371962015-07-09 22:26:21 -0700188
alshabib7bb05012015-08-05 10:15:09 -0700189 performOperation(sw, meterOp);
190
Jordi Ortiz9fe79a22017-04-25 12:57:11 +0200191 if (meterOp.type().equals(MeterOperation.Type.REMOVE)) {
192 forceMeterStats(deviceId);
193 }
194
195 }
196
197 private void forceMeterStats(DeviceId deviceId) {
198 Dpid dpid = Dpid.dpid(deviceId.uri());
199 OpenFlowSwitch sw = controller.getSwitch(dpid);
200
201 MeterStatsCollector once = new MeterStatsCollector(sw, 1);
Palash Kalaf95c38b2017-05-15 23:52:48 +0900202 once.sendMeterStatisticRequest();
Jordi Ortiz9fe79a22017-04-25 12:57:11 +0200203
alshabibbc371962015-07-09 22:26:21 -0700204 }
alshabibc7911792015-07-30 17:55:30 -0700205
206 private void performOperation(OpenFlowSwitch sw, MeterOperation op) {
207
208 pendingOperations.put(op.meter().id().id(), op);
209
210
211 Meter meter = op.meter();
212 MeterModBuilder builder = MeterModBuilder.builder(meter.id().id(), sw.factory());
213 if (meter.isBurst()) {
214 builder.burst();
215 }
216 builder.withBands(meter.bands())
217 .withId(meter.id())
218 .withRateUnit(meter.unit());
219
220 switch (op.type()) {
221 case ADD:
222 sw.sendMsg(builder.add());
223 break;
224 case REMOVE:
225 sw.sendMsg(builder.remove());
226 break;
227 case MODIFY:
228 sw.sendMsg(builder.modify());
229 break;
230 default:
231 log.warn("Unknown Meter command {}; not sending anything",
232 op.type());
233 providerService.meterOperationFailed(op,
234 MeterFailReason.UNKNOWN_COMMAND);
235 }
236
237 }
238
239 private void createStatsCollection(OpenFlowSwitch sw) {
Madan Jampani84382b92016-06-22 08:26:49 -0700240 if (sw != null && isMeterSupported(sw)) {
alshabibc7911792015-07-30 17:55:30 -0700241 MeterStatsCollector msc = new MeterStatsCollector(sw, POLL_INTERVAL);
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700242 stopCollectorIfNeeded(collectors.put(new Dpid(sw.getId()), msc));
Palash Kalaa439afe2017-05-16 14:53:15 +0900243 msc.start();
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700244 }
245 }
246
247 private void stopCollectorIfNeeded(MeterStatsCollector collector) {
248 if (collector != null) {
249 collector.stop();
alshabibc7911792015-07-30 17:55:30 -0700250 }
251 }
252
Charles Chan14967c22015-12-07 11:11:50 -0800253 // TODO: ONOS-3546 Support per device enabling/disabling via network config
alshabibc7911792015-07-30 17:55:30 -0700254 private boolean isMeterSupported(OpenFlowSwitch sw) {
255 if (sw.factory().getVersion() == OFVersion.OF_10 ||
256 sw.factory().getVersion() == OFVersion.OF_11 ||
Charles Chan14967c22015-12-07 11:11:50 -0800257 sw.factory().getVersion() == OFVersion.OF_12 ||
Yuta HIGUCHI67f2cca2017-01-19 19:31:58 -0800258 NO_METER_SUPPORT.contains(sw.deviceType()) ||
Charles Chana59f9b762017-07-30 18:09:44 -0700259 !isMeterCapable(sw)) {
alshabibc7911792015-07-30 17:55:30 -0700260 return false;
261 }
262
263 return true;
264 }
265
Charles Chana59f9b762017-07-30 18:09:44 -0700266 /**
267 * Determine whether the given switch is meter-capable.
268 *
269 * @param sw switch
270 * @return the boolean value of meterCapable property, or true if it is not configured.
271 */
272 private boolean isMeterCapable(OpenFlowSwitch sw) {
Charles Chanb9cfc3a2017-08-04 17:50:21 -0700273 Driver driver;
274
275 try {
276 driver = driverService.getDriver(DeviceId.deviceId(Dpid.uri(sw.getDpid())));
277 } catch (ItemNotFoundException e) {
278 driver = driverService.getDriver(sw.manufacturerDescription(), sw.hardwareDescription(),
279 sw.softwareDescription());
280 }
281
Charles Chana59f9b762017-07-30 18:09:44 -0700282 String isMeterCapable = driver.getProperty(METER_CAPABLE);
283 return isMeterCapable == null || Boolean.parseBoolean(isMeterCapable);
284 }
285
alshabibc7911792015-07-30 17:55:30 -0700286 private void pushMeterStats(Dpid dpid, OFStatsReply msg) {
alshabib7bb05012015-08-05 10:15:09 -0700287 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
alshabibc7911792015-07-30 17:55:30 -0700288
alshabib7bb05012015-08-05 10:15:09 -0700289 if (msg.getStatsType() == OFStatsType.METER) {
290 OFMeterStatsReply reply = (OFMeterStatsReply) msg;
291 Collection<Meter> meters = buildMeters(deviceId, reply.getEntries());
292 //TODO do meter accounting here.
293 providerService.pushMeterMetrics(deviceId, meters);
294 } else if (msg.getStatsType() == OFStatsType.METER_CONFIG) {
295 OFMeterConfigStatsReply reply = (OFMeterConfigStatsReply) msg;
296 // FIXME: Map<Long, Meter> meters = collectMeters(deviceId, reply);
297 }
298
299 }
300
Jordi Ortizaa8de492016-12-01 00:21:36 +0100301 private MeterFeatures buildMeterFeatures(Dpid dpid, OFMeterFeatures mf) {
302 if (mf != null) {
303 return new MeterFeaturesBuilder(mf, deviceId(uri(dpid)))
304 .build();
305 } else {
306 // This will usually happen for OpenFlow devices prior to 1.3
307 return MeterFeaturesBuilder.noMeterFeatures(deviceId(uri(dpid)));
308 }
309 }
310
311 private void pushMeterFeatures(Dpid dpid, OFMeterFeatures meterFeatures) {
312 providerService.pushMeterFeatures(deviceId(uri(dpid)),
313 buildMeterFeatures(dpid, meterFeatures));
314 }
315
316 private void destroyMeterFeatures(Dpid dpid) {
317 providerService.deleteMeterFeatures(deviceId(uri(dpid)));
318 }
319
alshabib7bb05012015-08-05 10:15:09 -0700320 private Map<Long, Meter> collectMeters(DeviceId deviceId,
321 OFMeterConfigStatsReply reply) {
322 return Maps.newHashMap();
323 //TODO: Needs a fix to be applied to loxi MeterConfig stat is incorrect
324 }
325
326 private Collection<Meter> buildMeters(DeviceId deviceId,
327 List<OFMeterStats> entries) {
328 return entries.stream().map(stat -> {
329 DefaultMeter.Builder builder = DefaultMeter.builder();
330 Collection<Band> bands = buildBands(stat.getBandStats());
331 builder.forDevice(deviceId)
alshabib58fe6dc2015-08-19 17:16:13 -0700332 .withId(MeterId.meterId(stat.getMeterId()))
alshabib7bb05012015-08-05 10:15:09 -0700333 //FIXME: need to encode appId in meter id, but that makes
334 // things a little annoying for debugging
335 .fromApp(coreService.getAppId("org.onosproject.core"))
336 .withBands(bands);
337 DefaultMeter meter = builder.build();
338 meter.setState(MeterState.ADDED);
339 meter.setLife(stat.getDurationSec());
340 meter.setProcessedBytes(stat.getByteInCount().getValue());
341 meter.setProcessedPackets(stat.getPacketInCount().getValue());
342 meter.setReferenceCount(stat.getFlowCount());
343
344 // marks the meter as seen on the dataplane
345 pendingOperations.invalidate(stat.getMeterId());
346 return meter;
347 }).collect(Collectors.toSet());
348 }
349
350 private Collection<Band> buildBands(List<OFMeterBandStats> bandStats) {
351 return bandStats.stream().map(stat -> {
Jordi Ortizaf75c132017-06-22 16:06:36 +0200352 DefaultBand band = ((DefaultBand.Builder) DefaultBand.builder().ofType(DefaultBand.Type.DROP)).build();
alshabib7bb05012015-08-05 10:15:09 -0700353 band.setBytes(stat.getByteBandCount().getValue());
354 band.setPackets(stat.getPacketBandCount().getValue());
355 return band;
Jordi Ortizaf75c132017-06-22 16:06:36 +0200356 }).collect(Collectors.toList());
alshabibc7911792015-07-30 17:55:30 -0700357 }
358
359 private void signalMeterError(OFMeterModFailedErrorMsg meterError,
360 MeterOperation op) {
361 switch (meterError.getCode()) {
362 case UNKNOWN:
363 providerService.meterOperationFailed(op,
364 MeterFailReason.UNKNOWN_DEVICE);
365 break;
366 case METER_EXISTS:
367 providerService.meterOperationFailed(op,
368 MeterFailReason.EXISTING_METER);
369 break;
370 case INVALID_METER:
371 providerService.meterOperationFailed(op,
372 MeterFailReason.INVALID_METER);
373 break;
374 case UNKNOWN_METER:
375 providerService.meterOperationFailed(op,
376 MeterFailReason.UNKNOWN);
377 break;
378 case BAD_COMMAND:
379 providerService.meterOperationFailed(op,
380 MeterFailReason.UNKNOWN_COMMAND);
381 break;
382 case BAD_FLAGS:
383 providerService.meterOperationFailed(op,
384 MeterFailReason.UNKNOWN_FLAGS);
385 break;
386 case BAD_RATE:
387 providerService.meterOperationFailed(op,
388 MeterFailReason.BAD_RATE);
389 break;
390 case BAD_BURST:
391 providerService.meterOperationFailed(op,
392 MeterFailReason.BAD_BURST);
393 break;
394 case BAD_BAND:
395 providerService.meterOperationFailed(op,
396 MeterFailReason.BAD_BAND);
397 break;
398 case BAD_BAND_VALUE:
399 providerService.meterOperationFailed(op,
400 MeterFailReason.BAD_BAND_VALUE);
401 break;
402 case OUT_OF_METERS:
403 providerService.meterOperationFailed(op,
404 MeterFailReason.OUT_OF_METERS);
405 break;
406 case OUT_OF_BANDS:
407 providerService.meterOperationFailed(op,
408 MeterFailReason.OUT_OF_BANDS);
409 break;
410 default:
411 providerService.meterOperationFailed(op,
412 MeterFailReason.UNKNOWN);
413 }
414 }
415
416 private class InternalMeterListener
417 implements OpenFlowSwitchListener, OpenFlowEventListener {
418 @Override
419 public void handleMessage(Dpid dpid, OFMessage msg) {
420 switch (msg.getType()) {
421 case STATS_REPLY:
422 pushMeterStats(dpid, (OFStatsReply) msg);
423 break;
424 case ERROR:
425 OFErrorMsg error = (OFErrorMsg) msg;
426 if (error.getErrType() == OFErrorType.METER_MOD_FAILED) {
427 MeterOperation op =
428 pendingOperations.getIfPresent(error.getXid());
429 pendingOperations.invalidate(error.getXid());
430 if (op == null) {
431 log.warn("Unknown Meter operation failed {}", error);
432 } else {
433 OFMeterModFailedErrorMsg meterError =
434 (OFMeterModFailedErrorMsg) error;
435 signalMeterError(meterError, op);
436 }
437 }
438 break;
439 default:
440 break;
441 }
442
443 }
444
445 @Override
446 public void switchAdded(Dpid dpid) {
447 createStatsCollection(controller.getSwitch(dpid));
Jordi Ortizaa8de492016-12-01 00:21:36 +0100448 pushMeterFeatures(dpid, controller.getSwitch(dpid).getMeterFeatures());
alshabibc7911792015-07-30 17:55:30 -0700449 }
450
451 @Override
452 public void switchRemoved(Dpid dpid) {
Thomas Vachuskaa394b952016-06-14 15:02:09 -0700453 stopCollectorIfNeeded(collectors.remove(dpid));
Jordi Ortizaa8de492016-12-01 00:21:36 +0100454 destroyMeterFeatures(dpid);
alshabibc7911792015-07-30 17:55:30 -0700455 }
456
457 @Override
458 public void switchChanged(Dpid dpid) {
459
460 }
461
462 @Override
463 public void portChanged(Dpid dpid, OFPortStatus status) {
464
465 }
466
467 @Override
468 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
469
470 }
471 }
472
473
474
alshabibe27055b2015-07-09 21:43:10 -0700475}