blob: 4e84126e62939d5ff62b53fd28010c6153cc252a [file] [log] [blame]
Gamze Abaka52e35922021-03-12 10:48:34 +00001/*
Joey Armstrong9cdee9f2024-01-03 04:56:14 -05002 * Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors
Gamze Abaka52e35922021-03-12 10:48:34 +00003 *
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 */
16package meters
17
18import (
19 "context"
20 "fmt"
khenaidoo26721882021-08-11 17:42:52 -040021
22 "github.com/opencord/voltha-lib-go/v7/pkg/log"
23 ofp "github.com/opencord/voltha-protos/v5/go/openflow_13"
24 tp_pb "github.com/opencord/voltha-protos/v5/go/tech_profile"
Gamze Abaka52e35922021-03-12 10:48:34 +000025)
26
27// GetTrafficShapingInfo returns CIR,PIR and GIR values
28func GetTrafficShapingInfo(ctx context.Context, meterConfig *ofp.OfpMeterConfig) (*tp_pb.TrafficShapingInfo, error) {
Abhay Kumar40252eb2025-10-13 13:25:53 +000029 meterBandSize := len(meterConfig.Bands)
30 switch meterBandSize {
31 case 1:
Gamze Abaka52e35922021-03-12 10:48:34 +000032 band := meterConfig.Bands[0]
Girish Gowdrab73e6ce2022-03-11 16:53:35 -080033 if band.BurstSize == 0 { // GIR = PIR, Burst Size = 0, tcont type 1
34 return &tp_pb.TrafficShapingInfo{Pir: band.Rate, Gir: band.Rate}, nil
Gamze Abaka52e35922021-03-12 10:48:34 +000035 }
36 return &tp_pb.TrafficShapingInfo{Pir: band.Rate, Pbs: band.BurstSize}, nil // PIR, tcont type 4
Abhay Kumar40252eb2025-10-13 13:25:53 +000037 case 2:
Gamze Abaka52e35922021-03-12 10:48:34 +000038 firstBand, secondBand := meterConfig.Bands[0], meterConfig.Bands[1]
39 if firstBand.BurstSize == 0 && secondBand.BurstSize == 0 &&
Girish Gowdrab73e6ce2022-03-11 16:53:35 -080040 firstBand.Rate == secondBand.Rate { // PIR = GIR, tcont type 1
Gamze Abaka52e35922021-03-12 10:48:34 +000041 return &tp_pb.TrafficShapingInfo{Pir: firstBand.Rate, Gir: secondBand.Rate}, nil
42 }
43 if firstBand.BurstSize > 0 && secondBand.BurstSize > 0 { // PIR, CIR, tcont type 2 or 3
44 if firstBand.Rate > secondBand.Rate { // always PIR >= CIR
45 return &tp_pb.TrafficShapingInfo{Pir: firstBand.Rate, Pbs: firstBand.BurstSize, Cir: secondBand.Rate, Cbs: secondBand.BurstSize}, nil
46 }
47 return &tp_pb.TrafficShapingInfo{Pir: secondBand.Rate, Pbs: secondBand.BurstSize, Cir: firstBand.Rate, Cbs: firstBand.BurstSize}, nil
48 }
Abhay Kumar40252eb2025-10-13 13:25:53 +000049 case 3: // PIR,CIR,GIR, tcont type 5
Gamze Abaka52e35922021-03-12 10:48:34 +000050 var count, girIndex int
51 for i, band := range meterConfig.Bands {
52 if band.BurstSize == 0 { // find GIR
53 count = count + 1
54 girIndex = i
55 }
56 }
57 if count == 1 {
58 bands := make([]*ofp.OfpMeterBandHeader, len(meterConfig.Bands))
59 copy(bands, meterConfig.Bands)
60 pirCirBands := append(bands[:girIndex], bands[girIndex+1:]...)
61 firstBand, secondBand := pirCirBands[0], pirCirBands[1]
62 if firstBand.Rate > secondBand.Rate {
63 return &tp_pb.TrafficShapingInfo{Pir: firstBand.Rate, Pbs: firstBand.BurstSize, Cir: secondBand.Rate, Cbs: secondBand.BurstSize, Gir: meterConfig.Bands[girIndex].Rate}, nil
64 }
65 return &tp_pb.TrafficShapingInfo{Pir: secondBand.Rate, Pbs: secondBand.BurstSize, Cir: firstBand.Rate, Cbs: firstBand.BurstSize, Gir: meterConfig.Bands[girIndex].Rate}, nil
66 }
67 default:
68 logger.Errorw(ctx, "invalid-meter-config", log.Fields{"meter-config": meterConfig})
69 return nil, fmt.Errorf("invalid-meter-config: %v", meterConfig)
70 }
71 return nil, fmt.Errorf("invalid-meter-config: %v", meterConfig)
72}