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