blob: f1bae7c71a33bd1989d58402316160fea439b03a [file] [log] [blame]
Tomáš Pecka2d578c82021-02-23 19:01:35 +01001/*
2 * Copyright (C) 2021 CESNET, https://photonics.cesnet.cz/
3 *
4 * Written by Tomáš Pecka <tomas.pecka@cesnet.cz>
5 *
6 */
7
8#pragma once
9
10#include <functional>
11#include <mutex>
12#include <netlink/netlink.h>
13#include <netlink/route/link.h>
14#include <stdexcept>
15#include <thread>
16#include "utils/log-fwd.h"
17
18namespace velia::system {
19
20namespace impl {
21class nlCacheMngrWatcher;
22}
23
24/** @brief Wrapper for monitoring changes in NETLINK_ROUTE */
25class Rtnetlink {
26public:
27 using nlCacheManager = std::shared_ptr<nl_cache_mngr>;
28 using LinkCB = std::function<void(rtnl_link* link, int cacheAction)>; /// cacheAction: NL_ACT_*
29
30 explicit Rtnetlink(LinkCB cbLink);
31 ~Rtnetlink();
32
33private:
34 velia::Log m_log;
35 nlCacheManager m_nlCacheManager;
36 LinkCB m_cbLink; /* (link object ptr, cache action). Cache action specified in libnl's cache.h (NL_ACT_*) */
37 std::unique_ptr<impl::nlCacheMngrWatcher> m_nlCacheMngrWatcher; // first to destroy, because the thread uses m_nlCacheManager and m_log
38};
39
40class RtnetlinkException : public std::runtime_error {
41public:
42 RtnetlinkException(const std::string& msg);
43 RtnetlinkException(const std::string& funcName, int error);
44};
45
46}