blob: 996bd431dc362b2ecbe5e2592385960cda9b10b8 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
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
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
Thomas Vachuska33601602014-11-19 03:32:15 -080021import org.apache.felix.scr.annotations.Modified;
22import org.apache.felix.scr.annotations.Property;
alshabibe1cf87d2014-10-17 09:23:50 -070023import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Harte8600eb2015-01-12 10:30:45 -080025import org.onlab.packet.ARP;
Charles Chane6067892016-11-17 10:23:46 -080026import org.onlab.packet.DHCP;
Jonathan Harte8600eb2015-01-12 10:30:45 -080027import org.onlab.packet.Ethernet;
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -080028import org.onlab.packet.ICMP6;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070029import org.onlab.packet.IPacket;
Charles Chane6067892016-11-17 10:23:46 -080030import org.onlab.packet.IPv4;
Jonathan Harte8600eb2015-01-12 10:30:45 -080031import org.onlab.packet.IPv6;
32import org.onlab.packet.IpAddress;
Charles M.C. Chan956cb692015-04-26 18:49:39 +080033import org.onlab.packet.MacAddress;
Charles Chane6067892016-11-17 10:23:46 -080034import org.onlab.packet.TpPort;
35import org.onlab.packet.UDP;
Jonathan Harte8600eb2015-01-12 10:30:45 -080036import org.onlab.packet.VlanId;
Charles M.C. Chan956cb692015-04-26 18:49:39 +080037import org.onlab.packet.ipv6.IExtensionHeader;
Jonathan Harte8600eb2015-01-12 10:30:45 -080038import org.onlab.packet.ndp.NeighborAdvertisement;
39import org.onlab.packet.ndp.NeighborSolicitation;
Charles M.C. Chan441d7da2015-03-17 21:03:39 +080040import org.onlab.packet.ndp.RouterAdvertisement;
41import org.onlab.packet.ndp.RouterSolicitation;
Jian Lid9b5f552016-03-11 18:15:31 -080042import org.onlab.util.Tools;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070043import org.onosproject.cfg.ComponentConfigService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080044import org.onosproject.core.ApplicationId;
45import org.onosproject.core.CoreService;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import org.onosproject.net.ConnectPoint;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080047import org.onosproject.net.Device;
Charles Chan888e20a2017-05-01 15:44:23 -070048import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import org.onosproject.net.Host;
50import org.onosproject.net.HostId;
51import org.onosproject.net.HostLocation;
52import org.onosproject.net.device.DeviceEvent;
53import org.onosproject.net.device.DeviceListener;
54import org.onosproject.net.device.DeviceService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080055import org.onosproject.net.flow.DefaultTrafficSelector;
sdn94b00152016-08-30 02:12:32 -070056import org.onosproject.net.flow.DefaultTrafficTreatment;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080057import org.onosproject.net.flow.TrafficSelector;
sdn94b00152016-08-30 02:12:32 -070058import org.onosproject.net.flow.TrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080059import org.onosproject.net.host.DefaultHostDescription;
60import org.onosproject.net.host.HostDescription;
61import org.onosproject.net.host.HostProvider;
62import org.onosproject.net.host.HostProviderRegistry;
63import org.onosproject.net.host.HostProviderService;
64import org.onosproject.net.host.HostService;
sdn94b00152016-08-30 02:12:32 -070065import org.onosproject.net.packet.DefaultOutboundPacket;
66import org.onosproject.net.packet.OutboundPacket;
Brian O'Connorabafb502014-12-02 22:26:20 -080067import org.onosproject.net.packet.PacketContext;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080068import org.onosproject.net.packet.PacketPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080069import org.onosproject.net.packet.PacketProcessor;
70import org.onosproject.net.packet.PacketService;
71import org.onosproject.net.provider.AbstractProvider;
72import org.onosproject.net.provider.ProviderId;
73import org.onosproject.net.topology.Topology;
74import org.onosproject.net.topology.TopologyService;
Thomas Vachuska33601602014-11-19 03:32:15 -080075import org.osgi.service.component.ComponentContext;
alshabibe1cf87d2014-10-17 09:23:50 -070076import org.slf4j.Logger;
77
sdn94b00152016-08-30 02:12:32 -070078import java.nio.ByteBuffer;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070079import java.util.Dictionary;
Madan Jampania3770c32015-12-11 12:07:41 -080080import java.util.concurrent.ExecutorService;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070081
Madan Jampania3770c32015-12-11 12:07:41 -080082import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
83import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070084import static org.slf4j.LoggerFactory.getLogger;
85
alshabibe1cf87d2014-10-17 09:23:50 -070086/**
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -070087 * Provider which uses an OpenFlow controller to detect network end-station
88 * hosts.
alshabibe1cf87d2014-10-17 09:23:50 -070089 */
90@Component(immediate = true)
91public class HostLocationProvider extends AbstractProvider implements HostProvider {
92
93 private final Logger log = getLogger(getClass());
94
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080095 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 protected CoreService coreService;
97
98 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibe1cf87d2014-10-17 09:23:50 -070099 protected HostProviderRegistry providerRegistry;
100
101 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800102 protected PacketService packetService;
alshabibe1cf87d2014-10-17 09:23:50 -0700103
104 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
105 protected TopologyService topologyService;
106
Thomas Vachuska33601602014-11-19 03:32:15 -0800107 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
108 protected HostService hostService;
109
110 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
111 protected DeviceService deviceService;
112
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700113 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
114 protected ComponentConfigService cfgService;
115
alshabibe1cf87d2014-10-17 09:23:50 -0700116 private HostProviderService providerService;
117
118 private final InternalHostProvider processor = new InternalHostProvider();
Thomas Vachuska33601602014-11-19 03:32:15 -0800119 private final DeviceListener deviceListener = new InternalDeviceListener();
120
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800121 private ApplicationId appId;
122
Thomas Vachuska33601602014-11-19 03:32:15 -0800123 @Property(name = "hostRemovalEnabled", boolValue = true,
124 label = "Enable host removal on port/device down events")
125 private boolean hostRemovalEnabled = true;
alshabibe1cf87d2014-10-17 09:23:50 -0700126
Charles Chane6067892016-11-17 10:23:46 -0800127 @Property(name = "useArp", boolValue = true,
128 label = "Enable using ARP for neighbor discovery by the " +
129 "Host Location Provider; default is true")
130 private boolean useArp = true;
131
132 @Property(name = "useIpv6ND", boolValue = false,
Thomas Vachuska27bee092015-06-23 19:03:10 -0700133 label = "Enable using IPv6 Neighbor Discovery by the " +
134 "Host Location Provider; default is false")
Charles Chane6067892016-11-17 10:23:46 -0800135 private boolean useIpv6ND = false;
136
137 @Property(name = "useDhcp", boolValue = false,
138 label = "Enable using DHCP for neighbor discovery by the " +
139 "Host Location Provider; default is false")
140 private boolean useDhcp = false;
alshabibe1cf87d2014-10-17 09:23:50 -0700141
Jonathan Hart9af322d2016-01-06 17:42:04 -0800142 @Property(name = "requestInterceptsEnabled", boolValue = true,
143 label = "Enable requesting packet intercepts")
144 private boolean requestInterceptsEnabled = true;
145
Madan Jampania3770c32015-12-11 12:07:41 -0800146 protected ExecutorService eventHandler;
147
sdn94b00152016-08-30 02:12:32 -0700148 private static final byte[] SENDER_ADDRESS = IpAddress.valueOf("0.0.0.0").toOctets();
149
alshabibe1cf87d2014-10-17 09:23:50 -0700150 /**
151 * Creates an OpenFlow host provider.
152 */
153 public HostLocationProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800154 super(new ProviderId("of", "org.onosproject.provider.host"));
alshabibe1cf87d2014-10-17 09:23:50 -0700155 }
156
157 @Activate
Thomas Vachuska33601602014-11-19 03:32:15 -0800158 public void activate(ComponentContext context) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700159 cfgService.registerProperties(getClass());
160 appId = coreService.registerApplication("org.onosproject.provider.host");
Jonathan Hart9af322d2016-01-06 17:42:04 -0800161 eventHandler = newSingleThreadScheduledExecutor(
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700162 groupedThreads("onos/host-loc-provider", "event-handler", log));
alshabibe1cf87d2014-10-17 09:23:50 -0700163 providerService = providerRegistry.register(this);
Brian O'Connor3b783262015-07-29 17:49:24 -0700164 packetService.addProcessor(processor, PacketProcessor.advisor(1));
Thomas Vachuska33601602014-11-19 03:32:15 -0800165 deviceService.addListener(deviceListener);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800166
167 modified(context);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800168
Charles M.C. Chane148de82015-05-06 12:38:21 +0800169 log.info("Started with Application ID {}", appId.id());
170 }
171
172 @Deactivate
173 public void deactivate() {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800174 cfgService.unregisterProperties(getClass(), false);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700175
176 withdrawIntercepts();
177
Charles M.C. Chane148de82015-05-06 12:38:21 +0800178 providerRegistry.unregister(this);
179 packetService.removeProcessor(processor);
180 deviceService.removeListener(deviceListener);
Madan Jampania3770c32015-12-11 12:07:41 -0800181 eventHandler.shutdown();
Charles M.C. Chane148de82015-05-06 12:38:21 +0800182 providerService = null;
183 log.info("Stopped");
184 }
185
186 @Modified
187 public void modified(ComponentContext context) {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800188 readComponentConfiguration(context);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800189
190 if (requestInterceptsEnabled) {
191 requestIntercepts();
192 } else {
193 withdrawIntercepts();
194 }
Charles M.C. Chane148de82015-05-06 12:38:21 +0800195 }
196
197 /**
Thomas Vachuska27bee092015-06-23 19:03:10 -0700198 * Request packet intercepts.
Charles M.C. Chane148de82015-05-06 12:38:21 +0800199 */
Thomas Vachuska27bee092015-06-23 19:03:10 -0700200 private void requestIntercepts() {
Charles Chane6067892016-11-17 10:23:46 -0800201 // Use ARP
202 TrafficSelector arpSelector = DefaultTrafficSelector.builder()
203 .matchEthType(Ethernet.TYPE_ARP)
204 .build();
205 if (useArp) {
206 packetService.requestPackets(arpSelector, PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700207 } else {
Charles Chane6067892016-11-17 10:23:46 -0800208 packetService.cancelPackets(arpSelector, PacketPriority.CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800209 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700210
Charles Chane6067892016-11-17 10:23:46 -0800211 // Use IPv6 Neighbor Discovery
212 TrafficSelector ipv6NsSelector = DefaultTrafficSelector.builder()
213 .matchEthType(Ethernet.TYPE_IPV6)
214 .matchIPProtocol(IPv6.PROTOCOL_ICMP6)
215 .matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION)
216 .build();
217 TrafficSelector ipv6NaSelector = DefaultTrafficSelector.builder()
218 .matchEthType(Ethernet.TYPE_IPV6)
219 .matchIPProtocol(IPv6.PROTOCOL_ICMP6)
220 .matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT)
221 .build();
222 if (useIpv6ND) {
223 packetService.requestPackets(ipv6NsSelector, PacketPriority.CONTROL, appId);
224 packetService.requestPackets(ipv6NaSelector, PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700225 } else {
Charles Chane6067892016-11-17 10:23:46 -0800226 packetService.cancelPackets(ipv6NsSelector, PacketPriority.CONTROL, appId);
227 packetService.cancelPackets(ipv6NaSelector, PacketPriority.CONTROL, appId);
228 }
229
230 // Use DHCP
231 TrafficSelector dhcpServerSelector = DefaultTrafficSelector.builder()
232 .matchEthType(Ethernet.TYPE_IPV4)
233 .matchIPProtocol(IPv4.PROTOCOL_UDP)
234 .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT))
235 .build();
236 TrafficSelector dhcpClientSelector = DefaultTrafficSelector.builder()
237 .matchEthType(Ethernet.TYPE_IPV4)
238 .matchIPProtocol(IPv4.PROTOCOL_UDP)
239 .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT))
240 .build();
241 if (useDhcp) {
242 packetService.requestPackets(dhcpServerSelector, PacketPriority.CONTROL, appId);
243 packetService.requestPackets(dhcpClientSelector, PacketPriority.CONTROL, appId);
244 } else {
245 packetService.cancelPackets(dhcpServerSelector, PacketPriority.CONTROL, appId);
246 packetService.cancelPackets(dhcpClientSelector, PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700247 }
248 }
249
250 /**
251 * Withdraw packet intercepts.
252 */
253 private void withdrawIntercepts() {
254 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
255 selector.matchEthType(Ethernet.TYPE_ARP);
Aaron Kruglikov07a923d2015-07-03 13:30:57 -0700256 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700257
258 // IPv6 Neighbor Solicitation packet.
259 selector.matchEthType(Ethernet.TYPE_IPV6);
260 selector.matchIPProtocol(IPv6.PROTOCOL_ICMP6);
261 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION);
262 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
263
264 // IPv6 Neighbor Advertisement packet.
265 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT);
266 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800267 }
268
269 /**
270 * Extracts properties from the component configuration context.
271 *
272 * @param context the component context
273 */
274 private void readComponentConfiguration(ComponentContext context) {
275 Dictionary<?, ?> properties = context.getProperties();
276 Boolean flag;
277
Jian Lid9b5f552016-03-11 18:15:31 -0800278 flag = Tools.isPropertyEnabled(properties, "hostRemovalEnabled");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800279 if (flag == null) {
280 log.info("Host removal on port/device down events is not configured, " +
Thomas Vachuska27bee092015-06-23 19:03:10 -0700281 "using current value of {}", hostRemovalEnabled);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800282 } else {
283 hostRemovalEnabled = flag;
284 log.info("Configured. Host removal on port/device down events is {}",
285 hostRemovalEnabled ? "enabled" : "disabled");
Thomas Vachuska33601602014-11-19 03:32:15 -0800286 }
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800287
Charles Chane6067892016-11-17 10:23:46 -0800288 flag = Tools.isPropertyEnabled(properties, "useArp");
289 if (flag == null) {
290 log.info("Using ARP is not configured, " +
291 "using current value of {}", useArp);
292 } else {
293 useArp = flag;
294 log.info("Configured. Using ARP is {}",
295 useArp ? "enabled" : "disabled");
296 }
297
298 flag = Tools.isPropertyEnabled(properties, "useIpv6ND");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800299 if (flag == null) {
300 log.info("Using IPv6 Neighbor Discovery is not configured, " +
Charles Chane6067892016-11-17 10:23:46 -0800301 "using current value of {}", useIpv6ND);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800302 } else {
Charles Chane6067892016-11-17 10:23:46 -0800303 useIpv6ND = flag;
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800304 log.info("Configured. Using IPv6 Neighbor Discovery is {}",
Charles Chane6067892016-11-17 10:23:46 -0800305 useIpv6ND ? "enabled" : "disabled");
306 }
307
308 flag = Tools.isPropertyEnabled(properties, "useDhcp");
309 if (flag == null) {
310 log.info("Using DHCP is not configured, " +
311 "using current value of {}", useDhcp);
312 } else {
313 useDhcp = flag;
314 log.info("Configured. Using DHCP is {}",
315 useDhcp ? "enabled" : "disabled");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800316 }
Jonathan Hart9af322d2016-01-06 17:42:04 -0800317
Jian Lid9b5f552016-03-11 18:15:31 -0800318 flag = Tools.isPropertyEnabled(properties, "requestInterceptsEnabled");
Jonathan Hart9af322d2016-01-06 17:42:04 -0800319 if (flag == null) {
320 log.info("Request intercepts is not configured, " +
321 "using current value of {}", requestInterceptsEnabled);
322 } else {
323 requestInterceptsEnabled = flag;
324 log.info("Configured. Request intercepts is {}",
325 requestInterceptsEnabled ? "enabled" : "disabled");
326 }
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800327 }
328
alshabibe1cf87d2014-10-17 09:23:50 -0700329 @Override
330 public void triggerProbe(Host host) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800331 //log.info("Triggering probe on device {} ", host);
Jonathan Hartf353e402016-09-23 09:27:36 -0700332
333 // FIXME Disabling host probing for now, because sending packets from a
334 // broadcast MAC address caused problems when two ONOS networks were
335 // interconnected. Host probing should take into account the interface
336 // configuration when determining which source address to use.
337
338 //MastershipRole role = deviceService.getRole(host.location().deviceId());
339 //if (role.equals(MastershipRole.MASTER)) {
340 // host.ipAddresses().forEach(ip -> {
341 // sendProbe(host, ip);
342 // });
343 //} else {
344 // log.info("not the master, master will probe {}");
345 //}
sdn94b00152016-08-30 02:12:32 -0700346 }
347
348 private void sendProbe(Host host, IpAddress targetIp) {
349 Ethernet probePacket = null;
350 if (targetIp.isIp4()) {
351 // IPv4: Use ARP
352 probePacket = buildArpRequest(targetIp, host);
353 } else {
354 // IPv6: Use Neighbor Discovery
Jonathan Hartf353e402016-09-23 09:27:36 -0700355 //TODO need to implement ndp probe
sdn94b00152016-08-30 02:12:32 -0700356 log.info("Triggering probe on device {} ", host);
357 }
358
359 TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(host.location().port()).build();
360
361 OutboundPacket outboundPacket = new DefaultOutboundPacket(host.location().deviceId(), treatment,
362 ByteBuffer.wrap(probePacket.serialize()));
363
364 packetService.emit(outboundPacket);
365 }
366
Charles Chan888e20a2017-05-01 15:44:23 -0700367 // 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 -0700368 private Ethernet buildArpRequest(IpAddress targetIp, Host host) {
369
370 ARP arp = new ARP();
371 arp.setHardwareType(ARP.HW_TYPE_ETHERNET)
372 .setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH)
373 .setProtocolType(ARP.PROTO_TYPE_IP)
374 .setProtocolAddressLength((byte) IpAddress.INET_BYTE_LENGTH)
375 .setOpCode(ARP.OP_REQUEST);
376
377 arp.setSenderHardwareAddress(MacAddress.BROADCAST.toBytes())
378 .setSenderProtocolAddress(SENDER_ADDRESS)
379 .setTargetHardwareAddress(MacAddress.BROADCAST.toBytes())
380 .setTargetProtocolAddress(targetIp.toOctets());
381
382 Ethernet ethernet = new Ethernet();
383 ethernet.setEtherType(Ethernet.TYPE_ARP)
384 .setDestinationMACAddress(MacAddress.BROADCAST)
385 .setSourceMACAddress(MacAddress.BROADCAST).setPayload(arp);
386
387 ethernet.setPad(true);
388 return ethernet;
alshabibe1cf87d2014-10-17 09:23:50 -0700389 }
390
391 private class InternalHostProvider implements PacketProcessor {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800392 /**
Charles Chan888e20a2017-05-01 15:44:23 -0700393 * Create or update host information.
394 * Will not update IP if IP is null, all zero or self-assigned.
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800395 *
Thomas Vachuska27bee092015-06-23 19:03:10 -0700396 * @param hid host ID
397 * @param mac source Mac address
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800398 * @param vlan VLAN ID
399 * @param hloc host location
Charles Chan888e20a2017-05-01 15:44:23 -0700400 * @param ip source IP address or null if not updating
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800401 */
Charles Chan888e20a2017-05-01 15:44:23 -0700402 private void createOrUpdateHost(HostId hid, MacAddress mac,
403 VlanId vlan, HostLocation hloc,
404 IpAddress ip) {
405 HostDescription desc = ip == null || ip.isZero() || ip.isSelfAssigned() ?
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700406 new DefaultHostDescription(mac, vlan, hloc) :
407 new DefaultHostDescription(mac, vlan, hloc, ip);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700408 try {
Ray Milkeydc083442016-02-22 11:27:57 -0800409 providerService.hostDetected(hid, desc, false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700410 } catch (IllegalStateException e) {
411 log.debug("Host {} suppressed", hid);
412 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800413 }
alshabibe1cf87d2014-10-17 09:23:50 -0700414
Charles Chane6067892016-11-17 10:23:46 -0800415 /**
Charles Chan888e20a2017-05-01 15:44:23 -0700416 * Updates IP address for an existing host.
Charles Chane6067892016-11-17 10:23:46 -0800417 *
418 * @param hid host ID
419 * @param ip IP address
420 */
Charles Chan888e20a2017-05-01 15:44:23 -0700421 private void updateHostIp(HostId hid, IpAddress ip) {
Charles Chane6067892016-11-17 10:23:46 -0800422 Host host = hostService.getHost(hid);
423 if (host == null) {
424 log.debug("Fail to update IP for {}. Host does not exist");
425 return;
426 }
427
Charles Chan888e20a2017-05-01 15:44:23 -0700428 HostDescription desc = new DefaultHostDescription(hid.mac(), hid.vlanId(),
429 host.location(), ip);
Charles Chane6067892016-11-17 10:23:46 -0800430 try {
431 providerService.hostDetected(hid, desc, false);
432 } catch (IllegalStateException e) {
433 log.debug("Host {} suppressed", hid);
434 }
435 }
436
alshabibe1cf87d2014-10-17 09:23:50 -0700437 @Override
438 public void process(PacketContext context) {
alshabib4a179dc2014-10-17 17:17:01 -0700439 if (context == null) {
440 return;
441 }
alshabibe1cf87d2014-10-17 09:23:50 -0700442
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700443 Ethernet eth = context.inPacket().parsed();
Jonathan Harte8600eb2015-01-12 10:30:45 -0800444 if (eth == null) {
445 return;
446 }
Jonathan Hartf353e402016-09-23 09:27:36 -0700447
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800448 MacAddress srcMac = eth.getSourceMAC();
Jonathan Hartf353e402016-09-23 09:27:36 -0700449 if (srcMac.isBroadcast() || srcMac.isMulticast()) {
450 return;
451 }
Jonathan Harte8600eb2015-01-12 10:30:45 -0800452
alshabibe1cf87d2014-10-17 09:23:50 -0700453 VlanId vlan = VlanId.vlanId(eth.getVlanID());
454 ConnectPoint heardOn = context.inPacket().receivedFrom();
455
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700456 // If this arrived on control port, bail out.
457 if (heardOn.port().isLogical()) {
458 return;
459 }
460
alshabibe1cf87d2014-10-17 09:23:50 -0700461 // If this is not an edge port, bail out.
462 Topology topology = topologyService.currentTopology();
463 if (topologyService.isInfrastructure(topology, heardOn)) {
464 return;
465 }
466
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700467 HostLocation hloc = new HostLocation(heardOn, System.currentTimeMillis());
alshabibe1cf87d2014-10-17 09:23:50 -0700468 HostId hid = HostId.hostId(eth.getSourceMAC(), vlan);
469
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800470 // ARP: possible new hosts, update both location and IP
alshabibe1cf87d2014-10-17 09:23:50 -0700471 if (eth.getEtherType() == Ethernet.TYPE_ARP) {
472 ARP arp = (ARP) eth.getPayload();
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800473 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET,
474 arp.getSenderProtocolAddress());
Charles Chan888e20a2017-05-01 15:44:23 -0700475 createOrUpdateHost(hid, srcMac, vlan, hloc, ip);
alshabibe1cf87d2014-10-17 09:23:50 -0700476
Charles Chan5d5e0622015-09-25 13:00:06 -0700477 // IPv4: update location only
Charles Chane6067892016-11-17 10:23:46 -0800478 // DHCP ACK: additionally update IP of DHCP client
alshabibe1cf87d2014-10-17 09:23:50 -0700479 } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) {
Charles Chane6067892016-11-17 10:23:46 -0800480 IPacket pkt = eth.getPayload();
481 if (pkt != null && pkt instanceof IPv4) {
482 pkt = pkt.getPayload();
483 if (pkt != null && pkt instanceof UDP) {
484 pkt = pkt.getPayload();
485 if (pkt != null && pkt instanceof DHCP) {
486 DHCP dhcp = (DHCP) pkt;
487 if (dhcp.getOptions().stream()
488 .anyMatch(dhcpOption -> dhcpOption.getCode() ==
489 DHCP.DHCPOptionCode.OptionCode_MessageType.getValue() &&
490 dhcpOption.getLength() == 1 &&
Yi Tsengc7403c22017-06-19 16:23:22 -0700491 dhcpOption.getData()[0] == DHCP.MsgType.DHCPACK.getValue())) {
Charles Chane6067892016-11-17 10:23:46 -0800492 MacAddress hostMac = MacAddress.valueOf(dhcp.getClientHardwareAddress());
493 VlanId hostVlan = VlanId.vlanId(eth.getVlanID());
494 HostId hostId = HostId.hostId(hostMac, hostVlan);
Charles Chan888e20a2017-05-01 15:44:23 -0700495 updateHostIp(hostId, IpAddress.valueOf(dhcp.getYourIPAddress()));
Charles Chane6067892016-11-17 10:23:46 -0800496 }
497 }
498 }
499 }
Charles Chan888e20a2017-05-01 15:44:23 -0700500 createOrUpdateHost(hid, srcMac, vlan, hloc, null);
alshabibe1cf87d2014-10-17 09:23:50 -0700501
Charles Chan5d5e0622015-09-25 13:00:06 -0700502 //
503 // NeighborAdvertisement and NeighborSolicitation: possible
504 // new hosts, update both location and IP.
505 //
506 // IPv6: update location only
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800507 } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800508 IPv6 ipv6 = (IPv6) eth.getPayload();
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800509 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET6,
Thomas Vachuska27bee092015-06-23 19:03:10 -0700510 ipv6.getSourceAddress());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800511
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800512 // skip extension headers
513 IPacket pkt = ipv6;
514 while (pkt.getPayload() != null &&
515 pkt.getPayload() instanceof IExtensionHeader) {
516 pkt = pkt.getPayload();
517 }
518
519 // Neighbor Discovery Protocol
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800520 pkt = pkt.getPayload();
521 if (pkt != null && pkt instanceof ICMP6) {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800522 pkt = pkt.getPayload();
523 // RouterSolicitation, RouterAdvertisement
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800524 if (pkt != null && (pkt instanceof RouterAdvertisement ||
Thomas Vachuska27bee092015-06-23 19:03:10 -0700525 pkt instanceof RouterSolicitation)) {
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800526 return;
527 }
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800528 if (pkt != null && (pkt instanceof NeighborSolicitation ||
Thomas Vachuska27bee092015-06-23 19:03:10 -0700529 pkt instanceof NeighborAdvertisement)) {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800530 // Duplicate Address Detection
531 if (ip.isZero()) {
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800532 return;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800533 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800534 // NeighborSolicitation, NeighborAdvertisement
Charles Chan888e20a2017-05-01 15:44:23 -0700535 createOrUpdateHost(hid, srcMac, vlan, hloc, ip);
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800536 return;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800537 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800538 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800539
540 // multicast
541 if (eth.isMulticast()) {
542 return;
543 }
544
545 // normal IPv6 packets
Charles Chan888e20a2017-05-01 15:44:23 -0700546 createOrUpdateHost(hid, srcMac, vlan, hloc, null);
alshabibe1cf87d2014-10-17 09:23:50 -0700547 }
548 }
549 }
Thomas Vachuska33601602014-11-19 03:32:15 -0800550
551 // Auxiliary listener to device events.
552 private class InternalDeviceListener implements DeviceListener {
553 @Override
554 public void event(DeviceEvent event) {
Madan Jampania3770c32015-12-11 12:07:41 -0800555 eventHandler.execute(() -> handleEvent(event));
556 }
557
558 private void handleEvent(DeviceEvent event) {
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800559 Device device = event.subject();
560 switch (event.type()) {
Thomas Vachuska27bee092015-06-23 19:03:10 -0700561 case DEVICE_ADDED:
562 break;
563 case DEVICE_AVAILABILITY_CHANGED:
Charles Chan888e20a2017-05-01 15:44:23 -0700564 if (hostRemovalEnabled && !deviceService.isAvailable(device.id())) {
565 processDeviceDown(device.id());
Thomas Vachuska27bee092015-06-23 19:03:10 -0700566 }
567 break;
568 case DEVICE_SUSPENDED:
569 case DEVICE_UPDATED:
570 // Nothing to do?
571 break;
572 case DEVICE_REMOVED:
573 if (hostRemovalEnabled) {
Charles Chan888e20a2017-05-01 15:44:23 -0700574 processDeviceDown(device.id());
Thomas Vachuska27bee092015-06-23 19:03:10 -0700575 }
576 break;
577 case PORT_ADDED:
578 break;
579 case PORT_UPDATED:
Charles Chan888e20a2017-05-01 15:44:23 -0700580 if (hostRemovalEnabled && !event.port().isEnabled()) {
581 processPortDown(new ConnectPoint(device.id(), event.port().number()));
Thomas Vachuska27bee092015-06-23 19:03:10 -0700582 }
583 break;
584 case PORT_REMOVED:
585 // Nothing to do?
586 break;
587 default:
588 break;
Thomas Vachuska33601602014-11-19 03:32:15 -0800589 }
590 }
591 }
592
Charles Chan888e20a2017-05-01 15:44:23 -0700593 /**
594 * When a device goes down, update the location of affected hosts.
595 *
596 * @param deviceId the device that goes down
597 */
598 private void processDeviceDown(DeviceId deviceId) {
599 hostService.getConnectedHosts(deviceId).forEach(affectedHost -> affectedHost.locations().stream()
600 .filter(hostLocation -> hostLocation.deviceId().equals(deviceId))
601 .forEach(affectedLocation ->
602 providerService.removeLocationFromHost(affectedHost.id(), affectedLocation))
603 );
604 }
605
606 /**
607 * When a port goes down, update the location of affected hosts.
608 *
609 * @param connectPoint the port that goes down
610 */
611 private void processPortDown(ConnectPoint connectPoint) {
612 hostService.getConnectedHosts(connectPoint).forEach(affectedHost ->
613 providerService.removeLocationFromHost(affectedHost.id(), new HostLocation(connectPoint, 0L))
614 );
Thomas Vachuska33601602014-11-19 03:32:15 -0800615 }
616
alshabibe1cf87d2014-10-17 09:23:50 -0700617}