Skip to content

Commit 08004cc

Browse files
qiluo-msftstcheng
authored andcommitted
orchagent: Enabling IPv6 and adding swssnet.h (sonic-net#76)
1 parent 91da248 commit 08004cc

File tree

8 files changed

+131
-71
lines changed

8 files changed

+131
-71
lines changed

neighsyncd/neighsync.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ void NeighSync::onMsg(int nlmsg_type, struct nl_object *obj)
3232
if (rtnl_neigh_get_family(neigh) == AF_INET)
3333
family = IPV4_NAME;
3434
else if (rtnl_neigh_get_family(neigh) == AF_INET6)
35-
{
3635
family = IPV6_NAME;
37-
/* Currently IPv6 is not supported */
38-
return;
39-
}
4036
else
4137
return;
4238

orchagent/Makefile.am

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ bin_PROGRAMS = orchagent routeresync
77
if DEBUG
88
DBGFLAGS = -ggdb -DDEBUG
99
else
10-
DBGFLAGS = -g
10+
DBGFLAGS = -g -DNDEBUG
1111
endif
1212

1313
orchagent_SOURCES = main.cpp orchdaemon.cpp orch.cpp routeorch.cpp neighorch.cpp intfsorch.cpp portsorch.cpp copporch.cpp tunneldecaporch.cpp

orchagent/intfsorch.cpp

+12-19
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
#include "intfsorch.h"
2-
3-
#include "ipprefix.h"
4-
#include "logger.h"
5-
6-
#include "assert.h"
1+
#include <assert.h>
72
#include <fstream>
83
#include <sstream>
94
#include <map>
10-
115
#include <net/if.h>
126

7+
#include "intfsorch.h"
8+
#include "ipprefix.h"
9+
#include "logger.h"
10+
#include "swssnet.h"
11+
1312
extern sai_object_id_t gVirtualRouterId;
1413

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

8483
sai_unicast_route_entry_t unicast_route_entry;
8584
unicast_route_entry.vr_id = gVirtualRouterId;
86-
unicast_route_entry.destination.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
87-
unicast_route_entry.destination.addr.ip4 = ip_prefix.getIp().getV4Addr() & ip_prefix.getMask().getV4Addr();
88-
unicast_route_entry.destination.mask.ip4 = ip_prefix.getMask().getV4Addr();
85+
copy(unicast_route_entry.destination, ip_prefix);
86+
subnet(unicast_route_entry.destination, unicast_route_entry.destination);
8987

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

123121
unicast_route_entry.vr_id = gVirtualRouterId;
124-
unicast_route_entry.destination.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
125-
unicast_route_entry.destination.addr.ip4 = ip_prefix.getIp().getV4Addr();
126-
unicast_route_entry.destination.mask.ip4 = 0xFFFFFFFF;
122+
copy(unicast_route_entry.destination, ip_prefix.getIp());
127123

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

153149
sai_unicast_route_entry_t unicast_route_entry;
154150
unicast_route_entry.vr_id = gVirtualRouterId;
155-
unicast_route_entry.destination.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
156-
unicast_route_entry.destination.addr.ip4 = ip_prefix.getIp().getV4Addr() & ip_prefix.getMask().getV4Addr();
157-
unicast_route_entry.destination.mask.ip4 = ip_prefix.getMask().getV4Addr();
151+
copy(unicast_route_entry.destination, ip_prefix);
152+
subnet(unicast_route_entry.destination, unicast_route_entry.destination);
158153

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

167162
unicast_route_entry.vr_id = gVirtualRouterId;
168-
unicast_route_entry.destination.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
169-
unicast_route_entry.destination.addr.ip4 = ip_prefix.getIp().getV4Addr();
170-
unicast_route_entry.destination.mask.ip4 = 0xFFFFFFFF;
163+
copy(unicast_route_entry.destination, ip_prefix);
171164

172165
status = sai_route_api->remove_route(&unicast_route_entry);
173166
if (status != SAI_STATUS_SUCCESS)

orchagent/neighorch.cpp

+5-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
#include <assert.h>
12
#include "neighorch.h"
2-
33
#include "logger.h"
4-
5-
#include "assert.h"
4+
#include "swssnet.h"
65

76
extern sai_neighbor_api_t* sai_neighbor_api;
87
extern sai_next_hop_api_t* sai_next_hop_api;
@@ -25,8 +24,7 @@ bool NeighOrch::addNextHop(IpAddress ipAddress, Port port)
2524
next_hop_attrs[0].id = SAI_NEXT_HOP_ATTR_TYPE;
2625
next_hop_attrs[0].value.s32 = SAI_NEXT_HOP_IP;
2726
next_hop_attrs[1].id = SAI_NEXT_HOP_ATTR_IP;
28-
next_hop_attrs[1].value.ipaddr.addr_family= SAI_IP_ADDR_FAMILY_IPV4;
29-
next_hop_attrs[1].value.ipaddr.addr.ip4 = ipAddress.getV4Addr();
27+
copy(next_hop_attrs[1].value.ipaddr, ipAddress);
3028
next_hop_attrs[2].id = SAI_NEXT_HOP_ATTR_ROUTER_INTERFACE_ID;
3129
next_hop_attrs[2].value.oid = port.m_rif_id;
3230

@@ -119,11 +117,6 @@ void NeighOrch::doTask(Consumer &consumer)
119117
}
120118

121119
IpAddress ip_address(key.substr(found+1));
122-
if (!ip_address.isV4())
123-
{
124-
it = consumer.m_toSync.erase(it);
125-
continue;
126-
}
127120

128121
NeighborEntry neighbor_entry = { ip_address, alias };
129122

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

187180
sai_neighbor_entry_t neighbor_entry;
188181
neighbor_entry.rif_id = p.m_rif_id;
189-
neighbor_entry.ip_address.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
190-
neighbor_entry.ip_address.addr.ip4 = ip_address.getV4Addr();
182+
copy(neighbor_entry.ip_address, ip_address);
191183

192184
sai_attribute_t neighbor_attr;
193185
neighbor_attr.id = SAI_NEIGHBOR_ATTR_DST_MAC_ADDRESS;
@@ -251,8 +243,7 @@ bool NeighOrch::removeNeighbor(NeighborEntry neighborEntry)
251243

252244
sai_neighbor_entry_t neighbor_entry;
253245
neighbor_entry.rif_id = p.m_rif_id;
254-
neighbor_entry.ip_address.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
255-
neighbor_entry.ip_address.addr.ip4 = ip_address.getV4Addr();
246+
copy(neighbor_entry.ip_address, ip_address);
256247

257248
sai_object_id_t next_hop_id = m_syncdNextHops[ip_address].next_hop_id;
258249
status = sai_next_hop_api->remove_next_hop(next_hop_id);

orchagent/orch.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef SWSS_ORCH_H
22
#define SWSS_ORCH_H
33

4+
#include <map>
5+
46
extern "C" {
57
#include "sai.h"
68
#include "saistatus.h"
@@ -10,8 +12,6 @@ extern "C" {
1012
#include "consumertable.h"
1113
#include "producertable.h"
1214

13-
#include <map>
14-
1515
using namespace std;
1616
using namespace swss;
1717

orchagent/routeorch.cpp

+4-17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
#include <assert.h>
12
#include "routeorch.h"
2-
33
#include "logger.h"
4-
5-
#include "assert.h"
4+
#include "swssnet.h"
65

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

7069
IpPrefix ip_prefix = IpPrefix(key);
7170

72-
/* Currently we don't support IPv6 */
73-
if (!ip_prefix.isV4())
74-
{
75-
SWSS_LOG_WARN("Get unsupported IPv6 task ip:%s", ip_prefix.to_string().c_str());
76-
it = consumer.m_toSync.erase(it);
77-
continue;
78-
}
79-
8071
if (op == SET_COMMAND)
8172
{
8273
IpAddresses ip_addresses;
@@ -359,9 +350,7 @@ bool RouteOrch::addRoute(IpPrefix ipPrefix, IpAddresses nextHops)
359350
/* Sync the route entry */
360351
sai_unicast_route_entry_t route_entry;
361352
route_entry.vr_id = gVirtualRouterId;
362-
route_entry.destination.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
363-
route_entry.destination.addr.ip4 = ipPrefix.getIp().getV4Addr();
364-
route_entry.destination.mask.ip4 = ipPrefix.getMask().getV4Addr();
353+
copy(route_entry.destination, ipPrefix);
365354

366355
sai_attribute_t route_attr;
367356
route_attr.id = SAI_ROUTE_ATTR_NEXT_HOP_ID;
@@ -426,9 +415,7 @@ bool RouteOrch::removeRoute(IpPrefix ipPrefix)
426415

427416
sai_unicast_route_entry_t route_entry;
428417
route_entry.vr_id = gVirtualRouterId;
429-
route_entry.destination.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
430-
route_entry.destination.addr.ip4 = ipPrefix.getIp().getV4Addr();
431-
route_entry.destination.mask.ip4 = ipPrefix.getMask().getV4Addr();
418+
copy(route_entry.destination, ipPrefix);
432419

433420
sai_status_t status = sai_route_api->remove_route(&route_entry);
434421
if (status != SAI_STATUS_SUCCESS)

orchagent/swssnet.h

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Header file defining base types and simple helper functions for network in SAI
2+
// Should keep the dependency as minimal as possible
3+
//
4+
#pragma once
5+
6+
#include <memory.h>
7+
#include <stdexcept>
8+
#include <net/if.h>
9+
extern "C" {
10+
#include "sai.h"
11+
}
12+
#include "ipaddress.h"
13+
#include "ipprefix.h"
14+
15+
namespace swss {
16+
17+
inline static sai_ip_address_t& copy(sai_ip_address_t& dst, const IpAddress& src)
18+
{
19+
auto& sip = src.getIp();
20+
switch(sip.family)
21+
{
22+
case AF_INET:
23+
dst.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
24+
dst.addr.ip4 = sip.ip_addr.ipv4_addr;
25+
break;
26+
case AF_INET6:
27+
dst.addr_family = SAI_IP_ADDR_FAMILY_IPV6;
28+
memcpy(dst.addr.ip6, sip.ip_addr.ipv6_addr, 16);
29+
break;
30+
default:
31+
throw std::logic_error("Invalid family");
32+
}
33+
return dst;
34+
}
35+
36+
inline static sai_ip_prefix_t& copy(sai_ip_prefix_t& dst, const IpPrefix& src)
37+
{
38+
auto& ia = src.getIp().getIp();
39+
auto& ma = src.getMask().getIp();
40+
switch(ia.family)
41+
{
42+
case AF_INET:
43+
dst.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
44+
dst.addr.ip4 = ia.ip_addr.ipv4_addr;
45+
dst.mask.ip4 = ma.ip_addr.ipv4_addr;
46+
break;
47+
case AF_INET6:
48+
dst.addr_family = SAI_IP_ADDR_FAMILY_IPV6;
49+
memcpy(dst.addr.ip6, ia.ip_addr.ipv6_addr, 16);
50+
memcpy(dst.mask.ip6, ma.ip_addr.ipv6_addr, 16);
51+
break;
52+
default:
53+
throw std::logic_error("Invalid family");
54+
}
55+
return dst;
56+
}
57+
58+
inline static sai_ip_prefix_t& copy(sai_ip_prefix_t& dst, const IpAddress& src)
59+
{
60+
auto& sip = src.getIp();
61+
switch(sip.family)
62+
{
63+
case AF_INET:
64+
dst.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
65+
dst.addr.ip4 = sip.ip_addr.ipv4_addr;
66+
dst.mask.ip4 = 0xFFFFFFFF;
67+
break;
68+
case AF_INET6:
69+
dst.addr_family = SAI_IP_ADDR_FAMILY_IPV6;
70+
memcpy(dst.addr.ip6, sip.ip_addr.ipv6_addr, 16);
71+
memset(dst.mask.ip6, 0xFF, 16);
72+
break;
73+
default:
74+
throw std::logic_error("Invalid family");
75+
}
76+
return dst;
77+
}
78+
79+
inline static sai_ip_prefix_t& subnet(sai_ip_prefix_t& dst, const sai_ip_prefix_t& src)
80+
{
81+
dst.addr_family = src.addr_family;
82+
switch(src.addr_family)
83+
{
84+
case SAI_IP_ADDR_FAMILY_IPV4:
85+
dst.addr.ip4 = src.addr.ip4 & src.mask.ip4;
86+
dst.mask.ip4 = 0xFFFFFFFF;
87+
break;
88+
case SAI_IP_ADDR_FAMILY_IPV6:
89+
for (size_t i = 0; i < 16; i++)
90+
{
91+
dst.addr.ip6[i] = src.addr.ip6[i] & src.mask.ip6[i];
92+
dst.mask.ip6[i] = 0xFF;
93+
}
94+
break;
95+
default:
96+
throw std::logic_error("Invalid family");
97+
}
98+
return dst;
99+
}
100+
101+
}

orchagent/tunneldecaporch.cpp

+6-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
#include "tunneldecaporch.h"
21
#include <string.h>
2+
#include "tunneldecaporch.h"
33
#include "logger.h"
4+
#include "swssnet.h"
45

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

292-
sai_ip_address_t tunnel_dst_ip;
293-
tunnel_dst_ip.addr_family = SAI_IP_ADDR_FAMILY_IPV4;
294-
295-
set<IpAddress> tunnel_ips = dst_ip.getIpAddresses();
296-
struct sockaddr_in tunnel_ip_struct;
297-
string ip;
298-
299293
TunnelEntry *tunnel_info = &tunnelTable.find(tunnelKey)->second;
300294

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

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

312308
else
313309
{
314-
// turn string ip into network byte order
315-
inet_pton(AF_INET, ip.c_str(), &(tunnel_ip_struct.sin_addr));
316-
tunnel_dst_ip.addr.ip4 = tunnel_ip_struct.sin_addr.s_addr;
317-
318310
attr.id = SAI_TUNNEL_TERM_TABLE_ENTRY_ATTR_DST_IP;
319-
attr.value.ipaddr = tunnel_dst_ip;
311+
copy(attr.value.ipaddr, ia);
320312
tunnel_table_entry_attrs.push_back(attr);
321313

322314
// create the tunnel table entry

0 commit comments

Comments
 (0)