Class: Distem::Algorithm::Network::TBF

Inherits:
TCAlgorithm show all
Defined in:
lib/distem/algorithm/network/tbf.rb

Overview

An algorithm that's using TC Token Bucket Filter (see en.wikipedia.org/wiki/Token_bucket) to limit network traffic

Instance Method Summary (collapse)

Methods inherited from TCAlgorithm

#clean

Constructor Details

- (TBF) initialize

Create a new TBF object



9
10
11
# File 'lib/distem/algorithm/network/tbf.rb', line 9

def initialize()
  super()
end

Instance Method Details

- (Object) apply(viface)

Apply limitations on a specific virtual network interface

Attributes

  • viface The VIface object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/distem/algorithm/network/tbf.rb', line 17

def apply(viface)
  super(viface)
  if viface.latency_filters
    apply_filters(viface)
  else
    if viface.voutput
      apply_vtraffic(viface.voutput)
    end
    if viface.vinput
      apply_vtraffic(viface.vinput)
    end
  end
end

- (Object) apply_filters(viface)



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/distem/algorithm/network/tbf.rb', line 32

def apply_filters(viface)
  baseiface = Lib::NetTools::get_iface_name(viface.vnode, viface)
  latency_mapping = viface.latency_filters.values.uniq
  nb_filters = latency_mapping.length
  if nb_filters > 255
    raise "Too much different latency values for #{viface.vnode.name}"
  end

  ingressroot = TCWrapper::QdiscIngress.new(baseiface)
  add = ingressroot.get_cmd(TCWrapper::Action::ADD)
  Lib::Shell.run(add)
  viface.ifb = @@ifballocator.get_ifb if viface.ifb.nil?
  iface = viface.ifb
  qdiscroot = TCWrapper::QdiscRoot.new(iface)

  prio = TCWrapper::QdiscPrio.new(iface, qdiscroot, { 'bands' => 16 })
  add = prio.get_cmd(TCWrapper::Action::ADD)
  Lib::Shell.run(add)
  prio_roots = []
  netem = []
  if (nb_filters < 16)
    #1 prio level
    prio_roots << prio
    #create the Netem queues
    latency_mapping.each { |lat|
      current = TCWrapper::QdiscNetem.new(iface, prio, { 'delay' => "#{lat}ms"})
      netem << current
      add = current.get_cmd(TCWrapper::Action::ADD)
      Lib::Shell.run(add)
    }
    viface.latency_filters.each_pair { |dest,val|
      filter = TCWrapper::FilterU32.new(iface, prio, netem[latency_mapping.index(val)], "ip", 1)
      filter.add_match_ip_dst(dest)
      add = filter.get_cmd(TCWrapper::Action::ADD)
      Lib::Shell.run(add)
    }
    #default traffic
    fake_qdisc = TCWrapper::QdiscPrio.new(iface, prio)
    filter = TCWrapper::FilterU32.new(iface, prio, fake_qdisc, "ip", 2)
    filter.add_match_u32('0','0')
    cmd = filter.get_cmd(TCWrapper::Action::ADD)
    Lib::Shell.run(cmd)
  else
    #2 prio levels
    nb_2nd_level_prios = ((nb_filters + 1) / 16) + (((nb_filters + 1) % 16) > 0 ? 1 : 0)
    (0...nb_2nd_level_prios).each {
      current = TCWrapper::QdiscPrio.new(iface, prio, { 'bands' => 16})
      prio_roots << current
      add = current.get_cmd(TCWrapper::Action::ADD)
      Lib::Shell.run(add)
    }
    #create the Netem queues
    latency_mapping.each { |lat|
      current = TCWrapper::QdiscNetem.new(iface, prio_roots[latency_mapping.index(lat) / 16], { 'delay' => "#{lat}ms"})
      netem << current
      add = current.get_cmd(TCWrapper::Action::ADD)
      Lib::Shell.run(add)
    }
    #Filters
    viface.latency_filters.each_pair { |dest,val|
      filter = TCWrapper::FilterU32.new(iface, prio, prio_roots[latency_mapping.index(val) / 16], "ip", 1)
      filter.add_match_ip_dst(dest)
      add = filter.get_cmd(TCWrapper::Action::ADD)
      Lib::Shell.run(add)
      filter = TCWrapper::FilterU32.new(iface, prio_roots[latency_mapping.index(val) / 16], netem[latency_mapping.index(val)], "ip", 1)
      filter.add_match_ip_dst(dest)
      add = filter.get_cmd(TCWrapper::Action::ADD)
      Lib::Shell.run(add)
    }

    #default traffic
    fake_qdisc = TCWrapper::QdiscPrio.new(iface, prio_roots[nb_filters / 16])
    filter = TCWrapper::FilterU32.new(iface, prio_roots[nb_filters / 16], fake_qdisc, "ip", 2)
    filter.add_match_u32('0','0')
    cmd = filter.get_cmd(TCWrapper::Action::ADD)
    Lib::Shell.run(cmd)
  end
  filter = TCWrapper::FilterU32.new(baseiface, ingressroot, qdiscroot)
  filter.add_match_u32('0','0')
  filter.add_param("action","mirred egress")
  filter.add_param("redirect","dev #{iface}")
  Lib::Shell.run(filter.get_cmd(TCWrapper::Action::ADD))
end

- (Object) apply_vtraffic(vtraffic)

Apply the limitation following a specific traffic instruction

Attributes

  • vtraffic The VTraffic object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/distem/algorithm/network/tbf.rb', line 121

def apply_vtraffic(vtraffic)

  limited_bw_output = @limited_bw_output
  limited_lat_output = @limited_lat_output
  limited_bw_input = @limited_bw_input
  limited_lat_input = @limited_lat_input

  @limited_bw_output = false
  @limited_lat_output = false
  @limited_bw_input = false
  @limited_lat_input = false

  iface = Lib::NetTools::get_iface_name(vtraffic.viface.vnode,
                                        vtraffic.viface)
  baseiface = iface
  action = nil
  direction = nil
  bwlim = vtraffic.get_property(Resource::Bandwidth.name)
  latlim = vtraffic.get_property(Resource::Latency.name)
  case vtraffic.direction
  when Resource::VIface::VTraffic::Direction::INPUT
    if !(limited_bw_input || limited_lat_input)
      tcroot = TCWrapper::QdiscRoot.new(iface)
      tmproot = tcroot
    end
    direction = 'input'
  when Resource::VIface::VTraffic::Direction::OUTPUT
    if !(limited_bw_output || limited_lat_output) &&
        ((bwlim && bwlim.rate) || (latlim && latlim.delay))
      tcroot = TCWrapper::QdiscIngress.new(iface)
      Lib::Shell.run(tcroot.get_cmd(TCWrapper::Action::ADD))
      vtraffic.viface.ifb = @@ifballocator.get_ifb if vtraffic.viface.ifb.nil?
      iface = vtraffic.viface.ifb
      tmproot = TCWrapper::QdiscRoot.new(iface)
    end
    direction = 'output'
  else
    raise "Invalid direction"
  end

  primroot = nil
  bandwidth = nil
  if bwlim && bwlim.rate
    existing_root = nil
    if eval("limited_bw_#{direction}")
      action = TCWrapper::Action::CHANGE
      @@lock.synchronize {
        existing_root = @@store[vtraffic.viface]["bw_#{direction}"]
      }
    else
      action = TCWrapper::Action::ADD
    end
    self.instance_variable_set("@limited_bw_#{direction}", true)
    bandwidth = bwlim.to_bytes()
    params = {
      'rate' => "#{bwlim.rate}",
      # cf. http://www.juniper.net/techpubs/en_US/junos11.2/topics/reference/general/policer-guidelines-burst-size-calculating.html
      # buffer size = rate * latency (here latency is 50ms)
      # warning, the buffer size should be at least equal to the MTU (plus some bytes...)
      'buffer' => [Integer(bwlim.to_bytes * 0.05), Lib::NetTools::get_iface_mtu(vtraffic.viface.vnode, vtraffic.viface) + 20].max,
      'latency' => '50ms',
      #mtu parameter fixed because of a kernel bug, see http://comments.gmane.org/gmane.linux.network/252860
      'mtu' => '65536'
    }
    if existing_root
      tmproot = existing_root
      params.each_pair { |k,v|
        tmproot.add_param(k,v)
      }
    else
      tmproot = TCWrapper::QdiscTBF.new(iface,tmproot,params)
    end
    @@lock.synchronize {
      @@store[vtraffic.viface]["bw_#{direction}"] = tmproot
    }
    primroot = tmproot
    Lib::Shell.run(tmproot.get_cmd(action))
  end

  if latlim && latlim.delay
    existing_root = nil
    if eval("limited_lat_#{direction}")
      # if bandwidth limitation has been set before, netem is removed, so we have
      # to add it again
      action = primroot ? TCWrapper::Action::ADD : TCWrapper::Action::CHANGE
      @@lock.synchronize {
        existing_root = @@store[vtraffic.viface]["lat_#{direction}"]
      }
    else
      action = TCWrapper::Action::ADD
    end
    self.instance_variable_set("@limited_lat_#{direction}", true)
    if existing_root
      tmproot = existing_root
      latlim.tc_params(bandwidth).each_pair { |k,v|
        tmproot.add_param(k,v)
      }
    else
      tmproot = TCWrapper::QdiscNetem.new(
                                          iface, tmproot,
                                          latlim.tc_params(bandwidth)
                                          )
    end
    @@lock.synchronize {
      @@store[vtraffic.viface]["lat_#{direction}"] = tmproot
    }
    primroot = tmproot unless primroot
    Lib::Shell.run(tmproot.get_cmd(action))
  end

  if (vtraffic.direction == Resource::VIface::VTraffic::Direction::OUTPUT) &&
      !(limited_bw_output || limited_lat_output) &&
      ((bwlim && bwlim.rate) || (latlim && latlim.delay))
    filter = TCWrapper::FilterU32.new(baseiface,tcroot,primroot)
    filter.add_match_u32('0','0')
    filter.add_param("action","mirred egress")
    filter.add_param("redirect","dev #{iface}")
    Lib::Shell.run(filter.get_cmd(TCWrapper::Action::ADD))
  end
end

- (Object) undo(viface)

Undo limitations effective on a specific virtual network interface

Attributes

  • viface The VIface object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/distem/algorithm/network/tbf.rb', line 247

def undo(viface)
  super(viface)
  @@lock.synchronize {
    @@store[viface] = {}
  }

  iface = Lib::NetTools::get_iface_name(viface.vnode,viface)

  if (@limited_bw_output || @limited_lat_output)
    outputroot = TCWrapper::QdiscRoot.new(viface.ifb)
    Lib::Shell.run(outputroot.get_cmd(TCWrapper::Action::DEL))
    outputroot = TCWrapper::QdiscIngress.new(iface)
    Lib::Shell.run(outputroot.get_cmd(TCWrapper::Action::DEL))
    @limited_bw_output = false
    @limited_lat_output = false
  end

  if (@limited_bw_input || @limited_lat_input)
    inputroot = TCWrapper::QdiscRoot.new(iface)
    Lib::Shell.run(inputroot.get_cmd(TCWrapper::Action::DEL))
    @limited_bw_intput = false
    @limited_lat_intput = false
  end
end