forked from alibaba/PhotonLibOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel_socket.cpp
More file actions
1126 lines (970 loc) · 36.5 KB
/
Copy pathkernel_socket.cpp
File metadata and controls
1126 lines (970 loc) · 36.5 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
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2022 The Photon Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "socket.h"
#include <inttypes.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#ifdef __linux__
#include <sys/epoll.h>
#include <sys/sendfile.h>
#endif
#include <unistd.h>
#include <memory>
#include <unordered_map>
#include <photon/common/alog.h>
#include <photon/common/iovector.h>
#include <photon/io/fd-events.h>
#include <photon/thread/thread11.h>
#include <photon/thread/list.h>
#include <photon/thread/timer.h>
#include <photon/common/estring.h>
#include <photon/common/utility.h>
#include <photon/common/event-loop.h>
#include <photon/net/basic_socket.h>
#include <photon/net/utils.h>
#ifdef PHOTON_URING
#include <photon/io/iouring-wrapper.h>
#endif
#ifdef ENABLE_FSTACK_DPDK
#include <photon/io/fstack-dpdk.h>
#endif
#include "base_socket.h"
#include "../io/events_map.h"
#ifndef SO_ZEROCOPY
#define SO_ZEROCOPY 60
#endif
#ifndef AF_SMC
#define AF_SMC 43
#endif
namespace photon {
namespace net {
static int socket(int family, int protocol = 0,
bool nonblocking = true, bool nodelay = true) {
int fd = ::socket(family, SOCK_STREAM, protocol);
if (fd < 0)
LOG_ERRNO_RETURN(0, -1, "failed to create socket fd");
if (nonblocking)
set_fd_nonblocking(fd);
if (nodelay) {
int v = 1;
if (::setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v)) < 0)
LOG_WARN("failed to set TCP_NODELAY ", ERRNO());
}
return fd;
}
template<typename Stream> inline
Stream* new_stream(int family, int protocol = 0, bool nonblocking = true) {
int fd = socket(family, protocol, nonblocking, family != AF_UNIX);
return (fd < 0) ? nullptr : new Stream(fd);
}
class KernelSocketStream : public SocketStreamBase {
public:
using ISocketStream::setsockopt;
using ISocketStream::getsockopt;
int fd = -1;
explicit KernelSocketStream(int fd) : fd(fd) {}
~KernelSocketStream() override {
if (fd < 0) return;
shutdown(ShutdownHow::ReadWrite);
close();
}
ssize_t read(void* buf, size_t count) override {
Timeout timeout(m_timeout);
return DOIO_LOOP(do_recv(fd, buf, count, 0, timeout), BufStep(buf, count));
}
ssize_t readv(const iovec* iov, int iovcnt) override {
SmartCloneIOV<8> clone(iov, iovcnt);
iovector_view view(clone.ptr, iovcnt);
Timeout timeout(m_timeout);
return DOIO_LOOP(do_recvmsg(fd, tmp_msg_hdr(view), 0, timeout), BufStepV(view));
}
ssize_t write(const void* buf, size_t count) override {
Timeout timeout(m_timeout);
return DOIO_LOOP(do_send(fd, buf, count, MSG_NOSIGNAL, timeout), BufStep((void*&)buf, count));
}
ssize_t writev(const iovec* iov, int iovcnt) override {
SmartCloneIOV<8> clone(iov, iovcnt);
iovector_view view(clone.ptr, iovcnt);
Timeout timeout(m_timeout);
return DOIO_LOOP(do_sendmsg(fd, tmp_msg_hdr(view), MSG_NOSIGNAL, timeout), BufStepV(view));
}
ssize_t recv(void* buf, size_t count, int flags = 0) override {
return do_recv(fd, buf, count, flags, m_timeout);
}
ssize_t recv(const iovec* iov, int iovcnt, int flags = 0) override {
return do_recvmsg(fd, tmp_msg_hdr(iov, iovcnt), flags, m_timeout);
}
ssize_t send(const void* buf, size_t count, int flags = 0) override {
return do_send(fd, buf, count, flags | MSG_NOSIGNAL, m_timeout);
}
ssize_t send(const iovec* iov, int iovcnt, int flags = 0) override {
return do_sendmsg(fd, tmp_msg_hdr(iov, iovcnt), flags | MSG_NOSIGNAL, m_timeout);
}
ssize_t sendfile(int in_fd, off_t offset, size_t count) override {
return net::sendfile_n(fd, in_fd, &offset, count);
}
int shutdown(ShutdownHow how) final {
// shutdown how defined as 0 for RD, 1 for WR and 2 for RDWR
// in sys/socket.h, cast ShutdownHow into int just fits
return ::shutdown(fd, static_cast<int>(how));
}
Object* get_underlay_object(uint64_t recursion = 0) override {
return (Object*) (uint64_t) fd;
}
int close() final {
if (fd < 0) return 0;
get_vcpu()->master_event_engine->wait_for_fd(fd, 0, -1UL);
auto ret = ::close(fd);
fd = -1;
return ret;
}
int getsockname(EndPoint& addr) override {
return get_socket_name(fd, addr);
}
int getpeername(EndPoint& addr) override {
return get_peer_name(fd, addr);
}
int getsockname(char* path, size_t count) override {
return get_socket_name(fd, path, count);
}
int getpeername(char* path, size_t count) override {
return get_peer_name(fd, path, count);
}
int setsockopt(int level, int option_name, const void* option_value, socklen_t option_len) override {
return ::setsockopt(fd, level, option_name, option_value, option_len);
}
int getsockopt(int level, int option_name, void* option_value, socklen_t* option_len) override {
return ::getsockopt(fd, level, option_name, option_value, option_len);
}
uint64_t timeout() const override { return m_timeout; }
void timeout(uint64_t tm) override { m_timeout = tm; }
protected:
uint64_t m_timeout = -1;
virtual ssize_t do_send(int sockfd, const void* buf, size_t count, int flags, Timeout timeout) {
return photon::net::send(sockfd, buf, count, flags, timeout);
}
virtual ssize_t do_sendmsg(int sockfd, const struct msghdr* message, int flags, Timeout timeout) {
return photon::net::sendmsg(sockfd, message, flags, timeout);
}
virtual ssize_t do_recv(int sockfd, void* buf, size_t count, int flags, Timeout timeout) {
return photon::net::recv(sockfd, buf, count, flags, timeout);
}
virtual ssize_t do_recvmsg(int sockfd, struct msghdr* message, int flags, Timeout timeout) {
return photon::net::recvmsg(sockfd, message, flags, timeout);
}
struct tmp_msg_hdr : public ::msghdr {
tmp_msg_hdr(const iovec* iov, int iovcnt) : ::msghdr{} {
this->msg_iov = (iovec*) iov;
this->msg_iovlen = iovcnt;
}
explicit tmp_msg_hdr(iovector_view& view) :
tmp_msg_hdr(view.iov, view.iovcnt) { }
operator ::msghdr*() { return this; }
};
};
class KernelSocketClient : public SocketClientBase {
public:
std::vector<sockaddr_storage> bind_ss;
KernelSocketClient(IPAddr* bind_ip = nullptr, int bind_ip_n = 0) {
for (int i = 0; i < bind_ip_n; i++)
bind_ss.emplace_back(EndPoint(bind_ip[i], 0));
}
ISocketStream* connect(const char* path, size_t count) override {
struct sockaddr_un addr_un;
if (fill_uds_path(addr_un, path, count) != 0) return nullptr;
sockaddr_storage r(addr_un);
return do_connect(&r);
}
ISocketStream* connect(const EndPoint& remote, const EndPoint* local) override {
sockaddr_storage r(remote);
if (likely(!local || local->is_ipv4() != remote.is_ipv4())) {
if (bind_ss.size() > 0) {
int rid = rand() % bind_ss.size();
return do_connect(&r, &bind_ss[rid]);
}
return do_connect(&r);
}
sockaddr_storage l(*local);
return do_connect(&r, &l);
}
virtual KernelSocketStream* create_stream(int socket_family) {
return new_stream<KernelSocketStream>(socket_family);
}
virtual int fd_connect(int fd, const sockaddr* remote, socklen_t addrlen) {
return net::connect(fd, remote, addrlen, m_timeout);
}
ISocketStream* do_connect(const sockaddr_storage* remote,
const sockaddr_storage* local = nullptr) {
auto stream = create_stream(remote->store.ss_family);
auto deleter = [&](KernelSocketStream*) {
auto errno_backup = errno;
delete stream;
errno = errno_backup;
};
std::unique_ptr<KernelSocketStream, decltype(deleter)> ptr(stream, deleter);
if (!ptr || ptr->fd < 0) {
LOG_ERROR_RETURN(0, nullptr, "Failed to create socket fd");
}
if (m_opts.setsockopt(ptr->fd) < 0) {
return nullptr;
}
ptr->timeout(m_timeout);
if (local != nullptr) {
if (::bind(ptr->fd, local->get_sockaddr(), local->get_socklen()) != 0) {
LOG_ERRNO_RETURN(0, nullptr, "fail to bind socket");
}
}
auto ret = fd_connect(ptr->fd, remote->get_sockaddr(), remote->get_socklen());
if (ret < 0) {
LOG_ERRNO_RETURN(0, nullptr, "Failed to connect to ", remote->to_endpoint());
}
return ptr.release();
}
};
class KernelSocketServer : public SocketServerBase {
public:
using ISocketServer::setsockopt;
using ISocketServer::getsockopt;
explicit KernelSocketServer(bool autoremove = false) : m_autoremove(autoremove) { }
int init() { return 0; }
~KernelSocketServer() {
terminate();
if (m_listen_fd < 0) {
return;
}
::shutdown(m_listen_fd, static_cast<int>(ShutdownHow::ReadWrite));
if (m_autoremove) {
char filename[PATH_MAX];
if (0 == get_socket_name(m_listen_fd, filename, PATH_MAX)) {
unlink(filename);
}
}
::close(m_listen_fd);
m_listen_fd = -1;
}
int start_loop(bool block) override {
if (workth) LOG_ERROR_RETURN(EALREADY, -1, "Already listening");
m_block_serv = block;
if (block) return accept_loop();
auto loop = &KernelSocketServer::accept_loop;
auto th = thread_create((thread_entry&)loop, this);
thread_enable_join(th);
thread_yield_to(th);
return 0;
}
void terminate() final {
if (!workth) return;
auto th = workth;
workth = nullptr;
if (waiting) {
thread_interrupt(th);
if (!m_block_serv)
thread_join((join_handle*)th);
}
}
ISocketServer* set_handler(Handler handler) override {
m_handler = handler;
return this;
}
int bind(const EndPoint& ep) override {
auto s = sockaddr_storage(ep);
if (m_listen_fd < 0) {
m_listen_fd = socket(s.get_sockaddr()->sa_family, 0, m_nonblocking, true);
if (m_listen_fd < 0) return -1;
}
if (m_opts.setsockopt(m_listen_fd) != 0) {
return -1;
}
int ret = ::bind(m_listen_fd, s.get_sockaddr(), s.get_socklen());
if (ret < 0)
LOG_ERRNO_RETURN(0, ret, "failed to bind to ", s.to_endpoint());
return 0;
}
int bind(const char* path, size_t count) override {
if (m_autoremove && is_socket(path)) {
if (unlink(path) < 0)
LOG_ERRNO_RETURN(0, -1, VALUE(path));
}
if (m_listen_fd < 0) {
m_listen_fd = socket(AF_UNIX, 0, true, true);
if (m_listen_fd < 0)
LOG_ERRNO_RETURN(0, m_listen_fd, "failed to create UNIX domain socket at ", ALogString(path, count));
}
if (m_opts.setsockopt(m_listen_fd) != 0) {
return -1;
}
struct sockaddr_un addr_un;
int ret = fill_uds_path(addr_un, path, count);
if (ret < 0) return -1;
ret = ::bind(m_listen_fd, (struct sockaddr*) &addr_un, sizeof(addr_un));
if (ret < 0)
LOG_ERRNO_RETURN(0, ret, "failed to bind to '`' ", ALogString(path, count))
return 0;
}
int listen(int backlog) override {
return ::listen(m_listen_fd, backlog);
}
ISocketStream* accept(EndPoint* remote_endpoint = nullptr) override {
int cfd = remote_endpoint ? do_accept(*remote_endpoint) : do_accept();
if (cfd < 0) {
return nullptr;
}
if (m_opts.setsockopt(cfd) != 0) {
return nullptr;
}
return create_stream(cfd);
}
Object* get_underlay_object(uint64_t recursion = 0) override {
return (Object*) (uint64_t) m_listen_fd;
}
int getsockname(EndPoint& addr) override {
return get_socket_name(m_listen_fd, addr);
}
int getpeername(EndPoint& addr) override {
return get_peer_name(m_listen_fd, addr);
}
int getsockname(char* path, size_t count) override {
return get_socket_name(m_listen_fd, path, count);
}
int getpeername(char* path, size_t count) override {
return get_peer_name(m_listen_fd, path, count);
}
int setsockopt(int level, int option_name, const void* option_value, socklen_t option_len) override {
if (m_listen_fd >= 0 && ::setsockopt(m_listen_fd, level, option_name, option_value, option_len) != 0)
LOG_ERRNO_RETURN(0, -1, "failed to setsockopt for fd `", m_listen_fd);
return m_opts.put_opt(level, option_name, option_value, option_len);
}
int getsockopt(int level, int option_name, void* option_value, socklen_t* option_len) override {
if (m_listen_fd >= 0 && ::getsockopt(m_listen_fd, level, option_name, option_value, option_len) == 0)
return 0;
return m_opts.get_opt(level, option_name, option_value, option_len);
}
protected:
bool m_autoremove;
bool m_nonblocking = true;
bool m_block_serv = false;
bool waiting = false;
Handler m_handler;
photon::thread* workth = nullptr;
int m_listen_fd = -1;
virtual KernelSocketStream* create_stream(int fd) {
return new KernelSocketStream(fd);
}
virtual int do_accept(struct sockaddr *addr, socklen_t *addrlen) {
return net::accept(m_listen_fd, addr, addrlen);
}
int do_accept() { return do_accept(nullptr, nullptr); }
int do_accept(EndPoint& remote_endpoint) {
sockaddr_storage s;
socklen_t len = s.get_max_socklen();
int cfd = do_accept(s.get_sockaddr(), &len);
if (cfd < 0 || len > s.get_max_socklen())
return -1;
remote_endpoint = s.to_endpoint();
return cfd;
}
static bool is_socket(const char* path) {
struct stat statbuf;
return (0 == stat(path, &statbuf)) ? S_ISSOCK(statbuf.st_mode) : false;
}
int accept_loop() {
if (workth) LOG_ERROR_RETURN(EALREADY, -1, "Already listening");
workth = photon::CURRENT;
DEFER(workth = nullptr);
while (workth) {
waiting = true;
auto connection = accept();
waiting = false;
if (!workth) return 0;
if (connection) {
connection->timeout(m_timeout);
photon::thread_create11(&KernelSocketServer::handler, m_handler, connection);
} else {
LOG_WARN("KernelSocketServer: failed to accept new connections: `", ERRNO());
photon::thread_usleep(1000);
}
}
return 0;
}
static void handler(Handler m_handler, ISocketStream* sess) {
m_handler(sess);
delete sess;
}
};
class SMCSocketClient : public KernelSocketClient {
public:
virtual KernelSocketStream* create_stream(int socket_family) {
int ver = (socket_family == AF_INET6);
return new_stream<KernelSocketStream>(AF_SMC, ver);
}
};
class SMCSocketServer : public KernelSocketServer {
public:
int bind(const EndPoint& ep) override {
auto s = sockaddr_storage(ep);
if (m_listen_fd < 0) {
int ver = (s.get_sockaddr()->sa_family == AF_INET6);
m_listen_fd = socket(AF_SMC, ver);
if (m_listen_fd < 0) return -1;
}
int ret = ::bind(m_listen_fd, s.get_sockaddr(), s.get_socklen());
if (ret < 0)
LOG_ERRNO_RETURN(0, ret, "failed to bind to ", s.to_endpoint());
return 0;
}
UNIMPLEMENTED(int bind(const char* path, size_t count));
};
#ifdef __linux__
class ZeroCopySocketStream : public KernelSocketStream {
public:
explicit ZeroCopySocketStream(int fd) : KernelSocketStream(fd) {
setsockopt<int>(SOL_SOCKET, SO_ZEROCOPY, 1);
}
protected:
uint32_t m_num_calls = 0;
ssize_t do_send(int sockfd, const void* buf, size_t count, int flags, Timeout timeout) override {
struct iovec iov{const_cast<void*>(buf), count};
return do_sendmsg(sockfd, tmp_msg_hdr(&iov, 1), flags, timeout);
}
ssize_t do_sendmsg(int sockfd, const struct msghdr* message, int flags, Timeout timeout) override {
ssize_t n = photon::net::sendmsg(sockfd, message, flags | ZEROCOPY_FLAG, timeout);
m_num_calls++;
auto ret = zerocopy_confirm(sockfd, m_num_calls - 1, timeout);
if (ret < 0)
return ret;
return n;
}
};
class ZeroCopySocketServer : public KernelSocketServer {
public:
using KernelSocketServer::KernelSocketServer;
int init() {
if (!net::zerocopy_available()) {
LOG_ERROR_RETURN(0, -1, "zerocopy not available");
}
// if (KernelSocketServer::init() != 0) {
// return -1;
// }
return 0;
}
protected:
KernelSocketStream* create_stream(int fd) override {
return new ZeroCopySocketStream(fd);
}
};
class ZeroCopySocketClient : public KernelSocketClient {
public:
using KernelSocketClient::KernelSocketClient;
protected:
KernelSocketStream* create_stream(int socket_family) override {
return new_stream<ZeroCopySocketStream>(socket_family);
}
};
#ifdef PHOTON_URING
class IouringSocketStream : public KernelSocketStream {
public:
using KernelSocketStream::KernelSocketStream;
ssize_t do_send(int sockfd, const void* buf, size_t count, int flags, Timeout timeout) override {
if (flags & ZEROCOPY_FLAG)
return photon::iouring_send_zc(sockfd, buf, count, flags | MSG_WAITALL, timeout);
else
return photon::iouring_send(sockfd, buf, count, flags, timeout);
}
ssize_t do_sendmsg(int sockfd, const struct msghdr* message, int flags, Timeout timeout) override {
if (flags & ZEROCOPY_FLAG)
return photon::iouring_sendmsg_zc(sockfd, message, flags | MSG_WAITALL, timeout);
else
return photon::iouring_sendmsg(sockfd, message, flags, timeout);
}
ssize_t do_recv(int sockfd, void* buf, size_t count, int flags, Timeout timeout) override {
return photon::iouring_recv(sockfd, buf, count, flags, timeout);
}
ssize_t do_recvmsg(int sockfd, struct msghdr* message, int flags, Timeout timeout) override {
return photon::iouring_recvmsg(sockfd, message, flags, timeout);
}
};
class IouringSocketClient : public KernelSocketClient {
public:
using KernelSocketClient::KernelSocketClient;
KernelSocketStream* create_stream(int socket_family) override {
return new_stream<IouringSocketStream>(socket_family, 0, false);
}
int fd_connect(int fd, const sockaddr* remote, socklen_t addrlen) override {
return photon::iouring_connect(fd, remote, addrlen, m_timeout);
}
};
class IouringSocketServer : public KernelSocketServer {
public:
using KernelSocketServer::KernelSocketServer;
int init() {
m_nonblocking = false;
return 0;
}
KernelSocketStream* create_stream(int fd) override {
return new IouringSocketStream(fd);
}
int do_accept(struct sockaddr* addr, socklen_t* addrlen) override {
return photon::iouring_accept(m_listen_fd, addr, addrlen, -1);
}
};
class IouringFixedFileSocketStream : public IouringSocketStream {
public:
explicit IouringFixedFileSocketStream(int fd) :
IouringSocketStream(fd) {
if (fd >= 0)
photon::iouring_register_files(fd);
}
~IouringFixedFileSocketStream() override {
if (fd >= 0)
photon::iouring_unregister_files(fd);
}
private:
ssize_t do_send(int sockfd, const void* buf, size_t count, int flags, Timeout timeout) override {
if (flags & ZEROCOPY_FLAG)
return photon::iouring_send_zc(sockfd, buf, count, IouringFixedFileFlag | flags | MSG_WAITALL, timeout);
else
return photon::iouring_send(sockfd, buf, count, IouringFixedFileFlag | flags, timeout);
}
ssize_t do_sendmsg(int sockfd, const struct msghdr* message, int flags, Timeout timeout) override {
if (flags & ZEROCOPY_FLAG)
return photon::iouring_sendmsg_zc(sockfd, message, IouringFixedFileFlag | flags | MSG_WAITALL, timeout);
else
return photon::iouring_sendmsg(sockfd, message, IouringFixedFileFlag | flags, timeout);
}
ssize_t do_recv(int sockfd, void* buf, size_t count, int flags, Timeout timeout) override {
return photon::iouring_recv(sockfd, buf, count, IouringFixedFileFlag | flags, timeout);
}
ssize_t do_recvmsg(int sockfd, struct msghdr* message, int flags, Timeout timeout) override {
return photon::iouring_recvmsg(sockfd, message, IouringFixedFileFlag | flags, timeout);
}
};
class IouringFixedFileSocketClient : public IouringSocketClient {
protected:
using IouringSocketClient::IouringSocketClient;
KernelSocketStream* create_stream(int socket_family) override {
return new_stream<IouringFixedFileSocketStream>(socket_family, 0, false);
}
};
class IouringFixedFileSocketServer : public IouringSocketServer {
protected:
using IouringSocketServer::IouringSocketServer;
KernelSocketStream* create_stream(int fd) override {
return new IouringFixedFileSocketStream(fd);
}
};
#endif // PHOTON_URING
#ifdef ENABLE_FSTACK_DPDK
class FstackDpdkSocketStream : public KernelSocketStream {
public:
using KernelSocketStream::KernelSocketStream;
~FstackDpdkSocketStream() override {
if (fd < 0) return;
fstack_shutdown(fd, (int) ShutdownHow::ReadWrite);
fstack_close(fd);
fd = -1;
}
protected:
ssize_t do_send(int sockfd, const void* buf, size_t count, int flags, Timeout timeout) override {
return fstack_send(sockfd, buf, count, flags, timeout);
}
ssize_t do_sendmsg(int sockfd, const struct msghdr* message, int flags, Timeout timeout) override {
return fstack_sendmsg(sockfd, message, flags, timeout);
}
ssize_t do_recv(int sockfd, void* buf, size_t count, int flags, Timeout timeout) override {
return fstack_recv(sockfd, buf, count, flags, timeout);
}
ssize_t do_recvmsg(int sockfd, struct msghdr* message, int flags, Timeout timeout) override {
return fstack_recvmsg(sockfd, message, flags, timeout);
}
int setsockopt(int level, int option_name, const void* option_value, socklen_t option_len) override {
return fstack_setsockopt(fd, level, option_name, option_value, option_len);
}
int getsockopt(int level, int option_name, void* option_value, socklen_t* option_len) override {
return fstack_getsockopt(fd, level, option_name, option_value, option_len);
}
};
class FstackDpdkSocketClient : public KernelSocketClient {
protected:
using KernelSocketClient::KernelSocketClient;
KernelSocketStream* create_stream(int socket_family) override {
int fd = fstack_socket(socket_family, SOCK_STREAM, 0);
if (fd < 0) return nullptr;
return new FstackDpdkSocketStream(fd);
}
int fd_connect(int fd, const sockaddr* remote, socklen_t addrlen) override {
return fstack_connect(fd, remote, addrlen, m_timeout);
}
};
class FstackDpdkSocketServer : public KernelSocketServer {
public:
using KernelSocketServer::KernelSocketServer;
int bind(const EndPoint& ep) override {
if (m_listen_fd >= 0)
LOG_ERROR_RETURN(EALREADY, -1, "already bound");
auto s = sockaddr_storage(ep);
m_listen_fd = fstack_socket(s.get_sockaddr()->sa_family, SOCK_STREAM, 0); // already non-blocking and no-delay
if (m_listen_fd < 0) {
LOG_ERRNO_RETURN(0, -1, "fail to setup DPDK listen fd");
}
int ret = fstack_bind(m_listen_fd, s.get_sockaddr(), s.get_socklen());
if (ret < 0)
LOG_ERRNO_RETURN(0, ret, "failed to bind to ", s.to_endpoint());
return 0;
}
int bind(const char* path, size_t count) override {
LOG_ERRNO_RETURN(ENOSYS, -1, "Not implemented");
}
int listen(int backlog) override {
return fstack_listen(m_listen_fd, backlog);
}
int setsockopt(int level, int option_name, const void* option_value, socklen_t option_len) override {
if (fstack_setsockopt(m_listen_fd, level, option_name, option_value, option_len) != 0) {
LOG_ERRNO_RETURN(EINVAL, -1, "failed to setsockopt");
}
return m_opts.put_opt(level, option_name, option_value, option_len);
}
int getsockopt(int level, int option_name, void* option_value, socklen_t* option_len) override {
if (fstack_getsockopt(m_listen_fd, level, option_name, option_value, option_len) == 0)
return 0;
return m_opts.get_opt(level, option_name, option_value, option_len);
}
protected:
KernelSocketStream* create_stream(int fd) override {
return new FstackDpdkSocketStream(fd);
}
int do_accept(struct sockaddr* addr, socklen_t* addrlen) override {
return fstack_accept(m_listen_fd, addr, addrlen, m_timeout);
}
private:
class FstackSockOptBuffer : public SockOptBuffer {
public:
int setsockopt(int fd) override {
for (auto& opt : *this) {
if (fstack_setsockopt(fd, opt.level, opt.opt_name, opt.opt_val, opt.opt_len) != 0) {
LOG_ERRNO_RETURN(EINVAL, -1, "Failed to setsockopt ",
VALUE(opt.level), VALUE(opt.opt_name), VALUE(opt.opt_val));
}
}
return 0;
}
};
FstackSockOptBuffer m_opts;
};
#endif // ENABLE_FSTACK_DPDK
/* ET Socket - Start */
constexpr static photon::EventsMap<
EVUnderlay<EVENT_READ, EVENT_WRITE, EVENT_ERROR>,
EVKey<EPOLLIN | EPOLLRDHUP, EPOLLOUT, EPOLLERR>>
et_evmap;
struct NotifyContext {
photon::thread* waiter[3] = {nullptr, nullptr, nullptr};
int wait_for_state(uint32_t event, uint64_t timeout) {
waiter[event >> 1] = photon::CURRENT;
auto ret = photon::thread_usleep(timeout);
waiter[event >> 1] = nullptr;
auto eno = &errno;
if (ret == 0) {
*eno = ETIMEDOUT;
return -1;
}
if (*eno != EOK) {
LOG_DEBUG("failed when wait for events: ", ERRNO(* eno));
return -1;
}
return 0;
}
void on_event(int ep_event) {
auto ev = et_evmap.translate_bitwisely(ep_event);
for (int i = 0; i < 3; i++) {
if (waiter[i] && (ev & (1 << i))) {
photon::thread_interrupt(waiter[i], EOK);
}
}
}
int wait_for_readable(uint64_t timeout) {
return wait_for_state(EVENT_READ, timeout);
}
int wait_for_writable(uint64_t timeout) {
return wait_for_state(EVENT_WRITE, timeout);
}
};
struct ETPoller {
int epfd = 0;
EventLoop* loop = nullptr;
int init() {
epfd = epoll_create(1);
loop = new_event_loop({this, &ETPoller::waiter},
{this, &ETPoller::notify});
loop->async_run();
return epfd;
}
int fini() {
loop->stop();
if (epfd > 0) {
close(epfd);
epfd = 0;
}
delete loop;
loop = nullptr;
return 0;
}
int waiter(EventLoop*) {
auto ret = photon::wait_for_fd_readable(epfd, 1 * 1000UL);
// LOG_DEBUG("WAITER ",VALUE(ret));
auto err = errno;
if (ret == 0)
return 1;
else if (err == ETIMEDOUT)
return 0;
else
return -1;
}
int notify(EventLoop*) {
struct epoll_event evs[1024];
auto ret = epoll_wait(epfd, evs, 1024, 0);
for (int i = 0; i < ret; i++) {
auto c = (NotifyContext*) evs[i].data.ptr;
c->on_event(evs[i].events);
}
return 0;
}
int register_notifier(int fd, NotifyContext* context) {
struct epoll_event ev;
ev.events = EPOLLIN | EPOLLOUT | EPOLLRDHUP | EPOLLET;
ev.data.ptr = context;
return epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev);
}
int unregister_notifier(int fd) {
return epoll_ctl(epfd, EPOLL_CTL_DEL, fd, nullptr);
}
};
thread_local ETPoller etpoller;
int et_poller_init() { return etpoller.init(); }
int et_poller_fini() { return etpoller.fini(); }
template<typename IOCB, typename WAITCB>
static __FORCE_INLINE__ ssize_t etdoio(IOCB iocb, WAITCB waitcb) {
while (true) {
ssize_t ret = iocb();
if (ret < 0) {
auto e = errno; // errno is usually a macro that expands to a function call
if (e == EINTR) continue;
if (e == EAGAIN || e == EWOULDBLOCK || e == EINPROGRESS) {
if (waitcb()) // non-zero result means timeout or
// interrupt, need to return
return ret;
continue;
}
}
return ret;
}
}
class ETKernelSocketStream : public KernelSocketStream, public NotifyContext {
public:
explicit ETKernelSocketStream(int fd) : KernelSocketStream(fd) {
if (fd >= 0) etpoller.register_notifier(fd, this);
}
~ETKernelSocketStream() {
if (fd >= 0) etpoller.unregister_notifier(fd);
}
ssize_t sendfile(int in_fd, off_t offset, size_t count) override {
return DOIO_LOOP(do_sendfile(in_fd, offset, count), BufStep(count));
}
private:
ssize_t do_send(int sockfd, const void* buf, size_t count, int flags, Timeout timeout) override {
return etdoio(LAMBDA(::send(sockfd, buf, count, flags)),
LAMBDA_TIMEOUT(wait_for_writable(timeout)));
}
ssize_t do_sendmsg(int sockfd, const struct msghdr* message, int flags, Timeout timeout) override {
return etdoio(LAMBDA(::sendmsg(sockfd, message, flags)),
LAMBDA_TIMEOUT(wait_for_writable(timeout)));
}
ssize_t do_recv(int sockfd, void* buf, size_t count, int flags, Timeout timeout) override {
return etdoio(LAMBDA(::read(sockfd, buf, count)),
LAMBDA_TIMEOUT(wait_for_readable(timeout)));
}
ssize_t do_recvmsg(int sockfd, struct msghdr* message, int flags, Timeout timeout) override {
return etdoio(LAMBDA(::recvmsg(sockfd, message, flags)),
LAMBDA_TIMEOUT(wait_for_readable(timeout)));
}
ssize_t do_sendfile(int in_fd, off_t offset, size_t count) {
auto timeout = m_timeout;
return etdoio(LAMBDA(::sendfile(this->fd, in_fd, &offset, count)),
LAMBDA_TIMEOUT(wait_for_writable(timeout)));
}
};
class ETKernelSocketClient : public KernelSocketClient {
public:
using KernelSocketClient::KernelSocketClient;
protected:
KernelSocketStream* create_stream(int socket_family) override {
return new_stream<ETKernelSocketStream>(socket_family);
}
};
class ETKernelSocketServer : public KernelSocketServer, public NotifyContext {
public:
using KernelSocketServer::KernelSocketServer;
~ETKernelSocketServer() {
if (m_listen_fd >= 0) etpoller.unregister_notifier(m_listen_fd);
}
int bind(const EndPoint& ep) override {
int fd = m_listen_fd;
int ret = KernelSocketServer::bind(ep);
if (fd < 0 && m_listen_fd >= 0)
etpoller.register_notifier(m_listen_fd, this);
return ret;
}
int bind(const char* path, size_t count) override {
int fd = m_listen_fd;
int ret = KernelSocketServer::bind(path, count);
if (fd < 0 && m_listen_fd >= 0)
etpoller.register_notifier(m_listen_fd, this);
return ret;
}
protected:
KernelSocketStream* create_stream(int fd) override {
return new ETKernelSocketStream(fd);
}
int do_accept(struct sockaddr* addr, socklen_t* addrlen) override {
uint64_t timeout = -1;
return (int) etdoio(LAMBDA(::accept4(m_listen_fd, addr, addrlen, SOCK_NONBLOCK)),
LAMBDA_TIMEOUT(wait_for_readable(timeout)));
}
};
#endif // __linux__
/* ET Socket - End */
extern "C" ISocketClient* new_tcp_socket_client(IPAddr* bind_ip, uint32_t bind_ip_n) {
return new KernelSocketClient(bind_ip, bind_ip_n);
}
extern "C" ISocketServer* new_tcp_socket_server() {
return NewObj<KernelSocketServer>()->init();
}