blob: 49528d04e8893526cbf76446c4e4df045189b641 [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;
23import java.util.List;
24import java.util.Map.Entry;
25import java.util.Set;
26
27import org.apache.felix.scr.annotations.Activate;
28import org.apache.felix.scr.annotations.Component;
29import org.apache.felix.scr.annotations.Deactivate;
30import org.apache.felix.scr.annotations.Reference;
31import org.apache.felix.scr.annotations.ReferenceCardinality;
32import org.apache.felix.scr.annotations.Service;
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -070033import org.onlab.onos.net.ConnectPoint;
alshabibb5522ff2014-09-29 19:20:00 -070034import org.onlab.onos.net.Device;
35import org.onlab.onos.net.Host;
36import org.onlab.onos.net.HostId;
37import org.onlab.onos.net.Link;
38import org.onlab.onos.net.Port;
39import org.onlab.onos.net.PortNumber;
40import org.onlab.onos.net.device.DeviceEvent;
41import org.onlab.onos.net.device.DeviceListener;
42import org.onlab.onos.net.device.DeviceService;
43import org.onlab.onos.net.flow.DefaultTrafficTreatment;
44import org.onlab.onos.net.flow.TrafficTreatment;
45import org.onlab.onos.net.host.HostService;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070046import org.onlab.onos.net.host.InterfaceIpAddress;
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -070047import org.onlab.onos.net.host.PortAddresses;
alshabibb5522ff2014-09-29 19:20:00 -070048import org.onlab.onos.net.link.LinkEvent;
49import org.onlab.onos.net.link.LinkListener;
50import org.onlab.onos.net.link.LinkService;
51import org.onlab.onos.net.packet.DefaultOutboundPacket;
alshabibc274c902014-10-03 14:58:27 -070052import org.onlab.onos.net.packet.InboundPacket;
53import org.onlab.onos.net.packet.PacketContext;
alshabibb5522ff2014-09-29 19:20:00 -070054import org.onlab.onos.net.packet.PacketService;
55import org.onlab.onos.net.proxyarp.ProxyArpService;
56import org.onlab.packet.ARP;
57import org.onlab.packet.Ethernet;
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -070058import org.onlab.packet.IpAddress;
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -070059import org.onlab.packet.MacAddress;
alshabibb5522ff2014-09-29 19:20:00 -070060import org.onlab.packet.VlanId;
61import org.slf4j.Logger;
62
63import com.google.common.collect.HashMultimap;
64import com.google.common.collect.Lists;
65import com.google.common.collect.Multimap;
66
alshabibb5522ff2014-09-29 19:20:00 -070067@Component(immediate = true)
68@Service
69public class ProxyArpManager implements ProxyArpService {
70
71 private final Logger log = getLogger(getClass());
72
73 private static final String MAC_ADDR_NULL = "Mac address cannot be null.";
74 private static final String REQUEST_NULL = "Arp request cannot be null.";
75 private static final String REQUEST_NOT_ARP = "Ethernet frame does not contain ARP request.";
76 private static final String NOT_ARP_REQUEST = "ARP is not a request.";
Jonathan Hart704ca142014-10-09 09:34:39 -070077 private static final String NOT_ARP_REPLY = "ARP is not a reply.";
alshabibb5522ff2014-09-29 19:20:00 -070078
79 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
80 protected HostService hostService;
81
82 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
83 protected PacketService packetService;
84
85 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
86 protected LinkService linkService;
87
88 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
89 protected DeviceService deviceService;
90
91 private final Multimap<Device, PortNumber> internalPorts =
92 HashMultimap.<Device, PortNumber>create();
93
94 private final Multimap<Device, PortNumber> externalPorts =
95 HashMultimap.<Device, PortNumber>create();
96
97 /**
98 * Listens to both device service and link service to determine
99 * whether a port is internal or external.
100 */
101 @Activate
102 public void activate() {
103 deviceService.addListener(new InternalDeviceListener());
104 linkService.addListener(new InternalLinkListener());
105 determinePortLocations();
106 log.info("Started");
107 }
108
109
110 @Deactivate
111 public void deactivate() {
112 log.info("Stopped");
113 }
114
115 @Override
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700116 public boolean known(IpAddress addr) {
Yuta HIGUCHI59718042014-10-04 22:04:56 -0700117 checkNotNull(addr, MAC_ADDR_NULL);
alshabibb5522ff2014-09-29 19:20:00 -0700118 Set<Host> hosts = hostService.getHostsByIp(addr);
119 return !hosts.isEmpty();
120 }
121
122 @Override
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700123 public void reply(Ethernet eth, ConnectPoint inPort) {
Yuta HIGUCHI59718042014-10-04 22:04:56 -0700124 checkNotNull(eth, REQUEST_NULL);
alshabibb5522ff2014-09-29 19:20:00 -0700125 checkArgument(eth.getEtherType() == Ethernet.TYPE_ARP,
126 REQUEST_NOT_ARP);
127 ARP arp = (ARP) eth.getPayload();
128 checkArgument(arp.getOpCode() == ARP.OP_REQUEST, NOT_ARP_REQUEST);
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700129 checkNotNull(inPort);
130
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700131 // If the request came from outside the network, only reply if it was
132 // for one of our external addresses.
133 if (isOutsidePort(inPort)) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700134 IpAddress target =
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700135 IpAddress.valueOf(IpAddress.Version.INET,
136 arp.getTargetProtocolAddress());
Jonathan Harta887ba82014-11-03 15:20:52 -0800137 Set<PortAddresses> addressSet =
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700138 hostService.getAddressBindingsForPort(inPort);
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700139
Jonathan Harta887ba82014-11-03 15:20:52 -0800140 for (PortAddresses addresses : addressSet) {
141 for (InterfaceIpAddress ia : addresses.ipAddresses()) {
142 if (ia.ipAddress().equals(target)) {
143 Ethernet arpReply =
144 buildArpReply(ia.ipAddress(), addresses.mac(), eth);
145 sendTo(arpReply, inPort);
146 }
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700147 }
148 }
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700149 return;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700150 } else {
151 // If the source address matches one of our external addresses
152 // it could be a request from an internal host to an external
153 // address. Forward it over to the correct port.
154 IpAddress source =
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700155 IpAddress.valueOf(IpAddress.Version.INET,
156 arp.getSenderProtocolAddress());
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700157 PortAddresses sourceAddresses = findPortInSubnet(source);
158 if (sourceAddresses != null) {
159 for (InterfaceIpAddress ia : sourceAddresses.ipAddresses()) {
160 if (ia.ipAddress().equals(source)) {
161 sendTo(eth, sourceAddresses.connectPoint());
162 return;
163 }
164 }
165 }
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700166 }
167
168 // Continue with normal proxy ARP case
alshabibb5522ff2014-09-29 19:20:00 -0700169
170 VlanId vlan = VlanId.vlanId(eth.getVlanID());
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700171 Set<Host> hosts =
172 hostService.getHostsByIp(IpAddress.valueOf(IpAddress.Version.INET,
173 arp.getTargetProtocolAddress()));
alshabibb5522ff2014-09-29 19:20:00 -0700174
175 Host dst = null;
176 Host src = hostService.getHost(HostId.hostId(eth.getSourceMAC(),
177 VlanId.vlanId(eth.getVlanID())));
178
179 for (Host host : hosts) {
180 if (host.vlan().equals(vlan)) {
181 dst = host;
182 break;
183 }
184 }
185
186 if (src == null || dst == null) {
187 flood(eth);
188 return;
189 }
190
Jonathan Hart7d1ad602014-10-17 11:48:32 -0700191 // TODO find the correct IP address
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700192 IpAddress ipAddress = dst.ipAddresses().iterator().next();
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700193 Ethernet arpReply = buildArpReply(ipAddress, dst.mac(), eth);
alshabibb5522ff2014-09-29 19:20:00 -0700194 // TODO: check send status with host service.
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700195 sendTo(arpReply, src.location());
196 }
197
198 /**
199 * Outputs the given packet out the given port.
200 *
201 * @param packet the packet to send
202 * @param outPort the port to send it out
203 */
204 private void sendTo(Ethernet packet, ConnectPoint outPort) {
205 if (internalPorts.containsEntry(
206 deviceService.getDevice(outPort.deviceId()), outPort.port())) {
207 // Sanity check to make sure we don't send the packet out an
208 // internal port and create a loop (could happen due to
209 // misconfiguration).
210 return;
211 }
212
tom9a693fd2014-10-03 11:32:19 -0700213 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700214 builder.setOutput(outPort.port());
215 packetService.emit(new DefaultOutboundPacket(outPort.deviceId(),
216 builder.build(), ByteBuffer.wrap(packet.serialize())));
217 }
218
219 /**
220 * Finds the port with an address in the subnet of the target address, if
221 * one exists.
222 *
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700223 * @param target the target address to find a matching port for
224 * @return a PortAddresses object if one was found, otherwise null
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700225 */
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700226 private PortAddresses findPortInSubnet(IpAddress target) {
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700227 for (PortAddresses addresses : hostService.getAddressBindings()) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700228 for (InterfaceIpAddress ia : addresses.ipAddresses()) {
229 if (ia.subnetAddress().contains(target)) {
230 return addresses;
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700231 }
232 }
233 }
234 return null;
235 }
236
237 /**
238 * Returns whether the given port is an outside-facing port with an IP
239 * address configured.
240 *
241 * @param port the port to check
242 * @return true if the port is an outside-facing port, otherwise false
243 */
244 private boolean isOutsidePort(ConnectPoint port) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700245 //
246 // TODO: Is this sufficient to identify outside-facing ports: just
247 // having IP addresses on a port?
248 //
Jonathan Harta887ba82014-11-03 15:20:52 -0800249 return !hostService.getAddressBindingsForPort(port).isEmpty();
alshabibb5522ff2014-09-29 19:20:00 -0700250 }
251
252 @Override
253 public void forward(Ethernet eth) {
Yuta HIGUCHI59718042014-10-04 22:04:56 -0700254 checkNotNull(eth, REQUEST_NULL);
alshabibb5522ff2014-09-29 19:20:00 -0700255 checkArgument(eth.getEtherType() == Ethernet.TYPE_ARP,
256 REQUEST_NOT_ARP);
257 ARP arp = (ARP) eth.getPayload();
Jonathan Hart704ca142014-10-09 09:34:39 -0700258 checkArgument(arp.getOpCode() == ARP.OP_REPLY, NOT_ARP_REPLY);
alshabibb5522ff2014-09-29 19:20:00 -0700259
260 Host h = hostService.getHost(HostId.hostId(eth.getDestinationMAC(),
261 VlanId.vlanId(eth.getVlanID())));
262
263 if (h == null) {
264 flood(eth);
265 } else {
tom9a693fd2014-10-03 11:32:19 -0700266 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabibb5522ff2014-09-29 19:20:00 -0700267 builder.setOutput(h.location().port());
268 packetService.emit(new DefaultOutboundPacket(h.location().deviceId(),
269 builder.build(), ByteBuffer.wrap(eth.serialize())));
270 }
271
272 }
273
alshabibc274c902014-10-03 14:58:27 -0700274 @Override
275 public boolean handleArp(PacketContext context) {
276 InboundPacket pkt = context.inPacket();
277 Ethernet ethPkt = pkt.parsed();
278 if (ethPkt.getEtherType() == Ethernet.TYPE_ARP) {
279 ARP arp = (ARP) ethPkt.getPayload();
280 if (arp.getOpCode() == ARP.OP_REPLY) {
281 forward(ethPkt);
282 } else if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700283 reply(ethPkt, context.inPacket().receivedFrom());
alshabibc274c902014-10-03 14:58:27 -0700284 }
285 context.block();
286 return true;
287 }
288 return false;
289 }
290
alshabibb5522ff2014-09-29 19:20:00 -0700291 /**
292 * Flood the arp request at all edges in the network.
293 * @param request the arp request.
294 */
295 private void flood(Ethernet request) {
296 TrafficTreatment.Builder builder = null;
297 ByteBuffer buf = ByteBuffer.wrap(request.serialize());
298
299 synchronized (externalPorts) {
300 for (Entry<Device, PortNumber> entry : externalPorts.entries()) {
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700301 ConnectPoint cp = new ConnectPoint(entry.getKey().id(), entry.getValue());
302 if (isOutsidePort(cp)) {
303 continue;
304 }
305
tom9a693fd2014-10-03 11:32:19 -0700306 builder = DefaultTrafficTreatment.builder();
alshabibb5522ff2014-09-29 19:20:00 -0700307 builder.setOutput(entry.getValue());
308 packetService.emit(new DefaultOutboundPacket(entry.getKey().id(),
309 builder.build(), buf));
310 }
alshabibb5522ff2014-09-29 19:20:00 -0700311 }
312 }
313
314 /**
315 * Determines the location of all known ports in the system.
316 */
317 private void determinePortLocations() {
318 Iterable<Device> devices = deviceService.getDevices();
319 Iterable<Link> links = null;
320 List<PortNumber> ports = null;
321 for (Device d : devices) {
322 ports = buildPortNumberList(deviceService.getPorts(d.id()));
323 links = linkService.getLinks();
324 for (Link l : links) {
325 // for each link, mark the concerned ports as internal
326 // and the remaining ports are therefore external.
Yuta HIGUCHI3541bf22014-10-04 22:06:19 -0700327 if (l.src().deviceId().equals(d.id())
alshabibb5522ff2014-09-29 19:20:00 -0700328 && ports.contains(l.src().port())) {
329 ports.remove(l.src().port());
330 internalPorts.put(d, l.src().port());
331 }
Yuta HIGUCHI3541bf22014-10-04 22:06:19 -0700332 if (l.dst().deviceId().equals(d.id())
alshabibb5522ff2014-09-29 19:20:00 -0700333 && ports.contains(l.dst().port())) {
334 ports.remove(l.dst().port());
335 internalPorts.put(d, l.dst().port());
336 }
337 }
338 synchronized (externalPorts) {
339 externalPorts.putAll(d, ports);
340 }
341 }
342
343 }
344
345 private List<PortNumber> buildPortNumberList(List<Port> ports) {
346 List<PortNumber> portNumbers = Lists.newLinkedList();
347 for (Port p : ports) {
348 portNumbers.add(p.number());
349 }
350 return portNumbers;
351 }
352
353 /**
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700354 * Builds an ARP reply based on a request.
355 *
356 * @param srcIp the IP address to use as the reply source
357 * @param srcMac the MAC address to use as the reply source
358 * @param request the ARP request we got
359 * @return an Ethernet frame containing the ARP reply
alshabibb5522ff2014-09-29 19:20:00 -0700360 */
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700361 private Ethernet buildArpReply(IpAddress srcIp, MacAddress srcMac,
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700362 Ethernet request) {
363
alshabibb5522ff2014-09-29 19:20:00 -0700364 Ethernet eth = new Ethernet();
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800365 eth.setDestinationMACAddress(request.getSourceMAC());
366 eth.setSourceMACAddress(srcMac);
alshabibb5522ff2014-09-29 19:20:00 -0700367 eth.setEtherType(Ethernet.TYPE_ARP);
368 eth.setVlanID(request.getVlanID());
369
370 ARP arp = new ARP();
371 arp.setOpCode(ARP.OP_REPLY);
372 arp.setProtocolType(ARP.PROTO_TYPE_IP);
373 arp.setHardwareType(ARP.HW_TYPE_ETHERNET);
alshabib6eb438a2014-10-01 16:39:37 -0700374
Pavlin Radoslavov52307e62014-10-29 15:07:37 -0700375 arp.setProtocolAddressLength((byte) IpAddress.INET_BYTE_LENGTH);
alshabibb5522ff2014-09-29 19:20:00 -0700376 arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH);
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800377 arp.setSenderHardwareAddress(srcMac.toBytes());
alshabibb5522ff2014-09-29 19:20:00 -0700378 arp.setTargetHardwareAddress(request.getSourceMACAddress());
379
380 arp.setTargetProtocolAddress(((ARP) request.getPayload())
381 .getSenderProtocolAddress());
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700382 arp.setSenderProtocolAddress(srcIp.toInt());
alshabibb5522ff2014-09-29 19:20:00 -0700383 eth.setPayload(arp);
384 return eth;
385 }
386
387 public class InternalLinkListener implements LinkListener {
388
389 @Override
390 public void event(LinkEvent event) {
391 Link link = event.subject();
392 Device src = deviceService.getDevice(link.src().deviceId());
393 Device dst = deviceService.getDevice(link.dst().deviceId());
394 switch (event.type()) {
395 case LINK_ADDED:
396 synchronized (externalPorts) {
397 externalPorts.remove(src, link.src().port());
398 externalPorts.remove(dst, link.dst().port());
399 internalPorts.put(src, link.src().port());
400 internalPorts.put(dst, link.dst().port());
401 }
402
403 break;
404 case LINK_REMOVED:
405 synchronized (externalPorts) {
406 externalPorts.put(src, link.src().port());
407 externalPorts.put(dst, link.dst().port());
408 internalPorts.remove(src, link.src().port());
409 internalPorts.remove(dst, link.dst().port());
410 }
411
412 break;
413 case LINK_UPDATED:
414 // don't care about links being updated.
415 break;
416 default:
417 break;
418 }
419
420 }
421
422 }
423
424 public class InternalDeviceListener implements DeviceListener {
425
426 @Override
427 public void event(DeviceEvent event) {
428 Device device = event.subject();
429 switch (event.type()) {
430 case DEVICE_ADDED:
431 case DEVICE_AVAILABILITY_CHANGED:
432 case DEVICE_MASTERSHIP_CHANGED:
433 case DEVICE_SUSPENDED:
434 case DEVICE_UPDATED:
alshabibb5522ff2014-09-29 19:20:00 -0700435 // nothing to do in these cases; handled when links get reported
436 break;
437 case DEVICE_REMOVED:
438 synchronized (externalPorts) {
439 externalPorts.removeAll(device);
440 internalPorts.removeAll(device);
441 }
442 break;
443 case PORT_ADDED:
alshabib6eb438a2014-10-01 16:39:37 -0700444 case PORT_UPDATED:
alshabibb5522ff2014-09-29 19:20:00 -0700445 synchronized (externalPorts) {
alshabib6eb438a2014-10-01 16:39:37 -0700446 if (event.port().isEnabled()) {
447 externalPorts.put(device, event.port().number());
448 internalPorts.remove(device, event.port().number());
449 }
alshabibb5522ff2014-09-29 19:20:00 -0700450 }
451 break;
452 case PORT_REMOVED:
453 synchronized (externalPorts) {
454 externalPorts.remove(device, event.port().number());
455 internalPorts.remove(device, event.port().number());
456 }
457 break;
458 default:
459 break;
460
461 }
462
463 }
464
alshabibc274c902014-10-03 14:58:27 -0700465 }
alshabibb5522ff2014-09-29 19:20:00 -0700466
467}