Skip to content

Commit

Permalink
Migrate saiserver to slog
Browse files Browse the repository at this point in the history
  • Loading branch information
DanG100 committed Sep 16, 2024
1 parent cf33039 commit 17e47c3
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 34 deletions.
8 changes: 4 additions & 4 deletions dataplane/saiserver/attrmgr/attrmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ package attrmgr
import (
"context"
"fmt"
"log/slog"
"reflect"
"strings"
"sync"
"sync/atomic"

log "github.com/golang/glog"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -93,7 +93,7 @@ func InvokeAndSave[T proto.Message, S proto.Message](ctx context.Context, mgr *A
}
id, err := mgr.getID(req, respMsg)
if err != nil {
log.Warningf("failed to get id %v", err)
slog.WarnContext(ctx, "failed to get id %v", err)

Check failure on line 96 in dataplane/saiserver/attrmgr/attrmgr.go

View workflow job for this annotation

GitHub Actions / lint

slog: slog.WarnContext arg "err" should be a string or a slog.Attr (possible missing key or value) (govet)
return respMsg.(S), nil
}
mgr.storeAttributes(id, req)
Expand Down Expand Up @@ -124,7 +124,7 @@ func (mgr *AttrMgr) Interceptor(ctx context.Context, req any, info *grpc.UnarySe
case strings.Contains(info.FullMethod, "Create") || strings.Contains(info.FullMethod, "Set"):
id, err := mgr.getID(reqMsg, respMsg)
if err != nil {
log.Warningf("failed to get id %v", err)
slog.WarnContext(ctx, "failed to get id %v", err)

Check failure on line 127 in dataplane/saiserver/attrmgr/attrmgr.go

View workflow job for this annotation

GitHub Actions / lint

slog: slog.WarnContext arg "err" should be a string or a slog.Attr (possible missing key or value) (govet)
return respMsg, nil
}
mgr.storeAttributes(id, reqMsg)
Expand All @@ -135,7 +135,7 @@ func (mgr *AttrMgr) Interceptor(ctx context.Context, req any, info *grpc.UnarySe
case strings.Contains(info.FullMethod, "Remove"):
id, err := mgr.getID(reqMsg, respMsg)
if err != nil {
log.Warningf("failed to get id %v", err)
slog.WarnContext(ctx, "failed to get id %v", err)
return respMsg, nil
}
if err := deleteOID(mgr, id); err != nil {
Expand Down
19 changes: 10 additions & 9 deletions dataplane/saiserver/hostif.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package saiserver
import (
"context"
"fmt"
"log/slog"
"strings"
"sync"

Expand Down Expand Up @@ -66,7 +67,7 @@ type hostif struct {
}

func (hostif *hostif) Reset() {
log.Info("resetting hostif")
slog.Info("resetting hostif")
for _, closeFn := range hostif.remoteClosers {
closeFn()
}
Expand Down Expand Up @@ -408,25 +409,25 @@ func (hostif *hostif) CPUPacketStream(srv pktiopb.PacketIO_CPUPacketStreamServer
err = hostif.dataplane.InjectPacket(&fwdpb.ContextId{Id: hostif.dataplane.ID()}, &fwdpb.PortId{ObjectId: &fwdpb.ObjectId{Id: cpuPortID}}, fwdpb.PacketHeaderId_PACKET_HEADER_ID_ETHERNET,
pkt.GetPacket().GetFrame(), acts, true, fwdpb.PortAction_PORT_ACTION_INPUT)
if err != nil {
log.Warningf("inject err: %v", err)
slog.WarnContext(ctx, "inject err: %v", err)

Check failure on line 412 in dataplane/saiserver/hostif.go

View workflow job for this annotation

GitHub Actions / lint

slog: slog.WarnContext arg "err" should be a string or a slog.Attr (possible missing key or value) (govet)
continue
}
}
}
}

func (hostif *hostif) HostPortControl(srv pktiopb.PacketIO_HostPortControlServer) error {
log.Info("started host port control channel")
slog.InfoContext(srv.Context(), "started host port control channel")
_, err := srv.Recv()
if err != nil {
return err
}
log.Info("received init port control channel")
slog.InfoContext(srv.Context(), "received init port control channel")

hostif.remoteMu.Lock()
ctx, cancelFn := context.WithCancel(srv.Context())
hostif.remoteClosers = append(hostif.remoteClosers, func() {
log.Info("canceling host port control")
slog.InfoContext(srv.Context(), "canceling host port control")
cancelFn()
})

Expand Down Expand Up @@ -454,20 +455,20 @@ func (hostif *hostif) HostPortControl(srv pktiopb.PacketIO_HostPortControlServer
}
hostif.remoteMu.Unlock()

log.Info("initialized host port control channel")
slog.InfoContext(srv.Context(), "initialized host port control channel")

// The HostPortControls exits in two cases: context cancels or RPC errors.
err = nil
select {
case <-ctx.Done():
log.Info("host port control done")
slog.InfoContext(srv.Context(), "host port control done")
case err = <-errCh:
log.Info("host port control err: %v", err)
slog.InfoContext(srv.Context(), "host port control err: %v", err)

Check failure on line 466 in dataplane/saiserver/hostif.go

View workflow job for this annotation

GitHub Actions / lint

slog: slog.InfoContext arg "err" should be a string or a slog.Attr (possible missing key or value) (govet)
}

hostif.remoteMu.Lock()
hostif.remotePortReq = nil
hostif.remoteMu.Unlock()
log.Info("cleared host port control channel")
slog.InfoContext(srv.Context(), "cleared host port control channel")
return err
}
9 changes: 4 additions & 5 deletions dataplane/saiserver/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package saiserver
import (
"context"
"fmt"
"log/slog"
"net"

"google.golang.org/grpc"
Expand All @@ -26,8 +27,6 @@ import (
"github.com/openconfig/lemming/dataplane/forwarding/fwdconfig"
"github.com/openconfig/lemming/dataplane/saiserver/attrmgr"

log "github.com/golang/glog"

saipb "github.com/openconfig/lemming/dataplane/proto/sai"
fwdpb "github.com/openconfig/lemming/proto/forwarding"
)
Expand Down Expand Up @@ -281,7 +280,7 @@ func (port *port) CreatePort(ctx context.Context, req *saipb.CreatePortRequest)
}
fwdPort.Port.PortType = port.opts.PortType

log.Infof("created port %v, dev %v with lanes %v", id, dev, req.GetHwLaneList())
slog.InfoContext(ctx, "created port", "port", id, "device", dev, "hwlane", req.GetHwLaneList())
_, err := port.dataplane.PortCreate(ctx, fwdPort)
if err != nil {
return nil, err
Expand Down Expand Up @@ -324,7 +323,7 @@ func (port *port) CreatePort(ctx context.Context, req *saipb.CreatePortRequest)
ObjectId: &fwdpb.ObjectId{Id: fmt.Sprint(id)},
})
if err != nil {
log.Infof("Failed to find NID for port id=%d: %v", id, err)
slog.InfoContext(ctx, "Failed to find NID for port", "port", id, "err", err)
return nil, err
}
vlanReq := fwdconfig.TableEntryAddRequest(port.dataplane.ID(), VlanTable).AppendEntry(
Expand Down Expand Up @@ -568,7 +567,7 @@ func (port *port) RemovePort(ctx context.Context, req *saipb.RemovePortRequest)
}

func (port *port) Reset() {
log.Info("reseting port")
slog.Info("reseting port")
}

type lagMember struct {
Expand Down
11 changes: 5 additions & 6 deletions dataplane/saiserver/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"encoding/binary"
"fmt"
"log/slog"
"sync"

"google.golang.org/grpc"
Expand All @@ -30,8 +31,6 @@ import (
"github.com/openconfig/lemming/dataplane/forwarding/fwdconfig"
"github.com/openconfig/lemming/dataplane/saiserver/attrmgr"

log "github.com/golang/glog"

saipb "github.com/openconfig/lemming/dataplane/proto/sai"
fwdpb "github.com/openconfig/lemming/proto/forwarding"
)
Expand Down Expand Up @@ -674,7 +673,7 @@ func (ri *routerInterface) CreateRouterInterface(ctx context.Context, req *saipb
switch req.GetType() {
case saipb.RouterInterfaceType_ROUTER_INTERFACE_TYPE_PORT:
case saipb.RouterInterfaceType_ROUTER_INTERFACE_TYPE_LOOPBACK: // TODO: Support loopback interfaces
log.Warning("loopback interfaces not supported")
slog.WarnContext(ctx, "loopback interfaces not supported")
return &saipb.CreateRouterInterfaceResponse{Oid: id}, nil
default:
return nil, status.Errorf(codes.InvalidArgument, "unknown interface type: %v", req.GetType())
Expand Down Expand Up @@ -970,7 +969,7 @@ func (vlan *vlan) CreateVlanMember(ctx context.Context, r *saipb.CreateVlanMembe
ObjectId: &fwdpb.ObjectId{Id: fmt.Sprint(r.GetBridgePortId())},
})
if err != nil {
log.Infof("Failed to find NID for port id=%d: %v", r.GetBridgePortId(), err)
slog.InfoContext(ctx, "Failed to find NID for port", "bridge_port", r.GetBridgePortId(), "err", err)
return nil, err
}
vlanReq := fwdconfig.TableEntryAddRequest(vlan.dataplane.ID(), VlanTable).AppendEntry(
Expand Down Expand Up @@ -1026,7 +1025,7 @@ func (vlan *vlan) RemoveVlanMember(ctx context.Context, r *saipb.RemoveVlanMembe
ObjectId: &fwdpb.ObjectId{Id: fmt.Sprint(member.PortID)},
})
if err != nil {
log.Infof("Failed to find NID for port id=%d: %v", member.PortID, err)
slog.InfoContext(ctx, "Failed to find NID for port", "bridge_port", member.PortID, "err", err)
return nil, err
}
if _, err := vlan.dataplane.TableEntryRemove(ctx, fwdconfig.TableEntryRemoveRequest(vlan.dataplane.ID(), VlanTable).AppendEntry(
Expand All @@ -1049,7 +1048,7 @@ func (vlan *vlan) CreateVlanMembers(ctx context.Context, r *saipb.CreateVlanMemb
}

func (vlan *vlan) Reset() {
log.Info("resetting vlan")
slog.Info("resetting vlan")
vlan.oidByVId = map[uint32]uint64{}
vlan.vlans = map[uint64]map[uint64]*vlanMember{}
}
Expand Down
9 changes: 4 additions & 5 deletions dataplane/saiserver/saiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ package saiserver
import (
"context"
"fmt"
"log/slog"

"github.com/openconfig/lemming/dataplane/dplaneopts"
"github.com/openconfig/lemming/dataplane/forwarding"
"github.com/openconfig/lemming/dataplane/saiserver/attrmgr"

"google.golang.org/grpc"

log "github.com/golang/glog"

saipb "github.com/openconfig/lemming/dataplane/proto/sai"
fwdpb "github.com/openconfig/lemming/proto/forwarding"
)
Expand Down Expand Up @@ -136,10 +135,10 @@ type Server struct {
tam *tam
}

func (s *Server) ObjectTypeQuery(_ context.Context, req *saipb.ObjectTypeQueryRequest) (*saipb.ObjectTypeQueryResponse, error) {
func (s *Server) ObjectTypeQuery(ctx context.Context, req *saipb.ObjectTypeQueryRequest) (*saipb.ObjectTypeQueryResponse, error) {
val := s.mgr.GetType(fmt.Sprint(req.GetObject()))
if val == saipb.ObjectType_OBJECT_TYPE_NULL {
log.Warningf("unknown object id %v, type %v", req.Object, val)
slog.WarnContext(ctx, "unknown object id", "oid", req.Object)
}
return &saipb.ObjectTypeQueryResponse{
Type: val,
Expand All @@ -148,7 +147,7 @@ func (s *Server) ObjectTypeQuery(_ context.Context, req *saipb.ObjectTypeQueryRe

func (s *Server) Initialize(ctx context.Context, _ *saipb.InitializeRequest) (*saipb.InitializeResponse, error) {
if s.initialized {
log.Info("dataplane already intialized, reseting")
slog.InfoContext(ctx, "dataplane already intialized, reseting")
s.mgr.Reset()
s.saiSwitch.Reset()
if err := s.Reset(ctx); err != nil {
Expand Down
9 changes: 4 additions & 5 deletions dataplane/saiserver/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package saiserver
import (
"context"
"fmt"
"log/slog"
"net"
"strconv"

Expand All @@ -25,8 +26,6 @@ import (
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"

log "github.com/golang/glog"

"github.com/openconfig/lemming/dataplane/dplaneopts"
"github.com/openconfig/lemming/dataplane/forwarding/fwdconfig"
"github.com/openconfig/lemming/dataplane/forwarding/infra/fwdcontext"
Expand Down Expand Up @@ -969,7 +968,7 @@ func (sw *saiSwitch) PortStateChangeNotification(_ *saipb.PortStateChangeNotific
case ed := <-fwdSrv.ch:
num, err := strconv.Atoi(ed.GetPort().GetPortId().GetObjectId().GetId())
if err != nil {
log.Warningf("couldn't get numeric port id: %v", err)
slog.WarnContext(srv.Context(), "couldn't get numeric port id", "err", err)
continue
}
oType := sw.mgr.GetType(ed.GetPort().GetPortId().GetObjectId().GetId())
Expand All @@ -978,7 +977,7 @@ func (sw *saiSwitch) PortStateChangeNotification(_ *saipb.PortStateChangeNotific
case saipb.ObjectType_OBJECT_TYPE_BRIDGE_PORT:
case saipb.ObjectType_OBJECT_TYPE_LAG:
default:
log.Infof("skipping port state event for type %v", oType)
slog.InfoContext(srv.Context(), "skipping port state event", "type", oType)
continue
}
status := saipb.PortOperStatus_PORT_OPER_STATUS_UNKNOWN
Expand All @@ -993,7 +992,7 @@ func (sw *saiSwitch) PortStateChangeNotification(_ *saipb.PortStateChangeNotific
PortState: status,
}},
}
log.Infof("send port event: %+v", resp)
slog.InfoContext(srv.Context(), "send port event", "event", resp)
err = srv.Send(resp)
if err != nil {
return err
Expand Down

0 comments on commit 17e47c3

Please sign in to comment.