Compare commits
No commits in common. "0618b41ae2aba36241c9a4219fbdb2e3a169d780" and "88ad00ae93dfcbba2bbe15d4ba450bac5a1302ec" have entirely different histories.
0618b41ae2
...
88ad00ae93
62
main.go
62
main.go
@ -1,8 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
@ -48,8 +46,8 @@ func cmdAdd(args *skel.CmdArgs) error {
|
||||
return fmt.Errorf("failed to get Mycelium IP: %v", err)
|
||||
}
|
||||
|
||||
// Create veth pair with unique naming
|
||||
hostVethName := generateVethName(args.ContainerID)
|
||||
// Create veth pair
|
||||
hostVethName := fmt.Sprintf("veth-%s", args.ContainerID[:8])
|
||||
containerVethName := "eth0"
|
||||
|
||||
hostVeth, containerVeth, err := createVethPair(hostVethName, containerVethName)
|
||||
@ -103,7 +101,7 @@ func cmdCheck(args *skel.CmdArgs) error {
|
||||
|
||||
func cmdDel(args *skel.CmdArgs) error {
|
||||
// Clean up veth pair (host side will be automatically removed)
|
||||
hostVethName := generateVethName(args.ContainerID)
|
||||
hostVethName := fmt.Sprintf("veth-%s", args.ContainerID[:8])
|
||||
|
||||
link, err := netlink.LinkByName(hostVethName)
|
||||
if err != nil {
|
||||
@ -148,25 +146,14 @@ func getMyceliumIP(interfaceName string) (net.IP, error) {
|
||||
}
|
||||
|
||||
func generateContainerIP(myceliumPrefix net.IP, containerID string) net.IP {
|
||||
// Generate a unique container IP within the /64 prefix using container ID hash
|
||||
hash := sha256.Sum256([]byte(containerID))
|
||||
|
||||
// Generate a container IP within the /64 prefix
|
||||
// Using simple approach: prefix + ::1 (could be made more sophisticated)
|
||||
containerIP := make(net.IP, len(myceliumPrefix))
|
||||
copy(containerIP, myceliumPrefix)
|
||||
|
||||
// Use first 8 bytes of hash for the host part (last 64 bits)
|
||||
copy(containerIP[8:], hash[:8])
|
||||
|
||||
containerIP[15] = 1 // Set last byte to 1
|
||||
return containerIP
|
||||
}
|
||||
|
||||
func generateVethName(containerID string) string {
|
||||
// Generate unique but short veth name using hash of container ID
|
||||
hash := sha256.Sum256([]byte(containerID))
|
||||
shortHash := hex.EncodeToString(hash[:4]) // Use first 4 bytes for 8-char hex
|
||||
return fmt.Sprintf("veth-%s", shortHash)
|
||||
}
|
||||
|
||||
func createVethPair(hostName, containerName string) (netlink.Link, netlink.Link, error) {
|
||||
veth := &netlink.Veth{
|
||||
LinkAttrs: netlink.LinkAttrs{Name: hostName},
|
||||
@ -229,47 +216,26 @@ func configureContainerInterface(containerNS netns.NsHandle, ifName string, cont
|
||||
return err
|
||||
}
|
||||
|
||||
// Switch to main namespace to get host veth address
|
||||
if err := netns.Set(originalNS); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Get host veth link-local address
|
||||
// Get host veth link-local address for routing
|
||||
hostVeth, err := netlink.LinkByName(hostVethName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
hostAddrs, err := netlink.AddrList(hostVeth, netlink.FAMILY_V6)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var hostLLAddr net.IP
|
||||
if err == nil {
|
||||
for _, addr := range hostAddrs {
|
||||
if addr.IP.IsLinkLocalUnicast() {
|
||||
hostLLAddr = addr.IP
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Switch back to container namespace to add route
|
||||
if err := netns.Set(containerNS); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if hostLLAddr != nil {
|
||||
// Add route to Mycelium network via host veth
|
||||
route := &netlink.Route{
|
||||
Dst: &net.IPNet{
|
||||
IP: net.ParseIP("400::"),
|
||||
Mask: net.CIDRMask(7, 128),
|
||||
},
|
||||
Gw: hostLLAddr,
|
||||
Gw: addr.IP,
|
||||
LinkIndex: link.Attrs().Index,
|
||||
}
|
||||
if err := netlink.RouteAdd(route); err != nil {
|
||||
return err
|
||||
netlink.RouteAdd(route)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user