Skip to content

Commit 1cedf6b

Browse files
committed
Orchagent changes in sonic-swss submodule to support NAT feature. (sonic-net#1125)
Signed-off-by: Akhilesh Samineni [email protected]
1 parent dc7e5f0 commit 1cedf6b

19 files changed

+5736
-10
lines changed

orchagent/Makefile.am

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ orchagent_SOURCES = \
5555
policerorch.cpp \
5656
sfloworch.cpp \
5757
chassisorch.cpp \
58-
debugcounterorch.cpp
58+
debugcounterorch.cpp \
59+
natorch.cpp
5960

6061
orchagent_SOURCES += flex_counter/flex_counter_manager.cpp flex_counter/flex_counter_stat_manager.cpp
6162
orchagent_SOURCES += debug_counter/debug_counter.cpp debug_counter/drop_counter.cpp

orchagent/aclorch.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ static acl_rule_attr_lookup_t aclL3ActionLookup =
6565
{
6666
{ ACTION_PACKET_ACTION, SAI_ACL_ENTRY_ATTR_ACTION_PACKET_ACTION },
6767
{ ACTION_REDIRECT_ACTION, SAI_ACL_ENTRY_ATTR_ACTION_REDIRECT },
68+
{ ACTION_DO_NOT_NAT_ACTION, SAI_ACL_ENTRY_ATTR_ACTION_NO_NAT },
6869
};
6970

7071
static acl_rule_attr_lookup_t aclMirrorStageLookup =
@@ -797,6 +798,12 @@ bool AclRuleL3::validateAddAction(string attr_name, string _attr_value)
797798

798799
action_str = ACTION_REDIRECT_ACTION;
799800
}
801+
// handle PACKET_ACTION_DO_NOT_NAT in ACTION_PACKET_ACTION
802+
else if (attr_value == PACKET_ACTION_DO_NOT_NAT)
803+
{
804+
value.aclaction.parameter.booldata = true;
805+
action_str = ACTION_DO_NOT_NAT_ACTION;
806+
}
800807
else
801808
{
802809
return false;

orchagent/aclorch.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363

6464
#define ACTION_PACKET_ACTION "PACKET_ACTION"
6565
#define ACTION_REDIRECT_ACTION "REDIRECT_ACTION"
66+
#define ACTION_DO_NOT_NAT_ACTION "DO_NOT_NAT_ACTION"
6667
#define ACTION_MIRROR_ACTION "MIRROR_ACTION"
6768
#define ACTION_MIRROR_INGRESS_ACTION "MIRROR_INGRESS_ACTION"
6869
#define ACTION_MIRROR_EGRESS_ACTION "MIRROR_EGRESS_ACTION"
@@ -73,9 +74,10 @@
7374
#define ACTION_DTEL_FLOW_SAMPLE_PERCENT "FLOW_SAMPLE_PERCENT"
7475
#define ACTION_DTEL_REPORT_ALL_PACKETS "REPORT_ALL_PACKETS"
7576

76-
#define PACKET_ACTION_FORWARD "FORWARD"
77-
#define PACKET_ACTION_DROP "DROP"
78-
#define PACKET_ACTION_REDIRECT "REDIRECT"
77+
#define PACKET_ACTION_FORWARD "FORWARD"
78+
#define PACKET_ACTION_DROP "DROP"
79+
#define PACKET_ACTION_REDIRECT "REDIRECT"
80+
#define PACKET_ACTION_DO_NOT_NAT "DO_NOT_NAT"
7981

8082
#define DTEL_FLOW_OP_NOP "NOP"
8183
#define DTEL_FLOW_OP_POSTCARD "POSTCARD"

orchagent/copporch.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ extern sai_switch_api_t* sai_switch_api;
1818

1919
extern sai_object_id_t gSwitchId;
2020
extern PortsOrch* gPortsOrch;
21+
extern bool gIsNatSupported;
2122

2223
static map<string, sai_meter_type_t> policer_meter_map = {
2324
{"packets", SAI_METER_TYPE_PACKETS},
@@ -72,7 +73,9 @@ static map<string, sai_hostif_trap_type_t> trap_id_map = {
7273
{"ttl_error", SAI_HOSTIF_TRAP_TYPE_TTL_ERROR},
7374
{"udld", SAI_HOSTIF_TRAP_TYPE_UDLD},
7475
{"bfd", SAI_HOSTIF_TRAP_TYPE_BFD},
75-
{"bfdv6", SAI_HOSTIF_TRAP_TYPE_BFDV6}
76+
{"bfdv6", SAI_HOSTIF_TRAP_TYPE_BFDV6},
77+
{"src_nat_miss", SAI_HOSTIF_TRAP_TYPE_SNAT_MISS},
78+
{"dest_nat_miss", SAI_HOSTIF_TRAP_TYPE_DNAT_MISS}
7679
};
7780

7881
static map<string, sai_packet_action_t> packet_action_map = {
@@ -189,6 +192,12 @@ void CoppOrch::getTrapIdList(vector<string> &trap_id_name_list, vector<sai_hosti
189192
SWSS_LOG_DEBUG("processing trap_id:%s", trap_id_str.c_str());
190193
trap_id = trap_id_map.at(trap_id_str);
191194
SWSS_LOG_DEBUG("Pushing trap_id:%d", trap_id);
195+
if (((trap_id == SAI_HOSTIF_TRAP_TYPE_SNAT_MISS) or (trap_id == SAI_HOSTIF_TRAP_TYPE_DNAT_MISS)) and
196+
(gIsNatSupported == false))
197+
{
198+
SWSS_LOG_NOTICE("Ignoring the trap_id: %s, as NAT is not supported", trap_id_str.c_str());
199+
continue;
200+
}
192201
trap_id_list.push_back(trap_id);
193202
}
194203
}

orchagent/intfsorch.cpp

+78-1
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ extern Directory<Orch*> gDirectory;
2323
extern sai_router_interface_api_t* sai_router_intfs_api;
2424
extern sai_route_api_t* sai_route_api;
2525
extern sai_neighbor_api_t* sai_neighbor_api;
26+
extern sai_switch_api_t* sai_switch_api;
2627

2728
extern sai_object_id_t gSwitchId;
2829
extern PortsOrch *gPortsOrch;
2930
extern RouteOrch *gRouteOrch;
3031
extern CrmOrch *gCrmOrch;
3132
extern BufferOrch *gBufferOrch;
33+
extern bool gIsNatSupported;
3234

3335
const int intfsorch_pri = 35;
3436

@@ -165,6 +167,35 @@ bool IntfsOrch::setRouterIntfsMtu(const Port &port)
165167
return true;
166168
}
167169

170+
bool IntfsOrch::setRouterIntfsNatZoneId(Port &port)
171+
{
172+
SWSS_LOG_ENTER();
173+
174+
/* Return true if the router interface is not exists */
175+
if (!port.m_rif_id)
176+
{
177+
SWSS_LOG_WARN("Router interface is not exists on %s",
178+
port.m_alias.c_str());
179+
return true;
180+
}
181+
182+
sai_attribute_t attr;
183+
attr.id = SAI_ROUTER_INTERFACE_ATTR_NAT_ZONE_ID;
184+
attr.value.u32 = port.m_nat_zone_id;
185+
186+
sai_status_t status = sai_router_intfs_api->
187+
set_router_interface_attribute(port.m_rif_id, &attr);
188+
if (status != SAI_STATUS_SUCCESS)
189+
{
190+
SWSS_LOG_ERROR("Failed to set router interface %s NAT Zone Id to %u, rv:%d",
191+
port.m_alias.c_str(), port.m_nat_zone_id, status);
192+
return false;
193+
}
194+
SWSS_LOG_NOTICE("Set router interface %s NAT Zone Id to %u",
195+
port.m_alias.c_str(), port.m_nat_zone_id);
196+
return true;
197+
}
198+
168199
bool IntfsOrch::setRouterIntfsAdminStatus(const Port &port)
169200
{
170201
SWSS_LOG_ENTER();
@@ -404,9 +435,11 @@ void IntfsOrch::doTask(Consumer &consumer)
404435
}
405436

406437
const vector<FieldValueTuple>& data = kfvFieldsValues(t);
407-
string vrf_name = "", vnet_name = "";
438+
string vrf_name = "", vnet_name = "", nat_zone = "";
408439
uint32_t mtu;
409440
bool adminUp;
441+
uint32_t nat_zone_id = 0;
442+
410443
for (auto idx : data)
411444
{
412445
const auto &field = fvField(idx);
@@ -419,6 +452,19 @@ void IntfsOrch::doTask(Consumer &consumer)
419452
{
420453
vnet_name = value;
421454
}
455+
else if (field == "nat_zone")
456+
{
457+
try
458+
{
459+
nat_zone_id = (uint32_t)stoul(value);
460+
}
461+
catch (...)
462+
{
463+
SWSS_LOG_ERROR("Invalid argument %s for nat zone", value.c_str());
464+
continue;
465+
}
466+
nat_zone = value;
467+
}
422468
else if (field == "mtu")
423469
{
424470
try
@@ -452,6 +498,10 @@ void IntfsOrch::doTask(Consumer &consumer)
452498
}
453499
}
454500
}
501+
else if (field == "nat_zone")
502+
{
503+
nat_zone = value;
504+
}
455505
}
456506

457507
if (alias == "eth0" || alias == "docker0")
@@ -555,6 +605,23 @@ void IntfsOrch::doTask(Consumer &consumer)
555605
it++;
556606
continue;
557607
}
608+
609+
/* Set nat zone id */
610+
if ((!nat_zone.empty()) and (port.m_nat_zone_id != nat_zone_id))
611+
{
612+
port.m_nat_zone_id = nat_zone_id;
613+
614+
if (gIsNatSupported)
615+
{
616+
setRouterIntfsNatZoneId(port);
617+
}
618+
else
619+
{
620+
SWSS_LOG_NOTICE("Not set router interface %s NAT Zone Id to %u, as NAT is not supported",
621+
port.m_alias.c_str(), port.m_nat_zone_id);
622+
}
623+
gPortsOrch->setPort(alias, port);
624+
}
558625
}
559626

560627
it = consumer.m_toSync.erase(it);
@@ -739,6 +806,15 @@ bool IntfsOrch::addRouterIntfs(sai_object_id_t vrf_id, Port &port)
739806
attr.value.u32 = port.m_mtu;
740807
attrs.push_back(attr);
741808

809+
if (gIsNatSupported)
810+
{
811+
attr.id = SAI_ROUTER_INTERFACE_ATTR_NAT_ZONE_ID;
812+
attr.value.u32 = port.m_nat_zone_id;
813+
814+
SWSS_LOG_INFO("Assinging NAT zone id %d to interface %s\n", attr.value.u32, port.m_alias.c_str());
815+
attrs.push_back(attr);
816+
}
817+
742818
sai_status_t status = sai_router_intfs_api->create_router_interface(&port.m_rif_id, gSwitchId, (uint32_t)attrs.size(), attrs.data());
743819
if (status != SAI_STATUS_SUCCESS)
744820
{
@@ -779,6 +855,7 @@ bool IntfsOrch::removeRouterIntfs(Port &port)
779855

780856
port.m_rif_id = 0;
781857
port.m_vr_id = 0;
858+
port.m_nat_zone_id = 0;
782859
gPortsOrch->setPort(port.m_alias, port);
783860

784861
SWSS_LOG_NOTICE("Remove router interface for port %s", port.m_alias.c_str());

orchagent/intfsorch.h

+2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ class IntfsOrch : public Orch
4040
void decreaseRouterIntfsRefCount(const string&);
4141

4242
bool setRouterIntfsMtu(const Port &port);
43+
bool setRouterIntfsNatZoneId(Port &port);
4344
bool setRouterIntfsAdminStatus(const Port &port);
45+
4446
std::set<IpPrefix> getSubnetRoutes();
4547

4648
void generateInterfaceMap();

orchagent/main.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ bool gLogRotate = false;
5050
bool gSaiRedisLogRotate = false;
5151
bool gSyncMode = false;
5252

53+
extern bool gIsNatSupported;
54+
5355
ofstream gRecordOfs;
5456
string gRecordFile;
5557

@@ -262,6 +264,22 @@ int main(int argc, char **argv)
262264
gVirtualRouterId = attr.value.oid;
263265
SWSS_LOG_NOTICE("Get switch virtual router ID %" PRIx64, gVirtualRouterId);
264266

267+
/* Get the NAT supported info */
268+
attr.id = SAI_SWITCH_ATTR_AVAILABLE_SNAT_ENTRY;
269+
270+
status = sai_switch_api->get_switch_attribute(gSwitchId, 1, &attr);
271+
if (status != SAI_STATUS_SUCCESS)
272+
{
273+
SWSS_LOG_NOTICE("Failed to get the SNAT available entry count, rv:%d", status);
274+
}
275+
else
276+
{
277+
if (attr.value.u32 != 0)
278+
{
279+
gIsNatSupported = true;
280+
}
281+
}
282+
265283
/* Create a loopback underlay router interface */
266284
vector<sai_attribute_t> underlay_intf_attrs;
267285

0 commit comments

Comments
 (0)