blob: 2150bf1f19d04f1a142937754a56d058e30028b4 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present 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 Hart6cd2f352015-01-13 17:44:45 -080018import org.onlab.packet.ARP;
19import org.onlab.packet.Ethernet;
Pavlin Radoslavov89edb542015-02-23 13:50:29 -080020import org.onlab.packet.IPv6;
Charles Chana9458532017-05-26 11:55:12 -070021import org.onlab.packet.Ip4Address;
22import org.onlab.packet.Ip6Address;
Jonathan Hart4cb39882015-08-12 23:50:55 -040023import org.onlab.packet.IpAddress;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080024import org.onlab.packet.MacAddress;
25import org.onlab.packet.VlanId;
Pavlin Radoslavov89edb542015-02-23 13:50:29 -080026import org.onlab.packet.ndp.NeighborSolicitation;
Yuta HIGUCHI19afc032017-05-20 23:44:17 -070027import org.onlab.util.SharedScheduledExecutors;
Jonathan Hart4cb39882015-08-12 23:50:55 -040028import org.onosproject.incubator.net.intf.InterfaceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.ConnectPoint;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.Host;
Jonathan Hartfb32a6e2015-09-01 12:12:14 +020031import org.onosproject.net.edge.EdgePortService;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.flow.DefaultTrafficTreatment;
33import org.onosproject.net.flow.TrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.host.HostProvider;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.packet.DefaultOutboundPacket;
36import org.onosproject.net.packet.OutboundPacket;
37import org.onosproject.net.packet.PacketService;
38import org.onosproject.net.provider.ProviderId;
Jonathan Hartfb32a6e2015-09-01 12:12:14 +020039import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080041
42import java.nio.ByteBuffer;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080043import java.util.Collections;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080044import java.util.Set;
45import java.util.concurrent.ConcurrentHashMap;
46import java.util.concurrent.ConcurrentMap;
Yuta HIGUCHI19afc032017-05-20 23:44:17 -070047import java.util.concurrent.ScheduledFuture;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080048import java.util.concurrent.TimeUnit;
Jonathan Hartfca736c2014-09-19 17:26:59 -070049
Jonathan Hart87fbbad2014-09-23 08:43:50 -070050/**
51 * Monitors hosts on the dataplane to detect changes in host data.
Thomas Vachuska4b420772014-10-30 16:46:17 -070052 * <p>
Jonathan Hart87fbbad2014-09-23 08:43:50 -070053 * The HostMonitor can monitor hosts that have already been detected for
54 * changes. At an application's request, it can also monitor and actively
55 * probe for hosts that have not yet been detected (specified by IP address).
Thomas Vachuska4b420772014-10-30 16:46:17 -070056 * </p>
Jonathan Hart87fbbad2014-09-23 08:43:50 -070057 */
Yuta HIGUCHI19afc032017-05-20 23:44:17 -070058public class HostMonitor implements Runnable {
Jonathan Hartfb32a6e2015-09-01 12:12:14 +020059
60 private Logger log = LoggerFactory.getLogger(getClass());
61
Jonathan Hart70da5122014-10-01 16:37:42 -070062 private PacketService packetService;
63 private HostManager hostManager;
Jonathan Hart4cb39882015-08-12 23:50:55 -040064 private InterfaceService interfaceService;
Jonathan Hartfb32a6e2015-09-01 12:12:14 +020065 private EdgePortService edgePortService;
Jonathan Hartfca736c2014-09-19 17:26:59 -070066
Jonathan Hart87fbbad2014-09-23 08:43:50 -070067 private final Set<IpAddress> monitoredAddresses;
Jonathan Hartfca736c2014-09-19 17:26:59 -070068
Jonathan Hart34ed2fd2014-10-02 19:08:55 -070069 private final ConcurrentMap<ProviderId, HostProvider> hostProviders;
Jonathan Hart70da5122014-10-01 16:37:42 -070070
Jonathan Hart34ed2fd2014-10-02 19:08:55 -070071 private static final long DEFAULT_PROBE_RATE = 30000; // milliseconds
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -080072 private static final byte[] ZERO_MAC_ADDRESS = MacAddress.ZERO.toBytes();
Jonathan Hart34ed2fd2014-10-02 19:08:55 -070073 private long probeRate = DEFAULT_PROBE_RATE;
Jonathan Hartfca736c2014-09-19 17:26:59 -070074
Yuta HIGUCHI19afc032017-05-20 23:44:17 -070075 private ScheduledFuture<?> timeout;
Jonathan Hartfca736c2014-09-19 17:26:59 -070076
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070077 /**
78 * Creates a new host monitor.
79 *
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070080 * @param packetService packet service used to send packets on the data plane
Jonathan Hartb4758a92014-09-24 10:46:45 -070081 * @param hostManager host manager used to look up host information and
82 * probe existing hosts
Jonathan Hart4cb39882015-08-12 23:50:55 -040083 * @param interfaceService interface service for interface information
Thomas Vachuska0ae45602015-09-22 17:06:19 -070084 * @param edgePortService edge port service
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070085 */
Jonathan Hart4cb39882015-08-12 23:50:55 -040086 public HostMonitor(PacketService packetService, HostManager hostManager,
Jonathan Hartfb32a6e2015-09-01 12:12:14 +020087 InterfaceService interfaceService,
88 EdgePortService edgePortService) {
Jonathan Hart70da5122014-10-01 16:37:42 -070089
Jonathan Hart87fbbad2014-09-23 08:43:50 -070090 this.packetService = packetService;
Jonathan Hartb4758a92014-09-24 10:46:45 -070091 this.hostManager = hostManager;
Jonathan Hart4cb39882015-08-12 23:50:55 -040092 this.interfaceService = interfaceService;
Jonathan Hartfb32a6e2015-09-01 12:12:14 +020093 this.edgePortService = edgePortService;
Jonathan Hartfca736c2014-09-19 17:26:59 -070094
Jonathan Hart4cb39882015-08-12 23:50:55 -040095 monitoredAddresses = Collections.newSetFromMap(new ConcurrentHashMap<>());
Jonathan Hart70da5122014-10-01 16:37:42 -070096 hostProviders = new ConcurrentHashMap<>();
Jonathan Hart70da5122014-10-01 16:37:42 -070097 }
98
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070099 /**
100 * Adds an IP address to be monitored by the host monitor. The monitor will
101 * periodically probe the host to detect changes.
102 *
103 * @param ip IP address of the host to monitor
104 */
Jonathan Hart70da5122014-10-01 16:37:42 -0700105 void addMonitoringFor(IpAddress ip) {
Jonathan Hartf4edb312017-03-24 10:43:10 -0700106 if (monitoredAddresses.add(ip)) {
107 probe(ip);
108 }
Jonathan Hartfca736c2014-09-19 17:26:59 -0700109 }
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) {
Yuta HIGUCHI19afc032017-05-20 23:44:17 -0700126 timeout = SharedScheduledExecutors.newTimeout(this, 0, TimeUnit.MILLISECONDS);
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -0700127 }
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) {
Yuta HIGUCHI19afc032017-05-20 23:44:17 -0700136 timeout.cancel(true);
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -0700137 timeout = null;
138 }
139 }
140
sdn94b00152016-08-30 02:12:32 -0700141 /*
142 * Sets the probe rate.
143 */
144 void setProbeRate(long probeRate) {
145 this.probeRate = probeRate;
146 }
147
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -0700148 /**
149 * Registers a host provider with the host monitor. The monitor can use the
150 * provider to probe hosts.
151 *
152 * @param provider the host provider to register
153 */
Jonathan Hart70da5122014-10-01 16:37:42 -0700154 void registerHostProvider(HostProvider provider) {
155 hostProviders.put(provider.id(), provider);
156 }
157
Jonathan Hartfca736c2014-09-19 17:26:59 -0700158 @Override
Yuta HIGUCHI19afc032017-05-20 23:44:17 -0700159 public void run() {
Jonathan Hart78613d22016-07-27 11:25:29 -0700160 monitoredAddresses.forEach(this::probe);
Jonathan Hartfca736c2014-09-19 17:26:59 -0700161
Satish Ke9d748f2015-11-24 19:05:01 +0530162 synchronized (this) {
Yuta HIGUCHI19afc032017-05-20 23:44:17 -0700163 this.timeout = SharedScheduledExecutors.newTimeout(this, probeRate, TimeUnit.MILLISECONDS);
Satish Ke9d748f2015-11-24 19:05:01 +0530164 }
Jonathan Hartfca736c2014-09-19 17:26:59 -0700165 }
166
Jonathan Hart78613d22016-07-27 11:25:29 -0700167 private void probe(IpAddress ip) {
168 Set<Host> hosts = hostManager.getHostsByIp(ip);
169
170 if (hosts.isEmpty()) {
171 sendRequest(ip);
172 } else {
173 for (Host host : hosts) {
174 HostProvider provider = hostProviders.get(host.providerId());
175 if (provider == null) {
176 hostProviders.remove(host.providerId(), null);
177 } else {
178 provider.triggerProbe(host);
179 }
180 }
181 }
182 }
183
Jonathan Hartfca736c2014-09-19 17:26:59 -0700184 /**
Jonathan Hart39ee6482015-08-31 16:00:19 +0200185 * Sends an ARP or NDP request for the given IP address.
Jonathan Hartfca736c2014-09-19 17:26:59 -0700186 *
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800187 * @param targetIp IP address to send the request for
Jonathan Hartfca736c2014-09-19 17:26:59 -0700188 */
Jonathan Hart39ee6482015-08-31 16:00:19 +0200189 private void sendRequest(IpAddress targetIp) {
Charles Chan9238b382017-03-14 13:14:58 -0700190 interfaceService.getMatchingInterfaces(targetIp).forEach(intf -> {
191 if (!edgePortService.isEdgePoint(intf.connectPoint())) {
192 log.warn("Aborting attempt to send probe out non-edge port: {}", intf);
193 return;
Jonathan Hartfca736c2014-09-19 17:26:59 -0700194 }
Charles Chan9238b382017-03-14 13:14:58 -0700195
196 intf.ipAddressesList().stream()
197 .filter(ia -> ia.subnetAddress().contains(targetIp))
198 .forEach(ia -> {
Charles Chana9458532017-05-26 11:55:12 -0700199 // Use DAD to probe when interface MAC is not supplied,
200 // such that host will not learn ONOS dummy MAC from the probe.
201 IpAddress sourceIp;
202 if (!MacAddress.NONE.equals(intf.mac())) {
203 sourceIp = ia.ipAddress();
204 } else {
205 sourceIp = targetIp.isIp4() ? Ip4Address.ZERO : Ip6Address.ZERO;
206 }
207
Jonathan Hartf4edb312017-03-24 10:43:10 -0700208 log.debug("Sending probe for target:{} out of intf:{} vlan:{}",
Charles Chan9238b382017-03-14 13:14:58 -0700209 targetIp, intf.connectPoint(), intf.vlan());
Charles Chana9458532017-05-26 11:55:12 -0700210 sendProbe(intf.connectPoint(), targetIp, sourceIp,
Charles Chan9238b382017-03-14 13:14:58 -0700211 intf.mac(), intf.vlan());
212 // account for use-cases where tagged-vlan config is used
213 if (!intf.vlanTagged().isEmpty()) {
214 intf.vlanTagged().forEach(tag -> {
Jonathan Hartf4edb312017-03-24 10:43:10 -0700215 log.debug("Sending probe for target:{} out of intf:{} vlan:{}",
Charles Chan9238b382017-03-14 13:14:58 -0700216 targetIp, intf.connectPoint(), tag);
Charles Chana9458532017-05-26 11:55:12 -0700217 sendProbe(intf.connectPoint(), targetIp, sourceIp,
Charles Chan9238b382017-03-14 13:14:58 -0700218 intf.mac(), tag);
219 });
220 }
221 });
222 });
Jonathan Hartfca736c2014-09-19 17:26:59 -0700223 }
224
Pier Luigi9b1d6262017-02-02 22:31:34 -0800225 public void sendProbe(ConnectPoint connectPoint,
226 IpAddress targetIp,
227 IpAddress sourceIp,
228 MacAddress sourceMac,
229 VlanId vlan) {
Pier Ventre78e73f62016-12-02 19:59:28 -0800230 Ethernet probePacket;
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800231
Pavlin Radoslavov87dd9302015-03-10 13:53:24 -0700232 if (targetIp.isIp4()) {
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800233 // IPv4: Use ARP
Jonathan Hart39ee6482015-08-31 16:00:19 +0200234 probePacket = buildArpRequest(targetIp, sourceIp, sourceMac, vlan);
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800235 } else {
Pier Luigi9b1d6262017-02-02 22:31:34 -0800236 // IPv6: Use Neighbor Discovery. According to the NDP protocol,
237 // we should use the solicitation node address as IPv6 destination
238 // and the multicast mac address as Ethernet destination.
Pier Luigi37b687b2017-01-16 22:46:33 -0800239 byte[] destIp = IPv6.getSolicitNodeAddress(targetIp.toOctets());
Pier Ventre78e73f62016-12-02 19:59:28 -0800240 probePacket = NeighborSolicitation.buildNdpSolicit(
241 targetIp.toOctets(),
242 sourceIp.toOctets(),
243 destIp,
244 sourceMac.toBytes(),
Pier Luigi37b687b2017-01-16 22:46:33 -0800245 IPv6.getMCastMacAddress(destIp),
Pier Ventre78e73f62016-12-02 19:59:28 -0800246 vlan
247 );
248 }
249
250 if (probePacket == null) {
251 log.warn("Not able to build the probe packet");
252 return;
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800253 }
Jonathan Hartfca736c2014-09-19 17:26:59 -0700254
tom9a693fd2014-10-03 11:32:19 -0700255 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
Jonathan Hart4cb39882015-08-12 23:50:55 -0400256 .setOutput(connectPoint.port())
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800257 .build();
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700258
259 OutboundPacket outboundPacket =
Jonathan Hart4cb39882015-08-12 23:50:55 -0400260 new DefaultOutboundPacket(connectPoint.deviceId(), treatment,
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800261 ByteBuffer.wrap(probePacket.serialize()));
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700262
263 packetService.emit(outboundPacket);
264 }
265
Jonathan Hart70da5122014-10-01 16:37:42 -0700266 private Ethernet buildArpRequest(IpAddress targetIp, IpAddress sourceIp,
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800267 MacAddress sourceMac, VlanId vlan) {
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700268
269 ARP arp = new ARP();
270 arp.setHardwareType(ARP.HW_TYPE_ETHERNET)
Jonathan Hart70da5122014-10-01 16:37:42 -0700271 .setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH)
272 .setProtocolType(ARP.PROTO_TYPE_IP)
Pavlin Radoslavov52307e62014-10-29 15:07:37 -0700273 .setProtocolAddressLength((byte) IpAddress.INET_BYTE_LENGTH)
Jonathan Hart70da5122014-10-01 16:37:42 -0700274 .setOpCode(ARP.OP_REQUEST);
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700275
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800276 arp.setSenderHardwareAddress(sourceMac.toBytes())
Jonathan Hart70da5122014-10-01 16:37:42 -0700277 .setSenderProtocolAddress(sourceIp.toOctets())
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800278 .setTargetHardwareAddress(ZERO_MAC_ADDRESS)
Jonathan Hart70da5122014-10-01 16:37:42 -0700279 .setTargetProtocolAddress(targetIp.toOctets());
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700280
281 Ethernet ethernet = new Ethernet();
282 ethernet.setEtherType(Ethernet.TYPE_ARP)
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800283 .setDestinationMACAddress(MacAddress.BROADCAST)
284 .setSourceMACAddress(sourceMac)
Jonathan Hart70da5122014-10-01 16:37:42 -0700285 .setPayload(arp);
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700286
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800287 if (!vlan.equals(VlanId.NONE)) {
288 ethernet.setVlanID(vlan.toShort());
289 }
290
alshabibaf734ff2015-07-01 16:35:26 -0700291 ethernet.setPad(true);
292
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700293 return ethernet;
Jonathan Hartfca736c2014-09-19 17:26:59 -0700294 }
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800295
Jonathan Hartfca736c2014-09-19 17:26:59 -0700296}