blob: 11b8cd36adc21f611d52a6736fe1792947ff6bf7 [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.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;
46
alshabib7911a052014-10-16 17:49:37 -070047/**
48 * Run discovery process from a physical switch. Ports are initially labeled as
49 * slow ports. When an LLDP is successfully received, label the remote port as
50 * fast. Every probeRate milliseconds, loop over all fast ports and send an
51 * LLDP, send an LLDP for a single slow port. Based on FlowVisor topology
52 * discovery implementation.
alshabib7911a052014-10-16 17:49:37 -070053 */
Thomas Vachuskae4ebac92015-09-10 11:39:05 -070054class LinkDiscovery implements TimerTask {
alshabib7911a052014-10-16 17:49:37 -070055
alshabib7911a052014-10-16 17:49:37 -070056 private final Logger log = getLogger(getClass());
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070057
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070058 private static final String SRC_MAC = "DE:AD:BE:EF:BA:11";
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070059
60 private final Device device;
Thomas Vachuska05453c92015-09-09 14:40:49 -070061 private final DiscoveryContext 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;
Ayaka Koshibe12c8c082015-12-08 12:48:46 -080068 // This LinkDiscovery can handle remote link probes (default false).
69 private volatile boolean fingerprinted;
Thomas Vachuska05453c92015-09-09 14:40:49 -070070 // Set of ports to be probed
71 private final Set<Long> ports = Sets.newConcurrentHashSet();
72
alshabib7911a052014-10-16 17:49:37 -070073 /**
74 * Instantiates discovery manager for the given physical switch. Creates a
75 * generic LLDP packet that will be customized for the port it is sent out on.
76 * Starts the the timer for the discovery process.
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070077 *
Thomas Vachuska05453c92015-09-09 14:40:49 -070078 * @param device the physical switch
79 * @param context discovery context
alshabib7911a052014-10-16 17:49:37 -070080 */
Thomas Vachuskae4ebac92015-09-10 11:39:05 -070081 LinkDiscovery(Device device, DiscoveryContext context) {
alshabib7911a052014-10-16 17:49:37 -070082 this.device = device;
Thomas Vachuska05453c92015-09-09 14:40:49 -070083 this.context = context;
alshabib3d643ec2014-10-22 18:33:00 -070084
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070085 ethPacket = new Ethernet();
86 ethPacket.setEtherType(Ethernet.TYPE_LLDP);
87 ethPacket.setDestinationMACAddress(ONOSLLDP.LLDP_NICIRA);
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070088 ethPacket.setPad(true);
alshabib7911a052014-10-16 17:49:37 -070089
Thomas Vachuska05453c92015-09-09 14:40:49 -070090 bddpEth = new Ethernet();
Thomas Vachuska05453c92015-09-09 14:40:49 -070091 bddpEth.setEtherType(Ethernet.TYPE_BSN);
92 bddpEth.setDestinationMACAddress(ONOSLLDP.BDDP_MULTICAST);
93 bddpEth.setPad(true);
alshabib7911a052014-10-16 17:49:37 -070094
Ayaka Koshibe12c8c082015-12-08 12:48:46 -080095 fingerprinted = false;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070096 isStopped = true;
alshabib7911a052014-10-16 17:49:37 -070097 start();
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070098 log.debug("Started discovery manager for switch {}", device.id());
alshabib7911a052014-10-16 17:49:37 -070099
100 }
101
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700102 synchronized void stop() {
Jon Hall3edc08a2015-09-14 16:59:07 -0700103 if (!isStopped) {
104 isStopped = true;
105 timeout.cancel();
106 } else {
107 log.warn("LinkDiscovery stopped multiple times?");
108 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700109 }
110
111 synchronized void start() {
112 if (isStopped) {
113 isStopped = false;
114 timeout = Timer.getTimer().newTimeout(this, 0, MILLISECONDS);
115 } else {
116 log.warn("LinkDiscovery started multiple times?");
117 }
118 }
119
120 synchronized boolean isStopped() {
121 return isStopped || timeout.isCancelled();
122 }
123
alshabib7911a052014-10-16 17:49:37 -0700124 /**
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700125 * Add physical port to discovery process.
alshabib7911a052014-10-16 17:49:37 -0700126 * Send out initial LLDP and label it as slow port.
127 *
128 * @param port the port
129 */
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700130 void addPort(Port port) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700131 boolean newPort = ports.add(port.number().toLong());
132 boolean isMaster = context.mastershipService().isLocalMaster(device.id());
Jonathan Hart45066bc2015-07-28 11:18:34 -0700133 if (newPort && isMaster) {
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700134 log.debug("Sending initial probe to port {}@{}", port.number().toLong(), device.id());
Jonathan Hart45066bc2015-07-28 11:18:34 -0700135 sendProbes(port.number().toLong());
alshabib7911a052014-10-16 17:49:37 -0700136 }
alshabib7911a052014-10-16 17:49:37 -0700137 }
138
139 /**
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700140 * removed physical port from discovery process.
141 * @param port the port number
142 */
143 void removePort(PortNumber port) {
144 ports.remove(port.toLong());
145 }
146
147 /**
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700148 * Handles an incoming LLDP packet. Creates link in topology and adds the
149 * link for staleness tracking.
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700150 *
Thomas Vachuska05453c92015-09-09 14:40:49 -0700151 * @param packetContext packet context
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800152 * @return true if handled
alshabib7911a052014-10-16 17:49:37 -0700153 */
Jonathan Hartb35540a2015-11-17 09:30:56 -0800154 boolean handleLldp(PacketContext packetContext) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700155 Ethernet eth = packetContext.inPacket().parsed();
Jonathan Harte8600eb2015-01-12 10:30:45 -0800156 if (eth == null) {
157 return false;
158 }
159
alshabib7911a052014-10-16 17:49:37 -0700160 ONOSLLDP onoslldp = ONOSLLDP.parseONOSLLDP(eth);
161 if (onoslldp != null) {
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700162 PortNumber srcPort = portNumber(onoslldp.getPort());
Thomas Vachuska05453c92015-09-09 14:40:49 -0700163 PortNumber dstPort = packetContext.inPacket().receivedFrom().port();
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700164 DeviceId srcDeviceId = DeviceId.deviceId(onoslldp.getDeviceString());
Thomas Vachuska05453c92015-09-09 14:40:49 -0700165 DeviceId dstDeviceId = packetContext.inPacket().receivedFrom().deviceId();
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700166
alshabib7911a052014-10-16 17:49:37 -0700167 ConnectPoint src = new ConnectPoint(srcDeviceId, srcPort);
168 ConnectPoint dst = new ConnectPoint(dstDeviceId, dstPort);
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);
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700176 context.touchLink(LinkKey.linkKey(src, dst));
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700177 } catch (IllegalStateException e) {
178 return true;
179 }
alshabib7911a052014-10-16 17:49:37 -0700180 return true;
181 }
182 return false;
183 }
184
185
alshabib7911a052014-10-16 17:49:37 -0700186 /**
187 * Execute this method every t milliseconds. Loops over all ports
188 * labeled as fast and sends out an LLDP. Send out an LLDP on a single slow
189 * port.
190 *
191 * @param t timeout
alshabib7911a052014-10-16 17:49:37 -0700192 */
193 @Override
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700194 public void run(Timeout t) {
Yuta HIGUCHI34198582014-11-10 16:24:58 -0800195 if (isStopped()) {
196 return;
197 }
Thomas Vachuska05453c92015-09-09 14:40:49 -0700198
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700199 if (context.mastershipService().isLocalMaster(device.id())) {
200 log.trace("Sending probes from {}", device.id());
201 ports.forEach(this::sendProbes);
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700202 }
203
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800204 if (!isStopped()) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700205 timeout = Timer.getTimer().newTimeout(this, context.probeRate(), MILLISECONDS);
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800206 }
alshabib7911a052014-10-16 17:49:37 -0700207 }
208
alshabib7911a052014-10-16 17:49:37 -0700209 /**
210 * Creates packet_out LLDP for specified output port.
211 *
212 * @param port the port
213 * @return Packet_out message with LLDP data
214 */
Jonathan Hartb35540a2015-11-17 09:30:56 -0800215 private OutboundPacket createOutBoundLldp(Long port) {
alshabib7911a052014-10-16 17:49:37 -0700216 if (port == null) {
217 return null;
218 }
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800219 ONOSLLDP lldp = getLinkProbe(port);
220 ethPacket.setSourceMACAddress(SRC_MAC).setPayload(lldp);
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700221 return new DefaultOutboundPacket(device.id(),
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700222 builder().setOutput(portNumber(port)).build(),
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700223 ByteBuffer.wrap(ethPacket.serialize()));
alshabib7911a052014-10-16 17:49:37 -0700224 }
225
226 /**
227 * Creates packet_out BDDP for specified output port.
228 *
229 * @param port the port
230 * @return Packet_out message with LLDP data
231 */
Jonathan Hartb35540a2015-11-17 09:30:56 -0800232 private OutboundPacket createOutBoundBddp(Long port) {
alshabib7911a052014-10-16 17:49:37 -0700233 if (port == null) {
234 return null;
235 }
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800236 ONOSLLDP lldp = getLinkProbe(port);
237 bddpEth.setSourceMACAddress(SRC_MAC).setPayload(lldp);
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700238 return new DefaultOutboundPacket(device.id(),
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700239 builder().setOutput(portNumber(port)).build(),
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700240 ByteBuffer.wrap(bddpEth.serialize()));
alshabib7911a052014-10-16 17:49:37 -0700241 }
242
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800243 private ONOSLLDP getLinkProbe(Long port) {
244 return fingerprinted
245 ? ONOSLLDP.fingerprintedLLDP(device.id().toString(), device.chassisId(),
246 port.intValue(), context.fingerprint())
247 : ONOSLLDP.onosLLDP(device.id().toString(), device.chassisId(),
248 port.intValue());
249 }
250
alshabib7911a052014-10-16 17:49:37 -0700251 private void sendProbes(Long portNumber) {
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800252 log.trace("Sending probes out to {}@{}", portNumber, device.id());
Jonathan Hartb35540a2015-11-17 09:30:56 -0800253 OutboundPacket pkt = createOutBoundLldp(portNumber);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700254 context.packetService().emit(pkt);
Jonathan Hartb35540a2015-11-17 09:30:56 -0800255 if (context.useBddp()) {
256 OutboundPacket bpkt = createOutBoundBddp(portNumber);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700257 context.packetService().emit(bpkt);
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700258 }
alshabib7911a052014-10-16 17:49:37 -0700259 }
260
Thomas Vachuska05453c92015-09-09 14:40:49 -0700261 boolean containsPort(long portNumber) {
262 return ports.contains(portNumber);
263 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700264
Ayaka Koshibe12c8c082015-12-08 12:48:46 -0800265 protected void enableFingerprint() {
266 fingerprinted = true;
267 }
268
269 protected void disableFingerprint() {
270 fingerprinted = false;
271 }
alshabib7911a052014-10-16 17:49:37 -0700272}