Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.sdnip; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 17 | |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 18 | import java.util.Collection; |
| 19 | import java.util.HashMap; |
| 20 | import java.util.HashSet; |
| 21 | import java.util.LinkedList; |
| 22 | import java.util.List; |
| 23 | import java.util.Map; |
| 24 | import java.util.Objects; |
| 25 | import java.util.Set; |
| 26 | import java.util.concurrent.ConcurrentHashMap; |
| 27 | import java.util.concurrent.ExecutorService; |
| 28 | import java.util.concurrent.Executors; |
| 29 | import java.util.concurrent.Semaphore; |
| 30 | |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 31 | import org.onlab.packet.Ethernet; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 32 | import org.onlab.packet.IpAddress; |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 33 | import org.onlab.packet.IpPrefix; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 34 | import org.onlab.packet.MacAddress; |
| 35 | import org.onlab.packet.VlanId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.core.ApplicationId; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 37 | import org.onosproject.net.ConnectPoint; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame^] | 38 | import org.onosproject.net.Host; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 39 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 40 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 41 | import org.onosproject.net.flow.TrafficSelector; |
| 42 | import org.onosproject.net.flow.TrafficTreatment; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 43 | import org.onosproject.net.flow.criteria.Criteria.IPCriterion; |
| 44 | import org.onosproject.net.flow.criteria.Criterion; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame^] | 45 | import org.onosproject.net.host.HostService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 46 | import org.onosproject.net.intent.Intent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 47 | import org.onosproject.net.intent.IntentService; |
| 48 | import org.onosproject.net.intent.IntentState; |
Pavlin Radoslavov | 2aa1f32 | 2015-03-11 17:59:44 -0700 | [diff] [blame] | 49 | import org.onosproject.net.intent.Key; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 50 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
| 51 | import org.onosproject.net.intent.PointToPointIntent; |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 52 | import org.onosproject.routing.FibListener; |
| 53 | import org.onosproject.routing.FibUpdate; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame^] | 54 | import org.onosproject.routing.IntentRequestListener; |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 55 | import org.onosproject.routing.config.BgpPeer; |
| 56 | import org.onosproject.routing.config.Interface; |
| 57 | import org.onosproject.routing.config.RoutingConfigurationService; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 58 | import org.slf4j.Logger; |
| 59 | import org.slf4j.LoggerFactory; |
| 60 | |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 61 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 62 | |
| 63 | import static com.google.common.base.Preconditions.checkArgument; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame^] | 64 | import static com.google.common.base.Preconditions.checkNotNull; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 65 | |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 66 | /** |
| 67 | * Synchronizes intents between the in-memory intent store and the |
| 68 | * IntentService. |
| 69 | */ |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame^] | 70 | public class IntentSynchronizer implements FibListener, IntentRequestListener { |
Pavlin Radoslavov | 2aa1f32 | 2015-03-11 17:59:44 -0700 | [diff] [blame] | 71 | private static final int PRIORITY_OFFSET = 100; |
| 72 | private static final int PRIORITY_MULTIPLIER = 5; |
| 73 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 74 | private static final Logger log = |
| 75 | LoggerFactory.getLogger(IntentSynchronizer.class); |
| 76 | |
| 77 | private final ApplicationId appId; |
| 78 | private final IntentService intentService; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame^] | 79 | private final HostService hostService; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 80 | private final Map<IntentKey, PointToPointIntent> peerIntents; |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 81 | private final Map<IpPrefix, MultiPointToSinglePointIntent> routeIntents; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 82 | |
| 83 | // |
| 84 | // State to deal with SDN-IP Leader election and pushing Intents |
| 85 | // |
| 86 | private final ExecutorService bgpIntentsSynchronizerExecutor; |
| 87 | private final Semaphore intentsSynchronizerSemaphore = new Semaphore(0); |
| 88 | private volatile boolean isElectedLeader = false; |
| 89 | private volatile boolean isActivatedLeader = false; |
| 90 | |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 91 | private final RoutingConfigurationService configService; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 92 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 93 | /** |
| 94 | * Class constructor. |
| 95 | * |
| 96 | * @param appId the Application ID |
| 97 | * @param intentService the intent service |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame^] | 98 | * @param hostService the host service |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 99 | * @param configService the SDN-IP configuration service |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 100 | */ |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 101 | IntentSynchronizer(ApplicationId appId, IntentService intentService, |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame^] | 102 | HostService hostService, |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 103 | RoutingConfigurationService configService) { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 104 | this.appId = appId; |
| 105 | this.intentService = intentService; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame^] | 106 | this.hostService = hostService; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 107 | peerIntents = new ConcurrentHashMap<>(); |
| 108 | routeIntents = new ConcurrentHashMap<>(); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 109 | |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 110 | this.configService = configService; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 111 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 112 | bgpIntentsSynchronizerExecutor = Executors.newSingleThreadExecutor( |
| 113 | new ThreadFactoryBuilder() |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 114 | .setNameFormat("sdnip-intents-synchronizer-%d").build()); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Starts the synchronizer. |
| 119 | */ |
| 120 | public void start() { |
| 121 | bgpIntentsSynchronizerExecutor.execute(new Runnable() { |
| 122 | @Override |
| 123 | public void run() { |
| 124 | doIntentSynchronizationThread(); |
| 125 | } |
| 126 | }); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Stops the synchronizer. |
| 131 | */ |
| 132 | public void stop() { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 133 | synchronized (this) { |
| 134 | // Stop the thread(s) |
| 135 | bgpIntentsSynchronizerExecutor.shutdownNow(); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 136 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 137 | // |
| 138 | // Withdraw all SDN-IP intents |
| 139 | // |
| 140 | if (!isElectedLeader) { |
| 141 | return; // Nothing to do: not the leader anymore |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 142 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 143 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 144 | // |
Pavlin Radoslavov | 20be3e6 | 2014-11-25 18:52:08 -0800 | [diff] [blame] | 145 | // NOTE: We don't withdraw the intents during shutdown, because |
| 146 | // it creates flux in the data plane during switchover. |
| 147 | // |
| 148 | |
| 149 | /* |
| 150 | // |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 151 | // Build a batch operation to withdraw all intents from this |
| 152 | // application. |
| 153 | // |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 154 | log.debug("SDN-IP Intent Synchronizer shutdown: " + |
| 155 | "withdrawing all intents..."); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 156 | IntentOperations.Builder builder = IntentOperations.builder(appId); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 157 | for (Intent intent : intentService.getIntents()) { |
| 158 | // Skip the intents from other applications |
| 159 | if (!intent.appId().equals(appId)) { |
| 160 | continue; |
| 161 | } |
| 162 | |
| 163 | // Skip the intents that are already withdrawn |
| 164 | IntentState intentState = |
| 165 | intentService.getIntentState(intent.id()); |
Pavlin Radoslavov | deb8a10 | 2014-11-26 13:31:36 -0800 | [diff] [blame] | 166 | if ((intentState == null) || |
| 167 | intentState.equals(IntentState.WITHDRAWING) || |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 168 | intentState.equals(IntentState.WITHDRAWN)) { |
| 169 | continue; |
| 170 | } |
| 171 | |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 172 | log.trace("SDN-IP Intent Synchronizer withdrawing intent: {}", |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 173 | intent); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 174 | builder.addWithdrawOperation(intent.id()); |
| 175 | } |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 176 | IntentOperations intentOperations = builder.build(); |
| 177 | intentService.execute(intentOperations); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 178 | leaderChanged(false); |
| 179 | |
| 180 | peerIntents.clear(); |
| 181 | routeIntents.clear(); |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 182 | log.debug("SDN-IP Intent Synchronizer shutdown completed"); |
Pavlin Radoslavov | 20be3e6 | 2014-11-25 18:52:08 -0800 | [diff] [blame] | 183 | */ |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 184 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Jonathan Hart | 5137218 | 2014-12-03 21:32:34 -0800 | [diff] [blame] | 187 | /** |
| 188 | * Signals the synchronizer that the SDN-IP leadership has changed. |
| 189 | * |
| 190 | * @param isLeader true if this instance is now the leader, otherwise false |
| 191 | */ |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 192 | public void leaderChanged(boolean isLeader) { |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 193 | log.debug("SDN-IP Leader changed: {}", isLeader); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 194 | |
| 195 | if (!isLeader) { |
| 196 | this.isElectedLeader = false; |
| 197 | this.isActivatedLeader = false; |
| 198 | return; // Nothing to do |
| 199 | } |
| 200 | this.isActivatedLeader = false; |
| 201 | this.isElectedLeader = true; |
| 202 | |
| 203 | // |
| 204 | // Tell the Intents Synchronizer thread to start the synchronization |
| 205 | // |
| 206 | intentsSynchronizerSemaphore.release(); |
| 207 | } |
| 208 | |
| 209 | /** |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 210 | * Gets the route intents. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 211 | * |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 212 | * @return the route intents |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 213 | */ |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 214 | public Collection<MultiPointToSinglePointIntent> getRouteIntents() { |
| 215 | List<MultiPointToSinglePointIntent> result = new LinkedList<>(); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 216 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 217 | for (Map.Entry<IpPrefix, MultiPointToSinglePointIntent> entry : |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 218 | routeIntents.entrySet()) { |
| 219 | result.add(entry.getValue()); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 220 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 221 | return result; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Thread for Intent Synchronization. |
| 226 | */ |
| 227 | private void doIntentSynchronizationThread() { |
| 228 | boolean interrupted = false; |
| 229 | try { |
| 230 | while (!interrupted) { |
| 231 | try { |
| 232 | intentsSynchronizerSemaphore.acquire(); |
| 233 | // |
| 234 | // Drain all permits, because a single synchronization is |
| 235 | // sufficient. |
| 236 | // |
| 237 | intentsSynchronizerSemaphore.drainPermits(); |
| 238 | } catch (InterruptedException e) { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 239 | interrupted = true; |
| 240 | break; |
| 241 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 242 | synchronizeIntents(); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 243 | } |
| 244 | } finally { |
| 245 | if (interrupted) { |
| 246 | Thread.currentThread().interrupt(); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 252 | * Submits a collection of point-to-point intents. |
| 253 | * |
| 254 | * @param intents the intents to submit |
| 255 | */ |
| 256 | void submitPeerIntents(Collection<PointToPointIntent> intents) { |
| 257 | synchronized (this) { |
| 258 | // Store the intents in memory |
| 259 | for (PointToPointIntent intent : intents) { |
| 260 | peerIntents.put(new IntentKey(intent), intent); |
| 261 | } |
| 262 | |
| 263 | // Push the intents |
| 264 | if (isElectedLeader && isActivatedLeader) { |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 265 | log.debug("SDN-IP Submitting all Peer Intents..."); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 266 | for (Intent intent : intents) { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 267 | log.trace("SDN-IP Submitting intents: {}", intent); |
| 268 | intentService.submit(intent); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | /** |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame^] | 275 | * Submits a MultiPointToSinglePointIntent for reactive routing. |
| 276 | * |
| 277 | * @param ipPrefix the IP prefix to match in a MultiPointToSinglePointIntent |
| 278 | * @param intent the intent to submit |
| 279 | */ |
| 280 | void submitReactiveIntent(IpPrefix ipPrefix, MultiPointToSinglePointIntent intent) { |
| 281 | synchronized (this) { |
| 282 | // Store the intent in memory |
| 283 | routeIntents.put(ipPrefix, intent); |
| 284 | |
| 285 | // Push the intent |
| 286 | if (isElectedLeader && isActivatedLeader) { |
| 287 | log.trace("SDN-IP submitting reactive routing intent: {}", intent); |
| 288 | intentService.submit(intent); |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | /** |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 294 | * Generates a route intent for a prefix, the next hop IP address, and |
| 295 | * the next hop MAC address. |
| 296 | * <p/> |
| 297 | * This method will find the egress interface for the intent. |
| 298 | * Intent will match dst IP prefix and rewrite dst MAC address at all other |
| 299 | * border switches, then forward packets according to dst MAC address. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 300 | * |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 301 | * @param prefix IP prefix of the route to add |
| 302 | * @param nextHopIpAddress IP address of the next hop |
| 303 | * @param nextHopMacAddress MAC address of the next hop |
| 304 | * @return the generated intent, or null if no intent should be submitted |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 305 | */ |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 306 | private MultiPointToSinglePointIntent generateRouteIntent( |
| 307 | IpPrefix prefix, |
| 308 | IpAddress nextHopIpAddress, |
| 309 | MacAddress nextHopMacAddress) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 310 | |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 311 | // Find the attachment point (egress interface) of the next hop |
| 312 | Interface egressInterface; |
| 313 | if (configService.getBgpPeers().containsKey(nextHopIpAddress)) { |
| 314 | // Route to a peer |
| 315 | log.debug("Route to peer {}", nextHopIpAddress); |
| 316 | BgpPeer peer = |
| 317 | configService.getBgpPeers().get(nextHopIpAddress); |
| 318 | egressInterface = |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 319 | configService.getInterface(peer.connectPoint()); |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 320 | } else { |
| 321 | // Route to non-peer |
| 322 | log.debug("Route to non-peer {}", nextHopIpAddress); |
| 323 | egressInterface = |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 324 | configService.getMatchingInterface(nextHopIpAddress); |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 325 | if (egressInterface == null) { |
| 326 | log.warn("No outgoing interface found for {}", |
| 327 | nextHopIpAddress); |
| 328 | return null; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | // |
| 333 | // Generate the intent itself |
| 334 | // |
| 335 | Set<ConnectPoint> ingressPorts = new HashSet<>(); |
| 336 | ConnectPoint egressPort = egressInterface.connectPoint(); |
| 337 | log.debug("Generating intent for prefix {}, next hop mac {}", |
| 338 | prefix, nextHopMacAddress); |
| 339 | |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 340 | for (Interface intf : configService.getInterfaces()) { |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 341 | if (!intf.connectPoint().equals(egressInterface.connectPoint())) { |
| 342 | ConnectPoint srcPort = intf.connectPoint(); |
| 343 | ingressPorts.add(srcPort); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | // Match the destination IP prefix at the first hop |
| 348 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
Pavlin Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 349 | if (prefix.isIp4()) { |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 350 | selector.matchEthType(Ethernet.TYPE_IPV4); |
Pavlin Radoslavov | a853709 | 2015-02-23 10:15:20 -0800 | [diff] [blame] | 351 | selector.matchIPDst(prefix); |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 352 | } else { |
| 353 | selector.matchEthType(Ethernet.TYPE_IPV6); |
Pavlin Radoslavov | a853709 | 2015-02-23 10:15:20 -0800 | [diff] [blame] | 354 | selector.matchIPv6Dst(prefix); |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 355 | } |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 356 | |
| 357 | // Rewrite the destination MAC address |
| 358 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder() |
| 359 | .setEthDst(nextHopMacAddress); |
| 360 | if (!egressInterface.vlan().equals(VlanId.NONE)) { |
| 361 | treatment.setVlanId(egressInterface.vlan()); |
| 362 | // If we set VLAN ID, we have to make sure a VLAN tag exists. |
| 363 | // TODO support no VLAN -> VLAN routing |
| 364 | selector.matchVlanId(VlanId.ANY); |
| 365 | } |
| 366 | |
Pavlin Radoslavov | 2aa1f32 | 2015-03-11 17:59:44 -0700 | [diff] [blame] | 367 | int priority = |
| 368 | prefix.prefixLength() * PRIORITY_MULTIPLIER + PRIORITY_OFFSET; |
| 369 | Key key = Key.of(prefix.toString(), appId); |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 370 | return MultiPointToSinglePointIntent.builder() |
| 371 | .appId(appId) |
| 372 | .key(key) |
| 373 | .selector(selector.build()) |
| 374 | .treatment(treatment.build()) |
| 375 | .ingressPoints(ingressPorts) |
| 376 | .egressPoint(egressPort) |
| 377 | .priority(priority) |
| 378 | .build(); |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | @Override |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame^] | 382 | public void setUpConnectivityInternetToHost(IpAddress hostIpAddress) { |
| 383 | checkNotNull(hostIpAddress); |
| 384 | Set<ConnectPoint> ingressPoints = new HashSet<ConnectPoint>(); |
| 385 | for (Interface intf : configService.getInterfaces()) { |
| 386 | ConnectPoint srcPoint = intf.connectPoint(); |
| 387 | ingressPoints.add(srcPoint); |
| 388 | } |
| 389 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 390 | |
| 391 | if (hostIpAddress.isIp4()) { |
| 392 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 393 | } else { |
| 394 | selector.matchEthType(Ethernet.TYPE_IPV6); |
| 395 | } |
| 396 | |
| 397 | // Match the destination IP prefix at the first hop |
| 398 | IpPrefix ipPrefix = hostIpAddress.toIpPrefix(); |
| 399 | selector.matchIPDst(ipPrefix); |
| 400 | |
| 401 | // Rewrite the destination MAC address |
| 402 | MacAddress hostMac = null; |
| 403 | ConnectPoint egressPoint = null; |
| 404 | for (Host host : hostService.getHostsByIp(hostIpAddress)) { |
| 405 | if (host.mac() != null) { |
| 406 | hostMac = host.mac(); |
| 407 | egressPoint = host.location(); |
| 408 | break; |
| 409 | } |
| 410 | } |
| 411 | if (hostMac == null) { |
| 412 | hostService.startMonitoringIp(hostIpAddress); |
| 413 | return; |
| 414 | } |
| 415 | |
| 416 | TrafficTreatment.Builder treatment = |
| 417 | DefaultTrafficTreatment.builder().setEthDst(hostMac); |
| 418 | Key key = Key.of(ipPrefix.toString(), appId); |
| 419 | int priority = ipPrefix.prefixLength() * PRIORITY_MULTIPLIER |
| 420 | + PRIORITY_OFFSET; |
| 421 | MultiPointToSinglePointIntent intent = |
| 422 | MultiPointToSinglePointIntent.builder() |
| 423 | .appId(appId) |
| 424 | .key(key) |
| 425 | .selector(selector.build()) |
| 426 | .treatment(treatment.build()) |
| 427 | .ingressPoints(ingressPoints) |
| 428 | .egressPoint(egressPoint) |
| 429 | .priority(priority) |
| 430 | .build(); |
| 431 | |
| 432 | log.trace("Generates ConnectivityInternetToHost intent {}", intent); |
| 433 | submitReactiveIntent(ipPrefix, intent); |
| 434 | } |
| 435 | |
| 436 | |
| 437 | @Override |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 438 | public void update(Collection<FibUpdate> updates, Collection<FibUpdate> withdraws) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 439 | // |
| 440 | // NOTE: Semantically, we MUST withdraw existing intents before |
| 441 | // submitting new intents. |
| 442 | // |
| 443 | synchronized (this) { |
| 444 | MultiPointToSinglePointIntent intent; |
| 445 | |
| 446 | log.debug("SDN-IP submitting intents = {} withdrawing = {}", |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 447 | updates.size(), withdraws.size()); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 448 | |
| 449 | // |
| 450 | // Prepare the Intent batch operations for the intents to withdraw |
| 451 | // |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 452 | for (FibUpdate withdraw : withdraws) { |
| 453 | checkArgument(withdraw.type() == FibUpdate.Type.DELETE, |
| 454 | "FibUpdate with wrong type in withdraws list"); |
| 455 | |
| 456 | IpPrefix prefix = withdraw.entry().prefix(); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 457 | intent = routeIntents.remove(prefix); |
| 458 | if (intent == null) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 459 | log.trace("SDN-IP No intent in routeIntents to delete " + |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 460 | "for prefix: {}", prefix); |
| 461 | continue; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 462 | } |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 463 | if (isElectedLeader && isActivatedLeader) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 464 | log.trace("SDN-IP Withdrawing intent: {}", intent); |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 465 | intentService.withdraw(intent); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 466 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 467 | } |
| 468 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 469 | // |
| 470 | // Prepare the Intent batch operations for the intents to submit |
| 471 | // |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 472 | for (FibUpdate update : updates) { |
| 473 | checkArgument(update.type() == FibUpdate.Type.UPDATE, |
| 474 | "FibUpdate with wrong type in updates list"); |
| 475 | |
| 476 | IpPrefix prefix = update.entry().prefix(); |
| 477 | intent = generateRouteIntent(prefix, update.entry().nextHopIp(), |
| 478 | update.entry().nextHopMac()); |
| 479 | |
| 480 | if (intent == null) { |
| 481 | // This preserves the old semantics - if an intent can't be |
| 482 | // generated, we don't do anything with that prefix. But |
| 483 | // perhaps we should withdraw the old intent anyway? |
| 484 | continue; |
| 485 | } |
| 486 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 487 | MultiPointToSinglePointIntent oldIntent = |
| 488 | routeIntents.put(prefix, intent); |
| 489 | if (isElectedLeader && isActivatedLeader) { |
| 490 | if (oldIntent != null) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 491 | log.trace("SDN-IP Withdrawing old intent: {}", |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 492 | oldIntent); |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 493 | intentService.withdraw(oldIntent); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 494 | } |
Jonathan Hart | b3b8a0a | 2015-02-12 17:36:13 -0800 | [diff] [blame] | 495 | log.trace("SDN-IP Submitting intent: {}", intent); |
| 496 | intentService.submit(intent); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 497 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | /** |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 503 | * Synchronize the in-memory Intents with the Intents in the Intent |
| 504 | * framework. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 505 | */ |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 506 | void synchronizeIntents() { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 507 | synchronized (this) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 508 | |
| 509 | Map<IntentKey, Intent> localIntents = new HashMap<>(); |
| 510 | Map<IntentKey, Intent> fetchedIntents = new HashMap<>(); |
| 511 | Collection<Intent> storeInMemoryIntents = new LinkedList<>(); |
| 512 | Collection<Intent> addIntents = new LinkedList<>(); |
| 513 | Collection<Intent> deleteIntents = new LinkedList<>(); |
| 514 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 515 | if (!isElectedLeader) { |
| 516 | return; // Nothing to do: not the leader anymore |
| 517 | } |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 518 | log.debug("SDN-IP synchronizing all intents..."); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 519 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 520 | // Prepare the local intents |
| 521 | for (Intent intent : routeIntents.values()) { |
| 522 | localIntents.put(new IntentKey(intent), intent); |
| 523 | } |
| 524 | for (Intent intent : peerIntents.values()) { |
| 525 | localIntents.put(new IntentKey(intent), intent); |
| 526 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 527 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 528 | // Fetch all intents for this application |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 529 | for (Intent intent : intentService.getIntents()) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 530 | if (!intent.appId().equals(appId)) { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 531 | continue; |
| 532 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 533 | fetchedIntents.put(new IntentKey(intent), intent); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 534 | } |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 535 | if (log.isDebugEnabled()) { |
| 536 | for (Intent intent: fetchedIntents.values()) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 537 | log.trace("SDN-IP Intent Synchronizer: fetched intent: {}", |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 538 | intent); |
| 539 | } |
| 540 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 541 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 542 | computeIntentsDelta(localIntents, fetchedIntents, |
| 543 | storeInMemoryIntents, addIntents, |
| 544 | deleteIntents); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 545 | |
| 546 | // |
| 547 | // Perform the actions: |
| 548 | // 1. Store in memory fetched intents that are same. Can be done |
| 549 | // even if we are not the leader anymore |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 550 | // 2. Delete intents: check if the leader before the operation |
| 551 | // 3. Add intents: check if the leader before the operation |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 552 | // |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 553 | for (Intent intent : storeInMemoryIntents) { |
| 554 | // Store the intent in memory based on its type |
| 555 | if (intent instanceof MultiPointToSinglePointIntent) { |
| 556 | MultiPointToSinglePointIntent mp2pIntent = |
| 557 | (MultiPointToSinglePointIntent) intent; |
| 558 | // Find the IP prefix |
| 559 | Criterion c = |
| 560 | mp2pIntent.selector().getCriterion(Criterion.Type.IPV4_DST); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 561 | if (c == null) { |
| 562 | // Try IPv6 |
| 563 | c = |
| 564 | mp2pIntent.selector().getCriterion(Criterion.Type.IPV6_DST); |
| 565 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 566 | if (c != null && c instanceof IPCriterion) { |
| 567 | IPCriterion ipCriterion = (IPCriterion) c; |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 568 | IpPrefix ipPrefix = ipCriterion.ip(); |
| 569 | if (ipPrefix == null) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 570 | continue; |
| 571 | } |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 572 | log.trace("SDN-IP Intent Synchronizer: updating " + |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 573 | "in-memory Route Intent for prefix {}", |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 574 | ipPrefix); |
| 575 | routeIntents.put(ipPrefix, mp2pIntent); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 576 | } else { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 577 | log.warn("SDN-IP no IPV4_DST or IPV6_DST criterion found for Intent {}", |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 578 | mp2pIntent.id()); |
| 579 | } |
| 580 | continue; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 581 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 582 | if (intent instanceof PointToPointIntent) { |
| 583 | PointToPointIntent p2pIntent = (PointToPointIntent) intent; |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 584 | log.trace("SDN-IP Intent Synchronizer: updating " + |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 585 | "in-memory Peer Intent {}", p2pIntent); |
| 586 | peerIntents.put(new IntentKey(intent), p2pIntent); |
| 587 | continue; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 588 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | // Withdraw Intents |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 592 | for (Intent intent : deleteIntents) { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 593 | intentService.withdraw(intent); |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 594 | log.trace("SDN-IP Intent Synchronizer: withdrawing intent: {}", |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 595 | intent); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 596 | } |
| 597 | if (!isElectedLeader) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 598 | log.trace("SDN-IP Intent Synchronizer: cannot withdraw intents: " + |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 599 | "not elected leader anymore"); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 600 | isActivatedLeader = false; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 601 | return; |
| 602 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 603 | |
| 604 | // Add Intents |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 605 | for (Intent intent : addIntents) { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 606 | intentService.submit(intent); |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 607 | log.trace("SDN-IP Intent Synchronizer: submitting intent: {}", |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 608 | intent); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 609 | } |
| 610 | if (!isElectedLeader) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 611 | log.trace("SDN-IP Intent Synchronizer: cannot submit intents: " + |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 612 | "not elected leader anymore"); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 613 | isActivatedLeader = false; |
| 614 | return; |
| 615 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 616 | |
| 617 | if (isElectedLeader) { |
| 618 | isActivatedLeader = true; // Allow push of Intents |
| 619 | } else { |
| 620 | isActivatedLeader = false; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 621 | } |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 622 | log.debug("SDN-IP intent synchronization completed"); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | |
| 626 | /** |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 627 | * Computes the delta in two sets of Intents: local in-memory Intents, |
| 628 | * and intents fetched from the Intent framework. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 629 | * |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 630 | * @param localIntents the local in-memory Intents |
| 631 | * @param fetchedIntents the Intents fetched from the Intent framework |
| 632 | * @param storeInMemoryIntents the Intents that should be stored in memory. |
| 633 | * Note: This Collection must be allocated by the caller, and it will |
| 634 | * be populated by this method. |
| 635 | * @param addIntents the Intents that should be added to the Intent |
| 636 | * framework. Note: This Collection must be allocated by the caller, and |
| 637 | * it will be populated by this method. |
| 638 | * @param deleteIntents the Intents that should be deleted from the Intent |
| 639 | * framework. Note: This Collection must be allocated by the caller, and |
| 640 | * it will be populated by this method. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 641 | */ |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 642 | private void computeIntentsDelta( |
| 643 | final Map<IntentKey, Intent> localIntents, |
| 644 | final Map<IntentKey, Intent> fetchedIntents, |
| 645 | Collection<Intent> storeInMemoryIntents, |
| 646 | Collection<Intent> addIntents, |
| 647 | Collection<Intent> deleteIntents) { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 648 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 649 | // |
| 650 | // Compute the deltas between the LOCAL in-memory Intents and the |
| 651 | // FETCHED Intents: |
| 652 | // - If an Intent is in both the LOCAL and FETCHED sets: |
| 653 | // If the FETCHED Intent is WITHDRAWING or WITHDRAWN, then |
| 654 | // the LOCAL Intent should be added/installed; otherwise the |
| 655 | // FETCHED intent should be stored in the local memory |
| 656 | // (i.e., override the LOCAL Intent) to preserve the original |
| 657 | // Intent ID. |
| 658 | // - if a LOCAL Intent is not in the FETCHED set, then the LOCAL |
| 659 | // Intent should be added/installed. |
| 660 | // - If a FETCHED Intent is not in the LOCAL set, then the FETCHED |
| 661 | // Intent should be deleted/withdrawn. |
| 662 | // |
| 663 | for (Map.Entry<IntentKey, Intent> entry : localIntents.entrySet()) { |
| 664 | IntentKey intentKey = entry.getKey(); |
| 665 | Intent localIntent = entry.getValue(); |
| 666 | Intent fetchedIntent = fetchedIntents.get(intentKey); |
| 667 | |
| 668 | if (fetchedIntent == null) { |
| 669 | // |
| 670 | // No FETCHED Intent found: push the LOCAL Intent. |
| 671 | // |
| 672 | addIntents.add(localIntent); |
| 673 | continue; |
| 674 | } |
| 675 | |
| 676 | IntentState state = |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 677 | intentService.getIntentState(fetchedIntent.key()); |
Pavlin Radoslavov | deb8a10 | 2014-11-26 13:31:36 -0800 | [diff] [blame] | 678 | if (state == null || |
| 679 | state == IntentState.WITHDRAWING || |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 680 | state == IntentState.WITHDRAWN) { |
| 681 | // The intent has been withdrawn but according to our route |
| 682 | // table it should be installed. We'll reinstall it. |
| 683 | addIntents.add(localIntent); |
| 684 | continue; |
| 685 | } |
| 686 | storeInMemoryIntents.add(fetchedIntent); |
| 687 | } |
| 688 | |
| 689 | for (Map.Entry<IntentKey, Intent> entry : fetchedIntents.entrySet()) { |
| 690 | IntentKey intentKey = entry.getKey(); |
| 691 | Intent fetchedIntent = entry.getValue(); |
| 692 | Intent localIntent = localIntents.get(intentKey); |
| 693 | |
| 694 | if (localIntent != null) { |
| 695 | continue; |
| 696 | } |
| 697 | |
| 698 | IntentState state = |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 699 | intentService.getIntentState(fetchedIntent.key()); |
Pavlin Radoslavov | deb8a10 | 2014-11-26 13:31:36 -0800 | [diff] [blame] | 700 | if (state == null || |
| 701 | state == IntentState.WITHDRAWING || |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 702 | state == IntentState.WITHDRAWN) { |
| 703 | // Nothing to do. The intent has been already withdrawn. |
| 704 | continue; |
| 705 | } |
| 706 | // |
| 707 | // No LOCAL Intent found: delete/withdraw the FETCHED Intent. |
| 708 | // |
| 709 | deleteIntents.add(fetchedIntent); |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | /** |
| 714 | * Helper class that can be used to compute the key for an Intent by |
| 715 | * by excluding the Intent ID. |
| 716 | */ |
| 717 | static final class IntentKey { |
| 718 | private final Intent intent; |
| 719 | |
| 720 | /** |
| 721 | * Constructor. |
| 722 | * |
| 723 | * @param intent the intent to use |
| 724 | */ |
| 725 | IntentKey(Intent intent) { |
| 726 | checkArgument((intent instanceof MultiPointToSinglePointIntent) || |
| 727 | (intent instanceof PointToPointIntent), |
| 728 | "Intent type not recognized", intent); |
| 729 | this.intent = intent; |
| 730 | } |
| 731 | |
| 732 | /** |
| 733 | * Compares two Multi-Point to Single-Point Intents whether they |
| 734 | * represent same logical intention. |
| 735 | * |
| 736 | * @param intent1 the first Intent to compare |
| 737 | * @param intent2 the second Intent to compare |
| 738 | * @return true if both Intents represent same logical intention, |
| 739 | * otherwise false |
| 740 | */ |
| 741 | static boolean equalIntents(MultiPointToSinglePointIntent intent1, |
| 742 | MultiPointToSinglePointIntent intent2) { |
| 743 | return Objects.equals(intent1.appId(), intent2.appId()) && |
| 744 | Objects.equals(intent1.selector(), intent2.selector()) && |
| 745 | Objects.equals(intent1.treatment(), intent2.treatment()) && |
| 746 | Objects.equals(intent1.ingressPoints(), intent2.ingressPoints()) && |
| 747 | Objects.equals(intent1.egressPoint(), intent2.egressPoint()); |
| 748 | } |
| 749 | |
| 750 | /** |
| 751 | * Compares two Point-to-Point Intents whether they represent |
| 752 | * same logical intention. |
| 753 | * |
| 754 | * @param intent1 the first Intent to compare |
| 755 | * @param intent2 the second Intent to compare |
| 756 | * @return true if both Intents represent same logical intention, |
| 757 | * otherwise false |
| 758 | */ |
| 759 | static boolean equalIntents(PointToPointIntent intent1, |
| 760 | PointToPointIntent intent2) { |
| 761 | return Objects.equals(intent1.appId(), intent2.appId()) && |
| 762 | Objects.equals(intent1.selector(), intent2.selector()) && |
| 763 | Objects.equals(intent1.treatment(), intent2.treatment()) && |
| 764 | Objects.equals(intent1.ingressPoint(), intent2.ingressPoint()) && |
| 765 | Objects.equals(intent1.egressPoint(), intent2.egressPoint()); |
| 766 | } |
| 767 | |
| 768 | @Override |
| 769 | public int hashCode() { |
| 770 | if (intent instanceof PointToPointIntent) { |
| 771 | PointToPointIntent p2pIntent = (PointToPointIntent) intent; |
| 772 | return Objects.hash(p2pIntent.appId(), |
| 773 | p2pIntent.resources(), |
| 774 | p2pIntent.selector(), |
| 775 | p2pIntent.treatment(), |
| 776 | p2pIntent.constraints(), |
| 777 | p2pIntent.ingressPoint(), |
| 778 | p2pIntent.egressPoint()); |
| 779 | } |
| 780 | if (intent instanceof MultiPointToSinglePointIntent) { |
| 781 | MultiPointToSinglePointIntent m2pIntent = |
| 782 | (MultiPointToSinglePointIntent) intent; |
| 783 | return Objects.hash(m2pIntent.appId(), |
| 784 | m2pIntent.resources(), |
| 785 | m2pIntent.selector(), |
| 786 | m2pIntent.treatment(), |
| 787 | m2pIntent.constraints(), |
| 788 | m2pIntent.ingressPoints(), |
| 789 | m2pIntent.egressPoint()); |
| 790 | } |
| 791 | checkArgument(false, "Intent type not recognized", intent); |
| 792 | return 0; |
| 793 | } |
| 794 | |
| 795 | @Override |
| 796 | public boolean equals(Object obj) { |
| 797 | if (this == obj) { |
| 798 | return true; |
| 799 | } |
| 800 | if ((obj == null) || (!(obj instanceof IntentKey))) { |
| 801 | return false; |
| 802 | } |
| 803 | IntentKey other = (IntentKey) obj; |
| 804 | |
| 805 | if (this.intent instanceof PointToPointIntent) { |
| 806 | if (!(other.intent instanceof PointToPointIntent)) { |
| 807 | return false; |
| 808 | } |
| 809 | return equalIntents((PointToPointIntent) this.intent, |
| 810 | (PointToPointIntent) other.intent); |
| 811 | } |
| 812 | if (this.intent instanceof MultiPointToSinglePointIntent) { |
| 813 | if (!(other.intent instanceof MultiPointToSinglePointIntent)) { |
| 814 | return false; |
| 815 | } |
| 816 | return equalIntents( |
| 817 | (MultiPointToSinglePointIntent) this.intent, |
| 818 | (MultiPointToSinglePointIntent) other.intent); |
| 819 | } |
| 820 | checkArgument(false, "Intent type not recognized", intent); |
| 821 | return false; |
| 822 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 823 | } |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame^] | 824 | |
| 825 | @Override |
| 826 | public void setUpConnectivityHostToHost(IpAddress dstIpAddress, |
| 827 | IpAddress srcIpAddress, |
| 828 | MacAddress srcMacAddress, |
| 829 | ConnectPoint srcConnectPoint) { |
| 830 | checkNotNull(dstIpAddress); |
| 831 | checkNotNull(srcIpAddress); |
| 832 | checkNotNull(srcMacAddress); |
| 833 | checkNotNull(srcConnectPoint); |
| 834 | |
| 835 | IpPrefix srcIpPrefix = srcIpAddress.toIpPrefix(); |
| 836 | IpPrefix dstIpPrefix = dstIpAddress.toIpPrefix(); |
| 837 | ConnectPoint dstConnectPoint = null; |
| 838 | MacAddress dstMacAddress = null; |
| 839 | |
| 840 | for (Host host : hostService.getHostsByIp(dstIpAddress)) { |
| 841 | if (host.mac() != null) { |
| 842 | dstMacAddress = host.mac(); |
| 843 | dstConnectPoint = host.location(); |
| 844 | break; |
| 845 | } |
| 846 | } |
| 847 | if (dstMacAddress == null) { |
| 848 | hostService.startMonitoringIp(dstIpAddress); |
| 849 | return; |
| 850 | } |
| 851 | |
| 852 | // |
| 853 | // Handle intent from source host to destination host |
| 854 | // |
| 855 | MultiPointToSinglePointIntent srcToDstIntent = |
| 856 | hostToHostIntentGenerator(dstIpAddress, dstConnectPoint, |
| 857 | dstMacAddress, srcConnectPoint); |
| 858 | submitReactiveIntent(dstIpPrefix, srcToDstIntent); |
| 859 | |
| 860 | // |
| 861 | // Handle intent from destination host to source host |
| 862 | // |
| 863 | |
| 864 | // Since we proactively handle the intent from destination host to |
| 865 | // source host, we should check whether there is an exiting intent |
| 866 | // first. |
| 867 | if (mp2pIntentExists(srcIpPrefix)) { |
| 868 | updateExistingMp2pIntent(srcIpPrefix, dstConnectPoint); |
| 869 | return; |
| 870 | } else { |
| 871 | // There is no existing intent, create a new one. |
| 872 | MultiPointToSinglePointIntent dstToSrcIntent = |
| 873 | hostToHostIntentGenerator(srcIpAddress, srcConnectPoint, |
| 874 | srcMacAddress, dstConnectPoint); |
| 875 | submitReactiveIntent(srcIpPrefix, dstToSrcIntent); |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | /** |
| 880 | * Generates MultiPointToSinglePointIntent for both source host and |
| 881 | * destination host located in local SDN network. |
| 882 | * |
| 883 | * @param dstIpAddress the destination IP address |
| 884 | * @param dstConnectPoint the destination host connect point |
| 885 | * @param dstMacAddress the MAC address of destination host |
| 886 | * @param srcConnectPoint the connect point where packet-in from |
| 887 | * @return the generated MultiPointToSinglePointIntent |
| 888 | */ |
| 889 | private MultiPointToSinglePointIntent hostToHostIntentGenerator( |
| 890 | IpAddress dstIpAddress, |
| 891 | ConnectPoint dstConnectPoint, |
| 892 | MacAddress dstMacAddress, |
| 893 | ConnectPoint srcConnectPoint) { |
| 894 | checkNotNull(dstIpAddress); |
| 895 | checkNotNull(dstConnectPoint); |
| 896 | checkNotNull(dstMacAddress); |
| 897 | checkNotNull(srcConnectPoint); |
| 898 | |
| 899 | Set<ConnectPoint> ingressPoints = new HashSet<ConnectPoint>(); |
| 900 | ingressPoints.add(srcConnectPoint); |
| 901 | IpPrefix dstIpPrefix = dstIpAddress.toIpPrefix(); |
| 902 | |
| 903 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 904 | if (dstIpAddress.isIp4()) { |
| 905 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 906 | selector.matchIPDst(dstIpPrefix); |
| 907 | } else { |
| 908 | selector.matchEthType(Ethernet.TYPE_IPV6); |
| 909 | selector.matchIPv6Dst(dstIpPrefix); |
| 910 | } |
| 911 | |
| 912 | // Rewrite the destination MAC address |
| 913 | TrafficTreatment.Builder treatment = |
| 914 | DefaultTrafficTreatment.builder().setEthDst(dstMacAddress); |
| 915 | |
| 916 | Key key = Key.of(dstIpPrefix.toString(), appId); |
| 917 | int priority = dstIpPrefix.prefixLength() * PRIORITY_MULTIPLIER |
| 918 | + PRIORITY_OFFSET; |
| 919 | MultiPointToSinglePointIntent intent = |
| 920 | MultiPointToSinglePointIntent.builder() |
| 921 | .appId(appId) |
| 922 | .key(key) |
| 923 | .selector(selector.build()) |
| 924 | .treatment(treatment.build()) |
| 925 | .ingressPoints(ingressPoints) |
| 926 | .egressPoint(dstConnectPoint) |
| 927 | .priority(priority) |
| 928 | .build(); |
| 929 | |
| 930 | log.trace("Generates ConnectivityHostToHost = {} ", intent); |
| 931 | return intent; |
| 932 | } |
| 933 | |
| 934 | @Override |
| 935 | public void updateExistingMp2pIntent(IpPrefix ipPrefix, |
| 936 | ConnectPoint ingressConnectPoint) { |
| 937 | checkNotNull(ipPrefix); |
| 938 | checkNotNull(ingressConnectPoint); |
| 939 | |
| 940 | MultiPointToSinglePointIntent existingIntent = |
| 941 | getExistingMp2pIntent(ipPrefix); |
| 942 | if (existingIntent != null) { |
| 943 | Set<ConnectPoint> ingressPoints = existingIntent.ingressPoints(); |
| 944 | // Add host connect point into ingressPoints of the existing intent |
| 945 | if (ingressPoints.add(ingressConnectPoint)) { |
| 946 | MultiPointToSinglePointIntent updatedMp2pIntent = |
| 947 | MultiPointToSinglePointIntent.builder() |
| 948 | .appId(appId) |
| 949 | .key(existingIntent.key()) |
| 950 | .selector(existingIntent.selector()) |
| 951 | .treatment(existingIntent.treatment()) |
| 952 | .ingressPoints(ingressPoints) |
| 953 | .egressPoint(existingIntent.egressPoint()) |
| 954 | .priority(existingIntent.priority()) |
| 955 | .build(); |
| 956 | |
| 957 | log.trace("Update an existing MultiPointToSinglePointIntent " |
| 958 | + "to new intent = {} ", updatedMp2pIntent); |
| 959 | submitReactiveIntent(ipPrefix, updatedMp2pIntent); |
| 960 | } |
| 961 | // If adding ingressConnectPoint to ingressPoints failed, it |
| 962 | // because between the time interval from checking existing intent |
| 963 | // to generating new intent, onos updated this intent due to other |
| 964 | // packet-in and the new intent also includes the |
| 965 | // ingressConnectPoint. This will not affect reactive routing. |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | @Override |
| 970 | public boolean mp2pIntentExists(IpPrefix ipPrefix) { |
| 971 | checkNotNull(ipPrefix); |
| 972 | return routeIntents.get(ipPrefix) == null ? false : true; |
| 973 | } |
| 974 | |
| 975 | /** |
| 976 | * Gets the existing MultiPointToSinglePointIntent from memory for a given |
| 977 | * IP prefix. |
| 978 | * |
| 979 | * @param ipPrefix the IP prefix used to find MultiPointToSinglePointIntent |
| 980 | * @return the MultiPointToSinglePointIntent if found, otherwise null |
| 981 | */ |
| 982 | private MultiPointToSinglePointIntent getExistingMp2pIntent(IpPrefix |
| 983 | ipPrefix) { |
| 984 | checkNotNull(ipPrefix); |
| 985 | return routeIntents.get(ipPrefix); |
| 986 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 987 | } |