Subversion Repositories Kolibri OS

Rev

Rev 1761 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ;-----------------------------------------------------------------
  2. ;
  3. ; TCP_output
  4. ;
  5. ; IN:  eax = socket pointer
  6. ;
  7. ; OUT: /
  8. ;
  9. ;-----------------------------------------------------------------
  10. align 4
  11. TCP_output:
  12.  
  13.         DEBUGF 1,"TCP_output, socket: %x\n", eax
  14.  
  15.  
  16. ; We'll detect the length of the data to be transmitted, and flags to be used
  17. ; If there is some data, or any critical controls to send (SYN / RST), then transmit
  18. ; Otherwise, investigate further
  19.  
  20.         mov     ebx, [eax + TCP_SOCKET.SND_MAX]
  21.         cmp     ebx, [eax + TCP_SOCKET.SND_UNA]
  22.         jne     .not_idle
  23.  
  24.         mov     ebx, [eax + TCP_SOCKET.t_idle]
  25.         cmp     ebx, [eax + TCP_SOCKET.t_rxtcur]
  26.         jle     .not_idle
  27.  
  28. ; We have been idle for a while and no ACKS are expected to clock out any data we send..
  29. ; Slow start to get ack "clock" running again.
  30.  
  31.         mov     ebx, [eax + TCP_SOCKET.t_maxseg]
  32.         mov     [eax + TCP_SOCKET.SND_CWND], ebx
  33.  
  34.   .not_idle:
  35.   .again:
  36.         mov     ebx, [eax + TCP_SOCKET.SND_NXT]         ; calculate offset (71)
  37.         sub     ebx, [eax + TCP_SOCKET.SND_UNA]         ;
  38.  
  39.         mov     ecx, [eax + TCP_SOCKET.SND_WND]         ; determine window
  40.         cmp     ecx, [eax + TCP_SOCKET.SND_CWND]        ;
  41.         jl      @f                                      ;
  42.         mov     ecx, [eax + TCP_SOCKET.SND_CWND]        ;
  43.        @@:                                              ;
  44.  
  45.         call    TCP_outflags                            ; flags in dl
  46.  
  47. ;------------------------
  48. ; data being forced out ?
  49.  
  50. ; If in persist timeout with window of 0, send 1 byte.
  51. ; Otherwise, if window is small but nonzero, and timer expired,
  52. ; we will send what we can and go to transmit state
  53.  
  54.         test    [eax + TCP_SOCKET.t_force], -1
  55.         jz      .no_force
  56.  
  57.         test    ecx, ecx
  58.         jnz     .no_zero_window
  59.  
  60.         cmp     ebx, [eax + STREAM_SOCKET.snd + RING_BUFFER.size]
  61.         jge     @f
  62.  
  63.         and     dl, not (TH_FIN)          ; clear the FIN flag    ??? how can it be set before?
  64.  
  65.        @@:
  66.         inc     ecx
  67.         jmp     .no_force
  68.  
  69.   .no_zero_window:
  70.         mov     [eax + TCP_SOCKET.timer_persist], 0
  71.         mov     [eax + TCP_SOCKET.t_rxtshift], 0
  72.  
  73.   .no_force:
  74.  
  75. ;--------------------------------
  76. ; Calculate how much data to send (106)
  77.  
  78.         mov     esi, [eax + STREAM_SOCKET.snd + RING_BUFFER.size]
  79.         cmp     esi, ecx
  80.         jl      @f
  81.         mov     esi, ecx
  82.        @@:
  83.         sub     esi, ebx
  84.  
  85. ;------------------------
  86. ; check for window shrink (107)
  87.  
  88. ; If FIN has been set, but not ACKed, but we havent been called to retransmit, esi will be -1
  89. ; Otherwise, window shrank after we sent into it.
  90.  
  91.         jns     .not_negative
  92.  
  93. ; enter persist state
  94.         xor     esi, esi
  95.  
  96. ; If window shrank to 0
  97.         test    ecx, ecx
  98.         jnz     @f
  99.  
  100. ; cancel pending retransmit
  101.         mov     [eax + TCP_SOCKET.timer_retransmission], 0
  102.  
  103. ; pull SND_NXT back to (closed) window, We will enter persist state below.
  104.         push    [eax + TCP_SOCKET.SND_UNA]
  105.         pop     [eax + TCP_SOCKET.SND_NXT]
  106.  
  107.        @@:
  108.  
  109. ; If window didn't close completely, just wait for an ACK
  110.  
  111.   .not_negative:
  112.  
  113. ;---------------------------
  114. ; Send one segment at a time (124)
  115.  
  116.         cmp     esi, [eax + TCP_SOCKET.t_maxseg]
  117.         jle     @f
  118.  
  119.         mov     esi, [eax + TCP_SOCKET.t_maxseg]
  120.  
  121. ;;; sendalot = 1
  122.  
  123.        @@:
  124.  
  125. ;--------------------------------------------
  126. ; Turn of FIN flag if send buffer not emptied (128)
  127.  
  128.         mov     edi, [eax + TCP_SOCKET.SND_NXT]
  129.         add     edi, esi
  130.         sub     edi, [eax + TCP_SOCKET.SND_UNA]
  131.         sub     edi, [eax + STREAM_SOCKET.snd + RING_BUFFER.size]
  132.         jns     @f
  133.  
  134.         and     dl, not (TH_FIN)
  135.  
  136.        @@:
  137.  
  138. ;-------------------------------
  139. ; calculate window advertisement (130)
  140.  
  141.         mov     ecx, SOCKET_MAXDATA
  142.         sub     ecx, [eax + STREAM_SOCKET.rcv + RING_BUFFER.size]
  143.  
  144. ;------------------------------
  145. ; Sender silly window avoidance (131)
  146.  
  147.         test    esi, esi
  148.         jz      .len_zero
  149.  
  150.         cmp     esi, [eax + TCP_SOCKET.t_maxseg]
  151.         je      .send
  152.  
  153. ;;; if (idle or TF_NODELAY) && (esi + ebx >= so_snd.sb_cc), send
  154.  
  155.         test    [eax + TCP_SOCKET.t_force], -1  ;;;
  156.         jnz     .send
  157.  
  158.         mov     ebx, [eax + TCP_SOCKET.max_sndwnd]
  159.         shr     ebx, 1
  160.         cmp     esi, ebx
  161.         jge     .send
  162.  
  163.         mov     ebx, [eax + TCP_SOCKET.SND_NXT]
  164.         cmp     ebx, [eax + TCP_SOCKET.SND_MAX]
  165.         jl      .send
  166.  
  167.   .len_zero:
  168.  
  169. ;----------------------------------------
  170. ; Check if a window update should be sent (154)
  171.  
  172.         test    ecx, ecx
  173.         jz      .no_window
  174.  
  175. ;;; TODO 167-172
  176.  
  177.   .no_window:
  178.  
  179. ;--------------------------
  180. ; Should a segment be sent? (174)
  181.  
  182.         test    [eax + TCP_SOCKET.t_flags], TF_ACKNOW   ; we need to ACK
  183.         jnz     .send
  184.  
  185.         test    dl, TH_SYN + TH_RST                     ; we need to send a SYN or RST
  186.         jnz     .send
  187.  
  188.         mov     ebx, [eax + TCP_SOCKET.SND_UP]          ; when urgent pointer is beyond start of send bufer
  189.         cmp     ebx, [eax + TCP_SOCKET.SND_UNA]
  190.         jg      .send
  191.  
  192.         test    dl, TH_FIN
  193.         jz      .enter_persist  ; no reason to send, enter persist state
  194.  
  195. ; FIN was set, only send if not already sent, or on retransmit
  196.  
  197.         test    [eax + TCP_SOCKET.t_flags], TF_SENTFIN
  198.         jnz     .send
  199.  
  200.         mov     ebx, [eax + TCP_SOCKET.SND_NXT]
  201.         cmp     ebx, [eax + TCP_SOCKET.SND_UNA]
  202.         je      .send
  203.  
  204. ;--------------------
  205. ; Enter persist state (191)
  206.  
  207.   .enter_persist:
  208.  
  209.         DEBUGF  1,"Entering persist state\n"
  210.  
  211.  
  212. ;;; 213 - 217
  213.  
  214. ;----------------------------
  215. ; No reason to send a segment (219)
  216.  
  217.         DEBUGF  1,"No reason to send a segment\n"
  218.  
  219.         mov     [eax + SOCKET.lock], 0
  220.  
  221.         ret
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231. ;-----------------------------------------------
  232. ;
  233. ; Send a segment (222)
  234. ;
  235. ; eax = socket pointer
  236. ; esi = data len
  237. ;  dl = flags
  238. ;
  239. ;-----------------------------------------------
  240.  
  241.   .send:
  242.  
  243.         DEBUGF  1,"Preparing to send a segment\n"
  244.  
  245.         mov     edi, TCP_segment.Data   ; edi will contain headersize
  246.  
  247.         sub     esp, 8                  ; create some space on stack
  248.         push    eax                     ; save socket pointer
  249.  
  250. ;------------------------------------
  251. ; Send options with first SYN segment
  252.  
  253.         test    dl, TH_SYN
  254.         jz      .options_done
  255.  
  256.         push    [eax + TCP_SOCKET.ISS]
  257.         pop     [eax + TCP_SOCKET.SND_NXT]
  258.  
  259.         test    [eax + TCP_SOCKET.t_flags], TF_NOOPT
  260.         jnz     .options_done
  261.  
  262.         mov     ecx, 1460
  263.         or      ecx, TCP_OPT_MAXSEG shl 24 + 4 shl 16
  264.         bswap   ecx
  265.         push    ecx
  266.         add     di, 4
  267.  
  268.         test    [eax + TCP_SOCKET.t_flags], TF_REQ_SCALE
  269.         jz      .no_syn
  270.  
  271.         test    dl, TH_ACK
  272.         jnz     .scale_opt
  273.  
  274.         test    [eax + TCP_SOCKET.t_flags], TF_RCVD_SCALE
  275.         jz      .no_syn
  276.  
  277.   .scale_opt:
  278.         movzx   ecx, byte [eax + TCP_SOCKET.request_r_scale]
  279.         or      ecx, TCP_OPT_WINDOW shl 24 + 4 shl 16 + TCP_OPT_NOP shl 8
  280.         bswap   ecx
  281.         pushd   ecx
  282.         add     di, 4
  283.  
  284.   .no_syn:
  285.  
  286. ;------------------------------------
  287. ; Make the timestamp option if needed
  288.  
  289.         test    [eax + TCP_SOCKET.t_flags], TF_REQ_TSTMP
  290.         jz      .no_timestamp
  291.  
  292.         test    dl, TH_RST
  293.         jnz     .no_timestamp
  294.  
  295.         test    dl, TH_ACK
  296.         jz      .timestamp
  297.  
  298.         test    [eax + TCP_SOCKET.t_flags], TF_RCVD_TSTMP
  299.         jz      .no_timestamp
  300.  
  301.   .timestamp:
  302.         mov     ebx, [timer_ticks]
  303.         bswap   ebx
  304.         push    ebx
  305.         pushw   0
  306.         pushd   TCP_OPT_TIMESTAMP + 10 shl 8 + TCP_OPT_NOP shl 16 + TCP_OPT_NOP shl 24
  307.         add     di, 10
  308.  
  309.   .no_timestamp:
  310.  
  311.         ; <Add additional options here>
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.   .options_done:
  321.  
  322. ; eax = socket ptr
  323. ; edx = flags
  324. ; edi = header size
  325. ; esi = data len
  326.  
  327. ;---------------------------------------------
  328. ; check if we dont exceed the max segment size (270)
  329.  
  330.         add     esi, edi                        ; total TCP segment size
  331.         cmp     esi, [eax + TCP_SOCKET.t_maxseg]
  332.         jle     .no_overflow
  333.  
  334.         mov     esi, [eax + TCP_SOCKET.t_maxseg]
  335.  
  336. ;;; sendalot = 1
  337.  
  338.   .no_overflow:
  339.  
  340. ;-----------------------------------------------------------------
  341. ; Start by pushing all TCP header values in reverse order on stack
  342. ; (essentially, creating the tcp header on the stack!)
  343.  
  344.         pushw   0       ;        .UrgentPointer          dw ?
  345.         pushw   0       ;        .Checksum               dw ?
  346.         pushw   0x00a0  ;        .Window                 dw ?    ;;;;;;;
  347.         shl     edi, 2  ;        .DataOffset             db ?  only 4 left-most bits
  348.         shl     dx, 8
  349.         or      dx, di  ;        .Flags                  db ?
  350.         pushw   dx
  351.         shr     edi, 2  ;        .DataOffset             db ? ;;;;
  352.  
  353.         push    [eax + TCP_SOCKET.RCV_NXT]      ;        .AckNumber              dd ?
  354.         ntohd   [esp]
  355.  
  356.         push    [eax + TCP_SOCKET.SND_NXT]      ;        .SequenceNumber         dd ?
  357.         ntohd   [esp]
  358.  
  359.         push    [eax + TCP_SOCKET.RemotePort]   ;        .DestinationPort        dw ?
  360.         ntohw   [esp]
  361.  
  362.         push    [eax + TCP_SOCKET.LocalPort]    ;        .SourcePort             dw ?
  363.         ntohw   [esp]
  364.  
  365.         push    edi             ; header size
  366.  
  367. ;---------------------
  368. ; Create the IP packet
  369.  
  370.         mov     ecx, esi
  371.  
  372.         mov     ebx, [eax + IP_SOCKET.LocalIP]  ; source ip
  373.         mov     eax, [eax + IP_SOCKET.RemoteIP] ; dest ip
  374.         mov     di, IP_PROTO_TCP shl 8 + 128
  375.         call    IPv4_output
  376.         jz      .fail
  377.  
  378. ;-----------------------------------------
  379. ; Move TCP header from stack to TCP packet
  380.  
  381.         push    ecx
  382.         mov     ecx, [esp+4]
  383.         lea     esi, [esp+4+4]
  384.         shr     ecx, 2
  385.         rep     movsd
  386.         pop     ecx             ; full TCP packet size
  387.  
  388.         pop     esi             ; headersize
  389.         add     esp, esi
  390.  
  391.         mov     [esp + 4], eax          ; packet ptr
  392.         mov     [esp + 4+4], edx        ; packet size
  393.  
  394.         mov     edx, edi                ; begin of data
  395.         sub     edx, esi                ; begin of packet (edi = begin of data)
  396.         push    ecx
  397.         sub     ecx, esi                ; data size
  398.  
  399. ;--------------
  400. ; Copy the data
  401.  
  402. ; eax = ptr to ring struct
  403. ; ecx = buffer size
  404. ; edi = ptr to buffer
  405.  
  406.         mov     eax, [esp+4]                    ; get socket ptr
  407.  
  408.         add     [eax + TCP_SOCKET.SND_NXT], ecx ; update sequence number
  409.  
  410.         add     eax, STREAM_SOCKET.snd
  411.         push    edx
  412.         call    SOCKET_ring_read
  413.         pop     esi                             ; begin of data
  414.         pop     ecx                             ; full packet size
  415.         pop     eax                             ; socket ptr
  416.  
  417. ;----------------------------------
  418. ; update sequence number and timers  (400)
  419.  
  420.         test    [esi + TCP_segment.Flags], TH_SYN + TH_FIN
  421.         jz      @f
  422.         inc     [eax + TCP_SOCKET.SND_NXT]      ; syn and fin take a sequence number
  423.         test    [esi + TCP_segment.Flags], TH_FIN
  424.         jz      @f
  425.         or      [eax + TCP_SOCKET.t_flags], TF_SENTFIN  ; if we sent a fin, set the sentfin flag
  426.        @@:
  427.  
  428.         mov     edx, [eax + TCP_SOCKET.SND_NXT]
  429.         cmp     edx, [eax + TCP_SOCKET.SND_MAX]
  430.         jle     @f
  431.         mov     [eax + TCP_SOCKET.SND_MAX], edx
  432.  
  433.         ;;;; TODO: time transmission (420)
  434.  
  435.        @@:
  436.  
  437. ; set retransmission timer if not already set, and not doing an ACK or keepalive probe
  438.  
  439.         cmp     [eax + TCP_SOCKET.timer_retransmission], 1000 ;;;;
  440.         jl      .retransmit_set
  441.  
  442.         cmp     edx, [eax + TCP_SOCKET.SND_UNA]         ; edx = [eax + TCP_SOCKET.SND_NXT]
  443.         je      .retransmit_set
  444.  
  445.         mov     edx, [eax + TCP_SOCKET.t_rxtcur]
  446.         mov     [eax + TCP_SOCKET.timer_retransmission], dx
  447.  
  448.         mov     [eax + TCP_SOCKET.timer_persist], 0
  449.         mov     [eax + TCP_SOCKET.t_rxtshift], 0        ;;; TODO: only do this if timer_persist was set
  450.  
  451.  
  452.   .retransmit_set:
  453.  
  454. ;--------------------
  455. ; Create the checksum
  456.  
  457.         DEBUGF  1,"checksum: ptr=%x size=%u\n", esi, ecx
  458.  
  459.         TCP_checksum (eax + IP_SOCKET.LocalIP), (eax + IP_SOCKET.RemoteIP)
  460.         mov     [esi+TCP_segment.Checksum], dx
  461.  
  462. ; unlock socket
  463.  
  464.         mov     [eax + SOCKET.lock], 0
  465.  
  466. ;----------------
  467. ; Send the packet
  468.  
  469.         DEBUGF  1,"Sending TCP Packet to device %x\n", ebx
  470.         call    [ebx + NET_DEVICE.transmit]
  471.         ret
  472.  
  473.  
  474.   .fail:
  475.         pop     ecx
  476.         add     esp, ecx
  477.         add     esp, 4+8
  478.         mov     [eax + SOCKET.lock], 0
  479.         DEBUGF 1,"TCP_output: failed\n"
  480.         ret
  481.  
  482.  
  483.