blob: 5fca57be1ca48d4ad52c625af5784347aad8fe2b [file] [log] [blame]
Yi Tsengbe342052017-11-03 10:21:23 -07001/*
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
17package org.onosproject.pipelines.fabric;
18
Jonghwan Hyuned478dc2018-08-06 15:35:18 +090019import org.onosproject.core.CoreService;
20import org.onosproject.inbandtelemetry.api.IntProgrammable;
Yi Tseng0b809722017-11-03 10:23:26 -070021import org.onosproject.net.behaviour.Pipeliner;
Yi Tsengbe342052017-11-03 10:21:23 -070022import org.onosproject.net.device.PortStatisticsDiscovery;
23import org.onosproject.net.pi.model.DefaultPiPipeconf;
24import org.onosproject.net.pi.model.PiPipeconf;
25import org.onosproject.net.pi.model.PiPipeconfId;
Yi Tsengfa4a1c72017-11-03 10:22:38 -070026import org.onosproject.net.pi.model.PiPipelineInterpreter;
Yi Tsengbe342052017-11-03 10:21:23 -070027import org.onosproject.net.pi.model.PiPipelineModel;
28import org.onosproject.net.pi.service.PiPipeconfService;
29import org.onosproject.p4runtime.model.P4InfoParser;
30import org.onosproject.p4runtime.model.P4InfoParserException;
Yi Tseng0b809722017-11-03 10:23:26 -070031import org.onosproject.pipelines.fabric.pipeliner.FabricPipeliner;
Carmelo Cascone228092b2018-06-15 20:41:10 +020032import org.osgi.framework.FrameworkUtil;
33import org.osgi.framework.wiring.BundleWiring;
Carmelo Cascone70e816b2019-03-19 16:15:47 -070034import org.osgi.service.component.annotations.Activate;
35import org.osgi.service.component.annotations.Component;
36import org.osgi.service.component.annotations.Deactivate;
37import org.osgi.service.component.annotations.Reference;
38import org.osgi.service.component.annotations.ReferenceCardinality;
Carmelo Cascone228092b2018-06-15 20:41:10 +020039import org.slf4j.Logger;
Yi Tsengbe342052017-11-03 10:21:23 -070040
Carmelo Cascone228092b2018-06-15 20:41:10 +020041import java.io.File;
42import java.io.FileNotFoundException;
Yi Tsengbe342052017-11-03 10:21:23 -070043import java.net.URL;
44import java.util.Collection;
Carmelo Cascone228092b2018-06-15 20:41:10 +020045import java.util.Objects;
46import java.util.stream.Collectors;
Yi Tsengbe342052017-11-03 10:21:23 -070047
Carmelo Cascone228092b2018-06-15 20:41:10 +020048import static java.lang.String.format;
49import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType;
50import static org.osgi.framework.wiring.BundleWiring.LISTRESOURCES_RECURSE;
51import static org.slf4j.LoggerFactory.getLogger;
Yi Tsengbe342052017-11-03 10:21:23 -070052
53/**
Carmelo Cascone228092b2018-06-15 20:41:10 +020054 * 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 Tsengbe342052017-11-03 10:21:23 -070057 */
58@Component(immediate = true)
59public class PipeconfLoader {
60
Carmelo Cascone228092b2018-06-15 20:41:10 +020061 // TODO: allow adding properties to pipeconf instead of adding it to driver
Carmelo Casconeb531b682018-01-30 17:55:56 -080062
Carmelo Cascone228092b2018-06-15 20:41:10 +020063 private static Logger log = getLogger(PipeconfLoader.class);
Yi Tsengbe342052017-11-03 10:21:23 -070064
Carmelo Casconefa421582018-09-13 10:05:57 -070065 static final String PIPELINE_APP_NAME = "org.onosproject.pipelines.fabric";
66
Carmelo Cascone228092b2018-06-15 20:41:10 +020067 private static final String BASE_PIPECONF_ID = "org.onosproject.pipelines";
Carmelo Cascone228092b2018-06-15 20:41:10 +020068 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 Lo5e2b7052018-10-18 14:24:24 +030074 private static final String SPECTRUM = "spectrum";
Carmelo Cascone228092b2018-06-15 20:41:10 +020075 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 Cascone6880ba62018-09-06 00:04:34 -070080 private static final String CPU_PORT_TXT = "cpu_port.txt";
Alan Lo5e2b7052018-10-18 14:24:24 +030081 private static final String SPECTRUM_BIN = "spectrum.bin";
Carmelo Cascone70e816b2019-03-19 16:15:47 -070082 private static final String TOFINO_BIN = "pipe/tofino.bin";
83 private static final String TOFINO_CTX_JSON = "pipe/context.json";
Jonghwan Hyuned478dc2018-08-06 15:35:18 +090084 private static final String INT_PROFILE_SUFFIX = "-int";
85 private static final String FULL_PROFILE_SUFFIX = "-full";
Carmelo Cascone228092b2018-06-15 20:41:10 +020086
87 private static final Collection<PiPipeconf> PIPECONFS = buildAllPipeconf();
Yi Tsengbe342052017-11-03 10:21:23 -070088
Ray Milkeyd84f89b2018-08-17 14:54:17 -070089 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Yi Tsengbe342052017-11-03 10:21:23 -070090 private PiPipeconfService piPipeconfService;
91
Ray Milkeyd84f89b2018-08-17 14:54:17 -070092 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonghwan Hyuned478dc2018-08-06 15:35:18 +090093 private CoreService coreService;
94
Yi Tsengbe342052017-11-03 10:21:23 -070095 @Activate
96 public void activate() {
Jonghwan Hyuned478dc2018-08-06 15:35:18 +090097 coreService.registerApplication(PIPELINE_APP_NAME);
Yi Tsengbe342052017-11-03 10:21:23 -070098 // Registers all pipeconf at component activation.
Carmelo Cascone228092b2018-06-15 20:41:10 +020099 PIPECONFS.forEach(piPipeconfService::register);
Yi Tsengbe342052017-11-03 10:21:23 -0700100 }
101
102 @Deactivate
103 public void deactivate() {
Carmelo Cascone75a9a892019-04-22 12:12:23 -0700104 PIPECONFS.stream().map(PiPipeconf::id).forEach(piPipeconfService::unregister);
Yi Tsengbe342052017-11-03 10:21:23 -0700105 }
106
Carmelo Cascone228092b2018-06-15 20:41:10 +0200107 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 Cascone6880ba62018-09-06 00:04:34 -0700132 final DefaultPiPipeconf.Builder pipeconfBuilder;
Carmelo Cascone228092b2018-06-15 20:41:10 +0200133 try {
134 switch (target) {
135 case BMV2:
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700136 pipeconfBuilder = bmv2Pipeconf(profile, platform);
137 break;
Alan Lo5e2b7052018-10-18 14:24:24 +0300138 case SPECTRUM:
139 pipeconfBuilder = spectrumPipeconf(profile, platform);
140 break;
Carmelo Cascone228092b2018-06-15 20:41:10 +0200141 case TOFINO:
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700142 pipeconfBuilder = tofinoPipeconf(profile, platform);
143 break;
Carmelo Cascone228092b2018-06-15 20:41:10 +0200144 default:
145 log.warn("Unknown target '{}', skipping pipeconf build...",
146 target);
147 return null;
148 }
149 } catch (FileNotFoundException e) {
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700150 log.warn("Unable to build pipeconf at {} because file is missing: {}",
151 path, e.getMessage());
Carmelo Cascone228092b2018-06-15 20:41:10 +0200152 return null;
153 }
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700154 // Add IntProgrammable behaviour for INT-enabled profiles.
155 if (profile.endsWith(INT_PROFILE_SUFFIX) || profile.endsWith(FULL_PROFILE_SUFFIX)) {
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800156 pipeconfBuilder.addBehaviour(IntProgrammable.class, FabricIntProgrammable.class);
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700157 }
158 return pipeconfBuilder.build();
Carmelo Cascone228092b2018-06-15 20:41:10 +0200159 }
160
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700161 private static DefaultPiPipeconf.Builder bmv2Pipeconf(
162 String profile, String platform)
Carmelo Cascone228092b2018-06-15 20:41:10 +0200163 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 Cascone6880ba62018-09-06 00:04:34 -0700168 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 Cascone228092b2018-06-15 20:41:10 +0200172 }
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700173 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 Cascone0c8d73e2018-09-07 16:31:06 -0700180 .addBehaviour(PortStatisticsDiscovery.class,
181 FabricPortStatisticsDiscovery.class)
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900182 .addExtension(ExtensionType.BMV2_JSON, bmv2JsonUrl);
Yi Tsengbe342052017-11-03 10:21:23 -0700183 }
184
Alan Lo5e2b7052018-10-18 14:24:24 +0300185 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 Cascone6880ba62018-09-06 00:04:34 -0700207 private static DefaultPiPipeconf.Builder tofinoPipeconf(
208 String profile, String platform)
Carmelo Cascone228092b2018-06-15 20:41:10 +0200209 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 Cascone6880ba62018-09-06 00:04:34 -0700216 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 Cascone228092b2018-06-15 20:41:10 +0200220 }
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700221 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 Cascone228092b2018-06-15 20:41:10 +0200231 .addExtension(ExtensionType.TOFINO_BIN, tofinoBinUrl)
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700232 .addExtension(ExtensionType.TOFINO_CONTEXT_JSON, contextJsonUrl);
Carmelo Cascone228092b2018-06-15 20:41:10 +0200233 }
234
235 private static DefaultPiPipeconf.Builder basePipeconfBuilder(
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700236 String profile, String platform, URL p4InfoUrl, URL cpuPortUrl) {
Carmelo Cascone228092b2018-06-15 20:41:10 +0200237 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 Cascone6880ba62018-09-06 00:04:34 -0700246 FabricInterpreter.class)
Carmelo Cascone228092b2018-06-15 20:41:10 +0200247 .addBehaviour(Pipeliner.class,
248 FabricPipeliner.class)
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700249 .addExtension(ExtensionType.P4_INFO_TEXT, p4InfoUrl)
250 .addExtension(ExtensionType.CPU_PORT_TXT, cpuPortUrl);
Carmelo Cascone228092b2018-06-15 20:41:10 +0200251 }
252
Yi Tsengbe342052017-11-03 10:21:23 -0700253 private static PiPipelineModel parseP4Info(URL p4InfoUrl) {
254 try {
255 return P4InfoParser.parse(p4InfoUrl);
256 } catch (P4InfoParserException e) {
Ray Milkey2b4958a2018-02-06 18:59:06 -0800257 throw new IllegalStateException(e);
Yi Tsengbe342052017-11-03 10:21:23 -0700258 }
259 }
Yi Tsengbe342052017-11-03 10:21:23 -0700260}