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 nic_bridge_state * bridge) {
67 /*detach from front device */
74 static struct v3_device_ops dev_ops = {
75 .free = (int (*)(void *))vnet_nic_free,
79 static int vnet_nic_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
80 struct nic_bridge_state * bridge = NULL;
81 char * dev_id = v3_cfg_val(cfg, "ID");
83 v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend");
85 bridge = (struct nic_bridge_state *)V3_Malloc(sizeof(struct nic_bridge_state));
86 memset(bridge, 0, sizeof(struct nic_bridge_state));
88 struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, bridge);
91 PrintError("Could not attach device %s\n", dev_id);
96 bridge->net_ops.send = bridge_send;
99 if (v3_dev_connect_net(vm, v3_cfg_val(frontend_cfg, "tag"),
100 &(bridge->net_ops), frontend_cfg, bridge) == -1) {
101 PrintError("Could not connect %s to frontend %s\n",
102 dev_id, v3_cfg_val(frontend_cfg, "tag"));
103 v3_remove_device(dev);
107 PrintDebug("NIC-Bridge: Connect %s to frontend %s\n",
108 dev_id, v3_cfg_val(frontend_cfg, "tag"));
110 v3_hook_host_event(vm, HOST_PACKET_EVT, V3_HOST_EVENT_HANDLER(packet_input), bridge);
115 device_register("NIC_BRIDGE", vnet_nic_init)