Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 16 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 17 | package org.onosproject.openflow.controller.impl; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 18 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 21 | import java.util.concurrent.TimeUnit; |
| 22 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 23 | import org.slf4j.Logger; |
| 24 | import io.netty.channel.ChannelDuplexHandler; |
| 25 | import io.netty.channel.ChannelHandlerContext; |
| 26 | import io.netty.channel.ChannelPromise; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Trigger a timeout if a switch fails to complete handshake soon enough. |
| 30 | */ |
| 31 | public class HandshakeTimeoutHandler |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 32 | extends ChannelDuplexHandler { |
| 33 | |
| 34 | private static final Logger log = getLogger(HandshakeTimeoutHandler.class); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 35 | |
| 36 | final OFChannelHandler channelHandler; |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 37 | final long timeoutMillis; |
| 38 | volatile long deadline; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 39 | |
| 40 | public HandshakeTimeoutHandler(OFChannelHandler channelHandler, |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 41 | long timeoutSeconds) { |
| 42 | super(); |
| 43 | this.channelHandler = channelHandler; |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 44 | this.timeoutMillis = TimeUnit.SECONDS.toMillis(timeoutSeconds); |
| 45 | this.deadline = System.currentTimeMillis() + timeoutMillis; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | @Override |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 49 | public void channelActive(ChannelHandlerContext ctx) throws Exception { |
| 50 | if (timeoutMillis > 0) { |
| 51 | // set Handshake deadline |
| 52 | deadline = System.currentTimeMillis() + timeoutMillis; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 53 | } |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 54 | super.channelActive(ctx); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @Override |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 58 | public void read(ChannelHandlerContext ctx) throws Exception { |
| 59 | checkTimeout(ctx); |
| 60 | super.read(ctx); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 63 | @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 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 70 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 71 | @Override |
| 72 | public void userEventTriggered(ChannelHandlerContext ctx, |
| 73 | Object evt) |
| 74 | throws Exception { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 75 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 76 | // 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; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 88 | if (!ctx.channel().isActive()) { |
| 89 | return; |
| 90 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 91 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 92 | if (System.currentTimeMillis() > deadline) { |
| 93 | log.info("Handshake time out {}", channelHandler); |
| 94 | ctx.fireExceptionCaught(new HandshakeTimeoutException()); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | } |