@@ -7,6 +7,11 @@ import (
7
7
"github.com/docker/libcontainer/netlink"
8
8
)
9
9
10
+ // Default MacVlan mode
11
+ const (
12
+ default_mode = "bridge"
13
+ )
14
+
10
15
// Supported macvlan modes by tenus package
11
16
var MacVlanModes = map [string ]bool {
12
17
"private" : true ,
@@ -17,7 +22,7 @@ var MacVlanModes = map[string]bool{
17
22
// MacVlanOptions allows you to specify some options for macvlan link.
18
23
type MacVlanOptions struct {
19
24
// macvlan device name
20
- MacVlanDev string
25
+ Dev string
21
26
// macvlan mode
22
27
Mode string
23
28
// MAC address
@@ -47,13 +52,12 @@ type MacVlanLink struct {
47
52
// NewMacVlanLink creates macvlan network link
48
53
//
49
54
// It is equivalent of running:
50
- // ip link add name mc${RANDOM STRING} link ${master interface} type macvlan mode ${mode}
55
+ // ip link add name mc${RANDOM STRING} link ${master interface} type macvlan
51
56
// NewMacVlanLink returns MacVlaner which is initialized to a pointer of type MacVlanLink if the
52
57
// macvlan link was created successfully on the Linux host. Newly created link is assigned
53
- // a random name starting with "mc". It sets the macvlan mode the parameter passed as argument.
54
- // If incorrect network mode is passed as a paramter, it sets the macvlan mode to "bridge".
58
+ // a random name starting with "mc". It sets the macvlan mode to "bridge" mode which is a default.
55
59
// It returns error if the link could not be created.
56
- func NewMacVlanLink (masterDev string , mode string ) (MacVlaner , error ) {
60
+ func NewMacVlanLink (masterDev string ) (MacVlaner , error ) {
57
61
macVlanDev := makeNetInterfaceName ("mc" )
58
62
59
63
if ok , err := NetInterfaceNameValid (masterDev ); ! ok {
@@ -64,15 +68,7 @@ func NewMacVlanLink(masterDev string, mode string) (MacVlaner, error) {
64
68
return nil , fmt .Errorf ("Master MAC VLAN device %s does not exist on the host" , masterDev )
65
69
}
66
70
67
- if mode != "" {
68
- if _ , ok := MacVlanModes [mode ]; ! ok {
69
- return nil , fmt .Errorf ("Unsupported MacVlan mode specified: %s" , mode )
70
- }
71
- } else {
72
- mode = "bridge"
73
- }
74
-
75
- if err := netlink .NetworkLinkAddMacVlan (masterDev , macVlanDev , mode ); err != nil {
71
+ if err := netlink .NetworkLinkAddMacVlan (masterDev , macVlanDev , default_mode ); err != nil {
76
72
return nil , err
77
73
}
78
74
@@ -91,7 +87,7 @@ func NewMacVlanLink(masterDev string, mode string) (MacVlaner, error) {
91
87
ifc : macVlanIfc ,
92
88
},
93
89
masterIfc : masterIfc ,
94
- mode : mode ,
90
+ mode : default_mode ,
95
91
}, nil
96
92
}
97
93
@@ -101,12 +97,9 @@ func NewMacVlanLink(masterDev string, mode string) (MacVlaner, error) {
101
97
// It is equivalent of running:
102
98
// ip link add name ${macvlan name} link ${master interface} address ${macaddress} type macvlan mode ${mode}
103
99
// NewMacVlanLinkWithOptions returns MacVlaner which is initialized to a pointer of type MacVlanLink if the
104
- // macvlan link was created successfully on the Linux host. It returns error if the macvlan link could not be created.
100
+ // macvlan link was created successfully on the Linux host. If particular option is empty, it sets default value if possible.
101
+ // It returns error if the macvlan link could not be created or if incorrect options have been passed.
105
102
func NewMacVlanLinkWithOptions (masterDev string , opts MacVlanOptions ) (MacVlaner , error ) {
106
- macVlanDev := opts .MacVlanDev
107
- mode := opts .Mode
108
- macaddr := opts .MacAddr
109
-
110
103
if ok , err := NetInterfaceNameValid (masterDev ); ! ok {
111
104
return nil , err
112
105
}
@@ -115,42 +108,23 @@ func NewMacVlanLinkWithOptions(masterDev string, opts MacVlanOptions) (MacVlaner
115
108
return nil , fmt .Errorf ("Master MAC VLAN device %s does not exist on the host" , masterDev )
116
109
}
117
110
118
- if macVlanDev != "" {
119
- if ok , err := NetInterfaceNameValid (macVlanDev ); ! ok {
120
- return nil , err
121
- }
122
-
123
- if _ , err := net .InterfaceByName (macVlanDev ); err == nil {
124
- return nil , fmt .Errorf ("MAC VLAN device %s already assigned on the host" , macVlanDev )
125
- }
126
- } else {
127
- macVlanDev = makeNetInterfaceName ("mc" )
128
- }
129
-
130
- if mode != "" {
131
- if _ , ok := MacVlanModes [mode ]; ! ok {
132
- return nil , fmt .Errorf ("Unsupported MacVlan mode specified: %s" , mode )
133
- }
134
- } else {
135
- mode = "bridge"
111
+ if err := validateMacVlanOptions (& opts ); err != nil {
112
+ return nil , err
136
113
}
137
114
138
- if err := netlink .NetworkLinkAddMacVlan (masterDev , macVlanDev , opts .Mode ); err != nil {
115
+ if err := netlink .NetworkLinkAddMacVlan (masterDev , opts . Dev , opts .Mode ); err != nil {
139
116
return nil , err
140
117
}
141
118
142
- macVlanIfc , err := net .InterfaceByName (macVlanDev )
119
+ macVlanIfc , err := net .InterfaceByName (opts . Dev )
143
120
if err != nil {
144
121
return nil , fmt .Errorf ("Could not find the new interface: %s" , err )
145
122
}
146
123
147
- if macaddr != "" {
148
- if _ , err = net .ParseMAC (macaddr ); err == nil {
149
- if err := netlink .NetworkSetMacAddress (macVlanIfc , macaddr ); err != nil {
150
- if errDel := DeleteLink (macVlanIfc .Name ); err != nil {
151
- return nil , fmt .Errorf ("Incorrect options specified. Attempt to delete the link failed: %s" ,
152
- errDel )
153
- }
124
+ if opts .MacAddr != "" {
125
+ if err := netlink .NetworkSetMacAddress (macVlanIfc , opts .MacAddr ); err != nil {
126
+ if errDel := DeleteLink (macVlanIfc .Name ); errDel != nil {
127
+ return nil , fmt .Errorf ("Incorrect options specified. Attempt to delete the link failed: %s" , errDel )
154
128
}
155
129
}
156
130
}
@@ -165,7 +139,7 @@ func NewMacVlanLinkWithOptions(masterDev string, opts MacVlanOptions) (MacVlaner
165
139
ifc : macVlanIfc ,
166
140
},
167
141
masterIfc : masterIfc ,
168
- mode : mode ,
142
+ mode : opts . Mode ,
169
143
}, nil
170
144
}
171
145
@@ -183,3 +157,33 @@ func (macvln *MacVlanLink) MasterNetInterface() *net.Interface {
183
157
func (macvln * MacVlanLink ) Mode () string {
184
158
return macvln .mode
185
159
}
160
+
161
+ func validateMacVlanOptions (opts * MacVlanOptions ) error {
162
+ if opts .Dev != "" {
163
+ if ok , err := NetInterfaceNameValid (opts .Dev ); ! ok {
164
+ return err
165
+ }
166
+
167
+ if _ , err := net .InterfaceByName (opts .Dev ); err == nil {
168
+ return fmt .Errorf ("MAC VLAN device %s already assigned on the host" , opts .Dev )
169
+ }
170
+ } else {
171
+ opts .Dev = makeNetInterfaceName ("mc" )
172
+ }
173
+
174
+ if opts .Mode != "" {
175
+ if _ , ok := MacVlanModes [opts .Mode ]; ! ok {
176
+ return fmt .Errorf ("Unsupported MacVlan mode specified: %s" , opts .Mode )
177
+ }
178
+ } else {
179
+ opts .Mode = default_mode
180
+ }
181
+
182
+ if opts .MacAddr != "" {
183
+ if _ , err := net .ParseMAC (opts .MacAddr ); err == nil {
184
+ return fmt .Errorf ("Incorrect MAC ADDRESS specified: %s" , opts .MacAddr )
185
+ }
186
+ }
187
+
188
+ return nil
189
+ }
0 commit comments