OLD | NEW |
1 /* | 1 /* |
2 * libjingle | 2 * libjingle |
3 * Copyright 2012 Google Inc. and Robin Seggelmann | 3 * Copyright 2012 Google Inc. and Robin Seggelmann |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 } // namespace | 102 } // namespace |
103 | 103 |
104 namespace cricket { | 104 namespace cricket { |
105 typedef rtc::ScopedMessageData<SctpInboundPacket> InboundPacketMessage; | 105 typedef rtc::ScopedMessageData<SctpInboundPacket> InboundPacketMessage; |
106 typedef rtc::ScopedMessageData<rtc::Buffer> OutboundPacketMessage; | 106 typedef rtc::ScopedMessageData<rtc::Buffer> OutboundPacketMessage; |
107 | 107 |
108 // The biggest SCTP packet. Starting from a 'safe' wire MTU value of 1280, | 108 // The biggest SCTP packet. Starting from a 'safe' wire MTU value of 1280, |
109 // take off 80 bytes for DTLS/TURN/TCP/IP overhead. | 109 // take off 80 bytes for DTLS/TURN/TCP/IP overhead. |
110 static const size_t kSctpMtu = 1200; | 110 static const size_t kSctpMtu = 1200; |
111 | 111 |
| 112 // The size of the SCTP association send buffer. 256kB, the usrsctp default. |
| 113 static const int kSendBufferSize = 262144; |
112 enum { | 114 enum { |
113 MSG_SCTPINBOUNDPACKET = 1, // MessageData is SctpInboundPacket | 115 MSG_SCTPINBOUNDPACKET = 1, // MessageData is SctpInboundPacket |
114 MSG_SCTPOUTBOUNDPACKET = 2, // MessageData is rtc:Buffer | 116 MSG_SCTPOUTBOUNDPACKET = 2, // MessageData is rtc:Buffer |
115 }; | 117 }; |
116 | 118 |
117 struct SctpInboundPacket { | 119 struct SctpInboundPacket { |
118 rtc::Buffer buffer; | 120 rtc::Buffer buffer; |
119 ReceiveDataParams params; | 121 ReceiveDataParams params; |
120 // The |flags| parameter is used by SCTP to distinguish notification packets | 122 // The |flags| parameter is used by SCTP to distinguish notification packets |
121 // from other types of packets. | 123 // from other types of packets. |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 case SctpDataMediaChannel::PPID_NONE: | 172 case SctpDataMediaChannel::PPID_NONE: |
171 *dest = cricket::DMT_NONE; | 173 *dest = cricket::DMT_NONE; |
172 return true; | 174 return true; |
173 | 175 |
174 default: | 176 default: |
175 return false; | 177 return false; |
176 } | 178 } |
177 } | 179 } |
178 | 180 |
179 // Log the packet in text2pcap format, if log level is at LS_VERBOSE. | 181 // Log the packet in text2pcap format, if log level is at LS_VERBOSE. |
180 static void VerboseLogPacket(void *addr, size_t length, int direction) { | 182 static void VerboseLogPacket(void *data, size_t length, int direction) { |
181 if (LOG_CHECK_LEVEL(LS_VERBOSE) && length > 0) { | 183 if (LOG_CHECK_LEVEL(LS_VERBOSE) && length > 0) { |
182 char *dump_buf; | 184 char *dump_buf; |
183 if ((dump_buf = usrsctp_dumppacket( | 185 if ((dump_buf = usrsctp_dumppacket( |
184 addr, length, direction)) != NULL) { | 186 data, length, direction)) != NULL) { |
185 LOG(LS_VERBOSE) << dump_buf; | 187 LOG(LS_VERBOSE) << dump_buf; |
186 usrsctp_freedumpbuffer(dump_buf); | 188 usrsctp_freedumpbuffer(dump_buf); |
187 } | 189 } |
188 } | 190 } |
189 } | 191 } |
190 | 192 |
191 // This is the callback usrsctp uses when there's data to send on the network | 193 // This is the callback usrsctp uses when there's data to send on the network |
192 // that has been wrapped appropriatly for the SCTP protocol. | 194 // that has been wrapped appropriatly for the SCTP protocol. |
193 static int OnSctpOutboundPacket(void* addr, void* data, size_t length, | 195 static int OnSctpOutboundPacket(void* addr, void* data, size_t length, |
194 uint8_t tos, uint8_t set_df) { | 196 uint8_t tos, uint8_t set_df) { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 // AF_CONN use of sctp. | 253 // AF_CONN use of sctp. |
252 usrsctp_init(0, cricket::OnSctpOutboundPacket, debug_sctp_printf); | 254 usrsctp_init(0, cricket::OnSctpOutboundPacket, debug_sctp_printf); |
253 | 255 |
254 // To turn on/off detailed SCTP debugging. You will also need to have the | 256 // To turn on/off detailed SCTP debugging. You will also need to have the |
255 // SCTP_DEBUG cpp defines flag. | 257 // SCTP_DEBUG cpp defines flag. |
256 // usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_ALL); | 258 // usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_ALL); |
257 | 259 |
258 // TODO(ldixon): Consider turning this on/off. | 260 // TODO(ldixon): Consider turning this on/off. |
259 usrsctp_sysctl_set_sctp_ecn_enable(0); | 261 usrsctp_sysctl_set_sctp_ecn_enable(0); |
260 | 262 |
| 263 // This is harmless, but we should find out when the library default |
| 264 // changes. |
| 265 int send_size = usrsctp_sysctl_get_sctp_sendspace(); |
| 266 if (send_size != kSendBufferSize) { |
| 267 LOG(LS_ERROR) << "Got different send size than expected: " << send_size; |
| 268 } |
| 269 |
261 // TODO(ldixon): Consider turning this on/off. | 270 // TODO(ldixon): Consider turning this on/off. |
262 // This is not needed right now (we don't do dynamic address changes): | 271 // This is not needed right now (we don't do dynamic address changes): |
263 // If SCTP Auto-ASCONF is enabled, the peer is informed automatically | 272 // If SCTP Auto-ASCONF is enabled, the peer is informed automatically |
264 // when a new address is added or removed. This feature is enabled by | 273 // when a new address is added or removed. This feature is enabled by |
265 // default. | 274 // default. |
266 // usrsctp_sysctl_set_sctp_auto_asconf(0); | 275 // usrsctp_sysctl_set_sctp_auto_asconf(0); |
267 | 276 |
268 // TODO(ldixon): Consider turning this on/off. | 277 // TODO(ldixon): Consider turning this on/off. |
269 // Add a blackhole sysctl. Setting it to 1 results in no ABORTs | 278 // Add a blackhole sysctl. Setting it to 1 results in no ABORTs |
270 // being sent in response to INITs, setting it to 2 results | 279 // being sent in response to INITs, setting it to 2 results |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 } | 317 } |
309 | 318 |
310 DataMediaChannel* SctpDataEngine::CreateChannel( | 319 DataMediaChannel* SctpDataEngine::CreateChannel( |
311 DataChannelType data_channel_type) { | 320 DataChannelType data_channel_type) { |
312 if (data_channel_type != DCT_SCTP) { | 321 if (data_channel_type != DCT_SCTP) { |
313 return NULL; | 322 return NULL; |
314 } | 323 } |
315 return new SctpDataMediaChannel(rtc::Thread::Current()); | 324 return new SctpDataMediaChannel(rtc::Thread::Current()); |
316 } | 325 } |
317 | 326 |
| 327 // static |
| 328 SctpDataMediaChannel* SctpDataEngine::GetChannelFromSocket( |
| 329 struct socket* sock) { |
| 330 struct sockaddr* addrs = nullptr; |
| 331 int naddrs = usrsctp_getladdrs(sock, 0, &addrs); |
| 332 if (naddrs <= 0 || addrs[0].sa_family != AF_CONN) { |
| 333 return nullptr; |
| 334 } |
| 335 // usrsctp_getladdrs() returns the addresses bound to this socket, which |
| 336 // contains the SctpDataMediaChannel* as sconn_addr. Read the pointer, |
| 337 // then free the list of addresses once we have the pointer. We only open |
| 338 // AF_CONN sockets, and they should all have the sconn_addr set to the |
| 339 // pointer that created them, so [0] is as good as any other. |
| 340 struct sockaddr_conn* sconn = |
| 341 reinterpret_cast<struct sockaddr_conn*>(&addrs[0]); |
| 342 SctpDataMediaChannel* channel = |
| 343 reinterpret_cast<SctpDataMediaChannel*>(sconn->sconn_addr); |
| 344 usrsctp_freeladdrs(addrs); |
| 345 |
| 346 return channel; |
| 347 } |
| 348 |
| 349 // static |
| 350 int SctpDataEngine::SendThresholdCallback(struct socket* sock, |
| 351 uint32_t sb_free) { |
| 352 // Fired on our I/O thread. SctpDataMediaChannel::OnPacketReceived() gets |
| 353 // a packet containing acknowledgments, which goes into usrsctp_conninput, |
| 354 // and then back here. |
| 355 SctpDataMediaChannel* channel = GetChannelFromSocket(sock); |
| 356 if (!channel) { |
| 357 LOG(LS_ERROR) << "SendThresholdCallback: Failed to get channel for socket " |
| 358 << sock; |
| 359 return 0; |
| 360 } |
| 361 channel->OnSendThresholdCallback(); |
| 362 return 0; |
| 363 } |
| 364 |
318 SctpDataMediaChannel::SctpDataMediaChannel(rtc::Thread* thread) | 365 SctpDataMediaChannel::SctpDataMediaChannel(rtc::Thread* thread) |
319 : worker_thread_(thread), | 366 : worker_thread_(thread), |
320 local_port_(kSctpDefaultPort), | 367 local_port_(kSctpDefaultPort), |
321 remote_port_(kSctpDefaultPort), | 368 remote_port_(kSctpDefaultPort), |
322 sock_(NULL), | 369 sock_(NULL), |
323 sending_(false), | 370 sending_(false), |
324 receiving_(false), | 371 receiving_(false), |
325 debug_name_("SctpDataMediaChannel") { | 372 debug_name_("SctpDataMediaChannel") { |
326 } | 373 } |
327 | 374 |
328 SctpDataMediaChannel::~SctpDataMediaChannel() { | 375 SctpDataMediaChannel::~SctpDataMediaChannel() { |
329 CloseSctpSocket(); | 376 CloseSctpSocket(); |
330 } | 377 } |
331 | 378 |
| 379 void SctpDataMediaChannel::OnSendThresholdCallback() { |
| 380 DCHECK(rtc::Thread::Current() == worker_thread_); |
| 381 SignalReadyToSend(true); |
| 382 } |
| 383 |
332 sockaddr_conn SctpDataMediaChannel::GetSctpSockAddr(int port) { | 384 sockaddr_conn SctpDataMediaChannel::GetSctpSockAddr(int port) { |
333 sockaddr_conn sconn = {0}; | 385 sockaddr_conn sconn = {0}; |
334 sconn.sconn_family = AF_CONN; | 386 sconn.sconn_family = AF_CONN; |
335 #ifdef HAVE_SCONN_LEN | 387 #ifdef HAVE_SCONN_LEN |
336 sconn.sconn_len = sizeof(sockaddr_conn); | 388 sconn.sconn_len = sizeof(sockaddr_conn); |
337 #endif | 389 #endif |
338 // Note: conversion from int to uint16_t happens here. | 390 // Note: conversion from int to uint16_t happens here. |
339 sconn.sconn_port = rtc::HostToNetwork16(port); | 391 sconn.sconn_port = rtc::HostToNetwork16(port); |
340 sconn.sconn_addr = this; | 392 sconn.sconn_addr = this; |
341 return sconn; | 393 return sconn; |
342 } | 394 } |
343 | 395 |
344 bool SctpDataMediaChannel::OpenSctpSocket() { | 396 bool SctpDataMediaChannel::OpenSctpSocket() { |
345 if (sock_) { | 397 if (sock_) { |
346 LOG(LS_VERBOSE) << debug_name_ | 398 LOG(LS_VERBOSE) << debug_name_ |
347 << "->Ignoring attempt to re-create existing socket."; | 399 << "->Ignoring attempt to re-create existing socket."; |
348 return false; | 400 return false; |
349 } | 401 } |
| 402 |
| 403 // If kSendBufferSize isn't reflective of reality, we log an error, but we |
| 404 // still have to do something reasonable here. Look up what the buffer's |
| 405 // real size is and set our threshold to something reasonable. |
| 406 const static int kSendThreshold = usrsctp_sysctl_get_sctp_sendspace() / 2; |
| 407 |
350 sock_ = usrsctp_socket(AF_CONN, SOCK_STREAM, IPPROTO_SCTP, | 408 sock_ = usrsctp_socket(AF_CONN, SOCK_STREAM, IPPROTO_SCTP, |
351 cricket::OnSctpInboundPacket, NULL, 0, this); | 409 cricket::OnSctpInboundPacket, |
| 410 &SctpDataEngine::SendThresholdCallback, |
| 411 kSendThreshold, this); |
352 if (!sock_) { | 412 if (!sock_) { |
353 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to create SCTP socket."; | 413 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to create SCTP socket."; |
354 return false; | 414 return false; |
355 } | 415 } |
356 | 416 |
357 // Make the socket non-blocking. Connect, close, shutdown etc will not block | 417 // Make the socket non-blocking. Connect, close, shutdown etc will not block |
358 // the thread waiting for the socket operation to complete. | 418 // the thread waiting for the socket operation to complete. |
359 if (usrsctp_set_non_blocking(sock_, 1) < 0) { | 419 if (usrsctp_set_non_blocking(sock_, 1) < 0) { |
360 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to set SCTP to non blocking."; | 420 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to set SCTP to non blocking."; |
361 return false; | 421 return false; |
(...skipping 24 matching lines...) Expand all Loading... |
386 | 446 |
387 // Nagle. | 447 // Nagle. |
388 uint32_t nodelay = 1; | 448 uint32_t nodelay = 1; |
389 if (usrsctp_setsockopt(sock_, IPPROTO_SCTP, SCTP_NODELAY, &nodelay, | 449 if (usrsctp_setsockopt(sock_, IPPROTO_SCTP, SCTP_NODELAY, &nodelay, |
390 sizeof(nodelay))) { | 450 sizeof(nodelay))) { |
391 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to set SCTP_NODELAY."; | 451 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to set SCTP_NODELAY."; |
392 return false; | 452 return false; |
393 } | 453 } |
394 | 454 |
395 // Disable MTU discovery | 455 // Disable MTU discovery |
396 struct sctp_paddrparams params = {{0}}; | 456 sctp_paddrparams params = {{0}}; |
397 params.spp_assoc_id = 0; | 457 params.spp_assoc_id = 0; |
398 params.spp_flags = SPP_PMTUD_DISABLE; | 458 params.spp_flags = SPP_PMTUD_DISABLE; |
399 params.spp_pathmtu = kSctpMtu; | 459 params.spp_pathmtu = kSctpMtu; |
400 if (usrsctp_setsockopt(sock_, IPPROTO_SCTP, SCTP_PEER_ADDR_PARAMS, ¶ms, | 460 if (usrsctp_setsockopt(sock_, IPPROTO_SCTP, SCTP_PEER_ADDR_PARAMS, ¶ms, |
401 sizeof(params))) { | 461 sizeof(params))) { |
402 LOG_ERRNO(LS_ERROR) << debug_name_ | 462 LOG_ERRNO(LS_ERROR) << debug_name_ |
403 << "Failed to set SCTP_PEER_ADDR_PARAMS."; | 463 << "Failed to set SCTP_PEER_ADDR_PARAMS."; |
404 return false; | 464 return false; |
405 } | 465 } |
406 | 466 |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
591 if (result) { | 651 if (result) { |
592 // Only way out now is success. | 652 // Only way out now is success. |
593 *result = SDR_SUCCESS; | 653 *result = SDR_SUCCESS; |
594 } | 654 } |
595 return true; | 655 return true; |
596 } | 656 } |
597 | 657 |
598 // Called by network interface when a packet has been received. | 658 // Called by network interface when a packet has been received. |
599 void SctpDataMediaChannel::OnPacketReceived( | 659 void SctpDataMediaChannel::OnPacketReceived( |
600 rtc::Buffer* packet, const rtc::PacketTime& packet_time) { | 660 rtc::Buffer* packet, const rtc::PacketTime& packet_time) { |
| 661 DCHECK(rtc::Thread::Current() == worker_thread_); |
601 LOG(LS_VERBOSE) << debug_name_ << "->OnPacketReceived(...): " | 662 LOG(LS_VERBOSE) << debug_name_ << "->OnPacketReceived(...): " |
602 << " length=" << packet->size() << ", sending: " << sending_; | 663 << " length=" << packet->size() << ", sending: " << sending_; |
603 // Only give receiving packets to usrsctp after if connected. This enables two | 664 // Only give receiving packets to usrsctp after if connected. This enables two |
604 // peers to each make a connect call, but for them not to receive an INIT | 665 // peers to each make a connect call, but for them not to receive an INIT |
605 // packet before they have called connect; least the last receiver of the INIT | 666 // packet before they have called connect; least the last receiver of the INIT |
606 // packet will have called connect, and a connection will be established. | 667 // packet will have called connect, and a connection will be established. |
607 if (sending_) { | 668 if (sending_) { |
608 // Pass received packet to SCTP stack. Once processed by usrsctp, the data | 669 // Pass received packet to SCTP stack. Once processed by usrsctp, the data |
609 // will be will be given to the global OnSctpInboundData, and then, | 670 // will be will be given to the global OnSctpInboundData, and then, |
610 // marshalled by a Post and handled with OnMessage. | 671 // marshalled by a Post and handled with OnMessage. |
611 | |
612 VerboseLogPacket(packet->data(), packet->size(), SCTP_DUMP_INBOUND); | 672 VerboseLogPacket(packet->data(), packet->size(), SCTP_DUMP_INBOUND); |
613 usrsctp_conninput(this, packet->data(), packet->size(), 0); | 673 usrsctp_conninput(this, packet->data(), packet->size(), 0); |
614 } else { | 674 } else { |
615 // TODO(ldixon): Consider caching the packet for very slightly better | 675 // TODO(ldixon): Consider caching the packet for very slightly better |
616 // reliability. | 676 // reliability. |
617 } | 677 } |
618 } | 678 } |
619 | 679 |
620 void SctpDataMediaChannel::OnInboundPacketFromSctpToChannel( | 680 void SctpDataMediaChannel::OnInboundPacketFromSctpToChannel( |
621 SctpInboundPacket* packet) { | 681 SctpInboundPacket* packet) { |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
897 } | 957 } |
898 | 958 |
899 bool SctpDataMediaChannel::SetRecvCodecs(const std::vector<DataCodec>& codecs) { | 959 bool SctpDataMediaChannel::SetRecvCodecs(const std::vector<DataCodec>& codecs) { |
900 return GetCodecIntParameter( | 960 return GetCodecIntParameter( |
901 codecs, kGoogleSctpDataCodecId, kGoogleSctpDataCodecName, kCodecParamPort, | 961 codecs, kGoogleSctpDataCodecId, kGoogleSctpDataCodecName, kCodecParamPort, |
902 &local_port_); | 962 &local_port_); |
903 } | 963 } |
904 | 964 |
905 void SctpDataMediaChannel::OnPacketFromSctpToNetwork( | 965 void SctpDataMediaChannel::OnPacketFromSctpToNetwork( |
906 rtc::Buffer* buffer) { | 966 rtc::Buffer* buffer) { |
907 if (buffer->size() > kSctpMtu) { | 967 // usrsctp seems to interpret the MTU we give it strangely -- it seems to |
| 968 // give us back packets bigger than that MTU, if only by a fixed amount. |
| 969 // This is that amount that we've observed. |
| 970 const int kSctpOverhead = 76; |
| 971 if (buffer->size() > (kSctpOverhead + kSctpMtu)) { |
908 LOG(LS_ERROR) << debug_name_ << "->OnPacketFromSctpToNetwork(...): " | 972 LOG(LS_ERROR) << debug_name_ << "->OnPacketFromSctpToNetwork(...): " |
909 << "SCTP seems to have made a packet that is bigger " | 973 << "SCTP seems to have made a packet that is bigger " |
910 "than its official MTU."; | 974 << "than its official MTU: " << buffer->size() |
| 975 << " vs max of " << kSctpMtu |
| 976 << " even after adding " << kSctpOverhead |
| 977 << " extra SCTP overhead"; |
911 } | 978 } |
912 MediaChannel::SendPacket(buffer); | 979 MediaChannel::SendPacket(buffer); |
913 } | 980 } |
914 | 981 |
915 bool SctpDataMediaChannel::SendQueuedStreamResets() { | 982 bool SctpDataMediaChannel::SendQueuedStreamResets() { |
916 if (!sent_reset_streams_.empty() || queued_reset_streams_.empty()) | 983 if (!sent_reset_streams_.empty() || queued_reset_streams_.empty()) |
917 return true; | 984 return true; |
918 | 985 |
919 LOG(LS_VERBOSE) << "SendQueuedStreamResets[" << debug_name_ << "]: Sending [" | 986 LOG(LS_VERBOSE) << "SendQueuedStreamResets[" << debug_name_ << "]: Sending [" |
920 << ListStreams(queued_reset_streams_) << "], Open: [" | 987 << ListStreams(queued_reset_streams_) << "], Open: [" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
962 } | 1029 } |
963 case MSG_SCTPOUTBOUNDPACKET: { | 1030 case MSG_SCTPOUTBOUNDPACKET: { |
964 rtc::scoped_ptr<OutboundPacketMessage> pdata( | 1031 rtc::scoped_ptr<OutboundPacketMessage> pdata( |
965 static_cast<OutboundPacketMessage*>(msg->pdata)); | 1032 static_cast<OutboundPacketMessage*>(msg->pdata)); |
966 OnPacketFromSctpToNetwork(pdata->data().get()); | 1033 OnPacketFromSctpToNetwork(pdata->data().get()); |
967 break; | 1034 break; |
968 } | 1035 } |
969 } | 1036 } |
970 } | 1037 } |
971 } // namespace cricket | 1038 } // namespace cricket |
OLD | NEW |