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.


Enhancements to VNET and to halting:
[palacios.git] / palacios / src / devices / vnet_nic.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
21
22 #include <vnet/vnet.h>
23 #include <palacios/vmm.h>
24 #include <palacios/vmm_dev_mgr.h>
25 #include <devices/lnx_virtio_pci.h>
26 #include <palacios/vm_guest_mem.h>
27 #include <devices/pci.h>
28 #include <palacios/vmm_sprintf.h>
29 #include <palacios/vmm_ethernet.h>
30
31 #ifndef V3_CONFIG_DEBUG_VNET_NIC
32 #undef PrintDebug
33 #define PrintDebug(fmt, args...)
34 #endif
35
36 struct vnet_nic_state {
37     struct v3_vm_info * vm;
38     struct v3_dev_net_ops net_ops;
39     int vnet_dev_id;
40 };
41
42
43 /* called by frontend, send pkt to VNET */
44 static int vnet_nic_send(uint8_t * buf, uint32_t len, 
45                          void * private_data) {
46     struct vnet_nic_state * vnetnic = (struct vnet_nic_state *)private_data;
47
48     struct v3_vnet_pkt pkt;
49     pkt.size = len;
50     pkt.src_type = LINK_INTERFACE;
51     pkt.src_id = vnetnic->vnet_dev_id;
52     memcpy(pkt.header, buf, ETHERNET_HEADER_LEN);
53     pkt.data = buf;
54
55     V3_Net_Print(2, "VNET-NIC: send pkt (size: %d, src_id: %d, src_type: %d)\n", 
56                    pkt.size, pkt.src_id, pkt.src_type);
57     if(net_debug >= 4){
58         v3_hexdump(buf, len, NULL, 0);
59     }
60
61     return v3_vnet_send_pkt(&pkt, NULL);
62 }
63
64
65 /* send pkt to frontend device */
66 static int fnt_input(struct v3_vm_info * info, 
67                         struct v3_vnet_pkt * pkt, 
68                         void * private_data){
69     struct vnet_nic_state *vnetnic = (struct vnet_nic_state *)private_data;
70
71     V3_Net_Print(2, "VNET-NIC: receive pkt (size %d, src_id:%d, src_type: %d, dst_id: %d, dst_type: %d)\n", 
72                 pkt->size, pkt->src_id, pkt->src_type, pkt->dst_id, pkt->dst_type);
73         
74     return vnetnic->net_ops.recv(pkt->data, pkt->size,
75                                  vnetnic->net_ops.config.frontend_data);
76 }
77
78
79 /* poll pkt from frontend device */
80 static int fnt_poll(struct v3_vm_info * info,
81                         int quote, void * private_data){
82     struct vnet_nic_state *vnetnic = (struct vnet_nic_state *)private_data;
83
84     return vnetnic->net_ops.poll(quote, vnetnic->net_ops.config.frontend_data);
85 }
86
87
88 static int vnet_nic_free(struct vnet_nic_state * vnetnic) {
89
90     v3_vnet_del_dev(vnetnic->vnet_dev_id);
91     V3_Free(vnetnic);
92         
93     return 0;
94 }
95
96 static struct v3_device_ops dev_ops = {
97     .free = (int (*)(void *))vnet_nic_free,
98
99 };
100
101 static struct v3_vnet_dev_ops vnet_dev_ops = {
102     .input = fnt_input,
103     .poll = fnt_poll,
104 };
105
106
107 static int vnet_nic_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
108     struct vnet_nic_state * vnetnic = NULL;
109     char * dev_id = v3_cfg_val(cfg, "ID");
110     int vnet_dev_id;
111
112     v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend");
113
114     if (!frontend_cfg || !(v3_cfg_val(frontend_cfg, "tag"))) { 
115         PrintError("No frontend config specified, or frontend has no tag\n");
116         return -1;
117     }
118
119     vnetnic = (struct vnet_nic_state *)V3_Malloc(sizeof(struct vnet_nic_state));
120
121     if (!vnetnic) {
122         PrintError("Cannot allocate in init\n");
123         return -1;
124     }
125
126     memset(vnetnic, 0, sizeof(struct vnet_nic_state));
127
128     struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, vnetnic);
129     if (dev == NULL) {
130         PrintError("Could not attach device %s\n", dev_id);
131         V3_Free(vnetnic);
132         return -1;
133     }
134
135     vnetnic->net_ops.send = vnet_nic_send;
136     vnetnic->vm = vm;
137         
138     if (v3_dev_connect_net(vm, v3_cfg_val(frontend_cfg, "tag"), 
139                            &(vnetnic->net_ops), frontend_cfg, vnetnic) == -1) {
140         PrintError("Could not connect %s to frontend %s\n", 
141                    dev_id, v3_cfg_val(frontend_cfg, "tag"));
142         v3_remove_device(dev);
143         return -1;
144     }
145
146     PrintDebug("Vnet-nic: Connect %s to frontend %s\n", 
147                dev_id, v3_cfg_val(frontend_cfg, "tag"));
148
149     if ((vnet_dev_id = v3_vnet_add_dev(vm, vnetnic->net_ops.config.fnt_mac, 
150                                        &vnet_dev_ops, vnetnic->net_ops.config.quote, 
151                                        vnetnic->net_ops.config.poll, (void *)vnetnic)) == -1) {
152         PrintError("Vnet-nic device %s fails to registered to VNET\n", dev_id);
153         
154         v3_remove_device(dev);
155         return 0;
156     }
157     vnetnic->vnet_dev_id = vnet_dev_id;
158
159     return 0;
160 }
161
162 device_register("VNET_NIC", vnet_nic_init)