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