diff options
author | 2015-09-09 22:15:21 -0700 | |
---|---|---|
committer | 2015-09-09 22:15:21 -0700 | |
commit | 13d05bc8458758ee39cb829098241e89616717ee (patch) | |
tree | 22a4d1ce65f15952f07a3df5af4b462b4697cb3a /framework/src/onos/providers/openflow/packet/src/main/java/org | |
parent | 6139282e1e93c2322076de4b91b1c85d0bc4a8b3 (diff) |
ONOS checkin based on commit tag e796610b1f721d02f9b0e213cf6f7790c10ecd60
Change-Id: Ife8810491034fe7becdba75dda20de4267bd15cd
Diffstat (limited to 'framework/src/onos/providers/openflow/packet/src/main/java/org')
3 files changed, 296 insertions, 0 deletions
diff --git a/framework/src/onos/providers/openflow/packet/src/main/java/org/onosproject/provider/of/packet/impl/OpenFlowCorePacketContext.java b/framework/src/onos/providers/openflow/packet/src/main/java/org/onosproject/provider/of/packet/impl/OpenFlowCorePacketContext.java new file mode 100644 index 00000000..6d153103 --- /dev/null +++ b/framework/src/onos/providers/openflow/packet/src/main/java/org/onosproject/provider/of/packet/impl/OpenFlowCorePacketContext.java @@ -0,0 +1,99 @@ +/* + * Copyright 2014-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.packet.impl; + +import org.onlab.packet.DeserializationException; +import org.onlab.packet.Ethernet; +import org.onosproject.net.PortNumber; +import org.onosproject.net.flow.instructions.Instruction; +import org.onosproject.net.flow.instructions.Instruction.Type; +import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; +import org.onosproject.net.packet.DefaultPacketContext; +import org.onosproject.net.packet.InboundPacket; +import org.onosproject.net.packet.OutboundPacket; +import org.onosproject.openflow.controller.OpenFlowPacketContext; +import org.projectfloodlight.openflow.types.OFPort; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +/** + * Packet context used with the OpenFlow providers. + */ +public class OpenFlowCorePacketContext extends DefaultPacketContext { + + private static final Logger log = LoggerFactory.getLogger(OpenFlowCorePacketContext.class); + + private final OpenFlowPacketContext ofPktCtx; + + /** + * Creates a new OpenFlow core packet context. + * + * @param time creation time + * @param inPkt inbound packet + * @param outPkt outbound packet + * @param block whether the context is blocked or not + * @param ofPktCtx OpenFlow packet context + */ + protected OpenFlowCorePacketContext(long time, InboundPacket inPkt, + OutboundPacket outPkt, boolean block, + OpenFlowPacketContext ofPktCtx) { + super(time, inPkt, outPkt, block); + this.ofPktCtx = ofPktCtx; + } + + @Override + public void send() { + if (!this.block()) { + if (outPacket() == null) { + sendPacket(null); + } else { + try { + Ethernet eth = Ethernet.deserializer() + .deserialize(outPacket().data().array(), 0, + outPacket().data().array().length); + sendPacket(eth); + } catch (DeserializationException e) { + log.warn("Unable to deserialize packet"); + } + } + } + } + + private void sendPacket(Ethernet eth) { + List<Instruction> ins = treatmentBuilder().build().allInstructions(); + OFPort p = null; + //TODO: support arbitrary list of treatments must be supported in ofPacketContext + for (Instruction i : ins) { + if (i.type() == Type.OUTPUT) { + p = buildPort(((OutputInstruction) i).port()); + break; //for now... + } + } + if (eth == null) { + ofPktCtx.build(p); + } else { + ofPktCtx.build(eth, p); + } + ofPktCtx.send(); + } + + private OFPort buildPort(PortNumber port) { + return OFPort.of((int) port.toLong()); + } + +} diff --git a/framework/src/onos/providers/openflow/packet/src/main/java/org/onosproject/provider/of/packet/impl/OpenFlowPacketProvider.java b/framework/src/onos/providers/openflow/packet/src/main/java/org/onosproject/provider/of/packet/impl/OpenFlowPacketProvider.java new file mode 100644 index 00000000..dc79feff --- /dev/null +++ b/framework/src/onos/providers/openflow/packet/src/main/java/org/onosproject/provider/of/packet/impl/OpenFlowPacketProvider.java @@ -0,0 +1,176 @@ +/* + * Copyright 2014-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.packet.impl; + +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.ConnectPoint; +import org.onosproject.net.DeviceId; +import org.onosproject.net.PortNumber; +import org.onosproject.net.flow.instructions.Instruction; +import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; +import org.onosproject.net.packet.DefaultInboundPacket; +import org.onosproject.net.packet.DefaultOutboundPacket; +import org.onosproject.net.packet.OutboundPacket; +import org.onosproject.net.packet.PacketProvider; +import org.onosproject.net.packet.PacketProviderRegistry; +import org.onosproject.net.packet.PacketProviderService; +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.OpenFlowPacketContext; +import org.onosproject.openflow.controller.OpenFlowSwitch; +import org.onosproject.openflow.controller.PacketListener; +import org.projectfloodlight.openflow.protocol.OFPacketOut; +import org.projectfloodlight.openflow.protocol.OFPortDesc; +import org.projectfloodlight.openflow.protocol.action.OFAction; +import org.projectfloodlight.openflow.protocol.ver10.OFFactoryVer10; +import org.projectfloodlight.openflow.types.OFBufferId; +import org.projectfloodlight.openflow.types.OFPort; +import org.slf4j.Logger; + +import java.nio.ByteBuffer; +import java.util.Collections; + +import static org.slf4j.LoggerFactory.getLogger; + + +/** + * Provider which uses an OpenFlow controller to detect network + * infrastructure links. + */ +@Component(immediate = true) +public class OpenFlowPacketProvider extends AbstractProvider implements PacketProvider { + + private final Logger log = getLogger(getClass()); + + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) + protected PacketProviderRegistry providerRegistry; + + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) + protected OpenFlowController controller; + + private PacketProviderService providerService; + + private final InternalPacketProvider listener = new InternalPacketProvider(); + + /** + * Creates an OpenFlow link provider. + */ + public OpenFlowPacketProvider() { + super(new ProviderId("of", "org.onosproject.provider.openflow")); + } + + @Activate + public void activate() { + providerService = providerRegistry.register(this); + controller.addPacketListener(20, listener); + log.info("Started"); + } + + @Deactivate + public void deactivate() { + providerRegistry.unregister(this); + controller.removePacketListener(listener); + providerService = null; + log.info("Stopped"); + } + + @Override + public void emit(OutboundPacket packet) { + DeviceId devId = packet.sendThrough(); + String scheme = devId.toString().split(":")[0]; + + if (!scheme.equals(this.id().scheme())) { + throw new IllegalArgumentException( + "Don't know how to handle Device with scheme " + scheme); + } + + Dpid dpid = Dpid.dpid(devId.uri()); + OpenFlowSwitch sw = controller.getSwitch(dpid); + if (sw == null) { + log.warn("Device {} isn't available?", devId); + return; + } + + //Ethernet eth = new Ethernet(); + //eth.deserialize(packet.data().array(), 0, packet.data().array().length); + OFPortDesc p = null; + for (Instruction inst : packet.treatment().allInstructions()) { + if (inst.type().equals(Instruction.Type.OUTPUT)) { + p = portDesc(((OutputInstruction) inst).port()); + OFPacketOut po = packetOut(sw, packet.data().array(), p.getPortNo()); + sw.sendMsg(po); + } + } + + } + + private OFPortDesc portDesc(PortNumber port) { + OFPortDesc.Builder builder = OFFactoryVer10.INSTANCE.buildPortDesc(); + builder.setPortNo(OFPort.of((int) port.toLong())); + + return builder.build(); + } + + private OFPacketOut packetOut(OpenFlowSwitch sw, byte[] eth, OFPort out) { + OFPacketOut.Builder builder = sw.factory().buildPacketOut(); + OFAction act = sw.factory().actions() + .buildOutput() + .setPort(out) + .build(); + return builder + .setBufferId(OFBufferId.NO_BUFFER) + .setInPort(OFPort.CONTROLLER) + .setActions(Collections.singletonList(act)) + .setData(eth) + .build(); + } + + /** + * Internal Packet Provider implementation. + * + */ + private class InternalPacketProvider implements PacketListener { + + @Override + public void handlePacket(OpenFlowPacketContext pktCtx) { + DeviceId id = DeviceId.deviceId(Dpid.uri(pktCtx.dpid().value())); + + DefaultInboundPacket inPkt = new DefaultInboundPacket( + new ConnectPoint(id, PortNumber.portNumber(pktCtx.inPort())), + pktCtx.parsed(), ByteBuffer.wrap(pktCtx.unparsed())); + + DefaultOutboundPacket outPkt = null; + if (!pktCtx.isBuffered()) { + outPkt = new DefaultOutboundPacket(id, null, + ByteBuffer.wrap(pktCtx.unparsed())); + } + + OpenFlowCorePacketContext corePktCtx = + new OpenFlowCorePacketContext(System.currentTimeMillis(), + inPkt, outPkt, pktCtx.isHandled(), pktCtx); + providerService.processPacket(corePktCtx); + } + + } + + +} diff --git a/framework/src/onos/providers/openflow/packet/src/main/java/org/onosproject/provider/of/packet/impl/package-info.java b/framework/src/onos/providers/openflow/packet/src/main/java/org/onosproject/provider/of/packet/impl/package-info.java new file mode 100644 index 00000000..dd1130c0 --- /dev/null +++ b/framework/src/onos/providers/openflow/packet/src/main/java/org/onosproject/provider/of/packet/impl/package-info.java @@ -0,0 +1,21 @@ +/* + * 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. + */ + +/** + * Provider that uses OpenFlow controller as a means of intercepting and + * emitting packets. + */ +package org.onosproject.provider.of.packet.impl; |