|
| 1 | +/* |
| 2 | + * Copyright 2019 Broadcom Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#include <swss/logger.h> |
| 17 | +#include <swss/dbconnector.h> |
| 18 | +#include <swss/producerstatetable.h> |
| 19 | +#include <swss/macaddress.h> |
| 20 | +#include <swss/tokenize.h> |
| 21 | +#include "pac_authmgrcfg.h" |
| 22 | + |
| 23 | +using namespace std; |
| 24 | +using namespace swss; |
| 25 | + |
| 26 | +// PAC SONIC config engine |
| 27 | +PacCfg::PacCfg(DBConnector *db, DBConnector *cfgDb, DBConnector *stateDb) : |
| 28 | + m_cfgFdbTable(cfgDb, CFG_FDB_TABLE_NAME), |
| 29 | + m_stateOperFdbTable(stateDb, STATE_OPER_FDB_TABLE_NAME), |
| 30 | + m_stateOperPortTable(stateDb, STATE_OPER_PORT_TABLE_NAME) |
| 31 | +{ |
| 32 | + Logger::linkToDbNative("paccfg"); |
| 33 | + SWSS_LOG_NOTICE("PAC: config object"); |
| 34 | + |
| 35 | + /* FDB flush notification producer */ |
| 36 | + m_flushFdb = std::make_shared<swss::NotificationProducer>(db, "FLUSHFDBREQUEST"); |
| 37 | +} |
| 38 | + |
| 39 | +PacCfg::~PacCfg() |
| 40 | +{ |
| 41 | + |
| 42 | +} |
| 43 | + |
| 44 | +// Set learning mode for a port |
| 45 | +bool PacCfg::intfLearningSet(string port, string learning) |
| 46 | +{ |
| 47 | + string key(port); |
| 48 | + vector<FieldValueTuple> fvVector; |
| 49 | + |
| 50 | + // Configure port learning mode for FDB manager |
| 51 | + fvVector.emplace_back("learn_mode", learning); |
| 52 | + m_stateOperPortTable.set(key, fvVector); |
| 53 | + |
| 54 | + return true; |
| 55 | +} |
| 56 | + |
| 57 | +// Get learning mode of a port |
| 58 | +bool PacCfg::intfLearningGet(string port, string *learning) |
| 59 | +{ |
| 60 | + return true; |
| 61 | +} |
| 62 | + |
| 63 | +// Add a static MAC address to FDB |
| 64 | +bool PacCfg::intfStaticMacAdd(string port, MacAddress mac, int vlan) |
| 65 | +{ |
| 66 | + string key = VLAN_PREFIX + to_string(vlan); |
| 67 | + key += STATE_DB_SEPARATOR; |
| 68 | + key += mac.to_string(); |
| 69 | + |
| 70 | + vector<FieldValueTuple> fvVector; |
| 71 | + |
| 72 | + fvVector.push_back(FieldValueTuple("port", port)); |
| 73 | + fvVector.push_back(FieldValueTuple("type", "static")); |
| 74 | + m_stateOperFdbTable.set(key, fvVector); |
| 75 | + |
| 76 | + return true; |
| 77 | +} |
| 78 | + |
| 79 | +// Remove an added static MAC address. |
| 80 | +bool PacCfg::intfStaticMacRemove(string port, MacAddress mac, int vlan) |
| 81 | +{ |
| 82 | + string key = VLAN_PREFIX + to_string(vlan); |
| 83 | + key += STATE_DB_SEPARATOR; |
| 84 | + key += mac.to_string(); |
| 85 | + |
| 86 | + m_stateOperFdbTable.del(key); |
| 87 | + |
| 88 | + return true; |
| 89 | +} |
| 90 | + |
| 91 | +// for now, blindly delete all entries. |
| 92 | +// Ideally we need to delete only the entries owned by PAC. |
| 93 | +void PacCfg::intfStaticMacCleanup(void) |
| 94 | +{ |
| 95 | + vector<string> keys; |
| 96 | + m_stateOperFdbTable.getKeys(keys); |
| 97 | + for (const auto key : keys) |
| 98 | + { |
| 99 | + m_stateOperFdbTable.del(key); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// Acquire/Release port. |
| 104 | +bool PacCfg::intfAcquireSet(string port, bool acquire) |
| 105 | +{ |
| 106 | + vector<FieldValueTuple> fvVector; |
| 107 | + |
| 108 | + // Configure port acquire config for port. |
| 109 | + if (acquire == true) |
| 110 | + { |
| 111 | + fvVector.emplace_back("acquired", "true"); |
| 112 | + } |
| 113 | + else |
| 114 | + { |
| 115 | + fvVector.emplace_back("acquired", "false"); |
| 116 | + } |
| 117 | + m_stateOperPortTable.set(port, fvVector); |
| 118 | + |
| 119 | + return true; |
| 120 | +} |
| 121 | + |
| 122 | +// Block a client |
| 123 | +bool PacCfg::intfClientBlock(string port, MacAddress mac, int vlan) |
| 124 | +{ |
| 125 | + // Add a static MAC entry with discard bits set. |
| 126 | + string key = VLAN_PREFIX + to_string(vlan); |
| 127 | + key += STATE_DB_SEPARATOR; |
| 128 | + key += mac.to_string(); |
| 129 | + |
| 130 | + vector<FieldValueTuple> fvVector; |
| 131 | + fvVector.push_back(FieldValueTuple("discard", "true")); |
| 132 | + fvVector.push_back(FieldValueTuple("port", port)); |
| 133 | + fvVector.push_back(FieldValueTuple("type", "static")); |
| 134 | + |
| 135 | + m_stateOperFdbTable.set(key, fvVector); |
| 136 | + |
| 137 | + return true; |
| 138 | +} |
| 139 | + |
| 140 | +bool PacCfg::intfFdbFlush(string port) |
| 141 | +{ |
| 142 | + vector<FieldValueTuple> values; |
| 143 | + |
| 144 | + SWSS_LOG_DEBUG("send fdb flush by port %s notification ", port.c_str()); |
| 145 | + |
| 146 | + // Send FDB flush notification. |
| 147 | + m_flushFdb->send("PORT", port, values); |
| 148 | + |
| 149 | + return true; |
| 150 | +} |
| 151 | + |
| 152 | +bool PacCfg::intfMacVlanTranslationAdd(string port, MacAddress mac, int vlan) |
| 153 | +{ |
| 154 | + // Add MAC-VLAN translation for given MAC-VLAN pair. |
| 155 | + return true; |
| 156 | +} |
| 157 | + |
| 158 | +bool PacCfg::intfMacVlanTranslationRemove(string port, MacAddress mac, int vlan) |
| 159 | +{ |
| 160 | + // Remove MAC-VLAN translation for given MAC-VLAN pair. |
| 161 | + return true; |
| 162 | +} |
| 163 | + |
| 164 | +bool PacCfg::sendFdbNotification(string op, string port) |
| 165 | +{ |
| 166 | + vector<string> keys; |
| 167 | + vector<FieldValueTuple> entry; |
| 168 | + const char delimiter = '|'; |
| 169 | + |
| 170 | + /* Retrieve static MAC entries configure on port and |
| 171 | + * send a notification to add/remove those entries on port. |
| 172 | + */ |
| 173 | + m_cfgFdbTable.getKeys(keys); |
| 174 | + |
| 175 | + for (auto id : keys) |
| 176 | + { |
| 177 | + m_cfgFdbTable.get(id, entry); |
| 178 | + for (auto i : entry) |
| 179 | + { |
| 180 | + if (fvField(i) == "port") |
| 181 | + { |
| 182 | + if (fvValue(i) == port) |
| 183 | + { |
| 184 | + vector<FieldValueTuple> values; |
| 185 | + |
| 186 | + /* Get MAC and VLAN from key */ |
| 187 | + vector <string> tokens = tokenize(id, delimiter, 1); |
| 188 | + |
| 189 | + /* tokens[0] is VLAN (Vlan20) and tokens[1] is mac (00:01:02:03:04:05). */ |
| 190 | + values.push_back(FieldValueTuple("mac", tokens[1])); |
| 191 | + values.push_back(FieldValueTuple("Vlan", tokens[0])); |
| 192 | + m_fdbCfgNotificationProducer->send(op, port, values); |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + return true; |
| 199 | +} |
0 commit comments