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