blob: 125df585d273b88ee8b5ef860773212fbe292f37 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
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
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070018import com.google.common.collect.Sets;
Ayaka Koshibe12c8c082015-12-08 12:48:46 -080019
alshabib7911a052014-10-16 17:49:37 -070020import org.jboss.netty.util.Timeout;
21import org.jboss.netty.util.TimerTask;
Jonathan Harte8600eb2015-01-12 10:30:45 -080022import org.onlab.packet.Ethernet;
23import org.onlab.packet.ONOSLLDP;
24import org.onlab.util.Timer;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.Device;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.Link.Type;
Thomas Vachuska05453c92015-09-09 14:40:49 -070029import org.onosproject.net.LinkKey;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.Port;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.link.DefaultLinkDescription;
33import org.onosproject.net.link.LinkDescription;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.packet.DefaultOutboundPacket;
35import org.onosproject.net.packet.OutboundPacket;
36import org.onosproject.net.packet.PacketContext;
alshabib7911a052014-10-16 17:49:37 -070037import org.slf4j.Logger;
38
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070039import java.nio.ByteBuffer;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070040import java.util.Set;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070041
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070042import static java.util.concurrent.TimeUnit.MILLISECONDS;
43import static org.onosproject.net.PortNumber.portNumber;
44import static org.onosproject.net.flow.DefaultTrafficTreatment.builder;
45import static org.slf4j.LoggerFactory.getLogger;
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -080046import static org.onosproject.cluster.ClusterMetadata.NO_NAME;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070047
alshabib7911a052014-10-16 17:49:37 -070048/**
49 * Run discovery process from a physical switch. Ports are initially labeled as
50 * slow ports. When an LLDP is successfully received, label the remote port as
51 * fast. Every probeRate milliseconds, loop over all fast ports and send an
52 * LLDP, send an LLDP for a single slow port. Based on FlowVisor topology
53 * discovery implementation.
alshabib7911a052014-10-16 17:49:37 -070054 */
Ray Milkey957390e2016-02-09 10:02:46 -080055public class LinkDiscovery implements TimerTask {
alshabib7911a052014-10-16 17:49:37 -070056
alshabib7911a052014-10-16 17:49:37 -070057 private final Logger log = getLogger(getClass());
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070058
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070059 private static final String SRC_MAC = "DE:AD:BE:EF:BA:11";
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070060
61 private final Device device;
Ray Milkey957390e2016-02-09 10:02:46 -080062 private final LinkDiscoveryContext context;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070063
alshabib7911a052014-10-16 17:49:37 -070064 private final Ethernet ethPacket;
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -070065 private final Ethernet bddpEth;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070066
67 private Timeout timeout;
68 private volatile boolean isStopped;
Ayaka Koshibe12c8c082015-12-08 12:48:46 -080069 // This LinkDiscovery can handle remote link probes (default false).
70 private volatile boolean fingerprinted;
Thomas Vachuska05453c92015-09-09 14:40:49 -070071 // Set of ports to be probed
72 private final Set<Long> ports = Sets.newConcurrentHashSet();
73
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);
88 ethPacket.setDestinationMACAddress(ONOSLLDP.LLDP_NICIRA);
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);
93 bddpEth.setDestinationMACAddress(ONOSLLDP.BDDP_MULTICAST);
94 bddpEth.setPad(true);
alshabib7911a052014-10-16 17:49:37 -070095
Ayaka Koshibe12c8c082015-12-08 12:48:46 -080096 fingerprinted = false;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070097 isStopped = true;
alshabib7911a052014-10-16 17:49:37 -070098 start();
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070099 log.debug("Started discovery manager for switch {}", device.id());
alshabib7911a052014-10-16 17:49:37 -0700100
101 }
102
Ray Milkey957390e2016-02-09 10:02:46 -0800103 public synchronized void stop() {
Jon Hall3edc08a2015-09-14 16:59:07 -0700104 if (!isStopped) {
105 isStopped = true;
106 timeout.cancel();
107 } else {
108 log.warn("LinkDiscovery stopped multiple times?");
109 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700110 }
111
Ray Milkey957390e2016-02-09 10:02:46 -0800112 public synchronized void start() {
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700113 if (isStopped) {
114 isStopped = false;
115 timeout = Timer.getTimer().newTimeout(this, 0, MILLISECONDS);
116 } else {
117 log.warn("LinkDiscovery started multiple times?");
118 }
119 }
120
Ray Milkey957390e2016-02-09 10:02:46 -0800121 public synchronized boolean isStopped() {
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700122 return isStopped || timeout.isCancelled();
123 }
124
alshabib7911a052014-10-16 17:49:37 -0700125 /**
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700126 * Add physical port to discovery process.
alshabib7911a052014-10-16 17:49:37 -0700127 * Send out initial LLDP and label it as slow port.
128 *
129 * @param port the port
130 */
Ray Milkey957390e2016-02-09 10:02:46 -0800131 public void addPort(Port port) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700132 boolean newPort = ports.add(port.number().toLong());
133 boolean isMaster = context.mastershipService().isLocalMaster(device.id());
Jonathan Hart45066bc2015-07-28 11:18:34 -0700134 if (newPort && isMaster) {
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700135 log.debug("Sending initial probe to port {}@{}", port.number().toLong(), device.id());
Jonathan Hart45066bc2015-07-28 11:18:34 -0700136 sendProbes(port.number().toLong());
alshabib7911a052014-10-16 17:49:37 -0700137 }
alshabib7911a052014-10-16 17:49:37 -0700138 }
139
140 /**
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700141 * removed physical port from discovery process.
142 * @param port the port number
143 */
Ray Milkey957390e2016-02-09 10:02:46 -0800144 public void removePort(PortNumber port) {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700145 ports.remove(port.toLong());
146 }
147
148 /**
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700149 * Handles an incoming LLDP packet. Creates link in topology and adds the
150 * link for staleness tracking.
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700151 *
Thomas Vachuska05453c92015-09-09 14:40:49 -0700152 * @param packetContext packet context
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800153 * @return true if handled
alshabib7911a052014-10-16 17:49:37 -0700154 */
Ray Milkey957390e2016-02-09 10:02:46 -0800155 public boolean handleLldp(PacketContext packetContext) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700156 Ethernet eth = packetContext.inPacket().parsed();
Jonathan Harte8600eb2015-01-12 10:30:45 -0800157 if (eth == null) {
158 return false;
159 }
160
alshabib7911a052014-10-16 17:49:37 -0700161 ONOSLLDP onoslldp = ONOSLLDP.parseONOSLLDP(eth);
162 if (onoslldp != null) {
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800163 if (notMy(onoslldp)) {
164 return true;
165 }
166
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700167 PortNumber srcPort = portNumber(onoslldp.getPort());
Thomas Vachuska05453c92015-09-09 14:40:49 -0700168 PortNumber dstPort = packetContext.inPacket().receivedFrom().port();
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700169 DeviceId srcDeviceId = DeviceId.deviceId(onoslldp.getDeviceString());
Thomas Vachuska05453c92015-09-09 14:40:49 -0700170 DeviceId dstDeviceId = packetContext.inPacket().receivedFrom().deviceId();
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700171
alshabib7911a052014-10-16 17:49:37 -0700172 ConnectPoint src = new ConnectPoint(srcDeviceId, srcPort);
173 ConnectPoint dst = new ConnectPoint(dstDeviceId, dstPort);
174
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700175 LinkDescription ld = eth.getEtherType() == Ethernet.TYPE_LLDP ?
176 new DefaultLinkDescription(src, dst, Type.DIRECT) :
177 new DefaultLinkDescription(src, dst, Type.INDIRECT);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700178
179 try {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700180 context.providerService().linkDetected(ld);
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700181 context.touchLink(LinkKey.linkKey(src, dst));
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700182 } catch (IllegalStateException e) {
183 return true;
184 }
alshabib7911a052014-10-16 17:49:37 -0700185 return true;
186 }
187 return false;
188 }
189
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800190 // true if *NOT* this cluster's own probe.
191 private boolean notMy(ONOSLLDP onoslldp) {
192 if (onoslldp.getDomainTLV() == null) {
193 // not finger-printed - but we can check the source
194 DeviceId src = DeviceId.deviceId(onoslldp.getDeviceString());
195 if (context.deviceService().getDevice(src) == null) {
196 return true;
197 }
198 return false;
199 }
200
201 String us = context.fingerprint();
202 String them = onoslldp.getDomainString();
203 // if: Our and/or their MetadataService in poorly state, conservative 'yes'
204 if (NO_NAME.equals(us) || NO_NAME.equals(them)) {
205 return true;
206 } else {
207 return !us.equals(them);
208 }
209 }
alshabib7911a052014-10-16 17:49:37 -0700210
alshabib7911a052014-10-16 17:49:37 -0700211 /**
212 * Execute this method every t milliseconds. Loops over all ports
213 * labeled as fast and sends out an LLDP. Send out an LLDP on a single slow
214 * port.
215 *
216 * @param t timeout
alshabib7911a052014-10-16 17:49:37 -0700217 */
218 @Override
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700219 public void run(Timeout t) {
Yuta HIGUCHI34198582014-11-10 16:24:58 -0800220 if (isStopped()) {
221 return;
222 }
Thomas Vachuska05453c92015-09-09 14:40:49 -0700223
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700224 if (context.mastershipService().isLocalMaster(device.id())) {
225 log.trace("Sending probes from {}", device.id());
226 ports.forEach(this::sendProbes);
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700227 }
228
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800229 if (!isStopped()) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700230 timeout = Timer.getTimer().newTimeout(this, context.probeRate(), MILLISECONDS);
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800231 }
alshabib7911a052014-10-16 17:49:37 -0700232 }
233
alshabib7911a052014-10-16 17:49:37 -0700234 /**
235 * Creates packet_out LLDP for specified output port.
236 *
237 * @param port the port
238 * @return Packet_out message with LLDP data
239 */
Jonathan Hartb35540a2015-11-17 09:30:56 -0800240 private OutboundPacket createOutBoundLldp(Long port) {
alshabib7911a052014-10-16 17:49:37 -0700241 if (port == null) {
242 return null;
243 }
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800244 ONOSLLDP lldp = getLinkProbe(port);
245 ethPacket.setSourceMACAddress(SRC_MAC).setPayload(lldp);
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700246 return new DefaultOutboundPacket(device.id(),
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700247 builder().setOutput(portNumber(port)).build(),
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700248 ByteBuffer.wrap(ethPacket.serialize()));
alshabib7911a052014-10-16 17:49:37 -0700249 }
250
251 /**
252 * Creates packet_out BDDP for specified output port.
253 *
254 * @param port the port
255 * @return Packet_out message with LLDP data
256 */
Jonathan Hartb35540a2015-11-17 09:30:56 -0800257 private OutboundPacket createOutBoundBddp(Long port) {
alshabib7911a052014-10-16 17:49:37 -0700258 if (port == null) {
259 return null;
260 }
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800261 ONOSLLDP lldp = getLinkProbe(port);
262 bddpEth.setSourceMACAddress(SRC_MAC).setPayload(lldp);
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700263 return new DefaultOutboundPacket(device.id(),
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700264 builder().setOutput(portNumber(port)).build(),
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700265 ByteBuffer.wrap(bddpEth.serialize()));
alshabib7911a052014-10-16 17:49:37 -0700266 }
267
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800268 private ONOSLLDP getLinkProbe(Long port) {
269 return fingerprinted
270 ? ONOSLLDP.fingerprintedLLDP(device.id().toString(), device.chassisId(),
271 port.intValue(), context.fingerprint())
272 : ONOSLLDP.onosLLDP(device.id().toString(), device.chassisId(),
273 port.intValue());
274 }
275
alshabib7911a052014-10-16 17:49:37 -0700276 private void sendProbes(Long portNumber) {
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800277 log.trace("Sending probes out to {}@{}", portNumber, device.id());
Jonathan Hartb35540a2015-11-17 09:30:56 -0800278 OutboundPacket pkt = createOutBoundLldp(portNumber);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700279 context.packetService().emit(pkt);
Jonathan Hartb35540a2015-11-17 09:30:56 -0800280 if (context.useBddp()) {
281 OutboundPacket bpkt = createOutBoundBddp(portNumber);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700282 context.packetService().emit(bpkt);
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700283 }
alshabib7911a052014-10-16 17:49:37 -0700284 }
285
Ray Milkey957390e2016-02-09 10:02:46 -0800286 public boolean containsPort(long portNumber) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700287 return ports.contains(portNumber);
288 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700289
Ray Milkey957390e2016-02-09 10:02:46 -0800290 public void enableFingerprint() {
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800291 fingerprinted = true;
292 }
293
Ray Milkey957390e2016-02-09 10:02:46 -0800294 public void disableFingerprint() {
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800295 fingerprinted = false;
296 }
alshabib7911a052014-10-16 17:49:37 -0700297}