blob: fb736de25672ac56e4fca5c2ab0910801237fe9c [file] [log] [blame]
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001#include "aos/network/sctp_server.h"
2
3#include <arpa/inet.h>
Adam Snaiderbe263512023-05-18 20:40:23 -07004#include <linux/sctp.h>
Austin Schuhe84c3ed2019-12-14 15:29:48 -08005#include <net/if.h>
6#include <netdb.h>
7#include <netinet/in.h>
Austin Schuhe84c3ed2019-12-14 15:29:48 -08008#include <sys/socket.h>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07009
10#include <cstdio>
11#include <cstdlib>
12#include <cstring>
Austin Schuhe84c3ed2019-12-14 15:29:48 -080013#include <memory>
Austin Schuh387b7de2020-03-15 14:28:07 -070014#include <thread>
Austin Schuhe84c3ed2019-12-14 15:29:48 -080015
Philipp Schrader790cb542023-07-05 21:06:52 -070016#include "glog/logging.h"
17
Austin Schuhe84c3ed2019-12-14 15:29:48 -080018#include "aos/network/sctp_lib.h"
19#include "aos/unique_malloc_ptr.h"
Austin Schuhe84c3ed2019-12-14 15:29:48 -080020
21namespace aos {
22namespace message_bridge {
23
Austin Schuh0a0a8272021-12-08 13:19:32 -080024SctpServer::SctpServer(int streams, std::string_view local_host,
25 int local_port) {
26 bool use_ipv6 = Ipv6Enabled();
27 sockaddr_local_ = ResolveSocket(local_host, local_port, use_ipv6);
Austin Schuh387b7de2020-03-15 14:28:07 -070028 while (true) {
Austin Schuh507f7582021-07-31 20:39:55 -070029 sctp_.OpenSocket(sockaddr_local_);
Austin Schuhe84c3ed2019-12-14 15:29:48 -080030
Austin Schuh387b7de2020-03-15 14:28:07 -070031 {
Brian Silverman833dfb62021-11-03 10:47:55 -070032 struct sctp_initmsg initmsg;
33 memset(&initmsg, 0, sizeof(struct sctp_initmsg));
34 initmsg.sinit_num_ostreams = streams;
35 initmsg.sinit_max_instreams = streams;
36 PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_INITMSG, &initmsg,
37 sizeof(struct sctp_initmsg)) == 0);
38 }
39
40 {
Austin Schuh387b7de2020-03-15 14:28:07 -070041 // Turn off the NAGLE algorithm.
42 int on = 1;
Austin Schuh507f7582021-07-31 20:39:55 -070043 PCHECK(setsockopt(fd(), IPPROTO_SCTP, SCTP_NODELAY, &on, sizeof(int)) ==
Austin Schuh387b7de2020-03-15 14:28:07 -070044 0);
45 }
46
Austin Schuhf6ed4522020-12-13 16:40:38 -080047 {
48 int on = 1;
Austin Schuh507f7582021-07-31 20:39:55 -070049 LOG(INFO) << "setsockopt(" << fd()
50 << ", SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int)";
51 PCHECK(setsockopt(fd(), SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int)) == 0);
Austin Schuhf6ed4522020-12-13 16:40:38 -080052 }
53
Austin Schuh387b7de2020-03-15 14:28:07 -070054 // And go!
Austin Schuh507f7582021-07-31 20:39:55 -070055 if (bind(fd(), (struct sockaddr *)&sockaddr_local_,
Austin Schuh387b7de2020-03-15 14:28:07 -070056 sockaddr_local_.ss_family == AF_INET6
57 ? sizeof(struct sockaddr_in6)
58 : sizeof(struct sockaddr_in)) != 0) {
59 PLOG(ERROR) << "Failed to bind, retrying";
Austin Schuh507f7582021-07-31 20:39:55 -070060 close(fd());
Austin Schuh387b7de2020-03-15 14:28:07 -070061 std::this_thread::sleep_for(std::chrono::seconds(5));
62 continue;
63 }
Austin Schuh507f7582021-07-31 20:39:55 -070064 LOG(INFO) << "bind(" << fd() << ", " << Address(sockaddr_local_) << ")";
Austin Schuh387b7de2020-03-15 14:28:07 -070065
Austin Schuh507f7582021-07-31 20:39:55 -070066 PCHECK(listen(fd(), 100) == 0);
Austin Schuh387b7de2020-03-15 14:28:07 -070067
Austin Schuh89e1e9c2023-05-15 14:38:44 -070068 SetMaxReadSize(1000);
69 SetMaxWriteSize(1000);
Austin Schuh387b7de2020-03-15 14:28:07 -070070 break;
Austin Schuhe84c3ed2019-12-14 15:29:48 -080071 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080072}
73
Sarah Newman1e1b0492023-01-12 14:57:31 -080074void SctpServer::SetPriorityScheduler([[maybe_unused]] sctp_assoc_t assoc_id) {
75// Kernel 4.9 does not have SCTP_SS_PRIO
76#ifdef SCTP_SS_PRIO
Austin Schuhe84c3ed2019-12-14 15:29:48 -080077 struct sctp_assoc_value scheduler;
78 memset(&scheduler, 0, sizeof(scheduler));
79 scheduler.assoc_id = assoc_id;
80 scheduler.assoc_value = SCTP_SS_PRIO;
81 if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER, &scheduler,
82 sizeof(scheduler)) != 0) {
Sarah Newman29da5ed2022-04-28 18:51:18 -070083 LOG_FIRST_N(WARNING, 1) << "Failed to set scheduler: " << strerror(errno)
84 << " [" << errno << "]";
Austin Schuhe84c3ed2019-12-14 15:29:48 -080085 }
Sarah Newman1e1b0492023-01-12 14:57:31 -080086#endif
Austin Schuhe84c3ed2019-12-14 15:29:48 -080087}
88
Sarah Newman1e1b0492023-01-12 14:57:31 -080089void SctpServer::SetStreamPriority([[maybe_unused]] sctp_assoc_t assoc_id,
90 [[maybe_unused]] int stream_id,
91 [[maybe_unused]] uint16_t priority) {
92// Kernel 4.9 does not have SCTP_STREAM_SCHEDULER_VALUE
93#ifdef SCTP_STREAM_SCHEDULER_VALUE
Austin Schuhe84c3ed2019-12-14 15:29:48 -080094 struct sctp_stream_value sctp_priority;
95 memset(&sctp_priority, 0, sizeof(sctp_priority));
96 sctp_priority.assoc_id = assoc_id;
97 sctp_priority.stream_id = stream_id;
98 sctp_priority.stream_value = priority;
99 if (setsockopt(fd(), IPPROTO_SCTP, SCTP_STREAM_SCHEDULER_VALUE,
100 &sctp_priority, sizeof(sctp_priority)) != 0) {
Sarah Newman29da5ed2022-04-28 18:51:18 -0700101 LOG_FIRST_N(WARNING, 1) << "Failed to set scheduler: " << strerror(errno)
102 << " [" << errno << "]";
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800103 }
Sarah Newman1e1b0492023-01-12 14:57:31 -0800104#endif
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800105}
106
107} // namespace message_bridge
108} // namespace aos