wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.pi.demo.app.common; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableMap; |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import com.google.common.collect.Lists; |
| 23 | import com.google.common.collect.Maps; |
| 24 | import com.google.common.collect.Sets; |
| 25 | import org.apache.felix.scr.annotations.Activate; |
| 26 | import org.apache.felix.scr.annotations.Component; |
| 27 | import org.apache.felix.scr.annotations.Deactivate; |
| 28 | import org.apache.felix.scr.annotations.Reference; |
| 29 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 30 | import org.onosproject.app.ApplicationAdminService; |
| 31 | import org.onosproject.core.ApplicationId; |
| 32 | import org.onosproject.core.CoreService; |
| 33 | import org.onosproject.net.ConnectPoint; |
| 34 | import org.onosproject.net.Device; |
| 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.net.Host; |
| 37 | import org.onosproject.net.Port; |
| 38 | import org.onosproject.net.device.DeviceEvent; |
| 39 | import org.onosproject.net.device.DeviceListener; |
| 40 | import org.onosproject.net.device.DeviceService; |
| 41 | import org.onosproject.net.flow.DefaultFlowRule; |
| 42 | import org.onosproject.net.flow.FlowRule; |
| 43 | import org.onosproject.net.flow.FlowRuleOperations; |
| 44 | import org.onosproject.net.flow.FlowRuleService; |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 45 | import org.onosproject.net.group.GroupService; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 46 | import org.onosproject.net.host.HostService; |
| 47 | import org.onosproject.net.pi.model.PiPipeconf; |
Carmelo Cascone | 6e85404 | 2017-09-11 21:37:53 +0200 | [diff] [blame] | 48 | import org.onosproject.net.pi.model.PiPipeconfId; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 49 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 50 | import org.onosproject.net.pi.model.PiTableId; |
Carmelo Cascone | 39c28ca | 2017-11-15 13:03:57 -0800 | [diff] [blame] | 51 | import org.onosproject.net.pi.service.PiPipeconfService; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 52 | import org.onosproject.net.topology.Topology; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 53 | import org.onosproject.net.topology.TopologyGraph; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 54 | import org.onosproject.net.topology.TopologyService; |
| 55 | import org.onosproject.net.topology.TopologyVertex; |
| 56 | import org.slf4j.Logger; |
| 57 | |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 58 | import java.util.Arrays; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 59 | import java.util.Collection; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 60 | import java.util.List; |
| 61 | import java.util.Map; |
| 62 | import java.util.Set; |
| 63 | import java.util.concurrent.ConcurrentMap; |
| 64 | import java.util.concurrent.ExecutorService; |
| 65 | import java.util.concurrent.Executors; |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 66 | import java.util.concurrent.ScheduledExecutorService; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 67 | import java.util.concurrent.TimeUnit; |
| 68 | import java.util.concurrent.locks.Lock; |
| 69 | import java.util.concurrent.locks.ReentrantLock; |
| 70 | import java.util.stream.Collectors; |
| 71 | import java.util.stream.Stream; |
| 72 | |
Carmelo Cascone | 6e85404 | 2017-09-11 21:37:53 +0200 | [diff] [blame] | 73 | import static com.google.common.base.Preconditions.checkArgument; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 74 | import static com.google.common.base.Preconditions.checkNotNull; |
Carmelo Cascone | 1fb27d3 | 2017-08-25 20:40:20 +0200 | [diff] [blame] | 75 | import static java.lang.String.format; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 76 | import static java.util.stream.Collectors.toSet; |
| 77 | import static java.util.stream.Stream.concat; |
| 78 | import static org.onlab.util.Tools.groupedThreads; |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 79 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED; |
| 80 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED; |
| 81 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_UPDATED; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 82 | import static org.slf4j.LoggerFactory.getLogger; |
| 83 | |
| 84 | /** |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 85 | * Abstract implementation of an app providing fabric connectivity for a 2-stage |
| 86 | * Clos topology of P4Runtime devices. |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 87 | */ |
| 88 | @Component(immediate = true) |
| 89 | public abstract class AbstractUpgradableFabricApp { |
| 90 | |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 91 | private static final Map<String, AbstractUpgradableFabricApp> |
| 92 | APP_HANDLES = Maps.newConcurrentMap(); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 93 | |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 94 | // TOPO_SIZE should be the same of the --size argument when running bmv2-demo.py |
| 95 | private static final int TOPO_SIZE = 2; |
| 96 | private static final boolean WITH_IMBALANCED_STRIPING = false; |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 97 | private static final int HASHED_LINKS = TOPO_SIZE + (WITH_IMBALANCED_STRIPING ? 1 : 0); |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 98 | |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 99 | private static final int FLOW_PRIORITY = 100; |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 100 | private static final int CHECK_TOPOLOGY_INTERVAL_SECONDS = 5; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 101 | |
| 102 | private static final int CLEANUP_SLEEP = 2000; |
| 103 | |
| 104 | protected final Logger log = getLogger(getClass()); |
| 105 | |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 106 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 107 | |
| 108 | private final ExecutorService executorService = Executors |
| 109 | .newFixedThreadPool(8, groupedThreads("onos/pi-demo-app", "pi-app-task", log)); |
| 110 | |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 111 | private final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); |
| 112 | |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 113 | private final String appName; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 114 | |
| 115 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 116 | protected TopologyService topologyService; |
| 117 | |
| 118 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 119 | protected DeviceService deviceService; |
| 120 | |
| 121 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 122 | private HostService hostService; |
| 123 | |
| 124 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 125 | private FlowRuleService flowRuleService; |
| 126 | |
| 127 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 128 | protected GroupService groupService; |
| 129 | |
| 130 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 131 | private ApplicationAdminService appService; |
| 132 | |
| 133 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 134 | private CoreService coreService; |
| 135 | |
| 136 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 137 | private PiPipeconfService piPipeconfService; |
| 138 | |
| 139 | private boolean appActive = false; |
| 140 | private boolean appFreezed = false; |
| 141 | |
| 142 | private boolean otherAppFound = false; |
| 143 | private AbstractUpgradableFabricApp otherApp; |
| 144 | |
| 145 | private boolean flowRuleGenerated = false; |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 146 | protected ApplicationId appId; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 147 | |
Carmelo Cascone | 6e85404 | 2017-09-11 21:37:53 +0200 | [diff] [blame] | 148 | private Collection<PiPipeconf> appPipeconfs; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 149 | |
| 150 | private Set<DeviceId> leafSwitches; |
| 151 | private Set<DeviceId> spineSwitches; |
| 152 | |
| 153 | private Map<DeviceId, List<FlowRule>> deviceFlowRules; |
| 154 | private Map<DeviceId, Boolean> pipeconfFlags; |
| 155 | private Map<DeviceId, Boolean> ruleFlags; |
| 156 | |
| 157 | private ConcurrentMap<DeviceId, Lock> deviceLocks = Maps.newConcurrentMap(); |
| 158 | |
| 159 | /** |
| 160 | * Creates a new PI fabric app. |
| 161 | * |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 162 | * @param appName app name |
Carmelo Cascone | 6e85404 | 2017-09-11 21:37:53 +0200 | [diff] [blame] | 163 | * @param appPipeconfs collection of compatible pipeconfs |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 164 | */ |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 165 | protected AbstractUpgradableFabricApp(String appName, |
| 166 | Collection<PiPipeconf> appPipeconfs) { |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 167 | this.appName = checkNotNull(appName); |
Carmelo Cascone | 6e85404 | 2017-09-11 21:37:53 +0200 | [diff] [blame] | 168 | this.appPipeconfs = checkNotNull(appPipeconfs); |
| 169 | checkArgument(appPipeconfs.size() > 0, "appPipeconfs cannot have size 0"); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Activate |
| 173 | public void activate() { |
| 174 | log.info("Starting..."); |
| 175 | |
| 176 | appActive = true; |
| 177 | appFreezed = false; |
| 178 | |
| 179 | if (APP_HANDLES.size() > 0) { |
| 180 | if (APP_HANDLES.size() > 1) { |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 181 | throw new IllegalStateException( |
| 182 | "Found more than 1 active app handles"); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 183 | } |
| 184 | otherAppFound = true; |
| 185 | otherApp = APP_HANDLES.values().iterator().next(); |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 186 | log.info("Found other fabric app active, signaling to freeze to {}...", |
| 187 | otherApp.appName); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 188 | otherApp.setAppFreezed(true); |
| 189 | } |
| 190 | |
| 191 | APP_HANDLES.put(appName, this); |
| 192 | |
| 193 | appId = coreService.registerApplication(appName); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 194 | deviceService.addListener(deviceListener); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 195 | |
| 196 | init(); |
| 197 | |
| 198 | log.info("STARTED", appId.id()); |
| 199 | } |
| 200 | |
| 201 | @Deactivate |
| 202 | public void deactivate() { |
| 203 | log.info("Stopping..."); |
| 204 | try { |
| 205 | executorService.shutdown(); |
| 206 | executorService.awaitTermination(5, TimeUnit.SECONDS); |
| 207 | } catch (InterruptedException e) { |
| 208 | List<Runnable> runningTasks = executorService.shutdownNow(); |
| 209 | log.warn("Unable to stop the following tasks: {}", runningTasks); |
Ray Milkey | 5c7d488 | 2018-02-05 14:50:39 -0800 | [diff] [blame] | 210 | Thread.currentThread().interrupt(); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 211 | } |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 212 | scheduledExecutorService.shutdown(); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 213 | deviceService.removeListener(deviceListener); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 214 | flowRuleService.removeFlowRulesById(appId); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 215 | |
| 216 | appActive = false; |
| 217 | APP_HANDLES.remove(appName); |
| 218 | |
| 219 | log.info("STOPPED"); |
| 220 | } |
| 221 | |
| 222 | private void init() { |
| 223 | |
| 224 | // Reset any previous state |
| 225 | synchronized (this) { |
| 226 | flowRuleGenerated = Boolean.FALSE; |
| 227 | leafSwitches = Sets.newHashSet(); |
| 228 | spineSwitches = Sets.newHashSet(); |
| 229 | deviceFlowRules = Maps.newConcurrentMap(); |
| 230 | ruleFlags = Maps.newConcurrentMap(); |
| 231 | pipeconfFlags = Maps.newConcurrentMap(); |
| 232 | } |
| 233 | |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 234 | // Schedules a thread that periodically checks the topology, as soon as |
| 235 | // it corresponds to the expected one, it generates the necessary flow |
| 236 | // rules and starts the deploy process on each device. |
| 237 | scheduledExecutorService.scheduleAtFixedRate( |
| 238 | this::checkTopologyAndGenerateFlowRules, |
| 239 | 0, CHECK_TOPOLOGY_INTERVAL_SECONDS, TimeUnit.SECONDS); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | private void setAppFreezed(boolean appFreezed) { |
| 243 | this.appFreezed = appFreezed; |
| 244 | if (appFreezed) { |
| 245 | log.info("Freezing..."); |
| 246 | } else { |
| 247 | log.info("Unfreezing...!"); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 252 | * Perform device initialization. Returns true if the operation was |
| 253 | * successful, false otherwise. |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 254 | * |
| 255 | * @param deviceId a device id |
| 256 | * @return a boolean value |
| 257 | */ |
| 258 | public abstract boolean initDevice(DeviceId deviceId); |
| 259 | |
| 260 | /** |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 261 | * Generates a list of flow rules for the given leaf switch, source host, |
| 262 | * destination hosts, spine switches and topology. |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 263 | * |
| 264 | * @param leaf a leaf device id |
| 265 | * @param srcHost a source host |
| 266 | * @param dstHosts a collection of destination hosts |
| 267 | * @param spines a collection of spine device IDs |
| 268 | * @param topology a topology |
| 269 | * @return a list of flow rules |
| 270 | * @throws FlowRuleGeneratorException if flow rules cannot be generated |
| 271 | */ |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 272 | public abstract List<FlowRule> generateLeafRules(DeviceId leaf, Host srcHost, |
| 273 | Set<Host> dstHosts, |
| 274 | Set<DeviceId> spines, |
| 275 | Topology topology) |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 276 | throws FlowRuleGeneratorException; |
| 277 | |
| 278 | /** |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 279 | * Generates a list of flow rules for the given spine switch, destination |
| 280 | * hosts and topology. |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 281 | * |
| 282 | * @param deviceId a spine device id |
| 283 | * @param dstHosts a collection of destination hosts |
| 284 | * @param topology a topology |
| 285 | * @return a list of flow rules |
| 286 | * @throws FlowRuleGeneratorException if flow rules cannot be generated |
| 287 | */ |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 288 | public abstract List<FlowRule> generateSpineRules(DeviceId deviceId, |
| 289 | Set<Host> dstHosts, |
| 290 | Topology topology) |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 291 | throws FlowRuleGeneratorException; |
| 292 | |
| 293 | private void deployAllDevices() { |
| 294 | if (otherAppFound && otherApp.appActive) { |
| 295 | log.info("Deactivating other app..."); |
| 296 | appService.deactivate(otherApp.appId); |
| 297 | try { |
| 298 | Thread.sleep(CLEANUP_SLEEP); |
| 299 | } catch (InterruptedException e) { |
| 300 | log.warn("Cleanup sleep interrupted!"); |
Ray Milkey | 5c7d488 | 2018-02-05 14:50:39 -0800 | [diff] [blame] | 301 | Thread.currentThread().interrupt(); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | Stream.concat(leafSwitches.stream(), spineSwitches.stream()) |
| 306 | .map(deviceService::getDevice) |
| 307 | .forEach(device -> spawnTask(() -> deployDevice(device))); |
| 308 | } |
| 309 | |
Carmelo Cascone | 6e85404 | 2017-09-11 21:37:53 +0200 | [diff] [blame] | 310 | private boolean matchPipeconf(PiPipeconfId piPipeconfId) { |
| 311 | return appPipeconfs.stream() |
| 312 | .anyMatch(p -> p.id().equals(piPipeconfId)); |
| 313 | } |
| 314 | |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 315 | /** |
| 316 | * Executes a device deploy. |
| 317 | * |
| 318 | * @param device a device |
| 319 | */ |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 320 | private void deployDevice(Device device) { |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 321 | |
| 322 | DeviceId deviceId = device.id(); |
| 323 | |
| 324 | // Synchronize executions over the same device. |
| 325 | Lock lock = deviceLocks.computeIfAbsent(deviceId, k -> new ReentrantLock()); |
| 326 | lock.lock(); |
| 327 | |
| 328 | try { |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 329 | // Set pipeconf flag if not already done. |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 330 | if (!pipeconfFlags.getOrDefault(deviceId, false)) { |
Carmelo Cascone | 1fb27d3 | 2017-08-25 20:40:20 +0200 | [diff] [blame] | 331 | if (piPipeconfService.ofDevice(deviceId).isPresent() && |
Carmelo Cascone | 6e85404 | 2017-09-11 21:37:53 +0200 | [diff] [blame] | 332 | matchPipeconf(piPipeconfService.ofDevice(deviceId).get())) { |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 333 | pipeconfFlags.put(device.id(), true); |
| 334 | } else { |
Carmelo Cascone | 1fb27d3 | 2017-08-25 20:40:20 +0200 | [diff] [blame] | 335 | log.warn("Wrong pipeconf for {}, expecting {}, but found {}, aborting deploy", |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 336 | deviceId, Arrays.toString(appPipeconfs.toArray()), |
Carmelo Cascone | 6e85404 | 2017-09-11 21:37:53 +0200 | [diff] [blame] | 337 | piPipeconfService.ofDevice(deviceId).get()); |
Carmelo Cascone | 1fb27d3 | 2017-08-25 20:40:20 +0200 | [diff] [blame] | 338 | return; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
| 342 | // Initialize device. |
| 343 | if (!initDevice(deviceId)) { |
| 344 | log.warn("Failed to initialize device {}", deviceId); |
| 345 | } |
| 346 | |
| 347 | // Install rules. |
Carmelo Cascone | 1fb27d3 | 2017-08-25 20:40:20 +0200 | [diff] [blame] | 348 | if (!ruleFlags.getOrDefault(deviceId, false) && |
| 349 | deviceFlowRules.containsKey(deviceId)) { |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 350 | log.info("Installing {} rules for {}...", |
| 351 | deviceFlowRules.get(deviceId).size(), deviceId); |
Carmelo Cascone | 1fb27d3 | 2017-08-25 20:40:20 +0200 | [diff] [blame] | 352 | installFlowRules(deviceFlowRules.get(deviceId)); |
| 353 | ruleFlags.put(deviceId, true); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 354 | } |
| 355 | } finally { |
| 356 | lock.unlock(); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | private void spawnTask(Runnable task) { |
| 361 | executorService.execute(task); |
| 362 | } |
| 363 | |
| 364 | |
| 365 | private void installFlowRules(Collection<FlowRule> rules) { |
| 366 | FlowRuleOperations.Builder opsBuilder = FlowRuleOperations.builder(); |
| 367 | rules.forEach(opsBuilder::add); |
| 368 | flowRuleService.apply(opsBuilder.build()); |
| 369 | } |
| 370 | |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 371 | /** |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 372 | * Generates flow rules to provide host-to-host connectivity for the given |
| 373 | * topology and hosts. |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 374 | */ |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 375 | private synchronized void checkTopologyAndGenerateFlowRules() { |
| 376 | |
| 377 | Topology topo = topologyService.currentTopology(); |
| 378 | Set<Host> hosts = Sets.newHashSet(hostService.getHosts()); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 379 | |
| 380 | if (flowRuleGenerated) { |
| 381 | log.debug("Flow rules have been already generated, aborting..."); |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | log.debug("Starting flow rules generator..."); |
| 386 | |
| 387 | TopologyGraph graph = topologyService.getGraph(topo); |
| 388 | Set<DeviceId> spines = Sets.newHashSet(); |
| 389 | Set<DeviceId> leafs = Sets.newHashSet(); |
| 390 | graph.getVertexes().stream() |
| 391 | .map(TopologyVertex::deviceId) |
| 392 | .forEach(did -> (isSpine(did, topo) ? spines : leafs).add(did)); |
| 393 | |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 394 | if (spines.size() != TOPO_SIZE || leafs.size() != TOPO_SIZE) { |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 395 | log.info("Invalid leaf/spine count, aborting... > leafCount={}, spineCount={}", |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 396 | spines.size(), leafs.size()); |
| 397 | return; |
| 398 | } |
| 399 | |
| 400 | for (DeviceId did : spines) { |
| 401 | int portCount = deviceService.getPorts(did).size(); |
Carmelo Cascone | 1fb27d3 | 2017-08-25 20:40:20 +0200 | [diff] [blame] | 402 | // Expected port count: num leafs + 1 redundant leaf link (if imbalanced) |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 403 | if (portCount != HASHED_LINKS) { |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 404 | log.info("Invalid port count for spine, aborting... > deviceId={}, portCount={}", |
| 405 | did, portCount); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 406 | return; |
| 407 | } |
| 408 | } |
| 409 | for (DeviceId did : leafs) { |
| 410 | int portCount = deviceService.getPorts(did).size(); |
| 411 | // Expected port count: num spines + host port + 1 redundant spine link |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 412 | if (portCount != HASHED_LINKS + 1) { |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 413 | log.info("Invalid port count for leaf, aborting... > deviceId={}, portCount={}", |
| 414 | did, portCount); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 415 | return; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | // Check hosts, number and exactly one per leaf |
| 420 | Map<DeviceId, Host> hostMap = Maps.newHashMap(); |
| 421 | hosts.forEach(h -> hostMap.put(h.location().deviceId(), h)); |
Carmelo Cascone | 3929cc8 | 2017-09-06 13:34:25 +0200 | [diff] [blame] | 422 | if (hosts.size() != TOPO_SIZE || !leafs.equals(hostMap.keySet())) { |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 423 | log.info("Wrong host configuration, aborting... > hostCount={}, hostMapz={}", |
| 424 | hosts.size(), hostMap); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 425 | return; |
| 426 | } |
| 427 | |
| 428 | List<FlowRule> newFlowRules = Lists.newArrayList(); |
| 429 | |
| 430 | try { |
| 431 | for (DeviceId deviceId : leafs) { |
| 432 | Host srcHost = hostMap.get(deviceId); |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 433 | Set<Host> dstHosts = hosts.stream() |
| 434 | .filter(h -> h != srcHost) |
| 435 | .collect(toSet()); |
| 436 | newFlowRules.addAll(generateLeafRules(deviceId, srcHost, |
| 437 | dstHosts, spines, topo)); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 438 | } |
| 439 | for (DeviceId deviceId : spines) { |
| 440 | newFlowRules.addAll(generateSpineRules(deviceId, hosts, topo)); |
| 441 | } |
| 442 | } catch (FlowRuleGeneratorException e) { |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 443 | log.warn("Exception while executing flow rule generator: {}", |
| 444 | e.getMessage()); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 445 | return; |
| 446 | } |
| 447 | |
| 448 | if (newFlowRules.size() == 0) { |
| 449 | // Something went wrong |
| 450 | log.error("0 flow rules generated, BUG?"); |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | // All good! |
| 455 | // Divide flow rules per device id... |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 456 | ImmutableMap.Builder<DeviceId, List<FlowRule>> mapBuilder = |
| 457 | ImmutableMap.builder(); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 458 | concat(spines.stream(), leafs.stream()) |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 459 | .map(deviceId -> ImmutableList.copyOf( |
| 460 | newFlowRules.stream() |
| 461 | .filter(fr -> fr.deviceId().equals(deviceId)) |
| 462 | .iterator())) |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 463 | .forEach(frs -> mapBuilder.put(frs.get(0).deviceId(), frs)); |
| 464 | this.deviceFlowRules = mapBuilder.build(); |
| 465 | |
| 466 | this.leafSwitches = ImmutableSet.copyOf(leafs); |
| 467 | this.spineSwitches = ImmutableSet.copyOf(spines); |
| 468 | |
| 469 | // Avoid other executions to modify the generated flow rules. |
| 470 | flowRuleGenerated = true; |
| 471 | |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 472 | log.info("Generated {} flow rules for {} devices", |
| 473 | newFlowRules.size(), spines.size() + leafs.size()); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 474 | |
| 475 | spawnTask(this::deployAllDevices); |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Returns a new, pre-configured flow rule builder. |
| 480 | * |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 481 | * @param did a device id |
| 482 | * @param tableId a table id |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 483 | * @return a new flow rule builder |
| 484 | */ |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 485 | protected FlowRule.Builder flowRuleBuilder(DeviceId did, PiTableId tableId) |
| 486 | throws FlowRuleGeneratorException { |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 487 | |
Carmelo Cascone | 1fb27d3 | 2017-08-25 20:40:20 +0200 | [diff] [blame] | 488 | final Device device = deviceService.getDevice(did); |
| 489 | if (!device.is(PiPipelineInterpreter.class)) { |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 490 | throw new FlowRuleGeneratorException(format( |
| 491 | "Device %s has no PiPipelineInterpreter", did)); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 492 | } |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 493 | |
| 494 | return DefaultFlowRule.builder() |
| 495 | .forDevice(did) |
Carmelo Cascone | 03f343d | 2017-11-13 16:54:39 -0800 | [diff] [blame] | 496 | .forTable(tableId) |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 497 | .fromApp(appId) |
| 498 | .withPriority(FLOW_PRIORITY) |
| 499 | .makePermanent(); |
| 500 | } |
| 501 | |
| 502 | private List<Port> getHostPorts(DeviceId deviceId, Topology topology) { |
| 503 | // Get all non-fabric ports. |
| 504 | return deviceService |
| 505 | .getPorts(deviceId) |
| 506 | .stream() |
| 507 | .filter(p -> !isFabricPort(p, topology)) |
| 508 | .collect(Collectors.toList()); |
| 509 | } |
| 510 | |
| 511 | private boolean isSpine(DeviceId deviceId, Topology topology) { |
| 512 | // True if all ports are fabric. |
| 513 | return getHostPorts(deviceId, topology).size() == 0; |
| 514 | } |
| 515 | |
| 516 | protected boolean isFabricPort(Port port, Topology topology) { |
| 517 | // True if the port connects this device to another infrastructure device. |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 518 | return topologyService.isInfrastructure( |
| 519 | topology, new ConnectPoint(port.element().id(), port.number())); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | /** |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 523 | * A listener of device events that executes a device deploy task each time |
| 524 | * a device is added, updated or re-connects. |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 525 | */ |
| 526 | private class InternalDeviceListener implements DeviceListener { |
| 527 | @Override |
| 528 | public void event(DeviceEvent event) { |
| 529 | spawnTask(() -> deployDevice(event.subject())); |
| 530 | } |
| 531 | |
| 532 | @Override |
| 533 | public boolean isRelevant(DeviceEvent event) { |
| 534 | return !appFreezed && |
| 535 | (event.type() == DEVICE_ADDED || |
| 536 | event.type() == DEVICE_UPDATED || |
| 537 | (event.type() == DEVICE_AVAILABILITY_CHANGED && |
| 538 | deviceService.isAvailable(event.subject().id()))); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | /** |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 543 | * An exception occurred while generating flow rules for this fabric. |
| 544 | */ |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 545 | public static class FlowRuleGeneratorException extends Exception { |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 546 | |
| 547 | public FlowRuleGeneratorException() { |
| 548 | } |
| 549 | |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame] | 550 | FlowRuleGeneratorException(String msg) { |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 551 | super(msg); |
| 552 | } |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 553 | } |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 554 | } |