Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.pipelines.fabric; |
| 18 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 24 | import org.onosproject.net.behaviour.Pipeliner; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 25 | import org.onosproject.net.device.PortStatisticsDiscovery; |
| 26 | import org.onosproject.net.pi.model.DefaultPiPipeconf; |
| 27 | import org.onosproject.net.pi.model.PiPipeconf; |
| 28 | import org.onosproject.net.pi.model.PiPipeconfId; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 29 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 30 | import org.onosproject.net.pi.model.PiPipelineModel; |
| 31 | import org.onosproject.net.pi.service.PiPipeconfService; |
| 32 | import org.onosproject.p4runtime.model.P4InfoParser; |
| 33 | import org.onosproject.p4runtime.model.P4InfoParserException; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 34 | import org.onosproject.pipelines.fabric.pipeliner.FabricPipeliner; |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 35 | import org.osgi.framework.FrameworkUtil; |
| 36 | import org.osgi.framework.wiring.BundleWiring; |
| 37 | import org.slf4j.Logger; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 38 | |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 39 | import java.io.File; |
| 40 | import java.io.FileNotFoundException; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 41 | import java.net.URL; |
| 42 | import java.util.Collection; |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 43 | import java.util.Objects; |
| 44 | import java.util.stream.Collectors; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 45 | |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 46 | import static java.lang.String.format; |
| 47 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType; |
| 48 | import static org.osgi.framework.wiring.BundleWiring.LISTRESOURCES_RECURSE; |
| 49 | import static org.slf4j.LoggerFactory.getLogger; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 52 | * 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 Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 55 | */ |
| 56 | @Component(immediate = true) |
| 57 | public class PipeconfLoader { |
| 58 | |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 59 | // TODO: allow adding properties to pipeconf instead of adding it to driver |
Carmelo Cascone | b531b68 | 2018-01-30 17:55:56 -0800 | [diff] [blame] | 60 | |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 61 | private static Logger log = getLogger(PipeconfLoader.class); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 62 | |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 63 | private static final String BASE_PIPECONF_ID = "org.onosproject.pipelines"; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 64 | |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 65 | 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 Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 80 | |
| 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 Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 87 | PIPECONFS.forEach(piPipeconfService::register); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Deactivate |
| 91 | public void deactivate() { |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 92 | PIPECONFS.stream().map(PiPipeconf::id).forEach(piPipeconfService::remove); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 95 | 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 Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 149 | .build(); |
| 150 | } |
| 151 | |
Carmelo Cascone | 228092b | 2018-06-15 20:41:10 +0200 | [diff] [blame] | 152 | 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 Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 188 | private static PiPipelineModel parseP4Info(URL p4InfoUrl) { |
| 189 | try { |
| 190 | return P4InfoParser.parse(p4InfoUrl); |
| 191 | } catch (P4InfoParserException e) { |
Ray Milkey | 2b4958a | 2018-02-06 18:59:06 -0800 | [diff] [blame] | 192 | throw new IllegalStateException(e); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | } |