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