Subversion Repositories Kolibri OS

Rev

Rev 5984 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3555 Serge 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
5565 serge 3
;; Copyright (C) KolibriOS team 2004-2015. All rights reserved.    ;;
3555 Serge 4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
6
;;  loopback.inc                                                   ;;
7
;;                                                                 ;;
8
;;  LoopBack device for KolibriOS                                  ;;
9
;;                                                                 ;;
10
;;    Written by hidnplayr@kolibrios.org                           ;;
11
;;                                                                 ;;
12
;;          GNU GENERAL PUBLIC LICENSE                             ;;
13
;;             Version 2, June 1991                                ;;
14
;;                                                                 ;;
15
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
16
 
6078 serge 17
$Revision: 6011 $
3555 Serge 18
 
19
iglobal
3725 Serge 20
align 4
3555 Serge 21
 
22
LOOPBACK_DEVICE:
23
 
3626 Serge 24
        .device_type    dd NET_DEVICE_LOOPBACK
25
        .mtu            dd 4096
26
        .name           dd .namestr
3555 Serge 27
 
3626 Serge 28
        .unload         dd .dummy_fn
29
        .reset          dd .dummy_fn
6078 serge 30
        .transmit       dd loop_input
3555 Serge 31
 
3626 Serge 32
        .bytes_tx       dq 0
33
        .bytes_rx       dq 0
34
        .packets_tx     dd 0
35
        .packets_rx     dd 0
3555 Serge 36
 
3626 Serge 37
        .link_state     dd -1
4423 Serge 38
        .hwacc          dd NET_HWACC_TCP_IPv4_IN + NET_HWACC_TCP_IPv4_OUT
3626 Serge 39
 
40
        .namestr        db 'loopback', 0
41
 
3555 Serge 42
        .dummy_fn:
3725 Serge 43
        ret
3555 Serge 44
 
45
endg
46
 
3626 Serge 47
 
6078 serge 48
macro   loop_init {
3626 Serge 49
local   .fail
50
 
51
        mov     ebx, LOOPBACK_DEVICE
6078 serge 52
        call    net_add_device
3626 Serge 53
 
54
        cmp     eax, -1
55
        je      .fail
56
 
57
        mov     [IP_LIST], 127 + 1 shl 24
58
        mov     [SUBNET_LIST], 255
59
        mov     [BROADCAST_LIST], 0xffffff00 + 127
60
 
61
  .fail:
62
}
63
 
5984 serge 64
;-----------------------------------------------------------------;
65
;                                                                 ;
6078 serge 66
; loop_input                                                      ;
5984 serge 67
;                                                                 ;
68
;   IN: [esp+4] = Pointer to buffer                               ;
69
;                                                                 ;
70
;  OUT: /                                                         ;
71
;                                                                 ;
72
;-----------------------------------------------------------------;
3555 Serge 73
align 4
6078 serge 74
loop_input:
3555 Serge 75
 
5565 serge 76
        mov     eax, [esp+4]
3555 Serge 77
 
5565 serge 78
; Update stats
3626 Serge 79
        inc     [LOOPBACK_DEVICE.packets_rx]
5565 serge 80
 
81
        mov     ecx, [eax + NET_BUFF.length]
3626 Serge 82
        add     dword[LOOPBACK_DEVICE.bytes_rx], ecx
83
        adc     dword[LOOPBACK_DEVICE.bytes_rx + 4], 0
84
 
5565 serge 85
        DEBUGF  DEBUG_NETWORK_VERBOSE, "LOOP_input: ptr=%x size=%u\n", eax, ecx
3555 Serge 86
 
5565 serge 87
; Reverse buffptr and returnaddr on stack
88
        pop     edx edi
89
        push    edx edi
90
 
91
; Set registers for protocol handlers
92
        lea     edx, [eax + NET_BUFF.data]
93
        mov     ebx, [eax + NET_BUFF.device]
94
        mov     eax, [eax + NET_BUFF.type]
95
 
96
; Place protocol handlers here
3626 Serge 97
        cmp     eax, AF_INET4
6078 serge 98
        je      ipv4_input
3555 Serge 99
 
5565 serge 100
        DEBUGF  DEBUG_NETWORK_VERBOSE, "LOOP_input: Unknown packet type=%x\n", eax
3555 Serge 101
 
102
  .dump:
3589 Serge 103
        DEBUGF  DEBUG_NETWORK_VERBOSE, "LOOP_input: dumping\n"
6078 serge 104
        call    net_buff_free
3555 Serge 105
        ret
106
 
5565 serge 107
 
5984 serge 108
;-----------------------------------------------------------------;
109
;                                                                 ;
6078 serge 110
; loop_output                                                     ;
5984 serge 111
;                                                                 ;
112
;  IN:  ecx = packet size                                         ;
113
;       edi = address family                                      ;
114
;                                                                 ;
115
; OUT:  eax = start of net frame / 0 on error                     ;
116
;       ebx = to device structure                                 ;
117
;       ecx = unchanged (packet size of embedded data)            ;
118
;       edi = start of payload                                    ;
119
;                                                                 ;
120
;-----------------------------------------------------------------;
3555 Serge 121
align 4
6078 serge 122
loop_output:
3555 Serge 123
 
3589 Serge 124
        DEBUGF  DEBUG_NETWORK_VERBOSE, "LOOP_output\n"
3555 Serge 125
 
5565 serge 126
        cmp     ecx, [LOOPBACK_DEVICE.mtu]
127
        ja      .too_large
3555 Serge 128
 
5565 serge 129
        push    ecx edi
130
        add     ecx, NET_BUFF.data
6078 serge 131
        stdcall net_buff_alloc, ecx
3555 Serge 132
        test    eax, eax
133
        jz      .out_of_ram
134
 
5565 serge 135
        pop     edi
136
        mov     [eax + NET_BUFF.type], edi
137
        mov     ebx, LOOPBACK_DEVICE
138
        mov     [eax + NET_BUFF.device], ebx
3555 Serge 139
        pop     ecx
5565 serge 140
        mov     [eax + NET_BUFF.length], ecx
141
        lea     edi, [eax + NET_BUFF.data]
3555 Serge 142
 
3626 Serge 143
        inc     [LOOPBACK_DEVICE.packets_tx]
144
        add     dword[LOOPBACK_DEVICE.bytes_tx], ecx
145
        adc     dword[LOOPBACK_DEVICE.bytes_tx + 4], 0
146
 
5565 serge 147
        DEBUGF  DEBUG_NETWORK_VERBOSE, "LOOP_output: ptr=%x size=%u\n", eax, ecx
3555 Serge 148
        ret
149
 
5565 serge 150
  .too_large:
151
        DEBUGF  DEBUG_NETWORK_ERROR, "LOOP_output: packet is too large\n"
152
        xor     eax, eax
153
        ret
154
 
3555 Serge 155
  .out_of_ram:
3626 Serge 156
        DEBUGF  DEBUG_NETWORK_ERROR, "LOOP_output: out of memory\n"
157
        add     esp, 4+4
5565 serge 158
        xor     eax, eax
3555 Serge 159
        ret
160