Tomáš Pecka | 2d578c8 | 2021-02-23 19:01:35 +0100 | [diff] [blame] | 1 | /* |
| 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áš Pecka | 0972938 | 2021-03-08 19:36:50 +0100 | [diff] [blame] | 13 | #include <netlink/route/addr.h> |
Tomáš Pecka | 2d578c8 | 2021-02-23 19:01:35 +0100 | [diff] [blame] | 14 | #include <netlink/route/link.h> |
Tomáš Pecka | c017eab | 2021-03-10 12:26:05 +0100 | [diff] [blame^] | 15 | #include <netlink/route/neighbour.h> |
Tomáš Pecka | 2d578c8 | 2021-02-23 19:01:35 +0100 | [diff] [blame] | 16 | #include <stdexcept> |
| 17 | #include <thread> |
| 18 | #include "utils/log-fwd.h" |
| 19 | |
| 20 | namespace velia::system { |
| 21 | |
| 22 | namespace impl { |
| 23 | class nlCacheMngrWatcher; |
| 24 | } |
| 25 | |
| 26 | /** @brief Wrapper for monitoring changes in NETLINK_ROUTE */ |
| 27 | class Rtnetlink { |
| 28 | public: |
| 29 | using nlCacheManager = std::shared_ptr<nl_cache_mngr>; |
Tomáš Pecka | c017eab | 2021-03-10 12:26:05 +0100 | [diff] [blame^] | 30 | using nlCache = std::unique_ptr<nl_cache, std::function<void(nl_cache*)>>; |
| 31 | using nlLink = std::unique_ptr<rtnl_link, std::function<void(rtnl_link*)>>; |
| 32 | |
Tomáš Pecka | 2d578c8 | 2021-02-23 19:01:35 +0100 | [diff] [blame] | 33 | using LinkCB = std::function<void(rtnl_link* link, int cacheAction)>; /// cacheAction: NL_ACT_* |
Tomáš Pecka | 0972938 | 2021-03-08 19:36:50 +0100 | [diff] [blame] | 34 | using AddrCB = std::function<void(rtnl_addr* addr, int cacheAction)>; /// cacheAction: NL_ACT_* |
Tomáš Pecka | 2d578c8 | 2021-02-23 19:01:35 +0100 | [diff] [blame] | 35 | |
Tomáš Pecka | 0972938 | 2021-03-08 19:36:50 +0100 | [diff] [blame] | 36 | Rtnetlink(LinkCB cbLink, AddrCB cbAddr); |
Tomáš Pecka | 2d578c8 | 2021-02-23 19:01:35 +0100 | [diff] [blame] | 37 | ~Rtnetlink(); |
Tomáš Pecka | c017eab | 2021-03-10 12:26:05 +0100 | [diff] [blame^] | 38 | std::vector<nlLink> getLinks(); |
Tomáš Pecka | 2d578c8 | 2021-02-23 19:01:35 +0100 | [diff] [blame] | 39 | |
| 40 | private: |
Tomáš Pecka | c017eab | 2021-03-10 12:26:05 +0100 | [diff] [blame^] | 41 | void resyncCache(const nlCache& cache); |
| 42 | |
Tomáš Pecka | 2d578c8 | 2021-02-23 19:01:35 +0100 | [diff] [blame] | 43 | velia::Log m_log; |
Tomáš Pecka | c017eab | 2021-03-10 12:26:05 +0100 | [diff] [blame^] | 44 | std::unique_ptr<nl_sock, std::function<void(nl_sock*)>> m_nlSocket; |
| 45 | nlCacheManager m_nlCacheManager; // for updates |
| 46 | nlCache m_nlCacheLink; // for getLinks |
Tomáš Pecka | 0972938 | 2021-03-08 19:36:50 +0100 | [diff] [blame] | 47 | LinkCB m_cbLink; |
| 48 | AddrCB m_cbAddr; |
Tomáš Pecka | 2d578c8 | 2021-02-23 19:01:35 +0100 | [diff] [blame] | 49 | std::unique_ptr<impl::nlCacheMngrWatcher> m_nlCacheMngrWatcher; // first to destroy, because the thread uses m_nlCacheManager and m_log |
| 50 | }; |
| 51 | |
| 52 | class RtnetlinkException : public std::runtime_error { |
| 53 | public: |
| 54 | RtnetlinkException(const std::string& msg); |
| 55 | RtnetlinkException(const std::string& funcName, int error); |
| 56 | }; |
| 57 | |
| 58 | } |