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