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 | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 19 | import io.netty.channel.Channel; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 20 | import org.onosproject.net.Port; |
| 21 | import org.onosproject.net.flow.FlowRule; |
| 22 | import org.onosproject.net.packet.InboundPacket; |
| 23 | import org.onosproject.ofagent.api.OFSwitch; |
| 24 | import org.onosproject.ofagent.api.OFSwitchCapabilities; |
Claudine Chiu | e2d5acc | 2017-06-08 22:49:21 -0400 | [diff] [blame^] | 25 | import org.projectfloodlight.openflow.protocol.OFBarrierReply; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 26 | import org.projectfloodlight.openflow.protocol.OFControllerRole; |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 27 | import org.projectfloodlight.openflow.protocol.OFEchoReply; |
| 28 | import org.projectfloodlight.openflow.protocol.OFEchoRequest; |
| 29 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 30 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 31 | import org.projectfloodlight.openflow.protocol.OFFeaturesReply; |
Claudine Chiu | e2d5acc | 2017-06-08 22:49:21 -0400 | [diff] [blame^] | 32 | import org.projectfloodlight.openflow.protocol.OFGetConfigReply; |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 33 | import org.projectfloodlight.openflow.protocol.OFHello; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 34 | import org.projectfloodlight.openflow.protocol.OFMessage; |
Claudine Chiu | e2d5acc | 2017-06-08 22:49:21 -0400 | [diff] [blame^] | 35 | import org.projectfloodlight.openflow.protocol.OFMeterFeatures; |
| 36 | import org.projectfloodlight.openflow.protocol.OFSetConfig; |
| 37 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
| 38 | import org.projectfloodlight.openflow.protocol.OFStatsRequest; |
| 39 | import org.projectfloodlight.openflow.protocol.OFType; |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 40 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 41 | import org.projectfloodlight.openflow.types.DatapathId; |
Claudine Chiu | e2d5acc | 2017-06-08 22:49:21 -0400 | [diff] [blame^] | 42 | import org.slf4j.Logger; |
| 43 | import org.slf4j.LoggerFactory; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 44 | |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 45 | import java.util.Collections; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 46 | import java.util.Set; |
| 47 | import java.util.concurrent.ConcurrentHashMap; |
| 48 | |
| 49 | import static com.google.common.base.Preconditions.checkArgument; |
| 50 | import static com.google.common.base.Preconditions.checkNotNull; |
Claudine Chiu | e2d5acc | 2017-06-08 22:49:21 -0400 | [diff] [blame^] | 51 | import static org.projectfloodlight.openflow.protocol.OFControllerRole.ROLE_EQUAL; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 52 | |
| 53 | /** |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 54 | * Implementation of the default OpenFlow switch. |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 55 | */ |
| 56 | public final class DefaultOFSwitch implements OFSwitch { |
| 57 | |
| 58 | private static final String ERR_CH_DUPLICATE = "Channel already exists: "; |
| 59 | private static final String ERR_CH_NOT_FOUND = "Channel not found: "; |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 60 | private static final long NUM_BUFFERS = 1024; |
| 61 | private static final short NUM_TABLES = 3; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 62 | |
Claudine Chiu | e2d5acc | 2017-06-08 22:49:21 -0400 | [diff] [blame^] | 63 | private final Logger log; |
| 64 | |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 65 | private final DatapathId dpId; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 66 | private final OFSwitchCapabilities capabilities; |
| 67 | |
Claudine Chiu | e2d5acc | 2017-06-08 22:49:21 -0400 | [diff] [blame^] | 68 | // miss_send_len field (in OFSetConfig and OFGetConfig messages) indicates the max |
| 69 | // bytes of a packet that the switch sends to the controller |
| 70 | private int missSendLen = 0xffff; |
| 71 | |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 72 | private final ConcurrentHashMap<Channel, OFControllerRole> controllerRoleMap |
| 73 | = new ConcurrentHashMap<>(); |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 74 | private static final OFFactory FACTORY = OFFactories.getFactory(OFVersion.OF_13); |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 75 | |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 76 | private int handshakeTransactionIds = -1; |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 77 | |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 78 | private DefaultOFSwitch(DatapathId dpid, OFSwitchCapabilities capabilities) { |
| 79 | this.dpId = dpid; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 80 | this.capabilities = capabilities; |
Claudine Chiu | e2d5acc | 2017-06-08 22:49:21 -0400 | [diff] [blame^] | 81 | log = LoggerFactory.getLogger(getClass().getName() + " : " + dpid); |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 84 | public static DefaultOFSwitch of(DatapathId dpid, OFSwitchCapabilities capabilities) { |
| 85 | checkNotNull(dpid, "DPID cannot be null"); |
| 86 | checkNotNull(capabilities, "OF capabilities cannot be null"); |
| 87 | return new DefaultOFSwitch(dpid, capabilities); |
| 88 | } |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 89 | |
| 90 | @Override |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 91 | public DatapathId dpid() { |
| 92 | return this.dpId; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public OFSwitchCapabilities capabilities() { |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 97 | return this.capabilities; |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void addControllerChannel(Channel channel) { |
| 102 | controllerRoleMap.compute(channel, (ch, existing) -> { |
| 103 | final String error = ERR_CH_DUPLICATE + channel.remoteAddress(); |
| 104 | checkArgument(existing == null, error); |
| 105 | return ROLE_EQUAL; |
| 106 | }); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public void deleteControllerChannel(Channel channel) { |
| 111 | if (controllerRoleMap.remove(channel) == null) { |
| 112 | final String error = ERR_CH_NOT_FOUND + channel.remoteAddress(); |
| 113 | throw new IllegalStateException(error); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void setRole(Channel channel, OFControllerRole role) { |
| 119 | controllerRoleMap.compute(channel, (ch, existing) -> { |
| 120 | final String error = ERR_CH_NOT_FOUND + channel.remoteAddress(); |
| 121 | checkNotNull(existing, error); |
| 122 | return role; |
| 123 | }); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public OFControllerRole role(Channel channel) { |
| 128 | OFControllerRole role = controllerRoleMap.get(channel); |
| 129 | if (role == null) { |
| 130 | final String error = ERR_CH_NOT_FOUND + channel.remoteAddress(); |
| 131 | throw new IllegalStateException(error); |
| 132 | } |
| 133 | return role; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public Set<Channel> controllerChannels() { |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 138 | return ImmutableSet.copyOf(controllerRoleMap.keySet()); |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void processPortAdded(Port port) { |
| 143 | // TODO generate FEATURES_REPLY message and send it to the controller |
Claudine Chiu | e2d5acc | 2017-06-08 22:49:21 -0400 | [diff] [blame^] | 144 | log.debug("Functionality not yet supported for {}", port); |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void processPortDown(Port port) { |
| 149 | // TODO generate PORT_STATUS message and send it to the controller |
Claudine Chiu | e2d5acc | 2017-06-08 22:49:21 -0400 | [diff] [blame^] | 150 | log.debug("Functionality not yet supported for {}", port); |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public void processPortUp(Port port) { |
| 155 | // TODO generate PORT_STATUS message and send it to the controller |
Claudine Chiu | e2d5acc | 2017-06-08 22:49:21 -0400 | [diff] [blame^] | 156 | log.debug("Functionality not yet supported for {}", port); |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void processFlowRemoved(FlowRule flowRule) { |
| 161 | // TODO generate FLOW_REMOVED message and send it to the controller |
Claudine Chiu | e2d5acc | 2017-06-08 22:49:21 -0400 | [diff] [blame^] | 162 | log.debug("Functionality not yet supported for {}", flowRule); |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void processPacketIn(InboundPacket packet) { |
| 167 | // TODO generate PACKET_IN message and send it to the controller |
Claudine Chiu | e2d5acc | 2017-06-08 22:49:21 -0400 | [diff] [blame^] | 168 | log.debug("Functionality not yet supported for {}", packet); |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public void processControllerCommand(Channel channel, OFMessage msg) { |
| 173 | // TODO process controller command |
Claudine Chiu | e2d5acc | 2017-06-08 22:49:21 -0400 | [diff] [blame^] | 174 | log.debug("Functionality not yet supported for {}", msg); |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public void processStatsRequest(Channel channel, OFMessage msg) { |
Claudine Chiu | e2d5acc | 2017-06-08 22:49:21 -0400 | [diff] [blame^] | 179 | if (msg.getType() != OFType.STATS_REQUEST) { |
| 180 | log.warn("Ignoring message of type {}.", msg.getType()); |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | OFStatsRequest ofStatsRequest = (OFStatsRequest) msg; |
| 185 | OFStatsReply ofStatsReply = null; |
| 186 | switch (ofStatsRequest.getStatsType()) { |
| 187 | case PORT_DESC: |
| 188 | ofStatsReply = FACTORY.buildPortDescStatsReply() |
| 189 | .setXid(msg.getXid()) |
| 190 | //TODO add details |
| 191 | .build(); |
| 192 | break; |
| 193 | case METER_FEATURES: |
| 194 | OFMeterFeatures ofMeterFeatures = FACTORY.buildMeterFeatures() |
| 195 | .build(); |
| 196 | ofStatsReply = FACTORY.buildMeterFeaturesStatsReply() |
| 197 | .setXid(msg.getXid()) |
| 198 | .setFeatures(ofMeterFeatures) |
| 199 | //TODO add details |
| 200 | .build(); |
| 201 | break; |
| 202 | case DESC: |
| 203 | ofStatsReply = FACTORY.buildDescStatsReply() |
| 204 | .setXid(msg.getXid()) |
| 205 | .build(); |
| 206 | break; |
| 207 | default: |
| 208 | log.debug("Functionality not yet supported for type {} statsType{} msg {}", |
| 209 | msg.getType(), ofStatsRequest.getStatsType(), msg); |
| 210 | break; |
| 211 | } |
| 212 | |
| 213 | if (ofStatsReply != null) { |
| 214 | log.trace("request {}; reply {}", msg, ofStatsReply); |
| 215 | channel.writeAndFlush(Collections.singletonList(ofStatsReply)); |
| 216 | } |
| 217 | |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public void processRoleRequest(Channel channel, OFMessage msg) { |
| 222 | // TODO process role request and send reply |
Claudine Chiu | e2d5acc | 2017-06-08 22:49:21 -0400 | [diff] [blame^] | 223 | log.debug("Functionality not yet supported for {}", msg); |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public void processFeaturesRequest(Channel channel, OFMessage msg) { |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 228 | OFFeaturesReply ofFeaturesReply = FACTORY.buildFeaturesReply() |
| 229 | .setDatapathId(dpId) |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 230 | .setNBuffers(NUM_BUFFERS) |
| 231 | .setNTables(NUM_TABLES) |
| 232 | .setCapabilities(capabilities.ofSwitchCapabilities()) |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 233 | .setXid(msg.getXid()) |
| 234 | .build(); |
| 235 | channel.writeAndFlush(Collections.singletonList(ofFeaturesReply)); |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public void processLldp(Channel channel, OFMessage msg) { |
| 240 | // TODO process lldp |
| 241 | } |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 242 | |
| 243 | @Override |
| 244 | public void sendOfHello(Channel channel) { |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 245 | OFHello ofHello = FACTORY.buildHello() |
| 246 | .setXid(this.handshakeTransactionIds--) |
| 247 | .build(); |
| 248 | channel.writeAndFlush(Collections.singletonList(ofHello)); |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public void processEchoRequest(Channel channel, OFMessage msg) { |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 253 | OFEchoReply ofEchoReply = FACTORY.buildEchoReply() |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 254 | .setXid(msg.getXid()) |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 255 | .setData(((OFEchoRequest) msg).getData()) |
| 256 | .build(); |
| 257 | channel.writeAndFlush(Collections.singletonList(ofEchoReply)); |
Daniel Park | be6b673 | 2016-11-11 15:52:19 +0900 | [diff] [blame] | 258 | } |
Claudine Chiu | e2d5acc | 2017-06-08 22:49:21 -0400 | [diff] [blame^] | 259 | |
| 260 | @Override |
| 261 | public void processGetConfigRequest(Channel channel, OFMessage msg) { |
| 262 | OFGetConfigReply ofGetConfigReply = FACTORY.buildGetConfigReply() |
| 263 | .setXid(msg.getXid()) |
| 264 | .setMissSendLen(missSendLen) |
| 265 | .build(); |
| 266 | log.trace("request {}; reply {}", msg, ofGetConfigReply); |
| 267 | channel.writeAndFlush(Collections.singletonList(ofGetConfigReply)); |
| 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public void processSetConfigMessage(Channel channel, OFMessage msg) { |
| 272 | OFSetConfig ofSetConfig = (OFSetConfig) msg; |
| 273 | if (missSendLen != ofSetConfig.getMissSendLen()) { |
| 274 | log.trace("Changing missSendLen from {} to {}.", |
| 275 | missSendLen, ofSetConfig.getMissSendLen()); |
| 276 | missSendLen = ofSetConfig.getMissSendLen(); |
| 277 | } |
| 278 | |
| 279 | // SetConfig message is not acknowledged |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public void processBarrierRequest(Channel channel, OFMessage msg) { |
| 284 | // TODO check previous state requests have been handled before issuing BarrierReply |
| 285 | OFBarrierReply ofBarrierReply = FACTORY.buildBarrierReply() |
| 286 | .setXid(msg.getXid()) |
| 287 | .build(); |
| 288 | log.trace("request {}; reply {}", msg, ofBarrierReply); |
| 289 | channel.writeAndFlush(Collections.singletonList(ofBarrierReply)); |
| 290 | } |
Hyunsun Moon | 90163ba | 2016-10-12 13:35:14 -0700 | [diff] [blame] | 291 | } |