Skip to content

Commit

Permalink
Make lemming buildable on non-Linux envs (#229)
Browse files Browse the repository at this point in the history
* Add constraint for kernel port to linux only

* bui

* error on non linux

* feedback

* lint
  • Loading branch information
DanG100 authored Aug 15, 2023
1 parent 7002603 commit 4a83fbb
Show file tree
Hide file tree
Showing 19 changed files with 281 additions and 61 deletions.
53 changes: 50 additions & 3 deletions dataplane/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "dataplane",
srcs = ["server.go"],
srcs = [
"reconcilers_linux.go",
"reconcilers_nonlinux.go",
"server.go",
],
importpath = "github.com/openconfig/lemming/dataplane",
visibility = ["//visibility:public"],
deps = [
"//dataplane/handlers",
"//dataplane/internal/engine",
"//gnmi/oc",
"//gnmi/reconciler",
Expand All @@ -16,5 +19,49 @@ go_library(
"@com_github_openconfig_ygnmi//ygnmi",
"@org_golang_google_grpc//:go_default_library",
"@org_golang_google_grpc//credentials/local",
],
] + select({
"@io_bazel_rules_go//go/platform:aix": [
"//dataplane/handlers",
],
"@io_bazel_rules_go//go/platform:android": [
"//dataplane/handlers",
],
"@io_bazel_rules_go//go/platform:darwin": [
"//dataplane/handlers",
],
"@io_bazel_rules_go//go/platform:dragonfly": [
"//dataplane/handlers",
],
"@io_bazel_rules_go//go/platform:freebsd": [
"//dataplane/handlers",
],
"@io_bazel_rules_go//go/platform:illumos": [
"//dataplane/handlers",
],
"@io_bazel_rules_go//go/platform:ios": [
"//dataplane/handlers",
],
"@io_bazel_rules_go//go/platform:js": [
"//dataplane/handlers",
],
"@io_bazel_rules_go//go/platform:linux": [
"//dataplane/handlers",
],
"@io_bazel_rules_go//go/platform:netbsd": [
"//dataplane/handlers",
],
"@io_bazel_rules_go//go/platform:openbsd": [
"//dataplane/handlers",
],
"@io_bazel_rules_go//go/platform:plan9": [
"//dataplane/handlers",
],
"@io_bazel_rules_go//go/platform:solaris": [
"//dataplane/handlers",
],
"@io_bazel_rules_go//go/platform:windows": [
"//dataplane/handlers",
],
"//conditions:default": [],
}),
)
20 changes: 15 additions & 5 deletions dataplane/forwarding/fwdport/ports/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,27 @@ go_library(
"//dataplane/forwarding/infra/fwdpacket",
"//dataplane/forwarding/util/hash/crc16",
"//dataplane/forwarding/util/queue",
"//dataplane/internal/kernel",
"//internal/debug",
"//proto/forwarding",
"@com_github_golang_glog//:glog",
"@com_github_google_gopacket//:gopacket",
"@com_github_google_gopacket//afpacket",
"@com_github_google_gopacket//layers",
"@com_github_google_gopacket//pcapgo",
"@com_github_vishvananda_netlink//:netlink",
"@org_golang_x_sys//unix",
],
] + select({
"@io_bazel_rules_go//go/platform:android": [
"//dataplane/internal/kernel",
"@com_github_google_gopacket//afpacket",
"@com_github_vishvananda_netlink//:netlink",
"@org_golang_x_sys//unix",
],
"@io_bazel_rules_go//go/platform:linux": [
"//dataplane/internal/kernel",
"@com_github_google_gopacket//afpacket",
"@com_github_vishvananda_netlink//:netlink",
"@org_golang_x_sys//unix",
],
"//conditions:default": [],
}),
)

go_test(
Expand Down
2 changes: 2 additions & 0 deletions dataplane/forwarding/fwdport/ports/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux

package ports

import (
Expand Down
2 changes: 2 additions & 0 deletions dataplane/forwarding/fwdport/ports/tap.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux

package ports

import (
Expand Down
32 changes: 23 additions & 9 deletions dataplane/handlers/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,33 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//dataplane/internal/engine",
"//dataplane/internal/kernel",
"//gnmi",
"//gnmi/gnmiclient",
"//gnmi/oc",
"//gnmi/oc/ocpath",
"//gnmi/reconciler",
"//proto/dataplane",
"//proto/forwarding",
"@com_github_golang_glog//:glog",
"@com_github_openconfig_ygnmi//schemaless",
"@com_github_openconfig_ygnmi//ygnmi",
"@com_github_openconfig_ygot//ygot",
"@com_github_vishvananda_netlink//:netlink",
"@org_golang_x_sys//unix",
],
] + select({
"@io_bazel_rules_go//go/platform:android": [
"//dataplane/internal/kernel",
"//gnmi/gnmiclient",
"//gnmi/oc",
"//gnmi/oc/ocpath",
"//proto/forwarding",
"@com_github_openconfig_ygot//ygot",
"@com_github_vishvananda_netlink//:netlink",
"@org_golang_x_sys//unix",
],
"@io_bazel_rules_go//go/platform:linux": [
"//dataplane/internal/kernel",
"//gnmi/gnmiclient",
"//gnmi/oc",
"//gnmi/oc/ocpath",
"//proto/forwarding",
"@com_github_openconfig_ygot//ygot",
"@com_github_vishvananda_netlink//:netlink",
"@org_golang_x_sys//unix",
],
"//conditions:default": [],
}),
)
2 changes: 2 additions & 0 deletions dataplane/handlers/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux

// Package handlers contains gNMI task handlers.
package handlers

Expand Down
13 changes: 11 additions & 2 deletions dataplane/internal/engine/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ go_library(
name = "engine",
srcs = [
"engine.go",
"engine_linux.go",
"engine_nonlinux.go",
"helpers.go",
],
importpath = "github.com/openconfig/lemming/dataplane/internal/engine",
Expand All @@ -15,8 +17,15 @@ go_library(
"//proto/dataplane",
"//proto/forwarding",
"@com_github_golang_glog//:glog",
"@com_github_vishvananda_netlink//:netlink",
"@org_golang_google_grpc//codes",
"@org_golang_google_grpc//status",
],
] + select({
"@io_bazel_rules_go//go/platform:android": [
"@com_github_vishvananda_netlink//:netlink",
],
"@io_bazel_rules_go//go/platform:linux": [
"@com_github_vishvananda_netlink//:netlink",
],
"//conditions:default": [],
}),
)
26 changes: 1 addition & 25 deletions dataplane/internal/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/vishvananda/netlink"

"github.com/openconfig/lemming/dataplane/forwarding"
"github.com/openconfig/lemming/dataplane/forwarding/attributes"
"github.com/openconfig/lemming/dataplane/forwarding/fwdconfig"
Expand Down Expand Up @@ -92,29 +90,7 @@ func New(ctx context.Context) (*Engine, error) {
internalToExternalID: map[string]string{},
}

updCh := make(chan netlink.AddrUpdate)
doneCh := make(chan struct{})

go func() {
for {
upd := <-updCh
l, err := netlink.LinkByIndex(upd.LinkIndex)
if err != nil {
log.Warningf("failed to get link: %v", err)
continue
}
e.ipToDevNameMu.Lock()
if upd.NewAddr {
log.Infof("added new ip %s to device %s", upd.LinkAddress.IP.String(), l.Attrs().Name)
e.ipToDevName[upd.LinkAddress.IP.String()] = l.Attrs().Name
} else {
delete(e.ipToDevName, upd.LinkAddress.IP.String())
}
e.ipToDevNameMu.Unlock()
}
}()

netlink.AddrSubscribe(updCh, doneCh)
e.handleIPUpdates()

_, err := e.Server.ContextCreate(context.Background(), &fwdpb.ContextCreateRequest{
ContextId: &fwdpb.ContextId{Id: e.id},
Expand Down
49 changes: 49 additions & 0 deletions dataplane/internal/engine/engine_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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.

//go:build linux

package engine

import (
"github.com/vishvananda/netlink"

log "github.com/golang/glog"
)

func (e *Engine) handleIPUpdates() {
updCh := make(chan netlink.AddrUpdate)
doneCh := make(chan struct{})

go func() {
for {
upd := <-updCh
l, err := netlink.LinkByIndex(upd.LinkIndex)
if err != nil {
log.Warningf("failed to get link: %v", err)
continue
}
e.ipToDevNameMu.Lock()
if upd.NewAddr {
log.Infof("added new ip %s to device %s", upd.LinkAddress.IP.String(), l.Attrs().Name)
e.ipToDevName[upd.LinkAddress.IP.String()] = l.Attrs().Name
} else {
delete(e.ipToDevName, upd.LinkAddress.IP.String())
}
e.ipToDevNameMu.Unlock()
}
}()

netlink.AddrSubscribe(updCh, doneCh)
}
19 changes: 19 additions & 0 deletions dataplane/internal/engine/engine_nonlinux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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.

//go:build !linux

package engine

func (e *Engine) handleIPUpdates() {}
15 changes: 11 additions & 4 deletions dataplane/internal/kernel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ go_library(
srcs = ["kernel.go"],
importpath = "github.com/openconfig/lemming/dataplane/internal/kernel",
visibility = ["//dataplane:__subpackages__"],
deps = [
"@com_github_vishvananda_netlink//:netlink",
"@org_golang_x_sys//unix",
],
deps = select({
"@io_bazel_rules_go//go/platform:android": [
"@com_github_vishvananda_netlink//:netlink",
"@org_golang_x_sys//unix",
],
"@io_bazel_rules_go//go/platform:linux": [
"@com_github_vishvananda_netlink//:netlink",
"@org_golang_x_sys//unix",
],
"//conditions:default": [],
}),
)
2 changes: 2 additions & 0 deletions dataplane/internal/kernel/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux

// Package kernel contains funcs that interact with the kernel (sycalls, netlink).
package kernel

Expand Down
28 changes: 22 additions & 6 deletions dataplane/internal/kernel/kerneltest/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,32 @@ go_library(
srcs = ["kernel.go"],
importpath = "github.com/openconfig/lemming/dataplane/internal/kernel/kerneltest",
visibility = ["//dataplane:__subpackages__"],
deps = ["@com_github_vishvananda_netlink//:netlink"],
deps = select({
"@io_bazel_rules_go//go/platform:android": [
"@com_github_vishvananda_netlink//:netlink",
],
"@io_bazel_rules_go//go/platform:linux": [
"@com_github_vishvananda_netlink//:netlink",
],
"//conditions:default": [],
}),
)

go_test(
name = "kerneltest_test",
srcs = ["kernel_test.go"],
embed = [":kerneltest"],
deps = [
"@com_github_google_go_cmp//cmp",
"@com_github_openconfig_gnmi//errdiff",
"@com_github_vishvananda_netlink//:netlink",
],
deps = select({
"@io_bazel_rules_go//go/platform:android": [
"@com_github_google_go_cmp//cmp",
"@com_github_openconfig_gnmi//errdiff",
"@com_github_vishvananda_netlink//:netlink",
],
"@io_bazel_rules_go//go/platform:linux": [
"@com_github_google_go_cmp//cmp",
"@com_github_openconfig_gnmi//errdiff",
"@com_github_vishvananda_netlink//:netlink",
],
"//conditions:default": [],
}),
)
2 changes: 2 additions & 0 deletions dataplane/internal/kernel/kerneltest/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux

// package kerneltest contains fake implemetation of structs in kernel package
package kerneltest

Expand Down
2 changes: 2 additions & 0 deletions dataplane/internal/kernel/kerneltest/kernel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux

package kerneltest

import (
Expand Down
Loading

0 comments on commit 4a83fbb

Please sign in to comment.