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