blob: 71312423e9e990c7adc0e85eaf29d5f86c23f2ad [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
16
tom7ef8ff92014-09-17 13:08:06 -070017//CHECKSTYLE:OFF
Brian O'Connorabafb502014-12-02 22:26:20 -080018package org.onosproject.openflow.controller.impl;
tom7ef8ff92014-09-17 13:08:06 -070019
20import java.io.IOException;
21import java.nio.channels.ClosedChannelException;
22import java.util.ArrayList;
23import java.util.Collections;
24import java.util.List;
25import java.util.concurrent.CopyOnWriteArrayList;
26import java.util.concurrent.RejectedExecutionException;
27
28import org.jboss.netty.channel.Channel;
29import org.jboss.netty.channel.ChannelHandlerContext;
30import org.jboss.netty.channel.ChannelStateEvent;
31import org.jboss.netty.channel.ExceptionEvent;
32import org.jboss.netty.channel.MessageEvent;
33import org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler;
34import org.jboss.netty.handler.timeout.IdleStateEvent;
35import org.jboss.netty.handler.timeout.ReadTimeoutException;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver;
37import org.onosproject.openflow.controller.driver.SwitchStateException;
tom7ef8ff92014-09-17 13:08:06 -070038import org.projectfloodlight.openflow.exceptions.OFParseError;
39import org.projectfloodlight.openflow.protocol.OFAsyncGetReply;
40import org.projectfloodlight.openflow.protocol.OFBadRequestCode;
41import org.projectfloodlight.openflow.protocol.OFBarrierReply;
42import org.projectfloodlight.openflow.protocol.OFBarrierRequest;
43import org.projectfloodlight.openflow.protocol.OFDescStatsReply;
44import org.projectfloodlight.openflow.protocol.OFDescStatsRequest;
45import org.projectfloodlight.openflow.protocol.OFEchoReply;
46import org.projectfloodlight.openflow.protocol.OFEchoRequest;
47import org.projectfloodlight.openflow.protocol.OFErrorMsg;
48import org.projectfloodlight.openflow.protocol.OFErrorType;
49import org.projectfloodlight.openflow.protocol.OFExperimenter;
50import org.projectfloodlight.openflow.protocol.OFFactory;
51import org.projectfloodlight.openflow.protocol.OFFeaturesReply;
52import org.projectfloodlight.openflow.protocol.OFFlowModFailedCode;
53import org.projectfloodlight.openflow.protocol.OFFlowRemoved;
54import org.projectfloodlight.openflow.protocol.OFGetConfigReply;
55import org.projectfloodlight.openflow.protocol.OFGetConfigRequest;
56import org.projectfloodlight.openflow.protocol.OFHello;
57import org.projectfloodlight.openflow.protocol.OFHelloElem;
58import org.projectfloodlight.openflow.protocol.OFMessage;
59import org.projectfloodlight.openflow.protocol.OFPacketIn;
60import org.projectfloodlight.openflow.protocol.OFPortDescStatsReply;
61import org.projectfloodlight.openflow.protocol.OFPortDescStatsRequest;
62import org.projectfloodlight.openflow.protocol.OFPortStatus;
63import org.projectfloodlight.openflow.protocol.OFQueueGetConfigReply;
64import org.projectfloodlight.openflow.protocol.OFRoleReply;
65import org.projectfloodlight.openflow.protocol.OFSetConfig;
66import org.projectfloodlight.openflow.protocol.OFStatsReply;
67import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags;
68import org.projectfloodlight.openflow.protocol.OFStatsType;
69import org.projectfloodlight.openflow.protocol.OFType;
70import org.projectfloodlight.openflow.protocol.OFVersion;
71import org.projectfloodlight.openflow.protocol.errormsg.OFBadRequestErrorMsg;
72import org.projectfloodlight.openflow.protocol.errormsg.OFFlowModFailedErrorMsg;
73import org.projectfloodlight.openflow.types.U32;
74import org.slf4j.Logger;
75import org.slf4j.LoggerFactory;
76
77/**
78 * Channel handler deals with the switch connection and dispatches
79 * switch messages to the appropriate locations.
80 */
81class OFChannelHandler extends IdleStateAwareChannelHandler {
82 private static final Logger log = LoggerFactory.getLogger(OFChannelHandler.class);
Thomas Vachuskae9af1f42015-07-06 08:42:18 -070083
84 private static final String RESET_BY_PEER = "Connection reset by peer";
85 private static final String BROKEN_PIPE = "Broken pipe";
86
tom7ef8ff92014-09-17 13:08:06 -070087 private final Controller controller;
88 private OpenFlowSwitchDriver sw;
89 private long thisdpid; // channelHandler cached value of connected switch id
90 private Channel channel;
91 // State needs to be volatile because the HandshakeTimeoutHandler
92 // needs to check if the handshake is complete
93 private volatile ChannelState state;
94
95 // When a switch with a duplicate dpid is found (i.e we already have a
96 // connected switch with the same dpid), the new switch is immediately
97 // disconnected. At that point netty callsback channelDisconnected() which
98 // proceeds to cleaup switch state - we need to ensure that it does not cleanup
99 // switch state for the older (still connected) switch
100 private volatile Boolean duplicateDpidFound;
101
102 // Temporary storage for switch-features and port-description
103 private OFFeaturesReply featuresReply;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700104 private List<OFPortDescStatsReply> portDescReplies;
105 //private OFPortDescStatsReply portDescReply;
tom7ef8ff92014-09-17 13:08:06 -0700106 // a concurrent ArrayList to temporarily store port status messages
107 // before we are ready to deal with them
108 private final CopyOnWriteArrayList<OFPortStatus> pendingPortStatusMsg;
109
110 //Indicates the openflow version used by this switch
111 protected OFVersion ofVersion;
112 protected OFFactory factory13;
113 protected OFFactory factory10;
114
115 /** transaction Ids to use during handshake. Since only one thread
116 * calls into an OFChannelHandler instance, we don't need atomic.
117 * We will count down
118 */
119 private int handshakeTransactionIds = -1;
120
121 /**
122 * Create a new unconnected OFChannelHandler.
Thomas Vachuskab14c77a2014-11-04 18:08:01 -0800123 * @param controller parent controller
tom7ef8ff92014-09-17 13:08:06 -0700124 */
125 OFChannelHandler(Controller controller) {
126 this.controller = controller;
127 this.state = ChannelState.INIT;
128 this.pendingPortStatusMsg = new CopyOnWriteArrayList<OFPortStatus>();
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700129 this.portDescReplies = new ArrayList<OFPortDescStatsReply>();
tom7ef8ff92014-09-17 13:08:06 -0700130 factory13 = controller.getOFMessageFactory13();
131 factory10 = controller.getOFMessageFactory10();
132 duplicateDpidFound = Boolean.FALSE;
133 }
134
135
136
137 // XXX S consider if necessary
138 public void disconnectSwitch() {
139 sw.disconnectSwitch();
140 }
141
142
143
144 //*************************
145 // Channel State Machine
146 //*************************
147
148 /**
149 * The state machine for handling the switch/channel state. All state
150 * transitions should happen from within the state machine (and not from other
151 * parts of the code)
152 */
153 enum ChannelState {
154 /**
155 * Initial state before channel is connected.
156 */
157 INIT(false) {
158 @Override
159 void processOFMessage(OFChannelHandler h, OFMessage m)
160 throws IOException, SwitchStateException {
161 illegalMessageReceived(h, m);
162 }
163
164 @Override
165 void processOFError(OFChannelHandler h, OFErrorMsg m)
166 throws IOException {
167 // need to implement since its abstract but it will never
168 // be called
169 }
170
171 @Override
172 void processOFPortStatus(OFChannelHandler h, OFPortStatus m)
173 throws IOException {
174 unhandledMessageReceived(h, m);
175 }
176 },
177
178 /**
179 * We send a OF 1.3 HELLO to the switch and wait for a Hello from the switch.
180 * Once we receive the reply, we decide on OF 1.3 or 1.0 switch - no other
181 * protocol version is accepted.
182 * We send an OFFeaturesRequest depending on the protocol version selected
183 * Next state is WAIT_FEATURES_REPLY
184 */
185 WAIT_HELLO(false) {
186 @Override
187 void processOFHello(OFChannelHandler h, OFHello m)
188 throws IOException {
189 // TODO We could check for the optional bitmap, but for now
190 // we are just checking the version number.
Chip Boling68bc6562015-07-06 10:00:01 -0500191 if (m.getVersion().getWireVersion() >= OFVersion.OF_13.getWireVersion()) {
192 log.debug("Received {} Hello from {} - switching to OF "
193 + "version 1.3", m.getVersion(),
tom7ef8ff92014-09-17 13:08:06 -0700194 h.channel.getRemoteAddress());
alshabib70fc7fb2015-01-06 11:04:29 -0800195 h.sendHandshakeHelloMessage();
tom7ef8ff92014-09-17 13:08:06 -0700196 h.ofVersion = OFVersion.OF_13;
Chip Boling68bc6562015-07-06 10:00:01 -0500197 } else if (m.getVersion().getWireVersion() >= OFVersion.OF_10.getWireVersion()) {
alshabib09d48be2014-10-03 15:43:33 -0700198 log.debug("Received {} Hello from {} - switching to OF "
tom7ef8ff92014-09-17 13:08:06 -0700199 + "version 1.0", m.getVersion(),
200 h.channel.getRemoteAddress());
alshabib70fc7fb2015-01-06 11:04:29 -0800201 OFHello hi =
202 h.factory10.buildHello()
203 .setXid(h.handshakeTransactionIds--)
204 .build();
205 h.channel.write(Collections.singletonList(hi));
tom7ef8ff92014-09-17 13:08:06 -0700206 h.ofVersion = OFVersion.OF_10;
207 } else {
208 log.error("Received Hello of version {} from switch at {}. "
209 + "This controller works with OF1.0 and OF1.3 "
210 + "switches. Disconnecting switch ...",
211 m.getVersion(), h.channel.getRemoteAddress());
212 h.channel.disconnect();
213 return;
214 }
215 h.sendHandshakeFeaturesRequestMessage();
216 h.setState(WAIT_FEATURES_REPLY);
217 }
218 @Override
219 void processOFFeaturesReply(OFChannelHandler h, OFFeaturesReply m)
220 throws IOException, SwitchStateException {
221 illegalMessageReceived(h, m);
222 }
223 @Override
224 void processOFStatisticsReply(OFChannelHandler h,
225 OFStatsReply m)
226 throws IOException, SwitchStateException {
227 illegalMessageReceived(h, m);
228 }
229 @Override
230 void processOFError(OFChannelHandler h, OFErrorMsg m) {
231 logErrorDisconnect(h, m);
232 }
233
234 @Override
235 void processOFPortStatus(OFChannelHandler h, OFPortStatus m)
236 throws IOException {
237 unhandledMessageReceived(h, m);
238 }
239 },
240
241
242 /**
243 * We are waiting for a features reply message. Once we receive it, the
244 * behavior depends on whether this is a 1.0 or 1.3 switch. For 1.0,
245 * we send a SetConfig request, barrier, and GetConfig request and the
246 * next state is WAIT_CONFIG_REPLY. For 1.3, we send a Port description
247 * request and the next state is WAIT_PORT_DESC_REPLY.
248 */
249 WAIT_FEATURES_REPLY(false) {
250 @Override
251 void processOFFeaturesReply(OFChannelHandler h, OFFeaturesReply m)
252 throws IOException {
253 h.thisdpid = m.getDatapathId().getLong();
alshabib09d48be2014-10-03 15:43:33 -0700254 log.debug("Received features reply for switch at {} with dpid {}",
tom7ef8ff92014-09-17 13:08:06 -0700255 h.getSwitchInfoString(), h.thisdpid);
256
257 h.featuresReply = m; //temp store
258 if (h.ofVersion == OFVersion.OF_10) {
259 h.sendHandshakeSetConfig();
260 h.setState(WAIT_CONFIG_REPLY);
261 } else {
262 //version is 1.3, must get switchport information
263 h.sendHandshakeOFPortDescRequest();
264 h.setState(WAIT_PORT_DESC_REPLY);
265 }
266 }
267 @Override
268 void processOFStatisticsReply(OFChannelHandler h,
269 OFStatsReply m)
270 throws IOException, SwitchStateException {
271 illegalMessageReceived(h, m);
272 }
273 @Override
274 void processOFError(OFChannelHandler h, OFErrorMsg m) {
275 logErrorDisconnect(h, m);
276 }
277
278 @Override
279 void processOFPortStatus(OFChannelHandler h, OFPortStatus m)
280 throws IOException {
Thomas Vachuska39274462014-12-02 13:23:50 -0800281 h.pendingPortStatusMsg.add(m);
tom7ef8ff92014-09-17 13:08:06 -0700282 }
283 },
284
285 /**
286 * We are waiting for a description of the 1.3 switch ports.
287 * Once received, we send a SetConfig request
288 * Next State is WAIT_CONFIG_REPLY
289 */
290 WAIT_PORT_DESC_REPLY(false) {
291
292 @Override
293 void processOFStatisticsReply(OFChannelHandler h, OFStatsReply m)
294 throws SwitchStateException {
295 // Read port description
296 if (m.getStatsType() != OFStatsType.PORT_DESC) {
297 log.warn("Expecting port description stats but received stats "
298 + "type {} from {}. Ignoring ...", m.getStatsType(),
299 h.channel.getRemoteAddress());
300 return;
301 }
302 if (m.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700303 log.debug("Stats reply indicates more stats from sw {} for "
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700304 + "port description",
tom7ef8ff92014-09-17 13:08:06 -0700305 h.getSwitchInfoString());
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700306 h.portDescReplies.add((OFPortDescStatsReply)m);
307 return;
tom7ef8ff92014-09-17 13:08:06 -0700308 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700309 else {
310 h.portDescReplies.add((OFPortDescStatsReply)m);
311 }
312 //h.portDescReply = (OFPortDescStatsReply) m; // temp store
tom7ef8ff92014-09-17 13:08:06 -0700313 log.info("Received port desc reply for switch at {}",
314 h.getSwitchInfoString());
315 try {
316 h.sendHandshakeSetConfig();
317 } catch (IOException e) {
318 log.error("Unable to send setConfig after PortDescReply. "
319 + "Error: {}", e.getMessage());
320 }
321 h.setState(WAIT_CONFIG_REPLY);
322 }
323
324 @Override
325 void processOFError(OFChannelHandler h, OFErrorMsg m)
326 throws IOException, SwitchStateException {
327 logErrorDisconnect(h, m);
328
329 }
330
331 @Override
332 void processOFPortStatus(OFChannelHandler h, OFPortStatus m)
333 throws IOException, SwitchStateException {
Thomas Vachuska39274462014-12-02 13:23:50 -0800334 h.pendingPortStatusMsg.add(m);
tom7ef8ff92014-09-17 13:08:06 -0700335
336 }
337 },
338
339 /**
340 * We are waiting for a config reply message. Once we receive it
341 * we send a DescriptionStatsRequest to the switch.
342 * Next state: WAIT_DESCRIPTION_STAT_REPLY
343 */
344 WAIT_CONFIG_REPLY(false) {
345 @Override
346 void processOFGetConfigReply(OFChannelHandler h, OFGetConfigReply m)
347 throws IOException {
348 if (m.getMissSendLen() == 0xffff) {
349 log.trace("Config Reply from switch {} confirms "
350 + "miss length set to 0xffff",
351 h.getSwitchInfoString());
352 } else {
353 // FIXME: we can't really deal with switches that don't send
354 // full packets. Shouldn't we drop the connection here?
355 log.warn("Config Reply from switch {} has"
356 + "miss length set to {}",
357 h.getSwitchInfoString(),
358 m.getMissSendLen());
359 }
360 h.sendHandshakeDescriptionStatsRequest();
361 h.setState(WAIT_DESCRIPTION_STAT_REPLY);
362 }
363
364 @Override
365 void processOFBarrierReply(OFChannelHandler h, OFBarrierReply m) {
366 // do nothing;
367 }
368
369 @Override
370 void processOFFeaturesReply(OFChannelHandler h, OFFeaturesReply m)
371 throws IOException, SwitchStateException {
372 illegalMessageReceived(h, m);
373 }
374 @Override
375 void processOFStatisticsReply(OFChannelHandler h,
376 OFStatsReply m)
377 throws IOException, SwitchStateException {
378 log.error("Received multipart(stats) message sub-type {}",
379 m.getStatsType());
380 illegalMessageReceived(h, m);
381 }
382
383 @Override
384 void processOFError(OFChannelHandler h, OFErrorMsg m) {
385 logErrorDisconnect(h, m);
386 }
387
388 @Override
389 void processOFPortStatus(OFChannelHandler h, OFPortStatus m)
390 throws IOException {
391 h.pendingPortStatusMsg.add(m);
392 }
393 },
394
395
396 /**
397 * We are waiting for a OFDescriptionStat message from the switch.
398 * Once we receive any stat message we try to parse it. If it's not
399 * a description stats message we disconnect. If its the expected
400 * description stats message, we:
401 * - use the switch driver to bind the switch and get an IOFSwitch instance
402 * - setup the IOFSwitch instance
403 * - add switch controller and send the initial role
404 * request to the switch.
405 * Next state: WAIT_INITIAL_ROLE
406 * In the typical case, where switches support role request messages
407 * the next state is where we expect the role reply message.
408 * In the special case that where the switch does not support any kind
409 * of role request messages, we don't send a role message, but we do
410 * request mastership from the registry service. This controller
411 * should become master once we hear back from the registry service.
412 * All following states will have a h.sw instance!
413 */
414 WAIT_DESCRIPTION_STAT_REPLY(false) {
415 @Override
416 void processOFStatisticsReply(OFChannelHandler h, OFStatsReply m)
417 throws SwitchStateException {
418 // Read description, if it has been updated
419 if (m.getStatsType() != OFStatsType.DESC) {
420 log.warn("Expecting Description stats but received stats "
421 + "type {} from {}. Ignoring ...", m.getStatsType(),
422 h.channel.getRemoteAddress());
423 return;
424 }
tom7ef8ff92014-09-17 13:08:06 -0700425 OFDescStatsReply drep = (OFDescStatsReply) m;
Saurav Dasf9ba4222015-05-07 17:13:59 -0700426 log.info("Received switch description reply {} from switch at {}",
427 drep, h.channel.getRemoteAddress());
tom7ef8ff92014-09-17 13:08:06 -0700428 // Here is where we differentiate between different kinds of switches
429 h.sw = h.controller.getOFSwitchInstance(h.thisdpid, drep, h.ofVersion);
430
431 h.sw.setOFVersion(h.ofVersion);
432 h.sw.setFeaturesReply(h.featuresReply);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700433 //h.sw.setPortDescReply(h.portDescReply);
434 h.sw.setPortDescReplies(h.portDescReplies);
tom7ef8ff92014-09-17 13:08:06 -0700435 h.sw.setConnected(true);
436 h.sw.setChannel(h.channel);
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700437// boolean success = h.sw.connectSwitch();
438//
439// if (!success) {
440// disconnectDuplicate(h);
441// return;
442// }
tom7ef8ff92014-09-17 13:08:06 -0700443 // set switch information
444
445
446
alshabib09d48be2014-10-03 15:43:33 -0700447 log.debug("Switch {} bound to class {}, description {}",
Ray Milkey6bc43c22015-11-06 13:22:38 -0800448 h.sw, h.sw.getClass(), drep);
tom7ef8ff92014-09-17 13:08:06 -0700449 //Put switch in EQUAL mode until we hear back from the global registry
450 //log.debug("Setting new switch {} to EQUAL and sending Role request",
451 // h.sw.getStringId());
452 //h.sw.activateEqualSwitch();
453 //h.setSwitchRole(RoleState.EQUAL);
454
455 h.sw.startDriverHandshake();
alshabib9eab22f2014-10-20 17:17:31 -0700456 if (h.sw.isDriverHandshakeComplete()) {
457 if (!h.sw.connectSwitch()) {
458 disconnectDuplicate(h);
459 }
Thomas Vachuska39274462014-12-02 13:23:50 -0800460 handlePendingPortStatusMessages(h);
alshabib9eab22f2014-10-20 17:17:31 -0700461 h.setState(ACTIVE);
462 } else {
463 h.setState(WAIT_SWITCH_DRIVER_SUB_HANDSHAKE);
464 }
tom7ef8ff92014-09-17 13:08:06 -0700465
466 }
467
468 @Override
469 void processOFError(OFChannelHandler h, OFErrorMsg m) {
470 logErrorDisconnect(h, m);
471 }
472
473 @Override
474 void processOFFeaturesReply(OFChannelHandler h, OFFeaturesReply m)
475 throws IOException, SwitchStateException {
476 illegalMessageReceived(h, m);
477 }
478
479 @Override
480 void processOFPortStatus(OFChannelHandler h, OFPortStatus m)
481 throws IOException {
482 h.pendingPortStatusMsg.add(m);
483 }
484 },
485
486
487 /**
488 * We are waiting for the respective switch driver to complete its
489 * configuration. Notice that we do not consider this to be part of the main
490 * switch-controller handshake. But we do consider it as a step that comes
491 * before we declare the switch as available to the controller.
492 * Next State: depends on the role of this controller for this switch - either
493 * MASTER or EQUAL.
494 */
495 WAIT_SWITCH_DRIVER_SUB_HANDSHAKE(true) {
496
497 @Override
498 void processOFError(OFChannelHandler h, OFErrorMsg m)
499 throws IOException {
500 // will never be called. We override processOFMessage
501 }
502
alshabibd7963912014-10-20 14:52:04 -0700503
504
tom7ef8ff92014-09-17 13:08:06 -0700505 @Override
506 void processOFMessage(OFChannelHandler h, OFMessage m)
507 throws IOException, SwitchStateException {
alshabibd7963912014-10-20 14:52:04 -0700508
509 if (h.sw.isDriverHandshakeComplete()) {
510 moveToActive(h);
alshabib9eab22f2014-10-20 17:17:31 -0700511 h.state.processOFMessage(h, m);
512 return;
alshabibd7963912014-10-20 14:52:04 -0700513
514 }
515
tom7ef8ff92014-09-17 13:08:06 -0700516 if (m.getType() == OFType.ECHO_REQUEST) {
517 processOFEchoRequest(h, (OFEchoRequest) m);
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700518 } else if (m.getType() == OFType.ECHO_REPLY) {
519 processOFEchoReply(h, (OFEchoReply) m);
tom7ef8ff92014-09-17 13:08:06 -0700520 } else if (m.getType() == OFType.ROLE_REPLY) {
521 h.sw.handleRole(m);
522 } else if (m.getType() == OFType.ERROR) {
523 if (!h.sw.handleRoleError((OFErrorMsg)m)) {
524 h.sw.processDriverHandshakeMessage(m);
525 if (h.sw.isDriverHandshakeComplete()) {
alshabibd7963912014-10-20 14:52:04 -0700526 moveToActive(h);
tom7ef8ff92014-09-17 13:08:06 -0700527 }
528 }
529 } else {
530 if (m.getType() == OFType.EXPERIMENTER &&
531 ((OFExperimenter) m).getExperimenter() ==
532 RoleManager.NICIRA_EXPERIMENTER) {
533 h.sw.handleNiciraRole(m);
534 } else {
535 h.sw.processDriverHandshakeMessage(m);
536 if (h.sw.isDriverHandshakeComplete()) {
alshabibd7963912014-10-20 14:52:04 -0700537 moveToActive(h);
tom7ef8ff92014-09-17 13:08:06 -0700538 }
539 }
540 }
541 }
542
543 @Override
544 void processOFPortStatus(OFChannelHandler h, OFPortStatus m)
545 throws IOException, SwitchStateException {
546 h.pendingPortStatusMsg.add(m);
547 }
alshabibd7963912014-10-20 14:52:04 -0700548
549 private void moveToActive(OFChannelHandler h) {
550 boolean success = h.sw.connectSwitch();
Thomas Vachuska39274462014-12-02 13:23:50 -0800551 handlePendingPortStatusMessages(h);
alshabibd7963912014-10-20 14:52:04 -0700552 h.setState(ACTIVE);
553 if (!success) {
554 disconnectDuplicate(h);
alshabibd7963912014-10-20 14:52:04 -0700555 }
556 }
557
tom7ef8ff92014-09-17 13:08:06 -0700558 },
559
560
561 /**
562 * This controller is in MASTER role for this switch. We enter this state
563 * after requesting and winning control from the global registry.
564 * The main handshake as well as the switch-driver sub-handshake
565 * is complete at this point.
566 * // XXX S reconsider below
567 * In the (near) future we may deterministically assign controllers to
568 * switches at startup.
569 * We only leave this state if the switch disconnects or
570 * if we send a role request for SLAVE /and/ receive the role reply for
571 * SLAVE.
572 */
573 ACTIVE(true) {
574 @Override
575 void processOFError(OFChannelHandler h, OFErrorMsg m)
576 throws IOException, SwitchStateException {
577 // if we get here, then the error message is for something else
578 if (m.getErrType() == OFErrorType.BAD_REQUEST &&
579 ((OFBadRequestErrorMsg) m).getCode() ==
580 OFBadRequestCode.EPERM) {
581 // We are the master controller and the switch returned
582 // a permission error. This is a likely indicator that
583 // the switch thinks we are slave. Reassert our
584 // role
585 // FIXME: this could be really bad during role transitions
586 // if two controllers are master (even if its only for
587 // a brief period). We might need to see if these errors
588 // persist before we reassert
alshabib339a3d92014-09-26 17:54:32 -0700589
tom7ef8ff92014-09-17 13:08:06 -0700590 h.sw.reassertRole();
591 } else if (m.getErrType() == OFErrorType.FLOW_MOD_FAILED &&
592 ((OFFlowModFailedErrorMsg) m).getCode() ==
593 OFFlowModFailedCode.ALL_TABLES_FULL) {
594 h.sw.setTableFull(true);
595 } else {
596 logError(h, m);
597 }
598 h.dispatchMessage(m);
599 }
600
601 @Override
602 void processOFStatisticsReply(OFChannelHandler h,
603 OFStatsReply m) {
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700604 if (m.getStatsType().equals(OFStatsType.PORT_DESC)) {
605 h.sw.setPortDescReply((OFPortDescStatsReply) m);
606 }
tom7ef8ff92014-09-17 13:08:06 -0700607 h.dispatchMessage(m);
608 }
609
610 @Override
611 void processOFExperimenter(OFChannelHandler h, OFExperimenter m)
612 throws SwitchStateException {
613 h.sw.handleNiciraRole(m);
614 }
615
616 @Override
617 void processOFRoleReply(OFChannelHandler h, OFRoleReply m)
618 throws SwitchStateException {
619 h.sw.handleRole(m);
620 }
621
622 @Override
623 void processOFPortStatus(OFChannelHandler h, OFPortStatus m)
624 throws SwitchStateException {
625 handlePortStatusMessage(h, m, true);
Thomas Vachuska39274462014-12-02 13:23:50 -0800626 //h.dispatchMessage(m);
tom7ef8ff92014-09-17 13:08:06 -0700627 }
628
629 @Override
630 void processOFPacketIn(OFChannelHandler h, OFPacketIn m) {
alshabib9eab22f2014-10-20 17:17:31 -0700631// OFPacketOut out =
632// h.sw.factory().buildPacketOut()
633// .setXid(m.getXid())
634// .setBufferId(m.getBufferId()).build();
635// h.sw.sendMsg(out);
tom7ef8ff92014-09-17 13:08:06 -0700636 h.dispatchMessage(m);
637 }
638
639 @Override
640 void processOFFlowRemoved(OFChannelHandler h,
641 OFFlowRemoved m) {
642 h.dispatchMessage(m);
643 }
644
645 @Override
646 void processOFBarrierReply(OFChannelHandler h, OFBarrierReply m) {
647 h.dispatchMessage(m);
648 }
649
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700650 @Override
651 void processOFFeaturesReply(OFChannelHandler h, OFFeaturesReply m) {
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700652 h.sw.setFeaturesReply(m);
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700653 h.dispatchMessage(m);
654 }
655
tom7ef8ff92014-09-17 13:08:06 -0700656 };
657
658 private final boolean handshakeComplete;
659 ChannelState(boolean handshakeComplete) {
660 this.handshakeComplete = handshakeComplete;
661 }
662
663 /**
664 * Is this a state in which the handshake has completed?
665 * @return true if the handshake is complete
666 */
667 public boolean isHandshakeComplete() {
668 return handshakeComplete;
669 }
670
671 /**
672 * Get a string specifying the switch connection, state, and
673 * message received. To be used as message for SwitchStateException
674 * or log messages
675 * @param h The channel handler (to get switch information_
676 * @param m The OFMessage that has just been received
677 * @param details A string giving more details about the exact nature
678 * of the problem.
679 * @return display string
680 */
681 // needs to be protected because enum members are actually subclasses
682 protected String getSwitchStateMessage(OFChannelHandler h,
683 OFMessage m,
684 String details) {
685 return String.format("Switch: [%s], State: [%s], received: [%s]"
686 + ", details: %s",
687 h.getSwitchInfoString(),
688 this.toString(),
689 m.getType().toString(),
690 details);
691 }
692
693 /**
694 * We have an OFMessage we didn't expect given the current state and
695 * we want to treat this as an error.
696 * We currently throw an exception that will terminate the connection
697 * However, we could be more forgiving
698 * @param h the channel handler that received the message
699 * @param m the message
Jonathan Hart147b2ac2014-10-23 10:03:52 -0700700 * @throws SwitchStateException we always throw the exception
tom7ef8ff92014-09-17 13:08:06 -0700701 */
Jonathan Hart147b2ac2014-10-23 10:03:52 -0700702 // needs to be protected because enum members are actually subclasses
tom7ef8ff92014-09-17 13:08:06 -0700703 protected void illegalMessageReceived(OFChannelHandler h, OFMessage m)
704 throws SwitchStateException {
705 String msg = getSwitchStateMessage(h, m,
706 "Switch should never send this message in the current state");
707 throw new SwitchStateException(msg);
708
709 }
710
711 /**
712 * We have an OFMessage we didn't expect given the current state and
713 * we want to ignore the message.
714 * @param h the channel handler the received the message
715 * @param m the message
716 */
717 protected void unhandledMessageReceived(OFChannelHandler h,
718 OFMessage m) {
719 if (log.isDebugEnabled()) {
720 String msg = getSwitchStateMessage(h, m,
721 "Ignoring unexpected message");
722 log.debug(msg);
723 }
724 }
725
726 /**
727 * Log an OpenFlow error message from a switch.
728 * @param h The switch that sent the error
729 * @param error The error message
730 */
731 protected void logError(OFChannelHandler h, OFErrorMsg error) {
alshabib09d48be2014-10-03 15:43:33 -0700732 log.error("{} from switch {} in state {}",
tom7ef8ff92014-09-17 13:08:06 -0700733 error,
734 h.getSwitchInfoString(),
Ray Milkey6bc43c22015-11-06 13:22:38 -0800735 this.toString());
tom7ef8ff92014-09-17 13:08:06 -0700736 }
737
738 /**
739 * Log an OpenFlow error message from a switch and disconnect the
740 * channel.
741 *
742 * @param h the IO channel for this switch.
743 * @param error The error message
744 */
745 protected void logErrorDisconnect(OFChannelHandler h, OFErrorMsg error) {
746 logError(h, error);
747 h.channel.disconnect();
748 }
749
750 /**
751 * log an error message for a duplicate dpid and disconnect this channel.
752 * @param h the IO channel for this switch.
753 */
754 protected void disconnectDuplicate(OFChannelHandler h) {
755 log.error("Duplicated dpid or incompleted cleanup - "
756 + "disconnecting channel {}", h.getSwitchInfoString());
757 h.duplicateDpidFound = Boolean.TRUE;
758 h.channel.disconnect();
759 }
760
761
762
763 /**
764 * Handles all pending port status messages before a switch is declared
765 * activated in MASTER or EQUAL role. Note that since this handling
766 * precedes the activation (and therefore notification to IOFSwitchListerners)
767 * the changes to ports will already be visible once the switch is
768 * activated. As a result, no notifications are sent out for these
769 * pending portStatus messages.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700770 *
771 * @param h the channel handler that received the message
tom7ef8ff92014-09-17 13:08:06 -0700772 */
773 protected void handlePendingPortStatusMessages(OFChannelHandler h) {
774 try {
775 handlePendingPortStatusMessages(h, 0);
776 } catch (SwitchStateException e) {
777 log.error(e.getMessage());
778 }
779 }
780
781 private void handlePendingPortStatusMessages(OFChannelHandler h, int index)
782 throws SwitchStateException {
783 if (h.sw == null) {
784 String msg = "State machine error: switch is null. Should never " +
785 "happen";
786 throw new SwitchStateException(msg);
787 }
Thomas Vachuska39274462014-12-02 13:23:50 -0800788 log.info("Processing {} pending port status messages for {}",
789 h.pendingPortStatusMsg.size(), h.sw.getStringId());
790
tom7ef8ff92014-09-17 13:08:06 -0700791 ArrayList<OFPortStatus> temp = new ArrayList<OFPortStatus>();
792 for (OFPortStatus ps: h.pendingPortStatusMsg) {
793 temp.add(ps);
794 handlePortStatusMessage(h, ps, false);
795 }
tom7ef8ff92014-09-17 13:08:06 -0700796 // expensive but ok - we don't expect too many port-status messages
797 // note that we cannot use clear(), because of the reasons below
798 h.pendingPortStatusMsg.removeAll(temp);
Thomas Vachuska39274462014-12-02 13:23:50 -0800799 temp.clear();
tom7ef8ff92014-09-17 13:08:06 -0700800 // the iterator above takes a snapshot of the list - so while we were
801 // dealing with the pending port-status messages, we could have received
802 // newer ones. Handle them recursively, but break the recursion after
803 // five steps to avoid an attack.
804 if (!h.pendingPortStatusMsg.isEmpty() && ++index < 5) {
805 handlePendingPortStatusMessages(h, index);
806 }
807 }
808
809 /**
810 * Handle a port status message.
811 *
812 * Handle a port status message by updating the port maps in the
813 * IOFSwitch instance and notifying Controller about the change so
814 * it can dispatch a switch update.
815 *
816 * @param h The OFChannelHhandler that received the message
817 * @param m The PortStatus message we received
818 * @param doNotify if true switch port changed events will be
819 * dispatched
Thomas Vachuskab14c77a2014-11-04 18:08:01 -0800820 * @throws SwitchStateException if the switch is not bound to the channel
tom7ef8ff92014-09-17 13:08:06 -0700821 *
822 */
823 protected void handlePortStatusMessage(OFChannelHandler h, OFPortStatus m,
824 boolean doNotify) throws SwitchStateException {
825 if (h.sw == null) {
826 String msg = getSwitchStateMessage(h, m,
827 "State machine error: switch is null. Should never " +
828 "happen");
829 throw new SwitchStateException(msg);
830 }
831
832 h.sw.handleMessage(m);
833 }
834
835
836 /**
837 * Process an OF message received on the channel and
838 * update state accordingly.
839 *
840 * The main "event" of the state machine. Process the received message,
841 * send follow up message if required and update state if required.
842 *
843 * Switches on the message type and calls more specific event handlers
844 * for each individual OF message type. If we receive a message that
845 * is supposed to be sent from a controller to a switch we throw
846 * a SwitchStateExeption.
847 *
848 * The more specific handlers can also throw SwitchStateExceptions
849 *
850 * @param h The OFChannelHandler that received the message
851 * @param m The message we received.
Thomas Vachuskab14c77a2014-11-04 18:08:01 -0800852 * @throws SwitchStateException if the switch is not bound to the channel
853 * @throws IOException if unable to send message back to the switch
tom7ef8ff92014-09-17 13:08:06 -0700854 */
855 void processOFMessage(OFChannelHandler h, OFMessage m)
856 throws IOException, SwitchStateException {
857 switch(m.getType()) {
858 case HELLO:
859 processOFHello(h, (OFHello) m);
860 break;
861 case BARRIER_REPLY:
862 processOFBarrierReply(h, (OFBarrierReply) m);
863 break;
864 case ECHO_REPLY:
865 processOFEchoReply(h, (OFEchoReply) m);
866 break;
867 case ECHO_REQUEST:
868 processOFEchoRequest(h, (OFEchoRequest) m);
869 break;
870 case ERROR:
871 processOFError(h, (OFErrorMsg) m);
872 break;
873 case FEATURES_REPLY:
874 processOFFeaturesReply(h, (OFFeaturesReply) m);
875 break;
876 case FLOW_REMOVED:
877 processOFFlowRemoved(h, (OFFlowRemoved) m);
878 break;
879 case GET_CONFIG_REPLY:
880 processOFGetConfigReply(h, (OFGetConfigReply) m);
881 break;
882 case PACKET_IN:
883 processOFPacketIn(h, (OFPacketIn) m);
884 break;
885 case PORT_STATUS:
886 processOFPortStatus(h, (OFPortStatus) m);
887 break;
888 case QUEUE_GET_CONFIG_REPLY:
889 processOFQueueGetConfigReply(h, (OFQueueGetConfigReply) m);
890 break;
891 case STATS_REPLY: // multipart_reply in 1.3
892 processOFStatisticsReply(h, (OFStatsReply) m);
893 break;
894 case EXPERIMENTER:
895 processOFExperimenter(h, (OFExperimenter) m);
896 break;
897 case ROLE_REPLY:
898 processOFRoleReply(h, (OFRoleReply) m);
899 break;
900 case GET_ASYNC_REPLY:
901 processOFGetAsyncReply(h, (OFAsyncGetReply) m);
902 break;
903
904 // The following messages are sent to switches. The controller
905 // should never receive them
906 case SET_CONFIG:
907 case GET_CONFIG_REQUEST:
908 case PACKET_OUT:
909 case PORT_MOD:
910 case QUEUE_GET_CONFIG_REQUEST:
911 case BARRIER_REQUEST:
912 case STATS_REQUEST: // multipart request in 1.3
913 case FEATURES_REQUEST:
914 case FLOW_MOD:
915 case GROUP_MOD:
916 case TABLE_MOD:
917 case GET_ASYNC_REQUEST:
918 case SET_ASYNC:
919 case METER_MOD:
920 default:
921 illegalMessageReceived(h, m);
922 break;
923 }
924 }
925
926 /*-----------------------------------------------------------------
927 * Default implementation for message handlers in any state.
928 *
929 * Individual states must override these if they want a behavior
930 * that differs from the default.
931 *
932 * In general, these handlers simply ignore the message and do
933 * nothing.
934 *
935 * There are some exceptions though, since some messages really
936 * are handled the same way in every state (e.g., ECHO_REQUST) or
937 * that are only valid in a single state (e.g., HELLO, GET_CONFIG_REPLY
938 -----------------------------------------------------------------*/
939
940 void processOFHello(OFChannelHandler h, OFHello m)
941 throws IOException, SwitchStateException {
942 // we only expect hello in the WAIT_HELLO state
alshabib45fd88a2015-09-24 17:34:35 -0700943 log.warn("Received Hello outside WAIT_HELLO state; switch {} is not complaint.",
944 h.channel.getRemoteAddress());
tom7ef8ff92014-09-17 13:08:06 -0700945 }
946
947 void processOFBarrierReply(OFChannelHandler h, OFBarrierReply m)
948 throws IOException {
949 // Silently ignore.
950 }
951
952 void processOFEchoRequest(OFChannelHandler h, OFEchoRequest m)
953 throws IOException {
954 if (h.ofVersion == null) {
955 log.error("No OF version set for {}. Not sending Echo REPLY",
956 h.channel.getRemoteAddress());
957 return;
958 }
959 OFFactory factory = (h.ofVersion == OFVersion.OF_13) ?
960 h.controller.getOFMessageFactory13() : h.controller.getOFMessageFactory10();
961 OFEchoReply reply = factory
962 .buildEchoReply()
963 .setXid(m.getXid())
964 .setData(m.getData())
965 .build();
966 h.channel.write(Collections.singletonList(reply));
967 }
968
969 void processOFEchoReply(OFChannelHandler h, OFEchoReply m)
970 throws IOException {
971 // Do nothing with EchoReplies !!
972 }
973
974 // no default implementation for OFError
975 // every state must override it
976 abstract void processOFError(OFChannelHandler h, OFErrorMsg m)
977 throws IOException, SwitchStateException;
978
979
980 void processOFFeaturesReply(OFChannelHandler h, OFFeaturesReply m)
981 throws IOException, SwitchStateException {
982 unhandledMessageReceived(h, m);
983 }
984
985 void processOFFlowRemoved(OFChannelHandler h, OFFlowRemoved m)
986 throws IOException {
987 unhandledMessageReceived(h, m);
988 }
989
990 void processOFGetConfigReply(OFChannelHandler h, OFGetConfigReply m)
991 throws IOException, SwitchStateException {
992 // we only expect config replies in the WAIT_CONFIG_REPLY state
993 illegalMessageReceived(h, m);
994 }
995
996 void processOFPacketIn(OFChannelHandler h, OFPacketIn m)
997 throws IOException {
998 unhandledMessageReceived(h, m);
999 }
1000
1001 // no default implementation. Every state needs to handle it.
1002 abstract void processOFPortStatus(OFChannelHandler h, OFPortStatus m)
1003 throws IOException, SwitchStateException;
1004
1005 void processOFQueueGetConfigReply(OFChannelHandler h,
1006 OFQueueGetConfigReply m)
1007 throws IOException {
1008 unhandledMessageReceived(h, m);
1009 }
1010
1011 void processOFStatisticsReply(OFChannelHandler h, OFStatsReply m)
1012 throws IOException, SwitchStateException {
1013 unhandledMessageReceived(h, m);
1014 }
1015
1016 void processOFExperimenter(OFChannelHandler h, OFExperimenter m)
1017 throws IOException, SwitchStateException {
1018 // TODO: it might make sense to parse the vendor message here
1019 // into the known vendor messages we support and then call more
1020 // specific event handlers
1021 unhandledMessageReceived(h, m);
1022 }
1023
1024 void processOFRoleReply(OFChannelHandler h, OFRoleReply m)
1025 throws SwitchStateException, IOException {
1026 unhandledMessageReceived(h, m);
1027 }
1028
1029 void processOFGetAsyncReply(OFChannelHandler h,
1030 OFAsyncGetReply m) {
1031 unhandledMessageReceived(h, m);
1032 }
1033
1034 }
1035
1036
1037
1038 //*************************
1039 // Channel handler methods
1040 //*************************
1041
1042 @Override
1043 public void channelConnected(ChannelHandlerContext ctx,
1044 ChannelStateEvent e) throws Exception {
1045 channel = e.getChannel();
1046 log.info("New switch connection from {}",
1047 channel.getRemoteAddress());
alshabib70fc7fb2015-01-06 11:04:29 -08001048 /*
1049 hack to wait for the switch to tell us what it's
1050 max version is. This is not spec compliant and should
1051 be removed as soon as switches behave better.
1052 */
1053 //sendHandshakeHelloMessage();
tom7ef8ff92014-09-17 13:08:06 -07001054 setState(ChannelState.WAIT_HELLO);
1055 }
1056
1057 @Override
1058 public void channelDisconnected(ChannelHandlerContext ctx,
1059 ChannelStateEvent e) throws Exception {
1060 log.info("Switch disconnected callback for sw:{}. Cleaning up ...",
1061 getSwitchInfoString());
1062 if (thisdpid != 0) {
1063 if (!duplicateDpidFound) {
1064 // if the disconnected switch (on this ChannelHandler)
1065 // was not one with a duplicate-dpid, it is safe to remove all
1066 // state for it at the controller. Notice that if the disconnected
1067 // switch was a duplicate-dpid, calling the method below would clear
1068 // all state for the original switch (with the same dpid),
1069 // which we obviously don't want.
Yuta HIGUCHI17679472014-10-09 21:53:14 -07001070 log.info("{}:removal called", getSwitchInfoString());
Jonathan Hart147b2ac2014-10-23 10:03:52 -07001071 if (sw != null) {
1072 sw.removeConnectedSwitch();
1073 }
tom7ef8ff92014-09-17 13:08:06 -07001074 } else {
1075 // A duplicate was disconnected on this ChannelHandler,
1076 // this is the same switch reconnecting, but the original state was
1077 // not cleaned up - XXX check liveness of original ChannelHandler
Yuta HIGUCHI17679472014-10-09 21:53:14 -07001078 log.info("{}:duplicate found", getSwitchInfoString());
tom7ef8ff92014-09-17 13:08:06 -07001079 duplicateDpidFound = Boolean.FALSE;
1080 }
1081 } else {
1082 log.warn("no dpid in channelHandler registered for "
1083 + "disconnected switch {}", getSwitchInfoString());
1084 }
1085 }
1086
1087 @Override
1088 public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
1089 throws Exception {
1090 if (e.getCause() instanceof ReadTimeoutException) {
1091 // switch timeout
1092 log.error("Disconnecting switch {} due to read timeout",
1093 getSwitchInfoString());
1094 ctx.getChannel().close();
1095 } else if (e.getCause() instanceof HandshakeTimeoutException) {
1096 log.error("Disconnecting switch {}: failed to complete handshake",
1097 getSwitchInfoString());
1098 ctx.getChannel().close();
1099 } else if (e.getCause() instanceof ClosedChannelException) {
1100 log.debug("Channel for sw {} already closed", getSwitchInfoString());
1101 } else if (e.getCause() instanceof IOException) {
Thomas Vachuskae9af1f42015-07-06 08:42:18 -07001102 if (!e.getCause().getMessage().equals(RESET_BY_PEER) &&
1103 !e.getCause().getMessage().equals(BROKEN_PIPE)) {
1104 log.error("Disconnecting switch {} due to IO Error: {}",
1105 getSwitchInfoString(), e.getCause().getMessage());
1106 if (log.isDebugEnabled()) {
1107 // still print stack trace if debug is enabled
1108 log.debug("StackTrace for previous Exception: ", e.getCause());
1109 }
tom7ef8ff92014-09-17 13:08:06 -07001110 }
1111 ctx.getChannel().close();
1112 } else if (e.getCause() instanceof SwitchStateException) {
1113 log.error("Disconnecting switch {} due to switch state error: {}",
1114 getSwitchInfoString(), e.getCause().getMessage());
1115 if (log.isDebugEnabled()) {
1116 // still print stack trace if debug is enabled
1117 log.debug("StackTrace for previous Exception: ", e.getCause());
1118 }
1119 ctx.getChannel().close();
1120 } else if (e.getCause() instanceof OFParseError) {
1121 log.error("Disconnecting switch "
1122 + getSwitchInfoString() +
1123 " due to message parse failure",
1124 e.getCause());
1125 ctx.getChannel().close();
1126 } else if (e.getCause() instanceof RejectedExecutionException) {
1127 log.warn("Could not process message: queue full");
1128 } else {
1129 log.error("Error while processing message from switch "
1130 + getSwitchInfoString()
1131 + "state " + this.state, e.getCause());
1132 ctx.getChannel().close();
1133 }
1134 }
1135
1136 @Override
1137 public String toString() {
1138 return getSwitchInfoString();
1139 }
1140
1141 @Override
1142 public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e)
1143 throws Exception {
1144 OFFactory factory = (ofVersion == OFVersion.OF_13) ? factory13 : factory10;
1145 OFMessage m = factory.buildEchoRequest().build();
alshabib09d48be2014-10-03 15:43:33 -07001146 log.debug("Sending Echo Request on idle channel: {}",
tom7ef8ff92014-09-17 13:08:06 -07001147 e.getChannel().getPipeline().getLast().toString());
1148 e.getChannel().write(Collections.singletonList(m));
1149 // XXX S some problems here -- echo request has no transaction id, and
1150 // echo reply is not correlated to the echo request.
1151 }
1152
1153 @Override
1154 public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
1155 throws Exception {
1156 if (e.getMessage() instanceof List) {
1157 @SuppressWarnings("unchecked")
1158 List<OFMessage> msglist = (List<OFMessage>) e.getMessage();
1159
1160
1161 for (OFMessage ofm : msglist) {
1162 // Do the actual packet processing
1163 state.processOFMessage(this, ofm);
1164 }
1165 } else {
1166 state.processOFMessage(this, (OFMessage) e.getMessage());
1167 }
1168 }
1169
1170
1171
1172 //*************************
1173 // Channel utility methods
1174 //*************************
1175
1176 /**
1177 * Is this a state in which the handshake has completed?
1178 * @return true if the handshake is complete
1179 */
1180 public boolean isHandshakeComplete() {
1181 return this.state.isHandshakeComplete();
1182 }
1183
1184 private void dispatchMessage(OFMessage m) {
1185 sw.handleMessage(m);
1186 }
1187
1188 /**
1189 * Return a string describing this switch based on the already available
1190 * information (DPID and/or remote socket).
1191 * @return display string
1192 */
1193 private String getSwitchInfoString() {
1194 if (sw != null) {
1195 return sw.toString();
1196 }
1197 String channelString;
1198 if (channel == null || channel.getRemoteAddress() == null) {
1199 channelString = "?";
1200 } else {
1201 channelString = channel.getRemoteAddress().toString();
1202 }
1203 String dpidString;
1204 if (featuresReply == null) {
1205 dpidString = "?";
1206 } else {
1207 dpidString = featuresReply.getDatapathId().toString();
1208 }
1209 return String.format("[%s DPID[%s]]", channelString, dpidString);
1210 }
1211
1212 /**
1213 * Update the channels state. Only called from the state machine.
1214 * TODO: enforce restricted state transitions
1215 * @param state
1216 */
1217 private void setState(ChannelState state) {
1218 this.state = state;
1219 }
1220
1221 /**
1222 * Send hello message to the switch using the handshake transactions ids.
1223 * @throws IOException
1224 */
1225 private void sendHandshakeHelloMessage() throws IOException {
1226 // The OF protocol requires us to start things off by sending the highest
1227 // version of the protocol supported.
1228
1229 // bitmap represents OF1.0 (ofp_version=0x01) and OF1.3 (ofp_version=0x04)
1230 // see Sec. 7.5.1 of the OF1.3.4 spec
1231 U32 bitmap = U32.ofRaw(0x00000012);
1232 OFHelloElem hem = factory13.buildHelloElemVersionbitmap()
1233 .setBitmaps(Collections.singletonList(bitmap))
1234 .build();
1235 OFMessage.Builder mb = factory13.buildHello()
1236 .setXid(this.handshakeTransactionIds--)
1237 .setElements(Collections.singletonList(hem));
1238 log.info("Sending OF_13 Hello to {}", channel.getRemoteAddress());
1239 channel.write(Collections.singletonList(mb.build()));
1240 }
1241
1242 /**
1243 * Send featuresRequest msg to the switch using the handshake transactions ids.
1244 * @throws IOException
1245 */
1246 private void sendHandshakeFeaturesRequestMessage() throws IOException {
1247 OFFactory factory = (ofVersion == OFVersion.OF_13) ? factory13 : factory10;
1248 OFMessage m = factory.buildFeaturesRequest()
1249 .setXid(this.handshakeTransactionIds--)
1250 .build();
1251 channel.write(Collections.singletonList(m));
1252 }
1253
1254 /**
1255 * Send the configuration requests to tell the switch we want full
1256 * packets.
1257 * @throws IOException
1258 */
1259 private void sendHandshakeSetConfig() throws IOException {
1260 OFFactory factory = (ofVersion == OFVersion.OF_13) ? factory13 : factory10;
1261 //log.debug("Sending CONFIG_REQUEST to {}", channel.getRemoteAddress());
1262 List<OFMessage> msglist = new ArrayList<OFMessage>(3);
1263
1264 // Ensure we receive the full packet via PacketIn
1265 // FIXME: We don't set the reassembly flags.
Michael Jarschel7f521a32015-08-12 16:31:07 +02001266 // Only send config to switches to send full packets, if they have a buffer.
1267 // Saves a packet & OFSetConfig can't be handled by certain switches.
1268 if(this.featuresReply.getNBuffers() > 0) {
1269 OFSetConfig sc = factory
1270 .buildSetConfig()
1271 .setMissSendLen((short) 0xffff)
1272 .setXid(this.handshakeTransactionIds--)
1273 .build();
1274 msglist.add(sc);
1275 }
tom7ef8ff92014-09-17 13:08:06 -07001276
1277 // Barrier
1278 OFBarrierRequest br = factory
1279 .buildBarrierRequest()
1280 .setXid(this.handshakeTransactionIds--)
1281 .build();
1282 msglist.add(br);
1283
1284 // Verify (need barrier?)
1285 OFGetConfigRequest gcr = factory
1286 .buildGetConfigRequest()
1287 .setXid(this.handshakeTransactionIds--)
1288 .build();
1289 msglist.add(gcr);
1290 channel.write(msglist);
1291 }
1292
1293 /**
1294 * send a description state request.
1295 * @throws IOException
1296 */
1297 private void sendHandshakeDescriptionStatsRequest() throws IOException {
1298 // Get Description to set switch-specific flags
1299 OFFactory factory = (ofVersion == OFVersion.OF_13) ? factory13 : factory10;
1300 OFDescStatsRequest dreq = factory
1301 .buildDescStatsRequest()
1302 .setXid(handshakeTransactionIds--)
1303 .build();
1304 channel.write(Collections.singletonList(dreq));
1305 }
1306
1307 private void sendHandshakeOFPortDescRequest() throws IOException {
1308 // Get port description for 1.3 switch
1309 OFPortDescStatsRequest preq = factory13
1310 .buildPortDescStatsRequest()
1311 .setXid(handshakeTransactionIds--)
1312 .build();
1313 channel.write(Collections.singletonList(preq));
1314 }
1315
1316 ChannelState getStateForTesting() {
1317 return state;
1318 }
1319
1320}