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