-
Notifications
You must be signed in to change notification settings - Fork 744
/
Copy pathremote.py
90 lines (71 loc) · 2.41 KB
/
remote.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
"""
Remote platform
This platform uses physical ethernet interfaces.
"""
import os
import yaml
ETH_PFX = 'eth'
BACKPLANE = 'backplane'
SUB_INTF_SEP = '.'
def get_ifaces():
with open('/proc/net/dev') as fp:
all = fp.read()
ifaces = []
for line in all.split('\n'):
# Skip a header
if ':' not in line:
continue
iface = line.split(':')[0].strip()
# Skip not FP interfaces and vlan interface, like eth1.20
if ETH_PFX not in iface and BACKPLANE != iface:
continue
ifaces.append(iface)
# Sort before return
return ifaces
def build_ifaces_map(ifaces):
"""Build interface map for ptf to init dataplane."""
ptf_port_mapping_mode = "use_orig_interface"
constants_file = os.path.join(os.path.dirname(__file__), "constants.yaml")
if os.path.exists(constants_file):
with open(constants_file) as fd:
constants = yaml.safe_load(fd)
ptf_port_mapping_mode = constants.get(
"PTF_PORT_MAPPING_MODE", ptf_port_mapping_mode)
sub_ifaces = []
iface_map = {}
used_index = set()
backplane_exist = False
for iface in ifaces:
iface_suffix = iface.lstrip(ETH_PFX)
if SUB_INTF_SEP in iface_suffix:
iface_index = int(iface_suffix.split(SUB_INTF_SEP)[0])
sub_ifaces.append((iface_index, iface))
elif iface == BACKPLANE:
backplane_exist = True
else:
iface_index = int(iface_suffix)
iface_map[(0, iface_index)] = iface
used_index.add(iface_index)
count = 1
while count in used_index:
count = count + 1
if backplane_exist:
iface_map[(0, count)] = "backplane"
if ptf_port_mapping_mode == "use_sub_interface":
# override those interfaces that has sub interfaces
for i, si in sub_ifaces:
iface_map[(0, i)] = si
return iface_map
elif ptf_port_mapping_mode == "use_orig_interface":
return iface_map
else:
raise ValueError("Unsupported ptf port mapping mode: %s" %
ptf_port_mapping_mode)
def platform_config_update(config):
"""
Update configuration for the remote platform
@param config The configuration dictionary to use/update
"""
remote_port_map = build_ifaces_map(get_ifaces())
config["port_map"] = remote_port_map.copy()
config["caps_table_idx"] = 0