-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Filter implementation for packetcapture service
- Loading branch information
riccardo
committed
Feb 22, 2020
1 parent
b0be98d
commit 6f81c12
Showing
29 changed files
with
1,273 additions
and
1,870 deletions.
There are no files selected for viewing
This file contains 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 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
65 changes: 65 additions & 0 deletions
65
Documentation/services/pcn-packetcapture/packetcapture-filter.rst
This file contains 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,65 @@ | ||
Packetcapture service filter | ||
============================ | ||
|
||
Packetcapture filter is a tcpdump like filter that allows to insert a specific rule using tcpdump syntax. | ||
|
||
|
||
How it needs | ||
------------ | ||
In order to use it you have to install **libpcap-dev** | ||
|
||
:: | ||
|
||
sudo apt-get install libpcap-dev | ||
|
||
|
||
How it works | ||
------------ | ||
The pipeline to convert the filter entered in the packetcapture service to C code is: | ||
|
||
**pcap filter** → *libpcap* → **cBPF** → *cbpf2c* → **C code** | ||
|
||
|
||
More in details, the first step is to obtain the cBPF code from the string value of the filter inserted, | ||
it can be done using the pcap_compile_nopcap function that returns a bpf_program structure containing the bpf_insn. | ||
|
||
Then will be created a sock_fprog structure called cbpf that will contains all the filter blocks needed. | ||
|
||
The second step (traslation from cBPF to C) starts with the validation of the cBPF code, and then for each filter block | ||
is called _cbpf_dump function that will return a string containing the equivalent C code for that block. | ||
|
||
Inside the _cbpf_dump function there is a switch statement that will prepare two variables, op (operation) and fmt (operand), | ||
depending on the type of instruction of the block (e.g.,return, load, store, alu op. etc.), then they will be used to | ||
generate the C code | ||
|
||
For reference see: `cloudflare project <https://blog.cloudflare.com/xdpcap/>`__ | ||
|
||
|
||
Example of C code generated | ||
--------------------------- | ||
Here is the generated C code for the filter "icmp": | ||
|
||
:: | ||
|
||
L0: //(ldh) | ||
if((data + 14) > data_end){ | ||
return RX_DROP; | ||
} | ||
a = ntohs(* ((uint16_t *) &data[12])); | ||
L1: if(a == 0x0800) { | ||
goto L2; | ||
}else{ | ||
goto L5; | ||
} | ||
L2: //(ldb) | ||
if((data + 24) > data_end){ | ||
return RX_DROP; | ||
} | ||
a = * ((uint8_t *) &data[23]); | ||
L3: if(a == 0x01) { | ||
goto L4; | ||
}else{ | ||
goto L5; | ||
} | ||
L4: return pcn_pkt_controller(ctx, md, reason); | ||
L5: return RX_OK; |
This file contains 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
60 changes: 60 additions & 0 deletions
60
src/libs/polycube/include/polycube/services/bcc_exception.h
This file contains 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,60 @@ | ||
/* | ||
* 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <cstdio> | ||
#include <string> | ||
|
||
namespace ebpf { | ||
|
||
class StatusTuple { | ||
public: | ||
StatusTuple(int ret) : ret_(ret) {} | ||
|
||
StatusTuple(int ret, const char *msg) : ret_(ret), msg_(msg) {} | ||
|
||
StatusTuple(int ret, const std::string &msg) : ret_(ret), msg_(msg) {} | ||
|
||
template <typename... Args> | ||
StatusTuple(int ret, const char *fmt, Args... args) : ret_(ret) { | ||
char buf[2048]; | ||
snprintf(buf, sizeof(buf), fmt, args...); | ||
msg_ = std::string(buf); | ||
} | ||
|
||
void append_msg(const std::string& msg) { | ||
msg_ += msg; | ||
} | ||
|
||
int code() { return ret_; } | ||
|
||
std::string msg() { return msg_; } | ||
|
||
private: | ||
int ret_; | ||
std::string msg_; | ||
}; | ||
|
||
#define TRY2(CMD) \ | ||
do { \ | ||
StatusTuple __stp = (CMD); \ | ||
if (__stp.code() != 0) { \ | ||
return __stp; \ | ||
} \ | ||
} while (0) | ||
|
||
} // namespace ebpf |
This file contains 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,67 @@ | ||
/* | ||
* Copyright (c) 2017 Facebook, 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <unistd.h> | ||
|
||
namespace ebpf { | ||
|
||
/// FileDesc is a helper class for managing open file descriptors. Copy is | ||
/// disallowed (call dup instead), and cleanup happens automatically. | ||
class FileDesc { | ||
public: | ||
explicit FileDesc(int fd = -1) : fd_(fd) {} | ||
FileDesc(FileDesc &&that) : fd_(-1) { *this = std::move(that); } | ||
FileDesc(const FileDesc &that) = delete; | ||
|
||
~FileDesc() { | ||
if (fd_ >= 0) | ||
::close(fd_); | ||
} | ||
|
||
FileDesc &operator=(int fd) { | ||
if (fd_ >= 0) | ||
::close(fd_); | ||
fd_ = fd; | ||
return *this; | ||
} | ||
FileDesc &operator=(FileDesc &&that) { | ||
if (fd_ >= 0) | ||
::close(fd_); | ||
fd_ = that.fd_; | ||
that.fd_ = -1; | ||
return *this; | ||
} | ||
FileDesc &operator=(const FileDesc &that) = delete; | ||
|
||
FileDesc dup() const { | ||
if (fd_ >= 0) { | ||
int dup_fd = ::dup(fd_); | ||
return FileDesc(dup_fd); | ||
} else { | ||
return FileDesc(-1); | ||
} | ||
} | ||
|
||
operator int() { return fd_; } | ||
operator int() const { return fd_; } | ||
|
||
private: | ||
int fd_; | ||
}; | ||
|
||
} // namespace ebpf |
Oops, something went wrong.