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