blob: 97b49a42cbe605a4ddef020c82ab2ddd7ef73183 [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;
Charles Chan888e20a2017-05-01 15:44:23 -070049import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080050import org.onosproject.net.Host;
51import org.onosproject.net.HostId;
52import org.onosproject.net.HostLocation;
53import org.onosproject.net.device.DeviceEvent;
54import org.onosproject.net.device.DeviceListener;
55import org.onosproject.net.device.DeviceService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080056import org.onosproject.net.flow.DefaultTrafficSelector;
sdn94b00152016-08-30 02:12:32 -070057import org.onosproject.net.flow.DefaultTrafficTreatment;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080058import org.onosproject.net.flow.TrafficSelector;
sdn94b00152016-08-30 02:12:32 -070059import org.onosproject.net.flow.TrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080060import org.onosproject.net.host.DefaultHostDescription;
61import org.onosproject.net.host.HostDescription;
62import org.onosproject.net.host.HostProvider;
63import org.onosproject.net.host.HostProviderRegistry;
64import org.onosproject.net.host.HostProviderService;
65import org.onosproject.net.host.HostService;
sdn94b00152016-08-30 02:12:32 -070066import org.onosproject.net.packet.DefaultOutboundPacket;
67import org.onosproject.net.packet.OutboundPacket;
Brian O'Connorabafb502014-12-02 22:26:20 -080068import org.onosproject.net.packet.PacketContext;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080069import org.onosproject.net.packet.PacketPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080070import org.onosproject.net.packet.PacketProcessor;
71import org.onosproject.net.packet.PacketService;
72import org.onosproject.net.provider.AbstractProvider;
73import org.onosproject.net.provider.ProviderId;
74import org.onosproject.net.topology.Topology;
75import org.onosproject.net.topology.TopologyService;
Thomas Vachuska33601602014-11-19 03:32:15 -080076import org.osgi.service.component.ComponentContext;
alshabibe1cf87d2014-10-17 09:23:50 -070077import org.slf4j.Logger;
78
sdn94b00152016-08-30 02:12:32 -070079import java.nio.ByteBuffer;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070080import java.util.Dictionary;
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
Charles Chan888e20a2017-05-01 15:44:23 -0700368 // 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 -0700369 private Ethernet buildArpRequest(IpAddress targetIp, Host host) {
370
371 ARP arp = new ARP();
372 arp.setHardwareType(ARP.HW_TYPE_ETHERNET)
373 .setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH)
374 .setProtocolType(ARP.PROTO_TYPE_IP)
375 .setProtocolAddressLength((byte) IpAddress.INET_BYTE_LENGTH)
376 .setOpCode(ARP.OP_REQUEST);
377
378 arp.setSenderHardwareAddress(MacAddress.BROADCAST.toBytes())
379 .setSenderProtocolAddress(SENDER_ADDRESS)
380 .setTargetHardwareAddress(MacAddress.BROADCAST.toBytes())
381 .setTargetProtocolAddress(targetIp.toOctets());
382
383 Ethernet ethernet = new Ethernet();
384 ethernet.setEtherType(Ethernet.TYPE_ARP)
385 .setDestinationMACAddress(MacAddress.BROADCAST)
386 .setSourceMACAddress(MacAddress.BROADCAST).setPayload(arp);
387
388 ethernet.setPad(true);
389 return ethernet;
alshabibe1cf87d2014-10-17 09:23:50 -0700390 }
391
392 private class InternalHostProvider implements PacketProcessor {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800393 /**
Charles Chan888e20a2017-05-01 15:44:23 -0700394 * Create or update host information.
395 * Will not update IP if IP is null, all zero or self-assigned.
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800396 *
Thomas Vachuska27bee092015-06-23 19:03:10 -0700397 * @param hid host ID
398 * @param mac source Mac address
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800399 * @param vlan VLAN ID
400 * @param hloc host location
Charles Chan888e20a2017-05-01 15:44:23 -0700401 * @param ip source IP address or null if not updating
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800402 */
Charles Chan888e20a2017-05-01 15:44:23 -0700403 private void createOrUpdateHost(HostId hid, MacAddress mac,
404 VlanId vlan, HostLocation hloc,
405 IpAddress ip) {
406 HostDescription desc = ip == null || ip.isZero() || ip.isSelfAssigned() ?
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700407 new DefaultHostDescription(mac, vlan, hloc) :
408 new DefaultHostDescription(mac, vlan, hloc, ip);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700409 try {
Ray Milkeydc083442016-02-22 11:27:57 -0800410 providerService.hostDetected(hid, desc, false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700411 } catch (IllegalStateException e) {
412 log.debug("Host {} suppressed", hid);
413 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800414 }
alshabibe1cf87d2014-10-17 09:23:50 -0700415
Charles Chane6067892016-11-17 10:23:46 -0800416 /**
Charles Chan888e20a2017-05-01 15:44:23 -0700417 * Updates IP address for an existing host.
Charles Chane6067892016-11-17 10:23:46 -0800418 *
419 * @param hid host ID
420 * @param ip IP address
421 */
Charles Chan888e20a2017-05-01 15:44:23 -0700422 private void updateHostIp(HostId hid, IpAddress ip) {
Charles Chane6067892016-11-17 10:23:46 -0800423 Host host = hostService.getHost(hid);
424 if (host == null) {
425 log.debug("Fail to update IP for {}. Host does not exist");
426 return;
427 }
428
Charles Chan888e20a2017-05-01 15:44:23 -0700429 HostDescription desc = new DefaultHostDescription(hid.mac(), hid.vlanId(),
430 host.location(), ip);
Charles Chane6067892016-11-17 10:23:46 -0800431 try {
432 providerService.hostDetected(hid, desc, false);
433 } catch (IllegalStateException e) {
434 log.debug("Host {} suppressed", hid);
435 }
436 }
437
alshabibe1cf87d2014-10-17 09:23:50 -0700438 @Override
439 public void process(PacketContext context) {
alshabib4a179dc2014-10-17 17:17:01 -0700440 if (context == null) {
441 return;
442 }
alshabibe1cf87d2014-10-17 09:23:50 -0700443
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700444 Ethernet eth = context.inPacket().parsed();
Jonathan Harte8600eb2015-01-12 10:30:45 -0800445 if (eth == null) {
446 return;
447 }
Jonathan Hartf353e402016-09-23 09:27:36 -0700448
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800449 MacAddress srcMac = eth.getSourceMAC();
Jonathan Hartf353e402016-09-23 09:27:36 -0700450 if (srcMac.isBroadcast() || srcMac.isMulticast()) {
451 return;
452 }
Jonathan Harte8600eb2015-01-12 10:30:45 -0800453
alshabibe1cf87d2014-10-17 09:23:50 -0700454 VlanId vlan = VlanId.vlanId(eth.getVlanID());
455 ConnectPoint heardOn = context.inPacket().receivedFrom();
456
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700457 // If this arrived on control port, bail out.
458 if (heardOn.port().isLogical()) {
459 return;
460 }
461
alshabibe1cf87d2014-10-17 09:23:50 -0700462 // If this is not an edge port, bail out.
463 Topology topology = topologyService.currentTopology();
464 if (topologyService.isInfrastructure(topology, heardOn)) {
465 return;
466 }
467
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700468 HostLocation hloc = new HostLocation(heardOn, System.currentTimeMillis());
alshabibe1cf87d2014-10-17 09:23:50 -0700469 HostId hid = HostId.hostId(eth.getSourceMAC(), vlan);
470
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800471 // ARP: possible new hosts, update both location and IP
alshabibe1cf87d2014-10-17 09:23:50 -0700472 if (eth.getEtherType() == Ethernet.TYPE_ARP) {
473 ARP arp = (ARP) eth.getPayload();
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800474 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET,
475 arp.getSenderProtocolAddress());
Charles Chan888e20a2017-05-01 15:44:23 -0700476 createOrUpdateHost(hid, srcMac, vlan, hloc, ip);
alshabibe1cf87d2014-10-17 09:23:50 -0700477
Charles Chan5d5e0622015-09-25 13:00:06 -0700478 // IPv4: update location only
Charles Chane6067892016-11-17 10:23:46 -0800479 // DHCP ACK: additionally update IP of DHCP client
alshabibe1cf87d2014-10-17 09:23:50 -0700480 } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) {
Charles Chane6067892016-11-17 10:23:46 -0800481 IPacket pkt = eth.getPayload();
482 if (pkt != null && pkt instanceof IPv4) {
483 pkt = pkt.getPayload();
484 if (pkt != null && pkt instanceof UDP) {
485 pkt = pkt.getPayload();
486 if (pkt != null && pkt instanceof DHCP) {
487 DHCP dhcp = (DHCP) pkt;
488 if (dhcp.getOptions().stream()
489 .anyMatch(dhcpOption -> dhcpOption.getCode() ==
490 DHCP.DHCPOptionCode.OptionCode_MessageType.getValue() &&
491 dhcpOption.getLength() == 1 &&
492 dhcpOption.getData()[0] == DHCPPacketType.DHCPACK.getValue())) {
493 MacAddress hostMac = MacAddress.valueOf(dhcp.getClientHardwareAddress());
494 VlanId hostVlan = VlanId.vlanId(eth.getVlanID());
495 HostId hostId = HostId.hostId(hostMac, hostVlan);
Charles Chan888e20a2017-05-01 15:44:23 -0700496 updateHostIp(hostId, IpAddress.valueOf(dhcp.getYourIPAddress()));
Charles Chane6067892016-11-17 10:23:46 -0800497 }
498 }
499 }
500 }
Charles Chan888e20a2017-05-01 15:44:23 -0700501 createOrUpdateHost(hid, srcMac, vlan, hloc, null);
alshabibe1cf87d2014-10-17 09:23:50 -0700502
Charles Chan5d5e0622015-09-25 13:00:06 -0700503 //
504 // NeighborAdvertisement and NeighborSolicitation: possible
505 // new hosts, update both location and IP.
506 //
507 // IPv6: update location only
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800508 } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800509 IPv6 ipv6 = (IPv6) eth.getPayload();
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800510 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET6,
Thomas Vachuska27bee092015-06-23 19:03:10 -0700511 ipv6.getSourceAddress());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800512
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800513 // skip extension headers
514 IPacket pkt = ipv6;
515 while (pkt.getPayload() != null &&
516 pkt.getPayload() instanceof IExtensionHeader) {
517 pkt = pkt.getPayload();
518 }
519
520 // Neighbor Discovery Protocol
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800521 pkt = pkt.getPayload();
522 if (pkt != null && pkt instanceof ICMP6) {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800523 pkt = pkt.getPayload();
524 // RouterSolicitation, RouterAdvertisement
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800525 if (pkt != null && (pkt instanceof RouterAdvertisement ||
Thomas Vachuska27bee092015-06-23 19:03:10 -0700526 pkt instanceof RouterSolicitation)) {
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800527 return;
528 }
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800529 if (pkt != null && (pkt instanceof NeighborSolicitation ||
Thomas Vachuska27bee092015-06-23 19:03:10 -0700530 pkt instanceof NeighborAdvertisement)) {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800531 // Duplicate Address Detection
532 if (ip.isZero()) {
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800533 return;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800534 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800535 // NeighborSolicitation, NeighborAdvertisement
Charles Chan888e20a2017-05-01 15:44:23 -0700536 createOrUpdateHost(hid, srcMac, vlan, hloc, ip);
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800537 return;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800538 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800539 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800540
541 // multicast
542 if (eth.isMulticast()) {
543 return;
544 }
545
546 // normal IPv6 packets
Charles Chan888e20a2017-05-01 15:44:23 -0700547 createOrUpdateHost(hid, srcMac, vlan, hloc, null);
alshabibe1cf87d2014-10-17 09:23:50 -0700548 }
549 }
550 }
Thomas Vachuska33601602014-11-19 03:32:15 -0800551
552 // Auxiliary listener to device events.
553 private class InternalDeviceListener implements DeviceListener {
554 @Override
555 public void event(DeviceEvent event) {
Madan Jampania3770c32015-12-11 12:07:41 -0800556 eventHandler.execute(() -> handleEvent(event));
557 }
558
559 private void handleEvent(DeviceEvent event) {
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800560 Device device = event.subject();
561 switch (event.type()) {
Thomas Vachuska27bee092015-06-23 19:03:10 -0700562 case DEVICE_ADDED:
563 break;
564 case DEVICE_AVAILABILITY_CHANGED:
Charles Chan888e20a2017-05-01 15:44:23 -0700565 if (hostRemovalEnabled && !deviceService.isAvailable(device.id())) {
566 processDeviceDown(device.id());
Thomas Vachuska27bee092015-06-23 19:03:10 -0700567 }
568 break;
569 case DEVICE_SUSPENDED:
570 case DEVICE_UPDATED:
571 // Nothing to do?
572 break;
573 case DEVICE_REMOVED:
574 if (hostRemovalEnabled) {
Charles Chan888e20a2017-05-01 15:44:23 -0700575 processDeviceDown(device.id());
Thomas Vachuska27bee092015-06-23 19:03:10 -0700576 }
577 break;
578 case PORT_ADDED:
579 break;
580 case PORT_UPDATED:
Charles Chan888e20a2017-05-01 15:44:23 -0700581 if (hostRemovalEnabled && !event.port().isEnabled()) {
582 processPortDown(new ConnectPoint(device.id(), event.port().number()));
Thomas Vachuska27bee092015-06-23 19:03:10 -0700583 }
584 break;
585 case PORT_REMOVED:
586 // Nothing to do?
587 break;
588 default:
589 break;
Thomas Vachuska33601602014-11-19 03:32:15 -0800590 }
591 }
592 }
593
Charles Chan888e20a2017-05-01 15:44:23 -0700594 /**
595 * When a device goes down, update the location of affected hosts.
596 *
597 * @param deviceId the device that goes down
598 */
599 private void processDeviceDown(DeviceId deviceId) {
600 hostService.getConnectedHosts(deviceId).forEach(affectedHost -> affectedHost.locations().stream()
601 .filter(hostLocation -> hostLocation.deviceId().equals(deviceId))
602 .forEach(affectedLocation ->
603 providerService.removeLocationFromHost(affectedHost.id(), affectedLocation))
604 );
605 }
606
607 /**
608 * When a port goes down, update the location of affected hosts.
609 *
610 * @param connectPoint the port that goes down
611 */
612 private void processPortDown(ConnectPoint connectPoint) {
613 hostService.getConnectedHosts(connectPoint).forEach(affectedHost ->
614 providerService.removeLocationFromHost(affectedHost.id(), new HostLocation(connectPoint, 0L))
615 );
Thomas Vachuska33601602014-11-19 03:32:15 -0800616 }
617
alshabibe1cf87d2014-10-17 09:23:50 -0700618}