Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.provider.of.packet.impl; |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 18 | import org.onlab.packet.DeserializationException; |
| 19 | import org.onlab.packet.Ethernet; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.net.PortNumber; |
| 21 | import org.onosproject.net.flow.instructions.Instruction; |
| 22 | import org.onosproject.net.flow.instructions.Instruction.Type; |
| 23 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
| 24 | import org.onosproject.net.packet.DefaultPacketContext; |
| 25 | import org.onosproject.net.packet.InboundPacket; |
| 26 | import org.onosproject.net.packet.OutboundPacket; |
| 27 | import org.onosproject.openflow.controller.OpenFlowPacketContext; |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 28 | import org.projectfloodlight.openflow.types.OFPort; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 31 | |
| 32 | import java.util.List; |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 33 | |
Jonathan Hart | cc36be8 | 2015-04-09 11:54:56 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Packet context used with the OpenFlow providers. |
| 36 | */ |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 37 | public class OpenFlowCorePacketContext extends DefaultPacketContext { |
| 38 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 39 | private static final Logger log = LoggerFactory.getLogger(OpenFlowCorePacketContext.class); |
| 40 | |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 41 | private final OpenFlowPacketContext ofPktCtx; |
| 42 | |
Jonathan Hart | cc36be8 | 2015-04-09 11:54:56 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Creates a new OpenFlow core packet context. |
| 45 | * |
| 46 | * @param time creation time |
| 47 | * @param inPkt inbound packet |
| 48 | * @param outPkt outbound packet |
| 49 | * @param block whether the context is blocked or not |
| 50 | * @param ofPktCtx OpenFlow packet context |
| 51 | */ |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 52 | protected OpenFlowCorePacketContext(long time, InboundPacket inPkt, |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 53 | OutboundPacket outPkt, boolean block, |
| 54 | OpenFlowPacketContext ofPktCtx) { |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 55 | super(time, inPkt, outPkt, block); |
| 56 | this.ofPktCtx = ofPktCtx; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public void send() { |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 61 | if (!this.block()) { |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 62 | if (outPacket() == null) { |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame] | 63 | sendPacket(null); |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 64 | } else { |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 65 | try { |
| 66 | Ethernet eth = Ethernet.deserializer() |
| 67 | .deserialize(outPacket().data().array(), 0, |
| 68 | outPacket().data().array().length); |
| 69 | sendPacket(eth); |
| 70 | } catch (DeserializationException e) { |
| 71 | log.warn("Unable to deserialize packet"); |
| 72 | } |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 73 | } |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame] | 77 | private void sendPacket(Ethernet eth) { |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 78 | List<Instruction> ins = treatmentBuilder().build().allInstructions(); |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 79 | OFPort p = null; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 80 | //TODO: support arbitrary list of treatments must be supported in ofPacketContext |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 81 | for (Instruction i : ins) { |
| 82 | if (i.type() == Type.OUTPUT) { |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 83 | p = buildPort(((OutputInstruction) i).port()); |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 84 | break; //for now... |
| 85 | } |
| 86 | } |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame] | 87 | if (eth == null) { |
| 88 | ofPktCtx.build(p); |
| 89 | } else { |
| 90 | ofPktCtx.build(eth, p); |
| 91 | } |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 92 | ofPktCtx.send(); |
| 93 | } |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 94 | |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 95 | private OFPort buildPort(PortNumber port) { |
| 96 | return OFPort.of((int) port.toLong()); |
| 97 | } |
| 98 | |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 99 | } |