From ac7c4cd2c551d3d9862fb8c1f719aca03eb9fd4e Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Fri, 18 Jun 2021 21:42:35 +0900 Subject: [PATCH] net: don't dynamically allocate hashmaps and lists? --- kernel/net/arp.c | 2 -- kernel/net/ipv4.c | 19 +++++++++---------- kernel/net/netif.c | 5 +++++ 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/kernel/net/arp.c b/kernel/net/arp.c index 258d47e5..aa3494cf 100644 --- a/kernel/net/arp.c +++ b/kernel/net/arp.c @@ -44,7 +44,6 @@ hashmap_t * net_arp_cache = NULL; void net_arp_cache_add(struct EthernetDevice * iface, uint32_t addr, uint8_t * hwaddr, uint16_t flags) { spin_lock(net_arp_cache_lock); - if (!net_arp_cache) net_arp_cache = hashmap_create_int(10); struct ArpCacheEntry * entry = hashmap_get(net_arp_cache, (void*)(uintptr_t)addr); if (!entry) entry = malloc(sizeof(struct ArpCacheEntry)); memcpy(entry->hwaddr, hwaddr, 6); @@ -55,7 +54,6 @@ void net_arp_cache_add(struct EthernetDevice * iface, uint32_t addr, uint8_t * h } struct ArpCacheEntry * net_arp_cache_get(uint32_t addr) { - if (!net_arp_cache) return NULL; spin_lock(net_arp_cache_lock); struct ArpCacheEntry * out = hashmap_get(net_arp_cache, (void*)(uintptr_t)addr); spin_unlock(net_arp_cache_lock); diff --git a/kernel/net/ipv4.c b/kernel/net/ipv4.c index 7694788c..3ceee00f 100644 --- a/kernel/net/ipv4.c +++ b/kernel/net/ipv4.c @@ -170,6 +170,11 @@ static void icmp_handle(struct ipv4_packet * packet, const char * src, const cha static hashmap_t * udp_sockets = NULL; static hashmap_t * tcp_sockets = NULL; +void ipv4_install(void) { + udp_sockets = hashmap_create_int(10); + tcp_sockets = hashmap_create_int(10); +} + #define TCP_FLAGS_FIN (1 << 0) #define TCP_FLAGS_SYN (1 << 1) #define TCP_FLAGS_RES (1 << 2) @@ -288,7 +293,7 @@ void net_ipv4_handle(struct ipv4_packet * packet, fs_node_t * nic) { case IPV4_PROT_UDP: { uint16_t dest_port = ntohs(((uint16_t*)&packet->payload)[1]); printf("net: ipv4: %s: %s -> %s udp %d to %d\n", nic->name, src, dest, ntohs(((uint16_t*)&packet->payload)[0]), dest_port); - if (udp_sockets && hashmap_has(udp_sockets, (void*)(uintptr_t)dest_port)) { + if (hashmap_has(udp_sockets, (void*)(uintptr_t)dest_port)) { printf("net: udp: received and have a waiting endpoint!\n"); sock_t * sock = hashmap_get(udp_sockets, (void*)(uintptr_t)dest_port); net_sock_add(sock, packet, ntohs(packet->length)); @@ -298,7 +303,7 @@ void net_ipv4_handle(struct ipv4_packet * packet, fs_node_t * nic) { case IPV4_PROT_TCP: { uint16_t dest_port = ntohs(((uint16_t*)&packet->payload)[1]); printf("net: ipv4: %s: %s -> %s tcp %d to %d\n", nic->name, src, dest, ntohs(((uint16_t*)&packet->payload)[0]), dest_port); - if (tcp_sockets && hashmap_has(tcp_sockets, (void*)(uintptr_t)dest_port)) { + if (hashmap_has(tcp_sockets, (void*)(uintptr_t)dest_port)) { printf("net: tcp: received and have a waiting endpoint!\n"); /* What kind of packet is this? Is it something we were expecting? */ sock_t * sock = hashmap_get(tcp_sockets, (void*)(uintptr_t)dest_port); @@ -339,9 +344,6 @@ static int next_port = 12345; static int udp_get_port(sock_t * sock) { spin_lock(udp_port_lock); int out = next_port++; - if (!udp_sockets) { - udp_sockets = hashmap_create_int(10); - } hashmap_set(udp_sockets, (void*)(uintptr_t)out, sock); sock->priv[0] = out; spin_unlock(udp_port_lock); @@ -429,7 +431,7 @@ static long sock_udp_recv(sock_t * sock, struct msghdr * msg, int flags) { } static void sock_udp_close(sock_t * sock) { - if (sock->priv[0] && udp_sockets) { + if (sock->priv[0]) { printf("udp: removing port %d from bound map\n", sock->priv[0]); spin_lock(udp_port_lock); hashmap_remove(udp_sockets, (void*)(uintptr_t)sock->priv[0]); @@ -448,7 +450,7 @@ static int udp_socket(void) { static spin_lock_t tcp_port_lock = {0}; static void sock_tcp_close(sock_t * sock) { - if (sock->priv[0] && tcp_sockets) { + if (sock->priv[0]) { printf("tcp: removing port %d from bound map\n", sock->priv[0]); spin_lock(tcp_port_lock); hashmap_remove(tcp_sockets, (void*)(uintptr_t)sock->priv[0]); @@ -501,9 +503,6 @@ static int next_tcp_port = 49152; static int tcp_get_port(sock_t * sock) { spin_lock(tcp_port_lock); int out = next_tcp_port++; - if (!tcp_sockets) { - tcp_sockets = hashmap_create_int(10); - } hashmap_set(tcp_sockets, (void*)(uintptr_t)out, sock); sock->priv[0] = out; spin_unlock(tcp_port_lock); diff --git a/kernel/net/netif.c b/kernel/net/netif.c index 41e854a2..c1e872a0 100644 --- a/kernel/net/netif.c +++ b/kernel/net/netif.c @@ -23,11 +23,16 @@ static hashmap_t * interfaces = NULL; extern list_t * net_raw_sockets_list; static fs_node_t * _if_first = NULL; +extern void ipv4_install(void); +extern hashmap_t * net_arp_cache; + void net_install(void) { /* Set up virtual devices */ map_vfs_directory("/dev/net"); interfaces = hashmap_create(10); net_raw_sockets_list = list_create("raw sockets", NULL); + net_arp_cache = hashmap_create_int(10); + ipv4_install(); } /* kinda temporary for now */