Skip to content

Commit 98824d4

Browse files
committed
Update api
1 parent 9377d0d commit 98824d4

File tree

7 files changed

+33
-73
lines changed

7 files changed

+33
-73
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/lib/pq v1.10.9
1111
github.com/nextmn/gopacket-gtp v0.0.7
1212
github.com/nextmn/gopacket-srv6 v0.0.8
13-
github.com/nextmn/json-api v0.0.10
13+
github.com/nextmn/json-api v0.0.11
1414
github.com/nextmn/logrus-formatter v0.0.1
1515
github.com/nextmn/rfc9433 v0.0.2
1616
github.com/sirupsen/logrus v1.9.3

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ github.com/nextmn/gopacket-gtp v0.0.7 h1:O2cuShLTlpVBEXyHn9OIi1Nd+j4QCB66RAwzKBe
5858
github.com/nextmn/gopacket-gtp v0.0.7/go.mod h1:94jLjLU04IOVTKBXUP09MXZCgmlizqmflU2ion1ht6E=
5959
github.com/nextmn/gopacket-srv6 v0.0.8 h1:oP4wuJ7dOiV/gWmX3zoFcdp2dKdSWLUaxH2fJ3TYAwA=
6060
github.com/nextmn/gopacket-srv6 v0.0.8/go.mod h1:2Tyuo9zsG0bP2IhC4tVRgPRuyUqOgrvEEH9seJSZTlU=
61-
github.com/nextmn/json-api v0.0.10 h1:/7WCtGaLEKFKGstOrssac6QgPL0MeGqpkRWU3hepS1A=
62-
github.com/nextmn/json-api v0.0.10/go.mod h1:0py63IYCOBp1ZtLkMjNCNnOwbwhOmkh+ymJ0/OrxYx8=
61+
github.com/nextmn/json-api v0.0.11 h1:wrx5IfWntdCmyGdSsFc31RyuKktAvqe9Un+DcxuSfi8=
62+
github.com/nextmn/json-api v0.0.11/go.mod h1:0py63IYCOBp1ZtLkMjNCNnOwbwhOmkh+ymJ0/OrxYx8=
6363
github.com/nextmn/logrus-formatter v0.0.1 h1:Bsf78jjiEESc+rV8xE6IyKj4frDPGMwXFNrLQzm6A1E=
6464
github.com/nextmn/logrus-formatter v0.0.1/go.mod h1:vdSZ+sIcSna8vjbXkSFxsnsKHqRwaUEed4JCPcXoGyM=
6565
github.com/nextmn/rfc9433 v0.0.2 h1:6FjMY+Qy8MNXQ0PPxezUsyXDxJiCbTp5j3OcXQgIQh8=

internal/database/database.go

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (db *Database) InsertRule(ctx context.Context, r jsonapi.Rule) (*uuid.UUID,
106106

107107
if stmt, ok := db.stmt["insert_uplink_rule"]; ok {
108108
var id uuid.UUID
109-
err := stmt.QueryRowContext(ctx, r.Enabled, inneripsrc, outeripsrc, r.Match.Header.Teid, inneripdst, r.Action.NextHop.String(), pq.Array(srh)).Scan(&id)
109+
err := stmt.QueryRowContext(ctx, r.Enabled, inneripsrc, outeripsrc, r.Match.Header.Teid, inneripdst, pq.Array(srh)).Scan(&id)
110110
return &id, err
111111
} else {
112112
return nil, fmt.Errorf("Procedure not registered")
@@ -120,7 +120,7 @@ func (db *Database) InsertRule(ctx context.Context, r jsonapi.Rule) (*uuid.UUID,
120120
} else {
121121
dst = r.Match.Payload.Dst.String() + "/32"
122122
}
123-
err := stmt.QueryRowContext(ctx, r.Enabled, dst, r.Action.NextHop.String(), pq.Array(srh)).Scan(&id)
123+
err := stmt.QueryRowContext(ctx, r.Enabled, dst, pq.Array(srh)).Scan(&id)
124124
return &id, err
125125
} else {
126126
return nil, fmt.Errorf("Procedure not registered")
@@ -133,14 +133,13 @@ func (db *Database) InsertRule(ctx context.Context, r jsonapi.Rule) (*uuid.UUID,
133133
func (db *Database) GetRule(ctx context.Context, uuid uuid.UUID) (jsonapi.Rule, error) {
134134
var type_uplink bool
135135
var enabled bool
136-
var action_next_hop string
137136
var action_srh []string
138137
var match_ue_ip string
139138
var match_gnb_ip *string
140139
var match_service_ip *string
141140
var match_uplink_teid *uint32
142141
if stmt, ok := db.stmt["get_rule"]; ok {
143-
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)
142+
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)
144143
if err != nil {
145144
return jsonapi.Rule{}, err
146145
}
@@ -186,14 +185,9 @@ func (db *Database) GetRule(ctx context.Context, uuid uuid.UUID) (jsonapi.Rule,
186185
if err != nil {
187186
return jsonapi.Rule{}, err
188187
}
189-
nh, err := jsonapi.NewNextHop(action_next_hop)
190-
if err != nil {
191-
return jsonapi.Rule{}, err
192-
}
193188

194189
rule.Action = jsonapi.Action{
195-
NextHop: *nh,
196-
SRH: *srh,
190+
SRH: *srh,
197191
}
198192

199193
return rule, err
@@ -206,7 +200,6 @@ func (db *Database) GetRules(ctx context.Context) (jsonapi.RuleMap, error) {
206200
var uuid uuid.UUID
207201
var type_uplink bool
208202
var enabled bool
209-
var action_next_hop string
210203
var action_srh []string
211204
var match_ue_ip string
212205
var match_gnb_ip *string
@@ -224,7 +217,7 @@ func (db *Database) GetRules(ctx context.Context) (jsonapi.RuleMap, error) {
224217
// avoid looping if no longer necessary
225218
return jsonapi.RuleMap{}, ctx.Err()
226219
default:
227-
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)
220+
err := rows.Scan(&uuid, &type_uplink, &enabled, pq.Array(&action_srh), &match_ue_ip, &match_gnb_ip, &match_uplink_teid, &match_service_ip)
228221
if err != nil {
229222
return m, err
230223
}
@@ -271,14 +264,9 @@ func (db *Database) GetRules(ctx context.Context) (jsonapi.RuleMap, error) {
271264
if err != nil {
272265
return jsonapi.RuleMap{}, err
273266
}
274-
nh, err := jsonapi.NewNextHop(action_next_hop)
275-
if err != nil {
276-
return jsonapi.RuleMap{}, err
277-
}
278267

279268
rule.Action = jsonapi.Action{
280-
NextHop: *nh,
281-
SRH: *srh,
269+
SRH: *srh,
282270
}
283271
m[uuid] = rule
284272
}
@@ -327,44 +315,34 @@ func (db *Database) DeleteRule(ctx context.Context, uuid uuid.UUID) error {
327315
}
328316

329317
func (db *Database) GetUplinkAction(ctx context.Context, uplinkTeid uint32, gnbIp netip.Addr, ueIp netip.Addr, serviceIp netip.Addr) (jsonapi.Action, error) {
330-
var action_next_hop string
331318
var action_srh []string
332319
if stmt, ok := db.stmt["get_uplink_action"]; ok {
333-
err := stmt.QueryRowContext(ctx, uplinkTeid, gnbIp.String(), ueIp.String(), serviceIp.String()).Scan(&action_next_hop, pq.Array(&action_srh))
320+
err := stmt.QueryRowContext(ctx, uplinkTeid, gnbIp.String(), ueIp.String(), serviceIp.String()).Scan(pq.Array(&action_srh))
334321
if err != nil {
335322
return jsonapi.Action{}, err
336323
}
337324
srh, err := jsonapi.NewSRH(action_srh)
338325
if err != nil {
339326
return jsonapi.Action{}, err
340327
}
341-
nh, err := jsonapi.NewNextHop(action_next_hop)
342-
if err != nil {
343-
return jsonapi.Action{}, err
344-
}
345-
return jsonapi.Action{NextHop: *nh, SRH: *srh}, err
328+
return jsonapi.Action{SRH: *srh}, err
346329
} else {
347330
return jsonapi.Action{}, fmt.Errorf("Procedure not registered")
348331
}
349332
}
350333

351334
func (db *Database) GetDownlinkAction(ctx context.Context, ueIp netip.Addr) (jsonapi.Action, error) {
352-
var action_next_hop string
353335
var action_srh []string
354336
if stmt, ok := db.stmt["get_downlink_action"]; ok {
355-
err := stmt.QueryRowContext(ctx, ueIp.String()).Scan(&action_next_hop, pq.Array(&action_srh))
337+
err := stmt.QueryRowContext(ctx, ueIp.String()).Scan(pq.Array(&action_srh))
356338
if err != nil {
357339
return jsonapi.Action{}, err
358340
}
359341
srh, err := jsonapi.NewSRH(action_srh)
360342
if err != nil {
361343
return jsonapi.Action{}, err
362344
}
363-
nh, err := jsonapi.NewNextHop(action_next_hop)
364-
if err != nil {
365-
return jsonapi.Action{}, err
366-
}
367-
return jsonapi.Action{NextHop: *nh, SRH: *srh}, err
345+
return jsonapi.Action{SRH: *srh}, err
368346
} else {
369347
return jsonapi.Action{}, fmt.Errorf("Procedure not registered")
370348
}

internal/database/database.sql

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ CREATE TABLE IF NOT EXISTS rule (
77
uuid UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
88
type_uplink BOOL NOT NULL,
99
enabled BOOL NOT NULL,
10-
action_next_hop INET NOT NULL,
1110
action_srh INET ARRAY NOT NULL,
1211
match_ue_ip CIDR NOT NULL,
1312
match_gnb_ip CIDR,
@@ -20,24 +19,24 @@ CREATE OR REPLACE PROCEDURE insert_uplink_rule(
2019
IN in_enabled BOOL, IN in_ue_ip CIDR,
2120
IN in_gnb_ip CIDR, IN in_uplink_teid INTEGER,
2221
IN in_service_ip CIDR,
23-
IN in_next_hop INET, IN in_srh INET ARRAY,
22+
IN in_srh INET ARRAY,
2423
OUT out_uuid UUID
2524
)
2625
LANGUAGE plpgsql AS $$
2726
BEGIN
28-
INSERT INTO rule(type_uplink, enabled, match_ue_ip, match_gnb_ip, match_uplink_teid, match_service_ip, action_next_hop, action_srh)
29-
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;
27+
INSERT INTO rule(type_uplink, enabled, match_ue_ip, match_gnb_ip, match_uplink_teid, match_service_ip, action_srh)
28+
VALUES(TRUE, in_enabled, in_ue_ip, in_gnb_ip, in_uplink_teid, in_service_ip, in_srh) RETURNING rule.uuid INTO out_uuid;
3029
END;$$;
3130

3231
CREATE OR REPLACE PROCEDURE insert_downlink_rule(
3332
IN in_enabled BOOL, IN in_ue_ip CIDR,
34-
IN in_next_hop INET, IN in_srh INET ARRAY,
33+
IN in_srh INET ARRAY,
3534
OUT out_uuid UUID
3635
)
3736
LANGUAGE plpgsql AS $$
3837
BEGIN
39-
INSERT INTO rule(type_uplink, enabled, match_ue_ip, action_next_hop, action_srh)
40-
VALUES(FALSE, in_enabled, in_ue_ip, in_next_hop, in_srh) RETURNING rule.uuid INTO out_uuid;
38+
INSERT INTO rule(type_uplink, enabled, match_ue_ip, action_srh)
39+
VALUES(FALSE, in_enabled, in_ue_ip, in_srh) RETURNING rule.uuid INTO out_uuid;
4140
END;$$;
4241

4342

@@ -80,12 +79,11 @@ CREATE OR REPLACE FUNCTION get_uplink_action(
8079
IN in_ue_ip INET, IN in_service_ip INET
8180
)
8281
RETURNS TABLE (
83-
t_action_next_hop INET,
8482
t_action_srh INET ARRAY
8583
)
8684
AS $$
8785
BEGIN
88-
RETURN QUERY SELECT rule.action_next_hop AS "t_action_next_hop", rule.action_srh AS "t_action_srh"
86+
RETURN QUERY SELECT rule.action_srh AS "t_action_srh"
8987
FROM rule
9088
WHERE (rule.match_uplink_teid = in_uplink_teid
9189
AND rule.match_gnb_ip && in_gnb_ip
@@ -98,12 +96,11 @@ CREATE OR REPLACE FUNCTION get_downlink_action(
9896
IN in_ue_ip_address INET
9997
)
10098
RETURNS TABLE (
101-
t_action_next_hop INET,
10299
t_action_srh INET ARRAY
103100
)
104101
AS $$
105102
BEGIN
106-
RETURN QUERY SELECT rule.action_next_hop AS "t_action_next_hop", rule.action_srh AS "t_action_srh"
103+
RETURN QUERY SELECT rule.action_srh AS "t_action_srh"
107104
FROM rule
108105
WHERE (rule.type_uplink = FALSE AND rule.enabled = TRUE
109106
AND match_ue_ip && in_ue_ip_address);
@@ -115,7 +112,6 @@ CREATE OR REPLACE FUNCTION get_rule(
115112
RETURNS TABLE (
116113
t_type_uplink BOOL,
117114
t_enabled BOOL,
118-
t_action_next_hop INET,
119115
t_action_srh INET ARRAY,
120116
t_match_ue_ip CIDR,
121117
t_match_gnb_ip CIDR,
@@ -124,7 +120,7 @@ RETURNS TABLE (
124120
)
125121
AS $$
126122
BEGIN
127-
RETURN QUERY SELECT type_uplink AS "t_type_uplink", enabled AS "t_enabled", action_next_hop AS "t_action_next_hop",
123+
RETURN QUERY SELECT type_uplink AS "t_type_uplink", enabled AS "t_enabled",
128124
action_srh AS "t_action_srh", match_ue_ip AS "t_match_ue_ip", match_gnb_ip AS "t_match_gnb_ip",
129125
match_uplink_teid AS "t_match_uplink_teid", match_service_ip AS "t_match_service_ip"
130126
FROM rule
@@ -136,7 +132,6 @@ RETURNS TABLE (
136132
t_uuid UUID,
137133
t_type_uplink BOOL,
138134
t_enabled BOOL,
139-
t_action_next_hop INET,
140135
t_action_srh INET ARRAY,
141136
t_match_ue_ip CIDR,
142137
t_match_gnb_ip CIDR,
@@ -146,7 +141,7 @@ RETURNS TABLE (
146141
AS $$
147142
BEGIN
148143
RETURN QUERY SELECT uuid AS "t_uuid", type_uplink AS "t_type_uplink",
149-
enabled AS "t_enabled", action_next_hop AS "t_action_next_hop",
144+
enabled AS "t_enabled",
150145
action_srh AS "t_action_srh", match_ue_ip AS "t_match_ue_ip", match_gnb_ip AS "t_match_gnb_ip",
151146
match_uplink_teid AS "t_match_uplink_teid", match_service_ip AS "t_match_service_ip"
152147
FROM rule;

internal/database/database_gen.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/netfunc/headend-encaps-ctrl.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package netfunc
77
import (
88
"context"
99
"fmt"
10-
"net"
1110
"net/netip"
1211

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

55-
nextHop := action.NextHop.AsSlice()
54+
segs := action.SRH.AsSlice()
5655
ipheader := &layers.IPv6{
5756
SrcIP: src,
5857
// S06. Set the IPv6 DA = B
59-
DstIP: nextHop,
58+
DstIP: segs[len(segs)-1],
6059
Version: 6,
6160
NextHeader: layers.IPProtocolIPv6Routing, // IPv6-Route
6261
HopLimit: h.HopLimit(),
6362
// TODO: Generate a FlowLabel with hash(IPv6SA + IPv6DA + policy)
6463
TrafficClass: 0, // FIXME: put this in Action
6564
}
66-
segList := []net.IP{}
67-
for _, seg := range action.SRH {
68-
segList = append(segList, seg.AsSlice())
69-
}
70-
segList = append(segList, nextHop)
7165

7266
srh := &gopacket_srv6.IPv6Routing{
7367
RoutingType: 4,
7468
// the first item on segments list is the next endpoint
75-
SegmentsLeft: uint8(len(segList) - 1), // pointer to next segment
76-
SourceRoutingIPs: segList,
69+
SegmentsLeft: uint8(len(segs) - 1), // pointer to next segment
70+
SourceRoutingIPs: segs,
7771
Tag: 0, // not used
7872
Flags: 0, // no flag defined
7973
GopacketIpv6ExtensionBase: gopacket_srv6.GopacketIpv6ExtensionBase{

internal/netfunc/headend-gtp4-ctrl.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"context"
99
"encoding/binary"
1010
"fmt"
11-
"net"
1211
"net/netip"
1312

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

8988
src, err := ipv6SA.Marshal()
89+
segs := action.SRH.AsSlice()
9090
if err != nil {
9191
return nil, fmt.Errorf("Error during serialization of IPv6 SA: %w", err)
9292
}
93-
nextHop := action.NextHop.AsSlice()
94-
9593
ipheader := &layers.IPv6{
9694
SrcIP: src,
9795
// S06. Set the IPv6 DA = B
98-
DstIP: nextHop,
96+
DstIP: segs[len(segs)-1],
9997
Version: 6,
10098
NextHeader: layers.IPProtocolIPv6Routing, // IPv6-Route
10199
HopLimit: h.HopLimit(),
102100
// TODO: Generate a FlowLabel with hash(IPv6SA + IPv6DA + policy)
103101
//TrafficClass: qfi << 2,
104102
TrafficClass: 0, // FIXME
105103
}
106-
segList := []net.IP{}
107-
for _, seg := range action.SRH {
108-
segList = append(segList, seg.AsSlice())
109-
}
110-
segList = append(segList, nextHop)
111104
srh := &gopacket_srv6.IPv6Routing{
112105
RoutingType: 4,
113106
// the first item on segments list is the next endpoint
114-
SegmentsLeft: uint8(len(segList) - 1), // pointer to next segment
115-
SourceRoutingIPs: segList,
107+
SegmentsLeft: uint8(len(segs) - 1), // pointer to next segment
108+
SourceRoutingIPs: segs,
116109
Tag: 0, // not used
117110
Flags: 0, // no flag defined
118111
GopacketIpv6ExtensionBase: gopacket_srv6.GopacketIpv6ExtensionBase{

0 commit comments

Comments
 (0)