-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsx_ecmp_hash.py
executable file
·130 lines (115 loc) · 4.89 KB
/
sx_ecmp_hash.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/python
'''
This script is setting custom ECMP Hash fields for Mellanox Switches.
First it tries to read and apply ECMP Hash configuration from file
(/etc/sx_ecmp_hash.conf).
Otherwise it applies the following default config:
SX_ROUTER_ECMP_HASH_SRC_IP
SX_ROUTER_ECMP_HASH_DST_IP
SX_ROUTER_ECMP_HASH_TCP_UDP
SX_ROUTER_ECMP_HASH_TCP_UDP_SRC_PORT
SX_ROUTER_ECMP_HASH_TCP_UDP_DST_PORT
SX_ROUTER_ECMP_HASH_SMAC
SX_ROUTER_ECMP_HASH_DMAC
SX_ROUTER_ECMP_HASH_ETH_TYPE
SX_ROUTER_ECMP_HASH_VID
'''
import sys, errno
sys.path.append('/lib/python2.7/dist-packages/python_sdk_api/')
sys.path.append('/lib/python2.7/site-packages/python_sdk_api/')
sys.path.append('/usr/lib/python2.7/dist-packages/python_sdk_api/')
sys.path.append('/usr/lib/python2.7/site-packages/python_sdk_api/')
sys.path.append('/usr/local/lib/python2.7/dist-packages/python_sdk_api/')
sys.path.append('/usr/local/lib/python2.7/site-packages/python_sdk_api/')
from sx_api import *
ECMP_HASH_CFG = "/etc/sx_ecmp_hash.conf"
DEFAULT_ECMP_HASH = SX_ROUTER_ECMP_HASH_SRC_IP | \
SX_ROUTER_ECMP_HASH_DST_IP | \
SX_ROUTER_ECMP_HASH_TCP_UDP | \
SX_ROUTER_ECMP_HASH_TCP_UDP_SRC_PORT | \
SX_ROUTER_ECMP_HASH_TCP_UDP_DST_PORT | \
SX_ROUTER_ECMP_HASH_SMAC | \
SX_ROUTER_ECMP_HASH_DMAC | \
SX_ROUTER_ECMP_HASH_ETH_TYPE | \
SX_ROUTER_ECMP_HASH_VID
ECMP_HASH_BITS = (
("SX_ROUTER_ECMP_HASH_SRC_IP", SX_ROUTER_ECMP_HASH_SRC_IP ),
("SX_ROUTER_ECMP_HASH_DST_IP", SX_ROUTER_ECMP_HASH_DST_IP),
("SX_ROUTER_ECMP_HASH_TCLASS", SX_ROUTER_ECMP_HASH_TCLASS),
("SX_ROUTER_ECMP_HASH_FLOW_LABEL", SX_ROUTER_ECMP_HASH_FLOW_LABEL),
("SX_ROUTER_ECMP_HASH_TCP_UDP", SX_ROUTER_ECMP_HASH_TCP_UDP),
("SX_ROUTER_ECMP_HASH_TCP_UDP_SRC_PORT", SX_ROUTER_ECMP_HASH_TCP_UDP_SRC_PORT),
("SX_ROUTER_ECMP_HASH_TCP_UDP_DST_PORT", SX_ROUTER_ECMP_HASH_TCP_UDP_DST_PORT),
("SX_ROUTER_ECMP_HASH_SMAC", SX_ROUTER_ECMP_HASH_SMAC),
("SX_ROUTER_ECMP_HASH_DMAC", SX_ROUTER_ECMP_HASH_DMAC),
("SX_ROUTER_ECMP_HASH_ETH_TYPE", SX_ROUTER_ECMP_HASH_ETH_TYPE),
("SX_ROUTER_ECMP_HASH_VID", SX_ROUTER_ECMP_HASH_VID),
("SX_ROUTER_ECMP_HASH_PCP", SX_ROUTER_ECMP_HASH_PCP),
("SX_ROUTER_ECMP_HASH_DEI", SX_ROUTER_ECMP_HASH_DEI)
)
def print_ecmp_hash_fields(ecmp_hash):
for bit in ECMP_HASH_BITS:
print(" %-40s %s" % (bit[0], (ecmp_hash & bit[1]) != 0))
def dump_ecmp_hash_params(handle):
router_ecmp_hash_params = sx_router_ecmp_hash_params_t()
rc = sx_api_router_ecmp_hash_params_get(handle, router_ecmp_hash_params)
if rc != 0:
print("[-] Error getting ECMP hash params: %d" % rc)
return rc
print("ECMP Hash params: %s" % router_ecmp_hash_params.ecmp_hash)
print(" %-20s %s" % ("ECMP Hash Type:", router_ecmp_hash_params.ecmp_hash_type))
print(" %-20s %d" % ("Symmetric Hash:", router_ecmp_hash_params.symmetric_hash))
print(" %-20s %d" % ("Seed:", router_ecmp_hash_params.seed))
print(" %-20s %d" % ("ECMP Hash:", router_ecmp_hash_params.ecmp_hash))
print("ECMP Hash fields:")
print_ecmp_hash_fields(router_ecmp_hash_params.ecmp_hash)
return rc
def set_ecmp_hash(handle, ecmp_hash):
router_ecmp_hash_params = sx_router_ecmp_hash_params_t()
rc = sx_api_router_ecmp_hash_params_get(handle, router_ecmp_hash_params)
if rc != 0:
print("[-] Error getting ECMP hash params: %d" % rc)
return rc
router_ecmp_hash_params.ecmp_hash = new_ecmp_hash
rc = sx_api_router_ecmp_hash_params_set(handle, router_ecmp_hash_params)
if rc != 0:
print("[-] Error setting ECMP hash params: %d" % rc)
return rc
def read_ecmp_hash_cfg(filename):
ecmp_hash = 0
hash_bits = dict(ECMP_HASH_BITS)
try:
with open(filename) as fh:
for line in fh:
line = line.strip()
if line.startswith("#"):
continue
bit = hash_bits.get(line)
if bit:
ecmp_hash |= bit
except IOError:
return None
return ecmp_hash
print("[+] opening sdk")
rc, handle = sx_api_open(None)
if rc != 0:
print("[-] Error opening SDK API: %d" % rc)
sys.exit(rc)
print("[+] Getting ECMP hash params")
rc = dump_ecmp_hash_params(handle)
if rc != 0:
exit(rc)
# configure new ECMP hash values
print("[+] Reading ECMP hash configuration from %s" % ECMP_HASH_CFG)
new_ecmp_hash = read_ecmp_hash_cfg(ECMP_HASH_CFG)
if new_ecmp_hash is None:
print("[-] Can't read ECMP hash configuration. Using default")
new_ecmp_hash = DEFAULT_ECMP_HASH
print("[+] Setting new ECMP hash values: %d" % new_ecmp_hash)
rc = set_ecmp_hash(handle, new_ecmp_hash)
if rc != 0:
exit(rc)
print("[+] Getting updated ECMP hash params")
dump_ecmp_hash_params(handle)
print("[+] closing sdk")
sx_api_close(handle)