blob: e320eda8671b4332ede20691033ba39e7ad8ced2 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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;
26import org.onlab.packet.Ethernet;
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -080027import org.onlab.packet.ICMP6;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070028import org.onlab.packet.IPacket;
Jonathan Harte8600eb2015-01-12 10:30:45 -080029import org.onlab.packet.IPv6;
30import org.onlab.packet.IpAddress;
Charles M.C. Chan956cb692015-04-26 18:49:39 +080031import org.onlab.packet.MacAddress;
Jonathan Harte8600eb2015-01-12 10:30:45 -080032import org.onlab.packet.VlanId;
Charles M.C. Chan956cb692015-04-26 18:49:39 +080033import org.onlab.packet.ipv6.IExtensionHeader;
Jonathan Harte8600eb2015-01-12 10:30:45 -080034import org.onlab.packet.ndp.NeighborAdvertisement;
35import org.onlab.packet.ndp.NeighborSolicitation;
Charles M.C. Chan441d7da2015-03-17 21:03:39 +080036import org.onlab.packet.ndp.RouterAdvertisement;
37import org.onlab.packet.ndp.RouterSolicitation;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070038import org.onosproject.cfg.ComponentConfigService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080039import org.onosproject.core.ApplicationId;
40import org.onosproject.core.CoreService;
Brian O'Connorabafb502014-12-02 22:26:20 -080041import org.onosproject.net.ConnectPoint;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080042import org.onosproject.net.Device;
Brian O'Connorabafb502014-12-02 22:26:20 -080043import org.onosproject.net.Host;
44import org.onosproject.net.HostId;
45import org.onosproject.net.HostLocation;
46import org.onosproject.net.device.DeviceEvent;
47import org.onosproject.net.device.DeviceListener;
48import org.onosproject.net.device.DeviceService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080049import org.onosproject.net.flow.DefaultTrafficSelector;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080050import org.onosproject.net.flow.TrafficSelector;
Brian O'Connorabafb502014-12-02 22:26:20 -080051import org.onosproject.net.host.DefaultHostDescription;
52import org.onosproject.net.host.HostDescription;
53import org.onosproject.net.host.HostProvider;
54import org.onosproject.net.host.HostProviderRegistry;
55import org.onosproject.net.host.HostProviderService;
56import org.onosproject.net.host.HostService;
57import org.onosproject.net.packet.PacketContext;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080058import org.onosproject.net.packet.PacketPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080059import org.onosproject.net.packet.PacketProcessor;
60import org.onosproject.net.packet.PacketService;
61import org.onosproject.net.provider.AbstractProvider;
62import org.onosproject.net.provider.ProviderId;
63import org.onosproject.net.topology.Topology;
64import org.onosproject.net.topology.TopologyService;
Thomas Vachuska33601602014-11-19 03:32:15 -080065import org.osgi.service.component.ComponentContext;
alshabibe1cf87d2014-10-17 09:23:50 -070066import org.slf4j.Logger;
67
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070068import java.util.Dictionary;
69import java.util.Set;
70
71import static com.google.common.base.Strings.isNullOrEmpty;
72import static org.slf4j.LoggerFactory.getLogger;
73
alshabibe1cf87d2014-10-17 09:23:50 -070074/**
75 * Provider which uses an OpenFlow controller to detect network
76 * end-station hosts.
77 */
78@Component(immediate = true)
79public class HostLocationProvider extends AbstractProvider implements HostProvider {
80
81 private final Logger log = getLogger(getClass());
82
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080083 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected CoreService coreService;
85
86 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibe1cf87d2014-10-17 09:23:50 -070087 protected HostProviderRegistry providerRegistry;
88
89 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -080090 protected PacketService packetService;
alshabibe1cf87d2014-10-17 09:23:50 -070091
92 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
93 protected TopologyService topologyService;
94
Thomas Vachuska33601602014-11-19 03:32:15 -080095 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 protected HostService hostService;
97
98 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
99 protected DeviceService deviceService;
100
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700101 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
102 protected ComponentConfigService cfgService;
103
alshabibe1cf87d2014-10-17 09:23:50 -0700104 private HostProviderService providerService;
105
106 private final InternalHostProvider processor = new InternalHostProvider();
Thomas Vachuska33601602014-11-19 03:32:15 -0800107 private final DeviceListener deviceListener = new InternalDeviceListener();
108
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800109 private ApplicationId appId;
110
Thomas Vachuska33601602014-11-19 03:32:15 -0800111 @Property(name = "hostRemovalEnabled", boolValue = true,
112 label = "Enable host removal on port/device down events")
113 private boolean hostRemovalEnabled = true;
alshabibe1cf87d2014-10-17 09:23:50 -0700114
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800115 @Property(name = "ipv6NeighborDiscovery", boolValue = false,
116 label = "Enable using IPv6 Neighbor Discovery by the " +
117 "Host Location Provider; default is false")
118 private boolean ipv6NeighborDiscovery = false;
alshabibe1cf87d2014-10-17 09:23:50 -0700119
120 /**
121 * Creates an OpenFlow host provider.
122 */
123 public HostLocationProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800124 super(new ProviderId("of", "org.onosproject.provider.host"));
alshabibe1cf87d2014-10-17 09:23:50 -0700125 }
126
127 @Activate
Thomas Vachuska33601602014-11-19 03:32:15 -0800128 public void activate(ComponentContext context) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700129 cfgService.registerProperties(getClass());
130 appId = coreService.registerApplication("org.onosproject.provider.host");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800131 readComponentConfiguration(context);
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800132
alshabibe1cf87d2014-10-17 09:23:50 -0700133 providerService = providerRegistry.register(this);
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800134 packetService.addProcessor(processor, 1);
Thomas Vachuska33601602014-11-19 03:32:15 -0800135 deviceService.addListener(deviceListener);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800136
137 TrafficSelector.Builder selectorBuilder =
138 DefaultTrafficSelector.builder();
139 selectorBuilder.matchEthType(Ethernet.TYPE_ARP);
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800140 packetService.requestPackets(selectorBuilder.build(),
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800141 PacketPriority.CONTROL, appId);
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800142
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800143 if (ipv6NeighborDiscovery) {
144 // IPv6 Neighbor Solicitation packet.
145 selectorBuilder = DefaultTrafficSelector.builder();
146 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
147 selectorBuilder.matchIPProtocol(IPv6.PROTOCOL_ICMP6);
148 selectorBuilder.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION);
149 packetService.requestPackets(selectorBuilder.build(),
150 PacketPriority.CONTROL, appId);
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800151
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800152 // IPv6 Neighbor Advertisement packet.
153 selectorBuilder = DefaultTrafficSelector.builder();
154 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
155 selectorBuilder.matchIPProtocol(IPv6.PROTOCOL_ICMP6);
156 selectorBuilder.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT);
157 packetService.requestPackets(selectorBuilder.build(),
158 PacketPriority.CONTROL, appId);
159 }
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800160
161 log.info("Started with Application ID {}", appId.id());
alshabibe1cf87d2014-10-17 09:23:50 -0700162 }
163
164 @Deactivate
165 public void deactivate() {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700166 cfgService.unregisterProperties(getClass(), false);
alshabibe1cf87d2014-10-17 09:23:50 -0700167 providerRegistry.unregister(this);
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800168 packetService.removeProcessor(processor);
alshabibbfc6b722014-11-29 12:53:48 -0800169 deviceService.removeListener(deviceListener);
alshabibe1cf87d2014-10-17 09:23:50 -0700170 providerService = null;
171 log.info("Stopped");
172 }
173
Thomas Vachuska33601602014-11-19 03:32:15 -0800174 @Modified
175 public void modified(ComponentContext context) {
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800176 readComponentConfiguration(context);
177 }
178
179 /**
180 * Extracts properties from the component configuration context.
181 *
182 * @param context the component context
183 */
184 private void readComponentConfiguration(ComponentContext context) {
185 Dictionary<?, ?> properties = context.getProperties();
186 Boolean flag;
187
188 flag = isPropertyEnabled(properties, "hostRemovalEnabled");
189 if (flag == null) {
190 log.info("Host removal on port/device down events is not configured, " +
191 "using current value of {}", hostRemovalEnabled);
192 } else {
193 hostRemovalEnabled = flag;
194 log.info("Configured. Host removal on port/device down events is {}",
195 hostRemovalEnabled ? "enabled" : "disabled");
Thomas Vachuska33601602014-11-19 03:32:15 -0800196 }
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800197
198 flag = isPropertyEnabled(properties, "ipv6NeighborDiscovery");
199 if (flag == null) {
200 log.info("Using IPv6 Neighbor Discovery is not configured, " +
201 "using current value of {}", ipv6NeighborDiscovery);
202 } else {
203 ipv6NeighborDiscovery = flag;
204 log.info("Configured. Using IPv6 Neighbor Discovery is {}",
205 ipv6NeighborDiscovery ? "enabled" : "disabled");
206 }
207 }
208
209 /**
210 * Check property name is defined and set to true.
211 *
212 * @param properties properties to be looked up
213 * @param propertyName the name of the property to look up
214 * @return value when the propertyName is defined or return null
215 */
216 private static Boolean isPropertyEnabled(Dictionary<?, ?> properties,
217 String propertyName) {
218 Boolean value = null;
219 try {
220 String s = (String) properties.get(propertyName);
221 value = isNullOrEmpty(s) ? null : s.trim().equals("true");
222 } catch (ClassCastException e) {
223 // No propertyName defined.
224 value = null;
225 }
226 return value;
Thomas Vachuska33601602014-11-19 03:32:15 -0800227 }
228
alshabibe1cf87d2014-10-17 09:23:50 -0700229 @Override
230 public void triggerProbe(Host host) {
231 log.info("Triggering probe on device {}", host);
232 }
233
234 private class InternalHostProvider implements PacketProcessor {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800235 /**
236 * Update host location only.
237 *
238 * @param hid host ID
239 * @param mac source Mac address
240 * @param vlan VLAN ID
241 * @param hloc host location
242 */
243 private void updateLocation(HostId hid, MacAddress mac,
244 VlanId vlan, HostLocation hloc) {
245 HostDescription desc = new DefaultHostDescription(mac, vlan, hloc);
246 providerService.hostDetected(hid, desc);
247 }
248 /**
249 * Update host location and IP address.
250 *
251 * @param hid host ID
252 * @param mac source Mac address
253 * @param vlan VLAN ID
254 * @param hloc host location
255 * @param ip source IP address
256 */
257 private void updateLocationIP(HostId hid, MacAddress mac,
258 VlanId vlan, HostLocation hloc,
259 IpAddress ip) {
260 HostDescription desc = new DefaultHostDescription(mac, vlan, hloc, ip);
261 providerService.hostDetected(hid, desc);
262 }
alshabibe1cf87d2014-10-17 09:23:50 -0700263
264 @Override
265 public void process(PacketContext context) {
alshabib4a179dc2014-10-17 17:17:01 -0700266 if (context == null) {
267 return;
268 }
alshabibe1cf87d2014-10-17 09:23:50 -0700269
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700270 Ethernet eth = context.inPacket().parsed();
Jonathan Harte8600eb2015-01-12 10:30:45 -0800271 if (eth == null) {
272 return;
273 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800274 MacAddress srcMac = eth.getSourceMAC();
Jonathan Harte8600eb2015-01-12 10:30:45 -0800275
alshabibe1cf87d2014-10-17 09:23:50 -0700276 VlanId vlan = VlanId.vlanId(eth.getVlanID());
277 ConnectPoint heardOn = context.inPacket().receivedFrom();
278
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700279 // If this arrived on control port, bail out.
280 if (heardOn.port().isLogical()) {
281 return;
282 }
283
alshabibe1cf87d2014-10-17 09:23:50 -0700284 // If this is not an edge port, bail out.
285 Topology topology = topologyService.currentTopology();
286 if (topologyService.isInfrastructure(topology, heardOn)) {
287 return;
288 }
289
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800290 HostLocation hloc =
291 new HostLocation(heardOn, System.currentTimeMillis());
alshabibe1cf87d2014-10-17 09:23:50 -0700292
293 HostId hid = HostId.hostId(eth.getSourceMAC(), vlan);
294
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800295 // ARP: possible new hosts, update both location and IP
alshabibe1cf87d2014-10-17 09:23:50 -0700296 if (eth.getEtherType() == Ethernet.TYPE_ARP) {
297 ARP arp = (ARP) eth.getPayload();
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800298 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET,
299 arp.getSenderProtocolAddress());
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800300 updateLocationIP(hid, srcMac, vlan, hloc, ip);
alshabibe1cf87d2014-10-17 09:23:50 -0700301
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800302 // IPv4: update location only
alshabibe1cf87d2014-10-17 09:23:50 -0700303 } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800304 updateLocation(hid, srcMac, vlan, hloc);
alshabibe1cf87d2014-10-17 09:23:50 -0700305
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800306 //
307 // NeighborAdvertisement and NeighborSolicitation: possible
308 // new hosts, update both location and IP.
309 //
310 // IPv6: update location only
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800311 } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800312 IPv6 ipv6 = (IPv6) eth.getPayload();
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800313 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET6,
314 ipv6.getSourceAddress());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800315
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800316 // skip extension headers
317 IPacket pkt = ipv6;
318 while (pkt.getPayload() != null &&
319 pkt.getPayload() instanceof IExtensionHeader) {
320 pkt = pkt.getPayload();
321 }
322
323 // Neighbor Discovery Protocol
324 if (pkt instanceof ICMP6) {
325 pkt = pkt.getPayload();
326 // RouterSolicitation, RouterAdvertisement
327 if (pkt instanceof RouterAdvertisement ||
328 pkt instanceof RouterSolicitation) {
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800329 return;
330 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800331 if (pkt instanceof NeighborSolicitation ||
332 pkt instanceof NeighborAdvertisement) {
333 // Duplicate Address Detection
334 if (ip.isZero()) {
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800335 return;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800336 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800337 // NeighborSolicitation, NeighborAdvertisement
338 updateLocationIP(hid, srcMac, vlan, hloc, ip);
339 return;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800340 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800341 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800342
343 // multicast
344 if (eth.isMulticast()) {
345 return;
346 }
347
348 // normal IPv6 packets
349 updateLocation(hid, srcMac, vlan, hloc);
alshabibe1cf87d2014-10-17 09:23:50 -0700350 }
351 }
352 }
Thomas Vachuska33601602014-11-19 03:32:15 -0800353
354 // Auxiliary listener to device events.
355 private class InternalDeviceListener implements DeviceListener {
356 @Override
357 public void event(DeviceEvent event) {
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800358 Device device = event.subject();
359 switch (event.type()) {
360 case DEVICE_ADDED:
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800361 break;
362 case DEVICE_AVAILABILITY_CHANGED:
363 if (hostRemovalEnabled &&
364 !deviceService.isAvailable(device.id())) {
365 removeHosts(hostService.getConnectedHosts(device.id()));
Thomas Vachuska33601602014-11-19 03:32:15 -0800366 }
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800367 break;
368 case DEVICE_SUSPENDED:
369 case DEVICE_UPDATED:
370 // Nothing to do?
371 break;
372 case DEVICE_REMOVED:
373 if (hostRemovalEnabled) {
374 removeHosts(hostService.getConnectedHosts(device.id()));
375 }
376 break;
377 case PORT_ADDED:
378 break;
379 case PORT_UPDATED:
380 if (hostRemovalEnabled) {
381 ConnectPoint point =
382 new ConnectPoint(device.id(), event.port().number());
383 removeHosts(hostService.getConnectedHosts(point));
384 }
385 break;
386 case PORT_REMOVED:
387 // Nothing to do?
388 break;
389 default:
390 break;
Thomas Vachuska33601602014-11-19 03:32:15 -0800391 }
392 }
393 }
394
395 // Signals host vanish for all specified hosts.
396 private void removeHosts(Set<Host> hosts) {
397 for (Host host : hosts) {
398 providerService.hostVanished(host.id());
399 }
400 }
401
alshabibe1cf87d2014-10-17 09:23:50 -0700402}