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