blob: f28dcdb35bdf847bc5baca37bd3efdf971a7a436 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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 */
tombe988312014-09-19 18:38:47 -070016package org.onlab.onos.net.host.impl;
Jonathan Hartfca736c2014-09-19 17:26:59 -070017
Jonathan Hart87fbbad2014-09-23 08:43:50 -070018import java.nio.ByteBuffer;
19import java.util.ArrayList;
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070020import java.util.Collections;
Jonathan Hart87fbbad2014-09-23 08:43:50 -070021import java.util.List;
Jonathan Hartfca736c2014-09-19 17:26:59 -070022import java.util.Set;
Jonathan Hart70da5122014-10-01 16:37:42 -070023import java.util.concurrent.ConcurrentHashMap;
Jonathan Hart34ed2fd2014-10-02 19:08:55 -070024import java.util.concurrent.ConcurrentMap;
Jonathan Hartfca736c2014-09-19 17:26:59 -070025import java.util.concurrent.TimeUnit;
26
27import org.jboss.netty.util.Timeout;
28import org.jboss.netty.util.TimerTask;
Jonathan Hart87fbbad2014-09-23 08:43:50 -070029import org.onlab.onos.net.ConnectPoint;
30import org.onlab.onos.net.Device;
31import org.onlab.onos.net.DeviceId;
Jonathan Hartfca736c2014-09-19 17:26:59 -070032import org.onlab.onos.net.Host;
33import org.onlab.onos.net.Port;
34import org.onlab.onos.net.device.DeviceService;
Jonathan Hart87fbbad2014-09-23 08:43:50 -070035import org.onlab.onos.net.flow.DefaultTrafficTreatment;
36import org.onlab.onos.net.flow.TrafficTreatment;
37import org.onlab.onos.net.flow.instructions.Instruction;
38import org.onlab.onos.net.flow.instructions.Instructions;
Jonathan Hartfca736c2014-09-19 17:26:59 -070039import org.onlab.onos.net.host.HostProvider;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070040import org.onlab.onos.net.host.InterfaceIpAddress;
Jonathan Hart87fbbad2014-09-23 08:43:50 -070041import org.onlab.onos.net.host.PortAddresses;
42import org.onlab.onos.net.packet.DefaultOutboundPacket;
43import org.onlab.onos.net.packet.OutboundPacket;
44import org.onlab.onos.net.packet.PacketService;
Jonathan Hart70da5122014-10-01 16:37:42 -070045import org.onlab.onos.net.provider.ProviderId;
Jonathan Hart87fbbad2014-09-23 08:43:50 -070046import org.onlab.packet.ARP;
47import org.onlab.packet.Ethernet;
48import org.onlab.packet.IpAddress;
Jonathan Hart87fbbad2014-09-23 08:43:50 -070049import org.onlab.packet.MacAddress;
Jonathan Hartfca736c2014-09-19 17:26:59 -070050import org.onlab.util.Timer;
51
Jonathan Hart87fbbad2014-09-23 08:43:50 -070052/**
53 * Monitors hosts on the dataplane to detect changes in host data.
Thomas Vachuska4b420772014-10-30 16:46:17 -070054 * <p>
Jonathan Hart87fbbad2014-09-23 08:43:50 -070055 * The HostMonitor can monitor hosts that have already been detected for
56 * changes. At an application's request, it can also monitor and actively
57 * probe for hosts that have not yet been detected (specified by IP address).
Thomas Vachuska4b420772014-10-30 16:46:17 -070058 * </p>
Jonathan Hart87fbbad2014-09-23 08:43:50 -070059 */
tom202175a2014-09-19 19:00:11 -070060public class HostMonitor implements TimerTask {
Jonathan Hart70da5122014-10-01 16:37:42 -070061 private DeviceService deviceService;
62 private PacketService packetService;
63 private HostManager hostManager;
Jonathan Hartfca736c2014-09-19 17:26:59 -070064
Jonathan Hart87fbbad2014-09-23 08:43:50 -070065 private final Set<IpAddress> monitoredAddresses;
Jonathan Hartfca736c2014-09-19 17:26:59 -070066
Jonathan Hart34ed2fd2014-10-02 19:08:55 -070067 private final ConcurrentMap<ProviderId, HostProvider> hostProviders;
Jonathan Hart70da5122014-10-01 16:37:42 -070068
Jonathan Hart34ed2fd2014-10-02 19:08:55 -070069 private static final long DEFAULT_PROBE_RATE = 30000; // milliseconds
70 private long probeRate = DEFAULT_PROBE_RATE;
Jonathan Hartfca736c2014-09-19 17:26:59 -070071
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070072 private Timeout timeout;
Jonathan Hartfca736c2014-09-19 17:26:59 -070073
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070074 /**
75 * Creates a new host monitor.
76 *
77 * @param deviceService device service used to find edge ports
78 * @param packetService packet service used to send packets on the data plane
Jonathan Hartb4758a92014-09-24 10:46:45 -070079 * @param hostManager host manager used to look up host information and
80 * probe existing hosts
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070081 */
Jonathan Hart34ed2fd2014-10-02 19:08:55 -070082 public HostMonitor(DeviceService deviceService, PacketService packetService,
Jonathan Hartb4758a92014-09-24 10:46:45 -070083 HostManager hostManager) {
Jonathan Hart70da5122014-10-01 16:37:42 -070084
Jonathan Hartfca736c2014-09-19 17:26:59 -070085 this.deviceService = deviceService;
Jonathan Hart87fbbad2014-09-23 08:43:50 -070086 this.packetService = packetService;
Jonathan Hartb4758a92014-09-24 10:46:45 -070087 this.hostManager = hostManager;
Jonathan Hartfca736c2014-09-19 17:26:59 -070088
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070089 monitoredAddresses = Collections.newSetFromMap(
90 new ConcurrentHashMap<IpAddress, Boolean>());
Jonathan Hart70da5122014-10-01 16:37:42 -070091 hostProviders = new ConcurrentHashMap<>();
Jonathan Hart70da5122014-10-01 16:37:42 -070092 }
93
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070094 /**
95 * Adds an IP address to be monitored by the host monitor. The monitor will
96 * periodically probe the host to detect changes.
97 *
98 * @param ip IP address of the host to monitor
99 */
Jonathan Hart70da5122014-10-01 16:37:42 -0700100 void addMonitoringFor(IpAddress ip) {
Jonathan Hartfca736c2014-09-19 17:26:59 -0700101 monitoredAddresses.add(ip);
102 }
103
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -0700104 /**
105 * Stops monitoring the given IP address.
106 *
107 * @param ip IP address to stop monitoring on
108 */
Jonathan Hart70da5122014-10-01 16:37:42 -0700109 void stopMonitoring(IpAddress ip) {
Jonathan Hartfca736c2014-09-19 17:26:59 -0700110 monitoredAddresses.remove(ip);
111 }
112
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -0700113 /**
114 * Starts the host monitor. Does nothing if the monitor is already running.
115 */
116 void start() {
117 synchronized (this) {
118 if (timeout == null) {
119 timeout = Timer.getTimer().newTimeout(this, 0, TimeUnit.MILLISECONDS);
120 }
121 }
Jonathan Hartfca736c2014-09-19 17:26:59 -0700122 }
123
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -0700124 /**
125 * Stops the host monitor.
126 */
127 void shutdown() {
128 synchronized (this) {
129 timeout.cancel();
130 timeout = null;
131 }
132 }
133
134 /**
135 * Registers a host provider with the host monitor. The monitor can use the
136 * provider to probe hosts.
137 *
138 * @param provider the host provider to register
139 */
Jonathan Hart70da5122014-10-01 16:37:42 -0700140 void registerHostProvider(HostProvider provider) {
141 hostProviders.put(provider.id(), provider);
142 }
143
Jonathan Hartfca736c2014-09-19 17:26:59 -0700144 @Override
145 public void run(Timeout timeout) throws Exception {
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700146 for (IpAddress ip : monitoredAddresses) {
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700147 Set<Host> hosts = hostManager.getHostsByIp(ip);
Jonathan Hartfca736c2014-09-19 17:26:59 -0700148
149 if (hosts.isEmpty()) {
150 sendArpRequest(ip);
151 } else {
152 for (Host host : hosts) {
Jonathan Hart70da5122014-10-01 16:37:42 -0700153 HostProvider provider = hostProviders.get(host.providerId());
Jonathan Hart34ed2fd2014-10-02 19:08:55 -0700154 if (provider == null) {
155 hostProviders.remove(host.providerId(), null);
156 } else {
Jonathan Hart70da5122014-10-01 16:37:42 -0700157 provider.triggerProbe(host);
158 }
Jonathan Hartfca736c2014-09-19 17:26:59 -0700159 }
160 }
161 }
162
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -0700163 this.timeout = Timer.getTimer().newTimeout(this, probeRate, TimeUnit.MILLISECONDS);
Jonathan Hartfca736c2014-09-19 17:26:59 -0700164 }
165
166 /**
167 * Sends an ARP request for the given IP address.
168 *
169 * @param targetIp IP address to ARP for
170 */
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700171 private void sendArpRequest(IpAddress targetIp) {
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700172 // Find ports with an IP address in the target's subnet and sent ARP
173 // probes out those ports.
174 for (Device device : deviceService.getDevices()) {
Jonathan Hartfca736c2014-09-19 17:26:59 -0700175 for (Port port : deviceService.getPorts(device.id())) {
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700176 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700177 PortAddresses portAddresses =
178 hostManager.getAddressBindingsForPort(cp);
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700179
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700180 for (InterfaceIpAddress ia : portAddresses.ipAddresses()) {
181 if (ia.subnetAddress().contains(targetIp)) {
Jonathan Hart70da5122014-10-01 16:37:42 -0700182 sendProbe(device.id(), port, targetIp,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700183 ia.ipAddress(), portAddresses.mac());
Jonathan Hart09585c62014-09-23 16:58:04 -0700184 }
Jonathan Hart70da5122014-10-01 16:37:42 -0700185 }
Jonathan Hartfca736c2014-09-19 17:26:59 -0700186 }
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700187 }
Jonathan Hartfca736c2014-09-19 17:26:59 -0700188 }
189
Jonathan Hart70da5122014-10-01 16:37:42 -0700190 private void sendProbe(DeviceId deviceId, Port port, IpAddress targetIp,
191 IpAddress sourceIp, MacAddress sourceMac) {
192 Ethernet arpPacket = buildArpRequest(targetIp, sourceIp, sourceMac);
Jonathan Hartfca736c2014-09-19 17:26:59 -0700193
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700194 List<Instruction> instructions = new ArrayList<>();
195 instructions.add(Instructions.createOutput(port.number()));
196
tom9a693fd2014-10-03 11:32:19 -0700197 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
alshabib010c31d2014-09-26 10:01:12 -0700198 .setOutput(port.number())
199 .build();
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700200
201 OutboundPacket outboundPacket =
202 new DefaultOutboundPacket(deviceId, treatment,
203 ByteBuffer.wrap(arpPacket.serialize()));
204
205 packetService.emit(outboundPacket);
206 }
207
Jonathan Hart70da5122014-10-01 16:37:42 -0700208 private Ethernet buildArpRequest(IpAddress targetIp, IpAddress sourceIp,
209 MacAddress sourceMac) {
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700210
211 ARP arp = new ARP();
212 arp.setHardwareType(ARP.HW_TYPE_ETHERNET)
Jonathan Hart70da5122014-10-01 16:37:42 -0700213 .setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH)
214 .setProtocolType(ARP.PROTO_TYPE_IP)
Pavlin Radoslavov52307e62014-10-29 15:07:37 -0700215 .setProtocolAddressLength((byte) IpAddress.INET_BYTE_LENGTH)
Jonathan Hart70da5122014-10-01 16:37:42 -0700216 .setOpCode(ARP.OP_REQUEST);
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700217
Jonathan Hart70da5122014-10-01 16:37:42 -0700218 arp.setSenderHardwareAddress(sourceMac.getAddress())
219 .setSenderProtocolAddress(sourceIp.toOctets())
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -0700220 .setTargetHardwareAddress(MacAddress.ZERO_MAC_ADDRESS)
Jonathan Hart70da5122014-10-01 16:37:42 -0700221 .setTargetProtocolAddress(targetIp.toOctets());
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700222
223 Ethernet ethernet = new Ethernet();
224 ethernet.setEtherType(Ethernet.TYPE_ARP)
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -0700225 .setDestinationMACAddress(MacAddress.BROADCAST_MAC)
Jonathan Hart70da5122014-10-01 16:37:42 -0700226 .setSourceMACAddress(sourceMac.getAddress())
227 .setPayload(arp);
Jonathan Hart87fbbad2014-09-23 08:43:50 -0700228
229 return ethernet;
Jonathan Hartfca736c2014-09-19 17:26:59 -0700230 }
231}