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