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

Zitingguo/add lag test #1522

Merged
Merged
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
162 changes: 162 additions & 0 deletions test/sai_test/config/lag_configer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# Copyright (c) 2021 Microsoft Open Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
# LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS
# FOR A PARTICULAR PURPOSE, MERCHANTABILITY OR NON-INFRINGEMENT.
#
# See the Apache Version 2.0 License for specific language governing
# permissions and limitations under the License.
#
# Microsoft would like to thank the following companies for their review and
# assistance with these files: Intel Corporation, Mellanox Technologies Ltd,
# Dell Products, L.P., Facebook, Inc., Marvell International Ltd.
#
#


from sai_thrift.sai_adapter import *
from sai_utils import * # pylint: disable=wildcard-import; lgtm[py/polluting-import]


def t0_lag_config_helper(test_obj, is_create_lag=True):
"""
Make lag configurations base on the configuration in the test plan.
set the configuration in test directly.

set the following test_obj attributes:
lag object

"""
lag_configer = LagConfiger(test_obj)

if is_create_lag:
test_obj.lag1 = lag_configer.create_lag([17, 18])
test_obj.lag2 = lag_configer.create_lag([19, 20])

"""
lag_configer.set_lag_hash_algorithm()
lag_configer.setup_lag_v4_hash()
lag_configer.set_lag_hash_seed()
"""


class LagConfiger(object):
"""
Class use to make all the Lag configurations.
"""

def __init__(self, test_obj) -> None:
"""
Init Lag configrer.

Args:
test_obj: the test object
"""
self.test_obj = test_obj
self.client = test_obj.client

def create_lag(self, lag_port_idxs):
"""
Create lag and its members.

Args:
lag_port_idxs: lag port indexs

Returns:
Lag: lag object
"""

lag = Lag()
lag_id = sai_thrift_create_lag(self.client)
lag_members = self.create_lag_member(lag_id, lag_port_idxs)
self.test_obj.assertEqual(self.test_obj.status(), SAI_STATUS_SUCCESS)
lag.lag_id = lag_id
lag.lag_members = lag_members
return lag

def create_lag_member(self, lag_id, lag_port_idxs):
"""
Create lag members for a lag.

Args:
lag: lag object
lag_port_idxs: lag member port indexs

Returns:
lag_members: list of lag_member
"""

lag_members = []
for port_index in lag_port_idxs:
lag_member = sai_thrift_create_lag_member(self.client,
lag_id=lag_id,
port_id=self.test_obj.port_list[port_index])
self.test_obj.assertEqual(self.test_obj.status(), SAI_STATUS_SUCCESS)
lag_members.append(lag_member)
return lag_members

def set_lag_hash_algorithm(self, algo=SAI_HASH_ALGORITHM_CRC):
"""
Set lag hash algorithm.

Args:
algo (int): hash algorithm id
"""
sai_thrift_set_switch_attribute(self.client, lag_default_hash_algorithm=algo)

def setup_lag_v4_hash(self, hash_fields_list=None, lag_hash_ipv4=None):
if hash_fields_list is None:
hash_fields_list = [SAI_NATIVE_HASH_FIELD_SRC_IP,
SAI_NATIVE_HASH_FIELD_DST_IP,
SAI_NATIVE_HASH_FIELD_IP_PROTOCOL,
SAI_NATIVE_HASH_FIELD_L4_DST_PORT,
SAI_NATIVE_HASH_FIELD_L4_SRC_PORT]

if lag_hash_ipv4 is None:
# create new hash
s32list = sai_thrift_s32_list_t(count=len(hash_fields_list), int32list=hash_fields_list)
lag_hash_ipv4 = sai_thrift_create_hash(self.client, native_hash_field_list=s32list)
self.test_obj.assertTrue(lag_hash_ipv4 != 0, "Failed to create IPv4 lag hash")
status = sai_thrift_set_switch_attribute(self.client, lag_hash_ipv4=lag_hash_ipv4)
self.test_obj.assertEqual(status, SAI_STATUS_SUCCESS)
else:
# update existing hash
s32list = sai_thrift_s32_list_t(count=len(hash_fields_list), int32list=hash_fields_list)
status = sai_thrift_set_hash_attribute(self.client, lag_hash_ipv4, native_hash_field_list=s32list)
self.test_obj.assertEqual(status, SAI_STATUS_SUCCESS)

def set_lag_hash_seed(self, seed=400):
"""
Set lag hash seed.

Args:
seed: hash seed value
"""
status = sai_thrift_set_switch_attribute(self.client, lag_default_hash_seed=seed)
self.test_obj.assertEqual(status, SAI_STATUS_SUCCESS)

def remove_lag_member(self, lag_member):
sai_thrift_remove_lag_member(self, lag_member)

def remove_all_lag_members(self, lag_members):
for lag_member in lag_members:
sai_thrift_remove_lag_member(self.client, lag_member)

def remove_lag(self, lag_id):
sai_thrift_remove_lag(self.client, lag_id)

class Lag(object):
"""
Represent the lag object.
Attrs:
lag_id: lag id
lag_members: lag members
"""
def __init__(self, lag_id=None, lag_members=None):
self.lag_id = lag_id
self.lag_members = lag_members
138 changes: 138 additions & 0 deletions test/sai_test/config/route_configer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# Copyright (c) 2021 Microsoft Open Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
# LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS
# FOR A PARTICULAR PURPOSE, MERCHANTABILITY OR NON-INFRINGEMENT.
#
# See the Apache Version 2.0 License for specific language governing
# permissions and limitations under the License.
#
# Microsoft would like to thank the following companies for their review and
# assistance with these files: Intel Corporation, Mellanox Technologies Ltd,
# Dell Products, L.P., Facebook, Inc., Marvell International Ltd.
#
#


from sai_thrift.sai_adapter import *
from sai_utils import * # pylint: disable=wildcard-import; lgtm[py/polluting-import]

def t0_route_config_helper(test_obj, is_create_route=True, is_create_route_for_lag=True):
route_configer = RouteConfiger(test_obj)

if is_create_route:
route_configer.create_default_route()

if is_create_route_for_lag:
ip_addr1 = '10.10.10.0'
mac_addr1 = '02:04:02:01:01:01'
route_configer.create_route_and_neighbor_entry_for_port(ip_addr=ip_addr1,
mac_addr=mac_addr1,
port_id=test_obj.lag1.lag_id,
virtual_router_id=test_obj.default_vrf)

ip_addr2 = '10.1.2.100'
mac_addr2 = '02:04:02:01:02:01'
route_configer.create_route_and_neighbor_entry_for_port(ip_addr=ip_addr1,
mac_addr=mac_addr2,
port_id=test_obj.lag2.lag_id,
virtual_router_id=test_obj.default_vrf)


class RouteConfiger(object):
"""
Class use to make all the route configurations.
"""

def __init__(self, test_obj) -> None:
"""
Init Route configer.

Args:
test_obj: the test object
"""
self.test_obj = test_obj
self.client = test_obj.client

def create_default_route(self):
self.create_default_route_intf()
self.create_default_v4_v6_route_entry()
self.create_local_v6_route()

def create_default_route_intf(self):
"""
Create default route interface on loop back interface.
"""
print("Create loop back interface...")
attr = sai_thrift_get_switch_attribute(self.client, default_virtual_router_id=True)
self.test_obj.assertNotEqual(attr['default_virtual_router_id'], 0)
self.test_obj.default_vrf = attr['default_virtual_router_id']

self.test_obj.loopback_intf = sai_thrift_create_router_interface(self.client,
type=SAI_ROUTER_INTERFACE_TYPE_LOOPBACK, virtual_router_id=self.test_obj.default_vrf)
self.test_obj.assertEqual(self.test_obj.status(), SAI_STATUS_SUCCESS)

def create_default_v4_v6_route_entry(self):
"""
Create default v4 and v6 route entry.
"""

print("Create default v4&v6 route entry...")
v6_default = sai_thrift_ip_prefix_t(addr_family=1,
addr=sai_thrift_ip_addr_t(ip6=DEFAULT_IP_V6_PREFIX),
mask=sai_thrift_ip_addr_t(ip6=DEFAULT_IP_V6_PREFIX))
entry = sai_thrift_route_entry_t(vr_id=self.test_obj.default_vrf,
destination=v6_default)
self.test_obj.default_ipv6_route_entry = sai_thrift_create_route_entry(
self.client,
route_entry=entry,
packet_action=SAI_PACKET_ACTION_DROP)
self.test_obj.assertEqual(self.test_obj.status(), SAI_STATUS_SUCCESS)

entry = sai_thrift_route_entry_t(vr_id=self.test_obj.default_vrf,
destination=sai_ipprefix(DEFAULT_IP_V4_PREFIX))
self.test_obj.default_ipv4_route_entry = sai_thrift_create_route_entry(
self.client,
route_entry=entry,
packet_action=SAI_PACKET_ACTION_DROP)
self.test_obj.assertEqual(self.test_obj.status(), SAI_STATUS_SUCCESS)

def create_local_v6_route(self):
"""
Create local v6 route base on the configuration of the actual switch.
"""

print("Create local v6 route...")
entry = sai_thrift_route_entry_t(vr_id=self.test_obj.default_vrf,
destination=sai_ipprefix(LOCAL_IP_10V6_PREFIX))
self.test_obj.local_10v6_route_entry = sai_thrift_create_route_entry(
self.client,
route_entry=entry,
packet_action=SAI_PACKET_ACTION_FORWARD)
self.test_obj.assertEqual(self.test_obj.status(), SAI_STATUS_SUCCESS)

entry = sai_thrift_route_entry_t(vr_id=self.test_obj.default_vrf,
destination=sai_ipprefix(LOCAL_IP_128V6_PREFIX))
self.test_obj.local_128v6_route_entry = sai_thrift_create_route_entry(
self.client,
route_entry=entry,
packet_action=SAI_PACKET_ACTION_FORWARD)
self.test_obj.assertEqual(self.test_obj.status(), SAI_STATUS_SUCCESS)

def create_route_and_neighbor_entry_for_port(self, ip_addr, mac_addr, port_id, virtual_router_id=None):
if virtual_router_id is None:
virtual_router_id = self.test_obj.default_vrf

rif_id1 = sai_thrift_create_router_interface(self.client, virtual_router_id=virtual_router_id, type=SAI_ROUTER_INTERFACE_TYPE_PORT, port_id=port_id)

nbr_entry_v4 = sai_thrift_neighbor_entry_t(rif_id=rif_id1, ip_address=sai_ipaddress(ip_addr))
sai_thrift_create_neighbor_entry(self.client, nbr_entry_v4, dst_mac_address=mac_addr)

nhop = sai_thrift_create_next_hop(self.client, ip=sai_ipaddress(ip_addr), router_interface_id=rif_id1, type=SAI_NEXT_HOP_TYPE_IP)
route1 =sai_thrift_route_entry_t(vr_id=virtual_router_id, destination=sai_ipprefix(ip_addr+'/24'))
sai_thrift_create_route_entry(self.client, route1, next_hop_id=nhop)
Loading