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*)>>; |
Tomáš Pecka | f9212c4 | 2021-03-10 11:45:05 +0100 | [diff] [blame^] | 32 | using nlNeigh = std::unique_ptr<rtnl_neigh, std::function<void(rtnl_neigh*)>>; |
Tomáš Pecka | c017eab | 2021-03-10 12:26:05 +0100 | [diff] [blame] | 33 | |
Tomáš Pecka | 2d578c8 | 2021-02-23 19:01:35 +0100 | [diff] [blame] | 34 | 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] | 35 | 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] | 36 | |
Tomáš Pecka | 0972938 | 2021-03-08 19:36:50 +0100 | [diff] [blame] | 37 | Rtnetlink(LinkCB cbLink, AddrCB cbAddr); |
Tomáš Pecka | 2d578c8 | 2021-02-23 19:01:35 +0100 | [diff] [blame] | 38 | ~Rtnetlink(); |
Tomáš Pecka | c017eab | 2021-03-10 12:26:05 +0100 | [diff] [blame] | 39 | std::vector<nlLink> getLinks(); |
Tomáš Pecka | f9212c4 | 2021-03-10 11:45:05 +0100 | [diff] [blame^] | 40 | std::vector<std::pair<nlNeigh, nlLink>> getNeighbours(); |
Tomáš Pecka | 2d578c8 | 2021-02-23 19:01:35 +0100 | [diff] [blame] | 41 | |
| 42 | private: |
Tomáš Pecka | c017eab | 2021-03-10 12:26:05 +0100 | [diff] [blame] | 43 | void resyncCache(const nlCache& cache); |
| 44 | |
Tomáš Pecka | 2d578c8 | 2021-02-23 19:01:35 +0100 | [diff] [blame] | 45 | velia::Log m_log; |
Tomáš Pecka | c017eab | 2021-03-10 12:26:05 +0100 | [diff] [blame] | 46 | std::unique_ptr<nl_sock, std::function<void(nl_sock*)>> m_nlSocket; |
| 47 | nlCacheManager m_nlCacheManager; // for updates |
Tomáš Pecka | f9212c4 | 2021-03-10 11:45:05 +0100 | [diff] [blame^] | 48 | nlCache m_nlCacheLink; // for getLinks, getNeighbours |
| 49 | nlCache m_nlCacheNeighbour; // for getNeighbours |
Tomáš Pecka | 0972938 | 2021-03-08 19:36:50 +0100 | [diff] [blame] | 50 | LinkCB m_cbLink; |
| 51 | AddrCB m_cbAddr; |
Tomáš Pecka | 2d578c8 | 2021-02-23 19:01:35 +0100 | [diff] [blame] | 52 | std::unique_ptr<impl::nlCacheMngrWatcher> m_nlCacheMngrWatcher; // first to destroy, because the thread uses m_nlCacheManager and m_log |
| 53 | }; |
| 54 | |
| 55 | class RtnetlinkException : public std::runtime_error { |
| 56 | public: |
| 57 | RtnetlinkException(const std::string& msg); |
| 58 | RtnetlinkException(const std::string& funcName, int error); |
| 59 | }; |
| 60 | |
| 61 | } |