Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions include/dp_session.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2021, SafePoint <info@safepoint.vn>. All rights reserved.
* Copyright (c) 2020, AT&T Intellectual Property. All rights reserved.
*
* SPDX-License-Identifier: LGPL-2.1-only
Expand Down Expand Up @@ -65,6 +66,56 @@ enum dp_session_state {
SESSION_STATE_CLOSED,
} __attribute__ ((__packed__));

/**
* Session attribute.
*/
enum dp_session_attr {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the need to request specific attrs due to the plugin directly outputting the returned JSON?
The plugin should retrieve the necessary attrs from the JSON and format them into a suitable table.
Therefore it should not matter if additional attrs are returned.
The dataplane could return all of the attrs, and the plugin only output the relevant values.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The plugin registers a session_watch to hook when session state or stats changed. This was called from dataplane forwarding path and I try to keep plugin not affect forwarding performance as much as possible.

I see that returning all attrs from dataplane make easier for adding new attributes but I afraid it could affect performance. That why I added a query as parameter.

SESSION_ATTR_BYTES_IN = 1,
SESSION_ATTR_PKTS_IN = (1 << 1),
SESSION_ATTR_PROTOCOL = (1 << 2),
SESSION_ATTR_TCP_FLAGS = (1 << 3),
SESSION_ATTR_L4_SRC_PORT = (1 << 4),
SESSION_ATTR_IPV4_SRC_ADDR = (1 << 5),
SESSION_ATTR_L4_DST_PORT = (1 << 6),
SESSION_ATTR_IPV4_DST_ADDR = (1 << 7),
SESSION_ATTR_CREATE_TIME = (1 << 8),
SESSION_ATTR_BYTES_OUT = (1 << 9),
SESSION_ATTR_PKTS_OUT = (1 << 10),
SESSION_ATTR_IF_NAME = (1 << 11),
SESSION_ATTR_DPI = (1 << 12),
};

#define SESSION_ATTR_ALL 0xffffffff
#define SESSION_ATTR_SENTRY (SESSION_ATTR_L4_SRC_PORT \
| SESSION_ATTR_IPV4_SRC_ADDR \
| SESSION_ATTR_L4_DST_PORT \
| SESSION_ATTR_IPV4_DST_ADDR \
| SESSION_ATTR_IF_NAME)

struct dp_session_info {
enum dp_session_attr query;
uint64_t se_id;
uint16_t se_flags;
uint8_t se_protocol;
uint8_t se_protocol_state;
uint64_t se_pkts_in;
uint64_t se_bytes_in;
uint64_t se_create_time; /* time session was created */
uint64_t se_pkts_out;
uint64_t se_bytes_out;

/* address */
int se_af;
uint16_t se_src_port;
uint32_t se_src_addr;
uint16_t se_dst_port;
uint32_t se_dst_addr;
const char *se_ifname;
const char *se_app_name;
const char *se_app_proto;
const char *se_app_type;
};

#define SESSION_STATE_FIRST SESSION_STATE_NONE
#define SESSION_STATE_LAST SESSION_STATE_CLOSED
#define SESSION_STATE_SIZE (SESSION_STATE_LAST + 1)
Expand Down Expand Up @@ -233,6 +284,12 @@ void *dp_session_get_private(int id, const struct session *session);
int dp_session_table_walk(dp_session_walk_t *fn, void *data,
unsigned int types);

/**
* Query a session's info.
*/
int dp_session_query(struct session *s, enum dp_session_attr query,
struct dp_session_info *info);

/**
* Get a session's unique id.
*
Expand Down
17 changes: 17 additions & 0 deletions src/npf/dpi/dpi.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2021, SafePoint <info@safepoint.vn>. All rights reserved.
* Copyright (c) 2017-2020, AT&T Intellectual Property. All rights reserved.
*
* Copyright (c) 2016-2017 by Brocade Communications Systems, Inc.
Expand Down Expand Up @@ -523,6 +524,22 @@ dpi_app_type_name_to_id(uint8_t engine_id, const char *type_name)
return engine ? engine->type_to_id(type_name) : DPI_APP_ERROR;
}

const char*
dpi_app_id_to_name(uint8_t engine_id, uint32_t app)
{
struct dpi_engine_procs *engine = NULL_ENGINE;
ENGINE_PROC_FIND(engine, engine_id, appid_to_name);
return engine ? engine->appid_to_name(app) : NULL;
}

const char*
dpi_app_type_to_name(uint8_t engine_id, uint32_t type)
{
struct dpi_engine_procs *engine = NULL_ENGINE;
ENGINE_PROC_FIND(engine, engine_id, apptype_to_name);
return engine ? engine->apptype_to_name(type) : NULL;
}

void
dpi_info_json(struct dpi_flow *dpi_flow, json_writer_t *json)
{
Expand Down
14 changes: 14 additions & 0 deletions src/npf/dpi/dpi_internal.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2021, SafePoint <info@safepoint.vn>. All rights reserved.
* Copyright (c) 2017-2018,2020, AT&T Intellectual Property.
* All rights reserved.
*
Expand Down Expand Up @@ -404,8 +405,21 @@ struct dpi_engine_procs {
*/
size_t (*info_log)(struct dpi_engine_flow *flow, char *buf,
size_t buf_len);

/**
* Get name of app id.
*/
const char* (*appid_to_name)(uint32_t app);

/**
* Get type of type id.
*/
const char* (*apptype_to_name)(uint32_t type);
};

const char *dpi_app_id_to_name(uint8_t engine_id, uint32_t app);
const char *dpi_app_type_to_name(uint8_t engine_id, uint32_t type);


bool no_app_id(uint32_t app_id);
bool no_app_type(uint32_t app_type);
Expand Down
3 changes: 3 additions & 0 deletions src/npf/dpi/dpi_user.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2021 SafePoint <info@safepoint.vn>. All rights reserved.
* Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
*
* SPDX-License-Identifier: LGPL-2.1-only
Expand Down Expand Up @@ -317,4 +318,6 @@ struct dpi_engine_procs user_engine_procs = {
.type_to_id = dpi_user_type_to_id,
.info_json = dpi_user_flow_json,
.info_log = dpi_user_flow_log,
.appid_to_name = dpi_user_id_to_name,
.apptype_to_name = dpi_user_type_to_name,
};
3 changes: 3 additions & 0 deletions src/npf/dpi/ndpi.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2021 SafePoint <info@safepoint.vn>. All rights reserved.
* Copyright (c) 2021 AT&T Intellectual Property. All rights reserved.
*
* SPDX-License-Identifier: LGPL-2.1-only
Expand Down Expand Up @@ -537,4 +538,6 @@ struct dpi_engine_procs ndpi_engine_procs = {
.type_to_id = dpi_ndpi_app_type_name_to_id,
.info_json = dpi_ndpi_info_json,
.info_log = dpi_ndpi_info_log,
.appid_to_name = dpi_ndpi_app_id_to_name,
.apptype_to_name = dpi_ndpi_app_type_to_name,
};
8 changes: 8 additions & 0 deletions src/npf/npf_dataplane_session.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2021, SafePoint <info@safepoint.vn>. All rights reserved.
* Copyright (c) 2017-2020, AT&T Intellectual Property. All rights reserved.
* Copyright (c) 2016 by Brocade Communications Systems, Inc.
* All rights reserved.
Expand Down Expand Up @@ -113,13 +114,20 @@ static int dps_feature_nat_info(void *data, uint32_t *taddr, uint16_t *tport)
return npf_session_feature_nat_info(se, taddr, tport);
}

static void dps_feature_query(struct dp_session_info *info, struct session *s,
struct session_feature *sf)
{
return npf_session_feature_query(info, s, sf);
}

/* Callbacks for the npf_session_t */
static const struct session_feature_ops ops = {
.expired = dps_feature_expire,
.destroy = dps_feature_destroy,
.json = dps_feature_json,
.log = dps_feature_log,
.nat_info = dps_feature_nat_info,
.query = dps_feature_query,
};

/*
Expand Down
26 changes: 26 additions & 0 deletions src/npf/npf_session.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2021, SafePoint <info@safepoint.vn>. All rights reserved.
* Copyright (c) 2017-2021, AT&T Intellectual Property. All rights reserved.
* Copyright (c) 2016 by Brocade Communications Systems, Inc.
* All rights reserved.
Expand Down Expand Up @@ -2099,3 +2100,28 @@ int npf_session_npf_pack_activate(struct npf_session *se, struct ifnet *ifp)
se->s_flags |= SE_ACTIVE;
return 0;
}

static int get_dpi_info(uint8_t engine, uint32_t app, uint32_t proto,
uint32_t type, void *data)
{
if (no_app_id(app) && no_app_id(proto) && no_app_type(type))
return 0;

struct dp_session_info *p = data;
p->se_app_name = dpi_app_id_to_name(engine, app);
p->se_app_proto = dpi_app_id_to_name(engine, proto);
p->se_app_type = dpi_app_type_to_name(engine, type);
return 1;
}

void npf_session_feature_query(struct dp_session_info *info,
struct session *s __unused,
struct session_feature *sf)
{
npf_session_t *se = sf->sf_data;
enum dp_session_attr query = info->query;

/* DPI query */
if (query & SESSION_ATTR_DPI && se->s_dpi)
dpi_flow_for_each_engine(se->s_dpi, get_dpi_info, info);
}
3 changes: 3 additions & 0 deletions src/npf/npf_session.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2021, SafePoint <info@safepoint.vn>. All rights reserved.
* Copyright (c) 2017-2019, AT&T Intellectual Property. All rights reserved.
* Copyright (c) 2016 by Brocade Communications Systems, Inc.
* All rights reserved.
Expand Down Expand Up @@ -135,6 +136,8 @@ npf_nat_t *npf_session_retnat(npf_session_t *se, const int di, bool *forw);
void npf_session_feature_json(json_writer_t *json, npf_session_t *se);
void npf_session_feature_log(enum session_log_event event, struct session *s,
struct session_feature *sf);
void npf_session_feature_query(struct dp_session_info *info, struct session *s,
struct session_feature *sf);
int npf_session_feature_nat_info(npf_session_t *se, uint32_t *taddr,
uint16_t *tport);

Expand Down
79 changes: 78 additions & 1 deletion src/session/session.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2021, SafePoint <info@safepoint.vn>. All rights reserved.
* Copyright (c) 2017-2021, AT&T Intellectual Property. All rights reserved.
* Copyright (c) 2017 by Brocade Communications Systems, Inc.
* All rights reserved.
Expand Down Expand Up @@ -1943,7 +1944,7 @@ int session_npf_pack_restore(struct npf_pack_dp_session *pds,
s->se_flags = SESSION_INSERTED;

rc = session_npf_pack_stats_restore(s, stats);
if (rc)
if (rc)
goto error;

*session = s;
Expand Down Expand Up @@ -2033,3 +2034,79 @@ uint64_t dp_session_unique_id(const struct session *session)
return 0;
return session->se_id;
}

/* walk function for query features */
static int se_feature_query(struct session *s, struct session_feature *sf,
void *data)
{
struct dp_session_info *info = data;

if (sf->sf_ops && sf->sf_ops->query)
sf->sf_ops->query(info, s, sf);

return 0;
}

int dp_session_query(struct session *s, enum dp_session_attr query,
struct dp_session_info *info)
{
if (!s)
return -1;

info->se_id = s->se_id;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If info was populated with all available information, then query wouldn't be necessary which would make it easier to add new information in future.

if (query & SESSION_ATTR_PROTOCOL) {
info->se_protocol = s->se_protocol;
info->se_protocol_state = s->se_protocol_state;
}
if (query & SESSION_ATTR_BYTES_IN)
info->se_bytes_in = rte_atomic64_read(&s->se_bytes_in);
if (query & SESSION_ATTR_PKTS_IN)
info->se_pkts_in = rte_atomic64_read(&s->se_pkts_in);
if (query & SESSION_ATTR_CREATE_TIME)
info->se_create_time = s->se_create_time;
if (query & SESSION_ATTR_BYTES_OUT)
info->se_bytes_out = rte_atomic64_read(&s->se_bytes_out);
if (query & SESSION_ATTR_PKTS_OUT)
info->se_pkts_out = rte_atomic64_read(&s->se_pkts_out);

if (query & SESSION_ATTR_SENTRY) {
const void *saddr;
const void *daddr;
uint32_t if_index;
uint16_t sid;
uint16_t did;

struct sentry *sen = rcu_dereference(s->se_sen);
if (sen) {
session_sentry_extract(sen, &if_index, &info->se_af,
&saddr, &sid, &daddr, &did);

if (query & SESSION_ATTR_L4_SRC_PORT)
info->se_src_port = sid;
if (query & SESSION_ATTR_IPV4_SRC_ADDR)
info->se_src_addr = *(uint32_t *)saddr;
if (query & SESSION_ATTR_L4_DST_PORT)
info->se_dst_port = did;
if (query & SESSION_ATTR_IPV4_DST_ADDR)
info->se_dst_addr = *(uint32_t *)daddr;
if (query & SESSION_ATTR_IF_NAME)
info->se_ifname =
ifnet_indextoname_safe(if_index);
}
}

if (query & SESSION_ATTR_DPI) {
info->query = query;

/* set default value in case no info found */
info->se_app_name = NULL;
info->se_app_proto = NULL;
info->se_app_type = NULL;

session_feature_walk_session(s, SESSION_FEATURE_ALL,
se_feature_query, info);
}

return 0;
}
4 changes: 4 additions & 0 deletions src/session/session.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2021, SafePoint <info@safepoint.vn>. All rights reserved.
* Copyright (c) 2017-2021, AT&T Intellectual Property. All rights reserved.
* Copyright (c) 2017 by Brocade Communications Systems, Inc.
* All rights reserved.
Expand Down Expand Up @@ -106,6 +107,9 @@ struct session_feature_ops {
void (*log)(enum session_log_event event, struct session *s,
struct session_feature *sf);
int (*nat_info)(void *data, uint32_t *taddr, uint16_t *tport);

void (*query)(struct dp_session_info *info, struct session *s,
struct session_feature *sf);
};

#define SESS_FEAT_REQ_EXPIRY 0x01 /* feature marked for expiry */
Expand Down