blob: 1609794fef00e0fa78ca2e1e564aca3413bf3ded [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 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;
74 private static final String TOFINO = "tofino";
75 private static final String BMV2 = "bmv2";
76 private static final String DEFAULT_PLATFORM = "default";
77 private static final String BMV2_JSON = "bmv2.json";
78 private static final String P4INFO_TXT = "p4info.txt";
Carmelo Cascone6880ba62018-09-06 00:04:34 -070079 private static final String CPU_PORT_TXT = "cpu_port.txt";
Carmelo Cascone228092b2018-06-15 20:41:10 +020080 private static final String TOFINO_BIN = "tofino.bin";
81 private static final String TOFINO_CTX_JSON = "context.json";
Jonghwan Hyuned478dc2018-08-06 15:35:18 +090082 private static final String INT_PROFILE_SUFFIX = "-int";
83 private static final String FULL_PROFILE_SUFFIX = "-full";
Carmelo Cascone228092b2018-06-15 20:41:10 +020084
85 private static final Collection<PiPipeconf> PIPECONFS = buildAllPipeconf();
Yi Tsengbe342052017-11-03 10:21:23 -070086
87 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
88 private PiPipeconfService piPipeconfService;
89
Jonghwan Hyuned478dc2018-08-06 15:35:18 +090090 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
91 private CoreService coreService;
92
Yi Tsengbe342052017-11-03 10:21:23 -070093 @Activate
94 public void activate() {
Jonghwan Hyuned478dc2018-08-06 15:35:18 +090095 coreService.registerApplication(PIPELINE_APP_NAME);
Yi Tsengbe342052017-11-03 10:21:23 -070096 // Registers all pipeconf at component activation.
Carmelo Cascone228092b2018-06-15 20:41:10 +020097 PIPECONFS.forEach(piPipeconfService::register);
Yi Tsengbe342052017-11-03 10:21:23 -070098 }
99
100 @Deactivate
101 public void deactivate() {
Carmelo Cascone228092b2018-06-15 20:41:10 +0200102 PIPECONFS.stream().map(PiPipeconf::id).forEach(piPipeconfService::remove);
Yi Tsengbe342052017-11-03 10:21:23 -0700103 }
104
Carmelo Cascone228092b2018-06-15 20:41:10 +0200105 private static Collection<PiPipeconf> buildAllPipeconf() {
106 return FrameworkUtil
107 .getBundle(PipeconfLoader.class)
108 .adapt(BundleWiring.class)
109 // List all resource files in /p4c-out
110 .listResources(P4C_OUT_PATH, "*", LISTRESOURCES_RECURSE)
111 .stream()
112 // Filter only directories
113 .filter(name -> name.endsWith(SEP))
114 // Derive profile, target, and platform and build pipeconf.
115 .map(PipeconfLoader::buildPipeconfFromPath)
116 .filter(Objects::nonNull)
117 .collect(Collectors.toList());
118 }
119
120 private static PiPipeconf buildPipeconfFromPath(String path) {
121 String[] pieces = path.split(SEP);
122 // We expect a path of 4 elements, e.g.
123 // p4c-out/<profile>/<target>/<platform>
124 if (pieces.length != 4) {
125 return null;
126 }
127 String profile = pieces[1];
128 String target = pieces[2];
129 String platform = pieces[3];
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700130 final DefaultPiPipeconf.Builder pipeconfBuilder;
Carmelo Cascone228092b2018-06-15 20:41:10 +0200131 try {
132 switch (target) {
133 case BMV2:
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700134 pipeconfBuilder = bmv2Pipeconf(profile, platform);
135 break;
Carmelo Cascone228092b2018-06-15 20:41:10 +0200136 case TOFINO:
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700137 pipeconfBuilder = tofinoPipeconf(profile, platform);
138 break;
Carmelo Cascone228092b2018-06-15 20:41:10 +0200139 default:
140 log.warn("Unknown target '{}', skipping pipeconf build...",
141 target);
142 return null;
143 }
144 } catch (FileNotFoundException e) {
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700145 log.warn("Unable to build pipeconf at {} because file is missing: {}",
146 path, e.getMessage());
Carmelo Cascone228092b2018-06-15 20:41:10 +0200147 return null;
148 }
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700149 // Add IntProgrammable behaviour for INT-enabled profiles.
150 if (profile.endsWith(INT_PROFILE_SUFFIX) || profile.endsWith(FULL_PROFILE_SUFFIX)) {
151 pipeconfBuilder.addBehaviour(IntProgrammable.class, IntProgrammableImpl.class);
152 }
153 return pipeconfBuilder.build();
Carmelo Cascone228092b2018-06-15 20:41:10 +0200154 }
155
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700156 private static DefaultPiPipeconf.Builder bmv2Pipeconf(
157 String profile, String platform)
Carmelo Cascone228092b2018-06-15 20:41:10 +0200158 throws FileNotFoundException {
159 final URL bmv2JsonUrl = PipeconfLoader.class.getResource(format(
160 P4C_RES_BASE_PATH + BMV2_JSON, profile, BMV2, platform));
161 final URL p4InfoUrl = PipeconfLoader.class.getResource(format(
162 P4C_RES_BASE_PATH + P4INFO_TXT, profile, BMV2, platform));
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700163 final URL cpuPortUrl = PipeconfLoader.class.getResource(format(
164 P4C_RES_BASE_PATH + CPU_PORT_TXT, profile, BMV2, platform));
165 if (bmv2JsonUrl == null) {
166 throw new FileNotFoundException(BMV2_JSON);
Carmelo Cascone228092b2018-06-15 20:41:10 +0200167 }
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700168 if (p4InfoUrl == null) {
169 throw new FileNotFoundException(P4INFO_TXT);
170 }
171 if (cpuPortUrl == null) {
172 throw new FileNotFoundException(CPU_PORT_TXT);
173 }
174 return basePipeconfBuilder(profile, platform, p4InfoUrl, cpuPortUrl)
Carmelo Cascone0c8d73e2018-09-07 16:31:06 -0700175 .addBehaviour(PortStatisticsDiscovery.class,
176 FabricPortStatisticsDiscovery.class)
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900177 .addExtension(ExtensionType.BMV2_JSON, bmv2JsonUrl);
Yi Tsengbe342052017-11-03 10:21:23 -0700178 }
179
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700180 private static DefaultPiPipeconf.Builder tofinoPipeconf(
181 String profile, String platform)
Carmelo Cascone228092b2018-06-15 20:41:10 +0200182 throws FileNotFoundException {
183 final URL tofinoBinUrl = PipeconfLoader.class.getResource(format(
184 P4C_RES_BASE_PATH + TOFINO_BIN, profile, TOFINO, platform));
185 final URL contextJsonUrl = PipeconfLoader.class.getResource(format(
186 P4C_RES_BASE_PATH + TOFINO_CTX_JSON, profile, TOFINO, platform));
187 final URL p4InfoUrl = PipeconfLoader.class.getResource(format(
188 P4C_RES_BASE_PATH + P4INFO_TXT, profile, TOFINO, platform));
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700189 final URL cpuPortUrl = PipeconfLoader.class.getResource(format(
190 P4C_RES_BASE_PATH + CPU_PORT_TXT, profile, TOFINO, platform));
191 if (tofinoBinUrl == null) {
192 throw new FileNotFoundException(TOFINO_BIN);
Carmelo Cascone228092b2018-06-15 20:41:10 +0200193 }
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700194 if (contextJsonUrl == null) {
195 throw new FileNotFoundException(TOFINO_CTX_JSON);
196 }
197 if (p4InfoUrl == null) {
198 throw new FileNotFoundException(P4INFO_TXT);
199 }
200 if (cpuPortUrl == null) {
201 throw new FileNotFoundException(CPU_PORT_TXT);
202 }
203 return basePipeconfBuilder(profile, platform, p4InfoUrl, cpuPortUrl)
Carmelo Cascone228092b2018-06-15 20:41:10 +0200204 .addExtension(ExtensionType.TOFINO_BIN, tofinoBinUrl)
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700205 .addExtension(ExtensionType.TOFINO_CONTEXT_JSON, contextJsonUrl);
Carmelo Cascone228092b2018-06-15 20:41:10 +0200206 }
207
208 private static DefaultPiPipeconf.Builder basePipeconfBuilder(
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700209 String profile, String platform, URL p4InfoUrl, URL cpuPortUrl) {
Carmelo Cascone228092b2018-06-15 20:41:10 +0200210 final String pipeconfId = platform.equals(DEFAULT_PLATFORM)
211 // Omit platform if default, e.g. with BMv2 pipeconf
212 ? format("%s.%s", BASE_PIPECONF_ID, profile)
213 : format("%s.%s.%s", BASE_PIPECONF_ID, profile, platform);
214 final PiPipelineModel model = parseP4Info(p4InfoUrl);
215 return DefaultPiPipeconf.builder()
216 .withId(new PiPipeconfId(pipeconfId))
217 .withPipelineModel(model)
218 .addBehaviour(PiPipelineInterpreter.class,
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700219 FabricInterpreter.class)
Carmelo Cascone228092b2018-06-15 20:41:10 +0200220 .addBehaviour(Pipeliner.class,
221 FabricPipeliner.class)
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700222 .addExtension(ExtensionType.P4_INFO_TEXT, p4InfoUrl)
223 .addExtension(ExtensionType.CPU_PORT_TXT, cpuPortUrl);
Carmelo Cascone228092b2018-06-15 20:41:10 +0200224 }
225
Yi Tsengbe342052017-11-03 10:21:23 -0700226 private static PiPipelineModel parseP4Info(URL p4InfoUrl) {
227 try {
228 return P4InfoParser.parse(p4InfoUrl);
229 } catch (P4InfoParserException e) {
Ray Milkey2b4958a2018-02-06 18:59:06 -0800230 throw new IllegalStateException(e);
Yi Tsengbe342052017-11-03 10:21:23 -0700231 }
232 }
Yi Tsengbe342052017-11-03 10:21:23 -0700233}