blob: e4dc3c301ee801985f0dbe196d217fdf8e1b6024 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080018import static com.google.common.base.Strings.isNullOrEmpty;
Jonathan Harte8600eb2015-01-12 10:30:45 -080019import static org.slf4j.LoggerFactory.getLogger;
20
21import java.util.Dictionary;
22import java.util.Set;
23
alshabibe1cf87d2014-10-17 09:23:50 -070024import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Deactivate;
Thomas Vachuska33601602014-11-19 03:32:15 -080027import org.apache.felix.scr.annotations.Modified;
28import org.apache.felix.scr.annotations.Property;
alshabibe1cf87d2014-10-17 09:23:50 -070029import org.apache.felix.scr.annotations.Reference;
30import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Harte8600eb2015-01-12 10:30:45 -080031import org.onlab.packet.ARP;
32import org.onlab.packet.Ethernet;
33import org.onlab.packet.IPacket;
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -080034import org.onlab.packet.ICMP6;
Jonathan Harte8600eb2015-01-12 10:30:45 -080035import org.onlab.packet.IPv6;
36import org.onlab.packet.IpAddress;
37import org.onlab.packet.VlanId;
38import 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;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070042import org.onosproject.cfg.ComponentConfigService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080043import org.onosproject.core.ApplicationId;
44import org.onosproject.core.CoreService;
Brian O'Connorabafb502014-12-02 22:26:20 -080045import org.onosproject.net.ConnectPoint;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080046import org.onosproject.net.Device;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import org.onosproject.net.Host;
48import org.onosproject.net.HostId;
49import org.onosproject.net.HostLocation;
50import org.onosproject.net.device.DeviceEvent;
51import org.onosproject.net.device.DeviceListener;
52import org.onosproject.net.device.DeviceService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080053import org.onosproject.net.flow.DefaultTrafficSelector;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080054import org.onosproject.net.flow.TrafficSelector;
Brian O'Connorabafb502014-12-02 22:26:20 -080055import org.onosproject.net.host.DefaultHostDescription;
56import org.onosproject.net.host.HostDescription;
57import org.onosproject.net.host.HostProvider;
58import org.onosproject.net.host.HostProviderRegistry;
59import org.onosproject.net.host.HostProviderService;
60import org.onosproject.net.host.HostService;
61import org.onosproject.net.packet.PacketContext;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080062import org.onosproject.net.packet.PacketPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080063import org.onosproject.net.packet.PacketProcessor;
64import org.onosproject.net.packet.PacketService;
65import org.onosproject.net.provider.AbstractProvider;
66import org.onosproject.net.provider.ProviderId;
67import org.onosproject.net.topology.Topology;
68import org.onosproject.net.topology.TopologyService;
Thomas Vachuska33601602014-11-19 03:32:15 -080069import org.osgi.service.component.ComponentContext;
alshabibe1cf87d2014-10-17 09:23:50 -070070import org.slf4j.Logger;
71
alshabibe1cf87d2014-10-17 09:23:50 -070072/**
73 * Provider which uses an OpenFlow controller to detect network
74 * end-station hosts.
75 */
76@Component(immediate = true)
77public class HostLocationProvider extends AbstractProvider implements HostProvider {
78
79 private final Logger log = getLogger(getClass());
80
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080081 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
82 protected CoreService coreService;
83
84 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibe1cf87d2014-10-17 09:23:50 -070085 protected HostProviderRegistry providerRegistry;
86
87 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -080088 protected PacketService packetService;
alshabibe1cf87d2014-10-17 09:23:50 -070089
90 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
91 protected TopologyService topologyService;
92
Thomas Vachuska33601602014-11-19 03:32:15 -080093 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
94 protected HostService hostService;
95
96 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
97 protected DeviceService deviceService;
98
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070099 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
100 protected ComponentConfigService cfgService;
101
alshabibe1cf87d2014-10-17 09:23:50 -0700102 private HostProviderService providerService;
103
104 private final InternalHostProvider processor = new InternalHostProvider();
Thomas Vachuska33601602014-11-19 03:32:15 -0800105 private final DeviceListener deviceListener = new InternalDeviceListener();
106
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800107 private ApplicationId appId;
108
Thomas Vachuska33601602014-11-19 03:32:15 -0800109 @Property(name = "hostRemovalEnabled", boolValue = true,
110 label = "Enable host removal on port/device down events")
111 private boolean hostRemovalEnabled = true;
alshabibe1cf87d2014-10-17 09:23:50 -0700112
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800113 @Property(name = "ipv6NeighborDiscovery", boolValue = false,
114 label = "Enable using IPv6 Neighbor Discovery by the " +
115 "Host Location Provider; default is false")
116 private boolean ipv6NeighborDiscovery = false;
alshabibe1cf87d2014-10-17 09:23:50 -0700117
118 /**
119 * Creates an OpenFlow host provider.
120 */
121 public HostLocationProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800122 super(new ProviderId("of", "org.onosproject.provider.host"));
alshabibe1cf87d2014-10-17 09:23:50 -0700123 }
124
125 @Activate
Thomas Vachuska33601602014-11-19 03:32:15 -0800126 public void activate(ComponentContext context) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700127 cfgService.registerProperties(getClass());
128 appId = coreService.registerApplication("org.onosproject.provider.host");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800129 readComponentConfiguration(context);
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800130
alshabibe1cf87d2014-10-17 09:23:50 -0700131 providerService = providerRegistry.register(this);
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800132 packetService.addProcessor(processor, 1);
Thomas Vachuska33601602014-11-19 03:32:15 -0800133 deviceService.addListener(deviceListener);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800134
135 TrafficSelector.Builder selectorBuilder =
136 DefaultTrafficSelector.builder();
137 selectorBuilder.matchEthType(Ethernet.TYPE_ARP);
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800138 packetService.requestPackets(selectorBuilder.build(),
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800139 PacketPriority.CONTROL, appId);
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800140
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800141 if (ipv6NeighborDiscovery) {
142 // IPv6 Neighbor Solicitation packet.
143 selectorBuilder = DefaultTrafficSelector.builder();
144 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
145 selectorBuilder.matchIPProtocol(IPv6.PROTOCOL_ICMP6);
146 selectorBuilder.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION);
147 packetService.requestPackets(selectorBuilder.build(),
148 PacketPriority.CONTROL, appId);
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800149
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800150 // IPv6 Neighbor Advertisement packet.
151 selectorBuilder = DefaultTrafficSelector.builder();
152 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
153 selectorBuilder.matchIPProtocol(IPv6.PROTOCOL_ICMP6);
154 selectorBuilder.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT);
155 packetService.requestPackets(selectorBuilder.build(),
156 PacketPriority.CONTROL, appId);
157 }
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800158
159 log.info("Started with Application ID {}", appId.id());
alshabibe1cf87d2014-10-17 09:23:50 -0700160 }
161
162 @Deactivate
163 public void deactivate() {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700164 cfgService.unregisterProperties(getClass(), false);
alshabibe1cf87d2014-10-17 09:23:50 -0700165 providerRegistry.unregister(this);
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800166 packetService.removeProcessor(processor);
alshabibbfc6b722014-11-29 12:53:48 -0800167 deviceService.removeListener(deviceListener);
alshabibe1cf87d2014-10-17 09:23:50 -0700168 providerService = null;
169 log.info("Stopped");
170 }
171
Thomas Vachuska33601602014-11-19 03:32:15 -0800172 @Modified
173 public void modified(ComponentContext context) {
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800174 readComponentConfiguration(context);
175 }
176
177 /**
178 * Extracts properties from the component configuration context.
179 *
180 * @param context the component context
181 */
182 private void readComponentConfiguration(ComponentContext context) {
183 Dictionary<?, ?> properties = context.getProperties();
184 Boolean flag;
185
186 flag = isPropertyEnabled(properties, "hostRemovalEnabled");
187 if (flag == null) {
188 log.info("Host removal on port/device down events is not configured, " +
189 "using current value of {}", hostRemovalEnabled);
190 } else {
191 hostRemovalEnabled = flag;
192 log.info("Configured. Host removal on port/device down events is {}",
193 hostRemovalEnabled ? "enabled" : "disabled");
Thomas Vachuska33601602014-11-19 03:32:15 -0800194 }
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800195
196 flag = isPropertyEnabled(properties, "ipv6NeighborDiscovery");
197 if (flag == null) {
198 log.info("Using IPv6 Neighbor Discovery is not configured, " +
199 "using current value of {}", ipv6NeighborDiscovery);
200 } else {
201 ipv6NeighborDiscovery = flag;
202 log.info("Configured. Using IPv6 Neighbor Discovery is {}",
203 ipv6NeighborDiscovery ? "enabled" : "disabled");
204 }
205 }
206
207 /**
208 * Check property name is defined and set to true.
209 *
210 * @param properties properties to be looked up
211 * @param propertyName the name of the property to look up
212 * @return value when the propertyName is defined or return null
213 */
214 private static Boolean isPropertyEnabled(Dictionary<?, ?> properties,
215 String propertyName) {
216 Boolean value = null;
217 try {
218 String s = (String) properties.get(propertyName);
219 value = isNullOrEmpty(s) ? null : s.trim().equals("true");
220 } catch (ClassCastException e) {
221 // No propertyName defined.
222 value = null;
223 }
224 return value;
Thomas Vachuska33601602014-11-19 03:32:15 -0800225 }
226
alshabibe1cf87d2014-10-17 09:23:50 -0700227 @Override
228 public void triggerProbe(Host host) {
229 log.info("Triggering probe on device {}", host);
230 }
231
232 private class InternalHostProvider implements PacketProcessor {
233
234 @Override
235 public void process(PacketContext context) {
alshabib4a179dc2014-10-17 17:17:01 -0700236 if (context == null) {
237 return;
238 }
alshabibe1cf87d2014-10-17 09:23:50 -0700239 Ethernet eth = context.inPacket().parsed();
240
Jonathan Harte8600eb2015-01-12 10:30:45 -0800241 if (eth == null) {
242 return;
243 }
244
alshabibe1cf87d2014-10-17 09:23:50 -0700245 VlanId vlan = VlanId.vlanId(eth.getVlanID());
246 ConnectPoint heardOn = context.inPacket().receivedFrom();
247
248 // If this is not an edge port, bail out.
249 Topology topology = topologyService.currentTopology();
250 if (topologyService.isInfrastructure(topology, heardOn)) {
251 return;
252 }
253
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800254 HostLocation hloc =
255 new HostLocation(heardOn, System.currentTimeMillis());
alshabibe1cf87d2014-10-17 09:23:50 -0700256
257 HostId hid = HostId.hostId(eth.getSourceMAC(), vlan);
258
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800259 // ARP: possible new hosts, update both location and IP
alshabibe1cf87d2014-10-17 09:23:50 -0700260 if (eth.getEtherType() == Ethernet.TYPE_ARP) {
261 ARP arp = (ARP) eth.getPayload();
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800262 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET,
263 arp.getSenderProtocolAddress());
alshabibe1cf87d2014-10-17 09:23:50 -0700264 HostDescription hdescr =
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800265 new DefaultHostDescription(eth.getSourceMAC(), vlan,
266 hloc, ip);
alshabibe1cf87d2014-10-17 09:23:50 -0700267 providerService.hostDetected(hid, hdescr);
268
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800269 // IPv4: update location only
alshabibe1cf87d2014-10-17 09:23:50 -0700270 } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) {
alshabibe1cf87d2014-10-17 09:23:50 -0700271 HostDescription hdescr =
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800272 new DefaultHostDescription(eth.getSourceMAC(), vlan, hloc);
alshabibe1cf87d2014-10-17 09:23:50 -0700273 providerService.hostDetected(hid, hdescr);
274
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800275 //
276 // NeighborAdvertisement and NeighborSolicitation: possible
277 // new hosts, update both location and IP.
278 //
279 // IPv6: update location only
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800280 } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
281 IpAddress ip = null;
282 IPv6 ipv6 = (IPv6) eth.getPayload();
283
284 IPacket iPkt = ipv6;
285 while (iPkt != null) {
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800286 // Ignore Router Solicitation and Advertisement
287 if (iPkt instanceof RouterAdvertisement ||
288 iPkt instanceof RouterSolicitation) {
289 return;
290 }
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800291 if (iPkt instanceof NeighborAdvertisement ||
292 iPkt instanceof NeighborSolicitation) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800293 IpAddress sourceAddress =
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800294 IpAddress.valueOf(IpAddress.Version.INET6,
295 ipv6.getSourceAddress());
296 // Ignore DAD packets, in which source address is zero
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800297 if (sourceAddress.isZero()) {
298 return;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800299 }
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800300 ip = sourceAddress;
301 break;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800302 }
303 iPkt = iPkt.getPayload();
304 }
305 HostDescription hdescr = (ip == null) ?
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800306 new DefaultHostDescription(eth.getSourceMAC(), vlan,
307 hloc) :
308 new DefaultHostDescription(eth.getSourceMAC(), vlan,
309 hloc, ip);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800310 providerService.hostDetected(hid, hdescr);
alshabibe1cf87d2014-10-17 09:23:50 -0700311 }
312 }
313 }
Thomas Vachuska33601602014-11-19 03:32:15 -0800314
315 // Auxiliary listener to device events.
316 private class InternalDeviceListener implements DeviceListener {
317 @Override
318 public void event(DeviceEvent event) {
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800319 Device device = event.subject();
320 switch (event.type()) {
321 case DEVICE_ADDED:
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800322 break;
323 case DEVICE_AVAILABILITY_CHANGED:
324 if (hostRemovalEnabled &&
325 !deviceService.isAvailable(device.id())) {
326 removeHosts(hostService.getConnectedHosts(device.id()));
Thomas Vachuska33601602014-11-19 03:32:15 -0800327 }
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800328 break;
329 case DEVICE_SUSPENDED:
330 case DEVICE_UPDATED:
331 // Nothing to do?
332 break;
333 case DEVICE_REMOVED:
334 if (hostRemovalEnabled) {
335 removeHosts(hostService.getConnectedHosts(device.id()));
336 }
337 break;
338 case PORT_ADDED:
339 break;
340 case PORT_UPDATED:
341 if (hostRemovalEnabled) {
342 ConnectPoint point =
343 new ConnectPoint(device.id(), event.port().number());
344 removeHosts(hostService.getConnectedHosts(point));
345 }
346 break;
347 case PORT_REMOVED:
348 // Nothing to do?
349 break;
350 default:
351 break;
Thomas Vachuska33601602014-11-19 03:32:15 -0800352 }
353 }
354 }
355
356 // Signals host vanish for all specified hosts.
357 private void removeHosts(Set<Host> hosts) {
358 for (Host host : hosts) {
359 providerService.hostVanished(host.id());
360 }
361 }
362
alshabibe1cf87d2014-10-17 09:23:50 -0700363}