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