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