Skip to content

Commit

Permalink
Update api
Browse files Browse the repository at this point in the history
  • Loading branch information
louisroyer committed Oct 23, 2024
1 parent 9377d0d commit 98824d4
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 73 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/lib/pq v1.10.9
github.com/nextmn/gopacket-gtp v0.0.7
github.com/nextmn/gopacket-srv6 v0.0.8
github.com/nextmn/json-api v0.0.10
github.com/nextmn/json-api v0.0.11
github.com/nextmn/logrus-formatter v0.0.1
github.com/nextmn/rfc9433 v0.0.2
github.com/sirupsen/logrus v1.9.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ github.com/nextmn/gopacket-gtp v0.0.7 h1:O2cuShLTlpVBEXyHn9OIi1Nd+j4QCB66RAwzKBe
github.com/nextmn/gopacket-gtp v0.0.7/go.mod h1:94jLjLU04IOVTKBXUP09MXZCgmlizqmflU2ion1ht6E=
github.com/nextmn/gopacket-srv6 v0.0.8 h1:oP4wuJ7dOiV/gWmX3zoFcdp2dKdSWLUaxH2fJ3TYAwA=
github.com/nextmn/gopacket-srv6 v0.0.8/go.mod h1:2Tyuo9zsG0bP2IhC4tVRgPRuyUqOgrvEEH9seJSZTlU=
github.com/nextmn/json-api v0.0.10 h1:/7WCtGaLEKFKGstOrssac6QgPL0MeGqpkRWU3hepS1A=
github.com/nextmn/json-api v0.0.10/go.mod h1:0py63IYCOBp1ZtLkMjNCNnOwbwhOmkh+ymJ0/OrxYx8=
github.com/nextmn/json-api v0.0.11 h1:wrx5IfWntdCmyGdSsFc31RyuKktAvqe9Un+DcxuSfi8=
github.com/nextmn/json-api v0.0.11/go.mod h1:0py63IYCOBp1ZtLkMjNCNnOwbwhOmkh+ymJ0/OrxYx8=
github.com/nextmn/logrus-formatter v0.0.1 h1:Bsf78jjiEESc+rV8xE6IyKj4frDPGMwXFNrLQzm6A1E=
github.com/nextmn/logrus-formatter v0.0.1/go.mod h1:vdSZ+sIcSna8vjbXkSFxsnsKHqRwaUEed4JCPcXoGyM=
github.com/nextmn/rfc9433 v0.0.2 h1:6FjMY+Qy8MNXQ0PPxezUsyXDxJiCbTp5j3OcXQgIQh8=
Expand Down
42 changes: 10 additions & 32 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (db *Database) InsertRule(ctx context.Context, r jsonapi.Rule) (*uuid.UUID,

if stmt, ok := db.stmt["insert_uplink_rule"]; ok {
var id uuid.UUID
err := stmt.QueryRowContext(ctx, r.Enabled, inneripsrc, outeripsrc, r.Match.Header.Teid, inneripdst, r.Action.NextHop.String(), pq.Array(srh)).Scan(&id)
err := stmt.QueryRowContext(ctx, r.Enabled, inneripsrc, outeripsrc, r.Match.Header.Teid, inneripdst, pq.Array(srh)).Scan(&id)
return &id, err
} else {
return nil, fmt.Errorf("Procedure not registered")
Expand All @@ -120,7 +120,7 @@ func (db *Database) InsertRule(ctx context.Context, r jsonapi.Rule) (*uuid.UUID,
} else {
dst = r.Match.Payload.Dst.String() + "/32"
}
err := stmt.QueryRowContext(ctx, r.Enabled, dst, r.Action.NextHop.String(), pq.Array(srh)).Scan(&id)
err := stmt.QueryRowContext(ctx, r.Enabled, dst, pq.Array(srh)).Scan(&id)
return &id, err
} else {
return nil, fmt.Errorf("Procedure not registered")
Expand All @@ -133,14 +133,13 @@ func (db *Database) InsertRule(ctx context.Context, r jsonapi.Rule) (*uuid.UUID,
func (db *Database) GetRule(ctx context.Context, uuid uuid.UUID) (jsonapi.Rule, error) {
var type_uplink bool
var enabled bool
var action_next_hop string
var action_srh []string
var match_ue_ip string
var match_gnb_ip *string
var match_service_ip *string
var match_uplink_teid *uint32
if stmt, ok := db.stmt["get_rule"]; ok {
err := stmt.QueryRowContext(ctx, uuid.String()).Scan(&type_uplink, &enabled, &action_next_hop, pq.Array(&action_srh), &match_ue_ip, &match_gnb_ip, &match_uplink_teid, &match_service_ip)
err := stmt.QueryRowContext(ctx, uuid.String()).Scan(&type_uplink, &enabled, pq.Array(&action_srh), &match_ue_ip, &match_gnb_ip, &match_uplink_teid, &match_service_ip)
if err != nil {
return jsonapi.Rule{}, err
}
Expand Down Expand Up @@ -186,14 +185,9 @@ func (db *Database) GetRule(ctx context.Context, uuid uuid.UUID) (jsonapi.Rule,
if err != nil {
return jsonapi.Rule{}, err
}
nh, err := jsonapi.NewNextHop(action_next_hop)
if err != nil {
return jsonapi.Rule{}, err
}

rule.Action = jsonapi.Action{
NextHop: *nh,
SRH: *srh,
SRH: *srh,
}

return rule, err
Expand All @@ -206,7 +200,6 @@ func (db *Database) GetRules(ctx context.Context) (jsonapi.RuleMap, error) {
var uuid uuid.UUID
var type_uplink bool
var enabled bool
var action_next_hop string
var action_srh []string
var match_ue_ip string
var match_gnb_ip *string
Expand All @@ -224,7 +217,7 @@ func (db *Database) GetRules(ctx context.Context) (jsonapi.RuleMap, error) {
// avoid looping if no longer necessary
return jsonapi.RuleMap{}, ctx.Err()
default:
err := rows.Scan(&uuid, &type_uplink, &enabled, &action_next_hop, pq.Array(&action_srh), &match_ue_ip, &match_gnb_ip, &match_uplink_teid, &match_service_ip)
err := rows.Scan(&uuid, &type_uplink, &enabled, pq.Array(&action_srh), &match_ue_ip, &match_gnb_ip, &match_uplink_teid, &match_service_ip)
if err != nil {
return m, err
}
Expand Down Expand Up @@ -271,14 +264,9 @@ func (db *Database) GetRules(ctx context.Context) (jsonapi.RuleMap, error) {
if err != nil {
return jsonapi.RuleMap{}, err
}
nh, err := jsonapi.NewNextHop(action_next_hop)
if err != nil {
return jsonapi.RuleMap{}, err
}

rule.Action = jsonapi.Action{
NextHop: *nh,
SRH: *srh,
SRH: *srh,
}
m[uuid] = rule
}
Expand Down Expand Up @@ -327,44 +315,34 @@ func (db *Database) DeleteRule(ctx context.Context, uuid uuid.UUID) error {
}

func (db *Database) GetUplinkAction(ctx context.Context, uplinkTeid uint32, gnbIp netip.Addr, ueIp netip.Addr, serviceIp netip.Addr) (jsonapi.Action, error) {
var action_next_hop string
var action_srh []string
if stmt, ok := db.stmt["get_uplink_action"]; ok {
err := stmt.QueryRowContext(ctx, uplinkTeid, gnbIp.String(), ueIp.String(), serviceIp.String()).Scan(&action_next_hop, pq.Array(&action_srh))
err := stmt.QueryRowContext(ctx, uplinkTeid, gnbIp.String(), ueIp.String(), serviceIp.String()).Scan(pq.Array(&action_srh))
if err != nil {
return jsonapi.Action{}, err
}
srh, err := jsonapi.NewSRH(action_srh)
if err != nil {
return jsonapi.Action{}, err
}
nh, err := jsonapi.NewNextHop(action_next_hop)
if err != nil {
return jsonapi.Action{}, err
}
return jsonapi.Action{NextHop: *nh, SRH: *srh}, err
return jsonapi.Action{SRH: *srh}, err
} else {
return jsonapi.Action{}, fmt.Errorf("Procedure not registered")
}
}

func (db *Database) GetDownlinkAction(ctx context.Context, ueIp netip.Addr) (jsonapi.Action, error) {
var action_next_hop string
var action_srh []string
if stmt, ok := db.stmt["get_downlink_action"]; ok {
err := stmt.QueryRowContext(ctx, ueIp.String()).Scan(&action_next_hop, pq.Array(&action_srh))
err := stmt.QueryRowContext(ctx, ueIp.String()).Scan(pq.Array(&action_srh))
if err != nil {
return jsonapi.Action{}, err
}
srh, err := jsonapi.NewSRH(action_srh)
if err != nil {
return jsonapi.Action{}, err
}
nh, err := jsonapi.NewNextHop(action_next_hop)
if err != nil {
return jsonapi.Action{}, err
}
return jsonapi.Action{NextHop: *nh, SRH: *srh}, err
return jsonapi.Action{SRH: *srh}, err
} else {
return jsonapi.Action{}, fmt.Errorf("Procedure not registered")
}
Expand Down
25 changes: 10 additions & 15 deletions internal/database/database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ CREATE TABLE IF NOT EXISTS rule (
uuid UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
type_uplink BOOL NOT NULL,
enabled BOOL NOT NULL,
action_next_hop INET NOT NULL,
action_srh INET ARRAY NOT NULL,
match_ue_ip CIDR NOT NULL,
match_gnb_ip CIDR,
Expand All @@ -20,24 +19,24 @@ CREATE OR REPLACE PROCEDURE insert_uplink_rule(
IN in_enabled BOOL, IN in_ue_ip CIDR,
IN in_gnb_ip CIDR, IN in_uplink_teid INTEGER,
IN in_service_ip CIDR,
IN in_next_hop INET, IN in_srh INET ARRAY,
IN in_srh INET ARRAY,
OUT out_uuid UUID
)
LANGUAGE plpgsql AS $$
BEGIN
INSERT INTO rule(type_uplink, enabled, match_ue_ip, match_gnb_ip, match_uplink_teid, match_service_ip, action_next_hop, action_srh)
VALUES(TRUE, in_enabled, in_ue_ip, in_gnb_ip, in_uplink_teid, in_service_ip, in_next_hop, in_srh) RETURNING rule.uuid INTO out_uuid;
INSERT INTO rule(type_uplink, enabled, match_ue_ip, match_gnb_ip, match_uplink_teid, match_service_ip, action_srh)
VALUES(TRUE, in_enabled, in_ue_ip, in_gnb_ip, in_uplink_teid, in_service_ip, in_srh) RETURNING rule.uuid INTO out_uuid;
END;$$;

CREATE OR REPLACE PROCEDURE insert_downlink_rule(
IN in_enabled BOOL, IN in_ue_ip CIDR,
IN in_next_hop INET, IN in_srh INET ARRAY,
IN in_srh INET ARRAY,
OUT out_uuid UUID
)
LANGUAGE plpgsql AS $$
BEGIN
INSERT INTO rule(type_uplink, enabled, match_ue_ip, action_next_hop, action_srh)
VALUES(FALSE, in_enabled, in_ue_ip, in_next_hop, in_srh) RETURNING rule.uuid INTO out_uuid;
INSERT INTO rule(type_uplink, enabled, match_ue_ip, action_srh)
VALUES(FALSE, in_enabled, in_ue_ip, in_srh) RETURNING rule.uuid INTO out_uuid;
END;$$;


Expand Down Expand Up @@ -80,12 +79,11 @@ CREATE OR REPLACE FUNCTION get_uplink_action(
IN in_ue_ip INET, IN in_service_ip INET
)
RETURNS TABLE (
t_action_next_hop INET,
t_action_srh INET ARRAY
)
AS $$
BEGIN
RETURN QUERY SELECT rule.action_next_hop AS "t_action_next_hop", rule.action_srh AS "t_action_srh"
RETURN QUERY SELECT rule.action_srh AS "t_action_srh"
FROM rule
WHERE (rule.match_uplink_teid = in_uplink_teid
AND rule.match_gnb_ip && in_gnb_ip
Expand All @@ -98,12 +96,11 @@ CREATE OR REPLACE FUNCTION get_downlink_action(
IN in_ue_ip_address INET
)
RETURNS TABLE (
t_action_next_hop INET,
t_action_srh INET ARRAY
)
AS $$
BEGIN
RETURN QUERY SELECT rule.action_next_hop AS "t_action_next_hop", rule.action_srh AS "t_action_srh"
RETURN QUERY SELECT rule.action_srh AS "t_action_srh"
FROM rule
WHERE (rule.type_uplink = FALSE AND rule.enabled = TRUE
AND match_ue_ip && in_ue_ip_address);
Expand All @@ -115,7 +112,6 @@ CREATE OR REPLACE FUNCTION get_rule(
RETURNS TABLE (
t_type_uplink BOOL,
t_enabled BOOL,
t_action_next_hop INET,
t_action_srh INET ARRAY,
t_match_ue_ip CIDR,
t_match_gnb_ip CIDR,
Expand All @@ -124,7 +120,7 @@ RETURNS TABLE (
)
AS $$
BEGIN
RETURN QUERY SELECT type_uplink AS "t_type_uplink", enabled AS "t_enabled", action_next_hop AS "t_action_next_hop",
RETURN QUERY SELECT type_uplink AS "t_type_uplink", enabled AS "t_enabled",
action_srh AS "t_action_srh", match_ue_ip AS "t_match_ue_ip", match_gnb_ip AS "t_match_gnb_ip",
match_uplink_teid AS "t_match_uplink_teid", match_service_ip AS "t_match_service_ip"
FROM rule
Expand All @@ -136,7 +132,6 @@ RETURNS TABLE (
t_uuid UUID,
t_type_uplink BOOL,
t_enabled BOOL,
t_action_next_hop INET,
t_action_srh INET ARRAY,
t_match_ue_ip CIDR,
t_match_gnb_ip CIDR,
Expand All @@ -146,7 +141,7 @@ RETURNS TABLE (
AS $$
BEGIN
RETURN QUERY SELECT uuid AS "t_uuid", type_uplink AS "t_type_uplink",
enabled AS "t_enabled", action_next_hop AS "t_action_next_hop",
enabled AS "t_enabled",
action_srh AS "t_action_srh", match_ue_ip AS "t_match_ue_ip", match_gnb_ip AS "t_match_gnb_ip",
match_uplink_teid AS "t_match_uplink_teid", match_service_ip AS "t_match_service_ip"
FROM rule;
Expand Down
4 changes: 2 additions & 2 deletions internal/database/database_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 4 additions & 10 deletions internal/netfunc/headend-encaps-ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package netfunc
import (
"context"
"fmt"
"net"
"net/netip"

"github.com/google/gopacket"
Expand Down Expand Up @@ -52,28 +51,23 @@ func (h HeadendEncapsWithCtrl) Handle(ctx context.Context, packet []byte) ([]byt
return nil, fmt.Errorf("Error during serialization of IPv6 SA: %w", err)
}

nextHop := action.NextHop.AsSlice()
segs := action.SRH.AsSlice()
ipheader := &layers.IPv6{
SrcIP: src,
// S06. Set the IPv6 DA = B
DstIP: nextHop,
DstIP: segs[len(segs)-1],
Version: 6,
NextHeader: layers.IPProtocolIPv6Routing, // IPv6-Route
HopLimit: h.HopLimit(),
// TODO: Generate a FlowLabel with hash(IPv6SA + IPv6DA + policy)
TrafficClass: 0, // FIXME: put this in Action
}
segList := []net.IP{}
for _, seg := range action.SRH {
segList = append(segList, seg.AsSlice())
}
segList = append(segList, nextHop)

srh := &gopacket_srv6.IPv6Routing{
RoutingType: 4,
// the first item on segments list is the next endpoint
SegmentsLeft: uint8(len(segList) - 1), // pointer to next segment
SourceRoutingIPs: segList,
SegmentsLeft: uint8(len(segs) - 1), // pointer to next segment
SourceRoutingIPs: segs,
Tag: 0, // not used
Flags: 0, // no flag defined
GopacketIpv6ExtensionBase: gopacket_srv6.GopacketIpv6ExtensionBase{
Expand Down
15 changes: 4 additions & 11 deletions internal/netfunc/headend-gtp4-ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"context"
"encoding/binary"
"fmt"
"net"
"net/netip"

"github.com/google/gopacket"
Expand Down Expand Up @@ -87,32 +86,26 @@ func (h HeadendGTP4WithCtrl) Handle(ctx context.Context, packet []byte) ([]byte,
ipv6SA := encoding.NewMGTP4IPv6Src(h.srcPrefix, [4]byte(ipv4SA), binary.BigEndian.Uint16(udpSP))

src, err := ipv6SA.Marshal()
segs := action.SRH.AsSlice()
if err != nil {
return nil, fmt.Errorf("Error during serialization of IPv6 SA: %w", err)
}
nextHop := action.NextHop.AsSlice()

ipheader := &layers.IPv6{
SrcIP: src,
// S06. Set the IPv6 DA = B
DstIP: nextHop,
DstIP: segs[len(segs)-1],
Version: 6,
NextHeader: layers.IPProtocolIPv6Routing, // IPv6-Route
HopLimit: h.HopLimit(),
// TODO: Generate a FlowLabel with hash(IPv6SA + IPv6DA + policy)
//TrafficClass: qfi << 2,
TrafficClass: 0, // FIXME
}
segList := []net.IP{}
for _, seg := range action.SRH {
segList = append(segList, seg.AsSlice())
}
segList = append(segList, nextHop)
srh := &gopacket_srv6.IPv6Routing{
RoutingType: 4,
// the first item on segments list is the next endpoint
SegmentsLeft: uint8(len(segList) - 1), // pointer to next segment
SourceRoutingIPs: segList,
SegmentsLeft: uint8(len(segs) - 1), // pointer to next segment
SourceRoutingIPs: segs,
Tag: 0, // not used
Flags: 0, // no flag defined
GopacketIpv6ExtensionBase: gopacket_srv6.GopacketIpv6ExtensionBase{
Expand Down

0 comments on commit 98824d4

Please sign in to comment.