blob: 6ce8697b2ce5c8f04732b0fc47a4b1540e5f8335 [file] [log] [blame]
/*
* Copyright 2017-present Open Networking Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.pi.demo.app.tor;
import com.google.common.collect.Lists;
import org.apache.felix.scr.annotations.Component;
import org.onlab.packet.IpAddress;
import org.onlab.packet.MacAddress;
import org.onosproject.net.DeviceId;
import org.onosproject.net.Host;
import org.onosproject.net.Path;
import org.onosproject.net.Port;
import org.onosproject.net.PortNumber;
import org.onosproject.net.flow.DefaultTrafficSelector;
import org.onosproject.net.flow.DefaultTrafficTreatment;
import org.onosproject.net.flow.FlowRule;
import org.onosproject.net.flow.TrafficTreatment;
import org.onosproject.net.pi.runtime.PiAction;
import org.onosproject.net.pi.runtime.PiActionId;
import org.onosproject.net.pi.runtime.PiActionParam;
import org.onosproject.net.topology.DefaultTopologyVertex;
import org.onosproject.net.topology.Topology;
import org.onosproject.net.topology.TopologyGraph;
import org.onosproject.pi.demo.app.common.AbstractUpgradableFabricApp;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static com.google.common.collect.Collections2.permutations;
import static org.onlab.util.ImmutableByteSequence.copyFrom;
import static org.onosproject.pi.demo.app.tor.Combination.combinations;
import static org.onosproject.pi.demo.app.tor.TorInterpreter.*;
/**
* Implementation of an upgradable fabric app for TOR configuration.
*/
@Component(immediate = true)
public class TorApp extends AbstractUpgradableFabricApp {
private static final String APP_NAME = "org.onosproject.pi-tor";
public TorApp() {
super(APP_NAME, TorPipeconfFactory.getAll());
}
@Override
public boolean initDevice(DeviceId deviceId) {
// Nothing to do.
return true;
}
@Override
public List<FlowRule> generateLeafRules(DeviceId leaf, Host localHost, Set<Host> remoteHosts,
Collection<DeviceId> availableSpines, Topology topo)
throws FlowRuleGeneratorException {
// Get ports which connect this leaf switch to hosts.
Set<PortNumber> hostPorts = deviceService.getPorts(leaf)
.stream()
.filter(port -> !isFabricPort(port, topo))
.map(Port::number)
.collect(Collectors.toSet());
// Get ports which connect this leaf to the given available spines.
TopologyGraph graph = topologyService.getGraph(topo);
Set<PortNumber> fabricPorts = graph.getEdgesFrom(new DefaultTopologyVertex(leaf))
.stream()
.filter(e -> availableSpines.contains(e.dst().deviceId()))
.map(e -> e.link().src().port())
.collect(Collectors.toSet());
if (hostPorts.size() != 1 || fabricPorts.size() == 0) {
log.error("Leaf switch has invalid port configuration: hostPorts={}, fabricPorts={}",
hostPorts.size(), fabricPorts.size());
throw new FlowRuleGeneratorException();
}
PortNumber hostPort = hostPorts.iterator().next();
List<FlowRule> rules = Lists.newArrayList();
// Filtering rules
rules.addAll(generateFilteringRules(leaf, remoteHosts));
rules.addAll(generateFilteringRules(leaf, Collections.singleton(localHost)));
// FIXME: ignore ECMP for the moment
// if (fabricPorts.size() > 1) {
// // Do ECMP.
// Pair<PiTableAction, List<FlowRule>> result = provisionEcmpPiTableAction(leaf, fabricPorts);
// rules.addAll(result.getRight());
// treatment = DefaultTrafficTreatment.builder().piTableAction(result.getLeft()).build();
// } else {
// // Output on port.
// PortNumber outPort = fabricPorts.iterator().next();
// treatment = DefaultTrafficTreatment.builder().setOutput(outPort).build();
// }
PortNumber outPort = fabricPorts.iterator().next();
// From local host to remote ones.
for (Host remoteHost : remoteHosts) {
for (IpAddress ipAddr : remoteHost.ipAddresses()) {
FlowRule rule = flowRuleBuilder(leaf, L3_FWD_TBL_ID)
.withSelector(
DefaultTrafficSelector.builder()
.matchIPDst(ipAddr.toIpPrefix())
.build())
.withTreatment(
DefaultTrafficTreatment.builder()
.piTableAction(nextHopAction(outPort, localHost.mac(), remoteHost.mac()))
.build())
.build();
rules.add(rule);
}
}
// From remote hosts to the local one
for (IpAddress dstIpAddr : localHost.ipAddresses()) {
for (Host remoteHost : remoteHosts) {
FlowRule rule = flowRuleBuilder(leaf, L3_FWD_TBL_ID)
.withSelector(
DefaultTrafficSelector.builder()
.matchIPDst(dstIpAddr.toIpPrefix())
.build())
.withTreatment(
DefaultTrafficTreatment.builder()
.piTableAction(nextHopAction(hostPort, remoteHost.mac(), localHost.mac()))
.build())
.build();
rules.add(rule);
}
}
return rules;
}
private PiAction nextHopAction(PortNumber port, MacAddress smac, MacAddress dmac) {
return PiAction.builder()
.withId(SET_NEXT_HOP_ACT_ID)
.withParameter(new PiActionParam(PORT_ACT_PRM_ID, copyFrom(port.toLong())))
// Ignore L3 routing behaviour by keeping the original host mac addresses at each hop.
.withParameter(new PiActionParam(SMAC_ACT_PRM_ID, copyFrom(smac.toBytes())))
.withParameter(new PiActionParam(DMAC_ACT_PRM_ID, copyFrom(dmac.toBytes())))
.build();
}
@Override
public List<FlowRule> generateSpineRules(DeviceId spine, Set<Host> hosts, Topology topo)
throws FlowRuleGeneratorException {
List<FlowRule> rules = Lists.newArrayList();
rules.addAll(generateFilteringRules(spine, hosts));
// For each host pair (src -> dst)
for (Set<Host> hostCombs : combinations(hosts, 2)) {
for (List<Host> hostPair : permutations(hostCombs)) {
Host srcHost = hostPair.get(0);
Host dstHost = hostPair.get(1);
Set<Path> paths = topologyService.getPaths(topo, spine, dstHost.location().deviceId());
if (paths.size() == 0) {
log.warn("Can't find any path between spine {} and host {}", spine, dstHost);
throw new FlowRuleGeneratorException();
}
TrafficTreatment treatment;
// FIXME: ingore ECMP for the moment
// if (paths.size() == 1) {
// // Only one path, do output on port.
// PortNumber port = paths.iterator().next().src().port();
// treatment = DefaultTrafficTreatment.builder().setOutput(port).build();
// } else {
// // Multiple paths, do ECMP.
// Set<PortNumber> portNumbers = paths.stream().map(p -> p.src().port()).collect(toSet());
// Pair<PiTableAction, List<FlowRule>> result = provisionEcmpPiTableAction(deviceId, portNumbers);
// rules.addAll(result.getRight());
// treatment = DefaultTrafficTreatment.builder().piTableAction(result.getLeft()).build();
// }
PortNumber port = paths.iterator().next().src().port();
treatment = DefaultTrafficTreatment.builder()
.piTableAction(nextHopAction(port, srcHost.mac(), dstHost.mac()))
.build();
for (IpAddress dstIpAddr : dstHost.ipAddresses()) {
FlowRule rule = flowRuleBuilder(spine, L3_FWD_TBL_ID)
.withSelector(
DefaultTrafficSelector.builder()
.matchIPDst(dstIpAddr.toIpPrefix())
.build())
.withTreatment(treatment)
.build();
rules.add(rule);
}
}
}
return rules;
}
private List<FlowRule> generateFilteringRules(DeviceId deviceId, Collection<Host> dstHosts)
throws FlowRuleGeneratorException {
List<FlowRule> rules = Lists.newArrayList();
for (Host host : dstHosts) {
MacAddress ethAddr = host.mac();
FlowRule rule = flowRuleBuilder(deviceId, L3_FILTER_TBL_ID)
.withSelector(DefaultTrafficSelector.builder()
.matchEthDst(ethAddr)
.build())
.withTreatment(DefaultTrafficTreatment.builder()
.piTableAction(PiAction.builder()
.withId(PiActionId.of("NoAction"))
.build())
.build())
.build();
rules.add(rule);
}
return rules;
}
}