|
| 1 | +#include <sstream> |
| 2 | +#include <syslog.h> |
| 3 | +#include <algorithm> |
| 4 | +#include "configInterface.h" |
| 5 | + |
| 6 | +constexpr auto DEFAULT_TIMEOUT_MSEC = 1000; |
| 7 | + |
| 8 | +bool pollSwssNotifcation = true; |
| 9 | +std::shared_ptr<boost::thread> mSwssThreadPtr; |
| 10 | + |
| 11 | +std::shared_ptr<swss::DBConnector> configDbPtr = std::make_shared<swss::DBConnector> ("CONFIG_DB", 0); |
| 12 | +swss::SubscriberStateTable ipHelpersTable(configDbPtr.get(), "DHCP_RELAY"); |
| 13 | +swss::Select swssSelect; |
| 14 | + |
| 15 | +/** |
| 16 | + * @code void initialize_swss() |
| 17 | + * |
| 18 | + * @brief initialize DB tables and start SWSS listening thread |
| 19 | + * |
| 20 | + * @return none |
| 21 | + */ |
| 22 | +void initialize_swss(std::vector<relay_config> *vlans) |
| 23 | +{ |
| 24 | + try { |
| 25 | + swssSelect.addSelectable(&ipHelpersTable); |
| 26 | + get_dhcp(vlans); |
| 27 | + mSwssThreadPtr = std::make_shared<boost::thread> (&handleSwssNotification, vlans); |
| 28 | + } |
| 29 | + catch (const std::bad_alloc &e) { |
| 30 | + syslog(LOG_ERR, "Failed allocate memory. Exception details: %s", e.what()); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * @code void deinitialize_swss() |
| 36 | + * |
| 37 | + * @brief deinitialize DB interface and join SWSS listening thread |
| 38 | + * |
| 39 | + * @return none |
| 40 | + */ |
| 41 | +void deinitialize_swss() |
| 42 | +{ |
| 43 | + stopSwssNotificationPoll(); |
| 44 | + mSwssThreadPtr->interrupt(); |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +/** |
| 49 | + * @code void get_dhcp(std::vector<relay_config> *vlans) |
| 50 | + * |
| 51 | + * @brief initialize and get vlan table information from DHCP_RELAY |
| 52 | + * |
| 53 | + * @return none |
| 54 | + */ |
| 55 | +void get_dhcp(std::vector<relay_config> *vlans) { |
| 56 | + swss::Selectable *selectable; |
| 57 | + int ret = swssSelect.select(&selectable, DEFAULT_TIMEOUT_MSEC); |
| 58 | + if (ret == swss::Select::ERROR) { |
| 59 | + syslog(LOG_WARNING, "Select: returned ERROR"); |
| 60 | + } else if (ret == swss::Select::TIMEOUT) { |
| 61 | + } |
| 62 | + if (selectable == static_cast<swss::Selectable *> (&ipHelpersTable)) { |
| 63 | + handleRelayNotification(ipHelpersTable, vlans); |
| 64 | + } |
| 65 | +} |
| 66 | +/** |
| 67 | + * @code void handleSwssNotification(std::vector<relay_config> *vlans) |
| 68 | + * |
| 69 | + * @brief main thread for handling SWSS notification |
| 70 | + * |
| 71 | + * @param context list of vlans/argument config that contains strings of server and option |
| 72 | + * |
| 73 | + * @return none |
| 74 | + */ |
| 75 | +void handleSwssNotification(std::vector<relay_config> *vlans) |
| 76 | +{ |
| 77 | + while (pollSwssNotifcation) { |
| 78 | + get_dhcp(vlans); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * @code void handleRelayNotification(swss::SubscriberStateTable &ipHelpersTable, std::vector<relay_config> *vlans) |
| 84 | + * |
| 85 | + * @brief handles DHCPv6 relay configuration change notification |
| 86 | + * |
| 87 | + * @param ipHelpersTable DHCP table |
| 88 | + * @param vlans list of vlans/argument config that contains strings of server and option |
| 89 | + * |
| 90 | + * @return none |
| 91 | + */ |
| 92 | +void handleRelayNotification(swss::SubscriberStateTable &ipHelpersTable, std::vector<relay_config> *vlans) |
| 93 | +{ |
| 94 | + std::deque<swss::KeyOpFieldsValuesTuple> entries; |
| 95 | + |
| 96 | + ipHelpersTable.pops(entries); |
| 97 | + processRelayNotification(entries, vlans); |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * @code void processRelayNotification(std::deque<swss::KeyOpFieldsValuesTuple> &entries, std::vector<relay_config> *vlans) |
| 102 | + * |
| 103 | + * @brief process DHCPv6 relay servers and options configuration change notification |
| 104 | + * |
| 105 | + * @param entries queue of std::tuple<std::string, std::string, std::vector<FieldValueTuple>> entries in DHCP table |
| 106 | + * @param vlans list of vlans/argument config that contains strings of server and option |
| 107 | + * |
| 108 | + * @return none |
| 109 | + */ |
| 110 | +void processRelayNotification(std::deque<swss::KeyOpFieldsValuesTuple> &entries, std::vector<relay_config> *vlans) |
| 111 | +{ |
| 112 | + std::vector<std::string> servers; |
| 113 | + |
| 114 | + for (auto &entry: entries) { |
| 115 | + std::string vlan = kfvKey(entry); |
| 116 | + std::string operation = kfvOp(entry); |
| 117 | + std::vector<swss::FieldValueTuple> fieldValues = kfvFieldsValues(entry); |
| 118 | + |
| 119 | + relay_config intf; |
| 120 | + intf.is_option_79 = true; |
| 121 | + intf.interface = vlan; |
| 122 | + intf.db = nullptr; |
| 123 | + for (auto &fieldValue: fieldValues) { |
| 124 | + std::string f = fvField(fieldValue); |
| 125 | + std::string v = fvValue(fieldValue); |
| 126 | + if(f == "dhcpv6_servers") { |
| 127 | + std::stringstream ss(v); |
| 128 | + while (ss.good()) { |
| 129 | + std::string substr; |
| 130 | + getline(ss, substr, ','); |
| 131 | + intf.servers.push_back(substr); |
| 132 | + } |
| 133 | + syslog(LOG_DEBUG, "key: %s, Operation: %s, f: %s, v: %s", vlan.c_str(), operation.c_str(), f.c_str(), v.c_str()); |
| 134 | + } |
| 135 | + if(f == "dhcpv6_option|rfc6939_support" && v == "false") { |
| 136 | + intf.is_option_79 = false; |
| 137 | + } |
| 138 | + } |
| 139 | + vlans->push_back(intf); |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | +*@code stopSwssNotificationPoll |
| 145 | +* |
| 146 | +*@brief stop SWSS listening thread |
| 147 | +* |
| 148 | +*@return none |
| 149 | +*/ |
| 150 | +void stopSwssNotificationPoll() { |
| 151 | + pollSwssNotifcation = false; |
| 152 | +}; |
0 commit comments