Praseed Balakrishnan
Committed by Gerrit Code Review

MPLS label selector and treatment

Change-Id: Id1fba1e04155e6d97de4c8fd95573641537f1b7a
/*
* Copyright 2014 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.onlab.onos.optical.testapp;
import static org.slf4j.LoggerFactory.getLogger;
import java.util.HashMap;
import java.util.Map;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.onlab.onos.core.ApplicationId;
import org.onlab.onos.core.CoreService;
import org.onlab.onos.net.Device;
import org.onlab.onos.net.DeviceId;
import org.onlab.onos.net.PortNumber;
import org.onlab.onos.net.device.DeviceEvent;
import org.onlab.onos.net.device.DeviceListener;
import org.onlab.onos.net.device.DeviceService;
import org.onlab.onos.net.flow.DefaultFlowRule;
import org.onlab.onos.net.flow.DefaultTrafficSelector;
import org.onlab.onos.net.flow.DefaultTrafficTreatment;
import org.onlab.onos.net.flow.FlowRule;
import org.onlab.onos.net.flow.FlowRuleService;
import org.onlab.onos.net.flow.TrafficSelector;
import org.onlab.onos.net.flow.TrafficTreatment;
import org.onlab.packet.Ethernet;
import org.slf4j.Logger;
/**
* Sample reactive forwarding application.
*/
//@Component(immediate = true)
public class MPLSForwarding {
private final Logger log = getLogger(getClass());
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected FlowRuleService flowRuleService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected CoreService coreService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected DeviceService deviceService;
private ApplicationId appId;
private final InternalDeviceListener listener = new InternalDeviceListener();
private final Map<DeviceId, Integer> uglyMap = new HashMap<>();
@Activate
public void activate() {
appId = coreService.registerApplication("org.onlab.onos.testapp" +
".mplsfwd");
uglyMap.put(DeviceId.deviceId("of:0000000000000001"), 1);
uglyMap.put(DeviceId.deviceId("of:0000000000000002"), 2);
uglyMap.put(DeviceId.deviceId("of:0000000000000003"), 3);
deviceService.addListener(listener);
for (Device d : deviceService.getDevices()) {
pushRules(d);
}
log.info("Started with Application ID {}", appId.id());
}
@Deactivate
public void deactivate() {
flowRuleService.removeFlowRulesById(appId);
log.info("Stopped");
}
private void pushRules(Device device) {
TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
int inport = 1;
int outport = 2;
Integer mplsLabel = 101;
Integer switchNumber = uglyMap.get(device.id());
if (switchNumber == null) {
return;
}
switch (switchNumber) {
case 1:
sbuilder.matchInport(PortNumber.portNumber(inport));
tbuilder.setOutput(PortNumber.portNumber(outport))
.pushMpls()
.setMpls(mplsLabel);
break;
case 2:
sbuilder.matchMplsLabel(mplsLabel)
.matchEthType(Ethernet.MPLS_UNICAST)
.matchInport(PortNumber.portNumber(inport));
tbuilder.setOutput(PortNumber.portNumber(outport));
break;
case 3:
sbuilder.matchMplsLabel(mplsLabel)
.matchEthType(Ethernet.MPLS_UNICAST)
.matchInport(PortNumber.portNumber(inport));
tbuilder.popMpls().setOutput(PortNumber.portNumber(outport));
break;
default:
}
TrafficTreatment treatement = tbuilder.build();
TrafficSelector selector = sbuilder.build();
FlowRule f = new DefaultFlowRule(device.id(), selector,
treatement, 100, appId, 600, false);
flowRuleService.applyFlowRules(f);
}
public class InternalDeviceListener implements DeviceListener {
@Override
public void event(DeviceEvent event) {
switch (event.type()) {
case DEVICE_ADDED:
pushRules(event.subject());
break;
case DEVICE_AVAILABILITY_CHANGED:
break;
case DEVICE_MASTERSHIP_CHANGED:
break;
case DEVICE_REMOVED:
break;
case DEVICE_SUSPENDED:
break;
case DEVICE_UPDATED:
break;
case PORT_ADDED:
break;
case PORT_REMOVED:
break;
case PORT_UPDATED:
break;
default:
break;
}
}
}
}
......@@ -184,6 +184,11 @@ public final class DefaultTrafficSelector implements TrafficSelector {
}
@Override
public Builder matchMplsLabel(Integer mplsLabel) {
return add(Criteria.matchMplsLabel(mplsLabel));
}
@Override
public Builder matchLambda(Short lambda) {
return add(Criteria.matchLambda(lambda));
}
......@@ -191,7 +196,6 @@ public final class DefaultTrafficSelector implements TrafficSelector {
@Override
public Builder matchOpticalSignalType(Short signalType) {
return add(Criteria.matchOpticalSignalType(signalType));
}
@Override
......
......@@ -192,6 +192,22 @@ public final class DefaultTrafficTreatment implements TrafficTreatment {
}
@Override
public Builder pushMpls() {
return add(Instructions.pushMpls());
}
@Override
public Builder popMpls() {
return add(Instructions.popMpls());
}
@Override
public Builder setMpls(Integer mplsLabel) {
return add(Instructions.modMplsLabel(mplsLabel));
}
@Override
public Builder setLambda(short lambda) {
return add(Instructions.modL0Lambda(lambda));
}
......
......@@ -146,6 +146,14 @@ public interface TrafficSelector {
*/
public Builder matchTcpDst(Short tcpPort);
/**
* Matches on a MPLS label .
* @param mplsLabel a MPLS label.
* @return a selection builder
*/
public Builder matchMplsLabel(Integer mplsLabel);
/**
* Matches an optical signal ID or lambda.
*
......
......@@ -110,6 +110,25 @@ public interface TrafficTreatment {
public Builder setIpDst(IpAddress addr);
/**
* Push MPLS ether type.
* @return a treatment builder.
*/
public Builder pushMpls();
/**
* Pops MPLS ether type.
* @return a treatment builder.
*/
public Builder popMpls();
/**
* Sets the mpls label.
* @param mplsLabel MPLS label.
* @return a treatment builder.
*/
public Builder setMpls(Integer mplsLabel);
/**
* Sets the optical channel ID or lambda.
*
* @param lambda optical channel ID
......
......@@ -149,6 +149,16 @@ public final class Criteria {
}
/**
* Creates a match on MPLS label.
* @param mplsLabel MPLS label
* @return match criterion
*/
public static Criterion matchMplsLabel(Integer mplsLabel) {
return new MplsCriterion(mplsLabel);
}
/**
* Creates a match on lambda field using the specified value.
*
* @param lambda lamda to match on
......@@ -541,6 +551,52 @@ public final class Criteria {
}
}
public static final class MplsCriterion implements Criterion {
private final Integer mplsLabel;
public MplsCriterion(Integer mplsLabel) {
this.mplsLabel = mplsLabel;
}
@Override
public Type type() {
return Type.MPLS_LABEL;
}
public Integer label() {
return mplsLabel;
}
@Override
public String toString() {
return toStringHelper(type().toString())
.add("mpls", mplsLabel.intValue()).toString();
}
@Override
public int hashCode() {
return Objects.hash(mplsLabel, type());
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof MplsCriterion) {
MplsCriterion that = (MplsCriterion) obj;
return Objects.equals(mplsLabel, that.mplsLabel) &&
Objects.equals(this.type(), that.type());
}
return false;
}
}
public static final class LambdaCriterion implements Criterion {
private final short lambda;
......
......@@ -17,6 +17,7 @@ package org.onlab.onos.net.flow.instructions;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.onlab.onos.net.flow.instructions.L2ModificationInstruction.*;
import java.util.Objects;
......@@ -27,6 +28,8 @@ import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.L2SubType;
import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.L3SubType;
import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
import org.onlab.packet.Ethernet;
import org.onlab.packet.IpAddress;
import org.onlab.packet.MacAddress;
import org.onlab.packet.VlanId;
......@@ -87,7 +90,7 @@ public final class Instructions {
*/
public static L2ModificationInstruction modL2Dst(MacAddress addr) {
checkNotNull(addr, "Dst l2 address cannot be null");
return new L2ModificationInstruction.ModEtherInstruction(L2SubType.ETH_DST, addr);
return new ModEtherInstruction(L2SubType.ETH_DST, addr);
}
/**
......@@ -97,7 +100,7 @@ public final class Instructions {
*/
public static L2ModificationInstruction modVlanId(VlanId vlanId) {
checkNotNull(vlanId, "VLAN id cannot be null");
return new L2ModificationInstruction.ModVlanIdInstruction(vlanId);
return new ModVlanIdInstruction(vlanId);
}
/**
......@@ -107,10 +110,19 @@ public final class Instructions {
*/
public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) {
checkNotNull(vlanPcp, "VLAN Pcp cannot be null");
return new L2ModificationInstruction.ModVlanPcpInstruction(vlanPcp);
return new ModVlanPcpInstruction(vlanPcp);
}
/**
* Creates a MPLS label modification.
* @param mplsLabel to set.
* @return a L2 Modification
*/
public static L2ModificationInstruction modMplsLabel(Integer mplsLabel) {
checkNotNull(mplsLabel, "MPLS label cannot be null");
return new ModMplsLabelInstruction(mplsLabel);
}
/**
* Creates a L3 src modification.
* @param addr the ip address to modify to.
* @return a L3 modification
......@@ -130,6 +142,23 @@ public final class Instructions {
return new ModIPInstruction(L3SubType.IP_DST, addr);
}
/**
* Creates a mpls header instruction.
* @return a L2 modification.
*/
public static Instruction pushMpls() {
return new PushHeaderInstructions(L2SubType.MPLS_PUSH,
new Ethernet().setEtherType(Ethernet.MPLS_UNICAST));
}
/**
* Creates a mpls header instruction.
* @return a L2 modification.
*/
public static Instruction popMpls() {
return new PushHeaderInstructions(L2SubType.MPLS_POP,
new Ethernet().setEtherType(Ethernet.MPLS_UNICAST));
}
/*
* Output instructions
......
......@@ -19,6 +19,7 @@ import static com.google.common.base.MoreObjects.toStringHelper;
import java.util.Objects;
import org.onlab.packet.Ethernet;
import org.onlab.packet.MacAddress;
import org.onlab.packet.VlanId;
......@@ -49,7 +50,22 @@ public abstract class L2ModificationInstruction implements Instruction {
/**
* VLAN priority modification.
*/
VLAN_PCP
VLAN_PCP,
/**
* MPLS Label modification.
*/
MPLS_LABEL,
/**
* MPLS Push modification.
*/
MPLS_PUSH,
/**
* MPLS Pop modification.
*/
MPLS_POP
}
// TODO: Create factory class 'Instructions' that will have various factory
......@@ -114,6 +130,53 @@ public abstract class L2ModificationInstruction implements Instruction {
}
public static final class PushHeaderInstructions extends
L2ModificationInstruction {
private final L2SubType subtype;
private final Ethernet ethernetType;
public PushHeaderInstructions(L2SubType subType, Ethernet ethernetType) {
this.subtype = subType;
this.ethernetType = ethernetType;
}
public Ethernet ethernetType() {
return ethernetType;
}
@Override
public L2SubType subtype() {
return this.subtype;
}
@Override
public String toString() {
return toStringHelper(subtype().toString()).toString();
}
@Override
public int hashCode() {
return Objects.hash(type(), subtype);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof PushHeaderInstructions) {
PushHeaderInstructions that = (PushHeaderInstructions) obj;
return Objects.equals(this.type(), that.type()) &&
Objects.equals(subtype, that.subtype);
}
return false;
}
}
/**
* Represents a VLAN id modification instruction.
*/
......@@ -212,4 +275,51 @@ public abstract class L2ModificationInstruction implements Instruction {
}
/**
* Represents a MPLS label modification.
*/
public static final class ModMplsLabelInstruction extends
L2ModificationInstruction {
private final Integer mplsLabel;
public ModMplsLabelInstruction(Integer mplsLabel) {
this.mplsLabel = mplsLabel;
}
public Integer label() {
return mplsLabel;
}
@Override
public L2SubType subtype() {
return L2SubType.MPLS_LABEL;
}
@Override
public String toString() {
return toStringHelper(type().toString())
.add("mpls", mplsLabel.intValue()).toString();
}
@Override
public int hashCode() {
return Objects.hash(mplsLabel);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof ModMplsLabelInstruction) {
ModMplsLabelInstruction that = (ModMplsLabelInstruction) obj;
return Objects.equals(mplsLabel, that.mplsLabel) &&
Objects.equals(this.type(), that.type());
}
return false;
}
}
}
......
......@@ -286,6 +286,10 @@ public class FlowEntryBuilder {
case TCP_SRC:
builder.matchTcpSrc((short) match.get(MatchField.TCP_SRC).getPort());
break;
case MPLS_LABEL:
builder.matchMplsLabel((int) match.get(MatchField.MPLS_LABEL)
.getValue());
break;
case OCH_SIGID:
builder.matchLambda(match.get(MatchField.OCH_SIGID).getChannelNumber());
break;
......@@ -312,7 +316,6 @@ public class FlowEntryBuilder {
case IP_DSCP:
case IP_ECN:
case METADATA:
case MPLS_LABEL:
case MPLS_TC:
case SCTP_DST:
case SCTP_SRC:
......
......@@ -47,6 +47,7 @@ import org.projectfloodlight.openflow.types.Masked;
import org.projectfloodlight.openflow.types.OFPort;
import org.projectfloodlight.openflow.types.OFVlanVidMatch;
import org.projectfloodlight.openflow.types.TransportPort;
import org.projectfloodlight.openflow.types.U32;
import org.projectfloodlight.openflow.types.U8;
import org.projectfloodlight.openflow.types.VlanPcp;
import org.projectfloodlight.openflow.types.VlanVid;
......@@ -195,6 +196,11 @@ public abstract class FlowModBuilder {
tp = (TcpPortCriterion) c;
mBuilder.setExact(MatchField.TCP_SRC, TransportPort.of(tp.tcpPort()));
break;
case MPLS_LABEL:
Criteria.MplsCriterion mp = (Criteria.MplsCriterion) c;
mBuilder.setExact(MatchField.MPLS_LABEL,
U32.of(mp.label().intValue()));
break;
case OCH_SIGID:
LambdaCriterion lc = (LambdaCriterion) c;
mBuilder.setExact(MatchField.OCH_SIGID,
......@@ -227,7 +233,6 @@ public abstract class FlowModBuilder {
case IP_ECN:
case METADATA:
case MPLS_BOS:
case MPLS_LABEL:
case MPLS_TC:
case PBB_ISID:
case SCTP_DST:
......
......@@ -29,6 +29,8 @@ import org.onlab.onos.net.flow.instructions.L2ModificationInstruction;
import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction;
import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModMplsLabelInstruction;
import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.PushHeaderInstructions;
import org.onlab.onos.net.flow.instructions.L3ModificationInstruction;
import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
import org.onlab.packet.Ip4Address;
......@@ -41,11 +43,13 @@ import org.projectfloodlight.openflow.protocol.action.OFAction;
import org.projectfloodlight.openflow.protocol.match.Match;
import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
import org.projectfloodlight.openflow.types.CircuitSignalID;
import org.projectfloodlight.openflow.types.EthType;
import org.projectfloodlight.openflow.types.IPv4Address;
import org.projectfloodlight.openflow.types.MacAddress;
import org.projectfloodlight.openflow.types.OFBufferId;
import org.projectfloodlight.openflow.types.OFPort;
import org.projectfloodlight.openflow.types.OFVlanVidMatch;
import org.projectfloodlight.openflow.types.U32;
import org.projectfloodlight.openflow.types.U64;
import org.projectfloodlight.openflow.types.VlanPcp;
import org.slf4j.Logger;
......@@ -201,25 +205,42 @@ public class FlowModBuilderVer13 extends FlowModBuilder {
ModEtherInstruction eth;
OFOxm<?> oxm = null;
switch (l2m.subtype()) {
case ETH_DST:
eth = (ModEtherInstruction) l2m;
oxm = factory().oxms().ethDst(MacAddress.of(eth.mac().toLong()));
break;
case ETH_SRC:
eth = (ModEtherInstruction) l2m;
oxm = factory().oxms().ethSrc(MacAddress.of(eth.mac().toLong()));
break;
case VLAN_ID:
ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m;
oxm = factory().oxms().vlanVid(OFVlanVidMatch.ofVlan(vlanId.vlanId().toShort()));
break;
case VLAN_PCP:
ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m;
oxm = factory().oxms().vlanPcp(VlanPcp.of(vlanPcp.vlanPcp()));
break;
default:
log.warn("Unimplemented action type {}.", l2m.subtype());
break;
case ETH_DST:
eth = (ModEtherInstruction) l2m;
oxm = factory().oxms().ethDst(MacAddress.of(eth.mac().toLong()));
break;
case ETH_SRC:
eth = (ModEtherInstruction) l2m;
oxm = factory().oxms().ethSrc(MacAddress.of(eth.mac().toLong()));
break;
case VLAN_ID:
ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m;
oxm = factory().oxms().vlanVid(OFVlanVidMatch.ofVlan(vlanId.vlanId().toShort()));
break;
case VLAN_PCP:
ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m;
oxm = factory().oxms().vlanPcp(VlanPcp.of(vlanPcp.vlanPcp()));
break;
case MPLS_PUSH:
PushHeaderInstructions pushHeaderInstructions =
(PushHeaderInstructions) l2m;
return factory().actions().pushMpls(EthType.of(pushHeaderInstructions
.ethernetType().getEtherType()));
case MPLS_POP:
PushHeaderInstructions popHeaderInstructions =
(PushHeaderInstructions) l2m;
return factory().actions().popMpls(EthType.of(popHeaderInstructions
.ethernetType().getEtherType()));
case MPLS_LABEL:
ModMplsLabelInstruction mplsLabel =
(ModMplsLabelInstruction) l2m;
oxm = factory().oxms().mplsLabel(U32.of(mplsLabel.label()
.longValue()));
break;
default:
log.warn("Unimplemented action type {}.", l2m.subtype());
break;
}
if (oxm != null) {
......
......@@ -37,6 +37,8 @@ public class Ethernet extends BasePacket {
public static final short TYPE_LLDP = (short) 0x88cc;
public static final short TYPE_BSN = (short) 0x8942;
public static final short VLAN_UNTAGGED = (short) 0xffff;
public static final short MPLS_UNICAST = (short) 0x8847;
public static final short MPLS_MULTICAST = (short) 0x8848;
public static final short DATALAYER_ADDRESS_LENGTH = 6; // bytes
public static Map<Short, Class<? extends IPacket>> etherTypeClassMap;
......