Class: OsCtld::NetInterface::Bridge

Inherits:
Veth
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System, Utils::Ip, Utils::SwitchUser
Defined in:
lib/osctld/net_interface/bridge.rb

Instance Attribute Summary collapse

Attributes inherited from Veth

#rx_queues, #tx_queues, #veth

Attributes inherited from Base

#ct, #hwaddr, #index, #max_rx, #max_tx, #name

Instance Method Summary collapse

Methods included from Utils::SwitchUser

#ct_attach, #ct_syscmd

Methods included from Utils::Ip

#ip, #tc

Methods inherited from Veth

#active_ip_versions, #del_all_ips, #down, #dup, #fetch_veth_name, #has_ip?, #hook_path, #ifb_veth, #ips, #is_up?, #load_ip_list, #mode_path, #rename, #save_ip_list, #set_shaper_rx, #set_shaper_tx, #setup, #unset_shaper_rx, #unset_shaper_tx, #up, #veth_hook_dir

Methods inherited from Base

#can_add_ip?, #can_run_distconfig?, #down, #dup, #has_ip?, #initialize, #ips, #is_down?, #is_up?, #rename, setup, #setup, type, #type, #up

Methods included from Lockable

#exclusively, included, #inclusively, #init_lock, #lock, #unlock

Constructor Details

This class inherits a constructor from OsCtld::NetInterface::Base

Instance Attribute Details

#dhcpObject (readonly)

Returns the value of attribute dhcp.



12
13
14
# File 'lib/osctld/net_interface/bridge.rb', line 12

def dhcp
  @dhcp
end

#gatewaysObject (readonly)

Returns the value of attribute gateways.



12
13
14
# File 'lib/osctld/net_interface/bridge.rb', line 12

def gateways
  @gateways
end

Returns the value of attribute link.



12
13
14
# File 'lib/osctld/net_interface/bridge.rb', line 12

def link
  @link
end

Instance Method Details

#add_ip(addr) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/osctld/net_interface/bridge.rb', line 77

def add_ip(addr)
  super

  v = addr.ipv4? ? 4 : 6

  ct.inclusively do
    next if ct.state != :running

    # Add IP within the CT
    ct_syscmd(
      ct,
      ['ip', "-#{v}", 'addr', 'add', addr.to_string, 'dev', name],
      valid_rcs: [2]
    )
  end
end

#create(opts) ⇒ Object

Parameters:

  • opts (Hash)

Options Hash (opts):

  • name (String)
  • link (String)
  • dhcp (Boolean)
  • gateways (Hash)


19
20
21
22
23
24
25
# File 'lib/osctld/net_interface/bridge.rb', line 19

def create(opts)
  super

  @link = opts[:link]
  @dhcp = opts.has_key?(:dhcp) ? opts[:dhcp] : true
  @gateways = opts[:gateways] || { 4 => 'auto', 6 => 'auto' }
end

#del_ip(addr) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/osctld/net_interface/bridge.rb', line 94

def del_ip(addr)
  super

  v = addr.ipv4? ? 4 : 6

  ct.inclusively do
    next if ct.state != :running

    # Remove IP from within the CT
    ct_syscmd(
      ct,
      ['ip', "-#{v}", 'addr', 'del', addr.to_string, 'dev', name],
      valid_rcs: [2]
    )
  end
end

#gateway(v) ⇒ String

Parameters:

  • v (Integer)

    IP version

Returns:

  • (String)


119
120
121
# File 'lib/osctld/net_interface/bridge.rb', line 119

def gateway(v)
  get_gateway(v) || (raise 'no gateway set')
end

#get_gateway(v) ⇒ Object (protected)



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/osctld/net_interface/bridge.rb', line 125

def get_gateway(v)
  inclusively do
    @gateway_cache ||= {}
    return @gateway_cache[v] if @gateway_cache.has_key?(v)

    gw = case gateways[v]
         when nil, 'auto'
           any_ifaddr = Socket.getifaddrs.detect do |ifaddr|
             ifaddr.name == link && ifaddr.addr.ip? && ifaddr.addr.send(:"ipv#{v}?")
           end

           any_ifaddr ? any_ifaddr.addr.ip_address : nil

         when 'none'
           nil

         else
           gateways[v]
         end

    @gateway_cache[v] = gw
  end
end

#has_gateway?(v) ⇒ Boolean

Parameters:

  • v (Integer)

    IP version

Returns:

  • (Boolean)


113
114
115
# File 'lib/osctld/net_interface/bridge.rb', line 113

def has_gateway?(v)
  !get_gateway(v).nil?
end

#load(cfg) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/osctld/net_interface/bridge.rb', line 27

def load(cfg)
  super

  @link = cfg['link']
  @dhcp = cfg.has_key?('dhcp') ? cfg['dhcp'] : true

  @gateways = if cfg['gateways']
                [4, 6].to_h do |ip_v|
                  [ip_v, cfg['gateways']["v#{ip_v}"] || 'auto']
                end
              else
                { 4 => 'auto', 6 => 'auto' }
              end
end

#render_optsObject



69
70
71
72
73
74
75
# File 'lib/osctld/net_interface/bridge.rb', line 69

def render_opts
  inclusively do
    super.merge({
      link:
    })
  end
end

#saveObject



42
43
44
45
46
47
48
49
50
# File 'lib/osctld/net_interface/bridge.rb', line 42

def save
  inclusively do
    super.merge({
      'link' => link,
      'dhcp' => dhcp,
      'gateways' => gateways.any? ? gateways.transform_keys { |k| "v#{k}" } : nil
    })
  end
end

#set(opts) ⇒ Object

Parameters:

  • opts (Hash)

    options

Options Hash (opts):

  • :link (String)
  • :dhcp (Boolean)
  • :gateways (Hash<Integer, String>)


56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/osctld/net_interface/bridge.rb', line 56

def set(opts)
  exclusively do
    super
    @link = opts[:link] if opts[:link]
    @dhcp = opts[:dhcp] if opts.has_key?(:dhcp)

    if opts[:gateways]
      @gateways.update(opts[:gateways])
      @gateway_cache = nil
    end
  end
end