blob: 99ead3701011c3a3775512194734428f9064a703 [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 Schuhe84c3ed2019-12-14 15:29:48 -080018
19namespace aos {
20namespace message_bridge {
21
22namespace {
23const char *sac_state_tbl[] = {"COMMUNICATION_UP", "COMMUNICATION_LOST",
24 "RESTART", "SHUTDOWN_COMPLETE",
25 "CANT_START_ASSOCICATION"};
26
27typedef union {
28 struct sctp_initmsg init;
29 struct sctp_sndrcvinfo sndrcvinfo;
30} _sctp_cmsg_data_t;
31
32} // namespace
33
Austin Schuh0a0a8272021-12-08 13:19:32 -080034bool Ipv6Enabled() {
35 if (FLAGS_disable_ipv6) {
36 return false;
37 }
38 int fd = socket(AF_INET6, SOCK_SEQPACKET, IPPROTO_SCTP);
39 if (fd != -1) {
40 close(fd);
41 return true;
42 }
43 switch (errno) {
44 case EAFNOSUPPORT:
45 case EINVAL:
46 case EPROTONOSUPPORT:
47 PLOG(INFO) << "no ipv6";
48 return false;
49 default:
50 PLOG(FATAL) << "Open socket failed";
51 return false;
52 };
53}
54
55struct sockaddr_storage ResolveSocket(std::string_view host, int port,
56 bool use_ipv6) {
Austin Schuhe84c3ed2019-12-14 15:29:48 -080057 struct sockaddr_storage result;
58 struct addrinfo *addrinfo_result;
59 struct sockaddr_in *t_addr = (struct sockaddr_in *)&result;
60 struct sockaddr_in6 *t_addr6 = (struct sockaddr_in6 *)&result;
Brian Silverman0c6d44e2021-11-10 12:27:49 -080061 struct addrinfo hints;
62 memset(&hints, 0, sizeof(hints));
Austin Schuh0a0a8272021-12-08 13:19:32 -080063 if (!use_ipv6) {
Brian Silverman0c6d44e2021-11-10 12:27:49 -080064 hints.ai_family = AF_INET;
65 } else {
Austin Schuh0a0a8272021-12-08 13:19:32 -080066 // Default to IPv6 as the clearly superior protocol, since it also handles
67 // IPv4.
Brian Silverman0c6d44e2021-11-10 12:27:49 -080068 hints.ai_family = AF_INET6;
69 }
70 hints.ai_socktype = SOCK_SEQPACKET;
71 hints.ai_protocol = IPPROTO_SCTP;
72 // We deliberately avoid AI_ADDRCONFIG here because it breaks running things
73 // inside Bazel's test sandbox, which has no non-localhost IPv4 or IPv6
74 // addresses. Also, it's not really helpful, because most systems will have
75 // link-local addresses of both types with any interface that's up.
76 hints.ai_flags = AI_PASSIVE | AI_V4MAPPED | AI_NUMERICSERV;
77 int ret = getaddrinfo(host.empty() ? nullptr : std::string(host).c_str(),
78 std::to_string(port).c_str(), &hints, &addrinfo_result);
Brian Silverman0c6d44e2021-11-10 12:27:49 -080079 if (ret == EAI_SYSTEM) {
80 PLOG(FATAL) << "getaddrinfo failed to look up '" << host << "'";
81 } else if (ret != 0) {
82 LOG(FATAL) << "getaddrinfo failed to look up '" << host
83 << "': " << gai_strerror(ret);
84 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080085 switch (addrinfo_result->ai_family) {
86 case AF_INET:
87 memcpy(t_addr, addrinfo_result->ai_addr, addrinfo_result->ai_addrlen);
88 t_addr->sin_family = addrinfo_result->ai_family;
89 t_addr->sin_port = htons(port);
90
91 break;
92 case AF_INET6:
93 memcpy(t_addr6, addrinfo_result->ai_addr, addrinfo_result->ai_addrlen);
94 t_addr6->sin6_family = addrinfo_result->ai_family;
95 t_addr6->sin6_port = htons(port);
96
97 if (FLAGS_interface.size() > 0) {
98 t_addr6->sin6_scope_id = if_nametoindex(FLAGS_interface.c_str());
99 }
100
101 break;
102 }
103
104 // Now print it back out nicely.
105 char host_string[NI_MAXHOST];
106 char service_string[NI_MAXSERV];
107
108 int error = getnameinfo((struct sockaddr *)&result,
109 addrinfo_result->ai_addrlen, host_string, NI_MAXHOST,
110 service_string, NI_MAXSERV, NI_NUMERICHOST);
111
112 if (error) {
113 LOG(ERROR) << "Reverse lookup failed ... " << gai_strerror(error);
114 }
115
116 LOG(INFO) << "remote:addr=" << host_string << ", port=" << service_string
117 << ", family=" << addrinfo_result->ai_family;
118
119 freeaddrinfo(addrinfo_result);
120
121 return result;
122}
123
124std::string_view Family(const struct sockaddr_storage &sockaddr) {
125 if (sockaddr.ss_family == AF_INET) {
126 return "AF_INET";
127 } else if (sockaddr.ss_family == AF_INET6) {
128 return "AF_INET6";
129 } else {
130 return "unknown";
131 }
132}
133std::string Address(const struct sockaddr_storage &sockaddr) {
134 char addrbuf[INET6_ADDRSTRLEN];
135 if (sockaddr.ss_family == AF_INET) {
136 const struct sockaddr_in *sin = (const struct sockaddr_in *)&sockaddr;
137 return std::string(
138 inet_ntop(AF_INET, &sin->sin_addr, addrbuf, INET6_ADDRSTRLEN));
139 } else {
140 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)&sockaddr;
141 return std::string(
142 inet_ntop(AF_INET6, &sin6->sin6_addr, addrbuf, INET6_ADDRSTRLEN));
143 }
144}
145
146void PrintNotification(const Message *msg) {
147 const union sctp_notification *snp =
148 (const union sctp_notification *)msg->data();
149
150 LOG(INFO) << "Notification:";
151
152 switch (snp->sn_header.sn_type) {
153 case SCTP_ASSOC_CHANGE: {
154 const struct sctp_assoc_change *sac = &snp->sn_assoc_change;
155 LOG(INFO) << "SCTP_ASSOC_CHANGE(" << sac_state_tbl[sac->sac_state] << ")";
156 VLOG(1) << " (assoc_change: state=" << sac->sac_state
157 << ", error=" << sac->sac_error
158 << ", instr=" << sac->sac_inbound_streams
159 << " outstr=" << sac->sac_outbound_streams
160 << ", assoc=" << sac->sac_assoc_id << ")";
161 } break;
162 case SCTP_PEER_ADDR_CHANGE: {
163 const struct sctp_paddr_change *spc = &snp->sn_paddr_change;
164 LOG(INFO) << " SlCTP_PEER_ADDR_CHANGE";
165 VLOG(1) << "\t\t(peer_addr_change: " << Address(spc->spc_aaddr)
166 << " state=" << spc->spc_state << ", error=" << spc->spc_error
167 << ")";
168 } break;
169 case SCTP_SEND_FAILED: {
170 const struct sctp_send_failed *ssf = &snp->sn_send_failed;
171 LOG(INFO) << " SCTP_SEND_FAILED";
172 VLOG(1) << "\t\t(sendfailed: len=" << ssf->ssf_length
173 << " err=" << ssf->ssf_error << ")";
174 } break;
175 case SCTP_REMOTE_ERROR: {
176 const struct sctp_remote_error *sre = &snp->sn_remote_error;
177 LOG(INFO) << " SCTP_REMOTE_ERROR";
178 VLOG(1) << "\t\t(remote_error: err=" << ntohs(sre->sre_error) << ")";
179 } break;
Austin Schuhf7777002020-09-01 18:41:28 -0700180 case SCTP_STREAM_CHANGE_EVENT: {
Austin Schuh62a0c272021-03-31 21:04:53 -0700181 const struct sctp_stream_change_event *sce = &snp->sn_strchange_event;
Austin Schuhf7777002020-09-01 18:41:28 -0700182 LOG(INFO) << " SCTP_STREAM_CHANGE_EVENT";
183 VLOG(1) << "\t\t(stream_change_event: flags=" << sce->strchange_flags
184 << ", assoc_id=" << sce->strchange_assoc_id
185 << ", instrms=" << sce->strchange_instrms
186 << ", outstrms=" << sce->strchange_outstrms << " )";
187 } break;
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800188 case SCTP_SHUTDOWN_EVENT: {
189 LOG(INFO) << " SCTP_SHUTDOWN_EVENT";
190 } break;
191 default:
192 LOG(INFO) << " Unknown type: " << snp->sn_header.sn_type;
193 break;
194 }
195}
196
197std::string GetHostname() {
198 char buf[256];
199 buf[sizeof(buf) - 1] = '\0';
200 PCHECK(gethostname(buf, sizeof(buf) - 1) == 0);
201 return buf;
202}
203
204std::string Message::PeerAddress() const { return Address(sin); }
205
206void LogSctpStatus(int fd, sctp_assoc_t assoc_id) {
207 struct sctp_status status;
208 memset(&status, 0, sizeof(status));
209 status.sstat_assoc_id = assoc_id;
210
211 socklen_t size = sizeof(status);
Austin Schuha5f545b2021-07-31 20:39:42 -0700212 const int result = getsockopt(fd, SOL_SCTP, SCTP_STATUS,
213 reinterpret_cast<void *>(&status), &size);
214 if (result == -1 && errno == EINVAL) {
215 LOG(INFO) << "sctp_status) not associated";
216 return;
217 }
218 PCHECK(result == 0);
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800219
220 LOG(INFO) << "sctp_status) sstat_assoc_id:" << status.sstat_assoc_id
221 << " sstat_state:" << status.sstat_state
222 << " sstat_rwnd:" << status.sstat_rwnd
223 << " sstat_unackdata:" << status.sstat_unackdata
224 << " sstat_penddata:" << status.sstat_penddata
225 << " sstat_instrms:" << status.sstat_instrms
226 << " sstat_outstrms:" << status.sstat_outstrms
227 << " sstat_fragmentation_point:" << status.sstat_fragmentation_point
228 << " sstat_primary.spinfo_srtt:" << status.sstat_primary.spinfo_srtt
229 << " sstat_primary.spinfo_rto:" << status.sstat_primary.spinfo_rto;
230}
231
Austin Schuh507f7582021-07-31 20:39:55 -0700232void SctpReadWrite::OpenSocket(const struct sockaddr_storage &sockaddr_local) {
233 fd_ = socket(sockaddr_local.ss_family, SOCK_SEQPACKET, IPPROTO_SCTP);
234 PCHECK(fd_ != -1);
235 LOG(INFO) << "socket(" << Family(sockaddr_local)
236 << ", SOCK_SEQPACKET, IPPROTOSCTP) = " << fd_;
237 {
238 // Per https://tools.ietf.org/html/rfc6458
239 // Setting this to !0 allows event notifications to be interleaved
Austin Schuha705d782021-07-31 20:40:00 -0700240 // with data if enabled. This typically only matters during congestion.
241 // However, Linux seems to interleave under memory pressure regardless of
242 // this being enabled, so we have to handle it in the code anyways, so might
243 // as well turn it on all the time.
244 // TODO(Brian): Change this to 2 once we have kernels that support it, and
245 // also address the TODO in ProcessNotification to match on all the
246 // necessary fields.
247 int interleaving = 1;
Austin Schuh507f7582021-07-31 20:39:55 -0700248 PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_FRAGMENT_INTERLEAVE,
249 &interleaving, sizeof(interleaving)) == 0);
250 }
251 {
252 // Enable recvinfo when a packet arrives.
253 int on = 1;
254 PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_RECVRCVINFO, &on, sizeof(int)) ==
255 0);
256 }
257
Austin Schuha705d782021-07-31 20:40:00 -0700258 {
259 // TODO(austin): This is the old style registration... But, the sctp
260 // stack out in the wild for linux is old and primitive.
261 struct sctp_event_subscribe subscribe;
262 memset(&subscribe, 0, sizeof(subscribe));
263 subscribe.sctp_association_event = 1;
264 subscribe.sctp_stream_change_event = 1;
265 subscribe.sctp_partial_delivery_event = 1;
266 PCHECK(setsockopt(fd(), SOL_SCTP, SCTP_EVENTS, (char *)&subscribe,
267 sizeof(subscribe)) == 0);
268 }
269
Austin Schuh507f7582021-07-31 20:39:55 -0700270 DoSetMaxSize();
271}
272
273bool SctpReadWrite::SendMessage(
274 int stream, std::string_view data, int time_to_live,
275 std::optional<struct sockaddr_storage> sockaddr_remote,
276 sctp_assoc_t snd_assoc_id) {
277 CHECK(fd_ != -1);
278 struct iovec iov;
279 iov.iov_base = const_cast<char *>(data.data());
280 iov.iov_len = data.size();
281
282 // Use the assoc_id for the destination instead of the msg_name.
283 struct msghdr outmsg;
284 if (sockaddr_remote) {
285 outmsg.msg_name = &*sockaddr_remote;
286 outmsg.msg_namelen = sizeof(*sockaddr_remote);
287 VLOG(1) << "Sending to " << Address(*sockaddr_remote);
288 } else {
289 outmsg.msg_namelen = 0;
290 }
291
292 // Data to send.
293 outmsg.msg_iov = &iov;
294 outmsg.msg_iovlen = 1;
295
296 // Build up the sndinfo message.
297 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
298 outmsg.msg_control = outcmsg;
299 outmsg.msg_controllen = sizeof(outcmsg);
300 outmsg.msg_flags = 0;
301
302 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&outmsg);
303 cmsg->cmsg_level = IPPROTO_SCTP;
304 cmsg->cmsg_type = SCTP_SNDRCV;
305 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
306
307 struct sctp_sndrcvinfo *sinfo =
308 reinterpret_cast<struct sctp_sndrcvinfo *>(CMSG_DATA(cmsg));
309 memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
310 sinfo->sinfo_ppid = ++send_ppid_;
311 sinfo->sinfo_stream = stream;
312 sinfo->sinfo_flags = 0;
313 sinfo->sinfo_assoc_id = snd_assoc_id;
314 sinfo->sinfo_timetolive = time_to_live;
315
316 // And send.
317 const ssize_t size = sendmsg(fd_, &outmsg, MSG_NOSIGNAL | MSG_DONTWAIT);
318 if (size == -1) {
319 if (errno == EPIPE || errno == EAGAIN || errno == ESHUTDOWN ||
320 errno == EINTR) {
321 return false;
322 }
323 PLOG(FATAL) << "sendmsg on sctp socket failed";
324 return false;
325 }
326 CHECK_EQ(static_cast<ssize_t>(data.size()), size);
327 VLOG(1) << "Sent " << data.size();
328 return true;
329}
330
Austin Schuha705d782021-07-31 20:40:00 -0700331// We read each fragment into a fresh Message, because most of them won't be
332// fragmented. If we do end up with a fragment, then we copy the data out of it.
Austin Schuh507f7582021-07-31 20:39:55 -0700333aos::unique_c_ptr<Message> SctpReadWrite::ReadMessage() {
334 CHECK(fd_ != -1);
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800335
Austin Schuha705d782021-07-31 20:40:00 -0700336 while (true) {
337 aos::unique_c_ptr<Message> result(
338 reinterpret_cast<Message *>(malloc(sizeof(Message) + max_size_ + 1)));
339
Austin Schuh05c18122021-07-31 20:39:47 -0700340 struct msghdr inmessage;
Austin Schuhc4202572021-03-31 21:06:55 -0700341 memset(&inmessage, 0, sizeof(struct msghdr));
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800342
Austin Schuh05c18122021-07-31 20:39:47 -0700343 struct iovec iov;
Austin Schuha705d782021-07-31 20:40:00 -0700344 iov.iov_len = max_size_ + 1;
345 iov.iov_base = result->mutable_data();
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800346
Austin Schuhc4202572021-03-31 21:06:55 -0700347 inmessage.msg_iov = &iov;
348 inmessage.msg_iovlen = 1;
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800349
Austin Schuh05c18122021-07-31 20:39:47 -0700350 char incmsg[CMSG_SPACE(sizeof(_sctp_cmsg_data_t))];
Austin Schuhc4202572021-03-31 21:06:55 -0700351 inmessage.msg_control = incmsg;
352 inmessage.msg_controllen = sizeof(incmsg);
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800353
Austin Schuhc4202572021-03-31 21:06:55 -0700354 inmessage.msg_namelen = sizeof(struct sockaddr_storage);
355 inmessage.msg_name = &result->sin;
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800356
Austin Schuha705d782021-07-31 20:40:00 -0700357 const ssize_t size = recvmsg(fd_, &inmessage, MSG_DONTWAIT);
358 if (size == -1) {
359 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
360 // These are all non-fatal failures indicating we should retry later.
361 return nullptr;
Austin Schuhc4202572021-03-31 21:06:55 -0700362 }
Austin Schuha705d782021-07-31 20:40:00 -0700363 PLOG(FATAL) << "recvmsg on sctp socket " << fd_ << " failed";
Austin Schuhc4202572021-03-31 21:06:55 -0700364 }
365
Austin Schuha705d782021-07-31 20:40:00 -0700366 CHECK(!(inmessage.msg_flags & MSG_CTRUNC))
Austin Schuhc4202572021-03-31 21:06:55 -0700367 << ": Control message truncated.";
368
Austin Schuha705d782021-07-31 20:40:00 -0700369 CHECK_LE(size, static_cast<ssize_t>(max_size_))
Austin Schuh507f7582021-07-31 20:39:55 -0700370 << ": Message overflowed buffer on stream "
371 << result->header.rcvinfo.rcv_sid << ".";
Austin Schuhc4202572021-03-31 21:06:55 -0700372
Austin Schuha705d782021-07-31 20:40:00 -0700373 result->size = size;
374 if (MSG_NOTIFICATION & inmessage.msg_flags) {
375 result->message_type = Message::kNotification;
376 } else {
377 result->message_type = Message::kMessage;
378 }
379 result->partial_deliveries = 0;
Austin Schuhc4202572021-03-31 21:06:55 -0700380
Austin Schuha705d782021-07-31 20:40:00 -0700381 {
382 bool found_rcvinfo = false;
383 for (struct cmsghdr *scmsg = CMSG_FIRSTHDR(&inmessage); scmsg != NULL;
384 scmsg = CMSG_NXTHDR(&inmessage, scmsg)) {
385 switch (scmsg->cmsg_type) {
386 case SCTP_RCVINFO: {
387 CHECK(!found_rcvinfo);
388 found_rcvinfo = true;
389 result->header.rcvinfo =
390 *reinterpret_cast<struct sctp_rcvinfo *>(CMSG_DATA(scmsg));
391 } break;
392 default:
393 LOG(INFO) << "\tUnknown type: " << scmsg->cmsg_type;
394 break;
395 }
396 }
397 CHECK_EQ(found_rcvinfo, result->message_type == Message::kMessage)
398 << ": Failed to find a SCTP_RCVINFO cmsghdr. flags: "
399 << inmessage.msg_flags;
400 }
401 if (result->message_type == Message::kNotification) {
402 // Notifications are never fragmented, just return it now.
403 CHECK(inmessage.msg_flags & MSG_EOR)
404 << ": Notifications should never be big enough to fragment";
405 if (ProcessNotification(result.get())) {
406 // We handled this notification internally, so don't pass it on.
407 return nullptr;
408 }
409 return result;
410 }
411
412 auto partial_message_iterator =
413 std::find_if(partial_messages_.begin(), partial_messages_.end(),
414 [&result](const aos::unique_c_ptr<Message> &candidate) {
415 return result->header.rcvinfo.rcv_sid ==
416 candidate->header.rcvinfo.rcv_sid &&
417 result->header.rcvinfo.rcv_ssn ==
418 candidate->header.rcvinfo.rcv_ssn &&
419 result->header.rcvinfo.rcv_assoc_id ==
420 candidate->header.rcvinfo.rcv_assoc_id;
421 });
422 if (partial_message_iterator != partial_messages_.end()) {
423 const aos::unique_c_ptr<Message> &partial_message =
424 *partial_message_iterator;
425 // Verify it's really part of the same message.
426 CHECK_EQ(partial_message->message_type, result->message_type)
427 << ": for " << result->header.rcvinfo.rcv_sid << ","
428 << result->header.rcvinfo.rcv_ssn << ","
429 << result->header.rcvinfo.rcv_assoc_id;
430 CHECK_EQ(partial_message->header.rcvinfo.rcv_ppid,
431 result->header.rcvinfo.rcv_ppid)
432 << ": for " << result->header.rcvinfo.rcv_sid << ","
433 << result->header.rcvinfo.rcv_ssn << ","
434 << result->header.rcvinfo.rcv_assoc_id;
435
436 // Now copy the data over and update the size.
437 CHECK_LE(partial_message->size + result->size, max_size_)
438 << ": Assembled fragments overflowed buffer on stream "
439 << result->header.rcvinfo.rcv_sid << ".";
440 memcpy(partial_message->mutable_data() + partial_message->size,
441 result->data(), result->size);
442 ++partial_message->partial_deliveries;
443 VLOG(1) << "Merged fragment of " << result->size << " after "
444 << partial_message->size << ", had "
445 << partial_message->partial_deliveries
446 << ", for: " << result->header.rcvinfo.rcv_sid << ","
447 << result->header.rcvinfo.rcv_ssn << ","
448 << result->header.rcvinfo.rcv_assoc_id;
449 partial_message->size += result->size;
450 result.reset();
451 }
452
453 if (inmessage.msg_flags & MSG_EOR) {
454 // This is the last fragment, so we have something to return.
455 if (partial_message_iterator != partial_messages_.end()) {
456 // It was already merged into the message in the list, so now we pull
457 // that out of the list and return it.
458 CHECK(!result);
459 result = std::move(*partial_message_iterator);
460 partial_messages_.erase(partial_message_iterator);
461 VLOG(1) << "Final count: " << (result->partial_deliveries + 1)
462 << ", size: " << result->size
463 << ", for: " << result->header.rcvinfo.rcv_sid << ","
464 << result->header.rcvinfo.rcv_ssn << ","
465 << result->header.rcvinfo.rcv_assoc_id;
466 }
467 CHECK(result);
468 return result;
469 }
470 if (partial_message_iterator == partial_messages_.end()) {
471 VLOG(1) << "Starting fragment for: " << result->header.rcvinfo.rcv_sid
472 << "," << result->header.rcvinfo.rcv_ssn << ","
473 << result->header.rcvinfo.rcv_assoc_id;
474 // Need to record this as the first fragment.
475 partial_messages_.emplace_back(std::move(result));
476 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800477 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800478}
479
Austin Schuh507f7582021-07-31 20:39:55 -0700480void SctpReadWrite::CloseSocket() {
481 if (fd_ == -1) {
482 return;
483 }
484 LOG(INFO) << "close(" << fd_ << ")";
485 PCHECK(close(fd_) == 0);
486 fd_ = -1;
487}
488
489void SctpReadWrite::DoSetMaxSize() {
Austin Schuh61224882021-10-11 18:21:11 -0700490 size_t max_size = max_size_;
Austin Schuh507f7582021-07-31 20:39:55 -0700491
Austin Schuh61224882021-10-11 18:21:11 -0700492 // This sets the max packet size that we can send.
Austin Schuh507f7582021-07-31 20:39:55 -0700493 CHECK_GE(ReadWMemMax(), max_size)
494 << "wmem_max is too low. To increase wmem_max temporarily, do sysctl "
495 "-w net.core.wmem_max="
496 << max_size;
Austin Schuh507f7582021-07-31 20:39:55 -0700497 PCHECK(setsockopt(fd(), SOL_SOCKET, SO_SNDBUF, &max_size, sizeof(max_size)) ==
498 0);
Austin Schuh61224882021-10-11 18:21:11 -0700499
500 // The SO_RCVBUF option (also controlled by net.core.rmem_default) needs to be
501 // decently large but the actual size can be measured by tuning. The defaults
502 // should be fine. If it isn't big enough, transmission will fail.
Austin Schuh507f7582021-07-31 20:39:55 -0700503}
504
Austin Schuha705d782021-07-31 20:40:00 -0700505bool SctpReadWrite::ProcessNotification(const Message *message) {
506 const union sctp_notification *const snp =
507 reinterpret_cast<const union sctp_notification *>(message->data());
508 switch (snp->sn_header.sn_type) {
509 case SCTP_PARTIAL_DELIVERY_EVENT: {
510 const struct sctp_pdapi_event *const partial_delivery =
511 &snp->sn_pdapi_event;
512 CHECK_EQ(partial_delivery->pdapi_length, sizeof(*partial_delivery))
513 << ": Kernel's SCTP code is not a version we support";
514 switch (partial_delivery->pdapi_indication) {
515 case SCTP_PARTIAL_DELIVERY_ABORTED: {
516 const auto iterator = std::find_if(
517 partial_messages_.begin(), partial_messages_.end(),
518 [partial_delivery](const aos::unique_c_ptr<Message> &candidate) {
519 // TODO(Brian): Once we have new enough userpace headers, for
520 // kernels that support level-2 interleaving, we'll need to add
521 // this:
522 // candidate->header.rcvinfo.rcv_sid ==
523 // partial_delivery->pdapi_stream &&
524 // candidate->header.rcvinfo.rcv_ssn ==
525 // partial_delivery->pdapi_seq &&
526 return candidate->header.rcvinfo.rcv_assoc_id ==
527 partial_delivery->pdapi_assoc_id;
528 });
529 CHECK(iterator != partial_messages_.end())
530 << ": Got out of sync with the kernel for "
531 << partial_delivery->pdapi_assoc_id;
532 VLOG(1) << "Pruning partial delivery for "
533 << iterator->get()->header.rcvinfo.rcv_sid << ","
534 << iterator->get()->header.rcvinfo.rcv_ssn << ","
535 << iterator->get()->header.rcvinfo.rcv_assoc_id;
536 partial_messages_.erase(iterator);
537 }
538 return true;
539 }
540 } break;
541 }
542 return false;
543}
544
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800545void Message::LogRcvInfo() const {
546 LOG(INFO) << "\tSNDRCV (stream=" << header.rcvinfo.rcv_sid
547 << " ssn=" << header.rcvinfo.rcv_ssn
548 << " tsn=" << header.rcvinfo.rcv_tsn << " flags=0x" << std::hex
549 << header.rcvinfo.rcv_flags << std::dec
550 << " ppid=" << header.rcvinfo.rcv_ppid
551 << " cumtsn=" << header.rcvinfo.rcv_cumtsn << ")";
552}
553
Austin Schuh2fe4b712020-03-15 14:21:45 -0700554size_t ReadRMemMax() {
555 struct stat current_stat;
556 if (stat("/proc/sys/net/core/rmem_max", &current_stat) != -1) {
557 return static_cast<size_t>(
558 std::stoi(util::ReadFileToStringOrDie("/proc/sys/net/core/rmem_max")));
559 } else {
560 LOG(WARNING) << "/proc/sys/net/core/rmem_max doesn't exist. Are you in a "
561 "container?";
562 return 212992;
563 }
564}
565
566size_t ReadWMemMax() {
567 struct stat current_stat;
568 if (stat("/proc/sys/net/core/wmem_max", &current_stat) != -1) {
569 return static_cast<size_t>(
570 std::stoi(util::ReadFileToStringOrDie("/proc/sys/net/core/wmem_max")));
571 } else {
572 LOG(WARNING) << "/proc/sys/net/core/wmem_max doesn't exist. Are you in a "
573 "container?";
574 return 212992;
575 }
576}
577
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800578} // namespace message_bridge
579} // namespace aos