Class: OsCtld::Devices::V2::BpfProgramCache

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
OsCtl::Lib::Utils::Log, Singleton
Defined in:
lib/osctld/devices/v2/bpf_program_cache.rb

Overview

Manage a list of BPF programs and their links

Program name is a hash of the listed devices, so identical programs are loaded just once and reused. Program links (i.e. attach on a cgroup) are tracked and when a program has no links left, it is destroyed.

Instance Method Summary collapse

Constructor Details

#initialize ⇒ BpfProgramCache

Returns a new instance of BpfProgramCache.



23
24
25
26
27
28
29
30
# File 'lib/osctld/devices/v2/bpf_program_cache.rb', line 23

def initialize
  @mutex = Mutex.new
  @programs = {}
  @links = {}
  @path_cache = {}

  load_programs
end

Instance Method Details

#assets(add) ⇒ Object

Parameters:



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
# File 'lib/osctld/devices/v2/bpf_program_cache.rb', line 33

def assets(add)
  sync do
    @programs.each_value do |prog|
      add.file(
        prog.path,
        desc: "BPF program #{prog.name}",
        user: 0,
        group: 0,
        mode: 0o600
      )
    end

    @links.each_value do |cgroup_paths|
      cgroup_paths.each_value do |link|
        add.file(
          link.path,
          desc: "BPF program #{link.prog_name} attached on #{link.cgroup_path}",
          user: 0,
          group: 0,
          mode: 0o600
        )
      end
    end
  end
end

#cgroup_key(path) ⇒ Object (protected)

CGroup setup validates the public/private views of the same hierarchy. Cache by their common logical path, while BpfLink preserves the actual existing pin name. Do not treat a similarly prefixed path as an alias.



207
208
209
210
211
212
213
# File 'lib/osctld/devices/v2/bpf_program_cache.rb', line 207

def cgroup_key(path)
  root = CGroup::DEFAULT_FS
  return CGroup::RUNSTATE_FS if path == root
  return path unless path.start_with?("#{root}/")

  "#{CGroup::RUNSTATE_FS}#{path.delete_prefix(root)}"
end

#get_prog_name(devices) ⇒ String

Parameters:

Returns:

  • (String)


170
171
172
173
# File 'lib/osctld/devices/v2/bpf_program_cache.rb', line 170

def get_prog_name(devices)
  data = devices.map(&:to_s).join(';')
  Digest::SHA2.hexdigest(data)[0..10]
end

Detect existing links in BPF FS

Parameters:

  • pool_name (String)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/osctld/devices/v2/bpf_program_cache.rb', line 61

def load_links(pool_name)
  sync do
    cnt = 0

    BpfFs.list_links(pool_name).each do |link_name|
      link = Devices::V2::BpfLink.from_name(pool_name, link_name)

      key = cgroup_key(link.cgroup_path)
      @links[link.prog_name] ||= {}
      @links[link.prog_name][key] = link

      @path_cache[key] = link

      cnt += 1
    end

    log(:info, "Loaded #{cnt} links")
  end
end

#load_programs ⇒ Object (protected)



215
216
217
218
219
220
221
222
223
# File 'lib/osctld/devices/v2/bpf_program_cache.rb', line 215

def load_programs
  sync do
    BpfFs.list_progs.each do |prog_name|
      @programs[prog_name] = Devices::V2::BpfProgram.new(prog_name, nil)
    end

    log(:info, "Loaded #{@programs.length} programs")
  end
end

#log_type ⇒ Object



198
199
200
# File 'lib/osctld/devices/v2/bpf_program_cache.rb', line 198

def log_type
  'bpf-program-cache'
end

Remove program links to the specified cgroup

Parameters:

  • cgroup_path (String)


177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/osctld/devices/v2/bpf_program_cache.rb', line 177

def prune_cgroup_links(cgroup_path)
  sync do
    key = cgroup_key(cgroup_path)
    link = @path_cache[key]
    return if link.nil?

    prog = @programs[link.prog_name]
    prog.detach(link)
    @path_cache.delete(key)
    @links[link.prog_name].delete(key)

    if @links[link.prog_name].empty?
      @links.delete(link.prog_name)
      @programs.delete(link.prog_name)
      prog.destroy
    end
  end

  nil
end

#set(pool_name, devices, cgroup_path, prog_name: nil) ⇒ String

Attach program allowing access to the specified devices to a cgroup

The program is attached only if it is not already attached. If prog_name is given, it is a caller hint; the pinned cached link identifies the actual program to replace, including inherited public-root aliases.

Parameters:

  • pool_name (String)
  • devices (Array<Devices::Device>)
  • cgroup_path (String)
  • prog_name (String, nil) (defaults to: nil) —

    previous program name

Returns:

  • (String) —

    program name



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
# File 'lib/osctld/devices/v2/bpf_program_cache.rb', line 92

def set(pool_name, devices, cgroup_path, prog_name: nil)
  sync do
    BpfFs.setup
    BpfFs.add_pool(pool_name)

    new_prog_name = get_prog_name(devices)

    original_program = @programs[new_prog_name]
    prog = original_program
    created_program = false

    unless prog&.exist?
      prog = Devices::V2::BpfProgram.new(
        new_prog_name,
        devices
      )
      begin
        prog.create
      rescue StandardError
        prog.destroy
        raise
      end

      @programs[new_prog_name] = prog
      created_program = true
    end

    begin
      key = cgroup_key(cgroup_path)
      old_link = @path_cache[key]
      old_link = nil if old_link && !BpfFs.link_pinned?(old_link.pool_name, old_link.name)

      # An adopted public-root pin remains valid after the hierarchy becomes
      # private. Its basename must not be rewritten before link replacement.
      return new_prog_name if old_link && old_link.prog_name == new_prog_name

      link = Devices::V2::BpfLink.new(new_prog_name, pool_name, cgroup_path)

      unless prog.attached?(link)
        if old_link
          prog.replace(old_link, link)
        else
          prog.attach(link)
        end
      end
    rescue StandardError
      # A failed link operation must not retain a new, unlinked program pin.
      # Preserve the original cached program and link for a later retry.
      if created_program
        prog.destroy
        if original_program
          @programs[new_prog_name] = original_program
        else
          @programs.delete(new_prog_name)
        end
      end
      raise
    end

    # Commit bookkeeping only after the kernel operation succeeded. A
    # failed replacement must retain the old pin for retry and cleanup.
    @links[old_link.prog_name].delete(key) if old_link
    @links[new_prog_name] ||= {}
    @links[new_prog_name][key] = link
    @path_cache[key] = link

    if old_link && @links[old_link.prog_name].empty?
      old_prog = @programs.delete(old_link.prog_name)
      @links.delete(old_link.prog_name)
      old_prog.destroy
    end

    new_prog_name
  end
end

#sync(&block) ⇒ Object (protected)



225
226
227
228
229
230
231
# File 'lib/osctld/devices/v2/bpf_program_cache.rb', line 225

def sync(&block)
  if @mutex.owned?
    block.call
  else
    @mutex.synchronize(&block)
  end
end