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