2 Palacios VNET Control Module
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 vnet_route_iter {
28 struct v3_vnet_route route;
31 struct list_head node;
35 struct vnet_link_iter {
38 vnet_brg_proto_t proto;
41 struct list_head node;
45 struct vnet_ctrl_state {
51 struct list_head route_list;
52 struct list_head link_iter_list;
56 struct proc_dir_entry * vnet_proc_root;
60 static struct vnet_ctrl_state vnet_ctrl_s;
63 static int parse_mac_str(char * str, uint8_t * qual, uint8_t * mac) {
66 printk("Parsing MAC (%s)\n", str);
69 if(strnicmp("any", str, strlen(str)) == 0){
72 }else if(strnicmp("none", str, strlen(str)) == 0){
76 if (strstr(str, "-")) {
77 token = strsep(&str, "-");
79 if (strnicmp("not", token, strlen("not")) == 0) {
82 printk("Invalid MAC String token (%s)\n", token);
87 if (strstr(str, ":")) {
90 if(*qual == MAC_NOSET){
94 for (i = 0; i < 6; i++) {
95 token = strsep(&str, ":");
97 printk("Invalid MAC String token (%s)\n", token);
100 mac[i] = simple_strtol(token, &token, 16);
102 printk("MAC: %2x:%2x:%2x:%2x:%2x:%2x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
105 printk("Invalid MAC String token (%s)\n", token);
115 static int str2mac(char * str, uint8_t * mac){
119 for (i = 0; i < ETH_ALEN; i++) {
120 hex = strsep(&str, ":");
122 printk("Invalid MAC String token (%s)\n", str);
125 mac[i] = simple_strtol(hex, &hex, 16);
132 static inline struct vnet_link_iter * link_by_ip(uint32_t ip) {
133 struct vnet_link_iter * link = NULL;
135 list_for_each_entry(link, &(vnet_ctrl_s.link_iter_list), node) {
137 if (link->dst_ip == ip) {
145 static inline struct vnet_link_iter * link_by_idx(int idx) {
146 struct vnet_link_iter * link = NULL;
148 list_for_each_entry(link, &(vnet_ctrl_s.link_iter_list), node) {
149 if (link->idx == idx) {
158 static int parse_route_str(char * str, struct v3_vnet_route * route) {
160 struct vnet_link_iter * link = NULL;
163 token = strsep(&str, " ");
167 parse_mac_str(token, &(route->src_mac_qual), route->src_mac);
170 token = strsep(&str, " ");
174 parse_mac_str(token, &(route->dst_mac_qual), route->dst_mac);
177 token = strsep(&str, " ");
181 printk("dst type =(%s)\n", token);
183 if (strnicmp("interface", token, strlen("interface")) == 0) {
184 route->dst_type = LINK_INTERFACE;
185 } else if (strnicmp("edge", token, strlen("edge")) == 0) {
186 route->dst_type = LINK_EDGE;
188 printk("Invalid Destination Link Type (%s)\n", token);
193 token = strsep(&str, " ");
197 printk("dst ID=(%s)\n", token);
199 // Figure out link here
200 if (route->dst_type == LINK_EDGE) {
203 // Figure out Link Here
204 if (in4_pton(token, strlen(token), (uint8_t *)&(link_ip), '\0', NULL) != 1) {
205 printk("Invalid Dst IP address (%s)\n", token);
209 link = link_by_ip(link_ip);
211 route->dst_id = link->idx;
213 printk("can not find dst link %s\n", token);
217 printk("link_ip = %d, link_id = %d\n", link_ip, link->idx);
218 } else if (route->dst_type == LINK_INTERFACE) {
219 uint8_t mac[ETH_ALEN];
221 if(str2mac(token, mac) == -1){
222 printk("wrong MAC format (%s)\n", token);
226 route->dst_id = v3_vnet_find_dev(mac);
227 if (route->dst_id == -1){
228 printk("can not find dst device %s\n", token);
232 printk("Unsupported dst link type\n");
237 route->src_type = -1;
240 token = strsep(&str, " ");
242 printk("SRC type = %s\n", token);
248 if (strnicmp("interface", token, strlen("interface")) == 0) {
249 route->src_type = LINK_INTERFACE;
250 } else if (strnicmp("edge", token, strlen("edge")) == 0) {
251 route->src_type = LINK_EDGE;
252 } else if (strnicmp("any", token, strlen("any")) == 0) {
253 route->src_type = LINK_ANY;
255 printk("Invalid Src link type (%s)\n", token);
260 if (route->src_type == LINK_ANY) {
262 } else if (route->src_type == LINK_EDGE) {
264 token = strsep(&str, " ");
270 // Figure out Link Here
271 if (in4_pton(token, strlen(token), (uint8_t *)&(src_ip), '\0', NULL) != 1) {
272 printk("Invalid SRC IP address (%s)\n", token);
276 link = link_by_ip(src_ip);
278 route->src_id = link->idx;
280 printk("can not find src link %s\n", token);
283 } else if(route->src_type == LINK_INTERFACE){
284 uint8_t mac[ETH_ALEN];
286 if(str2mac(token, mac) == -1){
287 printk("wrong MAC format (%s)\n", token);
291 route->src_id = v3_vnet_find_dev(mac);
292 if (route->src_id == -1){
293 printk("can not find dst device %s\n", token);
297 printk("Invalid link type\n");
305 static void * route_seq_start(struct seq_file * s, loff_t * pos) {
306 struct vnet_route_iter * route_iter = NULL;
309 if (*pos >= vnet_ctrl_s.num_routes) {
313 list_for_each_entry(route_iter, &(vnet_ctrl_s.route_list), node) {
325 static void * route_seq_next(struct seq_file * s, void * v, loff_t * pos) {
326 struct vnet_route_iter * route_iter = NULL;
328 route_iter = list_entry(((struct vnet_route_iter *)v)->node.next, struct vnet_route_iter, node);
330 // Check if the list has looped
331 if (&(route_iter->node) == &(vnet_ctrl_s.route_list)) {
340 static void route_seq_stop(struct seq_file * s, void * v) {
345 static void * link_seq_start(struct seq_file * s, loff_t * pos) {
346 struct vnet_link_iter * link_iter = NULL;
349 if (*pos >= vnet_ctrl_s.num_links) {
353 list_for_each_entry(link_iter, &(vnet_ctrl_s.link_iter_list), node) {
364 static int route_seq_show(struct seq_file * s, void * v) {
365 struct vnet_route_iter * route_iter = v;
366 struct v3_vnet_route * route = &(route_iter->route);
368 seq_printf(s, "%d:\t", route_iter->idx);
370 seq_printf(s, "\nSrc:\t");
371 switch (route->src_mac_qual) {
373 seq_printf(s, "any ");
376 seq_printf(s, "none ");
379 seq_printf(s, "not-%2x:%2x:%2x:%2x:%2x:%2x ",
380 route->src_mac[0], route->src_mac[1], route->src_mac[2],
381 route->src_mac[3], route->src_mac[4], route->src_mac[5]);
384 seq_printf(s, "%x:%x:%x:%x:%x:%x ",
385 route->src_mac[0], route->src_mac[1], route->src_mac[2],
386 route->src_mac[3], route->src_mac[4], route->src_mac[5]);
390 seq_printf(s, "\nDst:\t");
391 switch (route->dst_mac_qual) {
393 seq_printf(s, "any ");
396 seq_printf(s, "none ");
399 seq_printf(s, "not-%x:%x:%x:%x:%x:%x ",
400 route->src_mac[0], route->src_mac[1], route->src_mac[2],
401 route->src_mac[3], route->src_mac[4], route->src_mac[5]);
404 seq_printf(s, "%x:%x:%x:%x:%x:%x ",
405 route->src_mac[0], route->src_mac[1], route->src_mac[2],
406 route->src_mac[3], route->src_mac[4], route->src_mac[5]);
410 seq_printf(s, "\nDst-Type:\t");
411 switch (route->dst_type) {
413 struct vnet_link_iter * link = (struct vnet_link_iter *)link_by_idx(route->dst_id);
414 seq_printf(s, "EDGE %pI4", &link->dst_ip);
417 case LINK_INTERFACE: {
418 seq_printf(s, "INTERFACE ");
419 seq_printf(s, "%d ", route->dst_id);
423 seq_printf(s, "Invalid Dst Link Type (%d) ", route->dst_type);
427 seq_printf(s, "\nSrc-Type:\t");
428 switch (route->src_type) {
430 struct vnet_link_iter * link = (struct vnet_link_iter *)link_by_idx(route->src_id);
431 seq_printf(s, "EDGE %pI4", &link->dst_ip);
434 case LINK_INTERFACE: {
435 seq_printf(s, "INTERFACE %d", route->src_id);
439 seq_printf(s, "ANY");
442 seq_printf(s, "Invalid Src Link Type (%d) ", route->src_type);
451 static void * link_seq_next(struct seq_file * s, void * v, loff_t * pos) {
452 struct vnet_link_iter * link_iter = NULL;
454 link_iter = list_entry(((struct vnet_link_iter *)v)->node.next, struct vnet_link_iter, node);
456 // Check if the list has looped
457 if (&(link_iter->node) == &(vnet_ctrl_s.link_iter_list)) {
466 static void link_seq_stop(struct seq_file * s, void * v) {
471 static int link_seq_show(struct seq_file * s, void * v) {
472 struct vnet_link_iter * link_iter = v;
473 struct nic_statistics stats;
475 vnet_brg_link_stats(link_iter->idx, &stats);
477 seq_printf(s, "%d:\t%pI4\t%d\n\t\tReceived Pkts: %lld, Received Bytes %lld\n\t\tSent Pkts: %lld, Sent Bytes: %lld\n\n",
490 static struct seq_operations route_seq_ops = {
491 .start = route_seq_start,
492 .next = route_seq_next,
493 .stop = route_seq_stop,
494 .show = route_seq_show
498 static struct seq_operations link_seq_ops = {
499 .start = link_seq_start,
500 .next = link_seq_next,
501 .stop = link_seq_stop,
502 .show = link_seq_show
506 static int route_open(struct inode * inode, struct file * file) {
507 return seq_open(file, &route_seq_ops);
511 static int link_open(struct inode * inode, struct file * file) {
512 return seq_open(file, &link_seq_ops);
517 static int inject_route(struct vnet_route_iter * route) {
520 route->idx = v3_vnet_add_route(route->route);
522 spin_lock_irqsave(&(vnet_ctrl_s.lock), flags);
523 list_add(&(route->node), &(vnet_ctrl_s.route_list));
524 vnet_ctrl_s.num_routes ++;
525 spin_unlock_irqrestore(&(vnet_ctrl_s.lock), flags);
527 printk("VNET Control: One route added to VNET core\n");
533 static void delete_route(struct vnet_route_iter * route) {
536 v3_vnet_del_route(route->idx);
538 spin_lock_irqsave(&(vnet_ctrl_s.lock), flags);
539 list_del(&(route->node));
540 vnet_ctrl_s.num_routes --;
541 spin_unlock_irqrestore(&(vnet_ctrl_s.lock), flags);
543 printk("VNET Control: Route %d deleted from VNET\n", route->idx);
551 * add src-MAC dst-MAC dst-TYPE [dst-ID] src-TYPE [src-ID]
553 * src-MAC = dst-MAC = not-MAC|any|none|MAC
554 * dst-TYPE = edge|interface
555 * src-TYPE = edge|interface|any
556 * dst-ID = src-ID = IP|MAC
557 * MAC=xx:xx:xx:xx:xx:xx
558 * IP = xxx.xxx.xxx.xxx
565 route_write(struct file * file,
570 char * buf_iter = NULL;
571 char * line_str = route_buf;
578 if (copy_from_user(route_buf, buf, size)) {
582 route_buf[size] = '\0';
583 printk("Route written: %s\n", route_buf);
585 while ((buf_iter = strsep(&line_str, "\r\n"))) {
587 token = strsep(&buf_iter, " ");
592 if (strnicmp("ADD", token, strlen("ADD")) == 0) {
593 struct vnet_route_iter * new_route = NULL;
594 new_route = kmalloc(sizeof(struct vnet_route_iter), GFP_KERNEL);
600 memset(new_route, 0, sizeof(struct vnet_route_iter));
602 if (parse_route_str(buf_iter, &(new_route->route)) == -1) {
607 if (inject_route(new_route) != 0) {
611 } else if (strnicmp("DEL", token, strlen("DEL")) == 0) {
612 char * idx_str = NULL;
614 struct vnet_route_iter * route = NULL;
616 idx_str = strsep(&buf_iter, " ");
619 printk("Missing route idx in DEL Route command\n");
623 d_idx = simple_strtoul(idx_str, &idx_str, 10);
625 printk("VNET: deleting route %d\n", d_idx);
627 list_for_each_entry(route, &(vnet_ctrl_s.route_list), node) {
628 if (route->idx == d_idx) {
634 printk("Invalid Route command string\n");
642 static void delete_link(struct vnet_link_iter * link){
645 vnet_brg_delete_link(link->idx);
647 spin_lock_irqsave(&(vnet_ctrl_s.lock), flags);
648 list_del(&(link->node));
649 vnet_ctrl_s.num_links --;
650 spin_unlock_irqrestore(&(vnet_ctrl_s.lock), flags);
657 static void deinit_links_list(void){
658 struct vnet_link_iter * link;
660 list_for_each_entry(link, &(vnet_ctrl_s.link_iter_list), node) {
665 static void deinit_routes_list(void){
666 struct vnet_route_iter * route;
668 list_for_each_entry(route, &(vnet_ctrl_s.route_list), node) {
673 /* ADD dst-ip 9000 [udp|tcp] */
676 link_write(struct file * file, const char * buf, size_t size, loff_t * ppos) {
678 char * link_iter = NULL;
679 char * line_str = link_buf;
686 if (copy_from_user(link_buf, buf, size)) {
690 while ((link_iter = strsep(&line_str, "\r\n"))) {
691 printk("Link written: %s\n", link_buf);
693 token = strsep(&link_iter, " ");
699 if (strnicmp("ADD", token, strlen("ADD")) == 0) {
700 struct vnet_link_iter * link = NULL;
701 char * ip_str = NULL;
704 vnet_brg_proto_t d_proto;
708 ip_str = strsep(&link_iter, " ");
710 if ((!ip_str) || (!link_iter)) {
711 printk("Missing fields in ADD Link command\n");
715 if (in4_pton(ip_str, strlen(ip_str), (uint8_t *)&(d_ip), '\0', NULL) != 1) {
716 printk("Invalid Dst IP address (%s)\n", ip_str);
720 d_port = simple_strtol(link_iter, &link_iter, 10);
723 link_idx = vnet_brg_add_link(d_ip, d_port, d_proto);
725 printk("VNET Control: Failed to create link\n");
729 link = kmalloc(sizeof(struct vnet_link_iter), GFP_KERNEL);
730 memset(link, 0, sizeof(struct vnet_link_iter));
733 link->dst_port = d_port;
734 link->proto = d_proto;
735 link->idx = link_idx;
737 spin_lock_irqsave(&(vnet_ctrl_s.lock), flags);
738 list_add(&(link->node), &(vnet_ctrl_s.link_iter_list));
739 vnet_ctrl_s.num_links ++;
740 spin_unlock_irqrestore(&(vnet_ctrl_s.lock), flags);
741 } else if (strnicmp("DEL", token, strlen("DEL")) == 0) {
742 char * idx_str = NULL;
745 idx_str = strsep(&link_iter, " ");
748 printk("Missing link idx in DEL Link command\n");
752 d_idx = simple_strtoul(idx_str, &idx_str, 10);
754 vnet_brg_delete_link(d_idx);
756 printk("VNET Control: One link deleted\n");
758 printk("Invalid Link command string\n");
766 static struct file_operations route_fops = {
767 .owner = THIS_MODULE,
770 .write = route_write,
772 .release = seq_release
776 static struct file_operations link_fops = {
777 .owner = THIS_MODULE,
782 .release = seq_release
787 debug_write(struct file * file, const char * buf, size_t size, loff_t * ppos) {
789 char * in_iter = NULL;
790 char * line_str = in_buf;
797 if (copy_from_user(in_buf, buf, size)) {
801 in_iter = strsep(&line_str, "\r\n");
802 level = simple_strtol(in_iter, &in_iter, 10);
804 printk("VNET Control: Set VNET Debug level to %d\n", level);
814 static int debug_show(struct seq_file * file, void * v){
815 seq_printf(file, "Current NET Debug Level: %d\n", net_debug);
820 static int debug_open(struct inode * inode, struct file * file) {
821 return single_open(file, debug_show, NULL);
824 static struct file_operations debug_fops = {
825 .owner = THIS_MODULE,
828 .write = debug_write,
830 .release = seq_release
833 static int stat_show(struct seq_file * file, void * v){
834 struct vnet_stat stats;
835 struct vnet_brg_stats brg_stats;
837 v3_vnet_stat(&stats);
839 seq_printf(file, "VNET Core\n");
840 seq_printf(file, "\tReceived Packets: %d\n", stats.rx_pkts);
841 seq_printf(file, "\tReceived Bytes: %lld\n", stats.rx_bytes);
842 seq_printf(file, "\tTransmitted Packets: %d\n", stats.tx_pkts);
843 seq_printf(file, "\tTransmitted Bytes: %lld\n", stats.tx_bytes);
845 vnet_brg_stats(&brg_stats);
847 seq_printf(file, "\nVNET Bridge Server\n");
848 seq_printf(file, "\tReceived From VMM: %lld\n", brg_stats.pkt_from_vmm);
849 seq_printf(file, "\tSent To VMM: %lld\n", brg_stats.pkt_to_vmm);
850 seq_printf(file, "\tDropped From VMM: %lld\n", brg_stats.pkt_drop_vmm);
851 seq_printf(file, "\tReceived From Extern Network: %lld\n", brg_stats.pkt_from_phy);
852 seq_printf(file, "\tSent To Extern Network: %lld\n", brg_stats.pkt_to_phy);
853 seq_printf(file, "\tDropped From Extern Network: %lld\n", brg_stats.pkt_drop_phy);
858 static int stat_open(struct inode * inode, struct file * file) {
859 return single_open(file, stat_show, NULL);
862 static struct file_operations stat_fops = {
863 .owner = THIS_MODULE,
867 .release = seq_release
871 static int init_proc_files(void) {
872 struct proc_dir_entry * route_entry = NULL;
873 struct proc_dir_entry * link_entry = NULL;
874 struct proc_dir_entry * stat_entry = NULL;
875 struct proc_dir_entry * debug_entry = NULL;
876 struct proc_dir_entry * vnet_root = NULL;
878 vnet_root = proc_mkdir("vnet", NULL);
879 if (vnet_root == NULL) {
883 route_entry = create_proc_entry("routes", 0, vnet_root);
884 if (route_entry == NULL) {
885 remove_proc_entry("vnet", NULL);
888 route_entry->proc_fops = &route_fops;
891 link_entry = create_proc_entry("links", 0, vnet_root);
892 if (link_entry == NULL) {
893 remove_proc_entry("routes", vnet_root);
894 remove_proc_entry("vnet", NULL);
897 link_entry->proc_fops = &link_fops;
900 stat_entry = create_proc_entry("stats", 0, vnet_root);
901 if(stat_entry == NULL) {
902 remove_proc_entry("links", vnet_root);
903 remove_proc_entry("routes", vnet_root);
904 remove_proc_entry("vnet", NULL);
907 stat_entry->proc_fops = &stat_fops;
910 debug_entry = create_proc_entry("debug", 0, vnet_root);
911 if(debug_entry == NULL) {
912 remove_proc_entry("links", vnet_root);
913 remove_proc_entry("routes", vnet_root);
914 remove_proc_entry("stats", vnet_root);
915 remove_proc_entry("vnet", NULL);
918 debug_entry->proc_fops = &debug_fops;
920 vnet_ctrl_s.vnet_proc_root = vnet_root;
926 static void destroy_proc_files(void) {
927 struct proc_dir_entry * vnet_root = vnet_ctrl_s.vnet_proc_root;
929 remove_proc_entry("debug", vnet_root);
930 remove_proc_entry("links", vnet_root);
931 remove_proc_entry("routes", vnet_root);
932 remove_proc_entry("stats", vnet_root);
933 remove_proc_entry("vnet", NULL);
937 int vnet_ctrl_init(void) {
938 if(vnet_ctrl_s.status != 0) {
941 vnet_ctrl_s.status = 1;
943 memset(&vnet_ctrl_s, 0, sizeof(struct vnet_ctrl_state));
945 INIT_LIST_HEAD(&(vnet_ctrl_s.link_iter_list));
946 INIT_LIST_HEAD(&(vnet_ctrl_s.route_list));
947 spin_lock_init(&(vnet_ctrl_s.lock));
951 printk("VNET Linux control module initiated\n");
957 void vnet_ctrl_deinit(void){
958 destroy_proc_files();
961 deinit_routes_list();
963 vnet_ctrl_s.status = 0;