blob: 3f482c33ef7bc52859215d4abc96cc8d40ea2300 [file] [log] [blame]
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001#include "aos/network/sctp_lib.h"
2
3#include <arpa/inet.h>
4#include <net/if.h>
5#include <netdb.h>
6#include <netinet/sctp.h>
Austin Schuh2fe4b712020-03-15 14:21:45 -07007#include <sys/stat.h>
8#include <sys/types.h>
9#include <unistd.h>
Austin Schuhe84c3ed2019-12-14 15:29:48 -080010
Austin Schuha705d782021-07-31 20:40:00 -070011#include <algorithm>
Austin Schuhe84c3ed2019-12-14 15:29:48 -080012#include <string_view>
13
Austin Schuh2fe4b712020-03-15 14:21:45 -070014#include "aos/util/file.h"
15
Austin Schuh0a0a8272021-12-08 13:19:32 -080016DEFINE_string(interface, "", "network interface");
Brian Silverman0c6d44e2021-11-10 12:27:49 -080017DEFINE_bool(disable_ipv6, false, "disable ipv6");
Austin Schuh9dd8f592021-12-25 14:32:43 -080018DEFINE_int32(rmem, 0, "If nonzero, set rmem to this size.");
Austin Schuhe84c3ed2019-12-14 15:29:48 -080019
20namespace aos {
21namespace message_bridge {
22
23namespace {
24const char *sac_state_tbl[] = {"COMMUNICATION_UP", "COMMUNICATION_LOST",
25 "RESTART", "SHUTDOWN_COMPLETE",
Sarah Newman4aeb2372022-04-06 13:07:11 -070026 "CANT_START_ASSOCIATION"};
Austin Schuhe84c3ed2019-12-14 15:29:48 -080027
28typedef union {
29 struct sctp_initmsg init;
30 struct sctp_sndrcvinfo sndrcvinfo;
31} _sctp_cmsg_data_t;
32
33} // namespace
34
Austin Schuh0a0a8272021-12-08 13:19:32 -080035bool Ipv6Enabled() {
36 if (FLAGS_disable_ipv6) {
37 return false;
38 }
39 int fd = socket(AF_INET6, SOCK_SEQPACKET, IPPROTO_SCTP);
40 if (fd != -1) {
41 close(fd);
42 return true;
43 }
44 switch (errno) {
45 case EAFNOSUPPORT:
46 case EINVAL:
47 case EPROTONOSUPPORT:
48 PLOG(INFO) << "no ipv6";
49 return false;
50 default:
51 PLOG(FATAL) << "Open socket failed";
52 return false;
53 };
54}
55
56struct sockaddr_storage ResolveSocket(std::string_view host, int port,
57 bool use_ipv6) {
Austin Schuhe84c3ed2019-12-14 15:29:48 -080058 struct sockaddr_storage result;
James Kuszmaul784deb72023-02-17 14:42:51 -080059 memset(&result, 0, sizeof(result));
Austin Schuhe84c3ed2019-12-14 15:29:48 -080060 struct addrinfo *addrinfo_result;
61 struct sockaddr_in *t_addr = (struct sockaddr_in *)&result;
62 struct sockaddr_in6 *t_addr6 = (struct sockaddr_in6 *)&result;
Brian Silverman0c6d44e2021-11-10 12:27:49 -080063 struct addrinfo hints;
64 memset(&hints, 0, sizeof(hints));
Austin Schuh0a0a8272021-12-08 13:19:32 -080065 if (!use_ipv6) {
Brian Silverman0c6d44e2021-11-10 12:27:49 -080066 hints.ai_family = AF_INET;
67 } else {
Austin Schuh0a0a8272021-12-08 13:19:32 -080068 // Default to IPv6 as the clearly superior protocol, since it also handles
69 // IPv4.
Brian Silverman0c6d44e2021-11-10 12:27:49 -080070 hints.ai_family = AF_INET6;
71 }
72 hints.ai_socktype = SOCK_SEQPACKET;
73 hints.ai_protocol = IPPROTO_SCTP;
74 // We deliberately avoid AI_ADDRCONFIG here because it breaks running things
75 // inside Bazel's test sandbox, which has no non-localhost IPv4 or IPv6
76 // addresses. Also, it's not really helpful, because most systems will have
77 // link-local addresses of both types with any interface that's up.
78 hints.ai_flags = AI_PASSIVE | AI_V4MAPPED | AI_NUMERICSERV;
79 int ret = getaddrinfo(host.empty() ? nullptr : std::string(host).c_str(),
80 std::to_string(port).c_str(), &hints, &addrinfo_result);
Brian Silverman0c6d44e2021-11-10 12:27:49 -080081 if (ret == EAI_SYSTEM) {
82 PLOG(FATAL) << "getaddrinfo failed to look up '" << host << "'";
83 } else if (ret != 0) {
84 LOG(FATAL) << "getaddrinfo failed to look up '" << host
85 << "': " << gai_strerror(ret);
86 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080087 switch (addrinfo_result->ai_family) {
88 case AF_INET:
89 memcpy(t_addr, addrinfo_result->ai_addr, addrinfo_result->ai_addrlen);
90 t_addr->sin_family = addrinfo_result->ai_family;
91 t_addr->sin_port = htons(port);
92
93 break;
94 case AF_INET6:
95 memcpy(t_addr6, addrinfo_result->ai_addr, addrinfo_result->ai_addrlen);
96 t_addr6->sin6_family = addrinfo_result->ai_family;
97 t_addr6->sin6_port = htons(port);
98
99 if (FLAGS_interface.size() > 0) {
100 t_addr6->sin6_scope_id = if_nametoindex(FLAGS_interface.c_str());
101 }
102
103 break;
104 }
105
106 // Now print it back out nicely.
107 char host_string[NI_MAXHOST];
108 char service_string[NI_MAXSERV];
109
110 int error = getnameinfo((struct sockaddr *)&result,
111 addrinfo_result->ai_addrlen, host_string, NI_MAXHOST,
112 service_string, NI_MAXSERV, NI_NUMERICHOST);
113
114 if (error) {
115 LOG(ERROR) << "Reverse lookup failed ... " << gai_strerror(error);
116 }
117
118 LOG(INFO) << "remote:addr=" << host_string << ", port=" << service_string
119 << ", family=" << addrinfo_result->ai_family;
120
121 freeaddrinfo(addrinfo_result);
122
123 return result;
124}
125
126std::string_view Family(const struct sockaddr_storage &sockaddr) {
127 if (sockaddr.ss_family == AF_INET) {
128 return "AF_INET";
129 } else if (sockaddr.ss_family == AF_INET6) {
130 return "AF_INET6";
131 } else {
132 return "unknown";
133 }
134}
135std::string Address(const struct sockaddr_storage &sockaddr) {
136 char addrbuf[INET6_ADDRSTRLEN];
137 if (sockaddr.ss_family == AF_INET) {
138 const struct sockaddr_in *sin = (const struct sockaddr_in *)&sockaddr;
139 return std::string(
140 inet_ntop(AF_INET, &sin->sin_addr, addrbuf, INET6_ADDRSTRLEN));
141 } else {
142 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)&sockaddr;
143 return std::string(
144 inet_ntop(AF_INET6, &sin6->sin6_addr, addrbuf, INET6_ADDRSTRLEN));
145 }
146}
147
148void PrintNotification(const Message *msg) {
149 const union sctp_notification *snp =
150 (const union sctp_notification *)msg->data();
151
152 LOG(INFO) << "Notification:";
153
154 switch (snp->sn_header.sn_type) {
155 case SCTP_ASSOC_CHANGE: {
156 const struct sctp_assoc_change *sac = &snp->sn_assoc_change;
157 LOG(INFO) << "SCTP_ASSOC_CHANGE(" << sac_state_tbl[sac->sac_state] << ")";
158 VLOG(1) << " (assoc_change: state=" << sac->sac_state
159 << ", error=" << sac->sac_error
160 << ", instr=" << sac->sac_inbound_streams
161 << " outstr=" << sac->sac_outbound_streams
162 << ", assoc=" << sac->sac_assoc_id << ")";
163 } break;
164 case SCTP_PEER_ADDR_CHANGE: {
165 const struct sctp_paddr_change *spc = &snp->sn_paddr_change;
166 LOG(INFO) << " SlCTP_PEER_ADDR_CHANGE";
167 VLOG(1) << "\t\t(peer_addr_change: " << Address(spc->spc_aaddr)
168 << " state=" << spc->spc_state << ", error=" << spc->spc_error
169 << ")";
170 } break;
171 case SCTP_SEND_FAILED: {
172 const struct sctp_send_failed *ssf = &snp->sn_send_failed;
173 LOG(INFO) << " SCTP_SEND_FAILED";
174 VLOG(1) << "\t\t(sendfailed: len=" << ssf->ssf_length
175 << " err=" << ssf->ssf_error << ")";
176 } break;
177 case SCTP_REMOTE_ERROR: {
178 const struct sctp_remote_error *sre = &snp->sn_remote_error;
179 LOG(INFO) << " SCTP_REMOTE_ERROR";
180 VLOG(1) << "\t\t(remote_error: err=" << ntohs(sre->sre_error) << ")";
181 } break;
Austin Schuhf7777002020-09-01 18:41:28 -0700182 case SCTP_STREAM_CHANGE_EVENT: {
Austin Schuh62a0c272021-03-31 21:04:53 -0700183 const struct sctp_stream_change_event *sce = &snp->sn_strchange_event;
Austin Schuhf7777002020-09-01 18:41:28 -0700184 LOG(INFO) << " SCTP_STREAM_CHANGE_EVENT";
185 VLOG(1) << "\t\t(stream_change_event: flags=" << sce->strchange_flags
186 << ", assoc_id=" << sce->strchange_assoc_id
187 << ", instrms=" << sce->strchange_instrms
188 << ", outstrms=" << sce->strchange_outstrms << " )";
189 } break;
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800190 case SCTP_SHUTDOWN_EVENT: {
191 LOG(INFO) << " SCTP_SHUTDOWN_EVENT";
192 } break;
193 default:
194 LOG(INFO) << " Unknown type: " << snp->sn_header.sn_type;
195 break;
196 }
197}
198
199std::string GetHostname() {
200 char buf[256];
201 buf[sizeof(buf) - 1] = '\0';
202 PCHECK(gethostname(buf, sizeof(buf) - 1) == 0);
203 return buf;
204}
205
206std::string Message::PeerAddress() const { return Address(sin); }
207
208void LogSctpStatus(int fd, sctp_assoc_t assoc_id) {
209 struct sctp_status status;
210 memset(&status, 0, sizeof(status));
211 status.sstat_assoc_id = assoc_id;
212
213 socklen_t size = sizeof(status);
Austin Schuha5f545b2021-07-31 20:39:42 -0700214 const int result = getsockopt(fd, SOL_SCTP, SCTP_STATUS,
215 reinterpret_cast<void *>(&status), &size);
216 if (result == -1 && errno == EINVAL) {
217 LOG(INFO) << "sctp_status) not associated";
218 return;
219 }
220 PCHECK(result == 0);
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800221
222 LOG(INFO) << "sctp_status) sstat_assoc_id:" << status.sstat_assoc_id
223 << " sstat_state:" << status.sstat_state
224 << " sstat_rwnd:" << status.sstat_rwnd
225 << " sstat_unackdata:" << status.sstat_unackdata
226 << " sstat_penddata:" << status.sstat_penddata
227 << " sstat_instrms:" << status.sstat_instrms
228 << " sstat_outstrms:" << status.sstat_outstrms
229 << " sstat_fragmentation_point:" << status.sstat_fragmentation_point
230 << " sstat_primary.spinfo_srtt:" << status.sstat_primary.spinfo_srtt
231 << " sstat_primary.spinfo_rto:" << status.sstat_primary.spinfo_rto;
232}
233
Austin Schuh507f7582021-07-31 20:39:55 -0700234void SctpReadWrite::OpenSocket(const struct sockaddr_storage &sockaddr_local) {
235 fd_ = socket(sockaddr_local.ss_family, SOCK_SEQPACKET, IPPROTO_SCTP);
236 PCHECK(fd_ != -1);
237 LOG(INFO) << "socket(" << Family(sockaddr_local)
238 << ", SOCK_SEQPACKET, IPPROTOSCTP) = " << fd_;
239 {
240 // Per https://tools.ietf.org/html/rfc6458
241 // Setting this to !0 allows event notifications to be interleaved
Austin Schuha705d782021-07-31 20:40:00 -0700242 // with data if enabled. This typically only matters during congestion.
243 // However, Linux seems to interleave under memory pressure regardless of
244 // this being enabled, so we have to handle it in the code anyways, so might
245 // as well turn it on all the time.
246 // TODO(Brian): Change this to 2 once we have kernels that support it, and
247 // also address the TODO in ProcessNotification to match on all the
248 // necessary fields.
249 int interleaving = 1;
Austin Schuh507f7582021-07-31 20:39:55 -0700250 PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_FRAGMENT_INTERLEAVE,
251 &interleaving, sizeof(interleaving)) == 0);
252 }
253 {
254 // Enable recvinfo when a packet arrives.
255 int on = 1;
256 PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_RECVRCVINFO, &on, sizeof(int)) ==
257 0);
258 }
259
Austin Schuha705d782021-07-31 20:40:00 -0700260 {
261 // TODO(austin): This is the old style registration... But, the sctp
262 // stack out in the wild for linux is old and primitive.
263 struct sctp_event_subscribe subscribe;
264 memset(&subscribe, 0, sizeof(subscribe));
265 subscribe.sctp_association_event = 1;
266 subscribe.sctp_stream_change_event = 1;
267 subscribe.sctp_partial_delivery_event = 1;
268 PCHECK(setsockopt(fd(), SOL_SCTP, SCTP_EVENTS, (char *)&subscribe,
269 sizeof(subscribe)) == 0);
270 }
271
Austin Schuh507f7582021-07-31 20:39:55 -0700272 DoSetMaxSize();
273}
274
275bool SctpReadWrite::SendMessage(
276 int stream, std::string_view data, int time_to_live,
277 std::optional<struct sockaddr_storage> sockaddr_remote,
278 sctp_assoc_t snd_assoc_id) {
279 CHECK(fd_ != -1);
280 struct iovec iov;
281 iov.iov_base = const_cast<char *>(data.data());
282 iov.iov_len = data.size();
283
284 // Use the assoc_id for the destination instead of the msg_name.
285 struct msghdr outmsg;
James Kuszmaul784deb72023-02-17 14:42:51 -0800286 memset(&outmsg, 0, sizeof(outmsg));
Austin Schuh507f7582021-07-31 20:39:55 -0700287 if (sockaddr_remote) {
288 outmsg.msg_name = &*sockaddr_remote;
289 outmsg.msg_namelen = sizeof(*sockaddr_remote);
Sarah Newman4aeb2372022-04-06 13:07:11 -0700290 VLOG(2) << "Sending to " << Address(*sockaddr_remote);
Austin Schuh507f7582021-07-31 20:39:55 -0700291 } else {
292 outmsg.msg_namelen = 0;
293 }
294
295 // Data to send.
296 outmsg.msg_iov = &iov;
297 outmsg.msg_iovlen = 1;
298
299 // Build up the sndinfo message.
300 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
301 outmsg.msg_control = outcmsg;
302 outmsg.msg_controllen = sizeof(outcmsg);
303 outmsg.msg_flags = 0;
304
305 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&outmsg);
306 cmsg->cmsg_level = IPPROTO_SCTP;
307 cmsg->cmsg_type = SCTP_SNDRCV;
308 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
309
310 struct sctp_sndrcvinfo *sinfo =
311 reinterpret_cast<struct sctp_sndrcvinfo *>(CMSG_DATA(cmsg));
312 memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
313 sinfo->sinfo_ppid = ++send_ppid_;
314 sinfo->sinfo_stream = stream;
315 sinfo->sinfo_flags = 0;
316 sinfo->sinfo_assoc_id = snd_assoc_id;
317 sinfo->sinfo_timetolive = time_to_live;
318
319 // And send.
320 const ssize_t size = sendmsg(fd_, &outmsg, MSG_NOSIGNAL | MSG_DONTWAIT);
321 if (size == -1) {
322 if (errno == EPIPE || errno == EAGAIN || errno == ESHUTDOWN ||
323 errno == EINTR) {
Austin Schuh581fab92022-02-07 19:50:54 -0800324 if (VLOG_IS_ON(1)) {
325 PLOG(WARNING) << "sendmsg on sctp socket failed";
326 }
Austin Schuh507f7582021-07-31 20:39:55 -0700327 return false;
328 }
329 PLOG(FATAL) << "sendmsg on sctp socket failed";
330 return false;
331 }
332 CHECK_EQ(static_cast<ssize_t>(data.size()), size);
Sarah Newman4aeb2372022-04-06 13:07:11 -0700333 VLOG(2) << "Sent " << data.size();
Austin Schuh507f7582021-07-31 20:39:55 -0700334 return true;
335}
336
Austin Schuhf95a6ab2023-05-15 14:34:57 -0700337void SctpReadWrite::FreeMessage(aos::unique_c_ptr<Message> &&message) {
338 if (use_pool_) {
339 free_messages_.emplace_back(std::move(message));
340 }
341}
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800342
Austin Schuhf95a6ab2023-05-15 14:34:57 -0700343void SctpReadWrite::SetPoolSize(size_t pool_size) {
344 CHECK(!use_pool_);
345 free_messages_.reserve(pool_size);
346 for (size_t i = 0; i < pool_size; ++i) {
347 free_messages_.emplace_back(AcquireMessage());
348 }
349 use_pool_ = true;
350}
351
352aos::unique_c_ptr<Message> SctpReadWrite::AcquireMessage() {
353 if (!use_pool_) {
James Kuszmaul6e622382023-02-17 14:56:38 -0800354 constexpr size_t kMessageAlign = alignof(Message);
355 const size_t max_message_size =
Austin Schuh89e1e9c2023-05-15 14:38:44 -0700356 ((sizeof(Message) + max_read_size_ + 1 + (kMessageAlign - 1)) /
James Kuszmaul6e622382023-02-17 14:56:38 -0800357 kMessageAlign) *
358 kMessageAlign;
359 aos::unique_c_ptr<Message> result(reinterpret_cast<Message *>(
360 aligned_alloc(kMessageAlign, max_message_size)));
Austin Schuhf95a6ab2023-05-15 14:34:57 -0700361 return result;
362 } else {
363 CHECK_GT(free_messages_.size(), 0u);
364 aos::unique_c_ptr<Message> result = std::move(free_messages_.back());
365 free_messages_.pop_back();
366 return result;
367 }
368}
369
370// We read each fragment into a fresh Message, because most of them won't be
371// fragmented. If we do end up with a fragment, then we copy the data out of it.
372aos::unique_c_ptr<Message> SctpReadWrite::ReadMessage() {
373 CHECK(fd_ != -1);
374
375 while (true) {
376 aos::unique_c_ptr<Message> result = AcquireMessage();
Austin Schuha705d782021-07-31 20:40:00 -0700377
Austin Schuh05c18122021-07-31 20:39:47 -0700378 struct msghdr inmessage;
Austin Schuhc4202572021-03-31 21:06:55 -0700379 memset(&inmessage, 0, sizeof(struct msghdr));
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800380
Austin Schuh05c18122021-07-31 20:39:47 -0700381 struct iovec iov;
Austin Schuh89e1e9c2023-05-15 14:38:44 -0700382 iov.iov_len = max_read_size_ + 1;
Austin Schuha705d782021-07-31 20:40:00 -0700383 iov.iov_base = result->mutable_data();
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800384
Austin Schuhc4202572021-03-31 21:06:55 -0700385 inmessage.msg_iov = &iov;
386 inmessage.msg_iovlen = 1;
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800387
Austin Schuh05c18122021-07-31 20:39:47 -0700388 char incmsg[CMSG_SPACE(sizeof(_sctp_cmsg_data_t))];
Austin Schuhc4202572021-03-31 21:06:55 -0700389 inmessage.msg_control = incmsg;
390 inmessage.msg_controllen = sizeof(incmsg);
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800391
Austin Schuhc4202572021-03-31 21:06:55 -0700392 inmessage.msg_namelen = sizeof(struct sockaddr_storage);
393 inmessage.msg_name = &result->sin;
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800394
Austin Schuha705d782021-07-31 20:40:00 -0700395 const ssize_t size = recvmsg(fd_, &inmessage, MSG_DONTWAIT);
396 if (size == -1) {
397 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
398 // These are all non-fatal failures indicating we should retry later.
399 return nullptr;
Austin Schuhc4202572021-03-31 21:06:55 -0700400 }
Austin Schuha705d782021-07-31 20:40:00 -0700401 PLOG(FATAL) << "recvmsg on sctp socket " << fd_ << " failed";
Austin Schuhc4202572021-03-31 21:06:55 -0700402 }
403
Austin Schuha705d782021-07-31 20:40:00 -0700404 CHECK(!(inmessage.msg_flags & MSG_CTRUNC))
Austin Schuhc4202572021-03-31 21:06:55 -0700405 << ": Control message truncated.";
406
Austin Schuh89e1e9c2023-05-15 14:38:44 -0700407 CHECK_LE(size, static_cast<ssize_t>(max_read_size_))
Austin Schuh507f7582021-07-31 20:39:55 -0700408 << ": Message overflowed buffer on stream "
409 << result->header.rcvinfo.rcv_sid << ".";
Austin Schuhc4202572021-03-31 21:06:55 -0700410
Austin Schuha705d782021-07-31 20:40:00 -0700411 result->size = size;
412 if (MSG_NOTIFICATION & inmessage.msg_flags) {
413 result->message_type = Message::kNotification;
414 } else {
415 result->message_type = Message::kMessage;
416 }
417 result->partial_deliveries = 0;
Austin Schuhc4202572021-03-31 21:06:55 -0700418
Austin Schuha705d782021-07-31 20:40:00 -0700419 {
420 bool found_rcvinfo = false;
421 for (struct cmsghdr *scmsg = CMSG_FIRSTHDR(&inmessage); scmsg != NULL;
422 scmsg = CMSG_NXTHDR(&inmessage, scmsg)) {
423 switch (scmsg->cmsg_type) {
424 case SCTP_RCVINFO: {
425 CHECK(!found_rcvinfo);
426 found_rcvinfo = true;
427 result->header.rcvinfo =
428 *reinterpret_cast<struct sctp_rcvinfo *>(CMSG_DATA(scmsg));
429 } break;
430 default:
431 LOG(INFO) << "\tUnknown type: " << scmsg->cmsg_type;
432 break;
433 }
434 }
435 CHECK_EQ(found_rcvinfo, result->message_type == Message::kMessage)
436 << ": Failed to find a SCTP_RCVINFO cmsghdr. flags: "
437 << inmessage.msg_flags;
438 }
439 if (result->message_type == Message::kNotification) {
440 // Notifications are never fragmented, just return it now.
441 CHECK(inmessage.msg_flags & MSG_EOR)
442 << ": Notifications should never be big enough to fragment";
443 if (ProcessNotification(result.get())) {
444 // We handled this notification internally, so don't pass it on.
445 return nullptr;
446 }
447 return result;
448 }
449
450 auto partial_message_iterator =
451 std::find_if(partial_messages_.begin(), partial_messages_.end(),
452 [&result](const aos::unique_c_ptr<Message> &candidate) {
453 return result->header.rcvinfo.rcv_sid ==
454 candidate->header.rcvinfo.rcv_sid &&
455 result->header.rcvinfo.rcv_ssn ==
456 candidate->header.rcvinfo.rcv_ssn &&
457 result->header.rcvinfo.rcv_assoc_id ==
458 candidate->header.rcvinfo.rcv_assoc_id;
459 });
460 if (partial_message_iterator != partial_messages_.end()) {
461 const aos::unique_c_ptr<Message> &partial_message =
462 *partial_message_iterator;
463 // Verify it's really part of the same message.
464 CHECK_EQ(partial_message->message_type, result->message_type)
465 << ": for " << result->header.rcvinfo.rcv_sid << ","
466 << result->header.rcvinfo.rcv_ssn << ","
467 << result->header.rcvinfo.rcv_assoc_id;
468 CHECK_EQ(partial_message->header.rcvinfo.rcv_ppid,
469 result->header.rcvinfo.rcv_ppid)
470 << ": for " << result->header.rcvinfo.rcv_sid << ","
471 << result->header.rcvinfo.rcv_ssn << ","
472 << result->header.rcvinfo.rcv_assoc_id;
473
474 // Now copy the data over and update the size.
Austin Schuh89e1e9c2023-05-15 14:38:44 -0700475 CHECK_LE(partial_message->size + result->size, max_read_size_)
Austin Schuha705d782021-07-31 20:40:00 -0700476 << ": Assembled fragments overflowed buffer on stream "
477 << result->header.rcvinfo.rcv_sid << ".";
478 memcpy(partial_message->mutable_data() + partial_message->size,
479 result->data(), result->size);
480 ++partial_message->partial_deliveries;
Sarah Newman4aeb2372022-04-06 13:07:11 -0700481 VLOG(2) << "Merged fragment of " << result->size << " after "
Austin Schuha705d782021-07-31 20:40:00 -0700482 << partial_message->size << ", had "
483 << partial_message->partial_deliveries
484 << ", for: " << result->header.rcvinfo.rcv_sid << ","
485 << result->header.rcvinfo.rcv_ssn << ","
486 << result->header.rcvinfo.rcv_assoc_id;
487 partial_message->size += result->size;
488 result.reset();
489 }
490
491 if (inmessage.msg_flags & MSG_EOR) {
492 // This is the last fragment, so we have something to return.
493 if (partial_message_iterator != partial_messages_.end()) {
494 // It was already merged into the message in the list, so now we pull
495 // that out of the list and return it.
496 CHECK(!result);
497 result = std::move(*partial_message_iterator);
498 partial_messages_.erase(partial_message_iterator);
499 VLOG(1) << "Final count: " << (result->partial_deliveries + 1)
500 << ", size: " << result->size
501 << ", for: " << result->header.rcvinfo.rcv_sid << ","
502 << result->header.rcvinfo.rcv_ssn << ","
503 << result->header.rcvinfo.rcv_assoc_id;
504 }
505 CHECK(result);
506 return result;
507 }
508 if (partial_message_iterator == partial_messages_.end()) {
Sarah Newman4aeb2372022-04-06 13:07:11 -0700509 VLOG(2) << "Starting fragment for: " << result->header.rcvinfo.rcv_sid
Austin Schuha705d782021-07-31 20:40:00 -0700510 << "," << result->header.rcvinfo.rcv_ssn << ","
511 << result->header.rcvinfo.rcv_assoc_id;
512 // Need to record this as the first fragment.
513 partial_messages_.emplace_back(std::move(result));
514 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800515 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800516}
517
Sarah Newman80e955e2022-04-13 11:19:36 -0700518bool SctpReadWrite::Abort(sctp_assoc_t snd_assoc_id) {
519 if (fd_ == -1) {
520 return true;
521 }
522 VLOG(1) << "Sending abort to assoc " << snd_assoc_id;
523
524 // Use the assoc_id for the destination instead of the msg_name.
525 struct msghdr outmsg;
James Kuszmaul784deb72023-02-17 14:42:51 -0800526 memset(&outmsg, 0, sizeof(outmsg));
Sarah Newman80e955e2022-04-13 11:19:36 -0700527 outmsg.msg_namelen = 0;
528
529 outmsg.msg_iovlen = 0;
530
531 // Build up the sndinfo message.
532 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
533 outmsg.msg_control = outcmsg;
534 outmsg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
535 outmsg.msg_flags = 0;
536
537 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&outmsg);
538 cmsg->cmsg_level = IPPROTO_SCTP;
539 cmsg->cmsg_type = SCTP_SNDRCV;
540 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
541
542 struct sctp_sndrcvinfo *sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
543 memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
544 sinfo->sinfo_stream = 0;
545 sinfo->sinfo_flags = SCTP_ABORT;
546 sinfo->sinfo_assoc_id = snd_assoc_id;
547
548 // And send.
549 const ssize_t size = sendmsg(fd_, &outmsg, MSG_NOSIGNAL | MSG_DONTWAIT);
550 if (size == -1) {
551 if (errno == EPIPE || errno == EAGAIN || errno == ESHUTDOWN) {
552 return false;
553 }
554 return false;
555 } else {
556 CHECK_EQ(0, size);
557 return true;
558 }
559}
560
Austin Schuh507f7582021-07-31 20:39:55 -0700561void SctpReadWrite::CloseSocket() {
562 if (fd_ == -1) {
563 return;
564 }
565 LOG(INFO) << "close(" << fd_ << ")";
566 PCHECK(close(fd_) == 0);
567 fd_ = -1;
568}
569
570void SctpReadWrite::DoSetMaxSize() {
Austin Schuh89e1e9c2023-05-15 14:38:44 -0700571 size_t max_size = max_write_size_;
Austin Schuh507f7582021-07-31 20:39:55 -0700572
Austin Schuh61224882021-10-11 18:21:11 -0700573 // This sets the max packet size that we can send.
Austin Schuh89e1e9c2023-05-15 14:38:44 -0700574 CHECK_GE(ReadWMemMax(), max_write_size_)
Austin Schuh507f7582021-07-31 20:39:55 -0700575 << "wmem_max is too low. To increase wmem_max temporarily, do sysctl "
576 "-w net.core.wmem_max="
577 << max_size;
Austin Schuh507f7582021-07-31 20:39:55 -0700578 PCHECK(setsockopt(fd(), SOL_SOCKET, SO_SNDBUF, &max_size, sizeof(max_size)) ==
579 0);
Austin Schuh61224882021-10-11 18:21:11 -0700580
581 // The SO_RCVBUF option (also controlled by net.core.rmem_default) needs to be
582 // decently large but the actual size can be measured by tuning. The defaults
583 // should be fine. If it isn't big enough, transmission will fail.
Austin Schuh9dd8f592021-12-25 14:32:43 -0800584 if (FLAGS_rmem > 0) {
585 size_t rmem = FLAGS_rmem;
586 PCHECK(setsockopt(fd(), SOL_SOCKET, SO_RCVBUF, &rmem, sizeof(rmem)) == 0);
587 }
Austin Schuh507f7582021-07-31 20:39:55 -0700588}
589
Austin Schuha705d782021-07-31 20:40:00 -0700590bool SctpReadWrite::ProcessNotification(const Message *message) {
591 const union sctp_notification *const snp =
592 reinterpret_cast<const union sctp_notification *>(message->data());
593 switch (snp->sn_header.sn_type) {
594 case SCTP_PARTIAL_DELIVERY_EVENT: {
595 const struct sctp_pdapi_event *const partial_delivery =
596 &snp->sn_pdapi_event;
597 CHECK_EQ(partial_delivery->pdapi_length, sizeof(*partial_delivery))
598 << ": Kernel's SCTP code is not a version we support";
599 switch (partial_delivery->pdapi_indication) {
600 case SCTP_PARTIAL_DELIVERY_ABORTED: {
601 const auto iterator = std::find_if(
602 partial_messages_.begin(), partial_messages_.end(),
603 [partial_delivery](const aos::unique_c_ptr<Message> &candidate) {
604 // TODO(Brian): Once we have new enough userpace headers, for
605 // kernels that support level-2 interleaving, we'll need to add
606 // this:
607 // candidate->header.rcvinfo.rcv_sid ==
608 // partial_delivery->pdapi_stream &&
609 // candidate->header.rcvinfo.rcv_ssn ==
610 // partial_delivery->pdapi_seq &&
611 return candidate->header.rcvinfo.rcv_assoc_id ==
612 partial_delivery->pdapi_assoc_id;
613 });
614 CHECK(iterator != partial_messages_.end())
615 << ": Got out of sync with the kernel for "
616 << partial_delivery->pdapi_assoc_id;
617 VLOG(1) << "Pruning partial delivery for "
618 << iterator->get()->header.rcvinfo.rcv_sid << ","
619 << iterator->get()->header.rcvinfo.rcv_ssn << ","
620 << iterator->get()->header.rcvinfo.rcv_assoc_id;
621 partial_messages_.erase(iterator);
622 }
623 return true;
624 }
625 } break;
626 }
627 return false;
628}
629
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800630void Message::LogRcvInfo() const {
631 LOG(INFO) << "\tSNDRCV (stream=" << header.rcvinfo.rcv_sid
632 << " ssn=" << header.rcvinfo.rcv_ssn
633 << " tsn=" << header.rcvinfo.rcv_tsn << " flags=0x" << std::hex
634 << header.rcvinfo.rcv_flags << std::dec
635 << " ppid=" << header.rcvinfo.rcv_ppid
636 << " cumtsn=" << header.rcvinfo.rcv_cumtsn << ")";
637}
638
Austin Schuh2fe4b712020-03-15 14:21:45 -0700639size_t ReadRMemMax() {
640 struct stat current_stat;
641 if (stat("/proc/sys/net/core/rmem_max", &current_stat) != -1) {
642 return static_cast<size_t>(
643 std::stoi(util::ReadFileToStringOrDie("/proc/sys/net/core/rmem_max")));
644 } else {
645 LOG(WARNING) << "/proc/sys/net/core/rmem_max doesn't exist. Are you in a "
646 "container?";
647 return 212992;
648 }
649}
650
651size_t ReadWMemMax() {
652 struct stat current_stat;
653 if (stat("/proc/sys/net/core/wmem_max", &current_stat) != -1) {
654 return static_cast<size_t>(
655 std::stoi(util::ReadFileToStringOrDie("/proc/sys/net/core/wmem_max")));
656 } else {
657 LOG(WARNING) << "/proc/sys/net/core/wmem_max doesn't exist. Are you in a "
658 "container?";
659 return 212992;
660 }
661}
662
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800663} // namespace message_bridge
664} // namespace aos