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