From: Y7n05h Date: Thu, 14 Apr 2022 12:36:11 +0000 (+0800) Subject: Simplify the parameters of newBPFFilter X-Git-Tag: auth-4.8.0-alpha0~137^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fc8ce34711d08a97ae6fde087992f5a3d57ad2b4;p=thirdparty%2Fpdns.git Simplify the parameters of newBPFFilter --- diff --git a/pdns/bpf-filter.cc b/pdns/bpf-filter.cc index 35769e3b3d..5e85cbcda3 100644 --- a/pdns/bpf-filter.cc +++ b/pdns/bpf-filter.cc @@ -322,9 +322,9 @@ BPFFilter::BPFFilter(std::unordered_map& configs, auto maps = d_maps.lock(); - maps->d_v4 = BPFFilter::Map(configs["v4Params"], d_mapFormat); - maps->d_v6 = BPFFilter::Map(configs["v6Params"], d_mapFormat); - maps->d_qnames = BPFFilter::Map(configs["qnameParams"], d_mapFormat); + maps->d_v4 = BPFFilter::Map(configs["ipv4"], d_mapFormat); + maps->d_v6 = BPFFilter::Map(configs["ipv6"], d_mapFormat); + maps->d_qnames = BPFFilter::Map(configs["qnames"], d_mapFormat); if (!external) { BPFFilter::MapConfiguration filters; filters.d_maxItems = 1; diff --git a/pdns/dnsdist-lua-bindings.cc b/pdns/dnsdist-lua-bindings.cc index 7b5c837d6c..ee0071140f 100644 --- a/pdns/dnsdist-lua-bindings.cc +++ b/pdns/dnsdist-lua-bindings.cc @@ -424,8 +424,7 @@ void setupLuaBindings(LuaContext& luaCtx, bool client) /* BPF Filter */ #ifdef HAVE_EBPF - using bpfFilterMapParams = boost::variant>>; - using bpfopts_t = LuaAssociativeTable>; + using bpfopts_t = LuaAssociativeTable>; luaCtx.writeFunction("newBPFFilter", [client](bpfopts_t opts) { if (client) { return std::shared_ptr(nullptr); @@ -433,34 +432,30 @@ void setupLuaBindings(LuaContext& luaCtx, bool client) std::unordered_map mapsConfig; const auto convertParamsToConfig = [&](const std::string name, BPFFilter::MapType type) { - if (!opts.count(name)) { - return; - } - const auto& tmp = opts.at(name); - if (tmp.type() != typeid(bpfFilterMapParams)) { - throw std::runtime_error("params is invalid"); - } - const auto& params = boost::get(tmp); BPFFilter::MapConfiguration config; - config.d_type = type; - if (params.type() == typeid(uint32_t)) { - config.d_maxItems = boost::get(params); - } - else if (params.type() == typeid(LuaAssociativeTable>)) { - const auto& map = boost::get>>(params); - if (map.count("maxItems")) { - config.d_maxItems = boost::get(map.at("maxItems")); + if (const string key = name + "MaxItems"; opts.count(key)) { + const auto& tmp = opts.at(name); + if (tmp.type() != typeid(uint32_t)) { + throw std::runtime_error("params is invalid"); } - if (map.count("pinnedPath")) { - config.d_pinnedPath = boost::get(map.at("pinnedPath")); + const auto& params = boost::get(tmp); + config.d_maxItems = params; + } + + if (const string key = name + "PinnedPath"; opts.count(key)) { + auto& tmp = opts.at(name); + if (tmp.type() != typeid(string)) { + throw std::runtime_error("params is invalid"); } + auto& params = boost::get(tmp); + config.d_pinnedPath = std::move(params); } mapsConfig[name] = config; }; - convertParamsToConfig("v4Params", BPFFilter::MapType::IPv4); - convertParamsToConfig("v6Params", BPFFilter::MapType::IPv6); - convertParamsToConfig("qnameParams", BPFFilter::MapType::QNames); + convertParamsToConfig("ipv4", BPFFilter::MapType::IPv4); + convertParamsToConfig("ipv6", BPFFilter::MapType::IPv6); + convertParamsToConfig("qnames", BPFFilter::MapType::QNames); BPFFilter::MapFormat format = BPFFilter::MapFormat::Legacy; bool external = false; @@ -475,7 +470,7 @@ void setupLuaBindings(LuaContext& luaCtx, bool client) } return std::make_shared(mapsConfig, format, external); - }); + }); luaCtx.registerFunction::*)(const ComboAddress& ca, boost::optional action)>("block", [](std::shared_ptr bpf, const ComboAddress& ca, boost::optional action) { if (bpf) { diff --git a/pdns/dnsdistdist/docs/reference/ebpf.rst b/pdns/dnsdistdist/docs/reference/ebpf.rst index d12beb73f7..e2180ec491 100644 --- a/pdns/dnsdistdist/docs/reference/ebpf.rst +++ b/pdns/dnsdistdist/docs/reference/ebpf.rst @@ -26,15 +26,14 @@ These are all the functions, objects and methods related to the :doc:`../advance :param table options: A table with key: value pairs with options. Options: - * ``v4Params``: int or table - Maximum number of entries in this filter or a table with ``maxItems`` or ``pinnedPath`` . - * ``v6Params``: int or table - Maximum number of entries in this filter or a table with ``maxItems`` or ``pinnedPath`` . - * ``qnameParams``: int or table - Maximum number of entries in this filter or a table with ``maxItems`` or ``pinnedPath`` . + * ``ipv4MaxItems``: int - The maximum number of entries in a given map. Default is 0 which will not allow any entry at all. + * ``ipv4PinnedPath``: int - The filesystem path this map should be pinned to. + * ``ipv6MaxItems``: int - The maximum number of entries in a given map. Default is 0 which will not allow any entry at all. + * ``ipv6PinnedPath``: int - The filesystem path this map should be pinned to. + * ``qnamesMaxItems``: int - The maximum number of entries in a given map. Default is 0 which will not allow any entry at all. + * ``qnamesPinnedPath``: int - The filesystem path this map should be pinned to. * ``external``: bool - If set to true, DNSDist can to load the internal eBPF program. - Options for ``v4Params`` , ``v6Params`` and ``qnameParams``: - * ``maxItems``: int - The maximum number of entries in a given map. Default is 0 which will not allow any entry at all. - * ``pinnedPath``: str - The filesystem path this map should be pinned to. - .. function:: newDynBPFFilter(bpf) -> DynBPFFilter Return a new dynamic eBPF filter associated to a given BPF Filter.