-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Paging netlink #23
Merged
Merged
Paging netlink #23
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
323fd3a
Use netlink sockets instead of Unix domain sockets for buffered packets
ShouheiNishi 6a6316c
Move some definitions to go-gtp5gnl
ShouheiNishi e853494
update go-gtp5gnl and fix ci fail
tim-ywliu 4e61521
Merge branch 'main' into paging-netlink
tim-ywliu b0b2991
Fix golangci-lint errors
ShouheiNishi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package buffnetlink | ||
|
||
import ( | ||
"encoding/binary" | ||
"sync" | ||
"syscall" | ||
|
||
"github.com/khirono/go-genl" | ||
"github.com/khirono/go-nl" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/free5gc/go-gtp5gnl" | ||
"github.com/free5gc/go-upf/internal/report" | ||
) | ||
|
||
type Server struct { | ||
client *nl.Client | ||
mux *nl.Mux | ||
conn *nl.Conn | ||
handler report.Handler | ||
} | ||
|
||
var native binary.ByteOrder = gtp5gnl.NativeEndian() | ||
|
||
func OpenServer(wg *sync.WaitGroup, client *nl.Client, mux *nl.Mux) (*Server, error) { | ||
s := &Server{ | ||
client: client, | ||
mux: mux, | ||
} | ||
|
||
f, err := genl.GetFamily(s.client, "gtp5g") | ||
if err != nil { | ||
return nil, errors.Wrap(err, "get family") | ||
} | ||
|
||
s.conn, err = nl.Open(syscall.NETLINK_GENERIC, int(f.Groups[gtp5gnl.GENL_MCGRP].ID)) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "open netlink") | ||
} | ||
|
||
err = s.mux.PushHandler(s.conn, s) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "push handler") | ||
} | ||
|
||
// wg.Add(1) | ||
return s, nil | ||
} | ||
|
||
func (s *Server) Close() { | ||
s.mux.PopHandler(s.conn) | ||
s.conn.Close() | ||
} | ||
|
||
func (s *Server) Handle(handler report.Handler) { | ||
s.handler = handler | ||
} | ||
|
||
func (s *Server) ServeMsg(msg *nl.Msg) bool { | ||
b := msg.Body[genl.SizeofHeader:] | ||
|
||
var pkt []byte | ||
var seid uint64 | ||
var pdrid uint16 | ||
var action uint16 | ||
|
||
for len(b) > 0 { | ||
hdr, n, err := nl.DecodeAttrHdr(b) | ||
if err != nil { | ||
return false | ||
} | ||
switch hdr.MaskedType() { | ||
case gtp5gnl.BUFFER_ID: | ||
pdrid = native.Uint16(b[n:]) | ||
case gtp5gnl.BUFFER_ACTION: | ||
action = native.Uint16(b[n:]) | ||
case gtp5gnl.BUFFER_SEID: | ||
seid = native.Uint64(b[n:]) | ||
case gtp5gnl.BUFFER_PACKET: | ||
pkt = b[n:int(hdr.Len)] | ||
} | ||
b = b[hdr.Len.Align():] | ||
} | ||
|
||
if s.handler != nil && pkt != nil { | ||
dldr := report.DLDReport{ | ||
PDRID: pdrid, | ||
Action: action, | ||
BufPkt: pkt, | ||
} | ||
s.handler.NotifySessReport( | ||
report.SessReport{ | ||
SEID: seid, | ||
Reports: []report.Report{dldr}, | ||
}, | ||
) | ||
} | ||
|
||
return true | ||
} | ||
|
||
func (s *Server) Pop(seid uint64, pdrid uint16) ([]byte, bool) { | ||
return s.handler.PopBufPkt(seid, pdrid) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
package buffnetlink | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"fmt" | ||
"os" | ||
"sync" | ||
"syscall" | ||
"testing" | ||
"time" | ||
|
||
"github.com/khirono/go-genl" | ||
"github.com/khirono/go-nl" | ||
|
||
"github.com/free5gc/go-upf/internal/report" | ||
) | ||
|
||
type testHandler struct { | ||
q map[uint64]map[uint16]chan []byte | ||
} | ||
|
||
func NewTestHandler() *testHandler { | ||
return &testHandler{q: make(map[uint64]map[uint16]chan []byte)} | ||
} | ||
|
||
func (h *testHandler) Close() { | ||
for _, s := range h.q { | ||
for _, q := range s { | ||
close(q) | ||
} | ||
} | ||
} | ||
|
||
func (h *testHandler) NotifySessReport(sr report.SessReport) { | ||
s, ok := h.q[sr.SEID] | ||
if !ok { | ||
return | ||
} | ||
for _, rep := range sr.Reports { | ||
switch r := rep.(type) { | ||
case report.DLDReport: | ||
if r.Action&report.BUFF != 0 && len(r.BufPkt) > 0 { | ||
q, ok := s[r.PDRID] | ||
if !ok { | ||
qlen := 10 | ||
s[r.PDRID] = make(chan []byte, qlen) | ||
q = s[r.PDRID] | ||
} | ||
q <- r.BufPkt | ||
} | ||
default: | ||
} | ||
} | ||
} | ||
|
||
func (h *testHandler) PopBufPkt(seid uint64, pdrid uint16) ([]byte, bool) { | ||
s, ok := h.q[seid] | ||
if !ok { | ||
return nil, false | ||
} | ||
q, ok := s[pdrid] | ||
if !ok { | ||
return nil, false | ||
} | ||
select { | ||
case pkt := <-q: | ||
return pkt, true | ||
default: | ||
return nil, false | ||
} | ||
} | ||
|
||
func TestServer(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping testing in short mode") | ||
} | ||
|
||
var wg sync.WaitGroup | ||
|
||
mux, err := nl.NewMux() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
err = mux.Serve() | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "%v", err) | ||
} | ||
}() | ||
|
||
conn, err := nl.Open(syscall.NETLINK_GENERIC) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
c := nl.NewClient(conn, mux) | ||
s, err := OpenServer(&wg, c, mux) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
f, err := genl.GetFamily(c, "gtp5g") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
conn.Close() | ||
|
||
h := NewTestHandler() | ||
defer func() { | ||
h.Close() | ||
s.Close() | ||
mux.Close() | ||
wg.Wait() | ||
}() | ||
|
||
fd, err := syscall.Socket(syscall.AF_NETLINK, syscall.SOCK_RAW|syscall.SOCK_CLOEXEC, syscall.NETLINK_GENERIC) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer func() { | ||
errClose := syscall.Close(fd) | ||
if errClose != nil { | ||
t.Fatal(errClose) | ||
} | ||
}() | ||
|
||
seid := uint64(6) | ||
h.q[seid] = make(map[uint16]chan []byte) | ||
s.Handle(h) | ||
|
||
pkt := []byte{ | ||
0x00, 0x00, 0x00, 0x00, | ||
0x00, 0x00, | ||
0x00, 0x00, | ||
0x00, 0x00, 0x00, 0x00, | ||
0x00, 0x00, 0x00, 0x00, | ||
0x10, | ||
0x00, | ||
0x00, 0x00, | ||
0x0c, 0x00, 0x06, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x03, 0x00, | ||
0x06, 0x00, 0x05, 0x00, 0x03, 0x00, 0x00, 0x00, | ||
0x06, 0x00, 0x07, 0x00, 0x0c, 0x00, 0x00, 0x00, | ||
0x08, 0x00, 0x04, 0x00, | ||
0xee, 0xbb, | ||
0xdd, 0xcc, | ||
} | ||
|
||
binary.LittleEndian.PutUint16(pkt[4:6], f.ID) | ||
binary.LittleEndian.PutUint32(pkt[0:4], uint32(len(pkt))) | ||
|
||
N := 10 | ||
for i := 0; i < N; i++ { | ||
addr := syscall.SockaddrNetlink{ | ||
Family: syscall.AF_NETLINK, | ||
Groups: 1 << (f.Groups[0].ID - 1), | ||
} | ||
err = syscall.Sendmsg(fd, pkt, nil, &addr, 0) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
time.Sleep(100 * time.Millisecond) | ||
|
||
pdrid := uint16(3) | ||
pkt, ok := s.Pop(seid, pdrid) | ||
if !ok { | ||
t.Fatal("not found") | ||
} | ||
|
||
want := []byte{0xee, 0xbb, 0xdd, 0xcc} | ||
if !bytes.Equal(pkt, want) { | ||
t.Errorf("want %x; but got %x\n", want, pkt) | ||
} | ||
|
||
_, ok = s.Pop(seid, pdrid) | ||
if ok { | ||
t.Fatal("found") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ShouheiNishi
The latest report format is changed.
Please rebase your banch to the latest commit and refer to the latest
internal/forwarder/buff
to modify it to support DLDR & USAR report.Thanks.