blob: 3ba1c8ac45bb947911c008c9da696219f27c4d74 [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
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700102 public boolean known(IpAddress 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());
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700153 Set<Host> hosts = hostService.getHostsByIp(IpAddress.valueOf(arp
alshabibb5522ff2014-09-29 19:20:00 -0700154 .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 Radoslavov33f228a2014-10-27 19:33:16 -0700173 IpAddress ipAddress = dst.ipAddresses().iterator().next();
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700174 Ethernet arpReply = buildArpReply(ipAddress, dst.mac(), eth);
alshabibb5522ff2014-09-29 19:20:00 -0700175 // TODO: check send status with host service.
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700176 sendTo(arpReply, src.location());
177 }
178
179 /**
180 * Outputs the given packet out the given port.
181 *
182 * @param packet the packet to send
183 * @param outPort the port to send it out
184 */
185 private void sendTo(Ethernet packet, ConnectPoint outPort) {
186 if (internalPorts.containsEntry(
187 deviceService.getDevice(outPort.deviceId()), outPort.port())) {
188 // Sanity check to make sure we don't send the packet out an
189 // internal port and create a loop (could happen due to
190 // misconfiguration).
191 return;
192 }
193
tom9a693fd2014-10-03 11:32:19 -0700194 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700195 builder.setOutput(outPort.port());
196 packetService.emit(new DefaultOutboundPacket(outPort.deviceId(),
197 builder.build(), ByteBuffer.wrap(packet.serialize())));
198 }
199
200 /**
201 * Finds the port with an address in the subnet of the target address, if
202 * one exists.
203 *
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700204 * @param target the target address to find a matching port for
205 * @return a PortAddresses object if one was found, otherwise null
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700206 */
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700207 private PortAddresses findPortInSubnet(IpAddress target) {
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700208 for (PortAddresses addresses : hostService.getAddressBindings()) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700209 for (InterfaceIpAddress ia : addresses.ipAddresses()) {
210 if (ia.subnetAddress().contains(target)) {
211 return addresses;
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700212 }
213 }
214 }
215 return null;
216 }
217
218 /**
219 * Returns whether the given port is an outside-facing port with an IP
220 * address configured.
221 *
222 * @param port the port to check
223 * @return true if the port is an outside-facing port, otherwise false
224 */
225 private boolean isOutsidePort(ConnectPoint port) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700226 //
227 // TODO: Is this sufficient to identify outside-facing ports: just
228 // having IP addresses on a port?
229 //
230 return !hostService.getAddressBindingsForPort(port).ipAddresses().isEmpty();
alshabibb5522ff2014-09-29 19:20:00 -0700231 }
232
233 @Override
234 public void forward(Ethernet eth) {
Yuta HIGUCHI59718042014-10-04 22:04:56 -0700235 checkNotNull(eth, REQUEST_NULL);
alshabibb5522ff2014-09-29 19:20:00 -0700236 checkArgument(eth.getEtherType() == Ethernet.TYPE_ARP,
237 REQUEST_NOT_ARP);
238 ARP arp = (ARP) eth.getPayload();
Jonathan Hart704ca142014-10-09 09:34:39 -0700239 checkArgument(arp.getOpCode() == ARP.OP_REPLY, NOT_ARP_REPLY);
alshabibb5522ff2014-09-29 19:20:00 -0700240
241 Host h = hostService.getHost(HostId.hostId(eth.getDestinationMAC(),
242 VlanId.vlanId(eth.getVlanID())));
243
244 if (h == null) {
245 flood(eth);
246 } else {
tom9a693fd2014-10-03 11:32:19 -0700247 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabibb5522ff2014-09-29 19:20:00 -0700248 builder.setOutput(h.location().port());
249 packetService.emit(new DefaultOutboundPacket(h.location().deviceId(),
250 builder.build(), ByteBuffer.wrap(eth.serialize())));
251 }
252
253 }
254
alshabibc274c902014-10-03 14:58:27 -0700255 @Override
256 public boolean handleArp(PacketContext context) {
257 InboundPacket pkt = context.inPacket();
258 Ethernet ethPkt = pkt.parsed();
259 if (ethPkt.getEtherType() == Ethernet.TYPE_ARP) {
260 ARP arp = (ARP) ethPkt.getPayload();
261 if (arp.getOpCode() == ARP.OP_REPLY) {
262 forward(ethPkt);
263 } else if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700264 reply(ethPkt, context.inPacket().receivedFrom());
alshabibc274c902014-10-03 14:58:27 -0700265 }
266 context.block();
267 return true;
268 }
269 return false;
270 }
271
alshabibb5522ff2014-09-29 19:20:00 -0700272 /**
273 * Flood the arp request at all edges in the network.
274 * @param request the arp request.
275 */
276 private void flood(Ethernet request) {
277 TrafficTreatment.Builder builder = null;
278 ByteBuffer buf = ByteBuffer.wrap(request.serialize());
279
280 synchronized (externalPorts) {
281 for (Entry<Device, PortNumber> entry : externalPorts.entries()) {
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700282 ConnectPoint cp = new ConnectPoint(entry.getKey().id(), entry.getValue());
283 if (isOutsidePort(cp)) {
284 continue;
285 }
286
tom9a693fd2014-10-03 11:32:19 -0700287 builder = DefaultTrafficTreatment.builder();
alshabibb5522ff2014-09-29 19:20:00 -0700288 builder.setOutput(entry.getValue());
289 packetService.emit(new DefaultOutboundPacket(entry.getKey().id(),
290 builder.build(), buf));
291 }
alshabibb5522ff2014-09-29 19:20:00 -0700292 }
293 }
294
295 /**
296 * Determines the location of all known ports in the system.
297 */
298 private void determinePortLocations() {
299 Iterable<Device> devices = deviceService.getDevices();
300 Iterable<Link> links = null;
301 List<PortNumber> ports = null;
302 for (Device d : devices) {
303 ports = buildPortNumberList(deviceService.getPorts(d.id()));
304 links = linkService.getLinks();
305 for (Link l : links) {
306 // for each link, mark the concerned ports as internal
307 // and the remaining ports are therefore external.
Yuta HIGUCHI3541bf22014-10-04 22:06:19 -0700308 if (l.src().deviceId().equals(d.id())
alshabibb5522ff2014-09-29 19:20:00 -0700309 && ports.contains(l.src().port())) {
310 ports.remove(l.src().port());
311 internalPorts.put(d, l.src().port());
312 }
Yuta HIGUCHI3541bf22014-10-04 22:06:19 -0700313 if (l.dst().deviceId().equals(d.id())
alshabibb5522ff2014-09-29 19:20:00 -0700314 && ports.contains(l.dst().port())) {
315 ports.remove(l.dst().port());
316 internalPorts.put(d, l.dst().port());
317 }
318 }
319 synchronized (externalPorts) {
320 externalPorts.putAll(d, ports);
321 }
322 }
323
324 }
325
326 private List<PortNumber> buildPortNumberList(List<Port> ports) {
327 List<PortNumber> portNumbers = Lists.newLinkedList();
328 for (Port p : ports) {
329 portNumbers.add(p.number());
330 }
331 return portNumbers;
332 }
333
334 /**
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700335 * Builds an ARP reply based on a request.
336 *
337 * @param srcIp the IP address to use as the reply source
338 * @param srcMac the MAC address to use as the reply source
339 * @param request the ARP request we got
340 * @return an Ethernet frame containing the ARP reply
alshabibb5522ff2014-09-29 19:20:00 -0700341 */
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700342 private Ethernet buildArpReply(IpAddress srcIp, MacAddress srcMac,
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700343 Ethernet request) {
344
alshabibb5522ff2014-09-29 19:20:00 -0700345 Ethernet eth = new Ethernet();
346 eth.setDestinationMACAddress(request.getSourceMACAddress());
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700347 eth.setSourceMACAddress(srcMac.getAddress());
alshabibb5522ff2014-09-29 19:20:00 -0700348 eth.setEtherType(Ethernet.TYPE_ARP);
349 eth.setVlanID(request.getVlanID());
350
351 ARP arp = new ARP();
352 arp.setOpCode(ARP.OP_REPLY);
353 arp.setProtocolType(ARP.PROTO_TYPE_IP);
354 arp.setHardwareType(ARP.HW_TYPE_ETHERNET);
alshabib6eb438a2014-10-01 16:39:37 -0700355
alshabibb5522ff2014-09-29 19:20:00 -0700356 arp.setProtocolAddressLength((byte) IpPrefix.INET_LEN);
357 arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH);
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700358 arp.setSenderHardwareAddress(srcMac.getAddress());
alshabibb5522ff2014-09-29 19:20:00 -0700359 arp.setTargetHardwareAddress(request.getSourceMACAddress());
360
361 arp.setTargetProtocolAddress(((ARP) request.getPayload())
362 .getSenderProtocolAddress());
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700363 arp.setSenderProtocolAddress(srcIp.toInt());
alshabibb5522ff2014-09-29 19:20:00 -0700364 eth.setPayload(arp);
365 return eth;
366 }
367
368 public class InternalLinkListener implements LinkListener {
369
370 @Override
371 public void event(LinkEvent event) {
372 Link link = event.subject();
373 Device src = deviceService.getDevice(link.src().deviceId());
374 Device dst = deviceService.getDevice(link.dst().deviceId());
375 switch (event.type()) {
376 case LINK_ADDED:
377 synchronized (externalPorts) {
378 externalPorts.remove(src, link.src().port());
379 externalPorts.remove(dst, link.dst().port());
380 internalPorts.put(src, link.src().port());
381 internalPorts.put(dst, link.dst().port());
382 }
383
384 break;
385 case LINK_REMOVED:
386 synchronized (externalPorts) {
387 externalPorts.put(src, link.src().port());
388 externalPorts.put(dst, link.dst().port());
389 internalPorts.remove(src, link.src().port());
390 internalPorts.remove(dst, link.dst().port());
391 }
392
393 break;
394 case LINK_UPDATED:
395 // don't care about links being updated.
396 break;
397 default:
398 break;
399 }
400
401 }
402
403 }
404
405 public class InternalDeviceListener implements DeviceListener {
406
407 @Override
408 public void event(DeviceEvent event) {
409 Device device = event.subject();
410 switch (event.type()) {
411 case DEVICE_ADDED:
412 case DEVICE_AVAILABILITY_CHANGED:
413 case DEVICE_MASTERSHIP_CHANGED:
414 case DEVICE_SUSPENDED:
415 case DEVICE_UPDATED:
alshabibb5522ff2014-09-29 19:20:00 -0700416 // nothing to do in these cases; handled when links get reported
417 break;
418 case DEVICE_REMOVED:
419 synchronized (externalPorts) {
420 externalPorts.removeAll(device);
421 internalPorts.removeAll(device);
422 }
423 break;
424 case PORT_ADDED:
alshabib6eb438a2014-10-01 16:39:37 -0700425 case PORT_UPDATED:
alshabibb5522ff2014-09-29 19:20:00 -0700426 synchronized (externalPorts) {
alshabib6eb438a2014-10-01 16:39:37 -0700427 if (event.port().isEnabled()) {
428 externalPorts.put(device, event.port().number());
429 internalPorts.remove(device, event.port().number());
430 }
alshabibb5522ff2014-09-29 19:20:00 -0700431 }
432 break;
433 case PORT_REMOVED:
434 synchronized (externalPorts) {
435 externalPorts.remove(device, event.port().number());
436 internalPorts.remove(device, event.port().number());
437 }
438 break;
439 default:
440 break;
441
442 }
443
444 }
445
alshabibc274c902014-10-03 14:58:27 -0700446 }
alshabibb5522ff2014-09-29 19:20:00 -0700447
448}