net: Add stats counters

This commit is contained in:
K. Lange 2021-09-30 19:09:04 +09:00
parent 495d1a196a
commit 4ef5ee0416
3 changed files with 43 additions and 0 deletions

View File

@ -45,6 +45,19 @@ static char * flagsToStr(uint32_t flags) {
return out;
}
static int print_human_readable_size(char * _out, size_t s) {
size_t count = 5;
char * prefix = "PTGMK";
for (; count > 0 && *prefix; count--, prefix++) {
size_t base = 1UL << (count * 10);
if (s >= base) {
size_t t = s / base;
return sprintf(_out, "%zu.%1zu %cB", t, (s - t * base) / (base / 10), *prefix);
}
}
return sprintf(_out, "%d B", (int)s);
}
static int configure_interface(const char * if_name) {
char if_path[100];
snprintf(if_path, 100, "/dev/net/%s", if_name);
@ -99,6 +112,15 @@ static int configure_interface(const char * if_name) {
mac_addr[3], mac_addr[4], mac_addr[5]);
}
netif_counters_t counts;
if (!ioctl(netdev, SIOCGIFCOUNTS, &counts)) {
char _buf[100];
print_human_readable_size(_buf, counts.rx_bytes);
fprintf(stdout," RX packets %zu bytes %zu (%s)\n", counts.rx_count, counts.rx_bytes, _buf);
print_human_readable_size(_buf, counts.tx_bytes);
fprintf(stdout," TX packets %zu bytes %zu (%s)\n", counts.tx_count, counts.tx_bytes, _buf);
}
/* TODO stats */
fprintf(stdout,"\n");

View File

@ -1,6 +1,7 @@
#pragma once
#include <_cheader.h>
#include <stdint.h>
_Begin_C_Header
@ -19,6 +20,7 @@ _Begin_C_Header
#define SIOCGIFMTU 0x12340006 /* Get interface mtu */
#define SIOCGIFGATEWAY 0x12340007
#define SIOCSIFGATEWAY 0x12340017
#define SIOCGIFCOUNTS 0x12340018
/**
* Flags for interface status
@ -30,4 +32,11 @@ _Begin_C_Header
#define IFF_RUNNING 0x0010
#define IFF_MULTICAST 0x0020
typedef struct {
size_t tx_count;
size_t tx_bytes;
size_t rx_count;
size_t rx_bytes;
} netif_counters_t;
_End_C_Header

View File

@ -59,6 +59,8 @@ struct e1000_nic {
int configured;
process_t * queuer;
process_t * processor;
netif_counters_t counts;
};
static int device_count = 0;
@ -171,6 +173,8 @@ static void e1000_queuer(void * data) {
while ((nic->rx[nic->rx_index].status & 0x01) && (processed < budget)) {
int i = nic->rx_index;
if (!(nic->rx[i].errors & (0x97))) {
nic->counts.rx_count++;
nic->counts.rx_bytes += nic->rx[i].length;
net_eth_handle((void*)nic->rx_virt[i], nic->eth.device_node);
} else {
printf("error bits set in packet: %x\n", nic->rx[i].errors);
@ -249,6 +253,9 @@ static void send_packet(struct e1000_nic * device, uint8_t* payload, size_t payl
device->tx[device->tx_index].cmd = CMD_EOP | CMD_IFCS | CMD_RS; //| CMD_RPS;
device->tx[device->tx_index].status = 0;
device->counts.tx_count++;
device->counts.tx_bytes += payload_size;
if (++device->tx_index == E1000_NUM_TX_DESC) {
device->tx_index = 0;
}
@ -355,6 +362,11 @@ static int ioctl_e1000(fs_node_t * node, unsigned long request, void * argp) {
return 0;
}
case SIOCGIFCOUNTS: {
memcpy(argp, &nic->counts, sizeof(netif_counters_t));
return 0;
}
default:
return -EINVAL;
}