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, Direct Network Bridge
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 <palacios/vmm_packet.h>
28 #ifndef CONFIG_DEBUG_NIC_BRIDGE
30 #define PrintDebug(fmt, args...)
33 struct nic_bridge_state {
34 struct v3_vm_info * vm;
35 struct v3_dev_net_ops net_ops;
38 static int bridge_send(uint8_t * buf, uint32_t len,
39 void * private_data) {
40 //struct nic_bridge_state *bridge = (struct nic_bridge_state *)private_data;
42 #ifdef CONFIG_DEBUG_NIC_BRIDGE
44 PrintDebug("NIC Bridge: send pkt size: %d\n", len);
45 //v3_hexdump(buf, len, NULL, 0);
49 return V3_send_raw(buf, len);
52 static int packet_input(struct v3_vm_info * vm,
53 struct v3_packet_event * evt,
54 void * private_data) {
55 struct nic_bridge_state * bridge = (struct nic_bridge_state *)private_data;
57 PrintDebug("NIC_BRIDGE: Incoming packet size: %d\n", evt->size);
59 return bridge->net_ops.recv(evt->pkt,
61 bridge->net_ops.frontend_data);
65 static int vnet_nic_free(struct vm_device * dev) {
66 struct nic_bridge_state * bridge = dev->private_data;
68 /*detach from front device */
75 static struct v3_device_ops dev_ops = {
76 .free = vnet_nic_free,
80 static int vnet_nic_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
81 struct nic_bridge_state * bridge = NULL;
82 char * dev_id = v3_cfg_val(cfg, "ID");
84 v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend");
86 bridge = (struct nic_bridge_state *)V3_Malloc(sizeof(struct nic_bridge_state));
87 memset(bridge, 0, sizeof(struct nic_bridge_state));
89 struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, bridge);
92 PrintError("Could not attach device %s\n", dev_id);
97 bridge->net_ops.send = bridge_send;
100 if (v3_dev_connect_net(vm, v3_cfg_val(frontend_cfg, "tag"),
101 &(bridge->net_ops), frontend_cfg, bridge) == -1) {
102 PrintError("Could not connect %s to frontend %s\n",
103 dev_id, v3_cfg_val(frontend_cfg, "tag"));
104 v3_remove_device(dev);
108 PrintDebug("NIC-Bridge: Connect %s to frontend %s\n",
109 dev_id, v3_cfg_val(frontend_cfg, "tag"));
111 v3_hook_host_event(vm, HOST_PACKET_EVT, V3_HOST_EVENT_HANDLER(packet_input), bridge);
116 device_register("NIC_BRIDGE", vnet_nic_init)