2 Palacios VNET Host Bridge
5 #include <linux/spinlock.h>
6 #include <linux/seq_file.h>
7 #include <linux/proc_fs.h>
8 #include <asm/uaccess.h>
9 #include <linux/inet.h>
10 #include <linux/kthread.h>
12 #include <linux/netdevice.h>
15 #include <linux/net.h>
16 #include <linux/string.h>
17 #include <linux/preempt.h>
18 #include <linux/sched.h>
21 #include <vnet/vnet.h>
22 #include <vnet/vnet_hashtable.h>
23 #include "palacios-vnet.h"
25 #define VNET_SERVER_PORT 9000
27 struct palacios_vnet_route {
28 struct v3_vnet_route route;
32 struct list_head node;
35 typedef enum {UDP, TCP, RAW, NONE} vnet_bridge_proto_t;
42 struct sockaddr_in sock_addr;
43 vnet_bridge_proto_t sock_proto;
45 unsigned int pkt_rx, pkt_tx;
46 uint64_t bytes_rx, bytes_tx;
50 struct list_head node;
54 struct palacios_vnet_state {
60 struct list_head route_list;
61 struct list_head link_list;
63 struct hashtable *ip2link; /* map IP to its Link */
67 struct socket * serv_sock;
68 struct sockaddr_in serv_addr;
69 vnet_bridge_proto_t serv_proto;
71 /* Socket Receiving Thread */
72 struct task_struct * serv_thread;
74 unsigned int pkt_from_vmm, pkt_to_vmm, pkt_drop_vmm;
75 unsigned int pkt_from_phy, pkt_to_phy, pkt_drop_phy;
79 static struct palacios_vnet_state vnet_state;
82 static inline struct vnet_link * link_by_ip(uint32_t ip) {
83 return (struct vnet_link *)vnet_htable_search(vnet_state.ip2link, (addr_t)&ip);
86 static inline struct vnet_link * link_by_idx(int idx) {
87 struct vnet_link * link = NULL;
89 list_for_each_entry(link, &(vnet_state.link_list), node) {
91 if (link->link_idx == idx) {
98 static inline struct palacios_vnet_route * route_by_idx(int idx) {
99 struct palacios_vnet_route * route = NULL;
101 list_for_each_entry(route, &(vnet_state.route_list), node) {
103 if (route->route_idx == idx) {
112 static int parse_mac_str(char * str, uint8_t * qual, uint8_t * mac) {
115 printk("Parsing MAC (%s)\n", str);
118 if(strnicmp("any", str, strlen(str)) == 0){
121 }else if(strnicmp("none", str, strlen(str)) == 0){
125 if (strstr(str, "-")) {
126 token = strsep(&str, "-");
128 if (strnicmp("not", token, strlen("not")) == 0) {
131 printk("Invalid MAC String token (%s)\n", token);
136 if (strstr(str, ":")) {
139 if(*qual == MAC_NOSET){
143 for (i = 0; i < 6; i++) {
144 token = strsep(&str, ":");
146 printk("Invalid MAC String token (%s)\n", token);
149 mac[i] = simple_strtol(token, &token, 16);
151 printk("MAC: %2x:%2x:%2x:%2x:%2x:%2x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
154 printk("Invalid MAC String token (%s)\n", token);
164 static int str2mac(char * str, uint8_t * mac){
168 for (i = 0; i < ETH_ALEN; i++) {
169 hex = strsep(&str, ":");
171 printk("Invalid MAC String token (%s)\n", str);
174 mac[i] = simple_strtol(hex, &hex, 16);
182 * add src-MAC dst-MAC dst-TYPE [dst-ID] src-TYPE [src-ID]
184 * src-MAC = dst-MAC = not-MAC|any|none|MAC
185 * dst-TYPE = edge|interface
186 * src-TYPE = edge|interface|any
187 * dst-ID = src-ID = IP|MAC
188 * MAC=xx:xx:xx:xx:xx:xx
189 * IP = xxx.xxx.xxx.xxx
191 static int parse_route_str(char * str, struct v3_vnet_route * route) {
193 struct vnet_link *link = NULL;
196 token = strsep(&str, " ");
200 parse_mac_str(token, &(route->src_mac_qual), route->src_mac);
203 token = strsep(&str, " ");
207 parse_mac_str(token, &(route->dst_mac_qual), route->dst_mac);
210 token = strsep(&str, " ");
214 printk("dst type =(%s)\n", token);
216 if (strnicmp("interface", token, strlen("interface")) == 0) {
217 route->dst_type = LINK_INTERFACE;
218 } else if (strnicmp("edge", token, strlen("edge")) == 0) {
219 route->dst_type = LINK_EDGE;
221 printk("Invalid Destination Link Type (%s)\n", token);
226 token = strsep(&str, " ");
230 printk("dst ID=(%s)\n", token);
232 // Figure out link here
233 if (route->dst_type == LINK_EDGE) {
236 // Figure out Link Here
237 if (in4_pton(token, strlen(token), (uint8_t *)&(link_ip), '\0', NULL) != 1) {
238 printk("Invalid Dst IP address (%s)\n", token);
242 link = link_by_ip(link_ip);
244 route->dst_id = link->link_idx;
246 printk("can not find dst link %s\n", token);
250 printk("link_ip = %d, link_id = %d\n", link_ip, link->link_idx);
251 } else if (route->dst_type == LINK_INTERFACE) {
252 uint8_t mac[ETH_ALEN];
254 if(str2mac(token, mac) == -1){
255 printk("wrong MAC format (%s)\n", token);
259 route->dst_id = v3_vnet_find_dev(mac);
260 if (route->dst_id == -1){
261 printk("can not find dst device %s\n", token);
265 printk("Unsupported dst link type\n");
270 route->src_type = -1;
273 token = strsep(&str, " ");
275 printk("SRC type = %s\n", token);
281 if (strnicmp("interface", token, strlen("interface")) == 0) {
282 route->src_type = LINK_INTERFACE;
283 } else if (strnicmp("edge", token, strlen("edge")) == 0) {
284 route->src_type = LINK_EDGE;
285 } else if (strnicmp("any", token, strlen("any")) == 0) {
286 route->src_type = LINK_ANY;
288 printk("Invalid Src link type (%s)\n", token);
293 if (route->src_type == LINK_ANY) {
295 } else if (route->src_type == LINK_EDGE) {
297 token = strsep(&str, " ");
303 // Figure out Link Here
304 if (in4_pton(token, strlen(token), (uint8_t *)&(src_ip), '\0', NULL) != 1) {
305 printk("Invalid SRC IP address (%s)\n", token);
309 link = link_by_ip(src_ip);
311 route->src_id = link->link_idx;
313 printk("can not find src link %s\n", token);
316 } else if(route->src_type == LINK_INTERFACE){
317 uint8_t mac[ETH_ALEN];
319 if(str2mac(token, mac) == -1){
320 printk("wrong MAC format (%s)\n", token);
324 route->src_id = v3_vnet_find_dev(mac);
325 if (route->src_id == -1){
326 printk("can not find dst device %s\n", token);
330 printk("Invalid link type\n");
340 static void * route_seq_start(struct seq_file * s, loff_t * pos) {
341 struct palacios_vnet_route * route_iter = NULL;
345 if (*pos >= vnet_state.num_routes) {
349 list_for_each_entry(route_iter, &(vnet_state.route_list), node) {
362 static void * link_seq_start(struct seq_file * s, loff_t * pos) {
363 struct vnet_link * link_iter = NULL;
366 if (*pos >= vnet_state.num_links) {
370 list_for_each_entry(link_iter, &(vnet_state.link_list), node) {
384 static void * route_seq_next(struct seq_file * s, void * v, loff_t * pos) {
385 struct palacios_vnet_route * route_iter = NULL;
387 route_iter = list_entry(((struct palacios_vnet_route *)v)->node.next, struct palacios_vnet_route, node);
389 // Check if the list has looped
390 if (&(route_iter->node) == &(vnet_state.route_list)) {
400 static void * link_seq_next(struct seq_file * s, void * v, loff_t * pos) {
401 struct vnet_link * link_iter = NULL;
404 link_iter = list_entry(((struct vnet_link *)v)->node.next, struct vnet_link, node);
406 // Check if the list has looped
407 if (&(link_iter->node) == &(vnet_state.link_list)) {
417 static void route_seq_stop(struct seq_file * s, void * v) {
423 static void link_seq_stop(struct seq_file * s, void * v) {
428 static int route_seq_show(struct seq_file * s, void * v) {
429 struct palacios_vnet_route * route_iter = v;
430 struct v3_vnet_route * route = &(route_iter->route);
432 seq_printf(s, "%d:\t", route_iter->route_idx);
434 seq_printf(s, "\nSrc:\t");
435 switch (route->src_mac_qual) {
437 seq_printf(s, "any ");
440 seq_printf(s, "none ");
443 seq_printf(s, "not-%2x:%2x:%2x:%2x:%2x:%2x ",
444 route->src_mac[0], route->src_mac[1], route->src_mac[2],
445 route->src_mac[3], route->src_mac[4], route->src_mac[5]);
448 seq_printf(s, "%x:%x:%x:%x:%x:%x ",
449 route->src_mac[0], route->src_mac[1], route->src_mac[2],
450 route->src_mac[3], route->src_mac[4], route->src_mac[5]);
454 seq_printf(s, "\nDst:\t");
455 switch (route->dst_mac_qual) {
457 seq_printf(s, "any ");
460 seq_printf(s, "none ");
463 seq_printf(s, "not-%x:%x:%x:%x:%x:%x ",
464 route->src_mac[0], route->src_mac[1], route->src_mac[2],
465 route->src_mac[3], route->src_mac[4], route->src_mac[5]);
468 seq_printf(s, "%x:%x:%x:%x:%x:%x ",
469 route->src_mac[0], route->src_mac[1], route->src_mac[2],
470 route->src_mac[3], route->src_mac[4], route->src_mac[5]);
474 seq_printf(s, "\nDst-Type:\t");
475 switch (route->dst_type) {
477 struct vnet_link * link = (struct vnet_link *)link_by_idx(route->dst_id);
478 seq_printf(s, "EDGE %pI4", &link->dst_ip);
481 case LINK_INTERFACE: {
482 seq_printf(s, "INTERFACE ");
483 seq_printf(s, "%d ", route->dst_id);
487 seq_printf(s, "Invalid Dst Link Type (%d) ", route->dst_type);
491 seq_printf(s, "\nSrc-Type:\t");
492 switch (route->src_type) {
494 struct vnet_link * link = (struct vnet_link *)link_by_idx(route->src_id);
495 seq_printf(s, "EDGE %pI4", &link->dst_ip);
498 case LINK_INTERFACE: {
499 seq_printf(s, "INTERFACE %d", route->src_id);
503 seq_printf(s, "ANY");
506 seq_printf(s, "Invalid Src Link Type (%d) ", route->src_type);
517 static int link_seq_show(struct seq_file * s, void * v) {
518 struct vnet_link * link_iter = v;
520 seq_printf(s, "%d:\t%pI4\t%d\n\t\tReceived Pkts: %d, Received Bytes %lld\n\t\tSent Pkts: %d, Sent Bytes: %lld\n\n",
527 link_iter->bytes_tx);
533 static struct seq_operations route_seq_ops = {
534 .start = route_seq_start,
535 .next = route_seq_next,
536 .stop = route_seq_stop,
537 .show = route_seq_show
541 static struct seq_operations link_seq_ops = {
542 .start = link_seq_start,
543 .next = link_seq_next,
544 .stop = link_seq_stop,
545 .show = link_seq_show
549 static int route_open(struct inode * inode, struct file * file) {
550 return seq_open(file, &route_seq_ops);
554 static int link_open(struct inode * inode, struct file * file) {
555 return seq_open(file, &link_seq_ops);
558 static int inject_route(struct palacios_vnet_route * route) {
561 v3_vnet_add_route(route->route);
563 spin_lock_irqsave(&(vnet_state.lock), flags);
564 list_add(&(route->node), &(vnet_state.route_list));
565 route->route_idx = vnet_state.num_routes++;
566 spin_unlock_irqrestore(&(vnet_state.lock), flags);
568 printk("Palacios-vnet: One route added to VNET core\n");
574 route_write(struct file * file,
579 char * buf_iter = NULL;
580 char * line_str = route_buf;
587 if (copy_from_user(route_buf, buf, size)) {
591 route_buf[size] = '\0';
592 printk("Route written: %s\n", route_buf);
594 while ((buf_iter = strsep(&line_str, "\r\n"))) {
596 token = strsep(&buf_iter, " ");
601 if (strnicmp("ADD", token, strlen("ADD")) == 0) {
602 struct palacios_vnet_route * new_route = NULL;
603 new_route = kmalloc(sizeof(struct palacios_vnet_route), GFP_KERNEL);
609 memset(new_route, 0, sizeof(struct palacios_vnet_route));
611 if (parse_route_str(buf_iter, &(new_route->route)) == -1) {
616 if (inject_route(new_route) != 0) {
619 } else if (strnicmp("DEL", token, strlen("DEL")) == 0) {
620 printk("I should delete the route here\n");
622 printk("Invalid Route command string\n");
629 static int create_link(struct vnet_link * link) {
634 switch(link->sock_proto){
636 protocol = IPPROTO_UDP;
639 protocol = IPPROTO_TCP;
643 printk("Unsupported VNET Server Protocol\n");
647 if ((err = sock_create(AF_INET, SOCK_DGRAM, protocol, &link->sock)) < 0) {
648 printk("Could not create socket for VNET Link, error %d\n", err);
652 memset(&link->sock_addr, 0, sizeof(struct sockaddr));
654 link->sock_addr.sin_family = AF_INET;
655 link->sock_addr.sin_addr.s_addr = link->dst_ip;
656 link->sock_addr.sin_port = htons(link->dst_port);
658 if ((err = link->sock->ops->connect(link->sock, (struct sockaddr *)&(link->sock_addr), sizeof(struct sockaddr), 0) < 0)) {
659 printk("Could not connect to remote VNET Server, error %d\n", err);
663 // We use the file pointer because we are in the kernel
664 // This is only used to assigned File Descriptors for user space, so it is available here
665 // link->sock->file = link;
667 spin_lock_irqsave(&(vnet_state.lock), flags);
668 list_add(&(link->node), &(vnet_state.link_list));
669 link->link_idx = vnet_state.num_links++;
670 vnet_htable_insert(vnet_state.ip2link, (addr_t)&(link->dst_ip), (addr_t)link);
671 spin_unlock_irqrestore(&(vnet_state.lock), flags);
673 printk("VNET Bridge: Link created, ip 0x%x, port: %d, idx: %d, link: %p, protocol: %s\n",
678 ((link->sock_proto==UDP)?"UDP":"TCP"));
684 /* ADD dst-ip 9000 [udp|tcp] */
686 link_write(struct file * file, const char * buf, size_t size, loff_t * ppos) {
688 char * link_iter = NULL;
689 char * line_str = link_buf;
696 if (copy_from_user(link_buf, buf, size)) {
700 while ((link_iter = strsep(&line_str, "\r\n"))) {
701 printk("Link written: %s\n", link_buf);
703 token = strsep(&link_iter, " ");
709 if (strnicmp("ADD", token, strlen("ADD")) == 0) {
710 struct vnet_link * new_link = NULL;
711 char * ip_str = NULL;
714 ip_str = strsep(&link_iter, " ");
716 if ((!ip_str) || (!link_iter)) {
717 printk("Missing fields in ADD Link command\n");
721 if (in4_pton(ip_str, strlen(ip_str), (uint8_t *)&(ip), '\0', NULL) != 1) {
722 printk("Invalid Dst IP address (%s)\n", ip_str);
726 new_link = kmalloc(sizeof(struct vnet_link), GFP_KERNEL);
731 memset(new_link, 0, sizeof(struct vnet_link));
733 new_link->dst_ip = ip;
734 new_link->dst_port = simple_strtol(link_iter, &link_iter, 10);
736 //TODO: Parse UDP|TCP
737 new_link->sock_proto = UDP;
739 if (create_link(new_link) != 0) {
740 printk("Could not create link\n");
745 } else if (strnicmp("DEL", token, strlen("DEL")) == 0) {
746 printk("Link deletion not supported\n");
748 printk("Invalid Link command string\n");
756 static struct file_operations route_fops = {
757 .owner = THIS_MODULE,
760 .write = route_write,
762 .release = seq_release
766 static struct file_operations link_fops = {
767 .owner = THIS_MODULE,
772 .release = seq_release
777 debug_write(struct file * file, const char * buf, size_t size, loff_t * ppos) {
779 char * link_iter = NULL;
780 char * line_str = link_buf;
787 if (copy_from_user(link_buf, buf, size)) {
791 link_iter = strsep(&line_str, "\r\n");
792 level = simple_strtol(link_iter, &link_iter, 10);
794 printk("Set VNET Debug level to %d\n", level);
804 static int debug_show(struct seq_file * file, void * v){
805 seq_printf(file, "Current VNET Debug Level: %d\n", vnet_debug);
810 static int debug_open(struct inode * inode, struct file * file) {
811 return single_open(file, debug_show, NULL);
814 static struct file_operations debug_fops = {
815 .owner = THIS_MODULE,
818 .write = debug_write,
820 .release = seq_release
823 static int stat_show(struct seq_file * file, void * v){
824 struct vnet_stat stats;
826 v3_vnet_stat(&stats);
828 seq_printf(file, "VNET Core\n");
829 seq_printf(file, "\tReceived Packets: %d\n", stats.rx_pkts);
830 seq_printf(file, "\tReceived Bytes: %lld\n", stats.rx_bytes);
831 seq_printf(file, "\tTransmitted Packets: %d\n", stats.tx_pkts);
832 seq_printf(file, "\tTransmitted Bytes: %lld\n", stats.tx_bytes);
835 seq_printf(file, "\nVNET Bridge Server\n");
836 seq_printf(file, "\tReceived From VMM: %d\n", vnet_state.pkt_from_vmm);
837 seq_printf(file, "\tSent To VMM: %d\n", vnet_state.pkt_to_vmm);
838 seq_printf(file, "\tDropped From VMM: %d\n", vnet_state.pkt_drop_vmm);
839 seq_printf(file, "\tReceived From Extern Network: %d\n", vnet_state.pkt_from_phy);
840 seq_printf(file, "\tSent To Extern Network: %d\n", vnet_state.pkt_to_phy);
841 seq_printf(file, "\tDropped From Extern Network: %d\n", vnet_state.pkt_drop_phy);
846 static int stat_open(struct inode * inode, struct file * file) {
847 return single_open(file, stat_show, NULL);
850 static struct file_operations stat_fops = {
851 .owner = THIS_MODULE,
855 .release = seq_release
859 static int init_proc_files(void) {
860 struct proc_dir_entry * route_entry = NULL;
861 struct proc_dir_entry * link_entry = NULL;
862 struct proc_dir_entry * stat_entry = NULL;
863 struct proc_dir_entry * debug_entry = NULL;
864 struct proc_dir_entry * vnet_root = NULL;
866 vnet_root = proc_mkdir("vnet", NULL);
867 if (vnet_root == NULL) {
871 route_entry = create_proc_entry("routes", 0, vnet_root);
872 if (route_entry == NULL) {
873 remove_proc_entry("vnet", NULL);
876 route_entry->proc_fops = &route_fops;
879 link_entry = create_proc_entry("links", 0, vnet_root);
880 if (link_entry == NULL) {
881 remove_proc_entry("routes", vnet_root);
882 remove_proc_entry("vnet", NULL);
885 link_entry->proc_fops = &link_fops;
888 stat_entry = create_proc_entry("stats", 0, vnet_root);
889 if(stat_entry == NULL) {
890 remove_proc_entry("links", vnet_root);
891 remove_proc_entry("routes", vnet_root);
892 remove_proc_entry("vnet", NULL);
895 stat_entry->proc_fops = &stat_fops;
898 debug_entry = create_proc_entry("debug", 0, vnet_root);
899 if(debug_entry == NULL) {
900 remove_proc_entry("links", vnet_root);
901 remove_proc_entry("routes", vnet_root);
902 remove_proc_entry("stats", vnet_root);
903 remove_proc_entry("vnet", NULL);
906 debug_entry->proc_fops = &debug_fops;
915 udp_send(struct socket * sock,
916 struct sockaddr_in * addr,
917 unsigned char * buf, int len) {
924 if (sock->sk == NULL) {
933 msg.msg_namelen = sizeof(struct sockaddr_in);
934 msg.msg_control = NULL;
935 msg.msg_controllen = 0;
938 msg.msg_control = NULL;
942 size = sock_sendmsg(sock, &msg, len);
951 udp_recv(struct socket * sock,
952 struct sockaddr_in * addr,
953 unsigned char * buf, int len) {
959 if (sock->sk == NULL) {
968 msg.msg_namelen = sizeof(struct sockaddr_in);
969 msg.msg_control = NULL;
970 msg.msg_controllen = 0;
973 msg.msg_control = NULL;
977 size = sock_recvmsg(sock, &msg, len, msg.msg_flags);
984 /* send packets to VNET core */
986 send_to_palacios(unsigned char * buf,
989 struct v3_vnet_pkt pkt;
991 pkt.src_type = LINK_EDGE;
992 pkt.src_id = link_id;
993 memcpy(pkt.header, buf, ETHERNET_HEADER_LEN);
997 printk("VNET Lnx Bridge: send pkt to VNET core (size: %d, src_id: %d, src_type: %d)\n",
998 pkt.size, pkt.src_id, pkt.src_type);
1000 print_hex_dump(NULL, "pkt_data: ", 0, 20, 20, pkt.data, pkt.size, 0);
1004 vnet_state.pkt_to_vmm ++;
1006 return v3_vnet_send_pkt(&pkt, NULL, 1);
1010 /* send packet to extern network */
1012 bridge_send_pkt(struct v3_vm_info * vm,
1013 struct v3_vnet_pkt * pkt,
1014 void * private_data) {
1015 struct vnet_link * link;
1017 if(vnet_debug >= 2){
1018 printk("VNET Lnx Host Bridge: packet received from VNET Core ... pkt size: %d, link: %d\n",
1021 if(vnet_debug >= 4){
1022 print_hex_dump(NULL, "pkt_data: ", 0, 20, 20, pkt->data, pkt->size, 0);
1026 vnet_state.pkt_from_vmm ++;
1028 link = link_by_idx(pkt->dst_id);
1030 switch(link->sock_proto){
1032 udp_send(link->sock, &(link->sock_addr), pkt->data, pkt->size);
1033 vnet_state.pkt_to_phy ++;
1036 vnet_state.pkt_to_phy ++;
1040 printk("VNET Server: Invalid Link Protocol\n");
1041 vnet_state.pkt_drop_vmm ++;
1043 link->bytes_tx += pkt->size;
1046 printk("VNET Bridge Linux Host: wrong dst link, idx: %d, discards the packet\n", pkt->dst_id);
1047 vnet_state.pkt_drop_vmm ++;
1054 static int init_vnet_serv(void) {
1058 switch(vnet_state.serv_proto){
1060 protocol = IPPROTO_UDP;
1063 protocol = IPPROTO_TCP;
1067 printk("Unsupported VNET Server Protocol\n");
1071 if ((err = sock_create(AF_INET, SOCK_DGRAM, protocol, &vnet_state.serv_sock)) < 0) {
1072 printk("Could not create VNET server socket, error: %d\n", err);
1076 memset(&vnet_state.serv_addr, 0, sizeof(struct sockaddr));
1078 vnet_state.serv_addr.sin_family = AF_INET;
1079 vnet_state.serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
1080 vnet_state.serv_addr.sin_port = htons(VNET_SERVER_PORT);
1082 if ((err = vnet_state.serv_sock->ops->bind(vnet_state.serv_sock, (struct sockaddr *)&(vnet_state.serv_addr), sizeof(struct sockaddr))) < 0) {
1083 printk("Could not bind VNET server socket to port %d, error: %d\n", VNET_SERVER_PORT, err);
1087 printk("VNET server bind to port: %d\n", VNET_SERVER_PORT);
1089 if(vnet_state.serv_proto == TCP){
1090 if((err = vnet_state.serv_sock->ops->listen(vnet_state.serv_sock, 32)) < 0){
1091 printk("VNET Server error listening on port %d, error %d\n", VNET_SERVER_PORT, err);
1099 static int vnet_udp_server(void * arg) {
1100 unsigned char * pkt;
1101 struct sockaddr_in pkt_addr;
1102 struct vnet_link * link = NULL;
1105 printk("Palacios VNET Bridge: UDP receiving server ..... \n");
1107 pkt = kmalloc(MAX_PACKET_LEN, GFP_KERNEL);
1108 while (!kthread_should_stop()) {
1110 len = udp_recv(vnet_state.serv_sock, &pkt_addr, pkt, MAX_PACKET_LEN);
1112 printk("Receive error: Could not get packet, error %d\n", len);
1116 link = link_by_ip(pkt_addr.sin_addr.s_addr);
1118 printk("VNET Server: No VNET Link match the src IP\n");
1119 vnet_state.pkt_drop_phy ++;
1123 vnet_state.pkt_from_phy ++;
1124 link->bytes_rx += len;
1127 send_to_palacios(pkt, len, link->link_idx);
1137 static int vnet_server(void * arg) {
1139 if(vnet_state.serv_proto == UDP){
1140 vnet_udp_server(NULL);
1141 }else if(vnet_state.serv_proto == TCP) {
1142 //accept new connection
1143 //use select to receive pkt from physical network
1144 //or create new kthread to handle each connection?
1146 printk ("VNET Server: Unsupported Protocol\n");
1153 static inline unsigned int hash_fn(addr_t hdr_ptr) {
1154 return vnet_hash_buffer((uint8_t *)hdr_ptr, sizeof(uint32_t));
1157 static inline int hash_eq(addr_t key1, addr_t key2) {
1158 return (memcmp((uint8_t *)key1, (uint8_t *)key2, sizeof(uint32_t)) == 0);
1162 int palacios_init_vnet(void) {
1163 struct v3_vnet_bridge_ops bridge_ops;
1166 if(vnet_state.status != 0) {
1169 vnet_state.status = 1;
1171 memset(&vnet_state, 0, sizeof(struct palacios_vnet_state));
1173 INIT_LIST_HEAD(&(vnet_state.link_list));
1174 INIT_LIST_HEAD(&(vnet_state.route_list));
1175 spin_lock_init(&(vnet_state.lock));
1179 vnet_state.serv_proto = UDP;
1181 vnet_state.ip2link = vnet_create_htable(10, hash_fn, hash_eq);
1182 if(vnet_state.ip2link == NULL){
1183 printk("Failure to initiate VNET link hashtable\n");
1187 if(init_vnet_serv() < 0){
1188 printk("Failure to initiate VNET server\n");
1192 vnet_state.serv_thread = kthread_run(vnet_server, NULL, "vnet-server");
1194 bridge_ops.input = bridge_send_pkt;
1195 bridge_ops.poll = NULL;
1197 v3_vnet_add_bridge(NULL, &bridge_ops, HOST_LNX_BRIDGE, NULL);
1199 printk("Palacios VNET Linux Bridge initiated\n");