blob: f7fe8ab395d388c57dc83c831b7cd494b0be315a [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.provider.lldp.impl;
alshabib7911a052014-10-16 17:49:37 -070017
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070018import com.google.common.collect.Maps;
19import com.google.common.collect.Sets;
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.Map;
41import java.util.Set;
Thomas Vachuska05453c92015-09-09 14:40:49 -070042import java.util.stream.Collectors;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070043
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070044import static java.util.concurrent.TimeUnit.MILLISECONDS;
45import static org.onosproject.net.PortNumber.portNumber;
46import static org.onosproject.net.flow.DefaultTrafficTreatment.builder;
47import static org.slf4j.LoggerFactory.getLogger;
48
alshabib7911a052014-10-16 17:49:37 -070049/**
50 * Run discovery process from a physical switch. Ports are initially labeled as
51 * slow ports. When an LLDP is successfully received, label the remote port as
52 * fast. Every probeRate milliseconds, loop over all fast ports and send an
53 * LLDP, send an LLDP for a single slow port. Based on FlowVisor topology
54 * discovery implementation.
alshabib7911a052014-10-16 17:49:37 -070055 */
56public class LinkDiscovery implements TimerTask {
57
alshabib7911a052014-10-16 17:49:37 -070058 private final Logger log = getLogger(getClass());
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070059
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070060 private static final String SRC_MAC = "DE:AD:BE:EF:BA:11";
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070061
62 private final Device device;
Thomas Vachuska05453c92015-09-09 14:40:49 -070063 private final DiscoveryContext context;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070064
alshabib7911a052014-10-16 17:49:37 -070065 private final ONOSLLDP lldpPacket;
66 private final Ethernet ethPacket;
67 private Ethernet bddpEth;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070068
69 private Timeout timeout;
70 private volatile boolean isStopped;
71
Thomas Vachuska05453c92015-09-09 14:40:49 -070072 // Set of ports to be probed
73 private final Set<Long> ports = Sets.newConcurrentHashSet();
74
75 // Most recent time a link was seen
76 private final Map<LinkKey, Long> linkTimes = Maps.newConcurrentMap();
alshabib7911a052014-10-16 17:49:37 -070077
78 /**
79 * Instantiates discovery manager for the given physical switch. Creates a
80 * generic LLDP packet that will be customized for the port it is sent out on.
81 * Starts the the timer for the discovery process.
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070082 *
Thomas Vachuska05453c92015-09-09 14:40:49 -070083 * @param device the physical switch
84 * @param context discovery context
alshabib7911a052014-10-16 17:49:37 -070085 */
Thomas Vachuska05453c92015-09-09 14:40:49 -070086 public LinkDiscovery(Device device, DiscoveryContext context) {
alshabib7911a052014-10-16 17:49:37 -070087 this.device = device;
Thomas Vachuska05453c92015-09-09 14:40:49 -070088 this.context = context;
alshabib3d643ec2014-10-22 18:33:00 -070089
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070090 lldpPacket = new ONOSLLDP();
91 lldpPacket.setChassisId(device.chassisId());
92 lldpPacket.setDevice(device.id().toString());
alshabib7911a052014-10-16 17:49:37 -070093
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070094 ethPacket = new Ethernet();
95 ethPacket.setEtherType(Ethernet.TYPE_LLDP);
96 ethPacket.setDestinationMACAddress(ONOSLLDP.LLDP_NICIRA);
97 ethPacket.setPayload(this.lldpPacket);
98 ethPacket.setPad(true);
alshabib7911a052014-10-16 17:49:37 -070099
Thomas Vachuska05453c92015-09-09 14:40:49 -0700100 bddpEth = new Ethernet();
101 bddpEth.setPayload(lldpPacket);
102 bddpEth.setEtherType(Ethernet.TYPE_BSN);
103 bddpEth.setDestinationMACAddress(ONOSLLDP.BDDP_MULTICAST);
104 bddpEth.setPad(true);
105 log.info("Using BDDP to discover network");
alshabib7911a052014-10-16 17:49:37 -0700106
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700107 isStopped = true;
alshabib7911a052014-10-16 17:49:37 -0700108 start();
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700109 log.debug("Started discovery manager for switch {}", device.id());
alshabib7911a052014-10-16 17:49:37 -0700110
111 }
112
113 /**
114 * Add physical port port to discovery process.
115 * Send out initial LLDP and label it as slow port.
116 *
117 * @param port the port
118 */
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700119 public void addPort(Port port) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700120 boolean newPort = ports.add(port.number().toLong());
121 boolean isMaster = context.mastershipService().isLocalMaster(device.id());
Jonathan Hart45066bc2015-07-28 11:18:34 -0700122 if (newPort && isMaster) {
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700123 log.debug("Sending init probe to port {}@{}", port.number().toLong(), device.id());
Jonathan Hart45066bc2015-07-28 11:18:34 -0700124 sendProbes(port.number().toLong());
alshabib7911a052014-10-16 17:49:37 -0700125 }
alshabib7911a052014-10-16 17:49:37 -0700126 }
127
128 /**
alshabib7911a052014-10-16 17:49:37 -0700129 * Method called by remote port to acknowledge receipt of LLDP sent by
130 * this port. If slow port, updates label to fast. If fast port, decrements
131 * number of unacknowledged probes.
132 *
Thomas Vachuska05453c92015-09-09 14:40:49 -0700133 * @param key link key
alshabib7911a052014-10-16 17:49:37 -0700134 */
Thomas Vachuska05453c92015-09-09 14:40:49 -0700135 private void ackProbe(LinkKey key) {
136 long portNumber = key.src().port().toLong();
137 if (ports.contains(portNumber)) {
138 linkTimes.put(key, System.currentTimeMillis());
139 } else {
140 log.debug("Got ackProbe for non-existing port: {}", portNumber);
alshabib7911a052014-10-16 17:49:37 -0700141 }
142 }
143
144
145 /**
146 * Handles an incoming LLDP packet. Creates link in topology and sends ACK
147 * to port where LLDP originated.
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 */
Thomas Vachuska05453c92015-09-09 14:40:49 -0700152 public boolean handleLLDP(PacketContext packetContext) {
153 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) {
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700160 PortNumber srcPort = portNumber(onoslldp.getPort());
Thomas Vachuska05453c92015-09-09 14:40:49 -0700161 PortNumber dstPort = packetContext.inPacket().receivedFrom().port();
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700162 DeviceId srcDeviceId = DeviceId.deviceId(onoslldp.getDeviceString());
Thomas Vachuska05453c92015-09-09 14:40:49 -0700163 DeviceId dstDeviceId = packetContext.inPacket().receivedFrom().deviceId();
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700164
alshabib7911a052014-10-16 17:49:37 -0700165 ConnectPoint src = new ConnectPoint(srcDeviceId, srcPort);
166 ConnectPoint dst = new ConnectPoint(dstDeviceId, dstPort);
167
Thomas Vachuska05453c92015-09-09 14:40:49 -0700168 ackProbe(LinkKey.linkKey(src, dst));
169
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700170 LinkDescription ld = eth.getEtherType() == Ethernet.TYPE_LLDP ?
171 new DefaultLinkDescription(src, dst, Type.DIRECT) :
172 new DefaultLinkDescription(src, dst, Type.INDIRECT);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700173
174 try {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700175 context.providerService().linkDetected(ld);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700176 } catch (IllegalStateException e) {
177 return true;
178 }
alshabib7911a052014-10-16 17:49:37 -0700179 return true;
180 }
181 return false;
182 }
183
184
alshabib7911a052014-10-16 17:49:37 -0700185 /**
186 * Execute this method every t milliseconds. Loops over all ports
187 * labeled as fast and sends out an LLDP. Send out an LLDP on a single slow
188 * port.
189 *
190 * @param t timeout
alshabib7911a052014-10-16 17:49:37 -0700191 */
192 @Override
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700193 public void run(Timeout t) {
Yuta HIGUCHI34198582014-11-10 16:24:58 -0800194 if (isStopped()) {
195 return;
196 }
Thomas Vachuska05453c92015-09-09 14:40:49 -0700197
198 if (!context.mastershipService().isLocalMaster(device.id())) {
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800199 if (!isStopped()) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700200 timeout = Timer.getTimer().newTimeout(this, context.probeRate(), MILLISECONDS);
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800201 }
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700202 return;
203 }
204
Thomas Vachuska05453c92015-09-09 14:40:49 -0700205 // Prune stale links
206 linkTimes.entrySet().stream()
207 .filter(e -> isStale(e.getKey(), e.getValue()))
208 .map(Map.Entry::getKey).collect(Collectors.toSet())
209 .forEach(this::pruneLink);
210
211 // Probe ports
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700212 log.trace("Sending probes from {}", device.id());
Thomas Vachuska05453c92015-09-09 14:40:49 -0700213 ports.forEach(this::sendProbes);
alshabib7911a052014-10-16 17:49:37 -0700214
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800215 if (!isStopped()) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700216 timeout = Timer.getTimer().newTimeout(this, context.probeRate(), MILLISECONDS);
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800217 }
alshabib7911a052014-10-16 17:49:37 -0700218 }
219
Thomas Vachuska05453c92015-09-09 14:40:49 -0700220 private void pruneLink(LinkKey key) {
221 linkTimes.remove(key);
222 LinkDescription desc = new DefaultLinkDescription(key.src(), key.dst(), Type.DIRECT);
223 context.providerService().linkVanished(desc);
224 }
225
226 private boolean isStale(LinkKey key, long lastSeen) {
227 return lastSeen < (System.currentTimeMillis() - context.staleLinkAge());
228 }
229
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800230 public synchronized void stop() {
alshabib0ed6a202014-10-19 12:42:57 -0700231 isStopped = true;
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700232 timeout.cancel();
alshabib7911a052014-10-16 17:49:37 -0700233 }
234
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800235 public synchronized void start() {
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700236 if (isStopped) {
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700237 isStopped = false;
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700238 timeout = Timer.getTimer().newTimeout(this, 0, MILLISECONDS);
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700239 } else {
240 log.warn("LinkDiscovery started multiple times?");
241 }
alshabib7911a052014-10-16 17:49:37 -0700242 }
243
244 /**
245 * Creates packet_out LLDP for specified output port.
246 *
247 * @param port the port
248 * @return Packet_out message with LLDP data
249 */
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700250 private OutboundPacket createOutBoundLLDP(Long port) {
alshabib7911a052014-10-16 17:49:37 -0700251 if (port == null) {
252 return null;
253 }
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700254 lldpPacket.setPortId(port.intValue());
255 ethPacket.setSourceMACAddress(SRC_MAC);
256 return new DefaultOutboundPacket(device.id(),
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700257 builder().setOutput(portNumber(port)).build(),
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700258 ByteBuffer.wrap(ethPacket.serialize()));
alshabib7911a052014-10-16 17:49:37 -0700259 }
260
261 /**
262 * Creates packet_out BDDP for specified output port.
263 *
264 * @param port the port
265 * @return Packet_out message with LLDP data
266 */
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700267 private OutboundPacket createOutBoundBDDP(Long port) {
alshabib7911a052014-10-16 17:49:37 -0700268 if (port == null) {
269 return null;
270 }
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700271 lldpPacket.setPortId(port.intValue());
272 bddpEth.setSourceMACAddress(SRC_MAC);
273 return new DefaultOutboundPacket(device.id(),
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700274 builder().setOutput(portNumber(port)).build(),
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700275 ByteBuffer.wrap(bddpEth.serialize()));
alshabib7911a052014-10-16 17:49:37 -0700276 }
277
278 private void sendProbes(Long portNumber) {
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800279 log.trace("Sending probes out to {}@{}", portNumber, device.id());
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700280 OutboundPacket pkt = createOutBoundLLDP(portNumber);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700281 context.packetService().emit(pkt);
282 if (context.useBDDP()) {
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700283 OutboundPacket bpkt = createOutBoundBDDP(portNumber);
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
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700288 public synchronized boolean isStopped() {
289 return isStopped || timeout.isCancelled();
alshabib0ed6a202014-10-19 12:42:57 -0700290 }
291
Thomas Vachuska05453c92015-09-09 14:40:49 -0700292 boolean containsPort(long portNumber) {
293 return ports.contains(portNumber);
294 }
alshabib7911a052014-10-16 17:49:37 -0700295}