More work towards getting the appservers combined
authorKevin Klues <klueska@ros-dev.(none)>
Mon, 22 Mar 2010 01:07:18 +0000 (18:07 -0700)
committerKevin Klues <klueska@cs.berkeley.edu>
Thu, 3 Nov 2011 00:35:36 +0000 (17:35 -0700)
kern/arch/i686/frontend.h [new file with mode: 0644]
kern/arch/i686/ne2k.c
kern/arch/i686/nic_common.c
kern/arch/i686/nic_common.h
kern/arch/i686/rl8168.c
kern/include/frontend.h
kern/src/syscall.c

diff --git a/kern/arch/i686/frontend.h b/kern/arch/i686/frontend.h
new file mode 100644 (file)
index 0000000..6a7c37a
--- /dev/null
@@ -0,0 +1,11 @@
+/*
+ * Copyright (c) 2010 The Regents of the University of California
+ * See LICENSE for details.
+ */
+
+#ifndef ROS_ARCH_FRONTEND_H
+#define ROS_ARCH_FRONTEND_H
+
+int handle_appserver_packet(const char *buf, size_t len);
+
+#endif
index 9cf09bf..09aa4ad 100644 (file)
 
 #define SET_PAGE_0() (inb(ne2k_io_base_addr + NE2K_PG0_RW_CR) & 0x3F)
 
-extern uint32_t eth_up; // Fix this                               
-uint32_t ne2k_irq;      // And this
+uint32_t ne2k_irq;      // Fix this
 uint32_t ne2k_io_base_addr;
-char device_mac[6];
-
-extern uint32_t packet_buffer_count;
-extern char* packet_buffer[PACKET_BUFFER_SIZE];
-extern uint32_t packet_buffer_sizes[PACKET_BUFFER_SIZE];
-extern uint32_t packet_buffer_head;
-extern uint32_t packet_buffer_tail;
-spinlock_t packet_buffer_lock;
-
 
 void* base_page;
 uint32_t num_pages = 0;
 
-extern char *CT(PACKET_HEADER_SIZE + len) (*packet_wrap)(const char *CT(len) data, size_t len);
-extern int (*send_frame)(const char *CT(len) data, size_t len);
-
-
 void ne2k_init() {
        
        if (ne2k_scan_pci() < 0) return;
@@ -102,7 +88,7 @@ void ne2k_init() {
        //ne2k_test_interrupts();
        send_frame = &ne2k_send_frame;
 
-        ne2k_setup_interrupts();
+       ne2k_setup_interrupts();
 
        eth_up = 1;
 
@@ -396,22 +382,32 @@ void ne2k_handle_rx_packet() {
                return;
        }
 
-       spin_lock(&packet_buffer_lock);
+       // Treat as a syscall frontend response packet if eth_type says so
+       // Will eventually go away, so not too worried about elegance here...
+       #include <frontend.h>
+       #include <arch/frontend.h>
+       if((uint16_t)(rx_buffer[13]) == APPSERVER_ETH_TYPE) {
+               handle_appserver_packet(rx_buffer, packet_len);
+               kfree(rx_buffer);
+               return;
+       }
+
+       spin_lock(&packet_buffers_lock);
 
-       if (packet_buffer_count >= PACKET_BUFFER_SIZE) {
+       if (num_packet_buffers >= MAX_PACKET_BUFFERS) {
                printk("WARNING: DROPPING PACKET!\n");
-               spin_unlock(&packet_buffer_lock);
+               spin_unlock(&packet_buffers_lock);
                kfree(rx_buffer);
                return;
        }
 
-       packet_buffer[packet_buffer_tail] = rx_buffer;
-       packet_buffer_sizes[packet_buffer_tail] = packet_len;
+       packet_buffers[packet_buffers_tail] = rx_buffer;
+       packet_buffers_sizes[packet_buffers_tail] = packet_len;
 
-       packet_buffer_tail = (packet_buffer_tail + 1) % PACKET_BUFFER_SIZE;
-       packet_buffer_count = packet_buffer_count + 1;
+       packet_buffers_tail = (packet_buffers_tail + 1) % MAX_PACKET_BUFFERS;
+       num_packet_buffers++;
 
-       spin_unlock(&packet_buffer_lock);
+       spin_unlock(&packet_buffers_lock);
        
        return;
 }
index 6fa5433..ea8d2c4 100644 (file)
 #endif
 
 #include <arch/nic_common.h>
+#include <kmalloc.h>
+#include <stdio.h>
 
-uint8_t eth_up = 0; 
-
-// Hacky stuff for syscall hack. Go away.
-uint32_t packet_buffer_count = 0;
-char* packet_buffer[PACKET_BUFFER_SIZE];
-uint32_t packet_buffer_sizes[PACKET_BUFFER_SIZE];
-uint32_t packet_buffer_head = 0;
-uint32_t packet_buffer_tail = 0;
-spinlock_t packet_buffer_lock = SPINLOCK_INITIALIZER;
+// Global send_frame function pointer
+// Means we can only have one network card per system right now...
+int (*send_frame)(const char *data, size_t len);
 
+// Global variables for managing ethernet packets over a nic
+// Again, since these are global for all network cards we are 
+// limited to only one for now
+char device_mac[6];
+uint8_t eth_up = 0; 
+uint32_t num_packet_buffers = 0;
+char* packet_buffers[MAX_PACKET_BUFFERS];
+uint32_t packet_buffers_sizes[MAX_PACKET_BUFFERS];
+uint32_t packet_buffers_head = 0;
+uint32_t packet_buffers_tail = 0;
+spinlock_t packet_buffers_lock = SPINLOCK_INITIALIZER;
 
-char* (*packet_wrap)(const char *CT(len) data, size_t len);
-int (*send_frame)(const char *CT(len) data, size_t len);
index 79e7cdd..23e9b8d 100644 (file)
@@ -5,67 +5,58 @@
 #include <trap.h>
 #include <pmap.h>
 
+// Packet sizes
+#define MTU              1500
+#define MAX_FRAME_SIZE   (MTU + 14)
+#define MIN_FRAME_SIZE   60 // See the spec...
+
+// Maximum packet buffers we can handle at any given time
+#define MAX_PACKET_BUFFERS    1024
+// Global send_frame function pointer
+// Means we can only have one network card per system right now...
+extern int (*send_frame)(const char *data, size_t len);
+
+// Global variables for managing ethernet packets over a nic
+// Again, since these are global for all network cards we are 
+// limited to only one for now
+extern char device_mac[6];
+extern uint8_t eth_up;
+extern uint32_t num_packet_buffers;
+extern char* packet_buffers[MAX_PACKET_BUFFERS];
+extern uint32_t packet_buffers_sizes[MAX_PACKET_BUFFERS];
+extern uint32_t packet_buffers_head;
+extern uint32_t packet_buffers_tail;
+extern spinlock_t packet_buffers_lock; 
+
+// Host to network format conversions and vice-versa
+static inline uint16_t htons(uint16_t x)
+{
+       __asm__ ("xchgb %%al,%%ah" : "=a" (x) : "a" (x));
+       return x;
+}
 
-// Basic packet sizing
-// TODO handle jumbo packets
-#define ETHERNET_ENCAP_SIZE    18
-#define MTU                    1500    
-#define MAX_FRAME_SIZE         ETHERNET_ENCAP_SIZE + MTU       
-       
-// This is to make it simply compile when not in __NETWORK__ mode.
-#ifndef USER_MAC_ADDRESS
-#define USER_MAC_ADDRESS {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
-#endif
-
-// v----- Evil line ------v
-// Hacky stuff for syscalls go away.
+static inline uint32_t htonl(uint32_t x)
+{
+       __asm__ ("bswapl %0" : "=r" (x) : "r" (x));
+       return x;
+}
+#define ntohs htons
+#define ntohl htonl
 
-#define PACKET_BUFFER_SIZE 1024
+// Creates a new ethernet packet and puts the header on it
+char* eth_wrap(const char* data, size_t len, char src_mac[6], 
+               char dest_mac[6], uint16_t eth_type);
 
-struct ETH_Header
-{
+struct ETH_Header {
        char dest_mac[6];
        char source_mac[6];
        uint16_t eth_type;
 };
 
-
-struct IP_Header
-{
-       uint32_t ip_opts0;
-       uint32_t ip_opts1;
-       uint32_t ip_opts2;
-       uint32_t source_ip;
-       uint32_t dest_ip;
-};
-
-struct UDP_Header
-{
-       uint16_t source_port;
-       uint16_t dest_port;
-       uint16_t length;
-       uint16_t checksum;
-};     
-
-#define MINIMUM_PACKET_SIZE 14 // kinda evil. probably evil.
-#define MAX_PACKET_SIZE                MTU
-
-#define PACKET_HEADER_SIZE  sizeof(struct packet_header) //IP UDP ETH
-#define MAX_PACKET_DATA                MAX_FRAME_SIZE - PACKET_HEADER_SIZE
-// This number needs verification! Also, this is a huge hack, as the driver shouldnt care about UDP/IP etc.
-
-struct packet_header {
+struct eth_frame {
        struct ETH_Header eth_head;
-       struct IP_Header ip_head;
-       struct UDP_Header udp_head;
+       char data[MTU];
 } __attribute__((packed));
 
-struct eth_packet {
-       struct packet_header eth_head;
-       char data[MTU-PACKET_HEADER_SIZE];
-} __attribute__((packed));
-
-
-// ^----- Evil line ------^
-
 #endif /* !ROS_INC_NIC_COMMON_H */
index 5c75b69..d778c0d 100644 (file)
@@ -83,7 +83,6 @@ struct Descriptor
 
 uint32_t rl8168_io_base_addr = 0;
 uint32_t rl8168_irq = 0;
-char device_mac[6];
 
 struct Descriptor *CT(NUM_RX_DESCRIPTORS) rx_des_kva;
 unsigned long rx_des_pa;
@@ -94,16 +93,6 @@ unsigned long tx_des_pa;
 uint32_t rx_des_cur = 0;
 uint32_t tx_des_cur = 0;
 
-extern int eth_up;
-extern uint32_t packet_buffer_count;
-extern char* packet_buffer[PACKET_BUFFER_SIZE];
-extern uint32_t packet_buffer_sizes[PACKET_BUFFER_SIZE];
-extern uint32_t packet_buffer_head;
-extern uint32_t packet_buffer_tail;
-spinlock_t packet_buffer_lock;
-
-extern char *CT(PACKET_HEADER_SIZE + len) (*packet_wrap)(const char *CT(len) data, size_t len);
-extern int (*send_frame)(const char *CT(len) data, size_t len);
 
 
 void rl8168_init() {
@@ -519,22 +508,22 @@ void rl8168_handle_rx_packet() {
        } while (!(current_command & DES_LS_MASK));
        
 
-       spin_lock(&packet_buffer_lock);
+       spin_lock(&packet_buffers_lock);
 
-       if (packet_buffer_count >= PACKET_BUFFER_SIZE) {
+       if (num_packet_buffers >= MAX_PACKET_BUFFERS) {
                printk("WARNING: DROPPING PACKET!\n");
-               spin_unlock(&packet_buffer_lock);
+               spin_unlock(&packet_buffers_lock);
                kfree(rx_buffer);
                return;
        }
 
-       packet_buffer[packet_buffer_tail] = rx_buffer;
-       packet_buffer_sizes[packet_buffer_tail] = frame_size;
+       packet_buffers[packet_buffers_tail] = rx_buffer;
+       packet_buffers_sizes[packet_buffers_tail] = frame_size;
                
-       packet_buffer_tail = (packet_buffer_tail + 1) % PACKET_BUFFER_SIZE;
-       packet_buffer_count = packet_buffer_count + 1;
+       packet_buffers_tail = (packet_buffers_tail + 1) % MAX_PACKET_BUFFERS;
+       num_packet_buffers++;
 
-       spin_unlock(&packet_buffer_lock);
+       spin_unlock(&packet_buffers_lock);
                                
        rx_des_cur = rx_des_loop_cur;
                                
index 17db5cc..16761b1 100644 (file)
@@ -8,6 +8,11 @@
 #include <env.h>
 #include <process.h>
 
+// Default APPSERVER_ETH_TYPE if not defined externally
+#ifndef APPSERVER_ETH_TYPE
+#define APPSERVER_ETH_TYPE 0x8888
+#endif
+
 int32_t frontend_syscall_from_user(env_t* p, int32_t syscall_num, 
                                    uint32_t arg0, uint32_t arg1, 
                                    uint32_t arg2, uint32_t translate_args);
index 31e8e1b..4551b1e 100644 (file)
@@ -536,34 +536,25 @@ static ssize_t sys_serial_write(env_t* e, const char *DANGEROUS buf, size_t len)
 // This is not a syscall we want. Its hacky. Here just for syscall stuff until get a stack.
 static ssize_t sys_eth_read(env_t* e, char *DANGEROUS buf)
 {
-       extern int eth_up;
-
-        extern uint32_t packet_buffer_count;
-        extern char* packet_buffer[PACKET_BUFFER_SIZE];
-        extern uint32_t packet_buffer_sizes[PACKET_BUFFER_SIZE];
-        extern uint32_t packet_buffer_head;
-        extern uint32_t packet_buffer_tail;
-        extern spinlock_t packet_buffer_lock;
-
        if (eth_up) {
 
                uint32_t len;
                char *ptr;
 
-               spin_lock(&packet_buffer_lock);
+               spin_lock(&packet_buffers_lock);
 
-               if (packet_buffer_count == 0) {
-                       spin_unlock(&packet_buffer_lock);
+               if (num_packet_buffers == 0) {
+                       spin_unlock(&packet_buffers_lock);
                        return 0;
                }
 
-               ptr = packet_buffer[packet_buffer_head];
-               len = packet_buffer_sizes[packet_buffer_head];
+               ptr = packet_buffers[packet_buffers_head];
+               len = packet_buffers_sizes[packet_buffers_head];
 
-               packet_buffer_count = packet_buffer_count - 1;
-               packet_buffer_head = (packet_buffer_head + 1) % PACKET_BUFFER_SIZE;
+               num_packet_buffers--;
+               packet_buffers_head = (packet_buffers_head + 1) % MAX_PACKET_BUFFERS;
 
-               spin_unlock(&packet_buffer_lock);
+               spin_unlock(&packet_buffers_lock);
 
                char* _buf = user_mem_assert(e, buf, len, PTE_U);
 
@@ -580,15 +571,13 @@ static ssize_t sys_eth_read(env_t* e, char *DANGEROUS buf)
 // This is not a syscall we want. Its hacky. Here just for syscall stuff until get a stack.
 static ssize_t sys_eth_write(env_t* e, const char *DANGEROUS buf, size_t len)
 {
-       extern int eth_up;
-
        if (eth_up) {
 
                if (len == 0)
                        return 0;
 
                // HACK TO BYPASS HACK
-               int just_sent = send_frame( buf, len);
+               int just_sent = send_frame(buf, len);
 
                if (just_sent < 0) {
                        printk("Packet send fail\n");
@@ -625,12 +614,9 @@ static ssize_t sys_eth_write(env_t* e, const char *DANGEROUS buf, size_t len)
                return -EINVAL;
 }
 
-static ssize_t sys_eth_get_mac_addr(env_t* e, char *DANGEROUS buf) {
-       
-       extern int eth_up;
-
+static ssize_t sys_eth_get_mac_addr(env_t* e, char *DANGEROUS buf) 
+{
        if (eth_up) {
-               extern char device_mac[];
                for (int i = 0; i < 6; i++)
                        buf[i] = device_mac[i];
                return 0;
@@ -639,13 +625,10 @@ static ssize_t sys_eth_get_mac_addr(env_t* e, char *DANGEROUS buf) {
                return -EINVAL;
 }
 
-static int sys_eth_recv_check(env_t* e) {
-
-       extern uint32_t packet_buffer_count;
-       
-       if (packet_buffer_count != 0) {
+static int sys_eth_recv_check(env_t* e) 
+{
+       if (num_packet_buffers != 0) 
                return 1;
-       }
        else
                return 0;
 }