36
7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報報報 : 報報報

7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

Embed Size (px)

Citation preview

Page 1: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary

報告者 :梁凱鈞

Page 2: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

7.9 TCP Socket OptionsThere are two socket options for TCP. We

specify the level as IPPROTO_TCP.TCP_MAXSEG Socket OptionTCP_NODELAY Socket Option

Page 3: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

TCP_MAXSEG Socket OptionThis socket option allows us to fetch or set the

MSS for a TCP connection. The value returned is the maximum amount of

data that our TCP will send to the other end; often, it is the MSS announced by the other end with its SYN, unless our TCP chooses to use a smaller value than the peer's announced MSS.

If this value is fetched before the socket is connected, the value returned is the default value that will be used if an MSS option is not received from the other end.

Page 4: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

The maximum amount of data that our TCP will send per segment can also change during the life of a connection if TCP supports path MTU discovery. If the route to the peer changes, this value can go up or down.

Once the connection is established, this value is the MSS option announced by the peer, and we cannot exceed that value. Our TCP, however, can always send less than the peer's announced MSS.

Page 5: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

TCP_NODELAY Socket OptionIf set, this option disables TCP's Nagle

algorithm. By default, this algorithm is enabled.The purpose of the Nagle algorithm is to reduce

the number of small packets on a WAN. The algorithm states that if a given connection

has outstanding data (i.e., data that our TCP has sent, and for which it is currently awaiting an acknowledgment), then no small packets will be sent on the connection in response to a user write operation until the existing data is acknowledged.

Page 6: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

the Nagle algorithmThe definition of a "small" packet is any packet

smaller than the MSS.TCP will always send a full-sized packet if

possible; the purpose of the Nagle algorithm is to prevent a connection from having multiple small packets outstanding at any time.

The two common generators of small packets are the Rlogin and Telnet clients, since they normally send each keystroke as a separate packet.

Page 7: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞
Page 8: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞
Page 9: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

the delayed ACK algorithmThe Nagle algorithm often interacts with another

TCP algorithm: the delayed ACK algorithm. This algorithm causes TCP to not send an ACK

immediately when it receives data; instead, TCP will wait some small amount of time (typically 50–200 ms) and only then send the ACK.

The hope is that in this small amount of time, there will be data to send back to the peer, and the ACK can piggyback with the data, saving one TCP segment.

Page 10: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

Problem The problem is with other clients whose

servers do not generate traffic in the reverse direction on which ACKs can piggyback. These clients can detect noticeable delays because the client TCP will not send any data to the server until the server's delayed ACK timer expires. These clients need a way to disable the Nagle algorithm, hence the TCP_NODELAY option.

Page 11: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

Another type of client Another type of client that interacts badly

with the Nagle algorithm and TCP's delayed ACKs is a client that sends a single logical request to its server in small pieces.

Some problem

Page 12: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

Three way To solve problem1.Use writev (Section 14.4) instead of two calls to

write. A single call to writev ends up with one call to TCP output instead of two calls, resulting in one TCP segment for our example. This is the preferred solution.

2. Copy the 4 bytes of data and the 396 bytes of data into a single buffer and call write once for this buffer.

3. Set the TCP_NODELAY socket option and continue to call write two times. This is the least desirable solution, and is harmful to the network, so it generally should not even be considered.

Page 13: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

7.10 SCTP Socket OptionsThe relatively large number of socket options

for SCTP (17 at present writing) reflects the finer grain of control SCTP provides to the application developer. We specify the level as IPPROTO_SCTP.

Page 14: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

SCTP_ADAPTION_LAYER Socket OptionSCTP_ASSOCINFO Socket OptionSCTP_AUTOCLOSE Socket OptionSCTP_DEFAULT_SEND_PARAM Socket OptionSCTP_DISABLE_FRAGMENTS Socket OptionSCTP_EVENTS Socket OptionSCTP_GET_PEER_ADDR_INFO Socket OptionSCTP_I_WANT_MAPPED_V4_ADDR Socket

OptionSCTP_INITMSG Socket OptionSCTP_MAXBURST Socket Option

Page 15: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

SCTP_MAXSEG Socket OptionSCTP_NODELAY Socket OptionSCTP_PEER_ADDR_PARAMS Socket

OptionSCTP_PRIMARY_ADDR Socket OptionSCTP_RTOINFO Socket OptionSCTP_SET_PEER_PRIMARY_ADDR Socket

OptionSCTP_STATUS Socket Option

Page 16: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

SCTP_ADAPTION_LAYER Socket Option

During association initialization, either endpoint may specify an adaption layer indication. This indication is a 32-bit unsigned integer that can be used by the two applications to coordinate any local application adaption layer. This option allows the caller to fetch or set the adaption layer indication that this endpoint will provide to peers.

When fetching this value, the caller will only retrieve the value the local socket will provide to all future peers. To retrieve the peer's adaption layer indication, an application must subscribe to adaption layer events.

Page 17: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

SCTP_ASSOCINFO Socket Optionthree purposes: (i) to retrieve information

about an existing association, (ii) to change the parameters of an existing association, and/or (iii) to set defaults for future associations.

When retrieving information about an existing association, the sctp_opt_info function should be used instead of getsockopt. This option takes as input the sctp_assocparams structure.

Page 18: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

SCTP_AUTOCLOSE Socket OptionThis option allows us to fetch or set the autoclose

time for an SCTP endpoint.Idle is defined by the SCTP stack as neither endpoint

sending or receiving user data. The default is for the autoclose function to be disabled.

Autoclose can be used by a server to force the closing of idle associations without the server needing to maintain additional state. A server using this feature needs to carefully assess the longest idle time expected on all its associations. Setting the autoclose value smaller than needed results in the premature closing of associations.

Page 19: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

SCTP_DEFAULT_SEND_PARAM Socket Option

SCTP has many optional send parameters that are often passed as ancillary data or used with the sctp_sendmsg function call (which is often implemented as a library call that passes ancillary data for the user). An application that wishes to send a large number of messages, all with the same parameters, can use this option to set up the default parameters and thus avoid using ancillary data or the sctp_sendmsg call. This option takes as input the sctp_sndrcvinfo structure.

Page 20: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

SCTP_DISABLE_FRAGMENTS Socket Option

SCTP normally fragments any user message that does not fit in a single SCTP packet into multiple DATA chunks. Setting this option disables this behavior on the sender. When disabled by this option, SCTP will return the error EMSGSIZE and not send the message. The default behavior is for this option to be disabled; SCTP will normally fragment user messages.

This option may be used by applications that wish to control message sizes, ensuring that every user application message will fit in a single IP packet.

Page 21: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

SCTP_EVENTS Socket OptionThis socket option allows a caller to fetch,

enable, or disable various SCTP notifications. An SCTP notification is a message that the SCTP stack will send to the application.

Eight different types of events can be subscribed to by using this option and passing an sctp_event_subscribe structure.

0: non-subscription1 : subscription

Page 22: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

SCTP_GET_PEER_ADDR_INFO Socket Option

This option retrieves information about a peer address, including the congestion window, smoothed RTT and MTU.

Page 23: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

SCTP_I_WANT_MAPPED_V4_ADDR Socket OptionThis flag can be used to enable or disable

IPv4-mapped addresses on an AF_INET6-type socket.

Note that when enabled (which is the default behavior), all IPv4 addresses will be mapped to a IPv6 address before sending to the application.

If this option is disabled, the SCTP socket will not map IPv4 addresses and will instead pass them as a sockaddr_in structure.

Page 24: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

SCTP_INITMSG Socket OptionThis option can be used to get or set the

default initial parameters used on an SCTP socket when sending out the INIT message. The option uses the sctp_initmsg structure, which is defined as:

Page 25: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

SCTP_MAXSEG Socket OptionThis socket option allows the application to

fetch or set the maximum fragment size used during SCTP fragmentation. This option is similar to the TCP option TCP_MAXSEG described in Section 7.9.

Page 26: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

SCTP_NODELAY Socket OptionIf set, this option disables SCTP's Nagle

algorithm. This option is OFF by default (i.e., the Nagle algorithm is ON by default).

Page 27: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

SCTP_PEER_ADDR_PARAMS Socket Option

This socket option allows an application to fetch or set various parameters on an association. The caller provides the sctp_paddrparams structure

Page 28: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

SCTP_PRIMARY_ADDR Socket Option

This socket option fetches or sets the address that the local endpoint is using as primary. The primary address is used, by default, as the destination address for all messages sent to a peer.

To set this value, the caller fills in the association identification and the peer's address that should be used as the primary address. The caller passes this information in a sctp_setprim structure

Page 29: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

SCTP_RTOINFO Socket OptionThis socket option can be used to fetch or set

various RTO information on a specific association or the default values used by this endpoint. When fetching, the caller should use sctp_opt_info instead of getsockopt for maximum portability. The caller provides a sctp_rtoinfo structure

Page 30: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

SCTP_SET_PEER_PRIMARY_ADDR Socket Option

Setting this option causes a message to be sent that requests that the peer set the specified local address as its primary address.

Page 31: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

SCTP_STATUS Socket OptionThis socket option will retrieve the current

state of an SCTP association.The caller provides an sctp_status structure,

filling in the association identification field, sstat_assoc_id. The structure will be returned filled in with the information pertaining to the requested association.

Page 32: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

7.11 fcntl Functionfcntl stands for "file control" and this function

performs various descriptor control operations.

Page 33: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

The fcntl function provides the following features related to network programming:

Nonblocking I/O— We can set the O_NONBLOCK file status flag using the F_SETFL command to set a socket as nonblocking.

Signal-driven I/O— We can set the O_ASYNC file status flag using the F_SETFL command, which causes the SIGIO signal to be generated when the status of a socket changes.

The F_SETOWN command lets us set the socket owner (the process ID or process group ID) to receive the SIGIO and SIGURG signals. The former signal is generated when signal-driven I/O is enabled for a socket (Chapter 25) and the latter signal is generated when new out-of-band data arrives for a socket

Page 34: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

Each descriptor (including a socket) has a set of file flags that is fetched with the F_GETFL command and set with the F_SETFL command. The two flags that affect a socket are

O_NONBLOCK—nonblocking I/OO_ASYNC—signal-driven I/O

Page 35: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞
Page 36: 7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞

7.12 SummarySCTP provides 17 socket options that are

used by the application to control the transport. SCTP_NODELAY and SCTP_MAXSEG are similar to TCP_NODELAY and TCP_MAXSEG and perform equivalent functions. The other 15 options give the application finer control of the SCTP stack;