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