{-# LANGUAGE ScopedTypeVariables #-}{--
Copyright (c) 2006, Peng Li
2006, Stephan A. Zdancewic
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright owners nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--}moduleNetwork.TCP.LTS.Out(tcp_output_all,tcp_output,tcp_close,tcp_drop_and_close)whereimportData.ListasListimportNetwork.TCP.Aux.OutputimportNetwork.TCP.Aux.MiscimportNetwork.TCP.Type.BaseimportNetwork.TCP.Type.SyscallimportNetwork.TCP.Type.SocketimportNetwork.TCP.Aux.HostMonadimportNetwork.TCP.Aux.SockMonadimportControl.MonadimportControl.ExceptionimportForeignimportNetwork.TCP.Type.TimerimportNetwork.TCP.Type.DatagramasDatagramimportNetwork.TCP.Aux.Paramtcp_output_all::SMonadt()tcp_output_all=doh<-get_host_sock<-get_sockletscb=cb_sndsocktcb=cbsockwhen((stsock`elem`[ESTABLISHED,CLOSE_WAIT,FIN_WAIT_1,FIN_WAIT_2,CLOSING,LAST_ACK,TIME_WAIT]&&(snd_unascb/=isstcb))-- does this make sense?||(stsock`elem`[SYN_SENT,SYN_RECEIVED]&&cantsndmoretcb&&(tf_shouldacknow$cb_rcvsock)))$output_loophsockoutput_loophsock=let(sock1,outsegs)=tcp_output_really(clockh)False(ticksh)sockinifList.nulloutsegsthenput_socksock1elsedo--debug $ "tcp_output_all: " ++ (show outsegs)emit_segs_$!outsegsoutput_loophsock1{-# INLINE tcp_output_all #-}{-# INLINE output_loop #-}{-# INLINE tcp_output_really #-}tcp_output_really(curr_time::Time)(window_probe::Bool)(ts_val'::Timestamp)tcp_sock=lettcb=cbtcp_sockscb=cb_sndtcp_sockrcb=cb_rcvtcp_sockinassert((rcv_advrcb)>=(rcv_nxtrcb))$assert((snd_nxtscb)>=(snd_unascb))$letsnd_cwnd'=ifsnd_maxscb==snd_unascb&&(t_idletime$cb_timetcp_sock)-curr_time>=(computed_rxtcur$t_rttinfscb)then(t_maxsegtcb)*ss_fltsz-- has been idle for a while, slowstartelsesnd_cwndscbwin0=min(snd_wndscb)snd_cwnd'win=ifwindow_probe&&win0==0then1elsewin0snd_wnd_unused::Int=win-((snd_nxtscb)`seq_diff`(snd_unascb))syn_not_acked=(sttcp_sock`elem`[SYN_SENT,SYN_RECEIVED])fin_required=(cantsndmoretcb&&sttcp_sock`notElem`[FIN_WAIT_2,TIME_WAIT])last_sndq_data_seq=(snd_unascb)`seq_plus`(bufc_length$sndqscb)last_sndq_data_and_fin_seq=last_sndq_data_seq`seq_plus`(iffin_requiredthen1else0)`seq_plus`(ifsyn_not_ackedthen1else0)have_data_to_send=(snd_nxtscb)<last_sndq_data_seqhave_data_or_fin_to_send=(snd_nxtscb)<last_sndq_data_and_fin_seqwindow_update_delta=(min(tcp_maxwin`shiftL`(rcv_scaletcb))(freebsd_so_rcvbuf-(bufc_length$rcvqrcb)))-((rcv_advrcb)`seq_diff`(rcv_nxtrcb))need_to_send_a_window_update=(window_update_delta>=2*(t_maxsegtcb))||(2*window_update_delta>=freebsd_so_rcvbuf)do_output=(have_data_or_fin_to_send&&(ifhave_data_to_sendthensnd_wnd_unused>0elseTrue))||need_to_send_a_window_update-- sndurp tcp_sock /= Nothing||tf_shouldacknowrcbcant_send=(notdo_output)&&(bufc_length(sndqscb)>0)&&mode_of(tt_rexmtscb)==Nothingwindow_shrunk=win==0&&snd_wnd_unused<0&&sttcp_sock/=SYN_SENTtcp_sock0=ifcant_sendthentcp_sock{cb_snd=scb{tt_rexmt=start_tt_persist0(t_rttinfscb)curr_time}}elseifwindow_shrunkthentcp_sock{cb_snd=scb{tt_rexmt=casett_rexmtscbofJust(Timed(Persist,shift)d)->Just(Timed(Persist,0)d)_->start_tt_persist0(t_rttinfscb)curr_time,snd_nxt=snd_unascb}}elsetcp_sockinif(notdo_output)then(tcp_sock0,[])else------------ really do it ---------------------------------------------lettcp_sock=tcp_sock0scb=cb_sndtcp_sockdata'=bufferchain_drop(snd_nxtscb`seq_diff`(snd_unascb))(sndqscb)data_to_send=bufferchain_take(min(snd_wnd_unused)(t_maxsegtcb))data'bFIN=fin_required&&(snd_nxtscb)`seq_plus`(bufc_lengthdata_to_send)>=last_sndq_data_seqbACK=ifbFIN&&sttcp_sock==SYN_SENTthenFalseelseTruesnd_nxt'=ifbFIN&&((snd_nxtscb`seq_plus`(bufc_lengthdata_to_send)==last_sndq_data_seq`seq_plus`1&&snd_unascb/=isstcb)||(snd_nxtscb)`seq_diff`(isstcb)==2)thensnd_nxtscb`seq_minus`1elsesnd_nxtscbbPSH=bufc_lengthdata_to_send>0&&snd_nxtscb`seq_plus`(bufc_lengthdata_to_send)==last_sndq_data_seqrcv_wnd''=calculate_bsd_rcv_wndtcp_sockrcv_wnd'=max(rcv_advrcb`seq_diff`(rcv_nxtrcb))(min(tcp_maxwin`shiftL`(rcv_scaletcb))(ifrcv_wnd''<(freebsd_so_rcvbuf`div`4)&&rcv_wnd''<(t_maxsegtcb)then0elsercv_wnd''))want_tstmp=ifsttcp_sock==SYN_SENTthentf_req_tstmptcbelsetf_doing_tstmptcbts_=do_tcp_optionscurr_timewant_tstmp(ts_recent$cb_timetcp_sock)ts_val'inletwin_=rcv_wnd'`shiftR`(rcv_scaletcb)seg=TCPSegment{tcp_src=local_addrtcb,tcp_dst=remote_addrtcb,tcp_seq=snd_nxt',tcp_ack=rcv_nxtrcb,tcp_URG=False,tcp_ACK=bACK,tcp_PSH=bPSH,tcp_RST=False,tcp_SYN=False,tcp_FIN=bFIN,tcp_win=win_,tcp_urp=0,tcp_data=data_to_send-- option: window scaling,tcp_ws=Nothing-- option: max segment size,tcp_mss=Nothing-- option: RFC1323,tcp_ts=ts_}st'=ifbFINthencasesttcp_sockofESTABLISHED->FIN_WAIT_1CLOSE_WAIT->LAST_ACKxxx->xxxelsesttcp_socksnd_nxt''=snd_nxt'`seq_plus`(bufc_lengthdata_to_send)`seq_plus`(ifbFINthen1else0)snd_max'=max(snd_maxscb)snd_nxt''tt_rexmt'=if(mode_of(tt_rexmtscb)==Nothing||(mode_of(tt_rexmtscb)==JustPersist&&notwindow_probe))&&snd_nxt''>(snd_unascb)thenstart_tt_rexmt0False(t_rttinfscb)curr_timeelseif(window_probe{-- || sndurp tcp_sock /= Nothing --})&&win0/=0&&mode_of(tt_rexmtscb)==JustPersistthenNothingelsett_rexmtscbt_rttseg'=ift_rttsegscb==Nothing&&(bufc_lengthdata_to_send>0||bFIN)&&snd_nxt''>(snd_maxscb)&&notwindow_probethenJust(ts_val',snd_nxt')elset_rttsegscbtcp_sock'=tcp_sock{st=st',cb_snd=scb{tt_rexmt=tt_rexmt',snd_cwnd=snd_cwnd',t_rttseg=t_rttseg',snd_max=snd_max',snd_nxt=snd_nxt''},cb_rcv=rcb{last_ack_sent=rcv_nxtrcb,rcv_adv=rcv_nxtrcb`seq_plus`rcv_wnd',tt_delack=False,rcv_wnd=rcv_wnd',tf_rxwin0sent=(rcv_wnd'==0),tf_shouldacknow=False}}outsegs'=[TCPMessageseg]in(tcp_sock',outsegs'){-# INLINE tcp_output #-}tcp_output::Bool->SMonadt()tcp_outputwin_probe=dosock<-get_sockh<-get_host_let(newsock,segs)=tcp_output_really(clockh)win_probe(ticksh)sockput_socknewsockemit_segs_segs--if List.null segs then return () else debug $ "tcp_output: " ++ (show segs)