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