Debian Codegraph @

plugins/meta/bandwidth/ifb_creator.go

raw symbols SCIP
1 // Copyright 2018 CNI 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 package main
16
17 import (
18 "fmt"
19 "net"
20 "syscall"
21
22 "github.com/vishvananda/netlink"
23
24 "github.com/containernetworking/plugins/pkg/ip"
25 "github.com/containernetworking/plugins/pkg/netlinksafe"
26 )
27
28 const latencyInMillis = 25
29
30 func CreateIfb(ifbDeviceName string, mtu int) error {
31 // do not set TxQLen > 0 nor TxQLen == -1 until issues have been fixed with numrxqueues / numtxqueues across interfaces
32 // which needs to get set on IFB devices via upstream library: see hint https://github.com/containernetworking/plugins/pull/1097
33 err := netlink.LinkAdd(&netlink.Ifb{
34 LinkAttrs: netlink.LinkAttrs{
35 Name: ifbDeviceName,
36 Flags: net.FlagUp,
37 MTU: mtu,
38 TxQLen: 0,
39 },
40 })
41 if err != nil {
42 return fmt.Errorf("adding link: %s", err)
43 }
44
45 return nil
46 }
47
48 func TeardownIfb(deviceName string) error {
49 _, err := ip.DelLinkByNameAddr(deviceName)
50 if err != nil && err == ip.ErrLinkNotFound {
51 return nil
52 }
53 return err
54 }
55
56 func CreateIngressQdisc(rateInBits, burstInBits uint64, hostDeviceName string) error {
57 hostDevice, err := netlinksafe.LinkByName(hostDeviceName)
58 if err != nil {
59 return fmt.Errorf("get host device: %s", err)
60 }
61 return createTBF(rateInBits, burstInBits, hostDevice.Attrs().Index)
62 }
63
64 func CreateEgressQdisc(rateInBits, burstInBits uint64, hostDeviceName string, ifbDeviceName string) error {
65 ifbDevice, err := netlinksafe.LinkByName(ifbDeviceName)
66 if err != nil {
67 return fmt.Errorf("get ifb device: %s", err)
68 }
69 hostDevice, err := netlinksafe.LinkByName(hostDeviceName)
70 if err != nil {
71 return fmt.Errorf("get host device: %s", err)
72 }
73
74 // add qdisc ingress on host device
75 ingress := &netlink.Ingress{
76 QdiscAttrs: netlink.QdiscAttrs{
77 LinkIndex: hostDevice.Attrs().Index,
78 Handle: netlink.MakeHandle(0xffff, 0), // ffff:
79 Parent: netlink.HANDLE_INGRESS,
80 },
81 }
82
83 err = netlink.QdiscAdd(ingress)
84 if err != nil {
85 return fmt.Errorf("create ingress qdisc: %s", err)
86 }
87
88 // add filter on host device to mirror traffic to ifb device
89 filter := &netlink.U32{
90 FilterAttrs: netlink.FilterAttrs{
91 LinkIndex: hostDevice.Attrs().Index,
92 Parent: ingress.QdiscAttrs.Handle,
93 Priority: 1,
94 Protocol: syscall.ETH_P_ALL,
95 },
96 ClassId: netlink.MakeHandle(1, 1),
97 RedirIndex: ifbDevice.Attrs().Index,
98 Actions: []netlink.Action{
99 &netlink.MirredAction{
100 ActionAttrs: netlink.ActionAttrs{},
101 MirredAction: netlink.TCA_EGRESS_REDIR,
102 Ifindex: ifbDevice.Attrs().Index,
103 },
104 },
105 }
106 err = netlink.FilterAdd(filter)
107 if err != nil {
108 return fmt.Errorf("add filter: %s", err)
109 }
110
111 // throttle traffic on ifb device
112 err = createTBF(rateInBits, burstInBits, ifbDevice.Attrs().Index)
113 if err != nil {
114 return fmt.Errorf("create ifb qdisc: %s", err)
115 }
116 return nil
117 }
118
119 func createTBF(rateInBits, burstInBits uint64, linkIndex int) error {
120 // Equivalent to
121 // tc qdisc add dev link root tbf
122 // rate netConf.BandwidthLimits.Rate
123 // burst netConf.BandwidthLimits.Burst
124 if rateInBits <= 0 {
125 return fmt.Errorf("invalid rate: %d", rateInBits)
126 }
127 if burstInBits <= 0 {
128 return fmt.Errorf("invalid burst: %d", burstInBits)
129 }
130 rateInBytes := rateInBits / 8
131 burstInBytes := burstInBits / 8
132 bufferInBytes := buffer(rateInBytes, uint32(burstInBytes))
133 latency := latencyInUsec(latencyInMillis)
134 limitInBytes := limit(rateInBytes, latency, uint32(burstInBytes))
135
136 qdisc := &netlink.Tbf{
137 QdiscAttrs: netlink.QdiscAttrs{
138 LinkIndex: linkIndex,
139 Handle: netlink.MakeHandle(1, 0),
140 Parent: netlink.HANDLE_ROOT,
141 },
142 Limit: limitInBytes,
143 Rate: rateInBytes,
144 Buffer: bufferInBytes,
145 }
146 err := netlink.QdiscAdd(qdisc)
147 if err != nil {
148 return fmt.Errorf("create qdisc: %s", err)
149 }
150 return nil
151 }
152
153 func time2Tick(time uint32) uint32 {
154 return uint32(float64(time) * netlink.TickInUsec())
155 }
156
157 func buffer(rate uint64, burst uint32) uint32 {
158 return time2Tick(uint32(float64(burst) * float64(netlink.TIME_UNITS_PER_SEC) / float64(rate)))
159 }
160
161 func limit(rate uint64, latency float64, buffer uint32) uint32 {
162 return uint32(float64(rate)*latency/float64(netlink.TIME_UNITS_PER_SEC)) + buffer
163 }
164
165 func latencyInUsec(latencyInMillis float64) float64 {
166 return float64(netlink.TIME_UNITS_PER_SEC) * (latencyInMillis / 1000.0)
167 }