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