Пример #1
0
/* -------------------------------------------------------------------
 * Set socket options before the listen() or connect() calls.
 * These are optional performance tuning factors.
 * ------------------------------------------------------------------- */
void SetSocketOptions( thread_Settings *inSettings ) {
    // set the TCP window size (socket buffer sizes)
    // also the UDP buffer size
    // must occur before call to accept() for large window sizes
    setsock_tcp_windowsize( inSettings->mSock, inSettings->mTCPWin,
                            (inSettings->mThreadMode == kMode_Client ? 1 : 0) );

    if ( isCongestionControl( inSettings ) ) {
#ifdef TCP_CONGESTION
	Socklen_t len = strlen( inSettings->mCongestion ) + 1;
	int rc = setsockopt( inSettings->mSock, IPPROTO_TCP, TCP_CONGESTION,
			     inSettings->mCongestion, len);
	FAIL( rc == SOCKET_ERROR, ( "Attempt to set '%s' congestion control failed: %s\r\n", inSettings->mCongestion, strerror(errno) ), NULL );
#else
	fprintf( stderr, "The -Z option is not available on this operating system\r\n");
#endif /* TCP_CONGESTION */
    }

    // check if we're sending multicast, and set TTL
    if ( isMulticast( inSettings ) && ( inSettings->mTTL > 0 ) ) {
#ifdef HAVE_MULTICAST
    int val = inSettings->mTTL;
	if ( !SockAddr_isIPv6( &inSettings->local ) ) {
	    int rc = setsockopt( inSettings->mSock, IPPROTO_IP, IP_MULTICAST_TTL,
		    (const char*) &val, (Socklen_t) sizeof(val));

	    WARN_errno( rc == SOCKET_ERROR, ( "Failed to set multicast TTL.\r\n" ) );
	}
#ifdef HAVE_IPV6_MULTICAST
	else {
	    int rc = setsockopt( inSettings->mSock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
		    (const char*) &val, (Socklen_t) sizeof(val));
	    WARN_errno( rc == SOCKET_ERROR, ( "Failed to set multicast TTL.\r\n" ) );
	}
#endif /* HAVE_IPV6_MULTICAST */
#endif /* HAVE_MULTICAST */
    }

#ifdef IP_TOS
    // set IP TOS (type-of-service) field
//    if ( inSettings->mTOS > 0 ) {
        int  tos, rc;

        tos = inSettings->mTOS;
        Socklen_t len = sizeof(tos);
        rc = setsockopt( inSettings->mSock, IPPROTO_IP, IP_TOS,(char*) &tos, len );
        WARN_errno( rc == SOCKET_ERROR, ( "Failed to set IP_TOS.\r\n" ) );
//    }
#endif /* IP_TOS */

    if ( !isUDP( inSettings ) ) {
        // set the TCP maximum segment size
        setsock_tcp_mss( inSettings->mSock, inSettings->mMSS );

#ifdef TCP_NODELAY
        // set TCP nodelay option
        if ( isNoDelay( inSettings ) ) {
            int nodelay = 1;
            Socklen_t len = sizeof(nodelay);
            int rc = setsockopt( inSettings->mSock, IPPROTO_TCP, TCP_NODELAY,
                                 (char*) &nodelay, len );
            WARN_errno( rc == SOCKET_ERROR, ( "Failed to set TCP_NODELAY.\r\n" ) );
        }
#endif /* TCP_NODELAY */
    }
} // end SetSocketOptions
void PerfSocket::SetSocketOptions( void ) {
  // set the TCP window size (socket buffer sizes)
  // also the UDP buffer size
  // must occur before call to accept() for large window sizes
  set_tcp_windowsize( mSock, mSettings->mTCPWin );

#ifdef IP_TOS

  // set IP TOS (type-of-service) field
  if ( mSettings->mTOS > 0 ) {
    int  tos = mSettings->mTOS;
    Socklen_t len = sizeof(tos);
    int rc = setsockopt( mSock, IPPROTO_IP, IP_TOS,
			 (char*) &tos, len );
    FAIL_errno( rc == SOCKET_ERROR, "setsockopt IP_TOS" );
  }
#endif

  if ( ! mUDP ) {
    // set the TCP maximum segment size
    if(mSettings->mProtocol != IPPROTO_SCTP) {
      setsock_tcp_mss( mSock, mSettings->mMSS );

#ifdef TCP_NODELAY

      // set TCP nodelay option
      if ( mSettings->mNodelay ) {
	int nodelay = 1;
	Socklen_t len = sizeof(nodelay);
	int rc = setsockopt( mSock, IPPROTO_TCP, TCP_NODELAY,
			     (char*) &nodelay, len );
	FAIL_errno( rc == SOCKET_ERROR, "setsockopt TCP_NODELAY" );
      }
#endif
    } else {
      mSettings->mMSS  = getsock_sctp_mss( mSock );

#ifdef SCTP_NODELAY
      // set TCP nodelay option
      if ( mSettings->mNodelay ) {
	int nodelay = 1;
	Socklen_t len = sizeof(nodelay);
	int rc = setsockopt( mSock, IPPROTO_SCTP, SCTP_NODELAY,
			     (char*) &nodelay, len );
	FAIL_errno( rc == SOCKET_ERROR, "setsockopt SCTP_NODELAY" );
      }
#endif
#if defined(SCTP_PARTIAL_DELIVERY_POINT) && defined(SCTP_EXPLICIT_EOR)
      if(mSettings->mEmulation == true) {
	int rc;
	int val=1;
	Socklen_t len = sizeof(int);
	rc = setsockopt( mSock, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT,
			     (char*) &val, len );
	FAIL_errno( rc == SOCKET_ERROR, "setsockopt SCTP_PARTIAL_DELIVERY_POINT" );
	rc = setsockopt( mSock, IPPROTO_SCTP, SCTP_EXPLICIT_EOR,
			   (char*) &val, len );
	FAIL_errno( rc == SOCKET_ERROR, "setsockopt SCTP_EXPLICT_EOR" );
      }
#endif

    }
  }
}