blob: 735c1091cd587f22f715d2eeff6f4d8c080d5810 [file] [log] [blame]
alshabibb5522ff2014-09-29 19:20:00 -07001package org.onlab.onos.net.proxyarp.impl;
2
3import static com.google.common.base.Preconditions.checkArgument;
4import static com.google.common.base.Preconditions.checkNotNull;
5import static org.slf4j.LoggerFactory.getLogger;
6
7import java.nio.ByteBuffer;
8import java.util.List;
9import java.util.Map.Entry;
10import java.util.Set;
11
12import org.apache.felix.scr.annotations.Activate;
13import org.apache.felix.scr.annotations.Component;
14import org.apache.felix.scr.annotations.Deactivate;
15import org.apache.felix.scr.annotations.Reference;
16import org.apache.felix.scr.annotations.ReferenceCardinality;
17import org.apache.felix.scr.annotations.Service;
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -070018import org.onlab.onos.net.ConnectPoint;
alshabibb5522ff2014-09-29 19:20:00 -070019import org.onlab.onos.net.Device;
20import org.onlab.onos.net.Host;
21import org.onlab.onos.net.HostId;
22import org.onlab.onos.net.Link;
23import org.onlab.onos.net.Port;
24import org.onlab.onos.net.PortNumber;
25import org.onlab.onos.net.device.DeviceEvent;
26import org.onlab.onos.net.device.DeviceListener;
27import org.onlab.onos.net.device.DeviceService;
28import org.onlab.onos.net.flow.DefaultTrafficTreatment;
29import org.onlab.onos.net.flow.TrafficTreatment;
30import org.onlab.onos.net.host.HostService;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070031import org.onlab.onos.net.host.InterfaceIpAddress;
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -070032import org.onlab.onos.net.host.PortAddresses;
alshabibb5522ff2014-09-29 19:20:00 -070033import org.onlab.onos.net.link.LinkEvent;
34import org.onlab.onos.net.link.LinkListener;
35import org.onlab.onos.net.link.LinkService;
36import org.onlab.onos.net.packet.DefaultOutboundPacket;
alshabibc274c902014-10-03 14:58:27 -070037import org.onlab.onos.net.packet.InboundPacket;
38import org.onlab.onos.net.packet.PacketContext;
alshabibb5522ff2014-09-29 19:20:00 -070039import org.onlab.onos.net.packet.PacketService;
40import org.onlab.onos.net.proxyarp.ProxyArpService;
41import org.onlab.packet.ARP;
42import org.onlab.packet.Ethernet;
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -070043import org.onlab.packet.IpAddress;
alshabibb5522ff2014-09-29 19:20:00 -070044import org.onlab.packet.IpPrefix;
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -070045import org.onlab.packet.MacAddress;
alshabibb5522ff2014-09-29 19:20:00 -070046import org.onlab.packet.VlanId;
47import org.slf4j.Logger;
48
49import com.google.common.collect.HashMultimap;
50import com.google.common.collect.Lists;
51import com.google.common.collect.Multimap;
52
alshabibb5522ff2014-09-29 19:20:00 -070053@Component(immediate = true)
54@Service
55public class ProxyArpManager implements ProxyArpService {
56
57 private final Logger log = getLogger(getClass());
58
59 private static final String MAC_ADDR_NULL = "Mac address cannot be null.";
60 private static final String REQUEST_NULL = "Arp request cannot be null.";
61 private static final String REQUEST_NOT_ARP = "Ethernet frame does not contain ARP request.";
62 private static final String NOT_ARP_REQUEST = "ARP is not a request.";
Jonathan Hart704ca142014-10-09 09:34:39 -070063 private static final String NOT_ARP_REPLY = "ARP is not a reply.";
alshabibb5522ff2014-09-29 19:20:00 -070064
65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected HostService hostService;
67
68 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 protected PacketService packetService;
70
71 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
72 protected LinkService linkService;
73
74 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
75 protected DeviceService deviceService;
76
77 private final Multimap<Device, PortNumber> internalPorts =
78 HashMultimap.<Device, PortNumber>create();
79
80 private final Multimap<Device, PortNumber> externalPorts =
81 HashMultimap.<Device, PortNumber>create();
82
83 /**
84 * Listens to both device service and link service to determine
85 * whether a port is internal or external.
86 */
87 @Activate
88 public void activate() {
89 deviceService.addListener(new InternalDeviceListener());
90 linkService.addListener(new InternalLinkListener());
91 determinePortLocations();
92 log.info("Started");
93 }
94
95
96 @Deactivate
97 public void deactivate() {
98 log.info("Stopped");
99 }
100
101 @Override
102 public boolean known(IpPrefix addr) {
Yuta HIGUCHI59718042014-10-04 22:04:56 -0700103 checkNotNull(addr, MAC_ADDR_NULL);
alshabibb5522ff2014-09-29 19:20:00 -0700104 Set<Host> hosts = hostService.getHostsByIp(addr);
105 return !hosts.isEmpty();
106 }
107
108 @Override
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700109 public void reply(Ethernet eth, ConnectPoint inPort) {
Yuta HIGUCHI59718042014-10-04 22:04:56 -0700110 checkNotNull(eth, REQUEST_NULL);
alshabibb5522ff2014-09-29 19:20:00 -0700111 checkArgument(eth.getEtherType() == Ethernet.TYPE_ARP,
112 REQUEST_NOT_ARP);
113 ARP arp = (ARP) eth.getPayload();
114 checkArgument(arp.getOpCode() == ARP.OP_REQUEST, NOT_ARP_REQUEST);
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700115 checkNotNull(inPort);
116
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700117 // If the request came from outside the network, only reply if it was
118 // for one of our external addresses.
119 if (isOutsidePort(inPort)) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700120 IpAddress target =
121 IpAddress.valueOf(arp.getTargetProtocolAddress());
122 PortAddresses addresses =
123 hostService.getAddressBindingsForPort(inPort);
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700124
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700125 for (InterfaceIpAddress ia : addresses.ipAddresses()) {
126 if (ia.ipAddress().equals(target)) {
127 Ethernet arpReply =
128 buildArpReply(ia.ipAddress(), addresses.mac(), eth);
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700129 sendTo(arpReply, inPort);
130 }
131 }
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700132 return;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700133 } else {
134 // If the source address matches one of our external addresses
135 // it could be a request from an internal host to an external
136 // address. Forward it over to the correct port.
137 IpAddress source =
138 IpAddress.valueOf(arp.getSenderProtocolAddress());
139 PortAddresses sourceAddresses = findPortInSubnet(source);
140 if (sourceAddresses != null) {
141 for (InterfaceIpAddress ia : sourceAddresses.ipAddresses()) {
142 if (ia.ipAddress().equals(source)) {
143 sendTo(eth, sourceAddresses.connectPoint());
144 return;
145 }
146 }
147 }
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700148 }
149
150 // Continue with normal proxy ARP case
alshabibb5522ff2014-09-29 19:20:00 -0700151
152 VlanId vlan = VlanId.vlanId(eth.getVlanID());
153 Set<Host> hosts = hostService.getHostsByIp(IpPrefix.valueOf(arp
154 .getTargetProtocolAddress()));
155
156 Host dst = null;
157 Host src = hostService.getHost(HostId.hostId(eth.getSourceMAC(),
158 VlanId.vlanId(eth.getVlanID())));
159
160 for (Host host : hosts) {
161 if (host.vlan().equals(vlan)) {
162 dst = host;
163 break;
164 }
165 }
166
167 if (src == null || dst == null) {
168 flood(eth);
169 return;
170 }
171
Jonathan Hart7d1ad602014-10-17 11:48:32 -0700172 // TODO find the correct IP address
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700173 IpAddress ipAddress =
174 dst.ipAddresses().iterator().next().toIpAddress();
175 Ethernet arpReply = buildArpReply(ipAddress, dst.mac(), eth);
alshabibb5522ff2014-09-29 19:20:00 -0700176 // TODO: check send status with host service.
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700177 sendTo(arpReply, src.location());
178 }
179
180 /**
181 * Outputs the given packet out the given port.
182 *
183 * @param packet the packet to send
184 * @param outPort the port to send it out
185 */
186 private void sendTo(Ethernet packet, ConnectPoint outPort) {
187 if (internalPorts.containsEntry(
188 deviceService.getDevice(outPort.deviceId()), outPort.port())) {
189 // Sanity check to make sure we don't send the packet out an
190 // internal port and create a loop (could happen due to
191 // misconfiguration).
192 return;
193 }
194
tom9a693fd2014-10-03 11:32:19 -0700195 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700196 builder.setOutput(outPort.port());
197 packetService.emit(new DefaultOutboundPacket(outPort.deviceId(),
198 builder.build(), ByteBuffer.wrap(packet.serialize())));
199 }
200
201 /**
202 * Finds the port with an address in the subnet of the target address, if
203 * one exists.
204 *
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700205 * @param target the target address to find a matching port for
206 * @return a PortAddresses object if one was found, otherwise null
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700207 */
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700208 private PortAddresses findPortInSubnet(IpAddress target) {
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700209 for (PortAddresses addresses : hostService.getAddressBindings()) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700210 for (InterfaceIpAddress ia : addresses.ipAddresses()) {
211 if (ia.subnetAddress().contains(target)) {
212 return addresses;
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700213 }
214 }
215 }
216 return null;
217 }
218
219 /**
220 * Returns whether the given port is an outside-facing port with an IP
221 * address configured.
222 *
223 * @param port the port to check
224 * @return true if the port is an outside-facing port, otherwise false
225 */
226 private boolean isOutsidePort(ConnectPoint port) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700227 //
228 // TODO: Is this sufficient to identify outside-facing ports: just
229 // having IP addresses on a port?
230 //
231 return !hostService.getAddressBindingsForPort(port).ipAddresses().isEmpty();
alshabibb5522ff2014-09-29 19:20:00 -0700232 }
233
234 @Override
235 public void forward(Ethernet eth) {
Yuta HIGUCHI59718042014-10-04 22:04:56 -0700236 checkNotNull(eth, REQUEST_NULL);
alshabibb5522ff2014-09-29 19:20:00 -0700237 checkArgument(eth.getEtherType() == Ethernet.TYPE_ARP,
238 REQUEST_NOT_ARP);
239 ARP arp = (ARP) eth.getPayload();
Jonathan Hart704ca142014-10-09 09:34:39 -0700240 checkArgument(arp.getOpCode() == ARP.OP_REPLY, NOT_ARP_REPLY);
alshabibb5522ff2014-09-29 19:20:00 -0700241
242 Host h = hostService.getHost(HostId.hostId(eth.getDestinationMAC(),
243 VlanId.vlanId(eth.getVlanID())));
244
245 if (h == null) {
246 flood(eth);
247 } else {
tom9a693fd2014-10-03 11:32:19 -0700248 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabibb5522ff2014-09-29 19:20:00 -0700249 builder.setOutput(h.location().port());
250 packetService.emit(new DefaultOutboundPacket(h.location().deviceId(),
251 builder.build(), ByteBuffer.wrap(eth.serialize())));
252 }
253
254 }
255
alshabibc274c902014-10-03 14:58:27 -0700256 @Override
257 public boolean handleArp(PacketContext context) {
258 InboundPacket pkt = context.inPacket();
259 Ethernet ethPkt = pkt.parsed();
260 if (ethPkt.getEtherType() == Ethernet.TYPE_ARP) {
261 ARP arp = (ARP) ethPkt.getPayload();
262 if (arp.getOpCode() == ARP.OP_REPLY) {
263 forward(ethPkt);
264 } else if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700265 reply(ethPkt, context.inPacket().receivedFrom());
alshabibc274c902014-10-03 14:58:27 -0700266 }
267 context.block();
268 return true;
269 }
270 return false;
271 }
272
alshabibb5522ff2014-09-29 19:20:00 -0700273 /**
274 * Flood the arp request at all edges in the network.
275 * @param request the arp request.
276 */
277 private void flood(Ethernet request) {
278 TrafficTreatment.Builder builder = null;
279 ByteBuffer buf = ByteBuffer.wrap(request.serialize());
280
281 synchronized (externalPorts) {
282 for (Entry<Device, PortNumber> entry : externalPorts.entries()) {
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700283 ConnectPoint cp = new ConnectPoint(entry.getKey().id(), entry.getValue());
284 if (isOutsidePort(cp)) {
285 continue;
286 }
287
tom9a693fd2014-10-03 11:32:19 -0700288 builder = DefaultTrafficTreatment.builder();
alshabibb5522ff2014-09-29 19:20:00 -0700289 builder.setOutput(entry.getValue());
290 packetService.emit(new DefaultOutboundPacket(entry.getKey().id(),
291 builder.build(), buf));
292 }
alshabibb5522ff2014-09-29 19:20:00 -0700293 }
294 }
295
296 /**
297 * Determines the location of all known ports in the system.
298 */
299 private void determinePortLocations() {
300 Iterable<Device> devices = deviceService.getDevices();
301 Iterable<Link> links = null;
302 List<PortNumber> ports = null;
303 for (Device d : devices) {
304 ports = buildPortNumberList(deviceService.getPorts(d.id()));
305 links = linkService.getLinks();
306 for (Link l : links) {
307 // for each link, mark the concerned ports as internal
308 // and the remaining ports are therefore external.
Yuta HIGUCHI3541bf22014-10-04 22:06:19 -0700309 if (l.src().deviceId().equals(d.id())
alshabibb5522ff2014-09-29 19:20:00 -0700310 && ports.contains(l.src().port())) {
311 ports.remove(l.src().port());
312 internalPorts.put(d, l.src().port());
313 }
Yuta HIGUCHI3541bf22014-10-04 22:06:19 -0700314 if (l.dst().deviceId().equals(d.id())
alshabibb5522ff2014-09-29 19:20:00 -0700315 && ports.contains(l.dst().port())) {
316 ports.remove(l.dst().port());
317 internalPorts.put(d, l.dst().port());
318 }
319 }
320 synchronized (externalPorts) {
321 externalPorts.putAll(d, ports);
322 }
323 }
324
325 }
326
327 private List<PortNumber> buildPortNumberList(List<Port> ports) {
328 List<PortNumber> portNumbers = Lists.newLinkedList();
329 for (Port p : ports) {
330 portNumbers.add(p.number());
331 }
332 return portNumbers;
333 }
334
335 /**
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700336 * Builds an ARP reply based on a request.
337 *
338 * @param srcIp the IP address to use as the reply source
339 * @param srcMac the MAC address to use as the reply source
340 * @param request the ARP request we got
341 * @return an Ethernet frame containing the ARP reply
alshabibb5522ff2014-09-29 19:20:00 -0700342 */
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700343 private Ethernet buildArpReply(IpAddress srcIp, MacAddress srcMac,
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700344 Ethernet request) {
345
alshabibb5522ff2014-09-29 19:20:00 -0700346 Ethernet eth = new Ethernet();
347 eth.setDestinationMACAddress(request.getSourceMACAddress());
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700348 eth.setSourceMACAddress(srcMac.getAddress());
alshabibb5522ff2014-09-29 19:20:00 -0700349 eth.setEtherType(Ethernet.TYPE_ARP);
350 eth.setVlanID(request.getVlanID());
351
352 ARP arp = new ARP();
353 arp.setOpCode(ARP.OP_REPLY);
354 arp.setProtocolType(ARP.PROTO_TYPE_IP);
355 arp.setHardwareType(ARP.HW_TYPE_ETHERNET);
alshabib6eb438a2014-10-01 16:39:37 -0700356
alshabibb5522ff2014-09-29 19:20:00 -0700357 arp.setProtocolAddressLength((byte) IpPrefix.INET_LEN);
358 arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH);
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700359 arp.setSenderHardwareAddress(srcMac.getAddress());
alshabibb5522ff2014-09-29 19:20:00 -0700360 arp.setTargetHardwareAddress(request.getSourceMACAddress());
361
362 arp.setTargetProtocolAddress(((ARP) request.getPayload())
363 .getSenderProtocolAddress());
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700364 arp.setSenderProtocolAddress(srcIp.toInt());
alshabibb5522ff2014-09-29 19:20:00 -0700365 eth.setPayload(arp);
366 return eth;
367 }
368
369 public class InternalLinkListener implements LinkListener {
370
371 @Override
372 public void event(LinkEvent event) {
373 Link link = event.subject();
374 Device src = deviceService.getDevice(link.src().deviceId());
375 Device dst = deviceService.getDevice(link.dst().deviceId());
376 switch (event.type()) {
377 case LINK_ADDED:
378 synchronized (externalPorts) {
379 externalPorts.remove(src, link.src().port());
380 externalPorts.remove(dst, link.dst().port());
381 internalPorts.put(src, link.src().port());
382 internalPorts.put(dst, link.dst().port());
383 }
384
385 break;
386 case LINK_REMOVED:
387 synchronized (externalPorts) {
388 externalPorts.put(src, link.src().port());
389 externalPorts.put(dst, link.dst().port());
390 internalPorts.remove(src, link.src().port());
391 internalPorts.remove(dst, link.dst().port());
392 }
393
394 break;
395 case LINK_UPDATED:
396 // don't care about links being updated.
397 break;
398 default:
399 break;
400 }
401
402 }
403
404 }
405
406 public class InternalDeviceListener implements DeviceListener {
407
408 @Override
409 public void event(DeviceEvent event) {
410 Device device = event.subject();
411 switch (event.type()) {
412 case DEVICE_ADDED:
413 case DEVICE_AVAILABILITY_CHANGED:
414 case DEVICE_MASTERSHIP_CHANGED:
415 case DEVICE_SUSPENDED:
416 case DEVICE_UPDATED:
alshabibb5522ff2014-09-29 19:20:00 -0700417 // nothing to do in these cases; handled when links get reported
418 break;
419 case DEVICE_REMOVED:
420 synchronized (externalPorts) {
421 externalPorts.removeAll(device);
422 internalPorts.removeAll(device);
423 }
424 break;
425 case PORT_ADDED:
alshabib6eb438a2014-10-01 16:39:37 -0700426 case PORT_UPDATED:
alshabibb5522ff2014-09-29 19:20:00 -0700427 synchronized (externalPorts) {
alshabib6eb438a2014-10-01 16:39:37 -0700428 if (event.port().isEnabled()) {
429 externalPorts.put(device, event.port().number());
430 internalPorts.remove(device, event.port().number());
431 }
alshabibb5522ff2014-09-29 19:20:00 -0700432 }
433 break;
434 case PORT_REMOVED:
435 synchronized (externalPorts) {
436 externalPorts.remove(device, event.port().number());
437 internalPorts.remove(device, event.port().number());
438 }
439 break;
440 default:
441 break;
442
443 }
444
445 }
446
alshabibc274c902014-10-03 14:58:27 -0700447 }
alshabibb5522ff2014-09-29 19:20:00 -0700448
449}