Skip to content

Commit

Permalink
Revise the interface of a protocol. (#473)
Browse files Browse the repository at this point in the history
* Revise the interface of a protocol.

* Fix a lint errorwq./platforms/networking/model/nimble/tools/query_uim.sh  -c -d /tmp/b4/lamb4z.pb

* Refactor the code.

* Modify go.mod

* Remove the locks and simplify the code.

* Address comments.
  • Loading branch information
guoshiuan committed Sep 18, 2024
1 parent 0b67db7 commit 2f9ec57
Show file tree
Hide file tree
Showing 5 changed files with 429 additions and 3 deletions.
2 changes: 2 additions & 0 deletions dataplane/dplanerc/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ go_library(
] + select({
"@io_bazel_rules_go//go/platform:android": [
"//dataplane/kernel",
"//dataplane/protocol/lldp",
"//gnmi/gnmiclient",
"//gnmi/oc",
"//gnmi/oc/ocpath",
Expand All @@ -33,6 +34,7 @@ go_library(
],
"@io_bazel_rules_go//go/platform:linux": [
"//dataplane/kernel",
"//dataplane/protocol/lldp",
"//gnmi/gnmiclient",
"//gnmi/oc",
"//gnmi/oc/ocpath",
Expand Down
28 changes: 25 additions & 3 deletions dataplane/dplanerc/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"google.golang.org/protobuf/proto"

"github.com/openconfig/lemming/dataplane/kernel"
"github.com/openconfig/lemming/dataplane/protocol/lldp"
"github.com/openconfig/lemming/gnmi/gnmiclient"
"github.com/openconfig/lemming/gnmi/oc"
"github.com/openconfig/lemming/gnmi/oc/ocpath"
Expand Down Expand Up @@ -97,6 +98,10 @@ func (r routeMap) findRoute(ipPrefix string, vrfID uint64) *routeData {
return r[key]
}

type protocolHanlder interface {
Reconcile(context.Context, *oc.Root, *ygnmi.Client) error
}

// Reconciler handles config updates to the paths.
type Reconciler struct {
c *ygnmi.Client
Expand All @@ -113,6 +118,7 @@ type Reconciler struct {
nextHopGroupClient saipb.NextHopGroupClient
lagClient saipb.LagClient
stateMu sync.RWMutex
lldp protocolHanlder
// state keeps track of the applied state of the device's interfaces so that we do not issue duplicate configuration commands to the device's interfaces.
state map[string]*oc.Interface
switchID uint64
Expand Down Expand Up @@ -162,6 +168,7 @@ func New(conn grpc.ClientConnInterface, switchID, cpuPortID uint64, contextID st
nextHopGroupClient: saipb.NewNextHopGroupClient(conn),
fwdClient: fwdpb.NewForwardingClient(conn),
lagClient: saipb.NewLagClient(conn),
lldp: lldp.New(),
}
return r
}
Expand All @@ -186,17 +193,24 @@ func (ni *Reconciler) StartInterface(ctx context.Context, client *ygnmi.Client)
ocpath.Root().InterfaceAny().Subinterface(0).Ipv6().AddressAny().PrefixLength().Config().PathStruct(),
ocpath.Root().InterfaceAny().Aggregation().LagType().Config().PathStruct(),
ocpath.Root().InterfaceAny().Ethernet().AggregateId().Config().PathStruct(),
ocpath.Root().Lldp().Enabled().Config().PathStruct(),
ocpath.Root().Lldp().InterfaceAny().Config().PathStruct(),
)
cancelCtx, cancelFn := context.WithCancel(ctx)

watcher := ygnmi.Watch(cancelCtx, ni.c, b.Config(), func(val *ygnmi.Value[*oc.Root]) error {
log.V(2).Info("reconciling interfaces")
root, ok := val.Val()
if !ok || root.Interface == nil {
if !ok || (root.Interface == nil && root.Lldp.Interface == nil) {
return ygnmi.Continue
}
for _, i := range root.Interface {
ni.reconcile(cancelCtx, i)
if root.Interface != nil {
for _, i := range root.Interface {
ni.reconcile(cancelCtx, i)
}
}
if root.Lldp.Interface != nil {
ni.reconcileLldp(cancelCtx, root)
}
return ygnmi.Continue
})
Expand Down Expand Up @@ -501,6 +515,13 @@ func (ni *Reconciler) setMinLinks(intf ocInterface, data *interfaceData, minLink
return nil
}

// reconcileLldp compares the LLDP config with state and modifies state to match config.
func (ni *Reconciler) reconcileLldp(ctx context.Context, intent *oc.Root) {
if err := ni.lldp.Reconcile(ctx, intent, ni.c); err != nil {
log.Warningf("error found LLDP reconciliation: %v", err)
}
}

// reconcile compares the interface config with state and modifies state to match config.
func (ni *Reconciler) reconcile(ctx context.Context, config *oc.Interface) {
ni.stateMu.RLock()
Expand Down Expand Up @@ -711,6 +732,7 @@ func (ni *Reconciler) handleDataplaneEvent(ctx context.Context, resp *saipb.Port
}

// handleLinkUpdate modifies the state based on changes to link state.
// This is the callback from netlink.
func (ni *Reconciler) handleLinkUpdate(ctx context.Context, lu *netlink.LinkUpdate) {
ni.stateMu.Lock()
defer ni.stateMu.Unlock()
Expand Down
29 changes: 29 additions & 0 deletions dataplane/protocol/lldp/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "lldp",
srcs = ["lldp.go"],
importpath = "github.com/openconfig/lemming/dataplane/protocol/lldp",
visibility = ["//visibility:public"],
deps = [
"//dataplane/proto/packetio",
"//gnmi/gnmiclient",
"//gnmi/oc",
"//gnmi/oc/ocpath",
"@com_github_golang_glog//:glog",
"@com_github_google_gopacket//:gopacket",
"@com_github_google_gopacket//layers",
"@com_github_openconfig_ygnmi//ygnmi",
],
)

go_test(
name = "lldp_test",
srcs = ["lldp_test.go"],
embed = [":lldp"],
deps = [
"//dataplane/proto/packetio",
"@com_github_google_go_cmp//cmp",
"@com_github_openconfig_gnmi//errdiff",
],
)
Loading

0 comments on commit 2f9ec57

Please sign in to comment.