Commit 472103b3 authored by 4ast's avatar 4ast Committed by GitHub

Merge pull request #1187 from mauriciovasquezbernal/remove_trailing_spaces

remove trailing white spaces from source code files
parents 634f4b0a d1324ac2
...@@ -35,7 +35,7 @@ int on_context_switch(struct pt_regs *ctx) { ...@@ -35,7 +35,7 @@ int on_context_switch(struct pt_regs *ctx) {
event.pid = bpf_get_current_pid_tgid(); event.pid = bpf_get_current_pid_tgid();
bpf_get_current_comm(&event.name, sizeof(event.name)); bpf_get_current_comm(&event.name, sizeof(event.name));
bpf_usdt_readarg(1, ctx, &event.old_addr); bpf_usdt_readarg(1, ctx, &event.old_addr);
bpf_usdt_readarg(2, ctx, &event.new_addr); bpf_usdt_readarg(2, ctx, &event.new_addr);
......
...@@ -34,7 +34,7 @@ local prog = bpf.socket('lo', function (skb) ...@@ -34,7 +34,7 @@ local prog = bpf.socket('lo', function (skb)
-- Fetch 4 bytes of TCP data and compare -- Fetch 4 bytes of TCP data and compare
local h = data(0, 4) local h = data(0, 4)
if h == 'HTTP' or h == 'GET ' or if h == 'HTTP' or h == 'GET ' or
h == 'POST' or h == 'PUT ' or h == 'POST' or h == 'PUT ' or
h == 'HEAD' or h == 'DELE' then h == 'HEAD' or h == 'DELE' then
-- If hash key doesn't exist, create it -- If hash key doesn't exist, create it
-- otherwise increment counter -- otherwise increment counter
......
...@@ -2,21 +2,21 @@ ...@@ -2,21 +2,21 @@
#include <net/sock.h> #include <net/sock.h>
#include <bcc/proto.h> #include <bcc/proto.h>
#define IP_TCP 6 #define IP_TCP 6
#define ETH_HLEN 14 #define ETH_HLEN 14
struct Key { struct Key {
u32 src_ip; //source ip u32 src_ip; //source ip
u32 dst_ip; //destination ip u32 dst_ip; //destination ip
unsigned short src_port; //source port unsigned short src_port; //source port
unsigned short dst_port; //destination port unsigned short dst_port; //destination port
}; };
struct Leaf { struct Leaf {
int timestamp; //timestamp in ns int timestamp; //timestamp in ns
}; };
//BPF_TABLE(map_type, key_type, leaf_type, table_name, num_entry) //BPF_TABLE(map_type, key_type, leaf_type, table_name, num_entry)
//map <Key, Leaf> //map <Key, Leaf>
//tracing sessions having same Key(dst_ip, src_ip, dst_port,src_port) //tracing sessions having same Key(dst_ip, src_ip, dst_port,src_port)
BPF_HASH(sessions, struct Key, struct Leaf, 1024); BPF_HASH(sessions, struct Key, struct Leaf, 1024);
...@@ -40,7 +40,7 @@ int http_filter(struct __sk_buff *skb) { ...@@ -40,7 +40,7 @@ int http_filter(struct __sk_buff *skb) {
struct ethernet_t *ethernet = cursor_advance(cursor, sizeof(*ethernet)); struct ethernet_t *ethernet = cursor_advance(cursor, sizeof(*ethernet));
//filter IP packets (ethernet type = 0x0800) //filter IP packets (ethernet type = 0x0800)
if (!(ethernet->type == 0x0800)) { if (!(ethernet->type == 0x0800)) {
goto DROP; goto DROP;
} }
struct ip_t *ip = cursor_advance(cursor, sizeof(*ip)); struct ip_t *ip = cursor_advance(cursor, sizeof(*ip));
...@@ -69,16 +69,16 @@ int http_filter(struct __sk_buff *skb) { ...@@ -69,16 +69,16 @@ int http_filter(struct __sk_buff *skb) {
//value to multiply * 4 //value to multiply * 4
//e.g. ip->hlen = 5 ; IP Header Length = 5 x 4 byte = 20 byte //e.g. ip->hlen = 5 ; IP Header Length = 5 x 4 byte = 20 byte
ip_header_length = ip->hlen << 2; //SHL 2 -> *4 multiply ip_header_length = ip->hlen << 2; //SHL 2 -> *4 multiply
//calculate tcp header length //calculate tcp header length
//value to multiply *4 //value to multiply *4
//e.g. tcp->offset = 5 ; TCP Header Length = 5 x 4 byte = 20 byte //e.g. tcp->offset = 5 ; TCP Header Length = 5 x 4 byte = 20 byte
tcp_header_length = tcp->offset << 2; //SHL 2 -> *4 multiply tcp_header_length = tcp->offset << 2; //SHL 2 -> *4 multiply
//calculate patload offset and length //calculate patload offset and length
payload_offset = ETH_HLEN + ip_header_length + tcp_header_length; payload_offset = ETH_HLEN + ip_header_length + tcp_header_length;
payload_length = ip->tlen - ip_header_length - tcp_header_length; payload_length = ip->tlen - ip_header_length - tcp_header_length;
//http://stackoverflow.com/questions/25047905/http-request-minimum-size-in-bytes //http://stackoverflow.com/questions/25047905/http-request-minimum-size-in-bytes
//minimum length of http request is always geater than 7 bytes //minimum length of http request is always geater than 7 bytes
//avoid invalid access memory //avoid invalid access memory
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
#Bertrone Matteo - Polytechnic of Turin #Bertrone Matteo - Polytechnic of Turin
#November 2015 #November 2015
# #
#eBPF application that parses HTTP packets #eBPF application that parses HTTP packets
#and extracts (and prints on screen) the URL contained in the GET/POST request. #and extracts (and prints on screen) the URL contained in the GET/POST request.
# #
#eBPF program http_filter is used as SOCKET_FILTER attached to eth0 interface. #eBPF program http_filter is used as SOCKET_FILTER attached to eth0 interface.
...@@ -38,7 +38,7 @@ def toHex(s): ...@@ -38,7 +38,7 @@ def toHex(s):
if len(hv) == 1: if len(hv) == 1:
hv = '0'+hv hv = '0'+hv
lst.append(hv) lst.append(hv)
return reduce(lambda x,y:x+y, lst) return reduce(lambda x,y:x+y, lst)
#print str until CR+LF #print str until CR+LF
...@@ -50,7 +50,7 @@ def printUntilCRLF(str): ...@@ -50,7 +50,7 @@ def printUntilCRLF(str):
return return
print ("%c" % (str[k]), end = "") print ("%c" % (str[k]), end = "")
print("") print("")
return return
#cleanup function #cleanup function
def cleanup(): def cleanup():
...@@ -71,7 +71,7 @@ def cleanup(): ...@@ -71,7 +71,7 @@ def cleanup():
del bpf_sessions[key] del bpf_sessions[key]
except: except:
print("cleanup exception.") print("cleanup exception.")
return return
#args #args
def usage(): def usage():
...@@ -155,9 +155,9 @@ while 1: ...@@ -155,9 +155,9 @@ while 1:
#convert packet into bytearray #convert packet into bytearray
packet_bytearray = bytearray(packet_str) packet_bytearray = bytearray(packet_str)
#ethernet header length #ethernet header length
ETH_HLEN = 14 ETH_HLEN = 14
#IP HEADER #IP HEADER
#https://tools.ietf.org/html/rfc791 #https://tools.ietf.org/html/rfc791
...@@ -166,18 +166,18 @@ while 1: ...@@ -166,18 +166,18 @@ while 1:
# |Version| IHL |Type of Service| Total Length | # |Version| IHL |Type of Service| Total Length |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# #
#IHL : Internet Header Length is the length of the internet header #IHL : Internet Header Length is the length of the internet header
#value to multiply * 4 byte #value to multiply * 4 byte
#e.g. IHL = 5 ; IP Header Length = 5 * 4 byte = 20 byte #e.g. IHL = 5 ; IP Header Length = 5 * 4 byte = 20 byte
# #
#Total length: This 16-bit field defines the entire packet size, #Total length: This 16-bit field defines the entire packet size,
#including header and data, in bytes. #including header and data, in bytes.
#calculate packet total length #calculate packet total length
total_length = packet_bytearray[ETH_HLEN + 2] #load MSB total_length = packet_bytearray[ETH_HLEN + 2] #load MSB
total_length = total_length << 8 #shift MSB total_length = total_length << 8 #shift MSB
total_length = total_length + packet_bytearray[ETH_HLEN+3] #add LSB total_length = total_length + packet_bytearray[ETH_HLEN+3] #add LSB
#calculate ip header length #calculate ip header length
ip_header_length = packet_bytearray[ETH_HLEN] #load Byte ip_header_length = packet_bytearray[ETH_HLEN] #load Byte
ip_header_length = ip_header_length & 0x0F #mask bits 0..3 ip_header_length = ip_header_length & 0x0F #mask bits 0..3
...@@ -189,18 +189,18 @@ while 1: ...@@ -189,18 +189,18 @@ while 1:
ip_src = int(toHex(ip_src_str),16) ip_src = int(toHex(ip_src_str),16)
ip_dst = int(toHex(ip_dst_str),16) ip_dst = int(toHex(ip_dst_str),16)
#TCP HEADER #TCP HEADER
#https://www.rfc-editor.org/rfc/rfc793.txt #https://www.rfc-editor.org/rfc/rfc793.txt
# 12 13 14 15 # 12 13 14 15
# 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 # 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# | Data | |U|A|P|R|S|F| | # | Data | |U|A|P|R|S|F| |
# | Offset| Reserved |R|C|S|S|Y|I| Window | # | Offset| Reserved |R|C|S|S|Y|I| Window |
# | | |G|K|H|T|N|N| | # | | |G|K|H|T|N|N| |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# #
#Data Offset: This indicates where the data begins. #Data Offset: This indicates where the data begins.
#The TCP header is an integral number of 32 bits long. #The TCP header is an integral number of 32 bits long.
#value to multiply * 4 byte #value to multiply * 4 byte
#e.g. DataOffset = 5 ; TCP Header Length = 5 * 4 byte = 20 byte #e.g. DataOffset = 5 ; TCP Header Length = 5 * 4 byte = 20 byte
...@@ -209,17 +209,17 @@ while 1: ...@@ -209,17 +209,17 @@ while 1:
tcp_header_length = packet_bytearray[ETH_HLEN + ip_header_length + 12] #load Byte tcp_header_length = packet_bytearray[ETH_HLEN + ip_header_length + 12] #load Byte
tcp_header_length = tcp_header_length & 0xF0 #mask bit 4..7 tcp_header_length = tcp_header_length & 0xF0 #mask bit 4..7
tcp_header_length = tcp_header_length >> 2 #SHR 4 ; SHL 2 -> SHR 2 tcp_header_length = tcp_header_length >> 2 #SHR 4 ; SHL 2 -> SHR 2
#retrieve port source/dest #retrieve port source/dest
port_src_str = packet_str[ETH_HLEN+ip_header_length:ETH_HLEN+ip_header_length+2] port_src_str = packet_str[ETH_HLEN+ip_header_length:ETH_HLEN+ip_header_length+2]
port_dst_str = packet_str[ETH_HLEN+ip_header_length+2:ETH_HLEN+ip_header_length+4] port_dst_str = packet_str[ETH_HLEN+ip_header_length+2:ETH_HLEN+ip_header_length+4]
port_src = int(toHex(port_src_str),16) port_src = int(toHex(port_src_str),16)
port_dst = int(toHex(port_dst_str),16) port_dst = int(toHex(port_dst_str),16)
#calculate payload offset #calculate payload offset
payload_offset = ETH_HLEN + ip_header_length + tcp_header_length payload_offset = ETH_HLEN + ip_header_length + tcp_header_length
#payload_string contains only packet payload #payload_string contains only packet payload
payload_string = packet_str[(payload_offset):(len(packet_bytearray))] payload_string = packet_str[(payload_offset):(len(packet_bytearray))]
...@@ -238,14 +238,14 @@ while 1: ...@@ -238,14 +238,14 @@ while 1:
#url entirely contained in first packet -> print it all #url entirely contained in first packet -> print it all
printUntilCRLF(payload_string) printUntilCRLF(payload_string)
#delete current_Key from bpf_sessions, url already printed. current session not useful anymore #delete current_Key from bpf_sessions, url already printed. current session not useful anymore
try: try:
del bpf_sessions[current_Key] del bpf_sessions[current_Key]
except: except:
print ("error during delete from bpf map ") print ("error during delete from bpf map ")
else: else:
#url NOT entirely contained in first packet #url NOT entirely contained in first packet
#not found \r\n in payload. #not found \r\n in payload.
#save current part of the payload_string in dictionary <key(ips,ipd,ports,portd),payload_string> #save current part of the payload_string in dictionary <key(ips,ipd,ports,portd),payload_string>
local_dictionary[binascii.hexlify(current_Key)] = payload_string local_dictionary[binascii.hexlify(current_Key)] = payload_string
else: else:
...@@ -253,17 +253,17 @@ while 1: ...@@ -253,17 +253,17 @@ while 1:
#check if the packet belong to a session saved in bpf_sessions #check if the packet belong to a session saved in bpf_sessions
if (current_Key in bpf_sessions): if (current_Key in bpf_sessions):
#check id the packet belong to a session saved in local_dictionary #check id the packet belong to a session saved in local_dictionary
#(local_dictionary mantains HTTP GET/POST url not printed yet because splitted in N packets) #(local_dictionary mantains HTTP GET/POST url not printed yet because splitted in N packets)
if (binascii.hexlify(current_Key) in local_dictionary): if (binascii.hexlify(current_Key) in local_dictionary):
#first part of the HTTP GET/POST url is already present in local dictionary (prev_payload_string) #first part of the HTTP GET/POST url is already present in local dictionary (prev_payload_string)
prev_payload_string = local_dictionary[binascii.hexlify(current_Key)] prev_payload_string = local_dictionary[binascii.hexlify(current_Key)]
#looking for CR+LF in current packet. #looking for CR+LF in current packet.
if (crlf in payload_string): if (crlf in payload_string):
#last packet. containing last part of HTTP GET/POST url splitted in N packets. #last packet. containing last part of HTTP GET/POST url splitted in N packets.
#append current payload #append current payload
prev_payload_string += payload_string prev_payload_string += payload_string
#print HTTP GET/POST url #print HTTP GET/POST url
printUntilCRLF(prev_payload_string) printUntilCRLF(prev_payload_string)
#clean bpf_sessions & local_dictionary #clean bpf_sessions & local_dictionary
try: try:
...@@ -284,7 +284,7 @@ while 1: ...@@ -284,7 +284,7 @@ while 1:
except: except:
print ("error deleting from map or dict") print ("error deleting from map or dict")
#update dictionary #update dictionary
local_dictionary[binascii.hexlify(current_Key)] = prev_payload_string local_dictionary[binascii.hexlify(current_Key)] = prev_payload_string
else: else:
#first part of the HTTP GET/POST url is NOT present in local dictionary #first part of the HTTP GET/POST url is NOT present in local dictionary
#bpf_sessions contains invalid entry -> delete it #bpf_sessions contains invalid entry -> delete it
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
#include <net/sock.h> #include <net/sock.h>
#include <bcc/proto.h> #include <bcc/proto.h>
#define IP_TCP 6 #define IP_TCP 6
#define ETH_HLEN 14 #define ETH_HLEN 14
/*eBPF program. /*eBPF program.
...@@ -20,7 +20,7 @@ int http_filter(struct __sk_buff *skb) { ...@@ -20,7 +20,7 @@ int http_filter(struct __sk_buff *skb) {
struct ethernet_t *ethernet = cursor_advance(cursor, sizeof(*ethernet)); struct ethernet_t *ethernet = cursor_advance(cursor, sizeof(*ethernet));
//filter IP packets (ethernet type = 0x0800) //filter IP packets (ethernet type = 0x0800)
if (!(ethernet->type == 0x0800)) { if (!(ethernet->type == 0x0800)) {
goto DROP; goto DROP;
} }
struct ip_t *ip = cursor_advance(cursor, sizeof(*ip)); struct ip_t *ip = cursor_advance(cursor, sizeof(*ip));
...@@ -40,16 +40,16 @@ int http_filter(struct __sk_buff *skb) { ...@@ -40,16 +40,16 @@ int http_filter(struct __sk_buff *skb) {
//value to multiply * 4 //value to multiply * 4
//e.g. ip->hlen = 5 ; IP Header Length = 5 x 4 byte = 20 byte //e.g. ip->hlen = 5 ; IP Header Length = 5 x 4 byte = 20 byte
ip_header_length = ip->hlen << 2; //SHL 2 -> *4 multiply ip_header_length = ip->hlen << 2; //SHL 2 -> *4 multiply
//calculate tcp header length //calculate tcp header length
//value to multiply *4 //value to multiply *4
//e.g. tcp->offset = 5 ; TCP Header Length = 5 x 4 byte = 20 byte //e.g. tcp->offset = 5 ; TCP Header Length = 5 x 4 byte = 20 byte
tcp_header_length = tcp->offset << 2; //SHL 2 -> *4 multiply tcp_header_length = tcp->offset << 2; //SHL 2 -> *4 multiply
//calculate patload offset and length //calculate patload offset and length
payload_offset = ETH_HLEN + ip_header_length + tcp_header_length; payload_offset = ETH_HLEN + ip_header_length + tcp_header_length;
payload_length = ip->tlen - ip_header_length - tcp_header_length; payload_length = ip->tlen - ip_header_length - tcp_header_length;
//http://stackoverflow.com/questions/25047905/http-request-minimum-size-in-bytes //http://stackoverflow.com/questions/25047905/http-request-minimum-size-in-bytes
//minimum length of http request is always geater than 7 bytes //minimum length of http request is always geater than 7 bytes
//avoid invalid access memory //avoid invalid access memory
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
#Bertrone Matteo - Polytechnic of Turin #Bertrone Matteo - Polytechnic of Turin
#November 2015 #November 2015
# #
#eBPF application that parses HTTP packets #eBPF application that parses HTTP packets
#and extracts (and prints on screen) the URL contained in the GET/POST request. #and extracts (and prints on screen) the URL contained in the GET/POST request.
# #
#eBPF program http_filter is used as SOCKET_FILTER attached to eth0 interface. #eBPF program http_filter is used as SOCKET_FILTER attached to eth0 interface.
...@@ -90,9 +90,9 @@ while 1: ...@@ -90,9 +90,9 @@ while 1:
#convert packet into bytearray #convert packet into bytearray
packet_bytearray = bytearray(packet_str) packet_bytearray = bytearray(packet_str)
#ethernet header length #ethernet header length
ETH_HLEN = 14 ETH_HLEN = 14
#IP HEADER #IP HEADER
#https://tools.ietf.org/html/rfc791 #https://tools.ietf.org/html/rfc791
...@@ -101,34 +101,34 @@ while 1: ...@@ -101,34 +101,34 @@ while 1:
# |Version| IHL |Type of Service| Total Length | # |Version| IHL |Type of Service| Total Length |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# #
#IHL : Internet Header Length is the length of the internet header #IHL : Internet Header Length is the length of the internet header
#value to multiply * 4 byte #value to multiply * 4 byte
#e.g. IHL = 5 ; IP Header Length = 5 * 4 byte = 20 byte #e.g. IHL = 5 ; IP Header Length = 5 * 4 byte = 20 byte
# #
#Total length: This 16-bit field defines the entire packet size, #Total length: This 16-bit field defines the entire packet size,
#including header and data, in bytes. #including header and data, in bytes.
#calculate packet total length #calculate packet total length
total_length = packet_bytearray[ETH_HLEN + 2] #load MSB total_length = packet_bytearray[ETH_HLEN + 2] #load MSB
total_length = total_length << 8 #shift MSB total_length = total_length << 8 #shift MSB
total_length = total_length + packet_bytearray[ETH_HLEN+3] #add LSB total_length = total_length + packet_bytearray[ETH_HLEN+3] #add LSB
#calculate ip header length #calculate ip header length
ip_header_length = packet_bytearray[ETH_HLEN] #load Byte ip_header_length = packet_bytearray[ETH_HLEN] #load Byte
ip_header_length = ip_header_length & 0x0F #mask bits 0..3 ip_header_length = ip_header_length & 0x0F #mask bits 0..3
ip_header_length = ip_header_length << 2 #shift to obtain length ip_header_length = ip_header_length << 2 #shift to obtain length
#TCP HEADER #TCP HEADER
#https://www.rfc-editor.org/rfc/rfc793.txt #https://www.rfc-editor.org/rfc/rfc793.txt
# 12 13 14 15 # 12 13 14 15
# 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 # 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# | Data | |U|A|P|R|S|F| | # | Data | |U|A|P|R|S|F| |
# | Offset| Reserved |R|C|S|S|Y|I| Window | # | Offset| Reserved |R|C|S|S|Y|I| Window |
# | | |G|K|H|T|N|N| | # | | |G|K|H|T|N|N| |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# #
#Data Offset: This indicates where the data begins. #Data Offset: This indicates where the data begins.
#The TCP header is an integral number of 32 bits long. #The TCP header is an integral number of 32 bits long.
#value to multiply * 4 byte #value to multiply * 4 byte
#e.g. DataOffset = 5 ; TCP Header Length = 5 * 4 byte = 20 byte #e.g. DataOffset = 5 ; TCP Header Length = 5 * 4 byte = 20 byte
...@@ -137,10 +137,10 @@ while 1: ...@@ -137,10 +137,10 @@ while 1:
tcp_header_length = packet_bytearray[ETH_HLEN + ip_header_length + 12] #load Byte tcp_header_length = packet_bytearray[ETH_HLEN + ip_header_length + 12] #load Byte
tcp_header_length = tcp_header_length & 0xF0 #mask bit 4..7 tcp_header_length = tcp_header_length & 0xF0 #mask bit 4..7
tcp_header_length = tcp_header_length >> 2 #SHR 4 ; SHL 2 -> SHR 2 tcp_header_length = tcp_header_length >> 2 #SHR 4 ; SHL 2 -> SHR 2
#calculate payload offset #calculate payload offset
payload_offset = ETH_HLEN + ip_header_length + tcp_header_length payload_offset = ETH_HLEN + ip_header_length + tcp_header_length
#print first line of the HTTP GET/POST request #print first line of the HTTP GET/POST request
#line ends with 0xOD 0xOA (\r\n) #line ends with 0xOD 0xOA (\r\n)
#(if we want to print all the header print until \r\n\r\n) #(if we want to print all the header print until \r\n\r\n)
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
# #
# Copyright (c) PLUMgrid, Inc. # Copyright (c) PLUMgrid, Inc.
# Licensed under the Apache License, Version 2.0 (the "License") # Licensed under the Apache License, Version 2.0 (the "License")
# #
# Example output: # Example output:
# $ sudo ./strlen_hist.py # $ sudo ./strlen_hist.py
# 22:12:52 # 22:12:52
......
...@@ -122,7 +122,7 @@ static inline bool _is_tracepoint_struct_type(string const& type_name, ...@@ -122,7 +122,7 @@ static inline bool _is_tracepoint_struct_type(string const& type_name,
// (?:struct|class)\s+tracepoint__(\S+)__(\S+) // (?:struct|class)\s+tracepoint__(\S+)__(\S+)
// Not using std::regex because older versions of GCC don't support it yet. // Not using std::regex because older versions of GCC don't support it yet.
// E.g., the libstdc++ that ships with Ubuntu 14.04. // E.g., the libstdc++ that ships with Ubuntu 14.04.
auto first_space_pos = type_name.find_first_of("\t "); auto first_space_pos = type_name.find_first_of("\t ");
if (first_space_pos == string::npos) if (first_space_pos == string::npos)
return false; return false;
......
...@@ -53,7 +53,7 @@ class TracepointTypeVisitor : ...@@ -53,7 +53,7 @@ class TracepointTypeVisitor :
clang::ASTContext &C; clang::ASTContext &C;
clang::DiagnosticsEngine &diag_; clang::DiagnosticsEngine &diag_;
clang::Rewriter &rewriter_; clang::Rewriter &rewriter_;
llvm::raw_ostream &out_; llvm::raw_ostream &out_;
}; };
class TracepointTypeConsumer : public clang::ASTConsumer { class TracepointTypeConsumer : public clang::ASTConsumer {
......
...@@ -3,10 +3,10 @@ ...@@ -3,10 +3,10 @@
class CompilationException(Exception): class CompilationException(Exception):
"""Signals an error during compilation""" """Signals an error during compilation"""
def __init__(self, isBug, format, *message): def __init__(self, isBug, format, *message):
# isBug: indicates that this is a compiler bug # isBug: indicates that this is a compiler bug
super(CompilationException, self).__init__() super(CompilationException, self).__init__()
assert isinstance(format, str) assert isinstance(format, str)
assert isinstance(isBug, bool) assert isinstance(isBug, bool)
self.message = message self.message = message
......
...@@ -225,10 +225,10 @@ class EbpfAction(EbpfActionBase): ...@@ -225,10 +225,10 @@ class EbpfAction(EbpfActionBase):
dst.asString, dst.asString,
src.asString, src.asString,
size / 8) size / 8)
elif (callee.name == "add" or elif (callee.name == "add" or
callee.name == "bit_and" or callee.name == "bit_and" or
callee.name == "bit_or" or callee.name == "bit_or" or
callee.name == "bit_xor" or callee.name == "bit_xor" or
callee.name == "subtract"): callee.name == "subtract"):
size = self.checkSize(callee, size = self.checkSize(callee,
[a.widthInBits() for a in args], [a.widthInBits() for a in args],
...@@ -247,7 +247,7 @@ class EbpfAction(EbpfActionBase): ...@@ -247,7 +247,7 @@ class EbpfAction(EbpfActionBase):
args[1].asString, args[1].asString,
op, op,
args[2].asString) args[2].asString)
elif (callee.name == "add_to_field" or elif (callee.name == "add_to_field" or
callee.name == "subtract_from_field"): callee.name == "subtract_from_field"):
size = self.checkSize(callee, size = self.checkSize(callee,
[a.widthInBits() for a in args], [a.widthInBits() for a in args],
......
...@@ -50,7 +50,7 @@ class EbpfConditional(object): ...@@ -50,7 +50,7 @@ class EbpfConditional(object):
"{0}.{1}.{2}", base, einstance.name, node.name) "{0}.{1}.{2}", base, einstance.name, node.name)
else: else:
raise CompilationException(True, "{0} Unexpected expression ", node) raise CompilationException(True, "{0} Unexpected expression ", node)
def emitExpression(self, expression, serializer, program, toplevel): def emitExpression(self, expression, serializer, program, toplevel):
assert isinstance(serializer, ProgramSerializer) assert isinstance(serializer, ProgramSerializer)
assert isinstance(program, ebpfProgram.EbpfProgram) assert isinstance(program, ebpfProgram.EbpfProgram)
...@@ -59,9 +59,9 @@ class EbpfConditional(object): ...@@ -59,9 +59,9 @@ class EbpfConditional(object):
left = expression.left left = expression.left
op = expression.op op = expression.op
right = expression.right right = expression.right
assert isinstance(op, str) assert isinstance(op, str)
if op == "valid": if op == "valid":
self.emitNode(right, serializer, program) self.emitNode(right, serializer, program)
serializer.append(".valid") serializer.append(".valid")
......
...@@ -27,7 +27,7 @@ class EbpfCounter(object): ...@@ -27,7 +27,7 @@ class EbpfCounter(object):
self.dataMapName = self.name self.dataMapName = self.name
if ((hlircounter.binding is None) or if ((hlircounter.binding is None) or
(hlircounter.binding[0] != P4_DIRECT)): (hlircounter.binding[0] != P4_DIRECT)):
raise NotSupportedException( raise NotSupportedException(
"{0}: counter which is not direct", hlircounter) "{0}: counter which is not direct", hlircounter)
......
...@@ -19,7 +19,7 @@ from compilationException import * ...@@ -19,7 +19,7 @@ from compilationException import *
class EbpfProgram(object): class EbpfProgram(object):
def __init__(self, name, hlir, isRouter, config): def __init__(self, name, hlir, isRouter, config):
"""Representation of an EbpfProgram (in fact, """Representation of an EbpfProgram (in fact,
a C program that is converted to EBPF)""" a C program that is converted to EBPF)"""
assert isinstance(hlir, HLIR) assert isinstance(hlir, HLIR)
assert isinstance(isRouter, bool) assert isinstance(isRouter, bool)
......
...@@ -51,7 +51,7 @@ class EbpfScalarType(EbpfType): ...@@ -51,7 +51,7 @@ class EbpfScalarType(EbpfType):
return 4 return 4
else: else:
return 1 # Char array return 1 # Char array
def serialize(self, serializer): def serialize(self, serializer):
assert isinstance(serializer, ProgramSerializer) assert isinstance(serializer, ProgramSerializer)
serializer.append(self.asString()) serializer.append(self.asString())
......
...@@ -84,8 +84,8 @@ class EbpfTableKey(object): ...@@ -84,8 +84,8 @@ class EbpfTableKey(object):
matchType = f[1] matchType = f[1]
mask = f[2] mask = f[2]
if ((matchType is p4_match_type.P4_MATCH_TERNARY) or if ((matchType is p4_match_type.P4_MATCH_TERNARY) or
(matchType is p4_match_type.P4_MATCH_LPM) or (matchType is p4_match_type.P4_MATCH_LPM) or
(matchType is p4_match_type.P4_MATCH_RANGE)): (matchType is p4_match_type.P4_MATCH_RANGE)):
raise NotSupportedException( raise NotSupportedException(
False, "Match type {0}", matchType) False, "Match type {0}", matchType)
...@@ -126,7 +126,7 @@ class EbpfTableKey(object): ...@@ -126,7 +126,7 @@ class EbpfTableKey(object):
def fieldRank(field): def fieldRank(field):
assert isinstance(field, EbpfTableKeyField) assert isinstance(field, EbpfTableKeyField)
return field.field.type.alignment() return field.field.type.alignment()
def serializeType(self, serializer, keyTypeName): def serializeType(self, serializer, keyTypeName):
assert isinstance(serializer, ProgramSerializer) assert isinstance(serializer, ProgramSerializer)
serializer.emitIndent() serializer.emitIndent()
...@@ -193,7 +193,7 @@ class EbpfTable(object): ...@@ -193,7 +193,7 @@ class EbpfTable(object):
assert isinstance(ctr, ebpfCounter.EbpfCounter) assert isinstance(ctr, ebpfCounter.EbpfCounter)
self.counters.append(ctr) self.counters.append(ctr)
if (len(hlirtable.attached_meters) > 0 or if (len(hlirtable.attached_meters) > 0 or
len(hlirtable.attached_registers) > 0): len(hlirtable.attached_registers) > 0):
program.emitWarning("{0}: meters/registers {1}; ignored", program.emitWarning("{0}: meters/registers {1}; ignored",
hlirtable, NotSupportedException.archError) hlirtable, NotSupportedException.archError)
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
# Compiler from P4 to EBPF # Compiler from P4 to EBPF
# (See http://www.slideshare.net/PLUMgrid/ebpf-and-linux-networking). # (See http://www.slideshare.net/PLUMgrid/ebpf-and-linux-networking).
# This compiler in fact generates a C source file # This compiler in fact generates a C source file
# which can be compiled to EBPF using the LLVM compiler # which can be compiled to EBPF using the LLVM compiler
# with the ebpf target. # with the ebpf target.
# #
# Main entry point. # Main entry point.
......
...@@ -113,7 +113,7 @@ class KernelSamplesConfig(TargetConfig): ...@@ -113,7 +113,7 @@ class KernelSamplesConfig(TargetConfig):
#include "bpf_helpers.h" #include "bpf_helpers.h"
""" """
# Represents a target compiled by bcc that uses the TC # Represents a target compiled by bcc that uses the TC
class BccConfig(TargetConfig): class BccConfig(TargetConfig):
def __init__(self): def __init__(self):
......
...@@ -13,7 +13,7 @@ class EbpfTypeFactory(object): ...@@ -13,7 +13,7 @@ class EbpfTypeFactory(object):
name = hlirType.name name = hlirType.name
if hlirType.name in self.type_map: if hlirType.name in self.type_map:
retval = self.type_map[name] retval = self.type_map[name]
if ((not asMetadata and isinstance(retval, EbpfMetadataType)) or if ((not asMetadata and isinstance(retval, EbpfMetadataType)) or
(asMetadata and isinstance(retval, EbpfHeaderType))): (asMetadata and isinstance(retval, EbpfHeaderType))):
raise CompilationException( raise CompilationException(
True, "Same type used both as a header and metadata {0}", True, "Same type used both as a header and metadata {0}",
......
...@@ -26,7 +26,7 @@ class Base(object): ...@@ -26,7 +26,7 @@ class Base(object):
def message(self, *args): def message(self, *args):
if self.verbose: if self.verbose:
print(*args) print(*args)
class Endpoint(Base): class Endpoint(Base):
# a network interface really # a network interface really
...@@ -47,7 +47,7 @@ class Endpoint(Base): ...@@ -47,7 +47,7 @@ class Endpoint(Base):
def get_ip_address(self): def get_ip_address(self):
return IPAddress(self.ipaddress) return IPAddress(self.ipaddress)
class Node(Base): class Node(Base):
# Used to represent one of clt, sw, srv # Used to represent one of clt, sw, srv
# Each lives in its own namespace # Each lives in its own namespace
...@@ -67,7 +67,7 @@ class Node(Base): ...@@ -67,7 +67,7 @@ class Node(Base):
def get_ns_name(self): def get_ns_name(self):
return self.name return self.name
def get_ns(self): def get_ns(self):
nsname = self.get_ns_name() nsname = self.get_ns_name()
ns = NetNS(nsname) ns = NetNS(nsname)
...@@ -100,7 +100,7 @@ class NetworkBase(Base): ...@@ -100,7 +100,7 @@ class NetworkBase(Base):
Base.__init__(self) Base.__init__(self)
self.ipr = IPRoute() self.ipr = IPRoute()
self.nodes = [] self.nodes = []
def add_node(self, node): def add_node(self, node):
assert isinstance(node, Node) assert isinstance(node, Node)
self.nodes.append(node) self.nodes.append(node)
...@@ -123,7 +123,7 @@ class NetworkBase(Base): ...@@ -123,7 +123,7 @@ class NetworkBase(Base):
# Ask a node to set the specified interface address # Ask a node to set the specified interface address
if address is None: if address is None:
return return
assert isinstance(node, Node) assert isinstance(node, Node)
command = ["ip", "addr", "add", str(address) + "/" + str(mask), command = ["ip", "addr", "add", str(address) + "/" + str(mask),
"dev", str(ifname)] "dev", str(ifname)]
...@@ -132,7 +132,7 @@ class NetworkBase(Base): ...@@ -132,7 +132,7 @@ class NetworkBase(Base):
def create_link(self, src, dest): def create_link(self, src, dest):
assert isinstance(src, Endpoint) assert isinstance(src, Endpoint)
assert isinstance(dest, Endpoint) assert isinstance(dest, Endpoint)
ifname = self.get_interface_name(src.parent, dest.parent) ifname = self.get_interface_name(src.parent, dest.parent)
destname = self.get_interface_name(dest.parent, src.parent) destname = self.get_interface_name(dest.parent, src.parent)
...@@ -150,7 +150,7 @@ class NetworkBase(Base): ...@@ -150,7 +150,7 @@ class NetworkBase(Base):
# lost of set prior to moving to namespace # lost of set prior to moving to namespace
self.set_interface_ipaddress( self.set_interface_ipaddress(
src.parent, ifname, src.ipaddress , src.prefixlen) src.parent, ifname, src.ipaddress , src.prefixlen)
# Sef destination endpoint information # Sef destination endpoint information
ix = self.get_interface(destname) ix = self.get_interface(destname)
self.ipr.link("set", index=ix, address=dest.mac_addr) self.ipr.link("set", index=ix, address=dest.mac_addr)
...@@ -178,7 +178,7 @@ class NetworkBase(Base): ...@@ -178,7 +178,7 @@ class NetworkBase(Base):
n.remove() n.remove()
self.ipr.close() self.ipr.close()
### Here begins the concrete instantiation of the network ### Here begins the concrete instantiation of the network
# Network setup: # Network setup:
# Each of these is a separate namespace. # Each of these is a separate namespace.
...@@ -191,7 +191,7 @@ class NetworkBase(Base): ...@@ -191,7 +191,7 @@ class NetworkBase(Base):
# ---------- -------- --------- # ---------- -------- ---------
# 10.0.0.11 10.0.0.10 # 10.0.0.11 10.0.0.10
# #
class SimulatedNetwork(NetworkBase): class SimulatedNetwork(NetworkBase):
def __init__(self): def __init__(self):
NetworkBase.__init__(self) NetworkBase.__init__(self)
...@@ -219,7 +219,7 @@ class SimulatedNetwork(NetworkBase): ...@@ -219,7 +219,7 @@ class SimulatedNetwork(NetworkBase):
assert isinstance(node, Node) assert isinstance(node, Node)
assert isinstance(args, list) assert isinstance(args, list)
torun = __file__ torun = __file__
args.insert(0, torun) args.insert(0, torun)
args.insert(1, method) args.insert(1, method)
return node.execute(args) # runs the command argv[0] method args return node.execute(args) # runs the command argv[0] method args
...@@ -240,7 +240,7 @@ class SimulatedNetwork(NetworkBase): ...@@ -240,7 +240,7 @@ class SimulatedNetwork(NetworkBase):
self.message("Set ARP mappings") self.message("Set ARP mappings")
self.client.set_arp(self.server_endpoint) self.client.set_arp(self.server_endpoint)
self.server.set_arp(self.client_endpoint) self.server.set_arp(self.client_endpoint)
def setup_switch(self): def setup_switch(self):
# This method is run in the switch namespace. # This method is run in the switch namespace.
self.message("Compiling and loading BPF program") self.message("Compiling and loading BPF program")
...@@ -254,7 +254,7 @@ class SimulatedNetwork(NetworkBase): ...@@ -254,7 +254,7 @@ class SimulatedNetwork(NetworkBase):
routing_tbl = b.get_table("routing") routing_tbl = b.get_table("routing")
routing_miss_tbl = b.get_table("ebpf_routing_miss") routing_miss_tbl = b.get_table("ebpf_routing_miss")
cnt_tbl = b.get_table("cnt") cnt_tbl = b.get_table("cnt")
self.message("Hooking up BPF classifiers using TC") self.message("Hooking up BPF classifiers using TC")
interfname = self.get_interface_name(self.switch, self.server) interfname = self.get_interface_name(self.switch, self.server)
...@@ -268,7 +268,7 @@ class SimulatedNetwork(NetworkBase): ...@@ -268,7 +268,7 @@ class SimulatedNetwork(NetworkBase):
self.ipr.tc("add", "ingress", sw_clt_idx, "ffff:") self.ipr.tc("add", "ingress", sw_clt_idx, "ffff:")
self.ipr.tc("add-filter", "bpf", sw_clt_idx, ":1", fd=fn.fd, self.ipr.tc("add-filter", "bpf", sw_clt_idx, ":1", fd=fn.fd,
name=fn.name, parent="ffff:", action="ok", classid=1) name=fn.name, parent="ffff:", action="ok", classid=1)
self.message("Populating tables from the control plane") self.message("Populating tables from the control plane")
cltip = self.client_endpoint.get_ip_address() cltip = self.client_endpoint.get_ip_address()
srvip = self.server_endpoint.get_ip_address() srvip = self.server_endpoint.get_ip_address()
...@@ -276,17 +276,17 @@ class SimulatedNetwork(NetworkBase): ...@@ -276,17 +276,17 @@ class SimulatedNetwork(NetworkBase):
# BCC does not support tbl.Leaf when the type contains a union, # BCC does not support tbl.Leaf when the type contains a union,
# so we have to make up the value type manually. Unfortunately # so we have to make up the value type manually. Unfortunately
# these sizes are not portable... # these sizes are not portable...
class Forward(ctypes.Structure): class Forward(ctypes.Structure):
_fields_ = [("port", ctypes.c_ushort)] _fields_ = [("port", ctypes.c_ushort)]
class Nop(ctypes.Structure): class Nop(ctypes.Structure):
_fields_ = [] _fields_ = []
class Union(ctypes.Union): class Union(ctypes.Union):
_fields_ = [("nop", Nop), _fields_ = [("nop", Nop),
("forward", Forward)] ("forward", Forward)]
class Value(ctypes.Structure): class Value(ctypes.Structure):
_fields_ = [("action", ctypes.c_uint), _fields_ = [("action", ctypes.c_uint),
("u", Union)] ("u", Union)]
...@@ -301,14 +301,14 @@ class SimulatedNetwork(NetworkBase): ...@@ -301,14 +301,14 @@ class SimulatedNetwork(NetworkBase):
v1 = Value() v1 = Value()
v1.action = 1 v1.action = 1
v1.u.forward.port = sw_clt_idx v1.u.forward.port = sw_clt_idx
v2 = Value() v2 = Value()
v2.action = 1; v2.action = 1;
v2.u.forward.port = sw_srv_idx v2.u.forward.port = sw_srv_idx
routing_tbl[routing_tbl.Key(int(cltip))] = v1 routing_tbl[routing_tbl.Key(int(cltip))] = v1
routing_tbl[routing_tbl.Key(int(srvip))] = v2 routing_tbl[routing_tbl.Key(int(srvip))] = v2
self.message("Dumping table contents") self.message("Dumping table contents")
for key, leaf in routing_tbl.items(): for key, leaf in routing_tbl.items():
self.message(str(IPAddress(key.key_field_0)), self.message(str(IPAddress(key.key_field_0)),
...@@ -322,7 +322,7 @@ class SimulatedNetwork(NetworkBase): ...@@ -322,7 +322,7 @@ class SimulatedNetwork(NetworkBase):
raise Exception("Test failed") raise Exception("Test failed")
else: else:
print("Test succeeded!") print("Test succeeded!")
def prepare_switch(self): def prepare_switch(self):
self.message("Configuring switch") self.message("Configuring switch")
# Re-invokes this script in the switch namespace; # Re-invokes this script in the switch namespace;
...@@ -331,7 +331,7 @@ class SimulatedNetwork(NetworkBase): ...@@ -331,7 +331,7 @@ class SimulatedNetwork(NetworkBase):
# but in the switch namespace # but in the switch namespace
self.run_method_in_node(self.switch, "setup_switch", []) self.run_method_in_node(self.switch, "setup_switch", [])
def compile(source, destination): def compile(source, destination):
try: try:
status = subprocess.call( status = subprocess.call(
...@@ -344,7 +344,7 @@ def compile(source, destination): ...@@ -344,7 +344,7 @@ def compile(source, destination):
except OSError as e: except OSError as e:
print("Execution failed:", e, file=sys.stderr) print("Execution failed:", e, file=sys.stderr)
raise e raise e
def start_simulation(): def start_simulation():
compile("testprograms/simple.p4", "simple.c") compile("testprograms/simple.p4", "simple.c")
network = SimulatedNetwork() network = SimulatedNetwork()
......
...@@ -27,7 +27,7 @@ def set_error(kind, file, error): ...@@ -27,7 +27,7 @@ def set_error(kind, file, error):
def is_root(): def is_root():
# Is this code portable? # Is this code portable?
return os.getuid() == 0 return os.getuid() == 0
def main(): def main():
testpath = "testprograms" testpath = "testprograms"
destFolder = "testoutputs" destFolder = "testoutputs"
...@@ -40,7 +40,7 @@ def main(): ...@@ -40,7 +40,7 @@ def main():
print "Loading EBPF programs requires root priviledge." print "Loading EBPF programs requires root priviledge."
print "Will only test compilation, not loading." print "Will only test compilation, not loading."
print "(Run with sudo to test program loading.)" print "(Run with sudo to test program loading.)"
for f in files: for f in files:
path = os.path.join(testpath, f) path = os.path.join(testpath, f)
...@@ -62,14 +62,14 @@ def main(): ...@@ -62,14 +62,14 @@ def main():
else: else:
# Try to load the compiled function # Try to load the compiled function
if is_root(): if is_root():
try: try:
print("Compiling and loading BPF program") print("Compiling and loading BPF program")
b = BPF(src_file=destname, debug=0) b = BPF(src_file=destname, debug=0)
fn = b.load_func("ebpf_filter", BPF.SCHED_CLS) fn = b.load_func("ebpf_filter", BPF.SCHED_CLS)
except Exception as e: except Exception as e:
print(e) print(e)
set_error("BPF error", path, str(e)) set_error("BPF error", path, str(e))
filesDone += 1 filesDone += 1
print "Compiled", filesDone, "files", errors, "errors" print "Compiled", filesDone, "files", errors, "errors"
...@@ -78,7 +78,7 @@ def main(): ...@@ -78,7 +78,7 @@ def main():
for v in filesFailed[key]: for v in filesFailed[key]:
print "\t", v print "\t", v
exit(len(filesFailed) != 0) exit(len(filesFailed) != 0)
if __name__ == "__main__": if __name__ == "__main__":
main() main()
...@@ -346,7 +346,7 @@ static int bpf_attach_tracing_event(int progfd, const char *event_path, ...@@ -346,7 +346,7 @@ static int bpf_attach_tracing_event(int progfd, const char *event_path,
void * bpf_attach_kprobe(int progfd, enum bpf_probe_attach_type attach_type, const char *ev_name, void * bpf_attach_kprobe(int progfd, enum bpf_probe_attach_type attach_type, const char *ev_name,
const char *fn_name, const char *fn_name,
pid_t pid, int cpu, int group_fd, pid_t pid, int cpu, int group_fd,
perf_reader_cb cb, void *cb_cookie) perf_reader_cb cb, void *cb_cookie)
{ {
int kfd; int kfd;
char buf[256]; char buf[256];
...@@ -367,7 +367,7 @@ void * bpf_attach_kprobe(int progfd, enum bpf_probe_attach_type attach_type, con ...@@ -367,7 +367,7 @@ void * bpf_attach_kprobe(int progfd, enum bpf_probe_attach_type attach_type, con
goto error; goto error;
} }
snprintf(buf, sizeof(buf), "%c:%ss/%s %s", attach_type==BPF_PROBE_ENTRY ? 'p' : 'r', snprintf(buf, sizeof(buf), "%c:%ss/%s %s", attach_type==BPF_PROBE_ENTRY ? 'p' : 'r',
event_type, new_name, fn_name); event_type, new_name, fn_name);
if (write(kfd, buf, strlen(buf)) < 0) { if (write(kfd, buf, strlen(buf)) < 0) {
if (errno == EINVAL) if (errno == EINVAL)
...@@ -380,10 +380,10 @@ void * bpf_attach_kprobe(int progfd, enum bpf_probe_attach_type attach_type, con ...@@ -380,10 +380,10 @@ void * bpf_attach_kprobe(int progfd, enum bpf_probe_attach_type attach_type, con
if (access("/sys/kernel/debug/tracing/instances", F_OK) != -1) { if (access("/sys/kernel/debug/tracing/instances", F_OK) != -1) {
snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/instances/bcc_%d", getpid()); snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/instances/bcc_%d", getpid());
if (access(buf, F_OK) == -1) { if (access(buf, F_OK) == -1) {
if (mkdir(buf, 0755) == -1) if (mkdir(buf, 0755) == -1)
goto retry; goto retry;
} }
n = snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/instances/bcc_%d/events/%ss/%s", n = snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/instances/bcc_%d/events/%ss/%s",
getpid(), event_type, new_name); getpid(), event_type, new_name);
if (n < sizeof(buf) && bpf_attach_tracing_event(progfd, buf, reader, pid, cpu, group_fd) == 0) if (n < sizeof(buf) && bpf_attach_tracing_event(progfd, buf, reader, pid, cpu, group_fd) == 0)
goto out; goto out;
...@@ -406,7 +406,7 @@ error: ...@@ -406,7 +406,7 @@ error:
void * bpf_attach_uprobe(int progfd, enum bpf_probe_attach_type attach_type, const char *ev_name, void * bpf_attach_uprobe(int progfd, enum bpf_probe_attach_type attach_type, const char *ev_name,
const char *binary_path, uint64_t offset, const char *binary_path, uint64_t offset,
pid_t pid, int cpu, int group_fd, pid_t pid, int cpu, int group_fd,
perf_reader_cb cb, void *cb_cookie) perf_reader_cb cb, void *cb_cookie)
{ {
int kfd; int kfd;
char buf[PATH_MAX]; char buf[PATH_MAX];
...@@ -428,7 +428,7 @@ void * bpf_attach_uprobe(int progfd, enum bpf_probe_attach_type attach_type, con ...@@ -428,7 +428,7 @@ void * bpf_attach_uprobe(int progfd, enum bpf_probe_attach_type attach_type, con
goto error; goto error;
} }
n = snprintf(buf, sizeof(buf), "%c:%ss/%s %s:0x%lx", attach_type==BPF_PROBE_ENTRY ? 'p' : 'r', n = snprintf(buf, sizeof(buf), "%c:%ss/%s %s:0x%lx", attach_type==BPF_PROBE_ENTRY ? 'p' : 'r',
event_type, new_name, binary_path, offset); event_type, new_name, binary_path, offset);
if (n >= sizeof(buf)) { if (n >= sizeof(buf)) {
close(kfd); close(kfd);
......
...@@ -50,7 +50,7 @@ typedef void (*perf_reader_cb)(void *cb_cookie, int pid, uint64_t callchain_num, ...@@ -50,7 +50,7 @@ typedef void (*perf_reader_cb)(void *cb_cookie, int pid, uint64_t callchain_num,
typedef void (*perf_reader_raw_cb)(void *cb_cookie, void *raw, int raw_size); typedef void (*perf_reader_raw_cb)(void *cb_cookie, void *raw, int raw_size);
typedef void (*perf_reader_lost_cb)(uint64_t lost); typedef void (*perf_reader_lost_cb)(uint64_t lost);
void * bpf_attach_kprobe(int progfd, enum bpf_probe_attach_type attach_type, void * bpf_attach_kprobe(int progfd, enum bpf_probe_attach_type attach_type,
const char *ev_name, const char *fn_name, const char *ev_name, const char *fn_name,
pid_t pid, int cpu, int group_fd, pid_t pid, int cpu, int group_fd,
perf_reader_cb cb, void *cb_cookie); perf_reader_cb cb, void *cb_cookie);
......
...@@ -258,7 +258,7 @@ local function valloc(size, blank) ...@@ -258,7 +258,7 @@ local function valloc(size, blank)
stack_top = math.ceil(stack_top/8)*8 stack_top = math.ceil(stack_top/8)*8
-- Current kernel version doesn't support ARG_PTR_TO_RAW_STACK -- Current kernel version doesn't support ARG_PTR_TO_RAW_STACK
-- so we always need to have memory initialized, remove this when supported -- so we always need to have memory initialized, remove this when supported
if blank then if blank then
if type(blank) == 'string' then if type(blank) == 'string' then
local sp = 0 local sp = 0
while sp < size do while sp < size do
...@@ -276,7 +276,7 @@ local function valloc(size, blank) ...@@ -276,7 +276,7 @@ local function valloc(size, blank)
emit(BPF.MEM + BPF.STX + BPF.DW, 10, 0, -sp, 0) emit(BPF.MEM + BPF.STX + BPF.DW, 10, 0, -sp, 0)
end end
else error('NYI: will with unknown type '..type(blank)) end else error('NYI: will with unknown type '..type(blank)) end
end end
return stack_top return stack_top
end end
...@@ -604,7 +604,7 @@ local function MAP_DEL(map_var, key, key_imm) ...@@ -604,7 +604,7 @@ local function MAP_DEL(map_var, key, key_imm)
end end
local function MAP_SET(map_var, key, key_imm, src) local function MAP_SET(map_var, key, key_imm, src)
local map = V[map_var].const local map = V[map_var].const
-- Delete when setting nil -- Delete when setting nil
if V[src].type == ffi.typeof('void') then if V[src].type == ffi.typeof('void') then
return MAP_DEL(map_var, key, key_imm) return MAP_DEL(map_var, key, key_imm)
......
...@@ -205,7 +205,7 @@ builtins[ffi.copy] = function (e, ret, dst, src) ...@@ -205,7 +205,7 @@ builtins[ffi.copy] = function (e, ret, dst, src)
-- TODO: identify cheap register move -- TODO: identify cheap register move
-- TODO: identify copy to/from stack -- TODO: identify copy to/from stack
error('NYI: ffi.copy(dst, src) - src is neither BPF map/socket buffer or probe') error('NYI: ffi.copy(dst, src) - src is neither BPF map/socket buffer or probe')
end end
end end
-- print(format, ...) builtin changes semantics from Lua print(...) -- print(format, ...) builtin changes semantics from Lua print(...)
-- the first parameter has to be format and only reduced set of conversion specificers -- the first parameter has to be format and only reduced set of conversion specificers
......
...@@ -170,7 +170,7 @@ function M.typename(v) ...@@ -170,7 +170,7 @@ function M.typename(v)
return string.match(tostring(ffi.typeof(v)), '<([^>]+)') return string.match(tostring(ffi.typeof(v)), '<([^>]+)')
end end
-- Reflect if cdata type can be pointer (accepts array or pointer) -- Reflect if cdata type can be pointer (accepts array or pointer)
function M.isptr(v, noarray) function M.isptr(v, noarray)
local ctname = M.typename(v) local ctname = M.typename(v)
if ctname then if ctname then
......
...@@ -227,7 +227,7 @@ ffi.metatype('struct Elf_object', { ...@@ -227,7 +227,7 @@ ffi.metatype('struct Elf_object', {
end end
-- Match symbol name against pattern -- Match symbol name against pattern
if pattern and string.match(name, k) or k == name then if pattern and string.match(name, k) or k == name then
return sym[0] return sym[0]
end end
end end
end end
......
...@@ -222,7 +222,7 @@ local function next_offset(e, var, type, off, mask, shift) ...@@ -222,7 +222,7 @@ local function next_offset(e, var, type, off, mask, shift)
-- Finalize relative offset -- Finalize relative offset
if mask then if mask then
e.emit(BPF.ALU + BPF.AND + BPF.K, tmp_reg, 0, 0, mask) e.emit(BPF.ALU + BPF.AND + BPF.K, tmp_reg, 0, 0, mask)
end end
if shift then if shift then
local op = BPF.LSH local op = BPF.LSH
if shift < 0 then if shift < 0 then
......
...@@ -27,7 +27,7 @@ repeat = """ ...@@ -27,7 +27,7 @@ repeat = """
end = """ end = """
y = t1.lookup(&x); y = t1.lookup(&x);
x = *y; x = *y;
return 0; return 0;
} }
""" """
for i in range(0,300): for i in range(0,300):
...@@ -42,7 +42,7 @@ class TestBPFProgLoad(TestCase): ...@@ -42,7 +42,7 @@ class TestBPFProgLoad(TestCase):
def tearDown(self): def tearDown(self):
self.fp.close() self.fp.close()
def test_log_debug(self): def test_log_debug(self):
b = BPF(text=text, debug=2) b = BPF(text=text, debug=2)
......
...@@ -130,7 +130,7 @@ static int br_common(struct __sk_buff *skb, int which_br) { ...@@ -130,7 +130,7 @@ static int br_common(struct __sk_buff *skb, int which_br) {
index = 0; index = 0;
if (which_br == 1) if (which_br == 1)
rtrif_p = br1_rtr.lookup(&index); rtrif_p = br1_rtr.lookup(&index);
else else
rtrif_p = br2_rtr.lookup(&index); rtrif_p = br2_rtr.lookup(&index);
if (rtrif_p) if (rtrif_p)
bpf_clone_redirect(skb, *rtrif_p, 0); bpf_clone_redirect(skb, *rtrif_p, 0);
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
# #
# The vm1, vm2 and router are implemented as namespaces. # The vm1, vm2 and router are implemented as namespaces.
# The bridge is implemented with limited functionality in bpf program. # The bridge is implemented with limited functionality in bpf program.
# #
# vm1 and vm2 are in different subnet. For vm1 to communicate to vm2, # vm1 and vm2 are in different subnet. For vm1 to communicate to vm2,
# the packet will have to travel from vm1 to pem, bridge1, router, bridge2, pem, and # the packet will have to travel from vm1 to pem, bridge1, router, bridge2, pem, and
# then come to vm2. # then come to vm2.
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
# 8: PING 200.1.1.1 (200.1.1.1) 56(84) bytes of data. # 8: PING 200.1.1.1 (200.1.1.1) 56(84) bytes of data.
# 8: 64 bytes from 200.1.1.1: icmp_req=1 ttl=63 time=0.074 ms # 8: 64 bytes from 200.1.1.1: icmp_req=1 ttl=63 time=0.074 ms
# 8: 64 bytes from 200.1.1.1: icmp_req=2 ttl=63 time=0.061 ms # 8: 64 bytes from 200.1.1.1: icmp_req=2 ttl=63 time=0.061 ms
# 8: # 8:
# 8: --- 200.1.1.1 ping statistics --- # 8: --- 200.1.1.1 ping statistics ---
# 8: 2 packets transmitted, 2 received, 0% packet loss, time 999ms # 8: 2 packets transmitted, 2 received, 0% packet loss, time 999ms
# 8: rtt min/avg/max/mdev = 0.061/0.067/0.074/0.010 ms # 8: rtt min/avg/max/mdev = 0.061/0.067/0.074/0.010 ms
...@@ -41,24 +41,24 @@ ...@@ -41,24 +41,24 @@
# 8: [ 5] 0.0- 1.0 sec 4.00 GBytes 34.3 Gbits/sec # 8: [ 5] 0.0- 1.0 sec 4.00 GBytes 34.3 Gbits/sec
# 8: Starting netserver with host 'IN(6)ADDR_ANY' port '12865' and family AF_UNSPEC # 8: Starting netserver with host 'IN(6)ADDR_ANY' port '12865' and family AF_UNSPEC
# 8: MIGRATED TCP STREAM TEST from 0.0.0.0 (0.0.0.0) port 0 AF_INET to 200.1.1.1 (200.1.1.1) port 0 AF_INET : demo # 8: MIGRATED TCP STREAM TEST from 0.0.0.0 (0.0.0.0) port 0 AF_INET to 200.1.1.1 (200.1.1.1) port 0 AF_INET : demo
# 8: Recv Send Send # 8: Recv Send Send
# 8: Socket Socket Message Elapsed # 8: Socket Socket Message Elapsed
# 8: Size Size Size Time Throughput # 8: Size Size Size Time Throughput
# 8: bytes bytes bytes secs. 10^6bits/sec # 8: bytes bytes bytes secs. 10^6bits/sec
# 8: # 8:
# 8: 87380 16384 65160 1.00 41991.68 # 8: 87380 16384 65160 1.00 41991.68
# 8: MIGRATED TCP REQUEST/RESPONSE TEST from 0.0.0.0 (0.0.0.0) port 0 AF_INET to 200.1.1.1 (200.1.1.1) port 0 AF_INET : demo : first burst 0 # 8: MIGRATED TCP REQUEST/RESPONSE TEST from 0.0.0.0 (0.0.0.0) port 0 AF_INET to 200.1.1.1 (200.1.1.1) port 0 AF_INET : demo : first burst 0
# 8: Local /Remote # 8: Local /Remote
# 8: Socket Size Request Resp. Elapsed Trans. # 8: Socket Size Request Resp. Elapsed Trans.
# 8: Send Recv Size Size Time Rate # 8: Send Recv Size Size Time Rate
# 8: bytes Bytes bytes bytes secs. per sec # 8: bytes Bytes bytes bytes secs. per sec
# 8: # 8:
# 8: 16384 87380 1 1 1.00 48645.53 # 8: 16384 87380 1 1 1.00 48645.53
# 8: 16384 87380 # 8: 16384 87380
# 8: . # 8: .
# 8: ---------------------------------------------------------------------- # 8: ----------------------------------------------------------------------
# 8: Ran 1 test in 11.296s # 8: Ran 1 test in 11.296s
# 8: # 8:
# 8: OK # 8: OK
from ctypes import c_uint from ctypes import c_uint
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
# The vm1, vm2 and router are implemented as namespaces. # The vm1, vm2 and router are implemented as namespaces.
# The linux bridge device is used to provice bridge functionality. # The linux bridge device is used to provice bridge functionality.
# pem bpf will be attached to related network devices for vm1, vm1, bridge1 and bridge2. # pem bpf will be attached to related network devices for vm1, vm1, bridge1 and bridge2.
# #
# vm1 and vm2 are in different subnet. For vm1 to communicate to vm2, # vm1 and vm2 are in different subnet. For vm1 to communicate to vm2,
# the packet will have to travel from vm1 to pem, bridge1, router, bridge2, pem, and # the packet will have to travel from vm1 to pem, bridge1, router, bridge2, pem, and
# then come to vm2. # then come to vm2.
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
# 9: PING 200.1.1.1 (200.1.1.1) 56(84) bytes of data. # 9: PING 200.1.1.1 (200.1.1.1) 56(84) bytes of data.
# 9: 64 bytes from 200.1.1.1: icmp_req=1 ttl=63 time=0.090 ms # 9: 64 bytes from 200.1.1.1: icmp_req=1 ttl=63 time=0.090 ms
# 9: 64 bytes from 200.1.1.1: icmp_req=2 ttl=63 time=0.032 ms # 9: 64 bytes from 200.1.1.1: icmp_req=2 ttl=63 time=0.032 ms
# 9: # 9:
# 9: --- 200.1.1.1 ping statistics --- # 9: --- 200.1.1.1 ping statistics ---
# 9: 2 packets transmitted, 2 received, 0% packet loss, time 999ms # 9: 2 packets transmitted, 2 received, 0% packet loss, time 999ms
# 9: rtt min/avg/max/mdev = 0.032/0.061/0.090/0.029 ms # 9: rtt min/avg/max/mdev = 0.032/0.061/0.090/0.029 ms
...@@ -34,24 +34,24 @@ ...@@ -34,24 +34,24 @@
# 9: [ 5] 0.0- 1.0 sec 3.80 GBytes 32.6 Gbits/sec # 9: [ 5] 0.0- 1.0 sec 3.80 GBytes 32.6 Gbits/sec
# 9: Starting netserver with host 'IN(6)ADDR_ANY' port '12865' and family AF_UNSPEC # 9: Starting netserver with host 'IN(6)ADDR_ANY' port '12865' and family AF_UNSPEC
# 9: MIGRATED TCP STREAM TEST from 0.0.0.0 (0.0.0.0) port 0 AF_INET to 200.1.1.1 (200.1.1.1) port 0 AF_INET : demo # 9: MIGRATED TCP STREAM TEST from 0.0.0.0 (0.0.0.0) port 0 AF_INET to 200.1.1.1 (200.1.1.1) port 0 AF_INET : demo
# 9: Recv Send Send # 9: Recv Send Send
# 9: Socket Socket Message Elapsed # 9: Socket Socket Message Elapsed
# 9: Size Size Size Time Throughput # 9: Size Size Size Time Throughput
# 9: bytes bytes bytes secs. 10^6bits/sec # 9: bytes bytes bytes secs. 10^6bits/sec
# 9: # 9:
# 9: 87380 16384 65160 1.00 39940.46 # 9: 87380 16384 65160 1.00 39940.46
# 9: MIGRATED TCP REQUEST/RESPONSE TEST from 0.0.0.0 (0.0.0.0) port 0 AF_INET to 200.1.1.1 (200.1.1.1) port 0 AF_INET : demo : first burst 0 # 9: MIGRATED TCP REQUEST/RESPONSE TEST from 0.0.0.0 (0.0.0.0) port 0 AF_INET to 200.1.1.1 (200.1.1.1) port 0 AF_INET : demo : first burst 0
# 9: Local /Remote # 9: Local /Remote
# 9: Socket Size Request Resp. Elapsed Trans. # 9: Socket Size Request Resp. Elapsed Trans.
# 9: Send Recv Size Size Time Rate # 9: Send Recv Size Size Time Rate
# 9: bytes Bytes bytes bytes secs. per sec # 9: bytes Bytes bytes bytes secs. per sec
# 9: # 9:
# 9: 16384 87380 1 1 1.00 46387.80 # 9: 16384 87380 1 1 1.00 46387.80
# 9: 16384 87380 # 9: 16384 87380
# 9: . # 9: .
# 9: ---------------------------------------------------------------------- # 9: ----------------------------------------------------------------------
# 9: Ran 1 test in 7.495s # 9: Ran 1 test in 7.495s
# 9: # 9:
# 9: OK # 9: OK
from ctypes import c_uint from ctypes import c_uint
...@@ -87,7 +87,7 @@ class TestBPFSocket(TestCase): ...@@ -87,7 +87,7 @@ class TestBPFSocket(TestCase):
ipdb.interfaces[veth_br_2_pem].up().commit() ipdb.interfaces[veth_br_2_pem].up().commit()
subprocess.call(["sysctl", "-q", "-w", "net.ipv6.conf." + veth_pem_2_br + ".disable_ipv6=1"]) subprocess.call(["sysctl", "-q", "-w", "net.ipv6.conf." + veth_pem_2_br + ".disable_ipv6=1"])
subprocess.call(["sysctl", "-q", "-w", "net.ipv6.conf." + veth_br_2_pem + ".disable_ipv6=1"]) subprocess.call(["sysctl", "-q", "-w", "net.ipv6.conf." + veth_br_2_pem + ".disable_ipv6=1"])
# set up the bridge and add router interface as one of its slaves # set up the bridge and add router interface as one of its slaves
with ipdb.create(ifname=br, kind="bridge") as br1: with ipdb.create(ifname=br, kind="bridge") as br1:
br1.add_port(ipdb.interfaces[veth_pem_2_br]) br1.add_port(ipdb.interfaces[veth_pem_2_br])
......
...@@ -84,7 +84,7 @@ int count(struct pt_regs *ctx) { ...@@ -84,7 +84,7 @@ int count(struct pt_regs *ctx) {
libc = ctypes.CDLL("libc.so.6", use_errno=True) libc = ctypes.CDLL("libc.so.6", use_errno=True)
# Need to find path to libz.so.1 # Need to find path to libz.so.1
libz_path = None libz_path = None
p = subprocess.Popen(["ldconfig", "-p"], stdout=subprocess.PIPE) p = subprocess.Popen(["ldconfig", "-p"], stdout=subprocess.PIPE)
for l in p.stdout: for l in p.stdout:
n = l.split() n = l.split()
...@@ -102,7 +102,7 @@ int count(struct pt_regs *ctx) { ...@@ -102,7 +102,7 @@ int count(struct pt_regs *ctx) {
if libc.unshare(0x00020000) == -1: if libc.unshare(0x00020000) == -1:
e = ctypes.get_errno() e = ctypes.get_errno()
raise OSError(e, errno.errorcode[e]) raise OSError(e, errno.errorcode[e])
# Remount root MS_REC|MS_PRIVATE # Remount root MS_REC|MS_PRIVATE
if libc.mount(None, "/", None, (1<<14)|(1<<18) , None) == -1: if libc.mount(None, "/", None, (1<<14)|(1<<18) , None) == -1:
e = ctypes.get_errno() e = ctypes.get_errno()
...@@ -119,7 +119,7 @@ int count(struct pt_regs *ctx) { ...@@ -119,7 +119,7 @@ int count(struct pt_regs *ctx) {
libz.zlibVersion() libz.zlibVersion()
time.sleep(5) time.sleep(5)
os._exit(0) os._exit(0)
libname = "/tmp/libz.so.1" libname = "/tmp/libz.so.1"
symname = "zlibVersion" symname = "zlibVersion"
text = text.replace("PID", "%d" % child_pid) text = text.replace("PID", "%d" % child_pid)
...@@ -131,6 +131,6 @@ int count(struct pt_regs *ctx) { ...@@ -131,6 +131,6 @@ int count(struct pt_regs *ctx) {
b.detach_uretprobe(name=libname, sym=symname, pid=child_pid) b.detach_uretprobe(name=libname, sym=symname, pid=child_pid)
b.detach_uprobe(name=libname, sym=symname, pid=child_pid) b.detach_uprobe(name=libname, sym=symname, pid=child_pid)
os.wait() os.wait()
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()
...@@ -30,7 +30,7 @@ examples = """examples: ...@@ -30,7 +30,7 @@ examples = """examples:
./execsnoop -x # include failed exec()s ./execsnoop -x # include failed exec()s
./execsnoop -t # include timestamps ./execsnoop -t # include timestamps
./execsnoop -n main # only print command lines containing "main" ./execsnoop -n main # only print command lines containing "main"
./execsnoop -l tpkg # only print command where arguments contains "tpkg" ./execsnoop -l tpkg # only print command where arguments contains "tpkg"
""" """
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="Trace exec() syscalls", description="Trace exec() syscalls",
......
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