Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
alshabib | ae85758 | 2014-09-12 23:53:10 -0700 | [diff] [blame] | 19 | package org.onlab.onos.provider.of.packet.impl; |
| 20 | |
| 21 | import static org.slf4j.LoggerFactory.getLogger; |
| 22 | |
alshabib | d58d352 | 2014-09-13 17:14:53 -0700 | [diff] [blame] | 23 | import java.nio.ByteBuffer; |
Ayaka Koshibe | ab3374b | 2014-09-19 11:41:25 -0700 | [diff] [blame] | 24 | import java.util.Collections; |
alshabib | d58d352 | 2014-09-13 17:14:53 -0700 | [diff] [blame] | 25 | |
alshabib | ae85758 | 2014-09-12 23:53:10 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Activate; |
| 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Deactivate; |
| 29 | import org.apache.felix.scr.annotations.Reference; |
| 30 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
alshabib | d58d352 | 2014-09-13 17:14:53 -0700 | [diff] [blame] | 31 | import org.onlab.onos.net.ConnectPoint; |
| 32 | import org.onlab.onos.net.DeviceId; |
| 33 | import org.onlab.onos.net.PortNumber; |
Ayaka Koshibe | ab3374b | 2014-09-19 11:41:25 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.flow.instructions.Instruction; |
| 35 | import org.onlab.onos.net.flow.instructions.Instructions.OutputInstruction; |
alshabib | d58d352 | 2014-09-13 17:14:53 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.packet.DefaultInboundPacket; |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame] | 37 | import org.onlab.onos.net.packet.DefaultOutboundPacket; |
alshabib | ae85758 | 2014-09-12 23:53:10 -0700 | [diff] [blame] | 38 | import org.onlab.onos.net.packet.OutboundPacket; |
| 39 | import org.onlab.onos.net.packet.PacketProvider; |
| 40 | import org.onlab.onos.net.packet.PacketProviderRegistry; |
| 41 | import org.onlab.onos.net.packet.PacketProviderService; |
| 42 | import org.onlab.onos.net.provider.AbstractProvider; |
| 43 | import org.onlab.onos.net.provider.ProviderId; |
tom | 9c94c5b | 2014-09-17 13:14:42 -0700 | [diff] [blame] | 44 | import org.onlab.onos.openflow.controller.Dpid; |
| 45 | import org.onlab.onos.openflow.controller.OpenFlowController; |
| 46 | import org.onlab.onos.openflow.controller.OpenFlowPacketContext; |
Ayaka Koshibe | ab3374b | 2014-09-19 11:41:25 -0700 | [diff] [blame] | 47 | import org.onlab.onos.openflow.controller.OpenFlowSwitch; |
tom | 9c94c5b | 2014-09-17 13:14:42 -0700 | [diff] [blame] | 48 | import org.onlab.onos.openflow.controller.PacketListener; |
Ayaka Koshibe | ab3374b | 2014-09-19 11:41:25 -0700 | [diff] [blame] | 49 | import org.projectfloodlight.openflow.protocol.OFPacketOut; |
| 50 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
| 51 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
| 52 | import org.projectfloodlight.openflow.protocol.ver10.OFFactoryVer10; |
| 53 | import org.projectfloodlight.openflow.types.OFBufferId; |
| 54 | import org.projectfloodlight.openflow.types.OFPort; |
alshabib | ae85758 | 2014-09-12 23:53:10 -0700 | [diff] [blame] | 55 | import org.slf4j.Logger; |
| 56 | |
Ayaka Koshibe | ab3374b | 2014-09-19 11:41:25 -0700 | [diff] [blame] | 57 | |
alshabib | ae85758 | 2014-09-12 23:53:10 -0700 | [diff] [blame] | 58 | /** |
| 59 | * Provider which uses an OpenFlow controller to detect network |
| 60 | * infrastructure links. |
| 61 | */ |
| 62 | @Component(immediate = true) |
| 63 | public class OpenFlowPacketProvider extends AbstractProvider implements PacketProvider { |
| 64 | |
| 65 | private final Logger log = getLogger(getClass()); |
| 66 | |
| 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 68 | protected PacketProviderRegistry providerRegistry; |
| 69 | |
| 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 71 | protected OpenFlowController controller; |
| 72 | |
| 73 | private PacketProviderService providerService; |
| 74 | |
alshabib | ae85758 | 2014-09-12 23:53:10 -0700 | [diff] [blame] | 75 | private final InternalPacketProvider listener = new InternalPacketProvider(); |
| 76 | |
alshabib | ae85758 | 2014-09-12 23:53:10 -0700 | [diff] [blame] | 77 | /** |
| 78 | * Creates an OpenFlow link provider. |
| 79 | */ |
| 80 | public OpenFlowPacketProvider() { |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame] | 81 | super(new ProviderId("of", "org.onlab.onos.provider.openflow")); |
alshabib | ae85758 | 2014-09-12 23:53:10 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Activate |
| 85 | public void activate() { |
| 86 | providerService = providerRegistry.register(this); |
alshabib | 8aef1ad | 2014-09-15 17:47:31 -0700 | [diff] [blame] | 87 | controller.addPacketListener(20, listener); |
alshabib | ae85758 | 2014-09-12 23:53:10 -0700 | [diff] [blame] | 88 | log.info("Started"); |
| 89 | } |
| 90 | |
| 91 | @Deactivate |
| 92 | public void deactivate() { |
| 93 | providerRegistry.unregister(this); |
| 94 | controller.removePacketListener(listener); |
| 95 | providerService = null; |
| 96 | log.info("Stopped"); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void emit(OutboundPacket packet) { |
Ayaka Koshibe | ab3374b | 2014-09-19 11:41:25 -0700 | [diff] [blame] | 101 | DeviceId devId = packet.sendThrough(); |
| 102 | String scheme = devId.toString().split(":")[0]; |
| 103 | |
| 104 | if (!scheme.equals(this.id().scheme())) { |
| 105 | throw new IllegalArgumentException( |
| 106 | "Don't know how to handle Device with scheme " + scheme); |
| 107 | } |
| 108 | |
| 109 | Dpid dpid = Dpid.dpid(devId.uri()); |
| 110 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 111 | if (sw == null) { |
| 112 | log.warn("Device {} isn't available?", devId); |
| 113 | return; |
Ayaka Koshibe | ab3374b | 2014-09-19 11:41:25 -0700 | [diff] [blame] | 114 | } |
| 115 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 116 | //Ethernet eth = new Ethernet(); |
| 117 | //eth.deserialize(packet.data().array(), 0, packet.data().array().length); |
Ayaka Koshibe | ab3374b | 2014-09-19 11:41:25 -0700 | [diff] [blame] | 118 | OFPortDesc p = null; |
| 119 | for (Instruction inst : packet.treatment().instructions()) { |
| 120 | if (inst.type().equals(Instruction.Type.OUTPUT)) { |
| 121 | p = portDesc(((OutputInstruction) inst).port()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 122 | OFPacketOut po = packetOut(sw, packet.data().array(), p.getPortNo()); |
Ayaka Koshibe | ab3374b | 2014-09-19 11:41:25 -0700 | [diff] [blame] | 123 | sw.sendMsg(po); |
| 124 | } |
| 125 | } |
alshabib | ae85758 | 2014-09-12 23:53:10 -0700 | [diff] [blame] | 126 | |
| 127 | } |
| 128 | |
Ayaka Koshibe | ab3374b | 2014-09-19 11:41:25 -0700 | [diff] [blame] | 129 | private OFPortDesc portDesc(PortNumber port) { |
| 130 | OFPortDesc.Builder builder = OFFactoryVer10.INSTANCE.buildPortDesc(); |
| 131 | builder.setPortNo(OFPort.of((int) port.toLong())); |
| 132 | |
| 133 | return builder.build(); |
| 134 | } |
| 135 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 136 | private OFPacketOut packetOut(OpenFlowSwitch sw, byte[] eth, OFPort out) { |
Ayaka Koshibe | ab3374b | 2014-09-19 11:41:25 -0700 | [diff] [blame] | 137 | OFPacketOut.Builder builder = sw.factory().buildPacketOut(); |
| 138 | OFAction act = sw.factory().actions() |
| 139 | .buildOutput() |
| 140 | .setPort(out) |
| 141 | .build(); |
| 142 | return builder |
| 143 | .setBufferId(OFBufferId.NO_BUFFER) |
| 144 | .setInPort(OFPort.NO_MASK) |
| 145 | .setActions(Collections.singletonList(act)) |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 146 | .setData(eth) |
Ayaka Koshibe | ab3374b | 2014-09-19 11:41:25 -0700 | [diff] [blame] | 147 | .build(); |
| 148 | } |
alshabib | ae85758 | 2014-09-12 23:53:10 -0700 | [diff] [blame] | 149 | |
alshabib | d58d352 | 2014-09-13 17:14:53 -0700 | [diff] [blame] | 150 | /** |
| 151 | * Internal Packet Provider implementation. |
| 152 | * |
| 153 | */ |
alshabib | ae85758 | 2014-09-12 23:53:10 -0700 | [diff] [blame] | 154 | private class InternalPacketProvider implements PacketListener { |
| 155 | |
alshabib | ae85758 | 2014-09-12 23:53:10 -0700 | [diff] [blame] | 156 | @Override |
| 157 | public void handlePacket(OpenFlowPacketContext pktCtx) { |
alshabib | d58d352 | 2014-09-13 17:14:53 -0700 | [diff] [blame] | 158 | DeviceId id = DeviceId.deviceId(Dpid.uri(pktCtx.dpid().value())); |
alshabib | ae85758 | 2014-09-12 23:53:10 -0700 | [diff] [blame] | 159 | |
alshabib | d58d352 | 2014-09-13 17:14:53 -0700 | [diff] [blame] | 160 | DefaultInboundPacket inPkt = new DefaultInboundPacket( |
| 161 | new ConnectPoint(id, PortNumber.portNumber(pktCtx.inPort())), |
| 162 | pktCtx.parsed(), ByteBuffer.wrap(pktCtx.unparsed())); |
alshabib | ae85758 | 2014-09-12 23:53:10 -0700 | [diff] [blame] | 163 | |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame] | 164 | DefaultOutboundPacket outPkt = null; |
| 165 | if (!pktCtx.isBuffered()) { |
| 166 | outPkt = new DefaultOutboundPacket(id, null, |
| 167 | ByteBuffer.wrap(pktCtx.unparsed())); |
| 168 | } |
| 169 | |
alshabib | d58d352 | 2014-09-13 17:14:53 -0700 | [diff] [blame] | 170 | OpenFlowCorePacketContext corePktCtx = |
alshabib | 9842a4e | 2014-09-16 21:42:35 -0700 | [diff] [blame] | 171 | new OpenFlowCorePacketContext(System.currentTimeMillis(), |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame] | 172 | inPkt, outPkt, pktCtx.isHandled(), pktCtx); |
alshabib | d58d352 | 2014-09-13 17:14:53 -0700 | [diff] [blame] | 173 | providerService.processPacket(corePktCtx); |
alshabib | ae85758 | 2014-09-12 23:53:10 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | } |
| 177 | |
| 178 | |
| 179 | } |