Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.sdnip; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 18 | import org.onlab.packet.Ethernet; |
| 19 | import org.onlab.packet.IPv4; |
Kunihiro Ishiguro | 6e2ee15 | 2015-01-20 16:57:54 -0800 | [diff] [blame] | 20 | import org.onlab.packet.IPv6; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 21 | import org.onlab.packet.IpAddress; |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 22 | import org.onlab.packet.IpPrefix; |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 23 | import org.onlab.packet.TpPort; |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 24 | import org.onlab.packet.VlanId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.core.ApplicationId; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 26 | import org.onosproject.incubator.net.intf.Interface; |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 27 | import org.onosproject.incubator.net.intf.InterfaceEvent; |
| 28 | import org.onosproject.incubator.net.intf.InterfaceListener; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 29 | import org.onosproject.incubator.net.intf.InterfaceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.ConnectPoint; |
Jonathan Hart | 08105be | 2015-10-10 18:31:06 -0700 | [diff] [blame] | 31 | import org.onosproject.net.config.NetworkConfigEvent; |
| 32 | import org.onosproject.net.config.NetworkConfigListener; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 33 | import org.onosproject.net.config.NetworkConfigService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 35 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 36 | import org.onosproject.net.flow.TrafficSelector; |
| 37 | import org.onosproject.net.flow.TrafficTreatment; |
Jonathan Hart | 2a9ea49 | 2015-07-30 15:53:04 -0700 | [diff] [blame] | 38 | import org.onosproject.net.host.InterfaceIpAddress; |
Luca Prete | 86ac7d1 | 2015-12-02 23:36:49 -0800 | [diff] [blame] | 39 | import org.onosproject.net.intent.IntentUtils; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 40 | import org.onosproject.net.intent.Key; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import org.onosproject.net.intent.PointToPointIntent; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 42 | import org.onosproject.routing.IntentSynchronizationService; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 43 | import org.onosproject.routing.RoutingService; |
| 44 | import org.onosproject.routing.config.BgpConfig; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 45 | import org.slf4j.Logger; |
| 46 | import org.slf4j.LoggerFactory; |
| 47 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 48 | import java.util.ArrayList; |
| 49 | import java.util.Collection; |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 50 | import java.util.Collections; |
Jonathan Hart | 08105be | 2015-10-10 18:31:06 -0700 | [diff] [blame] | 51 | import java.util.HashMap; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 52 | import java.util.List; |
Jonathan Hart | 08105be | 2015-10-10 18:31:06 -0700 | [diff] [blame] | 53 | import java.util.Map; |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 54 | import java.util.Set; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 55 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 56 | import static com.google.common.base.Preconditions.checkNotNull; |
| 57 | |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 58 | /** |
| 59 | * Manages the connectivity requirements between peers. |
| 60 | */ |
Jonathan Hart | ce430a4 | 2014-10-16 20:44:29 -0700 | [diff] [blame] | 61 | public class PeerConnectivityManager { |
Pavlin Radoslavov | 2aa1f32 | 2015-03-11 17:59:44 -0700 | [diff] [blame] | 62 | private static final int PRIORITY_OFFSET = 1000; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 63 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 64 | private static final String SUFFIX_DST = "dst"; |
| 65 | private static final String SUFFIX_SRC = "src"; |
| 66 | private static final String SUFFIX_ICMP = "icmp"; |
| 67 | |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 68 | private static final Logger log = LoggerFactory.getLogger( |
Jonathan Hart | ce430a4 | 2014-10-16 20:44:29 -0700 | [diff] [blame] | 69 | PeerConnectivityManager.class); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 70 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 71 | private static final short BGP_PORT = 179; |
| 72 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 73 | private final IntentSynchronizationService intentSynchronizer; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 74 | private final NetworkConfigService configService; |
| 75 | private final InterfaceService interfaceService; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 76 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 77 | private final ApplicationId appId; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 78 | private final ApplicationId routerAppId; |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 79 | |
Jonathan Hart | 08105be | 2015-10-10 18:31:06 -0700 | [diff] [blame] | 80 | private final Map<Key, PointToPointIntent> peerIntents; |
| 81 | |
| 82 | private final InternalNetworkConfigListener configListener |
| 83 | = new InternalNetworkConfigListener(); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 84 | |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 85 | private final InternalInterfaceListener interfaceListener |
| 86 | = new InternalInterfaceListener(); |
| 87 | |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 88 | /** |
| 89 | * Creates a new PeerConnectivityManager. |
| 90 | * |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 91 | * @param appId the application ID |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 92 | * @param intentSynchronizer the intent synchronizer |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 93 | * @param configService the network config service |
Ray Milkey | 9b36d81 | 2015-09-09 15:24:54 -0700 | [diff] [blame] | 94 | * @param routerAppId application ID |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 95 | * @param interfaceService the interface service |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 96 | */ |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 97 | public PeerConnectivityManager(ApplicationId appId, |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 98 | IntentSynchronizationService intentSynchronizer, |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 99 | NetworkConfigService configService, |
| 100 | ApplicationId routerAppId, |
| 101 | InterfaceService interfaceService) { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 102 | this.appId = appId; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 103 | this.intentSynchronizer = intentSynchronizer; |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 104 | this.configService = configService; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 105 | this.routerAppId = routerAppId; |
| 106 | this.interfaceService = interfaceService; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 107 | |
Jonathan Hart | 08105be | 2015-10-10 18:31:06 -0700 | [diff] [blame] | 108 | peerIntents = new HashMap<>(); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 111 | /** |
| 112 | * Starts the peer connectivity manager. |
| 113 | */ |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 114 | public void start() { |
Jonathan Hart | 08105be | 2015-10-10 18:31:06 -0700 | [diff] [blame] | 115 | configService.addListener(configListener); |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 116 | interfaceService.addListener(interfaceListener); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 117 | setUpConnectivity(); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 121 | * Stops the peer connectivity manager. |
| 122 | */ |
| 123 | public void stop() { |
Jonathan Hart | 08105be | 2015-10-10 18:31:06 -0700 | [diff] [blame] | 124 | configService.removeListener(configListener); |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 125 | interfaceService.removeListener(interfaceListener); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 129 | * Sets up paths to establish connectivity between all internal |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 130 | * BGP speakers and external BGP peers. |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 131 | */ |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 132 | private void setUpConnectivity() { |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 133 | BgpConfig config = configService.getConfig(routerAppId, RoutingService.CONFIG_CLASS); |
| 134 | |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 135 | Set<BgpConfig.BgpSpeakerConfig> bgpSpeakers; |
| 136 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 137 | if (config == null) { |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 138 | log.warn("No BGP config available"); |
| 139 | bgpSpeakers = Collections.emptySet(); |
| 140 | } else { |
| 141 | bgpSpeakers = config.bgpSpeakers(); |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 142 | } |
| 143 | |
Jonathan Hart | 08105be | 2015-10-10 18:31:06 -0700 | [diff] [blame] | 144 | Map<Key, PointToPointIntent> existingIntents = new HashMap<>(peerIntents); |
| 145 | |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 146 | for (BgpConfig.BgpSpeakerConfig bgpSpeaker : bgpSpeakers) { |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 147 | log.debug("Start to set up BGP paths for BGP speaker: {}", |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 148 | bgpSpeaker); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 149 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 150 | buildSpeakerIntents(bgpSpeaker).forEach(i -> { |
Jonathan Hart | 08105be | 2015-10-10 18:31:06 -0700 | [diff] [blame] | 151 | PointToPointIntent intent = existingIntents.remove(i.key()); |
Ray Milkey | 4fd3ceb | 2015-12-10 14:43:08 -0800 | [diff] [blame] | 152 | if (intent == null || !IntentUtils.intentsAreEqual(i, intent)) { |
Jonathan Hart | 08105be | 2015-10-10 18:31:06 -0700 | [diff] [blame] | 153 | peerIntents.put(i.key(), i); |
| 154 | intentSynchronizer.submit(i); |
| 155 | } |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 156 | }); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 157 | } |
Jonathan Hart | 08105be | 2015-10-10 18:31:06 -0700 | [diff] [blame] | 158 | |
| 159 | // Remove any remaining intents that we used to have that we don't need |
| 160 | // anymore |
| 161 | existingIntents.values().forEach(i -> { |
| 162 | peerIntents.remove(i.key()); |
| 163 | intentSynchronizer.withdraw(i); |
| 164 | }); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 167 | private Collection<PointToPointIntent> buildSpeakerIntents(BgpConfig.BgpSpeakerConfig speaker) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 168 | List<PointToPointIntent> intents = new ArrayList<>(); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 169 | |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 170 | // Get the BGP Speaker VLAN Id |
| 171 | VlanId bgpSpeakerVlanId = speaker.vlan(); |
| 172 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 173 | for (IpAddress peerAddress : speaker.peers()) { |
| 174 | Interface peeringInterface = interfaceService.getMatchingInterface(peerAddress); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 175 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 176 | if (peeringInterface == null) { |
| 177 | log.debug("No peering interface found for peer {} on speaker {}", |
| 178 | peerAddress, speaker); |
Jonathan Hart | e30fcda | 2015-08-06 16:22:34 -0700 | [diff] [blame] | 179 | continue; |
| 180 | } |
| 181 | |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 182 | IpAddress bgpSpeakerAddress = null; |
| 183 | for (InterfaceIpAddress address : peeringInterface.ipAddressesList()) { |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 184 | if (address.subnetAddress().contains(peerAddress)) { |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 185 | bgpSpeakerAddress = address.ipAddress(); |
Jonathan Hart | e30fcda | 2015-08-06 16:22:34 -0700 | [diff] [blame] | 186 | break; |
Jonathan Hart | 2a9ea49 | 2015-07-30 15:53:04 -0700 | [diff] [blame] | 187 | } |
Jonathan Hart | e30fcda | 2015-08-06 16:22:34 -0700 | [diff] [blame] | 188 | } |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 189 | |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 190 | checkNotNull(bgpSpeakerAddress); |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 191 | |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 192 | VlanId peerVlanId = peeringInterface.vlan(); |
| 193 | |
| 194 | intents.addAll(buildIntents(speaker.connectPoint(), bgpSpeakerVlanId, |
| 195 | bgpSpeakerAddress, |
| 196 | peeringInterface.connectPoint(), |
| 197 | peerVlanId, |
| 198 | peerAddress)); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 201 | return intents; |
| 202 | } |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 203 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 204 | /** |
Luca Prete | e951151 | 2016-05-13 10:30:19 -0700 | [diff] [blame] | 205 | * Builds the required intents between a BGP speaker and an external router. |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 206 | * |
Luca Prete | e951151 | 2016-05-13 10:30:19 -0700 | [diff] [blame] | 207 | * @param portOne the BGP speaker connect point |
| 208 | * @param vlanOne the BGP speaker VLAN |
| 209 | * @param ipOne the BGP speaker IP address |
| 210 | * @param portTwo the external BGP peer connect point |
| 211 | * @param vlanTwo the external BGP peer VLAN |
| 212 | * @param ipTwo the external BGP peer IP address |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 213 | * @return the intents to install |
| 214 | */ |
| 215 | private Collection<PointToPointIntent> buildIntents(ConnectPoint portOne, |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 216 | VlanId vlanOne, |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 217 | IpAddress ipOne, |
| 218 | ConnectPoint portTwo, |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 219 | VlanId vlanTwo, |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 220 | IpAddress ipTwo) { |
| 221 | |
| 222 | List<PointToPointIntent> intents = new ArrayList<>(); |
Kunihiro Ishiguro | 6e2ee15 | 2015-01-20 16:57:54 -0800 | [diff] [blame] | 223 | |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 224 | TrafficTreatment.Builder treatmentToPeer = DefaultTrafficTreatment.builder(); |
| 225 | TrafficTreatment.Builder treatmentToSpeaker = DefaultTrafficTreatment.builder(); |
| 226 | |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 227 | TrafficSelector selector; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 228 | Key key; |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 229 | |
Kunihiro Ishiguro | 6e2ee15 | 2015-01-20 16:57:54 -0800 | [diff] [blame] | 230 | byte tcpProtocol; |
| 231 | byte icmpProtocol; |
| 232 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 233 | if (ipOne.isIp4()) { |
Kunihiro Ishiguro | 6e2ee15 | 2015-01-20 16:57:54 -0800 | [diff] [blame] | 234 | tcpProtocol = IPv4.PROTOCOL_TCP; |
| 235 | icmpProtocol = IPv4.PROTOCOL_ICMP; |
| 236 | } else { |
| 237 | tcpProtocol = IPv6.PROTOCOL_TCP; |
| 238 | icmpProtocol = IPv6.PROTOCOL_ICMP6; |
| 239 | } |
| 240 | |
Luca Prete | e951151 | 2016-05-13 10:30:19 -0700 | [diff] [blame] | 241 | // Add VLAN treatment for traffic going from BGP speaker to BGP peer |
Luca Prete | e4a5e1a | 2016-09-07 17:01:22 -0700 | [diff] [blame] | 242 | treatmentToPeer = applyVlanTreatment(vlanOne, vlanTwo, treatmentToPeer); |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 243 | |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 244 | // Path from BGP speaker to BGP peer matching destination TCP port 179 |
Kunihiro Ishiguro | 6e2ee15 | 2015-01-20 16:57:54 -0800 | [diff] [blame] | 245 | selector = buildSelector(tcpProtocol, |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 246 | vlanOne, |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 247 | ipOne, |
| 248 | ipTwo, |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 249 | null, |
| 250 | BGP_PORT); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 251 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 252 | key = buildKey(ipOne, ipTwo, SUFFIX_DST); |
| 253 | |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 254 | intents.add(PointToPointIntent.builder() |
| 255 | .appId(appId) |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 256 | .key(key) |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 257 | .selector(selector) |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 258 | .treatment(treatmentToPeer.build()) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 259 | .ingressPoint(portOne) |
| 260 | .egressPoint(portTwo) |
| 261 | .priority(PRIORITY_OFFSET) |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 262 | .build()); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 263 | |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 264 | // Path from BGP speaker to BGP peer matching source TCP port 179 |
Kunihiro Ishiguro | 6e2ee15 | 2015-01-20 16:57:54 -0800 | [diff] [blame] | 265 | selector = buildSelector(tcpProtocol, |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 266 | vlanOne, |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 267 | ipOne, |
| 268 | ipTwo, |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 269 | BGP_PORT, |
| 270 | null); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 271 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 272 | key = buildKey(ipOne, ipTwo, SUFFIX_SRC); |
| 273 | |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 274 | intents.add(PointToPointIntent.builder() |
| 275 | .appId(appId) |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 276 | .key(key) |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 277 | .selector(selector) |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 278 | .treatment(treatmentToPeer.build()) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 279 | .ingressPoint(portOne) |
| 280 | .egressPoint(portTwo) |
| 281 | .priority(PRIORITY_OFFSET) |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 282 | .build()); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 283 | |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 284 | // ICMP path from BGP speaker to BGP peer |
| 285 | selector = buildSelector(icmpProtocol, |
| 286 | vlanOne, |
| 287 | ipOne, |
| 288 | ipTwo, |
| 289 | null, |
| 290 | null); |
| 291 | |
| 292 | key = buildKey(ipOne, ipTwo, SUFFIX_ICMP); |
| 293 | |
| 294 | intents.add(PointToPointIntent.builder() |
| 295 | .appId(appId) |
| 296 | .key(key) |
| 297 | .selector(selector) |
| 298 | .treatment(treatmentToPeer.build()) |
| 299 | .ingressPoint(portOne) |
| 300 | .egressPoint(portTwo) |
| 301 | .priority(PRIORITY_OFFSET) |
| 302 | .build()); |
| 303 | |
Luca Prete | e951151 | 2016-05-13 10:30:19 -0700 | [diff] [blame] | 304 | // Add VLAN treatment for traffic going from BGP peer to BGP speaker |
Luca Prete | e4a5e1a | 2016-09-07 17:01:22 -0700 | [diff] [blame] | 305 | treatmentToSpeaker = applyVlanTreatment(vlanTwo, vlanOne, treatmentToSpeaker); |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 306 | |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 307 | // Path from BGP peer to BGP speaker matching destination TCP port 179 |
Kunihiro Ishiguro | 6e2ee15 | 2015-01-20 16:57:54 -0800 | [diff] [blame] | 308 | selector = buildSelector(tcpProtocol, |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 309 | vlanTwo, |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 310 | ipTwo, |
| 311 | ipOne, |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 312 | null, |
| 313 | BGP_PORT); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 314 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 315 | key = buildKey(ipTwo, ipOne, SUFFIX_DST); |
| 316 | |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 317 | intents.add(PointToPointIntent.builder() |
| 318 | .appId(appId) |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 319 | .key(key) |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 320 | .selector(selector) |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 321 | .treatment(treatmentToSpeaker.build()) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 322 | .ingressPoint(portTwo) |
| 323 | .egressPoint(portOne) |
| 324 | .priority(PRIORITY_OFFSET) |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 325 | .build()); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 326 | |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 327 | // Path from BGP peer to BGP speaker matching source TCP port 179 |
Kunihiro Ishiguro | 6e2ee15 | 2015-01-20 16:57:54 -0800 | [diff] [blame] | 328 | selector = buildSelector(tcpProtocol, |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 329 | vlanTwo, |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 330 | ipTwo, |
| 331 | ipOne, |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 332 | BGP_PORT, |
| 333 | null); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 334 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 335 | key = buildKey(ipTwo, ipOne, SUFFIX_SRC); |
| 336 | |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 337 | intents.add(PointToPointIntent.builder() |
| 338 | .appId(appId) |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 339 | .key(key) |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 340 | .selector(selector) |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 341 | .treatment(treatmentToSpeaker.build()) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 342 | .ingressPoint(portTwo) |
| 343 | .egressPoint(portOne) |
| 344 | .priority(PRIORITY_OFFSET) |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 345 | .build()); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 346 | |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 347 | // ICMP path from BGP peer to BGP speaker |
Kunihiro Ishiguro | 6e2ee15 | 2015-01-20 16:57:54 -0800 | [diff] [blame] | 348 | selector = buildSelector(icmpProtocol, |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 349 | vlanTwo, |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 350 | ipTwo, |
| 351 | ipOne, |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 352 | null, |
| 353 | null); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 354 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 355 | key = buildKey(ipTwo, ipOne, SUFFIX_ICMP); |
| 356 | |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 357 | intents.add(PointToPointIntent.builder() |
| 358 | .appId(appId) |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 359 | .key(key) |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 360 | .selector(selector) |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 361 | .treatment(treatmentToSpeaker.build()) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 362 | .ingressPoint(portTwo) |
| 363 | .egressPoint(portOne) |
| 364 | .priority(PRIORITY_OFFSET) |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 365 | .build()); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 366 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 367 | return intents; |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Builds a traffic selector based on the set of input parameters. |
| 372 | * |
| 373 | * @param ipProto IP protocol |
| 374 | * @param srcIp source IP address |
| 375 | * @param dstIp destination IP address |
| 376 | * @param srcTcpPort source TCP port, or null if shouldn't be set |
| 377 | * @param dstTcpPort destination TCP port, or null if shouldn't be set |
| 378 | * @return the new traffic selector |
| 379 | */ |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 380 | private TrafficSelector buildSelector(byte ipProto, VlanId ingressVlanId, |
| 381 | IpAddress srcIp, |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 382 | IpAddress dstIp, Short srcTcpPort, |
| 383 | Short dstTcpPort) { |
Pier Luigi Ventre | b38243f | 2015-11-11 13:01:37 +0100 | [diff] [blame] | 384 | TrafficSelector.Builder builder = DefaultTrafficSelector.builder().matchIPProtocol(ipProto); |
Kunihiro Ishiguro | 6e2ee15 | 2015-01-20 16:57:54 -0800 | [diff] [blame] | 385 | |
Luca Prete | e4a5e1a | 2016-09-07 17:01:22 -0700 | [diff] [blame] | 386 | // Match on VLAN Id if a VLAN Id configured on the ingress interface |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 387 | if (!ingressVlanId.equals(VlanId.NONE)) { |
Luca Prete | e4a5e1a | 2016-09-07 17:01:22 -0700 | [diff] [blame] | 388 | builder.matchVlanId(ingressVlanId); |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Pavlin Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 391 | if (dstIp.isIp4()) { |
Pier Luigi Ventre | b38243f | 2015-11-11 13:01:37 +0100 | [diff] [blame] | 392 | builder.matchEthType(Ethernet.TYPE_IPV4) |
| 393 | .matchIPSrc(IpPrefix.valueOf(srcIp, IpPrefix.MAX_INET_MASK_LENGTH)) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 394 | .matchIPDst(IpPrefix.valueOf(dstIp, IpPrefix.MAX_INET_MASK_LENGTH)); |
Kunihiro Ishiguro | 6e2ee15 | 2015-01-20 16:57:54 -0800 | [diff] [blame] | 395 | } else { |
Pier Luigi Ventre | b38243f | 2015-11-11 13:01:37 +0100 | [diff] [blame] | 396 | builder.matchEthType(Ethernet.TYPE_IPV6) |
| 397 | .matchIPv6Src(IpPrefix.valueOf(srcIp, IpPrefix.MAX_INET6_MASK_LENGTH)) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 398 | .matchIPv6Dst(IpPrefix.valueOf(dstIp, IpPrefix.MAX_INET6_MASK_LENGTH)); |
Kunihiro Ishiguro | 6e2ee15 | 2015-01-20 16:57:54 -0800 | [diff] [blame] | 399 | } |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 400 | |
| 401 | if (srcTcpPort != null) { |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 402 | builder.matchTcpSrc(TpPort.tpPort(srcTcpPort)); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | if (dstTcpPort != null) { |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 406 | builder.matchTcpDst(TpPort.tpPort(dstTcpPort)); |
Jonathan Hart | 6ec6829 | 2014-11-14 15:09:30 -0800 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | return builder.build(); |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Luca Prete | e4a5e1a | 2016-09-07 17:01:22 -0700 | [diff] [blame] | 412 | /* |
| 413 | * Adds the VLAN Id treatment before building the intents, depending on how |
| 414 | * the VLAN Ids of the BGP speakers and the BGP peers are configured. |
| 415 | */ |
| 416 | private TrafficTreatment.Builder applyVlanTreatment(VlanId vlanOne, |
| 417 | VlanId vlanTwo, |
| 418 | TrafficTreatment.Builder treatment) { |
| 419 | if (!vlanOne.equals(vlanTwo)) { |
| 420 | // VLANs are different. Do some VLAN treatment |
| 421 | if (vlanTwo.equals(VlanId.NONE)) { |
| 422 | // VLAN two is none. VLAN one is set. Do a pop |
| 423 | treatment.popVlan(); |
| 424 | } else { |
| 425 | // Either both VLANs are set or vlanOne is not |
| 426 | if (vlanOne.equals(VlanId.NONE)) { |
| 427 | // VLAN one is none. VLAN two is set. Push the VLAN header |
| 428 | treatment.pushVlan(); |
| 429 | } |
| 430 | // Set the VLAN Id to the egress VLAN Id |
| 431 | treatment.setVlanId(vlanTwo); |
| 432 | } |
| 433 | } |
| 434 | return treatment; |
| 435 | } |
| 436 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 437 | /** |
| 438 | * Builds an intent Key for a point-to-point intent based off the source |
| 439 | * and destination IP address, as well as a suffix String to distinguish |
| 440 | * between different types of intents between the same source and |
| 441 | * destination. |
| 442 | * |
| 443 | * @param srcIp source IP address |
| 444 | * @param dstIp destination IP address |
| 445 | * @param suffix suffix string |
Jonathan Hart | 08105be | 2015-10-10 18:31:06 -0700 | [diff] [blame] | 446 | * @return intent key |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 447 | */ |
| 448 | private Key buildKey(IpAddress srcIp, IpAddress dstIp, String suffix) { |
| 449 | String keyString = new StringBuilder() |
| 450 | .append(srcIp.toString()) |
| 451 | .append("-") |
| 452 | .append(dstIp.toString()) |
| 453 | .append("-") |
| 454 | .append(suffix) |
| 455 | .toString(); |
| 456 | |
| 457 | return Key.of(keyString, appId); |
| 458 | } |
| 459 | |
Jonathan Hart | 08105be | 2015-10-10 18:31:06 -0700 | [diff] [blame] | 460 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 461 | |
| 462 | @Override |
| 463 | public void event(NetworkConfigEvent event) { |
| 464 | switch (event.type()) { |
| 465 | case CONFIG_REGISTERED: |
| 466 | break; |
| 467 | case CONFIG_UNREGISTERED: |
| 468 | break; |
| 469 | case CONFIG_ADDED: |
| 470 | case CONFIG_UPDATED: |
| 471 | case CONFIG_REMOVED: |
| 472 | if (event.configClass() == RoutingService.CONFIG_CLASS) { |
| 473 | setUpConnectivity(); |
| 474 | } |
| 475 | break; |
| 476 | default: |
| 477 | break; |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 482 | private class InternalInterfaceListener implements InterfaceListener { |
| 483 | @Override |
| 484 | public void event(InterfaceEvent event) { |
| 485 | switch (event.type()) { |
| 486 | case INTERFACE_ADDED: |
| 487 | case INTERFACE_UPDATED: |
| 488 | case INTERFACE_REMOVED: |
| 489 | setUpConnectivity(); |
| 490 | break; |
| 491 | default: |
| 492 | break; |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 497 | } |