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