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; |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 22 | import org.onosproject.drivers.p4runtime.mirror.P4RuntimeMeterMirror; |
| 23 | import org.onosproject.net.meter.Band; |
| 24 | import org.onosproject.net.meter.DefaultBand; |
| 25 | import org.onosproject.net.meter.DefaultMeter; |
| 26 | import org.onosproject.net.meter.Meter; |
| 27 | import org.onosproject.net.meter.MeterOperation; |
| 28 | import org.onosproject.net.meter.MeterProgrammable; |
| 29 | import org.onosproject.net.meter.MeterState; |
| 30 | import org.onosproject.net.pi.model.PiMeterId; |
| 31 | import org.onosproject.net.pi.model.PiMeterModel; |
| 32 | import org.onosproject.net.pi.model.PiPipelineModel; |
| 33 | import org.onosproject.net.pi.runtime.PiMeterCellConfig; |
| 34 | import org.onosproject.net.pi.runtime.PiMeterHandle; |
| 35 | import org.onosproject.net.pi.service.PiMeterTranslator; |
| 36 | import org.onosproject.net.pi.service.PiTranslationException; |
| 37 | |
| 38 | import java.util.Collection; |
| 39 | import java.util.Collections; |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 40 | import java.util.HashSet; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 41 | import java.util.Set; |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 42 | import java.util.concurrent.CompletableFuture; |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 43 | import java.util.concurrent.ExecutionException; |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 44 | import java.util.concurrent.TimeUnit; |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 45 | import java.util.concurrent.locks.Lock; |
| 46 | import java.util.concurrent.locks.ReentrantLock; |
| 47 | import java.util.stream.Collectors; |
| 48 | |
| 49 | import static com.google.common.collect.Lists.newArrayList; |
| 50 | |
| 51 | /** |
| 52 | * Implementation of MeterProgrammable behaviour for P4Runtime. |
| 53 | */ |
| 54 | public class P4RuntimeMeterProgrammable extends AbstractP4RuntimeHandlerBehaviour implements MeterProgrammable { |
| 55 | |
| 56 | private static final int METER_LOCK_EXPIRE_TIME_IN_MIN = 10; |
| 57 | private static final LoadingCache<PiMeterHandle, Lock> |
| 58 | ENTRY_LOCKS = CacheBuilder.newBuilder() |
| 59 | .expireAfterAccess(METER_LOCK_EXPIRE_TIME_IN_MIN, TimeUnit.MINUTES) |
| 60 | .build(new CacheLoader<PiMeterHandle, Lock>() { |
| 61 | @Override |
| 62 | public Lock load(PiMeterHandle handle) { |
| 63 | return new ReentrantLock(); |
| 64 | } |
| 65 | }); |
| 66 | |
| 67 | private PiMeterTranslator translator; |
| 68 | private P4RuntimeMeterMirror meterMirror; |
| 69 | private PiPipelineModel pipelineModel; |
| 70 | |
| 71 | @Override |
| 72 | protected boolean setupBehaviour() { |
| 73 | if (!super.setupBehaviour()) { |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | translator = piTranslationService.meterTranslator(); |
| 78 | meterMirror = handler().get(P4RuntimeMeterMirror.class); |
| 79 | pipelineModel = pipeconf.pipelineModel(); |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public CompletableFuture<Boolean> performMeterOperation(MeterOperation meterOp) { |
| 85 | |
Kevin Chuang | c267df2 | 2018-05-02 16:26:04 +0800 | [diff] [blame] | 86 | if (!setupBehaviour()) { |
| 87 | return CompletableFuture.completedFuture(false); |
| 88 | } |
| 89 | |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 90 | return CompletableFuture.completedFuture(processMeterOp(meterOp)); |
| 91 | } |
| 92 | |
| 93 | private boolean processMeterOp(MeterOperation meterOp) { |
| 94 | |
| 95 | if (meterOp.type() != MeterOperation.Type.MODIFY) { |
| 96 | log.warn("P4runtime meter operations must be MODIFY!"); |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | PiMeterCellConfig piMeterCellConfig; |
| 101 | try { |
| 102 | piMeterCellConfig = translator.translate(meterOp.meter(), pipeconf); |
| 103 | } catch (PiTranslationException e) { |
| 104 | log.warn("Unable translate meter, aborting meter operation {}: {}", meterOp.type(), e.getMessage()); |
| 105 | log.debug("exception", e); |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | final PiMeterHandle handle = PiMeterHandle.of(deviceId, piMeterCellConfig); |
| 110 | ENTRY_LOCKS.getUnchecked(handle).lock(); |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 111 | final boolean result = getFutureWithDeadline( |
| 112 | client.writeMeterCells(newArrayList(piMeterCellConfig), pipeconf), |
| 113 | "writing meter cells", false); |
| 114 | if (result) { |
| 115 | meterMirror.put(handle, piMeterCellConfig); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 116 | } |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 117 | ENTRY_LOCKS.getUnchecked(handle).unlock(); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 118 | |
| 119 | return result; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public CompletableFuture<Collection<Meter>> getMeters() { |
| 124 | |
| 125 | if (!setupBehaviour()) { |
| 126 | return CompletableFuture.completedFuture(Collections.emptyList()); |
| 127 | } |
| 128 | |
| 129 | Collection<PiMeterCellConfig> piMeterCellConfigs; |
| 130 | |
| 131 | Set<PiMeterId> meterIds = new HashSet<>(); |
| 132 | for (PiMeterModel mode : pipelineModel.meters()) { |
| 133 | meterIds.add(mode.id()); |
| 134 | } |
| 135 | |
| 136 | try { |
| 137 | piMeterCellConfigs = client.readAllMeterCells(meterIds, pipeconf).get(); |
| 138 | } catch (InterruptedException | ExecutionException e) { |
| 139 | log.warn("Exception while reading meters from {}: {}", deviceId, e.toString()); |
| 140 | log.debug("", e); |
| 141 | return CompletableFuture.completedFuture(Collections.emptyList()); |
| 142 | } |
| 143 | |
| 144 | Collection<Meter> meters = piMeterCellConfigs.stream() |
| 145 | .map(p -> { |
| 146 | DefaultMeter meter = (DefaultMeter) DefaultMeter.builder() |
| 147 | .withBands(p.meterBands().stream().map(b -> DefaultBand.builder() |
| 148 | .withRate(b.rate()) |
| 149 | .burstSize(b.burst()) |
| 150 | .ofType(Band.Type.NONE) |
| 151 | .build()).collect(Collectors.toList())) |
| 152 | .withCellId(p.cellId()).build(); |
| 153 | meter.setState(MeterState.ADDED); |
| 154 | return meter; |
| 155 | }) |
| 156 | .collect(Collectors.toList()); |
| 157 | |
| 158 | return CompletableFuture.completedFuture(meters); |
| 159 | } |
Carmelo Cascone | e5b2872 | 2018-06-22 17:28:28 +0200 | [diff] [blame] | 160 | } |