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