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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
237 // The ownership of |packet| transfers to |msg|. | 239 // The ownership of |packet| transfers to |msg|. |
238 InboundPacketMessage* msg = new InboundPacketMessage(packet); | 240 InboundPacketMessage* msg = new InboundPacketMessage(packet); |
239 channel->worker_thread()->Post(channel, MSG_SCTPINBOUNDPACKET, msg); | 241 channel->worker_thread()->Post(channel, MSG_SCTPINBOUNDPACKET, msg); |
240 } | 242 } |
241 free(data); | 243 free(data); |
242 return 1; | 244 return 1; |
243 } | 245 } |
244 | 246 |
245 // Set the initial value of the static SCTP Data Engines reference count. | 247 // Set the initial value of the static SCTP Data Engines reference count. |
246 int SctpDataEngine::usrsctp_engines_count = 0; | 248 int SctpDataEngine::usrsctp_engines_count = 0; |
249 // Our registrar of sockets to their channels. Used for callbacks. | |
pthatcher1
2015/08/24 19:09:31
This might be more clear as "All the channels crea
lally1
2015/08/25 16:01:56
Done.
| |
250 std::vector<SctpDataMediaChannel*> SctpDataEngine::channels_; | |
247 | 251 |
248 SctpDataEngine::SctpDataEngine() { | 252 SctpDataEngine::SctpDataEngine() { |
249 if (usrsctp_engines_count == 0) { | 253 if (usrsctp_engines_count == 0) { |
250 // First argument is udp_encapsulation_port, which is not releveant for our | 254 // First argument is udp_encapsulation_port, which is not releveant for our |
251 // AF_CONN use of sctp. | 255 // AF_CONN use of sctp. |
252 usrsctp_init(0, cricket::OnSctpOutboundPacket, debug_sctp_printf); | 256 usrsctp_init(0, cricket::OnSctpOutboundPacket, debug_sctp_printf); |
253 | 257 |
254 // To turn on/off detailed SCTP debugging. You will also need to have the | 258 // To turn on/off detailed SCTP debugging. You will also need to have the |
255 // SCTP_DEBUG cpp defines flag. | 259 // SCTP_DEBUG cpp defines flag. |
256 // usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_ALL); | 260 // usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_ALL); |
257 | 261 |
258 // TODO(ldixon): Consider turning this on/off. | 262 // TODO(ldixon): Consider turning this on/off. |
259 usrsctp_sysctl_set_sctp_ecn_enable(0); | 263 usrsctp_sysctl_set_sctp_ecn_enable(0); |
260 | 264 |
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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
305 } | 314 } |
306 LOG(LS_ERROR) << "Failed to shutdown usrsctp."; | 315 LOG(LS_ERROR) << "Failed to shutdown usrsctp."; |
307 } | 316 } |
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 SctpDataMediaChannel *channel = new SctpDataMediaChannel( |
325 rtc::Thread::Current()); | |
326 channels_.push_back(channel); | |
327 channel->SignalDestroyed.connect(this, &SctpDataEngine::OnChannelDestroyed); | |
328 return channel; | |
316 } | 329 } |
317 | 330 |
331 void SctpDataEngine::OnChannelDestroyed(SctpDataMediaChannel* channel) { | |
332 auto it = std::find(channels_.begin(), channels_.end(), channel); | |
333 if (it == channels_.end()) { | |
334 LOG(LS_ERROR) << "OnChannelDestroyed: the channel wasn't registered."; | |
335 return; | |
336 } | |
337 channels_.erase(it); | |
338 } | |
339 | |
340 // static | |
pthatcher1
2015/08/24 19:09:31
Is it really static?
lally1
2015/08/25 16:01:56
Yup.
| |
341 int SctpDataEngine::SendThresholdCallback(struct socket* sock, | |
342 uint32_t sb_free) { | |
343 for (auto p:channels_) { | |
344 if (p->socket() == sock) { | |
345 p->SignalReadyToSend(true); | |
346 return 0; | |
347 } | |
pthatcher1
2015/08/24 19:09:31
Would a GetChannelFromSocket method make sense?
lally1
2015/08/25 16:01:56
I added it in. Whether it makes sense: it feels l
| |
348 } | |
349 LOG(LS_ERROR) << "SendThresholdCallback: Failed to get channel for socket " | |
350 << sock; | |
351 return 0; | |
352 } | |
353 | |
354 | |
318 SctpDataMediaChannel::SctpDataMediaChannel(rtc::Thread* thread) | 355 SctpDataMediaChannel::SctpDataMediaChannel(rtc::Thread* thread) |
319 : worker_thread_(thread), | 356 : worker_thread_(thread), |
320 local_port_(kSctpDefaultPort), | 357 local_port_(kSctpDefaultPort), |
321 remote_port_(kSctpDefaultPort), | 358 remote_port_(kSctpDefaultPort), |
322 sock_(NULL), | 359 sock_(NULL), |
323 sending_(false), | 360 sending_(false), |
324 receiving_(false), | 361 receiving_(false), |
325 debug_name_("SctpDataMediaChannel") { | 362 debug_name_("SctpDataMediaChannel") { |
326 } | 363 } |
327 | 364 |
328 SctpDataMediaChannel::~SctpDataMediaChannel() { | 365 SctpDataMediaChannel::~SctpDataMediaChannel() { |
329 CloseSctpSocket(); | 366 CloseSctpSocket(); |
367 SignalDestroyed(this); | |
330 } | 368 } |
331 | 369 |
332 sockaddr_conn SctpDataMediaChannel::GetSctpSockAddr(int port) { | 370 sockaddr_conn SctpDataMediaChannel::GetSctpSockAddr(int port) { |
333 sockaddr_conn sconn = {0}; | 371 sockaddr_conn sconn = {0}; |
334 sconn.sconn_family = AF_CONN; | 372 sconn.sconn_family = AF_CONN; |
335 #ifdef HAVE_SCONN_LEN | 373 #ifdef HAVE_SCONN_LEN |
336 sconn.sconn_len = sizeof(sockaddr_conn); | 374 sconn.sconn_len = sizeof(sockaddr_conn); |
337 #endif | 375 #endif |
338 // Note: conversion from int to uint16_t happens here. | 376 // Note: conversion from int to uint16_t happens here. |
339 sconn.sconn_port = rtc::HostToNetwork16(port); | 377 sconn.sconn_port = rtc::HostToNetwork16(port); |
340 sconn.sconn_addr = this; | 378 sconn.sconn_addr = this; |
341 return sconn; | 379 return sconn; |
342 } | 380 } |
343 | 381 |
344 bool SctpDataMediaChannel::OpenSctpSocket() { | 382 bool SctpDataMediaChannel::OpenSctpSocket() { |
345 if (sock_) { | 383 if (sock_) { |
346 LOG(LS_VERBOSE) << debug_name_ | 384 LOG(LS_VERBOSE) << debug_name_ |
347 << "->Ignoring attempt to re-create existing socket."; | 385 << "->Ignoring attempt to re-create existing socket."; |
348 return false; | 386 return false; |
349 } | 387 } |
388 | |
389 // If kSendBufferSize isn't reflective of reality, we log an error, still | |
pthatcher1
2015/08/24 19:09:31
still => but we still
?
lally1
2015/08/25 16:01:56
Done.
| |
390 // have to do something reasonable here. Look up what the buffer's real | |
391 // size is and set our threshold to something reasonable. | |
392 const static int send_threshold = usrsctp_sysctl_get_sctp_sendspace() / 2; | |
393 | |
350 sock_ = usrsctp_socket(AF_CONN, SOCK_STREAM, IPPROTO_SCTP, | 394 sock_ = usrsctp_socket(AF_CONN, SOCK_STREAM, IPPROTO_SCTP, |
351 cricket::OnSctpInboundPacket, NULL, 0, this); | 395 cricket::OnSctpInboundPacket, |
396 &SctpDataEngine::SendThresholdCallback, | |
397 send_threshold, this); | |
352 if (!sock_) { | 398 if (!sock_) { |
353 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to create SCTP socket."; | 399 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to create SCTP socket."; |
354 return false; | 400 return false; |
355 } | 401 } |
356 | 402 |
357 // Make the socket non-blocking. Connect, close, shutdown etc will not block | 403 // Make the socket non-blocking. Connect, close, shutdown etc will not block |
358 // the thread waiting for the socket operation to complete. | 404 // the thread waiting for the socket operation to complete. |
359 if (usrsctp_set_non_blocking(sock_, 1) < 0) { | 405 if (usrsctp_set_non_blocking(sock_, 1) < 0) { |
360 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to set SCTP to non blocking."; | 406 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to set SCTP to non blocking."; |
361 return false; | 407 return false; |
(...skipping 24 matching lines...) Expand all Loading... | |
386 | 432 |
387 // Nagle. | 433 // Nagle. |
388 uint32_t nodelay = 1; | 434 uint32_t nodelay = 1; |
389 if (usrsctp_setsockopt(sock_, IPPROTO_SCTP, SCTP_NODELAY, &nodelay, | 435 if (usrsctp_setsockopt(sock_, IPPROTO_SCTP, SCTP_NODELAY, &nodelay, |
390 sizeof(nodelay))) { | 436 sizeof(nodelay))) { |
391 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to set SCTP_NODELAY."; | 437 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to set SCTP_NODELAY."; |
392 return false; | 438 return false; |
393 } | 439 } |
394 | 440 |
395 // Disable MTU discovery | 441 // Disable MTU discovery |
396 struct sctp_paddrparams params = {{0}}; | 442 struct sctp_paddrparams params; |
443 memset(¶ms, 0, sizeof(params)); | |
397 params.spp_assoc_id = 0; | 444 params.spp_assoc_id = 0; |
398 params.spp_flags = SPP_PMTUD_DISABLE; | 445 params.spp_flags = SPP_PMTUD_DISABLE; |
399 params.spp_pathmtu = kSctpMtu; | 446 params.spp_pathmtu = kSctpMtu; |
400 if (usrsctp_setsockopt(sock_, IPPROTO_SCTP, SCTP_PEER_ADDR_PARAMS, ¶ms, | 447 if (usrsctp_setsockopt(sock_, IPPROTO_SCTP, SCTP_PEER_ADDR_PARAMS, ¶ms, |
401 sizeof(params))) { | 448 sizeof(params))) { |
402 LOG_ERRNO(LS_ERROR) << debug_name_ | 449 LOG_ERRNO(LS_ERROR) << debug_name_ |
403 << "Failed to set SCTP_PEER_ADDR_PARAMS."; | 450 << "Failed to set SCTP_PEER_ADDR_PARAMS."; |
404 return false; | 451 return false; |
405 } | 452 } |
406 | 453 |
(...skipping 490 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
897 } | 944 } |
898 | 945 |
899 bool SctpDataMediaChannel::SetRecvCodecs(const std::vector<DataCodec>& codecs) { | 946 bool SctpDataMediaChannel::SetRecvCodecs(const std::vector<DataCodec>& codecs) { |
900 return GetCodecIntParameter( | 947 return GetCodecIntParameter( |
901 codecs, kGoogleSctpDataCodecId, kGoogleSctpDataCodecName, kCodecParamPort, | 948 codecs, kGoogleSctpDataCodecId, kGoogleSctpDataCodecName, kCodecParamPort, |
902 &local_port_); | 949 &local_port_); |
903 } | 950 } |
904 | 951 |
905 void SctpDataMediaChannel::OnPacketFromSctpToNetwork( | 952 void SctpDataMediaChannel::OnPacketFromSctpToNetwork( |
906 rtc::Buffer* buffer) { | 953 rtc::Buffer* buffer) { |
907 if (buffer->size() > kSctpMtu) { | 954 // usrsctp seems to interpret the MTU we give it strangely -- it seems to |
955 // give us back packets bigger than that MTU, if only by a fixed amount. | |
956 // This is that amount that we've observed. | |
957 const int kSctpOverhead = 76; | |
958 if (buffer->size() > (kSctpOverhead + kSctpMtu)) { | |
908 LOG(LS_ERROR) << debug_name_ << "->OnPacketFromSctpToNetwork(...): " | 959 LOG(LS_ERROR) << debug_name_ << "->OnPacketFromSctpToNetwork(...): " |
909 << "SCTP seems to have made a packet that is bigger " | 960 << "SCTP seems to have made a packet that is bigger " |
910 "than its official MTU."; | 961 << "than its official MTU: " << buffer->size() |
962 << " vs max of " << kSctpMtu | |
963 << " even after adding " << kSctpOverhead | |
964 << " extra SCTP overhead"; | |
911 } | 965 } |
912 MediaChannel::SendPacket(buffer); | 966 MediaChannel::SendPacket(buffer); |
913 } | 967 } |
914 | 968 |
915 bool SctpDataMediaChannel::SendQueuedStreamResets() { | 969 bool SctpDataMediaChannel::SendQueuedStreamResets() { |
916 if (!sent_reset_streams_.empty() || queued_reset_streams_.empty()) | 970 if (!sent_reset_streams_.empty() || queued_reset_streams_.empty()) |
917 return true; | 971 return true; |
918 | 972 |
919 LOG(LS_VERBOSE) << "SendQueuedStreamResets[" << debug_name_ << "]: Sending [" | 973 LOG(LS_VERBOSE) << "SendQueuedStreamResets[" << debug_name_ << "]: Sending [" |
920 << ListStreams(queued_reset_streams_) << "], Open: [" | 974 << ListStreams(queued_reset_streams_) << "], Open: [" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
962 } | 1016 } |
963 case MSG_SCTPOUTBOUNDPACKET: { | 1017 case MSG_SCTPOUTBOUNDPACKET: { |
964 rtc::scoped_ptr<OutboundPacketMessage> pdata( | 1018 rtc::scoped_ptr<OutboundPacketMessage> pdata( |
965 static_cast<OutboundPacketMessage*>(msg->pdata)); | 1019 static_cast<OutboundPacketMessage*>(msg->pdata)); |
966 OnPacketFromSctpToNetwork(pdata->data().get()); | 1020 OnPacketFromSctpToNetwork(pdata->data().get()); |
967 break; | 1021 break; |
968 } | 1022 } |
969 } | 1023 } |
970 } | 1024 } |
971 } // namespace cricket | 1025 } // namespace cricket |
OLD | NEW |