From: Lei Xia Date: Tue, 16 Feb 2010 18:02:36 +0000 (-0600) Subject: Add initial code for multicore support VNET X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?p=palacios.git;a=commitdiff_plain;h=262c92c9f620f87500f350b4744d2f31b6de7da5 Add initial code for multicore support VNET --- diff --git a/modules/linux_mods/i386/binaries/test.ko b/modules/linux_mods/i386/binaries/test.ko deleted file mode 100644 index e5af2d2..0000000 Binary files a/modules/linux_mods/i386/binaries/test.ko and /dev/null differ diff --git a/palacios/include/devices/lnx_virtio_pci.h b/palacios/include/devices/lnx_virtio_pci.h index 9f7d286..c0f2295 100644 --- a/palacios/include/devices/lnx_virtio_pci.h +++ b/palacios/include/devices/lnx_virtio_pci.h @@ -136,6 +136,8 @@ struct virtio_queue { addr_t ring_avail_addr; addr_t ring_used_addr; + uint16_t last_avail_idx; + int idx_overflow; struct vring_desc * desc; // We can treat this as an array... struct vring_avail * avail; diff --git a/palacios/include/palacios/vm_guest.h b/palacios/include/palacios/vm_guest.h index 4498705..f918da6 100644 --- a/palacios/include/palacios/vm_guest.h +++ b/palacios/include/palacios/vm_guest.h @@ -34,7 +34,7 @@ #include #include #include - +#include #ifdef CONFIG_TELEMETRY #include diff --git a/palacios/include/palacios/vmm_vnet.h b/palacios/include/palacios/vmm_vnet.h index c94c6c6..1a88c24 100644 --- a/palacios/include/palacios/vmm_vnet.h +++ b/palacios/include/palacios/vmm_vnet.h @@ -9,8 +9,6 @@ * * Copyright (c) 2009, Lei Xia * Copyright (c) 2009, Yuan Tang - * Copyright (c) 2009, Jack Lange - * Copyright (c) 2009, Peter Dinda * All rights reserved. * @@ -31,115 +29,54 @@ #include #include -#define ETHERNET_HEADER_LEN 14 -#define ETHERNET_DATA_MIN 46 -#define ETHERNET_DATA_MAX 1500 -#define ETHERNET_PACKET_LEN (ETHERNET_HEADER_LEN + ETHERNET_DATA_MAX) - -typedef enum {MAC_ANY, MAC_NOT, MAC_NONE, MAC_EMPTY} mac_type_t; //for 'src_mac_qual' and 'dst_mac_qual' +typedef enum {MAC_ANY, MAC_NOT, MAC_NONE} mac_type_t; //for 'src_mac_qual' and 'dst_mac_qual' typedef enum {LINK_INTERFACE, LINK_EDGE, LINK_ANY} link_type_t; //for 'type' and 'src_type' in struct routing -typedef enum {TCP_TYPE, UDP_TYPE, NONE_TYPE} prot_type_t; //routing table entry -struct routing_entry{ +struct v3_vnet_route { char src_mac[6]; char dest_mac[6]; - int src_mac_qual; - int dest_mac_qual; + mac_type_t src_mac_qual; + mac_type_t dest_mac_qual; int link_idx; //link[dest] is the link to be used to send pkt link_type_t link_type; //EDGE|INTERFACE|ANY int src_link_idx; link_type_t src_type; //EDGE|INTERFACE|ANY -}__attribute__((packed)); - - -struct eth_header { - uchar_t dest[6]; - uchar_t src[6]; - uint16_t type; // indicates layer 3 protocol type -}__attribute__((packed)); - -struct ip_header { - uint8_t version: 4; - uint8_t hdr_len: 4; - uchar_t tos; - uint16_t total_len; - uint16_t id; - uint8_t flags: 3; - uint16_t offset: 13; - uchar_t ttl; - uchar_t proto; - uint16_t cksum; - uint32_t src_addr; - uint32_t dst_addr; -}__attribute__((packed)); - -struct udp_header { - uint16_t src_port; - uint16_t dst_port; - uint16_t len; - uint16_t csum;//set to zero, disable the xsum -}__attribute__((packed)); - -struct udp_link_header { - struct eth_header eth_hdr; - struct ip_header ip_hdr; - struct udp_header udp_hdr; -}__attribute__((packed)); - -#define DEVICE_NAME_LEN 20 -struct vnet_if_device { - char name[DEVICE_NAME_LEN]; - uchar_t mac_addr[6]; - struct vm_device *dev; - - int (*input)(uchar_t *data, uint32_t len, void *private_data); - - void *private_data; -}__attribute__((packed)); - - -struct vnet_if_link { - prot_type_t pro_type; //protocal type of this link - unsigned long dest_ip; - uint16_t dest_port; - - struct udp_link_header vnet_header; //header applied to the packet in/out from this link - - int (*input)(uchar_t *data, uint32_t len, void *private_data); - - void *private_data; -}__attribute__((packed)); - - -//link table entry -struct link_entry { - link_type_t type; - - union { - struct vnet_if_device *dst_dev; - struct vnet_if_link *dst_link; - } __attribute__((packed)); - - int use; -}__attribute__((packed)); +}; int v3_vnet_send_rawpkt(uchar_t *buf, int len, void *private_data); int v3_vnet_send_udppkt(uchar_t *buf, int len, void *private_data); -//int vnet_register_device(struct vm_device *vdev, -// char *dev_name, -// uchar_t mac[6], -// int (*netif_input)(uchar_t * pkt, uint_t size, void *private_data), -// void *data); -//int vnet_unregister_device(char *dev_name); -int v3_vnet_pkt_process(); +int v3_vnet_add_route(struct v3_vnet_route *route); + +//int v3_vnet_del_route(); +//int v3_vnet_get_routes(); + + +//int v3_vnet_add_link(struct v3_vnet_link link); + +// int v3_vnet_del_link(); +//int v3_vnet_get_link(int idx, struct vnet_link * link); + + +int v3_init_vnet(); + +//int v3_vnet_add_bridge(struct v3_vm_info * vm, uint8_t mac[6]); + +int v3_vnet_add_node(struct v3_vm_info *info, + char * dev_name, + uchar_t mac[6], + int (*netif_input)(struct v3_vm_info * vm, uchar_t * pkt, uint_t size, void * private_data), + void * priv_data); + + +// temporary hack +int v3_vnet_pkt_process(); -void v3_vnet_init(struct guest_info *vm); #endif diff --git a/palacios/src/devices/Kconfig b/palacios/src/devices/Kconfig index 55b6b1e..0c6f07b 100644 --- a/palacios/src/devices/Kconfig +++ b/palacios/src/devices/Kconfig @@ -147,7 +147,7 @@ config DEBUG_VIRTIO_SYM config LINUX_VIRTIO_NET bool "Enable Virtio Network Device" default n - depends on PCI + depends on PCI && EXPERIMENTAL help Enable the Virtio Net @@ -158,6 +158,20 @@ config DEBUG_VIRTIO_NET help Enable debugging for the Linux Virtio Network Device +config VNET_NIC + bool "Enable VNET NIC Device" + default n + depends on PCI && EXPERIMENTAL + help + Enable the VNET NIC backend device + +config VNET_NIC_NET + bool "VNET NIC Device Debugging" + default n + depends on VNET_NIC && DEBUG_ON + help + Enable debugging for the VNET NIC Device + config NE2K bool "NE2K" diff --git a/palacios/src/devices/Makefile b/palacios/src/devices/Makefile index 50fea64..0a1fcf0 100644 --- a/palacios/src/devices/Makefile +++ b/palacios/src/devices/Makefile @@ -13,6 +13,7 @@ obj-$(CONFIG_LINUX_VIRTIO_BALLOON) += lnx_virtio_balloon.o obj-$(CONFIG_LINUX_VIRTIO_BLOCK) += lnx_virtio_blk.o obj-$(CONFIG_LINUX_VIRTIO_SYM) += lnx_virtio_sym.o obj-$(CONFIG_LINUX_VIRTIO_NET) += lnx_virtio_nic.o +obj-$(CONFIG_VNET_NIC) += vnet_nic.o obj-$(CONFIG_NVRAM) += nvram.o obj-$(CONFIG_OS_DEBUG) += os_debug.o obj-$(CONFIG_PCI) += pci.o diff --git a/palacios/src/devices/lnx_virtio_nic.c b/palacios/src/devices/lnx_virtio_nic.c index a21bdb8..58e9e58 100644 --- a/palacios/src/devices/lnx_virtio_nic.c +++ b/palacios/src/devices/lnx_virtio_nic.c @@ -7,13 +7,13 @@ * and the University of New Mexico. You can find out more at * http://www.v3vee.org * - * Copyright (c) 2008, Jack Lange * Copyright (c) 2008, Lei Xia + * Copyright (c) 2008, Cui Zheng * Copyright (c) 2008, The V3VEE Project * All rights reserved. * - * Author: Jack Lange - * Lei Xia + * Author: Lei Xia + * Cui Zheng * * * This is free software. You are permitted to use, @@ -25,6 +25,8 @@ #include #include #include +#include +#include #include @@ -34,28 +36,9 @@ #define PrintDebug(fmt, args...) #endif -/* The feature bitmap for virtio net */ -#define VIRTIO_NET_F_CSUM 0 /* Host handles pkts w/ partial csum */ -#define VIRTIO_NET_F_GUEST_CSUM 1 /* Guest handles pkts w/ partial csum */ -#define VIRTIO_NET_F_MAC 5 /* Host has given MAC address. */ -#define VIRTIO_NET_F_GSO 6 /* Host handles pkts w/ any GSO type */ -#define VIRTIO_NET_F_GUEST_TSO4 7 /* Guest can handle TSOv4 in. */ -#define VIRTIO_NET_F_GUEST_TSO6 8 /* Guest can handle TSOv6 in. */ -#define VIRTIO_NET_F_GUEST_ECN 9 /* Guest can handle TSO[6] w/ ECN in. */ -#define VIRTIO_NET_F_GUEST_UFO 10 /* Guest can handle UFO in. */ -#define VIRTIO_NET_F_HOST_TSO4 11 /* Host can handle TSOv4 in. */ -#define VIRTIO_NET_F_HOST_TSO6 12 /* Host can handle TSOv6 in. */ -#define VIRTIO_NET_F_HOST_ECN 13 /* Host can handle TSO[6] w/ ECN in. */ -#define VIRTIO_NET_F_HOST_UFO 14 /* Host can handle UFO in. */ -#define VIRTIO_NET_F_MRG_RXBUF 15 /* Host can merge receive buffers. */ -#define VIRTIO_NET_F_STATUS 16 /* virtio_net_config.status available */ -#define VIRTIO_NET_F_CTRL_VQ 17 /* Control channel available */ -#define VIRTIO_NET_F_CTRL_RX 18 /* Control channel RX mode support */ -#define VIRTIO_NET_F_CTRL_VLAN 19 /* Control channel VLAN filtering */ -#define VIRTIO_NET_F_CTRL_RX_EXTRA 20 /* Extra RX mode control support */ -#define VIRTIO_NET_S_LINK_UP 1 /* Link is up */ +#define VIRTIO_NIC_PROFILE -/* Maximum packet size we can receive from tap device: header + 64k */ +#define VIRTIO_NET_S_LINK_UP 1 /* Link is up */ #define VIRTIO_NET_MAX_BUFSIZE (sizeof(struct virtio_net_hdr) + (64 << 10)) struct virtio_net_hdr { @@ -63,7 +46,6 @@ struct virtio_net_hdr { #define VIRTIO_NET_HDR_GSO_NONE 0 /* Not a GSO frame */ uint8_t gso_type; - uint16_t hdr_len; /* Ethernet + IP + tcp/udp hdrs */ uint16_t gso_size; /* Bytes to append to hdr_len per frame */ uint16_t csum_start; /* Position to start checksumming from */ @@ -71,7 +53,7 @@ struct virtio_net_hdr { }__attribute__((packed)); -#define QUEUE_SIZE 256 +#define QUEUE_SIZE 1024 #define CTRL_QUEUE_SIZE 64 #define ETH_ALEN 6 @@ -91,6 +73,7 @@ struct virtio_net_state { struct virtio_net_config net_cfg; struct virtio_config virtio_cfg; + struct vm_device * dev; struct pci_device * pci_dev; int io_range_size; @@ -98,6 +81,9 @@ struct virtio_net_state { struct virtio_queue tx_vq; //index 1, svq in Linux virtio driver, handle packet from guest struct virtio_queue ctrl_vq; //index 2, ctrol info from guest + ulong_t pkt_sent, pkt_recv, pkt_drop; + struct gen_queue * inpkt_q; + struct v3_dev_net_ops * net_ops; void * backend_data; @@ -105,42 +91,11 @@ struct virtio_net_state { struct list_head dev_link; }; -#if 1 -//Temporarly for debug -static void print_packet(uchar_t *pkt, int size) { - PrintDebug("Virtio Nic: print_data_packet: size: %d\n", size); - v3_hexdump(pkt, size, NULL, 0); -} - -static struct virtio_net_state *temp_net_states[3]; -static int net_idx = 0; - -static int __virtio_dev_send(uchar_t * buf, uint32_t size, void *private_data); - -static int send(uint8_t * buf, uint32_t count, void * private_data, struct vm_device *dest_dev) -{ - PrintDebug("Virito NIC: In sending stub, guest %p\n", private_data); - print_packet(buf, count); - struct virtio_net_state *virtio_state = (struct virtio_net_state *)private_data; - - if (virtio_state == temp_net_states[0]) - __virtio_dev_send(buf, count, temp_net_states[1]); - - if (virtio_state == temp_net_states[1]){ //return a RARP packet - __virtio_dev_send(buf, count, temp_net_states[0]); - } - - return count; -} - -static int receive(uint8_t * buf, uint32_t count, void * private_data, struct vm_device *src_dev) -{ - PrintDebug("Virito NIC: In receiveing stub\n"); - - return 0; -} - -#endif +#define ETHERNET_PACKET_LEN 1514 +struct eth_pkt { + uint32_t size; //size of data + char data[ETHERNET_PACKET_LEN]; +}__attribute__((packed)); static int virtio_free(struct vm_device * dev) { @@ -148,7 +103,7 @@ static int virtio_free(struct vm_device * dev) return -1; } -static int virtio_reset(struct virtio_net_state * virtio) +static int virtio_init_state(struct virtio_net_state * virtio) { virtio->rx_vq.ring_desc_addr = 0; virtio->rx_vq.ring_avail_addr = 0; @@ -172,14 +127,16 @@ static int virtio_reset(struct virtio_net_state * virtio) //virtio->virtio_cfg.status = VIRTIO_NET_S_LINK_UP; virtio->virtio_cfg.pci_isr = 0; + virtio->pkt_sent = virtio->pkt_recv = virtio->pkt_drop = 0; + + virtio->inpkt_q = v3_create_queue(); + v3_init_queue(virtio->inpkt_q); + return 0; } - -//sending guest's packet to network sink -static int pkt_write(struct virtio_net_state * virtio, struct vring_desc * buf_desc) +static int pkt_tx(struct virtio_net_state * virtio, struct vring_desc * buf_desc) { - //struct virtio_net_state * virtio = (struct virtio_net_state *)dev->private_data; uint8_t * buf = NULL; uint32_t len = buf_desc->length; @@ -190,10 +147,6 @@ static int pkt_write(struct virtio_net_state * virtio, struct vring_desc * buf_d return -1; } - PrintDebug("Length=%d\n", buf_desc->length); - PrintDebug("Buffer Descriptor (ptr=%p) gpa=%p, len=%d, flags=%x, next=%d, buf address: %p, send address: %p\n", buf_desc, - (void *)(buf_desc->addr_gpa), buf_desc->length, buf_desc->flags, buf_desc->next, buf, virtio->net_ops->send); - if (virtio->net_ops->send(buf, len, (void *)virtio, NULL) == -1) { return -1; } @@ -201,7 +154,6 @@ static int pkt_write(struct virtio_net_state * virtio, struct vring_desc * buf_d return 0; } - static int build_receive_header(struct virtio_net_hdr * hdr, const void * buf, int raw) { hdr->flags = 0; @@ -214,8 +166,6 @@ static int build_receive_header(struct virtio_net_hdr * hdr, const void * buf, i return 0; } - -//sending guest's packet to network sink static int copy_data_to_desc(struct virtio_net_state * virtio_state, struct vring_desc * desc, uchar_t * buf, uint_t buf_len) { uint32_t len; @@ -225,104 +175,83 @@ static int copy_data_to_desc(struct virtio_net_state * virtio_state, struct vrin PrintError("Could not translate buffer address\n"); return -1; } - len = (desc->length < buf_len)?desc->length:buf_len; - memcpy(desc_buf, buf, len); - PrintDebug("Length=%d\n", len); - return len; } - //send data to guest -static int send_pkt_to_guest(struct virtio_net_state * virtio, uchar_t * buf, uint_t size, int raw, void * private_data) +int send_pkt_to_guest(struct virtio_net_state * virtio, uchar_t * buf, uint_t size, int raw, void * private_data) { struct virtio_queue * q = &(virtio->rx_vq); struct virtio_net_hdr hdr; uint32_t hdr_len = sizeof(struct virtio_net_hdr); uint32_t data_len = size; uint32_t offset = 0; + + PrintDebug("VIRTIO NIC: sending packet to virtio nic %p, size:%d", virtio, size); - PrintDebug("VIRTIO NIC: Handle RX: cur_index=%d (mod=%d), avail_index=%d\n", - q->cur_avail_idx, q->cur_avail_idx % q->queue_size, q->avail->index); - - PrintDebug("VIRTIO NIC: sending packet to net_state %p, size:%d", virtio, size); - + virtio->pkt_recv ++; if (!raw) { data_len -= hdr_len; } build_receive_header(&hdr, buf, 1); - //queue is not set yet if (q->ring_avail_addr == 0) { PrintError("Queue is not set\n"); return -1; } - - if (q->cur_avail_idx < q->avail->index) { + if (q->last_avail_idx > q->avail->index) + q->idx_overflow = true; + q->last_avail_idx = q->avail->index; + + if (q->cur_avail_idx < q->avail->index || (q->idx_overflow && q->cur_avail_idx < q->avail->index+65536)){ addr_t hdr_addr = 0; uint16_t hdr_idx = q->avail->ring[q->cur_avail_idx % q->queue_size]; uint16_t buf_idx = 0; struct vring_desc * hdr_desc = NULL; - PrintDebug("Descriptor index=%d\n", q->cur_avail_idx % q->queue_size); - hdr_desc = &(q->desc[hdr_idx]); - - PrintDebug("Header Descriptor (ptr=%p) gpa=%p, len=%d, flags=%x, next=%d\n", hdr_desc, - (void *)(hdr_desc->addr_gpa), hdr_desc->length, hdr_desc->flags, hdr_desc->next); - if (guest_pa_to_host_va(virtio->virtio_dev->vm, hdr_desc->addr_gpa, &(hdr_addr)) == -1) { PrintError("Could not translate receive buffer address\n"); return -1; } - //copy header to the header descriptor memcpy((void *)hdr_addr, &hdr, sizeof(struct virtio_net_hdr)); - - //Zheng 01/02/2010: zero payload if (offset >= data_len) { hdr_desc->flags &= ~VIRTIO_NEXT_FLAG; } - //copy data to the next descriptors - //Zheng 01/02/2010: put data into the next descriptor, rather than 0! for (buf_idx = hdr_desc->next; offset < data_len; buf_idx = q->desc[hdr_idx].next) { - // for (buf_idx = 0; offset < data_len; buf_idx = q->desc[hdr_idx].next) { struct vring_desc * buf_desc = &(q->desc[buf_idx]); uint32_t len = 0; - //Zheng 01/02/2010: commented this - we need to check - // if there still is some data left - //buf_desc->flags = VIRTIO_NEXT_FLAG; - - - PrintError("JACK: copying packet to up desc (len = %d)\n", data_len - offset); - v3_hexdump(buf + offset, data_len - offset, NULL, 0); - - len = copy_data_to_desc(virtio, buf_desc, buf + offset, data_len - offset); - + len = copy_data_to_desc(virtio, buf_desc, buf + offset, data_len - offset); offset += len; - - //Zheng 01/02/2010: check if there still is some data left if (offset < data_len) { buf_desc->flags = VIRTIO_NEXT_FLAG; } - - buf_desc->length = len; // TODO: do we need this? - PrintError("JACK: setting buffer descriptor length to %d)\n", buf_desc->length); + buf_desc->length = len; } - q->used->ring[q->used->index % q->queue_size].id = q->avail->ring[q->cur_avail_idx % q->queue_size]; q->used->ring[q->used->index % q->queue_size].length = data_len + hdr_len; // This should be the total length of data sent to guest (header+pkt_data) - q->used->index++; + + int last_idx = q->cur_avail_idx; q->cur_avail_idx++; + if (q->cur_avail_idx < last_idx) + q->idx_overflow = false; + } else { + virtio->pkt_drop++; + +#ifdef VIRTIO_NIC_PROFILE + PrintError("Virtio NIC: %p, one pkt dropped receieved: %ld, dropped: %ld, sent: %ld curidx: %d, avaiIdx: %d\n", + virtio, virtio->pkt_recv, virtio->pkt_drop, virtio->pkt_sent, q->cur_avail_idx, q->avail->index); +#endif } if (!(q->avail->flags & VIRTIO_NO_IRQ_FLAG)) { @@ -331,28 +260,39 @@ static int send_pkt_to_guest(struct virtio_net_state * virtio, uchar_t * buf, ui virtio->virtio_cfg.pci_isr = 0x1; } +#ifdef VIRTIO_NIC_PROFILE + if ((virtio->pkt_recv % 10000) == 0){ + PrintError("Virtio NIC: %p, receieved: %ld, dropped: %ld, sent: %ld\n", + virtio, virtio->pkt_recv, virtio->pkt_drop, virtio->pkt_sent); + } +#endif - PrintError("\n\n\n\n"); + return offset; } +static int virtio_sendto_buf(struct virtio_net_state * net_state, uchar_t * buf, uint_t size) { + struct eth_pkt *pkt; -// TODO: -int virtio_send(struct guest_info * vm, char *dev_name, uchar_t * buf, uint_t size) { - struct vm_device * virtio_dev = v3_find_dev(vm, dev_name); - - // TODO: how to get virtio net state from device?? - // this is not right now - struct virtio_net_state * virtio_state = (struct virtio_net_state *)virtio_dev->private_data; + pkt = (struct eth_pkt *)V3_Malloc(sizeof(struct eth_pkt)); + if(pkt == NULL){ + PrintError("Virtio NIC: Memory allocate fails\n"); + return -1; + } + + pkt->size = size; + memcpy(pkt->data, buf, size); + v3_enqueue(net_state->inpkt_q, (addr_t)pkt); - return send_pkt_to_guest(virtio_state, buf, size, 1, NULL); -} + PrintDebug("Virtio NIC: __virtio_sendto_buf: transmitting packet: (size:%d)\n", (int)pkt->size); + return pkt->size; +} -static int __virtio_dev_send(uchar_t * buf, uint32_t size, void *private_data) { +int virtio_dev_send(uchar_t * buf, uint32_t size, void *private_data) { struct virtio_net_state *virtio_state = (struct virtio_net_state *)private_data; - return send_pkt_to_guest(virtio_state, buf, size, 1, NULL); + return virtio_sendto_buf(virtio_state, buf, size); } static int get_desc_count(struct virtio_queue * q, int index) { @@ -367,23 +307,21 @@ static int get_desc_count(struct virtio_queue * q, int index) { return cnt; } - static int handle_ctrl(struct virtio_net_state * dev) { return 0; } -//get packet from guest static int handle_pkt_tx(struct virtio_net_state * virtio_state) { - //struct virtio_net_state *virtio = (struct virtio_net_state *)dev->private_data; struct virtio_queue * q = &(virtio_state->tx_vq); - - PrintDebug("VIRTIO NIC pkt_tx: cur_index=%d (mod=%d), avail_index=%d\n", - q->cur_avail_idx, q->cur_avail_idx % q->queue_size, q->avail->index); - struct virtio_net_hdr * hdr = NULL; - while (q->cur_avail_idx < q->avail->index) { + if (q->avail->index < q->last_avail_idx) + q->idx_overflow = true; + q->last_avail_idx = q->avail->index; + + while (q->cur_avail_idx < q->avail->index || + (q->idx_overflow && q->cur_avail_idx < (q->avail->index + 65536))) { struct vring_desc * hdr_desc = NULL; addr_t hdr_addr = 0; uint16_t desc_idx = q->avail->ring[q->cur_avail_idx % q->queue_size]; @@ -391,32 +329,18 @@ static int handle_pkt_tx(struct virtio_net_state * virtio_state) uint32_t req_len = 0; int i = 0; - PrintDebug("Descriptor Count=%d, index=%d\n", desc_cnt, q->cur_avail_idx % q->queue_size); - hdr_desc = &(q->desc[desc_idx]); - - PrintDebug("Header Descriptor (ptr=%p) gpa=%p, len=%d, flags=%x, next=%d\n", hdr_desc, - (void *)(hdr_desc->addr_gpa), hdr_desc->length, hdr_desc->flags, hdr_desc->next); - if (guest_pa_to_host_va(virtio_state->virtio_dev->vm, hdr_desc->addr_gpa, &(hdr_addr)) == -1) { PrintError("Could not translate block header address\n"); return -1; } - //memcpy(&hdr, (void *)hdr_addr, sizeof(struct virtio_net_hdr)); hdr = (struct virtio_net_hdr*)hdr_addr; - - PrintDebug("NIC Op Hdr (ptr=%p) header len =%x\n", (void *)hdr_addr, (int)hdr->hdr_len); - desc_idx = hdr_desc->next; for (i = 0; i < desc_cnt - 1; i++) { struct vring_desc * buf_desc = &(q->desc[desc_idx]); - - PrintDebug("Buffer Descriptor (ptr=%p) gpa=%p, len=%d, flags=%x, next=%d\n", buf_desc, - (void *)(buf_desc->addr_gpa), buf_desc->length, buf_desc->flags, buf_desc->next); - - if (pkt_write(virtio_state, buf_desc) == -1) { + if (pkt_tx(virtio_state, buf_desc) == -1) { PrintError("Error handling nic operation\n"); return -1; } @@ -424,24 +348,31 @@ static int handle_pkt_tx(struct virtio_net_state * virtio_state) req_len += buf_desc->length; desc_idx = buf_desc->next; } + virtio_state->pkt_sent ++; q->used->ring[q->used->index % q->queue_size].id = q->avail->ring[q->cur_avail_idx % q->queue_size]; q->used->ring[q->used->index % q->queue_size].length = req_len; // What do we set this to???? - q->used->index++; - q->cur_avail_idx++; + + int last_idx = q->cur_avail_idx; + q->cur_avail_idx ++; + if (q->cur_avail_idx < last_idx) + q->idx_overflow = false; } if (!(q->avail->flags & VIRTIO_NO_IRQ_FLAG)) { - PrintDebug("Raising IRQ %d\n", virtio_state->pci_dev->config_header.intr_line); v3_pci_raise_irq(virtio_state->virtio_dev->pci_bus, 0, virtio_state->pci_dev); virtio_state->virtio_cfg.pci_isr = 0x1; } +#ifdef VIRTIO_NIC_PROFILE + if(virtio_state->pkt_sent % 10000 == 0) + PrintError("Virtio NIC: %p, pkt_sent: %ld\n", virtio_state, virtio_state->pkt_sent); +#endif + return 0; } - static int virtio_setup_queue(struct virtio_net_state * virtio_state, struct virtio_queue * queue, addr_t pfn, addr_t page_addr) { queue->pfn = pfn; @@ -453,19 +384,16 @@ static int virtio_setup_queue(struct virtio_net_state * virtio_state, struct vir // round up to next page boundary. queue->ring_used_addr = (queue->ring_used_addr + 0xfff) & ~0xfff; - if (guest_pa_to_host_va(virtio_state->virtio_dev->vm, queue->ring_desc_addr, (addr_t *)&(queue->desc)) == -1) { PrintError("Could not translate ring descriptor address\n"); return -1; } - if (guest_pa_to_host_va(virtio_state->virtio_dev->vm, queue->ring_avail_addr, (addr_t *)&(queue->avail)) == -1) { PrintError("Could not translate ring available address\n"); return -1; } - if (guest_pa_to_host_va(virtio_state->virtio_dev->vm, queue->ring_used_addr, (addr_t *)&(queue->used)) == -1) { PrintError("Could not translate ring used address\n"); return -1; @@ -482,41 +410,32 @@ static int virtio_setup_queue(struct virtio_net_state * virtio_state, struct vir return 0; } - - -static int virtio_io_write(uint16_t port, void * src, uint_t length, void * private_data) +static int virtio_io_write(struct guest_info *core, uint16_t port, void * src, uint_t length, void * private_data) { - //struct vm_device * dev = (struct vm_device *)private_data; struct virtio_net_state * virtio = (struct virtio_net_state *)private_data; int port_idx = port % virtio->io_range_size; - PrintDebug("VIRTIO NIC %p Write for port %d (index=%d) len=%d, value=%x\n", private_data, port, port_idx, length, *(uint32_t *)src); - switch (port_idx) { case GUEST_FEATURES_PORT: if (length != 4) { PrintError("Illegal write length for guest features\n"); return -1; - } - + } virtio->virtio_cfg.guest_features = *(uint32_t *)src; PrintDebug("Setting Guest Features to %x\n", virtio->virtio_cfg.guest_features); - break; + case VRING_PG_NUM_PORT: - if (length != 4) { PrintError("Illegal write length for page frame number\n"); return -1; } - addr_t pfn = *(uint32_t *)src; addr_t page_addr = (pfn << VIRTIO_PAGE_SHIFT); uint16_t queue_idx = virtio->virtio_cfg.vring_queue_selector; - switch (queue_idx) { case 0: virtio_setup_queue(virtio, &virtio->rx_vq, pfn, page_addr); @@ -526,31 +445,26 @@ static int virtio_io_write(uint16_t port, void * src, uint_t length, void * priv break; case 2: virtio_setup_queue(virtio, &virtio->ctrl_vq, pfn, page_addr); - break; - + break; default: break; } - break; + case VRING_Q_SEL_PORT: virtio->virtio_cfg.vring_queue_selector = *(uint16_t *)src; - if (virtio->virtio_cfg.vring_queue_selector > 2) { PrintError("Virtio NIC device only uses 3 queue, selected %d\n", virtio->virtio_cfg.vring_queue_selector); return -1; } - break; + case VRING_Q_NOTIFY_PORT: { - uint16_t queue_idx = *(uint16_t *)src; - - PrintDebug("Handling Kick\n"); - + uint16_t queue_idx = *(uint16_t *)src; if (queue_idx == 0){ - PrintError("receive queue notification\n"); + PrintDebug("receive queue notification 0, packet get by Guest\n"); } else if (queue_idx == 1){ if (handle_pkt_tx(virtio) == -1) { PrintError("Could not handle NIC Notification\n"); @@ -564,18 +478,16 @@ static int virtio_io_write(uint16_t port, void * src, uint_t length, void * priv } else { PrintError("Virtio NIC device only uses 3 queue, selected %d\n", queue_idx); - } - + } break; } + case VIRTIO_STATUS_PORT: virtio->virtio_cfg.status = *(uint8_t *)src; - if (virtio->virtio_cfg.status == 0) { PrintDebug("Resetting device\n"); - virtio_reset(virtio); + virtio_init_state(virtio); } - break; case VIRTIO_ISR_PORT: @@ -589,10 +501,8 @@ static int virtio_io_write(uint16_t port, void * src, uint_t length, void * priv return length; } - -static int virtio_io_read(uint16_t port, void * dst, uint_t length, void * private_data) +static int virtio_io_read(struct guest_info *core, uint16_t port, void * dst, uint_t length, void * private_data) { - //struct vm_device * dev = (struct vm_device *)private_data; struct virtio_net_state * virtio = (struct virtio_net_state *)private_data; int port_idx = port % virtio->io_range_size; uint16_t queue_idx = virtio->virtio_cfg.vring_queue_selector; @@ -606,19 +516,14 @@ static int virtio_io_read(uint16_t port, void * dst, uint_t length, void * priva PrintError("Illegal read length for host features\n"); return -1; } - *(uint32_t *)dst = virtio->virtio_cfg.host_features; - - PrintDebug("value=0x%x\n", *(uint32_t *)dst); - break; - case VRING_PG_NUM_PORT: + case VRING_PG_NUM_PORT: if (length != 4) { PrintError("Illegal read length for page frame number\n"); return -1; } - switch (queue_idx) { case 0: *(uint32_t *)dst = virtio->rx_vq.pfn; @@ -632,17 +537,13 @@ static int virtio_io_read(uint16_t port, void * dst, uint_t length, void * priva default: break; } - - PrintDebug(", value=0x%x\n", *(uint32_t *)dst); - break; - case VRING_SIZE_PORT: + case VRING_SIZE_PORT: if (length != 2) { PrintError("Illegal read length for vring size\n"); return -1; } - switch (queue_idx) { case 0: *(uint16_t *)dst = virtio->rx_vq.queue_size; @@ -656,29 +557,21 @@ static int virtio_io_read(uint16_t port, void * dst, uint_t length, void * priva default: break; } - PrintDebug("queue index: %d, value=0x%x\n", (int)queue_idx, *(uint16_t *)dst); - break; - case VIRTIO_STATUS_PORT: + case VIRTIO_STATUS_PORT: if (length != 1) { PrintError("Illegal read length for status\n"); return -1; } - *(uint8_t *)dst = virtio->virtio_cfg.status; - - PrintDebug(", value=0x%x\n", *(uint8_t *)dst); break; - + case VIRTIO_ISR_PORT: *(uint8_t *)dst = virtio->virtio_cfg.pci_isr; virtio->virtio_cfg.pci_isr = 0; v3_pci_lower_irq(virtio->virtio_dev->pci_bus, 0, virtio->pci_dev); - - PrintDebug(", value=0x%x\n", *(uint8_t *)dst); - break; default: @@ -697,7 +590,6 @@ static struct v3_device_ops dev_ops = { .stop = NULL, }; - static int register_dev(struct virtio_dev_state * virtio, struct virtio_net_state * net_state) { struct pci_device * pci_dev = NULL; @@ -708,7 +600,6 @@ static int register_dev(struct virtio_dev_state * virtio, struct virtio_net_stat // This gets the number of ports, rounded up to a power of 2 net_state->io_range_size = 1; // must be a power of 2 - while (tmp_ports > 0) { tmp_ports >>= 1; net_state->io_range_size <<= 1; @@ -720,7 +611,6 @@ static int register_dev(struct virtio_dev_state * virtio, struct virtio_net_stat net_state->io_range_size >>= 1; } - for (i = 0; i < 6; i++) { bars[i].type = PCI_BAR_NONE; } @@ -730,7 +620,6 @@ static int register_dev(struct virtio_dev_state * virtio, struct virtio_net_stat bars[0].type = PCI_BAR_IO; bars[0].default_base_port = -1; bars[0].num_ports = net_state->io_range_size; - bars[0].io_read = virtio_io_read; bars[0].io_write = virtio_io_write; bars[0].private_data = net_state; @@ -753,34 +642,24 @@ static int register_dev(struct virtio_dev_state * virtio, struct virtio_net_stat pci_dev->config_header.device_id = VIRTIO_NET_DEV_ID; pci_dev->config_header.class = PCI_CLASS_NETWORK; - pci_dev->config_header.subclass = PCI_NET_SUBCLASS_OTHER; - - pci_dev->config_header.subsystem_id = VIRTIO_NET_SUBDEVICE_ID;; - + pci_dev->config_header.subclass = PCI_NET_SUBCLASS_OTHER; + pci_dev->config_header.subsystem_id = VIRTIO_NET_SUBDEVICE_ID; pci_dev->config_header.intr_pin = 1; - pci_dev->config_header.max_latency = 1; // ?? (qemu does it...) - - net_state->pci_dev = pci_dev; - //net_state->pci_bus = pci_bus; - + net_state->pci_dev = pci_dev; net_state->virtio_cfg.host_features = 0; //no features support now - net_state->rx_vq.queue_size = QUEUE_SIZE; net_state->tx_vq.queue_size = QUEUE_SIZE; net_state->ctrl_vq.queue_size = CTRL_QUEUE_SIZE; - net_state->virtio_dev = virtio; - - virtio_reset(net_state); + + virtio_init_state(net_state); return 0; } - - -static int connect_fn(struct guest_info * info, +static int connect_fn(struct v3_vm_info * info, void * frontend_data, struct v3_dev_net_ops * ops, v3_cfg_tree_t * cfg, @@ -789,7 +668,6 @@ static int connect_fn(struct guest_info * info, struct virtio_net_state * net_state = (struct virtio_net_state *)V3_Malloc(sizeof(struct virtio_net_state)); memset(net_state, 0, sizeof(struct virtio_net_state)); - register_dev(virtio, net_state); net_state->net_ops = ops; @@ -798,8 +676,7 @@ static int connect_fn(struct guest_info * info, return 0; } - -static int virtio_init(struct guest_info * vm, v3_cfg_tree_t * cfg) { +static int virtio_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) { struct vm_device * pci_bus = v3_find_dev(vm, v3_cfg_val(cfg, "bus")); struct virtio_dev_state * virtio_state = NULL; char * name = v3_cfg_val(cfg, "name"); @@ -816,7 +693,7 @@ static int virtio_init(struct guest_info * vm, v3_cfg_tree_t * cfg) { INIT_LIST_HEAD(&(virtio_state->dev_list)); virtio_state->pci_bus = pci_bus; - virtio_state->vm = vm; + //virtio_state->vm = vm; struct vm_device * dev = v3_allocate_device(name, &dev_ops, virtio_state); if (v3_attach_device(vm, dev) == -1) { @@ -828,30 +705,7 @@ static int virtio_init(struct guest_info * vm, v3_cfg_tree_t * cfg) { PrintError("Virtio NIC: Could not register %s as net frontend\n", name); return -1; } - - - //for temporary testing, add a backend - #if 1 - - struct virtio_net_state * net_state = (struct virtio_net_state *)V3_Malloc(sizeof(struct virtio_net_state)); - memset(net_state, 0, sizeof(struct virtio_net_state)); - - net_state->net_ops = (struct v3_dev_net_ops *)V3_Malloc(sizeof(struct v3_dev_net_ops)); - - net_state->net_ops->send = &send; - net_state->net_ops->receive = &receive; - - register_dev(virtio_state, net_state); - PrintDebug("Virtio NIC After register Device %s: queue size: %d, %d\n", dev->name, - net_state->rx_vq.queue_size, net_state->tx_vq.queue_size); - - temp_net_states[net_idx ++] = net_state; - - PrintDebug("Net_states: 0: %p, 1: %p, 2: %p\n", temp_net_states[0], temp_net_states[1], temp_net_states[2]); - - #endif - return 0; } diff --git a/palacios/src/palacios/vmm_vnet.c b/palacios/src/palacios/vmm_vnet.c index bf3b2c6..e7a1a8d 100644 --- a/palacios/src/palacios/vmm_vnet.c +++ b/palacios/src/palacios/vmm_vnet.c @@ -8,16 +8,12 @@ * http://www.v3vee.org * * Copyright (c) 2009, Lei Xia - * Copyright (c) 2009, Yuan Tang - * Copyright (c) 2009, Jack Lange - * Copyright (c) 2009, Peter Dinda + * Copyright (c) 2009, Yuan Tang * Copyright (c) 2009, The V3VEE Project * All rights reserved. * * Author: Lei Xia * Yuan Tang - * Jack Lange - * Peter Dinda #include #include +#include #ifndef CONFIG_DEBUG_VNET #undef PrintDebug @@ -33,81 +30,154 @@ #endif +#define ETHERNET_HEADER_LEN 14 +#define ETHERNET_DATA_MIN 46 +#define ETHERNET_DATA_MAX 1500 +#define ETHERNET_PACKET_LEN (ETHERNET_HEADER_LEN + ETHERNET_DATA_MAX) + + +typedef enum {TCP_TYPE, UDP_TYPE, NONE_TYPE} prot_type_t; + +#define VNET_INITAB_HCALL 0xca00 + +struct eth_header { + uchar_t dest[6]; + uchar_t src[6]; + uint16_t type; // indicates layer 3 protocol type +}__attribute__((packed)); + +struct ip_header { + uint8_t version: 4; + uint8_t hdr_len: 4; + uchar_t tos; + uint16_t total_len; + uint16_t id; + uint8_t flags: 3; + uint16_t offset: 13; + uchar_t ttl; + uchar_t proto; + uint16_t cksum; + uint32_t src_addr; + uint32_t dst_addr; +}__attribute__((packed)); + +struct udp_header { + uint16_t src_port; + uint16_t dst_port; + uint16_t len; + uint16_t csum;//set to zero, disable the xsum +}__attribute__((packed)); + +struct udp_link_header { + struct eth_header eth_hdr; + struct ip_header ip_hdr; + struct udp_header udp_hdr; +}__attribute__((packed)); + + struct ethernet_pkt { - uint32_t size; //size of data + uint32_t size; //size of data field uint16_t type; - struct udp_link_header ext_hdr; //possible externel header to applied to data before sent + uint8_t use_header; + struct udp_link_header ext_hdr; char data[ETHERNET_PACKET_LEN]; +}__attribute__((packed)); + + +#define DEVICE_NAME_LEN 20 +struct vnet_if_device { + char name[DEVICE_NAME_LEN]; + uchar_t mac_addr[6]; + struct v3_vm_info *vm; + + int (*input)(struct v3_vm_info *vm, uchar_t *data, uint32_t len, void *private_data); + + void *private_data; +}__attribute__((packed)); + +struct vnet_if_link { + prot_type_t pro_type; //transport layer protocal type of this link + unsigned long dest_ip; + uint16_t dest_port; + + struct udp_link_header vnet_header; //header applied to the packet in/out from this link + + int (*input)(uchar_t *data, uint32_t len, void *private_data); + + void *private_data; +}__attribute__((packed)); + + +struct link_entry { + link_type_t type; + + union { + struct vnet_if_device *dst_dev; + struct vnet_if_link *dst_link; + } __attribute__((packed)); + + int use; +}__attribute__((packed)); + +//routing table entry +struct routing_entry { + char src_mac[6]; + char dest_mac[6]; + + mac_type_t src_mac_qual; + mac_type_t dest_mac_qual; + + int link_idx; //link[dest] is the link to be used to send pkt + link_type_t link_type; //EDGE|INTERFACE|ANY + + int src_link_idx; + link_type_t src_type; //EDGE|INTERFACE|ANY }; // 14 (ethernet frame) + 20 bytes struct in_pkt_header { - char ethernetdest[6]; - char ethernetsrc[6]; - unsigned char ethernettype[2]; //layer 3 protocol type + uchar_t ethernetdest[6]; + uchar_t ethernetsrc[6]; + uchar_t ethernettype[2]; //layer 3 protocol type char ip[20]; -}; +}__attribute__((packed)); -#define VNET_INITAB_HCALL 0xca00 // inital hypercall id #define MAX_LINKS 10 #define MAX_ROUTES 10 #define HASH_KEY_LEN 16 #define MIN_CACHE_SIZE 100 -static const uint_t hash_key_size = 16; +#define HASH_KEY_SIZE 16 struct link_table { struct link_entry * links[MAX_LINKS]; uint16_t size; -}; + v3_lock_t lock; +}__attribute__((packed)); struct routing_table { struct routing_entry * routes[MAX_ROUTES]; uint16_t size; -}; - -static struct link_table g_links; -static struct routing_table g_routes; -static struct gen_queue * g_inpkt_q; + v3_lock_t lock; +}__attribute__((packed)); -/* Hash key format: - * 0-5: src_eth_addr - * 6-11: dest_eth_addr - * 12: src type - * 13-16: src index - */ typedef char * route_hashkey_t; -// This is the hash value, Format: 0: num_matched_routes, 1...n: matches[] -- TY struct route_cache_entry { int num_matched_routes; int * matches; }; -// the route cache -static struct hashtable * g_route_cache; - - -static void print_packet(uchar_t *pkt, int size) { - PrintDebug("Vnet: print_data_packet: size: %d\n", size); - v3_hexdump(pkt, size, NULL, 0); -} - -#if 0 -static void print_packet_addr(char * pkt) { - PrintDebug("Vnet: print_packet_destination_addr: "); - v3_hexdump(pkt + 8, 6, NULL, 0); - - PrintDebug("Vnet: print_packet_source_addr: "); - v3_hexdump(pkt + 14, 6, NULL, 0); -} +struct vnet_state_t { + struct link_table g_links; + struct routing_table g_routes; + struct gen_queue * g_inpkt_q; + struct hashtable * g_route_cache; + v3_lock_t cache_lock; +}; -static void print_device_addr(char * ethaddr) { - PrintDebug("Vnet: print_device_addr: "); - v3_hexdump(ethaddr, 6, NULL, 0); -} -#endif +static struct vnet_state_t g_vnet_state;//global state for vnet static uint16_t ip_xsum(struct ip_header *ip_hdr, int hdr_len){ long sum = 0; @@ -129,27 +199,30 @@ static uint16_t ip_xsum(struct ip_header *ip_hdr, int hdr_len){ return (uint16_t)~sum; } -static inline void ethernet_packet_init(struct ethernet_pkt *pt, uchar_t *data, const size_t size) { +static void ethernet_packet_init(struct ethernet_pkt *pt, + uchar_t *data, + const size_t size) { pt->size = size; + pt->use_header = 0; + memset(&pt->ext_hdr, 0, sizeof(struct udp_link_header)); memcpy(pt->data, data, size); } -static uint_t hash_from_key_fn(addr_t hashkey) { - uint8_t * key = (uint8_t *)hashkey; - return v3_hash_buffer(key, HASH_KEY_LEN); +static inline uint_t hash_from_key_fn(addr_t hashkey) { + return v3_hash_buffer((uint8_t *)hashkey, HASH_KEY_LEN); } -static int hash_key_equal(addr_t key1, addr_t key2) { - uint8_t * buf1 = (uint8_t *)key1; - uint8_t * buf2 = (uint8_t *)key2; - return (memcmp(buf1, buf2, HASH_KEY_LEN) == 0); +static inline int hash_key_equal(addr_t key1, addr_t key2) { + return (memcmp((uint8_t *)key1, (uint8_t *)key2, HASH_KEY_LEN) == 0); } -static int init_route_cache() { - g_route_cache = v3_create_htable(MIN_CACHE_SIZE, &hash_from_key_fn, &hash_key_equal); +static int init_route_cache(struct vnet_state_t *vnet_state) { + vnet_state->g_route_cache = v3_create_htable(MIN_CACHE_SIZE, + &hash_from_key_fn, + &hash_key_equal); - if (g_route_cache == NULL) { - PrintError("Vnet: Route Cache Initiate Failurely\n"); + if (vnet_state->g_route_cache == NULL) { + PrintError("Vnet: Route Cache Init Fails\n"); return -1; } @@ -157,10 +230,10 @@ static int init_route_cache() { } static void make_hash_key(route_hashkey_t hashkey, - char src_addr[6], - char dest_addr[6], - char src_type, - int src_index) { + char src_addr[6], + char dest_addr[6], + char src_type, + int src_index) { int j; for (j = 0; j < 6; j++) { @@ -173,8 +246,11 @@ static void make_hash_key(route_hashkey_t hashkey, *(int *)(hashkey + 12) = src_index; } -static int add_route_to_cache(route_hashkey_t hashkey, int num_matched_r, int * matches) { +static int add_route_to_cache(route_hashkey_t hashkey, + int num_matched_r, + int * matches) { struct route_cache_entry * new_entry = NULL; + struct vnet_state_t *vnet_state = &g_vnet_state; int i; new_entry = (struct route_cache_entry *)V3_Malloc(sizeof(struct route_cache_entry)); @@ -184,7 +260,6 @@ static int add_route_to_cache(route_hashkey_t hashkey, int num_matched_r, int * } new_entry->num_matched_routes = num_matched_r; - new_entry->matches = (int *)V3_Malloc(num_matched_r * sizeof(int)); if (new_entry->matches == NULL) { @@ -196,9 +271,8 @@ static int add_route_to_cache(route_hashkey_t hashkey, int num_matched_r, int * new_entry->matches[i] = matches[i]; } - // here, when v3_htable_insert return 0, it means insert fails - if (v3_htable_insert(g_route_cache, (addr_t)hashkey, (addr_t)new_entry) == 0) { - PrintError("Vnet: Insert new route entry to cache failed\n"); + if (v3_htable_insert(vnet_state->g_route_cache, (addr_t)hashkey, (addr_t)new_entry) == 0) { + PrintError("Vnet: Failed to insert new route entry to the cache\n"); V3_Free(new_entry->matches); V3_Free(new_entry); } @@ -207,11 +281,14 @@ static int add_route_to_cache(route_hashkey_t hashkey, int num_matched_r, int * } static int clear_hash_cache() { - v3_free_htable(g_route_cache, 1, 1); - - g_route_cache = v3_create_htable(MIN_CACHE_SIZE, hash_from_key_fn, hash_key_equal); + struct vnet_state_t *vnet_state = &g_vnet_state; + + v3_free_htable(vnet_state->g_route_cache, 1, 1); + vnet_state->g_route_cache = v3_create_htable(MIN_CACHE_SIZE, + hash_from_key_fn, + hash_key_equal); - if (g_route_cache == NULL) { + if (vnet_state->g_route_cache == NULL) { PrintError("Vnet: Route Cache Create Failurely\n"); return -1; } @@ -223,8 +300,10 @@ static int look_into_cache(route_hashkey_t hashkey, int * matches) { struct route_cache_entry * found = NULL; int n_matches = -1; int i = 0; + struct vnet_state_t *vnet_state = &g_vnet_state; - found = (struct route_cache_entry *)v3_htable_search(g_route_cache, (addr_t)hashkey); + found = (struct route_cache_entry *)v3_htable_search(vnet_state->g_route_cache, + (addr_t)hashkey); if (found != NULL) { n_matches = found->num_matched_routes; @@ -237,7 +316,14 @@ static int look_into_cache(route_hashkey_t hashkey, int * matches) { return n_matches; } + #ifdef CONFIG_DEBUG_VNET + +static void print_packet(uchar_t *pkt, int size) { + PrintDebug("Vnet: data_packet: size: %d\n", size); + v3_hexdump(pkt, size, NULL, 0); +} + static inline uint8_t hex_nybble_to_nybble(const uint8_t hexnybble) { uint8_t x = toupper(hexnybble); @@ -262,72 +348,149 @@ static inline void string_to_mac(const char *str, uint8_t mac[6]) { } static inline void mac_to_string(char mac[6], char * buf) { - snprintf(buf, 20, "%x:%x:%x:%x:%x:%x", + snprintf(buf, 20, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); } + + +static void print_route(struct routing_entry *route){ + char dest_str[18]; + char src_str[18]; + + mac_to_string(route->src_mac, src_str); + mac_to_string(route->dest_mac, dest_str); + + PrintDebug("SRC(%s), DEST(%s), src_mac_qual(%d), dst_mac_qual(%d)\n", + src_str, + dest_str, + route->src_mac_qual, + route->dest_mac_qual); + PrintDebug("Src_Link(%d), src_type(%d), dst_link(%d), dst_type(%d)\n\n", + route->src_link_idx, + route->src_type, + route->link_idx, + route->link_type); +} + + +static void dump_routes(struct routing_entry **route_tables) { + int i; + + PrintDebug("\nVnet: route table dump start =====\n"); + + for(i = 0; i < MAX_ROUTES; i++) { + if (route_tables[i] != NULL){ + print_route(route_tables[i]); + } + } + + PrintDebug("\nVnet: route table dump end =====\n"); +} + +static void dump_dom0_routes(struct routing_entry routes[], int size) { + int i; + + PrintDebug("\nVnet: route table from dom0 guest =====\n"); + + for(i = 0; i dest_ip, + link->dest_port, + link->pro_type); + PrintDebug("vnet_header:\n"); + v3_hexdump(&link->vnet_header, sizeof(struct udp_link_header), NULL, 0); + } + + PrintDebug("\nVnet: link table dom0 guest end =====\n"); +} + #endif static int __add_link_entry(struct link_entry * link) { int idx; - + struct vnet_state_t *vnet_state = &g_vnet_state; + + v3_lock(vnet_state->g_links.lock); for (idx = 0; idx < MAX_LINKS; idx++) { - if (g_links.links[idx] == NULL) { - g_links.links[idx] = link; - g_links.size++; - - return idx; + if (vnet_state->g_links.links[idx] == NULL) { + vnet_state->g_links.links[idx] = link; + vnet_state->g_links.size++; + break; } } + v3_unlock(vnet_state->g_links.lock); - PrintError("No available Link entry\n"); - return -1; + if (idx == MAX_LINKS) { + PrintDebug("VNET: No available Link entry for new link\n"); + return -1; + } + + return idx; } static int __add_route_entry(struct routing_entry * route) { int idx; - + struct vnet_state_t *vnet_state = &g_vnet_state; + + v3_lock(vnet_state->g_routes.lock); for (idx = 0; idx < MAX_ROUTES; idx++) { - if (g_routes.routes[idx] == NULL) { - g_routes.routes[idx] = route; - g_routes.size++; - - return idx; + if (vnet_state->g_routes.routes[idx] == NULL) { + vnet_state->g_routes.routes[idx] = route; + vnet_state->g_routes.size++; + break; } } + v3_unlock(vnet_state->g_routes.lock); - PrintError("No available route entry\n"); - return -1; -} + if(idx == MAX_LINKS){ + PrintDebug("VNET: No available route entry for new route\n"); + return -1; + } +#ifdef CONFIG_DEBUG_VNET + dump_routes(vnet_state->g_routes.routes); +#endif + + return idx; +} -static int vnet_add_route_entry(char src_mac[6], - char dest_mac[6], - int src_mac_qual, - int dest_mac_qual, - int link_idx, - link_type_t link_type, - int src, - link_type_t src_type) { +int v3_vnet_add_route(struct v3_vnet_route *route){ struct routing_entry * new_route = (struct routing_entry *)V3_Malloc(sizeof(struct routing_entry)); int idx = -1; + PrintDebug("Vnet: vnet_add_route_entry\n"); + memset(new_route, 0, sizeof(struct routing_entry)); - - if ((src_mac_qual != MAC_ANY) && (src_mac_qual != MAC_NONE)) { - memcpy(new_route->src_mac, src_mac, 6); + if ((route->src_mac_qual != MAC_ANY)) { + memcpy(new_route->src_mac, route->src_mac, 6); } - if ((dest_mac_qual != MAC_ANY) && (dest_mac_qual != MAC_NONE)) { - memcpy(new_route->dest_mac, dest_mac, 6); + if ((route->dest_mac_qual != MAC_ANY)) { + memcpy(new_route->dest_mac, route->dest_mac, 6); } - new_route->src_mac_qual = src_mac_qual; - new_route->dest_mac_qual = dest_mac_qual; - new_route->link_idx= link_idx; - new_route->link_type = link_type; - new_route->src_link_idx = src; - new_route->src_type = src_type; + new_route->src_mac_qual = route->src_mac_qual; + new_route->dest_mac_qual = route->dest_mac_qual; + new_route->link_idx= route->link_idx; + new_route->link_type = route->link_type; + new_route->src_link_idx = route->src_link_idx; + new_route->src_type = route->src_type; if ((idx = __add_route_entry(new_route)) == -1) { PrintError("Could not add route entry\n"); @@ -335,264 +498,114 @@ static int vnet_add_route_entry(char src_mac[6], } clear_hash_cache(); - - return idx; -} -static void * __delete_link_entry(int index) { - struct link_entry * link = NULL; - void * ret = NULL; - link_type_t type; - - if ((index >= MAX_LINKS) || (g_links.links[index] == NULL)) { - return NULL; - } - - link = g_links.links[index]; - type = g_links.links[index]->type; - - if (type == LINK_INTERFACE) { - ret = (void *)g_links.links[index]->dst_dev; - } else if (type == LINK_EDGE) { - ret = (void *)g_links.links[index]->dst_link; - } - - g_links.links[index] = NULL; - g_links.size--; - - V3_Free(link); - - return ret; -} - -static int find_route_entry(char src_mac[6], - char dest_mac[6], - int src_mac_qual, - int dest_mac_qual, - int link_idx, - link_type_t link_type, - int src, - link_type_t src_type) { - int i; - char temp_src_mac[6]; - char temp_dest_mac[6]; - - if ((src_mac_qual != MAC_ANY) && (src_mac_qual != MAC_NONE)) { - memcpy(temp_src_mac, src_mac, 6); - } else { - memset(temp_src_mac, 0, 6); - } - - if ((dest_mac_qual != MAC_ANY) && (dest_mac_qual != MAC_NONE)) { - memcpy(temp_dest_mac, dest_mac, 6); - } else { - memset(temp_dest_mac, 0, 6); - } - - for (i = 0; i < MAX_ROUTES; i++) { - if (g_routes.routes[i] != NULL) { - if ((memcmp(temp_src_mac, g_routes.routes[i]->src_mac, 6) == 0) && - (memcmp(temp_dest_mac, g_routes.routes[i]->dest_mac, 6) == 0) && - (g_routes.routes[i]->src_mac_qual == src_mac_qual) && - (g_routes.routes[i]->dest_mac_qual == dest_mac_qual) && - ( (link_type == LINK_ANY) || - ((link_type == g_routes.routes[i]->link_type) && (g_routes.routes[i]->link_idx == link_idx))) && - ( (src_type == LINK_ANY) || - ((src_type == g_routes.routes[i]->src_type) && (g_routes.routes[i]->src_link_idx == src)))) { - return i; - } - } - } - - return -1; -} - -static int __delete_route_entry(int index) { - struct routing_entry * route; - - if ((index >= MAX_ROUTES) || (g_routes.routes[index] == NULL)) { - PrintDebug("VNET: wrong index in delete route entry %d\n", index); - return -1; - } - - route = g_routes.routes[index]; - g_routes.routes[index] = NULL; - g_routes.size--; - - V3_Free(route); - - clear_hash_cache(); - - return 0; -} - -static int vnet_delete_route_entry_by_addr(char src_mac[6], - char dest_mac[6], - int src_mac_qual, - int dest_mac_qual, - int link_idx, - link_type_t type, - int src, - link_type_t src_type) { - int index = find_route_entry(src_mac, dest_mac, src_mac_qual, - dest_mac_qual, link_idx, type, src, src_type); - - if (index == -1) { - PrintDebug("VNET: wrong in delete route entry %d\n", index); - return -1; - } - - return __delete_route_entry(index); + return idx; } static int match_route(uint8_t * src_mac, uint8_t * dst_mac, link_type_t src_type, int src_index, - int * matches) { - int values[MAX_ROUTES]; + int * matches) { + struct routing_entry *route = NULL; + struct vnet_state_t *vnet_state = &g_vnet_state; int matched_routes[MAX_ROUTES]; - int num_matches = 0; int i; - int max = 0; - int no = 0; - int exact_match = 0; + +#ifdef CONFIG_DEBUG_VNET + char dest_str[18]; + char src_str[18]; + + mac_to_string(src_mac, src_str); + mac_to_string(dst_mac, dest_str); + PrintDebug("Vnet: match_route. pkt: SRC(%s), DEST(%s)\n", src_str, dest_str); +#endif for(i = 0; i < MAX_ROUTES; i++) { - if (g_routes.routes[i] != NULL){ - if ( (g_routes.routes[i]->src_type != LINK_ANY) && - ( (g_routes.routes[i]->src_type != src_type) || - ( (g_routes.routes[i]->src_link_idx != src_index) && - (g_routes.routes[i]->src_link_idx != -1)))) { - PrintDebug("Vnet: MatchRoute: Source route is on and does not match\n"); - continue; - } + if (vnet_state->g_routes.routes[i] != NULL){ + route = vnet_state->g_routes.routes[i]; + + if(src_type == LINK_ANY && src_index == -1) { + if ((route->dest_mac_qual == MAC_ANY) && + (route->src_mac_qual == MAC_ANY)) { + matched_routes[num_matches] = i; + num_matches++; + } - if ( (g_routes.routes[i]->dest_mac_qual == MAC_ANY) && - (g_routes.routes[i]->src_mac_qual == MAC_ANY)) { + if (memcmp((void *)&route->src_mac, (void *)src_mac, 6) == 0) { + if (route->src_mac_qual != MAC_NOT) { + if (route->dest_mac_qual == MAC_ANY) { + matched_routes[num_matches] = i; + num_matches++; + } else if (route->dest_mac_qual != MAC_NOT && + memcmp((void *)&route->dest_mac, (void *)dst_mac, 6) == 0) { + matched_routes[num_matches] = i; + num_matches++; + } + } + } - matched_routes[num_matches] = i; - values[num_matches] = 3; - num_matches++; - } - - if (memcmp((void *)&g_routes.routes[i]->src_mac, (void *)src_mac, 6) == 0) { - if (g_routes.routes[i]->src_mac_qual != MAC_NOT) { - if (g_routes.routes[i]->dest_mac_qual == MAC_ANY) { - matched_routes[num_matches] = i; - values[num_matches] = 6; - - num_matches++; - } else if (memcmp((void *)&g_routes.routes[i]->dest_mac, (void *)dst_mac, 6) == 0) { - if (g_routes.routes[i]->dest_mac_qual != MAC_NOT) { - matched_routes[num_matches] = i; - values[num_matches] = 8; - exact_match = 1; - num_matches++; - } - } - } - } - - if (memcmp((void *)&g_routes.routes[i]->dest_mac, (void *)dst_mac, 6) == 0) { - if (g_routes.routes[i]->dest_mac_qual != MAC_NOT) { - if (g_routes.routes[i]->src_mac_qual == MAC_ANY) { - matched_routes[num_matches] = i; - values[num_matches] = 6; - - num_matches++; - } else if (memcmp((void *)&g_routes.routes[i]->src_mac, (void *)src_mac, 6) == 0) { - if (g_routes.routes[i]->src_mac_qual != MAC_NOT) { - if (exact_match == 0) { - matched_routes[num_matches] = i; - values[num_matches] = 8; - num_matches++; - } - } - } - } - } + if (memcmp((void *)&route->dest_mac, (void *)dst_mac, 6) == 0) { + if (route->dest_mac_qual != MAC_NOT) { + if (route->src_mac_qual == MAC_ANY) { + matched_routes[num_matches] = i; + num_matches++; + } else if ((route->src_mac_qual != MAC_NOT) && + (memcmp((void *)&route->src_mac, (void *)src_mac, 6) == 0)) { + matched_routes[num_matches] = i; + num_matches++; + } + } + } + + if ((route->dest_mac_qual == MAC_NOT) && + (memcmp((void *)&route->dest_mac, (void *)dst_mac, 6) != 0)) { + if (route->src_mac_qual == MAC_ANY) { + matched_routes[num_matches] = i; + num_matches++; + } else if ((route->src_mac_qual != MAC_NOT) && + (memcmp((void *)&route->src_mac, (void *)src_mac, 6) == 0)) { + matched_routes[num_matches] = i; + num_matches++; + } + } + + if ((route->src_mac_qual == MAC_NOT) && + (memcmp((void *)&route->src_mac, (void *)src_mac, 6) != 0)) { + if (route->dest_mac_qual == MAC_ANY) { + matched_routes[num_matches] = i; + num_matches++; + } else if ((route->dest_mac_qual != MAC_NOT) && + (memcmp((void *)&route->dest_mac, (void *)dst_mac, 6) == 0)) { + matched_routes[num_matches] = i; + num_matches++; + } + } + }//end if src_type == Link_any + } + }//end for + + PrintDebug("Vnet: match_route: Matches=%d\n", num_matches); - if ( (g_routes.routes[i]->dest_mac_qual == MAC_NOT) && - (memcmp((void *)&g_routes.routes[i]->dest_mac, (void *)dst_mac, 6) != 0)) { - if (g_routes.routes[i]->src_mac_qual == MAC_ANY) { - matched_routes[num_matches] = i; - values[num_matches] = 5; - num_matches++; - } else if (memcmp((void *)&g_routes.routes[i]->src_mac, (void *)src_mac, 6) == 0) { - if (g_routes.routes[i]->src_mac_qual != MAC_NOT) { - matched_routes[num_matches] = i; - values[num_matches] = 7; - num_matches++; - } - } - } - - if ( (g_routes.routes[i]->src_mac_qual == MAC_NOT) && - (memcmp((void *)&g_routes.routes[i]->src_mac, (void *)src_mac, 6) != 0)) { - if (g_routes.routes[i]->dest_mac_qual == MAC_ANY) { - matched_routes[num_matches] = i; - values[num_matches] = 5; - num_matches++; - } else if (memcmp((void *)&g_routes.routes[i]->dest_mac, (void *)dst_mac, 6) == 0) { - if (g_routes.routes[i]->dest_mac_qual != MAC_NOT) { - matched_routes[num_matches] = i; - values[num_matches] = 7; - num_matches++; - } - } - } - } - } // end for - - for (i = 0; i < MAX_ROUTES; i++) { - if ( (memcmp((void *)&g_routes.routes[i]->src_mac, (void *)src_mac, 6) == 0) && - (g_routes.routes[i]->dest_mac_qual == MAC_NONE) && - ( (g_routes.routes[i]->src_type == LINK_ANY) || - ( (g_routes.routes[i]->src_type == src_type) && - ( (g_routes.routes[i]->src_link_idx == src_index) || - (g_routes.routes[i]->src_link_idx == -1))))) { - matched_routes[num_matches] = i; - values[num_matches] = 4; - PrintDebug("Vnet: MatchRoute: We matched a default route (%d)\n", i); - num_matches++; - } - } - - //If many rules have been matched, we choose one which has the highest value rating - if (num_matches == 0) { - return 0; - } - for (i = 0; i < num_matches; i++) { - if (values[i] > max) { - no = 0; - max = values[i]; - matches[no] = matched_routes[i]; - no++; - } else if (values[i] == max) { - matches[no] = matched_routes[i]; - no++; - } + matches[i] = matched_routes[i]; } - return no; + return num_matches; } static int handle_one_pkt(struct ethernet_pkt *pkt) { int src_link_index = -1; //the value of src_link_index of udp always is 0 - int i; char src_mac[6]; char dst_mac[6]; - int matches[MAX_ROUTES]; int num_matched_routes = 0; - struct in_pkt_header header; + char hash_key[HASH_KEY_SIZE]; + struct vnet_state_t *vnet_state = &g_vnet_state; + int i; - char hash_key[hash_key_size]; - // get the ethernet and ip headers from the packet memcpy((void *)&header, (void *)pkt->data, sizeof(header)); memcpy(src_mac, header.ethernetsrc, 6); @@ -601,28 +614,27 @@ static int handle_one_pkt(struct ethernet_pkt *pkt) { #ifdef CONFIG_DEBUG_VNET char dest_str[18]; char src_str[18]; - + mac_to_string(src_mac, src_str); mac_to_string(dst_mac, dest_str); - PrintDebug("Vnet: HandleDataOverLink. SRC(%s), DEST(%s)\n", src_str, dest_str); #endif - // link_edge -> pt->type??? make_hash_key(hash_key, src_mac, dst_mac, LINK_EDGE, src_link_index); - num_matched_routes = look_into_cache((route_hashkey_t)hash_key, matches); if (num_matched_routes == -1) { - // no match in the cache - num_matched_routes = match_route(src_mac, dst_mac, LINK_ANY, src_link_index, matches); - + num_matched_routes = match_route(src_mac, dst_mac, LINK_ANY, src_link_index, matches); if (num_matched_routes > 0) { add_route_to_cache(hash_key, num_matched_routes,matches); } } PrintDebug("Vnet: HandleDataOverLink: Matches=%d\n", num_matched_routes); + + if (num_matched_routes == 0) { + return -1; + } for (i = 0; i < num_matched_routes; i++) {//send packet to all destinations int route_index = -1; @@ -631,20 +643,16 @@ static int handle_one_pkt(struct ethernet_pkt *pkt) { struct link_entry * link = NULL; route_index = matches[i]; - - PrintDebug("Vnet: HandleDataOverLink: Forward packet from link according to Route entry %d\n", route_index); - - link_index = g_routes.routes[route_index]->link_idx; + link_index = vnet_state->g_routes.routes[route_index]->link_idx; - if ((link_index < 0) || - (link_index > MAX_LINKS) || - (g_links.links[link_index] == NULL)) { + if ((link_index < 0) || (link_index > MAX_LINKS) || + (vnet_state->g_links.links[link_index] == NULL)) { continue; } - link = g_links.links[link_index]; + link = vnet_state->g_links.links[link_index]; pkt_len = pkt->size; - if (g_routes.routes[route_index]->link_type == LINK_EDGE) { + if (link->type == LINK_EDGE) { //apply the header in the beginning of the packet to be sent if (link->dst_link->pro_type == UDP_TYPE) { @@ -660,70 +668,77 @@ static int handle_one_pkt(struct ethernet_pkt *pkt) { pkt_len += hdr_size; if ((link->dst_link->input((uchar_t *)&pkt->ext_hdr, pkt_len, link->dst_link->private_data)) != pkt_len) { - PrintDebug("VNET: Packet not sent properly\n"); - return -1; + PrintDebug("VNET: Packet not sent properly to link: %d\n", link_index); + continue; } }else { PrintDebug("VNET: Link protocol type not support\n"); - return -1; + continue; } - } else if (g_routes.routes[route_index]->link_type == LINK_INTERFACE) { - if ((link->dst_dev->input(pkt->data, pkt_len, link->dst_dev->private_data)) != pkt_len) { - PrintDebug("VNET: Packet not sent properly\n"); - return -1; + } else if (link->type == LINK_INTERFACE) { + if ((link->dst_dev->input(link->dst_dev->vm, pkt->data, pkt_len, link->dst_dev->private_data)) != pkt_len) { + PrintDebug("VNET: Packet not sent properly to link: %d\n", link_index); + continue; } } else { - PrintDebug("Vnet: Wrong Edge type\n"); - return -1; + PrintDebug("Vnet: Wrong Edge type of link: %d\n", link_index); + continue; } + + PrintDebug("Vnet: HandleDataOverLink: Forward packet according to Route entry %d to link %d\n", route_index, link_index); } return 0; } -static int send_ethernet_pkt(uchar_t *data, int len) { +static int send_ethernet_pkt(uchar_t *data, int len, void *private_data) { struct ethernet_pkt *pkt; + struct vnet_state_t *vnet_state = &g_vnet_state; pkt = (struct ethernet_pkt *)V3_Malloc(sizeof(struct ethernet_pkt)); - memset(pkt, 0, sizeof(struct ethernet_pkt)); - if(pkt == NULL){ PrintError("VNET: Memory allocate fails\n"); return -1; } - - ethernet_packet_init(pkt, data, len); //====here we copy sending data once - - PrintDebug("VNET: vm_send_pkt: transmitting packet: (size:%d)\n", (int)pkt->size); + + memset(pkt, 0, sizeof(struct ethernet_pkt)); + ethernet_packet_init(pkt, data, len); + v3_enqueue(vnet_state->g_inpkt_q, (addr_t)pkt); + +#ifdef CONFIG_DEBUG_VNET + PrintDebug("VNET: send_pkt: transmitting packet: (size:%d)\n", (int)pkt->size); print_packet((char *)data, len); - - v3_enqueue(g_inpkt_q, (addr_t)pkt); +#endif return 0; } -//send raw ethernet packet -int v3_vnet_send_rawpkt(uchar_t * buf, int len, void *private_data) { - PrintDebug("VNET: In V3_Send_pkt: pkt length %d\n", len); +int v3_vnet_send_rawpkt(uchar_t * buf, + int len, + void *private_data) { + PrintDebug("VNET: In v3_vnet_send_rawpkt: pkt length %d\n", len); - return send_ethernet_pkt(buf, len); + return send_ethernet_pkt(buf, len, private_data); } //sending the packet from Dom0, should remove the link header -int v3_vnet_send_udppkt(uchar_t * buf, int len, void *private_data) { - PrintDebug("VNET: In V3_Send_pkt: pkt length %d\n", len); - +int v3_vnet_send_udppkt(uchar_t * buf, + int len, + void *private_data) { uint_t hdr_len = sizeof(struct udp_link_header); + + PrintDebug("VNET: In v3_vnet_send_udppkt: pkt length %d\n", len); - return send_ethernet_pkt((uchar_t *)(buf+hdr_len), len - hdr_len); + return send_ethernet_pkt((uchar_t *)(buf+hdr_len), len - hdr_len, private_data); } static int search_device(char * device_name) { + struct vnet_state_t *vnet_state = &g_vnet_state; int i; for (i = 0; i < MAX_LINKS; i++) { - if ((g_links.links[i] != NULL) && (g_links.links[i]->type == LINK_INTERFACE)) { - if (strcmp(device_name, g_links.links[i]->dst_dev->name) == 0) { + if ((vnet_state->g_links.links[i] != NULL) && (vnet_state->g_links.links[i]->type == LINK_INTERFACE)) { + if (strcmp(device_name, vnet_state->g_links.links[i]->dst_dev->name) == 0) { return i; } } @@ -732,22 +747,20 @@ static int search_device(char * device_name) { return -1; } -int vnet_register_device(struct vm_device * vdev, - char * dev_name, - uchar_t mac[6], - int (*netif_input)(uchar_t * pkt, uint_t size, void * private_data), - void * data) { +int v3_vnet_add_node(struct v3_vm_info *info, + char * dev_name, + uchar_t mac[6], + int (*netif_input)(struct v3_vm_info * vm, uchar_t * pkt, uint_t size, void * private_data), + void * priv_data){ struct vnet_if_device * if_dev; int idx = search_device(dev_name); - if (idx != -1) { PrintDebug("VNET: register device: Already has device with the name %s\n", dev_name); return -1; } - if_dev = (struct vnet_if_device *)(sizeof(struct vnet_if_device)); - + if_dev = (struct vnet_if_device *)V3_Malloc(sizeof(struct vnet_if_device)); if (if_dev == NULL) { PrintError("VNET: Malloc fails\n"); return -1; @@ -755,260 +768,76 @@ int vnet_register_device(struct vm_device * vdev, strcpy(if_dev->name, dev_name); strncpy(if_dev->mac_addr, mac, 6); - if_dev->dev = vdev; if_dev->input = netif_input; - if_dev->private_data = data; - - struct link_entry * link = (struct link_entry *)(sizeof(struct link_entry)); + if_dev->private_data = priv_data; + if_dev->vm = info; + struct link_entry * link = (struct link_entry *)V3_Malloc(sizeof(struct link_entry)); link->type = LINK_INTERFACE; link->dst_dev = if_dev; idx = __add_link_entry(link); - PrintDebug("VNET: Add device %s to link table, idx %d", dev_name, idx); - - return idx; -} - -#if 0 -static int vnet_unregister_device(char * dev_name) { - int idx; - - idx = search_device(dev_name); - - if (idx == -1) { - PrintDebug("VNET: No device with name %s found\n", dev_name); - return -1; - } - - struct vnet_if_device * device = (struct vnet_if_device *)__delete_link_entry(idx); - if (device == NULL) { - PrintError("VNET: Device %s not in the link table %d, something may be wrong in link table\n", dev_name, idx); - return -1; - } - - V3_Free(device); + if (idx < 0) return -1; return idx; } -#endif - int v3_vnet_pkt_process() { struct ethernet_pkt * pkt; + struct vnet_state_t *vnet_state = &g_vnet_state; - PrintDebug("VNET: In vnet_check\n"); - - while ((pkt = (struct ethernet_pkt *)v3_dequeue(g_inpkt_q)) != NULL) { - PrintDebug("VNET: In vnet_check: pt length %d, pt type %d\n", (int)pkt->size, (int)pkt->type); - v3_hexdump(pkt->data, pkt->size, NULL, 0); - - if (handle_one_pkt(pkt)) { - PrintDebug("VNET: vnet_check: handle one packet!\n"); - } else { - PrintError("VNET: vnet_check: fail to forward one packet, discard it!\n"); - } + while ((pkt = (struct ethernet_pkt *)v3_dequeue(vnet_state->g_inpkt_q))!= NULL) { + if (handle_one_pkt(pkt) != -1) { + PrintDebug("VNET: vnet_check: handle one packet! pt length %d, pt type %d\n", (int)pkt->size, (int)pkt->type); + } else { + PrintDebug("VNET: vnet_check: Fail to forward one packet, discard it!\n"); + } - V3_Free(pkt); // be careful here + V3_Free(pkt); // be careful here } return 0; } - -static void init_empty_link_table() { +static void vnet_state_init(struct vnet_state_t *vnet_state) { int i; + /*initial links table */ for (i = 0; i < MAX_LINKS; i++) { - g_links.links[i] = NULL; - } - - g_links.size = 0; -} - -static void init_empty_route_table() { - int i; - - for (i = 0; i < MAX_ROUTES; i++) { - g_routes.routes[i] = NULL; + vnet_state->g_links.links[i] = NULL; } - - g_links.size = 0; -} - -static void init_tables() { - init_empty_link_table(); - init_empty_route_table(); - init_route_cache(); -} - -static void init_pkt_queue() { - PrintDebug("VNET Init package receiving queue\n"); - - g_inpkt_q = v3_create_queue(); - v3_init_queue(g_inpkt_q); -} - -static void free_link_mem(struct link_entry *link){ - V3_Free(link->dst_dev); - V3_Free(link); -} - -// TODO: -static int addto_routing_link_tables(struct routing_entry *route_tab, - uint16_t num_routes, - struct link_entry *link_tab, - uint16_t num_links){ - struct routing_entry *route, *new_route; - struct link_entry *link, *new_link; - int new_idx; - int i; - int link_idxs[MAX_LINKS]; - - //add all of the links first, record their new indexs - for (i = 0; i < num_links; i++) { - link_idxs[i] = -1; - link = &link_tab[i]; - new_link = (struct link_entry *)V3_Malloc(sizeof(struct link_entry)); - - if (new_link == NULL){ - PrintError("VNET: Memory allocate error\n"); - return -1; - } - - new_link->type = link->type; - - //TODO: how to set the input parameters here - if (link->type == LINK_EDGE){ - struct vnet_if_device *ifdev = (struct vnet_if_device *)V3_Malloc(sizeof(struct vnet_if_device)); - - if (ifdev == NULL){ - PrintError("VNET: Memory allocate fails\n"); - return -1; - } - - memcpy(ifdev->name, link->dst_dev->name, DEVICE_NAME_LEN); - - // TODO: - //ifdev->mac_addr - //ifdev->input - //ifdev->private_data - - new_link->dst_dev = ifdev; - }else if (link->type == LINK_INTERFACE){ - struct vnet_if_link *iflink = (struct vnet_if_link *)V3_Malloc(sizeof(struct vnet_if_link)); - - if (iflink == NULL){ - PrintError("VNET: Memory allocate fails\n"); - return -1; - } - iflink->pro_type = link->dst_link->pro_type; - iflink->dest_ip = link->dst_link->dest_ip; - iflink->dest_port = link->dst_link->dest_port; - memcpy(&iflink->vnet_header, &link->dst_link->vnet_header, sizeof(struct udp_link_header)); - - // TODO: - //iflink->input = - //iflink->private_data = - - new_link->dst_link = iflink; - }else{ - PrintDebug("VNET: invalid link type\n"); - V3_Free(new_link); - continue; - } - - new_idx = __add_link_entry(new_link); - if (new_idx < 0) { - PrintError("VNET: Adding link fails\n"); - free_link_mem(new_link); - continue; - } - link_idxs[i] = new_idx; - } - - //add all of routes, replace with new link indexs - for (i = 0; i < num_routes; i++) { - route = &route_tab[i]; - if (route->link_idx < 0 || route->link_idx >= num_links || - ((route->src_link_idx != -1) && - (route->src_link_idx < 0 || route->src_link_idx >= num_links))){ - PrintError("VNET: There is error in the intial tables data from guest\n"); - continue; - } - - new_route = (struct routing_entry *)V3_Malloc(sizeof(struct routing_entry)); - - if (new_route == NULL){ - PrintError("VNET: Memory allocate fails\n"); - return -1; - } - memcpy(new_route, route, sizeof(struct routing_entry)); - - new_route->link_idx = link_idxs[new_route->link_idx]; - - if (route->src_link_idx != -1) - new_route->src_link_idx = link_idxs[new_route->src_link_idx]; - - if((__add_route_entry(new_route)) == -1){ - PrintDebug("VNET: Adding route fails"); - V3_Free(new_route); - } - new_route = NULL; + vnet_state->g_links.size = 0; + if(v3_lock_init(&(vnet_state->g_links.lock)) == -1){ + PrintError("VNET: Failure to init lock for links table\n"); } + PrintDebug("VNET: Links table initiated\n"); - - return 0; -} - -struct table_init_info { - addr_t routing_table_start; - uint16_t routing_table_size; - addr_t link_table_start; - uint16_t link_table_size; -}; - -//add the guest specified routes and links to the tables -static int handle_init_tables_hcall(struct guest_info * info, uint_t hcall_id, void * priv_data) { - addr_t guest_addr = (addr_t)info->vm_regs.rcx; - addr_t info_addr, route_addr, link_addr; - struct table_init_info *init_info; - struct link_entry *link_array; - struct routing_entry *route_array; - - if (guest_va_to_host_va(info, guest_addr, &info_addr) == -1) { - PrintError("VNET: Could not translate guest address\n"); - return -1; + /*initial routes table */ + for (i = 0; i < MAX_ROUTES; i++) { + vnet_state->g_routes.routes[i] = NULL; } - - init_info = (struct table_init_info *)info_addr; - - if (guest_va_to_host_va(info, init_info->routing_table_start, &route_addr) == -1) { - PrintError("VNET: Could not translate guest address\n"); - return -1; + vnet_state->g_routes.size = 0; + if(v3_lock_init(&(vnet_state->g_routes.lock)) == -1){ + PrintError("VNET: Failure to init lock for routes table\n"); } - route_array = (struct routing_entry *)route_addr; + PrintDebug("VNET: Routes table initiated\n"); - if (guest_va_to_host_va(info, init_info->link_table_start, &link_addr) == -1) { - PrintError("VNET: Could not translate guest address\n"); - return -1; - } - link_array = (struct link_entry *)link_addr; + /*initial pkt receiving queue */ + vnet_state->g_inpkt_q = v3_create_queue(); + v3_init_queue(vnet_state->g_inpkt_q); + PrintDebug("VNET: Receiving queue initiated\n"); - addto_routing_link_tables(route_array, init_info->routing_table_size, link_array, init_info->link_table_size); - - return 0; + /*initial routing cache */ + init_route_cache(vnet_state); } -void v3_vnet_init(struct guest_info * vm) { - init_tables(); - init_pkt_queue(); +int v3_init_vnet() { + vnet_state_init(&g_vnet_state); - v3_register_hypercall(vm, VNET_INITAB_HCALL, handle_init_tables_hcall, NULL); - PrintDebug("VNET Initialized\n"); -} + return 0; +}