Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 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.openflow.controller; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 17 | |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 18 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 19 | import org.onlab.packet.Ethernet; |
| 20 | import org.projectfloodlight.openflow.protocol.OFPacketIn; |
| 21 | import org.projectfloodlight.openflow.protocol.OFPacketOut; |
| 22 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
| 23 | import org.projectfloodlight.openflow.protocol.action.OFActionOutput; |
| 24 | import org.projectfloodlight.openflow.protocol.match.MatchField; |
| 25 | import org.projectfloodlight.openflow.types.OFBufferId; |
| 26 | import org.projectfloodlight.openflow.types.OFPort; |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 27 | |
Jonathan Hart | e46c041 | 2015-01-20 13:19:55 -0800 | [diff] [blame^] | 28 | import java.nio.BufferUnderflowException; |
| 29 | import java.util.Collections; |
| 30 | import java.util.concurrent.atomic.AtomicBoolean; |
| 31 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 32 | public final class DefaultOpenFlowPacketContext implements OpenFlowPacketContext { |
| 33 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 34 | private final AtomicBoolean free = new AtomicBoolean(true); |
| 35 | private final AtomicBoolean isBuilt = new AtomicBoolean(false); |
| 36 | private final OpenFlowSwitch sw; |
| 37 | private final OFPacketIn pktin; |
| 38 | private OFPacketOut pktout = null; |
| 39 | |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame] | 40 | private final boolean isBuffered; |
| 41 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 42 | private DefaultOpenFlowPacketContext(OpenFlowSwitch s, OFPacketIn pkt) { |
| 43 | this.sw = s; |
| 44 | this.pktin = pkt; |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame] | 45 | this.isBuffered = pktin.getBufferId() != OFBufferId.NO_BUFFER; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public void send() { |
| 50 | if (block() && isBuilt.get()) { |
| 51 | sw.sendMsg(pktout); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void build(OFPort outPort) { |
| 57 | if (isBuilt.getAndSet(true)) { |
| 58 | return; |
| 59 | } |
| 60 | OFPacketOut.Builder builder = sw.factory().buildPacketOut(); |
| 61 | OFAction act = buildOutput(outPort.getPortNumber()); |
| 62 | pktout = builder.setXid(pktin.getXid()) |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 63 | .setInPort(inport()) |
Thomas Vachuska | 5f99849 | 2014-10-31 00:46:11 -0700 | [diff] [blame] | 64 | .setBufferId(OFBufferId.NO_BUFFER) |
| 65 | .setData(pktin.getData()) |
| 66 | // .setBufferId(pktin.getBufferId()) |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 67 | .setActions(Collections.singletonList(act)) |
| 68 | .build(); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public void build(Ethernet ethFrame, OFPort outPort) { |
| 73 | if (isBuilt.getAndSet(true)) { |
| 74 | return; |
| 75 | } |
| 76 | OFPacketOut.Builder builder = sw.factory().buildPacketOut(); |
| 77 | OFAction act = buildOutput(outPort.getPortNumber()); |
| 78 | pktout = builder.setXid(pktin.getXid()) |
| 79 | .setBufferId(OFBufferId.NO_BUFFER) |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 80 | .setInPort(inport()) |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 81 | .setActions(Collections.singletonList(act)) |
| 82 | .setData(ethFrame.serialize()) |
| 83 | .build(); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public Ethernet parsed() { |
| 88 | Ethernet eth = new Ethernet(); |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 89 | try { |
| 90 | eth.deserialize(pktin.getData(), 0, pktin.getData().length); |
| 91 | return eth; |
Jonathan Hart | e46c041 | 2015-01-20 13:19:55 -0800 | [diff] [blame^] | 92 | } catch (BufferUnderflowException | NullPointerException e) { |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 93 | return null; |
| 94 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public Dpid dpid() { |
| 99 | return new Dpid(sw.getId()); |
| 100 | } |
| 101 | |
| 102 | public static OpenFlowPacketContext packetContextFromPacketIn(OpenFlowSwitch s, |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 103 | OFPacketIn pkt) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 104 | return new DefaultOpenFlowPacketContext(s, pkt); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public Integer inPort() { |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 109 | return inport().getPortNumber(); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | private OFPort inport() { |
Jonathan Hart | e1588ed | 2014-12-03 17:26:51 -0800 | [diff] [blame] | 114 | //FIXME: this has to change in loxi |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 115 | try { |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 116 | return pktin.getInPort(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 117 | } catch (UnsupportedOperationException e) { |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 118 | return pktin.getMatch().get(MatchField.IN_PORT); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public byte[] unparsed() { |
| 124 | |
| 125 | return pktin.getData().clone(); |
| 126 | |
| 127 | } |
| 128 | |
| 129 | private OFActionOutput buildOutput(Integer port) { |
| 130 | OFActionOutput act = sw.factory().actions() |
| 131 | .buildOutput() |
| 132 | .setPort(OFPort.of(port)) |
| 133 | .build(); |
| 134 | return act; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public boolean block() { |
| 139 | return free.getAndSet(false); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public boolean isHandled() { |
| 144 | return !free.get(); |
| 145 | } |
| 146 | |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame] | 147 | @Override |
| 148 | public boolean isBuffered() { |
| 149 | return isBuffered; |
| 150 | } |
| 151 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 152 | } |