mirror of
https://github.com/Mathewnd/Astral.git
synced 2026-10-27 12:48:22 +00:00
netdev: add a way to get it by name and show it in /dev
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
#define DEV_MAJOR_BLOCK 7
|
||||
#define DEV_MAJOR_E9 8
|
||||
#define DEV_MAJOR_URANDOM 9
|
||||
#define DEV_MAJOR_NET 10
|
||||
|
||||
typedef struct {
|
||||
int (*open)(int minor, int flags);
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
#include <kernel/net.h>
|
||||
#include <kernel/devfs.h>
|
||||
#include <hashtable.h>
|
||||
#include <mutex.h>
|
||||
#include <string.h>
|
||||
#include <logging.h>
|
||||
|
||||
static hashtable_t nametable;
|
||||
static mutex_t tablelock;
|
||||
|
||||
static devops_t devops = {0};
|
||||
|
||||
int netdev_register(netdev_t *netdev, char *name) {
|
||||
MUTEX_ACQUIRE(&tablelock, false);
|
||||
int e = hashtable_set(&nametable, netdev, name, strlen(name), true);
|
||||
if (e)
|
||||
goto leave;
|
||||
|
||||
|
||||
e = devfs_register(&devops, name, V_TYPE_CHDEV, DEV_MAJOR_NET, 0, 0644);
|
||||
if (e)
|
||||
hashtable_remove(&nametable, name, strlen(name));
|
||||
|
||||
leave:
|
||||
MUTEX_RELEASE(&tablelock);
|
||||
return e;
|
||||
}
|
||||
|
||||
netdev_t *netdev_getdev(char *name) {
|
||||
MUTEX_ACQUIRE(&tablelock, false);
|
||||
netdev_t *netdev = NULL;
|
||||
void *tmp;
|
||||
|
||||
if (hashtable_get(&nametable, &tmp, name, strlen(name)) == 0)
|
||||
netdev = tmp;
|
||||
|
||||
MUTEX_RELEASE(&tablelock);
|
||||
return netdev;
|
||||
}
|
||||
|
||||
void netdev_init() {
|
||||
__assert(hashtable_init(&nametable, 10) == 0);
|
||||
MUTEX_INIT(&tablelock);
|
||||
}
|
||||
@@ -49,7 +49,7 @@ static void rx_dpc(context_t *context, dpcarg_t arg) {
|
||||
int buffidx = VIO_QUEUE_DEV_RING(&netdev->rxqueue)[idx].index;
|
||||
void *ethbufferphys = (void *)(buffers[buffidx].address + sizeof(vioframe_t));
|
||||
eth_process((netdev_t *)netdev, MAKE_HHDM(ethbufferphys));
|
||||
VIO_QUEUE_DRV_RING(&netdev->rxqueue)[VIO_QUEUE_DRV_IDX(&netdev->rxqueue)++] = buffidx;
|
||||
VIO_QUEUE_DRV_RING(&netdev->rxqueue)[VIO_QUEUE_DRV_IDX(&netdev->rxqueue)++ % netdev->rxqueue.size] = buffidx;
|
||||
*netdev->rxqueue.notify = 0;
|
||||
}
|
||||
}
|
||||
@@ -145,6 +145,7 @@ static int vionet_sendpacket(netdev_t *internal, netdesc_t desc, mac_t targetmac
|
||||
netdev->txwait[idx] = &event;
|
||||
|
||||
*netdev->txqueue.notify = 0;
|
||||
|
||||
spinlock_release(&netdev->txlock);
|
||||
|
||||
event_wait(&event, false);
|
||||
@@ -225,15 +226,14 @@ int vionet_newdevice(viodevice_t *viodevice) {
|
||||
|
||||
printf("vionet%d: mac: %02x:%02x:%02x:%02x:%02x:%02x\n", netdev->id, netdev->netdev.mac.address[0], netdev->netdev.mac.address[1], netdev->netdev.mac.address[2], netdev->netdev.mac.address[3], netdev->netdev.mac.address[4], netdev->netdev.mac.address[5]);
|
||||
|
||||
char name[10];
|
||||
snprintf(name, 10, "vionet%d", netdev->id);
|
||||
|
||||
__assert(netdev_register((netdev_t *)netdev, name) == 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void vionet_init() {
|
||||
__assert(hashtable_init(&irqtable, 20) == 0);
|
||||
}
|
||||
|
||||
netdev_t *vionet_temporary_getnetdev() {
|
||||
HASHTABLE_FOREACH(&irqtable) {
|
||||
return entry->value;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user