Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added some extra utilities for working with bridge and interfaces #14

Merged
merged 2 commits into from
Jan 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,51 @@ func main() {
}
```

### Working with existing bridges and interfaces

The following examples show how to retrieve exisiting interfaces as a tenus link and bridge

```
package main

import (
"fmt"
"log"
"net"

"github.com/milosgajdos83/tenus"
)

func main() {
// RETRIEVE EXISTING BRIDGE
br, err := tenus.BridgeFromName("bridge0")
if err != nil {
log.Fatal(err)
}

// REMOVING AN IP FROM A BRIDGE INTERFACE (BEFORE RECONFIGURATION)
brIp, brIpNet, err := net.ParseCIDR("10.0.41.1/16")
if err != nil {
log.Fatal(err)
}
if err := br.UnsetLinkIp(brIp, brIpNet); err != nil {
log.Fatal(err)
}

// RETRIEVE EXISTING INTERFACE
dl, err := tenus.NewLinkFrom("eth0")
if err != nil {
log.Fatal(err)
}

// RENAMING AN INTERFACE BY NAME
if err := tenus.RenameInterfaceByName("vethPSQSEl", "vethNEWNAME"); err != nil {
log.Fatal(err)
}

}
```

### VLAN and MAC VLAN interfaces

You can check out [VLAN](https://gist.github.com/milosgajdos83/9f68b1818dca886e9ae8) and [Mac VLAN](https://gist.github.com/milosgajdos83/296fb90d076f259a5b0a) examples, too.
Expand Down
19 changes: 19 additions & 0 deletions bridge_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,25 @@ func NewBridgeWithName(ifcName string) (Bridger, error) {
}, nil
}

// BridgeFromName returns a tenus network bridge from an existing bridge of given name on the Linux host.
// It returns error if the bridge of the given name cannot be found.
func BridgeFromName(ifcName string) (Bridger, error) {
if ok, err := NetInterfaceNameValid(ifcName); !ok {
return nil, err
}

newIfc, err := net.InterfaceByName(ifcName)
if err != nil {
return nil, fmt.Errorf("Could not find the new interface: %s", err)
}

return &Bridge{
Link: Link{
ifc: newIfc,
},
}, nil
}

// AddToBridge adds network interfaces to network bridge.
// It is equivalent of running: ip link set ${netIfc name} master ${netBridge name}
// It returns error when it fails to add the network interface to bridge.
Expand Down
4 changes: 4 additions & 0 deletions helpers_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func randomString(size int) string {
return string(bytes)
}

func MakeNetInterfaceName(base string) string {
return makeNetInterfaceName(base)
}

// generates new unused network interfaces name with given prefix
func makeNetInterfaceName(base string) string {
for {
Expand Down
33 changes: 33 additions & 0 deletions link_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ type Linker interface {
SetLinkDown() error
// SetLinkIp configures the link's IP address
SetLinkIp(net.IP, *net.IPNet) error
// UnsetLinkIp remove and IP address from the link
UnsetLinkIp(net.IP, *net.IPNet) error
// SetLinkDefaultGw configures the link's default gateway
SetLinkDefaultGw(*net.IP) error
// SetLinkNetNsPid moves the link to network namespace specified by PID
Expand Down Expand Up @@ -80,6 +82,22 @@ func NewLink(ifcName string) (Linker, error) {
}, nil
}

// NewLinkFrom creates new tenus link on Linux host from an existing interface of given name
func NewLinkFrom(ifcName string) (Linker, error) {
if ok, err := NetInterfaceNameValid(ifcName); !ok {
return nil, err
}

newIfc, err := net.InterfaceByName(ifcName)
if err != nil {
return nil, fmt.Errorf("Could not find the new interface: %s", err)
}

return &Link{
ifc: newIfc,
}, nil
}

// NewLinkWithOptions creates new network link on Linux host and sets some of its network
// parameters passed in as LinkOptions
//
Expand Down Expand Up @@ -174,6 +192,12 @@ func (l *Link) SetLinkIp(ip net.IP, network *net.IPNet) error {
return netlink.NetworkLinkAddIp(l.NetInterface(), ip, network)
}

// UnsetLinkIp configures the link's IP address.
// It is equivalent of running: ip address del ${address}/${mask} dev ${interface name}
func (l *Link) UnsetLinkIp(ip net.IP, network *net.IPNet) error {
return netlink.NetworkLinkDelIp(l.NetInterface(), ip, network)
}

// SetLinkDefaultGw configures the link's default Gateway.
// It is equivalent of running: ip route add default via ${ip address}
func (l *Link) SetLinkDefaultGw(gw *net.IP) error {
Expand Down Expand Up @@ -232,6 +256,15 @@ func (l *Link) SetLinkNsToDocker(name string, dockerHost string) error {
return l.SetLinkNetNsPid(pid)
}

// RenameInterfaceByName renames an interface of given name.
func RenameInterfaceByName(old string, newName string) error {
iface, err := net.InterfaceByName(old)
if err != nil {
return err
}
return netlink.NetworkChangeName(iface, newName)
}

// setLinkOptions validates and sets link's various options passed in as LinkOptions.
func setLinkOptions(ifc *net.Interface, opts LinkOptions) error {
macaddr, mtu, flags, ns := opts.MacAddr, opts.MTU, opts.Flags, opts.Ns
Expand Down