class RbBCC::TableBase

Attributes

bpf[R]
flags[R]
keysize[R]
keytype[R]
leafsize[R]
leaftype[R]
map_fd[R]
map_id[R]
name[R]
ttype[R]

Public Class Methods

new(bpf, map_id, map_fd, keytype, leaftype, name: nil) click to toggle source
# File lib/rbbcc/table.rb, line 50
def initialize(bpf, map_id, map_fd, keytype, leaftype, name: nil)
  @bpf, @map_id, @map_fd, @keysize, @leafsize = \
                                    bpf, map_id, map_fd, sizeof(keytype), sizeof(leaftype)
  @keytype = keytype
  @leaftype = leaftype
  @ttype = Clib.bpf_table_type_id(self.bpf.module, self.map_id)
  @flags = Clib.bpf_table_flags_id(self.bpf.module, self.map_id)
  @name = name
end

Public Instance Methods

[](_key) click to toggle source
# File lib/rbbcc/table.rb, line 82
def [](_key)
  key = normalize_key(_key)

  leaf = Fiddle::Pointer.malloc(self.leafsize)
  res = Clib.bpf_lookup_elem(self.map_fd, key, leaf)
  if res < 0
    nil
  end
  leaf.bcc_value_type = leaftype
  return leaf
end
[]=(_key, _leaf) click to toggle source
# File lib/rbbcc/table.rb, line 98
def []=(_key, _leaf)
  key = normalize_key(_key)
  leaf = normalize_leaf(_leaf)
  res = Clib.bpf_update_elem(self.map_fd, key, leaf, 0)
  if res < 0
    raise SystemCallError.new("Could not update table", Fiddle.last_error)
  end
  leaf
end
clear() click to toggle source
# File lib/rbbcc/table.rb, line 144
def clear
  each_key {|key| self.delete(key) }
  return items # reload contents
end
delete(key) click to toggle source
# File lib/rbbcc/table.rb, line 108
def delete(key)
  res = Clib.bpf_delete_elem(self.map_fd, key)
  if res < 0
    raise KeyError, "key not found"
  end
  res
end
each()
Alias for: each_pair
each_key() { |k| ... } click to toggle source
# File lib/rbbcc/table.rb, line 116
def each_key
  k = nil
  keys = []
  loop do
    k = self.next(k)
    keys << k
    yield k
  end
  keys
end
each_pair() { |key, self| ... } click to toggle source
# File lib/rbbcc/table.rb, line 131
def each_pair
  each_key {|key| yield(key, self[key]) if self[key] }
end
Also aliased as: each
each_value() { |self| ... } click to toggle source
# File lib/rbbcc/table.rb, line 127
def each_value
  each_key {|key| yield(self[key]) if self[key] }
end
fetch(key) click to toggle source
# File lib/rbbcc/table.rb, line 94
def fetch(key)
  self[key] || raise(KeyError, "key not found")
end
items() click to toggle source
# File lib/rbbcc/table.rb, line 140
def items
  enum_for(:each_pair).to_a
end
next(key) click to toggle source
# File lib/rbbcc/table.rb, line 61
def next(key)
  next_key = Fiddle::Pointer.malloc(self.keysize)

  if !key
    res = Clib.bpf_get_first_key(self.map_fd, next_key,
                                 next_key.size)
  else
    unless key.is_a?(Fiddle::Pointer)
      raise TypeError, key.inspect
    end
    res = Clib.bpf_get_next_key(self.map_fd, key,
                                next_key)
  end

  if res < 0
    raise StopIteration
  end

  return next_key
end
print_linear_hist(val_type="value", section_header: "Bucket ptr", section_print_fn: nil, bucket_fn: nil, bucket_sort_fn: nil) click to toggle source
print_log2_hist(val_type="value", section_header: "Bucket ptr", section_print_fn: nil, bucket_fn: nil, strip_leading_zero: false, bucket_sort_fn: nil) click to toggle source
structured_key?() click to toggle source
# File lib/rbbcc/table.rb, line 184
def structured_key?
  false # TODO: implement me in the future
end
values() click to toggle source
# File lib/rbbcc/table.rb, line 136
def values
  enum_for(:each_value).to_a
end

Private Instance Methods

byref(value, size=sizeof("int")) click to toggle source
# File lib/rbbcc/table.rb, line 218
def byref(value, size=sizeof("int"))
  pack_fmt = case size
             when sizeof("int") ; "i!"
             when sizeof("long"); "l!"
             else               ; "Z*"
             end
  ptr = Fiddle::Pointer.malloc(size)
  ptr[0, size] = [value].pack(pack_fmt)
  ptr.bcc_size = size
  ptr
end
normalize_key(key) click to toggle source
# File lib/rbbcc/table.rb, line 189
def normalize_key(key)
  case key
  when Fiddle::Pointer
    key.bcc_value_type = keytype
    key.bcc_size = keysize
    key
  when Integer
    ret = byref(key, keysize)
    ret.bcc_value_type = keytype
    ret
  else
    raise ArgumentError, "#{key.inspect} must be integer or pointor"
  end
end
normalize_leaf(leaf) click to toggle source
# File lib/rbbcc/table.rb, line 204
def normalize_leaf(leaf)
  case leaf
  when Fiddle::Pointer
    leaf.bcc_value_type = leaftype
    leaf
  when Integer
    ret = byref(leaf, keysize)
    ret.bcc_value_type = leaftype
    ret
  else
    raise KeyError, "#{leaf.inspect} must be integer or pointor"
  end
end