blob: db37d82b3214997841efaf638e3efb4c2dbbb058 [file] [log] [blame]
Carmelo Casconeefc0a922016-06-14 14:32:33 -07001/*
2 * Copyright 2016-present 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 */
16
17package org.onosproject.bmv2.demo.app.common;
18
19import com.google.common.collect.ImmutableList;
20import com.google.common.collect.ImmutableMap;
21import com.google.common.collect.ImmutableSet;
22import com.google.common.collect.Lists;
23import com.google.common.collect.Maps;
24import com.google.common.collect.Sets;
25import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
28import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
30import org.onosproject.app.ApplicationAdminService;
31import org.onosproject.bmv2.api.context.Bmv2DeviceContext;
32import org.onosproject.bmv2.api.service.Bmv2DeviceContextService;
33import org.onosproject.core.ApplicationId;
34import org.onosproject.core.CoreService;
35import org.onosproject.net.ConnectPoint;
36import org.onosproject.net.Device;
37import org.onosproject.net.DeviceId;
38import org.onosproject.net.Host;
39import org.onosproject.net.Port;
40import org.onosproject.net.device.DeviceEvent;
41import org.onosproject.net.device.DeviceListener;
42import org.onosproject.net.device.DeviceService;
43import org.onosproject.net.flow.DefaultFlowRule;
44import org.onosproject.net.flow.FlowRule;
45import org.onosproject.net.flow.FlowRuleOperations;
46import org.onosproject.net.flow.FlowRuleService;
47import org.onosproject.net.host.HostEvent;
48import org.onosproject.net.host.HostListener;
49import org.onosproject.net.host.HostService;
50import org.onosproject.net.topology.Topology;
51import org.onosproject.net.topology.TopologyEvent;
52import org.onosproject.net.topology.TopologyGraph;
53import org.onosproject.net.topology.TopologyListener;
54import org.onosproject.net.topology.TopologyService;
55import org.onosproject.net.topology.TopologyVertex;
56import org.slf4j.Logger;
57
58import java.util.Collection;
59import java.util.List;
60import java.util.Map;
61import java.util.Set;
62import java.util.concurrent.ExecutorService;
63import java.util.concurrent.Executors;
64import java.util.concurrent.TimeUnit;
65import java.util.stream.Collectors;
66import java.util.stream.Stream;
67
68import static com.google.common.base.Preconditions.checkNotNull;
69import static java.util.stream.Collectors.toSet;
70import static java.util.stream.Stream.concat;
71import static org.onlab.util.Tools.groupedThreads;
72import static org.onosproject.net.device.DeviceEvent.Type.*;
73import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED;
74import static org.slf4j.LoggerFactory.getLogger;
75
76/**
77 * Abstract implementation of an app providing fabric connectivity for a 2-stage Clos topology of BMv2 devices.
78 */
79@Component(immediate = true)
80public abstract class AbstractUpgradableFabricApp {
81
82 private static final Map<String, AbstractUpgradableFabricApp> APP_HANDLES = Maps.newConcurrentMap();
83
84 private static final int NUM_LEAFS = 3;
85 private static final int NUM_SPINES = 3;
86 private static final int FLOW_PRIORITY = 100;
87
88 private static final int CLEANUP_SLEEP = 1000;
89
90 protected final Logger log = getLogger(getClass());
91
92 private final TopologyListener topologyListener = new InternalTopologyListener();
93 private final DeviceListener deviceListener = new InternalDeviceListener();
94 private final HostListener hostListener = new InternalHostListener();
95
96 private final ExecutorService executorService = Executors
97 .newFixedThreadPool(8, groupedThreads("onos/bmv2-demo-app", "bmv2-app-task", log));
98
99 private final String appName;
100 private final String configurationName;
101
102 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
103 protected TopologyService topologyService;
104
105 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
106 protected DeviceService deviceService;
107
108 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
109 private HostService hostService;
110
111 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
112 private FlowRuleService flowRuleService;
113
114 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
115 private ApplicationAdminService appService;
116
117 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
118 private CoreService coreService;
119
120 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
121 private Bmv2DeviceContextService bmv2ContextService;
122
123 private boolean appActive = false;
124 private boolean appFreezed = false;
125
126 private boolean otherAppFound = false;
127 private AbstractUpgradableFabricApp otherApp;
128
129 private boolean flowRuleGenerated = false;
130 private ApplicationId appId;
131
132 private Bmv2DeviceContext bmv2Context;
133
134 private Set<DeviceId> leafSwitches;
135 private Set<DeviceId> spineSwitches;
136
137 private Map<DeviceId, List<FlowRule>> deviceFlowRules;
138 private Map<DeviceId, Boolean> rulesInstalled;
139
140 /**
141 * Creates a new Bmv2 Fabric Component.
142 *
143 * @param appName app name
144 * @param configurationName a common name for the P4 program / BMv2 configuration used by this app
145 * @param context a BMv2 device context to be used on devices
146 */
147 protected AbstractUpgradableFabricApp(String appName, String configurationName, Bmv2DeviceContext context) {
148 this.appName = checkNotNull(appName);
149 this.configurationName = checkNotNull(configurationName);
150 this.bmv2Context = checkNotNull(context);
151 }
152
153 @Activate
154 public void activate() {
155 log.info("Starting...");
156
157 appActive = true;
158 appFreezed = false;
159
160 if (APP_HANDLES.size() > 0) {
161 if (APP_HANDLES.size() > 1) {
162 throw new IllegalStateException("Found more than 1 active app handles");
163 }
164 otherAppFound = true;
165 otherApp = APP_HANDLES.values().iterator().next();
166 log.info("Found other fabric app active, signaling to freeze to {}...", otherApp.appName);
167 otherApp.setAppFreezed(true);
168 }
169
170 APP_HANDLES.put(appName, this);
171
172 appId = coreService.registerApplication(appName);
173
174 topologyService.addListener(topologyListener);
175 deviceService.addListener(deviceListener);
176 hostService.addListener(hostListener);
177
178 bmv2ContextService.registerInterpreterClassLoader(bmv2Context.interpreter().getClass(),
179 this.getClass().getClassLoader());
180
181 init();
182
183 log.info("STARTED", appId.id());
184 }
185
186 @Deactivate
187 public void deactivate() {
188 log.info("Stopping...");
189 try {
190 executorService.shutdown();
191 executorService.awaitTermination(5, TimeUnit.SECONDS);
192 } catch (InterruptedException e) {
193 List<Runnable> runningTasks = executorService.shutdownNow();
194 log.warn("Unable to stop the following tasks: {}", runningTasks);
195 }
196 deviceService.removeListener(deviceListener);
197 topologyService.removeListener(topologyListener);
198 hostService.removeListener(hostListener);
199 flowRuleService.removeFlowRulesById(appId);
200
201 appActive = false;
202 APP_HANDLES.remove(appName);
203
204 log.info("STOPPED");
205 }
206
207 private void init() {
208
209 // Reset any previous state
210 synchronized (this) {
211 flowRuleGenerated = Boolean.FALSE;
212 leafSwitches = Sets.newHashSet();
213 spineSwitches = Sets.newHashSet();
214 deviceFlowRules = Maps.newConcurrentMap();
215 rulesInstalled = Maps.newConcurrentMap();
216 }
217
218 // Start flow rules generator...
219 spawnTask(() -> generateFlowRules(topologyService.currentTopology(), Sets.newHashSet(hostService.getHosts())));
220 }
221
222 private void setAppFreezed(boolean appFreezed) {
223 this.appFreezed = appFreezed;
224 if (appFreezed) {
225 log.info("Freezing...");
226 } else {
227 log.info("Unfreezing...!");
228 }
229 }
230
231 /**
232 * Perform device initialization. Returns true if the operation was successful, false otherwise.
233 *
234 * @param deviceId a device id
235 * @return a boolean value
236 */
237 public abstract boolean initDevice(DeviceId deviceId);
238
239 /**
240 * Generates a list of flow rules for the given leaf switch, source host, destination hosts, spine switches and
241 * topology.
242 *
243 * @param leaf a leaf device id
244 * @param srcHost a source host
245 * @param dstHosts a collection of destination hosts
246 * @param spines a collection of spine device IDs
247 * @param topology a topology
248 * @return a list of flow rules
249 * @throws FlowRuleGeneratorException if flow rules cannot be generated
250 */
251 public abstract List<FlowRule> generateLeafRules(DeviceId leaf, Host srcHost, Collection<Host> dstHosts,
252 Collection<DeviceId> spines, Topology topology)
253 throws FlowRuleGeneratorException;
254
255 /**
256 * Generates a list of flow rules for the given spine switch, destination hosts and topology.
257 *
258 * @param deviceId a spine device id
259 * @param dstHosts a collection of destination hosts
260 * @param topology a topology
261 * @return a list of flow rules
262 * @throws FlowRuleGeneratorException if flow rules cannot be generated
263 */
264 public abstract List<FlowRule> generateSpineRules(DeviceId deviceId, Collection<Host> dstHosts, Topology topology)
265 throws FlowRuleGeneratorException;
266
267 private void deployRoutine() {
268 if (otherAppFound && otherApp.appActive) {
269 log.info("Starting update routine...");
270 updateRoutine();
271 appService.deactivate(otherApp.appId);
272 } else {
273 Stream.concat(leafSwitches.stream(), spineSwitches.stream())
274 .map(deviceService::getDevice)
275 .forEach(device -> spawnTask(() -> deployDevice(device)));
276 }
277 }
278
279 private void updateRoutine() {
280 Stream.concat(leafSwitches.stream(), spineSwitches.stream())
281 .forEach(did -> spawnTask(() -> {
282 cleanUpDevice(did);
283 try {
284 Thread.sleep(CLEANUP_SLEEP);
285 } catch (InterruptedException e) {
286 log.warn("Cleanup sleep interrupted!");
287 Thread.interrupted();
288 }
289 deployDevice(deviceService.getDevice(did));
290 }));
291 }
292
293 private void cleanUpDevice(DeviceId deviceId) {
294 List<FlowRule> flowRulesToRemove = Lists.newArrayList();
295 flowRuleService.getFlowEntries(deviceId).forEach(fe -> {
296 if (fe.appId() == otherApp.appId.id()) {
297 flowRulesToRemove.add(fe);
298 }
299 });
300 if (flowRulesToRemove.size() > 0) {
301 log.info("Cleaning {} old flow rules from {}...", flowRulesToRemove.size(), deviceId);
302 removeFlowRules(flowRulesToRemove);
303 }
304 }
305
306 /**
307 * Executes a device deploy.
308 *
309 * @param device a device
310 */
311 public void deployDevice(Device device) {
312 // Serialize executions per device ID using a concurrent map.
313 rulesInstalled.compute(device.id(), (did, deployed) -> {
314 Bmv2DeviceContext deviceContext = bmv2ContextService.getContext(device.id());
315 if (deviceContext == null) {
316 log.error("Unable to get context for device {}", device.id());
317 return deployed;
318 } else if (!deviceContext.equals(bmv2Context)) {
319 log.info("Swapping configuration to {} on device {}...", configurationName, device.id());
320 bmv2ContextService.triggerConfigurationSwap(device.id(), bmv2Context);
321 return deployed;
322 }
323
324 List<FlowRule> rules = deviceFlowRules.get(device.id());
325 if (initDevice(device.id())) {
326 if (deployed == null && rules != null && rules.size() > 0) {
327 log.info("Installing rules for {}...", did);
328 installFlowRules(rules);
329 return true;
330 }
331 } else {
332 log.warn("Filed to initialize device {}", device.id());
333 if (deployed != null && rules != null && rules.size() > 0) {
334 log.info("Removing rules for {}...", did);
335 removeFlowRules(rules);
336 return null;
337 }
338 }
339
340 return deployed;
341 });
342 }
343
344 private void spawnTask(Runnable task) {
345 executorService.execute(task);
346 }
347
348
349 private void installFlowRules(Collection<FlowRule> rules) {
350 FlowRuleOperations.Builder opsBuilder = FlowRuleOperations.builder();
351 rules.forEach(opsBuilder::add);
352 flowRuleService.apply(opsBuilder.build());
353 }
354
355 private void removeFlowRules(Collection<FlowRule> rules) {
356 FlowRuleOperations.Builder opsBuilder = FlowRuleOperations.builder();
357 rules.forEach(opsBuilder::remove);
358 flowRuleService.apply(opsBuilder.build());
359 }
360
361 /**
362 * Generates the flow rules to provide host-to-host connectivity for the given topology and hosts.
363 *
364 * @param topo a topology
365 * @param hosts a collection of hosts
366 */
367 private synchronized void generateFlowRules(Topology topo, Collection<Host> hosts) {
368
369 if (flowRuleGenerated) {
370 log.debug("Flow rules have been already generated, aborting...");
371 return;
372 }
373
374 log.debug("Starting flow rules generator...");
375
376 TopologyGraph graph = topologyService.getGraph(topo);
377 Set<DeviceId> spines = Sets.newHashSet();
378 Set<DeviceId> leafs = Sets.newHashSet();
379 graph.getVertexes().stream()
380 .map(TopologyVertex::deviceId)
381 .forEach(did -> (isSpine(did, topo) ? spines : leafs).add(did));
382
383 if (spines.size() != NUM_SPINES || leafs.size() != NUM_LEAFS) {
384 log.info("Invalid leaf/spine switches count, aborting... > leafCount={}, spineCount={}",
385 spines.size(), leafs.size());
386 return;
387 }
388
389 for (DeviceId did : spines) {
390 int portCount = deviceService.getPorts(did).size();
391 // Expected port count: num leafs + 1 redundant leaf link
392 if (portCount != (NUM_LEAFS + 1)) {
393 log.info("Invalid port count for spine, aborting... > deviceId={}, portCount={}", did, portCount);
394 return;
395 }
396 }
397 for (DeviceId did : leafs) {
398 int portCount = deviceService.getPorts(did).size();
399 // Expected port count: num spines + host port + 1 redundant spine link
400 if (portCount != (NUM_SPINES + 2)) {
401 log.info("Invalid port count for leaf, aborting... > deviceId={}, portCount={}", did, portCount);
402 return;
403 }
404 }
405
406 // Check hosts, number and exactly one per leaf
407 Map<DeviceId, Host> hostMap = Maps.newHashMap();
408 hosts.forEach(h -> hostMap.put(h.location().deviceId(), h));
409 if (hosts.size() != NUM_LEAFS || !leafs.equals(hostMap.keySet())) {
410 log.info("Wrong host configuration, aborting... > hostCount={}, hostMapz={}", hosts.size(), hostMap);
411 return;
412 }
413
414 List<FlowRule> newFlowRules = Lists.newArrayList();
415
416 try {
417 for (DeviceId deviceId : leafs) {
418 Host srcHost = hostMap.get(deviceId);
419 Set<Host> dstHosts = hosts.stream().filter(h -> h != srcHost).collect(toSet());
420 newFlowRules.addAll(generateLeafRules(deviceId, srcHost, dstHosts, spines, topo));
421 }
422 for (DeviceId deviceId : spines) {
423 newFlowRules.addAll(generateSpineRules(deviceId, hosts, topo));
424 }
425 } catch (FlowRuleGeneratorException e) {
426 log.warn("Exception while executing flow rule generator: ", e.toString());
427 return;
428 }
429
430 if (newFlowRules.size() == 0) {
431 // Something went wrong
432 log.error("0 flow rules generated, BUG?");
433 return;
434 }
435
436 // All good!
437 // Divide flow rules per device id...
438 ImmutableMap.Builder<DeviceId, List<FlowRule>> mapBuilder = ImmutableMap.builder();
439 concat(spines.stream(), leafs.stream())
440 .map(deviceId -> ImmutableList.copyOf(newFlowRules
441 .stream()
442 .filter(fr -> fr.deviceId().equals(deviceId))
443 .iterator()))
444 .forEach(frs -> mapBuilder.put(frs.get(0).deviceId(), frs));
445 this.deviceFlowRules = mapBuilder.build();
446
447 this.leafSwitches = ImmutableSet.copyOf(leafs);
448 this.spineSwitches = ImmutableSet.copyOf(spines);
449
450 // Avoid other executions to modify the generated flow rules.
451 flowRuleGenerated = true;
452
453 log.info("DONE! Generated {} flow rules for {} devices...", newFlowRules.size(), spines.size() + leafs.size());
454
455 // Deploy configuration.
456 spawnTask(this::deployRoutine);
457 }
458
459 /**
460 * Returns a new, pre-configured flow rule builder.
461 *
462 * @param did a device id
463 * @param tableName a table name
464 * @return a new flow rule builder
465 */
466 protected FlowRule.Builder flowRuleBuilder(DeviceId did, String tableName) throws FlowRuleGeneratorException {
467 Map<String, Integer> tableMap = bmv2Context.interpreter().tableIdMap().inverse();
468 if (tableMap.get(tableName) == null) {
469 throw new FlowRuleGeneratorException("Unknown table " + tableName);
470 }
471 return DefaultFlowRule.builder()
472 .forDevice(did)
473 .forTable(tableMap.get(tableName))
474 .fromApp(appId)
475 .withPriority(FLOW_PRIORITY)
476 .makePermanent();
477 }
478
479 private List<Port> getHostPorts(DeviceId deviceId, Topology topology) {
480 // Get all non-fabric ports.
481 return deviceService
482 .getPorts(deviceId)
483 .stream()
484 .filter(p -> !isFabricPort(p, topology))
485 .collect(Collectors.toList());
486 }
487
488 private boolean isSpine(DeviceId deviceId, Topology topology) {
489 // True if all ports are fabric.
490 return getHostPorts(deviceId, topology).size() == 0;
491 }
492
493 protected boolean isFabricPort(Port port, Topology topology) {
494 // True if the port connects this device to another infrastructure device.
495 return topologyService.isInfrastructure(topology, new ConnectPoint(port.element().id(), port.number()));
496 }
497
498 /**
499 * A listener of topology events that executes a flow rule generation task each time a device is added.
500 */
501 private class InternalTopologyListener implements TopologyListener {
502
503 @Override
504 public void event(TopologyEvent event) {
505 spawnTask(() -> generateFlowRules(event.subject(), Sets.newHashSet(hostService.getHosts())));
506 }
507
508 @Override
509 public boolean isRelevant(TopologyEvent event) {
510 return !appFreezed &&
511 // If at least one reason is of type DEVICE_ADDED.
512 event.reasons().stream().
513 filter(r -> r instanceof DeviceEvent)
514 .filter(r -> ((DeviceEvent) r).type() == DEVICE_ADDED)
515 .findAny()
516 .isPresent();
517 }
518 }
519
520 /**
521 * A listener of device events that executes a device deploy task each time a device is added, updated or
522 * re-connects.
523 */
524 private class InternalDeviceListener implements DeviceListener {
525 @Override
526 public void event(DeviceEvent event) {
527 spawnTask(() -> deployDevice(event.subject()));
528 }
529
530 @Override
531 public boolean isRelevant(DeviceEvent event) {
532 return !appFreezed &&
533 (event.type() == DEVICE_ADDED ||
534 event.type() == DEVICE_UPDATED ||
535 (event.type() == DEVICE_AVAILABILITY_CHANGED &&
536 deviceService.isAvailable(event.subject().id())));
537 }
538 }
539
540 /**
541 * A listener of host events that generates flow rules each time a new host is added.
542 */
543 private class InternalHostListener implements HostListener {
544 @Override
545 public void event(HostEvent event) {
546 spawnTask(() -> generateFlowRules(topologyService.currentTopology(),
547 Sets.newHashSet(hostService.getHosts())));
548 }
549
550 @Override
551 public boolean isRelevant(HostEvent event) {
552 return !appFreezed && event.type() == HOST_ADDED;
553 }
554 }
555
556 /**
557 * An exception occurred while generating flow rules for this fabric.
558 */
559 public class FlowRuleGeneratorException extends Exception {
560
561 public FlowRuleGeneratorException() {
562 }
563
564 public FlowRuleGeneratorException(String msg) {
565 super(msg);
566 }
567
568 public FlowRuleGeneratorException(Exception cause) {
569 super(cause);
570 }
571 }
572}