本文整理汇总了Golang中github.com/vishvananda/netlink.AddrAdd函数的典型用法代码示例。如果您正苦于以下问题:Golang AddrAdd函数的具体用法?Golang AddrAdd怎么用?Golang AddrAdd使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了AddrAdd函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
}
开发者ID:DaveDaCoda,项目名称:docker,代码行数:53,代码来源:setup_ipv6.go
示例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)
}
开发者ID:CtrlZvi,项目名称:libnetwork,代码行数:26,代码来源:ipvs_test.go
示例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
}
开发者ID:ably-forks,项目名称:flynn,代码行数:27,代码来源:device.go
示例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
}
开发者ID:TencentSA,项目名称:kubernetes-0.5,代码行数:37,代码来源:sriov.go
示例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
}
开发者ID:tswindell,项目名称:nest-boot,代码行数:32,代码来源:main.go
示例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}
}
开发者ID:0xef53,项目名称:phoenix-guest-agent,代码行数:27,代码来源:network.go
示例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)
}
开发者ID:colebrumley,项目名称:docker,代码行数:7,代码来源:configure_linux.go
示例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
}
开发者ID:cilium-team,项目名称:cilium,代码行数:35,代码来源:cilium-cni.go
示例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
}
开发者ID:sinfomicien,项目名称:rkt,代码行数:28,代码来源:ptp.go
示例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)
}
开发者ID:ailispaw,项目名称:docker,代码行数:7,代码来源:interface_linux.go
示例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
}
开发者ID:luxas,项目名称:flannel,代码行数:34,代码来源:network.go
示例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
}
开发者ID:zachgersh,项目名称:cni,代码行数:33,代码来源:ipam.go
示例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])
}
开发者ID:shiaho,项目名称:netlink_tool,代码行数:34,代码来源:main_linux.go
示例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)
}
}
开发者ID:vdemeester,项目名称:libnetwork,代码行数:27,代码来源:utils_test.go
示例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
}
开发者ID:cloudfoundry,项目名称:guardian,代码行数:32,代码来源:bridge_linux.go
示例16: setupBridgeIPv6
func setupBridgeIPv6(config *networkConfiguration, i *bridgeInterface) error {
// Enable IPv6 on the bridge
procFile := "/proc/sys/net/ipv6/conf/" + config.BridgeName + "/disable_ipv6"
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
return nil
}
开发者ID:souravbh,项目名称:lattice-release,代码行数:25,代码来源:setup_ipv6.go
示例17: routingUp
func routingUp() {
veth, err := netlink.LinkByName("myveth1")
if err != nil {
panic(err)
}
err = netlink.LinkSetUp(veth)
if err != nil {
panic(err)
}
addr, _ := netlink.ParseAddr("172.19.80.2/24")
err = netlink.AddrAdd(veth, addr)
if err != nil {
panic(err)
}
routes := createRoutes(veth)
for _, route := range routes {
fmt.Println("Adding route", route)
err := netlink.RouteAdd(route)
if err != nil {
fmt.Println(err)
// panic(err)
}
}
}
开发者ID:mycroft,项目名称:go-snippets,代码行数:28,代码来源:main.go
示例18: applyNetConf
func applyNetConf(link netlink.Link, netConf config.InterfaceConfig) error {
if netConf.DHCP {
log.Infof("Running DHCP on %s", link.Attrs().Name)
cmd := exec.Command("dhcpcd", "-A4", "-e", "force_hostname=true", link.Attrs().Name)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Error(err)
}
} else if netConf.IPV4LL {
if err := AssignLinkLocalIP(link); err != nil {
log.Error("IPV4LL set failed")
return err
}
} else if netConf.Address == "" {
return nil
} else {
addr, err := netlink.ParseAddr(netConf.Address)
if err != nil {
return err
}
if err := netlink.AddrAdd(link, addr); err != nil {
log.Error("addr add failed")
return err
}
log.Infof("Set %s on %s", netConf.Address, link.Attrs().Name)
}
if netConf.MTU > 0 {
if err := netlink.LinkSetMTU(link, netConf.MTU); err != nil {
log.Error("set MTU Failed")
return err
}
}
if err := netlink.LinkSetUp(link); err != nil {
log.Error("failed to setup link")
return err
}
if netConf.Gateway != "" {
gatewayIp := net.ParseIP(netConf.Gateway)
if gatewayIp == nil {
return errors.New("Invalid gateway address " + netConf.Gateway)
}
route := netlink.Route{
Scope: netlink.SCOPE_UNIVERSE,
Gw: net.ParseIP(netConf.Gateway),
}
if err := netlink.RouteAdd(&route); err != nil {
log.Error("gateway set failed")
return err
}
log.Infof("Set default gateway %s", netConf.Gateway)
}
return nil
}
开发者ID:jgatkinsn,项目名称:os,代码行数:60,代码来源:network.go
示例19: setInterfaceIP
// Set the IP addr of a netlink interface
func (driver *driver) setInterfaceIP(name string, rawIP string) error {
var netlinkRetryTimer time.Duration
netlinkRetryTimer = 2
iface, err := netlink.LinkByName(name)
if err != nil {
log.Debugf("error retrieving new OVS bridge netlink link [ %s ] allowing another [ %d ] seconds for the host to finish creating it..", bridgeName, netlinkRetryTimer)
time.Sleep(netlinkRetryTimer * time.Second)
iface, err = netlink.LinkByName(name)
if err != nil {
log.Debugf("error retrieving new OVS bridge netlink link [ %s ] allowing another [ %d ] seconds for the host to finish creating it..", bridgeName, netlinkRetryTimer)
time.Sleep(netlinkRetryTimer * time.Second)
iface, err = netlink.LinkByName(name)
if err != nil {
log.Fatalf("Abandoning retrieving the new OVS bridge link from netlink, Run [ ip link ] to troubleshoot the error: %s", err)
return err
}
}
}
ipNet, err := netlink.ParseIPNet(rawIP)
if err != nil {
return err
}
addr := &netlink.Addr{ipNet, ""}
return netlink.AddrAdd(iface, addr)
}
开发者ID:m-barthelemy,项目名称:docker-ovs-plugin,代码行数:26,代码来源:utils.go
示例20: setInterfaceIPv6
func setInterfaceIPv6(iface netlink.Link, i *nwIface) error {
if i.AddressIPv6() == nil {
return nil
}
ipAddr := &netlink.Addr{IPNet: i.AddressIPv6(), Label: ""}
return netlink.AddrAdd(iface, ipAddr)
}
开发者ID:DaveDaCoda,项目名称:docker,代码行数:7,代码来源:interface_linux.go
注:本文中的github.com/vishvananda/netlink.AddrAdd函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论