blob: ed3db8b2c5d68d5895b5dbc1d2ab2d5a0b7ed7be [file] [log] [blame]
khenaidoo106c61a2021-08-11 18:05:46 -04001/*
2 * Copyright 2019-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 */
16package meters
17
18import (
19 "context"
20 "fmt"
21
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"
25)
26
27// GetTrafficShapingInfo returns CIR,PIR and GIR values
28func GetTrafficShapingInfo(ctx context.Context, meterConfig *ofp.OfpMeterConfig) (*tp_pb.TrafficShapingInfo, error) {
29 switch meterBandSize := len(meterConfig.Bands); {
30 case meterBandSize == 1:
31 band := meterConfig.Bands[0]
Girish Gowdra3e909f72022-03-11 16:57:22 -080032 if band.BurstSize == 0 { // GIR = PIR, Burst Size = 0, tcont type 1
33 return &tp_pb.TrafficShapingInfo{Pir: band.Rate, Gir: band.Rate}, nil
khenaidoo106c61a2021-08-11 18:05:46 -040034 }
35 return &tp_pb.TrafficShapingInfo{Pir: band.Rate, Pbs: band.BurstSize}, nil // PIR, tcont type 4
36 case meterBandSize == 2:
37 firstBand, secondBand := meterConfig.Bands[0], meterConfig.Bands[1]
38 if firstBand.BurstSize == 0 && secondBand.BurstSize == 0 &&
Girish Gowdra3e909f72022-03-11 16:57:22 -080039 firstBand.Rate == secondBand.Rate { // PIR = GIR, tcont type 1
khenaidoo106c61a2021-08-11 18:05:46 -040040 return &tp_pb.TrafficShapingInfo{Pir: firstBand.Rate, Gir: secondBand.Rate}, nil
41 }
42 if firstBand.BurstSize > 0 && secondBand.BurstSize > 0 { // PIR, CIR, tcont type 2 or 3
43 if firstBand.Rate > secondBand.Rate { // always PIR >= CIR
44 return &tp_pb.TrafficShapingInfo{Pir: firstBand.Rate, Pbs: firstBand.BurstSize, Cir: secondBand.Rate, Cbs: secondBand.BurstSize}, nil
45 }
46 return &tp_pb.TrafficShapingInfo{Pir: secondBand.Rate, Pbs: secondBand.BurstSize, Cir: firstBand.Rate, Cbs: firstBand.BurstSize}, nil
47 }
48 case meterBandSize == 3: // PIR,CIR,GIR, tcont type 5
49 var count, girIndex int
50 for i, band := range meterConfig.Bands {
51 if band.BurstSize == 0 { // find GIR
52 count = count + 1
53 girIndex = i
54 }
55 }
56 if count == 1 {
57 bands := make([]*ofp.OfpMeterBandHeader, len(meterConfig.Bands))
58 copy(bands, meterConfig.Bands)
59 pirCirBands := append(bands[:girIndex], bands[girIndex+1:]...)
60 firstBand, secondBand := pirCirBands[0], pirCirBands[1]
61 if firstBand.Rate > secondBand.Rate {
62 return &tp_pb.TrafficShapingInfo{Pir: firstBand.Rate, Pbs: firstBand.BurstSize, Cir: secondBand.Rate, Cbs: secondBand.BurstSize, Gir: meterConfig.Bands[girIndex].Rate}, nil
63 }
64 return &tp_pb.TrafficShapingInfo{Pir: secondBand.Rate, Pbs: secondBand.BurstSize, Cir: firstBand.Rate, Cbs: firstBand.BurstSize, Gir: meterConfig.Bands[girIndex].Rate}, nil
65 }
66 default:
67 logger.Errorw(ctx, "invalid-meter-config", log.Fields{"meter-config": meterConfig})
68 return nil, fmt.Errorf("invalid-meter-config: %v", meterConfig)
69 }
70 return nil, fmt.Errorf("invalid-meter-config: %v", meterConfig)
71}