blob: 283a0962e6ff5654d265652625ad796391c5fed0 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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 */
alshabibb5522ff2014-09-29 19:20:00 -070016package org.onlab.onos.net.proxyarp.impl;
17
18import static com.google.common.base.Preconditions.checkArgument;
19import static com.google.common.base.Preconditions.checkNotNull;
20import static org.slf4j.LoggerFactory.getLogger;
21
22import java.nio.ByteBuffer;
Jonathan Hart1f793a72014-11-12 23:22:02 -080023import java.util.HashSet;
alshabibb5522ff2014-09-29 19:20:00 -070024import java.util.List;
25import java.util.Map.Entry;
26import java.util.Set;
27
28import org.apache.felix.scr.annotations.Activate;
29import org.apache.felix.scr.annotations.Component;
30import org.apache.felix.scr.annotations.Deactivate;
31import org.apache.felix.scr.annotations.Reference;
32import org.apache.felix.scr.annotations.ReferenceCardinality;
33import org.apache.felix.scr.annotations.Service;
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -070034import org.onlab.onos.net.ConnectPoint;
alshabibb5522ff2014-09-29 19:20:00 -070035import org.onlab.onos.net.Device;
36import org.onlab.onos.net.Host;
37import org.onlab.onos.net.HostId;
38import org.onlab.onos.net.Link;
39import org.onlab.onos.net.Port;
40import org.onlab.onos.net.PortNumber;
41import org.onlab.onos.net.device.DeviceEvent;
42import org.onlab.onos.net.device.DeviceListener;
43import org.onlab.onos.net.device.DeviceService;
44import org.onlab.onos.net.flow.DefaultTrafficTreatment;
45import org.onlab.onos.net.flow.TrafficTreatment;
46import org.onlab.onos.net.host.HostService;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070047import org.onlab.onos.net.host.InterfaceIpAddress;
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -070048import org.onlab.onos.net.host.PortAddresses;
alshabibb5522ff2014-09-29 19:20:00 -070049import org.onlab.onos.net.link.LinkEvent;
50import org.onlab.onos.net.link.LinkListener;
51import org.onlab.onos.net.link.LinkService;
52import org.onlab.onos.net.packet.DefaultOutboundPacket;
alshabibc274c902014-10-03 14:58:27 -070053import org.onlab.onos.net.packet.InboundPacket;
54import org.onlab.onos.net.packet.PacketContext;
alshabibb5522ff2014-09-29 19:20:00 -070055import org.onlab.onos.net.packet.PacketService;
56import org.onlab.onos.net.proxyarp.ProxyArpService;
57import org.onlab.packet.ARP;
58import org.onlab.packet.Ethernet;
Pavlin Radoslavov5b5dc482014-11-05 14:48:08 -080059import org.onlab.packet.Ip4Address;
Jonathan Hart1f793a72014-11-12 23:22:02 -080060import org.onlab.packet.IpAddress;
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -070061import org.onlab.packet.MacAddress;
alshabibb5522ff2014-09-29 19:20:00 -070062import org.onlab.packet.VlanId;
63import org.slf4j.Logger;
64
65import com.google.common.collect.HashMultimap;
66import com.google.common.collect.Lists;
67import com.google.common.collect.Multimap;
68
alshabibb5522ff2014-09-29 19:20:00 -070069@Component(immediate = true)
70@Service
71public 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 Hart704ca142014-10-09 09:34:39 -070079 private static final String NOT_ARP_REPLY = "ARP is not a reply.";
alshabibb5522ff2014-09-29 19:20:00 -070080
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 Radoslavov5b5dc482014-11-05 14:48:08 -0800118 public boolean known(Ip4Address addr) {
Yuta HIGUCHI59718042014-10-04 22:04:56 -0700119 checkNotNull(addr, MAC_ADDR_NULL);
alshabibb5522ff2014-09-29 19:20:00 -0700120 Set<Host> hosts = hostService.getHostsByIp(addr);
121 return !hosts.isEmpty();
122 }
123
124 @Override
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700125 public void reply(Ethernet eth, ConnectPoint inPort) {
Yuta HIGUCHI59718042014-10-04 22:04:56 -0700126 checkNotNull(eth, REQUEST_NULL);
alshabibb5522ff2014-09-29 19:20:00 -0700127 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 Hartdbdbdbb2014-10-06 18:35:30 -0700131 checkNotNull(inPort);
132
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700133 // 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 Radoslavov5b5dc482014-11-05 14:48:08 -0800136 Ip4Address target =
137 Ip4Address.valueOf(arp.getTargetProtocolAddress());
Jonathan Harta887ba82014-11-03 15:20:52 -0800138 Set<PortAddresses> addressSet =
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700139 hostService.getAddressBindingsForPort(inPort);
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700140
Jonathan Harta887ba82014-11-03 15:20:52 -0800141 for (PortAddresses addresses : addressSet) {
142 for (InterfaceIpAddress ia : addresses.ipAddresses()) {
143 if (ia.ipAddress().equals(target)) {
144 Ethernet arpReply =
Pavlin Radoslavov5b5dc482014-11-05 14:48:08 -0800145 buildArpReply(target, addresses.mac(), eth);
Jonathan Harta887ba82014-11-03 15:20:52 -0800146 sendTo(arpReply, inPort);
147 }
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700148 }
149 }
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700150 return;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700151 } 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 Hart1f793a72014-11-12 23:22:02 -0800154 // address. Forward it over to the correct ports.
Pavlin Radoslavov5b5dc482014-11-05 14:48:08 -0800155 Ip4Address source =
156 Ip4Address.valueOf(arp.getSenderProtocolAddress());
Jonathan Hart1f793a72014-11-12 23:22:02 -0800157 Set<PortAddresses> sourceAddresses = findPortsInSubnet(source);
158 boolean matched = false;
159 for (PortAddresses pa : sourceAddresses) {
160 for (InterfaceIpAddress ia : pa.ipAddresses()) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700161 if (ia.ipAddress().equals(source)) {
Jonathan Hart1f793a72014-11-12 23:22:02 -0800162 matched = true;
163 sendTo(eth, pa.connectPoint());
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700164 }
165 }
166 }
Jonathan Hart1f793a72014-11-12 23:22:02 -0800167
168 if (matched) {
169 return;
170 }
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700171 }
172
173 // Continue with normal proxy ARP case
alshabibb5522ff2014-09-29 19:20:00 -0700174
175 VlanId vlan = VlanId.vlanId(eth.getVlanID());
Pavlin Radoslavov5b5dc482014-11-05 14:48:08 -0800176 Set<Host> hosts = hostService.getHostsByIp(
177 Ip4Address.valueOf(arp.getTargetProtocolAddress()));
alshabibb5522ff2014-09-29 19:20:00 -0700178
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 Radoslavov5b5dc482014-11-05 14:48:08 -0800195 //
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 Hartdbdbdbb2014-10-06 18:35:30 -0700208 }
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
tom9a693fd2014-10-03 11:32:19 -0700225 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700226 builder.setOutput(outPort.port());
227 packetService.emit(new DefaultOutboundPacket(outPort.deviceId(),
228 builder.build(), ByteBuffer.wrap(packet.serialize())));
229 }
230
231 /**
Jonathan Hart1f793a72014-11-12 23:22:02 -0800232 * Finds ports with an address in the subnet of the target address.
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700233 *
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700234 * @param target the target address to find a matching port for
Jonathan Hart1f793a72014-11-12 23:22:02 -0800235 * @return a set of PortAddresses describing ports in the subnet
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700236 */
Jonathan Hart1f793a72014-11-12 23:22:02 -0800237 private Set<PortAddresses> findPortsInSubnet(Ip4Address target) {
238 Set<PortAddresses> result = new HashSet<PortAddresses>();
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700239 for (PortAddresses addresses : hostService.getAddressBindings()) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700240 for (InterfaceIpAddress ia : addresses.ipAddresses()) {
241 if (ia.subnetAddress().contains(target)) {
Jonathan Hart1f793a72014-11-12 23:22:02 -0800242 result.add(addresses);
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700243 }
244 }
245 }
Jonathan Hart1f793a72014-11-12 23:22:02 -0800246 return result;
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700247 }
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 Radoslavov76b0ae22014-10-27 15:33:19 -0700257 //
258 // TODO: Is this sufficient to identify outside-facing ports: just
259 // having IP addresses on a port?
260 //
Jonathan Harta887ba82014-11-03 15:20:52 -0800261 return !hostService.getAddressBindingsForPort(port).isEmpty();
alshabibb5522ff2014-09-29 19:20:00 -0700262 }
263
264 @Override
265 public void forward(Ethernet eth) {
Yuta HIGUCHI59718042014-10-04 22:04:56 -0700266 checkNotNull(eth, REQUEST_NULL);
alshabibb5522ff2014-09-29 19:20:00 -0700267 checkArgument(eth.getEtherType() == Ethernet.TYPE_ARP,
268 REQUEST_NOT_ARP);
269 ARP arp = (ARP) eth.getPayload();
Jonathan Hart704ca142014-10-09 09:34:39 -0700270 checkArgument(arp.getOpCode() == ARP.OP_REPLY, NOT_ARP_REPLY);
alshabibb5522ff2014-09-29 19:20:00 -0700271
272 Host h = hostService.getHost(HostId.hostId(eth.getDestinationMAC(),
273 VlanId.vlanId(eth.getVlanID())));
274
275 if (h == null) {
276 flood(eth);
277 } else {
tom9a693fd2014-10-03 11:32:19 -0700278 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabibb5522ff2014-09-29 19:20:00 -0700279 builder.setOutput(h.location().port());
280 packetService.emit(new DefaultOutboundPacket(h.location().deviceId(),
281 builder.build(), ByteBuffer.wrap(eth.serialize())));
282 }
283
284 }
285
alshabibc274c902014-10-03 14:58:27 -0700286 @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 Hartdbdbdbb2014-10-06 18:35:30 -0700295 reply(ethPkt, context.inPacket().receivedFrom());
alshabibc274c902014-10-03 14:58:27 -0700296 }
297 context.block();
298 return true;
299 }
300 return false;
301 }
302
alshabibb5522ff2014-09-29 19:20:00 -0700303 /**
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 Hartdbdbdbb2014-10-06 18:35:30 -0700313 ConnectPoint cp = new ConnectPoint(entry.getKey().id(), entry.getValue());
314 if (isOutsidePort(cp)) {
315 continue;
316 }
317
tom9a693fd2014-10-03 11:32:19 -0700318 builder = DefaultTrafficTreatment.builder();
alshabibb5522ff2014-09-29 19:20:00 -0700319 builder.setOutput(entry.getValue());
320 packetService.emit(new DefaultOutboundPacket(entry.getKey().id(),
321 builder.build(), buf));
322 }
alshabibb5522ff2014-09-29 19:20:00 -0700323 }
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 HIGUCHI3541bf22014-10-04 22:06:19 -0700339 if (l.src().deviceId().equals(d.id())
alshabibb5522ff2014-09-29 19:20:00 -0700340 && ports.contains(l.src().port())) {
341 ports.remove(l.src().port());
342 internalPorts.put(d, l.src().port());
343 }
Yuta HIGUCHI3541bf22014-10-04 22:06:19 -0700344 if (l.dst().deviceId().equals(d.id())
alshabibb5522ff2014-09-29 19:20:00 -0700345 && 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 Hartdbdbdbb2014-10-06 18:35:30 -0700366 * 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
alshabibb5522ff2014-09-29 19:20:00 -0700372 */
Pavlin Radoslavov5b5dc482014-11-05 14:48:08 -0800373 private Ethernet buildArpReply(Ip4Address srcIp, MacAddress srcMac,
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700374 Ethernet request) {
375
alshabibb5522ff2014-09-29 19:20:00 -0700376 Ethernet eth = new Ethernet();
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800377 eth.setDestinationMACAddress(request.getSourceMAC());
378 eth.setSourceMACAddress(srcMac);
alshabibb5522ff2014-09-29 19:20:00 -0700379 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);
alshabib6eb438a2014-10-01 16:39:37 -0700386
Pavlin Radoslavov5b5dc482014-11-05 14:48:08 -0800387 arp.setProtocolAddressLength((byte) Ip4Address.BYTE_LENGTH);
alshabibb5522ff2014-09-29 19:20:00 -0700388 arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH);
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800389 arp.setSenderHardwareAddress(srcMac.toBytes());
alshabibb5522ff2014-09-29 19:20:00 -0700390 arp.setTargetHardwareAddress(request.getSourceMACAddress());
391
392 arp.setTargetProtocolAddress(((ARP) request.getPayload())
393 .getSenderProtocolAddress());
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700394 arp.setSenderProtocolAddress(srcIp.toInt());
alshabibb5522ff2014-09-29 19:20:00 -0700395 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:
alshabibb5522ff2014-09-29 19:20:00 -0700444 case DEVICE_SUSPENDED:
445 case DEVICE_UPDATED:
alshabibb5522ff2014-09-29 19:20:00 -0700446 // 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:
alshabib6eb438a2014-10-01 16:39:37 -0700455 case PORT_UPDATED:
alshabibb5522ff2014-09-29 19:20:00 -0700456 synchronized (externalPorts) {
alshabib6eb438a2014-10-01 16:39:37 -0700457 if (event.port().isEnabled()) {
458 externalPorts.put(device, event.port().number());
459 internalPorts.remove(device, event.port().number());
460 }
alshabibb5522ff2014-09-29 19:20:00 -0700461 }
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
alshabibc274c902014-10-03 14:58:27 -0700476 }
alshabibb5522ff2014-09-29 19:20:00 -0700477
478}