Skip to content

Commit 7cb659a

Browse files
authored
Remove AmfRan from pool on SCTP connection close (#748)
* Remove AmfRan from pool on SCTP connection close Signed-off-by: Arrobo, Gabriel <gabriel.arrobo@intel.com> * Create patch release Signed-off-by: Arrobo, Gabriel <gabriel.arrobo@intel.com> * Address Copilot's comments Signed-off-by: Arrobo, Gabriel <gabriel.arrobo@intel.com> --------- Signed-off-by: Arrobo, Gabriel <gabriel.arrobo@intel.com>
1 parent 9a9e44b commit 7cb659a

4 files changed

Lines changed: 56 additions & 3 deletions

File tree

‎VERSION‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.1.2-dev
1+
3.1.2

‎ngap/dispatcher.go‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,15 @@ func Dispatch(conn net.Conn, msg []byte) {
144144

145145
ran, ok := amfSelf.AmfRanFindByConn(conn)
146146
if !ok {
147-
logger.NgapLog.Infof("Create a new NG connection for: %s", conn.RemoteAddr().String())
147+
if len(msg) == 0 {
148+
// No existing RAN context for this connection: either the connection
149+
// closed before any NGAP message arrived, or an SCTP notification
150+
// already triggered ran.Remove(). Skip creating a transient context.
151+
logger.NgapLog.Debugf("RAN context not found for closing connection[addr: %+v], nothing to remove",
152+
conn.RemoteAddr())
153+
return
154+
}
155+
logger.NgapLog.Infof("Create a new NG connection for: %+v", conn.RemoteAddr())
148156
ran = amfSelf.NewAmfRan(conn)
149157
}
150158

‎ngap/dispatcher_test.go‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,31 @@ package ngap
77

88
import (
99
ctxt "context"
10+
"net"
1011
"testing"
12+
"time"
1113

1214
"github.com/omec-project/amf/context"
1315
"github.com/omec-project/amf/protos/sdcoreAmfServer"
1416
)
1517

18+
// mockConn is a minimal net.Conn used to exercise Dispatch without a real SCTP socket.
19+
type mockConn struct{}
20+
21+
type mockAddr struct{}
22+
23+
func (a mockAddr) Network() string { return "sctp" }
24+
func (a mockAddr) String() string { return "192.0.2.1:38412" }
25+
26+
func (c *mockConn) RemoteAddr() net.Addr { return mockAddr{} }
27+
func (c *mockConn) LocalAddr() net.Addr { return mockAddr{} }
28+
func (c *mockConn) Read(_ []byte) (int, error) { return 0, net.ErrClosed }
29+
func (c *mockConn) Write(_ []byte) (int, error) { return 0, net.ErrClosed }
30+
func (c *mockConn) Close() error { return nil }
31+
func (c *mockConn) SetDeadline(_ time.Time) error { return nil }
32+
func (c *mockConn) SetReadDeadline(_ time.Time) error { return nil }
33+
func (c *mockConn) SetWriteDeadline(_ time.Time) error { return nil }
34+
1635
func TestDispatchLbIgnoresMissingRanContext(t *testing.T) {
1736
msg := &sdcoreAmfServer.SctplbMessage{
1837
Msg: []byte{0x00},
@@ -38,3 +57,23 @@ func TestDispatchNgapMsgIgnoresNilPdu(t *testing.T) {
3857

3958
DispatchNgapMsg(ctxt.Background(), ran, nil, nil)
4059
}
60+
61+
// TestDispatchEmptyMsgUnknownConnDoesNotPanic verifies that Dispatch does not panic
62+
// and does not create an AmfRan entry when it receives an empty (connection-close)
63+
// message for a connection that has no existing RAN context.
64+
func TestDispatchEmptyMsgUnknownConnDoesNotPanic(t *testing.T) {
65+
conn := &mockConn{}
66+
67+
defer func() {
68+
if r := recover(); r != nil {
69+
t.Fatalf("Dispatch panicked with empty message and unknown conn: %v", r)
70+
}
71+
}()
72+
73+
Dispatch(conn, nil)
74+
75+
// Confirm that no AmfRan was created in the pool for this unknown connection.
76+
if _, ok := context.AMF_Self().AmfRanFindByConn(conn); ok {
77+
t.Error("Dispatch must not create an AmfRan for an unknown connection with an empty message")
78+
}
79+
}

‎ngap/service/service.go‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func listenAndServe(addr *sctp.SCTPAddr, handler NGAPHandler) {
165165
logger.NgapLog.Debugf("set read timeout: %+v", readTimeout)
166166
}
167167

168-
logger.NgapLog.Infof("[AMF] SCTP Accept from: %s", newConn.RemoteAddr().String())
168+
logger.NgapLog.Infof("[AMF] SCTP Accept from: %+v", newConn.RemoteAddr())
169169
connections.Store(newConn, true)
170170

171171
go handleConnection(newConn, readBufSize, handler)
@@ -196,6 +196,12 @@ func handleConnection(conn *sctp.SCTPConn, bufsize uint32, handler NGAPHandler)
196196
defer func() {
197197
connections.Delete(conn)
198198

199+
// Notify the NGAP dispatcher that this RAN connection has closed so that
200+
// its AmfRan entry is removed from AmfRanPool
201+
if handler.HandleMessage != nil {
202+
handler.HandleMessage(conn, nil)
203+
}
204+
199205
// if AMF call Stop(), then conn.Close() will return EBADF because conn has been closed inside Stop()
200206
if err := conn.Close(); err != nil && err != syscall.EBADF {
201207
logger.NgapLog.Errorf("close connection error: %+v", err)

0 commit comments

Comments
 (0)