Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.host.impl; |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | 87fbbad | 2014-09-23 08:43:50 -0700 | [diff] [blame] | 18 | import java.nio.ByteBuffer; |
| 19 | import java.util.ArrayList; |
Jonathan Hart | 8f6f1ea | 2014-10-03 16:05:19 -0700 | [diff] [blame] | 20 | import java.util.Collections; |
Jonathan Hart | 87fbbad | 2014-09-23 08:43:50 -0700 | [diff] [blame] | 21 | import java.util.List; |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 22 | import java.util.Set; |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 23 | import java.util.concurrent.ConcurrentHashMap; |
Jonathan Hart | 34ed2fd | 2014-10-02 19:08:55 -0700 | [diff] [blame] | 24 | import java.util.concurrent.ConcurrentMap; |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 25 | import java.util.concurrent.TimeUnit; |
| 26 | |
| 27 | import org.jboss.netty.util.Timeout; |
| 28 | import org.jboss.netty.util.TimerTask; |
Jonathan Hart | 87fbbad | 2014-09-23 08:43:50 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.ConnectPoint; |
| 30 | import org.onlab.onos.net.Device; |
| 31 | import org.onlab.onos.net.DeviceId; |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.Host; |
| 33 | import org.onlab.onos.net.Port; |
| 34 | import org.onlab.onos.net.device.DeviceService; |
Jonathan Hart | 87fbbad | 2014-09-23 08:43:50 -0700 | [diff] [blame] | 35 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 36 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 37 | import org.onlab.onos.net.flow.instructions.Instruction; |
| 38 | import org.onlab.onos.net.flow.instructions.Instructions; |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 39 | import org.onlab.onos.net.host.HostProvider; |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 40 | import org.onlab.onos.net.host.InterfaceIpAddress; |
Jonathan Hart | 87fbbad | 2014-09-23 08:43:50 -0700 | [diff] [blame] | 41 | import org.onlab.onos.net.host.PortAddresses; |
| 42 | import org.onlab.onos.net.packet.DefaultOutboundPacket; |
| 43 | import org.onlab.onos.net.packet.OutboundPacket; |
| 44 | import org.onlab.onos.net.packet.PacketService; |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 45 | import org.onlab.onos.net.provider.ProviderId; |
Jonathan Hart | 87fbbad | 2014-09-23 08:43:50 -0700 | [diff] [blame] | 46 | import org.onlab.packet.ARP; |
| 47 | import org.onlab.packet.Ethernet; |
| 48 | import org.onlab.packet.IpAddress; |
Jonathan Hart | 87fbbad | 2014-09-23 08:43:50 -0700 | [diff] [blame] | 49 | import org.onlab.packet.MacAddress; |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 50 | import org.onlab.util.Timer; |
| 51 | |
Jonathan Hart | 87fbbad | 2014-09-23 08:43:50 -0700 | [diff] [blame] | 52 | /** |
| 53 | * Monitors hosts on the dataplane to detect changes in host data. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 54 | * <p> |
Jonathan Hart | 87fbbad | 2014-09-23 08:43:50 -0700 | [diff] [blame] | 55 | * 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 Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 58 | * </p> |
Jonathan Hart | 87fbbad | 2014-09-23 08:43:50 -0700 | [diff] [blame] | 59 | */ |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 60 | public class HostMonitor implements TimerTask { |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 61 | private DeviceService deviceService; |
| 62 | private PacketService packetService; |
| 63 | private HostManager hostManager; |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 64 | |
Jonathan Hart | 87fbbad | 2014-09-23 08:43:50 -0700 | [diff] [blame] | 65 | private final Set<IpAddress> monitoredAddresses; |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 66 | |
Jonathan Hart | 34ed2fd | 2014-10-02 19:08:55 -0700 | [diff] [blame] | 67 | private final ConcurrentMap<ProviderId, HostProvider> hostProviders; |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 68 | |
Jonathan Hart | 34ed2fd | 2014-10-02 19:08:55 -0700 | [diff] [blame] | 69 | private static final long DEFAULT_PROBE_RATE = 30000; // milliseconds |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 70 | private static final byte[] ZERO_MAC_ADDRESS = MacAddress.ZERO.toBytes(); |
Jonathan Hart | 34ed2fd | 2014-10-02 19:08:55 -0700 | [diff] [blame] | 71 | private long probeRate = DEFAULT_PROBE_RATE; |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 72 | |
Jonathan Hart | 8f6f1ea | 2014-10-03 16:05:19 -0700 | [diff] [blame] | 73 | private Timeout timeout; |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 74 | |
Jonathan Hart | 8f6f1ea | 2014-10-03 16:05:19 -0700 | [diff] [blame] | 75 | /** |
| 76 | * Creates a new host monitor. |
| 77 | * |
| 78 | * @param deviceService device service used to find edge ports |
| 79 | * @param packetService packet service used to send packets on the data plane |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 80 | * @param hostManager host manager used to look up host information and |
| 81 | * probe existing hosts |
Jonathan Hart | 8f6f1ea | 2014-10-03 16:05:19 -0700 | [diff] [blame] | 82 | */ |
Jonathan Hart | 34ed2fd | 2014-10-02 19:08:55 -0700 | [diff] [blame] | 83 | public HostMonitor(DeviceService deviceService, PacketService packetService, |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 84 | HostManager hostManager) { |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 85 | |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 86 | this.deviceService = deviceService; |
Jonathan Hart | 87fbbad | 2014-09-23 08:43:50 -0700 | [diff] [blame] | 87 | this.packetService = packetService; |
Jonathan Hart | b4758a9 | 2014-09-24 10:46:45 -0700 | [diff] [blame] | 88 | this.hostManager = hostManager; |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 89 | |
Jonathan Hart | 8f6f1ea | 2014-10-03 16:05:19 -0700 | [diff] [blame] | 90 | monitoredAddresses = Collections.newSetFromMap( |
| 91 | new ConcurrentHashMap<IpAddress, Boolean>()); |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 92 | hostProviders = new ConcurrentHashMap<>(); |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Jonathan Hart | 8f6f1ea | 2014-10-03 16:05:19 -0700 | [diff] [blame] | 95 | /** |
| 96 | * Adds an IP address to be monitored by the host monitor. The monitor will |
| 97 | * periodically probe the host to detect changes. |
| 98 | * |
| 99 | * @param ip IP address of the host to monitor |
| 100 | */ |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 101 | void addMonitoringFor(IpAddress ip) { |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 102 | monitoredAddresses.add(ip); |
| 103 | } |
| 104 | |
Jonathan Hart | 8f6f1ea | 2014-10-03 16:05:19 -0700 | [diff] [blame] | 105 | /** |
| 106 | * Stops monitoring the given IP address. |
| 107 | * |
| 108 | * @param ip IP address to stop monitoring on |
| 109 | */ |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 110 | void stopMonitoring(IpAddress ip) { |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 111 | monitoredAddresses.remove(ip); |
| 112 | } |
| 113 | |
Jonathan Hart | 8f6f1ea | 2014-10-03 16:05:19 -0700 | [diff] [blame] | 114 | /** |
| 115 | * Starts the host monitor. Does nothing if the monitor is already running. |
| 116 | */ |
| 117 | void start() { |
| 118 | synchronized (this) { |
| 119 | if (timeout == null) { |
| 120 | timeout = Timer.getTimer().newTimeout(this, 0, TimeUnit.MILLISECONDS); |
| 121 | } |
| 122 | } |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Jonathan Hart | 8f6f1ea | 2014-10-03 16:05:19 -0700 | [diff] [blame] | 125 | /** |
| 126 | * Stops the host monitor. |
| 127 | */ |
| 128 | void shutdown() { |
| 129 | synchronized (this) { |
| 130 | timeout.cancel(); |
| 131 | timeout = null; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Registers a host provider with the host monitor. The monitor can use the |
| 137 | * provider to probe hosts. |
| 138 | * |
| 139 | * @param provider the host provider to register |
| 140 | */ |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 141 | void registerHostProvider(HostProvider provider) { |
| 142 | hostProviders.put(provider.id(), provider); |
| 143 | } |
| 144 | |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 145 | @Override |
| 146 | public void run(Timeout timeout) throws Exception { |
Jonathan Hart | 87fbbad | 2014-09-23 08:43:50 -0700 | [diff] [blame] | 147 | for (IpAddress ip : monitoredAddresses) { |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 148 | Set<Host> hosts = hostManager.getHostsByIp(ip); |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 149 | |
| 150 | if (hosts.isEmpty()) { |
| 151 | sendArpRequest(ip); |
| 152 | } else { |
| 153 | for (Host host : hosts) { |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 154 | HostProvider provider = hostProviders.get(host.providerId()); |
Jonathan Hart | 34ed2fd | 2014-10-02 19:08:55 -0700 | [diff] [blame] | 155 | if (provider == null) { |
| 156 | hostProviders.remove(host.providerId(), null); |
| 157 | } else { |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 158 | provider.triggerProbe(host); |
| 159 | } |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
Jonathan Hart | 8f6f1ea | 2014-10-03 16:05:19 -0700 | [diff] [blame] | 164 | this.timeout = Timer.getTimer().newTimeout(this, probeRate, TimeUnit.MILLISECONDS); |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Sends an ARP request for the given IP address. |
| 169 | * |
| 170 | * @param targetIp IP address to ARP for |
| 171 | */ |
Jonathan Hart | 87fbbad | 2014-09-23 08:43:50 -0700 | [diff] [blame] | 172 | private void sendArpRequest(IpAddress targetIp) { |
Jonathan Hart | 87fbbad | 2014-09-23 08:43:50 -0700 | [diff] [blame] | 173 | // Find ports with an IP address in the target's subnet and sent ARP |
| 174 | // probes out those ports. |
| 175 | for (Device device : deviceService.getDevices()) { |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 176 | for (Port port : deviceService.getPorts(device.id())) { |
Jonathan Hart | 87fbbad | 2014-09-23 08:43:50 -0700 | [diff] [blame] | 177 | ConnectPoint cp = new ConnectPoint(device.id(), port.number()); |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 178 | Set<PortAddresses> portAddressSet = |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 179 | hostManager.getAddressBindingsForPort(cp); |
Jonathan Hart | 87fbbad | 2014-09-23 08:43:50 -0700 | [diff] [blame] | 180 | |
Jonathan Hart | a887ba8 | 2014-11-03 15:20:52 -0800 | [diff] [blame] | 181 | for (PortAddresses portAddresses : portAddressSet) { |
| 182 | for (InterfaceIpAddress ia : portAddresses.ipAddresses()) { |
| 183 | if (ia.subnetAddress().contains(targetIp)) { |
| 184 | sendProbe(device.id(), port, targetIp, |
| 185 | ia.ipAddress(), portAddresses.mac()); |
| 186 | } |
Jonathan Hart | 09585c6 | 2014-09-23 16:58:04 -0700 | [diff] [blame] | 187 | } |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 188 | } |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 189 | } |
Jonathan Hart | 87fbbad | 2014-09-23 08:43:50 -0700 | [diff] [blame] | 190 | } |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 193 | private void sendProbe(DeviceId deviceId, Port port, IpAddress targetIp, |
| 194 | IpAddress sourceIp, MacAddress sourceMac) { |
| 195 | Ethernet arpPacket = buildArpRequest(targetIp, sourceIp, sourceMac); |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 196 | |
Jonathan Hart | 87fbbad | 2014-09-23 08:43:50 -0700 | [diff] [blame] | 197 | List<Instruction> instructions = new ArrayList<>(); |
| 198 | instructions.add(Instructions.createOutput(port.number())); |
| 199 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 200 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 201 | .setOutput(port.number()) |
| 202 | .build(); |
Jonathan Hart | 87fbbad | 2014-09-23 08:43:50 -0700 | [diff] [blame] | 203 | |
| 204 | OutboundPacket outboundPacket = |
| 205 | new DefaultOutboundPacket(deviceId, treatment, |
| 206 | ByteBuffer.wrap(arpPacket.serialize())); |
| 207 | |
| 208 | packetService.emit(outboundPacket); |
| 209 | } |
| 210 | |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 211 | private Ethernet buildArpRequest(IpAddress targetIp, IpAddress sourceIp, |
| 212 | MacAddress sourceMac) { |
Jonathan Hart | 87fbbad | 2014-09-23 08:43:50 -0700 | [diff] [blame] | 213 | |
| 214 | ARP arp = new ARP(); |
| 215 | arp.setHardwareType(ARP.HW_TYPE_ETHERNET) |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 216 | .setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH) |
| 217 | .setProtocolType(ARP.PROTO_TYPE_IP) |
Pavlin Radoslavov | 52307e6 | 2014-10-29 15:07:37 -0700 | [diff] [blame] | 218 | .setProtocolAddressLength((byte) IpAddress.INET_BYTE_LENGTH) |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 219 | .setOpCode(ARP.OP_REQUEST); |
Jonathan Hart | 87fbbad | 2014-09-23 08:43:50 -0700 | [diff] [blame] | 220 | |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 221 | arp.setSenderHardwareAddress(sourceMac.toBytes()) |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 222 | .setSenderProtocolAddress(sourceIp.toOctets()) |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 223 | .setTargetHardwareAddress(ZERO_MAC_ADDRESS) |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 224 | .setTargetProtocolAddress(targetIp.toOctets()); |
Jonathan Hart | 87fbbad | 2014-09-23 08:43:50 -0700 | [diff] [blame] | 225 | |
| 226 | Ethernet ethernet = new Ethernet(); |
| 227 | ethernet.setEtherType(Ethernet.TYPE_ARP) |
Yuta HIGUCHI | 3e848a8 | 2014-11-02 20:19:42 -0800 | [diff] [blame] | 228 | .setDestinationMACAddress(MacAddress.BROADCAST) |
| 229 | .setSourceMACAddress(sourceMac) |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 230 | .setPayload(arp); |
Jonathan Hart | 87fbbad | 2014-09-23 08:43:50 -0700 | [diff] [blame] | 231 | |
| 232 | return ethernet; |
Jonathan Hart | fca736c | 2014-09-19 17:26:59 -0700 | [diff] [blame] | 233 | } |
| 234 | } |