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 v3_vm_info * vm;
39 struct v3_dev_net_ops net_ops;
44 static int vnet_send(uint8_t * buf, uint32_t len, void * private_data, struct vm_device * dest_dev){
45 struct vnet_nic_state * vnetnic = (struct vnet_nic_state *)private_data;
46 struct v3_vnet_pkt pkt;
49 pkt.src_type = LINK_INTERFACE;
50 pkt.src_id = vnetnic->vnet_dev_id;
51 memcpy(pkt.header, buf, ETHERNET_HEADER_LEN);
54 #ifdef CONFIG_DEBUG_VNET_NIC
56 PrintDebug("Virtio VNET-NIC: send pkt size: %d, pkt src_id: %d\n",
57 len, vnetnic->vnet_dev_id);
58 v3_hexdump(buf, len, NULL, 0);
62 v3_vnet_rx(buf, len, vnetnic->vnet_dev_id, LINK_INTERFACE);
64 //if on the dom0 core, interrupt the domU core to poll pkts
65 //otherwise, call the polling directly
66 int cpu = V3_Get_CPU();
68 v3_interrupt_cpu(vnetnic->vm, cpu, V3_VNET_POLLING_VECTOR);
71 v3_vnet_send_pkt(&pkt, NULL);
76 static int virtio_input(struct v3_vm_info *info, struct v3_vnet_pkt * pkt, void * private_data){
77 struct vnet_nic_state *vnetnic = (struct vnet_nic_state *)private_data;
79 PrintDebug("Vnet-nic: In input: vnet_nic state %p\n", vnetnic);
81 return vnetnic->net_ops.recv(pkt->data, pkt->size, vnetnic->net_ops.frontend_data);
84 static int register_to_vnet(struct v3_vm_info * vm,
85 struct vnet_nic_state * vnet_nic,
89 PrintDebug("Vnet-nic: register Vnet-nic device %s, state %p to VNET\n", dev_name, vnet_nic);
91 return v3_vnet_add_dev(vm, mac, virtio_input, (void *)vnet_nic);
94 static int vnet_nic_free(struct vm_device * dev) {
98 static struct v3_device_ops dev_ops = {
99 .free = vnet_nic_free,
106 static int str2mac(char * macstr, uint8_t mac[6]){
113 mac[i++] = (char)atox(hex);
129 static int vnet_nic_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
130 struct vnet_nic_state * vnetnic = NULL;
131 char * dev_id = v3_cfg_val(cfg, "ID");
132 char * macstr = NULL;
134 v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend");
136 macstr = v3_cfg_val(frontend_cfg, "mac");
138 if (macstr == NULL) {
139 PrintDebug("Vnet-nic configuration error: No Mac specified\n");
143 vnetnic = (struct vnet_nic_state *)V3_Malloc(sizeof(struct vnet_nic_state));
144 memset(vnetnic, 0, sizeof(struct vnet_nic_state));
146 struct vm_device * dev = v3_allocate_device(dev_id, &dev_ops, vnetnic);
148 if (v3_attach_device(vm, dev) == -1) {
149 PrintError("Could not attach device %s\n", dev_id);
155 vnetnic->net_ops.send = vnet_send;
156 str2mac(macstr, vnetnic->mac);
159 if (v3_dev_connect_net(vm, v3_cfg_val(frontend_cfg, "tag"),
160 &(vnetnic->net_ops), frontend_cfg, vnetnic) == -1) {
161 PrintError("Could not connect %s to frontend %s\n",
162 dev_id, v3_cfg_val(frontend_cfg, "tag"));
166 PrintDebug("Vnet-nic: Connect %s to frontend %s\n",
167 dev_id, v3_cfg_val(frontend_cfg, "tag"));
169 if ((vnet_dev_id = register_to_vnet(vm, vnetnic, dev_id, vnetnic->mac)) == -1) {
170 PrintError("Vnet-nic device %s (mac: %s) fails to registered to VNET\n", dev_id, macstr);
174 vnetnic->vnet_dev_id = vnet_dev_id;
176 PrintDebug("Vnet-nic device %s (mac: %s, %ld) registered to VNET\n", dev_id, macstr, *((uint32_t *)vnetnic->mac));
182 device_register("VNET_NIC", vnet_nic_init)