func main() { // Create a new network bridge br, err := tenus.NewBridgeWithName("mybridge") if err != nil { log.Fatal(err) } // Bring the bridge up if err = br.SetLinkUp(); err != nil { fmt.Println(err) } // Create a dummy link dl, err := tenus.NewLink("mydummylink") if err != nil { log.Fatal(err) } // Add the dummy link into bridge if err = br.AddSlaveIfc(dl.NetInterface()); err != nil { log.Fatal(err) } // Bring the dummy link up if err = dl.SetLinkUp(); err != nil { fmt.Println(err) } }
func BridgeInit(bridgeMAC string, nmIgnoreFile string, log *logging.Logger) (*HostNetwork, error) { if os.Getpid() == 1 { panic(errors.New("Cannot use BridgeInit from child.")) } htn := &HostNetwork{ BridgeMAC: bridgeMAC, } if nmIgnoreFile != "" { if _, err := os.Stat(nmIgnoreFile); os.IsNotExist(err) { log.Warning("Warning! Network Manager may not properly configured to ignore the bridge interface! This may result in management conflicts!") } } br, err := tenus.BridgeFromName(ozDefaultInterfaceBridge) if err != nil { log.Info("Bridge not found, attempting to create a new one") br, err = tenus.NewBridgeWithName(ozDefaultInterfaceBridge) if err != nil { return nil, fmt.Errorf("Unable to create bridge %+v", err) } } htn.Interface = br if err := htn.configureBridgeInterface(log); err != nil { return nil, err } brL := br.NetInterface() addrs, err := brL.Addrs() if err != nil { return nil, fmt.Errorf("Unable to get bridge interface addresses: %+v", err) } // Build the ip range which we will use for the network if err := htn.buildBridgeNetwork(addrs); err != nil { return nil, err } return htn, nil }
func main() { // CREATE BRIDGE AND BRING IT UP br, err := tenus.NewBridgeWithName("mybridge") if err != nil { log.Fatal(err) } ip, ipNet, err := net.ParseCIDR("10.0.41.1/16") if err != nil { log.Fatal(err) } if err := br.SetLinkIp(ip, ipNet); err != nil { fmt.Println(err) } if err = br.SetLinkUp(); err != nil { fmt.Println(err) } // CREATE VETH PAIR veth, err := tenus.NewVethPairWithOptions("myveth01", tenus.VethOptions{PeerName: "myveth02"}) if err != nil { log.Fatal(err) } // ASSIGN AN IP TO MYVETH01 ip, ipNet, err = net.ParseCIDR("10.0.41.2/16") if err != nil { log.Fatal(err) } if err = veth.SetLinkIp(ip, ipNet); err != nil { fmt.Println(err) } // ASSIGN AN IP TO MYVETH02 ip, ipNet, err = net.ParseCIDR("10.0.41.3/16") if err != nil { log.Fatal(err) } if err := veth.SetPeerLinkIp(ip, ipNet); err != nil { fmt.Println(err) } // ADD MYVETH01 INTERFACE TO THE MYBRIDGE BRIDGE AND BRING IT UP // we could also simply do myveth01 := veth.NetInterface() myveth01, err := net.InterfaceByName("myveth01") if err != nil { log.Fatal(err) } if err = br.AddSlaveIfc(myveth01); err != nil { fmt.Println(err) } if err = veth.SetLinkUp(); err != nil { fmt.Println(err) } // ADD MYVETH02 INTERFACE TO THE MYBRIDGE BRIDGE AND BRING IT UP // we could also simply do myveth01 := veth.NetInterface() myveth02, err := net.InterfaceByName("myveth02") if err != nil { log.Fatal(err) } if err = br.AddSlaveIfc(myveth02); err != nil { fmt.Println(err) } if err = veth.SetPeerLinkUp(); err != nil { fmt.Println(err) } // CREATE MACVLAN INTERFACE AND BRING IT UP macvlan, err := tenus.NewMacVlanLinkWithOptions("eth0", tenus.MacVlanOptions{Mode: "bridge", MacVlanDev: "macvlan01"}) if err != nil { log.Fatal(err) } if err := macvlan.SetLinkUp(); err != nil { fmt.Println(err) } // CREATE VLAN INTERFACE AND BRING IT UP vlan, err := tenus.NewVlanLinkWithOptions("eth1", tenus.VlanOptions{Id: 10, VlanDev: "vlan01"}) if err != nil { log.Fatal(err) } if err = vlan.SetLinkUp(); err != nil { fmt.Println(err) } }
func main() { // CREATE BRIDGE AND BRING IT UP br, err := tenus.NewBridgeWithName("vethbridge") if err != nil { log.Fatal(err) } brIp, brIpNet, err := net.ParseCIDR("10.0.41.1/16") if err != nil { log.Fatal(err) } if err := br.SetLinkIp(brIp, brIpNet); err != nil { fmt.Println(err) } if err = br.SetLinkUp(); err != nil { fmt.Println(err) } // CREATE VETH PAIR veth, err := tenus.NewVethPairWithOptions("myveth01", tenus.VethOptions{PeerName: "myveth02"}) if err != nil { log.Fatal(err) } // ASSIGN IP ADDRESS TO THE HOST VETH INTERFACE vethHostIp, vethHostIpNet, err := net.ParseCIDR("10.0.41.2/16") if err != nil { log.Fatal(err) } if err := veth.SetLinkIp(vethHostIp, vethHostIpNet); err != nil { fmt.Println(err) } // ADD MYVETH01 INTERFACE TO THE MYBRIDGE BRIDGE myveth01, err := net.InterfaceByName("myveth01") if err != nil { log.Fatal(err) } if err = br.AddSlaveIfc(myveth01); err != nil { fmt.Println(err) } if err = veth.SetLinkUp(); err != nil { fmt.Println(err) } // PASS VETH PEER INTERFACE TO A RUNNING DOCKER BY PID pid, err := tenus.DockerPidByName("vethdckr", "/var/run/docker.sock") if err != nil { fmt.Println(err) } if err := veth.SetPeerLinkNsPid(pid); err != nil { log.Fatal(err) } // ALLOCATE AND SET IP FOR THE NEW DOCKER INTERFACE vethGuestIp, vethGuestIpNet, err := net.ParseCIDR("10.0.41.5/16") if err != nil { log.Fatal(err) } if err := veth.SetPeerLinkNetInNs(pid, vethGuestIp, vethGuestIpNet, nil); err != nil { log.Fatal(err) } }