blob: d0924d568c77a7225926f5167723e01aba29df46 [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 Schuha705d782021-07-31 20:40:00 -0700407 if (MSG_NOTIFICATION & inmessage.msg_flags) {
408 result->message_type = Message::kNotification;
409 } else {
410 result->message_type = Message::kMessage;
411 }
412 result->partial_deliveries = 0;
Austin Schuhc4202572021-03-31 21:06:55 -0700413
Austin Schuha705d782021-07-31 20:40:00 -0700414 {
415 bool found_rcvinfo = false;
416 for (struct cmsghdr *scmsg = CMSG_FIRSTHDR(&inmessage); scmsg != NULL;
417 scmsg = CMSG_NXTHDR(&inmessage, scmsg)) {
418 switch (scmsg->cmsg_type) {
419 case SCTP_RCVINFO: {
420 CHECK(!found_rcvinfo);
421 found_rcvinfo = true;
422 result->header.rcvinfo =
423 *reinterpret_cast<struct sctp_rcvinfo *>(CMSG_DATA(scmsg));
424 } break;
425 default:
426 LOG(INFO) << "\tUnknown type: " << scmsg->cmsg_type;
427 break;
428 }
429 }
430 CHECK_EQ(found_rcvinfo, result->message_type == Message::kMessage)
431 << ": Failed to find a SCTP_RCVINFO cmsghdr. flags: "
432 << inmessage.msg_flags;
433 }
Austin Schuh89f23e32023-05-15 17:06:43 -0700434
435 // Client just sent too big a block of data. Eat it and signal up the
436 // chain.
437 result->size = size;
438 if (size > static_cast<ssize_t>(max_read_size_)) {
439 Abort(result->header.rcvinfo.rcv_assoc_id);
440 result->message_type = Message::kOverflow;
441
442 VLOG(1) << "Message overflowed buffer on stream "
443 << result->header.rcvinfo.rcv_sid << ", disconnecting."
444 << " Check for config mismatch or rogue device.";
445 return result;
446 }
447
448 CHECK_LE(size, static_cast<ssize_t>(max_read_size_))
449 << ": Message overflowed buffer on stream "
450 << result->header.rcvinfo.rcv_sid << "."
451 << " Check for config mismatch or rogue device.";
452
Austin Schuha705d782021-07-31 20:40:00 -0700453 if (result->message_type == Message::kNotification) {
454 // Notifications are never fragmented, just return it now.
455 CHECK(inmessage.msg_flags & MSG_EOR)
456 << ": Notifications should never be big enough to fragment";
457 if (ProcessNotification(result.get())) {
458 // We handled this notification internally, so don't pass it on.
459 return nullptr;
460 }
461 return result;
462 }
463
464 auto partial_message_iterator =
465 std::find_if(partial_messages_.begin(), partial_messages_.end(),
466 [&result](const aos::unique_c_ptr<Message> &candidate) {
467 return result->header.rcvinfo.rcv_sid ==
468 candidate->header.rcvinfo.rcv_sid &&
469 result->header.rcvinfo.rcv_ssn ==
470 candidate->header.rcvinfo.rcv_ssn &&
471 result->header.rcvinfo.rcv_assoc_id ==
472 candidate->header.rcvinfo.rcv_assoc_id;
473 });
474 if (partial_message_iterator != partial_messages_.end()) {
475 const aos::unique_c_ptr<Message> &partial_message =
476 *partial_message_iterator;
477 // Verify it's really part of the same message.
478 CHECK_EQ(partial_message->message_type, result->message_type)
479 << ": for " << result->header.rcvinfo.rcv_sid << ","
480 << result->header.rcvinfo.rcv_ssn << ","
481 << result->header.rcvinfo.rcv_assoc_id;
482 CHECK_EQ(partial_message->header.rcvinfo.rcv_ppid,
483 result->header.rcvinfo.rcv_ppid)
484 << ": for " << result->header.rcvinfo.rcv_sid << ","
485 << result->header.rcvinfo.rcv_ssn << ","
486 << result->header.rcvinfo.rcv_assoc_id;
487
488 // Now copy the data over and update the size.
Austin Schuh89e1e9c2023-05-15 14:38:44 -0700489 CHECK_LE(partial_message->size + result->size, max_read_size_)
Austin Schuha705d782021-07-31 20:40:00 -0700490 << ": Assembled fragments overflowed buffer on stream "
491 << result->header.rcvinfo.rcv_sid << ".";
492 memcpy(partial_message->mutable_data() + partial_message->size,
493 result->data(), result->size);
494 ++partial_message->partial_deliveries;
Sarah Newman4aeb2372022-04-06 13:07:11 -0700495 VLOG(2) << "Merged fragment of " << result->size << " after "
Austin Schuha705d782021-07-31 20:40:00 -0700496 << partial_message->size << ", had "
497 << partial_message->partial_deliveries
498 << ", for: " << result->header.rcvinfo.rcv_sid << ","
499 << result->header.rcvinfo.rcv_ssn << ","
500 << result->header.rcvinfo.rcv_assoc_id;
501 partial_message->size += result->size;
502 result.reset();
503 }
504
505 if (inmessage.msg_flags & MSG_EOR) {
506 // This is the last fragment, so we have something to return.
507 if (partial_message_iterator != partial_messages_.end()) {
508 // It was already merged into the message in the list, so now we pull
509 // that out of the list and return it.
510 CHECK(!result);
511 result = std::move(*partial_message_iterator);
512 partial_messages_.erase(partial_message_iterator);
513 VLOG(1) << "Final count: " << (result->partial_deliveries + 1)
514 << ", size: " << result->size
515 << ", for: " << result->header.rcvinfo.rcv_sid << ","
516 << result->header.rcvinfo.rcv_ssn << ","
517 << result->header.rcvinfo.rcv_assoc_id;
518 }
519 CHECK(result);
520 return result;
521 }
522 if (partial_message_iterator == partial_messages_.end()) {
Sarah Newman4aeb2372022-04-06 13:07:11 -0700523 VLOG(2) << "Starting fragment for: " << result->header.rcvinfo.rcv_sid
Austin Schuha705d782021-07-31 20:40:00 -0700524 << "," << result->header.rcvinfo.rcv_ssn << ","
525 << result->header.rcvinfo.rcv_assoc_id;
526 // Need to record this as the first fragment.
527 partial_messages_.emplace_back(std::move(result));
528 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800529 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800530}
531
Sarah Newman80e955e2022-04-13 11:19:36 -0700532bool SctpReadWrite::Abort(sctp_assoc_t snd_assoc_id) {
533 if (fd_ == -1) {
534 return true;
535 }
536 VLOG(1) << "Sending abort to assoc " << snd_assoc_id;
537
538 // Use the assoc_id for the destination instead of the msg_name.
539 struct msghdr outmsg;
James Kuszmaul784deb72023-02-17 14:42:51 -0800540 memset(&outmsg, 0, sizeof(outmsg));
Sarah Newman80e955e2022-04-13 11:19:36 -0700541 outmsg.msg_namelen = 0;
542
543 outmsg.msg_iovlen = 0;
544
545 // Build up the sndinfo message.
546 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
547 outmsg.msg_control = outcmsg;
548 outmsg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
549 outmsg.msg_flags = 0;
550
551 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&outmsg);
552 cmsg->cmsg_level = IPPROTO_SCTP;
553 cmsg->cmsg_type = SCTP_SNDRCV;
554 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
555
556 struct sctp_sndrcvinfo *sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
557 memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
558 sinfo->sinfo_stream = 0;
559 sinfo->sinfo_flags = SCTP_ABORT;
560 sinfo->sinfo_assoc_id = snd_assoc_id;
561
562 // And send.
563 const ssize_t size = sendmsg(fd_, &outmsg, MSG_NOSIGNAL | MSG_DONTWAIT);
564 if (size == -1) {
565 if (errno == EPIPE || errno == EAGAIN || errno == ESHUTDOWN) {
566 return false;
567 }
568 return false;
569 } else {
570 CHECK_EQ(0, size);
571 return true;
572 }
573}
574
Austin Schuh507f7582021-07-31 20:39:55 -0700575void SctpReadWrite::CloseSocket() {
576 if (fd_ == -1) {
577 return;
578 }
579 LOG(INFO) << "close(" << fd_ << ")";
580 PCHECK(close(fd_) == 0);
581 fd_ = -1;
582}
583
584void SctpReadWrite::DoSetMaxSize() {
Austin Schuh89e1e9c2023-05-15 14:38:44 -0700585 size_t max_size = max_write_size_;
Austin Schuh507f7582021-07-31 20:39:55 -0700586
Austin Schuh61224882021-10-11 18:21:11 -0700587 // This sets the max packet size that we can send.
Austin Schuh89e1e9c2023-05-15 14:38:44 -0700588 CHECK_GE(ReadWMemMax(), max_write_size_)
Austin Schuh507f7582021-07-31 20:39:55 -0700589 << "wmem_max is too low. To increase wmem_max temporarily, do sysctl "
590 "-w net.core.wmem_max="
591 << max_size;
Austin Schuh507f7582021-07-31 20:39:55 -0700592 PCHECK(setsockopt(fd(), SOL_SOCKET, SO_SNDBUF, &max_size, sizeof(max_size)) ==
593 0);
Austin Schuh61224882021-10-11 18:21:11 -0700594
595 // The SO_RCVBUF option (also controlled by net.core.rmem_default) needs to be
596 // decently large but the actual size can be measured by tuning. The defaults
597 // should be fine. If it isn't big enough, transmission will fail.
Austin Schuh9dd8f592021-12-25 14:32:43 -0800598 if (FLAGS_rmem > 0) {
599 size_t rmem = FLAGS_rmem;
600 PCHECK(setsockopt(fd(), SOL_SOCKET, SO_RCVBUF, &rmem, sizeof(rmem)) == 0);
601 }
Austin Schuh507f7582021-07-31 20:39:55 -0700602}
603
Austin Schuha705d782021-07-31 20:40:00 -0700604bool SctpReadWrite::ProcessNotification(const Message *message) {
605 const union sctp_notification *const snp =
606 reinterpret_cast<const union sctp_notification *>(message->data());
607 switch (snp->sn_header.sn_type) {
608 case SCTP_PARTIAL_DELIVERY_EVENT: {
609 const struct sctp_pdapi_event *const partial_delivery =
610 &snp->sn_pdapi_event;
611 CHECK_EQ(partial_delivery->pdapi_length, sizeof(*partial_delivery))
612 << ": Kernel's SCTP code is not a version we support";
613 switch (partial_delivery->pdapi_indication) {
614 case SCTP_PARTIAL_DELIVERY_ABORTED: {
615 const auto iterator = std::find_if(
616 partial_messages_.begin(), partial_messages_.end(),
617 [partial_delivery](const aos::unique_c_ptr<Message> &candidate) {
618 // TODO(Brian): Once we have new enough userpace headers, for
619 // kernels that support level-2 interleaving, we'll need to add
620 // this:
621 // candidate->header.rcvinfo.rcv_sid ==
622 // partial_delivery->pdapi_stream &&
623 // candidate->header.rcvinfo.rcv_ssn ==
624 // partial_delivery->pdapi_seq &&
625 return candidate->header.rcvinfo.rcv_assoc_id ==
626 partial_delivery->pdapi_assoc_id;
627 });
628 CHECK(iterator != partial_messages_.end())
629 << ": Got out of sync with the kernel for "
630 << partial_delivery->pdapi_assoc_id;
631 VLOG(1) << "Pruning partial delivery for "
632 << iterator->get()->header.rcvinfo.rcv_sid << ","
633 << iterator->get()->header.rcvinfo.rcv_ssn << ","
634 << iterator->get()->header.rcvinfo.rcv_assoc_id;
635 partial_messages_.erase(iterator);
636 }
637 return true;
638 }
639 } break;
640 }
641 return false;
642}
643
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800644void Message::LogRcvInfo() const {
645 LOG(INFO) << "\tSNDRCV (stream=" << header.rcvinfo.rcv_sid
646 << " ssn=" << header.rcvinfo.rcv_ssn
647 << " tsn=" << header.rcvinfo.rcv_tsn << " flags=0x" << std::hex
648 << header.rcvinfo.rcv_flags << std::dec
649 << " ppid=" << header.rcvinfo.rcv_ppid
650 << " cumtsn=" << header.rcvinfo.rcv_cumtsn << ")";
651}
652
Austin Schuh2fe4b712020-03-15 14:21:45 -0700653size_t ReadRMemMax() {
654 struct stat current_stat;
655 if (stat("/proc/sys/net/core/rmem_max", &current_stat) != -1) {
656 return static_cast<size_t>(
657 std::stoi(util::ReadFileToStringOrDie("/proc/sys/net/core/rmem_max")));
658 } else {
659 LOG(WARNING) << "/proc/sys/net/core/rmem_max doesn't exist. Are you in a "
660 "container?";
661 return 212992;
662 }
663}
664
665size_t ReadWMemMax() {
666 struct stat current_stat;
667 if (stat("/proc/sys/net/core/wmem_max", &current_stat) != -1) {
668 return static_cast<size_t>(
669 std::stoi(util::ReadFileToStringOrDie("/proc/sys/net/core/wmem_max")));
670 } else {
671 LOG(WARNING) << "/proc/sys/net/core/wmem_max doesn't exist. Are you in a "
672 "container?";
673 return 212992;
674 }
675}
676
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800677} // namespace message_bridge
678} // namespace aos