...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/netem/convert.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/netem

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package netem
    17  
    18  import (
    19  	"strconv"
    20  	"time"
    21  
    22  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    23  	chaosdaemonpb "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/pb"
    24  )
    25  
    26  // FromDelay convert delay to netem
    27  func FromDelay(in *v1alpha1.DelaySpec) (*chaosdaemonpb.Netem, error) {
    28  	delayTime, err := time.ParseDuration(in.Latency)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	jitter, err := time.ParseDuration(in.Jitter)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  
    37  	corr, err := strconv.ParseFloat(in.Correlation, 32)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  
    42  	netem := &chaosdaemonpb.Netem{
    43  		Time:      uint32(delayTime.Nanoseconds() / 1e3),
    44  		DelayCorr: float32(corr),
    45  		Jitter:    uint32(jitter.Nanoseconds() / 1e3),
    46  	}
    47  
    48  	if in.Reorder != nil {
    49  		reorderPercentage, err := strconv.ParseFloat(in.Reorder.Reorder, 32)
    50  		if err != nil {
    51  			return nil, err
    52  		}
    53  
    54  		corr, err := strconv.ParseFloat(in.Reorder.Correlation, 32)
    55  		if err != nil {
    56  			return nil, err
    57  		}
    58  
    59  		netem.Reorder = float32(reorderPercentage)
    60  		netem.ReorderCorr = float32(corr)
    61  		netem.Gap = uint32(in.Reorder.Gap)
    62  	}
    63  
    64  	return netem, nil
    65  }
    66  
    67  // FromLoss convert loss to netem
    68  func FromLoss(in *v1alpha1.LossSpec) (*chaosdaemonpb.Netem, error) {
    69  	lossPercentage, err := strconv.ParseFloat(in.Loss, 32)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	corr, err := strconv.ParseFloat(in.Correlation, 32)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	return &chaosdaemonpb.Netem{
    80  		Loss:     float32(lossPercentage),
    81  		LossCorr: float32(corr),
    82  	}, nil
    83  }
    84  
    85  // FromDuplicate convert duplicate to netem
    86  func FromDuplicate(in *v1alpha1.DuplicateSpec) (*chaosdaemonpb.Netem, error) {
    87  	duplicatePercentage, err := strconv.ParseFloat(in.Duplicate, 32)
    88  	if err != nil {
    89  		return nil, err
    90  	}
    91  
    92  	corr, err := strconv.ParseFloat(in.Correlation, 32)
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  
    97  	return &chaosdaemonpb.Netem{
    98  		Duplicate:     float32(duplicatePercentage),
    99  		DuplicateCorr: float32(corr),
   100  	}, nil
   101  }
   102  
   103  // FromCorrupt convert corrupt to netem
   104  func FromCorrupt(in *v1alpha1.CorruptSpec) (*chaosdaemonpb.Netem, error) {
   105  	corruptPercentage, err := strconv.ParseFloat(in.Corrupt, 32)
   106  	if err != nil {
   107  		return nil, err
   108  	}
   109  
   110  	corr, err := strconv.ParseFloat(in.Correlation, 32)
   111  	if err != nil {
   112  		return nil, err
   113  	}
   114  
   115  	return &chaosdaemonpb.Netem{
   116  		Corrupt:     float32(corruptPercentage),
   117  		CorruptCorr: float32(corr),
   118  	}, nil
   119  }
   120  
   121  // FromRate convert RateSpec to netem
   122  func FromRate(in *v1alpha1.RateSpec) (*chaosdaemonpb.Netem, error) {
   123  	return &chaosdaemonpb.Netem{
   124  		Rate: in.Rate,
   125  	}, nil
   126  }
   127  
   128  // FromBandwidth converts BandwidthSpec to *chaosdaemonpb.Tbf
   129  // Bandwidth action use TBF under the hood.
   130  // TBF stands for Token Bucket Filter, is a classful queueing discipline available
   131  // for traffic control with the tc command.
   132  // http://man7.org/linux/man-pages/man8/tc-tbf.8.html
   133  func FromBandwidth(in *v1alpha1.BandwidthSpec) (*chaosdaemonpb.Tbf, error) {
   134  	tbf := &chaosdaemonpb.Tbf{
   135  		Rate:   in.Rate,
   136  		Limit:  in.Limit,
   137  		Buffer: in.Buffer,
   138  	}
   139  
   140  	if in.Peakrate != nil && in.Minburst != nil {
   141  		tbf.PeakRate = *in.Peakrate
   142  		tbf.MinBurst = *in.Minburst
   143  	}
   144  
   145  	return tbf, nil
   146  }
   147