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 #ifdef CONFIG_DEBUG_NIC_BRIDGE
59 PrintDebug("NIC Bridge: recv pkt size: %d\n", evt->size);
60 v3_hexdump(evt->pkt, evt->size, NULL, 0);
64 return bridge->net_ops.recv(evt->pkt,
66 bridge->net_ops.frontend_data);
70 static int nic_bridge_free(struct nic_bridge_state * bridge) {
72 /*detach from front device */
79 static struct v3_device_ops dev_ops = {
80 .free = (int (*)(void *))nic_bridge_free,
84 static int nic_bridge_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
85 struct nic_bridge_state * bridge = NULL;
86 char * dev_id = v3_cfg_val(cfg, "ID");
88 v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend");
90 bridge = (struct nic_bridge_state *)V3_Malloc(sizeof(struct nic_bridge_state));
91 memset(bridge, 0, sizeof(struct nic_bridge_state));
93 struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, bridge);
96 PrintError("Could not attach device %s\n", dev_id);
101 bridge->net_ops.send = bridge_send;
104 if (v3_dev_connect_net(vm, v3_cfg_val(frontend_cfg, "tag"),
105 &(bridge->net_ops), frontend_cfg, bridge) == -1) {
106 PrintError("Could not connect %s to frontend %s\n",
107 dev_id, v3_cfg_val(frontend_cfg, "tag"));
108 v3_remove_device(dev);
112 PrintDebug("NIC-Bridge: Connect %s to frontend %s\n",
113 dev_id, v3_cfg_val(frontend_cfg, "tag"));
115 v3_hook_host_event(vm, HOST_PACKET_EVT, V3_HOST_EVENT_HANDLER(packet_input), bridge);
120 device_register("NIC_BRIDGE", nic_bridge_init)