Skip to content

Commit

Permalink
Merge pull request #370 from openconfig/mymac
Browse files Browse the repository at this point in the history
Add my station mac.
  • Loading branch information
guoshiuan authored Mar 8, 2024
2 parents 9bab41a + 9b8db6e commit ae0809d
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 13 deletions.
118 changes: 118 additions & 0 deletions dataplane/saiserver/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,121 @@ func (a *acl) CreateAclEntry(ctx context.Context, req *saipb.CreateAclEntryReque

return &saipb.CreateAclEntryResponse{Oid: id}, nil
}

type myMacInfo struct {
priority *uint32
portID *uint64 // wildcard if not specified
vlanID *uint16 // wildcard if not specified
macAddress []byte
macAddressMask []byte
}

// ToEntryDesc returns the EntryDesc.
func (mi *myMacInfo) ToEntryDesc(m *myMac) (*fwdpb.EntryDesc, error) {
fields := []*fwdconfig.PacketFieldMaskedBytesBuilder{
fwdconfig.PacketFieldMaskedBytes(fwdpb.PacketFieldNum_PACKET_FIELD_NUM_ETHER_MAC_DST).
WithBytes(mi.macAddress, mi.macAddressMask),
}

if mi.vlanID != nil {
fields = append(fields,
fwdconfig.PacketFieldMaskedBytes(fwdpb.PacketFieldNum_PACKET_FIELD_NUM_VLAN_TAG).
WithBytes(binary.BigEndian.AppendUint16(nil, *mi.vlanID), binary.BigEndian.AppendUint16(nil, 0x0FFF)))
}

if mi.portID != nil {
fwdCtx, err := m.dataplane.FindContext(&fwdpb.ContextId{Id: m.dataplane.ID()})
if err != nil {
return nil, err
}
obj, err := fwdCtx.Objects.FindID(&fwdpb.ObjectId{Id: fmt.Sprint(*mi.portID)})
if err != nil {
return nil, err
}
fields = append(fields,
fwdconfig.PacketFieldMaskedBytes(fwdpb.PacketFieldNum_PACKET_FIELD_NUM_PACKET_PORT_INPUT).
WithBytes(binary.BigEndian.AppendUint64(nil, uint64(obj.NID())), binary.BigEndian.AppendUint64(nil, math.MaxUint64)))
}

ed := &fwdpb.EntryDesc{
Entry: &fwdpb.EntryDesc_Flow{
Flow: &fwdpb.FlowEntryDesc{
Priority: *mi.priority,
Bank: 1,
},
},
}
for _, f := range fields {
ed.GetFlow().Fields = append(ed.GetFlow().Fields, f.Build())
}
return ed, nil
}

type myMac struct {
saipb.UnimplementedMyMacServer
mgr *attrmgr.AttrMgr
dataplane switchDataplaneAPI
myMacTable map[uint64]*myMacInfo
}

func newMyMac(mgr *attrmgr.AttrMgr, dataplane switchDataplaneAPI, s *grpc.Server) *myMac {
m := &myMac{
mgr: mgr,
dataplane: dataplane,
myMacTable: map[uint64]*myMacInfo{},
}
saipb.RegisterMyMacServer(s, m)
return m
}

func (m *myMac) CreateMyMac(ctx context.Context, req *saipb.CreateMyMacRequest) (*saipb.CreateMyMacResponse, error) {
mi := &myMacInfo{
priority: req.Priority,
portID: req.PortId,
macAddress: req.GetMacAddress(),
macAddressMask: req.GetMacAddressMask(),
}
if req.VlanId != nil {
vid := (uint16)(req.GetVlanId())
mi.vlanID = &vid
}
if mi.macAddress == nil || mi.macAddressMask == nil {
return nil, status.Errorf(codes.InvalidArgument, "MAC address and MAC address mask cannot be empty")
}
id := m.mgr.NextID()
ed, err := mi.ToEntryDesc(m)
if err != nil {
return nil, status.Errorf(codes.FailedPrecondition, "failed to create entry descriptor: %v", err)
}
mReq := &fwdpb.TableEntryAddRequest{
ContextId: &fwdpb.ContextId{Id: m.dataplane.ID()},
TableId: &fwdpb.TableId{ObjectId: &fwdpb.ObjectId{Id: MyMacTable}},
EntryDesc: ed,
}

if _, err := m.dataplane.TableEntryAdd(ctx, mReq); err != nil {
return nil, err
}
return &saipb.CreateMyMacResponse{Oid: id}, nil
}

func (m *myMac) RemoveMyMac(ctx context.Context, req *saipb.RemoveMyMacRequest) (*saipb.RemoveMyMacResponse, error) {
mi, ok := m.myMacTable[req.GetOid()]
if !ok {
return nil, status.Errorf(codes.FailedPrecondition, "%d does not exist", req.GetOid())
}
ed, err := mi.ToEntryDesc(m)
if err != nil {
return nil, status.Errorf(codes.FailedPrecondition, "failed to create entry descriptor: %v", err)
}
mReq := &fwdpb.TableEntryRemoveRequest{
ContextId: &fwdpb.ContextId{Id: m.dataplane.ID()},
TableId: &fwdpb.TableId{ObjectId: &fwdpb.ObjectId{Id: MyMacTable}},
EntryDesc: ed,
}

if _, err := m.dataplane.TableEntryRemove(ctx, mReq); err != nil {
return nil, err
}
return &saipb.RemoveMyMacResponse{}, nil
}
7 changes: 0 additions & 7 deletions dataplane/saiserver/saiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ type mpls struct {
saipb.UnimplementedMplsServer
}

type myMac struct {
saipb.UnimplementedMyMacServer
}

type nat struct {
saipb.UnimplementedNatServer
}
Expand Down Expand Up @@ -192,7 +188,6 @@ type Server struct {
mcastFdb *mcastFdb
mirror *mirror
mpls *mpls
myMac *myMac
nat *nat
policer *policer
qosMap *qosMap
Expand Down Expand Up @@ -282,7 +277,6 @@ func New(ctx context.Context, mgr *attrmgr.AttrMgr, s *grpc.Server, opts *dplane
mcastFdb: &mcastFdb{},
mirror: &mirror{},
mpls: &mpls{},
myMac: &myMac{},
nat: &nat{},
policer: &policer{},
qosMap: &qosMap{},
Expand Down Expand Up @@ -315,7 +309,6 @@ func New(ctx context.Context, mgr *attrmgr.AttrMgr, s *grpc.Server, opts *dplane
saipb.RegisterMcastFdbServer(s, srv.mcastFdb)
saipb.RegisterMirrorServer(s, srv.mirror)
saipb.RegisterMplsServer(s, srv.mpls)
saipb.RegisterMyMacServer(s, srv.myMac)
saipb.RegisterNatServer(s, srv.nat)
saipb.RegisterPolicerServer(s, srv.policer)
saipb.RegisterQosMapServer(s, srv.qosMap)
Expand Down
27 changes: 21 additions & 6 deletions dataplane/saiserver/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type saiSwitch struct {
bridge *bridge
hostif *hostif
hash *hash
myMac *myMac
neighbor *neighbor
nextHopGroup *nextHopGroup
nextHop *nextHop
Expand All @@ -71,6 +72,9 @@ type switchDataplaneAPI interface {
}

const (
inputIfaceTable = "input-iface"
outputIfaceTable = "output-iface"
IngressVRFTable = "ingress-vrf"
FIBV4Table = "fib-v4"
FIBV6Table = "fib-v6"
SRCMACTable = "port-mac"
Expand All @@ -86,6 +90,7 @@ const (
EgressActionTable = "egress-action-table"
NHActionTable = "nh-action"
TunnelEncap = "tunnel-encap"
MyMacTable = "my-mac-table"
hostifToPortTable = "cpu-input"
portToHostifTable = "cpu-output"
)
Expand All @@ -105,6 +110,7 @@ func newSwitch(mgr *attrmgr.AttrMgr, engine switchDataplaneAPI, s *grpc.Server,
bridge: newBridge(mgr, engine, s),
hostif: newHostif(mgr, engine, s, opts),
hash: &hash{},
myMac: newMyMac(mgr, engine, s),
neighbor: newNeighbor(mgr, engine, s),
nextHopGroup: newNextHopGroup(mgr, engine, s),
nextHop: newNextHop(mgr, engine, s),
Expand All @@ -121,12 +127,6 @@ func newSwitch(mgr *attrmgr.AttrMgr, engine switchDataplaneAPI, s *grpc.Server,
return sw, nil
}

const (
inputIfaceTable = "input-iface"
outputIfaceTable = "output-iface"
IngressVRFTable = "ingress-vrf"
)

// CreateSwitch a creates a new switch and populates its default values.
func (sw *saiSwitch) CreateSwitch(ctx context.Context, _ *saipb.CreateSwitchRequest) (*saipb.CreateSwitchResponse, error) {
swID := sw.mgr.NextID()
Expand Down Expand Up @@ -221,6 +221,21 @@ func (sw *saiSwitch) CreateSwitch(ctx context.Context, _ *saipb.CreateSwitchRequ
if _, err := sw.dataplane.TableCreate(ctx, portMAC); err != nil {
return nil, err
}
myMAC := &fwdpb.TableCreateRequest{
ContextId: &fwdpb.ContextId{Id: sw.dataplane.ID()},
Desc: &fwdpb.TableDesc{
TableType: fwdpb.TableType_TABLE_TYPE_FLOW,
TableId: &fwdpb.TableId{ObjectId: &fwdpb.ObjectId{Id: MyMacTable}},
Table: &fwdpb.TableDesc_Flow{
Flow: &fwdpb.FlowTableDesc{
BankCount: 1,
},
},
},
}
if _, err := sw.dataplane.TableCreate(ctx, myMAC); err != nil {
return nil, err
}
neighbor := &fwdpb.TableCreateRequest{
ContextId: &fwdpb.ContextId{Id: sw.dataplane.ID()},
Desc: &fwdpb.TableDesc{
Expand Down

0 comments on commit ae0809d

Please sign in to comment.