forked from scionproto/scion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
221 lines (169 loc) · 6.25 KB
/
common.py
File metadata and controls
221 lines (169 loc) · 6.25 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# Copyright 2018 ETH Zurich, Anapaya Systems
#
# 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
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Stdlib
import os
import subprocess
import sys
# SCION
from lib.packet.scion_addr import ISD_AS
from topology.net import AddressProxy
COMMON_DIR = 'endhost'
SCION_SERVICE_NAMES = (
"BeaconService",
"CertificateService",
"BorderRouters",
"PathService",
"DiscoveryService",
)
BR_CONFIG_NAME = 'br.toml'
BS_CONFIG_NAME = 'bs.toml'
CS_CONFIG_NAME = 'cs.toml'
PS_CONFIG_NAME = 'ps.toml'
SD_CONFIG_NAME = 'sd.toml'
DISP_CONFIG_NAME = 'disp.toml'
SIG_CONFIG_NAME = 'sig.toml'
DOCKER_USR_VOL = ['/etc/passwd:/etc/passwd:ro', '/etc/group:/etc/group:ro']
class ArgsBase:
def __init__(self, args):
for k, v in vars(args).items():
setattr(self, k, v)
class ArgsTopoConfig(ArgsBase):
def __init__(self, args, topo_config):
"""
:param object args: Contains the passed command line arguments as named attributes.
:param dict topo_config: The parsed topology config.
"""
super().__init__(args)
self.config = topo_config
class ArgsTopoDicts(ArgsBase):
def __init__(self, args, topo_dicts, port_gen=None):
"""
:param object args: Contains the passed command line arguments as named attributes.
:param dict topo_dicts: The generated topo dicts from TopoGenerator.
:param PortGenerator port_gen: The port generator
"""
super().__init__(args)
self.topo_dicts = topo_dicts
self.port_gen = port_gen
class TopoID(ISD_AS):
def ISD(self):
return "ISD%s" % self.isd_str()
def AS(self):
return "AS%s" % self.as_str()
def AS_file(self):
return "AS%s" % self.as_file_fmt()
def file_fmt(self):
return "%s-%s" % (self.isd_str(), self.as_file_fmt())
def base_dir(self, out_dir):
return os.path.join(out_dir, self.ISD(), self.AS_file())
def __lt__(self, other):
return str(self) < str(other)
def __repr__(self):
return "<TopoID: %s>" % self
def prom_addr_br(br_id, br_ele, port):
"""Get the prometheus address for a border router"""
pub = get_pub(br_ele['InternalAddrs'])
return "[%s]:%s" % (pub['PublicOverlay']['Addr'].ip, port)
def prom_addr_infra(docker, infra_id, infra_ele, port):
"""Get the prometheus address for an infrastructure element."""
pub = get_pub(infra_ele['Addrs'])
# For dockerized infra use scion port since multiple instances can be on the same IP,
# so we can't use the default port.
port = pub['Public']['L4Port'] if docker else port
return "[%s]:%s" % (pub['Public']['Addr'].ip, port)
def prom_addr_sciond(docker, topo_id, networks, port):
for i, net in enumerate(networks):
for prog, ip_net in networks[net].items():
if prog == 'sd%s' % topo_id.file_fmt():
return '[%s]:%s' % (ip_net.ip, port)
return None
def prom_addr_dispatcher(docker, topo_id, networks, port, disp_type):
if not docker:
return "[127.0.0.1]:%s" % port
target_name = ''
if disp_type == 'br':
target_name = 'br%s-1_ctrl' % topo_id.file_fmt()
elif disp_type == 'sig':
target_name = 'sig%s' % topo_id.file_fmt()
else:
target_name = 'disp%s' % topo_id.file_fmt()
for _, net in enumerate(networks):
if target_name in networks[net]:
return '[%s]:%s' % (networks[net][target_name].ip, port)
return None
def get_pub(topo_addr):
pub = topo_addr.get('IPv6')
if pub is not None:
return pub
return topo_addr['IPv4']
def get_pub_ip(topo_addr):
return get_pub(topo_addr)["Public"]["Addr"].ip
def get_l4_port(topo_addr):
return get_pub(topo_addr)["Public"]["L4Port"]
def srv_iter(topo_dicts, out_dir, common=False):
for topo_id, as_topo in topo_dicts.items():
base = topo_id.base_dir(out_dir)
for service in SCION_SERVICE_NAMES:
for elem in as_topo[service]:
yield topo_id, as_topo, os.path.join(base, elem)
if common:
yield topo_id, as_topo, os.path.join(base, COMMON_DIR)
def docker_image(args, image):
if args.docker_registry:
image = '%s/%s' % (args.docker_registry, image)
else:
image = 'scion_%s' % image
if args.image_tag:
image = '%s:%s' % (image, args.image_tag)
return image
def docker_host(in_docker, docker, addr=None):
if in_docker:
# If in-docker we need to know the DOCKER0 IP
addr = os.getenv('DOCKER0', None)
if not addr:
print('DOCKER0 env variable required! Exiting!')
sys.exit(1)
elif docker or not addr:
# Using docker topology or there is no default addr,
# we directly get the DOCKER0 IP
addr = docker_ip()
return addr
def docker_ip():
return subprocess.check_output(['tools/docker-ip']).decode("utf-8").strip()
def remote_nets(networks, topo_id):
"""
Returns the subnets of all remote ASes the SIG in topo_id is connected to.
:param networks dict: Scion elem to subnet/IP map.
:param topo_id: A key of a topo dict generated by TopoGenerator.
:return: String of comma separated subnets.
"""
rem_nets = []
for key in networks:
if 'sig' in key and topo_id.file_fmt() not in key:
rem_nets.append(str(networks[key][0]['net']))
return ','.join(rem_nets)
def sciond_name(topo_id):
return 'sd%s' % topo_id.file_fmt()
def sciond_svc_name(topo_id):
return 'scion_%s' % sciond_name(topo_id)
def json_default(o):
if isinstance(o, AddressProxy):
return str(o.ip)
raise TypeError
def trust_db_conf_entry(args, name):
db_dir = '/share/cache' if args.docker else 'gen-cache'
return {
'Backend': 'sqlite',
'Connection': os.path.join(db_dir, '%s.trust.db' % name),
}