blob: 65472dd844dbdd41cc69078af2ff38f6720b34b7 [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;
27import org.onlab.packet.DHCPPacketType;
Jonathan Harte8600eb2015-01-12 10:30:45 -080028import org.onlab.packet.Ethernet;
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -080029import org.onlab.packet.ICMP6;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070030import org.onlab.packet.IPacket;
Charles Chane6067892016-11-17 10:23:46 -080031import org.onlab.packet.IPv4;
Jonathan Harte8600eb2015-01-12 10:30:45 -080032import org.onlab.packet.IPv6;
33import org.onlab.packet.IpAddress;
Charles M.C. Chan956cb692015-04-26 18:49:39 +080034import org.onlab.packet.MacAddress;
Charles Chane6067892016-11-17 10:23:46 -080035import org.onlab.packet.TpPort;
36import org.onlab.packet.UDP;
Jonathan Harte8600eb2015-01-12 10:30:45 -080037import org.onlab.packet.VlanId;
Charles M.C. Chan956cb692015-04-26 18:49:39 +080038import org.onlab.packet.ipv6.IExtensionHeader;
Jonathan Harte8600eb2015-01-12 10:30:45 -080039import org.onlab.packet.ndp.NeighborAdvertisement;
40import org.onlab.packet.ndp.NeighborSolicitation;
Charles M.C. Chan441d7da2015-03-17 21:03:39 +080041import org.onlab.packet.ndp.RouterAdvertisement;
42import org.onlab.packet.ndp.RouterSolicitation;
Jian Lid9b5f552016-03-11 18:15:31 -080043import org.onlab.util.Tools;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070044import org.onosproject.cfg.ComponentConfigService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080045import org.onosproject.core.ApplicationId;
46import org.onosproject.core.CoreService;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import org.onosproject.net.ConnectPoint;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080048import org.onosproject.net.Device;
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;
80import java.util.Set;
Madan Jampania3770c32015-12-11 12:07:41 -080081import java.util.concurrent.ExecutorService;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070082
Madan Jampania3770c32015-12-11 12:07:41 -080083import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
84import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070085import static org.slf4j.LoggerFactory.getLogger;
86
alshabibe1cf87d2014-10-17 09:23:50 -070087/**
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -070088 * Provider which uses an OpenFlow controller to detect network end-station
89 * hosts.
alshabibe1cf87d2014-10-17 09:23:50 -070090 */
91@Component(immediate = true)
92public class HostLocationProvider extends AbstractProvider implements HostProvider {
93
94 private final Logger log = getLogger(getClass());
95
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080096 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
97 protected CoreService coreService;
98
99 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibe1cf87d2014-10-17 09:23:50 -0700100 protected HostProviderRegistry providerRegistry;
101
102 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800103 protected PacketService packetService;
alshabibe1cf87d2014-10-17 09:23:50 -0700104
105 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
106 protected TopologyService topologyService;
107
Thomas Vachuska33601602014-11-19 03:32:15 -0800108 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
109 protected HostService hostService;
110
111 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
112 protected DeviceService deviceService;
113
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700114 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
115 protected ComponentConfigService cfgService;
116
alshabibe1cf87d2014-10-17 09:23:50 -0700117 private HostProviderService providerService;
118
119 private final InternalHostProvider processor = new InternalHostProvider();
Thomas Vachuska33601602014-11-19 03:32:15 -0800120 private final DeviceListener deviceListener = new InternalDeviceListener();
121
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800122 private ApplicationId appId;
123
Thomas Vachuska33601602014-11-19 03:32:15 -0800124 @Property(name = "hostRemovalEnabled", boolValue = true,
125 label = "Enable host removal on port/device down events")
126 private boolean hostRemovalEnabled = true;
alshabibe1cf87d2014-10-17 09:23:50 -0700127
Charles Chane6067892016-11-17 10:23:46 -0800128 @Property(name = "useArp", boolValue = true,
129 label = "Enable using ARP for neighbor discovery by the " +
130 "Host Location Provider; default is true")
131 private boolean useArp = true;
132
133 @Property(name = "useIpv6ND", boolValue = false,
Thomas Vachuska27bee092015-06-23 19:03:10 -0700134 label = "Enable using IPv6 Neighbor Discovery by the " +
135 "Host Location Provider; default is false")
Charles Chane6067892016-11-17 10:23:46 -0800136 private boolean useIpv6ND = false;
137
138 @Property(name = "useDhcp", boolValue = false,
139 label = "Enable using DHCP for neighbor discovery by the " +
140 "Host Location Provider; default is false")
141 private boolean useDhcp = false;
alshabibe1cf87d2014-10-17 09:23:50 -0700142
Jonathan Hart9af322d2016-01-06 17:42:04 -0800143 @Property(name = "requestInterceptsEnabled", boolValue = true,
144 label = "Enable requesting packet intercepts")
145 private boolean requestInterceptsEnabled = true;
146
Madan Jampania3770c32015-12-11 12:07:41 -0800147 protected ExecutorService eventHandler;
148
sdn94b00152016-08-30 02:12:32 -0700149 private static final byte[] SENDER_ADDRESS = IpAddress.valueOf("0.0.0.0").toOctets();
150
alshabibe1cf87d2014-10-17 09:23:50 -0700151 /**
152 * Creates an OpenFlow host provider.
153 */
154 public HostLocationProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800155 super(new ProviderId("of", "org.onosproject.provider.host"));
alshabibe1cf87d2014-10-17 09:23:50 -0700156 }
157
158 @Activate
Thomas Vachuska33601602014-11-19 03:32:15 -0800159 public void activate(ComponentContext context) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700160 cfgService.registerProperties(getClass());
161 appId = coreService.registerApplication("org.onosproject.provider.host");
Jonathan Hart9af322d2016-01-06 17:42:04 -0800162 eventHandler = newSingleThreadScheduledExecutor(
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700163 groupedThreads("onos/host-loc-provider", "event-handler", log));
alshabibe1cf87d2014-10-17 09:23:50 -0700164 providerService = providerRegistry.register(this);
Brian O'Connor3b783262015-07-29 17:49:24 -0700165 packetService.addProcessor(processor, PacketProcessor.advisor(1));
Thomas Vachuska33601602014-11-19 03:32:15 -0800166 deviceService.addListener(deviceListener);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800167
168 modified(context);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800169
Charles M.C. Chane148de82015-05-06 12:38:21 +0800170 log.info("Started with Application ID {}", appId.id());
171 }
172
173 @Deactivate
174 public void deactivate() {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800175 cfgService.unregisterProperties(getClass(), false);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700176
177 withdrawIntercepts();
178
Charles M.C. Chane148de82015-05-06 12:38:21 +0800179 providerRegistry.unregister(this);
180 packetService.removeProcessor(processor);
181 deviceService.removeListener(deviceListener);
Madan Jampania3770c32015-12-11 12:07:41 -0800182 eventHandler.shutdown();
Charles M.C. Chane148de82015-05-06 12:38:21 +0800183 providerService = null;
184 log.info("Stopped");
185 }
186
187 @Modified
188 public void modified(ComponentContext context) {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800189 readComponentConfiguration(context);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800190
191 if (requestInterceptsEnabled) {
192 requestIntercepts();
193 } else {
194 withdrawIntercepts();
195 }
Charles M.C. Chane148de82015-05-06 12:38:21 +0800196 }
197
198 /**
Thomas Vachuska27bee092015-06-23 19:03:10 -0700199 * Request packet intercepts.
Charles M.C. Chane148de82015-05-06 12:38:21 +0800200 */
Thomas Vachuska27bee092015-06-23 19:03:10 -0700201 private void requestIntercepts() {
Charles Chane6067892016-11-17 10:23:46 -0800202 // Use ARP
203 TrafficSelector arpSelector = DefaultTrafficSelector.builder()
204 .matchEthType(Ethernet.TYPE_ARP)
205 .build();
206 if (useArp) {
207 packetService.requestPackets(arpSelector, PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700208 } else {
Charles Chane6067892016-11-17 10:23:46 -0800209 packetService.cancelPackets(arpSelector, PacketPriority.CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800210 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700211
Charles Chane6067892016-11-17 10:23:46 -0800212 // Use IPv6 Neighbor Discovery
213 TrafficSelector ipv6NsSelector = DefaultTrafficSelector.builder()
214 .matchEthType(Ethernet.TYPE_IPV6)
215 .matchIPProtocol(IPv6.PROTOCOL_ICMP6)
216 .matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION)
217 .build();
218 TrafficSelector ipv6NaSelector = DefaultTrafficSelector.builder()
219 .matchEthType(Ethernet.TYPE_IPV6)
220 .matchIPProtocol(IPv6.PROTOCOL_ICMP6)
221 .matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT)
222 .build();
223 if (useIpv6ND) {
224 packetService.requestPackets(ipv6NsSelector, PacketPriority.CONTROL, appId);
225 packetService.requestPackets(ipv6NaSelector, PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700226 } else {
Charles Chane6067892016-11-17 10:23:46 -0800227 packetService.cancelPackets(ipv6NsSelector, PacketPriority.CONTROL, appId);
228 packetService.cancelPackets(ipv6NaSelector, PacketPriority.CONTROL, appId);
229 }
230
231 // Use DHCP
232 TrafficSelector dhcpServerSelector = DefaultTrafficSelector.builder()
233 .matchEthType(Ethernet.TYPE_IPV4)
234 .matchIPProtocol(IPv4.PROTOCOL_UDP)
235 .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT))
236 .build();
237 TrafficSelector dhcpClientSelector = DefaultTrafficSelector.builder()
238 .matchEthType(Ethernet.TYPE_IPV4)
239 .matchIPProtocol(IPv4.PROTOCOL_UDP)
240 .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT))
241 .build();
242 if (useDhcp) {
243 packetService.requestPackets(dhcpServerSelector, PacketPriority.CONTROL, appId);
244 packetService.requestPackets(dhcpClientSelector, PacketPriority.CONTROL, appId);
245 } else {
246 packetService.cancelPackets(dhcpServerSelector, PacketPriority.CONTROL, appId);
247 packetService.cancelPackets(dhcpClientSelector, PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700248 }
249 }
250
251 /**
252 * Withdraw packet intercepts.
253 */
254 private void withdrawIntercepts() {
255 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
256 selector.matchEthType(Ethernet.TYPE_ARP);
Aaron Kruglikov07a923d2015-07-03 13:30:57 -0700257 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700258
259 // IPv6 Neighbor Solicitation packet.
260 selector.matchEthType(Ethernet.TYPE_IPV6);
261 selector.matchIPProtocol(IPv6.PROTOCOL_ICMP6);
262 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION);
263 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
264
265 // IPv6 Neighbor Advertisement packet.
266 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT);
267 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800268 }
269
270 /**
271 * Extracts properties from the component configuration context.
272 *
273 * @param context the component context
274 */
275 private void readComponentConfiguration(ComponentContext context) {
276 Dictionary<?, ?> properties = context.getProperties();
277 Boolean flag;
278
Jian Lid9b5f552016-03-11 18:15:31 -0800279 flag = Tools.isPropertyEnabled(properties, "hostRemovalEnabled");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800280 if (flag == null) {
281 log.info("Host removal on port/device down events is not configured, " +
Thomas Vachuska27bee092015-06-23 19:03:10 -0700282 "using current value of {}", hostRemovalEnabled);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800283 } else {
284 hostRemovalEnabled = flag;
285 log.info("Configured. Host removal on port/device down events is {}",
286 hostRemovalEnabled ? "enabled" : "disabled");
Thomas Vachuska33601602014-11-19 03:32:15 -0800287 }
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800288
Charles Chane6067892016-11-17 10:23:46 -0800289 flag = Tools.isPropertyEnabled(properties, "useArp");
290 if (flag == null) {
291 log.info("Using ARP is not configured, " +
292 "using current value of {}", useArp);
293 } else {
294 useArp = flag;
295 log.info("Configured. Using ARP is {}",
296 useArp ? "enabled" : "disabled");
297 }
298
299 flag = Tools.isPropertyEnabled(properties, "useIpv6ND");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800300 if (flag == null) {
301 log.info("Using IPv6 Neighbor Discovery is not configured, " +
Charles Chane6067892016-11-17 10:23:46 -0800302 "using current value of {}", useIpv6ND);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800303 } else {
Charles Chane6067892016-11-17 10:23:46 -0800304 useIpv6ND = flag;
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800305 log.info("Configured. Using IPv6 Neighbor Discovery is {}",
Charles Chane6067892016-11-17 10:23:46 -0800306 useIpv6ND ? "enabled" : "disabled");
307 }
308
309 flag = Tools.isPropertyEnabled(properties, "useDhcp");
310 if (flag == null) {
311 log.info("Using DHCP is not configured, " +
312 "using current value of {}", useDhcp);
313 } else {
314 useDhcp = flag;
315 log.info("Configured. Using DHCP is {}",
316 useDhcp ? "enabled" : "disabled");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800317 }
Jonathan Hart9af322d2016-01-06 17:42:04 -0800318
Jian Lid9b5f552016-03-11 18:15:31 -0800319 flag = Tools.isPropertyEnabled(properties, "requestInterceptsEnabled");
Jonathan Hart9af322d2016-01-06 17:42:04 -0800320 if (flag == null) {
321 log.info("Request intercepts is not configured, " +
322 "using current value of {}", requestInterceptsEnabled);
323 } else {
324 requestInterceptsEnabled = flag;
325 log.info("Configured. Request intercepts is {}",
326 requestInterceptsEnabled ? "enabled" : "disabled");
327 }
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800328 }
329
alshabibe1cf87d2014-10-17 09:23:50 -0700330 @Override
331 public void triggerProbe(Host host) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800332 //log.info("Triggering probe on device {} ", host);
Jonathan Hartf353e402016-09-23 09:27:36 -0700333
334 // FIXME Disabling host probing for now, because sending packets from a
335 // broadcast MAC address caused problems when two ONOS networks were
336 // interconnected. Host probing should take into account the interface
337 // configuration when determining which source address to use.
338
339 //MastershipRole role = deviceService.getRole(host.location().deviceId());
340 //if (role.equals(MastershipRole.MASTER)) {
341 // host.ipAddresses().forEach(ip -> {
342 // sendProbe(host, ip);
343 // });
344 //} else {
345 // log.info("not the master, master will probe {}");
346 //}
sdn94b00152016-08-30 02:12:32 -0700347 }
348
349 private void sendProbe(Host host, IpAddress targetIp) {
350 Ethernet probePacket = null;
351 if (targetIp.isIp4()) {
352 // IPv4: Use ARP
353 probePacket = buildArpRequest(targetIp, host);
354 } else {
355 // IPv6: Use Neighbor Discovery
Jonathan Hartf353e402016-09-23 09:27:36 -0700356 //TODO need to implement ndp probe
sdn94b00152016-08-30 02:12:32 -0700357 log.info("Triggering probe on device {} ", host);
358 }
359
360 TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(host.location().port()).build();
361
362 OutboundPacket outboundPacket = new DefaultOutboundPacket(host.location().deviceId(), treatment,
363 ByteBuffer.wrap(probePacket.serialize()));
364
365 packetService.emit(outboundPacket);
366 }
367
368 /*
369 * This method is using source ip as 0.0.0.0 , to receive the reply even from the sub net hosts.
370 */
371 private Ethernet buildArpRequest(IpAddress targetIp, Host host) {
372
373 ARP arp = new ARP();
374 arp.setHardwareType(ARP.HW_TYPE_ETHERNET)
375 .setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH)
376 .setProtocolType(ARP.PROTO_TYPE_IP)
377 .setProtocolAddressLength((byte) IpAddress.INET_BYTE_LENGTH)
378 .setOpCode(ARP.OP_REQUEST);
379
380 arp.setSenderHardwareAddress(MacAddress.BROADCAST.toBytes())
381 .setSenderProtocolAddress(SENDER_ADDRESS)
382 .setTargetHardwareAddress(MacAddress.BROADCAST.toBytes())
383 .setTargetProtocolAddress(targetIp.toOctets());
384
385 Ethernet ethernet = new Ethernet();
386 ethernet.setEtherType(Ethernet.TYPE_ARP)
387 .setDestinationMACAddress(MacAddress.BROADCAST)
388 .setSourceMACAddress(MacAddress.BROADCAST).setPayload(arp);
389
390 ethernet.setPad(true);
391 return ethernet;
alshabibe1cf87d2014-10-17 09:23:50 -0700392 }
393
394 private class InternalHostProvider implements PacketProcessor {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800395 /**
Charles Chane6067892016-11-17 10:23:46 -0800396 * Updates host location only.
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800397 *
Thomas Vachuska27bee092015-06-23 19:03:10 -0700398 * @param hid host ID
399 * @param mac source Mac address
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800400 * @param vlan VLAN ID
401 * @param hloc host location
402 */
403 private void updateLocation(HostId hid, MacAddress mac,
Thomas Vachuska27bee092015-06-23 19:03:10 -0700404 VlanId vlan, HostLocation hloc) {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800405 HostDescription desc = new DefaultHostDescription(mac, vlan, hloc);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700406 try {
Ray Milkeydc083442016-02-22 11:27:57 -0800407 providerService.hostDetected(hid, desc, false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700408 } catch (IllegalStateException e) {
409 log.debug("Host {} suppressed", hid);
410 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800411 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700412
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800413 /**
Charles Chane6067892016-11-17 10:23:46 -0800414 * Updates host location and IP address.
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800415 *
Thomas Vachuska27bee092015-06-23 19:03:10 -0700416 * @param hid host ID
417 * @param mac source Mac address
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800418 * @param vlan VLAN ID
419 * @param hloc host location
Thomas Vachuska27bee092015-06-23 19:03:10 -0700420 * @param ip source IP address
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800421 */
422 private void updateLocationIP(HostId hid, MacAddress mac,
423 VlanId vlan, HostLocation hloc,
424 IpAddress ip) {
Thomas Vachuskae7966102015-09-09 17:33:33 -0700425 HostDescription desc = ip.isZero() || ip.isSelfAssigned() ?
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700426 new DefaultHostDescription(mac, vlan, hloc) :
427 new DefaultHostDescription(mac, vlan, hloc, ip);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700428 try {
Ray Milkeydc083442016-02-22 11:27:57 -0800429 providerService.hostDetected(hid, desc, false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700430 } catch (IllegalStateException e) {
431 log.debug("Host {} suppressed", hid);
432 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800433 }
alshabibe1cf87d2014-10-17 09:23:50 -0700434
Charles Chane6067892016-11-17 10:23:46 -0800435 /**
436 * Updates host IP address for an existing host.
437 *
438 * @param hid host ID
439 * @param ip IP address
440 */
441 private void updateIp(HostId hid, IpAddress ip) {
442 Host host = hostService.getHost(hid);
443 if (host == null) {
444 log.debug("Fail to update IP for {}. Host does not exist");
445 return;
446 }
447
448 HostDescription desc =
449 new DefaultHostDescription(hid.mac(), hid.vlanId(), host.location(), ip);
450 try {
451 providerService.hostDetected(hid, desc, false);
452 } catch (IllegalStateException e) {
453 log.debug("Host {} suppressed", hid);
454 }
455 }
456
alshabibe1cf87d2014-10-17 09:23:50 -0700457 @Override
458 public void process(PacketContext context) {
alshabib4a179dc2014-10-17 17:17:01 -0700459 if (context == null) {
460 return;
461 }
alshabibe1cf87d2014-10-17 09:23:50 -0700462
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700463 Ethernet eth = context.inPacket().parsed();
Jonathan Harte8600eb2015-01-12 10:30:45 -0800464 if (eth == null) {
465 return;
466 }
Jonathan Hartf353e402016-09-23 09:27:36 -0700467
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800468 MacAddress srcMac = eth.getSourceMAC();
Jonathan Hartf353e402016-09-23 09:27:36 -0700469 if (srcMac.isBroadcast() || srcMac.isMulticast()) {
470 return;
471 }
Jonathan Harte8600eb2015-01-12 10:30:45 -0800472
alshabibe1cf87d2014-10-17 09:23:50 -0700473 VlanId vlan = VlanId.vlanId(eth.getVlanID());
474 ConnectPoint heardOn = context.inPacket().receivedFrom();
475
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700476 // If this arrived on control port, bail out.
477 if (heardOn.port().isLogical()) {
478 return;
479 }
480
alshabibe1cf87d2014-10-17 09:23:50 -0700481 // If this is not an edge port, bail out.
482 Topology topology = topologyService.currentTopology();
483 if (topologyService.isInfrastructure(topology, heardOn)) {
484 return;
485 }
486
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700487 HostLocation hloc = new HostLocation(heardOn, System.currentTimeMillis());
alshabibe1cf87d2014-10-17 09:23:50 -0700488 HostId hid = HostId.hostId(eth.getSourceMAC(), vlan);
489
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800490 // ARP: possible new hosts, update both location and IP
alshabibe1cf87d2014-10-17 09:23:50 -0700491 if (eth.getEtherType() == Ethernet.TYPE_ARP) {
492 ARP arp = (ARP) eth.getPayload();
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800493 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET,
494 arp.getSenderProtocolAddress());
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800495 updateLocationIP(hid, srcMac, vlan, hloc, ip);
alshabibe1cf87d2014-10-17 09:23:50 -0700496
Charles Chan5d5e0622015-09-25 13:00:06 -0700497 // IPv4: update location only
Charles Chane6067892016-11-17 10:23:46 -0800498 // DHCP ACK: additionally update IP of DHCP client
alshabibe1cf87d2014-10-17 09:23:50 -0700499 } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) {
Charles Chane6067892016-11-17 10:23:46 -0800500 IPacket pkt = eth.getPayload();
501 if (pkt != null && pkt instanceof IPv4) {
502 pkt = pkt.getPayload();
503 if (pkt != null && pkt instanceof UDP) {
504 pkt = pkt.getPayload();
505 if (pkt != null && pkt instanceof DHCP) {
506 DHCP dhcp = (DHCP) pkt;
507 if (dhcp.getOptions().stream()
508 .anyMatch(dhcpOption -> dhcpOption.getCode() ==
509 DHCP.DHCPOptionCode.OptionCode_MessageType.getValue() &&
510 dhcpOption.getLength() == 1 &&
511 dhcpOption.getData()[0] == DHCPPacketType.DHCPACK.getValue())) {
512 MacAddress hostMac = MacAddress.valueOf(dhcp.getClientHardwareAddress());
513 VlanId hostVlan = VlanId.vlanId(eth.getVlanID());
514 HostId hostId = HostId.hostId(hostMac, hostVlan);
515 updateIp(hostId, IpAddress.valueOf(dhcp.getYourIPAddress()));
516 }
517 }
518 }
519 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800520 updateLocation(hid, srcMac, vlan, hloc);
alshabibe1cf87d2014-10-17 09:23:50 -0700521
Charles Chan5d5e0622015-09-25 13:00:06 -0700522 //
523 // NeighborAdvertisement and NeighborSolicitation: possible
524 // new hosts, update both location and IP.
525 //
526 // IPv6: update location only
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800527 } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800528 IPv6 ipv6 = (IPv6) eth.getPayload();
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800529 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET6,
Thomas Vachuska27bee092015-06-23 19:03:10 -0700530 ipv6.getSourceAddress());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800531
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800532 // skip extension headers
533 IPacket pkt = ipv6;
534 while (pkt.getPayload() != null &&
535 pkt.getPayload() instanceof IExtensionHeader) {
536 pkt = pkt.getPayload();
537 }
538
539 // Neighbor Discovery Protocol
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800540 pkt = pkt.getPayload();
541 if (pkt != null && pkt instanceof ICMP6) {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800542 pkt = pkt.getPayload();
543 // RouterSolicitation, RouterAdvertisement
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800544 if (pkt != null && (pkt instanceof RouterAdvertisement ||
Thomas Vachuska27bee092015-06-23 19:03:10 -0700545 pkt instanceof RouterSolicitation)) {
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800546 return;
547 }
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800548 if (pkt != null && (pkt instanceof NeighborSolicitation ||
Thomas Vachuska27bee092015-06-23 19:03:10 -0700549 pkt instanceof NeighborAdvertisement)) {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800550 // Duplicate Address Detection
551 if (ip.isZero()) {
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800552 return;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800553 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800554 // NeighborSolicitation, NeighborAdvertisement
555 updateLocationIP(hid, srcMac, vlan, hloc, ip);
556 return;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800557 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800558 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800559
560 // multicast
561 if (eth.isMulticast()) {
562 return;
563 }
564
565 // normal IPv6 packets
566 updateLocation(hid, srcMac, vlan, hloc);
alshabibe1cf87d2014-10-17 09:23:50 -0700567 }
568 }
569 }
Thomas Vachuska33601602014-11-19 03:32:15 -0800570
571 // Auxiliary listener to device events.
572 private class InternalDeviceListener implements DeviceListener {
573 @Override
574 public void event(DeviceEvent event) {
Madan Jampania3770c32015-12-11 12:07:41 -0800575 eventHandler.execute(() -> handleEvent(event));
576 }
577
578 private void handleEvent(DeviceEvent event) {
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800579 Device device = event.subject();
580 switch (event.type()) {
Thomas Vachuska27bee092015-06-23 19:03:10 -0700581 case DEVICE_ADDED:
582 break;
583 case DEVICE_AVAILABILITY_CHANGED:
584 if (hostRemovalEnabled &&
585 !deviceService.isAvailable(device.id())) {
586 removeHosts(hostService.getConnectedHosts(device.id()));
587 }
588 break;
589 case DEVICE_SUSPENDED:
590 case DEVICE_UPDATED:
591 // Nothing to do?
592 break;
593 case DEVICE_REMOVED:
594 if (hostRemovalEnabled) {
595 removeHosts(hostService.getConnectedHosts(device.id()));
596 }
597 break;
598 case PORT_ADDED:
599 break;
600 case PORT_UPDATED:
601 if (hostRemovalEnabled) {
602 ConnectPoint point =
603 new ConnectPoint(device.id(), event.port().number());
604 removeHosts(hostService.getConnectedHosts(point));
605 }
606 break;
607 case PORT_REMOVED:
608 // Nothing to do?
609 break;
610 default:
611 break;
Thomas Vachuska33601602014-11-19 03:32:15 -0800612 }
613 }
614 }
615
616 // Signals host vanish for all specified hosts.
617 private void removeHosts(Set<Host> hosts) {
618 for (Host host : hosts) {
Charles Chan29ecdee2017-02-22 18:46:56 -0800619 providerService.hostVanished(host.id());
Thomas Vachuska33601602014-11-19 03:32:15 -0800620 }
621 }
622
alshabibe1cf87d2014-10-17 09:23:50 -0700623}