blob: 44f8cbf00044cdcbe294c4d21261d331d675eeac [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
4 * 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
7 *
8 * 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.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.host.impl;
Jonathan Hartfca736c2014-09-19 17:26:59 -070017
Jonathan Hartfca736c2014-09-19 17:26:59 -070018import org.jboss.netty.util.Timeout;
19import org.jboss.netty.util.TimerTask;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080020import org.onlab.packet.ARP;
21import org.onlab.packet.Ethernet;
Pavlin Radoslavov89edb542015-02-23 13:50:29 -080022import org.onlab.packet.ICMP6;
Pavlin Radoslavov89edb542015-02-23 13:50:29 -080023import org.onlab.packet.IPv6;
Jonathan Hart4cb39882015-08-12 23:50:55 -040024import org.onlab.packet.IpAddress;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080025import org.onlab.packet.MacAddress;
26import org.onlab.packet.VlanId;
Pavlin Radoslavov89edb542015-02-23 13:50:29 -080027import org.onlab.packet.ndp.NeighborDiscoveryOptions;
28import org.onlab.packet.ndp.NeighborSolicitation;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080029import org.onlab.util.Timer;
Jonathan Hart4cb39882015-08-12 23:50:55 -040030import org.onosproject.incubator.net.intf.Interface;
31import org.onosproject.incubator.net.intf.InterfaceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.ConnectPoint;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.Host;
Jonathan Hartfb32a6e2015-09-01 12:12:14 +020034import org.onosproject.net.edge.EdgePortService;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.flow.DefaultTrafficTreatment;
36import org.onosproject.net.flow.TrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.net.host.HostProvider;
38import org.onosproject.net.host.InterfaceIpAddress;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.packet.DefaultOutboundPacket;
40import org.onosproject.net.packet.OutboundPacket;
41import org.onosproject.net.packet.PacketService;
42import org.onosproject.net.provider.ProviderId;
Jonathan Hartfb32a6e2015-09-01 12:12:14 +020043import org.slf4j.Logger;
44import org.slf4j.LoggerFactory;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080045
46import java.nio.ByteBuffer;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080047import java.util.Collections;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080048import java.util.Set;
49import java.util.concurrent.ConcurrentHashMap;
50import java.util.concurrent.ConcurrentMap;
51import java.util.concurrent.TimeUnit;
Jonathan Hartfca736c2014-09-19 17:26:59 -070052
Jonathan Hart87fbbad2014-09-23 08:43:50 -070053/**
54 * Monitors hosts on the dataplane to detect changes in host data.
Thomas Vachuska4b420772014-10-30 16:46:17 -070055 * <p>
Jonathan Hart87fbbad2014-09-23 08:43:50 -070056 * The HostMonitor can monitor hosts that have already been detected for
57 * changes. At an application's request, it can also monitor and actively
58 * probe for hosts that have not yet been detected (specified by IP address).
Thomas Vachuska4b420772014-10-30 16:46:17 -070059 * </p>
Jonathan Hart87fbbad2014-09-23 08:43:50 -070060 */
tom202175a2014-09-19 19:00:11 -070061public class HostMonitor implements TimerTask {
Jonathan Hartfb32a6e2015-09-01 12:12:14 +020062
63 private Logger log = LoggerFactory.getLogger(getClass());
64
Jonathan Hart70da5122014-10-01 16:37:42 -070065 private PacketService packetService;
66 private HostManager hostManager;
Jonathan Hart4cb39882015-08-12 23:50:55 -040067 private InterfaceService interfaceService;
Jonathan Hartfb32a6e2015-09-01 12:12:14 +020068 private EdgePortService edgePortService;
Jonathan Hartfca736c2014-09-19 17:26:59 -070069
Jonathan Hart87fbbad2014-09-23 08:43:50 -070070 private final Set<IpAddress> monitoredAddresses;
Jonathan Hartfca736c2014-09-19 17:26:59 -070071
Jonathan Hart34ed2fd2014-10-02 19:08:55 -070072 private final ConcurrentMap<ProviderId, HostProvider> hostProviders;
Jonathan Hart70da5122014-10-01 16:37:42 -070073
Jonathan Hart34ed2fd2014-10-02 19:08:55 -070074 private static final long DEFAULT_PROBE_RATE = 30000; // milliseconds
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -080075 private static final byte[] ZERO_MAC_ADDRESS = MacAddress.ZERO.toBytes();
Jonathan Hart34ed2fd2014-10-02 19:08:55 -070076 private long probeRate = DEFAULT_PROBE_RATE;
Jonathan Hartfca736c2014-09-19 17:26:59 -070077
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070078 private Timeout timeout;
Jonathan Hartfca736c2014-09-19 17:26:59 -070079
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070080 /**
81 * Creates a new host monitor.
82 *
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070083 * @param packetService packet service used to send packets on the data plane
Jonathan Hartb4758a92014-09-24 10:46:45 -070084 * @param hostManager host manager used to look up host information and
85 * probe existing hosts
Jonathan Hart4cb39882015-08-12 23:50:55 -040086 * @param interfaceService interface service for interface information
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070087 */
Jonathan Hart4cb39882015-08-12 23:50:55 -040088 public HostMonitor(PacketService packetService, HostManager hostManager,
Jonathan Hartfb32a6e2015-09-01 12:12:14 +020089 InterfaceService interfaceService,
90 EdgePortService edgePortService) {
Jonathan Hart70da5122014-10-01 16:37:42 -070091
Jonathan Hart87fbbad2014-09-23 08:43:50 -070092 this.packetService = packetService;
Jonathan Hartb4758a92014-09-24 10:46:45 -070093 this.hostManager = hostManager;
Jonathan Hart4cb39882015-08-12 23:50:55 -040094 this.interfaceService = interfaceService;
Jonathan Hartfb32a6e2015-09-01 12:12:14 +020095 this.edgePortService = edgePortService;
Jonathan Hartfca736c2014-09-19 17:26:59 -070096
Jonathan Hart4cb39882015-08-12 23:50:55 -040097 monitoredAddresses = Collections.newSetFromMap(new ConcurrentHashMap<>());
Jonathan Hart70da5122014-10-01 16:37:42 -070098 hostProviders = new ConcurrentHashMap<>();
Jonathan Hart70da5122014-10-01 16:37:42 -070099 }
100
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -0700101 /**
102 * Adds an IP address to be monitored by the host monitor. The monitor will
103 * periodically probe the host to detect changes.
104 *
105 * @param ip IP address of the host to monitor
106 */
Jonathan Hart70da5122014-10-01 16:37:42 -0700107 void addMonitoringFor(IpAddress ip) {
Jonathan Hartfca736c2014-09-19 17:26:59 -0700108 monitoredAddresses.add(ip);
109 }
110
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -0700111 /**
112 * Stops monitoring the given IP address.
113 *
114 * @param ip IP address to stop monitoring on
115 */
Jonathan Hart70da5122014-10-01 16:37:42 -0700116 void stopMonitoring(IpAddress ip) {
Jonathan Hartfca736c2014-09-19 17:26:59 -0700117 monitoredAddresses.remove(ip);
118 }
119
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -0700120 /**
121 * Starts the host monitor. Does nothing if the monitor is already running.
122 */
123 void start() {
124 synchronized (this) {
125 if (timeout == null) {
126 timeout = Timer.getTimer().newTimeout(this, 0, TimeUnit.MILLISECONDS);
127 }
128 }
Jonathan Hartfca736c2014-09-19 17:26:59 -0700129 }
130
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -0700131 /**
132 * Stops the host monitor.
133 */
134 void shutdown() {
135 synchronized (this) {
136 timeout.cancel();
137 timeout = null;
138 }
139 }
140
141 /**
142 * Registers a host provider with the host monitor. The monitor can use the
143 * provider to probe hosts.
144 *
145 * @param provider the host provider to register
146 */
Jonathan Hart70da5122014-10-01 16:37:42 -0700147 void registerHostProvider(HostProvider provider) {
148 hostProviders.put(provider.id(), provider);
149 }
150
Jonathan Hartfca736c2014-09-19 17:26:59 -0700151 @Override
152 public void run(Timeout timeout) throws Exception {
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700153 for (IpAddress ip : monitoredAddresses) {
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700154 Set<Host> hosts = hostManager.getHostsByIp(ip);
Jonathan Hartfca736c2014-09-19 17:26:59 -0700155
156 if (hosts.isEmpty()) {
Jonathan Hart39ee6482015-08-31 16:00:19 +0200157 sendRequest(ip);
Jonathan Hartfca736c2014-09-19 17:26:59 -0700158 } else {
159 for (Host host : hosts) {
Jonathan Hart70da5122014-10-01 16:37:42 -0700160 HostProvider provider = hostProviders.get(host.providerId());
Jonathan Hart34ed2fd2014-10-02 19:08:55 -0700161 if (provider == null) {
162 hostProviders.remove(host.providerId(), null);
163 } else {
Jonathan Hart70da5122014-10-01 16:37:42 -0700164 provider.triggerProbe(host);
165 }
Jonathan Hartfca736c2014-09-19 17:26:59 -0700166 }
167 }
168 }
169
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -0700170 this.timeout = Timer.getTimer().newTimeout(this, probeRate, TimeUnit.MILLISECONDS);
Jonathan Hartfca736c2014-09-19 17:26:59 -0700171 }
172
173 /**
Jonathan Hart39ee6482015-08-31 16:00:19 +0200174 * Sends an ARP or NDP request for the given IP address.
Jonathan Hartfca736c2014-09-19 17:26:59 -0700175 *
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800176 * @param targetIp IP address to send the request for
Jonathan Hartfca736c2014-09-19 17:26:59 -0700177 */
Jonathan Hart39ee6482015-08-31 16:00:19 +0200178 private void sendRequest(IpAddress targetIp) {
Jonathan Hart4cb39882015-08-12 23:50:55 -0400179 Interface intf = interfaceService.getMatchingInterface(targetIp);
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700180
Jonathan Hart4cb39882015-08-12 23:50:55 -0400181 if (intf == null) {
182 return;
183 }
184
Jonathan Hartfb32a6e2015-09-01 12:12:14 +0200185 if (!edgePortService.isEdgePoint(intf.connectPoint())) {
186 log.warn("Attempt to send probe out non-edge port: {}", intf);
187 return;
188 }
189
Jonathan Hart4cb39882015-08-12 23:50:55 -0400190 for (InterfaceIpAddress ia : intf.ipAddresses()) {
191 if (ia.subnetAddress().contains(targetIp)) {
Jonathan Hart39ee6482015-08-31 16:00:19 +0200192 sendProbe(intf.connectPoint(), targetIp, ia.ipAddress(),
Jonathan Hart4cb39882015-08-12 23:50:55 -0400193 intf.mac(), intf.vlan());
Jonathan Hartfca736c2014-09-19 17:26:59 -0700194 }
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700195 }
Jonathan Hartfca736c2014-09-19 17:26:59 -0700196 }
197
Jonathan Hart39ee6482015-08-31 16:00:19 +0200198 private void sendProbe(ConnectPoint connectPoint,
199 IpAddress targetIp,
200 IpAddress sourceIp, MacAddress sourceMac,
201 VlanId vlan) {
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800202 Ethernet probePacket = null;
203
Pavlin Radoslavov87dd9302015-03-10 13:53:24 -0700204 if (targetIp.isIp4()) {
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800205 // IPv4: Use ARP
Jonathan Hart39ee6482015-08-31 16:00:19 +0200206 probePacket = buildArpRequest(targetIp, sourceIp, sourceMac, vlan);
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800207 } else {
208 // IPv6: Use Neighbor Discovery
Jonathan Hart39ee6482015-08-31 16:00:19 +0200209 probePacket = buildNdpRequest(targetIp, sourceIp, sourceMac, vlan);
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800210 }
Jonathan Hartfca736c2014-09-19 17:26:59 -0700211
tom9a693fd2014-10-03 11:32:19 -0700212 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
Jonathan Hart4cb39882015-08-12 23:50:55 -0400213 .setOutput(connectPoint.port())
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800214 .build();
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700215
216 OutboundPacket outboundPacket =
Jonathan Hart4cb39882015-08-12 23:50:55 -0400217 new DefaultOutboundPacket(connectPoint.deviceId(), treatment,
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800218 ByteBuffer.wrap(probePacket.serialize()));
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700219
220 packetService.emit(outboundPacket);
221 }
222
Jonathan Hart70da5122014-10-01 16:37:42 -0700223 private Ethernet buildArpRequest(IpAddress targetIp, IpAddress sourceIp,
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800224 MacAddress sourceMac, VlanId vlan) {
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700225
226 ARP arp = new ARP();
227 arp.setHardwareType(ARP.HW_TYPE_ETHERNET)
Jonathan Hart70da5122014-10-01 16:37:42 -0700228 .setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH)
229 .setProtocolType(ARP.PROTO_TYPE_IP)
Pavlin Radoslavov52307e62014-10-29 15:07:37 -0700230 .setProtocolAddressLength((byte) IpAddress.INET_BYTE_LENGTH)
Jonathan Hart70da5122014-10-01 16:37:42 -0700231 .setOpCode(ARP.OP_REQUEST);
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700232
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800233 arp.setSenderHardwareAddress(sourceMac.toBytes())
Jonathan Hart70da5122014-10-01 16:37:42 -0700234 .setSenderProtocolAddress(sourceIp.toOctets())
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800235 .setTargetHardwareAddress(ZERO_MAC_ADDRESS)
Jonathan Hart70da5122014-10-01 16:37:42 -0700236 .setTargetProtocolAddress(targetIp.toOctets());
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700237
238 Ethernet ethernet = new Ethernet();
239 ethernet.setEtherType(Ethernet.TYPE_ARP)
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800240 .setDestinationMACAddress(MacAddress.BROADCAST)
241 .setSourceMACAddress(sourceMac)
Jonathan Hart70da5122014-10-01 16:37:42 -0700242 .setPayload(arp);
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700243
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800244 if (!vlan.equals(VlanId.NONE)) {
245 ethernet.setVlanID(vlan.toShort());
246 }
247
alshabibaf734ff2015-07-01 16:35:26 -0700248 ethernet.setPad(true);
249
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700250 return ethernet;
Jonathan Hartfca736c2014-09-19 17:26:59 -0700251 }
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800252
253 private Ethernet buildNdpRequest(IpAddress targetIp, IpAddress sourceIp,
254 MacAddress sourceMac, VlanId vlan) {
255
256 // Create the Ethernet packet
257 Ethernet ethernet = new Ethernet();
258 ethernet.setEtherType(Ethernet.TYPE_IPV6)
259 .setDestinationMACAddress(MacAddress.BROADCAST)
260 .setSourceMACAddress(sourceMac);
261 if (!vlan.equals(VlanId.NONE)) {
262 ethernet.setVlanID(vlan.toShort());
263 }
264
265 //
266 // Create the IPv6 packet
267 //
268 // TODO: The destination IP address should be the
269 // solicited-node multicast address
270 IPv6 ipv6 = new IPv6();
271 ipv6.setSourceAddress(sourceIp.toOctets());
272 ipv6.setDestinationAddress(targetIp.toOctets());
273 ipv6.setHopLimit((byte) 255);
274
275 // Create the ICMPv6 packet
276 ICMP6 icmp6 = new ICMP6();
277 icmp6.setIcmpType(ICMP6.NEIGHBOR_SOLICITATION);
278 icmp6.setIcmpCode((byte) 0);
279
Jonathan Hart39ee6482015-08-31 16:00:19 +0200280 // Create the Neighbor Solicitation packet
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800281 NeighborSolicitation ns = new NeighborSolicitation();
282 ns.setTargetAddress(targetIp.toOctets());
283 ns.addOption(NeighborDiscoveryOptions.TYPE_SOURCE_LL_ADDRESS,
284 sourceMac.toBytes());
285
286 icmp6.setPayload(ns);
287 ipv6.setPayload(icmp6);
288 ethernet.setPayload(ipv6);
289
290 return ethernet;
291 }
Jonathan Hartfca736c2014-09-19 17:26:59 -0700292}