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) 2008, Lei Xia <lxia@northwestern.edu>
11 * Copyright (c) 2008, 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 guest_info * core;
39 struct v3_dev_net_ops net_ops;
43 //used when virtio_nic get a packet from guest and send it to the backend
44 static int vnet_send(uint8_t * buf, uint32_t len, void * private_data, struct vm_device *dest_dev){
45 struct v3_vnet_pkt * pkt = NULL;
46 struct guest_info *core = (struct guest_info *)private_data;
48 #ifdef WCONFIG_DEBUG_VNET_NIC
50 PrintDebug("Virtio VNET-NIC: send pkt size: %d\n", len);
51 v3_hexdump(buf, len, NULL, 0);
55 #ifdef CONFIG_VNET_PROFILE
60 pkt = (struct v3_vnet_pkt *)V3_Malloc(sizeof(struct v3_vnet_pkt));
63 PrintError("Malloc() fails");
67 #ifdef CONFIG_VNET_PROFILE
70 core->vnet_times.time_mallocfree = end - start;
76 pkt->src_type = LINK_INTERFACE;
78 memcpy(pkt->data, buf, pkt->size);
80 #ifdef CONFIG_VNET_PROFILE
83 core->vnet_times.memcpy_time = start - end;
84 core->vnet_times.time_copy_from_guest = start - core->vnet_times.virtio_handle_start - core->vnet_times.time_mallocfree;
88 v3_vnet_send_pkt(pkt, (void *)core);
90 #ifdef CONFIG_VNET_PROFILE
96 #ifdef CONFIG_VNET_PROFILE
99 core->vnet_times.time_mallocfree += end - start;
109 //alternative way, no malloc/free
110 static int vnet_send(uint8_t * buf, uint32_t len, void * private_data, struct vm_device *dest_dev){
111 struct v3_vnet_pkt pkt;
112 struct guest_info *core = (struct guest_info *)private_data;
114 #ifdef CONFIG_DEBUG_VNET_NIC
116 PrintDebug("Virtio VNET-NIC: send pkt size: %d\n", len);
117 v3_hexdump(buf, len, NULL, 0);
121 #ifdef CONFIG_VNET_PROFILE
127 pkt.src_type = LINK_INTERFACE;
129 memcpy(pkt.data, buf, pkt.size);
131 #ifdef CONFIG_VNET_PROFILE
133 core->vnet_times.time_copy_from_guest = end - core->vnet_times.virtio_handle_start;
134 core->vnet_times.memcpy_time = end - start;
137 v3_vnet_send_pkt(&pkt, (void *)core);
144 //alternative way, no malloc/free, no copy
145 //need to change pkt format
146 static int vnet_send(uint8_t * buf, uint32_t len, void * private_data, struct vm_device *dest_dev){
147 struct v3_vnet_pkt pkt;
148 struct guest_info *core = (struct guest_info *)private_data;
151 #ifdef CONFIG_DEBUG_VNET_NIC
153 PrintDebug("Virtio VNET-NIC: send pkt size: %d\n", len);
154 v3_hexdump(buf, len, NULL, 0);
161 #ifdef CONFIG_VNET_PROFILE
166 v3_vnet_send_pkt(&pkt, (void *)core);
168 #ifdef CONFIG_VNET_PROFILE
171 core->vnet_times.vnet_handle_time = end - start;
180 static int virtio_input(struct v3_vm_info *info, struct v3_vnet_pkt * pkt, void * private_data){
181 struct vnet_nic_state *vnetnic = (struct vnet_nic_state *)private_data;
183 PrintDebug("Vnet-nic: In input: vnet_nic state %p\n", vnetnic);
185 return vnetnic->net_ops.recv(pkt->data, pkt->size, vnetnic->net_ops.frontend_data);
188 static int register_to_vnet(struct v3_vm_info * vm,
189 struct vnet_nic_state *vnet_nic,
193 PrintDebug("Vnet-nic: register Vnet-nic device %s, state %p to VNET\n", dev_name, vnet_nic);
195 return v3_vnet_add_dev(vm, mac, virtio_input, (void *)vnet_nic);
198 static int vnet_nic_free(struct vm_device * dev) {
202 static struct v3_device_ops dev_ops = {
203 .free = vnet_nic_free,
210 static int str2mac(char *macstr, char mac[6]){
211 char hex[2], *s = macstr;
216 mac[i++] = (char)atox(hex);
217 if (i == 6) return 0;
225 static int vnet_nic_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
226 struct vnet_nic_state * vnetnic = NULL;
227 char * name = v3_cfg_val(cfg, "name");
228 char * macstr = NULL;
232 v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend");
233 macstr = v3_cfg_val(frontend_cfg, "mac");
235 if (macstr == NULL) {
236 PrintDebug("Vnet-nic: No Mac specified\n");
238 str2mac(macstr, mac);
241 vnetnic = (struct vnet_nic_state *)V3_Malloc(sizeof(struct vnet_nic_state));
242 memset(vnetnic, 0, sizeof(struct vnet_nic_state));
244 struct vm_device * dev = v3_allocate_device(name, &dev_ops, vnetnic);
246 if (v3_attach_device(vm, dev) == -1) {
247 PrintError("Could not attach device %s\n", name);
251 vnetnic->net_ops.send = vnet_send;
252 memcpy(vnetnic->mac, mac, 6);
254 if (v3_dev_connect_net(vm, v3_cfg_val(frontend_cfg, "tag"),
255 &(vnetnic->net_ops), frontend_cfg, vnetnic) == -1) {
256 PrintError("Could not connect %s to frontend %s\n",
257 name, v3_cfg_val(frontend_cfg, "tag"));
261 PrintDebug("Vnet-nic: Connect %s to frontend %s\n",
262 name, v3_cfg_val(frontend_cfg, "tag"));
264 if ((vnet_dev_id = register_to_vnet(vm, vnetnic, name, vnetnic->mac)) == -1) {
265 PrintError("Vnet-nic device %s (mac: %s) fails to registered to VNET\n", name, macstr);
268 PrintDebug("Vnet-nic device %s (mac: %s, %ld) registered to VNET\n", name, macstr, *((ulong_t *)vnetnic->mac));
274 uchar_t tapmac[6] = {0x00,0x02,0x55,0x67,0x42,0x39}; //for Intel-VT test HW
275 //uchar_t tapmac[6] = {0x6e,0xa8,0x75,0xf4,0x82,0x95};
276 uchar_t dstmac[6] = {0xff,0xff,0xff,0xff,0xff,0xff};
277 uchar_t zeromac[6] = {0,0,0,0,0,0};
279 struct v3_vnet_route route;
280 route.dst_id = vnet_dev_id;
281 route.dst_type = LINK_INTERFACE;
283 route.src_type = LINK_ANY;
285 if(!strcmp(name, "vnet_nicdom0")){
286 memcpy(route.dst_mac, tapmac, 6);
287 route.dst_mac_qual = MAC_NONE;
288 memcpy(route.src_mac, zeromac, 6);
289 route.src_mac_qual = MAC_ANY;
291 v3_vnet_add_route(route);
293 memcpy(route.dst_mac, dstmac, 6);
294 route.dst_mac_qual = MAC_NONE;
295 memcpy(route.src_mac, tapmac, 6);
296 route.src_mac_qual = MAC_NOT;
298 v3_vnet_add_route(route);
301 if (!strcmp(name, "vnet_nic0")){
302 memcpy(route.dst_mac, zeromac, 6);
303 route.dst_mac_qual = MAC_ANY;
304 memcpy(route.src_mac, tapmac, 6);
305 route.src_mac_qual = MAC_NONE;
307 v3_vnet_add_route(route);
316 device_register("VNET_NIC", vnet_nic_init)