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