Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding vrrporch module #3315

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions orchagent/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ orchagent_SOURCES = \
crmorch.cpp \
request_parser.cpp \
vrforch.cpp \
vrrporch.cpp \
countercheckorch.cpp \
vxlanorch.cpp \
vnetorch.cpp \
Expand Down
3 changes: 3 additions & 0 deletions orchagent/orchdaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ bool OrchDaemon::init()
gDirectory.set(vnet_rt_orch);
VRFOrch *vrf_orch = new VRFOrch(m_applDb, APP_VRF_TABLE_NAME, m_stateDb, STATE_VRF_OBJECT_TABLE_NAME);
gDirectory.set(vrf_orch);
VrrpOrch *vrrp_orch = new VrrpOrch(m_applDb, APP_VRRP_TABLE_NAME);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

APP_VRRP_TABLE_NAME name is not defined. It needs to be defined in common/schema.h.

gDirectory.set(vrrp_orch);
gMonitorOrch = new MonitorOrch(m_stateDb, STATE_VNET_MONITOR_TABLE_NAME);
gDirectory.set(gMonitorOrch);

Expand Down Expand Up @@ -492,6 +494,7 @@ bool OrchDaemon::init()
m_orchList.push_back(gPbhOrch);
m_orchList.push_back(chassis_frontend_orch);
m_orchList.push_back(vrf_orch);
m_orchList.push_back(vrrp_orch);
m_orchList.push_back(vxlan_tunnel_orch);
m_orchList.push_back(evpn_nvo_orch);
m_orchList.push_back(vxlan_tunnel_map_orch);
Expand Down
1 change: 1 addition & 0 deletions orchagent/orchdaemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "switchorch.h"
#include "crmorch.h"
#include "vrforch.h"
#include "vrrporch.h"
#include "vxlanorch.h"
#include "vnetorch.h"
#include "countercheckorch.h"
Expand Down
244 changes: 244 additions & 0 deletions orchagent/vrrporch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
#include "sai.h"
#include "macaddress.h"
#include "orch.h"
#include "request_parser.h"
#include "portsorch.h"
#include "port.h"
#include "swssnet.h"
#include "fdborch.h"
#include "intfsorch.h"
#include "vrrporch.h"

extern sai_router_interface_api_t* sai_router_intfs_api;
extern PortsOrch *gPortsOrch;
extern sai_object_id_t gSwitchId;
extern sai_route_api_t* sai_route_api;
extern FdbOrch *gFdbOrch;
extern IntfsOrch *gIntfsOrch;
bool VrrpOrch::hasSameIpAddr(const string &alias,const IpPrefix &vip_prefix)
{
IntfsTable m_syncdIntfses = gIntfsOrch->getSyncdIntfses();
for (const auto &intfPrefix: m_syncdIntfses[alias].ip_addresses)
{
if (intfPrefix.getIp() == vip_prefix.getIp())
{
SWSS_LOG_NOTICE("vrrp hasSameIpAddr configured %s vip %s",alias.c_str(),vip_prefix.to_string().c_str());
return true;
}
}
return false;
}
bool VrrpOrch::addOperation(const Request& request)
{
SWSS_LOG_ENTER();
sai_attribute_t attr;
vector<sai_attribute_t> vmac_attrs;
vector<sai_attribute_t> vip_attrs;
Port port;
MacAddress mac;
sai_object_id_t vrrp_rif_id;
sai_object_id_t port_oid;
for (const auto& name: request.getAttrFieldNames())
{
if (name == "vmac")
{
mac = request.getAttrMacAddress("vmac");
attr.id = SAI_ROUTER_INTERFACE_ATTR_SRC_MAC_ADDRESS;
memcpy(attr.value.mac, mac.getMac(), sizeof(sai_mac_t));
SWSS_LOG_INFO("vrrp orch add : vmac %s ",mac.to_string().c_str());
vmac_attrs.push_back(attr);
}
}
auto ip_pfx = request.getKeyIpPrefix(1);
/* Check if the vmac,vip combination is already programmed on the port.If yes, skip & return*/
auto key = vrrp_key_t(request.getKeyString(0),request.getKeyIpPrefix(1));
auto it = vrrp_table_.find(key);
if (it != vrrp_table_.end())
{
if (vrrp_table_[key].vmac == request.getAttrMacAddress("vmac"))
{
SWSS_LOG_ERROR("_vrrp_table entry already exists, with vmac %s, vip %s for port %s",mac.to_string().c_str(),
ip_pfx.to_string().c_str(),request.getKeyString(0).c_str());
return true;
}
}
gPortsOrch->getPort(request.getKeyString(0), port);
attr.id = SAI_ROUTER_INTERFACE_ATTR_TYPE;
switch(port.m_type)
{
case Port::PHY:
case Port::LAG:
attr.value.s32 = SAI_ROUTER_INTERFACE_TYPE_PORT;
break;
case Port::VLAN:
attr.value.s32 = SAI_ROUTER_INTERFACE_TYPE_VLAN;
break;
case Port::SUBPORT:
attr.value.s32 = SAI_ROUTER_INTERFACE_TYPE_SUB_PORT;
break;
default:
SWSS_LOG_ERROR("Unsupported port type: %d", port.m_type);
break;
}
vmac_attrs.push_back(attr);
switch(port.m_type)
{
case Port::PHY:
attr.id = SAI_ROUTER_INTERFACE_ATTR_PORT_ID;
attr.value.oid = port.m_port_id;
break;
case Port::LAG:
attr.id = SAI_ROUTER_INTERFACE_ATTR_PORT_ID;
attr.value.oid = port.m_lag_id;
break;
case Port::VLAN:
attr.id = SAI_ROUTER_INTERFACE_ATTR_VLAN_ID;
attr.value.oid = port.m_vlan_info.vlan_oid;
break;
case Port::SUBPORT:
attr.id = SAI_ROUTER_INTERFACE_ATTR_OUTER_VLAN_ID;
attr.value.u16 = port.m_vlan_info.vlan_id;
vmac_attrs.push_back(attr);
attr.id = SAI_ROUTER_INTERFACE_ATTR_PORT_ID;
attr.value.oid = port.m_parent_port_id;
break;
default:
SWSS_LOG_ERROR("Unsupported port type: %d", port.m_type);
break;
}
port_oid = attr.value.oid;
vmac_attrs.push_back(attr);
SWSS_LOG_NOTICE("vrrp orch add : port %s, ip %s, vmac %s",request.getKeyString(0).c_str(),ip_pfx.to_string().c_str(),mac.to_string().c_str());
sai_route_entry_t unicast_route_entry;
unicast_route_entry.switch_id = gSwitchId;
unicast_route_entry.vr_id = port.m_vr_id;
copy(unicast_route_entry.destination, ip_pfx.getIp());
subnet(unicast_route_entry.destination, unicast_route_entry.destination);
attr.id = SAI_ROUTER_INTERFACE_ATTR_VIRTUAL_ROUTER_ID;
attr.value.oid = gVirtualRouterId;
vmac_attrs.push_back(attr);
attr.id = SAI_ROUTER_INTERFACE_ATTR_IS_VIRTUAL;
attr.value.booldata = true;
vmac_attrs.push_back(attr);
//program VRRPMAC.
SWSS_LOG_INFO("vrrp orch add, before create_router_interface : port name %s, rif_id %lx, &rif_id %p",request.getKeyString(0).c_str(),vrrp_rif_id,&vrrp_rif_id);
sai_status_t vmac_status = sai_router_intfs_api->create_router_interface(&vrrp_rif_id, gSwitchId, (uint32_t)vmac_attrs.size(), vmac_attrs.data());
SWSS_LOG_NOTICE("vrrp orch add, after create_router_interface : port name %s, rif_id %lx",request.getKeyString(0).c_str(),vrrp_rif_id);
if (vmac_status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to program Vrrp Mac on interface %s, rv:%d",
port.m_alias.c_str(), vmac_status);
throw runtime_error("Failed to program Vrrp Mac on interface.");
}
//program VIP
if (!hasSameIpAddr (request.getKeyString(0),request.getKeyIpPrefix(1)))
{
attr.id = SAI_ROUTE_ENTRY_ATTR_PACKET_ACTION;
attr.value.s32 = SAI_PACKET_ACTION_FORWARD;
vip_attrs.push_back(attr);
attr.id = SAI_ROUTE_ENTRY_ATTR_NEXT_HOP_ID;
attr.value.oid = vrrp_rif_id;
vip_attrs.push_back(attr);
//API to program VRRP ipaddress
sai_status_t vip_status = sai_route_api->create_route_entry(&unicast_route_entry, (uint32_t)vip_attrs.size(), vip_attrs.data());
if (vip_status != SAI_STATUS_SUCCESS)
{
sai_status_t rif_status = sai_router_intfs_api->remove_router_interface (vrrp_rif_id);
if (rif_status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to delete rif %lx added for Vrrp Mac on interface %s, rv:%d",
vrrp_rif_id, port.m_alias.c_str(), rif_status);
}
SWSS_LOG_ERROR("Failed to program Vrrp IP on interface %s, rv:%d",
port.m_alias.c_str(), vip_status);
throw runtime_error("Failed to program Vrrp Ip on interface.");
}
}
key = vrrp_key_t(request.getKeyString(0),request.getKeyIpPrefix(1));
it = vrrp_table_.find(key);
if (it == vrrp_table_.end())
{
vrrp_table_[key] = {request.getAttrMacAddress("vmac"),vrrp_rif_id};
SWSS_LOG_NOTICE("_vrrp_table add port %s, ip %s, vmac %s, rif_id %lx",request.getKeyString(0).c_str(),
ip_pfx.to_string().c_str(),mac.to_string().c_str(),vrrp_rif_id);
}
else
{
SWSS_LOG_NOTICE("_vrrp_table entry already exists with vmac %s, rif_id %lx, update vmac to %s, rifid to %lx",
vrrp_table_[key].vmac.to_string().c_str(),vrrp_table_[key].rifid,
request.getAttrMacAddress("vmac").to_string().c_str(),vrrp_rif_id);
vrrp_table_.erase(key);
vrrp_table_[key] = {request.getAttrMacAddress("vmac"),vrrp_rif_id};
}
//Flush the FDB entry for vmac on vrrp master
FdbEntry entry;
entry.mac = request.getAttrMacAddress("vmac");
entry.bv_id = port_oid;
gFdbOrch->removeFdbEntry(entry, FDB_ORIGIN_LEARN);
return true;
}
bool VrrpOrch::delOperation(const Request& request)
{
SWSS_LOG_ENTER();
Port port;
sai_object_id_t port_oid = 0;
auto key = vrrp_key_t(request.getKeyString(0),request.getKeyIpPrefix(1));
auto it = vrrp_table_.find(key);
if (it == vrrp_table_.end())
{
SWSS_LOG_ERROR("VRRP entry for port %s, vip %s doesn't exist", request.getKeyString(0).c_str(),request.getKeyIpPrefix(1).to_string().c_str());
return true;
}
gPortsOrch->getPort(request.getKeyString(0), port);
auto ip_pfx = request.getKeyIpPrefix(1);
sai_route_entry_t unicast_route_entry;
unicast_route_entry.switch_id = gSwitchId;
unicast_route_entry.vr_id = port.m_vr_id;
copy(unicast_route_entry.destination, ip_pfx.getIp());
switch(port.m_type)
{
case Port::PHY:
port_oid = port.m_port_id;
break;
case Port::LAG:
port_oid = port.m_lag_id;
break;
case Port::VLAN:
port_oid = port.m_vlan_info.vlan_oid;
break;
case Port::SUBPORT:
port_oid = port.m_parent_port_id;
break;
default:
SWSS_LOG_ERROR("Unsupported port type: %d", port.m_type);
break;
}
//Flush the FDB entry for vmac
FdbEntry entry;
entry.mac = vrrp_table_[key].vmac;
entry.bv_id = port_oid;
gFdbOrch->removeFdbEntry(entry, FDB_ORIGIN_LEARN);
//If same vip is configured as DIP on the port, then skip delete vip as it will delete the DIP on the port.
if (!hasSameIpAddr (request.getKeyString(0), request.getKeyIpPrefix(1)))
{
sai_status_t vip_status = sai_route_api->remove_route_entry(&unicast_route_entry);
if (vip_status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to delete Vrrp Vip on interface %s, rv:%d",
port.m_alias.c_str(), vip_status);
throw runtime_error("Failed to remove Vrrp Vip on interface");
}
}

sai_status_t vmac_status = sai_router_intfs_api->remove_router_interface (vrrp_table_[key].rifid);
if (vmac_status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Failed to delete Vrrp Mac on interface %s, rv:%d",
port.m_alias.c_str(), vmac_status);
throw runtime_error("Failed to remove Vrrp Mac on interface.");
}
SWSS_LOG_NOTICE("vrrp orch del success,port %s vip %s vmac %s rifid %lx",request.getKeyString(0).c_str(),
ip_pfx.to_string().c_str(),vrrp_table_[key].vmac.to_string().c_str(),vrrp_table_[key].rifid);
vrrp_table_.erase(key);
return true;
}
83 changes: 83 additions & 0 deletions orchagent/vrrporch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#ifndef VRRPORCH_H
#define VRRPORCH_H

#include <string>
#include <sstream>
#include "orch.h"
#include "request_parser.h"
#include "ipaddress.h"
#include "ipaddresses.h"
#include "ipprefix.h"
#include "macaddress.h"

extern sai_object_id_t gVirtualRouterId;

struct vrrp_key_t
{
std::string port_name;
IpPrefix ip_addr;

vrrp_key_t() = default;

vrrp_key_t(std::string portName, IpPrefix ipAddr)
{
port_name = portName;
ip_addr = ipAddr;
};

bool operator== (const vrrp_key_t& rhs) const
{
if (!(ip_addr == rhs.ip_addr) || port_name != rhs.port_name)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't "!(ip_addr == rhs.ip_addr)" be "(ip_addr != rhs.ip_addr)" similar to other case?

{
return false;
}
return true;
}
};

struct vrrp_data_t
{
MacAddress vmac;
sai_object_id_t rifid;
};

struct vrrp_key_hash
{
size_t operator() (const vrrp_key_t& key) const
{
stringstream ss;
ss << key.port_name << key.ip_addr.to_string();
return std::hash<std::string>() (ss.str());
}
};

typedef std::unordered_map<vrrp_key_t, vrrp_data_t, vrrp_key_hash> VrrpTable;

const request_description_t request_desc = {
{ REQ_T_STRING, REQ_T_IP_PREFIX },
{
{ "vmac", REQ_T_MAC_ADDRESS },
},
{ } // no mandatory attributes
};

class VrrpRequest : public Request
{
public:
VrrpRequest() : Request(request_desc, '|') { }
};

class VrrpOrch : public Orch2
{
public:
VrrpOrch(DBConnector *db, const std::string& tableName) : Orch2(db, tableName, request_) { }

private:
virtual bool addOperation(const Request& request);
virtual bool delOperation(const Request& request);
bool hasSameIpAddr(const string &alias,const IpPrefix &vip_prefix);
VrrpTable vrrp_table_;
VrrpRequest request_;
};

#endif //VRRPORCH_H
1 change: 1 addition & 0 deletions tests/mock_tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ tests_SOURCES = aclorch_ut.cpp \
$(top_srcdir)/orchagent/crmorch.cpp \
$(top_srcdir)/orchagent/request_parser.cpp \
$(top_srcdir)/orchagent/vrforch.cpp \
$(top_srcdir)/orchagent/vrrporch.cpp \
$(top_srcdir)/orchagent/countercheckorch.cpp \
$(top_srcdir)/orchagent/vxlanorch.cpp \
$(top_srcdir)/orchagent/vnetorch.cpp \
Expand Down
Loading