-
Notifications
You must be signed in to change notification settings - Fork 61
/
macvtap_linux.go
123 lines (102 loc) · 3.45 KB
/
macvtap_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package tenus
import (
"fmt"
"net"
"github.com/docker/libcontainer/netlink"
)
// MacVtaper embeds MacVlaner interface
type MacVtaper interface {
MacVlaner
}
// MacVtapLink is MacVlanLink. It implements MacVtaper interface
type MacVtapLink struct {
*MacVlanLink
}
// NewMacVtapLink creates macvtap network link
//
// It is equivalent of running:
// ip link add name mvt${RANDOM STRING} link ${master interface} type macvtap
// NewMacVtapLink returns MacVtaper which is initialized to a pointer of type MacVtapLink if the
// macvtap link was created successfully on the Linux host. Newly created link is assigned
// a random name starting with "mvt". It sets the macvlan mode to "bridge" which is a default.
// It returns error if the link could not be created.
func NewMacVtapLink(masterDev string) (MacVtaper, error) {
macVtapDev := makeNetInterfaceName("mvt")
if ok, err := NetInterfaceNameValid(masterDev); !ok {
return nil, err
}
if _, err := net.InterfaceByName(masterDev); err != nil {
return nil, fmt.Errorf("Master MAC VTAP device %s does not exist on the host", masterDev)
}
if err := netlink.NetworkLinkAddMacVtap(masterDev, macVtapDev, default_mode); err != nil {
return nil, err
}
macVtapIfc, err := net.InterfaceByName(macVtapDev)
if err != nil {
return nil, fmt.Errorf("Could not find the new interface: %s", err)
}
masterIfc, err := net.InterfaceByName(masterDev)
if err != nil {
return nil, fmt.Errorf("Could not find the new interface: %s", err)
}
return &MacVtapLink{
MacVlanLink: &MacVlanLink{
Link: Link{
ifc: macVtapIfc,
},
masterIfc: masterIfc,
mode: default_mode,
},
}, nil
}
// NewMacVtapLinkWithOptions creates macvtap network link and can set some of its network parameters
// passed in as MacVlanOptions.
//
// It is equivalent of running:
// ip link add name ${macvlan name} link ${master interface} address ${macaddress} type macvtap mode ${mode}
// NewMacVtapLinkWithOptions returns MacVtaper which is initialized to a pointer of type MacVtapLink if the
// macvtap link was created successfully on the Linux host. It returns error if the macvtap link could not be created.
func NewMacVtapLinkWithOptions(masterDev string, opts MacVlanOptions) (MacVtaper, error) {
if ok, err := NetInterfaceNameValid(masterDev); !ok {
return nil, err
}
if _, err := net.InterfaceByName(masterDev); err != nil {
return nil, fmt.Errorf("Master MAC VLAN device %s does not exist on the host", masterDev)
}
if err := validateMacVlanOptions(&opts); err != nil {
return nil, err
}
if err := netlink.NetworkLinkAddMacVtap(masterDev, opts.Dev, opts.Mode); err != nil {
return nil, err
}
macVtapIfc, err := net.InterfaceByName(opts.Dev)
if err != nil {
return nil, fmt.Errorf("Could not find the new interface: %s", err)
}
if opts.MacAddr != "" {
if err := netlink.NetworkSetMacAddress(macVtapIfc, opts.MacAddr); err != nil {
if errDel := DeleteLink(macVtapIfc.Name); errDel != nil {
return nil, fmt.Errorf("Incorrect options specified. Attempt to delete the link failed: %s",
errDel)
}
}
hwaddr, err := net.ParseMAC(opts.MacAddr)
if err != nil {
return nil, err
}
macVtapIfc.HardwareAddr = hwaddr
}
masterIfc, err := net.InterfaceByName(masterDev)
if err != nil {
return nil, fmt.Errorf("Could not find the new interface: %s", err)
}
return &MacVtapLink{
MacVlanLink: &MacVlanLink{
Link: Link{
ifc: macVtapIfc,
},
masterIfc: masterIfc,
mode: opts.Mode,
},
}, nil
}