blob: 317fda8e27e97d97393ba02cc2710d13044e3ce7 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
alshabib7911a052014-10-16 17:49:37 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
alshabib7911a052014-10-16 17:49:37 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
Ray Milkey957390e2016-02-09 10:02:46 -080016package org.onosproject.provider.lldpcommon;
alshabib7911a052014-10-16 17:49:37 -070017
Ray Milkey1ab441b2018-03-06 15:33:44 -080018import com.google.common.collect.ImmutableMap;
jaegonkimd9119d0d2018-02-06 23:16:52 +090019import com.google.common.collect.Maps;
Yuta HIGUCHI19afc032017-05-20 23:44:17 -070020import io.netty.util.Timeout;
21import io.netty.util.TimerTask;
jaegonkimaaa4f832018-03-11 00:48:36 +090022import io.netty.util.internal.StringUtil;
Jonathan Harte8600eb2015-01-12 10:30:45 -080023import org.onlab.packet.Ethernet;
Charles Chan928ff8b2017-05-04 12:22:54 -070024import org.onlab.packet.MacAddress;
Jonathan Harte8600eb2015-01-12 10:30:45 -080025import org.onlab.packet.ONOSLLDP;
26import org.onlab.util.Timer;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.Device;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.Link.Type;
Thomas Vachuska05453c92015-09-09 14:40:49 -070031import org.onosproject.net.LinkKey;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.Port;
33import org.onosproject.net.PortNumber;
34import org.onosproject.net.link.DefaultLinkDescription;
35import org.onosproject.net.link.LinkDescription;
Ray Milkey44ba9372018-05-07 09:37:37 -070036import org.onosproject.net.link.ProbedLinkProvider;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.net.packet.DefaultOutboundPacket;
38import org.onosproject.net.packet.OutboundPacket;
39import org.onosproject.net.packet.PacketContext;
alshabib7911a052014-10-16 17:49:37 -070040import org.slf4j.Logger;
41
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070042import java.nio.ByteBuffer;
Ray Milkey44ba9372018-05-07 09:37:37 -070043import java.util.Map;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070044
Thomas Vachuska586bc4f2016-08-19 12:44:26 -070045import static com.google.common.base.Strings.isNullOrEmpty;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070046import static java.util.concurrent.TimeUnit.MILLISECONDS;
jaegonkimd9119d0d2018-02-06 23:16:52 +090047import static org.onosproject.net.AnnotationKeys.PORT_NAME;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070048import static org.onosproject.net.PortNumber.portNumber;
49import static org.onosproject.net.flow.DefaultTrafficTreatment.builder;
50import static org.slf4j.LoggerFactory.getLogger;
51
alshabib7911a052014-10-16 17:49:37 -070052/**
53 * Run discovery process from a physical switch. Ports are initially labeled as
54 * slow ports. When an LLDP is successfully received, label the remote port as
55 * fast. Every probeRate milliseconds, loop over all fast ports and send an
56 * LLDP, send an LLDP for a single slow port. Based on FlowVisor topology
57 * discovery implementation.
alshabib7911a052014-10-16 17:49:37 -070058 */
Ray Milkey957390e2016-02-09 10:02:46 -080059public class LinkDiscovery implements TimerTask {
alshabib7911a052014-10-16 17:49:37 -070060
alshabib7911a052014-10-16 17:49:37 -070061 private final Logger log = getLogger(getClass());
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070062
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070063 private final Device device;
Ray Milkey957390e2016-02-09 10:02:46 -080064 private final LinkDiscoveryContext context;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070065
alshabib7911a052014-10-16 17:49:37 -070066 private final Ethernet ethPacket;
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -070067 private final Ethernet bddpEth;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070068
69 private Timeout timeout;
70 private volatile boolean isStopped;
Thomas Vachuska9280e192018-03-06 11:19:29 -080071
Thomas Vachuska05453c92015-09-09 14:40:49 -070072 // Set of ports to be probed
Ray Milkey44ba9372018-05-07 09:37:37 -070073 private final Map<Long, String> portMap = Maps.newConcurrentMap();
alshabib7911a052014-10-16 17:49:37 -070074 /**
75 * Instantiates discovery manager for the given physical switch. Creates a
76 * generic LLDP packet that will be customized for the port it is sent out on.
77 * Starts the the timer for the discovery process.
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070078 *
Thomas Vachuska05453c92015-09-09 14:40:49 -070079 * @param device the physical switch
80 * @param context discovery context
alshabib7911a052014-10-16 17:49:37 -070081 */
Ray Milkey957390e2016-02-09 10:02:46 -080082 public LinkDiscovery(Device device, LinkDiscoveryContext context) {
alshabib7911a052014-10-16 17:49:37 -070083 this.device = device;
Thomas Vachuska05453c92015-09-09 14:40:49 -070084 this.context = context;
alshabib3d643ec2014-10-22 18:33:00 -070085
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070086 ethPacket = new Ethernet();
87 ethPacket.setEtherType(Ethernet.TYPE_LLDP);
Charles Chan928ff8b2017-05-04 12:22:54 -070088 ethPacket.setDestinationMACAddress(MacAddress.ONOS_LLDP);
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070089 ethPacket.setPad(true);
alshabib7911a052014-10-16 17:49:37 -070090
Thomas Vachuska05453c92015-09-09 14:40:49 -070091 bddpEth = new Ethernet();
Thomas Vachuska05453c92015-09-09 14:40:49 -070092 bddpEth.setEtherType(Ethernet.TYPE_BSN);
Charles Chan928ff8b2017-05-04 12:22:54 -070093 bddpEth.setDestinationMACAddress(MacAddress.BROADCAST);
Thomas Vachuska05453c92015-09-09 14:40:49 -070094 bddpEth.setPad(true);
alshabib7911a052014-10-16 17:49:37 -070095
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070096 isStopped = true;
alshabib7911a052014-10-16 17:49:37 -070097 start();
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070098 log.debug("Started discovery manager for switch {}", device.id());
alshabib7911a052014-10-16 17:49:37 -070099
100 }
101
Ray Milkey957390e2016-02-09 10:02:46 -0800102 public synchronized void stop() {
Jon Hall3edc08a2015-09-14 16:59:07 -0700103 if (!isStopped) {
104 isStopped = true;
105 timeout.cancel();
106 } else {
107 log.warn("LinkDiscovery stopped multiple times?");
108 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700109 }
110
Ray Milkey957390e2016-02-09 10:02:46 -0800111 public synchronized void start() {
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700112 if (isStopped) {
113 isStopped = false;
Yuta HIGUCHI19afc032017-05-20 23:44:17 -0700114 timeout = Timer.newTimeout(this, 0, MILLISECONDS);
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700115 } else {
116 log.warn("LinkDiscovery started multiple times?");
117 }
118 }
119
Ray Milkey957390e2016-02-09 10:02:46 -0800120 public synchronized boolean isStopped() {
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700121 return isStopped || timeout.isCancelled();
122 }
123
alshabib7911a052014-10-16 17:49:37 -0700124 /**
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700125 * Add physical port to discovery process.
alshabib7911a052014-10-16 17:49:37 -0700126 * Send out initial LLDP and label it as slow port.
127 *
128 * @param port the port
129 */
Ray Milkey957390e2016-02-09 10:02:46 -0800130 public void addPort(Port port) {
jaegonkimd9119d0d2018-02-06 23:16:52 +0900131 Long portNum = port.number().toLong();
132 String portName = port.annotations().value(PORT_NAME);
jaegonkimaaa4f832018-03-11 00:48:36 +0900133 if (portName == null) {
134 portName = StringUtil.EMPTY_STRING;
135 }
jaegonkimd9119d0d2018-02-06 23:16:52 +0900136
You Wang168aa622018-03-08 14:01:37 -0800137 boolean newPort = !containsPort(portNum);
jaegonkimd9119d0d2018-02-06 23:16:52 +0900138 portMap.put(portNum, portName);
139
Thomas Vachuska05453c92015-09-09 14:40:49 -0700140 boolean isMaster = context.mastershipService().isLocalMaster(device.id());
Jonathan Hart45066bc2015-07-28 11:18:34 -0700141 if (newPort && isMaster) {
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700142 log.debug("Sending initial probe to port {}@{}", port.number().toLong(), device.id());
jaegonkimd9119d0d2018-02-06 23:16:52 +0900143 sendProbes(portNum, portName);
alshabib7911a052014-10-16 17:49:37 -0700144 }
alshabib7911a052014-10-16 17:49:37 -0700145 }
146
147 /**
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700148 * removed physical port from discovery process.
149 * @param port the port number
150 */
Ray Milkey957390e2016-02-09 10:02:46 -0800151 public void removePort(PortNumber port) {
jaegonkimd9119d0d2018-02-06 23:16:52 +0900152 portMap.remove(port.toLong());
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700153 }
154
155 /**
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700156 * Handles an incoming LLDP packet. Creates link in topology and adds the
157 * link for staleness tracking.
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700158 *
Thomas Vachuska05453c92015-09-09 14:40:49 -0700159 * @param packetContext packet context
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800160 * @return true if handled
alshabib7911a052014-10-16 17:49:37 -0700161 */
Ray Milkey957390e2016-02-09 10:02:46 -0800162 public boolean handleLldp(PacketContext packetContext) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700163 Ethernet eth = packetContext.inPacket().parsed();
Jonathan Harte8600eb2015-01-12 10:30:45 -0800164 if (eth == null) {
165 return false;
166 }
167
alshabib7911a052014-10-16 17:49:37 -0700168 ONOSLLDP onoslldp = ONOSLLDP.parseONOSLLDP(eth);
169 if (onoslldp != null) {
Ayaka Koshibe48229222016-05-16 18:04:26 -0700170 Type lt;
171 if (notMy(eth.getSourceMAC().toString())) {
172 lt = Type.EDGE;
173 } else {
174 lt = eth.getEtherType() == Ethernet.TYPE_LLDP ?
175 Type.DIRECT : Type.INDIRECT;
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800176 }
177
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700178 PortNumber srcPort = portNumber(onoslldp.getPort());
Thomas Vachuska05453c92015-09-09 14:40:49 -0700179 PortNumber dstPort = packetContext.inPacket().receivedFrom().port();
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700180
Thomas Vachuska586bc4f2016-08-19 12:44:26 -0700181 String idString = onoslldp.getDeviceString();
182 if (!isNullOrEmpty(idString)) {
183 DeviceId srcDeviceId = DeviceId.deviceId(idString);
184 DeviceId dstDeviceId = packetContext.inPacket().receivedFrom().deviceId();
alshabib7911a052014-10-16 17:49:37 -0700185
Thomas Vachuska586bc4f2016-08-19 12:44:26 -0700186 ConnectPoint src = new ConnectPoint(srcDeviceId, srcPort);
187 ConnectPoint dst = new ConnectPoint(dstDeviceId, dstPort);
188
189 LinkDescription ld = new DefaultLinkDescription(src, dst, lt);
190 try {
191 context.providerService().linkDetected(ld);
192 context.touchLink(LinkKey.linkKey(src, dst));
193 } catch (IllegalStateException e) {
194 return true;
195 }
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700196 return true;
197 }
alshabib7911a052014-10-16 17:49:37 -0700198 }
199 return false;
200 }
201
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800202 // true if *NOT* this cluster's own probe.
Ayaka Koshibe48229222016-05-16 18:04:26 -0700203 private boolean notMy(String mac) {
204 // if we are using DEFAULT_MAC, clustering hadn't initialized, so conservative 'yes'
205 String ourMac = context.fingerprint();
206 if (ProbedLinkProvider.defaultMac().equalsIgnoreCase(ourMac)) {
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800207 return true;
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800208 }
Ayaka Koshibe48229222016-05-16 18:04:26 -0700209 return !mac.equalsIgnoreCase(ourMac);
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800210 }
alshabib7911a052014-10-16 17:49:37 -0700211
alshabib7911a052014-10-16 17:49:37 -0700212 /**
213 * Execute this method every t milliseconds. Loops over all ports
214 * labeled as fast and sends out an LLDP. Send out an LLDP on a single slow
215 * port.
216 *
217 * @param t timeout
alshabib7911a052014-10-16 17:49:37 -0700218 */
219 @Override
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700220 public void run(Timeout t) {
Yuta HIGUCHI34198582014-11-10 16:24:58 -0800221 if (isStopped()) {
222 return;
223 }
Thomas Vachuska05453c92015-09-09 14:40:49 -0700224
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700225 if (context.mastershipService().isLocalMaster(device.id())) {
226 log.trace("Sending probes from {}", device.id());
Ray Milkey1ab441b2018-03-06 15:33:44 -0800227 ImmutableMap.copyOf(portMap).forEach(this::sendProbes);
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700228 }
229
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800230 if (!isStopped()) {
Yuta HIGUCHI19afc032017-05-20 23:44:17 -0700231 timeout = t.timer().newTimeout(this, context.probeRate(), MILLISECONDS);
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800232 }
alshabib7911a052014-10-16 17:49:37 -0700233 }
234
alshabib7911a052014-10-16 17:49:37 -0700235 /**
236 * Creates packet_out LLDP for specified output port.
237 *
jaegonkimd9119d0d2018-02-06 23:16:52 +0900238 * @param portNumber the port
239 * @param portDesc the port description
alshabib7911a052014-10-16 17:49:37 -0700240 * @return Packet_out message with LLDP data
241 */
jaegonkimd9119d0d2018-02-06 23:16:52 +0900242 private OutboundPacket createOutBoundLldp(Long portNumber, String portDesc) {
243 if (portNumber == null) {
alshabib7911a052014-10-16 17:49:37 -0700244 return null;
245 }
jaegonkimd9119d0d2018-02-06 23:16:52 +0900246 ONOSLLDP lldp = getLinkProbe(portNumber, portDesc);
Ayaka Koshibe48229222016-05-16 18:04:26 -0700247 ethPacket.setSourceMACAddress(context.fingerprint()).setPayload(lldp);
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700248 return new DefaultOutboundPacket(device.id(),
jaegonkimd9119d0d2018-02-06 23:16:52 +0900249 builder().setOutput(portNumber(portNumber)).build(),
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700250 ByteBuffer.wrap(ethPacket.serialize()));
alshabib7911a052014-10-16 17:49:37 -0700251 }
252
253 /**
254 * Creates packet_out BDDP for specified output port.
255 *
jaegonkimd9119d0d2018-02-06 23:16:52 +0900256 * @param portNumber the port
257 * @param portDesc the port description
alshabib7911a052014-10-16 17:49:37 -0700258 * @return Packet_out message with LLDP data
259 */
jaegonkimd9119d0d2018-02-06 23:16:52 +0900260 private OutboundPacket createOutBoundBddp(Long portNumber, String portDesc) {
261 if (portNumber == null) {
alshabib7911a052014-10-16 17:49:37 -0700262 return null;
263 }
jaegonkimd9119d0d2018-02-06 23:16:52 +0900264 ONOSLLDP lldp = getLinkProbe(portNumber, portDesc);
Ayaka Koshibe48229222016-05-16 18:04:26 -0700265 bddpEth.setSourceMACAddress(context.fingerprint()).setPayload(lldp);
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700266 return new DefaultOutboundPacket(device.id(),
jaegonkimd9119d0d2018-02-06 23:16:52 +0900267 builder().setOutput(portNumber(portNumber)).build(),
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700268 ByteBuffer.wrap(bddpEth.serialize()));
alshabib7911a052014-10-16 17:49:37 -0700269 }
270
jaegonkimd9119d0d2018-02-06 23:16:52 +0900271 private ONOSLLDP getLinkProbe(Long portNumber, String portDesc) {
272 return ONOSLLDP.onosLLDP(device.id().toString(), device.chassisId(), portNumber.intValue(), portDesc);
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800273 }
274
jaegonkimd9119d0d2018-02-06 23:16:52 +0900275 private void sendProbes(Long portNumber, String portDesc) {
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700276 if (context.packetService() == null) {
277 return;
278 }
Jon Hall274cecb2017-08-09 12:15:48 -0700279 log.trace("Sending probes out of {}@{}", portNumber, device.id());
jaegonkimd9119d0d2018-02-06 23:16:52 +0900280 OutboundPacket pkt = createOutBoundLldp(portNumber, portDesc);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700281 context.packetService().emit(pkt);
Jonathan Hartb35540a2015-11-17 09:30:56 -0800282 if (context.useBddp()) {
jaegonkimd9119d0d2018-02-06 23:16:52 +0900283 OutboundPacket bpkt = createOutBoundBddp(portNumber, portDesc);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700284 context.packetService().emit(bpkt);
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700285 }
alshabib7911a052014-10-16 17:49:37 -0700286 }
287
Ray Milkey957390e2016-02-09 10:02:46 -0800288 public boolean containsPort(long portNumber) {
jaegonkimd9119d0d2018-02-06 23:16:52 +0900289 return portMap.containsKey(portNumber);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700290 }
alshabib7911a052014-10-16 17:49:37 -0700291}