summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorluqiuwen <[email protected]>2018-12-16 19:19:32 +0600
committerluqiuwen <[email protected]>2018-12-28 16:47:17 +0600
commitda1896a51dbe3768835c75f18db1db1876f6ad42 (patch)
treee6c7e2bf50c6927b78fbe646f0d85f65c3bb3129 /examples
parent520f9ed51651ad822239add9beda3b97f1caf942 (diff)
#3 实现按链路号报文构建功能,并增加注入测试工具
Diffstat (limited to 'examples')
-rw-r--r--examples/CMakeLists.txt3
-rw-r--r--examples/rxonly.c4
-rw-r--r--examples/vxlan-encap.c4
-rw-r--r--examples/vxlan-virtual-link-id.cc184
4 files changed, 191 insertions, 4 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index 5b73a59..6746f31 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -17,3 +17,6 @@ target_link_libraries(mrtest-pag-rx pag)
add_executable(mrtest-vxlan-encap vxlan-encap.c)
target_link_libraries(mrtest-vxlan-encap marsio pthread)
+
+add_executable(mrtest-vxlan-link-id-inject vxlan-virtual-link-id.cc)
+target_link_libraries(mrtest-vxlan-link-id-inject marsio pthread pcap) \ No newline at end of file
diff --git a/examples/rxonly.c b/examples/rxonly.c
index cab15e1..840af84 100644
--- a/examples/rxonly.c
+++ b/examples/rxonly.c
@@ -20,7 +20,7 @@ struct mr_sendpath * sendpath = NULL;
unsigned int nr_burst = 32;
unsigned int self_loop = 0;
-void * rxonly_loop(void * arg)
+void * txonly_loop(void * arg)
{
uintptr_t sid = (uintptr_t)arg;
marsio_buff_t * rx_buff[BURST_MAX];
@@ -122,7 +122,7 @@ int main(int argc, char * argv[])
pthread_t __tmp_pid[nr_thread];
for (int i = 0; i < nr_thread; i++)
{
- pthread_create(&__tmp_pid[i], NULL, rxonly_loop, (void *)(uintptr_t)i);
+ pthread_create(&__tmp_pid[i], NULL, txonly_loop, (void *) (uintptr_t) i);
}
for (int i = 0; i < nr_thread; i++)
diff --git a/examples/vxlan-encap.c b/examples/vxlan-encap.c
index 68a3d3b..871102b 100644
--- a/examples/vxlan-encap.c
+++ b/examples/vxlan-encap.c
@@ -22,7 +22,7 @@ struct mr_sendpath * sendpath = NULL;
unsigned int nr_burst = 1;
unsigned int self_loop = 0;
-void * rxonly_loop(void * arg)
+void * txonly_loop(void * arg)
{
uintptr_t sid = (uintptr_t)arg;
marsio_buff_t * rx_buff[BURST_MAX];
@@ -142,7 +142,7 @@ int main(int argc, char * argv[])
pthread_t __tmp_pid[nr_thread];
for (int i = 0; i < nr_thread; i++)
{
- pthread_create(&__tmp_pid[i], NULL, rxonly_loop, (void *)(uintptr_t)i);
+ pthread_create(&__tmp_pid[i], NULL, txonly_loop, (void *) (uintptr_t) i);
}
for (int i = 0; i < nr_thread; i++)
diff --git a/examples/vxlan-virtual-link-id.cc b/examples/vxlan-virtual-link-id.cc
new file mode 100644
index 0000000..9a675dd
--- /dev/null
+++ b/examples/vxlan-virtual-link-id.cc
@@ -0,0 +1,184 @@
+
+extern "C"
+{
+#include <marsio.h>
+#include <mrtunnat.h>
+#include <stdio.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <pthread.h>
+#include <unistd.h>
+#include <string.h>
+#include <pcap/pcap.h>
+}
+
+#include <string>
+
+static char appsym[64] = "vxlan_inject";
+static char dev_symbol[64] = "vxlan_user";
+static char str_pcap_file[2048] = "dumpfile";
+
+uint64_t cpu_mask = 0x1;
+unsigned int nr_thread = 1;
+uint64_t link_id = 0;
+
+struct mr_instance * mr_instance = NULL;
+struct mr_vdev * dev_handler = NULL;
+struct mr_sendpath * sendpath = NULL;
+
+#define BURST_MAX 64
+unsigned int nr_burst = 1;
+unsigned int self_loop = 0;
+
+class PacketLoader
+{
+private:
+ pcap_t*pcap_handle_{ nullptr };
+
+public:
+ static constexpr unsigned int PKTLEN_MAX = 4096;
+ void LoadFromPcapFile(const std::string &path)
+ {
+ char error_buffer[PCAP_ERRBUF_SIZE];
+ pcap_handle_ = pcap_open_offline(path.c_str(), error_buffer);
+ assert(pcap_handle_ != nullptr);
+ }
+
+ virtual ~PacketLoader()
+ {
+ if (pcap_handle_) pcap_close(pcap_handle_);
+ }
+
+ const unsigned char * FetchPacket(unsigned & pkt_len)
+ {
+ struct pcap_pkthdr header{};
+ auto *pkt_content = pcap_next(pcap_handle_, &header);
+ pkt_len = header.caplen;
+ return pkt_content;
+ }
+};
+
+class PacketLoader * packet_loader = nullptr;
+
+void * txonly_loop(void * arg)
+{
+ uintptr_t sid = (uintptr_t) arg;
+ marsio_buff_t * rx_buff[BURST_MAX];
+ marsio_buff_t * tx_buff[BURST_MAX];
+ marsio_thread_init(mr_instance);
+
+ struct mr_tunnat_ctrlzone tx_ctrlzone;
+ memset(&tx_ctrlzone, 0, sizeof(struct mr_tunnat_ctrlzone));
+
+ tx_ctrlzone.virtual_link_id = link_id;
+ tx_ctrlzone.action = TUNNAT_CZ_ACTION_ENCAP_VIRTUAL_LINK_ID;
+
+ for (;;)
+ {
+ unsigned pkt_len;
+ auto * pkt_ptr = packet_loader->FetchPacket(pkt_len);
+ if(pkt_ptr == nullptr) break;
+
+ int ret = marsio_buff_malloc_global(mr_instance, tx_buff, 1, MARSIO_SOCKET_ID_ANY, MARSIO_LCORE_ID_ANY);
+ assert(ret >= 0);
+
+ marsio_buff_t * tx_buff_ptr = tx_buff[0];
+ char * tx_buff_begin = marsio_buff_append(tx_buff_ptr, pkt_len);
+
+ memcpy(tx_buff_begin, pkt_ptr, pkt_len);
+ marsio_buff_ctrlzone_set(tx_buff_ptr, 0, &tx_ctrlzone, sizeof(tx_ctrlzone));
+ marsio_send_burst_with_options(sendpath, sid, tx_buff, 1, MARSIO_SEND_OPT_FAST);
+ }
+
+ return (void *) NULL;
+}
+
+int help()
+{
+ return 0;
+}
+
+int main(int argc, char * argv[])
+{
+ int opt = 0;
+ while ((opt = getopt(argc, argv, "s:t:r:a:c:b:d:h?rl")) != -1)
+ {
+ char * endptr = NULL;
+ switch (opt)
+ {
+ case '?':
+ case 'h':
+ {
+ help();
+ break;
+ }
+ case 'd':
+ {
+ snprintf(dev_symbol, sizeof(dev_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 'r':
+ {
+ snprintf(str_pcap_file, sizeof(str_pcap_file), "%s", optarg);
+ break;
+ }
+ case 't':
+ {
+ link_id = strtoull(optarg, &endptr, 0);
+ if (cpu_mask == 0 && endptr == optarg) help();
+ break;
+ }
+
+ default:help();
+ break;
+ }
+ }
+
+ mr_instance = marsio_create();
+ if (mr_instance == nullptr)
+ {
+ 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_handler = marsio_open_device(mr_instance, dev_symbol, 0, nr_thread);
+ fprintf(stdout, "Thread Count = %d\n", nr_thread);
+
+ sendpath = marsio_sendpath_create_by_vdev(dev_handler);
+ assert(sendpath != nullptr);
+
+ packet_loader = new PacketLoader();
+ packet_loader->LoadFromPcapFile(str_pcap_file);
+
+ pthread_t __tmp_pid[nr_thread];
+ for (int i = 0; i < nr_thread; i++)
+ {
+ pthread_create(&__tmp_pid[i], NULL, txonly_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, "RXONLY is terminated. ");
+ return 0;
+}