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 | 1ea7e31 | 2021-03-31 20:00:03 +0200 | [diff] [blame^] | 16 | #include <netlink/route/route.h> |
Tomáš Pecka | 2d578c8 | 2021-02-23 19:01:35 +0100 | [diff] [blame] | 17 | #include <stdexcept> |
| 18 | #include <thread> |
| 19 | #include "utils/log-fwd.h" |
| 20 | |
| 21 | namespace velia::system { |
| 22 | |
| 23 | namespace impl { |
| 24 | class nlCacheMngrWatcher; |
| 25 | } |
| 26 | |
| 27 | /** @brief Wrapper for monitoring changes in NETLINK_ROUTE */ |
| 28 | class Rtnetlink { |
| 29 | public: |
| 30 | using nlCacheManager = std::shared_ptr<nl_cache_mngr>; |
Tomáš Pecka | c017eab | 2021-03-10 12:26:05 +0100 | [diff] [blame] | 31 | 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áš Pecka | f9212c4 | 2021-03-10 11:45:05 +0100 | [diff] [blame] | 33 | using nlNeigh = std::unique_ptr<rtnl_neigh, std::function<void(rtnl_neigh*)>>; |
Tomáš Pecka | 1ea7e31 | 2021-03-31 20:00:03 +0200 | [diff] [blame^] | 34 | using nlRoute = std::unique_ptr<rtnl_route, std::function<void(rtnl_route*)>>; |
Tomáš Pecka | c017eab | 2021-03-10 12:26:05 +0100 | [diff] [blame] | 35 | |
Tomáš Pecka | 2d578c8 | 2021-02-23 19:01:35 +0100 | [diff] [blame] | 36 | 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] | 37 | 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] | 38 | |
Tomáš Pecka | 0972938 | 2021-03-08 19:36:50 +0100 | [diff] [blame] | 39 | Rtnetlink(LinkCB cbLink, AddrCB cbAddr); |
Tomáš Pecka | 2d578c8 | 2021-02-23 19:01:35 +0100 | [diff] [blame] | 40 | ~Rtnetlink(); |
Tomáš Pecka | c017eab | 2021-03-10 12:26:05 +0100 | [diff] [blame] | 41 | std::vector<nlLink> getLinks(); |
Tomáš Pecka | 1ea7e31 | 2021-03-31 20:00:03 +0200 | [diff] [blame^] | 42 | std::vector<nlRoute> getRoutes(); |
Tomáš Pecka | f9212c4 | 2021-03-10 11:45:05 +0100 | [diff] [blame] | 43 | std::vector<std::pair<nlNeigh, nlLink>> getNeighbours(); |
Tomáš Pecka | 2d578c8 | 2021-02-23 19:01:35 +0100 | [diff] [blame] | 44 | |
Tomáš Pecka | 3d3cf61 | 2021-03-31 19:51:31 +0200 | [diff] [blame] | 45 | void invokeInitialCallbacks(); |
| 46 | |
Tomáš Pecka | 2d578c8 | 2021-02-23 19:01:35 +0100 | [diff] [blame] | 47 | private: |
Tomáš Pecka | c017eab | 2021-03-10 12:26:05 +0100 | [diff] [blame] | 48 | void resyncCache(const nlCache& cache); |
| 49 | |
Tomáš Pecka | 2d578c8 | 2021-02-23 19:01:35 +0100 | [diff] [blame] | 50 | velia::Log m_log; |
Tomáš Pecka | c017eab | 2021-03-10 12:26:05 +0100 | [diff] [blame] | 51 | std::unique_ptr<nl_sock, std::function<void(nl_sock*)>> m_nlSocket; |
| 52 | nlCacheManager m_nlCacheManager; // for updates |
Tomáš Pecka | 3d3cf61 | 2021-03-31 19:51:31 +0200 | [diff] [blame] | 53 | nl_cache* m_nlManagedCacheLink; |
| 54 | nl_cache* m_nlManagedCacheAddr; |
Tomáš Pecka | 1ea7e31 | 2021-03-31 20:00:03 +0200 | [diff] [blame^] | 55 | nl_cache* m_nlManagedCacheRoute; |
Tomáš Pecka | f9212c4 | 2021-03-10 11:45:05 +0100 | [diff] [blame] | 56 | nlCache m_nlCacheLink; // for getLinks, getNeighbours |
| 57 | nlCache m_nlCacheNeighbour; // for getNeighbours |
Tomáš Pecka | 1ea7e31 | 2021-03-31 20:00:03 +0200 | [diff] [blame^] | 58 | nlCache m_nlCacheRoute; // for getRoutes |
Tomáš Pecka | 0972938 | 2021-03-08 19:36:50 +0100 | [diff] [blame] | 59 | LinkCB m_cbLink; |
| 60 | AddrCB m_cbAddr; |
Tomáš Pecka | 2d578c8 | 2021-02-23 19:01:35 +0100 | [diff] [blame] | 61 | std::unique_ptr<impl::nlCacheMngrWatcher> m_nlCacheMngrWatcher; // first to destroy, because the thread uses m_nlCacheManager and m_log |
| 62 | }; |
| 63 | |
| 64 | class RtnetlinkException : public std::runtime_error { |
| 65 | public: |
| 66 | RtnetlinkException(const std::string& msg); |
| 67 | RtnetlinkException(const std::string& funcName, int error); |
| 68 | }; |
| 69 | |
| 70 | } |