blob: b9840c66b954515fd8b415255a4e917800cc7757 [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");
17
18namespace aos {
19namespace message_bridge {
20
21namespace {
22const char *sac_state_tbl[] = {"COMMUNICATION_UP", "COMMUNICATION_LOST",
23 "RESTART", "SHUTDOWN_COMPLETE",
24 "CANT_START_ASSOCICATION"};
25
26typedef union {
27 struct sctp_initmsg init;
28 struct sctp_sndrcvinfo sndrcvinfo;
29} _sctp_cmsg_data_t;
30
31} // namespace
32
33struct sockaddr_storage ResolveSocket(std::string_view host, int port) {
34 struct sockaddr_storage result;
35 struct addrinfo *addrinfo_result;
36 struct sockaddr_in *t_addr = (struct sockaddr_in *)&result;
37 struct sockaddr_in6 *t_addr6 = (struct sockaddr_in6 *)&result;
38
Austin Schuh6d227942020-02-22 13:29:57 -080039 PCHECK(getaddrinfo(std::string(host).c_str(), 0, NULL, &addrinfo_result) == 0)
40 << ": Failed to look up " << host;
Austin Schuhe84c3ed2019-12-14 15:29:48 -080041
42 switch (addrinfo_result->ai_family) {
43 case AF_INET:
44 memcpy(t_addr, addrinfo_result->ai_addr, addrinfo_result->ai_addrlen);
45 t_addr->sin_family = addrinfo_result->ai_family;
46 t_addr->sin_port = htons(port);
47
48 break;
49 case AF_INET6:
50 memcpy(t_addr6, addrinfo_result->ai_addr, addrinfo_result->ai_addrlen);
51 t_addr6->sin6_family = addrinfo_result->ai_family;
52 t_addr6->sin6_port = htons(port);
53
54 if (FLAGS_interface.size() > 0) {
55 t_addr6->sin6_scope_id = if_nametoindex(FLAGS_interface.c_str());
56 }
57
58 break;
59 }
60
61 // Now print it back out nicely.
62 char host_string[NI_MAXHOST];
63 char service_string[NI_MAXSERV];
64
65 int error = getnameinfo((struct sockaddr *)&result,
66 addrinfo_result->ai_addrlen, host_string, NI_MAXHOST,
67 service_string, NI_MAXSERV, NI_NUMERICHOST);
68
69 if (error) {
70 LOG(ERROR) << "Reverse lookup failed ... " << gai_strerror(error);
71 }
72
73 LOG(INFO) << "remote:addr=" << host_string << ", port=" << service_string
74 << ", family=" << addrinfo_result->ai_family;
75
76 freeaddrinfo(addrinfo_result);
77
78 return result;
79}
80
81std::string_view Family(const struct sockaddr_storage &sockaddr) {
82 if (sockaddr.ss_family == AF_INET) {
83 return "AF_INET";
84 } else if (sockaddr.ss_family == AF_INET6) {
85 return "AF_INET6";
86 } else {
87 return "unknown";
88 }
89}
90std::string Address(const struct sockaddr_storage &sockaddr) {
91 char addrbuf[INET6_ADDRSTRLEN];
92 if (sockaddr.ss_family == AF_INET) {
93 const struct sockaddr_in *sin = (const struct sockaddr_in *)&sockaddr;
94 return std::string(
95 inet_ntop(AF_INET, &sin->sin_addr, addrbuf, INET6_ADDRSTRLEN));
96 } else {
97 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)&sockaddr;
98 return std::string(
99 inet_ntop(AF_INET6, &sin6->sin6_addr, addrbuf, INET6_ADDRSTRLEN));
100 }
101}
102
103void PrintNotification(const Message *msg) {
104 const union sctp_notification *snp =
105 (const union sctp_notification *)msg->data();
106
107 LOG(INFO) << "Notification:";
108
109 switch (snp->sn_header.sn_type) {
110 case SCTP_ASSOC_CHANGE: {
111 const struct sctp_assoc_change *sac = &snp->sn_assoc_change;
112 LOG(INFO) << "SCTP_ASSOC_CHANGE(" << sac_state_tbl[sac->sac_state] << ")";
113 VLOG(1) << " (assoc_change: state=" << sac->sac_state
114 << ", error=" << sac->sac_error
115 << ", instr=" << sac->sac_inbound_streams
116 << " outstr=" << sac->sac_outbound_streams
117 << ", assoc=" << sac->sac_assoc_id << ")";
118 } break;
119 case SCTP_PEER_ADDR_CHANGE: {
120 const struct sctp_paddr_change *spc = &snp->sn_paddr_change;
121 LOG(INFO) << " SlCTP_PEER_ADDR_CHANGE";
122 VLOG(1) << "\t\t(peer_addr_change: " << Address(spc->spc_aaddr)
123 << " state=" << spc->spc_state << ", error=" << spc->spc_error
124 << ")";
125 } break;
126 case SCTP_SEND_FAILED: {
127 const struct sctp_send_failed *ssf = &snp->sn_send_failed;
128 LOG(INFO) << " SCTP_SEND_FAILED";
129 VLOG(1) << "\t\t(sendfailed: len=" << ssf->ssf_length
130 << " err=" << ssf->ssf_error << ")";
131 } break;
132 case SCTP_REMOTE_ERROR: {
133 const struct sctp_remote_error *sre = &snp->sn_remote_error;
134 LOG(INFO) << " SCTP_REMOTE_ERROR";
135 VLOG(1) << "\t\t(remote_error: err=" << ntohs(sre->sre_error) << ")";
136 } break;
Austin Schuhf7777002020-09-01 18:41:28 -0700137 case SCTP_STREAM_CHANGE_EVENT: {
Austin Schuh62a0c272021-03-31 21:04:53 -0700138 const struct sctp_stream_change_event *sce = &snp->sn_strchange_event;
Austin Schuhf7777002020-09-01 18:41:28 -0700139 LOG(INFO) << " SCTP_STREAM_CHANGE_EVENT";
140 VLOG(1) << "\t\t(stream_change_event: flags=" << sce->strchange_flags
141 << ", assoc_id=" << sce->strchange_assoc_id
142 << ", instrms=" << sce->strchange_instrms
143 << ", outstrms=" << sce->strchange_outstrms << " )";
144 } break;
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800145 case SCTP_SHUTDOWN_EVENT: {
146 LOG(INFO) << " SCTP_SHUTDOWN_EVENT";
147 } break;
148 default:
149 LOG(INFO) << " Unknown type: " << snp->sn_header.sn_type;
150 break;
151 }
152}
153
154std::string GetHostname() {
155 char buf[256];
156 buf[sizeof(buf) - 1] = '\0';
157 PCHECK(gethostname(buf, sizeof(buf) - 1) == 0);
158 return buf;
159}
160
161std::string Message::PeerAddress() const { return Address(sin); }
162
163void LogSctpStatus(int fd, sctp_assoc_t assoc_id) {
164 struct sctp_status status;
165 memset(&status, 0, sizeof(status));
166 status.sstat_assoc_id = assoc_id;
167
168 socklen_t size = sizeof(status);
Austin Schuha5f545b2021-07-31 20:39:42 -0700169 const int result = getsockopt(fd, SOL_SCTP, SCTP_STATUS,
170 reinterpret_cast<void *>(&status), &size);
171 if (result == -1 && errno == EINVAL) {
172 LOG(INFO) << "sctp_status) not associated";
173 return;
174 }
175 PCHECK(result == 0);
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800176
177 LOG(INFO) << "sctp_status) sstat_assoc_id:" << status.sstat_assoc_id
178 << " sstat_state:" << status.sstat_state
179 << " sstat_rwnd:" << status.sstat_rwnd
180 << " sstat_unackdata:" << status.sstat_unackdata
181 << " sstat_penddata:" << status.sstat_penddata
182 << " sstat_instrms:" << status.sstat_instrms
183 << " sstat_outstrms:" << status.sstat_outstrms
184 << " sstat_fragmentation_point:" << status.sstat_fragmentation_point
185 << " sstat_primary.spinfo_srtt:" << status.sstat_primary.spinfo_srtt
186 << " sstat_primary.spinfo_rto:" << status.sstat_primary.spinfo_rto;
187}
188
Austin Schuh507f7582021-07-31 20:39:55 -0700189void SctpReadWrite::OpenSocket(const struct sockaddr_storage &sockaddr_local) {
190 fd_ = socket(sockaddr_local.ss_family, SOCK_SEQPACKET, IPPROTO_SCTP);
191 PCHECK(fd_ != -1);
192 LOG(INFO) << "socket(" << Family(sockaddr_local)
193 << ", SOCK_SEQPACKET, IPPROTOSCTP) = " << fd_;
194 {
195 // Per https://tools.ietf.org/html/rfc6458
196 // Setting this to !0 allows event notifications to be interleaved
Austin Schuha705d782021-07-31 20:40:00 -0700197 // with data if enabled. This typically only matters during congestion.
198 // However, Linux seems to interleave under memory pressure regardless of
199 // this being enabled, so we have to handle it in the code anyways, so might
200 // as well turn it on all the time.
201 // TODO(Brian): Change this to 2 once we have kernels that support it, and
202 // also address the TODO in ProcessNotification to match on all the
203 // necessary fields.
204 int interleaving = 1;
Austin Schuh507f7582021-07-31 20:39:55 -0700205 PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_FRAGMENT_INTERLEAVE,
206 &interleaving, sizeof(interleaving)) == 0);
207 }
208 {
209 // Enable recvinfo when a packet arrives.
210 int on = 1;
211 PCHECK(setsockopt(fd_, IPPROTO_SCTP, SCTP_RECVRCVINFO, &on, sizeof(int)) ==
212 0);
213 }
214
Austin Schuha705d782021-07-31 20:40:00 -0700215 {
216 // TODO(austin): This is the old style registration... But, the sctp
217 // stack out in the wild for linux is old and primitive.
218 struct sctp_event_subscribe subscribe;
219 memset(&subscribe, 0, sizeof(subscribe));
220 subscribe.sctp_association_event = 1;
221 subscribe.sctp_stream_change_event = 1;
222 subscribe.sctp_partial_delivery_event = 1;
223 PCHECK(setsockopt(fd(), SOL_SCTP, SCTP_EVENTS, (char *)&subscribe,
224 sizeof(subscribe)) == 0);
225 }
226
Austin Schuh507f7582021-07-31 20:39:55 -0700227 DoSetMaxSize();
228}
229
230bool SctpReadWrite::SendMessage(
231 int stream, std::string_view data, int time_to_live,
232 std::optional<struct sockaddr_storage> sockaddr_remote,
233 sctp_assoc_t snd_assoc_id) {
234 CHECK(fd_ != -1);
235 struct iovec iov;
236 iov.iov_base = const_cast<char *>(data.data());
237 iov.iov_len = data.size();
238
239 // Use the assoc_id for the destination instead of the msg_name.
240 struct msghdr outmsg;
241 if (sockaddr_remote) {
242 outmsg.msg_name = &*sockaddr_remote;
243 outmsg.msg_namelen = sizeof(*sockaddr_remote);
244 VLOG(1) << "Sending to " << Address(*sockaddr_remote);
245 } else {
246 outmsg.msg_namelen = 0;
247 }
248
249 // Data to send.
250 outmsg.msg_iov = &iov;
251 outmsg.msg_iovlen = 1;
252
253 // Build up the sndinfo message.
254 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
255 outmsg.msg_control = outcmsg;
256 outmsg.msg_controllen = sizeof(outcmsg);
257 outmsg.msg_flags = 0;
258
259 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&outmsg);
260 cmsg->cmsg_level = IPPROTO_SCTP;
261 cmsg->cmsg_type = SCTP_SNDRCV;
262 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
263
264 struct sctp_sndrcvinfo *sinfo =
265 reinterpret_cast<struct sctp_sndrcvinfo *>(CMSG_DATA(cmsg));
266 memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
267 sinfo->sinfo_ppid = ++send_ppid_;
268 sinfo->sinfo_stream = stream;
269 sinfo->sinfo_flags = 0;
270 sinfo->sinfo_assoc_id = snd_assoc_id;
271 sinfo->sinfo_timetolive = time_to_live;
272
273 // And send.
274 const ssize_t size = sendmsg(fd_, &outmsg, MSG_NOSIGNAL | MSG_DONTWAIT);
275 if (size == -1) {
276 if (errno == EPIPE || errno == EAGAIN || errno == ESHUTDOWN ||
277 errno == EINTR) {
278 return false;
279 }
280 PLOG(FATAL) << "sendmsg on sctp socket failed";
281 return false;
282 }
283 CHECK_EQ(static_cast<ssize_t>(data.size()), size);
284 VLOG(1) << "Sent " << data.size();
285 return true;
286}
287
Austin Schuha705d782021-07-31 20:40:00 -0700288// We read each fragment into a fresh Message, because most of them won't be
289// fragmented. If we do end up with a fragment, then we copy the data out of it.
Austin Schuh507f7582021-07-31 20:39:55 -0700290aos::unique_c_ptr<Message> SctpReadWrite::ReadMessage() {
291 CHECK(fd_ != -1);
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800292
Austin Schuha705d782021-07-31 20:40:00 -0700293 while (true) {
294 aos::unique_c_ptr<Message> result(
295 reinterpret_cast<Message *>(malloc(sizeof(Message) + max_size_ + 1)));
296
Austin Schuh05c18122021-07-31 20:39:47 -0700297 struct msghdr inmessage;
Austin Schuhc4202572021-03-31 21:06:55 -0700298 memset(&inmessage, 0, sizeof(struct msghdr));
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800299
Austin Schuh05c18122021-07-31 20:39:47 -0700300 struct iovec iov;
Austin Schuha705d782021-07-31 20:40:00 -0700301 iov.iov_len = max_size_ + 1;
302 iov.iov_base = result->mutable_data();
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800303
Austin Schuhc4202572021-03-31 21:06:55 -0700304 inmessage.msg_iov = &iov;
305 inmessage.msg_iovlen = 1;
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800306
Austin Schuh05c18122021-07-31 20:39:47 -0700307 char incmsg[CMSG_SPACE(sizeof(_sctp_cmsg_data_t))];
Austin Schuhc4202572021-03-31 21:06:55 -0700308 inmessage.msg_control = incmsg;
309 inmessage.msg_controllen = sizeof(incmsg);
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800310
Austin Schuhc4202572021-03-31 21:06:55 -0700311 inmessage.msg_namelen = sizeof(struct sockaddr_storage);
312 inmessage.msg_name = &result->sin;
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800313
Austin Schuha705d782021-07-31 20:40:00 -0700314 const ssize_t size = recvmsg(fd_, &inmessage, MSG_DONTWAIT);
315 if (size == -1) {
316 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
317 // These are all non-fatal failures indicating we should retry later.
318 return nullptr;
Austin Schuhc4202572021-03-31 21:06:55 -0700319 }
Austin Schuha705d782021-07-31 20:40:00 -0700320 PLOG(FATAL) << "recvmsg on sctp socket " << fd_ << " failed";
Austin Schuhc4202572021-03-31 21:06:55 -0700321 }
322
Austin Schuha705d782021-07-31 20:40:00 -0700323 CHECK(!(inmessage.msg_flags & MSG_CTRUNC))
Austin Schuhc4202572021-03-31 21:06:55 -0700324 << ": Control message truncated.";
325
Austin Schuha705d782021-07-31 20:40:00 -0700326 CHECK_LE(size, static_cast<ssize_t>(max_size_))
Austin Schuh507f7582021-07-31 20:39:55 -0700327 << ": Message overflowed buffer on stream "
328 << result->header.rcvinfo.rcv_sid << ".";
Austin Schuhc4202572021-03-31 21:06:55 -0700329
Austin Schuha705d782021-07-31 20:40:00 -0700330 result->size = size;
331 if (MSG_NOTIFICATION & inmessage.msg_flags) {
332 result->message_type = Message::kNotification;
333 } else {
334 result->message_type = Message::kMessage;
335 }
336 result->partial_deliveries = 0;
Austin Schuhc4202572021-03-31 21:06:55 -0700337
Austin Schuha705d782021-07-31 20:40:00 -0700338 {
339 bool found_rcvinfo = false;
340 for (struct cmsghdr *scmsg = CMSG_FIRSTHDR(&inmessage); scmsg != NULL;
341 scmsg = CMSG_NXTHDR(&inmessage, scmsg)) {
342 switch (scmsg->cmsg_type) {
343 case SCTP_RCVINFO: {
344 CHECK(!found_rcvinfo);
345 found_rcvinfo = true;
346 result->header.rcvinfo =
347 *reinterpret_cast<struct sctp_rcvinfo *>(CMSG_DATA(scmsg));
348 } break;
349 default:
350 LOG(INFO) << "\tUnknown type: " << scmsg->cmsg_type;
351 break;
352 }
353 }
354 CHECK_EQ(found_rcvinfo, result->message_type == Message::kMessage)
355 << ": Failed to find a SCTP_RCVINFO cmsghdr. flags: "
356 << inmessage.msg_flags;
357 }
358 if (result->message_type == Message::kNotification) {
359 // Notifications are never fragmented, just return it now.
360 CHECK(inmessage.msg_flags & MSG_EOR)
361 << ": Notifications should never be big enough to fragment";
362 if (ProcessNotification(result.get())) {
363 // We handled this notification internally, so don't pass it on.
364 return nullptr;
365 }
366 return result;
367 }
368
369 auto partial_message_iterator =
370 std::find_if(partial_messages_.begin(), partial_messages_.end(),
371 [&result](const aos::unique_c_ptr<Message> &candidate) {
372 return result->header.rcvinfo.rcv_sid ==
373 candidate->header.rcvinfo.rcv_sid &&
374 result->header.rcvinfo.rcv_ssn ==
375 candidate->header.rcvinfo.rcv_ssn &&
376 result->header.rcvinfo.rcv_assoc_id ==
377 candidate->header.rcvinfo.rcv_assoc_id;
378 });
379 if (partial_message_iterator != partial_messages_.end()) {
380 const aos::unique_c_ptr<Message> &partial_message =
381 *partial_message_iterator;
382 // Verify it's really part of the same message.
383 CHECK_EQ(partial_message->message_type, result->message_type)
384 << ": for " << result->header.rcvinfo.rcv_sid << ","
385 << result->header.rcvinfo.rcv_ssn << ","
386 << result->header.rcvinfo.rcv_assoc_id;
387 CHECK_EQ(partial_message->header.rcvinfo.rcv_ppid,
388 result->header.rcvinfo.rcv_ppid)
389 << ": for " << result->header.rcvinfo.rcv_sid << ","
390 << result->header.rcvinfo.rcv_ssn << ","
391 << result->header.rcvinfo.rcv_assoc_id;
392
393 // Now copy the data over and update the size.
394 CHECK_LE(partial_message->size + result->size, max_size_)
395 << ": Assembled fragments overflowed buffer on stream "
396 << result->header.rcvinfo.rcv_sid << ".";
397 memcpy(partial_message->mutable_data() + partial_message->size,
398 result->data(), result->size);
399 ++partial_message->partial_deliveries;
400 VLOG(1) << "Merged fragment of " << result->size << " after "
401 << partial_message->size << ", had "
402 << partial_message->partial_deliveries
403 << ", for: " << result->header.rcvinfo.rcv_sid << ","
404 << result->header.rcvinfo.rcv_ssn << ","
405 << result->header.rcvinfo.rcv_assoc_id;
406 partial_message->size += result->size;
407 result.reset();
408 }
409
410 if (inmessage.msg_flags & MSG_EOR) {
411 // This is the last fragment, so we have something to return.
412 if (partial_message_iterator != partial_messages_.end()) {
413 // It was already merged into the message in the list, so now we pull
414 // that out of the list and return it.
415 CHECK(!result);
416 result = std::move(*partial_message_iterator);
417 partial_messages_.erase(partial_message_iterator);
418 VLOG(1) << "Final count: " << (result->partial_deliveries + 1)
419 << ", size: " << result->size
420 << ", for: " << result->header.rcvinfo.rcv_sid << ","
421 << result->header.rcvinfo.rcv_ssn << ","
422 << result->header.rcvinfo.rcv_assoc_id;
423 }
424 CHECK(result);
425 return result;
426 }
427 if (partial_message_iterator == partial_messages_.end()) {
428 VLOG(1) << "Starting fragment for: " << result->header.rcvinfo.rcv_sid
429 << "," << result->header.rcvinfo.rcv_ssn << ","
430 << result->header.rcvinfo.rcv_assoc_id;
431 // Need to record this as the first fragment.
432 partial_messages_.emplace_back(std::move(result));
433 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800434 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800435}
436
Austin Schuh507f7582021-07-31 20:39:55 -0700437void SctpReadWrite::CloseSocket() {
438 if (fd_ == -1) {
439 return;
440 }
441 LOG(INFO) << "close(" << fd_ << ")";
442 PCHECK(close(fd_) == 0);
443 fd_ = -1;
444}
445
446void SctpReadWrite::DoSetMaxSize() {
Austin Schuh61224882021-10-11 18:21:11 -0700447 size_t max_size = max_size_;
Austin Schuh507f7582021-07-31 20:39:55 -0700448
Austin Schuh61224882021-10-11 18:21:11 -0700449 // This sets the max packet size that we can send.
Austin Schuh507f7582021-07-31 20:39:55 -0700450 CHECK_GE(ReadWMemMax(), max_size)
451 << "wmem_max is too low. To increase wmem_max temporarily, do sysctl "
452 "-w net.core.wmem_max="
453 << max_size;
Austin Schuh507f7582021-07-31 20:39:55 -0700454 PCHECK(setsockopt(fd(), SOL_SOCKET, SO_SNDBUF, &max_size, sizeof(max_size)) ==
455 0);
Austin Schuh61224882021-10-11 18:21:11 -0700456
457 // The SO_RCVBUF option (also controlled by net.core.rmem_default) needs to be
458 // decently large but the actual size can be measured by tuning. The defaults
459 // should be fine. If it isn't big enough, transmission will fail.
Austin Schuh507f7582021-07-31 20:39:55 -0700460}
461
Austin Schuha705d782021-07-31 20:40:00 -0700462bool SctpReadWrite::ProcessNotification(const Message *message) {
463 const union sctp_notification *const snp =
464 reinterpret_cast<const union sctp_notification *>(message->data());
465 switch (snp->sn_header.sn_type) {
466 case SCTP_PARTIAL_DELIVERY_EVENT: {
467 const struct sctp_pdapi_event *const partial_delivery =
468 &snp->sn_pdapi_event;
469 CHECK_EQ(partial_delivery->pdapi_length, sizeof(*partial_delivery))
470 << ": Kernel's SCTP code is not a version we support";
471 switch (partial_delivery->pdapi_indication) {
472 case SCTP_PARTIAL_DELIVERY_ABORTED: {
473 const auto iterator = std::find_if(
474 partial_messages_.begin(), partial_messages_.end(),
475 [partial_delivery](const aos::unique_c_ptr<Message> &candidate) {
476 // TODO(Brian): Once we have new enough userpace headers, for
477 // kernels that support level-2 interleaving, we'll need to add
478 // this:
479 // candidate->header.rcvinfo.rcv_sid ==
480 // partial_delivery->pdapi_stream &&
481 // candidate->header.rcvinfo.rcv_ssn ==
482 // partial_delivery->pdapi_seq &&
483 return candidate->header.rcvinfo.rcv_assoc_id ==
484 partial_delivery->pdapi_assoc_id;
485 });
486 CHECK(iterator != partial_messages_.end())
487 << ": Got out of sync with the kernel for "
488 << partial_delivery->pdapi_assoc_id;
489 VLOG(1) << "Pruning partial delivery for "
490 << iterator->get()->header.rcvinfo.rcv_sid << ","
491 << iterator->get()->header.rcvinfo.rcv_ssn << ","
492 << iterator->get()->header.rcvinfo.rcv_assoc_id;
493 partial_messages_.erase(iterator);
494 }
495 return true;
496 }
497 } break;
498 }
499 return false;
500}
501
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800502void Message::LogRcvInfo() const {
503 LOG(INFO) << "\tSNDRCV (stream=" << header.rcvinfo.rcv_sid
504 << " ssn=" << header.rcvinfo.rcv_ssn
505 << " tsn=" << header.rcvinfo.rcv_tsn << " flags=0x" << std::hex
506 << header.rcvinfo.rcv_flags << std::dec
507 << " ppid=" << header.rcvinfo.rcv_ppid
508 << " cumtsn=" << header.rcvinfo.rcv_cumtsn << ")";
509}
510
Austin Schuh2fe4b712020-03-15 14:21:45 -0700511size_t ReadRMemMax() {
512 struct stat current_stat;
513 if (stat("/proc/sys/net/core/rmem_max", &current_stat) != -1) {
514 return static_cast<size_t>(
515 std::stoi(util::ReadFileToStringOrDie("/proc/sys/net/core/rmem_max")));
516 } else {
517 LOG(WARNING) << "/proc/sys/net/core/rmem_max doesn't exist. Are you in a "
518 "container?";
519 return 212992;
520 }
521}
522
523size_t ReadWMemMax() {
524 struct stat current_stat;
525 if (stat("/proc/sys/net/core/wmem_max", &current_stat) != -1) {
526 return static_cast<size_t>(
527 std::stoi(util::ReadFileToStringOrDie("/proc/sys/net/core/wmem_max")));
528 } else {
529 LOG(WARNING) << "/proc/sys/net/core/wmem_max doesn't exist. Are you in a "
530 "container?";
531 return 212992;
532 }
533}
534
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800535} // namespace message_bridge
536} // namespace aos