-
Notifications
You must be signed in to change notification settings - Fork 86
/
Efvi.h
610 lines (534 loc) · 18 KB
/
Efvi.h
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
MIT License
Copyright (c) 2019 Meng Rao <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <etherfabric/vi.h>
#include <etherfabric/pd.h>
#include <etherfabric/memreg.h>
#include <etherfabric/capabilities.h>
#include <arpa/inet.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <net/if.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
class EfviReceiver
{
public:
const char* getLastError() { return last_error_; };
bool isClosed() { return dh < 0; }
protected:
bool init(const char* interface) {
int rc;
if ((rc = ef_driver_open(&dh)) < 0) {
saveError("ef_driver_open failed", rc);
return false;
}
if ((rc = ef_pd_alloc_by_name(&pd, dh, interface, EF_PD_DEFAULT)) < 0) {
saveError("ef_pd_alloc_by_name failed", rc);
return false;
}
int vi_flags = EF_VI_FLAGS_DEFAULT;
if ((rc = ef_vi_alloc_from_pd(&vi, dh, &pd, dh, -1, N_BUF + 1, 0, NULL, -1, (enum ef_vi_flags)vi_flags)) < 0) {
saveError("ef_vi_alloc_from_pd failed", rc);
return false;
}
size_t alloc_size = N_BUF * PKT_BUF_SIZE;
buf_mmapped = true;
pkt_bufs =
(uint8_t*)mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_HUGETLB, -1, 0);
if (pkt_bufs == MAP_FAILED) {
buf_mmapped = false;
rc = posix_memalign((void**)&pkt_bufs, 4096, alloc_size);
if (rc != 0) {
saveError("posix_memalign failed", -rc);
return false;
}
}
if ((rc = ef_memreg_alloc(&memreg, dh, &pd, dh, pkt_bufs, alloc_size)) < 0) {
saveError("ef_memreg_alloc failed", rc);
return false;
}
for (int i = 0; i < N_BUF; i++) {
struct pkt_buf* pkt_buf = (struct pkt_buf*)(pkt_bufs + i * PKT_BUF_SIZE);
pkt_buf->post_addr =
ef_memreg_dma_addr(&memreg, i * PKT_BUF_SIZE) + 64; // reserve a cache line for saving ef_addr...
if ((rc = ef_vi_receive_post(&vi, pkt_buf->post_addr, i)) < 0) {
saveError("ef_vi_receive_post failed", rc);
return false;
}
}
return true;
}
void saveError(const char* msg, int rc) {
snprintf(last_error_, sizeof(last_error_), "%s %s", msg, rc < 0 ? (const char*)strerror(-rc) : "");
}
void close() {
if (dh >= 0) {
ef_driver_close(dh);
dh = -1;
}
if (pkt_bufs) {
if (buf_mmapped) {
munmap(pkt_bufs, N_BUF * PKT_BUF_SIZE);
}
else {
free(pkt_bufs);
}
pkt_bufs = nullptr;
}
}
static const int N_BUF = 512;
static const int PKT_BUF_SIZE = 2048;
struct pkt_buf
{
ef_addr post_addr;
};
struct ef_vi vi;
uint8_t* pkt_bufs = nullptr;
ef_driver_handle dh = -1;
struct ef_pd pd;
struct ef_memreg memreg;
bool buf_mmapped;
char last_error_[64] = "";
};
class EfviUdpReceiver : public EfviReceiver
{
public:
bool init(const char* interface, const char* dest_ip, uint16_t dest_port, const char* subscribe_ip = "") {
if (!EfviReceiver::init(interface)) {
return false;
}
udp_prefix_len = 64 + ef_vi_receive_prefix_len(&vi) + 14 + 20 + 8;
int rc;
ef_filter_spec filter_spec;
struct sockaddr_in sa_local;
sa_local.sin_port = htons(dest_port);
inet_pton(AF_INET, dest_ip, &(sa_local.sin_addr));
ef_filter_spec_init(&filter_spec, EF_FILTER_FLAG_NONE);
if ((rc = ef_filter_spec_set_ip4_local(&filter_spec, IPPROTO_UDP, sa_local.sin_addr.s_addr, sa_local.sin_port)) <
0) {
saveError("ef_filter_spec_set_ip4_local failed", rc);
return false;
}
if ((rc = ef_vi_filter_add(&vi, dh, &filter_spec, NULL)) < 0) {
saveError("ef_vi_filter_add failed", rc);
return false;
}
if (subscribe_ip[0]) {
if ((subscribe_fd_ = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
saveError("socket failed", -errno);
return false;
}
struct ip_mreq group;
inet_pton(AF_INET, subscribe_ip, &(group.imr_interface));
inet_pton(AF_INET, dest_ip, &(group.imr_multiaddr));
if (setsockopt(subscribe_fd_, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&group, sizeof(group)) < 0) {
saveError("setsockopt IP_ADD_MEMBERSHIP failed", -errno);
return false;
}
}
return true;
}
~EfviUdpReceiver() { close("destruct"); }
void close(const char* reason) {
if (subscribe_fd_ >= 0) {
saveError(reason, 0);
::close(subscribe_fd_);
subscribe_fd_ = -1;
}
EfviReceiver::close();
}
template<typename Handler>
bool read(Handler handler) {
ef_event evs;
if (ef_eventq_poll(&vi, &evs, 1) == 0) return false;
int id = EF_EVENT_RX_RQ_ID(evs);
struct pkt_buf* pkt_buf = (struct pkt_buf*)(pkt_bufs + id * PKT_BUF_SIZE);
int type = EF_EVENT_TYPE(evs);
bool ret = false;
if (type == EF_EVENT_TYPE_RX) {
const uint8_t* data = (const uint8_t*)pkt_buf + udp_prefix_len;
uint16_t len = ntohs(*(uint16_t*)(data - 4)) - 8;
handler(data, len);
ret = true;
}
ef_vi_receive_post(&vi, pkt_buf->post_addr, id);
return ret;
}
// for those whose ip checksum is not computed...
template<typename Handler>
bool readNoCheck(Handler handler) {
ef_event evs;
if (ef_eventq_poll(&vi, &evs, 1) == 0) return false;
int id = EF_EVENT_RX_RQ_ID(evs);
struct pkt_buf* pkt_buf = (struct pkt_buf*)(pkt_bufs + id * PKT_BUF_SIZE);
const uint8_t* data = (const uint8_t*)pkt_buf + udp_prefix_len;
uint16_t len = ntohs(*(uint16_t*)(data - 4)) - 8;
handler(data, len);
ef_vi_receive_post(&vi, pkt_buf->post_addr, id);
return true;
}
template<typename Handler>
bool recvfrom(Handler handler) {
ef_event evs;
if (ef_eventq_poll(&vi, &evs, 1) == 0) return false;
struct sockaddr_in src_addr;
int id = EF_EVENT_RX_RQ_ID(evs);
struct pkt_buf* pkt_buf = (struct pkt_buf*)(pkt_bufs + id * PKT_BUF_SIZE);
const uint8_t* data = (const uint8_t*)pkt_buf + udp_prefix_len;
src_addr.sin_family = AF_INET;
src_addr.sin_addr.s_addr = *(uint32_t*)(data - 16);
src_addr.sin_port = *(uint16_t*)(data - 8);
uint16_t len = ntohs(*(uint16_t*)(data - 4)) - 8;
handler(data, len, src_addr);
ef_vi_receive_post(&vi, pkt_buf->post_addr, id);
return true;
}
private:
int udp_prefix_len;
int subscribe_fd_ = -1;
};
class EfviEthReceiver : public EfviReceiver
{
public:
bool init(const char* interface, bool promiscuous = false) {
if (!EfviReceiver::init(interface)) {
return false;
}
rx_prefix_len = ef_vi_receive_prefix_len(&vi);
int rc;
ef_filter_spec fs;
ef_filter_spec_init(&fs, EF_FILTER_FLAG_NONE);
if ((rc = ef_filter_spec_set_port_sniff(&fs, (int)promiscuous)) < 0) {
saveError("ef_filter_spec_set_port_sniff failed", rc);
return false;
}
if ((rc = ef_vi_filter_add(&vi, dh, &fs, NULL)) < 0) {
saveError("ef_vi_filter_add failed", rc);
return false;
}
return true;
}
~EfviEthReceiver() { close(); }
void close() { EfviReceiver::close(); }
template<typename Handler>
bool read(Handler handler) {
ef_event evs;
if (ef_eventq_poll(&vi, &evs, 1) == 0) return false;
int id = EF_EVENT_RX_RQ_ID(evs);
struct pkt_buf* pkt_buf = (struct pkt_buf*)(pkt_bufs + id * PKT_BUF_SIZE);
int type = EF_EVENT_TYPE(evs);
bool ret = false;
if (type == EF_EVENT_TYPE_RX) {
const uint8_t* data = (const uint8_t*)pkt_buf + 64 + rx_prefix_len;
uint32_t len = EF_EVENT_RX_BYTES(evs) - rx_prefix_len;
handler(data, len);
}
ef_vi_receive_post(&vi, pkt_buf->post_addr, id);
return ret;
}
private:
int rx_prefix_len;
};
class EfviUdpSender
{
public:
bool init(const char* interface, const char* local_ip, uint16_t local_port, const char* dest_ip, uint16_t dest_port) {
struct sockaddr_in local_addr;
struct sockaddr_in dest_addr;
uint8_t local_mac[6];
uint8_t dest_mac[6];
local_addr.sin_port = htons(local_port);
inet_pton(AF_INET, local_ip, &(local_addr.sin_addr));
dest_addr.sin_port = htons(dest_port);
inet_pton(AF_INET, dest_ip, &(dest_addr.sin_addr));
if ((0xff & dest_addr.sin_addr.s_addr) < 224) { // unicast
char dest_mac_addr[64];
if (!getMacFromARP(interface, dest_ip, dest_mac_addr)) {
char gw[64];
if (!getGW(dest_ip, gw) || !getMacFromARP(interface, gw, dest_mac_addr)) {
saveError("Can't find dest ip from arp cache, please ping dest ip first", 0);
return false;
}
}
// std::cout << "dest_mac_addr: " << dest_mac_addr << std::endl;
if (strlen(dest_mac_addr) != 17) {
saveError("invalid dest_mac_addr", 0);
return false;
}
for (int i = 0; i < 6; ++i) {
dest_mac[i] = hexchartoi(dest_mac_addr[3 * i]) * 16 + hexchartoi(dest_mac_addr[3 * i + 1]);
}
}
else { // multicast
dest_mac[0] = 0x1;
dest_mac[1] = 0;
dest_mac[2] = 0x5e;
dest_mac[3] = 0x7f & (dest_addr.sin_addr.s_addr >> 8);
dest_mac[4] = 0xff & (dest_addr.sin_addr.s_addr >> 16);
dest_mac[5] = 0xff & (dest_addr.sin_addr.s_addr >> 24);
}
int rc;
if ((rc = ef_driver_open(&dh)) < 0) {
saveError("ef_driver_open failed", rc);
return false;
}
if ((rc = ef_pd_alloc_by_name(&pd, dh, interface, EF_PD_DEFAULT)) < 0) {
saveError("ef_pd_alloc_by_name failed", rc);
return false;
}
int vi_flags = EF_VI_FLAGS_DEFAULT;
int ifindex = if_nametoindex(interface);
unsigned long capability_val = 0;
if (ef_vi_capabilities_get(dh, ifindex, EF_VI_CAP_CTPIO, &capability_val) == 0 && capability_val) {
use_ctpio = true;
vi_flags |= EF_VI_TX_CTPIO;
}
if ((rc = ef_vi_alloc_from_pd(&vi, dh, &pd, dh, -1, 0, N_BUF + 1, NULL, -1, (enum ef_vi_flags)vi_flags)) < 0) {
saveError("ef_vi_alloc_from_pd failed", rc);
return false;
}
ef_vi_get_mac(&vi, dh, local_mac);
size_t alloc_size = N_BUF * PKT_BUF_SIZE;
buf_mmapped = true;
pkt_bufs =
(uint8_t*)mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_HUGETLB, -1, 0);
if (pkt_bufs == MAP_FAILED) {
buf_mmapped = false;
rc = posix_memalign((void**)&pkt_bufs, 4096, alloc_size);
if (rc != 0) {
saveError("posix_memalign failed", -rc);
return false;
}
}
if ((rc = ef_memreg_alloc(&memreg, dh, &pd, dh, pkt_bufs, alloc_size)) < 0) {
saveError("ef_memreg_alloc failed", rc);
return false;
}
for (int i = 0; i < N_BUF; i++) {
struct pkt_buf* pkt = (struct pkt_buf*)(pkt_bufs + i * PKT_BUF_SIZE);
pkt->post_addr = ef_memreg_dma_addr(&memreg, i * PKT_BUF_SIZE) + sizeof(ef_addr);
init_udp_pkt(&(pkt->eth), local_addr, local_mac, dest_addr, dest_mac);
}
uint16_t* ip4 = (uint16_t*)&((struct pkt_buf*)pkt_bufs)->ip4;
ipsum_cache = 0;
for (int i = 0; i < 10; i++) {
ipsum_cache += ip4[i];
}
ipsum_cache = (ipsum_cache >> 16u) + (ipsum_cache & 0xffff);
ipsum_cache += (ipsum_cache >> 16u);
return true;
}
~EfviUdpSender() { close(); }
const char* getLastError() { return last_error_; };
bool isClosed() { return dh < 0; }
void close() {
if (dh >= 0) {
ef_driver_close(dh);
dh = -1;
}
if (pkt_bufs) {
if (buf_mmapped) {
munmap(pkt_bufs, N_BUF * PKT_BUF_SIZE);
}
else {
free(pkt_bufs);
}
pkt_bufs = nullptr;
}
}
bool write(const void* data, uint32_t size) {
struct pkt_buf* pkt = (struct pkt_buf*)(pkt_bufs + buf_index_ * PKT_BUF_SIZE);
struct ci_ether_hdr* eth = &pkt->eth;
struct ci_ip4_hdr* ip4 = (struct ci_ip4_hdr*)(eth + 1);
struct ci_udp_hdr* udp = (struct ci_udp_hdr*)(ip4 + 1);
uint16_t iplen = htons(28 + size);
ip4->ip_tot_len_be16 = iplen;
udp->udp_len_be16 = htons(8 + size);
memcpy(pkt + 1, data, size);
uint32_t frame_len = 42 + size;
int rc;
if (use_ctpio) {
uint32_t ipsum = ipsum_cache + iplen;
ipsum += (ipsum >> 16u);
ip4->ip_check_be16 = ~ipsum & 0xffff;
ef_vi_transmit_ctpio(&vi, &pkt->eth, frame_len, frame_len);
rc = ef_vi_transmit_ctpio_fallback(&vi, pkt->post_addr, frame_len, buf_index_);
}
else {
rc = ef_vi_transmit(&vi, pkt->post_addr, frame_len, buf_index_);
}
buf_index_ = (buf_index_ + 1) % N_BUF;
ef_event evs[EF_VI_EVENT_POLL_MIN_EVS];
ef_request_id ids[EF_VI_TRANSMIT_BATCH];
int events = ef_eventq_poll(&vi, evs, EF_VI_EVENT_POLL_MIN_EVS);
for (int i = 0; i < events; ++i) {
if (EF_EVENT_TYPE_TX == EF_EVENT_TYPE(evs[i])) {
ef_vi_transmit_unbundle(&vi, &evs[i], ids);
}
}
return rc == 0;
}
private:
bool getMacFromARP(const char* interface, const char* dest_ip, char* dest_mac) {
FILE* arp_file = fopen("/proc/net/arp", "r");
if (!arp_file) {
saveError("Can't open /proc/net/arp", -errno);
return false;
}
char header[1024];
if (!fgets(header, sizeof(header), arp_file)) {
saveError("Invalid file /proc/net/arp", 0);
fclose(arp_file);
return false;
}
char ip[64], hw[64], device[64];
while (3 == fscanf(arp_file, "%63s %*s %*s %63s %*s %63s", ip, hw, device)) {
if (!strcmp(ip, dest_ip) && !strcmp(interface, device)) {
strcpy(dest_mac, hw);
fclose(arp_file);
return true;
}
}
fclose(arp_file);
return false;
}
bool getGW(const char* ip, char* gw) {
char buf[1024];
sprintf(buf, "/usr/sbin/ip route get %s", ip);
FILE* pipe = popen(buf, "r");
if (!pipe) return false;
while (fgets(buf, sizeof(buf), pipe)) {
if (sscanf(buf, "%*s via %s", gw) == 1) {
pclose(pipe);
return true;
}
}
pclose(pipe);
return false;
}
void saveError(const char* msg, int rc) {
snprintf(last_error_, sizeof(last_error_), "%s %s", msg, rc < 0 ? (const char*)strerror(-rc) : "");
}
uint8_t hexchartoi(char c) {
if (c >= '0' && c <= '9')
return c - '0';
else if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
else if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
else
return 0;
}
#pragma pack(push, 1)
struct ci_ether_hdr
{
uint8_t ether_dhost[6];
uint8_t ether_shost[6];
uint16_t ether_type;
};
struct ci_ip4_hdr
{
uint8_t ip_ihl_version;
uint8_t ip_tos;
uint16_t ip_tot_len_be16;
uint16_t ip_id_be16;
uint16_t ip_frag_off_be16;
uint8_t ip_ttl;
uint8_t ip_protocol;
uint16_t ip_check_be16;
uint32_t ip_saddr_be32;
uint32_t ip_daddr_be32;
/* ...options... */
};
struct ci_udp_hdr
{
uint16_t udp_source_be16;
uint16_t udp_dest_be16;
uint16_t udp_len_be16;
uint16_t udp_check_be16;
};
struct pkt_buf
{
ef_addr post_addr;
ci_ether_hdr eth;
ci_ip4_hdr ip4;
ci_udp_hdr udp;
};
#pragma pack(pop)
void init_udp_pkt(void* buf, struct sockaddr_in& local_addr, uint8_t* local_mac, struct sockaddr_in& dest_addr,
uint8_t* dest_mac) {
struct ci_ether_hdr* eth = (struct ci_ether_hdr*)buf;
struct ci_ip4_hdr* ip4 = (struct ci_ip4_hdr*)(eth + 1);
struct ci_udp_hdr* udp = (struct ci_udp_hdr*)(ip4 + 1);
eth->ether_type = htons(0x0800);
memcpy(eth->ether_shost, local_mac, 6);
memcpy(eth->ether_dhost, dest_mac, 6);
ci_ip4_hdr_init(ip4, 0, 0, 0, IPPROTO_UDP, local_addr.sin_addr.s_addr, dest_addr.sin_addr.s_addr);
ci_udp_hdr_init(udp, ip4, local_addr.sin_port, dest_addr.sin_port, 0);
}
inline int update_udp_pkt(void* buf, uint32_t paylen) {
struct ci_ether_hdr* eth = (struct ci_ether_hdr*)buf;
struct ci_ip4_hdr* ip4 = (struct ci_ip4_hdr*)(eth + 1);
struct ci_udp_hdr* udp = (struct ci_udp_hdr*)(ip4 + 1);
uint16_t iplen = htons(28 + paylen);
ip4->ip_tot_len_be16 = iplen;
uint32_t ipsum = ipsum_cache + iplen;
ipsum += (ipsum >> 16u);
ip4->ip_check_be16 = ~ipsum & 0xffff;
udp->udp_len_be16 = htons(8 + paylen);
return 42 + paylen;
}
void ci_ip4_hdr_init(struct ci_ip4_hdr* ip, int opts_len, int tot_len, int id_be16, int protocol, unsigned saddr_be32,
unsigned daddr_be32) {
#define CI_IP4_IHL_VERSION(ihl) ((4u << 4u) | ((ihl) >> 2u))
ip->ip_ihl_version = CI_IP4_IHL_VERSION(sizeof(*ip) + opts_len);
ip->ip_tos = 0;
ip->ip_tot_len_be16 = htons(tot_len);
ip->ip_id_be16 = id_be16;
ip->ip_frag_off_be16 = 0x0040;
ip->ip_ttl = 64;
ip->ip_protocol = protocol;
ip->ip_saddr_be32 = saddr_be32;
ip->ip_daddr_be32 = daddr_be32;
ip->ip_check_be16 = 0;
}
void ci_udp_hdr_init(struct ci_udp_hdr* udp, struct ci_ip4_hdr* ip, unsigned sport_be16, unsigned dport_be16,
int payload_len) {
udp->udp_source_be16 = sport_be16;
udp->udp_dest_be16 = dport_be16;
udp->udp_len_be16 = htons(sizeof(*udp) + payload_len);
udp->udp_check_be16 = 0;
}
static const int N_BUF = 128;
static const int PKT_BUF_SIZE = 2048;
struct ef_vi vi;
ef_driver_handle dh = -1;
struct ef_pd pd;
struct ef_memreg memreg;
uint8_t* pkt_bufs = nullptr;
bool use_ctpio = false;
uint32_t ipsum_cache;
uint32_t buf_index_ = 0;
bool buf_mmapped;
char last_error_[64] = "";
};