/
netlink.go
126 lines (106 loc) · 4.11 KB
/
netlink.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
// Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package netlinkwrapper
import (
"net"
"github.com/vishvananda/netlink"
)
// NetLink wraps methods used from the vishvananda/netlink package
type NetLink interface {
// LinkByName gets a link object given the device name
LinkByName(name string) (netlink.Link, error)
// LinkSetNsFd is equivalent to `ip link set $link netns $ns`
LinkSetNsFd(link netlink.Link, fd int) error
// ParseAddr parses an address string
ParseAddr(s string) (*netlink.Addr, error)
// AddrAdd is equivalent to `ip addr add $addr dev $link`
AddrAdd(link netlink.Link, addr *netlink.Addr) error
// AddrList is equivalent to `ip addr show `
AddrList(link netlink.Link, family int) ([]netlink.Addr, error)
// LinkAdd is equivalent to `ip link add`
LinkAdd(link netlink.Link) error
// LinkSetMaster is equivalent to `ip link set $link master $master`
LinkSetMaster(link netlink.Link, master *netlink.Bridge) error
// LinkSetName sets the interface name
LinkSetName(link netlink.Link, name string) error
// LinkSetUp is equivalent to `ip link set $link up`
LinkSetUp(link netlink.Link) error
// LinkSetHardwareAddr sets the hardware address of the link device. Equivalent to: `ip link set $link address $hwaddr`
LinkSetHardwareAddr(link netlink.Link, hwaddr net.HardwareAddr) error
// LinkSetMTU is equivalent to `ip link set $link mtu $mtu`
LinkSetMTU(link netlink.Link, mtu int) error
// LinkList is equivalent to: `ip link show`
LinkList() ([]netlink.Link, error)
// LinkSetDown is equivalent to: `ip link set $link down`
LinkSetDown(link netlink.Link) error
// RouteList gets a list of routes in the system.
RouteList(link netlink.Link, family int) ([]netlink.Route, error)
// RouteAdd will add a route to the route table
RouteAdd(route *netlink.Route) error
// RouteDel is equivalent to `ip route del`
RouteDel(route *netlink.Route) error
}
type netLink struct {
}
// NewNetLink creates a new NetLink object
func NewNetLink() NetLink {
return &netLink{}
}
func (*netLink) LinkSetMaster(link netlink.Link, master *netlink.Bridge) error {
return netlink.LinkSetMaster(link, master)
}
func (*netLink) LinkAdd(link netlink.Link) error {
return netlink.LinkAdd(link)
}
func (*netLink) LinkByName(name string) (netlink.Link, error) {
return netlink.LinkByName(name)
}
func (*netLink) LinkSetNsFd(link netlink.Link, fd int) error {
return netlink.LinkSetNsFd(link, fd)
}
func (*netLink) ParseAddr(s string) (*netlink.Addr, error) {
return netlink.ParseAddr(s)
}
func (*netLink) AddrAdd(link netlink.Link, addr *netlink.Addr) error {
return netlink.AddrAdd(link, addr)
}
func (*netLink) LinkSetUp(link netlink.Link) error {
return netlink.LinkSetUp(link)
}
func (*netLink) LinkSetName(link netlink.Link, name string) error {
return netlink.LinkSetName(link, name)
}
func (*netLink) LinkSetHardwareAddr(link netlink.Link, hwaddr net.HardwareAddr) error {
return netlink.LinkSetHardwareAddr(link, hwaddr)
}
func (*netLink) LinkSetMTU(link netlink.Link, mtu int) error {
return netlink.LinkSetMTU(link, mtu)
}
func (*netLink) LinkList() ([]netlink.Link, error) {
return netlink.LinkList()
}
func (*netLink) LinkSetDown(link netlink.Link) error {
return netlink.LinkSetDown(link)
}
func (*netLink) RouteList(link netlink.Link, family int) ([]netlink.Route, error) {
return netlink.RouteList(link, family)
}
func (*netLink) RouteAdd(route *netlink.Route) error {
return netlink.RouteAdd(route)
}
func (*netLink) RouteDel(route *netlink.Route) error {
return netlink.RouteDel(route)
}
func (*netLink) AddrList(link netlink.Link, family int) ([]netlink.Addr, error) {
return netlink.AddrList(link, family)
}