blob: 9e62bd9da7098ad1c81e6705f2978b63f7bb77c6 [file] [log] [blame]
Jonathan Hart03102132014-07-01 23:22:04 -07001package net.onrc.onos.core.hostmanager;
Jonathan Hartd857ad62013-12-14 18:08:17 -08002
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.Date;
6import java.util.HashMap;
TeruU80ce5062014-03-03 17:16:13 -08007import java.util.HashSet;
Jonathan Hartd857ad62013-12-14 18:08:17 -08008import java.util.List;
9import java.util.Map;
TeruU80ce5062014-03-03 17:16:13 -080010import java.util.Set;
TeruUd1c5b652014-03-24 13:58:46 -070011import java.util.concurrent.CopyOnWriteArrayList;
TeruU80ce5062014-03-03 17:16:13 -080012import java.util.concurrent.Executors;
13import java.util.concurrent.ScheduledExecutorService;
14import java.util.concurrent.TimeUnit;
Jonathan Hartd857ad62013-12-14 18:08:17 -080015
16import net.floodlightcontroller.core.FloodlightContext;
17import net.floodlightcontroller.core.IFloodlightProviderService;
18import net.floodlightcontroller.core.IOFMessageListener;
19import net.floodlightcontroller.core.IOFSwitch;
20import net.floodlightcontroller.core.IUpdate;
21import net.floodlightcontroller.core.module.FloodlightModuleContext;
22import net.floodlightcontroller.core.module.FloodlightModuleException;
23import net.floodlightcontroller.core.module.IFloodlightModule;
24import net.floodlightcontroller.core.module.IFloodlightService;
Jonathan Hartd857ad62013-12-14 18:08:17 -080025import net.floodlightcontroller.util.MACAddress;
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070026import net.onrc.onos.core.packet.Ethernet;
TeruU5d2c9392014-06-09 20:02:02 -070027import net.onrc.onos.core.topology.Device;
Jonathan Harte37e4e22014-05-13 19:12:02 -070028import net.onrc.onos.core.topology.ITopologyService;
TeruU5d2c9392014-06-09 20:02:02 -070029import net.onrc.onos.core.topology.Port;
Jonathan Harte37e4e22014-05-13 19:12:02 -070030import net.onrc.onos.core.topology.Topology;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070031import net.onrc.onos.core.util.Dpid;
32import net.onrc.onos.core.util.PortNumber;
Jonathan Hartd857ad62013-12-14 18:08:17 -080033
34import org.openflow.protocol.OFMessage;
35import org.openflow.protocol.OFPacketIn;
36import org.openflow.protocol.OFType;
TeruU80ce5062014-03-03 17:16:13 -080037import org.slf4j.Logger;
38import org.slf4j.LoggerFactory;
Jonathan Hartd857ad62013-12-14 18:08:17 -080039
Jonathan Hart03102132014-07-01 23:22:04 -070040public class HostManager implements IFloodlightModule,
Ray Milkey269ffb92014-04-03 14:43:30 -070041 IOFMessageListener,
Jonathan Hart03102132014-07-01 23:22:04 -070042 IHostService {
TeruU28adcc32014-04-15 17:57:35 -070043
Jonathan Hart03102132014-07-01 23:22:04 -070044 private static final Logger log = LoggerFactory.getLogger(HostManager.class);
45 private static final long HOST_CLEANING_INITIAL_DELAY = 30;
TeruU28adcc32014-04-15 17:57:35 -070046 private int cleanupSecondConfig = 60 * 60;
47 private int agingMillisecConfig = 60 * 60 * 1000;
Yuta HIGUCHIe7eac182014-03-19 19:18:30 -070048
Jonathan Hart03102132014-07-01 23:22:04 -070049 private CopyOnWriteArrayList<IHostListener> hostListeners;
Ray Milkey269ffb92014-04-03 14:43:30 -070050 private IFloodlightProviderService floodlightProvider;
Jonathan Hart83ce8c42014-06-02 00:07:06 -070051 private static final ScheduledExecutorService EXECUTOR_SERVICE =
52 Executors.newSingleThreadScheduledExecutor();
Yuta HIGUCHIe7eac182014-03-19 19:18:30 -070053
Jonathan Harte37e4e22014-05-13 19:12:02 -070054 private ITopologyService topologyService;
55 private Topology topology;
Yuta HIGUCHIe7eac182014-03-19 19:18:30 -070056
Jonathan Hart03102132014-07-01 23:22:04 -070057 public enum HostUpdateType {
TeruU80ce5062014-03-03 17:16:13 -080058 ADD, DELETE, UPDATE;
59 }
Yuta HIGUCHIe7eac182014-03-19 19:18:30 -070060
Jonathan Hart03102132014-07-01 23:22:04 -070061 private class HostUpdate implements IUpdate {
62 private final Host host;
63 private final HostUpdateType type;
Yuta HIGUCHIe7eac182014-03-19 19:18:30 -070064
Jonathan Hart03102132014-07-01 23:22:04 -070065 public HostUpdate(Host host, HostUpdateType type) {
66 this.host = host;
Ray Milkey269ffb92014-04-03 14:43:30 -070067 this.type = type;
68 }
Yuta HIGUCHIe7eac182014-03-19 19:18:30 -070069
Ray Milkey269ffb92014-04-03 14:43:30 -070070 @Override
71 public void dispatch() {
Jonathan Hart03102132014-07-01 23:22:04 -070072 if (type == HostUpdateType.ADD) {
73 for (IHostListener listener : hostListeners) {
74 listener.hostAdded(host);
Ray Milkey269ffb92014-04-03 14:43:30 -070075 }
Jonathan Hart03102132014-07-01 23:22:04 -070076 } else if (type == HostUpdateType.DELETE) {
77 for (IHostListener listener : hostListeners) {
78 listener.hostRemoved(host);
Ray Milkey269ffb92014-04-03 14:43:30 -070079 }
80 }
81 }
82 }
Yuta HIGUCHIe7eac182014-03-19 19:18:30 -070083
Ray Milkey269ffb92014-04-03 14:43:30 -070084 @Override
85 public String getName() {
Jonathan Hart03102132014-07-01 23:22:04 -070086 return "hostmanager";
Ray Milkey269ffb92014-04-03 14:43:30 -070087 }
Jonathan Hartd857ad62013-12-14 18:08:17 -080088
Ray Milkey269ffb92014-04-03 14:43:30 -070089 @Override
90 public boolean isCallbackOrderingPrereq(OFType type, String name) {
91 // We want link discovery to consume LLDP first otherwise we'll
Jonathan Hart03102132014-07-01 23:22:04 -070092 // end up reading bad host info from LLDP packets
Ray Milkey269ffb92014-04-03 14:43:30 -070093 return type == OFType.PACKET_IN && "linkdiscovery".equals(name);
94 }
Jonathan Hartd857ad62013-12-14 18:08:17 -080095
Ray Milkey269ffb92014-04-03 14:43:30 -070096 @Override
97 public boolean isCallbackOrderingPostreq(OFType type, String name) {
98 return type == OFType.PACKET_IN &&
99 ("proxyarpmanager".equals(name) || "onosforwarding".equals(name));
100 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800101
Ray Milkey269ffb92014-04-03 14:43:30 -0700102 @Override
103 public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
Pavlin Radoslavov0b88a262014-04-10 15:43:27 -0700104 if (msg.getType().equals(OFType.PACKET_IN) &&
Patrick Liuab1e6062014-05-05 11:12:13 -0700105 (msg instanceof OFPacketIn)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700106 OFPacketIn pi = (OFPacketIn) msg;
Yuta HIGUCHIe7eac182014-03-19 19:18:30 -0700107
Ray Milkey269ffb92014-04-03 14:43:30 -0700108 Ethernet eth = IFloodlightProviderService.bcStore.
109 get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
Yuta HIGUCHIe7eac182014-03-19 19:18:30 -0700110
Ray Milkey269ffb92014-04-03 14:43:30 -0700111 return processPacketIn(sw, pi, eth);
112 }
Yuta HIGUCHIe7eac182014-03-19 19:18:30 -0700113
Ray Milkey269ffb92014-04-03 14:43:30 -0700114 return Command.CONTINUE;
115 }
Yuta HIGUCHIe7eac182014-03-19 19:18:30 -0700116
Jonathan Hart83ce8c42014-06-02 00:07:06 -0700117 // This "protected" modifier is for unit test.
118 // The above "receive" method couldn't be tested
119 // because of IFloodlightProviderService static final field.
TeruU28adcc32014-04-15 17:57:35 -0700120 protected Command processPacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth) {
TeruU5d2c9392014-06-09 20:02:02 -0700121 if (log.isTraceEnabled()) {
122 log.trace("Receive PACKET_IN swId {}, portId {}", sw.getId(), pi.getInPort());
123 }
124
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700125 final Dpid dpid = new Dpid(sw.getId());
126 final PortNumber portNum = new PortNumber(pi.getInPort());
TeruU80ce5062014-03-03 17:16:13 -0800127 Long mac = eth.getSourceMAC().toLong();
128
Jonathan Hart03102132014-07-01 23:22:04 -0700129 Host srcHost =
130 getSourceHostFromPacket(eth, dpid.value(), portNum.value());
TeruU80ce5062014-03-03 17:16:13 -0800131
Jonathan Hart03102132014-07-01 23:22:04 -0700132 if (srcHost == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700133 return Command.STOP;
TeruU80ce5062014-03-03 17:16:13 -0800134 }
Yuta HIGUCHIe7eac182014-03-19 19:18:30 -0700135
Jonathan Hart03102132014-07-01 23:22:04 -0700136 // If the switch port we try to attach a new host already has a link,
137 // then don't add the host
Jonathan Hart83ce8c42014-06-02 00:07:06 -0700138 // TODO We probably don't need to check this here, it should be done in
139 // the Topology module.
TeruU5d2c9392014-06-09 20:02:02 -0700140 topology.acquireReadLock();
141 try {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700142 if (topology.getOutgoingLink(dpid, portNum) != null ||
143 topology.getIncomingLink(dpid, portNum) != null) {
Jonathan Hart03102132014-07-01 23:22:04 -0700144 log.debug("Not adding host {} as " +
TeruU5d2c9392014-06-09 20:02:02 -0700145 "there is a link on the port: dpid {} port {}",
Jonathan Hart03102132014-07-01 23:22:04 -0700146 srcHost.getMacAddress(), dpid, portNum);
TeruU5d2c9392014-06-09 20:02:02 -0700147 return Command.CONTINUE;
Ray Milkey269ffb92014-04-03 14:43:30 -0700148 }
TeruU5d2c9392014-06-09 20:02:02 -0700149 } finally {
150 topology.releaseReadLock();
TeruU80ce5062014-03-03 17:16:13 -0800151 }
Yuta HIGUCHIe7eac182014-03-19 19:18:30 -0700152
Jonathan Hart03102132014-07-01 23:22:04 -0700153 addHost(mac, srcHost);
Yuta HIGUCHIe7eac182014-03-19 19:18:30 -0700154
Ray Milkey269ffb92014-04-03 14:43:30 -0700155 if (log.isTraceEnabled()) {
Jonathan Hart03102132014-07-01 23:22:04 -0700156 log.trace("Add host info: {}", srcHost);
TeruU80ce5062014-03-03 17:16:13 -0800157 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800158 return Command.CONTINUE;
Ray Milkey269ffb92014-04-03 14:43:30 -0700159 }
Yuta HIGUCHIe7eac182014-03-19 19:18:30 -0700160
Jonathan Hart03102132014-07-01 23:22:04 -0700161 // Thread to delete hosts periodically.
162 // Remove all hosts from the map first and then finally delete hosts
Jonathan Hart83ce8c42014-06-02 00:07:06 -0700163 // from the DB.
164
Jonathan Hart03102132014-07-01 23:22:04 -0700165 // TODO This should be sharded based on host 'owner' (i.e. the instance
Jonathan Hart83ce8c42014-06-02 00:07:06 -0700166 // that owns the switch it is attached to). Currently any instance can
Jonathan Hart03102132014-07-01 23:22:04 -0700167 // issue deletes for any host, which permits race conditions and could
Jonathan Hart83ce8c42014-06-02 00:07:06 -0700168 // cause the Topology replicas to diverge.
Jonathan Hart03102132014-07-01 23:22:04 -0700169 private class HostCleaner implements Runnable {
Ray Milkey269ffb92014-04-03 14:43:30 -0700170 @Override
171 public void run() {
Jonathan Hart03102132014-07-01 23:22:04 -0700172 log.debug("called HostCleaner");
TeruU5d2c9392014-06-09 20:02:02 -0700173 topology.acquireReadLock();
Ray Milkey269ffb92014-04-03 14:43:30 -0700174 try {
TeruU5d2c9392014-06-09 20:02:02 -0700175 Set<Device> deleteSet = new HashSet<Device>();
Jonathan Hart03102132014-07-01 23:22:04 -0700176 for (Device host : topology.getDevices()) {
TeruU5d2c9392014-06-09 20:02:02 -0700177 long now = System.currentTimeMillis();
Jonathan Hart03102132014-07-01 23:22:04 -0700178 if ((now - host.getLastSeenTime() > agingMillisecConfig)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700179 if (log.isTraceEnabled()) {
Jonathan Hart03102132014-07-01 23:22:04 -0700180 log.trace("Removing host info: mac {}, now {}, lastSeenTime {}, diff {}",
181 host.getMacAddress(), now, host.getLastSeenTime(), now - host.getLastSeenTime());
Ray Milkey269ffb92014-04-03 14:43:30 -0700182 }
Jonathan Hart03102132014-07-01 23:22:04 -0700183 deleteSet.add(host);
Ray Milkey269ffb92014-04-03 14:43:30 -0700184 }
185 }
Yuta HIGUCHIe7eac182014-03-19 19:18:30 -0700186
Jonathan Hart03102132014-07-01 23:22:04 -0700187 for (Device host : deleteSet) {
188 deleteHostByMac(host.getMacAddress());
Ray Milkey269ffb92014-04-03 14:43:30 -0700189 }
190 } catch (Exception e) {
Jonathan Hart83ce8c42014-06-02 00:07:06 -0700191 // Any exception thrown by the task will prevent the Executor
192 // from running the next iteration, so we need to catch and log
193 // all exceptions here.
Jonathan Hart03102132014-07-01 23:22:04 -0700194 log.error("Exception in host cleanup thread:", e);
TeruU5d2c9392014-06-09 20:02:02 -0700195 } finally {
196 topology.releaseReadLock();
Ray Milkey269ffb92014-04-03 14:43:30 -0700197 }
198 }
199 }
TeruU80ce5062014-03-03 17:16:13 -0800200
Jonathan Hartd857ad62013-12-14 18:08:17 -0800201 /**
Jonathan Hart03102132014-07-01 23:22:04 -0700202 * Parse a host from an {@link Ethernet} packet.
Ray Milkey269ffb92014-04-03 14:43:30 -0700203 *
Jonathan Hartd857ad62013-12-14 18:08:17 -0800204 * @param eth the packet to parse
Jonathan Hart83ce8c42014-06-02 00:07:06 -0700205 * @param swdpid the switch on which the packet arrived
206 * @param port the port on which the packet arrived
Jonathan Hart03102132014-07-01 23:22:04 -0700207 * @return the host from the packet
Jonathan Hartd857ad62013-12-14 18:08:17 -0800208 */
Jonathan Hart03102132014-07-01 23:22:04 -0700209 protected Host getSourceHostFromPacket(Ethernet eth,
210 long swdpid, long port) {
Jonathan Hart7ab71612014-05-27 13:37:31 -0700211 MACAddress sourceMac = eth.getSourceMAC();
Jonathan Hartd857ad62013-12-14 18:08:17 -0800212
Jonathan Hart7ab71612014-05-27 13:37:31 -0700213 // Ignore broadcast/multicast source
214 if (sourceMac.isBroadcast() || sourceMac.isBroadcast()) {
Jonathan Hartd857ad62013-12-14 18:08:17 -0800215 return null;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700216 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800217
218 short vlan = eth.getVlanID();
Jonathan Hart03102132014-07-01 23:22:04 -0700219 return new Host(sourceMac,
Ray Milkey269ffb92014-04-03 14:43:30 -0700220 ((vlan >= 0) ? vlan : null),
Ray Milkey269ffb92014-04-03 14:43:30 -0700221 swdpid,
222 port,
223 new Date());
Jonathan Hartd857ad62013-12-14 18:08:17 -0800224 }
225
Ray Milkey269ffb92014-04-03 14:43:30 -0700226 @Override
227 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
228 List<Class<? extends IFloodlightService>> services =
229 new ArrayList<Class<? extends IFloodlightService>>();
Jonathan Hart03102132014-07-01 23:22:04 -0700230 services.add(IHostService.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700231 return services;
232 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800233
TeruUd1c5b652014-03-24 13:58:46 -0700234 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -0700235 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
236 Map<Class<? extends IFloodlightService>, IFloodlightService> impls =
237 new HashMap<Class<? extends IFloodlightService>, IFloodlightService>();
Jonathan Hart03102132014-07-01 23:22:04 -0700238 impls.put(IHostService.class, this);
Ray Milkey269ffb92014-04-03 14:43:30 -0700239 return impls;
240 }
241
TeruUd1c5b652014-03-24 13:58:46 -0700242 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -0700243 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
244 List<Class<? extends IFloodlightService>> dependencies =
245 new ArrayList<Class<? extends IFloodlightService>>();
246 dependencies.add(IFloodlightProviderService.class);
Jonathan Harte37e4e22014-05-13 19:12:02 -0700247 dependencies.add(ITopologyService.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700248 return dependencies;
249 }
Yuta HIGUCHIe7eac182014-03-19 19:18:30 -0700250
Ray Milkey269ffb92014-04-03 14:43:30 -0700251 @Override
252 public void init(FloodlightModuleContext context)
253 throws FloodlightModuleException {
254 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Jonathan Hart03102132014-07-01 23:22:04 -0700255 hostListeners = new CopyOnWriteArrayList<IHostListener>();
Jonathan Harte37e4e22014-05-13 19:12:02 -0700256 topologyService = context.getServiceImpl(ITopologyService.class);
257 topology = topologyService.getTopology();
TeruU28adcc32014-04-15 17:57:35 -0700258
Jonathan Hart03102132014-07-01 23:22:04 -0700259 setHostManagerProperties(context);
Ray Milkey269ffb92014-04-03 14:43:30 -0700260 }
TeruU80ce5062014-03-03 17:16:13 -0800261
Ray Milkey269ffb92014-04-03 14:43:30 -0700262 @Override
263 public void startUp(FloodlightModuleContext context) {
264 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
Jonathan Hart03102132014-07-01 23:22:04 -0700265 EXECUTOR_SERVICE.scheduleAtFixedRate(new HostCleaner(),
266 HOST_CLEANING_INITIAL_DELAY, cleanupSecondConfig, TimeUnit.SECONDS);
Ray Milkey269ffb92014-04-03 14:43:30 -0700267 }
TeruUd1c5b652014-03-24 13:58:46 -0700268
Ray Milkey269ffb92014-04-03 14:43:30 -0700269 @Override
Jonathan Hart03102132014-07-01 23:22:04 -0700270 public void deleteHost(Host host) {
Jonathan Hart83ce8c42014-06-02 00:07:06 -0700271 floodlightProvider.publishUpdate(
Jonathan Hart03102132014-07-01 23:22:04 -0700272 new HostUpdate(host, HostUpdateType.DELETE));
Ray Milkey269ffb92014-04-03 14:43:30 -0700273 }
TeruUd1c5b652014-03-24 13:58:46 -0700274
Ray Milkey269ffb92014-04-03 14:43:30 -0700275 @Override
Jonathan Hart03102132014-07-01 23:22:04 -0700276 public void deleteHostByMac(MACAddress mac) {
277 Host deleteHost = null;
TeruU5d2c9392014-06-09 20:02:02 -0700278 topology.acquireReadLock();
279 try {
Jonathan Hart03102132014-07-01 23:22:04 -0700280 Device host = topology.getDeviceByMac(mac);
TeruU5d2c9392014-06-09 20:02:02 -0700281
Jonathan Hart03102132014-07-01 23:22:04 -0700282 for (Port switchPort : host.getAttachmentPoints()) {
TeruU5d2c9392014-06-09 20:02:02 -0700283 // We don't handle vlan now and multiple attachment points.
Jonathan Hart03102132014-07-01 23:22:04 -0700284 deleteHost = new Host(host.getMacAddress(),
TeruU5d2c9392014-06-09 20:02:02 -0700285 null,
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700286 switchPort.getDpid().value(),
287 (long) switchPort.getNumber().value(),
Jonathan Hart03102132014-07-01 23:22:04 -0700288 new Date(host.getLastSeenTime()));
TeruU5d2c9392014-06-09 20:02:02 -0700289 break;
290 }
291 } finally {
292 topology.releaseReadLock();
293 }
294
Jonathan Hart03102132014-07-01 23:22:04 -0700295 if (deleteHost != null) {
296 deleteHost(deleteHost);
TeruU5d2c9392014-06-09 20:02:02 -0700297 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700298 }
299
300 @Override
Jonathan Hart03102132014-07-01 23:22:04 -0700301 public void addHost(Long mac, Host host) {
Jonathan Hart83ce8c42014-06-02 00:07:06 -0700302 floodlightProvider.publishUpdate(
Jonathan Hart03102132014-07-01 23:22:04 -0700303 new HostUpdate(host, HostUpdateType.ADD));
Ray Milkey269ffb92014-04-03 14:43:30 -0700304 }
305
306 @Override
Jonathan Hart03102132014-07-01 23:22:04 -0700307 public void addHostListener(IHostListener listener) {
308 hostListeners.add(listener);
Ray Milkey269ffb92014-04-03 14:43:30 -0700309 }
310
311 @Override
Jonathan Hart03102132014-07-01 23:22:04 -0700312 public void removeHostListener(IHostListener listener) {
313 hostListeners.remove(listener);
Ray Milkey269ffb92014-04-03 14:43:30 -0700314 }
TeruU28adcc32014-04-15 17:57:35 -0700315
Jonathan Hart03102132014-07-01 23:22:04 -0700316 private void setHostManagerProperties(FloodlightModuleContext context) {
TeruU28adcc32014-04-15 17:57:35 -0700317 Map<String, String> configOptions = context.getConfigParams(this);
318 String cleanupsec = configOptions.get("cleanupsec");
319 String agingmsec = configOptions.get("agingmsec");
320 if (cleanupsec != null) {
321 cleanupSecondConfig = Integer.parseInt(cleanupsec);
322 log.debug("CLEANUP_SECOND is set to {}", cleanupSecondConfig);
323 }
324
325 if (agingmsec != null) {
326 agingMillisecConfig = Integer.parseInt(agingmsec);
327 log.debug("AGEING_MILLSEC is set to {}", agingMillisecConfig);
328 }
329 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800330}