From e32ea5d3c8c9646b8d24206c0bd38d27cb4a8572 Mon Sep 17 00:00:00 2001 From: hidnplayr Date: Wed, 7 Jul 2021 19:26:17 +0000 Subject: [PATCH] Network device error/missed/dropped packet counters. + Network buffer sanity checks. git-svn-id: svn://kolibrios.org@9017 a494cfbc-eb01-0410-851d-a64ba20cac60 --- kernel/trunk/docs/sysfuncr.txt | 78 +++++++++++++++++ kernel/trunk/docs/sysfuncs.txt | 80 +++++++++++++++++- kernel/trunk/network/ethernet.inc | 66 +++++++++++---- kernel/trunk/network/stack.inc | 120 +++++++++++++++++++++++---- programs/KOSfuncs.inc | 6 ++ programs/network/netstat/netstat.asm | 81 ++++++++++++++---- 6 files changed, 383 insertions(+), 48 deletions(-) diff --git a/kernel/trunk/docs/sysfuncr.txt b/kernel/trunk/docs/sysfuncr.txt index b88aeb96f..ab3d0ae6d 100644 --- a/kernel/trunk/docs/sysfuncr.txt +++ b/kernel/trunk/docs/sysfuncr.txt @@ -4615,6 +4615,84 @@ Architecture Software Developer's Manual, Volume 3, Appendix B); eax - SF_NETWORK_GET (74) bl - SSF_LINK_STATUS (10) ====================================================================== +==== Function 74, Subfunction 11, Get TX error packets counter. ====== +====================================================================== +Parameters: + * eax = 74 - function number + * bl = 11 - subfunction number + * bh = device number +Returned value: + * eax = Number of erroneous packets received since device start, -1 on error + +---------------------- Constants for registers: ---------------------- + eax - SF_NETWORK_GET (74) + bl - SSF_TX_PACKET_ERROR_COUNT (11) +====================================================================== +=== Function 74, Subfunction 12, Get TX dropped packets counter. ===== +====================================================================== +Parameters: + * eax = 74 - function number + * bl = 12 - subfunction number + * bh = device number +Returned value: + * eax = Number of dropped packets since device start, -1 on error + +---------------------- Constants for registers: ---------------------- + eax - SF_NETWORK_GET (74) + bl - SSF_TX_PACKET_DROP_COUNT (12) +====================================================================== +==== Function 74, Subfunction 13, Get TX missed packets counter. ===== +====================================================================== +Parameters: + * eax = 74 - function number + * bl = 13 - subfunction number + * bh = device number +Returned value: + * eax = Number of missed packets since device start, -1 on error + +---------------------- Constants for registers: ---------------------- + eax - SF_NETWORK_GET (74) + bl - SSF_TX_PACKET_MISS_COUNT (13) + ====================================================================== +==== Function 74, Subfunction 14, Get RX error packets counter. ====== +====================================================================== +Parameters: + * eax = 74 - function number + * bl = 14 - subfunction number + * bh = device number +Returned value: + * eax = Number of erroneous packets received since device start, -1 on error + +---------------------- Constants for registers: ---------------------- + eax - SF_NETWORK_GET (74) + bl - SSF_RX_PACKET_ERROR_COUNT (14) +====================================================================== +=== Function 74, Subfunction 15, Get RX dropped packets counter. ===== +====================================================================== +Parameters: + * eax = 74 - function number + * bl = 15 - subfunction number + * bh = device number +Returned value: + * eax = Number of dropped packets since device start, -1 on error + +---------------------- Constants for registers: ---------------------- + eax - SF_NETWORK_GET (74) + bl - SSF_RX_PACKET_DROP_COUNT (12) +====================================================================== +==== Function 74, Subfunction 16, Get RX missed packets counter. ===== +====================================================================== +Parameters: + * eax = 74 - function number + * bl = 16 - subfunction number + * bh = device number +Returned value: + * eax = Number of missed packets since device start, -1 on error + +---------------------- Constants for registers: ---------------------- + eax - SF_NETWORK_GET (74) + bl - SSF_RX_PACKET_MISS_COUNT (16) +====================================================================== ============== Функция 75, подфункция 0, Открыть сокет. ============== ====================================================================== Параметры: diff --git a/kernel/trunk/docs/sysfuncs.txt b/kernel/trunk/docs/sysfuncs.txt index 0115e0d0d..0cb41aca3 100644 --- a/kernel/trunk/docs/sysfuncs.txt +++ b/kernel/trunk/docs/sysfuncs.txt @@ -1,6 +1,6 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; -;; Copyright (C) KolibriOS team 2004-2016. All rights reserved. ;; +;; Copyright (C) KolibriOS team 2004-2021. All rights reserved. ;; ;; Distributed under terms of the GNU General Public License ;; ;; ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -4590,6 +4590,84 @@ Returned value: eax - SF_NETWORK_GET (74) bl - SSF_LINK_STATUS (10) ====================================================================== +==== Function 74, Subfunction 11, Get TX error packets counter. ====== +====================================================================== +Parameters: + * eax = 74 - function number + * bl = 11 - subfunction number + * bh = device number +Returned value: + * eax = Number of erroneous packets received since device start, -1 on error + +---------------------- Constants for registers: ---------------------- + eax - SF_NETWORK_GET (74) + bl - SSF_TX_PACKET_ERROR_COUNT (11) +====================================================================== +=== Function 74, Subfunction 12, Get TX dropped packets counter. ===== +====================================================================== +Parameters: + * eax = 74 - function number + * bl = 12 - subfunction number + * bh = device number +Returned value: + * eax = Number of dropped packets since device start, -1 on error + +---------------------- Constants for registers: ---------------------- + eax - SF_NETWORK_GET (74) + bl - SSF_TX_PACKET_DROP_COUNT (12) +====================================================================== +==== Function 74, Subfunction 13, Get TX missed packets counter. ===== +====================================================================== +Parameters: + * eax = 74 - function number + * bl = 13 - subfunction number + * bh = device number +Returned value: + * eax = Number of missed packets since device start, -1 on error + +---------------------- Constants for registers: ---------------------- + eax - SF_NETWORK_GET (74) + bl - SSF_TX_PACKET_MISS_COUNT (13) + ====================================================================== +==== Function 74, Subfunction 14, Get RX error packets counter. ====== +====================================================================== +Parameters: + * eax = 74 - function number + * bl = 14 - subfunction number + * bh = device number +Returned value: + * eax = Number of erroneous packets received since device start, -1 on error + +---------------------- Constants for registers: ---------------------- + eax - SF_NETWORK_GET (74) + bl - SSF_RX_PACKET_ERROR_COUNT (14) +====================================================================== +=== Function 74, Subfunction 15, Get RX dropped packets counter. ===== +====================================================================== +Parameters: + * eax = 74 - function number + * bl = 15 - subfunction number + * bh = device number +Returned value: + * eax = Number of dropped packets since device start, -1 on error + +---------------------- Constants for registers: ---------------------- + eax - SF_NETWORK_GET (74) + bl - SSF_RX_PACKET_DROP_COUNT (12) +====================================================================== +==== Function 74, Subfunction 16, Get RX missed packets counter. ===== +====================================================================== +Parameters: + * eax = 74 - function number + * bl = 16 - subfunction number + * bh = device number +Returned value: + * eax = Number of missed packets since device start, -1 on error + +---------------------- Constants for registers: ---------------------- + eax - SF_NETWORK_GET (74) + bl - SSF_RX_PACKET_MISS_COUNT (16) +====================================================================== ============== Function 75, Subfunction 0, Open socket. ============== ====================================================================== Parameters: diff --git a/kernel/trunk/network/ethernet.inc b/kernel/trunk/network/ethernet.inc index 22ae4377b..b17eaaf8e 100644 --- a/kernel/trunk/network/ethernet.inc +++ b/kernel/trunk/network/ethernet.inc @@ -1,6 +1,6 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; -;; Copyright (C) KolibriOS team 2004-2019. All rights reserved. ;; +;; Copyright (C) KolibriOS team 2004-2021. All rights reserved. ;; ;; Distributed under terms of the GNU General Public License ;; ;; ;; ;; ETHERNET.INC ;; @@ -76,8 +76,17 @@ align 4 eth_input: pop eax - pushf - cli + +if defined NETWORK_SANITY_CHECKS + cmp eax, [net_buffs_low] + jb .assert_mbuff + cmp eax, [net_buffs_high] + ja .assert_mbuff + test eax, 0x7ff + jnz .assert_mbuff +end if + + spin_lock_irqsave cmp [ETH_frame_queued], ETH_QUEUE_SIZE jae .full @@ -92,7 +101,7 @@ eth_input: mov [ETH_frame_tail], eax mov [ebx + NET_BUFF.NextPtr], eax - popf + spin_unlock_irqrestore ; Mark it as being an Ethernet Frame mov [eax + NET_BUFF.type], NET_BUFF_ETH @@ -107,12 +116,21 @@ eth_input: ret .full: - DEBUGF DEBUG_NETWORK_ERROR, "ETH incoming queue is full, discarding packet!\n" - popf - push eax - call net_buff_free + mov ebx, [eax + NET_BUFF.device] + inc [ebx + NET_DEVICE.packets_rx_ovr] + DEBUGF DEBUG_NETWORK_VERBOSE, "ETH incoming queue is full, discarding packet!\n" + spin_unlock_irqrestore + stdcall net_buff_free, eax ret +if defined NETWORK_SANITY_CHECKS + .assert_mbuff: + DEBUGF DEBUG_NETWORK_ERROR, "eth_input: invalid buffer 0x%x\n", eax + DEBUGF DEBUG_NETWORK_ERROR, "eth_input: caller=0x%x\n", [esp+4] + xor eax, eax + ret +end if + ;-----------------------------------------------------------------; @@ -164,7 +182,7 @@ eth_process_input: DEBUGF DEBUG_NETWORK_VERBOSE, "ETH_input: size=%u\n", ecx sub ecx, sizeof.ETH_header - jb .dump + jb .err ; Set registers for protocol handlers lea edx, [eax + sizeof.ETH_header] @@ -188,11 +206,23 @@ eth_process_input: DEBUGF DEBUG_NETWORK_VERBOSE, "ETH_input: Unknown packet type=%x\n", ax - .dump: - DEBUGF DEBUG_NETWORK_VERBOSE, "ETH_input: dumping\n" + .drop: + mov eax, [esp] + mov eax, [eax + NET_BUFF.device] + inc [eax + NET_DEVICE.packets_rx_drop] + DEBUGF DEBUG_NETWORK_VERBOSE, "ETH_input: dropping\n" call net_buff_free ret + .err: + mov eax, [esp] + mov eax, [eax + NET_BUFF.device] + inc [eax + NET_DEVICE.packets_rx_err] + DEBUGF DEBUG_NETWORK_VERBOSE, "ETH_input: invalid frame received\n" + call net_buff_free + ret + + ;-----------------------------------------------------------------; @@ -216,7 +246,7 @@ eth_output: DEBUGF DEBUG_NETWORK_VERBOSE, "ETH_output: size=%u device=%x\n", ecx, ebx cmp ecx, [ebx + ETH_DEVICE.mtu] - ja .exit + ja .too_large push ecx push ax edx @@ -225,6 +255,7 @@ eth_output: stdcall net_buff_alloc, ecx test eax, eax jz .out_of_ram + mov [eax + NET_BUFF.type], NET_BUFF_ETH mov [eax + NET_BUFF.device], ebx mov [eax + NET_BUFF.offset], NET_BUFF.data @@ -256,13 +287,16 @@ eth_output: jmp .done .out_of_ram: - DEBUGF DEBUG_NETWORK_ERROR, "ETH_output: Out of ram!\n" - add esp, 4+2+4 + inc [ebx + NET_DEVICE.packets_tx_drop] + DEBUGF DEBUG_NETWORK_VERBOSE, "ETH_output: Out of ram!\n" + add esp, 4+2 + pop ecx xor eax, eax ret - .exit: - DEBUGF DEBUG_NETWORK_ERROR, "ETH_output: Packet too large!\n" + .too_large: + inc [eax + NET_DEVICE.packets_tx_err] + DEBUGF DEBUG_NETWORK_VERBOSE, "ETH_output: Packet too large!\n" xor eax, eax ret diff --git a/kernel/trunk/network/stack.inc b/kernel/trunk/network/stack.inc index 4e7f99a43..77263b20c 100644 --- a/kernel/trunk/network/stack.inc +++ b/kernel/trunk/network/stack.inc @@ -30,6 +30,7 @@ endg DEBUG_NETWORK_ERROR = 1 DEBUG_NETWORK_VERBOSE = 0 +NETWORK_SANITY_CHECKS = 1 NET_DEVICES_MAX = 16 NET_BUFFERS = 512 @@ -262,8 +263,13 @@ align 4 net_device_count dd ? net_device_list rd NET_DEVICES_MAX - net_buffs_free rd NET_BUFFERS - .current dd ? + net_buffs_free rd NET_BUFFERS ; list of pointers to actual net buffs + .current dd ? ; pointer to current element in net_buffs_free list + +if defined NETWORK_SANITY_CHECKS + net_buffs_low dd ? ; actual net buff mem region start + net_buffs_high dd ? ; actual net buff mem region stop +end if endg @@ -284,6 +290,10 @@ stack_init: test eax, eax jz .fail +if defined NETWORK_SANITY_CHECKS + mov [net_buffs_low], eax +end if + mov edi, net_buffs_free mov ecx, NET_BUFFERS cld @@ -293,6 +303,11 @@ stack_init: dec ecx jnz .loop +if defined NETWORK_SANITY_CHECKS + sub eax, NET_BUFFER_SIZE + mov [net_buffs_high], eax +end if + mov eax, net_buffs_free stosd @@ -394,6 +409,16 @@ proc net_buff_alloc stdcall, buffersize spin_unlock_irqrestore +if defined NETWORK_SANITY_CHECKS + cmp eax, [net_buffs_low] + cmp eax, [net_buffs_low] + jb .assert_mbuff + cmp eax, [net_buffs_high] + ja .assert_mbuff + test eax, 0x7ff + jnz .assert_mbuff +end if + DEBUGF DEBUG_NETWORK_VERBOSE, "net_buff_alloc: 0x%x\n", eax ret @@ -408,6 +433,15 @@ proc net_buff_alloc stdcall, buffersize xor eax, eax DEBUGF DEBUG_NETWORK_ERROR, "net_buff_alloc: too large!\n" ret + +if defined NETWORK_SANITY_CHECKS + .assert_mbuff: + DEBUGF DEBUG_NETWORK_ERROR, "net_buff_alloc: invalid buffer 0x%x\n", eax + DEBUGF DEBUG_NETWORK_ERROR, "net_buff_alloc: caller=0x%x\n", [esp+4] + xor eax, eax + ret +end if + endp @@ -416,6 +450,16 @@ proc net_buff_free stdcall, buffer DEBUGF DEBUG_NETWORK_VERBOSE, "net_buff_free: 0x%x\n", [buffer] +if defined NETWORK_SANITY_CHECKS + mov eax, [buffer] + cmp eax, [net_buffs_low] + jb .assert_mbuff + cmp eax, [net_buffs_high] + ja .assert_mbuff + test eax, 0x7ff + jnz .assert_mbuff +end if + spin_lock_irqsave sub [net_buffs_free.current], 4 ; move pointer backwards @@ -426,6 +470,15 @@ proc net_buff_free stdcall, buffer spin_unlock_irqrestore ret + +if defined NETWORK_SANITY_CHECKS + .assert_mbuff: + DEBUGF DEBUG_NETWORK_ERROR, "net_buff_free: invalid buffer 0x%x\n", eax + DEBUGF DEBUG_NETWORK_ERROR, "net_buff_free: caller=0x%x\n", [esp+4] + xor eax, eax + ret +end if + endp @@ -748,18 +801,25 @@ sys_network: jmp dword [.table + 4*ebx] .table: - dd .get_type ; 0 - dd .get_dev_name ; 1 - dd .reset ; 2 - dd .stop ; 3 - dd .get_ptr ; 4 - dd .get_drv_name ; 5 + dd .get_type ; 0 + dd .get_dev_name ; 1 + dd .reset ; 2 + dd .stop ; 3 + dd .get_ptr ; 4 + dd .get_drv_name ; 5 - dd .packets_tx ; 6 - dd .packets_rx ; 7 - dd .bytes_tx ; 8 - dd .bytes_rx ; 9 + dd .packets_tx ; 6 + dd .packets_rx ; 7 + dd .bytes_tx ; 8 + dd .bytes_rx ; 9 dd .state ; 10 + dd .packets_tx_err ; 11 + dd .packets_tx_drop ; 12 + dd .packets_tx_ovr ; 13 + dd .packets_rx_err ; 14 + dd .packets_rx_drop ; 15 + dd .packets_rx_ovr ; 16 + .number = ($ - .table) / 4 - 1 .get_type: @@ -768,12 +828,10 @@ sys_network: ret .get_dev_name: -; { Patch by Coldy, sanity check - mov ebx, eax ; eax will used for is_region_userspace return + mov ebx, eax stdcall is_region_userspace, ecx, 64 jz .bad_buffer - mov esi, [ebx + NET_DEVICE.name] ;mov esi, [eax + NET_DEVICE.name] -; } End patch by Coldy, sanity check + mov esi, [ebx + NET_DEVICE.name] mov edi, ecx mov ecx, 64/4 ; max length @@ -828,6 +886,36 @@ sys_network: mov [esp+32], eax ret + .packets_tx_err: + mov eax, [eax + NET_DEVICE.packets_tx_err] + mov [esp+32], eax + ret + + .packets_tx_drop: + mov eax, [eax + NET_DEVICE.packets_tx_drop] + mov [esp+32], eax + ret + + .packets_tx_ovr: + mov eax, [eax + NET_DEVICE.packets_tx_ovr] + mov [esp+32], eax + ret + + .packets_rx_err: + mov eax, [eax + NET_DEVICE.packets_rx_err] + mov [esp+32], eax + ret + + .packets_rx_drop: + mov eax, [eax + NET_DEVICE.packets_rx_drop] + mov [esp+32], eax + ret + + .packets_rx_ovr: + mov eax, [eax + NET_DEVICE.packets_rx_ovr] + mov [esp+32], eax + ret + .state: mov eax, [eax + NET_DEVICE.link_state] mov [esp+32], eax diff --git a/programs/KOSfuncs.inc b/programs/KOSfuncs.inc index 219f97e4d..4c484ae25 100644 --- a/programs/KOSfuncs.inc +++ b/programs/KOSfuncs.inc @@ -233,6 +233,12 @@ SF_NETWORK_DEVICE=74 SSF_TX_BYTE_COUNT=8 SSF_RX_BYTE_COUNT=9 SSF_LINK_STATUS=10 + SSF_TX_PACKET_ERROR_COUNT=11 + SSF_TX_PACKET_DROP_COUNT=12 + SSF_TX_PACKET_MISS_COUNT=13 + SSF_RX_PACKET_ERROR_COUNT=14 + SSF_RX_PACKET_DROP_COUNT=15 + SSF_RX_PACKET_MISS_COUNT=16 SF_NETWORK_SOCKET=75 SSF_OPEN=0 SSF_CLOSE=1 diff --git a/programs/network/netstat/netstat.asm b/programs/network/netstat/netstat.asm index 6092a5357..09d77d634 100644 --- a/programs/network/netstat/netstat.asm +++ b/programs/network/netstat/netstat.asm @@ -1,6 +1,6 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; -;; Copyright (C) KolibriOS team 2010-2018. All rights reserved. ;; +;; Copyright (C) KolibriOS team 2010-2021. All rights reserved. ;; ;; Distributed under terms of the GNU General Public License ;; ;; ;; ;; netstat.asm - Network Status Tool for KolibriOS ;; @@ -144,6 +144,23 @@ redraw: cmp [mode], 101 jne .no_eth + mcall 4, 212 shl 16 + 35, 0x80000000, str_err + add ebx, 18 + mov edx, str_dropped + mcall + add ebx, 18 + mov edx, str_missed + mcall + add ebx, 18 + mov edx, str_err + mcall + add ebx, 18 + mov edx, str_dropped + mcall + add ebx, 18 + mov edx, str_missed + mcall + mcall 4, 8 shl 16 + 35, 0x80000000, str_packets_tx add ebx, 18 mov edx, str_packets_rx @@ -177,7 +194,7 @@ redraw: push eax push bx - mov edx, 134 shl 16 + 35 + 7*18 + mov edx, 115 shl 16 + 35 + 7*18 call draw_mac jmp end_of_draw @@ -224,7 +241,7 @@ redraw: mcall 76 push eax - mov edx, 134 shl 16 + 35 + 2*18 + mov edx, 115 shl 16 + 35 + 2*18 call draw_ip add edx, 18 @@ -280,7 +297,7 @@ redraw: mcall add ebx, 18 - mov edx, str_dumped + mov edx, str_dropped mcall add ebx, 18 @@ -305,9 +322,42 @@ draw_stats: pop ebx push eax inc bl - cmp bl, 10 + cmp bl, 16 jbe @r +; rx ovr + mov ebx, 0x000a0000 + pop ecx + mov edx, 320 shl 16 + 35 + 5*18 + mov esi, 0x40000000 + mcall 47 + +; rx drop + sub edx, 18 + pop ecx + mcall + +; rx err + sub edx, 18 + pop ecx + mcall + +; tx ovr + sub edx, 18 + pop ecx + mcall + +; tx drop + sub edx, 18 + pop ecx + mcall + +; tx err + sub edx, 18 + pop ecx + mcall + +; Calculate speed pop ecx push [time] @@ -376,19 +426,19 @@ draw_stats: mov edx, str_unknown .print_link: - mov ebx, 134 shl 16 + 35 + 6*18 + mov ebx, 115 shl 16 + 35 + 6*18 mov ecx, 0xc0000000 mov edi, 0x00f3f3f3 mcall 4 -; speed tx +; speed rx mov ebx, 0x000a0000 pop ecx - mov edx, 134 shl 16 + 35 + 5*18 + mov edx, 115 shl 16 + 35 + 5*18 mov esi, 0x40000000 mcall 47 -; speed rx +; speed tx sub edx, 18 pop ecx mcall @@ -437,7 +487,7 @@ not_101: mov ebx, 0x000a0000 pop ecx - mov edx, 134 shl 16 + 35 + 18 + mov edx, 115 shl 16 + 35 + 18 mov esi, 0x40000000 mov edi, 0x00F3F3F3 mcall 47 @@ -481,7 +531,7 @@ not_102: mov ebx, 0x000a0000 pop ecx - mov edx, 134 shl 16 + 35 + 3*18 + mov edx, 115 shl 16 + 35 + 3*18 mov esi, 0x40000000 mov edi, 0x00F3F3F3 mcall 47 @@ -598,7 +648,7 @@ not_103: mov ebx, 0x000a0000 pop ecx - mov edx, 134 shl 16 + 35 + 18 + mov edx, 115 shl 16 + 35 + 18 mov esi, 0x40000000 mov edi, 0x00F3F3F3 mcall 47 @@ -629,7 +679,7 @@ not_104: mov ebx, 0x000a0000 pop ecx - mov edx, 134 shl 16 + 35 + 18 + mov edx, 115 shl 16 + 35 + 18 mov esi, 0x40000000 mov edi, 0x00F3F3F3 mcall 47 @@ -678,7 +728,7 @@ not_105: mov ebx, 0x000a0000 pop ecx - mov edx, 134 shl 16 + 35 + 18*4 + mov edx, 115 shl 16 + 35 + 18*4 mov esi, 0x40000000 mov edi, 0x00F3F3F3 mcall 47 @@ -945,7 +995,8 @@ str_gateway db 'Standard gateway: . . .', 0 str_arp db 'ARP entrys:', 0 str_conflicts db 'ARP conflicts:', 0 str_missed db 'Packets missed:', 0 -str_dumped db 'Packets dumped:', 0 +str_dropped db 'Packets dropped:', 0 +str_err db 'Packet errors:', 0 str_queued db 'Packets queued:', 0 str_link db 'Link state:', 0 str_speed_tx db 'Upload (kb/s):', 0