Class: OsCtld::Devices::V2::BpfProgram

Inherits:
Object
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log
Defined in:
lib/osctld/devices/v2/bpf_program.rb

Overview

Create/destroy & attach/detach BPF programs for devices access control

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, devices) ⇒ BpfProgram

Create a new program

The device list can be nil. In that case, the program cannot be loaded into the kernel, because we don't know its contents, but we can still create/destroy links or unload the program from the kernel.

Parameters:



23
24
25
26
27
# File 'lib/osctld/devices/v2/bpf_program.rb', line 23

def initialize(name, devices)
  @name = name
  @devices = devices
  @path = BpfFs.prog_pin_path(name)
end

Instance Attribute Details

#name ⇒ String (readonly)

Returns:

  • (String)


9
10
11
# File 'lib/osctld/devices/v2/bpf_program.rb', line 9

def name
  @name
end

#path ⇒ String (readonly)

Pin file path

Returns:

  • (String)


13
14
15
# File 'lib/osctld/devices/v2/bpf_program.rb', line 13

def path
  @path
end

Instance Method Details

#attach(link) ⇒ Object

Attach program to cgroup

Parameters:



74
75
76
77
78
79
80
81
82
# File 'lib/osctld/devices/v2/bpf_program.rb', line 74

def attach(link)
  BpfFs.add_pool(link.pool_name)
  run_devcgprog(
    'attach',
    path,
    link.cgroup_path,
    link.path
  )
end

#attached?(link) ⇒ Boolean

Check if program is attached to a cgroup

Note that even if this method returns true, the link may still be broken if the underlying cgroup has been destroyed and recreated. We have no way of verifying it using BPF FS.

Parameters:

Returns:

  • (Boolean)


68
69
70
# File 'lib/osctld/devices/v2/bpf_program.rb', line 68

def attached?(link)
  BpfFs.link_pinned?(link.pool_name, link.name)
end

#create ⇒ Object

Load the program to the kernel



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/osctld/devices/v2/bpf_program.rb', line 35

def create
  if @devices.nil?
    raise 'unable to create incomplete program'
  end

  args = %W[
    -name #{@name}
    new
    #{path}
    allow
  ]

  @devices.each do |dev|
    args << "#{dev.type_s}:#{dev.major}:#{dev.minor}:#{dev.mode}"
  end

  BpfFs.setup
  run_devcgprog(*args)
end

#destroy ⇒ Object



55
56
57
58
59
# File 'lib/osctld/devices/v2/bpf_program.rb', line 55

def destroy
  File.unlink(path)
rescue Errno::ENOENT
  # The pin can already be gone after cgroup/BPF cleanup or pool export.
end

#detach(link) ⇒ Object

Detach program from cgroup

Parameters:



118
119
120
121
122
# File 'lib/osctld/devices/v2/bpf_program.rb', line 118

def detach(link)
  File.unlink(link.path)
rescue Errno::ENOENT
  # The link pin can already be gone when the cgroup tree was removed.
end

#exist? ⇒ Boolean

Check if the program is loaded within the kernel

Returns:

  • (Boolean)


30
31
32
# File 'lib/osctld/devices/v2/bpf_program.rb', line 30

def exist?
  BpfFs.prog_pinned?(@name)
end

#replace(link, new_link) ⇒ Object

Atomically replace attached program with another program

Parameters:



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/osctld/devices/v2/bpf_program.rb', line 87

def replace(link, new_link)
  if link.pool_name != new_link.pool_name
    raise ArgumentError,
          "link on pool #{link.pool_name} while new_link on pool #{new_link.pool_name}"
  end

  BpfFs.add_pool(new_link.pool_name)
  begin
    run_devcgprog(
      'replace',
      link.path,
      BpfFs.prog_pin_path(new_link.prog_name),
      new_link.path
    )
  rescue StandardError => e
    # devcgprog updates the live link before renaming its pin. If the rename
    # fails, the old pin still exists but now points at the new program.
    # Restore the original program without attempting another pin rename.
    replace_error = e
    begin
      run_devcgprog('replace', link.path, BpfFs.prog_pin_path(link.prog_name))
    rescue StandardError => e
      raise "unable to restore BPF link #{link.path} after #{replace_error.message}: #{e.message}"
    end

    raise replace_error
  end
end

#run_devcgprog(*args) ⇒ Object (protected)



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/osctld/devices/v2/bpf_program.rb', line 126

def run_devcgprog(*args)
  cmd = ['devcgprog'] + args

  log(:info, cmd.join(' '))
  pid = Process.spawn(*cmd)
  Process.wait(pid)

  if $?.exitstatus != 0
    raise "#{cmd.join(' ')} failed with exit status #{$?.exitstatus}"
  end

  nil
end