blob: dc58dccc9371a692700c7fa9eba798c9804db7d9 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.provider.host.impl;
alshabibe1cf87d2014-10-17 09:23:50 -070017
Charles Chan35a32322017-08-14 11:42:11 -070018import com.google.common.collect.Sets;
alshabibe1cf87d2014-10-17 09:23:50 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
Thomas Vachuska33601602014-11-19 03:32:15 -080022import org.apache.felix.scr.annotations.Modified;
23import org.apache.felix.scr.annotations.Property;
alshabibe1cf87d2014-10-17 09:23:50 -070024import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
Charles Chan47933752017-11-30 15:37:50 -080026import org.apache.felix.scr.annotations.Service;
Jonathan Harte8600eb2015-01-12 10:30:45 -080027import org.onlab.packet.ARP;
Yi Tsengfcf5dce2017-07-26 14:30:41 -070028import org.onlab.packet.BasePacket;
Charles Chane6067892016-11-17 10:23:46 -080029import org.onlab.packet.DHCP;
Yi Tsengfcf5dce2017-07-26 14:30:41 -070030import org.onlab.packet.DHCP6;
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070031import org.onlab.packet.EthType;
Jonathan Harte8600eb2015-01-12 10:30:45 -080032import org.onlab.packet.Ethernet;
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -080033import org.onlab.packet.ICMP6;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070034import org.onlab.packet.IPacket;
Charles Chane6067892016-11-17 10:23:46 -080035import org.onlab.packet.IPv4;
Jonathan Harte8600eb2015-01-12 10:30:45 -080036import org.onlab.packet.IPv6;
Charles Chan35a32322017-08-14 11:42:11 -070037import org.onlab.packet.Ip4Address;
Yi Tsengfcf5dce2017-07-26 14:30:41 -070038import org.onlab.packet.Ip6Address;
Jonathan Harte8600eb2015-01-12 10:30:45 -080039import org.onlab.packet.IpAddress;
Charles M.C. Chan956cb692015-04-26 18:49:39 +080040import org.onlab.packet.MacAddress;
Charles Chane6067892016-11-17 10:23:46 -080041import org.onlab.packet.UDP;
Jonathan Harte8600eb2015-01-12 10:30:45 -080042import org.onlab.packet.VlanId;
Yi Tsengfcf5dce2017-07-26 14:30:41 -070043import org.onlab.packet.dhcp.Dhcp6ClientIdOption;
44import org.onlab.packet.dhcp.Dhcp6IaAddressOption;
45import org.onlab.packet.dhcp.Dhcp6IaNaOption;
46import org.onlab.packet.dhcp.Dhcp6IaTaOption;
47import org.onlab.packet.dhcp.Dhcp6RelayOption;
Charles M.C. Chan956cb692015-04-26 18:49:39 +080048import org.onlab.packet.ipv6.IExtensionHeader;
Jonathan Harte8600eb2015-01-12 10:30:45 -080049import org.onlab.packet.ndp.NeighborAdvertisement;
50import org.onlab.packet.ndp.NeighborSolicitation;
Charles M.C. Chan441d7da2015-03-17 21:03:39 +080051import org.onlab.packet.ndp.RouterAdvertisement;
52import org.onlab.packet.ndp.RouterSolicitation;
Jian Lid9b5f552016-03-11 18:15:31 -080053import org.onlab.util.Tools;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070054import org.onosproject.cfg.ComponentConfigService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080055import org.onosproject.core.ApplicationId;
56import org.onosproject.core.CoreService;
Charles Chan47933752017-11-30 15:37:50 -080057import org.onosproject.net.host.HostLocationProbingService;
Ray Milkeyfacf2862017-08-03 11:58:29 -070058import org.onosproject.net.intf.InterfaceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080059import org.onosproject.net.ConnectPoint;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080060import org.onosproject.net.Device;
Charles Chan888e20a2017-05-01 15:44:23 -070061import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080062import org.onosproject.net.Host;
63import org.onosproject.net.HostId;
64import org.onosproject.net.HostLocation;
65import org.onosproject.net.device.DeviceEvent;
66import org.onosproject.net.device.DeviceListener;
67import org.onosproject.net.device.DeviceService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080068import org.onosproject.net.flow.DefaultTrafficSelector;
sdn94b00152016-08-30 02:12:32 -070069import org.onosproject.net.flow.DefaultTrafficTreatment;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080070import org.onosproject.net.flow.TrafficSelector;
sdn94b00152016-08-30 02:12:32 -070071import org.onosproject.net.flow.TrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080072import org.onosproject.net.host.DefaultHostDescription;
73import org.onosproject.net.host.HostDescription;
74import org.onosproject.net.host.HostProvider;
75import org.onosproject.net.host.HostProviderRegistry;
76import org.onosproject.net.host.HostProviderService;
77import org.onosproject.net.host.HostService;
sdn94b00152016-08-30 02:12:32 -070078import org.onosproject.net.packet.DefaultOutboundPacket;
79import org.onosproject.net.packet.OutboundPacket;
Brian O'Connorabafb502014-12-02 22:26:20 -080080import org.onosproject.net.packet.PacketContext;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080081import org.onosproject.net.packet.PacketPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080082import org.onosproject.net.packet.PacketProcessor;
83import org.onosproject.net.packet.PacketService;
84import org.onosproject.net.provider.AbstractProvider;
85import org.onosproject.net.provider.ProviderId;
86import org.onosproject.net.topology.Topology;
87import org.onosproject.net.topology.TopologyService;
Thomas Vachuska33601602014-11-19 03:32:15 -080088import org.osgi.service.component.ComponentContext;
alshabibe1cf87d2014-10-17 09:23:50 -070089import org.slf4j.Logger;
90
sdn94b00152016-08-30 02:12:32 -070091import java.nio.ByteBuffer;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070092import java.util.Dictionary;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070093import java.util.Objects;
94import java.util.Optional;
Madan Jampania3770c32015-12-11 12:07:41 -080095import java.util.concurrent.ExecutorService;
Charles Chanbb86b782017-09-13 16:14:22 -070096import java.util.concurrent.ScheduledExecutorService;
97import java.util.concurrent.TimeUnit;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070098import java.util.stream.Stream;
Charles Chan35a32322017-08-14 11:42:11 -070099import java.util.Set;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700100
Charles Chan6577f612018-04-19 13:10:01 -0700101import static java.util.concurrent.Executors.newScheduledThreadPool;
Madan Jampania3770c32015-12-11 12:07:41 -0800102import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
103import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700104import static org.slf4j.LoggerFactory.getLogger;
105
alshabibe1cf87d2014-10-17 09:23:50 -0700106/**
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700107 * Provider which uses an OpenFlow controller to detect network end-station
108 * hosts.
alshabibe1cf87d2014-10-17 09:23:50 -0700109 */
110@Component(immediate = true)
Charles Chan47933752017-11-30 15:37:50 -0800111@Service
112public class HostLocationProvider extends AbstractProvider implements HostProvider, HostLocationProbingService {
alshabibe1cf87d2014-10-17 09:23:50 -0700113 private final Logger log = getLogger(getClass());
114
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800115 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
116 protected CoreService coreService;
117
118 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibe1cf87d2014-10-17 09:23:50 -0700119 protected HostProviderRegistry providerRegistry;
120
121 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800122 protected PacketService packetService;
alshabibe1cf87d2014-10-17 09:23:50 -0700123
124 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
125 protected TopologyService topologyService;
126
Thomas Vachuska33601602014-11-19 03:32:15 -0800127 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
128 protected HostService hostService;
129
130 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
131 protected DeviceService deviceService;
132
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700133 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
134 protected ComponentConfigService cfgService;
135
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700136 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
137 protected InterfaceService interfaceService;
138
alshabibe1cf87d2014-10-17 09:23:50 -0700139 private HostProviderService providerService;
140
141 private final InternalHostProvider processor = new InternalHostProvider();
Thomas Vachuska33601602014-11-19 03:32:15 -0800142 private final DeviceListener deviceListener = new InternalDeviceListener();
143
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800144 private ApplicationId appId;
145
Thomas Vachuska33601602014-11-19 03:32:15 -0800146 @Property(name = "hostRemovalEnabled", boolValue = true,
147 label = "Enable host removal on port/device down events")
148 private boolean hostRemovalEnabled = true;
alshabibe1cf87d2014-10-17 09:23:50 -0700149
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700150 @Property(name = "requestArp", boolValue = true,
151 label = "Request ARP packets for neighbor discovery by the " +
Charles Chane6067892016-11-17 10:23:46 -0800152 "Host Location Provider; default is true")
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700153 private boolean requestArp = true;
Charles Chane6067892016-11-17 10:23:46 -0800154
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700155 @Property(name = "requestIpv6ND", boolValue = false,
156 label = "Requests IPv6 Neighbor Discovery by the " +
Thomas Vachuska27bee092015-06-23 19:03:10 -0700157 "Host Location Provider; default is false")
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700158 private boolean requestIpv6ND = false;
Charles Chane6067892016-11-17 10:23:46 -0800159
160 @Property(name = "useDhcp", boolValue = false,
Yi Tsengaa417a62017-09-08 17:22:51 -0700161 label = "Use DHCP to update IP address of the host; default is false")
Charles Chane6067892016-11-17 10:23:46 -0800162 private boolean useDhcp = false;
alshabibe1cf87d2014-10-17 09:23:50 -0700163
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700164 @Property(name = "useDhcp6", boolValue = false,
Yi Tsengaa417a62017-09-08 17:22:51 -0700165 label = "Use DHCPv6 to update IP address of the host; default is false")
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700166 private boolean useDhcp6 = false;
167
Jonathan Hart9af322d2016-01-06 17:42:04 -0800168 @Property(name = "requestInterceptsEnabled", boolValue = true,
169 label = "Enable requesting packet intercepts")
170 private boolean requestInterceptsEnabled = true;
171
Charles Chan35a32322017-08-14 11:42:11 -0700172 @Property(name = "multihomingEnabled", boolValue = false,
173 label = "Allow hosts to be multihomed")
174 private boolean multihomingEnabled = false;
Madan Jampania3770c32015-12-11 12:07:41 -0800175
Charles Chan47933752017-11-30 15:37:50 -0800176 private int probeInitDelayMs = 1000;
177
Charles Chan6577f612018-04-19 13:10:01 -0700178 ExecutorService eventHandler;
Charles Chan07f15f22018-05-08 21:35:50 -0700179 private ExecutorService packetHandler;
Charles Chan6577f612018-04-19 13:10:01 -0700180 private ScheduledExecutorService hostProber;
Charles Chan8277b6b2017-12-03 13:48:46 -0800181
alshabibe1cf87d2014-10-17 09:23:50 -0700182 /**
183 * Creates an OpenFlow host provider.
184 */
185 public HostLocationProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800186 super(new ProviderId("of", "org.onosproject.provider.host"));
alshabibe1cf87d2014-10-17 09:23:50 -0700187 }
188
189 @Activate
Thomas Vachuska33601602014-11-19 03:32:15 -0800190 public void activate(ComponentContext context) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700191 cfgService.registerProperties(getClass());
192 appId = coreService.registerApplication("org.onosproject.provider.host");
Charles Chan6577f612018-04-19 13:10:01 -0700193 eventHandler = newSingleThreadScheduledExecutor(groupedThreads("onos/host-loc-provider", "event-handler", log));
Charles Chan07f15f22018-05-08 21:35:50 -0700194 packetHandler = newSingleThreadScheduledExecutor(groupedThreads("onos/host-loc-provider",
195 "packet-handler", log));
Charles Chan6577f612018-04-19 13:10:01 -0700196 hostProber = newScheduledThreadPool(32, groupedThreads("onos/host-loc-probe", "%d", log));
alshabibe1cf87d2014-10-17 09:23:50 -0700197 providerService = providerRegistry.register(this);
Brian O'Connor3b783262015-07-29 17:49:24 -0700198 packetService.addProcessor(processor, PacketProcessor.advisor(1));
Thomas Vachuska33601602014-11-19 03:32:15 -0800199 deviceService.addListener(deviceListener);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800200
201 modified(context);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800202
Charles M.C. Chane148de82015-05-06 12:38:21 +0800203 log.info("Started with Application ID {}", appId.id());
204 }
205
206 @Deactivate
207 public void deactivate() {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800208 cfgService.unregisterProperties(getClass(), false);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700209
210 withdrawIntercepts();
211
Charles M.C. Chane148de82015-05-06 12:38:21 +0800212 providerRegistry.unregister(this);
213 packetService.removeProcessor(processor);
214 deviceService.removeListener(deviceListener);
Madan Jampania3770c32015-12-11 12:07:41 -0800215 eventHandler.shutdown();
Charles Chan07f15f22018-05-08 21:35:50 -0700216 packetHandler.shutdown();
Charles Chan6577f612018-04-19 13:10:01 -0700217 hostProber.shutdown();
Charles M.C. Chane148de82015-05-06 12:38:21 +0800218 providerService = null;
219 log.info("Stopped");
220 }
221
222 @Modified
223 public void modified(ComponentContext context) {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800224 readComponentConfiguration(context);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800225
226 if (requestInterceptsEnabled) {
227 requestIntercepts();
228 } else {
229 withdrawIntercepts();
230 }
Charles M.C. Chane148de82015-05-06 12:38:21 +0800231 }
232
233 /**
Thomas Vachuska27bee092015-06-23 19:03:10 -0700234 * Request packet intercepts.
Charles M.C. Chane148de82015-05-06 12:38:21 +0800235 */
Thomas Vachuska27bee092015-06-23 19:03:10 -0700236 private void requestIntercepts() {
Charles Chane6067892016-11-17 10:23:46 -0800237 // Use ARP
238 TrafficSelector arpSelector = DefaultTrafficSelector.builder()
239 .matchEthType(Ethernet.TYPE_ARP)
240 .build();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700241 if (requestArp) {
Charles Chane6067892016-11-17 10:23:46 -0800242 packetService.requestPackets(arpSelector, PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700243 } else {
Charles Chane6067892016-11-17 10:23:46 -0800244 packetService.cancelPackets(arpSelector, PacketPriority.CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800245 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700246
Charles Chane6067892016-11-17 10:23:46 -0800247 // Use IPv6 Neighbor Discovery
248 TrafficSelector ipv6NsSelector = DefaultTrafficSelector.builder()
249 .matchEthType(Ethernet.TYPE_IPV6)
250 .matchIPProtocol(IPv6.PROTOCOL_ICMP6)
251 .matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION)
252 .build();
253 TrafficSelector ipv6NaSelector = DefaultTrafficSelector.builder()
254 .matchEthType(Ethernet.TYPE_IPV6)
255 .matchIPProtocol(IPv6.PROTOCOL_ICMP6)
256 .matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT)
257 .build();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700258 if (requestIpv6ND) {
Charles Chane6067892016-11-17 10:23:46 -0800259 packetService.requestPackets(ipv6NsSelector, PacketPriority.CONTROL, appId);
260 packetService.requestPackets(ipv6NaSelector, PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700261 } else {
Charles Chane6067892016-11-17 10:23:46 -0800262 packetService.cancelPackets(ipv6NsSelector, PacketPriority.CONTROL, appId);
263 packetService.cancelPackets(ipv6NaSelector, PacketPriority.CONTROL, appId);
264 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700265 }
266
267 /**
268 * Withdraw packet intercepts.
269 */
270 private void withdrawIntercepts() {
271 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
272 selector.matchEthType(Ethernet.TYPE_ARP);
Aaron Kruglikov07a923d2015-07-03 13:30:57 -0700273 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700274
275 // IPv6 Neighbor Solicitation packet.
276 selector.matchEthType(Ethernet.TYPE_IPV6);
277 selector.matchIPProtocol(IPv6.PROTOCOL_ICMP6);
278 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION);
279 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
280
281 // IPv6 Neighbor Advertisement packet.
282 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT);
283 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800284 }
285
286 /**
287 * Extracts properties from the component configuration context.
288 *
289 * @param context the component context
290 */
291 private void readComponentConfiguration(ComponentContext context) {
292 Dictionary<?, ?> properties = context.getProperties();
293 Boolean flag;
294
Jian Lid9b5f552016-03-11 18:15:31 -0800295 flag = Tools.isPropertyEnabled(properties, "hostRemovalEnabled");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800296 if (flag == null) {
297 log.info("Host removal on port/device down events is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700298 "using current value of {}", hostRemovalEnabled);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800299 } else {
300 hostRemovalEnabled = flag;
301 log.info("Configured. Host removal on port/device down events is {}",
302 hostRemovalEnabled ? "enabled" : "disabled");
Thomas Vachuska33601602014-11-19 03:32:15 -0800303 }
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800304
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700305 flag = Tools.isPropertyEnabled(properties, "requestArp");
Charles Chane6067892016-11-17 10:23:46 -0800306 if (flag == null) {
307 log.info("Using ARP is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700308 "using current value of {}", requestArp);
Charles Chane6067892016-11-17 10:23:46 -0800309 } else {
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700310 requestArp = flag;
Charles Chane6067892016-11-17 10:23:46 -0800311 log.info("Configured. Using ARP is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700312 requestArp ? "enabled" : "disabled");
Charles Chane6067892016-11-17 10:23:46 -0800313 }
314
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700315 flag = Tools.isPropertyEnabled(properties, "requestIpv6ND");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800316 if (flag == null) {
317 log.info("Using IPv6 Neighbor Discovery is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700318 "using current value of {}", requestIpv6ND);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800319 } else {
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700320 requestIpv6ND = flag;
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800321 log.info("Configured. Using IPv6 Neighbor Discovery is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700322 requestIpv6ND ? "enabled" : "disabled");
Charles Chane6067892016-11-17 10:23:46 -0800323 }
324
325 flag = Tools.isPropertyEnabled(properties, "useDhcp");
326 if (flag == null) {
327 log.info("Using DHCP is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700328 "using current value of {}", useDhcp);
Charles Chane6067892016-11-17 10:23:46 -0800329 } else {
330 useDhcp = flag;
331 log.info("Configured. Using DHCP is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700332 useDhcp ? "enabled" : "disabled");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800333 }
Jonathan Hart9af322d2016-01-06 17:42:04 -0800334
Jian Lid9b5f552016-03-11 18:15:31 -0800335 flag = Tools.isPropertyEnabled(properties, "requestInterceptsEnabled");
Jonathan Hart9af322d2016-01-06 17:42:04 -0800336 if (flag == null) {
337 log.info("Request intercepts is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700338 "using current value of {}", requestInterceptsEnabled);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800339 } else {
340 requestInterceptsEnabled = flag;
341 log.info("Configured. Request intercepts is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700342 requestInterceptsEnabled ? "enabled" : "disabled");
Jonathan Hart9af322d2016-01-06 17:42:04 -0800343 }
Charles Chan35a32322017-08-14 11:42:11 -0700344
345 flag = Tools.isPropertyEnabled(properties, "multihomingEnabled");
346 if (flag == null) {
347 log.info("Multihoming is not configured, " +
348 "using current value of {}", multihomingEnabled);
349 } else {
350 multihomingEnabled = flag;
351 log.info("Configured. Multihoming is {}",
352 multihomingEnabled ? "enabled" : "disabled");
353 }
Charles Chan47933752017-11-30 15:37:50 -0800354 }
Charles Chan35a32322017-08-14 11:42:11 -0700355
Charles Chan47933752017-11-30 15:37:50 -0800356 @Override
357 public void probeHostLocation(Host host, ConnectPoint connectPoint, ProbeMode probeMode) {
358 host.ipAddresses().stream().findFirst().ifPresent(ip -> {
359 MacAddress probeMac = providerService.addPendingHostLocation(host.id(), connectPoint, probeMode);
360 log.debug("Constructing {} probe for host {} with {}", probeMode, host.id(), ip);
361 Ethernet probe;
362 if (ip.isIp4()) {
363 probe = ARP.buildArpRequest(probeMac.toBytes(), Ip4Address.ZERO.toOctets(),
364 host.id().mac().toBytes(), ip.toOctets(),
365 host.id().mac().toBytes(), host.id().vlanId().toShort());
366 } else {
367 probe = NeighborSolicitation.buildNdpSolicit(
368 ip.getIp6Address(),
369 Ip6Address.valueOf(IPv6.getLinkLocalAddress(probeMac.toBytes())),
370 ip.getIp6Address(),
371 probeMac,
372 host.id().mac(),
373 host.id().vlanId());
374 }
375
376 // NOTE: delay the probe a little bit to wait for the store synchronization is done
Charles Chan6577f612018-04-19 13:10:01 -0700377 hostProber.schedule(() ->
Charles Chan47933752017-11-30 15:37:50 -0800378 sendLocationProbe(probe, connectPoint), probeInitDelayMs, TimeUnit.MILLISECONDS);
379 });
380 }
381
382 /**
383 * Send the probe packet on given port.
384 *
385 * @param probe the probe packet
386 * @param connectPoint the port we want to probe
387 */
388 private void sendLocationProbe(Ethernet probe, ConnectPoint connectPoint) {
Charles Chan1dd75572018-05-08 11:49:05 -0700389 log.debug("Sending probe for host {} on location {} with probeMac {}",
Charles Chan47933752017-11-30 15:37:50 -0800390 probe.getDestinationMAC(), connectPoint, probe.getSourceMAC());
391 TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(connectPoint.port()).build();
392 OutboundPacket outboundPacket = new DefaultOutboundPacket(connectPoint.deviceId(),
393 treatment, ByteBuffer.wrap(probe.serialize()));
394 packetService.emit(outboundPacket);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800395 }
396
alshabibe1cf87d2014-10-17 09:23:50 -0700397 @Override
398 public void triggerProbe(Host host) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800399 //log.info("Triggering probe on device {} ", host);
Jonathan Hartf353e402016-09-23 09:27:36 -0700400
401 // FIXME Disabling host probing for now, because sending packets from a
402 // broadcast MAC address caused problems when two ONOS networks were
403 // interconnected. Host probing should take into account the interface
404 // configuration when determining which source address to use.
405
406 //MastershipRole role = deviceService.getRole(host.location().deviceId());
407 //if (role.equals(MastershipRole.MASTER)) {
408 // host.ipAddresses().forEach(ip -> {
409 // sendProbe(host, ip);
410 // });
411 //} else {
412 // log.info("not the master, master will probe {}");
413 //}
sdn94b00152016-08-30 02:12:32 -0700414 }
415
416 private void sendProbe(Host host, IpAddress targetIp) {
417 Ethernet probePacket = null;
418 if (targetIp.isIp4()) {
419 // IPv4: Use ARP
420 probePacket = buildArpRequest(targetIp, host);
421 } else {
422 // IPv6: Use Neighbor Discovery
Jonathan Hartf353e402016-09-23 09:27:36 -0700423 //TODO need to implement ndp probe
sdn94b00152016-08-30 02:12:32 -0700424 log.info("Triggering probe on device {} ", host);
Ray Milkey74e59132018-01-17 15:24:52 -0800425 return;
sdn94b00152016-08-30 02:12:32 -0700426 }
427
428 TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(host.location().port()).build();
429
430 OutboundPacket outboundPacket = new DefaultOutboundPacket(host.location().deviceId(), treatment,
431 ByteBuffer.wrap(probePacket.serialize()));
432
433 packetService.emit(outboundPacket);
434 }
435
Charles Chan888e20a2017-05-01 15:44:23 -0700436 // This method is using source ip as 0.0.0.0 , to receive the reply even from the sub net hosts.
sdn94b00152016-08-30 02:12:32 -0700437 private Ethernet buildArpRequest(IpAddress targetIp, Host host) {
Charles Chan35a32322017-08-14 11:42:11 -0700438 return ARP.buildArpRequest(MacAddress.BROADCAST.toBytes(), Ip4Address.ZERO.toOctets(),
439 MacAddress.BROADCAST.toBytes(), targetIp.toOctets(),
440 MacAddress.BROADCAST.toBytes(), VlanId.NONE.toShort());
alshabibe1cf87d2014-10-17 09:23:50 -0700441 }
442
443 private class InternalHostProvider implements PacketProcessor {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800444 /**
Charles Chan888e20a2017-05-01 15:44:23 -0700445 * Create or update host information.
446 * Will not update IP if IP is null, all zero or self-assigned.
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800447 *
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700448 * @param hid host ID
449 * @param mac source Mac address
450 * @param vlan VLAN ID
451 * @param innerVlan inner VLAN ID
452 * @param outerTpid outer TPID
453 * @param hloc host location
454 * @param ip source IP address or null if not updating
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800455 */
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700456 private void createOrUpdateHost(HostId hid, MacAddress mac, VlanId vlan,
457 VlanId innerVlan, EthType outerTpid,
458 HostLocation hloc, IpAddress ip) {
Charles Chan35a32322017-08-14 11:42:11 -0700459 Set<HostLocation> newLocations = Sets.newHashSet(hloc);
460
461 if (multihomingEnabled) {
462 Host existingHost = hostService.getHost(hid);
463 if (existingHost != null) {
464 Set<HostLocation> prevLocations = existingHost.locations();
Charles Chan35a32322017-08-14 11:42:11 -0700465
Charles Chan653e2ac2017-11-22 14:49:55 -0800466 if (prevLocations.stream().noneMatch(loc -> loc.deviceId().equals(hloc.deviceId()))) {
467 // New location is on a device that we haven't seen before
468 // Could be a dual-home host. Append new location and send out the probe
469 newLocations.addAll(prevLocations);
Charles Chan47933752017-11-30 15:37:50 -0800470 prevLocations.forEach(prevLocation ->
471 probeHostLocation(existingHost, prevLocation, ProbeMode.VERIFY));
Charles Chan653e2ac2017-11-22 14:49:55 -0800472 } else {
473 // Move within the same switch
474 // Simply replace old location that is on the same device
475 prevLocations.stream().filter(loc -> !loc.deviceId().equals(hloc.deviceId()))
476 .forEach(newLocations::add);
Charles Chan35a32322017-08-14 11:42:11 -0700477 }
478 }
479 }
480
Charles Chan888e20a2017-05-01 15:44:23 -0700481 HostDescription desc = ip == null || ip.isZero() || ip.isSelfAssigned() ?
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700482 new DefaultHostDescription(mac, vlan, newLocations, Sets.newHashSet(),
483 innerVlan, outerTpid, false) :
484 new DefaultHostDescription(mac, vlan, newLocations, Sets.newHashSet(ip),
485 innerVlan, outerTpid, false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700486 try {
Ray Milkeydc083442016-02-22 11:27:57 -0800487 providerService.hostDetected(hid, desc, false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700488 } catch (IllegalStateException e) {
489 log.debug("Host {} suppressed", hid);
490 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800491 }
alshabibe1cf87d2014-10-17 09:23:50 -0700492
Charles Chane6067892016-11-17 10:23:46 -0800493 /**
Charles Chan888e20a2017-05-01 15:44:23 -0700494 * Updates IP address for an existing host.
Charles Chane6067892016-11-17 10:23:46 -0800495 *
496 * @param hid host ID
497 * @param ip IP address
498 */
Charles Chan888e20a2017-05-01 15:44:23 -0700499 private void updateHostIp(HostId hid, IpAddress ip) {
Charles Chane6067892016-11-17 10:23:46 -0800500 Host host = hostService.getHost(hid);
501 if (host == null) {
Charles Chan35a32322017-08-14 11:42:11 -0700502 log.warn("Fail to update IP for {}. Host does not exist", hid);
Charles Chane6067892016-11-17 10:23:46 -0800503 return;
504 }
505
Charles Chan888e20a2017-05-01 15:44:23 -0700506 HostDescription desc = new DefaultHostDescription(hid.mac(), hid.vlanId(),
Charles Chan35a32322017-08-14 11:42:11 -0700507 host.locations(), Sets.newHashSet(ip), false);
Charles Chane6067892016-11-17 10:23:46 -0800508 try {
509 providerService.hostDetected(hid, desc, false);
510 } catch (IllegalStateException e) {
511 log.debug("Host {} suppressed", hid);
512 }
513 }
514
alshabibe1cf87d2014-10-17 09:23:50 -0700515 @Override
516 public void process(PacketContext context) {
Charles Chan07f15f22018-05-08 21:35:50 -0700517 packetHandler.execute(() -> processPacketInternal(context));
518 }
519
520 private void processPacketInternal(PacketContext context) {
alshabib4a179dc2014-10-17 17:17:01 -0700521 if (context == null) {
522 return;
523 }
alshabibe1cf87d2014-10-17 09:23:50 -0700524
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700525 Ethernet eth = context.inPacket().parsed();
Jonathan Harte8600eb2015-01-12 10:30:45 -0800526 if (eth == null) {
527 return;
528 }
Jonathan Hartf353e402016-09-23 09:27:36 -0700529
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800530 MacAddress srcMac = eth.getSourceMAC();
Jonathan Hartf353e402016-09-23 09:27:36 -0700531 if (srcMac.isBroadcast() || srcMac.isMulticast()) {
532 return;
533 }
Jonathan Harte8600eb2015-01-12 10:30:45 -0800534
alshabibe1cf87d2014-10-17 09:23:50 -0700535 VlanId vlan = VlanId.vlanId(eth.getVlanID());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700536 VlanId outerVlan = VlanId.vlanId(eth.getQinQVID());
537 VlanId innerVlan = VlanId.NONE;
538 EthType outerTpid = EthType.EtherType.UNKNOWN.ethType();
539 // Set up values for double-tagged hosts
540 if (outerVlan.toShort() != Ethernet.VLAN_UNTAGGED) {
541 innerVlan = vlan;
542 vlan = outerVlan;
543 outerTpid = EthType.EtherType.lookup(eth.getQinQTPID()).ethType();
544 }
alshabibe1cf87d2014-10-17 09:23:50 -0700545 ConnectPoint heardOn = context.inPacket().receivedFrom();
546
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700547 // If this arrived on control port, bail out.
548 if (heardOn.port().isLogical()) {
549 return;
550 }
551
alshabibe1cf87d2014-10-17 09:23:50 -0700552 // If this is not an edge port, bail out.
553 Topology topology = topologyService.currentTopology();
554 if (topologyService.isInfrastructure(topology, heardOn)) {
555 return;
556 }
557
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700558 HostLocation hloc = new HostLocation(heardOn, System.currentTimeMillis());
alshabibe1cf87d2014-10-17 09:23:50 -0700559 HostId hid = HostId.hostId(eth.getSourceMAC(), vlan);
Charles Chan35a32322017-08-14 11:42:11 -0700560 MacAddress destMac = eth.getDestinationMAC();
561
562 // Receives a location probe. Invalid entry from the cache
563 if (multihomingEnabled && destMac.isOnos() && !MacAddress.NONE.equals(destMac)) {
Charles Chan1dd75572018-05-08 11:49:05 -0700564 log.debug("Receives probe for {}/{} on {}", srcMac, vlan, heardOn);
Charles Chan35a32322017-08-14 11:42:11 -0700565 providerService.removePendingHostLocation(destMac);
566 return;
567 }
alshabibe1cf87d2014-10-17 09:23:50 -0700568
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800569 // ARP: possible new hosts, update both location and IP
alshabibe1cf87d2014-10-17 09:23:50 -0700570 if (eth.getEtherType() == Ethernet.TYPE_ARP) {
571 ARP arp = (ARP) eth.getPayload();
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800572 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET,
573 arp.getSenderProtocolAddress());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700574 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, ip);
alshabibe1cf87d2014-10-17 09:23:50 -0700575
Charles Chan5d5e0622015-09-25 13:00:06 -0700576 // IPv4: update location only
alshabibe1cf87d2014-10-17 09:23:50 -0700577 } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) {
Yi Tsengaa417a62017-09-08 17:22:51 -0700578 // Update host location
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700579 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
Yi Tsengaa417a62017-09-08 17:22:51 -0700580 if (useDhcp) {
581 DHCP dhcp = findDhcp(eth).orElse(null);
582 // DHCP ACK: additionally update IP of DHCP client
583 if (dhcp != null && dhcp.getPacketType().equals(DHCP.MsgType.DHCPACK)) {
584 MacAddress hostMac = MacAddress.valueOf(dhcp.getClientHardwareAddress());
585 VlanId hostVlan = VlanId.vlanId(eth.getVlanID());
586 HostId hostId = HostId.hostId(hostMac, hostVlan);
587 updateHostIp(hostId, IpAddress.valueOf(dhcp.getYourIPAddress()));
Yi Tsengcfa637a2017-07-26 17:24:10 -0700588 }
Charles Chane6067892016-11-17 10:23:46 -0800589 }
Charles Chan5d5e0622015-09-25 13:00:06 -0700590 // NeighborAdvertisement and NeighborSolicitation: possible
591 // new hosts, update both location and IP.
592 //
593 // IPv6: update location only
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800594 } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800595 IPv6 ipv6 = (IPv6) eth.getPayload();
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800596 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET6,
Thomas Vachuska27bee092015-06-23 19:03:10 -0700597 ipv6.getSourceAddress());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800598
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800599 // skip extension headers
600 IPacket pkt = ipv6;
601 while (pkt.getPayload() != null &&
602 pkt.getPayload() instanceof IExtensionHeader) {
603 pkt = pkt.getPayload();
604 }
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800605 pkt = pkt.getPayload();
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700606
607 // DHCPv6 protocol
608 DHCP6 dhcp6 = findDhcp6(pkt).orElse(null);
609 if (dhcp6 != null && useDhcp6) {
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700610 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700611 handleDhcp6(dhcp6, vlan);
612 return;
613 }
614
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800615 if (pkt != null && pkt instanceof ICMP6) {
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700616 // Neighbor Discovery Protocol
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800617 pkt = pkt.getPayload();
Charles Chan178b0f52017-10-08 23:51:22 -0400618 if (pkt != null) {
619 // RouterSolicitation, RouterAdvertisement
620 if (pkt instanceof RouterAdvertisement || pkt instanceof RouterSolicitation) {
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800621 return;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800622 }
Charles Chan178b0f52017-10-08 23:51:22 -0400623 if (pkt instanceof NeighborSolicitation || pkt instanceof NeighborAdvertisement) {
624 // Duplicate Address Detection
625 if (ip.isZero()) {
626 return;
627 }
628 // NeighborSolicitation, NeighborAdvertisement
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700629 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, ip);
Charles Chan178b0f52017-10-08 23:51:22 -0400630
631 // Also learn from the target address of NeighborAdvertisement
632 if (pkt instanceof NeighborAdvertisement) {
633 NeighborAdvertisement na = (NeighborAdvertisement) pkt;
634 Ip6Address targetAddr = Ip6Address.valueOf(na.getTargetAddress());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700635 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, targetAddr);
Charles Chan178b0f52017-10-08 23:51:22 -0400636 }
637 return;
638 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800639 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800640 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800641
Yi Tsengaa417a62017-09-08 17:22:51 -0700642 // multicast, exclude DHCPv6
643 if (eth.isMulticast() && dhcp6 == null) {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800644 return;
645 }
646
647 // normal IPv6 packets
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700648 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
alshabibe1cf87d2014-10-17 09:23:50 -0700649 }
650 }
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700651
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700652 /**
653 * Handles DHCPv6 packet, if message type is ACK, update IP address
654 * according to DHCPv6 payload (IA Address option).
655 *
656 * @param dhcp6 the DHCPv6 payload
657 * @param vlanId the vlan of this packet
658 */
659 private void handleDhcp6(DHCP6 dhcp6, VlanId vlanId) {
660 // extract the relay message if exist
661 while (dhcp6 != null && DHCP6.RELAY_MSG_TYPES.contains(dhcp6.getMsgType())) {
662 dhcp6 = dhcp6.getOptions().stream()
663 .filter(opt -> opt instanceof Dhcp6RelayOption)
664 .map(BasePacket::getPayload)
665 .map(pld -> (DHCP6) pld)
666 .findFirst()
667 .orElse(null);
668 }
669
670 if (dhcp6 == null) {
671 // Can't find dhcp payload
672 log.warn("Can't find dhcp payload from relay message");
673 return;
674 }
675
676 if (dhcp6.getMsgType() != DHCP6.MsgType.REPLY.value()) {
677 // Update IP address only when we received REPLY message
678 return;
679 }
680 Optional<Dhcp6ClientIdOption> clientIdOption = dhcp6.getOptions()
681 .stream()
682 .filter(opt -> opt instanceof Dhcp6ClientIdOption)
683 .map(opt -> (Dhcp6ClientIdOption) opt)
684 .findFirst();
685
686 if (!clientIdOption.isPresent()) {
687 // invalid DHCPv6 option
688 log.warn("Can't find client ID from DHCPv6 {}", dhcp6);
689 return;
690 }
691
692 byte[] linkLayerAddr = clientIdOption.get().getDuid().getLinkLayerAddress();
693 if (linkLayerAddr == null || linkLayerAddr.length != 6) {
694 // No any mac address found
695 log.warn("Can't find client mac from option {}", clientIdOption);
696 return;
697 }
698 MacAddress clientMac = MacAddress.valueOf(linkLayerAddr);
699
700 // Extract IPv6 address from IA NA ot IA TA option
701 Optional<Dhcp6IaNaOption> iaNaOption = dhcp6.getOptions()
702 .stream()
703 .filter(opt -> opt instanceof Dhcp6IaNaOption)
704 .map(opt -> (Dhcp6IaNaOption) opt)
705 .findFirst();
706 Optional<Dhcp6IaTaOption> iaTaOption = dhcp6.getOptions()
707 .stream()
708 .filter(opt -> opt instanceof Dhcp6IaTaOption)
709 .map(opt -> (Dhcp6IaTaOption) opt)
710 .findFirst();
711 Optional<Dhcp6IaAddressOption> iaAddressOption;
712 if (iaNaOption.isPresent()) {
713 iaAddressOption = iaNaOption.get().getOptions().stream()
714 .filter(opt -> opt instanceof Dhcp6IaAddressOption)
715 .map(opt -> (Dhcp6IaAddressOption) opt)
716 .findFirst();
717 } else if (iaTaOption.isPresent()) {
718 iaAddressOption = iaTaOption.get().getOptions().stream()
719 .filter(opt -> opt instanceof Dhcp6IaAddressOption)
720 .map(opt -> (Dhcp6IaAddressOption) opt)
721 .findFirst();
722 } else {
723 iaAddressOption = Optional.empty();
724 }
725 if (iaAddressOption.isPresent()) {
726 Ip6Address ip = iaAddressOption.get().getIp6Address();
727 HostId hostId = HostId.hostId(clientMac, vlanId);
728 updateHostIp(hostId, ip);
729 } else {
730 log.warn("Can't find IPv6 address from DHCPv6 {}", dhcp6);
731 }
732 }
733
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700734 private Optional<DHCP> findDhcp(Ethernet eth) {
735 IPacket pkt = eth.getPayload();
736 return Stream.of(pkt)
737 .filter(Objects::nonNull)
738 .filter(p -> p instanceof IPv4)
739 .map(IPacket::getPayload)
740 .filter(Objects::nonNull)
741 .filter(p -> p instanceof UDP)
742 .map(IPacket::getPayload)
743 .filter(Objects::nonNull)
744 .filter(p -> p instanceof DHCP)
745 .map(p -> (DHCP) p)
746 .findFirst();
747 }
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700748
749 private Optional<DHCP6> findDhcp6(IPacket pkt) {
750 return Stream.of(pkt)
751 .filter(Objects::nonNull)
752 .filter(p -> p instanceof UDP)
753 .map(IPacket::getPayload)
754 .filter(Objects::nonNull)
755 .filter(p -> p instanceof DHCP6)
756 .map(p -> (DHCP6) p)
757 .findFirst();
758 }
alshabibe1cf87d2014-10-17 09:23:50 -0700759 }
Thomas Vachuska33601602014-11-19 03:32:15 -0800760
761 // Auxiliary listener to device events.
762 private class InternalDeviceListener implements DeviceListener {
763 @Override
764 public void event(DeviceEvent event) {
Madan Jampania3770c32015-12-11 12:07:41 -0800765 eventHandler.execute(() -> handleEvent(event));
766 }
767
768 private void handleEvent(DeviceEvent event) {
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800769 Device device = event.subject();
770 switch (event.type()) {
Thomas Vachuska27bee092015-06-23 19:03:10 -0700771 case DEVICE_ADDED:
772 break;
773 case DEVICE_AVAILABILITY_CHANGED:
Charles Chan888e20a2017-05-01 15:44:23 -0700774 if (hostRemovalEnabled && !deviceService.isAvailable(device.id())) {
775 processDeviceDown(device.id());
Thomas Vachuska27bee092015-06-23 19:03:10 -0700776 }
777 break;
778 case DEVICE_SUSPENDED:
779 case DEVICE_UPDATED:
780 // Nothing to do?
781 break;
782 case DEVICE_REMOVED:
783 if (hostRemovalEnabled) {
Charles Chan888e20a2017-05-01 15:44:23 -0700784 processDeviceDown(device.id());
Thomas Vachuska27bee092015-06-23 19:03:10 -0700785 }
786 break;
787 case PORT_ADDED:
788 break;
789 case PORT_UPDATED:
Charles Chan888e20a2017-05-01 15:44:23 -0700790 if (hostRemovalEnabled && !event.port().isEnabled()) {
791 processPortDown(new ConnectPoint(device.id(), event.port().number()));
Thomas Vachuska27bee092015-06-23 19:03:10 -0700792 }
793 break;
794 case PORT_REMOVED:
795 // Nothing to do?
796 break;
797 default:
798 break;
Thomas Vachuska33601602014-11-19 03:32:15 -0800799 }
800 }
801 }
802
Charles Chan888e20a2017-05-01 15:44:23 -0700803 /**
804 * When a device goes down, update the location of affected hosts.
805 *
806 * @param deviceId the device that goes down
807 */
808 private void processDeviceDown(DeviceId deviceId) {
809 hostService.getConnectedHosts(deviceId).forEach(affectedHost -> affectedHost.locations().stream()
810 .filter(hostLocation -> hostLocation.deviceId().equals(deviceId))
811 .forEach(affectedLocation ->
812 providerService.removeLocationFromHost(affectedHost.id(), affectedLocation))
813 );
814 }
815
816 /**
817 * When a port goes down, update the location of affected hosts.
818 *
819 * @param connectPoint the port that goes down
820 */
821 private void processPortDown(ConnectPoint connectPoint) {
822 hostService.getConnectedHosts(connectPoint).forEach(affectedHost ->
823 providerService.removeLocationFromHost(affectedHost.id(), new HostLocation(connectPoint, 0L))
824 );
Thomas Vachuska33601602014-11-19 03:32:15 -0800825 }
826
alshabibe1cf87d2014-10-17 09:23:50 -0700827}