summaryrefslogtreecommitdiff
path: root/examples/feedback.c
blob: e4798d71e47b29463bc988e8bef6afa3ddae2ee4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

#include <marsio.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>

static char appsym[64] = "mrtest-fb";
static char dev_src_symbol[64] = "meth1";
static char dev_dst_symbol[64] = "meth0";
static char str_target_ipaddr[64] = "192.168.100.50";

uint64_t cpu_mask = 0xff;
unsigned int nr_thread;

struct mr_instance * mr_instance = NULL;
struct mr_vdev * dev_src_handler = NULL;
struct mr_vdev * dev_dst_handler = NULL;
struct mr_sendpath * to_dev_dst_sendpath = NULL;

#define BURST_MAX       64
unsigned int nr_burst = 32;

void * feedback_loop(void * arg)
{
    uintptr_t sid = (uintptr_t)arg;
    marsio_buff_t * rx_buff[BURST_MAX];
    marsio_buff_t * feedback_buffs[BURST_MAX];
    unsigned int ret = 0;

    marsio_thread_init(mr_instance);

    for (;;)
    {
        ret = marsio_recv_burst(dev_src_handler, sid, rx_buff, nr_burst);
        if (ret == 0) continue;

        int alloc_ret = marsio_buff_malloc_device(dev_dst_handler, feedback_buffs, ret, 0, sid);
        if (alloc_ret < 0)
        {
            printf("Alloc marsio buffer failed. ");
            marsio_buff_free(mr_instance, rx_buff, ret, 0, 0);
            continue;
        }

        for (int i = 0; i < ret; i++)
        {
            /* 复制数据 */
            unsigned int raw_len = marsio_buff_datalen(rx_buff[i]);
            void * raw_data = marsio_buff_mtod(rx_buff[i]);
            void * dst_data = marsio_buff_append(feedback_buffs[i], raw_len);
            memcpy(dst_data, raw_data, raw_len);
            
            /* 构建UDP头部 */
//            marsio_udp_header_construct(feedback_buffs[i], sid + 8080, 8866);
//            marsio_ipv4_header_construct(feedback_buffs[i], 0, 0, IPPROTO_UDP);
        }

        marsio_send_burst(to_dev_dst_sendpath, sid, feedback_buffs, ret);
        marsio_buff_free(mr_instance, rx_buff, ret, 0, 0);
    }

    return (void *)NULL;
}

int help()
{
    return 0;
}

int main(int argc, char * argv[])
{
    int opt = 0;
    while ((opt = getopt(argc, argv, "s:d:a:c:b:t:h?")) != -1)
    {
        char * endptr = NULL;
        switch (opt)
        {
        case '?':
        case 'h':
        {
            help();
            break;
        }
        case 's':
        {
            snprintf(dev_src_symbol, sizeof(dev_src_symbol), "%s", optarg);
            break;
        }
        case 'd':
        {
            snprintf(dev_dst_symbol, sizeof(dev_dst_symbol), "%s", optarg);
            break;
        }
        case 't':
        {
            snprintf(str_target_ipaddr, sizeof(str_target_ipaddr), "%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();
    }

    struct in_addr target_ip_addr;
    inet_pton(AF_INET, str_target_ipaddr, &target_ip_addr);
    
    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_src_handler = marsio_open_device(mr_instance, dev_src_symbol, nr_thread, nr_thread);
    dev_dst_handler = marsio_open_device(mr_instance, dev_dst_symbol, nr_thread, nr_thread);
    to_dev_dst_sendpath = marsio_sendpath_create_by_droute(dev_dst_handler, target_ip_addr);
    
    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, feedback_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, "Feedback is terminated. ");
    return 0;
}