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.ecmp; |
| 18 | |
| 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.collect.Maps; |
| 21 | import org.apache.commons.lang3.tuple.Pair; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.onlab.util.ImmutableByteSequence; |
Carmelo Cascone | 9f760a1 | 2017-08-24 13:14:53 +0200 | [diff] [blame] | 24 | import org.onosproject.driver.pipeline.DefaultSingleTablePipeline; |
| 25 | import org.onosproject.net.behaviour.Pipeliner; |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 26 | import org.onosproject.net.flow.TrafficSelector; |
| 27 | import org.onosproject.net.flow.criteria.Criterion; |
| 28 | import org.onosproject.net.flow.criteria.PiCriterion; |
| 29 | import org.onosproject.net.pi.runtime.PiAction; |
| 30 | import org.onosproject.net.pi.runtime.PiActionId; |
| 31 | import org.onosproject.net.pi.runtime.PiActionParam; |
| 32 | import org.onosproject.net.pi.runtime.PiActionParamId; |
| 33 | import org.onosproject.net.pi.runtime.PiHeaderFieldId; |
| 34 | import org.onosproject.net.pi.runtime.PiTableAction; |
| 35 | import org.onosproject.pi.demo.app.common.AbstractUpgradableFabricApp; |
| 36 | import org.onosproject.net.DeviceId; |
| 37 | import org.onosproject.net.Host; |
| 38 | import org.onosproject.net.Path; |
| 39 | import org.onosproject.net.Port; |
| 40 | import org.onosproject.net.PortNumber; |
| 41 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 42 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 43 | import org.onosproject.net.flow.FlowRule; |
| 44 | import org.onosproject.net.flow.TrafficTreatment; |
| 45 | import org.onosproject.net.pi.model.DefaultPiPipeconf; |
| 46 | import org.onosproject.net.pi.model.PiPipeconf; |
| 47 | import org.onosproject.net.pi.model.PiPipeconfId; |
| 48 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
| 49 | import org.onosproject.net.topology.DefaultTopologyVertex; |
| 50 | import org.onosproject.net.topology.Topology; |
| 51 | import org.onosproject.net.topology.TopologyGraph; |
| 52 | import org.onosproject.bmv2.model.Bmv2PipelineModelParser; |
| 53 | |
| 54 | import java.net.URL; |
| 55 | import java.util.Collection; |
| 56 | import java.util.Iterator; |
| 57 | import java.util.List; |
| 58 | import java.util.Map; |
| 59 | import java.util.Set; |
| 60 | import java.util.stream.Collectors; |
| 61 | |
| 62 | import static java.util.stream.Collectors.toSet; |
| 63 | import static org.onlab.packet.EthType.EtherType.IPV4; |
| 64 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.BMV2_JSON; |
| 65 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT; |
| 66 | import static org.onosproject.pi.demo.app.ecmp.EcmpInterpreter.*; |
| 67 | |
| 68 | |
| 69 | /** |
| 70 | * Implementation of an upgradable fabric app for the ECMP configuration. |
| 71 | */ |
| 72 | @Component(immediate = true) |
| 73 | public class EcmpFabricApp extends AbstractUpgradableFabricApp { |
| 74 | |
| 75 | private static final String APP_NAME = "org.onosproject.pi-ecmp-fabric"; |
Carmelo Cascone | 1fb27d3 | 2017-08-25 20:40:20 +0200 | [diff] [blame] | 76 | private static final String PIPECONF_ID = "pi-demo-ecmp"; |
Carmelo Cascone | 9f760a1 | 2017-08-24 13:14:53 +0200 | [diff] [blame] | 77 | private static final URL P4INFO_URL = EcmpFabricApp.class.getResource("/ecmp.p4info"); |
| 78 | private static final URL JSON_URL = EcmpFabricApp.class.getResource("/ecmp.json"); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 79 | |
| 80 | private static final PiPipeconf ECMP_PIPECONF = DefaultPiPipeconf.builder() |
| 81 | .withId(new PiPipeconfId(PIPECONF_ID)) |
| 82 | .withPipelineModel(Bmv2PipelineModelParser.parse(JSON_URL)) |
| 83 | .addBehaviour(PiPipelineInterpreter.class, EcmpInterpreter.class) |
Carmelo Cascone | 9f760a1 | 2017-08-24 13:14:53 +0200 | [diff] [blame] | 84 | .addBehaviour(Pipeliner.class, DefaultSingleTablePipeline.class) |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 85 | .addExtension(P4_INFO_TEXT, P4INFO_URL) |
| 86 | .addExtension(BMV2_JSON, JSON_URL) |
| 87 | .build(); |
| 88 | |
| 89 | private static final Map<DeviceId, Map<Set<PortNumber>, Short>> DEVICE_GROUP_ID_MAP = Maps.newHashMap(); |
| 90 | |
| 91 | public EcmpFabricApp() { |
Carmelo Cascone | 1fb27d3 | 2017-08-25 20:40:20 +0200 | [diff] [blame] | 92 | super(APP_NAME, ECMP_PIPECONF); |
wu | 5f6c5b8 | 2017-08-04 16:45:19 +0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public boolean initDevice(DeviceId deviceId) { |
| 97 | // Nothing to do. |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public List<FlowRule> generateLeafRules(DeviceId leaf, Host srcHost, Collection<Host> dstHosts, |
| 103 | Collection<DeviceId> availableSpines, Topology topo) |
| 104 | throws FlowRuleGeneratorException { |
| 105 | |
| 106 | // Get ports which connect this leaf switch to hosts. |
| 107 | Set<PortNumber> hostPorts = deviceService.getPorts(leaf) |
| 108 | .stream() |
| 109 | .filter(port -> !isFabricPort(port, topo)) |
| 110 | .map(Port::number) |
| 111 | .collect(Collectors.toSet()); |
| 112 | |
| 113 | // Get ports which connect this leaf to the given available spines. |
| 114 | TopologyGraph graph = topologyService.getGraph(topo); |
| 115 | Set<PortNumber> fabricPorts = graph.getEdgesFrom(new DefaultTopologyVertex(leaf)) |
| 116 | .stream() |
| 117 | .filter(e -> availableSpines.contains(e.dst().deviceId())) |
| 118 | .map(e -> e.link().src().port()) |
| 119 | .collect(Collectors.toSet()); |
| 120 | |
| 121 | if (hostPorts.size() != 1 || fabricPorts.size() == 0) { |
| 122 | log.error("Leaf switch has invalid port configuration: hostPorts={}, fabricPorts={}", |
| 123 | hostPorts.size(), fabricPorts.size()); |
| 124 | throw new FlowRuleGeneratorException(); |
| 125 | } |
| 126 | PortNumber hostPort = hostPorts.iterator().next(); |
| 127 | |
| 128 | List<FlowRule> rules = Lists.newArrayList(); |
| 129 | |
| 130 | TrafficTreatment treatment; |
| 131 | if (fabricPorts.size() > 1) { |
| 132 | // Do ECMP. |
| 133 | Pair<PiTableAction, List<FlowRule>> result = provisionEcmpPiTableAction(leaf, fabricPorts); |
| 134 | rules.addAll(result.getRight()); |
| 135 | treatment = DefaultTrafficTreatment.builder().piTableAction(result.getLeft()).build(); |
| 136 | } else { |
| 137 | // Output on port. |
| 138 | PortNumber outPort = fabricPorts.iterator().next(); |
| 139 | treatment = DefaultTrafficTreatment.builder().setOutput(outPort).build(); |
| 140 | } |
| 141 | |
| 142 | // From srHost to dstHosts. |
| 143 | for (Host dstHost : dstHosts) { |
| 144 | FlowRule rule = flowRuleBuilder(leaf, EcmpInterpreter.TABLE0) |
| 145 | .withSelector( |
| 146 | DefaultTrafficSelector.builder() |
| 147 | .matchInPort(hostPort) |
| 148 | .matchEthType(IPV4.ethType().toShort()) |
| 149 | .matchEthSrc(srcHost.mac()) |
| 150 | .matchEthDst(dstHost.mac()) |
| 151 | .build()) |
| 152 | .withTreatment(treatment) |
| 153 | .build(); |
| 154 | rules.add(rule); |
| 155 | } |
| 156 | |
| 157 | // From fabric ports to this leaf host. |
| 158 | for (PortNumber port : fabricPorts) { |
| 159 | FlowRule rule = flowRuleBuilder(leaf, EcmpInterpreter.TABLE0) |
| 160 | .withSelector( |
| 161 | DefaultTrafficSelector.builder() |
| 162 | .matchInPort(port) |
| 163 | .matchEthType(IPV4.ethType().toShort()) |
| 164 | .matchEthDst(srcHost.mac()) |
| 165 | .build()) |
| 166 | .withTreatment( |
| 167 | DefaultTrafficTreatment.builder() |
| 168 | .setOutput(hostPort) |
| 169 | .build()) |
| 170 | .build(); |
| 171 | rules.add(rule); |
| 172 | } |
| 173 | |
| 174 | return rules; |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public List<FlowRule> generateSpineRules(DeviceId deviceId, Collection<Host> dstHosts, Topology topo) |
| 179 | throws FlowRuleGeneratorException { |
| 180 | |
| 181 | List<FlowRule> rules = Lists.newArrayList(); |
| 182 | |
| 183 | // for each host |
| 184 | for (Host dstHost : dstHosts) { |
| 185 | |
| 186 | Set<Path> paths = topologyService.getPaths(topo, deviceId, dstHost.location().deviceId()); |
| 187 | |
| 188 | if (paths.size() == 0) { |
| 189 | log.warn("Can't find any path between spine {} and host {}", deviceId, dstHost); |
| 190 | throw new FlowRuleGeneratorException(); |
| 191 | } |
| 192 | |
| 193 | TrafficTreatment treatment; |
| 194 | |
| 195 | if (paths.size() == 1) { |
| 196 | // Only one path, do output on port. |
| 197 | PortNumber port = paths.iterator().next().src().port(); |
| 198 | treatment = DefaultTrafficTreatment.builder().setOutput(port).build(); |
| 199 | } else { |
| 200 | // Multiple paths, do ECMP. |
| 201 | Set<PortNumber> portNumbers = paths.stream().map(p -> p.src().port()).collect(toSet()); |
| 202 | Pair<PiTableAction, List<FlowRule>> result = provisionEcmpPiTableAction(deviceId, portNumbers); |
| 203 | rules.addAll(result.getRight()); |
| 204 | treatment = DefaultTrafficTreatment.builder().piTableAction(result.getLeft()).build(); |
| 205 | } |
| 206 | |
| 207 | FlowRule rule = flowRuleBuilder(deviceId, EcmpInterpreter.TABLE0) |
| 208 | .withSelector( |
| 209 | DefaultTrafficSelector.builder() |
| 210 | .matchEthType(IPV4.ethType().toShort()) |
| 211 | .matchEthDst(dstHost.mac()) |
| 212 | .build()) |
| 213 | .withTreatment(treatment) |
| 214 | .build(); |
| 215 | |
| 216 | rules.add(rule); |
| 217 | } |
| 218 | |
| 219 | return rules; |
| 220 | } |
| 221 | |
| 222 | private Pair<PiTableAction, List<FlowRule>> provisionEcmpPiTableAction(DeviceId deviceId, |
| 223 | Set<PortNumber> fabricPorts) |
| 224 | throws FlowRuleGeneratorException { |
| 225 | |
| 226 | // Install ECMP group table entries that map from hash values to actual fabric ports... |
| 227 | int groupId = groupIdOf(deviceId, fabricPorts); |
| 228 | int groupSize = fabricPorts.size(); |
| 229 | Iterator<PortNumber> portIterator = fabricPorts.iterator(); |
| 230 | List<FlowRule> rules = Lists.newArrayList(); |
| 231 | for (short i = 0; i < groupSize; i++) { |
| 232 | FlowRule rule = flowRuleBuilder(deviceId, EcmpInterpreter.ECMP_GROUP_TABLE) |
| 233 | .withSelector( |
| 234 | buildEcmpTrafficSelector(groupId, i)) |
| 235 | .withTreatment( |
| 236 | DefaultTrafficTreatment.builder() |
| 237 | .setOutput(portIterator.next()) |
| 238 | .build()) |
| 239 | .build(); |
| 240 | rules.add(rule); |
| 241 | } |
| 242 | |
| 243 | PiTableAction piTableAction = buildEcmpPiTableAction(groupId, groupSize); |
| 244 | |
| 245 | return Pair.of(piTableAction, rules); |
| 246 | } |
| 247 | |
| 248 | private PiTableAction buildEcmpPiTableAction(int groupId, int groupSize) { |
| 249 | |
| 250 | return PiAction.builder() |
| 251 | .withId(PiActionId.of(ECMP_GROUP_ACTION_NAME)) |
| 252 | .withParameter(new PiActionParam(PiActionParamId.of(GROUP_ID), |
| 253 | ImmutableByteSequence.copyFrom(groupId))) |
| 254 | .withParameter(new PiActionParam(PiActionParamId.of(GROUP_SIZE), |
| 255 | ImmutableByteSequence.copyFrom(groupSize))) |
| 256 | .build(); |
| 257 | } |
| 258 | |
| 259 | private TrafficSelector buildEcmpTrafficSelector(int groupId, int selector) { |
| 260 | Criterion ecmpCriterion = PiCriterion.builder() |
| 261 | .matchExact(PiHeaderFieldId.of(ECMP_METADATA_HEADER_NAME, GROUP_ID), groupId) |
| 262 | .matchExact(PiHeaderFieldId.of(ECMP_METADATA_HEADER_NAME, SELECTOR), selector) |
| 263 | .build(); |
| 264 | |
| 265 | return DefaultTrafficSelector.builder() |
| 266 | .matchPi((PiCriterion) ecmpCriterion) |
| 267 | .build(); |
| 268 | } |
| 269 | |
| 270 | public int groupIdOf(DeviceId deviceId, Set<PortNumber> ports) { |
| 271 | DEVICE_GROUP_ID_MAP.putIfAbsent(deviceId, Maps.newHashMap()); |
| 272 | // Counts the number of unique portNumber sets for each deviceId. |
| 273 | // Each distinct set of portNumbers will have a unique ID. |
| 274 | return DEVICE_GROUP_ID_MAP.get(deviceId).computeIfAbsent(ports, (pp) -> |
| 275 | (short) (DEVICE_GROUP_ID_MAP.get(deviceId).size() + 1)); |
| 276 | } |
| 277 | } |