Module: Dcmgr::NodeModules::Bandwidth

Includes:
Helpers::NicHelper, Logger
Included in:
ServiceNetfilter
Defined in:
lib/dcmgr/node_modules/service_netfilter.rb

Instance Method Summary collapse

Methods included from Logger

create, default_logdev, included

Methods included from Helpers::NicHelper

#clean_mac, #find_nic, #is_natted?, #nic_state, #valid_nic?

Instance Method Details

#clear_bandwidth_limitsObject



12
13
14
15
# File 'lib/dcmgr/node_modules/service_netfilter.rb', line 12

def clear_bandwidth_limits
  logger.debug "Removing all bandwidth limits"
  "tc qdisc del dev #{find_nic(@node.manifest.config.hv_ifindex)} root"
end

#limit_bandwidth(networks) ⇒ Object

Enforces the bandwidth limits set for the networks. This uses the tc command to do so. networks is an array containing the networks to set the bandwidth limits for.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dcmgr/node_modules/service_netfilter.rb', line 22

def limit_bandwidth(networks)
  bandwidth_cmd = []
  #raise ArgumentError unless inst_maps.is_a?(Hash)
  nic = find_nic(@node.manifest.config.hv_ifindex)

  #Determine the physical nic's peed in Mbit/s
  speed = %x{ethtool #{nic} | grep Speed | cut -d ' ' -f2}.chomp.to_i

  #Set up root disc
  bandwidth_cmd << "tc qdisc add dev #{nic} root handle 1: htb"
  bandwidth_cmd << "tc class add dev #{nic} parent 1: classid 1:1 htb rate #{speed}mbit ceil #{speed}mbit"

  networks.each { |nw|
    next if nw[:bandwidth].nil?

    logger.debug "Limiting bandwidth to #{nw[:bandwidth]}Mbit/s for #{nw[:uuid]}."

    #Set up the bandwidth limit for this network
    bandwidth_cmd << "tc class add dev #{nic} parent 1:1 classid 1:1#{nw[:bandwidth_mark]} htb rate #{nw[:bandwidth]}mbit ceil #{nw[:bandwidth]}mbit prio 1"
    bandwidth_cmd << "tc qdisc add dev #{nic} parent 1:1#{nw[:bandwidth_mark]} handle 1#{nw[:bandwidth_mark]}: sfq perturb 10"
    bandwidth_cmd << "tc filter add dev #{nic} protocol ip parent 1: prio 1 handle #{nw[:bandwidth_mark]} fw classid 1:1#{nw[:bandwidth_mark]}"

    #Mark the packets passing through this network
    ["s","d"].each { |x| bandwidth_cmd << "iptables -A FORWARD -#{x} #{nw[:ipv4_gw]}/#{nw[:prefix]} -j MARK --set-mark 0x#{nw[:bandwidth_mark]}" }
  }

  bandwidth_cmd
end