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 | */ |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 16 | package org.opencord.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 | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 24 | import org.onlab.packet.ARP; |
| 25 | import org.onlab.packet.EthType; |
| 26 | import org.onlab.packet.Ethernet; |
| 27 | import org.onlab.packet.Ip4Address; |
| 28 | import org.onlab.packet.IpAddress; |
| 29 | import org.onlab.packet.MacAddress; |
Hyunsun Moon | 4302c2b | 2017-01-19 14:20:34 +0900 | [diff] [blame] | 30 | import org.onosproject.core.ApplicationId; |
| 31 | import org.onosproject.core.CoreService; |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 32 | import org.onosproject.net.DeviceId; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 33 | import org.onosproject.net.Host; |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 34 | import org.onosproject.net.PortNumber; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 35 | import org.onosproject.net.config.NetworkConfigEvent; |
| 36 | import org.onosproject.net.config.NetworkConfigListener; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 37 | import org.onosproject.net.config.NetworkConfigService; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 38 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 39 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 40 | import org.onosproject.net.flow.TrafficSelector; |
| 41 | import org.onosproject.net.flow.TrafficTreatment; |
Hyunsun Moon | 4302c2b | 2017-01-19 14:20:34 +0900 | [diff] [blame] | 42 | import org.onosproject.net.host.HostService; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 43 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 44 | import org.onosproject.net.packet.PacketContext; |
| 45 | import org.onosproject.net.packet.PacketPriority; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 46 | import org.onosproject.net.packet.PacketProcessor; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 47 | import org.onosproject.net.packet.PacketService; |
Hyunsun Moon | 4302c2b | 2017-01-19 14:20:34 +0900 | [diff] [blame] | 48 | import org.opencord.cordvtn.api.Constants; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 49 | import org.opencord.cordvtn.api.CordVtnConfig; |
| 50 | import org.opencord.cordvtn.api.core.Instance; |
Hyunsun Moon | 4302c2b | 2017-01-19 14:20:34 +0900 | [diff] [blame] | 51 | import org.opencord.cordvtn.api.core.ServiceNetworkEvent; |
| 52 | import org.opencord.cordvtn.api.core.ServiceNetworkListener; |
| 53 | import org.opencord.cordvtn.api.core.ServiceNetworkService; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 54 | import org.opencord.cordvtn.api.net.ServiceNetwork; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 55 | import org.slf4j.Logger; |
| 56 | |
| 57 | import java.nio.ByteBuffer; |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 58 | import java.util.Map; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 59 | import java.util.Optional; |
| 60 | import java.util.Set; |
| 61 | |
Hyunsun Moon | 4302c2b | 2017-01-19 14:20:34 +0900 | [diff] [blame] | 62 | import static com.google.common.base.Preconditions.checkArgument; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 63 | import static com.google.common.base.Preconditions.checkNotNull; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 64 | import static org.opencord.cordvtn.api.net.ServiceNetwork.NetworkType.*; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 65 | import static org.slf4j.LoggerFactory.getLogger; |
| 66 | |
| 67 | /** |
| 68 | * Handles ARP requests for virtual network service IPs. |
| 69 | */ |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 70 | @Component(immediate = true) |
Hyunsun Moon | 4302c2b | 2017-01-19 14:20:34 +0900 | [diff] [blame] | 71 | public class CordVtnArpProxy { |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 72 | protected final Logger log = getLogger(getClass()); |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 73 | |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 75 | protected PacketService packetService; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 76 | |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 4302c2b | 2017-01-19 14:20:34 +0900 | [diff] [blame] | 78 | protected CoreService coreService; |
| 79 | |
| 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 81 | protected HostService hostService; |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 82 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 84 | protected NetworkConfigService configService; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 85 | |
Hyunsun Moon | 4302c2b | 2017-01-19 14:20:34 +0900 | [diff] [blame] | 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 87 | protected CordVtnNodeManager nodeManager; |
| 88 | |
| 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 90 | protected ServiceNetworkService snetService; |
| 91 | |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 92 | private final PacketProcessor packetProcessor = new InternalPacketProcessor(); |
Hyunsun Moon | 8e9bc93 | 2017-01-31 21:32:20 +0900 | [diff] [blame] | 93 | private final Map<IpAddress, MacAddress> gateways = Maps.newConcurrentMap(); |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 94 | |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 95 | private MacAddress privateGatewayMac = MacAddress.NONE; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 96 | private NetworkConfigListener configListener = new InternalConfigListener(); |
Hyunsun Moon | 4302c2b | 2017-01-19 14:20:34 +0900 | [diff] [blame] | 97 | private ServiceNetworkListener snetListener = new InternalServiceNetworkListener(); |
| 98 | private ApplicationId appId; |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 99 | |
| 100 | @Activate |
| 101 | protected void activate() { |
Hyunsun Moon | 4302c2b | 2017-01-19 14:20:34 +0900 | [diff] [blame] | 102 | appId = coreService.registerApplication(Constants.CORDVTN_APP_ID); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 103 | configService.addListener(configListener); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 104 | readConfiguration(); |
| 105 | |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 106 | packetService.addProcessor(packetProcessor, PacketProcessor.director(0)); |
| 107 | requestPacket(); |
Hyunsun Moon | 4302c2b | 2017-01-19 14:20:34 +0900 | [diff] [blame] | 108 | |
| 109 | snetService.addListener(snetListener); |
Hyunsun Moon | 8e9bc93 | 2017-01-31 21:32:20 +0900 | [diff] [blame] | 110 | snetService.serviceNetworks().stream() |
| 111 | .filter(net -> net.type() == PRIVATE || net.type() == VSG) |
| 112 | .filter(net -> net.serviceIp() != null) |
| 113 | .forEach(net -> addGateway(net.serviceIp(), privateGatewayMac)); |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Deactivate |
| 117 | protected void deactivate() { |
Hyunsun Moon | 4302c2b | 2017-01-19 14:20:34 +0900 | [diff] [blame] | 118 | snetService.removeListener(snetListener); |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 119 | packetService.removeProcessor(packetProcessor); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 120 | configService.removeListener(configListener); |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Requests ARP packet. |
| 125 | */ |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 126 | private void requestPacket() { |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 127 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 128 | .matchEthType(EthType.EtherType.ARP.ethType().toShort()) |
| 129 | .build(); |
| 130 | |
Hyunsun Moon | 4302c2b | 2017-01-19 14:20:34 +0900 | [diff] [blame] | 131 | packetService.requestPackets( |
| 132 | selector, |
| 133 | PacketPriority.CONTROL, |
| 134 | appId, |
| 135 | Optional.empty()); |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Cancels ARP packet. |
| 140 | */ |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 141 | private void cancelPacket() { |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 142 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 143 | .matchEthType(EthType.EtherType.ARP.ethType().toShort()) |
| 144 | .build(); |
| 145 | |
Hyunsun Moon | 4302c2b | 2017-01-19 14:20:34 +0900 | [diff] [blame] | 146 | packetService.cancelPackets( |
| 147 | selector, |
| 148 | PacketPriority.CONTROL, |
| 149 | appId, |
| 150 | Optional.empty()); |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /** |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 154 | * Adds a given gateway IP and MAC address to this ARP proxy. |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 155 | * |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 156 | * @param gatewayIp gateway ip address |
| 157 | * @param gatewayMac gateway mac address |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 158 | */ |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 159 | private void addGateway(IpAddress gatewayIp, MacAddress gatewayMac) { |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 160 | checkNotNull(gatewayIp); |
Hyunsun Moon | 4302c2b | 2017-01-19 14:20:34 +0900 | [diff] [blame] | 161 | checkArgument(gatewayMac != null && gatewayMac != MacAddress.NONE, |
| 162 | "privateGatewayMac is not configured"); |
Hyunsun Moon | 8e9bc93 | 2017-01-31 21:32:20 +0900 | [diff] [blame] | 163 | |
| 164 | MacAddress existing = gateways.get(gatewayIp); |
| 165 | if (existing != null && !existing.equals(privateGatewayMac) && |
| 166 | gatewayMac.equals(privateGatewayMac)) { |
| 167 | // this is public gateway IP and MAC configured via netcfg |
| 168 | // don't update with private gateway MAC |
| 169 | return; |
| 170 | } |
| 171 | gateways.put(gatewayIp, gatewayMac); |
| 172 | log.debug("Added ARP proxy entry IP:{} MAC:{}", gatewayIp, gatewayMac); |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Removes a given service IP address from this ARP proxy. |
| 177 | * |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 178 | * @param gatewayIp gateway ip address |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 179 | */ |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 180 | private void removeGateway(IpAddress gatewayIp) { |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 181 | checkNotNull(gatewayIp); |
Hyunsun Moon | 8e9bc93 | 2017-01-31 21:32:20 +0900 | [diff] [blame] | 182 | MacAddress existing = gateways.get(gatewayIp); |
| 183 | if (existing == null) { |
| 184 | return; |
| 185 | } |
| 186 | if (!existing.equals(privateGatewayMac)) { |
| 187 | // this is public gateway IP and MAC configured via netcfg |
| 188 | // do nothing |
| 189 | return; |
| 190 | } |
| 191 | gateways.remove(gatewayIp); |
| 192 | log.debug("Removed ARP proxy entry for IP:{} MAC: {}", gatewayIp, existing); |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Emits ARP reply with fake MAC address for a given ARP request. |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 197 | * It only handles requests for the registered gateway IPs and host IPs. |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 198 | * |
| 199 | * @param context packet context |
| 200 | * @param ethPacket ethernet packet |
| 201 | */ |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 202 | private void processArpRequest(PacketContext context, Ethernet ethPacket) { |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 203 | ARP arpPacket = (ARP) ethPacket.getPayload(); |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 204 | Ip4Address targetIp = Ip4Address.valueOf(arpPacket.getTargetProtocolAddress()); |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 205 | |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 206 | MacAddress gatewayMac = gateways.get(targetIp); |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 207 | MacAddress replyMac = gatewayMac != null ? gatewayMac : |
| 208 | getMacFromHostService(targetIp); |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 209 | |
| 210 | if (replyMac.equals(MacAddress.NONE)) { |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 211 | log.trace("Failed to find MAC for {}", targetIp); |
| 212 | forwardManagementArpRequest(context, ethPacket); |
Hyunsun Moon | 0d836e2 | 2016-02-01 23:30:58 -0800 | [diff] [blame] | 213 | return; |
| 214 | } |
| 215 | |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 216 | Ethernet ethReply = ARP.buildArpReply( |
| 217 | targetIp, |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 218 | replyMac, |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 219 | ethPacket); |
| 220 | |
| 221 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 222 | .setOutput(context.inPacket().receivedFrom().port()) |
| 223 | .build(); |
| 224 | |
| 225 | packetService.emit(new DefaultOutboundPacket( |
| 226 | context.inPacket().receivedFrom().deviceId(), |
| 227 | treatment, |
| 228 | ByteBuffer.wrap(ethReply.serialize()))); |
| 229 | |
| 230 | context.block(); |
| 231 | } |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 232 | |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 233 | private void processArpReply(PacketContext context, Ethernet ethPacket) { |
| 234 | ARP arpPacket = (ARP) ethPacket.getPayload(); |
| 235 | Ip4Address targetIp = Ip4Address.valueOf(arpPacket.getTargetProtocolAddress()); |
| 236 | |
| 237 | DeviceId deviceId = context.inPacket().receivedFrom().deviceId(); |
| 238 | Host host = hostService.getHostsByIp(targetIp).stream() |
| 239 | .filter(h -> h.location().deviceId().equals(deviceId)) |
| 240 | .findFirst() |
| 241 | .orElse(null); |
| 242 | |
| 243 | if (host == null) { |
| 244 | // do nothing for the unknown ARP reply |
| 245 | log.trace("No host found for {} in {}", targetIp, deviceId); |
| 246 | context.block(); |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 251 | .setOutput(host.location().port()) |
| 252 | .build(); |
| 253 | |
| 254 | packetService.emit(new DefaultOutboundPacket( |
| 255 | deviceId, |
| 256 | treatment, |
| 257 | ByteBuffer.wrap(ethPacket.serialize()))); |
| 258 | |
| 259 | context.block(); |
| 260 | } |
| 261 | |
| 262 | private void forwardManagementArpRequest(PacketContext context, Ethernet ethPacket) { |
| 263 | DeviceId deviceId = context.inPacket().receivedFrom().deviceId(); |
| 264 | PortNumber hostMgmtPort = nodeManager.hostManagementPort(deviceId); |
| 265 | Host host = hostService.getConnectedHosts(context.inPacket().receivedFrom()) |
| 266 | .stream() |
| 267 | .findFirst().orElse(null); |
| 268 | |
| 269 | if (host == null || |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 270 | !Instance.of(host).netType().name().contains("MANAGEMENT") || |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 271 | hostMgmtPort == null) { |
| 272 | context.block(); |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 277 | .setOutput(hostMgmtPort) |
| 278 | .build(); |
| 279 | |
| 280 | packetService.emit(new DefaultOutboundPacket( |
| 281 | context.inPacket().receivedFrom().deviceId(), |
| 282 | treatment, |
| 283 | ByteBuffer.wrap(ethPacket.serialize()))); |
| 284 | |
| 285 | log.trace("Forward ARP request to management network"); |
| 286 | context.block(); |
| 287 | } |
| 288 | |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 289 | /** |
| 290 | * Emits gratuitous ARP when a gateway mac address has been changed. |
| 291 | * |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 292 | * @param gatewayIp gateway ip address to update MAC |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 293 | * @param instances set of instances to send gratuitous ARP packet |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 294 | */ |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 295 | private void sendGratuitousArp(IpAddress gatewayIp, Set<Instance> instances) { |
Hyunsun Moon | 8e9bc93 | 2017-01-31 21:32:20 +0900 | [diff] [blame] | 296 | MacAddress gatewayMac = gateways.get(gatewayIp); |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 297 | if (gatewayMac == null) { |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 298 | log.debug("Gateway {} is not registered to ARP proxy", gatewayIp); |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 299 | return; |
| 300 | } |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 301 | |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 302 | Ethernet ethArp = buildGratuitousArp(gatewayIp.getIp4Address(), gatewayMac); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 303 | instances.stream().forEach(instance -> { |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 304 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 305 | .setOutput(instance.portNumber()) |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 306 | .build(); |
| 307 | |
| 308 | packetService.emit(new DefaultOutboundPacket( |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 309 | instance.deviceId(), |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 310 | treatment, |
| 311 | ByteBuffer.wrap(ethArp.serialize()))); |
| 312 | }); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Builds gratuitous ARP packet with a given IP and MAC address. |
| 317 | * |
| 318 | * @param ip ip address for TPA and SPA |
| 319 | * @param mac new mac address |
| 320 | * @return ethernet packet |
| 321 | */ |
| 322 | private Ethernet buildGratuitousArp(IpAddress ip, MacAddress mac) { |
| 323 | Ethernet eth = new Ethernet(); |
| 324 | |
| 325 | eth.setEtherType(Ethernet.TYPE_ARP); |
| 326 | eth.setSourceMACAddress(mac); |
| 327 | eth.setDestinationMACAddress(MacAddress.BROADCAST); |
| 328 | |
| 329 | ARP arp = new ARP(); |
| 330 | arp.setOpCode(ARP.OP_REQUEST); |
| 331 | arp.setHardwareType(ARP.HW_TYPE_ETHERNET); |
| 332 | arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH); |
| 333 | arp.setProtocolType(ARP.PROTO_TYPE_IP); |
| 334 | arp.setProtocolAddressLength((byte) Ip4Address.BYTE_LENGTH); |
| 335 | |
| 336 | arp.setSenderHardwareAddress(mac.toBytes()); |
| 337 | arp.setTargetHardwareAddress(MacAddress.BROADCAST.toBytes()); |
| 338 | arp.setSenderProtocolAddress(ip.getIp4Address().toOctets()); |
| 339 | arp.setTargetProtocolAddress(ip.getIp4Address().toOctets()); |
| 340 | |
| 341 | eth.setPayload(arp); |
| 342 | return eth; |
| 343 | } |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 344 | |
| 345 | /** |
| 346 | * Returns MAC address of a host with a given target IP address by asking to |
| 347 | * host service. It does not support overlapping IP. |
| 348 | * |
| 349 | * @param targetIp target ip |
| 350 | * @return mac address, or NONE mac address if it fails to find the mac |
| 351 | */ |
| 352 | private MacAddress getMacFromHostService(IpAddress targetIp) { |
| 353 | checkNotNull(targetIp); |
| 354 | |
| 355 | Host host = hostService.getHostsByIp(targetIp) |
| 356 | .stream() |
| 357 | .findFirst() |
| 358 | .orElse(null); |
| 359 | |
| 360 | if (host != null) { |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 361 | log.trace("Found MAC from host service for {}", targetIp); |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 362 | return host.mac(); |
| 363 | } else { |
| 364 | return MacAddress.NONE; |
| 365 | } |
| 366 | } |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 367 | |
| 368 | private class InternalPacketProcessor implements PacketProcessor { |
| 369 | |
| 370 | @Override |
| 371 | public void process(PacketContext context) { |
| 372 | if (context.isHandled()) { |
| 373 | return; |
| 374 | } |
| 375 | Ethernet ethPacket = context.inPacket().parsed(); |
| 376 | if (ethPacket == null || ethPacket.getEtherType() != Ethernet.TYPE_ARP) { |
| 377 | return; |
| 378 | } |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 379 | |
| 380 | ARP arpPacket = (ARP) ethPacket.getPayload(); |
| 381 | switch (arpPacket.getOpCode()) { |
| 382 | case ARP.OP_REQUEST: |
| 383 | processArpRequest(context, ethPacket); |
| 384 | break; |
| 385 | case ARP.OP_REPLY: |
| 386 | processArpReply(context, ethPacket); |
| 387 | break; |
| 388 | default: |
| 389 | break; |
| 390 | } |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
Hyunsun Moon | 4302c2b | 2017-01-19 14:20:34 +0900 | [diff] [blame] | 394 | private class InternalServiceNetworkListener implements ServiceNetworkListener { |
| 395 | |
| 396 | @Override |
| 397 | public boolean isRelevant(ServiceNetworkEvent event) { |
| 398 | ServiceNetwork snet = event.subject(); |
Hyunsun Moon | 8e9bc93 | 2017-01-31 21:32:20 +0900 | [diff] [blame] | 399 | return snet.serviceIp() != null; |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Hyunsun Moon | 4302c2b | 2017-01-19 14:20:34 +0900 | [diff] [blame] | 402 | @Override |
| 403 | public void event(ServiceNetworkEvent event) { |
| 404 | ServiceNetwork snet = event.subject(); |
| 405 | switch (event.type()) { |
| 406 | case SERVICE_NETWORK_CREATED: |
| 407 | case SERVICE_NETWORK_UPDATED: |
Hyunsun Moon | 8e9bc93 | 2017-01-31 21:32:20 +0900 | [diff] [blame] | 408 | addGateway(snet.serviceIp(), privateGatewayMac); |
Hyunsun Moon | 4302c2b | 2017-01-19 14:20:34 +0900 | [diff] [blame] | 409 | break; |
| 410 | case SERVICE_NETWORK_REMOVED: |
| 411 | removeGateway(snet.serviceIp()); |
| 412 | break; |
| 413 | case SERVICE_PORT_CREATED: |
| 414 | case SERVICE_PORT_UPDATED: |
| 415 | case SERVICE_PORT_REMOVED: |
| 416 | default: |
| 417 | // do nothing for the other events |
| 418 | break; |
| 419 | } |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 423 | private void readConfiguration() { |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 424 | CordVtnConfig config = configService.getConfig(appId, CordVtnConfig.class); |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 425 | if (config == null) { |
| 426 | log.debug("No configuration found"); |
| 427 | return; |
| 428 | } |
Hyunsun Moon | 8e9bc93 | 2017-01-31 21:32:20 +0900 | [diff] [blame] | 429 | // TODO handle the case that private gateway MAC is changed |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 430 | privateGatewayMac = config.privateGatewayMac(); |
Hyunsun Moon | 4302c2b | 2017-01-19 14:20:34 +0900 | [diff] [blame] | 431 | log.debug("Set default service IP MAC address {}", privateGatewayMac); |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 432 | |
| 433 | config.publicGateways().entrySet().stream().forEach(entry -> { |
| 434 | addGateway(entry.getKey(), entry.getValue()); |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 435 | }); |
| 436 | // TODO send gratuitous arp in case the MAC is changed |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 437 | } |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 438 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 439 | private class InternalConfigListener implements NetworkConfigListener { |
| 440 | |
| 441 | @Override |
Hyunsun Moon | 4302c2b | 2017-01-19 14:20:34 +0900 | [diff] [blame] | 442 | public boolean isRelevant(NetworkConfigEvent event) { |
| 443 | return event.configClass().equals(CordVtnConfig.class); |
| 444 | } |
| 445 | |
| 446 | @Override |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 447 | public void event(NetworkConfigEvent event) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 448 | |
| 449 | switch (event.type()) { |
| 450 | case CONFIG_ADDED: |
| 451 | case CONFIG_UPDATED: |
| 452 | readConfiguration(); |
| 453 | break; |
| 454 | default: |
| 455 | break; |
| 456 | } |
| 457 | } |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 458 | } |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 459 | } |