@@ -27,6 +27,7 @@ var gwaddr net.IP
2727var addr * netlink.Addr
2828
2929func init () {
30+ // Parse the arguments
3031 flag .StringVar (& ip , "ip" , "192.168.1.11/24" , "IP network from where the command will be executed" )
3132 flag .StringVar (& intf , "interface" , "eth0" , "interface used to get out of the network" )
3233 flag .StringVar (& command , "command" , "ip route" , "command to be executed" )
@@ -45,64 +46,11 @@ func init() {
4546 flag .Parse ()
4647}
4748
48- func setTcAttributes (link * netlink.Link ) error {
49- // Check if we need to add Qdisc attributes
50- if latency != 0 || jitter != 0 || loss != 0 {
51- log .Debugf ("Add TC : latency %d ms | jitter %d ms | loss %f" , latency * 1000 , jitter * 1000 , loss )
52- netem := netlink.NetemQdiscAttrs {
53- Latency : uint32 (latency ) * 1000 ,
54- Jitter : uint32 (jitter ) * 1000 ,
55- Loss : float32 (loss ),
56- }
57- qdisc := netlink .NewNetem (
58- netlink.QdiscAttrs {
59- LinkIndex : (* link ).Attrs ().Index ,
60- Parent : netlink .HANDLE_ROOT ,
61- },
62- netem ,
63- )
64- err = netlink .QdiscAdd (qdisc )
65- if err != nil {
66- log .Warn ("Error while setting qdisc on macVlan: " , err )
67- return err
68- }
69- }
70- return nil
71- }
72-
73- func newMacVLAN () (* netlink.Link , error ) {
74- macVlan := & netlink.Macvlan {
75- LinkAttrs : netlink.LinkAttrs {
76- Name : "peth0" ,
77- ParentIndex : eth .Attrs ().Index ,
78- TxQLen : - 1 ,
79- },
80- Mode : netlink .MACVLAN_MODE_BRIDGE ,
81- }
82- // Creat the macVLAN
83- err = netlink .LinkAdd (macVlan )
84- if err != nil {
85- log .Warn ("Error while creating macVlan: " , err )
86- return nil , err
87- }
88-
89- // Retrieve the newly created macVLAN
90- link , err := netlink .LinkByName ("peth0" )
91- if err != nil {
92- log .Warn ("Error while getting macVlan: " , err )
93- return nil , err
94- }
95- log .Debugf ("MacVlan created : %+v" , link )
96-
97- return & link , err
98- }
99-
100- func initVar () {
101-
49+ func initVar () error {
10250 lvl , err := logrus .ParseLevel (logLevel )
10351 if err != nil {
10452 logrus .Errorf ("invalid log level %q: %q" , logLevel , err )
105- return
53+ return err
10654 }
10755
10856 // Setup the logger
@@ -116,13 +64,14 @@ func initVar() {
11664 err = setupNetnsDir ()
11765 if err != nil {
11866 log .Warn ("Error setting up netns: " , err )
119- return
67+ return err
12068 }
12169
70+ // Get the link
12271 eth , err = netlink .LinkByName (intf )
12372 if err != nil {
12473 log .Warnf ("Error while getting %s : %s" , intf , err )
125- return
74+ return err
12675 }
12776 log .Debugf ("%s : %+v" , intf , eth .Attrs ().Flags )
12877
@@ -134,10 +83,11 @@ func initVar() {
13483
13584 // If no gateway is specified, we'll use the first route of the given interface
13685 if gateway == "" {
86+ // Get the routes
13787 routes , err := netlink .RouteList (eth , netlink .FAMILY_V4 )
13888 if err != nil {
13989 log .Warn ("Failed to get the route of the interface: " , err )
140- return
90+ return err
14191 }
14292
14393 for _ , r := range routes {
@@ -147,42 +97,16 @@ func initVar() {
14797 }
14898 }
14999 if gateway == "" {
150- log .Warnf ("Couldn't find a default gateway for the specified interface" )
151- return
100+ return fmt .Errorf ("Couldn't find a default gateway for the specified interface" )
152101 }
153102 }
154103 gwaddr = net .ParseIP (gateway )
155104
105+ // Parse the IP
156106 addr , err = netlink .ParseAddr (ip )
157107 if err != nil {
158108 log .Warn ("Failed to parse the given IP: " , err )
159- return
160- }
161- return
162- }
163-
164- func setLinkRoute (link * netlink.Link ) error {
165- return netlink .RouteAdd (& netlink.Route {
166- Scope : netlink .SCOPE_UNIVERSE ,
167- LinkIndex : (* link ).Attrs ().Index ,
168- Gw : gwaddr ,
169- })
170- }
171-
172- func setMacVlanMacAddr (link * netlink.Link ) error {
173- // If a mac was specified, set it now
174- if mac != "" {
175- log .Debugf ("Setting macVlan with specified MAC : %s" , mac )
176- hardwareAddr , err := net .ParseMAC (mac )
177- if err != nil {
178- log .Warn ("Error while parsing given mac: " , err )
179- return err
180- }
181- err = netlink .LinkSetHardwareAddr (* link , hardwareAddr )
182- if err != nil {
183- log .Warn ("Error while setting given mac on macVlan: " , err )
184- return err
185- }
109+ return err
186110 }
187111 return nil
188112}
@@ -200,7 +124,10 @@ func main() {
200124 defer origns .Close ()
201125
202126 // Init de main variables
203- initVar ()
127+ err = initVar ()
128+ if err != nil {
129+ panic (fmt .Sprintf ("panic when initializing vars: %s" , err ))
130+ }
204131
205132 // ============================== Create the macVLAN
206133
0 commit comments