Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 1 | #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 Schuh | 2fe4b71 | 2020-03-15 14:21:45 -0700 | [diff] [blame] | 7 | #include <sys/stat.h> |
| 8 | #include <sys/types.h> |
| 9 | #include <unistd.h> |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 10 | |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 11 | #include <algorithm> |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 12 | #include <string_view> |
| 13 | |
Austin Schuh | 2fe4b71 | 2020-03-15 14:21:45 -0700 | [diff] [blame] | 14 | #include "aos/util/file.h" |
| 15 | |
Austin Schuh | 0a0a827 | 2021-12-08 13:19:32 -0800 | [diff] [blame] | 16 | DEFINE_string(interface, "", "network interface"); |
Brian Silverman | 0c6d44e | 2021-11-10 12:27:49 -0800 | [diff] [blame] | 17 | DEFINE_bool(disable_ipv6, false, "disable ipv6"); |
Austin Schuh | 9dd8f59 | 2021-12-25 14:32:43 -0800 | [diff] [blame] | 18 | DEFINE_int32(rmem, 0, "If nonzero, set rmem to this size."); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 19 | |
| 20 | namespace aos { |
| 21 | namespace message_bridge { |
| 22 | |
| 23 | namespace { |
| 24 | const char *sac_state_tbl[] = {"COMMUNICATION_UP", "COMMUNICATION_LOST", |
| 25 | "RESTART", "SHUTDOWN_COMPLETE", |
| 26 | "CANT_START_ASSOCICATION"}; |
| 27 | |
| 28 | typedef union { |
| 29 | struct sctp_initmsg init; |
| 30 | struct sctp_sndrcvinfo sndrcvinfo; |
| 31 | } _sctp_cmsg_data_t; |
| 32 | |
| 33 | } // namespace |
| 34 | |
Austin Schuh | 0a0a827 | 2021-12-08 13:19:32 -0800 | [diff] [blame] | 35 | bool 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 | |
| 56 | struct sockaddr_storage ResolveSocket(std::string_view host, int port, |
| 57 | bool use_ipv6) { |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 58 | 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 Silverman | 0c6d44e | 2021-11-10 12:27:49 -0800 | [diff] [blame] | 62 | struct addrinfo hints; |
| 63 | memset(&hints, 0, sizeof(hints)); |
Austin Schuh | 0a0a827 | 2021-12-08 13:19:32 -0800 | [diff] [blame] | 64 | if (!use_ipv6) { |
Brian Silverman | 0c6d44e | 2021-11-10 12:27:49 -0800 | [diff] [blame] | 65 | hints.ai_family = AF_INET; |
| 66 | } else { |
Austin Schuh | 0a0a827 | 2021-12-08 13:19:32 -0800 | [diff] [blame] | 67 | // Default to IPv6 as the clearly superior protocol, since it also handles |
| 68 | // IPv4. |
Brian Silverman | 0c6d44e | 2021-11-10 12:27:49 -0800 | [diff] [blame] | 69 | 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 Silverman | 0c6d44e | 2021-11-10 12:27:49 -0800 | [diff] [blame] | 80 | 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 Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 86 | 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 | |
| 125 | std::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 | } |
| 134 | std::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 | |
| 147 | void 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 Schuh | f777700 | 2020-09-01 18:41:28 -0700 | [diff] [blame] | 181 | case SCTP_STREAM_CHANGE_EVENT: { |
Austin Schuh | 62a0c27 | 2021-03-31 21:04:53 -0700 | [diff] [blame] | 182 | const struct sctp_stream_change_event *sce = &snp->sn_strchange_event; |
Austin Schuh | f777700 | 2020-09-01 18:41:28 -0700 | [diff] [blame] | 183 | 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 Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 189 | 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 | |
| 198 | std::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 | |
| 205 | std::string Message::PeerAddress() const { return Address(sin); } |
| 206 | |
| 207 | void 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 Schuh | a5f545b | 2021-07-31 20:39:42 -0700 | [diff] [blame] | 213 | 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 Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 220 | |
| 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 Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 233 | void 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 Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 241 | // 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 Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 249 | 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 Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 259 | { |
| 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 Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 271 | DoSetMaxSize(); |
| 272 | } |
| 273 | |
| 274 | bool 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 Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 332 | // 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 Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 334 | aos::unique_c_ptr<Message> SctpReadWrite::ReadMessage() { |
| 335 | CHECK(fd_ != -1); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 336 | |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 337 | while (true) { |
| 338 | aos::unique_c_ptr<Message> result( |
| 339 | reinterpret_cast<Message *>(malloc(sizeof(Message) + max_size_ + 1))); |
| 340 | |
Austin Schuh | 05c1812 | 2021-07-31 20:39:47 -0700 | [diff] [blame] | 341 | struct msghdr inmessage; |
Austin Schuh | c420257 | 2021-03-31 21:06:55 -0700 | [diff] [blame] | 342 | memset(&inmessage, 0, sizeof(struct msghdr)); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 343 | |
Austin Schuh | 05c1812 | 2021-07-31 20:39:47 -0700 | [diff] [blame] | 344 | struct iovec iov; |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 345 | iov.iov_len = max_size_ + 1; |
| 346 | iov.iov_base = result->mutable_data(); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 347 | |
Austin Schuh | c420257 | 2021-03-31 21:06:55 -0700 | [diff] [blame] | 348 | inmessage.msg_iov = &iov; |
| 349 | inmessage.msg_iovlen = 1; |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 350 | |
Austin Schuh | 05c1812 | 2021-07-31 20:39:47 -0700 | [diff] [blame] | 351 | char incmsg[CMSG_SPACE(sizeof(_sctp_cmsg_data_t))]; |
Austin Schuh | c420257 | 2021-03-31 21:06:55 -0700 | [diff] [blame] | 352 | inmessage.msg_control = incmsg; |
| 353 | inmessage.msg_controllen = sizeof(incmsg); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 354 | |
Austin Schuh | c420257 | 2021-03-31 21:06:55 -0700 | [diff] [blame] | 355 | inmessage.msg_namelen = sizeof(struct sockaddr_storage); |
| 356 | inmessage.msg_name = &result->sin; |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 357 | |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 358 | 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 Schuh | c420257 | 2021-03-31 21:06:55 -0700 | [diff] [blame] | 363 | } |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 364 | PLOG(FATAL) << "recvmsg on sctp socket " << fd_ << " failed"; |
Austin Schuh | c420257 | 2021-03-31 21:06:55 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 367 | CHECK(!(inmessage.msg_flags & MSG_CTRUNC)) |
Austin Schuh | c420257 | 2021-03-31 21:06:55 -0700 | [diff] [blame] | 368 | << ": Control message truncated."; |
| 369 | |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 370 | CHECK_LE(size, static_cast<ssize_t>(max_size_)) |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 371 | << ": Message overflowed buffer on stream " |
| 372 | << result->header.rcvinfo.rcv_sid << "."; |
Austin Schuh | c420257 | 2021-03-31 21:06:55 -0700 | [diff] [blame] | 373 | |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 374 | 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 Schuh | c420257 | 2021-03-31 21:06:55 -0700 | [diff] [blame] | 381 | |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 382 | { |
| 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 Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 478 | } |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 479 | } |
| 480 | |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 481 | void SctpReadWrite::CloseSocket() { |
| 482 | if (fd_ == -1) { |
| 483 | return; |
| 484 | } |
| 485 | LOG(INFO) << "close(" << fd_ << ")"; |
| 486 | PCHECK(close(fd_) == 0); |
| 487 | fd_ = -1; |
| 488 | } |
| 489 | |
| 490 | void SctpReadWrite::DoSetMaxSize() { |
Austin Schuh | 6122488 | 2021-10-11 18:21:11 -0700 | [diff] [blame] | 491 | size_t max_size = max_size_; |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 492 | |
Austin Schuh | 6122488 | 2021-10-11 18:21:11 -0700 | [diff] [blame] | 493 | // This sets the max packet size that we can send. |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 494 | 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 Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 498 | PCHECK(setsockopt(fd(), SOL_SOCKET, SO_SNDBUF, &max_size, sizeof(max_size)) == |
| 499 | 0); |
Austin Schuh | 6122488 | 2021-10-11 18:21:11 -0700 | [diff] [blame] | 500 | |
| 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 Schuh | 9dd8f59 | 2021-12-25 14:32:43 -0800 | [diff] [blame] | 504 | if (FLAGS_rmem > 0) { |
| 505 | size_t rmem = FLAGS_rmem; |
| 506 | PCHECK(setsockopt(fd(), SOL_SOCKET, SO_RCVBUF, &rmem, sizeof(rmem)) == 0); |
| 507 | } |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Austin Schuh | a705d78 | 2021-07-31 20:40:00 -0700 | [diff] [blame] | 510 | bool 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 Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 550 | void 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 Schuh | 2fe4b71 | 2020-03-15 14:21:45 -0700 | [diff] [blame] | 559 | size_t ReadRMemMax() { |
| 560 | struct stat current_stat; |
| 561 | if (stat("/proc/sys/net/core/rmem_max", ¤t_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 | |
| 571 | size_t ReadWMemMax() { |
| 572 | struct stat current_stat; |
| 573 | if (stat("/proc/sys/net/core/wmem_max", ¤t_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 Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 583 | } // namespace message_bridge |
| 584 | } // namespace aos |