blob: c3ccba1072a0098578a77e6d44c3054209705519 [file] [log] [blame]
Gamze Abaka01174422021-03-10 06:55:27 +00001/*
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"
Girish Gowdra4c3d4602021-07-22 16:33:37 -070021 "github.com/opencord/voltha-lib-go/v6/pkg/log"
Gamze Abaka01174422021-03-10 06:55:27 +000022 ofp "github.com/opencord/voltha-protos/v4/go/openflow_13"
23 tp_pb "github.com/opencord/voltha-protos/v4/go/tech_profile"
24)
25
26// GetTrafficShapingInfo returns CIR,PIR and GIR values
27func GetTrafficShapingInfo(ctx context.Context, meterConfig *ofp.OfpMeterConfig) (*tp_pb.TrafficShapingInfo, error) {
28 switch meterBandSize := len(meterConfig.Bands); {
29 case meterBandSize == 1:
30 band := meterConfig.Bands[0]
31 if band.BurstSize == 0 { // GIR, tcont type 1
32 return &tp_pb.TrafficShapingInfo{Gir: band.Rate}, nil
33 }
34 return &tp_pb.TrafficShapingInfo{Pir: band.Rate, Pbs: band.BurstSize}, nil // PIR, tcont type 4
35 case meterBandSize == 2:
36 firstBand, secondBand := meterConfig.Bands[0], meterConfig.Bands[1]
37 if firstBand.BurstSize == 0 && secondBand.BurstSize == 0 &&
38 firstBand.Rate == secondBand.Rate { // PIR == GIR, tcont type 1
39 return &tp_pb.TrafficShapingInfo{Pir: firstBand.Rate, Gir: secondBand.Rate}, nil
40 }
41 if firstBand.BurstSize > 0 && secondBand.BurstSize > 0 { // PIR, CIR, tcont type 2 or 3
42 if firstBand.Rate > secondBand.Rate { // always PIR >= CIR
43 return &tp_pb.TrafficShapingInfo{Pir: firstBand.Rate, Pbs: firstBand.BurstSize, Cir: secondBand.Rate, Cbs: secondBand.BurstSize}, nil
44 }
45 return &tp_pb.TrafficShapingInfo{Pir: secondBand.Rate, Pbs: secondBand.BurstSize, Cir: firstBand.Rate, Cbs: firstBand.BurstSize}, nil
46 }
47 case meterBandSize == 3: // PIR,CIR,GIR, tcont type 5
48 var count, girIndex int
49 for i, band := range meterConfig.Bands {
50 if band.BurstSize == 0 { // find GIR
51 count = count + 1
52 girIndex = i
53 }
54 }
55 if count == 1 {
56 bands := make([]*ofp.OfpMeterBandHeader, len(meterConfig.Bands))
57 copy(bands, meterConfig.Bands)
58 pirCirBands := append(bands[:girIndex], bands[girIndex+1:]...)
59 firstBand, secondBand := pirCirBands[0], pirCirBands[1]
60 if firstBand.Rate > secondBand.Rate {
61 return &tp_pb.TrafficShapingInfo{Pir: firstBand.Rate, Pbs: firstBand.BurstSize, Cir: secondBand.Rate, Cbs: secondBand.BurstSize, Gir: meterConfig.Bands[girIndex].Rate}, nil
62 }
63 return &tp_pb.TrafficShapingInfo{Pir: secondBand.Rate, Pbs: secondBand.BurstSize, Cir: firstBand.Rate, Cbs: firstBand.BurstSize, Gir: meterConfig.Bands[girIndex].Rate}, nil
64 }
65 default:
66 logger.Errorw(ctx, "invalid-meter-config", log.Fields{"meter-config": meterConfig})
67 return nil, fmt.Errorf("invalid-meter-config: %v", meterConfig)
68 }
69 return nil, fmt.Errorf("invalid-meter-config: %v", meterConfig)
70}