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 |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -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 |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -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.driver; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 18 | |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 20 | import org.jboss.netty.channel.Channel; |
Ray Milkey | e53f171 | 2015-01-16 09:17:16 -0800 | [diff] [blame] | 21 | import org.onlab.packet.IpAddress; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 22 | import org.onosproject.net.Device; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 23 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.openflow.controller.Dpid; |
| 25 | import org.onosproject.openflow.controller.RoleState; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 26 | import org.projectfloodlight.openflow.protocol.OFDescStatsReply; |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 27 | import org.projectfloodlight.openflow.protocol.OFErrorMsg; |
Marc De Leenheer | c662d32 | 2016-02-18 16:05:10 -0800 | [diff] [blame] | 28 | import org.projectfloodlight.openflow.protocol.OFExperimenter; |
| 29 | import org.projectfloodlight.openflow.protocol.OFFactories; |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 30 | import org.projectfloodlight.openflow.protocol.OFFactory; |
Marc De Leenheer | c662d32 | 2016-02-18 16:05:10 -0800 | [diff] [blame] | 31 | import org.projectfloodlight.openflow.protocol.OFFeaturesReply; |
| 32 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 33 | import org.projectfloodlight.openflow.protocol.OFNiciraControllerRoleRequest; |
| 34 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
| 35 | import org.projectfloodlight.openflow.protocol.OFPortDescStatsReply; |
| 36 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 37 | import org.projectfloodlight.openflow.protocol.OFRoleReply; |
Marc De Leenheer | c662d32 | 2016-02-18 16:05:10 -0800 | [diff] [blame] | 38 | import org.projectfloodlight.openflow.protocol.OFRoleRequest; |
Marc De Leenheer | c662d32 | 2016-02-18 16:05:10 -0800 | [diff] [blame] | 39 | import org.projectfloodlight.openflow.protocol.OFVersion; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | import org.slf4j.LoggerFactory; |
| 42 | |
Thomas Vachuska | 1c681d7 | 2015-05-18 14:58:53 -0700 | [diff] [blame] | 43 | import java.io.IOException; |
| 44 | import java.net.InetSocketAddress; |
| 45 | import java.net.SocketAddress; |
| 46 | import java.util.ArrayList; |
| 47 | import java.util.Collections; |
| 48 | import java.util.List; |
| 49 | import java.util.concurrent.atomic.AtomicInteger; |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 50 | import java.util.concurrent.atomic.AtomicReference; |
Thomas Vachuska | 1c681d7 | 2015-05-18 14:58:53 -0700 | [diff] [blame] | 51 | import java.util.stream.Collectors; |
| 52 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 53 | /** |
| 54 | * An abstract representation of an OpenFlow switch. Can be extended by others |
| 55 | * to serve as a base for their vendor specific representation of a switch. |
| 56 | */ |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 57 | public abstract class AbstractOpenFlowSwitch extends AbstractHandlerBehaviour |
| 58 | implements OpenFlowSwitchDriver { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 59 | |
Yuta HIGUCHI | f5416d8 | 2014-10-24 21:17:40 -0700 | [diff] [blame] | 60 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 61 | |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 62 | private Channel channel; |
Ray Milkey | e53f171 | 2015-01-16 09:17:16 -0800 | [diff] [blame] | 63 | protected String channelId; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 64 | |
| 65 | private boolean connected; |
| 66 | protected boolean startDriverHandshakeCalled = false; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 67 | private Dpid dpid; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 68 | private OpenFlowAgent agent; |
| 69 | private final AtomicInteger xidCounter = new AtomicInteger(0); |
| 70 | |
| 71 | private OFVersion ofVersion; |
| 72 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 73 | protected List<OFPortDescStatsReply> ports = new ArrayList<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 74 | |
| 75 | protected boolean tableFull; |
| 76 | |
| 77 | private RoleHandler roleMan; |
| 78 | |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 79 | // TODO this is accessed from multiple threads, but volatile may have performance implications |
| 80 | protected volatile RoleState role; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 81 | |
| 82 | protected OFFeaturesReply features; |
| 83 | protected OFDescStatsReply desc; |
| 84 | |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 85 | // messagesPendingMastership is used as synchronization variable for |
| 86 | // all mastership related changes. In this block, mastership (including |
| 87 | // role update) will have either occurred or not. |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 88 | private final AtomicReference<List<OFMessage>> messagesPendingMastership |
| 89 | = new AtomicReference<>(); |
Charles Chan | 5b7ec34 | 2015-10-18 20:55:41 -0700 | [diff] [blame] | 90 | |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 91 | @Override |
| 92 | public void init(Dpid dpid, OFDescStatsReply desc, OFVersion ofv) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 93 | this.dpid = dpid; |
| 94 | this.desc = desc; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 95 | this.ofVersion = ofv; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | //************************ |
| 99 | // Channel related |
| 100 | //************************ |
| 101 | |
| 102 | @Override |
| 103 | public final void disconnectSwitch() { |
Charles Chan | ecfdfb7 | 2015-11-24 19:05:50 -0800 | [diff] [blame] | 104 | setConnected(false); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 105 | this.channel.close(); |
| 106 | } |
| 107 | |
| 108 | @Override |
Charles Chan | 5b7ec34 | 2015-10-18 20:55:41 -0700 | [diff] [blame] | 109 | public void sendMsg(OFMessage msg) { |
| 110 | this.sendMsg(Collections.singletonList(msg)); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public final void sendMsg(List<OFMessage> msgs) { |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 115 | /* |
| 116 | It is possible that in this block, we transition to SLAVE/EQUAL. |
| 117 | If this is the case, the supplied messages will race with the |
| 118 | RoleRequest message, and they could be rejected by the switch. |
| 119 | In the interest of performance, we will not protect this block with |
| 120 | a synchronization primitive, because the message would have just been |
| 121 | dropped anyway. |
| 122 | */ |
Jian Li | a78cdb2 | 2016-04-21 13:03:58 -0700 | [diff] [blame^] | 123 | |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 124 | if (role == RoleState.MASTER) { |
| 125 | // fast path send when we are master |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 126 | sendMsgsOnChannel(msgs); |
| 127 | return; |
| 128 | } |
| 129 | // check to see if mastership transition is in progress |
| 130 | synchronized (messagesPendingMastership) { |
| 131 | /* |
| 132 | messagesPendingMastership is used as synchronization variable for |
| 133 | all mastership related changes. In this block, mastership (including |
| 134 | role update) will have either occurred or not. |
| 135 | */ |
| 136 | if (role == RoleState.MASTER) { |
| 137 | // transition to MASTER complete, send messages |
| 138 | sendMsgsOnChannel(msgs); |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | List<OFMessage> messages = messagesPendingMastership.get(); |
| 143 | if (messages != null) { |
| 144 | // we are transitioning to MASTER, so add messages to queue |
| 145 | messages.addAll(msgs); |
| 146 | log.debug("Enqueue message for switch {}. queue size after is {}", |
| 147 | dpid, messages.size()); |
| 148 | } else { |
| 149 | // not transitioning to MASTER |
| 150 | log.warn("Dropping message for switch {} (role: {}, connected: {}): {}", |
| 151 | dpid, role, channel.isConnected(), msgs); |
| 152 | } |
| 153 | } |
Jian Li | 1111197 | 2016-04-01 23:49:00 -0700 | [diff] [blame] | 154 | } |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 155 | |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 156 | private void sendMsgsOnChannel(List<OFMessage> msgs) { |
| 157 | if (channel.isConnected()) { |
Thomas Vachuska | 1c681d7 | 2015-05-18 14:58:53 -0700 | [diff] [blame] | 158 | channel.write(msgs); |
Jian Li | a78cdb2 | 2016-04-21 13:03:58 -0700 | [diff] [blame^] | 159 | agent.processDownstreamMessage(dpid, msgs); |
Charles Chan | 5b7ec34 | 2015-10-18 20:55:41 -0700 | [diff] [blame] | 160 | } else { |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 161 | log.warn("Dropping messages for switch {} because channel is not connected: {}", |
| 162 | dpid, msgs); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 163 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | @Override |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 167 | public final void sendRoleRequest(OFMessage msg) { |
| 168 | if (msg instanceof OFRoleRequest || |
| 169 | msg instanceof OFNiciraControllerRoleRequest) { |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 170 | sendMsgsOnChannel(Collections.singletonList(msg)); |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 171 | return; |
| 172 | } |
| 173 | throw new IllegalArgumentException("Someone is trying to send " + |
| 174 | "a non role request message"); |
| 175 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 176 | |
Ayaka Koshibe | 3c24077 | 2015-05-20 16:23:40 -0700 | [diff] [blame] | 177 | @Override |
Marc De Leenheer | c662d32 | 2016-02-18 16:05:10 -0800 | [diff] [blame] | 178 | public final void |
| 179 | sendHandshakeMessage(OFMessage message) { |
alshabib | a2df7b2a | 2015-05-06 13:57:10 -0700 | [diff] [blame] | 180 | if (!this.isDriverHandshakeComplete()) { |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 181 | sendMsgsOnChannel(Collections.singletonList(message)); |
alshabib | a2df7b2a | 2015-05-06 13:57:10 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 185 | @Override |
| 186 | public final boolean isConnected() { |
| 187 | return this.connected; |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public final void setConnected(boolean connected) { |
| 192 | this.connected = connected; |
Thomas Vachuska | 1c681d7 | 2015-05-18 14:58:53 -0700 | [diff] [blame] | 193 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 194 | |
| 195 | @Override |
| 196 | public final void setChannel(Channel channel) { |
| 197 | this.channel = channel; |
Ray Milkey | e53f171 | 2015-01-16 09:17:16 -0800 | [diff] [blame] | 198 | final SocketAddress address = channel.getRemoteAddress(); |
| 199 | if (address instanceof InetSocketAddress) { |
| 200 | final InetSocketAddress inetAddress = (InetSocketAddress) address; |
| 201 | final IpAddress ipAddress = IpAddress.valueOf(inetAddress.getAddress()); |
Pavlin Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 202 | if (ipAddress.isIp4()) { |
Ray Milkey | e53f171 | 2015-01-16 09:17:16 -0800 | [diff] [blame] | 203 | channelId = ipAddress.toString() + ':' + inetAddress.getPort(); |
| 204 | } else { |
| 205 | channelId = '[' + ipAddress.toString() + "]:" + inetAddress.getPort(); |
| 206 | } |
| 207 | } |
Thomas Vachuska | 1c681d7 | 2015-05-18 14:58:53 -0700 | [diff] [blame] | 208 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 209 | |
Ray Milkey | e53f171 | 2015-01-16 09:17:16 -0800 | [diff] [blame] | 210 | @Override |
| 211 | public String channelId() { |
| 212 | return channelId; |
| 213 | } |
| 214 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 215 | //************************ |
| 216 | // Switch features related |
| 217 | //************************ |
| 218 | |
| 219 | @Override |
| 220 | public final long getId() { |
| 221 | return this.dpid.value(); |
Thomas Vachuska | 1c681d7 | 2015-05-18 14:58:53 -0700 | [diff] [blame] | 222 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 223 | |
| 224 | @Override |
| 225 | public final String getStringId() { |
| 226 | return this.dpid.toString(); |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public final void setOFVersion(OFVersion ofV) { |
| 231 | this.ofVersion = ofV; |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public void setTableFull(boolean full) { |
| 236 | this.tableFull = full; |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public void setFeaturesReply(OFFeaturesReply featuresReply) { |
| 241 | this.features = featuresReply; |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public abstract Boolean supportNxRole(); |
| 246 | |
| 247 | //************************ |
| 248 | // Message handling |
| 249 | //************************ |
| 250 | /** |
| 251 | * Handle the message coming from the dataplane. |
| 252 | * |
| 253 | * @param m the actual message |
| 254 | */ |
| 255 | @Override |
| 256 | public final void handleMessage(OFMessage m) { |
Thomas Vachuska | 3927446 | 2014-12-02 13:23:50 -0800 | [diff] [blame] | 257 | if (this.role == RoleState.MASTER || m instanceof OFPortStatus) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 258 | this.agent.processMessage(dpid, m); |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 259 | } else { |
| 260 | log.trace("Dropping received message {}, was not MASTER", m); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 261 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public RoleState getRole() { |
| 266 | return role; |
Thomas Vachuska | 1c681d7 | 2015-05-18 14:58:53 -0700 | [diff] [blame] | 267 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 268 | |
| 269 | @Override |
| 270 | public final boolean connectSwitch() { |
| 271 | return this.agent.addConnectedSwitch(dpid, this); |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public final boolean activateMasterSwitch() { |
| 276 | return this.agent.addActivatedMasterSwitch(dpid, this); |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public final boolean activateEqualSwitch() { |
| 281 | return this.agent.addActivatedEqualSwitch(dpid, this); |
| 282 | } |
| 283 | |
| 284 | @Override |
| 285 | public final void transitionToEqualSwitch() { |
| 286 | this.agent.transitionToEqualSwitch(dpid); |
| 287 | } |
| 288 | |
| 289 | @Override |
| 290 | public final void transitionToMasterSwitch() { |
| 291 | this.agent.transitionToMasterSwitch(dpid); |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 292 | synchronized (messagesPendingMastership) { |
| 293 | List<OFMessage> messages = messagesPendingMastership.get(); |
| 294 | if (messages != null) { |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 295 | // Cannot use sendMsg here. It will only append to pending list. |
| 296 | sendMsgsOnChannel(messages); |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 297 | log.debug("Sending {} pending messages to switch {}", |
| 298 | messages.size(), dpid); |
| 299 | messagesPendingMastership.set(null); |
| 300 | } |
| 301 | // perform role transition after clearing messages queue |
| 302 | this.role = RoleState.MASTER; |
Charles Chan | 5b7ec34 | 2015-10-18 20:55:41 -0700 | [diff] [blame] | 303 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | @Override |
| 307 | public final void removeConnectedSwitch() { |
| 308 | this.agent.removeConnectedSwitch(dpid); |
| 309 | } |
| 310 | |
| 311 | @Override |
| 312 | public OFFactory factory() { |
| 313 | return OFFactories.getFactory(ofVersion); |
| 314 | } |
| 315 | |
| 316 | @Override |
| 317 | public void setPortDescReply(OFPortDescStatsReply portDescReply) { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 318 | this.ports.add(portDescReply); |
| 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public void setPortDescReplies(List<OFPortDescStatsReply> portDescReplies) { |
| 323 | this.ports.addAll(portDescReplies); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | @Override |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 327 | public void returnRoleReply(RoleState requested, RoleState response) { |
| 328 | this.agent.returnRoleReply(dpid, requested, response); |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | @Override |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 332 | public abstract void startDriverHandshake(); |
| 333 | |
| 334 | @Override |
| 335 | public abstract boolean isDriverHandshakeComplete(); |
| 336 | |
| 337 | @Override |
| 338 | public abstract void processDriverHandshakeMessage(OFMessage m); |
| 339 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 340 | |
| 341 | // Role Handling |
| 342 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 343 | @Override |
| 344 | public void setRole(RoleState role) { |
| 345 | try { |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 346 | if (role == RoleState.SLAVE || role == RoleState.EQUAL) { |
| 347 | // perform role transition to SLAVE/EQUAL before sending role request |
| 348 | this.role = role; |
| 349 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 350 | if (this.roleMan.sendRoleRequest(role, RoleRecvStatus.MATCHED_SET_ROLE)) { |
Madan Jampani | f2af771 | 2015-05-29 18:43:52 -0700 | [diff] [blame] | 351 | log.debug("Sending role {} to switch {}", role, getStringId()); |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 352 | if (role == RoleState.MASTER) { |
| 353 | synchronized (messagesPendingMastership) { |
| 354 | if (messagesPendingMastership.get() == null) { |
| 355 | log.debug("Initializing new message queue for switch {}", dpid); |
| 356 | /* |
| 357 | The presence of messagesPendingMastership indicates that |
| 358 | a switch is currently transitioning to MASTER, but |
| 359 | is still awaiting role reply from switch. |
| 360 | */ |
| 361 | messagesPendingMastership.set(Lists.newArrayList()); |
| 362 | } |
Charles Chan | 5b7ec34 | 2015-10-18 20:55:41 -0700 | [diff] [blame] | 363 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 364 | } |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 365 | } else if (role == RoleState.MASTER) { |
| 366 | // role request not support; transition switch to MASTER |
alshabib | 7814e9f | 2014-09-30 11:52:12 -0700 | [diff] [blame] | 367 | this.role = role; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 368 | } |
| 369 | } catch (IOException e) { |
| 370 | log.error("Unable to write to switch {}.", this.dpid); |
| 371 | } |
| 372 | } |
| 373 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 374 | @Override |
| 375 | public void reassertRole() { |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 376 | // TODO should messages be sent directly or queue during reassertion? |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 377 | if (this.getRole() == RoleState.MASTER) { |
| 378 | log.warn("Received permission error from switch {} while " + |
| 379 | "being master. Reasserting master role.", |
| 380 | this.getStringId()); |
| 381 | this.setRole(RoleState.MASTER); |
| 382 | } |
| 383 | } |
| 384 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 385 | @Override |
| 386 | public void handleRole(OFMessage m) throws SwitchStateException { |
| 387 | RoleReplyInfo rri = roleMan.extractOFRoleReply((OFRoleReply) m); |
| 388 | RoleRecvStatus rrs = roleMan.deliverRoleReply(rri); |
| 389 | if (rrs == RoleRecvStatus.MATCHED_SET_ROLE) { |
| 390 | if (rri.getRole() == RoleState.MASTER) { |
| 391 | this.transitionToMasterSwitch(); |
| 392 | } else if (rri.getRole() == RoleState.EQUAL || |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 393 | rri.getRole() == RoleState.SLAVE) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 394 | this.transitionToEqualSwitch(); |
| 395 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 396 | } else { |
alshabib | 4785eec | 2014-12-04 16:45:45 -0800 | [diff] [blame] | 397 | log.warn("Failed to set role for {}", this.getStringId()); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
| 401 | @Override |
| 402 | public void handleNiciraRole(OFMessage m) throws SwitchStateException { |
| 403 | RoleState r = this.roleMan.extractNiciraRoleReply((OFExperimenter) m); |
| 404 | if (r == null) { |
| 405 | // The message wasn't really a Nicira role reply. We just |
| 406 | // dispatch it to the OFMessage listeners in this case. |
| 407 | this.handleMessage(m); |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 408 | return; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | RoleRecvStatus rrs = this.roleMan.deliverRoleReply( |
| 412 | new RoleReplyInfo(r, null, m.getXid())); |
| 413 | if (rrs == RoleRecvStatus.MATCHED_SET_ROLE) { |
| 414 | if (r == RoleState.MASTER) { |
| 415 | this.transitionToMasterSwitch(); |
| 416 | } else if (r == RoleState.EQUAL || |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 417 | r == RoleState.SLAVE) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 418 | this.transitionToEqualSwitch(); |
| 419 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 420 | } else { |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 421 | this.disconnectSwitch(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
| 425 | @Override |
| 426 | public boolean handleRoleError(OFErrorMsg error) { |
| 427 | try { |
| 428 | return RoleRecvStatus.OTHER_EXPECTATION != this.roleMan.deliverError(error); |
| 429 | } catch (SwitchStateException e) { |
| 430 | this.disconnectSwitch(); |
| 431 | } |
| 432 | return true; |
| 433 | } |
| 434 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 435 | @Override |
| 436 | public final void setAgent(OpenFlowAgent ag) { |
| 437 | if (this.agent == null) { |
| 438 | this.agent = ag; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | @Override |
| 443 | public final void setRoleHandler(RoleHandler roleHandler) { |
| 444 | if (this.roleMan == null) { |
| 445 | this.roleMan = roleHandler; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | @Override |
| 450 | public void setSwitchDescription(OFDescStatsReply d) { |
| 451 | this.desc = d; |
| 452 | } |
| 453 | |
| 454 | @Override |
| 455 | public int getNextTransactionId() { |
| 456 | return this.xidCounter.getAndIncrement(); |
| 457 | } |
| 458 | |
| 459 | @Override |
| 460 | public List<OFPortDesc> getPorts() { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 461 | return this.ports.stream() |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 462 | .flatMap(portReply -> portReply.getEntries().stream()) |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 463 | .collect(Collectors.toList()); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | @Override |
Ray Milkey | d3edd03 | 2015-01-16 11:38:58 -0800 | [diff] [blame] | 467 | public String manufacturerDescription() { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 468 | return this.desc.getMfrDesc(); |
| 469 | } |
| 470 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 471 | @Override |
| 472 | public String datapathDescription() { |
| 473 | return this.desc.getDpDesc(); |
| 474 | } |
| 475 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 476 | @Override |
| 477 | public String hardwareDescription() { |
| 478 | return this.desc.getHwDesc(); |
| 479 | } |
| 480 | |
| 481 | @Override |
| 482 | public String softwareDescription() { |
| 483 | return this.desc.getSwDesc(); |
| 484 | } |
| 485 | |
| 486 | @Override |
| 487 | public String serialNumber() { |
| 488 | return this.desc.getSerialNum(); |
| 489 | } |
| 490 | |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 491 | @Override |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 492 | public Device.Type deviceType() { |
| 493 | return Device.Type.SWITCH; |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 494 | } |
| 495 | |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 496 | @Override |
| 497 | public String toString() { |
| 498 | return this.getClass().getName() + " [" + ((channel != null) |
| 499 | ? channel.getRemoteAddress() : "?") |
| 500 | + " DPID[" + ((getStringId() != null) ? getStringId() : "?") + "]]"; |
| 501 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 502 | } |