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