2015-06-10 17:24:19 -04:00
|
|
|
package overlay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/docker/libnetwork/netutils"
|
2015-08-31 14:55:58 -04:00
|
|
|
"github.com/docker/libnetwork/osl"
|
2015-06-10 17:24:19 -04:00
|
|
|
"github.com/vishvananda/netlink"
|
2015-08-07 17:58:41 -04:00
|
|
|
"github.com/vishvananda/netlink/nl"
|
2015-06-10 17:24:19 -04:00
|
|
|
)
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
func validateID(nid, eid string) error {
|
2015-06-10 17:24:19 -04:00
|
|
|
if nid == "" {
|
|
|
|
return fmt.Errorf("invalid network id")
|
|
|
|
}
|
|
|
|
|
|
|
|
if eid == "" {
|
|
|
|
return fmt.Errorf("invalid endpoint id")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createVethPair() (string, string, error) {
|
2015-08-31 14:55:58 -04:00
|
|
|
defer osl.InitOSContext()()
|
|
|
|
|
2015-06-10 17:24:19 -04:00
|
|
|
// Generate a name for what will be the host side pipe interface
|
|
|
|
name1, err := netutils.GenerateIfaceName(vethPrefix, vethLen)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", fmt.Errorf("error generating veth name1: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a name for what will be the sandbox side pipe interface
|
|
|
|
name2, err := netutils.GenerateIfaceName(vethPrefix, vethLen)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", fmt.Errorf("error generating veth name2: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate and add the interface pipe host <-> sandbox
|
|
|
|
veth := &netlink.Veth{
|
|
|
|
LinkAttrs: netlink.LinkAttrs{Name: name1, TxQLen: 0},
|
|
|
|
PeerName: name2}
|
|
|
|
if err := netlink.LinkAdd(veth); err != nil {
|
|
|
|
return "", "", fmt.Errorf("error creating veth pair: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return name1, name2, nil
|
|
|
|
}
|
|
|
|
|
2015-12-10 17:35:49 -05:00
|
|
|
func createVxlan(name string, vni uint32) error {
|
2015-08-31 14:55:58 -04:00
|
|
|
defer osl.InitOSContext()()
|
|
|
|
|
2015-06-10 17:24:19 -04:00
|
|
|
vxlan := &netlink.Vxlan{
|
|
|
|
LinkAttrs: netlink.LinkAttrs{Name: name},
|
|
|
|
VxlanId: int(vni),
|
|
|
|
Learning: true,
|
2015-08-07 17:58:41 -04:00
|
|
|
Port: int(nl.Swap16(vxlanPort)), //network endian order
|
2015-06-10 17:24:19 -04:00
|
|
|
Proxy: true,
|
|
|
|
L3miss: true,
|
|
|
|
L2miss: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := netlink.LinkAdd(vxlan); err != nil {
|
2015-12-10 17:35:49 -05:00
|
|
|
return fmt.Errorf("error creating vxlan interface: %v", err)
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
|
2015-12-10 17:35:49 -05:00
|
|
|
return nil
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func deleteVxlan(name string) error {
|
2015-08-31 14:55:58 -04:00
|
|
|
defer osl.InitOSContext()()
|
|
|
|
|
2015-06-10 17:24:19 -04:00
|
|
|
link, err := netlink.LinkByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to find vxlan interface with name %s: %v", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := netlink.LinkDel(link); err != nil {
|
|
|
|
return fmt.Errorf("error deleting vxlan interface: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-12-21 17:51:39 -05:00
|
|
|
|
|
|
|
func deleteVxlanByVNI(vni uint32) error {
|
|
|
|
defer osl.InitOSContext()()
|
|
|
|
|
|
|
|
links, err := netlink.LinkList()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to list interfaces while deleting vxlan interface by vni: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, l := range links {
|
|
|
|
if l.Type() == "vxlan" && l.(*netlink.Vxlan).VxlanId == int(vni) {
|
|
|
|
err = netlink.LinkDel(l)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error deleting vxlan interface with id %d: %v", vni, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("could not find a vxlan interface to delete with id %d", vni)
|
|
|
|
}
|