diff options
| author | 陆秋文 <[email protected]> | 2022-09-21 08:26:41 +0000 |
|---|---|---|
| committer | 陆秋文 <[email protected]> | 2022-09-21 08:26:41 +0000 |
| commit | 0c134eb8c0012448bd1aa898cc6ce0bccf634d5f (patch) | |
| tree | 0ea248bb183f47d7002f26aec5ace022b4435f9c /examples | |
| parent | ee1caef09ed3e108cb80bc471a0aec4447a7f12f (diff) | |
增加virtual wire功能的实现,增加测试程序并调整libevent的编译方式。
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | examples/l2fwd-nf.c | 178 |
2 files changed, 181 insertions, 0 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index f5e44b6..3a59982 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -23,3 +23,6 @@ target_link_libraries(mrtest-vxlan-link-id-inject marsio pthread pcap) add_executable(mrtest-smartoffload smartoffload.cc) target_link_libraries(mrtest-smartoffload marsio pthread pcap) + +add_executable(mrtest-l2fwd-nf l2fwd-nf.c) +target_link_libraries(mrtest-l2fwd-nf marsio pthread pcap) diff --git a/examples/l2fwd-nf.c b/examples/l2fwd-nf.c new file mode 100644 index 0000000..bfbc1ba --- /dev/null +++ b/examples/l2fwd-nf.c @@ -0,0 +1,178 @@ + +#include <marsio.h> +#include <stdio.h> +#include <assert.h> +#include <stdlib.h> +#include <pthread.h> +#include <unistd.h> +#include <string.h> + +static char appsym[64] = "mrfwd"; +static char dev_1_symbol[64] = "meth0"; + +uint64_t cpu_mask = 0x3c; +unsigned int nr_thread; + +struct mr_instance * mr_instance = NULL; +struct mr_vdev * dev_1_handler = NULL; +struct mr_sendpath * to_dev_1_sendpath = NULL; + +#define BURST_MAX 64 +unsigned int nr_burst = 32; + +/* options */ +unsigned int opt_dump_packet_metadata = 1; +unsigned int opt_inject_by_deep_copy = 1; + +void dump_packet_metadata(marsio_buff_t * buff) +{ + unsigned int vlan_tci = 0; + unsigned int is_vlan_tci_set = 0; + + int ret = marsio_buff_get_metadata(buff, MR_BUFF_METADATA_VLAN_TCI, &vlan_tci, sizeof(vlan_tci)); + is_vlan_tci_set = ret >= 0 ? 1 : 0; + + fprintf(stderr, "packet buf = %p, is_vlan_tci_set = %u, vlan_tci = %u\n", buff, is_vlan_tci_set, vlan_tci); +} + +__attribute__((noreturn)) void * l2fwd_loop(void * arg) +{ + uintptr_t sid = (uintptr_t)arg; + marsio_buff_t * rx_buff[BURST_MAX]; + marsio_buff_t * tx_buff[BURST_MAX]; + unsigned int ret = 0; + + marsio_thread_init(mr_instance); + + for (;;) + { + ret = marsio_recv_burst(dev_1_handler, sid, rx_buff, (int)nr_burst); + if (ret <= 0) + { + continue; + } + + for (int i = 0; i < ret; i++) + { + if(opt_dump_packet_metadata) + { + dump_packet_metadata(rx_buff[i]); + } + + if(opt_inject_by_deep_copy) + { + marsio_buff_t * orin_buff = rx_buff[i]; + marsio_buff_t * deep_copy_buff = NULL; + marsio_buff_malloc_global(mr_instance, &deep_copy_buff, 1, MARSIO_SOCKET_ID_ANY, MARSIO_LCORE_ID_ANY); + + /* copy the packet body */ + unsigned int orin_pkt_len = marsio_buff_datalen(orin_buff); + const char * orin_pkt_ptr = marsio_buff_mtod(orin_buff); + + char * deep_copy_ptr = marsio_buff_append(deep_copy_buff, orin_pkt_len); + memcpy(deep_copy_ptr, orin_pkt_ptr, orin_pkt_len); + + /* set the vlan metadata */ + unsigned int vlan_tci = 0; + marsio_buff_get_metadata(orin_buff, MR_BUFF_METADATA_VLAN_TCI, &vlan_tci, sizeof(vlan_tci)); + + //vlan_tci = vlan_tci ^ 0x1; + marsio_buff_set_metadata(deep_copy_buff, MR_BUFF_METADATA_VLAN_TCI, &vlan_tci, sizeof(vlan_tci)); + + tx_buff[i] = deep_copy_buff; + } + } + + if(opt_inject_by_deep_copy) + { + marsio_buff_free(mr_instance, rx_buff, ret, MARSIO_SOCKET_ID_ANY, MARSIO_LCORE_ID_ANY); + marsio_send_burst(to_dev_1_sendpath, sid, tx_buff, (int)ret); + } + else + { + marsio_send_burst(to_dev_1_sendpath, sid, rx_buff, (int)ret); + } + } +} + + +int help() +{ + return 0; +} + +int main(int argc, char * argv[]) +{ + int opt = 0; + while ((opt = getopt(argc, argv, "s:d:a:c:b:h?")) != -1) + { + char * endptr = NULL; + switch (opt) + { + case '?': + case 'h': + { + help(); + break; + } + case 's': + { + snprintf(dev_1_symbol, sizeof(dev_1_symbol), "%s", optarg); + break; + } + case 'a': + { + snprintf(appsym, sizeof(appsym), "%s", optarg); + break; + } + case 'c': + { + cpu_mask = strtoull(optarg, &endptr, 0); + if (cpu_mask == 0 && endptr == optarg) help(); + break; + } + case 'b': + { + nr_burst = strtoull(optarg, &endptr, 0); + if (nr_burst == 0 && endptr == optarg) help(); + break; + } + + default: + help(); + break; + } + } + + mr_instance = marsio_create(); + if (mr_instance == NULL) + { + fprintf(stderr, "Marsio instance create failed. "); + abort(); + } + + unsigned int opt_value = 1; + marsio_option_set(mr_instance, MARSIO_OPT_EXIT_WHEN_ERR, &opt_value, sizeof(opt_value)); + marsio_option_set(mr_instance, MARSIO_OPT_THREAD_MASK, &cpu_mask, sizeof(cpu_mask)); + marsio_init(mr_instance, appsym); + + nr_thread = __builtin_popcountll(cpu_mask); + dev_1_handler = marsio_open_device(mr_instance, dev_1_symbol, nr_thread, nr_thread); + to_dev_1_sendpath = marsio_sendpath_create_by_vdev(dev_1_handler); + + fprintf(stdout, "Thread Count = %d\n", nr_thread); + pthread_t __tmp_pid[nr_thread]; + for (int i = 0; i < nr_thread; i++) + { + pthread_create(&__tmp_pid[i], NULL, l2fwd_loop, (void *)(uintptr_t)i); + } + + for (int i = 0; i < nr_thread; i++) + { + pthread_join(__tmp_pid[i], NULL); + } + + marsio_destory(mr_instance); + fprintf(stdout, "L2FWD is terminated. "); + return 0; +} |
