Skip to content

Commit 5f641a7

Browse files
solidDoWantvishvananda
authored andcommitted
Add additional functions for reconfiguring VXLAN interfaces after creation
Signed-off-by: solidDoWant <[email protected]>
1 parent b851498 commit 5f641a7

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

link_linux.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,83 @@ func (h *Handle) LinkSetGROIPv4MaxSize(link Link, maxSize int) error {
11811181
return err
11821182
}
11831183

1184+
// LinkSetVXLANGroup sets the group address of a VXLAN link device.
1185+
// Equivalent to: `ip link set $link type vxlan { remote | group } $group`
1186+
// Note: netlink does not support changing the group address family. If attempted, this will return an EOPNOTSUPP error.
1187+
func LinkSetVXLANGroup(link *Vxlan, group net.IP) error {
1188+
return pkgHandle.LinkSetVXLANGroup(link, group)
1189+
}
1190+
1191+
// LinkSetVXLANGroup sets the group address of a VXLAN link device.
1192+
// Equivalent to: `ip link set $link type vxlan { remote | group } $group [ dev $dev ]`
1193+
// Note: netlink does not support changing the group address family. If attempted, this will return an EOPNOTSUPP error.
1194+
func (h *Handle) LinkSetVXLANGroup(link *Vxlan, group net.IP) error {
1195+
if link.VtepDevIndex == 0 {
1196+
return fmt.Errorf("VTEP device index must be set to change or set the group address")
1197+
}
1198+
1199+
base := link.Attrs()
1200+
h.ensureIndex(base)
1201+
// RTM_SETLINK does not currently support changing the group address
1202+
req := h.newNetlinkRequest(unix.RTM_NEWLINK, unix.NLM_F_ACK)
1203+
1204+
msg := nl.NewIfInfomsg(unix.AF_UNSPEC)
1205+
msg.Index = int32(base.Index)
1206+
req.AddData(msg)
1207+
1208+
linkInfo := nl.NewRtAttr(unix.IFLA_LINKINFO, nil)
1209+
linkInfo.AddRtAttr(nl.IFLA_INFO_KIND, nl.NonZeroTerminated(link.Type()))
1210+
1211+
data := linkInfo.AddRtAttr(nl.IFLA_INFO_DATA, nil)
1212+
1213+
if v4Group := group.To4(); v4Group != nil {
1214+
data.AddRtAttr(nl.IFLA_VXLAN_GROUP, []byte(v4Group))
1215+
} else if v6group := group.To16(); v6group != nil {
1216+
data.AddRtAttr(nl.IFLA_VXLAN_GROUP6, []byte(v6group))
1217+
} else {
1218+
return fmt.Errorf("invalid group address %q", group.String())
1219+
}
1220+
1221+
req.AddData(linkInfo)
1222+
1223+
_, err := req.Execute(unix.NETLINK_ROUTE, 0)
1224+
return err
1225+
}
1226+
1227+
// LinkSetVXLANVtepDevIndex sets the VTEP device index of a VXLAN link device.
1228+
// Equivalent to: `ip link set $link type [ dev $dev ]`
1229+
func LinkSetVXLANVtepDevIndex(link *Vxlan, ifIndex int) error {
1230+
return pkgHandle.LinkSetVXLANVtepDevIndex(link, ifIndex)
1231+
}
1232+
1233+
// LinkSetVXLANVtepDevIndex sets the VTEP device index of a VXLAN link device.
1234+
// Equivalent to: `ip link set $link type [ dev $dev ]`
1235+
func (h *Handle) LinkSetVXLANVtepDevIndex(link *Vxlan, ifIndex int) error {
1236+
if link.Group != nil && ifIndex == 0 {
1237+
return fmt.Errorf("VTEP device index must be set when group address is set")
1238+
}
1239+
1240+
base := link.Attrs()
1241+
h.ensureIndex(base)
1242+
// RTM_SETLINK does not currently support changing the the VTEP device index
1243+
req := h.newNetlinkRequest(unix.RTM_NEWLINK, unix.NLM_F_ACK)
1244+
1245+
msg := nl.NewIfInfomsg(unix.AF_UNSPEC)
1246+
msg.Index = int32(base.Index)
1247+
req.AddData(msg)
1248+
1249+
linkInfo := nl.NewRtAttr(unix.IFLA_LINKINFO, nil)
1250+
linkInfo.AddRtAttr(nl.IFLA_INFO_KIND, nl.NonZeroTerminated(link.Type()))
1251+
1252+
data := linkInfo.AddRtAttr(nl.IFLA_INFO_DATA, nil)
1253+
data.AddRtAttr(nl.IFLA_VXLAN_LINK, nl.Uint32Attr(uint32(ifIndex)))
1254+
1255+
req.AddData(linkInfo)
1256+
1257+
_, err := req.Execute(unix.NETLINK_ROUTE, 0)
1258+
return err
1259+
}
1260+
11841261
func boolAttr(val bool) []byte {
11851262
var v uint8
11861263
if val {

0 commit comments

Comments
 (0)