blob: 0b7f4309d928f98a2071d25cdd548d4379add837 [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 */
tom7ef8ff92014-09-17 13:08:06 -070016
Brian O'Connorabafb502014-12-02 22:26:20 -080017package org.onosproject.openflow.controller.impl;
tom7ef8ff92014-09-17 13:08:06 -070018
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070019import static org.slf4j.LoggerFactory.getLogger;
20
tom7ef8ff92014-09-17 13:08:06 -070021import java.util.concurrent.TimeUnit;
22
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070023import org.slf4j.Logger;
24import io.netty.channel.ChannelDuplexHandler;
25import io.netty.channel.ChannelHandlerContext;
26import io.netty.channel.ChannelPromise;
tom7ef8ff92014-09-17 13:08:06 -070027
28/**
29 * Trigger a timeout if a switch fails to complete handshake soon enough.
30 */
31public class HandshakeTimeoutHandler
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070032 extends ChannelDuplexHandler {
33
34 private static final Logger log = getLogger(HandshakeTimeoutHandler.class);
tom7ef8ff92014-09-17 13:08:06 -070035
36 final OFChannelHandler channelHandler;
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070037 final long timeoutMillis;
38 volatile long deadline;
tom7ef8ff92014-09-17 13:08:06 -070039
40 public HandshakeTimeoutHandler(OFChannelHandler channelHandler,
tom7ef8ff92014-09-17 13:08:06 -070041 long timeoutSeconds) {
42 super();
43 this.channelHandler = channelHandler;
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070044 this.timeoutMillis = TimeUnit.SECONDS.toMillis(timeoutSeconds);
45 this.deadline = System.currentTimeMillis() + timeoutMillis;
tom7ef8ff92014-09-17 13:08:06 -070046 }
47
48 @Override
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070049 public void channelActive(ChannelHandlerContext ctx) throws Exception {
50 if (timeoutMillis > 0) {
51 // set Handshake deadline
52 deadline = System.currentTimeMillis() + timeoutMillis;
tom7ef8ff92014-09-17 13:08:06 -070053 }
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070054 super.channelActive(ctx);
tom7ef8ff92014-09-17 13:08:06 -070055 }
56
57 @Override
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070058 public void read(ChannelHandlerContext ctx) throws Exception {
59 checkTimeout(ctx);
60 super.read(ctx);
tom7ef8ff92014-09-17 13:08:06 -070061 }
62
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070063 @Override
64 public void write(ChannelHandlerContext ctx, Object msg,
65 ChannelPromise promise)
66 throws Exception {
67 checkTimeout(ctx);
68 super.write(ctx, msg, promise);
69 }
tom7ef8ff92014-09-17 13:08:06 -070070
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070071 @Override
72 public void userEventTriggered(ChannelHandlerContext ctx,
73 Object evt)
74 throws Exception {
tom7ef8ff92014-09-17 13:08:06 -070075
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070076 // expecting idle event
77 checkTimeout(ctx);
78 super.userEventTriggered(ctx, evt);
79 }
80
81 void checkTimeout(ChannelHandlerContext ctx) {
82 if (channelHandler.isHandshakeComplete()) {
83 // handshake complete, Handshake monitoring timeout no-longer needed
84 ctx.channel().pipeline().remove(this);
85 return;
tom7ef8ff92014-09-17 13:08:06 -070086 }
87
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070088 if (!ctx.channel().isActive()) {
89 return;
90 }
tom7ef8ff92014-09-17 13:08:06 -070091
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070092 if (System.currentTimeMillis() > deadline) {
93 log.info("Handshake time out {}", channelHandler);
94 ctx.fireExceptionCaught(new HandshakeTimeoutException());
tom7ef8ff92014-09-17 13:08:06 -070095 }
96 }
97}