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