Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.proxyarp; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.net.InetAddress; |
| 5 | import java.net.UnknownHostException; |
| 6 | import java.util.ArrayList; |
| 7 | import java.util.Collection; |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 8 | import java.util.HashMap; |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 9 | import java.util.HashSet; |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 10 | import java.util.Iterator; |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 11 | import java.util.List; |
| 12 | import java.util.Map; |
| 13 | import java.util.Set; |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 14 | import java.util.Timer; |
| 15 | import java.util.TimerTask; |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 16 | |
| 17 | import net.floodlightcontroller.core.FloodlightContext; |
| 18 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 19 | import net.floodlightcontroller.core.IOFMessageListener; |
| 20 | import net.floodlightcontroller.core.IOFSwitch; |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 21 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 22 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 23 | import net.floodlightcontroller.core.module.IFloodlightService; |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 24 | import net.floodlightcontroller.packet.ARP; |
| 25 | import net.floodlightcontroller.packet.Ethernet; |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 26 | import net.floodlightcontroller.packet.IPv4; |
Jonathan Hart | 5afde49 | 2013-10-01 12:30:53 +1300 | [diff] [blame] | 27 | import net.floodlightcontroller.restserver.IRestApiService; |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 28 | import net.floodlightcontroller.topology.ITopologyService; |
Jonathan Hart | 8ec133c | 2013-06-26 15:25:18 +1200 | [diff] [blame] | 29 | import net.floodlightcontroller.util.MACAddress; |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 30 | import net.onrc.onos.datagrid.IDatagridService; |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 31 | import net.onrc.onos.ofcontroller.bgproute.Interface; |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 32 | import net.onrc.onos.ofcontroller.core.IDeviceStorage; |
| 33 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject; |
Jonathan Hart | 9a21e0d | 2013-11-14 15:09:09 -0800 | [diff] [blame] | 34 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
pingping-lin | 017a892 | 2013-12-11 11:15:33 +0800 | [diff] [blame] | 35 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 36 | import net.onrc.onos.ofcontroller.core.INetMapTopologyService.ITopoSwitchService; |
Jonathan Hart | ebba1e1 | 2013-10-29 11:37:02 -0700 | [diff] [blame] | 37 | import net.onrc.onos.ofcontroller.core.config.IConfigInfoService; |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 38 | import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl; |
Jonathan Hart | 9a21e0d | 2013-11-14 15:09:09 -0800 | [diff] [blame] | 39 | import net.onrc.onos.ofcontroller.core.internal.TopoSwitchServiceImpl; |
Jonathan Hart | ba9ced9 | 2013-11-24 16:52:13 -0800 | [diff] [blame] | 40 | import net.onrc.onos.ofcontroller.util.Dpid; |
| 41 | import net.onrc.onos.ofcontroller.util.Port; |
| 42 | import net.onrc.onos.ofcontroller.util.SwitchPort; |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 43 | |
| 44 | import org.openflow.protocol.OFMessage; |
| 45 | import org.openflow.protocol.OFPacketIn; |
| 46 | import org.openflow.protocol.OFPacketOut; |
| 47 | import org.openflow.protocol.OFPort; |
| 48 | import org.openflow.protocol.OFType; |
| 49 | import org.openflow.protocol.action.OFAction; |
| 50 | import org.openflow.protocol.action.OFActionOutput; |
Jonathan Hart | 8ec133c | 2013-06-26 15:25:18 +1200 | [diff] [blame] | 51 | import org.openflow.util.HexString; |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 52 | import org.slf4j.Logger; |
| 53 | import org.slf4j.LoggerFactory; |
| 54 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 55 | import com.google.common.collect.HashMultimap; |
| 56 | import com.google.common.collect.Multimaps; |
| 57 | import com.google.common.collect.SetMultimap; |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 58 | import com.google.common.net.InetAddresses; |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 59 | |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 60 | public class ProxyArpManager implements IProxyArpService, IOFMessageListener, |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 61 | IArpEventHandler, IFloodlightModule { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 62 | private final static Logger log = LoggerFactory.getLogger(ProxyArpManager.class); |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 63 | |
pingping-lin | 017a892 | 2013-12-11 11:15:33 +0800 | [diff] [blame] | 64 | private final long ARP_TIMER_PERIOD = 100; //ms |
| 65 | |
| 66 | private static final int ARP_REQUEST_TIMEOUT = 500; //ms |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 67 | |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 68 | private IFloodlightProviderService floodlightProvider; |
| 69 | private ITopologyService topology; |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 70 | private IDatagridService datagrid; |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 71 | private IConfigInfoService configService; |
| 72 | private IRestApiService restApi; |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 73 | |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 74 | private IDeviceStorage deviceStorage; |
Jonathan Hart | 9a21e0d | 2013-11-14 15:09:09 -0800 | [diff] [blame] | 75 | private volatile ITopoSwitchService topoSwitchService; |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 76 | |
Jonathan Hart | 1cf9de0 | 2013-10-21 17:42:29 -0700 | [diff] [blame] | 77 | private short vlan; |
| 78 | private static final short NO_VLAN = 0; |
| 79 | |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 80 | private ArpCache arpCache; |
Jonathan Hart | df6ec33 | 2013-08-04 01:37:14 +1200 | [diff] [blame] | 81 | |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 82 | private SetMultimap<InetAddress, ArpRequest> arpRequests; |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 83 | |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 84 | private static class ArpRequest { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 85 | private final IArpRequester requester; |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 86 | private final boolean retry; |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 87 | private long requestTime; |
| 88 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 89 | public ArpRequest(IArpRequester requester, boolean retry){ |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 90 | this.requester = requester; |
| 91 | this.retry = retry; |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 92 | this.requestTime = System.currentTimeMillis(); |
| 93 | } |
| 94 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 95 | public ArpRequest(ArpRequest old) { |
| 96 | this.requester = old.requester; |
| 97 | this.retry = old.retry; |
| 98 | this.requestTime = System.currentTimeMillis(); |
| 99 | } |
| 100 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 101 | public boolean isExpired() { |
Jonathan Hart | da4d0e1 | 2013-09-30 21:00:20 +1300 | [diff] [blame] | 102 | return (System.currentTimeMillis() - requestTime) > ARP_REQUEST_TIMEOUT; |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 103 | } |
| 104 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 105 | public boolean shouldRetry() { |
| 106 | return retry; |
| 107 | } |
| 108 | |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 109 | public void dispatchReply(InetAddress ipAddress, MACAddress replyMacAddress) { |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 110 | requester.arpResponse(ipAddress, replyMacAddress); |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
Jonathan Hart | da4d0e1 | 2013-09-30 21:00:20 +1300 | [diff] [blame] | 114 | private class HostArpRequester implements IArpRequester { |
| 115 | private final ARP arpRequest; |
| 116 | private final long dpid; |
| 117 | private final short port; |
| 118 | |
| 119 | public HostArpRequester(ARP arpRequest, long dpid, short port) { |
| 120 | this.arpRequest = arpRequest; |
| 121 | this.dpid = dpid; |
| 122 | this.port = port; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void arpResponse(InetAddress ipAddress, MACAddress macAddress) { |
| 127 | ProxyArpManager.this.sendArpReply(arpRequest, dpid, port, macAddress); |
| 128 | } |
| 129 | } |
| 130 | |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 131 | @Override |
| 132 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 133 | Collection<Class<? extends IFloodlightService>> l |
| 134 | = new ArrayList<Class<? extends IFloodlightService>>(); |
| 135 | l.add(IProxyArpService.class); |
| 136 | return l; |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 137 | } |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 138 | |
| 139 | @Override |
| 140 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 141 | Map<Class<? extends IFloodlightService>, IFloodlightService> m |
| 142 | = new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(); |
| 143 | m.put(IProxyArpService.class, this); |
| 144 | return m; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 149 | Collection<Class<? extends IFloodlightService>> dependencies |
| 150 | = new ArrayList<Class<? extends IFloodlightService>>(); |
| 151 | dependencies.add(IFloodlightProviderService.class); |
| 152 | dependencies.add(ITopologyService.class); |
| 153 | dependencies.add(IRestApiService.class); |
| 154 | dependencies.add(IDatagridService.class); |
| 155 | dependencies.add(IConfigInfoService.class); |
| 156 | return dependencies; |
| 157 | } |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 158 | |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 159 | @Override |
| 160 | public void init(FloodlightModuleContext context){ |
| 161 | this.floodlightProvider = |
| 162 | context.getServiceImpl(IFloodlightProviderService.class); |
| 163 | this.topology = context.getServiceImpl(ITopologyService.class); |
| 164 | this.datagrid = context.getServiceImpl(IDatagridService.class); |
| 165 | this.configService = context.getServiceImpl(IConfigInfoService.class); |
| 166 | this.restApi = context.getServiceImpl(IRestApiService.class); |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 167 | |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 168 | arpCache = new ArpCache(); |
Jonathan Hart | df6ec33 | 2013-08-04 01:37:14 +1200 | [diff] [blame] | 169 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 170 | arpRequests = Multimaps.synchronizedSetMultimap( |
| 171 | HashMultimap.<InetAddress, ArpRequest>create()); |
Jonathan Hart | 9a21e0d | 2013-11-14 15:09:09 -0800 | [diff] [blame] | 172 | |
| 173 | topoSwitchService = new TopoSwitchServiceImpl(); |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 174 | } |
| 175 | |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 176 | @Override |
| 177 | public void startUp(FloodlightModuleContext context) { |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 178 | this.vlan = configService.getVlan(); |
Jonathan Hart | 1cf9de0 | 2013-10-21 17:42:29 -0700 | [diff] [blame] | 179 | log.info("vlan set to {}", this.vlan); |
| 180 | |
Jonathan Hart | 5afde49 | 2013-10-01 12:30:53 +1300 | [diff] [blame] | 181 | restApi.addRestletRoutable(new ArpWebRoutable()); |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 182 | floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this); |
Jonathan Hart | 5afde49 | 2013-10-01 12:30:53 +1300 | [diff] [blame] | 183 | |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 184 | datagrid.registerArpEventHandler(this); |
| 185 | |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 186 | deviceStorage = new DeviceStorageImpl(); |
| 187 | deviceStorage.init(""); |
| 188 | |
Jonathan Hart | 4aa2b4e | 2013-09-24 14:50:23 +1200 | [diff] [blame] | 189 | Timer arpTimer = new Timer("arp-processing"); |
Jonathan Hart | df6ec33 | 2013-08-04 01:37:14 +1200 | [diff] [blame] | 190 | arpTimer.scheduleAtFixedRate(new TimerTask() { |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 191 | @Override |
| 192 | public void run() { |
Jonathan Hart | df6ec33 | 2013-08-04 01:37:14 +1200 | [diff] [blame] | 193 | doPeriodicArpProcessing(); |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 194 | } |
Jonathan Hart | df6ec33 | 2013-08-04 01:37:14 +1200 | [diff] [blame] | 195 | }, 0, ARP_TIMER_PERIOD); |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 196 | } |
| 197 | |
Jonathan Hart | df6ec33 | 2013-08-04 01:37:14 +1200 | [diff] [blame] | 198 | /* |
| 199 | * Function that runs periodically to manage the asynchronous request mechanism. |
| 200 | * It basically cleans up old ARP requests if we don't get a response for them. |
| 201 | * The caller can designate that a request should be retried indefinitely, and |
| 202 | * this task will handle that as well. |
| 203 | */ |
| 204 | private void doPeriodicArpProcessing() { |
| 205 | SetMultimap<InetAddress, ArpRequest> retryList |
| 206 | = HashMultimap.<InetAddress, ArpRequest>create(); |
| 207 | |
| 208 | //Have to synchronize externally on the Multimap while using an iterator, |
| 209 | //even though it's a synchronizedMultimap |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 210 | synchronized (arpRequests) { |
Jonathan Hart | 3cd4862 | 2013-12-12 14:43:40 -0800 | [diff] [blame] | 211 | //log.debug("Current have {} outstanding requests", |
| 212 | //arpRequests.size()); |
Jonathan Hart | df6ec33 | 2013-08-04 01:37:14 +1200 | [diff] [blame] | 213 | |
| 214 | Iterator<Map.Entry<InetAddress, ArpRequest>> it |
| 215 | = arpRequests.entries().iterator(); |
| 216 | |
| 217 | while (it.hasNext()) { |
| 218 | Map.Entry<InetAddress, ArpRequest> entry |
| 219 | = it.next(); |
| 220 | ArpRequest request = entry.getValue(); |
| 221 | if (request.isExpired()) { |
| 222 | log.debug("Cleaning expired ARP request for {}", |
| 223 | entry.getKey().getHostAddress()); |
| 224 | |
pingping-lin | 017a892 | 2013-12-11 11:15:33 +0800 | [diff] [blame] | 225 | //if he ARP Request is expired and then delete the device |
| 226 | IDeviceObject targetDevice = |
| 227 | deviceStorage.getDeviceByIP(InetAddresses.coerceToInteger(entry.getKey())); |
| 228 | |
| 229 | if(targetDevice!=null) |
| 230 | {deviceStorage.removeDevice(targetDevice); |
| 231 | log.debug("RemoveDevice: {} due to no have not recieve the ARP reply", targetDevice.toString()); |
| 232 | } |
| 233 | |
Jonathan Hart | df6ec33 | 2013-08-04 01:37:14 +1200 | [diff] [blame] | 234 | it.remove(); |
| 235 | |
| 236 | if (request.shouldRetry()) { |
| 237 | retryList.put(entry.getKey(), request); |
| 238 | } |
| 239 | } |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 240 | } |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 241 | } |
Jonathan Hart | df6ec33 | 2013-08-04 01:37:14 +1200 | [diff] [blame] | 242 | |
| 243 | for (Map.Entry<InetAddress, Collection<ArpRequest>> entry |
| 244 | : retryList.asMap().entrySet()) { |
| 245 | |
| 246 | InetAddress address = entry.getKey(); |
| 247 | |
| 248 | log.debug("Resending ARP request for {}", address.getHostAddress()); |
| 249 | |
| 250 | sendArpRequestForAddress(address); |
| 251 | |
| 252 | for (ArpRequest request : entry.getValue()) { |
| 253 | arpRequests.put(address, new ArpRequest(request)); |
| 254 | } |
| 255 | } |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 256 | } |
| 257 | |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 258 | @Override |
| 259 | public String getName() { |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 260 | return "proxyarpmanager"; |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | @Override |
| 264 | public boolean isCallbackOrderingPrereq(OFType type, String name) { |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 265 | if (type == OFType.PACKET_IN) { |
| 266 | return "devicemanager".equals(name); |
| 267 | } |
| 268 | else { |
| 269 | return false; |
| 270 | } |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | @Override |
| 274 | public boolean isCallbackOrderingPostreq(OFType type, String name) { |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 275 | return false; |
| 276 | } |
| 277 | |
| 278 | @Override |
| 279 | public Command receive( |
| 280 | IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { |
| 281 | |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 282 | OFPacketIn pi = (OFPacketIn) msg; |
| 283 | |
| 284 | Ethernet eth = IFloodlightProviderService.bcStore.get(cntx, |
| 285 | IFloodlightProviderService.CONTEXT_PI_PAYLOAD); |
| 286 | |
| 287 | if (eth.getEtherType() == Ethernet.TYPE_ARP){ |
Jonathan Hart | 5ea7cc0 | 2013-11-12 11:56:23 -0800 | [diff] [blame] | 288 | ARP arp = (ARP) eth.getPayload(); |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 289 | if (arp.getOpCode() == ARP.OP_REQUEST) { |
pingping-lin | 017a892 | 2013-12-11 11:15:33 +0800 | [diff] [blame] | 290 | log.debug("receive ARP request"); |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 291 | //TODO check what the DeviceManager does about propagating |
| 292 | //or swallowing ARPs. We want to go after DeviceManager in the |
| 293 | //chain but we really need it to CONTINUE ARP packets so we can |
| 294 | //get them. |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 295 | handleArpRequest(sw, pi, arp, eth); |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 296 | } |
| 297 | else if (arp.getOpCode() == ARP.OP_REPLY) { |
pingping-lin | 017a892 | 2013-12-11 11:15:33 +0800 | [diff] [blame] | 298 | log.debug("receive ARP reply"); |
| 299 | handleArpReply(sw, pi, arp); |
| 300 | sendToOtherNodesReply(eth, pi); |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 301 | } |
Jonathan Hart | 1767299 | 2013-12-12 16:15:16 -0800 | [diff] [blame] | 302 | |
| 303 | // Stop ARP packets here |
| 304 | return Command.STOP; |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 305 | } |
| 306 | |
Jonathan Hart | 1767299 | 2013-12-12 16:15:16 -0800 | [diff] [blame] | 307 | // Propagate everything else |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 308 | return Command.CONTINUE; |
| 309 | } |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 310 | |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 311 | private void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp, Ethernet eth) { |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 312 | if (log.isTraceEnabled()) { |
| 313 | log.trace("ARP request received for {}", |
| 314 | inetAddressToString(arp.getTargetProtocolAddress())); |
| 315 | } |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 316 | |
| 317 | InetAddress target; |
| 318 | try { |
pingping-lin | b8757bf | 2013-12-13 01:48:58 +0800 | [diff] [blame] | 319 | target = InetAddress.getByAddress(arp.getTargetProtocolAddress()); |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 320 | } catch (UnknownHostException e) { |
| 321 | log.debug("Invalid address in ARP request", e); |
| 322 | return; |
| 323 | } |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 324 | |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 325 | if (configService.fromExternalNetwork(sw.getId(), pi.getInPort())) { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 326 | //If the request came from outside our network, we only care if |
| 327 | //it was a request for one of our interfaces. |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 328 | if (configService.isInterfaceAddress(target)) { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 329 | log.trace("ARP request for our interface. Sending reply {} => {}", |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 330 | target.getHostAddress(), configService.getRouterMacAddress()); |
pingping-lin | b8757bf | 2013-12-13 01:48:58 +0800 | [diff] [blame] | 331 | |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 332 | sendArpReply(arp, sw.getId(), pi.getInPort(), |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 333 | configService.getRouterMacAddress()); |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 334 | } |
pingping-lin | b8757bf | 2013-12-13 01:48:58 +0800 | [diff] [blame] | 335 | |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 336 | return; |
| 337 | } |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 338 | |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 339 | //MACAddress macAddress = arpCache.lookup(target); |
pingping-lin | 017a892 | 2013-12-11 11:15:33 +0800 | [diff] [blame] | 340 | |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 341 | IDeviceObject targetDevice = |
| 342 | deviceStorage.getDeviceByIP(InetAddresses.coerceToInteger(target)); |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 343 | log.debug("targetDevice: {}", targetDevice); |
pingping-lin | b8757bf | 2013-12-13 01:48:58 +0800 | [diff] [blame] | 344 | |
| 345 | arpRequests.put(target, new ArpRequest( |
| 346 | new HostArpRequester(arp, sw.getId(), pi.getInPort()), false)); |
| 347 | |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 348 | if (targetDevice != null) { |
pingping-lin | b8757bf | 2013-12-13 01:48:58 +0800 | [diff] [blame] | 349 | // Even the device in our database is not null, we do not reply to the request directly, but to check whether the device is still valid |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 350 | MACAddress macAddress = MACAddress.valueOf(targetDevice.getMACAddress()); |
pingping-lin | b8757bf | 2013-12-13 01:48:58 +0800 | [diff] [blame] | 351 | |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 352 | if (log.isTraceEnabled()) { |
pingping-lin | b8757bf | 2013-12-13 01:48:58 +0800 | [diff] [blame] | 353 | log.trace("The target Device Record in DB is: {} => {} from ARP request host at {}/{}", new Object [] { |
| 354 | inetAddressToString(arp.getTargetProtocolAddress()), |
| 355 | macAddress.toString(), |
| 356 | HexString.toHexString(sw.getId()), pi.getInPort()}); |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 357 | } |
pingping-lin | b8757bf | 2013-12-13 01:48:58 +0800 | [diff] [blame] | 358 | |
| 359 | // sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress); |
| 360 | |
| 361 | log.trace("Checking the device info from DB is still valid or not"); |
| 362 | Iterable<IPortObject> outPorts=targetDevice.getAttachedPorts(); |
| 363 | |
| 364 | if(!outPorts.iterator().hasNext()){ |
| 365 | log.debug("outPort : null"); |
Jonathan Hart | f9bd98d | 2013-12-13 11:40:55 -0800 | [diff] [blame] | 366 | sendToOtherNodes(eth, sw.getId(), pi); |
pingping-lin | b8757bf | 2013-12-13 01:48:58 +0800 | [diff] [blame] | 367 | }else{ |
| 368 | |
| 369 | for (IPortObject portObject : outPorts) { |
| 370 | long outSwitch=0; |
| 371 | short outPort=0; |
| 372 | |
| 373 | |
| 374 | if (!portObject.getLinkedPorts().iterator().hasNext()) { |
| 375 | outPort=portObject.getNumber(); |
| 376 | log.debug("outPort:{} ", outPort); |
| 377 | } |
| 378 | |
pingping-lin | 568c601 | 2013-12-14 05:57:11 +0800 | [diff] [blame] | 379 | ISwitchObject outSwitchObject = portObject.getSwitch(); |
| 380 | outSwitch= HexString.toLong(outSwitchObject.getDPID()); |
| 381 | log.debug("outSwitch.DPID:{}; outPort: {}", outSwitchObject.getDPID(), outPort ); |
| 382 | sendToOtherNodes( eth, pi, outSwitch, outPort); |
| 383 | |
pingping-lin | b8757bf | 2013-12-13 01:48:58 +0800 | [diff] [blame] | 384 | } |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 385 | } |
pingping-lin | b8757bf | 2013-12-13 01:48:58 +0800 | [diff] [blame] | 386 | |
| 387 | }else { |
| 388 | log.debug("The Device info in DB is {} for IP {}", targetDevice, inetAddressToString(arp.getTargetProtocolAddress())); |
| 389 | |
| 390 | // We don't know the device so broadcast the request out |
Jonathan Hart | f9bd98d | 2013-12-13 11:40:55 -0800 | [diff] [blame] | 391 | sendToOtherNodes(eth, sw.getId(), pi); |
pingping-lin | 017a892 | 2013-12-11 11:15:33 +0800 | [diff] [blame] | 392 | } |
| 393 | |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 394 | } |
| 395 | |
Jonathan Hart | 1912afc | 2013-10-11 12:02:44 +1300 | [diff] [blame] | 396 | private void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){ |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 397 | if (log.isTraceEnabled()) { |
| 398 | log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] { |
| 399 | inetAddressToString(arp.getSenderProtocolAddress()), |
| 400 | HexString.toHexString(arp.getSenderHardwareAddress()), |
| 401 | HexString.toHexString(sw.getId()), pi.getInPort()}); |
| 402 | } |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 403 | |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 404 | InetAddress senderIpAddress; |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 405 | try { |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 406 | senderIpAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress()); |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 407 | } catch (UnknownHostException e) { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 408 | log.debug("Invalid address in ARP reply", e); |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 409 | return; |
| 410 | } |
| 411 | |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 412 | MACAddress senderMacAddress = MACAddress.valueOf(arp.getSenderHardwareAddress()); |
| 413 | |
pingping-lin | 017a892 | 2013-12-11 11:15:33 +0800 | [diff] [blame] | 414 | //arpCache.update(senderIpAddress, senderMacAddress); |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 415 | |
| 416 | //See if anyone's waiting for this ARP reply |
| 417 | Set<ArpRequest> requests = arpRequests.get(senderIpAddress); |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 418 | |
| 419 | //Synchronize on the Multimap while using an iterator for one of the sets |
Jonathan Hart | e751e1c | 2013-08-23 00:48:47 +1200 | [diff] [blame] | 420 | List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size()); |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 421 | synchronized (arpRequests) { |
| 422 | Iterator<ArpRequest> it = requests.iterator(); |
| 423 | while (it.hasNext()) { |
| 424 | ArpRequest request = it.next(); |
| 425 | it.remove(); |
Jonathan Hart | e751e1c | 2013-08-23 00:48:47 +1200 | [diff] [blame] | 426 | requestsToSend.add(request); |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 427 | } |
| 428 | } |
Jonathan Hart | e751e1c | 2013-08-23 00:48:47 +1200 | [diff] [blame] | 429 | |
| 430 | //Don't hold an ARP lock while dispatching requests |
| 431 | for (ArpRequest request : requestsToSend) { |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 432 | request.dispatchReply(senderIpAddress, senderMacAddress); |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 436 | private void sendArpRequestForAddress(InetAddress ipAddress) { |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 437 | //TODO what should the sender IP address and MAC address be if no |
| 438 | //IP addresses are configured? Will there ever be a need to send |
| 439 | //ARP requests from the controller in that case? |
| 440 | //All-zero MAC address doesn't seem to work - hosts don't respond to it |
| 441 | |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 442 | byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0}; |
| 443 | byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 444 | byte[] genericNonZeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01}; |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 445 | byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff, |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 446 | (byte)0xff, (byte)0xff, (byte)0xff}; |
| 447 | |
| 448 | ARP arpRequest = new ARP(); |
| 449 | |
| 450 | arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET) |
| 451 | .setProtocolType(ARP.PROTO_TYPE_IP) |
| 452 | .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH) |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 453 | .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH) |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 454 | .setOpCode(ARP.OP_REQUEST) |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 455 | .setTargetHardwareAddress(zeroMac) |
| 456 | .setTargetProtocolAddress(ipAddress.getAddress()); |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 457 | |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 458 | MACAddress routerMacAddress = configService.getRouterMacAddress(); |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 459 | //TODO hack for now as it's unclear what the MAC address should be |
| 460 | byte[] senderMacAddress = genericNonZeroMac; |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 461 | if (routerMacAddress != null) { |
| 462 | senderMacAddress = routerMacAddress.toBytes(); |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 463 | } |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 464 | arpRequest.setSenderHardwareAddress(senderMacAddress); |
| 465 | |
| 466 | byte[] senderIPAddress = zeroIpv4; |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 467 | Interface intf = configService.getOutgoingInterface(ipAddress); |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 468 | if (intf != null) { |
| 469 | senderIPAddress = intf.getIpAddress().getAddress(); |
| 470 | } |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 471 | |
| 472 | arpRequest.setSenderProtocolAddress(senderIPAddress); |
| 473 | |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 474 | Ethernet eth = new Ethernet(); |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 475 | eth.setSourceMACAddress(senderMacAddress) |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 476 | .setDestinationMACAddress(broadcastMac) |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 477 | .setEtherType(Ethernet.TYPE_ARP) |
| 478 | .setPayload(arpRequest); |
| 479 | |
Jonathan Hart | 1cf9de0 | 2013-10-21 17:42:29 -0700 | [diff] [blame] | 480 | if (vlan != NO_VLAN) { |
| 481 | eth.setVlanID(vlan) |
| 482 | .setPriorityCode((byte)0); |
| 483 | } |
| 484 | |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 485 | sendArpRequestToSwitches(ipAddress, eth.serialize()); |
| 486 | } |
| 487 | |
| 488 | private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 489 | sendArpRequestToSwitches(dstAddress, arpRequest, |
| 490 | 0, OFPort.OFPP_NONE.getValue()); |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 491 | } |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 492 | |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 493 | private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest, |
| 494 | long inSwitch, short inPort) { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 495 | |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 496 | if (configService.hasLayer3Configuration()) { |
| 497 | Interface intf = configService.getOutgoingInterface(dstAddress); |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 498 | if (intf != null) { |
| 499 | sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort()); |
| 500 | } |
| 501 | else { |
| 502 | //TODO here it should be broadcast out all non-interface edge ports. |
| 503 | //I think we can assume that if it's not a request for an external |
| 504 | //network, it's an ARP for a host in our own network. So we want to |
| 505 | //send it out all edge ports that don't have an interface configured |
| 506 | //to ensure it reaches all hosts in our network. |
| 507 | log.debug("No interface found to send ARP request for {}", |
| 508 | dstAddress.getHostAddress()); |
| 509 | } |
| 510 | } |
| 511 | else { |
| 512 | broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort); |
| 513 | } |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 514 | } |
| 515 | |
Jonathan Hart | f9bd98d | 2013-12-13 11:40:55 -0800 | [diff] [blame] | 516 | private void sendToOtherNodes(Ethernet eth, long inSwitchId, OFPacketIn pi) { |
Jonathan Hart | 5ea7cc0 | 2013-11-12 11:56:23 -0800 | [diff] [blame] | 517 | ARP arp = (ARP) eth.getPayload(); |
Jonathan Hart | d300325 | 2013-11-15 09:44:46 -0800 | [diff] [blame] | 518 | |
Jonathan Hart | 5ea7cc0 | 2013-11-12 11:56:23 -0800 | [diff] [blame] | 519 | if (log.isTraceEnabled()) { |
| 520 | log.trace("Sending ARP request for {} to other ONOS instances", |
Jonathan Hart | 9a21e0d | 2013-11-14 15:09:09 -0800 | [diff] [blame] | 521 | inetAddressToString(arp.getTargetProtocolAddress())); |
Jonathan Hart | 5ea7cc0 | 2013-11-12 11:56:23 -0800 | [diff] [blame] | 522 | } |
Jonathan Hart | d300325 | 2013-11-15 09:44:46 -0800 | [diff] [blame] | 523 | |
| 524 | InetAddress targetAddress; |
| 525 | try { |
| 526 | targetAddress = InetAddress.getByAddress(arp.getTargetProtocolAddress()); |
| 527 | } catch (UnknownHostException e) { |
| 528 | log.error("Unknown host", e); |
| 529 | return; |
| 530 | } |
| 531 | |
Jonathan Hart | f9bd98d | 2013-12-13 11:40:55 -0800 | [diff] [blame] | 532 | datagrid.sendArpRequest(ArpMessage.newRequest(targetAddress, eth.serialize(), |
| 533 | -1L, (short)-1, inSwitchId, pi.getInPort())); |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 534 | } |
pingping-lin | 017a892 | 2013-12-11 11:15:33 +0800 | [diff] [blame] | 535 | //hazelcast to other ONOS instances to send the ARP packet out on outPort of outSwitch |
| 536 | private void sendToOtherNodes(Ethernet eth, OFPacketIn pi, long outSwitch, short outPort) { |
| 537 | ARP arp = (ARP) eth.getPayload(); |
| 538 | |
| 539 | if (log.isTraceEnabled()) { |
| 540 | log.trace("Sending ARP request for {} to other ONOS instances with outSwitch {} ", |
| 541 | inetAddressToString(arp.getTargetProtocolAddress()), String.valueOf(outSwitch)); |
| 542 | |
| 543 | } |
| 544 | |
| 545 | InetAddress targetAddress; |
| 546 | try { |
| 547 | targetAddress = InetAddress.getByAddress(arp.getTargetProtocolAddress()); |
| 548 | } catch (UnknownHostException e) { |
| 549 | log.error("Unknown host", e); |
| 550 | return; |
| 551 | } |
| 552 | |
| 553 | datagrid.sendArpRequest(ArpMessage.newRequest(targetAddress, eth.serialize(), outSwitch, outPort)); |
| 554 | //datagrid.sendArpRequest(ArpMessage.newRequest(targetAddress, eth.serialize())); |
| 555 | |
| 556 | |
| 557 | } |
| 558 | private void sendToOtherNodesReply(Ethernet eth, OFPacketIn pi) { |
| 559 | ARP arp = (ARP) eth.getPayload(); |
| 560 | |
| 561 | if (log.isTraceEnabled()) { |
| 562 | log.trace("Sending ARP reply for {} to other ONOS instances", |
| 563 | inetAddressToString(arp.getSenderProtocolAddress())); |
| 564 | } |
| 565 | |
| 566 | InetAddress targetAddress; |
| 567 | MACAddress mac = new MACAddress(arp.getSenderHardwareAddress()); |
| 568 | |
| 569 | try { |
| 570 | targetAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress()); |
| 571 | } catch (UnknownHostException e) { |
| 572 | log.error("Unknown host", e); |
| 573 | return; |
| 574 | } |
| 575 | |
| 576 | datagrid.sendArpRequest(ArpMessage.newReply(targetAddress,mac)); |
| 577 | //datagrid.sendArpReply(ArpMessage.newRequest(targetAddress, eth.serialize())); |
| 578 | |
| 579 | } |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 580 | |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 581 | private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) { |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 582 | for (IOFSwitch sw : floodlightProvider.getSwitches().values()){ |
| 583 | Collection<Short> enabledPorts = sw.getEnabledPortNumbers(); |
| 584 | Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId()); |
| 585 | |
Jonathan Hart | 8ec133c | 2013-06-26 15:25:18 +1200 | [diff] [blame] | 586 | if (linkPorts == null){ |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 587 | //I think this means the switch doesn't have any links. |
| 588 | //continue; |
| 589 | linkPorts = new HashSet<Short>(); |
Jonathan Hart | 8ec133c | 2013-06-26 15:25:18 +1200 | [diff] [blame] | 590 | } |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 591 | |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 592 | |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 593 | OFPacketOut po = new OFPacketOut(); |
| 594 | po.setInPort(OFPort.OFPP_NONE) |
| 595 | .setBufferId(-1) |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 596 | .setPacketData(arpRequest); |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 597 | |
| 598 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 599 | |
| 600 | for (short portNum : enabledPorts){ |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 601 | if (linkPorts.contains(portNum) || |
| 602 | (sw.getId() == inSwitch && portNum == inPort)){ |
| 603 | //If this port isn't an edge port or is the ingress port |
| 604 | //for the ARP, don't broadcast out it |
| 605 | continue; |
| 606 | } |
| 607 | |
| 608 | actions.add(new OFActionOutput(portNum)); |
| 609 | } |
| 610 | |
| 611 | po.setActions(actions); |
| 612 | short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH); |
| 613 | po.setActionsLength(actionsLength); |
| 614 | po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 615 | + arpRequest.length); |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 616 | |
| 617 | List<OFMessage> msgList = new ArrayList<OFMessage>(); |
| 618 | msgList.add(po); |
| 619 | |
| 620 | try { |
| 621 | sw.write(msgList, null); |
| 622 | sw.flush(); |
| 623 | } catch (IOException e) { |
| 624 | log.error("Failure writing packet out to switch", e); |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | |
Jonathan Hart | f9bd98d | 2013-12-13 11:40:55 -0800 | [diff] [blame] | 629 | private void broadcastArpRequestOutMyEdge(byte[] arpRequest, |
| 630 | long inSwitch, short inPort) { |
Jonathan Hart | ba9ced9 | 2013-11-24 16:52:13 -0800 | [diff] [blame] | 631 | List<SwitchPort> switchPorts = new ArrayList<SwitchPort>(); |
| 632 | |
Jonathan Hart | 9a21e0d | 2013-11-14 15:09:09 -0800 | [diff] [blame] | 633 | for (IOFSwitch sw : floodlightProvider.getSwitches().values()) { |
| 634 | |
| 635 | OFPacketOut po = new OFPacketOut(); |
| 636 | po.setInPort(OFPort.OFPP_NONE) |
| 637 | .setBufferId(-1) |
| 638 | .setPacketData(arpRequest); |
| 639 | |
| 640 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 641 | |
| 642 | Iterable<IPortObject> ports |
| 643 | = topoSwitchService.getPortsOnSwitch(sw.getStringId()); |
| 644 | if (ports == null) { |
| 645 | continue; |
| 646 | } |
| 647 | |
| 648 | for (IPortObject portObject : ports) { |
| 649 | if (!portObject.getLinkedPorts().iterator().hasNext()) { |
Jonathan Hart | f9bd98d | 2013-12-13 11:40:55 -0800 | [diff] [blame] | 650 | short portNumber = portObject.getNumber(); |
| 651 | |
| 652 | if (sw.getId() == inSwitch && portNumber == inPort) { |
| 653 | // This is the port that the ARP message came in, |
| 654 | // so don't broadcast out this port |
| 655 | continue; |
| 656 | } |
| 657 | |
Jonathan Hart | ba9ced9 | 2013-11-24 16:52:13 -0800 | [diff] [blame] | 658 | switchPorts.add(new SwitchPort(new Dpid(sw.getId()), |
Jonathan Hart | f9bd98d | 2013-12-13 11:40:55 -0800 | [diff] [blame] | 659 | new Port(portNumber))); |
| 660 | actions.add(new OFActionOutput(portNumber)); |
Jonathan Hart | 9a21e0d | 2013-11-14 15:09:09 -0800 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | |
| 664 | po.setActions(actions); |
| 665 | short actionsLength = (short) |
| 666 | (actions.size() * OFActionOutput.MINIMUM_LENGTH); |
| 667 | po.setActionsLength(actionsLength); |
| 668 | po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength |
| 669 | + arpRequest.length); |
| 670 | |
| 671 | try { |
| 672 | sw.write(po, null); |
| 673 | sw.flush(); |
| 674 | } catch (IOException e) { |
| 675 | log.error("Failure writing packet out to switch", e); |
| 676 | } |
| 677 | } |
Jonathan Hart | ba9ced9 | 2013-11-24 16:52:13 -0800 | [diff] [blame] | 678 | |
pingping-lin | 017a892 | 2013-12-11 11:15:33 +0800 | [diff] [blame] | 679 | log.debug("Broadcast ARP request to: {}", switchPorts); |
Jonathan Hart | 9a21e0d | 2013-11-14 15:09:09 -0800 | [diff] [blame] | 680 | } |
| 681 | |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 682 | private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 683 | if (log.isTraceEnabled()) { |
| 684 | log.trace("Sending ARP request out {}/{}", |
| 685 | HexString.toHexString(dpid), port); |
| 686 | } |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 687 | |
| 688 | OFPacketOut po = new OFPacketOut(); |
| 689 | po.setInPort(OFPort.OFPP_NONE) |
| 690 | .setBufferId(-1) |
| 691 | .setPacketData(arpRequest); |
| 692 | |
| 693 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 694 | actions.add(new OFActionOutput(port)); |
| 695 | po.setActions(actions); |
| 696 | short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH); |
| 697 | po.setActionsLength(actionsLength); |
| 698 | po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength |
| 699 | + arpRequest.length); |
| 700 | |
| 701 | IOFSwitch sw = floodlightProvider.getSwitches().get(dpid); |
| 702 | |
| 703 | if (sw == null) { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 704 | log.warn("Switch not found when sending ARP request"); |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 705 | return; |
| 706 | } |
| 707 | |
| 708 | try { |
| 709 | sw.write(po, null); |
| 710 | sw.flush(); |
| 711 | } catch (IOException e) { |
| 712 | log.error("Failure writing packet out to switch", e); |
| 713 | } |
| 714 | } |
| 715 | |
Jonathan Hart | da4d0e1 | 2013-09-30 21:00:20 +1300 | [diff] [blame] | 716 | private void sendArpReply(ARP arpRequest, long dpid, short port, MACAddress targetMac) { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 717 | if (log.isTraceEnabled()) { |
| 718 | log.trace("Sending reply {} => {} to {}", new Object[] { |
| 719 | inetAddressToString(arpRequest.getTargetProtocolAddress()), |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 720 | targetMac, |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 721 | inetAddressToString(arpRequest.getSenderProtocolAddress())}); |
| 722 | } |
Jonathan Hart | 1633a40 | 2013-08-24 11:38:56 +1200 | [diff] [blame] | 723 | |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 724 | ARP arpReply = new ARP(); |
| 725 | arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET) |
| 726 | .setProtocolType(ARP.PROTO_TYPE_IP) |
| 727 | .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH) |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 728 | .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH) |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 729 | .setOpCode(ARP.OP_REPLY) |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 730 | .setSenderHardwareAddress(targetMac.toBytes()) |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 731 | .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress()) |
| 732 | .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress()) |
| 733 | .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress()); |
| 734 | |
Jonathan Hart | 1cf9de0 | 2013-10-21 17:42:29 -0700 | [diff] [blame] | 735 | |
| 736 | |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 737 | Ethernet eth = new Ethernet(); |
| 738 | eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress()) |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 739 | .setSourceMACAddress(targetMac.toBytes()) |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 740 | .setEtherType(Ethernet.TYPE_ARP) |
| 741 | .setPayload(arpReply); |
| 742 | |
Jonathan Hart | 1cf9de0 | 2013-10-21 17:42:29 -0700 | [diff] [blame] | 743 | if (vlan != NO_VLAN) { |
| 744 | eth.setVlanID(vlan) |
| 745 | .setPriorityCode((byte)0); |
| 746 | } |
| 747 | |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 748 | List<OFAction> actions = new ArrayList<OFAction>(); |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 749 | actions.add(new OFActionOutput(port)); |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 750 | |
| 751 | OFPacketOut po = new OFPacketOut(); |
| 752 | po.setInPort(OFPort.OFPP_NONE) |
| 753 | .setBufferId(-1) |
Jonathan Hart | 8ec133c | 2013-06-26 15:25:18 +1200 | [diff] [blame] | 754 | .setPacketData(eth.serialize()) |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 755 | .setActions(actions) |
| 756 | .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH) |
| 757 | .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH |
| 758 | + po.getPacketData().length); |
| 759 | |
| 760 | List<OFMessage> msgList = new ArrayList<OFMessage>(); |
| 761 | msgList.add(po); |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 762 | |
| 763 | IOFSwitch sw = floodlightProvider.getSwitches().get(dpid); |
| 764 | |
| 765 | if (sw == null) { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 766 | log.warn("Switch {} not found when sending ARP reply", |
Jonathan Hart | 1633a40 | 2013-08-24 11:38:56 +1200 | [diff] [blame] | 767 | HexString.toHexString(dpid)); |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 768 | return; |
| 769 | } |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 770 | |
| 771 | try { |
| 772 | sw.write(msgList, null); |
| 773 | sw.flush(); |
| 774 | } catch (IOException e) { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 775 | log.error("Failure writing packet out to switch", e); |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 776 | } |
| 777 | } |
Jonathan Hart | da4d0e1 | 2013-09-30 21:00:20 +1300 | [diff] [blame] | 778 | |
| 779 | private String inetAddressToString(byte[] bytes) { |
| 780 | try { |
| 781 | return InetAddress.getByAddress(bytes).getHostAddress(); |
| 782 | } catch (UnknownHostException e) { |
| 783 | log.debug("Invalid IP address", e); |
| 784 | return ""; |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | /* |
| 789 | * IProxyArpService methods |
| 790 | */ |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 791 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 792 | @Override |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 793 | public MACAddress getMacAddress(InetAddress ipAddress) { |
| 794 | return arpCache.lookup(ipAddress); |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 795 | } |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 796 | |
| 797 | @Override |
| 798 | public void sendArpRequest(InetAddress ipAddress, IArpRequester requester, |
| 799 | boolean retry) { |
Jonathan Hart | df6ec33 | 2013-08-04 01:37:14 +1200 | [diff] [blame] | 800 | arpRequests.put(ipAddress, new ArpRequest(requester, retry)); |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 801 | |
Jonathan Hart | 6e61821 | 2013-08-21 22:28:43 +1200 | [diff] [blame] | 802 | //Sanity check to make sure we don't send a request for our own address |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 803 | if (!configService.isInterfaceAddress(ipAddress)) { |
Jonathan Hart | 6e61821 | 2013-08-21 22:28:43 +1200 | [diff] [blame] | 804 | sendArpRequestForAddress(ipAddress); |
| 805 | } |
| 806 | } |
Jonathan Hart | 5afde49 | 2013-10-01 12:30:53 +1300 | [diff] [blame] | 807 | |
| 808 | @Override |
| 809 | public List<String> getMappings() { |
| 810 | return arpCache.getMappings(); |
| 811 | } |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 812 | |
| 813 | /* |
| 814 | * IArpEventHandler methods |
| 815 | */ |
| 816 | |
| 817 | @Override |
Jonathan Hart | d300325 | 2013-11-15 09:44:46 -0800 | [diff] [blame] | 818 | public void arpRequestNotification(ArpMessage arpMessage) { |
pingping-lin | 017a892 | 2013-12-11 11:15:33 +0800 | [diff] [blame] | 819 | log.debug("Received ARP notification from other instances"); |
pingping-lin | b8757bf | 2013-12-13 01:48:58 +0800 | [diff] [blame] | 820 | |
Jonathan Hart | d300325 | 2013-11-15 09:44:46 -0800 | [diff] [blame] | 821 | switch (arpMessage.getType()){ |
| 822 | case REQUEST: |
pingping-lin | b8757bf | 2013-12-13 01:48:58 +0800 | [diff] [blame] | 823 | if(arpMessage.getOutSwitch() == -1 || arpMessage.getOutPort() == -1){ |
Jonathan Hart | f9bd98d | 2013-12-13 11:40:55 -0800 | [diff] [blame] | 824 | broadcastArpRequestOutMyEdge(arpMessage.getPacket(), |
| 825 | arpMessage.getInSwitch(), arpMessage.getInPort()); |
pingping-lin | b8757bf | 2013-12-13 01:48:58 +0800 | [diff] [blame] | 826 | }else{ |
| 827 | sendArpRequestOutPort(arpMessage.getPacket(),arpMessage.getOutSwitch(),arpMessage.getOutPort()); |
| 828 | log.debug("OutSwitch in ARP request message is: {}; OutPort in ARP request message is: {}",arpMessage.getOutSwitch(),arpMessage.getOutPort()); |
| 829 | } |
Jonathan Hart | d300325 | 2013-11-15 09:44:46 -0800 | [diff] [blame] | 830 | break; |
| 831 | case REPLY: |
Jonathan Hart | ba9ced9 | 2013-11-24 16:52:13 -0800 | [diff] [blame] | 832 | log.debug("Received ARP reply notification for {}", |
| 833 | arpMessage.getAddress()); |
pingping-lin | 017a892 | 2013-12-11 11:15:33 +0800 | [diff] [blame] | 834 | sendArpReplyToWaitingRequesters(arpMessage.getAddress(),arpMessage.getMAC()); |
Jonathan Hart | d300325 | 2013-11-15 09:44:46 -0800 | [diff] [blame] | 835 | break; |
| 836 | } |
| 837 | } |
| 838 | |
pingping-lin | 017a892 | 2013-12-11 11:15:33 +0800 | [diff] [blame] | 839 | private void sendArpReplyToWaitingRequesters(InetAddress address, MACAddress mac) { |
Jonathan Hart | d300325 | 2013-11-15 09:44:46 -0800 | [diff] [blame] | 840 | log.debug("Sending ARP reply for {} to requesters", |
| 841 | address.getHostAddress()); |
| 842 | |
| 843 | //See if anyone's waiting for this ARP reply |
| 844 | Set<ArpRequest> requests = arpRequests.get(address); |
| 845 | |
| 846 | //Synchronize on the Multimap while using an iterator for one of the sets |
| 847 | List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size()); |
| 848 | synchronized (arpRequests) { |
| 849 | Iterator<ArpRequest> it = requests.iterator(); |
| 850 | while (it.hasNext()) { |
| 851 | ArpRequest request = it.next(); |
| 852 | it.remove(); |
| 853 | requestsToSend.add(request); |
| 854 | } |
| 855 | } |
| 856 | |
pingping-lin | 017a892 | 2013-12-11 11:15:33 +0800 | [diff] [blame] | 857 | /*IDeviceObject deviceObject = deviceStorage.getDeviceByIP( |
Jonathan Hart | d300325 | 2013-11-15 09:44:46 -0800 | [diff] [blame] | 858 | InetAddresses.coerceToInteger(address)); |
| 859 | |
| 860 | MACAddress mac = MACAddress.valueOf(deviceObject.getMACAddress()); |
| 861 | |
| 862 | log.debug("Found {} at {} in network map", |
pingping-lin | 017a892 | 2013-12-11 11:15:33 +0800 | [diff] [blame] | 863 | address.getHostAddress(), mac);*/ |
Jonathan Hart | d300325 | 2013-11-15 09:44:46 -0800 | [diff] [blame] | 864 | |
| 865 | //Don't hold an ARP lock while dispatching requests |
| 866 | for (ArpRequest request : requestsToSend) { |
| 867 | request.dispatchReply(address, mac); |
| 868 | } |
Jonathan Hart | 18ad55c | 2013-11-11 22:49:55 -0800 | [diff] [blame] | 869 | } |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 870 | } |