blob: 9dd385efaceaf3cb16443045cfd37366d2d36cd3 [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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.osgi.service.component.annotations.Activate;
20import org.osgi.service.component.annotations.Component;
21import org.osgi.service.component.annotations.Deactivate;
22import org.osgi.service.component.annotations.Reference;
23import org.osgi.service.component.annotations.ReferenceCardinality;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +090024import org.onosproject.core.CoreService;
25import org.onosproject.inbandtelemetry.api.IntProgrammable;
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020026import org.onosproject.net.PortNumber;
Yi Tseng0b809722017-11-03 10:23:26 -070027import org.onosproject.net.behaviour.Pipeliner;
Yi Tsengbe342052017-11-03 10:21:23 -070028import org.onosproject.net.device.PortStatisticsDiscovery;
29import org.onosproject.net.pi.model.DefaultPiPipeconf;
30import org.onosproject.net.pi.model.PiPipeconf;
31import org.onosproject.net.pi.model.PiPipeconfId;
Yi Tsengfa4a1c72017-11-03 10:22:38 -070032import org.onosproject.net.pi.model.PiPipelineInterpreter;
Yi Tsengbe342052017-11-03 10:21:23 -070033import org.onosproject.net.pi.model.PiPipelineModel;
34import org.onosproject.net.pi.service.PiPipeconfService;
35import org.onosproject.p4runtime.model.P4InfoParser;
36import org.onosproject.p4runtime.model.P4InfoParserException;
Yi Tseng0b809722017-11-03 10:23:26 -070037import org.onosproject.pipelines.fabric.pipeliner.FabricPipeliner;
Carmelo Cascone228092b2018-06-15 20:41:10 +020038import org.osgi.framework.FrameworkUtil;
39import org.osgi.framework.wiring.BundleWiring;
40import org.slf4j.Logger;
Yi Tsengbe342052017-11-03 10:21:23 -070041
Carmelo Cascone228092b2018-06-15 20:41:10 +020042import java.io.File;
43import java.io.FileNotFoundException;
Yi Tsengbe342052017-11-03 10:21:23 -070044import java.net.URL;
45import java.util.Collection;
Carmelo Cascone228092b2018-06-15 20:41:10 +020046import java.util.Objects;
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020047import java.util.Optional;
Carmelo Cascone228092b2018-06-15 20:41:10 +020048import java.util.stream.Collectors;
Yi Tsengbe342052017-11-03 10:21:23 -070049
Carmelo Cascone228092b2018-06-15 20:41:10 +020050import static java.lang.String.format;
51import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType;
52import static org.osgi.framework.wiring.BundleWiring.LISTRESOURCES_RECURSE;
53import static org.slf4j.LoggerFactory.getLogger;
Yi Tsengbe342052017-11-03 10:21:23 -070054
55/**
Carmelo Cascone228092b2018-06-15 20:41:10 +020056 * 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 Tsengbe342052017-11-03 10:21:23 -070059 */
60@Component(immediate = true)
61public class PipeconfLoader {
62
Carmelo Cascone228092b2018-06-15 20:41:10 +020063 // TODO: allow adding properties to pipeconf instead of adding it to driver
Carmelo Casconeb531b682018-01-30 17:55:56 -080064
Carmelo Cascone228092b2018-06-15 20:41:10 +020065 private static Logger log = getLogger(PipeconfLoader.class);
Yi Tsengbe342052017-11-03 10:21:23 -070066
Carmelo Cascone228092b2018-06-15 20:41:10 +020067 private static final String BASE_PIPECONF_ID = "org.onosproject.pipelines";
Yi Tsengbe342052017-11-03 10:21:23 -070068
Carmelo Cascone228092b2018-06-15 20:41:10 +020069 private static final String P4C_OUT_PATH = "/p4c-out";
70
Jonghwan Hyuned478dc2018-08-06 15:35:18 +090071 private static final String PIPELINE_APP_NAME = "org.onosproject.pipelines.fabric";
72
Carmelo Cascone228092b2018-06-15 20:41:10 +020073 // 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 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
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020087 private static final int BMV2_CPU_PORT = 255;
88
Carmelo Cascone228092b2018-06-15 20:41:10 +020089 private static final Collection<PiPipeconf> PIPECONFS = buildAllPipeconf();
Yi Tsengbe342052017-11-03 10:21:23 -070090
Ray Milkeyd84f89b2018-08-17 14:54:17 -070091 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Yi Tsengbe342052017-11-03 10:21:23 -070092 private PiPipeconfService piPipeconfService;
93
Ray Milkeyd84f89b2018-08-17 14:54:17 -070094 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonghwan Hyuned478dc2018-08-06 15:35:18 +090095 private CoreService coreService;
96
Yi Tsengbe342052017-11-03 10:21:23 -070097 @Activate
98 public void activate() {
Jonghwan Hyuned478dc2018-08-06 15:35:18 +090099 coreService.registerApplication(PIPELINE_APP_NAME);
Yi Tsengbe342052017-11-03 10:21:23 -0700100 // Registers all pipeconf at component activation.
Carmelo Cascone228092b2018-06-15 20:41:10 +0200101 PIPECONFS.forEach(piPipeconfService::register);
Yi Tsengbe342052017-11-03 10:21:23 -0700102 }
103
104 @Deactivate
105 public void deactivate() {
Carmelo Cascone228092b2018-06-15 20:41:10 +0200106 PIPECONFS.stream().map(PiPipeconf::id).forEach(piPipeconfService::remove);
Yi Tsengbe342052017-11-03 10:21:23 -0700107 }
108
Carmelo Cascone228092b2018-06-15 20:41:10 +0200109 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 Hyuned478dc2018-08-06 15:35:18 +0900161
162 DefaultPiPipeconf.Builder builder = basePipeconfBuilder(
Carmelo Cascone1e8843f2018-07-19 19:01:12 +0200163 profile, platform, p4InfoUrl, Bmv2FabricInterpreter.class)
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900164 .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 Tsengbe342052017-11-03 10:21:23 -0700170 }
171
Carmelo Cascone228092b2018-06-15 20:41:10 +0200172 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 Cascone1e8843f2018-07-19 19:01:12 +0200183 return basePipeconfBuilder(
184 profile, platform, p4InfoUrl, FabricInterpreter.class)
Carmelo Cascone228092b2018-06-15 20:41:10 +0200185 .addExtension(ExtensionType.TOFINO_BIN, tofinoBinUrl)
186 .addExtension(ExtensionType.TOFINO_CONTEXT_JSON, contextJsonUrl)
187 .build();
188 }
189
190 private static DefaultPiPipeconf.Builder basePipeconfBuilder(
Carmelo Cascone1e8843f2018-07-19 19:01:12 +0200191 String profile, String platform, URL p4InfoUrl,
192 Class<? extends FabricInterpreter> interpreterClass) {
Carmelo Cascone228092b2018-06-15 20:41:10 +0200193 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 Cascone1e8843f2018-07-19 19:01:12 +0200202 interpreterClass)
Carmelo Cascone228092b2018-06-15 20:41:10 +0200203 .addBehaviour(Pipeliner.class,
204 FabricPipeliner.class)
205 .addBehaviour(PortStatisticsDiscovery.class,
206 FabricPortStatisticsDiscovery.class)
207 .addExtension(ExtensionType.P4_INFO_TEXT, p4InfoUrl);
208 }
209
Yi Tsengbe342052017-11-03 10:21:23 -0700210 private static PiPipelineModel parseP4Info(URL p4InfoUrl) {
211 try {
212 return P4InfoParser.parse(p4InfoUrl);
213 } catch (P4InfoParserException e) {
Ray Milkey2b4958a2018-02-06 18:59:06 -0800214 throw new IllegalStateException(e);
Yi Tsengbe342052017-11-03 10:21:23 -0700215 }
216 }
Carmelo Cascone1e8843f2018-07-19 19:01:12 +0200217
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 Tsengbe342052017-11-03 10:21:23 -0700229}