Commit cd5cb41e authored by Brenden Blanco's avatar Brenden Blanco

Initial commit

Signed-off-by: default avatarBrenden Blanco <bblanco@plumgrid.com>
parent a94bd931
*.swp
*.swo
cmake_minimum_required(VERSION 2.8.7)
project(bpf-tools)
set(CMAKE_BUILD_TYPE Debug)
enable_testing()
find_package(BISON)
find_package(FLEX)
find_package(LLVM REQUIRED CONFIG)
message(STATUS "Found LLVM: ${LLVM_INCLUDE_DIRS}")
find_program(XXD xxd)
if (${XXD} STREQUAL "XXD-NOTFOUND")
message(FATAL_ERROR "program xxd not found, install vim-common")
......@@ -13,8 +17,15 @@ find_program(CLANG clang)
if (${CLANG} STREQUAL "CLANG-NOTFOUND")
message(FATAL_ERROR "program clang not found, install clang with bpf support")
endif()
execute_process(COMMAND ${CLANG} --version OUTPUT_VARIABLE CLANG_VERSION_RAW)
string(REGEX MATCH "[0-9]+[.][0-9]+[.][0-9]+" CLANG_VERSION ${CLANG_VERSION_RAW})
message(STATUS "Found CLANG: ${CLANG} (found version \"${CLANG_VERSION}\")")
if (CLANG_VERSION VERSION_LESS 3.6.0)
message(FATAL_ERROR "requires clang version >= 3.6.0, ${CLANG_VERSION} found")
endif()
set(CMAKE_C_FLAGS "-Wall")
set(CMAKE_CXX_FLAGS "-std=c++11 -Wall")
add_subdirectory(jit)
add_subdirectory(src)
add_subdirectory(tests)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -flto")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto -fno-rtti")
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/compat/include)
add_subdirectory(src)
This diff is collapsed.
/*
* =====================================================================
* Copyright (c) 2012, PLUMgrid, http://plumgrid.com
*
* This source is subject to the PLUMgrid License.
* All rights reserved.
*
* THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF
* ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
* PARTICULAR PURPOSE.
*
* PLUMgrid confidential information, delete if you are not the
* intended recipient.
*
* =====================================================================
*/
#pragma once
#include <stdio.h>
#include <vector>
#include <string>
#include <set>
#include "cc/node.h"
#include "cc/scope.h"
namespace ebpf {
namespace cc {
using std::vector;
using std::string;
using std::set;
class CodegenC : public Visitor {
public:
CodegenC(FILE* out, Scopes::Ptr scopes, Scopes::Ptr proto_scopes, bool use_pre_header)
: out_(out), indent_(0), tmp_reg_index_(0), scopes_(scopes),
proto_scopes_(proto_scopes), use_pre_header_(use_pre_header) {}
#define VISIT(type, func) virtual void visit_##func(type* n);
EXPAND_NODES(VISIT)
#undef VISIT
virtual int visit(Node* n);
void emit_table_lookup(MethodCallExprNode* n);
void emit_table_update(MethodCallExprNode* n);
void emit_table_delete(MethodCallExprNode* n);
void emit_channel_push(MethodCallExprNode* n);
void emit_channel_push_generic(MethodCallExprNode* n);
void emit_log(MethodCallExprNode* n);
void emit_packet_forward(MethodCallExprNode* n);
void emit_packet_replicate(MethodCallExprNode* n);
void emit_packet_clone_forward(MethodCallExprNode* n);
void emit_packet_forward_self(MethodCallExprNode* n);
void emit_packet_drop(MethodCallExprNode* n);
void emit_packet_broadcast(MethodCallExprNode* n);
void emit_packet_multicast(MethodCallExprNode* n);
void emit_packet_push_header(MethodCallExprNode* n);
void emit_packet_pop_header(MethodCallExprNode* n);
void emit_packet_push_vlan(MethodCallExprNode* n);
void emit_packet_pop_vlan(MethodCallExprNode* n);
void emit_packet_rewrite_field(MethodCallExprNode* n);
void emit_atomic_add(MethodCallExprNode* n);
void emit_cksum(MethodCallExprNode* n);
void emit_incr_cksum_u16(MethodCallExprNode* n);
void emit_incr_cksum_u32(MethodCallExprNode* n);
void emit_lb_hash(MethodCallExprNode* n);
void emit_sizeof(MethodCallExprNode* n);
void emit_get_usec_time(MethodCallExprNode* n);
void emit_forward_to_vnf(MethodCallExprNode* n);
void emit_forward_to_group(MethodCallExprNode* n);
void print_parser();
void print_timer();
void print_header();
void print_footer();
private:
void indent();
template <typename... Args> void emitln(const char *fmt, Args&&... params);
template <typename... Args> void lnemit(const char *fmt, Args&&... params);
template <typename... Args> void emit(const char *fmt, Args&&... params);
void emitln(const char *s);
void lnemit(const char *s);
void emit(const char *s);
void emit_comment(Node* n);
FILE* out_;
int indent_;
int tmp_reg_index_;
Scopes::Ptr scopes_;
Scopes::Ptr proto_scopes_;
bool use_pre_header_;
vector<vector<string> > free_instructions_;
vector<string> table_inits_;
map<string, string> proto_rewrites_;
};
} // namespace cc
} // namespace ebpf
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti")
#include_directories(${CMAKE_CURRENT_SOURCE_DIR}/compat/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${CMAKE_CURRENT_BINARY_DIR})
......
import ctypes as ct
import os
import pyroute2 as pr
lib = ct.cdll.LoadLibrary("libbpfprog.so")
......@@ -35,35 +34,45 @@ lib.bpf_attach_filter.argtypes = [ct.c_int, ct.c_char_p, ct.c_uint, ct.c_ubyte,
lib.bpf_prog_load.restype = ct.c_int
lib.bpf_prog_load.argtypes = [ct.c_int, ct.c_void_p, ct.c_size_t,
ct.c_char_p]
lib.bpf_attach_kprobe.restype = ct.c_int
lib.bpf_attach_kprobe.argtypes = [ct.c_int, ct.c_char_p, ct.c_char_p, ct.c_int, ct.c_int, ct.c_int]
class BPF(object):
BPF_PROG_TYPE_SOCKET_FILTER = 1
BPF_PROG_TYPE_SCHED_CLS = 2
BPF_PROG_TYPE_SCHED_ACT = 3
BPF_PROG_TYPE_KPROBE = 2
BPF_PROG_TYPE_SCHED_CLS = 3
BPF_PROG_TYPE_SCHED_ACT = 4
def __init__(self, name, dp_file, dph_file,
prog_type=BPF_PROG_TYPE_SOCKET_FILTER,
debug=0):
self.debug = debug
self.name = name
self.prog_type = prog_type
self.fd = {}
self.prog = lib.bpf_program_create(dp_file.encode("ascii"),
dph_file.encode("ascii"), self.debug)
if self.prog == ct.c_void_p(None):
if self.prog == None:
raise Exception("Failed to compile BPF program %s" % dp_file)
if lib.bpf_program_start(self.prog,
self.name.encode("ascii")) == ct.c_void_p(None):
if prog_type == BPF.BPF_PROG_TYPE_KPROBE:
return
self.load(self.name)
def load(self, prog_name):
if lib.bpf_program_start(self.prog, prog_name.encode("ascii")) == None:
raise Exception("Unknown program %s" % self.name)
self.fd = lib.bpf_prog_load(prog_type,
lib.bpf_program_start(self.prog, self.name.encode("ascii")),
lib.bpf_program_size(self.prog, self.name.encode("ascii")),
self.fd[prog_name] = lib.bpf_prog_load(self.prog_type,
lib.bpf_program_start(self.prog, prog_name.encode("ascii")),
lib.bpf_program_size(self.prog, prog_name.encode("ascii")),
lib.bpf_program_license(self.prog))
if self.fd < 0:
if self.fd[prog_name] < 0:
print((ct.c_char * 65536).in_dll(lib, "bpf_log_buf").value)
#print(ct.c_char_p.in_dll(lib, "bpf_log_buf").value)
raise Exception("Failed to load BPF program %s" % dp_file)
raise Exception("Failed to load BPF program %s" % self.name)
class Table(object):
def __init__(self, bpf, map_fd, keytype, leaftype):
......@@ -126,20 +135,39 @@ class BPF(object):
raise Exception("Failed to find BPF Table %s" % name)
return BPF.Table(self, map_fd, keytype, leaftype)
def attach(self, dev):
def attach(self, dev, prog_name=None):
prog_name = prog_name or self.name
self.sock = lib.bpf_open_raw_sock(dev.encode("ascii"))
if self.sock < 0:
errstr = os.strerror(ct.get_errno())
raise Exception("Failed to open raw device %s: %s" % (dev, errstr))
res = lib.bpf_attach_socket(self.sock, self.fd)
res = lib.bpf_attach_socket(self.sock, self.fd[prog_name])
if res < 0:
errstr = os.strerror(ct.get_errno())
raise Exception("Failed to attach BPF to device %s: %s"
% (dev, errstr))
def attach_filter(self, ifindex, prio, classid):
res = lib.bpf_attach_filter(self.fd, self.name.encode("ascii"), ifindex, prio, classid)
def attach_filter(self, ifindex, prio, classid, prog_name=None):
prog_name = prog_name or self.name
res = lib.bpf_attach_filter(self.fd[prog_name], self.name.encode("ascii"), ifindex, prio, classid)
if res < 0:
raise Exception("Failed to filter with BPF")
def attach_kprobe(self, event, prog_name, pid=-1, cpu=0, group_fd=-1):
ev_name = "p_" + event.replace("+", "_")
desc = "p:kprobes/%s %s" % (ev_name, event)
res = lib.bpf_attach_kprobe(self.fd[prog_name], ev_name.encode("ascii"),
desc.encode("ascii"), pid, cpu, group_fd)
if res < 0:
raise Exception("Failed to attach BPF to kprobe")
return res
def attach_kretprobe(self, event, prog_name, pid=-1, cpu=0, group_fd=-1):
ev_name = "r_" + event.replace("+", "_")
desc = "r:kprobes/%s %s" % (ev_name, event)
res = lib.bpf_attach_kprobe(self.fd[prog_name], ev_name.encode("ascii"),
desc.encode("ascii"), pid, cpu, group_fd)
if res < 0:
raise Exception("Failed to attach BPF to kprobe")
return res
......@@ -10,9 +10,9 @@ ADD_FLEX_BISON_DEPENDENCY(Lexer Parser)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/bitops.bc
COMMAND ${CLANG}
ARGS -O3 -emit-llvm -S -o bitops.bc -I${CMAKE_SOURCE_DIR}/jit/compat/include
ARGS -O3 -emit-llvm -o bitops.bc -I${CMAKE_SOURCE_DIR}/jit/compat/include
-c ${CMAKE_CURRENT_SOURCE_DIR}/bitops.c
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/bitops.c
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/bitops.c ${CMAKE_CURRENT_SOURCE_DIR}/bpf_helpers.h
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating bitops IR")
add_custom_command(
......
......@@ -15,6 +15,12 @@ static int (*bpf_map_update_elem)(void *map, void *key, void *value,
(void *) BPF_FUNC_map_update_elem;
static int (*bpf_map_delete_elem)(void *map, void *key) =
(void *) BPF_FUNC_map_delete_elem;
static int (*bpf_probe_read)(void *dst, unsigned long long size, void *unsafe_ptr) =
(void *) BPF_FUNC_probe_read;
static unsigned long long (*bpf_ktime_get_ns)(void) =
(void *) BPF_FUNC_ktime_get_ns;
static int (*bpf_trace_printk)(const char *fmt, unsigned long long fmt_size, ...) =
(void *) BPF_FUNC_trace_printk;
/* llvm builtin functions that eBPF C program may use to
* emit BPF_LD_ABS and BPF_LD_IND instructions
......
......@@ -85,11 +85,17 @@ int BPFProgram::parse() {
proto_parser_ = make_unique<ebpf::cc::Parser>(proto_filename_);
rc = proto_parser_->parse();
if (rc) return rc;
if (rc) {
fprintf(stderr, "In file: %s\n", filename_.c_str());
return rc;
}
parser_ = make_unique<ebpf::cc::Parser>(filename_);
rc = parser_->parse();
if (rc) return rc;
if (rc) {
fprintf(stderr, "In file: %s\n", filename_.c_str());
return rc;
}
//ebpf::cc::Printer printer(stderr);
//printer.visit(parser_->root_node_);
......@@ -101,10 +107,7 @@ int BPFProgram::parse() {
exit(1);
}
codegen_ = ebpf::make_unique<ebpf::cc::CodegenLLVM>(mod_, parser_->scopes_.get(),
proto_parser_->scopes_.get(),
/*use_pre_header*/false,
parser_->pragma("name"));
codegen_ = ebpf::make_unique<ebpf::cc::CodegenLLVM>(mod_, parser_->scopes_.get(), proto_parser_->scopes_.get());
ret = codegen_->visit(parser_->root_node_);
if (get<0>(ret) != 0 || get<1>(ret).size()) {
fprintf(stderr, "Codegen error @line=%d: %s\n", get<0>(ret), get<1>(ret).c_str());
......@@ -182,7 +185,7 @@ int BPFProgram::finalize() {
}
uint8_t * BPFProgram::start(const string &name) const {
auto section = sections_.find(name);
auto section = sections_.find("." + name);
if (section == sections_.end())
return nullptr;
......@@ -190,7 +193,7 @@ uint8_t * BPFProgram::start(const string &name) const {
}
size_t BPFProgram::size(const string &name) const {
auto section = sections_.find(name);
auto section = sections_.find("." + name);
if (section == sections_.end())
return 0;
......
......@@ -53,8 +53,7 @@ class CodegenLLVM : public Visitor {
friend class BlockStack;
friend class SwitchStack;
public:
CodegenLLVM(llvm::Module *mod, Scopes *scopes, Scopes *proto_scopes,
bool use_pre_header, const std::string &section);
CodegenLLVM(llvm::Module *mod, Scopes *scopes, Scopes *proto_scopes);
virtual ~CodegenLLVM();
#define VISIT(type, func) virtual STATUS_RETURN visit_##func(type* n);
......@@ -94,25 +93,21 @@ class CodegenLLVM : public Visitor {
STATUS_RETURN emit_get_usec_time(MethodCallExprNode* n);
STATUS_RETURN emit_forward_to_vnf(MethodCallExprNode* n);
STATUS_RETURN emit_forward_to_group(MethodCallExprNode* n);
STATUS_RETURN print_parser();
STATUS_RETURN print_timer();
STATUS_RETURN print_header();
void indent();
llvm::LLVMContext & ctx() const;
llvm::Constant * const_int(uint64_t val, unsigned bits = 64, bool is_signed = false);
llvm::Value * pop_expr();
llvm::BasicBlock * resolve_label(const string &label);
llvm::Instruction * resolve_entry_stack();
StatusTuple lookup_var(Node *n, const std::string &name, Scopes::VarScope *scope,
VariableDeclStmtNode **decl, llvm::Value **mem) const;
StatusTuple lookup_struct_type(StructDeclStmtNode *decl, llvm::StructType **stype) const;
StatusTuple lookup_struct_type(VariableDeclStmtNode *n, llvm::StructType **stype,
StructDeclStmtNode **decl = nullptr) const;
template <typename... Args> void emitln(const char *fmt, Args&&... params);
template <typename... Args> void lnemit(const char *fmt, Args&&... params);
template <typename... Args> void emit(const char *fmt, Args&&... params);
void emitln(const char *s);
void lnemit(const char *s);
void emit(const char *s);
void emit_comment(Node* n);
FILE* out_;
llvm::Module* mod_;
......@@ -121,8 +116,6 @@ class CodegenLLVM : public Visitor {
int tmp_reg_index_;
Scopes *scopes_;
Scopes *proto_scopes_;
bool use_pre_header_;
std::string section_;
vector<vector<string> > free_instructions_;
vector<string> table_inits_;
map<string, string> proto_rewrites_;
......@@ -131,10 +124,10 @@ class CodegenLLVM : public Visitor {
map<VariableDeclStmtNode *, llvm::Value *> vars_;
map<StructDeclStmtNode *, llvm::StructType *> structs_;
map<string, llvm::BasicBlock *> labels_;
llvm::BasicBlock *entry_bb_;
llvm::SwitchInst *cur_switch_;
llvm::Value *expr_;
llvm::AllocaInst *retval_;
llvm::AllocaInst *errval_;
};
} // namespace cc
......
......@@ -82,7 +82,8 @@ class Lexer : public yyFlexLexer {
case Tok::TRBRACK:
case Tok::TTRUE:
case Tok::TFALSE:
return true;
// uncomment to add implicit semicolons
//return true;
default:
break;
}
......
......@@ -31,14 +31,14 @@ std::string tmp_str_cc;
%x STRING_
%%
\' {BEGIN STRING_;}
<STRING_>\' { BEGIN 0;
\" {BEGIN STRING_;}
<STRING_>\" { BEGIN 0;
yylval_->string = new std::string(tmp_str_cc);
tmp_str_cc = "";
return Tok::TSTRING;
}
<STRING_>\\n {tmp_str_cc += "\n"; }
<STRING_>. {tmp_str_cc += *yytext; }
<STRING_>\n {tmp_str_cc += "\n"; }
......@@ -59,10 +59,13 @@ std::string tmp_str_cc;
"}" return save(Tok::TRBRACE);
"[" return save(Tok::TLBRACK);
"]" return save(Tok::TRBRACK);
"->" return save(Tok::TARROW);
"." return save(Tok::TDOT);
"," return save(Tok::TCOMMA);
"+" return save(Tok::TPLUS);
"++" return save(Tok::TINCR);
"-" return save(Tok::TMINUS);
"--" return save(Tok::TDECR);
"*" return save(Tok::TMUL);
"/" return save(Tok::TDIV);
"%" return save(Tok::TMOD);
......@@ -79,30 +82,31 @@ std::string tmp_str_cc;
"|" return save(Tok::TLOR);
"@" return save(Tok::TAT);
"const" return save(Tok::TCONST);
"struct" return save(Tok::TSTRUCT);
"var" return save(Tok::TVAR);
"state" return save(Tok::TSTATE);
"timer" return save(Tok::TTIMER);
"goto" return save(Tok::TGOTO);
"case" return save(Tok::TCASE);
"continue" return save(Tok::TCONTINUE);
"else" return save(Tok::TELSE);
"false" return save(Tok::TFALSE);
"goto" return save(Tok::TGOTO);
"if" return save(Tok::TIF);
"next" return save(Tok::TNEXT);
"on_match" return save(Tok::TMATCH);
"on_miss" return save(Tok::TMISS);
"on_failure" return save(Tok::TFAILURE);
"on_valid" return save(Tok::TVALID);
"true" return save(Tok::TTRUE);
"false" return save(Tok::TFALSE);
"if" return save(Tok::TIF);
"else" return save(Tok::TELSE);
"switch" return save(Tok::TSWITCH);
"case" return save(Tok::TCASE);
"return" return save(Tok::TRETURN);
"state" return save(Tok::TSTATE);
"struct" return save(Tok::TSTRUCT);
"switch" return save(Tok::TSWITCH);
"true" return save(Tok::TTRUE);
"u8" return save(Tok::TU8);
"u16" return save(Tok::TU16);
"u32" return save(Tok::TU32);
"u64" return save(Tok::TU64);
[a-zA-Z][a-zA-Z0-9_]* return save(Tok::TIDENTIFIER);
[0-9]+ return save(Tok::TINTEGER);
0x[0-9a-fA-F]+ return save(Tok::THEXINTEGER);
. printf("Unknown token\n"); yyterminate();
. printf("Unknown token \"%s\"\n", yytext); yyterminate();
%%
/* eBPF mini library */
#include <stdlib.h>
#include <stdio.h>
#include <linux/unistd.h>
#include <unistd.h>
#include <string.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/bpf.h>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <libmnl/libmnl.h>
#include <linux/bpf.h>
#include <linux/if_packet.h>
#include <linux/pkt_cls.h>
#include <linux/perf_event.h>
#include <linux/rtnetlink.h>
#include <linux/unistd.h>
#include <linux/version.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <linux/if_packet.h>
#include <linux/pkt_sched.h>
#include <arpa/inet.h>
#include <libmnl/libmnl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include "libbpf.h"
static __u64 ptr_to_u64(void *ptr)
......@@ -95,9 +97,14 @@ int bpf_prog_load(enum bpf_prog_type prog_type,
.log_level = 1,
};
attr.kern_version = LINUX_VERSION_CODE;
bpf_log_buf[0] = 0;
return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
int ret = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
if (ret < 0) {
fprintf(stderr, "bpf: %s\n%s\n", strerror(errno), bpf_log_buf);
}
return ret;
}
int bpf_open_raw_sock(const char *name)
......@@ -144,8 +151,8 @@ static int cb(const struct nlmsghdr *nlh, void *data) {
}
}
int bpf_attach_filter(int progfd, const char *prog_name, uint32_t ifindex, uint8_t prio, uint32_t classid)
{
int bpf_attach_filter(int progfd, const char *prog_name,
uint32_t ifindex, uint8_t prio, uint32_t classid) {
int rc = -1;
char buf[1024];
struct nlmsghdr *nlh;
......@@ -169,9 +176,9 @@ int bpf_attach_filter(int progfd, const char *prog_name, uint32_t ifindex, uint8
tc->tcm_ifindex = ifindex;
mnl_attr_put_strz(nlh, TCA_KIND, "bpf");
opt = mnl_attr_nest_start(nlh, TCA_OPTIONS);
mnl_attr_put_u32(nlh, 6 /*TCA_BPF_FD*/, progfd);
mnl_attr_put_strz(nlh, 7 /*TCP_BPF_NAME*/, prog_name);
mnl_attr_put_u32(nlh, 3 /*TCA_BPF_CLASSID*/, classid);
mnl_attr_put_u32(nlh, TCA_BPF_FD, progfd);
mnl_attr_put_strz(nlh, TCA_BPF_NAME, prog_name);
mnl_attr_put_u32(nlh, TCA_BPF_CLASSID, classid);
mnl_attr_nest_end(nlh, opt);
nl = mnl_socket_open(NETLINK_ROUTE);
......@@ -209,3 +216,82 @@ cleanup:
perror("mnl_socket_close");
return rc;
}
static int bpf_attach_tracing_event(int progfd, const char *event_path, pid_t pid, int cpu, int group_fd)
{
int efd = -1, rc = -1, pfd = -1;
ssize_t bytes = -1;
char buf[256];
struct perf_event_attr attr = {};
snprintf(buf, sizeof(buf), "%s/id", event_path);
efd = open(buf, O_RDONLY, 0);
if (efd < 0) {
fprintf(stderr, "open(%s): %s\n", buf, strerror(errno));
goto cleanup;
}
bytes = read(efd, buf, sizeof(buf));
if (bytes <= 0 || bytes >= sizeof(buf)) {
fprintf(stderr, "read(%s): %s\n", buf, strerror(errno));
goto cleanup;
}
buf[bytes] = '\0';
attr.config = strtol(buf, NULL, 0);
attr.type = PERF_TYPE_TRACEPOINT;
attr.sample_type = PERF_SAMPLE_RAW;
attr.sample_period = 1;
attr.wakeup_events = 1;
pfd = syscall(__NR_perf_event_open, &attr, pid, cpu, group_fd, PERF_FLAG_FD_CLOEXEC);
if (pfd < 0) {
perror("perf_event_open");
goto cleanup;
}
if (ioctl(pfd, PERF_EVENT_IOC_SET_BPF, progfd) < 0) {
perror("ioctl(PERF_EVENT_IOC_SET_BPF)");
goto cleanup;
}
if (ioctl(pfd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
perror("ioctl(PERF_EVENT_IOC_ENABLE)");
goto cleanup;
}
rc = pfd;
pfd = -1;
cleanup:
if (efd >= 0)
close(efd);
if (pfd >= 0)
close(pfd);
return rc;
}
int bpf_attach_kprobe(int progfd, const char *event,
const char *event_desc, pid_t pid,
int cpu, int group_fd) {
int rc = -1, kfd = -1;
char buf[256];
kfd = open("/sys/kernel/debug/tracing/kprobe_events", O_WRONLY | O_APPEND, 0);
if (kfd < 0) {
perror("open(kprobe_events)");
goto cleanup;
}
if (write(kfd, event_desc, strlen(event_desc)) < 0) {
perror("write(kprobe_events)");
goto cleanup;
}
snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/events/kprobes/%s", event);
rc = bpf_attach_tracing_event(progfd, buf, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/);
cleanup:
if (kfd >= 0)
close(kfd);
return rc;
}
......@@ -67,12 +67,14 @@ typedef unique_ptr<string> String;
EXPAND(AssignExprNode, assign_expr_node) \
EXPAND(PacketExprNode, packet_expr_node) \
EXPAND(IntegerExprNode, integer_expr_node) \
EXPAND(StringExprNode, string_expr_node) \
EXPAND(BinopExprNode, binop_expr_node) \
EXPAND(UnopExprNode, unop_expr_node) \
EXPAND(BitopExprNode, bitop_expr_node) \
EXPAND(GotoExprNode, goto_expr_node) \
EXPAND(ReturnExprNode, return_expr_node) \
EXPAND(MethodCallExprNode, method_call_expr_node)
EXPAND(MethodCallExprNode, method_call_expr_node) \
EXPAND(TableIndexExprNode, table_index_expr_node)
#define NODE_STATEMENTS(EXPAND) \
EXPAND(ExprStmtNode, expr_stmt_node) \
......@@ -85,13 +87,12 @@ typedef unique_ptr<string> String;
EXPAND(IntegerVariableDeclStmtNode, integer_variable_decl_stmt_node) \
EXPAND(StructDeclStmtNode, struct_decl_stmt_node) \
EXPAND(StateDeclStmtNode, state_decl_stmt_node) \
EXPAND(TimerDeclStmtNode, timer_decl_stmt_node) \
EXPAND(ParserStateStmtNode, parser_state_stmt_node) \
EXPAND(MatchDeclStmtNode, match_decl_stmt_node) \
EXPAND(MissDeclStmtNode, miss_decl_stmt_node) \
EXPAND(FailureDeclStmtNode, failure_decl_stmt_node) \
EXPAND(TableDeclStmtNode, table_decl_stmt_node) \
EXPAND(VersionStmtNode, version_stmt_node)
EXPAND(FuncDeclStmtNode, func_decl_stmt_node)
#define EXPAND_NODES(EXPAND) \
NODE_EXPRESSIONS(EXPAND) \
......@@ -148,7 +149,7 @@ class ExprNode : public Node {
public:
typedef unique_ptr<ExprNode> Ptr;
virtual StatusTuple accept(Visitor* v) = 0;
enum expr_type { STRUCT, INTEGER, VOID, UNKNOWN };
enum expr_type { STRUCT, INTEGER, STRING, VOID, UNKNOWN };
enum prop_flag { READ = 0, WRITE, PROTO, IS_LHS, IS_REF, LAST };
expr_type typeof_;
StructDeclStmtNode *struct_type_;
......@@ -259,6 +260,17 @@ class PacketExprNode : public ExprNode {
explicit PacketExprNode(IdentExprNode::Ptr id) : id_(move(id)) {}
};
class StringExprNode : public ExprNode {
public:
DECLARE(StringExprNode)
string val_;
explicit StringExprNode(string *val) : val_(move(*val)) {
delete val;
}
explicit StringExprNode(const string &val) : val_(val) {}
};
class IntegerExprNode : public ExprNode {
public:
DECLARE(IntegerExprNode)
......@@ -318,25 +330,15 @@ class ReturnExprNode : public ExprNode {
: expr_(move(expr)) {}
};
class VersionStmtNode : public StmtNode {
public:
DECLARE(VersionStmtNode)
VersionStmtNode(const int major, const int minor, const int rev)
: major_(major), minor_(minor), rev_(rev) {}
uint32_t major_, minor_, rev_;
};
class BlockStmtNode : public StmtNode {
public:
DECLARE(BlockStmtNode)
explicit BlockStmtNode(StmtNodeList stmts = StmtNodeList())
: stmts_(move(stmts)), scope_(NULL), ver_(0, 0, 0) {}
: stmts_(move(stmts)), scope_(NULL) {}
~BlockStmtNode() { delete scope_; }
StmtNodeList stmts_;
Scopes::VarScope* scope_;
VersionStmtNode ver_;
};
class MethodCallExprNode : public ExprNode {
......@@ -352,6 +354,20 @@ class MethodCallExprNode : public ExprNode {
}
};
class TableIndexExprNode : public ExprNode {
public:
DECLARE(TableIndexExprNode)
IdentExprNode::Ptr id_;
IdentExprNode::Ptr sub_;
ExprNode::Ptr index_;
TableDeclStmtNode *table_;
VariableDeclStmtNode *sub_decl_;
TableIndexExprNode(IdentExprNode::Ptr id, ExprNode::Ptr index)
: id_(move(id)), index_(move(index)), table_(nullptr), sub_decl_(nullptr)
{}
};
class ExprStmtNode : public StmtNode {
public:
DECLARE(ExprStmtNode)
......@@ -532,16 +548,6 @@ class StateDeclStmtNode : public StmtNode {
}
};
class TimerDeclStmtNode : public StateDeclStmtNode {
public:
DECLARE(TimerDeclStmtNode)
BlockStmtNode::Ptr block_;
Scopes::StateScope* scope_;
explicit TimerDeclStmtNode(BlockStmtNode::Ptr block): block_(move(block)) {
}
};
class MatchDeclStmtNode : public StmtNode {
public:
DECLARE(MatchDeclStmtNode)
......@@ -582,6 +588,8 @@ class TableDeclStmtNode : public StmtNode {
IdentExprNode::Ptr table_type_;
IdentExprNodeList templates_;
IdentExprNode::Ptr id_;
StructDeclStmtNode *key_type_;
StructDeclStmtNode *leaf_type_;
IdentExprNode * key_id() { return templates_.at(0).get(); }
IdentExprNode * leaf_id() { return templates_.at(1).get(); }
IdentExprNode * type_id() { return templates_.at(2).get(); }
......@@ -590,11 +598,23 @@ class TableDeclStmtNode : public StmtNode {
TableDeclStmtNode(IdentExprNode::Ptr table_type, IdentExprNodeList&& templates,
IdentExprNode::Ptr id, string* size)
: table_type_(move(table_type)), templates_(move(templates)), id_(move(id)),
size_(strtoul(size->c_str(), NULL, 0)) {
key_type_(nullptr), leaf_type_(nullptr), size_(strtoul(size->c_str(), NULL, 0)) {
delete size;
}
};
class FuncDeclStmtNode : public StmtNode {
public:
DECLARE(FuncDeclStmtNode)
IdentExprNode::Ptr id_;
FormalList formals_;
BlockStmtNode::Ptr block_;
Scopes::StateScope* scope_;
FuncDeclStmtNode(IdentExprNode::Ptr id, FormalList&& formals, BlockStmtNode::Ptr block)
: id_(move(id)), formals_(move(formals)), block_(move(block)), scope_(NULL) {}
};
class Visitor {
public:
typedef StatusTuple Ret;
......
......@@ -30,47 +30,43 @@ using std::move;
using std::string;
using std::unique_ptr;
bool Parser::variable_exists(VariableDeclStmtNode* decl, bool search_local) {
if (scopes_->current_var()->lookup(decl->id_->name_, search_local) == NULL) {
bool Parser::variable_exists(VariableDeclStmtNode *decl) const {
if (scopes_->current_var()->lookup(decl->id_->name_, SCOPE_LOCAL) == NULL) {
return false;
}
return true;
}
VariableDeclStmtNode* Parser::variable_add(VariableDeclStmtNode* decl) {
VariableDeclStmtNode *Parser::variable_add(vector<int> *types, VariableDeclStmtNode *decl) {
if (variable_exists(decl, true)) {
if (variable_exists(decl)) {
fprintf(stderr, "redeclaration of variable %s", decl->id_->name_.c_str());
assert(0 && "redeclaration of variable");
// how to raise a bison error from here?
return decl;
return nullptr;
}
decl->scope_id_ = string("v") + std::to_string(scopes_->current_var()->id_) + string("_");
scopes_->current_var()->add(decl->id_->name_, decl);
return decl;
}
VariableDeclStmtNode* Parser::variable_add(VariableDeclStmtNode* decl, ExprNode* init_expr) {
VariableDeclStmtNode *Parser::variable_add(vector<int> *types, VariableDeclStmtNode *decl, ExprNode *init_expr) {
AssignExprNode::Ptr assign(new AssignExprNode(decl->id_->copy(), ExprNode::Ptr(init_expr)));
decl->init_.push_back(move(assign));
if (variable_exists(decl, true)) {
if (variable_exists(decl)) {
fprintf(stderr, "redeclaration of variable %s", decl->id_->name_.c_str());
assert(0 && "redeclaration of variable");
// how to raise a bison error from here?
return decl;
return nullptr;
}
decl->scope_id_ = string("v") + std::to_string(scopes_->current_var()->id_) + string("_");
scopes_->current_var()->add(decl->id_->name_, decl);
return decl;
}
StructVariableDeclStmtNode* Parser::variable_add(StructVariableDeclStmtNode* decl, ExprNodeList* args, bool is_kv) {
StructVariableDeclStmtNode *Parser::variable_add(StructVariableDeclStmtNode *decl, ExprNodeList *args, bool is_kv) {
if (is_kv) {
// annotate the init expressions with the declared id
for (auto arg = args->begin(); arg != args->end(); ++arg) {
// decorate with the name of this decl
auto n = static_cast<AssignExprNode*>(arg->get());
auto n = static_cast<AssignExprNode *>(arg->get());
n->id_->prepend_dot(decl->id_->name_);
}
} else {
......@@ -81,32 +77,17 @@ StructVariableDeclStmtNode* Parser::variable_add(StructVariableDeclStmtNode* dec
decl->init_ = move(*args);
delete args;
if (variable_exists(decl, true)) {
if (variable_exists(decl)) {
fprintf(stderr, "ccpg: warning: redeclaration of variable '%s'\n", decl->id_->name_.c_str());
// how to raise a bison error from here?
return decl;
return nullptr;
}
decl->scope_id_ = string("v") + std::to_string(scopes_->current_var()->id_) + string("_");
scopes_->current_var()->add(decl->id_->name_, decl);
return decl;
}
StmtNode* Parser::timer_add(Scopes::StateScope* scope, BlockStmtNode* body) {
if (scopes_->top_timer()->lookup("timer", true)) {
fprintf(stderr, "redeclaration of timer. Only one timer supported per plum");
assert(0 && "redeclaration of timer. Only one timer supported per plum");
}
auto timer = new TimerDeclStmtNode(BlockStmtNode::Ptr(body));
timer->scope_ = scope;
scopes_->top_timer()->add("timer", timer);
return timer;
}
StmtNode* Parser::state_add(Scopes::StateScope* scope, IdentExprNode* id, BlockStmtNode* body) {
if (scopes_->current_state()->lookup(id->full_name(), true)) {
StmtNode *Parser::state_add(Scopes::StateScope *scope, IdentExprNode *id, BlockStmtNode *body) {
if (scopes_->current_state()->lookup(id->full_name(), SCOPE_LOCAL)) {
fprintf(stderr, "redeclaration of state %s\n", id->full_name().c_str());
// redeclaration
return NULL;
......@@ -122,8 +103,8 @@ StmtNode* Parser::state_add(Scopes::StateScope* scope, IdentExprNode* id, BlockS
return state;
}
StmtNode* Parser::state_add(Scopes::StateScope* scope, IdentExprNode* id1, IdentExprNode* id2, BlockStmtNode* body) {
auto state = scopes_->current_state()->lookup(id1->full_name(), true);
StmtNode *Parser::state_add(Scopes::StateScope *scope, IdentExprNode *id1, IdentExprNode *id2, BlockStmtNode *body) {
auto state = scopes_->current_state()->lookup(id1->full_name(), SCOPE_LOCAL);
if (!state) {
state = new StateDeclStmtNode(IdentExprNode::Ptr(id1), IdentExprNode::Ptr(id2), BlockStmtNode::Ptr(body));
// add a reference to the lower scope
......@@ -146,15 +127,15 @@ StmtNode* Parser::state_add(Scopes::StateScope* scope, IdentExprNode* id1, Ident
}
}
bool Parser::table_exists(TableDeclStmtNode* decl, bool search_local) {
bool Parser::table_exists(TableDeclStmtNode *decl, bool search_local) {
if (scopes_->top_table()->lookup(decl->id_->name_, search_local) == NULL) {
return false;
}
return true;
}
StmtNode* Parser::table_add(IdentExprNode* type, IdentExprNodeList* templates,
IdentExprNode* id, string* size) {
StmtNode *Parser::table_add(IdentExprNode *type, IdentExprNodeList *templates,
IdentExprNode *id, string *size) {
auto table = new TableDeclStmtNode(IdentExprNode::Ptr(type),
move(*templates),
IdentExprNode::Ptr(id), size);
......@@ -166,9 +147,9 @@ StmtNode* Parser::table_add(IdentExprNode* type, IdentExprNodeList* templates,
return table;
}
StmtNode* Parser::struct_add(IdentExprNode* type, FormalList* formals) {
StmtNode * Parser::struct_add(IdentExprNode *type, FormalList *formals) {
auto struct_decl = new StructDeclStmtNode(IdentExprNode::Ptr(type), move(*formals));
if (scopes_->top_struct()->lookup(type->name_, true) != NULL) {
if (scopes_->top_struct()->lookup(type->name_, SCOPE_LOCAL) != NULL) {
fprintf(stderr, "redeclaration of struct %s\n", type->name_.c_str());
return struct_decl;
}
......@@ -192,11 +173,7 @@ StmtNode* Parser::struct_add(IdentExprNode* type, FormalList* formals) {
return struct_decl;
}
StmtNode* Parser::result_add(int token, IdentExprNode* id, FormalList* formals, BlockStmtNode* body) {
// result arguments are pass-by-reference instead of value
for (auto it = formals->begin(); it != formals->end(); ++it) {
(*it)->storage_type_ = VariableDeclStmtNode::STRUCT_REFERENCE;
}
StmtNode * Parser::result_add(int token, IdentExprNode *id, FormalList *formals, BlockStmtNode *body) {
StmtNode *stmt = NULL;
switch (token) {
case Tok::TMATCH:
......@@ -214,6 +191,24 @@ StmtNode* Parser::result_add(int token, IdentExprNode* id, FormalList* formals,
return stmt;
}
StmtNode * Parser::func_add(vector<int> *types, Scopes::StateScope *scope,
IdentExprNode *id, FormalList *formals, BlockStmtNode *body) {
auto decl = new FuncDeclStmtNode(IdentExprNode::Ptr(id), move(*formals), BlockStmtNode::Ptr(body));
if (scopes_->top_func()->lookup(decl->id_->name_, SCOPE_LOCAL)) {
fprintf(stderr, "redeclaration of func %s\n", id->name_.c_str());
return decl;
}
auto cur_scope = scopes_->current_var();
scopes_->set_current(scope);
for (auto it = formals->begin(); it != formals->end(); ++it)
if (!variable_add(nullptr, it->get()))
return nullptr;
scopes_->set_current(cur_scope);
decl->scope_ = scope;
scopes_->top_func()->add(id->name_, decl);
return decl;
}
void Parser::set_loc(Node *n, const BisonParser::location_type &loc) const {
n->line_ = loc.begin.line;
n->column_ = loc.begin.column;
......@@ -222,7 +217,7 @@ void Parser::set_loc(Node *n, const BisonParser::location_type &loc) const {
string Parser::pragma(const string &name) const {
auto it = pragmas_.find(name);
if (it == pragmas_.end()) return "";
if (it == pragmas_.end()) return "main";
return it->second;
}
......
......@@ -26,8 +26,9 @@
namespace ebpf {
namespace cc {
using std::string;
using std::pair;
using std::string;
using std::vector;
class Parser {
public:
......@@ -40,22 +41,23 @@ class Parser {
return parser.parse();
}
VariableDeclStmtNode* variable_add(VariableDeclStmtNode* decl);
VariableDeclStmtNode* variable_add(VariableDeclStmtNode* decl, ExprNode* init_expr);
StructVariableDeclStmtNode* variable_add(StructVariableDeclStmtNode* decl, ExprNodeList* args, bool is_kv);
StmtNode* state_add(Scopes::StateScope* scope, IdentExprNode* id1, BlockStmtNode* body);
StmtNode* timer_add(Scopes::StateScope* scope, BlockStmtNode* body);
StmtNode* state_add(Scopes::StateScope* scope, IdentExprNode* id1, IdentExprNode* id2, BlockStmtNode* body);
StmtNode* table_add(IdentExprNode* type, IdentExprNodeList* templates, IdentExprNode* id, string* size);
StmtNode* struct_add(IdentExprNode* type, FormalList* formals);
StmtNode* result_add(int token, IdentExprNode* id, FormalList* formals, BlockStmtNode* body);
bool variable_exists(VariableDeclStmtNode* decl, bool search_local = true);
bool table_exists(TableDeclStmtNode* decl, bool search_local = true);
VariableDeclStmtNode * variable_add(vector<int> *types, VariableDeclStmtNode *decl);
VariableDeclStmtNode * variable_add(vector<int> *types, VariableDeclStmtNode *decl, ExprNode *init_expr);
StructVariableDeclStmtNode * variable_add(StructVariableDeclStmtNode *decl, ExprNodeList *args, bool is_kv);
StmtNode * state_add(Scopes::StateScope *scope, IdentExprNode *id1, BlockStmtNode *body);
StmtNode * state_add(Scopes::StateScope *scope, IdentExprNode *id1, IdentExprNode *id2, BlockStmtNode *body);
StmtNode * func_add(std::vector<int> *types, Scopes::StateScope *scope,
IdentExprNode *id, FormalList *formals, BlockStmtNode *body);
StmtNode * table_add(IdentExprNode *type, IdentExprNodeList *templates, IdentExprNode *id, string *size);
StmtNode * struct_add(IdentExprNode *type, FormalList *formals);
StmtNode * result_add(int token, IdentExprNode *id, FormalList *formals, BlockStmtNode *body);
bool variable_exists(VariableDeclStmtNode *decl) const;
bool table_exists(TableDeclStmtNode *decl, bool search_local = true);
void add_pragma(const std::string& pr, const std::string& v) { pragmas_[pr] = v; }
void set_loc(Node *n, const BisonParser::location_type &loc) const;
std::string pragma(const std::string &name) const;
Node* root_node_;
Node *root_node_;
Scopes::Ptr scopes_;
std::map<std::string, std::string> pragmas_;
private:
......
......@@ -68,17 +68,19 @@
FormalList *formals;
VariableDeclStmtNode *decl;
StructVariableDeclStmtNode *type_decl;
TableIndexExprNode *table_index;
std::vector<int> *type_specifiers;
std::string* string;
int token;
VersionStmtNode *ver;
}
/* Define the terminal symbols. */
%token <string> TIDENTIFIER TINTEGER THEXINTEGER TPRAGMA TSTRING
%token <token> TU8 TU16 TU32 TU64
%token <token> TEQUAL TCEQ TCNE TCLT TCLE TCGT TCGE TAND TOR
%token <token> TLPAREN TRPAREN TLBRACE TRBRACE TLBRACK TRBRACK
%token <token> TDOT TCOMMA TPLUS TMINUS TMUL TDIV TMOD TXOR TDOLLAR TCOLON TSCOPE TNOT TSEMI TCMPL TLAND TLOR
%token <token> TCONST TSTRUCT TVAR TSTATE TTIMER TGOTO TCONTINUE TNEXT TTRUE TFALSE TRETURN
%token <token> TDOT TARROW TCOMMA TPLUS TMINUS TMUL TDIV TMOD TXOR TDOLLAR TCOLON TSCOPE TNOT TSEMI TCMPL TLAND TLOR
%token <token> TSTRUCT TSTATE TFUNC TGOTO TCONTINUE TNEXT TTRUE TFALSE TRETURN
%token <token> TIF TELSE TSWITCH TCASE
%token <token> TMATCH TMISS TFAILURE TVALID
%token <token> TAT
......@@ -88,24 +90,26 @@
%type <expr> expr assign_expr return_expr init_arg_kv
%type <numeric> numeric
%type <bitop> bitop
%type <args> call_args init_args init_args_kv
%type <args> call_args /*init_args*/ init_args_kv
%type <ident_args> table_decl_args
%type <formals> struct_decl_stmts formals
%type <block> program block prog_decls
%type <decl> decl_stmt int_decl ref_stmt
%type <type_decl> type_decl ptr_decl
%type <stmt> stmt prog_decl var_decl struct_decl state_decl timer_decl
%type <stmt> stmt prog_decl var_decl struct_decl state_decl func_decl
%type <stmt> table_decl table_result_stmt if_stmt switch_stmt case_stmt onvalid_stmt
%type <var_scope> enter_varscope exit_varscope
%type <state_scope> enter_statescope exit_statescope
%type <stmts> stmts table_result_stmts case_stmts
%type <call> call_expr
%type <table_index> table_index_expr
%type <type_specifiers> type_specifiers
%type <stmt> pragma_decl
%type <ver> version_stmt
%type <token> type_specifier
/* taken from C++ operator precedence wiki page */
%nonassoc TSCOPE
%left TDOT TLBRACK TLBRACE TLPAREN
%left TDOT TLBRACK TLBRACE TLPAREN TINCR TDECR
%right TNOT TCMPL
%left TMUL
%left TDIV
......@@ -127,17 +131,10 @@
%%
program
: enter_statescope enter_varscope version_stmt prog_decls exit_varscope exit_statescope
{ parser.root_node_ = $4; $4->scope_ = $2; $4->ver_ = (*$3); delete $3;}
: enter_statescope enter_varscope prog_decls exit_varscope exit_statescope
{ parser.root_node_ = $3; $3->scope_ = $2; }
;
version_stmt
: TAT TINTEGER TDOT TINTEGER TDOT TINTEGER TSEMI
{
$$ = new VersionStmtNode(atoi($2->c_str()), atoi($4->c_str()), atoi($6->c_str()));
}
;
/* program is a list of declarations */
prog_decls
: prog_decl
......@@ -156,10 +153,10 @@ prog_decls
prog_decl
: var_decl TSEMI
| struct_decl TSEMI
| state_decl TSEMI
| timer_decl TSEMI
| state_decl
| table_decl TSEMI
| pragma_decl TSEMI
| pragma_decl
| func_decl
;
pragma_decl
......@@ -194,12 +191,12 @@ stmt
| call_expr TLBRACE TRBRACE TSEMI // support empty curly braces
{ $$ = new ExprStmtNode(ExprNode::Ptr($1));
parser.set_loc($$, @$); }
| if_stmt TSEMI
| switch_stmt TSEMI
| if_stmt
| switch_stmt
| var_decl TSEMI
{ $$ = $1; }
| state_decl TSEMI
| onvalid_stmt TSEMI
| state_decl
| onvalid_stmt
;
call_expr
......@@ -229,10 +226,10 @@ struct_decl
;
struct_decl_stmts
: decl_stmt TSEMI
{ $$ = new FormalList; $$->push_back(VariableDeclStmtNode::Ptr($1)); }
| struct_decl_stmts decl_stmt TSEMI
{ $1->push_back(VariableDeclStmtNode::Ptr($2)); }
: type_specifiers decl_stmt TSEMI
{ $$ = new FormalList; $$->push_back(VariableDeclStmtNode::Ptr($2)); }
| struct_decl_stmts type_specifiers decl_stmt TSEMI
{ $1->push_back(VariableDeclStmtNode::Ptr($3)); }
;
table_decl
......@@ -251,18 +248,22 @@ table_decl_args
state_decl
: TSTATE scoped_ident enter_statescope enter_varscope block exit_varscope exit_statescope
{ $$ = parser.state_add($3, $2, $5); $5->scope_ = $4;
if (!$$) YYERROR;
parser.set_loc($$, @$); }
| TSTATE scoped_ident TCOMMA TMUL enter_statescope enter_varscope block exit_varscope exit_statescope
{ $$ = parser.state_add($5, $2, new IdentExprNode(""), $7); $7->scope_ = $6;
if (!$$) YYERROR;
parser.set_loc($$, @$); }
| TSTATE scoped_ident TCOMMA scoped_ident enter_statescope enter_varscope block exit_varscope exit_statescope
{ $$ = parser.state_add($5, $2, $4, $7); $7->scope_ = $6;
if (!$$) YYERROR;
parser.set_loc($$, @$); }
;
timer_decl
: TTIMER enter_statescope enter_varscope block exit_varscope exit_statescope
{ $$ = parser.timer_add($2, $4); $4->scope_ = $3;
func_decl
: type_specifiers ident enter_statescope enter_varscope TLPAREN formals TRPAREN block exit_varscope exit_statescope
{ $$ = parser.func_add($1, $3, $2, $6, $8); $8->scope_ = $4;
if (!$$) YYERROR;
parser.set_loc($$, @$); }
;
......@@ -276,37 +277,56 @@ table_result_stmts
table_result_stmt
: TMATCH ident enter_varscope TLPAREN formals TRPAREN block exit_varscope TSEMI
{ $$ = parser.result_add($1, $2, $5, $7); delete $5; $7->scope_ = $3;
if (!$$) YYERROR;
parser.set_loc($$, @$); }
| TMISS ident enter_varscope TLPAREN TRPAREN block exit_varscope TSEMI
{ $$ = parser.result_add($1, $2, new FormalList, $6); $6->scope_ = $3;
if (!$$) YYERROR;
parser.set_loc($$, @$); }
| TFAILURE ident enter_varscope TLPAREN formals TRPAREN block exit_varscope TSEMI
{ $$ = parser.result_add($1, $2, $5, $7); delete $5; $7->scope_ = $3;
if (!$$) YYERROR;
parser.set_loc($$, @$); }
;
formals
: TVAR ptr_decl
{ $$ = new FormalList; $$->push_back(VariableDeclStmtNode::Ptr(parser.variable_add($2))); }
| formals TCOMMA TVAR ptr_decl
{ $1->push_back(VariableDeclStmtNode::Ptr(parser.variable_add($4))); }
: TSTRUCT ptr_decl
{ $$ = new FormalList; $$->push_back(VariableDeclStmtNode::Ptr(parser.variable_add(nullptr, $2))); }
| formals TCOMMA TSTRUCT ptr_decl
{ $1->push_back(VariableDeclStmtNode::Ptr(parser.variable_add(nullptr, $4))); }
;
type_specifier
: TU8
| TU16
| TU32
| TU64
;
type_specifiers
: type_specifier { $$ = new std::vector<int>; $$->push_back($1); }
| type_specifiers type_specifier { $$->push_back($2); }
;
var_decl
: TVAR decl_stmt
{ $$ = parser.variable_add($2);
: type_specifiers decl_stmt
{ $$ = parser.variable_add($1, $2);
if (!$$) YYERROR;
parser.set_loc($$, @$); }
| TVAR int_decl TEQUAL expr
{ $$ = parser.variable_add($2, $4);
| type_specifiers int_decl TEQUAL expr
{ $$ = parser.variable_add($1, $2, $4);
if (!$$) YYERROR;
parser.set_loc($$, @$); }
| TVAR type_decl TLBRACE init_args_kv TRBRACE
{ $$ = parser.variable_add($2, $4, true);
| TSTRUCT type_decl TEQUAL TLBRACE init_args_kv TRBRACE
{ $$ = parser.variable_add($2, $5, true);
if (!$$) YYERROR;
parser.set_loc($$, @$); }
/*| TVAR type_decl TLBRACE init_args TRBRACE
{ $$ = parser.variable_add($2, $4, false);
/*| TSTRUCT type_decl TEQUAL TLBRACE init_args TRBRACE
{ $$ = parser.variable_add($2, $5, false);
parser.set_loc($$, @$); }*/
| TVAR ref_stmt
{ $$ = parser.variable_add($2);
| TSTRUCT ref_stmt
{ $$ = parser.variable_add(nullptr, $2);
if (!$$) YYERROR;
parser.set_loc($$, @$); }
;
......@@ -331,10 +351,10 @@ ptr_decl : scoped_ident TMUL ident
;
/* normal initializer */
init_args
/* init_args
: expr { $$ = new ExprNodeList; $$->push_back(ExprNode::Ptr($1)); }
| init_args TCOMMA expr { $$->push_back(ExprNode::Ptr($3)); }
;
;*/
/* one or more of "field" = "expr" */
init_args_kv
......@@ -342,11 +362,11 @@ init_args_kv
| init_args_kv TCOMMA init_arg_kv { $$->push_back(ExprNode::Ptr($3)); }
;
init_arg_kv
: ident TEQUAL expr
{ $$ = new AssignExprNode(IdentExprNode::Ptr($1), ExprNode::Ptr($3));
: TDOT ident TEQUAL expr
{ $$ = new AssignExprNode(IdentExprNode::Ptr($2), ExprNode::Ptr($4));
parser.set_loc($$, @$); }
| ident bitop TEQUAL expr
{ $$ = new AssignExprNode(IdentExprNode::Ptr($1), ExprNode::Ptr($4)); $$->bitop_ = BitopExprNode::Ptr($2);
| TDOT ident bitop TEQUAL expr
{ $$ = new AssignExprNode(IdentExprNode::Ptr($2), ExprNode::Ptr($5)); $$->bitop_ = BitopExprNode::Ptr($3);
parser.set_loc($$, @$); }
;
......@@ -439,6 +459,10 @@ expr
{ $$ = $1; }
| call_expr bitop
{ $$ = $1; $$->bitop_ = BitopExprNode::Ptr($2); }
| table_index_expr
{ $$ = $1; }
| table_index_expr TDOT ident
{ $$ = $1; $1->sub_ = IdentExprNode::Ptr($3); }
| any_ident
{ $$ = $1; }
| TAT dotted_ident
......@@ -464,6 +488,9 @@ expr
{ $$ = $2; }
| TLPAREN expr TRPAREN bitop
{ $$ = $2; $$->bitop_ = BitopExprNode::Ptr($4); }
| TSTRING
{ $$ = new StringExprNode($1);
parser.set_loc($$, @$); }
| numeric
{ $$ = $1; }
| numeric bitop
......@@ -544,6 +571,12 @@ bitop
parser.set_loc($$, @$); }
;
table_index_expr
: dotted_ident TLBRACK ident TRBRACK
{ $$ = new TableIndexExprNode(IdentExprNode::Ptr($1), IdentExprNode::Ptr($3));
parser.set_loc($$, @$); }
;
scoped_ident
: ident
{ $$ = $1; }
......@@ -561,6 +594,8 @@ dotted_ident
any_ident
: ident
{ $$ = $1; }
| dotted_ident TARROW TIDENTIFIER
{ $$->append_dot(*$3); delete $3; }
| dotted_ident TDOT TIDENTIFIER
{ $$->append_dot(*$3); delete $3; }
| scoped_ident TSCOPE TIDENTIFIER
......
......@@ -30,7 +30,6 @@ void Printer::print_indent() {
StatusTuple Printer::visit_block_stmt_node(BlockStmtNode* n) {
fprintf(out_, "{\n");
TRY2(n->ver_.accept(this));
if (!n->stmts_.empty()) {
++indent_;
for (auto it = n->stmts_.begin(); it != n->stmts_.end(); ++it) {
......@@ -44,14 +43,6 @@ StatusTuple Printer::visit_block_stmt_node(BlockStmtNode* n) {
return mkstatus(0);
}
StatusTuple Printer::visit_version_stmt_node(VersionStmtNode* n) {
uint32_t version;
version = MAKE_VERSION(n->major_, n->minor_, n->rev_);
fprintf(out_, "static const uint32_t plumlet_version __attribute__"
"((section (\".version\"), used)) = 0x%x;\n", version);
return mkstatus(0);
}
StatusTuple Printer::visit_if_stmt_node(IfStmtNode* n) {
fprintf(out_, "if ");
TRY2(n->cond_->accept(this));
......@@ -124,6 +115,11 @@ StatusTuple Printer::visit_integer_expr_node(IntegerExprNode* n) {
return mkstatus(0);
}
StatusTuple Printer::visit_string_expr_node(StringExprNode *n) {
fprintf(out_, "%s", n->val_.c_str());
return mkstatus(0);
}
StatusTuple Printer::visit_binop_expr_node(BinopExprNode* n) {
TRY2(n->lhs_->accept(this));
fprintf(out_, "%d", n->op_);
......@@ -186,6 +182,13 @@ StatusTuple Printer::visit_method_call_expr_node(MethodCallExprNode* n) {
return mkstatus(0);
}
StatusTuple Printer::visit_table_index_expr_node(TableIndexExprNode *n) {
fprintf(out_, "%s[", n->id_->c_str());
TRY2(n->index_->accept(this));
fprintf(out_, "]");
return mkstatus(0);
}
StatusTuple Printer::visit_expr_stmt_node(ExprStmtNode* n) {
TRY2(n->expr_->accept(this));
return mkstatus(0);
......@@ -235,15 +238,6 @@ StatusTuple Printer::visit_struct_decl_stmt_node(StructDeclStmtNode* n) {
return mkstatus(0);
}
StatusTuple Printer::visit_timer_decl_stmt_node(TimerDeclStmtNode* n) {
if (!n->id_) {
return mkstatus(0);
}
fprintf(out_, "timer ");
TRY2(n->id_->accept(this));
return mkstatus(0);
}
StatusTuple Printer::visit_state_decl_stmt_node(StateDeclStmtNode* n) {
if (!n->id_) {
return mkstatus(0);
......@@ -324,5 +318,20 @@ StatusTuple Printer::visit_table_decl_stmt_node(TableDeclStmtNode* n) {
return mkstatus(0);
}
StatusTuple Printer::visit_func_decl_stmt_node(FuncDeclStmtNode *n) {
fprintf(out_, "func ");
TRY2(n->id_->accept(this));
fprintf(out_, "(");
for (auto it = n->formals_.begin(); it != n->formals_.end(); ++it) {
TRY2((*it)->accept(this));
if (it + 1 != n->formals_.end()) {
fprintf(out_, ", ");
}
}
fprintf(out_, ") ");
TRY2(n->block_->accept(this));
return mkstatus(0);
}
} // namespace cc
} // namespace ebpf
......@@ -33,28 +33,30 @@ using std::pair;
using std::unique_ptr;
class StateDeclStmtNode;
class TimerDeclStmtNode;
class VariableDeclStmtNode;
class TableDeclStmtNode;
class StructDeclStmtNode;
class FuncDeclStmtNode;
enum search_type { SCOPE_LOCAL, SCOPE_GLOBAL };
template <typename T>
class Scope {
public:
Scope() {}
Scope(Scope<T>* scope, int id) : parent_(scope), id_(id) {}
enum search_type { LOCAL, GLOBAL };
T* lookup(const string& name, bool search_local = true) {
T* lookup(const string &name, bool search_local = true) {
return lookup(name, search_local ? SCOPE_LOCAL : SCOPE_GLOBAL);
}
T * lookup(const string &name, search_type stype) {
auto it = elems_.find(name);
if (it != elems_.end()) {
if (it != elems_.end())
return it->second;
}
if (search_local || !parent_) {
return NULL;
}
return parent_->lookup(name, search_local);
if (stype == SCOPE_LOCAL || !parent_)
return nullptr;
return parent_->lookup(name, stype);
}
void add(const string& name, T* n) {
elems_[name] = n;
......@@ -80,23 +82,51 @@ class Scopes {
typedef unique_ptr<Scopes> Ptr;
typedef Scope<StructDeclStmtNode> StructScope;
typedef Scope<StateDeclStmtNode> StateScope;
typedef Scope<TimerDeclStmtNode> TimerScope;
typedef Scope<VariableDeclStmtNode> VarScope;
typedef Scope<TableDeclStmtNode> TableScope;
typedef Scope<FuncDeclStmtNode> FuncScope;
Scopes() : var_id__(0), state_id_(0), var_id_(0),
current_var_scope_(NULL), top_var_scope_(NULL),
current_state_scope_(NULL), top_state_scope_(NULL),
top_timer_scope_(new TimerScope(NULL, 1)),
top_struct_scope_(new StructScope(NULL, 1)),
top_table_scope_(new TableScope(NULL, 1)) {}
current_var_scope_(nullptr), top_var_scope_(nullptr),
current_state_scope_(nullptr), top_state_scope_(nullptr),
top_struct_scope_(new StructScope(nullptr, 1)),
top_table_scope_(new TableScope(nullptr, 1)),
top_func_scope_(new FuncScope(nullptr, 1)) {}
~Scopes() {
delete top_timer_scope_;
delete top_func_scope_;
delete top_struct_scope_;
delete top_table_scope_;
delete top_state_scope_;
}
void push_var(VarScope *scope) {
if (scope == top_var_scope_)
return;
scope->parent_ = current_var_scope_;
current_var_scope_ = scope;
}
void pop_var() {
if (current_var_scope_ == top_var_scope_)
return;
VarScope *old = current_var_scope_;
current_var_scope_ = old->parent_;
old->parent_ = nullptr;
}
void push_state(StateScope *scope) {
if (scope == top_state_scope_)
return;
scope->parent_ = current_state_scope_;
current_state_scope_ = scope;
}
void pop_state() {
if (current_state_scope_ == top_state_scope_)
return;
StateScope *old = current_state_scope_;
current_state_scope_ = old->parent_;
old->parent_ = nullptr;
}
/// While building the AST, allocate a new scope
VarScope* enter_var_scope() {
current_var_scope_ = new VarScope(current_var_scope_, next_var_id());
......@@ -125,18 +155,17 @@ class Scopes {
}
void set_current(VarScope* s) { current_var_scope_ = s; }
VarScope* current_var() { return current_var_scope_; }
VarScope* top_var() { return top_var_scope_; }
VarScope* current_var() const { return current_var_scope_; }
VarScope* top_var() const { return top_var_scope_; }
void set_current(StateScope* s) { current_state_scope_ = s; }
StateScope* current_state() { return current_state_scope_; }
StateScope* top_state() { return top_state_scope_; }
TimerScope* top_timer() { return top_timer_scope_; }
StateScope* current_state() const { return current_state_scope_; }
StateScope* top_state() const { return top_state_scope_; }
StructScope* top_struct() { return top_struct_scope_; }
StructScope* top_struct() const { return top_struct_scope_; }
TableScope* top_table() { return top_table_scope_; }
TableScope* top_table() const { return top_table_scope_; }
FuncScope* top_func() const { return top_func_scope_; }
int next_id() { return ++var_id__; }
int next_state_id() { return ++state_id_; }
......@@ -149,9 +178,9 @@ class Scopes {
VarScope* top_var_scope_;
StateScope* current_state_scope_;
StateScope* top_state_scope_;
TimerScope* top_timer_scope_;
StructScope* top_struct_scope_;
TableScope* top_table_scope_;
FuncScope* top_func_scope_;
};
} // namespace cc
......
......@@ -118,6 +118,7 @@ enum bpf_map_type {
enum bpf_prog_type {
BPF_PROG_TYPE_UNSPEC,
BPF_PROG_TYPE_SOCKET_FILTER,
BPF_PROG_TYPE_KPROBE,
BPF_PROG_TYPE_SCHED_CLS,
BPF_PROG_TYPE_SCHED_ACT,
};
......@@ -155,6 +156,7 @@ union bpf_attr {
__u32 log_level; /* verbosity level of verifier */
__u32 log_size; /* size of user buffer */
__aligned_u64 log_buf; /* user supplied buffer */
__u32 kern_version; /* checked when prog_type=kprobe */
};
} __attribute__((aligned(8)));
......@@ -166,13 +168,16 @@ enum bpf_func_id {
BPF_FUNC_map_lookup_elem, /* void *map_lookup_elem(&map, &key) */
BPF_FUNC_map_update_elem, /* int map_update_elem(&map, &key, &value, flags) */
BPF_FUNC_map_delete_elem, /* int map_delete_elem(&map, &key) */
BPF_FUNC_probe_read, /* int bpf_probe_read(void *dst, int size, void *src) */
BPF_FUNC_ktime_get_ns, /* u64 bpf_ktime_get_ns(void) */
BPF_FUNC_trace_printk, /* int bpf_trace_printk(const char *fmt, int fmt_size, ...) */
BPF_FUNC_get_prandom_u32, /* u32 prandom_u32(void) */
BPF_FUNC_get_smp_processor_id, /* u32 raw_smp_processor_id(void) */
/**
* skb_store_bytes(skb, offset, from, len, flags) - store bytes into packet
* @skb: pointer to skb
* @offset: offset within packet from skb->data
* @offset: offset within packet from skb->mac_header
* @from: pointer where to copy bytes from
* @len: number of bytes to store into packet
* @flags: bit 0 - if true, recompute skb->csum
......
#ifndef __LINUX_BPF_COMMON_H__
#define __LINUX_BPF_COMMON_H__
#ifndef _UAPI__LINUX_BPF_COMMON_H__
#define _UAPI__LINUX_BPF_COMMON_H__
/* Instruction classes */
#define BPF_CLASS(code) ((code) & 0x07)
......@@ -52,4 +52,4 @@
#define BPF_MAXINSNS 4096
#endif
#endif /* __LINUX_BPF_COMMON_H__ */
#endif /* _UAPI__LINUX_BPF_COMMON_H__ */
......@@ -27,6 +27,8 @@ int bpf_attach_filter(int progfd, const char *prog_name, uint32_t ifindex,
/* create RAW socket and bind to interface 'name' */
int bpf_open_raw_sock(const char *name);
int bpf_attach_kprobe(int progfd, const char *event, const char *event_desc, pid_t pid, int cpu, int group_fd);
#define LOG_BUF_SIZE 65536
extern char bpf_log_buf[LOG_BUF_SIZE];
......
configure_file(wrapper.sh.in "${CMAKE_CURRENT_BINARY_DIR}/wrapper.sh" @ONLY)
set(TEST_WRAPPER ${CMAKE_CURRENT_BINARY_DIR}/wrapper.sh)
add_subdirectory(jit)
add_test(NAME py_test1 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${TEST_WRAPPER} py_test1 ${CMAKE_CURRENT_SOURCE_DIR}/test1.py namespace)
add_test(NAME py_test2 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${TEST_WRAPPER} py_test2 ${CMAKE_CURRENT_SOURCE_DIR}/test2.py namespace)
add_test(NAME py_trace1 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${TEST_WRAPPER} py_trace1 ${CMAKE_CURRENT_SOURCE_DIR}/trace1.py sudo)
add_test(NAME py_trace2 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${TEST_WRAPPER} py_trace2 ${CMAKE_CURRENT_SOURCE_DIR}/trace2.py sudo)
#packed "false"
struct pt_regs {
u64 r15:64;
u64 r14:64;
u64 r13:64;
u64 r12:64;
u64 bp:64;
u64 bx:64;
u64 r11:64;
u64 r10:64;
u64 r9:64;
u64 r8:64;
u64 ax:64;
u64 cx:64;
u64 dx:64;
u64 si:64;
u64 di:64;
};
#!/usr/bin/env python
import sys
from src.bpf import BPF
from bpf import BPF
prog = BPF(sys.argv[1], sys.argv[2], sys.argv[3],
prog_type=int(sys.argv[4]), debug=int(sys.argv[5]))
#packed "true"
struct skbuff {
u32 len:32;
u32 pkt_type:32;
u32 mark:32;
u32 queue_mapping:32;
u32 protocol:32;
u32 vlan_present:32;
u32 vlan_tci:32;
u32 vlan_proto:32;
u32 priority:32;
};
struct ethernet {
u64 dst:48;
u64 src:48;
u32 type:16;
};
state ethernet {
switch $ethernet.type {
case 0x0800 {
next proto::ip;
};
case 0x8100 {
next proto::dot1q;
};
case * {
goto EOP;
};
}
}
struct dot1q {
u32 pri:3;
u32 cfi:1;
u32 vlanid:12;
u32 type:16;
};
state dot1q {
switch $dot1q.type {
case 0x0800 {
next proto::ip;
};
case * {
goto EOP;
};
}
}
struct ip {
u32 ver:4;
u32 hlen:4;
u32 tos:8;
u32 tlen:16;
u32 identification:16;
u32 ffo_unused:1;
u32 df:1;
u32 mf:1;
u32 foffset:13;
u32 ttl:8;
u32 nextp:8;
u32 hchecksum:16;
u32 src:32;
u32 dst:32;
};
state ip {
switch $ip.nextp {
case 6 {
next proto::tcp;
};
case 17 {
next proto::udp;
};
case 47 {
next proto::gre;
};
case * {
goto EOP;
};
}
}
struct udp {
u32 sport:16;
u32 dport:16;
u32 length:16;
u32 crc:16;
};
state udp {
switch $udp.dport {
case 8472 {
next proto::vxlan;
};
case * {
goto EOP;
};
}
}
struct tcp {
u16 src_port:16;
u16 dst_port:16;
u32 seq_num:32;
u32 ack_num:32;
u8 offset:4;
u8 reserved:4;
u8 flag_cwr:1;
u8 flag_ece:1;
u8 flag_urg:1;
u8 flag_ack:1;
u8 flag_psh:1;
u8 flag_rst:1;
u8 flag_syn:1;
u8 flag_fin:1;
u16 rcv_wnd:16;
u16 cksum:16;
u16 urg_ptr:16;
};
state tcp {
goto EOP;
}
struct vxlan {
u32 rsv1:4;
u32 iflag:1;
u32 rsv2:3;
u32 rsv3:24;
u32 key:24;
u32 rsv4:8;
};
state vxlan {
goto EOP;
}
struct gre {
u32 cflag:1;
u32 rflag:1;
u32 kflag:1;
u32 snflag:1;
u32 srflag:1;
u32 recurflag:3;
u32 reserved:5;
u32 vflag:3;
u32 protocol:16;
u32 key:32;
};
state gre {
switch $gre.protocol {
case * {
goto EOP;
};
}
}
@1.0.0
#packed 'true'
struct ethernet {
dst:48
src:48
type:16
}
state ethernet {
switch $ethernet.type {
case 0x0800 {
next proto::ip
}
case 0x8100 {
next proto::dot1q
}
case * {
goto EOP
}
}
}
struct dot1q {
pri:3
cfi:1
vlanid:12
type:16
}
state dot1q {
switch $dot1q.type {
case 0x0800 {
next proto::ip
}
case * {
goto EOP
}
}
}
struct ip {
ver:4
hlen:4
tos:8
tlen:16
identification:16
ffo_unused:1
df:1
mf:1
foffset:13
ttl:8
nextp:8
hchecksum:16
src:32
dst:32
}
state ip {
switch $ip.nextp {
case 6 {
next proto::tcp
}
case 17 {
next proto::udp
}
case 47 {
next proto::gre
}
case * {
goto EOP
}
}
}
struct udp {
sport:16
dport:16
length:16
crc:16
}
state udp {
switch $udp.dport {
case 8472 {
next proto::vxlan
}
case * {
goto EOP
}
}
}
struct tcp {
src_port:16
dst_port:16
seq_num:32
ack_num:32
offset:4
reserved:4
flag_cwr:1
flag_ece:1
flag_urg:1
flag_ack:1
flag_psh:1
flag_rst:1
flag_syn:1
flag_fin:1
rcv_wnd:16
cksum:16
urg_ptr:16
}
state tcp {
goto EOP
}
struct vxlan {
rsv1:4
iflag:1
rsv2:3
rsv3:24
key:24
rsv4:8
}
state vxlan {
goto EOP
}
struct gre {
cflag:1
rflag:1
kflag:1
snflag:1
srflag:1
recurflag:3
reserved:5
vflag:3
protocol:16
key:32
}
state gre {
switch $gre.protocol {
case * {
goto EOP
}
}
}
@1.0.0
#packed 'true'
struct ethernet {
dst:48
src:48
type:16
}
state ethernet {
switch $ethernet.type {
case * {
goto EOP
}
}
}
#packed "false"
struct IPKey {
u32 dip:32;
u32 sip:32;
};
struct IPLeaf {
u32 rx_pkts:64;
u32 tx_pkts:64;
};
Table<IPKey, IPLeaf, FIXED_MATCH, AUTO> stats(1024);
u32 main(struct proto::skbuff *skb) {
u32 ret:32 = 0;
goto proto::ethernet;
state proto::ethernet {
}
state proto::dot1q {
}
state proto::ip {
u32 rx:32 = 0;
u32 tx:32 = 0;
u32 IPKey key;
if $ip.dst > $ip.src {
key.dip = $ip.dst;
key.sip = $ip.src;
rx = 1;
// test arbitrary return stmt
if false {
return 3;
}
} else {
key.dip = $ip.src;
key.sip = $ip.dst;
tx = 1;
ret = 1;
}
struct IPLeaf *leaf;
leaf = stats[key];
on_valid(leaf) {
atomic_add(leaf.rx_pkts, rx);
atomic_add(leaf.tx_pkts, tx);
}
}
state proto::udp {
}
state proto::vxlan {
}
state proto::gre {
}
state EOP {
return ret;
}
}
@1.0.0
#name test1
struct SomeKey {
foo:3
foo1:9
foo2:63
}
struct SomeLeaf {
bar:64
}
Table<SomeKey, SomeLeaf, FIXED_MATCH, LRU> stats(8)
state INIT {
goto proto::ethernet
}
var ret:32 = 0
state proto::ethernet {
var SomeKey sk2{foo = 2}
stats.lookup(sk2) {
on_match stats (var SomeLeaf *l21) {
atomic_add(l21.bar, 1)
}
on_miss stats () {
ret = 1
}
}
var SomeKey sk3{foo = 3}
stats.lookup(sk3) {
}
var SomeKey sk4{foo = 4}
var SomeLeaf sl4{bar = 1}
stats.update(sk4, sl4) { }
var SomeKey sk5{foo = 5}
var SomeLeaf sl5{bar = 1}
stats.update(sk5, sl5) {
on_failure stats (var SomeKey *k21) {}
}
}
state EOP {
return ret
}
#!/usr/bin/env python
# test program to count the packets sent to a device in a .5
# second period
from ctypes import c_uint, c_ulong, Structure
from netaddr import IPAddress
from bpf import BPF
from subprocess import check_call
from unittest import main, TestCase
class Key(Structure):
_fields_ = [("dip", c_uint),
("sip", c_uint)]
class Leaf(Structure):
_fields_ = [("rx_pkts", c_ulong),
("tx_pkts", c_ulong)]
class TestBPFSocket(TestCase):
def setUp(self):
self.prog = BPF("main", "test1.b", "proto.b", debug=0)
self.prog.attach("eth0")
self.stats = self.prog.table("stats", Key, Leaf)
def test_ping(self):
cmd = ["ping", "-f", "-c", "100", "172.16.1.1"]
check_call(cmd)
#for key in self.stats.iter():
# leaf = self.stats.get(key)
# print(IPAddress(key.sip), "=>", IPAddress(key.dip),
# "rx", leaf.rx_pkts, "tx", leaf.tx_pkts)
key = Key(IPAddress("172.16.1.2").value, IPAddress("172.16.1.1").value)
leaf = self.stats.get(key)
self.assertEqual(leaf.rx_pkts, 100)
self.assertEqual(leaf.tx_pkts, 100)
if __name__ == "__main__":
main()
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
#include "kprobe.b"
struct Ptr { u64 ptr:64; };
struct Counters { u64 stat1:64; };
Table<Ptr, Counters, FIXED_MATCH, AUTO> stats(1024);
u32 count_sched (struct proto::pt_regs *ctx) {
struct Ptr key = {.ptr=ctx->bx};
atomic_add(stats[key].stat1, 1);
}
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment