Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.proxyarp.impl; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | import static org.slf4j.LoggerFactory.getLogger; |
| 21 | |
| 22 | import java.nio.ByteBuffer; |
| 23 | import java.util.List; |
| 24 | import java.util.Map.Entry; |
| 25 | import java.util.Set; |
| 26 | |
| 27 | import org.apache.felix.scr.annotations.Activate; |
| 28 | import org.apache.felix.scr.annotations.Component; |
| 29 | import org.apache.felix.scr.annotations.Deactivate; |
| 30 | import org.apache.felix.scr.annotations.Reference; |
| 31 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 32 | import org.apache.felix.scr.annotations.Service; |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 33 | import org.onlab.onos.net.ConnectPoint; |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.Device; |
| 35 | import org.onlab.onos.net.Host; |
| 36 | import org.onlab.onos.net.HostId; |
| 37 | import org.onlab.onos.net.Link; |
| 38 | import org.onlab.onos.net.Port; |
| 39 | import org.onlab.onos.net.PortNumber; |
| 40 | import org.onlab.onos.net.device.DeviceEvent; |
| 41 | import org.onlab.onos.net.device.DeviceListener; |
| 42 | import org.onlab.onos.net.device.DeviceService; |
| 43 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 44 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 45 | import org.onlab.onos.net.host.HostService; |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 46 | import org.onlab.onos.net.host.InterfaceIpAddress; |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 47 | import org.onlab.onos.net.host.PortAddresses; |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 48 | import org.onlab.onos.net.link.LinkEvent; |
| 49 | import org.onlab.onos.net.link.LinkListener; |
| 50 | import org.onlab.onos.net.link.LinkService; |
| 51 | import org.onlab.onos.net.packet.DefaultOutboundPacket; |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 52 | import org.onlab.onos.net.packet.InboundPacket; |
| 53 | import org.onlab.onos.net.packet.PacketContext; |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 54 | import org.onlab.onos.net.packet.PacketService; |
| 55 | import org.onlab.onos.net.proxyarp.ProxyArpService; |
| 56 | import org.onlab.packet.ARP; |
| 57 | import org.onlab.packet.Ethernet; |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 58 | import org.onlab.packet.IpAddress; |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 59 | import org.onlab.packet.IpPrefix; |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 60 | import org.onlab.packet.MacAddress; |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 61 | import org.onlab.packet.VlanId; |
| 62 | import org.slf4j.Logger; |
| 63 | |
| 64 | import com.google.common.collect.HashMultimap; |
| 65 | import com.google.common.collect.Lists; |
| 66 | import com.google.common.collect.Multimap; |
| 67 | |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 68 | @Component(immediate = true) |
| 69 | @Service |
| 70 | public class ProxyArpManager implements ProxyArpService { |
| 71 | |
| 72 | private final Logger log = getLogger(getClass()); |
| 73 | |
| 74 | private static final String MAC_ADDR_NULL = "Mac address cannot be null."; |
| 75 | private static final String REQUEST_NULL = "Arp request cannot be null."; |
| 76 | private static final String REQUEST_NOT_ARP = "Ethernet frame does not contain ARP request."; |
| 77 | private static final String NOT_ARP_REQUEST = "ARP is not a request."; |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 78 | private static final String NOT_ARP_REPLY = "ARP is not a reply."; |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 79 | |
| 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 81 | protected HostService hostService; |
| 82 | |
| 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 84 | protected PacketService packetService; |
| 85 | |
| 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 87 | protected LinkService linkService; |
| 88 | |
| 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 90 | protected DeviceService deviceService; |
| 91 | |
| 92 | private final Multimap<Device, PortNumber> internalPorts = |
| 93 | HashMultimap.<Device, PortNumber>create(); |
| 94 | |
| 95 | private final Multimap<Device, PortNumber> externalPorts = |
| 96 | HashMultimap.<Device, PortNumber>create(); |
| 97 | |
| 98 | /** |
| 99 | * Listens to both device service and link service to determine |
| 100 | * whether a port is internal or external. |
| 101 | */ |
| 102 | @Activate |
| 103 | public void activate() { |
| 104 | deviceService.addListener(new InternalDeviceListener()); |
| 105 | linkService.addListener(new InternalLinkListener()); |
| 106 | determinePortLocations(); |
| 107 | log.info("Started"); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | @Deactivate |
| 112 | public void deactivate() { |
| 113 | log.info("Stopped"); |
| 114 | } |
| 115 | |
| 116 | @Override |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 117 | public boolean known(IpAddress addr) { |
Yuta HIGUCHI | 5971804 | 2014-10-04 22:04:56 -0700 | [diff] [blame] | 118 | checkNotNull(addr, MAC_ADDR_NULL); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 119 | Set<Host> hosts = hostService.getHostsByIp(addr); |
| 120 | return !hosts.isEmpty(); |
| 121 | } |
| 122 | |
| 123 | @Override |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 124 | public void reply(Ethernet eth, ConnectPoint inPort) { |
Yuta HIGUCHI | 5971804 | 2014-10-04 22:04:56 -0700 | [diff] [blame] | 125 | checkNotNull(eth, REQUEST_NULL); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 126 | checkArgument(eth.getEtherType() == Ethernet.TYPE_ARP, |
| 127 | REQUEST_NOT_ARP); |
| 128 | ARP arp = (ARP) eth.getPayload(); |
| 129 | checkArgument(arp.getOpCode() == ARP.OP_REQUEST, NOT_ARP_REQUEST); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 130 | checkNotNull(inPort); |
| 131 | |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 132 | // If the request came from outside the network, only reply if it was |
| 133 | // for one of our external addresses. |
| 134 | if (isOutsidePort(inPort)) { |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 135 | IpAddress target = |
| 136 | IpAddress.valueOf(arp.getTargetProtocolAddress()); |
| 137 | PortAddresses addresses = |
| 138 | hostService.getAddressBindingsForPort(inPort); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 139 | |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 140 | for (InterfaceIpAddress ia : addresses.ipAddresses()) { |
| 141 | if (ia.ipAddress().equals(target)) { |
| 142 | Ethernet arpReply = |
| 143 | buildArpReply(ia.ipAddress(), addresses.mac(), eth); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 144 | sendTo(arpReply, inPort); |
| 145 | } |
| 146 | } |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 147 | return; |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 148 | } else { |
| 149 | // If the source address matches one of our external addresses |
| 150 | // it could be a request from an internal host to an external |
| 151 | // address. Forward it over to the correct port. |
| 152 | IpAddress source = |
| 153 | IpAddress.valueOf(arp.getSenderProtocolAddress()); |
| 154 | PortAddresses sourceAddresses = findPortInSubnet(source); |
| 155 | if (sourceAddresses != null) { |
| 156 | for (InterfaceIpAddress ia : sourceAddresses.ipAddresses()) { |
| 157 | if (ia.ipAddress().equals(source)) { |
| 158 | sendTo(eth, sourceAddresses.connectPoint()); |
| 159 | return; |
| 160 | } |
| 161 | } |
| 162 | } |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | // Continue with normal proxy ARP case |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 166 | |
| 167 | VlanId vlan = VlanId.vlanId(eth.getVlanID()); |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 168 | Set<Host> hosts = hostService.getHostsByIp(IpAddress.valueOf(arp |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 169 | .getTargetProtocolAddress())); |
| 170 | |
| 171 | Host dst = null; |
| 172 | Host src = hostService.getHost(HostId.hostId(eth.getSourceMAC(), |
| 173 | VlanId.vlanId(eth.getVlanID()))); |
| 174 | |
| 175 | for (Host host : hosts) { |
| 176 | if (host.vlan().equals(vlan)) { |
| 177 | dst = host; |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if (src == null || dst == null) { |
| 183 | flood(eth); |
| 184 | return; |
| 185 | } |
| 186 | |
Jonathan Hart | 7d1ad60 | 2014-10-17 11:48:32 -0700 | [diff] [blame] | 187 | // TODO find the correct IP address |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 188 | IpAddress ipAddress = dst.ipAddresses().iterator().next(); |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 189 | Ethernet arpReply = buildArpReply(ipAddress, dst.mac(), eth); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 190 | // TODO: check send status with host service. |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 191 | sendTo(arpReply, src.location()); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Outputs the given packet out the given port. |
| 196 | * |
| 197 | * @param packet the packet to send |
| 198 | * @param outPort the port to send it out |
| 199 | */ |
| 200 | private void sendTo(Ethernet packet, ConnectPoint outPort) { |
| 201 | if (internalPorts.containsEntry( |
| 202 | deviceService.getDevice(outPort.deviceId()), outPort.port())) { |
| 203 | // Sanity check to make sure we don't send the packet out an |
| 204 | // internal port and create a loop (could happen due to |
| 205 | // misconfiguration). |
| 206 | return; |
| 207 | } |
| 208 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 209 | TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder(); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 210 | builder.setOutput(outPort.port()); |
| 211 | packetService.emit(new DefaultOutboundPacket(outPort.deviceId(), |
| 212 | builder.build(), ByteBuffer.wrap(packet.serialize()))); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Finds the port with an address in the subnet of the target address, if |
| 217 | * one exists. |
| 218 | * |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 219 | * @param target the target address to find a matching port for |
| 220 | * @return a PortAddresses object if one was found, otherwise null |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 221 | */ |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 222 | private PortAddresses findPortInSubnet(IpAddress target) { |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 223 | for (PortAddresses addresses : hostService.getAddressBindings()) { |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 224 | for (InterfaceIpAddress ia : addresses.ipAddresses()) { |
| 225 | if (ia.subnetAddress().contains(target)) { |
| 226 | return addresses; |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | } |
| 230 | return null; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Returns whether the given port is an outside-facing port with an IP |
| 235 | * address configured. |
| 236 | * |
| 237 | * @param port the port to check |
| 238 | * @return true if the port is an outside-facing port, otherwise false |
| 239 | */ |
| 240 | private boolean isOutsidePort(ConnectPoint port) { |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 241 | // |
| 242 | // TODO: Is this sufficient to identify outside-facing ports: just |
| 243 | // having IP addresses on a port? |
| 244 | // |
| 245 | return !hostService.getAddressBindingsForPort(port).ipAddresses().isEmpty(); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public void forward(Ethernet eth) { |
Yuta HIGUCHI | 5971804 | 2014-10-04 22:04:56 -0700 | [diff] [blame] | 250 | checkNotNull(eth, REQUEST_NULL); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 251 | checkArgument(eth.getEtherType() == Ethernet.TYPE_ARP, |
| 252 | REQUEST_NOT_ARP); |
| 253 | ARP arp = (ARP) eth.getPayload(); |
Jonathan Hart | 704ca14 | 2014-10-09 09:34:39 -0700 | [diff] [blame] | 254 | checkArgument(arp.getOpCode() == ARP.OP_REPLY, NOT_ARP_REPLY); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 255 | |
| 256 | Host h = hostService.getHost(HostId.hostId(eth.getDestinationMAC(), |
| 257 | VlanId.vlanId(eth.getVlanID()))); |
| 258 | |
| 259 | if (h == null) { |
| 260 | flood(eth); |
| 261 | } else { |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 262 | TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder(); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 263 | builder.setOutput(h.location().port()); |
| 264 | packetService.emit(new DefaultOutboundPacket(h.location().deviceId(), |
| 265 | builder.build(), ByteBuffer.wrap(eth.serialize()))); |
| 266 | } |
| 267 | |
| 268 | } |
| 269 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 270 | @Override |
| 271 | public boolean handleArp(PacketContext context) { |
| 272 | InboundPacket pkt = context.inPacket(); |
| 273 | Ethernet ethPkt = pkt.parsed(); |
| 274 | if (ethPkt.getEtherType() == Ethernet.TYPE_ARP) { |
| 275 | ARP arp = (ARP) ethPkt.getPayload(); |
| 276 | if (arp.getOpCode() == ARP.OP_REPLY) { |
| 277 | forward(ethPkt); |
| 278 | } else if (arp.getOpCode() == ARP.OP_REQUEST) { |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 279 | reply(ethPkt, context.inPacket().receivedFrom()); |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 280 | } |
| 281 | context.block(); |
| 282 | return true; |
| 283 | } |
| 284 | return false; |
| 285 | } |
| 286 | |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 287 | /** |
| 288 | * Flood the arp request at all edges in the network. |
| 289 | * @param request the arp request. |
| 290 | */ |
| 291 | private void flood(Ethernet request) { |
| 292 | TrafficTreatment.Builder builder = null; |
| 293 | ByteBuffer buf = ByteBuffer.wrap(request.serialize()); |
| 294 | |
| 295 | synchronized (externalPorts) { |
| 296 | for (Entry<Device, PortNumber> entry : externalPorts.entries()) { |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 297 | ConnectPoint cp = new ConnectPoint(entry.getKey().id(), entry.getValue()); |
| 298 | if (isOutsidePort(cp)) { |
| 299 | continue; |
| 300 | } |
| 301 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 302 | builder = DefaultTrafficTreatment.builder(); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 303 | builder.setOutput(entry.getValue()); |
| 304 | packetService.emit(new DefaultOutboundPacket(entry.getKey().id(), |
| 305 | builder.build(), buf)); |
| 306 | } |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Determines the location of all known ports in the system. |
| 312 | */ |
| 313 | private void determinePortLocations() { |
| 314 | Iterable<Device> devices = deviceService.getDevices(); |
| 315 | Iterable<Link> links = null; |
| 316 | List<PortNumber> ports = null; |
| 317 | for (Device d : devices) { |
| 318 | ports = buildPortNumberList(deviceService.getPorts(d.id())); |
| 319 | links = linkService.getLinks(); |
| 320 | for (Link l : links) { |
| 321 | // for each link, mark the concerned ports as internal |
| 322 | // and the remaining ports are therefore external. |
Yuta HIGUCHI | 3541bf2 | 2014-10-04 22:06:19 -0700 | [diff] [blame] | 323 | if (l.src().deviceId().equals(d.id()) |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 324 | && ports.contains(l.src().port())) { |
| 325 | ports.remove(l.src().port()); |
| 326 | internalPorts.put(d, l.src().port()); |
| 327 | } |
Yuta HIGUCHI | 3541bf2 | 2014-10-04 22:06:19 -0700 | [diff] [blame] | 328 | if (l.dst().deviceId().equals(d.id()) |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 329 | && ports.contains(l.dst().port())) { |
| 330 | ports.remove(l.dst().port()); |
| 331 | internalPorts.put(d, l.dst().port()); |
| 332 | } |
| 333 | } |
| 334 | synchronized (externalPorts) { |
| 335 | externalPorts.putAll(d, ports); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | } |
| 340 | |
| 341 | private List<PortNumber> buildPortNumberList(List<Port> ports) { |
| 342 | List<PortNumber> portNumbers = Lists.newLinkedList(); |
| 343 | for (Port p : ports) { |
| 344 | portNumbers.add(p.number()); |
| 345 | } |
| 346 | return portNumbers; |
| 347 | } |
| 348 | |
| 349 | /** |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 350 | * Builds an ARP reply based on a request. |
| 351 | * |
| 352 | * @param srcIp the IP address to use as the reply source |
| 353 | * @param srcMac the MAC address to use as the reply source |
| 354 | * @param request the ARP request we got |
| 355 | * @return an Ethernet frame containing the ARP reply |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 356 | */ |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 357 | private Ethernet buildArpReply(IpAddress srcIp, MacAddress srcMac, |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 358 | Ethernet request) { |
| 359 | |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 360 | Ethernet eth = new Ethernet(); |
| 361 | eth.setDestinationMACAddress(request.getSourceMACAddress()); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 362 | eth.setSourceMACAddress(srcMac.getAddress()); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 363 | eth.setEtherType(Ethernet.TYPE_ARP); |
| 364 | eth.setVlanID(request.getVlanID()); |
| 365 | |
| 366 | ARP arp = new ARP(); |
| 367 | arp.setOpCode(ARP.OP_REPLY); |
| 368 | arp.setProtocolType(ARP.PROTO_TYPE_IP); |
| 369 | arp.setHardwareType(ARP.HW_TYPE_ETHERNET); |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 370 | |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 371 | arp.setProtocolAddressLength((byte) IpPrefix.INET_LEN); |
| 372 | arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH); |
Jonathan Hart | dbdbdbb | 2014-10-06 18:35:30 -0700 | [diff] [blame] | 373 | arp.setSenderHardwareAddress(srcMac.getAddress()); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 374 | arp.setTargetHardwareAddress(request.getSourceMACAddress()); |
| 375 | |
| 376 | arp.setTargetProtocolAddress(((ARP) request.getPayload()) |
| 377 | .getSenderProtocolAddress()); |
Jonathan Hart | bcae7bd | 2014-10-16 10:24:41 -0700 | [diff] [blame] | 378 | arp.setSenderProtocolAddress(srcIp.toInt()); |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 379 | eth.setPayload(arp); |
| 380 | return eth; |
| 381 | } |
| 382 | |
| 383 | public class InternalLinkListener implements LinkListener { |
| 384 | |
| 385 | @Override |
| 386 | public void event(LinkEvent event) { |
| 387 | Link link = event.subject(); |
| 388 | Device src = deviceService.getDevice(link.src().deviceId()); |
| 389 | Device dst = deviceService.getDevice(link.dst().deviceId()); |
| 390 | switch (event.type()) { |
| 391 | case LINK_ADDED: |
| 392 | synchronized (externalPorts) { |
| 393 | externalPorts.remove(src, link.src().port()); |
| 394 | externalPorts.remove(dst, link.dst().port()); |
| 395 | internalPorts.put(src, link.src().port()); |
| 396 | internalPorts.put(dst, link.dst().port()); |
| 397 | } |
| 398 | |
| 399 | break; |
| 400 | case LINK_REMOVED: |
| 401 | synchronized (externalPorts) { |
| 402 | externalPorts.put(src, link.src().port()); |
| 403 | externalPorts.put(dst, link.dst().port()); |
| 404 | internalPorts.remove(src, link.src().port()); |
| 405 | internalPorts.remove(dst, link.dst().port()); |
| 406 | } |
| 407 | |
| 408 | break; |
| 409 | case LINK_UPDATED: |
| 410 | // don't care about links being updated. |
| 411 | break; |
| 412 | default: |
| 413 | break; |
| 414 | } |
| 415 | |
| 416 | } |
| 417 | |
| 418 | } |
| 419 | |
| 420 | public class InternalDeviceListener implements DeviceListener { |
| 421 | |
| 422 | @Override |
| 423 | public void event(DeviceEvent event) { |
| 424 | Device device = event.subject(); |
| 425 | switch (event.type()) { |
| 426 | case DEVICE_ADDED: |
| 427 | case DEVICE_AVAILABILITY_CHANGED: |
| 428 | case DEVICE_MASTERSHIP_CHANGED: |
| 429 | case DEVICE_SUSPENDED: |
| 430 | case DEVICE_UPDATED: |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 431 | // nothing to do in these cases; handled when links get reported |
| 432 | break; |
| 433 | case DEVICE_REMOVED: |
| 434 | synchronized (externalPorts) { |
| 435 | externalPorts.removeAll(device); |
| 436 | internalPorts.removeAll(device); |
| 437 | } |
| 438 | break; |
| 439 | case PORT_ADDED: |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 440 | case PORT_UPDATED: |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 441 | synchronized (externalPorts) { |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 442 | if (event.port().isEnabled()) { |
| 443 | externalPorts.put(device, event.port().number()); |
| 444 | internalPorts.remove(device, event.port().number()); |
| 445 | } |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 446 | } |
| 447 | break; |
| 448 | case PORT_REMOVED: |
| 449 | synchronized (externalPorts) { |
| 450 | externalPorts.remove(device, event.port().number()); |
| 451 | internalPorts.remove(device, event.port().number()); |
| 452 | } |
| 453 | break; |
| 454 | default: |
| 455 | break; |
| 456 | |
| 457 | } |
| 458 | |
| 459 | } |
| 460 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 461 | } |
alshabib | b5522ff | 2014-09-29 19:20:00 -0700 | [diff] [blame] | 462 | |
| 463 | } |