[PACKET_IO]add ip hash func

This commit is contained in:
liuwentan
2022-08-11 21:25:29 +08:00
parent 0ed9a0ccca
commit 5c7c2a4052
10 changed files with 160 additions and 56 deletions

View File

@@ -319,7 +319,7 @@ int pio_marsio_device_close(struct packet_io_device *pdev)
return 0;
}
int pio_marsio_device_receive(struct packet_io_device *pdev, uint32_t rxq_id, struct stellar_packet **pkts, int nr_pkts)
int pio_marsio_device_receive(struct packet_io_device *pdev, uint16_t rxq_id, struct stellar_packet **pkts, int nr_pkts)
{
struct mr_vdev *mr_dev_handle = pdev->entity.marsio_dev_ctx->mr_dev_handle;
marsio_buff_t *rx_buff[MARSIO_BURST_PKT_MAX];
@@ -335,7 +335,7 @@ int pio_marsio_device_receive(struct packet_io_device *pdev, uint32_t rxq_id, st
return recv_res;
}
int pio_marsio_device_send(struct packet_io_device *pdev, uint32_t txq_id, struct stellar_packet **pkts, int nr_pkts)
int pio_marsio_device_send(struct packet_io_device *pdev, uint16_t txq_id, struct stellar_packet **pkts, int nr_pkts)
{
struct mr_sendpath *sendpath_handle = pdev->entity.marsio_dev_ctx->mr_sendpath_handle;
int ret = g_marsio_dll_func.marsio_send_burst(sendpath_handle, txq_id, (marsio_buff_t **)pkts, nr_pkts);
@@ -347,7 +347,7 @@ int pio_marsio_device_send(struct packet_io_device *pdev, uint32_t txq_id, struc
return ret;
}
void pio_marsio_device_pkt_free(struct packet_io_device *pdev, uint32_t qid, struct stellar_packet **pkts, int nr_pkts)
void pio_marsio_device_pkt_free(struct packet_io_device *pdev, uint16_t qid, struct stellar_packet **pkts, int nr_pkts)
{
struct mr_instance *mr_inst = pdev->ppio_inst->entity.marsio_inst_ctx->mr_inst_handle;
g_marsio_dll_func.marsio_buff_free(mr_inst, (marsio_buff_t **)pkts, nr_pkts, MARSIO_SOCKET_ID_ANY, qid);

View File

@@ -141,7 +141,7 @@ int pio_marsio_device_close(struct packet_io_device *pdev);
*
* @retval number of packets actually received
*/
int pio_marsio_device_receive(struct packet_io_device *pdev, uint32_t rxq_id, struct stellar_packet **pkts, int nr_pkts);
int pio_marsio_device_receive(struct packet_io_device *pdev, uint16_t rxq_id, struct stellar_packet **pkts, int nr_pkts);
/**
* @brief send packets by device's single tx queue which specified by txq_id
@@ -153,7 +153,7 @@ int pio_marsio_device_receive(struct packet_io_device *pdev, uint32_t rxq_id, st
*
* @retval if ret<0, means the sending fails; if ret==0 means the sending succeeds
*/
int pio_marsio_device_send(struct packet_io_device *pdev, uint32_t txq_id, struct stellar_packet **pkts, int nr_pkts);
int pio_marsio_device_send(struct packet_io_device *pdev, uint16_t txq_id, struct stellar_packet **pkts, int nr_pkts);
/**
* @brief manually free packet's memory
@@ -163,7 +163,7 @@ int pio_marsio_device_send(struct packet_io_device *pdev, uint32_t txq_id, struc
* @param pkts:
* @param nr_pkts:
*/
void pio_marsio_device_pkt_free(struct packet_io_device *pdev, uint32_t qid, struct stellar_packet **pkts, int nr_pkts);
void pio_marsio_device_pkt_free(struct packet_io_device *pdev, uint16_t qid, struct stellar_packet **pkts, int nr_pkts);
void *pio_marsio_device_buff_ctrlzone(struct stellar_packet *p);

View File

@@ -113,7 +113,7 @@ void packet_io_instance_destroy(struct packet_io_instance *pinst) {
}
struct packet_io_device *
packet_io_device_open(struct packet_io_instance *pinst, const char *dev_name, uint32_t nr_rxq, uint32_t nr_txq)
packet_io_device_open(struct packet_io_instance *pinst, const char *dev_name, uint16_t nr_rxq, uint16_t nr_txq)
{
struct packet_io_device *ppio_dev = CALLOC(struct packet_io_device, 1);
if (nullptr == ppio_dev) {
@@ -167,17 +167,17 @@ void packet_io_device_close(struct packet_io_device *pdev)
FREE(pdev);
}
int packet_io_device_rx(struct packet_io_device *pdev, uint32_t rxq_id, struct stellar_packet **pkts, int nr_pkts)
int packet_io_device_rx(struct packet_io_device *pdev, uint16_t rxq_id, struct stellar_packet **pkts, int nr_pkts)
{
return pdev->dev_ops->recv(pdev, rxq_id, pkts, nr_pkts);
}
int packet_io_device_tx(struct packet_io_device *pdev, uint32_t txq_id, struct stellar_packet **pkts, int nr_pkts)
int packet_io_device_tx(struct packet_io_device *pdev, uint16_t txq_id, struct stellar_packet **pkts, int nr_pkts)
{
return pdev->dev_ops->send(pdev, txq_id, pkts, nr_pkts);
}
void packet_io_pkts_free(struct packet_io_device *pdev, uint32_t qid, struct stellar_packet **pkts, int nr_pkts)
void packet_io_pkts_free(struct packet_io_device *pdev, uint16_t qid, struct stellar_packet **pkts, int nr_pkts)
{
return pdev->dev_ops->pkt_free(pdev, qid, pkts, nr_pkts);
}

View File

@@ -66,11 +66,11 @@ struct pio_device_operations {
int (*close)(struct packet_io_device *pdev);
int (*recv)(struct packet_io_device *pdev, uint32_t rxq_id, struct stellar_packet **pkts, int nr_pkts);
int (*recv)(struct packet_io_device *pdev, uint16_t rxq_id, struct stellar_packet **pkts, int nr_pkts);
int (*send)(struct packet_io_device *pdev, uint32_t txq_id, struct stellar_packet **pkts, int nr_pkts);
int (*send)(struct packet_io_device *pdev, uint16_t txq_id, struct stellar_packet **pkts, int nr_pkts);
void (*pkt_free)(struct packet_io_device *pdev, uint32_t qid, struct stellar_packet **pkts, int nr_pkts);
void (*pkt_free)(struct packet_io_device *pdev, uint16_t qid, struct stellar_packet **pkts, int nr_pkts);
void *(*buff_ctrlzone)(struct stellar_packet *p);
@@ -89,10 +89,10 @@ struct packet_io_device {
struct pio_device_operations *dev_ops;
/* number of receive queue */
uint32_t rxq_num;
uint16_t rxq_num;
/* number of send queue */
uint32_t txq_num;
uint16_t txq_num;
/* packet io device context */
union {
@@ -126,7 +126,7 @@ void packet_io_instance_destroy(struct packet_io_instance *pinst);
* @param nr_txq: number of send queue for the device
*/
struct packet_io_device *
packet_io_device_open(struct packet_io_instance *pinst, const char *dev_name, uint32_t nr_rxq, uint32_t nr_txq);
packet_io_device_open(struct packet_io_instance *pinst, const char *dev_name, uint16_t nr_rxq, uint16_t nr_txq);
/** close packet_io device */
void packet_io_device_close(struct packet_io_device *dev);
@@ -139,7 +139,7 @@ void packet_io_device_close(struct packet_io_device *dev);
* @param p: received packet's pointer array
* @param nr_p: number of received packets
*/
int packet_io_device_rx(struct packet_io_device *pdev, uint32_t rxq_id, struct stellar_packet **pkts, int nr_pkts);
int packet_io_device_rx(struct packet_io_device *pdev, uint16_t rxq_id, struct stellar_packet **pkts, int nr_pkts);
/**
* @brief packet_io device send function
@@ -149,12 +149,12 @@ int packet_io_device_rx(struct packet_io_device *pdev, uint32_t rxq_id, struct s
* @param p: prepare to send packet's pointer array
* @param nr_p: number of packets which prepare to send
*/
int packet_io_device_tx(struct packet_io_device *pdev, uint32_t txq_id, struct stellar_packet **pkts, int nr_pkts);
int packet_io_device_tx(struct packet_io_device *pdev, uint16_t txq_id, struct stellar_packet **pkts, int nr_pkts);
/*
* @brief packet_io free packet buff
*/
void packet_io_pkts_free(struct packet_io_device *pdev, uint32_t qid, struct stellar_packet **pkts, int nr_pkts);
void packet_io_pkts_free(struct packet_io_device *pdev, uint16_t qid, struct stellar_packet **pkts, int nr_pkts);
/**
* @brief get packet_io packet's ctrlzone

View File

@@ -9,6 +9,9 @@
*/
#include <string.h>
#include <netinet/ether.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include "utils.h"
#include "packet_io_util.h"
@@ -105,4 +108,88 @@ int strncpy_safe(char *dst, const char *src, size_t dst_size)
}
return 0;
}
static uint32_t simple_murmur_hash(const void *key, int len)
{
const uint32_t m = 0x5bd1e995;
const int r = 24;
/* Initialize the hash to a 'random' value */
uint32_t h = len;
const unsigned char *data = (const unsigned char *)key;
while (len >= 4) {
uint32_t k = *(uint32_t *)data;
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
data += 4;
len -= 4;
}
/* Handle the last few bytes of the input array */
switch (len) {
case 3:
h ^= data[2] << 16;
case 2:
h ^= data[1] << 8;
case 1:
h ^= data[0];
h *= m;
};
/* Do a few final mixes of the hash to ensure the last few
// bytes are well-incorporated. */
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h;
}
static uint64_t generic_2tuple_hash(uint8_t *src, uint8_t *dst, size_t n)
{
if (nullptr == src || nullptr == dst || n == 0) {
return 0;
}
uint64_t key1 = simple_murmur_hash(src, n);
uint64_t key2 = simple_murmur_hash(dst, n);
return (key1 | key2);
}
uint64_t pio_packet_hash(struct pio_packet *p)
{
struct ethhdr *eth_hdr = (struct ethhdr *)p->pkt_payload;
uint8_t *disp_arg1 = nullptr;
uint8_t *disp_arg2 = nullptr;
size_t disp_len = 0;
if (p->data_link != LINKTYPE_ETHERNET) {
return 0;
}
uint16_t eth_type = ntohs(eth_hdr->h_proto);
if (eth_type == ETHERNET_TYPE_IP) {
struct iphdr *ipv4_hdr = (struct iphdr *)(eth_hdr + 1);
disp_arg1 = (uint8_t *)&ipv4_hdr->saddr;
disp_arg2= (uint8_t *)&ipv4_hdr->daddr;
disp_len = sizeof(uint32_t);
} else if (eth_type == ETHERNET_TYPE_IPV6) {
struct ip6_hdr *ipv6_hdr = (struct ip6_hdr *)(eth_hdr + 1);
disp_arg1 = (uint8_t *)&ipv6_hdr->ip6_src;
disp_arg2 = (uint8_t *)&ipv6_hdr->ip6_dst;
disp_len = sizeof(struct in6_addr);
}
uint64_t hash = generic_2tuple_hash(disp_arg1, disp_arg2, disp_len);
return hash;
}

View File

@@ -19,6 +19,15 @@ extern "C"
#include <stdint.h>
#include <pthread.h>
#ifndef DLT_EN10MB
#define DLT_EN10MB 1
#endif
#define LINKTYPE_ETHERNET DLT_EN10MB
#define ETHERNET_TYPE_IP 0x0800
#define ETHERNET_TYPE_IPV6 0x86dd
#define PKT_QUEUE_MAX_NUM 256
#define CUSTOM_ZONE_LEN 64
@@ -49,6 +58,9 @@ struct pio_packet {
/* pkt payload pointer */
void *pkt_payload;
/* data link type */
uint32_t data_link;
/* reference counts */
uint32_t ref_cnt;
@@ -64,14 +76,6 @@ struct pio_packet_queue {
pthread_mutex_t mutex_q;
};
int packet_copy_data(uint8_t *ptr, const uint8_t *pkt_data, uint32_t pkt_len);
void pio_packet_enqueue(struct pio_packet_queue *, struct pio_packet *);
struct pio_packet *pio_packet_dequeue(struct pio_packet_queue *);
void release_pio_packet_queue(struct pio_packet_queue *);
/**
* @brief string copy safely,
*
@@ -79,6 +83,21 @@ void release_pio_packet_queue(struct pio_packet_queue *);
*/
int strncpy_safe(char *dst, const char *src, size_t dst_size);
int packet_copy_data(uint8_t *ptr, const uint8_t *pkt_data, uint32_t pkt_len);
/**
* @brief ip hash function for struct pio_packet, 2 tuple(sip/dip) hash
*
* @retval hash value
*/
uint64_t pio_packet_hash(struct pio_packet *p);
void pio_packet_enqueue(struct pio_packet_queue *, struct pio_packet *);
struct pio_packet *pio_packet_dequeue(struct pio_packet_queue *);
void release_pio_packet_queue(struct pio_packet_queue *);
#ifdef __cpluscplus
}
#endif

View File

@@ -21,6 +21,7 @@
#include "time_helper.h"
#include "pio_pcap_file.h"
#include "packet_io.h"
#include "packet_io_util.h"
/**
* @brief validate path is a valid plain file or directory
@@ -183,7 +184,7 @@ static int pcap_directory_file_init(struct pio_pcap_file_device_context *pfile_d
return 0;
}
static int pcap_file_shared_init(struct pio_pcap_file_device_context *pfile_dev_ctx, uint32_t nr_rxq)
static int pcap_file_shared_init(struct pio_pcap_file_device_context *pfile_dev_ctx)
{
if (nullptr == pfile_dev_ctx) {
return -1;
@@ -257,7 +258,7 @@ int pio_pcap_file_device_open(struct packet_io_device *pdev)
pdev->entity.pcap_file_dev_ctx->pio_dev = pdev;
status = pcap_file_shared_init(pdev->entity.pcap_file_dev_ctx, pdev->rxq_num);
status = pcap_file_shared_init(pdev->entity.pcap_file_dev_ctx);
if (status < 0) {
log_error(ST_ERR_PIO_PCAP_FILE_DEVICE, "pcap file shared init failed.");
return -1;
@@ -321,28 +322,26 @@ void pcap_file_pkt_callback_oneshot(char *user, struct pcap_pkthdr *pkt_hdr, u_c
return;
}
memset(p, 0, SIZE_OF_PIO_PACKET);
p->pkt_hdr = p;
p->pkt_payload = (uint8_t *)p + CUSTOM_ZONE_LEN;
p->pkt_len = pkt_hdr->caplen;
p->data_link = pfile_dev_ctx->entity.file->data_link;
if (packet_copy_data((uint8_t *)p->pkt_payload, (uint8_t *)pkt, pkt_hdr->caplen)) {
FREE(p);
return;
}
//uint32_t nr_rxq = pfile_dev_ctx->pio_dev->rxq_num;
/*
hash to specific queue id and enqueue
hash_id = decode_packet(p) % nr_rxq;
packet_enqueue(&pfile_dev_ctx->pkt_queues[hash_id], p);
*/
int rxq_id = 0;
/* nr_rxq <= PKT_QUEUE_MAX_NUM */
uint16_t nr_rxq = pfile_dev_ctx->pio_dev->rxq_num;
uint16_t rxq_id = pio_packet_hash(p) % nr_rxq;
/* hash to specific queue id and enqueue */
pthread_mutex_lock(&pfile_dev_ctx->pkt_queues[rxq_id].mutex_q);
pio_packet_enqueue(&pfile_dev_ctx->pkt_queues[rxq_id], p);
pthread_mutex_unlock(&pfile_dev_ctx->pkt_queues[rxq_id].mutex_q);
}
static int pcap_file_dispatch(struct pio_pcap_file_device_context *pfile_dev_ctx, uint32_t rxq_id,
static int pcap_file_dispatch(struct pio_pcap_file_device_context *pfile_dev_ctx, uint16_t rxq_id,
struct stellar_packet **pkts, int nr_pkts)
{
if (pfile_dev_ctx->entity.file->first_pkt_hdr != nullptr) {
@@ -518,7 +517,7 @@ static int pcap_directory_collect_pending_files(struct pio_pcap_file_device_cont
return 0;
}
static int pcap_directory_dispatch(struct pio_pcap_file_device_context *pfile_dev_ctx, uint32_t rxq_id,
static int pcap_directory_dispatch(struct pio_pcap_file_device_context *pfile_dev_ctx, uint16_t rxq_id,
struct stellar_packet **pkts, int nr_pkts)
{
int res = -1;
@@ -603,7 +602,7 @@ static int pcap_directory_dispatch(struct pio_pcap_file_device_context *pfile_de
return res;
}
int pio_pcap_file_device_receive(struct packet_io_device *pdev, uint32_t rxq_id, struct stellar_packet **pkts, int nr_pkts)
int pio_pcap_file_device_receive(struct packet_io_device *pdev, uint16_t rxq_id, struct stellar_packet **pkts, int nr_pkts)
{
struct pio_pcap_file_device_context *pfile_dev_ctx = pdev->entity.pcap_file_dev_ctx;
if (nullptr == pfile_dev_ctx) {
@@ -623,7 +622,7 @@ int pio_pcap_file_device_receive(struct packet_io_device *pdev, uint32_t rxq_id,
return res;
}
void pio_pcap_file_device_pkt_free(__unused struct packet_io_device *pdev, __unused uint32_t qid, struct stellar_packet **pkts, int nr_pkts)
void pio_pcap_file_device_pkt_free(__unused struct packet_io_device *pdev, __unused uint16_t qid, struct stellar_packet **pkts, int nr_pkts)
{
for (int i = 0; i < nr_pkts; i++) {
struct pio_packet *p = (struct pio_packet *)pkts[i];

View File

@@ -133,9 +133,9 @@ int pio_pcap_file_device_open(struct packet_io_device *pdev);
*/
int pio_pcap_file_device_close(struct packet_io_device *pdev);
int pio_pcap_file_device_receive(struct packet_io_device *pdev, uint32_t rxq_id, struct stellar_packet **pkts, int nr_pkts);
int pio_pcap_file_device_receive(struct packet_io_device *pdev, uint16_t rxq_id, struct stellar_packet **pkts, int nr_pkts);
void pio_pcap_file_device_pkt_free(struct packet_io_device *pdev, uint32_t qid, struct stellar_packet **pkts, int nr_pkts);
void pio_pcap_file_device_pkt_free(struct packet_io_device *pdev, uint16_t qid, struct stellar_packet **pkts, int nr_pkts);
void *pio_pcap_file_device_buff_ctrlzone(struct stellar_packet *p);

View File

@@ -160,24 +160,23 @@ static void pcap_live_pkt_callback_oneshot(char *user, struct pcap_pkthdr *pkt_h
p->pkt_hdr = p;
p->pkt_payload = (uint8_t *)p + CUSTOM_ZONE_LEN;
p->pkt_len = pkt_hdr->caplen;
p->data_link = plive_dev_ctx->data_link;
if (packet_copy_data((uint8_t *)p->pkt_payload, (uint8_t *)pkt, pkt_hdr->caplen)) {
FREE(p);
return;
}
//uint32_t nr_rxq = plive_dev_ctx->pio_dev->rxq_num;
/*
hash to specific queue id and enqueue
hash_id = decode_packet(p) % nr_rxq;
packet_enqueue(&pfile_dev_ctx->pkt_queues[hash_id], p);
*/
int rxq_id = 0;
/* nr_rxq <= PKT_QUEUE_MAX_NUM */
uint16_t nr_rxq = plive_dev_ctx->pio_dev->rxq_num;
uint16_t rxq_id = pio_packet_hash(p) % nr_rxq;
/* hash to specific queue id and enqueue */
pthread_mutex_lock(&plive_dev_ctx->pkt_queues[rxq_id].mutex_q);
pio_packet_enqueue(&plive_dev_ctx->pkt_queues[rxq_id], p);
pthread_mutex_unlock(&plive_dev_ctx->pkt_queues[rxq_id].mutex_q);
}
int pio_pcap_live_device_receive(struct packet_io_device *pdev, uint32_t rxq_id, struct stellar_packet **pkts, int nr_pkts)
int pio_pcap_live_device_receive(struct packet_io_device *pdev, uint16_t rxq_id, struct stellar_packet **pkts, int nr_pkts)
{
int res = -1;
@@ -218,7 +217,7 @@ int pio_pcap_live_device_receive(struct packet_io_device *pdev, uint32_t rxq_id,
return res;
}
int pio_pcap_live_device_send(struct packet_io_device *pdev, uint32_t txq_id, struct stellar_packet **pkts, int nr_pkts)
int pio_pcap_live_device_send(struct packet_io_device *pdev, uint16_t txq_id, struct stellar_packet **pkts, int nr_pkts)
{
int res = -1;
@@ -239,7 +238,7 @@ int pio_pcap_live_device_send(struct packet_io_device *pdev, uint32_t txq_id, st
return 0;
}
void pio_pcap_live_device_pkt_free(__unused struct packet_io_device *pdev, __unused uint32_t qid, struct stellar_packet **pkts, int nr_pkts)
void pio_pcap_live_device_pkt_free(__unused struct packet_io_device *pdev, __unused uint16_t qid, struct stellar_packet **pkts, int nr_pkts)
{
for (int i = 0; i < nr_pkts; i++) {
struct pio_packet *p = (struct pio_packet *)pkts[i];

View File

@@ -93,11 +93,11 @@ int pio_pcap_live_device_open(struct packet_io_device *pdev);
*/
int pio_pcap_live_device_close(struct packet_io_device *pdev);
int pio_pcap_live_device_receive(struct packet_io_device *pdev, uint32_t rxq_id, struct stellar_packet **pkts, int nr_pkts);
int pio_pcap_live_device_receive(struct packet_io_device *pdev, uint16_t rxq_id, struct stellar_packet **pkts, int nr_pkts);
int pio_pcap_live_device_send(struct packet_io_device *pdev, uint32_t txq_id, struct stellar_packet **pkts, int nr_pkts);
int pio_pcap_live_device_send(struct packet_io_device *pdev, uint16_t txq_id, struct stellar_packet **pkts, int nr_pkts);
void pio_pcap_live_device_pkt_free(struct packet_io_device *pdev, uint32_t qid, struct stellar_packet **pkts, int nr_pkts);
void pio_pcap_live_device_pkt_free(struct packet_io_device *pdev, uint16_t qid, struct stellar_packet **pkts, int nr_pkts);
void *pio_pcap_live_device_buff_ctrlzone(struct stellar_packet *p);