Skip to content

Commit

Permalink
Properly set MTU on AF_PACKET interfaces (#115)
Browse files Browse the repository at this point in the history
Signed-off-by: Ed Warnicke <hagbard@gmail.com>
  • Loading branch information
edwarnicke authored Apr 12, 2021
1 parent 1a4ec43 commit d25e1d0
Showing 1 changed file with 48 additions and 17 deletions.
65 changes: 48 additions & 17 deletions internal/vppinit/vppinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,22 @@ func LinkToAfPacket(ctx context.Context, vppConn api.Connection, tunnelIP net.IP
if link == nil {
return tunnelIP, nil
}
afPacketCreate := &af_packet.AfPacketCreate{
HwAddr: types.ToVppMacAddress(&link.Attrs().HardwareAddr),
HostIfName: link.Attrs().Name,
}
now := time.Now()
afPacketCreateRsp, err := af_packet.NewServiceClient(vppConn).AfPacketCreate(ctx, afPacketCreate)

swIfIndex, err := createAfPacket(ctx, vppConn, link)
if err != nil {
return nil, err
}
log.FromContext(ctx).
WithField("swIfIndex", afPacketCreateRsp.SwIfIndex).
WithField("duration", time.Since(now)).
WithField("vppapi", "AfPacketCreate").Debug("completed")

now = time.Now()
now := time.Now()
_, err = interfaces.NewServiceClient(vppConn).SwInterfaceSetFlags(ctx, &interfaces.SwInterfaceSetFlags{
SwIfIndex: afPacketCreateRsp.SwIfIndex,
SwIfIndex: swIfIndex,
Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
})
if err != nil {
return nil, err
}
log.FromContext(ctx).
WithField("swIfIndex", afPacketCreateRsp.SwIfIndex).
WithField("swIfIndex", swIfIndex).
WithField("duration", time.Since(now)).
WithField("vppapi", "SwInterfaceSetFlags").Debug("completed")

Expand All @@ -91,15 +83,15 @@ func LinkToAfPacket(ctx context.Context, vppConn api.Connection, tunnelIP net.IP
if addr.IPNet != nil && addr.IPNet.IP.Equal(tunnelIP) {
now = time.Now()
_, err = interfaces.NewServiceClient(vppConn).SwInterfaceAddDelAddress(ctx, &interfaces.SwInterfaceAddDelAddress{
SwIfIndex: afPacketCreateRsp.SwIfIndex,
SwIfIndex: swIfIndex,
IsAdd: true,
Prefix: types.ToVppAddressWithPrefix(addr.IPNet),
})
if err != nil {
return nil, err
}
log.FromContext(ctx).
WithField("swIfIndex", afPacketCreateRsp.SwIfIndex).
WithField("swIfIndex", swIfIndex).
WithField("prefix", addr.IPNet).
WithField("isAdd", true).
WithField("duration", time.Since(now)).
Expand All @@ -113,7 +105,7 @@ func LinkToAfPacket(ctx context.Context, vppConn api.Connection, tunnelIP net.IP
NPaths: 1,
Paths: []fib_types.FibPath{
{
SwIfIndex: uint32(afPacketCreateRsp.SwIfIndex),
SwIfIndex: uint32(swIfIndex),
TableID: 0,
RpfID: 0,
Weight: 1,
Expand All @@ -135,7 +127,7 @@ func LinkToAfPacket(ctx context.Context, vppConn api.Connection, tunnelIP net.IP
return nil, err
}
log.FromContext(ctx).
WithField("swIfIndex", afPacketCreateRsp.SwIfIndex).
WithField("swIfIndex", swIfIndex).
WithField("prefix", ipRouteAddDel.Route.Prefix).
WithField("isAdd", true).
WithField("duration", time.Since(now)).
Expand All @@ -144,6 +136,45 @@ func LinkToAfPacket(ctx context.Context, vppConn api.Connection, tunnelIP net.IP
return tunnelIP, nil
}

func createAfPacket(ctx context.Context, vppConn api.Connection, link netlink.Link) (interface_types.InterfaceIndex, error) {
afPacketCreate := &af_packet.AfPacketCreate{
HwAddr: types.ToVppMacAddress(&link.Attrs().HardwareAddr),
HostIfName: link.Attrs().Name,
}
now := time.Now()
afPacketCreateRsp, err := af_packet.NewServiceClient(vppConn).AfPacketCreate(ctx, afPacketCreate)
if err != nil {
return 0, err
}
log.FromContext(ctx).
WithField("swIfIndex", afPacketCreateRsp.SwIfIndex).
WithField("duration", time.Since(now)).
WithField("vppapi", "AfPacketCreate").Debug("completed")

if err := setMtu(ctx, vppConn, link, afPacketCreateRsp.SwIfIndex); err != nil {
return 0, err
}
return afPacketCreateRsp.SwIfIndex, nil
}

func setMtu(ctx context.Context, vppConn api.Connection, link netlink.Link, swIfIndex interface_types.InterfaceIndex) error {
now := time.Now()
setMtu := &interfaces.HwInterfaceSetMtu{
SwIfIndex: swIfIndex,
Mtu: uint16(link.Attrs().MTU),
}
_, err := interfaces.NewServiceClient(vppConn).HwInterfaceSetMtu(ctx, setMtu)
if err != nil {
return err
}
log.FromContext(ctx).
WithField("swIfIndex", setMtu.SwIfIndex).
WithField("MTU", setMtu.Mtu).
WithField("duration", time.Since(now)).
WithField("vppapi", "HwInterfaceSetMtu").Debug("completed")
return nil
}

func linkAddrsRoutes(ctx context.Context, tunnelIP net.IP) (netlink.Link, []netlink.Addr, []netlink.Route, error) {
link, err := linkByIP(ctx, tunnelIP)
if err != nil {
Expand Down

0 comments on commit d25e1d0

Please sign in to comment.