Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
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; |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 43 | import org.onosproject.net.flow.criteria.IPCriterion; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 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); |
Pingping Lin | 8a52471 | 2015-06-24 14:58:24 -0700 | [diff] [blame^] | 384 | Set<ConnectPoint> ingressPoints = |
| 385 | configService.getBgpPeerConnectPoints(); |
| 386 | |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 387 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 388 | |
| 389 | if (hostIpAddress.isIp4()) { |
| 390 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 391 | } else { |
| 392 | selector.matchEthType(Ethernet.TYPE_IPV6); |
| 393 | } |
| 394 | |
| 395 | // Match the destination IP prefix at the first hop |
| 396 | IpPrefix ipPrefix = hostIpAddress.toIpPrefix(); |
| 397 | selector.matchIPDst(ipPrefix); |
| 398 | |
| 399 | // Rewrite the destination MAC address |
| 400 | MacAddress hostMac = null; |
| 401 | ConnectPoint egressPoint = null; |
| 402 | for (Host host : hostService.getHostsByIp(hostIpAddress)) { |
| 403 | if (host.mac() != null) { |
| 404 | hostMac = host.mac(); |
| 405 | egressPoint = host.location(); |
| 406 | break; |
| 407 | } |
| 408 | } |
| 409 | if (hostMac == null) { |
| 410 | hostService.startMonitoringIp(hostIpAddress); |
| 411 | return; |
| 412 | } |
| 413 | |
| 414 | TrafficTreatment.Builder treatment = |
| 415 | DefaultTrafficTreatment.builder().setEthDst(hostMac); |
| 416 | Key key = Key.of(ipPrefix.toString(), appId); |
| 417 | int priority = ipPrefix.prefixLength() * PRIORITY_MULTIPLIER |
| 418 | + PRIORITY_OFFSET; |
| 419 | MultiPointToSinglePointIntent intent = |
| 420 | MultiPointToSinglePointIntent.builder() |
| 421 | .appId(appId) |
| 422 | .key(key) |
| 423 | .selector(selector.build()) |
| 424 | .treatment(treatment.build()) |
| 425 | .ingressPoints(ingressPoints) |
| 426 | .egressPoint(egressPoint) |
| 427 | .priority(priority) |
| 428 | .build(); |
| 429 | |
| 430 | log.trace("Generates ConnectivityInternetToHost intent {}", intent); |
| 431 | submitReactiveIntent(ipPrefix, intent); |
| 432 | } |
| 433 | |
| 434 | |
| 435 | @Override |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 436 | public void update(Collection<FibUpdate> updates, Collection<FibUpdate> withdraws) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 437 | // |
| 438 | // NOTE: Semantically, we MUST withdraw existing intents before |
| 439 | // submitting new intents. |
| 440 | // |
| 441 | synchronized (this) { |
| 442 | MultiPointToSinglePointIntent intent; |
| 443 | |
| 444 | log.debug("SDN-IP submitting intents = {} withdrawing = {}", |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 445 | updates.size(), withdraws.size()); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 446 | |
| 447 | // |
| 448 | // Prepare the Intent batch operations for the intents to withdraw |
| 449 | // |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 450 | for (FibUpdate withdraw : withdraws) { |
| 451 | checkArgument(withdraw.type() == FibUpdate.Type.DELETE, |
| 452 | "FibUpdate with wrong type in withdraws list"); |
| 453 | |
| 454 | IpPrefix prefix = withdraw.entry().prefix(); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 455 | intent = routeIntents.remove(prefix); |
| 456 | if (intent == null) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 457 | log.trace("SDN-IP No intent in routeIntents to delete " + |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 458 | "for prefix: {}", prefix); |
| 459 | continue; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 460 | } |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 461 | if (isElectedLeader && isActivatedLeader) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 462 | log.trace("SDN-IP Withdrawing intent: {}", intent); |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 463 | intentService.withdraw(intent); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 464 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 465 | } |
| 466 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 467 | // |
| 468 | // Prepare the Intent batch operations for the intents to submit |
| 469 | // |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 470 | for (FibUpdate update : updates) { |
| 471 | checkArgument(update.type() == FibUpdate.Type.UPDATE, |
| 472 | "FibUpdate with wrong type in updates list"); |
| 473 | |
| 474 | IpPrefix prefix = update.entry().prefix(); |
| 475 | intent = generateRouteIntent(prefix, update.entry().nextHopIp(), |
| 476 | update.entry().nextHopMac()); |
| 477 | |
| 478 | if (intent == null) { |
| 479 | // This preserves the old semantics - if an intent can't be |
| 480 | // generated, we don't do anything with that prefix. But |
| 481 | // perhaps we should withdraw the old intent anyway? |
| 482 | continue; |
| 483 | } |
| 484 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 485 | MultiPointToSinglePointIntent oldIntent = |
| 486 | routeIntents.put(prefix, intent); |
| 487 | if (isElectedLeader && isActivatedLeader) { |
| 488 | if (oldIntent != null) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 489 | log.trace("SDN-IP Withdrawing old intent: {}", |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 490 | oldIntent); |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 491 | intentService.withdraw(oldIntent); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 492 | } |
Jonathan Hart | b3b8a0a | 2015-02-12 17:36:13 -0800 | [diff] [blame] | 493 | log.trace("SDN-IP Submitting intent: {}", intent); |
| 494 | intentService.submit(intent); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 495 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | /** |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 501 | * Synchronize the in-memory Intents with the Intents in the Intent |
| 502 | * framework. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 503 | */ |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 504 | void synchronizeIntents() { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 505 | synchronized (this) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 506 | |
| 507 | Map<IntentKey, Intent> localIntents = new HashMap<>(); |
| 508 | Map<IntentKey, Intent> fetchedIntents = new HashMap<>(); |
| 509 | Collection<Intent> storeInMemoryIntents = new LinkedList<>(); |
| 510 | Collection<Intent> addIntents = new LinkedList<>(); |
| 511 | Collection<Intent> deleteIntents = new LinkedList<>(); |
| 512 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 513 | if (!isElectedLeader) { |
| 514 | return; // Nothing to do: not the leader anymore |
| 515 | } |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 516 | log.debug("SDN-IP synchronizing all intents..."); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 517 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 518 | // Prepare the local intents |
| 519 | for (Intent intent : routeIntents.values()) { |
| 520 | localIntents.put(new IntentKey(intent), intent); |
| 521 | } |
| 522 | for (Intent intent : peerIntents.values()) { |
| 523 | localIntents.put(new IntentKey(intent), intent); |
| 524 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 525 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 526 | // Fetch all intents for this application |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 527 | for (Intent intent : intentService.getIntents()) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 528 | if (!intent.appId().equals(appId)) { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 529 | continue; |
| 530 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 531 | fetchedIntents.put(new IntentKey(intent), intent); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 532 | } |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 533 | if (log.isDebugEnabled()) { |
| 534 | for (Intent intent: fetchedIntents.values()) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 535 | log.trace("SDN-IP Intent Synchronizer: fetched intent: {}", |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 536 | intent); |
| 537 | } |
| 538 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 539 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 540 | computeIntentsDelta(localIntents, fetchedIntents, |
| 541 | storeInMemoryIntents, addIntents, |
| 542 | deleteIntents); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 543 | |
| 544 | // |
| 545 | // Perform the actions: |
| 546 | // 1. Store in memory fetched intents that are same. Can be done |
| 547 | // even if we are not the leader anymore |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 548 | // 2. Delete intents: check if the leader before the operation |
| 549 | // 3. Add intents: check if the leader before the operation |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 550 | // |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 551 | for (Intent intent : storeInMemoryIntents) { |
| 552 | // Store the intent in memory based on its type |
| 553 | if (intent instanceof MultiPointToSinglePointIntent) { |
| 554 | MultiPointToSinglePointIntent mp2pIntent = |
| 555 | (MultiPointToSinglePointIntent) intent; |
| 556 | // Find the IP prefix |
| 557 | Criterion c = |
| 558 | mp2pIntent.selector().getCriterion(Criterion.Type.IPV4_DST); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 559 | if (c == null) { |
| 560 | // Try IPv6 |
| 561 | c = |
| 562 | mp2pIntent.selector().getCriterion(Criterion.Type.IPV6_DST); |
| 563 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 564 | if (c != null && c instanceof IPCriterion) { |
| 565 | IPCriterion ipCriterion = (IPCriterion) c; |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 566 | IpPrefix ipPrefix = ipCriterion.ip(); |
| 567 | if (ipPrefix == null) { |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 568 | continue; |
| 569 | } |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 570 | log.trace("SDN-IP Intent Synchronizer: updating " + |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 571 | "in-memory Route Intent for prefix {}", |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 572 | ipPrefix); |
| 573 | routeIntents.put(ipPrefix, mp2pIntent); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 574 | } else { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 575 | 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] | 576 | mp2pIntent.id()); |
| 577 | } |
| 578 | continue; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 579 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 580 | if (intent instanceof PointToPointIntent) { |
| 581 | PointToPointIntent p2pIntent = (PointToPointIntent) intent; |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 582 | log.trace("SDN-IP Intent Synchronizer: updating " + |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 583 | "in-memory Peer Intent {}", p2pIntent); |
| 584 | peerIntents.put(new IntentKey(intent), p2pIntent); |
| 585 | continue; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 586 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | // Withdraw Intents |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 590 | for (Intent intent : deleteIntents) { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 591 | intentService.withdraw(intent); |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 592 | log.trace("SDN-IP Intent Synchronizer: withdrawing intent: {}", |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 593 | intent); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 594 | } |
| 595 | if (!isElectedLeader) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 596 | log.trace("SDN-IP Intent Synchronizer: cannot withdraw intents: " + |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 597 | "not elected leader anymore"); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 598 | isActivatedLeader = false; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 599 | return; |
| 600 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 601 | |
| 602 | // Add Intents |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 603 | for (Intent intent : addIntents) { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 604 | intentService.submit(intent); |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 605 | log.trace("SDN-IP Intent Synchronizer: submitting intent: {}", |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 606 | intent); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 607 | } |
| 608 | if (!isElectedLeader) { |
Pavlin Radoslavov | 8049bb8 | 2014-12-02 13:58:35 -0800 | [diff] [blame] | 609 | log.trace("SDN-IP Intent Synchronizer: cannot submit intents: " + |
Pavlin Radoslavov | caf6337 | 2014-11-26 11:59:11 -0800 | [diff] [blame] | 610 | "not elected leader anymore"); |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 611 | isActivatedLeader = false; |
| 612 | return; |
| 613 | } |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 614 | |
| 615 | if (isElectedLeader) { |
| 616 | isActivatedLeader = true; // Allow push of Intents |
| 617 | } else { |
| 618 | isActivatedLeader = false; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 619 | } |
Pavlin Radoslavov | 93ae832 | 2014-11-24 20:54:36 -0800 | [diff] [blame] | 620 | log.debug("SDN-IP intent synchronization completed"); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | |
| 624 | /** |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 625 | * Computes the delta in two sets of Intents: local in-memory Intents, |
| 626 | * and intents fetched from the Intent framework. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 627 | * |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 628 | * @param localIntents the local in-memory Intents |
| 629 | * @param fetchedIntents the Intents fetched from the Intent framework |
| 630 | * @param storeInMemoryIntents the Intents that should be stored in memory. |
| 631 | * Note: This Collection must be allocated by the caller, and it will |
| 632 | * be populated by this method. |
| 633 | * @param addIntents the Intents that should be added to the Intent |
| 634 | * framework. Note: This Collection must be allocated by the caller, and |
| 635 | * it will be populated by this method. |
| 636 | * @param deleteIntents the Intents that should be deleted from the Intent |
| 637 | * framework. Note: This Collection must be allocated by the caller, and |
| 638 | * it will be populated by this method. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 639 | */ |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 640 | private void computeIntentsDelta( |
| 641 | final Map<IntentKey, Intent> localIntents, |
| 642 | final Map<IntentKey, Intent> fetchedIntents, |
| 643 | Collection<Intent> storeInMemoryIntents, |
| 644 | Collection<Intent> addIntents, |
| 645 | Collection<Intent> deleteIntents) { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 646 | |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 647 | // |
| 648 | // Compute the deltas between the LOCAL in-memory Intents and the |
| 649 | // FETCHED Intents: |
| 650 | // - If an Intent is in both the LOCAL and FETCHED sets: |
| 651 | // If the FETCHED Intent is WITHDRAWING or WITHDRAWN, then |
| 652 | // the LOCAL Intent should be added/installed; otherwise the |
| 653 | // FETCHED intent should be stored in the local memory |
| 654 | // (i.e., override the LOCAL Intent) to preserve the original |
| 655 | // Intent ID. |
| 656 | // - if a LOCAL Intent is not in the FETCHED set, then the LOCAL |
| 657 | // Intent should be added/installed. |
| 658 | // - If a FETCHED Intent is not in the LOCAL set, then the FETCHED |
| 659 | // Intent should be deleted/withdrawn. |
| 660 | // |
| 661 | for (Map.Entry<IntentKey, Intent> entry : localIntents.entrySet()) { |
| 662 | IntentKey intentKey = entry.getKey(); |
| 663 | Intent localIntent = entry.getValue(); |
| 664 | Intent fetchedIntent = fetchedIntents.get(intentKey); |
| 665 | |
| 666 | if (fetchedIntent == null) { |
| 667 | // |
| 668 | // No FETCHED Intent found: push the LOCAL Intent. |
| 669 | // |
| 670 | addIntents.add(localIntent); |
| 671 | continue; |
| 672 | } |
| 673 | |
| 674 | IntentState state = |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 675 | intentService.getIntentState(fetchedIntent.key()); |
Pavlin Radoslavov | deb8a10 | 2014-11-26 13:31:36 -0800 | [diff] [blame] | 676 | if (state == null || |
| 677 | state == IntentState.WITHDRAWING || |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 678 | state == IntentState.WITHDRAWN) { |
| 679 | // The intent has been withdrawn but according to our route |
| 680 | // table it should be installed. We'll reinstall it. |
| 681 | addIntents.add(localIntent); |
| 682 | continue; |
| 683 | } |
| 684 | storeInMemoryIntents.add(fetchedIntent); |
| 685 | } |
| 686 | |
| 687 | for (Map.Entry<IntentKey, Intent> entry : fetchedIntents.entrySet()) { |
| 688 | IntentKey intentKey = entry.getKey(); |
| 689 | Intent fetchedIntent = entry.getValue(); |
| 690 | Intent localIntent = localIntents.get(intentKey); |
| 691 | |
| 692 | if (localIntent != null) { |
| 693 | continue; |
| 694 | } |
| 695 | |
| 696 | IntentState state = |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 697 | intentService.getIntentState(fetchedIntent.key()); |
Pavlin Radoslavov | deb8a10 | 2014-11-26 13:31:36 -0800 | [diff] [blame] | 698 | if (state == null || |
| 699 | state == IntentState.WITHDRAWING || |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 700 | state == IntentState.WITHDRAWN) { |
| 701 | // Nothing to do. The intent has been already withdrawn. |
| 702 | continue; |
| 703 | } |
| 704 | // |
| 705 | // No LOCAL Intent found: delete/withdraw the FETCHED Intent. |
| 706 | // |
| 707 | deleteIntents.add(fetchedIntent); |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | /** |
| 712 | * Helper class that can be used to compute the key for an Intent by |
| 713 | * by excluding the Intent ID. |
| 714 | */ |
| 715 | static final class IntentKey { |
| 716 | private final Intent intent; |
| 717 | |
| 718 | /** |
| 719 | * Constructor. |
| 720 | * |
| 721 | * @param intent the intent to use |
| 722 | */ |
| 723 | IntentKey(Intent intent) { |
| 724 | checkArgument((intent instanceof MultiPointToSinglePointIntent) || |
| 725 | (intent instanceof PointToPointIntent), |
| 726 | "Intent type not recognized", intent); |
| 727 | this.intent = intent; |
| 728 | } |
| 729 | |
| 730 | /** |
| 731 | * Compares two Multi-Point to Single-Point Intents whether they |
| 732 | * represent same logical intention. |
| 733 | * |
| 734 | * @param intent1 the first Intent to compare |
| 735 | * @param intent2 the second Intent to compare |
| 736 | * @return true if both Intents represent same logical intention, |
| 737 | * otherwise false |
| 738 | */ |
| 739 | static boolean equalIntents(MultiPointToSinglePointIntent intent1, |
| 740 | MultiPointToSinglePointIntent intent2) { |
| 741 | return Objects.equals(intent1.appId(), intent2.appId()) && |
| 742 | Objects.equals(intent1.selector(), intent2.selector()) && |
| 743 | Objects.equals(intent1.treatment(), intent2.treatment()) && |
| 744 | Objects.equals(intent1.ingressPoints(), intent2.ingressPoints()) && |
| 745 | Objects.equals(intent1.egressPoint(), intent2.egressPoint()); |
| 746 | } |
| 747 | |
| 748 | /** |
| 749 | * Compares two Point-to-Point Intents whether they represent |
| 750 | * same logical intention. |
| 751 | * |
| 752 | * @param intent1 the first Intent to compare |
| 753 | * @param intent2 the second Intent to compare |
| 754 | * @return true if both Intents represent same logical intention, |
| 755 | * otherwise false |
| 756 | */ |
| 757 | static boolean equalIntents(PointToPointIntent intent1, |
| 758 | PointToPointIntent intent2) { |
| 759 | return Objects.equals(intent1.appId(), intent2.appId()) && |
| 760 | Objects.equals(intent1.selector(), intent2.selector()) && |
| 761 | Objects.equals(intent1.treatment(), intent2.treatment()) && |
| 762 | Objects.equals(intent1.ingressPoint(), intent2.ingressPoint()) && |
| 763 | Objects.equals(intent1.egressPoint(), intent2.egressPoint()); |
| 764 | } |
| 765 | |
| 766 | @Override |
| 767 | public int hashCode() { |
| 768 | if (intent instanceof PointToPointIntent) { |
| 769 | PointToPointIntent p2pIntent = (PointToPointIntent) intent; |
| 770 | return Objects.hash(p2pIntent.appId(), |
| 771 | p2pIntent.resources(), |
| 772 | p2pIntent.selector(), |
| 773 | p2pIntent.treatment(), |
| 774 | p2pIntent.constraints(), |
| 775 | p2pIntent.ingressPoint(), |
| 776 | p2pIntent.egressPoint()); |
| 777 | } |
| 778 | if (intent instanceof MultiPointToSinglePointIntent) { |
| 779 | MultiPointToSinglePointIntent m2pIntent = |
| 780 | (MultiPointToSinglePointIntent) intent; |
| 781 | return Objects.hash(m2pIntent.appId(), |
| 782 | m2pIntent.resources(), |
| 783 | m2pIntent.selector(), |
| 784 | m2pIntent.treatment(), |
| 785 | m2pIntent.constraints(), |
| 786 | m2pIntent.ingressPoints(), |
| 787 | m2pIntent.egressPoint()); |
| 788 | } |
| 789 | checkArgument(false, "Intent type not recognized", intent); |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | @Override |
| 794 | public boolean equals(Object obj) { |
| 795 | if (this == obj) { |
| 796 | return true; |
| 797 | } |
| 798 | if ((obj == null) || (!(obj instanceof IntentKey))) { |
| 799 | return false; |
| 800 | } |
| 801 | IntentKey other = (IntentKey) obj; |
| 802 | |
| 803 | if (this.intent instanceof PointToPointIntent) { |
| 804 | if (!(other.intent instanceof PointToPointIntent)) { |
| 805 | return false; |
| 806 | } |
| 807 | return equalIntents((PointToPointIntent) this.intent, |
| 808 | (PointToPointIntent) other.intent); |
| 809 | } |
| 810 | if (this.intent instanceof MultiPointToSinglePointIntent) { |
| 811 | if (!(other.intent instanceof MultiPointToSinglePointIntent)) { |
| 812 | return false; |
| 813 | } |
| 814 | return equalIntents( |
| 815 | (MultiPointToSinglePointIntent) this.intent, |
| 816 | (MultiPointToSinglePointIntent) other.intent); |
| 817 | } |
| 818 | checkArgument(false, "Intent type not recognized", intent); |
| 819 | return false; |
| 820 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 821 | } |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 822 | |
| 823 | @Override |
| 824 | public void setUpConnectivityHostToHost(IpAddress dstIpAddress, |
| 825 | IpAddress srcIpAddress, |
| 826 | MacAddress srcMacAddress, |
| 827 | ConnectPoint srcConnectPoint) { |
| 828 | checkNotNull(dstIpAddress); |
| 829 | checkNotNull(srcIpAddress); |
| 830 | checkNotNull(srcMacAddress); |
| 831 | checkNotNull(srcConnectPoint); |
| 832 | |
| 833 | IpPrefix srcIpPrefix = srcIpAddress.toIpPrefix(); |
| 834 | IpPrefix dstIpPrefix = dstIpAddress.toIpPrefix(); |
| 835 | ConnectPoint dstConnectPoint = null; |
| 836 | MacAddress dstMacAddress = null; |
| 837 | |
| 838 | for (Host host : hostService.getHostsByIp(dstIpAddress)) { |
| 839 | if (host.mac() != null) { |
| 840 | dstMacAddress = host.mac(); |
| 841 | dstConnectPoint = host.location(); |
| 842 | break; |
| 843 | } |
| 844 | } |
| 845 | if (dstMacAddress == null) { |
| 846 | hostService.startMonitoringIp(dstIpAddress); |
| 847 | return; |
| 848 | } |
| 849 | |
| 850 | // |
| 851 | // Handle intent from source host to destination host |
| 852 | // |
| 853 | MultiPointToSinglePointIntent srcToDstIntent = |
| 854 | hostToHostIntentGenerator(dstIpAddress, dstConnectPoint, |
| 855 | dstMacAddress, srcConnectPoint); |
| 856 | submitReactiveIntent(dstIpPrefix, srcToDstIntent); |
| 857 | |
| 858 | // |
| 859 | // Handle intent from destination host to source host |
| 860 | // |
| 861 | |
| 862 | // Since we proactively handle the intent from destination host to |
| 863 | // source host, we should check whether there is an exiting intent |
| 864 | // first. |
| 865 | if (mp2pIntentExists(srcIpPrefix)) { |
| 866 | updateExistingMp2pIntent(srcIpPrefix, dstConnectPoint); |
| 867 | return; |
| 868 | } else { |
| 869 | // There is no existing intent, create a new one. |
| 870 | MultiPointToSinglePointIntent dstToSrcIntent = |
| 871 | hostToHostIntentGenerator(srcIpAddress, srcConnectPoint, |
| 872 | srcMacAddress, dstConnectPoint); |
| 873 | submitReactiveIntent(srcIpPrefix, dstToSrcIntent); |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | /** |
| 878 | * Generates MultiPointToSinglePointIntent for both source host and |
| 879 | * destination host located in local SDN network. |
| 880 | * |
| 881 | * @param dstIpAddress the destination IP address |
| 882 | * @param dstConnectPoint the destination host connect point |
| 883 | * @param dstMacAddress the MAC address of destination host |
| 884 | * @param srcConnectPoint the connect point where packet-in from |
| 885 | * @return the generated MultiPointToSinglePointIntent |
| 886 | */ |
| 887 | private MultiPointToSinglePointIntent hostToHostIntentGenerator( |
| 888 | IpAddress dstIpAddress, |
| 889 | ConnectPoint dstConnectPoint, |
| 890 | MacAddress dstMacAddress, |
| 891 | ConnectPoint srcConnectPoint) { |
| 892 | checkNotNull(dstIpAddress); |
| 893 | checkNotNull(dstConnectPoint); |
| 894 | checkNotNull(dstMacAddress); |
| 895 | checkNotNull(srcConnectPoint); |
| 896 | |
| 897 | Set<ConnectPoint> ingressPoints = new HashSet<ConnectPoint>(); |
| 898 | ingressPoints.add(srcConnectPoint); |
| 899 | IpPrefix dstIpPrefix = dstIpAddress.toIpPrefix(); |
| 900 | |
| 901 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 902 | if (dstIpAddress.isIp4()) { |
| 903 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 904 | selector.matchIPDst(dstIpPrefix); |
| 905 | } else { |
| 906 | selector.matchEthType(Ethernet.TYPE_IPV6); |
| 907 | selector.matchIPv6Dst(dstIpPrefix); |
| 908 | } |
| 909 | |
| 910 | // Rewrite the destination MAC address |
| 911 | TrafficTreatment.Builder treatment = |
| 912 | DefaultTrafficTreatment.builder().setEthDst(dstMacAddress); |
| 913 | |
| 914 | Key key = Key.of(dstIpPrefix.toString(), appId); |
| 915 | int priority = dstIpPrefix.prefixLength() * PRIORITY_MULTIPLIER |
| 916 | + PRIORITY_OFFSET; |
| 917 | MultiPointToSinglePointIntent intent = |
| 918 | MultiPointToSinglePointIntent.builder() |
| 919 | .appId(appId) |
| 920 | .key(key) |
| 921 | .selector(selector.build()) |
| 922 | .treatment(treatment.build()) |
| 923 | .ingressPoints(ingressPoints) |
| 924 | .egressPoint(dstConnectPoint) |
| 925 | .priority(priority) |
| 926 | .build(); |
| 927 | |
| 928 | log.trace("Generates ConnectivityHostToHost = {} ", intent); |
| 929 | return intent; |
| 930 | } |
| 931 | |
| 932 | @Override |
| 933 | public void updateExistingMp2pIntent(IpPrefix ipPrefix, |
| 934 | ConnectPoint ingressConnectPoint) { |
| 935 | checkNotNull(ipPrefix); |
| 936 | checkNotNull(ingressConnectPoint); |
| 937 | |
| 938 | MultiPointToSinglePointIntent existingIntent = |
| 939 | getExistingMp2pIntent(ipPrefix); |
| 940 | if (existingIntent != null) { |
| 941 | Set<ConnectPoint> ingressPoints = existingIntent.ingressPoints(); |
| 942 | // Add host connect point into ingressPoints of the existing intent |
| 943 | if (ingressPoints.add(ingressConnectPoint)) { |
| 944 | MultiPointToSinglePointIntent updatedMp2pIntent = |
| 945 | MultiPointToSinglePointIntent.builder() |
| 946 | .appId(appId) |
| 947 | .key(existingIntent.key()) |
| 948 | .selector(existingIntent.selector()) |
| 949 | .treatment(existingIntent.treatment()) |
| 950 | .ingressPoints(ingressPoints) |
| 951 | .egressPoint(existingIntent.egressPoint()) |
| 952 | .priority(existingIntent.priority()) |
| 953 | .build(); |
| 954 | |
| 955 | log.trace("Update an existing MultiPointToSinglePointIntent " |
| 956 | + "to new intent = {} ", updatedMp2pIntent); |
| 957 | submitReactiveIntent(ipPrefix, updatedMp2pIntent); |
| 958 | } |
| 959 | // If adding ingressConnectPoint to ingressPoints failed, it |
| 960 | // because between the time interval from checking existing intent |
| 961 | // to generating new intent, onos updated this intent due to other |
| 962 | // packet-in and the new intent also includes the |
| 963 | // ingressConnectPoint. This will not affect reactive routing. |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | @Override |
| 968 | public boolean mp2pIntentExists(IpPrefix ipPrefix) { |
| 969 | checkNotNull(ipPrefix); |
| 970 | return routeIntents.get(ipPrefix) == null ? false : true; |
| 971 | } |
| 972 | |
| 973 | /** |
| 974 | * Gets the existing MultiPointToSinglePointIntent from memory for a given |
| 975 | * IP prefix. |
| 976 | * |
| 977 | * @param ipPrefix the IP prefix used to find MultiPointToSinglePointIntent |
| 978 | * @return the MultiPointToSinglePointIntent if found, otherwise null |
| 979 | */ |
| 980 | private MultiPointToSinglePointIntent getExistingMp2pIntent(IpPrefix |
| 981 | ipPrefix) { |
| 982 | checkNotNull(ipPrefix); |
| 983 | return routeIntents.get(ipPrefix); |
| 984 | } |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 985 | } |