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