Skip to content

Do not unwrap on flow table limits - #347

Merged
bnaecker merged 1 commit into
masterfrom
no-limit-panics
Apr 27, 2023
Merged

bnaecker merged 1 commit into
masterfrom
no-limit-panics

Conversation

@bnaecker

@bnaecker bnaecker commented Mar 3, 2023

Copy link
Copy Markdown
Collaborator
  • Add error type for handling TCP flow errors
  • Return new error variants rather than String from methods processing TCP flows.
  • Handle TCP flow errors, now just unexpected segments, in the port processing code. This is done by returning a new ProcessError variant.
  • Add ProcessError::FlowTableFull, which wraps attempts to add a new flow entry which fail because the table is full. This includes the table "kind", which for now is just a static string, and the current limit.
  • Add an integration test verifying that TCP packets are dropped when the flow table is full.

@bnaecker
bnaecker requested review from jmpesp and luqmana March 3, 2023 22:41
@bnaecker

bnaecker commented Mar 3, 2023

Copy link
Copy Markdown
Collaborator Author

Fixes #346.

This PR removes some of the lingering unwrap() calls around TCP behavior. When establishing a new connection, we try to create a flow-table entry for it. Those have thus far been statically-sized in the xde driver, to 8096 active connections, for example. It seems this limit has been hit in #346.

There's not really a great static limit we can apply here. This PR uses the VpcCfg provided for a guest, specifically the number of available ports, to size the flow tables. If one provisions a guest with 1024 ports, that's the limit of the UFT and TCP flow tables. If one has the full port range for an IPv4 and IPv6 address, then the guest gets 2 * u16::MAX entries.

I've verified that this panic is fixed with a new integration test. That creates a tiny flow-table of 1 entry, and then tries to send two outbound packets through the OPTE port. Here's the behavior of that integration test on master:

bnaecker@feldspar : ~/opte/oxide-vpc $ cargo t -- flow_table_full
   Compiling oxide-vpc v0.1.0 (/home/bnaecker/opte/oxide-vpc)
    Finished test [unoptimized + debuginfo] target(s) in 4.14s
     Running unittests src/lib.rs (target/debug/deps/oxide_vpc-fa7540b92438ab30)

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 6 filtered out; finished in 0.00s

     Running tests/firewall_tests.rs (target/debug/deps/firewall_tests-a3f255194b2ae2a7)

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 3 filtered out; finished in 0.00s

     Running tests/integration_tests.rs (target/debug/deps/integration_tests-fc02c98ac1f45784)

running 1 test
test no_panic_on_flow_table_full ... FAILED

failures:

---- no_panic_on_flow_table_full stdout ----
thread 'no_panic_on_flow_table_full' panicked at 'called `Result::unwrap()` on an `Err` value: MaxCapacity(1)', /home/bnaecker/opte/opte/src/engine/port.rs:1820:55
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    no_panic_on_flow_table_full

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 23 filtered out; finished in 0.00s

error: test failed, to rerun pass `--test integration_tests

On this PR branch, that test passes fine.

One thing I'm not sure about is really whether this is the right solution. There are two aspects to this. First, we're sizing both the TCP and UFT flow tables to be the number of ports. But the UFT flow table will include non-TCP connections, suggesting we might want that to be larger. It seems overkill to make the table large enough to hold all possible protocols, though, so I don't know the right answer.

The other aspect is the failure mode when a guest tries to open a connection and the table is exhausted. We just drop the packet. I don't think there's anything else we can reasonably do, but it still feels odd. For example, a guest might have 16k entries in their table. But from the perspective of the guest OS, there's no real reason that one couldn't open a socket on all 65K ports. With this fix, they'd just see some of those time out. Like I said, I'm not sure what else one can do, since we can't really inject an RST or something back into the guest without confusing the crap out of them. @rmustacc and @rcgoodfellow I'd love to hear if you have any thoughts on that.

But the TL;DR is that this should address the panic itself.

- Add error type for handling TCP flow errors
- Return new error variants rather than `String` from methods processing
  TCP flows.
- Handle TCP flow errors, now just unexpected segments, in the port
  processing code. This is done by returning a new `ProcessError`
  variant.
- Add `ProcessError::FlowTableFull`, which wraps attempts to add a new
  flow entry which fail because the table is full. This includes the
  table "kind", which for now is just a static string, and the current
  limit.
- Add an integration test verifying that TCP packets are dropped when
  the flow table is full.
@bnaecker

bnaecker commented Mar 3, 2023

Copy link
Copy Markdown
Collaborator Author

Ah, I also just realized this may be alleviated by a solution to #315. It's possible there are not 8096 active connections, but there have been more than 8096 connections made at some point. We should certainly get rid of these unwraps, but we may want to revisit the choices of size made here (based on the VPC config).

@jmpesp jmpesp left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I tested this, and the host no longer panics in the same situation. :shipit:

@luqmana

luqmana commented Mar 6, 2023

Copy link
Copy Markdown
Contributor

Thanks for addressing the panic! Was out Friday so just taking a look today.

Reading through the issue a bit now, I'd propose a different approach: adding an entry to the flow tables should be infallible. We already expect flows to expire after some time (or just be removed explicitly in the case of TCP when the state machine gets to close/reset). If the table's full, we should just retire the earliest flow (*). Of course that still requires tweaking table size so we're not constantly ping-ponging flows in and out.

(*) have to check but not sure if the current structure of the code allows easily determining earliest flows, in which case I'd say just clearing the table all together is not the worst short-term option. All flows are already currently hard-coded with a TTL of 60s today AFAIK.

@bnaecker

bnaecker commented Mar 6, 2023

Copy link
Copy Markdown
Collaborator Author

Thanks @luqmana. That approach seems plausible. I don't see a quick way to look up the flows in time order now, so we'd need to add a "secondary index" to FlowTable that points to InnerFlowIds or FlowEntrys ordered by time. I didn't realize flows were expired on a period now, or at all. That seems to be done by the time of last hit, which is good.

In any case, I'm not what to make of this design. Talking out loud a bit, let's suppose we picked an arbitrary flow table limit, as we currently do, of N. All connections up to N would behave the same way, in that we'd set up a UFT and then keep hitting it as long as those are active. For connections made beyond N however, flows would start to behave less predictably. First, we'd have some initial setup time to find and remove the oldest flow and insert the new one. Second, there'd be some complicated interactions, where adding new connections would start to impact others. The impacted ones are "old," so that might be fine. It seems particularly hard to reason about in a guest that has a lot of active connections, such as a database server or load balancer. That might be an edge case though, I really don't know.

@luqmana

luqmana commented Mar 6, 2023

Copy link
Copy Markdown
Contributor

I agree but there is already some amount of unpredictability given the slow-path (traverse layers), fast-path (lookup in UFT) design. But I think that's better than just dropping the packets. It's definitely something we can tweak with how long before we expire flows and how big of a table we use. But that's already where we're at today.

Another not fully fleshed out idea is maybe just skip creating a table entry if it's full but otherwise deliver the packet as normal. Downside is subsequent packets will end up hitting slowpath but by then perhaps previous flows would've expired?

@bnaecker

bnaecker commented Mar 6, 2023 •

Copy link
Copy Markdown
Collaborator Author

That's fair about the existing design. I would imagine it's slow once, and then fast after that (assuming enough consistent activity to not expire the flow). You're definitely right though, some amount of this is already there.

It's a good point that we'd degrade more gracefully if we drop old flows as new ones above the static limit come in. As this is currently implemented, those TCP connections would just be slow to establish until / unless other flows were expired based on time. It might be better to have it driven by time and guest activity.

By "skip creating the table entry," you mean the UFT table? So that would mean the flow would always be layer-processed? That's an interesting idea. It's similar to the first proposal, except newer flows can't bump older ones out of the UFT. Those later flows would be slower but more predictable, IIUC. That sounds better that the FIFO-esque behavior of the UFT, given how sensitive TCP can be to small latency variations.

I think I'm on board with one or both of your proposals, with a slight preference for the second (not adding a UFT entry if it's full, and using layer-processing instead).

@luqmana

luqmana commented Mar 6, 2023

Copy link
Copy Markdown
Contributor

By "skip creating the table entry," you mean the UFT table? So that would mean the flow would always be layer-processed?

Right, at the point we'd be running into this we're already in the slow-path for that flow. Not returning an error/dropping the packet then would mean a subsequent packet would hit the slow-path again but hopefully by then we have space.

In the slightly longer term we should probably start thinking of different workloads & usecases to start collecting perf data. We can add some probes/stats in this case to see how often we run into this situation and use that to better size the table.

Comment thread opte/src/engine/port.rs
pmeta: &PacketMeta,
pkt_len: u64,
) -> result::Result<TcpState, String> {
) -> result::Result<TcpState, ProcessError> {

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.

I would use a separate error here so you don't need all the unreachable! cases for errors that don't apply. You can then add that as a variant to ProcessError if you want.

@bnaecker

bnaecker commented Mar 9, 2023

Copy link
Copy Markdown
Collaborator Author

I agree but there is already some amount of unpredictability given the slow-path (traverse layers), fast-path (lookup in UFT) design. But I think that's better than just dropping the packets.

I've been thinking more about this. A lot of this is notes for myself, but it hopefully helps others.

I was having some trouble squaring this with the ideas in RFD 21 and the current implementation in Nexus. In particular, we limit SNAT IP addresses to 16K ports. Let's suppose we follow this proposal, of "falling back" to layer-processing when a guest hits the size limit for a UFT table. I couldn't quite grok how our 16K limit is actually limiting anything though.

As a concrete example, if we allow 16K + 1 connections, what port do we rewrite the outgoing packets with? We can't pick anything outside the range that OPTE's SNat{4,6}Cfg has been provided, because those don't belong to us. Those are ports on an IP on the customer's network, and another VM may already be using them.

So it seemed like we'd need to drop something, say this new connection or the oldest. Both of those suck as a customer experience. There's nothing inherent in the VM that would appear to be limiting. The connect(2) call doesn't give some errno indicating a lack of resources. All they'd see is a black hole for data sent on that socket.

The think I'm doing here is conflating two distinct objects: the unified flow table, and the NAT pool. The former is really just to store the "compiled" set of rules applying to a particular flow, as an optimization avoiding iterating over each layer and action for every packet. The latter is the pool of IPs (always one right now) and ports that are available to the guest for use. There are two flavors of this: opte::engine::nat::Nat, which is a 1-1 NAT that allows using the whole port range, and NatPool, which is used to support a limited number of outbound connections.

These are different things. The WIP I have here makes them the same size, so that every possible connection a guest can make is equally snappy. But they are still different things, and different limits and policies may apply to them.

One thing that's confusing here is that, the current NatPool will definitely just start dropping packets if the guest exceeds the number of ports the control plane has allocated. We have issues tracking how we can learn if this happens, and whether we need to bump the number of ports for each guest. But today, if you are in such a guest, connection 16K + 1 will just appear to fly off into space.

All of this is to say that (1) the proposal of less efficient processing is fine, (2) limiting the size of the pool to the number of connections the guest can make is also fine (I think?), but unnecessary. If the guest is in fact limited, because they've an SNAT IP with 16K ports only, then the packets will already be dropped because the oxide-vpc NAT layer will just fail to generate an ActionDesc as the SNAT resource (ports, in this case) has been exhausted.

@rcgoodfellow

rcgoodfellow commented Apr 27, 2023 •

Copy link
Copy Markdown
Contributor

@bnaecker can we merge this? It fixes a crash we are encountering in system-level testing. Perhaps with some follow on issues for remaining items.

@bnaecker

Copy link
Copy Markdown
Collaborator Author

@luqmana It looks like the fix here is blocking some other work, so I'd like to merge this. I'll file a new issue to track moving towards what we discussed: new flows are either always layer-processed; displace the oldest flow; or result in entirely clearing the UFT when it's full.

@bnaecker
bnaecker merged commit 2da9dca into master Apr 27, 2023
@bnaecker
bnaecker deleted the no-limit-panics branch April 27, 2023 20:54
@luqmana luqmana mentioned this pull request May 2, 2023
luqmana added a commit that referenced this pull request May 2, 2023
Lag between when #347 was created vs merged means some tests need to be fixed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants