Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | package org.onosproject.ofagent.impl; |
| 17 | |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 18 | import io.netty.channel.ChannelDuplexHandler; |
| 19 | import io.netty.channel.ChannelHandlerContext; |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 20 | import io.netty.handler.timeout.ReadTimeoutException; |
| 21 | import org.onlab.osgi.DefaultServiceDirectory; |
| 22 | import org.onlab.osgi.ServiceDirectory; |
| 23 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 24 | import org.onosproject.ofagent.api.OFSwitch; |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 25 | import org.projectfloodlight.openflow.protocol.OFErrorMsg; |
| 26 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 27 | import org.projectfloodlight.openflow.protocol.OFFactory; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 28 | import org.projectfloodlight.openflow.protocol.OFMessage; |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 29 | import org.projectfloodlight.openflow.protocol.OFVersion; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 33 | import java.io.IOException; |
| 34 | import java.nio.channels.ClosedChannelException; |
| 35 | import java.util.List; |
| 36 | import java.util.concurrent.RejectedExecutionException; |
| 37 | |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Implementation of OpenFlow channel handler. |
| 40 | * It processes OpenFlow message according to the channel state. |
| 41 | */ |
| 42 | public final class OFChannelHandler extends ChannelDuplexHandler { |
| 43 | |
| 44 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 45 | private final OFSwitch ofSwitch; |
| 46 | |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 47 | private ChannelHandlerContext ctx; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 48 | private ChannelState state; |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 49 | protected static final OFFactory FACTORY = OFFactories.getFactory(OFVersion.OF_13); |
| 50 | protected VirtualNetworkService vNetService; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 51 | |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 52 | enum ChannelState { |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 53 | |
| 54 | INIT { |
| 55 | @Override |
| 56 | void processOFMessage(final OFChannelHandler handler, |
| 57 | final OFMessage msg) { |
| 58 | // TODO implement |
| 59 | } |
| 60 | }, |
| 61 | WAIT_HELLO { |
| 62 | @Override |
| 63 | void processOFMessage(final OFChannelHandler handler, |
| 64 | final OFMessage msg) { |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 65 | |
| 66 | switch (msg.getType()) { |
| 67 | case HELLO: |
| 68 | handler.setState(ChannelState.WAIT_FEATURE_REQUEST); |
| 69 | break; |
| 70 | default: |
| 71 | handler.illegalMessageReceived(msg); |
| 72 | break; |
| 73 | } |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 74 | } |
| 75 | }, |
| 76 | WAIT_FEATURE_REQUEST { |
| 77 | @Override |
| 78 | void processOFMessage(final OFChannelHandler handler, |
| 79 | final OFMessage msg) { |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 80 | |
| 81 | switch (msg.getType()) { |
| 82 | case FEATURES_REQUEST: |
| 83 | handler.ofSwitch.processFeaturesRequest(handler.ctx.channel(), msg); |
| 84 | handler.setState(ChannelState.ESTABLISHED); |
| 85 | break; |
| 86 | case ECHO_REQUEST: |
| 87 | handler.ofSwitch.processEchoRequest(handler.ctx.channel(), msg); |
| 88 | break; |
| 89 | case ERROR: |
| 90 | handler.logErrorClose(handler.ctx, (OFErrorMsg) msg); |
| 91 | break; |
| 92 | default: |
| 93 | handler.illegalMessageReceived(msg); |
| 94 | break; |
| 95 | |
| 96 | } |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 97 | } |
| 98 | }, |
| 99 | ESTABLISHED { |
| 100 | @Override |
| 101 | void processOFMessage(final OFChannelHandler handler, |
| 102 | final OFMessage msg) { |
| 103 | // TODO implement |
| 104 | // TODO add this channel to ofSwitch role service |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 105 | switch (msg.getType()) { |
| 106 | case STATS_REQUEST: |
| 107 | //TODO implement |
| 108 | //TODO: use vNetService to build OFPortDesc. |
| 109 | break; |
| 110 | case SET_CONFIG: |
| 111 | //TODO implement |
| 112 | break; |
| 113 | case GET_CONFIG_REQUEST: |
| 114 | //TODO implement |
| 115 | break; |
| 116 | case BARRIER_REQUEST: |
| 117 | //TODO implement |
| 118 | break; |
| 119 | case ECHO_REQUEST: |
| 120 | handler.ofSwitch.processEchoRequest(handler.ctx.channel(), msg); |
| 121 | break; |
| 122 | case ERROR: |
| 123 | handler.logErrorClose(handler.ctx, (OFErrorMsg) msg); |
| 124 | break; |
| 125 | default: |
| 126 | handler.unhandledMessageReceived(msg); |
| 127 | break; |
| 128 | } |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 129 | } |
| 130 | }; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 131 | abstract void processOFMessage(final OFChannelHandler handler, |
| 132 | final OFMessage msg); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Default constructor. |
| 137 | * |
| 138 | * @param ofSwitch openflow switch that owns this channel |
| 139 | */ |
| 140 | public OFChannelHandler(OFSwitch ofSwitch) { |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 141 | super(); |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 142 | this.ofSwitch = ofSwitch; |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 143 | |
| 144 | setState(ChannelState.INIT); |
| 145 | |
| 146 | ServiceDirectory services = new DefaultServiceDirectory(); |
| 147 | vNetService = services.get(VirtualNetworkService.class); |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void channelActive(ChannelHandlerContext ctx) throws Exception { |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 152 | this.ctx = ctx; |
| 153 | log.debug("Channel Active. Send OF_13 Hello to {}", ctx.channel().remoteAddress()); |
| 154 | |
| 155 | try { |
| 156 | ofSwitch.sendOfHello(ctx.channel()); |
| 157 | setState(ChannelState.WAIT_HELLO); |
| 158 | } catch (Throwable cause) { |
| 159 | log.error("Exception occured because of{}", cause.getMessage()); |
| 160 | } |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public void channelRead(ChannelHandlerContext ctx, Object msg) |
| 165 | throws Exception { |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 166 | |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 167 | try { |
| 168 | if (msg instanceof List) { |
| 169 | ((List) msg).forEach(ofm -> { |
| 170 | state.processOFMessage(this, (OFMessage) ofm); |
| 171 | }); |
| 172 | } else { |
| 173 | state.processOFMessage(this, (OFMessage) msg); |
| 174 | } |
| 175 | } catch (Throwable cause) { |
| 176 | log.error("Exception occured {}", cause.getMessage()); |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 177 | } |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 178 | |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public void channelReadComplete(ChannelHandlerContext ctx) |
| 183 | throws Exception { |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 188 | if (cause instanceof ReadTimeoutException) { |
| 189 | log.error("Connection closed because of ReadTimeoutException {}", cause.getMessage()); |
| 190 | } else if (cause instanceof ClosedChannelException) { |
| 191 | log.error("ClosedChannelException occured"); |
| 192 | return; |
| 193 | } else if (cause instanceof RejectedExecutionException) { |
| 194 | log.error("Could not process message: queue full"); |
| 195 | } else if (cause instanceof IOException) { |
| 196 | log.error("IOException occured"); |
| 197 | } else { |
| 198 | log.error("Error while processing message from switch {}", cause.getMessage()); |
| 199 | } |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 200 | ctx.close(); |
| 201 | } |
| 202 | |
| 203 | private void setState(ChannelState state) { |
| 204 | this.state = state; |
| 205 | } |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame^] | 206 | |
| 207 | private void logErrorClose(ChannelHandlerContext ctx, OFErrorMsg errorMsg) { |
| 208 | log.error("{} from switch {} in state {}", |
| 209 | errorMsg, |
| 210 | ofSwitch.device().id().toString(), |
| 211 | state); |
| 212 | |
| 213 | log.error("Disconnecting..."); |
| 214 | ctx.close(); |
| 215 | } |
| 216 | |
| 217 | private void illegalMessageReceived(OFMessage ofMessage) { |
| 218 | log.warn("Controller should never send this message {} in current state {}", |
| 219 | ofMessage.getType().toString(), |
| 220 | state); |
| 221 | } |
| 222 | |
| 223 | private void unhandledMessageReceived(OFMessage ofMessage) { |
| 224 | log.warn("Unhandled message {} received in state {}. Ignored", |
| 225 | ofMessage.getType().toString(), |
| 226 | state); |
| 227 | } |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 228 | } |