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

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: Response to 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.
113 static const int kSendBufferSize = 262144;
pthatcher1 2015/08/18 21:51:52 Can you comment on how this number was chosen?
lally1 2015/08/24 15:46:39 Done.
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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 int send_size = usrsctp_sysctl_get_sctp_sendspace();
264 if (send_size != kSendBufferSize) {
265 LOG(LS_ERROR) << "Got different send size than expected: " << send_size;
266 }
267
261 // TODO(ldixon): Consider turning this on/off. 268 // TODO(ldixon): Consider turning this on/off.
262 // This is not needed right now (we don't do dynamic address changes): 269 // 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 270 // 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 271 // when a new address is added or removed. This feature is enabled by
265 // default. 272 // default.
266 // usrsctp_sysctl_set_sctp_auto_asconf(0); 273 // usrsctp_sysctl_set_sctp_auto_asconf(0);
267 274
268 // TODO(ldixon): Consider turning this on/off. 275 // TODO(ldixon): Consider turning this on/off.
269 // Add a blackhole sysctl. Setting it to 1 results in no ABORTs 276 // Add a blackhole sysctl. Setting it to 1 results in no ABORTs
270 // being sent in response to INITs, setting it to 2 results 277 // being sent in response to INITs, setting it to 2 results
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 } 315 }
309 316
310 DataMediaChannel* SctpDataEngine::CreateChannel( 317 DataMediaChannel* SctpDataEngine::CreateChannel(
311 DataChannelType data_channel_type) { 318 DataChannelType data_channel_type) {
312 if (data_channel_type != DCT_SCTP) { 319 if (data_channel_type != DCT_SCTP) {
313 return NULL; 320 return NULL;
314 } 321 }
315 return new SctpDataMediaChannel(rtc::Thread::Current()); 322 return new SctpDataMediaChannel(rtc::Thread::Current());
316 } 323 }
317 324
325 // Our registrar of sockets to their channels. Used for callbacks.
326 std::vector<SctpDataMediaChannel*> SctpDataMediaChannel::sock_channel_map_;
pthatcher1 2015/08/18 21:51:52 Since it's not a map anymore, perhaps channels_ wo
lally1 2015/08/24 15:46:39 Done.
327
318 SctpDataMediaChannel::SctpDataMediaChannel(rtc::Thread* thread) 328 SctpDataMediaChannel::SctpDataMediaChannel(rtc::Thread* thread)
319 : worker_thread_(thread), 329 : worker_thread_(thread),
320 local_port_(kSctpDefaultPort), 330 local_port_(kSctpDefaultPort),
321 remote_port_(kSctpDefaultPort), 331 remote_port_(kSctpDefaultPort),
322 sock_(NULL), 332 sock_(NULL),
323 sending_(false), 333 sending_(false),
324 receiving_(false), 334 receiving_(false),
325 debug_name_("SctpDataMediaChannel") { 335 debug_name_("SctpDataMediaChannel") {
336 sock_channel_map_.push_back(this);
326 } 337 }
327 338
328 SctpDataMediaChannel::~SctpDataMediaChannel() { 339 SctpDataMediaChannel::~SctpDataMediaChannel() {
329 CloseSctpSocket(); 340 CloseSctpSocket();
341 auto it = std::find(sock_channel_map_.begin(),
342 sock_channel_map_.end(),
343 this);
344 if (it != sock_channel_map_.end()) {
345 sock_channel_map_.erase(it);
346 } else {
347 LOG(LS_ERROR) << "~SctpDataMediaChannel: the channel wasn't registered.";
348 }
pthatcher1 2015/08/18 21:51:53 An early return might look a little better: if (i
lally1 2015/08/24 15:46:39 Done, in OnChannelDestroyed(), where this code was
330 } 349 }
331 350
332 sockaddr_conn SctpDataMediaChannel::GetSctpSockAddr(int port) { 351 sockaddr_conn SctpDataMediaChannel::GetSctpSockAddr(int port) {
333 sockaddr_conn sconn = {0}; 352 sockaddr_conn sconn = {0};
334 sconn.sconn_family = AF_CONN; 353 sconn.sconn_family = AF_CONN;
335 #ifdef HAVE_SCONN_LEN 354 #ifdef HAVE_SCONN_LEN
336 sconn.sconn_len = sizeof(sockaddr_conn); 355 sconn.sconn_len = sizeof(sockaddr_conn);
337 #endif 356 #endif
338 // Note: conversion from int to uint16_t happens here. 357 // Note: conversion from int to uint16_t happens here.
339 sconn.sconn_port = rtc::HostToNetwork16(port); 358 sconn.sconn_port = rtc::HostToNetwork16(port);
340 sconn.sconn_addr = this; 359 sconn.sconn_addr = this;
341 return sconn; 360 return sconn;
342 } 361 }
343 362
363 // static
364 int SctpDataMediaChannel::SendThresholdCallback(struct socket* sock,
365 uint32_t sb_free) {
366 for (auto p:sock_channel_map_) {
367 if (p->socket() == sock) {
pthatcher1 2015/08/18 21:51:52 Did you mean to put this in GetSctpChannel?
lally1 2015/08/24 15:46:39 I took that method out. Did you want it back in?
368 p->SignalReadyToSend(true);
369 return 0;
370 }
371 }
372 LOG(LS_ERROR) << "SendThresholdCallback: Failed to get channel for socket "
373 << sock;
374 return 0;
375 }
376
344 bool SctpDataMediaChannel::OpenSctpSocket() { 377 bool SctpDataMediaChannel::OpenSctpSocket() {
345 if (sock_) { 378 if (sock_) {
346 LOG(LS_VERBOSE) << debug_name_ 379 LOG(LS_VERBOSE) << debug_name_
347 << "->Ignoring attempt to re-create existing socket."; 380 << "->Ignoring attempt to re-create existing socket.";
348 return false; 381 return false;
349 } 382 }
383
384 // kSendBufferSize is our expected default buffer size. If it isn't
385 // reflective of reality we'll log an error, but we still have to do
386 // something reasonable here.
pthatcher1 2015/08/18 21:51:52 This comment looks like it belongs up by where kSe
lally1 2015/08/24 15:46:39 No I just wrote it poorly. Rewritten.
387 const int send_threshold = usrsctp_sysctl_get_sctp_sendspace() / 2;
388
350 sock_ = usrsctp_socket(AF_CONN, SOCK_STREAM, IPPROTO_SCTP, 389 sock_ = usrsctp_socket(AF_CONN, SOCK_STREAM, IPPROTO_SCTP,
351 cricket::OnSctpInboundPacket, NULL, 0, this); 390 cricket::OnSctpInboundPacket,
391 &SctpDataMediaChannel::SendThresholdCallback,
392 send_threshold, this);
352 if (!sock_) { 393 if (!sock_) {
353 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to create SCTP socket."; 394 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to create SCTP socket.";
354 return false; 395 return false;
355 } 396 }
356 397
357 // Make the socket non-blocking. Connect, close, shutdown etc will not block 398 // Make the socket non-blocking. Connect, close, shutdown etc will not block
358 // the thread waiting for the socket operation to complete. 399 // the thread waiting for the socket operation to complete.
359 if (usrsctp_set_non_blocking(sock_, 1) < 0) { 400 if (usrsctp_set_non_blocking(sock_, 1) < 0) {
360 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to set SCTP to non blocking."; 401 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to set SCTP to non blocking.";
361 return false; 402 return false;
(...skipping 24 matching lines...) Expand all
386 427
387 // Nagle. 428 // Nagle.
388 uint32_t nodelay = 1; 429 uint32_t nodelay = 1;
389 if (usrsctp_setsockopt(sock_, IPPROTO_SCTP, SCTP_NODELAY, &nodelay, 430 if (usrsctp_setsockopt(sock_, IPPROTO_SCTP, SCTP_NODELAY, &nodelay,
390 sizeof(nodelay))) { 431 sizeof(nodelay))) {
391 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to set SCTP_NODELAY."; 432 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to set SCTP_NODELAY.";
392 return false; 433 return false;
393 } 434 }
394 435
395 // Disable MTU discovery 436 // Disable MTU discovery
396 struct sctp_paddrparams params = {{0}}; 437 struct sctp_paddrparams params;
438 memset(&params, 0, sizeof(params));
397 params.spp_assoc_id = 0; 439 params.spp_assoc_id = 0;
398 params.spp_flags = SPP_PMTUD_DISABLE; 440 params.spp_flags = SPP_PMTUD_DISABLE;
399 params.spp_pathmtu = kSctpMtu; 441 params.spp_pathmtu = kSctpMtu;
400 if (usrsctp_setsockopt(sock_, IPPROTO_SCTP, SCTP_PEER_ADDR_PARAMS, &params, 442 if (usrsctp_setsockopt(sock_, IPPROTO_SCTP, SCTP_PEER_ADDR_PARAMS, &params,
401 sizeof(params))) { 443 sizeof(params))) {
402 LOG_ERRNO(LS_ERROR) << debug_name_ 444 LOG_ERRNO(LS_ERROR) << debug_name_
403 << "Failed to set SCTP_PEER_ADDR_PARAMS."; 445 << "Failed to set SCTP_PEER_ADDR_PARAMS.";
404 return false; 446 return false;
405 } 447 }
406 448
(...skipping 490 matching lines...) Expand 10 before | Expand all | Expand 10 after
897 } 939 }
898 940
899 bool SctpDataMediaChannel::SetRecvCodecs(const std::vector<DataCodec>& codecs) { 941 bool SctpDataMediaChannel::SetRecvCodecs(const std::vector<DataCodec>& codecs) {
900 return GetCodecIntParameter( 942 return GetCodecIntParameter(
901 codecs, kGoogleSctpDataCodecId, kGoogleSctpDataCodecName, kCodecParamPort, 943 codecs, kGoogleSctpDataCodecId, kGoogleSctpDataCodecName, kCodecParamPort,
902 &local_port_); 944 &local_port_);
903 } 945 }
904 946
905 void SctpDataMediaChannel::OnPacketFromSctpToNetwork( 947 void SctpDataMediaChannel::OnPacketFromSctpToNetwork(
906 rtc::Buffer* buffer) { 948 rtc::Buffer* buffer) {
907 if (buffer->size() > kSctpMtu) { 949 // usrsctp seems to interpret the MTU we give it strangely -- it seems to
950 // give us back packets bigger than that MTU, if only by a fixed amount.
951 // This is that amount that we've observed.
952 const int kSctpOverhead = 76;
953 if (buffer->size() > (kSctpOverhead + kSctpMtu)) {
908 LOG(LS_ERROR) << debug_name_ << "->OnPacketFromSctpToNetwork(...): " 954 LOG(LS_ERROR) << debug_name_ << "->OnPacketFromSctpToNetwork(...): "
909 << "SCTP seems to have made a packet that is bigger " 955 << "SCTP seems to have made a packet that is bigger "
910 "than its official MTU."; 956 << "than its official MTU: " << buffer->size()
957 << " vs max of " << kSctpMtu
958 << " even after adding " << kSctpOverhead
959 << " extra SCTP overhead";
911 } 960 }
912 MediaChannel::SendPacket(buffer); 961 MediaChannel::SendPacket(buffer);
913 } 962 }
914 963
915 bool SctpDataMediaChannel::SendQueuedStreamResets() { 964 bool SctpDataMediaChannel::SendQueuedStreamResets() {
916 if (!sent_reset_streams_.empty() || queued_reset_streams_.empty()) 965 if (!sent_reset_streams_.empty() || queued_reset_streams_.empty())
917 return true; 966 return true;
918 967
919 LOG(LS_VERBOSE) << "SendQueuedStreamResets[" << debug_name_ << "]: Sending [" 968 LOG(LS_VERBOSE) << "SendQueuedStreamResets[" << debug_name_ << "]: Sending ["
920 << ListStreams(queued_reset_streams_) << "], Open: [" 969 << ListStreams(queued_reset_streams_) << "], Open: ["
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
962 } 1011 }
963 case MSG_SCTPOUTBOUNDPACKET: { 1012 case MSG_SCTPOUTBOUNDPACKET: {
964 rtc::scoped_ptr<OutboundPacketMessage> pdata( 1013 rtc::scoped_ptr<OutboundPacketMessage> pdata(
965 static_cast<OutboundPacketMessage*>(msg->pdata)); 1014 static_cast<OutboundPacketMessage*>(msg->pdata));
966 OnPacketFromSctpToNetwork(pdata->data().get()); 1015 OnPacketFromSctpToNetwork(pdata->data().get());
967 break; 1016 break;
968 } 1017 }
969 } 1018 }
970 } 1019 }
971 } // namespace cricket 1020 } // namespace cricket
OLDNEW
« talk/media/sctp/sctpdataengine.h ('K') | « talk/media/sctp/sctpdataengine.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698