Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: talk/media/sctp/sctpdataengine.cc

Issue 1266033005: Added send-thresholding and fixed text packet dumping. Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Added a test and more pthatcher@ comments. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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
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 // All the channels created by this engine, used for callbacks from
250 // usrsctplib that only contain socket pointers. Channels are removed when
251 // SignalDestroyed is fired.
252 std::vector<SctpDataMediaChannel*> SctpDataEngine::channels_;
tommi 2015/08/27 11:38:02 how is thread safety guaranteed?
lally1 2015/08/27 17:56:08 Accesses to the member are via message-posting to
247 253
248 SctpDataEngine::SctpDataEngine() { 254 SctpDataEngine::SctpDataEngine() {
249 if (usrsctp_engines_count == 0) { 255 if (usrsctp_engines_count == 0) {
250 // First argument is udp_encapsulation_port, which is not releveant for our 256 // First argument is udp_encapsulation_port, which is not releveant for our
251 // AF_CONN use of sctp. 257 // AF_CONN use of sctp.
252 usrsctp_init(0, cricket::OnSctpOutboundPacket, debug_sctp_printf); 258 usrsctp_init(0, cricket::OnSctpOutboundPacket, debug_sctp_printf);
253 259
254 // To turn on/off detailed SCTP debugging. You will also need to have the 260 // To turn on/off detailed SCTP debugging. You will also need to have the
255 // SCTP_DEBUG cpp defines flag. 261 // SCTP_DEBUG cpp defines flag.
256 // usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_ALL); 262 // usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_ALL);
257 263
258 // TODO(ldixon): Consider turning this on/off. 264 // TODO(ldixon): Consider turning this on/off.
259 usrsctp_sysctl_set_sctp_ecn_enable(0); 265 usrsctp_sysctl_set_sctp_ecn_enable(0);
260 266
267 int send_size = usrsctp_sysctl_get_sctp_sendspace();
268 if (send_size != kSendBufferSize) {
269 LOG(LS_ERROR) << "Got different send size than expected: " << send_size;
270 }
271
261 // TODO(ldixon): Consider turning this on/off. 272 // TODO(ldixon): Consider turning this on/off.
262 // This is not needed right now (we don't do dynamic address changes): 273 // 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 274 // 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 275 // when a new address is added or removed. This feature is enabled by
265 // default. 276 // default.
266 // usrsctp_sysctl_set_sctp_auto_asconf(0); 277 // usrsctp_sysctl_set_sctp_auto_asconf(0);
267 278
268 // TODO(ldixon): Consider turning this on/off. 279 // TODO(ldixon): Consider turning this on/off.
269 // Add a blackhole sysctl. Setting it to 1 results in no ABORTs 280 // Add a blackhole sysctl. Setting it to 1 results in no ABORTs
270 // being sent in response to INITs, setting it to 2 results 281 // being sent in response to INITs, setting it to 2 results
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 } 316 }
306 LOG(LS_ERROR) << "Failed to shutdown usrsctp."; 317 LOG(LS_ERROR) << "Failed to shutdown usrsctp.";
307 } 318 }
308 } 319 }
309 320
310 DataMediaChannel* SctpDataEngine::CreateChannel( 321 DataMediaChannel* SctpDataEngine::CreateChannel(
311 DataChannelType data_channel_type) { 322 DataChannelType data_channel_type) {
312 if (data_channel_type != DCT_SCTP) { 323 if (data_channel_type != DCT_SCTP) {
313 return NULL; 324 return NULL;
314 } 325 }
315 return new SctpDataMediaChannel(rtc::Thread::Current()); 326 SctpDataMediaChannel *channel = new SctpDataMediaChannel(
327 rtc::Thread::Current());
328 channels_.push_back(channel);
329 channel->SignalDestroyed.connect(this, &SctpDataEngine::OnChannelDestroyed);
330 return channel;
316 } 331 }
317 332
333 // static
334 SctpDataMediaChannel* SctpDataEngine::GetChannelFromSocket(
tommi 2015/08/27 11:38:02 On what thread does this method run and how do you
lally1 2015/08/27 17:56:09 I/O thread for usrsctplib, which only has once ins
335 struct socket* sock) {
336 for (auto p:channels_) {
tommi 2015/08/27 11:38:02 nit: spaces around : nit: would be good to use the
lally1 2015/08/27 17:56:08 Done and done. It's a vector of pointers to chann
337 if (p->socket() == sock) {
338 return p;
339 }
340 }
341 return 0;
342 }
343
344
345 void SctpDataEngine::OnChannelDestroyed(SctpDataMediaChannel* channel) {
346 auto it = std::find(channels_.begin(), channels_.end(), channel);
347 if (it == channels_.end()) {
348 LOG(LS_ERROR) << "OnChannelDestroyed: the channel wasn't registered.";
349 return;
350 }
351 channels_.erase(it);
352 }
353
354 // static
355 int SctpDataEngine::SendThresholdCallback(struct socket* sock,
356 uint32_t sb_free) {
357 SctpDataMediaChannel *channel = GetChannelFromSocket(sock);
tommi 2015/08/27 11:38:02 SctpDataMediaChannel* channel
lally1 2015/08/27 17:56:08 Acknowledged.
358 if (!channel) {
359 LOG(LS_ERROR) << "SendThresholdCallback: Failed to get channel for socket "
360 << sock;
361 return 0;
362 }
363 channel->SignalReadyToSend(true);
364 return 0;
365 }
366
tommi 2015/08/27 11:38:02 nit: one empty line
lally1 2015/08/27 17:56:08 Done.
367
318 SctpDataMediaChannel::SctpDataMediaChannel(rtc::Thread* thread) 368 SctpDataMediaChannel::SctpDataMediaChannel(rtc::Thread* thread)
319 : worker_thread_(thread), 369 : worker_thread_(thread),
320 local_port_(kSctpDefaultPort), 370 local_port_(kSctpDefaultPort),
321 remote_port_(kSctpDefaultPort), 371 remote_port_(kSctpDefaultPort),
322 sock_(NULL), 372 sock_(NULL),
323 sending_(false), 373 sending_(false),
324 receiving_(false), 374 receiving_(false),
325 debug_name_("SctpDataMediaChannel") { 375 debug_name_("SctpDataMediaChannel") {
326 } 376 }
327 377
328 SctpDataMediaChannel::~SctpDataMediaChannel() { 378 SctpDataMediaChannel::~SctpDataMediaChannel() {
329 CloseSctpSocket(); 379 CloseSctpSocket();
380 SignalDestroyed(this);
330 } 381 }
331 382
332 sockaddr_conn SctpDataMediaChannel::GetSctpSockAddr(int port) { 383 sockaddr_conn SctpDataMediaChannel::GetSctpSockAddr(int port) {
333 sockaddr_conn sconn = {0}; 384 sockaddr_conn sconn = {0};
334 sconn.sconn_family = AF_CONN; 385 sconn.sconn_family = AF_CONN;
335 #ifdef HAVE_SCONN_LEN 386 #ifdef HAVE_SCONN_LEN
336 sconn.sconn_len = sizeof(sockaddr_conn); 387 sconn.sconn_len = sizeof(sockaddr_conn);
337 #endif 388 #endif
338 // Note: conversion from int to uint16_t happens here. 389 // Note: conversion from int to uint16_t happens here.
339 sconn.sconn_port = rtc::HostToNetwork16(port); 390 sconn.sconn_port = rtc::HostToNetwork16(port);
340 sconn.sconn_addr = this; 391 sconn.sconn_addr = this;
341 return sconn; 392 return sconn;
342 } 393 }
343 394
344 bool SctpDataMediaChannel::OpenSctpSocket() { 395 bool SctpDataMediaChannel::OpenSctpSocket() {
345 if (sock_) { 396 if (sock_) {
346 LOG(LS_VERBOSE) << debug_name_ 397 LOG(LS_VERBOSE) << debug_name_
347 << "->Ignoring attempt to re-create existing socket."; 398 << "->Ignoring attempt to re-create existing socket.";
348 return false; 399 return false;
349 } 400 }
401
402 // If kSendBufferSize isn't reflective of reality, we log an error, but we
403 // still have to do something reasonable here. Look up what the buffer's
404 // real size is and set our threshold to something reasonable.
405 const static int send_threshold = usrsctp_sysctl_get_sctp_sendspace() / 2;
tommi 2015/08/27 11:38:02 nit: kSendThreshold
lally1 2015/08/27 17:56:09 Done.
406
350 sock_ = usrsctp_socket(AF_CONN, SOCK_STREAM, IPPROTO_SCTP, 407 sock_ = usrsctp_socket(AF_CONN, SOCK_STREAM, IPPROTO_SCTP,
351 cricket::OnSctpInboundPacket, NULL, 0, this); 408 cricket::OnSctpInboundPacket,
409 &SctpDataEngine::SendThresholdCallback,
410 send_threshold, this);
352 if (!sock_) { 411 if (!sock_) {
353 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to create SCTP socket."; 412 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to create SCTP socket.";
354 return false; 413 return false;
355 } 414 }
356 415
357 // Make the socket non-blocking. Connect, close, shutdown etc will not block 416 // Make the socket non-blocking. Connect, close, shutdown etc will not block
358 // the thread waiting for the socket operation to complete. 417 // the thread waiting for the socket operation to complete.
359 if (usrsctp_set_non_blocking(sock_, 1) < 0) { 418 if (usrsctp_set_non_blocking(sock_, 1) < 0) {
360 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to set SCTP to non blocking."; 419 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to set SCTP to non blocking.";
361 return false; 420 return false;
(...skipping 24 matching lines...) Expand all
386 445
387 // Nagle. 446 // Nagle.
388 uint32_t nodelay = 1; 447 uint32_t nodelay = 1;
389 if (usrsctp_setsockopt(sock_, IPPROTO_SCTP, SCTP_NODELAY, &nodelay, 448 if (usrsctp_setsockopt(sock_, IPPROTO_SCTP, SCTP_NODELAY, &nodelay,
390 sizeof(nodelay))) { 449 sizeof(nodelay))) {
391 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to set SCTP_NODELAY."; 450 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to set SCTP_NODELAY.";
392 return false; 451 return false;
393 } 452 }
394 453
395 // Disable MTU discovery 454 // Disable MTU discovery
396 struct sctp_paddrparams params = {{0}}; 455 struct sctp_paddrparams params;
tommi 2015/08/27 11:38:02 prefer this the way it was (without memset)
lally1 2015/08/27 17:56:08 The old way was leaving some bytes uninitialized,
456 memset(&params, 0, sizeof(params));
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, &params, 460 if (usrsctp_setsockopt(sock_, IPPROTO_SCTP, SCTP_PEER_ADDR_PARAMS, &params,
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 490 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698