blob: b012d28d515236fa54444b362f21629268b92c59 [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;
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;
Ayaka Koshibe48229222016-05-16 18:04:26 -070037import org.onosproject.net.link.ProbedLinkProvider;
alshabib7911a052014-10-16 17:49:37 -070038import org.slf4j.Logger;
39
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070040import java.nio.ByteBuffer;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070041import java.util.Set;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070042
Thomas Vachuska586bc4f2016-08-19 12:44:26 -070043import static com.google.common.base.Strings.isNullOrEmpty;
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 */
Ray Milkey957390e2016-02-09 10:02:46 -080056public class LinkDiscovery implements TimerTask {
alshabib7911a052014-10-16 17:49:37 -070057
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 final Device device;
Ray Milkey957390e2016-02-09 10:02:46 -080061 private final LinkDiscoveryContext context;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070062
alshabib7911a052014-10-16 17:49:37 -070063 private final Ethernet ethPacket;
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -070064 private final Ethernet bddpEth;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070065
66 private Timeout timeout;
67 private volatile boolean isStopped;
Thomas Vachuska05453c92015-09-09 14:40:49 -070068 // Set of ports to be probed
69 private final Set<Long> ports = Sets.newConcurrentHashSet();
70
alshabib7911a052014-10-16 17:49:37 -070071 /**
72 * Instantiates discovery manager for the given physical switch. Creates a
73 * generic LLDP packet that will be customized for the port it is sent out on.
74 * Starts the the timer for the discovery process.
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070075 *
Thomas Vachuska05453c92015-09-09 14:40:49 -070076 * @param device the physical switch
77 * @param context discovery context
alshabib7911a052014-10-16 17:49:37 -070078 */
Ray Milkey957390e2016-02-09 10:02:46 -080079 public LinkDiscovery(Device device, LinkDiscoveryContext context) {
alshabib7911a052014-10-16 17:49:37 -070080 this.device = device;
Thomas Vachuska05453c92015-09-09 14:40:49 -070081 this.context = context;
alshabib3d643ec2014-10-22 18:33:00 -070082
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070083 ethPacket = new Ethernet();
84 ethPacket.setEtherType(Ethernet.TYPE_LLDP);
Jonathan Hart7838c512016-06-07 15:18:22 -070085 ethPacket.setDestinationMACAddress(ONOSLLDP.LLDP_ONLAB);
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070086 ethPacket.setPad(true);
alshabib7911a052014-10-16 17:49:37 -070087
Thomas Vachuska05453c92015-09-09 14:40:49 -070088 bddpEth = new Ethernet();
Thomas Vachuska05453c92015-09-09 14:40:49 -070089 bddpEth.setEtherType(Ethernet.TYPE_BSN);
90 bddpEth.setDestinationMACAddress(ONOSLLDP.BDDP_MULTICAST);
91 bddpEth.setPad(true);
alshabib7911a052014-10-16 17:49:37 -070092
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070093 isStopped = true;
alshabib7911a052014-10-16 17:49:37 -070094 start();
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070095 log.debug("Started discovery manager for switch {}", device.id());
alshabib7911a052014-10-16 17:49:37 -070096
97 }
98
Ray Milkey957390e2016-02-09 10:02:46 -080099 public synchronized void stop() {
Jon Hall3edc08a2015-09-14 16:59:07 -0700100 if (!isStopped) {
101 isStopped = true;
102 timeout.cancel();
103 } else {
104 log.warn("LinkDiscovery stopped multiple times?");
105 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700106 }
107
Ray Milkey957390e2016-02-09 10:02:46 -0800108 public synchronized void start() {
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700109 if (isStopped) {
110 isStopped = false;
111 timeout = Timer.getTimer().newTimeout(this, 0, MILLISECONDS);
112 } else {
113 log.warn("LinkDiscovery started multiple times?");
114 }
115 }
116
Ray Milkey957390e2016-02-09 10:02:46 -0800117 public synchronized boolean isStopped() {
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700118 return isStopped || timeout.isCancelled();
119 }
120
alshabib7911a052014-10-16 17:49:37 -0700121 /**
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700122 * Add physical port to discovery process.
alshabib7911a052014-10-16 17:49:37 -0700123 * Send out initial LLDP and label it as slow port.
124 *
125 * @param port the port
126 */
Ray Milkey957390e2016-02-09 10:02:46 -0800127 public void addPort(Port port) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700128 boolean newPort = ports.add(port.number().toLong());
129 boolean isMaster = context.mastershipService().isLocalMaster(device.id());
Jonathan Hart45066bc2015-07-28 11:18:34 -0700130 if (newPort && isMaster) {
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700131 log.debug("Sending initial probe to port {}@{}", port.number().toLong(), device.id());
Jonathan Hart45066bc2015-07-28 11:18:34 -0700132 sendProbes(port.number().toLong());
alshabib7911a052014-10-16 17:49:37 -0700133 }
alshabib7911a052014-10-16 17:49:37 -0700134 }
135
136 /**
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700137 * removed physical port from discovery process.
138 * @param port the port number
139 */
Ray Milkey957390e2016-02-09 10:02:46 -0800140 public void removePort(PortNumber port) {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700141 ports.remove(port.toLong());
142 }
143
144 /**
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700145 * Handles an incoming LLDP packet. Creates link in topology and adds the
146 * link for staleness tracking.
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700147 *
Thomas Vachuska05453c92015-09-09 14:40:49 -0700148 * @param packetContext packet context
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800149 * @return true if handled
alshabib7911a052014-10-16 17:49:37 -0700150 */
Ray Milkey957390e2016-02-09 10:02:46 -0800151 public boolean handleLldp(PacketContext packetContext) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700152 Ethernet eth = packetContext.inPacket().parsed();
Jonathan Harte8600eb2015-01-12 10:30:45 -0800153 if (eth == null) {
154 return false;
155 }
156
alshabib7911a052014-10-16 17:49:37 -0700157 ONOSLLDP onoslldp = ONOSLLDP.parseONOSLLDP(eth);
158 if (onoslldp != null) {
Ayaka Koshibe48229222016-05-16 18:04:26 -0700159 Type lt;
160 if (notMy(eth.getSourceMAC().toString())) {
161 lt = Type.EDGE;
162 } else {
163 lt = eth.getEtherType() == Ethernet.TYPE_LLDP ?
164 Type.DIRECT : Type.INDIRECT;
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800165 }
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
Thomas Vachuska586bc4f2016-08-19 12:44:26 -0700170 String idString = onoslldp.getDeviceString();
171 if (!isNullOrEmpty(idString)) {
172 DeviceId srcDeviceId = DeviceId.deviceId(idString);
173 DeviceId dstDeviceId = packetContext.inPacket().receivedFrom().deviceId();
alshabib7911a052014-10-16 17:49:37 -0700174
Thomas Vachuska586bc4f2016-08-19 12:44:26 -0700175 ConnectPoint src = new ConnectPoint(srcDeviceId, srcPort);
176 ConnectPoint dst = new ConnectPoint(dstDeviceId, dstPort);
177
178 LinkDescription ld = new DefaultLinkDescription(src, dst, lt);
179 try {
180 context.providerService().linkDetected(ld);
181 context.touchLink(LinkKey.linkKey(src, dst));
182 } catch (IllegalStateException e) {
183 return true;
184 }
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700185 return true;
186 }
alshabib7911a052014-10-16 17:49:37 -0700187 }
188 return false;
189 }
190
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800191 // true if *NOT* this cluster's own probe.
Ayaka Koshibe48229222016-05-16 18:04:26 -0700192 private boolean notMy(String mac) {
193 // if we are using DEFAULT_MAC, clustering hadn't initialized, so conservative 'yes'
194 String ourMac = context.fingerprint();
195 if (ProbedLinkProvider.defaultMac().equalsIgnoreCase(ourMac)) {
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800196 return true;
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800197 }
Ayaka Koshibe48229222016-05-16 18:04:26 -0700198 return !mac.equalsIgnoreCase(ourMac);
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800199 }
alshabib7911a052014-10-16 17:49:37 -0700200
alshabib7911a052014-10-16 17:49:37 -0700201 /**
202 * Execute this method every t milliseconds. Loops over all ports
203 * labeled as fast and sends out an LLDP. Send out an LLDP on a single slow
204 * port.
205 *
206 * @param t timeout
alshabib7911a052014-10-16 17:49:37 -0700207 */
208 @Override
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700209 public void run(Timeout t) {
Yuta HIGUCHI34198582014-11-10 16:24:58 -0800210 if (isStopped()) {
211 return;
212 }
Thomas Vachuska05453c92015-09-09 14:40:49 -0700213
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700214 if (context.mastershipService().isLocalMaster(device.id())) {
215 log.trace("Sending probes from {}", device.id());
216 ports.forEach(this::sendProbes);
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700217 }
218
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800219 if (!isStopped()) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700220 timeout = Timer.getTimer().newTimeout(this, context.probeRate(), MILLISECONDS);
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800221 }
alshabib7911a052014-10-16 17:49:37 -0700222 }
223
alshabib7911a052014-10-16 17:49:37 -0700224 /**
225 * Creates packet_out LLDP for specified output port.
226 *
227 * @param port the port
228 * @return Packet_out message with LLDP data
229 */
Jonathan Hartb35540a2015-11-17 09:30:56 -0800230 private OutboundPacket createOutBoundLldp(Long port) {
alshabib7911a052014-10-16 17:49:37 -0700231 if (port == null) {
232 return null;
233 }
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800234 ONOSLLDP lldp = getLinkProbe(port);
Ayaka Koshibe48229222016-05-16 18:04:26 -0700235 ethPacket.setSourceMACAddress(context.fingerprint()).setPayload(lldp);
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700236 return new DefaultOutboundPacket(device.id(),
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700237 builder().setOutput(portNumber(port)).build(),
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700238 ByteBuffer.wrap(ethPacket.serialize()));
alshabib7911a052014-10-16 17:49:37 -0700239 }
240
241 /**
242 * Creates packet_out BDDP for specified output port.
243 *
244 * @param port the port
245 * @return Packet_out message with LLDP data
246 */
Jonathan Hartb35540a2015-11-17 09:30:56 -0800247 private OutboundPacket createOutBoundBddp(Long port) {
alshabib7911a052014-10-16 17:49:37 -0700248 if (port == null) {
249 return null;
250 }
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800251 ONOSLLDP lldp = getLinkProbe(port);
Ayaka Koshibe48229222016-05-16 18:04:26 -0700252 bddpEth.setSourceMACAddress(context.fingerprint()).setPayload(lldp);
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700253 return new DefaultOutboundPacket(device.id(),
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700254 builder().setOutput(portNumber(port)).build(),
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700255 ByteBuffer.wrap(bddpEth.serialize()));
alshabib7911a052014-10-16 17:49:37 -0700256 }
257
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800258 private ONOSLLDP getLinkProbe(Long port) {
Ayaka Koshibe48229222016-05-16 18:04:26 -0700259 return ONOSLLDP.onosLLDP(device.id().toString(), device.chassisId(), port.intValue());
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800260 }
261
alshabib7911a052014-10-16 17:49:37 -0700262 private void sendProbes(Long portNumber) {
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700263 if (context.packetService() == null) {
264 return;
265 }
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800266 log.trace("Sending probes out to {}@{}", portNumber, device.id());
Jonathan Hartb35540a2015-11-17 09:30:56 -0800267 OutboundPacket pkt = createOutBoundLldp(portNumber);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700268 context.packetService().emit(pkt);
Jonathan Hartb35540a2015-11-17 09:30:56 -0800269 if (context.useBddp()) {
270 OutboundPacket bpkt = createOutBoundBddp(portNumber);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700271 context.packetService().emit(bpkt);
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700272 }
alshabib7911a052014-10-16 17:49:37 -0700273 }
274
Ray Milkey957390e2016-02-09 10:02:46 -0800275 public boolean containsPort(long portNumber) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700276 return ports.contains(portNumber);
277 }
alshabib7911a052014-10-16 17:49:37 -0700278}