#include "misc.hh"
-static __u64 ptr_to_u64(const void *ptr)
+static __u64 ptr_to_u64(const void* ptr)
{
- return (__u64) (unsigned long) ptr;
+ return (__u64)(unsigned long)ptr;
}
/* these can be static as they are not declared in libbpf.h: */
return syscall(SYS_bpf, BPF_MAP_CREATE, &attr, sizeof(attr));
}
-static int bpf_update_elem(int descriptor, void *key, void *value, unsigned long long flags)
+static int bpf_update_elem(int descriptor, void* key, void* value, unsigned long long flags)
{
union bpf_attr attr;
memset(&attr, 0, sizeof(attr));
return syscall(SYS_bpf, BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
}
-static int bpf_lookup_elem(int descriptor, void *key, void *value)
+static int bpf_lookup_elem(int descriptor, void* key, void* value)
{
union bpf_attr attr;
memset(&attr, 0, sizeof(attr));
return syscall(SYS_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
}
-static int bpf_delete_elem(int descriptor, void *key)
+static int bpf_delete_elem(int descriptor, void* key)
{
union bpf_attr attr;
memset(&attr, 0, sizeof(attr));
return syscall(SYS_bpf, BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
}
-static int bpf_get_next_key(int descriptor, void *key, void *next_key)
+static int bpf_get_next_key(int descriptor, void* key, void* next_key)
{
union bpf_attr attr;
memset(&attr, 0, sizeof(attr));
}
static int bpf_prog_load(enum bpf_prog_type prog_type,
- const struct bpf_insn *insns, size_t prog_len,
- const char *license, int kern_version)
+ const struct bpf_insn* insns, size_t prog_len,
+ const char* license, int kern_version)
{
char log_buf[65535];
union bpf_attr attr;
memset(&attr, 0, sizeof(attr));
attr.prog_type = prog_type;
- attr.insns = ptr_to_u64((void *) insns);
+ attr.insns = ptr_to_u64((void*)insns);
attr.insn_cnt = static_cast<int>(prog_len / sizeof(struct bpf_insn));
- attr.license = ptr_to_u64((void *) license);
+ attr.license = ptr_to_u64((void*)license);
attr.log_buf = ptr_to_u64(log_buf);
attr.log_size = sizeof(log_buf);
attr.log_level = 1;
uint16_t qtype{0};
};
-
-BPFFilter::Map::Map(const BPFFilter::MapConfiguration& config, BPFFilter::MapFormat format): d_config(config)
+BPFFilter::Map::Map(const BPFFilter::MapConfiguration& config, BPFFilter::MapFormat format) :
+ d_config(config)
{
if (d_config.d_type == BPFFilter::MapType::Filters) {
/* special case, this is a map of eBPF programs */
return descriptor;
}
-
BPFFilter::BPFFilter(std::unordered_map<std::string, MapConfiguration>& configs, BPFFilter::MapFormat format, bool external) :
d_mapFormat(format), d_external(external)
{
}
}
-std::vector<std::pair<ComboAddress, uint64_t> > BPFFilter::getAddrStats()
+std::vector<std::pair<ComboAddress, uint64_t>> BPFFilter::getAddrStats()
{
- std::vector<std::pair<ComboAddress, uint64_t> > result;
+ std::vector<std::pair<ComboAddress, uint64_t>> result;
{
auto maps = d_maps.lock();
result.reserve(maps->d_v4.d_count + maps->d_v6.d_count);
while (res == 0) {
v4Key = nextV4Key;
if (bpf_lookup_elem(map.d_fd.getHandle(), &v4Key, &value) == 0) {
- v4Addr.sin_addr.s_addr = ntohl(v4Key);
- result.emplace_back(ComboAddress(&v4Addr), value.counter);
+ v4Addr.sin_addr.s_addr = ntohl(v4Key);
+ result.emplace_back(ComboAddress(&v4Addr), value.counter);
}
res = bpf_get_next_key(map.d_fd.getHandle(), &v4Key, &nextV4Key);
return result;
}
-std::vector<std::tuple<DNSName, uint16_t, uint64_t> > BPFFilter::getQNameStats()
+std::vector<std::tuple<DNSName, uint16_t, uint64_t>> BPFFilter::getQNameStats()
{
- std::vector<std::tuple<DNSName, uint16_t, uint64_t> > result;
+ std::vector<std::tuple<DNSName, uint16_t, uint64_t>> result;
if (d_mapFormat == MapFormat::Legacy) {
- QNameKey key = { { 0 } };
- QNameKey nextKey = { { 0 } };
+ QNameKey key = {{0}};
+ QNameKey nextKey = {{0}};
QNameValue value;
auto maps = d_maps.lock();
while (res == 0) {
if (bpf_lookup_elem(map.d_fd.getHandle(), &nextKey, &value) == 0) {
- nextKey.qname[sizeof(nextKey.qname) - 1 ] = '\0';
+ nextKey.qname[sizeof(nextKey.qname) - 1] = '\0';
result.emplace_back(DNSName(reinterpret_cast<const char*>(nextKey.qname), sizeof(nextKey.qname), 0, false), value.qtype, value.counter);
}
while (res == 0) {
if (bpf_lookup_elem(map.d_fd.getHandle(), &nextKey, &value) == 0) {
- nextKey.qname[sizeof(nextKey.qname) - 1 ] = '\0';
+ nextKey.qname[sizeof(nextKey.qname) - 1] = '\0';
result.emplace_back(DNSName(reinterpret_cast<const char*>(nextKey.qname), sizeof(nextKey.qname), 0, false), key.qtype, value.counter);
}
throw std::runtime_error("eBPF support not enabled");
}
-std::vector<std::pair<Netmask, CounterAndActionValue>> BPFFilter::getRangeRule(){
+std::vector<std::pair<Netmask, CounterAndActionValue>> BPFFilter::getRangeRule()
+{
std::vector<std::pair<Netmask, CounterAndActionValue>> result;
return result;
}
-std::vector<std::pair<ComboAddress, uint64_t> > BPFFilter::getAddrStats()
+std::vector<std::pair<ComboAddress, uint64_t>> BPFFilter::getAddrStats()
{
- std::vector<std::pair<ComboAddress, uint64_t> > result;
+ std::vector<std::pair<ComboAddress, uint64_t>> result;
return result;
}
-std::vector<std::tuple<DNSName, uint16_t, uint64_t> > BPFFilter::getQNameStats()
+std::vector<std::tuple<DNSName, uint16_t, uint64_t>> BPFFilter::getQNameStats()
{
- std::vector<std::tuple<DNSName, uint16_t, uint64_t> > result;
+ std::vector<std::tuple<DNSName, uint16_t, uint64_t>> result;
return result;
}
class BPFFilter
{
public:
- enum class MapType : uint8_t {
+ enum class MapType : uint8_t
+ {
IPv4,
IPv6,
QNames,
CIDR6
};
- enum class MapFormat : uint8_t {
+ enum class MapFormat : uint8_t
+ {
Legacy = 0,
WithActions = 1
};
- enum class MatchAction : uint8_t {
+ enum class MatchAction : uint8_t
+ {
Pass = 0,
Drop = 1,
Truncate = 2
uint64_t counter{0};
BPFFilter::MatchAction action{BPFFilter::MatchAction::Pass};
};
-
BPFFilter(std::unordered_map<std::string, MapConfiguration>& configs, BPFFilter::MapFormat format, bool external);
BPFFilter(const BPFFilter&) = delete;
void removeSocket(int sock);
void block(const ComboAddress& addr, MatchAction action);
void addRangeRule(const Netmask& address, bool force, BPFFilter::MatchAction action);
- void block(const DNSName& qname, MatchAction action, uint16_t qtype=255);
+ void block(const DNSName& qname, MatchAction action, uint16_t qtype = 255);
void unblock(const ComboAddress& addr);
void rmRangeRule(const Netmask& address);
- void unblock(const DNSName& qname, uint16_t qtype=255);
+ void unblock(const DNSName& qname, uint16_t qtype = 255);
- std::vector<std::pair<ComboAddress, uint64_t> > getAddrStats();
+ std::vector<std::pair<ComboAddress, uint64_t>> getAddrStats();
std::vector<std::pair<Netmask, CounterAndActionValue>> getRangeRule();
- std::vector<std::tuple<DNSName, uint16_t, uint64_t> > getQNameStats();
+ std::vector<std::tuple<DNSName, uint16_t, uint64_t>> getQNameStats();
uint64_t getHits(const ComboAddress& requestor);
#include <thread>
#include "threadname.hh"
-template<class T>
+template <class T>
ObjectPipe<T>::ObjectPipe()
{
auto [sender, receiver] = pdns::channel::createObjectQueue<T>(pdns::channel::SenderBlockingMode::SenderBlocking, pdns::channel::ReceiverBlockingMode::ReceiverNonBlocking, 0, false);
d_receiver = std::move(receiver);
}
-template<class T>
+template <class T>
void ObjectPipe<T>::close()
{
d_sender.close();
}
-template<class T>
+template <class T>
void ObjectPipe<T>::write(T& t)
{
auto ptr = std::make_unique<T>(t);
}
}
-template<class T>
+template <class T>
int ObjectPipe<T>::readTimeout(T* t, double msec)
{
while (true) {
- int ret = waitForData(d_receiver.getDescriptor(), 0, 1000*msec);
+ int ret = waitForData(d_receiver.getDescriptor(), 0, 1000 * msec);
if (ret < 0) {
if (errno == EINTR) {
continue;
}
}
-
-template<class T>
-DelayPipe<T>::DelayPipe() : d_thread(&DelayPipe<T>::worker, this)
+template <class T>
+DelayPipe<T>::DelayPipe() :
+ d_thread(&DelayPipe<T>::worker, this)
{
}
-template<class T>
+template <class T>
void DelayPipe<T>::gettime(struct timespec* ts)
{
::gettime(ts);
}
-
-template<class T>
+template <class T>
void DelayPipe<T>::submit(T& t, int msec)
{
struct timespec now;
gettime(&now);
- now.tv_nsec += msec*1e6;
- while(now.tv_nsec > 1e9) {
+ now.tv_nsec += msec * 1e6;
+ while (now.tv_nsec > 1e9) {
now.tv_sec++;
- now.tv_nsec-=1e9;
+ now.tv_nsec -= 1e9;
}
Combo c{t, now};
d_pipe.write(c);
}
-template<class T>
+template <class T>
DelayPipe<T>::~DelayPipe()
{
d_pipe.close();
d_thread.join();
}
-
-
-template<class T>
+template <class T>
void DelayPipe<T>::worker()
{
setThreadName("dnsdist/delayPi");
Combo c;
- for(;;) {
+ for (;;) {
/* this code is slightly too subtle, but I don't see how it could be any simpler.
So we have a set of work to do, and we need to wait until the time arrives to do it.
Simultaneously new work might come in. So we try to combine both of these things by
The other special case is that the first we have to do.. is in the past, so we need to do it
immediately. */
-
- double delay=-1; // infinite
+ double delay = -1; // infinite
struct timespec now;
- if(!d_work.empty()) {
+ if (!d_work.empty()) {
gettime(&now);
- delay=1000*tsdelta(d_work.begin()->first, now);
- if(delay < 0) {
- delay=0; // don't wait - we have work that is late already!
+ delay = 1000 * tsdelta(d_work.begin()->first, now);
+ if (delay < 0) {
+ delay = 0; // don't wait - we have work that is late already!
}
}
- if(delay != 0 ) {
+ if (delay != 0) {
int ret = d_pipe.readTimeout(&c, delay);
- if(ret > 0) { // we got an object
+ if (ret > 0) { // we got an object
d_work.emplace(c.when, c.what);
}
- else if(ret==0) { // EOF
- break;
+ else if (ret == 0) { // EOF
+ break;
}
else {
- ;
+ ;
}
gettime(&now);
}
tscomp cmp;
- for(auto iter = d_work.begin() ; iter != d_work.end(); ) { // do the needful
- if(cmp(iter->first, now)) {
- iter->second();
- d_work.erase(iter++);
+ for (auto iter = d_work.begin(); iter != d_work.end();) { // do the needful
+ if (cmp(iter->first, now)) {
+ iter->second();
+ d_work.erase(iter++);
}
else {
- break;
+ break;
}
}
}