Skip to content

Commit

Permalink
Merge pull request #525 from royyi8/host_intf_oper_status
Browse files Browse the repository at this point in the history
Send request to set oper status state for host interfaces.
  • Loading branch information
DanG100 authored Jan 10, 2025
2 parents 2eef855 + 33318fc commit 0ca7407
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
2 changes: 1 addition & 1 deletion dataplane/dplanerc/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ func (ni *Reconciler) setupPorts(ctx context.Context) error {
Type: saipb.HostifType_HOSTIF_TYPE_NETDEV.Enum(),
ObjId: &portResp.Oid,
Name: []byte(hostifName),
OperStatus: proto.Bool(true),
OperStatus: proto.Bool(false),
})
if err != nil {
return fmt.Errorf("failed to create host interface %q: %w", hostifName, err)
Expand Down
35 changes: 30 additions & 5 deletions dataplane/saiserver/hostif.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"

"github.com/openconfig/lemming/dataplane/dplaneopts"
"github.com/openconfig/lemming/dataplane/forwarding/fwdconfig"
Expand Down Expand Up @@ -89,6 +88,18 @@ func (hostif *hostif) CreateHostif(ctx context.Context, req *saipb.CreateHostifR
Create: true,
DataplanePort: req.GetObjId(),
PortId: id,
Op: pktiopb.PortOperation_PORT_OPERATION_CREATE,
}

operStatus := pktiopb.PortOperation_PORT_OPERATION_SET_DOWN

if req.GetOperStatus() {
operStatus = pktiopb.PortOperation_PORT_OPERATION_SET_UP
}

operReq := &pktiopb.HostPortControlMessage{
PortId: id,
Op: operStatus,
}

switch req.GetType() {
Expand Down Expand Up @@ -157,11 +168,12 @@ func (hostif *hostif) CreateHostif(ctx context.Context, req *saipb.CreateHostifR
if err := hostif.remotePortReq(ctlReq); err != nil {
return nil, err
}

attr := &saipb.HostifAttribute{
OperStatus: proto.Bool(true),
slog.Info("hostif created", "oid", id)
if err := hostif.remotePortReq(operReq); err != nil {
return nil, err
}
hostif.mgr.StoreAttributes(id, attr)

slog.Info("hostif status set", "oid", id, "op", operStatus)
hostif.remoteHostifs[id] = ctlReq

return &saipb.CreateHostifResponse{Oid: id}, nil
Expand Down Expand Up @@ -211,6 +223,19 @@ func (hostif *hostif) RemoveHostif(ctx context.Context, req *saipb.RemoveHostifR

// SetHostifAttribute sets the attributes in the request.
func (hostif *hostif) SetHostifAttribute(ctx context.Context, req *saipb.SetHostifAttributeRequest) (*saipb.SetHostifAttributeResponse, error) {
if req.OperStatus != nil {
op := pktiopb.PortOperation_PORT_OPERATION_SET_DOWN

if req.GetOperStatus() {
op = pktiopb.PortOperation_PORT_OPERATION_SET_UP
}
if err := hostif.remotePortReq(&pktiopb.HostPortControlMessage{
PortId: req.GetOid(),
Op: op,
}); err != nil {
return nil, err
}
}
return nil, nil
}

Expand Down
22 changes: 13 additions & 9 deletions dataplane/saiserver/hostif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ func TestCreateHostif(t *testing.T) {
Oid: 3,
},
wantAttr: &saipb.HostifAttribute{
Type: saipb.HostifType_HOSTIF_TYPE_NETDEV.Enum(),
ObjId: proto.Uint64(2),
OperStatus: proto.Bool(true),
Type: saipb.HostifType_HOSTIF_TYPE_NETDEV.Enum(),
ObjId: proto.Uint64(2),
},
}, {
desc: "success cpu port",
Expand All @@ -73,9 +72,8 @@ func TestCreateHostif(t *testing.T) {
Oid: 3,
},
wantAttr: &saipb.HostifAttribute{
Type: saipb.HostifType_HOSTIF_TYPE_NETDEV.Enum(),
ObjId: proto.Uint64(10),
OperStatus: proto.Bool(true),
Type: saipb.HostifType_HOSTIF_TYPE_NETDEV.Enum(),
ObjId: proto.Uint64(10),
},
}}
for _, tt := range tests {
Expand All @@ -98,7 +96,7 @@ func TestCreateHostif(t *testing.T) {

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
msgCh := make(chan *pktiopb.HostPortControlMessage, 1)
msgCh := make(chan *pktiopb.HostPortControlMessage, 2)
pc, err := c.HostPortControl(ctx)
if err != nil {
t.Fatal(err)
Expand All @@ -107,7 +105,7 @@ func TestCreateHostif(t *testing.T) {
t.Fatal(err)
}
time.Sleep(time.Millisecond)
go func() {
processRequest := func() {
msg, _ := pc.Recv()
msgCh <- msg
pc.Send(&pktiopb.HostPortControlRequest{
Expand All @@ -118,7 +116,10 @@ func TestCreateHostif(t *testing.T) {
},
},
})
}()
}

go processRequest()
go processRequest()

defer stopFn()
got, gotErr := c.CreateHostif(context.TODO(), tt.req)
Expand All @@ -131,7 +132,10 @@ func TestCreateHostif(t *testing.T) {
if d := cmp.Diff(got, tt.want, protocmp.Transform()); d != "" {
t.Errorf("CreateHostif() failed: diff(-got,+want)\n:%s", d)
}

_ = <-msgCh
_ = <-msgCh

attr := &saipb.HostifAttribute{}
if err := mgr.PopulateAllAttributes("3", attr); err != nil {
t.Fatal(err)
Expand Down
8 changes: 4 additions & 4 deletions dataplane/standalone/pkthandler/pktiohandler/pktiohandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ func (m *PacketIOMgr) ManagePorts(c pktiopb.PacketIO_HostPortControlClient) erro
return err
}
log.Infof("received port control message: %+v", resp)
switch {
case (resp.Op == pktiopb.PortOperation_PORT_OPERATION_UNSPECIFIED && resp.Create) || resp.Op == pktiopb.PortOperation_PORT_OPERATION_CREATE:
switch resp.Op {
case pktiopb.PortOperation_PORT_OPERATION_CREATE:
st := &status.Status{
Code: int32(codes.OK),
}
Expand All @@ -181,7 +181,7 @@ func (m *PacketIOMgr) ManagePorts(c pktiopb.PacketIO_HostPortControlClient) erro
if err := m.writePorts(); err != nil {
log.Warningf("failed to write file: %v", err)
}
case resp.Op == pktiopb.PortOperation_PORT_OPERATION_UNSPECIFIED || resp.Op == pktiopb.PortOperation_PORT_OPERATION_DELETE:
case pktiopb.PortOperation_PORT_OPERATION_DELETE:
p, ok := m.hostifs[resp.GetPortId()]
if !ok {
sendErr := c.Send(&pktiopb.HostPortControlRequest{Msg: &pktiopb.HostPortControlRequest_Status{
Expand Down Expand Up @@ -219,7 +219,7 @@ func (m *PacketIOMgr) ManagePorts(c pktiopb.PacketIO_HostPortControlClient) erro
if err := m.writePorts(); err != nil {
log.Warningf("failed to write file: %v", err)
}
case resp.Op == pktiopb.PortOperation_PORT_OPERATION_SET_UP || resp.Op == pktiopb.PortOperation_PORT_OPERATION_SET_DOWN:
case pktiopb.PortOperation_PORT_OPERATION_SET_UP, pktiopb.PortOperation_PORT_OPERATION_SET_DOWN:
p, ok := m.hostifs[resp.GetPortId()]
if !ok {
sendErr := c.Send(&pktiopb.HostPortControlRequest{Msg: &pktiopb.HostPortControlRequest_Status{
Expand Down

0 comments on commit 0ca7407

Please sign in to comment.