blob: f4d2e5fc6048ee72469412c8a25b286399ab70fa [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>
Tomáš Pecka09729382021-03-08 19:36:50 +010013#include <netlink/route/addr.h>
Tomáš Pecka2d578c82021-02-23 19:01:35 +010014#include <netlink/route/link.h>
Tomáš Peckac017eab2021-03-10 12:26:05 +010015#include <netlink/route/neighbour.h>
Tomáš Pecka1ea7e312021-03-31 20:00:03 +020016#include <netlink/route/route.h>
Tomáš Pecka2d578c82021-02-23 19:01:35 +010017#include <stdexcept>
18#include <thread>
19#include "utils/log-fwd.h"
20
21namespace velia::system {
22
23namespace impl {
24class nlCacheMngrWatcher;
25}
26
27/** @brief Wrapper for monitoring changes in NETLINK_ROUTE */
28class Rtnetlink {
29public:
30 using nlCacheManager = std::shared_ptr<nl_cache_mngr>;
Tomáš Peckac017eab2021-03-10 12:26:05 +010031 using nlCache = std::unique_ptr<nl_cache, std::function<void(nl_cache*)>>;
32 using nlLink = std::unique_ptr<rtnl_link, std::function<void(rtnl_link*)>>;
Tomáš Peckaf9212c42021-03-10 11:45:05 +010033 using nlNeigh = std::unique_ptr<rtnl_neigh, std::function<void(rtnl_neigh*)>>;
Tomáš Pecka1ea7e312021-03-31 20:00:03 +020034 using nlRoute = std::unique_ptr<rtnl_route, std::function<void(rtnl_route*)>>;
Tomáš Peckac017eab2021-03-10 12:26:05 +010035
Tomáš Pecka2d578c82021-02-23 19:01:35 +010036 using LinkCB = std::function<void(rtnl_link* link, int cacheAction)>; /// cacheAction: NL_ACT_*
Tomáš Pecka09729382021-03-08 19:36:50 +010037 using AddrCB = std::function<void(rtnl_addr* addr, int cacheAction)>; /// cacheAction: NL_ACT_*
Tomáš Pecka2d578c82021-02-23 19:01:35 +010038
Tomáš Pecka09729382021-03-08 19:36:50 +010039 Rtnetlink(LinkCB cbLink, AddrCB cbAddr);
Tomáš Pecka2d578c82021-02-23 19:01:35 +010040 ~Rtnetlink();
Tomáš Peckac017eab2021-03-10 12:26:05 +010041 std::vector<nlLink> getLinks();
Tomáš Pecka1ea7e312021-03-31 20:00:03 +020042 std::vector<nlRoute> getRoutes();
Tomáš Peckaf9212c42021-03-10 11:45:05 +010043 std::vector<std::pair<nlNeigh, nlLink>> getNeighbours();
Tomáš Pecka2d578c82021-02-23 19:01:35 +010044
Tomáš Pecka3d3cf612021-03-31 19:51:31 +020045 void invokeInitialCallbacks();
46
Tomáš Pecka2d578c82021-02-23 19:01:35 +010047private:
Tomáš Peckac017eab2021-03-10 12:26:05 +010048 void resyncCache(const nlCache& cache);
49
Tomáš Pecka2d578c82021-02-23 19:01:35 +010050 velia::Log m_log;
Tomáš Peckac017eab2021-03-10 12:26:05 +010051 std::unique_ptr<nl_sock, std::function<void(nl_sock*)>> m_nlSocket;
52 nlCacheManager m_nlCacheManager; // for updates
Tomáš Pecka3d3cf612021-03-31 19:51:31 +020053 nl_cache* m_nlManagedCacheLink;
54 nl_cache* m_nlManagedCacheAddr;
Tomáš Pecka1ea7e312021-03-31 20:00:03 +020055 nl_cache* m_nlManagedCacheRoute;
Tomáš Peckaf9212c42021-03-10 11:45:05 +010056 nlCache m_nlCacheLink; // for getLinks, getNeighbours
57 nlCache m_nlCacheNeighbour; // for getNeighbours
Tomáš Pecka1ea7e312021-03-31 20:00:03 +020058 nlCache m_nlCacheRoute; // for getRoutes
Tomáš Pecka09729382021-03-08 19:36:50 +010059 LinkCB m_cbLink;
60 AddrCB m_cbAddr;
Tomáš Pecka2d578c82021-02-23 19:01:35 +010061 std::unique_ptr<impl::nlCacheMngrWatcher> m_nlCacheMngrWatcher; // first to destroy, because the thread uses m_nlCacheManager and m_log
62};
63
64class RtnetlinkException : public std::runtime_error {
65public:
66 RtnetlinkException(const std::string& msg);
67 RtnetlinkException(const std::string& funcName, int error);
68};
69
70}