Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.hostmanager; |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.Date; |
| 6 | import java.util.HashMap; |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 7 | import java.util.HashSet; |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 8 | import java.util.List; |
| 9 | import java.util.Map; |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 10 | import java.util.Set; |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 11 | import java.util.concurrent.CopyOnWriteArrayList; |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 12 | import java.util.concurrent.Executors; |
| 13 | import java.util.concurrent.ScheduledExecutorService; |
| 14 | import java.util.concurrent.TimeUnit; |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 15 | |
| 16 | import net.floodlightcontroller.core.FloodlightContext; |
| 17 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 18 | import net.floodlightcontroller.core.IOFMessageListener; |
| 19 | import net.floodlightcontroller.core.IOFSwitch; |
| 20 | import net.floodlightcontroller.core.IUpdate; |
| 21 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 22 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 23 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 24 | import net.floodlightcontroller.core.module.IFloodlightService; |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 25 | import net.floodlightcontroller.util.MACAddress; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 26 | import net.onrc.onos.core.packet.Ethernet; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 27 | import net.onrc.onos.core.topology.ITopologyService; |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 28 | import net.onrc.onos.core.topology.Port; |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 29 | import net.onrc.onos.core.topology.MutableTopology; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 30 | import net.onrc.onos.core.util.Dpid; |
| 31 | import net.onrc.onos.core.util.PortNumber; |
Yuta HIGUCHI | a507baf | 2014-08-22 13:42:40 -0700 | [diff] [blame] | 32 | import net.onrc.onos.core.util.PortNumberUtils; |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 33 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 34 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 35 | import org.projectfloodlight.openflow.protocol.OFPacketIn; |
| 36 | import org.projectfloodlight.openflow.protocol.OFType; |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 39 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 40 | public class HostManager implements IFloodlightModule, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 41 | IOFMessageListener, |
| 42 | IHostService { |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 43 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 44 | private static final Logger log = LoggerFactory.getLogger(HostManager.class); |
| 45 | private static final long HOST_CLEANING_INITIAL_DELAY = 30; |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 46 | private int cleanupSecondConfig = 60 * 60; |
| 47 | private int agingMillisecConfig = 60 * 60 * 1000; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 48 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 49 | private CopyOnWriteArrayList<IHostListener> hostListeners; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 50 | private IFloodlightProviderService floodlightProvider; |
Jonathan Hart | 83ce8c4 | 2014-06-02 00:07:06 -0700 | [diff] [blame] | 51 | private static final ScheduledExecutorService EXECUTOR_SERVICE = |
| 52 | Executors.newSingleThreadScheduledExecutor(); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 53 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 54 | private ITopologyService topologyService; |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 55 | private MutableTopology mutableTopology; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 56 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 57 | public enum HostUpdateType { |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 58 | ADD, DELETE, UPDATE; |
| 59 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 60 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 61 | private class HostUpdate implements IUpdate { |
| 62 | private final Host host; |
| 63 | private final HostUpdateType type; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 64 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 65 | public HostUpdate(Host host, HostUpdateType type) { |
| 66 | this.host = host; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 67 | this.type = type; |
| 68 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 69 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 70 | @Override |
| 71 | public void dispatch() { |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 72 | if (type == HostUpdateType.ADD) { |
| 73 | for (IHostListener listener : hostListeners) { |
| 74 | listener.hostAdded(host); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 75 | } |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 76 | } else if (type == HostUpdateType.DELETE) { |
| 77 | for (IHostListener listener : hostListeners) { |
| 78 | listener.hostRemoved(host); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 83 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 84 | @Override |
| 85 | public String getName() { |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 86 | return "hostmanager"; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 87 | } |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 88 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 89 | @Override |
| 90 | public boolean isCallbackOrderingPrereq(OFType type, String name) { |
| 91 | // We want link discovery to consume LLDP first otherwise we'll |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 92 | // end up reading bad host info from LLDP packets |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 93 | return type == OFType.PACKET_IN && "linkdiscovery".equals(name); |
| 94 | } |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 95 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 96 | @Override |
| 97 | public boolean isCallbackOrderingPostreq(OFType type, String name) { |
| 98 | return type == OFType.PACKET_IN && |
| 99 | ("proxyarpmanager".equals(name) || "onosforwarding".equals(name)); |
| 100 | } |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 101 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 102 | @Override |
| 103 | public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { |
Pavlin Radoslavov | 0b88a26 | 2014-04-10 15:43:27 -0700 | [diff] [blame] | 104 | if (msg.getType().equals(OFType.PACKET_IN) && |
Patrick Liu | ab1e606 | 2014-05-05 11:12:13 -0700 | [diff] [blame] | 105 | (msg instanceof OFPacketIn)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 106 | OFPacketIn pi = (OFPacketIn) msg; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 107 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 108 | Ethernet eth = IFloodlightProviderService.bcStore. |
| 109 | get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 110 | short inport = (short) cntx.getStorage() |
| 111 | .get(IFloodlightProviderService.CONTEXT_PI_INPORT); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 112 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 113 | return processPacketIn(sw, pi, eth, inport); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 114 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 115 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 116 | return Command.CONTINUE; |
| 117 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 118 | |
Jonathan Hart | 83ce8c4 | 2014-06-02 00:07:06 -0700 | [diff] [blame] | 119 | // This "protected" modifier is for unit test. |
| 120 | // The above "receive" method couldn't be tested |
| 121 | // because of IFloodlightProviderService static final field. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 122 | protected Command processPacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth, |
| 123 | short inport) { |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 124 | if (log.isTraceEnabled()) { |
Yuta HIGUCHI | a507baf | 2014-08-22 13:42:40 -0700 | [diff] [blame] | 125 | log.trace("Receive PACKET_IN swId {}, portId {}", sw.getId(), inport); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 128 | final Dpid dpid = new Dpid(sw.getId()); |
Yuta HIGUCHI | a507baf | 2014-08-22 13:42:40 -0700 | [diff] [blame] | 129 | // FIXME method signature needs to be fixed. losing port number precision |
| 130 | final PortNumber portNum = PortNumberUtils |
| 131 | .openFlow(sw.getOFVersion(), inport); |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 132 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 133 | Host srcHost = |
| 134 | getSourceHostFromPacket(eth, dpid.value(), portNum.value()); |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 135 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 136 | if (srcHost == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 137 | return Command.STOP; |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 138 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 139 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 140 | // If the switch port we try to attach a new host already has a link, |
| 141 | // then don't add the host |
Jonathan Hart | 83ce8c4 | 2014-06-02 00:07:06 -0700 | [diff] [blame] | 142 | // TODO We probably don't need to check this here, it should be done in |
| 143 | // the Topology module. |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 144 | mutableTopology.acquireReadLock(); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 145 | try { |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 146 | if (mutableTopology.getOutgoingLink(dpid, portNum) != null || |
| 147 | mutableTopology.getIncomingLink(dpid, portNum) != null) { |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 148 | log.debug("Not adding host {} as " + |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 149 | "there is a link on the port: dpid {} port {}", |
| 150 | srcHost.getMacAddress(), dpid, portNum); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 151 | return Command.CONTINUE; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 152 | } |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 153 | } finally { |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 154 | mutableTopology.releaseReadLock(); |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 155 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 156 | |
Ray Milkey | 9289721 | 2014-07-21 10:33:16 -0700 | [diff] [blame] | 157 | Long mac = eth.getSourceMAC().toLong(); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 158 | addHost(mac, srcHost); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 159 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 160 | if (log.isTraceEnabled()) { |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 161 | log.trace("Add host info: {}", srcHost); |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 162 | } |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 163 | return Command.CONTINUE; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 164 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 165 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 166 | // Thread to delete hosts periodically. |
| 167 | // Remove all hosts from the map first and then finally delete hosts |
Jonathan Hart | 83ce8c4 | 2014-06-02 00:07:06 -0700 | [diff] [blame] | 168 | // from the DB. |
| 169 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 170 | // TODO This should be sharded based on host 'owner' (i.e. the instance |
Jonathan Hart | 83ce8c4 | 2014-06-02 00:07:06 -0700 | [diff] [blame] | 171 | // that owns the switch it is attached to). Currently any instance can |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 172 | // issue deletes for any host, which permits race conditions and could |
Jonathan Hart | 83ce8c4 | 2014-06-02 00:07:06 -0700 | [diff] [blame] | 173 | // cause the Topology replicas to diverge. |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 174 | private class HostCleaner implements Runnable { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 175 | @Override |
| 176 | public void run() { |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 177 | log.debug("called HostCleaner"); |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 178 | mutableTopology.acquireReadLock(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 179 | try { |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 180 | Set<net.onrc.onos.core.topology.Host> deleteSet = new HashSet<>(); |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 181 | for (net.onrc.onos.core.topology.Host host : mutableTopology.getHosts()) { |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 182 | long now = System.currentTimeMillis(); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 183 | if ((now - host.getLastSeenTime() > agingMillisecConfig)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 184 | if (log.isTraceEnabled()) { |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 185 | log.trace("Removing host info: mac {}, now {}, lastSeenTime {}, diff {}", |
| 186 | host.getMacAddress(), now, host.getLastSeenTime(), now - host.getLastSeenTime()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 187 | } |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 188 | deleteSet.add(host); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 191 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 192 | for (net.onrc.onos.core.topology.Host host : deleteSet) { |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 193 | deleteHostByMac(host.getMacAddress()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 194 | } |
| 195 | } catch (Exception e) { |
Jonathan Hart | 83ce8c4 | 2014-06-02 00:07:06 -0700 | [diff] [blame] | 196 | // Any exception thrown by the task will prevent the Executor |
| 197 | // from running the next iteration, so we need to catch and log |
| 198 | // all exceptions here. |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 199 | log.error("Exception in host cleanup thread:", e); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 200 | } finally { |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 201 | mutableTopology.releaseReadLock(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | } |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 205 | |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 206 | /** |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 207 | * Parse a host from an {@link Ethernet} packet. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 208 | * |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 209 | * @param eth the packet to parse |
Jonathan Hart | 83ce8c4 | 2014-06-02 00:07:06 -0700 | [diff] [blame] | 210 | * @param swdpid the switch on which the packet arrived |
| 211 | * @param port the port on which the packet arrived |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 212 | * @return the host from the packet |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 213 | */ |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 214 | protected Host getSourceHostFromPacket(Ethernet eth, |
| 215 | long swdpid, long port) { |
Jonathan Hart | 7ab7161 | 2014-05-27 13:37:31 -0700 | [diff] [blame] | 216 | MACAddress sourceMac = eth.getSourceMAC(); |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 217 | |
Jonathan Hart | 7ab7161 | 2014-05-27 13:37:31 -0700 | [diff] [blame] | 218 | // Ignore broadcast/multicast source |
| 219 | if (sourceMac.isBroadcast() || sourceMac.isBroadcast()) { |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 220 | return null; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 221 | } |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 222 | |
| 223 | short vlan = eth.getVlanID(); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 224 | return new Host(sourceMac, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 225 | ((vlan >= 0) ? vlan : null), |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 226 | swdpid, |
| 227 | port, |
| 228 | new Date()); |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 231 | @Override |
| 232 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 233 | List<Class<? extends IFloodlightService>> services = |
| 234 | new ArrayList<Class<? extends IFloodlightService>>(); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 235 | services.add(IHostService.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 236 | return services; |
| 237 | } |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 238 | |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 239 | @Override |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 240 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 241 | Map<Class<? extends IFloodlightService>, IFloodlightService> impls = |
| 242 | new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 243 | impls.put(IHostService.class, this); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 244 | return impls; |
| 245 | } |
| 246 | |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 247 | @Override |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 248 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 249 | List<Class<? extends IFloodlightService>> dependencies = |
| 250 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 251 | dependencies.add(IFloodlightProviderService.class); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 252 | dependencies.add(ITopologyService.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 253 | return dependencies; |
| 254 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 255 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 256 | @Override |
| 257 | public void init(FloodlightModuleContext context) |
| 258 | throws FloodlightModuleException { |
| 259 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 260 | hostListeners = new CopyOnWriteArrayList<IHostListener>(); |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 261 | topologyService = context.getServiceImpl(ITopologyService.class); |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 262 | mutableTopology = topologyService.getTopology(); |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 263 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 264 | setHostManagerProperties(context); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 265 | } |
TeruU | 80ce506 | 2014-03-03 17:16:13 -0800 | [diff] [blame] | 266 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 267 | @Override |
| 268 | public void startUp(FloodlightModuleContext context) { |
| 269 | floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this); |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 270 | EXECUTOR_SERVICE.scheduleAtFixedRate(new HostCleaner(), |
| 271 | HOST_CLEANING_INITIAL_DELAY, cleanupSecondConfig, TimeUnit.SECONDS); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 272 | } |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 273 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 274 | @Override |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 275 | public void deleteHost(Host host) { |
Jonathan Hart | 83ce8c4 | 2014-06-02 00:07:06 -0700 | [diff] [blame] | 276 | floodlightProvider.publishUpdate( |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 277 | new HostUpdate(host, HostUpdateType.DELETE)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 278 | } |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 279 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 280 | @Override |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 281 | public void deleteHostByMac(MACAddress mac) { |
| 282 | Host deleteHost = null; |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 283 | mutableTopology.acquireReadLock(); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 284 | try { |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 285 | net.onrc.onos.core.topology.Host host = mutableTopology.getHostByMac(mac); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 286 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 287 | for (Port switchPort : host.getAttachmentPoints()) { |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 288 | // We don't handle vlan now and multiple attachment points. |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 289 | deleteHost = new Host(host.getMacAddress(), |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 290 | null, |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 291 | switchPort.getDpid().value(), |
Yuta HIGUCHI | 9da3a6e | 2014-06-10 22:11:58 -0700 | [diff] [blame] | 292 | switchPort.getNumber().value(), |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 293 | new Date(host.getLastSeenTime())); |
Ray Milkey | 9289721 | 2014-07-21 10:33:16 -0700 | [diff] [blame] | 294 | // FIXME: remove NOPMD flag after multiple attachment points are implemented |
| 295 | break; // NOPMD |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 296 | } |
| 297 | } finally { |
Yuta HIGUCHI | d92b10c | 2014-08-25 09:30:28 -0700 | [diff] [blame] | 298 | mutableTopology.releaseReadLock(); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 301 | if (deleteHost != null) { |
| 302 | deleteHost(deleteHost); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 303 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | @Override |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 307 | public void addHost(Long mac, Host host) { |
Jonathan Hart | 83ce8c4 | 2014-06-02 00:07:06 -0700 | [diff] [blame] | 308 | floodlightProvider.publishUpdate( |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 309 | new HostUpdate(host, HostUpdateType.ADD)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | @Override |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 313 | public void addHostListener(IHostListener listener) { |
| 314 | hostListeners.add(listener); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | @Override |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 318 | public void removeHostListener(IHostListener listener) { |
| 319 | hostListeners.remove(listener); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 320 | } |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 321 | |
Jonathan Hart | 0310213 | 2014-07-01 23:22:04 -0700 | [diff] [blame] | 322 | private void setHostManagerProperties(FloodlightModuleContext context) { |
TeruU | 28adcc3 | 2014-04-15 17:57:35 -0700 | [diff] [blame] | 323 | Map<String, String> configOptions = context.getConfigParams(this); |
| 324 | String cleanupsec = configOptions.get("cleanupsec"); |
| 325 | String agingmsec = configOptions.get("agingmsec"); |
| 326 | if (cleanupsec != null) { |
| 327 | cleanupSecondConfig = Integer.parseInt(cleanupsec); |
| 328 | log.debug("CLEANUP_SECOND is set to {}", cleanupSecondConfig); |
| 329 | } |
| 330 | |
| 331 | if (agingmsec != null) { |
| 332 | agingMillisecConfig = Integer.parseInt(agingmsec); |
| 333 | log.debug("AGEING_MILLSEC is set to {}", agingMillisecConfig); |
| 334 | } |
| 335 | } |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 336 | } |