forked from jsimonetti/rtnetlink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Driver Interface for Enhanced Data Encoding
This commit implements significant changes by introducing a Driver interface, fundamentally altering the LinkInfo struct. The Data and SlaveData fields are replaced in a non-backward-compatible manner to support driver-specific data encoding. This update addresses the previous limitation where these fields were only byte slices, lacking the capability for detailed data encoding. LinkNetkit is implemented as a Driver interface. During the decoding process, data identified as "netkit" is now explicitly converted into a LinkNetkit struct. Conversely, data not matching recognized types is assigned to a LinkUndefined struct. Important: This update introduces breaking changes and will require a major version update of the library. Signed-off-by: Birol Bilgin <[email protected]>
- Loading branch information
Showing
5 changed files
with
157 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package rtnetlink | ||
|
||
import ( | ||
"github.com/jsimonetti/rtnetlink/internal/unix" | ||
"github.com/mdlayher/netlink" | ||
) | ||
|
||
type Driver interface { | ||
encode(*netlink.AttributeEncoder) error | ||
decode(*netlink.AttributeDecoder) error | ||
rtDriver() | ||
} | ||
|
||
func getDriver(kind string) Driver { | ||
switch kind { | ||
case "netkit": | ||
return &LinkNetkit{} | ||
default: | ||
return &LinkData{} | ||
} | ||
} | ||
|
||
// LinkNetkit holds netkit link specific information | ||
type LinkNetkit struct { | ||
Mode uint32 | ||
Policy int32 | ||
PeerPolicy int32 | ||
Primary bool | ||
PeerInfo *LinkMessage | ||
} | ||
|
||
var _ Driver = &LinkNetkit{} | ||
|
||
func (n *LinkNetkit) decode(ad *netlink.AttributeDecoder) error { | ||
for ad.Next() { | ||
switch ad.Type() { | ||
case unix.IFLA_NETKIT_MODE: | ||
n.Mode = ad.Uint32() | ||
case unix.IFLA_NETKIT_POLICY: | ||
n.Policy = ad.Int32() | ||
case unix.IFLA_NETKIT_PEER_POLICY: | ||
n.PeerPolicy = ad.Int32() | ||
case unix.IFLA_NETKIT_PRIMARY: | ||
n.Primary = ad.Uint8() != 0 | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (n *LinkNetkit) encode(ae *netlink.AttributeEncoder) error { | ||
ae.Uint32(unix.IFLA_NETKIT_MODE, n.Mode) | ||
ae.Int32(unix.IFLA_NETKIT_POLICY, n.Policy) | ||
ae.Int32(unix.IFLA_NETKIT_PEER_POLICY, n.PeerPolicy) | ||
if n.PeerInfo != nil { | ||
n.PeerInfo.Family = unix.AF_UNSPEC | ||
// we need to set the netkit.peerInfo.Flags and .Change according to primary link | ||
// however we do not have this information here | ||
b, err := n.PeerInfo.MarshalBinary() | ||
if err != nil { | ||
return err | ||
} | ||
ae.Bytes(unix.IFLA_NETKIT_PEER_INFO, b) | ||
} | ||
return nil | ||
} | ||
|
||
// rtDriver is an empty method to sattisfy the Driver interface. | ||
func (n *LinkNetkit) rtDriver() {} | ||
|
||
// LinkData is retured when the Driver kind for the LinkInfo Data is not defined | ||
type LinkData struct { | ||
Data []byte | ||
} | ||
|
||
var _ Driver = &LinkData{} | ||
|
||
// dencode is an empty method to sattisfy the Driver interface. | ||
func (u *LinkData) decode(ad *netlink.AttributeDecoder) error { | ||
return nil | ||
} | ||
|
||
// encode is an empty method to sattisfy the Driver interface. | ||
func (u *LinkData) encode(ae *netlink.AttributeEncoder) error { | ||
return nil | ||
} | ||
|
||
// rtDriver is an empty method to sattisfy the Driver interface. | ||
func (u *LinkData) rtDriver() {} |
This file contains 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