Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 8e57fd5 | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 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 | */ |
Hyunsun Moon | f948020 | 2016-04-14 16:13:42 -0700 | [diff] [blame] | 16 | package org.onosproject.cordvtn.impl; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 17 | |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 18 | import com.google.common.collect.Maps; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 19 | import org.onlab.packet.ARP; |
| 20 | import org.onlab.packet.EthType; |
| 21 | import org.onlab.packet.Ethernet; |
| 22 | import org.onlab.packet.Ip4Address; |
| 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onlab.packet.MacAddress; |
| 25 | import org.onosproject.core.ApplicationId; |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 26 | import org.onosproject.net.Host; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 27 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 28 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 29 | import org.onosproject.net.flow.TrafficSelector; |
| 30 | import org.onosproject.net.flow.TrafficTreatment; |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 31 | import org.onosproject.net.host.HostService; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 32 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 33 | import org.onosproject.net.packet.PacketContext; |
| 34 | import org.onosproject.net.packet.PacketPriority; |
| 35 | import org.onosproject.net.packet.PacketService; |
| 36 | import org.slf4j.Logger; |
| 37 | |
| 38 | import java.nio.ByteBuffer; |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 39 | import java.util.Map; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 40 | import java.util.Optional; |
| 41 | import java.util.Set; |
| 42 | |
| 43 | import static com.google.common.base.Preconditions.checkNotNull; |
| 44 | import static org.slf4j.LoggerFactory.getLogger; |
| 45 | |
| 46 | /** |
| 47 | * Handles ARP requests for virtual network service IPs. |
| 48 | */ |
| 49 | public class CordVtnArpProxy { |
| 50 | protected final Logger log = getLogger(getClass()); |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 51 | |
| 52 | private final ApplicationId appId; |
| 53 | private final PacketService packetService; |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 54 | private final HostService hostService; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 55 | |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 56 | private final Map<Ip4Address, MacAddress> gateways = Maps.newConcurrentMap(); |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Default constructor. |
| 60 | * |
| 61 | * @param appId application id |
| 62 | * @param packetService packet service |
Thomas Vachuska | 220c61c | 2016-02-18 11:11:46 -0800 | [diff] [blame] | 63 | * @param hostService host service reference |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 64 | */ |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 65 | public CordVtnArpProxy(ApplicationId appId, PacketService packetService, HostService hostService) { |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 66 | this.appId = appId; |
| 67 | this.packetService = packetService; |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 68 | this.hostService = hostService; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Requests ARP packet. |
| 73 | */ |
| 74 | public void requestPacket() { |
| 75 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 76 | .matchEthType(EthType.EtherType.ARP.ethType().toShort()) |
| 77 | .build(); |
| 78 | |
| 79 | packetService.requestPackets(selector, |
| 80 | PacketPriority.CONTROL, |
| 81 | appId, |
| 82 | Optional.empty()); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Cancels ARP packet. |
| 87 | */ |
| 88 | public void cancelPacket() { |
| 89 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 90 | .matchEthType(EthType.EtherType.ARP.ethType().toShort()) |
| 91 | .build(); |
| 92 | |
| 93 | packetService.cancelPackets(selector, |
| 94 | PacketPriority.CONTROL, |
| 95 | appId, |
| 96 | Optional.empty()); |
| 97 | } |
| 98 | |
| 99 | /** |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 100 | * Adds a given gateway IP and MAC address to this ARP proxy. |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 101 | * |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 102 | * @param gatewayIp gateway ip address |
| 103 | * @param gatewayMac gateway mac address |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 104 | */ |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 105 | public void addGateway(IpAddress gatewayIp, MacAddress gatewayMac) { |
| 106 | checkNotNull(gatewayIp); |
| 107 | checkNotNull(gatewayMac); |
| 108 | gateways.put(gatewayIp.getIp4Address(), gatewayMac); |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Removes a given service IP address from this ARP proxy. |
| 113 | * |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 114 | * @param gatewayIp gateway ip address |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 115 | */ |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 116 | public void removeGateway(IpAddress gatewayIp) { |
| 117 | checkNotNull(gatewayIp); |
| 118 | gateways.remove(gatewayIp.getIp4Address()); |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Emits ARP reply with fake MAC address for a given ARP request. |
| 123 | * It only handles requests for the registered service IPs, and the other |
| 124 | * requests can be handled by other ARP handlers like openstackSwitching or |
| 125 | * proxyArp, for example. |
| 126 | * |
| 127 | * @param context packet context |
| 128 | * @param ethPacket ethernet packet |
| 129 | */ |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 130 | public void processArpPacket(PacketContext context, Ethernet ethPacket) { |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 131 | ARP arpPacket = (ARP) ethPacket.getPayload(); |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 132 | if (arpPacket.getOpCode() != ARP.OP_REQUEST) { |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 133 | return; |
| 134 | } |
| 135 | |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 136 | Ip4Address targetIp = Ip4Address.valueOf(arpPacket.getTargetProtocolAddress()); |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 137 | |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 138 | MacAddress gatewayMac = gateways.get(targetIp); |
| 139 | MacAddress replyMac = gatewayMac != null ? gatewayMac : getMacFromHostService(targetIp); |
| 140 | |
| 141 | if (replyMac.equals(MacAddress.NONE)) { |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 142 | log.debug("Failed to find MAC for {}", targetIp.toString()); |
Hyunsun Moon | 0d836e2 | 2016-02-01 23:30:58 -0800 | [diff] [blame] | 143 | context.block(); |
| 144 | return; |
| 145 | } |
| 146 | |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 147 | log.trace("Send ARP reply for {} with {}", targetIp.toString(), replyMac.toString()); |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 148 | Ethernet ethReply = ARP.buildArpReply( |
| 149 | targetIp, |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 150 | replyMac, |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 151 | ethPacket); |
| 152 | |
| 153 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 154 | .setOutput(context.inPacket().receivedFrom().port()) |
| 155 | .build(); |
| 156 | |
| 157 | packetService.emit(new DefaultOutboundPacket( |
| 158 | context.inPacket().receivedFrom().deviceId(), |
| 159 | treatment, |
| 160 | ByteBuffer.wrap(ethReply.serialize()))); |
| 161 | |
| 162 | context.block(); |
| 163 | } |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 164 | |
| 165 | /** |
| 166 | * Emits gratuitous ARP when a gateway mac address has been changed. |
| 167 | * |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 168 | * @param gatewayIp gateway ip address to update MAC |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 169 | * @param hosts set of hosts to send gratuitous ARP packet |
| 170 | */ |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 171 | public void sendGratuitousArpForGateway(IpAddress gatewayIp, Set<Host> hosts) { |
| 172 | MacAddress gatewayMac = gateways.get(gatewayIp.getIp4Address()); |
| 173 | if (gatewayMac == null) { |
| 174 | log.debug("Gateway {} is not registered to ARP proxy", gatewayIp.toString()); |
| 175 | return; |
| 176 | } |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 177 | |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 178 | Ethernet ethArp = buildGratuitousArp(gatewayIp.getIp4Address(), gatewayMac); |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 179 | hosts.stream().forEach(host -> { |
| 180 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 181 | .setOutput(host.location().port()) |
| 182 | .build(); |
| 183 | |
| 184 | packetService.emit(new DefaultOutboundPacket( |
| 185 | host.location().deviceId(), |
| 186 | treatment, |
| 187 | ByteBuffer.wrap(ethArp.serialize()))); |
| 188 | }); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Builds gratuitous ARP packet with a given IP and MAC address. |
| 193 | * |
| 194 | * @param ip ip address for TPA and SPA |
| 195 | * @param mac new mac address |
| 196 | * @return ethernet packet |
| 197 | */ |
| 198 | private Ethernet buildGratuitousArp(IpAddress ip, MacAddress mac) { |
| 199 | Ethernet eth = new Ethernet(); |
| 200 | |
| 201 | eth.setEtherType(Ethernet.TYPE_ARP); |
| 202 | eth.setSourceMACAddress(mac); |
| 203 | eth.setDestinationMACAddress(MacAddress.BROADCAST); |
| 204 | |
| 205 | ARP arp = new ARP(); |
| 206 | arp.setOpCode(ARP.OP_REQUEST); |
| 207 | arp.setHardwareType(ARP.HW_TYPE_ETHERNET); |
| 208 | arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH); |
| 209 | arp.setProtocolType(ARP.PROTO_TYPE_IP); |
| 210 | arp.setProtocolAddressLength((byte) Ip4Address.BYTE_LENGTH); |
| 211 | |
| 212 | arp.setSenderHardwareAddress(mac.toBytes()); |
| 213 | arp.setTargetHardwareAddress(MacAddress.BROADCAST.toBytes()); |
| 214 | arp.setSenderProtocolAddress(ip.getIp4Address().toOctets()); |
| 215 | arp.setTargetProtocolAddress(ip.getIp4Address().toOctets()); |
| 216 | |
| 217 | eth.setPayload(arp); |
| 218 | return eth; |
| 219 | } |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 220 | |
| 221 | /** |
| 222 | * Returns MAC address of a host with a given target IP address by asking to |
| 223 | * host service. It does not support overlapping IP. |
| 224 | * |
| 225 | * @param targetIp target ip |
| 226 | * @return mac address, or NONE mac address if it fails to find the mac |
| 227 | */ |
| 228 | private MacAddress getMacFromHostService(IpAddress targetIp) { |
| 229 | checkNotNull(targetIp); |
| 230 | |
| 231 | Host host = hostService.getHostsByIp(targetIp) |
| 232 | .stream() |
| 233 | .findFirst() |
| 234 | .orElse(null); |
| 235 | |
| 236 | if (host != null) { |
Hyunsun Moon | 97c7419 | 2016-02-17 19:00:50 -0800 | [diff] [blame] | 237 | log.trace("Found MAC from host service for {}", targetIp.toString()); |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 238 | return host.mac(); |
| 239 | } else { |
| 240 | return MacAddress.NONE; |
| 241 | } |
| 242 | } |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 243 | } |