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