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