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