blob: 4ed38b51d8ca4c42ab74d23ba9228e03d76669dc [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;
Yi Tseng0b809722017-11-03 10:23:26 -070024import org.onosproject.net.behaviour.Pipeliner;
Yi Tsengbe342052017-11-03 10:21:23 -070025import org.onosproject.net.device.PortStatisticsDiscovery;
26import org.onosproject.net.pi.model.DefaultPiPipeconf;
27import org.onosproject.net.pi.model.PiPipeconf;
28import org.onosproject.net.pi.model.PiPipeconfId;
Yi Tsengfa4a1c72017-11-03 10:22:38 -070029import org.onosproject.net.pi.model.PiPipelineInterpreter;
Yi Tsengbe342052017-11-03 10:21:23 -070030import org.onosproject.net.pi.model.PiPipelineModel;
31import org.onosproject.net.pi.service.PiPipeconfService;
32import org.onosproject.p4runtime.model.P4InfoParser;
33import org.onosproject.p4runtime.model.P4InfoParserException;
Yi Tseng0b809722017-11-03 10:23:26 -070034import org.onosproject.pipelines.fabric.pipeliner.FabricPipeliner;
Carmelo Cascone228092b2018-06-15 20:41:10 +020035import org.osgi.framework.FrameworkUtil;
36import org.osgi.framework.wiring.BundleWiring;
37import org.slf4j.Logger;
Yi Tsengbe342052017-11-03 10:21:23 -070038
Carmelo Cascone228092b2018-06-15 20:41:10 +020039import java.io.File;
40import java.io.FileNotFoundException;
Yi Tsengbe342052017-11-03 10:21:23 -070041import java.net.URL;
42import java.util.Collection;
Carmelo Cascone228092b2018-06-15 20:41:10 +020043import java.util.Objects;
44import java.util.stream.Collectors;
Yi Tsengbe342052017-11-03 10:21:23 -070045
Carmelo Cascone228092b2018-06-15 20:41:10 +020046import static java.lang.String.format;
47import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType;
48import static org.osgi.framework.wiring.BundleWiring.LISTRESOURCES_RECURSE;
49import static org.slf4j.LoggerFactory.getLogger;
Yi Tsengbe342052017-11-03 10:21:23 -070050
51/**
Carmelo Cascone228092b2018-06-15 20:41:10 +020052 * Pipeconf loader for fabric.p4 which uses p4c output available in the resource
53 * path to automatically build pipeconfs for different profiles, target and
54 * platforms.
Yi Tsengbe342052017-11-03 10:21:23 -070055 */
56@Component(immediate = true)
57public class PipeconfLoader {
58
Carmelo Cascone228092b2018-06-15 20:41:10 +020059 // TODO: allow adding properties to pipeconf instead of adding it to driver
Carmelo Casconeb531b682018-01-30 17:55:56 -080060
Carmelo Cascone228092b2018-06-15 20:41:10 +020061 private static Logger log = getLogger(PipeconfLoader.class);
Yi Tsengbe342052017-11-03 10:21:23 -070062
Carmelo Cascone228092b2018-06-15 20:41:10 +020063 private static final String BASE_PIPECONF_ID = "org.onosproject.pipelines";
Yi Tsengbe342052017-11-03 10:21:23 -070064
Carmelo Cascone228092b2018-06-15 20:41:10 +020065 private static final String P4C_OUT_PATH = "/p4c-out";
66
67 // profile/target/platform
68 private static final String P4C_RES_BASE_PATH = P4C_OUT_PATH + "/%s/%s/%s/";
69
70 private static final String SEP = File.separator;
71 private static final String TOFINO = "tofino";
72 private static final String BMV2 = "bmv2";
73 private static final String DEFAULT_PLATFORM = "default";
74 private static final String BMV2_JSON = "bmv2.json";
75 private static final String P4INFO_TXT = "p4info.txt";
76 private static final String TOFINO_BIN = "tofino.bin";
77 private static final String TOFINO_CTX_JSON = "context.json";
78
79 private static final Collection<PiPipeconf> PIPECONFS = buildAllPipeconf();
Yi Tsengbe342052017-11-03 10:21:23 -070080
81 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
82 private PiPipeconfService piPipeconfService;
83
84 @Activate
85 public void activate() {
86 // Registers all pipeconf at component activation.
Carmelo Cascone228092b2018-06-15 20:41:10 +020087 PIPECONFS.forEach(piPipeconfService::register);
Yi Tsengbe342052017-11-03 10:21:23 -070088 }
89
90 @Deactivate
91 public void deactivate() {
Carmelo Cascone228092b2018-06-15 20:41:10 +020092 PIPECONFS.stream().map(PiPipeconf::id).forEach(piPipeconfService::remove);
Yi Tsengbe342052017-11-03 10:21:23 -070093 }
94
Carmelo Cascone228092b2018-06-15 20:41:10 +020095 private static Collection<PiPipeconf> buildAllPipeconf() {
96 return FrameworkUtil
97 .getBundle(PipeconfLoader.class)
98 .adapt(BundleWiring.class)
99 // List all resource files in /p4c-out
100 .listResources(P4C_OUT_PATH, "*", LISTRESOURCES_RECURSE)
101 .stream()
102 // Filter only directories
103 .filter(name -> name.endsWith(SEP))
104 // Derive profile, target, and platform and build pipeconf.
105 .map(PipeconfLoader::buildPipeconfFromPath)
106 .filter(Objects::nonNull)
107 .collect(Collectors.toList());
108 }
109
110 private static PiPipeconf buildPipeconfFromPath(String path) {
111 String[] pieces = path.split(SEP);
112 // We expect a path of 4 elements, e.g.
113 // p4c-out/<profile>/<target>/<platform>
114 if (pieces.length != 4) {
115 return null;
116 }
117 String profile = pieces[1];
118 String target = pieces[2];
119 String platform = pieces[3];
120 try {
121 switch (target) {
122 case BMV2:
123 return buildBmv2Pipeconf(profile, platform);
124 case TOFINO:
125 return buildTofinoPipeconf(profile, platform);
126 default:
127 log.warn("Unknown target '{}', skipping pipeconf build...",
128 target);
129 return null;
130 }
131 } catch (FileNotFoundException e) {
132 log.warn("Unable to build pipeconf at {} because one or more p4c outputs are missing",
133 path);
134 return null;
135 }
136 }
137
138 private static PiPipeconf buildBmv2Pipeconf(String profile, String platform)
139 throws FileNotFoundException {
140 final URL bmv2JsonUrl = PipeconfLoader.class.getResource(format(
141 P4C_RES_BASE_PATH + BMV2_JSON, profile, BMV2, platform));
142 final URL p4InfoUrl = PipeconfLoader.class.getResource(format(
143 P4C_RES_BASE_PATH + P4INFO_TXT, profile, BMV2, platform));
144 if (bmv2JsonUrl == null || p4InfoUrl == null) {
145 throw new FileNotFoundException();
146 }
147 return basePipeconfBuilder(profile, platform, p4InfoUrl)
148 .addExtension(ExtensionType.BMV2_JSON, bmv2JsonUrl)
Yi Tsengbe342052017-11-03 10:21:23 -0700149 .build();
150 }
151
Carmelo Cascone228092b2018-06-15 20:41:10 +0200152 private static PiPipeconf buildTofinoPipeconf(String profile, String platform)
153 throws FileNotFoundException {
154 final URL tofinoBinUrl = PipeconfLoader.class.getResource(format(
155 P4C_RES_BASE_PATH + TOFINO_BIN, profile, TOFINO, platform));
156 final URL contextJsonUrl = PipeconfLoader.class.getResource(format(
157 P4C_RES_BASE_PATH + TOFINO_CTX_JSON, profile, TOFINO, platform));
158 final URL p4InfoUrl = PipeconfLoader.class.getResource(format(
159 P4C_RES_BASE_PATH + P4INFO_TXT, profile, TOFINO, platform));
160 if (tofinoBinUrl == null || contextJsonUrl == null || p4InfoUrl == null) {
161 throw new FileNotFoundException();
162 }
163 return basePipeconfBuilder(profile, platform, p4InfoUrl)
164 .addExtension(ExtensionType.TOFINO_BIN, tofinoBinUrl)
165 .addExtension(ExtensionType.TOFINO_CONTEXT_JSON, contextJsonUrl)
166 .build();
167 }
168
169 private static DefaultPiPipeconf.Builder basePipeconfBuilder(
170 String profile, String platform, URL p4InfoUrl) {
171 final String pipeconfId = platform.equals(DEFAULT_PLATFORM)
172 // Omit platform if default, e.g. with BMv2 pipeconf
173 ? format("%s.%s", BASE_PIPECONF_ID, profile)
174 : format("%s.%s.%s", BASE_PIPECONF_ID, profile, platform);
175 final PiPipelineModel model = parseP4Info(p4InfoUrl);
176 return DefaultPiPipeconf.builder()
177 .withId(new PiPipeconfId(pipeconfId))
178 .withPipelineModel(model)
179 .addBehaviour(PiPipelineInterpreter.class,
180 FabricInterpreter.class)
181 .addBehaviour(Pipeliner.class,
182 FabricPipeliner.class)
183 .addBehaviour(PortStatisticsDiscovery.class,
184 FabricPortStatisticsDiscovery.class)
185 .addExtension(ExtensionType.P4_INFO_TEXT, p4InfoUrl);
186 }
187
Yi Tsengbe342052017-11-03 10:21:23 -0700188 private static PiPipelineModel parseP4Info(URL p4InfoUrl) {
189 try {
190 return P4InfoParser.parse(p4InfoUrl);
191 } catch (P4InfoParserException e) {
Ray Milkey2b4958a2018-02-06 18:59:06 -0800192 throw new IllegalStateException(e);
Yi Tsengbe342052017-11-03 10:21:23 -0700193 }
194 }
195}