blob: 72614c24e846b1c4bba445c56d801dc6bdad86a8 [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;
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020024import org.onosproject.net.PortNumber;
Yi Tseng0b809722017-11-03 10:23:26 -070025import org.onosproject.net.behaviour.Pipeliner;
Yi Tsengbe342052017-11-03 10:21:23 -070026import org.onosproject.net.device.PortStatisticsDiscovery;
27import org.onosproject.net.pi.model.DefaultPiPipeconf;
28import org.onosproject.net.pi.model.PiPipeconf;
29import org.onosproject.net.pi.model.PiPipeconfId;
Yi Tsengfa4a1c72017-11-03 10:22:38 -070030import org.onosproject.net.pi.model.PiPipelineInterpreter;
Yi Tsengbe342052017-11-03 10:21:23 -070031import org.onosproject.net.pi.model.PiPipelineModel;
32import org.onosproject.net.pi.service.PiPipeconfService;
33import org.onosproject.p4runtime.model.P4InfoParser;
34import org.onosproject.p4runtime.model.P4InfoParserException;
Yi Tseng0b809722017-11-03 10:23:26 -070035import org.onosproject.pipelines.fabric.pipeliner.FabricPipeliner;
Carmelo Cascone228092b2018-06-15 20:41:10 +020036import org.osgi.framework.FrameworkUtil;
37import org.osgi.framework.wiring.BundleWiring;
38import org.slf4j.Logger;
Yi Tsengbe342052017-11-03 10:21:23 -070039
Carmelo Cascone228092b2018-06-15 20:41:10 +020040import java.io.File;
41import java.io.FileNotFoundException;
Yi Tsengbe342052017-11-03 10:21:23 -070042import java.net.URL;
43import java.util.Collection;
Carmelo Cascone228092b2018-06-15 20:41:10 +020044import java.util.Objects;
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020045import java.util.Optional;
Carmelo Cascone228092b2018-06-15 20:41:10 +020046import 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
69 // profile/target/platform
70 private static final String P4C_RES_BASE_PATH = P4C_OUT_PATH + "/%s/%s/%s/";
71
72 private static final String SEP = File.separator;
73 private static final String TOFINO = "tofino";
74 private static final String BMV2 = "bmv2";
75 private static final String DEFAULT_PLATFORM = "default";
76 private static final String BMV2_JSON = "bmv2.json";
77 private static final String P4INFO_TXT = "p4info.txt";
78 private static final String TOFINO_BIN = "tofino.bin";
79 private static final String TOFINO_CTX_JSON = "context.json";
80
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020081 private static final int BMV2_CPU_PORT = 255;
82
Carmelo Cascone228092b2018-06-15 20:41:10 +020083 private static final Collection<PiPipeconf> PIPECONFS = buildAllPipeconf();
Yi Tsengbe342052017-11-03 10:21:23 -070084
85 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
86 private PiPipeconfService piPipeconfService;
87
88 @Activate
89 public void activate() {
90 // Registers all pipeconf at component activation.
Carmelo Cascone228092b2018-06-15 20:41:10 +020091 PIPECONFS.forEach(piPipeconfService::register);
Yi Tsengbe342052017-11-03 10:21:23 -070092 }
93
94 @Deactivate
95 public void deactivate() {
Carmelo Cascone228092b2018-06-15 20:41:10 +020096 PIPECONFS.stream().map(PiPipeconf::id).forEach(piPipeconfService::remove);
Yi Tsengbe342052017-11-03 10:21:23 -070097 }
98
Carmelo Cascone228092b2018-06-15 20:41:10 +020099 private static Collection<PiPipeconf> buildAllPipeconf() {
100 return FrameworkUtil
101 .getBundle(PipeconfLoader.class)
102 .adapt(BundleWiring.class)
103 // List all resource files in /p4c-out
104 .listResources(P4C_OUT_PATH, "*", LISTRESOURCES_RECURSE)
105 .stream()
106 // Filter only directories
107 .filter(name -> name.endsWith(SEP))
108 // Derive profile, target, and platform and build pipeconf.
109 .map(PipeconfLoader::buildPipeconfFromPath)
110 .filter(Objects::nonNull)
111 .collect(Collectors.toList());
112 }
113
114 private static PiPipeconf buildPipeconfFromPath(String path) {
115 String[] pieces = path.split(SEP);
116 // We expect a path of 4 elements, e.g.
117 // p4c-out/<profile>/<target>/<platform>
118 if (pieces.length != 4) {
119 return null;
120 }
121 String profile = pieces[1];
122 String target = pieces[2];
123 String platform = pieces[3];
124 try {
125 switch (target) {
126 case BMV2:
127 return buildBmv2Pipeconf(profile, platform);
128 case TOFINO:
129 return buildTofinoPipeconf(profile, platform);
130 default:
131 log.warn("Unknown target '{}', skipping pipeconf build...",
132 target);
133 return null;
134 }
135 } catch (FileNotFoundException e) {
136 log.warn("Unable to build pipeconf at {} because one or more p4c outputs are missing",
137 path);
138 return null;
139 }
140 }
141
142 private static PiPipeconf buildBmv2Pipeconf(String profile, String platform)
143 throws FileNotFoundException {
144 final URL bmv2JsonUrl = PipeconfLoader.class.getResource(format(
145 P4C_RES_BASE_PATH + BMV2_JSON, profile, BMV2, platform));
146 final URL p4InfoUrl = PipeconfLoader.class.getResource(format(
147 P4C_RES_BASE_PATH + P4INFO_TXT, profile, BMV2, platform));
148 if (bmv2JsonUrl == null || p4InfoUrl == null) {
149 throw new FileNotFoundException();
150 }
Carmelo Cascone1e8843f2018-07-19 19:01:12 +0200151 return basePipeconfBuilder(
152 profile, platform, p4InfoUrl, Bmv2FabricInterpreter.class)
Carmelo Cascone228092b2018-06-15 20:41:10 +0200153 .addExtension(ExtensionType.BMV2_JSON, bmv2JsonUrl)
Yi Tsengbe342052017-11-03 10:21:23 -0700154 .build();
155 }
156
Carmelo Cascone228092b2018-06-15 20:41:10 +0200157 private static PiPipeconf buildTofinoPipeconf(String profile, String platform)
158 throws FileNotFoundException {
159 final URL tofinoBinUrl = PipeconfLoader.class.getResource(format(
160 P4C_RES_BASE_PATH + TOFINO_BIN, profile, TOFINO, platform));
161 final URL contextJsonUrl = PipeconfLoader.class.getResource(format(
162 P4C_RES_BASE_PATH + TOFINO_CTX_JSON, profile, TOFINO, platform));
163 final URL p4InfoUrl = PipeconfLoader.class.getResource(format(
164 P4C_RES_BASE_PATH + P4INFO_TXT, profile, TOFINO, platform));
165 if (tofinoBinUrl == null || contextJsonUrl == null || p4InfoUrl == null) {
166 throw new FileNotFoundException();
167 }
Carmelo Cascone1e8843f2018-07-19 19:01:12 +0200168 return basePipeconfBuilder(
169 profile, platform, p4InfoUrl, FabricInterpreter.class)
Carmelo Cascone228092b2018-06-15 20:41:10 +0200170 .addExtension(ExtensionType.TOFINO_BIN, tofinoBinUrl)
171 .addExtension(ExtensionType.TOFINO_CONTEXT_JSON, contextJsonUrl)
172 .build();
173 }
174
175 private static DefaultPiPipeconf.Builder basePipeconfBuilder(
Carmelo Cascone1e8843f2018-07-19 19:01:12 +0200176 String profile, String platform, URL p4InfoUrl,
177 Class<? extends FabricInterpreter> interpreterClass) {
Carmelo Cascone228092b2018-06-15 20:41:10 +0200178 final String pipeconfId = platform.equals(DEFAULT_PLATFORM)
179 // Omit platform if default, e.g. with BMv2 pipeconf
180 ? format("%s.%s", BASE_PIPECONF_ID, profile)
181 : format("%s.%s.%s", BASE_PIPECONF_ID, profile, platform);
182 final PiPipelineModel model = parseP4Info(p4InfoUrl);
183 return DefaultPiPipeconf.builder()
184 .withId(new PiPipeconfId(pipeconfId))
185 .withPipelineModel(model)
186 .addBehaviour(PiPipelineInterpreter.class,
Carmelo Cascone1e8843f2018-07-19 19:01:12 +0200187 interpreterClass)
Carmelo Cascone228092b2018-06-15 20:41:10 +0200188 .addBehaviour(Pipeliner.class,
189 FabricPipeliner.class)
190 .addBehaviour(PortStatisticsDiscovery.class,
191 FabricPortStatisticsDiscovery.class)
192 .addExtension(ExtensionType.P4_INFO_TEXT, p4InfoUrl);
193 }
194
Yi Tsengbe342052017-11-03 10:21:23 -0700195 private static PiPipelineModel parseP4Info(URL p4InfoUrl) {
196 try {
197 return P4InfoParser.parse(p4InfoUrl);
198 } catch (P4InfoParserException e) {
Ray Milkey2b4958a2018-02-06 18:59:06 -0800199 throw new IllegalStateException(e);
Yi Tsengbe342052017-11-03 10:21:23 -0700200 }
201 }
Carmelo Cascone1e8843f2018-07-19 19:01:12 +0200202
203 // TODO: define interpreters with logical port mapping for Tofino platforms.
204 public static class Bmv2FabricInterpreter extends FabricInterpreter {
205 @Override
206 public Optional<Integer> mapLogicalPortNumber(PortNumber port) {
207 if (port.equals(PortNumber.CONTROLLER)) {
208 return Optional.of(BMV2_CPU_PORT);
209 } else {
210 return Optional.empty();
211 }
212 }
213 }
Yi Tsengbe342052017-11-03 10:21:23 -0700214}