Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dataplane/forwarding/fwdport/ports/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ go_library(
"common.go",
"cpu.go",
"doc.go",
"dummy.go",
"fake.go",
"genetlink.go",
"group.go",
"kernel.go",
"tap.go",
Expand Down
4 changes: 0 additions & 4 deletions dataplane/forwarding/fwdport/ports/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,6 @@ func (w *linkStateWatcher) run(updCh <-chan netlink.LinkUpdate, doneCh <-chan st
continue
}
admin, oper := stateFromAttrs(upd.Attrs())
if portType := target.port.Type(); portType == fwdpb.PortType_PORT_TYPE_TAP {
// Dummy ports created by Lucius should not be set to enabled up.
oper = fwdpb.PortState_PORT_STATE_DISABLED_DOWN
}
log.V(1).Infof("dataplane receive link update: port %q, admin %v, oper %v", devName, admin.String(), oper.String())
if err := target.ctx.Notify(&fwdpb.EventDesc{
Event: fwdpb.Event_EVENT_PORT,
Expand Down
117 changes: 117 additions & 0 deletions dataplane/forwarding/fwdport/ports/dummy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ports

import (
"fmt"

"github.com/openconfig/lemming/dataplane/forwarding/fwdaction"
"github.com/openconfig/lemming/dataplane/forwarding/fwdport"
"github.com/openconfig/lemming/dataplane/forwarding/infra/fwdcontext"
"github.com/openconfig/lemming/dataplane/forwarding/infra/fwdobject"
"github.com/openconfig/lemming/dataplane/forwarding/infra/fwdpacket"
fwdpb "github.com/openconfig/lemming/proto/forwarding"
)

func init() {
fwdport.Register(fwdpb.PortType_PORT_TYPE_DUMMY, dummyBuilder{})
}

// dummyPort is a ports that drops all packets written to it. It is used to
// represent a port that is not present in the forwarding plane.
type dummyPort struct {
fwdobject.Base
desc *fwdpb.PortDesc
devName string
adminState fwdpb.PortState
ctx *fwdcontext.Context // Forwarding context containing the port
}

// Desc returns the port description proto.
func (p *dummyPort) Desc() *fwdpb.PortDesc {
return p.desc
}

func (p *dummyPort) String() string {
desc := fmt.Sprintf("Type=%v;DeviceName=%v;", fwdpb.PortType_PORT_TYPE_DUMMY, p.devName)
if state, err := p.State(nil); err == nil {
desc += fmt.Sprintf("<State=%v>;", state)
}
return desc
}

func (p *dummyPort) Type() fwdpb.PortType {
return fwdpb.PortType_PORT_TYPE_DUMMY
}

func (p *dummyPort) Cleanup() {
}

// Update updates the actions of the port.
Comment thread
gavinz-ship-it marked this conversation as resolved.
Resolved
func (p *dummyPort) Update(upd *fwdpb.PortUpdateDesc) error {
defer func() {
p.Cleanup()
}()
return nil
}

func (p *dummyPort) Write(fwdpacket.Packet) (fwdaction.State, error) {
return fwdaction.CONSUME, nil
}

// Actions returns the port actions of the specified type
func (p *dummyPort) Actions(dir fwdpb.PortAction) fwdaction.Actions {
return nil
Comment thread
gavinz-ship-it marked this conversation as resolved.
Resolved
}

// State returns the state of the port.
func (p *dummyPort) State(pi *fwdpb.PortInfo) (*fwdpb.PortStateReply, error) {
if pi != nil {
p.adminState = pi.AdminStatus
}
return &fwdpb.PortStateReply{
Status: &fwdpb.PortInfo{
OperStatus: fwdpb.PortState_PORT_STATE_DISABLED_DOWN,
AdminStatus: p.adminState,
},
}, nil
}

type dummyBuilder struct{}

// Build creates a new port.
func (dummyBuilder) Build(portDesc *fwdpb.PortDesc, ctx *fwdcontext.Context) (fwdport.Port, error) {
kp, ok := portDesc.Port.(*fwdpb.PortDesc_Dummy)
if !ok {
return nil, fmt.Errorf("invalid port type in proto")
}
var adminState fwdpb.PortState
if kp.Dummy.GetAdminState() {
adminState = fwdpb.PortState_PORT_STATE_ENABLED_UP
} else {
adminState = fwdpb.PortState_PORT_STATE_DISABLED_DOWN
}
p := dummyPort{
ctx: ctx,
desc: portDesc,
devName: kp.Dummy.GetDeviceName(),
adminState: adminState,
}
list := append(fwdport.CounterList, fwdaction.CounterList...)
if err := p.InitCounters("", list...); err != nil {
return nil, err
}
return &p, nil
}
125 changes: 0 additions & 125 deletions dataplane/forwarding/fwdport/ports/genetlink.go

This file was deleted.

2 changes: 1 addition & 1 deletion dataplane/forwarding/fwdport/ports/tap.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (p *tapPort) Desc() *fwdpb.PortDesc {
}

func (p *tapPort) String() string {
desc := fmt.Sprintf("Type=%v;DeviceName=%v;<Input=%v>;<Output=%v>", fwdpb.PortType_PORT_TYPE_KERNEL, p.fd, p.input, p.output)
desc := fmt.Sprintf("Type=%v;DeviceName=%v;<Input=%v>;<Output=%v>", fwdpb.PortType_PORT_TYPE_KERNEL, p.devName, p.input, p.output)
if state, err := p.State(nil); err == nil {
desc += fmt.Sprintf("<State=%v>;", state)
}
Expand Down
58 changes: 34 additions & 24 deletions dataplane/saiserver/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,28 @@ const (
numSchedulerGroups = 12
)

func (port *port) updatePortList(id uint64) error {
switchID, ok := port.mgr.GetSwitchID()
if !ok {
return fmt.Errorf("failed to get switch ID")
}
switchAttrs := &saipb.SwitchAttribute{}
if err := port.mgr.PopulateAllAttributes(switchID, switchAttrs); err != nil {
return fmt.Errorf("failed to get switch attributes: %v", err)
}
uSwitchID, err := strconv.ParseUint(switchID, 10, 64)
if err != nil {
return err
}
portList := switchAttrs.GetPortList()
portList = append(portList, id)
port.mgr.StoreAttributes(uSwitchID, &saipb.SwitchAttribute{
PortList: portList,
NumberOfActivePorts: proto.Uint32(uint32(len(portList))),
})
return nil
}

// CreatePort creates a new port, mapping the port to ethX, where X is assigned sequentially from 1 to n.
// Note: If more ports are created than eth devices, no error is returned, but the OperStatus is set to NOT_PRESENT.
func (port *port) CreatePort(ctx context.Context, req *saipb.CreatePortRequest) (*saipb.CreatePortResponse, error) {
Expand Down Expand Up @@ -240,19 +262,17 @@ func (port *port) CreatePort(ctx context.Context, req *saipb.CreatePortRequest)
AdminState: req.GetAdminState(),
},
}
// For ports that don't exist, do not create dataplane ports.
if _, err := getInterface(dev); err != nil {
// 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.
// Create a dummy port so we don't get errors for a nonexistent port.
fwdPort := &fwdpb.PortCreateRequest{
ContextId: &fwdpb.ContextId{Id: port.dataplane.ID()},
Port: &fwdpb.PortDesc{
PortType: fwdpb.PortType_PORT_TYPE_TAP,
PortType: fwdpb.PortType_PORT_TYPE_DUMMY,
PortId: &fwdpb.PortId{
ObjectId: &fwdpb.ObjectId{Id: fmt.Sprint(id)},
},
Port: &fwdpb.PortDesc_Tap{
Tap: &fwdpb.TAPPortDesc{
Port: &fwdpb.PortDesc_Dummy{
Dummy: &fwdpb.DummyPortDesc{
DeviceName: dev,
AdminState: req.GetAdminState(),
},
Expand All @@ -264,6 +284,9 @@ func (port *port) CreatePort(ctx context.Context, req *saipb.CreatePortRequest)
}
attrs.OperStatus = saipb.PortOperStatus_PORT_OPER_STATUS_DOWN.Enum()
port.mgr.StoreAttributes(id, attrs)
if err := port.updatePortList(id); err != nil {
return nil, err
}
return &saipb.CreatePortResponse{
Oid: id,
}, nil
Expand Down Expand Up @@ -306,26 +329,9 @@ func (port *port) CreatePort(ctx context.Context, req *saipb.CreatePortRequest)
attrs.OperStatus = saipb.PortOperStatus_PORT_OPER_STATUS_DOWN.Enum()
}
port.mgr.StoreAttributes(id, attrs)

// Update the switch's port list.
switchID, ok := port.mgr.GetSwitchID()
if !ok {
return nil, fmt.Errorf("failed to get switch ID")
}
switchAttrs := &saipb.SwitchAttribute{}
if err := port.mgr.PopulateAllAttributes(switchID, switchAttrs); err != nil {
return nil, fmt.Errorf("failed to get switch attributes: %v", err)
}
uSwitchID, err := strconv.ParseUint(switchID, 10, 64)
if err != nil {
if err := port.updatePortList(id); err != nil {
return nil, err
}
portList := switchAttrs.GetPortList()
portList = append(portList, id)
port.mgr.StoreAttributes(uSwitchID, &saipb.SwitchAttribute{
PortList: portList,
NumberOfActivePorts: proto.Uint32(uint32(len(portList))),
})

nid, err := port.dataplane.ObjectNID(ctx, &fwdpb.ObjectNIDRequest{
ContextId: &fwdpb.ContextId{Id: port.dataplane.ID()},
Expand Down Expand Up @@ -534,6 +540,10 @@ func (port *port) SetPortAttribute(ctx context.Context, req *saipb.SetPortAttrib
return nil, fmt.Errorf("port %v has no lanes", req.GetOid())
}
dev := fmt.Sprintf("eth%v", portAttr.GetAttr().GetHwLaneList()[0])
if _, err := getInterface(dev); err != nil {
// Skip setting MTU if the interface doesn't exist. These could be fake ports.
return &saipb.SetPortAttributeResponse{}, nil
}
slog.InfoContext(ctx, "setting port mtu", "oid", req.GetOid(), "dev", dev, "mtu", req.GetMtu())
link, err := netlink.LinkByName(dev)
if err != nil {
Expand Down
Loading
Loading