Skip to content

Commit 5b7e784

Browse files
authored
Remove ports from default VLAN at start and set port VLAN ID afterwards (sonic-net#43)
* orchagent: Adding logic to remove all ports from default VLAN * orchagent: Adding set SAI_PORT_ATTR_PORT_VLAN_ID logic - When joining a VLAN, set the port VLAN ID to VLAN ID - When departing from a VLAN, reset the port VLAN ID to default 1
1 parent f00fad6 commit 5b7e784

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

orchagent/port.h

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ extern "C" {
99
#include <set>
1010
#include <string>
1111

12+
#define DEFAULT_PORT_VLAN_ID 1
13+
1214
namespace swss {
1315

1416
class Port
@@ -49,6 +51,7 @@ class Port
4951
int m_ifindex = 0;
5052
sai_object_id_t m_port_id = 0;
5153
sai_vlan_id_t m_vlan_id = 0;
54+
sai_vlan_id_t m_port_vlan_id = DEFAULT_PORT_VLAN_ID; // Port VLAN ID
5255
sai_object_id_t m_vlan_member_id = 0;
5356
sai_object_id_t m_rif_id = 0;
5457
sai_object_id_t m_hif_id = 0;

orchagent/portsorch.cpp

+55-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ extern sai_vlan_api_t *sai_vlan_api;
1515
extern sai_lag_api_t *sai_lag_api;
1616
extern sai_hostif_api_t* sai_hostif_api;
1717

18-
#define VLAN_PREFIX "Vlan"
18+
#define VLAN_PREFIX "Vlan"
19+
#define DEFAULT_VLAN_ID 1
1920

2021
PortsOrch::PortsOrch(DBConnector *db, vector<string> tableNames) :
2122
Orch(db, tableNames)
@@ -92,10 +93,10 @@ PortsOrch::PortsOrch(DBConnector *db, vector<string> tableNames) :
9293
status = sai_switch_api->get_switch_attribute(1, &attr);
9394
if (status != SAI_STATUS_SUCCESS)
9495
{
95-
SWSS_LOG_ERROR("\n");
96+
SWSS_LOG_ERROR("Failed to get port list");
9697
}
9798

98-
/* Get port lane info */
99+
/* Get port hardware lane info */
99100
for (i = 0; i < (int)m_portCount; i++)
100101
{
101102
sai_uint32_t lanes[4];
@@ -124,7 +125,7 @@ PortsOrch::PortsOrch(DBConnector *db, vector<string> tableNames) :
124125
m_portListLaneMap[tmp_lane_set] = port_list[i];
125126
}
126127

127-
/* Set port hardware learn mode */
128+
/* Set port to hardware learn mode */
128129
for (i = 0; i < (int)m_portCount; i++)
129130
{
130131
attr.id = SAI_PORT_ATTR_FDB_LEARNING;
@@ -133,7 +134,29 @@ PortsOrch::PortsOrch(DBConnector *db, vector<string> tableNames) :
133134
status = sai_port_api->set_port_attribute(port_list[i], &attr);
134135
if (status != SAI_STATUS_SUCCESS)
135136
{
136-
SWSS_LOG_ERROR("Failed to set port hardware learn mode pid:%llx\n", port_list[i]);
137+
SWSS_LOG_ERROR("Failed to set port to hardware learn mode pid:%llx\n", port_list[i]);
138+
}
139+
}
140+
141+
/* Get default VLAN member list */
142+
sai_object_id_t *vlan_member_list = new sai_object_id_t[m_portCount];
143+
attr.id = SAI_VLAN_ATTR_MEMBER_LIST;
144+
attr.value.objlist.count = m_portCount;
145+
attr.value.objlist.list = vlan_member_list;
146+
147+
status = sai_vlan_api->get_vlan_attribute(DEFAULT_VLAN_ID, 1, &attr);
148+
if (status != SAI_STATUS_SUCCESS)
149+
{
150+
SWSS_LOG_ERROR("Failed to get default VLAN member list");
151+
}
152+
153+
/* Remove port from default VLAN */
154+
for (i = 0; i < (int)m_portCount; i++)
155+
{
156+
status = sai_vlan_api->remove_vlan_member(vlan_member_list[i]);
157+
if (status != SAI_STATUS_SUCCESS)
158+
{
159+
SWSS_LOG_ERROR("Failed to remove port from default VLAN %d", i);
137160
}
138161
}
139162
}
@@ -663,7 +686,21 @@ bool PortsOrch::addVlanMember(Port vlan, Port port)
663686
SWSS_LOG_NOTICE("Add member %s to VLAN %s vid:%hu pid%llx",
664687
port.m_alias.c_str(), vlan.m_alias.c_str(), vlan.m_vlan_id, port.m_port_id);
665688

689+
attr.id = SAI_PORT_ATTR_PORT_VLAN_ID;
690+
attr.value.u16 = vlan.m_vlan_id;
691+
692+
status = sai_port_api->set_port_attribute(port.m_port_id, &attr);
693+
if (status != SAI_STATUS_SUCCESS)
694+
{
695+
SWSS_LOG_ERROR("Failed to set port VLAN ID vid:%hu pid:%llx",
696+
vlan.m_vlan_id, port.m_port_id);
697+
return false;
698+
}
699+
700+
SWSS_LOG_NOTICE("Set port %s VLAN ID to %hu", port.m_alias.c_str(), vlan.m_vlan_id);
701+
666702
port.m_vlan_id = vlan.m_vlan_id;
703+
port.m_port_vlan_id = vlan.m_vlan_id;
667704
port.m_vlan_member_id = vlan_member_id;
668705
m_portList[port.m_alias] = port;
669706
vlan.m_members.insert(port.m_alias);
@@ -688,7 +725,20 @@ bool PortsOrch::removeVlanMember(Port vlan, Port port)
688725
SWSS_LOG_ERROR("Remove member %s from VLAN %s lid:%hx vmid:%llx",
689726
port.m_alias.c_str(), vlan.m_alias.c_str(), vlan.m_vlan_id, port.m_vlan_member_id);
690727

728+
sai_attribute_t attr;
729+
attr.id = SAI_PORT_ATTR_PORT_VLAN_ID;
730+
attr.value.u16 = DEFAULT_PORT_VLAN_ID;
731+
732+
status = sai_port_api->set_port_attribute(port.m_port_id, &attr);
733+
if (status != SAI_STATUS_SUCCESS)
734+
{
735+
SWSS_LOG_ERROR("Failed to reset port VLAN ID to DEFAULT_PORT_VLAN_ID pid:%llx",
736+
port.m_port_id);
737+
return false;
738+
}
739+
691740
port.m_vlan_id = 0;
741+
port.m_port_vlan_id = DEFAULT_PORT_VLAN_ID;
692742
port.m_vlan_member_id = 0;
693743
m_portList[port.m_alias] = port;
694744
vlan.m_members.erase(port.m_alias);

0 commit comments

Comments
 (0)