blob: f4e2231d68c0c2bbca4f10d63eb4b1c555cb74ca [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present 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;
Charles Chan928ff8b2017-05-04 12:22:54 -070023import org.onlab.packet.MacAddress;
Jonathan Harte8600eb2015-01-12 10:30:45 -080024import org.onlab.packet.ONOSLLDP;
25import org.onlab.util.Timer;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.Device;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.Link.Type;
Thomas Vachuska05453c92015-09-09 14:40:49 -070030import org.onosproject.net.LinkKey;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.Port;
32import org.onosproject.net.PortNumber;
33import org.onosproject.net.link.DefaultLinkDescription;
34import org.onosproject.net.link.LinkDescription;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.packet.DefaultOutboundPacket;
36import org.onosproject.net.packet.OutboundPacket;
37import org.onosproject.net.packet.PacketContext;
Ayaka Koshibe48229222016-05-16 18:04:26 -070038import org.onosproject.net.link.ProbedLinkProvider;
alshabib7911a052014-10-16 17:49:37 -070039import org.slf4j.Logger;
40
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070041import java.nio.ByteBuffer;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070042import java.util.Set;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070043
Thomas Vachuska586bc4f2016-08-19 12:44:26 -070044import static com.google.common.base.Strings.isNullOrEmpty;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070045import static java.util.concurrent.TimeUnit.MILLISECONDS;
46import static org.onosproject.net.PortNumber.portNumber;
47import static org.onosproject.net.flow.DefaultTrafficTreatment.builder;
48import static org.slf4j.LoggerFactory.getLogger;
49
alshabib7911a052014-10-16 17:49:37 -070050/**
51 * Run discovery process from a physical switch. Ports are initially labeled as
52 * slow ports. When an LLDP is successfully received, label the remote port as
53 * fast. Every probeRate milliseconds, loop over all fast ports and send an
54 * LLDP, send an LLDP for a single slow port. Based on FlowVisor topology
55 * discovery implementation.
alshabib7911a052014-10-16 17:49:37 -070056 */
Ray Milkey957390e2016-02-09 10:02:46 -080057public class LinkDiscovery implements TimerTask {
alshabib7911a052014-10-16 17:49:37 -070058
alshabib7911a052014-10-16 17:49:37 -070059 private final Logger log = getLogger(getClass());
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070060
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070061 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;
Thomas Vachuska05453c92015-09-09 14:40:49 -070069 // Set of ports to be probed
70 private final Set<Long> ports = Sets.newConcurrentHashSet();
71
alshabib7911a052014-10-16 17:49:37 -070072 /**
73 * Instantiates discovery manager for the given physical switch. Creates a
74 * generic LLDP packet that will be customized for the port it is sent out on.
75 * Starts the the timer for the discovery process.
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070076 *
Thomas Vachuska05453c92015-09-09 14:40:49 -070077 * @param device the physical switch
78 * @param context discovery context
alshabib7911a052014-10-16 17:49:37 -070079 */
Ray Milkey957390e2016-02-09 10:02:46 -080080 public LinkDiscovery(Device device, LinkDiscoveryContext context) {
alshabib7911a052014-10-16 17:49:37 -070081 this.device = device;
Thomas Vachuska05453c92015-09-09 14:40:49 -070082 this.context = context;
alshabib3d643ec2014-10-22 18:33:00 -070083
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070084 ethPacket = new Ethernet();
85 ethPacket.setEtherType(Ethernet.TYPE_LLDP);
Charles Chan928ff8b2017-05-04 12:22:54 -070086 ethPacket.setDestinationMACAddress(MacAddress.ONOS_LLDP);
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070087 ethPacket.setPad(true);
alshabib7911a052014-10-16 17:49:37 -070088
Thomas Vachuska05453c92015-09-09 14:40:49 -070089 bddpEth = new Ethernet();
Thomas Vachuska05453c92015-09-09 14:40:49 -070090 bddpEth.setEtherType(Ethernet.TYPE_BSN);
Charles Chan928ff8b2017-05-04 12:22:54 -070091 bddpEth.setDestinationMACAddress(MacAddress.BROADCAST);
Thomas Vachuska05453c92015-09-09 14:40:49 -070092 bddpEth.setPad(true);
alshabib7911a052014-10-16 17:49:37 -070093
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070094 isStopped = true;
alshabib7911a052014-10-16 17:49:37 -070095 start();
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070096 log.debug("Started discovery manager for switch {}", device.id());
alshabib7911a052014-10-16 17:49:37 -070097
98 }
99
Ray Milkey957390e2016-02-09 10:02:46 -0800100 public synchronized void stop() {
Jon Hall3edc08a2015-09-14 16:59:07 -0700101 if (!isStopped) {
102 isStopped = true;
103 timeout.cancel();
104 } else {
105 log.warn("LinkDiscovery stopped multiple times?");
106 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700107 }
108
Ray Milkey957390e2016-02-09 10:02:46 -0800109 public synchronized void start() {
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700110 if (isStopped) {
111 isStopped = false;
112 timeout = Timer.getTimer().newTimeout(this, 0, MILLISECONDS);
113 } else {
114 log.warn("LinkDiscovery started multiple times?");
115 }
116 }
117
Ray Milkey957390e2016-02-09 10:02:46 -0800118 public synchronized boolean isStopped() {
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700119 return isStopped || timeout.isCancelled();
120 }
121
alshabib7911a052014-10-16 17:49:37 -0700122 /**
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700123 * Add physical port to discovery process.
alshabib7911a052014-10-16 17:49:37 -0700124 * Send out initial LLDP and label it as slow port.
125 *
126 * @param port the port
127 */
Ray Milkey957390e2016-02-09 10:02:46 -0800128 public void addPort(Port port) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700129 boolean newPort = ports.add(port.number().toLong());
130 boolean isMaster = context.mastershipService().isLocalMaster(device.id());
Jonathan Hart45066bc2015-07-28 11:18:34 -0700131 if (newPort && isMaster) {
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700132 log.debug("Sending initial probe to port {}@{}", port.number().toLong(), device.id());
Jonathan Hart45066bc2015-07-28 11:18:34 -0700133 sendProbes(port.number().toLong());
alshabib7911a052014-10-16 17:49:37 -0700134 }
alshabib7911a052014-10-16 17:49:37 -0700135 }
136
137 /**
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700138 * removed physical port from discovery process.
139 * @param port the port number
140 */
Ray Milkey957390e2016-02-09 10:02:46 -0800141 public void removePort(PortNumber port) {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700142 ports.remove(port.toLong());
143 }
144
145 /**
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700146 * Handles an incoming LLDP packet. Creates link in topology and adds the
147 * link for staleness tracking.
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700148 *
Thomas Vachuska05453c92015-09-09 14:40:49 -0700149 * @param packetContext packet context
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800150 * @return true if handled
alshabib7911a052014-10-16 17:49:37 -0700151 */
Ray Milkey957390e2016-02-09 10:02:46 -0800152 public boolean handleLldp(PacketContext packetContext) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700153 Ethernet eth = packetContext.inPacket().parsed();
Jonathan Harte8600eb2015-01-12 10:30:45 -0800154 if (eth == null) {
155 return false;
156 }
157
alshabib7911a052014-10-16 17:49:37 -0700158 ONOSLLDP onoslldp = ONOSLLDP.parseONOSLLDP(eth);
159 if (onoslldp != null) {
Ayaka Koshibe48229222016-05-16 18:04:26 -0700160 Type lt;
161 if (notMy(eth.getSourceMAC().toString())) {
162 lt = Type.EDGE;
163 } else {
164 lt = eth.getEtherType() == Ethernet.TYPE_LLDP ?
165 Type.DIRECT : Type.INDIRECT;
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800166 }
167
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700168 PortNumber srcPort = portNumber(onoslldp.getPort());
Thomas Vachuska05453c92015-09-09 14:40:49 -0700169 PortNumber dstPort = packetContext.inPacket().receivedFrom().port();
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700170
Thomas Vachuska586bc4f2016-08-19 12:44:26 -0700171 String idString = onoslldp.getDeviceString();
172 if (!isNullOrEmpty(idString)) {
173 DeviceId srcDeviceId = DeviceId.deviceId(idString);
174 DeviceId dstDeviceId = packetContext.inPacket().receivedFrom().deviceId();
alshabib7911a052014-10-16 17:49:37 -0700175
Thomas Vachuska586bc4f2016-08-19 12:44:26 -0700176 ConnectPoint src = new ConnectPoint(srcDeviceId, srcPort);
177 ConnectPoint dst = new ConnectPoint(dstDeviceId, dstPort);
178
179 LinkDescription ld = new DefaultLinkDescription(src, dst, lt);
180 try {
181 context.providerService().linkDetected(ld);
182 context.touchLink(LinkKey.linkKey(src, dst));
183 } catch (IllegalStateException e) {
184 return true;
185 }
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700186 return true;
187 }
alshabib7911a052014-10-16 17:49:37 -0700188 }
189 return false;
190 }
191
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800192 // true if *NOT* this cluster's own probe.
Ayaka Koshibe48229222016-05-16 18:04:26 -0700193 private boolean notMy(String mac) {
194 // if we are using DEFAULT_MAC, clustering hadn't initialized, so conservative 'yes'
195 String ourMac = context.fingerprint();
196 if (ProbedLinkProvider.defaultMac().equalsIgnoreCase(ourMac)) {
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800197 return true;
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800198 }
Ayaka Koshibe48229222016-05-16 18:04:26 -0700199 return !mac.equalsIgnoreCase(ourMac);
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800200 }
alshabib7911a052014-10-16 17:49:37 -0700201
alshabib7911a052014-10-16 17:49:37 -0700202 /**
203 * Execute this method every t milliseconds. Loops over all ports
204 * labeled as fast and sends out an LLDP. Send out an LLDP on a single slow
205 * port.
206 *
207 * @param t timeout
alshabib7911a052014-10-16 17:49:37 -0700208 */
209 @Override
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700210 public void run(Timeout t) {
Yuta HIGUCHI34198582014-11-10 16:24:58 -0800211 if (isStopped()) {
212 return;
213 }
Thomas Vachuska05453c92015-09-09 14:40:49 -0700214
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700215 if (context.mastershipService().isLocalMaster(device.id())) {
216 log.trace("Sending probes from {}", device.id());
217 ports.forEach(this::sendProbes);
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700218 }
219
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800220 if (!isStopped()) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700221 timeout = Timer.getTimer().newTimeout(this, context.probeRate(), MILLISECONDS);
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800222 }
alshabib7911a052014-10-16 17:49:37 -0700223 }
224
alshabib7911a052014-10-16 17:49:37 -0700225 /**
226 * Creates packet_out LLDP for specified output port.
227 *
228 * @param port the port
229 * @return Packet_out message with LLDP data
230 */
Jonathan Hartb35540a2015-11-17 09:30:56 -0800231 private OutboundPacket createOutBoundLldp(Long port) {
alshabib7911a052014-10-16 17:49:37 -0700232 if (port == null) {
233 return null;
234 }
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800235 ONOSLLDP lldp = getLinkProbe(port);
Ayaka Koshibe48229222016-05-16 18:04:26 -0700236 ethPacket.setSourceMACAddress(context.fingerprint()).setPayload(lldp);
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700237 return new DefaultOutboundPacket(device.id(),
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700238 builder().setOutput(portNumber(port)).build(),
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700239 ByteBuffer.wrap(ethPacket.serialize()));
alshabib7911a052014-10-16 17:49:37 -0700240 }
241
242 /**
243 * Creates packet_out BDDP for specified output port.
244 *
245 * @param port the port
246 * @return Packet_out message with LLDP data
247 */
Jonathan Hartb35540a2015-11-17 09:30:56 -0800248 private OutboundPacket createOutBoundBddp(Long port) {
alshabib7911a052014-10-16 17:49:37 -0700249 if (port == null) {
250 return null;
251 }
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800252 ONOSLLDP lldp = getLinkProbe(port);
Ayaka Koshibe48229222016-05-16 18:04:26 -0700253 bddpEth.setSourceMACAddress(context.fingerprint()).setPayload(lldp);
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700254 return new DefaultOutboundPacket(device.id(),
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700255 builder().setOutput(portNumber(port)).build(),
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700256 ByteBuffer.wrap(bddpEth.serialize()));
alshabib7911a052014-10-16 17:49:37 -0700257 }
258
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800259 private ONOSLLDP getLinkProbe(Long port) {
Ayaka Koshibe48229222016-05-16 18:04:26 -0700260 return ONOSLLDP.onosLLDP(device.id().toString(), device.chassisId(), port.intValue());
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800261 }
262
alshabib7911a052014-10-16 17:49:37 -0700263 private void sendProbes(Long portNumber) {
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700264 if (context.packetService() == null) {
265 return;
266 }
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800267 log.trace("Sending probes out to {}@{}", portNumber, device.id());
Jonathan Hartb35540a2015-11-17 09:30:56 -0800268 OutboundPacket pkt = createOutBoundLldp(portNumber);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700269 context.packetService().emit(pkt);
Jonathan Hartb35540a2015-11-17 09:30:56 -0800270 if (context.useBddp()) {
271 OutboundPacket bpkt = createOutBoundBddp(portNumber);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700272 context.packetService().emit(bpkt);
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700273 }
alshabib7911a052014-10-16 17:49:37 -0700274 }
275
Ray Milkey957390e2016-02-09 10:02:46 -0800276 public boolean containsPort(long portNumber) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700277 return ports.contains(portNumber);
278 }
alshabib7911a052014-10-16 17:49:37 -0700279}