blob: 16e37985aaf4d16f8346f6c5197a9ea634d071b1 [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());
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700137 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 =
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700153 IpAddress.valueOf(IpAddress.Version.INET,
154 arp.getSenderProtocolAddress());
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700155 PortAddresses sourceAddresses = findPortInSubnet(source);
156 if (sourceAddresses != null) {
157 for (InterfaceIpAddress ia : sourceAddresses.ipAddresses()) {
158 if (ia.ipAddress().equals(source)) {
159 sendTo(eth, sourceAddresses.connectPoint());
160 return;
161 }
162 }
163 }
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700164 }
165
166 // Continue with normal proxy ARP case
alshabibb5522ff2014-09-29 19:20:00 -0700167
168 VlanId vlan = VlanId.vlanId(eth.getVlanID());
Pavlin Radoslavovd0e32d72014-10-31 18:11:43 -0700169 Set<Host> hosts =
170 hostService.getHostsByIp(IpAddress.valueOf(IpAddress.Version.INET,
171 arp.getTargetProtocolAddress()));
alshabibb5522ff2014-09-29 19:20:00 -0700172
173 Host dst = null;
174 Host src = hostService.getHost(HostId.hostId(eth.getSourceMAC(),
175 VlanId.vlanId(eth.getVlanID())));
176
177 for (Host host : hosts) {
178 if (host.vlan().equals(vlan)) {
179 dst = host;
180 break;
181 }
182 }
183
184 if (src == null || dst == null) {
185 flood(eth);
186 return;
187 }
188
Jonathan Hart7d1ad602014-10-17 11:48:32 -0700189 // TODO find the correct IP address
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700190 IpAddress ipAddress = dst.ipAddresses().iterator().next();
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700191 Ethernet arpReply = buildArpReply(ipAddress, dst.mac(), eth);
alshabibb5522ff2014-09-29 19:20:00 -0700192 // TODO: check send status with host service.
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700193 sendTo(arpReply, src.location());
194 }
195
196 /**
197 * Outputs the given packet out the given port.
198 *
199 * @param packet the packet to send
200 * @param outPort the port to send it out
201 */
202 private void sendTo(Ethernet packet, ConnectPoint outPort) {
203 if (internalPorts.containsEntry(
204 deviceService.getDevice(outPort.deviceId()), outPort.port())) {
205 // Sanity check to make sure we don't send the packet out an
206 // internal port and create a loop (could happen due to
207 // misconfiguration).
208 return;
209 }
210
tom9a693fd2014-10-03 11:32:19 -0700211 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700212 builder.setOutput(outPort.port());
213 packetService.emit(new DefaultOutboundPacket(outPort.deviceId(),
214 builder.build(), ByteBuffer.wrap(packet.serialize())));
215 }
216
217 /**
218 * Finds the port with an address in the subnet of the target address, if
219 * one exists.
220 *
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700221 * @param target the target address to find a matching port for
222 * @return a PortAddresses object if one was found, otherwise null
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700223 */
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700224 private PortAddresses findPortInSubnet(IpAddress target) {
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700225 for (PortAddresses addresses : hostService.getAddressBindings()) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700226 for (InterfaceIpAddress ia : addresses.ipAddresses()) {
227 if (ia.subnetAddress().contains(target)) {
228 return addresses;
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700229 }
230 }
231 }
232 return null;
233 }
234
235 /**
236 * Returns whether the given port is an outside-facing port with an IP
237 * address configured.
238 *
239 * @param port the port to check
240 * @return true if the port is an outside-facing port, otherwise false
241 */
242 private boolean isOutsidePort(ConnectPoint port) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700243 //
244 // TODO: Is this sufficient to identify outside-facing ports: just
245 // having IP addresses on a port?
246 //
247 return !hostService.getAddressBindingsForPort(port).ipAddresses().isEmpty();
alshabibb5522ff2014-09-29 19:20:00 -0700248 }
249
250 @Override
251 public void forward(Ethernet eth) {
Yuta HIGUCHI59718042014-10-04 22:04:56 -0700252 checkNotNull(eth, REQUEST_NULL);
alshabibb5522ff2014-09-29 19:20:00 -0700253 checkArgument(eth.getEtherType() == Ethernet.TYPE_ARP,
254 REQUEST_NOT_ARP);
255 ARP arp = (ARP) eth.getPayload();
Jonathan Hart704ca142014-10-09 09:34:39 -0700256 checkArgument(arp.getOpCode() == ARP.OP_REPLY, NOT_ARP_REPLY);
alshabibb5522ff2014-09-29 19:20:00 -0700257
258 Host h = hostService.getHost(HostId.hostId(eth.getDestinationMAC(),
259 VlanId.vlanId(eth.getVlanID())));
260
261 if (h == null) {
262 flood(eth);
263 } else {
tom9a693fd2014-10-03 11:32:19 -0700264 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabibb5522ff2014-09-29 19:20:00 -0700265 builder.setOutput(h.location().port());
266 packetService.emit(new DefaultOutboundPacket(h.location().deviceId(),
267 builder.build(), ByteBuffer.wrap(eth.serialize())));
268 }
269
270 }
271
alshabibc274c902014-10-03 14:58:27 -0700272 @Override
273 public boolean handleArp(PacketContext context) {
274 InboundPacket pkt = context.inPacket();
275 Ethernet ethPkt = pkt.parsed();
276 if (ethPkt.getEtherType() == Ethernet.TYPE_ARP) {
277 ARP arp = (ARP) ethPkt.getPayload();
278 if (arp.getOpCode() == ARP.OP_REPLY) {
279 forward(ethPkt);
280 } else if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700281 reply(ethPkt, context.inPacket().receivedFrom());
alshabibc274c902014-10-03 14:58:27 -0700282 }
283 context.block();
284 return true;
285 }
286 return false;
287 }
288
alshabibb5522ff2014-09-29 19:20:00 -0700289 /**
290 * Flood the arp request at all edges in the network.
291 * @param request the arp request.
292 */
293 private void flood(Ethernet request) {
294 TrafficTreatment.Builder builder = null;
295 ByteBuffer buf = ByteBuffer.wrap(request.serialize());
296
297 synchronized (externalPorts) {
298 for (Entry<Device, PortNumber> entry : externalPorts.entries()) {
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700299 ConnectPoint cp = new ConnectPoint(entry.getKey().id(), entry.getValue());
300 if (isOutsidePort(cp)) {
301 continue;
302 }
303
tom9a693fd2014-10-03 11:32:19 -0700304 builder = DefaultTrafficTreatment.builder();
alshabibb5522ff2014-09-29 19:20:00 -0700305 builder.setOutput(entry.getValue());
306 packetService.emit(new DefaultOutboundPacket(entry.getKey().id(),
307 builder.build(), buf));
308 }
alshabibb5522ff2014-09-29 19:20:00 -0700309 }
310 }
311
312 /**
313 * Determines the location of all known ports in the system.
314 */
315 private void determinePortLocations() {
316 Iterable<Device> devices = deviceService.getDevices();
317 Iterable<Link> links = null;
318 List<PortNumber> ports = null;
319 for (Device d : devices) {
320 ports = buildPortNumberList(deviceService.getPorts(d.id()));
321 links = linkService.getLinks();
322 for (Link l : links) {
323 // for each link, mark the concerned ports as internal
324 // and the remaining ports are therefore external.
Yuta HIGUCHI3541bf22014-10-04 22:06:19 -0700325 if (l.src().deviceId().equals(d.id())
alshabibb5522ff2014-09-29 19:20:00 -0700326 && ports.contains(l.src().port())) {
327 ports.remove(l.src().port());
328 internalPorts.put(d, l.src().port());
329 }
Yuta HIGUCHI3541bf22014-10-04 22:06:19 -0700330 if (l.dst().deviceId().equals(d.id())
alshabibb5522ff2014-09-29 19:20:00 -0700331 && ports.contains(l.dst().port())) {
332 ports.remove(l.dst().port());
333 internalPorts.put(d, l.dst().port());
334 }
335 }
336 synchronized (externalPorts) {
337 externalPorts.putAll(d, ports);
338 }
339 }
340
341 }
342
343 private List<PortNumber> buildPortNumberList(List<Port> ports) {
344 List<PortNumber> portNumbers = Lists.newLinkedList();
345 for (Port p : ports) {
346 portNumbers.add(p.number());
347 }
348 return portNumbers;
349 }
350
351 /**
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700352 * Builds an ARP reply based on a request.
353 *
354 * @param srcIp the IP address to use as the reply source
355 * @param srcMac the MAC address to use as the reply source
356 * @param request the ARP request we got
357 * @return an Ethernet frame containing the ARP reply
alshabibb5522ff2014-09-29 19:20:00 -0700358 */
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700359 private Ethernet buildArpReply(IpAddress srcIp, MacAddress srcMac,
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700360 Ethernet request) {
361
alshabibb5522ff2014-09-29 19:20:00 -0700362 Ethernet eth = new Ethernet();
363 eth.setDestinationMACAddress(request.getSourceMACAddress());
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700364 eth.setSourceMACAddress(srcMac.getAddress());
alshabibb5522ff2014-09-29 19:20:00 -0700365 eth.setEtherType(Ethernet.TYPE_ARP);
366 eth.setVlanID(request.getVlanID());
367
368 ARP arp = new ARP();
369 arp.setOpCode(ARP.OP_REPLY);
370 arp.setProtocolType(ARP.PROTO_TYPE_IP);
371 arp.setHardwareType(ARP.HW_TYPE_ETHERNET);
alshabib6eb438a2014-10-01 16:39:37 -0700372
Pavlin Radoslavov52307e62014-10-29 15:07:37 -0700373 arp.setProtocolAddressLength((byte) IpAddress.INET_BYTE_LENGTH);
alshabibb5522ff2014-09-29 19:20:00 -0700374 arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH);
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700375 arp.setSenderHardwareAddress(srcMac.getAddress());
alshabibb5522ff2014-09-29 19:20:00 -0700376 arp.setTargetHardwareAddress(request.getSourceMACAddress());
377
378 arp.setTargetProtocolAddress(((ARP) request.getPayload())
379 .getSenderProtocolAddress());
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700380 arp.setSenderProtocolAddress(srcIp.toInt());
alshabibb5522ff2014-09-29 19:20:00 -0700381 eth.setPayload(arp);
382 return eth;
383 }
384
385 public class InternalLinkListener implements LinkListener {
386
387 @Override
388 public void event(LinkEvent event) {
389 Link link = event.subject();
390 Device src = deviceService.getDevice(link.src().deviceId());
391 Device dst = deviceService.getDevice(link.dst().deviceId());
392 switch (event.type()) {
393 case LINK_ADDED:
394 synchronized (externalPorts) {
395 externalPorts.remove(src, link.src().port());
396 externalPorts.remove(dst, link.dst().port());
397 internalPorts.put(src, link.src().port());
398 internalPorts.put(dst, link.dst().port());
399 }
400
401 break;
402 case LINK_REMOVED:
403 synchronized (externalPorts) {
404 externalPorts.put(src, link.src().port());
405 externalPorts.put(dst, link.dst().port());
406 internalPorts.remove(src, link.src().port());
407 internalPorts.remove(dst, link.dst().port());
408 }
409
410 break;
411 case LINK_UPDATED:
412 // don't care about links being updated.
413 break;
414 default:
415 break;
416 }
417
418 }
419
420 }
421
422 public class InternalDeviceListener implements DeviceListener {
423
424 @Override
425 public void event(DeviceEvent event) {
426 Device device = event.subject();
427 switch (event.type()) {
428 case DEVICE_ADDED:
429 case DEVICE_AVAILABILITY_CHANGED:
430 case DEVICE_MASTERSHIP_CHANGED:
431 case DEVICE_SUSPENDED:
432 case DEVICE_UPDATED:
alshabibb5522ff2014-09-29 19:20:00 -0700433 // nothing to do in these cases; handled when links get reported
434 break;
435 case DEVICE_REMOVED:
436 synchronized (externalPorts) {
437 externalPorts.removeAll(device);
438 internalPorts.removeAll(device);
439 }
440 break;
441 case PORT_ADDED:
alshabib6eb438a2014-10-01 16:39:37 -0700442 case PORT_UPDATED:
alshabibb5522ff2014-09-29 19:20:00 -0700443 synchronized (externalPorts) {
alshabib6eb438a2014-10-01 16:39:37 -0700444 if (event.port().isEnabled()) {
445 externalPorts.put(device, event.port().number());
446 internalPorts.remove(device, event.port().number());
447 }
alshabibb5522ff2014-09-29 19:20:00 -0700448 }
449 break;
450 case PORT_REMOVED:
451 synchronized (externalPorts) {
452 externalPorts.remove(device, event.port().number());
453 internalPorts.remove(device, event.port().number());
454 }
455 break;
456 default:
457 break;
458
459 }
460
461 }
462
alshabibc274c902014-10-03 14:58:27 -0700463 }
alshabibb5522ff2014-09-29 19:20:00 -0700464
465}