Commit Graph
1387 Commits
Author SHA1 Message Date
Evgeny 4ba790b921 fix: SIGSEGV in tcp_proxy_client_destroy — abort pcbs before lwip destroy
Move lwip_tcp_destroy after pcb cleanup to avoid aborting pcbs
with already-freed context. Add state != CLOSED guard to tcp_abort.
2026-06-04 00:02:47 +03:00
Evgeny e0378ebf1d refactor: remove consumer_ack, isolate proxy from router internals
etcp_router:
  - removed consumer_ack field, consumer_ack function, consumer_ack logic
  - ACK always auto-sent via router_schedule_ack (10ms throttle)
  - conn_restart: local-only reset, no remote CLOSE, unified close notification (len=9)
  - START detection: only when rx_seq>0 (first conn doesn't trigger)
  - new API: etcp_router_on_send_ready / cancel_send_ready for send_q backpressure

tcp_proxy_server:
  - removed write_on_get_cb, queue_set_on_get, rconn->consumer_ack
  - removed etcp_router_conn_get, direct send_q access
  - uses etcp_router_on_send_ready for backpressure

tcp_proxy_client:
  - removed consumer_ack call
  - unified conn=NULL handler (len=9 = close/restart with node_id)

protocol: close/restart notification now len=9 (svc_id + node_id), no RESTART subcmd
2026-06-03 23:46:11 +03:00
Evgeny d784b2bfbe fix: use-after-free in tcp_conn_destroy + throttle consumer_ack
tcp_conn_destroy: set read/write queue callbacks to NULL before
queue_free to stop deferred autofetch (drain_cb on freed rc).

write_on_get_cb: throttle consumer_ack to every 8th write_queue
dequeue instead of every single one (was 30+ ACK/sec).

ack_batch field in tcp_proxy_server_conn.
2026-06-03 22:47:23 +03:00
Evgeny 0449a5fd9d test: 5 new router unit tests — RST, restart, sess_id
test_rst_flag: inject ROUTER_FLAG_RST → rconn destroyed, service notified
test_client_restart: pump random data → inject START → restart detected
test_sess_id_change: pump sess_id=0 → inject sess_id=1+START → detected
test_sess_id_wrap: pump sess_id=3, peer_sess_id=3 → inject sess_id=0+START → wrap detected
test_server_reinit: reinit router → new rconn start_sent=0, sess_id=0

inject() extended with uint8_t flags parameter.
pump_data helper for random data injection.
test_handler tracks g_notify_count for close/restart notifications.

fix: etcp_router_recv_cb — return after RST flag to avoid NULL rconn deref
2026-06-03 21:56:11 +03:00
Evgeny d0029178e7 feat: 1-byte flags in SVC_ROUTE_HDR — START, RST, 2-bit sess_id
SVC_ROUTE_HDR: +uint8_t flags (23 bytes total).
  bit7=START (0x80) — first data packet of new session
  bit6=RST (0x40) — sequence violation detected
  bit5-4=sess_id (0-3) — cyclically incremented on restart

router_send_one_flags: sets START+sess_id on first data packet.
router_send_ack: sets sess_id only (no START for ACK).
etcp_router_recv_cb: detects restart via sess_id change or START flag.
  Old seq=0 heuristic replaced with explicit flags.
etcp_router_conn_get: init sess_id=0, peer_sess_id=0, start_sent=0.
etcp_router_conn_restart: increment sess_id before closing rconn.
2026-06-03 21:20:05 +03:00
Evgeny ca7f6c0d01 feat: peer restart detection + etcp_router_conn_restart with service notification
etcp_router: new etcp_router_conn_restart(inst, node_id, svc_id) — closes
all rconns for the peer, notifies service via cb(NULL, entry) with
node_id, resets seq state.

seq=0 detection in etcp_router_recv_cb: when rx_seq >= 256 (MAX_INFLIGHT)
and a data packet arrives with seq=0, detect peer restart and reset rconn.

tcp_proxy_client: handle TCP_PROXY_SUBCMD_RESTART — clear all client conns
and server conns for the restarted peer.

Threshold prevents false positives on in-window seq=0 duplicates.
2026-06-03 20:45:42 +03:00
Evgeny 609be50b8f feat: ETCP trace logging — ETCP_SEND, ETCP_RECV, SEND_Q_DRAIN, SEND_Q_ACKED, CONSUMER_ACK, ACK_SEND
Six key log points at DEBUG level to trace the full ACK chain:
  ETCP_SEND  — server sends direction A data
  ETCP_RECV  — received any svc_route packet
  SEND_Q_DRAIN — send_q drained after client ACK
  SEND_Q_ACKED — server received client ACK (direction A)
  CONSUMER_ACK  — server sends consumer ACK (direction B)
  ACK_SEND      — low-level ACK packet sent
2026-06-03 19:27:31 +03:00
Evgeny e4e5ed71f8 fix: etcp_router_consumer_ack — use router_ack_do_send instead of schedule
router_schedule_ack skips when rx_seq==last_sent_ack_seq (no-op),
so the first consumer_ack call after setting consumer_ack=1 was
silently ignored. Client never got ACK → send_q stuck at 256.

router_ack_do_send always sends the ACK unconditionally.
2026-06-03 19:20:06 +03:00
Evgeny 4716641dff fix: initial consumer_ack in handle_connect + diag: send_q, BACKPRESSURE, ACK logs
handle_connect: send first consumer_ack after setting consumer_ack=1
to unblock client router — fixes rq=32/send_q=256 deadlock.

diag_timer_cb: added send_q=N to DIAG line for instant root cause visibility.
read_queue_drain_cb: SOCK:BACKPRESSURE log with send_q count.
write_on_get_cb: SOCK:ACK debug log on each consumer_ack.
2026-06-03 19:07:06 +03:00
Evgeny bbf39c091d feat: send_q backpressure + ll_queue on_get callback + consumer_ack flow control
etcp_router: ROUTER_MAX_SEND_Q_PACKETS=256, router_enqueue_send returns
error when send_q full, etcp_route_send propagates -1 on backpressure.

ll_queue: queue_set_on_get — deferred callback after each queue_data_get.
Replaces manual consumer_ack polling with queue-driven notification.

tcp_proxy_server: consumer_ack=1, write_on_get_cb triggers ACK on
each write_queue dequeue. Full backpressure chain:
  write_queue full → no dequeue → no ACK → sender stops → all queues pause
  write_queue drains → dequeue → on_get → ACK → sender resumes
2026-06-03 18:51:12 +03:00
Evgeny a0da29f1eb fix: simplify diag_timer_cb — use pool->free_count instead of allocations-reuse
DIAG line now shows clear metrics:
  rq/wq — queue sizes
  ent_f/dat_f — free_count of entry/data pools (→0 = leak)
  wbuf — partial send stuck
  tcp_rb/tcp_sb — kernel socket buffers
2026-06-03 17:58:08 +03:00
Evgeny dc2b394e7a feat: per-connection diagnostic timer in tcp_proxy_server
1-sec diag_timer_cb prints rq/wq counts+bytes, outstanding pool
allocations, tcp kernel buffer sizes (SO_RCVBUF/SO_SNDBUF).
Started in handle_connect, cancelled in conn_free.
2026-06-03 17:36:45 +03:00
Evgeny 04f6b3e040 refactor: remove tcp_conn_push_write, use direct queue put with chunking
External code pushes to write_queue directly (entry_pool + data_pool).
Added queue_set_threshold(write_queue, 32, 0) for write backpressure.
Tests split data into write_chunk_size chunks before queue put.
2026-06-03 16:38:09 +03:00
Evgeny 63f019530b feat: tcp_io library + uasync socket toggle + ll_queue empty_callback
lib/u_async: uasync_set_socket_read/write — O(1) EPOLL_CTL_MOD toggle per socket
lib/ll_queue: queue_set_empty_callback — one-shot deferred callback on queue drain
lib/tcp_io: TCP connection management via uasync + ll_queue with pool alloc
  - read: direct recv into pool buffer, high/low water flow control
  - write: autofetch via ll_queue callback, partial send via write_buf
  - on_fin/on_error/on_flushed callbacks, FIN deferred until read_queue drained
  - getpeername for pre-connected socket detection
tcp_proxy_server: migrated to tcp_io (22→13 fields, 437→270 lines, -7 functions)
tests: test_tcp_io (5 tests), test_tcp_proxy_server rewritten for tcp_conn
2026-06-03 16:24:46 +03:00
Evgeny 3533ec3122 fix: memory leaks in TCP proxy server/client
- tcp_proxy_server: close socket + conn_count-- on uasync_add_socket_t failure
- tcp_proxy_server: conn_count-- on connect() failure
- tcp_proxy_client: queue_free(to_lwip) + tcp_abort on send_connect failure
- tcp_proxy_client: free tx_buf + cancel tx_waiter in destroy loop
2026-06-03 12:55:00 +03:00
Evgeny ac3fe79db8 etcp_router: consumer-driven ACK with interval throttle (10ms)
- consumer_ack flag: ACK sent only on consumption, not assembly
- etcp_router_consumer_ack() — called from tcp_proxy_client feed_from_transport
- last_ack_sent_tb: interval-based throttle — send immediately if >=10ms passed,
  otherwise timer for remaining time
- timer rules: NULL handle after cancel/fire, NULL check before start
- ROUTER_ACK_INTERVAL_TB 1000→100 (100ms→10ms)
- test_etcp_router_unit: updated test 14 for new immediate-send behavior
2026-06-03 11:50:50 +03:00
Evgeny e7eb24537d fix: proxy ERROR handling — single CLOSE on error, no ERROR response cascade
- tcp_proxy_client_handle_error: set error flag + send CLOSE once if flag was clear
- tcp_proxy_client_handle_data/handle_close: drop silently, don't send ERROR back
- tcp_proxy_server_handle_data/handle_close: drop silently, don't send ERROR back
- Add recv→send 1MB integration test (3 clients × 3 requests, shell + python)
2026-06-03 10:36:55 +03:00
Evgeny 51d5790938 fix: remove tautological BBR_EXTRA_ACKED_GAIN checks 2026-06-03 02:21:26 +03:00
Evgeny 331d43c6f7 fix: proxy ERROR loop, BBR test backpressure, O(1) u_free, test ordering
- tcp_proxy: ERROR silently dropped (no ERROR->ERROR loop), Russian removed from logs
- etcp_bbr: swap min(lo) and max(4*mss) in bbr_bound_cwnd_for_inflight_model
- test_etcp_bbr: fix cwnd comparison (packets vs bytes)
- ll_queue: double-put detection (abort), Floyd cycle detection in consistency check
- ll_queue: QUEUE_DEBUG commented out (was always-on, O(N) per put)
- mem: u_free_impl now O(1) via doubly-linked list, double-free detection
- bbr_integration: queue_waiter_wait + call_soon backpressure (threshold=1)
- tests/Makefile.am: fast tests first, *.log cleanup before run
2026-06-03 02:06:09 +03:00
Evgeny 7f5325ac24 etcp_router: fix inflight underflow by splitting rx_acked into tx_acked + last_sent_ack_seq
rx_acked was used for two conflicting purposes:
1. remote ack of our sends (inflight = tx_seq - rx_acked)
2. our last sent ACK seq (dedup: rx_seq != rx_acked)

When the ack timer fired and set rx_acked = rx_seq, it overwrote
the inflight-tracking value. If rx_seq > tx_seq, the computation
tx_seq - rx_acked underflowed (e.g. 18 - 24 = 0xFFFFFFFA),
permanently blocking router_drain_send_q and causing send_q to
grow indefinitely.

Fix:
- Split rx_acked into tx_acked (remote ack, for inflight) and
  last_sent_ack_seq (our ACK, for dedup)
- Incoming ACK handler only advances tx_acked forward (stale guard)
- Use int32_t cast on all inflight comparisons to handle stale states
- Add etcp_router architecture diagram (doc/etcp_router_arch.md)
2026-06-02 22:38:20 +03:00
Evgeny fceec1aea6 fix: move debug logs before async operations (queue_put, timer_set, resume) for meaningful log ordering 2026-06-02 21:53:51 +03:00
Evgeny c5c4967b69 1 2026-06-02 21:19:07 +03:00
Evgeny 4556f327c2 1 2026-06-02 21:05:06 +03:00
Evgeny 1f4fe37fe5 proxy: rename + simplify + backpressure
- tcp_proxy/remote_proxy → tcp_proxy_client/tcp_proxy_server
- Все proxy файлы перенесены в src/proxy/
- Конфиг: [tcp_proxy]→[tcp_proxy_client], [remote_proxy]→[tcp_proxy_server]
- Убран subcmd CONNECTED (0x02) — клиент шлёт DATA сразу после CONNECT
- Backpressure: etcp_router_waiter_register/cancel через ll_queue threshold waiter
- Клиент: recv_cb send fail → tx_buf+waiter, не tcp_recved() (окно lwIP=0)
- Сервер: read_cb send fail → pause_buf+waiter, пауза read_id
- CLOSE/ERROR отправляется только после drain всех очередей
- Fail: conn не найден/closed → send ERROR вместо CLOSE/silent drop
- PROTOCOL.md: полная архитектура с диаграммами состояний и потоков
2026-06-02 18:02:37 +03:00
Evgeny e7a9163011 remote_proxy: гарантированная доставка CLOSE/ERROR обратно (exit→client) через retry-таймер
- remote_proxy_conn: close_pending, close_timer, close_backoff
- rp_send_close/rp_send_error: при сбое etcp_route_send → close_pending=1 + таймер с backoff
- rp_close_retry_cb: повторная отправка CLOSE/ERROR с экспоненциальным backoff (50→5000 tb)
- rp_conn_free: очистка close_timer
2026-06-01 23:31:39 +03:00
Evgeny b43de4c1f0 tcp_proxy: гарантированная доставка CLOSE/ERROR через retry при сбое маршрута
- proxy_conn.close_pending: флаг недоставленного CLOSE/ERROR
- send_close/send_error: при возврате -1 выставляют close_pending=1
- proxy_poll_cb: периодический retry close_pending → CLOSE дойдёт при восстановлении ETCP
- proxy_recv_cb: проверка !close_pending перед повторной отправкой close
2026-06-01 23:26:15 +03:00
Evgeny 60800a7f35 tcp_proxy: не слать данные в lwip после закрытия TUN-стороны (tun_closed)
- handle_data: добавить pc->tun_closed в проверку сброса данных от exit
- proxy_feed_from_transport: не писать в PCB после tun_closed
- предотвращает ERR_RST (-14) при записи в TIME_WAIT PCB
2026-06-01 22:59:20 +03:00
Evgeny 6d661360b1 etcp: адаптивный keepalive — отдельная команда ETCP_KEEPALIVE с period_ms
- ETCP_KEEPALIVE=0x08, формат: [0x08][period_lo][period_hi]
- link->ka_period_ms: 200ms при трафике, ×1.05+1 в idle, потолок 10s
- keepalive_timeout = peer_period * KA_TIMEOUT_MULT(10)
- приём KA обновляет keepalive_timeout адаптивно (2000ms→100000ms)
- флаг recv_keepalive по-прежнему в flag_up для быстрого сигнала
2026-06-01 20:15:06 +03:00
Evgeny b2e72db31b etcp: link_status = remote_keepalive && recv_keepalive (вместо только remote)
- link_status теперь требует двусторонней слышимости: remote && local
- recv_keepalive восстанавливается ДО вычисления link_status (избегает заклинивания)
- loadbalancer_link_ready по переходу 0→1 с проверкой was_up
2026-06-01 19:43:39 +03:00
Evgeny c5b4ec7a05 bbr: исправить бесконечную рекурсию в bbr_now — bbr_now(bbr) → get_time_tb() 2026-06-01 18:28:02 +03:00
Evgeny 4f0baa37db bbr: исправить передачу потерь в BBR + пол для cwnd на уровне BBR_CWND_MIN_TARGET*mss
- etcp.c: rs->lost из link->bbr_loss_since_ack (был хардкод 0), вызов bbr_note_loss при ретрансмите
- etcp_connections.h: поле bbr_loss_since_ack для per-link учёта потерь в байтах
- etcp_bbr.c: везде где BBR_CWND_MIN_TARGET — умножить на mss (юнит-мизматч пакеты/байты)
  - bbr_quantization_budget, bbr_set_cwnd, bbr_bound_cwnd_for_inflight_model, bbr_probe_rtt_cwnd
  - bbr_init_lower_bounds: инициализация inflight_hi из cwnd (был ~0U навсегда)
- control_server.c + etcpmon: экспорт BBR-метрик (bw_hi/lo, inflight_hi/lo, режим, потери)
- tests/bbr_integration: интеграционный тест BBR (15с, 50ms/5%loss/1Mbit) с таблицей метрик
- устранён death spiral BBR при высоких потерях: cwnd теперь не падает ниже 4*mss
2026-06-01 17:50:16 +03:00
Evgeny 6c79939db3 congestion: заменить AIMD на BBR v3 (per-link), добавить юнит-тесты BBR
- Новые файлы: src/etcp_bbr.h/c — полный порт BBR v3 из BBR/bbr_v3.c (референс сохранён)
- struct ETCP_LINK: удалены AIMD-поля (rtt_history, rtt_swm, stat_win, stats_timer, inflight_phase...), добавлены bbr*, delivered_bytes, bbr_pacing_rate
- struct INFLIGHT_PACKET: +3 поля для rate_sample (delivered_at_send, inflight_at_send, is_app_limited)
- etcp_ack_recv: вызов bbr_main() с rate_sample, обновление inflight_lim_bytes и pacing
- etcp_request_pkt: заполнение BBR rate_sample полей при отправке
- Шейпер: bandwidth обновляется из bbr_pacing_rate
- Конфиг: inflight_min/max заменены на bbr_max_cwnd
- Дамп: BBR поля (mode, cycle, pacing, min_rtt_us, bw_hi/lo, inflight_hi/lo)
- Time injection: bbr->now_tb для детерминированных тестов
- tests/test_etcp_bbr.c: 8 юнит-тестов (init, startup, full_bw/drain, loss, cwnd cap, loss cut, fast_path, probe_rtt)
- Отключены test_etcp_congestion и test_etcp_reinit_inflight (старый congestion control)
- 40/40 тестов пройдено
2026-06-01 12:58:31 +03:00
Evgeny ed6b4d1046 debug: выделить link keepalive в отдельную категорию KEEPALIVE (21), добавить DEBUG_DEBUG при приёме keepalive-пакета 2026-06-01 10:03:14 +03:00
Evgeny 803387f2b4 router close_all_for_node: integrate with BGP remove_conn, fail fast on no route, stop PAUSE cycle
- route_bgp_remove_conn: call etcp_router_conn_close_all_for_node() when node loses all paths
- etcp_route_send: check BGP route before creating conn (return -1 early, no conn leak)
- remote_proxy: check rp_send_msg return value, close conn on error to break PAUSE↔FLUSH cycle
2026-06-01 01:44:29 +03:00
Evgeny 53d3f26e87 etcp_router: CLOSE/RST signaling flags in seq, close notification to services
- ROUTER_SEQ_CLOSE_FLAG (0x80000000): normal conn close
- ROUTER_SEQ_RST_FLAG   (0x40000000): conn not found
- router_send_to(): SVC_ROUTE with flags, no conn needed
- router_send_close_to_service(): notify local service [svc_id],len=1
- router_close_and_notify(): CLOSE to remote + close to local + cleanup
- etcp_router_conn_close(): sends CLOSE to remote before cleanup
- etcp_router_conn_close_all_for_node(): close all conns for dead peer
- recv_cb: flags checked only when pl_len==0 (avoid seq collision)
- etcp_router_destroy: close notification to each service
2026-06-01 01:12:52 +03:00
Evgeny 21cfe2d381 etcp_router: remove legacy, auto-conn for all services, send_q inflight control, remove tcp_proxy internal seq
- All SVC_ROUTE traffic now auto-creates ETCP_ROUTER_CONN, no legacy path
- etcp_route_send: auto-conn + seq + inflight check → send_q if full
- Inflight control via send_q: queue instead of drop, drain on ACK, retry timer 50ms
- router_send_one(): unified SVC_ROUTE wrapper with seq assignment
- router_deliver/try_assembly: pass real ETCP_CONN* to handlers (NAT needs it)
- tcp_proxy: remove internal 16-bit seq (send_seq/recv_last_seq/recv_init), shrink HDR 8→6
- remote_proxy: same seq removal, HDR/constants adjusted
- conn init/close/destroy: manage send_q, send_resume_timer lifecycle
- test_etcp_router_unit: −legacy, +send_q inflight test (18 tests)
2026-05-31 22:55:23 +03:00
Evgeny ac3cdd7739 etcp_router: simplified TCP — reorder recv_q, dedup, ACK timers, inflight control, per-(remote,svc) conn state
- New SVC_ROUTE_HDR (packed struct, 22 bytes): cmd+dst+src+seq+svc_id
- ETCP_ROUTER_CONN: state per (remote_node_id, svc_id), hash-indexed in router_conns
- Reorder via recv_q (hash by seq), dedup with 32-bit circular compare
- Periodic ACK (100ms), idle ACK (500ms), inflight limit via tx_seq - rx_acked
- Legacy mode: no conn → direct delivery without reorder
- etcp_router_conn_get/send/close API for seq-managed connections
- Unit test test_etcp_router_unit: 18 tests, 3ms, no ETCP/sockets/BGP
2026-05-31 21:59:46 +03:00
Evgeny 0f0857c22a add non-blocking send with EAGAIN+retry timer, normalizer flow control on recv, remove client-side send_buf buffering 2026-05-31 18:09:50 +03:00
Evgeny c4e80e6fd7 add traffic debug: PROXY RECV/SEND/FEED for full DATA path tracing on client 2026-05-31 17:21:47 +03:00
Evgeny 9d4c5b00f4 fix: stop forwarding after CLOSE, send CLOSE for non-existent conn in proxy data handlers 2026-05-31 16:54:36 +03:00
Evgeny f691c01b45 перевод комментариев tcp/udp/icmp proxy на русский 2026-05-31 16:32:10 +03:00
Evgeny 9a5d8797af tun: socket pair in test_mode, remove ip_fd_mode from tcp_proxy, fix test_remote_proxy stream_id size 2026-05-31 15:58:30 +03:00
Evgeny e5d11ef9c1 refactor tcp_proxy: remove local socket mode, simplify architecture
- remove sock_transport, transport abstraction, active connections API
- simplify proxy_conn to 13 fields (was 15), remove tcp_proxy_mapping linked list
- new protocol: stream_id 4B, seq 2B cyclic, HDR_SIZE 8B
- add ERROR subcmd for immediate abort, CLOSE for graceful half-close
- proper half-close: shutdown SHUT_WR, keep reading, no timer
- exhaustive DEBUG_ERROR/WARN on all error/unusual branches
- config parser: report unknown options with filename:line and valid keys
2026-05-31 13:35:26 +03:00
Evgeny c3eccd7b35 add backpressure support: uip_retry_timer, feed drain at closing_tun/sent, send buffering with retry
- proxy_uip_retry_cb: retry send from uip_to_transport every 5ms
- proxy_recv_cb: buffer to uip_to_transport when send fails, set retry timer
- proxy_feed: drain transport_to_uip when closing_tun || closing_sent (prevents RST)
- proxy_recv_cb(p==NULL): only tcp_recved + closing_tun, no drain
- proxy_poll_cb/tcp_proxy_destroy: cancel retry timer on cleanup
- uip_retry_timer field in proxy_conn
2026-05-31 03:31:03 +03:00
Evgeny 616986457f add DEBUG_ERROR in all silent proxy error paths: NULL checks, alloc fails, transport send fail, data drop 2026-05-31 01:58:44 +03:00
Evgeny 10b9c93fcb add SOCK:EVENT unified diagnostics on exit: NEW/CONN/SEND/RECV/CLOSE/TIMER with fd, stream, total count 2026-05-31 01:31:59 +03:00
Evgeny be791ee7b2 drain transport_to_uip at FIN to prevent data delivery after close (eliminates RST storm) 2026-05-31 01:05:12 +03:00
Evgeny 01254406d8 fix tun_platform_write return value on FreeBSD: subtract 4-byte AF_INET header from writev result 2026-05-31 00:53:19 +03:00
Evgeny c4b1436919 add TUN write hex dump diagnostic (ip_total, ver_ihl, proto, first 32 bytes hex) in traffic category 2026-05-31 00:44:17 +03:00
Evgeny f708b1d095 add TUN write diagnostic: log tun_platform_write errors 2026-05-31 00:31:47 +03:00