Добавлен group_id в etcp_router и topo_group, удалён msg_transport
- etcp_router: SVC_ROUTE_HDR (+8B), ETCP_ROUTER_CONN/TRANSIT_QUEUE хеши расширены под group_id - Все caller'ы (proxy, routing, conn_mgr, nat, chatgui, tests) обновлены - topo_group: глобальный реестр TOPO_NODE по node_id, ref_count=0 при создании - BGP-обмен активируется для всех групп (а не только дефолтной) - Протокол: group_id добавлен в WITHDRAW/TABLE_REQ/TABLE_COMPLETE/ERR_GROUP_MISMATCH - Per-connection коллбэки заменены на instance-level conn_status - msg_transport полностью удалён из проекта 57/57 тестов
This commit is contained in:
1 parent
5265783164
commit
852ce401e7
37 files changed
+375
-1125
No files matched your search
@@ -238,7 +238,7 @@ int conn_mgr_disconnect_node(struct CONN_MGR* mgr, uint64_t node_id) {
|
||||
pkt.subcmd = CONN_MGR_SUBCMD_DISCONNECT;
|
||||
pkt.node_id = node_id;
|
||||
struct ll_entry* qe = queue_entry_new(0);
|
||||
if (qe) { qe->dgram = u_malloc(sizeof(pkt)); memcpy(qe->dgram, &pkt, sizeof(pkt)); qe->len = sizeof(pkt); etcp_route_send(mgr->instance, node_id, qe, 1); }
|
||||
if (qe) { qe->dgram = u_malloc(sizeof(pkt)); memcpy(qe->dgram, &pkt, sizeof(pkt)); qe->len = sizeof(pkt); etcp_route_send(mgr->instance, TOPO_GROUP_UTUN, node_id, qe, 1); }
|
||||
cm_clear_nodeinfo(mgr, node_id);
|
||||
cm_entry_destroy(entry);
|
||||
entry->state = CONN_MGR_STATE_DISCONNECTED;
|
||||
@@ -272,7 +272,7 @@ int conn_mgr_send(struct CONN_MGR* mgr, uint64_t node_id, struct ll_entry* entry
|
||||
struct CONN_MGR_ENTRY* e = cm_find_entry(mgr, node_id);
|
||||
if (!e || e->state != CONN_MGR_STATE_CONNECTED) { queue_entry_free(entry); return -1; }
|
||||
e->last_traffic_tb = get_time_tb();
|
||||
struct ETCP_ROUTER_CONN* rconn = etcp_router_conn_get(mgr->instance, node_id, ETCP_RT_ID_CONN_MGR);
|
||||
struct ETCP_ROUTER_CONN* rconn = etcp_router_conn_get(mgr->instance, TOPO_GROUP_UTUN, node_id, ETCP_RT_ID_CONN_MGR);
|
||||
if (!rconn) { queue_entry_free(entry); return -1; }
|
||||
int ret = etcp_router_conn_send(rconn, entry->dgram, entry->len);
|
||||
queue_entry_free(entry);
|
||||
@@ -567,7 +567,7 @@ static void cm_start_phase_reverse(struct CONN_MGR_ENTRY* entry) {
|
||||
if (!qe) { u_free(pkt); cm_deliver_result(entry, CONN_MGR_ERR_INTERNAL); return; }
|
||||
qe->dgram = pkt;
|
||||
qe->len = (uint16_t)pkt_size;
|
||||
etcp_route_send(entry->mgr->instance, entry->node_id, qe, 1);
|
||||
etcp_route_send(entry->mgr->instance, TOPO_GROUP_UTUN, entry->node_id, qe, 1);
|
||||
|
||||
struct cm_reverse_pending* rp = u_calloc(1, sizeof(struct cm_reverse_pending));
|
||||
if (rp) { rp->request_id = req_id; rp->entry = entry; rp->next = entry->mgr->reverse_pending;
|
||||
@@ -625,7 +625,7 @@ static int cm_start_phase_indirect(struct CONN_MGR_ENTRY* entry) {
|
||||
qe->dgram = u_malloc(sizeof(req));
|
||||
memcpy(qe->dgram, &req, sizeof(req));
|
||||
qe->len = (uint16_t)sizeof(req);
|
||||
etcp_route_send(entry->mgr->instance, entry->node_id, qe, 1);
|
||||
etcp_route_send(entry->mgr->instance, TOPO_GROUP_UTUN, entry->node_id, qe, 1);
|
||||
entry->main.timer = uasync_set_timeout(mgr->instance->ua, CONN_MGR_INTERM_EXCHANGE_TIMEOUT_MS * 10,
|
||||
entry, cm_exchange_timeout_cb, "conn_mgr_interm_exch");
|
||||
{
|
||||
@@ -693,7 +693,7 @@ static void cm_compute_intermediaries(struct CONN_MGR_ENTRY* entry, struct CONN_
|
||||
if (nq) pkt.selected[i].rtt = cm_get_node_min_rtt(nq);
|
||||
}
|
||||
struct ll_entry* qe = queue_entry_new(0);
|
||||
if (qe) { qe->dgram = u_malloc(sizeof(pkt)); memcpy(qe->dgram, &pkt, sizeof(pkt)); qe->len = sizeof(pkt); etcp_route_send(entry->mgr->instance, entry->node_id, qe, 1); }
|
||||
if (qe) { qe->dgram = u_malloc(sizeof(pkt)); memcpy(qe->dgram, &pkt, sizeof(pkt)); qe->len = sizeof(pkt); etcp_route_send(entry->mgr->instance, TOPO_GROUP_UTUN, entry->node_id, qe, 1); }
|
||||
|
||||
entry->conn_type = CONN_TYPE_INDIRECT;
|
||||
entry->state = CONN_MGR_STATE_CONNECTED;
|
||||
@@ -877,7 +877,7 @@ static void cm_handle_interm_exchange_req(struct ETCP_CONN* conn,
|
||||
qe->dgram = u_malloc(resp_size);
|
||||
memcpy(qe->dgram, &resp, resp_size);
|
||||
qe->len = (uint16_t)resp_size;
|
||||
etcp_route_send(mgr->instance, conn->peer_node_id, qe, 1);
|
||||
etcp_route_send(mgr->instance, TOPO_GROUP_UTUN, conn->peer_node_id, qe, 1);
|
||||
}
|
||||
DEBUG_INFO(DEBUG_CATEGORY_GENERAL, "conn_mgr: sent EXCHANGE_RESP to 0x%016llx my=%u your=%u",
|
||||
(unsigned long long)conn->peer_node_id, resp.my_count, resp.your_count);
|
||||
|
||||
@@ -32,7 +32,7 @@ static void router_try_assembly(struct ETCP_ROUTER_CONN* rconn, struct ETCP_CONN
|
||||
static void router_deliver(struct ETCP_ROUTER_CONN* rconn, struct ETCP_CONN* conn, const uint8_t* payload, size_t payload_len);
|
||||
static int router_send_one(struct ETCP_ROUTER_CONN* rconn, const uint8_t* payload, size_t pl_len);
|
||||
static int router_send_one_flags(struct ETCP_ROUTER_CONN* rconn, const uint8_t* payload, size_t pl_len, uint8_t flag_bits, int is_signed);
|
||||
static void router_send_to(struct UTUN_INSTANCE* inst, uint64_t dst, uint8_t svc_id, uint8_t flag_bits);
|
||||
static void router_send_to(struct UTUN_INSTANCE* inst, uint64_t group_id, uint64_t dst, uint8_t svc_id, uint8_t flag_bits);
|
||||
static void router_drain_send_q(struct ETCP_ROUTER_CONN* rconn);
|
||||
static void router_send_resume_cb(void* arg);
|
||||
static void router_no_route_retry_cb(void* arg);
|
||||
@@ -58,23 +58,26 @@ static void router_update_minrtt(struct ETCP_ROUTER_CONN* rconn);
|
||||
// Транзитные очереди — per (src, dst) pair, backpressure через waiter на send_input_q
|
||||
|
||||
static struct TRANSIT_QUEUE* transit_queue_find(struct ETCP_CONN* conn,
|
||||
uint64_t group_id,
|
||||
uint64_t src_node_id, uint64_t dst_node_id) {
|
||||
if (!conn->transit_queues) return NULL;
|
||||
uint8_t key[16];
|
||||
memcpy(key, &src_node_id, 8);
|
||||
memcpy(key + 8, &dst_node_id, 8);
|
||||
uint8_t key[24];
|
||||
memcpy(key, &group_id, 8);
|
||||
memcpy(key + 8, &src_node_id, 8);
|
||||
memcpy(key + 16, &dst_node_id, 8);
|
||||
return (struct TRANSIT_QUEUE*)queue_find_data_by_index(conn->transit_queues, key);
|
||||
}
|
||||
|
||||
static struct TRANSIT_QUEUE* transit_queue_get_or_create(struct ETCP_CONN* conn,
|
||||
uint64_t group_id,
|
||||
uint64_t src_node_id, uint64_t dst_node_id) {
|
||||
if (!conn->transit_queues) {
|
||||
struct UASYNC* ua = conn->instance ? conn->instance->ua : NULL;
|
||||
if (!ua) { DEBUG_ERROR(DEBUG_CATEGORY_ETCPROUTE, "transit_queue_get_or_create: no ua"); return NULL; }
|
||||
conn->transit_queues = queue_new(ua, TRANSIT_QUEUE_HASH, 0, 16, "transit_q_reg");
|
||||
conn->transit_queues = queue_new(ua, TRANSIT_QUEUE_HASH, 0, 24, "transit_q_reg");
|
||||
if (!conn->transit_queues) { DEBUG_ERROR(DEBUG_CATEGORY_ETCPROUTE, "transit_queue_get_or_create: queue_new failed"); return NULL; }
|
||||
}
|
||||
struct TRANSIT_QUEUE* tq = transit_queue_find(conn, src_node_id, dst_node_id);
|
||||
struct TRANSIT_QUEUE* tq = transit_queue_find(conn, group_id, src_node_id, dst_node_id);
|
||||
if (tq) return tq;
|
||||
size_t data_size = sizeof(struct TRANSIT_QUEUE) - sizeof(struct ll_entry);
|
||||
tq = (struct TRANSIT_QUEUE*)queue_entry_new(data_size);
|
||||
@@ -83,14 +86,18 @@ static struct TRANSIT_QUEUE* transit_queue_get_or_create(struct ETCP_CONN* conn,
|
||||
return NULL;
|
||||
}
|
||||
memset(&tq->waiter, 0, sizeof(tq->waiter));
|
||||
tq->group_id = group_id;
|
||||
tq->src_node_id = src_node_id;
|
||||
tq->dst_node_id = dst_node_id;
|
||||
tq->conn = conn;
|
||||
tq->q = queue_new(conn->instance->ua, 0, 0, 0, "transit_pair_q");
|
||||
if (!tq->q) { queue_entry_free(&tq->ll); return NULL; }
|
||||
memcpy(tq->ll.data, &src_node_id, 8);
|
||||
memcpy(tq->ll.data + 8, &dst_node_id, 8);
|
||||
memcpy(tq->ll.data, &group_id, 8);
|
||||
memcpy(tq->ll.data + 8, &src_node_id, 8);
|
||||
memcpy(tq->ll.data + 16, &dst_node_id, 8);
|
||||
queue_data_put_with_index(conn->transit_queues, &tq->ll);
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCPROUTE, "transit_q: new src=%016llx dst=%016llx",
|
||||
(unsigned long long)src_node_id, (unsigned long long)dst_node_id);
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCPROUTE, "transit_q: new group=%016llx src=%016llx dst=%016llx",
|
||||
(unsigned long long)group_id, (unsigned long long)src_node_id, (unsigned long long)dst_node_id);
|
||||
return tq;
|
||||
}
|
||||
|
||||
@@ -136,11 +143,13 @@ void etcp_router_transit_queues_destroy(struct ETCP_CONN* conn) {
|
||||
// ====================================================================
|
||||
|
||||
static struct ETCP_ROUTER_CONN* router_conn_find(struct UTUN_INSTANCE* inst,
|
||||
uint64_t group_id,
|
||||
uint64_t remote_node_id, uint8_t svc_id) {
|
||||
if (!inst || !inst->router_conns) return NULL;
|
||||
uint8_t key[9];
|
||||
memcpy(key, &remote_node_id, 8);
|
||||
key[8] = svc_id;
|
||||
uint8_t key[17];
|
||||
memcpy(key, &group_id, 8);
|
||||
memcpy(key + 8, &remote_node_id, 8);
|
||||
key[16] = svc_id;
|
||||
return (struct ETCP_ROUTER_CONN*)queue_find_data_by_index(inst->router_conns, key);
|
||||
}
|
||||
|
||||
@@ -161,6 +170,7 @@ static int router_send_one_flags(struct ETCP_ROUTER_CONN* rconn, const uint8_t*
|
||||
if (!dgram) { if (!flag_bits) rconn->tx_seq--; rconn->c_pkts_send_err++; return -1; }
|
||||
struct SVC_ROUTE_HDR* hdr = (struct SVC_ROUTE_HDR*)dgram;
|
||||
hdr->cmd = ETCP_RT_ID_SVC_ROUTE;
|
||||
hdr->group_id = rconn->group_id;
|
||||
hdr->dst_node_id = rconn->remote_node_id;
|
||||
hdr->src_node_id = inst->node_id;
|
||||
hdr->seq = seq;
|
||||
@@ -178,7 +188,7 @@ static int router_send_one_flags(struct ETCP_ROUTER_CONN* rconn, const uint8_t*
|
||||
if (pl_len > 0) memcpy(dgram + SVC_ROUTE_HDR_SIZE, payload, pl_len);
|
||||
|
||||
struct ETCP_CONN* conn = NULL;
|
||||
struct TOPO_GROUP* group = topo_groups_get_default(inst->topo_groups);
|
||||
struct TOPO_GROUP* group = topo_groups_find(inst->topo_groups, rconn->group_id);
|
||||
if (group) {
|
||||
struct TOPO_NODEQ* nq = topo_node_find_by_id(group, rconn->remote_node_id);
|
||||
if (nq && nq->conn_mgr_type == CONN_TYPE_INDIRECT && nq->conn_mgr_intermediariy_count > 0) {
|
||||
@@ -258,12 +268,13 @@ static int router_send_one_flags(struct ETCP_ROUTER_CONN* rconn, const uint8_t*
|
||||
}
|
||||
|
||||
// Отправка без conn (RST для неизвестного src)
|
||||
static void router_send_to(struct UTUN_INSTANCE* inst, uint64_t dst, uint8_t svc_id, uint8_t flag_bits) {
|
||||
struct ETCP_CONN* conn = topo_group_find_conn_for_node(topo_groups_get_default(inst->topo_groups), dst);
|
||||
static void router_send_to(struct UTUN_INSTANCE* inst, uint64_t group_id, uint64_t dst, uint8_t svc_id, uint8_t flag_bits) {
|
||||
struct ETCP_CONN* conn = topo_group_find_conn_for_node(topo_groups_find(inst->topo_groups, group_id), dst);
|
||||
if (!conn) return;
|
||||
struct SVC_ROUTE_HDR* hdr = (struct SVC_ROUTE_HDR*)u_malloc(SVC_ROUTE_HDR_SIZE);
|
||||
if (!hdr) return;
|
||||
hdr->cmd = ETCP_RT_ID_SVC_ROUTE;
|
||||
hdr->group_id = group_id;
|
||||
hdr->dst_node_id = dst;
|
||||
hdr->src_node_id = inst->node_id;
|
||||
hdr->seq = 0;
|
||||
@@ -314,7 +325,7 @@ static void router_close_and_notify(struct ETCP_ROUTER_CONN* rconn) {
|
||||
rconn->closed = 1;
|
||||
router_send_close_to_service(rconn);
|
||||
// Отправляем CLOSE удалённой стороне
|
||||
struct ETCP_CONN* conn = topo_group_find_conn_for_node(topo_groups_get_default(inst->topo_groups), rconn->remote_node_id);
|
||||
struct ETCP_CONN* conn = topo_group_find_conn_for_node(topo_groups_find(inst->topo_groups, rconn->group_id), rconn->remote_node_id);
|
||||
if (conn) router_send_one_flags(rconn, NULL, 0, ROUTER_FLAG_CLOSE, 0);
|
||||
// Таймеры
|
||||
if (rconn->ack_timer) { uasync_cancel_timeout(inst->ua, rconn->ack_timer); rconn->ack_timer = NULL; }
|
||||
@@ -349,8 +360,8 @@ static void router_close_and_notify(struct ETCP_ROUTER_CONN* rconn) {
|
||||
}
|
||||
if (inst->router_conns)
|
||||
queue_remove_data(inst->router_conns, &rconn->ll);
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCPROUTE, "router_conn: closed remote=%016llx svc_id=%u",
|
||||
(unsigned long long)rconn->remote_node_id, rconn->svc_id);
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCPROUTE, "router_conn: closed group=%016llx remote=%016llx svc_id=%u",
|
||||
(unsigned long long)rconn->group_id, (unsigned long long)rconn->remote_node_id, rconn->svc_id);
|
||||
uasync_call_soon(inst->ua, rconn, router_close_finalize);
|
||||
}
|
||||
|
||||
@@ -389,7 +400,7 @@ static void router_no_route_retry_cb(void* arg) {
|
||||
struct ETCP_ROUTER_CONN* rconn = (struct ETCP_ROUTER_CONN*)arg;
|
||||
if (rconn->closed) return;
|
||||
rconn->no_route_timer = NULL;
|
||||
struct ETCP_CONN* c = topo_group_find_conn_for_node(topo_groups_get_default(rconn->inst->topo_groups), rconn->remote_node_id);;
|
||||
struct ETCP_CONN* c = topo_group_find_conn_for_node(topo_groups_find(rconn->inst->topo_groups, rconn->group_id), rconn->remote_node_id);;
|
||||
if (c) {
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCPROUTE, "router: route appeared for %016llx svc_id=%u, draining send_q=%d",
|
||||
(unsigned long long)rconn->remote_node_id, rconn->svc_id, queue_entry_count(rconn->send_q));
|
||||
@@ -412,6 +423,7 @@ static void router_retransmit_one(struct ETCP_ROUTER_CONN* rconn, struct ROUTER_
|
||||
if (!dgram) { rconn->c_pkts_send_err++; return; }
|
||||
struct SVC_ROUTE_HDR* hdr = (struct SVC_ROUTE_HDR*)dgram;
|
||||
hdr->cmd = ETCP_RT_ID_SVC_ROUTE;
|
||||
hdr->group_id = rconn->group_id;
|
||||
hdr->dst_node_id = rconn->remote_node_id;
|
||||
hdr->src_node_id = inst->node_id;
|
||||
hdr->seq = inf->seq;
|
||||
@@ -420,7 +432,7 @@ static void router_retransmit_one(struct ETCP_ROUTER_CONN* rconn, struct ROUTER_
|
||||
hdr->timestamp = get_current_timestamp();
|
||||
|
||||
struct ETCP_CONN* conn = NULL;
|
||||
struct TOPO_GROUP* group = topo_groups_get_default(inst->topo_groups);
|
||||
struct TOPO_GROUP* group = topo_groups_find(inst->topo_groups, rconn->group_id);
|
||||
if (group) {
|
||||
struct TOPO_NODEQ* nq = topo_node_find_by_id(group, rconn->remote_node_id);
|
||||
if (nq && nq->conn_mgr_type == CONN_TYPE_INDIRECT && nq->conn_mgr_intermediariy_count > 0) {
|
||||
@@ -618,7 +630,7 @@ static void router_ack_do_send(struct ETCP_ROUTER_CONN* rconn) {
|
||||
|
||||
static void router_send_ack(struct ETCP_ROUTER_CONN* rconn) {
|
||||
struct UTUN_INSTANCE* inst = rconn->inst;
|
||||
struct ETCP_CONN* conn = topo_group_find_conn_for_node(topo_groups_get_default(inst->topo_groups), rconn->remote_node_id);
|
||||
struct ETCP_CONN* conn = topo_group_find_conn_for_node(topo_groups_find(inst->topo_groups, rconn->group_id), rconn->remote_node_id);
|
||||
if (!conn) {
|
||||
DEBUG_WARN(DEBUG_CATEGORY_ETCPROUTE, "router_ack: no route to %016llx svc_id=%u",
|
||||
(unsigned long long)rconn->remote_node_id, rconn->svc_id);
|
||||
@@ -628,6 +640,7 @@ static void router_send_ack(struct ETCP_ROUTER_CONN* rconn) {
|
||||
struct SVC_ROUTE_HDR* hdr = (struct SVC_ROUTE_HDR*)u_malloc(SVC_ROUTE_HDR_SIZE);
|
||||
if (!hdr) { DEBUG_ERROR(DEBUG_CATEGORY_ETCPROUTE, "router_ack: u_malloc failed"); return; }
|
||||
hdr->cmd = ETCP_RT_ID_SVC_ROUTE;
|
||||
hdr->group_id = rconn->group_id;
|
||||
hdr->dst_node_id = rconn->remote_node_id;
|
||||
hdr->src_node_id = inst->node_id;
|
||||
hdr->seq = rconn->rx_seq;
|
||||
@@ -768,7 +781,7 @@ static void router_incoming_q_cb(struct ll_queue* q, void* arg) {
|
||||
|
||||
queue_data_put_with_index(rconn->recv_q, rq);
|
||||
|
||||
struct ETCP_CONN* conn = topo_group_find_conn_for_node(topo_groups_get_default(rconn->inst->topo_groups), rconn->remote_node_id);;
|
||||
struct ETCP_CONN* conn = topo_group_find_conn_for_node(topo_groups_find(rconn->inst->topo_groups, rconn->group_id), rconn->remote_node_id);;
|
||||
if (seq == rconn->rx_seq) router_try_assembly(rconn, conn);
|
||||
router_schedule_ack(rconn);
|
||||
queue_resume_callback(q);
|
||||
@@ -788,7 +801,7 @@ static int router_verify_signature(struct UTUN_INSTANCE* inst, struct ll_entry*
|
||||
}
|
||||
size_t actual_payload = *pl_len - SC_SIGN_SIZE;
|
||||
uint8_t* sig = (uint8_t*)entry->dgram + SVC_ROUTE_HDR_SIZE + actual_payload;
|
||||
struct TOPO_GROUP* group = topo_groups_get_default(inst->topo_groups);
|
||||
struct TOPO_GROUP* group = topo_groups_find(inst->topo_groups, hdr->group_id);
|
||||
struct TOPO_NODEQ* nq = group ? topo_node_find_by_id(group, hdr->src_node_id) : NULL;
|
||||
if (!nq) {
|
||||
DEBUG_WARN(DEBUG_CATEGORY_ETCPROUTE, "router: SIGNED packet from unknown node %016llx — dropping",
|
||||
@@ -818,7 +831,7 @@ static int router_verify_signature(struct UTUN_INSTANCE* inst, struct ll_entry*
|
||||
if (sc_stream_sign_verify(&vfy_state, sig, SC_SIGN_SIZE) != SC_OK) {
|
||||
DEBUG_WARN(DEBUG_CATEGORY_ETCPROUTE, "router: signature verification FAILED for node %016llx svc_id=%u — dropping",
|
||||
(unsigned long long)hdr->src_node_id, hdr->svc_id);
|
||||
struct ETCP_ROUTER_CONN* sr = router_conn_find(inst, hdr->src_node_id, hdr->svc_id);
|
||||
struct ETCP_ROUTER_CONN* sr = router_conn_find(inst, hdr->group_id, hdr->src_node_id, hdr->svc_id);
|
||||
if (sr) sr->c_sign_fail++;
|
||||
free_entry(entry); return -1;
|
||||
}
|
||||
@@ -875,13 +888,13 @@ static void router_handle_ack(struct UTUN_INSTANCE* inst, struct ETCP_ROUTER_CON
|
||||
|
||||
static void router_forward_transit(struct UTUN_INSTANCE* inst, struct ll_entry* entry,
|
||||
struct SVC_ROUTE_HDR* hdr) {
|
||||
struct ETCP_CONN* next = topo_group_find_conn_for_node(topo_groups_get_default(inst->topo_groups), hdr->dst_node_id);
|
||||
struct ETCP_CONN* next = topo_group_find_conn_for_node(topo_groups_find(inst->topo_groups, hdr->group_id), hdr->dst_node_id);
|
||||
if (!next) {
|
||||
DEBUG_WARN(DEBUG_CATEGORY_ETCPROUTE, "etcp_router: no route to %016llx svc_id=%u, dropping",
|
||||
(unsigned long long)hdr->dst_node_id, hdr->svc_id);
|
||||
free_entry(entry); return;
|
||||
}
|
||||
struct TRANSIT_QUEUE* tq = transit_queue_find(next, hdr->src_node_id, hdr->dst_node_id);
|
||||
struct TRANSIT_QUEUE* tq = transit_queue_find(next, hdr->group_id, hdr->src_node_id, hdr->dst_node_id);
|
||||
|
||||
if (!tq && next->send_input_q && queue_entry_count(next->send_input_q) == 0) {
|
||||
DEBUG_TRACE(DEBUG_CATEGORY_ETCPROUTE, "etcp_router: forwarding svc_id=%u → %016llx via %s",
|
||||
@@ -892,7 +905,7 @@ static void router_forward_transit(struct UTUN_INSTANCE* inst, struct ll_entry*
|
||||
}
|
||||
|
||||
if (!tq) {
|
||||
tq = transit_queue_get_or_create(next, hdr->src_node_id, hdr->dst_node_id);
|
||||
tq = transit_queue_get_or_create(next, hdr->group_id, hdr->src_node_id, hdr->dst_node_id);
|
||||
if (!tq) { free_entry(entry); return; }
|
||||
}
|
||||
int was_empty = (queue_entry_count(tq->q) == 0);
|
||||
@@ -914,8 +927,8 @@ static int router_check_peer_restart(struct UTUN_INSTANCE* inst, struct ETCP_ROU
|
||||
"router: peer restart sess_id=%u→%u svc_id=%u from %016llx",
|
||||
rconn->peer_sess_id, incoming_sess, hdr->svc_id,
|
||||
(unsigned long long)hdr->src_node_id);
|
||||
etcp_router_conn_restart(inst, hdr->src_node_id, hdr->svc_id);
|
||||
*prconn = etcp_router_conn_get(inst, hdr->src_node_id, hdr->svc_id);
|
||||
etcp_router_conn_restart(inst, hdr->group_id, hdr->src_node_id, hdr->svc_id);
|
||||
*prconn = etcp_router_conn_get(inst, hdr->group_id, hdr->src_node_id, hdr->svc_id);
|
||||
if (!*prconn) return -1;
|
||||
(*prconn)->peer_sess_id = incoming_sess;
|
||||
rconn = *prconn;
|
||||
@@ -987,7 +1000,7 @@ static void etcp_router_recv_cb(struct ETCP_CONN* conn, struct ll_entry* entry)
|
||||
|
||||
if (router_verify_signature(inst, entry, hdr, &pl_len) != 0) return;
|
||||
|
||||
struct ETCP_ROUTER_CONN* rconn = router_conn_find(inst, hdr->src_node_id, hdr->svc_id);
|
||||
struct ETCP_ROUTER_CONN* rconn = router_conn_find(inst, hdr->group_id, hdr->src_node_id, hdr->svc_id);
|
||||
|
||||
if (pl_len == 0 && (hdr->flags & (ROUTER_FLAG_CLOSE | ROUTER_FLAG_RST))) {
|
||||
if (rconn) router_close_and_notify(rconn);
|
||||
@@ -996,7 +1009,7 @@ static void etcp_router_recv_cb(struct ETCP_CONN* conn, struct ll_entry* entry)
|
||||
if (pl_len == 0) { router_handle_ack(inst, rconn, hdr->seq, hdr->src_node_id, hdr->timestamp); free_entry(entry); return; }
|
||||
|
||||
if (!rconn) {
|
||||
rconn = etcp_router_conn_get(inst, hdr->src_node_id, hdr->svc_id);
|
||||
rconn = etcp_router_conn_get(inst, hdr->group_id, hdr->src_node_id, hdr->svc_id);
|
||||
if (!rconn) { free_entry(entry); return; }
|
||||
rconn->peer_sess_id = (hdr->flags >> ROUTER_SESS_ID_SHIFT) & 0x3;
|
||||
}
|
||||
@@ -1017,7 +1030,7 @@ int etcp_router_init(struct UTUN_INSTANCE* inst) {
|
||||
if (!inst) { DEBUG_ERROR(DEBUG_CATEGORY_ETCPROUTE, "NULL instance"); return -1; }
|
||||
memset(&inst->router_bindings, 0, sizeof(inst->router_bindings));
|
||||
inst->router_conns = queue_new(inst->ua,
|
||||
ROUTER_CONN_HASH_SIZE, 0, 9, "router_conns");
|
||||
ROUTER_CONN_HASH_SIZE, 0, 17, "router_conns");
|
||||
if (!inst->router_conns) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCPROUTE, "queue_new(router_conns) failed");
|
||||
return -1;
|
||||
@@ -1069,7 +1082,7 @@ int etcp_router_unbind(struct UTUN_INSTANCE* inst, uint8_t svc_id) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int etcp_route_send(struct UTUN_INSTANCE* inst, uint64_t dst_node_id, struct ll_entry* entry, int force) {
|
||||
int etcp_route_send(struct UTUN_INSTANCE* inst, uint64_t group_id, uint64_t dst_node_id, struct ll_entry* entry, int force) {
|
||||
if (!inst || !entry) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCPROUTE, "NULL inst=%p entry=%p", (void*)inst, (void*)entry);
|
||||
if (entry) { queue_dgram_free(entry); queue_entry_free(entry); } return -1;
|
||||
@@ -1089,9 +1102,9 @@ int etcp_route_send(struct UTUN_INSTANCE* inst, uint64_t dst_node_id, struct ll_
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct ETCP_CONN* etcp_conn = topo_group_find_conn_for_node(topo_groups_get_default(inst->topo_groups), dst_node_id);
|
||||
struct ETCP_CONN* etcp_conn = topo_group_find_conn_for_node(topo_groups_find(inst->topo_groups, group_id), dst_node_id);
|
||||
if (!etcp_conn) {
|
||||
struct ETCP_ROUTER_CONN* rconn = etcp_router_conn_get(inst, dst_node_id, svc_id);
|
||||
struct ETCP_ROUTER_CONN* rconn = etcp_router_conn_get(inst, group_id, dst_node_id, svc_id);
|
||||
if (!rconn) { queue_dgram_free(entry); queue_entry_free(entry); return -1; }
|
||||
int eq_ret = router_enqueue_send(rconn, entry->dgram + 1, payload_len, force);
|
||||
queue_dgram_free(entry); queue_entry_free(entry);
|
||||
@@ -1105,7 +1118,7 @@ int etcp_route_send(struct UTUN_INSTANCE* inst, uint64_t dst_node_id, struct ll_
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct ETCP_ROUTER_CONN* rconn = etcp_router_conn_get(inst, dst_node_id, svc_id);
|
||||
struct ETCP_ROUTER_CONN* rconn = etcp_router_conn_get(inst, group_id, dst_node_id, svc_id);
|
||||
if (!rconn) { queue_dgram_free(entry); queue_entry_free(entry); return -1; }
|
||||
|
||||
if ((int32_t)(rconn->tx_seq - rconn->tx_acked) >= (int32_t)router_effective_max_inflight(rconn)) {
|
||||
@@ -1119,32 +1132,32 @@ int etcp_route_send(struct UTUN_INSTANCE* inst, uint64_t dst_node_id, struct ll_
|
||||
return ret;
|
||||
}
|
||||
|
||||
int etcp_router_input_q_count(struct UTUN_INSTANCE* inst, uint64_t node_id) {
|
||||
if (!inst || !topo_groups_get_default(inst->topo_groups)) return -1;
|
||||
struct ETCP_CONN* conn = topo_group_find_conn_for_node(topo_groups_get_default(inst->topo_groups), node_id);
|
||||
int etcp_router_input_q_count(struct UTUN_INSTANCE* inst, uint64_t group_id, uint64_t node_id) {
|
||||
if (!inst || !topo_groups_find(inst->topo_groups, group_id)) return -1;
|
||||
struct ETCP_CONN* conn = topo_group_find_conn_for_node(topo_groups_find(inst->topo_groups, group_id), node_id);
|
||||
if (!conn || !conn->normalizer || !conn->normalizer->input) return -1;
|
||||
return queue_entry_count(conn->normalizer->input);
|
||||
}
|
||||
|
||||
void etcp_router_waiter_register(struct UTUN_INSTANCE* inst, uint64_t peer_node_id,
|
||||
void etcp_router_waiter_register(struct UTUN_INSTANCE* inst, uint64_t group_id, uint64_t peer_node_id,
|
||||
struct queue_waiter_handle* h,
|
||||
queue_threshold_callback_fn callback, void* arg) {
|
||||
if (!inst || !topo_groups_get_default(inst->topo_groups) || !h) return;
|
||||
struct ETCP_CONN* conn = topo_group_find_conn_for_node(topo_groups_get_default(inst->topo_groups), peer_node_id);
|
||||
if (!inst || !topo_groups_find(inst->topo_groups, group_id) || !h) return;
|
||||
struct ETCP_CONN* conn = topo_group_find_conn_for_node(topo_groups_find(inst->topo_groups, group_id), peer_node_id);
|
||||
if (!conn || !conn->normalizer || !conn->normalizer->input) return;
|
||||
queue_waiter_wait(conn->normalizer->input, h, callback, arg);
|
||||
}
|
||||
|
||||
void etcp_router_waiter_cancel(struct UTUN_INSTANCE* inst, uint64_t peer_node_id,
|
||||
void etcp_router_waiter_cancel(struct UTUN_INSTANCE* inst, uint64_t group_id, uint64_t peer_node_id,
|
||||
struct queue_waiter_handle* h) {
|
||||
if (!inst || !topo_groups_get_default(inst->topo_groups) || !h) return;
|
||||
struct ETCP_CONN* conn = topo_group_find_conn_for_node(topo_groups_get_default(inst->topo_groups), peer_node_id);
|
||||
if (!inst || !topo_groups_find(inst->topo_groups, group_id) || !h) return;
|
||||
struct ETCP_CONN* conn = topo_group_find_conn_for_node(topo_groups_find(inst->topo_groups, group_id), peer_node_id);
|
||||
if (!conn || !conn->normalizer || !conn->normalizer->input) return;
|
||||
queue_waiter_cancel(conn->normalizer->input, h);
|
||||
}
|
||||
|
||||
void etcp_router_conn_restart(struct UTUN_INSTANCE* inst, uint64_t remote_node_id, uint8_t svc_id) {
|
||||
struct ETCP_ROUTER_CONN* rconn = router_conn_find(inst, remote_node_id, svc_id);
|
||||
void etcp_router_conn_restart(struct UTUN_INSTANCE* inst, uint64_t group_id, uint64_t remote_node_id, uint8_t svc_id) {
|
||||
struct ETCP_ROUTER_CONN* rconn = router_conn_find(inst, group_id, remote_node_id, svc_id);
|
||||
if (!rconn) return;
|
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCPROUTE, "router_restart: svc_id=%u remote=%016llx — resetting local state",
|
||||
@@ -1241,19 +1254,19 @@ void etcp_router_conn_restart(struct UTUN_INSTANCE* inst, uint64_t remote_node_i
|
||||
rconn->closed = 0;
|
||||
}
|
||||
|
||||
void etcp_router_on_send_ready(struct UTUN_INSTANCE* inst, uint64_t node_id, uint8_t svc_id,
|
||||
void etcp_router_on_send_ready(struct UTUN_INSTANCE* inst, uint64_t group_id, uint64_t node_id, uint8_t svc_id,
|
||||
struct queue_waiter_handle* h,
|
||||
queue_threshold_callback_fn callback, void* arg) {
|
||||
if (!inst || !h) return;
|
||||
struct ETCP_ROUTER_CONN* rconn = router_conn_find(inst, node_id, svc_id);
|
||||
struct ETCP_ROUTER_CONN* rconn = router_conn_find(inst, group_id, node_id, svc_id);
|
||||
if (!rconn || !rconn->send_q) return;
|
||||
queue_waiter_wait(rconn->send_q, h, callback, arg);
|
||||
}
|
||||
|
||||
void etcp_router_cancel_send_ready(struct UTUN_INSTANCE* inst, uint64_t node_id, uint8_t svc_id,
|
||||
void etcp_router_cancel_send_ready(struct UTUN_INSTANCE* inst, uint64_t group_id, uint64_t node_id, uint8_t svc_id,
|
||||
struct queue_waiter_handle* h) {
|
||||
if (!inst || !h) return;
|
||||
struct ETCP_ROUTER_CONN* rconn = router_conn_find(inst, node_id, svc_id);
|
||||
struct ETCP_ROUTER_CONN* rconn = router_conn_find(inst, group_id, node_id, svc_id);
|
||||
if (!rconn || !rconn->send_q) return;
|
||||
queue_waiter_cancel(rconn->send_q, h);
|
||||
}
|
||||
@@ -1263,16 +1276,21 @@ void etcp_router_cancel_send_ready(struct UTUN_INSTANCE* inst, uint64_t node_id,
|
||||
// ====================================================================
|
||||
|
||||
struct ETCP_ROUTER_CONN* etcp_router_conn_get(struct UTUN_INSTANCE* inst,
|
||||
uint64_t group_id,
|
||||
uint64_t remote_node_id, uint8_t svc_id) {
|
||||
if (!inst || !inst->router_conns) return NULL;
|
||||
struct ETCP_ROUTER_CONN* rconn = router_conn_find(inst, remote_node_id, svc_id);
|
||||
struct ETCP_ROUTER_CONN* rconn = router_conn_find(inst, group_id, remote_node_id, svc_id);
|
||||
if (rconn) return rconn;
|
||||
|
||||
size_t data_size = sizeof(struct ETCP_ROUTER_CONN) - sizeof(struct ll_entry);
|
||||
rconn = (struct ETCP_ROUTER_CONN*)queue_entry_new(data_size);
|
||||
if (!rconn) { DEBUG_ERROR(DEBUG_CATEGORY_ETCPROUTE, "router_conn_get: queue_entry_new failed"); return NULL; }
|
||||
rconn->group_id = group_id;
|
||||
rconn->remote_node_id = remote_node_id;
|
||||
rconn->svc_id = svc_id;
|
||||
memcpy(rconn->ll.data, &group_id, 8);
|
||||
memcpy(rconn->ll.data + 8, &remote_node_id, 8);
|
||||
rconn->ll.data[16] = svc_id;
|
||||
rconn->last_dgram_ts = get_current_timestamp();
|
||||
rconn->rtt = 0;
|
||||
rconn->rtt_jitter = 0;
|
||||
@@ -1333,8 +1351,8 @@ struct ETCP_ROUTER_CONN* etcp_router_conn_get(struct UTUN_INSTANCE* inst,
|
||||
queue_set_threshold(rconn->incoming_q, 0, 0);
|
||||
rconn->incoming_data_ready = 0;
|
||||
queue_set_threshold(rconn->send_q, ROUTER_MAX_SEND_Q_PACKETS - 1, 0);
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCPROUTE, "router_conn: new conn remote=%016llx svc_id=%u",
|
||||
(unsigned long long)remote_node_id, svc_id);
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCPROUTE, "router_conn: new conn group=%016llx remote=%016llx svc_id=%u",
|
||||
(unsigned long long)group_id, (unsigned long long)remote_node_id, svc_id);
|
||||
queue_data_put_with_index(inst->router_conns, &rconn->ll);
|
||||
return rconn;
|
||||
}
|
||||
@@ -1408,7 +1426,7 @@ void etcp_router_pause_retrans_for_node(struct UTUN_INSTANCE* inst, uint64_t rem
|
||||
}
|
||||
}
|
||||
|
||||
void etcp_router_conn_close_all_for_node(struct UTUN_INSTANCE* inst, uint64_t remote_node_id) {
|
||||
void etcp_router_conn_close_all_for_node(struct UTUN_INSTANCE* inst, uint64_t group_id, uint64_t remote_node_id) {
|
||||
if (!inst || !inst->router_conns) return;
|
||||
// Iterate via hash chains to avoid inf-loop from put-back in FIFO iteration
|
||||
struct ll_entry* to_close[256];
|
||||
@@ -1419,7 +1437,7 @@ void etcp_router_conn_close_all_for_node(struct UTUN_INSTANCE* inst, uint64_t re
|
||||
while (entry) {
|
||||
struct ll_entry* next = entry->hash_next;
|
||||
struct ETCP_ROUTER_CONN* rconn = (struct ETCP_ROUTER_CONN*)entry;
|
||||
if (rconn->remote_node_id == remote_node_id)
|
||||
if (rconn->group_id == group_id && rconn->remote_node_id == remote_node_id)
|
||||
to_close[count++] = entry;
|
||||
entry = next;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// Упрощённый TCP поверх ETCP: восстановление порядка, дедупликация, без переповторов
|
||||
//
|
||||
// Формат SVC_ROUTE пакета:
|
||||
// [cmd:1] [dst_node_id:8] [src_node_id:8] [seq:4] [svc_id:1] [flags:1] [payload...]
|
||||
// [cmd:1] [group_id:8] [dst_node_id:8] [src_node_id:8] [seq:4] [svc_id:1] [flags:1] [timestamp:2] [payload...]
|
||||
// flags: bit7=START, bit6=RST, bit5-4=sess_id(0-3)
|
||||
// ACK-пакет: тот же заголовок, seq=rx_seq, payload_len=0
|
||||
#ifndef ETCP_ROUTER_H
|
||||
@@ -22,6 +22,7 @@ extern "C" {
|
||||
#pragma pack(push, 1)
|
||||
struct SVC_ROUTE_HDR {
|
||||
uint8_t cmd; // ETCP_ID_SVC_ROUTE (0x03) / ETCP_RT_ID_SVC_ROUTE
|
||||
uint64_t group_id; // идентификатор группы
|
||||
uint64_t dst_node_id;
|
||||
uint64_t src_node_id;
|
||||
uint32_t seq; // data: tx_seq; ACK: rx_seq (ожидаемый seq)
|
||||
@@ -30,7 +31,7 @@ struct SVC_ROUTE_HDR {
|
||||
uint16_t timestamp; // часы отправителя (0.1ms), для RTT; ACK: эхо последнего data-пакета
|
||||
};
|
||||
#pragma pack(pop)
|
||||
#define SVC_ROUTE_HDR_SIZE sizeof(struct SVC_ROUTE_HDR) // 25
|
||||
#define SVC_ROUTE_HDR_SIZE sizeof(struct SVC_ROUTE_HDR) // 33
|
||||
#define SVC_ROUTE_MAX_BINDINGS 256
|
||||
|
||||
// Биты в flags
|
||||
@@ -41,24 +42,26 @@ struct SVC_ROUTE_HDR {
|
||||
#define ROUTER_SESS_ID_SHIFT 4
|
||||
#define ROUTER_SESS_ID_MASK 0x30
|
||||
|
||||
// Очередь транзитных пакетов для пары (src_node_id, dst_node_id)
|
||||
// Очередь транзитных пакетов для пары (group_id, src_node_id, dst_node_id)
|
||||
// Создаётся когда не получается отправить пакет в ETCP напрямую,
|
||||
// удаляется когда очередь становится пустой.
|
||||
// ll.data[0..15] = src:8 + dst:8 — совпадает с полями ниже по памяти
|
||||
// ll.data[0..23] = group_id:8 + src:8 + dst:8 — совпадает с полями ниже по памяти
|
||||
struct TRANSIT_QUEUE {
|
||||
struct ll_entry ll; // data[0..15] = src_node_id:8 + dst_node_id:8 (16-byte hash index)
|
||||
uint64_t src_node_id; // = data[0..7]
|
||||
uint64_t dst_node_id; // = data[8..15]
|
||||
struct ll_entry ll; // data[0..23] = group_id:8 + src_node_id:8 + dst_node_id:8 (24-byte hash index)
|
||||
uint64_t group_id; // = data[0..7]
|
||||
uint64_t src_node_id; // = data[8..15]
|
||||
uint64_t dst_node_id; // = data[16..23]
|
||||
struct ll_queue* q; // FIFO транзитных пакетов (ll_entry с dgram), начиная с data[16]
|
||||
struct queue_waiter_handle waiter; // backpressure waiter на bp-очередь (normalizer->input или tx_queue)
|
||||
struct ETCP_CONN* conn; // next_hop (для etcp_send в drain_cb)
|
||||
};
|
||||
|
||||
// Состояние одного логического подключения (remote_node_id + svc_id)
|
||||
// Состояние одного логического подключения (group_id + remote_node_id + svc_id)
|
||||
struct ETCP_ROUTER_CONN {
|
||||
struct ll_entry ll; // data[0..7]=remote_node_id, data[8]=svc_id — хеш-индекс
|
||||
uint64_t remote_node_id; // = data[0..7]
|
||||
uint8_t svc_id; // = data[8]
|
||||
struct ll_entry ll; // data[0..15]=group_id:8+remote_node_id:8, data[16]=svc_id — хеш-индекс
|
||||
uint64_t group_id; // = data[0..7]
|
||||
uint64_t remote_node_id; // = data[8..15]
|
||||
uint8_t svc_id; // = data[16]
|
||||
uint16_t last_dgram_ts; // последний timestamp датаграммы (rx или tx, timebase 0.1ms)
|
||||
uint16_t rtt; // измеренный RTT (0.1ms)
|
||||
int32_t rtt_jitter; // EMA jitter, fixed-point *65536 (как в ETCP)
|
||||
@@ -170,11 +173,11 @@ int etcp_router_bind(struct UTUN_INSTANCE* inst, uint8_t svc_id, etcp_recv_fn c
|
||||
int etcp_router_unbind(struct UTUN_INSTANCE* inst, uint8_t svc_id);
|
||||
|
||||
// Отправить сервисный пакет (авто-conn, seq, inflight-контроль через send_q)
|
||||
int etcp_route_send(struct UTUN_INSTANCE* inst, uint64_t dst_node_id, struct ll_entry* entry, int force);
|
||||
int etcp_route_send(struct UTUN_INSTANCE* inst, uint64_t group_id, uint64_t dst_node_id, struct ll_entry* entry, int force);
|
||||
|
||||
// Найти/создать состояние seq-подключения по (remote_node_id, svc_id)
|
||||
// Найти/создать состояние seq-подключения по (group_id, remote_node_id, svc_id)
|
||||
struct ETCP_ROUTER_CONN* etcp_router_conn_get(struct UTUN_INSTANCE* inst,
|
||||
uint64_t remote_node_id, uint8_t svc_id);
|
||||
uint64_t group_id, uint64_t remote_node_id, uint8_t svc_id);
|
||||
|
||||
// Отправить данные с авто-seq и контролем inflight
|
||||
// data: payload без svc_id, flags: битовые флаги (зарезервировано)
|
||||
@@ -200,30 +203,28 @@ void etcp_router_conn_close_async(struct ETCP_ROUTER_CONN* rconn,
|
||||
// Используется при etcp_conn_reinit чтобы избежать гонки с очисткой ETCP-очередей
|
||||
void etcp_router_pause_retrans_for_node(struct UTUN_INSTANCE* inst, uint64_t remote_node_id);
|
||||
|
||||
// Закрыть все router_conn для указанного remote_node_id (peer умер)
|
||||
void etcp_router_conn_close_all_for_node(struct UTUN_INSTANCE* inst, uint64_t remote_node_id);
|
||||
// Закрыть все router_conn для указанного (group_id, remote_node_id) (peer умер)
|
||||
void etcp_router_conn_close_all_for_node(struct UTUN_INSTANCE* inst, uint64_t group_id, uint64_t remote_node_id);
|
||||
|
||||
// Возвращает количество пакетов в очереди normalizer->input для узла node_id
|
||||
int etcp_router_input_q_count(struct UTUN_INSTANCE* inst, uint64_t node_id);
|
||||
// Возвращает количество пакетов в очереди normalizer->input для узла node_id в группе group_id
|
||||
int etcp_router_input_q_count(struct UTUN_INSTANCE* inst, uint64_t group_id, uint64_t node_id);
|
||||
|
||||
// Backpressure: зарегистрировать/отменить waiter на normalizer->input очереди
|
||||
void etcp_router_waiter_register(struct UTUN_INSTANCE* inst, uint64_t peer_node_id,
|
||||
void etcp_router_waiter_register(struct UTUN_INSTANCE* inst, uint64_t group_id, uint64_t peer_node_id,
|
||||
struct queue_waiter_handle* h,
|
||||
queue_threshold_callback_fn callback, void* arg);
|
||||
void etcp_router_waiter_cancel(struct UTUN_INSTANCE* inst, uint64_t peer_node_id,
|
||||
void etcp_router_waiter_cancel(struct UTUN_INSTANCE* inst, uint64_t group_id, uint64_t peer_node_id,
|
||||
struct queue_waiter_handle* h);
|
||||
|
||||
// Backpressure: зарегистрировать/отменить waiter на send_q очереди
|
||||
// h — handle из структуры сервиса, callback вызывается когда send_q.count <= threshold (128)
|
||||
void etcp_router_on_send_ready(struct UTUN_INSTANCE* inst, uint64_t node_id, uint8_t svc_id,
|
||||
void etcp_router_on_send_ready(struct UTUN_INSTANCE* inst, uint64_t group_id, uint64_t node_id, uint8_t svc_id,
|
||||
struct queue_waiter_handle* h,
|
||||
queue_threshold_callback_fn callback, void* arg);
|
||||
void etcp_router_cancel_send_ready(struct UTUN_INSTANCE* inst, uint64_t node_id, uint8_t svc_id,
|
||||
void etcp_router_cancel_send_ready(struct UTUN_INSTANCE* inst, uint64_t group_id, uint64_t node_id, uint8_t svc_id,
|
||||
struct queue_waiter_handle* h);
|
||||
|
||||
// Сбросить состояние роутера для конкретного peer+svc (перезапуск удалённой стороны).
|
||||
// Очищает send_q/recv_q, уведомляет сервис через cb(NULL, entry) с remote_node_id.
|
||||
void etcp_router_conn_restart(struct UTUN_INSTANCE* inst, uint64_t remote_node_id, uint8_t svc_id);
|
||||
// Сбросить состояние роутера для конкретного peer+svc в группе (перезапуск удалённой стороны).
|
||||
void etcp_router_conn_restart(struct UTUN_INSTANCE* inst, uint64_t group_id, uint64_t remote_node_id, uint8_t svc_id);
|
||||
|
||||
// Удалить все транзитные очереди соединения (вызывается при закрытии ETCP_CONN)
|
||||
void etcp_router_transit_queues_destroy(struct ETCP_CONN* conn);
|
||||
|
||||
@@ -144,7 +144,7 @@ void route_pkt(struct UTUN_INSTANCE* instance, struct ll_entry* entry, uint64_t
|
||||
} else {
|
||||
DEBUG_TRACE(DEBUG_CATEGORY_ROUTING, "route_pkt: sending %zu bytes to node %016llx dst=%s",
|
||||
ip_len, (unsigned long long)nq->node->node_id, ip_to_str(&addr, AF_INET).str);
|
||||
int send_err = etcp_route_send(instance, nq->node->node_id, entry, 1);
|
||||
int send_err = etcp_route_send(instance, nq->node->group_id, nq->node->node_id, entry, 1);
|
||||
if (send_err != 0) {
|
||||
DEBUG_WARN(DEBUG_CATEGORY_ROUTING, "route_pkt: etcp_route_send failed: dst=%s err=%d",
|
||||
ip_to_str(&addr, AF_INET).str, send_err);
|
||||
|
||||
@@ -38,6 +38,7 @@ static void topo_group_send_table_request(struct TOPO_GROUP* group, struct ETCP_
|
||||
if (!req) return;
|
||||
req->cmd = ETCP_ID_TOPO_ENTRY;
|
||||
req->subcmd = TOPO_SUBCMD_REQUEST_TABLE;
|
||||
req->group_id = group->group_id;
|
||||
struct ll_entry* e = queue_entry_new(0);
|
||||
if (!e) { u_free(req); return; }
|
||||
e->dgram = (uint8_t*)req; e->len = sizeof(struct TOPOMSG_TABLE_REQ);
|
||||
@@ -50,6 +51,7 @@ static void topo_group_send_table_complete(struct TOPO_GROUP* group, struct ETCP
|
||||
if (!req) return;
|
||||
req->cmd = ETCP_ID_TOPO_ENTRY;
|
||||
req->subcmd = TOPO_SUBCMD_TABLE_COMPLETE;
|
||||
req->group_id = group->group_id;
|
||||
struct ll_entry* e = queue_entry_new(0);
|
||||
if (!e) { u_free(req); return; }
|
||||
e->dgram = (uint8_t*)req; e->len = sizeof(struct TOPOMSG_TABLE_REQ);
|
||||
@@ -123,7 +125,7 @@ static void topo_group_broadcast_withdraw(struct TOPO_GROUP* group, uint64_t nod
|
||||
struct TOPOMSG_WITHDRAW_PKT* pkt = u_calloc(1, sizeof(struct TOPOMSG_WITHDRAW_PKT));
|
||||
if (!pkt) return;
|
||||
pkt->cmd = ETCP_ID_TOPO_ENTRY; pkt->subcmd = TOPO_SUBCMD_WITHDRAW;
|
||||
pkt->node_id = node_id; pkt->wd_source = wd_source;
|
||||
pkt->group_id = group->group_id; pkt->node_id = node_id; pkt->wd_source = wd_source;
|
||||
struct ll_entry* e = group->senders_list ? group->senders_list->head : NULL;
|
||||
while (e) {
|
||||
struct TOPO_GROUP_CONN_ITEM* item = (struct TOPO_GROUP_CONN_ITEM*)e->data;
|
||||
@@ -145,12 +147,29 @@ static void topo_group_receive_cbk(struct ETCP_CONN* from_conn, struct ll_entry*
|
||||
if (!from_conn || !entry || entry->len < 2) { if (entry) { queue_dgram_free(entry); queue_entry_free(entry); } return; }
|
||||
struct UTUN_INSTANCE* instance = from_conn->instance;
|
||||
if (!instance || !instance->topo_groups) { DEBUG_ERROR(DEBUG_CATEGORY_BGP, "invalid instance/topo_groups"); queue_dgram_free(entry); queue_entry_free(entry); return; }
|
||||
struct TOPO_GROUP* group = topo_groups_get_default(instance->topo_groups);
|
||||
if (!group) { DEBUG_ERROR(DEBUG_CATEGORY_BGP, "no default group"); queue_dgram_free(entry); queue_entry_free(entry); return; }
|
||||
uint8_t* data = entry->dgram; uint8_t cmd = data[0]; uint8_t subcmd = data[1];
|
||||
if (cmd != ETCP_ID_TOPO_ENTRY) { queue_dgram_free(entry); queue_entry_free(entry); return; }
|
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_BGP, "BGP recv %s from %s len=%zu", group_subcmd_name(subcmd), from_conn->log_name, entry->len);
|
||||
uint64_t pkt_group_id = 0;
|
||||
if (subcmd == TOPO_SUBCMD_NODEINFO && entry->len >= 3) {
|
||||
pkt_group_id = ((struct TOPOMSG_NODEINFO_PKT*)data)->node.group_id;
|
||||
} else if (subcmd == TOPO_SUBCMD_WITHDRAW && entry->len >= sizeof(struct TOPOMSG_WITHDRAW_PKT)) {
|
||||
pkt_group_id = ((struct TOPOMSG_WITHDRAW_PKT*)data)->group_id;
|
||||
} else if (subcmd == TOPO_SUBCMD_REQUEST_TABLE && entry->len >= sizeof(struct TOPOMSG_TABLE_REQ)) {
|
||||
pkt_group_id = ((struct TOPOMSG_TABLE_REQ*)data)->group_id;
|
||||
} else if (subcmd == TOPO_SUBCMD_TABLE_COMPLETE && entry->len >= sizeof(struct TOPOMSG_TABLE_REQ)) {
|
||||
pkt_group_id = ((struct TOPOMSG_TABLE_REQ*)data)->group_id;
|
||||
} else if (subcmd == TOPO_SUBCMD_ERR_GROUP_MISMATCH && entry->len >= sizeof(struct TOPOMSG_ERR_GROUP_MISMATCH)) {
|
||||
pkt_group_id = ((struct TOPOMSG_ERR_GROUP_MISMATCH*)data)->group_id;
|
||||
}
|
||||
|
||||
struct TOPO_GROUP* group = topo_groups_find(instance->topo_groups, pkt_group_id);
|
||||
if (!group) {
|
||||
DEBUG_WARN(DEBUG_CATEGORY_BGP, "BGP recv %s from %s: group %016llx not found, dropping", group_subcmd_name(subcmd), from_conn->log_name, (unsigned long long)pkt_group_id);
|
||||
queue_dgram_free(entry); queue_entry_free(entry); return;
|
||||
}
|
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_BGP, "BGP recv %s from %s len=%zu group=%016llx", group_subcmd_name(subcmd), from_conn->log_name, entry->len, (unsigned long long)pkt_group_id);
|
||||
|
||||
if (subcmd == TOPO_SUBCMD_NODEINFO) { nodeinfo_dump_log(data, entry->len); topo_group_process_nodeinfo(group, from_conn, data, entry->len); }
|
||||
else if (subcmd == TOPO_SUBCMD_WITHDRAW) topo_group_process_withdraw(group, from_conn, data, entry->len);
|
||||
@@ -159,8 +178,8 @@ static void topo_group_receive_cbk(struct ETCP_CONN* from_conn, struct ll_entry*
|
||||
else if (subcmd == TOPO_SUBCMD_ERR_GROUP_MISMATCH) {
|
||||
if (entry->len >= sizeof(struct TOPOMSG_ERR_GROUP_MISMATCH)) {
|
||||
struct TOPOMSG_ERR_GROUP_MISMATCH* err = (struct TOPOMSG_ERR_GROUP_MISMATCH*)data;
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_BGP, "Group type mismatch from %s: expected_type=%u received_flags=0x%02x",
|
||||
from_conn->log_name, err->expected_type, err->received_flags);
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_BGP, "Group mismatch from %s: expected=%u received=0x%02x group=%016llx",
|
||||
from_conn->log_name, err->expected_type, err->received_flags, (unsigned long long)err->group_id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,25 +191,13 @@ static void topo_group_receive_cbk(struct ETCP_CONN* from_conn, struct ll_entry*
|
||||
// Init / Destroy / New / Remove conn
|
||||
// ============================================================================
|
||||
|
||||
static void topo_group_on_conn_up(struct ETCP_CONN* conn, void* arg) {
|
||||
(void)arg;
|
||||
if (conn && conn->instance && conn->instance->topo_groups) {
|
||||
struct TOPO_GROUP* group = topo_groups_get_default(conn->instance->topo_groups);
|
||||
if (group) { DEBUG_INFO(DEBUG_CATEGORY_BGP, "BGP peer UP: %s peer=%016llx", conn->log_name, (unsigned long long)conn->peer_node_id); topo_group_new_conn(group, conn); }
|
||||
}
|
||||
}
|
||||
static void topo_group_on_conn_down(struct ETCP_CONN* conn, void* arg) {
|
||||
(void)arg;
|
||||
if (conn && conn->instance && conn->instance->topo_groups) {
|
||||
struct TOPO_GROUP* group = topo_groups_get_default(conn->instance->topo_groups);
|
||||
if (group) { DEBUG_INFO(DEBUG_CATEGORY_BGP, "BGP peer DOWN: %s peer=%016llx", conn->log_name, (unsigned long long)conn->peer_node_id); topo_group_remove_conn(group, conn); }
|
||||
}
|
||||
}
|
||||
static void topo_group_etcp_conn_cbk(struct ETCP_CONN* conn, void* arg) {
|
||||
(void)arg;
|
||||
if (conn && conn->instance && conn->instance->topo_groups) {
|
||||
struct TOPO_GROUP* group = topo_groups_get_default(conn->instance->topo_groups);
|
||||
if (group) { DEBUG_INFO(DEBUG_CATEGORY_BGP, "BGP set callbacks: %s", conn->log_name); etcp_conn_add_up_cbk(conn, topo_group_on_conn_up, group); etcp_conn_add_down_cbk(conn, topo_group_on_conn_down, group); }
|
||||
static void topo_group_conn_status(struct ETCP_CONN* conn, int status, void* arg) {
|
||||
struct TOPO_GROUPS* groups = (struct TOPO_GROUPS*)arg;
|
||||
if (!conn || !groups) return;
|
||||
struct ll_entry* fe = groups->group_list->head;
|
||||
while (fe) { struct TOPO_GROUP* g = (struct TOPO_GROUP*)fe; fe = fe->next;
|
||||
if (status == ETCP_CONN_STATUS_UP) topo_group_new_conn(g, conn);
|
||||
if (status == ETCP_CONN_STATUS_DOWN) topo_group_remove_conn(g, conn);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -246,6 +253,9 @@ struct TOPO_GROUPS* topo_groups_init(struct UTUN_INSTANCE* instance) {
|
||||
g->group_list = queue_new(instance->ua, 16, 0, 8, "topo_groups");
|
||||
if (!g->group_list) { u_free(g); DEBUG_ERROR(DEBUG_CATEGORY_BGP, "group_list queue creation failed"); return NULL; }
|
||||
|
||||
g->node_registry = queue_new(instance->ua, TOPO_NODE_REGISTRY_HASH_SIZE, 0, 8, "node_registry");
|
||||
if (!g->node_registry) { queue_free(g->group_list); u_free(g); DEBUG_ERROR(DEBUG_CATEGORY_BGP, "node_registry queue creation failed"); return NULL; }
|
||||
|
||||
g->v4_sock_meta_pool = memory_pool_init(sizeof(struct TOPO_SOCKMETA4), "to_v4sock");
|
||||
g->v4_addr_pool = memory_pool_init(sizeof(struct TOPO_ADDR4), "to_v4addr");
|
||||
g->v6_sock_meta_pool = memory_pool_init(sizeof(struct TOPO_SOCKMETA6), "to_v6sock");
|
||||
@@ -281,14 +291,14 @@ struct TOPO_GROUPS* topo_groups_init(struct UTUN_INSTANCE* instance) {
|
||||
memory_pool_destroy(g->v4_sock_meta_pool); memory_pool_destroy(g->v4_addr_pool);
|
||||
memory_pool_destroy(g->v6_sock_meta_pool); memory_pool_destroy(g->v6_addr_pool);
|
||||
memory_pool_destroy(g->v4_subnet_pool); memory_pool_destroy(g->v6_subnet_pool);
|
||||
queue_free(g->group_list); u_free(g);
|
||||
queue_free(g->node_registry); queue_free(g->group_list); u_free(g);
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_BGP, "failed to create default group");
|
||||
return NULL;
|
||||
}
|
||||
queue_data_put_with_index(g->group_list, &default_group->ll);
|
||||
|
||||
etcp_bind(instance, ETCP_ID_TOPO_ENTRY, topo_group_receive_cbk);
|
||||
etcp_add_new_conn_cbk(instance, topo_group_etcp_conn_cbk, NULL);
|
||||
etcp_add_conn_status_cbk(instance, topo_group_conn_status, g);
|
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_BGP, "TOPO groups initialized (with default group)");
|
||||
return g;
|
||||
@@ -300,6 +310,7 @@ void topo_groups_destroy(struct UTUN_INSTANCE* instance) {
|
||||
DEBUG_INFO(DEBUG_CATEGORY_BGP, "node_id=%016llx", (unsigned long long)instance->node_id);
|
||||
|
||||
etcp_unbind(instance, ETCP_ID_TOPO_ENTRY);
|
||||
etcp_remove_conn_status_cbk(instance, topo_group_conn_status, instance->topo_groups);
|
||||
route_connectivity_cancel_all(instance);
|
||||
|
||||
struct TOPO_GROUPS* g = instance->topo_groups;
|
||||
@@ -307,6 +318,16 @@ void topo_groups_destroy(struct UTUN_INSTANCE* instance) {
|
||||
while ((ge = queue_data_get(g->group_list)) != NULL) { struct TOPO_GROUP* grp = (struct TOPO_GROUP*)ge; topo_group_destroy(grp); queue_entry_free(ge); }
|
||||
queue_free(g->group_list);
|
||||
|
||||
if (g->node_registry) {
|
||||
struct ll_entry* re;
|
||||
while ((re = queue_data_get(g->node_registry)) != NULL) {
|
||||
struct TOPO_NODE_REG_ENTRY* r = (struct TOPO_NODE_REG_ENTRY*)re;
|
||||
topo_node_unref(r->node);
|
||||
queue_entry_free(re);
|
||||
}
|
||||
queue_free(g->node_registry);
|
||||
}
|
||||
|
||||
memory_pool_destroy(g->v4_sock_meta_pool);
|
||||
memory_pool_destroy(g->v4_addr_pool);
|
||||
memory_pool_destroy(g->v6_sock_meta_pool);
|
||||
@@ -336,6 +357,20 @@ struct TOPO_GROUP* topo_groups_create_group(struct TOPO_GROUPS* g, uint64_t grou
|
||||
if (!group) return NULL;
|
||||
if (channel_id) { strncpy(group->channel_id, channel_id, sizeof(group->channel_id) - 1); group->channel_id[sizeof(group->channel_id) - 1] = '\0'; }
|
||||
queue_data_put_with_index(g->group_list, &group->ll);
|
||||
|
||||
struct ll_queue* connections = g->instance->connections;
|
||||
if (connections) {
|
||||
for (uint32_t slot = 0; slot < connections->hash_size; slot++) {
|
||||
struct ll_entry* entry = connections->hash_table[slot];
|
||||
while (entry) {
|
||||
struct conn_queue_entry* cqe = (struct conn_queue_entry*)entry;
|
||||
if (cqe->conn && cqe->peer_node_id != 0)
|
||||
topo_group_new_conn(group, cqe->conn);
|
||||
entry = entry->hash_next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_BGP, "Group created: group_id=%016llx type=%u ch_id=%s", (unsigned long long)group_id, group_type, group->channel_id);
|
||||
return group;
|
||||
}
|
||||
@@ -390,7 +425,7 @@ void topo_group_remove_conn(struct TOPO_GROUP* group, struct ETCP_CONN* conn) {
|
||||
struct ll_entry* entry = node_entry;
|
||||
if (entry) { queue_remove_data(group->nodes, entry); queue_entry_free(entry); }
|
||||
nodes_removed++;
|
||||
etcp_router_conn_close_all_for_node(group->instance, key);
|
||||
etcp_router_conn_close_all_for_node(group->instance, group->group_id, key);
|
||||
uint64_t wd_src = (key == conn->peer_node_id) ? conn->instance->node_id : conn->peer_node_id;
|
||||
topo_group_broadcast_withdraw(group, key, wd_src, NULL);
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_BGP, "Removed node %016llx after link down", (unsigned long long)key);
|
||||
@@ -468,6 +503,7 @@ static void topo_group_send_err_group_mismatch(struct TOPO_GROUP* group, struct
|
||||
struct TOPOMSG_ERR_GROUP_MISMATCH* pkt = u_calloc(1, sizeof(struct TOPOMSG_ERR_GROUP_MISMATCH));
|
||||
if (!pkt) return;
|
||||
pkt->cmd = ETCP_ID_TOPO_ENTRY; pkt->subcmd = TOPO_SUBCMD_ERR_GROUP_MISMATCH;
|
||||
pkt->group_id = group->group_id;
|
||||
pkt->expected_type = expected_type; pkt->received_flags = received_flags;
|
||||
struct ll_entry* e = queue_entry_new(0);
|
||||
if (!e) { u_free(pkt); return; }
|
||||
@@ -481,6 +517,12 @@ int topo_group_process_nodeinfo(struct TOPO_GROUP* group, struct ETCP_CONN* from
|
||||
struct TOPOMSG_NODEINFO_PKT* pkt = (struct TOPOMSG_NODEINFO_PKT*)data;
|
||||
struct TOPOMSG_NODE* ni = &pkt->node;
|
||||
|
||||
if (ni->group_id != group->group_id) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_BGP, "NODEINFO group_id mismatch from %s: expected=%016llx got=%016llx",
|
||||
from->log_name, (unsigned long long)group->group_id, (unsigned long long)ni->group_id);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Проверка соответствия типа группы
|
||||
uint8_t expected_flags = (group->group_type == TOPO_GROUP_TYPE_CHAT) ? 0 : TOPO_FLAG_SEND_SUBNETS;
|
||||
if ((ni->flags & TOPO_FLAG_SEND_SUBNETS) != expected_flags) {
|
||||
@@ -538,8 +580,8 @@ int topo_group_process_nodeinfo(struct TOPO_GROUP* group, struct ETCP_CONN* from
|
||||
}
|
||||
|
||||
if (nodeinfo1) {
|
||||
nodeinfo1->node = new_ni; topo_node_ref(new_ni);
|
||||
nodeinfo1->hash_node_id = new_ni->node_id;
|
||||
nodeinfo1->node = topo_node_registry_acquire(group->instance->topo_groups, new_ni);
|
||||
nodeinfo1->hash_node_id = nodeinfo1->node ? nodeinfo1->node->node_id : 0;
|
||||
nodeinfo1->subnets = new_subnets;
|
||||
u_free(nodeinfo1->tranzit_data); nodeinfo1->tranzit_data = new_tranzit;
|
||||
nodeinfo1->tranzit_count = new_tranzit_count;
|
||||
@@ -547,11 +589,11 @@ int topo_group_process_nodeinfo(struct TOPO_GROUP* group, struct ETCP_CONN* from
|
||||
nodeinfo1->hop_count = new_hop_count;
|
||||
} else {
|
||||
struct ll_entry* qe = queue_entry_new(sizeof(struct TOPO_NODEQ));
|
||||
if (!qe) { topo_node_unref(new_ni); u_free(new_tranzit); u_free(new_hop_list); u_free(new_subnets); return -1; }
|
||||
if (!qe) { topo_node_free_raw(group->instance->topo_groups, new_ni); u_free(new_tranzit); u_free(new_hop_list); u_free(new_subnets); return -1; }
|
||||
nodeinfo1 = (struct TOPO_NODEQ*)qe;
|
||||
memset((uint8_t*)nodeinfo1 + sizeof(struct ll_entry), 0, sizeof(*nodeinfo1) - sizeof(struct ll_entry));
|
||||
nodeinfo1->node = new_ni; topo_node_ref(new_ni);
|
||||
nodeinfo1->hash_node_id = new_ni->node_id;
|
||||
nodeinfo1->node = topo_node_registry_acquire(group->instance->topo_groups, new_ni);
|
||||
nodeinfo1->hash_node_id = nodeinfo1->node ? nodeinfo1->node->node_id : 0;
|
||||
nodeinfo1->subnets = new_subnets;
|
||||
nodeinfo1->tranzit_data = new_tranzit; nodeinfo1->tranzit_count = new_tranzit_count;
|
||||
nodeinfo1->hop_list = new_hop_list; nodeinfo1->hop_count = new_hop_count;
|
||||
|
||||
@@ -76,16 +76,18 @@ struct TOPOMSG_NODEINFO_PKT {
|
||||
struct TOPOMSG_WITHDRAW_PKT {
|
||||
uint8_t cmd;
|
||||
uint8_t subcmd;
|
||||
uint64_t group_id; // идентификатор группы
|
||||
uint64_t node_id; // удаляемый узел (который стал недоступен)
|
||||
uint64_t wd_source; // узел который инициировал withdraw (при удалении он должен быть в hoplist или = current node_id)
|
||||
} __attribute__((packed));
|
||||
|
||||
/**
|
||||
* @brief Пакет запроса полной таблицы
|
||||
* @brief Пакет запроса полной таблицы / завершения синхронизации
|
||||
*/
|
||||
struct TOPOMSG_TABLE_REQ {
|
||||
uint8_t cmd;
|
||||
uint8_t subcmd;
|
||||
uint64_t group_id; // идентификатор группы
|
||||
} __attribute__((packed));
|
||||
|
||||
/**
|
||||
@@ -94,6 +96,7 @@ struct TOPOMSG_TABLE_REQ {
|
||||
struct TOPOMSG_ERR_GROUP_MISMATCH {
|
||||
uint8_t cmd;
|
||||
uint8_t subcmd;
|
||||
uint64_t group_id; // идентификатор группы
|
||||
uint8_t expected_type;
|
||||
uint8_t received_flags;
|
||||
} __attribute__((packed));
|
||||
@@ -105,6 +108,13 @@ struct TOPO_GROUP_CONN_ITEM {
|
||||
|
||||
struct ETCP_LINK;
|
||||
|
||||
#define TOPO_NODE_REGISTRY_HASH_SIZE 256
|
||||
|
||||
struct TOPO_NODE_REG_ENTRY {
|
||||
struct ll_entry ll; // data[0..7] = node_id (hash key)
|
||||
struct TOPO_NODE* node;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Группа топологии (одна на каждый group_id: utun, чат-группа, …)
|
||||
*/
|
||||
@@ -126,6 +136,7 @@ struct TOPO_GROUP {
|
||||
struct TOPO_GROUPS {
|
||||
struct UTUN_INSTANCE* instance;
|
||||
struct ll_queue* group_list; // ll_queue of TOPO_GROUP entries
|
||||
struct ll_queue* node_registry; // глобальный реестр TOPO_NODE* по node_id (hash=8)
|
||||
struct memory_pool* v4_sock_meta_pool;
|
||||
struct memory_pool* v4_addr_pool;
|
||||
struct memory_pool* v6_sock_meta_pool;
|
||||
|
||||
@@ -48,6 +48,18 @@ static void free_v6_sub_list(struct memory_pool* pool, struct TOPO_SUBNET6* head
|
||||
while (head) { struct TOPO_SUBNET6* next = head->next; memory_pool_free(pool, head); head = next; }
|
||||
}
|
||||
|
||||
void topo_node_free_raw(struct TOPO_GROUPS* groups, struct TOPO_NODE* ni) {
|
||||
if (!ni) return;
|
||||
if (groups) {
|
||||
free_v4_sock_list(groups->v4_sock_meta_pool, ni->v4_sock_meta);
|
||||
free_v4_addr_list(groups->v4_addr_pool, ni->v4_addrs);
|
||||
free_v6_sock_list(groups->v6_sock_meta_pool, ni->v6_sock_meta);
|
||||
free_v6_addr_list(groups->v6_addr_pool, ni->v6_addrs);
|
||||
}
|
||||
u_free(ni->node_name);
|
||||
u_free(ni);
|
||||
}
|
||||
|
||||
void topo_node_free_lists(struct TOPO_GROUP* group, struct TOPO_NODEQ* nq) {
|
||||
if (!group || !nq) return;
|
||||
if (nq->node) {
|
||||
@@ -57,7 +69,7 @@ void topo_node_free_lists(struct TOPO_GROUP* group, struct TOPO_NODEQ* nq) {
|
||||
free_v6_addr_list(group->instance->topo_groups->v6_addr_pool, nq->node->v6_addrs);
|
||||
nq->node->v4_sock_meta = NULL; nq->node->v4_addrs = NULL;
|
||||
nq->node->v6_sock_meta = NULL; nq->node->v6_addrs = NULL;
|
||||
topo_node_unref(nq->node);
|
||||
topo_node_registry_release(group->instance->topo_groups, nq->node);
|
||||
nq->node = NULL;
|
||||
}
|
||||
if (nq->subnets) {
|
||||
@@ -70,6 +82,46 @@ void topo_node_free_lists(struct TOPO_GROUP* group, struct TOPO_NODEQ* nq) {
|
||||
u_free(nq->hop_list); nq->hop_list = NULL; nq->hop_count = 0;
|
||||
}
|
||||
|
||||
// ===== глобальный реестр TOPO_NODE =====
|
||||
|
||||
static struct TOPO_NODE* node_registry_find(struct TOPO_GROUPS* groups, uint64_t node_id) {
|
||||
if (!groups || !groups->node_registry) return NULL;
|
||||
struct ll_entry* e = queue_find_data_by_index(groups->node_registry, &node_id);
|
||||
return e ? ((struct TOPO_NODE_REG_ENTRY*)e)->node : NULL;
|
||||
}
|
||||
|
||||
struct TOPO_NODE* topo_node_registry_acquire(struct TOPO_GROUPS* groups, struct TOPO_NODE* ni) {
|
||||
if (!groups || !ni) return NULL;
|
||||
struct TOPO_NODE* existing = node_registry_find(groups, ni->node_id);
|
||||
if (existing) {
|
||||
topo_node_ref(existing);
|
||||
topo_node_free_raw(groups, ni);
|
||||
return existing;
|
||||
}
|
||||
struct ll_entry* qe = queue_entry_new(sizeof(struct TOPO_NODE_REG_ENTRY));
|
||||
if (!qe) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_BGP, "registry_acquire: queue_entry_new failed for node=%016llx", (unsigned long long)ni->node_id);
|
||||
return NULL;
|
||||
}
|
||||
struct TOPO_NODE_REG_ENTRY* re = (struct TOPO_NODE_REG_ENTRY*)qe;
|
||||
re->node = ni;
|
||||
topo_node_ref(ni);
|
||||
memcpy(re->ll.data, &ni->node_id, 8);
|
||||
queue_data_put_with_index(groups->node_registry, &re->ll);
|
||||
return ni;
|
||||
}
|
||||
|
||||
void topo_node_registry_release(struct TOPO_GROUPS* groups, struct TOPO_NODE* ni) {
|
||||
if (!groups || !ni) return;
|
||||
uint64_t node_id = ni->node_id;
|
||||
uint32_t prev_ref = ni->ref_count;
|
||||
topo_node_unref(ni);
|
||||
if (prev_ref == 1) {
|
||||
struct ll_entry* e = groups->node_registry ? queue_find_data_by_index(groups->node_registry, &node_id) : NULL;
|
||||
if (e) { queue_remove_data(groups->node_registry, e); queue_entry_free(e); }
|
||||
}
|
||||
}
|
||||
|
||||
// ===== serialization =====
|
||||
|
||||
int topo_node_dyn_size(const struct TOPOMSG_NODE* msg) {
|
||||
@@ -167,7 +219,7 @@ int topo_node_deserialize(struct TOPO_GROUP* group, const uint8_t* data, size_t
|
||||
|
||||
struct TOPO_NODE* ni = u_calloc(1, sizeof(struct TOPO_NODE));
|
||||
if (!ni) { DEBUG_ERROR(DEBUG_CATEGORY_BGP, "alloc TOPO_NODE failed"); return -1; }
|
||||
ni->ref_count = 1;
|
||||
ni->ref_count = 0;
|
||||
ni->flags = msg->flags;
|
||||
ni->group_id = msg->group_id;
|
||||
ni->node_id = msg->node_id;
|
||||
@@ -186,46 +238,46 @@ int topo_node_deserialize(struct TOPO_GROUP* group, const uint8_t* data, size_t
|
||||
for (int i = 0; i < msg->local_v4_sockets; i++) {
|
||||
const struct TOPOMSG_SOCKMETA4* w = (const struct TOPOMSG_SOCKMETA4*)dp; dp += sizeof(*w);
|
||||
struct TOPO_SOCKMETA4* e = memory_pool_alloc(group->instance->topo_groups->v4_sock_meta_pool);
|
||||
if (!e) { topo_node_unref(ni); return -1; }
|
||||
if (!e) { topo_node_free_raw(group->instance->topo_groups, ni); return -1; }
|
||||
e->id = w->id; e->config_type = w->config_type; e->nat_type = w->nat_type;
|
||||
e->next = ni->v4_sock_meta; ni->v4_sock_meta = e;
|
||||
}
|
||||
for (int i = 0; i < msg->local_v4_addrs; i++) {
|
||||
const struct TOPOMSG_ADDR4* w = (const struct TOPOMSG_ADDR4*)dp; dp += sizeof(*w);
|
||||
struct TOPO_ADDR4* e = memory_pool_alloc(group->instance->topo_groups->v4_addr_pool);
|
||||
if (!e) { topo_node_unref(ni); return -1; }
|
||||
if (!e) { topo_node_free_raw(group->instance->topo_groups, ni); return -1; }
|
||||
memcpy(e->addr, w->addr, 4); e->port = w->port; e->type = w->type; e->socket_id = w->socket_id; e->protocol = w->protocol;
|
||||
e->next = ni->v4_addrs; ni->v4_addrs = e;
|
||||
}
|
||||
for (int i = 0; i < msg->local_v6_sockets; i++) {
|
||||
const struct TOPOMSG_SOCKMETA6* w = (const struct TOPOMSG_SOCKMETA6*)dp; dp += sizeof(*w);
|
||||
struct TOPO_SOCKMETA6* e = memory_pool_alloc(group->instance->topo_groups->v6_sock_meta_pool);
|
||||
if (!e) { topo_node_unref(ni); return -1; }
|
||||
if (!e) { topo_node_free_raw(group->instance->topo_groups, ni); return -1; }
|
||||
e->id = w->id; e->config_type = w->config_type; e->nat_type = w->nat_type;
|
||||
e->next = ni->v6_sock_meta; ni->v6_sock_meta = e;
|
||||
}
|
||||
for (int i = 0; i < msg->local_v6_addrs; i++) {
|
||||
const struct TOPOMSG_ADDR6* w = (const struct TOPOMSG_ADDR6*)dp; dp += sizeof(*w);
|
||||
struct TOPO_ADDR6* e = memory_pool_alloc(group->instance->topo_groups->v6_addr_pool);
|
||||
if (!e) { topo_node_unref(ni); return -1; }
|
||||
if (!e) { topo_node_free_raw(group->instance->topo_groups, ni); return -1; }
|
||||
memcpy(e->addr, w->addr, 16); e->port = w->port; e->type = w->type; e->socket_id = w->socket_id; e->protocol = w->protocol;
|
||||
e->next = ni->v6_addrs; ni->v6_addrs = e;
|
||||
}
|
||||
|
||||
if (out_subnets && (msg->flags & TOPO_FLAG_SEND_SUBNETS) && (msg->local_v4_subnets || msg->local_v6_subnets)) {
|
||||
struct TOPO_NODESUBNETS* r = u_calloc(1, sizeof(struct TOPO_NODESUBNETS));
|
||||
if (!r) { topo_node_unref(ni); return -1; }
|
||||
if (!r) { topo_node_free_raw(group->instance->topo_groups, ni); return -1; }
|
||||
for (int i = 0; i < msg->local_v4_subnets; i++) {
|
||||
const struct TOPOMSG_SUBNET4* w = (const struct TOPOMSG_SUBNET4*)dp; dp += sizeof(*w);
|
||||
struct TOPO_SUBNET4* e = memory_pool_alloc(group->instance->topo_groups->v4_subnet_pool);
|
||||
if (!e) { u_free(r); topo_node_unref(ni); return -1; }
|
||||
if (!e) { u_free(r); topo_node_free_raw(group->instance->topo_groups, ni); return -1; }
|
||||
memcpy(e->addr, w->addr, 4); e->prefix_length = w->prefix_length;
|
||||
e->next = r->v4_subnets; r->v4_subnets = e;
|
||||
}
|
||||
for (int i = 0; i < msg->local_v6_subnets; i++) {
|
||||
const struct TOPOMSG_SUBNET6* w = (const struct TOPOMSG_SUBNET6*)dp; dp += sizeof(*w);
|
||||
struct TOPO_SUBNET6* e = memory_pool_alloc(group->instance->topo_groups->v6_subnet_pool);
|
||||
if (!e) { u_free(r); topo_node_unref(ni); return -1; }
|
||||
if (!e) { u_free(r); topo_node_free_raw(group->instance->topo_groups, ni); return -1; }
|
||||
memcpy(e->addr, w->addr, 16); e->prefix_length = w->prefix_length;
|
||||
e->next = r->v6_subnets; r->v6_subnets = e;
|
||||
}
|
||||
@@ -241,14 +293,14 @@ int topo_node_deserialize(struct TOPO_GROUP* group, const uint8_t* data, size_t
|
||||
if (out_tranzit && msg->tranzit_nodes > 0) {
|
||||
size_t tsz = msg->tranzit_nodes * sizeof(struct TOPOMSG_TRANZIT);
|
||||
*out_tranzit = u_malloc(tsz);
|
||||
if (!*out_tranzit) { topo_node_unref(ni); return -1; }
|
||||
if (!*out_tranzit) { topo_node_free_raw(group->instance->topo_groups, ni); return -1; }
|
||||
memcpy(*out_tranzit, dp, tsz); dp += tsz;
|
||||
*out_tranzit_count = msg->tranzit_nodes;
|
||||
}
|
||||
if (out_hop_list && msg->hop_count > 0) {
|
||||
size_t hsz = msg->hop_count * 8;
|
||||
*out_hop_list = u_malloc(hsz);
|
||||
if (!*out_hop_list) { topo_node_unref(ni); return -1; }
|
||||
if (!*out_hop_list) { topo_node_free_raw(group->instance->topo_groups, ni); return -1; }
|
||||
memcpy(*out_hop_list, dp, hsz); dp += hsz;
|
||||
*out_hop_count = msg->hop_count;
|
||||
}
|
||||
@@ -600,7 +652,7 @@ int topo_group_update_my_nodeinfo(struct UTUN_INSTANCE* instance, struct TOPO_GR
|
||||
|
||||
struct TOPO_NODE* ni = u_calloc(1, sizeof(struct TOPO_NODE));
|
||||
if (!ni) { u_free(lq); group->local_node = NULL; return -1; }
|
||||
ni->ref_count = 1;
|
||||
ni->ref_count = 0;
|
||||
ni->flags = (group->group_type == TOPO_GROUP_TYPE_CHAT) ? 0 : TOPO_FLAG_SEND_SUBNETS;
|
||||
ni->group_id = group->group_id;
|
||||
ni->node_id = instance->node_id;
|
||||
@@ -609,7 +661,7 @@ int topo_group_update_my_nodeinfo(struct UTUN_INSTANCE* instance, struct TOPO_GR
|
||||
memcpy(ni->ed25519_public_key, group->ed25519_public_key, SC_PUBKEY_SIZE);
|
||||
if (name_len) { ni->node_name = u_malloc(name_len + 1); if (ni->node_name) { memcpy(ni->node_name, instance->name, name_len); ni->node_name[name_len] = 0; } }
|
||||
|
||||
lq->node = ni;
|
||||
lq->node = topo_node_registry_acquire(instance->topo_groups, ni);
|
||||
lq->hash_node_id = ni->node_id;
|
||||
lq->hop_count = 0;
|
||||
lq->dirty = 1;
|
||||
|
||||
@@ -252,8 +252,20 @@ struct TOPO_NODEQ {
|
||||
|
||||
struct TOPO_NODEQ* topo_node_find_by_id(struct TOPO_GROUP* group, uint64_t node_id);
|
||||
|
||||
// Владение TOPO_NODE: создаётся с ref_count=0. Владелец вызывает topo_node_ref() чтобы завладеть.
|
||||
// При отказе от владения — topo_node_unref(). При ref_count=0 узел удаляется.
|
||||
void topo_node_ref(struct TOPO_NODE* ni);
|
||||
void topo_node_unref(struct TOPO_NODE* ni);
|
||||
|
||||
// Глобальный реестр TOPO_NODE* (по node_id). Каждый узел хранится один раз.
|
||||
// При помещении в реестр и при получении из реестра владение берётся (ref++).
|
||||
// При освобождении из реестра владение возвращается (unref, при ref=0 — удаление).
|
||||
struct TOPO_NODE* topo_node_registry_acquire(struct TOPO_GROUPS* groups, struct TOPO_NODE* ni);
|
||||
void topo_node_registry_release(struct TOPO_GROUPS* groups, struct TOPO_NODE* ni);
|
||||
|
||||
// Освободить узел без учёта ref_count (для ошибок после deserialize, пока ref=0).
|
||||
void topo_node_free_raw(struct TOPO_GROUPS* groups, struct TOPO_NODE* ni);
|
||||
|
||||
void topo_node_free_lists(struct TOPO_GROUP* group, struct TOPO_NODEQ* nq);
|
||||
int topo_node_dyn_size(const struct TOPOMSG_NODE* msg);
|
||||
|
||||
|
||||
Reference in new issue
Block a user