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) 2010, Lei Xia <lxia@northwestern.edu>
11 * Copyright (c) 2010, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Lei Xia <lxia@northwestern.edu>
17 * This is free software. You are permitted to use,
18 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 //backend device for Virtio NIC
22 #include <palacios/vmm_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>
30 #ifndef CONFIG_DEBUG_VNET_NIC
32 #define PrintDebug(fmt, args...)
36 struct vnet_nic_state {
38 struct v3_vm_info * vm;
39 struct v3_dev_net_ops net_ops;
43 /* called by frontend device,
44 * tell the VNET can start sending pkt to it */
45 static void start_rx(void * private_data){
46 //struct vnet_nic_state *vnetnic = (struct vnet_nic_state *)private_data;
48 //v3_vnet_enable_device(vnetnic->vnet_dev_id);
51 /* called by frontend device,
52 * tell the VNET stop sending pkt to it */
53 static void stop_rx(void * private_data){
54 //struct vnet_nic_state *vnetnic = (struct vnet_nic_state *)private_data;
56 //v3_vnet_disable_device(vnetnic->vnet_dev_id);
59 /* called by frontend, send pkt to VNET */
60 static int vnet_nic_send(uint8_t * buf, uint32_t len,
62 struct vm_device * dest_dev){
63 struct vnet_nic_state *vnetnic = (struct vnet_nic_state *)private_data;
65 struct v3_vnet_pkt pkt;
67 pkt.src_type = LINK_INTERFACE;
68 pkt.src_id = vnetnic->vnet_dev_id;
69 memcpy(pkt.header, buf, ETHERNET_HEADER_LEN);
72 #ifdef CONFIG_DEBUG_VNET_NIC
74 PrintDebug("VNET-NIC: send pkt (size: %d, src_id: %d, src_type: %d)\n",
75 pkt.size, pkt.src_id, pkt.src_type);
76 //v3_hexdump(buf, len, NULL, 0);
80 return v3_vnet_send_pkt(&pkt, NULL);;
84 /* send pkt to frontend device */
85 static int virtio_input(struct v3_vm_info * info,
86 struct v3_vnet_pkt * pkt,
88 struct vnet_nic_state *vnetnic = (struct vnet_nic_state *)private_data;
90 PrintDebug("VNET-NIC: receive pkt (size %d, src_id:%d, src_type: %d, dst_id: %d, dst_type: %d)\n",
91 pkt->size, pkt->src_id, pkt->src_type, pkt->dst_id, pkt->dst_type);
93 return vnetnic->net_ops.recv(pkt->data, pkt->size,
94 vnetnic->net_ops.frontend_data);
97 /* tell frontend device to poll data from guest */
98 static void virtio_poll(struct v3_vm_info * info,
100 struct vnet_nic_state *vnetnic = (struct vnet_nic_state *)private_data;
102 vnetnic->net_ops.poll(info, vnetnic->net_ops.frontend_data);
106 /* tell the frontend to start sending pkt to VNET*/
107 static void start_tx(void * private_data){
108 struct vnet_nic_state *vnetnic = (struct vnet_nic_state *)private_data;
110 vnetnic->net_ops.start_tx(vnetnic->net_ops.frontend_data);
113 /* tell the frontend device to stop sending pkt to VNET*/
114 static void stop_tx(void * private_data){
115 struct vnet_nic_state *vnetnic = (struct vnet_nic_state *)private_data;
117 vnetnic->net_ops.stop_tx(vnetnic->net_ops.frontend_data);
121 static int vnet_nic_free(struct vm_device * dev) {
125 static struct v3_device_ops dev_ops = {
126 .free = vnet_nic_free,
132 static struct v3_vnet_dev_ops vnet_dev_ops = {
133 .input = virtio_input,
135 .start_tx = start_tx,
140 static int register_to_vnet(struct v3_vm_info * vm,
141 struct vnet_nic_state * vnet_nic,
142 char * dev_name, uchar_t mac[6]) {
144 PrintDebug("Vnet-nic: register Vnet-nic device %s, state %p to VNET\n", dev_name, vnet_nic);
146 return v3_vnet_add_dev(vm, mac, &vnet_dev_ops, (void *)vnet_nic);
150 static int str2mac(char * macstr, char mac[6]){
151 char hex[2], *s = macstr;
156 mac[i++] = (char)atox(hex);
157 if (i == 6) return 0;
165 static int vnet_nic_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
166 struct vnet_nic_state * vnetnic = NULL;
167 char * dev_id = v3_cfg_val(cfg, "ID");
168 char * macstr = NULL;
172 v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend");
173 macstr = v3_cfg_val(frontend_cfg, "mac");
175 if (macstr == NULL) {
176 PrintDebug("Vnet-nic: No Mac specified\n");
178 str2mac(macstr, mac);
181 vnetnic = (struct vnet_nic_state *)V3_Malloc(sizeof(struct vnet_nic_state));
182 memset(vnetnic, 0, sizeof(struct vnet_nic_state));
184 struct vm_device * dev = v3_allocate_device(dev_id, &dev_ops, vnetnic);
186 if (v3_attach_device(vm, dev) == -1) {
187 PrintError("Could not attach device %s\n", dev_id);
191 vnetnic->net_ops.send = vnet_nic_send;
192 vnetnic->net_ops.start_rx = start_rx;
193 vnetnic->net_ops.stop_rx = stop_rx;
194 memcpy(vnetnic->mac, mac, 6);
197 if (v3_dev_connect_net(vm, v3_cfg_val(frontend_cfg, "tag"),
198 &(vnetnic->net_ops), frontend_cfg, vnetnic) == -1) {
199 PrintError("Could not connect %s to frontend %s\n",
200 dev_id, v3_cfg_val(frontend_cfg, "tag"));
204 PrintDebug("Vnet-nic: Connect %s to frontend %s\n",
205 dev_id, v3_cfg_val(frontend_cfg, "tag"));
207 if ((vnet_dev_id = register_to_vnet(vm, vnetnic, dev_id, vnetnic->mac)) == -1) {
208 PrintError("Vnet-nic device %s (mac: %s) fails to registered to VNET\n", dev_id, macstr);
210 vnetnic->vnet_dev_id = vnet_dev_id;
212 PrintDebug("Vnet-nic device %s (mac: %s, %ld) registered to VNET\n",
213 dev_id, macstr, *((ulong_t *)vnetnic->mac));
216 //for temporary hack for vnet bridge test
219 uchar_t zeromac[6] = {0,0,0,0,0,0};
221 if(!strcmp(dev_id, "vnet_nic")){
222 struct v3_vnet_route route;
224 route.dst_id = vnet_dev_id;
225 route.dst_type = LINK_INTERFACE;
227 route.src_type = LINK_EDGE;
228 memcpy(route.dst_mac, zeromac, 6);
229 route.dst_mac_qual = MAC_ANY;
230 memcpy(route.src_mac, zeromac, 6);
231 route.src_mac_qual = MAC_ANY;
232 v3_vnet_add_route(route);
236 route.dst_type = LINK_EDGE;
237 route.src_id = vnet_dev_id;
238 route.src_type = LINK_INTERFACE;
239 memcpy(route.dst_mac, zeromac, 6);
240 route.dst_mac_qual = MAC_ANY;
241 memcpy(route.src_mac, zeromac, 6);
242 route.src_mac_qual = MAC_ANY;
244 v3_vnet_add_route(route);
249 //for temporary hack for Linux bridge (w/o encapuslation) test
252 static int vnet_nic_guestid = -1;
253 static int vnet_nic_dom0 = -1;
254 uchar_t zeromac[6] = {0,0,0,0,0,0};
256 if(!strcmp(dev_id, "vnet_nic")){ //domu
257 vnet_nic_guestid = vnet_dev_id;
259 if (!strcmp(dev_id, "vnet_nic_dom0")){
260 vnet_nic_dom0 = vnet_dev_id;
263 if(vnet_nic_guestid != -1 && vnet_nic_dom0 !=-1){
264 struct v3_vnet_route route;
266 route.src_id = vnet_nic_guestid;
267 route.src_type = LINK_INTERFACE;
268 route.dst_id = vnet_nic_dom0;
269 route.dst_type = LINK_INTERFACE;
270 memcpy(route.dst_mac, zeromac, 6);
271 route.dst_mac_qual = MAC_ANY;
272 memcpy(route.src_mac, zeromac, 6);
273 route.src_mac_qual = MAC_ANY;
274 v3_vnet_add_route(route);
277 route.src_id = vnet_nic_dom0;
278 route.src_type = LINK_INTERFACE;
279 route.dst_id = vnet_nic_guestid;
280 route.dst_type = LINK_INTERFACE;
281 memcpy(route.dst_mac, zeromac, 6);
282 route.dst_mac_qual = MAC_ANY;
283 memcpy(route.src_mac, zeromac, 6);
284 route.src_mac_qual = MAC_ANY;
286 v3_vnet_add_route(route);
294 device_register("VNET_NIC", vnet_nic_init)