blob: 1ead47169ed921e1699f294ac1f5d0c4ecfa9e93 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.provider.host.impl;
alshabibe1cf87d2014-10-17 09:23:50 -070017
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;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070046import org.onosproject.incubator.net.intf.InterfaceService;
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;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070081import java.util.Objects;
82import java.util.Optional;
Madan Jampania3770c32015-12-11 12:07:41 -080083import java.util.concurrent.ExecutorService;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070084import java.util.stream.Stream;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070085
Madan Jampania3770c32015-12-11 12:07:41 -080086import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
87import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070088import static org.slf4j.LoggerFactory.getLogger;
89
alshabibe1cf87d2014-10-17 09:23:50 -070090/**
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -070091 * Provider which uses an OpenFlow controller to detect network end-station
92 * hosts.
alshabibe1cf87d2014-10-17 09:23:50 -070093 */
94@Component(immediate = true)
95public class HostLocationProvider extends AbstractProvider implements HostProvider {
alshabibe1cf87d2014-10-17 09:23:50 -070096 private final Logger log = getLogger(getClass());
97
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080098 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
99 protected CoreService coreService;
100
101 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibe1cf87d2014-10-17 09:23:50 -0700102 protected HostProviderRegistry providerRegistry;
103
104 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800105 protected PacketService packetService;
alshabibe1cf87d2014-10-17 09:23:50 -0700106
107 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
108 protected TopologyService topologyService;
109
Thomas Vachuska33601602014-11-19 03:32:15 -0800110 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
111 protected HostService hostService;
112
113 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
114 protected DeviceService deviceService;
115
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700116 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
117 protected ComponentConfigService cfgService;
118
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700119 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
120 protected InterfaceService interfaceService;
121
alshabibe1cf87d2014-10-17 09:23:50 -0700122 private HostProviderService providerService;
123
124 private final InternalHostProvider processor = new InternalHostProvider();
Thomas Vachuska33601602014-11-19 03:32:15 -0800125 private final DeviceListener deviceListener = new InternalDeviceListener();
126
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800127 private ApplicationId appId;
128
Thomas Vachuska33601602014-11-19 03:32:15 -0800129 @Property(name = "hostRemovalEnabled", boolValue = true,
130 label = "Enable host removal on port/device down events")
131 private boolean hostRemovalEnabled = true;
alshabibe1cf87d2014-10-17 09:23:50 -0700132
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700133 @Property(name = "requestArp", boolValue = true,
134 label = "Request ARP packets for neighbor discovery by the " +
Charles Chane6067892016-11-17 10:23:46 -0800135 "Host Location Provider; default is true")
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700136 private boolean requestArp = true;
Charles Chane6067892016-11-17 10:23:46 -0800137
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700138 @Property(name = "requestIpv6ND", boolValue = false,
139 label = "Requests IPv6 Neighbor Discovery by the " +
Thomas Vachuska27bee092015-06-23 19:03:10 -0700140 "Host Location Provider; default is false")
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700141 private boolean requestIpv6ND = false;
Charles Chane6067892016-11-17 10:23:46 -0800142
143 @Property(name = "useDhcp", boolValue = false,
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700144 label = "Use DHCP for neighbor discovery by the " +
Charles Chane6067892016-11-17 10:23:46 -0800145 "Host Location Provider; default is false")
146 private boolean useDhcp = false;
alshabibe1cf87d2014-10-17 09:23:50 -0700147
Jonathan Hart9af322d2016-01-06 17:42:04 -0800148 @Property(name = "requestInterceptsEnabled", boolValue = true,
149 label = "Enable requesting packet intercepts")
150 private boolean requestInterceptsEnabled = true;
151
Madan Jampania3770c32015-12-11 12:07:41 -0800152 protected ExecutorService eventHandler;
153
sdn94b00152016-08-30 02:12:32 -0700154 private static final byte[] SENDER_ADDRESS = IpAddress.valueOf("0.0.0.0").toOctets();
155
alshabibe1cf87d2014-10-17 09:23:50 -0700156 /**
157 * Creates an OpenFlow host provider.
158 */
159 public HostLocationProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800160 super(new ProviderId("of", "org.onosproject.provider.host"));
alshabibe1cf87d2014-10-17 09:23:50 -0700161 }
162
163 @Activate
Thomas Vachuska33601602014-11-19 03:32:15 -0800164 public void activate(ComponentContext context) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700165 cfgService.registerProperties(getClass());
166 appId = coreService.registerApplication("org.onosproject.provider.host");
Jonathan Hart9af322d2016-01-06 17:42:04 -0800167 eventHandler = newSingleThreadScheduledExecutor(
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700168 groupedThreads("onos/host-loc-provider", "event-handler", log));
alshabibe1cf87d2014-10-17 09:23:50 -0700169 providerService = providerRegistry.register(this);
Brian O'Connor3b783262015-07-29 17:49:24 -0700170 packetService.addProcessor(processor, PacketProcessor.advisor(1));
Thomas Vachuska33601602014-11-19 03:32:15 -0800171 deviceService.addListener(deviceListener);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800172
173 modified(context);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800174
Charles M.C. Chane148de82015-05-06 12:38:21 +0800175 log.info("Started with Application ID {}", appId.id());
176 }
177
178 @Deactivate
179 public void deactivate() {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800180 cfgService.unregisterProperties(getClass(), false);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700181
182 withdrawIntercepts();
183
Charles M.C. Chane148de82015-05-06 12:38:21 +0800184 providerRegistry.unregister(this);
185 packetService.removeProcessor(processor);
186 deviceService.removeListener(deviceListener);
Madan Jampania3770c32015-12-11 12:07:41 -0800187 eventHandler.shutdown();
Charles M.C. Chane148de82015-05-06 12:38:21 +0800188 providerService = null;
189 log.info("Stopped");
190 }
191
192 @Modified
193 public void modified(ComponentContext context) {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800194 readComponentConfiguration(context);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800195
196 if (requestInterceptsEnabled) {
197 requestIntercepts();
198 } else {
199 withdrawIntercepts();
200 }
Charles M.C. Chane148de82015-05-06 12:38:21 +0800201 }
202
203 /**
Thomas Vachuska27bee092015-06-23 19:03:10 -0700204 * Request packet intercepts.
Charles M.C. Chane148de82015-05-06 12:38:21 +0800205 */
Thomas Vachuska27bee092015-06-23 19:03:10 -0700206 private void requestIntercepts() {
Charles Chane6067892016-11-17 10:23:46 -0800207 // Use ARP
208 TrafficSelector arpSelector = DefaultTrafficSelector.builder()
209 .matchEthType(Ethernet.TYPE_ARP)
210 .build();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700211 if (requestArp) {
Charles Chane6067892016-11-17 10:23:46 -0800212 packetService.requestPackets(arpSelector, PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700213 } else {
Charles Chane6067892016-11-17 10:23:46 -0800214 packetService.cancelPackets(arpSelector, PacketPriority.CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800215 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700216
Charles Chane6067892016-11-17 10:23:46 -0800217 // Use IPv6 Neighbor Discovery
218 TrafficSelector ipv6NsSelector = DefaultTrafficSelector.builder()
219 .matchEthType(Ethernet.TYPE_IPV6)
220 .matchIPProtocol(IPv6.PROTOCOL_ICMP6)
221 .matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION)
222 .build();
223 TrafficSelector ipv6NaSelector = DefaultTrafficSelector.builder()
224 .matchEthType(Ethernet.TYPE_IPV6)
225 .matchIPProtocol(IPv6.PROTOCOL_ICMP6)
226 .matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT)
227 .build();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700228 if (requestIpv6ND) {
Charles Chane6067892016-11-17 10:23:46 -0800229 packetService.requestPackets(ipv6NsSelector, PacketPriority.CONTROL, appId);
230 packetService.requestPackets(ipv6NaSelector, PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700231 } else {
Charles Chane6067892016-11-17 10:23:46 -0800232 packetService.cancelPackets(ipv6NsSelector, PacketPriority.CONTROL, appId);
233 packetService.cancelPackets(ipv6NaSelector, PacketPriority.CONTROL, appId);
234 }
235
236 // Use DHCP
237 TrafficSelector dhcpServerSelector = DefaultTrafficSelector.builder()
238 .matchEthType(Ethernet.TYPE_IPV4)
239 .matchIPProtocol(IPv4.PROTOCOL_UDP)
240 .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT))
241 .build();
242 TrafficSelector dhcpClientSelector = DefaultTrafficSelector.builder()
243 .matchEthType(Ethernet.TYPE_IPV4)
244 .matchIPProtocol(IPv4.PROTOCOL_UDP)
245 .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT))
246 .build();
Thomas Vachuska27bee092015-06-23 19:03:10 -0700247 }
248
249 /**
250 * Withdraw packet intercepts.
251 */
252 private void withdrawIntercepts() {
253 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
254 selector.matchEthType(Ethernet.TYPE_ARP);
Aaron Kruglikov07a923d2015-07-03 13:30:57 -0700255 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700256
257 // IPv6 Neighbor Solicitation packet.
258 selector.matchEthType(Ethernet.TYPE_IPV6);
259 selector.matchIPProtocol(IPv6.PROTOCOL_ICMP6);
260 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION);
261 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
262
263 // IPv6 Neighbor Advertisement packet.
264 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT);
265 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800266 }
267
268 /**
269 * Extracts properties from the component configuration context.
270 *
271 * @param context the component context
272 */
273 private void readComponentConfiguration(ComponentContext context) {
274 Dictionary<?, ?> properties = context.getProperties();
275 Boolean flag;
276
Jian Lid9b5f552016-03-11 18:15:31 -0800277 flag = Tools.isPropertyEnabled(properties, "hostRemovalEnabled");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800278 if (flag == null) {
279 log.info("Host removal on port/device down events is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700280 "using current value of {}", hostRemovalEnabled);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800281 } else {
282 hostRemovalEnabled = flag;
283 log.info("Configured. Host removal on port/device down events is {}",
284 hostRemovalEnabled ? "enabled" : "disabled");
Thomas Vachuska33601602014-11-19 03:32:15 -0800285 }
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800286
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700287 flag = Tools.isPropertyEnabled(properties, "requestArp");
Charles Chane6067892016-11-17 10:23:46 -0800288 if (flag == null) {
289 log.info("Using ARP is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700290 "using current value of {}", requestArp);
Charles Chane6067892016-11-17 10:23:46 -0800291 } else {
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700292 requestArp = flag;
Charles Chane6067892016-11-17 10:23:46 -0800293 log.info("Configured. Using ARP is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700294 requestArp ? "enabled" : "disabled");
Charles Chane6067892016-11-17 10:23:46 -0800295 }
296
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700297 flag = Tools.isPropertyEnabled(properties, "requestIpv6ND");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800298 if (flag == null) {
299 log.info("Using IPv6 Neighbor Discovery is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700300 "using current value of {}", requestIpv6ND);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800301 } else {
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700302 requestIpv6ND = flag;
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800303 log.info("Configured. Using IPv6 Neighbor Discovery is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700304 requestIpv6ND ? "enabled" : "disabled");
Charles Chane6067892016-11-17 10:23:46 -0800305 }
306
307 flag = Tools.isPropertyEnabled(properties, "useDhcp");
308 if (flag == null) {
309 log.info("Using DHCP is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700310 "using current value of {}", useDhcp);
Charles Chane6067892016-11-17 10:23:46 -0800311 } else {
312 useDhcp = flag;
313 log.info("Configured. Using DHCP is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700314 useDhcp ? "enabled" : "disabled");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800315 }
Jonathan Hart9af322d2016-01-06 17:42:04 -0800316
Jian Lid9b5f552016-03-11 18:15:31 -0800317 flag = Tools.isPropertyEnabled(properties, "requestInterceptsEnabled");
Jonathan Hart9af322d2016-01-06 17:42:04 -0800318 if (flag == null) {
319 log.info("Request intercepts is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700320 "using current value of {}", requestInterceptsEnabled);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800321 } else {
322 requestInterceptsEnabled = flag;
323 log.info("Configured. Request intercepts is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700324 requestInterceptsEnabled ? "enabled" : "disabled");
Jonathan Hart9af322d2016-01-06 17:42:04 -0800325 }
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800326 }
327
alshabibe1cf87d2014-10-17 09:23:50 -0700328 @Override
329 public void triggerProbe(Host host) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800330 //log.info("Triggering probe on device {} ", host);
Jonathan Hartf353e402016-09-23 09:27:36 -0700331
332 // FIXME Disabling host probing for now, because sending packets from a
333 // broadcast MAC address caused problems when two ONOS networks were
334 // interconnected. Host probing should take into account the interface
335 // configuration when determining which source address to use.
336
337 //MastershipRole role = deviceService.getRole(host.location().deviceId());
338 //if (role.equals(MastershipRole.MASTER)) {
339 // host.ipAddresses().forEach(ip -> {
340 // sendProbe(host, ip);
341 // });
342 //} else {
343 // log.info("not the master, master will probe {}");
344 //}
sdn94b00152016-08-30 02:12:32 -0700345 }
346
347 private void sendProbe(Host host, IpAddress targetIp) {
348 Ethernet probePacket = null;
349 if (targetIp.isIp4()) {
350 // IPv4: Use ARP
351 probePacket = buildArpRequest(targetIp, host);
352 } else {
353 // IPv6: Use Neighbor Discovery
Jonathan Hartf353e402016-09-23 09:27:36 -0700354 //TODO need to implement ndp probe
sdn94b00152016-08-30 02:12:32 -0700355 log.info("Triggering probe on device {} ", host);
356 }
357
358 TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(host.location().port()).build();
359
360 OutboundPacket outboundPacket = new DefaultOutboundPacket(host.location().deviceId(), treatment,
361 ByteBuffer.wrap(probePacket.serialize()));
362
363 packetService.emit(outboundPacket);
364 }
365
Charles Chan888e20a2017-05-01 15:44:23 -0700366 // 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 -0700367 private Ethernet buildArpRequest(IpAddress targetIp, Host host) {
368
369 ARP arp = new ARP();
370 arp.setHardwareType(ARP.HW_TYPE_ETHERNET)
371 .setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH)
372 .setProtocolType(ARP.PROTO_TYPE_IP)
373 .setProtocolAddressLength((byte) IpAddress.INET_BYTE_LENGTH)
374 .setOpCode(ARP.OP_REQUEST);
375
376 arp.setSenderHardwareAddress(MacAddress.BROADCAST.toBytes())
377 .setSenderProtocolAddress(SENDER_ADDRESS)
378 .setTargetHardwareAddress(MacAddress.BROADCAST.toBytes())
379 .setTargetProtocolAddress(targetIp.toOctets());
380
381 Ethernet ethernet = new Ethernet();
382 ethernet.setEtherType(Ethernet.TYPE_ARP)
383 .setDestinationMACAddress(MacAddress.BROADCAST)
384 .setSourceMACAddress(MacAddress.BROADCAST).setPayload(arp);
385
386 ethernet.setPad(true);
387 return ethernet;
alshabibe1cf87d2014-10-17 09:23:50 -0700388 }
389
390 private class InternalHostProvider implements PacketProcessor {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800391 /**
Charles Chan888e20a2017-05-01 15:44:23 -0700392 * Create or update host information.
393 * Will not update IP if IP is null, all zero or self-assigned.
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800394 *
Thomas Vachuska27bee092015-06-23 19:03:10 -0700395 * @param hid host ID
396 * @param mac source Mac address
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800397 * @param vlan VLAN ID
398 * @param hloc host location
Charles Chan888e20a2017-05-01 15:44:23 -0700399 * @param ip source IP address or null if not updating
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800400 */
Charles Chan888e20a2017-05-01 15:44:23 -0700401 private void createOrUpdateHost(HostId hid, MacAddress mac,
402 VlanId vlan, HostLocation hloc,
403 IpAddress ip) {
404 HostDescription desc = ip == null || ip.isZero() || ip.isSelfAssigned() ?
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700405 new DefaultHostDescription(mac, vlan, hloc) :
406 new DefaultHostDescription(mac, vlan, hloc, ip);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700407 try {
Ray Milkeydc083442016-02-22 11:27:57 -0800408 providerService.hostDetected(hid, desc, false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700409 } catch (IllegalStateException e) {
410 log.debug("Host {} suppressed", hid);
411 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800412 }
alshabibe1cf87d2014-10-17 09:23:50 -0700413
Charles Chane6067892016-11-17 10:23:46 -0800414 /**
Charles Chan888e20a2017-05-01 15:44:23 -0700415 * Updates IP address for an existing host.
Charles Chane6067892016-11-17 10:23:46 -0800416 *
417 * @param hid host ID
418 * @param ip IP address
419 */
Charles Chan888e20a2017-05-01 15:44:23 -0700420 private void updateHostIp(HostId hid, IpAddress ip) {
Charles Chane6067892016-11-17 10:23:46 -0800421 Host host = hostService.getHost(hid);
422 if (host == null) {
423 log.debug("Fail to update IP for {}. Host does not exist");
424 return;
425 }
426
Charles Chan888e20a2017-05-01 15:44:23 -0700427 HostDescription desc = new DefaultHostDescription(hid.mac(), hid.vlanId(),
428 host.location(), ip);
Charles Chane6067892016-11-17 10:23:46 -0800429 try {
430 providerService.hostDetected(hid, desc, false);
431 } catch (IllegalStateException e) {
432 log.debug("Host {} suppressed", hid);
433 }
434 }
435
alshabibe1cf87d2014-10-17 09:23:50 -0700436 @Override
437 public void process(PacketContext context) {
alshabib4a179dc2014-10-17 17:17:01 -0700438 if (context == null) {
439 return;
440 }
alshabibe1cf87d2014-10-17 09:23:50 -0700441
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700442 Ethernet eth = context.inPacket().parsed();
Jonathan Harte8600eb2015-01-12 10:30:45 -0800443 if (eth == null) {
444 return;
445 }
Jonathan Hartf353e402016-09-23 09:27:36 -0700446
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800447 MacAddress srcMac = eth.getSourceMAC();
Jonathan Hartf353e402016-09-23 09:27:36 -0700448 if (srcMac.isBroadcast() || srcMac.isMulticast()) {
449 return;
450 }
Jonathan Harte8600eb2015-01-12 10:30:45 -0800451
alshabibe1cf87d2014-10-17 09:23:50 -0700452 VlanId vlan = VlanId.vlanId(eth.getVlanID());
453 ConnectPoint heardOn = context.inPacket().receivedFrom();
454
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700455 // If this arrived on control port, bail out.
456 if (heardOn.port().isLogical()) {
457 return;
458 }
459
alshabibe1cf87d2014-10-17 09:23:50 -0700460 // If this is not an edge port, bail out.
461 Topology topology = topologyService.currentTopology();
462 if (topologyService.isInfrastructure(topology, heardOn)) {
463 return;
464 }
465
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700466 HostLocation hloc = new HostLocation(heardOn, System.currentTimeMillis());
alshabibe1cf87d2014-10-17 09:23:50 -0700467 HostId hid = HostId.hostId(eth.getSourceMAC(), vlan);
468
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800469 // ARP: possible new hosts, update both location and IP
alshabibe1cf87d2014-10-17 09:23:50 -0700470 if (eth.getEtherType() == Ethernet.TYPE_ARP) {
471 ARP arp = (ARP) eth.getPayload();
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800472 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET,
473 arp.getSenderProtocolAddress());
Charles Chan888e20a2017-05-01 15:44:23 -0700474 createOrUpdateHost(hid, srcMac, vlan, hloc, ip);
alshabibe1cf87d2014-10-17 09:23:50 -0700475
Charles Chan5d5e0622015-09-25 13:00:06 -0700476 // IPv4: update location only
alshabibe1cf87d2014-10-17 09:23:50 -0700477 } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) {
Yi Tsengcfa637a2017-07-26 17:24:10 -0700478 DHCP dhcp = findDhcp(eth).orElse(null);
479 if (dhcp != null) {
480 if (useDhcp) {
481 // learn host (server or client) MAC address
482 createOrUpdateHost(hid, srcMac, vlan, hloc, null);
483
484 // DHCP ACK: additionally update IP of DHCP client
485 if (dhcp.getPacketType().equals(DHCP.MsgType.DHCPACK)) {
486 MacAddress hostMac = MacAddress.valueOf(dhcp.getClientHardwareAddress());
487 VlanId hostVlan = VlanId.vlanId(eth.getVlanID());
488 HostId hostId = HostId.hostId(hostMac, hostVlan);
489 updateHostIp(hostId, IpAddress.valueOf(dhcp.getYourIPAddress()));
490 }
491 }
492 } else {
493 // learn host MAC address
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700494 createOrUpdateHost(hid, srcMac, vlan, hloc, null);
Charles Chane6067892016-11-17 10:23:46 -0800495 }
Charles Chan5d5e0622015-09-25 13:00:06 -0700496 //
497 // NeighborAdvertisement and NeighborSolicitation: possible
498 // new hosts, update both location and IP.
499 //
500 // IPv6: update location only
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800501 } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800502 IPv6 ipv6 = (IPv6) eth.getPayload();
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800503 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET6,
Thomas Vachuska27bee092015-06-23 19:03:10 -0700504 ipv6.getSourceAddress());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800505
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800506 // skip extension headers
507 IPacket pkt = ipv6;
508 while (pkt.getPayload() != null &&
509 pkt.getPayload() instanceof IExtensionHeader) {
510 pkt = pkt.getPayload();
511 }
512
513 // Neighbor Discovery Protocol
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800514 pkt = pkt.getPayload();
515 if (pkt != null && pkt instanceof ICMP6) {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800516 pkt = pkt.getPayload();
517 // RouterSolicitation, RouterAdvertisement
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800518 if (pkt != null && (pkt instanceof RouterAdvertisement ||
Thomas Vachuska27bee092015-06-23 19:03:10 -0700519 pkt instanceof RouterSolicitation)) {
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800520 return;
521 }
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800522 if (pkt != null && (pkt instanceof NeighborSolicitation ||
Thomas Vachuska27bee092015-06-23 19:03:10 -0700523 pkt instanceof NeighborAdvertisement)) {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800524 // Duplicate Address Detection
525 if (ip.isZero()) {
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800526 return;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800527 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800528 // NeighborSolicitation, NeighborAdvertisement
Charles Chan888e20a2017-05-01 15:44:23 -0700529 createOrUpdateHost(hid, srcMac, vlan, hloc, ip);
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800530 return;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800531 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800532 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800533
534 // multicast
535 if (eth.isMulticast()) {
536 return;
537 }
538
539 // normal IPv6 packets
Charles Chan888e20a2017-05-01 15:44:23 -0700540 createOrUpdateHost(hid, srcMac, vlan, hloc, null);
alshabibe1cf87d2014-10-17 09:23:50 -0700541 }
542 }
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700543
544 private Optional<DHCP> findDhcp(Ethernet eth) {
545 IPacket pkt = eth.getPayload();
546 return Stream.of(pkt)
547 .filter(Objects::nonNull)
548 .filter(p -> p instanceof IPv4)
549 .map(IPacket::getPayload)
550 .filter(Objects::nonNull)
551 .filter(p -> p instanceof UDP)
552 .map(IPacket::getPayload)
553 .filter(Objects::nonNull)
554 .filter(p -> p instanceof DHCP)
555 .map(p -> (DHCP) p)
556 .findFirst();
557 }
alshabibe1cf87d2014-10-17 09:23:50 -0700558 }
Thomas Vachuska33601602014-11-19 03:32:15 -0800559
560 // Auxiliary listener to device events.
561 private class InternalDeviceListener implements DeviceListener {
562 @Override
563 public void event(DeviceEvent event) {
Madan Jampania3770c32015-12-11 12:07:41 -0800564 eventHandler.execute(() -> handleEvent(event));
565 }
566
567 private void handleEvent(DeviceEvent event) {
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800568 Device device = event.subject();
569 switch (event.type()) {
Thomas Vachuska27bee092015-06-23 19:03:10 -0700570 case DEVICE_ADDED:
571 break;
572 case DEVICE_AVAILABILITY_CHANGED:
Charles Chan888e20a2017-05-01 15:44:23 -0700573 if (hostRemovalEnabled && !deviceService.isAvailable(device.id())) {
574 processDeviceDown(device.id());
Thomas Vachuska27bee092015-06-23 19:03:10 -0700575 }
576 break;
577 case DEVICE_SUSPENDED:
578 case DEVICE_UPDATED:
579 // Nothing to do?
580 break;
581 case DEVICE_REMOVED:
582 if (hostRemovalEnabled) {
Charles Chan888e20a2017-05-01 15:44:23 -0700583 processDeviceDown(device.id());
Thomas Vachuska27bee092015-06-23 19:03:10 -0700584 }
585 break;
586 case PORT_ADDED:
587 break;
588 case PORT_UPDATED:
Charles Chan888e20a2017-05-01 15:44:23 -0700589 if (hostRemovalEnabled && !event.port().isEnabled()) {
590 processPortDown(new ConnectPoint(device.id(), event.port().number()));
Thomas Vachuska27bee092015-06-23 19:03:10 -0700591 }
592 break;
593 case PORT_REMOVED:
594 // Nothing to do?
595 break;
596 default:
597 break;
Thomas Vachuska33601602014-11-19 03:32:15 -0800598 }
599 }
600 }
601
Charles Chan888e20a2017-05-01 15:44:23 -0700602 /**
603 * When a device goes down, update the location of affected hosts.
604 *
605 * @param deviceId the device that goes down
606 */
607 private void processDeviceDown(DeviceId deviceId) {
608 hostService.getConnectedHosts(deviceId).forEach(affectedHost -> affectedHost.locations().stream()
609 .filter(hostLocation -> hostLocation.deviceId().equals(deviceId))
610 .forEach(affectedLocation ->
611 providerService.removeLocationFromHost(affectedHost.id(), affectedLocation))
612 );
613 }
614
615 /**
616 * When a port goes down, update the location of affected hosts.
617 *
618 * @param connectPoint the port that goes down
619 */
620 private void processPortDown(ConnectPoint connectPoint) {
621 hostService.getConnectedHosts(connectPoint).forEach(affectedHost ->
622 providerService.removeLocationFromHost(affectedHost.id(), new HostLocation(connectPoint, 0L))
623 );
Thomas Vachuska33601602014-11-19 03:32:15 -0800624 }
625
alshabibe1cf87d2014-10-17 09:23:50 -0700626}