Dobby  3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
ThunderPlugin.h
1 /*
2 * If not stated otherwise in this file or this component's LICENSE file the
3 * following copyright and licenses apply:
4 *
5 * Copyright 2021 Sky UK
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19 
20 #ifndef THUNDERPLUGIN_H
21 #define THUNDERPLUGIN_H
22 
23 #include <Netfilter.h>
24 #if defined (DOBBY_BUILD)
25  #include <RdkPluginBase.h>
26 #else
27  #include <Dobby/rdkPlugins/RdkPluginBase.h>
28 #endif
29 
30 #include <sys/types.h>
31 #include <netinet/in.h>
32 
33 #include <map>
34 #include <set>
35 #include <list>
36 #include <mutex>
37 #include <string>
38 #include <memory>
39 
40 // -----------------------------------------------------------------------------
54 {
55 public:
56  ThunderPlugin(std::shared_ptr<rt_dobby_schema> &containerConfig,
57  const std::shared_ptr<DobbyRdkPluginUtils> &utils,
58  const std::string &rootfsPath);
59 
60  ~ThunderPlugin();
61 
62 public:
63  inline std::string name() const override
64  {
65  return mName;
66  };
67 
68  unsigned hookHints() const final;
69 
70 public:
71  bool postInstallation() final;
72 
73  bool preCreation() final;
74 
75  bool createRuntime() final;
76 
77  bool postHalt() final;
78 
79 public:
80  std::vector<std::string> getDependencies() const override;
81 
82 private:
83  Netfilter::RuleSet constructRules() const;
84 
85  std::string constructDNATRule(const std::string &containerIp,
86  in_port_t port) const;
87 
88  std::string constructCONNLIMITRule(const std::string &containerIp,
89  const std::string &vethName,
90  in_port_t port,
91  uint32_t connLimit) const;
92 
93  std::string constructACCEPTRule(const std::string &containerIp,
94  const std::string &vethName,
95  in_port_t port) const;
96  bool isNatNetworkMode() const;
97 
98 private:
99  const std::string mName;
100  std::shared_ptr<rt_dobby_schema> mContainerConfig;
101  const std::string mRootfsPath;
102  const std::shared_ptr<DobbyRdkPluginUtils> mUtils;
103 
104  std::shared_ptr<Netfilter> mNetfilter;
105  in_port_t mThunderPort;
106 
107 private:
108  std::mutex mLock;
109  const bool mEnableConnLimit;
110  const std::string mSocketDirectory;
111  const std::string mSocketPath;
112  bool mSocketExists;
113 };
114 #endif // !defined(THUNDERPLUGIN_H)
Class for useful utility methods for plugins such as adding mounts and environment variables.
Definition: DobbyRdkPluginUtils.h:75
Class that can read / write iptables rule sets.
Definition: Netfilter.h:45
Basic object that provides the default overrides for a plugin.
Definition: RdkPluginBase.h:34
Plugin used to map in the wpeframework (aka thunder) server.
Definition: ThunderPlugin.h:54
bool createRuntime() final
Definition: ThunderPlugin.cpp:258
std::string name() const override
Should return the name of the plugin.
Definition: ThunderPlugin.h:63
bool postInstallation() final
Dobby Hook - run in host namespace once when container bundle is downloaded.
Definition: ThunderPlugin.cpp:113
bool preCreation() final
Definition: ThunderPlugin.cpp:175
bool postHalt() final
Definition: ThunderPlugin.cpp:294
std::string constructDNATRule(const std::string &containerIp, in_port_t port) const
Constructs a DNAT PREROUTING rule to send anything from the container on the given port to localhost ...
Definition: ThunderPlugin.cpp:416
unsigned hookHints() const final
Set the bit flags for which hooks we're going to use.
Definition: ThunderPlugin.cpp:96
std::string constructCONNLIMITRule(const std::string &containerIp, const std::string &vethName, in_port_t port, uint32_t connLimit) const
Constructs an INPUT REJECT rule to reject connection if exceed the limit.
Definition: ThunderPlugin.cpp:453
std::string constructACCEPTRule(const std::string &containerIp, const std::string &vethName, in_port_t port) const
Constructs a INPUT ACCEPT rule to allow packets from the container over the dobby0 bridge to localhos...
Definition: ThunderPlugin.cpp:494
ThunderPlugin(std::shared_ptr< rt_dobby_schema > &containerConfig, const std::shared_ptr< DobbyRdkPluginUtils > &utils, const std::string &rootfsPath)
Constructor - called when plugin is loaded by PluginLauncher.
Definition: ThunderPlugin.cpp:44
std::vector< std::string > getDependencies() const override
Should return the names of the plugins this plugin depends on.
Definition: ThunderPlugin.cpp:340