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);
53 static int packet_input(struct v3_vm_info * vm,
54 struct v3_packet_event * evt,
55 void * private_data) {
56 struct nic_bridge_state * bridge = (struct nic_bridge_state *)private_data;
58 PrintDebug("NIC_BRIDGE: Incoming packet size: %d\n", evt->size);
60 return bridge->net_ops.recv(evt->pkt,
62 bridge->net_ops.frontend_data);
66 static int vnet_nic_free(struct vm_device * dev) {
67 struct nic_bridge_state * bridge = dev->private_data;
69 /*detach from front device */
76 static struct v3_device_ops dev_ops = {
77 .free = vnet_nic_free,
81 static int vnet_nic_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
82 struct nic_bridge_state * bridge = NULL;
83 char * dev_id = v3_cfg_val(cfg, "ID");
85 v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend");
87 bridge = (struct nic_bridge_state *)V3_Malloc(sizeof(struct nic_bridge_state));
88 memset(bridge, 0, sizeof(struct nic_bridge_state));
90 struct vm_device * dev = v3_allocate_device(dev_id, &dev_ops, bridge);
92 if (v3_attach_device(vm, dev) == -1) {
93 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"));
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)