blob: 47e0cffd76c0a38e6ffa7348e426bbed60a96076 [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;
alshabibb5522ff2014-09-29 19:20:00 -070059import org.onlab.packet.IpPrefix;
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -070060import org.onlab.packet.MacAddress;
alshabibb5522ff2014-09-29 19:20:00 -070061import org.onlab.packet.VlanId;
62import org.slf4j.Logger;
63
64import com.google.common.collect.HashMultimap;
65import com.google.common.collect.Lists;
66import com.google.common.collect.Multimap;
67
alshabibb5522ff2014-09-29 19:20:00 -070068@Component(immediate = true)
69@Service
70public class ProxyArpManager implements ProxyArpService {
71
72 private final Logger log = getLogger(getClass());
73
74 private static final String MAC_ADDR_NULL = "Mac address cannot be null.";
75 private static final String REQUEST_NULL = "Arp request cannot be null.";
76 private static final String REQUEST_NOT_ARP = "Ethernet frame does not contain ARP request.";
77 private static final String NOT_ARP_REQUEST = "ARP is not a request.";
Jonathan Hart704ca142014-10-09 09:34:39 -070078 private static final String NOT_ARP_REPLY = "ARP is not a reply.";
alshabibb5522ff2014-09-29 19:20:00 -070079
80 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
81 protected HostService hostService;
82
83 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected PacketService packetService;
85
86 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
87 protected LinkService linkService;
88
89 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
90 protected DeviceService deviceService;
91
92 private final Multimap<Device, PortNumber> internalPorts =
93 HashMultimap.<Device, PortNumber>create();
94
95 private final Multimap<Device, PortNumber> externalPorts =
96 HashMultimap.<Device, PortNumber>create();
97
98 /**
99 * Listens to both device service and link service to determine
100 * whether a port is internal or external.
101 */
102 @Activate
103 public void activate() {
104 deviceService.addListener(new InternalDeviceListener());
105 linkService.addListener(new InternalLinkListener());
106 determinePortLocations();
107 log.info("Started");
108 }
109
110
111 @Deactivate
112 public void deactivate() {
113 log.info("Stopped");
114 }
115
116 @Override
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700117 public boolean known(IpAddress addr) {
Yuta HIGUCHI59718042014-10-04 22:04:56 -0700118 checkNotNull(addr, MAC_ADDR_NULL);
alshabibb5522ff2014-09-29 19:20:00 -0700119 Set<Host> hosts = hostService.getHostsByIp(addr);
120 return !hosts.isEmpty();
121 }
122
123 @Override
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700124 public void reply(Ethernet eth, ConnectPoint inPort) {
Yuta HIGUCHI59718042014-10-04 22:04:56 -0700125 checkNotNull(eth, REQUEST_NULL);
alshabibb5522ff2014-09-29 19:20:00 -0700126 checkArgument(eth.getEtherType() == Ethernet.TYPE_ARP,
127 REQUEST_NOT_ARP);
128 ARP arp = (ARP) eth.getPayload();
129 checkArgument(arp.getOpCode() == ARP.OP_REQUEST, NOT_ARP_REQUEST);
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700130 checkNotNull(inPort);
131
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700132 // If the request came from outside the network, only reply if it was
133 // for one of our external addresses.
134 if (isOutsidePort(inPort)) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700135 IpAddress target =
136 IpAddress.valueOf(arp.getTargetProtocolAddress());
137 PortAddresses addresses =
138 hostService.getAddressBindingsForPort(inPort);
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700139
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700140 for (InterfaceIpAddress ia : addresses.ipAddresses()) {
141 if (ia.ipAddress().equals(target)) {
142 Ethernet arpReply =
143 buildArpReply(ia.ipAddress(), addresses.mac(), eth);
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700144 sendTo(arpReply, inPort);
145 }
146 }
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700147 return;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700148 } else {
149 // If the source address matches one of our external addresses
150 // it could be a request from an internal host to an external
151 // address. Forward it over to the correct port.
152 IpAddress source =
153 IpAddress.valueOf(arp.getSenderProtocolAddress());
154 PortAddresses sourceAddresses = findPortInSubnet(source);
155 if (sourceAddresses != null) {
156 for (InterfaceIpAddress ia : sourceAddresses.ipAddresses()) {
157 if (ia.ipAddress().equals(source)) {
158 sendTo(eth, sourceAddresses.connectPoint());
159 return;
160 }
161 }
162 }
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700163 }
164
165 // Continue with normal proxy ARP case
alshabibb5522ff2014-09-29 19:20:00 -0700166
167 VlanId vlan = VlanId.vlanId(eth.getVlanID());
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700168 Set<Host> hosts = hostService.getHostsByIp(IpAddress.valueOf(arp
alshabibb5522ff2014-09-29 19:20:00 -0700169 .getTargetProtocolAddress()));
170
171 Host dst = null;
172 Host src = hostService.getHost(HostId.hostId(eth.getSourceMAC(),
173 VlanId.vlanId(eth.getVlanID())));
174
175 for (Host host : hosts) {
176 if (host.vlan().equals(vlan)) {
177 dst = host;
178 break;
179 }
180 }
181
182 if (src == null || dst == null) {
183 flood(eth);
184 return;
185 }
186
Jonathan Hart7d1ad602014-10-17 11:48:32 -0700187 // TODO find the correct IP address
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700188 IpAddress ipAddress = dst.ipAddresses().iterator().next();
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700189 Ethernet arpReply = buildArpReply(ipAddress, dst.mac(), eth);
alshabibb5522ff2014-09-29 19:20:00 -0700190 // TODO: check send status with host service.
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700191 sendTo(arpReply, src.location());
192 }
193
194 /**
195 * Outputs the given packet out the given port.
196 *
197 * @param packet the packet to send
198 * @param outPort the port to send it out
199 */
200 private void sendTo(Ethernet packet, ConnectPoint outPort) {
201 if (internalPorts.containsEntry(
202 deviceService.getDevice(outPort.deviceId()), outPort.port())) {
203 // Sanity check to make sure we don't send the packet out an
204 // internal port and create a loop (could happen due to
205 // misconfiguration).
206 return;
207 }
208
tom9a693fd2014-10-03 11:32:19 -0700209 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700210 builder.setOutput(outPort.port());
211 packetService.emit(new DefaultOutboundPacket(outPort.deviceId(),
212 builder.build(), ByteBuffer.wrap(packet.serialize())));
213 }
214
215 /**
216 * Finds the port with an address in the subnet of the target address, if
217 * one exists.
218 *
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700219 * @param target the target address to find a matching port for
220 * @return a PortAddresses object if one was found, otherwise null
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700221 */
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700222 private PortAddresses findPortInSubnet(IpAddress target) {
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700223 for (PortAddresses addresses : hostService.getAddressBindings()) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700224 for (InterfaceIpAddress ia : addresses.ipAddresses()) {
225 if (ia.subnetAddress().contains(target)) {
226 return addresses;
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700227 }
228 }
229 }
230 return null;
231 }
232
233 /**
234 * Returns whether the given port is an outside-facing port with an IP
235 * address configured.
236 *
237 * @param port the port to check
238 * @return true if the port is an outside-facing port, otherwise false
239 */
240 private boolean isOutsidePort(ConnectPoint port) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700241 //
242 // TODO: Is this sufficient to identify outside-facing ports: just
243 // having IP addresses on a port?
244 //
245 return !hostService.getAddressBindingsForPort(port).ipAddresses().isEmpty();
alshabibb5522ff2014-09-29 19:20:00 -0700246 }
247
248 @Override
249 public void forward(Ethernet eth) {
Yuta HIGUCHI59718042014-10-04 22:04:56 -0700250 checkNotNull(eth, REQUEST_NULL);
alshabibb5522ff2014-09-29 19:20:00 -0700251 checkArgument(eth.getEtherType() == Ethernet.TYPE_ARP,
252 REQUEST_NOT_ARP);
253 ARP arp = (ARP) eth.getPayload();
Jonathan Hart704ca142014-10-09 09:34:39 -0700254 checkArgument(arp.getOpCode() == ARP.OP_REPLY, NOT_ARP_REPLY);
alshabibb5522ff2014-09-29 19:20:00 -0700255
256 Host h = hostService.getHost(HostId.hostId(eth.getDestinationMAC(),
257 VlanId.vlanId(eth.getVlanID())));
258
259 if (h == null) {
260 flood(eth);
261 } else {
tom9a693fd2014-10-03 11:32:19 -0700262 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabibb5522ff2014-09-29 19:20:00 -0700263 builder.setOutput(h.location().port());
264 packetService.emit(new DefaultOutboundPacket(h.location().deviceId(),
265 builder.build(), ByteBuffer.wrap(eth.serialize())));
266 }
267
268 }
269
alshabibc274c902014-10-03 14:58:27 -0700270 @Override
271 public boolean handleArp(PacketContext context) {
272 InboundPacket pkt = context.inPacket();
273 Ethernet ethPkt = pkt.parsed();
274 if (ethPkt.getEtherType() == Ethernet.TYPE_ARP) {
275 ARP arp = (ARP) ethPkt.getPayload();
276 if (arp.getOpCode() == ARP.OP_REPLY) {
277 forward(ethPkt);
278 } else if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700279 reply(ethPkt, context.inPacket().receivedFrom());
alshabibc274c902014-10-03 14:58:27 -0700280 }
281 context.block();
282 return true;
283 }
284 return false;
285 }
286
alshabibb5522ff2014-09-29 19:20:00 -0700287 /**
288 * Flood the arp request at all edges in the network.
289 * @param request the arp request.
290 */
291 private void flood(Ethernet request) {
292 TrafficTreatment.Builder builder = null;
293 ByteBuffer buf = ByteBuffer.wrap(request.serialize());
294
295 synchronized (externalPorts) {
296 for (Entry<Device, PortNumber> entry : externalPorts.entries()) {
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700297 ConnectPoint cp = new ConnectPoint(entry.getKey().id(), entry.getValue());
298 if (isOutsidePort(cp)) {
299 continue;
300 }
301
tom9a693fd2014-10-03 11:32:19 -0700302 builder = DefaultTrafficTreatment.builder();
alshabibb5522ff2014-09-29 19:20:00 -0700303 builder.setOutput(entry.getValue());
304 packetService.emit(new DefaultOutboundPacket(entry.getKey().id(),
305 builder.build(), buf));
306 }
alshabibb5522ff2014-09-29 19:20:00 -0700307 }
308 }
309
310 /**
311 * Determines the location of all known ports in the system.
312 */
313 private void determinePortLocations() {
314 Iterable<Device> devices = deviceService.getDevices();
315 Iterable<Link> links = null;
316 List<PortNumber> ports = null;
317 for (Device d : devices) {
318 ports = buildPortNumberList(deviceService.getPorts(d.id()));
319 links = linkService.getLinks();
320 for (Link l : links) {
321 // for each link, mark the concerned ports as internal
322 // and the remaining ports are therefore external.
Yuta HIGUCHI3541bf22014-10-04 22:06:19 -0700323 if (l.src().deviceId().equals(d.id())
alshabibb5522ff2014-09-29 19:20:00 -0700324 && ports.contains(l.src().port())) {
325 ports.remove(l.src().port());
326 internalPorts.put(d, l.src().port());
327 }
Yuta HIGUCHI3541bf22014-10-04 22:06:19 -0700328 if (l.dst().deviceId().equals(d.id())
alshabibb5522ff2014-09-29 19:20:00 -0700329 && ports.contains(l.dst().port())) {
330 ports.remove(l.dst().port());
331 internalPorts.put(d, l.dst().port());
332 }
333 }
334 synchronized (externalPorts) {
335 externalPorts.putAll(d, ports);
336 }
337 }
338
339 }
340
341 private List<PortNumber> buildPortNumberList(List<Port> ports) {
342 List<PortNumber> portNumbers = Lists.newLinkedList();
343 for (Port p : ports) {
344 portNumbers.add(p.number());
345 }
346 return portNumbers;
347 }
348
349 /**
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700350 * Builds an ARP reply based on a request.
351 *
352 * @param srcIp the IP address to use as the reply source
353 * @param srcMac the MAC address to use as the reply source
354 * @param request the ARP request we got
355 * @return an Ethernet frame containing the ARP reply
alshabibb5522ff2014-09-29 19:20:00 -0700356 */
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700357 private Ethernet buildArpReply(IpAddress srcIp, MacAddress srcMac,
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700358 Ethernet request) {
359
alshabibb5522ff2014-09-29 19:20:00 -0700360 Ethernet eth = new Ethernet();
361 eth.setDestinationMACAddress(request.getSourceMACAddress());
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700362 eth.setSourceMACAddress(srcMac.getAddress());
alshabibb5522ff2014-09-29 19:20:00 -0700363 eth.setEtherType(Ethernet.TYPE_ARP);
364 eth.setVlanID(request.getVlanID());
365
366 ARP arp = new ARP();
367 arp.setOpCode(ARP.OP_REPLY);
368 arp.setProtocolType(ARP.PROTO_TYPE_IP);
369 arp.setHardwareType(ARP.HW_TYPE_ETHERNET);
alshabib6eb438a2014-10-01 16:39:37 -0700370
alshabibb5522ff2014-09-29 19:20:00 -0700371 arp.setProtocolAddressLength((byte) IpPrefix.INET_LEN);
372 arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH);
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700373 arp.setSenderHardwareAddress(srcMac.getAddress());
alshabibb5522ff2014-09-29 19:20:00 -0700374 arp.setTargetHardwareAddress(request.getSourceMACAddress());
375
376 arp.setTargetProtocolAddress(((ARP) request.getPayload())
377 .getSenderProtocolAddress());
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700378 arp.setSenderProtocolAddress(srcIp.toInt());
alshabibb5522ff2014-09-29 19:20:00 -0700379 eth.setPayload(arp);
380 return eth;
381 }
382
383 public class InternalLinkListener implements LinkListener {
384
385 @Override
386 public void event(LinkEvent event) {
387 Link link = event.subject();
388 Device src = deviceService.getDevice(link.src().deviceId());
389 Device dst = deviceService.getDevice(link.dst().deviceId());
390 switch (event.type()) {
391 case LINK_ADDED:
392 synchronized (externalPorts) {
393 externalPorts.remove(src, link.src().port());
394 externalPorts.remove(dst, link.dst().port());
395 internalPorts.put(src, link.src().port());
396 internalPorts.put(dst, link.dst().port());
397 }
398
399 break;
400 case LINK_REMOVED:
401 synchronized (externalPorts) {
402 externalPorts.put(src, link.src().port());
403 externalPorts.put(dst, link.dst().port());
404 internalPorts.remove(src, link.src().port());
405 internalPorts.remove(dst, link.dst().port());
406 }
407
408 break;
409 case LINK_UPDATED:
410 // don't care about links being updated.
411 break;
412 default:
413 break;
414 }
415
416 }
417
418 }
419
420 public class InternalDeviceListener implements DeviceListener {
421
422 @Override
423 public void event(DeviceEvent event) {
424 Device device = event.subject();
425 switch (event.type()) {
426 case DEVICE_ADDED:
427 case DEVICE_AVAILABILITY_CHANGED:
428 case DEVICE_MASTERSHIP_CHANGED:
429 case DEVICE_SUSPENDED:
430 case DEVICE_UPDATED:
alshabibb5522ff2014-09-29 19:20:00 -0700431 // nothing to do in these cases; handled when links get reported
432 break;
433 case DEVICE_REMOVED:
434 synchronized (externalPorts) {
435 externalPorts.removeAll(device);
436 internalPorts.removeAll(device);
437 }
438 break;
439 case PORT_ADDED:
alshabib6eb438a2014-10-01 16:39:37 -0700440 case PORT_UPDATED:
alshabibb5522ff2014-09-29 19:20:00 -0700441 synchronized (externalPorts) {
alshabib6eb438a2014-10-01 16:39:37 -0700442 if (event.port().isEnabled()) {
443 externalPorts.put(device, event.port().number());
444 internalPorts.remove(device, event.port().number());
445 }
alshabibb5522ff2014-09-29 19:20:00 -0700446 }
447 break;
448 case PORT_REMOVED:
449 synchronized (externalPorts) {
450 externalPorts.remove(device, event.port().number());
451 internalPorts.remove(device, event.port().number());
452 }
453 break;
454 default:
455 break;
456
457 }
458
459 }
460
alshabibc274c902014-10-03 14:58:27 -0700461 }
alshabibb5522ff2014-09-29 19:20:00 -0700462
463}