blob: 550262459c5fc84522c43f4300a02a0f639b0093 [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;
64 private 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() {
107 isStopped = true;
108 timeout.cancel();
109 }
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 /**
125 * Add physical port port to discovery process.
126 * 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 /**
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700140 * Handles an incoming LLDP packet. Creates link in topology and adds the
141 * link for staleness tracking.
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700142 *
Thomas Vachuska05453c92015-09-09 14:40:49 -0700143 * @param packetContext packet context
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800144 * @return true if handled
alshabib7911a052014-10-16 17:49:37 -0700145 */
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700146 boolean handleLLDP(PacketContext packetContext) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700147 Ethernet eth = packetContext.inPacket().parsed();
Jonathan Harte8600eb2015-01-12 10:30:45 -0800148 if (eth == null) {
149 return false;
150 }
151
alshabib7911a052014-10-16 17:49:37 -0700152 ONOSLLDP onoslldp = ONOSLLDP.parseONOSLLDP(eth);
153 if (onoslldp != null) {
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700154 PortNumber srcPort = portNumber(onoslldp.getPort());
Thomas Vachuska05453c92015-09-09 14:40:49 -0700155 PortNumber dstPort = packetContext.inPacket().receivedFrom().port();
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700156 DeviceId srcDeviceId = DeviceId.deviceId(onoslldp.getDeviceString());
Thomas Vachuska05453c92015-09-09 14:40:49 -0700157 DeviceId dstDeviceId = packetContext.inPacket().receivedFrom().deviceId();
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700158
alshabib7911a052014-10-16 17:49:37 -0700159 ConnectPoint src = new ConnectPoint(srcDeviceId, srcPort);
160 ConnectPoint dst = new ConnectPoint(dstDeviceId, dstPort);
161
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700162 LinkDescription ld = eth.getEtherType() == Ethernet.TYPE_LLDP ?
163 new DefaultLinkDescription(src, dst, Type.DIRECT) :
164 new DefaultLinkDescription(src, dst, Type.INDIRECT);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700165
166 try {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700167 context.providerService().linkDetected(ld);
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700168 context.touchLink(LinkKey.linkKey(src, dst));
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700169 } catch (IllegalStateException e) {
170 return true;
171 }
alshabib7911a052014-10-16 17:49:37 -0700172 return true;
173 }
174 return false;
175 }
176
177
alshabib7911a052014-10-16 17:49:37 -0700178 /**
179 * Execute this method every t milliseconds. Loops over all ports
180 * labeled as fast and sends out an LLDP. Send out an LLDP on a single slow
181 * port.
182 *
183 * @param t timeout
alshabib7911a052014-10-16 17:49:37 -0700184 */
185 @Override
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700186 public void run(Timeout t) {
Yuta HIGUCHI34198582014-11-10 16:24:58 -0800187 if (isStopped()) {
188 return;
189 }
Thomas Vachuska05453c92015-09-09 14:40:49 -0700190
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700191 if (context.mastershipService().isLocalMaster(device.id())) {
192 log.trace("Sending probes from {}", device.id());
193 ports.forEach(this::sendProbes);
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700194 }
195
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800196 if (!isStopped()) {
Thomas Vachuska05453c92015-09-09 14:40:49 -0700197 timeout = Timer.getTimer().newTimeout(this, context.probeRate(), MILLISECONDS);
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800198 }
alshabib7911a052014-10-16 17:49:37 -0700199 }
200
alshabib7911a052014-10-16 17:49:37 -0700201 /**
202 * Creates packet_out LLDP for specified output port.
203 *
204 * @param port the port
205 * @return Packet_out message with LLDP data
206 */
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700207 private OutboundPacket createOutBoundLLDP(Long port) {
alshabib7911a052014-10-16 17:49:37 -0700208 if (port == null) {
209 return null;
210 }
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700211 lldpPacket.setPortId(port.intValue());
212 ethPacket.setSourceMACAddress(SRC_MAC);
213 return new DefaultOutboundPacket(device.id(),
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700214 builder().setOutput(portNumber(port)).build(),
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700215 ByteBuffer.wrap(ethPacket.serialize()));
alshabib7911a052014-10-16 17:49:37 -0700216 }
217
218 /**
219 * Creates packet_out BDDP for specified output port.
220 *
221 * @param port the port
222 * @return Packet_out message with LLDP data
223 */
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700224 private OutboundPacket createOutBoundBDDP(Long port) {
alshabib7911a052014-10-16 17:49:37 -0700225 if (port == null) {
226 return null;
227 }
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700228 lldpPacket.setPortId(port.intValue());
229 bddpEth.setSourceMACAddress(SRC_MAC);
230 return new DefaultOutboundPacket(device.id(),
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700231 builder().setOutput(portNumber(port)).build(),
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700232 ByteBuffer.wrap(bddpEth.serialize()));
alshabib7911a052014-10-16 17:49:37 -0700233 }
234
235 private void sendProbes(Long portNumber) {
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800236 log.trace("Sending probes out to {}@{}", portNumber, device.id());
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700237 OutboundPacket pkt = createOutBoundLLDP(portNumber);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700238 context.packetService().emit(pkt);
239 if (context.useBDDP()) {
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700240 OutboundPacket bpkt = createOutBoundBDDP(portNumber);
Thomas Vachuska05453c92015-09-09 14:40:49 -0700241 context.packetService().emit(bpkt);
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700242 }
alshabib7911a052014-10-16 17:49:37 -0700243 }
244
Thomas Vachuska05453c92015-09-09 14:40:49 -0700245 boolean containsPort(long portNumber) {
246 return ports.contains(portNumber);
247 }
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700248
alshabib7911a052014-10-16 17:49:37 -0700249}