blob: 4b962ae5627bbe96ac6ad27f7de2df345ad8b01c [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;
alshabib7911a052014-10-16 17:49:37 -070019import org.jboss.netty.util.Timeout;
20import org.jboss.netty.util.TimerTask;
Jonathan Harte8600eb2015-01-12 10:30:45 -080021import org.onlab.packet.Ethernet;
22import org.onlab.packet.ONOSLLDP;
23import org.onlab.util.Timer;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.Device;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.Link.Type;
Thomas Vachuska05453c92015-09-09 14:40:49 -070028import org.onosproject.net.LinkKey;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.Port;
30import org.onosproject.net.PortNumber;
31import org.onosproject.net.link.DefaultLinkDescription;
32import org.onosproject.net.link.LinkDescription;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.packet.DefaultOutboundPacket;
34import org.onosproject.net.packet.OutboundPacket;
35import org.onosproject.net.packet.PacketContext;
alshabib7911a052014-10-16 17:49:37 -070036import org.slf4j.Logger;
37
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070038import java.nio.ByteBuffer;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070039import java.util.Set;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070040
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070041import static java.util.concurrent.TimeUnit.MILLISECONDS;
42import static org.onosproject.net.PortNumber.portNumber;
43import static org.onosproject.net.flow.DefaultTrafficTreatment.builder;
44import static org.slf4j.LoggerFactory.getLogger;
45
alshabib7911a052014-10-16 17:49:37 -070046/**
47 * Run discovery process from a physical switch. Ports are initially labeled as
48 * slow ports. When an LLDP is successfully received, label the remote port as
49 * fast. Every probeRate milliseconds, loop over all fast ports and send an
50 * LLDP, send an LLDP for a single slow port. Based on FlowVisor topology
51 * discovery implementation.
alshabib7911a052014-10-16 17:49:37 -070052 */
Thomas Vachuskae4ebac92015-09-10 11:39:05 -070053class LinkDiscovery implements TimerTask {
alshabib7911a052014-10-16 17:49:37 -070054
alshabib7911a052014-10-16 17:49:37 -070055 private final Logger log = getLogger(getClass());
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070056
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070057 private static final String SRC_MAC = "DE:AD:BE:EF:BA:11";
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070058
59 private final Device device;
Thomas Vachuska05453c92015-09-09 14:40:49 -070060 private final DiscoveryContext context;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070061
alshabib7911a052014-10-16 17:49:37 -070062 private final ONOSLLDP lldpPacket;
63 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;
68
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 */
Thomas Vachuskae4ebac92015-09-10 11:39:05 -070080 LinkDiscovery(Device device, DiscoveryContext 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 lldpPacket = new ONOSLLDP();
85 lldpPacket.setChassisId(device.chassisId());
86 lldpPacket.setDevice(device.id().toString());
alshabib7911a052014-10-16 17:49:37 -070087
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070088 ethPacket = new Ethernet();
89 ethPacket.setEtherType(Ethernet.TYPE_LLDP);
90 ethPacket.setDestinationMACAddress(ONOSLLDP.LLDP_NICIRA);
91 ethPacket.setPayload(this.lldpPacket);
92 ethPacket.setPad(true);
alshabib7911a052014-10-16 17:49:37 -070093
Thomas Vachuska05453c92015-09-09 14:40:49 -070094 bddpEth = new Ethernet();
95 bddpEth.setPayload(lldpPacket);
96 bddpEth.setEtherType(Ethernet.TYPE_BSN);
97 bddpEth.setDestinationMACAddress(ONOSLLDP.BDDP_MULTICAST);
98 bddpEth.setPad(true);
alshabib7911a052014-10-16 17:49:37 -070099
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700100 isStopped = true;
alshabib7911a052014-10-16 17:49:37 -0700101 start();
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700102 log.debug("Started discovery manager for switch {}", device.id());
alshabib7911a052014-10-16 17:49:37 -0700103
104 }
105
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700106 synchronized void stop() {
Jon Hall3edc08a2015-09-14 16:59:07 -0700107 if (!isStopped) {
108 isStopped = true;
109 timeout.cancel();
110 } else {
111 log.warn("LinkDiscovery stopped multiple times?");
112 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700113 }
114
115 synchronized void start() {
116 if (isStopped) {
117 isStopped = false;
118 timeout = Timer.getTimer().newTimeout(this, 0, MILLISECONDS);
119 } else {
120 log.warn("LinkDiscovery started multiple times?");
121 }
122 }
123
124 synchronized boolean isStopped() {
125 return isStopped || timeout.isCancelled();
126 }
127
alshabib7911a052014-10-16 17:49:37 -0700128 /**
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700129 * Add physical port to discovery process.
alshabib7911a052014-10-16 17:49:37 -0700130 * Send out initial LLDP and label it as slow port.
131 *
132 * @param port the port
133 */
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700134 void addPort(Port port) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700135 boolean newPort = ports.add(port.number().toLong());
136 boolean isMaster = context.mastershipService().isLocalMaster(device.id());
Jonathan Hart45066bc2015-07-28 11:18:34 -0700137 if (newPort && isMaster) {
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700138 log.debug("Sending initial probe to port {}@{}", port.number().toLong(), device.id());
Jonathan Hart45066bc2015-07-28 11:18:34 -0700139 sendProbes(port.number().toLong());
alshabib7911a052014-10-16 17:49:37 -0700140 }
alshabib7911a052014-10-16 17:49:37 -0700141 }
142
143 /**
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700144 * removed physical port from discovery process.
145 * @param port the port number
146 */
147 void removePort(PortNumber port) {
148 ports.remove(port.toLong());
149 }
150
151 /**
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700152 * Handles an incoming LLDP packet. Creates link in topology and adds the
153 * link for staleness tracking.
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700154 *
Thomas Vachuska05453c92015-09-09 14:40:49 -0700155 * @param packetContext packet context
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800156 * @return true if handled
alshabib7911a052014-10-16 17:49:37 -0700157 */
Jonathan Hartb35540a2015-11-17 09:30:56 -0800158 boolean handleLldp(PacketContext packetContext) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700159 Ethernet eth = packetContext.inPacket().parsed();
Jonathan Harte8600eb2015-01-12 10:30:45 -0800160 if (eth == null) {
161 return false;
162 }
163
alshabib7911a052014-10-16 17:49:37 -0700164 ONOSLLDP onoslldp = ONOSLLDP.parseONOSLLDP(eth);
165 if (onoslldp != null) {
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700166 PortNumber srcPort = portNumber(onoslldp.getPort());
Thomas Vachuska05453c92015-09-09 14:40:49 -0700167 PortNumber dstPort = packetContext.inPacket().receivedFrom().port();
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700168 DeviceId srcDeviceId = DeviceId.deviceId(onoslldp.getDeviceString());
Thomas Vachuska05453c92015-09-09 14:40:49 -0700169 DeviceId dstDeviceId = packetContext.inPacket().receivedFrom().deviceId();
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700170
alshabib7911a052014-10-16 17:49:37 -0700171 ConnectPoint src = new ConnectPoint(srcDeviceId, srcPort);
172 ConnectPoint dst = new ConnectPoint(dstDeviceId, dstPort);
173
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700174 LinkDescription ld = eth.getEtherType() == Ethernet.TYPE_LLDP ?
175 new DefaultLinkDescription(src, dst, Type.DIRECT) :
176 new DefaultLinkDescription(src, dst, Type.INDIRECT);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700177
178 try {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700179 context.providerService().linkDetected(ld);
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700180 context.touchLink(LinkKey.linkKey(src, dst));
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700181 } catch (IllegalStateException e) {
182 return true;
183 }
alshabib7911a052014-10-16 17:49:37 -0700184 return true;
185 }
186 return false;
187 }
188
189
alshabib7911a052014-10-16 17:49:37 -0700190 /**
191 * Execute this method every t milliseconds. Loops over all ports
192 * labeled as fast and sends out an LLDP. Send out an LLDP on a single slow
193 * port.
194 *
195 * @param t timeout
alshabib7911a052014-10-16 17:49:37 -0700196 */
197 @Override
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700198 public void run(Timeout t) {
Yuta HIGUCHI34198582014-11-10 16:24:58 -0800199 if (isStopped()) {
200 return;
201 }
Thomas Vachuska05453c92015-09-09 14:40:49 -0700202
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700203 if (context.mastershipService().isLocalMaster(device.id())) {
204 log.trace("Sending probes from {}", device.id());
205 ports.forEach(this::sendProbes);
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700206 }
207
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800208 if (!isStopped()) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700209 timeout = Timer.getTimer().newTimeout(this, context.probeRate(), MILLISECONDS);
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800210 }
alshabib7911a052014-10-16 17:49:37 -0700211 }
212
alshabib7911a052014-10-16 17:49:37 -0700213 /**
214 * Creates packet_out LLDP for specified output port.
215 *
216 * @param port the port
217 * @return Packet_out message with LLDP data
218 */
Jonathan Hartb35540a2015-11-17 09:30:56 -0800219 private OutboundPacket createOutBoundLldp(Long port) {
alshabib7911a052014-10-16 17:49:37 -0700220 if (port == null) {
221 return null;
222 }
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700223 lldpPacket.setPortId(port.intValue());
224 ethPacket.setSourceMACAddress(SRC_MAC);
225 return new DefaultOutboundPacket(device.id(),
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700226 builder().setOutput(portNumber(port)).build(),
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700227 ByteBuffer.wrap(ethPacket.serialize()));
alshabib7911a052014-10-16 17:49:37 -0700228 }
229
230 /**
231 * Creates packet_out BDDP for specified output port.
232 *
233 * @param port the port
234 * @return Packet_out message with LLDP data
235 */
Jonathan Hartb35540a2015-11-17 09:30:56 -0800236 private OutboundPacket createOutBoundBddp(Long port) {
alshabib7911a052014-10-16 17:49:37 -0700237 if (port == null) {
238 return null;
239 }
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700240 lldpPacket.setPortId(port.intValue());
241 bddpEth.setSourceMACAddress(SRC_MAC);
242 return new DefaultOutboundPacket(device.id(),
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700243 builder().setOutput(portNumber(port)).build(),
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700244 ByteBuffer.wrap(bddpEth.serialize()));
alshabib7911a052014-10-16 17:49:37 -0700245 }
246
247 private void sendProbes(Long portNumber) {
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800248 log.trace("Sending probes out to {}@{}", portNumber, device.id());
Jonathan Hartb35540a2015-11-17 09:30:56 -0800249 OutboundPacket pkt = createOutBoundLldp(portNumber);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700250 context.packetService().emit(pkt);
Jonathan Hartb35540a2015-11-17 09:30:56 -0800251 if (context.useBddp()) {
252 OutboundPacket bpkt = createOutBoundBddp(portNumber);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700253 context.packetService().emit(bpkt);
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700254 }
alshabib7911a052014-10-16 17:49:37 -0700255 }
256
Thomas Vachuska05453c92015-09-09 14:40:49 -0700257 boolean containsPort(long portNumber) {
258 return ports.contains(portNumber);
259 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700260
alshabib7911a052014-10-16 17:49:37 -0700261}