-
Notifications
You must be signed in to change notification settings - Fork 960
Expand file tree
/
Copy pathmain.c
More file actions
424 lines (381 loc) · 12.3 KB
/
Copy pathmain.c
File metadata and controls
424 lines (381 loc) · 12.3 KB
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
/*
* helloworld_stacksel -- demonstrate F-Stack + host kernel-stack COEXISTENCE
* in native ff_api mode.
*
* Correct paradigm (v4): the application runs ON F-Stack. Business sockets use
* the F-Stack user-space stack (default / SOCK_FSTACK). A socket created with
* SOCK_KERNEL (when [stack] kernel_coexist=1) additionally uses the host Linux
* kernel stack, so local ping/curl can reach it and the app can connect() to
* local/external kernel-stack services. Both stacks coexist in the SAME
* process and a single ff_epoll loop. F-Stack is NEVER bypassed.
*
* Modes:
* (no args) selftest: kernel-stack loopback server+client via
* ff_socket(SOCK_KERNEL). This exercises the native managed
* kernel-fd path (ff_socket/bind/listen/accept/connect/
* send/recv/close) and needs NO DPDK/EAL runtime, so it can
* run anywhere.
* bench <port> kernel-stack HTTP keep-alive server returning a fixed
* preset body, driven by host epoll (ff_host_epoll_*), for
* the PERF-3 kernel-side throughput baseline. Also EAL-free.
*
* Full coexistence (F-Stack business listener + SOCK_KERNEL kernel listener +
* merged ff_epoll) additionally requires ff_init()/ff_run() and a working DPDK
* data plane (NIC/hugepages); see docs/kernel_event_support_spec for the
* design and the integration-test plan.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <stdint.h>
#include "ff_api.h" /* ff_socket + SOCK_KERNEL / SOCK_FSTACK markers */
#include "ff_config.h" /* ff_global_cfg: enable coexistence without ff_init */
#include "ff_host_interface.h" /* ff_host_epoll_* + managed kernel-fd encode/real */
#include "ff_epoll.h" /* ff_epoll_* for the dualstack (ff_init/ff_run) demo */
#define KPORT 18399
/* Create a kernel-stack socket via the single F-Stack API + SOCK_KERNEL.
* Returns a managed kernel fd (opaque; use only with ff_* calls). */
static int
ksock(void)
{
int fd = ff_socket(AF_INET, SOCK_STREAM | SOCK_KERNEL, 0);
if (fd < 0)
perror("ff_socket(SOCK_KERNEL)");
return fd;
}
static void
fill_loopback(struct sockaddr_in *sa, int port)
{
memset(sa, 0, sizeof(*sa));
sa->sin_family = AF_INET;
sa->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
sa->sin_port = htons((unsigned short)port);
}
/* Child: kernel-stack client connecting to the loopback server. */
static int
run_client(int port)
{
struct sockaddr_in sa;
char buf[64];
int c = ksock();
if (c < 0)
return 1;
fill_loopback(&sa, port);
if (ff_connect(c, (struct linux_sockaddr *)&sa, sizeof(sa)) < 0) {
perror("ff_connect");
ff_close(c);
return 1;
}
/* R8: exercise getpeername/getsockname on the kernel fd. */
struct sockaddr_in pn;
socklen_t pnlen = sizeof(pn);
if (ff_getpeername(c, (struct linux_sockaddr *)&pn, &pnlen) < 0 ||
ntohs(pn.sin_port) != (unsigned short)port) {
fprintf(stderr, "client: ff_getpeername mismatch\n");
ff_close(c);
return 1;
}
pnlen = sizeof(pn);
if (ff_getsockname(c, (struct linux_sockaddr *)&pn, &pnlen) < 0) {
perror("ff_getsockname");
ff_close(c);
return 1;
}
/* R8: send via ff_sendmsg. */
struct iovec io = { (void *)"ping", 4 };
struct msghdr mh;
memset(&mh, 0, sizeof(mh));
mh.msg_iov = &io;
mh.msg_iovlen = 1;
if (ff_sendmsg(c, &mh, 0) != 4) {
perror("ff_sendmsg");
ff_close(c);
return 1;
}
ssize_t n = ff_recv(c, buf, sizeof(buf), 0);
ff_close(c);
if (n == 4 && memcmp(buf, "pong", 4) == 0)
return 0;
fprintf(stderr, "client: unexpected reply (n=%zd)\n", n);
return 1;
}
/* Parent: kernel-stack server, accept one connection and echo ping->pong. */
static int
run_server_once(int lfd)
{
char buf[64];
int c = ff_accept(lfd, NULL, NULL);
if (c < 0) {
perror("ff_accept");
return 1;
}
/* R8: getpeername/getsockname on the accepted kernel fd. */
struct sockaddr_in pn;
socklen_t pnlen = sizeof(pn);
if (ff_getpeername(c, (struct linux_sockaddr *)&pn, &pnlen) < 0 ||
ff_getsockname(c, (struct linux_sockaddr *)&pn, &pnlen) < 0) {
perror("ff_get*name");
ff_close(c);
return 1;
}
/* R8: receive via ff_recvmsg. */
struct iovec io = { buf, sizeof(buf) };
struct msghdr mh;
memset(&mh, 0, sizeof(mh));
mh.msg_iov = &io;
mh.msg_iovlen = 1;
ssize_t n = ff_recvmsg(c, &mh, 0);
if (n == 4 && memcmp(buf, "ping", 4) == 0)
ff_send(c, "pong", 4, 0);
/* R8: half-close the write side via ff_shutdown. */
ff_shutdown(c, SHUT_WR);
ff_close(c);
return (n == 4) ? 0 : 1;
}
static int
do_selftest(void)
{
struct sockaddr_in sa;
int on = 1;
int lfd, rc;
pid_t pid;
/* Enable kernel-stack coexistence for this process (in a real deployment
* this comes from config.ini [stack] kernel_coexist=1 via ff_init). */
ff_global_cfg.stack.kernel_coexist = 1;
lfd = ksock();
if (lfd < 0)
return 1;
ff_setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
fill_loopback(&sa, KPORT);
if (ff_bind(lfd, (struct linux_sockaddr *)&sa, sizeof(sa)) < 0) {
perror("ff_bind");
ff_close(lfd);
return 1;
}
if (ff_listen(lfd, 8) < 0) {
perror("ff_listen");
ff_close(lfd);
return 1;
}
pid = fork();
if (pid == 0) {
/* child: give the parent a moment to reach accept() */
usleep(100 * 1000);
_exit(run_client(KPORT));
}
rc = run_server_once(lfd);
ff_close(lfd);
int cstatus = 1;
if (pid > 0) {
int st;
waitpid(pid, &st, 0);
if (WIFEXITED(st))
cstatus = WEXITSTATUS(st);
}
if (rc == 0 && cstatus == 0) {
printf("COEXIST SELFTEST PASS: native ff_socket(SOCK_KERNEL) "
"kernel-stack server+client over loopback\n");
return 0;
}
printf("COEXIST SELFTEST FAIL: server_rc=%d client_rc=%d\n", rc, cstatus);
return 1;
}
/* Preset HTTP/1.1 keep-alive response, fixed 15-byte body. */
static char http_resp[] =
"HTTP/1.1 200 OK\r\n"
"Server: F-Stack-kernel-coexist\r\n"
"Content-Type: text/plain\r\n"
"Content-Length: 15\r\n"
"Connection: keep-alive\r\n"
"\r\n"
"kernel-coexist\n";
static int
set_nonblock(int fd)
{
int fl = ff_fcntl(fd, F_GETFL, 0);
if (fl < 0)
return -1;
return ff_fcntl(fd, F_SETFL, fl | O_NONBLOCK);
}
/* PERF-3 kernel-side bench: SOCK_KERNEL listener + host-epoll event loop,
* HTTP keep-alive returning the preset body. No ff_init / EAL. */
static int
do_bench(int port)
{
struct sockaddr_in sa;
struct epoll_event ev, evs[512];
int on = 1;
int lfd, epfd, rlfd, i;
ff_global_cfg.stack.kernel_coexist = 1;
lfd = ksock();
if (lfd < 0)
return 1;
ff_setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = htonl(INADDR_ANY);
sa.sin_port = htons((unsigned short)port);
if (ff_bind(lfd, (struct linux_sockaddr *)&sa, sizeof(sa)) < 0) {
perror("ff_bind");
ff_close(lfd);
return 1;
}
if (ff_listen(lfd, 1024) < 0) {
perror("ff_listen");
ff_close(lfd);
return 1;
}
set_nonblock(lfd);
epfd = ff_host_epoll_create1(0);
if (epfd < 0) {
perror("ff_host_epoll_create1");
ff_close(lfd);
return 1;
}
rlfd = ff_kernel_fd_real(lfd);
memset(&ev, 0, sizeof(ev));
ev.events = EPOLLIN;
ev.data.fd = rlfd;
ff_host_epoll_ctl(epfd, EPOLL_CTL_ADD, rlfd, &ev);
printf("COEXIST BENCH: kernel-stack HTTP keep-alive server on 0.0.0.0:%d\n", port);
fflush(stdout);
for (;;) {
int n = ff_host_epoll_wait(epfd, evs, 512, -1);
if (n < 0) {
if (errno == EINTR)
continue;
perror("ff_host_epoll_wait");
break;
}
for (i = 0; i < n; i++) {
int rfd = evs[i].data.fd;
if (rfd == rlfd) {
for (;;) {
int c = ff_accept(lfd, NULL, NULL);
if (c < 0)
break;
set_nonblock(c);
memset(&ev, 0, sizeof(ev));
ev.events = EPOLLIN;
ev.data.fd = ff_kernel_fd_real(c);
ff_host_epoll_ctl(epfd, EPOLL_CTL_ADD, ev.data.fd, &ev);
}
} else if (evs[i].events & (EPOLLHUP | EPOLLERR)) {
ff_host_epoll_ctl(epfd, EPOLL_CTL_DEL, rfd, NULL);
ff_close(ff_kernel_fd_encode(rfd));
} else {
char buf[2048];
int cfd = ff_kernel_fd_encode(rfd);
ssize_t r = ff_recv(cfd, buf, sizeof(buf), 0);
if (r > 0) {
ff_send(cfd, http_resp, sizeof(http_resp) - 1, 0);
} else if (r == 0 || (errno != EAGAIN && errno != EWOULDBLOCK)) {
ff_host_epoll_ctl(epfd, EPOLL_CTL_DEL, rfd, NULL);
ff_close(cfd);
}
}
}
}
ff_close(lfd);
return 0;
}
/*
* Dualstack demo (needs ff_init + a working DPDK data plane). A single plain
* listen socket (no marker) is, with [stack] kernel_coexist=1 and the lib built
* with FF_KERNEL_COEXIST, served on BOTH the F-Stack user-space stack (via the
* DPDK NIC) and the host Linux kernel stack (reachable via 127.0.0.1) from the
* same ff_epoll loop. Port comes from $FF_DUALSTACK_PORT.
*/
static int g_dual_lfd;
static int g_dual_epfd;
static int
dual_loop(void *arg)
{
struct epoll_event evs[512];
int n, i;
(void)arg;
n = ff_epoll_wait(g_dual_epfd, evs, 512, 0);
for (i = 0; i < n; i++) {
if (evs[i].data.fd == g_dual_lfd) {
for (;;) {
int c = ff_accept(g_dual_lfd, NULL, NULL);
struct epoll_event ce;
if (c < 0)
break;
set_nonblock(c);
memset(&ce, 0, sizeof(ce));
ce.events = EPOLLIN;
ce.data.fd = c;
ff_epoll_ctl(g_dual_epfd, EPOLL_CTL_ADD, c, &ce);
}
} else {
int cfd = evs[i].data.fd;
char buf[2048];
ssize_t r = ff_recv(cfd, buf, sizeof(buf), 0);
if (r > 0) {
ff_send(cfd, http_resp, sizeof(http_resp) - 1, 0);
} else if (r == 0 || (errno != EAGAIN && errno != EWOULDBLOCK)) {
ff_epoll_ctl(g_dual_epfd, EPOLL_CTL_DEL, cfd, NULL);
ff_close(cfd);
}
}
}
return 0;
}
static int
do_dualstack(int argc, char **argv, int port)
{
struct sockaddr_in sa;
struct epoll_event ev;
int on = 1;
ff_init(argc, argv);
g_dual_lfd = ff_socket(AF_INET, SOCK_STREAM, 0); /* no marker -> dual stack */
if (g_dual_lfd < 0) {
perror("ff_socket");
return 1;
}
ff_setsockopt(g_dual_lfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
set_nonblock(g_dual_lfd);
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = htonl(INADDR_ANY);
sa.sin_port = htons((unsigned short)port);
if (ff_bind(g_dual_lfd, (struct linux_sockaddr *)&sa, sizeof(sa)) < 0) {
perror("ff_bind");
return 1;
}
if (ff_listen(g_dual_lfd, 1024) < 0) {
perror("ff_listen");
return 1;
}
g_dual_epfd = ff_epoll_create(1);
memset(&ev, 0, sizeof(ev));
ev.events = EPOLLIN;
ev.data.fd = g_dual_lfd;
ff_epoll_ctl(g_dual_epfd, EPOLL_CTL_ADD, g_dual_lfd, &ev);
printf("COEXIST DUALSTACK: single listen on :%d served by F-Stack + kernel\n", port);
fflush(stdout);
ff_run(dual_loop, NULL);
return 0;
}
int
main(int argc, char **argv)
{
char *p;
if (argc >= 3 && strcmp(argv[1], "bench") == 0)
return do_bench(atoi(argv[2]));
p = getenv("FF_DUALSTACK_PORT");
if (p != NULL)
return do_dualstack(argc, argv, atoi(p));
return do_selftest();
}