Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present 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 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 18 | import org.onlab.packet.DeserializationException; |
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; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 30 | |
Jonathan Hart | e46c041 | 2015-01-20 13:19:55 -0800 | [diff] [blame] | 31 | import java.nio.BufferUnderflowException; |
| 32 | import java.util.Collections; |
Charles Chan | cb13d12 | 2016-02-10 13:03:40 -0800 | [diff] [blame] | 33 | import java.util.Optional; |
Jonathan Hart | e46c041 | 2015-01-20 13:19:55 -0800 | [diff] [blame] | 34 | import java.util.concurrent.atomic.AtomicBoolean; |
| 35 | |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 36 | import static org.onosproject.security.AppGuard.checkPermission; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 37 | import static org.onosproject.security.AppPermission.Type.*; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 38 | |
| 39 | |
Jonathan Hart | cc36be8 | 2015-04-09 11:54:56 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Default implementation of an OpenFlowPacketContext. |
| 42 | */ |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 43 | public final class DefaultOpenFlowPacketContext implements OpenFlowPacketContext { |
| 44 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 45 | private final AtomicBoolean free = new AtomicBoolean(true); |
| 46 | private final AtomicBoolean isBuilt = new AtomicBoolean(false); |
| 47 | private final OpenFlowSwitch sw; |
| 48 | private final OFPacketIn pktin; |
| 49 | private OFPacketOut pktout = null; |
| 50 | |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame] | 51 | private final boolean isBuffered; |
| 52 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 53 | private DefaultOpenFlowPacketContext(OpenFlowSwitch s, OFPacketIn pkt) { |
| 54 | this.sw = s; |
| 55 | this.pktin = pkt; |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame] | 56 | this.isBuffered = pktin.getBufferId() != OFBufferId.NO_BUFFER; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public void send() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 61 | checkPermission(PACKET_WRITE); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 62 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 63 | if (block() && isBuilt.get()) { |
| 64 | sw.sendMsg(pktout); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void build(OFPort outPort) { |
| 70 | if (isBuilt.getAndSet(true)) { |
| 71 | return; |
| 72 | } |
| 73 | OFPacketOut.Builder builder = sw.factory().buildPacketOut(); |
| 74 | OFAction act = buildOutput(outPort.getPortNumber()); |
| 75 | pktout = builder.setXid(pktin.getXid()) |
Pavlin Radoslavov | 2031899 | 2015-02-04 13:41:14 -0800 | [diff] [blame] | 76 | .setInPort(pktinInPort()) |
Thomas Vachuska | 5f99849 | 2014-10-31 00:46:11 -0700 | [diff] [blame] | 77 | .setBufferId(OFBufferId.NO_BUFFER) |
| 78 | .setData(pktin.getData()) |
| 79 | // .setBufferId(pktin.getBufferId()) |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 80 | .setActions(Collections.singletonList(act)) |
| 81 | .build(); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void build(Ethernet ethFrame, OFPort outPort) { |
| 86 | if (isBuilt.getAndSet(true)) { |
| 87 | return; |
| 88 | } |
| 89 | OFPacketOut.Builder builder = sw.factory().buildPacketOut(); |
| 90 | OFAction act = buildOutput(outPort.getPortNumber()); |
| 91 | pktout = builder.setXid(pktin.getXid()) |
| 92 | .setBufferId(OFBufferId.NO_BUFFER) |
Pavlin Radoslavov | 2031899 | 2015-02-04 13:41:14 -0800 | [diff] [blame] | 93 | .setInPort(pktinInPort()) |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 94 | .setActions(Collections.singletonList(act)) |
| 95 | .setData(ethFrame.serialize()) |
| 96 | .build(); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public Ethernet parsed() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 101 | checkPermission(PACKET_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 102 | |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 103 | try { |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 104 | return Ethernet.deserializer().deserialize(pktin.getData(), 0, pktin.getData().length); |
| 105 | } catch (BufferUnderflowException | NullPointerException | |
| 106 | DeserializationException e) { |
| 107 | Logger log = LoggerFactory.getLogger(getClass()); |
alshabib | a55c1c6 | 2015-10-05 15:07:08 +0200 | [diff] [blame] | 108 | log.error("packet deserialization problem : {}", e.getMessage()); |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 109 | return null; |
| 110 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public Dpid dpid() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 115 | checkPermission(PACKET_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 116 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 117 | return new Dpid(sw.getId()); |
| 118 | } |
| 119 | |
Jonathan Hart | cc36be8 | 2015-04-09 11:54:56 -0700 | [diff] [blame] | 120 | /** |
| 121 | * Creates an OpenFlow packet context based on a packet-in. |
| 122 | * |
| 123 | * @param s OpenFlow switch |
| 124 | * @param pkt OpenFlow packet-in |
| 125 | * @return the OpenFlow packet context |
| 126 | */ |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 127 | public static OpenFlowPacketContext packetContextFromPacketIn(OpenFlowSwitch s, |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 128 | OFPacketIn pkt) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 129 | return new DefaultOpenFlowPacketContext(s, pkt); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public Integer inPort() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 134 | checkPermission(PACKET_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 135 | |
Pavlin Radoslavov | 2031899 | 2015-02-04 13:41:14 -0800 | [diff] [blame] | 136 | return pktinInPort().getPortNumber(); |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Pavlin Radoslavov | 2031899 | 2015-02-04 13:41:14 -0800 | [diff] [blame] | 139 | private OFPort pktinInPort() { |
Jonathan Hart | cc36be8 | 2015-04-09 11:54:56 -0700 | [diff] [blame] | 140 | if (pktin.getVersion() == OFVersion.OF_10) { |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 141 | return pktin.getInPort(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 142 | } |
Jonathan Hart | cc36be8 | 2015-04-09 11:54:56 -0700 | [diff] [blame] | 143 | return pktin.getMatch().get(MatchField.IN_PORT); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public byte[] unparsed() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 148 | checkPermission(PACKET_READ); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 149 | |
| 150 | return pktin.getData().clone(); |
| 151 | |
| 152 | } |
| 153 | |
| 154 | private OFActionOutput buildOutput(Integer port) { |
| 155 | OFActionOutput act = sw.factory().actions() |
| 156 | .buildOutput() |
| 157 | .setPort(OFPort.of(port)) |
| 158 | .build(); |
| 159 | return act; |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public boolean block() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 164 | checkPermission(PACKET_WRITE); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 165 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 166 | return free.getAndSet(false); |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public boolean isHandled() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 171 | checkPermission(PACKET_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 172 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 173 | return !free.get(); |
| 174 | } |
| 175 | |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame] | 176 | @Override |
| 177 | public boolean isBuffered() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 178 | checkPermission(PACKET_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 179 | |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame] | 180 | return isBuffered; |
| 181 | } |
| 182 | |
Charles Chan | cb13d12 | 2016-02-10 13:03:40 -0800 | [diff] [blame] | 183 | @Override |
| 184 | public Optional<Long> cookie() { |
| 185 | checkPermission(PACKET_READ); |
| 186 | if (pktin.getVersion() != OFVersion.OF_10) { |
| 187 | return Optional.of(pktin.getCookie().getValue()); |
| 188 | } else { |
| 189 | return Optional.empty(); |
| 190 | } |
| 191 | } |
| 192 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 193 | } |