]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Simplify the parameters of newBPFFilter
authorY7n05h <Y7n05h@protonmail.com>
Thu, 14 Apr 2022 12:36:11 +0000 (20:36 +0800)
committerY7n05h <Y7n05h@protonmail.com>
Thu, 14 Apr 2022 13:49:10 +0000 (21:49 +0800)
pdns/bpf-filter.cc
pdns/dnsdist-lua-bindings.cc
pdns/dnsdistdist/docs/reference/ebpf.rst

index 35769e3b3d9e42cdda80c38fba851d2b158f4639..5e85cbcda3fe5cba0db9043854d68b8a862adca0 100644 (file)
@@ -322,9 +322,9 @@ BPFFilter::BPFFilter(std::unordered_map<std::string, MapConfiguration>& 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;
index 7b5c837d6c1c22158b3d65730afe229110cf5cdf..ee0071140f7f4683e0c978c48da1c0e5dfe86ef7 100644 (file)
@@ -424,8 +424,7 @@ void setupLuaBindings(LuaContext& luaCtx, bool client)
 
   /* BPF Filter */
 #ifdef HAVE_EBPF
-  using bpfFilterMapParams = boost::variant<uint32_t, LuaAssociativeTable<boost::variant<uint32_t, std::string>>>;
-  using bpfopts_t = LuaAssociativeTable<boost::variant<bool, std::string, bpfFilterMapParams>>;
+  using bpfopts_t = LuaAssociativeTable<boost::variant<bool, uint32_t, std::string>>;
   luaCtx.writeFunction("newBPFFilter", [client](bpfopts_t opts) {
       if (client) {
         return std::shared_ptr<BPFFilter>(nullptr);
@@ -433,34 +432,30 @@ void setupLuaBindings(LuaContext& luaCtx, bool client)
       std::unordered_map<std::string, BPFFilter::MapConfiguration> 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<bpfFilterMapParams>(tmp);
         BPFFilter::MapConfiguration config;
-        config.d_type = type;
-        if (params.type() == typeid(uint32_t)) {
-          config.d_maxItems = boost::get<uint32_t>(params);
-        }
-        else if (params.type() == typeid(LuaAssociativeTable<boost::variant<uint32_t, std::string>>)) {
-          const auto& map = boost::get<LuaAssociativeTable<boost::variant<uint32_t, std::string>>>(params);
-          if (map.count("maxItems")) {
-            config.d_maxItems = boost::get<uint32_t>(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<std::string>(map.at("pinnedPath"));
+          const auto& params = boost::get<uint32_t>(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<string>(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<BPFFilter>(mapsConfig, format, external);
-    });
+  });
 
   luaCtx.registerFunction<void(std::shared_ptr<BPFFilter>::*)(const ComboAddress& ca, boost::optional<uint32_t> action)>("block", [](std::shared_ptr<BPFFilter> bpf, const ComboAddress& ca, boost::optional<uint32_t> action) {
       if (bpf) {
index d12beb73f75bde1b60c05bcb73980d86f71f6b93..e2180ec4910840b1b46d3c32dd36b69f5d0c71e2 100644 (file)
@@ -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.