blob: 3631e169e63503f74771ed59c60ec3b9909f43bc [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;
Ayaka Koshibe12c8c082015-12-08 12:48:46 -080020
Yuta HIGUCHI19afc032017-05-20 23:44:17 -070021import io.netty.util.Timeout;
22import io.netty.util.TimerTask;
23
jaegonkimaaa4f832018-03-11 00:48:36 +090024import io.netty.util.internal.StringUtil;
Jonathan Harte8600eb2015-01-12 10:30:45 -080025import org.onlab.packet.Ethernet;
Charles Chan928ff8b2017-05-04 12:22:54 -070026import org.onlab.packet.MacAddress;
Jonathan Harte8600eb2015-01-12 10:30:45 -080027import org.onlab.packet.ONOSLLDP;
28import org.onlab.util.Timer;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.ConnectPoint;
30import org.onosproject.net.Device;
31import org.onosproject.net.DeviceId;
32import org.onosproject.net.Link.Type;
Thomas Vachuska05453c92015-09-09 14:40:49 -070033import org.onosproject.net.LinkKey;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.Port;
35import org.onosproject.net.PortNumber;
36import org.onosproject.net.link.DefaultLinkDescription;
37import org.onosproject.net.link.LinkDescription;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.packet.DefaultOutboundPacket;
39import org.onosproject.net.packet.OutboundPacket;
40import org.onosproject.net.packet.PacketContext;
Ayaka Koshibe48229222016-05-16 18:04:26 -070041import org.onosproject.net.link.ProbedLinkProvider;
alshabib7911a052014-10-16 17:49:37 -070042import org.slf4j.Logger;
43
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070044import java.nio.ByteBuffer;
jaegonkimd9119d0d2018-02-06 23:16:52 +090045import java.util.HashMap;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070046
Thomas Vachuska586bc4f2016-08-19 12:44:26 -070047import static com.google.common.base.Strings.isNullOrEmpty;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070048import static java.util.concurrent.TimeUnit.MILLISECONDS;
jaegonkimd9119d0d2018-02-06 23:16:52 +090049import static org.onosproject.net.AnnotationKeys.PORT_NAME;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070050import static org.onosproject.net.PortNumber.portNumber;
51import static org.onosproject.net.flow.DefaultTrafficTreatment.builder;
52import static org.slf4j.LoggerFactory.getLogger;
53
alshabib7911a052014-10-16 17:49:37 -070054/**
55 * Run discovery process from a physical switch. Ports are initially labeled as
56 * slow ports. When an LLDP is successfully received, label the remote port as
57 * fast. Every probeRate milliseconds, loop over all fast ports and send an
58 * LLDP, send an LLDP for a single slow port. Based on FlowVisor topology
59 * discovery implementation.
alshabib7911a052014-10-16 17:49:37 -070060 */
Ray Milkey957390e2016-02-09 10:02:46 -080061public class LinkDiscovery implements TimerTask {
alshabib7911a052014-10-16 17:49:37 -070062
alshabib7911a052014-10-16 17:49:37 -070063 private final Logger log = getLogger(getClass());
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070064
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070065 private final Device device;
Ray Milkey957390e2016-02-09 10:02:46 -080066 private final LinkDiscoveryContext context;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070067
alshabib7911a052014-10-16 17:49:37 -070068 private final Ethernet ethPacket;
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -070069 private final Ethernet bddpEth;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070070
71 private Timeout timeout;
72 private volatile boolean isStopped;
Thomas Vachuska9280e192018-03-06 11:19:29 -080073
Thomas Vachuska05453c92015-09-09 14:40:49 -070074 // Set of ports to be probed
jaegonkimd9119d0d2018-02-06 23:16:52 +090075 private final HashMap<Long, String> portMap = Maps.newHashMap();
Thomas Vachuska05453c92015-09-09 14:40:49 -070076
alshabib7911a052014-10-16 17:49:37 -070077 /**
78 * Instantiates discovery manager for the given physical switch. Creates a
79 * generic LLDP packet that will be customized for the port it is sent out on.
80 * Starts the the timer for the discovery process.
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070081 *
Thomas Vachuska05453c92015-09-09 14:40:49 -070082 * @param device the physical switch
83 * @param context discovery context
alshabib7911a052014-10-16 17:49:37 -070084 */
Ray Milkey957390e2016-02-09 10:02:46 -080085 public LinkDiscovery(Device device, LinkDiscoveryContext context) {
alshabib7911a052014-10-16 17:49:37 -070086 this.device = device;
Thomas Vachuska05453c92015-09-09 14:40:49 -070087 this.context = context;
alshabib3d643ec2014-10-22 18:33:00 -070088
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070089 ethPacket = new Ethernet();
90 ethPacket.setEtherType(Ethernet.TYPE_LLDP);
Charles Chan928ff8b2017-05-04 12:22:54 -070091 ethPacket.setDestinationMACAddress(MacAddress.ONOS_LLDP);
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070092 ethPacket.setPad(true);
alshabib7911a052014-10-16 17:49:37 -070093
Thomas Vachuska05453c92015-09-09 14:40:49 -070094 bddpEth = new Ethernet();
Thomas Vachuska05453c92015-09-09 14:40:49 -070095 bddpEth.setEtherType(Ethernet.TYPE_BSN);
Charles Chan928ff8b2017-05-04 12:22:54 -070096 bddpEth.setDestinationMACAddress(MacAddress.BROADCAST);
Thomas Vachuska05453c92015-09-09 14:40:49 -070097 bddpEth.setPad(true);
alshabib7911a052014-10-16 17:49:37 -070098
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070099 isStopped = true;
alshabib7911a052014-10-16 17:49:37 -0700100 start();
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700101 log.debug("Started discovery manager for switch {}", device.id());
alshabib7911a052014-10-16 17:49:37 -0700102
103 }
104
Ray Milkey957390e2016-02-09 10:02:46 -0800105 public synchronized void stop() {
Jon Hall3edc08a2015-09-14 16:59:07 -0700106 if (!isStopped) {
107 isStopped = true;
108 timeout.cancel();
109 } else {
110 log.warn("LinkDiscovery stopped multiple times?");
111 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700112 }
113
Ray Milkey957390e2016-02-09 10:02:46 -0800114 public synchronized void start() {
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700115 if (isStopped) {
116 isStopped = false;
Yuta HIGUCHI19afc032017-05-20 23:44:17 -0700117 timeout = Timer.newTimeout(this, 0, MILLISECONDS);
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700118 } else {
119 log.warn("LinkDiscovery started multiple times?");
120 }
121 }
122
Ray Milkey957390e2016-02-09 10:02:46 -0800123 public synchronized boolean isStopped() {
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700124 return isStopped || timeout.isCancelled();
125 }
126
alshabib7911a052014-10-16 17:49:37 -0700127 /**
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700128 * Add physical port to discovery process.
alshabib7911a052014-10-16 17:49:37 -0700129 * Send out initial LLDP and label it as slow port.
130 *
131 * @param port the port
132 */
Ray Milkey957390e2016-02-09 10:02:46 -0800133 public void addPort(Port port) {
jaegonkimd9119d0d2018-02-06 23:16:52 +0900134 Long portNum = port.number().toLong();
135 String portName = port.annotations().value(PORT_NAME);
jaegonkimaaa4f832018-03-11 00:48:36 +0900136 if (portName == null) {
137 portName = StringUtil.EMPTY_STRING;
138 }
jaegonkimd9119d0d2018-02-06 23:16:52 +0900139
You Wang168aa622018-03-08 14:01:37 -0800140 boolean newPort = !containsPort(portNum);
jaegonkimd9119d0d2018-02-06 23:16:52 +0900141 portMap.put(portNum, portName);
142
Thomas Vachuska05453c92015-09-09 14:40:49 -0700143 boolean isMaster = context.mastershipService().isLocalMaster(device.id());
Jonathan Hart45066bc2015-07-28 11:18:34 -0700144 if (newPort && isMaster) {
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700145 log.debug("Sending initial probe to port {}@{}", port.number().toLong(), device.id());
jaegonkimd9119d0d2018-02-06 23:16:52 +0900146 sendProbes(portNum, portName);
alshabib7911a052014-10-16 17:49:37 -0700147 }
alshabib7911a052014-10-16 17:49:37 -0700148 }
149
150 /**
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700151 * removed physical port from discovery process.
152 * @param port the port number
153 */
Ray Milkey957390e2016-02-09 10:02:46 -0800154 public void removePort(PortNumber port) {
jaegonkimd9119d0d2018-02-06 23:16:52 +0900155 portMap.remove(port.toLong());
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700156 }
157
158 /**
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700159 * Handles an incoming LLDP packet. Creates link in topology and adds the
160 * link for staleness tracking.
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700161 *
Thomas Vachuska05453c92015-09-09 14:40:49 -0700162 * @param packetContext packet context
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800163 * @return true if handled
alshabib7911a052014-10-16 17:49:37 -0700164 */
Ray Milkey957390e2016-02-09 10:02:46 -0800165 public boolean handleLldp(PacketContext packetContext) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700166 Ethernet eth = packetContext.inPacket().parsed();
Jonathan Harte8600eb2015-01-12 10:30:45 -0800167 if (eth == null) {
168 return false;
169 }
170
alshabib7911a052014-10-16 17:49:37 -0700171 ONOSLLDP onoslldp = ONOSLLDP.parseONOSLLDP(eth);
172 if (onoslldp != null) {
Ayaka Koshibe48229222016-05-16 18:04:26 -0700173 Type lt;
174 if (notMy(eth.getSourceMAC().toString())) {
175 lt = Type.EDGE;
176 } else {
177 lt = eth.getEtherType() == Ethernet.TYPE_LLDP ?
178 Type.DIRECT : Type.INDIRECT;
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800179 }
180
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700181 PortNumber srcPort = portNumber(onoslldp.getPort());
Thomas Vachuska05453c92015-09-09 14:40:49 -0700182 PortNumber dstPort = packetContext.inPacket().receivedFrom().port();
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700183
Thomas Vachuska586bc4f2016-08-19 12:44:26 -0700184 String idString = onoslldp.getDeviceString();
185 if (!isNullOrEmpty(idString)) {
186 DeviceId srcDeviceId = DeviceId.deviceId(idString);
187 DeviceId dstDeviceId = packetContext.inPacket().receivedFrom().deviceId();
alshabib7911a052014-10-16 17:49:37 -0700188
Thomas Vachuska586bc4f2016-08-19 12:44:26 -0700189 ConnectPoint src = new ConnectPoint(srcDeviceId, srcPort);
190 ConnectPoint dst = new ConnectPoint(dstDeviceId, dstPort);
191
192 LinkDescription ld = new DefaultLinkDescription(src, dst, lt);
193 try {
194 context.providerService().linkDetected(ld);
195 context.touchLink(LinkKey.linkKey(src, dst));
196 } catch (IllegalStateException e) {
197 return true;
198 }
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700199 return true;
200 }
alshabib7911a052014-10-16 17:49:37 -0700201 }
202 return false;
203 }
204
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800205 // true if *NOT* this cluster's own probe.
Ayaka Koshibe48229222016-05-16 18:04:26 -0700206 private boolean notMy(String mac) {
207 // if we are using DEFAULT_MAC, clustering hadn't initialized, so conservative 'yes'
208 String ourMac = context.fingerprint();
209 if (ProbedLinkProvider.defaultMac().equalsIgnoreCase(ourMac)) {
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800210 return true;
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800211 }
Ayaka Koshibe48229222016-05-16 18:04:26 -0700212 return !mac.equalsIgnoreCase(ourMac);
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -0800213 }
alshabib7911a052014-10-16 17:49:37 -0700214
alshabib7911a052014-10-16 17:49:37 -0700215 /**
216 * Execute this method every t milliseconds. Loops over all ports
217 * labeled as fast and sends out an LLDP. Send out an LLDP on a single slow
218 * port.
219 *
220 * @param t timeout
alshabib7911a052014-10-16 17:49:37 -0700221 */
222 @Override
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700223 public void run(Timeout t) {
Yuta HIGUCHI34198582014-11-10 16:24:58 -0800224 if (isStopped()) {
225 return;
226 }
Thomas Vachuska05453c92015-09-09 14:40:49 -0700227
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700228 if (context.mastershipService().isLocalMaster(device.id())) {
229 log.trace("Sending probes from {}", device.id());
Ray Milkey1ab441b2018-03-06 15:33:44 -0800230 ImmutableMap.copyOf(portMap).forEach(this::sendProbes);
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700231 }
232
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800233 if (!isStopped()) {
Yuta HIGUCHI19afc032017-05-20 23:44:17 -0700234 timeout = t.timer().newTimeout(this, context.probeRate(), MILLISECONDS);
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800235 }
alshabib7911a052014-10-16 17:49:37 -0700236 }
237
alshabib7911a052014-10-16 17:49:37 -0700238 /**
239 * Creates packet_out LLDP for specified output port.
240 *
jaegonkimd9119d0d2018-02-06 23:16:52 +0900241 * @param portNumber the port
242 * @param portDesc the port description
alshabib7911a052014-10-16 17:49:37 -0700243 * @return Packet_out message with LLDP data
244 */
jaegonkimd9119d0d2018-02-06 23:16:52 +0900245 private OutboundPacket createOutBoundLldp(Long portNumber, String portDesc) {
246 if (portNumber == null) {
alshabib7911a052014-10-16 17:49:37 -0700247 return null;
248 }
jaegonkimd9119d0d2018-02-06 23:16:52 +0900249 ONOSLLDP lldp = getLinkProbe(portNumber, portDesc);
Ayaka Koshibe48229222016-05-16 18:04:26 -0700250 ethPacket.setSourceMACAddress(context.fingerprint()).setPayload(lldp);
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700251 return new DefaultOutboundPacket(device.id(),
jaegonkimd9119d0d2018-02-06 23:16:52 +0900252 builder().setOutput(portNumber(portNumber)).build(),
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700253 ByteBuffer.wrap(ethPacket.serialize()));
alshabib7911a052014-10-16 17:49:37 -0700254 }
255
256 /**
257 * Creates packet_out BDDP for specified output port.
258 *
jaegonkimd9119d0d2018-02-06 23:16:52 +0900259 * @param portNumber the port
260 * @param portDesc the port description
alshabib7911a052014-10-16 17:49:37 -0700261 * @return Packet_out message with LLDP data
262 */
jaegonkimd9119d0d2018-02-06 23:16:52 +0900263 private OutboundPacket createOutBoundBddp(Long portNumber, String portDesc) {
264 if (portNumber == null) {
alshabib7911a052014-10-16 17:49:37 -0700265 return null;
266 }
jaegonkimd9119d0d2018-02-06 23:16:52 +0900267 ONOSLLDP lldp = getLinkProbe(portNumber, portDesc);
Ayaka Koshibe48229222016-05-16 18:04:26 -0700268 bddpEth.setSourceMACAddress(context.fingerprint()).setPayload(lldp);
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700269 return new DefaultOutboundPacket(device.id(),
jaegonkimd9119d0d2018-02-06 23:16:52 +0900270 builder().setOutput(portNumber(portNumber)).build(),
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700271 ByteBuffer.wrap(bddpEth.serialize()));
alshabib7911a052014-10-16 17:49:37 -0700272 }
273
jaegonkimd9119d0d2018-02-06 23:16:52 +0900274 private ONOSLLDP getLinkProbe(Long portNumber, String portDesc) {
275 return ONOSLLDP.onosLLDP(device.id().toString(), device.chassisId(), portNumber.intValue(), portDesc);
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800276 }
277
jaegonkimd9119d0d2018-02-06 23:16:52 +0900278 private void sendProbes(Long portNumber, String portDesc) {
Ray Milkeyd9bbde82016-06-09 11:35:00 -0700279 if (context.packetService() == null) {
280 return;
281 }
Jon Hall274cecb2017-08-09 12:15:48 -0700282 log.trace("Sending probes out of {}@{}", portNumber, device.id());
jaegonkimd9119d0d2018-02-06 23:16:52 +0900283 OutboundPacket pkt = createOutBoundLldp(portNumber, portDesc);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700284 context.packetService().emit(pkt);
Jonathan Hartb35540a2015-11-17 09:30:56 -0800285 if (context.useBddp()) {
jaegonkimd9119d0d2018-02-06 23:16:52 +0900286 OutboundPacket bpkt = createOutBoundBddp(portNumber, portDesc);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700287 context.packetService().emit(bpkt);
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700288 }
alshabib7911a052014-10-16 17:49:37 -0700289 }
290
Ray Milkey957390e2016-02-09 10:02:46 -0800291 public boolean containsPort(long portNumber) {
jaegonkimd9119d0d2018-02-06 23:16:52 +0900292 return portMap.containsKey(portNumber);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700293 }
alshabib7911a052014-10-16 17:49:37 -0700294}