Reputation: 65
Requirement: I would like to make use of DPDK basicfwd, to send back custom reply to the incoming port.
Explanation:
DPDK ports: Port-0 and Port-1
Sample code: edited DPDK basicfwd.
Custom Function: PacketHandler()
Requirement: Sometimes I need to send custom packet(kind of RST packet) to incoming port.
example: Port-0 -> RX burst -> PacketHandler() -> normal packets fwd to Port-1, special packets send back to Port-0
Code Snippet:
while (isRun) {
num_rx = rte_eth_rx_burst(port_id, queue_id, mbufs, BURST_SIZE);
if (num_rx == 0) {
continue;
}
num_tx = 0;
for (int idx = 0; idx < num_rx; idx++) {
struct pcap_pkthdr pktHdr;
gettimeofday(&pktHdr.ts, NULL);
pktHdr.caplen = rte_pktmbuf_pkt_len(mbufs[idx]);
pktHdr.len = rte_pktmbuf_pkt_len(mbufs[idx]);
if (PacketHandler(&pktHdr, rte_pktmbuf_mtod(mbufs[idx], unsigned char *)) > 1) {
printf("Blocked Packets, do not fwd");
//TODO: send custom packet to incoming way
rte_pktmbuf_free(mbufs[idx]);
continue;
}
txbufs[num_tx++] = mbufs[idx];
}
if (num_tx > 0) {
sent = rte_eth_tx_burst(port_id ^ 1, queue_id, txbufs, num_tx);
for (int idx = sent; idx < num_tx; idx++) {
rte_pktmbuf_free(txbufs[idx]);
}
}
}
My custom packet type is u_char. How can i send my packet to incoming way?
Thanks
Upvotes: 1
Views: 770
Reputation: 4798
Option 1
: Simple way (but can affect performance)
get packets via rte_eth_rx_burst
iterate packets, to update dest_port = PacketHandler() ? mbufs[idx]->port : (mbufs[idx]->port ^ 1)
Send packet out with rte_eth_tx_burst
with dest_port
Comment out the tx_burst generic call below.
Option 2
:
Create 2 mbuf-array namely mbufs_inport, and mbufs_outport.
Iterate packets, to invoke PacketHandler(). Matching Packets goes to mbufs_inport
and non-matching goes to mbufs_outport
to invoke rte_eth_tx_burst
for mbufs_inport
and mbufs_outprt
.
Option 3
:
register rx-callback to filter packets with packethandler
.
Now you have the option to directly send back the selected back based on mbuf->portid
. While non-matching packets continue in the main loop.
If there is concern about packet order
or previous packets needs to be processed
, make use of mbuf-udata64
as a place holder for exit port. For cases not matching set is as -1
. Then in the main loop, you have convert tx_burst for n
packets to iterate for each as you have to check for udata64
value.
Upvotes: 2