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 com.google.common.collect.Sets; |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 25 | import org.onlab.packet.ARP; |
| 26 | import org.onlab.packet.EthType; |
| 27 | import org.onlab.packet.Ethernet; |
| 28 | import org.onlab.packet.Ip4Address; |
| 29 | import org.onlab.packet.IpAddress; |
| 30 | import org.onlab.packet.MacAddress; |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.net.PortNumber; |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 33 | import org.onosproject.net.packet.PacketProcessor; |
| 34 | import org.onosproject.xosclient.api.VtnService; |
| 35 | import org.opencord.cordvtn.api.CordVtnConfig; |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 36 | import org.opencord.cordvtn.api.Instance; |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 37 | import org.onosproject.net.Host; |
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; |
| 42 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 43 | import org.onosproject.net.packet.PacketContext; |
| 44 | import org.onosproject.net.packet.PacketPriority; |
| 45 | import org.onosproject.net.packet.PacketService; |
| 46 | import org.slf4j.Logger; |
| 47 | |
| 48 | import java.nio.ByteBuffer; |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 49 | import java.util.Map; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 50 | import java.util.Optional; |
| 51 | import java.util.Set; |
| 52 | |
| 53 | import static com.google.common.base.Preconditions.checkNotNull; |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 54 | import static org.onosproject.xosclient.api.VtnServiceApi.NetworkType.PRIVATE; |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 55 | import static org.onosproject.xosclient.api.VtnServiceApi.ServiceType.MANAGEMENT; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 56 | import static org.slf4j.LoggerFactory.getLogger; |
| 57 | |
| 58 | /** |
| 59 | * Handles ARP requests for virtual network service IPs. |
| 60 | */ |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 61 | @Component(immediate = true) |
| 62 | public class CordVtnArpProxy extends AbstractInstanceHandler { |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 63 | protected final Logger log = getLogger(getClass()); |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 64 | |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 66 | protected PacketService packetService; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 67 | |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 69 | protected CordVtnNodeManager nodeManager; |
| 70 | |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 71 | private final PacketProcessor packetProcessor = new InternalPacketProcessor(); |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 72 | private final Map<Ip4Address, MacAddress> gateways = Maps.newConcurrentMap(); |
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 | private MacAddress privateGatewayMac = MacAddress.NONE; |
| 75 | |
| 76 | @Activate |
| 77 | protected void activate() { |
| 78 | super.activate(); |
| 79 | packetService.addProcessor(packetProcessor, PacketProcessor.director(0)); |
| 80 | requestPacket(); |
| 81 | } |
| 82 | |
| 83 | @Deactivate |
| 84 | protected void deactivate() { |
| 85 | packetService.removeProcessor(packetProcessor); |
| 86 | super.deactivate(); |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Requests ARP packet. |
| 91 | */ |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 92 | private void requestPacket() { |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 93 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 94 | .matchEthType(EthType.EtherType.ARP.ethType().toShort()) |
| 95 | .build(); |
| 96 | |
| 97 | packetService.requestPackets(selector, |
| 98 | PacketPriority.CONTROL, |
| 99 | appId, |
| 100 | Optional.empty()); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Cancels ARP packet. |
| 105 | */ |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 106 | private void cancelPacket() { |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 107 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 108 | .matchEthType(EthType.EtherType.ARP.ethType().toShort()) |
| 109 | .build(); |
| 110 | |
| 111 | packetService.cancelPackets(selector, |
| 112 | PacketPriority.CONTROL, |
| 113 | appId, |
| 114 | Optional.empty()); |
| 115 | } |
| 116 | |
| 117 | /** |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 118 | * Adds a given gateway IP and MAC address to this ARP proxy. |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 119 | * |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 120 | * @param gatewayIp gateway ip address |
| 121 | * @param gatewayMac gateway mac address |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 122 | */ |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 123 | private void addGateway(IpAddress gatewayIp, MacAddress gatewayMac) { |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 124 | checkNotNull(gatewayIp); |
| 125 | checkNotNull(gatewayMac); |
| 126 | gateways.put(gatewayIp.getIp4Address(), gatewayMac); |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Removes a given service IP address from this ARP proxy. |
| 131 | * |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 132 | * @param gatewayIp gateway ip address |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 133 | */ |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 134 | private void removeGateway(IpAddress gatewayIp) { |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 135 | checkNotNull(gatewayIp); |
| 136 | gateways.remove(gatewayIp.getIp4Address()); |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Emits ARP reply with fake MAC address for a given ARP request. |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 141 | * It only handles requests for the registered gateway IPs and host IPs. |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 142 | * |
| 143 | * @param context packet context |
| 144 | * @param ethPacket ethernet packet |
| 145 | */ |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 146 | private void processArpRequest(PacketContext context, Ethernet ethPacket) { |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 147 | ARP arpPacket = (ARP) ethPacket.getPayload(); |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 148 | Ip4Address targetIp = Ip4Address.valueOf(arpPacket.getTargetProtocolAddress()); |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 149 | |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 150 | MacAddress gatewayMac = gateways.get(targetIp); |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 151 | MacAddress replyMac = gatewayMac != null ? gatewayMac : |
| 152 | getMacFromHostService(targetIp); |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 153 | |
| 154 | if (replyMac.equals(MacAddress.NONE)) { |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 155 | log.trace("Failed to find MAC for {}", targetIp); |
| 156 | forwardManagementArpRequest(context, ethPacket); |
Hyunsun Moon | 0d836e2 | 2016-02-01 23:30:58 -0800 | [diff] [blame] | 157 | return; |
| 158 | } |
| 159 | |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 160 | log.trace("Send ARP reply for {} with {}", targetIp, replyMac); |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 161 | Ethernet ethReply = ARP.buildArpReply( |
| 162 | targetIp, |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 163 | replyMac, |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 164 | ethPacket); |
| 165 | |
| 166 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 167 | .setOutput(context.inPacket().receivedFrom().port()) |
| 168 | .build(); |
| 169 | |
| 170 | packetService.emit(new DefaultOutboundPacket( |
| 171 | context.inPacket().receivedFrom().deviceId(), |
| 172 | treatment, |
| 173 | ByteBuffer.wrap(ethReply.serialize()))); |
| 174 | |
| 175 | context.block(); |
| 176 | } |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 177 | |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 178 | private void processArpReply(PacketContext context, Ethernet ethPacket) { |
| 179 | ARP arpPacket = (ARP) ethPacket.getPayload(); |
| 180 | Ip4Address targetIp = Ip4Address.valueOf(arpPacket.getTargetProtocolAddress()); |
| 181 | |
| 182 | DeviceId deviceId = context.inPacket().receivedFrom().deviceId(); |
| 183 | Host host = hostService.getHostsByIp(targetIp).stream() |
| 184 | .filter(h -> h.location().deviceId().equals(deviceId)) |
| 185 | .findFirst() |
| 186 | .orElse(null); |
| 187 | |
| 188 | if (host == null) { |
| 189 | // do nothing for the unknown ARP reply |
| 190 | log.trace("No host found for {} in {}", targetIp, deviceId); |
| 191 | context.block(); |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 196 | .setOutput(host.location().port()) |
| 197 | .build(); |
| 198 | |
| 199 | packetService.emit(new DefaultOutboundPacket( |
| 200 | deviceId, |
| 201 | treatment, |
| 202 | ByteBuffer.wrap(ethPacket.serialize()))); |
| 203 | |
| 204 | context.block(); |
| 205 | } |
| 206 | |
| 207 | private void forwardManagementArpRequest(PacketContext context, Ethernet ethPacket) { |
| 208 | DeviceId deviceId = context.inPacket().receivedFrom().deviceId(); |
| 209 | PortNumber hostMgmtPort = nodeManager.hostManagementPort(deviceId); |
| 210 | Host host = hostService.getConnectedHosts(context.inPacket().receivedFrom()) |
| 211 | .stream() |
| 212 | .findFirst().orElse(null); |
| 213 | |
| 214 | if (host == null || |
| 215 | !Instance.of(host).serviceType().equals(MANAGEMENT) || |
| 216 | hostMgmtPort == null) { |
| 217 | context.block(); |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 222 | .setOutput(hostMgmtPort) |
| 223 | .build(); |
| 224 | |
| 225 | packetService.emit(new DefaultOutboundPacket( |
| 226 | context.inPacket().receivedFrom().deviceId(), |
| 227 | treatment, |
| 228 | ByteBuffer.wrap(ethPacket.serialize()))); |
| 229 | |
| 230 | log.trace("Forward ARP request to management network"); |
| 231 | context.block(); |
| 232 | } |
| 233 | |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 234 | /** |
| 235 | * Emits gratuitous ARP when a gateway mac address has been changed. |
| 236 | * |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 237 | * @param gatewayIp gateway ip address to update MAC |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 238 | * @param instances set of instances to send gratuitous ARP packet |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 239 | */ |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 240 | private void sendGratuitousArp(IpAddress gatewayIp, Set<Instance> instances) { |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 241 | MacAddress gatewayMac = gateways.get(gatewayIp.getIp4Address()); |
| 242 | if (gatewayMac == null) { |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 243 | log.debug("Gateway {} is not registered to ARP proxy", gatewayIp); |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 244 | return; |
| 245 | } |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 246 | |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 247 | Ethernet ethArp = buildGratuitousArp(gatewayIp.getIp4Address(), gatewayMac); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 248 | instances.stream().forEach(instance -> { |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 249 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 250 | .setOutput(instance.portNumber()) |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 251 | .build(); |
| 252 | |
| 253 | packetService.emit(new DefaultOutboundPacket( |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 254 | instance.deviceId(), |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 255 | treatment, |
| 256 | ByteBuffer.wrap(ethArp.serialize()))); |
| 257 | }); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Builds gratuitous ARP packet with a given IP and MAC address. |
| 262 | * |
| 263 | * @param ip ip address for TPA and SPA |
| 264 | * @param mac new mac address |
| 265 | * @return ethernet packet |
| 266 | */ |
| 267 | private Ethernet buildGratuitousArp(IpAddress ip, MacAddress mac) { |
| 268 | Ethernet eth = new Ethernet(); |
| 269 | |
| 270 | eth.setEtherType(Ethernet.TYPE_ARP); |
| 271 | eth.setSourceMACAddress(mac); |
| 272 | eth.setDestinationMACAddress(MacAddress.BROADCAST); |
| 273 | |
| 274 | ARP arp = new ARP(); |
| 275 | arp.setOpCode(ARP.OP_REQUEST); |
| 276 | arp.setHardwareType(ARP.HW_TYPE_ETHERNET); |
| 277 | arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH); |
| 278 | arp.setProtocolType(ARP.PROTO_TYPE_IP); |
| 279 | arp.setProtocolAddressLength((byte) Ip4Address.BYTE_LENGTH); |
| 280 | |
| 281 | arp.setSenderHardwareAddress(mac.toBytes()); |
| 282 | arp.setTargetHardwareAddress(MacAddress.BROADCAST.toBytes()); |
| 283 | arp.setSenderProtocolAddress(ip.getIp4Address().toOctets()); |
| 284 | arp.setTargetProtocolAddress(ip.getIp4Address().toOctets()); |
| 285 | |
| 286 | eth.setPayload(arp); |
| 287 | return eth; |
| 288 | } |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 289 | |
| 290 | /** |
| 291 | * Returns MAC address of a host with a given target IP address by asking to |
| 292 | * host service. It does not support overlapping IP. |
| 293 | * |
| 294 | * @param targetIp target ip |
| 295 | * @return mac address, or NONE mac address if it fails to find the mac |
| 296 | */ |
| 297 | private MacAddress getMacFromHostService(IpAddress targetIp) { |
| 298 | checkNotNull(targetIp); |
| 299 | |
| 300 | Host host = hostService.getHostsByIp(targetIp) |
| 301 | .stream() |
| 302 | .findFirst() |
| 303 | .orElse(null); |
| 304 | |
| 305 | if (host != null) { |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 306 | log.trace("Found MAC from host service for {}", targetIp); |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 307 | return host.mac(); |
| 308 | } else { |
| 309 | return MacAddress.NONE; |
| 310 | } |
| 311 | } |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 312 | |
| 313 | private class InternalPacketProcessor implements PacketProcessor { |
| 314 | |
| 315 | @Override |
| 316 | public void process(PacketContext context) { |
| 317 | if (context.isHandled()) { |
| 318 | return; |
| 319 | } |
| 320 | Ethernet ethPacket = context.inPacket().parsed(); |
| 321 | if (ethPacket == null || ethPacket.getEtherType() != Ethernet.TYPE_ARP) { |
| 322 | return; |
| 323 | } |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 324 | |
| 325 | ARP arpPacket = (ARP) ethPacket.getPayload(); |
| 326 | switch (arpPacket.getOpCode()) { |
| 327 | case ARP.OP_REQUEST: |
| 328 | processArpRequest(context, ethPacket); |
| 329 | break; |
| 330 | case ARP.OP_REPLY: |
| 331 | processArpReply(context, ethPacket); |
| 332 | break; |
| 333 | default: |
| 334 | break; |
| 335 | } |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
| 339 | @Override |
| 340 | public void instanceDetected(Instance instance) { |
| 341 | VtnService service = getVtnService(instance.serviceId()); |
| 342 | if (service == null) { |
| 343 | return; |
| 344 | } |
| 345 | |
| 346 | if (service.networkType().equals(PRIVATE)) { |
| 347 | log.trace("Added IP:{} MAC:{}", service.serviceIp(), privateGatewayMac); |
| 348 | addGateway(service.serviceIp(), privateGatewayMac); |
| 349 | // send gratuitous ARP for the existing VMs when ONOS is restarted |
| 350 | sendGratuitousArp(service.serviceIp(), Sets.newHashSet(instance)); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | @Override |
| 355 | public void instanceRemoved(Instance instance) { |
| 356 | VtnService service = getVtnService(instance.serviceId()); |
| 357 | if (service == null) { |
| 358 | return; |
| 359 | } |
| 360 | |
| 361 | // remove this network gateway from proxy ARP if no instance presents |
| 362 | if (service.networkType().equals(PRIVATE) && |
| 363 | getInstances(service.id()).isEmpty()) { |
| 364 | removeGateway(service.serviceIp()); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | @Override |
| 369 | protected void readConfiguration() { |
| 370 | CordVtnConfig config = configRegistry.getConfig(appId, CordVtnConfig.class); |
| 371 | if (config == null) { |
| 372 | log.debug("No configuration found"); |
| 373 | return; |
| 374 | } |
| 375 | |
| 376 | privateGatewayMac = config.privateGatewayMac(); |
| 377 | log.debug("Added private gateway MAC {}", privateGatewayMac); |
| 378 | |
| 379 | config.publicGateways().entrySet().stream().forEach(entry -> { |
| 380 | addGateway(entry.getKey(), entry.getValue()); |
| 381 | log.debug("Added public gateway IP {}, MAC {}", |
| 382 | entry.getKey(), entry.getValue()); |
| 383 | }); |
| 384 | // TODO send gratuitous arp in case the MAC is changed |
| 385 | |
| 386 | super.readConfiguration(); |
| 387 | } |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 388 | } |