Skip to content

Commit

Permalink
final renamings
Browse files Browse the repository at this point in the history
  • Loading branch information
mogaika committed Feb 2, 2024
1 parent c977bbd commit 1af2003
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 136 deletions.
22 changes: 10 additions & 12 deletions upf/flowtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ flowtable_entry_remove_internal (flowtable_main_t *fm,
/* hashtable unlink */
clib_bihash_kv_48_8_t kv = { 0 };
flow_key_t *hash_key = (flow_key_t *) &kv.key;
flow_key_apply_direction (hash_key, &f->key, f->flow_key_direction);
flow_key_apply_direction (hash_key, &f->key, f->key_direction);
clib_bihash_add_del_48_8 (&fmt->flows_ht, &kv, 0 /* is_add */);

flow_entry_free (fm, fmt, f);
Expand Down Expand Up @@ -206,9 +206,8 @@ flowtable_lifetime_calculate (flowtable_main_t *fm, flow_key_t const *key)
return fm->timer_lifetime[FT_TIMEOUT_TYPE_TCP];

default:
return ip46_address_is_ip4 (&key->ip[FT_FORWARD]) ?
fm->timer_lifetime[FT_TIMEOUT_TYPE_IPV4] :
fm->timer_lifetime[FT_TIMEOUT_TYPE_IPV6];
return key->is_ip4 ? fm->timer_lifetime[FT_TIMEOUT_TYPE_IPV4] :
fm->timer_lifetime[FT_TIMEOUT_TYPE_IPV6];
}

return fm->timer_lifetime[FT_TIMEOUT_TYPE_UNKNOWN];
Expand Down Expand Up @@ -241,7 +240,7 @@ flowtable_entry_lookup_create (flowtable_main_t *fm,
flowtable_main_per_cpu_t *fmt,
clib_bihash_kv_48_8_t *kv, u64 timestamp_ns,
u32 const now,
flow_key_direction_t pkt_key_direction,
flow_direction_op_t key_direction,
u16 generation, u32 session_index, int *created)
{
flow_entry_t *f;
Expand All @@ -262,11 +261,10 @@ flowtable_entry_lookup_create (flowtable_main_t *fm,

pool_get_zero (fm->flows, f);

flow_key_apply_direction (&f->key, (flow_key_t *) kv->key,
pkt_key_direction);
flow_key_apply_direction (&f->key, (flow_key_t *) kv->key, key_direction);

// make it so FT_INITIATOR corresponds to first packet
f->flow_key_direction = pkt_key_direction;
f->key_direction = key_direction;
f->lifetime = flowtable_lifetime_calculate (fm, &f->key);
f->active = now;
f->flow_start_time = timestamp_ns;
Expand Down Expand Up @@ -313,10 +311,10 @@ format_flow_key (u8 *s, va_list *args)
flow_key_t *key = va_arg (*args, flow_key_t *);

return format (s, "proto 0x%x, %U:%u <-> %U:%u, seid 0x%016llx", key->proto,
format_ip46_address, &key->ip[FT_FORWARD], IP46_TYPE_ANY,
clib_net_to_host_u16 (key->port[FT_FORWARD]),
format_ip46_address, &key->ip[FT_REVERSE], IP46_TYPE_ANY,
clib_net_to_host_u16 (key->port[FT_REVERSE]), key->up_seid);
format_ip46_address, &key->ip[FTK_EL_SRC], IP46_TYPE_ANY,
clib_net_to_host_u16 (key->port[FTK_EL_SRC]),
format_ip46_address, &key->ip[FTK_EL_DST], IP46_TYPE_ANY,
clib_net_to_host_u16 (key->port[FTK_EL_DST]), key->up_seid);
}

u8 *
Expand Down
94 changes: 46 additions & 48 deletions upf/flowtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,6 @@ typedef enum
FT_NEXT_N_NEXT
} flowtable_next_t;

// Flowtable direction based on 3 values which can be xored:
// - packet key direction - if key is reversed for packet
// - flow key direction - convert key to index in flow key elements
// - packet flow direction (or just direction) - direction of packet in flow
// Good rule of thumb is to use pkt/flow/key in variable names and on every XOR
// operation "XOR" this names as well. Example:
// pkt_key_direction ^ flow_key_direction = pkt_flow_direction
typedef enum
{
FT_INITIATOR = 0, // direction in which flow was created
Expand All @@ -72,18 +65,19 @@ typedef enum

typedef enum
{
FT_FORWARD = 0, // original key direction
FT_REVERSE, // reversed key direction
} __clib_packed flow_key_direction_t;
FTD_OP_SAME = 0, // original key direction
FTD_OP_FLIP, // reversed key direction
} __clib_packed flow_direction_op_t;

typedef enum
{
FTK_EL_SRC = 0, // select src field of pkt in direction
FTK_EL_DST = 1, // select dst field of pkt in direction
} __clib_packed flow_key_el_t;

// Flowtable hashmap key. Fields can be reversed and because of this key access
// usually should use flow_key_direction_t
// Flowtable hashmap key.
// Fields can be reversed and because of this key access usually should use
// flow_direction_op_t determined from packet ip addresses
typedef struct
{
union
Expand All @@ -94,27 +88,29 @@ typedef struct
ip46_address_t ip[FT_DIRECTION_MAX];
u16 port[FT_DIRECTION_MAX];
u8 proto;
u8 is_ip4 : 1;
};
u64 key[6];
};
} flow_key_t;

// Key like flow_key_t, but elements are ordered and can be accessed directly
// with flow_direction_t, without accounting for flow_key_direction_t
typedef flow_key_t flow_key_directioned_t;
// Like flow_key_t, but elements are ordered and can be accessed directly with
// flow_direction_t, without accounting for ip address comparison
typedef flow_key_t key_directioned_t;

__always_inline void
flow_key_apply_direction (flow_key_t *dst_key, const flow_key_t *src_key,
flow_key_direction_t direction)
flow_direction_op_t direction)
{
dst_key->up_seid = src_key->up_seid;
ip46_address_copy (&dst_key->ip[FT_FORWARD ^ direction],
&src_key->ip[FT_FORWARD]);
ip46_address_copy (&dst_key->ip[FT_REVERSE ^ direction],
&src_key->ip[FT_REVERSE]);
dst_key->port[FT_FORWARD ^ direction] = src_key->port[FT_FORWARD];
dst_key->port[FT_REVERSE ^ direction] = src_key->port[FT_REVERSE];
ip46_address_copy (&dst_key->ip[FTK_EL_SRC ^ direction],
&src_key->ip[FTK_EL_SRC]);
ip46_address_copy (&dst_key->ip[FTK_EL_DST ^ direction],
&src_key->ip[FTK_EL_DST]);
dst_key->port[FTK_EL_SRC ^ direction] = src_key->port[FTK_EL_SRC];
dst_key->port[FTK_EL_DST ^ direction] = src_key->port[FTK_EL_DST];
dst_key->proto = src_key->proto;
dst_key->is_ip4 = src_key->is_ip4;
}

typedef struct
Expand Down Expand Up @@ -173,10 +169,10 @@ typedef struct flow_entry

/* flow signature */
// key elements can be addressed directily with flow_direction_t
flow_key_directioned_t key;
key_directioned_t key;
u32 session_index;

u8 flow_key_direction : 1; // is bihash flow_key_t reversed
u8 key_direction : 1; // flow_direction_op_t of bihash key
u8 is_redirect : 1;
u8 is_l3_proxy : 1;
u8 is_spliced : 1;
Expand Down Expand Up @@ -311,7 +307,7 @@ u32 flowtable_entry_lookup_create (flowtable_main_t *fm,
flowtable_main_per_cpu_t *fmt,
clib_bihash_kv_48_8_t *kv, u64 timestamp_ns,
u32 const now,
flow_key_direction_t flow_key_direction,
flow_direction_op_t key_direction,
u16 generation, u32 session_index,
int *created);

Expand Down Expand Up @@ -362,91 +358,93 @@ flowtable_timeout_start_entry (flowtable_main_t *fm,
}

static inline void
parse_packet_protocol (udp_header_t *udp, flow_key_direction_t key_direction,
parse_packet_protocol (udp_header_t *udp, flow_direction_op_t key_direction,
flow_key_t *key)
{
if (key->proto == IP_PROTOCOL_UDP || key->proto == IP_PROTOCOL_TCP)
{
/* tcp and udp ports have the same offset */
key->port[FT_FORWARD ^ key_direction] = udp->src_port;
key->port[FT_REVERSE ^ key_direction] = udp->dst_port;
key->port[FTK_EL_SRC ^ key_direction] = udp->src_port;
key->port[FTK_EL_DST ^ key_direction] = udp->dst_port;
}
else
{
key->port[FT_FORWARD] = key->port[FT_REVERSE] = 0;
key->port[FTK_EL_SRC] = 0;
key->port[FTK_EL_DST] = 0;
}
}

static inline flow_key_direction_t
static inline flow_direction_op_t
ip4_packet_is_reverse (ip4_header_t *ip4)
{
return (ip4_address_compare (&ip4->src_address, &ip4->dst_address) < 0) ?
FT_REVERSE :
FT_FORWARD;
FTD_OP_FLIP :
FTD_OP_SAME;
}

static inline void
parse_ip4_packet (ip4_header_t *ip4, flow_key_direction_t *pkt_key_direction,
parse_ip4_packet (ip4_header_t *ip4, flow_direction_op_t *key_direction,
flow_key_t *key)
{
key->proto = ip4->protocol;

*pkt_key_direction = ip4_packet_is_reverse (ip4);
*key_direction = ip4_packet_is_reverse (ip4);

ip46_address_set_ip4 (&key->ip[FTK_EL_SRC ^ *pkt_key_direction],
ip46_address_set_ip4 (&key->ip[FTK_EL_SRC ^ *key_direction],
&ip4->src_address);
ip46_address_set_ip4 (&key->ip[FTK_EL_DST ^ *pkt_key_direction],
ip46_address_set_ip4 (&key->ip[FTK_EL_DST ^ *key_direction],
&ip4->dst_address);

parse_packet_protocol ((udp_header_t *) ip4_next_header (ip4),
*pkt_key_direction, key);
*key_direction, key);
}

static inline flow_key_direction_t
static inline flow_direction_op_t
ip6_packet_is_reverse (ip6_header_t *ip6)
{
return (ip6_address_compare (&ip6->src_address, &ip6->dst_address) < 0) ?
FT_REVERSE :
FT_FORWARD;
FTD_OP_FLIP :
FTD_OP_SAME;
}

static inline void
parse_ip6_packet (ip6_header_t *ip6, flow_key_direction_t *pkt_key_direction,
parse_ip6_packet (ip6_header_t *ip6, flow_direction_op_t *key_direction,
flow_key_t *key)
{
key->proto = ip6->protocol;

*pkt_key_direction = ip6_packet_is_reverse (ip6);
*key_direction = ip6_packet_is_reverse (ip6);

ip46_address_set_ip6 (&key->ip[FTK_EL_SRC ^ *pkt_key_direction],
ip46_address_set_ip6 (&key->ip[FTK_EL_SRC ^ *key_direction],
&ip6->src_address);
ip46_address_set_ip6 (&key->ip[FTK_EL_DST ^ *pkt_key_direction],
ip46_address_set_ip6 (&key->ip[FTK_EL_DST ^ *key_direction],
&ip6->dst_address);

parse_packet_protocol ((udp_header_t *) ip6_next_header (ip6),
*pkt_key_direction, key);
*key_direction, key);
}

__clib_unused static inline void
flow_mk_key (u64 up_seid, u8 *header, u8 is_ip4,
flow_key_direction_t *pkt_key_direction,
flow_direction_op_t *flow_key_direction,
clib_bihash_kv_48_8_t *kv)
{
flow_key_t *key = (flow_key_t *) &kv->key;

memset (key, 0, sizeof (*key));

key->up_seid = up_seid;
key->is_ip4 = is_ip4;

/* compute 5 tuple key so that 2 half connections
* get into the same flow */
if (is_ip4)
{
parse_ip4_packet ((ip4_header_t *) header, pkt_key_direction, key);
parse_ip4_packet ((ip4_header_t *) header, flow_key_direction, key);
}
else
{
parse_ip6_packet ((ip6_header_t *) header, pkt_key_direction, key);
parse_ip6_packet ((ip6_header_t *) header, flow_key_direction, key);
}
}

Expand Down
46 changes: 23 additions & 23 deletions upf/unittest.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,36 +153,36 @@ ip_app_test_v4 (void)

flow_entry_t flow;
memset (&flow, 0, sizeof(flow));
flow.key.ip[FT_FORWARD].ip4.as_u32 = ip_ue_172_17_0_5.ip4.as_u32;
flow.key.port[FT_FORWARD] = clib_host_to_net_u16(12345);
flow.key.ip[FT_REVERSE].ip4.as_u32 = ip_10_20_20_20.ip4.as_u32;
flow.key.port[FT_REVERSE] = clib_host_to_net_u16(80);
flow.key.ip[FT_INITIATOR].ip4.as_u32 = ip_ue_172_17_0_5.ip4.as_u32;
flow.key.port[FT_INITIATOR] = clib_host_to_net_u16(12345);
flow.key.ip[FT_RESPONDER].ip4.as_u32 = ip_10_20_20_20.ip4.as_u32;
flow.key.port[FT_RESPONDER] = clib_host_to_net_u16(80);

UPF_TEST (!upf_app_ip_rule_match (app_id, &flow, &ip_ue_172_17_0_5),
"rule mismatch (IPAPP): 172.17.0.5:12345 -> 10.20.20.20:80");
UPF_TEST (upf_app_ip_rule_match (app_id_any, &flow, &ip_ue_172_17_0_5),
"rule match (IPANY): 172.17.0.5:12345 -> 10.20.20.20:80");

flow.key.ip[FT_REVERSE].ip4.as_u32 = ip_10_10_10_10.ip4.as_u32;
flow.key.ip[FT_RESPONDER].ip4.as_u32 = ip_10_10_10_10.ip4.as_u32;
UPF_TEST (upf_app_ip_rule_match (app_id, &flow, &ip_ue_172_17_0_5),
"rule match (IPAPP): 172.17.0.5:12345 -> 10.10.10.10:80");
UPF_TEST (upf_app_ip_rule_match (app_id_any, &flow, &ip_ue_172_17_0_5),
"rule match (IPANY): 172.17.0.5:12345 -> 10.10.10.10:80");

flow.key.ip[FT_REVERSE].ip4.as_u32 = ip_192_168_0_5.ip4.as_u32;
flow.key.ip[FT_RESPONDER].ip4.as_u32 = ip_192_168_0_5.ip4.as_u32;
UPF_TEST (upf_app_ip_rule_match (app_id, &flow, &ip_ue_172_17_0_5),
"rule match (IPAPP): 172.17.0.5:12345 -> 192.168.0.5:80");
UPF_TEST (upf_app_ip_rule_match (app_id_any, &flow, &ip_ue_172_17_0_5),
"rule match (IPANY): 172.17.0.5:12345 -> 192.168.0.5:80");

flow.key.port[FT_REVERSE] = clib_host_to_net_u16(9999);
flow.key.port[FT_RESPONDER] = clib_host_to_net_u16(9999);
UPF_TEST (!upf_app_ip_rule_match (app_id, &flow, &ip_ue_172_17_0_5),
"rule mismatch (IPAPP): 172.17.0.5:12345 -> 192.168.0.5:9999");
UPF_TEST (upf_app_ip_rule_match (app_id_any, &flow, &ip_ue_172_17_0_5),
"rule match (IPANY): 172.17.0.5:12345 -> 192.168.0.5:9999");

flow.key.ip[FT_FORWARD].ip4.as_u32 = ip_10_20_20_20.ip4.as_u32;
flow.key.port[FT_REVERSE] = clib_host_to_net_u16(80);
flow.key.ip[FT_INITIATOR].ip4.as_u32 = ip_10_20_20_20.ip4.as_u32;
flow.key.port[FT_RESPONDER] = clib_host_to_net_u16(80);
UPF_TEST (!upf_app_ip_rule_match (app_id, &flow, &ip_ue_172_17_0_5),
"rule mismatch (IPAPP): 10.20.20.20:12345 -> 192.168.0.5:80");
UPF_TEST (!upf_app_ip_rule_match (app_id_any, &flow, &ip_ue_172_17_0_5),
Expand Down Expand Up @@ -228,41 +228,41 @@ ip_app_test_v6 (void)

flow_entry_t flow;
memset (&flow, 0, sizeof(flow));
flow.key.ip[FT_FORWARD].ip6.as_u64[0] = ip_ue_2001_db8_11__3.ip6.as_u64[0];
flow.key.ip[FT_FORWARD].ip6.as_u64[1] = ip_ue_2001_db8_11__3.ip6.as_u64[1];
flow.key.port[FT_FORWARD] = clib_host_to_net_u16(12345);
flow.key.ip[FT_REVERSE].ip6.as_u64[0] = ip_2001_db8_12__3.ip6.as_u64[0];
flow.key.ip[FT_REVERSE].ip6.as_u64[1] = ip_2001_db8_12__3.ip6.as_u64[1];
flow.key.port[FT_REVERSE] = clib_host_to_net_u16(80);
flow.key.ip[FT_INITIATOR].ip6.as_u64[0] = ip_ue_2001_db8_11__3.ip6.as_u64[0];
flow.key.ip[FT_INITIATOR].ip6.as_u64[1] = ip_ue_2001_db8_11__3.ip6.as_u64[1];
flow.key.port[FT_INITIATOR] = clib_host_to_net_u16(12345);
flow.key.ip[FT_RESPONDER].ip6.as_u64[0] = ip_2001_db8_12__3.ip6.as_u64[0];
flow.key.ip[FT_RESPONDER].ip6.as_u64[1] = ip_2001_db8_12__3.ip6.as_u64[1];
flow.key.port[FT_RESPONDER] = clib_host_to_net_u16(80);

UPF_TEST (!upf_app_ip_rule_match (app_id, &flow, &ip_ue_2001_db8_11__3),
"rule mismatch (IPAPP): [2001:db8:11::3]:12345 -> [2001:db8:12::3]:80");
UPF_TEST (upf_app_ip_rule_match (app_id_any, &flow, &ip_ue_2001_db8_11__3),
"rule match (IPANY): [2001:db8:11::3]:12345 -> [2001:db8:12::3]:80");

flow.key.ip[FT_REVERSE].ip6.as_u64[0] = ip_2001_db8_12__2.ip6.as_u64[0];
flow.key.ip[FT_REVERSE].ip6.as_u64[1] = ip_2001_db8_12__2.ip6.as_u64[1];
flow.key.ip[FT_RESPONDER].ip6.as_u64[0] = ip_2001_db8_12__2.ip6.as_u64[0];
flow.key.ip[FT_RESPONDER].ip6.as_u64[1] = ip_2001_db8_12__2.ip6.as_u64[1];
UPF_TEST (upf_app_ip_rule_match (app_id, &flow, &ip_ue_2001_db8_11__3),
"rule match (IPAPP): [2001:db8:11::3]:12345 -> [2001:db8:12::2]:80");
UPF_TEST (upf_app_ip_rule_match (app_id_any, &flow, &ip_ue_2001_db8_11__3),
"rule match (IPANY): [2001:db8:11::3]:12345 -> [2001:db8:12::2]:80");

flow.key.ip[FT_REVERSE].ip6.as_u64[0] = ip_2001_db8_13__5.ip6.as_u64[0];
flow.key.ip[FT_REVERSE].ip6.as_u64[1] = ip_2001_db8_13__5.ip6.as_u64[1];
flow.key.ip[FT_RESPONDER].ip6.as_u64[0] = ip_2001_db8_13__5.ip6.as_u64[0];
flow.key.ip[FT_RESPONDER].ip6.as_u64[1] = ip_2001_db8_13__5.ip6.as_u64[1];
UPF_TEST (upf_app_ip_rule_match (app_id, &flow, &ip_ue_2001_db8_11__3),
"rule match (IPAPP): [2001:db8:11::3]:12345 -> [2001:db8:13::5]:80");
UPF_TEST (upf_app_ip_rule_match (app_id_any, &flow, &ip_ue_2001_db8_11__3),
"rule match (IPANY): [2001:db8:11::3]:12345 -> [2001:db8:13::5]:80");

flow.key.port[FT_REVERSE] = clib_host_to_net_u16(9999);
flow.key.port[FT_RESPONDER] = clib_host_to_net_u16(9999);
UPF_TEST (!upf_app_ip_rule_match (app_id, &flow, &ip_ue_2001_db8_11__3),
"rule mismatch (IPAPP): [2001:db8:11::3]:12345 -> [2001:db8:13::5]:9999");
UPF_TEST (upf_app_ip_rule_match (app_id_any, &flow, &ip_ue_2001_db8_11__3),
"rule match (IPANY): [2001:db8:11::3]:12345 -> [2001:db8:13::5]:9999");

flow.key.ip[FT_FORWARD].ip6.as_u64[0] = ip_2001_db8_12__3.ip6.as_u64[0];
flow.key.ip[FT_FORWARD].ip6.as_u64[1] = ip_2001_db8_12__3.ip6.as_u64[1];
flow.key.port[FT_REVERSE] = clib_host_to_net_u16(80);
flow.key.ip[FT_INITIATOR].ip6.as_u64[0] = ip_2001_db8_12__3.ip6.as_u64[0];
flow.key.ip[FT_INITIATOR].ip6.as_u64[1] = ip_2001_db8_12__3.ip6.as_u64[1];
flow.key.port[FT_RESPONDER] = clib_host_to_net_u16(80);
UPF_TEST (!upf_app_ip_rule_match (app_id, &flow, &ip_ue_2001_db8_11__3),
"rule mismatch (IPAPP): [2001:db8:12::3]:12345 -> [2001:db8:13::5]:80");
UPF_TEST (!upf_app_ip_rule_match (app_id_any, &flow, &ip_ue_2001_db8_11__3),
Expand Down
4 changes: 2 additions & 2 deletions upf/upf.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,11 +498,11 @@ upf_format_buffer_opaque_helper (const vlib_buffer_t *b, u8 *s)
s = format (
s,
"gtpu.teid: 0x%08x, gtpu.session_index: 0x%x, gtpu.ext_hdr_len: %u, "
"gtpu.data_offset: %u, gtpu.flags: 0x%02x, gtpu.pkt_key_direction: %u, "
"gtpu.data_offset: %u, gtpu.flags: 0x%02x, gtpu.flow_key_direction: %u, "
"gtpu.direction: %u, gtpu.pdr_idx: 0x%x, gtpu.flow_id: 0x%x",
(u32) (o->gtpu.teid), (u32) (o->gtpu.session_index),
(u32) (o->gtpu.ext_hdr_len), (u32) (o->gtpu.data_offset),
(u32) (o->gtpu.flags), (u32) (o->gtpu.pkt_key_direction),
(u32) (o->gtpu.flags), (u32) (o->gtpu.flow_key_direction),
(u32) (o->gtpu.direction), (u32) (o->gtpu.pdr_idx),
(u32) (o->gtpu.flow_id));
vec_add1 (s, '\n');
Expand Down
Loading

0 comments on commit 1af2003

Please sign in to comment.