Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [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.pipelines.fabric; |
| 18 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 19 | import org.osgi.service.component.annotations.Activate; |
| 20 | import org.osgi.service.component.annotations.Component; |
| 21 | import org.osgi.service.component.annotations.Deactivate; |
| 22 | import org.osgi.service.component.annotations.Reference; |
| 23 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 24 | import org.onosproject.core.CoreService; |
| 25 | import org.onosproject.inbandtelemetry.api.IntProgrammable; |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 26 | import org.onosproject.net.PortNumber; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 27 | import org.onosproject.net.behaviour.Pipeliner; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 28 | import org.onosproject.net.device.PortStatisticsDiscovery; |
| 29 | import org.onosproject.net.pi.model.DefaultPiPipeconf; |
| 30 | import org.onosproject.net.pi.model.PiPipeconf; |
| 31 | import org.onosproject.net.pi.model.PiPipeconfId; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 32 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 33 | import org.onosproject.net.pi.model.PiPipelineModel; |
| 34 | import org.onosproject.net.pi.service.PiPipeconfService; |
| 35 | import org.onosproject.p4runtime.model.P4InfoParser; |
| 36 | import org.onosproject.p4runtime.model.P4InfoParserException; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 37 | import org.onosproject.pipelines.fabric.pipeliner.FabricPipeliner; |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 38 | import org.osgi.framework.FrameworkUtil; |
| 39 | import org.osgi.framework.wiring.BundleWiring; |
| 40 | import org.slf4j.Logger; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 41 | |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 42 | import java.io.File; |
| 43 | import java.io.FileNotFoundException; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 44 | import java.net.URL; |
| 45 | import java.util.Collection; |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 46 | import java.util.Objects; |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 47 | import java.util.Optional; |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 48 | import java.util.stream.Collectors; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 49 | |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 50 | import static java.lang.String.format; |
| 51 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType; |
| 52 | import static org.osgi.framework.wiring.BundleWiring.LISTRESOURCES_RECURSE; |
| 53 | import static org.slf4j.LoggerFactory.getLogger; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 54 | |
| 55 | /** |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 56 | * Pipeconf loader for fabric.p4 which uses p4c output available in the resource |
| 57 | * path to automatically build pipeconfs for different profiles, target and |
| 58 | * platforms. |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 59 | */ |
| 60 | @Component(immediate = true) |
| 61 | public class PipeconfLoader { |
| 62 | |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 63 | // TODO: allow adding properties to pipeconf instead of adding it to driver |
Carmelo Cascone | b531b68 | 2018-01-30 17:55:56 -0800 | [diff] [blame] | 64 | |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 65 | private static Logger log = getLogger(PipeconfLoader.class); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 66 | |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 67 | private static final String BASE_PIPECONF_ID = "org.onosproject.pipelines"; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 68 | |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 69 | private static final String P4C_OUT_PATH = "/p4c-out"; |
| 70 | |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 71 | private static final String PIPELINE_APP_NAME = "org.onosproject.pipelines.fabric"; |
| 72 | |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 73 | // profile/target/platform |
| 74 | private static final String P4C_RES_BASE_PATH = P4C_OUT_PATH + "/%s/%s/%s/"; |
| 75 | |
| 76 | private static final String SEP = File.separator; |
| 77 | private static final String TOFINO = "tofino"; |
| 78 | private static final String BMV2 = "bmv2"; |
| 79 | private static final String DEFAULT_PLATFORM = "default"; |
| 80 | private static final String BMV2_JSON = "bmv2.json"; |
| 81 | private static final String P4INFO_TXT = "p4info.txt"; |
| 82 | private static final String TOFINO_BIN = "tofino.bin"; |
| 83 | private static final String TOFINO_CTX_JSON = "context.json"; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 84 | private static final String INT_PROFILE_SUFFIX = "-int"; |
| 85 | private static final String FULL_PROFILE_SUFFIX = "-full"; |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 86 | |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 87 | private static final int BMV2_CPU_PORT = 255; |
| 88 | |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 89 | private static final Collection<PiPipeconf> PIPECONFS = buildAllPipeconf(); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 90 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 92 | private PiPipeconfService piPipeconfService; |
| 93 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 95 | private CoreService coreService; |
| 96 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 97 | @Activate |
| 98 | public void activate() { |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 99 | coreService.registerApplication(PIPELINE_APP_NAME); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 100 | // Registers all pipeconf at component activation. |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 101 | PIPECONFS.forEach(piPipeconfService::register); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Deactivate |
| 105 | public void deactivate() { |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 106 | PIPECONFS.stream().map(PiPipeconf::id).forEach(piPipeconfService::remove); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 109 | private static Collection<PiPipeconf> buildAllPipeconf() { |
| 110 | return FrameworkUtil |
| 111 | .getBundle(PipeconfLoader.class) |
| 112 | .adapt(BundleWiring.class) |
| 113 | // List all resource files in /p4c-out |
| 114 | .listResources(P4C_OUT_PATH, "*", LISTRESOURCES_RECURSE) |
| 115 | .stream() |
| 116 | // Filter only directories |
| 117 | .filter(name -> name.endsWith(SEP)) |
| 118 | // Derive profile, target, and platform and build pipeconf. |
| 119 | .map(PipeconfLoader::buildPipeconfFromPath) |
| 120 | .filter(Objects::nonNull) |
| 121 | .collect(Collectors.toList()); |
| 122 | } |
| 123 | |
| 124 | private static PiPipeconf buildPipeconfFromPath(String path) { |
| 125 | String[] pieces = path.split(SEP); |
| 126 | // We expect a path of 4 elements, e.g. |
| 127 | // p4c-out/<profile>/<target>/<platform> |
| 128 | if (pieces.length != 4) { |
| 129 | return null; |
| 130 | } |
| 131 | String profile = pieces[1]; |
| 132 | String target = pieces[2]; |
| 133 | String platform = pieces[3]; |
| 134 | try { |
| 135 | switch (target) { |
| 136 | case BMV2: |
| 137 | return buildBmv2Pipeconf(profile, platform); |
| 138 | case TOFINO: |
| 139 | return buildTofinoPipeconf(profile, platform); |
| 140 | default: |
| 141 | log.warn("Unknown target '{}', skipping pipeconf build...", |
| 142 | target); |
| 143 | return null; |
| 144 | } |
| 145 | } catch (FileNotFoundException e) { |
| 146 | log.warn("Unable to build pipeconf at {} because one or more p4c outputs are missing", |
| 147 | path); |
| 148 | return null; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | private static PiPipeconf buildBmv2Pipeconf(String profile, String platform) |
| 153 | throws FileNotFoundException { |
| 154 | final URL bmv2JsonUrl = PipeconfLoader.class.getResource(format( |
| 155 | P4C_RES_BASE_PATH + BMV2_JSON, profile, BMV2, platform)); |
| 156 | final URL p4InfoUrl = PipeconfLoader.class.getResource(format( |
| 157 | P4C_RES_BASE_PATH + P4INFO_TXT, profile, BMV2, platform)); |
| 158 | if (bmv2JsonUrl == null || p4InfoUrl == null) { |
| 159 | throw new FileNotFoundException(); |
| 160 | } |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 161 | |
| 162 | DefaultPiPipeconf.Builder builder = basePipeconfBuilder( |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 163 | profile, platform, p4InfoUrl, Bmv2FabricInterpreter.class) |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 164 | .addExtension(ExtensionType.BMV2_JSON, bmv2JsonUrl); |
| 165 | // Add IntProgrammable behaviour for INT-enabled profiles. |
| 166 | if (profile.endsWith(INT_PROFILE_SUFFIX) || profile.endsWith(FULL_PROFILE_SUFFIX)) { |
| 167 | builder.addBehaviour(IntProgrammable.class, IntProgrammableImpl.class); |
| 168 | } |
| 169 | return builder.build(); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 172 | private static PiPipeconf buildTofinoPipeconf(String profile, String platform) |
| 173 | throws FileNotFoundException { |
| 174 | final URL tofinoBinUrl = PipeconfLoader.class.getResource(format( |
| 175 | P4C_RES_BASE_PATH + TOFINO_BIN, profile, TOFINO, platform)); |
| 176 | final URL contextJsonUrl = PipeconfLoader.class.getResource(format( |
| 177 | P4C_RES_BASE_PATH + TOFINO_CTX_JSON, profile, TOFINO, platform)); |
| 178 | final URL p4InfoUrl = PipeconfLoader.class.getResource(format( |
| 179 | P4C_RES_BASE_PATH + P4INFO_TXT, profile, TOFINO, platform)); |
| 180 | if (tofinoBinUrl == null || contextJsonUrl == null || p4InfoUrl == null) { |
| 181 | throw new FileNotFoundException(); |
| 182 | } |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 183 | return basePipeconfBuilder( |
| 184 | profile, platform, p4InfoUrl, FabricInterpreter.class) |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 185 | .addExtension(ExtensionType.TOFINO_BIN, tofinoBinUrl) |
| 186 | .addExtension(ExtensionType.TOFINO_CONTEXT_JSON, contextJsonUrl) |
| 187 | .build(); |
| 188 | } |
| 189 | |
| 190 | private static DefaultPiPipeconf.Builder basePipeconfBuilder( |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 191 | String profile, String platform, URL p4InfoUrl, |
| 192 | Class<? extends FabricInterpreter> interpreterClass) { |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 193 | final String pipeconfId = platform.equals(DEFAULT_PLATFORM) |
| 194 | // Omit platform if default, e.g. with BMv2 pipeconf |
| 195 | ? format("%s.%s", BASE_PIPECONF_ID, profile) |
| 196 | : format("%s.%s.%s", BASE_PIPECONF_ID, profile, platform); |
| 197 | final PiPipelineModel model = parseP4Info(p4InfoUrl); |
| 198 | return DefaultPiPipeconf.builder() |
| 199 | .withId(new PiPipeconfId(pipeconfId)) |
| 200 | .withPipelineModel(model) |
| 201 | .addBehaviour(PiPipelineInterpreter.class, |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 202 | interpreterClass) |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 203 | .addBehaviour(Pipeliner.class, |
| 204 | FabricPipeliner.class) |
| 205 | .addBehaviour(PortStatisticsDiscovery.class, |
| 206 | FabricPortStatisticsDiscovery.class) |
| 207 | .addExtension(ExtensionType.P4_INFO_TEXT, p4InfoUrl); |
| 208 | } |
| 209 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 210 | private static PiPipelineModel parseP4Info(URL p4InfoUrl) { |
| 211 | try { |
| 212 | return P4InfoParser.parse(p4InfoUrl); |
| 213 | } catch (P4InfoParserException e) { |
Ray Milkey | 2b4958a | 2018-02-06 18:59:06 -0800 | [diff] [blame] | 214 | throw new IllegalStateException(e); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 217 | |
| 218 | // TODO: define interpreters with logical port mapping for Tofino platforms. |
| 219 | public static class Bmv2FabricInterpreter extends FabricInterpreter { |
| 220 | @Override |
| 221 | public Optional<Integer> mapLogicalPortNumber(PortNumber port) { |
| 222 | if (port.equals(PortNumber.CONTROLLER)) { |
| 223 | return Optional.of(BMV2_CPU_PORT); |
| 224 | } else { |
| 225 | return Optional.empty(); |
| 226 | } |
| 227 | } |
| 228 | } |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 229 | } |