X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?a=blobdiff_plain;f=palacios%2Fsrc%2Fpalacios%2Fvmm_vnet.c;h=d2daa7e9f98e566fde9c88cdfca5993956d39c4b;hb=87b1679efe8bee960b65cfd730188ccc94966b71;hp=72d4049572c2456218b5dd7bbcd9cc45cb9a8984;hpb=9e9d28b5fe29fcf8fbed010c981254b5e25a6eac;p=palacios.git diff --git a/palacios/src/palacios/vmm_vnet.c b/palacios/src/palacios/vmm_vnet.c index 72d4049..d2daa7e 100644 --- a/palacios/src/palacios/vmm_vnet.c +++ b/palacios/src/palacios/vmm_vnet.c @@ -34,19 +34,20 @@ struct ethernet_pkt { - uint32_t size; + uint32_t size; //size of data uint16_t type; + struct udp_link_header ext_hdr; //possible externel header to applied to data before sent char data[ETHERNET_PACKET_LEN]; -}; +}__attribute__((packed)); // 14 (ethernet frame) + 20 bytes struct in_pkt_header { - char ethernetdest[6]; - char ethernetsrc[6]; - unsigned char ethernettype[2]; // indicates 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 @@ -59,12 +60,12 @@ static const uint_t hash_key_size = 16; struct link_table { struct link_entry * links[MAX_LINKS]; uint16_t size; -}; +}__attribute__((packed)); struct routing_table { struct routing_entry * routes[MAX_ROUTES]; uint16_t size; -}; +}__attribute__((packed)); static struct link_table g_links; static struct routing_table g_routes; @@ -87,30 +88,27 @@ struct route_cache_entry { // the route cache static struct hashtable * g_route_cache; +static uint16_t ip_xsum(struct ip_header *ip_hdr, int hdr_len){ + long sum = 0; -static void print_packet(char * pkt, int size) { - PrintDebug("Vnet: print_data_packet: size: %d\n", size); - v3_hexdump(pkt, size, NULL, 0); -} + uint16_t *p = (uint16_t*) ip_hdr; + while(hdr_len > 1){ + sum += *(p++); + if(sum & 0x80000000) + sum = (sum & 0xFFFF) + (sum >> 16); + hdr_len -= 2; + } + + if(hdr_len) + sum += (uint16_t) *(uchar_t *)p; + + while(sum>>16) + sum = (sum & 0xFFFF) + (sum >> 16); -#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); + return (uint16_t)~sum; } -static void print_device_addr(char * ethaddr) { - PrintDebug("Vnet: print_device_addr: "); - v3_hexdump(ethaddr, 6, NULL, 0); -} -#endif - - -// network connection functions -static inline void ethernet_packet_init(struct ethernet_pkt * pt, const char * data, const size_t size) { +static inline void ethernet_packet_init(struct ethernet_pkt *pt, uchar_t *data, const size_t size) { pt->size = size; memcpy(pt->data, data, size); } @@ -218,6 +216,13 @@ 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: print_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); @@ -242,12 +247,38 @@ 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 int add_link_entry(struct link_entry * link) { +static void dump_routes(struct routing_entry **route_tables) { + char dest_str[18]; + char src_str[18]; + struct routing_entry *route = NULL; + int i; + + PrintDebug("\nVnet: route table dump start =====\n"); + + for(i = 0; i < MAX_ROUTES; i++) { + if (route_tables[i] != NULL){ + route = route_tables[i]; + + mac_to_string(route->src_mac, src_str); + mac_to_string(route->dest_mac, dest_str); + + PrintDebug("route: %d\n", i); + 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); + } + } + + PrintDebug("\nVnet: route table dump end =====\n"); +} + +#endif + +static int __add_link_entry(struct link_entry * link) { int idx; for (idx = 0; idx < MAX_LINKS; idx++) { @@ -263,7 +294,7 @@ static int add_link_entry(struct link_entry * link) { return -1; } -static int add_route_entry(struct routing_entry * route) { +static int __add_route_entry(struct routing_entry * route) { int idx; for (idx = 0; idx < MAX_ROUTES; idx++) { @@ -280,24 +311,24 @@ static int add_route_entry(struct routing_entry * route) { } -static int vnet_add_route_entry(char src_mac[6], +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 src_link_idx, + link_type_t src_link_type) { struct routing_entry * new_route = (struct routing_entry *)V3_Malloc(sizeof(struct routing_entry)); int idx = -1; memset(new_route, 0, sizeof(struct routing_entry)); - if ((src_mac_qual != MAC_ANY) && (src_mac_qual != MAC_NONE)) { + if ((src_mac_qual != MAC_ANY)) { memcpy(new_route->src_mac, src_mac, 6); } - if ((dest_mac_qual != MAC_ANY) && (dest_mac_qual != MAC_NONE)) { + if ((dest_mac_qual != MAC_ANY)) { memcpy(new_route->dest_mac, dest_mac, 6); } @@ -305,10 +336,10 @@ static int vnet_add_route_entry(char src_mac[6], 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_link_idx = src_link_idx; + new_route->src_type = src_link_type; - if ((idx = add_route_entry(new_route)) == -1) { + if ((idx = __add_route_entry(new_route)) == -1) { PrintError("Could not add route entry\n"); return -1; } @@ -318,7 +349,8 @@ static int vnet_add_route_entry(char src_mac[6], return idx; } -static void * delete_link_entry(int index) { +#if 0 +static void * __delete_link_entry(int index) { struct link_entry * link = NULL; void * ret = NULL; link_type_t type; @@ -386,11 +418,11 @@ static int find_route_entry(char src_mac[6], return -1; } -static int delete_route_entry(int index) { +static int __delete_route_entry(int index) { struct routing_entry * route; if ((index >= MAX_ROUTES) || (g_routes.routes[index] == NULL)) { - PrintError("Could not find route entry %d\n", index); + PrintDebug("VNET: wrong index in delete route entry %d\n", index); return -1; } @@ -417,244 +449,110 @@ static int vnet_delete_route_entry_by_addr(char src_mac[6], dest_mac_qual, link_idx, type, src, src_type); if (index == -1) { - PrintError("Error deleting route entry by addr\n"); + PrintDebug("VNET: wrong in delete route entry %d\n", index); return -1; } - return delete_route_entry(index); + return __delete_route_entry(index); } +#endif 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) { int matched_routes[MAX_ROUTES]; - int num_matches = 0; int i; - int max = 0; - int no = 0; - int exact_match = 0; - - 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 ( (g_routes.routes[i]->dest_mac_qual == MAC_ANY) && - (g_routes.routes[i]->src_mac_qual == MAC_ANY)) { + struct routing_entry *route = NULL; - 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 ( (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; - } +#ifdef CONFIG_DEBUG_VNET + char dest_str[18]; + char src_str[18]; - 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++; - } - } + mac_to_string(src_mac, src_str); + mac_to_string(dst_mac, dest_str); - return no; -} - -#if 0 -// TODO: To be removed -static int process_udpdata() -{ - struct ethernet_pkt * pt; - - uint32_t dest = 0; - uint16_t remote_port = 0; - SOCK link_sock = g_udp_sockfd; - int length = sizeof(struct ethernet_pkt) - (2 * sizeof(int)); //minus the "size" and "type" - - //run in a loop to get packets from outside network, adding them to the incoming packet queue - while (1) { - pt = (struct ethernet_pkt *)V3_Malloc(sizeof(struct ethernet_pkt)); - - if (pt == NULL){ - PrintError("Vnet: process_udp: Malloc fails\n"); - continue; - } - - PrintDebug("Vnet: route_thread: socket: [%d]. ready to receive from ip [%x], port [%d] or from VMs\n", link_sock, (uint_t)dest, remote_port); - pt->size = V3_RecvFrom_IP( link_sock, dest, remote_port, pt->data, length); - PrintDebug("Vnet: route_thread: socket: [%d] receive from ip [%x], port [%d]\n", link_sock, (uint_t)dest, remote_port); - - if (pt->size <= 0) { - PrintDebug("Vnet: process_udp: receiving packet from UDP fails\n"); - V3_Free(pt); - return -1; - } - - PrintDebug("Vnet: process_udp: get packet\n"); - print_packet(pt->data, pt->size); - } -} - -// TODO: To be removed -static int indata_handler( ) -{ - if (!use_tcp) { - process_udpdata(); - } + PrintDebug("Vnet: match_route. pkt: SRC(%s), DEST(%s)\n", src_str, dest_str); - return 0; -} + dump_routes(g_routes.routes); +#endif -// TODO: To be removed -static int start_recv_data() -{ - if (use_tcp){ - - } else { - SOCK udp_data_socket; - - if ((udp_data_socket = V3_Create_UDP_Socket()) < 0){ - PrintError("VNET: Can't setup udp socket\n"); - return -1; - } - PrintDebug("Vnet: vnet_setup_udp: get socket: %d\n", udp_data_socket); - g_udp_sockfd = udp_data_socket; + for(i = 0; i < MAX_ROUTES; i++) { + if (g_routes.routes[i] != NULL){ + route = 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++; + } - store_topologies(udp_data_socket); + 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++; + } + } + } + + 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 (V3_Bind_Socket(udp_data_socket, vnet_udp_port) < 0){ - PrintError("VNET: Can't bind socket\n"); - return -1; - } - PrintDebug("VNET: vnet_setup_udp: bind socket successful\n"); + for (i = 0; i < num_matches; i++) { + matches[i] = matched_routes[i]; } - V3_CREATE_THREAD(&indata_handler, NULL, "VNET_DATA_HANDLER"); - return 0; -} - -static void store_test_topologies(SOCK fd) -{ - int i; - int src_mac_qual = MAC_ANY; - int dest_mac_qual = MAC_ANY; - uint_t dest; -#ifndef VNET_SERVER - dest = (0 | 172 << 24 | 23 << 16 | 1 ); - PrintDebug("VNET: store_topologies. NOT VNET_SERVER, dest = %x\n", dest); -#else - dest = (0 | 172 << 24 | 23 << 16 | 2 ); - PrintDebug("VNET: store_topologies. VNET_SERVER, dest = %x\n", dest); -#endif - - int type = UDP_TYPE; - int src = 0; - int src_type= LINK_ANY; //ANY_SRC_TYPE - int data_port = 22; + return num_matches; } -#endif - -static int handle_one_pkt(struct ethernet_pkt * pkt) { +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]; @@ -687,81 +585,114 @@ static int handle_one_pkt(struct ethernet_pkt * pkt) { num_matched_routes = look_into_cache((route_hashkey_t)hash_key, matches); - if (num_matched_routes == -1) { - // no match - + // no match in the cache 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); - } + 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++) { + for (i = 0; i < num_matched_routes; i++) {//send packet to all destinations int route_index = -1; int link_index = -1; - int pkt_len = 0; - struct link_entry * link = NULL; + int pkt_len = 0; + 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 = g_routes.routes[route_index]->link_idx; - if ( (link_index < 0) || - (link_index > MAX_LINKS) || + if ((link_index < 0) || (link_index > MAX_LINKS) || (g_links.links[link_index] == NULL)) { - continue; - } - - link = g_links.links[link_index]; - + continue; + } - pkt_len = pkt->size; - - if (g_routes.routes[route_index]->link_type == LINK_EDGE) { - - // TODO: apply the header in the beginning of the packet to be sent - if ((link->dst_link->input(pkt->data, pkt_len, NULL)) != pkt_len) { - PrintError("LEI -- WHAT IS THE ERROR HERE?\n"); - return -1; - } - } else if (g_routes.routes[route_index]->link_type == LINK_INTERFACE) { - if ((link->dst_link->input(pkt->data, pkt_len, NULL)) != pkt_len) { - PrintError("LEI -- WHAT IS THE ERROR HERE?\n"); - return -1; - } + link = g_links.links[link_index]; + pkt_len = pkt->size; + 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) { + struct udp_link_header *hdr = &(link->dst_link->vnet_header); + struct ip_header *ip = &hdr->ip_hdr; + struct udp_header *udp = &hdr->udp_hdr; + udp->len = pkt_len + sizeof(struct udp_header); + ip->total_len = pkt_len + sizeof(struct udp_header) + sizeof(struct ip_header); + ip->cksum = ip_xsum(ip, sizeof(struct ip_header)); + + int hdr_size = sizeof(struct udp_link_header); + memcpy(&pkt->ext_hdr, hdr, hdr_size); + + 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 to link: %d\n", link_index); + continue; + } + }else { + PrintDebug("VNET: Link protocol type not support\n"); + continue; + } + } else if (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 to link: %d\n", link_index); + continue; + } } else { - PrintError("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(char * buf, int length) { - struct ethernet_pkt * pkt; +static int send_ethernet_pkt(uchar_t *data, int len) { + struct ethernet_pkt *pkt; pkt = (struct ethernet_pkt *)V3_Malloc(sizeof(struct ethernet_pkt)); - ethernet_packet_init(pkt, buf, length); //====here we copy sending data once + 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); - print_packet((char *)buf, length); + +#ifdef CONFIG_DEBUG_VNET + print_packet((char *)data, len); +#endif v3_enqueue(g_inpkt_q, (addr_t)pkt); return 0; } -int v3_vnet_send_pkt(uchar_t * buf, int length) { - PrintDebug("VNET: In V3_Send_pkt: pkt length %d\n", length); +//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); - return send_ethernet_pkt((char *)buf, length); + return send_ethernet_pkt(buf, len); +} + +//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); + + uint_t hdr_len = sizeof(struct udp_link_header); + + return send_ethernet_pkt((uchar_t *)(buf+hdr_len), len - hdr_len); } static int search_device(char * device_name) { @@ -774,12 +705,11 @@ static int search_device(char * device_name) { } } } - - PrintError("LEI -- WHAT IS THE ERROR HERE?\n"); + return -1; } -static int vnet_register_device(struct vm_device * vdev, +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), @@ -787,14 +717,12 @@ static int vnet_register_device(struct vm_device * vdev, struct vnet_if_device * if_dev; int idx = search_device(dev_name); - if (idx != -1) { - PrintError("LEI -- WHAT IS THE ERROR HERE?\n"); + PrintDebug("VNET: register device: Already has device with the name %s\n", dev_name); return -1; } - if_dev = (struct vnet_if_device *)V3_Malloc(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; @@ -807,28 +735,28 @@ static int vnet_register_device(struct vm_device * vdev, if_dev->private_data = data; 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); - + idx = __add_link_entry(link); + return idx; } +#if 0 static int vnet_unregister_device(char * dev_name) { int idx; idx = search_device(dev_name); if (idx == -1) { - PrintError("LEI -- WHAT IS THE ERROR HERE?\n"); + 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); + struct vnet_if_device * device = (struct vnet_if_device *)__delete_link_entry(idx); if (device == NULL) { - PrintError("LEI -- WHAT IS THE ERROR HERE?\n"); + PrintError("VNET: Device %s not in the link table %d, something may be wrong in link table\n", dev_name, idx); return -1; } @@ -837,28 +765,26 @@ static int vnet_unregister_device(char * dev_name) { return idx; } +#endif + int v3_vnet_pkt_process() { struct ethernet_pkt * pkt; - 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"); - } + PrintDebug("VNET: In vnet_check: pt length %d, pt type %d\n", (int)pkt->size, (int)pkt->type); + + if (handle_one_pkt(pkt) != -1) { + PrintDebug("VNET: vnet_check: handle one packet!\n"); + } 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() { int i; @@ -892,27 +818,117 @@ static void init_pkt_queue() { v3_init_queue(g_inpkt_q); } -#if 0 -// TODO: -static int init_routing_tables(struct routing_entry *route_tab, uint16_t size) -{ - //struct routing_entry *route; +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)); - return 0; -} + 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]; -// TODO: -static int init_link_tables(struct link_entry *link_tab, uint16_t size) -{ - //struct link_entry *link; + if((__add_route_entry(new_route)) == -1){ + PrintDebug("VNET: Adding route fails"); + V3_Free(new_route); + } + new_route = NULL; + } return 0; } -#endif - struct table_init_info { addr_t routing_table_start; uint16_t routing_table_size; @@ -920,29 +936,45 @@ struct table_init_info { 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) { - uint8_t * buf = NULL; - addr_t info_addr = (addr_t)info->vm_regs.rcx; + 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; + + PrintDebug("Vnet: In handle_init_tables_hcall\n"); + + if (guest_va_to_host_va(info, guest_addr, &info_addr) == -1) { + PrintError("VNET: Could not translate guest address\n"); + return -1; + } + init_info = (struct table_init_info *)info_addr; - if (guest_va_to_host_va(info, info_addr, (addr_t *)&(buf)) == -1) { - PrintError("Could not translate buffer address\n"); + 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; } + route_array = (struct routing_entry *)route_addr; + + 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; - //struct table_init_info *init_info = (struct table_init_info *)buf; + addto_routing_link_tables(route_array, init_info->routing_table_size, link_array, init_info->link_table_size); return 0; } - void v3_vnet_init(struct guest_info * vm) { init_tables(); init_pkt_queue(); v3_register_hypercall(vm, VNET_INITAB_HCALL, handle_init_tables_hcall, NULL); - //store_test_topologies(udp_data_socket); - PrintDebug("VNET Initialized\n"); }