2014-06-24 07:23:51 +08:00
|
|
|
// +build linux
|
|
|
|
|
2014-02-22 14:20:15 +08:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-06-20 07:36:39 +08:00
|
|
|
|
2014-06-10 23:14:16 +08:00
|
|
|
"github.com/docker/libcontainer/utils"
|
2014-02-22 14:20:15 +08:00
|
|
|
)
|
|
|
|
|
2014-02-25 13:11:52 +08:00
|
|
|
// Veth is a network strategy that uses a bridge and creates
|
|
|
|
// a veth pair, one that stays outside on the host and the other
|
|
|
|
// is placed inside the container's namespace
|
2014-02-22 14:20:15 +08:00
|
|
|
type Veth struct {
|
|
|
|
}
|
|
|
|
|
2014-05-17 15:06:29 +08:00
|
|
|
const defaultDevice = "eth0"
|
|
|
|
|
2014-06-26 06:51:28 +08:00
|
|
|
func (v *Veth) Create(n *Network, nspid int, networkState *NetworkState) error {
|
2014-02-22 14:20:15 +08:00
|
|
|
var (
|
2014-06-20 07:36:39 +08:00
|
|
|
bridge = n.Bridge
|
|
|
|
prefix = n.VethPrefix
|
2014-02-22 14:20:15 +08:00
|
|
|
)
|
2014-06-20 07:36:39 +08:00
|
|
|
if bridge == "" {
|
|
|
|
return fmt.Errorf("bridge is not specified")
|
2014-02-22 14:20:15 +08:00
|
|
|
}
|
2014-06-20 07:36:39 +08:00
|
|
|
if prefix == "" {
|
|
|
|
return fmt.Errorf("veth prefix is not specified")
|
2014-02-22 14:20:15 +08:00
|
|
|
}
|
|
|
|
name1, name2, err := createVethPair(prefix)
|
|
|
|
if err != nil {
|
2014-02-27 06:19:39 +08:00
|
|
|
return err
|
2014-02-22 14:20:15 +08:00
|
|
|
}
|
|
|
|
if err := SetInterfaceMaster(name1, bridge); err != nil {
|
2014-02-27 06:19:39 +08:00
|
|
|
return err
|
2014-02-22 14:20:15 +08:00
|
|
|
}
|
|
|
|
if err := SetMtu(name1, n.Mtu); err != nil {
|
2014-02-27 06:19:39 +08:00
|
|
|
return err
|
2014-02-22 14:20:15 +08:00
|
|
|
}
|
|
|
|
if err := InterfaceUp(name1); err != nil {
|
2014-02-27 06:19:39 +08:00
|
|
|
return err
|
2014-02-22 14:20:15 +08:00
|
|
|
}
|
|
|
|
if err := SetInterfaceInNamespacePid(name2, nspid); err != nil {
|
2014-02-27 06:19:39 +08:00
|
|
|
return err
|
2014-02-22 14:20:15 +08:00
|
|
|
}
|
2014-06-25 07:43:00 +08:00
|
|
|
networkState.VethHost = name1
|
|
|
|
networkState.VethChild = name2
|
2014-06-26 06:51:28 +08:00
|
|
|
|
2014-06-25 05:34:09 +08:00
|
|
|
return nil
|
2014-02-22 14:20:15 +08:00
|
|
|
}
|
|
|
|
|
2014-06-26 06:51:28 +08:00
|
|
|
func (v *Veth) Initialize(config *Network, networkState *NetworkState) error {
|
|
|
|
var vethChild = networkState.VethChild
|
|
|
|
if vethChild == "" {
|
|
|
|
return fmt.Errorf("vethChild is not specified")
|
2014-02-22 14:20:15 +08:00
|
|
|
}
|
|
|
|
if err := InterfaceDown(vethChild); err != nil {
|
|
|
|
return fmt.Errorf("interface down %s %s", vethChild, err)
|
|
|
|
}
|
2014-05-17 15:06:29 +08:00
|
|
|
if err := ChangeInterfaceName(vethChild, defaultDevice); err != nil {
|
|
|
|
return fmt.Errorf("change %s to %s %s", vethChild, defaultDevice, err)
|
2014-02-22 14:20:15 +08:00
|
|
|
}
|
2014-05-17 15:06:29 +08:00
|
|
|
if err := SetInterfaceIp(defaultDevice, config.Address); err != nil {
|
|
|
|
return fmt.Errorf("set %s ip %s", defaultDevice, err)
|
2014-02-22 14:20:15 +08:00
|
|
|
}
|
2014-09-25 02:47:23 +08:00
|
|
|
if config.IPv6Address != "" {
|
|
|
|
if err := SetInterfaceIp(defaultDevice, config.IPv6Address); err != nil {
|
2014-09-24 17:36:40 +08:00
|
|
|
return fmt.Errorf("set %s ipv6 %s", defaultDevice, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-17 15:06:29 +08:00
|
|
|
if err := SetMtu(defaultDevice, config.Mtu); err != nil {
|
|
|
|
return fmt.Errorf("set %s mtu to %d %s", defaultDevice, config.Mtu, err)
|
2014-02-22 14:20:15 +08:00
|
|
|
}
|
2014-05-17 15:06:29 +08:00
|
|
|
if err := InterfaceUp(defaultDevice); err != nil {
|
|
|
|
return fmt.Errorf("%s up %s", defaultDevice, err)
|
2014-02-22 14:20:15 +08:00
|
|
|
}
|
|
|
|
if config.Gateway != "" {
|
2014-05-17 15:06:29 +08:00
|
|
|
if err := SetDefaultGateway(config.Gateway, defaultDevice); err != nil {
|
|
|
|
return fmt.Errorf("set gateway to %s on device %s failed with %s", config.Gateway, defaultDevice, err)
|
2014-02-22 14:20:15 +08:00
|
|
|
}
|
|
|
|
}
|
2014-09-25 02:47:23 +08:00
|
|
|
if config.IPv6Gateway != "" {
|
|
|
|
if err := SetDefaultGateway(config.IPv6Gateway, defaultDevice); err != nil {
|
|
|
|
return fmt.Errorf("set gateway for ipv6 to %s on device %s failed with %s", config.IPv6Gateway, defaultDevice, err)
|
2014-09-24 17:36:40 +08:00
|
|
|
}
|
|
|
|
}
|
2014-02-22 14:20:15 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// createVethPair will automatically generage two random names for
|
|
|
|
// the veth pair and ensure that they have been created
|
|
|
|
func createVethPair(prefix string) (name1 string, name2 string, err error) {
|
|
|
|
name1, err = utils.GenerateRandomName(prefix, 4)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
name2, err = utils.GenerateRandomName(prefix, 4)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = CreateVethPair(name1, name2); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|