blob: 1a1ddd6d72b0b4b0f227610b577312a138073179 [file] [log] [blame]
pierventre85208182021-09-02 19:16:32 +02001/*
2 * Copyright 2021-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.drivers.barefoot;
18
19import org.onosproject.drivers.p4runtime.P4RuntimeMeterProgrammable;
20import org.onosproject.net.meter.MeterProgrammable;
21import org.onosproject.net.pi.runtime.PiMeterBand;
22import org.onosproject.net.pi.runtime.PiMeterCellConfig;
23
24/**
25 * Implementation of the MeterProgrammable behaviour for a Tofino-based switch.
26 */
27public class TofinoMeterProgrammable extends P4RuntimeMeterProgrammable implements MeterProgrammable {
28 // More insights for these magic numbers can be found in this doc
29 // https://docs.google.com/document/d/1Vuf_2RaO0BJPTj_weE9h2spUBCMsiBX-iIMb4OoEIB0/edit?usp=sharing
30 private static final double RATE_ERROR = 0.02;
31 private static final long BURST_LOWER_DIVIDER = 126;
32 private static final long BURST_UPPER_DIVIDER = 125;
33 private static final long BURST_MULTIPLIER = 125;
34
35 @Override
36 public boolean isSimilar(PiMeterCellConfig onosMeter, PiMeterCellConfig deviceMeter) {
37 final PiMeterBand onosCommittedBand = onosMeter.committedBand();
38 final PiMeterBand onosPeakBand = onosMeter.peakBand();
39 final PiMeterBand deviceCommittedBand = deviceMeter.committedBand();
40 final PiMeterBand devicePeakBand = deviceMeter.peakBand();
41 final long onosCir = onosCommittedBand.rate();
42 final long onosCburst = onosCommittedBand.burst();
43 final long onosPir = onosPeakBand.rate();
44 final long onosPburst = onosPeakBand.burst();
45 final long deviceCir = deviceCommittedBand.rate();
46 final long deviceCburst = deviceCommittedBand.burst();
47 final long devicePir = devicePeakBand.rate();
48 final long devicePburst = devicePeakBand.burst();
49
50 return isRateSimilar(onosCir, deviceCir) && isRateSimilar(onosPir, devicePir) &&
51 isBurstSimilar(onosCburst, deviceCburst) && isBurstSimilar(onosPburst, devicePburst);
52 }
53
54 // Verify if device rate is included in the confidence interval
55 // derived from the rate stored in ONOS
56 private boolean isRateSimilar(long onosRate, long deviceRate) {
57 double lowerEnd = (double) onosRate * (1.0 - RATE_ERROR);
58 double upperEnd = (double) onosRate * (1.0 + RATE_ERROR);
59
60 if (log.isDebugEnabled()) {
61 log.debug("isRateSimilar {} in [{}, {}]", deviceRate, lowerEnd, upperEnd);
62 }
63
64 return deviceRate >= lowerEnd && deviceRate <= upperEnd;
65 }
66
67 // Verify if device burst is included in the confidence interval
68 // derived from the burst stored in ONOS
69 private boolean isBurstSimilar(long onosBurst, long deviceBurst) {
70 // Rundown removing the decimal part
71 long lowerEnd = (onosBurst / BURST_LOWER_DIVIDER) * BURST_MULTIPLIER;
72 long upperEnd = (onosBurst / BURST_UPPER_DIVIDER) * BURST_MULTIPLIER;
73
74 if (log.isDebugEnabled()) {
75 log.debug("isBurstSimilar {} in [{}, {}]", deviceBurst, lowerEnd, upperEnd);
76 }
77
78 return deviceBurst >= lowerEnd && deviceBurst <= upperEnd;
79 }
80
81}