Skip to content

Commit 61d5882

Browse files
authored
Merge pull request #22 from threefoldtech/main_PrivateMyceliumPeers
revise mycelium wiring
2 parents 750a050 + 634dd95 commit 61d5882

File tree

4 files changed

+106
-37
lines changed

4 files changed

+106
-37
lines changed

pkg/netbase/ifaceutil/ifaceutil.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package ifaceutil
2+
3+
import (
4+
"fmt"
5+
"net"
6+
7+
"github.com/containernetworking/plugins/pkg/ns"
8+
"github.com/threefoldtech/zosbase/pkg/network/namespace"
9+
"github.com/vishvananda/netlink"
10+
)
11+
12+
const (
13+
myceliumPort = "9651"
14+
)
15+
16+
// GetIPsForIFace retrieves the IP addresses for a given interface name in a specified network namespace.
17+
// If the namespace name is empty, it retrieves the IP addresses from the host.
18+
func GetIPsForIFace(iface, nsName string) ([]net.IPNet, error) {
19+
getIPs := func() ([]net.IPNet, error) {
20+
var results []net.IPNet
21+
22+
ln, err := netlink.LinkByName(iface)
23+
if err != nil {
24+
return nil, err
25+
}
26+
27+
ips, err := netlink.AddrList(ln, netlink.FAMILY_V4)
28+
if err != nil {
29+
return nil, err
30+
}
31+
32+
for _, ip := range ips {
33+
results = append(results, *ip.IPNet)
34+
}
35+
36+
return results, nil
37+
}
38+
39+
if nsName == "" {
40+
return getIPs()
41+
}
42+
43+
netns, err := namespace.GetByName(nsName)
44+
if err != nil {
45+
return nil, err
46+
}
47+
defer netns.Close()
48+
49+
var results []net.IPNet
50+
err = netns.Do(func(_ ns.NetNS) error {
51+
var getErr error
52+
results, getErr = getIPs()
53+
return getErr
54+
})
55+
56+
return results, err
57+
}
58+
59+
// BuildMyceliumPeerURLs constructs a list of Mycelium peer URLs from a list of IP networks.
60+
func BuildMyceliumPeerURLs(ips []net.IPNet) []string {
61+
peers := make([]string, len(ips))
62+
for i, ip := range ips {
63+
peers[i] = fmt.Sprintf("tcp://%s:%s", ip.IP.String(), myceliumPort)
64+
}
65+
return peers
66+
}

pkg/netlight/resource/mycelium.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ import (
1313

1414
"github.com/containernetworking/plugins/pkg/ns"
1515
"github.com/pkg/errors"
16+
"github.com/threefoldtech/zosbase/pkg/netbase/ifaceutil"
1617
"github.com/threefoldtech/zosbase/pkg/netlight/resource/peers"
1718
"github.com/threefoldtech/zosbase/pkg/zinit"
1819
)
1920

2021
const (
21-
myceliumBin = "mycelium"
22-
MyceliumSeedDir = "/tmp/network/mycelium"
22+
myceliumBin = "mycelium"
23+
MyceliumSeedDir = "/tmp/network/mycelium"
24+
myceliumPublicInterface = "zos"
2325

2426
myceliumSeedLen = 6
2527
HostMyceliumBr = "br-hmy"
@@ -190,7 +192,14 @@ func SetupMycelium(netNS ns.NetNS, mycelium string, seed []byte) error {
190192
return fmt.Errorf("failed to create seed file '%s': %w", name, err)
191193
}
192194

193-
return ensureMyceliumService(zinit.Default(), &name, list)
195+
ips, err := ifaceutil.GetIPsForIFace(myceliumPublicInterface, "")
196+
if err != nil || len(ips) == 0 {
197+
return fmt.Errorf("failed to get ip for interface 'zos': %w", err)
198+
}
199+
200+
hostPeers := ifaceutil.BuildMyceliumPeerURLs(ips)
201+
202+
return ensureMyceliumService(zinit.Default(), &name, hostPeers)
194203
}
195204

196205
if err := os.WriteFile(filepath.Join(MyceliumSeedDir, HostMyceliumBr), seed, 0444); err != nil {

pkg/network/ndmz/dualstack.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ const (
3737
//NdmzBridge is the name of the ipv4 routing bridge in the host namespace
3838
NdmzBridge = "br-ndmz"
3939

40-
//dmzNamespace name of the dmz namespace
41-
dmzNamespace = "ndmz"
40+
//DmzNamespace name of the dmz namespace
41+
DmzNamespace = "ndmz"
4242

4343
ndmzNsMACDerivationSuffix6 = "-ndmz6"
4444
ndmzNsMACDerivationSuffix4 = "-ndmz4"
4545

46-
// dmzPub4 ipv4 public interface
47-
dmzPub4 = "npub4"
46+
// DmzPub4 ipv4 public interface
47+
DmzPub4 = "npub4"
4848
// dmzPub6 ipv6 public interface
4949
dmzPub6 = "npub6"
5050

@@ -69,7 +69,7 @@ func New(nodeID string, public *netlink.Bridge) DMZ {
6969
}
7070

7171
func (d *dmzImpl) Namespace() string {
72-
return dmzNamespace
72+
return DmzNamespace
7373
}
7474

7575
// Create create the NDMZ network namespace and configure its default routes and addresses
@@ -85,9 +85,9 @@ func (d *dmzImpl) Create(ctx context.Context) error {
8585
// master will actually be `zos`. In that case, we can't plug the physical
8686
// iface, but need to create a veth pair between br-pub and zos.
8787

88-
netNS, err := namespace.GetByName(dmzNamespace)
88+
netNS, err := namespace.GetByName(DmzNamespace)
8989
if err != nil {
90-
netNS, err = namespace.Create(dmzNamespace)
90+
netNS, err = namespace.Create(DmzNamespace)
9191
if err != nil {
9292
return errors.Wrap(err, "failed to create ndmz namespace")
9393
}
@@ -103,7 +103,7 @@ func (d *dmzImpl) Create(ctx context.Context) error {
103103
return errors.Wrapf(err, "ndmz: could not node create pub iface 6")
104104
}
105105

106-
if err := createPubIface4(dmzPub4, d.nodeID, netNS); err != nil {
106+
if err := createPubIface4(DmzPub4, d.nodeID, netNS); err != nil {
107107
return errors.Wrapf(err, "ndmz: could not create pub iface 4")
108108
}
109109

@@ -134,7 +134,7 @@ func (d *dmzImpl) Create(ctx context.Context) error {
134134
}
135135

136136
z := zinit.Default()
137-
dhcpMon := NewDHCPMon(dmzPub4, dmzNamespace, z)
137+
dhcpMon := NewDHCPMon(DmzPub4, DmzNamespace, z)
138138
go func() {
139139
_ = dhcpMon.Start(ctx)
140140
}()
@@ -144,7 +144,7 @@ func (d *dmzImpl) Create(ctx context.Context) error {
144144

145145
// Delete deletes the NDMZ network namespace
146146
func (d *dmzImpl) Delete() error {
147-
netNS, err := namespace.GetByName(dmzNamespace)
147+
netNS, err := namespace.GetByName(DmzNamespace)
148148
if err == nil {
149149
if err := namespace.Delete(netNS); err != nil {
150150
return errors.Wrap(err, "failed to delete ndmz network namespace")
@@ -230,7 +230,7 @@ func (d *dmzImpl) AttachNR(networkID, nrNSName string, ipamLeaseDir string) erro
230230

231231
func (d *dmzImpl) GetIPFor(inf string) ([]net.IPNet, error) {
232232

233-
netns, err := namespace.GetByName(dmzNamespace)
233+
netns, err := namespace.GetByName(DmzNamespace)
234234
if err != nil {
235235
return nil, err
236236
}
@@ -262,7 +262,7 @@ func (d *dmzImpl) GetIPFor(inf string) ([]net.IPNet, error) {
262262
func (d *dmzImpl) GetIP(family int) ([]net.IPNet, error) {
263263
var links []string
264264
if family == netlink.FAMILY_V4 || family == netlink.FAMILY_ALL {
265-
links = append(links, dmzPub4)
265+
links = append(links, DmzPub4)
266266
}
267267
if family == netlink.FAMILY_V6 || family == netlink.FAMILY_ALL {
268268
links = append(links, dmzPub6)
@@ -272,7 +272,7 @@ func (d *dmzImpl) GetIP(family int) ([]net.IPNet, error) {
272272
return nil, nil
273273
}
274274

275-
netns, err := namespace.GetByName(dmzNamespace)
275+
netns, err := namespace.GetByName(DmzNamespace)
276276
if err != nil {
277277
return nil, err
278278
}
@@ -305,7 +305,7 @@ func (d *dmzImpl) GetIP(family int) ([]net.IPNet, error) {
305305

306306
// Get gateway to given destination ip
307307
func (d *dmzImpl) GetDefaultGateway(destination net.IP) (net.IP, error) {
308-
netns, err := namespace.GetByName(dmzNamespace)
308+
netns, err := namespace.GetByName(DmzNamespace)
309309
if err != nil {
310310
return nil, err
311311
}
@@ -378,7 +378,7 @@ func (d *dmzImpl) Interfaces() ([]types.IfaceInfo, error) {
378378
}
379379

380380
// get the ndmz network namespace
381-
ndmz, err := namespace.GetByName(dmzNamespace)
381+
ndmz, err := namespace.GetByName(DmzNamespace)
382382
if err != nil {
383383
return nil, err
384384
}
@@ -398,10 +398,10 @@ func waitIP4() error {
398398
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
399399
defer cancel()
400400

401-
probe, err := dhcp.Probe(ctx, dmzPub4)
401+
probe, err := dhcp.Probe(ctx, DmzPub4)
402402

403403
if err != nil {
404-
return errors.Wrapf(err, "error while proping interface '%s'", dmzPub4)
404+
return errors.Wrapf(err, "error while proping interface '%s'", DmzPub4)
405405
}
406406
if len(probe.IP) != 0 && len(probe.Router) != 0 {
407407
return nil
@@ -554,7 +554,7 @@ func applyFirewall() error {
554554
return errors.Wrap(err, "failed to build nft rule set")
555555
}
556556

557-
if err := nft.Apply(&buf, dmzNamespace); err != nil {
557+
if err := nft.Apply(&buf, DmzNamespace); err != nil {
558558
return errors.Wrap(err, "failed to apply nft rule set")
559559
}
560560

pkg/network/nr/net_resource.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import (
1313
"strings"
1414
"time"
1515

16-
"github.com/threefoldtech/zosbase/pkg/environment"
1716
"github.com/threefoldtech/zosbase/pkg/gridtypes/zos"
1817
"github.com/threefoldtech/zosbase/pkg/network/ifaceutil"
1918
"github.com/threefoldtech/zosbase/pkg/network/macvlan"
19+
"github.com/threefoldtech/zosbase/pkg/network/ndmz"
2020
"github.com/threefoldtech/zosbase/pkg/network/options"
2121
"github.com/threefoldtech/zosbase/pkg/network/tuntap"
2222
"github.com/threefoldtech/zosbase/pkg/zinit"
@@ -33,6 +33,8 @@ import (
3333
"github.com/threefoldtech/zosbase/pkg/network/namespace"
3434
"github.com/threefoldtech/zosbase/pkg/network/wireguard"
3535
"github.com/vishvananda/netlink"
36+
37+
baseifaceutil "github.com/threefoldtech/zosbase/pkg/netbase/ifaceutil"
3638
)
3739

3840
const (
@@ -258,11 +260,6 @@ func (nr *NetResource) SetMycelium() (err error) {
258260
return nil
259261
}
260262

261-
peers, err := environment.GetConfig()
262-
if err != nil {
263-
return errors.Wrap(err, "failed to get public mycelium peer list")
264-
}
265-
266263
config := nr.resource.Mycelium
267264
// create the bridge.
268265
if err := nr.ensureMyceliumBridge(); err != nil {
@@ -311,17 +308,14 @@ func (nr *NetResource) SetMycelium() (err error) {
311308
"--peers",
312309
}
313310

314-
// first append peers from user input.
315-
// right now this is shadowed by Mycelium config validation
316-
// which does not allow custom peer list.
317-
args = AppendFunc(args, config.Peers, func(mp zos.MyceliumPeer) string {
318-
return string(mp)
319-
})
320-
321-
// global peers list
322-
args = append(args, peers.Mycelium.Peers...)
311+
// set mycelium public addresses are the private peers
312+
ips, err := baseifaceutil.GetIPsForIFace(ndmz.DmzPub4, ndmz.DmzNamespace)
313+
if err != nil {
314+
return errors.Wrap(err, "failed to get IPs for npub4")
315+
}
323316

324-
// todo: add custom peers requested by the user
317+
hostPeers := baseifaceutil.BuildMyceliumPeerURLs(ips)
318+
args = append(args, hostPeers...)
325319

326320
err = zinit.AddService(name, zinit.InitService{
327321
Exec: strings.Join(args, " "),

0 commit comments

Comments
 (0)