Voxia OS v0.0.1
Hobby Project Operating System Targeting x86-64
Loading...
Searching...
No Matches
netdev.c
Go to the documentation of this file.
1#include "netdev.h"
2#include "init/init.h"
3#include <hash.h>
4#include "libk/serial.h"
5#include <str.h>
6#include "memory/slab.h"
7#include <init/loader.h>
8
9#define NETDEV_HASHMAP_MAX_ENTRY 512
10
11static struct slab_cache* netdev_cache = 0;
15
17
18static struct netdev_ops* ethernet_ops = 0;
19
20static void bind_nic(netdev_t* netdev, struct ioforge_nic_service* nic);
21
22INIT(Netdev) {
23 vxCreateSlabCache(&netdev_cache, "netdev", sizeof(struct netdev), 0, 0);
24
25 // init ethernet ops
26 ethernet_ops = (struct netdev_ops*) kalloc(sizeof(struct netdev_ops));
27 ethernet_ops->bind_nic = bind_nic;
28}
29
31 auto netdev = (struct netdev*) vxSlabAlloc(netdev_cache);
33
34 // TODO
35 netdev->mtu = 15000;
36
37 netdev->is_up = false;
38 netdev->type = type;
39
40 switch ((int) type) {
43 break;
44
45 default:
46 // type tidak terdaftar
47 return -1;
48 break;
49 }
50
51 // general ops
52
53 // put in hashmap
54 auto hash_name = hash(name, NETDEV_HASHMAP_MAX_ENTRY);
55 netdev->hash = hash_name;
56
57 auto bucket = &netdev_lists[hash_name];
58
59 if (!bucket->buckets) {
60 bucket->buckets = netdev;
61 } else {
62 netdev->next = (void*) bucket->buckets;
63 bucket->buckets = netdev;
64 }
65
66 netdev_lists[hash_name].buckets = netdev;
67 return 0;
68}
69
71 auto hash_name = hash(name, NETDEV_HASHMAP_MAX_ENTRY);
72
73 auto bucket = netdev_lists[hash_name];
74
75 if (!bucket.buckets) {
76 return 0;
77 }
78
79 auto curr = (netdev_t*) bucket.buckets;
80 while (curr) {
81 if (curr->hash == hash_name) {
82 LOG2_DEBUG("NETDEV", "bucket at 0x%x", curr);
83 return curr;
84 }
85
86 __builtin_prefetch(curr->next);
87 curr = (netdev_t*) curr->next;
88 }
89
90 return 0;
91}
92
93// ops implementation
95 netdev->nic = nic;
97 LOG2_INFO("netdev", "netdev mac %x", netdev->mac[0]);
98}
99
101 return __atomic_fetch_add(&dev->ip_id_counter, 1, __ATOMIC_RELAXED);
102}
kstring name
Definition dentry.h:5
uint32_t hash
Definition dentry.h:3
#define INIT(fn)
Definition init.h:26
void * kalloc(size_t size)
#define NETDEV_HASHMAP_MAX_ENTRY
Definition netdev.c:9
int create_netdev(char *name, netdev_type_t type)
Definition netdev.c:30
uint16_t get_next_ip_id(netdev_t *dev)
Definition netdev.c:100
netdev_t * lookup_netdev(char *name)
Definition netdev.c:70
static struct netdev_ops * ethernet_ops
Definition netdev.c:18
static struct netdev_list netdev_lists[512]
Definition netdev.c:16
static struct slab_cache * netdev_cache
Definition netdev.c:11
static void bind_nic(netdev_t *netdev, struct ioforge_nic_service *nic)
Definition netdev.c:94
struct netdev netdev_t
Definition netdev.h:19
netdev_type_t
Definition netdev.h:11
@ NETDEV_TYPE_ETHERNET
Definition netdev.h:12
#define LOG2_DEBUG(mod, fmt,...)
Definition serial.h:35
#define LOG2_INFO(mod, fmt,...)
Definition serial.h:33
void * vxSlabAlloc(struct slab_cache *cache)
Definition slab.c:93
void vxCreateSlabCache(struct slab_cache **cache, const char *name, const size_t obj_size, size_t alignment, const uintptr_t virt_addr)
Definition slab.c:44
size_t strlen(const char *s)
Definition str.c:105
void memcopy(void *dest, void *src, size_t size)
int(* get_mac_address)(uint8_t mac[6])
Definition ioforge_nic.h:24
struct ioforge_nic_operation ops
Definition ioforge_nic.h:60
netdev_t * buckets
Definition netdev.c:13
uint8_t mac[6]
Definition netdev.h:35
uint16_t mtu
Definition netdev.h:36
boolean_t is_up
Definition netdev.h:32
netdev_type_t type
Definition netdev.h:33
struct ioforge_nic_service * nic
Definition netdev.h:38
uint16_t ip_id_counter
Definition netdev.h:41
uint64_t hash
Definition netdev.h:31
struct netdev_ops * ops
Definition netdev.h:39
void * next
Definition netdev.h:43
char name[64]
Definition netdev.h:34
static thread_bucket_t bucket
Definition thread.c:11
unsigned short uint16_t
Definition type.h:13
uint8_t type
Definition vnode.h:2