2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2009, Lei Xia <lxia@northwestern.edu>
11 * Copyright (c) 2009, Yuan Tang <ytang@northwestern.edu>
12 * Copyright (c) 2009, Jack Lange <jarusl@cs.northwestern.edu>
13 * Copyright (c) 2009, Peter Dinda <pdinda@northwestern.edu>
14 * Copyright (c) 2009, The V3VEE Project <http://www.v3vee.org>
15 * All rights reserved.
17 * Author: Lei Xia <lxia@northwestern.edu>
18 * Yuan Tang <ytang@northwestern.edu>
19 * Jack Lange <jarusl@cs.northwestern.edu>
20 * Peter Dinda <pdinda@northwestern.edu
22 * This is free software. You are permitted to use,
23 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
26 #include <palacios/vmm_vnet.h>
28 static const char any_type_str[] = "any";
29 static const char not_type_str[] = "not";
30 static const char none_type_str[] = "none";
31 static const char empty_type_str[] = "empty";
33 static const char link_interface_str[] = "INTERFACE";
34 static const char link_edge_str[] = "EDGE";
35 static const char link_any_str[] = "ANY";
37 static const char link_prot_tcp_str[] = "tcp";
38 static const char link_prot_udp_str[] = "udp";
40 typedef enum {MAC_ANY, MAC_NOT, MAC_NONE, MAC_EMPTY} mac_type_t; //for 'src_mac_qual' and 'dst_mac_qual'
41 typedef enum {LINK_INTERFACE, LINK_EDGE, LINK_ANY} link_type_t; //for 'type' and 'src_type' in struct routing
42 typedef enum {TCP_TYPE, UDP_TYPE} link_prot_type_t;
44 static const uint_t hash_key_size = 16;
45 static SOCK g_udp_sockfd;
46 static struct gen_queue * g_inpkt_q;
47 static bool use_tcp = false;
48 static uint_t vnet_udp_port = 1022;
50 struct raw_ethernet_pkt {
52 int type; // vm or link type: INTERFACE|EDGE
53 char data[ETHERNET_PACKET_LEN];
56 //static char *vnet_version = "0.9";
57 //static int vnet_server = 0;
62 #define MAX_DEVICES 16
64 static struct topology g_links[MAX_LINKS];
65 static int g_num_links; //The current number of links
66 static int g_first_link;
67 static int g_last_link;
69 static struct routing g_routes[MAX_ROUTES];
70 static int g_num_routes; //The current number of routes
71 static int g_first_route;
72 static int g_last_route;
74 static struct device_list g_devices[MAX_DEVICES];
75 static int g_num_devices;
76 static int g_first_device;
77 static int g_last_device;
80 static void print_packet(char *pkt, int size) {
81 PrintDebug("Vnet: print_data_packet: size: %d\n", size);
82 v3_hexdump(pkt, size, NULL, 0);
86 static void print_packet_addr(char *pkt) {
87 PrintDebug("Vnet: print_packet_destination_addr: ");
88 v3_hexdump(pkt + 8, 6, NULL, 0);
90 PrintDebug("Vnet: print_packet_source_addr: ");
91 v3_hexdump(pkt + 14, 6, NULL, 0);
94 static void print_device_addr(char *ethaddr) {
95 PrintDebug("Vnet: print_device_addr: ");
96 v3_hexdump(ethaddr, 6, NULL, 0);
101 //network connection functions
102 static inline void raw_ethernet_packet_init(struct raw_ethernet_pkt * pt, const char * data, const size_t size) {
104 memcpy(pt->data, data, size);
111 * 6-11: dest_eth_addr
115 typedef char * route_hashkey_t;
117 // This is the hash value, Format: 0: num_matched_routes, 1...n: matches[] -- TY
118 struct route_cache_entry {
119 int num_matched_routes;
123 #define HASH_KEY_LEN 16
124 #define MIN_CACHE_SIZE 100
127 //Header of the route cache
128 static struct hashtable * g_route_cache;
130 static uint_t hash_from_key_fn(addr_t hashkey) {
131 uint8_t * key = (uint8_t *)hashkey;
132 return v3_hash_buffer(key, HASH_KEY_LEN);
135 static int hash_key_equal(addr_t key1, addr_t key2) {
136 uint8_t * buf1 = (uint8_t *)key1;
137 uint8_t * buf2 = (uint8_t *)key2;
138 return (memcmp(buf1, buf2, HASH_KEY_LEN) == 0);
141 static int init_route_cache() {
142 g_route_cache = v3_create_htable(MIN_CACHE_SIZE, &hash_from_key_fn, &hash_key_equal);
144 if (g_route_cache == NULL){
145 PrintError("Vnet: Route Cache Initiate Failurely\n");
152 static void make_hash_key(route_hashkey_t hashkey, char src_addr[6], char dest_addr[6], char src_type, int src_index) {
155 for (j = 0; j < 6; j++) {
156 hashkey[j] = src_addr[j];
157 hashkey[j + 6] = dest_addr[j] + 1;
160 hashkey[12] = src_type;
162 *(int *)(hashkey + 12) = src_index;
164 static int add_route_to_cache(route_hashkey_t hashkey, int num_matched_r, int * matches) {
165 struct route_cache_entry * new_entry = NULL;
168 new_entry = (struct route_cache_entry *)V3_Malloc(sizeof(struct route_cache_entry));
169 if (new_entry == NULL){
170 PrintError("Vnet: Malloc fails\n");
174 new_entry->num_matched_routes = num_matched_r;
176 new_entry->matches = (int *)V3_Malloc(num_matched_r * sizeof(int));
178 if (new_entry->matches == NULL){
179 PrintError("Vnet: Malloc fails\n");
183 for (i = 0; i < num_matched_r; i++) {
184 new_entry->matches[i] = matches[i];
187 //here, when v3_htable_insert return 0, it means insert fails
188 if (v3_htable_insert(g_route_cache, (addr_t)hashkey, (addr_t)new_entry) == 0){
189 PrintError("Vnet: Insert new route entry to cache failed\n");
190 V3_Free(new_entry->matches);
197 static int clear_hash_cache() {
198 v3_free_htable(g_route_cache, 1, 1);
200 g_route_cache = v3_create_htable(MIN_CACHE_SIZE, hash_from_key_fn, hash_key_equal);
202 if (g_route_cache == NULL){
203 PrintError("Vnet: Route Cache Create Failurely\n");
210 static int look_into_cache(route_hashkey_t hashkey, int * matches) {
213 struct route_cache_entry * found = NULL;
215 found = (struct route_cache_entry *)v3_htable_search(g_route_cache, (addr_t)hashkey);
218 n_matches = found->num_matched_routes;
220 for (i = 0; i < n_matches; i++) {
221 matches[i] = found->matches[i];
231 static inline uint8_t hex_nybble_to_nybble(const uint8_t hexnybble) {
232 uint8_t x = toupper(hexnybble);
237 return 10 + (x - 'A');
241 static inline uint8_t hex_byte_to_byte(const uint8_t hexbyte[2]) {
242 return ((hex_nybble_to_nybble(hexbyte[0]) << 4) +
243 (hex_nybble_to_nybble(hexbyte[1]) & 0xf));
247 static inline void string_to_mac(const char * str, uint8_t mac[6]) {
250 for (k = 0; k < 6; k++) {
251 mac[k] = hex_byte_to_byte(&(str[(2 * k) + k]));
255 static inline void mac_to_string(int mac[6], char * buf) {
256 snprintf(buf, 20, "%x:%x:%x:%x:%x:%x",
257 mac[0], mac[1], mac[2],
258 mac[3], mac[4], mac[5]);
262 static void ip_to_string(uint32_t addr, char * buf) {
266 addr_st = v3_htonl(addr);
267 tmp_str = v3_inet_ntoa(addr_st);
269 memcpy(buf, tmp_str, strlen(tmp_str));
273 int find_link_by_fd(SOCK sock) {
276 FOREACH_LINK(i, g_links, g_first_link) {
277 if (g_links[i].link_sock == sock) {
285 int vnet_add_link_entry(unsigned long dest, int type, int data_port, SOCK fd) {
288 for (i = 0; i < MAX_LINKS; i++) {
289 if (g_links[i].use == 0) {
290 g_links[i].dest = dest;
291 g_links[i].type = type;
292 g_links[i].link_sock = fd;
293 g_links[i].remote_port = data_port;
296 if (g_first_link == -1) {
300 g_links[i].prev = g_last_link;
301 g_links[i].next = -1;
303 if (g_last_link != -1) {
304 g_links[g_last_link].next = i;
319 int add_sock(struct sock_list *socks, int len, int *first_sock, int *last_sock, SOCK fd) {
322 for (i = 0; i < len; i++) {
323 if (socks[i].sock == -1) {
326 if (*first_sock == -1) {
330 socks[i].prev = *last_sock;
333 if (*last_sock != -1) {
334 socks[*last_sock].next = i;
346 int vnet_add_route_entry(char src_mac[6], char dest_mac[6], int src_mac_qual, int dest_mac_qual, int dest, int type, int src, int src_type) {
349 for(i = 0; i < MAX_ROUTES; i++) {
350 if (g_routes[i].use == 0) {
352 if ((src_mac_qual != MAC_ANY) && (src_mac_qual != MAC_NONE)) {
353 memcpy(g_routes[i].src_mac, src_mac, 6);
355 memset(g_routes[i].src_mac, 0, 6);
358 if ((dest_mac_qual != MAC_ANY) && (dest_mac_qual != MAC_NONE)) {
359 memcpy(g_routes[i].dest_mac, dest_mac, 6);
361 memset(g_routes[i].dest_mac, 0, 6);
364 g_routes[i].src_mac_qual = src_mac_qual;
365 g_routes[i].dest_mac_qual = dest_mac_qual;
366 g_routes[i].dest = dest;
367 g_routes[i].type = type;
368 g_routes[i].src = src;
369 g_routes[i].src_type = src_type;
372 if (g_first_route == -1) {
376 g_routes[i].prev = g_last_route;
377 g_routes[i].next = -1;
379 if (g_last_route != -1) {
380 g_routes[g_last_route].next = i;
397 static int find_link_entry(unsigned long dest, int type) {
400 FOREACH_LINK(i, g_links, g_first_link) {
401 if ((g_links[i].dest == dest) &&
402 ((type == -1) || (g_links[i].type == type)) ) {
410 static int delete_link_entry(int index) {
414 if (g_links[index].use == 0) {
418 g_links[index].dest = 0;
419 g_links[index].type = 0;
420 g_links[index].link_sock = -1;
421 g_links[index].use = 0;
423 prev_i = g_links[index].prev;
424 next_i = g_links[index].next;
427 g_links[prev_i].next = g_links[index].next;
431 g_links[next_i].prev = g_links[index].prev;
434 if (g_first_link == index) {
435 g_first_link = g_links[index].next;
438 if (g_last_link == index) {
439 g_last_link = g_links[index].prev;
442 g_links[index].next = -1;
443 g_links[index].prev = -1;
450 int vnet_delete_link_entry_by_addr(unsigned long dest, int type) {
451 int index = find_link_entry(dest, type);
457 return delete_link_entry(index);
461 static int find_route_entry(char src_mac[6],
470 char temp_src_mac[6];
471 char temp_dest_mac[6];
473 if ((src_mac_qual != MAC_ANY) && (src_mac_qual != MAC_NONE)) {
474 memcpy(temp_src_mac, src_mac, 6);
476 memset(temp_src_mac, 0, 6);
479 if ((dest_mac_qual != MAC_ANY) && (dest_mac_qual != MAC_NONE)) {
480 memcpy(temp_dest_mac, dest_mac, 6);
482 memset(temp_dest_mac, 0, 6);
485 FOREACH_LINK(i, g_routes, g_first_route) {
486 if ( (memcmp(temp_src_mac, g_routes[i].src_mac, 6) == 0) &&
487 (memcmp(temp_dest_mac, g_routes[i].dest_mac, 6) == 0) &&
488 (g_routes[i].src_mac_qual == src_mac_qual) &&
489 (g_routes[i].dest_mac_qual == dest_mac_qual) &&
491 ( (type == g_routes[i].type) && (g_routes[i].dest == dest)) ) &&
492 ( (src_type == -1) ||
493 ( (src_type == g_routes[i].src_type) && (g_routes[i].src == src)) ) ) {
501 static int delete_route_entry(int index) {
505 memset(g_routes[index].src_mac, 0, 6);
506 memset(g_routes[index].dest_mac, 0, 6);
508 g_routes[index].dest = 0;
509 g_routes[index].src = 0;
510 g_routes[index].src_mac_qual = 0;
511 g_routes[index].dest_mac_qual = 0;
512 g_routes[index].type = -1;
513 g_routes[index].src_type = -1;
514 g_routes[index].use = 0;
516 prev_i = g_routes[index].prev;
517 next_i = g_routes[index].next;
520 g_routes[prev_i].next = g_routes[index].next;
524 g_routes[next_i].prev = g_routes[index].prev;
527 if (g_first_route == index) {
528 g_first_route = g_routes[index].next;
531 if (g_last_route == index) {
532 g_last_route = g_routes[index].prev;
535 g_routes[index].next = -1;
536 g_routes[index].prev = -1;
545 int vnet_delete_route_entry_by_addr(char src_mac[6],
553 int index = find_route_entry(src_mac, dest_mac, src_mac_qual,
554 dest_mac_qual, dest, type, src, src_type);
560 delete_route_entry(index);
565 int delete_sock(struct sock_list * socks, int * first_sock, int * last_sock, SOCK fd) {
571 FOREACH_SOCK(i, socks, (*first_sock)) {
572 if (socks[i].sock == fd) {
573 V3_Close_Socket(socks[i].sock);
576 prev_i = socks[i].prev;
577 next_i = socks[i].next;
580 socks[prev_i].next = socks[i].next;
584 socks[next_i].prev = socks[i].prev;
587 if (*first_sock == i) {
588 *first_sock = socks[i].next;
591 if (*last_sock == i) {
592 *last_sock = socks[i].prev;
604 //setup the topology of the testing network
605 static void store_topologies(SOCK fd) {
607 int src_mac_qual = MAC_ANY;
608 int dest_mac_qual = MAC_ANY;
611 dest = (0 | 172 << 24 | 23 << 16 | 1 );
612 PrintDebug("VNET: store_topologies. NOT VNET_SERVER, dest = %x\n", dest);
614 dest = (0 | 172 << 24 | 23 << 16 | 2 );
615 PrintDebug("VNET: store_topologies. VNET_SERVER, dest = %x\n", dest);
620 int src_type= LINK_ANY; //ANY_SRC_TYPE
624 for (i = 0; i < MAX_LINKS; i++) {
625 if (g_links[i].use == 0) {
626 g_links[i].dest = (int)dest;
627 g_links[i].type = type;
628 g_links[i].link_sock = fd;
629 g_links[i].remote_port = data_port;
632 if (g_first_link == -1) {
636 g_links[i].prev = g_last_link;
637 g_links[i].next = -1;
639 if (g_last_link != -1) {
640 g_links[g_last_link].next = i;
646 PrintDebug("VNET: store_topologies. new link: socket: %d, remote %x:[%d]\n", g_links[i].link_sock, (uint_t)g_links[i].dest, g_links[i].remote_port);
656 for (i = 0; i < MAX_ROUTES; i++) {
657 if (g_routes[i].use == 0) {
658 if ((src_mac_qual != MAC_ANY) && (src_mac_qual != MAC_NONE)) {
659 // memcpy(g_routes[i].src_mac, src_mac, 6);
661 memset(g_routes[i].src_mac, 0, 6);
664 if ((dest_mac_qual != MAC_ANY) && (dest_mac_qual != MAC_NONE)) {
665 // memcpy(g_routes[i].dest_mac, dest_mac, 6);
667 memset(g_routes[i].dest_mac, 0, 6);
670 g_routes[i].src_mac_qual = src_mac_qual;
671 g_routes[i].dest_mac_qual = dest_mac_qual;
672 g_routes[i].dest = (int)dest;
673 g_routes[i].type = type;
674 g_routes[i].src = src;
675 g_routes[i].src_type = src_type;
679 if (g_first_route == -1) {
683 g_routes[i].prev = g_last_route;
684 g_routes[i].next = -1;
686 if (g_last_route != -1) {
687 g_routes[g_last_route].next = i;
694 PrintDebug("VNET: store_topologies. new route: src_mac: %s, dest_mac: %s, dest: %d\n", g_routes[i].src_mac, g_routes[i].dest_mac, dest);
700 static int match_route(uint8_t * src_mac, uint8_t * dst_mac, int src_type, int src_index, int * matches) {
701 int values[MAX_ROUTES];
702 int matched_routes[MAX_ROUTES];
710 FOREACH_ROUTE(i, g_routes, g_first_route) {
711 if ((g_routes[i].src_type != LINK_ANY) &&
712 ((g_routes[i].src_type != src_type) ||
713 ((g_routes[i].src != src_index) &&
714 (g_routes[i].src != -1)))) {
715 PrintDebug("Vnet: MatchRoute: Source route is on and does not match\n");
719 if ( (g_routes[i].dest_mac_qual == MAC_ANY) &&
720 (g_routes[i].src_mac_qual == MAC_ANY) ) {
721 matched_routes[num_matches] = i;
722 values[num_matches] = 3;
726 if (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0) {
727 if (g_routes[i].src_mac_qual != MAC_NOT) {
728 if (g_routes[i].dest_mac_qual == MAC_ANY) {
729 matched_routes[num_matches] = i;
730 values[num_matches] = 6;
733 } else if (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) == 0) {
734 if (g_routes[i].dest_mac_qual != MAC_NOT) {
735 matched_routes[num_matches] = i;
736 values[num_matches] = 8;
744 if (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) == 0) {
745 if (g_routes[i].dest_mac_qual != MAC_NOT) {
746 if (g_routes[i].src_mac_qual == MAC_ANY) {
747 matched_routes[num_matches] = i;
748 values[num_matches] = 6;
751 } else if (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0) {
752 if (g_routes[i].src_mac_qual != MAC_NOT) {
753 if (exact_match == 0) {
754 matched_routes[num_matches] = i;
755 values[num_matches] = 8;
763 if ((g_routes[i].dest_mac_qual == MAC_NOT) &&
764 (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) != 0)) {
765 if (g_routes[i].src_mac_qual == MAC_ANY) {
766 matched_routes[num_matches] = i;
767 values[num_matches] = 5;
769 } else if (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0) {
770 if (g_routes[i].src_mac_qual != MAC_NOT) {
771 matched_routes[num_matches] = i;
772 values[num_matches] = 7;
778 if ((g_routes[i].src_mac_qual == MAC_NOT) &&
779 (memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) != 0)) {
780 if (g_routes[i].dest_mac_qual == MAC_ANY) {
781 matched_routes[num_matches] = i;
782 values[num_matches] = 5;
784 } else if (memcmp((void *)&g_routes[i].dest_mac, (void *)dst_mac, 6) == 0) {
785 if (g_routes[i].dest_mac_qual != MAC_NOT) {
786 matched_routes[num_matches] = i;
787 values[num_matches] = 7;
795 FOREACH_ROUTE(i, g_routes, g_first_route) {
796 if ((memcmp((void *)&g_routes[i].src_mac, (void *)src_mac, 6) == 0) &&
797 (g_routes[i].dest_mac_qual == MAC_NONE) &&
798 ((g_routes[i].src_type == LINK_ANY) ||
799 ((g_routes[i].src_type == src_type) &&
800 ((g_routes[i].src == src_index) ||
801 (g_routes[i].src == -1))))) {
802 matched_routes[num_matches] = i;
803 values[num_matches] = 4;
804 PrintDebug("Vnet: MatchRoute: We matched a default route (%d)\n", i);
809 //If many rules have been matched, we choose one which has the highest value rating
810 if (num_matches == 0) {
814 for (i = 0; i < num_matches; i++) {
815 if (values[i] > max) {
818 matches[no] = matched_routes[i];
820 } else if (values[i] == max) {
821 matches[no] = matched_routes[i];
830 static int process_udpdata() {
831 struct raw_ethernet_pkt * pt;
834 uint16_t remote_port = 0;
835 SOCK link_sock = g_udp_sockfd;
836 int length = sizeof(struct raw_ethernet_pkt) - (2 * sizeof(int)); //minus the "size" and "type"
838 //run in a loop to get packets from outside network, adding them to the incoming packet queue
840 pt = (struct raw_ethernet_pkt *)V3_Malloc(sizeof(struct raw_ethernet_pkt));
843 PrintError("Vnet: process_udp: Malloc fails\n");
847 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);
848 pt->size = V3_RecvFrom_IP( link_sock, dest, remote_port, pt->data, length);
849 PrintDebug("Vnet: route_thread: socket: [%d] receive from ip [%x], port [%d]\n", link_sock, (uint_t)dest, remote_port);
852 PrintDebug("Vnet: process_udp: receiving packet from UDP fails\n");
857 PrintDebug("Vnet: process_udp: get packet\n");
858 print_packet(pt->data, pt->size);
867 static int indata_handler( )
875 static int start_recv_data()
880 SOCK udp_data_socket;
882 if ((udp_data_socket = V3_Create_UDP_Socket()) < 0){
883 PrintError("VNET: Can't setup udp socket\n");
886 PrintDebug("Vnet: vnet_setup_udp: get socket: %d\n", udp_data_socket);
887 g_udp_sockfd = udp_data_socket;
889 store_topologies(udp_data_socket);
891 if (V3_Bind_Socket(udp_data_socket, vnet_udp_port) < 0){
892 PrintError("VNET: Can't bind socket\n");
895 PrintDebug("VNET: vnet_setup_udp: bind socket successful\n");
898 V3_CREATE_THREAD(&indata_handler, NULL, "VNET_DATA_HANDLER");
903 static inline int if_write_pkt(struct vnet_if_device *iface, struct raw_ethernet_pkt * pkt) {
904 return iface->input((uchar_t *)pkt->data, pkt->size);
907 static int handle_one_pkt(struct raw_ethernet_pkt * pkt) {
908 int src_link_index = 0; //the value of src_link_index of udp always is 0
913 int matches[g_num_routes];
914 int num_matched_routes = 0;
916 struct HEADERS headers;
918 // get the ethernet and ip headers from the packet
919 memcpy((void *)&headers, (void *)pkt->data, sizeof(headers));
922 for (j = 0;j < 6; j++) {
923 src_mac[j] = headers.ethernetsrc[j];
924 dst_mac[j] = headers.ethernetdest[j];
932 mac_to_string(src_mac, src_str);
933 mac_to_string(dst_mac, dest_str);
935 PrintDebug("Vnet: HandleDataOverLink. SRC(%s), DEST(%s)\n", src_str, dest_str);
938 char hash_key[hash_key_size];
939 make_hash_key(hash_key, src_mac, dst_mac, LINK_EDGE, src_link_index);//link_edge -> pt->type???
941 num_matched_routes = look_into_cache((route_hashkey_t)hash_key, matches);
943 if (num_matched_routes == -1) { //no match
944 num_matched_routes = match_route(src_mac, dst_mac, pkt->type, src_link_index, matches);
946 if (num_matched_routes > 0) {
947 add_route_to_cache(hash_key, num_matched_routes,matches);
951 PrintDebug("Vnet: HandleDataOverLink: Matches=%d\n", num_matched_routes);
953 for (i = 0; i < num_matched_routes; i++) {
954 int route_index = -1;
958 route_index = matches[i];
960 PrintDebug("Vnet: HandleDataOverLink: Forward packet from link according to Route entry %d\n", route_index);
962 if (g_routes[route_index].type == LINK_EDGE) {
963 link_index = g_routes[route_index].dest;
965 if(g_links[link_index].type == UDP_TYPE) {
969 PrintDebug("===Vnet: HandleDataOverLink: Serializing UDP Packet to link_sock [%d], dest [%x], remote_port [%d], size [%d]\n", g_links[link_index].link_sock, (uint_t)g_links[link_index].dest, g_links[link_index].remote_port, (int)pkt->size);
971 if ((size = V3_SendTo_IP(g_links[link_index].link_sock, g_links[link_index].dest, g_links[link_index].remote_port, pkt->data, pkt->size)) != pkt->size) {
972 PrintError("Vnet: sending by UDP Exception, %x\n", size);
976 PrintDebug("Vnet: HandleDataOverLink: Serializing UDP Packet to link_sock [%d], dest [%x], remote_port [%d], size [%d]\n", g_links[link_index].link_sock, (uint_t)g_links[link_index].dest, g_links[link_index].remote_port, (int)pkt->size);
978 } else if (g_links[link_index].type == TCP_TYPE) {
981 } else if (g_routes[route_index].type == LINK_INTERFACE) {
982 dev_index = g_routes[route_index].dest;
984 PrintDebug("Writing Packet to device=%s\n", g_devices[dev_index].device->name);
986 if (if_write_pkt(g_devices[dev_index].device, pkt) == -1) {
987 PrintDebug("Can't write output packet to link\n");
991 PrintDebug("Vnet: Wrong Edge type\n");
998 static int send_ethernet_pkt(char * buf, int length) {
999 struct raw_ethernet_pkt * pt;
1001 pt = (struct raw_ethernet_pkt *)V3_Malloc(sizeof(struct raw_ethernet_pkt));
1002 raw_ethernet_packet_init(pt, buf, length); //====here we copy sending data once
1004 PrintDebug("VNET: vm_send_pkt: transmitting packet: (size:%d)\n", (int)pt->size);
1005 print_packet((char *)buf, length);
1007 v3_enqueue(g_inpkt_q, (addr_t)pt);
1012 int v3_Send_pkt(uchar_t *buf, int length) {
1013 PrintDebug("VNET: In V3_Send_pkt: pkt length %d\n", length);
1015 return send_ethernet_pkt((char *)buf, length);
1018 static int add_device_to_table(struct vnet_if_device*device, int type) {
1021 for (i = 0; i < MAX_DEVICES; i++) {
1022 if (g_devices[i].use == 0) {
1023 g_devices[i].type = type;
1024 g_devices[i].use = 1;
1026 if (g_first_device == -1) {
1030 g_devices[i].prev = g_last_device;
1031 g_devices[i].next = -1;
1033 if (g_last_device != -1) {
1034 g_devices[g_last_device].next = i;
1047 static int search_device(char *device_name) {
1050 for (i = 0; i < MAX_DEVICES; i++) {
1051 if (g_devices[i].use == 1) {
1052 if (!strcmp(device_name, g_devices[i].device->name)) {
1061 static struct vnet_if_device * delete_device_from_table(int index) {
1064 struct vnet_if_device * device = NULL;
1066 if (g_devices[index].use == 0) {
1070 g_devices[index].use = 0;
1072 prev_i = g_devices[index].prev;
1073 next_i = g_devices[index].next;
1076 g_devices[prev_i].next = g_devices[index].next;
1080 g_devices[next_i].prev = g_devices[index].prev;
1083 if (g_first_device == index) {
1084 g_first_device = g_devices[index].next;
1087 if (g_last_device == index) {
1088 g_last_device = g_devices[index].prev;
1091 g_devices[index].next = -1;
1092 g_devices[index].prev = -1;
1094 device = g_devices[index].device;
1095 g_devices[index].device = NULL;
1103 int vnet_register_device(char * dev_name, int (*netif_input)(uchar_t * pkt, uint_t size), void * data) {
1104 struct vnet_if_device * dev;
1106 dev = (struct vnet_if_device *)V3_Malloc(sizeof(struct vnet_if_device));
1109 PrintError("VNET: Malloc fails\n");
1113 strncpy(dev->name, dev_name, 50);
1114 dev->input = netif_input;
1117 if (add_device_to_table(dev, GENERAL_NIC) == -1) {
1124 int vnet_unregister_device(char * dev_name) {
1127 i = search_device(dev_name);
1133 struct vnet_if_device * device = delete_device_from_table(i);
1135 if (device == NULL) {
1144 int v3_Register_pkt_event(int (*netif_input)(uchar_t * pkt, uint_t size)) {
1145 return vnet_register_device("NE2000", netif_input, NULL);
1148 int v3_vnet_pkt_process() {
1149 struct raw_ethernet_pkt * pt;
1151 PrintDebug("VNET: In vnet_check\n");
1153 while ((pt = (struct raw_ethernet_pkt *)v3_dequeue(g_inpkt_q)) != NULL) {
1154 PrintDebug("VNET: In vnet_check: pt length %d, pt type %d\n", (int)pt->size, (int)pt->type);
1155 v3_hexdump(pt->data, pt->size, NULL, 0);
1157 if(handle_one_pkt(pt)) {
1158 PrintDebug("VNET: vnet_check: handle one packet!\n");
1161 V3_Free(pt); //be careful here
1167 static void init_link_table() {
1170 for (i = 0; i < MAX_LINKS; i++) {
1172 g_links[i].next = -1;
1173 g_links[i].prev = -1;
1181 static void init_device_table() {
1184 for (i = 0; i < MAX_DEVICES; i++) {
1185 g_devices[i].use = 0;
1186 g_devices[i].next = -1;
1187 g_devices[i].prev = -1;
1190 g_first_device = -1;
1195 static void init_route_table() {
1198 for (i = 0; i < MAX_ROUTES; i++) {
1199 g_routes[i].use = 0;
1200 g_routes[i].next = -1;
1201 g_routes[i].prev = -1;
1209 static void init_tables() {
1211 init_device_table();
1216 static void init_pkt_queue()
1218 PrintDebug("VNET Init package receiving queue\n");
1220 g_inpkt_q = v3_create_queue();
1221 v3_init_queue(g_inpkt_q);
1226 void v3_vnet_init() {
1228 PrintDebug("VNET Init: Vnet input queue successful.\n");
1234 //store_topologies(udp_data_socket);