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