blob: 953e88a92be6d7c13a135af3e1818856567ce105 [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 =
135 IpAddress.valueOf(arp.getTargetProtocolAddress());
136 PortAddresses addresses =
137 hostService.getAddressBindingsForPort(inPort);
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700138
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700139 for (InterfaceIpAddress ia : addresses.ipAddresses()) {
140 if (ia.ipAddress().equals(target)) {
141 Ethernet arpReply =
142 buildArpReply(ia.ipAddress(), addresses.mac(), eth);
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700143 sendTo(arpReply, inPort);
144 }
145 }
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700146 return;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700147 } else {
148 // If the source address matches one of our external addresses
149 // it could be a request from an internal host to an external
150 // address. Forward it over to the correct port.
151 IpAddress source =
152 IpAddress.valueOf(arp.getSenderProtocolAddress());
153 PortAddresses sourceAddresses = findPortInSubnet(source);
154 if (sourceAddresses != null) {
155 for (InterfaceIpAddress ia : sourceAddresses.ipAddresses()) {
156 if (ia.ipAddress().equals(source)) {
157 sendTo(eth, sourceAddresses.connectPoint());
158 return;
159 }
160 }
161 }
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700162 }
163
164 // Continue with normal proxy ARP case
alshabibb5522ff2014-09-29 19:20:00 -0700165
166 VlanId vlan = VlanId.vlanId(eth.getVlanID());
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700167 Set<Host> hosts = hostService.getHostsByIp(IpAddress.valueOf(arp
alshabibb5522ff2014-09-29 19:20:00 -0700168 .getTargetProtocolAddress()));
169
170 Host dst = null;
171 Host src = hostService.getHost(HostId.hostId(eth.getSourceMAC(),
172 VlanId.vlanId(eth.getVlanID())));
173
174 for (Host host : hosts) {
175 if (host.vlan().equals(vlan)) {
176 dst = host;
177 break;
178 }
179 }
180
181 if (src == null || dst == null) {
182 flood(eth);
183 return;
184 }
185
Jonathan Hart7d1ad602014-10-17 11:48:32 -0700186 // TODO find the correct IP address
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700187 IpAddress ipAddress = dst.ipAddresses().iterator().next();
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700188 Ethernet arpReply = buildArpReply(ipAddress, dst.mac(), eth);
alshabibb5522ff2014-09-29 19:20:00 -0700189 // TODO: check send status with host service.
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700190 sendTo(arpReply, src.location());
191 }
192
193 /**
194 * Outputs the given packet out the given port.
195 *
196 * @param packet the packet to send
197 * @param outPort the port to send it out
198 */
199 private void sendTo(Ethernet packet, ConnectPoint outPort) {
200 if (internalPorts.containsEntry(
201 deviceService.getDevice(outPort.deviceId()), outPort.port())) {
202 // Sanity check to make sure we don't send the packet out an
203 // internal port and create a loop (could happen due to
204 // misconfiguration).
205 return;
206 }
207
tom9a693fd2014-10-03 11:32:19 -0700208 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700209 builder.setOutput(outPort.port());
210 packetService.emit(new DefaultOutboundPacket(outPort.deviceId(),
211 builder.build(), ByteBuffer.wrap(packet.serialize())));
212 }
213
214 /**
215 * Finds the port with an address in the subnet of the target address, if
216 * one exists.
217 *
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700218 * @param target the target address to find a matching port for
219 * @return a PortAddresses object if one was found, otherwise null
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700220 */
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700221 private PortAddresses findPortInSubnet(IpAddress target) {
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700222 for (PortAddresses addresses : hostService.getAddressBindings()) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700223 for (InterfaceIpAddress ia : addresses.ipAddresses()) {
224 if (ia.subnetAddress().contains(target)) {
225 return addresses;
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700226 }
227 }
228 }
229 return null;
230 }
231
232 /**
233 * Returns whether the given port is an outside-facing port with an IP
234 * address configured.
235 *
236 * @param port the port to check
237 * @return true if the port is an outside-facing port, otherwise false
238 */
239 private boolean isOutsidePort(ConnectPoint port) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700240 //
241 // TODO: Is this sufficient to identify outside-facing ports: just
242 // having IP addresses on a port?
243 //
244 return !hostService.getAddressBindingsForPort(port).ipAddresses().isEmpty();
alshabibb5522ff2014-09-29 19:20:00 -0700245 }
246
247 @Override
248 public void forward(Ethernet eth) {
Yuta HIGUCHI59718042014-10-04 22:04:56 -0700249 checkNotNull(eth, REQUEST_NULL);
alshabibb5522ff2014-09-29 19:20:00 -0700250 checkArgument(eth.getEtherType() == Ethernet.TYPE_ARP,
251 REQUEST_NOT_ARP);
252 ARP arp = (ARP) eth.getPayload();
Jonathan Hart704ca142014-10-09 09:34:39 -0700253 checkArgument(arp.getOpCode() == ARP.OP_REPLY, NOT_ARP_REPLY);
alshabibb5522ff2014-09-29 19:20:00 -0700254
255 Host h = hostService.getHost(HostId.hostId(eth.getDestinationMAC(),
256 VlanId.vlanId(eth.getVlanID())));
257
258 if (h == null) {
259 flood(eth);
260 } else {
tom9a693fd2014-10-03 11:32:19 -0700261 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabibb5522ff2014-09-29 19:20:00 -0700262 builder.setOutput(h.location().port());
263 packetService.emit(new DefaultOutboundPacket(h.location().deviceId(),
264 builder.build(), ByteBuffer.wrap(eth.serialize())));
265 }
266
267 }
268
alshabibc274c902014-10-03 14:58:27 -0700269 @Override
270 public boolean handleArp(PacketContext context) {
271 InboundPacket pkt = context.inPacket();
272 Ethernet ethPkt = pkt.parsed();
273 if (ethPkt.getEtherType() == Ethernet.TYPE_ARP) {
274 ARP arp = (ARP) ethPkt.getPayload();
275 if (arp.getOpCode() == ARP.OP_REPLY) {
276 forward(ethPkt);
277 } else if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700278 reply(ethPkt, context.inPacket().receivedFrom());
alshabibc274c902014-10-03 14:58:27 -0700279 }
280 context.block();
281 return true;
282 }
283 return false;
284 }
285
alshabibb5522ff2014-09-29 19:20:00 -0700286 /**
287 * Flood the arp request at all edges in the network.
288 * @param request the arp request.
289 */
290 private void flood(Ethernet request) {
291 TrafficTreatment.Builder builder = null;
292 ByteBuffer buf = ByteBuffer.wrap(request.serialize());
293
294 synchronized (externalPorts) {
295 for (Entry<Device, PortNumber> entry : externalPorts.entries()) {
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700296 ConnectPoint cp = new ConnectPoint(entry.getKey().id(), entry.getValue());
297 if (isOutsidePort(cp)) {
298 continue;
299 }
300
tom9a693fd2014-10-03 11:32:19 -0700301 builder = DefaultTrafficTreatment.builder();
alshabibb5522ff2014-09-29 19:20:00 -0700302 builder.setOutput(entry.getValue());
303 packetService.emit(new DefaultOutboundPacket(entry.getKey().id(),
304 builder.build(), buf));
305 }
alshabibb5522ff2014-09-29 19:20:00 -0700306 }
307 }
308
309 /**
310 * Determines the location of all known ports in the system.
311 */
312 private void determinePortLocations() {
313 Iterable<Device> devices = deviceService.getDevices();
314 Iterable<Link> links = null;
315 List<PortNumber> ports = null;
316 for (Device d : devices) {
317 ports = buildPortNumberList(deviceService.getPorts(d.id()));
318 links = linkService.getLinks();
319 for (Link l : links) {
320 // for each link, mark the concerned ports as internal
321 // and the remaining ports are therefore external.
Yuta HIGUCHI3541bf22014-10-04 22:06:19 -0700322 if (l.src().deviceId().equals(d.id())
alshabibb5522ff2014-09-29 19:20:00 -0700323 && ports.contains(l.src().port())) {
324 ports.remove(l.src().port());
325 internalPorts.put(d, l.src().port());
326 }
Yuta HIGUCHI3541bf22014-10-04 22:06:19 -0700327 if (l.dst().deviceId().equals(d.id())
alshabibb5522ff2014-09-29 19:20:00 -0700328 && ports.contains(l.dst().port())) {
329 ports.remove(l.dst().port());
330 internalPorts.put(d, l.dst().port());
331 }
332 }
333 synchronized (externalPorts) {
334 externalPorts.putAll(d, ports);
335 }
336 }
337
338 }
339
340 private List<PortNumber> buildPortNumberList(List<Port> ports) {
341 List<PortNumber> portNumbers = Lists.newLinkedList();
342 for (Port p : ports) {
343 portNumbers.add(p.number());
344 }
345 return portNumbers;
346 }
347
348 /**
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700349 * Builds an ARP reply based on a request.
350 *
351 * @param srcIp the IP address to use as the reply source
352 * @param srcMac the MAC address to use as the reply source
353 * @param request the ARP request we got
354 * @return an Ethernet frame containing the ARP reply
alshabibb5522ff2014-09-29 19:20:00 -0700355 */
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700356 private Ethernet buildArpReply(IpAddress srcIp, MacAddress srcMac,
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700357 Ethernet request) {
358
alshabibb5522ff2014-09-29 19:20:00 -0700359 Ethernet eth = new Ethernet();
360 eth.setDestinationMACAddress(request.getSourceMACAddress());
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700361 eth.setSourceMACAddress(srcMac.getAddress());
alshabibb5522ff2014-09-29 19:20:00 -0700362 eth.setEtherType(Ethernet.TYPE_ARP);
363 eth.setVlanID(request.getVlanID());
364
365 ARP arp = new ARP();
366 arp.setOpCode(ARP.OP_REPLY);
367 arp.setProtocolType(ARP.PROTO_TYPE_IP);
368 arp.setHardwareType(ARP.HW_TYPE_ETHERNET);
alshabib6eb438a2014-10-01 16:39:37 -0700369
Pavlin Radoslavov52307e62014-10-29 15:07:37 -0700370 arp.setProtocolAddressLength((byte) IpAddress.INET_BYTE_LENGTH);
alshabibb5522ff2014-09-29 19:20:00 -0700371 arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH);
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700372 arp.setSenderHardwareAddress(srcMac.getAddress());
alshabibb5522ff2014-09-29 19:20:00 -0700373 arp.setTargetHardwareAddress(request.getSourceMACAddress());
374
375 arp.setTargetProtocolAddress(((ARP) request.getPayload())
376 .getSenderProtocolAddress());
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700377 arp.setSenderProtocolAddress(srcIp.toInt());
alshabibb5522ff2014-09-29 19:20:00 -0700378 eth.setPayload(arp);
379 return eth;
380 }
381
382 public class InternalLinkListener implements LinkListener {
383
384 @Override
385 public void event(LinkEvent event) {
386 Link link = event.subject();
387 Device src = deviceService.getDevice(link.src().deviceId());
388 Device dst = deviceService.getDevice(link.dst().deviceId());
389 switch (event.type()) {
390 case LINK_ADDED:
391 synchronized (externalPorts) {
392 externalPorts.remove(src, link.src().port());
393 externalPorts.remove(dst, link.dst().port());
394 internalPorts.put(src, link.src().port());
395 internalPorts.put(dst, link.dst().port());
396 }
397
398 break;
399 case LINK_REMOVED:
400 synchronized (externalPorts) {
401 externalPorts.put(src, link.src().port());
402 externalPorts.put(dst, link.dst().port());
403 internalPorts.remove(src, link.src().port());
404 internalPorts.remove(dst, link.dst().port());
405 }
406
407 break;
408 case LINK_UPDATED:
409 // don't care about links being updated.
410 break;
411 default:
412 break;
413 }
414
415 }
416
417 }
418
419 public class InternalDeviceListener implements DeviceListener {
420
421 @Override
422 public void event(DeviceEvent event) {
423 Device device = event.subject();
424 switch (event.type()) {
425 case DEVICE_ADDED:
426 case DEVICE_AVAILABILITY_CHANGED:
427 case DEVICE_MASTERSHIP_CHANGED:
428 case DEVICE_SUSPENDED:
429 case DEVICE_UPDATED:
alshabibb5522ff2014-09-29 19:20:00 -0700430 // nothing to do in these cases; handled when links get reported
431 break;
432 case DEVICE_REMOVED:
433 synchronized (externalPorts) {
434 externalPorts.removeAll(device);
435 internalPorts.removeAll(device);
436 }
437 break;
438 case PORT_ADDED:
alshabib6eb438a2014-10-01 16:39:37 -0700439 case PORT_UPDATED:
alshabibb5522ff2014-09-29 19:20:00 -0700440 synchronized (externalPorts) {
alshabib6eb438a2014-10-01 16:39:37 -0700441 if (event.port().isEnabled()) {
442 externalPorts.put(device, event.port().number());
443 internalPorts.remove(device, event.port().number());
444 }
alshabibb5522ff2014-09-29 19:20:00 -0700445 }
446 break;
447 case PORT_REMOVED:
448 synchronized (externalPorts) {
449 externalPorts.remove(device, event.port().number());
450 internalPorts.remove(device, event.port().number());
451 }
452 break;
453 default:
454 break;
455
456 }
457
458 }
459
alshabibc274c902014-10-03 14:58:27 -0700460 }
alshabibb5522ff2014-09-29 19:20:00 -0700461
462}