|
| 1 | +#include <cstring> |
| 2 | + |
| 3 | +#include <logger.h> |
| 4 | + |
| 5 | +#include "teamdctl_mgr.h" |
| 6 | + |
| 7 | +/// |
| 8 | +/// The destructor clean up handlers to teamds |
| 9 | +/// |
| 10 | +TeamdCtlMgr::~TeamdCtlMgr() |
| 11 | +{ |
| 12 | + for (const auto & p: m_handlers) |
| 13 | + { |
| 14 | + const auto & lag_name = p.first; |
| 15 | + const auto & tdc = m_handlers[lag_name]; |
| 16 | + teamdctl_disconnect(tdc); |
| 17 | + teamdctl_free(tdc); |
| 18 | + SWSS_LOG_NOTICE("Exiting. Disconnecting from teamd. LAG '%s'", lag_name.c_str()); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +/// |
| 23 | +/// Returns true, if we have LAG with name lag_name |
| 24 | +/// in the manager. |
| 25 | +/// @param lag_name a name for LAG interface |
| 26 | +/// @return true if has key, false if doesn't |
| 27 | +/// |
| 28 | +bool TeamdCtlMgr::has_key(const std::string & lag_name) const |
| 29 | +{ |
| 30 | + return m_handlers.find(lag_name) != m_handlers.end(); |
| 31 | +} |
| 32 | + |
| 33 | +/// |
| 34 | +/// Adds a LAG interface with lag_name to the manager |
| 35 | +/// This method allocates structures to connect to teamd |
| 36 | +/// @param lag_name a name for LAG interface |
| 37 | +/// @return true if the lag was added successfully, false otherwise |
| 38 | +/// |
| 39 | +bool TeamdCtlMgr::add_lag(const std::string & lag_name) |
| 40 | +{ |
| 41 | + if (has_key(lag_name)) |
| 42 | + { |
| 43 | + SWSS_LOG_DEBUG("The LAG '%s' was already added. Skip adding it.", lag_name.c_str()); |
| 44 | + return true; |
| 45 | + } |
| 46 | + else |
| 47 | + { |
| 48 | + auto tdc = teamdctl_alloc(); |
| 49 | + if (!tdc) |
| 50 | + { |
| 51 | + SWSS_LOG_ERROR("Can't allocate memory for teamdctl handler. LAG='%s'", lag_name.c_str()); |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + int err = teamdctl_connect(tdc, lag_name.c_str(), nullptr, nullptr); |
| 56 | + if (err) |
| 57 | + { |
| 58 | + SWSS_LOG_ERROR("Can't connect to teamd LAG='%s', error='%s'", lag_name.c_str(), strerror(-err)); |
| 59 | + teamdctl_free(tdc); |
| 60 | + return false; |
| 61 | + } |
| 62 | + m_handlers.emplace(lag_name, tdc); |
| 63 | + SWSS_LOG_NOTICE("The LAG '%s' has been added.", lag_name.c_str()); |
| 64 | + } |
| 65 | + |
| 66 | + return true; |
| 67 | +} |
| 68 | + |
| 69 | +/// |
| 70 | +/// Removes a LAG interface with lag_name from the manager |
| 71 | +/// This method deallocates teamd structures |
| 72 | +/// @param lag_name a name for LAG interface |
| 73 | +/// @return true if the lag was removed successfully, false otherwise |
| 74 | +/// |
| 75 | +bool TeamdCtlMgr::remove_lag(const std::string & lag_name) |
| 76 | +{ |
| 77 | + if (has_key(lag_name)) |
| 78 | + { |
| 79 | + auto tdc = m_handlers[lag_name]; |
| 80 | + teamdctl_disconnect(tdc); |
| 81 | + teamdctl_free(tdc); |
| 82 | + m_handlers.erase(lag_name); |
| 83 | + SWSS_LOG_NOTICE("The LAG '%s' has been removed.", lag_name.c_str()); |
| 84 | + } |
| 85 | + else |
| 86 | + { |
| 87 | + SWSS_LOG_WARN("The LAG '%s' hasn't been added. Can't remove it", lag_name.c_str()); |
| 88 | + } |
| 89 | + return true; |
| 90 | +} |
| 91 | + |
| 92 | +/// |
| 93 | +/// Get json dump from teamd for LAG interface with name lag_name |
| 94 | +/// @param lag_name a name for LAG interface |
| 95 | +/// @return a pair. First element of the pair is true, if the method is successful |
| 96 | +/// false otherwise. If the first element is true, the second element has a dump |
| 97 | +/// otherwise the second element is an empty string |
| 98 | +/// |
| 99 | +TeamdCtlDump TeamdCtlMgr::get_dump(const std::string & lag_name) |
| 100 | +{ |
| 101 | + TeamdCtlDump res = { false, "" }; |
| 102 | + if (has_key(lag_name)) |
| 103 | + { |
| 104 | + auto tdc = m_handlers[lag_name]; |
| 105 | + char * dump; |
| 106 | + int r = teamdctl_state_get_raw_direct(tdc, &dump); |
| 107 | + if (r == 0) |
| 108 | + { |
| 109 | + res = { true, std::string(dump) }; |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + SWSS_LOG_ERROR("Can't get dump for LAG '%s'. Skipping", lag_name.c_str()); |
| 114 | + } |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + SWSS_LOG_ERROR("Can't update state. LAG not found. LAG='%s'", lag_name.c_str()); |
| 119 | + } |
| 120 | + |
| 121 | + return res; |
| 122 | +} |
| 123 | + |
| 124 | +/// |
| 125 | +/// Get dumps for all registered LAG interfaces |
| 126 | +/// @return vector of pairs. Each pair first value is a name of LAG, second value is a dump |
| 127 | +/// |
| 128 | +TeamdCtlDumps TeamdCtlMgr::get_dumps() |
| 129 | +{ |
| 130 | + TeamdCtlDumps res; |
| 131 | + |
| 132 | + for (const auto & p: m_handlers) |
| 133 | + { |
| 134 | + const auto & lag_name = p.first; |
| 135 | + const auto & result = get_dump(lag_name); |
| 136 | + const auto & status = result.first; |
| 137 | + const auto & dump = result.second; |
| 138 | + if (status) |
| 139 | + { |
| 140 | + res.push_back({ lag_name, dump }); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return res; |
| 145 | +} |
| 146 | + |
0 commit comments