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