Skip to content

Commit

Permalink
[sg noup] net: ppp: delay PPP thread after receiving a packet
Browse files Browse the repository at this point in the history
This is a simple way to create back-pressure towards Linux.

A better approach might be to have a separate network queue for PPP and
delay while there is already a packet in the queue. This is currently not
supported by the Zephyr network queue API (there is no method to check how
many packets are currently in the queue), and would also require a bit of
cleanup in the lb_radio driver (correctly handle getting a new packet from
PPP while receiving or starting to receive a packet via Lemonbeat).

Note regarding the defaults:
- 100 ms fixed delay is meant to accommodate for sending ACKs & general
  overhead such as sending the preamble (2x 17.6 ms for packet & ACK,
  another 2x 17.6 ms if a reply is sent).
- The 400 μs variable delay is meant to allow for transmission of the data
  over UART (around 70 μs per byte at 115200 bps), over the air using
  Lemonbeat (80 μs per byte at 100 kbps), and sending an equally large
  packet back. Although a value of 300 μs should be sufficient, experiments
  show that when testing with large packets and short intervals (1450
  bytes, 100 ms interval), only 1-2 out of 3 pings get through; with a per
  byte delay of 400 μs, all 3 pings get through.

Issue: SG-21138

(cherry picked from commit e49c0f55dc9dc7500f610ed091e7a2b9734d26f8)
  • Loading branch information
andreasmueller authored and rettichschnidi committed May 8, 2024
1 parent c23af30 commit eeae945
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
14 changes: 14 additions & 0 deletions drivers/net/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ config NET_PPP_RX_PRIORITY
help
Sets the priority of the RX workqueue thread.

config NET_PPP_RX_DELAY_FIXED_MS
int "Fixed PPP RX thread delay in ms"
default 100
help
Fixed delay before reading next byte in PPP RX thread after sucessfully receiving a
packet.

config NET_PPP_RX_DELAY_PER_BYTE_US
int "Per-byte PPP RX thread delay in μs"
default 400
help
Per-byte delay before reading next byte in PPP RX thread after sucessfully receiving a
packet.

config NET_PPP_VERIFY_FCS
bool "Verify that received FCS is valid"
default y
Expand Down
13 changes: 13 additions & 0 deletions drivers/net/ppp.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ BUILD_ASSERT(UART_YIELD_INTERVAL_BYTES * 8 * 1000 /

#define RX_RINGBUF_MIN_SPACE CONFIG_NET_PPP_RINGBUF_MIN_SPACE

/* delay after successfully receiving a packet via PPP (to give lb_radio time to send it) */
#define NET_RECV_DELAY_FIXED_MS CONFIG_NET_PPP_RX_DELAY_FIXED_MS
#define NET_RECV_DELAY_PER_BYTE_US CONFIG_NET_PPP_RX_DELAY_PER_BYTE_US

enum ppp_driver_state {
STATE_HDLC_FRAME_START,
STATE_HDLC_FRAME_ADDRESS,
Expand Down Expand Up @@ -666,8 +670,17 @@ static void ppp_process_msg(struct ppp_driver_context *ppp)
net_pkt_cursor_init(ppp->pkt);
net_pkt_set_overwrite(ppp->pkt, true);

size_t size = net_pkt_get_len(ppp->pkt);
uint32_t delay_ms =
NET_RECV_DELAY_FIXED_MS + size * NET_RECV_DELAY_PER_BYTE_US / 1000;
if (net_recv_data(ppp->iface, ppp->pkt) < 0) {
net_pkt_unref(ppp->pkt);
} else {
if (delay_ms > 0) {
LOG_DBG("PPP received pkt (%d bytes); sleeping for %d ms",
size, delay_ms);
k_msleep(delay_ms);
}
}
}
}
Expand Down

0 comments on commit eeae945

Please sign in to comment.