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