Skip to content
9 changes: 8 additions & 1 deletion dataplane/forwarding/fwdport/ports/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,20 @@ func (kernelBuilder) Build(portDesc *fwdpb.PortDesc, ctx *fwdcontext.Context) (f
return nil, fmt.Errorf("failed to set arp_ignore to 2: %v", err)
}

netlink.LinkSetUp(l)
if err := netlink.LinkSetUp(l); err != nil {
return nil, fmt.Errorf("failed to set link up: %v", err)
}

// Set snaplen to 64KB to capture max size IP packets
handle, err := pcap.OpenLive(kp.Kernel.GetDeviceName(), 65536, true, pcap.BlockForever)
if err != nil {
return nil, fmt.Errorf("failed to create afpacket: %v", err)
}
if !kp.Kernel.GetAdminState() {
if err := netlink.LinkSetDown(l); err != nil {
return nil, fmt.Errorf("failed to set link down: %v", err)
}
}
p := &kernelPort{
ctx: ctx,
handle: handle,
Expand Down
14 changes: 14 additions & 0 deletions dataplane/forwarding/fwdport/ports/tap.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,20 @@ func (tp tapBuilder) Build(portDesc *fwdpb.PortDesc, ctx *fwdcontext.Context) (f
if err := unix.SetNonblock(fd, true); err != nil {
return nil, fmt.Errorf("failed to set tap in non-blocking mode: %v", err)
}
l, err := tp.ifaceMgr.LinkByName(kp.Tap.GetDeviceName())
if err != nil {
return nil, fmt.Errorf("failed to get tap interface %q: %w", kp.Tap.GetDeviceName(), err)
}
isUp := l.Attrs().Flags&net.FlagUp != 0
if kp.Tap.GetAdminState() && !isUp {
Comment thread
sonikajindal marked this conversation as resolved.
Resolved
if err := tp.ifaceMgr.LinkSetUp(l); err != nil {
return nil, fmt.Errorf("failed to set tap interface %q up: %w", kp.Tap.GetDeviceName(), err)
}
} else if !kp.Tap.GetAdminState() && isUp {
if err := tp.ifaceMgr.LinkSetDown(l); err != nil {
return nil, fmt.Errorf("failed to set tap interface %q down: %w", kp.Tap.GetDeviceName(), err)
}
}
file := os.NewFile(uintptr(fd), "/dev/tun")
p := &tapPort{
ctx: ctx,
Expand Down
40 changes: 9 additions & 31 deletions dataplane/saiserver/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (port *port) CreatePort(ctx context.Context, req *saipb.CreatePortRequest)
SupportedFecMode: []saipb.PortFecMode{saipb.PortFecMode_PORT_FEC_MODE_NONE},
NumberOfIngressPriorityGroups: proto.Uint32(0),
QosMaximumHeadroomSize: proto.Uint32(0),
AdminState: proto.Bool(true),
AdminState: proto.Bool(req.GetAdminState()),
AutoNegMode: proto.Bool(req.GetAutoNegMode()),
Mtu: proto.Uint32(1514),
HwLaneList: req.HwLaneList,
Expand All @@ -237,12 +237,11 @@ func (port *port) CreatePort(ctx context.Context, req *saipb.CreatePortRequest)
fwdPort.Port.Port = &fwdpb.PortDesc_Kernel{
Kernel: &fwdpb.KernelPortDesc{
DeviceName: dev,
AdminState: req.GetAdminState(),
},
}
// For ports that don't exist, do not create dataplane ports.
if _, err := getInterface(dev); err != nil {
attrs.OperStatus = saipb.PortOperStatus_PORT_OPER_STATUS_DOWN.Enum()
port.mgr.StoreAttributes(id, attrs)
// TODO: This should be a real error, improve once we a correct config solution.
// For now, create dummy port with no actions so we don't get a bunch error for a nonexistent port.
fwdPort := &fwdpb.PortCreateRequest{
Expand All @@ -255,25 +254,16 @@ func (port *port) CreatePort(ctx context.Context, req *saipb.CreatePortRequest)
Port: &fwdpb.PortDesc_Tap{
Tap: &fwdpb.TAPPortDesc{
DeviceName: fmt.Sprintf("port%d", id),
AdminState: req.GetAdminState(),
},
},
},
}
_, err := port.dataplane.PortCreate(ctx, fwdPort)
if err != nil {
return nil, err
}
stateReq := &fwdpb.PortStateRequest{
ContextId: &fwdpb.ContextId{Id: port.dataplane.ID()},
PortId: &fwdpb.PortId{ObjectId: &fwdpb.ObjectId{Id: fmt.Sprint(id)}},
Operation: &fwdpb.PortInfo{
AdminStatus: fwdpb.PortState_PORT_STATE_DISABLED_DOWN,
},
}
if _, err := port.dataplane.PortState(ctx, stateReq); err != nil {
if _, err := port.dataplane.PortCreate(ctx, fwdPort); err != nil {
return nil, err
}

attrs.OperStatus = saipb.PortOperStatus_PORT_OPER_STATUS_DOWN.Enum()
port.mgr.StoreAttributes(id, attrs)
return &saipb.CreatePortResponse{
Oid: id,
}, nil
Expand Down Expand Up @@ -310,23 +300,11 @@ func (port *port) CreatePort(ctx context.Context, req *saipb.CreatePortRequest)
if _, err := port.dataplane.PortUpdate(ctx, update); err != nil {
return nil, err
}
attrs.OperStatus = saipb.PortOperStatus_PORT_OPER_STATUS_UP.Enum()
if req.AdminState == nil || req.GetAdminState() == false {
stateReq := &fwdpb.PortStateRequest{
ContextId: &fwdpb.ContextId{Id: port.dataplane.ID()},
PortId: &fwdpb.PortId{ObjectId: &fwdpb.ObjectId{Id: fmt.Sprint(id)}},
Operation: &fwdpb.PortInfo{
AdminStatus: fwdpb.PortState_PORT_STATE_DISABLED_DOWN,
},
}
_, err := port.dataplane.PortState(ctx, stateReq)
if err != nil {
return nil, err
}
attrs.AdminState = proto.Bool(false)
if req.GetAdminState() {
attrs.OperStatus = saipb.PortOperStatus_PORT_OPER_STATUS_UP.Enum()
Comment thread
sonikajindal marked this conversation as resolved.
Resolved
} else {
attrs.OperStatus = saipb.PortOperStatus_PORT_OPER_STATUS_DOWN.Enum()
}

port.mgr.StoreAttributes(id, attrs)

// Update the switch's port list.
Expand Down
2 changes: 1 addition & 1 deletion dataplane/saiserver/ports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestCreatePort(t *testing.T) {
SupportedFecMode: []saipb.PortFecMode{saipb.PortFecMode_PORT_FEC_MODE_NONE},
NumberOfIngressPriorityGroups: proto.Uint32(0),
QosMaximumHeadroomSize: proto.Uint32(0),
AdminState: proto.Bool(true),
AdminState: proto.Bool(false),
AutoNegMode: proto.Bool(false),
Mtu: proto.Uint32(1514),
PortVlanId: proto.Uint32(DefaultVlanId),
Expand Down
26 changes: 23 additions & 3 deletions proto/forwarding/forwarding_port.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions proto/forwarding/forwarding_port.proto
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ message CPUPortDesc {

message KernelPortDesc {
string device_name = 1;
bool admin_state = 2;
}

message TAPPortDesc {
string device_name = 1;
bool admin_state = 2;
}

message FakePortDesc {
Expand Down
Loading