Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [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.drivers.p4runtime; |
| 18 | |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 19 | import com.google.common.cache.CacheBuilder; |
| 20 | import com.google.common.cache.CacheLoader; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 21 | import com.google.common.cache.LoadingCache; |
Wailok Shum | f013a78 | 2021-07-26 16:51:01 +0800 | [diff] [blame^] | 22 | import com.google.common.collect.Sets; |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 23 | import org.onosproject.drivers.p4runtime.mirror.P4RuntimeMeterMirror; |
Wailok Shum | f013a78 | 2021-07-26 16:51:01 +0800 | [diff] [blame^] | 24 | import org.onosproject.net.DeviceId; |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 25 | import org.onosproject.net.meter.Band; |
| 26 | import org.onosproject.net.meter.DefaultBand; |
| 27 | import org.onosproject.net.meter.DefaultMeter; |
Wailok Shum | f013a78 | 2021-07-26 16:51:01 +0800 | [diff] [blame^] | 28 | import org.onosproject.net.meter.DefaultMeterFeatures; |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 29 | import org.onosproject.net.meter.Meter; |
Wailok Shum | f013a78 | 2021-07-26 16:51:01 +0800 | [diff] [blame^] | 30 | import org.onosproject.net.meter.MeterFeatures; |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 31 | import org.onosproject.net.meter.MeterOperation; |
| 32 | import org.onosproject.net.meter.MeterProgrammable; |
Wailok Shum | f013a78 | 2021-07-26 16:51:01 +0800 | [diff] [blame^] | 33 | import org.onosproject.net.meter.MeterScope; |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 34 | import org.onosproject.net.meter.MeterState; |
| 35 | import org.onosproject.net.pi.model.PiMeterId; |
| 36 | import org.onosproject.net.pi.model.PiMeterModel; |
| 37 | import org.onosproject.net.pi.model.PiPipelineModel; |
| 38 | import org.onosproject.net.pi.runtime.PiMeterCellConfig; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 39 | import org.onosproject.net.pi.runtime.PiMeterCellHandle; |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 40 | import org.onosproject.net.pi.service.PiMeterTranslator; |
| 41 | import org.onosproject.net.pi.service.PiTranslationException; |
| 42 | |
| 43 | import java.util.Collection; |
| 44 | import java.util.Collections; |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 45 | import java.util.HashSet; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 46 | import java.util.Set; |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 47 | import java.util.concurrent.CompletableFuture; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 48 | import java.util.concurrent.TimeUnit; |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 49 | import java.util.concurrent.locks.Lock; |
| 50 | import java.util.concurrent.locks.ReentrantLock; |
| 51 | import java.util.stream.Collectors; |
| 52 | |
Wailok Shum | f013a78 | 2021-07-26 16:51:01 +0800 | [diff] [blame^] | 53 | import static com.google.common.base.Preconditions.checkNotNull; |
| 54 | |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 55 | /** |
| 56 | * Implementation of MeterProgrammable behaviour for P4Runtime. |
| 57 | */ |
| 58 | public class P4RuntimeMeterProgrammable extends AbstractP4RuntimeHandlerBehaviour implements MeterProgrammable { |
| 59 | |
| 60 | private static final int METER_LOCK_EXPIRE_TIME_IN_MIN = 10; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 61 | private static final LoadingCache<PiMeterCellHandle, Lock> |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 62 | ENTRY_LOCKS = CacheBuilder.newBuilder() |
| 63 | .expireAfterAccess(METER_LOCK_EXPIRE_TIME_IN_MIN, TimeUnit.MINUTES) |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 64 | .build(new CacheLoader<PiMeterCellHandle, Lock>() { |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 65 | @Override |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 66 | public Lock load(PiMeterCellHandle handle) { |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 67 | return new ReentrantLock(); |
| 68 | } |
| 69 | }); |
| 70 | |
| 71 | private PiMeterTranslator translator; |
| 72 | private P4RuntimeMeterMirror meterMirror; |
| 73 | private PiPipelineModel pipelineModel; |
| 74 | |
| 75 | @Override |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 76 | protected boolean setupBehaviour(String opName) { |
| 77 | if (!super.setupBehaviour(opName)) { |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 78 | return false; |
| 79 | } |
| 80 | |
Yi Tseng | d771648 | 2018-10-31 15:34:30 -0700 | [diff] [blame] | 81 | translator = translationService.meterTranslator(); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 82 | meterMirror = handler().get(P4RuntimeMeterMirror.class); |
| 83 | pipelineModel = pipeconf.pipelineModel(); |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public CompletableFuture<Boolean> performMeterOperation(MeterOperation meterOp) { |
| 89 | |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 90 | if (!setupBehaviour("performMeterOperation()")) { |
Kevin Chuang | c267df2 | 2018-05-02 16:26:04 +0800 | [diff] [blame] | 91 | return CompletableFuture.completedFuture(false); |
| 92 | } |
| 93 | |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 94 | return CompletableFuture.completedFuture(processMeterOp(meterOp)); |
| 95 | } |
| 96 | |
| 97 | private boolean processMeterOp(MeterOperation meterOp) { |
| 98 | |
| 99 | if (meterOp.type() != MeterOperation.Type.MODIFY) { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 100 | log.warn("P4Runtime meter operations must be MODIFY!"); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 101 | return false; |
| 102 | } |
| 103 | |
| 104 | PiMeterCellConfig piMeterCellConfig; |
| 105 | try { |
| 106 | piMeterCellConfig = translator.translate(meterOp.meter(), pipeconf); |
| 107 | } catch (PiTranslationException e) { |
| 108 | log.warn("Unable translate meter, aborting meter operation {}: {}", meterOp.type(), e.getMessage()); |
| 109 | log.debug("exception", e); |
| 110 | return false; |
| 111 | } |
| 112 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 113 | final PiMeterCellHandle handle = PiMeterCellHandle.of(deviceId, piMeterCellConfig); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 114 | ENTRY_LOCKS.getUnchecked(handle).lock(); |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 115 | final boolean result = client.write(p4DeviceId, pipeconf) |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 116 | .modify(piMeterCellConfig).submitSync().isSuccess(); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 117 | if (result) { |
| 118 | meterMirror.put(handle, piMeterCellConfig); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 119 | } |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 120 | ENTRY_LOCKS.getUnchecked(handle).unlock(); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 121 | |
| 122 | return result; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public CompletableFuture<Collection<Meter>> getMeters() { |
| 127 | |
Carmelo Cascone | c32976e | 2019-04-08 14:50:52 -0700 | [diff] [blame] | 128 | if (!setupBehaviour("getMeters()")) { |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 129 | return CompletableFuture.completedFuture(Collections.emptyList()); |
| 130 | } |
| 131 | |
| 132 | Collection<PiMeterCellConfig> piMeterCellConfigs; |
| 133 | |
| 134 | Set<PiMeterId> meterIds = new HashSet<>(); |
| 135 | for (PiMeterModel mode : pipelineModel.meters()) { |
| 136 | meterIds.add(mode.id()); |
| 137 | } |
| 138 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 139 | piMeterCellConfigs = client.read(p4DeviceId, pipeconf) |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 140 | .meterCells(meterIds).submitSync().all(PiMeterCellConfig.class); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 141 | |
| 142 | Collection<Meter> meters = piMeterCellConfigs.stream() |
| 143 | .map(p -> { |
| 144 | DefaultMeter meter = (DefaultMeter) DefaultMeter.builder() |
| 145 | .withBands(p.meterBands().stream().map(b -> DefaultBand.builder() |
| 146 | .withRate(b.rate()) |
| 147 | .burstSize(b.burst()) |
| 148 | .ofType(Band.Type.NONE) |
| 149 | .build()).collect(Collectors.toList())) |
| 150 | .withCellId(p.cellId()).build(); |
| 151 | meter.setState(MeterState.ADDED); |
| 152 | return meter; |
| 153 | }) |
| 154 | .collect(Collectors.toList()); |
| 155 | |
| 156 | return CompletableFuture.completedFuture(meters); |
| 157 | } |
Wailok Shum | f013a78 | 2021-07-26 16:51:01 +0800 | [diff] [blame^] | 158 | |
| 159 | @Override |
| 160 | public CompletableFuture<Collection<MeterFeatures>> getMeterFeatures() { |
| 161 | |
| 162 | if (!setupBehaviour("getMeterFeatures()")) { |
| 163 | return CompletableFuture.completedFuture(Collections.emptyList()); |
| 164 | } |
| 165 | |
| 166 | Collection<MeterFeatures> meterFeatures = new HashSet<>(); |
| 167 | pipeconf.pipelineModel().meters().forEach( |
| 168 | m -> meterFeatures.add(new P4RuntimeMeterFeaturesBuilder(m, deviceId).build())); |
| 169 | |
| 170 | return CompletableFuture.completedFuture(meterFeatures); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * P4 meter features builder. |
| 175 | */ |
| 176 | public class P4RuntimeMeterFeaturesBuilder { |
| 177 | private final PiMeterModel piMeterModel; |
| 178 | private DeviceId deviceId; |
| 179 | |
| 180 | private static final long PI_METER_START_INDEX = 0L; |
| 181 | private static final short PI_METER_MAX_BAND = 2; |
| 182 | private static final short PI_METER_MAX_COLOR = 3; |
| 183 | |
| 184 | public P4RuntimeMeterFeaturesBuilder(PiMeterModel piMeterModel, DeviceId deviceId) { |
| 185 | this.piMeterModel = checkNotNull(piMeterModel); |
| 186 | this.deviceId = deviceId; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * To build a MeterFeatures using the PiMeterModel object |
| 191 | * retrieved from pipeconf. |
| 192 | * |
| 193 | * @return the meter features object |
| 194 | */ |
| 195 | public MeterFeatures build() { |
| 196 | /* |
| 197 | * We set the basic values before to extract the other information. |
| 198 | */ |
| 199 | MeterFeatures.Builder builder = DefaultMeterFeatures.builder() |
| 200 | .forDevice(deviceId) |
| 201 | // The scope value will be PiMeterId |
| 202 | .withScope(MeterScope.of(piMeterModel.id().id())) |
| 203 | .withMaxBands(PI_METER_MAX_BAND) |
| 204 | .withMaxColors(PI_METER_MAX_COLOR) |
| 205 | .withStartIndex(PI_METER_START_INDEX) |
| 206 | .withEndIndex(piMeterModel.size() - 1); |
| 207 | /* |
| 208 | * Pi meter only support NONE type |
| 209 | */ |
| 210 | Set<Band.Type> bands = Sets.newHashSet(); |
| 211 | bands.add(Band.Type.NONE); |
| 212 | builder.withBandTypes(bands); |
| 213 | /* |
| 214 | * We extract the supported units; |
| 215 | */ |
| 216 | Set<Meter.Unit> units = Sets.newHashSet(); |
| 217 | if (piMeterModel.unit() == PiMeterModel.Unit.BYTES) { |
| 218 | units.add(Meter.Unit.KB_PER_SEC); |
| 219 | } else if (piMeterModel.unit() == PiMeterModel.Unit.PACKETS) { |
| 220 | units.add(Meter.Unit.PKTS_PER_SEC); |
| 221 | } |
| 222 | builder.withUnits(units); |
| 223 | /* |
| 224 | * Burst is supported ? |
| 225 | */ |
| 226 | builder.hasBurst(true); |
| 227 | /* |
| 228 | * Stats are supported ? |
| 229 | */ |
| 230 | builder.hasStats(false); |
| 231 | |
| 232 | return builder.build(); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * To build an empty meter features. |
| 237 | * @param deviceId the device id |
| 238 | * @return the meter features |
| 239 | */ |
| 240 | public MeterFeatures noMeterFeatures(DeviceId deviceId) { |
| 241 | return DefaultMeterFeatures.noMeterFeatures(deviceId); |
| 242 | } |
| 243 | } |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 244 | } |