本文整理汇总了Golang中github.com/vishvananda/netlink.AddrAdd函数的典型用法代码示例。如果您正苦于以下问题:Golang AddrAdd函数的具体用法?Golang AddrAdd怎么用?Golang AddrAdd使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了AddrAdd函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: setupBridgeIPv6
func setupBridgeIPv6(config *networkConfiguration, i *bridgeInterface) error {
procFile := "/proc/sys/net/ipv6/conf/" + config.BridgeName + "/disable_ipv6"
ipv6BridgeData, err := ioutil.ReadFile(procFile)
if err != nil {
return fmt.Errorf("Cannot read IPv6 setup for bridge %v: %v", config.BridgeName, err)
}
// Enable IPv6 on the bridge only if it isn't already enabled
if ipv6BridgeData[0] != '0' {
if err := ioutil.WriteFile(procFile, []byte{'0', '\n'}, ipv6ForwardConfPerm); err != nil {
return fmt.Errorf("Unable to enable IPv6 addresses on bridge: %v", err)
}
}
_, addrsv6, err := i.addresses()
if err != nil {
return err
}
// Add the default link local ipv6 address if it doesn't exist
if !findIPv6Address(netlink.Addr{IPNet: bridgeIPv6}, addrsv6) {
if err := netlink.AddrAdd(i.Link, &netlink.Addr{IPNet: bridgeIPv6}); err != nil {
return &IPv6AddrAddError{IP: bridgeIPv6, Err: err}
}
}
// Store bridge network and default gateway
i.bridgeIPv6 = bridgeIPv6
i.gatewayIPv6 = i.bridgeIPv6.IP
if config.AddressIPv6 == nil {
return nil
}
// Store and program user specified bridge network and network gateway
i.bridgeIPv6 = config.AddressIPv6
i.gatewayIPv6 = config.AddressIPv6.IP
if err := netlink.AddrAdd(i.Link, &netlink.Addr{IPNet: i.bridgeIPv6}); err != nil {
return &IPv6AddrAddError{IP: i.bridgeIPv6, Err: err}
}
// Setting route to global IPv6 subnet
logrus.Debugf("Adding route to IPv6 network %s via device %s", config.AddressIPv6.String(), config.BridgeName)
err = netlink.RouteAdd(&netlink.Route{
Scope: netlink.SCOPE_UNIVERSE,
LinkIndex: i.Link.Attrs().Index,
Dst: config.AddressIPv6,
})
if err != nil && !os.IsExist(err) {
logrus.Errorf("Could not add route to IPv6 network %s via device %s", config.AddressIPv6.String(), config.BridgeName)
}
return nil
}
示例2: createDummyInterface
func createDummyInterface(t *testing.T) {
if testutils.RunningOnCircleCI() {
t.Skipf("Skipping as not supported on CIRCLE CI kernel")
}
dummy := &netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
Name: "dummy",
},
}
err := netlink.LinkAdd(dummy)
require.NoError(t, err)
dummyLink, err := netlink.LinkByName("dummy")
require.NoError(t, err)
ip, ipNet, err := net.ParseCIDR("10.1.1.1/24")
require.NoError(t, err)
ipNet.IP = ip
ipAddr := &netlink.Addr{IPNet: ipNet, Label: ""}
err = netlink.AddrAdd(dummyLink, ipAddr)
require.NoError(t, err)
}
示例3: setAddr4
// sets IP4 addr on link removing any existing ones first
func setAddr4(link *netlink.Vxlan, ipn *net.IPNet) error {
addrs, err := netlink.AddrList(link, syscall.AF_INET)
if err != nil {
return err
}
addr := netlink.Addr{ipn, ""}
existing := false
for _, old := range addrs {
if old.IPNet.String() == addr.IPNet.String() {
existing = true
continue
}
if err = netlink.AddrDel(link, &old); err != nil {
return fmt.Errorf("failed to delete IPv4 addr %s from %s", old.String(), link.Attrs().Name)
}
}
if !existing {
if err = netlink.AddrAdd(link, &addr); err != nil {
return fmt.Errorf("failed to add IP address %s to %s: %s", ipn.String(), link.Attrs().Name, err)
}
}
return nil
}
示例4: configureIface
// ConfigureIface takes the result of IPAM plugin and
// applies to the ifName interface
func configureIface(conf *NetConf) error {
ifName := conf.IfName
link, err := netlink.LinkByName(ifName)
if err != nil {
return fmt.Errorf("failed to lookup %q: %v", ifName, err)
}
if err := netlink.LinkSetUp(link); err != nil {
return fmt.Errorf("failed to set %q UP: %v", ifName, err)
}
i, n, err := net.ParseCIDR(conf.IPAddr)
if err != nil {
return fmt.Errorf("failed to parse ip address :%s", conf.IPAddr)
}
addr := &net.IPNet{IP: i, Mask: n.Mask}
nlAddr := &netlink.Addr{IPNet: addr, Label: ""}
if err = netlink.AddrAdd(link, nlAddr); err != nil {
return fmt.Errorf("failed to add IP addr to %q: %v", ifName, err)
}
gw := net.ParseIP(conf.Gateway)
if gw == nil {
return fmt.Errorf("parse gateway: %s return nil", conf.Gateway)
}
if err = ip.AddDefaultRoute(gw, link); err != nil {
// we skip over duplicate routes as we assume the first one wins
if !os.IsExist(err) {
return fmt.Errorf("failed to add default route via %v dev %v: %v", gw, ifName, err)
}
}
return nil
}
示例5: SetupNetwork
/* SetupNetwork()
*/
func SetupNetwork(addr string) error {
// Bring up loop back interface.
lo, e := netlink.LinkByName("lo")
if e != nil {
return fmt.Errorf("Failed to find loopback interface: %v", e)
}
if e := netlink.LinkSetUp(lo); e != nil {
return fmt.Errorf("Failed to setup loopback interface: %v", e)
}
if len(addr) > 0 {
veth, e := netlink.LinkByName("veth0")
if e != nil {
return fmt.Errorf("Failed to find veth interface: %v", e)
}
addr, e := netlink.ParseAddr(addr)
if e != nil {
return fmt.Errorf("Failed to parse NetworkAddr: %v", e)
}
netlink.AddrAdd(veth, addr)
if e := netlink.LinkSetUp(veth); e != nil {
return fmt.Errorf("Network link failed to come up: %v", e)
}
}
return nil
}
示例6: IpAddrAdd
func IpAddrAdd(cResp chan<- *Response, rawArgs *json.RawMessage, tag string) {
args := &struct {
Ifname string `json:"ifname"`
IpCidr string `json:"ip"`
}{}
json.Unmarshal(*rawArgs, &args)
iface, err := netlink.LinkByName(args.Ifname)
if err != nil {
cResp <- &Response{nil, tag, NewRTNetlinkError(err)}
return
}
ip, err := netlink.ParseAddr(args.IpCidr)
if err != nil {
cResp <- &Response{nil, tag, NewRTNetlinkError(err)}
return
}
if err := netlink.AddrAdd(iface, ip); err != nil {
cResp <- &Response{nil, tag, NewRTNetlinkError(err)}
return
}
cResp <- &Response{true, tag, nil}
}
示例7: setInterfaceIPv6
func setInterfaceIPv6(iface netlink.Link, settings *Interface) error {
if settings.AddressIPv6 == nil {
return nil
}
ipAddr := &netlink.Addr{IPNet: settings.AddressIPv6, Label: ""}
return netlink.AddrAdd(iface, ipAddr)
}
示例8: addIPConfigToLink
func addIPConfigToLink(ipConfig *ipam.IPConfig, link netlink.Link, ifName string) error {
log.Debugf("Configuring link %+v/%s with %+v", link, ifName, ipConfig)
addr := &netlink.Addr{IPNet: &ipConfig.IP}
if err := netlink.AddrAdd(link, addr); err != nil {
return fmt.Errorf("failed to add addr to %q: %v", ifName, err)
}
// Sort provided routes to make sure we apply any more specific
// routes first which may be used as nexthops in wider routes
sort.Sort(ipam.ByMask(ipConfig.Routes))
for _, r := range ipConfig.Routes {
log.Debugf("Adding route %+v", r)
rt := &netlink.Route{
LinkIndex: link.Attrs().Index,
Scope: netlink.SCOPE_UNIVERSE,
Dst: &r.Destination,
Gw: r.NextHop,
}
if r.IsL2() {
rt.Scope = netlink.SCOPE_LINK
}
if err := netlink.RouteAdd(rt); err != nil {
if !os.IsExist(err) {
return fmt.Errorf("failed to add route '%s via %v dev %v': %v",
r.Destination.String(), r.NextHop, ifName, err)
}
}
}
return nil
}
示例9: setupHostVeth
func setupHostVeth(vethName string, ipConf *types.IPConfig) error {
// hostVeth moved namespaces and may have a new ifindex
veth, err := netlink.LinkByName(vethName)
if err != nil {
return fmt.Errorf("failed to lookup %q: %v", vethName, err)
}
// TODO(eyakubovich): IPv6
ipn := &net.IPNet{
IP: ipConf.Gateway,
Mask: net.CIDRMask(32, 32),
}
addr := &netlink.Addr{IPNet: ipn, Label: ""}
if err = netlink.AddrAdd(veth, addr); err != nil {
return fmt.Errorf("failed to add IP addr (%#v) to veth: %v", ipn, err)
}
ipn = &net.IPNet{
IP: ipConf.IP.IP,
Mask: net.CIDRMask(32, 32),
}
// dst happens to be the same as IP/net of host veth
if err = ip.AddHostRoute(ipn, nil, veth); err != nil && !os.IsExist(err) {
return fmt.Errorf("failed to add route on host: %v", err)
}
return nil
}
示例10: setInterfaceIPv6
func setInterfaceIPv6(iface netlink.Link, i *nwIface) error {
if i.AddressIPv6() == nil {
return nil
}
ipAddr := &netlink.Addr{IPNet: i.AddressIPv6(), Label: "", Flags: syscall.IFA_F_NODAD}
return netlink.AddrAdd(iface, ipAddr)
}
示例11: configureIface
func configureIface(ifname string, ipn ip.IP4Net, mtu int) error {
iface, err := netlink.LinkByName(ifname)
if err != nil {
return fmt.Errorf("failed to lookup interface %v", ifname)
}
err = netlink.AddrAdd(iface, &netlink.Addr{IPNet: ipn.ToIPNet(), Label: ""})
if err != nil {
return fmt.Errorf("failed to add IP address %v to %v: %v", ipn.String(), ifname, err)
}
err = netlink.LinkSetMTU(iface, mtu)
if err != nil {
return fmt.Errorf("failed to set MTU for %v: %v", ifname, err)
}
err = netlink.LinkSetUp(iface)
if err != nil {
return fmt.Errorf("failed to set interface %v to UP state: %v", ifname, err)
}
// explicitly add a route since there might be a route for a subnet already
// installed by Docker and then it won't get auto added
err = netlink.RouteAdd(&netlink.Route{
LinkIndex: iface.Attrs().Index,
Scope: netlink.SCOPE_UNIVERSE,
Dst: ipn.Network().ToIPNet(),
})
if err != nil && err != syscall.EEXIST {
return fmt.Errorf("failed to add route (%v -> %v): %v", ipn.Network().String(), ifname, err)
}
return nil
}
示例12: ConfigureIface
// ConfigureIface takes the result of IPAM plugin and
// applies to the ifName interface
func ConfigureIface(ifName string, res *types.Result) error {
link, err := netlink.LinkByName(ifName)
if err != nil {
return fmt.Errorf("failed to lookup %q: %v", ifName, err)
}
if err := netlink.LinkSetUp(link); err != nil {
return fmt.Errorf("failed to set %q UP: %v", ifName, err)
}
// TODO(eyakubovich): IPv6
addr := &netlink.Addr{IPNet: &res.IP4.IP, Label: ""}
if err = netlink.AddrAdd(link, addr); err != nil {
return fmt.Errorf("failed to add IP addr to %q: %v", ifName, err)
}
for _, r := range res.IP4.Routes {
gw := r.GW
if gw == nil {
gw = res.IP4.Gateway
}
if err = ip.AddRoute(&r.Dst, gw, link); err != nil {
// we skip over duplicate routes as we assume the first one wins
if !os.IsExist(err) {
return fmt.Errorf("failed to add route '%v via %v dev %v': %v", r.Dst, gw, ifName, err)
}
}
}
return nil
}
示例13: main
func main() {
lo, _ := netlink.LinkByName("lo")
fmt.Println(lo)
addr, _ := netlink.ParseAddr("127.0.0.2/8")
fmt.Println(addr)
netlink.AddrAdd(lo, addr)
fmt.Println(netlink.AddrList(lo, netlink.FAMILY_ALL))
req := nl.NewNetlinkRequest(syscall.NLMSG_MIN_TYPE, syscall.NLM_F_REQUEST)
data := append([]byte("TASKSTATS"), 0)
a := &Attr{
Type: 2,
Data: data,
}
Hdr := []byte{
3,
0,
0,
0,
}
m := &Message{
Type: 16,
Pid: -1,
Seq: -1,
Flags: NLM_F_REQUEST,
Payload: append(Hdr, a.Dump()...),
}
req.AddData(m)
res, _ := req.Execute(syscall.NETLINK_GENERIC, 0)
fmt.Println(res)
fmt.Println(parse_attributes(res[0][4:])[1])
}
示例14: createInterface
func createInterface(t *testing.T, name string, nws ...string) {
// Add interface
link := &netlink.Bridge{
LinkAttrs: netlink.LinkAttrs{
Name: "test",
},
}
bips := []*net.IPNet{}
for _, nw := range nws {
bip, err := types.ParseCIDR(nw)
if err != nil {
t.Fatal(err)
}
bips = append(bips, bip)
}
if err := netlink.LinkAdd(link); err != nil {
t.Fatalf("Failed to create interface via netlink: %v", err)
}
for _, bip := range bips {
if err := netlink.AddrAdd(link, &netlink.Addr{IPNet: bip}); err != nil {
t.Fatal(err)
}
}
if err := netlink.LinkSetUp(link); err != nil {
t.Fatal(err)
}
}
示例15: Create
// Create creates a bridge device and returns the interface.
// If the device already exists, returns the existing interface.
func (Bridge) Create(name string, ip net.IP, subnet *net.IPNet) (intf *net.Interface, err error) {
netlinkMu.Lock()
defer netlinkMu.Unlock()
if intf, _ := net.InterfaceByName(name); intf != nil {
return intf, nil
}
link := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: name}}
if err := netlink.LinkAdd(link); err != nil && err.Error() != "file exists" {
return nil, fmt.Errorf("devices: create bridge: %v", err)
}
hAddr, _ := net.ParseMAC(randMacAddr())
err = netlink.LinkSetHardwareAddr(link, hAddr)
if err != nil {
return nil, fmt.Errorf("devices: set hardware address: %v", err)
}
if intf, err = net.InterfaceByName(name); err != nil {
return nil, fmt.Errorf("devices: look up created bridge interface: %v", err)
}
addr := &netlink.Addr{IPNet: &net.IPNet{IP: ip, Mask: subnet.Mask}}
if err = netlink.AddrAdd(link, addr); err != nil && err.Error() != "file exists" {
return nil, fmt.Errorf("devices: add IP to bridge: %v", err)
}
return intf, nil
}