blob: 9b54dfcbad18c776698027a4a897dd46fec5556a [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
Yi Tsengbe342052017-11-03 10:21:23 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +090024import org.onosproject.core.CoreService;
25import org.onosproject.inbandtelemetry.api.IntProgrammable;
Yi Tseng0b809722017-11-03 10:23:26 -070026import org.onosproject.net.behaviour.Pipeliner;
Yi Tsengbe342052017-11-03 10:21:23 -070027import org.onosproject.net.device.PortStatisticsDiscovery;
28import org.onosproject.net.pi.model.DefaultPiPipeconf;
29import org.onosproject.net.pi.model.PiPipeconf;
30import org.onosproject.net.pi.model.PiPipeconfId;
Yi Tsengfa4a1c72017-11-03 10:22:38 -070031import org.onosproject.net.pi.model.PiPipelineInterpreter;
Yi Tsengbe342052017-11-03 10:21:23 -070032import org.onosproject.net.pi.model.PiPipelineModel;
33import org.onosproject.net.pi.service.PiPipeconfService;
34import org.onosproject.p4runtime.model.P4InfoParser;
35import org.onosproject.p4runtime.model.P4InfoParserException;
Yi Tseng0b809722017-11-03 10:23:26 -070036import org.onosproject.pipelines.fabric.pipeliner.FabricPipeliner;
Carmelo Cascone228092b2018-06-15 20:41:10 +020037import org.osgi.framework.FrameworkUtil;
38import org.osgi.framework.wiring.BundleWiring;
39import 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 Cascone228092b2018-06-15 20:41:10 +020065 private static final String BASE_PIPECONF_ID = "org.onosproject.pipelines";
Yi Tsengbe342052017-11-03 10:21:23 -070066
Carmelo Cascone228092b2018-06-15 20:41:10 +020067 private static final String P4C_OUT_PATH = "/p4c-out";
68
Jonghwan Hyuned478dc2018-08-06 15:35:18 +090069 private static final String PIPELINE_APP_NAME = "org.onosproject.pipelines.fabric";
70
Carmelo Cascone228092b2018-06-15 20:41:10 +020071 // profile/target/platform
72 private static final String P4C_RES_BASE_PATH = P4C_OUT_PATH + "/%s/%s/%s/";
73
74 private static final String SEP = File.separator;
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 Cascone6880ba62018-09-06 00:04:34 -070080 private static final String CPU_PORT_TXT = "cpu_port.txt";
Carmelo Cascone228092b2018-06-15 20:41:10 +020081 private static final String TOFINO_BIN = "tofino.bin";
82 private static final String TOFINO_CTX_JSON = "context.json";
Jonghwan Hyuned478dc2018-08-06 15:35:18 +090083 private static final String INT_PROFILE_SUFFIX = "-int";
84 private static final String FULL_PROFILE_SUFFIX = "-full";
Carmelo Cascone228092b2018-06-15 20:41:10 +020085
86 private static final Collection<PiPipeconf> PIPECONFS = buildAllPipeconf();
Yi Tsengbe342052017-11-03 10:21:23 -070087
88 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
89 private PiPipeconfService piPipeconfService;
90
Jonghwan Hyuned478dc2018-08-06 15:35:18 +090091 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
92 private CoreService coreService;
93
Yi Tsengbe342052017-11-03 10:21:23 -070094 @Activate
95 public void activate() {
Jonghwan Hyuned478dc2018-08-06 15:35:18 +090096 coreService.registerApplication(PIPELINE_APP_NAME);
Yi Tsengbe342052017-11-03 10:21:23 -070097 // Registers all pipeconf at component activation.
Carmelo Cascone228092b2018-06-15 20:41:10 +020098 PIPECONFS.forEach(piPipeconfService::register);
Yi Tsengbe342052017-11-03 10:21:23 -070099 }
100
101 @Deactivate
102 public void deactivate() {
Carmelo Cascone228092b2018-06-15 20:41:10 +0200103 PIPECONFS.stream().map(PiPipeconf::id).forEach(piPipeconfService::remove);
Yi Tsengbe342052017-11-03 10:21:23 -0700104 }
105
Carmelo Cascone228092b2018-06-15 20:41:10 +0200106 private static Collection<PiPipeconf> buildAllPipeconf() {
107 return FrameworkUtil
108 .getBundle(PipeconfLoader.class)
109 .adapt(BundleWiring.class)
110 // List all resource files in /p4c-out
111 .listResources(P4C_OUT_PATH, "*", LISTRESOURCES_RECURSE)
112 .stream()
113 // Filter only directories
114 .filter(name -> name.endsWith(SEP))
115 // Derive profile, target, and platform and build pipeconf.
116 .map(PipeconfLoader::buildPipeconfFromPath)
117 .filter(Objects::nonNull)
118 .collect(Collectors.toList());
119 }
120
121 private static PiPipeconf buildPipeconfFromPath(String path) {
122 String[] pieces = path.split(SEP);
123 // We expect a path of 4 elements, e.g.
124 // p4c-out/<profile>/<target>/<platform>
125 if (pieces.length != 4) {
126 return null;
127 }
128 String profile = pieces[1];
129 String target = pieces[2];
130 String platform = pieces[3];
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700131 final DefaultPiPipeconf.Builder pipeconfBuilder;
Carmelo Cascone228092b2018-06-15 20:41:10 +0200132 try {
133 switch (target) {
134 case BMV2:
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700135 pipeconfBuilder = bmv2Pipeconf(profile, platform);
136 break;
Carmelo Cascone228092b2018-06-15 20:41:10 +0200137 case TOFINO:
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700138 pipeconfBuilder = tofinoPipeconf(profile, platform);
139 break;
Carmelo Cascone228092b2018-06-15 20:41:10 +0200140 default:
141 log.warn("Unknown target '{}', skipping pipeconf build...",
142 target);
143 return null;
144 }
145 } catch (FileNotFoundException e) {
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700146 log.warn("Unable to build pipeconf at {} because file is missing: {}",
147 path, e.getMessage());
Carmelo Cascone228092b2018-06-15 20:41:10 +0200148 return null;
149 }
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700150 // Add IntProgrammable behaviour for INT-enabled profiles.
151 if (profile.endsWith(INT_PROFILE_SUFFIX) || profile.endsWith(FULL_PROFILE_SUFFIX)) {
152 pipeconfBuilder.addBehaviour(IntProgrammable.class, IntProgrammableImpl.class);
153 }
154 return pipeconfBuilder.build();
Carmelo Cascone228092b2018-06-15 20:41:10 +0200155 }
156
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700157 private static DefaultPiPipeconf.Builder bmv2Pipeconf(
158 String profile, String platform)
Carmelo Cascone228092b2018-06-15 20:41:10 +0200159 throws FileNotFoundException {
160 final URL bmv2JsonUrl = PipeconfLoader.class.getResource(format(
161 P4C_RES_BASE_PATH + BMV2_JSON, profile, BMV2, platform));
162 final URL p4InfoUrl = PipeconfLoader.class.getResource(format(
163 P4C_RES_BASE_PATH + P4INFO_TXT, profile, BMV2, platform));
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700164 final URL cpuPortUrl = PipeconfLoader.class.getResource(format(
165 P4C_RES_BASE_PATH + CPU_PORT_TXT, profile, BMV2, platform));
166 if (bmv2JsonUrl == null) {
167 throw new FileNotFoundException(BMV2_JSON);
Carmelo Cascone228092b2018-06-15 20:41:10 +0200168 }
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700169 if (p4InfoUrl == null) {
170 throw new FileNotFoundException(P4INFO_TXT);
171 }
172 if (cpuPortUrl == null) {
173 throw new FileNotFoundException(CPU_PORT_TXT);
174 }
175 return basePipeconfBuilder(profile, platform, p4InfoUrl, cpuPortUrl)
Carmelo Cascone0c8d73e2018-09-07 16:31:06 -0700176 .addBehaviour(PortStatisticsDiscovery.class,
177 FabricPortStatisticsDiscovery.class)
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900178 .addExtension(ExtensionType.BMV2_JSON, bmv2JsonUrl);
Yi Tsengbe342052017-11-03 10:21:23 -0700179 }
180
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700181 private static DefaultPiPipeconf.Builder tofinoPipeconf(
182 String profile, String platform)
Carmelo Cascone228092b2018-06-15 20:41:10 +0200183 throws FileNotFoundException {
184 final URL tofinoBinUrl = PipeconfLoader.class.getResource(format(
185 P4C_RES_BASE_PATH + TOFINO_BIN, profile, TOFINO, platform));
186 final URL contextJsonUrl = PipeconfLoader.class.getResource(format(
187 P4C_RES_BASE_PATH + TOFINO_CTX_JSON, profile, TOFINO, platform));
188 final URL p4InfoUrl = PipeconfLoader.class.getResource(format(
189 P4C_RES_BASE_PATH + P4INFO_TXT, profile, TOFINO, platform));
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700190 final URL cpuPortUrl = PipeconfLoader.class.getResource(format(
191 P4C_RES_BASE_PATH + CPU_PORT_TXT, profile, TOFINO, platform));
192 if (tofinoBinUrl == null) {
193 throw new FileNotFoundException(TOFINO_BIN);
Carmelo Cascone228092b2018-06-15 20:41:10 +0200194 }
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700195 if (contextJsonUrl == null) {
196 throw new FileNotFoundException(TOFINO_CTX_JSON);
197 }
198 if (p4InfoUrl == null) {
199 throw new FileNotFoundException(P4INFO_TXT);
200 }
201 if (cpuPortUrl == null) {
202 throw new FileNotFoundException(CPU_PORT_TXT);
203 }
204 return basePipeconfBuilder(profile, platform, p4InfoUrl, cpuPortUrl)
Carmelo Cascone228092b2018-06-15 20:41:10 +0200205 .addExtension(ExtensionType.TOFINO_BIN, tofinoBinUrl)
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700206 .addExtension(ExtensionType.TOFINO_CONTEXT_JSON, contextJsonUrl);
Carmelo Cascone228092b2018-06-15 20:41:10 +0200207 }
208
209 private static DefaultPiPipeconf.Builder basePipeconfBuilder(
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700210 String profile, String platform, URL p4InfoUrl, URL cpuPortUrl) {
Carmelo Cascone228092b2018-06-15 20:41:10 +0200211 final String pipeconfId = platform.equals(DEFAULT_PLATFORM)
212 // Omit platform if default, e.g. with BMv2 pipeconf
213 ? format("%s.%s", BASE_PIPECONF_ID, profile)
214 : format("%s.%s.%s", BASE_PIPECONF_ID, profile, platform);
215 final PiPipelineModel model = parseP4Info(p4InfoUrl);
216 return DefaultPiPipeconf.builder()
217 .withId(new PiPipeconfId(pipeconfId))
218 .withPipelineModel(model)
219 .addBehaviour(PiPipelineInterpreter.class,
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700220 FabricInterpreter.class)
Carmelo Cascone228092b2018-06-15 20:41:10 +0200221 .addBehaviour(Pipeliner.class,
222 FabricPipeliner.class)
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700223 .addExtension(ExtensionType.P4_INFO_TEXT, p4InfoUrl)
224 .addExtension(ExtensionType.CPU_PORT_TXT, cpuPortUrl);
Carmelo Cascone228092b2018-06-15 20:41:10 +0200225 }
226
Yi Tsengbe342052017-11-03 10:21:23 -0700227 private static PiPipelineModel parseP4Info(URL p4InfoUrl) {
228 try {
229 return P4InfoParser.parse(p4InfoUrl);
230 } catch (P4InfoParserException e) {
Ray Milkey2b4958a2018-02-06 18:59:06 -0800231 throw new IllegalStateException(e);
Yi Tsengbe342052017-11-03 10:21:23 -0700232 }
233 }
Yi Tsengbe342052017-11-03 10:21:23 -0700234}