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 | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 8 | import java.util.HashSet; |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 9 | import java.util.Iterator; |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 10 | import java.util.List; |
| 11 | import java.util.Map; |
| 12 | import java.util.Set; |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 13 | import java.util.Timer; |
| 14 | import java.util.TimerTask; |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 15 | |
| 16 | import net.floodlightcontroller.core.FloodlightContext; |
| 17 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 18 | import net.floodlightcontroller.core.IOFMessageListener; |
| 19 | import net.floodlightcontroller.core.IOFSwitch; |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 20 | import net.floodlightcontroller.devicemanager.IDeviceService; |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 21 | import net.floodlightcontroller.packet.ARP; |
| 22 | import net.floodlightcontroller.packet.Ethernet; |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 23 | import net.floodlightcontroller.packet.IPv4; |
Jonathan Hart | 5afde49 | 2013-10-01 12:30:53 +1300 | [diff] [blame] | 24 | import net.floodlightcontroller.restserver.IRestApiService; |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 25 | import net.floodlightcontroller.topology.ITopologyService; |
Jonathan Hart | 8ec133c | 2013-06-26 15:25:18 +1200 | [diff] [blame] | 26 | import net.floodlightcontroller.util.MACAddress; |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 27 | import net.onrc.onos.ofcontroller.bgproute.Interface; |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 28 | import net.onrc.onos.ofcontroller.core.IDeviceStorage; |
| 29 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject; |
Jonathan Hart | ebba1e1 | 2013-10-29 11:37:02 -0700 | [diff] [blame] | 30 | import net.onrc.onos.ofcontroller.core.config.IConfigInfoService; |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 31 | import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl; |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 32 | |
| 33 | import org.openflow.protocol.OFMessage; |
| 34 | import org.openflow.protocol.OFPacketIn; |
| 35 | import org.openflow.protocol.OFPacketOut; |
| 36 | import org.openflow.protocol.OFPort; |
| 37 | import org.openflow.protocol.OFType; |
| 38 | import org.openflow.protocol.action.OFAction; |
| 39 | import org.openflow.protocol.action.OFActionOutput; |
Jonathan Hart | 8ec133c | 2013-06-26 15:25:18 +1200 | [diff] [blame] | 40 | import org.openflow.util.HexString; |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 41 | import org.slf4j.Logger; |
| 42 | import org.slf4j.LoggerFactory; |
| 43 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 44 | import com.google.common.collect.HashMultimap; |
| 45 | import com.google.common.collect.Multimaps; |
| 46 | import com.google.common.collect.SetMultimap; |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 47 | import com.google.common.net.InetAddresses; |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 48 | |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 49 | public class ProxyArpManager implements IProxyArpService, IOFMessageListener { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 50 | private final static Logger log = LoggerFactory.getLogger(ProxyArpManager.class); |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 51 | |
Jonathan Hart | df6ec33 | 2013-08-04 01:37:14 +1200 | [diff] [blame] | 52 | private final long ARP_TIMER_PERIOD = 60000; //ms (== 1 min) |
Jonathan Hart | da4d0e1 | 2013-09-30 21:00:20 +1300 | [diff] [blame] | 53 | |
| 54 | private static final int ARP_REQUEST_TIMEOUT = 2000; //ms |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 55 | |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 56 | private IFloodlightProviderService floodlightProvider; |
| 57 | private ITopologyService topology; |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 58 | private IDeviceService deviceService; |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 59 | private IConfigInfoService configService; |
| 60 | private IRestApiService restApi; |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 61 | |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 62 | private IDeviceStorage deviceStorage; |
| 63 | |
Jonathan Hart | 1cf9de0 | 2013-10-21 17:42:29 -0700 | [diff] [blame] | 64 | private short vlan; |
| 65 | private static final short NO_VLAN = 0; |
| 66 | |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 67 | private ArpCache arpCache; |
Jonathan Hart | df6ec33 | 2013-08-04 01:37:14 +1200 | [diff] [blame] | 68 | |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 69 | private SetMultimap<InetAddress, ArpRequest> arpRequests; |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 70 | |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 71 | private static class ArpRequest { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 72 | private final IArpRequester requester; |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 73 | private final boolean retry; |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 74 | private long requestTime; |
| 75 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 76 | public ArpRequest(IArpRequester requester, boolean retry){ |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 77 | this.requester = requester; |
| 78 | this.retry = retry; |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 79 | this.requestTime = System.currentTimeMillis(); |
| 80 | } |
| 81 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 82 | public ArpRequest(ArpRequest old) { |
| 83 | this.requester = old.requester; |
| 84 | this.retry = old.retry; |
| 85 | this.requestTime = System.currentTimeMillis(); |
| 86 | } |
| 87 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 88 | public boolean isExpired() { |
Jonathan Hart | da4d0e1 | 2013-09-30 21:00:20 +1300 | [diff] [blame] | 89 | return (System.currentTimeMillis() - requestTime) > ARP_REQUEST_TIMEOUT; |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 90 | } |
| 91 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 92 | public boolean shouldRetry() { |
| 93 | return retry; |
| 94 | } |
| 95 | |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 96 | public void dispatchReply(InetAddress ipAddress, MACAddress replyMacAddress) { |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 97 | requester.arpResponse(ipAddress, replyMacAddress); |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Jonathan Hart | da4d0e1 | 2013-09-30 21:00:20 +1300 | [diff] [blame] | 101 | private class HostArpRequester implements IArpRequester { |
| 102 | private final ARP arpRequest; |
| 103 | private final long dpid; |
| 104 | private final short port; |
| 105 | |
| 106 | public HostArpRequester(ARP arpRequest, long dpid, short port) { |
| 107 | this.arpRequest = arpRequest; |
| 108 | this.dpid = dpid; |
| 109 | this.port = port; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public void arpResponse(InetAddress ipAddress, MACAddress macAddress) { |
| 114 | ProxyArpManager.this.sendArpReply(arpRequest, dpid, port, macAddress); |
| 115 | } |
| 116 | } |
| 117 | |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 118 | /* |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 119 | public ProxyArpManager(IFloodlightProviderService floodlightProvider, |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 120 | ITopologyService topology, IConfigInfoService configService, |
Jonathan Hart | 5afde49 | 2013-10-01 12:30:53 +1300 | [diff] [blame] | 121 | IRestApiService restApi){ |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 122 | |
| 123 | } |
| 124 | */ |
| 125 | |
| 126 | public void init(IFloodlightProviderService floodlightProvider, |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 127 | ITopologyService topology, IDeviceService deviceService, |
| 128 | IConfigInfoService config, IRestApiService restApi){ |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 129 | this.floodlightProvider = floodlightProvider; |
| 130 | this.topology = topology; |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 131 | this.deviceService = deviceService; |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 132 | this.configService = config; |
Jonathan Hart | 5afde49 | 2013-10-01 12:30:53 +1300 | [diff] [blame] | 133 | this.restApi = restApi; |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 134 | |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 135 | arpCache = new ArpCache(); |
Jonathan Hart | df6ec33 | 2013-08-04 01:37:14 +1200 | [diff] [blame] | 136 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 137 | arpRequests = Multimaps.synchronizedSetMultimap( |
| 138 | HashMultimap.<InetAddress, ArpRequest>create()); |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 139 | } |
| 140 | |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 141 | public void startUp() { |
| 142 | this.vlan = configService.getVlan(); |
Jonathan Hart | 1cf9de0 | 2013-10-21 17:42:29 -0700 | [diff] [blame] | 143 | log.info("vlan set to {}", this.vlan); |
| 144 | |
Jonathan Hart | 5afde49 | 2013-10-01 12:30:53 +1300 | [diff] [blame] | 145 | restApi.addRestletRoutable(new ArpWebRoutable()); |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 146 | floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this); |
Jonathan Hart | 5afde49 | 2013-10-01 12:30:53 +1300 | [diff] [blame] | 147 | |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 148 | deviceStorage = new DeviceStorageImpl(); |
| 149 | deviceStorage.init(""); |
| 150 | |
Jonathan Hart | 4aa2b4e | 2013-09-24 14:50:23 +1200 | [diff] [blame] | 151 | Timer arpTimer = new Timer("arp-processing"); |
Jonathan Hart | df6ec33 | 2013-08-04 01:37:14 +1200 | [diff] [blame] | 152 | arpTimer.scheduleAtFixedRate(new TimerTask() { |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 153 | @Override |
| 154 | public void run() { |
Jonathan Hart | df6ec33 | 2013-08-04 01:37:14 +1200 | [diff] [blame] | 155 | doPeriodicArpProcessing(); |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 156 | } |
Jonathan Hart | df6ec33 | 2013-08-04 01:37:14 +1200 | [diff] [blame] | 157 | }, 0, ARP_TIMER_PERIOD); |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 158 | } |
| 159 | |
Jonathan Hart | df6ec33 | 2013-08-04 01:37:14 +1200 | [diff] [blame] | 160 | /* |
| 161 | * Function that runs periodically to manage the asynchronous request mechanism. |
| 162 | * It basically cleans up old ARP requests if we don't get a response for them. |
| 163 | * The caller can designate that a request should be retried indefinitely, and |
| 164 | * this task will handle that as well. |
| 165 | */ |
| 166 | private void doPeriodicArpProcessing() { |
| 167 | SetMultimap<InetAddress, ArpRequest> retryList |
| 168 | = HashMultimap.<InetAddress, ArpRequest>create(); |
| 169 | |
| 170 | //Have to synchronize externally on the Multimap while using an iterator, |
| 171 | //even though it's a synchronizedMultimap |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 172 | synchronized (arpRequests) { |
Jonathan Hart | df6ec33 | 2013-08-04 01:37:14 +1200 | [diff] [blame] | 173 | log.debug("Current have {} outstanding requests", |
| 174 | arpRequests.size()); |
| 175 | |
| 176 | Iterator<Map.Entry<InetAddress, ArpRequest>> it |
| 177 | = arpRequests.entries().iterator(); |
| 178 | |
| 179 | while (it.hasNext()) { |
| 180 | Map.Entry<InetAddress, ArpRequest> entry |
| 181 | = it.next(); |
| 182 | ArpRequest request = entry.getValue(); |
| 183 | if (request.isExpired()) { |
| 184 | log.debug("Cleaning expired ARP request for {}", |
| 185 | entry.getKey().getHostAddress()); |
| 186 | |
| 187 | it.remove(); |
| 188 | |
| 189 | if (request.shouldRetry()) { |
| 190 | retryList.put(entry.getKey(), request); |
| 191 | } |
| 192 | } |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 193 | } |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 194 | } |
Jonathan Hart | df6ec33 | 2013-08-04 01:37:14 +1200 | [diff] [blame] | 195 | |
| 196 | for (Map.Entry<InetAddress, Collection<ArpRequest>> entry |
| 197 | : retryList.asMap().entrySet()) { |
| 198 | |
| 199 | InetAddress address = entry.getKey(); |
| 200 | |
| 201 | log.debug("Resending ARP request for {}", address.getHostAddress()); |
| 202 | |
| 203 | sendArpRequestForAddress(address); |
| 204 | |
| 205 | for (ArpRequest request : entry.getValue()) { |
| 206 | arpRequests.put(address, new ArpRequest(request)); |
| 207 | } |
| 208 | } |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 209 | } |
| 210 | |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 211 | @Override |
| 212 | public String getName() { |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 213 | return "proxyarpmanager"; |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public boolean isCallbackOrderingPrereq(OFType type, String name) { |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 218 | if (type == OFType.PACKET_IN) { |
| 219 | return "devicemanager".equals(name); |
| 220 | } |
| 221 | else { |
| 222 | return false; |
| 223 | } |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public boolean isCallbackOrderingPostreq(OFType type, String name) { |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 228 | return false; |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public Command receive( |
| 233 | IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { |
| 234 | |
| 235 | if (msg.getType() != OFType.PACKET_IN){ |
| 236 | return Command.CONTINUE; |
| 237 | } |
| 238 | |
| 239 | OFPacketIn pi = (OFPacketIn) msg; |
| 240 | |
| 241 | Ethernet eth = IFloodlightProviderService.bcStore.get(cntx, |
| 242 | IFloodlightProviderService.CONTEXT_PI_PAYLOAD); |
| 243 | |
| 244 | if (eth.getEtherType() == Ethernet.TYPE_ARP){ |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 245 | ARP arp = (ARP) eth.getPayload(); |
| 246 | |
| 247 | if (arp.getOpCode() == ARP.OP_REQUEST) { |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 248 | //TODO check what the DeviceManager does about propagating |
| 249 | //or swallowing ARPs. We want to go after DeviceManager in the |
| 250 | //chain but we really need it to CONTINUE ARP packets so we can |
| 251 | //get them. |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 252 | handleArpRequest(sw, pi, arp); |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 253 | } |
| 254 | else if (arp.getOpCode() == ARP.OP_REPLY) { |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 255 | //handleArpReply(sw, pi, arp); |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 259 | //TODO should we propagate ARP or swallow it? |
| 260 | //Always propagate for now so DeviceManager can learn the host location |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 261 | return Command.CONTINUE; |
| 262 | } |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 263 | |
Jonathan Hart | 1912afc | 2013-10-11 12:02:44 +1300 | [diff] [blame] | 264 | private void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp) { |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 265 | if (log.isTraceEnabled()) { |
| 266 | log.trace("ARP request received for {}", |
| 267 | inetAddressToString(arp.getTargetProtocolAddress())); |
| 268 | } |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 269 | |
| 270 | InetAddress target; |
| 271 | try { |
| 272 | target = InetAddress.getByAddress(arp.getTargetProtocolAddress()); |
| 273 | } catch (UnknownHostException e) { |
| 274 | log.debug("Invalid address in ARP request", e); |
| 275 | return; |
| 276 | } |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 277 | |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 278 | if (configService.fromExternalNetwork(sw.getId(), pi.getInPort())) { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 279 | //If the request came from outside our network, we only care if |
| 280 | //it was a request for one of our interfaces. |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 281 | if (configService.isInterfaceAddress(target)) { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 282 | log.trace("ARP request for our interface. Sending reply {} => {}", |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 283 | target.getHostAddress(), configService.getRouterMacAddress()); |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 284 | |
| 285 | sendArpReply(arp, sw.getId(), pi.getInPort(), |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 286 | configService.getRouterMacAddress()); |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 287 | } |
Jonathan Hart | 6e61821 | 2013-08-21 22:28:43 +1200 | [diff] [blame] | 288 | |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 289 | return; |
| 290 | } |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 291 | |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 292 | //MACAddress macAddress = arpCache.lookup(target); |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 293 | |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 294 | //IDevice dstDevice = deviceService.fcStore.get(cntx, IDeviceService.CONTEXT_DST_DEVICE); |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 295 | //Iterator<? extends IDevice> it = deviceService.queryDevices( |
| 296 | //null, null, InetAddresses.coerceToInteger(target), null, null); |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 297 | |
Jonathan Hart | d6ed62b | 2013-11-01 13:18:25 -0700 | [diff] [blame] | 298 | //IDevice targetDevice = null; |
| 299 | //if (it.hasNext()) { |
| 300 | //targetDevice = it.next(); |
| 301 | //} |
| 302 | IDeviceObject targetDevice = |
| 303 | deviceStorage.getDeviceByIP(InetAddresses.coerceToInteger(target)); |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 304 | |
| 305 | if (targetDevice != null) { |
| 306 | //We have the device in our database, so send a reply |
| 307 | MACAddress macAddress = MACAddress.valueOf(targetDevice.getMACAddress()); |
| 308 | |
| 309 | if (log.isTraceEnabled()) { |
| 310 | log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] { |
| 311 | inetAddressToString(arp.getTargetProtocolAddress()), |
| 312 | macAddress.toString(), |
| 313 | HexString.toHexString(sw.getId()), pi.getInPort()}); |
| 314 | } |
| 315 | |
| 316 | sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress); |
| 317 | } |
| 318 | |
| 319 | /*if (macAddress == null){ |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 320 | //MAC address is not in our ARP cache. |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 321 | |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 322 | //Record where the request came from so we know where to send the reply |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 323 | //arpRequests.put(target, new ArpRequest( |
| 324 | //new HostArpRequester(arp, sw.getId(), pi.getInPort()), false)); |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 325 | |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 326 | //Flood the request out edge ports |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 327 | //sendArpRequestToSwitches(target, pi.getPacketData(), sw.getId(), pi.getInPort()); |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 328 | } |
| 329 | else { |
| 330 | //We know the address, so send a reply |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 331 | if (log.isTraceEnabled()) { |
| 332 | log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] { |
| 333 | inetAddressToString(arp.getTargetProtocolAddress()), |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 334 | macAddress.toString(), |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 335 | HexString.toHexString(sw.getId()), pi.getInPort()}); |
| 336 | } |
Jonathan Hart | 1633a40 | 2013-08-24 11:38:56 +1200 | [diff] [blame] | 337 | |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 338 | sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress); |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 339 | }*/ |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 340 | } |
| 341 | |
Jonathan Hart | 1912afc | 2013-10-11 12:02:44 +1300 | [diff] [blame] | 342 | private void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){ |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 343 | if (log.isTraceEnabled()) { |
| 344 | log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] { |
| 345 | inetAddressToString(arp.getSenderProtocolAddress()), |
| 346 | HexString.toHexString(arp.getSenderHardwareAddress()), |
| 347 | HexString.toHexString(sw.getId()), pi.getInPort()}); |
| 348 | } |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 349 | |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 350 | InetAddress senderIpAddress; |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 351 | try { |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 352 | senderIpAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress()); |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 353 | } catch (UnknownHostException e) { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 354 | log.debug("Invalid address in ARP reply", e); |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 355 | return; |
| 356 | } |
| 357 | |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 358 | MACAddress senderMacAddress = MACAddress.valueOf(arp.getSenderHardwareAddress()); |
| 359 | |
| 360 | arpCache.update(senderIpAddress, senderMacAddress); |
| 361 | |
| 362 | //See if anyone's waiting for this ARP reply |
| 363 | Set<ArpRequest> requests = arpRequests.get(senderIpAddress); |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 364 | |
| 365 | //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] | 366 | List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size()); |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 367 | synchronized (arpRequests) { |
| 368 | Iterator<ArpRequest> it = requests.iterator(); |
| 369 | while (it.hasNext()) { |
| 370 | ArpRequest request = it.next(); |
| 371 | it.remove(); |
Jonathan Hart | e751e1c | 2013-08-23 00:48:47 +1200 | [diff] [blame] | 372 | requestsToSend.add(request); |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 373 | } |
| 374 | } |
Jonathan Hart | e751e1c | 2013-08-23 00:48:47 +1200 | [diff] [blame] | 375 | |
| 376 | //Don't hold an ARP lock while dispatching requests |
| 377 | for (ArpRequest request : requestsToSend) { |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 378 | request.dispatchReply(senderIpAddress, senderMacAddress); |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 382 | private void sendArpRequestForAddress(InetAddress ipAddress) { |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 383 | //TODO what should the sender IP address and MAC address be if no |
| 384 | //IP addresses are configured? Will there ever be a need to send |
| 385 | //ARP requests from the controller in that case? |
| 386 | //All-zero MAC address doesn't seem to work - hosts don't respond to it |
| 387 | |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 388 | byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0}; |
| 389 | byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 390 | byte[] genericNonZeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01}; |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 391 | byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff, |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 392 | (byte)0xff, (byte)0xff, (byte)0xff}; |
| 393 | |
| 394 | ARP arpRequest = new ARP(); |
| 395 | |
| 396 | arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET) |
| 397 | .setProtocolType(ARP.PROTO_TYPE_IP) |
| 398 | .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH) |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 399 | .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH) |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 400 | .setOpCode(ARP.OP_REQUEST) |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 401 | .setTargetHardwareAddress(zeroMac) |
| 402 | .setTargetProtocolAddress(ipAddress.getAddress()); |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 403 | |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 404 | MACAddress routerMacAddress = configService.getRouterMacAddress(); |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 405 | //TODO hack for now as it's unclear what the MAC address should be |
| 406 | byte[] senderMacAddress = genericNonZeroMac; |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 407 | if (routerMacAddress != null) { |
| 408 | senderMacAddress = routerMacAddress.toBytes(); |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 409 | } |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 410 | arpRequest.setSenderHardwareAddress(senderMacAddress); |
| 411 | |
| 412 | byte[] senderIPAddress = zeroIpv4; |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 413 | Interface intf = configService.getOutgoingInterface(ipAddress); |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 414 | if (intf != null) { |
| 415 | senderIPAddress = intf.getIpAddress().getAddress(); |
| 416 | } |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 417 | |
| 418 | arpRequest.setSenderProtocolAddress(senderIPAddress); |
| 419 | |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 420 | Ethernet eth = new Ethernet(); |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 421 | eth.setSourceMACAddress(senderMacAddress) |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 422 | .setDestinationMACAddress(broadcastMac) |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 423 | .setEtherType(Ethernet.TYPE_ARP) |
| 424 | .setPayload(arpRequest); |
| 425 | |
Jonathan Hart | 1cf9de0 | 2013-10-21 17:42:29 -0700 | [diff] [blame] | 426 | if (vlan != NO_VLAN) { |
| 427 | eth.setVlanID(vlan) |
| 428 | .setPriorityCode((byte)0); |
| 429 | } |
| 430 | |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 431 | sendArpRequestToSwitches(ipAddress, eth.serialize()); |
| 432 | } |
| 433 | |
| 434 | private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 435 | sendArpRequestToSwitches(dstAddress, arpRequest, |
| 436 | 0, OFPort.OFPP_NONE.getValue()); |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 437 | } |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 438 | |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 439 | private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest, |
| 440 | long inSwitch, short inPort) { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 441 | |
Jonathan Hart | a888764 | 2013-10-28 13:46:54 -0700 | [diff] [blame] | 442 | if (configService.hasLayer3Configuration()) { |
| 443 | Interface intf = configService.getOutgoingInterface(dstAddress); |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 444 | if (intf != null) { |
| 445 | sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort()); |
| 446 | } |
| 447 | else { |
| 448 | //TODO here it should be broadcast out all non-interface edge ports. |
| 449 | //I think we can assume that if it's not a request for an external |
| 450 | //network, it's an ARP for a host in our own network. So we want to |
| 451 | //send it out all edge ports that don't have an interface configured |
| 452 | //to ensure it reaches all hosts in our network. |
| 453 | log.debug("No interface found to send ARP request for {}", |
| 454 | dstAddress.getHostAddress()); |
| 455 | } |
| 456 | } |
| 457 | else { |
| 458 | broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort); |
| 459 | } |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 460 | } |
| 461 | |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 462 | private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) { |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 463 | for (IOFSwitch sw : floodlightProvider.getSwitches().values()){ |
| 464 | Collection<Short> enabledPorts = sw.getEnabledPortNumbers(); |
| 465 | Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId()); |
| 466 | |
Jonathan Hart | 8ec133c | 2013-06-26 15:25:18 +1200 | [diff] [blame] | 467 | if (linkPorts == null){ |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 468 | //I think this means the switch doesn't have any links. |
| 469 | //continue; |
| 470 | linkPorts = new HashSet<Short>(); |
Jonathan Hart | 8ec133c | 2013-06-26 15:25:18 +1200 | [diff] [blame] | 471 | } |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 472 | |
Jonathan Hart | a18e479 | 2013-10-31 10:10:54 -0700 | [diff] [blame] | 473 | |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 474 | OFPacketOut po = new OFPacketOut(); |
| 475 | po.setInPort(OFPort.OFPP_NONE) |
| 476 | .setBufferId(-1) |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 477 | .setPacketData(arpRequest); |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 478 | |
| 479 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 480 | |
| 481 | for (short portNum : enabledPorts){ |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 482 | if (linkPorts.contains(portNum) || |
| 483 | (sw.getId() == inSwitch && portNum == inPort)){ |
| 484 | //If this port isn't an edge port or is the ingress port |
| 485 | //for the ARP, don't broadcast out it |
| 486 | continue; |
| 487 | } |
| 488 | |
| 489 | actions.add(new OFActionOutput(portNum)); |
| 490 | } |
| 491 | |
| 492 | po.setActions(actions); |
| 493 | short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH); |
| 494 | po.setActionsLength(actionsLength); |
| 495 | po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 496 | + arpRequest.length); |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 497 | |
| 498 | List<OFMessage> msgList = new ArrayList<OFMessage>(); |
| 499 | msgList.add(po); |
| 500 | |
| 501 | try { |
| 502 | sw.write(msgList, null); |
| 503 | sw.flush(); |
| 504 | } catch (IOException e) { |
| 505 | log.error("Failure writing packet out to switch", e); |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 510 | private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 511 | if (log.isTraceEnabled()) { |
| 512 | log.trace("Sending ARP request out {}/{}", |
| 513 | HexString.toHexString(dpid), port); |
| 514 | } |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 515 | |
| 516 | OFPacketOut po = new OFPacketOut(); |
| 517 | po.setInPort(OFPort.OFPP_NONE) |
| 518 | .setBufferId(-1) |
| 519 | .setPacketData(arpRequest); |
| 520 | |
| 521 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 522 | actions.add(new OFActionOutput(port)); |
| 523 | po.setActions(actions); |
| 524 | short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH); |
| 525 | po.setActionsLength(actionsLength); |
| 526 | po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength |
| 527 | + arpRequest.length); |
| 528 | |
| 529 | IOFSwitch sw = floodlightProvider.getSwitches().get(dpid); |
| 530 | |
| 531 | if (sw == null) { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 532 | log.warn("Switch not found when sending ARP request"); |
Jonathan Hart | 2f790d2 | 2013-08-15 14:01:24 +1200 | [diff] [blame] | 533 | return; |
| 534 | } |
| 535 | |
| 536 | try { |
| 537 | sw.write(po, null); |
| 538 | sw.flush(); |
| 539 | } catch (IOException e) { |
| 540 | log.error("Failure writing packet out to switch", e); |
| 541 | } |
| 542 | } |
| 543 | |
Jonathan Hart | da4d0e1 | 2013-09-30 21:00:20 +1300 | [diff] [blame] | 544 | private void sendArpReply(ARP arpRequest, long dpid, short port, MACAddress targetMac) { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 545 | if (log.isTraceEnabled()) { |
| 546 | log.trace("Sending reply {} => {} to {}", new Object[] { |
| 547 | inetAddressToString(arpRequest.getTargetProtocolAddress()), |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 548 | targetMac, |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 549 | inetAddressToString(arpRequest.getSenderProtocolAddress())}); |
| 550 | } |
Jonathan Hart | 1633a40 | 2013-08-24 11:38:56 +1200 | [diff] [blame] | 551 | |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 552 | ARP arpReply = new ARP(); |
| 553 | arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET) |
| 554 | .setProtocolType(ARP.PROTO_TYPE_IP) |
| 555 | .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH) |
Jonathan Hart | 08ee852 | 2013-09-22 17:34:43 +1200 | [diff] [blame] | 556 | .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH) |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 557 | .setOpCode(ARP.OP_REPLY) |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 558 | .setSenderHardwareAddress(targetMac.toBytes()) |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 559 | .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress()) |
| 560 | .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress()) |
| 561 | .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress()); |
| 562 | |
Jonathan Hart | 1cf9de0 | 2013-10-21 17:42:29 -0700 | [diff] [blame] | 563 | |
| 564 | |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 565 | Ethernet eth = new Ethernet(); |
| 566 | eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress()) |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 567 | .setSourceMACAddress(targetMac.toBytes()) |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 568 | .setEtherType(Ethernet.TYPE_ARP) |
| 569 | .setPayload(arpReply); |
| 570 | |
Jonathan Hart | 1cf9de0 | 2013-10-21 17:42:29 -0700 | [diff] [blame] | 571 | if (vlan != NO_VLAN) { |
| 572 | eth.setVlanID(vlan) |
| 573 | .setPriorityCode((byte)0); |
| 574 | } |
| 575 | |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 576 | List<OFAction> actions = new ArrayList<OFAction>(); |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 577 | actions.add(new OFActionOutput(port)); |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 578 | |
| 579 | OFPacketOut po = new OFPacketOut(); |
| 580 | po.setInPort(OFPort.OFPP_NONE) |
| 581 | .setBufferId(-1) |
Jonathan Hart | 8ec133c | 2013-06-26 15:25:18 +1200 | [diff] [blame] | 582 | .setPacketData(eth.serialize()) |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 583 | .setActions(actions) |
| 584 | .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH) |
| 585 | .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH |
| 586 | + po.getPacketData().length); |
| 587 | |
| 588 | List<OFMessage> msgList = new ArrayList<OFMessage>(); |
| 589 | msgList.add(po); |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 590 | |
| 591 | IOFSwitch sw = floodlightProvider.getSwitches().get(dpid); |
| 592 | |
| 593 | if (sw == null) { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 594 | log.warn("Switch {} not found when sending ARP reply", |
Jonathan Hart | 1633a40 | 2013-08-24 11:38:56 +1200 | [diff] [blame] | 595 | HexString.toHexString(dpid)); |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 596 | return; |
| 597 | } |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 598 | |
| 599 | try { |
| 600 | sw.write(msgList, null); |
| 601 | sw.flush(); |
| 602 | } catch (IOException e) { |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 603 | log.error("Failure writing packet out to switch", e); |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 604 | } |
| 605 | } |
Jonathan Hart | da4d0e1 | 2013-09-30 21:00:20 +1300 | [diff] [blame] | 606 | |
| 607 | private String inetAddressToString(byte[] bytes) { |
| 608 | try { |
| 609 | return InetAddress.getByAddress(bytes).getHostAddress(); |
| 610 | } catch (UnknownHostException e) { |
| 611 | log.debug("Invalid IP address", e); |
| 612 | return ""; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | /* |
| 617 | * IProxyArpService methods |
| 618 | */ |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 619 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 620 | @Override |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 621 | public MACAddress getMacAddress(InetAddress ipAddress) { |
| 622 | return arpCache.lookup(ipAddress); |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 623 | } |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 624 | |
| 625 | @Override |
| 626 | public void sendArpRequest(InetAddress ipAddress, IArpRequester requester, |
| 627 | boolean retry) { |
Jonathan Hart | df6ec33 | 2013-08-04 01:37:14 +1200 | [diff] [blame] | 628 | arpRequests.put(ipAddress, new ArpRequest(requester, retry)); |
Jonathan Hart | f0c0dcb | 2013-07-24 15:28:42 +1200 | [diff] [blame] | 629 | |
Jonathan Hart | 6e61821 | 2013-08-21 22:28:43 +1200 | [diff] [blame] | 630 | //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] | 631 | if (!configService.isInterfaceAddress(ipAddress)) { |
Jonathan Hart | 6e61821 | 2013-08-21 22:28:43 +1200 | [diff] [blame] | 632 | sendArpRequestForAddress(ipAddress); |
| 633 | } |
| 634 | } |
Jonathan Hart | 5afde49 | 2013-10-01 12:30:53 +1300 | [diff] [blame] | 635 | |
| 636 | @Override |
| 637 | public List<String> getMappings() { |
| 638 | return arpCache.getMappings(); |
| 639 | } |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 640 | } |