-
-
Notifications
You must be signed in to change notification settings - Fork 297
WIP: use netlink instead of calling iproute2 commands #1990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
gwenya
wants to merge
22
commits into
lxc:main
Choose a base branch
from
gwenya:netlink
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
5343bef
incusd/ip/utils: Switch to netlink
gwenya f981481
incusd/ip/addr: Switch to netlink
gwenya e178d2a
incusd/ip/class: Switch to netlink
gwenya ab64cea
incusd/ip/filter: Switch to netlink
gwenya ff84cd8
incusd/ip/link: Switch to netlink (incomplete)
gwenya d8cc640
incusd/ip/neigh: Switch to netlink
gwenya 6a94358
incusd/ip/neigh_proxy: Switch to netlink
gwenya d3c2e07
incusd/ip/qdisc: Switch to netlink
gwenya 70a007e
incusd/ip/route: Switch to netlink
gwenya 12e8d1e
incusd/ip/tuntap: Switch to netlink
gwenya dabad91
incusd/ip/vdpa: Switch to vishvananda/netlink library instead of doin…
gwenya 37dccfc
incusd/ip: Refactor family from string to `Family` type
gwenya 2ed3317
incusd/ip/filter: Refactor Rate, Burst and Mtu from strings to integers
gwenya 7ffc44d
incusd/ip/filter: Refactor Mask and Value from strings to integers
gwenya eafa5c6
incusd/ip/link_vxlan: Refactor VxLanID, DstPort and TTL from string t…
gwenya dbea830
incusd/ip/neigh: Refactor NeighbourIPState from string to int
gwenya a01c26c
incusd/ip/qdisc_htb: Refactor Default from string to int
gwenya 828bb69
incusd/ip/vdpa: Remove unused
gwenya 7031eeb
incusd/ip: Merge GetLinkInfoByName and LinkFromName into LinkByName
gwenya 924191d
incusd/ip/link: Refactor SetVfSpoofchk mode parameter to bool
gwenya 45bed3e
fixup! incusd/ip/link: Switch to netlink (incomplete)
gwenya d3207fe
fixup! incusd/ip/link: Switch to netlink (incomplete)
gwenya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,95 @@ | ||
package ip | ||
|
||
import ( | ||
"github.com/lxc/incus/v6/shared/subprocess" | ||
"fmt" | ||
|
||
"github.com/vishvananda/netlink" | ||
) | ||
|
||
// Addr represents arguments for address protocol manipulation. | ||
type Addr struct { | ||
DevName string | ||
Address string | ||
Scope string | ||
Family string | ||
Family Family | ||
} | ||
|
||
// Add adds new protocol address. | ||
func (a *Addr) Add() error { | ||
_, err := subprocess.RunCommand("ip", a.Family, "addr", "add", "dev", a.DevName, a.Address) | ||
addr, err := netlink.ParseIPNet(a.Address) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
scope, err := a.scopeNum() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = netlink.AddrAdd(&netlink.GenericLink{ | ||
LinkAttrs: netlink.LinkAttrs{ | ||
Name: a.DevName, | ||
}, | ||
}, &netlink.Addr{ | ||
IPNet: addr, | ||
Scope: scope, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to add address %v: %w", addr, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (a *Addr) scopeNum() (int, error) { | ||
var scope netlink.Scope | ||
switch a.Scope { | ||
case "global", "universe", "": | ||
scope = netlink.SCOPE_UNIVERSE | ||
case "site": | ||
scope = netlink.SCOPE_SITE | ||
case "link": | ||
scope = netlink.SCOPE_LINK | ||
case "host": | ||
scope = netlink.SCOPE_HOST | ||
case "nowhere": | ||
scope = netlink.SCOPE_NOWHERE | ||
default: | ||
return 0, fmt.Errorf("unknown address scope %q", a.Scope) | ||
} | ||
|
||
return int(scope), nil | ||
} | ||
|
||
// Flush flushes protocol addresses. | ||
func (a *Addr) Flush() error { | ||
cmd := []string{} | ||
if a.Family != "" { | ||
cmd = append(cmd, a.Family) | ||
link, err := linkByName(a.DevName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cmd = append(cmd, "addr", "flush", "dev", a.DevName) | ||
if a.Scope != "" { | ||
cmd = append(cmd, "scope", a.Scope) | ||
addrs, err := netlink.AddrList(link, int(a.Family)) | ||
if err != nil { | ||
return fmt.Errorf("failed to get addresses for device %s: %w", a.DevName, err) | ||
} | ||
|
||
_, err := subprocess.RunCommand("ip", cmd...) | ||
scope, err := a.scopeNum() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// TODO: iproute2 supposedly does some kind of batch delete in multiple rounds, which is probably more performant than deleting addresses one by one, but the iproute2 code is arcane and uncommented and I haven't yet figured out how it works | ||
|
||
for _, addr := range addrs { | ||
if a.Scope != "" && scope != addr.Scope { | ||
continue | ||
} | ||
|
||
err := netlink.AddrDel(link, &addr) | ||
if err != nil { | ||
return fmt.Errorf("failed to delete address %v: %w", addr, err) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stgraber should I put these kinds of changes into their own commit or leave them together with the corresponding change in the ip package?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's fine to leave them with the change I think