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