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