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