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 2014-present Open Networking Foundation |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.provider.host.impl; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 17 | |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Sets; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Modified; |
| 23 | import org.apache.felix.scr.annotations.Property; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Service; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 27 | import org.onlab.packet.ARP; |
Yi Tseng | fcf5dce | 2017-07-26 14:30:41 -0700 | [diff] [blame] | 28 | import org.onlab.packet.BasePacket; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 29 | import org.onlab.packet.DHCP; |
Yi Tseng | fcf5dce | 2017-07-26 14:30:41 -0700 | [diff] [blame] | 30 | import org.onlab.packet.DHCP6; |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 31 | import org.onlab.packet.EthType; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 32 | import org.onlab.packet.Ethernet; |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 33 | import org.onlab.packet.ICMP6; |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 34 | import org.onlab.packet.IPacket; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 35 | import org.onlab.packet.IPv4; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 36 | import org.onlab.packet.IPv6; |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 37 | import org.onlab.packet.Ip4Address; |
Yi Tseng | fcf5dce | 2017-07-26 14:30:41 -0700 | [diff] [blame] | 38 | import org.onlab.packet.Ip6Address; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 39 | import org.onlab.packet.IpAddress; |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 40 | import org.onlab.packet.MacAddress; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 41 | import org.onlab.packet.UDP; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 42 | import org.onlab.packet.VlanId; |
Yi Tseng | fcf5dce | 2017-07-26 14:30:41 -0700 | [diff] [blame] | 43 | import org.onlab.packet.dhcp.Dhcp6ClientIdOption; |
| 44 | import org.onlab.packet.dhcp.Dhcp6IaAddressOption; |
| 45 | import org.onlab.packet.dhcp.Dhcp6IaNaOption; |
| 46 | import org.onlab.packet.dhcp.Dhcp6IaTaOption; |
| 47 | import org.onlab.packet.dhcp.Dhcp6RelayOption; |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 48 | import org.onlab.packet.ipv6.IExtensionHeader; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 49 | import org.onlab.packet.ndp.NeighborAdvertisement; |
| 50 | import org.onlab.packet.ndp.NeighborSolicitation; |
Charles M.C. Chan | 441d7da | 2015-03-17 21:03:39 +0800 | [diff] [blame] | 51 | import org.onlab.packet.ndp.RouterAdvertisement; |
| 52 | import org.onlab.packet.ndp.RouterSolicitation; |
Jian Li | d9b5f55 | 2016-03-11 18:15:31 -0800 | [diff] [blame] | 53 | import org.onlab.util.Tools; |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 54 | import org.onosproject.cfg.ComponentConfigService; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 55 | import org.onosproject.core.ApplicationId; |
| 56 | import org.onosproject.core.CoreService; |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 57 | import org.onosproject.net.host.HostLocationProbingService; |
Ray Milkey | facf286 | 2017-08-03 11:58:29 -0700 | [diff] [blame] | 58 | import org.onosproject.net.intf.InterfaceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 59 | import org.onosproject.net.ConnectPoint; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 60 | import org.onosproject.net.Device; |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 61 | import org.onosproject.net.DeviceId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 62 | import org.onosproject.net.Host; |
| 63 | import org.onosproject.net.HostId; |
| 64 | import org.onosproject.net.HostLocation; |
| 65 | import org.onosproject.net.device.DeviceEvent; |
| 66 | import org.onosproject.net.device.DeviceListener; |
| 67 | import org.onosproject.net.device.DeviceService; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 68 | import org.onosproject.net.flow.DefaultTrafficSelector; |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 69 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 70 | import org.onosproject.net.flow.TrafficSelector; |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 71 | import org.onosproject.net.flow.TrafficTreatment; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 72 | import org.onosproject.net.host.DefaultHostDescription; |
| 73 | import org.onosproject.net.host.HostDescription; |
| 74 | import org.onosproject.net.host.HostProvider; |
| 75 | import org.onosproject.net.host.HostProviderRegistry; |
| 76 | import org.onosproject.net.host.HostProviderService; |
| 77 | import org.onosproject.net.host.HostService; |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 78 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 79 | import org.onosproject.net.packet.OutboundPacket; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 80 | import org.onosproject.net.packet.PacketContext; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 81 | import org.onosproject.net.packet.PacketPriority; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 82 | import org.onosproject.net.packet.PacketProcessor; |
| 83 | import org.onosproject.net.packet.PacketService; |
| 84 | import org.onosproject.net.provider.AbstractProvider; |
| 85 | import org.onosproject.net.provider.ProviderId; |
| 86 | import org.onosproject.net.topology.Topology; |
| 87 | import org.onosproject.net.topology.TopologyService; |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 88 | import org.osgi.service.component.ComponentContext; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 89 | import org.slf4j.Logger; |
| 90 | |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 91 | import java.nio.ByteBuffer; |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 92 | import java.util.Dictionary; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 93 | import java.util.Objects; |
| 94 | import java.util.Optional; |
Madan Jampani | a3770c3 | 2015-12-11 12:07:41 -0800 | [diff] [blame] | 95 | import java.util.concurrent.ExecutorService; |
Charles Chan | bb86b78 | 2017-09-13 16:14:22 -0700 | [diff] [blame] | 96 | import java.util.concurrent.ScheduledExecutorService; |
| 97 | import java.util.concurrent.TimeUnit; |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 98 | import java.util.stream.Stream; |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 99 | import java.util.Set; |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 100 | |
Charles Chan | 6577f61 | 2018-04-19 13:10:01 -0700 | [diff] [blame^] | 101 | import static java.util.concurrent.Executors.newScheduledThreadPool; |
Madan Jampani | a3770c3 | 2015-12-11 12:07:41 -0800 | [diff] [blame] | 102 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 103 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 104 | import static org.slf4j.LoggerFactory.getLogger; |
| 105 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 106 | /** |
Thomas Vachuska | ec9c7dd | 2015-09-03 18:30:04 -0700 | [diff] [blame] | 107 | * Provider which uses an OpenFlow controller to detect network end-station |
| 108 | * hosts. |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 109 | */ |
| 110 | @Component(immediate = true) |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 111 | @Service |
| 112 | public class HostLocationProvider extends AbstractProvider implements HostProvider, HostLocationProbingService { |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 113 | private final Logger log = getLogger(getClass()); |
| 114 | |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 115 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 116 | protected CoreService coreService; |
| 117 | |
| 118 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 119 | protected HostProviderRegistry providerRegistry; |
| 120 | |
| 121 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 122 | protected PacketService packetService; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 123 | |
| 124 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 125 | protected TopologyService topologyService; |
| 126 | |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 127 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 128 | protected HostService hostService; |
| 129 | |
| 130 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 131 | protected DeviceService deviceService; |
| 132 | |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 133 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 134 | protected ComponentConfigService cfgService; |
| 135 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 136 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 137 | protected InterfaceService interfaceService; |
| 138 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 139 | private HostProviderService providerService; |
| 140 | |
| 141 | private final InternalHostProvider processor = new InternalHostProvider(); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 142 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
| 143 | |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 144 | private ApplicationId appId; |
| 145 | |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 146 | @Property(name = "hostRemovalEnabled", boolValue = true, |
| 147 | label = "Enable host removal on port/device down events") |
| 148 | private boolean hostRemovalEnabled = true; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 149 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 150 | @Property(name = "requestArp", boolValue = true, |
| 151 | label = "Request ARP packets for neighbor discovery by the " + |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 152 | "Host Location Provider; default is true") |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 153 | private boolean requestArp = true; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 154 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 155 | @Property(name = "requestIpv6ND", boolValue = false, |
| 156 | label = "Requests IPv6 Neighbor Discovery by the " + |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 157 | "Host Location Provider; default is false") |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 158 | private boolean requestIpv6ND = false; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 159 | |
| 160 | @Property(name = "useDhcp", boolValue = false, |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 161 | label = "Use DHCP to update IP address of the host; default is false") |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 162 | private boolean useDhcp = false; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 163 | |
Yi Tseng | fcf5dce | 2017-07-26 14:30:41 -0700 | [diff] [blame] | 164 | @Property(name = "useDhcp6", boolValue = false, |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 165 | label = "Use DHCPv6 to update IP address of the host; default is false") |
Yi Tseng | fcf5dce | 2017-07-26 14:30:41 -0700 | [diff] [blame] | 166 | private boolean useDhcp6 = false; |
| 167 | |
Jonathan Hart | 9af322d | 2016-01-06 17:42:04 -0800 | [diff] [blame] | 168 | @Property(name = "requestInterceptsEnabled", boolValue = true, |
| 169 | label = "Enable requesting packet intercepts") |
| 170 | private boolean requestInterceptsEnabled = true; |
| 171 | |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 172 | @Property(name = "multihomingEnabled", boolValue = false, |
| 173 | label = "Allow hosts to be multihomed") |
| 174 | private boolean multihomingEnabled = false; |
Madan Jampani | a3770c3 | 2015-12-11 12:07:41 -0800 | [diff] [blame] | 175 | |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 176 | private int probeInitDelayMs = 1000; |
| 177 | |
Charles Chan | 6577f61 | 2018-04-19 13:10:01 -0700 | [diff] [blame^] | 178 | ExecutorService eventHandler; |
| 179 | private ScheduledExecutorService hostProber; |
Charles Chan | 8277b6b | 2017-12-03 13:48:46 -0800 | [diff] [blame] | 180 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 181 | /** |
| 182 | * Creates an OpenFlow host provider. |
| 183 | */ |
| 184 | public HostLocationProvider() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 185 | super(new ProviderId("of", "org.onosproject.provider.host")); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Activate |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 189 | public void activate(ComponentContext context) { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 190 | cfgService.registerProperties(getClass()); |
| 191 | appId = coreService.registerApplication("org.onosproject.provider.host"); |
Charles Chan | 6577f61 | 2018-04-19 13:10:01 -0700 | [diff] [blame^] | 192 | eventHandler = newSingleThreadScheduledExecutor(groupedThreads("onos/host-loc-provider", "event-handler", log)); |
| 193 | hostProber = newScheduledThreadPool(32, groupedThreads("onos/host-loc-probe", "%d", log)); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 194 | providerService = providerRegistry.register(this); |
Brian O'Connor | 3b78326 | 2015-07-29 17:49:24 -0700 | [diff] [blame] | 195 | packetService.addProcessor(processor, PacketProcessor.advisor(1)); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 196 | deviceService.addListener(deviceListener); |
Jonathan Hart | 9af322d | 2016-01-06 17:42:04 -0800 | [diff] [blame] | 197 | |
| 198 | modified(context); |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 199 | |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 200 | log.info("Started with Application ID {}", appId.id()); |
| 201 | } |
| 202 | |
| 203 | @Deactivate |
| 204 | public void deactivate() { |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 205 | cfgService.unregisterProperties(getClass(), false); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 206 | |
| 207 | withdrawIntercepts(); |
| 208 | |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 209 | providerRegistry.unregister(this); |
| 210 | packetService.removeProcessor(processor); |
| 211 | deviceService.removeListener(deviceListener); |
Madan Jampani | a3770c3 | 2015-12-11 12:07:41 -0800 | [diff] [blame] | 212 | eventHandler.shutdown(); |
Charles Chan | 6577f61 | 2018-04-19 13:10:01 -0700 | [diff] [blame^] | 213 | hostProber.shutdown(); |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 214 | providerService = null; |
| 215 | log.info("Stopped"); |
| 216 | } |
| 217 | |
| 218 | @Modified |
| 219 | public void modified(ComponentContext context) { |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 220 | readComponentConfiguration(context); |
Jonathan Hart | 9af322d | 2016-01-06 17:42:04 -0800 | [diff] [blame] | 221 | |
| 222 | if (requestInterceptsEnabled) { |
| 223 | requestIntercepts(); |
| 224 | } else { |
| 225 | withdrawIntercepts(); |
| 226 | } |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /** |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 230 | * Request packet intercepts. |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 231 | */ |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 232 | private void requestIntercepts() { |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 233 | // Use ARP |
| 234 | TrafficSelector arpSelector = DefaultTrafficSelector.builder() |
| 235 | .matchEthType(Ethernet.TYPE_ARP) |
| 236 | .build(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 237 | if (requestArp) { |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 238 | packetService.requestPackets(arpSelector, PacketPriority.CONTROL, appId); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 239 | } else { |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 240 | packetService.cancelPackets(arpSelector, PacketPriority.CONTROL, appId); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 241 | } |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 242 | |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 243 | // Use IPv6 Neighbor Discovery |
| 244 | TrafficSelector ipv6NsSelector = DefaultTrafficSelector.builder() |
| 245 | .matchEthType(Ethernet.TYPE_IPV6) |
| 246 | .matchIPProtocol(IPv6.PROTOCOL_ICMP6) |
| 247 | .matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION) |
| 248 | .build(); |
| 249 | TrafficSelector ipv6NaSelector = DefaultTrafficSelector.builder() |
| 250 | .matchEthType(Ethernet.TYPE_IPV6) |
| 251 | .matchIPProtocol(IPv6.PROTOCOL_ICMP6) |
| 252 | .matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT) |
| 253 | .build(); |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 254 | if (requestIpv6ND) { |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 255 | packetService.requestPackets(ipv6NsSelector, PacketPriority.CONTROL, appId); |
| 256 | packetService.requestPackets(ipv6NaSelector, PacketPriority.CONTROL, appId); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 257 | } else { |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 258 | packetService.cancelPackets(ipv6NsSelector, PacketPriority.CONTROL, appId); |
| 259 | packetService.cancelPackets(ipv6NaSelector, PacketPriority.CONTROL, appId); |
| 260 | } |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Withdraw packet intercepts. |
| 265 | */ |
| 266 | private void withdrawIntercepts() { |
| 267 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 268 | selector.matchEthType(Ethernet.TYPE_ARP); |
Aaron Kruglikov | 07a923d | 2015-07-03 13:30:57 -0700 | [diff] [blame] | 269 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 270 | |
| 271 | // IPv6 Neighbor Solicitation packet. |
| 272 | selector.matchEthType(Ethernet.TYPE_IPV6); |
| 273 | selector.matchIPProtocol(IPv6.PROTOCOL_ICMP6); |
| 274 | selector.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION); |
| 275 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId); |
| 276 | |
| 277 | // IPv6 Neighbor Advertisement packet. |
| 278 | selector.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT); |
| 279 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Extracts properties from the component configuration context. |
| 284 | * |
| 285 | * @param context the component context |
| 286 | */ |
| 287 | private void readComponentConfiguration(ComponentContext context) { |
| 288 | Dictionary<?, ?> properties = context.getProperties(); |
| 289 | Boolean flag; |
| 290 | |
Jian Li | d9b5f55 | 2016-03-11 18:15:31 -0800 | [diff] [blame] | 291 | flag = Tools.isPropertyEnabled(properties, "hostRemovalEnabled"); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 292 | if (flag == null) { |
| 293 | log.info("Host removal on port/device down events is not configured, " + |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 294 | "using current value of {}", hostRemovalEnabled); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 295 | } else { |
| 296 | hostRemovalEnabled = flag; |
| 297 | log.info("Configured. Host removal on port/device down events is {}", |
| 298 | hostRemovalEnabled ? "enabled" : "disabled"); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 299 | } |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 300 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 301 | flag = Tools.isPropertyEnabled(properties, "requestArp"); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 302 | if (flag == null) { |
| 303 | log.info("Using ARP is not configured, " + |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 304 | "using current value of {}", requestArp); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 305 | } else { |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 306 | requestArp = flag; |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 307 | log.info("Configured. Using ARP is {}", |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 308 | requestArp ? "enabled" : "disabled"); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 311 | flag = Tools.isPropertyEnabled(properties, "requestIpv6ND"); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 312 | if (flag == null) { |
| 313 | log.info("Using IPv6 Neighbor Discovery is not configured, " + |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 314 | "using current value of {}", requestIpv6ND); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 315 | } else { |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 316 | requestIpv6ND = flag; |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 317 | log.info("Configured. Using IPv6 Neighbor Discovery is {}", |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 318 | requestIpv6ND ? "enabled" : "disabled"); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | flag = Tools.isPropertyEnabled(properties, "useDhcp"); |
| 322 | if (flag == null) { |
| 323 | log.info("Using DHCP is not configured, " + |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 324 | "using current value of {}", useDhcp); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 325 | } else { |
| 326 | useDhcp = flag; |
| 327 | log.info("Configured. Using DHCP is {}", |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 328 | useDhcp ? "enabled" : "disabled"); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 329 | } |
Jonathan Hart | 9af322d | 2016-01-06 17:42:04 -0800 | [diff] [blame] | 330 | |
Jian Li | d9b5f55 | 2016-03-11 18:15:31 -0800 | [diff] [blame] | 331 | flag = Tools.isPropertyEnabled(properties, "requestInterceptsEnabled"); |
Jonathan Hart | 9af322d | 2016-01-06 17:42:04 -0800 | [diff] [blame] | 332 | if (flag == null) { |
| 333 | log.info("Request intercepts is not configured, " + |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 334 | "using current value of {}", requestInterceptsEnabled); |
Jonathan Hart | 9af322d | 2016-01-06 17:42:04 -0800 | [diff] [blame] | 335 | } else { |
| 336 | requestInterceptsEnabled = flag; |
| 337 | log.info("Configured. Request intercepts is {}", |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 338 | requestInterceptsEnabled ? "enabled" : "disabled"); |
Jonathan Hart | 9af322d | 2016-01-06 17:42:04 -0800 | [diff] [blame] | 339 | } |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 340 | |
| 341 | flag = Tools.isPropertyEnabled(properties, "multihomingEnabled"); |
| 342 | if (flag == null) { |
| 343 | log.info("Multihoming is not configured, " + |
| 344 | "using current value of {}", multihomingEnabled); |
| 345 | } else { |
| 346 | multihomingEnabled = flag; |
| 347 | log.info("Configured. Multihoming is {}", |
| 348 | multihomingEnabled ? "enabled" : "disabled"); |
| 349 | } |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 350 | } |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 351 | |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 352 | @Override |
| 353 | public void probeHostLocation(Host host, ConnectPoint connectPoint, ProbeMode probeMode) { |
| 354 | host.ipAddresses().stream().findFirst().ifPresent(ip -> { |
| 355 | MacAddress probeMac = providerService.addPendingHostLocation(host.id(), connectPoint, probeMode); |
| 356 | log.debug("Constructing {} probe for host {} with {}", probeMode, host.id(), ip); |
| 357 | Ethernet probe; |
| 358 | if (ip.isIp4()) { |
| 359 | probe = ARP.buildArpRequest(probeMac.toBytes(), Ip4Address.ZERO.toOctets(), |
| 360 | host.id().mac().toBytes(), ip.toOctets(), |
| 361 | host.id().mac().toBytes(), host.id().vlanId().toShort()); |
| 362 | } else { |
| 363 | probe = NeighborSolicitation.buildNdpSolicit( |
| 364 | ip.getIp6Address(), |
| 365 | Ip6Address.valueOf(IPv6.getLinkLocalAddress(probeMac.toBytes())), |
| 366 | ip.getIp6Address(), |
| 367 | probeMac, |
| 368 | host.id().mac(), |
| 369 | host.id().vlanId()); |
| 370 | } |
| 371 | |
| 372 | // NOTE: delay the probe a little bit to wait for the store synchronization is done |
Charles Chan | 6577f61 | 2018-04-19 13:10:01 -0700 | [diff] [blame^] | 373 | hostProber.schedule(() -> |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 374 | sendLocationProbe(probe, connectPoint), probeInitDelayMs, TimeUnit.MILLISECONDS); |
| 375 | }); |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Send the probe packet on given port. |
| 380 | * |
| 381 | * @param probe the probe packet |
| 382 | * @param connectPoint the port we want to probe |
| 383 | */ |
| 384 | private void sendLocationProbe(Ethernet probe, ConnectPoint connectPoint) { |
| 385 | log.info("Sending probe for host {} on location {} with probeMac {}", |
| 386 | probe.getDestinationMAC(), connectPoint, probe.getSourceMAC()); |
| 387 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(connectPoint.port()).build(); |
| 388 | OutboundPacket outboundPacket = new DefaultOutboundPacket(connectPoint.deviceId(), |
| 389 | treatment, ByteBuffer.wrap(probe.serialize())); |
| 390 | packetService.emit(outboundPacket); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 391 | } |
| 392 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 393 | @Override |
| 394 | public void triggerProbe(Host host) { |
Saurav Das | b0ae6ee | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 395 | //log.info("Triggering probe on device {} ", host); |
Jonathan Hart | f353e40 | 2016-09-23 09:27:36 -0700 | [diff] [blame] | 396 | |
| 397 | // FIXME Disabling host probing for now, because sending packets from a |
| 398 | // broadcast MAC address caused problems when two ONOS networks were |
| 399 | // interconnected. Host probing should take into account the interface |
| 400 | // configuration when determining which source address to use. |
| 401 | |
| 402 | //MastershipRole role = deviceService.getRole(host.location().deviceId()); |
| 403 | //if (role.equals(MastershipRole.MASTER)) { |
| 404 | // host.ipAddresses().forEach(ip -> { |
| 405 | // sendProbe(host, ip); |
| 406 | // }); |
| 407 | //} else { |
| 408 | // log.info("not the master, master will probe {}"); |
| 409 | //} |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | private void sendProbe(Host host, IpAddress targetIp) { |
| 413 | Ethernet probePacket = null; |
| 414 | if (targetIp.isIp4()) { |
| 415 | // IPv4: Use ARP |
| 416 | probePacket = buildArpRequest(targetIp, host); |
| 417 | } else { |
| 418 | // IPv6: Use Neighbor Discovery |
Jonathan Hart | f353e40 | 2016-09-23 09:27:36 -0700 | [diff] [blame] | 419 | //TODO need to implement ndp probe |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 420 | log.info("Triggering probe on device {} ", host); |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 421 | return; |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(host.location().port()).build(); |
| 425 | |
| 426 | OutboundPacket outboundPacket = new DefaultOutboundPacket(host.location().deviceId(), treatment, |
| 427 | ByteBuffer.wrap(probePacket.serialize())); |
| 428 | |
| 429 | packetService.emit(outboundPacket); |
| 430 | } |
| 431 | |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 432 | // This method is using source ip as 0.0.0.0 , to receive the reply even from the sub net hosts. |
sdn | 94b0015 | 2016-08-30 02:12:32 -0700 | [diff] [blame] | 433 | private Ethernet buildArpRequest(IpAddress targetIp, Host host) { |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 434 | return ARP.buildArpRequest(MacAddress.BROADCAST.toBytes(), Ip4Address.ZERO.toOctets(), |
| 435 | MacAddress.BROADCAST.toBytes(), targetIp.toOctets(), |
| 436 | MacAddress.BROADCAST.toBytes(), VlanId.NONE.toShort()); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | private class InternalHostProvider implements PacketProcessor { |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 440 | /** |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 441 | * Create or update host information. |
| 442 | * Will not update IP if IP is null, all zero or self-assigned. |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 443 | * |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 444 | * @param hid host ID |
| 445 | * @param mac source Mac address |
| 446 | * @param vlan VLAN ID |
| 447 | * @param innerVlan inner VLAN ID |
| 448 | * @param outerTpid outer TPID |
| 449 | * @param hloc host location |
| 450 | * @param ip source IP address or null if not updating |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 451 | */ |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 452 | private void createOrUpdateHost(HostId hid, MacAddress mac, VlanId vlan, |
| 453 | VlanId innerVlan, EthType outerTpid, |
| 454 | HostLocation hloc, IpAddress ip) { |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 455 | Set<HostLocation> newLocations = Sets.newHashSet(hloc); |
| 456 | |
| 457 | if (multihomingEnabled) { |
| 458 | Host existingHost = hostService.getHost(hid); |
| 459 | if (existingHost != null) { |
| 460 | Set<HostLocation> prevLocations = existingHost.locations(); |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 461 | |
Charles Chan | 653e2ac | 2017-11-22 14:49:55 -0800 | [diff] [blame] | 462 | if (prevLocations.stream().noneMatch(loc -> loc.deviceId().equals(hloc.deviceId()))) { |
| 463 | // New location is on a device that we haven't seen before |
| 464 | // Could be a dual-home host. Append new location and send out the probe |
| 465 | newLocations.addAll(prevLocations); |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 466 | prevLocations.forEach(prevLocation -> |
| 467 | probeHostLocation(existingHost, prevLocation, ProbeMode.VERIFY)); |
Charles Chan | 653e2ac | 2017-11-22 14:49:55 -0800 | [diff] [blame] | 468 | } else { |
| 469 | // Move within the same switch |
| 470 | // Simply replace old location that is on the same device |
| 471 | prevLocations.stream().filter(loc -> !loc.deviceId().equals(hloc.deviceId())) |
| 472 | .forEach(newLocations::add); |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 477 | HostDescription desc = ip == null || ip.isZero() || ip.isSelfAssigned() ? |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 478 | new DefaultHostDescription(mac, vlan, newLocations, Sets.newHashSet(), |
| 479 | innerVlan, outerTpid, false) : |
| 480 | new DefaultHostDescription(mac, vlan, newLocations, Sets.newHashSet(ip), |
| 481 | innerVlan, outerTpid, false); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 482 | try { |
Ray Milkey | dc08344 | 2016-02-22 11:27:57 -0800 | [diff] [blame] | 483 | providerService.hostDetected(hid, desc, false); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 484 | } catch (IllegalStateException e) { |
| 485 | log.debug("Host {} suppressed", hid); |
| 486 | } |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 487 | } |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 488 | |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 489 | /** |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 490 | * Updates IP address for an existing host. |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 491 | * |
| 492 | * @param hid host ID |
| 493 | * @param ip IP address |
| 494 | */ |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 495 | private void updateHostIp(HostId hid, IpAddress ip) { |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 496 | Host host = hostService.getHost(hid); |
| 497 | if (host == null) { |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 498 | log.warn("Fail to update IP for {}. Host does not exist", hid); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 499 | return; |
| 500 | } |
| 501 | |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 502 | HostDescription desc = new DefaultHostDescription(hid.mac(), hid.vlanId(), |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 503 | host.locations(), Sets.newHashSet(ip), false); |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 504 | try { |
| 505 | providerService.hostDetected(hid, desc, false); |
| 506 | } catch (IllegalStateException e) { |
| 507 | log.debug("Host {} suppressed", hid); |
| 508 | } |
| 509 | } |
| 510 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 511 | @Override |
| 512 | public void process(PacketContext context) { |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 513 | if (context == null) { |
| 514 | return; |
| 515 | } |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 516 | |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 517 | Ethernet eth = context.inPacket().parsed(); |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 518 | if (eth == null) { |
| 519 | return; |
| 520 | } |
Jonathan Hart | f353e40 | 2016-09-23 09:27:36 -0700 | [diff] [blame] | 521 | |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 522 | MacAddress srcMac = eth.getSourceMAC(); |
Jonathan Hart | f353e40 | 2016-09-23 09:27:36 -0700 | [diff] [blame] | 523 | if (srcMac.isBroadcast() || srcMac.isMulticast()) { |
| 524 | return; |
| 525 | } |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 526 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 527 | VlanId vlan = VlanId.vlanId(eth.getVlanID()); |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 528 | VlanId outerVlan = VlanId.vlanId(eth.getQinQVID()); |
| 529 | VlanId innerVlan = VlanId.NONE; |
| 530 | EthType outerTpid = EthType.EtherType.UNKNOWN.ethType(); |
| 531 | // Set up values for double-tagged hosts |
| 532 | if (outerVlan.toShort() != Ethernet.VLAN_UNTAGGED) { |
| 533 | innerVlan = vlan; |
| 534 | vlan = outerVlan; |
| 535 | outerTpid = EthType.EtherType.lookup(eth.getQinQTPID()).ethType(); |
| 536 | } |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 537 | ConnectPoint heardOn = context.inPacket().receivedFrom(); |
| 538 | |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 539 | // If this arrived on control port, bail out. |
| 540 | if (heardOn.port().isLogical()) { |
| 541 | return; |
| 542 | } |
| 543 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 544 | // If this is not an edge port, bail out. |
| 545 | Topology topology = topologyService.currentTopology(); |
| 546 | if (topologyService.isInfrastructure(topology, heardOn)) { |
| 547 | return; |
| 548 | } |
| 549 | |
Thomas Vachuska | ec9c7dd | 2015-09-03 18:30:04 -0700 | [diff] [blame] | 550 | HostLocation hloc = new HostLocation(heardOn, System.currentTimeMillis()); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 551 | HostId hid = HostId.hostId(eth.getSourceMAC(), vlan); |
Charles Chan | 35a3232 | 2017-08-14 11:42:11 -0700 | [diff] [blame] | 552 | MacAddress destMac = eth.getDestinationMAC(); |
| 553 | |
| 554 | // Receives a location probe. Invalid entry from the cache |
| 555 | if (multihomingEnabled && destMac.isOnos() && !MacAddress.NONE.equals(destMac)) { |
| 556 | log.info("Receives probe for {}/{} on {}", srcMac, vlan, heardOn); |
| 557 | providerService.removePendingHostLocation(destMac); |
| 558 | return; |
| 559 | } |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 560 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 561 | // ARP: possible new hosts, update both location and IP |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 562 | if (eth.getEtherType() == Ethernet.TYPE_ARP) { |
| 563 | ARP arp = (ARP) eth.getPayload(); |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 564 | IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET, |
| 565 | arp.getSenderProtocolAddress()); |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 566 | createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, ip); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 567 | |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 568 | // IPv4: update location only |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 569 | } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) { |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 570 | // Update host location |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 571 | createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null); |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 572 | if (useDhcp) { |
| 573 | DHCP dhcp = findDhcp(eth).orElse(null); |
| 574 | // DHCP ACK: additionally update IP of DHCP client |
| 575 | if (dhcp != null && dhcp.getPacketType().equals(DHCP.MsgType.DHCPACK)) { |
| 576 | MacAddress hostMac = MacAddress.valueOf(dhcp.getClientHardwareAddress()); |
| 577 | VlanId hostVlan = VlanId.vlanId(eth.getVlanID()); |
| 578 | HostId hostId = HostId.hostId(hostMac, hostVlan); |
| 579 | updateHostIp(hostId, IpAddress.valueOf(dhcp.getYourIPAddress())); |
Yi Tseng | cfa637a | 2017-07-26 17:24:10 -0700 | [diff] [blame] | 580 | } |
Charles Chan | e606789 | 2016-11-17 10:23:46 -0800 | [diff] [blame] | 581 | } |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 582 | // NeighborAdvertisement and NeighborSolicitation: possible |
| 583 | // new hosts, update both location and IP. |
| 584 | // |
| 585 | // IPv6: update location only |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 586 | } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) { |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 587 | IPv6 ipv6 = (IPv6) eth.getPayload(); |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 588 | IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET6, |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 589 | ipv6.getSourceAddress()); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 590 | |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 591 | // skip extension headers |
| 592 | IPacket pkt = ipv6; |
| 593 | while (pkt.getPayload() != null && |
| 594 | pkt.getPayload() instanceof IExtensionHeader) { |
| 595 | pkt = pkt.getPayload(); |
| 596 | } |
Charles M.C. Chan | 9148d2d | 2015-04-27 03:36:39 +0800 | [diff] [blame] | 597 | pkt = pkt.getPayload(); |
Yi Tseng | fcf5dce | 2017-07-26 14:30:41 -0700 | [diff] [blame] | 598 | |
| 599 | // DHCPv6 protocol |
| 600 | DHCP6 dhcp6 = findDhcp6(pkt).orElse(null); |
| 601 | if (dhcp6 != null && useDhcp6) { |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 602 | createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null); |
Yi Tseng | fcf5dce | 2017-07-26 14:30:41 -0700 | [diff] [blame] | 603 | handleDhcp6(dhcp6, vlan); |
| 604 | return; |
| 605 | } |
| 606 | |
Charles M.C. Chan | 9148d2d | 2015-04-27 03:36:39 +0800 | [diff] [blame] | 607 | if (pkt != null && pkt instanceof ICMP6) { |
Yi Tseng | fcf5dce | 2017-07-26 14:30:41 -0700 | [diff] [blame] | 608 | // Neighbor Discovery Protocol |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 609 | pkt = pkt.getPayload(); |
Charles Chan | 178b0f5 | 2017-10-08 23:51:22 -0400 | [diff] [blame] | 610 | if (pkt != null) { |
| 611 | // RouterSolicitation, RouterAdvertisement |
| 612 | if (pkt instanceof RouterAdvertisement || pkt instanceof RouterSolicitation) { |
Charles M.C. Chan | 441d7da | 2015-03-17 21:03:39 +0800 | [diff] [blame] | 613 | return; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 614 | } |
Charles Chan | 178b0f5 | 2017-10-08 23:51:22 -0400 | [diff] [blame] | 615 | if (pkt instanceof NeighborSolicitation || pkt instanceof NeighborAdvertisement) { |
| 616 | // Duplicate Address Detection |
| 617 | if (ip.isZero()) { |
| 618 | return; |
| 619 | } |
| 620 | // NeighborSolicitation, NeighborAdvertisement |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 621 | createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, ip); |
Charles Chan | 178b0f5 | 2017-10-08 23:51:22 -0400 | [diff] [blame] | 622 | |
| 623 | // Also learn from the target address of NeighborAdvertisement |
| 624 | if (pkt instanceof NeighborAdvertisement) { |
| 625 | NeighborAdvertisement na = (NeighborAdvertisement) pkt; |
| 626 | Ip6Address targetAddr = Ip6Address.valueOf(na.getTargetAddress()); |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 627 | createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, targetAddr); |
Charles Chan | 178b0f5 | 2017-10-08 23:51:22 -0400 | [diff] [blame] | 628 | } |
| 629 | return; |
| 630 | } |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 631 | } |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 632 | } |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 633 | |
Yi Tseng | aa417a6 | 2017-09-08 17:22:51 -0700 | [diff] [blame] | 634 | // multicast, exclude DHCPv6 |
| 635 | if (eth.isMulticast() && dhcp6 == null) { |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 636 | return; |
| 637 | } |
| 638 | |
| 639 | // normal IPv6 packets |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 640 | createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 641 | } |
| 642 | } |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 643 | |
Yi Tseng | fcf5dce | 2017-07-26 14:30:41 -0700 | [diff] [blame] | 644 | /** |
| 645 | * Handles DHCPv6 packet, if message type is ACK, update IP address |
| 646 | * according to DHCPv6 payload (IA Address option). |
| 647 | * |
| 648 | * @param dhcp6 the DHCPv6 payload |
| 649 | * @param vlanId the vlan of this packet |
| 650 | */ |
| 651 | private void handleDhcp6(DHCP6 dhcp6, VlanId vlanId) { |
| 652 | // extract the relay message if exist |
| 653 | while (dhcp6 != null && DHCP6.RELAY_MSG_TYPES.contains(dhcp6.getMsgType())) { |
| 654 | dhcp6 = dhcp6.getOptions().stream() |
| 655 | .filter(opt -> opt instanceof Dhcp6RelayOption) |
| 656 | .map(BasePacket::getPayload) |
| 657 | .map(pld -> (DHCP6) pld) |
| 658 | .findFirst() |
| 659 | .orElse(null); |
| 660 | } |
| 661 | |
| 662 | if (dhcp6 == null) { |
| 663 | // Can't find dhcp payload |
| 664 | log.warn("Can't find dhcp payload from relay message"); |
| 665 | return; |
| 666 | } |
| 667 | |
| 668 | if (dhcp6.getMsgType() != DHCP6.MsgType.REPLY.value()) { |
| 669 | // Update IP address only when we received REPLY message |
| 670 | return; |
| 671 | } |
| 672 | Optional<Dhcp6ClientIdOption> clientIdOption = dhcp6.getOptions() |
| 673 | .stream() |
| 674 | .filter(opt -> opt instanceof Dhcp6ClientIdOption) |
| 675 | .map(opt -> (Dhcp6ClientIdOption) opt) |
| 676 | .findFirst(); |
| 677 | |
| 678 | if (!clientIdOption.isPresent()) { |
| 679 | // invalid DHCPv6 option |
| 680 | log.warn("Can't find client ID from DHCPv6 {}", dhcp6); |
| 681 | return; |
| 682 | } |
| 683 | |
| 684 | byte[] linkLayerAddr = clientIdOption.get().getDuid().getLinkLayerAddress(); |
| 685 | if (linkLayerAddr == null || linkLayerAddr.length != 6) { |
| 686 | // No any mac address found |
| 687 | log.warn("Can't find client mac from option {}", clientIdOption); |
| 688 | return; |
| 689 | } |
| 690 | MacAddress clientMac = MacAddress.valueOf(linkLayerAddr); |
| 691 | |
| 692 | // Extract IPv6 address from IA NA ot IA TA option |
| 693 | Optional<Dhcp6IaNaOption> iaNaOption = dhcp6.getOptions() |
| 694 | .stream() |
| 695 | .filter(opt -> opt instanceof Dhcp6IaNaOption) |
| 696 | .map(opt -> (Dhcp6IaNaOption) opt) |
| 697 | .findFirst(); |
| 698 | Optional<Dhcp6IaTaOption> iaTaOption = dhcp6.getOptions() |
| 699 | .stream() |
| 700 | .filter(opt -> opt instanceof Dhcp6IaTaOption) |
| 701 | .map(opt -> (Dhcp6IaTaOption) opt) |
| 702 | .findFirst(); |
| 703 | Optional<Dhcp6IaAddressOption> iaAddressOption; |
| 704 | if (iaNaOption.isPresent()) { |
| 705 | iaAddressOption = iaNaOption.get().getOptions().stream() |
| 706 | .filter(opt -> opt instanceof Dhcp6IaAddressOption) |
| 707 | .map(opt -> (Dhcp6IaAddressOption) opt) |
| 708 | .findFirst(); |
| 709 | } else if (iaTaOption.isPresent()) { |
| 710 | iaAddressOption = iaTaOption.get().getOptions().stream() |
| 711 | .filter(opt -> opt instanceof Dhcp6IaAddressOption) |
| 712 | .map(opt -> (Dhcp6IaAddressOption) opt) |
| 713 | .findFirst(); |
| 714 | } else { |
| 715 | iaAddressOption = Optional.empty(); |
| 716 | } |
| 717 | if (iaAddressOption.isPresent()) { |
| 718 | Ip6Address ip = iaAddressOption.get().getIp6Address(); |
| 719 | HostId hostId = HostId.hostId(clientMac, vlanId); |
| 720 | updateHostIp(hostId, ip); |
| 721 | } else { |
| 722 | log.warn("Can't find IPv6 address from DHCPv6 {}", dhcp6); |
| 723 | } |
| 724 | } |
| 725 | |
Yi Tseng | 7a38f9a | 2017-06-09 14:36:40 -0700 | [diff] [blame] | 726 | private Optional<DHCP> findDhcp(Ethernet eth) { |
| 727 | IPacket pkt = eth.getPayload(); |
| 728 | return Stream.of(pkt) |
| 729 | .filter(Objects::nonNull) |
| 730 | .filter(p -> p instanceof IPv4) |
| 731 | .map(IPacket::getPayload) |
| 732 | .filter(Objects::nonNull) |
| 733 | .filter(p -> p instanceof UDP) |
| 734 | .map(IPacket::getPayload) |
| 735 | .filter(Objects::nonNull) |
| 736 | .filter(p -> p instanceof DHCP) |
| 737 | .map(p -> (DHCP) p) |
| 738 | .findFirst(); |
| 739 | } |
Yi Tseng | fcf5dce | 2017-07-26 14:30:41 -0700 | [diff] [blame] | 740 | |
| 741 | private Optional<DHCP6> findDhcp6(IPacket pkt) { |
| 742 | return Stream.of(pkt) |
| 743 | .filter(Objects::nonNull) |
| 744 | .filter(p -> p instanceof UDP) |
| 745 | .map(IPacket::getPayload) |
| 746 | .filter(Objects::nonNull) |
| 747 | .filter(p -> p instanceof DHCP6) |
| 748 | .map(p -> (DHCP6) p) |
| 749 | .findFirst(); |
| 750 | } |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 751 | } |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 752 | |
| 753 | // Auxiliary listener to device events. |
| 754 | private class InternalDeviceListener implements DeviceListener { |
| 755 | @Override |
| 756 | public void event(DeviceEvent event) { |
Madan Jampani | a3770c3 | 2015-12-11 12:07:41 -0800 | [diff] [blame] | 757 | eventHandler.execute(() -> handleEvent(event)); |
| 758 | } |
| 759 | |
| 760 | private void handleEvent(DeviceEvent event) { |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 761 | Device device = event.subject(); |
| 762 | switch (event.type()) { |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 763 | case DEVICE_ADDED: |
| 764 | break; |
| 765 | case DEVICE_AVAILABILITY_CHANGED: |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 766 | if (hostRemovalEnabled && !deviceService.isAvailable(device.id())) { |
| 767 | processDeviceDown(device.id()); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 768 | } |
| 769 | break; |
| 770 | case DEVICE_SUSPENDED: |
| 771 | case DEVICE_UPDATED: |
| 772 | // Nothing to do? |
| 773 | break; |
| 774 | case DEVICE_REMOVED: |
| 775 | if (hostRemovalEnabled) { |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 776 | processDeviceDown(device.id()); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 777 | } |
| 778 | break; |
| 779 | case PORT_ADDED: |
| 780 | break; |
| 781 | case PORT_UPDATED: |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 782 | if (hostRemovalEnabled && !event.port().isEnabled()) { |
| 783 | processPortDown(new ConnectPoint(device.id(), event.port().number())); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 784 | } |
| 785 | break; |
| 786 | case PORT_REMOVED: |
| 787 | // Nothing to do? |
| 788 | break; |
| 789 | default: |
| 790 | break; |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 791 | } |
| 792 | } |
| 793 | } |
| 794 | |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 795 | /** |
| 796 | * When a device goes down, update the location of affected hosts. |
| 797 | * |
| 798 | * @param deviceId the device that goes down |
| 799 | */ |
| 800 | private void processDeviceDown(DeviceId deviceId) { |
| 801 | hostService.getConnectedHosts(deviceId).forEach(affectedHost -> affectedHost.locations().stream() |
| 802 | .filter(hostLocation -> hostLocation.deviceId().equals(deviceId)) |
| 803 | .forEach(affectedLocation -> |
| 804 | providerService.removeLocationFromHost(affectedHost.id(), affectedLocation)) |
| 805 | ); |
| 806 | } |
| 807 | |
| 808 | /** |
| 809 | * When a port goes down, update the location of affected hosts. |
| 810 | * |
| 811 | * @param connectPoint the port that goes down |
| 812 | */ |
| 813 | private void processPortDown(ConnectPoint connectPoint) { |
| 814 | hostService.getConnectedHosts(connectPoint).forEach(affectedHost -> |
| 815 | providerService.removeLocationFromHost(affectedHost.id(), new HostLocation(connectPoint, 0L)) |
| 816 | ); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 817 | } |
| 818 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 819 | } |