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