Skip to content

Commit

Permalink
anapaya-hackathon: enable gateway connect rpcs
Browse files Browse the repository at this point in the history
  • Loading branch information
oncilla committed Dec 13, 2023
1 parent a15fa19 commit 0e130d2
Show file tree
Hide file tree
Showing 31 changed files with 623 additions and 133 deletions.
2 changes: 1 addition & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ pkg_tar(
"//control/cmd/control",
"//daemon/cmd/daemon",
"//dispatcher/cmd/dispatcher",
#"//gateway/cmd/gateway",
"//gateway/cmd/gateway",
"//router/cmd/router",
"//scion-pki/cmd/scion-pki",
"//scion/cmd/scion",
Expand Down
4 changes: 2 additions & 2 deletions control/beaconing/connect/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

type BeaconSenderFactory struct {
Dialer func(net.Addr, ...squic.EarlyDialerOption) squic.EarlyDialer
Dialer libconnect.Dialer
}

func (f *BeaconSenderFactory) NewSender(
Expand Down Expand Up @@ -64,7 +64,7 @@ func (s BeaconSender) Close() error {

// Registrar registers segments.
type Registrar struct {
Dialer func(net.Addr, ...squic.EarlyDialerOption) squic.EarlyDialer
Dialer libconnect.Dialer
}

// RegisterSegment registers a segment with the remote.
Expand Down
2 changes: 1 addition & 1 deletion control/beaconing/happy/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ go_library(
deps = [
"//control/beaconing:go_default_library",
"//pkg/addr:go_default_library",
"//pkg/log:go_default_library",
"//pkg/connect/happy:go_default_library",
"//pkg/private/serrors:go_default_library",
"//pkg/segment:go_default_library",
],
Expand Down
8 changes: 4 additions & 4 deletions control/beaconing/happy/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ func (s BeaconSender) Send(ctx context.Context, b *seg.PathSegment) error {
happy.Call1[*seg.PathSegment, struct{}]{
Call: happy.NoReturn1[*seg.PathSegment](s.Connect.Send).Call,
Input1: b,
Typ: "connect",
Typ: "control_plane.v1.SegmentCreationService.Beacon",
},
happy.Call1[*seg.PathSegment, struct{}]{
Call: happy.NoReturn1[*seg.PathSegment](s.Connect.Send).Call,
Input1: b,
Typ: "grpc",
Typ: "control_plane.v1.SegmentCreationService.Beacon",
},
)
return err
Expand Down Expand Up @@ -83,13 +83,13 @@ func (r *Registrar) RegisterSegment(ctx context.Context, meta seg.Meta, remote n
Call: happy.NoReturn2[seg.Meta, net.Addr](r.Connect.RegisterSegment).Call,
Input1: meta,
Input2: remote,
Typ: "connect",
Typ: "control_plane.v1.SegmentRegistrationService.SegmentsRegistration",
},
happy.Call2[seg.Meta, net.Addr, struct{}]{
Call: happy.NoReturn2[seg.Meta, net.Addr](r.Connect.RegisterSegment).Call,
Input1: meta,
Input2: remote,
Typ: "grpc",
Typ: "control_plane.v1.SegmentRegistrationService.SegmentsRegistration",
},
)
return err
Expand Down
4 changes: 3 additions & 1 deletion control/cmd/control/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ go_library(
"//control/trust/grpc:go_default_library",
"//control/trust/metrics:go_default_library",
"//pkg/addr:go_default_library",
"//pkg/connect:go_default_library",
"//pkg/grpc:go_default_library",
"//pkg/log:go_default_library",
"//pkg/metrics:go_default_library",
Expand All @@ -41,6 +42,7 @@ go_library(
"//pkg/proto/control_plane:go_default_library",
"//pkg/proto/control_plane/v1/control_planeconnect:go_default_library",
"//pkg/proto/discovery:go_default_library",
"//pkg/proto/discovery/v1/discoveryconnect:go_default_library",
"//pkg/scrypto:go_default_library",
"//pkg/scrypto/cppki:go_default_library",
"//pkg/snet:go_default_library",
Expand All @@ -55,6 +57,7 @@ go_library(
"//private/ca/renewal/connect:go_default_library",
"//private/ca/renewal/grpc:go_default_library",
"//private/discovery:go_default_library",
"//private/discovery/connect:go_default_library",
"//private/drkey/drkeyutil:go_default_library",
"//private/keyconf:go_default_library",
"//private/mgmtapi/cppki/api:go_default_library",
Expand Down Expand Up @@ -89,7 +92,6 @@ go_library(
"@in_gopkg_yaml_v2//:go_default_library",
"@org_go4_netipx//:go_default_library",
"@org_golang_google_grpc//:go_default_library",
"@org_golang_google_grpc//peer:go_default_library",
"@org_golang_x_net//http2:go_default_library",
"@org_golang_x_net//http2/h2c:go_default_library",
"@org_golang_x_sync//errgroup:go_default_library",
Expand Down
53 changes: 15 additions & 38 deletions control/cmd/control/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ import (
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
_ "net/http/pprof"
"net/netip"
"path/filepath"
"strings"
"sync"
Expand All @@ -42,7 +40,6 @@ import (
"go4.org/netipx"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/peer"

cs "github.com/scionproto/scion/control"
"github.com/scionproto/scion/control/beacon"
Expand All @@ -67,6 +64,7 @@ import (
cstrustgrpc "github.com/scionproto/scion/control/trust/grpc"
cstrustmetrics "github.com/scionproto/scion/control/trust/metrics"
"github.com/scionproto/scion/pkg/addr"
libconnect "github.com/scionproto/scion/pkg/connect"
libgrpc "github.com/scionproto/scion/pkg/grpc"
"github.com/scionproto/scion/pkg/log"
libmetrics "github.com/scionproto/scion/pkg/metrics"
Expand All @@ -76,6 +74,7 @@ import (
cppb "github.com/scionproto/scion/pkg/proto/control_plane"
cpconnect "github.com/scionproto/scion/pkg/proto/control_plane/v1/control_planeconnect"
dpb "github.com/scionproto/scion/pkg/proto/discovery"
dconnect "github.com/scionproto/scion/pkg/proto/discovery/v1/discoveryconnect"
"github.com/scionproto/scion/pkg/scrypto"
"github.com/scionproto/scion/pkg/scrypto/cppki"
"github.com/scionproto/scion/pkg/snet"
Expand All @@ -90,6 +89,7 @@ import (
renewalconnect "github.com/scionproto/scion/private/ca/renewal/connect"
renewalgrpc "github.com/scionproto/scion/private/ca/renewal/grpc"
"github.com/scionproto/scion/private/discovery"
discoveryconnect "github.com/scionproto/scion/private/discovery/connect"
"github.com/scionproto/scion/private/drkey/drkeyutil"
"github.com/scionproto/scion/private/keyconf"
cppkiapi "github.com/scionproto/scion/private/mgmtapi/cppki/api"
Expand Down Expand Up @@ -117,27 +117,6 @@ import (
trustmetrics "github.com/scionproto/scion/private/trust/metrics"
)

type loggingHandler struct {
prefix string
next http.Handler
}

func (h loggingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Println(h.prefix, r.Method, r.URL)

if addr, ok := r.Context().Value(http3.RemoteAddrContextKey).(net.Addr); ok {
log.Info("HTTP3 request", "remote", r.Context().Value(http3.RemoteAddrContextKey))
ctx := peer.NewContext(r.Context(), &peer.Peer{Addr: addr})
r = r.WithContext(ctx)
} else if addrPort, err := netip.ParseAddrPort(r.RemoteAddr); err == nil {
log.Info("HTTP request", "remote", addrPort)
tcpAddr := net.TCPAddrFromAddrPort(addrPort)
ctx := peer.NewContext(r.Context(), &peer.Peer{Addr: tcpAddr})
r = r.WithContext(ctx)
}
h.next.ServeHTTP(w, r)
}

var globalCfg config.Config

func main() {
Expand Down Expand Up @@ -318,12 +297,8 @@ func realMain(ctx context.Context) error {
IA: topo.IA(),
Dialer: (&squic.EarlyDialerFactory{
Transport: quicStack.InsecureDialer.Transport,
TLSConfig: func() *tls.Config {
cfg := quicStack.InsecureDialer.TLSConfig.Clone()
cfg.NextProtos = []string{"h3", "SCION"}
return cfg
}(),
Rewriter: dialer.Rewriter,
TLSConfig: libconnect.AdaptTLS(quicStack.InsecureDialer.TLSConfig),
Rewriter: dialer.Rewriter,
}).NewDialer,
},
Grpc: trustgrpc.Fetcher{
Expand Down Expand Up @@ -390,7 +365,6 @@ func realMain(ctx context.Context) error {
IA: topo.IA(),
Requests: libmetrics.NewPromCounter(cstrustmetrics.Handler.Requests),
}
connectInter.Handle(cpconnect.NewTrustMaterialServiceHandler(cstrustconnect.MaterialServer{MaterialServer: trustServer}))
cppb.RegisterTrustMaterialServiceServer(quicServer, trustServer)
connectInter.Handle(cpconnect.NewTrustMaterialServiceHandler(cstrustconnect.MaterialServer{MaterialServer: trustServer}))
connectIntra.Handle(cpconnect.NewTrustMaterialServiceHandler(cstrustconnect.MaterialServer{MaterialServer: trustServer}))
Expand All @@ -406,11 +380,11 @@ func realMain(ctx context.Context) error {
},
}
cppb.RegisterSegmentCreationServiceServer(quicServer, segmentCreationServer)
{
pattern, handler := cpconnect.NewSegmentCreationServiceHandler(beaconingconnect.SegmentCreationServer{SegmentCreationServer: segmentCreationServer})
fmt.Println(pattern)
connectInter.Handle(pattern, handler)
}
connectInter.Handle(
cpconnect.NewSegmentCreationServiceHandler(beaconingconnect.SegmentCreationServer{
SegmentCreationServer: segmentCreationServer,
}),
)

// Handle segment lookup
authLookupServer := &segreqgrpc.LookupServer{
Expand Down Expand Up @@ -639,6 +613,9 @@ func realMain(ctx context.Context) error {
Requests: libmetrics.NewPromCounter(metrics.DiscoveryRequestsTotal),
}
dpb.RegisterDiscoveryServiceServer(quicServer, ds)
connectInter.Handle(
dconnect.NewDiscoveryServiceHandler(discoveryconnect.Topology{Topology: ds}),
)

// dsHealth := health.NewServer()
// dsHealth.SetServingStatus("discovery", healthpb.HealthCheckResponse_SERVING)
Expand Down Expand Up @@ -742,7 +719,7 @@ func realMain(ctx context.Context) error {

var cleanup app.Cleanup
connectServer := http3.Server{
Handler: loggingHandler{"inter", connectInter},
Handler: libconnect.AttachPeer(connectInter),
}

grpcConns := make(chan quic.Connection)
Expand Down Expand Up @@ -788,7 +765,7 @@ func realMain(ctx context.Context) error {
cleanup.Add(func() error { quicServer.GracefulStop(); return nil })

intraServer := http.Server{
Handler: h2c.NewHandler(loggingHandler{"intra", connectIntra}, &http2.Server{}),
Handler: h2c.NewHandler(libconnect.AttachPeer(connectIntra), &http2.Server{}),
}
g.Go(func() error {
defer log.HandlePanic()
Expand Down
5 changes: 5 additions & 0 deletions gateway/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,25 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//gateway/control:go_default_library",
"//gateway/control/connect:go_default_library",
"//gateway/control/grpc:go_default_library",
"//gateway/control/happy:go_default_library",
"//gateway/dataplane:go_default_library",
"//gateway/pathhealth:go_default_library",
"//gateway/pathhealth/policies:go_default_library",
"//gateway/routemgr:go_default_library",
"//gateway/routing:go_default_library",
"//gateway/xnet:go_default_library",
"//pkg/addr:go_default_library",
"//pkg/connect:go_default_library",
"//pkg/daemon:go_default_library",
"//pkg/grpc:go_default_library",
"//pkg/log:go_default_library",
"//pkg/metrics:go_default_library",
"//pkg/private/serrors:go_default_library",
"//pkg/private/util:go_default_library",
"//pkg/proto/gateway:go_default_library",
"//pkg/proto/gateway/v1/gatewayconnect:go_default_library",
"//pkg/snet:go_default_library",
"//pkg/snet/metrics:go_default_library",
"//pkg/snet/squic:go_default_library",
Expand All @@ -41,6 +45,7 @@ go_library(
"@com_github_prometheus_client_golang//prometheus:go_default_library",
"@com_github_prometheus_client_golang//prometheus/promauto:go_default_library",
"@com_github_quic_go_quic_go//:go_default_library",
"@com_github_quic_go_quic_go//http3:go_default_library",
"@org_golang_google_grpc//:go_default_library",
],
)
Expand Down
26 changes: 26 additions & 0 deletions gateway/control/connect/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
load("//tools/lint:go.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = [
"discoverer.go",
"prefix_fetcher.go",
"prefix_server.go",
],
importpath = "github.com/scionproto/scion/gateway/control/connect",
visibility = ["//visibility:public"],
deps = [
"//gateway/control:go_default_library",
"//gateway/control/grpc:go_default_library",
"//pkg/addr:go_default_library",
"//pkg/connect:go_default_library",
"//pkg/private/serrors:go_default_library",
"//pkg/proto/discovery:go_default_library",
"//pkg/proto/discovery/v1/discoveryconnect:go_default_library",
"//pkg/proto/gateway:go_default_library",
"//pkg/proto/gateway/v1/gatewayconnect:go_default_library",
"//pkg/snet:go_default_library",
"@com_connectrpc_connect//:go_default_library",
"@com_github_quic_go_quic_go//http3:go_default_library",
],
)
55 changes: 55 additions & 0 deletions gateway/control/connect/discoverer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package connect

import (
"context"

"connectrpc.com/connect"
"github.com/quic-go/quic-go/http3"
"github.com/scionproto/scion/gateway/control"
"github.com/scionproto/scion/gateway/control/grpc"
"github.com/scionproto/scion/pkg/addr"
libconnect "github.com/scionproto/scion/pkg/connect"
"github.com/scionproto/scion/pkg/private/serrors"
dpb "github.com/scionproto/scion/pkg/proto/discovery"
"github.com/scionproto/scion/pkg/proto/discovery/v1/discoveryconnect"
"github.com/scionproto/scion/pkg/snet"
)

// Discoverer discovers the gateways for a specific remote AS.
type Discoverer struct {
// Remote is the ISD-AS of the remote AS.
Remote addr.IA
// Dialer dials a new QUIC connection.
Dialer libconnect.Dialer
// Paths is a registration for the paths to the remote AS.
Paths control.PathMonitorRegistration
}

func (d Discoverer) Gateways(ctx context.Context) ([]control.Gateway, error) {
paths := d.Paths.Get().Paths
if len(paths) == 0 {
return nil, serrors.New("no path available")
}
ds := &snet.SVCAddr{
IA: d.Remote,
Path: paths[0].Dataplane(),
NextHop: paths[0].UnderlayNextHop(),
SVC: addr.SvcDS,
}

dialer := d.Dialer(ds)
client := discoveryconnect.NewDiscoveryServiceClient(
libconnect.HTTPClient{
RoundTripper: &http3.RoundTripper{
Dial: dialer.DialEarly,
},
},
libconnect.BaseUrl(ds),
)

rep, err := client.Gateways(ctx, connect.NewRequest(&dpb.GatewaysRequest{}))
if err != nil {
return nil, serrors.WrapStr("receiving gateways", err)
}
return grpc.TransformGateways(rep.Msg.Gateways)
}
Loading

0 comments on commit 0e130d2

Please sign in to comment.