From b21112608eb0da6c29c8a3828c3b5a612b9394ef Mon Sep 17 00:00:00 2001 From: jinliu777 Date: Tue, 11 Nov 2025 21:47:01 +0800 Subject: [PATCH] add an API ff_regist_packet_dispatcher_context that allows to pass more context to callback --- lib/ff_api.h | 39 +++++++++++++++++++++++++++++++++++++++ lib/ff_dpdk_if.c | 32 ++++++++++++++++++++++++++++---- 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/lib/ff_api.h b/lib/ff_api.h index 135c3d122..45bc130c9 100644 --- a/lib/ff_api.h +++ b/lib/ff_api.h @@ -219,9 +219,48 @@ int ff_route_ctl(enum FF_ROUTE_CTL req, enum FF_ROUTE_FLAG flag, typedef int (*dispatch_func_t)(void *data, uint16_t *len, uint16_t queue_id, uint16_t nb_queues); +/* + * Packet dispatcher context structure. + * Contains additional context information for packet dispatching. + */ +struct ff_dispatcher_context { + struct { + uint8_t stripped; + uint16_t vlan_tci; /**< Priority (3) + CFI (1) + Identifier Code (12) */ + } vlan; +}; + +/* + * Enhanced packet dispatch callback function with context. + * Implemented by user. + * + * @param data + * The data pointer of this packet. + * @param len + * The length of this packet. + * @param queue_id + * Current queue of this packet. + * @param nb_queues + * Number of queues to be dispatched. + * @param context + * Additional context information for packet dispatching. + * + * @return 0 to (nb_queues - 1) + * The queue id that the packet will be dispatched to. + * @return FF_DISPATCH_ERROR (-1) + * Error occurs or packet is handled by user, packet will be freed. + * @return FF_DISPATCH_RESPONSE (-2) + * Packet is handled by user, packet will be responsed. + */ +typedef int (*dispatch_func_context_t)(void *data, uint16_t *len, + uint16_t queue_id, uint16_t nb_queues, struct ff_dispatcher_context context); + /* regist a packet dispath function */ void ff_regist_packet_dispatcher(dispatch_func_t func); +/* Register a packet dispatch function with context support */ +void ff_regist_packet_dispatcher_context(dispatch_func_context_t func); + /* * RAW packet send direty with DPDK by user APP. * diff --git a/lib/ff_dpdk_if.c b/lib/ff_dpdk_if.c index efc44040d..2c626ee75 100644 --- a/lib/ff_dpdk_if.c +++ b/lib/ff_dpdk_if.c @@ -125,6 +125,7 @@ static pcblddr_func_t pcblddr_fun; static struct rte_ring **dispatch_ring[RTE_MAX_ETHPORTS]; static dispatch_func_t packet_dispatcher; +static dispatch_func_context_t packet_dispatcher_with_context; static uint16_t rss_reta_size[RTE_MAX_ETHPORTS]; @@ -1614,10 +1615,27 @@ process_packets(uint16_t port_id, uint16_t queue_id, struct rte_mbuf **bufs, ff_traffic.rx_bytes += rte_pktmbuf_pkt_len(rtem); } - if (!pkts_from_ring && packet_dispatcher) { - uint64_t cur_tsc = rte_rdtsc(); - int ret = (*packet_dispatcher)(data, &len, queue_id, nb_queues); - usr_cb_tsc += rte_rdtsc() - cur_tsc; + if (!pkts_from_ring && (packet_dispatcher || packet_dispatcher_with_context)) { + uint64_t cur_tsc; + int ret; + if (packet_dispatcher) { + cur_tsc = rte_rdtsc(); + ret = (*packet_dispatcher)(data, &len, queue_id, nb_queues); + usr_cb_tsc += rte_rdtsc() - cur_tsc; + } else if (packet_dispatcher_with_context) { + struct ff_dispatcher_context ctx; + if (rtem->ol_flags & RTE_MBUF_F_RX_VLAN_STRIPPED) { + ctx.vlan.stripped = 1; + ctx.vlan.vlan_tci = rte_cpu_to_be_16(rtem->vlan_tci); + } else { + ctx.vlan.stripped = 0; + ctx.vlan.vlan_tci = 0; + } + cur_tsc = rte_rdtsc(); + ret = (*packet_dispatcher_with_context)(data, &len, queue_id, nb_queues, ctx); + usr_cb_tsc += rte_rdtsc() - cur_tsc; + } + if (ret == FF_DISPATCH_RESPONSE) { rte_pktmbuf_pkt_len(rtem) = rte_pktmbuf_data_len(rtem) = len; /* @@ -2772,6 +2790,12 @@ ff_regist_packet_dispatcher(dispatch_func_t func) packet_dispatcher = func; } +void +ff_regist_packet_dispatcher_context(dispatch_func_context_t func) +{ + packet_dispatcher_with_context = func; +} + uint64_t ff_get_tsc_ns() {