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