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 | 28247b5 | 2016-01-07 17:24:15 -0800 | [diff] [blame^] | 95 | protected Set<OpenFlowEventListener> ofOutgoingMsgListener = new CopyOnWriteArraySet<>(); |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 96 | |
| 97 | protected ExecutorService executorMsgs = |
Jian Li | 28247b5 | 2016-01-07 17:24:15 -0800 | [diff] [blame^] | 98 | Executors.newCachedThreadPool(groupedThreads("onos/of", "event-outgoing-msg-stats-%d")); |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 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 | |
Jian Li | 28247b5 | 2016-01-07 17:24:15 -0800 | [diff] [blame^] | 170 | // listen to outgoing control messages only if listeners are registered |
| 171 | if (ofOutgoingMsgListener.size() != 0) { |
| 172 | msgs.forEach(m -> { |
| 173 | if (m.getType() == OFType.PACKET_OUT || |
| 174 | m.getType() == OFType.FLOW_MOD || |
| 175 | m.getType() == OFType.STATS_REQUEST) { |
| 176 | executorMsgs.submit(new OFMessageHandler(dpid, m)); |
| 177 | } |
| 178 | }); |
| 179 | } |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | private void sendMsgsOnChannel(List<OFMessage> msgs) { |
| 183 | if (channel.isConnected()) { |
Thomas Vachuska | 1c681d7 | 2015-05-18 14:58:53 -0700 | [diff] [blame] | 184 | channel.write(msgs); |
Charles Chan | 5b7ec34 | 2015-10-18 20:55:41 -0700 | [diff] [blame] | 185 | } else { |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 186 | log.warn("Dropping messages for switch {} because channel is not connected: {}", |
| 187 | dpid, msgs); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 188 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | @Override |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 192 | public final void sendRoleRequest(OFMessage msg) { |
| 193 | if (msg instanceof OFRoleRequest || |
| 194 | msg instanceof OFNiciraControllerRoleRequest) { |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 195 | sendMsgsOnChannel(Collections.singletonList(msg)); |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 196 | return; |
| 197 | } |
| 198 | throw new IllegalArgumentException("Someone is trying to send " + |
| 199 | "a non role request message"); |
| 200 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 201 | |
Ayaka Koshibe | 3c24077 | 2015-05-20 16:23:40 -0700 | [diff] [blame] | 202 | @Override |
alshabib | a2df7b2a | 2015-05-06 13:57:10 -0700 | [diff] [blame] | 203 | public final void sendHandshakeMessage(OFMessage message) { |
| 204 | if (!this.isDriverHandshakeComplete()) { |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 205 | sendMsgsOnChannel(Collections.singletonList(message)); |
alshabib | a2df7b2a | 2015-05-06 13:57:10 -0700 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 209 | @Override |
| 210 | public final boolean isConnected() { |
| 211 | return this.connected; |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public final void setConnected(boolean connected) { |
| 216 | this.connected = connected; |
Thomas Vachuska | 1c681d7 | 2015-05-18 14:58:53 -0700 | [diff] [blame] | 217 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 218 | |
| 219 | @Override |
| 220 | public final void setChannel(Channel channel) { |
| 221 | this.channel = channel; |
Ray Milkey | e53f171 | 2015-01-16 09:17:16 -0800 | [diff] [blame] | 222 | final SocketAddress address = channel.getRemoteAddress(); |
| 223 | if (address instanceof InetSocketAddress) { |
| 224 | final InetSocketAddress inetAddress = (InetSocketAddress) address; |
| 225 | final IpAddress ipAddress = IpAddress.valueOf(inetAddress.getAddress()); |
Pavlin Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 226 | if (ipAddress.isIp4()) { |
Ray Milkey | e53f171 | 2015-01-16 09:17:16 -0800 | [diff] [blame] | 227 | channelId = ipAddress.toString() + ':' + inetAddress.getPort(); |
| 228 | } else { |
| 229 | channelId = '[' + ipAddress.toString() + "]:" + inetAddress.getPort(); |
| 230 | } |
| 231 | } |
Thomas Vachuska | 1c681d7 | 2015-05-18 14:58:53 -0700 | [diff] [blame] | 232 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 233 | |
Ray Milkey | e53f171 | 2015-01-16 09:17:16 -0800 | [diff] [blame] | 234 | @Override |
| 235 | public String channelId() { |
| 236 | return channelId; |
| 237 | } |
| 238 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 239 | //************************ |
| 240 | // Switch features related |
| 241 | //************************ |
| 242 | |
| 243 | @Override |
| 244 | public final long getId() { |
| 245 | return this.dpid.value(); |
Thomas Vachuska | 1c681d7 | 2015-05-18 14:58:53 -0700 | [diff] [blame] | 246 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 247 | |
| 248 | @Override |
| 249 | public final String getStringId() { |
| 250 | return this.dpid.toString(); |
| 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public final void setOFVersion(OFVersion ofV) { |
| 255 | this.ofVersion = ofV; |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public void setTableFull(boolean full) { |
| 260 | this.tableFull = full; |
| 261 | } |
| 262 | |
| 263 | @Override |
| 264 | public void setFeaturesReply(OFFeaturesReply featuresReply) { |
| 265 | this.features = featuresReply; |
| 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public abstract Boolean supportNxRole(); |
| 270 | |
| 271 | //************************ |
| 272 | // Message handling |
| 273 | //************************ |
| 274 | /** |
| 275 | * Handle the message coming from the dataplane. |
| 276 | * |
| 277 | * @param m the actual message |
| 278 | */ |
| 279 | @Override |
| 280 | public final void handleMessage(OFMessage m) { |
Thomas Vachuska | 3927446 | 2014-12-02 13:23:50 -0800 | [diff] [blame] | 281 | if (this.role == RoleState.MASTER || m instanceof OFPortStatus) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 282 | this.agent.processMessage(dpid, m); |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 283 | } else { |
| 284 | log.trace("Dropping received message {}, was not MASTER", m); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 285 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public RoleState getRole() { |
| 290 | return role; |
Thomas Vachuska | 1c681d7 | 2015-05-18 14:58:53 -0700 | [diff] [blame] | 291 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 292 | |
| 293 | @Override |
| 294 | public final boolean connectSwitch() { |
| 295 | return this.agent.addConnectedSwitch(dpid, this); |
| 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public final boolean activateMasterSwitch() { |
| 300 | return this.agent.addActivatedMasterSwitch(dpid, this); |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public final boolean activateEqualSwitch() { |
| 305 | return this.agent.addActivatedEqualSwitch(dpid, this); |
| 306 | } |
| 307 | |
| 308 | @Override |
| 309 | public final void transitionToEqualSwitch() { |
| 310 | this.agent.transitionToEqualSwitch(dpid); |
| 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public final void transitionToMasterSwitch() { |
| 315 | this.agent.transitionToMasterSwitch(dpid); |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 316 | synchronized (messagesPendingMastership) { |
| 317 | List<OFMessage> messages = messagesPendingMastership.get(); |
| 318 | if (messages != null) { |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 319 | // Cannot use sendMsg here. It will only append to pending list. |
| 320 | sendMsgsOnChannel(messages); |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 321 | log.debug("Sending {} pending messages to switch {}", |
| 322 | messages.size(), dpid); |
| 323 | messagesPendingMastership.set(null); |
| 324 | } |
| 325 | // perform role transition after clearing messages queue |
| 326 | this.role = RoleState.MASTER; |
Charles Chan | 5b7ec34 | 2015-10-18 20:55:41 -0700 | [diff] [blame] | 327 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | @Override |
| 331 | public final void removeConnectedSwitch() { |
| 332 | this.agent.removeConnectedSwitch(dpid); |
| 333 | } |
| 334 | |
| 335 | @Override |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 336 | public void addEventListener(OpenFlowEventListener listener) { |
Jian Li | 28247b5 | 2016-01-07 17:24:15 -0800 | [diff] [blame^] | 337 | ofOutgoingMsgListener.add(listener); |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | @Override |
| 341 | public void removeEventListener(OpenFlowEventListener listener) { |
Jian Li | 28247b5 | 2016-01-07 17:24:15 -0800 | [diff] [blame^] | 342 | ofOutgoingMsgListener.remove(listener); |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | @Override |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 346 | public OFFactory factory() { |
| 347 | return OFFactories.getFactory(ofVersion); |
| 348 | } |
| 349 | |
| 350 | @Override |
| 351 | public void setPortDescReply(OFPortDescStatsReply portDescReply) { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 352 | this.ports.add(portDescReply); |
| 353 | } |
| 354 | |
| 355 | @Override |
| 356 | public void setPortDescReplies(List<OFPortDescStatsReply> portDescReplies) { |
| 357 | this.ports.addAll(portDescReplies); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | @Override |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 361 | public void returnRoleReply(RoleState requested, RoleState response) { |
| 362 | this.agent.returnRoleReply(dpid, requested, response); |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | @Override |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 366 | public abstract void startDriverHandshake(); |
| 367 | |
| 368 | @Override |
| 369 | public abstract boolean isDriverHandshakeComplete(); |
| 370 | |
| 371 | @Override |
| 372 | public abstract void processDriverHandshakeMessage(OFMessage m); |
| 373 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 374 | |
| 375 | // Role Handling |
| 376 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 377 | @Override |
| 378 | public void setRole(RoleState role) { |
| 379 | try { |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 380 | if (role == RoleState.SLAVE || role == RoleState.EQUAL) { |
| 381 | // perform role transition to SLAVE/EQUAL before sending role request |
| 382 | this.role = role; |
| 383 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 384 | if (this.roleMan.sendRoleRequest(role, RoleRecvStatus.MATCHED_SET_ROLE)) { |
Madan Jampani | f2af771 | 2015-05-29 18:43:52 -0700 | [diff] [blame] | 385 | log.debug("Sending role {} to switch {}", role, getStringId()); |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 386 | if (role == RoleState.MASTER) { |
| 387 | synchronized (messagesPendingMastership) { |
| 388 | if (messagesPendingMastership.get() == null) { |
| 389 | log.debug("Initializing new message queue for switch {}", dpid); |
| 390 | /* |
| 391 | The presence of messagesPendingMastership indicates that |
| 392 | a switch is currently transitioning to MASTER, but |
| 393 | is still awaiting role reply from switch. |
| 394 | */ |
| 395 | messagesPendingMastership.set(Lists.newArrayList()); |
| 396 | } |
Charles Chan | 5b7ec34 | 2015-10-18 20:55:41 -0700 | [diff] [blame] | 397 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 398 | } |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 399 | } else if (role == RoleState.MASTER) { |
| 400 | // role request not support; transition switch to MASTER |
alshabib | 7814e9f | 2014-09-30 11:52:12 -0700 | [diff] [blame] | 401 | this.role = role; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 402 | } |
| 403 | } catch (IOException e) { |
| 404 | log.error("Unable to write to switch {}.", this.dpid); |
| 405 | } |
| 406 | } |
| 407 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 408 | @Override |
| 409 | public void reassertRole() { |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 410 | // TODO should messages be sent directly or queue during reassertion? |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 411 | if (this.getRole() == RoleState.MASTER) { |
| 412 | log.warn("Received permission error from switch {} while " + |
| 413 | "being master. Reasserting master role.", |
| 414 | this.getStringId()); |
| 415 | this.setRole(RoleState.MASTER); |
| 416 | } |
| 417 | } |
| 418 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 419 | @Override |
| 420 | public void handleRole(OFMessage m) throws SwitchStateException { |
| 421 | RoleReplyInfo rri = roleMan.extractOFRoleReply((OFRoleReply) m); |
| 422 | RoleRecvStatus rrs = roleMan.deliverRoleReply(rri); |
| 423 | if (rrs == RoleRecvStatus.MATCHED_SET_ROLE) { |
| 424 | if (rri.getRole() == RoleState.MASTER) { |
| 425 | this.transitionToMasterSwitch(); |
| 426 | } else if (rri.getRole() == RoleState.EQUAL || |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 427 | rri.getRole() == RoleState.SLAVE) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 428 | this.transitionToEqualSwitch(); |
| 429 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 430 | } else { |
alshabib | 4785eec | 2014-12-04 16:45:45 -0800 | [diff] [blame] | 431 | log.warn("Failed to set role for {}", this.getStringId()); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
| 435 | @Override |
| 436 | public void handleNiciraRole(OFMessage m) throws SwitchStateException { |
| 437 | RoleState r = this.roleMan.extractNiciraRoleReply((OFExperimenter) m); |
| 438 | if (r == null) { |
| 439 | // The message wasn't really a Nicira role reply. We just |
| 440 | // dispatch it to the OFMessage listeners in this case. |
| 441 | this.handleMessage(m); |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 442 | return; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | RoleRecvStatus rrs = this.roleMan.deliverRoleReply( |
| 446 | new RoleReplyInfo(r, null, m.getXid())); |
| 447 | if (rrs == RoleRecvStatus.MATCHED_SET_ROLE) { |
| 448 | if (r == RoleState.MASTER) { |
| 449 | this.transitionToMasterSwitch(); |
| 450 | } else if (r == RoleState.EQUAL || |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 451 | r == RoleState.SLAVE) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 452 | this.transitionToEqualSwitch(); |
| 453 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 454 | } else { |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 455 | this.disconnectSwitch(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | |
| 459 | @Override |
| 460 | public boolean handleRoleError(OFErrorMsg error) { |
| 461 | try { |
| 462 | return RoleRecvStatus.OTHER_EXPECTATION != this.roleMan.deliverError(error); |
| 463 | } catch (SwitchStateException e) { |
| 464 | this.disconnectSwitch(); |
| 465 | } |
| 466 | return true; |
| 467 | } |
| 468 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 469 | @Override |
| 470 | public final void setAgent(OpenFlowAgent ag) { |
| 471 | if (this.agent == null) { |
| 472 | this.agent = ag; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | @Override |
| 477 | public final void setRoleHandler(RoleHandler roleHandler) { |
| 478 | if (this.roleMan == null) { |
| 479 | this.roleMan = roleHandler; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | @Override |
| 484 | public void setSwitchDescription(OFDescStatsReply d) { |
| 485 | this.desc = d; |
| 486 | } |
| 487 | |
| 488 | @Override |
| 489 | public int getNextTransactionId() { |
| 490 | return this.xidCounter.getAndIncrement(); |
| 491 | } |
| 492 | |
| 493 | @Override |
| 494 | public List<OFPortDesc> getPorts() { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 495 | return this.ports.stream() |
Brian O'Connor | e755caa | 2015-11-16 16:43:09 -0800 | [diff] [blame] | 496 | .flatMap(portReply -> portReply.getEntries().stream()) |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 497 | .collect(Collectors.toList()); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | @Override |
Ray Milkey | d3edd03 | 2015-01-16 11:38:58 -0800 | [diff] [blame] | 501 | public String manufacturerDescription() { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 502 | return this.desc.getMfrDesc(); |
| 503 | } |
| 504 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 505 | @Override |
| 506 | public String datapathDescription() { |
| 507 | return this.desc.getDpDesc(); |
| 508 | } |
| 509 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 510 | @Override |
| 511 | public String hardwareDescription() { |
| 512 | return this.desc.getHwDesc(); |
| 513 | } |
| 514 | |
| 515 | @Override |
| 516 | public String softwareDescription() { |
| 517 | return this.desc.getSwDesc(); |
| 518 | } |
| 519 | |
| 520 | @Override |
| 521 | public String serialNumber() { |
| 522 | return this.desc.getSerialNum(); |
| 523 | } |
| 524 | |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 525 | @Override |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 526 | public Device.Type deviceType() { |
| 527 | return Device.Type.SWITCH; |
Praseed Balakrishnan | a22eadf | 2014-10-20 14:21:45 -0700 | [diff] [blame] | 528 | } |
| 529 | |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 530 | @Override |
| 531 | public String toString() { |
| 532 | return this.getClass().getName() + " [" + ((channel != null) |
| 533 | ? channel.getRemoteAddress() : "?") |
| 534 | + " DPID[" + ((getStringId() != null) ? getStringId() : "?") + "]]"; |
| 535 | } |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 536 | |
| 537 | /** |
| 538 | * OpenFlow message handler for outgoing control messages. |
| 539 | */ |
| 540 | protected final class OFMessageHandler implements Runnable { |
| 541 | |
| 542 | protected final OFMessage msg; |
| 543 | protected final Dpid dpid; |
| 544 | |
| 545 | public OFMessageHandler(Dpid dpid, OFMessage msg) { |
| 546 | this.msg = msg; |
| 547 | this.dpid = dpid; |
| 548 | } |
| 549 | |
| 550 | @Override |
| 551 | public void run() { |
Jian Li | 28247b5 | 2016-01-07 17:24:15 -0800 | [diff] [blame^] | 552 | for (OpenFlowEventListener listener : ofOutgoingMsgListener) { |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 553 | listener.handleMessage(dpid, msg); |
| 554 | } |
| 555 | } |
| 556 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 557 | } |