umka/vnet.c

86 lines
1.8 KiB
C
Raw Normal View History

2020-05-06 23:33:32 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
2020-05-06 23:33:32 +02:00
#include <inttypes.h>
#include <errno.h>
#include "umka.h"
2020-05-06 23:33:32 +02:00
#include "trace.h"
#include "vnet.h"
2020-05-06 23:33:32 +02:00
typedef struct {
int fd;
} vnet_userdata_t;
net_device_t *vnet_init(int fd) {
// printf("vnet_init\n");
vnet_userdata_t *u = (vnet_userdata_t*)malloc(sizeof(vnet_userdata_t));
u->fd = fd;
net_device_t *vnet = (net_device_t*)malloc(sizeof(net_device_t));
*vnet = (net_device_t){
.device_type = NET_TYPE_ETH,
.mtu = 1514,
.name = "UMK0770",
.unload = vnet_unload,
.reset = vnet_reset,
.transmit = vnet_transmit,
.bytes_tx = 0,
.bytes_rx = 0,
.packets_tx = 0,
.packets_rx = 0,
.link_state = ETH_LINK_FD + ETH_LINK_10M,
.hwacc = 0,
.mac = {0x80, 0x2b, 0xf9, 0x3b, 0x6c, 0xca},
.userdata = u,
};
2020-05-06 23:33:32 +02:00
return vnet;
}
__attribute__((__stdcall__))
void vnet_unload() {
printf("vnet_unload\n");
2020-05-06 23:33:32 +02:00
COVERAGE_OFF();
COVERAGE_ON();
}
__attribute__((__stdcall__))
void vnet_reset() {
printf("vnet_reset\n");
2020-05-06 23:33:32 +02:00
COVERAGE_OFF();
COVERAGE_ON();
}
static void dump_net_buff(net_buff_t *buf) {
for (size_t i = 0; i < buf->length; i++) {
printf("%2.2x ", buf->data[i]);
}
putchar('\n');
}
2020-05-06 23:33:32 +02:00
__attribute__((__stdcall__))
int vnet_transmit(net_buff_t *buf) {
net_device_t *vnet;
__asm__ __inline__ __volatile__ (
"nop"
: "=b"(vnet)
:
: "memory");
vnet_userdata_t *u = vnet->userdata;
printf("vnet_transmit: %d bytes\n", buf->length);
dump_net_buff(buf);
write(u->fd, buf->data, buf->length);
buf->length = 0;
2020-05-06 23:33:32 +02:00
COVERAGE_OFF();
COVERAGE_ON();
printf("vnet_transmit: done\n");
return 0;
2020-05-06 23:33:32 +02:00
}