Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Ray Milkey | 957390e | 2016-02-09 10:02:46 -0800 | [diff] [blame] | 16 | package org.onosproject.provider.lldpcommon; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 17 | |
Ray Milkey | 1ab441b | 2018-03-06 15:33:44 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableMap; |
jaegonkim | d9119d0d | 2018-02-06 23:16:52 +0900 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
Yuta HIGUCHI | 19afc03 | 2017-05-20 23:44:17 -0700 | [diff] [blame] | 20 | import io.netty.util.Timeout; |
| 21 | import io.netty.util.TimerTask; |
jaegonkim | aaa4f83 | 2018-03-11 00:48:36 +0900 | [diff] [blame] | 22 | import io.netty.util.internal.StringUtil; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 23 | import org.onlab.packet.Ethernet; |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 24 | import org.onlab.packet.MacAddress; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 25 | import org.onlab.packet.ONOSLLDP; |
| 26 | import org.onlab.util.Timer; |
DongRyeol Cha | ce65cc0 | 2018-07-23 15:02:28 +0900 | [diff] [blame] | 27 | import org.onlab.util.Tools; |
| 28 | import org.onosproject.net.AnnotationKeys; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.net.ConnectPoint; |
DongRyeol Cha | ce65cc0 | 2018-07-23 15:02:28 +0900 | [diff] [blame] | 30 | import org.onosproject.net.DefaultAnnotations; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.net.Device; |
| 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.onosproject.net.Link.Type; |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 34 | import org.onosproject.net.LinkKey; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.net.Port; |
| 36 | import org.onosproject.net.PortNumber; |
DongRyeol Cha | ce65cc0 | 2018-07-23 15:02:28 +0900 | [diff] [blame] | 37 | import org.onosproject.net.device.DeviceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.link.DefaultLinkDescription; |
| 39 | import org.onosproject.net.link.LinkDescription; |
Ray Milkey | 44ba937 | 2018-05-07 09:37:37 -0700 | [diff] [blame] | 40 | import org.onosproject.net.link.ProbedLinkProvider; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 42 | import org.onosproject.net.packet.OutboundPacket; |
| 43 | import org.onosproject.net.packet.PacketContext; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 44 | import org.slf4j.Logger; |
| 45 | |
Thomas Vachuska | 96f3ea7 | 2015-09-08 13:50:12 -0700 | [diff] [blame] | 46 | import java.nio.ByteBuffer; |
Ray Milkey | 44ba937 | 2018-05-07 09:37:37 -0700 | [diff] [blame] | 47 | import java.util.Map; |
Ray Milkey | e3c454e | 2018-11-14 10:47:50 -0800 | [diff] [blame] | 48 | import java.util.Objects; |
DongRyeol Cha | ce65cc0 | 2018-07-23 15:02:28 +0900 | [diff] [blame] | 49 | import java.util.Optional; |
| 50 | import java.util.function.Supplier; |
| 51 | import java.util.stream.Stream; |
| 52 | import java.util.stream.StreamSupport; |
Thomas Vachuska | 96f3ea7 | 2015-09-08 13:50:12 -0700 | [diff] [blame] | 53 | |
Thomas Vachuska | 586bc4f | 2016-08-19 12:44:26 -0700 | [diff] [blame] | 54 | import static com.google.common.base.Strings.isNullOrEmpty; |
Thomas Vachuska | 96f3ea7 | 2015-09-08 13:50:12 -0700 | [diff] [blame] | 55 | import static java.util.concurrent.TimeUnit.MILLISECONDS; |
jaegonkim | d9119d0d | 2018-02-06 23:16:52 +0900 | [diff] [blame] | 56 | import static org.onosproject.net.AnnotationKeys.PORT_NAME; |
Thomas Vachuska | 96f3ea7 | 2015-09-08 13:50:12 -0700 | [diff] [blame] | 57 | import static org.onosproject.net.PortNumber.portNumber; |
| 58 | import static org.onosproject.net.flow.DefaultTrafficTreatment.builder; |
| 59 | import static org.slf4j.LoggerFactory.getLogger; |
| 60 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 61 | /** |
| 62 | * Run discovery process from a physical switch. Ports are initially labeled as |
| 63 | * slow ports. When an LLDP is successfully received, label the remote port as |
| 64 | * fast. Every probeRate milliseconds, loop over all fast ports and send an |
| 65 | * LLDP, send an LLDP for a single slow port. Based on FlowVisor topology |
| 66 | * discovery implementation. |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 67 | */ |
Ray Milkey | 957390e | 2016-02-09 10:02:46 -0800 | [diff] [blame] | 68 | public class LinkDiscovery implements TimerTask { |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 69 | |
DongRyeol Cha | ce65cc0 | 2018-07-23 15:02:28 +0900 | [diff] [blame] | 70 | private static final String SCHEME_NAME = "linkdiscovery"; |
| 71 | private static final String ETHERNET = "ETHERNET"; |
| 72 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 73 | private final Logger log = getLogger(getClass()); |
Thomas Vachuska | 96f3ea7 | 2015-09-08 13:50:12 -0700 | [diff] [blame] | 74 | |
DongRyeol Cha | c80570b | 2018-11-07 11:55:32 +0900 | [diff] [blame] | 75 | private final DeviceId deviceId; |
Ray Milkey | 957390e | 2016-02-09 10:02:46 -0800 | [diff] [blame] | 76 | private final LinkDiscoveryContext context; |
Thomas Vachuska | 96f3ea7 | 2015-09-08 13:50:12 -0700 | [diff] [blame] | 77 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 78 | private final Ethernet ethPacket; |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 79 | private final Ethernet bddpEth; |
Thomas Vachuska | 96f3ea7 | 2015-09-08 13:50:12 -0700 | [diff] [blame] | 80 | |
| 81 | private Timeout timeout; |
| 82 | private volatile boolean isStopped; |
Thomas Vachuska | 9280e19 | 2018-03-06 11:19:29 -0800 | [diff] [blame] | 83 | |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 84 | // Set of ports to be probed |
Ray Milkey | 44ba937 | 2018-05-07 09:37:37 -0700 | [diff] [blame] | 85 | private final Map<Long, String> portMap = Maps.newConcurrentMap(); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 86 | /** |
| 87 | * Instantiates discovery manager for the given physical switch. Creates a |
| 88 | * generic LLDP packet that will be customized for the port it is sent out on. |
| 89 | * Starts the the timer for the discovery process. |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 90 | * |
DongRyeol Cha | c80570b | 2018-11-07 11:55:32 +0900 | [diff] [blame] | 91 | * @param deviceId the physical switch |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 92 | * @param context discovery context |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 93 | */ |
DongRyeol Cha | c80570b | 2018-11-07 11:55:32 +0900 | [diff] [blame] | 94 | public LinkDiscovery(DeviceId deviceId, LinkDiscoveryContext context) { |
| 95 | this.deviceId = deviceId; |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 96 | this.context = context; |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 97 | |
Thomas Vachuska | 96f3ea7 | 2015-09-08 13:50:12 -0700 | [diff] [blame] | 98 | ethPacket = new Ethernet(); |
| 99 | ethPacket.setEtherType(Ethernet.TYPE_LLDP); |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 100 | ethPacket.setDestinationMACAddress(MacAddress.ONOS_LLDP); |
Thomas Vachuska | 96f3ea7 | 2015-09-08 13:50:12 -0700 | [diff] [blame] | 101 | ethPacket.setPad(true); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 102 | |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 103 | bddpEth = new Ethernet(); |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 104 | bddpEth.setEtherType(Ethernet.TYPE_BSN); |
Charles Chan | 928ff8b | 2017-05-04 12:22:54 -0700 | [diff] [blame] | 105 | bddpEth.setDestinationMACAddress(MacAddress.BROADCAST); |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 106 | bddpEth.setPad(true); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 107 | |
Thomas Vachuska | 96f3ea7 | 2015-09-08 13:50:12 -0700 | [diff] [blame] | 108 | isStopped = true; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 109 | start(); |
DongRyeol Cha | c80570b | 2018-11-07 11:55:32 +0900 | [diff] [blame] | 110 | log.debug("Started discovery manager for switch {}", deviceId); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 111 | |
| 112 | } |
| 113 | |
Ray Milkey | 957390e | 2016-02-09 10:02:46 -0800 | [diff] [blame] | 114 | public synchronized void stop() { |
Jon Hall | 3edc08a | 2015-09-14 16:59:07 -0700 | [diff] [blame] | 115 | if (!isStopped) { |
| 116 | isStopped = true; |
| 117 | timeout.cancel(); |
| 118 | } else { |
| 119 | log.warn("LinkDiscovery stopped multiple times?"); |
| 120 | } |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Ray Milkey | 957390e | 2016-02-09 10:02:46 -0800 | [diff] [blame] | 123 | public synchronized void start() { |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 124 | if (isStopped) { |
| 125 | isStopped = false; |
Yuta HIGUCHI | 19afc03 | 2017-05-20 23:44:17 -0700 | [diff] [blame] | 126 | timeout = Timer.newTimeout(this, 0, MILLISECONDS); |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 127 | } else { |
| 128 | log.warn("LinkDiscovery started multiple times?"); |
| 129 | } |
| 130 | } |
| 131 | |
Ray Milkey | 957390e | 2016-02-09 10:02:46 -0800 | [diff] [blame] | 132 | public synchronized boolean isStopped() { |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 133 | return isStopped || timeout.isCancelled(); |
| 134 | } |
| 135 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 136 | /** |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 137 | * Add physical port to discovery process. |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 138 | * Send out initial LLDP and label it as slow port. |
| 139 | * |
| 140 | * @param port the port |
| 141 | */ |
Ray Milkey | 957390e | 2016-02-09 10:02:46 -0800 | [diff] [blame] | 142 | public void addPort(Port port) { |
jaegonkim | d9119d0d | 2018-02-06 23:16:52 +0900 | [diff] [blame] | 143 | Long portNum = port.number().toLong(); |
| 144 | String portName = port.annotations().value(PORT_NAME); |
jaegonkim | aaa4f83 | 2018-03-11 00:48:36 +0900 | [diff] [blame] | 145 | if (portName == null) { |
| 146 | portName = StringUtil.EMPTY_STRING; |
| 147 | } |
jaegonkim | d9119d0d | 2018-02-06 23:16:52 +0900 | [diff] [blame] | 148 | |
You Wang | 168aa62 | 2018-03-08 14:01:37 -0800 | [diff] [blame] | 149 | boolean newPort = !containsPort(portNum); |
jaegonkim | d9119d0d | 2018-02-06 23:16:52 +0900 | [diff] [blame] | 150 | portMap.put(portNum, portName); |
| 151 | |
DongRyeol Cha | c80570b | 2018-11-07 11:55:32 +0900 | [diff] [blame] | 152 | boolean isMaster = context.mastershipService().isLocalMaster(deviceId); |
Jonathan Hart | 45066bc | 2015-07-28 11:18:34 -0700 | [diff] [blame] | 153 | if (newPort && isMaster) { |
DongRyeol Cha | c80570b | 2018-11-07 11:55:32 +0900 | [diff] [blame] | 154 | log.debug("Sending initial probe to port {}@{}", port.number().toLong(), deviceId); |
jaegonkim | d9119d0d | 2018-02-06 23:16:52 +0900 | [diff] [blame] | 155 | sendProbes(portNum, portName); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 156 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /** |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 160 | * removed physical port from discovery process. |
| 161 | * @param port the port number |
| 162 | */ |
Ray Milkey | 957390e | 2016-02-09 10:02:46 -0800 | [diff] [blame] | 163 | public void removePort(PortNumber port) { |
jaegonkim | d9119d0d | 2018-02-06 23:16:52 +0900 | [diff] [blame] | 164 | portMap.remove(port.toLong()); |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 168 | * Handles an incoming LLDP packet. Creates link in topology and adds the |
| 169 | * link for staleness tracking. |
Thomas Vachuska | 96f3ea7 | 2015-09-08 13:50:12 -0700 | [diff] [blame] | 170 | * |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 171 | * @param packetContext packet context |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 172 | * @return true if handled |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 173 | */ |
Ray Milkey | 957390e | 2016-02-09 10:02:46 -0800 | [diff] [blame] | 174 | public boolean handleLldp(PacketContext packetContext) { |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 175 | Ethernet eth = packetContext.inPacket().parsed(); |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 176 | if (eth == null) { |
| 177 | return false; |
| 178 | } |
| 179 | |
DongRyeol Cha | ce65cc0 | 2018-07-23 15:02:28 +0900 | [diff] [blame] | 180 | if (processOnosLldp(packetContext, eth)) { |
| 181 | return true; |
| 182 | } |
| 183 | |
| 184 | if (processLldp(packetContext, eth)) { |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | ONOSLLDP lldp = ONOSLLDP.parseLLDP(eth); |
| 189 | |
| 190 | if (lldp == null) { |
| 191 | log.debug("Cannot parse the packet. It seems that it is not the lldp or bsn packet."); |
| 192 | } else { |
| 193 | log.debug("LLDP packet is dropped due to there are no handlers that properly handle this packet: {}", |
| 194 | lldp.toString()); |
| 195 | } |
| 196 | |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | private boolean processOnosLldp(PacketContext packetContext, Ethernet eth) { |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 201 | ONOSLLDP onoslldp = ONOSLLDP.parseONOSLLDP(eth); |
| 202 | if (onoslldp != null) { |
Ayaka Koshibe | 4822922 | 2016-05-16 18:04:26 -0700 | [diff] [blame] | 203 | Type lt; |
| 204 | if (notMy(eth.getSourceMAC().toString())) { |
| 205 | lt = Type.EDGE; |
| 206 | } else { |
| 207 | lt = eth.getEtherType() == Ethernet.TYPE_LLDP ? |
| 208 | Type.DIRECT : Type.INDIRECT; |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 209 | |
| 210 | /* Verify MAC in LLDP packets */ |
| 211 | if (!ONOSLLDP.verify(onoslldp, context.lldpSecret(), context.maxDiscoveryDelay())) { |
| 212 | log.warn("LLDP Packet failed to validate!"); |
| 213 | return true; |
| 214 | } |
Ayaka Koshibe | 3ddb7b2 | 2015-12-10 17:32:59 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Thomas Vachuska | 96f3ea7 | 2015-09-08 13:50:12 -0700 | [diff] [blame] | 217 | PortNumber srcPort = portNumber(onoslldp.getPort()); |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 218 | PortNumber dstPort = packetContext.inPacket().receivedFrom().port(); |
Thomas Vachuska | 96f3ea7 | 2015-09-08 13:50:12 -0700 | [diff] [blame] | 219 | |
Thomas Vachuska | 586bc4f | 2016-08-19 12:44:26 -0700 | [diff] [blame] | 220 | String idString = onoslldp.getDeviceString(); |
| 221 | if (!isNullOrEmpty(idString)) { |
Thomas Vachuska | 586bc4f | 2016-08-19 12:44:26 -0700 | [diff] [blame] | 222 | try { |
Ray Milkey | d7ed697 | 2018-11-15 18:24:22 -0800 | [diff] [blame] | 223 | DeviceId srcDeviceId = DeviceId.deviceId(idString); |
| 224 | DeviceId dstDeviceId = packetContext.inPacket().receivedFrom().deviceId(); |
| 225 | |
| 226 | ConnectPoint src = new ConnectPoint(srcDeviceId, srcPort); |
| 227 | ConnectPoint dst = new ConnectPoint(dstDeviceId, dstPort); |
| 228 | |
| 229 | LinkDescription ld = new DefaultLinkDescription(src, dst, lt); |
Thomas Vachuska | 586bc4f | 2016-08-19 12:44:26 -0700 | [diff] [blame] | 230 | context.providerService().linkDetected(ld); |
| 231 | context.touchLink(LinkKey.linkKey(src, dst)); |
Ray Milkey | d7ed697 | 2018-11-15 18:24:22 -0800 | [diff] [blame] | 232 | } catch (IllegalStateException | IllegalArgumentException e) { |
| 233 | log.warn("There is a exception during link creation: {}", e.getMessage()); |
Thomas Vachuska | 586bc4f | 2016-08-19 12:44:26 -0700 | [diff] [blame] | 234 | return true; |
| 235 | } |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 236 | return true; |
| 237 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 238 | } |
| 239 | return false; |
| 240 | } |
| 241 | |
DongRyeol Cha | ce65cc0 | 2018-07-23 15:02:28 +0900 | [diff] [blame] | 242 | private boolean processLldp(PacketContext packetContext, Ethernet eth) { |
| 243 | ONOSLLDP onoslldp = ONOSLLDP.parseLLDP(eth); |
| 244 | if (onoslldp != null) { |
| 245 | Type lt = eth.getEtherType() == Ethernet.TYPE_LLDP ? |
| 246 | Type.DIRECT : Type.INDIRECT; |
| 247 | |
| 248 | DeviceService deviceService = context.deviceService(); |
| 249 | MacAddress srcChassisId = onoslldp.getChassisIdByMac(); |
| 250 | String srcPortName = onoslldp.getPortNameString(); |
| 251 | String srcPortDesc = onoslldp.getPortDescString(); |
| 252 | |
| 253 | log.debug("srcChassisId:{}, srcPortName:{}, srcPortDesc:{}", srcChassisId, srcPortName, srcPortDesc); |
| 254 | |
| 255 | if (srcChassisId == null && srcPortDesc == null) { |
| 256 | log.warn("there are no valid port id"); |
| 257 | return false; |
| 258 | } |
| 259 | |
| 260 | Optional<Device> srcDevice = findSourceDeviceByChassisId(deviceService, srcChassisId); |
| 261 | |
| 262 | if (!srcDevice.isPresent()) { |
| 263 | log.warn("source device not found. srcChassisId value: {}", srcChassisId); |
| 264 | return false; |
| 265 | } |
| 266 | Optional<Port> sourcePort = findSourcePortByName( |
| 267 | srcPortName == null ? srcPortDesc : srcPortName, |
| 268 | deviceService, |
| 269 | srcDevice.get()); |
| 270 | |
| 271 | if (!sourcePort.isPresent()) { |
| 272 | log.warn("source port not found. sourcePort value: {}", sourcePort); |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | PortNumber srcPort = sourcePort.get().number(); |
| 277 | PortNumber dstPort = packetContext.inPacket().receivedFrom().port(); |
| 278 | |
| 279 | DeviceId srcDeviceId = srcDevice.get().id(); |
| 280 | DeviceId dstDeviceId = packetContext.inPacket().receivedFrom().deviceId(); |
| 281 | |
DongRyeol Cha | 81935a6 | 2018-11-30 16:51:21 +0900 | [diff] [blame] | 282 | if (!sourcePort.get().isEnabled()) { |
| 283 | log.debug("Ports are disabled. Cannot create a link between {}/{} and {}/{}", |
| 284 | srcDeviceId, sourcePort.get(), dstDeviceId, dstPort); |
| 285 | return false; |
| 286 | } |
| 287 | |
DongRyeol Cha | ce65cc0 | 2018-07-23 15:02:28 +0900 | [diff] [blame] | 288 | ConnectPoint src = new ConnectPoint(srcDeviceId, srcPort); |
| 289 | ConnectPoint dst = new ConnectPoint(dstDeviceId, dstPort); |
| 290 | |
| 291 | DefaultAnnotations annotations = DefaultAnnotations.builder() |
| 292 | .set(AnnotationKeys.PROTOCOL, SCHEME_NAME.toUpperCase()) |
| 293 | .set(AnnotationKeys.LAYER, ETHERNET) |
| 294 | .build(); |
| 295 | |
| 296 | LinkDescription ld = new DefaultLinkDescription(src, dst, lt, true, annotations); |
| 297 | try { |
| 298 | context.providerService().linkDetected(ld); |
| 299 | context.setTtl(LinkKey.linkKey(src, dst), onoslldp.getTtlBySeconds()); |
| 300 | } catch (IllegalStateException e) { |
| 301 | log.debug("There is a exception during link creation: {}", e); |
| 302 | return true; |
| 303 | } |
| 304 | return true; |
| 305 | } |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | private Optional<Device> findSourceDeviceByChassisId(DeviceService deviceService, MacAddress srcChassisId) { |
| 310 | Supplier<Stream<Device>> deviceStream = () -> |
| 311 | StreamSupport.stream(deviceService.getAvailableDevices().spliterator(), false); |
| 312 | Optional<Device> remoteDeviceOptional = deviceStream.get() |
| 313 | .filter(device -> device.chassisId() != null |
| 314 | && MacAddress.valueOf(device.chassisId().value()).equals(srcChassisId)) |
| 315 | .findAny(); |
| 316 | |
| 317 | if (remoteDeviceOptional.isPresent()) { |
| 318 | log.debug("sourceDevice found by chassis id: {}", srcChassisId); |
| 319 | return remoteDeviceOptional; |
| 320 | } else { |
| 321 | remoteDeviceOptional = deviceStream.get().filter(device -> |
| 322 | Tools.stream(deviceService.getPorts(device.id())) |
| 323 | .anyMatch(port -> port.annotations().keys().contains(AnnotationKeys.PORT_MAC) |
| 324 | && MacAddress.valueOf(port.annotations().value(AnnotationKeys.PORT_MAC)) |
| 325 | .equals(srcChassisId))) |
| 326 | .findAny(); |
| 327 | if (remoteDeviceOptional.isPresent()) { |
| 328 | log.debug("sourceDevice found by port mac: {}", srcChassisId); |
| 329 | return remoteDeviceOptional; |
| 330 | } else { |
| 331 | return Optional.empty(); |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | private Optional<Port> findSourcePortByName(String remotePortName, |
| 337 | DeviceService deviceService, |
| 338 | Device remoteDevice) { |
Ray Milkey | 208bcb3 | 2018-11-15 10:22:44 -0800 | [diff] [blame] | 339 | if (remotePortName == null) { |
| 340 | return Optional.empty(); |
| 341 | } |
DongRyeol Cha | ce65cc0 | 2018-07-23 15:02:28 +0900 | [diff] [blame] | 342 | Optional<Port> remotePort = deviceService.getPorts(remoteDevice.id()) |
Ray Milkey | e3c454e | 2018-11-14 10:47:50 -0800 | [diff] [blame] | 343 | .stream().filter(port -> Objects.equals(remotePortName, |
| 344 | port.annotations().value(AnnotationKeys.PORT_NAME))) |
DongRyeol Cha | ce65cc0 | 2018-07-23 15:02:28 +0900 | [diff] [blame] | 345 | .findAny(); |
| 346 | |
| 347 | if (remotePort.isPresent()) { |
| 348 | return remotePort; |
| 349 | } else { |
DongRyeol Cha | 597315d | 2018-11-27 10:25:20 +0900 | [diff] [blame] | 350 | return Optional.empty(); |
DongRyeol Cha | ce65cc0 | 2018-07-23 15:02:28 +0900 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
Ayaka Koshibe | 3ddb7b2 | 2015-12-10 17:32:59 -0800 | [diff] [blame] | 354 | // true if *NOT* this cluster's own probe. |
Ayaka Koshibe | 4822922 | 2016-05-16 18:04:26 -0700 | [diff] [blame] | 355 | private boolean notMy(String mac) { |
| 356 | // if we are using DEFAULT_MAC, clustering hadn't initialized, so conservative 'yes' |
| 357 | String ourMac = context.fingerprint(); |
| 358 | if (ProbedLinkProvider.defaultMac().equalsIgnoreCase(ourMac)) { |
Ayaka Koshibe | 3ddb7b2 | 2015-12-10 17:32:59 -0800 | [diff] [blame] | 359 | return true; |
Ayaka Koshibe | 3ddb7b2 | 2015-12-10 17:32:59 -0800 | [diff] [blame] | 360 | } |
Ayaka Koshibe | 4822922 | 2016-05-16 18:04:26 -0700 | [diff] [blame] | 361 | return !mac.equalsIgnoreCase(ourMac); |
Ayaka Koshibe | 3ddb7b2 | 2015-12-10 17:32:59 -0800 | [diff] [blame] | 362 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 363 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 364 | /** |
| 365 | * Execute this method every t milliseconds. Loops over all ports |
| 366 | * labeled as fast and sends out an LLDP. Send out an LLDP on a single slow |
| 367 | * port. |
| 368 | * |
| 369 | * @param t timeout |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 370 | */ |
| 371 | @Override |
Thomas Vachuska | 96f3ea7 | 2015-09-08 13:50:12 -0700 | [diff] [blame] | 372 | public void run(Timeout t) { |
Yuta HIGUCHI | 3419858 | 2014-11-10 16:24:58 -0800 | [diff] [blame] | 373 | if (isStopped()) { |
| 374 | return; |
| 375 | } |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 376 | |
DongRyeol Cha | c80570b | 2018-11-07 11:55:32 +0900 | [diff] [blame] | 377 | if (context.mastershipService().isLocalMaster(deviceId)) { |
| 378 | log.trace("Sending probes from {}", deviceId); |
Ray Milkey | 1ab441b | 2018-03-06 15:33:44 -0800 | [diff] [blame] | 379 | ImmutableMap.copyOf(portMap).forEach(this::sendProbes); |
Yuta HIGUCHI | f672588 | 2014-10-29 15:25:51 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Yuta HIGUCHI | 3225578 | 2014-11-04 22:32:14 -0800 | [diff] [blame] | 382 | if (!isStopped()) { |
Yuta HIGUCHI | 19afc03 | 2017-05-20 23:44:17 -0700 | [diff] [blame] | 383 | timeout = t.timer().newTimeout(this, context.probeRate(), MILLISECONDS); |
Yuta HIGUCHI | 3225578 | 2014-11-04 22:32:14 -0800 | [diff] [blame] | 384 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 385 | } |
| 386 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 387 | /** |
| 388 | * Creates packet_out LLDP for specified output port. |
| 389 | * |
jaegonkim | d9119d0d | 2018-02-06 23:16:52 +0900 | [diff] [blame] | 390 | * @param portNumber the port |
| 391 | * @param portDesc the port description |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 392 | * @return Packet_out message with LLDP data |
| 393 | */ |
jaegonkim | d9119d0d | 2018-02-06 23:16:52 +0900 | [diff] [blame] | 394 | private OutboundPacket createOutBoundLldp(Long portNumber, String portDesc) { |
| 395 | if (portNumber == null) { |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 396 | return null; |
| 397 | } |
DongRyeol Cha | c80570b | 2018-11-07 11:55:32 +0900 | [diff] [blame] | 398 | ONOSLLDP lldp = getLinkProbe(portNumber, portDesc); |
| 399 | if (lldp == null) { |
| 400 | log.warn("Cannot get link probe with portNumber {} and portDesc {} for {} at LLDP packet creation.", |
| 401 | portNumber, portDesc, deviceId); |
| 402 | return null; |
| 403 | } |
Ayaka Koshibe | 4822922 | 2016-05-16 18:04:26 -0700 | [diff] [blame] | 404 | ethPacket.setSourceMACAddress(context.fingerprint()).setPayload(lldp); |
DongRyeol Cha | c80570b | 2018-11-07 11:55:32 +0900 | [diff] [blame] | 405 | return new DefaultOutboundPacket(deviceId, |
jaegonkim | d9119d0d | 2018-02-06 23:16:52 +0900 | [diff] [blame] | 406 | builder().setOutput(portNumber(portNumber)).build(), |
Thomas Vachuska | 96f3ea7 | 2015-09-08 13:50:12 -0700 | [diff] [blame] | 407 | ByteBuffer.wrap(ethPacket.serialize())); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Creates packet_out BDDP for specified output port. |
| 412 | * |
jaegonkim | d9119d0d | 2018-02-06 23:16:52 +0900 | [diff] [blame] | 413 | * @param portNumber the port |
| 414 | * @param portDesc the port description |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 415 | * @return Packet_out message with LLDP data |
| 416 | */ |
jaegonkim | d9119d0d | 2018-02-06 23:16:52 +0900 | [diff] [blame] | 417 | private OutboundPacket createOutBoundBddp(Long portNumber, String portDesc) { |
| 418 | if (portNumber == null) { |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 419 | return null; |
| 420 | } |
DongRyeol Cha | c80570b | 2018-11-07 11:55:32 +0900 | [diff] [blame] | 421 | ONOSLLDP lldp = getLinkProbe(portNumber, portDesc); |
| 422 | if (lldp == null) { |
| 423 | log.warn("Cannot get link probe with portNumber {} and portDesc {} for {} at BDDP packet creation.", |
| 424 | portNumber, portDesc, deviceId); |
| 425 | return null; |
| 426 | } |
Ayaka Koshibe | 4822922 | 2016-05-16 18:04:26 -0700 | [diff] [blame] | 427 | bddpEth.setSourceMACAddress(context.fingerprint()).setPayload(lldp); |
DongRyeol Cha | c80570b | 2018-11-07 11:55:32 +0900 | [diff] [blame] | 428 | return new DefaultOutboundPacket(deviceId, |
jaegonkim | d9119d0d | 2018-02-06 23:16:52 +0900 | [diff] [blame] | 429 | builder().setOutput(portNumber(portNumber)).build(), |
Thomas Vachuska | 96f3ea7 | 2015-09-08 13:50:12 -0700 | [diff] [blame] | 430 | ByteBuffer.wrap(bddpEth.serialize())); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 431 | } |
| 432 | |
DongRyeol Cha | c80570b | 2018-11-07 11:55:32 +0900 | [diff] [blame] | 433 | private ONOSLLDP getLinkProbe(Long portNumber, String portDesc) { |
| 434 | Device device = context.deviceService().getDevice(deviceId); |
| 435 | if (device == null) { |
| 436 | log.warn("Cannot find the device {}", deviceId); |
| 437 | return null; |
| 438 | } |
| 439 | return ONOSLLDP.onosSecureLLDP(deviceId.toString(), device.chassisId(), portNumber.intValue(), portDesc, |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 440 | context.lldpSecret()); |
Ayaka Koshibe | 12c8c08 | 2015-12-08 12:48:46 -0800 | [diff] [blame] | 441 | } |
| 442 | |
jaegonkim | d9119d0d | 2018-02-06 23:16:52 +0900 | [diff] [blame] | 443 | private void sendProbes(Long portNumber, String portDesc) { |
Ray Milkey | d9bbde8 | 2016-06-09 11:35:00 -0700 | [diff] [blame] | 444 | if (context.packetService() == null) { |
| 445 | return; |
| 446 | } |
DongRyeol Cha | c80570b | 2018-11-07 11:55:32 +0900 | [diff] [blame] | 447 | log.trace("Sending probes out of {}@{}", portNumber, deviceId); |
jaegonkim | d9119d0d | 2018-02-06 23:16:52 +0900 | [diff] [blame] | 448 | OutboundPacket pkt = createOutBoundLldp(portNumber, portDesc); |
DongRyeol Cha | c80570b | 2018-11-07 11:55:32 +0900 | [diff] [blame] | 449 | if (pkt != null) { |
| 450 | context.packetService().emit(pkt); |
| 451 | } else { |
| 452 | log.warn("Cannot send lldp packet due to packet is null {}", deviceId); |
| 453 | } |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 454 | if (context.useBddp()) { |
jaegonkim | d9119d0d | 2018-02-06 23:16:52 +0900 | [diff] [blame] | 455 | OutboundPacket bpkt = createOutBoundBddp(portNumber, portDesc); |
DongRyeol Cha | c80570b | 2018-11-07 11:55:32 +0900 | [diff] [blame] | 456 | if (bpkt != null) { |
| 457 | context.packetService().emit(bpkt); |
| 458 | } else { |
| 459 | log.warn("Cannot send bddp packet due to packet is null {}", deviceId); |
| 460 | } |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 461 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Ray Milkey | 957390e | 2016-02-09 10:02:46 -0800 | [diff] [blame] | 464 | public boolean containsPort(long portNumber) { |
jaegonkim | d9119d0d | 2018-02-06 23:16:52 +0900 | [diff] [blame] | 465 | return portMap.containsKey(portNumber); |
Thomas Vachuska | 05453c9 | 2015-09-09 14:40:49 -0700 | [diff] [blame] | 466 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 467 | } |