Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Restructure of direct host network bridge.
[palacios.git] / palacios / src / devices / nic_bridge.c
1 /* 
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.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
10  * Copyright (c) 2010, Lei Xia <lxia@northwestern.edu>
11  * Copyright (c) 2010, The V3VEE Project <http://www.v3vee.org> 
12  * All rights reserved.
13  *
14  * Author: Lei Xia <lxia@northwestern.edu>
15  *               
16  *
17  * This is free software.  You are permitted to use,
18  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
19  */
20 //backend device for Virtio NIC, Direct Network Bridge
21
22 #include <palacios/vmm.h>
23 #include <palacios/vmm_dev_mgr.h>
24 #include <palacios/vm_guest_mem.h>
25 #include <palacios/vmm_sprintf.h>
26 #include <interfaces/vmm_packet.h>
27
28 #ifndef V3_CONFIG_DEBUG_NIC_BRIDGE
29 #undef PrintDebug
30 #define PrintDebug(fmt, args...)
31 #endif
32
33 struct nic_bridge_state {
34     struct v3_vm_info * vm;
35     struct v3_dev_net_ops net_ops;
36     struct v3_packet * packet_state;
37 };
38
39 static int bridge_send(uint8_t * buf, uint32_t len, 
40                        void * private_data) {
41     struct nic_bridge_state * bridge = (struct nic_bridge_state *)private_data;
42     
43 #ifdef V3_CONFIG_DEBUG_NIC_BRIDGE
44     {
45         PrintDebug("NIC Bridge: send pkt size: %d\n", len);
46         v3_hexdump(buf, len, NULL, 0);
47     }
48 #endif
49     
50     return v3_packet_send(bridge->packet_state, buf, len);
51 }
52
53 static int packet_input(struct v3_packet * packet_state, uint8_t * pkt, uint32_t size) {
54     struct nic_bridge_state * bridge = (struct nic_bridge_state *)packet_state->guest_packet_data;
55     
56 #ifdef V3_CONFIG_DEBUG_NIC_BRIDGE
57     {
58         PrintDebug("NIC Bridge: recv pkt size: %d\n", size);
59         v3_hexdump(pkt, size, NULL, 0);
60     }
61 #endif
62     
63     return bridge->net_ops.recv(pkt, 
64                                 size, 
65                                 bridge->net_ops.config.frontend_data);
66 }
67
68
69 static int nic_bridge_free(struct nic_bridge_state * bridge) {
70     /*TODO: detach from front device */
71     
72     v3_packet_close(bridge->packet_state);
73     V3_Free(bridge);
74         
75     return 0;
76 }
77
78 static struct v3_device_ops dev_ops = {
79     .free = (int (*)(void *))nic_bridge_free,
80     
81 };
82
83 static int nic_bridge_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
84     struct nic_bridge_state * bridge = NULL;
85     char * dev_id = v3_cfg_val(cfg, "ID");
86     char * host_nic;
87     
88     v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend");
89     
90     v3_cfg_tree_t * hostnic_cfg = v3_cfg_subtree(cfg, "hostnic");
91     host_nic = v3_cfg_val(hostnic_cfg, "name"); 
92     if(host_nic == NULL) {
93         host_nic = "eth0";
94     }
95     
96     bridge = (struct nic_bridge_state *)V3_Malloc(sizeof(struct nic_bridge_state));
97     memset(bridge, 0, sizeof(struct nic_bridge_state));
98     
99     struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, bridge);
100     
101     if (dev == NULL) {
102         PrintError("Could not attach device %s\n", dev_id);
103         V3_Free(bridge);
104         return -1;
105     }
106     
107     bridge->net_ops.send = bridge_send;
108     bridge->vm = vm;
109     
110     if (v3_dev_connect_net(vm, v3_cfg_val(frontend_cfg, "tag"), 
111                            &(bridge->net_ops), frontend_cfg, bridge) == -1) {
112         PrintError("Could not connect %s to frontend %s\n", 
113                    dev_id, v3_cfg_val(frontend_cfg, "tag"));
114         v3_remove_device(dev);
115         return -1;
116     }
117     
118     PrintDebug("NIC-Bridge: Connect %s to frontend %s\n", 
119                dev_id, v3_cfg_val(frontend_cfg, "tag"));
120     
121     bridge->packet_state = v3_packet_connect(vm, host_nic, 
122                                              bridge->net_ops.config.fnt_mac, 
123                                              packet_input, 
124                                              (void *)bridge);
125     
126     if(bridge->packet_state == NULL){
127         PrintError("NIC-Bridge: Error to connect to host ethernet device\n");
128         return -1;
129     }
130     
131     return 0;
132 }
133
134 device_register("NIC_BRIDGE", nic_bridge_init)