From d58fe06cf26673c7e3f53435e71aa099aaeabd1b Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Wed, 30 Mar 2016 17:31:20 +0200 Subject: [PATCH] table.py: Implement`scanf` and `printf` helpers --- src/lua/bcc/table.lua | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/lua/bcc/table.lua b/src/lua/bcc/table.lua index a0b8ade..094a783 100644 --- a/src/lua/bcc/table.lua +++ b/src/lua/bcc/table.lua @@ -38,6 +38,46 @@ function BaseTable:initialize(t_type, bpf, map_id, map_fd, key_type, leaf_type) self.c_leaf = ffi.typeof(leaf_type.."[1]") end +function BaseTable:key_sprintf(key) + local pkey = self.c_key(key) + local buf_len = ffi.sizeof(self.c_key) * 8 + local pbuf = ffi.new("char[?]", buf_len) + + local res = libbcc.bpf_table_key_snprintf( + self.bpf.module, self.map_id, pbuf, buf_len, pkey) + assert(res == 0, "could not print key") + + return ffi.string(pbuf) +end + +function BaseTable:leaf_sprintf(leaf) + local pleaf = self.c_leaf(leaf) + local buf_len = ffi.sizeof(self.c_leaf) * 8 + local pbuf = ffi.new("char[?]", buf_len) + + local res = libbcc.bpf_table_leaf_snprintf( + self.bpf.module, self.map_id, pbuf, buf_len, pleaf) + assert(res == 0, "could not print leaf") + + return ffi.string(pbuf) +end + +function BaseTable:key_scanf(key_str) + local pkey = self.c_key() + local res = libbcc.bpf_table_key_sscanf( + self.bpf.module, self.map_id, key_str, pkey) + assert(res == 0, "could not scanf key") + return pkey[0] +end + +function BaseTable:leaf_scanf(leaf_str) + local pleaf = self.c_leaf() + local res = libbcc.bpf_table_leaf_sscanf( + self.bpf.module, self.map_id, leaf_str, pleaf) + assert(res == 0, "could not scanf leaf") + return pleaf[0] +end + function BaseTable:get(key) local pkey = self.c_key(key) local pvalue = self.c_leaf() -- 2.7.4