17 #ifndef __LINUX_ROUTING_QUEUEING_INTERNAL_HPP__ 18 #define __LINUX_ROUTING_QUEUEING_INTERNAL_HPP__ 20 #include <netlink/cache.h> 21 #include <netlink/errno.h> 22 #include <netlink/object.h> 23 #include <netlink/socket.h> 25 #include <netlink/route/link.h> 26 #include <netlink/route/qdisc.h> 27 #include <netlink/route/tc.h> 50 inline void cleanup(
struct rtnl_qdisc* qdisc)
52 rtnl_qdisc_put(qdisc);
65 template <
typename Config>
68 const Config& config);
76 template <
typename Config>
83 template <
typename Config>
88 struct rtnl_qdisc* q = rtnl_qdisc_alloc();
90 return Error(
"Failed to allocate a libnl qdisc");
95 rtnl_tc_set_link(TC_CAST(qdisc.
get()), link.
get());
96 rtnl_tc_set_parent(TC_CAST(qdisc.
get()), discipline.
parent.get());
98 if (discipline.
handle.isSome()) {
99 rtnl_tc_set_handle(TC_CAST(qdisc.
get()), discipline.
handle->get());
102 int error = rtnl_tc_set_kind(TC_CAST(qdisc.
get()), discipline.
kind.c_str());
105 "Failed to set the kind of the queueing discipline: " +
106 std::string(nl_geterror(error)));
113 "Failed to encode the queueing discipline: " +
134 struct nl_cache* c =
nullptr;
135 int error = rtnl_qdisc_alloc_cache(socket->
get(), &c);
138 "Failed to get queueing discipline info from kernel: " +
139 std::string(nl_geterror(error)));
144 std::vector<Netlink<struct rtnl_qdisc>> results;
146 for (
struct nl_object* o = nl_cache_get_first(cache.
get());
147 o !=
nullptr; o = nl_cache_get_next(o)) {
148 if (rtnl_tc_get_ifindex(TC_CAST(o)) == rtnl_link_get_ifindex(link.
get())) {
168 const std::string&
kind)
176 if (rtnl_tc_get_parent(TC_CAST(qdisc.
get())) == parent.
get() &&
177 rtnl_tc_get_kind(TC_CAST(qdisc.
get())) ==
kind) {
193 const std::string& _link,
195 const std::string&
kind)
200 }
else if (link.
isNone()) {
217 template <
typename Config>
219 const std::string& _link,
225 }
else if (link.
isNone()) {
226 return Error(
"Link '" + _link +
"' is not found");
233 return Error(
"Failed to encode the queueing discipline: " + qdisc.
error());
237 if (socket.isError()) {
238 return Error(socket.error());
243 int error = rtnl_qdisc_add(
246 NLM_F_CREATE | NLM_F_EXCL);
249 if (error == -NLE_EXIST) {
253 "Failed to add a queueing discipline to the link: " +
254 std::string(nl_geterror(error)));
265 const std::string& _link,
267 const std::string&
kind)
272 }
else if (link.
isNone()) {
279 }
else if (qdisc.
isNone()) {
288 int error = rtnl_qdisc_delete(socket->
get(), qdisc.
get().get());
292 return Error(std::string(nl_geterror(error)));
303 const std::string& _link,
305 const std::string&
kind)
310 }
else if (link.
isNone()) {
317 }
else if (qdisc.
isNone()) {
326 for (
size_t i = 0; i <= static_cast<size_t>(RTNL_TC_STATS_MAX); i++) {
327 if (rtnl_tc_stat2str(static_cast<rtnl_tc_stat>(i), name,
sizeof(name))) {
328 results[
name] = rtnl_tc_get_stat(
329 TC_CAST(qdisc->
get()),
330 static_cast<rtnl_tc_stat>(i));
340 #endif // __LINUX_ROUTING_QUEUEING_INTERNAL_HPP__ std::string kind
Definition: discipline.hpp:42
bool isNone() const
Definition: result.hpp:113
Definition: errorbase.hpp:36
Result< hashmap< std::string, uint64_t > > statistics(const std::string &_link, const Handle &parent, const std::string &kind)
Definition: internal.hpp:302
T & get()&
Definition: try.hpp:80
static Result< T > error(const std::string &message)
Definition: result.hpp:54
Result< Netlink< struct rtnl_link > > get(const std::string &link)
Definition: internal.hpp:57
Definition: handle.hpp:38
Try< Netlink< struct rtnl_qdisc > > encodeDiscipline(const Netlink< struct rtnl_link > &link, const Discipline< Config > &discipline)
Definition: internal.hpp:84
Result< Netlink< struct rtnl_qdisc > > getQdisc(const Netlink< struct rtnl_link > &link, const Handle &parent, const std::string &kind)
Definition: internal.hpp:165
Try< Nothing > encode(const Netlink< struct rtnl_qdisc > &qdisc, const Config &config)
Definition: hashmap.hpp:38
Try< std::vector< Netlink< struct rtnl_qdisc > > > getQdiscs(const Netlink< struct rtnl_link > &link)
Definition: internal.hpp:125
Try< bool > exists(const std::string &_link, const Handle &parent, const std::string &kind)
Definition: internal.hpp:192
T * get() const
Definition: internal.hpp:65
Definition: internal.hpp:60
static Try error(const E &e)
Definition: try.hpp:43
Definition: attributes.hpp:24
bool isError() const
Definition: try.hpp:78
Try< bool > create(const std::string &_link, const Discipline< Config > &discipline)
Definition: internal.hpp:218
std::string error(const std::string &msg, uint32_t code)
T & get()&
Definition: result.hpp:116
Option< Handle > handle
Definition: discipline.hpp:44
Definition: diagnosis.hpp:30
Config config
Definition: discipline.hpp:47
bool isSome() const
Definition: result.hpp:112
bool isError() const
Definition: result.hpp:114
Definition: discipline.hpp:30
void cleanup(struct rtnl_cls *cls)
Definition: internal.hpp:64
Try< Netlink< struct nl_sock > > socket(int protocol=NETLINK_ROUTE)
Definition: internal.hpp:91
constexpr const char * name
Definition: shell.hpp:41
constexpr uint32_t get() const
Definition: handle.hpp:66
Result< Config > decode(const Netlink< struct rtnl_qdisc > &qdisc)
Handle parent
Definition: discipline.hpp:43