Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | 6547c0e | 2017-08-03 18:48:25 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Foundation |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [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 | */ |
| 16 | |
| 17 | package org.onosproject.sdxl3.impl; |
| 18 | |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [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; |
| 25 | import org.apache.felix.scr.annotations.Service; |
| 26 | import org.onlab.packet.Ethernet; |
| 27 | import org.onlab.packet.IPv4; |
| 28 | import org.onlab.packet.IPv6; |
| 29 | import org.onlab.packet.IpAddress; |
| 30 | import org.onlab.packet.IpPrefix; |
| 31 | import org.onlab.packet.TpPort; |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 32 | import org.onlab.packet.VlanId; |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 33 | import org.onlab.util.ItemNotFoundException; |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 34 | import org.onosproject.core.ApplicationId; |
| 35 | import org.onosproject.core.CoreService; |
| 36 | import org.onosproject.incubator.net.intf.Interface; |
Andreas Papazois | 1f94191 | 2016-07-01 11:48:12 +0300 | [diff] [blame] | 37 | import org.onosproject.incubator.net.intf.InterfaceEvent; |
| 38 | import org.onosproject.incubator.net.intf.InterfaceListener; |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 39 | import org.onosproject.incubator.net.intf.InterfaceService; |
| 40 | import org.onosproject.net.ConnectPoint; |
| 41 | import org.onosproject.net.config.ConfigFactory; |
| 42 | import org.onosproject.net.config.NetworkConfigEvent; |
| 43 | import org.onosproject.net.config.NetworkConfigListener; |
| 44 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 45 | import org.onosproject.net.config.NetworkConfigService; |
| 46 | import org.onosproject.net.config.basics.SubjectFactories; |
| 47 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 48 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 49 | import org.onosproject.net.flow.TrafficSelector; |
| 50 | import org.onosproject.net.flow.TrafficTreatment; |
| 51 | import org.onosproject.net.host.InterfaceIpAddress; |
| 52 | import org.onosproject.net.intent.IntentUtils; |
| 53 | import org.onosproject.net.intent.Key; |
| 54 | import org.onosproject.net.intent.PointToPointIntent; |
| 55 | import org.onosproject.routing.IntentSynchronizationService; |
| 56 | import org.onosproject.routing.RoutingService; |
| 57 | import org.onosproject.routing.config.BgpConfig; |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 58 | import org.onosproject.sdxl3.SdxL3; |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 59 | import org.onosproject.sdxl3.SdxL3PeerService; |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 60 | import org.onosproject.sdxl3.config.SdxParticipantsConfig; |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 61 | import org.slf4j.Logger; |
| 62 | import org.slf4j.LoggerFactory; |
| 63 | |
| 64 | import java.util.ArrayList; |
| 65 | import java.util.Collection; |
| 66 | import java.util.HashMap; |
| 67 | import java.util.List; |
| 68 | import java.util.Map; |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 69 | import java.util.Optional; |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 70 | |
| 71 | import static com.google.common.base.Preconditions.checkNotNull; |
| 72 | |
| 73 | /** |
| 74 | * Manages the connectivity requirements between peers. |
| 75 | */ |
| 76 | @Service |
| 77 | @Component(immediate = true, enabled = false) |
| 78 | public class SdxL3PeerManager implements SdxL3PeerService { |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 79 | private static final int PRIORITY_OFFSET = 1000; |
| 80 | |
| 81 | private static final String SUFFIX_DST = "dst"; |
| 82 | private static final String SUFFIX_SRC = "src"; |
| 83 | private static final String SUFFIX_ICMP = "icmp"; |
| 84 | |
| 85 | private static final Logger log = LoggerFactory.getLogger( |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 86 | SdxL3PeerManager.class); |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 87 | |
| 88 | private static final short BGP_PORT = 179; |
| 89 | |
| 90 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 91 | protected CoreService coreService; |
| 92 | |
| 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 94 | protected NetworkConfigService configService; |
| 95 | |
| 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 97 | protected InterfaceService interfaceService; |
| 98 | |
| 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 100 | protected IntentSynchronizationService intentSynchronizer; |
| 101 | |
| 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 103 | protected NetworkConfigRegistry registry; |
| 104 | |
| 105 | private ConfigFactory configFactory = |
| 106 | new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 107 | CONFIG_CLASS, CONFIG_KEY) { |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 108 | @Override |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 109 | public SdxParticipantsConfig createConfig() { |
| 110 | return new SdxParticipantsConfig(); |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 111 | } |
| 112 | }; |
| 113 | |
| 114 | private ApplicationId sdxAppId; |
| 115 | private ApplicationId routerAppId; |
| 116 | |
| 117 | private final Map<Key, PointToPointIntent> peerIntents = new HashMap<>(); |
| 118 | |
| 119 | private final InternalNetworkConfigListener configListener |
| 120 | = new InternalNetworkConfigListener(); |
| 121 | |
Andreas Papazois | 1f94191 | 2016-07-01 11:48:12 +0300 | [diff] [blame] | 122 | private final InternalInterfaceListener interfaceListener |
| 123 | = new InternalInterfaceListener(); |
| 124 | |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 125 | @Activate |
| 126 | public void activate() { |
| 127 | sdxAppId = coreService.getAppId(SdxL3.SDX_L3_APP); |
| 128 | routerAppId = coreService.getAppId(RoutingService.ROUTER_APP_ID); |
| 129 | |
| 130 | registry.registerConfigFactory(configFactory); |
| 131 | |
| 132 | configService.addListener(configListener); |
Andreas Papazois | 1f94191 | 2016-07-01 11:48:12 +0300 | [diff] [blame] | 133 | interfaceService.addListener(interfaceListener); |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 134 | |
| 135 | setUpConnectivity(); |
| 136 | |
| 137 | log.info("Connectivity with BGP peers established"); |
| 138 | } |
| 139 | |
| 140 | @Deactivate |
| 141 | public void deactivate() { |
| 142 | configService.removeListener(configListener); |
Andreas Papazois | 1f94191 | 2016-07-01 11:48:12 +0300 | [diff] [blame] | 143 | interfaceService.removeListener(interfaceListener); |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 144 | |
| 145 | log.info("Connectivity with BGP peers stopped"); |
| 146 | } |
| 147 | |
| 148 | /** |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 149 | * Adds details for a BGP peer to the SDX-L3 configuration. |
| 150 | * |
| 151 | * @param peerName Peer name |
| 152 | * @param peerAddress Peer IP address |
| 153 | * @param port Connection point with peer |
| 154 | * @param interfaceName Name of the interface configured on port |
| 155 | */ |
| 156 | @Override |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 157 | public void addPeerDetails(String peerName, |
| 158 | IpAddress peerAddress, |
| 159 | ConnectPoint port, |
| 160 | String interfaceName) { |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 161 | |
| 162 | BgpConfig bgpConfig = getBgpConfig(); |
| 163 | if (bgpConfig == null) { |
| 164 | throw new ItemNotFoundException("BGP configuration not found"); |
| 165 | } |
| 166 | |
| 167 | if (!peerAddressExists(bgpConfig, peerAddress)) { |
| 168 | throw new ItemNotFoundException("Peer IP not found"); |
| 169 | } |
| 170 | |
| 171 | Interface peerInterface = getInterface(port, interfaceName); |
| 172 | if (peerInterface == null) { |
| 173 | throw new ItemNotFoundException("Interface not found"); |
| 174 | } |
| 175 | |
| 176 | if (!interfaceSubnetIncludesIp(peerInterface, peerAddress)) { |
| 177 | throw new IllegalArgumentException("Interface not configured for IP " |
| 178 | + peerAddress); |
| 179 | } |
| 180 | |
| 181 | Interface confInterface = getConfiguredInterfaceForPeer(peerAddress); |
| 182 | if (confInterface != null) { |
| 183 | if (confInterface.equals(peerInterface)) { |
| 184 | // Do nothing since the association exists. |
| 185 | return; |
| 186 | } else { |
| 187 | // The peer is associated with another interface. |
| 188 | throw new IllegalArgumentException("Peer details already exist"); |
| 189 | } |
| 190 | } |
| 191 | |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 192 | SdxParticipantsConfig peersConfig = |
| 193 | configService.addConfig(sdxAppId, SdxParticipantsConfig.class); |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 194 | if (peerName != null && peerNameExists(peersConfig, peerName)) { |
| 195 | throw new IllegalArgumentException("Peer name in use"); |
| 196 | } |
| 197 | |
| 198 | addPeerToConf(peersConfig, peerName, peerAddress, port, interfaceName); |
| 199 | configService. |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 200 | applyConfig(sdxAppId, SdxParticipantsConfig.class, peersConfig.node()); |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Removes details for a BGP peer to the SDX-L3 configuration. |
| 205 | * |
| 206 | * @param peerAddress Peer IP address |
| 207 | */ |
| 208 | @Override |
| 209 | public void removePeerDetails(IpAddress peerAddress) { |
| 210 | BgpConfig bgpConfig = getBgpConfig(); |
| 211 | if (bgpConfig == null) { |
| 212 | throw new ItemNotFoundException("BGP configuration not found"); |
| 213 | } |
| 214 | |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 215 | SdxParticipantsConfig peersConfig = |
| 216 | configService.addConfig(sdxAppId, SdxParticipantsConfig.class); |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 217 | |
| 218 | if (peersConfig.getPeerForIp(peerAddress) == null) { |
| 219 | throw new ItemNotFoundException("Peer details not found"); |
| 220 | } |
| 221 | |
| 222 | removePeerFromConf(peersConfig, peerAddress); |
| 223 | configService.applyConfig(sdxAppId, |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 224 | SdxParticipantsConfig.class, |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 225 | peersConfig.node()); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Returns BGP configuration has been specified and any BGP speaker |
| 230 | * exists in this configuration, else null is returned. |
| 231 | * |
| 232 | * @return BGP configuration or null |
| 233 | */ |
| 234 | private BgpConfig getBgpConfig() { |
| 235 | BgpConfig bgpConfig = configService. |
| 236 | getConfig(routerAppId, BgpConfig.class); |
| 237 | |
| 238 | if (bgpConfig == null || bgpConfig.bgpSpeakers().isEmpty()) { |
| 239 | return null; |
| 240 | } |
| 241 | return bgpConfig; |
| 242 | } |
| 243 | |
| 244 | private Interface getInterface(ConnectPoint port, String interfaceName) { |
| 245 | Optional<Interface> interfaceMatch = interfaceService |
| 246 | .getInterfacesByPort(port) |
| 247 | .stream() |
| 248 | .filter(intf -> intf.name().equals(interfaceName)) |
| 249 | .findFirst(); |
| 250 | if (!interfaceMatch.isPresent()) { |
| 251 | // No such interface name configured for given connectPoint |
| 252 | return null; |
| 253 | } |
| 254 | return interfaceMatch.get(); |
| 255 | } |
| 256 | |
Andreas Papazois | f0ec25b | 2016-02-05 14:20:06 +0200 | [diff] [blame] | 257 | private boolean interfaceSubnetIncludesIp(Interface peerInterface, IpAddress peerAddress) { |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 258 | if (peerInterface.ipAddressesList().stream() |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 259 | .anyMatch(intfIp -> intfIp.subnetAddress(). |
| 260 | contains(peerAddress))) { |
| 261 | // Interface configured subnet not including peer address |
| 262 | return true; |
| 263 | } |
| 264 | return false; |
| 265 | } |
| 266 | |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 267 | private boolean peerNameExists(SdxParticipantsConfig config, String peerName) { |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 268 | if (config.getPeerForName(Optional.of(peerName)) == null) { |
| 269 | return false; |
| 270 | } |
| 271 | return true; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Adds the peer to the SdxProvidersConfig . |
| 276 | * |
| 277 | * @param peersConfig the BGP peers configuration |
| 278 | */ |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 279 | private void addPeerToConf(SdxParticipantsConfig peersConfig, String peerName, |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 280 | IpAddress peerAddress, ConnectPoint port, |
| 281 | String interfaceName) { |
| 282 | log.debug("Adding peer with IP to configuration: {}", peerAddress); |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 283 | SdxParticipantsConfig.PeerConfig peer = new SdxParticipantsConfig. |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 284 | PeerConfig(Optional.ofNullable(peerName), peerAddress, |
| 285 | port, interfaceName); |
| 286 | |
| 287 | peersConfig.addPeer(peer); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Removes the speaker from the BgpConfig service. |
| 292 | * |
| 293 | * @param peersConfig the BGP peeers configuration |
| 294 | */ |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 295 | private void removePeerFromConf(SdxParticipantsConfig peersConfig, |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 296 | IpAddress peerAddress) { |
| 297 | log.debug("Removing peer details from configuration: {}", |
| 298 | peerAddress.toString()); |
| 299 | peersConfig.removePeer(peerAddress); |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Returns true if a given IP address has been specified as a BGP peer |
| 304 | * address in the network configuration. |
| 305 | * |
Andreas Papazois | f0ec25b | 2016-02-05 14:20:06 +0200 | [diff] [blame] | 306 | * @param bgpConfig BGP configuration |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 307 | * @param peerAddress IP address of peer |
| 308 | * @return whether address has been specified for a peer or not |
| 309 | */ |
| 310 | private Boolean peerAddressExists(BgpConfig bgpConfig, |
| 311 | IpAddress peerAddress) { |
| 312 | List<IpAddress> peeringAddresses = |
| 313 | getPeerAddresses(bgpConfig); |
| 314 | if (!peeringAddresses.contains(peerAddress)) { |
| 315 | return false; |
| 316 | } |
| 317 | return true; |
| 318 | } |
| 319 | |
| 320 | private Interface getConfiguredInterfaceForPeer(IpAddress peerAddress) { |
| 321 | if (sdxAppId == null) { |
| 322 | return null; |
| 323 | } |
| 324 | |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 325 | SdxParticipantsConfig config = configService.getConfig(sdxAppId, SdxParticipantsConfig.class); |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 326 | if (config == null) { |
| 327 | return null; |
| 328 | } |
| 329 | |
| 330 | ConnectPoint port = config.getPortForPeer(peerAddress); |
| 331 | String intfName = config.getInterfaceNameForPeer(peerAddress); |
| 332 | if (port != null && intfName != null) { |
| 333 | Optional<Interface> interfaceMatch = interfaceService |
| 334 | .getInterfacesByPort(port) |
| 335 | .stream() |
| 336 | .filter(intf -> intf.name().equals(intfName)) |
| 337 | .findFirst(); |
| 338 | if (interfaceMatch.isPresent()) { |
| 339 | return interfaceMatch.get(); |
| 340 | } |
| 341 | } |
| 342 | return null; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Returns the interface used as connection point to peer. |
| 347 | * |
| 348 | * @param peerAddress IP address of peer |
| 349 | * @return interface to the peer |
| 350 | */ |
| 351 | @Override |
| 352 | public Interface getInterfaceForPeer(IpAddress peerAddress) { |
| 353 | Interface peeringInterface = getConfiguredInterfaceForPeer(peerAddress); |
| 354 | if (peeringInterface == null) { |
| 355 | peeringInterface = interfaceService.getMatchingInterface(peerAddress); |
| 356 | } |
| 357 | return peeringInterface; |
| 358 | } |
| 359 | |
| 360 | @Override |
| 361 | public List<IpAddress> getPeerAddresses(BgpConfig bgpConfig) { |
| 362 | List<IpAddress> peeringAddresses = Lists.newArrayList(); |
| 363 | |
| 364 | List<BgpConfig.BgpSpeakerConfig> bgpSpeakers = |
| 365 | Lists.newArrayList(bgpConfig.bgpSpeakers()); |
| 366 | bgpSpeakers.forEach( |
| 367 | s -> peeringAddresses.addAll(s.peers())); |
| 368 | |
| 369 | return peeringAddresses; |
| 370 | } |
| 371 | |
| 372 | /** |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 373 | * Sets up paths to establish connectivity between all internal |
| 374 | * BGP speakers and external BGP peers. |
| 375 | */ |
| 376 | private void setUpConnectivity() { |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 377 | BgpConfig config = getBgpConfig(); |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 378 | if (config == null) { |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 379 | log.warn("No BGP configuration found"); |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 380 | return; |
| 381 | } |
| 382 | |
| 383 | Map<Key, PointToPointIntent> existingIntents = new HashMap<>(peerIntents); |
| 384 | |
| 385 | for (BgpConfig.BgpSpeakerConfig bgpSpeaker : config.bgpSpeakers()) { |
| 386 | log.debug("Start to set up BGP paths for BGP speaker: {}", |
| 387 | bgpSpeaker); |
| 388 | |
| 389 | buildSpeakerIntents(bgpSpeaker).forEach(i -> { |
| 390 | PointToPointIntent intent = existingIntents.remove(i.key()); |
| 391 | if (intent == null || !IntentUtils.intentsAreEqual(i, intent)) { |
| 392 | peerIntents.put(i.key(), i); |
| 393 | intentSynchronizer.submit(i); |
| 394 | } |
| 395 | }); |
| 396 | } |
| 397 | |
| 398 | // Remove any remaining intents that we used to have that we don't need |
| 399 | // anymore |
| 400 | existingIntents.values().forEach(i -> { |
| 401 | peerIntents.remove(i.key()); |
| 402 | intentSynchronizer.withdraw(i); |
| 403 | }); |
| 404 | } |
| 405 | |
| 406 | private Collection<PointToPointIntent> buildSpeakerIntents(BgpConfig.BgpSpeakerConfig speaker) { |
| 407 | List<PointToPointIntent> intents = new ArrayList<>(); |
| 408 | |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 409 | // Get the BGP Speaker VLAN Id |
| 410 | VlanId bgpSpeakerVlanId = speaker.vlan(); |
| 411 | |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 412 | for (IpAddress peerAddress : speaker.peers()) { |
| 413 | Interface peeringInterface = getInterfaceForPeer(peerAddress); |
| 414 | |
| 415 | if (peeringInterface == null) { |
| 416 | log.debug("No peering interface found for peer {} on speaker {}", |
| 417 | peerAddress, speaker); |
| 418 | continue; |
| 419 | } |
| 420 | |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 421 | IpAddress bgpSpeakerAddress = null; |
| 422 | for (InterfaceIpAddress address : peeringInterface.ipAddressesList()) { |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 423 | if (address.subnetAddress().contains(peerAddress)) { |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 424 | bgpSpeakerAddress = address.ipAddress(); |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 425 | break; |
| 426 | } |
| 427 | } |
| 428 | |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 429 | checkNotNull(bgpSpeakerAddress); |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 430 | |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 431 | VlanId peerVlanId = peeringInterface.vlan(); |
| 432 | |
| 433 | intents.addAll(buildIntents(speaker.connectPoint(), bgpSpeakerVlanId, |
| 434 | bgpSpeakerAddress, |
| 435 | peeringInterface.connectPoint(), |
| 436 | peerVlanId, |
| 437 | peerAddress)); |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | return intents; |
| 441 | } |
| 442 | |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 443 | /** |
Andreas Papazois | 1f94191 | 2016-07-01 11:48:12 +0300 | [diff] [blame] | 444 | * Builds the required intents between a BGP speaker and an external router. |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 445 | * |
Andreas Papazois | 1f94191 | 2016-07-01 11:48:12 +0300 | [diff] [blame] | 446 | * @param portOne the BGP speaker connect point |
| 447 | * @param vlanOne the BGP speaker VLAN |
| 448 | * @param ipOne the BGP speaker IP address |
| 449 | * @param portTwo the external BGP peer connect point |
| 450 | * @param vlanTwo the external BGP peer VLAN |
| 451 | * @param ipTwo the external BGP peer IP address |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 452 | * @return the intents to install |
| 453 | */ |
| 454 | private Collection<PointToPointIntent> buildIntents(ConnectPoint portOne, |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 455 | VlanId vlanOne, |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 456 | IpAddress ipOne, |
| 457 | ConnectPoint portTwo, |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 458 | VlanId vlanTwo, |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 459 | IpAddress ipTwo) { |
| 460 | |
| 461 | List<PointToPointIntent> intents = new ArrayList<>(); |
| 462 | |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 463 | TrafficTreatment.Builder treatmentToPeer = DefaultTrafficTreatment.builder(); |
| 464 | TrafficTreatment.Builder treatmentToSpeaker = DefaultTrafficTreatment.builder(); |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 465 | TrafficSelector selector; |
| 466 | Key key; |
| 467 | |
| 468 | byte tcpProtocol; |
| 469 | byte icmpProtocol; |
| 470 | |
| 471 | if (ipOne.isIp4()) { |
| 472 | tcpProtocol = IPv4.PROTOCOL_TCP; |
| 473 | icmpProtocol = IPv4.PROTOCOL_ICMP; |
| 474 | } else { |
| 475 | tcpProtocol = IPv6.PROTOCOL_TCP; |
| 476 | icmpProtocol = IPv6.PROTOCOL_ICMP6; |
| 477 | } |
| 478 | |
Andreas Papazois | 1f94191 | 2016-07-01 11:48:12 +0300 | [diff] [blame] | 479 | // Add VLAN treatment for traffic going from BGP speaker to BGP peer |
Andreas papazois | 47deeaa | 2016-10-18 12:19:52 +0300 | [diff] [blame] | 480 | treatmentToPeer = applyVlanTreatment(vlanOne, vlanTwo, treatmentToPeer); |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 481 | |
| 482 | // Path from BGP speaker to BGP peer matching destination TCP port 179 |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 483 | selector = buildSelector(tcpProtocol, |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 484 | vlanOne, |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 485 | ipOne, |
| 486 | ipTwo, |
| 487 | null, |
| 488 | BGP_PORT); |
| 489 | |
| 490 | key = buildKey(ipOne, ipTwo, SUFFIX_DST); |
| 491 | |
| 492 | intents.add(PointToPointIntent.builder() |
| 493 | .appId(sdxAppId) |
| 494 | .key(key) |
| 495 | .selector(selector) |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 496 | .treatment(treatmentToPeer.build()) |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 497 | .ingressPoint(portOne) |
| 498 | .egressPoint(portTwo) |
| 499 | .priority(PRIORITY_OFFSET) |
| 500 | .build()); |
| 501 | |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 502 | // Path from BGP speaker to BGP peer matching source TCP port 179 |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 503 | selector = buildSelector(tcpProtocol, |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 504 | vlanOne, |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 505 | ipOne, |
| 506 | ipTwo, |
| 507 | BGP_PORT, |
| 508 | null); |
| 509 | |
| 510 | key = buildKey(ipOne, ipTwo, SUFFIX_SRC); |
| 511 | |
| 512 | intents.add(PointToPointIntent.builder() |
| 513 | .appId(sdxAppId) |
| 514 | .key(key) |
| 515 | .selector(selector) |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 516 | .treatment(treatmentToPeer.build()) |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 517 | .ingressPoint(portOne) |
| 518 | .egressPoint(portTwo) |
| 519 | .priority(PRIORITY_OFFSET) |
| 520 | .build()); |
| 521 | |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 522 | // ICMP path from BGP speaker to BGP peer |
| 523 | selector = buildSelector(icmpProtocol, |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 524 | vlanOne, |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 525 | ipOne, |
| 526 | ipTwo, |
| 527 | null, |
| 528 | null); |
| 529 | |
| 530 | key = buildKey(ipOne, ipTwo, SUFFIX_ICMP); |
| 531 | |
| 532 | intents.add(PointToPointIntent.builder() |
| 533 | .appId(sdxAppId) |
| 534 | .key(key) |
| 535 | .selector(selector) |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 536 | .treatment(treatmentToPeer.build()) |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 537 | .ingressPoint(portOne) |
| 538 | .egressPoint(portTwo) |
| 539 | .priority(PRIORITY_OFFSET) |
| 540 | .build()); |
| 541 | |
Andreas Papazois | 1f94191 | 2016-07-01 11:48:12 +0300 | [diff] [blame] | 542 | // Add VLAN treatment for traffic going from BGP peer to BGP speaker |
Andreas papazois | 47deeaa | 2016-10-18 12:19:52 +0300 | [diff] [blame] | 543 | treatmentToSpeaker = applyVlanTreatment(vlanTwo, vlanOne, treatmentToSpeaker); |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 544 | |
| 545 | // Path from BGP peer to BGP speaker matching destination TCP port 179 |
| 546 | selector = buildSelector(tcpProtocol, |
| 547 | vlanTwo, |
| 548 | ipTwo, |
| 549 | ipOne, |
| 550 | null, |
| 551 | BGP_PORT); |
| 552 | |
| 553 | key = buildKey(ipTwo, ipOne, SUFFIX_DST); |
| 554 | |
| 555 | intents.add(PointToPointIntent.builder() |
| 556 | .appId(sdxAppId) |
| 557 | .key(key) |
| 558 | .selector(selector) |
| 559 | .treatment(treatmentToSpeaker.build()) |
| 560 | .ingressPoint(portTwo) |
| 561 | .egressPoint(portOne) |
| 562 | .priority(PRIORITY_OFFSET) |
| 563 | .build()); |
| 564 | |
| 565 | // Path from BGP peer to BGP speaker matching source TCP port 179 |
| 566 | selector = buildSelector(tcpProtocol, |
| 567 | vlanTwo, |
| 568 | ipTwo, |
| 569 | ipOne, |
| 570 | BGP_PORT, |
| 571 | null); |
| 572 | |
| 573 | key = buildKey(ipTwo, ipOne, SUFFIX_SRC); |
| 574 | |
| 575 | intents.add(PointToPointIntent.builder() |
| 576 | .appId(sdxAppId) |
| 577 | .key(key) |
| 578 | .selector(selector) |
| 579 | .treatment(treatmentToSpeaker.build()) |
| 580 | .ingressPoint(portTwo) |
| 581 | .egressPoint(portOne) |
| 582 | .priority(PRIORITY_OFFSET) |
| 583 | .build()); |
| 584 | |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 585 | // ICMP path from BGP peer to BGP speaker |
| 586 | selector = buildSelector(icmpProtocol, |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 587 | vlanTwo, |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 588 | ipTwo, |
| 589 | ipOne, |
| 590 | null, |
| 591 | null); |
| 592 | |
| 593 | key = buildKey(ipTwo, ipOne, SUFFIX_ICMP); |
| 594 | |
| 595 | intents.add(PointToPointIntent.builder() |
| 596 | .appId(sdxAppId) |
| 597 | .key(key) |
| 598 | .selector(selector) |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 599 | .treatment(treatmentToSpeaker.build()) |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 600 | .ingressPoint(portTwo) |
| 601 | .egressPoint(portOne) |
| 602 | .priority(PRIORITY_OFFSET) |
| 603 | .build()); |
| 604 | |
| 605 | return intents; |
| 606 | } |
| 607 | |
| 608 | /** |
| 609 | * Builds a traffic selector based on the set of input parameters. |
| 610 | * |
| 611 | * @param ipProto IP protocol |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 612 | * @param ingressVlanId VLAN Id configured on the ingress interface |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 613 | * @param srcIp source IP address |
| 614 | * @param dstIp destination IP address |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 615 | * @param srcTcpPort source TCP port, or null if shouldn't be set |
| 616 | * @param dstTcpPort destination TCP port, or null if shouldn't be set |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 617 | * @return the new traffic selector |
| 618 | */ |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 619 | private TrafficSelector buildSelector(byte ipProto, |
| 620 | VlanId ingressVlanId, |
| 621 | IpAddress srcIp, |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 622 | IpAddress dstIp, Short srcTcpPort, |
| 623 | Short dstTcpPort) { |
| 624 | TrafficSelector.Builder builder = DefaultTrafficSelector.builder().matchIPProtocol(ipProto); |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame] | 625 | // Match on any VLAN Id if a VLAN Id configured on the ingress interface |
| 626 | if (!ingressVlanId.equals(VlanId.NONE)) { |
| 627 | builder.matchVlanId(VlanId.ANY); |
| 628 | } |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 629 | |
| 630 | if (dstIp.isIp4()) { |
| 631 | builder.matchEthType(Ethernet.TYPE_IPV4) |
| 632 | .matchIPSrc(IpPrefix.valueOf(srcIp, IpPrefix.MAX_INET_MASK_LENGTH)) |
| 633 | .matchIPDst(IpPrefix.valueOf(dstIp, IpPrefix.MAX_INET_MASK_LENGTH)); |
| 634 | } else { |
| 635 | builder.matchEthType(Ethernet.TYPE_IPV6) |
| 636 | .matchIPv6Src(IpPrefix.valueOf(srcIp, IpPrefix.MAX_INET6_MASK_LENGTH)) |
| 637 | .matchIPv6Dst(IpPrefix.valueOf(dstIp, IpPrefix.MAX_INET6_MASK_LENGTH)); |
| 638 | } |
| 639 | |
| 640 | if (srcTcpPort != null) { |
| 641 | builder.matchTcpSrc(TpPort.tpPort(srcTcpPort)); |
| 642 | } |
| 643 | |
| 644 | if (dstTcpPort != null) { |
| 645 | builder.matchTcpDst(TpPort.tpPort(dstTcpPort)); |
| 646 | } |
| 647 | |
| 648 | return builder.build(); |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * Builds an intent Key for a point-to-point intent based off the source |
| 653 | * and destination IP address, as well as a suffix String to distinguish |
| 654 | * between different types of intents between the same source and |
| 655 | * destination. |
| 656 | * |
| 657 | * @param srcIp source IP address |
| 658 | * @param dstIp destination IP address |
| 659 | * @param suffix suffix string |
| 660 | * @return intent key |
| 661 | */ |
| 662 | private Key buildKey(IpAddress srcIp, IpAddress dstIp, String suffix) { |
| 663 | String keyString = new StringBuilder() |
| 664 | .append(srcIp.toString()) |
| 665 | .append("-") |
| 666 | .append(dstIp.toString()) |
| 667 | .append("-") |
| 668 | .append(suffix) |
| 669 | .toString(); |
| 670 | |
| 671 | return Key.of(keyString, sdxAppId); |
| 672 | } |
| 673 | |
Andreas papazois | 47deeaa | 2016-10-18 12:19:52 +0300 | [diff] [blame] | 674 | /** |
| 675 | * Adds the VLAN Id treatment before building the intents, depending on how |
| 676 | * the VLAN Ids of the BGP speakers and the BGP peers are configured. |
| 677 | */ |
| 678 | private TrafficTreatment.Builder applyVlanTreatment(VlanId vlanOne, |
| 679 | VlanId vlanTwo, |
| 680 | TrafficTreatment.Builder treatment) { |
| 681 | if (!vlanOne.equals(vlanTwo)) { |
| 682 | // VLANs are different. Do some VLAN treatment |
| 683 | if (vlanTwo.equals(VlanId.NONE)) { |
| 684 | // VLAN two is none. VLAN one is set. Do a pop |
| 685 | treatment.popVlan(); |
| 686 | } else { |
| 687 | // Either both VLANs are set or vlanOne is not |
| 688 | if (vlanOne.equals(VlanId.NONE)) { |
| 689 | // VLAN one is none. VLAN two is set. Push the VLAN header |
| 690 | treatment.pushVlan(); |
| 691 | } |
| 692 | // Set the VLAN Id to the egress VLAN Id |
| 693 | treatment.setVlanId(vlanTwo); |
| 694 | } |
| 695 | } |
| 696 | return treatment; |
| 697 | } |
| 698 | |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 699 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 700 | |
| 701 | @Override |
| 702 | public void event(NetworkConfigEvent event) { |
| 703 | switch (event.type()) { |
| 704 | case CONFIG_REGISTERED: |
| 705 | break; |
| 706 | case CONFIG_UNREGISTERED: |
| 707 | break; |
| 708 | case CONFIG_ADDED: |
| 709 | case CONFIG_UPDATED: |
| 710 | case CONFIG_REMOVED: |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 711 | if (event.configClass() == RoutingService.CONFIG_CLASS || |
| 712 | event.configClass() == CONFIG_CLASS) { |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 713 | setUpConnectivity(); |
| 714 | } |
| 715 | break; |
| 716 | default: |
| 717 | break; |
| 718 | } |
| 719 | } |
| 720 | } |
| 721 | |
Andreas Papazois | 1f94191 | 2016-07-01 11:48:12 +0300 | [diff] [blame] | 722 | private class InternalInterfaceListener implements InterfaceListener { |
| 723 | @Override |
| 724 | public void event(InterfaceEvent event) { |
| 725 | switch (event.type()) { |
| 726 | case INTERFACE_ADDED: |
| 727 | case INTERFACE_UPDATED: |
| 728 | case INTERFACE_REMOVED: |
| 729 | setUpConnectivity(); |
| 730 | break; |
| 731 | default: |
| 732 | break; |
| 733 | } |
| 734 | } |
| 735 | } |
| 736 | |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 737 | } |