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 | */ |
| 16 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 17 | package org.onosproject.openflow.controller.impl; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 18 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 19 | import static org.onlab.util.Tools.groupedThreads; |
| 20 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 21 | import java.io.IOException; |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 22 | import java.net.InetSocketAddress; |
| 23 | import java.net.SocketAddress; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 24 | import java.nio.channels.ClosedChannelException; |
Brian O'Connor | 47a1609 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 25 | import java.security.cert.Certificate; |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 26 | import java.util.ArrayDeque; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 27 | import java.util.ArrayList; |
| 28 | import java.util.Collections; |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 29 | import java.util.Deque; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 30 | import java.util.List; |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 31 | import java.util.Optional; |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 32 | import java.util.concurrent.BlockingQueue; |
| 33 | import java.util.concurrent.CompletableFuture; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 34 | import java.util.concurrent.CopyOnWriteArrayList; |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 35 | import java.util.concurrent.ExecutorService; |
| 36 | import java.util.concurrent.Executors; |
| 37 | import java.util.concurrent.Future; |
| 38 | import java.util.concurrent.LinkedBlockingQueue; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 39 | import java.util.concurrent.RejectedExecutionException; |
Brian O'Connor | 47a1609 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 40 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 41 | import org.onlab.packet.IpAddress; |
Charles Chan | 34155e5 | 2016-11-30 18:28:11 -0800 | [diff] [blame] | 42 | import org.onosproject.openflow.controller.Dpid; |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 43 | import org.onosproject.openflow.controller.OpenFlowSession; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 44 | import org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver; |
| 45 | import org.onosproject.openflow.controller.driver.SwitchStateException; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 46 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
| 47 | import org.projectfloodlight.openflow.protocol.OFAsyncGetReply; |
| 48 | import org.projectfloodlight.openflow.protocol.OFBadRequestCode; |
| 49 | import org.projectfloodlight.openflow.protocol.OFBarrierReply; |
| 50 | import org.projectfloodlight.openflow.protocol.OFBarrierRequest; |
| 51 | import org.projectfloodlight.openflow.protocol.OFDescStatsReply; |
| 52 | import org.projectfloodlight.openflow.protocol.OFDescStatsRequest; |
| 53 | import org.projectfloodlight.openflow.protocol.OFEchoReply; |
| 54 | import org.projectfloodlight.openflow.protocol.OFEchoRequest; |
| 55 | import org.projectfloodlight.openflow.protocol.OFErrorMsg; |
| 56 | import org.projectfloodlight.openflow.protocol.OFErrorType; |
| 57 | import org.projectfloodlight.openflow.protocol.OFExperimenter; |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 58 | import org.projectfloodlight.openflow.protocol.OFFactories; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 59 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 60 | import org.projectfloodlight.openflow.protocol.OFFeaturesReply; |
| 61 | import org.projectfloodlight.openflow.protocol.OFFlowModFailedCode; |
| 62 | import org.projectfloodlight.openflow.protocol.OFFlowRemoved; |
| 63 | import org.projectfloodlight.openflow.protocol.OFGetConfigReply; |
| 64 | import org.projectfloodlight.openflow.protocol.OFGetConfigRequest; |
| 65 | import org.projectfloodlight.openflow.protocol.OFHello; |
| 66 | import org.projectfloodlight.openflow.protocol.OFHelloElem; |
| 67 | import org.projectfloodlight.openflow.protocol.OFMessage; |
Jordi Ortiz | 91477b8 | 2016-11-29 15:22:50 +0100 | [diff] [blame] | 68 | import org.projectfloodlight.openflow.protocol.OFMeterFeaturesStatsReply; |
| 69 | import org.projectfloodlight.openflow.protocol.OFMeterFeaturesStatsRequest; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 70 | import org.projectfloodlight.openflow.protocol.OFPacketIn; |
| 71 | import org.projectfloodlight.openflow.protocol.OFPortDescStatsReply; |
| 72 | import org.projectfloodlight.openflow.protocol.OFPortDescStatsRequest; |
| 73 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
| 74 | import org.projectfloodlight.openflow.protocol.OFQueueGetConfigReply; |
| 75 | import org.projectfloodlight.openflow.protocol.OFRoleReply; |
| 76 | import org.projectfloodlight.openflow.protocol.OFSetConfig; |
| 77 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
| 78 | import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags; |
| 79 | import org.projectfloodlight.openflow.protocol.OFStatsType; |
| 80 | import org.projectfloodlight.openflow.protocol.OFType; |
| 81 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 82 | import org.projectfloodlight.openflow.protocol.errormsg.OFBadRequestErrorMsg; |
| 83 | import org.projectfloodlight.openflow.protocol.errormsg.OFFlowModFailedErrorMsg; |
| 84 | import org.projectfloodlight.openflow.types.U32; |
| 85 | import org.slf4j.Logger; |
| 86 | import org.slf4j.LoggerFactory; |
| 87 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 88 | import io.netty.channel.Channel; |
| 89 | import io.netty.channel.ChannelHandlerContext; |
| 90 | import io.netty.channel.ChannelInboundHandlerAdapter; |
Brian O'Connor | 47a1609 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 91 | import io.netty.handler.ssl.SslHandler; |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 92 | import io.netty.handler.timeout.IdleStateEvent; |
| 93 | import io.netty.handler.timeout.ReadTimeoutException; |
| 94 | import io.netty.util.ReferenceCountUtil; |
| 95 | |
Brian O'Connor | 47a1609 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 96 | import javax.net.ssl.SSLPeerUnverifiedException; |
| 97 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 98 | /** |
| 99 | * Channel handler deals with the switch connection and dispatches |
| 100 | * switch messages to the appropriate locations. |
| 101 | */ |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 102 | class OFChannelHandler extends ChannelInboundHandlerAdapter |
| 103 | implements OpenFlowSession { |
| 104 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 105 | private static final Logger log = LoggerFactory.getLogger(OFChannelHandler.class); |
Thomas Vachuska | e9af1f4 | 2015-07-06 08:42:18 -0700 | [diff] [blame] | 106 | |
| 107 | private static final String RESET_BY_PEER = "Connection reset by peer"; |
| 108 | private static final String BROKEN_PIPE = "Broken pipe"; |
| 109 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 110 | private final Controller controller; |
| 111 | private OpenFlowSwitchDriver sw; |
| 112 | private long thisdpid; // channelHandler cached value of connected switch id |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 113 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 114 | private Channel channel; |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 115 | private String channelId; |
| 116 | |
| 117 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 118 | // State needs to be volatile because the HandshakeTimeoutHandler |
| 119 | // needs to check if the handshake is complete |
| 120 | private volatile ChannelState state; |
| 121 | |
Yuta HIGUCHI | 10f4513 | 2017-03-01 17:09:32 -0800 | [diff] [blame] | 122 | /** |
| 123 | * Timeout in ms to wait for meter feature reply. |
| 124 | */ |
| 125 | private static final long METER_TIMEOUT = 60_000; |
| 126 | |
| 127 | private volatile long lastStateChange = System.currentTimeMillis(); |
| 128 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 129 | // When a switch with a duplicate dpid is found (i.e we already have a |
| 130 | // connected switch with the same dpid), the new switch is immediately |
| 131 | // disconnected. At that point netty callsback channelDisconnected() which |
| 132 | // proceeds to cleaup switch state - we need to ensure that it does not cleanup |
| 133 | // switch state for the older (still connected) switch |
| 134 | private volatile Boolean duplicateDpidFound; |
| 135 | |
| 136 | // Temporary storage for switch-features and port-description |
| 137 | private OFFeaturesReply featuresReply; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 138 | private List<OFPortDescStatsReply> portDescReplies; |
Jordi Ortiz | 91477b8 | 2016-11-29 15:22:50 +0100 | [diff] [blame] | 139 | private OFMeterFeaturesStatsReply meterFeaturesReply; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 140 | //private OFPortDescStatsReply portDescReply; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 141 | // a concurrent ArrayList to temporarily store port status messages |
| 142 | // before we are ready to deal with them |
| 143 | private final CopyOnWriteArrayList<OFPortStatus> pendingPortStatusMsg; |
| 144 | |
| 145 | //Indicates the openflow version used by this switch |
| 146 | protected OFVersion ofVersion; |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 147 | protected OFFactory factory; |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 148 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 149 | /** transaction Ids to use during handshake. Since only one thread |
| 150 | * calls into an OFChannelHandler instance, we don't need atomic. |
| 151 | * We will count down |
| 152 | */ |
| 153 | private int handshakeTransactionIds = -1; |
| 154 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 155 | |
| 156 | |
| 157 | private static final int MSG_READ_BUFFER = 5000; |
| 158 | |
| 159 | /** |
| 160 | * OFMessage dispatch queue. |
| 161 | */ |
| 162 | private final BlockingQueue<OFMessage> dispatchQueue = |
| 163 | new LinkedBlockingQueue<>(MSG_READ_BUFFER); |
| 164 | |
| 165 | /** |
| 166 | * Single thread executor for OFMessage dispatching. |
| 167 | * |
| 168 | * Gets initialized on channelActive, shutdown on channelInactive. |
| 169 | */ |
| 170 | private ExecutorService dispatcher; |
| 171 | |
| 172 | /** |
| 173 | * Handle for dispatcher thread. |
| 174 | * <p> |
| 175 | * Should only be touched from the Channel I/O thread |
| 176 | */ |
| 177 | private Future<?> dispatcherHandle = CompletableFuture.completedFuture(null); |
| 178 | |
| 179 | /** |
| 180 | * Dispatch backlog. |
| 181 | * <p> |
| 182 | * Should only be touched from the Channel I/O thread |
| 183 | */ |
| 184 | private final Deque<OFMessage> dispatchBacklog = new ArrayDeque<>(); |
| 185 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 186 | /** |
| 187 | * Create a new unconnected OFChannelHandler. |
Thomas Vachuska | b14c77a | 2014-11-04 18:08:01 -0800 | [diff] [blame] | 188 | * @param controller parent controller |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 189 | */ |
| 190 | OFChannelHandler(Controller controller) { |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 191 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 192 | this.controller = controller; |
| 193 | this.state = ChannelState.INIT; |
Yuta HIGUCHI | 605758e | 2017-01-13 20:26:44 -0800 | [diff] [blame] | 194 | this.pendingPortStatusMsg = new CopyOnWriteArrayList<>(); |
| 195 | this.portDescReplies = new ArrayList<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 196 | duplicateDpidFound = Boolean.FALSE; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | |
| 201 | // XXX S consider if necessary |
| 202 | public void disconnectSwitch() { |
| 203 | sw.disconnectSwitch(); |
| 204 | } |
| 205 | |
| 206 | |
| 207 | |
| 208 | //************************* |
| 209 | // Channel State Machine |
| 210 | //************************* |
| 211 | |
| 212 | /** |
| 213 | * The state machine for handling the switch/channel state. All state |
| 214 | * transitions should happen from within the state machine (and not from other |
| 215 | * parts of the code) |
| 216 | */ |
| 217 | enum ChannelState { |
| 218 | /** |
| 219 | * Initial state before channel is connected. |
| 220 | */ |
| 221 | INIT(false) { |
| 222 | @Override |
| 223 | void processOFMessage(OFChannelHandler h, OFMessage m) |
| 224 | throws IOException, SwitchStateException { |
| 225 | illegalMessageReceived(h, m); |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | void processOFError(OFChannelHandler h, OFErrorMsg m) |
| 230 | throws IOException { |
| 231 | // need to implement since its abstract but it will never |
| 232 | // be called |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | void processOFPortStatus(OFChannelHandler h, OFPortStatus m) |
| 237 | throws IOException { |
| 238 | unhandledMessageReceived(h, m); |
| 239 | } |
| 240 | }, |
| 241 | |
| 242 | /** |
| 243 | * We send a OF 1.3 HELLO to the switch and wait for a Hello from the switch. |
| 244 | * Once we receive the reply, we decide on OF 1.3 or 1.0 switch - no other |
| 245 | * protocol version is accepted. |
| 246 | * We send an OFFeaturesRequest depending on the protocol version selected |
| 247 | * Next state is WAIT_FEATURES_REPLY |
| 248 | */ |
| 249 | WAIT_HELLO(false) { |
| 250 | @Override |
| 251 | void processOFHello(OFChannelHandler h, OFHello m) |
| 252 | throws IOException { |
| 253 | // TODO We could check for the optional bitmap, but for now |
| 254 | // we are just checking the version number. |
Chip Boling | 68bc656 | 2015-07-06 10:00:01 -0500 | [diff] [blame] | 255 | if (m.getVersion().getWireVersion() >= OFVersion.OF_13.getWireVersion()) { |
| 256 | log.debug("Received {} Hello from {} - switching to OF " |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 257 | + "version 1.3+", m.getVersion(), |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 258 | h.channel.remoteAddress()); |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 259 | h.ofVersion = m.getVersion(); |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 260 | h.factory = OFFactories.getFactory(h.ofVersion); |
alshabib | 70fc7fb | 2015-01-06 11:04:29 -0800 | [diff] [blame] | 261 | h.sendHandshakeHelloMessage(); |
Chip Boling | 68bc656 | 2015-07-06 10:00:01 -0500 | [diff] [blame] | 262 | } else if (m.getVersion().getWireVersion() >= OFVersion.OF_10.getWireVersion()) { |
alshabib | 09d48be | 2014-10-03 15:43:33 -0700 | [diff] [blame] | 263 | log.debug("Received {} Hello from {} - switching to OF " |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 264 | + "version 1.0", m.getVersion(), |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 265 | h.channel.remoteAddress()); |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 266 | h.ofVersion = m.getVersion(); |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 267 | h.factory = OFFactories.getFactory(h.ofVersion); |
alshabib | 70fc7fb | 2015-01-06 11:04:29 -0800 | [diff] [blame] | 268 | OFHello hi = |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 269 | h.factory.buildHello() |
alshabib | 70fc7fb | 2015-01-06 11:04:29 -0800 | [diff] [blame] | 270 | .setXid(h.handshakeTransactionIds--) |
| 271 | .build(); |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 272 | h.channel.writeAndFlush(Collections.singletonList(hi)); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 273 | } else { |
| 274 | log.error("Received Hello of version {} from switch at {}. " |
| 275 | + "This controller works with OF1.0 and OF1.3 " |
| 276 | + "switches. Disconnecting switch ...", |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 277 | m.getVersion(), h.channel.remoteAddress()); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 278 | h.channel.disconnect(); |
| 279 | return; |
| 280 | } |
| 281 | h.sendHandshakeFeaturesRequestMessage(); |
| 282 | h.setState(WAIT_FEATURES_REPLY); |
| 283 | } |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 284 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 285 | @Override |
| 286 | void processOFFeaturesReply(OFChannelHandler h, OFFeaturesReply m) |
| 287 | throws IOException, SwitchStateException { |
| 288 | illegalMessageReceived(h, m); |
| 289 | } |
| 290 | @Override |
| 291 | void processOFStatisticsReply(OFChannelHandler h, |
| 292 | OFStatsReply m) |
| 293 | throws IOException, SwitchStateException { |
| 294 | illegalMessageReceived(h, m); |
| 295 | } |
| 296 | @Override |
| 297 | void processOFError(OFChannelHandler h, OFErrorMsg m) { |
| 298 | logErrorDisconnect(h, m); |
| 299 | } |
| 300 | |
| 301 | @Override |
| 302 | void processOFPortStatus(OFChannelHandler h, OFPortStatus m) |
| 303 | throws IOException { |
| 304 | unhandledMessageReceived(h, m); |
| 305 | } |
| 306 | }, |
| 307 | |
| 308 | |
| 309 | /** |
| 310 | * We are waiting for a features reply message. Once we receive it, the |
| 311 | * behavior depends on whether this is a 1.0 or 1.3 switch. For 1.0, |
| 312 | * we send a SetConfig request, barrier, and GetConfig request and the |
| 313 | * next state is WAIT_CONFIG_REPLY. For 1.3, we send a Port description |
| 314 | * request and the next state is WAIT_PORT_DESC_REPLY. |
| 315 | */ |
| 316 | WAIT_FEATURES_REPLY(false) { |
| 317 | @Override |
Brian O'Connor | 47a1609 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 318 | void processOFFeaturesReply(OFChannelHandler h, OFFeaturesReply m) |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 319 | throws IOException { |
Brian O'Connor | 47a1609 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 320 | Long dpid = m.getDatapathId().getLong(); |
| 321 | if (!h.setDpid(dpid, h.channel)) { |
| 322 | log.error("Switch presented invalid certificate for dpid {}. Disconnecting", |
| 323 | dpid); |
| 324 | h.channel.disconnect(); |
| 325 | return; |
| 326 | } |
alshabib | 09d48be | 2014-10-03 15:43:33 -0700 | [diff] [blame] | 327 | log.debug("Received features reply for switch at {} with dpid {}", |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 328 | h.getSwitchInfoString(), h.thisdpid); |
| 329 | |
| 330 | h.featuresReply = m; //temp store |
| 331 | if (h.ofVersion == OFVersion.OF_10) { |
| 332 | h.sendHandshakeSetConfig(); |
| 333 | h.setState(WAIT_CONFIG_REPLY); |
| 334 | } else { |
| 335 | //version is 1.3, must get switchport information |
| 336 | h.sendHandshakeOFPortDescRequest(); |
| 337 | h.setState(WAIT_PORT_DESC_REPLY); |
| 338 | } |
| 339 | } |
| 340 | @Override |
| 341 | void processOFStatisticsReply(OFChannelHandler h, |
| 342 | OFStatsReply m) |
| 343 | throws IOException, SwitchStateException { |
| 344 | illegalMessageReceived(h, m); |
| 345 | } |
| 346 | @Override |
| 347 | void processOFError(OFChannelHandler h, OFErrorMsg m) { |
| 348 | logErrorDisconnect(h, m); |
| 349 | } |
| 350 | |
| 351 | @Override |
| 352 | void processOFPortStatus(OFChannelHandler h, OFPortStatus m) |
| 353 | throws IOException { |
Thomas Vachuska | 3927446 | 2014-12-02 13:23:50 -0800 | [diff] [blame] | 354 | h.pendingPortStatusMsg.add(m); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 355 | } |
| 356 | }, |
| 357 | |
| 358 | /** |
| 359 | * We are waiting for a description of the 1.3 switch ports. |
| 360 | * Once received, we send a SetConfig request |
| 361 | * Next State is WAIT_CONFIG_REPLY |
| 362 | */ |
| 363 | WAIT_PORT_DESC_REPLY(false) { |
| 364 | |
| 365 | @Override |
| 366 | void processOFStatisticsReply(OFChannelHandler h, OFStatsReply m) |
| 367 | throws SwitchStateException { |
| 368 | // Read port description |
| 369 | if (m.getStatsType() != OFStatsType.PORT_DESC) { |
| 370 | log.warn("Expecting port description stats but received stats " |
| 371 | + "type {} from {}. Ignoring ...", m.getStatsType(), |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 372 | h.channel.remoteAddress()); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 373 | return; |
| 374 | } |
| 375 | if (m.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 376 | log.debug("Stats reply indicates more stats from sw {} for " |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 377 | + "port description", |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 378 | h.getSwitchInfoString()); |
Yuta HIGUCHI | 605758e | 2017-01-13 20:26:44 -0800 | [diff] [blame] | 379 | h.portDescReplies.add((OFPortDescStatsReply) m); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 380 | return; |
Yuta HIGUCHI | 605758e | 2017-01-13 20:26:44 -0800 | [diff] [blame] | 381 | } else { |
| 382 | h.portDescReplies.add((OFPortDescStatsReply) m); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 383 | } |
| 384 | //h.portDescReply = (OFPortDescStatsReply) m; // temp store |
Saurav Das | 45f4815 | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 385 | log.debug("Received port desc reply for switch at {}: {}", |
| 386 | h.getSwitchInfoString(), |
| 387 | ((OFPortDescStatsReply) m).getEntries()); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 388 | try { |
| 389 | h.sendHandshakeSetConfig(); |
| 390 | } catch (IOException e) { |
| 391 | log.error("Unable to send setConfig after PortDescReply. " |
| 392 | + "Error: {}", e.getMessage()); |
| 393 | } |
| 394 | h.setState(WAIT_CONFIG_REPLY); |
| 395 | } |
| 396 | |
| 397 | @Override |
| 398 | void processOFError(OFChannelHandler h, OFErrorMsg m) |
| 399 | throws IOException, SwitchStateException { |
| 400 | logErrorDisconnect(h, m); |
| 401 | |
| 402 | } |
| 403 | |
| 404 | @Override |
| 405 | void processOFPortStatus(OFChannelHandler h, OFPortStatus m) |
| 406 | throws IOException, SwitchStateException { |
Thomas Vachuska | 3927446 | 2014-12-02 13:23:50 -0800 | [diff] [blame] | 407 | h.pendingPortStatusMsg.add(m); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 408 | |
| 409 | } |
| 410 | }, |
| 411 | |
| 412 | /** |
| 413 | * We are waiting for a config reply message. Once we receive it |
| 414 | * we send a DescriptionStatsRequest to the switch. |
| 415 | * Next state: WAIT_DESCRIPTION_STAT_REPLY |
| 416 | */ |
| 417 | WAIT_CONFIG_REPLY(false) { |
| 418 | @Override |
| 419 | void processOFGetConfigReply(OFChannelHandler h, OFGetConfigReply m) |
| 420 | throws IOException { |
| 421 | if (m.getMissSendLen() == 0xffff) { |
| 422 | log.trace("Config Reply from switch {} confirms " |
| 423 | + "miss length set to 0xffff", |
| 424 | h.getSwitchInfoString()); |
| 425 | } else { |
| 426 | // FIXME: we can't really deal with switches that don't send |
| 427 | // full packets. Shouldn't we drop the connection here? |
Yuta HIGUCHI | 605758e | 2017-01-13 20:26:44 -0800 | [diff] [blame] | 428 | log.warn("Config Reply from switch {} has " |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 429 | + "miss length set to {}", |
| 430 | h.getSwitchInfoString(), |
| 431 | m.getMissSendLen()); |
| 432 | } |
Jordi Ortiz | 91477b8 | 2016-11-29 15:22:50 +0100 | [diff] [blame] | 433 | |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 434 | nextState(h); |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Transition to next state based on OF version. |
| 439 | * |
| 440 | * @param h current channel handler |
| 441 | * @throws IOException |
| 442 | */ |
| 443 | private void nextState(OFChannelHandler h) throws IOException { |
| 444 | if (h.ofVersion.getWireVersion() >= OFVersion.OF_13.getWireVersion()) { |
Jordi Ortiz | 91477b8 | 2016-11-29 15:22:50 +0100 | [diff] [blame] | 445 | // Meters were introduced in OpenFlow 1.3 |
| 446 | h.sendMeterFeaturesRequest(); |
| 447 | h.setState(WAIT_METER_FEATURES_REPLY); |
Yuta HIGUCHI | 605758e | 2017-01-13 20:26:44 -0800 | [diff] [blame] | 448 | } else { |
Jordi Ortiz | 91477b8 | 2016-11-29 15:22:50 +0100 | [diff] [blame] | 449 | h.sendHandshakeDescriptionStatsRequest(); |
| 450 | h.setState(WAIT_DESCRIPTION_STAT_REPLY); |
| 451 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | @Override |
| 455 | void processOFBarrierReply(OFChannelHandler h, OFBarrierReply m) { |
| 456 | // do nothing; |
| 457 | } |
| 458 | |
| 459 | @Override |
| 460 | void processOFFeaturesReply(OFChannelHandler h, OFFeaturesReply m) |
| 461 | throws IOException, SwitchStateException { |
| 462 | illegalMessageReceived(h, m); |
| 463 | } |
| 464 | @Override |
| 465 | void processOFStatisticsReply(OFChannelHandler h, |
| 466 | OFStatsReply m) |
| 467 | throws IOException, SwitchStateException { |
| 468 | log.error("Received multipart(stats) message sub-type {}", |
| 469 | m.getStatsType()); |
| 470 | illegalMessageReceived(h, m); |
| 471 | } |
| 472 | |
| 473 | @Override |
| 474 | void processOFError(OFChannelHandler h, OFErrorMsg m) { |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 475 | if (m.getErrType() == OFErrorType.BAD_REQUEST) { |
| 476 | OFBadRequestErrorMsg badRequest = (OFBadRequestErrorMsg) m; |
| 477 | if (badRequest.getCode() == OFBadRequestCode.BAD_TYPE) { |
| 478 | log.debug("{} does not support GetConfig, moving on", h.getSwitchInfoString()); |
| 479 | try { |
| 480 | nextState(h); |
| 481 | return; |
| 482 | } catch (IOException e) { |
| 483 | log.error("Exception thrown transitioning to next", e); |
| 484 | logErrorDisconnect(h, m); |
| 485 | } |
| 486 | } |
| 487 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 488 | logErrorDisconnect(h, m); |
| 489 | } |
| 490 | |
| 491 | @Override |
| 492 | void processOFPortStatus(OFChannelHandler h, OFPortStatus m) |
| 493 | throws IOException { |
| 494 | h.pendingPortStatusMsg.add(m); |
| 495 | } |
| 496 | }, |
| 497 | |
| 498 | |
| 499 | /** |
| 500 | * We are waiting for a OFDescriptionStat message from the switch. |
| 501 | * Once we receive any stat message we try to parse it. If it's not |
| 502 | * a description stats message we disconnect. If its the expected |
| 503 | * description stats message, we: |
| 504 | * - use the switch driver to bind the switch and get an IOFSwitch instance |
| 505 | * - setup the IOFSwitch instance |
| 506 | * - add switch controller and send the initial role |
| 507 | * request to the switch. |
| 508 | * Next state: WAIT_INITIAL_ROLE |
| 509 | * In the typical case, where switches support role request messages |
| 510 | * the next state is where we expect the role reply message. |
| 511 | * In the special case that where the switch does not support any kind |
| 512 | * of role request messages, we don't send a role message, but we do |
| 513 | * request mastership from the registry service. This controller |
| 514 | * should become master once we hear back from the registry service. |
| 515 | * All following states will have a h.sw instance! |
| 516 | */ |
| 517 | WAIT_DESCRIPTION_STAT_REPLY(false) { |
| 518 | @Override |
| 519 | void processOFStatisticsReply(OFChannelHandler h, OFStatsReply m) |
| 520 | throws SwitchStateException { |
| 521 | // Read description, if it has been updated |
| 522 | if (m.getStatsType() != OFStatsType.DESC) { |
| 523 | log.warn("Expecting Description stats but received stats " |
| 524 | + "type {} from {}. Ignoring ...", m.getStatsType(), |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 525 | h.channel.remoteAddress()); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 526 | return; |
| 527 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 528 | OFDescStatsReply drep = (OFDescStatsReply) m; |
Saurav Das | f9ba422 | 2015-05-07 17:13:59 -0700 | [diff] [blame] | 529 | log.info("Received switch description reply {} from switch at {}", |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 530 | drep, h.channel.remoteAddress()); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 531 | // Here is where we differentiate between different kinds of switches |
| 532 | h.sw = h.controller.getOFSwitchInstance(h.thisdpid, drep, h.ofVersion); |
| 533 | |
| 534 | h.sw.setOFVersion(h.ofVersion); |
| 535 | h.sw.setFeaturesReply(h.featuresReply); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 536 | h.sw.setPortDescReplies(h.portDescReplies); |
Jordi Ortiz | 91477b8 | 2016-11-29 15:22:50 +0100 | [diff] [blame] | 537 | h.sw.setMeterFeaturesReply(h.meterFeaturesReply); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 538 | h.sw.setConnected(true); |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 539 | h.sw.setChannel(h); |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 540 | // boolean success = h.sw.connectSwitch(); |
| 541 | // |
| 542 | // if (!success) { |
| 543 | // disconnectDuplicate(h); |
| 544 | // return; |
| 545 | // } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 546 | // set switch information |
| 547 | |
| 548 | |
| 549 | |
alshabib | 09d48be | 2014-10-03 15:43:33 -0700 | [diff] [blame] | 550 | log.debug("Switch {} bound to class {}, description {}", |
Ray Milkey | 6bc43c2 | 2015-11-06 13:22:38 -0800 | [diff] [blame] | 551 | h.sw, h.sw.getClass(), drep); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 552 | //Put switch in EQUAL mode until we hear back from the global registry |
| 553 | //log.debug("Setting new switch {} to EQUAL and sending Role request", |
| 554 | // h.sw.getStringId()); |
| 555 | //h.sw.activateEqualSwitch(); |
| 556 | //h.setSwitchRole(RoleState.EQUAL); |
| 557 | |
| 558 | h.sw.startDriverHandshake(); |
alshabib | 9eab22f | 2014-10-20 17:17:31 -0700 | [diff] [blame] | 559 | if (h.sw.isDriverHandshakeComplete()) { |
pier | 7f292e7 | 2019-07-16 15:52:50 +0200 | [diff] [blame] | 560 | // We are not able to complete the connection for a dpid collision. |
| 561 | // Same device reconnecting or different device configured with |
| 562 | // the same dpid. |
alshabib | 9eab22f | 2014-10-20 17:17:31 -0700 | [diff] [blame] | 563 | if (!h.sw.connectSwitch()) { |
pier | 7f292e7 | 2019-07-16 15:52:50 +0200 | [diff] [blame] | 564 | // Disconnect from the device and return |
alshabib | 9eab22f | 2014-10-20 17:17:31 -0700 | [diff] [blame] | 565 | disconnectDuplicate(h); |
pier | 7f292e7 | 2019-07-16 15:52:50 +0200 | [diff] [blame] | 566 | return; |
alshabib | 9eab22f | 2014-10-20 17:17:31 -0700 | [diff] [blame] | 567 | } |
Thomas Vachuska | 3927446 | 2014-12-02 13:23:50 -0800 | [diff] [blame] | 568 | handlePendingPortStatusMessages(h); |
alshabib | 9eab22f | 2014-10-20 17:17:31 -0700 | [diff] [blame] | 569 | h.setState(ACTIVE); |
| 570 | } else { |
| 571 | h.setState(WAIT_SWITCH_DRIVER_SUB_HANDSHAKE); |
| 572 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 573 | |
| 574 | } |
| 575 | |
| 576 | @Override |
| 577 | void processOFError(OFChannelHandler h, OFErrorMsg m) { |
| 578 | logErrorDisconnect(h, m); |
| 579 | } |
| 580 | |
| 581 | @Override |
| 582 | void processOFFeaturesReply(OFChannelHandler h, OFFeaturesReply m) |
| 583 | throws IOException, SwitchStateException { |
| 584 | illegalMessageReceived(h, m); |
| 585 | } |
| 586 | |
| 587 | @Override |
| 588 | void processOFPortStatus(OFChannelHandler h, OFPortStatus m) |
| 589 | throws IOException { |
| 590 | h.pendingPortStatusMsg.add(m); |
| 591 | } |
| 592 | }, |
| 593 | |
| 594 | |
| 595 | /** |
| 596 | * We are waiting for the respective switch driver to complete its |
| 597 | * configuration. Notice that we do not consider this to be part of the main |
| 598 | * switch-controller handshake. But we do consider it as a step that comes |
| 599 | * before we declare the switch as available to the controller. |
| 600 | * Next State: depends on the role of this controller for this switch - either |
| 601 | * MASTER or EQUAL. |
| 602 | */ |
| 603 | WAIT_SWITCH_DRIVER_SUB_HANDSHAKE(true) { |
| 604 | |
| 605 | @Override |
| 606 | void processOFError(OFChannelHandler h, OFErrorMsg m) |
| 607 | throws IOException { |
| 608 | // will never be called. We override processOFMessage |
| 609 | } |
| 610 | |
alshabib | d796391 | 2014-10-20 14:52:04 -0700 | [diff] [blame] | 611 | |
| 612 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 613 | @Override |
| 614 | void processOFMessage(OFChannelHandler h, OFMessage m) |
| 615 | throws IOException, SwitchStateException { |
alshabib | d796391 | 2014-10-20 14:52:04 -0700 | [diff] [blame] | 616 | |
| 617 | if (h.sw.isDriverHandshakeComplete()) { |
| 618 | moveToActive(h); |
alshabib | 9eab22f | 2014-10-20 17:17:31 -0700 | [diff] [blame] | 619 | h.state.processOFMessage(h, m); |
| 620 | return; |
alshabib | d796391 | 2014-10-20 14:52:04 -0700 | [diff] [blame] | 621 | |
| 622 | } |
| 623 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 624 | if (m.getType() == OFType.ECHO_REQUEST) { |
| 625 | processOFEchoRequest(h, (OFEchoRequest) m); |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 626 | } else if (m.getType() == OFType.ECHO_REPLY) { |
| 627 | processOFEchoReply(h, (OFEchoReply) m); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 628 | } else if (m.getType() == OFType.ROLE_REPLY) { |
| 629 | h.sw.handleRole(m); |
| 630 | } else if (m.getType() == OFType.ERROR) { |
Yuta HIGUCHI | 605758e | 2017-01-13 20:26:44 -0800 | [diff] [blame] | 631 | if (!h.sw.handleRoleError((OFErrorMsg) m)) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 632 | h.sw.processDriverHandshakeMessage(m); |
| 633 | if (h.sw.isDriverHandshakeComplete()) { |
alshabib | d796391 | 2014-10-20 14:52:04 -0700 | [diff] [blame] | 634 | moveToActive(h); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | } else { |
| 638 | if (m.getType() == OFType.EXPERIMENTER && |
| 639 | ((OFExperimenter) m).getExperimenter() == |
| 640 | RoleManager.NICIRA_EXPERIMENTER) { |
| 641 | h.sw.handleNiciraRole(m); |
| 642 | } else { |
| 643 | h.sw.processDriverHandshakeMessage(m); |
| 644 | if (h.sw.isDriverHandshakeComplete()) { |
alshabib | d796391 | 2014-10-20 14:52:04 -0700 | [diff] [blame] | 645 | moveToActive(h); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | @Override |
| 652 | void processOFPortStatus(OFChannelHandler h, OFPortStatus m) |
| 653 | throws IOException, SwitchStateException { |
| 654 | h.pendingPortStatusMsg.add(m); |
| 655 | } |
alshabib | d796391 | 2014-10-20 14:52:04 -0700 | [diff] [blame] | 656 | |
| 657 | private void moveToActive(OFChannelHandler h) { |
| 658 | boolean success = h.sw.connectSwitch(); |
pier | 7f292e7 | 2019-07-16 15:52:50 +0200 | [diff] [blame] | 659 | // Disconnect from the device and return |
alshabib | d796391 | 2014-10-20 14:52:04 -0700 | [diff] [blame] | 660 | if (!success) { |
| 661 | disconnectDuplicate(h); |
pier | 7f292e7 | 2019-07-16 15:52:50 +0200 | [diff] [blame] | 662 | return; |
alshabib | d796391 | 2014-10-20 14:52:04 -0700 | [diff] [blame] | 663 | } |
pier | 7f292e7 | 2019-07-16 15:52:50 +0200 | [diff] [blame] | 664 | handlePendingPortStatusMessages(h); |
| 665 | h.setState(ACTIVE); |
alshabib | d796391 | 2014-10-20 14:52:04 -0700 | [diff] [blame] | 666 | } |
| 667 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 668 | }, |
| 669 | |
Jordi Ortiz | 91477b8 | 2016-11-29 15:22:50 +0100 | [diff] [blame] | 670 | /** |
| 671 | * We are expecting a OF Multi Part Meter Features Stats Reply. |
| 672 | * Notice that this information is only available for switches running |
| 673 | * OpenFlow 1.3 |
| 674 | */ |
| 675 | WAIT_METER_FEATURES_REPLY(true) { |
Yuta HIGUCHI | 10f4513 | 2017-03-01 17:09:32 -0800 | [diff] [blame] | 676 | |
| 677 | @Override |
| 678 | void processOFEchoRequest(OFChannelHandler h, OFEchoRequest m) |
| 679 | throws IOException { |
| 680 | super.processOFEchoRequest(h, m); |
| 681 | if (System.currentTimeMillis() - h.lastStateChange > METER_TIMEOUT) { |
| 682 | log.info("{} did not respond to MeterFeaturesRequest on time, " + |
| 683 | "moving on without it.", |
| 684 | h.getSwitchInfoString()); |
| 685 | h.sendHandshakeDescriptionStatsRequest(); |
| 686 | h.setState(WAIT_DESCRIPTION_STAT_REPLY); |
| 687 | } |
| 688 | } |
| 689 | |
Jordi Ortiz | 91477b8 | 2016-11-29 15:22:50 +0100 | [diff] [blame] | 690 | @Override |
| 691 | void processOFError(OFChannelHandler h, OFErrorMsg m) |
| 692 | throws IOException { |
Charles Chan | 34155e5 | 2016-11-30 18:28:11 -0800 | [diff] [blame] | 693 | // Hardware switches may reply OFErrorMsg if meter is not supported |
| 694 | log.warn("Received OFError {}. It seems {} does not support Meter.", |
| 695 | m.getErrType().name(), Dpid.uri(h.thisdpid)); |
| 696 | log.debug("{}", m); |
| 697 | h.sendHandshakeDescriptionStatsRequest(); |
| 698 | h.setState(WAIT_DESCRIPTION_STAT_REPLY); |
Jordi Ortiz | 91477b8 | 2016-11-29 15:22:50 +0100 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | @Override |
| 702 | void processOFStatisticsReply(OFChannelHandler h, |
| 703 | OFStatsReply m) |
| 704 | throws IOException, SwitchStateException { |
Yuta HIGUCHI | 605758e | 2017-01-13 20:26:44 -0800 | [diff] [blame] | 705 | switch (m.getStatsType()) { |
Jordi Ortiz | 91477b8 | 2016-11-29 15:22:50 +0100 | [diff] [blame] | 706 | case METER_FEATURES: |
| 707 | |
| 708 | log.debug("Received Meter Features"); |
Yuta HIGUCHI | 605758e | 2017-01-13 20:26:44 -0800 | [diff] [blame] | 709 | OFMeterFeaturesStatsReply ofmfsr = (OFMeterFeaturesStatsReply) m; |
Jordi Ortiz | 91477b8 | 2016-11-29 15:22:50 +0100 | [diff] [blame] | 710 | log.info("Received meter features from {} with max meters: {}", |
| 711 | h.getSwitchInfoString(), |
| 712 | ofmfsr.getFeatures().getMaxMeter()); |
| 713 | h.meterFeaturesReply = ofmfsr; |
| 714 | h.sendHandshakeDescriptionStatsRequest(); |
| 715 | h.setState(WAIT_DESCRIPTION_STAT_REPLY); |
| 716 | break; |
| 717 | default: |
| 718 | log.error("Unexpected OF Multi Part stats reply"); |
| 719 | illegalMessageReceived(h, m); |
| 720 | break; |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | @Override |
| 725 | void processOFFeaturesReply(OFChannelHandler h, OFFeaturesReply m) |
| 726 | throws IOException, SwitchStateException { |
| 727 | illegalMessageReceived(h, m); |
| 728 | } |
| 729 | |
| 730 | @Override |
| 731 | void processOFPortStatus(OFChannelHandler h, OFPortStatus m) |
| 732 | throws IOException { |
| 733 | h.pendingPortStatusMsg.add(m); |
| 734 | } |
Yuta HIGUCHI | 1745e5a | 2017-01-15 21:43:02 -0800 | [diff] [blame] | 735 | |
| 736 | @Override |
| 737 | void processIdle(OFChannelHandler h) throws IOException { |
| 738 | log.info("{} did not respond to MeterFeaturesRequest, " + |
| 739 | "moving on without it.", |
| 740 | h.getSwitchInfoString()); |
| 741 | h.sendHandshakeDescriptionStatsRequest(); |
| 742 | h.setState(WAIT_DESCRIPTION_STAT_REPLY); |
| 743 | } |
Jordi Ortiz | 91477b8 | 2016-11-29 15:22:50 +0100 | [diff] [blame] | 744 | }, |
| 745 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 746 | |
| 747 | /** |
| 748 | * This controller is in MASTER role for this switch. We enter this state |
| 749 | * after requesting and winning control from the global registry. |
| 750 | * The main handshake as well as the switch-driver sub-handshake |
| 751 | * is complete at this point. |
| 752 | * // XXX S reconsider below |
| 753 | * In the (near) future we may deterministically assign controllers to |
| 754 | * switches at startup. |
| 755 | * We only leave this state if the switch disconnects or |
| 756 | * if we send a role request for SLAVE /and/ receive the role reply for |
| 757 | * SLAVE. |
| 758 | */ |
| 759 | ACTIVE(true) { |
| 760 | @Override |
| 761 | void processOFError(OFChannelHandler h, OFErrorMsg m) |
| 762 | throws IOException, SwitchStateException { |
| 763 | // if we get here, then the error message is for something else |
| 764 | if (m.getErrType() == OFErrorType.BAD_REQUEST && |
Ray Milkey | 30d1965 | 2016-09-06 12:09:46 -0700 | [diff] [blame] | 765 | (((OFBadRequestErrorMsg) m).getCode() == |
Charles Chan | 9d7465e | 2016-09-09 19:02:34 -0700 | [diff] [blame] | 766 | OFBadRequestCode.EPERM || |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 767 | ((OFBadRequestErrorMsg) m).getCode() == |
Charles Chan | 9d7465e | 2016-09-09 19:02:34 -0700 | [diff] [blame] | 768 | OFBadRequestCode.IS_SLAVE)) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 769 | // We are the master controller and the switch returned |
| 770 | // a permission error. This is a likely indicator that |
| 771 | // the switch thinks we are slave. Reassert our |
| 772 | // role |
| 773 | // FIXME: this could be really bad during role transitions |
| 774 | // if two controllers are master (even if its only for |
| 775 | // a brief period). We might need to see if these errors |
| 776 | // persist before we reassert |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 777 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 778 | h.sw.reassertRole(); |
| 779 | } else if (m.getErrType() == OFErrorType.FLOW_MOD_FAILED && |
| 780 | ((OFFlowModFailedErrorMsg) m).getCode() == |
| 781 | OFFlowModFailedCode.ALL_TABLES_FULL) { |
| 782 | h.sw.setTableFull(true); |
| 783 | } else { |
| 784 | logError(h, m); |
| 785 | } |
| 786 | h.dispatchMessage(m); |
| 787 | } |
| 788 | |
| 789 | @Override |
| 790 | void processOFStatisticsReply(OFChannelHandler h, |
| 791 | OFStatsReply m) { |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 792 | if (m.getStatsType().equals(OFStatsType.PORT_DESC)) { |
Saurav Das | 45f4815 | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 793 | log.debug("Received port desc message from {}: {}", |
| 794 | h.sw.getDpid(), |
| 795 | ((OFPortDescStatsReply) m).getEntries()); |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 796 | h.sw.setPortDescReply((OFPortDescStatsReply) m); |
| 797 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 798 | h.dispatchMessage(m); |
| 799 | } |
| 800 | |
| 801 | @Override |
| 802 | void processOFExperimenter(OFChannelHandler h, OFExperimenter m) |
| 803 | throws SwitchStateException { |
| 804 | h.sw.handleNiciraRole(m); |
| 805 | } |
| 806 | |
| 807 | @Override |
| 808 | void processOFRoleReply(OFChannelHandler h, OFRoleReply m) |
| 809 | throws SwitchStateException { |
| 810 | h.sw.handleRole(m); |
| 811 | } |
| 812 | |
| 813 | @Override |
| 814 | void processOFPortStatus(OFChannelHandler h, OFPortStatus m) |
| 815 | throws SwitchStateException { |
| 816 | handlePortStatusMessage(h, m, true); |
Thomas Vachuska | 3927446 | 2014-12-02 13:23:50 -0800 | [diff] [blame] | 817 | //h.dispatchMessage(m); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | @Override |
| 821 | void processOFPacketIn(OFChannelHandler h, OFPacketIn m) { |
alshabib | 9eab22f | 2014-10-20 17:17:31 -0700 | [diff] [blame] | 822 | // OFPacketOut out = |
| 823 | // h.sw.factory().buildPacketOut() |
| 824 | // .setXid(m.getXid()) |
| 825 | // .setBufferId(m.getBufferId()).build(); |
| 826 | // h.sw.sendMsg(out); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 827 | h.dispatchMessage(m); |
| 828 | } |
| 829 | |
| 830 | @Override |
| 831 | void processOFFlowRemoved(OFChannelHandler h, |
| 832 | OFFlowRemoved m) { |
| 833 | h.dispatchMessage(m); |
| 834 | } |
| 835 | |
| 836 | @Override |
| 837 | void processOFBarrierReply(OFChannelHandler h, OFBarrierReply m) { |
| 838 | h.dispatchMessage(m); |
| 839 | } |
| 840 | |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 841 | @Override |
| 842 | void processOFFeaturesReply(OFChannelHandler h, OFFeaturesReply m) { |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 843 | h.sw.setFeaturesReply(m); |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 844 | h.dispatchMessage(m); |
| 845 | } |
| 846 | |
Yuta HIGUCHI | 1745e5a | 2017-01-15 21:43:02 -0800 | [diff] [blame] | 847 | @Override |
| 848 | void processIdle(OFChannelHandler h) throws IOException { |
| 849 | log.info("{} idle", h.getSwitchInfoString()); |
| 850 | } |
| 851 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 852 | }; |
| 853 | |
| 854 | private final boolean handshakeComplete; |
| 855 | ChannelState(boolean handshakeComplete) { |
| 856 | this.handshakeComplete = handshakeComplete; |
| 857 | } |
| 858 | |
| 859 | /** |
| 860 | * Is this a state in which the handshake has completed? |
| 861 | * @return true if the handshake is complete |
| 862 | */ |
| 863 | public boolean isHandshakeComplete() { |
| 864 | return handshakeComplete; |
| 865 | } |
| 866 | |
| 867 | /** |
| 868 | * Get a string specifying the switch connection, state, and |
| 869 | * message received. To be used as message for SwitchStateException |
| 870 | * or log messages |
| 871 | * @param h The channel handler (to get switch information_ |
| 872 | * @param m The OFMessage that has just been received |
| 873 | * @param details A string giving more details about the exact nature |
| 874 | * of the problem. |
| 875 | * @return display string |
| 876 | */ |
| 877 | // needs to be protected because enum members are actually subclasses |
| 878 | protected String getSwitchStateMessage(OFChannelHandler h, |
| 879 | OFMessage m, |
| 880 | String details) { |
| 881 | return String.format("Switch: [%s], State: [%s], received: [%s]" |
| 882 | + ", details: %s", |
| 883 | h.getSwitchInfoString(), |
| 884 | this.toString(), |
| 885 | m.getType().toString(), |
| 886 | details); |
| 887 | } |
| 888 | |
| 889 | /** |
| 890 | * We have an OFMessage we didn't expect given the current state and |
| 891 | * we want to treat this as an error. |
| 892 | * We currently throw an exception that will terminate the connection |
| 893 | * However, we could be more forgiving |
| 894 | * @param h the channel handler that received the message |
| 895 | * @param m the message |
Jonathan Hart | 147b2ac | 2014-10-23 10:03:52 -0700 | [diff] [blame] | 896 | * @throws SwitchStateException we always throw the exception |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 897 | */ |
Jonathan Hart | 147b2ac | 2014-10-23 10:03:52 -0700 | [diff] [blame] | 898 | // needs to be protected because enum members are actually subclasses |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 899 | protected void illegalMessageReceived(OFChannelHandler h, OFMessage m) |
| 900 | throws SwitchStateException { |
| 901 | String msg = getSwitchStateMessage(h, m, |
| 902 | "Switch should never send this message in the current state"); |
| 903 | throw new SwitchStateException(msg); |
| 904 | |
| 905 | } |
| 906 | |
| 907 | /** |
| 908 | * We have an OFMessage we didn't expect given the current state and |
| 909 | * we want to ignore the message. |
| 910 | * @param h the channel handler the received the message |
| 911 | * @param m the message |
| 912 | */ |
| 913 | protected void unhandledMessageReceived(OFChannelHandler h, |
| 914 | OFMessage m) { |
| 915 | if (log.isDebugEnabled()) { |
| 916 | String msg = getSwitchStateMessage(h, m, |
| 917 | "Ignoring unexpected message"); |
| 918 | log.debug(msg); |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | /** |
| 923 | * Log an OpenFlow error message from a switch. |
| 924 | * @param h The switch that sent the error |
| 925 | * @param error The error message |
| 926 | */ |
| 927 | protected void logError(OFChannelHandler h, OFErrorMsg error) { |
alshabib | 09d48be | 2014-10-03 15:43:33 -0700 | [diff] [blame] | 928 | log.error("{} from switch {} in state {}", |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 929 | error, |
| 930 | h.getSwitchInfoString(), |
Yuta HIGUCHI | 605758e | 2017-01-13 20:26:44 -0800 | [diff] [blame] | 931 | this); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | /** |
| 935 | * Log an OpenFlow error message from a switch and disconnect the |
| 936 | * channel. |
| 937 | * |
| 938 | * @param h the IO channel for this switch. |
| 939 | * @param error The error message |
| 940 | */ |
| 941 | protected void logErrorDisconnect(OFChannelHandler h, OFErrorMsg error) { |
| 942 | logError(h, error); |
HIGUCHI Yuta | dc5cf8a | 2016-04-29 15:17:06 -0700 | [diff] [blame] | 943 | log.error("Disconnecting switch {}", h.getSwitchInfoString()); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 944 | h.channel.disconnect(); |
| 945 | } |
| 946 | |
| 947 | /** |
| 948 | * log an error message for a duplicate dpid and disconnect this channel. |
| 949 | * @param h the IO channel for this switch. |
| 950 | */ |
| 951 | protected void disconnectDuplicate(OFChannelHandler h) { |
| 952 | log.error("Duplicated dpid or incompleted cleanup - " |
| 953 | + "disconnecting channel {}", h.getSwitchInfoString()); |
| 954 | h.duplicateDpidFound = Boolean.TRUE; |
| 955 | h.channel.disconnect(); |
| 956 | } |
| 957 | |
| 958 | |
| 959 | |
| 960 | /** |
| 961 | * Handles all pending port status messages before a switch is declared |
| 962 | * activated in MASTER or EQUAL role. Note that since this handling |
| 963 | * precedes the activation (and therefore notification to IOFSwitchListerners) |
| 964 | * the changes to ports will already be visible once the switch is |
| 965 | * activated. As a result, no notifications are sent out for these |
| 966 | * pending portStatus messages. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 967 | * |
| 968 | * @param h the channel handler that received the message |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 969 | */ |
| 970 | protected void handlePendingPortStatusMessages(OFChannelHandler h) { |
| 971 | try { |
| 972 | handlePendingPortStatusMessages(h, 0); |
| 973 | } catch (SwitchStateException e) { |
| 974 | log.error(e.getMessage()); |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | private void handlePendingPortStatusMessages(OFChannelHandler h, int index) |
| 979 | throws SwitchStateException { |
| 980 | if (h.sw == null) { |
| 981 | String msg = "State machine error: switch is null. Should never " + |
| 982 | "happen"; |
| 983 | throw new SwitchStateException(msg); |
| 984 | } |
Thomas Vachuska | 3927446 | 2014-12-02 13:23:50 -0800 | [diff] [blame] | 985 | log.info("Processing {} pending port status messages for {}", |
| 986 | h.pendingPortStatusMsg.size(), h.sw.getStringId()); |
| 987 | |
Yuta HIGUCHI | 605758e | 2017-01-13 20:26:44 -0800 | [diff] [blame] | 988 | ArrayList<OFPortStatus> temp = new ArrayList<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 989 | for (OFPortStatus ps: h.pendingPortStatusMsg) { |
| 990 | temp.add(ps); |
| 991 | handlePortStatusMessage(h, ps, false); |
| 992 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 993 | // expensive but ok - we don't expect too many port-status messages |
| 994 | // note that we cannot use clear(), because of the reasons below |
| 995 | h.pendingPortStatusMsg.removeAll(temp); |
Thomas Vachuska | 3927446 | 2014-12-02 13:23:50 -0800 | [diff] [blame] | 996 | temp.clear(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 997 | // the iterator above takes a snapshot of the list - so while we were |
| 998 | // dealing with the pending port-status messages, we could have received |
| 999 | // newer ones. Handle them recursively, but break the recursion after |
| 1000 | // five steps to avoid an attack. |
| 1001 | if (!h.pendingPortStatusMsg.isEmpty() && ++index < 5) { |
| 1002 | handlePendingPortStatusMessages(h, index); |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | /** |
| 1007 | * Handle a port status message. |
| 1008 | * |
| 1009 | * Handle a port status message by updating the port maps in the |
| 1010 | * IOFSwitch instance and notifying Controller about the change so |
| 1011 | * it can dispatch a switch update. |
| 1012 | * |
| 1013 | * @param h The OFChannelHhandler that received the message |
| 1014 | * @param m The PortStatus message we received |
| 1015 | * @param doNotify if true switch port changed events will be |
| 1016 | * dispatched |
Thomas Vachuska | b14c77a | 2014-11-04 18:08:01 -0800 | [diff] [blame] | 1017 | * @throws SwitchStateException if the switch is not bound to the channel |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1018 | * |
| 1019 | */ |
| 1020 | protected void handlePortStatusMessage(OFChannelHandler h, OFPortStatus m, |
| 1021 | boolean doNotify) throws SwitchStateException { |
| 1022 | if (h.sw == null) { |
| 1023 | String msg = getSwitchStateMessage(h, m, |
| 1024 | "State machine error: switch is null. Should never " + |
| 1025 | "happen"); |
| 1026 | throw new SwitchStateException(msg); |
| 1027 | } |
Saurav Das | bd071d8 | 2018-01-09 17:38:44 -0800 | [diff] [blame] | 1028 | log.info("Received port status message from {}/{}: {}", |
| 1029 | h.sw.getDpid(), m.getDesc().getPortNo(), m); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1030 | |
| 1031 | h.sw.handleMessage(m); |
| 1032 | } |
| 1033 | |
| 1034 | |
| 1035 | /** |
| 1036 | * Process an OF message received on the channel and |
| 1037 | * update state accordingly. |
| 1038 | * |
| 1039 | * The main "event" of the state machine. Process the received message, |
| 1040 | * send follow up message if required and update state if required. |
| 1041 | * |
| 1042 | * Switches on the message type and calls more specific event handlers |
| 1043 | * for each individual OF message type. If we receive a message that |
| 1044 | * is supposed to be sent from a controller to a switch we throw |
| 1045 | * a SwitchStateExeption. |
| 1046 | * |
| 1047 | * The more specific handlers can also throw SwitchStateExceptions |
| 1048 | * |
| 1049 | * @param h The OFChannelHandler that received the message |
| 1050 | * @param m The message we received. |
Thomas Vachuska | b14c77a | 2014-11-04 18:08:01 -0800 | [diff] [blame] | 1051 | * @throws SwitchStateException if the switch is not bound to the channel |
| 1052 | * @throws IOException if unable to send message back to the switch |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1053 | */ |
| 1054 | void processOFMessage(OFChannelHandler h, OFMessage m) |
| 1055 | throws IOException, SwitchStateException { |
Yuta HIGUCHI | 605758e | 2017-01-13 20:26:44 -0800 | [diff] [blame] | 1056 | switch (m.getType()) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1057 | case HELLO: |
| 1058 | processOFHello(h, (OFHello) m); |
| 1059 | break; |
| 1060 | case BARRIER_REPLY: |
| 1061 | processOFBarrierReply(h, (OFBarrierReply) m); |
| 1062 | break; |
| 1063 | case ECHO_REPLY: |
| 1064 | processOFEchoReply(h, (OFEchoReply) m); |
| 1065 | break; |
| 1066 | case ECHO_REQUEST: |
| 1067 | processOFEchoRequest(h, (OFEchoRequest) m); |
| 1068 | break; |
| 1069 | case ERROR: |
| 1070 | processOFError(h, (OFErrorMsg) m); |
| 1071 | break; |
| 1072 | case FEATURES_REPLY: |
| 1073 | processOFFeaturesReply(h, (OFFeaturesReply) m); |
| 1074 | break; |
| 1075 | case FLOW_REMOVED: |
| 1076 | processOFFlowRemoved(h, (OFFlowRemoved) m); |
| 1077 | break; |
| 1078 | case GET_CONFIG_REPLY: |
| 1079 | processOFGetConfigReply(h, (OFGetConfigReply) m); |
| 1080 | break; |
| 1081 | case PACKET_IN: |
| 1082 | processOFPacketIn(h, (OFPacketIn) m); |
| 1083 | break; |
| 1084 | case PORT_STATUS: |
| 1085 | processOFPortStatus(h, (OFPortStatus) m); |
| 1086 | break; |
| 1087 | case QUEUE_GET_CONFIG_REPLY: |
| 1088 | processOFQueueGetConfigReply(h, (OFQueueGetConfigReply) m); |
| 1089 | break; |
| 1090 | case STATS_REPLY: // multipart_reply in 1.3 |
| 1091 | processOFStatisticsReply(h, (OFStatsReply) m); |
| 1092 | break; |
| 1093 | case EXPERIMENTER: |
| 1094 | processOFExperimenter(h, (OFExperimenter) m); |
| 1095 | break; |
| 1096 | case ROLE_REPLY: |
| 1097 | processOFRoleReply(h, (OFRoleReply) m); |
| 1098 | break; |
| 1099 | case GET_ASYNC_REPLY: |
| 1100 | processOFGetAsyncReply(h, (OFAsyncGetReply) m); |
| 1101 | break; |
| 1102 | |
| 1103 | // The following messages are sent to switches. The controller |
| 1104 | // should never receive them |
| 1105 | case SET_CONFIG: |
| 1106 | case GET_CONFIG_REQUEST: |
| 1107 | case PACKET_OUT: |
| 1108 | case PORT_MOD: |
| 1109 | case QUEUE_GET_CONFIG_REQUEST: |
| 1110 | case BARRIER_REQUEST: |
| 1111 | case STATS_REQUEST: // multipart request in 1.3 |
| 1112 | case FEATURES_REQUEST: |
| 1113 | case FLOW_MOD: |
| 1114 | case GROUP_MOD: |
| 1115 | case TABLE_MOD: |
| 1116 | case GET_ASYNC_REQUEST: |
| 1117 | case SET_ASYNC: |
| 1118 | case METER_MOD: |
| 1119 | default: |
| 1120 | illegalMessageReceived(h, m); |
| 1121 | break; |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | /*----------------------------------------------------------------- |
| 1126 | * Default implementation for message handlers in any state. |
| 1127 | * |
| 1128 | * Individual states must override these if they want a behavior |
| 1129 | * that differs from the default. |
| 1130 | * |
| 1131 | * In general, these handlers simply ignore the message and do |
| 1132 | * nothing. |
| 1133 | * |
| 1134 | * There are some exceptions though, since some messages really |
| 1135 | * are handled the same way in every state (e.g., ECHO_REQUST) or |
| 1136 | * that are only valid in a single state (e.g., HELLO, GET_CONFIG_REPLY |
| 1137 | -----------------------------------------------------------------*/ |
| 1138 | |
| 1139 | void processOFHello(OFChannelHandler h, OFHello m) |
| 1140 | throws IOException, SwitchStateException { |
| 1141 | // we only expect hello in the WAIT_HELLO state |
alshabib | 45fd88a | 2015-09-24 17:34:35 -0700 | [diff] [blame] | 1142 | log.warn("Received Hello outside WAIT_HELLO state; switch {} is not complaint.", |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1143 | h.channel.remoteAddress()); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | void processOFBarrierReply(OFChannelHandler h, OFBarrierReply m) |
| 1147 | throws IOException { |
| 1148 | // Silently ignore. |
| 1149 | } |
| 1150 | |
| 1151 | void processOFEchoRequest(OFChannelHandler h, OFEchoRequest m) |
| 1152 | throws IOException { |
| 1153 | if (h.ofVersion == null) { |
| 1154 | log.error("No OF version set for {}. Not sending Echo REPLY", |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1155 | h.channel.remoteAddress()); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1156 | return; |
| 1157 | } |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1158 | OFEchoReply reply = h.factory |
| 1159 | .buildEchoReply() |
| 1160 | .setXid(m.getXid()) |
| 1161 | .setData(m.getData()) |
| 1162 | .build(); |
| 1163 | h.channel.writeAndFlush(Collections.singletonList(reply)); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | void processOFEchoReply(OFChannelHandler h, OFEchoReply m) |
| 1167 | throws IOException { |
| 1168 | // Do nothing with EchoReplies !! |
| 1169 | } |
| 1170 | |
| 1171 | // no default implementation for OFError |
| 1172 | // every state must override it |
| 1173 | abstract void processOFError(OFChannelHandler h, OFErrorMsg m) |
| 1174 | throws IOException, SwitchStateException; |
| 1175 | |
| 1176 | |
| 1177 | void processOFFeaturesReply(OFChannelHandler h, OFFeaturesReply m) |
| 1178 | throws IOException, SwitchStateException { |
| 1179 | unhandledMessageReceived(h, m); |
| 1180 | } |
| 1181 | |
| 1182 | void processOFFlowRemoved(OFChannelHandler h, OFFlowRemoved m) |
| 1183 | throws IOException { |
| 1184 | unhandledMessageReceived(h, m); |
| 1185 | } |
| 1186 | |
| 1187 | void processOFGetConfigReply(OFChannelHandler h, OFGetConfigReply m) |
| 1188 | throws IOException, SwitchStateException { |
| 1189 | // we only expect config replies in the WAIT_CONFIG_REPLY state |
| 1190 | illegalMessageReceived(h, m); |
| 1191 | } |
| 1192 | |
| 1193 | void processOFPacketIn(OFChannelHandler h, OFPacketIn m) |
| 1194 | throws IOException { |
| 1195 | unhandledMessageReceived(h, m); |
| 1196 | } |
| 1197 | |
| 1198 | // no default implementation. Every state needs to handle it. |
| 1199 | abstract void processOFPortStatus(OFChannelHandler h, OFPortStatus m) |
| 1200 | throws IOException, SwitchStateException; |
| 1201 | |
| 1202 | void processOFQueueGetConfigReply(OFChannelHandler h, |
| 1203 | OFQueueGetConfigReply m) |
| 1204 | throws IOException { |
| 1205 | unhandledMessageReceived(h, m); |
| 1206 | } |
| 1207 | |
| 1208 | void processOFStatisticsReply(OFChannelHandler h, OFStatsReply m) |
| 1209 | throws IOException, SwitchStateException { |
| 1210 | unhandledMessageReceived(h, m); |
| 1211 | } |
| 1212 | |
| 1213 | void processOFExperimenter(OFChannelHandler h, OFExperimenter m) |
| 1214 | throws IOException, SwitchStateException { |
| 1215 | // TODO: it might make sense to parse the vendor message here |
| 1216 | // into the known vendor messages we support and then call more |
| 1217 | // specific event handlers |
| 1218 | unhandledMessageReceived(h, m); |
| 1219 | } |
| 1220 | |
| 1221 | void processOFRoleReply(OFChannelHandler h, OFRoleReply m) |
| 1222 | throws SwitchStateException, IOException { |
| 1223 | unhandledMessageReceived(h, m); |
| 1224 | } |
| 1225 | |
| 1226 | void processOFGetAsyncReply(OFChannelHandler h, |
| 1227 | OFAsyncGetReply m) { |
| 1228 | unhandledMessageReceived(h, m); |
| 1229 | } |
| 1230 | |
Yuta HIGUCHI | 1745e5a | 2017-01-15 21:43:02 -0800 | [diff] [blame] | 1231 | void processIdle(OFChannelHandler h) throws IOException { |
| 1232 | // disconnect channel which did no complete handshake |
| 1233 | log.error("{} idle in state {}, disconnecting", h.getSwitchInfoString(), this); |
| 1234 | h.channel.disconnect(); |
| 1235 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | |
| 1239 | |
| 1240 | //************************* |
| 1241 | // Channel handler methods |
| 1242 | //************************* |
| 1243 | |
| 1244 | @Override |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1245 | public void channelActive(ChannelHandlerContext ctx) |
| 1246 | throws Exception { |
| 1247 | |
| 1248 | channel = ctx.channel(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1249 | log.info("New switch connection from {}", |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1250 | channel.remoteAddress()); |
| 1251 | |
| 1252 | SocketAddress address = channel.remoteAddress(); |
| 1253 | if (address instanceof InetSocketAddress) { |
| 1254 | final InetSocketAddress inetAddress = (InetSocketAddress) address; |
| 1255 | final IpAddress ipAddress = IpAddress.valueOf(inetAddress.getAddress()); |
| 1256 | if (ipAddress.isIp4()) { |
| 1257 | channelId = ipAddress.toString() + ':' + inetAddress.getPort(); |
| 1258 | } else { |
| 1259 | channelId = '[' + ipAddress.toString() + "]:" + inetAddress.getPort(); |
| 1260 | } |
| 1261 | } else { |
| 1262 | channelId = channel.toString(); |
| 1263 | } |
| 1264 | |
| 1265 | dispatcher = Executors.newSingleThreadExecutor(groupedThreads("onos/of/dispatcher", channelId, log)); |
| 1266 | |
alshabib | 70fc7fb | 2015-01-06 11:04:29 -0800 | [diff] [blame] | 1267 | /* |
| 1268 | hack to wait for the switch to tell us what it's |
| 1269 | max version is. This is not spec compliant and should |
| 1270 | be removed as soon as switches behave better. |
| 1271 | */ |
| 1272 | //sendHandshakeHelloMessage(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1273 | setState(ChannelState.WAIT_HELLO); |
| 1274 | } |
| 1275 | |
| 1276 | @Override |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1277 | public void channelInactive(ChannelHandlerContext ctx) |
| 1278 | throws Exception { |
| 1279 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1280 | log.info("Switch disconnected callback for sw:{}. Cleaning up ...", |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1281 | getSwitchInfoString()); |
| 1282 | |
| 1283 | if (dispatcher != null) { |
Harold Huang | 828cd59 | 2017-11-04 10:46:04 +0800 | [diff] [blame] | 1284 | dispatcher.shutdownNow(); |
Thomas Vachuska | d75684a | 2018-01-03 09:04:47 -0800 | [diff] [blame] | 1285 | dispatcher = null; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1286 | } |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1287 | |
| 1288 | if (thisdpid != 0) { |
| 1289 | if (!duplicateDpidFound) { |
| 1290 | // if the disconnected switch (on this ChannelHandler) |
| 1291 | // was not one with a duplicate-dpid, it is safe to remove all |
| 1292 | // state for it at the controller. Notice that if the disconnected |
| 1293 | // switch was a duplicate-dpid, calling the method below would clear |
| 1294 | // all state for the original switch (with the same dpid), |
| 1295 | // which we obviously don't want. |
| 1296 | log.info("{}:removal called", getSwitchInfoString()); |
| 1297 | if (sw != null) { |
| 1298 | sw.removeConnectedSwitch(); |
| 1299 | } |
| 1300 | } else { |
| 1301 | // A duplicate was disconnected on this ChannelHandler, |
| 1302 | // this is the same switch reconnecting, but the original state was |
| 1303 | // not cleaned up - XXX check liveness of original ChannelHandler |
| 1304 | log.info("{}:duplicate found", getSwitchInfoString()); |
| 1305 | duplicateDpidFound = Boolean.FALSE; |
| 1306 | } |
| 1307 | } else { |
| 1308 | log.warn("no dpid in channelHandler registered for " |
| 1309 | + "disconnected switch {}", getSwitchInfoString()); |
| 1310 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | @Override |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1314 | public void exceptionCaught(ChannelHandlerContext ctx, |
| 1315 | Throwable cause) |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1316 | throws Exception { |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1317 | |
| 1318 | if (cause instanceof ReadTimeoutException) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1319 | // switch timeout |
| 1320 | log.error("Disconnecting switch {} due to read timeout", |
| 1321 | getSwitchInfoString()); |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1322 | ctx.channel().close(); |
| 1323 | } else if (cause instanceof HandshakeTimeoutException) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1324 | log.error("Disconnecting switch {}: failed to complete handshake", |
| 1325 | getSwitchInfoString()); |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1326 | ctx.channel().close(); |
| 1327 | } else if (cause instanceof ClosedChannelException) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1328 | log.debug("Channel for sw {} already closed", getSwitchInfoString()); |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1329 | } else if (cause instanceof IOException) { |
| 1330 | if (!cause.getMessage().equals(RESET_BY_PEER) && |
| 1331 | !cause.getMessage().equals(BROKEN_PIPE)) { |
Thomas Vachuska | e9af1f4 | 2015-07-06 08:42:18 -0700 | [diff] [blame] | 1332 | log.error("Disconnecting switch {} due to IO Error: {}", |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1333 | getSwitchInfoString(), cause.getMessage()); |
Thomas Vachuska | e9af1f4 | 2015-07-06 08:42:18 -0700 | [diff] [blame] | 1334 | if (log.isDebugEnabled()) { |
| 1335 | // still print stack trace if debug is enabled |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1336 | log.debug("StackTrace for previous Exception: ", cause); |
Thomas Vachuska | e9af1f4 | 2015-07-06 08:42:18 -0700 | [diff] [blame] | 1337 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1338 | } |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1339 | ctx.channel().close(); |
| 1340 | } else if (cause instanceof SwitchStateException) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1341 | log.error("Disconnecting switch {} due to switch state error: {}", |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1342 | getSwitchInfoString(), cause.getMessage()); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1343 | if (log.isDebugEnabled()) { |
| 1344 | // still print stack trace if debug is enabled |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1345 | log.debug("StackTrace for previous Exception: ", cause); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1346 | } |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1347 | ctx.channel().close(); |
| 1348 | } else if (cause instanceof OFParseError) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1349 | log.error("Disconnecting switch " |
| 1350 | + getSwitchInfoString() + |
| 1351 | " due to message parse failure", |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1352 | cause); |
| 1353 | ctx.channel().close(); |
| 1354 | } else if (cause instanceof RejectedExecutionException) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1355 | log.warn("Could not process message: queue full"); |
| 1356 | } else { |
| 1357 | log.error("Error while processing message from switch " |
| 1358 | + getSwitchInfoString() |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1359 | + "state " + this.state, cause); |
| 1360 | ctx.channel().close(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | @Override |
| 1365 | public String toString() { |
| 1366 | return getSwitchInfoString(); |
| 1367 | } |
| 1368 | |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 1369 | private void channelIdle(ChannelHandlerContext ctx, |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1370 | IdleStateEvent e) |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 1371 | throws IOException { |
Charles Chan | 982d390 | 2018-03-21 14:58:53 -0700 | [diff] [blame] | 1372 | // Factory can be null if the channel goes idle during initial handshake. Since the switch |
| 1373 | // is not even initialized properly, we just skip this and disconnect the channel. |
| 1374 | if (factory != null) { |
| 1375 | OFMessage m = factory.buildEchoRequest().build(); |
| 1376 | log.debug("Sending Echo Request on idle channel: {}", ctx.channel()); |
pier | 6fe5a80 | 2019-10-24 16:53:25 +0200 | [diff] [blame^] | 1377 | ctx.writeAndFlush(Collections.singletonList(m), ctx.voidPromise()); |
Charles Chan | 982d390 | 2018-03-21 14:58:53 -0700 | [diff] [blame] | 1378 | // XXX S some problems here -- echo request has no transaction id, and |
| 1379 | // echo reply is not correlated to the echo request. |
| 1380 | } |
Yuta HIGUCHI | 1745e5a | 2017-01-15 21:43:02 -0800 | [diff] [blame] | 1381 | state.processIdle(this); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | @Override |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1385 | public void userEventTriggered(ChannelHandlerContext ctx, |
| 1386 | Object evt) |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1387 | throws Exception { |
pier | 6fe5a80 | 2019-10-24 16:53:25 +0200 | [diff] [blame^] | 1388 | // If the connection is READER/WRITER idle try to send an echo request |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1389 | if (evt instanceof IdleStateEvent) { |
pier | 6fe5a80 | 2019-10-24 16:53:25 +0200 | [diff] [blame^] | 1390 | log.info("Channel {} is {}", ctx.channel(), ((IdleStateEvent) evt).state()); |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1391 | channelIdle(ctx, (IdleStateEvent) evt); |
pier | 6fe5a80 | 2019-10-24 16:53:25 +0200 | [diff] [blame^] | 1392 | } else { |
| 1393 | super.userEventTriggered(ctx, evt); |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1394 | } |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | // SimpleChannelInboundHandler without dependency to TypeParameterMatcher |
| 1398 | @Override |
| 1399 | public void channelRead(ChannelHandlerContext ctx, |
| 1400 | Object msg) throws Exception { |
| 1401 | |
| 1402 | boolean release = true; |
| 1403 | try { |
| 1404 | if (msg instanceof OFMessage) { |
| 1405 | // channelRead0 inlined |
| 1406 | state.processOFMessage(this, (OFMessage) msg); |
| 1407 | } else { |
| 1408 | release = false; |
| 1409 | ctx.fireChannelRead(msg); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1410 | } |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1411 | } finally { |
| 1412 | if (release) { |
| 1413 | ReferenceCountUtil.release(msg); |
| 1414 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | |
| 1419 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1420 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1421 | //************************* |
| 1422 | // Channel utility methods |
| 1423 | //************************* |
| 1424 | |
| 1425 | /** |
| 1426 | * Is this a state in which the handshake has completed? |
| 1427 | * @return true if the handshake is complete |
| 1428 | */ |
| 1429 | public boolean isHandshakeComplete() { |
| 1430 | return this.state.isHandshakeComplete(); |
| 1431 | } |
| 1432 | |
| 1433 | private void dispatchMessage(OFMessage m) { |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1434 | |
| 1435 | if (dispatchBacklog.isEmpty()) { |
| 1436 | if (!dispatchQueue.offer(m)) { |
| 1437 | // queue full |
| 1438 | channel.config().setAutoRead(false); |
| 1439 | // put it on the head of backlog |
| 1440 | dispatchBacklog.addFirst(m); |
| 1441 | return; |
| 1442 | } |
| 1443 | } else { |
| 1444 | dispatchBacklog.addLast(m); |
| 1445 | } |
| 1446 | |
| 1447 | while (!dispatchBacklog.isEmpty()) { |
| 1448 | OFMessage msg = dispatchBacklog.pop(); |
| 1449 | |
| 1450 | if (!dispatchQueue.offer(msg)) { |
| 1451 | // queue full |
| 1452 | channel.config().setAutoRead(false); |
| 1453 | // put it back to the head of backlog |
| 1454 | dispatchBacklog.addFirst(msg); |
| 1455 | return; |
| 1456 | } |
| 1457 | } |
| 1458 | |
| 1459 | |
| 1460 | if (dispatcherHandle.isDone()) { |
| 1461 | // dispatcher terminated for some reason, restart |
| 1462 | |
Yuta HIGUCHI | fbd9ae9 | 2018-01-24 23:39:06 -0800 | [diff] [blame] | 1463 | dispatcherHandle = dispatcher.submit((Runnable) () -> { |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1464 | try { |
| 1465 | List<OFMessage> msgs = new ArrayList<>(); |
| 1466 | for (;;) { |
| 1467 | // wait for new message |
| 1468 | OFMessage msg = dispatchQueue.take(); |
| 1469 | sw.handleMessage(msg); |
| 1470 | |
| 1471 | while (dispatchQueue.drainTo(msgs, MSG_READ_BUFFER) > 0) { |
| 1472 | if (!channel.config().isAutoRead()) { |
| 1473 | channel.config().setAutoRead(true); |
| 1474 | } |
| 1475 | msgs.forEach(sw::handleMessage); |
| 1476 | msgs.clear(); |
| 1477 | } |
| 1478 | |
| 1479 | if (!channel.config().isAutoRead()) { |
| 1480 | channel.config().setAutoRead(true); |
| 1481 | } |
| 1482 | } |
| 1483 | } catch (InterruptedException e) { |
| 1484 | Thread.currentThread().interrupt(); |
| 1485 | // interrupted. gracefully shutting down |
| 1486 | return; |
| 1487 | } |
| 1488 | |
| 1489 | }); |
| 1490 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | /** |
| 1494 | * Return a string describing this switch based on the already available |
| 1495 | * information (DPID and/or remote socket). |
| 1496 | * @return display string |
| 1497 | */ |
| 1498 | private String getSwitchInfoString() { |
| 1499 | if (sw != null) { |
| 1500 | return sw.toString(); |
| 1501 | } |
| 1502 | String channelString; |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1503 | if (channel == null || channel.remoteAddress() == null) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1504 | channelString = "?"; |
| 1505 | } else { |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1506 | channelString = channel.remoteAddress().toString(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1507 | } |
| 1508 | String dpidString; |
| 1509 | if (featuresReply == null) { |
| 1510 | dpidString = "?"; |
| 1511 | } else { |
| 1512 | dpidString = featuresReply.getDatapathId().toString(); |
| 1513 | } |
| 1514 | return String.format("[%s DPID[%s]]", channelString, dpidString); |
| 1515 | } |
| 1516 | |
| 1517 | /** |
| 1518 | * Update the channels state. Only called from the state machine. |
| 1519 | * TODO: enforce restricted state transitions |
pier | 7f292e7 | 2019-07-16 15:52:50 +0200 | [diff] [blame] | 1520 | * @param state new state |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1521 | */ |
pier | 7f292e7 | 2019-07-16 15:52:50 +0200 | [diff] [blame] | 1522 | void setState(ChannelState state) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1523 | this.state = state; |
Yuta HIGUCHI | 10f4513 | 2017-03-01 17:09:32 -0800 | [diff] [blame] | 1524 | this.lastStateChange = System.currentTimeMillis(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1525 | } |
| 1526 | |
Brian O'Connor | 47a1609 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 1527 | private boolean setDpid(Long dpid, Channel channel) { |
| 1528 | ChannelHandlerContext sslContext = channel.pipeline().context(SslHandler.class); |
| 1529 | if (sslContext != null) { |
| 1530 | try { |
| 1531 | SslHandler sslHandler = (SslHandler) sslContext.handler(); |
| 1532 | Certificate[] certs = sslHandler.engine().getSession().getPeerCertificates(); |
| 1533 | Certificate cert = certs.length > 0 ? certs[0] : null; |
| 1534 | if (!controller.isValidCertificate(dpid, cert)) { |
| 1535 | return false; |
| 1536 | } |
| 1537 | } catch (SSLPeerUnverifiedException e) { |
| 1538 | log.info("Switch with dpid {} is an unverified SSL peer.", dpid, e); |
| 1539 | return false; |
| 1540 | } |
| 1541 | } |
| 1542 | this.thisdpid = dpid; |
| 1543 | return true; |
| 1544 | } |
| 1545 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1546 | /** |
| 1547 | * Send hello message to the switch using the handshake transactions ids. |
| 1548 | * @throws IOException |
| 1549 | */ |
| 1550 | private void sendHandshakeHelloMessage() throws IOException { |
| 1551 | // The OF protocol requires us to start things off by sending the highest |
| 1552 | // version of the protocol supported. |
| 1553 | |
Yuta HIGUCHI | 6512f3e | 2017-05-18 17:21:24 -0700 | [diff] [blame] | 1554 | // bitmap represents OF1.0, OF1.3, OF1.4, and OF1.5 |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1555 | // see Sec. 7.5.1 of the OF1.3.4 spec |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 1556 | U32 bitmap = U32.ofRaw((0b1 << OFVersion.OF_10.getWireVersion()) | |
| 1557 | (0b1 << OFVersion.OF_13.getWireVersion()) | |
Yuta HIGUCHI | 6512f3e | 2017-05-18 17:21:24 -0700 | [diff] [blame] | 1558 | (0b1 << OFVersion.OF_14.getWireVersion()) | |
| 1559 | (0b1 << OFVersion.OF_15.getWireVersion())); |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 1560 | OFVersion version = Optional.ofNullable(ofVersion).orElse(OFVersion.OF_13); |
| 1561 | OFHelloElem hem = OFFactories.getFactory(version) |
| 1562 | .buildHelloElemVersionbitmap() |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1563 | .setBitmaps(Collections.singletonList(bitmap)) |
| 1564 | .build(); |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 1565 | OFMessage.Builder mb = OFFactories.getFactory(version) |
| 1566 | .buildHello() |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1567 | .setXid(this.handshakeTransactionIds--) |
| 1568 | .setElements(Collections.singletonList(hem)); |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1569 | log.info("Sending {} Hello to {}", version, channel.remoteAddress()); |
| 1570 | channel.writeAndFlush(Collections.singletonList(mb.build())); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | /** |
| 1574 | * Send featuresRequest msg to the switch using the handshake transactions ids. |
| 1575 | * @throws IOException |
| 1576 | */ |
| 1577 | private void sendHandshakeFeaturesRequestMessage() throws IOException { |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1578 | log.debug("Sending FEATURES_REQUEST to {}", channel.remoteAddress()); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1579 | OFMessage m = factory.buildFeaturesRequest() |
| 1580 | .setXid(this.handshakeTransactionIds--) |
| 1581 | .build(); |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1582 | channel.writeAndFlush(Collections.singletonList(m)); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | /** |
| 1586 | * Send the configuration requests to tell the switch we want full |
| 1587 | * packets. |
| 1588 | * @throws IOException |
| 1589 | */ |
| 1590 | private void sendHandshakeSetConfig() throws IOException { |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1591 | log.debug("Sending CONFIG_REQUEST to {}", channel.remoteAddress()); |
Yuta HIGUCHI | 605758e | 2017-01-13 20:26:44 -0800 | [diff] [blame] | 1592 | List<OFMessage> msglist = new ArrayList<>(3); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1593 | |
| 1594 | // Ensure we receive the full packet via PacketIn |
| 1595 | // FIXME: We don't set the reassembly flags. |
Yuta HIGUCHI | 605758e | 2017-01-13 20:26:44 -0800 | [diff] [blame] | 1596 | // Only send config to switches to send full packets, if they have a buffer. |
Michael Jarschel | 7f521a3 | 2015-08-12 16:31:07 +0200 | [diff] [blame] | 1597 | // Saves a packet & OFSetConfig can't be handled by certain switches. |
Yuta HIGUCHI | 605758e | 2017-01-13 20:26:44 -0800 | [diff] [blame] | 1598 | if (this.featuresReply.getNBuffers() > 0) { |
Michael Jarschel | 7f521a3 | 2015-08-12 16:31:07 +0200 | [diff] [blame] | 1599 | OFSetConfig sc = factory |
| 1600 | .buildSetConfig() |
| 1601 | .setMissSendLen((short) 0xffff) |
| 1602 | .setXid(this.handshakeTransactionIds--) |
| 1603 | .build(); |
| 1604 | msglist.add(sc); |
| 1605 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1606 | |
| 1607 | // Barrier |
| 1608 | OFBarrierRequest br = factory |
| 1609 | .buildBarrierRequest() |
| 1610 | .setXid(this.handshakeTransactionIds--) |
| 1611 | .build(); |
| 1612 | msglist.add(br); |
| 1613 | |
| 1614 | // Verify (need barrier?) |
| 1615 | OFGetConfigRequest gcr = factory |
| 1616 | .buildGetConfigRequest() |
| 1617 | .setXid(this.handshakeTransactionIds--) |
| 1618 | .build(); |
| 1619 | msglist.add(gcr); |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1620 | channel.writeAndFlush(msglist); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | /** |
| 1624 | * send a description state request. |
| 1625 | * @throws IOException |
| 1626 | */ |
| 1627 | private void sendHandshakeDescriptionStatsRequest() throws IOException { |
| 1628 | // Get Description to set switch-specific flags |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1629 | log.debug("Sending DESC_STATS_REQUEST to {}", channel.remoteAddress()); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1630 | OFDescStatsRequest dreq = factory |
| 1631 | .buildDescStatsRequest() |
| 1632 | .setXid(handshakeTransactionIds--) |
| 1633 | .build(); |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1634 | channel.writeAndFlush(Collections.singletonList(dreq)); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1635 | } |
| 1636 | |
Jordi Ortiz | 91477b8 | 2016-11-29 15:22:50 +0100 | [diff] [blame] | 1637 | /** |
Yuta HIGUCHI | 605758e | 2017-01-13 20:26:44 -0800 | [diff] [blame] | 1638 | * send a meter features request. |
| 1639 | * |
Jordi Ortiz | 91477b8 | 2016-11-29 15:22:50 +0100 | [diff] [blame] | 1640 | * @throws IOException |
| 1641 | */ |
| 1642 | private void sendMeterFeaturesRequest() throws IOException { |
| 1643 | // Get meter features including the MaxMeters value available for the device |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 1644 | OFFactory factory = OFFactories.getFactory(ofVersion); |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1645 | log.debug("Sending METER_FEATURES_REQUEST to {}", channel.remoteAddress()); |
Jordi Ortiz | 91477b8 | 2016-11-29 15:22:50 +0100 | [diff] [blame] | 1646 | OFMeterFeaturesStatsRequest mfreq = factory |
| 1647 | .buildMeterFeaturesStatsRequest() |
| 1648 | .setXid(handshakeTransactionIds--) |
| 1649 | .build(); |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1650 | channel.writeAndFlush(Collections.singletonList(mfreq)); |
Jordi Ortiz | 91477b8 | 2016-11-29 15:22:50 +0100 | [diff] [blame] | 1651 | } |
| 1652 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1653 | private void sendHandshakeOFPortDescRequest() throws IOException { |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1654 | log.debug("Sending OF_PORT_DESC_REQUEST to {}", channel.remoteAddress()); |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 1655 | // Get port description for 1.3+ switch |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1656 | OFPortDescStatsRequest preq = factory |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1657 | .buildPortDescStatsRequest() |
| 1658 | .setXid(handshakeTransactionIds--) |
| 1659 | .build(); |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1660 | channel.writeAndFlush(Collections.singletonList(preq)); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | ChannelState getStateForTesting() { |
| 1664 | return state; |
| 1665 | } |
| 1666 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1667 | |
| 1668 | @Override |
| 1669 | public boolean isActive() { |
| 1670 | if (channel != null) { |
| 1671 | return channel.isActive(); |
| 1672 | } |
| 1673 | return false; |
| 1674 | } |
| 1675 | |
| 1676 | @Override |
| 1677 | public void closeSession() { |
| 1678 | if (channel != null) { |
| 1679 | channel.close(); |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | @Override |
| 1684 | public boolean sendMsg(Iterable<OFMessage> msgs) { |
| 1685 | if (channel.isActive()) { |
Laszlo Papp | b68fe7e | 2017-11-24 17:06:59 +0000 | [diff] [blame] | 1686 | if (log.isTraceEnabled()) { |
| 1687 | log.trace("Sending messages for switch {} via openflow channel: {}", getSwitchInfoString(), msgs); |
| 1688 | } |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 1689 | channel.writeAndFlush(msgs, channel.voidPromise()); |
| 1690 | return true; |
| 1691 | } else { |
| 1692 | log.warn("Dropping messages for switch {} because channel is not connected: {}", |
| 1693 | getSwitchInfoString(), msgs); |
| 1694 | return false; |
| 1695 | } |
| 1696 | } |
| 1697 | |
| 1698 | @Override |
| 1699 | public CharSequence sessionInfo() { |
| 1700 | return channelId; |
| 1701 | } |
| 1702 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1703 | } |