blob: 2ef63552b7b156697fa64caf30320d602129592f [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 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.IPv6;
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;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080027import org.onlab.util.Timer;
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;
47import java.util.concurrent.TimeUnit;
Jonathan Hartfca736c2014-09-19 17:26:59 -070048
Jonathan Hart87fbbad2014-09-23 08:43:50 -070049/**
50 * Monitors hosts on the dataplane to detect changes in host data.
Thomas Vachuska4b420772014-10-30 16:46:17 -070051 * <p>
Jonathan Hart87fbbad2014-09-23 08:43:50 -070052 * The HostMonitor can monitor hosts that have already been detected for
53 * changes. At an application's request, it can also monitor and actively
54 * probe for hosts that have not yet been detected (specified by IP address).
Thomas Vachuska4b420772014-10-30 16:46:17 -070055 * </p>
Jonathan Hart87fbbad2014-09-23 08:43:50 -070056 */
tom202175a2014-09-19 19:00:11 -070057public class HostMonitor implements TimerTask {
Jonathan Hartfb32a6e2015-09-01 12:12:14 +020058
59 private Logger log = LoggerFactory.getLogger(getClass());
60
Jonathan Hart70da5122014-10-01 16:37:42 -070061 private PacketService packetService;
62 private HostManager hostManager;
Jonathan Hart4cb39882015-08-12 23:50:55 -040063 private InterfaceService interfaceService;
Jonathan Hartfb32a6e2015-09-01 12:12:14 +020064 private EdgePortService edgePortService;
Jonathan Hartfca736c2014-09-19 17:26:59 -070065
Jonathan Hart87fbbad2014-09-23 08:43:50 -070066 private final Set<IpAddress> monitoredAddresses;
Jonathan Hartfca736c2014-09-19 17:26:59 -070067
Jonathan Hart34ed2fd2014-10-02 19:08:55 -070068 private final ConcurrentMap<ProviderId, HostProvider> hostProviders;
Jonathan Hart70da5122014-10-01 16:37:42 -070069
Jonathan Hart34ed2fd2014-10-02 19:08:55 -070070 private static final long DEFAULT_PROBE_RATE = 30000; // milliseconds
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -080071 private static final byte[] ZERO_MAC_ADDRESS = MacAddress.ZERO.toBytes();
Jonathan Hart34ed2fd2014-10-02 19:08:55 -070072 private long probeRate = DEFAULT_PROBE_RATE;
Jonathan Hartfca736c2014-09-19 17:26:59 -070073
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070074 private Timeout timeout;
Jonathan Hartfca736c2014-09-19 17:26:59 -070075
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070076 /**
77 * Creates a new host monitor.
78 *
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070079 * @param packetService packet service used to send packets on the data plane
Jonathan Hartb4758a92014-09-24 10:46:45 -070080 * @param hostManager host manager used to look up host information and
81 * probe existing hosts
Jonathan Hart4cb39882015-08-12 23:50:55 -040082 * @param interfaceService interface service for interface information
Thomas Vachuska0ae45602015-09-22 17:06:19 -070083 * @param edgePortService edge port service
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070084 */
Jonathan Hart4cb39882015-08-12 23:50:55 -040085 public HostMonitor(PacketService packetService, HostManager hostManager,
Jonathan Hartfb32a6e2015-09-01 12:12:14 +020086 InterfaceService interfaceService,
87 EdgePortService edgePortService) {
Jonathan Hart70da5122014-10-01 16:37:42 -070088
Jonathan Hart87fbbad2014-09-23 08:43:50 -070089 this.packetService = packetService;
Jonathan Hartb4758a92014-09-24 10:46:45 -070090 this.hostManager = hostManager;
Jonathan Hart4cb39882015-08-12 23:50:55 -040091 this.interfaceService = interfaceService;
Jonathan Hartfb32a6e2015-09-01 12:12:14 +020092 this.edgePortService = edgePortService;
Jonathan Hartfca736c2014-09-19 17:26:59 -070093
Jonathan Hart4cb39882015-08-12 23:50:55 -040094 monitoredAddresses = Collections.newSetFromMap(new ConcurrentHashMap<>());
Jonathan Hart70da5122014-10-01 16:37:42 -070095 hostProviders = new ConcurrentHashMap<>();
Jonathan Hart70da5122014-10-01 16:37:42 -070096 }
97
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070098 /**
99 * Adds an IP address to be monitored by the host monitor. The monitor will
100 * periodically probe the host to detect changes.
101 *
102 * @param ip IP address of the host to monitor
103 */
Jonathan Hart70da5122014-10-01 16:37:42 -0700104 void addMonitoringFor(IpAddress ip) {
Jonathan Hartf4edb312017-03-24 10:43:10 -0700105 if (monitoredAddresses.add(ip)) {
106 probe(ip);
107 }
Jonathan Hartfca736c2014-09-19 17:26:59 -0700108 }
109
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -0700110 /**
111 * Stops monitoring the given IP address.
112 *
113 * @param ip IP address to stop monitoring on
114 */
Jonathan Hart70da5122014-10-01 16:37:42 -0700115 void stopMonitoring(IpAddress ip) {
Jonathan Hartfca736c2014-09-19 17:26:59 -0700116 monitoredAddresses.remove(ip);
117 }
118
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -0700119 /**
120 * Starts the host monitor. Does nothing if the monitor is already running.
121 */
122 void start() {
123 synchronized (this) {
124 if (timeout == null) {
125 timeout = Timer.getTimer().newTimeout(this, 0, TimeUnit.MILLISECONDS);
126 }
127 }
Jonathan Hartfca736c2014-09-19 17:26:59 -0700128 }
129
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -0700130 /**
131 * Stops the host monitor.
132 */
133 void shutdown() {
134 synchronized (this) {
135 timeout.cancel();
136 timeout = null;
137 }
138 }
139
sdn94b00152016-08-30 02:12:32 -0700140 /*
141 * Sets the probe rate.
142 */
143 void setProbeRate(long probeRate) {
144 this.probeRate = probeRate;
145 }
146
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -0700147 /**
148 * Registers a host provider with the host monitor. The monitor can use the
149 * provider to probe hosts.
150 *
151 * @param provider the host provider to register
152 */
Jonathan Hart70da5122014-10-01 16:37:42 -0700153 void registerHostProvider(HostProvider provider) {
154 hostProviders.put(provider.id(), provider);
155 }
156
Jonathan Hartfca736c2014-09-19 17:26:59 -0700157 @Override
158 public void run(Timeout timeout) throws Exception {
Jonathan Hart78613d22016-07-27 11:25:29 -0700159 monitoredAddresses.forEach(this::probe);
Jonathan Hartfca736c2014-09-19 17:26:59 -0700160
Satish Ke9d748f2015-11-24 19:05:01 +0530161 synchronized (this) {
162 this.timeout = Timer.getTimer().newTimeout(this, probeRate, TimeUnit.MILLISECONDS);
163 }
Jonathan Hartfca736c2014-09-19 17:26:59 -0700164 }
165
Jonathan Hart78613d22016-07-27 11:25:29 -0700166 private void probe(IpAddress ip) {
167 Set<Host> hosts = hostManager.getHostsByIp(ip);
168
169 if (hosts.isEmpty()) {
170 sendRequest(ip);
171 } else {
172 for (Host host : hosts) {
173 HostProvider provider = hostProviders.get(host.providerId());
174 if (provider == null) {
175 hostProviders.remove(host.providerId(), null);
176 } else {
177 provider.triggerProbe(host);
178 }
179 }
180 }
181 }
182
Jonathan Hartfca736c2014-09-19 17:26:59 -0700183 /**
Jonathan Hart39ee6482015-08-31 16:00:19 +0200184 * Sends an ARP or NDP request for the given IP address.
Jonathan Hartfca736c2014-09-19 17:26:59 -0700185 *
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800186 * @param targetIp IP address to send the request for
Jonathan Hartfca736c2014-09-19 17:26:59 -0700187 */
Jonathan Hart39ee6482015-08-31 16:00:19 +0200188 private void sendRequest(IpAddress targetIp) {
Charles Chan9238b382017-03-14 13:14:58 -0700189 interfaceService.getMatchingInterfaces(targetIp).forEach(intf -> {
190 if (!edgePortService.isEdgePoint(intf.connectPoint())) {
191 log.warn("Aborting attempt to send probe out non-edge port: {}", intf);
192 return;
Jonathan Hartfca736c2014-09-19 17:26:59 -0700193 }
Charles Chan9238b382017-03-14 13:14:58 -0700194
195 intf.ipAddressesList().stream()
196 .filter(ia -> ia.subnetAddress().contains(targetIp))
197 .forEach(ia -> {
Jonathan Hartf4edb312017-03-24 10:43:10 -0700198 log.debug("Sending probe for target:{} out of intf:{} vlan:{}",
Charles Chan9238b382017-03-14 13:14:58 -0700199 targetIp, intf.connectPoint(), intf.vlan());
200 sendProbe(intf.connectPoint(), targetIp, ia.ipAddress(),
201 intf.mac(), intf.vlan());
202 // account for use-cases where tagged-vlan config is used
203 if (!intf.vlanTagged().isEmpty()) {
204 intf.vlanTagged().forEach(tag -> {
Jonathan Hartf4edb312017-03-24 10:43:10 -0700205 log.debug("Sending probe for target:{} out of intf:{} vlan:{}",
Charles Chan9238b382017-03-14 13:14:58 -0700206 targetIp, intf.connectPoint(), tag);
207 sendProbe(intf.connectPoint(), targetIp, ia.ipAddress(),
208 intf.mac(), tag);
209 });
210 }
211 });
212 });
Jonathan Hartfca736c2014-09-19 17:26:59 -0700213 }
214
Pier Luigi9b1d6262017-02-02 22:31:34 -0800215 public void sendProbe(ConnectPoint connectPoint,
216 IpAddress targetIp,
217 IpAddress sourceIp,
218 MacAddress sourceMac,
219 VlanId vlan) {
Pier Ventre78e73f62016-12-02 19:59:28 -0800220 Ethernet probePacket;
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800221
Pavlin Radoslavov87dd9302015-03-10 13:53:24 -0700222 if (targetIp.isIp4()) {
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800223 // IPv4: Use ARP
Jonathan Hart39ee6482015-08-31 16:00:19 +0200224 probePacket = buildArpRequest(targetIp, sourceIp, sourceMac, vlan);
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800225 } else {
Pier Luigi9b1d6262017-02-02 22:31:34 -0800226 // IPv6: Use Neighbor Discovery. According to the NDP protocol,
227 // we should use the solicitation node address as IPv6 destination
228 // and the multicast mac address as Ethernet destination.
Pier Luigi37b687b2017-01-16 22:46:33 -0800229 byte[] destIp = IPv6.getSolicitNodeAddress(targetIp.toOctets());
Pier Ventre78e73f62016-12-02 19:59:28 -0800230 probePacket = NeighborSolicitation.buildNdpSolicit(
231 targetIp.toOctets(),
232 sourceIp.toOctets(),
233 destIp,
234 sourceMac.toBytes(),
Pier Luigi37b687b2017-01-16 22:46:33 -0800235 IPv6.getMCastMacAddress(destIp),
Pier Ventre78e73f62016-12-02 19:59:28 -0800236 vlan
237 );
238 }
239
240 if (probePacket == null) {
241 log.warn("Not able to build the probe packet");
242 return;
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800243 }
Jonathan Hartfca736c2014-09-19 17:26:59 -0700244
tom9a693fd2014-10-03 11:32:19 -0700245 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
Jonathan Hart4cb39882015-08-12 23:50:55 -0400246 .setOutput(connectPoint.port())
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800247 .build();
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700248
249 OutboundPacket outboundPacket =
Jonathan Hart4cb39882015-08-12 23:50:55 -0400250 new DefaultOutboundPacket(connectPoint.deviceId(), treatment,
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800251 ByteBuffer.wrap(probePacket.serialize()));
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700252
253 packetService.emit(outboundPacket);
254 }
255
Jonathan Hart70da5122014-10-01 16:37:42 -0700256 private Ethernet buildArpRequest(IpAddress targetIp, IpAddress sourceIp,
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800257 MacAddress sourceMac, VlanId vlan) {
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700258
259 ARP arp = new ARP();
260 arp.setHardwareType(ARP.HW_TYPE_ETHERNET)
Jonathan Hart70da5122014-10-01 16:37:42 -0700261 .setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH)
262 .setProtocolType(ARP.PROTO_TYPE_IP)
Pavlin Radoslavov52307e62014-10-29 15:07:37 -0700263 .setProtocolAddressLength((byte) IpAddress.INET_BYTE_LENGTH)
Jonathan Hart70da5122014-10-01 16:37:42 -0700264 .setOpCode(ARP.OP_REQUEST);
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700265
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800266 arp.setSenderHardwareAddress(sourceMac.toBytes())
Jonathan Hart70da5122014-10-01 16:37:42 -0700267 .setSenderProtocolAddress(sourceIp.toOctets())
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800268 .setTargetHardwareAddress(ZERO_MAC_ADDRESS)
Jonathan Hart70da5122014-10-01 16:37:42 -0700269 .setTargetProtocolAddress(targetIp.toOctets());
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700270
271 Ethernet ethernet = new Ethernet();
272 ethernet.setEtherType(Ethernet.TYPE_ARP)
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800273 .setDestinationMACAddress(MacAddress.BROADCAST)
274 .setSourceMACAddress(sourceMac)
Jonathan Hart70da5122014-10-01 16:37:42 -0700275 .setPayload(arp);
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700276
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800277 if (!vlan.equals(VlanId.NONE)) {
278 ethernet.setVlanID(vlan.toShort());
279 }
280
alshabibaf734ff2015-07-01 16:35:26 -0700281 ethernet.setPad(true);
282
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700283 return ethernet;
Jonathan Hartfca736c2014-09-19 17:26:59 -0700284 }
Pavlin Radoslavov89edb542015-02-23 13:50:29 -0800285
Jonathan Hartfca736c2014-09-19 17:26:59 -0700286}