Skip to content

Commit

Permalink
conntrack: Prefer dst port range during unique tuple search.
Browse files Browse the repository at this point in the history
This commit splits the nested loop used to search the unique ports for
the reverse tuple.
It affects only the dnat action, giving more precedence to the dnat
range, similarly to the kernel dp, instead of searching through the
default ephemeral source range for each destination port.

Acked-by: Paolo Valerio <[email protected]>
Signed-off-by: wenxu <[email protected]>
Signed-off-by: Ilya Maximets <[email protected]>
  • Loading branch information
wenxu authored and igsilya committed Mar 4, 2022
1 parent ec85f53 commit 545b644
Showing 1 changed file with 43 additions and 12 deletions.
55 changes: 43 additions & 12 deletions lib/conntrack.c
Original file line number Diff line number Diff line change
Expand Up @@ -2397,6 +2397,26 @@ next_addr_in_range_guarded(union ct_addr *curr, union ct_addr *min,
return exhausted;
}

static bool
nat_get_unique_l4(struct conntrack *ct, struct conn *nat_conn,
ovs_be16 *port, uint16_t curr, uint16_t min,
uint16_t max)
{
uint16_t orig = curr;

FOR_EACH_PORT_IN_RANGE (curr, min, max) {
*port = htons(curr);
if (!conn_lookup(ct, &nat_conn->rev_key,
time_msec(), NULL, NULL)) {
return true;
}
}

*port = htons(orig);

return false;
}

/* This function tries to get a unique tuple.
* Every iteration checks that the reverse tuple doesn't
* collide with any existing one.
Expand All @@ -2411,9 +2431,11 @@ next_addr_in_range_guarded(union ct_addr *curr, union ct_addr *min,
*
* In case of DNAT:
* - For each dst IP address in the range (if any).
* - For each dport in range (if any).
* - Try to find a source port in the ephemeral range
* (after testing the port used by the sender).
* - For each dport in range (if any) tries to find
* an unique tuple.
* - Eventually, if the previous attempt fails,
* tries to find a source port in the ephemeral
* range (after testing the port used by the sender).
*
* If none can be found, return exhaustion to the caller. */
static bool
Expand Down Expand Up @@ -2444,6 +2466,11 @@ nat_get_unique_tuple(struct conntrack *ct, const struct conn *conn,
set_dport_range(nat_info, &conn->key, hash, &curr_dport,
&min_dport, &max_dport);

if (pat_proto) {
nat_conn->rev_key.src.port = htons(curr_dport);
nat_conn->rev_key.dst.port = htons(curr_sport);
}

another_round:
store_addr_to_key(&curr_addr, &nat_conn->rev_key,
nat_info->nat_action);
Expand All @@ -2457,15 +2484,19 @@ nat_get_unique_tuple(struct conntrack *ct, const struct conn *conn,
goto next_addr;
}

FOR_EACH_PORT_IN_RANGE(curr_dport, min_dport, max_dport) {
nat_conn->rev_key.src.port = htons(curr_dport);
FOR_EACH_PORT_IN_RANGE(curr_sport, min_sport, max_sport) {
nat_conn->rev_key.dst.port = htons(curr_sport);
if (!conn_lookup(ct, &nat_conn->rev_key,
time_msec(), NULL, NULL)) {
return true;
}
}
bool found = false;
if (nat_info->nat_action & NAT_ACTION_DST_PORT) {
found = nat_get_unique_l4(ct, nat_conn, &nat_conn->rev_key.src.port,
curr_dport, min_dport, max_dport);
}

if (!found) {
found = nat_get_unique_l4(ct, nat_conn, &nat_conn->rev_key.dst.port,
curr_sport, min_sport, max_sport);
}

if (found) {
return true;
}

/* Check if next IP is in range and respin. Otherwise, notify
Expand Down

0 comments on commit 545b644

Please sign in to comment.