Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make installation prefixes more realistic #36

Merged
merged 1 commit into from
Jun 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ find_library(libclangParse NAMES clangParse HINTS ${CLANG_SEARCH})
find_library(libclangRewrite NAMES clangRewrite HINTS ${CLANG_SEARCH})
find_library(libclangSema NAMES clangSema HINTS ${CLANG_SEARCH})
find_library(libclangSerialization NAMES clangSerialization HINTS ${CLANG_SEARCH})
if(libclangBasic STREQUAL "libclangBasic-NOTFOUND")
message(FATAL_ERROR "Unable to find clang libraries")
endif()

set(CMAKE_C_FLAGS "-Wall")
set(CMAKE_CXX_FLAGS "-std=c++11 -Wall")
Expand Down
110 changes: 45 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,89 +36,69 @@ The features of this toolkit include:

To get started using this toolchain, one needs:
* Linux kernel 4.1 or newer, with these flags enabled:
* CONFIG_BPF=y
* CONFIG_BPF_SYSCALL=y
* CONFIG_NET_CLS_BPF=m [optional, for tc filters]
* CONFIG_NET_ACT_BPF=m [optional, for tc actions]
* CONFIG_BPF_JIT=y
* CONFIG_HAVE_BPF_JIT=y
* CONFIG_BPF_EVENTS=y [optional, for kprobes]
* `CONFIG_BPF=y`
* `CONFIG_BPF_SYSCALL=y`
* `CONFIG_NET_CLS_BPF=m` [optional, for tc filters]
* `CONFIG_NET_ACT_BPF=m` [optional, for tc actions]
* `CONFIG_BPF_JIT=y`
* `CONFIG_HAVE_BPF_JIT=y`
* `CONFIG_BPF_EVENTS=y` [optional, for kprobes]
* LLVM 3.7 or newer, compiled with BPF support (currently experimental)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

experimental line can be dropped :)

* Clang 3.5 or newer (this requirement is orthoganal to the LLVM requirement,
and the versions do not necessarily need to match)
* cmake, gcc-4.9, flex, bison, xxd, libstdc++-static, libmnl-devel
* Clang 3.7, built from the same tree as LLVM
* pyroute2, version X.X (currently master, tag TBD) or newer
* cmake, gcc-4.7, flex, bison

## Getting started

Included in the scripts/ directory of this project is a VM kickstart script that
captures the above requirements inside a Fedora VM. Before running the script,
ensure that virt-install is available on the system.
### Demo VM

`./build_bpf_demo.sh -n bpf-demo -k bpf_demo.ks.erb`
See https://github.com/iovisor/bcc/scripts/README.md for a script that can
be used to set up a libvirt VM with the required dependencies.

After setting up the initial VM, log in (the default password is 'iovisor')
and determine the DHCP IP. SSH to this IP as root.
### Quick Setup

To set up a kernel with the right options, run `bpf-kernel-setup`.
If the LLVM and Linux kernel requirements are satisfied, testing out this
package should be as simple as:

```
[root@bpf-demo ~]# bpf-kernel-setup
Cloning into 'net-next'...
git clone https://github.com/iovisor/bcc.git
cd bcc; mkdir build; cd build
cmake .. -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_PREFIX_PATH=/opt/local/llvm
make -j$(grep -c ^processor /proc/cpuinfo)
sudo make install
cd ../../
sudo python examples/hello_world.py
<ctrl-C>
```
After pulling the net-next branch, the kernel config menu should pop up. Ensure
that the below settings are proper.
```
General setup --->
[*] Enable bpf() system call
Networking support --->
Networking options --->
QoS and/or fair queueing --->
<M> BPF-based classifier
<M> BPF based action
[*] enable BPF Just In Time compiler
```
Once the .config is saved, the build will proceed and install the resulting
kernel. This kernel has updated userspace headers (e.g. the bpf() syscall) which
install into /usr/local/include...proper packaging for this will be
distro-dependent.

Next, run `bpf-llvm-setup` to pull and compile LLVM with BPF support enabled.
```
[root@bpf-demo ~]# bpf-llvm-setup
Cloning into 'llvm'...
```
The resulting libraries will be installed into /opt/local/llvm.
Change `CMAKE_PREFIX_PATH` if llvm is installed elsewhere.

### Cleaning up

Since packaging is currently not available, one can cleanup the collateral of
bcc by doing:

Next, reboot into the new kernel, either manually or by using the kexec helper.
```
[root@bpf-demo ~]# kexec-4.1.0-rc1+
Connection to 192.168.122.247 closed by remote host.
Connection to 192.168.122.247 closed.
sudo rm -rf /usr/{lib/libbpf.prog.so,include/bcc,share/bcc}
sudo pip uninstall bpf
```

Reconnect and run the final step, building and testing bcc.
### Building LLVM

See https://llvm.org/docs/GettingStarted.html for the full guide.

The short version:

```
[root@bpf-demo ~]# bcc-setup
Cloning into 'bcc'...
...
Linking CXX shared library libbpfprog.so
[100%] Built target bpfprog
...
Running tests...
Test project /root/bcc/build
Start 1: py_test1
1/4 Test #1: py_test1 ......................... Passed 0.24 sec
Start 2: py_test2
2/4 Test #2: py_test2 ......................... Passed 0.53 sec
Start 3: py_trace1
3/4 Test #3: py_trace1 ........................ Passed 0.09 sec
Start 4: py_trace2
4/4 Test #4: py_trace2 ........................ Passed 1.06 sec

100% tests passed, 0 tests failed out of 4
git clone https://github.com/llvm-mirror/llvm.git llvm
git clone https://github.com/llvm-mirror/clang.git llvm/tools/clang
mkdir llvm/build/
cd llvm/build/
cmake .. -DCMAKE_INSTALL_PREFIX=/opt/local/llvm
make -j$(grep -c ^processor /proc/cpuinfo)
sudo make install
```


## Release notes

* 0.1
Expand Down
1 change: 0 additions & 1 deletion examples/hello_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from subprocess import call

prog = """
#include "src/cc/bpf_helpers.h"
BPF_EXPORT(hello)
int hello(void *ctx) {
char fmt[] = "Hello, World!\\n";
Expand Down
68 changes: 68 additions & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

## Fedora Demo VM

Before running the script, ensure that virt-install is available on the system.

`./build_bpf_demo.sh -n bpf-demo -k bpf_demo.ks.erb`

After setting up the initial VM, log in (the default password is 'iovisor')
and determine the DHCP IP. SSH to this IP as root.

To set up a kernel with the right options, run `bpf-kernel-setup`.

```
[root@bpf-demo ~]# bpf-kernel-setup
Cloning into 'net-next'...
```
After pulling the net-next branch, the kernel config menu should pop up. Ensure
that the below settings are proper.
```
General setup --->
[*] Enable bpf() system call
Networking support --->
Networking options --->
QoS and/or fair queueing --->
<M> BPF-based classifier
<M> BPF based action
[*] enable BPF Just In Time compiler
```
Once the .config is saved, the build will proceed and install the resulting
kernel. This kernel has updated userspace headers (e.g. the bpf() syscall) which
install into /usr/local/include...proper packaging for this will be
distro-dependent.

Next, run `bpf-llvm-setup` to pull and compile LLVM with BPF support enabled.
```
[root@bpf-demo ~]# bpf-llvm-setup
Cloning into 'llvm'...
```
The resulting libraries will be installed into /opt/local/llvm.

Next, reboot into the new kernel, either manually or by using the kexec helper.
```
[root@bpf-demo ~]# kexec-4.1.0-rc1+
Connection to 192.168.122.247 closed by remote host.
Connection to 192.168.122.247 closed.
```

Reconnect and run the final step, building and testing bcc.
```
[root@bpf-demo ~]# bcc-setup
Cloning into 'bcc'...
...
Linking CXX shared library libbpfprog.so
[100%] Built target bpfprog
...
Running tests...
Test project /root/bcc/build
Start 1: py_test1
1/4 Test #1: py_test1 ......................... Passed 0.24 sec
Start 2: py_test2
2/4 Test #2: py_test2 ......................... Passed 0.53 sec
Start 3: py_trace1
3/4 Test #3: py_trace1 ........................ Passed 0.09 sec
Start 4: py_trace2
4/4 Test #4: py_trace2 ........................ Passed 1.06 sec

100% tests passed, 0 tests failed out of 4
```
2 changes: 1 addition & 1 deletion scripts/bpf_demo.ks.erb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ set -e -x
numcpu=$(grep -c ^processor /proc/cpuinfo)

git clone https://github.com/llvm-mirror/llvm.git
git clone https://github.com/llvm-mirror/clang.git llvm/tools/clang
mkdir llvm/build/
cd llvm/build/

Expand All @@ -93,7 +94,6 @@ cmake .. \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_ENABLE_TERMINFO=OFF \
-DLLVM_TARGETS_TO_BUILD="ARM;CppBackend;X86;BPF" \
-DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=BPF \
-DCMAKE_INSTALL_PREFIX=/opt/local/llvm

make -j$numcpu
Expand Down
16 changes: 14 additions & 2 deletions src/cc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,20 @@ BISON_TARGET(Parser parser.yy ${CMAKE_CURRENT_BINARY_DIR}/parser.yy.cc COMPILE_F
FLEX_TARGET(Lexer lexer.ll ${CMAKE_CURRENT_BINARY_DIR}/lexer.ll.cc COMPILE_FLAGS "--c++ --o lexer.ll.cc")
ADD_FLEX_BISON_DEPENDENCY(Lexer Parser)

# if gcc 4.9 or higher is used, static libstdc++ is a good option
#set(CMAKE_SHARED_LINKER_FLAGS "-static-libstdc++ -Wl,--exclude-libs=ALL")
# prune unused llvm static library stuff when linking into the new .so
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--exclude-libs=ALL")

# if gcc 4.9 or higher is used, static libstdc++ is a good option
if (CMAKE_COMPILER_IS_GNUCC)
execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION)
if (GCC_VERSION VERSION_GREATER 4.9 OR GCC_VERSION VERSION_EQUAL 4.9)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libstdc++")
endif()
endif()

# tell the shared library where it is being installed so it can find shared header files
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DBCC_INSTALL_PREFIX='\"${CMAKE_INSTALL_PREFIX}\"'")

add_library(bpfprog SHARED bpf_common.cc bpf_module.cc codegen_llvm.cc
node.cc parser.cc printer.cc type_check.cc libbpf.c b_frontend_action.cc
kbuild_helper.cc
Expand All @@ -30,3 +40,5 @@ set(clang_libs ${libclangFrontend} ${libclangSerialization} ${libclangDriver} ${
target_link_libraries(bpfprog ${clang_libs} ${llvm_libs} LLVMBPFCodeGen)

install(TARGETS bpfprog LIBRARY DESTINATION lib)
install(DIRECTORY export/ DESTINATION share/bcc/include/bcc
FILES_MATCHING PATTERN "*.h")
7 changes: 5 additions & 2 deletions src/cc/bpf_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ int BPFModule::load_file_module(unique_ptr<llvm::Module> *mod, const string &fil
vector<string> kflags;
if (kbuild_helper.get_flags(un.release, &kflags))
return -1;
kflags.push_back("-include");
kflags.push_back(BCC_INSTALL_PREFIX "/share/bcc/include/bcc/helpers.h");
kflags.push_back("-I");
kflags.push_back(BCC_INSTALL_PREFIX "/share/bcc/include");
for (auto it = kflags.begin(); it != kflags.end(); ++it)
flags_cstr.push_back(it->c_str());

Expand Down Expand Up @@ -329,8 +333,7 @@ int BPFModule::parse() {
return -1;
}

// TODO: clean this
if (load_includes("../../src/cc/bpf_helpers.h") < 0)
if (load_includes(BCC_INSTALL_PREFIX "/share/bcc/include/bcc/helpers.h") < 0)
return -1;

codegen_ = ebpf::make_unique<ebpf::cc::CodegenLLVM>(mod_, parser_->scopes_.get(), proto_parser_->scopes_.get());
Expand Down
File renamed without changes.
84 changes: 84 additions & 0 deletions src/cc/export/proto.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2015 PLUMgrid, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

struct ethernet_t {
unsigned long long dst:48;
unsigned long long src:48;
unsigned int type:16;
} __attribute__((packed));

struct dot1q_t {
unsigned short pri:3;
unsigned short cfi:1;
unsigned short vlanid:12;
unsigned short type;
} __attribute__((packed));

struct arp_t {
unsigned short htype;
unsigned short ptype;
unsigned char hlen;
unsigned char plen;
unsigned short oper;
unsigned long long sha:48;
unsigned long long spa:32;
unsigned long long tha:48;
unsigned int tpa;
} __attribute__((packed));

struct ip_t {
unsigned char ver:4; // byte 0
unsigned char hlen:4;
unsigned char tos;
unsigned short tlen;
unsigned short identification; // byte 4
unsigned short ffo_unused:1;
unsigned short df:1;
unsigned short mf:1;
unsigned short foffset:13;
unsigned char ttl; // byte 8
unsigned char nextp;
unsigned short hchecksum;
unsigned int src; // byte 12
unsigned int dst; // byte 16
} __attribute__((packed));

struct udp_t {
unsigned short sport;
unsigned short dport;
unsigned short length;
unsigned short crc;
} __attribute__((packed));

struct tcp_t {
unsigned short src_port; // byte 0
unsigned short dst_port;
unsigned int seq_num; // byte 4
unsigned int ack_num; // byte 8
unsigned char offset:4; // byte 12
unsigned char reserved:4;
unsigned char flag_cwr:1;
unsigned char flag_ece:1;
unsigned char flag_urg:1;
unsigned char flag_ack:1;
unsigned char flag_psh:1;
unsigned char flag_rst:1;
unsigned char flag_syn:1;
unsigned char flag_fin:1;
unsigned short rcv_wnd;
unsigned short cksum; // byte 16
unsigned short urg_ptr;
} __attribute__((packed));
Loading