alshabib
Committed by Gerrit Code Review

OpenFlow Meter Provider - still need to parse meter stats

Change-Id: I4b313ea003b3801f81ec7d4823bf69de37fd8643
......@@ -32,7 +32,7 @@ public interface Band {
/**
* defines a simple DiffServ policer that remark
* the drop precedence of the DSCP eld in the
* the drop precedence of the DSCP field in the
* IP header of the packets that exceed the band
* rate value.
*/
......@@ -42,16 +42,32 @@ public interface Band {
/**
* The rate at which this meter applies.
*
* @return the integer value of the rate
* @return the long value of the rate
*/
int rate();
long rate();
/**
* The burst size at which the meter applies.
*
* @return the integer value of the size
* @return the long value of the size
*/
int burst();
long burst();
/**
* Only meaningful in the case of a REMARK band type.
* indicates by which amount the drop precedence of
* the packet should be increase if the band is exceeded.
*
* @return a short value
*/
short dropPrecedence();
/**
* Signals the type of band to create.
*
* @return a band type
*/
Type type();
}
......
......@@ -38,5 +38,52 @@ public enum MeterFailReason {
/**
* The meter that was attempted to be modified is unknown.
*/
UNKNOWN
UNKNOWN,
/**
* The operation for this meter installation timed out.
*/
TIMEOUT,
/**
* Invalid meter definition.
*/
INVALID_METER,
/**
* The target device is unknown.
*/
UNKNOWN_DEVICE,
/**
* Unknown command.
*/
UNKNOWN_COMMAND,
/**
* Unknown flags.
*/
UNKNOWN_FLAGS,
/**
* Bad rate value.
*/
BAD_RATE,
/**
* Bad burst size value.
*/
BAD_BURST,
/**
* Bad band.
*/
BAD_BAND,
/**
* Bad value value.
*/
BAD_BAND_VALUE
}
......
......@@ -41,7 +41,7 @@ public final class MeterId {
*
* @return an integer
*/
int id() {
public int id() {
return id;
}
......
......@@ -29,10 +29,10 @@ public interface MeterProviderService extends ProviderService<MeterProvider> {
/**
* Notifies the core that a meter operaton failed for a
* specific reason.
* @param deviceId
* @param operation
* @param operation the failed operation
* @param reason the failure reason
*/
void meterOperationFailed(DeviceId deviceId, MeterOperation operation,
void meterOperationFailed(MeterOperation operation,
MeterFailReason reason);
/**
......
......@@ -29,4 +29,5 @@
<artifact>mvn:${project.groupId}/onos-of-provider-packet/${project.version}</artifact>
<artifact>mvn:${project.groupId}/onos-of-provider-flow/${project.version}</artifact>
<artifact>mvn:${project.groupId}/onos-of-provider-group/${project.version}</artifact>
<artifact>mvn:${project.groupId}/onos-of-provider-meter/${project.version}</artifact>
</app>
......
......@@ -29,5 +29,6 @@
<bundle>mvn:${project.groupId}/onos-of-provider-packet/${project.version}</bundle>
<bundle>mvn:${project.groupId}/onos-of-provider-flow/${project.version}</bundle>
<bundle>mvn:${project.groupId}/onos-of-provider-group/${project.version}</bundle>
<bundle>mvn:${project.groupId}/onos-of-provider-meter/${project.version}</bundle>
</feature>
</features>
......
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.provider.of.meter.impl;
import org.onosproject.net.meter.Band;
import org.onosproject.net.meter.Meter;
import org.onosproject.net.meter.MeterId;
import org.projectfloodlight.openflow.protocol.OFFactory;
import org.projectfloodlight.openflow.protocol.OFMeterFlags;
import org.projectfloodlight.openflow.protocol.OFMeterMod;
import org.projectfloodlight.openflow.protocol.OFMeterModCommand;
import org.projectfloodlight.openflow.protocol.meterband.OFMeterBand;
import org.projectfloodlight.openflow.protocol.meterband.OFMeterBandDrop;
import org.projectfloodlight.openflow.protocol.meterband.OFMeterBandDscpRemark;
import org.slf4j.Logger;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.slf4j.LoggerFactory.getLogger;
/**
* Builder for a meter modification.
*/
public final class MeterModBuilder {
private final Logger log = getLogger(getClass());
private final long xid;
private final OFFactory factory;
private Meter.Unit unit = Meter.Unit.KB_PER_SEC;
private boolean burst = false;
private Integer id;
private Collection<Band> bands;
public MeterModBuilder(long xid, OFFactory factory) {
this.xid = xid;
this.factory = factory;
}
public static MeterModBuilder builder(long xid, OFFactory factory) {
return new MeterModBuilder(xid, factory);
}
public MeterModBuilder withRateUnit(Meter.Unit unit) {
this.unit = unit;
return this;
}
public MeterModBuilder burst() {
this.burst = true;
return this;
}
public MeterModBuilder withId(MeterId meterId) {
this.id = meterId.id();
return this;
}
public MeterModBuilder withBands(Collection<Band> bands) {
this.bands = bands;
return this;
}
public OFMeterMod add() {
validate();
OFMeterMod.Builder builder = builderMeterMod();
builder.setCommand(OFMeterModCommand.ADD.ordinal());
return builder.build();
}
public OFMeterMod remove() {
validate();
OFMeterMod.Builder builder = builderMeterMod();
builder.setCommand(OFMeterModCommand.DELETE.ordinal());
return builder.build();
}
public OFMeterMod modify() {
validate();
OFMeterMod.Builder builder = builderMeterMod();
builder.setCommand(OFMeterModCommand.MODIFY.ordinal());
return builder.build();
}
private OFMeterMod.Builder builderMeterMod() {
OFMeterMod.Builder builder = factory.buildMeterMod();
int flags = 0;
if (burst) {
// covering loxi short comings.
flags |= 1 << OFMeterFlags.BURST.ordinal();
}
switch (unit) {
case PKTS_PER_SEC:
flags |= 1 << OFMeterFlags.PKTPS.ordinal();
break;
case KB_PER_SEC:
flags |= 1 << OFMeterFlags.KBPS.ordinal();
break;
default:
log.warn("Unknown unit type {}", unit);
}
builder.setBands(buildBands());
builder.setFlags(flags)
.setMeterId(id)
.setXid(xid);
return builder;
}
private List<OFMeterBand> buildBands() {
return bands.stream().map(b -> {
switch (b.type()) {
case DROP:
OFMeterBandDrop.Builder dropBuilder =
factory.meterBands().buildDrop();
if (burst) {
dropBuilder.setBurstSize(b.burst());
}
dropBuilder.setRate(b.rate());
return dropBuilder.build();
case REMARK:
OFMeterBandDscpRemark.Builder remarkBand =
factory.meterBands().buildDscpRemark();
if (burst) {
remarkBand.setBurstSize(b.burst());
}
remarkBand.setRate(b.rate());
remarkBand.setPrecLevel(b.dropPrecedence());
return remarkBand.build();
default:
log.warn("Unknown band type {}", b.type());
return null;
}
}).filter(value -> value != null).collect(Collectors.toList());
}
private void validate() {
checkNotNull(id, "id cannot be null");
checkNotNull(bands, "Must have bands");
checkArgument(bands.size() > 0, "Must have at lease one band");
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.provider.of.meter.impl;
import org.jboss.netty.util.HashedWheelTimer;
import org.jboss.netty.util.Timeout;
import org.jboss.netty.util.TimerTask;
import org.onlab.util.Timer;
import org.onosproject.openflow.controller.OpenFlowSwitch;
import org.onosproject.openflow.controller.RoleState;
import org.projectfloodlight.openflow.protocol.OFMeterStatsRequest;
import org.slf4j.Logger;
import java.util.concurrent.TimeUnit;
import static org.slf4j.LoggerFactory.getLogger;
/*
* Sends Meter Stats Request and collect the Meter statistics with a time interval.
*/
public class MeterStatsCollector implements TimerTask {
private final HashedWheelTimer timer = Timer.getTimer();
private final OpenFlowSwitch sw;
private final Logger log = getLogger(getClass());
private final int refreshInterval;
private Timeout timeout;
private boolean stopTimer = false;
/**
* Creates a GroupStatsCollector object.
*
* @param sw Open Flow switch
* @param interval time interval for collecting group statistic
*/
public MeterStatsCollector(OpenFlowSwitch sw, int interval) {
this.sw = sw;
this.refreshInterval = interval;
}
@Override
public void run(Timeout timeout) throws Exception {
log.trace("Collecting stats for {}", sw.getStringId());
sendMeterStatistic();
if (!this.stopTimer) {
log.trace("Scheduling stats collection in {} seconds for {}",
this.refreshInterval, this.sw.getStringId());
timeout.getTimer().newTimeout(this, refreshInterval,
TimeUnit.SECONDS);
}
}
private void sendMeterStatistic() {
if (log.isTraceEnabled()) {
log.trace("sendMeterStatistics {}:{}", sw.getStringId(), sw.getRole());
}
if (sw.getRole() != RoleState.MASTER) {
return;
}
OFMeterStatsRequest.Builder builder =
sw.factory().buildMeterStatsRequest();
builder.setXid(0).setMeterId(0xFFFFFFFF);
sw.sendMsg(builder.build());
}
/**
* Starts the collector.
*/
public void start() {
log.info("Starting Meter Stats collection thread for {}", sw.getStringId());
timeout = timer.newTimeout(this, 1, TimeUnit.SECONDS);
}
/**
* Stops the collector.
*/
public void stop() {
log.info("Stopping Meter Stats collection thread for {}", sw.getStringId());
this.stopTimer = true;
timeout.cancel();
}
}
......@@ -16,18 +16,45 @@
package org.onosproject.provider.of.meter.impl;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalCause;
import com.google.common.cache.RemovalNotification;
import com.google.common.collect.Maps;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.onosproject.net.DeviceId;
import org.onosproject.net.meter.Meter;
import org.onosproject.net.meter.MeterFailReason;
import org.onosproject.net.meter.MeterOperation;
import org.onosproject.net.meter.MeterOperations;
import org.onosproject.net.meter.MeterProvider;
import org.onosproject.net.meter.MeterProviderRegistry;
import org.onosproject.net.meter.MeterProviderService;
import org.onosproject.net.provider.AbstractProvider;
import org.onosproject.net.provider.ProviderId;
import org.onosproject.openflow.controller.Dpid;
import org.onosproject.openflow.controller.OpenFlowController;
import org.onosproject.openflow.controller.OpenFlowEventListener;
import org.onosproject.openflow.controller.OpenFlowSwitch;
import org.onosproject.openflow.controller.OpenFlowSwitchListener;
import org.onosproject.openflow.controller.RoleState;
import org.projectfloodlight.openflow.protocol.OFErrorMsg;
import org.projectfloodlight.openflow.protocol.OFErrorType;
import org.projectfloodlight.openflow.protocol.OFMessage;
import org.projectfloodlight.openflow.protocol.OFPortStatus;
import org.projectfloodlight.openflow.protocol.OFStatsReply;
import org.projectfloodlight.openflow.protocol.OFVersion;
import org.projectfloodlight.openflow.protocol.errormsg.OFMeterModFailedErrorMsg;
import org.slf4j.Logger;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import static org.slf4j.LoggerFactory.getLogger;
/**
......@@ -36,11 +63,28 @@ import static org.slf4j.LoggerFactory.getLogger;
@Component(immediate = true)
public class OpenFlowMeterProvider extends AbstractProvider implements MeterProvider {
private final Logger log = getLogger(getClass());
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected OpenFlowController controller;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected MeterProviderRegistry providerRegistry;
private MeterProviderService providerService;
private static final AtomicLong XID_COUNTER = new AtomicLong(1);
static final int POLL_INTERVAL = 10;
static final long TIMEOUT = 30;
private Cache<Integer, MeterOperation> pendingOperations;
private Cache<Long, MeterOperation> pendingXid;
private InternalMeterListener listener = new InternalMeterListener();
private Map<Dpid, MeterStatsCollector> collectors = Maps.newHashMap();
/**
* Creates a OpenFlow meter provider.
......@@ -49,13 +93,232 @@ public class OpenFlowMeterProvider extends AbstractProvider implements MeterProv
super(new ProviderId("of", "org.onosproject.provider.meter"));
}
@Activate
public void activate() {
providerService = providerRegistry.register(this);
pendingOperations = CacheBuilder.newBuilder()
.expireAfterWrite(TIMEOUT, TimeUnit.SECONDS)
.removalListener((RemovalNotification<Integer, MeterOperation> notification) -> {
if (notification.getCause() == RemovalCause.EXPIRED) {
providerService.meterOperationFailed(notification.getValue(),
MeterFailReason.TIMEOUT);
}
}).build();
controller.addEventListener(listener);
controller.addListener(listener);
controller.getSwitches().forEach((sw -> createStatsCollection(sw)));
}
@Deactivate
public void deactivate() {
providerRegistry.unregister(this);
controller.removeEventListener(listener);
controller.removeListener(listener);
providerService = null;
}
@Override
public void performMeterOperation(DeviceId deviceId, MeterOperations meterOps) {
Dpid dpid = Dpid.dpid(deviceId.uri());
OpenFlowSwitch sw = controller.getSwitch(dpid);
if (sw == null) {
log.error("Unknown device {}", deviceId);
meterOps.operations().forEach(op ->
providerService.meterOperationFailed(op,
MeterFailReason.UNKNOWN_DEVICE)
);
return;
}
meterOps.operations().forEach(op -> performOperation(sw, op));
}
@Override
public void performMeterOperation(DeviceId deviceId, MeterOperation meterOp) {
Dpid dpid = Dpid.dpid(deviceId.uri());
OpenFlowSwitch sw = controller.getSwitch(dpid);
if (sw == null) {
log.error("Unknown device {}", deviceId);
providerService.meterOperationFailed(meterOp,
MeterFailReason.UNKNOWN_DEVICE);
return;
}
}
private void performOperation(OpenFlowSwitch sw, MeterOperation op) {
pendingOperations.put(op.meter().id().id(), op);
Meter meter = op.meter();
MeterModBuilder builder = MeterModBuilder.builder(meter.id().id(), sw.factory());
if (meter.isBurst()) {
builder.burst();
}
builder.withBands(meter.bands())
.withId(meter.id())
.withRateUnit(meter.unit());
switch (op.type()) {
case ADD:
sw.sendMsg(builder.add());
break;
case REMOVE:
sw.sendMsg(builder.remove());
break;
case MODIFY:
sw.sendMsg(builder.modify());
break;
default:
log.warn("Unknown Meter command {}; not sending anything",
op.type());
providerService.meterOperationFailed(op,
MeterFailReason.UNKNOWN_COMMAND);
}
}
private void createStatsCollection(OpenFlowSwitch sw) {
if (isMeterSupported(sw)) {
MeterStatsCollector msc = new MeterStatsCollector(sw, POLL_INTERVAL);
msc.start();
collectors.put(new Dpid(sw.getId()), msc);
}
}
private boolean isMeterSupported(OpenFlowSwitch sw) {
if (sw.factory().getVersion() == OFVersion.OF_10 ||
sw.factory().getVersion() == OFVersion.OF_11 ||
sw.factory().getVersion() == OFVersion.OF_12) {
return false;
}
return true;
}
private void pushMeterStats(Dpid dpid, OFStatsReply msg) {
}
private void signalMeterError(OFMeterModFailedErrorMsg meterError,
MeterOperation op) {
switch (meterError.getCode()) {
case UNKNOWN:
providerService.meterOperationFailed(op,
MeterFailReason.UNKNOWN_DEVICE);
break;
case METER_EXISTS:
providerService.meterOperationFailed(op,
MeterFailReason.EXISTING_METER);
break;
case INVALID_METER:
providerService.meterOperationFailed(op,
MeterFailReason.INVALID_METER);
break;
case UNKNOWN_METER:
providerService.meterOperationFailed(op,
MeterFailReason.UNKNOWN);
break;
case BAD_COMMAND:
providerService.meterOperationFailed(op,
MeterFailReason.UNKNOWN_COMMAND);
break;
case BAD_FLAGS:
providerService.meterOperationFailed(op,
MeterFailReason.UNKNOWN_FLAGS);
break;
case BAD_RATE:
providerService.meterOperationFailed(op,
MeterFailReason.BAD_RATE);
break;
case BAD_BURST:
providerService.meterOperationFailed(op,
MeterFailReason.BAD_BURST);
break;
case BAD_BAND:
providerService.meterOperationFailed(op,
MeterFailReason.BAD_BAND);
break;
case BAD_BAND_VALUE:
providerService.meterOperationFailed(op,
MeterFailReason.BAD_BAND_VALUE);
break;
case OUT_OF_METERS:
providerService.meterOperationFailed(op,
MeterFailReason.OUT_OF_METERS);
break;
case OUT_OF_BANDS:
providerService.meterOperationFailed(op,
MeterFailReason.OUT_OF_BANDS);
break;
default:
providerService.meterOperationFailed(op,
MeterFailReason.UNKNOWN);
}
}
private class InternalMeterListener
implements OpenFlowSwitchListener, OpenFlowEventListener {
@Override
public void handleMessage(Dpid dpid, OFMessage msg) {
switch (msg.getType()) {
case STATS_REPLY:
pushMeterStats(dpid, (OFStatsReply) msg);
break;
case ERROR:
OFErrorMsg error = (OFErrorMsg) msg;
if (error.getErrType() == OFErrorType.METER_MOD_FAILED) {
MeterOperation op =
pendingOperations.getIfPresent(error.getXid());
pendingOperations.invalidate(error.getXid());
if (op == null) {
log.warn("Unknown Meter operation failed {}", error);
} else {
OFMeterModFailedErrorMsg meterError =
(OFMeterModFailedErrorMsg) error;
signalMeterError(meterError, op);
}
}
break;
default:
break;
}
}
@Override
public void switchAdded(Dpid dpid) {
createStatsCollection(controller.getSwitch(dpid));
}
@Override
public void switchRemoved(Dpid dpid) {
MeterStatsCollector msc = collectors.remove(dpid);
if (msc != null) {
msc.stop();
}
}
@Override
public void switchChanged(Dpid dpid) {
}
@Override
public void portChanged(Dpid dpid, OFPortStatus status) {
}
@Override
public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
}
}
}
......