|
1 | 1 | #include <algorithm>
|
| 2 | +#include <regex> |
| 3 | +#include <sstream> |
| 4 | +#include <string> |
2 | 5 | #include <net/if.h>
|
3 | 6 |
|
4 | 7 | #include "logger.h"
|
@@ -155,6 +158,13 @@ VxlanMgr::VxlanMgr(DBConnector *cfgDb, DBConnector *appDb, DBConnector *stateDb,
|
155 | 158 | m_stateVrfTable(stateDb, STATE_VRF_TABLE_NAME),
|
156 | 159 | m_stateVxlanTable(stateDb, STATE_VXLAN_TABLE_NAME)
|
157 | 160 | {
|
| 161 | + // Clear old vxlan devices that were created at last time. |
| 162 | + clearAllVxlanDevices(); |
| 163 | +} |
| 164 | + |
| 165 | +VxlanMgr::~VxlanMgr() |
| 166 | +{ |
| 167 | + clearAllVxlanDevices(); |
158 | 168 | }
|
159 | 169 |
|
160 | 170 | void VxlanMgr::doTask(Consumer &consumer)
|
@@ -526,5 +536,38 @@ bool VxlanMgr::deleteVxlan(const VxlanInfo & info)
|
526 | 536 | return true;
|
527 | 537 | }
|
528 | 538 |
|
529 |
| - |
| 539 | +void VxlanMgr::clearAllVxlanDevices() |
| 540 | +{ |
| 541 | + std::string stdout; |
| 542 | + const std::string cmd = std::string("") + IP_CMD + " link"; |
| 543 | + int ret = EXECUTE(cmd, stdout); |
| 544 | + if (ret != 0) |
| 545 | + { |
| 546 | + SWSS_LOG_ERROR("Cannot get devices by command : %s", cmd.c_str()); |
| 547 | + return; |
| 548 | + } |
| 549 | + std::regex device_name_pattern("^\\d+:\\s+([^:]+)"); |
| 550 | + std::smatch match_result; |
| 551 | + auto lines = tokenize(stdout, '\n'); |
| 552 | + for (const std::string & line : lines) |
| 553 | + { |
| 554 | + if (!std::regex_search(line, match_result, device_name_pattern)) |
| 555 | + { |
| 556 | + continue; |
| 557 | + } |
| 558 | + std::string res; |
| 559 | + std::string device_name = match_result[1]; |
| 560 | + VxlanInfo info; |
| 561 | + if (device_name.find(VXLAN_NAME_PREFIX) == 0) |
| 562 | + { |
| 563 | + info.m_vxlan = device_name; |
| 564 | + cmdDeleteVxlan(info, res); |
| 565 | + } |
| 566 | + else if (device_name.find(VXLAN_IF_NAME_PREFIX) == 0) |
| 567 | + { |
| 568 | + info.m_vxlanIf = device_name; |
| 569 | + cmdDeleteVxlanIf(info, res); |
| 570 | + } |
| 571 | + } |
| 572 | +} |
530 | 573 |
|
0 commit comments