-
Notifications
You must be signed in to change notification settings - Fork 312
add bcc socket filter support and example #270
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
Open
zhangbo1882
wants to merge
1
commit into
iovisor:master
Choose a base branch
from
zhangbo1882:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# usage | ||
|
||
1 In current directory (example/bcc/protocol_count) | ||
|
||
```bash | ||
go build net_protocol.go | ||
sudo ./net_protocol | ||
``` | ||
|
||
2 Open another terminal | ||
|
||
```bash | ||
ping 127.0.0.1 -c 10 | ||
``` | ||
|
||
3 Result | ||
|
||
``` | ||
TCP: 0, UDP: 0, ICMP: 0 | ||
TCP: 0, UDP: 0, ICMP: 4 | ||
TCP: 0, UDP: 0, ICMP: 24 | ||
TCP: 0, UDP: 0, ICMP: 40 | ||
TCP: 0, UDP: 0, ICMP: 40 | ||
TCP: 0, UDP: 0, ICMP: 40 | ||
TCP: 4, UDP: 0, ICMP: 40 | ||
``` | ||
|
||
# Misc | ||
|
||
Since we run ping for the loop interface, there are 4 packets for one ping including( egress send, ingress receive, egress reply and ingress reply) | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <linux/if_packet.h> | ||
#include <linux/in.h> | ||
#include <linux/ip.h> | ||
#include <linux/string.h> | ||
#include <linux/tcp.h> | ||
#include <linux/types.h> | ||
#include <linux/udp.h> | ||
|
||
#ifndef offsetof | ||
#define offsetof(TYPE, MEMBER) ((size_t) & ((TYPE *)0)->MEMBER) | ||
#endif | ||
|
||
#define SEC(NAME) __attribute__((section(NAME), used)) | ||
|
||
unsigned long long load_byte(void *skb,unsigned long long off) asm("llvm.bpf.load.byte"); | ||
BPF_HASH(countmap,u32,u32,32); | ||
int protocol_count(struct __sk_buff *skb) { | ||
|
||
int proto = load_byte(skb, ETH_HLEN + offsetof(struct iphdr, protocol)); | ||
int one = 1; | ||
|
||
int *el = countmap.lookup(&proto); | ||
|
||
if (el) { | ||
(*el)++; | ||
} else { | ||
el = &one; | ||
} | ||
countmap.update(&proto,el); | ||
|
||
return 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"fmt" | ||
"io/ioutil" | ||
"syscall" | ||
"time" | ||
"unsafe" | ||
|
||
"github.com/iovisor/gobpf/bcc" | ||
) | ||
|
||
const ( | ||
PROTOCOL_FUNC = "protocol_count" | ||
PROTOCOL_COUNT = "./net_protocol.c" | ||
LOOP = 1 | ||
) | ||
|
||
func ReadBPFFile(file string) (string, error) { | ||
content, err := ioutil.ReadFile(file) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(content), nil | ||
} | ||
|
||
func Htons(i uint16) uint16 { | ||
b := make([]byte, 2) | ||
binary.BigEndian.PutUint16(b, i) | ||
return *(*uint16)(unsafe.Pointer(&b[0])) | ||
} | ||
|
||
func OpenRawSock(index int) (int, error) { | ||
sock, err := syscall.Socket(syscall.AF_PACKET, syscall.SOCK_RAW, int(Htons(syscall.ETH_P_ALL))) | ||
if err != nil { | ||
return 0, err | ||
} | ||
sll := syscall.SockaddrLinklayer{ | ||
Ifindex: index, | ||
Protocol: Htons(syscall.ETH_P_ALL), | ||
} | ||
if err := syscall.Bind(sock, &sll); err != nil { | ||
return 0, err | ||
} | ||
return sock, nil | ||
} | ||
func ProtocolCount(cSource string) { | ||
source, err := ReadBPFFile(cSource) | ||
if err != nil { | ||
fmt.Errorf("read BPF file error: %v", err) | ||
return | ||
} | ||
m := bcc.NewModule(source, []string{}) | ||
defer m.Close() | ||
|
||
socketFilter, err := m.LoadSocketFilter(PROTOCOL_FUNC) | ||
if err != nil { | ||
fmt.Errorf("socket filter %s not found, err: %v", PROTOCOL_FUNC, err) | ||
return | ||
} | ||
|
||
fd, err := OpenRawSock(LOOP) | ||
if err != nil { | ||
fmt.Errorf("unable to open a raw socket: %s", err) | ||
return | ||
} | ||
defer syscall.Close(fd) | ||
|
||
if err := m.AttachSocketFilter(fd, socketFilter); err != nil { | ||
fmt.Errorf("failed trying to attach socket filter: %s", err) | ||
return | ||
} | ||
|
||
table := bcc.NewTable(m.TableId("countmap"), m) | ||
var tcp, udp, icmp, leafInt, keyInt uint32 | ||
hostEndian := bcc.GetHostByteOrder() | ||
for { | ||
iter := table.Iter() | ||
for iter.Next() { | ||
key, leaf := iter.Key(), iter.Leaf() | ||
if err := binary.Read(bytes.NewBuffer(key), hostEndian, &keyInt); err != nil { | ||
continue | ||
} | ||
if err := binary.Read(bytes.NewBuffer(leaf), hostEndian, &leafInt); err != nil { | ||
continue | ||
} | ||
switch keyInt { | ||
case syscall.IPPROTO_TCP: | ||
tcp = leafInt | ||
case syscall.IPPROTO_UDP: | ||
udp = leafInt | ||
case syscall.IPPROTO_ICMP: | ||
icmp = leafInt | ||
} | ||
} | ||
fmt.Printf("TCP: %v, UDP: %v, ICMP: %v\n", tcp, udp, icmp) | ||
time.Sleep(5 * time.Second) | ||
} | ||
} | ||
|
||
func main() { | ||
ProtocolCount(PROTOCOL_COUNT) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.