-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathvlan_linux.go
182 lines (151 loc) · 4.75 KB
/
vlan_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package tenus
import (
"fmt"
"net"
"github.com/docker/libcontainer/netlink"
)
// VlanOptions allows you to specify options for vlan link.
type VlanOptions struct {
// Name of the vlan device
Dev string
// VLAN tag id
Id uint16
// MAC address
MacAddr string
}
// Vlaner is interface which embeds Linker interface and adds few more functions.
type Vlaner interface {
// Linker interface
Linker
// MasterNetInterface returns vlan master network interface
MasterNetInterface() *net.Interface
// Id returns VLAN tag
Id() uint16
}
// VlanLink is a Link which has a master network device.
// Each VlanLink has a VLAN tag id
type VlanLink struct {
Link
// Master device logical network interface
masterIfc *net.Interface
// VLAN tag
id uint16
}
// NewVlanLink creates vlan network link.
//
// It is equivalent of running:
// ip link add name vlan${RANDOM STRING} link ${master interface name} type vlan id ${tag}
// NewVlanLink returns Vlaner which is initialized to a pointer of type VlanLink if the
// vlan link was successfully created on the Linux host. Newly created link is assigned
// a random name starting with "vlan". It returns error if the link can not be created.
func NewVlanLink(masterDev string, id uint16) (Vlaner, error) {
vlanDev := makeNetInterfaceName("vlan")
if ok, err := NetInterfaceNameValid(masterDev); !ok {
return nil, err
}
if _, err := net.InterfaceByName(masterDev); err != nil {
return nil, fmt.Errorf("Master VLAN device %s does not exist on the host", masterDev)
}
if id <= 0 {
return nil, fmt.Errorf("VLAN id must be a postive Integer: %d", id)
}
if err := netlink.NetworkLinkAddVlan(masterDev, vlanDev, id); err != nil {
return nil, err
}
vlanIfc, err := net.InterfaceByName(vlanDev)
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 &VlanLink{
Link: Link{
ifc: vlanIfc,
},
masterIfc: masterIfc,
id: id,
}, nil
}
// NewVlanLinkWithOptions creates vlan network link and sets some of its network parameters
// to values passed in as VlanOptions
//
// It is equivalent of running:
// ip link add name ${vlan name} link ${master interface} address ${macaddress} type vlan id ${tag}
// NewVlanLinkWithOptions returns Vlaner which is initialized to a pointer of type VlanLink if the
// vlan link was created successfully on the Linux host. It accepts VlanOptions which allow you to set
// link's options. It returns error if the link could not be created.
func NewVlanLinkWithOptions(masterDev string, opts VlanOptions) (Vlaner, error) {
if ok, err := NetInterfaceNameValid(masterDev); !ok {
return nil, err
}
if _, err := net.InterfaceByName(masterDev); err != nil {
return nil, fmt.Errorf("Master VLAN device %s does not exist on the host", masterDev)
}
if err := validateVlanOptions(&opts); err != nil {
return nil, err
}
if err := netlink.NetworkLinkAddVlan(masterDev, opts.Dev, opts.Id); err != nil {
return nil, err
}
vlanIfc, 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(vlanIfc, opts.MacAddr); err != nil {
if errDel := DeleteLink(vlanIfc.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
}
vlanIfc.HardwareAddr = hwaddr
}
masterIfc, err := net.InterfaceByName(masterDev)
if err != nil {
return nil, fmt.Errorf("Could not find the new interface: %s", err)
}
return &VlanLink{
Link: Link{
ifc: vlanIfc,
},
masterIfc: masterIfc,
id: opts.Id,
}, nil
}
// NetInterface returns vlan link's network interface
func (vln *VlanLink) NetInterface() *net.Interface {
return vln.ifc
}
// MasterNetInterface returns vlan link's master network interface
func (vln *VlanLink) MasterNetInterface() *net.Interface {
return vln.masterIfc
}
// Id returns vlan link's vlan tag id
func (vln *VlanLink) Id() uint16 {
return vln.id
}
func validateVlanOptions(opts *VlanOptions) error {
if opts.Dev != "" {
if ok, err := NetInterfaceNameValid(opts.Dev); !ok {
return err
}
if _, err := net.InterfaceByName(opts.Dev); err == nil {
return fmt.Errorf("VLAN device %s already assigned on the host", opts.Dev)
}
} else {
opts.Dev = makeNetInterfaceName("vlan")
}
if opts.Id <= 0 {
return fmt.Errorf("Incorrect VLAN tag specified: %d", opts.Id)
}
if _, err := net.ParseMAC(opts.MacAddr); err != nil {
return fmt.Errorf("Incorrect MacAddress specified: %s", opts.MacAddr)
}
return nil
}