Skip to content

Commit

Permalink
orchagent: Enabling IPv6 and adding swssnet.h (sonic-net#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
qiluo-msft authored and stcheng committed Sep 9, 2016
1 parent 91da248 commit 08004cc
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 71 deletions.
4 changes: 0 additions & 4 deletions neighsyncd/neighsync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ void NeighSync::onMsg(int nlmsg_type, struct nl_object *obj)
if (rtnl_neigh_get_family(neigh) == AF_INET)
family = IPV4_NAME;
else if (rtnl_neigh_get_family(neigh) == AF_INET6)
{
family = IPV6_NAME;
/* Currently IPv6 is not supported */
return;
}
else
return;

Expand Down
2 changes: 1 addition & 1 deletion orchagent/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ bin_PROGRAMS = orchagent routeresync
if DEBUG
DBGFLAGS = -ggdb -DDEBUG
else
DBGFLAGS = -g
DBGFLAGS = -g -DNDEBUG
endif

orchagent_SOURCES = main.cpp orchdaemon.cpp orch.cpp routeorch.cpp neighorch.cpp intfsorch.cpp portsorch.cpp copporch.cpp tunneldecaporch.cpp
Expand Down
31 changes: 12 additions & 19 deletions orchagent/intfsorch.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#include "intfsorch.h"

#include "ipprefix.h"
#include "logger.h"

#include "assert.h"
#include <assert.h>
#include <fstream>
#include <sstream>
#include <map>

#include <net/if.h>

#include "intfsorch.h"
#include "ipprefix.h"
#include "logger.h"
#include "swssnet.h"

extern sai_object_id_t gVirtualRouterId;

extern sai_router_interface_api_t* sai_router_intfs_api;
Expand Down Expand Up @@ -83,9 +82,8 @@ void IntfsOrch::doTask(Consumer &consumer)

sai_unicast_route_entry_t unicast_route_entry;
unicast_route_entry.vr_id = gVirtualRouterId;
unicast_route_entry.destination.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
unicast_route_entry.destination.addr.ip4 = ip_prefix.getIp().getV4Addr() & ip_prefix.getMask().getV4Addr();
unicast_route_entry.destination.mask.ip4 = ip_prefix.getMask().getV4Addr();
copy(unicast_route_entry.destination, ip_prefix);
subnet(unicast_route_entry.destination, unicast_route_entry.destination);

sai_attribute_t attr;
vector<sai_attribute_t> attrs;
Expand Down Expand Up @@ -121,9 +119,7 @@ void IntfsOrch::doTask(Consumer &consumer)
ip2me_attrs.push_back(ip2me_attr);

unicast_route_entry.vr_id = gVirtualRouterId;
unicast_route_entry.destination.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
unicast_route_entry.destination.addr.ip4 = ip_prefix.getIp().getV4Addr();
unicast_route_entry.destination.mask.ip4 = 0xFFFFFFFF;
copy(unicast_route_entry.destination, ip_prefix.getIp());

status = sai_route_api->create_route(&unicast_route_entry, ip2me_attrs.size(), ip2me_attrs.data());
if (status != SAI_STATUS_SUCCESS)
Expand Down Expand Up @@ -152,9 +148,8 @@ void IntfsOrch::doTask(Consumer &consumer)

sai_unicast_route_entry_t unicast_route_entry;
unicast_route_entry.vr_id = gVirtualRouterId;
unicast_route_entry.destination.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
unicast_route_entry.destination.addr.ip4 = ip_prefix.getIp().getV4Addr() & ip_prefix.getMask().getV4Addr();
unicast_route_entry.destination.mask.ip4 = ip_prefix.getMask().getV4Addr();
copy(unicast_route_entry.destination, ip_prefix);
subnet(unicast_route_entry.destination, unicast_route_entry.destination);

sai_status_t status = sai_route_api->remove_route(&unicast_route_entry);
if (status != SAI_STATUS_SUCCESS)
Expand All @@ -165,9 +160,7 @@ void IntfsOrch::doTask(Consumer &consumer)
}

unicast_route_entry.vr_id = gVirtualRouterId;
unicast_route_entry.destination.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
unicast_route_entry.destination.addr.ip4 = ip_prefix.getIp().getV4Addr();
unicast_route_entry.destination.mask.ip4 = 0xFFFFFFFF;
copy(unicast_route_entry.destination, ip_prefix);

status = sai_route_api->remove_route(&unicast_route_entry);
if (status != SAI_STATUS_SUCCESS)
Expand Down
19 changes: 5 additions & 14 deletions orchagent/neighorch.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include <assert.h>
#include "neighorch.h"

#include "logger.h"

#include "assert.h"
#include "swssnet.h"

extern sai_neighbor_api_t* sai_neighbor_api;
extern sai_next_hop_api_t* sai_next_hop_api;
Expand All @@ -25,8 +24,7 @@ bool NeighOrch::addNextHop(IpAddress ipAddress, Port port)
next_hop_attrs[0].id = SAI_NEXT_HOP_ATTR_TYPE;
next_hop_attrs[0].value.s32 = SAI_NEXT_HOP_IP;
next_hop_attrs[1].id = SAI_NEXT_HOP_ATTR_IP;
next_hop_attrs[1].value.ipaddr.addr_family= SAI_IP_ADDR_FAMILY_IPV4;
next_hop_attrs[1].value.ipaddr.addr.ip4 = ipAddress.getV4Addr();
copy(next_hop_attrs[1].value.ipaddr, ipAddress);
next_hop_attrs[2].id = SAI_NEXT_HOP_ATTR_ROUTER_INTERFACE_ID;
next_hop_attrs[2].value.oid = port.m_rif_id;

Expand Down Expand Up @@ -119,11 +117,6 @@ void NeighOrch::doTask(Consumer &consumer)
}

IpAddress ip_address(key.substr(found+1));
if (!ip_address.isV4())
{
it = consumer.m_toSync.erase(it);
continue;
}

NeighborEntry neighbor_entry = { ip_address, alias };

Expand Down Expand Up @@ -186,8 +179,7 @@ bool NeighOrch::addNeighbor(NeighborEntry neighborEntry, MacAddress macAddress)

sai_neighbor_entry_t neighbor_entry;
neighbor_entry.rif_id = p.m_rif_id;
neighbor_entry.ip_address.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
neighbor_entry.ip_address.addr.ip4 = ip_address.getV4Addr();
copy(neighbor_entry.ip_address, ip_address);

sai_attribute_t neighbor_attr;
neighbor_attr.id = SAI_NEIGHBOR_ATTR_DST_MAC_ADDRESS;
Expand Down Expand Up @@ -251,8 +243,7 @@ bool NeighOrch::removeNeighbor(NeighborEntry neighborEntry)

sai_neighbor_entry_t neighbor_entry;
neighbor_entry.rif_id = p.m_rif_id;
neighbor_entry.ip_address.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
neighbor_entry.ip_address.addr.ip4 = ip_address.getV4Addr();
copy(neighbor_entry.ip_address, ip_address);

sai_object_id_t next_hop_id = m_syncdNextHops[ip_address].next_hop_id;
status = sai_next_hop_api->remove_next_hop(next_hop_id);
Expand Down
4 changes: 2 additions & 2 deletions orchagent/orch.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef SWSS_ORCH_H
#define SWSS_ORCH_H

#include <map>

extern "C" {
#include "sai.h"
#include "saistatus.h"
Expand All @@ -10,8 +12,6 @@ extern "C" {
#include "consumertable.h"
#include "producertable.h"

#include <map>

using namespace std;
using namespace swss;

Expand Down
21 changes: 4 additions & 17 deletions orchagent/routeorch.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include <assert.h>
#include "routeorch.h"

#include "logger.h"

#include "assert.h"
#include "swssnet.h"

extern sai_next_hop_group_api_t* sai_next_hop_group_api;
extern sai_route_api_t* sai_route_api;
Expand Down Expand Up @@ -69,14 +68,6 @@ void RouteOrch::doTask(Consumer& consumer)

IpPrefix ip_prefix = IpPrefix(key);

/* Currently we don't support IPv6 */
if (!ip_prefix.isV4())
{
SWSS_LOG_WARN("Get unsupported IPv6 task ip:%s", ip_prefix.to_string().c_str());
it = consumer.m_toSync.erase(it);
continue;
}

if (op == SET_COMMAND)
{
IpAddresses ip_addresses;
Expand Down Expand Up @@ -359,9 +350,7 @@ bool RouteOrch::addRoute(IpPrefix ipPrefix, IpAddresses nextHops)
/* Sync the route entry */
sai_unicast_route_entry_t route_entry;
route_entry.vr_id = gVirtualRouterId;
route_entry.destination.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
route_entry.destination.addr.ip4 = ipPrefix.getIp().getV4Addr();
route_entry.destination.mask.ip4 = ipPrefix.getMask().getV4Addr();
copy(route_entry.destination, ipPrefix);

sai_attribute_t route_attr;
route_attr.id = SAI_ROUTE_ATTR_NEXT_HOP_ID;
Expand Down Expand Up @@ -426,9 +415,7 @@ bool RouteOrch::removeRoute(IpPrefix ipPrefix)

sai_unicast_route_entry_t route_entry;
route_entry.vr_id = gVirtualRouterId;
route_entry.destination.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
route_entry.destination.addr.ip4 = ipPrefix.getIp().getV4Addr();
route_entry.destination.mask.ip4 = ipPrefix.getMask().getV4Addr();
copy(route_entry.destination, ipPrefix);

sai_status_t status = sai_route_api->remove_route(&route_entry);
if (status != SAI_STATUS_SUCCESS)
Expand Down
101 changes: 101 additions & 0 deletions orchagent/swssnet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Header file defining base types and simple helper functions for network in SAI
// Should keep the dependency as minimal as possible
//
#pragma once

#include <memory.h>
#include <stdexcept>
#include <net/if.h>
extern "C" {
#include "sai.h"
}
#include "ipaddress.h"
#include "ipprefix.h"

namespace swss {

inline static sai_ip_address_t& copy(sai_ip_address_t& dst, const IpAddress& src)
{
auto& sip = src.getIp();
switch(sip.family)
{
case AF_INET:
dst.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
dst.addr.ip4 = sip.ip_addr.ipv4_addr;
break;
case AF_INET6:
dst.addr_family = SAI_IP_ADDR_FAMILY_IPV6;
memcpy(dst.addr.ip6, sip.ip_addr.ipv6_addr, 16);
break;
default:
throw std::logic_error("Invalid family");
}
return dst;
}

inline static sai_ip_prefix_t& copy(sai_ip_prefix_t& dst, const IpPrefix& src)
{
auto& ia = src.getIp().getIp();
auto& ma = src.getMask().getIp();
switch(ia.family)
{
case AF_INET:
dst.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
dst.addr.ip4 = ia.ip_addr.ipv4_addr;
dst.mask.ip4 = ma.ip_addr.ipv4_addr;
break;
case AF_INET6:
dst.addr_family = SAI_IP_ADDR_FAMILY_IPV6;
memcpy(dst.addr.ip6, ia.ip_addr.ipv6_addr, 16);
memcpy(dst.mask.ip6, ma.ip_addr.ipv6_addr, 16);
break;
default:
throw std::logic_error("Invalid family");
}
return dst;
}

inline static sai_ip_prefix_t& copy(sai_ip_prefix_t& dst, const IpAddress& src)
{
auto& sip = src.getIp();
switch(sip.family)
{
case AF_INET:
dst.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
dst.addr.ip4 = sip.ip_addr.ipv4_addr;
dst.mask.ip4 = 0xFFFFFFFF;
break;
case AF_INET6:
dst.addr_family = SAI_IP_ADDR_FAMILY_IPV6;
memcpy(dst.addr.ip6, sip.ip_addr.ipv6_addr, 16);
memset(dst.mask.ip6, 0xFF, 16);
break;
default:
throw std::logic_error("Invalid family");
}
return dst;
}

inline static sai_ip_prefix_t& subnet(sai_ip_prefix_t& dst, const sai_ip_prefix_t& src)
{
dst.addr_family = src.addr_family;
switch(src.addr_family)
{
case SAI_IP_ADDR_FAMILY_IPV4:
dst.addr.ip4 = src.addr.ip4 & src.mask.ip4;
dst.mask.ip4 = 0xFFFFFFFF;
break;
case SAI_IP_ADDR_FAMILY_IPV6:
for (size_t i = 0; i < 16; i++)
{
dst.addr.ip6[i] = src.addr.ip6[i] & src.mask.ip6[i];
dst.mask.ip6[i] = 0xFF;
}
break;
default:
throw std::logic_error("Invalid family");
}
return dst;
}

}
20 changes: 6 additions & 14 deletions orchagent/tunneldecaporch.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "tunneldecaporch.h"
#include <string.h>
#include "tunneldecaporch.h"
#include "logger.h"
#include "swssnet.h"

extern sai_tunnel_api_t* sai_tunnel_api;
extern sai_router_interface_api_t* sai_router_intfs_api;
Expand Down Expand Up @@ -289,19 +290,14 @@ bool TunnelDecapOrch::addDecapTunnelTermEntries(string tunnelKey, IpAddresses ds
attr.value.oid = tunnel_id;
tunnel_table_entry_attrs.push_back(attr);

sai_ip_address_t tunnel_dst_ip;
tunnel_dst_ip.addr_family = SAI_IP_ADDR_FAMILY_IPV4;

set<IpAddress> tunnel_ips = dst_ip.getIpAddresses();
struct sockaddr_in tunnel_ip_struct;
string ip;

TunnelEntry *tunnel_info = &tunnelTable.find(tunnelKey)->second;

// loop through the IP list and create a new tunnel table entry for every IP (in network byte order)
set<IpAddress> tunnel_ips = dst_ip.getIpAddresses();
for (auto it = tunnel_ips.begin(); it != tunnel_ips.end(); ++it)
{
ip = it->to_string();
const IpAddress& ia = *it;
string ip = ia.to_string();

// check if the there's an entry already for the ip
if (existingIps.find(ip) != existingIps.end())
Expand All @@ -311,12 +307,8 @@ bool TunnelDecapOrch::addDecapTunnelTermEntries(string tunnelKey, IpAddresses ds

else
{
// turn string ip into network byte order
inet_pton(AF_INET, ip.c_str(), &(tunnel_ip_struct.sin_addr));
tunnel_dst_ip.addr.ip4 = tunnel_ip_struct.sin_addr.s_addr;

attr.id = SAI_TUNNEL_TERM_TABLE_ENTRY_ATTR_DST_IP;
attr.value.ipaddr = tunnel_dst_ip;
copy(attr.value.ipaddr, ia);
tunnel_table_entry_attrs.push_back(attr);

// create the tunnel table entry
Expand Down

0 comments on commit 08004cc

Please sign in to comment.