Skip to content

Commit

Permalink
pass refactor topology endhost_port_range
Browse files Browse the repository at this point in the history
  • Loading branch information
JordiSubira committed Feb 29, 2024
1 parent f72949d commit 8b957f4
Show file tree
Hide file tree
Showing 20 changed files with 57 additions and 55 deletions.
3 changes: 1 addition & 2 deletions acceptance/router_multi/conf/topology.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"isd_as": "1-ff00:0:1",
"mtu": 1472,
"attributes": [],
"endhost_start_port": 1024,
"endhost_end_port": 65535,
"endhost_port_range": "1024-65535",
"border_routers": {
"brA": {
"internal_addr": "192.168.0.11:30001",
Expand Down
3 changes: 1 addition & 2 deletions acceptance/topo_common/topology.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"isd_as": "1-ff00:0:110",
"mtu": 1400,
"endhost_start_port": 1024,
"endhost_end_port": 65535,
"endhost_port_range": "1024-65535",
"attributes": [
"core"
],
Expand Down
3 changes: 1 addition & 2 deletions acceptance/topo_cs_reload/testdata/topology_reload.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"isd_as": "1-ff00:0:110",
"mtu": 1400,
"endhost_start_port": 1024,
"endhost_end_port": 65535,
"endhost_port_range": "1024-65535",
"attributes": [
"core"
],
Expand Down
3 changes: 1 addition & 2 deletions acceptance/topo_daemon_reload/testdata/topology_reload.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"isd_as": "1-ff00:0:110",
"mtu": 1400,
"endhost_start_port": 1024,
"endhost_end_port": 65535,
"endhost_port_range": "1024-65535",
"attributes": [
"core"
],
Expand Down
3 changes: 1 addition & 2 deletions control/beaconing/testdata/topology-core.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"isd_as": "1-ff00:0:110",
"mtu": 1472,
"endhost_start_port": 1024,
"endhost_end_port": 65535,
"endhost_port_range": "1024-65535",
"attributes": [
"core"
],
Expand Down
3 changes: 1 addition & 2 deletions control/beaconing/testdata/topology.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"isd_as": "1-ff00:0:111",
"mtu": 1472,
"endhost_start_port": 1024,
"endhost_end_port": 65535,
"endhost_port_range": "1024-65535",
"attributes": [],
"border_routers": {
"br1-ff00_0_111-1": {
Expand Down
3 changes: 1 addition & 2 deletions pkg/experimental/hiddenpath/testdata/topology.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"isd_as": "1-ff00:0:111",
"mtu": 1472,
"endhost_start_port": 1024,
"endhost_end_port": 65535,
"endhost_port_range": "1024-65535",
"attributes": [],
"border_routers": {
"br1-ff00_0_111-1": {
Expand Down
8 changes: 6 additions & 2 deletions pkg/snet/snet.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func listenUDPRange(addr *net.UDPAddr, start, end uint16) (*net.UDPConn, error)
}
return pconn, nil
}
return nil, serrors.New("There are no UDP ports available in range", "start", start, "end", end)
return nil, serrors.WrapStr("binding to port range", syscall.EADDRINUSE)
}

var _ Network = (*SCIONNetwork)(nil)
Expand Down Expand Up @@ -182,7 +182,11 @@ func (n *SCIONNetwork) Dial(ctx context.Context, network string, listen *net.UDP
//
// The context is used for connection setup, it doesn't affect the returned
// connection.
func (n *SCIONNetwork) Listen(ctx context.Context, network string, listen *net.UDPAddr) (*Conn, error) {
func (n *SCIONNetwork) Listen(
ctx context.Context,
network string,
listen *net.UDPAddr,
) (*Conn, error) {

metrics.CounterInc(n.Metrics.Listens)

Expand Down
3 changes: 1 addition & 2 deletions private/topology/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ type Topology struct {
TimestampHuman string `json:"timestamp_human,omitempty"`
IA string `json:"isd_as"`
MTU int `json:"mtu"`
EndhostStartPort int `json:"endhost_start_port"`
EndhostEndPort int `json:"endhost_end_port"`
EndhostPortRange string `json:"endhost_port_range"`
// Attributes specify whether this is a core AS or not.
Attributes Attributes `json:"attributes"`
BorderRouters map[string]*BRInfo `json:"border_routers,omitempty"`
Expand Down
3 changes: 1 addition & 2 deletions private/topology/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func TestLoadRawFromFile(t *testing.T) {
TimestampHuman: "May 6 00:00:00 CET 1975",
IA: "6-ff00:0:362",
MTU: 1472,
EndhostStartPort: 1024,
EndhostEndPort: 1<<16 - 1,
EndhostPortRange: "1024-65535",
Attributes: []jsontopo.Attribute{jsontopo.AttrCore},
BorderRouters: map[string]*jsontopo.BRInfo{
"borderrouter6-f00:0:362-1": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"timestamp_human": "May 6 00:00:00 CET 1975",
"isd_as": "6-ff00:0:362",
"mtu": 1472,
"endhost_start_port": 1024,
"endhost_end_port": 65535,
"endhost_port_range": "1024-65535",
"attributes": [
"authoritative",
"core",
Expand Down
3 changes: 1 addition & 2 deletions private/topology/json/testdata/topology.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"timestamp_human": "May 6 00:00:00 CET 1975",
"isd_as": "6-ff00:0:362",
"mtu": 1472,
"endhost_start_port": 1024,
"endhost_end_port": 65535,
"endhost_port_range": "1024-65535",
"attributes": [
"core"
],
Expand Down
3 changes: 1 addition & 2 deletions private/topology/testdata/basic.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"timestamp_human": "1975-05-06 01:02:03.000000+0000",
"isd_as": "1-ff00:0:311",
"mtu": 1472,
"endhost_start_port": 1024,
"endhost_end_port": 65535,
"endhost_port_range": "1024-65535",
"attributes": [],
"border_routers": {
"br1-ff00:0:311-1": {
Expand Down
3 changes: 1 addition & 2 deletions private/topology/testdata/core.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"timestamp_human": "May 6 00:00:00 CET 1975",
"isd_as": "6-ff00:0:362",
"mtu": 1472,
"endhost_start_port": 1024,
"endhost_end_port": 65535,
"endhost_port_range": "1024-65535",
"attributes": [
"core"
],
Expand Down
40 changes: 29 additions & 11 deletions private/topology/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"net"
"os"
"sort"
"strconv"
"strings"
"time"

"github.com/scionproto/scion/pkg/addr"
Expand Down Expand Up @@ -205,18 +207,10 @@ func (t *RWTopology) populateMeta(raw *jsontopo.Topology) error {
}
t.MTU = raw.MTU

if raw.EndhostStartPort < 1 || raw.EndhostStartPort > (1<<16-1) {
return serrors.New("Invalid value for start port", "start port", raw.EndhostStartPort)
}
if raw.EndhostEndPort < 1 || raw.EndhostEndPort > (1<<16-1) {
return serrors.New("Invalid value for end port", "end port", raw.EndhostEndPort)
}
if raw.EndhostStartPort > raw.EndhostEndPort {
return serrors.New("Start port is bigger than end port for the SCION port range",
"start port", raw.EndhostStartPort, "end port", raw.EndhostEndPort)
t.EndhostStartPort, t.EndhostEndPort, err = validatePortRange(raw.EndhostPortRange)
if err != nil {
return err
}
t.EndhostStartPort = uint16(raw.EndhostStartPort)
t.EndhostEndPort = uint16(raw.EndhostEndPort)

isCore := false
for _, attr := range raw.Attributes {
Expand All @@ -229,6 +223,30 @@ func (t *RWTopology) populateMeta(raw *jsontopo.Topology) error {
return nil
}

func validatePortRange(portRange string) (uint16, uint16, error) {
ports := strings.Split(portRange, "-")
if len(ports) != 2 {
return 0, 0, serrors.New("invalid format: expected startPort-endPort", "got", portRange)
}
startPort, errStart := strconv.Atoi(ports[0])
endPort, errEnd := strconv.Atoi(ports[1])
if errStart != nil || errEnd != nil {
return 0, 0, serrors.New("invalid port numbers", "got", portRange)
}

if startPort < 1 || startPort > (1<<16-1) {
return 0, 0, serrors.New("Invalid value for start port", "start port", startPort)
}
if endPort < 1 || endPort > (1<<16-1) {
return 0, 0, serrors.New("Invalid value for end port", "end port", endPort)
}
if startPort > endPort {
return 0, 0, serrors.New("Start port is bigger than end port for the SCION port range",
"start port", startPort, "end port", endPort)
}
return uint16(startPort), uint16(endPort), nil
}

func (t *RWTopology) populateBR(raw *jsontopo.Topology) error {
for name, rawBr := range raw.BorderRouters {
if rawBr.InternalAddr == "" {
Expand Down
3 changes: 1 addition & 2 deletions router/control/testdata/topology.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"isd_as": "1-ff00:0:110",
"mtu": 1472,
"endhost_start_port": 1024,
"endhost_end_port": 65535,
"endhost_port_range": "1024-65535",
"attributes": [
"core"
],
Expand Down
8 changes: 3 additions & 5 deletions tools/topology/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
DEFAULT_MTU,
DEFAULT6_NETWORK,
NETWORKS_FILE,
DEFAULT_ENDHOST_START_PORT,
DEFAULT_ENDHOST_END_PORT,
DEFAULT_ENDHOST_PORT_RANGE,
)
from topology.scion_addr import ISD_AS
from topology.util import write_file
Expand Down Expand Up @@ -88,8 +87,7 @@ def _read_defaults(self, network):
self.subnet_gen4 = SubnetGenerator(DEFAULT_NETWORK, self.args.docker)
self.subnet_gen6 = SubnetGenerator(DEFAULT6_NETWORK, self.args.docker)
self.default_mtu = defaults.get("mtu", DEFAULT_MTU)
self.endhost_start_port = defaults.get("endhost_start_port", DEFAULT_ENDHOST_START_PORT)
self.endhost_end_port = defaults.get("endhost_end_port", DEFAULT_ENDHOST_END_PORT)
self.endhost_port_range = defaults.get("endhost_port_range", DEFAULT_ENDHOST_PORT_RANGE)

def generate_all(self):
"""
Expand Down Expand Up @@ -151,7 +149,7 @@ def _generate_topology(self):
def _topo_args(self):
return TopoGenArgs(self.args, self.topo_config, self.subnet_gen4,
self.subnet_gen6, self.default_mtu,
self.endhost_start_port, self.endhost_end_port)
self.endhost_port_range)

def _generate_supervisor(self, topo_dicts):
args = self._supervisor_args(topo_dicts)
Expand Down
3 changes: 1 addition & 2 deletions tools/topology/defines.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
#: Default SCION router UDP port.
SCION_ROUTER_PORT = 50000

DEFAULT_ENDHOST_START_PORT = 1024
DEFAULT_ENDHOST_END_PORT = (1 << 16) - 1
DEFAULT_ENDHOST_PORT_RANGE = "1024-65535"

#: Default MTU - assumes overlay is ipv4+udp
DEFAULT_MTU = 1500 - 20 - 8
Expand Down
2 changes: 1 addition & 1 deletion tools/topology/net.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def _exclude_net(self, alloc, net):

class PortGenerator(object):
# TODO(JordiSubira): It probably makes more sense to also feed the port
# range for CP services, so that it is within [endhost_start_port, endhost_end_port]
# range for CP services, so that it is within endhost_port_range
def __init__(self):
self.iter = iter(range(31000, 35000))
self._ports = defaultdict(lambda: next(self.iter))
Expand Down
9 changes: 3 additions & 6 deletions tools/topology/topo.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ def __init__(self,
subnet_gen4: SubnetGenerator,
subnet_gen6: SubnetGenerator,
default_mtu: int,
start_port: int,
end_port: int):
endhost_port_range: str):
"""
:param ArgsBase args: Contains the passed command line arguments.
:param dict topo_config: The parsed topology config.
Expand All @@ -83,8 +82,7 @@ def __init__(self,
ADDR_TYPE_6: subnet_gen6,
}
self.default_mtu = default_mtu
self.endhost_start_port = start_port
self.endhost_end_port = end_port
self.endhost_port_range = endhost_port_range
self.port_gen = PortGenerator()


Expand Down Expand Up @@ -247,8 +245,7 @@ def _generate_as_topo(self, topo_id, as_conf):
'isd_as': str(topo_id),
'mtu': mtu,
'test_dispatcher': as_conf.get('test_dispatcher', True),
'endhost_start_port': as_conf.get('endhost_start_port', self.args.endhost_start_port),
'endhost_end_port': as_conf.get('endhost_end_port', self.args.endhost_end_port),
'endhost_port_range': as_conf.get('endhost_port_range', self.args.endhost_port_range),
}
for i in SCION_SERVICE_NAMES:
self.topo_dicts[topo_id][i] = {}
Expand Down

0 comments on commit 8b957f4

Please sign in to comment.