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