open5gs/lib/gtp/gtp_xact.c

354 lines
10 KiB
C
Raw Normal View History

2017-03-26 15:48:33 +00:00
#define TRACE_MODULE _gtp_xact
#include "core_debug.h"
#include "core_pool.h"
2017-03-27 10:12:45 +00:00
#include "core_event.h"
2017-03-26 15:48:33 +00:00
#include "gtp_xact.h"
#define SIZE_OF_GTP_XACT_POOL 32
#define GTP_XACT_NEXT_ID(__id) \
((__id) == 0x800000 ? 1 : ((__id) + 1))
#define GTP_XACT_COMPARE_ID(__id1, __id2) \
((__id2) > (__id1) ? ((__id2) - (__id1) < 0x7fffff ? -1 : 1) : \
(__id1) > (__id2) ? ((__id1) - (__id2) < 0x7fffff ? 1 : -1) : 0)
2017-04-01 14:28:22 +00:00
#define GTP_XACT_LOCAL_DURATION 3000 /* 3 seconds */
#define GTP_XACT_LOCAL_RETRY_COUNT 3
#define GTP_XACT_REMOTE_DURATION \
(GTP_XACT_LOCAL_DURATION * GTP_XACT_LOCAL_RETRY_COUNT) /* 9 seconds */
#define GTP_XACT_REMOTE_RETRY_COUNT 1
2017-04-03 11:50:20 +00:00
/* 5.1 General format */
#define GTPV2C_HEADER_LEN 12
#define GTPV2C_TEID_LEN 4
typedef struct _gtpv2c_header_t {
ED4(c_uint8_t version:3;,
c_uint8_t piggybacked:1;,
c_uint8_t teid_presence:1;,
c_uint8_t spare1:3;)
c_uint8_t type;
c_uint16_t length;
union {
struct {
c_uint32_t teid;
/* sqn : 31bit ~ 8bit, spare : 7bit ~ 0bit */
#define GTP_XID_TO_SQN(__xid) ((__xid) << 8)
#define GTP_SQN_TO_XID(__sqn) ((__sqn) >> 8)
c_uint32_t sqn;
};
/* sqn : 31bit ~ 8bit, spare : 7bit ~ 0bit */
c_uint32_t spare2;
};
} __attribute__ ((packed)) gtpv2c_header_t;
2017-03-28 07:35:57 +00:00
static int gtp_xact_pool_initialized = 0;
2017-03-29 02:54:18 +00:00
pool_declare(gtp_xact_pool, gtp_xact_t, SIZE_OF_GTP_XACT_POOL);
2017-03-26 15:48:33 +00:00
2017-04-01 14:28:22 +00:00
status_t gtp_xact_init(gtp_xact_ctx_t *context,
tm_service_t *tm_service, c_uintptr_t event)
2017-03-26 15:48:33 +00:00
{
2017-03-28 07:35:57 +00:00
if (gtp_xact_pool_initialized == 0)
{
pool_init(&gtp_xact_pool, SIZE_OF_GTP_XACT_POOL);
}
gtp_xact_pool_initialized = 1;
2017-03-26 15:48:33 +00:00
2017-03-28 07:35:57 +00:00
memset(context, 0, sizeof(gtp_xact_ctx_t));
2017-03-26 15:48:33 +00:00
2017-04-01 14:13:15 +00:00
context->g_xact_id = 0;
2017-03-28 07:35:57 +00:00
context->tm_service = tm_service;
context->event = event;;
2017-03-26 15:48:33 +00:00
return CORE_OK;
}
status_t gtp_xact_final(void)
{
2017-03-28 07:35:57 +00:00
if (gtp_xact_pool_initialized == 1)
{
2017-03-30 13:23:47 +00:00
d_print("%d not freed in gtp_xact_pool[%d] of S11/S5-SM\n",
pool_size(&gtp_xact_pool) - pool_avail(&gtp_xact_pool),
pool_size(&gtp_xact_pool));
2017-03-28 07:35:57 +00:00
pool_final(&gtp_xact_pool);
}
gtp_xact_pool_initialized = 0;
2017-03-26 15:48:33 +00:00
return CORE_OK;
}
2017-03-30 12:26:42 +00:00
gtp_xact_t *gtp_xact_create(gtp_xact_ctx_t *context,
2017-03-30 00:20:40 +00:00
net_sock_t *sock, gtp_node_t *gnode, c_uint8_t org, c_uint32_t xid,
2017-04-02 11:53:59 +00:00
c_uint32_t duration, c_uint8_t retry_count)
2017-03-26 15:48:33 +00:00
{
2017-03-29 02:54:18 +00:00
gtp_xact_t *xact = NULL;
2017-03-27 10:12:45 +00:00
2017-04-01 14:13:15 +00:00
d_assert(context, return NULL, "Null param");
d_assert(sock, return NULL, "Null param");
d_assert(gnode, return NULL, "Null param");
2017-03-27 10:12:45 +00:00
2017-03-28 07:35:57 +00:00
pool_alloc_node(&gtp_xact_pool, &xact);
2017-04-01 14:13:15 +00:00
d_assert(xact, return NULL, "Transaction allocation failed");
2017-03-30 00:06:56 +00:00
2017-03-29 02:54:18 +00:00
memset(xact, 0, sizeof(gtp_xact_t));
2017-03-27 10:12:45 +00:00
2017-03-30 00:20:40 +00:00
xact->org = org;
xact->xid = xid;
2017-03-28 07:35:57 +00:00
xact->sock = sock;
xact->gnode = gnode;
2017-03-27 10:12:45 +00:00
2017-03-28 07:35:57 +00:00
xact->tm_wait = event_timer(context->tm_service, context->event,
2017-03-30 13:23:47 +00:00
duration, (c_uintptr_t)xact);
2017-04-01 14:13:15 +00:00
d_assert(xact->tm_wait,
pool_free_node(&gtp_xact_pool, xact); return NULL,
"Timer allocation failed");
2017-03-30 13:23:47 +00:00
xact->retry_count = retry_count;
2017-04-01 14:13:15 +00:00
tm_start(xact->tm_wait);
2017-03-27 10:12:45 +00:00
2017-03-30 00:20:40 +00:00
list_append(xact->org == GTP_LOCAL_ORIGINATOR ?
2017-04-03 06:31:57 +00:00
&xact->gnode->local_list : &xact->gnode->remote_list, xact);
2017-03-27 10:12:45 +00:00
2017-04-02 11:36:58 +00:00
d_trace(1, "[%d]%s Create : xid = 0x%x\n",
2017-03-30 15:15:13 +00:00
gnode->port,
2017-03-30 14:27:26 +00:00
xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
2017-04-02 11:36:58 +00:00
xact->xid);
2017-03-30 14:27:26 +00:00
2017-03-28 07:35:57 +00:00
return xact;
2017-03-26 15:48:33 +00:00
}
2017-04-03 05:18:25 +00:00
gtp_xact_t *gtp_xact_local_create(
gtp_xact_ctx_t *context, net_sock_t *sock, gtp_node_t *gnode)
2017-04-01 14:37:27 +00:00
{
2017-04-03 11:50:20 +00:00
return gtp_xact_create(context, sock, gnode, GTP_LOCAL_ORIGINATOR,
2017-04-02 11:53:59 +00:00
GTP_XACT_NEXT_ID(context->g_xact_id),
2017-04-01 14:37:27 +00:00
GTP_XACT_LOCAL_DURATION, GTP_XACT_LOCAL_RETRY_COUNT);
}
gtp_xact_t *gtp_xact_remote_create(gtp_xact_ctx_t *context,
2017-04-03 04:06:56 +00:00
net_sock_t *sock, gtp_node_t *gnode, c_uint32_t sqn)
2017-04-01 14:37:27 +00:00
{
return gtp_xact_create(context, sock, gnode,
2017-04-03 04:06:56 +00:00
GTP_REMOTE_ORIGINATOR, GTP_SQN_TO_XID(sqn),
2017-04-01 14:37:27 +00:00
GTP_XACT_REMOTE_DURATION, GTP_XACT_REMOTE_RETRY_COUNT);
}
2017-03-29 02:54:18 +00:00
status_t gtp_xact_delete(gtp_xact_t *xact)
2017-03-26 15:48:33 +00:00
{
2017-03-30 00:06:56 +00:00
d_assert(xact, , "Null param");
2017-03-30 15:15:13 +00:00
d_assert(xact->gnode, , "Null param");
2017-04-01 14:13:15 +00:00
d_assert(xact->tm_wait, , "Null param");
2017-03-30 15:15:13 +00:00
2017-04-02 11:36:58 +00:00
d_trace(1, "[%d]%s Delete : xid = 0x%x\n",
2017-03-30 15:15:13 +00:00
xact->gnode->port,
2017-03-30 14:27:26 +00:00
xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
2017-04-02 11:36:58 +00:00
xact->xid);
2017-03-30 14:27:26 +00:00
2017-04-01 14:13:15 +00:00
if (xact->pkbuf)
pkbuf_free(xact->pkbuf);
2017-03-29 14:23:21 +00:00
2017-03-27 10:12:45 +00:00
tm_delete(xact->tm_wait);
2017-04-03 06:31:57 +00:00
list_remove(xact->org == GTP_LOCAL_ORIGINATOR ? &xact->gnode->local_list :
&xact->gnode->remote_list, xact);
2017-03-27 10:12:45 +00:00
pool_free_node(&gtp_xact_pool, xact);
2017-03-26 15:48:33 +00:00
return CORE_OK;
}
2017-04-03 13:09:48 +00:00
status_t gtp_xact_commit(
gtp_xact_t *xact, c_uint8_t type, gtp_message_t *gtp_message)
2017-03-26 15:48:33 +00:00
{
2017-04-03 05:18:25 +00:00
status_t rv;
pkbuf_t *pkbuf = NULL;
gtpv2c_header_t *h = NULL;
d_assert(xact, goto out, "Null param");
d_assert(xact->sock, goto out, "Null param");
d_assert(xact->gnode, goto out, "Null param");
d_assert(gtp_message, goto out, "Null param");
2017-03-29 14:19:31 +00:00
2017-04-02 11:36:58 +00:00
d_trace(1, "[%d]%s Commit : xid = 0x%x\n",
2017-04-01 14:13:15 +00:00
xact->gnode->port,
xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
2017-04-02 11:36:58 +00:00
xact->xid);
2017-04-01 14:13:15 +00:00
2017-04-03 13:09:48 +00:00
rv = gtp_build_msg(&pkbuf, type, gtp_message);
2017-04-03 05:18:25 +00:00
d_assert(rv == CORE_OK, goto out, "gtp build failed");
2017-04-03 12:13:52 +00:00
d_assert(pkbuf, goto out, "Null param");
xact->pkbuf = pkbuf;
2017-04-03 05:18:25 +00:00
pkbuf_header(pkbuf, GTPV2C_HEADER_LEN);
h = pkbuf->payload;
d_assert(h, goto out, "Null param");
h->version = 2;
h->teid_presence = 1;
2017-04-03 13:09:48 +00:00
h->type = type;
2017-04-03 05:18:25 +00:00
h->length = htons(pkbuf->len - 4);
h->sqn = GTP_XID_TO_SQN(xact->xid);
2017-04-01 14:13:15 +00:00
d_assert(gtp_send(xact->sock, xact->gnode, xact->pkbuf) == CORE_OK,
2017-04-03 05:18:25 +00:00
goto out, "gtp_send error");
2017-04-01 14:13:15 +00:00
return CORE_OK;
2017-04-03 05:18:25 +00:00
out:
gtp_xact_delete(xact);
return CORE_ERROR;
2017-04-01 14:13:15 +00:00
}
2017-03-30 15:15:13 +00:00
2017-04-01 14:13:15 +00:00
status_t gtp_xact_timeout(gtp_xact_t *xact)
{
d_assert(xact, goto out, "Null param");
d_assert(xact->sock, goto out, "Null param");
d_assert(xact->gnode, goto out, "Null param");
2017-04-02 11:36:58 +00:00
d_trace(1, "[%d]%s Timeout : xid = 0x%x\n",
2017-03-30 15:15:13 +00:00
xact->gnode->port,
2017-03-30 14:27:26 +00:00
xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
2017-04-02 11:36:58 +00:00
xact->xid);
2017-03-30 14:27:26 +00:00
2017-04-03 04:06:56 +00:00
if (xact->org == GTP_LOCAL_ORIGINATOR)
2017-03-29 12:09:29 +00:00
{
2017-04-01 14:28:22 +00:00
if (--xact->retry_count > 0)
{
tm_start(xact->tm_wait);
d_assert(xact->pkbuf, goto out, "Null param");
d_assert(gtp_send(xact->sock, xact->gnode, xact->pkbuf) == CORE_OK,
goto out, "gtp_send error");
}
else
{
d_warn("[%d]%s No Reponse. Give up",
xact->gnode->port,
xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE");
gtp_xact_delete(xact);
}
2017-03-29 12:09:29 +00:00
}
2017-04-03 04:06:56 +00:00
else
{
gtp_xact_delete(xact);
}
2017-03-29 12:09:29 +00:00
2017-03-26 15:48:33 +00:00
return CORE_OK;
2017-03-30 00:06:56 +00:00
out:
2017-04-01 14:13:15 +00:00
gtp_xact_delete(xact);
2017-03-30 00:06:56 +00:00
return CORE_ERROR;
2017-03-26 15:48:33 +00:00
}
2017-04-03 12:17:08 +00:00
gtp_xact_t *gtp_xact_find(gtp_node_t *gnode, c_uint8_t type, c_uint32_t sqn)
2017-03-26 15:48:33 +00:00
{
2017-03-27 10:12:45 +00:00
c_uint32_t xid;
2017-03-29 02:54:18 +00:00
gtp_xact_t *xact = NULL;
2017-03-27 10:12:45 +00:00
d_assert(gnode, return NULL, "Null param");
2017-04-03 12:17:08 +00:00
switch(type)
2017-03-27 10:12:45 +00:00
{
case GTP_CREATE_SESSION_REQUEST_TYPE:
case GTP_MODIFY_BEARER_REQUEST_TYPE:
case GTP_DELETE_SESSION_REQUEST_TYPE:
case GTP_CREATE_BEARER_REQUEST_TYPE:
case GTP_UPDATE_BEARER_REQUEST_TYPE:
case GTP_DELETE_BEARER_REQUEST_TYPE:
case GTP_RELEASE_ACCESS_BEARERS_REQUEST_TYPE:
case GTP_CREATE_INDIRECT_DATA_FORWARDING_TUNNEL_REQUEST_TYPE:
case GTP_DELETE_INDIRECT_DATA_FORWARDING_TUNNEL_REQUEST_TYPE:
2017-04-03 06:31:57 +00:00
xact = list_first(&gnode->remote_list);
2017-03-27 10:12:45 +00:00
break;
case GTP_CREATE_SESSION_RESPONSE_TYPE:
case GTP_MODIFY_BEARER_RESPONSE_TYPE:
case GTP_DELETE_SESSION_RESPONSE_TYPE:
case GTP_CREATE_BEARER_RESPONSE_TYPE:
case GTP_UPDATE_BEARER_RESPONSE_TYPE:
case GTP_DELETE_BEARER_RESPONSE_TYPE:
case GTP_RELEASE_ACCESS_BEARERS_RESPONSE_TYPE:
case GTP_CREATE_INDIRECT_DATA_FORWARDING_TUNNEL_RESPONSE_TYPE:
case GTP_DELETE_INDIRECT_DATA_FORWARDING_TUNNEL_RESPONSE_TYPE:
2017-04-03 06:31:57 +00:00
xact = list_first(&gnode->local_list);
2017-03-27 10:12:45 +00:00
break;
default:
2017-04-03 12:17:08 +00:00
d_error("Not implemented GTPv2 Message Type(%d)", type);
2017-03-27 10:12:45 +00:00
return NULL;
}
2017-04-03 12:17:08 +00:00
xid = GTP_SQN_TO_XID(sqn);
2017-03-27 10:12:45 +00:00
while(xact)
{
if (xact->xid == xid)
break;
xact = list_next(xact);
}
2017-03-30 14:27:26 +00:00
if (xact)
{
2017-04-02 11:36:58 +00:00
d_trace(1, "[%d]%s Find : xid = 0x%x\n",
2017-03-30 15:15:13 +00:00
gnode->port,
2017-03-30 14:27:26 +00:00
xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
2017-04-02 11:36:58 +00:00
xact->xid);
2017-03-30 14:27:26 +00:00
}
2017-03-27 10:12:45 +00:00
return xact;
2017-03-26 15:48:33 +00:00
}
2017-03-29 14:19:31 +00:00
2017-04-02 11:51:12 +00:00
gtp_xact_t *gtp_xact_recv(
gtp_xact_ctx_t *context, net_sock_t *sock, gtp_node_t *gnode,
gtp_message_t *gtp_message, pkbuf_t *pkbuf)
2017-03-29 14:19:31 +00:00
{
gtpv2c_header_t *h = NULL;
2017-04-03 06:48:43 +00:00
gtp_xact_t *xact = NULL;
2017-03-29 14:19:31 +00:00
2017-04-03 06:48:43 +00:00
d_assert(context, return NULL, "Null param");
d_assert(sock, return NULL, "Null param");
d_assert(gnode, return NULL, "Null param");
d_assert(gtp_message, return NULL, "Null param");
d_assert(pkbuf, return NULL, "Null param");
2017-03-29 14:19:31 +00:00
h = pkbuf->payload;
2017-04-03 06:48:43 +00:00
d_assert(h, return NULL, "Null param");
2017-03-30 14:27:26 +00:00
2017-04-03 12:17:08 +00:00
xact = gtp_xact_find(gnode, h->type, h->sqn);
2017-03-29 14:19:31 +00:00
if (!xact)
{
2017-04-03 04:06:56 +00:00
xact = gtp_xact_remote_create(context, sock, gnode, h->sqn);
2017-03-29 14:19:31 +00:00
}
2017-04-03 06:48:43 +00:00
d_assert(xact, return NULL, "Null param");
if (xact->org == GTP_LOCAL_ORIGINATOR)
{
2017-04-03 12:37:20 +00:00
gtp_xact_t *assoc_xact = xact->assoc_xact;
gtp_xact_delete(xact);
xact = assoc_xact;
2017-04-03 06:48:43 +00:00
}
else
{
if (xact->pkbuf)
{
2017-04-03 11:50:20 +00:00
pkbuf_free(pkbuf);
2017-04-03 06:48:43 +00:00
d_warn("[%d]%s Request Duplicated. Retransmit!",
xact->gnode->port,
xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE");
d_assert(gtp_send(xact->sock, xact->gnode, xact->pkbuf)
2017-04-03 11:50:20 +00:00
== CORE_OK, return NULL, "gtp_send error");
return xact;
2017-04-03 06:48:43 +00:00
}
}
2017-03-29 14:19:31 +00:00
if (h->teid_presence)
pkbuf_header(pkbuf, -GTPV2C_HEADER_LEN);
else
pkbuf_header(pkbuf, -(GTPV2C_HEADER_LEN-GTPV2C_TEID_LEN));
2017-04-03 13:09:48 +00:00
d_assert(gtp_parse_msg(gtp_message, h->type, pkbuf) == CORE_OK,
2017-04-03 06:48:43 +00:00
return NULL, "parse error");
2017-03-30 13:23:47 +00:00
2017-03-29 14:19:31 +00:00
return xact;
}