Skip to content

Commit 9716106

Browse files
committed
Add dp_session_query function
Add support query session information from pipeline plugin. Signed-off-by: Dung Man <[email protected]>
1 parent b93a3c7 commit 9716106

File tree

10 files changed

+207
-1
lines changed

10 files changed

+207
-1
lines changed

include/dp_session.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,60 @@ enum dp_session_state {
6565
SESSION_STATE_CLOSED,
6666
} __attribute__ ((__packed__));
6767

68+
/**
69+
* Session attribute.
70+
*/
71+
enum dp_session_attr {
72+
SESSION_ATTR_BYTES_IN = 1,
73+
SESSION_ATTR_PKTS_IN = (1 << 1),
74+
SESSION_ATTR_PROTOCOL = (1 << 2),
75+
SESSION_ATTR_TCP_FLAGS = (1 << 3),
76+
SESSION_ATTR_L4_SRC_PORT = (1 << 4),
77+
SESSION_ATTR_IPV4_SRC_ADDR = (1 << 5),
78+
SESSION_ATTR_L4_DST_PORT = (1 << 6),
79+
SESSION_ATTR_IPV4_DST_ADDR = (1 << 7),
80+
SESSION_ATTR_CREATE_TIME = (1 << 8),
81+
SESSION_ATTR_BYTES_OUT = (1 << 9),
82+
SESSION_ATTR_PKTS_OUT = (1 << 10),
83+
SESSION_ATTR_IF_NAME = (1 << 11),
84+
SESSION_ATTR_DPI = (1 << 12),
85+
};
86+
87+
#define SESSION_ATTR_ALL 0xffffffff
88+
#define SESSION_ATTR_SENTRY (SESSION_ATTR_L4_SRC_PORT \
89+
| SESSION_ATTR_IPV4_SRC_ADDR \
90+
| SESSION_ATTR_L4_DST_PORT \
91+
| SESSION_ATTR_IPV4_DST_ADDR \
92+
| SESSION_ATTR_IF_NAME)
93+
94+
struct dp_session_info {
95+
enum dp_session_attr query;
96+
uint64_t se_id;
97+
uint16_t se_flags;
98+
uint8_t se_protocol;
99+
uint8_t se_protocol_state;
100+
uint64_t se_pkts_in;
101+
uint64_t se_bytes_in;
102+
uint64_t se_create_time; /* time session was created */
103+
uint64_t se_pkts_out;
104+
uint64_t se_bytes_out;
105+
106+
// address
107+
int se_af;
108+
uint16_t se_src_port;
109+
uint32_t se_src_addr;
110+
uint16_t se_dst_port;
111+
uint32_t se_dst_addr;
112+
const char *se_ifname;
113+
const char *se_app_name;
114+
const char *se_app_proto;
115+
const char *se_app_type;
116+
117+
// misc
118+
time_t timestamp;
119+
uint64_t duration; /* seconds */
120+
};
121+
68122
#define SESSION_STATE_FIRST SESSION_STATE_NONE
69123
#define SESSION_STATE_LAST SESSION_STATE_CLOSED
70124
#define SESSION_STATE_SIZE (SESSION_STATE_LAST + 1)
@@ -233,6 +287,12 @@ void *dp_session_get_private(int id, const struct session *session);
233287
int dp_session_table_walk(dp_session_walk_t *fn, void *data,
234288
unsigned int types);
235289

290+
/**
291+
* Query a session's info.
292+
*/
293+
int dp_session_query(struct session *s, enum dp_session_attr query,
294+
struct dp_session_info *info);
295+
236296
/**
237297
* Get a session's unique id.
238298
*

src/npf/dpi/dpi.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,22 @@ dpi_app_type_name_to_id(uint8_t engine_id, const char *type_name)
523523
return engine ? engine->type_to_id(type_name) : DPI_APP_ERROR;
524524
}
525525

526+
const char*
527+
dpi_app_id_to_name(uint8_t engine_id, uint32_t app)
528+
{
529+
struct dpi_engine_procs *engine = NULL_ENGINE;
530+
ENGINE_PROC_FIND(engine, engine_id, appid_to_name);
531+
return engine ? engine->appid_to_name(app) : NULL;
532+
}
533+
534+
const char*
535+
dpi_app_type_to_name(uint8_t engine_id, uint32_t type)
536+
{
537+
struct dpi_engine_procs *engine = NULL_ENGINE;
538+
ENGINE_PROC_FIND(engine, engine_id, apptype_to_name);
539+
return engine ? engine->apptype_to_name(type) : NULL;
540+
}
541+
526542
void
527543
dpi_info_json(struct dpi_flow *dpi_flow, json_writer_t *json)
528544
{

src/npf/dpi/dpi_internal.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,21 @@ struct dpi_engine_procs {
404404
*/
405405
size_t (*info_log)(struct dpi_engine_flow *flow, char *buf,
406406
size_t buf_len);
407+
408+
/**
409+
* Get name of app id.
410+
*/
411+
const char* (*appid_to_name)(uint32_t app);
412+
413+
/**
414+
* Get type of type id.
415+
*/
416+
const char* (*apptype_to_name)(uint32_t type);
407417
};
408418

419+
const char *dpi_app_id_to_name(uint8_t engine_id, uint32_t app);
420+
const char *dpi_app_type_to_name(uint8_t engine_id, uint32_t type);
421+
409422

410423
bool no_app_id(uint32_t app_id);
411424
bool no_app_type(uint32_t app_type);

src/npf/dpi/dpi_user.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,4 +317,6 @@ struct dpi_engine_procs user_engine_procs = {
317317
.type_to_id = dpi_user_type_to_id,
318318
.info_json = dpi_user_flow_json,
319319
.info_log = dpi_user_flow_log,
320+
.appid_to_name = dpi_user_id_to_name,
321+
.apptype_to_name = dpi_user_type_to_name,
320322
};

src/npf/dpi/ndpi.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,4 +537,6 @@ struct dpi_engine_procs ndpi_engine_procs = {
537537
.type_to_id = dpi_ndpi_app_type_name_to_id,
538538
.info_json = dpi_ndpi_info_json,
539539
.info_log = dpi_ndpi_info_log,
540+
.appid_to_name = dpi_ndpi_app_id_to_name,
541+
.apptype_to_name = dpi_ndpi_app_type_to_name,
540542
};

src/npf/npf_dataplane_session.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,20 @@ static int dps_feature_nat_info(void *data, uint32_t *taddr, uint16_t *tport)
113113
return npf_session_feature_nat_info(se, taddr, tport);
114114
}
115115

116+
static void dps_feature_query(struct dp_session_info *info, struct session *s,
117+
struct session_feature *sf)
118+
{
119+
return npf_session_feature_query(info, s, sf);
120+
}
121+
116122
/* Callbacks for the npf_session_t */
117123
static const struct session_feature_ops ops = {
118124
.expired = dps_feature_expire,
119125
.destroy = dps_feature_destroy,
120126
.json = dps_feature_json,
121127
.log = dps_feature_log,
122128
.nat_info = dps_feature_nat_info,
129+
.query = dps_feature_query,
123130
};
124131

125132
/*

src/npf/npf_session.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,3 +2089,28 @@ int npf_session_npf_pack_activate(struct npf_session *se, struct ifnet *ifp)
20892089
se->s_flags |= SE_ACTIVE;
20902090
return 0;
20912091
}
2092+
2093+
static int get_dpi_info(uint8_t engine, uint32_t app, uint32_t proto,
2094+
uint32_t type, void *data)
2095+
{
2096+
if (no_app_id(app) && no_app_id(proto) && no_app_type(type))
2097+
return 0;
2098+
2099+
struct dp_session_info *p = data;
2100+
p->se_app_name = dpi_app_id_to_name(engine, app);
2101+
p->se_app_proto = dpi_app_id_to_name(engine, proto);
2102+
p->se_app_type = dpi_app_type_to_name(engine, type);
2103+
return 1;
2104+
}
2105+
2106+
void npf_session_feature_query(struct dp_session_info *info,
2107+
struct session *s __unused,
2108+
struct session_feature *sf)
2109+
{
2110+
npf_session_t *se = sf->sf_data;
2111+
enum dp_session_attr query = info->query;
2112+
2113+
/* DPI query */
2114+
if (query & SESSION_ATTR_DPI && se->s_dpi)
2115+
dpi_flow_for_each_engine(se->s_dpi, get_dpi_info, info);
2116+
}

src/npf/npf_session.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ npf_nat_t *npf_session_retnat(npf_session_t *se, const int di, bool *forw);
135135
void npf_session_feature_json(json_writer_t *json, npf_session_t *se);
136136
void npf_session_feature_log(enum session_log_event event, struct session *s,
137137
struct session_feature *sf);
138+
void npf_session_feature_query(struct dp_session_info *info, struct session *s,
139+
struct session_feature *sf);
138140
int npf_session_feature_nat_info(npf_session_t *se, uint32_t *taddr,
139141
uint16_t *tport);
140142

src/session/session.c

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1934,7 +1934,7 @@ int session_npf_pack_restore(struct npf_pack_dp_session *pds,
19341934
s->se_flags = SESSION_INSERTED;
19351935

19361936
rc = session_npf_pack_stats_restore(s, stats);
1937-
if (rc)
1937+
if (rc)
19381938
goto error;
19391939

19401940
*session = s;
@@ -2024,3 +2024,79 @@ uint64_t dp_session_unique_id(const struct session *session)
20242024
return 0;
20252025
return session->se_id;
20262026
}
2027+
2028+
/* walk function for query features */
2029+
static int se_feature_query(struct session *s, struct session_feature *sf,
2030+
void *data)
2031+
{
2032+
struct dp_session_info *info = data;
2033+
2034+
if (sf->sf_ops && sf->sf_ops->query)
2035+
sf->sf_ops->query(info, s, sf);
2036+
2037+
return 0;
2038+
}
2039+
2040+
int dp_session_query(struct session *s, enum dp_session_attr query,
2041+
struct dp_session_info *info)
2042+
{
2043+
if (!s)
2044+
return -1;
2045+
2046+
info->se_id = s->se_id;
2047+
2048+
if (query & SESSION_ATTR_PROTOCOL) {
2049+
info->se_protocol = s->se_protocol;
2050+
info->se_protocol_state = s->se_protocol_state;
2051+
}
2052+
if (query & SESSION_ATTR_BYTES_IN)
2053+
info->se_bytes_in = rte_atomic64_read(&s->se_bytes_in);
2054+
if (query & SESSION_ATTR_PKTS_IN)
2055+
info->se_pkts_in = rte_atomic64_read(&s->se_pkts_in);
2056+
if (query & SESSION_ATTR_CREATE_TIME)
2057+
info->se_create_time = s->se_create_time;
2058+
if (query & SESSION_ATTR_BYTES_OUT)
2059+
info->se_bytes_out = rte_atomic64_read(&s->se_bytes_out);
2060+
if (query & SESSION_ATTR_PKTS_OUT)
2061+
info->se_pkts_out = rte_atomic64_read(&s->se_pkts_out);
2062+
2063+
if (query & SESSION_ATTR_SENTRY) {
2064+
const void *saddr;
2065+
const void *daddr;
2066+
uint32_t if_index;
2067+
uint16_t sid;
2068+
uint16_t did;
2069+
2070+
struct sentry *sen = rcu_dereference(s->se_sen);
2071+
if (sen) {
2072+
session_sentry_extract(sen, &if_index, &info->se_af,
2073+
&saddr, &sid, &daddr, &did);
2074+
2075+
if (query & SESSION_ATTR_L4_SRC_PORT)
2076+
info->se_src_port = sid;
2077+
if (query & SESSION_ATTR_IPV4_SRC_ADDR)
2078+
info->se_src_addr = *(uint32_t *)saddr;
2079+
if (query & SESSION_ATTR_L4_DST_PORT)
2080+
info->se_dst_port = did;
2081+
if (query & SESSION_ATTR_IPV4_DST_ADDR)
2082+
info->se_dst_addr = *(uint32_t *)daddr;
2083+
if (query & SESSION_ATTR_IF_NAME)
2084+
info->se_ifname =
2085+
ifnet_indextoname_safe(if_index);
2086+
}
2087+
}
2088+
2089+
if (query & SESSION_ATTR_DPI) {
2090+
info->query = query;
2091+
2092+
/* set default value in case no info found */
2093+
info->se_app_name = NULL;
2094+
info->se_app_proto = NULL;
2095+
info->se_app_type = NULL;
2096+
2097+
session_feature_walk_session(s, SESSION_FEATURE_ALL,
2098+
se_feature_query, info);
2099+
}
2100+
2101+
return 0;
2102+
}

src/session/session.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ struct session_feature_ops {
106106
void (*log)(enum session_log_event event, struct session *s,
107107
struct session_feature *sf);
108108
int (*nat_info)(void *data, uint32_t *taddr, uint16_t *tport);
109+
110+
void (*query)(struct dp_session_info *info, struct session *s,
111+
struct session_feature *sf);
109112
};
110113

111114
#define SESS_FEAT_REQ_EXPIRY 0x01 /* feature marked for expiry */

0 commit comments

Comments
 (0)